* readelf.c (decode_location_expression): Loop through multiple
[deliverable/binutils-gdb.git] / bfd / coffgen.c
CommitLineData
252b5132 1/* Support for the generic parts of COFF, for BFD.
363d7b14 2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
24
25/* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
28 put it another way,
29
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
34
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
37 coff_data (abfd). */
38
39#include "bfd.h"
40#include "sysdep.h"
41#include "libbfd.h"
42#include "coff/internal.h"
43#include "libcoff.h"
44
45static void coff_fix_symbol_name
46 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
47 asection **, bfd_size_type *));
48static boolean coff_write_symbol
49 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
50 bfd_size_type *, asection **, bfd_size_type *));
51static boolean coff_write_alien_symbol
52 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
53 asection **, bfd_size_type *));
54static boolean coff_write_native_symbol
55 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
56 asection **, bfd_size_type *));
57static void coff_pointerize_aux
58 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
59 unsigned int, combined_entry_type *));
60static boolean make_a_section_from_file
61 PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
62static const bfd_target *coff_real_object_p
63 PARAMS ((bfd *, unsigned, struct internal_filehdr *,
64 struct internal_aouthdr *));
65static void fixup_symbol_value
66 PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
67static char *build_debug_section
68 PARAMS ((bfd *));
69static char *copy_name
70 PARAMS ((bfd *, char *, int));
71
72#define STRING_SIZE_SIZE (4)
73
74/* Take a section header read from a coff file (in HOST byte order),
75 and make a BFD "section" out of it. This is used by ECOFF. */
76static boolean
77make_a_section_from_file (abfd, hdr, target_index)
78 bfd *abfd;
79 struct internal_scnhdr *hdr;
80 unsigned int target_index;
81{
82 asection *return_section;
83 char *name;
84
85 name = NULL;
86
87 /* Handle long section names as in PE. */
88 if (bfd_coff_long_section_names (abfd)
89 && hdr->s_name[0] == '/')
90 {
91 char buf[SCNNMLEN];
92 long strindex;
93 char *p;
94 const char *strings;
95
96 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
97 buf[SCNNMLEN - 1] = '\0';
98 strindex = strtol (buf, &p, 10);
99 if (*p == '\0' && strindex >= 0)
100 {
101 strings = _bfd_coff_read_string_table (abfd);
102 if (strings == NULL)
103 return false;
104 /* FIXME: For extra safety, we should make sure that
105 strindex does not run us past the end, but right now we
106 don't know the length of the string table. */
107 strings += strindex;
108 name = bfd_alloc (abfd, strlen (strings) + 1);
109 if (name == NULL)
110 return false;
111 strcpy (name, strings);
112 }
113 }
114
115 if (name == NULL)
116 {
117 /* Assorted wastage to null-terminate the name, thanks AT&T! */
118 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
119 if (name == NULL)
120 return false;
121 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
122 name[sizeof (hdr->s_name)] = 0;
123 }
124
125 return_section = bfd_make_section_anyway (abfd, name);
126 if (return_section == NULL)
127 return false;
128
129 return_section->vma = hdr->s_vaddr;
130 return_section->lma = hdr->s_paddr;
131 return_section->_raw_size = hdr->s_size;
132 return_section->filepos = hdr->s_scnptr;
133 return_section->rel_filepos = hdr->s_relptr;
134 return_section->reloc_count = hdr->s_nreloc;
135
136 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
137
138 return_section->line_filepos = hdr->s_lnnoptr;
139
140 return_section->lineno_count = hdr->s_nlnno;
141 return_section->userdata = NULL;
142 return_section->next = (asection *) NULL;
252b5132 143 return_section->target_index = target_index;
41733515
ILT
144 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name,
145 return_section);
252b5132
RH
146
147 /* At least on i386-coff, the line number count for a shared library
148 section must be ignored. */
149 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
150 return_section->lineno_count = 0;
151
152 if (hdr->s_nreloc != 0)
153 return_section->flags |= SEC_RELOC;
154 /* FIXME: should this check 'hdr->s_size > 0' */
155 if (hdr->s_scnptr != 0)
156 return_section->flags |= SEC_HAS_CONTENTS;
157 return true;
158}
159
160/* Read in a COFF object and make it into a BFD. This is used by
161 ECOFF as well. */
162
163static const bfd_target *
164coff_real_object_p (abfd, nscns, internal_f, internal_a)
165 bfd *abfd;
166 unsigned nscns;
167 struct internal_filehdr *internal_f;
168 struct internal_aouthdr *internal_a;
169{
170 flagword oflags = abfd->flags;
171 bfd_vma ostart = bfd_get_start_address (abfd);
172 PTR tdata;
173 size_t readsize; /* length of file_info */
174 unsigned int scnhsz;
175 char *external_sections;
176
177 if (!(internal_f->f_flags & F_RELFLG))
178 abfd->flags |= HAS_RELOC;
179 if ((internal_f->f_flags & F_EXEC))
180 abfd->flags |= EXEC_P;
181 if (!(internal_f->f_flags & F_LNNO))
182 abfd->flags |= HAS_LINENO;
183 if (!(internal_f->f_flags & F_LSYMS))
184 abfd->flags |= HAS_LOCALS;
185
186 /* FIXME: How can we set D_PAGED correctly? */
187 if ((internal_f->f_flags & F_EXEC) != 0)
188 abfd->flags |= D_PAGED;
189
190 bfd_get_symcount (abfd) = internal_f->f_nsyms;
191 if (internal_f->f_nsyms)
192 abfd->flags |= HAS_SYMS;
193
194 if (internal_a != (struct internal_aouthdr *) NULL)
195 bfd_get_start_address (abfd) = internal_a->entry;
196 else
197 bfd_get_start_address (abfd) = 0;
198
199 /* Set up the tdata area. ECOFF uses its own routine, and overrides
200 abfd->flags. */
201 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
202 if (tdata == NULL)
203 return 0;
204
205 scnhsz = bfd_coff_scnhsz (abfd);
206 readsize = nscns * scnhsz;
207 external_sections = (char *) bfd_alloc (abfd, readsize);
208 if (!external_sections)
209 goto fail;
210
211 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
212 goto fail;
213
363d7b14
TW
214 /* Set the arch/mach *before* swapping in sections; section header swapping
215 may depend on arch/mach info. */
216 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
217 goto fail;
218
252b5132
RH
219 /* Now copy data as required; construct all asections etc */
220 if (nscns != 0)
221 {
222 unsigned int i;
223 for (i = 0; i < nscns; i++)
224 {
225 struct internal_scnhdr tmp;
226 bfd_coff_swap_scnhdr_in (abfd,
227 (PTR) (external_sections + i * scnhsz),
228 (PTR) & tmp);
229 if (! make_a_section_from_file (abfd, &tmp, i + 1))
230 goto fail;
231 }
232 }
233
234 /* make_abs_section (abfd); */
235
252b5132
RH
236 return abfd->xvec;
237
238 fail:
239 bfd_release (abfd, tdata);
240 abfd->flags = oflags;
241 bfd_get_start_address (abfd) = ostart;
242 return (const bfd_target *) NULL;
243}
244
245/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
246 not a COFF file. This is also used by ECOFF. */
247
248const bfd_target *
249coff_object_p (abfd)
250 bfd *abfd;
251{
252 unsigned int filhsz;
253 unsigned int aoutsz;
254 int nscns;
255 PTR filehdr;
256 struct internal_filehdr internal_f;
257 struct internal_aouthdr internal_a;
258
259 /* figure out how much to read */
260 filhsz = bfd_coff_filhsz (abfd);
261 aoutsz = bfd_coff_aoutsz (abfd);
262
263 filehdr = bfd_alloc (abfd, filhsz);
264 if (filehdr == NULL)
265 return 0;
266 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
267 {
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_wrong_format);
270 return 0;
271 }
272 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
273 bfd_release (abfd, filehdr);
274
275 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
276 {
277 bfd_set_error (bfd_error_wrong_format);
278 return 0;
279 }
280 nscns = internal_f.f_nscns;
281
282 if (internal_f.f_opthdr)
283 {
284 PTR opthdr;
285
286 opthdr = bfd_alloc (abfd, aoutsz);
287 if (opthdr == NULL)
288 return 0;;
289 if (bfd_read (opthdr, 1, internal_f.f_opthdr, abfd)
290 != internal_f.f_opthdr)
291 {
292 return 0;
293 }
294 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
295 }
296
297 return coff_real_object_p (abfd, nscns, &internal_f,
298 (internal_f.f_opthdr != 0
299 ? &internal_a
300 : (struct internal_aouthdr *) NULL));
301}
302
303/* Get the BFD section from a COFF symbol section number. */
304
305asection *
306coff_section_from_bfd_index (abfd, index)
307 bfd *abfd;
308 int index;
309{
310 struct sec *answer = abfd->sections;
311
312 if (index == N_ABS)
313 return bfd_abs_section_ptr;
314 if (index == N_UNDEF)
315 return bfd_und_section_ptr;
316 if (index == N_DEBUG)
317 return bfd_abs_section_ptr;
318
319 while (answer)
320 {
321 if (answer->target_index == index)
322 return answer;
323 answer = answer->next;
324 }
325
326 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
327 has a bad symbol table in biglitpow.o. */
328 return bfd_und_section_ptr;
329}
330
331/* Get the upper bound of a COFF symbol table. */
332
333long
334coff_get_symtab_upper_bound (abfd)
335 bfd *abfd;
336{
337 if (!bfd_coff_slurp_symbol_table (abfd))
338 return -1;
339
340 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
341}
342
343
344/* Canonicalize a COFF symbol table. */
345
346long
347coff_get_symtab (abfd, alocation)
348 bfd *abfd;
349 asymbol **alocation;
350{
351 unsigned int counter;
352 coff_symbol_type *symbase;
353 coff_symbol_type **location = (coff_symbol_type **) alocation;
354
355 if (!bfd_coff_slurp_symbol_table (abfd))
356 return -1;
357
358 symbase = obj_symbols (abfd);
359 counter = bfd_get_symcount (abfd);
360 while (counter-- > 0)
361 *location++ = symbase++;
362
363 *location = NULL;
364
365 return bfd_get_symcount (abfd);
366}
367
368/* Get the name of a symbol. The caller must pass in a buffer of size
369 >= SYMNMLEN + 1. */
370
371const char *
372_bfd_coff_internal_syment_name (abfd, sym, buf)
373 bfd *abfd;
374 const struct internal_syment *sym;
375 char *buf;
376{
377 /* FIXME: It's not clear this will work correctly if sizeof
378 (_n_zeroes) != 4. */
379 if (sym->_n._n_n._n_zeroes != 0
380 || sym->_n._n_n._n_offset == 0)
381 {
382 memcpy (buf, sym->_n._n_name, SYMNMLEN);
383 buf[SYMNMLEN] = '\0';
384 return buf;
385 }
386 else
387 {
388 const char *strings;
389
390 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
391 strings = obj_coff_strings (abfd);
392 if (strings == NULL)
393 {
394 strings = _bfd_coff_read_string_table (abfd);
395 if (strings == NULL)
396 return NULL;
397 }
398 return strings + sym->_n._n_n._n_offset;
399 }
400}
401
402/* Read in and swap the relocs. This returns a buffer holding the
403 relocs for section SEC in file ABFD. If CACHE is true and
404 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
405 the function is called again. If EXTERNAL_RELOCS is not NULL, it
406 is a buffer large enough to hold the unswapped relocs. If
407 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
408 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
409 value must be INTERNAL_RELOCS. The function returns NULL on error. */
410
411struct internal_reloc *
412_bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
413 require_internal, internal_relocs)
414 bfd *abfd;
415 asection *sec;
416 boolean cache;
417 bfd_byte *external_relocs;
418 boolean require_internal;
419 struct internal_reloc *internal_relocs;
420{
421 bfd_size_type relsz;
422 bfd_byte *free_external = NULL;
423 struct internal_reloc *free_internal = NULL;
424 bfd_byte *erel;
425 bfd_byte *erel_end;
426 struct internal_reloc *irel;
427
428 if (coff_section_data (abfd, sec) != NULL
429 && coff_section_data (abfd, sec)->relocs != NULL)
430 {
431 if (! require_internal)
432 return coff_section_data (abfd, sec)->relocs;
433 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
434 sec->reloc_count * sizeof (struct internal_reloc));
435 return internal_relocs;
436 }
437
438 relsz = bfd_coff_relsz (abfd);
439
440 if (external_relocs == NULL)
441 {
442 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
443 if (free_external == NULL && sec->reloc_count > 0)
444 goto error_return;
445 external_relocs = free_external;
446 }
447
448 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
449 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
450 != relsz * sec->reloc_count))
451 goto error_return;
452
453 if (internal_relocs == NULL)
454 {
455 free_internal = ((struct internal_reloc *)
456 bfd_malloc (sec->reloc_count
457 * sizeof (struct internal_reloc)));
458 if (free_internal == NULL && sec->reloc_count > 0)
459 goto error_return;
460 internal_relocs = free_internal;
461 }
462
463 /* Swap in the relocs. */
464 erel = external_relocs;
465 erel_end = erel + relsz * sec->reloc_count;
466 irel = internal_relocs;
467 for (; erel < erel_end; erel += relsz, irel++)
468 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
469
470 if (free_external != NULL)
471 {
472 free (free_external);
473 free_external = NULL;
474 }
475
476 if (cache && free_internal != NULL)
477 {
478 if (coff_section_data (abfd, sec) == NULL)
479 {
480 sec->used_by_bfd =
481 (PTR) bfd_zalloc (abfd,
482 sizeof (struct coff_section_tdata));
483 if (sec->used_by_bfd == NULL)
484 goto error_return;
485 coff_section_data (abfd, sec)->contents = NULL;
486 }
487 coff_section_data (abfd, sec)->relocs = free_internal;
488 }
489
490 return internal_relocs;
491
492 error_return:
493 if (free_external != NULL)
494 free (free_external);
495 if (free_internal != NULL)
496 free (free_internal);
497 return NULL;
498}
499
500/* Set lineno_count for the output sections of a COFF file. */
501
502int
503coff_count_linenumbers (abfd)
504 bfd *abfd;
505{
506 unsigned int limit = bfd_get_symcount (abfd);
507 unsigned int i;
508 int total = 0;
509 asymbol **p;
510 asection *s;
511
512 if (limit == 0)
513 {
514 /* This may be from the backend linker, in which case the
515 lineno_count in the sections is correct. */
516 for (s = abfd->sections; s != NULL; s = s->next)
517 total += s->lineno_count;
518 return total;
519 }
520
521 for (s = abfd->sections; s != NULL; s = s->next)
522 BFD_ASSERT (s->lineno_count == 0);
523
524 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
525 {
526 asymbol *q_maybe = *p;
527
528 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
529 {
530 coff_symbol_type *q = coffsymbol (q_maybe);
531
532 /* The AIX 4.1 compiler can sometimes generate line numbers
533 attached to debugging symbols. We try to simply ignore
534 those here. */
535 if (q->lineno != NULL
536 && q->symbol.section->owner != NULL)
537 {
538 /* This symbol has line numbers. Increment the owning
539 section's linenumber count. */
540 alent *l = q->lineno;
541
542 ++q->symbol.section->output_section->lineno_count;
543 ++total;
544 ++l;
545 while (l->line_number != 0)
546 {
547 ++total;
548 ++q->symbol.section->output_section->lineno_count;
549 ++l;
550 }
551 }
552 }
553 }
554
555 return total;
556}
557
558/* Takes a bfd and a symbol, returns a pointer to the coff specific
559 area of the symbol if there is one. */
560
561/*ARGSUSED*/
562coff_symbol_type *
563coff_symbol_from (ignore_abfd, symbol)
7442e600 564 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
565 asymbol *symbol;
566{
567 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
568 return (coff_symbol_type *) NULL;
569
570 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
571 return (coff_symbol_type *) NULL;
572
573 return (coff_symbol_type *) symbol;
574}
575
576static void
577fixup_symbol_value (abfd, coff_symbol_ptr, syment)
578 bfd *abfd;
579 coff_symbol_type *coff_symbol_ptr;
580 struct internal_syment *syment;
581{
582
583 /* Normalize the symbol flags */
584 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
585 {
586 /* a common symbol is undefined with a value */
587 syment->n_scnum = N_UNDEF;
588 syment->n_value = coff_symbol_ptr->symbol.value;
589 }
703153b5
ILT
590 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
591 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
252b5132
RH
592 {
593 syment->n_value = coff_symbol_ptr->symbol.value;
594 }
595 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
596 {
597 syment->n_scnum = N_UNDEF;
598 syment->n_value = 0;
599 }
7bb9db4d 600 /* FIXME: Do we need to handle the absolute section here? */
252b5132
RH
601 else
602 {
603 if (coff_symbol_ptr->symbol.section)
604 {
605 syment->n_scnum =
606 coff_symbol_ptr->symbol.section->output_section->target_index;
607
608 syment->n_value = (coff_symbol_ptr->symbol.value
609 + coff_symbol_ptr->symbol.section->output_offset);
610 if (! obj_pe (abfd))
611 syment->n_value +=
612 coff_symbol_ptr->symbol.section->output_section->vma;
613 }
614 else
615 {
616 BFD_ASSERT (0);
617 /* This can happen, but I don't know why yet (steve@cygnus.com) */
618 syment->n_scnum = N_ABS;
619 syment->n_value = coff_symbol_ptr->symbol.value;
620 }
621 }
622}
623
624/* Run through all the symbols in the symbol table and work out what
625 their indexes into the symbol table will be when output.
626
627 Coff requires that each C_FILE symbol points to the next one in the
628 chain, and that the last one points to the first external symbol. We
629 do that here too. */
630
631boolean
632coff_renumber_symbols (bfd_ptr, first_undef)
633 bfd *bfd_ptr;
634 int *first_undef;
635{
636 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
637 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
638 unsigned int native_index = 0;
639 struct internal_syment *last_file = (struct internal_syment *) NULL;
640 unsigned int symbol_index;
641
642 /* COFF demands that undefined symbols come after all other symbols.
643 Since we don't need to impose this extra knowledge on all our
644 client programs, deal with that here. Sort the symbol table;
645 just move the undefined symbols to the end, leaving the rest
646 alone. The O'Reilly book says that defined global symbols come
647 at the end before the undefined symbols, so we do that here as
648 well. */
649 /* @@ Do we have some condition we could test for, so we don't always
650 have to do this? I don't think relocatability is quite right, but
651 I'm not certain. [raeburn:19920508.1711EST] */
652 {
653 asymbol **newsyms;
654 unsigned int i;
655
656 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
657 sizeof (asymbol *) * (symbol_count + 1));
658 if (!newsyms)
659 return false;
660 bfd_ptr->outsymbols = newsyms;
661 for (i = 0; i < symbol_count; i++)
662 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
663 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
664 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
665 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
666 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
667 == 0))))
668 *newsyms++ = symbol_ptr_ptr[i];
669
670 for (i = 0; i < symbol_count; i++)
671 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
672 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
673 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
674 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
675 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
676 != 0))))
677 *newsyms++ = symbol_ptr_ptr[i];
678
679 *first_undef = newsyms - bfd_ptr->outsymbols;
680
681 for (i = 0; i < symbol_count; i++)
682 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
683 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
684 *newsyms++ = symbol_ptr_ptr[i];
685 *newsyms = (asymbol *) NULL;
686 symbol_ptr_ptr = bfd_ptr->outsymbols;
687 }
688
689 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
690 {
691 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
692 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
693 if (coff_symbol_ptr && coff_symbol_ptr->native)
694 {
695 combined_entry_type *s = coff_symbol_ptr->native;
696 int i;
697
698 if (s->u.syment.n_sclass == C_FILE)
699 {
700 if (last_file != (struct internal_syment *) NULL)
701 last_file->n_value = native_index;
702 last_file = &(s->u.syment);
703 }
704 else
705 {
706
707 /* Modify the symbol values according to their section and
708 type */
709
710 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
711 }
712 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
713 s[i].offset = native_index++;
714 }
715 else
716 {
717 native_index++;
718 }
719 }
720 obj_conv_table_size (bfd_ptr) = native_index;
721
722 return true;
723}
724
725/* Run thorough the symbol table again, and fix it so that all
726 pointers to entries are changed to the entries' index in the output
727 symbol table. */
728
729void
730coff_mangle_symbols (bfd_ptr)
731 bfd *bfd_ptr;
732{
733 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
734 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
735 unsigned int symbol_index;
736
737 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
738 {
739 coff_symbol_type *coff_symbol_ptr =
740 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
741
742 if (coff_symbol_ptr && coff_symbol_ptr->native)
743 {
744 int i;
745 combined_entry_type *s = coff_symbol_ptr->native;
746
747 if (s->fix_value)
748 {
749 /* FIXME: We should use a union here. */
750 s->u.syment.n_value =
751 ((combined_entry_type *) s->u.syment.n_value)->offset;
752 s->fix_value = 0;
753 }
754 if (s->fix_line)
755 {
756 /* The value is the offset into the line number entries
757 for the symbol's section. On output, the symbol's
758 section should be N_DEBUG. */
759 s->u.syment.n_value =
760 (coff_symbol_ptr->symbol.section->output_section->line_filepos
761 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
762 coff_symbol_ptr->symbol.section =
763 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
764 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
765 }
766 for (i = 0; i < s->u.syment.n_numaux; i++)
767 {
768 combined_entry_type *a = s + i + 1;
769 if (a->fix_tag)
770 {
771 a->u.auxent.x_sym.x_tagndx.l =
772 a->u.auxent.x_sym.x_tagndx.p->offset;
773 a->fix_tag = 0;
774 }
775 if (a->fix_end)
776 {
777 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
778 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
779 a->fix_end = 0;
780 }
781 if (a->fix_scnlen)
782 {
783 a->u.auxent.x_csect.x_scnlen.l =
784 a->u.auxent.x_csect.x_scnlen.p->offset;
785 a->fix_scnlen = 0;
786 }
787 }
788 }
789 }
790}
791
792static void
793coff_fix_symbol_name (abfd, symbol, native, string_size_p,
794 debug_string_section_p, debug_string_size_p)
795 bfd *abfd;
796 asymbol *symbol;
797 combined_entry_type *native;
798 bfd_size_type *string_size_p;
799 asection **debug_string_section_p;
800 bfd_size_type *debug_string_size_p;
801{
802 unsigned int name_length;
803 union internal_auxent *auxent;
804 char *name = (char *) (symbol->name);
805
806 if (name == (char *) NULL)
807 {
808 /* coff symbols always have names, so we'll make one up */
809 symbol->name = "strange";
810 name = (char *) symbol->name;
811 }
812 name_length = strlen (name);
813
814 if (native->u.syment.n_sclass == C_FILE
815 && native->u.syment.n_numaux > 0)
816 {
692b7d62
ILT
817 unsigned int filnmlen;
818
252b5132
RH
819 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
820 auxent = &(native + 1)->u.auxent;
821
692b7d62
ILT
822 filnmlen = bfd_coff_filnmlen (abfd);
823
252b5132
RH
824 if (bfd_coff_long_filenames (abfd))
825 {
692b7d62 826 if (name_length <= filnmlen)
252b5132 827 {
692b7d62 828 strncpy (auxent->x_file.x_fname, name, filnmlen);
252b5132
RH
829 }
830 else
831 {
832 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
833 auxent->x_file.x_n.x_zeroes = 0;
834 *string_size_p += name_length + 1;
835 }
836 }
837 else
838 {
692b7d62
ILT
839 strncpy (auxent->x_file.x_fname, name, filnmlen);
840 if (name_length > filnmlen)
841 name[filnmlen] = '\0';
252b5132
RH
842 }
843 }
844 else
845 {
846 if (name_length <= SYMNMLEN)
847 {
848 /* This name will fit into the symbol neatly */
849 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
850 }
851 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
852 {
853 native->u.syment._n._n_n._n_offset = (*string_size_p
854 + STRING_SIZE_SIZE);
855 native->u.syment._n._n_n._n_zeroes = 0;
856 *string_size_p += name_length + 1;
857 }
858 else
859 {
860 long filepos;
861 bfd_byte buf[2];
862
863 /* This name should be written into the .debug section. For
864 some reason each name is preceded by a two byte length
865 and also followed by a null byte. FIXME: We assume that
866 the .debug section has already been created, and that it
867 is large enough. */
868 if (*debug_string_section_p == (asection *) NULL)
869 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
870 filepos = bfd_tell (abfd);
871 bfd_put_16 (abfd, name_length + 1, buf);
872 if (!bfd_set_section_contents (abfd,
873 *debug_string_section_p,
874 (PTR) buf,
875 (file_ptr) *debug_string_size_p,
876 (bfd_size_type) 2)
877 || !bfd_set_section_contents (abfd,
878 *debug_string_section_p,
879 (PTR) symbol->name,
880 ((file_ptr) *debug_string_size_p
881 + 2),
882 (bfd_size_type) name_length + 1))
883 abort ();
884 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
885 abort ();
886 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
887 native->u.syment._n._n_n._n_zeroes = 0;
888 *debug_string_size_p += name_length + 3;
889 }
890 }
891}
892
893/* We need to keep track of the symbol index so that when we write out
894 the relocs we can get the index for a symbol. This method is a
895 hack. FIXME. */
896
897#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
898
899/* Write a symbol out to a COFF file. */
900
901static boolean
902coff_write_symbol (abfd, symbol, native, written, string_size_p,
903 debug_string_section_p, debug_string_size_p)
904 bfd *abfd;
905 asymbol *symbol;
906 combined_entry_type *native;
907 unsigned int *written;
908 bfd_size_type *string_size_p;
909 asection **debug_string_section_p;
910 bfd_size_type *debug_string_size_p;
911{
912 unsigned int numaux = native->u.syment.n_numaux;
913 int type = native->u.syment.n_type;
914 int class = native->u.syment.n_sclass;
915 PTR buf;
916 bfd_size_type symesz;
917
918 if (native->u.syment.n_sclass == C_FILE)
919 symbol->flags |= BSF_DEBUGGING;
920
921 if (symbol->flags & BSF_DEBUGGING
922 && bfd_is_abs_section (symbol->section))
923 {
924 native->u.syment.n_scnum = N_DEBUG;
925 }
926 else if (bfd_is_abs_section (symbol->section))
927 {
928 native->u.syment.n_scnum = N_ABS;
929 }
930 else if (bfd_is_und_section (symbol->section))
931 {
932 native->u.syment.n_scnum = N_UNDEF;
933 }
934 else
935 {
936 native->u.syment.n_scnum =
937 symbol->section->output_section->target_index;
938 }
939
940 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
941 debug_string_section_p, debug_string_size_p);
942
943 symesz = bfd_coff_symesz (abfd);
944 buf = bfd_alloc (abfd, symesz);
945 if (!buf)
946 return false;
947 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
948 if (bfd_write (buf, 1, symesz, abfd) != symesz)
949 return false;
950 bfd_release (abfd, buf);
951
952 if (native->u.syment.n_numaux > 0)
953 {
954 bfd_size_type auxesz;
955 unsigned int j;
956
957 auxesz = bfd_coff_auxesz (abfd);
958 buf = bfd_alloc (abfd, auxesz);
959 if (!buf)
960 return false;
961 for (j = 0; j < native->u.syment.n_numaux; j++)
962 {
963 bfd_coff_swap_aux_out (abfd,
964 &((native + j + 1)->u.auxent),
965 type,
966 class,
967 j,
968 native->u.syment.n_numaux,
969 buf);
970 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
971 return false;
972 }
973 bfd_release (abfd, buf);
974 }
975
976 /* Store the index for use when we write out the relocs. */
977 set_index (symbol, *written);
978
979 *written += numaux + 1;
980 return true;
981}
982
983/* Write out a symbol to a COFF file that does not come from a COFF
984 file originally. This symbol may have been created by the linker,
985 or we may be linking a non COFF file to a COFF file. */
986
987static boolean
988coff_write_alien_symbol (abfd, symbol, written, string_size_p,
989 debug_string_section_p, debug_string_size_p)
990 bfd *abfd;
991 asymbol *symbol;
992 unsigned int *written;
993 bfd_size_type *string_size_p;
994 asection **debug_string_section_p;
995 bfd_size_type *debug_string_size_p;
996{
997 combined_entry_type *native;
998 combined_entry_type dummy;
999
1000 native = &dummy;
1001 native->u.syment.n_type = T_NULL;
1002 native->u.syment.n_flags = 0;
1003 if (bfd_is_und_section (symbol->section))
1004 {
1005 native->u.syment.n_scnum = N_UNDEF;
1006 native->u.syment.n_value = symbol->value;
1007 }
1008 else if (bfd_is_com_section (symbol->section))
1009 {
1010 native->u.syment.n_scnum = N_UNDEF;
1011 native->u.syment.n_value = symbol->value;
1012 }
1013 else if (symbol->flags & BSF_DEBUGGING)
1014 {
1015 /* There isn't much point to writing out a debugging symbol
1016 unless we are prepared to convert it into COFF debugging
1017 format. So, we just ignore them. We must clobber the symbol
1018 name to keep it from being put in the string table. */
1019 symbol->name = "";
1020 return true;
1021 }
1022 else
1023 {
1024 native->u.syment.n_scnum =
1025 symbol->section->output_section->target_index;
1026 native->u.syment.n_value = (symbol->value
1027 + symbol->section->output_offset);
1028 if (! obj_pe (abfd))
1029 native->u.syment.n_value += symbol->section->output_section->vma;
1030
1031 /* Copy the any flags from the the file header into the symbol.
1032 FIXME: Why? */
1033 {
1034 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1035 if (c != (coff_symbol_type *) NULL)
1036 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1037 }
1038 }
1039
1040 native->u.syment.n_type = 0;
1041 if (symbol->flags & BSF_LOCAL)
1042 native->u.syment.n_sclass = C_STAT;
1043 else if (symbol->flags & BSF_WEAK)
1044 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1045 else
1046 native->u.syment.n_sclass = C_EXT;
1047 native->u.syment.n_numaux = 0;
1048
1049 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1050 debug_string_section_p, debug_string_size_p);
1051}
1052
1053/* Write a native symbol to a COFF file. */
1054
1055static boolean
1056coff_write_native_symbol (abfd, symbol, written, string_size_p,
1057 debug_string_section_p, debug_string_size_p)
1058 bfd *abfd;
1059 coff_symbol_type *symbol;
1060 unsigned int *written;
1061 bfd_size_type *string_size_p;
1062 asection **debug_string_section_p;
1063 bfd_size_type *debug_string_size_p;
1064{
1065 combined_entry_type *native = symbol->native;
1066 alent *lineno = symbol->lineno;
1067
1068 /* If this symbol has an associated line number, we must store the
1069 symbol index in the line number field. We also tag the auxent to
1070 point to the right place in the lineno table. */
1071 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1072 {
1073 unsigned int count = 0;
1074 lineno[count].u.offset = *written;
1075 if (native->u.syment.n_numaux)
1076 {
1077 union internal_auxent *a = &((native + 1)->u.auxent);
1078
1079 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1080 symbol->symbol.section->output_section->moving_line_filepos;
1081 }
1082
1083 /* Count and relocate all other linenumbers. */
1084 count++;
1085 while (lineno[count].line_number != 0)
1086 {
1087#if 0
1088 /* 13 april 92. sac
1089 I've been told this, but still need proof:
1090 > The second bug is also in `bfd/coffcode.h'. This bug
1091 > causes the linker to screw up the pc-relocations for
1092 > all the line numbers in COFF code. This bug isn't only
1093 > specific to A29K implementations, but affects all
1094 > systems using COFF format binaries. Note that in COFF
1095 > object files, the line number core offsets output by
1096 > the assembler are relative to the start of each
1097 > procedure, not to the start of the .text section. This
1098 > patch relocates the line numbers relative to the
1099 > `native->u.syment.n_value' instead of the section
1100 > virtual address.
1101 > modular!olson@cs.arizona.edu (Jon Olson)
1102 */
1103 lineno[count].u.offset += native->u.syment.n_value;
1104#else
1105 lineno[count].u.offset +=
1106 (symbol->symbol.section->output_section->vma
1107 + symbol->symbol.section->output_offset);
1108#endif
1109 count++;
1110 }
1111 symbol->done_lineno = true;
1112
1113 symbol->symbol.section->output_section->moving_line_filepos +=
1114 count * bfd_coff_linesz (abfd);
1115 }
1116
1117 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1118 string_size_p, debug_string_section_p,
1119 debug_string_size_p);
1120}
1121
1122/* Write out the COFF symbols. */
1123
1124boolean
1125coff_write_symbols (abfd)
1126 bfd *abfd;
1127{
1128 bfd_size_type string_size;
1129 asection *debug_string_section;
1130 bfd_size_type debug_string_size;
1131 unsigned int i;
1132 unsigned int limit = bfd_get_symcount (abfd);
1133 unsigned int written = 0;
1134 asymbol **p;
1135
1136 string_size = 0;
1137 debug_string_section = NULL;
1138 debug_string_size = 0;
1139
1140 /* If this target supports long section names, they must be put into
1141 the string table. This is supported by PE. This code must
1142 handle section names just as they are handled in
1143 coff_write_object_contents. */
1144 if (bfd_coff_long_section_names (abfd))
1145 {
1146 asection *o;
1147
1148 for (o = abfd->sections; o != NULL; o = o->next)
1149 {
1150 size_t len;
1151
1152 len = strlen (o->name);
1153 if (len > SCNNMLEN)
1154 string_size += len + 1;
1155 }
1156 }
1157
1158 /* Seek to the right place */
1159 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1160 return false;
1161
1162 /* Output all the symbols we have */
1163
1164 written = 0;
1165 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1166 {
1167 asymbol *symbol = *p;
1168 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1169
1170 if (c_symbol == (coff_symbol_type *) NULL
1171 || c_symbol->native == (combined_entry_type *) NULL)
1172 {
1173 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1174 &debug_string_section,
1175 &debug_string_size))
1176 return false;
1177 }
1178 else
1179 {
1180 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1181 &string_size, &debug_string_section,
1182 &debug_string_size))
1183 return false;
1184 }
1185 }
1186
1187 obj_raw_syment_count (abfd) = written;
1188
1189 /* Now write out strings */
1190
1191 if (string_size != 0)
1192 {
1193 unsigned int size = string_size + STRING_SIZE_SIZE;
1194 bfd_byte buffer[STRING_SIZE_SIZE];
1195
1196#if STRING_SIZE_SIZE == 4
1197 bfd_h_put_32 (abfd, size, buffer);
1198#else
1199 #error Change bfd_h_put_32
1200#endif
1201 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1202 return false;
1203
1204 /* Handle long section names. This code must handle section
1205 names just as they are handled in coff_write_object_contents. */
1206 if (bfd_coff_long_section_names (abfd))
1207 {
1208 asection *o;
1209
1210 for (o = abfd->sections; o != NULL; o = o->next)
1211 {
1212 size_t len;
1213
1214 len = strlen (o->name);
1215 if (len > SCNNMLEN)
1216 {
1217 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1218 return false;
1219 }
1220 }
1221 }
1222
1223 for (p = abfd->outsymbols, i = 0;
1224 i < limit;
1225 i++, p++)
1226 {
1227 asymbol *q = *p;
1228 size_t name_length = strlen (q->name);
1229 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1230 size_t maxlen;
1231
1232 /* Figure out whether the symbol name should go in the string
1233 table. Symbol names that are short enough are stored
1234 directly in the syment structure. File names permit a
1235 different, longer, length in the syment structure. On
1236 XCOFF, some symbol names are stored in the .debug section
1237 rather than in the string table. */
1238
1239 if (c_symbol == NULL
1240 || c_symbol->native == NULL)
1241 {
1242 /* This is not a COFF symbol, so it certainly is not a
1243 file name, nor does it go in the .debug section. */
1244 maxlen = SYMNMLEN;
1245 }
1246 else if (bfd_coff_symname_in_debug (abfd,
1247 &c_symbol->native->u.syment))
1248 {
1249 /* This symbol name is in the XCOFF .debug section.
1250 Don't write it into the string table. */
1251 maxlen = name_length;
1252 }
1253 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1254 && c_symbol->native->u.syment.n_numaux > 0)
692b7d62 1255 maxlen = bfd_coff_filnmlen (abfd);
252b5132
RH
1256 else
1257 maxlen = SYMNMLEN;
1258
1259 if (name_length > maxlen)
1260 {
1261 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1262 != name_length + 1)
1263 return false;
1264 }
1265 }
1266 }
1267 else
1268 {
1269 /* We would normally not write anything here, but we'll write
1270 out 4 so that any stupid coff reader which tries to read the
1271 string table even when there isn't one won't croak. */
1272 unsigned int size = STRING_SIZE_SIZE;
1273 bfd_byte buffer[STRING_SIZE_SIZE];
1274
1275#if STRING_SIZE_SIZE == 4
1276 bfd_h_put_32 (abfd, size, buffer);
1277#else
1278 #error Change bfd_h_put_32
1279#endif
1280 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1281 != STRING_SIZE_SIZE)
1282 return false;
1283 }
1284
1285 /* Make sure the .debug section was created to be the correct size.
1286 We should create it ourselves on the fly, but we don't because
1287 BFD won't let us write to any section until we know how large all
1288 the sections are. We could still do it by making another pass
1289 over the symbols. FIXME. */
1290 BFD_ASSERT (debug_string_size == 0
1291 || (debug_string_section != (asection *) NULL
1292 && (BFD_ALIGN (debug_string_size,
1293 1 << debug_string_section->alignment_power)
1294 == bfd_section_size (abfd, debug_string_section))));
1295
1296 return true;
1297}
1298
1299boolean
1300coff_write_linenumbers (abfd)
1301 bfd *abfd;
1302{
1303 asection *s;
1304 bfd_size_type linesz;
1305 PTR buff;
1306
1307 linesz = bfd_coff_linesz (abfd);
1308 buff = bfd_alloc (abfd, linesz);
1309 if (!buff)
1310 return false;
1311 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1312 {
1313 if (s->lineno_count)
1314 {
1315 asymbol **q = abfd->outsymbols;
1316 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1317 return false;
1318 /* Find all the linenumbers in this section */
1319 while (*q)
1320 {
1321 asymbol *p = *q;
1322 if (p->section->output_section == s)
1323 {
1324 alent *l =
1325 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1326 (bfd_asymbol_bfd (p), p));
1327 if (l)
1328 {
1329 /* Found a linenumber entry, output */
1330 struct internal_lineno out;
1331 memset ((PTR) & out, 0, sizeof (out));
1332 out.l_lnno = 0;
1333 out.l_addr.l_symndx = l->u.offset;
1334 bfd_coff_swap_lineno_out (abfd, &out, buff);
1335 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1336 return false;
1337 l++;
1338 while (l->line_number)
1339 {
1340 out.l_lnno = l->line_number;
1341 out.l_addr.l_symndx = l->u.offset;
1342 bfd_coff_swap_lineno_out (abfd, &out, buff);
1343 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1344 return false;
1345 l++;
1346 }
1347 }
1348 }
1349 q++;
1350 }
1351 }
1352 }
1353 bfd_release (abfd, buff);
1354 return true;
1355}
1356
1357/*ARGSUSED */
1358alent *
1359coff_get_lineno (ignore_abfd, symbol)
7442e600 1360 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
1361 asymbol *symbol;
1362{
1363 return coffsymbol (symbol)->lineno;
1364}
1365
1366#if 0
1367
1368/* This is only called from coff_add_missing_symbols, which has been
1369 disabled. */
1370
1371asymbol *
1372coff_section_symbol (abfd, name)
1373 bfd *abfd;
1374 char *name;
1375{
1376 asection *sec = bfd_make_section_old_way (abfd, name);
1377 asymbol *sym;
1378 combined_entry_type *csym;
1379
1380 sym = sec->symbol;
1381 csym = coff_symbol_from (abfd, sym)->native;
1382 /* Make sure back-end COFF stuff is there. */
1383 if (csym == 0)
1384 {
1385 struct foo
1386 {
1387 coff_symbol_type sym;
1388 /* @@FIXME This shouldn't use a fixed size!! */
1389 combined_entry_type e[10];
1390 };
1391 struct foo *f;
1392 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1393 if (!f)
1394 {
1395 bfd_set_error (bfd_error_no_error);
1396 return NULL;
1397 }
1398 memset ((char *) f, 0, sizeof (*f));
1399 coff_symbol_from (abfd, sym)->native = csym = f->e;
1400 }
1401 csym[0].u.syment.n_sclass = C_STAT;
1402 csym[0].u.syment.n_numaux = 1;
1403/* SF_SET_STATICS (sym); @@ ??? */
1404 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1405 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1406 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1407
1408 if (sec->output_section == NULL)
1409 {
1410 sec->output_section = sec;
1411 sec->output_offset = 0;
1412 }
1413
1414 return sym;
1415}
1416
1417#endif /* 0 */
1418
1419/* This function transforms the offsets into the symbol table into
1420 pointers to syments. */
1421
1422static void
1423coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1424 bfd *abfd;
1425 combined_entry_type *table_base;
1426 combined_entry_type *symbol;
1427 unsigned int indaux;
1428 combined_entry_type *auxent;
1429{
1430 unsigned int type = symbol->u.syment.n_type;
1431 unsigned int class = symbol->u.syment.n_sclass;
1432
1433 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1434 {
1435 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1436 (abfd, table_base, symbol, indaux, auxent))
1437 return;
1438 }
1439
1440 /* Don't bother if this is a file or a section */
1441 if (class == C_STAT && type == T_NULL)
1442 return;
1443 if (class == C_FILE)
1444 return;
1445
1446 /* Otherwise patch up */
1447#define N_TMASK coff_data (abfd)->local_n_tmask
1448#define N_BTSHFT coff_data (abfd)->local_n_btshft
1449 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1450 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1451 {
1452 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1453 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1454 auxent->fix_end = 1;
1455 }
1456 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1457 generate one, so we must be careful to ignore it. */
1458 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1459 {
1460 auxent->u.auxent.x_sym.x_tagndx.p =
1461 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1462 auxent->fix_tag = 1;
1463 }
1464}
1465
1466/* Allocate space for the ".debug" section, and read it.
1467 We did not read the debug section until now, because
1468 we didn't want to go to the trouble until someone needed it. */
1469
1470static char *
1471build_debug_section (abfd)
1472 bfd *abfd;
1473{
1474 char *debug_section;
1475 long position;
1476
1477 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1478
1479 if (!sect)
1480 {
1481 bfd_set_error (bfd_error_no_debug_section);
1482 return NULL;
1483 }
1484
1485 debug_section = (PTR) bfd_alloc (abfd,
1486 bfd_get_section_size_before_reloc (sect));
1487 if (debug_section == NULL)
1488 return NULL;
1489
1490 /* Seek to the beginning of the `.debug' section and read it.
1491 Save the current position first; it is needed by our caller.
1492 Then read debug section and reset the file pointer. */
1493
1494 position = bfd_tell (abfd);
1495 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1496 || (bfd_read (debug_section,
1497 bfd_get_section_size_before_reloc (sect), 1, abfd)
1498 != bfd_get_section_size_before_reloc (sect))
1499 || bfd_seek (abfd, position, SEEK_SET) != 0)
1500 return NULL;
1501 return debug_section;
1502}
1503
1504
1505/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1506 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1507 be \0-terminated. */
1508static char *
1509copy_name (abfd, name, maxlen)
1510 bfd *abfd;
1511 char *name;
1512 int maxlen;
1513{
1514 int len;
1515 char *newname;
1516
1517 for (len = 0; len < maxlen; ++len)
1518 {
1519 if (name[len] == '\0')
1520 {
1521 break;
1522 }
1523 }
1524
1525 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1526 return (NULL);
1527 strncpy (newname, name, len);
1528 newname[len] = '\0';
1529 return newname;
1530}
1531
1532/* Read in the external symbols. */
1533
1534boolean
1535_bfd_coff_get_external_symbols (abfd)
1536 bfd *abfd;
1537{
1538 bfd_size_type symesz;
1539 size_t size;
1540 PTR syms;
1541
1542 if (obj_coff_external_syms (abfd) != NULL)
1543 return true;
1544
1545 symesz = bfd_coff_symesz (abfd);
1546
1547 size = obj_raw_syment_count (abfd) * symesz;
1548
1549 syms = (PTR) bfd_malloc (size);
1550 if (syms == NULL && size != 0)
1551 return false;
1552
1553 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1554 || bfd_read (syms, size, 1, abfd) != size)
1555 {
1556 if (syms != NULL)
1557 free (syms);
1558 return false;
1559 }
1560
1561 obj_coff_external_syms (abfd) = syms;
1562
1563 return true;
1564}
1565
1566/* Read in the external strings. The strings are not loaded until
1567 they are needed. This is because we have no simple way of
1568 detecting a missing string table in an archive. */
1569
1570const char *
1571_bfd_coff_read_string_table (abfd)
1572 bfd *abfd;
1573{
1574 char extstrsize[STRING_SIZE_SIZE];
1575 size_t strsize;
1576 char *strings;
1577
1578 if (obj_coff_strings (abfd) != NULL)
1579 return obj_coff_strings (abfd);
1580
1581 if (obj_sym_filepos (abfd) == 0)
1582 {
1583 bfd_set_error (bfd_error_no_symbols);
1584 return NULL;
1585 }
1586
1587 if (bfd_seek (abfd,
1588 (obj_sym_filepos (abfd)
1589 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1590 SEEK_SET) != 0)
1591 return NULL;
1592
1593 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1594 {
1595 if (bfd_get_error () != bfd_error_file_truncated)
1596 return NULL;
1597
1598 /* There is no string table. */
1599 strsize = STRING_SIZE_SIZE;
1600 }
1601 else
1602 {
1603#if STRING_SIZE_SIZE == 4
1604 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1605#else
1606 #error Change bfd_h_get_32
1607#endif
1608 }
1609
1610 if (strsize < STRING_SIZE_SIZE)
1611 {
1612 (*_bfd_error_handler)
1613 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1614 (unsigned long) strsize);
1615 bfd_set_error (bfd_error_bad_value);
1616 return NULL;
1617 }
1618
1619 strings = (char *) bfd_malloc (strsize);
1620 if (strings == NULL)
1621 return NULL;
1622
1623 if (bfd_read (strings + STRING_SIZE_SIZE,
1624 strsize - STRING_SIZE_SIZE, 1, abfd)
1625 != strsize - STRING_SIZE_SIZE)
1626 {
1627 free (strings);
1628 return NULL;
1629 }
1630
1631 obj_coff_strings (abfd) = strings;
1632
1633 return strings;
1634}
1635
1636/* Free up the external symbols and strings read from a COFF file. */
1637
1638boolean
1639_bfd_coff_free_symbols (abfd)
1640 bfd *abfd;
1641{
1642 if (obj_coff_external_syms (abfd) != NULL
1643 && ! obj_coff_keep_syms (abfd))
1644 {
1645 free (obj_coff_external_syms (abfd));
1646 obj_coff_external_syms (abfd) = NULL;
1647 }
1648 if (obj_coff_strings (abfd) != NULL
1649 && ! obj_coff_keep_strings (abfd))
1650 {
1651 free (obj_coff_strings (abfd));
1652 obj_coff_strings (abfd) = NULL;
1653 }
1654 return true;
1655}
1656
1657/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1658 knit the symbol names into a normalized form. By normalized here I
1659 mean that all symbols have an n_offset pointer that points to a null-
1660 terminated string. */
1661
1662combined_entry_type *
1663coff_get_normalized_symtab (abfd)
1664 bfd *abfd;
1665{
1666 combined_entry_type *internal;
1667 combined_entry_type *internal_ptr;
1668 combined_entry_type *symbol_ptr;
1669 combined_entry_type *internal_end;
1670 bfd_size_type symesz;
1671 char *raw_src;
1672 char *raw_end;
1673 const char *string_table = NULL;
1674 char *debug_section = NULL;
1675 unsigned long size;
1676
1677 if (obj_raw_syments (abfd) != NULL)
1678 return obj_raw_syments (abfd);
1679
1680 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1681 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1682 if (internal == NULL && size != 0)
1683 return NULL;
1684 internal_end = internal + obj_raw_syment_count (abfd);
1685
1686 if (! _bfd_coff_get_external_symbols (abfd))
1687 return NULL;
1688
1689 raw_src = (char *) obj_coff_external_syms (abfd);
1690
1691 /* mark the end of the symbols */
1692 symesz = bfd_coff_symesz (abfd);
1693 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1694
1695 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1696 probably possible. If one shows up, it will probably kill us. */
1697
1698 /* Swap all the raw entries */
1699 for (internal_ptr = internal;
1700 raw_src < raw_end;
1701 raw_src += symesz, internal_ptr++)
1702 {
1703
1704 unsigned int i;
1705 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1706 (PTR) & internal_ptr->u.syment);
1707 symbol_ptr = internal_ptr;
1708
1709 for (i = 0;
1710 i < symbol_ptr->u.syment.n_numaux;
1711 i++)
1712 {
1713 internal_ptr++;
1714 raw_src += symesz;
1715 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1716 symbol_ptr->u.syment.n_type,
1717 symbol_ptr->u.syment.n_sclass,
1718 i, symbol_ptr->u.syment.n_numaux,
1719 &(internal_ptr->u.auxent));
1720 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1721 internal_ptr);
1722 }
1723 }
1724
1725 /* Free the raw symbols, but not the strings (if we have them). */
1726 obj_coff_keep_strings (abfd) = true;
1727 if (! _bfd_coff_free_symbols (abfd))
1728 return NULL;
1729
1730 for (internal_ptr = internal; internal_ptr < internal_end;
1731 internal_ptr++)
1732 {
1733 if (internal_ptr->u.syment.n_sclass == C_FILE
1734 && internal_ptr->u.syment.n_numaux > 0)
1735 {
1736 /* make a file symbol point to the name in the auxent, since
1737 the text ".file" is redundant */
1738 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1739 {
1740 /* the filename is a long one, point into the string table */
1741 if (string_table == NULL)
1742 {
1743 string_table = _bfd_coff_read_string_table (abfd);
1744 if (string_table == NULL)
1745 return NULL;
1746 }
1747
1748 internal_ptr->u.syment._n._n_n._n_offset =
1749 ((long)
1750 (string_table
1751 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1752 }
1753 else
1754 {
7bb9db4d
ILT
1755 /* Ordinary short filename, put into memory anyway. The
1756 Microsoft PE tools sometimes store a filename in
1757 multiple AUX entries. */
ec0ef80e
DD
1758 if (internal_ptr->u.syment.n_numaux > 1
1759 && coff_data (abfd)->pe)
1760 {
7bb9db4d
ILT
1761 internal_ptr->u.syment._n._n_n._n_offset =
1762 ((long)
1763 copy_name (abfd,
1764 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1765 internal_ptr->u.syment.n_numaux * symesz));
ec0ef80e
DD
1766 }
1767 else
1768 {
7bb9db4d
ILT
1769 internal_ptr->u.syment._n._n_n._n_offset =
1770 ((long)
1771 copy_name (abfd,
1772 (internal_ptr + 1)->u.auxent.x_file.x_fname,
692b7d62 1773 bfd_coff_filnmlen (abfd)));
ec0ef80e 1774 }
252b5132
RH
1775 }
1776 }
1777 else
1778 {
1779 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1780 {
1781 /* This is a "short" name. Make it long. */
1782 unsigned long i = 0;
1783 char *newstring = NULL;
1784
1785 /* find the length of this string without walking into memory
1786 that isn't ours. */
1787 for (i = 0; i < 8; ++i)
1788 {
1789 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1790 {
1791 break;
1792 } /* if end of string */
1793 } /* possible lengths of this string. */
1794
1795 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1796 return (NULL);
1797 memset (newstring, 0, i);
1798 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1799 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1800 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1801 }
1802 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1803 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1804 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1805 {
1806 /* Long name already. Point symbol at the string in the
1807 table. */
1808 if (string_table == NULL)
1809 {
1810 string_table = _bfd_coff_read_string_table (abfd);
1811 if (string_table == NULL)
1812 return NULL;
1813 }
1814 internal_ptr->u.syment._n._n_n._n_offset =
1815 ((long int)
1816 (string_table
1817 + internal_ptr->u.syment._n._n_n._n_offset));
1818 }
1819 else
1820 {
1821 /* Long name in debug section. Very similar. */
1822 if (debug_section == NULL)
1823 debug_section = build_debug_section (abfd);
1824 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1825 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1826 }
1827 }
1828 internal_ptr += internal_ptr->u.syment.n_numaux;
1829 }
1830
1831 obj_raw_syments (abfd) = internal;
1832 BFD_ASSERT (obj_raw_syment_count (abfd)
1833 == (unsigned int) (internal_ptr - internal));
1834
1835 return (internal);
1836} /* coff_get_normalized_symtab() */
1837
1838long
1839coff_get_reloc_upper_bound (abfd, asect)
1840 bfd *abfd;
1841 sec_ptr asect;
1842{
1843 if (bfd_get_format (abfd) != bfd_object)
1844 {
1845 bfd_set_error (bfd_error_invalid_operation);
1846 return -1;
1847 }
1848 return (asect->reloc_count + 1) * sizeof (arelent *);
1849}
1850
1851asymbol *
1852coff_make_empty_symbol (abfd)
1853 bfd *abfd;
1854{
1855 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1856 if (new == NULL)
1857 return (NULL);
1858 memset (new, 0, sizeof *new);
1859 new->symbol.section = 0;
1860 new->native = 0;
1861 new->lineno = (alent *) NULL;
1862 new->done_lineno = false;
1863 new->symbol.the_bfd = abfd;
1864 return &new->symbol;
1865}
1866
1867/* Make a debugging symbol. */
1868
1869asymbol *
1870coff_bfd_make_debug_symbol (abfd, ptr, sz)
1871 bfd *abfd;
7442e600
ILT
1872 PTR ptr ATTRIBUTE_UNUSED;
1873 unsigned long sz ATTRIBUTE_UNUSED;
252b5132
RH
1874{
1875 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1876 if (new == NULL)
1877 return (NULL);
1878 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1879 (but shouldn't be a constant). */
1880 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1881 if (!new->native)
1882 return (NULL);
1883 new->symbol.section = bfd_abs_section_ptr;
1884 new->symbol.flags = BSF_DEBUGGING;
1885 new->lineno = (alent *) NULL;
1886 new->done_lineno = false;
1887 new->symbol.the_bfd = abfd;
1888 return &new->symbol;
1889}
1890
1891/*ARGSUSED */
1892void
1893coff_get_symbol_info (abfd, symbol, ret)
1894 bfd *abfd;
1895 asymbol *symbol;
1896 symbol_info *ret;
1897{
1898 bfd_symbol_info (symbol, ret);
1899 if (coffsymbol (symbol)->native != NULL
1900 && coffsymbol (symbol)->native->fix_value)
1901 {
1902 combined_entry_type *psym;
1903
1904 psym = ((combined_entry_type *)
1905 coffsymbol (symbol)->native->u.syment.n_value);
1906 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1907 }
1908}
1909
1910/* Return the COFF syment for a symbol. */
1911
1912boolean
1913bfd_coff_get_syment (abfd, symbol, psyment)
1914 bfd *abfd;
1915 asymbol *symbol;
1916 struct internal_syment *psyment;
1917{
1918 coff_symbol_type *csym;
1919
1920 csym = coff_symbol_from (abfd, symbol);
1921 if (csym == NULL || csym->native == NULL)
1922 {
1923 bfd_set_error (bfd_error_invalid_operation);
1924 return false;
1925 }
1926
1927 *psyment = csym->native->u.syment;
1928
1929 if (csym->native->fix_value)
1930 psyment->n_value = ((combined_entry_type *) psyment->n_value
1931 - obj_raw_syments (abfd));
1932
1933 /* FIXME: We should handle fix_line here. */
1934
1935 return true;
1936}
1937
1938/* Return the COFF auxent for a symbol. */
1939
1940boolean
1941bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1942 bfd *abfd;
1943 asymbol *symbol;
1944 int indx;
1945 union internal_auxent *pauxent;
1946{
1947 coff_symbol_type *csym;
1948 combined_entry_type *ent;
1949
1950 csym = coff_symbol_from (abfd, symbol);
1951
1952 if (csym == NULL
1953 || csym->native == NULL
1954 || indx >= csym->native->u.syment.n_numaux)
1955 {
1956 bfd_set_error (bfd_error_invalid_operation);
1957 return false;
1958 }
1959
1960 ent = csym->native + indx + 1;
1961
1962 *pauxent = ent->u.auxent;
1963
1964 if (ent->fix_tag)
1965 pauxent->x_sym.x_tagndx.l =
1966 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1967 - obj_raw_syments (abfd));
1968
1969 if (ent->fix_end)
1970 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1971 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1972 - obj_raw_syments (abfd));
1973
1974 if (ent->fix_scnlen)
1975 pauxent->x_csect.x_scnlen.l =
1976 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1977 - obj_raw_syments (abfd));
1978
1979 return true;
1980}
1981
1982/* Print out information about COFF symbol. */
1983
1984void
1985coff_print_symbol (abfd, filep, symbol, how)
1986 bfd *abfd;
1987 PTR filep;
1988 asymbol *symbol;
1989 bfd_print_symbol_type how;
1990{
1991 FILE *file = (FILE *) filep;
1992
1993 switch (how)
1994 {
1995 case bfd_print_symbol_name:
1996 fprintf (file, "%s", symbol->name);
1997 break;
1998
1999 case bfd_print_symbol_more:
2000 fprintf (file, "coff %s %s",
2001 coffsymbol (symbol)->native ? "n" : "g",
2002 coffsymbol (symbol)->lineno ? "l" : " ");
2003 break;
2004
2005 case bfd_print_symbol_all:
2006 if (coffsymbol (symbol)->native)
2007 {
2008 unsigned long val;
2009 unsigned int aux;
2010 combined_entry_type *combined = coffsymbol (symbol)->native;
2011 combined_entry_type *root = obj_raw_syments (abfd);
2012 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2013
2014 fprintf (file, "[%3ld]", (long) (combined - root));
2015
2016 if (! combined->fix_value)
2017 val = (unsigned long) combined->u.syment.n_value;
2018 else
2019 val = ((unsigned long)
2020 ((combined_entry_type *) combined->u.syment.n_value
2021 - root));
2022
2023 fprintf (file,
2024 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2025 combined->u.syment.n_scnum,
2026 combined->u.syment.n_flags,
2027 combined->u.syment.n_type,
2028 combined->u.syment.n_sclass,
2029 combined->u.syment.n_numaux,
2030 val,
2031 symbol->name);
2032
2033 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2034 {
2035 combined_entry_type *auxp = combined + aux + 1;
2036 long tagndx;
2037
2038 if (auxp->fix_tag)
2039 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2040 else
2041 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2042
2043 fprintf (file, "\n");
2044
2045 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2046 continue;
2047
2048 switch (combined->u.syment.n_sclass)
2049 {
2050 case C_FILE:
2051 fprintf (file, "File ");
2052 break;
2053
2054 case C_STAT:
2055 if (combined->u.syment.n_type == T_NULL)
2056 /* probably a section symbol? */
2057 {
2058 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2059 (long) auxp->u.auxent.x_scn.x_scnlen,
2060 auxp->u.auxent.x_scn.x_nreloc,
2061 auxp->u.auxent.x_scn.x_nlinno);
2062 if (auxp->u.auxent.x_scn.x_checksum != 0
2063 || auxp->u.auxent.x_scn.x_associated != 0
2064 || auxp->u.auxent.x_scn.x_comdat != 0)
2065 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2066 auxp->u.auxent.x_scn.x_checksum,
2067 auxp->u.auxent.x_scn.x_associated,
2068 auxp->u.auxent.x_scn.x_comdat);
2069 break;
2070 }
312191a6
ILT
2071 /* else fall through */
2072 case C_EXT:
2073 if (ISFCN (combined->u.syment.n_type))
2074 {
2075 fprintf (file,
2076 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2077 tagndx,
2078 auxp->u.auxent.x_sym.x_misc.x_fsize,
2079 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2080 (auxp->fix_end
2081 ? ((long)
2082 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2083 - root))
2084 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2085 break;
2086 }
252b5132 2087 /* else fall through */
252b5132
RH
2088 default:
2089 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2090 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2091 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2092 tagndx);
2093 if (auxp->fix_end)
2094 fprintf (file, " endndx %ld",
2095 ((long)
2096 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2097 - root)));
2098 break;
2099 }
2100 }
2101
2102 if (l)
2103 {
2104 fprintf (file, "\n%s :", l->u.sym->name);
2105 l++;
2106 while (l->line_number)
2107 {
2108 fprintf (file, "\n%4d : 0x%lx",
2109 l->line_number,
2110 ((unsigned long)
2111 (l->u.offset + symbol->section->vma)));
2112 l++;
2113 }
2114 }
2115 }
2116 else
2117 {
2118 bfd_print_symbol_vandf ((PTR) file, symbol);
2119 fprintf (file, " %-5s %s %s %s",
2120 symbol->section->name,
2121 coffsymbol (symbol)->native ? "n" : "g",
2122 coffsymbol (symbol)->lineno ? "l" : " ",
2123 symbol->name);
2124 }
2125 }
2126}
2127
2128/* Return whether a symbol name implies a local symbol. In COFF,
2129 local symbols generally start with ``.L''. Most targets use this
2130 function for the is_local_label_name entry point, but some may
2131 override it. */
2132
2133boolean
2134_bfd_coff_is_local_label_name (abfd, name)
7442e600 2135 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
2136 const char *name;
2137{
2138 return name[0] == '.' && name[1] == 'L';
2139}
2140
9a968f43
NC
2141/* Provided a BFD, a section and an offset (in bytes, not octets) into the
2142 section, calculate and return the name of the source file and the line
2143 nearest to the wanted location. */
252b5132
RH
2144/*ARGSUSED*/
2145boolean
2146coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2147 functionname_ptr, line_ptr)
2148 bfd *abfd;
2149 asection *section;
2150 asymbol **symbols;
2151 bfd_vma offset;
2152 CONST char **filename_ptr;
2153 CONST char **functionname_ptr;
2154 unsigned int *line_ptr;
2155{
2156 boolean found;
2157 unsigned int i;
2158 unsigned int line_base;
2159 coff_data_type *cof = coff_data (abfd);
2160 /* Run through the raw syments if available */
2161 combined_entry_type *p;
2162 combined_entry_type *pend;
2163 alent *l;
2164 struct coff_section_tdata *sec_data;
2165
2166 /* Before looking through the symbol table, try to use a .stab
2167 section to find the information. */
2168 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2169 &found, filename_ptr,
2170 functionname_ptr, line_ptr,
2171 &coff_data (abfd)->line_info))
2172 return false;
2173 if (found)
2174 return true;
2175
2176 *filename_ptr = 0;
2177 *functionname_ptr = 0;
2178 *line_ptr = 0;
2179
2180 /* Don't try and find line numbers in a non coff file */
2181 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2182 return false;
2183
2184 if (cof == NULL)
2185 return false;
2186
2187 /* Find the first C_FILE symbol. */
2188 p = cof->raw_syments;
2189 if (!p)
2190 return false;
2191
2192 pend = p + cof->raw_syment_count;
2193 while (p < pend)
2194 {
2195 if (p->u.syment.n_sclass == C_FILE)
2196 break;
2197 p += 1 + p->u.syment.n_numaux;
2198 }
2199
2200 if (p < pend)
2201 {
2202 bfd_vma sec_vma;
2203 bfd_vma maxdiff;
2204
2205 /* Look through the C_FILE symbols to find the best one. */
2206 sec_vma = bfd_get_section_vma (abfd, section);
2207 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2208 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2209 while (1)
2210 {
2211 combined_entry_type *p2;
2212
2213 for (p2 = p + 1 + p->u.syment.n_numaux;
2214 p2 < pend;
2215 p2 += 1 + p2->u.syment.n_numaux)
2216 {
2217 if (p2->u.syment.n_scnum > 0
2218 && (section
2219 == coff_section_from_bfd_index (abfd,
2220 p2->u.syment.n_scnum)))
2221 break;
2222 if (p2->u.syment.n_sclass == C_FILE)
2223 {
2224 p2 = pend;
2225 break;
2226 }
2227 }
2228
798c1fb8
ILT
2229 /* We use <= MAXDIFF here so that if we get a zero length
2230 file, we actually use the next file entry. */
252b5132
RH
2231 if (p2 < pend
2232 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
798c1fb8 2233 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
252b5132
RH
2234 {
2235 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2236 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2237 }
2238
2239 /* Avoid endless loops on erroneous files by ensuring that
2240 we always move forward in the file. */
2241 if (p - cof->raw_syments >= p->u.syment.n_value)
2242 break;
2243
2244 p = cof->raw_syments + p->u.syment.n_value;
2245 if (p > pend || p->u.syment.n_sclass != C_FILE)
2246 break;
2247 }
2248 }
2249
2250 /* Now wander though the raw linenumbers of the section */
2251 /* If we have been called on this section before, and the offset we
2252 want is further down then we can prime the lookup loop. */
2253 sec_data = coff_section_data (abfd, section);
2254 if (sec_data != NULL
2255 && sec_data->i > 0
2256 && offset >= sec_data->offset)
2257 {
2258 i = sec_data->i;
2259 *functionname_ptr = sec_data->function;
2260 line_base = sec_data->line_base;
2261 }
2262 else
2263 {
2264 i = 0;
2265 line_base = 0;
2266 }
2267
2268 if (section->lineno != NULL)
2269 {
798c1fb8
ILT
2270 bfd_vma last_value = 0;
2271
252b5132
RH
2272 l = &section->lineno[i];
2273
2274 for (; i < section->lineno_count; i++)
2275 {
2276 if (l->line_number == 0)
2277 {
2278 /* Get the symbol this line number points at */
2279 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2280 if (coff->symbol.value > offset)
2281 break;
2282 *functionname_ptr = coff->symbol.name;
798c1fb8 2283 last_value = coff->symbol.value;
252b5132
RH
2284 if (coff->native)
2285 {
2286 combined_entry_type *s = coff->native;
2287 s = s + 1 + s->u.syment.n_numaux;
2288
2289 /* In XCOFF a debugging symbol can follow the
2290 function symbol. */
2291 if (s->u.syment.n_scnum == N_DEBUG)
2292 s = s + 1 + s->u.syment.n_numaux;
2293
2294 /* S should now point to the .bf of the function. */
2295 if (s->u.syment.n_numaux)
2296 {
2297 /* The linenumber is stored in the auxent. */
2298 union internal_auxent *a = &((s + 1)->u.auxent);
2299 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2300 *line_ptr = line_base;
2301 }
2302 }
2303 }
2304 else
2305 {
2306 if (l->u.offset > offset)
2307 break;
2308 *line_ptr = l->line_number + line_base - 1;
2309 }
2310 l++;
2311 }
798c1fb8
ILT
2312
2313 /* If we fell off the end of the loop, then assume that this
2314 symbol has no line number info. Otherwise, symbols with no
2315 line number info get reported with the line number of the
2316 last line of the last symbol which does have line number
2317 info. We use 0x100 as a slop to account for cases where the
2318 last line has executable code. */
2319 if (i >= section->lineno_count
2320 && last_value != 0
2321 && offset - last_value > 0x100)
2322 {
2323 *functionname_ptr = NULL;
2324 *line_ptr = 0;
2325 }
252b5132
RH
2326 }
2327
2328 /* Cache the results for the next call. */
2329 if (sec_data == NULL && section->owner == abfd)
2330 {
2331 section->used_by_bfd =
2332 ((PTR) bfd_zalloc (abfd,
2333 sizeof (struct coff_section_tdata)));
2334 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2335 }
2336 if (sec_data != NULL)
2337 {
2338 sec_data->offset = offset;
2339 sec_data->i = i;
2340 sec_data->function = *functionname_ptr;
2341 sec_data->line_base = line_base;
2342 }
2343
2344 return true;
2345}
2346
2347int
2348coff_sizeof_headers (abfd, reloc)
2349 bfd *abfd;
2350 boolean reloc;
2351{
2352 size_t size;
2353
2354 if (reloc == false)
2355 {
2356 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2357 }
2358 else
2359 {
2360 size = bfd_coff_filhsz (abfd);
2361 }
2362
2363 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2364 return size;
2365}
2366
2367/* Change the class of a coff symbol held by BFD. */
2368boolean
2369bfd_coff_set_symbol_class (abfd, symbol, class)
2370 bfd * abfd;
2371 asymbol * symbol;
2372 unsigned int class;
2373{
2374 coff_symbol_type * csym;
2375
2376 csym = coff_symbol_from (abfd, symbol);
2377 if (csym == NULL)
2378 {
2379 bfd_set_error (bfd_error_invalid_operation);
2380 return false;
2381 }
2382 else if (csym->native == NULL)
2383 {
2384 /* This is an alien symbol which no native coff backend data.
2385 We cheat here by creating a fake native entry for it and
2386 then filling in the class. This code is based on that in
2387 coff_write_alien_symbol(). */
2388
2389 combined_entry_type * native;
2390
2391 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2392 if (native == NULL)
2393 return false;
2394
2395 memset (native, 0, sizeof (* native));
2396
2397 native->u.syment.n_type = T_NULL;
2398 native->u.syment.n_sclass = class;
2399
2400 if (bfd_is_und_section (symbol->section))
2401 {
2402 native->u.syment.n_scnum = N_UNDEF;
2403 native->u.syment.n_value = symbol->value;
2404 }
2405 else if (bfd_is_com_section (symbol->section))
2406 {
2407 native->u.syment.n_scnum = N_UNDEF;
2408 native->u.syment.n_value = symbol->value;
2409 }
2410 else
2411 {
2412 native->u.syment.n_scnum =
2413 symbol->section->output_section->target_index;
2414 native->u.syment.n_value = (symbol->value
2415 + symbol->section->output_offset);
2416 if (! obj_pe (abfd))
2417 native->u.syment.n_value += symbol->section->output_section->vma;
2418
2419 /* Copy the any flags from the the file header into the symbol.
2420 FIXME: Why? */
2421 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2422 }
2423
2424 csym->native = native;
2425 }
2426 else
2427 {
2428 csym->native->u.syment.n_sclass = class;
2429 }
2430
2431 return true;
2432}
2433
This page took 0.132573 seconds and 4 git commands to generate.