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 *));
344 /* Canonicalize a COFF symbol table. */
347 coff_get_symtab (abfd, alocation)
351 unsigned int counter;
352 coff_symbol_type *symbase;
353 coff_symbol_type **location = (coff_symbol_type **) alocation;
355 if (!bfd_coff_slurp_symbol_table (abfd))
358 symbase = obj_symbols (abfd);
359 counter = bfd_get_symcount (abfd);
360 while (counter-- > 0)
361 *location++ = symbase++;
365 return bfd_get_symcount (abfd);
368 /* Get the name of a symbol. The caller must pass in a buffer of size
372 _bfd_coff_internal_syment_name (abfd, sym, buf)
374 const struct internal_syment *sym;
377 /* FIXME: It's not clear this will work correctly if sizeof
379 if (sym->_n._n_n._n_zeroes != 0
380 || sym->_n._n_n._n_offset == 0)
382 memcpy (buf, sym->_n._n_name, SYMNMLEN);
383 buf[SYMNMLEN] = '\0';
390 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
391 strings = obj_coff_strings (abfd);
394 strings = _bfd_coff_read_string_table (abfd);
398 return strings + sym->_n._n_n._n_offset;
402 /* Read in and swap the relocs. This returns a buffer holding the
403 relocs for section SEC in file ABFD. If CACHE is true and
404 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
405 the function is called again. If EXTERNAL_RELOCS is not NULL, it
406 is a buffer large enough to hold the unswapped relocs. If
407 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
408 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
409 value must be INTERNAL_RELOCS. The function returns NULL on error. */
411 struct internal_reloc *
412 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
413 require_internal, internal_relocs)
417 bfd_byte *external_relocs;
418 boolean require_internal;
419 struct internal_reloc *internal_relocs;
422 bfd_byte *free_external = NULL;
423 struct internal_reloc *free_internal = NULL;
426 struct internal_reloc *irel;
428 if (coff_section_data (abfd, sec) != NULL
429 && coff_section_data (abfd, sec)->relocs != NULL)
431 if (! require_internal)
432 return coff_section_data (abfd, sec)->relocs;
433 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
434 sec->reloc_count * sizeof (struct internal_reloc));
435 return internal_relocs;
438 relsz = bfd_coff_relsz (abfd);
440 if (external_relocs == NULL)
442 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
443 if (free_external == NULL && sec->reloc_count > 0)
445 external_relocs = free_external;
448 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
449 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
450 != relsz * sec->reloc_count))
453 if (internal_relocs == NULL)
455 free_internal = ((struct internal_reloc *)
456 bfd_malloc (sec->reloc_count
457 * sizeof (struct internal_reloc)));
458 if (free_internal == NULL && sec->reloc_count > 0)
460 internal_relocs = free_internal;
463 /* Swap in the relocs. */
464 erel = external_relocs;
465 erel_end = erel + relsz * sec->reloc_count;
466 irel = internal_relocs;
467 for (; erel < erel_end; erel += relsz, irel++)
468 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
470 if (free_external != NULL)
472 free (free_external);
473 free_external = NULL;
476 if (cache && free_internal != NULL)
478 if (coff_section_data (abfd, sec) == NULL)
481 (PTR) bfd_zalloc (abfd,
482 sizeof (struct coff_section_tdata));
483 if (sec->used_by_bfd == NULL)
485 coff_section_data (abfd, sec)->contents = NULL;
487 coff_section_data (abfd, sec)->relocs = free_internal;
490 return internal_relocs;
493 if (free_external != NULL)
494 free (free_external);
495 if (free_internal != NULL)
496 free (free_internal);
500 /* Set lineno_count for the output sections of a COFF file. */
503 coff_count_linenumbers (abfd)
506 unsigned int limit = bfd_get_symcount (abfd);
514 /* This may be from the backend linker, in which case the
515 lineno_count in the sections is correct. */
516 for (s = abfd->sections; s != NULL; s = s->next)
517 total += s->lineno_count;
521 for (s = abfd->sections; s != NULL; s = s->next)
522 BFD_ASSERT (s->lineno_count == 0);
524 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
526 asymbol *q_maybe = *p;
528 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
530 coff_symbol_type *q = coffsymbol (q_maybe);
532 /* The AIX 4.1 compiler can sometimes generate line numbers
533 attached to debugging symbols. We try to simply ignore
535 if (q->lineno != NULL
536 && q->symbol.section->owner != NULL)
538 /* This symbol has line numbers. Increment the owning
539 section's linenumber count. */
540 alent *l = q->lineno;
542 ++q->symbol.section->output_section->lineno_count;
545 while (l->line_number != 0)
548 ++q->symbol.section->output_section->lineno_count;
558 /* Takes a bfd and a symbol, returns a pointer to the coff specific
559 area of the symbol if there is one. */
563 coff_symbol_from (ignore_abfd, symbol)
564 bfd *ignore_abfd ATTRIBUTE_UNUSED;
567 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
568 return (coff_symbol_type *) NULL;
570 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
571 return (coff_symbol_type *) NULL;
573 return (coff_symbol_type *) symbol;
577 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
579 coff_symbol_type *coff_symbol_ptr;
580 struct internal_syment *syment;
583 /* Normalize the symbol flags */
584 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
586 /* a common symbol is undefined with a value */
587 syment->n_scnum = N_UNDEF;
588 syment->n_value = coff_symbol_ptr->symbol.value;
590 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
591 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
593 syment->n_value = coff_symbol_ptr->symbol.value;
595 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
597 syment->n_scnum = N_UNDEF;
600 /* FIXME: Do we need to handle the absolute section here? */
603 if (coff_symbol_ptr->symbol.section)
606 coff_symbol_ptr->symbol.section->output_section->target_index;
608 syment->n_value = (coff_symbol_ptr->symbol.value
609 + coff_symbol_ptr->symbol.section->output_offset);
612 syment->n_value += (syment->n_sclass == C_STATLAB)
613 ? coff_symbol_ptr->symbol.section->output_section->lma
614 : coff_symbol_ptr->symbol.section->output_section->vma;
620 /* This can happen, but I don't know why yet (steve@cygnus.com) */
621 syment->n_scnum = N_ABS;
622 syment->n_value = coff_symbol_ptr->symbol.value;
627 /* Run through all the symbols in the symbol table and work out what
628 their indexes into the symbol table will be when output.
630 Coff requires that each C_FILE symbol points to the next one in the
631 chain, and that the last one points to the first external symbol. We
635 coff_renumber_symbols (bfd_ptr, first_undef)
639 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
640 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
641 unsigned int native_index = 0;
642 struct internal_syment *last_file = (struct internal_syment *) NULL;
643 unsigned int symbol_index;
645 /* COFF demands that undefined symbols come after all other symbols.
646 Since we don't need to impose this extra knowledge on all our
647 client programs, deal with that here. Sort the symbol table;
648 just move the undefined symbols to the end, leaving the rest
649 alone. The O'Reilly book says that defined global symbols come
650 at the end before the undefined symbols, so we do that here as
652 /* @@ Do we have some condition we could test for, so we don't always
653 have to do this? I don't think relocatability is quite right, but
654 I'm not certain. [raeburn:19920508.1711EST] */
659 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
660 sizeof (asymbol *) * (symbol_count + 1));
663 bfd_ptr->outsymbols = newsyms;
664 for (i = 0; i < symbol_count; i++)
665 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
666 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
667 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
668 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
669 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
671 *newsyms++ = symbol_ptr_ptr[i];
673 for (i = 0; i < symbol_count; i++)
674 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
675 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
676 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
677 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
678 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
680 *newsyms++ = symbol_ptr_ptr[i];
682 *first_undef = newsyms - bfd_ptr->outsymbols;
684 for (i = 0; i < symbol_count; i++)
685 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
686 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
687 *newsyms++ = symbol_ptr_ptr[i];
688 *newsyms = (asymbol *) NULL;
689 symbol_ptr_ptr = bfd_ptr->outsymbols;
692 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
694 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
695 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
696 if (coff_symbol_ptr && coff_symbol_ptr->native)
698 combined_entry_type *s = coff_symbol_ptr->native;
701 if (s->u.syment.n_sclass == C_FILE)
703 if (last_file != (struct internal_syment *) NULL)
704 last_file->n_value = native_index;
705 last_file = &(s->u.syment);
710 /* Modify the symbol values according to their section and
713 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
715 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
716 s[i].offset = native_index++;
723 obj_conv_table_size (bfd_ptr) = native_index;
728 /* Run thorough the symbol table again, and fix it so that all
729 pointers to entries are changed to the entries' index in the output
733 coff_mangle_symbols (bfd_ptr)
736 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
737 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
738 unsigned int symbol_index;
740 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
742 coff_symbol_type *coff_symbol_ptr =
743 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
745 if (coff_symbol_ptr && coff_symbol_ptr->native)
748 combined_entry_type *s = coff_symbol_ptr->native;
752 /* FIXME: We should use a union here. */
753 s->u.syment.n_value =
754 ((combined_entry_type *) s->u.syment.n_value)->offset;
759 /* The value is the offset into the line number entries
760 for the symbol's section. On output, the symbol's
761 section should be N_DEBUG. */
762 s->u.syment.n_value =
763 (coff_symbol_ptr->symbol.section->output_section->line_filepos
764 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
765 coff_symbol_ptr->symbol.section =
766 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
767 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
769 for (i = 0; i < s->u.syment.n_numaux; i++)
771 combined_entry_type *a = s + i + 1;
774 a->u.auxent.x_sym.x_tagndx.l =
775 a->u.auxent.x_sym.x_tagndx.p->offset;
780 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
781 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
786 a->u.auxent.x_csect.x_scnlen.l =
787 a->u.auxent.x_csect.x_scnlen.p->offset;
796 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
797 debug_string_section_p, debug_string_size_p)
800 combined_entry_type *native;
801 bfd_size_type *string_size_p;
802 asection **debug_string_section_p;
803 bfd_size_type *debug_string_size_p;
805 unsigned int name_length;
806 union internal_auxent *auxent;
807 char *name = (char *) (symbol->name);
809 if (name == (char *) NULL)
811 /* coff symbols always have names, so we'll make one up */
812 symbol->name = "strange";
813 name = (char *) symbol->name;
815 name_length = strlen (name);
817 if (native->u.syment.n_sclass == C_FILE
818 && native->u.syment.n_numaux > 0)
820 unsigned int filnmlen;
822 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
823 auxent = &(native + 1)->u.auxent;
825 filnmlen = bfd_coff_filnmlen (abfd);
827 if (bfd_coff_long_filenames (abfd))
829 if (name_length <= filnmlen)
831 strncpy (auxent->x_file.x_fname, name, filnmlen);
835 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
836 auxent->x_file.x_n.x_zeroes = 0;
837 *string_size_p += name_length + 1;
842 strncpy (auxent->x_file.x_fname, name, filnmlen);
843 if (name_length > filnmlen)
844 name[filnmlen] = '\0';
849 if (name_length <= SYMNMLEN)
851 /* This name will fit into the symbol neatly */
852 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
854 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
856 native->u.syment._n._n_n._n_offset = (*string_size_p
858 native->u.syment._n._n_n._n_zeroes = 0;
859 *string_size_p += name_length + 1;
866 /* This name should be written into the .debug section. For
867 some reason each name is preceded by a two byte length
868 and also followed by a null byte. FIXME: We assume that
869 the .debug section has already been created, and that it
871 if (*debug_string_section_p == (asection *) NULL)
872 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
873 filepos = bfd_tell (abfd);
874 bfd_put_16 (abfd, name_length + 1, buf);
875 if (!bfd_set_section_contents (abfd,
876 *debug_string_section_p,
878 (file_ptr) *debug_string_size_p,
880 || !bfd_set_section_contents (abfd,
881 *debug_string_section_p,
883 ((file_ptr) *debug_string_size_p
885 (bfd_size_type) name_length + 1))
887 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
889 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
890 native->u.syment._n._n_n._n_zeroes = 0;
891 *debug_string_size_p += name_length + 3;
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 (abfd, symbol, native, written, string_size_p,
906 debug_string_section_p, debug_string_size_p)
909 combined_entry_type *native;
910 unsigned int *written;
911 bfd_size_type *string_size_p;
912 asection **debug_string_section_p;
913 bfd_size_type *debug_string_size_p;
915 unsigned int numaux = native->u.syment.n_numaux;
916 int type = native->u.syment.n_type;
917 int class = native->u.syment.n_sclass;
919 bfd_size_type symesz;
921 if (native->u.syment.n_sclass == C_FILE)
922 symbol->flags |= BSF_DEBUGGING;
924 if (symbol->flags & BSF_DEBUGGING
925 && bfd_is_abs_section (symbol->section))
927 native->u.syment.n_scnum = N_DEBUG;
929 else if (bfd_is_abs_section (symbol->section))
931 native->u.syment.n_scnum = N_ABS;
933 else if (bfd_is_und_section (symbol->section))
935 native->u.syment.n_scnum = N_UNDEF;
939 native->u.syment.n_scnum =
940 symbol->section->output_section->target_index;
943 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
944 debug_string_section_p, debug_string_size_p);
946 symesz = bfd_coff_symesz (abfd);
947 buf = bfd_alloc (abfd, symesz);
950 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
951 if (bfd_write (buf, 1, symesz, abfd) != symesz)
953 bfd_release (abfd, buf);
955 if (native->u.syment.n_numaux > 0)
957 bfd_size_type auxesz;
960 auxesz = bfd_coff_auxesz (abfd);
961 buf = bfd_alloc (abfd, auxesz);
964 for (j = 0; j < native->u.syment.n_numaux; j++)
966 bfd_coff_swap_aux_out (abfd,
967 &((native + j + 1)->u.auxent),
971 native->u.syment.n_numaux,
973 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
976 bfd_release (abfd, buf);
979 /* Store the index for use when we write out the relocs. */
980 set_index (symbol, *written);
982 *written += numaux + 1;
986 /* Write out a symbol to a COFF file that does not come from a COFF
987 file originally. This symbol may have been created by the linker,
988 or we may be linking a non COFF file to a COFF file. */
991 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
992 debug_string_section_p, debug_string_size_p)
995 unsigned int *written;
996 bfd_size_type *string_size_p;
997 asection **debug_string_section_p;
998 bfd_size_type *debug_string_size_p;
1000 combined_entry_type *native;
1001 combined_entry_type dummy;
1004 native->u.syment.n_type = T_NULL;
1005 native->u.syment.n_flags = 0;
1006 if (bfd_is_und_section (symbol->section))
1008 native->u.syment.n_scnum = N_UNDEF;
1009 native->u.syment.n_value = symbol->value;
1011 else if (bfd_is_com_section (symbol->section))
1013 native->u.syment.n_scnum = N_UNDEF;
1014 native->u.syment.n_value = symbol->value;
1016 else if (symbol->flags & BSF_DEBUGGING)
1018 /* There isn't much point to writing out a debugging symbol
1019 unless we are prepared to convert it into COFF debugging
1020 format. So, we just ignore them. We must clobber the symbol
1021 name to keep it from being put in the string table. */
1027 native->u.syment.n_scnum =
1028 symbol->section->output_section->target_index;
1029 native->u.syment.n_value = (symbol->value
1030 + symbol->section->output_offset);
1031 if (! obj_pe (abfd))
1032 native->u.syment.n_value += symbol->section->output_section->vma;
1034 /* Copy the any flags from the the file header into the symbol.
1037 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1038 if (c != (coff_symbol_type *) NULL)
1039 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1043 native->u.syment.n_type = 0;
1044 if (symbol->flags & BSF_LOCAL)
1045 native->u.syment.n_sclass = C_STAT;
1046 else if (symbol->flags & BSF_WEAK)
1047 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1049 native->u.syment.n_sclass = C_EXT;
1050 native->u.syment.n_numaux = 0;
1052 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1053 debug_string_section_p, debug_string_size_p);
1056 /* Write a native symbol to a COFF file. */
1059 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1060 debug_string_section_p, debug_string_size_p)
1062 coff_symbol_type *symbol;
1063 unsigned int *written;
1064 bfd_size_type *string_size_p;
1065 asection **debug_string_section_p;
1066 bfd_size_type *debug_string_size_p;
1068 combined_entry_type *native = symbol->native;
1069 alent *lineno = symbol->lineno;
1071 /* If this symbol has an associated line number, we must store the
1072 symbol index in the line number field. We also tag the auxent to
1073 point to the right place in the lineno table. */
1074 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1076 unsigned int count = 0;
1077 lineno[count].u.offset = *written;
1078 if (native->u.syment.n_numaux)
1080 union internal_auxent *a = &((native + 1)->u.auxent);
1082 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1083 symbol->symbol.section->output_section->moving_line_filepos;
1086 /* Count and relocate all other linenumbers. */
1088 while (lineno[count].line_number != 0)
1092 I've been told this, but still need proof:
1093 > The second bug is also in `bfd/coffcode.h'. This bug
1094 > causes the linker to screw up the pc-relocations for
1095 > all the line numbers in COFF code. This bug isn't only
1096 > specific to A29K implementations, but affects all
1097 > systems using COFF format binaries. Note that in COFF
1098 > object files, the line number core offsets output by
1099 > the assembler are relative to the start of each
1100 > procedure, not to the start of the .text section. This
1101 > patch relocates the line numbers relative to the
1102 > `native->u.syment.n_value' instead of the section
1104 > modular!olson@cs.arizona.edu (Jon Olson)
1106 lineno[count].u.offset += native->u.syment.n_value;
1108 lineno[count].u.offset +=
1109 (symbol->symbol.section->output_section->vma
1110 + symbol->symbol.section->output_offset);
1114 symbol->done_lineno = true;
1116 symbol->symbol.section->output_section->moving_line_filepos +=
1117 count * bfd_coff_linesz (abfd);
1120 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1121 string_size_p, debug_string_section_p,
1122 debug_string_size_p);
1125 /* Write out the COFF symbols. */
1128 coff_write_symbols (abfd)
1131 bfd_size_type string_size;
1132 asection *debug_string_section;
1133 bfd_size_type debug_string_size;
1135 unsigned int limit = bfd_get_symcount (abfd);
1136 unsigned int written = 0;
1140 debug_string_section = NULL;
1141 debug_string_size = 0;
1143 /* If this target supports long section names, they must be put into
1144 the string table. This is supported by PE. This code must
1145 handle section names just as they are handled in
1146 coff_write_object_contents. */
1147 if (bfd_coff_long_section_names (abfd))
1151 for (o = abfd->sections; o != NULL; o = o->next)
1155 len = strlen (o->name);
1157 string_size += len + 1;
1161 /* Seek to the right place */
1162 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1165 /* Output all the symbols we have */
1168 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1170 asymbol *symbol = *p;
1171 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1173 if (c_symbol == (coff_symbol_type *) NULL
1174 || c_symbol->native == (combined_entry_type *) NULL)
1176 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1177 &debug_string_section,
1178 &debug_string_size))
1183 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1184 &string_size, &debug_string_section,
1185 &debug_string_size))
1190 obj_raw_syment_count (abfd) = written;
1192 /* Now write out strings */
1194 if (string_size != 0)
1196 unsigned int size = string_size + STRING_SIZE_SIZE;
1197 bfd_byte buffer[STRING_SIZE_SIZE];
1199 #if STRING_SIZE_SIZE == 4
1200 bfd_h_put_32 (abfd, size, buffer);
1202 #error Change bfd_h_put_32
1204 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1207 /* Handle long section names. This code must handle section
1208 names just as they are handled in coff_write_object_contents. */
1209 if (bfd_coff_long_section_names (abfd))
1213 for (o = abfd->sections; o != NULL; o = o->next)
1217 len = strlen (o->name);
1220 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1226 for (p = abfd->outsymbols, i = 0;
1231 size_t name_length = strlen (q->name);
1232 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1235 /* Figure out whether the symbol name should go in the string
1236 table. Symbol names that are short enough are stored
1237 directly in the syment structure. File names permit a
1238 different, longer, length in the syment structure. On
1239 XCOFF, some symbol names are stored in the .debug section
1240 rather than in the string table. */
1242 if (c_symbol == NULL
1243 || c_symbol->native == NULL)
1245 /* This is not a COFF symbol, so it certainly is not a
1246 file name, nor does it go in the .debug section. */
1249 else if (bfd_coff_symname_in_debug (abfd,
1250 &c_symbol->native->u.syment))
1252 /* This symbol name is in the XCOFF .debug section.
1253 Don't write it into the string table. */
1254 maxlen = name_length;
1256 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1257 && c_symbol->native->u.syment.n_numaux > 0)
1258 maxlen = bfd_coff_filnmlen (abfd);
1262 if (name_length > maxlen)
1264 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1272 /* We would normally not write anything here, but we'll write
1273 out 4 so that any stupid coff reader which tries to read the
1274 string table even when there isn't one won't croak. */
1275 unsigned int size = STRING_SIZE_SIZE;
1276 bfd_byte buffer[STRING_SIZE_SIZE];
1278 #if STRING_SIZE_SIZE == 4
1279 bfd_h_put_32 (abfd, size, buffer);
1281 #error Change bfd_h_put_32
1283 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1284 != STRING_SIZE_SIZE)
1288 /* Make sure the .debug section was created to be the correct size.
1289 We should create it ourselves on the fly, but we don't because
1290 BFD won't let us write to any section until we know how large all
1291 the sections are. We could still do it by making another pass
1292 over the symbols. FIXME. */
1293 BFD_ASSERT (debug_string_size == 0
1294 || (debug_string_section != (asection *) NULL
1295 && (BFD_ALIGN (debug_string_size,
1296 1 << debug_string_section->alignment_power)
1297 == bfd_section_size (abfd, debug_string_section))));
1303 coff_write_linenumbers (abfd)
1307 bfd_size_type linesz;
1310 linesz = bfd_coff_linesz (abfd);
1311 buff = bfd_alloc (abfd, linesz);
1314 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1316 if (s->lineno_count)
1318 asymbol **q = abfd->outsymbols;
1319 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1321 /* Find all the linenumbers in this section */
1325 if (p->section->output_section == s)
1328 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1329 (bfd_asymbol_bfd (p), p));
1332 /* Found a linenumber entry, output */
1333 struct internal_lineno out;
1334 memset ((PTR) & out, 0, sizeof (out));
1336 out.l_addr.l_symndx = l->u.offset;
1337 bfd_coff_swap_lineno_out (abfd, &out, buff);
1338 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1341 while (l->line_number)
1343 out.l_lnno = l->line_number;
1344 out.l_addr.l_symndx = l->u.offset;
1345 bfd_coff_swap_lineno_out (abfd, &out, buff);
1346 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1356 bfd_release (abfd, buff);
1362 coff_get_lineno (ignore_abfd, symbol)
1363 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1366 return coffsymbol (symbol)->lineno;
1371 /* This is only called from coff_add_missing_symbols, which has been
1375 coff_section_symbol (abfd, name)
1379 asection *sec = bfd_make_section_old_way (abfd, name);
1381 combined_entry_type *csym;
1384 csym = coff_symbol_from (abfd, sym)->native;
1385 /* Make sure back-end COFF stuff is there. */
1390 coff_symbol_type sym;
1391 /* @@FIXME This shouldn't use a fixed size!! */
1392 combined_entry_type e[10];
1395 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1398 bfd_set_error (bfd_error_no_error);
1401 memset ((char *) f, 0, sizeof (*f));
1402 coff_symbol_from (abfd, sym)->native = csym = f->e;
1404 csym[0].u.syment.n_sclass = C_STAT;
1405 csym[0].u.syment.n_numaux = 1;
1406 /* SF_SET_STATICS (sym); @@ ??? */
1407 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1408 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1409 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1411 if (sec->output_section == NULL)
1413 sec->output_section = sec;
1414 sec->output_offset = 0;
1422 /* This function transforms the offsets into the symbol table into
1423 pointers to syments. */
1426 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1428 combined_entry_type *table_base;
1429 combined_entry_type *symbol;
1430 unsigned int indaux;
1431 combined_entry_type *auxent;
1433 unsigned int type = symbol->u.syment.n_type;
1434 unsigned int class = symbol->u.syment.n_sclass;
1436 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1438 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1439 (abfd, table_base, symbol, indaux, auxent))
1443 /* Don't bother if this is a file or a section */
1444 if (class == C_STAT && type == T_NULL)
1446 if (class == C_FILE)
1449 /* Otherwise patch up */
1450 #define N_TMASK coff_data (abfd)->local_n_tmask
1451 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1452 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1453 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1455 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1456 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1457 auxent->fix_end = 1;
1459 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1460 generate one, so we must be careful to ignore it. */
1461 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1463 auxent->u.auxent.x_sym.x_tagndx.p =
1464 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1465 auxent->fix_tag = 1;
1469 /* Allocate space for the ".debug" section, and read it.
1470 We did not read the debug section until now, because
1471 we didn't want to go to the trouble until someone needed it. */
1474 build_debug_section (abfd)
1477 char *debug_section;
1480 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1484 bfd_set_error (bfd_error_no_debug_section);
1488 debug_section = (PTR) bfd_alloc (abfd,
1489 bfd_get_section_size_before_reloc (sect));
1490 if (debug_section == NULL)
1493 /* Seek to the beginning of the `.debug' section and read it.
1494 Save the current position first; it is needed by our caller.
1495 Then read debug section and reset the file pointer. */
1497 position = bfd_tell (abfd);
1498 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1499 || (bfd_read (debug_section,
1500 bfd_get_section_size_before_reloc (sect), 1, abfd)
1501 != bfd_get_section_size_before_reloc (sect))
1502 || bfd_seek (abfd, position, SEEK_SET) != 0)
1504 return debug_section;
1508 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1509 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1510 be \0-terminated. */
1512 copy_name (abfd, name, maxlen)
1520 for (len = 0; len < maxlen; ++len)
1522 if (name[len] == '\0')
1528 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1530 strncpy (newname, name, len);
1531 newname[len] = '\0';
1535 /* Read in the external symbols. */
1538 _bfd_coff_get_external_symbols (abfd)
1541 bfd_size_type symesz;
1545 if (obj_coff_external_syms (abfd) != NULL)
1548 symesz = bfd_coff_symesz (abfd);
1550 size = obj_raw_syment_count (abfd) * symesz;
1552 syms = (PTR) bfd_malloc (size);
1553 if (syms == NULL && size != 0)
1556 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1557 || bfd_read (syms, size, 1, abfd) != size)
1564 obj_coff_external_syms (abfd) = syms;
1569 /* Read in the external strings. The strings are not loaded until
1570 they are needed. This is because we have no simple way of
1571 detecting a missing string table in an archive. */
1574 _bfd_coff_read_string_table (abfd)
1577 char extstrsize[STRING_SIZE_SIZE];
1581 if (obj_coff_strings (abfd) != NULL)
1582 return obj_coff_strings (abfd);
1584 if (obj_sym_filepos (abfd) == 0)
1586 bfd_set_error (bfd_error_no_symbols);
1591 (obj_sym_filepos (abfd)
1592 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1596 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1598 if (bfd_get_error () != bfd_error_file_truncated)
1601 /* There is no string table. */
1602 strsize = STRING_SIZE_SIZE;
1606 #if STRING_SIZE_SIZE == 4
1607 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1609 #error Change bfd_h_get_32
1613 if (strsize < STRING_SIZE_SIZE)
1615 (*_bfd_error_handler)
1616 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1617 (unsigned long) strsize);
1618 bfd_set_error (bfd_error_bad_value);
1622 strings = (char *) bfd_malloc (strsize);
1623 if (strings == NULL)
1626 if (bfd_read (strings + STRING_SIZE_SIZE,
1627 strsize - STRING_SIZE_SIZE, 1, abfd)
1628 != strsize - STRING_SIZE_SIZE)
1634 obj_coff_strings (abfd) = strings;
1639 /* Free up the external symbols and strings read from a COFF file. */
1642 _bfd_coff_free_symbols (abfd)
1645 if (obj_coff_external_syms (abfd) != NULL
1646 && ! obj_coff_keep_syms (abfd))
1648 free (obj_coff_external_syms (abfd));
1649 obj_coff_external_syms (abfd) = NULL;
1651 if (obj_coff_strings (abfd) != NULL
1652 && ! obj_coff_keep_strings (abfd))
1654 free (obj_coff_strings (abfd));
1655 obj_coff_strings (abfd) = NULL;
1660 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1661 knit the symbol names into a normalized form. By normalized here I
1662 mean that all symbols have an n_offset pointer that points to a null-
1663 terminated string. */
1665 combined_entry_type *
1666 coff_get_normalized_symtab (abfd)
1669 combined_entry_type *internal;
1670 combined_entry_type *internal_ptr;
1671 combined_entry_type *symbol_ptr;
1672 combined_entry_type *internal_end;
1673 bfd_size_type symesz;
1676 const char *string_table = NULL;
1677 char *debug_section = NULL;
1680 if (obj_raw_syments (abfd) != NULL)
1681 return obj_raw_syments (abfd);
1683 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1684 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1685 if (internal == NULL && size != 0)
1687 internal_end = internal + obj_raw_syment_count (abfd);
1689 if (! _bfd_coff_get_external_symbols (abfd))
1692 raw_src = (char *) obj_coff_external_syms (abfd);
1694 /* mark the end of the symbols */
1695 symesz = bfd_coff_symesz (abfd);
1696 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1698 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1699 probably possible. If one shows up, it will probably kill us. */
1701 /* Swap all the raw entries */
1702 for (internal_ptr = internal;
1704 raw_src += symesz, internal_ptr++)
1708 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1709 (PTR) & internal_ptr->u.syment);
1710 symbol_ptr = internal_ptr;
1713 i < symbol_ptr->u.syment.n_numaux;
1718 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1719 symbol_ptr->u.syment.n_type,
1720 symbol_ptr->u.syment.n_sclass,
1721 i, symbol_ptr->u.syment.n_numaux,
1722 &(internal_ptr->u.auxent));
1723 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1728 /* Free the raw symbols, but not the strings (if we have them). */
1729 obj_coff_keep_strings (abfd) = true;
1730 if (! _bfd_coff_free_symbols (abfd))
1733 for (internal_ptr = internal; internal_ptr < internal_end;
1736 if (internal_ptr->u.syment.n_sclass == C_FILE
1737 && internal_ptr->u.syment.n_numaux > 0)
1739 /* make a file symbol point to the name in the auxent, since
1740 the text ".file" is redundant */
1741 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1743 /* the filename is a long one, point into the string table */
1744 if (string_table == NULL)
1746 string_table = _bfd_coff_read_string_table (abfd);
1747 if (string_table == NULL)
1751 internal_ptr->u.syment._n._n_n._n_offset =
1754 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1758 /* Ordinary short filename, put into memory anyway. The
1759 Microsoft PE tools sometimes store a filename in
1760 multiple AUX entries. */
1761 if (internal_ptr->u.syment.n_numaux > 1
1762 && coff_data (abfd)->pe)
1764 internal_ptr->u.syment._n._n_n._n_offset =
1767 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1768 internal_ptr->u.syment.n_numaux * symesz));
1772 internal_ptr->u.syment._n._n_n._n_offset =
1775 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1776 bfd_coff_filnmlen (abfd)));
1782 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1784 /* This is a "short" name. Make it long. */
1785 unsigned long i = 0;
1786 char *newstring = NULL;
1788 /* find the length of this string without walking into memory
1790 for (i = 0; i < 8; ++i)
1792 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1795 } /* if end of string */
1796 } /* possible lengths of this string. */
1798 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1800 memset (newstring, 0, i);
1801 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1802 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1803 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1805 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1806 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1807 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1809 /* Long name already. Point symbol at the string in the
1811 if (string_table == NULL)
1813 string_table = _bfd_coff_read_string_table (abfd);
1814 if (string_table == NULL)
1817 internal_ptr->u.syment._n._n_n._n_offset =
1820 + internal_ptr->u.syment._n._n_n._n_offset));
1824 /* Long name in debug section. Very similar. */
1825 if (debug_section == NULL)
1826 debug_section = build_debug_section (abfd);
1827 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1828 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1831 internal_ptr += internal_ptr->u.syment.n_numaux;
1834 obj_raw_syments (abfd) = internal;
1835 BFD_ASSERT (obj_raw_syment_count (abfd)
1836 == (unsigned int) (internal_ptr - internal));
1839 } /* coff_get_normalized_symtab() */
1842 coff_get_reloc_upper_bound (abfd, asect)
1846 if (bfd_get_format (abfd) != bfd_object)
1848 bfd_set_error (bfd_error_invalid_operation);
1851 return (asect->reloc_count + 1) * sizeof (arelent *);
1855 coff_make_empty_symbol (abfd)
1858 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1861 memset (new, 0, sizeof *new);
1862 new->symbol.section = 0;
1864 new->lineno = (alent *) NULL;
1865 new->done_lineno = false;
1866 new->symbol.the_bfd = abfd;
1867 return &new->symbol;
1870 /* Make a debugging symbol. */
1873 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1875 PTR ptr ATTRIBUTE_UNUSED;
1876 unsigned long sz ATTRIBUTE_UNUSED;
1878 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1881 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1882 (but shouldn't be a constant). */
1883 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1886 new->symbol.section = bfd_abs_section_ptr;
1887 new->symbol.flags = BSF_DEBUGGING;
1888 new->lineno = (alent *) NULL;
1889 new->done_lineno = false;
1890 new->symbol.the_bfd = abfd;
1891 return &new->symbol;
1896 coff_get_symbol_info (abfd, symbol, ret)
1901 bfd_symbol_info (symbol, ret);
1902 if (coffsymbol (symbol)->native != NULL
1903 && coffsymbol (symbol)->native->fix_value)
1905 combined_entry_type *psym;
1907 psym = ((combined_entry_type *)
1908 coffsymbol (symbol)->native->u.syment.n_value);
1909 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1913 /* Return the COFF syment for a symbol. */
1916 bfd_coff_get_syment (abfd, symbol, psyment)
1919 struct internal_syment *psyment;
1921 coff_symbol_type *csym;
1923 csym = coff_symbol_from (abfd, symbol);
1924 if (csym == NULL || csym->native == NULL)
1926 bfd_set_error (bfd_error_invalid_operation);
1930 *psyment = csym->native->u.syment;
1932 if (csym->native->fix_value)
1933 psyment->n_value = ((combined_entry_type *) psyment->n_value
1934 - obj_raw_syments (abfd));
1936 /* FIXME: We should handle fix_line here. */
1941 /* Return the COFF auxent for a symbol. */
1944 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1948 union internal_auxent *pauxent;
1950 coff_symbol_type *csym;
1951 combined_entry_type *ent;
1953 csym = coff_symbol_from (abfd, symbol);
1956 || csym->native == NULL
1957 || indx >= csym->native->u.syment.n_numaux)
1959 bfd_set_error (bfd_error_invalid_operation);
1963 ent = csym->native + indx + 1;
1965 *pauxent = ent->u.auxent;
1968 pauxent->x_sym.x_tagndx.l =
1969 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1970 - obj_raw_syments (abfd));
1973 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1974 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1975 - obj_raw_syments (abfd));
1977 if (ent->fix_scnlen)
1978 pauxent->x_csect.x_scnlen.l =
1979 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1980 - obj_raw_syments (abfd));
1985 /* Print out information about COFF symbol. */
1988 coff_print_symbol (abfd, filep, symbol, how)
1992 bfd_print_symbol_type how;
1994 FILE *file = (FILE *) filep;
1998 case bfd_print_symbol_name:
1999 fprintf (file, "%s", symbol->name);
2002 case bfd_print_symbol_more:
2003 fprintf (file, "coff %s %s",
2004 coffsymbol (symbol)->native ? "n" : "g",
2005 coffsymbol (symbol)->lineno ? "l" : " ");
2008 case bfd_print_symbol_all:
2009 if (coffsymbol (symbol)->native)
2013 combined_entry_type *combined = coffsymbol (symbol)->native;
2014 combined_entry_type *root = obj_raw_syments (abfd);
2015 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2017 fprintf (file, "[%3ld]", (long) (combined - root));
2019 if (! combined->fix_value)
2020 val = (unsigned long) combined->u.syment.n_value;
2022 val = ((unsigned long)
2023 ((combined_entry_type *) combined->u.syment.n_value
2027 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2028 combined->u.syment.n_scnum,
2029 combined->u.syment.n_flags,
2030 combined->u.syment.n_type,
2031 combined->u.syment.n_sclass,
2032 combined->u.syment.n_numaux,
2036 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2038 combined_entry_type *auxp = combined + aux + 1;
2042 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2044 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2046 fprintf (file, "\n");
2048 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2051 switch (combined->u.syment.n_sclass)
2054 fprintf (file, "File ");
2058 if (combined->u.syment.n_type == T_NULL)
2059 /* probably a section symbol? */
2061 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2062 (long) auxp->u.auxent.x_scn.x_scnlen,
2063 auxp->u.auxent.x_scn.x_nreloc,
2064 auxp->u.auxent.x_scn.x_nlinno);
2065 if (auxp->u.auxent.x_scn.x_checksum != 0
2066 || auxp->u.auxent.x_scn.x_associated != 0
2067 || auxp->u.auxent.x_scn.x_comdat != 0)
2068 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2069 auxp->u.auxent.x_scn.x_checksum,
2070 auxp->u.auxent.x_scn.x_associated,
2071 auxp->u.auxent.x_scn.x_comdat);
2074 /* else fall through */
2076 if (ISFCN (combined->u.syment.n_type))
2079 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2081 auxp->u.auxent.x_sym.x_misc.x_fsize,
2082 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2085 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2087 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2090 /* else fall through */
2092 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2093 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2094 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2097 fprintf (file, " endndx %ld",
2099 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2107 fprintf (file, "\n%s :", l->u.sym->name);
2109 while (l->line_number)
2111 fprintf (file, "\n%4d : 0x%lx",
2114 (l->u.offset + symbol->section->vma)));
2121 bfd_print_symbol_vandf ((PTR) file, symbol);
2122 fprintf (file, " %-5s %s %s %s",
2123 symbol->section->name,
2124 coffsymbol (symbol)->native ? "n" : "g",
2125 coffsymbol (symbol)->lineno ? "l" : " ",
2131 /* Return whether a symbol name implies a local symbol. In COFF,
2132 local symbols generally start with ``.L''. Most targets use this
2133 function for the is_local_label_name entry point, but some may
2137 _bfd_coff_is_local_label_name (abfd, name)
2138 bfd *abfd ATTRIBUTE_UNUSED;
2141 return name[0] == '.' && name[1] == 'L';
2144 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2145 section, calculate and return the name of the source file and the line
2146 nearest to the wanted location. */
2149 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2150 functionname_ptr, line_ptr)
2155 CONST char **filename_ptr;
2156 CONST char **functionname_ptr;
2157 unsigned int *line_ptr;
2161 unsigned int line_base;
2162 coff_data_type *cof = coff_data (abfd);
2163 /* Run through the raw syments if available */
2164 combined_entry_type *p;
2165 combined_entry_type *pend;
2167 struct coff_section_tdata *sec_data;
2169 /* Before looking through the symbol table, try to use a .stab
2170 section to find the information. */
2171 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2172 &found, filename_ptr,
2173 functionname_ptr, line_ptr,
2174 &coff_data (abfd)->line_info))
2180 *functionname_ptr = 0;
2183 /* Don't try and find line numbers in a non coff file */
2184 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2190 /* Find the first C_FILE symbol. */
2191 p = cof->raw_syments;
2195 pend = p + cof->raw_syment_count;
2198 if (p->u.syment.n_sclass == C_FILE)
2200 p += 1 + p->u.syment.n_numaux;
2208 /* Look through the C_FILE symbols to find the best one. */
2209 sec_vma = bfd_get_section_vma (abfd, section);
2210 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2211 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2214 combined_entry_type *p2;
2216 for (p2 = p + 1 + p->u.syment.n_numaux;
2218 p2 += 1 + p2->u.syment.n_numaux)
2220 if (p2->u.syment.n_scnum > 0
2222 == coff_section_from_bfd_index (abfd,
2223 p2->u.syment.n_scnum)))
2225 if (p2->u.syment.n_sclass == C_FILE)
2232 /* We use <= MAXDIFF here so that if we get a zero length
2233 file, we actually use the next file entry. */
2235 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2236 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2238 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2239 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2242 /* Avoid endless loops on erroneous files by ensuring that
2243 we always move forward in the file. */
2244 if (p - cof->raw_syments >= p->u.syment.n_value)
2247 p = cof->raw_syments + p->u.syment.n_value;
2248 if (p > pend || p->u.syment.n_sclass != C_FILE)
2253 /* Now wander though the raw linenumbers of the section */
2254 /* If we have been called on this section before, and the offset we
2255 want is further down then we can prime the lookup loop. */
2256 sec_data = coff_section_data (abfd, section);
2257 if (sec_data != NULL
2259 && offset >= sec_data->offset)
2262 *functionname_ptr = sec_data->function;
2263 line_base = sec_data->line_base;
2271 if (section->lineno != NULL)
2273 bfd_vma last_value = 0;
2275 l = §ion->lineno[i];
2277 for (; i < section->lineno_count; i++)
2279 if (l->line_number == 0)
2281 /* Get the symbol this line number points at */
2282 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2283 if (coff->symbol.value > offset)
2285 *functionname_ptr = coff->symbol.name;
2286 last_value = coff->symbol.value;
2289 combined_entry_type *s = coff->native;
2290 s = s + 1 + s->u.syment.n_numaux;
2292 /* In XCOFF a debugging symbol can follow the
2294 if (s->u.syment.n_scnum == N_DEBUG)
2295 s = s + 1 + s->u.syment.n_numaux;
2297 /* S should now point to the .bf of the function. */
2298 if (s->u.syment.n_numaux)
2300 /* The linenumber is stored in the auxent. */
2301 union internal_auxent *a = &((s + 1)->u.auxent);
2302 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2303 *line_ptr = line_base;
2309 if (l->u.offset > offset)
2311 *line_ptr = l->line_number + line_base - 1;
2316 /* If we fell off the end of the loop, then assume that this
2317 symbol has no line number info. Otherwise, symbols with no
2318 line number info get reported with the line number of the
2319 last line of the last symbol which does have line number
2320 info. We use 0x100 as a slop to account for cases where the
2321 last line has executable code. */
2322 if (i >= section->lineno_count
2324 && offset - last_value > 0x100)
2326 *functionname_ptr = NULL;
2331 /* Cache the results for the next call. */
2332 if (sec_data == NULL && section->owner == abfd)
2334 section->used_by_bfd =
2335 ((PTR) bfd_zalloc (abfd,
2336 sizeof (struct coff_section_tdata)));
2337 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2339 if (sec_data != NULL)
2341 sec_data->offset = offset;
2343 sec_data->function = *functionname_ptr;
2344 sec_data->line_base = line_base;
2351 coff_sizeof_headers (abfd, reloc)
2359 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2363 size = bfd_coff_filhsz (abfd);
2366 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2370 /* Change the class of a coff symbol held by BFD. */
2372 bfd_coff_set_symbol_class (abfd, symbol, class)
2377 coff_symbol_type * csym;
2379 csym = coff_symbol_from (abfd, symbol);
2382 bfd_set_error (bfd_error_invalid_operation);
2385 else if (csym->native == NULL)
2387 /* This is an alien symbol which no native coff backend data.
2388 We cheat here by creating a fake native entry for it and
2389 then filling in the class. This code is based on that in
2390 coff_write_alien_symbol(). */
2392 combined_entry_type * native;
2394 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2398 memset (native, 0, sizeof (* native));
2400 native->u.syment.n_type = T_NULL;
2401 native->u.syment.n_sclass = class;
2403 if (bfd_is_und_section (symbol->section))
2405 native->u.syment.n_scnum = N_UNDEF;
2406 native->u.syment.n_value = symbol->value;
2408 else if (bfd_is_com_section (symbol->section))
2410 native->u.syment.n_scnum = N_UNDEF;
2411 native->u.syment.n_value = symbol->value;
2415 native->u.syment.n_scnum =
2416 symbol->section->output_section->target_index;
2417 native->u.syment.n_value = (symbol->value
2418 + symbol->section->output_offset);
2419 if (! obj_pe (abfd))
2420 native->u.syment.n_value += symbol->section->output_section->vma;
2422 /* Copy the any flags from the the file header into the symbol.
2424 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2427 csym->native = native;
2431 csym->native->u.syment.n_sclass = class;