1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
25 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
27 /* This file contains COFF code that is not dependent on any
28 particular COFF target. There is only one version of this file in
29 libbfd.a, so no target specific code may be put in here. Or, to
32 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
34 If you need to add some target specific behaviour, add a new hook
35 function to bfd_coff_backend_data.
37 Some of these functions are also called by the ECOFF routines.
38 Those functions may not use any COFF specific information, such as
44 #include "coff/internal.h"
47 /* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
51 make_a_section_from_file (bfd *abfd,
52 struct internal_scnhdr *hdr,
53 unsigned int target_index)
55 asection *return_section;
57 bfd_boolean result = TRUE;
62 /* Handle long section names as in PE. On reading, we want to
63 accept long names if the format permits them at all, regardless
64 of the current state of the flag that dictates if we would generate
65 them in outputs; this construct checks if that is the case by
66 attempting to set the flag, without changing its state; the call
67 will fail for formats that do not support long names at all. */
68 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
69 && hdr->s_name[0] == '/')
76 /* Flag that this BFD uses long names, even though the format might
77 expect them to be off by default. This won't directly affect the
78 format of any output BFD created from this one, but the information
79 can be used to decide what to do. */
80 bfd_coff_set_long_section_names (abfd, TRUE);
81 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
82 buf[SCNNMLEN - 1] = '\0';
83 strindex = strtol (buf, &p, 10);
84 if (*p == '\0' && strindex >= 0)
86 strings = _bfd_coff_read_string_table (abfd);
89 /* FIXME: For extra safety, we should make sure that
90 strindex does not run us past the end, but right now we
91 don't know the length of the string table. */
93 name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
96 strcpy (name, strings);
102 /* Assorted wastage to null-terminate the name, thanks AT&T! */
103 name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107 name[sizeof (hdr->s_name)] = 0;
110 return_section = bfd_make_section_anyway (abfd, name);
111 if (return_section == NULL)
114 return_section->vma = hdr->s_vaddr;
115 return_section->lma = hdr->s_paddr;
116 return_section->size = hdr->s_size;
117 return_section->filepos = hdr->s_scnptr;
118 return_section->rel_filepos = hdr->s_relptr;
119 return_section->reloc_count = hdr->s_nreloc;
121 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
123 return_section->line_filepos = hdr->s_lnnoptr;
125 return_section->lineno_count = hdr->s_nlnno;
126 return_section->userdata = NULL;
127 return_section->next = NULL;
128 return_section->target_index = target_index;
130 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
134 return_section->flags = flags;
136 /* At least on i386-coff, the line number count for a shared library
137 section must be ignored. */
138 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139 return_section->lineno_count = 0;
141 if (hdr->s_nreloc != 0)
142 return_section->flags |= SEC_RELOC;
143 /* FIXME: should this check 'hdr->s_size > 0'. */
144 if (hdr->s_scnptr != 0)
145 return_section->flags |= SEC_HAS_CONTENTS;
150 /* Read in a COFF object and make it into a BFD. This is used by
153 static const bfd_target *
154 coff_real_object_p (bfd *abfd,
156 struct internal_filehdr *internal_f,
157 struct internal_aouthdr *internal_a)
159 flagword oflags = abfd->flags;
160 bfd_vma ostart = bfd_get_start_address (abfd);
163 bfd_size_type readsize; /* Length of file_info. */
165 char *external_sections;
167 if (!(internal_f->f_flags & F_RELFLG))
168 abfd->flags |= HAS_RELOC;
169 if ((internal_f->f_flags & F_EXEC))
170 abfd->flags |= EXEC_P;
171 if (!(internal_f->f_flags & F_LNNO))
172 abfd->flags |= HAS_LINENO;
173 if (!(internal_f->f_flags & F_LSYMS))
174 abfd->flags |= HAS_LOCALS;
176 /* FIXME: How can we set D_PAGED correctly? */
177 if ((internal_f->f_flags & F_EXEC) != 0)
178 abfd->flags |= D_PAGED;
180 bfd_get_symcount (abfd) = internal_f->f_nsyms;
181 if (internal_f->f_nsyms)
182 abfd->flags |= HAS_SYMS;
184 if (internal_a != (struct internal_aouthdr *) NULL)
185 bfd_get_start_address (abfd) = internal_a->entry;
187 bfd_get_start_address (abfd) = 0;
189 /* Set up the tdata area. ECOFF uses its own routine, and overrides
191 tdata_save = abfd->tdata.any;
192 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
196 scnhsz = bfd_coff_scnhsz (abfd);
197 readsize = (bfd_size_type) nscns * scnhsz;
198 external_sections = bfd_alloc (abfd, readsize);
199 if (!external_sections)
202 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
205 /* Set the arch/mach *before* swapping in sections; section header swapping
206 may depend on arch/mach info. */
207 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
210 /* Now copy data as required; construct all asections etc. */
214 for (i = 0; i < nscns; i++)
216 struct internal_scnhdr tmp;
217 bfd_coff_swap_scnhdr_in (abfd,
218 (void *) (external_sections + i * scnhsz),
220 if (! make_a_section_from_file (abfd, &tmp, i + 1))
228 bfd_release (abfd, tdata);
230 abfd->tdata.any = tdata_save;
231 abfd->flags = oflags;
232 bfd_get_start_address (abfd) = ostart;
233 return (const bfd_target *) NULL;
236 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
237 not a COFF file. This is also used by ECOFF. */
240 coff_object_p (bfd *abfd)
242 bfd_size_type filhsz;
243 bfd_size_type aoutsz;
246 struct internal_filehdr internal_f;
247 struct internal_aouthdr internal_a;
249 /* Figure out how much to read. */
250 filhsz = bfd_coff_filhsz (abfd);
251 aoutsz = bfd_coff_aoutsz (abfd);
253 filehdr = bfd_alloc (abfd, filhsz);
256 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
258 if (bfd_get_error () != bfd_error_system_call)
259 bfd_set_error (bfd_error_wrong_format);
260 bfd_release (abfd, filehdr);
263 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
264 bfd_release (abfd, filehdr);
266 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
267 (less than aoutsz) used in object files and AOUTSZ (equal to
268 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
269 expects this header to be aoutsz bytes in length, so we use that
270 value in the call to bfd_alloc below. But we must be careful to
271 only read in f_opthdr bytes in the call to bfd_bread. We should
272 also attempt to catch corrupt or non-COFF binaries with a strange
273 value for f_opthdr. */
274 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
275 || internal_f.f_opthdr > aoutsz)
277 bfd_set_error (bfd_error_wrong_format);
280 nscns = internal_f.f_nscns;
282 if (internal_f.f_opthdr)
286 opthdr = bfd_alloc (abfd, aoutsz);
289 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
290 != internal_f.f_opthdr)
292 bfd_release (abfd, opthdr);
295 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
296 bfd_release (abfd, opthdr);
299 return coff_real_object_p (abfd, nscns, &internal_f,
300 (internal_f.f_opthdr != 0
302 : (struct internal_aouthdr *) NULL));
305 /* Get the BFD section from a COFF symbol section number. */
308 coff_section_from_bfd_index (bfd *abfd, int index)
310 struct bfd_section *answer = abfd->sections;
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;
321 if (answer->target_index == index)
323 answer = answer->next;
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;
331 /* Get the upper bound of a COFF symbol table. */
334 coff_get_symtab_upper_bound (bfd *abfd)
336 if (!bfd_coff_slurp_symbol_table (abfd))
339 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
342 /* Canonicalize a COFF symbol table. */
345 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
347 unsigned int counter;
348 coff_symbol_type *symbase;
349 coff_symbol_type **location = (coff_symbol_type **) alocation;
351 if (!bfd_coff_slurp_symbol_table (abfd))
354 symbase = obj_symbols (abfd);
355 counter = bfd_get_symcount (abfd);
356 while (counter-- > 0)
357 *location++ = symbase++;
361 return bfd_get_symcount (abfd);
364 /* Get the name of a symbol. The caller must pass in a buffer of size
368 _bfd_coff_internal_syment_name (bfd *abfd,
369 const struct internal_syment *sym,
372 /* FIXME: It's not clear this will work correctly if sizeof
374 if (sym->_n._n_n._n_zeroes != 0
375 || sym->_n._n_n._n_offset == 0)
377 memcpy (buf, sym->_n._n_name, SYMNMLEN);
378 buf[SYMNMLEN] = '\0';
385 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
386 strings = obj_coff_strings (abfd);
389 strings = _bfd_coff_read_string_table (abfd);
393 return strings + sym->_n._n_n._n_offset;
397 /* Read in and swap the relocs. This returns a buffer holding the
398 relocs for section SEC in file ABFD. If CACHE is TRUE and
399 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
400 the function is called again. If EXTERNAL_RELOCS is not NULL, it
401 is a buffer large enough to hold the unswapped relocs. If
402 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
403 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
404 value must be INTERNAL_RELOCS. The function returns NULL on error. */
406 struct internal_reloc *
407 _bfd_coff_read_internal_relocs (bfd *abfd,
410 bfd_byte *external_relocs,
411 bfd_boolean require_internal,
412 struct internal_reloc *internal_relocs)
415 bfd_byte *free_external = NULL;
416 struct internal_reloc *free_internal = NULL;
419 struct internal_reloc *irel;
422 if (sec->reloc_count == 0)
423 return internal_relocs; /* Nothing to do. */
425 if (coff_section_data (abfd, sec) != NULL
426 && coff_section_data (abfd, sec)->relocs != NULL)
428 if (! require_internal)
429 return coff_section_data (abfd, sec)->relocs;
430 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
431 sec->reloc_count * sizeof (struct internal_reloc));
432 return internal_relocs;
435 relsz = bfd_coff_relsz (abfd);
437 amt = sec->reloc_count * relsz;
438 if (external_relocs == NULL)
440 free_external = bfd_malloc (amt);
441 if (free_external == NULL)
443 external_relocs = free_external;
446 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
447 || bfd_bread (external_relocs, amt, abfd) != amt)
450 if (internal_relocs == NULL)
452 amt = sec->reloc_count;
453 amt *= sizeof (struct internal_reloc);
454 free_internal = bfd_malloc (amt);
455 if (free_internal == NULL)
457 internal_relocs = free_internal;
460 /* Swap in the relocs. */
461 erel = external_relocs;
462 erel_end = erel + relsz * sec->reloc_count;
463 irel = internal_relocs;
464 for (; erel < erel_end; erel += relsz, irel++)
465 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
467 if (free_external != NULL)
469 free (free_external);
470 free_external = NULL;
473 if (cache && free_internal != NULL)
475 if (coff_section_data (abfd, sec) == NULL)
477 amt = sizeof (struct coff_section_tdata);
478 sec->used_by_bfd = bfd_zalloc (abfd, amt);
479 if (sec->used_by_bfd == NULL)
481 coff_section_data (abfd, sec)->contents = NULL;
483 coff_section_data (abfd, sec)->relocs = free_internal;
486 return internal_relocs;
489 if (free_external != NULL)
490 free (free_external);
491 if (free_internal != NULL)
492 free (free_internal);
496 /* Set lineno_count for the output sections of a COFF file. */
499 coff_count_linenumbers (bfd *abfd)
501 unsigned int limit = bfd_get_symcount (abfd);
509 /* This may be from the backend linker, in which case the
510 lineno_count in the sections is correct. */
511 for (s = abfd->sections; s != NULL; s = s->next)
512 total += s->lineno_count;
516 for (s = abfd->sections; s != NULL; s = s->next)
517 BFD_ASSERT (s->lineno_count == 0);
519 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
521 asymbol *q_maybe = *p;
523 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
525 coff_symbol_type *q = coffsymbol (q_maybe);
527 /* The AIX 4.1 compiler can sometimes generate line numbers
528 attached to debugging symbols. We try to simply ignore
530 if (q->lineno != NULL
531 && q->symbol.section->owner != NULL)
533 /* This symbol has line numbers. Increment the owning
534 section's linenumber count. */
535 alent *l = q->lineno;
539 asection * sec = q->symbol.section->output_section;
541 /* Do not try to update fields in read-only sections. */
542 if (! bfd_is_const_section (sec))
543 sec->lineno_count ++;
548 while (l->line_number != 0);
556 /* Takes a bfd and a symbol, returns a pointer to the coff specific
557 area of the symbol if there is one. */
560 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
563 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
564 return (coff_symbol_type *) NULL;
566 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
567 return (coff_symbol_type *) NULL;
569 return (coff_symbol_type *) symbol;
573 fixup_symbol_value (bfd *abfd,
574 coff_symbol_type *coff_symbol_ptr,
575 struct internal_syment *syment)
577 /* Normalize the symbol flags. */
578 if (coff_symbol_ptr->symbol.section
579 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
581 /* A common symbol is undefined with a value. */
582 syment->n_scnum = N_UNDEF;
583 syment->n_value = coff_symbol_ptr->symbol.value;
585 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
586 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
588 syment->n_value = coff_symbol_ptr->symbol.value;
590 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
592 syment->n_scnum = N_UNDEF;
595 /* FIXME: Do we need to handle the absolute section here? */
598 if (coff_symbol_ptr->symbol.section)
601 coff_symbol_ptr->symbol.section->output_section->target_index;
603 syment->n_value = (coff_symbol_ptr->symbol.value
604 + coff_symbol_ptr->symbol.section->output_offset);
607 syment->n_value += (syment->n_sclass == C_STATLAB)
608 ? coff_symbol_ptr->symbol.section->output_section->lma
609 : coff_symbol_ptr->symbol.section->output_section->vma;
615 /* This can happen, but I don't know why yet (steve@cygnus.com) */
616 syment->n_scnum = N_ABS;
617 syment->n_value = coff_symbol_ptr->symbol.value;
622 /* Run through all the symbols in the symbol table and work out what
623 their indexes into the symbol table will be when output.
625 Coff requires that each C_FILE symbol points to the next one in the
626 chain, and that the last one points to the first external symbol. We
630 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
632 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
633 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
634 unsigned int native_index = 0;
635 struct internal_syment *last_file = NULL;
636 unsigned int symbol_index;
638 /* COFF demands that undefined symbols come after all other symbols.
639 Since we don't need to impose this extra knowledge on all our
640 client programs, deal with that here. Sort the symbol table;
641 just move the undefined symbols to the end, leaving the rest
642 alone. The O'Reilly book says that defined global symbols come
643 at the end before the undefined symbols, so we do that here as
645 /* @@ Do we have some condition we could test for, so we don't always
646 have to do this? I don't think relocatability is quite right, but
647 I'm not certain. [raeburn:19920508.1711EST] */
653 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
654 newsyms = bfd_alloc (bfd_ptr, amt);
657 bfd_ptr->outsymbols = newsyms;
658 for (i = 0; i < symbol_count; i++)
659 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
660 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
661 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
662 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
663 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
665 *newsyms++ = symbol_ptr_ptr[i];
667 for (i = 0; i < symbol_count; i++)
668 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
669 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
670 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
671 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
672 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
674 *newsyms++ = symbol_ptr_ptr[i];
676 *first_undef = newsyms - bfd_ptr->outsymbols;
678 for (i = 0; i < symbol_count; i++)
679 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
680 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
681 *newsyms++ = symbol_ptr_ptr[i];
682 *newsyms = (asymbol *) NULL;
683 symbol_ptr_ptr = bfd_ptr->outsymbols;
686 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
688 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
689 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
690 if (coff_symbol_ptr && coff_symbol_ptr->native)
692 combined_entry_type *s = coff_symbol_ptr->native;
695 if (s->u.syment.n_sclass == C_FILE)
697 if (last_file != NULL)
698 last_file->n_value = native_index;
699 last_file = &(s->u.syment);
702 /* Modify the symbol values according to their section and
704 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
706 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
707 s[i].offset = native_index++;
713 obj_conv_table_size (bfd_ptr) = native_index;
718 /* Run thorough the symbol table again, and fix it so that all
719 pointers to entries are changed to the entries' index in the output
723 coff_mangle_symbols (bfd *bfd_ptr)
725 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
726 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
727 unsigned int symbol_index;
729 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
731 coff_symbol_type *coff_symbol_ptr =
732 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
734 if (coff_symbol_ptr && coff_symbol_ptr->native)
737 combined_entry_type *s = coff_symbol_ptr->native;
741 /* FIXME: We should use a union here. */
742 s->u.syment.n_value =
743 (bfd_hostptr_t) ((combined_entry_type *)
744 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
749 /* The value is the offset into the line number entries
750 for the symbol's section. On output, the symbol's
751 section should be N_DEBUG. */
752 s->u.syment.n_value =
753 (coff_symbol_ptr->symbol.section->output_section->line_filepos
754 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
755 coff_symbol_ptr->symbol.section =
756 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
757 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
759 for (i = 0; i < s->u.syment.n_numaux; i++)
761 combined_entry_type *a = s + i + 1;
764 a->u.auxent.x_sym.x_tagndx.l =
765 a->u.auxent.x_sym.x_tagndx.p->offset;
770 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
771 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
776 a->u.auxent.x_csect.x_scnlen.l =
777 a->u.auxent.x_csect.x_scnlen.p->offset;
786 coff_fix_symbol_name (bfd *abfd,
788 combined_entry_type *native,
789 bfd_size_type *string_size_p,
790 asection **debug_string_section_p,
791 bfd_size_type *debug_string_size_p)
793 unsigned int name_length;
794 union internal_auxent *auxent;
795 char *name = (char *) (symbol->name);
799 /* COFF symbols always have names, so we'll make one up. */
800 symbol->name = "strange";
801 name = (char *) symbol->name;
803 name_length = strlen (name);
805 if (native->u.syment.n_sclass == C_FILE
806 && native->u.syment.n_numaux > 0)
808 unsigned int filnmlen;
810 if (bfd_coff_force_symnames_in_strings (abfd))
812 native->u.syment._n._n_n._n_offset =
813 (*string_size_p + STRING_SIZE_SIZE);
814 native->u.syment._n._n_n._n_zeroes = 0;
815 *string_size_p += 6; /* strlen(".file") + 1 */
818 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
820 auxent = &(native + 1)->u.auxent;
822 filnmlen = bfd_coff_filnmlen (abfd);
824 if (bfd_coff_long_filenames (abfd))
826 if (name_length <= filnmlen)
827 strncpy (auxent->x_file.x_fname, name, filnmlen);
830 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
831 auxent->x_file.x_n.x_zeroes = 0;
832 *string_size_p += name_length + 1;
837 strncpy (auxent->x_file.x_fname, name, filnmlen);
838 if (name_length > filnmlen)
839 name[filnmlen] = '\0';
844 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
845 /* This name will fit into the symbol neatly. */
846 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
848 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
850 native->u.syment._n._n_n._n_offset = (*string_size_p
852 native->u.syment._n._n_n._n_zeroes = 0;
853 *string_size_p += name_length + 1;
859 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
861 /* This name should be written into the .debug section. For
862 some reason each name is preceded by a two byte length
863 and also followed by a null byte. FIXME: We assume that
864 the .debug section has already been created, and that it
866 if (*debug_string_section_p == (asection *) NULL)
867 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
868 filepos = bfd_tell (abfd);
870 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
872 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
874 if (!bfd_set_section_contents (abfd,
875 *debug_string_section_p,
877 (file_ptr) *debug_string_size_p,
878 (bfd_size_type) prefix_len)
879 || !bfd_set_section_contents (abfd,
880 *debug_string_section_p,
881 (void *) symbol->name,
882 (file_ptr) (*debug_string_size_p
884 (bfd_size_type) name_length + 1))
886 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
888 native->u.syment._n._n_n._n_offset =
889 *debug_string_size_p + prefix_len;
890 native->u.syment._n._n_n._n_zeroes = 0;
891 *debug_string_size_p += name_length + 1 + prefix_len;
896 /* We need to keep track of the symbol index so that when we write out
897 the relocs we can get the index for a symbol. This method is a
900 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
902 /* Write a symbol out to a COFF file. */
905 coff_write_symbol (bfd *abfd,
907 combined_entry_type *native,
909 bfd_size_type *string_size_p,
910 asection **debug_string_section_p,
911 bfd_size_type *debug_string_size_p)
913 unsigned int numaux = native->u.syment.n_numaux;
914 int type = native->u.syment.n_type;
915 int class = native->u.syment.n_sclass;
917 bfd_size_type symesz;
919 if (native->u.syment.n_sclass == C_FILE)
920 symbol->flags |= BSF_DEBUGGING;
922 if (symbol->flags & BSF_DEBUGGING
923 && bfd_is_abs_section (symbol->section))
924 native->u.syment.n_scnum = N_DEBUG;
926 else if (bfd_is_abs_section (symbol->section))
927 native->u.syment.n_scnum = N_ABS;
929 else if (bfd_is_und_section (symbol->section))
930 native->u.syment.n_scnum = N_UNDEF;
933 native->u.syment.n_scnum =
934 symbol->section->output_section->target_index;
936 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
937 debug_string_section_p, debug_string_size_p);
939 symesz = bfd_coff_symesz (abfd);
940 buf = bfd_alloc (abfd, symesz);
943 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
944 if (bfd_bwrite (buf, symesz, abfd) != symesz)
946 bfd_release (abfd, buf);
948 if (native->u.syment.n_numaux > 0)
950 bfd_size_type auxesz;
953 auxesz = bfd_coff_auxesz (abfd);
954 buf = bfd_alloc (abfd, auxesz);
957 for (j = 0; j < native->u.syment.n_numaux; j++)
959 bfd_coff_swap_aux_out (abfd,
960 &((native + j + 1)->u.auxent),
961 type, class, (int) j,
962 native->u.syment.n_numaux,
964 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
967 bfd_release (abfd, buf);
970 /* Store the index for use when we write out the relocs. */
971 set_index (symbol, *written);
973 *written += numaux + 1;
977 /* Write out a symbol to a COFF file that does not come from a COFF
978 file originally. This symbol may have been created by the linker,
979 or we may be linking a non COFF file to a COFF file. */
982 coff_write_alien_symbol (bfd *abfd,
985 bfd_size_type *string_size_p,
986 asection **debug_string_section_p,
987 bfd_size_type *debug_string_size_p)
989 combined_entry_type *native;
990 combined_entry_type dummy;
993 native->u.syment.n_type = T_NULL;
994 native->u.syment.n_flags = 0;
995 if (bfd_is_und_section (symbol->section))
997 native->u.syment.n_scnum = N_UNDEF;
998 native->u.syment.n_value = symbol->value;
1000 else if (bfd_is_com_section (symbol->section))
1002 native->u.syment.n_scnum = N_UNDEF;
1003 native->u.syment.n_value = symbol->value;
1005 else if (symbol->flags & BSF_DEBUGGING)
1007 /* There isn't much point to writing out a debugging symbol
1008 unless we are prepared to convert it into COFF debugging
1009 format. So, we just ignore them. We must clobber the symbol
1010 name to keep it from being put in the string table. */
1016 native->u.syment.n_scnum =
1017 symbol->section->output_section->target_index;
1018 native->u.syment.n_value = (symbol->value
1019 + symbol->section->output_offset);
1020 if (! obj_pe (abfd))
1021 native->u.syment.n_value += symbol->section->output_section->vma;
1023 /* Copy the any flags from the file header into the symbol.
1026 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1027 if (c != (coff_symbol_type *) NULL)
1028 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1032 native->u.syment.n_type = 0;
1033 if (symbol->flags & BSF_LOCAL)
1034 native->u.syment.n_sclass = C_STAT;
1035 else if (symbol->flags & BSF_WEAK)
1036 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1038 native->u.syment.n_sclass = C_EXT;
1039 native->u.syment.n_numaux = 0;
1041 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1042 debug_string_section_p, debug_string_size_p);
1045 /* Write a native symbol to a COFF file. */
1048 coff_write_native_symbol (bfd *abfd,
1049 coff_symbol_type *symbol,
1051 bfd_size_type *string_size_p,
1052 asection **debug_string_section_p,
1053 bfd_size_type *debug_string_size_p)
1055 combined_entry_type *native = symbol->native;
1056 alent *lineno = symbol->lineno;
1058 /* If this symbol has an associated line number, we must store the
1059 symbol index in the line number field. We also tag the auxent to
1060 point to the right place in the lineno table. */
1061 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1063 unsigned int count = 0;
1065 lineno[count].u.offset = *written;
1066 if (native->u.syment.n_numaux)
1068 union internal_auxent *a = &((native + 1)->u.auxent);
1070 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1071 symbol->symbol.section->output_section->moving_line_filepos;
1074 /* Count and relocate all other linenumbers. */
1076 while (lineno[count].line_number != 0)
1078 lineno[count].u.offset +=
1079 (symbol->symbol.section->output_section->vma
1080 + symbol->symbol.section->output_offset);
1083 symbol->done_lineno = TRUE;
1085 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1086 symbol->symbol.section->output_section->moving_line_filepos +=
1087 count * bfd_coff_linesz (abfd);
1090 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1091 string_size_p, debug_string_section_p,
1092 debug_string_size_p);
1096 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1100 /* Write out the COFF symbols. */
1103 coff_write_symbols (bfd *abfd)
1105 bfd_size_type string_size;
1106 asection *debug_string_section;
1107 bfd_size_type debug_string_size;
1109 unsigned int limit = bfd_get_symcount (abfd);
1110 bfd_vma written = 0;
1114 debug_string_section = NULL;
1115 debug_string_size = 0;
1117 /* If this target supports long section names, they must be put into
1118 the string table. This is supported by PE. This code must
1119 handle section names just as they are handled in
1120 coff_write_object_contents. */
1121 if (bfd_coff_long_section_names (abfd))
1125 for (o = abfd->sections; o != NULL; o = o->next)
1129 len = strlen (o->name);
1131 string_size += len + 1;
1135 /* Seek to the right place. */
1136 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1139 /* Output all the symbols we have. */
1141 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1143 asymbol *symbol = *p;
1144 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1146 if (c_symbol == (coff_symbol_type *) NULL
1147 || c_symbol->native == (combined_entry_type *) NULL)
1149 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1150 &debug_string_section,
1151 &debug_string_size))
1156 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1158 bfd_error_handler_type current_error_handler;
1159 enum coff_symbol_classification class;
1160 unsigned char *n_sclass;
1162 /* Suppress error reporting by bfd_coff_classify_symbol.
1163 Error messages can be generated when we are processing a local
1164 symbol which has no associated section and we do not have to
1165 worry about this, all we need to know is that it is local. */
1166 current_error_handler = bfd_set_error_handler (null_error_handler);
1167 class = bfd_coff_classify_symbol (abfd, &c_symbol->native->u.syment);
1168 (void) bfd_set_error_handler (current_error_handler);
1170 n_sclass = &c_symbol->native->u.syment.n_sclass;
1172 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1173 we cannot retain the existing sclass from the original symbol.
1174 Weak symbols only have one valid sclass, so just set it always.
1175 If it is not local class and should be, set it C_STAT.
1176 If it is global and not classified as global, or if it is
1177 weak (which is also classified as global), set it C_EXT. */
1179 if (symbol->flags & BSF_WEAK)
1180 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1181 else if (symbol->flags & BSF_LOCAL && class != COFF_SYMBOL_LOCAL)
1183 else if (symbol->flags & BSF_GLOBAL
1184 && (class != COFF_SYMBOL_GLOBAL
1186 || *n_sclass == C_NT_WEAK
1188 || *n_sclass == C_WEAKEXT))
1189 c_symbol->native->u.syment.n_sclass = C_EXT;
1192 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1193 &string_size, &debug_string_section,
1194 &debug_string_size))
1199 obj_raw_syment_count (abfd) = written;
1201 /* Now write out strings. */
1202 if (string_size != 0)
1204 unsigned int size = string_size + STRING_SIZE_SIZE;
1205 bfd_byte buffer[STRING_SIZE_SIZE];
1207 #if STRING_SIZE_SIZE == 4
1208 H_PUT_32 (abfd, size, buffer);
1210 #error Change H_PUT_32
1212 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1216 /* Handle long section names. This code must handle section
1217 names just as they are handled in coff_write_object_contents. */
1218 if (bfd_coff_long_section_names (abfd))
1222 for (o = abfd->sections; o != NULL; o = o->next)
1226 len = strlen (o->name);
1229 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1236 for (p = abfd->outsymbols, i = 0;
1241 size_t name_length = strlen (q->name);
1242 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1245 /* Figure out whether the symbol name should go in the string
1246 table. Symbol names that are short enough are stored
1247 directly in the syment structure. File names permit a
1248 different, longer, length in the syment structure. On
1249 XCOFF, some symbol names are stored in the .debug section
1250 rather than in the string table. */
1252 if (c_symbol == NULL
1253 || c_symbol->native == NULL)
1254 /* This is not a COFF symbol, so it certainly is not a
1255 file name, nor does it go in the .debug section. */
1256 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1258 else if (bfd_coff_symname_in_debug (abfd,
1259 &c_symbol->native->u.syment))
1260 /* This symbol name is in the XCOFF .debug section.
1261 Don't write it into the string table. */
1262 maxlen = name_length;
1264 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1265 && c_symbol->native->u.syment.n_numaux > 0)
1267 if (bfd_coff_force_symnames_in_strings (abfd))
1269 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1272 maxlen = bfd_coff_filnmlen (abfd);
1275 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1277 if (name_length > maxlen)
1279 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1280 abfd) != name_length + 1)
1287 /* We would normally not write anything here, but we'll write
1288 out 4 so that any stupid coff reader which tries to read the
1289 string table even when there isn't one won't croak. */
1290 unsigned int size = STRING_SIZE_SIZE;
1291 bfd_byte buffer[STRING_SIZE_SIZE];
1293 #if STRING_SIZE_SIZE == 4
1294 H_PUT_32 (abfd, size, buffer);
1296 #error Change H_PUT_32
1298 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1299 != STRING_SIZE_SIZE)
1303 /* Make sure the .debug section was created to be the correct size.
1304 We should create it ourselves on the fly, but we don't because
1305 BFD won't let us write to any section until we know how large all
1306 the sections are. We could still do it by making another pass
1307 over the symbols. FIXME. */
1308 BFD_ASSERT (debug_string_size == 0
1309 || (debug_string_section != (asection *) NULL
1310 && (BFD_ALIGN (debug_string_size,
1311 1 << debug_string_section->alignment_power)
1312 == debug_string_section->size)));
1318 coff_write_linenumbers (bfd *abfd)
1321 bfd_size_type linesz;
1324 linesz = bfd_coff_linesz (abfd);
1325 buff = bfd_alloc (abfd, linesz);
1328 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1330 if (s->lineno_count)
1332 asymbol **q = abfd->outsymbols;
1333 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1335 /* Find all the linenumbers in this section. */
1339 if (p->section->output_section == s)
1342 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1343 (bfd_asymbol_bfd (p), p));
1346 /* Found a linenumber entry, output. */
1347 struct internal_lineno out;
1348 memset ((void *) & out, 0, sizeof (out));
1350 out.l_addr.l_symndx = l->u.offset;
1351 bfd_coff_swap_lineno_out (abfd, &out, buff);
1352 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1356 while (l->line_number)
1358 out.l_lnno = l->line_number;
1359 out.l_addr.l_symndx = l->u.offset;
1360 bfd_coff_swap_lineno_out (abfd, &out, buff);
1361 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1372 bfd_release (abfd, buff);
1377 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1379 return coffsymbol (symbol)->lineno;
1382 /* This function transforms the offsets into the symbol table into
1383 pointers to syments. */
1386 coff_pointerize_aux (bfd *abfd,
1387 combined_entry_type *table_base,
1388 combined_entry_type *symbol,
1389 unsigned int indaux,
1390 combined_entry_type *auxent)
1392 unsigned int type = symbol->u.syment.n_type;
1393 unsigned int class = symbol->u.syment.n_sclass;
1395 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1397 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1398 (abfd, table_base, symbol, indaux, auxent))
1402 /* Don't bother if this is a file or a section. */
1403 if (class == C_STAT && type == T_NULL)
1405 if (class == C_FILE)
1408 /* Otherwise patch up. */
1409 #define N_TMASK coff_data (abfd)->local_n_tmask
1410 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1412 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1413 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1415 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1416 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1417 auxent->fix_end = 1;
1419 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1420 generate one, so we must be careful to ignore it. */
1421 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1423 auxent->u.auxent.x_sym.x_tagndx.p =
1424 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1425 auxent->fix_tag = 1;
1429 /* Allocate space for the ".debug" section, and read it.
1430 We did not read the debug section until now, because
1431 we didn't want to go to the trouble until someone needed it. */
1434 build_debug_section (bfd *abfd)
1436 char *debug_section;
1438 bfd_size_type sec_size;
1440 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1444 bfd_set_error (bfd_error_no_debug_section);
1448 sec_size = sect->size;
1449 debug_section = bfd_alloc (abfd, sec_size);
1450 if (debug_section == NULL)
1453 /* Seek to the beginning of the `.debug' section and read it.
1454 Save the current position first; it is needed by our caller.
1455 Then read debug section and reset the file pointer. */
1457 position = bfd_tell (abfd);
1458 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1459 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1460 || bfd_seek (abfd, position, SEEK_SET) != 0)
1462 return debug_section;
1465 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1466 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1467 be \0-terminated. */
1470 copy_name (bfd *abfd, char *name, size_t maxlen)
1475 for (len = 0; len < maxlen; ++len)
1476 if (name[len] == '\0')
1479 if ((newname = bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1482 strncpy (newname, name, len);
1483 newname[len] = '\0';
1487 /* Read in the external symbols. */
1490 _bfd_coff_get_external_symbols (bfd *abfd)
1492 bfd_size_type symesz;
1496 if (obj_coff_external_syms (abfd) != NULL)
1499 symesz = bfd_coff_symesz (abfd);
1501 size = obj_raw_syment_count (abfd) * symesz;
1505 syms = bfd_malloc (size);
1509 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1510 || bfd_bread (syms, size, abfd) != size)
1517 obj_coff_external_syms (abfd) = syms;
1522 /* Read in the external strings. The strings are not loaded until
1523 they are needed. This is because we have no simple way of
1524 detecting a missing string table in an archive. */
1527 _bfd_coff_read_string_table (bfd *abfd)
1529 char extstrsize[STRING_SIZE_SIZE];
1530 bfd_size_type strsize;
1534 if (obj_coff_strings (abfd) != NULL)
1535 return obj_coff_strings (abfd);
1537 if (obj_sym_filepos (abfd) == 0)
1539 bfd_set_error (bfd_error_no_symbols);
1543 pos = obj_sym_filepos (abfd);
1544 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1545 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1548 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1549 != sizeof extstrsize)
1551 if (bfd_get_error () != bfd_error_file_truncated)
1554 /* There is no string table. */
1555 strsize = STRING_SIZE_SIZE;
1559 #if STRING_SIZE_SIZE == 4
1560 strsize = H_GET_32 (abfd, extstrsize);
1562 #error Change H_GET_32
1566 if (strsize < STRING_SIZE_SIZE)
1568 (*_bfd_error_handler)
1569 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1570 bfd_set_error (bfd_error_bad_value);
1574 strings = bfd_malloc (strsize);
1575 if (strings == NULL)
1578 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1579 != strsize - STRING_SIZE_SIZE)
1585 obj_coff_strings (abfd) = strings;
1590 /* Free up the external symbols and strings read from a COFF file. */
1593 _bfd_coff_free_symbols (bfd *abfd)
1595 if (obj_coff_external_syms (abfd) != NULL
1596 && ! obj_coff_keep_syms (abfd))
1598 free (obj_coff_external_syms (abfd));
1599 obj_coff_external_syms (abfd) = NULL;
1601 if (obj_coff_strings (abfd) != NULL
1602 && ! obj_coff_keep_strings (abfd))
1604 free (obj_coff_strings (abfd));
1605 obj_coff_strings (abfd) = NULL;
1610 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1611 knit the symbol names into a normalized form. By normalized here I
1612 mean that all symbols have an n_offset pointer that points to a null-
1613 terminated string. */
1615 combined_entry_type *
1616 coff_get_normalized_symtab (bfd *abfd)
1618 combined_entry_type *internal;
1619 combined_entry_type *internal_ptr;
1620 combined_entry_type *symbol_ptr;
1621 combined_entry_type *internal_end;
1625 const char *string_table = NULL;
1626 char *debug_section = NULL;
1629 if (obj_raw_syments (abfd) != NULL)
1630 return obj_raw_syments (abfd);
1632 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1633 internal = bfd_zalloc (abfd, size);
1634 if (internal == NULL && size != 0)
1636 internal_end = internal + obj_raw_syment_count (abfd);
1638 if (! _bfd_coff_get_external_symbols (abfd))
1641 raw_src = (char *) obj_coff_external_syms (abfd);
1643 /* Mark the end of the symbols. */
1644 symesz = bfd_coff_symesz (abfd);
1645 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1647 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1648 probably possible. If one shows up, it will probably kill us. */
1650 /* Swap all the raw entries. */
1651 for (internal_ptr = internal;
1653 raw_src += symesz, internal_ptr++)
1657 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1658 (void *) & internal_ptr->u.syment);
1659 symbol_ptr = internal_ptr;
1662 i < symbol_ptr->u.syment.n_numaux;
1667 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1668 symbol_ptr->u.syment.n_type,
1669 symbol_ptr->u.syment.n_sclass,
1670 (int) i, symbol_ptr->u.syment.n_numaux,
1671 &(internal_ptr->u.auxent));
1672 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1677 /* Free the raw symbols, but not the strings (if we have them). */
1678 obj_coff_keep_strings (abfd) = TRUE;
1679 if (! _bfd_coff_free_symbols (abfd))
1682 for (internal_ptr = internal; internal_ptr < internal_end;
1685 if (internal_ptr->u.syment.n_sclass == C_FILE
1686 && internal_ptr->u.syment.n_numaux > 0)
1688 /* Make a file symbol point to the name in the auxent, since
1689 the text ".file" is redundant. */
1690 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1692 /* The filename is a long one, point into the string table. */
1693 if (string_table == NULL)
1695 string_table = _bfd_coff_read_string_table (abfd);
1696 if (string_table == NULL)
1700 internal_ptr->u.syment._n._n_n._n_offset =
1703 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1707 /* Ordinary short filename, put into memory anyway. The
1708 Microsoft PE tools sometimes store a filename in
1709 multiple AUX entries. */
1710 if (internal_ptr->u.syment.n_numaux > 1
1711 && coff_data (abfd)->pe)
1712 internal_ptr->u.syment._n._n_n._n_offset =
1715 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1716 internal_ptr->u.syment.n_numaux * symesz));
1718 internal_ptr->u.syment._n._n_n._n_offset =
1721 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1722 (size_t) bfd_coff_filnmlen (abfd)));
1727 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1729 /* This is a "short" name. Make it long. */
1733 /* Find the length of this string without walking into memory
1735 for (i = 0; i < 8; ++i)
1736 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1739 newstring = bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1740 if (newstring == NULL)
1742 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1743 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1744 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1746 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1747 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1748 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1750 /* Long name already. Point symbol at the string in the
1752 if (string_table == NULL)
1754 string_table = _bfd_coff_read_string_table (abfd);
1755 if (string_table == NULL)
1758 internal_ptr->u.syment._n._n_n._n_offset =
1761 + internal_ptr->u.syment._n._n_n._n_offset));
1765 /* Long name in debug section. Very similar. */
1766 if (debug_section == NULL)
1767 debug_section = build_debug_section (abfd);
1768 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1769 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1772 internal_ptr += internal_ptr->u.syment.n_numaux;
1775 obj_raw_syments (abfd) = internal;
1776 BFD_ASSERT (obj_raw_syment_count (abfd)
1777 == (unsigned int) (internal_ptr - internal));
1783 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1785 if (bfd_get_format (abfd) != bfd_object)
1787 bfd_set_error (bfd_error_invalid_operation);
1790 return (asect->reloc_count + 1) * sizeof (arelent *);
1794 coff_make_empty_symbol (bfd *abfd)
1796 bfd_size_type amt = sizeof (coff_symbol_type);
1797 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1799 if (new_symbol == NULL)
1801 new_symbol->symbol.section = 0;
1802 new_symbol->native = 0;
1803 new_symbol->lineno = NULL;
1804 new_symbol->done_lineno = FALSE;
1805 new_symbol->symbol.the_bfd = abfd;
1807 return & new_symbol->symbol;
1810 /* Make a debugging symbol. */
1813 coff_bfd_make_debug_symbol (bfd *abfd,
1814 void * ptr ATTRIBUTE_UNUSED,
1815 unsigned long sz ATTRIBUTE_UNUSED)
1817 bfd_size_type amt = sizeof (coff_symbol_type);
1818 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1820 if (new_symbol == NULL)
1822 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1823 (but shouldn't be a constant). */
1824 amt = sizeof (combined_entry_type) * 10;
1825 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1826 if (!new_symbol->native)
1828 new_symbol->symbol.section = bfd_abs_section_ptr;
1829 new_symbol->symbol.flags = BSF_DEBUGGING;
1830 new_symbol->lineno = NULL;
1831 new_symbol->done_lineno = FALSE;
1832 new_symbol->symbol.the_bfd = abfd;
1834 return & new_symbol->symbol;
1838 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1840 bfd_symbol_info (symbol, ret);
1842 if (coffsymbol (symbol)->native != NULL
1843 && coffsymbol (symbol)->native->fix_value)
1844 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1845 (bfd_hostptr_t) obj_raw_syments (abfd);
1848 /* Return the COFF syment for a symbol. */
1851 bfd_coff_get_syment (bfd *abfd,
1853 struct internal_syment *psyment)
1855 coff_symbol_type *csym;
1857 csym = coff_symbol_from (abfd, symbol);
1858 if (csym == NULL || csym->native == NULL)
1860 bfd_set_error (bfd_error_invalid_operation);
1864 *psyment = csym->native->u.syment;
1866 if (csym->native->fix_value)
1867 psyment->n_value = psyment->n_value -
1868 (bfd_hostptr_t) obj_raw_syments (abfd);
1870 /* FIXME: We should handle fix_line here. */
1875 /* Return the COFF auxent for a symbol. */
1878 bfd_coff_get_auxent (bfd *abfd,
1881 union internal_auxent *pauxent)
1883 coff_symbol_type *csym;
1884 combined_entry_type *ent;
1886 csym = coff_symbol_from (abfd, symbol);
1889 || csym->native == NULL
1890 || indx >= csym->native->u.syment.n_numaux)
1892 bfd_set_error (bfd_error_invalid_operation);
1896 ent = csym->native + indx + 1;
1898 *pauxent = ent->u.auxent;
1901 pauxent->x_sym.x_tagndx.l =
1902 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1903 - obj_raw_syments (abfd));
1906 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1907 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1908 - obj_raw_syments (abfd));
1910 if (ent->fix_scnlen)
1911 pauxent->x_csect.x_scnlen.l =
1912 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1913 - obj_raw_syments (abfd));
1918 /* Print out information about COFF symbol. */
1921 coff_print_symbol (bfd *abfd,
1924 bfd_print_symbol_type how)
1926 FILE * file = (FILE *) filep;
1930 case bfd_print_symbol_name:
1931 fprintf (file, "%s", symbol->name);
1934 case bfd_print_symbol_more:
1935 fprintf (file, "coff %s %s",
1936 coffsymbol (symbol)->native ? "n" : "g",
1937 coffsymbol (symbol)->lineno ? "l" : " ");
1940 case bfd_print_symbol_all:
1941 if (coffsymbol (symbol)->native)
1945 combined_entry_type *combined = coffsymbol (symbol)->native;
1946 combined_entry_type *root = obj_raw_syments (abfd);
1947 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1949 fprintf (file, "[%3ld]", (long) (combined - root));
1951 if (! combined->fix_value)
1952 val = (bfd_vma) combined->u.syment.n_value;
1954 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
1956 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1957 combined->u.syment.n_scnum,
1958 combined->u.syment.n_flags,
1959 combined->u.syment.n_type,
1960 combined->u.syment.n_sclass,
1961 combined->u.syment.n_numaux);
1962 bfd_fprintf_vma (abfd, file, val);
1963 fprintf (file, " %s", symbol->name);
1965 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1967 combined_entry_type *auxp = combined + aux + 1;
1971 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1973 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1975 fprintf (file, "\n");
1977 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1980 switch (combined->u.syment.n_sclass)
1983 fprintf (file, "File ");
1987 if (combined->u.syment.n_type == T_NULL)
1988 /* Probably a section symbol ? */
1990 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1991 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
1992 auxp->u.auxent.x_scn.x_nreloc,
1993 auxp->u.auxent.x_scn.x_nlinno);
1994 if (auxp->u.auxent.x_scn.x_checksum != 0
1995 || auxp->u.auxent.x_scn.x_associated != 0
1996 || auxp->u.auxent.x_scn.x_comdat != 0)
1997 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
1998 auxp->u.auxent.x_scn.x_checksum,
1999 auxp->u.auxent.x_scn.x_associated,
2000 auxp->u.auxent.x_scn.x_comdat);
2003 /* Otherwise fall through. */
2006 if (ISFCN (combined->u.syment.n_type))
2011 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2014 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2015 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2017 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2019 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2023 /* Otherwise fall through. */
2025 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2026 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2027 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2030 fprintf (file, " endndx %ld",
2032 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2040 fprintf (file, "\n%s :", l->u.sym->name);
2042 while (l->line_number)
2044 fprintf (file, "\n%4d : ", l->line_number);
2045 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2052 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2053 fprintf (file, " %-5s %s %s %s",
2054 symbol->section->name,
2055 coffsymbol (symbol)->native ? "n" : "g",
2056 coffsymbol (symbol)->lineno ? "l" : " ",
2062 /* Return whether a symbol name implies a local symbol. In COFF,
2063 local symbols generally start with ``.L''. Most targets use this
2064 function for the is_local_label_name entry point, but some may
2068 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2071 return name[0] == '.' && name[1] == 'L';
2074 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2075 section, calculate and return the name of the source file and the line
2076 nearest to the wanted location. */
2079 coff_find_nearest_line (bfd *abfd,
2083 const char **filename_ptr,
2084 const char **functionname_ptr,
2085 unsigned int *line_ptr)
2089 unsigned int line_base;
2090 coff_data_type *cof = coff_data (abfd);
2091 /* Run through the raw syments if available. */
2092 combined_entry_type *p;
2093 combined_entry_type *pend;
2095 struct coff_section_tdata *sec_data;
2098 /* Before looking through the symbol table, try to use a .stab
2099 section to find the information. */
2100 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2101 &found, filename_ptr,
2102 functionname_ptr, line_ptr,
2103 &coff_data(abfd)->line_info))
2109 /* Also try examining DWARF2 debugging information. */
2110 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2111 filename_ptr, functionname_ptr,
2113 &coff_data(abfd)->dwarf2_find_line_info))
2117 *functionname_ptr = 0;
2120 /* Don't try and find line numbers in a non coff file. */
2121 if (!bfd_family_coff (abfd))
2127 /* Find the first C_FILE symbol. */
2128 p = cof->raw_syments;
2132 pend = p + cof->raw_syment_count;
2135 if (p->u.syment.n_sclass == C_FILE)
2137 p += 1 + p->u.syment.n_numaux;
2145 /* Look through the C_FILE symbols to find the best one. */
2146 sec_vma = bfd_get_section_vma (abfd, section);
2147 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2148 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2151 combined_entry_type *p2;
2153 for (p2 = p + 1 + p->u.syment.n_numaux;
2155 p2 += 1 + p2->u.syment.n_numaux)
2157 if (p2->u.syment.n_scnum > 0
2159 == coff_section_from_bfd_index (abfd,
2160 p2->u.syment.n_scnum)))
2162 if (p2->u.syment.n_sclass == C_FILE)
2169 /* We use <= MAXDIFF here so that if we get a zero length
2170 file, we actually use the next file entry. */
2172 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2173 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2175 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2176 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2179 /* Avoid endless loops on erroneous files by ensuring that
2180 we always move forward in the file. */
2181 if (p >= cof->raw_syments + p->u.syment.n_value)
2184 p = cof->raw_syments + p->u.syment.n_value;
2185 if (p > pend || p->u.syment.n_sclass != C_FILE)
2190 /* Now wander though the raw linenumbers of the section. */
2191 /* If we have been called on this section before, and the offset we
2192 want is further down then we can prime the lookup loop. */
2193 sec_data = coff_section_data (abfd, section);
2194 if (sec_data != NULL
2196 && offset >= sec_data->offset)
2199 *functionname_ptr = sec_data->function;
2200 line_base = sec_data->line_base;
2208 if (section->lineno != NULL)
2210 bfd_vma last_value = 0;
2212 l = §ion->lineno[i];
2214 for (; i < section->lineno_count; i++)
2216 if (l->line_number == 0)
2218 /* Get the symbol this line number points at. */
2219 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2220 if (coff->symbol.value > offset)
2222 *functionname_ptr = coff->symbol.name;
2223 last_value = coff->symbol.value;
2226 combined_entry_type *s = coff->native;
2227 s = s + 1 + s->u.syment.n_numaux;
2229 /* In XCOFF a debugging symbol can follow the
2231 if (s->u.syment.n_scnum == N_DEBUG)
2232 s = s + 1 + s->u.syment.n_numaux;
2234 /* S should now point to the .bf of the function. */
2235 if (s->u.syment.n_numaux)
2237 /* The linenumber is stored in the auxent. */
2238 union internal_auxent *a = &((s + 1)->u.auxent);
2239 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2240 *line_ptr = line_base;
2246 if (l->u.offset > offset)
2248 *line_ptr = l->line_number + line_base - 1;
2253 /* If we fell off the end of the loop, then assume that this
2254 symbol has no line number info. Otherwise, symbols with no
2255 line number info get reported with the line number of the
2256 last line of the last symbol which does have line number
2257 info. We use 0x100 as a slop to account for cases where the
2258 last line has executable code. */
2259 if (i >= section->lineno_count
2261 && offset - last_value > 0x100)
2263 *functionname_ptr = NULL;
2268 /* Cache the results for the next call. */
2269 if (sec_data == NULL && section->owner == abfd)
2271 amt = sizeof (struct coff_section_tdata);
2272 section->used_by_bfd = bfd_zalloc (abfd, amt);
2273 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2275 if (sec_data != NULL)
2277 sec_data->offset = offset;
2278 sec_data->i = i - 1;
2279 sec_data->function = *functionname_ptr;
2280 sec_data->line_base = line_base;
2287 coff_find_inliner_info (bfd *abfd,
2288 const char **filename_ptr,
2289 const char **functionname_ptr,
2290 unsigned int *line_ptr)
2294 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2295 functionname_ptr, line_ptr,
2296 &coff_data(abfd)->dwarf2_find_line_info);
2301 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2305 if (!info->relocatable)
2306 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2308 size = bfd_coff_filhsz (abfd);
2310 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2314 /* Change the class of a coff symbol held by BFD. */
2317 bfd_coff_set_symbol_class (bfd * abfd,
2321 coff_symbol_type * csym;
2323 csym = coff_symbol_from (abfd, symbol);
2326 bfd_set_error (bfd_error_invalid_operation);
2329 else if (csym->native == NULL)
2331 /* This is an alien symbol which no native coff backend data.
2332 We cheat here by creating a fake native entry for it and
2333 then filling in the class. This code is based on that in
2334 coff_write_alien_symbol(). */
2336 combined_entry_type * native;
2337 bfd_size_type amt = sizeof (* native);
2339 native = bfd_zalloc (abfd, amt);
2343 native->u.syment.n_type = T_NULL;
2344 native->u.syment.n_sclass = class;
2346 if (bfd_is_und_section (symbol->section))
2348 native->u.syment.n_scnum = N_UNDEF;
2349 native->u.syment.n_value = symbol->value;
2351 else if (bfd_is_com_section (symbol->section))
2353 native->u.syment.n_scnum = N_UNDEF;
2354 native->u.syment.n_value = symbol->value;
2358 native->u.syment.n_scnum =
2359 symbol->section->output_section->target_index;
2360 native->u.syment.n_value = (symbol->value
2361 + symbol->section->output_offset);
2362 if (! obj_pe (abfd))
2363 native->u.syment.n_value += symbol->section->output_section->vma;
2365 /* Copy the any flags from the file header into the symbol.
2367 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2370 csym->native = native;
2373 csym->native->u.syment.n_sclass = class;
2378 struct coff_comdat_info *
2379 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2381 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2382 && coff_section_data (abfd, sec) != NULL)
2383 return coff_section_data (abfd, sec)->comdat;