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, 2006, 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 n_sclass = 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, n_sclass, (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 sym_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 sym_class = bfd_coff_classify_symbol (abfd,
1168 &c_symbol->native->u.syment);
1169 (void) bfd_set_error_handler (current_error_handler);
1171 n_sclass = &c_symbol->native->u.syment.n_sclass;
1173 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1174 we cannot retain the existing sclass from the original symbol.
1175 Weak symbols only have one valid sclass, so just set it always.
1176 If it is not local class and should be, set it C_STAT.
1177 If it is global and not classified as global, or if it is
1178 weak (which is also classified as global), set it C_EXT. */
1180 if (symbol->flags & BSF_WEAK)
1181 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1182 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1184 else if (symbol->flags & BSF_GLOBAL
1185 && (sym_class != COFF_SYMBOL_GLOBAL
1187 || *n_sclass == C_NT_WEAK
1189 || *n_sclass == C_WEAKEXT))
1190 c_symbol->native->u.syment.n_sclass = C_EXT;
1193 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1194 &string_size, &debug_string_section,
1195 &debug_string_size))
1200 obj_raw_syment_count (abfd) = written;
1202 /* Now write out strings. */
1203 if (string_size != 0)
1205 unsigned int size = string_size + STRING_SIZE_SIZE;
1206 bfd_byte buffer[STRING_SIZE_SIZE];
1208 #if STRING_SIZE_SIZE == 4
1209 H_PUT_32 (abfd, size, buffer);
1211 #error Change H_PUT_32
1213 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1217 /* Handle long section names. This code must handle section
1218 names just as they are handled in coff_write_object_contents. */
1219 if (bfd_coff_long_section_names (abfd))
1223 for (o = abfd->sections; o != NULL; o = o->next)
1227 len = strlen (o->name);
1230 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1237 for (p = abfd->outsymbols, i = 0;
1242 size_t name_length = strlen (q->name);
1243 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1246 /* Figure out whether the symbol name should go in the string
1247 table. Symbol names that are short enough are stored
1248 directly in the syment structure. File names permit a
1249 different, longer, length in the syment structure. On
1250 XCOFF, some symbol names are stored in the .debug section
1251 rather than in the string table. */
1253 if (c_symbol == NULL
1254 || c_symbol->native == NULL)
1255 /* This is not a COFF symbol, so it certainly is not a
1256 file name, nor does it go in the .debug section. */
1257 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1259 else if (bfd_coff_symname_in_debug (abfd,
1260 &c_symbol->native->u.syment))
1261 /* This symbol name is in the XCOFF .debug section.
1262 Don't write it into the string table. */
1263 maxlen = name_length;
1265 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1266 && c_symbol->native->u.syment.n_numaux > 0)
1268 if (bfd_coff_force_symnames_in_strings (abfd))
1270 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1273 maxlen = bfd_coff_filnmlen (abfd);
1276 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1278 if (name_length > maxlen)
1280 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1281 abfd) != name_length + 1)
1288 /* We would normally not write anything here, but we'll write
1289 out 4 so that any stupid coff reader which tries to read the
1290 string table even when there isn't one won't croak. */
1291 unsigned int size = STRING_SIZE_SIZE;
1292 bfd_byte buffer[STRING_SIZE_SIZE];
1294 #if STRING_SIZE_SIZE == 4
1295 H_PUT_32 (abfd, size, buffer);
1297 #error Change H_PUT_32
1299 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1300 != STRING_SIZE_SIZE)
1304 /* Make sure the .debug section was created to be the correct size.
1305 We should create it ourselves on the fly, but we don't because
1306 BFD won't let us write to any section until we know how large all
1307 the sections are. We could still do it by making another pass
1308 over the symbols. FIXME. */
1309 BFD_ASSERT (debug_string_size == 0
1310 || (debug_string_section != (asection *) NULL
1311 && (BFD_ALIGN (debug_string_size,
1312 1 << debug_string_section->alignment_power)
1313 == debug_string_section->size)));
1319 coff_write_linenumbers (bfd *abfd)
1322 bfd_size_type linesz;
1325 linesz = bfd_coff_linesz (abfd);
1326 buff = bfd_alloc (abfd, linesz);
1329 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1331 if (s->lineno_count)
1333 asymbol **q = abfd->outsymbols;
1334 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1336 /* Find all the linenumbers in this section. */
1340 if (p->section->output_section == s)
1343 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1344 (bfd_asymbol_bfd (p), p));
1347 /* Found a linenumber entry, output. */
1348 struct internal_lineno out;
1349 memset ((void *) & out, 0, sizeof (out));
1351 out.l_addr.l_symndx = l->u.offset;
1352 bfd_coff_swap_lineno_out (abfd, &out, buff);
1353 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1357 while (l->line_number)
1359 out.l_lnno = l->line_number;
1360 out.l_addr.l_symndx = l->u.offset;
1361 bfd_coff_swap_lineno_out (abfd, &out, buff);
1362 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1373 bfd_release (abfd, buff);
1378 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1380 return coffsymbol (symbol)->lineno;
1383 /* This function transforms the offsets into the symbol table into
1384 pointers to syments. */
1387 coff_pointerize_aux (bfd *abfd,
1388 combined_entry_type *table_base,
1389 combined_entry_type *symbol,
1390 unsigned int indaux,
1391 combined_entry_type *auxent)
1393 unsigned int type = symbol->u.syment.n_type;
1394 unsigned int n_sclass = symbol->u.syment.n_sclass;
1396 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1398 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1399 (abfd, table_base, symbol, indaux, auxent))
1403 /* Don't bother if this is a file or a section. */
1404 if (n_sclass == C_STAT && type == T_NULL)
1406 if (n_sclass == C_FILE)
1409 /* Otherwise patch up. */
1410 #define N_TMASK coff_data (abfd)->local_n_tmask
1411 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1413 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1414 || n_sclass == C_FCN)
1415 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1417 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1418 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1419 auxent->fix_end = 1;
1421 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1422 generate one, so we must be careful to ignore it. */
1423 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1425 auxent->u.auxent.x_sym.x_tagndx.p =
1426 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1427 auxent->fix_tag = 1;
1431 /* Allocate space for the ".debug" section, and read it.
1432 We did not read the debug section until now, because
1433 we didn't want to go to the trouble until someone needed it. */
1436 build_debug_section (bfd *abfd)
1438 char *debug_section;
1440 bfd_size_type sec_size;
1442 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1446 bfd_set_error (bfd_error_no_debug_section);
1450 sec_size = sect->size;
1451 debug_section = bfd_alloc (abfd, sec_size);
1452 if (debug_section == NULL)
1455 /* Seek to the beginning of the `.debug' section and read it.
1456 Save the current position first; it is needed by our caller.
1457 Then read debug section and reset the file pointer. */
1459 position = bfd_tell (abfd);
1460 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1461 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1462 || bfd_seek (abfd, position, SEEK_SET) != 0)
1464 return debug_section;
1467 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1468 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1469 be \0-terminated. */
1472 copy_name (bfd *abfd, char *name, size_t maxlen)
1477 for (len = 0; len < maxlen; ++len)
1478 if (name[len] == '\0')
1481 if ((newname = bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1484 strncpy (newname, name, len);
1485 newname[len] = '\0';
1489 /* Read in the external symbols. */
1492 _bfd_coff_get_external_symbols (bfd *abfd)
1494 bfd_size_type symesz;
1498 if (obj_coff_external_syms (abfd) != NULL)
1501 symesz = bfd_coff_symesz (abfd);
1503 size = obj_raw_syment_count (abfd) * symesz;
1507 syms = bfd_malloc (size);
1511 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1512 || bfd_bread (syms, size, abfd) != size)
1519 obj_coff_external_syms (abfd) = syms;
1524 /* Read in the external strings. The strings are not loaded until
1525 they are needed. This is because we have no simple way of
1526 detecting a missing string table in an archive. */
1529 _bfd_coff_read_string_table (bfd *abfd)
1531 char extstrsize[STRING_SIZE_SIZE];
1532 bfd_size_type strsize;
1536 if (obj_coff_strings (abfd) != NULL)
1537 return obj_coff_strings (abfd);
1539 if (obj_sym_filepos (abfd) == 0)
1541 bfd_set_error (bfd_error_no_symbols);
1545 pos = obj_sym_filepos (abfd);
1546 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1547 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1550 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1551 != sizeof extstrsize)
1553 if (bfd_get_error () != bfd_error_file_truncated)
1556 /* There is no string table. */
1557 strsize = STRING_SIZE_SIZE;
1561 #if STRING_SIZE_SIZE == 4
1562 strsize = H_GET_32 (abfd, extstrsize);
1564 #error Change H_GET_32
1568 if (strsize < STRING_SIZE_SIZE)
1570 (*_bfd_error_handler)
1571 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1572 bfd_set_error (bfd_error_bad_value);
1576 strings = bfd_malloc (strsize);
1577 if (strings == NULL)
1580 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1581 != strsize - STRING_SIZE_SIZE)
1587 obj_coff_strings (abfd) = strings;
1592 /* Free up the external symbols and strings read from a COFF file. */
1595 _bfd_coff_free_symbols (bfd *abfd)
1597 if (obj_coff_external_syms (abfd) != NULL
1598 && ! obj_coff_keep_syms (abfd))
1600 free (obj_coff_external_syms (abfd));
1601 obj_coff_external_syms (abfd) = NULL;
1603 if (obj_coff_strings (abfd) != NULL
1604 && ! obj_coff_keep_strings (abfd))
1606 free (obj_coff_strings (abfd));
1607 obj_coff_strings (abfd) = NULL;
1612 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1613 knit the symbol names into a normalized form. By normalized here I
1614 mean that all symbols have an n_offset pointer that points to a null-
1615 terminated string. */
1617 combined_entry_type *
1618 coff_get_normalized_symtab (bfd *abfd)
1620 combined_entry_type *internal;
1621 combined_entry_type *internal_ptr;
1622 combined_entry_type *symbol_ptr;
1623 combined_entry_type *internal_end;
1627 const char *string_table = NULL;
1628 char *debug_section = NULL;
1631 if (obj_raw_syments (abfd) != NULL)
1632 return obj_raw_syments (abfd);
1634 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1635 internal = bfd_zalloc (abfd, size);
1636 if (internal == NULL && size != 0)
1638 internal_end = internal + obj_raw_syment_count (abfd);
1640 if (! _bfd_coff_get_external_symbols (abfd))
1643 raw_src = (char *) obj_coff_external_syms (abfd);
1645 /* Mark the end of the symbols. */
1646 symesz = bfd_coff_symesz (abfd);
1647 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1649 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1650 probably possible. If one shows up, it will probably kill us. */
1652 /* Swap all the raw entries. */
1653 for (internal_ptr = internal;
1655 raw_src += symesz, internal_ptr++)
1659 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1660 (void *) & internal_ptr->u.syment);
1661 symbol_ptr = internal_ptr;
1664 i < symbol_ptr->u.syment.n_numaux;
1669 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1670 symbol_ptr->u.syment.n_type,
1671 symbol_ptr->u.syment.n_sclass,
1672 (int) i, symbol_ptr->u.syment.n_numaux,
1673 &(internal_ptr->u.auxent));
1674 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1679 /* Free the raw symbols, but not the strings (if we have them). */
1680 obj_coff_keep_strings (abfd) = TRUE;
1681 if (! _bfd_coff_free_symbols (abfd))
1684 for (internal_ptr = internal; internal_ptr < internal_end;
1687 if (internal_ptr->u.syment.n_sclass == C_FILE
1688 && internal_ptr->u.syment.n_numaux > 0)
1690 /* Make a file symbol point to the name in the auxent, since
1691 the text ".file" is redundant. */
1692 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1694 /* The filename is a long one, point into the string table. */
1695 if (string_table == NULL)
1697 string_table = _bfd_coff_read_string_table (abfd);
1698 if (string_table == NULL)
1702 internal_ptr->u.syment._n._n_n._n_offset =
1705 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1709 /* Ordinary short filename, put into memory anyway. The
1710 Microsoft PE tools sometimes store a filename in
1711 multiple AUX entries. */
1712 if (internal_ptr->u.syment.n_numaux > 1
1713 && coff_data (abfd)->pe)
1714 internal_ptr->u.syment._n._n_n._n_offset =
1717 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1718 internal_ptr->u.syment.n_numaux * symesz));
1720 internal_ptr->u.syment._n._n_n._n_offset =
1723 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1724 (size_t) bfd_coff_filnmlen (abfd)));
1729 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1731 /* This is a "short" name. Make it long. */
1735 /* Find the length of this string without walking into memory
1737 for (i = 0; i < 8; ++i)
1738 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1741 newstring = bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1742 if (newstring == NULL)
1744 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1745 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1746 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1748 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1749 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1750 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1752 /* Long name already. Point symbol at the string in the
1754 if (string_table == NULL)
1756 string_table = _bfd_coff_read_string_table (abfd);
1757 if (string_table == NULL)
1760 internal_ptr->u.syment._n._n_n._n_offset =
1763 + internal_ptr->u.syment._n._n_n._n_offset));
1767 /* Long name in debug section. Very similar. */
1768 if (debug_section == NULL)
1769 debug_section = build_debug_section (abfd);
1770 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1771 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1774 internal_ptr += internal_ptr->u.syment.n_numaux;
1777 obj_raw_syments (abfd) = internal;
1778 BFD_ASSERT (obj_raw_syment_count (abfd)
1779 == (unsigned int) (internal_ptr - internal));
1785 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1787 if (bfd_get_format (abfd) != bfd_object)
1789 bfd_set_error (bfd_error_invalid_operation);
1792 return (asect->reloc_count + 1) * sizeof (arelent *);
1796 coff_make_empty_symbol (bfd *abfd)
1798 bfd_size_type amt = sizeof (coff_symbol_type);
1799 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1801 if (new_symbol == NULL)
1803 new_symbol->symbol.section = 0;
1804 new_symbol->native = 0;
1805 new_symbol->lineno = NULL;
1806 new_symbol->done_lineno = FALSE;
1807 new_symbol->symbol.the_bfd = abfd;
1809 return & new_symbol->symbol;
1812 /* Make a debugging symbol. */
1815 coff_bfd_make_debug_symbol (bfd *abfd,
1816 void * ptr ATTRIBUTE_UNUSED,
1817 unsigned long sz ATTRIBUTE_UNUSED)
1819 bfd_size_type amt = sizeof (coff_symbol_type);
1820 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1822 if (new_symbol == NULL)
1824 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1825 (but shouldn't be a constant). */
1826 amt = sizeof (combined_entry_type) * 10;
1827 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1828 if (!new_symbol->native)
1830 new_symbol->symbol.section = bfd_abs_section_ptr;
1831 new_symbol->symbol.flags = BSF_DEBUGGING;
1832 new_symbol->lineno = NULL;
1833 new_symbol->done_lineno = FALSE;
1834 new_symbol->symbol.the_bfd = abfd;
1836 return & new_symbol->symbol;
1840 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1842 bfd_symbol_info (symbol, ret);
1844 if (coffsymbol (symbol)->native != NULL
1845 && coffsymbol (symbol)->native->fix_value)
1846 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1847 (bfd_hostptr_t) obj_raw_syments (abfd);
1850 /* Return the COFF syment for a symbol. */
1853 bfd_coff_get_syment (bfd *abfd,
1855 struct internal_syment *psyment)
1857 coff_symbol_type *csym;
1859 csym = coff_symbol_from (abfd, symbol);
1860 if (csym == NULL || csym->native == NULL)
1862 bfd_set_error (bfd_error_invalid_operation);
1866 *psyment = csym->native->u.syment;
1868 if (csym->native->fix_value)
1869 psyment->n_value = psyment->n_value -
1870 (bfd_hostptr_t) obj_raw_syments (abfd);
1872 /* FIXME: We should handle fix_line here. */
1877 /* Return the COFF auxent for a symbol. */
1880 bfd_coff_get_auxent (bfd *abfd,
1883 union internal_auxent *pauxent)
1885 coff_symbol_type *csym;
1886 combined_entry_type *ent;
1888 csym = coff_symbol_from (abfd, symbol);
1891 || csym->native == NULL
1892 || indx >= csym->native->u.syment.n_numaux)
1894 bfd_set_error (bfd_error_invalid_operation);
1898 ent = csym->native + indx + 1;
1900 *pauxent = ent->u.auxent;
1903 pauxent->x_sym.x_tagndx.l =
1904 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1905 - obj_raw_syments (abfd));
1908 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1909 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1910 - obj_raw_syments (abfd));
1912 if (ent->fix_scnlen)
1913 pauxent->x_csect.x_scnlen.l =
1914 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1915 - obj_raw_syments (abfd));
1920 /* Print out information about COFF symbol. */
1923 coff_print_symbol (bfd *abfd,
1926 bfd_print_symbol_type how)
1928 FILE * file = (FILE *) filep;
1932 case bfd_print_symbol_name:
1933 fprintf (file, "%s", symbol->name);
1936 case bfd_print_symbol_more:
1937 fprintf (file, "coff %s %s",
1938 coffsymbol (symbol)->native ? "n" : "g",
1939 coffsymbol (symbol)->lineno ? "l" : " ");
1942 case bfd_print_symbol_all:
1943 if (coffsymbol (symbol)->native)
1947 combined_entry_type *combined = coffsymbol (symbol)->native;
1948 combined_entry_type *root = obj_raw_syments (abfd);
1949 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1951 fprintf (file, "[%3ld]", (long) (combined - root));
1953 if (! combined->fix_value)
1954 val = (bfd_vma) combined->u.syment.n_value;
1956 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
1958 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1959 combined->u.syment.n_scnum,
1960 combined->u.syment.n_flags,
1961 combined->u.syment.n_type,
1962 combined->u.syment.n_sclass,
1963 combined->u.syment.n_numaux);
1964 bfd_fprintf_vma (abfd, file, val);
1965 fprintf (file, " %s", symbol->name);
1967 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1969 combined_entry_type *auxp = combined + aux + 1;
1973 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1975 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1977 fprintf (file, "\n");
1979 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1982 switch (combined->u.syment.n_sclass)
1985 fprintf (file, "File ");
1989 if (combined->u.syment.n_type == T_NULL)
1990 /* Probably a section symbol ? */
1992 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1993 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
1994 auxp->u.auxent.x_scn.x_nreloc,
1995 auxp->u.auxent.x_scn.x_nlinno);
1996 if (auxp->u.auxent.x_scn.x_checksum != 0
1997 || auxp->u.auxent.x_scn.x_associated != 0
1998 || auxp->u.auxent.x_scn.x_comdat != 0)
1999 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2000 auxp->u.auxent.x_scn.x_checksum,
2001 auxp->u.auxent.x_scn.x_associated,
2002 auxp->u.auxent.x_scn.x_comdat);
2005 /* Otherwise fall through. */
2008 if (ISFCN (combined->u.syment.n_type))
2013 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2016 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2017 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2019 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2021 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2025 /* Otherwise fall through. */
2027 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2028 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2029 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2032 fprintf (file, " endndx %ld",
2034 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2042 fprintf (file, "\n%s :", l->u.sym->name);
2044 while (l->line_number)
2046 fprintf (file, "\n%4d : ", l->line_number);
2047 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2054 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2055 fprintf (file, " %-5s %s %s %s",
2056 symbol->section->name,
2057 coffsymbol (symbol)->native ? "n" : "g",
2058 coffsymbol (symbol)->lineno ? "l" : " ",
2064 /* Return whether a symbol name implies a local symbol. In COFF,
2065 local symbols generally start with ``.L''. Most targets use this
2066 function for the is_local_label_name entry point, but some may
2070 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2073 return name[0] == '.' && name[1] == 'L';
2076 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2077 section, calculate and return the name of the source file and the line
2078 nearest to the wanted location. */
2081 coff_find_nearest_line (bfd *abfd,
2085 const char **filename_ptr,
2086 const char **functionname_ptr,
2087 unsigned int *line_ptr)
2091 unsigned int line_base;
2092 coff_data_type *cof = coff_data (abfd);
2093 /* Run through the raw syments if available. */
2094 combined_entry_type *p;
2095 combined_entry_type *pend;
2097 struct coff_section_tdata *sec_data;
2100 /* Before looking through the symbol table, try to use a .stab
2101 section to find the information. */
2102 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2103 &found, filename_ptr,
2104 functionname_ptr, line_ptr,
2105 &coff_data(abfd)->line_info))
2111 /* Also try examining DWARF2 debugging information. */
2112 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2113 filename_ptr, functionname_ptr,
2115 &coff_data(abfd)->dwarf2_find_line_info))
2119 *functionname_ptr = 0;
2122 /* Don't try and find line numbers in a non coff file. */
2123 if (!bfd_family_coff (abfd))
2129 /* Find the first C_FILE symbol. */
2130 p = cof->raw_syments;
2134 pend = p + cof->raw_syment_count;
2137 if (p->u.syment.n_sclass == C_FILE)
2139 p += 1 + p->u.syment.n_numaux;
2147 /* Look through the C_FILE symbols to find the best one. */
2148 sec_vma = bfd_get_section_vma (abfd, section);
2149 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2150 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2153 combined_entry_type *p2;
2155 for (p2 = p + 1 + p->u.syment.n_numaux;
2157 p2 += 1 + p2->u.syment.n_numaux)
2159 if (p2->u.syment.n_scnum > 0
2161 == coff_section_from_bfd_index (abfd,
2162 p2->u.syment.n_scnum)))
2164 if (p2->u.syment.n_sclass == C_FILE)
2171 /* We use <= MAXDIFF here so that if we get a zero length
2172 file, we actually use the next file entry. */
2174 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2175 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2177 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2178 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2181 /* Avoid endless loops on erroneous files by ensuring that
2182 we always move forward in the file. */
2183 if (p >= cof->raw_syments + p->u.syment.n_value)
2186 p = cof->raw_syments + p->u.syment.n_value;
2187 if (p > pend || p->u.syment.n_sclass != C_FILE)
2192 /* Now wander though the raw linenumbers of the section. */
2193 /* If we have been called on this section before, and the offset we
2194 want is further down then we can prime the lookup loop. */
2195 sec_data = coff_section_data (abfd, section);
2196 if (sec_data != NULL
2198 && offset >= sec_data->offset)
2201 *functionname_ptr = sec_data->function;
2202 line_base = sec_data->line_base;
2210 if (section->lineno != NULL)
2212 bfd_vma last_value = 0;
2214 l = §ion->lineno[i];
2216 for (; i < section->lineno_count; i++)
2218 if (l->line_number == 0)
2220 /* Get the symbol this line number points at. */
2221 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2222 if (coff->symbol.value > offset)
2224 *functionname_ptr = coff->symbol.name;
2225 last_value = coff->symbol.value;
2228 combined_entry_type *s = coff->native;
2229 s = s + 1 + s->u.syment.n_numaux;
2231 /* In XCOFF a debugging symbol can follow the
2233 if (s->u.syment.n_scnum == N_DEBUG)
2234 s = s + 1 + s->u.syment.n_numaux;
2236 /* S should now point to the .bf of the function. */
2237 if (s->u.syment.n_numaux)
2239 /* The linenumber is stored in the auxent. */
2240 union internal_auxent *a = &((s + 1)->u.auxent);
2241 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2242 *line_ptr = line_base;
2248 if (l->u.offset > offset)
2250 *line_ptr = l->line_number + line_base - 1;
2255 /* If we fell off the end of the loop, then assume that this
2256 symbol has no line number info. Otherwise, symbols with no
2257 line number info get reported with the line number of the
2258 last line of the last symbol which does have line number
2259 info. We use 0x100 as a slop to account for cases where the
2260 last line has executable code. */
2261 if (i >= section->lineno_count
2263 && offset - last_value > 0x100)
2265 *functionname_ptr = NULL;
2270 /* Cache the results for the next call. */
2271 if (sec_data == NULL && section->owner == abfd)
2273 amt = sizeof (struct coff_section_tdata);
2274 section->used_by_bfd = bfd_zalloc (abfd, amt);
2275 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2277 if (sec_data != NULL)
2279 sec_data->offset = offset;
2280 sec_data->i = i - 1;
2281 sec_data->function = *functionname_ptr;
2282 sec_data->line_base = line_base;
2289 coff_find_inliner_info (bfd *abfd,
2290 const char **filename_ptr,
2291 const char **functionname_ptr,
2292 unsigned int *line_ptr)
2296 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2297 functionname_ptr, line_ptr,
2298 &coff_data(abfd)->dwarf2_find_line_info);
2303 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2307 if (!info->relocatable)
2308 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2310 size = bfd_coff_filhsz (abfd);
2312 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2316 /* Change the class of a coff symbol held by BFD. */
2319 bfd_coff_set_symbol_class (bfd * abfd,
2321 unsigned int symbol_class)
2323 coff_symbol_type * csym;
2325 csym = coff_symbol_from (abfd, symbol);
2328 bfd_set_error (bfd_error_invalid_operation);
2331 else if (csym->native == NULL)
2333 /* This is an alien symbol which no native coff backend data.
2334 We cheat here by creating a fake native entry for it and
2335 then filling in the class. This code is based on that in
2336 coff_write_alien_symbol(). */
2338 combined_entry_type * native;
2339 bfd_size_type amt = sizeof (* native);
2341 native = bfd_zalloc (abfd, amt);
2345 native->u.syment.n_type = T_NULL;
2346 native->u.syment.n_sclass = symbol_class;
2348 if (bfd_is_und_section (symbol->section))
2350 native->u.syment.n_scnum = N_UNDEF;
2351 native->u.syment.n_value = symbol->value;
2353 else if (bfd_is_com_section (symbol->section))
2355 native->u.syment.n_scnum = N_UNDEF;
2356 native->u.syment.n_value = symbol->value;
2360 native->u.syment.n_scnum =
2361 symbol->section->output_section->target_index;
2362 native->u.syment.n_value = (symbol->value
2363 + symbol->section->output_offset);
2364 if (! obj_pe (abfd))
2365 native->u.syment.n_value += symbol->section->output_section->vma;
2367 /* Copy the any flags from the file header into the symbol.
2369 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2372 csym->native = native;
2375 csym->native->u.syment.n_sclass = symbol_class;
2380 struct coff_comdat_info *
2381 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2383 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2384 && coff_section_data (abfd, sec) != NULL)
2385 return coff_section_data (abfd, sec)->comdat;