1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
25 /* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
42 #include "coff/internal.h"
45 static void coff_fix_symbol_name
46 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
47 asection **, bfd_size_type *));
48 static boolean coff_write_symbol
49 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
50 bfd_size_type *, asection **, bfd_size_type *));
51 static boolean coff_write_alien_symbol
52 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
53 asection **, bfd_size_type *));
54 static boolean coff_write_native_symbol
55 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
56 asection **, bfd_size_type *));
57 static void coff_pointerize_aux
58 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
59 unsigned int, combined_entry_type *));
60 static boolean make_a_section_from_file
61 PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
62 static const bfd_target *coff_real_object_p
63 PARAMS ((bfd *, unsigned, struct internal_filehdr *,
64 struct internal_aouthdr *));
65 static void fixup_symbol_value
66 PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
67 static char *build_debug_section
69 static char *copy_name
70 PARAMS ((bfd *, char *, int));
72 #define STRING_SIZE_SIZE (4)
74 /* Take a section header read from a coff file (in HOST byte order),
75 and make a BFD "section" out of it. This is used by ECOFF. */
77 make_a_section_from_file (abfd, hdr, target_index)
79 struct internal_scnhdr *hdr;
80 unsigned int target_index;
82 asection *return_section;
87 /* Handle long section names as in PE. */
88 if (bfd_coff_long_section_names (abfd)
89 && hdr->s_name[0] == '/')
96 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
97 buf[SCNNMLEN - 1] = '\0';
98 strindex = strtol (buf, &p, 10);
99 if (*p == '\0' && strindex >= 0)
101 strings = _bfd_coff_read_string_table (abfd);
104 /* FIXME: For extra safety, we should make sure that
105 strindex does not run us past the end, but right now we
106 don't know the length of the string table. */
108 name = bfd_alloc (abfd, strlen (strings) + 1);
111 strcpy (name, strings);
117 /* Assorted wastage to null-terminate the name, thanks AT&T! */
118 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
121 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
122 name[sizeof (hdr->s_name)] = 0;
125 return_section = bfd_make_section_anyway (abfd, name);
126 if (return_section == NULL)
129 return_section->vma = hdr->s_vaddr;
130 return_section->lma = hdr->s_paddr;
131 return_section->_raw_size = hdr->s_size;
132 return_section->filepos = hdr->s_scnptr;
133 return_section->rel_filepos = hdr->s_relptr;
134 return_section->reloc_count = hdr->s_nreloc;
136 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
138 return_section->line_filepos = hdr->s_lnnoptr;
140 return_section->lineno_count = hdr->s_nlnno;
141 return_section->userdata = NULL;
142 return_section->next = (asection *) NULL;
143 return_section->target_index = target_index;
144 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name,
147 /* At least on i386-coff, the line number count for a shared library
148 section must be ignored. */
149 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
150 return_section->lineno_count = 0;
152 if (hdr->s_nreloc != 0)
153 return_section->flags |= SEC_RELOC;
154 /* FIXME: should this check 'hdr->s_size > 0' */
155 if (hdr->s_scnptr != 0)
156 return_section->flags |= SEC_HAS_CONTENTS;
160 /* Read in a COFF object and make it into a BFD. This is used by
163 static const bfd_target *
164 coff_real_object_p (abfd, nscns, internal_f, internal_a)
167 struct internal_filehdr *internal_f;
168 struct internal_aouthdr *internal_a;
170 flagword oflags = abfd->flags;
171 bfd_vma ostart = bfd_get_start_address (abfd);
173 size_t readsize; /* length of file_info */
175 char *external_sections;
177 if (!(internal_f->f_flags & F_RELFLG))
178 abfd->flags |= HAS_RELOC;
179 if ((internal_f->f_flags & F_EXEC))
180 abfd->flags |= EXEC_P;
181 if (!(internal_f->f_flags & F_LNNO))
182 abfd->flags |= HAS_LINENO;
183 if (!(internal_f->f_flags & F_LSYMS))
184 abfd->flags |= HAS_LOCALS;
186 /* FIXME: How can we set D_PAGED correctly? */
187 if ((internal_f->f_flags & F_EXEC) != 0)
188 abfd->flags |= D_PAGED;
190 bfd_get_symcount (abfd) = internal_f->f_nsyms;
191 if (internal_f->f_nsyms)
192 abfd->flags |= HAS_SYMS;
194 if (internal_a != (struct internal_aouthdr *) NULL)
195 bfd_get_start_address (abfd) = internal_a->entry;
197 bfd_get_start_address (abfd) = 0;
199 /* Set up the tdata area. ECOFF uses its own routine, and overrides
201 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
205 scnhsz = bfd_coff_scnhsz (abfd);
206 readsize = nscns * scnhsz;
207 external_sections = (char *) bfd_alloc (abfd, readsize);
208 if (!external_sections)
211 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
214 /* Set the arch/mach *before* swapping in sections; section header swapping
215 may depend on arch/mach info. */
216 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
219 /* Now copy data as required; construct all asections etc */
223 for (i = 0; i < nscns; i++)
225 struct internal_scnhdr tmp;
226 bfd_coff_swap_scnhdr_in (abfd,
227 (PTR) (external_sections + i * scnhsz),
229 if (! make_a_section_from_file (abfd, &tmp, i + 1))
234 /* make_abs_section (abfd); */
239 bfd_release (abfd, tdata);
240 abfd->flags = oflags;
241 bfd_get_start_address (abfd) = ostart;
242 return (const bfd_target *) NULL;
245 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
246 not a COFF file. This is also used by ECOFF. */
256 struct internal_filehdr internal_f;
257 struct internal_aouthdr internal_a;
259 /* figure out how much to read */
260 filhsz = bfd_coff_filhsz (abfd);
261 aoutsz = bfd_coff_aoutsz (abfd);
263 filehdr = bfd_alloc (abfd, filhsz);
266 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_wrong_format);
272 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
273 bfd_release (abfd, filehdr);
275 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
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_read (opthdr, 1, internal_f.f_opthdr, abfd)
290 != internal_f.f_opthdr)
294 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
297 return coff_real_object_p (abfd, nscns, &internal_f,
298 (internal_f.f_opthdr != 0
300 : (struct internal_aouthdr *) NULL));
303 /* Get the BFD section from a COFF symbol section number. */
306 coff_section_from_bfd_index (abfd, index)
310 struct sec *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 (abfd)
337 if (!bfd_coff_slurp_symbol_table (abfd))
340 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
343 /* Canonicalize a COFF symbol table. */
346 coff_get_symtab (abfd, alocation)
350 unsigned int counter;
351 coff_symbol_type *symbase;
352 coff_symbol_type **location = (coff_symbol_type **) alocation;
354 if (!bfd_coff_slurp_symbol_table (abfd))
357 symbase = obj_symbols (abfd);
358 counter = bfd_get_symcount (abfd);
359 while (counter-- > 0)
360 *location++ = symbase++;
364 return bfd_get_symcount (abfd);
367 /* Get the name of a symbol. The caller must pass in a buffer of size
371 _bfd_coff_internal_syment_name (abfd, sym, buf)
373 const struct internal_syment *sym;
376 /* FIXME: It's not clear this will work correctly if sizeof
378 if (sym->_n._n_n._n_zeroes != 0
379 || sym->_n._n_n._n_offset == 0)
381 memcpy (buf, sym->_n._n_name, SYMNMLEN);
382 buf[SYMNMLEN] = '\0';
389 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
390 strings = obj_coff_strings (abfd);
393 strings = _bfd_coff_read_string_table (abfd);
397 return strings + sym->_n._n_n._n_offset;
401 /* Read in and swap the relocs. This returns a buffer holding the
402 relocs for section SEC in file ABFD. If CACHE is true and
403 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
404 the function is called again. If EXTERNAL_RELOCS is not NULL, it
405 is a buffer large enough to hold the unswapped relocs. If
406 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
407 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
408 value must be INTERNAL_RELOCS. The function returns NULL on error. */
410 struct internal_reloc *
411 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
412 require_internal, internal_relocs)
416 bfd_byte *external_relocs;
417 boolean require_internal;
418 struct internal_reloc *internal_relocs;
421 bfd_byte *free_external = NULL;
422 struct internal_reloc *free_internal = NULL;
425 struct internal_reloc *irel;
427 if (coff_section_data (abfd, sec) != NULL
428 && coff_section_data (abfd, sec)->relocs != NULL)
430 if (! require_internal)
431 return coff_section_data (abfd, sec)->relocs;
432 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
433 sec->reloc_count * sizeof (struct internal_reloc));
434 return internal_relocs;
437 relsz = bfd_coff_relsz (abfd);
439 if (external_relocs == NULL)
441 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
442 if (free_external == NULL && sec->reloc_count > 0)
444 external_relocs = free_external;
447 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
448 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
449 != relsz * sec->reloc_count))
452 if (internal_relocs == NULL)
454 free_internal = ((struct internal_reloc *)
455 bfd_malloc (sec->reloc_count
456 * sizeof (struct internal_reloc)));
457 if (free_internal == NULL && sec->reloc_count > 0)
459 internal_relocs = free_internal;
462 /* Swap in the relocs. */
463 erel = external_relocs;
464 erel_end = erel + relsz * sec->reloc_count;
465 irel = internal_relocs;
466 for (; erel < erel_end; erel += relsz, irel++)
467 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
469 if (free_external != NULL)
471 free (free_external);
472 free_external = NULL;
475 if (cache && free_internal != NULL)
477 if (coff_section_data (abfd, sec) == NULL)
480 (PTR) bfd_zalloc (abfd,
481 sizeof (struct coff_section_tdata));
482 if (sec->used_by_bfd == NULL)
484 coff_section_data (abfd, sec)->contents = NULL;
486 coff_section_data (abfd, sec)->relocs = free_internal;
489 return internal_relocs;
492 if (free_external != NULL)
493 free (free_external);
494 if (free_internal != NULL)
495 free (free_internal);
499 /* Set lineno_count for the output sections of a COFF file. */
502 coff_count_linenumbers (abfd)
505 unsigned int limit = bfd_get_symcount (abfd);
513 /* This may be from the backend linker, in which case the
514 lineno_count in the sections is correct. */
515 for (s = abfd->sections; s != NULL; s = s->next)
516 total += s->lineno_count;
520 for (s = abfd->sections; s != NULL; s = s->next)
521 BFD_ASSERT (s->lineno_count == 0);
523 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
525 asymbol *q_maybe = *p;
527 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
529 coff_symbol_type *q = coffsymbol (q_maybe);
531 /* The AIX 4.1 compiler can sometimes generate line numbers
532 attached to debugging symbols. We try to simply ignore
534 if (q->lineno != NULL
535 && q->symbol.section->owner != NULL)
537 /* This symbol has line numbers. Increment the owning
538 section's linenumber count. */
539 alent *l = q->lineno;
541 ++q->symbol.section->output_section->lineno_count;
544 while (l->line_number != 0)
547 ++q->symbol.section->output_section->lineno_count;
557 /* Takes a bfd and a symbol, returns a pointer to the coff specific
558 area of the symbol if there is one. */
562 coff_symbol_from (ignore_abfd, symbol)
563 bfd *ignore_abfd ATTRIBUTE_UNUSED;
566 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
567 return (coff_symbol_type *) NULL;
569 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
570 return (coff_symbol_type *) NULL;
572 return (coff_symbol_type *) symbol;
576 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
578 coff_symbol_type *coff_symbol_ptr;
579 struct internal_syment *syment;
582 /* Normalize the symbol flags */
583 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
585 /* a common symbol is undefined with a value */
586 syment->n_scnum = N_UNDEF;
587 syment->n_value = coff_symbol_ptr->symbol.value;
589 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
590 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
592 syment->n_value = coff_symbol_ptr->symbol.value;
594 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
596 syment->n_scnum = N_UNDEF;
599 /* FIXME: Do we need to handle the absolute section here? */
602 if (coff_symbol_ptr->symbol.section)
605 coff_symbol_ptr->symbol.section->output_section->target_index;
607 syment->n_value = (coff_symbol_ptr->symbol.value
608 + coff_symbol_ptr->symbol.section->output_offset);
611 syment->n_value += (syment->n_sclass == C_STATLAB)
612 ? coff_symbol_ptr->symbol.section->output_section->lma
613 : coff_symbol_ptr->symbol.section->output_section->vma;
619 /* This can happen, but I don't know why yet (steve@cygnus.com) */
620 syment->n_scnum = N_ABS;
621 syment->n_value = coff_symbol_ptr->symbol.value;
626 /* Run through all the symbols in the symbol table and work out what
627 their indexes into the symbol table will be when output.
629 Coff requires that each C_FILE symbol points to the next one in the
630 chain, and that the last one points to the first external symbol. We
634 coff_renumber_symbols (bfd_ptr, first_undef)
638 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
639 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
640 unsigned int native_index = 0;
641 struct internal_syment *last_file = (struct internal_syment *) NULL;
642 unsigned int symbol_index;
644 /* COFF demands that undefined symbols come after all other symbols.
645 Since we don't need to impose this extra knowledge on all our
646 client programs, deal with that here. Sort the symbol table;
647 just move the undefined symbols to the end, leaving the rest
648 alone. The O'Reilly book says that defined global symbols come
649 at the end before the undefined symbols, so we do that here as
651 /* @@ Do we have some condition we could test for, so we don't always
652 have to do this? I don't think relocatability is quite right, but
653 I'm not certain. [raeburn:19920508.1711EST] */
658 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
659 sizeof (asymbol *) * (symbol_count + 1));
662 bfd_ptr->outsymbols = newsyms;
663 for (i = 0; i < symbol_count; i++)
664 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
665 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
666 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
667 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
668 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
670 *newsyms++ = symbol_ptr_ptr[i];
672 for (i = 0; i < symbol_count; i++)
673 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
674 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
675 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
676 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
677 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
679 *newsyms++ = symbol_ptr_ptr[i];
681 *first_undef = newsyms - bfd_ptr->outsymbols;
683 for (i = 0; i < symbol_count; i++)
684 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
685 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
686 *newsyms++ = symbol_ptr_ptr[i];
687 *newsyms = (asymbol *) NULL;
688 symbol_ptr_ptr = bfd_ptr->outsymbols;
691 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
693 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
694 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
695 if (coff_symbol_ptr && coff_symbol_ptr->native)
697 combined_entry_type *s = coff_symbol_ptr->native;
700 if (s->u.syment.n_sclass == C_FILE)
702 if (last_file != (struct internal_syment *) NULL)
703 last_file->n_value = native_index;
704 last_file = &(s->u.syment);
709 /* Modify the symbol values according to their section and
712 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
714 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
715 s[i].offset = native_index++;
722 obj_conv_table_size (bfd_ptr) = native_index;
727 /* Run thorough the symbol table again, and fix it so that all
728 pointers to entries are changed to the entries' index in the output
732 coff_mangle_symbols (bfd_ptr)
735 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
736 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
737 unsigned int symbol_index;
739 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
741 coff_symbol_type *coff_symbol_ptr =
742 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
744 if (coff_symbol_ptr && coff_symbol_ptr->native)
747 combined_entry_type *s = coff_symbol_ptr->native;
751 /* FIXME: We should use a union here. */
752 s->u.syment.n_value =
753 ((combined_entry_type *) s->u.syment.n_value)->offset;
758 /* The value is the offset into the line number entries
759 for the symbol's section. On output, the symbol's
760 section should be N_DEBUG. */
761 s->u.syment.n_value =
762 (coff_symbol_ptr->symbol.section->output_section->line_filepos
763 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
764 coff_symbol_ptr->symbol.section =
765 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
766 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
768 for (i = 0; i < s->u.syment.n_numaux; i++)
770 combined_entry_type *a = s + i + 1;
773 a->u.auxent.x_sym.x_tagndx.l =
774 a->u.auxent.x_sym.x_tagndx.p->offset;
779 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
780 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
785 a->u.auxent.x_csect.x_scnlen.l =
786 a->u.auxent.x_csect.x_scnlen.p->offset;
795 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
796 debug_string_section_p, debug_string_size_p)
799 combined_entry_type *native;
800 bfd_size_type *string_size_p;
801 asection **debug_string_section_p;
802 bfd_size_type *debug_string_size_p;
804 unsigned int name_length;
805 union internal_auxent *auxent;
806 char *name = (char *) (symbol->name);
808 if (name == (char *) NULL)
810 /* coff symbols always have names, so we'll make one up */
811 symbol->name = "strange";
812 name = (char *) symbol->name;
814 name_length = strlen (name);
816 if (native->u.syment.n_sclass == C_FILE
817 && native->u.syment.n_numaux > 0)
819 unsigned int filnmlen;
821 if (bfd_coff_force_symnames_in_strings (abfd))
823 native->u.syment._n._n_n._n_offset =
824 (*string_size_p + STRING_SIZE_SIZE);
825 native->u.syment._n._n_n._n_zeroes = 0;
826 *string_size_p += 6; /* strlen(".file") + 1 */
829 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
831 auxent = &(native + 1)->u.auxent;
833 filnmlen = bfd_coff_filnmlen (abfd);
835 if (bfd_coff_long_filenames (abfd))
837 if (name_length <= filnmlen)
839 strncpy (auxent->x_file.x_fname, name, filnmlen);
843 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
844 auxent->x_file.x_n.x_zeroes = 0;
845 *string_size_p += name_length + 1;
850 strncpy (auxent->x_file.x_fname, name, filnmlen);
851 if (name_length > filnmlen)
852 name[filnmlen] = '\0';
857 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
859 /* This name will fit into the symbol neatly */
860 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
862 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
864 native->u.syment._n._n_n._n_offset = (*string_size_p
866 native->u.syment._n._n_n._n_zeroes = 0;
867 *string_size_p += name_length + 1;
873 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
875 /* This name should be written into the .debug section. For
876 some reason each name is preceded by a two byte length
877 and also followed by a null byte. FIXME: We assume that
878 the .debug section has already been created, and that it
880 if (*debug_string_section_p == (asection *) NULL)
881 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
882 filepos = bfd_tell (abfd);
884 bfd_put_32 (abfd, name_length + 1, buf);
886 bfd_put_16 (abfd, name_length + 1, buf);
888 if (!bfd_set_section_contents (abfd,
889 *debug_string_section_p,
891 (file_ptr) *debug_string_size_p,
892 (bfd_size_type) prefix_len)
893 || !bfd_set_section_contents (abfd,
894 *debug_string_section_p,
896 ((file_ptr) *debug_string_size_p
898 (bfd_size_type) name_length + 1))
900 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
902 native->u.syment._n._n_n._n_offset =
903 *debug_string_size_p + prefix_len;
904 native->u.syment._n._n_n._n_zeroes = 0;
905 *debug_string_size_p += name_length + 1 + prefix_len;
910 /* We need to keep track of the symbol index so that when we write out
911 the relocs we can get the index for a symbol. This method is a
914 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
916 /* Write a symbol out to a COFF file. */
919 coff_write_symbol (abfd, symbol, native, written, string_size_p,
920 debug_string_section_p, debug_string_size_p)
923 combined_entry_type *native;
924 unsigned int *written;
925 bfd_size_type *string_size_p;
926 asection **debug_string_section_p;
927 bfd_size_type *debug_string_size_p;
929 unsigned int numaux = native->u.syment.n_numaux;
930 int type = native->u.syment.n_type;
931 int class = native->u.syment.n_sclass;
933 bfd_size_type symesz;
935 if (native->u.syment.n_sclass == C_FILE)
936 symbol->flags |= BSF_DEBUGGING;
938 if (symbol->flags & BSF_DEBUGGING
939 && bfd_is_abs_section (symbol->section))
941 native->u.syment.n_scnum = N_DEBUG;
943 else if (bfd_is_abs_section (symbol->section))
945 native->u.syment.n_scnum = N_ABS;
947 else if (bfd_is_und_section (symbol->section))
949 native->u.syment.n_scnum = N_UNDEF;
953 native->u.syment.n_scnum =
954 symbol->section->output_section->target_index;
957 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
958 debug_string_section_p, debug_string_size_p);
960 symesz = bfd_coff_symesz (abfd);
961 buf = bfd_alloc (abfd, symesz);
964 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
965 if (bfd_write (buf, 1, symesz, abfd) != symesz)
967 bfd_release (abfd, buf);
969 if (native->u.syment.n_numaux > 0)
971 bfd_size_type auxesz;
974 auxesz = bfd_coff_auxesz (abfd);
975 buf = bfd_alloc (abfd, auxesz);
978 for (j = 0; j < native->u.syment.n_numaux; j++)
980 bfd_coff_swap_aux_out (abfd,
981 &((native + j + 1)->u.auxent),
985 native->u.syment.n_numaux,
987 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
990 bfd_release (abfd, buf);
993 /* Store the index for use when we write out the relocs. */
994 set_index (symbol, *written);
996 *written += numaux + 1;
1000 /* Write out a symbol to a COFF file that does not come from a COFF
1001 file originally. This symbol may have been created by the linker,
1002 or we may be linking a non COFF file to a COFF file. */
1005 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1006 debug_string_section_p, debug_string_size_p)
1009 unsigned int *written;
1010 bfd_size_type *string_size_p;
1011 asection **debug_string_section_p;
1012 bfd_size_type *debug_string_size_p;
1014 combined_entry_type *native;
1015 combined_entry_type dummy;
1018 native->u.syment.n_type = T_NULL;
1019 native->u.syment.n_flags = 0;
1020 if (bfd_is_und_section (symbol->section))
1022 native->u.syment.n_scnum = N_UNDEF;
1023 native->u.syment.n_value = symbol->value;
1025 else if (bfd_is_com_section (symbol->section))
1027 native->u.syment.n_scnum = N_UNDEF;
1028 native->u.syment.n_value = symbol->value;
1030 else if (symbol->flags & BSF_DEBUGGING)
1032 /* There isn't much point to writing out a debugging symbol
1033 unless we are prepared to convert it into COFF debugging
1034 format. So, we just ignore them. We must clobber the symbol
1035 name to keep it from being put in the string table. */
1041 native->u.syment.n_scnum =
1042 symbol->section->output_section->target_index;
1043 native->u.syment.n_value = (symbol->value
1044 + symbol->section->output_offset);
1045 if (! obj_pe (abfd))
1046 native->u.syment.n_value += symbol->section->output_section->vma;
1048 /* Copy the any flags from the the file header into the symbol.
1051 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1052 if (c != (coff_symbol_type *) NULL)
1053 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1057 native->u.syment.n_type = 0;
1058 if (symbol->flags & BSF_LOCAL)
1059 native->u.syment.n_sclass = C_STAT;
1060 else if (symbol->flags & BSF_WEAK)
1061 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1063 native->u.syment.n_sclass = C_EXT;
1064 native->u.syment.n_numaux = 0;
1066 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1067 debug_string_section_p, debug_string_size_p);
1070 /* Write a native symbol to a COFF file. */
1073 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1074 debug_string_section_p, debug_string_size_p)
1076 coff_symbol_type *symbol;
1077 unsigned int *written;
1078 bfd_size_type *string_size_p;
1079 asection **debug_string_section_p;
1080 bfd_size_type *debug_string_size_p;
1082 combined_entry_type *native = symbol->native;
1083 alent *lineno = symbol->lineno;
1085 /* If this symbol has an associated line number, we must store the
1086 symbol index in the line number field. We also tag the auxent to
1087 point to the right place in the lineno table. */
1088 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1090 unsigned int count = 0;
1091 lineno[count].u.offset = *written;
1092 if (native->u.syment.n_numaux)
1094 union internal_auxent *a = &((native + 1)->u.auxent);
1096 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1097 symbol->symbol.section->output_section->moving_line_filepos;
1100 /* Count and relocate all other linenumbers. */
1102 while (lineno[count].line_number != 0)
1106 I've been told this, but still need proof:
1107 > The second bug is also in `bfd/coffcode.h'. This bug
1108 > causes the linker to screw up the pc-relocations for
1109 > all the line numbers in COFF code. This bug isn't only
1110 > specific to A29K implementations, but affects all
1111 > systems using COFF format binaries. Note that in COFF
1112 > object files, the line number core offsets output by
1113 > the assembler are relative to the start of each
1114 > procedure, not to the start of the .text section. This
1115 > patch relocates the line numbers relative to the
1116 > `native->u.syment.n_value' instead of the section
1118 > modular!olson@cs.arizona.edu (Jon Olson)
1120 lineno[count].u.offset += native->u.syment.n_value;
1122 lineno[count].u.offset +=
1123 (symbol->symbol.section->output_section->vma
1124 + symbol->symbol.section->output_offset);
1128 symbol->done_lineno = true;
1130 symbol->symbol.section->output_section->moving_line_filepos +=
1131 count * bfd_coff_linesz (abfd);
1134 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1135 string_size_p, debug_string_section_p,
1136 debug_string_size_p);
1139 /* Write out the COFF symbols. */
1142 coff_write_symbols (abfd)
1145 bfd_size_type string_size;
1146 asection *debug_string_section;
1147 bfd_size_type debug_string_size;
1149 unsigned int limit = bfd_get_symcount (abfd);
1150 unsigned int written = 0;
1154 debug_string_section = NULL;
1155 debug_string_size = 0;
1157 /* If this target supports long section names, they must be put into
1158 the string table. This is supported by PE. This code must
1159 handle section names just as they are handled in
1160 coff_write_object_contents. */
1161 if (bfd_coff_long_section_names (abfd))
1165 for (o = abfd->sections; o != NULL; o = o->next)
1169 len = strlen (o->name);
1171 string_size += len + 1;
1175 /* Seek to the right place */
1176 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1179 /* Output all the symbols we have */
1182 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1184 asymbol *symbol = *p;
1185 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1187 if (c_symbol == (coff_symbol_type *) NULL
1188 || c_symbol->native == (combined_entry_type *) NULL)
1190 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1191 &debug_string_section,
1192 &debug_string_size))
1197 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1198 &string_size, &debug_string_section,
1199 &debug_string_size))
1204 obj_raw_syment_count (abfd) = written;
1206 /* Now write out strings */
1208 if (string_size != 0)
1210 unsigned int size = string_size + STRING_SIZE_SIZE;
1211 bfd_byte buffer[STRING_SIZE_SIZE];
1213 #if STRING_SIZE_SIZE == 4
1214 bfd_h_put_32 (abfd, size, buffer);
1216 #error Change bfd_h_put_32
1218 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1221 /* Handle long section names. This code must handle section
1222 names just as they are handled in coff_write_object_contents. */
1223 if (bfd_coff_long_section_names (abfd))
1227 for (o = abfd->sections; o != NULL; o = o->next)
1231 len = strlen (o->name);
1234 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1240 for (p = abfd->outsymbols, i = 0;
1245 size_t name_length = strlen (q->name);
1246 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1249 /* Figure out whether the symbol name should go in the string
1250 table. Symbol names that are short enough are stored
1251 directly in the syment structure. File names permit a
1252 different, longer, length in the syment structure. On
1253 XCOFF, some symbol names are stored in the .debug section
1254 rather than in the string table. */
1256 if (c_symbol == NULL
1257 || c_symbol->native == NULL)
1259 /* This is not a COFF symbol, so it certainly is not a
1260 file name, nor does it go in the .debug section. */
1261 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1263 else if (bfd_coff_symname_in_debug (abfd,
1264 &c_symbol->native->u.syment))
1266 /* This symbol name is in the XCOFF .debug section.
1267 Don't write it into the string table. */
1268 maxlen = name_length;
1270 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1271 && c_symbol->native->u.syment.n_numaux > 0)
1273 if (bfd_coff_force_symnames_in_strings (abfd))
1274 bfd_write (".file", 1, 6, abfd);
1275 maxlen = bfd_coff_filnmlen (abfd);
1278 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1280 if (name_length > maxlen)
1282 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1290 /* We would normally not write anything here, but we'll write
1291 out 4 so that any stupid coff reader which tries to read the
1292 string table even when there isn't one won't croak. */
1293 unsigned int size = STRING_SIZE_SIZE;
1294 bfd_byte buffer[STRING_SIZE_SIZE];
1296 #if STRING_SIZE_SIZE == 4
1297 bfd_h_put_32 (abfd, size, buffer);
1299 #error Change bfd_h_put_32
1301 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1302 != STRING_SIZE_SIZE)
1306 /* Make sure the .debug section was created to be the correct size.
1307 We should create it ourselves on the fly, but we don't because
1308 BFD won't let us write to any section until we know how large all
1309 the sections are. We could still do it by making another pass
1310 over the symbols. FIXME. */
1311 BFD_ASSERT (debug_string_size == 0
1312 || (debug_string_section != (asection *) NULL
1313 && (BFD_ALIGN (debug_string_size,
1314 1 << debug_string_section->alignment_power)
1315 == bfd_section_size (abfd, debug_string_section))));
1321 coff_write_linenumbers (abfd)
1325 bfd_size_type linesz;
1328 linesz = bfd_coff_linesz (abfd);
1329 buff = bfd_alloc (abfd, linesz);
1332 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1334 if (s->lineno_count)
1336 asymbol **q = abfd->outsymbols;
1337 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1339 /* Find all the linenumbers in this section */
1343 if (p->section->output_section == s)
1346 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1347 (bfd_asymbol_bfd (p), p));
1350 /* Found a linenumber entry, output */
1351 struct internal_lineno out;
1352 memset ((PTR) & out, 0, sizeof (out));
1354 out.l_addr.l_symndx = l->u.offset;
1355 bfd_coff_swap_lineno_out (abfd, &out, buff);
1356 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1359 while (l->line_number)
1361 out.l_lnno = l->line_number;
1362 out.l_addr.l_symndx = l->u.offset;
1363 bfd_coff_swap_lineno_out (abfd, &out, buff);
1364 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1374 bfd_release (abfd, buff);
1380 coff_get_lineno (ignore_abfd, symbol)
1381 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1384 return coffsymbol (symbol)->lineno;
1389 /* This is only called from coff_add_missing_symbols, which has been
1393 coff_section_symbol (abfd, name)
1397 asection *sec = bfd_make_section_old_way (abfd, name);
1399 combined_entry_type *csym;
1402 csym = coff_symbol_from (abfd, sym)->native;
1403 /* Make sure back-end COFF stuff is there. */
1408 coff_symbol_type sym;
1409 /* @@FIXME This shouldn't use a fixed size!! */
1410 combined_entry_type e[10];
1413 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1416 bfd_set_error (bfd_error_no_error);
1419 memset ((char *) f, 0, sizeof (*f));
1420 coff_symbol_from (abfd, sym)->native = csym = f->e;
1422 csym[0].u.syment.n_sclass = C_STAT;
1423 csym[0].u.syment.n_numaux = 1;
1424 /* SF_SET_STATICS (sym); @@ ??? */
1425 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1426 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1427 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1429 if (sec->output_section == NULL)
1431 sec->output_section = sec;
1432 sec->output_offset = 0;
1440 /* This function transforms the offsets into the symbol table into
1441 pointers to syments. */
1444 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1446 combined_entry_type *table_base;
1447 combined_entry_type *symbol;
1448 unsigned int indaux;
1449 combined_entry_type *auxent;
1451 unsigned int type = symbol->u.syment.n_type;
1452 unsigned int class = symbol->u.syment.n_sclass;
1454 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1456 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1457 (abfd, table_base, symbol, indaux, auxent))
1461 /* Don't bother if this is a file or a section */
1462 if (class == C_STAT && type == T_NULL)
1464 if (class == C_FILE)
1467 /* Otherwise patch up */
1468 #define N_TMASK coff_data (abfd)->local_n_tmask
1469 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1470 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1471 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1473 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1474 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1475 auxent->fix_end = 1;
1477 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1478 generate one, so we must be careful to ignore it. */
1479 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1481 auxent->u.auxent.x_sym.x_tagndx.p =
1482 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1483 auxent->fix_tag = 1;
1487 /* Allocate space for the ".debug" section, and read it.
1488 We did not read the debug section until now, because
1489 we didn't want to go to the trouble until someone needed it. */
1492 build_debug_section (abfd)
1495 char *debug_section;
1498 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1502 bfd_set_error (bfd_error_no_debug_section);
1506 debug_section = (PTR) bfd_alloc (abfd,
1507 bfd_get_section_size_before_reloc (sect));
1508 if (debug_section == NULL)
1511 /* Seek to the beginning of the `.debug' section and read it.
1512 Save the current position first; it is needed by our caller.
1513 Then read debug section and reset the file pointer. */
1515 position = bfd_tell (abfd);
1516 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1517 || (bfd_read (debug_section,
1518 bfd_get_section_size_before_reloc (sect), 1, abfd)
1519 != bfd_get_section_size_before_reloc (sect))
1520 || bfd_seek (abfd, position, SEEK_SET) != 0)
1522 return debug_section;
1525 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1526 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1527 be \0-terminated. */
1529 copy_name (abfd, name, maxlen)
1537 for (len = 0; len < maxlen; ++len)
1539 if (name[len] == '\0')
1545 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1547 strncpy (newname, name, len);
1548 newname[len] = '\0';
1552 /* Read in the external symbols. */
1555 _bfd_coff_get_external_symbols (abfd)
1558 bfd_size_type symesz;
1562 if (obj_coff_external_syms (abfd) != NULL)
1565 symesz = bfd_coff_symesz (abfd);
1567 size = obj_raw_syment_count (abfd) * symesz;
1569 syms = (PTR) bfd_malloc (size);
1570 if (syms == NULL && size != 0)
1573 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1574 || bfd_read (syms, size, 1, abfd) != size)
1581 obj_coff_external_syms (abfd) = syms;
1586 /* Read in the external strings. The strings are not loaded until
1587 they are needed. This is because we have no simple way of
1588 detecting a missing string table in an archive. */
1591 _bfd_coff_read_string_table (abfd)
1594 char extstrsize[STRING_SIZE_SIZE];
1598 if (obj_coff_strings (abfd) != NULL)
1599 return obj_coff_strings (abfd);
1601 if (obj_sym_filepos (abfd) == 0)
1603 bfd_set_error (bfd_error_no_symbols);
1608 (obj_sym_filepos (abfd)
1609 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1613 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1615 if (bfd_get_error () != bfd_error_file_truncated)
1618 /* There is no string table. */
1619 strsize = STRING_SIZE_SIZE;
1623 #if STRING_SIZE_SIZE == 4
1624 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1626 #error Change bfd_h_get_32
1630 if (strsize < STRING_SIZE_SIZE)
1632 (*_bfd_error_handler)
1633 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1634 (unsigned long) strsize);
1635 bfd_set_error (bfd_error_bad_value);
1639 strings = (char *) bfd_malloc (strsize);
1640 if (strings == NULL)
1643 if (bfd_read (strings + STRING_SIZE_SIZE,
1644 strsize - STRING_SIZE_SIZE, 1, abfd)
1645 != strsize - STRING_SIZE_SIZE)
1651 obj_coff_strings (abfd) = strings;
1656 /* Free up the external symbols and strings read from a COFF file. */
1659 _bfd_coff_free_symbols (abfd)
1662 if (obj_coff_external_syms (abfd) != NULL
1663 && ! obj_coff_keep_syms (abfd))
1665 free (obj_coff_external_syms (abfd));
1666 obj_coff_external_syms (abfd) = NULL;
1668 if (obj_coff_strings (abfd) != NULL
1669 && ! obj_coff_keep_strings (abfd))
1671 free (obj_coff_strings (abfd));
1672 obj_coff_strings (abfd) = NULL;
1677 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1678 knit the symbol names into a normalized form. By normalized here I
1679 mean that all symbols have an n_offset pointer that points to a null-
1680 terminated string. */
1682 combined_entry_type *
1683 coff_get_normalized_symtab (abfd)
1686 combined_entry_type *internal;
1687 combined_entry_type *internal_ptr;
1688 combined_entry_type *symbol_ptr;
1689 combined_entry_type *internal_end;
1690 bfd_size_type symesz;
1693 const char *string_table = NULL;
1694 char *debug_section = NULL;
1697 if (obj_raw_syments (abfd) != NULL)
1698 return obj_raw_syments (abfd);
1700 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1701 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1702 if (internal == NULL && size != 0)
1704 internal_end = internal + obj_raw_syment_count (abfd);
1706 if (! _bfd_coff_get_external_symbols (abfd))
1709 raw_src = (char *) obj_coff_external_syms (abfd);
1711 /* mark the end of the symbols */
1712 symesz = bfd_coff_symesz (abfd);
1713 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1715 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1716 probably possible. If one shows up, it will probably kill us. */
1718 /* Swap all the raw entries */
1719 for (internal_ptr = internal;
1721 raw_src += symesz, internal_ptr++)
1725 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1726 (PTR) & internal_ptr->u.syment);
1727 symbol_ptr = internal_ptr;
1730 i < symbol_ptr->u.syment.n_numaux;
1735 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1736 symbol_ptr->u.syment.n_type,
1737 symbol_ptr->u.syment.n_sclass,
1738 i, symbol_ptr->u.syment.n_numaux,
1739 &(internal_ptr->u.auxent));
1740 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1745 /* Free the raw symbols, but not the strings (if we have them). */
1746 obj_coff_keep_strings (abfd) = true;
1747 if (! _bfd_coff_free_symbols (abfd))
1750 for (internal_ptr = internal; internal_ptr < internal_end;
1753 if (internal_ptr->u.syment.n_sclass == C_FILE
1754 && internal_ptr->u.syment.n_numaux > 0)
1756 /* make a file symbol point to the name in the auxent, since
1757 the text ".file" is redundant */
1758 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1760 /* the filename is a long one, point into the string table */
1761 if (string_table == NULL)
1763 string_table = _bfd_coff_read_string_table (abfd);
1764 if (string_table == NULL)
1768 internal_ptr->u.syment._n._n_n._n_offset =
1771 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1775 /* Ordinary short filename, put into memory anyway. The
1776 Microsoft PE tools sometimes store a filename in
1777 multiple AUX entries. */
1778 if (internal_ptr->u.syment.n_numaux > 1
1779 && coff_data (abfd)->pe)
1781 internal_ptr->u.syment._n._n_n._n_offset =
1784 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1785 internal_ptr->u.syment.n_numaux * symesz));
1789 internal_ptr->u.syment._n._n_n._n_offset =
1792 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1793 bfd_coff_filnmlen (abfd)));
1799 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1801 /* This is a "short" name. Make it long. */
1802 unsigned long i = 0;
1803 char *newstring = NULL;
1805 /* find the length of this string without walking into memory
1807 for (i = 0; i < 8; ++i)
1809 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1812 } /* if end of string */
1813 } /* possible lengths of this string. */
1815 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1817 memset (newstring, 0, i);
1818 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1819 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1820 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1822 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1823 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1824 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1826 /* Long name already. Point symbol at the string in the
1828 if (string_table == NULL)
1830 string_table = _bfd_coff_read_string_table (abfd);
1831 if (string_table == NULL)
1834 internal_ptr->u.syment._n._n_n._n_offset =
1837 + internal_ptr->u.syment._n._n_n._n_offset));
1841 /* Long name in debug section. Very similar. */
1842 if (debug_section == NULL)
1843 debug_section = build_debug_section (abfd);
1844 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1845 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1848 internal_ptr += internal_ptr->u.syment.n_numaux;
1851 obj_raw_syments (abfd) = internal;
1852 BFD_ASSERT (obj_raw_syment_count (abfd)
1853 == (unsigned int) (internal_ptr - internal));
1856 } /* coff_get_normalized_symtab() */
1859 coff_get_reloc_upper_bound (abfd, asect)
1863 if (bfd_get_format (abfd) != bfd_object)
1865 bfd_set_error (bfd_error_invalid_operation);
1868 return (asect->reloc_count + 1) * sizeof (arelent *);
1872 coff_make_empty_symbol (abfd)
1875 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1878 memset (new, 0, sizeof *new);
1879 new->symbol.section = 0;
1881 new->lineno = (alent *) NULL;
1882 new->done_lineno = false;
1883 new->symbol.the_bfd = abfd;
1884 return &new->symbol;
1887 /* Make a debugging symbol. */
1890 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1892 PTR ptr ATTRIBUTE_UNUSED;
1893 unsigned long sz ATTRIBUTE_UNUSED;
1895 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1898 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1899 (but shouldn't be a constant). */
1900 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1903 new->symbol.section = bfd_abs_section_ptr;
1904 new->symbol.flags = BSF_DEBUGGING;
1905 new->lineno = (alent *) NULL;
1906 new->done_lineno = false;
1907 new->symbol.the_bfd = abfd;
1908 return &new->symbol;
1913 coff_get_symbol_info (abfd, symbol, ret)
1918 bfd_symbol_info (symbol, ret);
1919 if (coffsymbol (symbol)->native != NULL
1920 && coffsymbol (symbol)->native->fix_value)
1922 combined_entry_type *psym;
1924 psym = ((combined_entry_type *)
1925 coffsymbol (symbol)->native->u.syment.n_value);
1926 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1930 /* Return the COFF syment for a symbol. */
1933 bfd_coff_get_syment (abfd, symbol, psyment)
1936 struct internal_syment *psyment;
1938 coff_symbol_type *csym;
1940 csym = coff_symbol_from (abfd, symbol);
1941 if (csym == NULL || csym->native == NULL)
1943 bfd_set_error (bfd_error_invalid_operation);
1947 *psyment = csym->native->u.syment;
1949 if (csym->native->fix_value)
1950 psyment->n_value = ((combined_entry_type *) psyment->n_value
1951 - obj_raw_syments (abfd));
1953 /* FIXME: We should handle fix_line here. */
1958 /* Return the COFF auxent for a symbol. */
1961 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1965 union internal_auxent *pauxent;
1967 coff_symbol_type *csym;
1968 combined_entry_type *ent;
1970 csym = coff_symbol_from (abfd, symbol);
1973 || csym->native == NULL
1974 || indx >= csym->native->u.syment.n_numaux)
1976 bfd_set_error (bfd_error_invalid_operation);
1980 ent = csym->native + indx + 1;
1982 *pauxent = ent->u.auxent;
1985 pauxent->x_sym.x_tagndx.l =
1986 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1987 - obj_raw_syments (abfd));
1990 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1991 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1992 - obj_raw_syments (abfd));
1994 if (ent->fix_scnlen)
1995 pauxent->x_csect.x_scnlen.l =
1996 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1997 - obj_raw_syments (abfd));
2002 /* Print out information about COFF symbol. */
2005 coff_print_symbol (abfd, filep, symbol, how)
2009 bfd_print_symbol_type how;
2011 FILE *file = (FILE *) filep;
2015 case bfd_print_symbol_name:
2016 fprintf (file, "%s", symbol->name);
2019 case bfd_print_symbol_more:
2020 fprintf (file, "coff %s %s",
2021 coffsymbol (symbol)->native ? "n" : "g",
2022 coffsymbol (symbol)->lineno ? "l" : " ");
2025 case bfd_print_symbol_all:
2026 if (coffsymbol (symbol)->native)
2030 combined_entry_type *combined = coffsymbol (symbol)->native;
2031 combined_entry_type *root = obj_raw_syments (abfd);
2032 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2034 fprintf (file, "[%3ld]", (long) (combined - root));
2036 if (! combined->fix_value)
2037 val = (unsigned long) combined->u.syment.n_value;
2039 val = ((unsigned long)
2040 ((combined_entry_type *) combined->u.syment.n_value
2044 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2045 combined->u.syment.n_scnum,
2046 combined->u.syment.n_flags,
2047 combined->u.syment.n_type,
2048 combined->u.syment.n_sclass,
2049 combined->u.syment.n_numaux,
2053 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2055 combined_entry_type *auxp = combined + aux + 1;
2059 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2061 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2063 fprintf (file, "\n");
2065 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2068 switch (combined->u.syment.n_sclass)
2071 fprintf (file, "File ");
2075 if (combined->u.syment.n_type == T_NULL)
2076 /* probably a section symbol? */
2078 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2079 (long) auxp->u.auxent.x_scn.x_scnlen,
2080 auxp->u.auxent.x_scn.x_nreloc,
2081 auxp->u.auxent.x_scn.x_nlinno);
2082 if (auxp->u.auxent.x_scn.x_checksum != 0
2083 || auxp->u.auxent.x_scn.x_associated != 0
2084 || auxp->u.auxent.x_scn.x_comdat != 0)
2085 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2086 auxp->u.auxent.x_scn.x_checksum,
2087 auxp->u.auxent.x_scn.x_associated,
2088 auxp->u.auxent.x_scn.x_comdat);
2091 /* else fall through */
2093 if (ISFCN (combined->u.syment.n_type))
2096 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2098 auxp->u.auxent.x_sym.x_misc.x_fsize,
2099 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2102 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2104 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2107 /* else fall through */
2109 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2110 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2111 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2114 fprintf (file, " endndx %ld",
2116 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2124 fprintf (file, "\n%s :", l->u.sym->name);
2126 while (l->line_number)
2128 fprintf (file, "\n%4d : 0x%lx",
2131 (l->u.offset + symbol->section->vma)));
2138 bfd_print_symbol_vandf ((PTR) file, symbol);
2139 fprintf (file, " %-5s %s %s %s",
2140 symbol->section->name,
2141 coffsymbol (symbol)->native ? "n" : "g",
2142 coffsymbol (symbol)->lineno ? "l" : " ",
2148 /* Return whether a symbol name implies a local symbol. In COFF,
2149 local symbols generally start with ``.L''. Most targets use this
2150 function for the is_local_label_name entry point, but some may
2154 _bfd_coff_is_local_label_name (abfd, name)
2155 bfd *abfd ATTRIBUTE_UNUSED;
2158 return name[0] == '.' && name[1] == 'L';
2161 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2162 section, calculate and return the name of the source file and the line
2163 nearest to the wanted location. */
2166 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2167 functionname_ptr, line_ptr)
2172 CONST char **filename_ptr;
2173 CONST char **functionname_ptr;
2174 unsigned int *line_ptr;
2178 unsigned int line_base;
2179 coff_data_type *cof = coff_data (abfd);
2180 /* Run through the raw syments if available */
2181 combined_entry_type *p;
2182 combined_entry_type *pend;
2184 struct coff_section_tdata *sec_data;
2186 /* Before looking through the symbol table, try to use a .stab
2187 section to find the information. */
2188 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2189 &found, filename_ptr,
2190 functionname_ptr, line_ptr,
2191 &coff_data (abfd)->line_info))
2197 *functionname_ptr = 0;
2200 /* Don't try and find line numbers in a non coff file */
2201 if (!bfd_family_coff (abfd))
2207 /* Find the first C_FILE symbol. */
2208 p = cof->raw_syments;
2212 pend = p + cof->raw_syment_count;
2215 if (p->u.syment.n_sclass == C_FILE)
2217 p += 1 + p->u.syment.n_numaux;
2225 /* Look through the C_FILE symbols to find the best one. */
2226 sec_vma = bfd_get_section_vma (abfd, section);
2227 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2228 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2231 combined_entry_type *p2;
2233 for (p2 = p + 1 + p->u.syment.n_numaux;
2235 p2 += 1 + p2->u.syment.n_numaux)
2237 if (p2->u.syment.n_scnum > 0
2239 == coff_section_from_bfd_index (abfd,
2240 p2->u.syment.n_scnum)))
2242 if (p2->u.syment.n_sclass == C_FILE)
2249 /* We use <= MAXDIFF here so that if we get a zero length
2250 file, we actually use the next file entry. */
2252 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2253 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2255 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2256 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2259 /* Avoid endless loops on erroneous files by ensuring that
2260 we always move forward in the file. */
2261 if (p - cof->raw_syments >= p->u.syment.n_value)
2264 p = cof->raw_syments + p->u.syment.n_value;
2265 if (p > pend || p->u.syment.n_sclass != C_FILE)
2270 /* Now wander though the raw linenumbers of the section */
2271 /* If we have been called on this section before, and the offset we
2272 want is further down then we can prime the lookup loop. */
2273 sec_data = coff_section_data (abfd, section);
2274 if (sec_data != NULL
2276 && offset >= sec_data->offset)
2279 *functionname_ptr = sec_data->function;
2280 line_base = sec_data->line_base;
2288 if (section->lineno != NULL)
2290 bfd_vma last_value = 0;
2292 l = §ion->lineno[i];
2294 for (; i < section->lineno_count; i++)
2296 if (l->line_number == 0)
2298 /* Get the symbol this line number points at */
2299 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2300 if (coff->symbol.value > offset)
2302 *functionname_ptr = coff->symbol.name;
2303 last_value = coff->symbol.value;
2306 combined_entry_type *s = coff->native;
2307 s = s + 1 + s->u.syment.n_numaux;
2309 /* In XCOFF a debugging symbol can follow the
2311 if (s->u.syment.n_scnum == N_DEBUG)
2312 s = s + 1 + s->u.syment.n_numaux;
2314 /* S should now point to the .bf of the function. */
2315 if (s->u.syment.n_numaux)
2317 /* The linenumber is stored in the auxent. */
2318 union internal_auxent *a = &((s + 1)->u.auxent);
2319 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2320 *line_ptr = line_base;
2326 if (l->u.offset > offset)
2328 *line_ptr = l->line_number + line_base - 1;
2333 /* If we fell off the end of the loop, then assume that this
2334 symbol has no line number info. Otherwise, symbols with no
2335 line number info get reported with the line number of the
2336 last line of the last symbol which does have line number
2337 info. We use 0x100 as a slop to account for cases where the
2338 last line has executable code. */
2339 if (i >= section->lineno_count
2341 && offset - last_value > 0x100)
2343 *functionname_ptr = NULL;
2348 /* Cache the results for the next call. */
2349 if (sec_data == NULL && section->owner == abfd)
2351 section->used_by_bfd =
2352 ((PTR) bfd_zalloc (abfd,
2353 sizeof (struct coff_section_tdata)));
2354 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2356 if (sec_data != NULL)
2358 sec_data->offset = offset;
2360 sec_data->function = *functionname_ptr;
2361 sec_data->line_base = line_base;
2368 coff_sizeof_headers (abfd, reloc)
2376 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2380 size = bfd_coff_filhsz (abfd);
2383 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2387 /* Change the class of a coff symbol held by BFD. */
2389 bfd_coff_set_symbol_class (abfd, symbol, class)
2394 coff_symbol_type * csym;
2396 csym = coff_symbol_from (abfd, symbol);
2399 bfd_set_error (bfd_error_invalid_operation);
2402 else if (csym->native == NULL)
2404 /* This is an alien symbol which no native coff backend data.
2405 We cheat here by creating a fake native entry for it and
2406 then filling in the class. This code is based on that in
2407 coff_write_alien_symbol(). */
2409 combined_entry_type * native;
2411 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2415 memset (native, 0, sizeof (* native));
2417 native->u.syment.n_type = T_NULL;
2418 native->u.syment.n_sclass = class;
2420 if (bfd_is_und_section (symbol->section))
2422 native->u.syment.n_scnum = N_UNDEF;
2423 native->u.syment.n_value = symbol->value;
2425 else if (bfd_is_com_section (symbol->section))
2427 native->u.syment.n_scnum = N_UNDEF;
2428 native->u.syment.n_value = symbol->value;
2432 native->u.syment.n_scnum =
2433 symbol->section->output_section->target_index;
2434 native->u.syment.n_value = (symbol->value
2435 + symbol->section->output_offset);
2436 if (! obj_pe (abfd))
2437 native->u.syment.n_value += symbol->section->output_section->vma;
2439 /* Copy the any flags from the the file header into the symbol.
2441 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2444 csym->native = native;
2448 csym->native->u.syment.n_sclass = class;