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 coff_symbol_ptr->symbol.section->output_section->vma;
617 /* This can happen, but I don't know why yet (steve@cygnus.com) */
618 syment->n_scnum = N_ABS;
619 syment->n_value = coff_symbol_ptr->symbol.value;
624 /* Run through all the symbols in the symbol table and work out what
625 their indexes into the symbol table will be when output.
627 Coff requires that each C_FILE symbol points to the next one in the
628 chain, and that the last one points to the first external symbol. We
632 coff_renumber_symbols (bfd_ptr, first_undef)
636 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
637 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
638 unsigned int native_index = 0;
639 struct internal_syment *last_file = (struct internal_syment *) NULL;
640 unsigned int symbol_index;
642 /* COFF demands that undefined symbols come after all other symbols.
643 Since we don't need to impose this extra knowledge on all our
644 client programs, deal with that here. Sort the symbol table;
645 just move the undefined symbols to the end, leaving the rest
646 alone. The O'Reilly book says that defined global symbols come
647 at the end before the undefined symbols, so we do that here as
649 /* @@ Do we have some condition we could test for, so we don't always
650 have to do this? I don't think relocatability is quite right, but
651 I'm not certain. [raeburn:19920508.1711EST] */
656 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
657 sizeof (asymbol *) * (symbol_count + 1));
660 bfd_ptr->outsymbols = newsyms;
661 for (i = 0; i < symbol_count; i++)
662 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
663 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
664 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
665 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
666 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
668 *newsyms++ = symbol_ptr_ptr[i];
670 for (i = 0; i < symbol_count; i++)
671 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
672 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
673 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
674 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
675 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
677 *newsyms++ = symbol_ptr_ptr[i];
679 *first_undef = newsyms - bfd_ptr->outsymbols;
681 for (i = 0; i < symbol_count; i++)
682 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
683 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
684 *newsyms++ = symbol_ptr_ptr[i];
685 *newsyms = (asymbol *) NULL;
686 symbol_ptr_ptr = bfd_ptr->outsymbols;
689 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
691 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
692 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
693 if (coff_symbol_ptr && coff_symbol_ptr->native)
695 combined_entry_type *s = coff_symbol_ptr->native;
698 if (s->u.syment.n_sclass == C_FILE)
700 if (last_file != (struct internal_syment *) NULL)
701 last_file->n_value = native_index;
702 last_file = &(s->u.syment);
707 /* Modify the symbol values according to their section and
710 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
712 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
713 s[i].offset = native_index++;
720 obj_conv_table_size (bfd_ptr) = native_index;
725 /* Run thorough the symbol table again, and fix it so that all
726 pointers to entries are changed to the entries' index in the output
730 coff_mangle_symbols (bfd_ptr)
733 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
734 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
735 unsigned int symbol_index;
737 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
739 coff_symbol_type *coff_symbol_ptr =
740 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
742 if (coff_symbol_ptr && coff_symbol_ptr->native)
745 combined_entry_type *s = coff_symbol_ptr->native;
749 /* FIXME: We should use a union here. */
750 s->u.syment.n_value =
751 ((combined_entry_type *) s->u.syment.n_value)->offset;
756 /* The value is the offset into the line number entries
757 for the symbol's section. On output, the symbol's
758 section should be N_DEBUG. */
759 s->u.syment.n_value =
760 (coff_symbol_ptr->symbol.section->output_section->line_filepos
761 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
762 coff_symbol_ptr->symbol.section =
763 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
764 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
766 for (i = 0; i < s->u.syment.n_numaux; i++)
768 combined_entry_type *a = s + i + 1;
771 a->u.auxent.x_sym.x_tagndx.l =
772 a->u.auxent.x_sym.x_tagndx.p->offset;
777 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
778 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
783 a->u.auxent.x_csect.x_scnlen.l =
784 a->u.auxent.x_csect.x_scnlen.p->offset;
793 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
794 debug_string_section_p, debug_string_size_p)
797 combined_entry_type *native;
798 bfd_size_type *string_size_p;
799 asection **debug_string_section_p;
800 bfd_size_type *debug_string_size_p;
802 unsigned int name_length;
803 union internal_auxent *auxent;
804 char *name = (char *) (symbol->name);
806 if (name == (char *) NULL)
808 /* coff symbols always have names, so we'll make one up */
809 symbol->name = "strange";
810 name = (char *) symbol->name;
812 name_length = strlen (name);
814 if (native->u.syment.n_sclass == C_FILE
815 && native->u.syment.n_numaux > 0)
817 unsigned int filnmlen;
819 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
820 auxent = &(native + 1)->u.auxent;
822 filnmlen = bfd_coff_filnmlen (abfd);
824 if (bfd_coff_long_filenames (abfd))
826 if (name_length <= filnmlen)
828 strncpy (auxent->x_file.x_fname, name, filnmlen);
832 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
833 auxent->x_file.x_n.x_zeroes = 0;
834 *string_size_p += name_length + 1;
839 strncpy (auxent->x_file.x_fname, name, filnmlen);
840 if (name_length > filnmlen)
841 name[filnmlen] = '\0';
846 if (name_length <= SYMNMLEN)
848 /* This name will fit into the symbol neatly */
849 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
851 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
853 native->u.syment._n._n_n._n_offset = (*string_size_p
855 native->u.syment._n._n_n._n_zeroes = 0;
856 *string_size_p += name_length + 1;
863 /* This name should be written into the .debug section. For
864 some reason each name is preceded by a two byte length
865 and also followed by a null byte. FIXME: We assume that
866 the .debug section has already been created, and that it
868 if (*debug_string_section_p == (asection *) NULL)
869 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
870 filepos = bfd_tell (abfd);
871 bfd_put_16 (abfd, name_length + 1, buf);
872 if (!bfd_set_section_contents (abfd,
873 *debug_string_section_p,
875 (file_ptr) *debug_string_size_p,
877 || !bfd_set_section_contents (abfd,
878 *debug_string_section_p,
880 ((file_ptr) *debug_string_size_p
882 (bfd_size_type) name_length + 1))
884 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
886 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
887 native->u.syment._n._n_n._n_zeroes = 0;
888 *debug_string_size_p += name_length + 3;
893 /* We need to keep track of the symbol index so that when we write out
894 the relocs we can get the index for a symbol. This method is a
897 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
899 /* Write a symbol out to a COFF file. */
902 coff_write_symbol (abfd, symbol, native, written, string_size_p,
903 debug_string_section_p, debug_string_size_p)
906 combined_entry_type *native;
907 unsigned int *written;
908 bfd_size_type *string_size_p;
909 asection **debug_string_section_p;
910 bfd_size_type *debug_string_size_p;
912 unsigned int numaux = native->u.syment.n_numaux;
913 int type = native->u.syment.n_type;
914 int class = native->u.syment.n_sclass;
916 bfd_size_type symesz;
918 if (native->u.syment.n_sclass == C_FILE)
919 symbol->flags |= BSF_DEBUGGING;
921 if (symbol->flags & BSF_DEBUGGING
922 && bfd_is_abs_section (symbol->section))
924 native->u.syment.n_scnum = N_DEBUG;
926 else if (bfd_is_abs_section (symbol->section))
928 native->u.syment.n_scnum = N_ABS;
930 else if (bfd_is_und_section (symbol->section))
932 native->u.syment.n_scnum = N_UNDEF;
936 native->u.syment.n_scnum =
937 symbol->section->output_section->target_index;
940 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
941 debug_string_section_p, debug_string_size_p);
943 symesz = bfd_coff_symesz (abfd);
944 buf = bfd_alloc (abfd, symesz);
947 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
948 if (bfd_write (buf, 1, symesz, abfd) != symesz)
950 bfd_release (abfd, buf);
952 if (native->u.syment.n_numaux > 0)
954 bfd_size_type auxesz;
957 auxesz = bfd_coff_auxesz (abfd);
958 buf = bfd_alloc (abfd, auxesz);
961 for (j = 0; j < native->u.syment.n_numaux; j++)
963 bfd_coff_swap_aux_out (abfd,
964 &((native + j + 1)->u.auxent),
968 native->u.syment.n_numaux,
970 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
973 bfd_release (abfd, buf);
976 /* Store the index for use when we write out the relocs. */
977 set_index (symbol, *written);
979 *written += numaux + 1;
983 /* Write out a symbol to a COFF file that does not come from a COFF
984 file originally. This symbol may have been created by the linker,
985 or we may be linking a non COFF file to a COFF file. */
988 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
989 debug_string_section_p, debug_string_size_p)
992 unsigned int *written;
993 bfd_size_type *string_size_p;
994 asection **debug_string_section_p;
995 bfd_size_type *debug_string_size_p;
997 combined_entry_type *native;
998 combined_entry_type dummy;
1001 native->u.syment.n_type = T_NULL;
1002 native->u.syment.n_flags = 0;
1003 if (bfd_is_und_section (symbol->section))
1005 native->u.syment.n_scnum = N_UNDEF;
1006 native->u.syment.n_value = symbol->value;
1008 else if (bfd_is_com_section (symbol->section))
1010 native->u.syment.n_scnum = N_UNDEF;
1011 native->u.syment.n_value = symbol->value;
1013 else if (symbol->flags & BSF_DEBUGGING)
1015 /* There isn't much point to writing out a debugging symbol
1016 unless we are prepared to convert it into COFF debugging
1017 format. So, we just ignore them. We must clobber the symbol
1018 name to keep it from being put in the string table. */
1024 native->u.syment.n_scnum =
1025 symbol->section->output_section->target_index;
1026 native->u.syment.n_value = (symbol->value
1027 + symbol->section->output_offset);
1028 if (! obj_pe (abfd))
1029 native->u.syment.n_value += symbol->section->output_section->vma;
1031 /* Copy the any flags from the the file header into the symbol.
1034 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1035 if (c != (coff_symbol_type *) NULL)
1036 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1040 native->u.syment.n_type = 0;
1041 if (symbol->flags & BSF_LOCAL)
1042 native->u.syment.n_sclass = C_STAT;
1043 else if (symbol->flags & BSF_WEAK)
1044 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1046 native->u.syment.n_sclass = C_EXT;
1047 native->u.syment.n_numaux = 0;
1049 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1050 debug_string_section_p, debug_string_size_p);
1053 /* Write a native symbol to a COFF file. */
1056 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1057 debug_string_section_p, debug_string_size_p)
1059 coff_symbol_type *symbol;
1060 unsigned int *written;
1061 bfd_size_type *string_size_p;
1062 asection **debug_string_section_p;
1063 bfd_size_type *debug_string_size_p;
1065 combined_entry_type *native = symbol->native;
1066 alent *lineno = symbol->lineno;
1068 /* If this symbol has an associated line number, we must store the
1069 symbol index in the line number field. We also tag the auxent to
1070 point to the right place in the lineno table. */
1071 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1073 unsigned int count = 0;
1074 lineno[count].u.offset = *written;
1075 if (native->u.syment.n_numaux)
1077 union internal_auxent *a = &((native + 1)->u.auxent);
1079 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1080 symbol->symbol.section->output_section->moving_line_filepos;
1083 /* Count and relocate all other linenumbers. */
1085 while (lineno[count].line_number != 0)
1089 I've been told this, but still need proof:
1090 > The second bug is also in `bfd/coffcode.h'. This bug
1091 > causes the linker to screw up the pc-relocations for
1092 > all the line numbers in COFF code. This bug isn't only
1093 > specific to A29K implementations, but affects all
1094 > systems using COFF format binaries. Note that in COFF
1095 > object files, the line number core offsets output by
1096 > the assembler are relative to the start of each
1097 > procedure, not to the start of the .text section. This
1098 > patch relocates the line numbers relative to the
1099 > `native->u.syment.n_value' instead of the section
1101 > modular!olson@cs.arizona.edu (Jon Olson)
1103 lineno[count].u.offset += native->u.syment.n_value;
1105 lineno[count].u.offset +=
1106 (symbol->symbol.section->output_section->vma
1107 + symbol->symbol.section->output_offset);
1111 symbol->done_lineno = true;
1113 symbol->symbol.section->output_section->moving_line_filepos +=
1114 count * bfd_coff_linesz (abfd);
1117 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1118 string_size_p, debug_string_section_p,
1119 debug_string_size_p);
1122 /* Write out the COFF symbols. */
1125 coff_write_symbols (abfd)
1128 bfd_size_type string_size;
1129 asection *debug_string_section;
1130 bfd_size_type debug_string_size;
1132 unsigned int limit = bfd_get_symcount (abfd);
1133 unsigned int written = 0;
1137 debug_string_section = NULL;
1138 debug_string_size = 0;
1140 /* If this target supports long section names, they must be put into
1141 the string table. This is supported by PE. This code must
1142 handle section names just as they are handled in
1143 coff_write_object_contents. */
1144 if (bfd_coff_long_section_names (abfd))
1148 for (o = abfd->sections; o != NULL; o = o->next)
1152 len = strlen (o->name);
1154 string_size += len + 1;
1158 /* Seek to the right place */
1159 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1162 /* Output all the symbols we have */
1165 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1167 asymbol *symbol = *p;
1168 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1170 if (c_symbol == (coff_symbol_type *) NULL
1171 || c_symbol->native == (combined_entry_type *) NULL)
1173 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1174 &debug_string_section,
1175 &debug_string_size))
1180 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1181 &string_size, &debug_string_section,
1182 &debug_string_size))
1187 obj_raw_syment_count (abfd) = written;
1189 /* Now write out strings */
1191 if (string_size != 0)
1193 unsigned int size = string_size + STRING_SIZE_SIZE;
1194 bfd_byte buffer[STRING_SIZE_SIZE];
1196 #if STRING_SIZE_SIZE == 4
1197 bfd_h_put_32 (abfd, size, buffer);
1199 #error Change bfd_h_put_32
1201 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1204 /* Handle long section names. This code must handle section
1205 names just as they are handled in coff_write_object_contents. */
1206 if (bfd_coff_long_section_names (abfd))
1210 for (o = abfd->sections; o != NULL; o = o->next)
1214 len = strlen (o->name);
1217 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1223 for (p = abfd->outsymbols, i = 0;
1228 size_t name_length = strlen (q->name);
1229 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1232 /* Figure out whether the symbol name should go in the string
1233 table. Symbol names that are short enough are stored
1234 directly in the syment structure. File names permit a
1235 different, longer, length in the syment structure. On
1236 XCOFF, some symbol names are stored in the .debug section
1237 rather than in the string table. */
1239 if (c_symbol == NULL
1240 || c_symbol->native == NULL)
1242 /* This is not a COFF symbol, so it certainly is not a
1243 file name, nor does it go in the .debug section. */
1246 else if (bfd_coff_symname_in_debug (abfd,
1247 &c_symbol->native->u.syment))
1249 /* This symbol name is in the XCOFF .debug section.
1250 Don't write it into the string table. */
1251 maxlen = name_length;
1253 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1254 && c_symbol->native->u.syment.n_numaux > 0)
1255 maxlen = bfd_coff_filnmlen (abfd);
1259 if (name_length > maxlen)
1261 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1269 /* We would normally not write anything here, but we'll write
1270 out 4 so that any stupid coff reader which tries to read the
1271 string table even when there isn't one won't croak. */
1272 unsigned int size = STRING_SIZE_SIZE;
1273 bfd_byte buffer[STRING_SIZE_SIZE];
1275 #if STRING_SIZE_SIZE == 4
1276 bfd_h_put_32 (abfd, size, buffer);
1278 #error Change bfd_h_put_32
1280 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1281 != STRING_SIZE_SIZE)
1285 /* Make sure the .debug section was created to be the correct size.
1286 We should create it ourselves on the fly, but we don't because
1287 BFD won't let us write to any section until we know how large all
1288 the sections are. We could still do it by making another pass
1289 over the symbols. FIXME. */
1290 BFD_ASSERT (debug_string_size == 0
1291 || (debug_string_section != (asection *) NULL
1292 && (BFD_ALIGN (debug_string_size,
1293 1 << debug_string_section->alignment_power)
1294 == bfd_section_size (abfd, debug_string_section))));
1300 coff_write_linenumbers (abfd)
1304 bfd_size_type linesz;
1307 linesz = bfd_coff_linesz (abfd);
1308 buff = bfd_alloc (abfd, linesz);
1311 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1313 if (s->lineno_count)
1315 asymbol **q = abfd->outsymbols;
1316 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1318 /* Find all the linenumbers in this section */
1322 if (p->section->output_section == s)
1325 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1326 (bfd_asymbol_bfd (p), p));
1329 /* Found a linenumber entry, output */
1330 struct internal_lineno out;
1331 memset ((PTR) & out, 0, sizeof (out));
1333 out.l_addr.l_symndx = l->u.offset;
1334 bfd_coff_swap_lineno_out (abfd, &out, buff);
1335 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1338 while (l->line_number)
1340 out.l_lnno = l->line_number;
1341 out.l_addr.l_symndx = l->u.offset;
1342 bfd_coff_swap_lineno_out (abfd, &out, buff);
1343 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1353 bfd_release (abfd, buff);
1359 coff_get_lineno (ignore_abfd, symbol)
1360 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1363 return coffsymbol (symbol)->lineno;
1368 /* This is only called from coff_add_missing_symbols, which has been
1372 coff_section_symbol (abfd, name)
1376 asection *sec = bfd_make_section_old_way (abfd, name);
1378 combined_entry_type *csym;
1381 csym = coff_symbol_from (abfd, sym)->native;
1382 /* Make sure back-end COFF stuff is there. */
1387 coff_symbol_type sym;
1388 /* @@FIXME This shouldn't use a fixed size!! */
1389 combined_entry_type e[10];
1392 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1395 bfd_set_error (bfd_error_no_error);
1398 memset ((char *) f, 0, sizeof (*f));
1399 coff_symbol_from (abfd, sym)->native = csym = f->e;
1401 csym[0].u.syment.n_sclass = C_STAT;
1402 csym[0].u.syment.n_numaux = 1;
1403 /* SF_SET_STATICS (sym); @@ ??? */
1404 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1405 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1406 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1408 if (sec->output_section == NULL)
1410 sec->output_section = sec;
1411 sec->output_offset = 0;
1419 /* This function transforms the offsets into the symbol table into
1420 pointers to syments. */
1423 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1425 combined_entry_type *table_base;
1426 combined_entry_type *symbol;
1427 unsigned int indaux;
1428 combined_entry_type *auxent;
1430 unsigned int type = symbol->u.syment.n_type;
1431 unsigned int class = symbol->u.syment.n_sclass;
1433 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1435 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1436 (abfd, table_base, symbol, indaux, auxent))
1440 /* Don't bother if this is a file or a section */
1441 if (class == C_STAT && type == T_NULL)
1443 if (class == C_FILE)
1446 /* Otherwise patch up */
1447 #define N_TMASK coff_data (abfd)->local_n_tmask
1448 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1449 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1450 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1452 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1453 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1454 auxent->fix_end = 1;
1456 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1457 generate one, so we must be careful to ignore it. */
1458 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1460 auxent->u.auxent.x_sym.x_tagndx.p =
1461 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1462 auxent->fix_tag = 1;
1466 /* Allocate space for the ".debug" section, and read it.
1467 We did not read the debug section until now, because
1468 we didn't want to go to the trouble until someone needed it. */
1471 build_debug_section (abfd)
1474 char *debug_section;
1477 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1481 bfd_set_error (bfd_error_no_debug_section);
1485 debug_section = (PTR) bfd_alloc (abfd,
1486 bfd_get_section_size_before_reloc (sect));
1487 if (debug_section == NULL)
1490 /* Seek to the beginning of the `.debug' section and read it.
1491 Save the current position first; it is needed by our caller.
1492 Then read debug section and reset the file pointer. */
1494 position = bfd_tell (abfd);
1495 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1496 || (bfd_read (debug_section,
1497 bfd_get_section_size_before_reloc (sect), 1, abfd)
1498 != bfd_get_section_size_before_reloc (sect))
1499 || bfd_seek (abfd, position, SEEK_SET) != 0)
1501 return debug_section;
1505 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1506 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1507 be \0-terminated. */
1509 copy_name (abfd, name, maxlen)
1517 for (len = 0; len < maxlen; ++len)
1519 if (name[len] == '\0')
1525 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1527 strncpy (newname, name, len);
1528 newname[len] = '\0';
1532 /* Read in the external symbols. */
1535 _bfd_coff_get_external_symbols (abfd)
1538 bfd_size_type symesz;
1542 if (obj_coff_external_syms (abfd) != NULL)
1545 symesz = bfd_coff_symesz (abfd);
1547 size = obj_raw_syment_count (abfd) * symesz;
1549 syms = (PTR) bfd_malloc (size);
1550 if (syms == NULL && size != 0)
1553 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1554 || bfd_read (syms, size, 1, abfd) != size)
1561 obj_coff_external_syms (abfd) = syms;
1566 /* Read in the external strings. The strings are not loaded until
1567 they are needed. This is because we have no simple way of
1568 detecting a missing string table in an archive. */
1571 _bfd_coff_read_string_table (abfd)
1574 char extstrsize[STRING_SIZE_SIZE];
1578 if (obj_coff_strings (abfd) != NULL)
1579 return obj_coff_strings (abfd);
1581 if (obj_sym_filepos (abfd) == 0)
1583 bfd_set_error (bfd_error_no_symbols);
1588 (obj_sym_filepos (abfd)
1589 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1593 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1595 if (bfd_get_error () != bfd_error_file_truncated)
1598 /* There is no string table. */
1599 strsize = STRING_SIZE_SIZE;
1603 #if STRING_SIZE_SIZE == 4
1604 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1606 #error Change bfd_h_get_32
1610 if (strsize < STRING_SIZE_SIZE)
1612 (*_bfd_error_handler)
1613 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1614 (unsigned long) strsize);
1615 bfd_set_error (bfd_error_bad_value);
1619 strings = (char *) bfd_malloc (strsize);
1620 if (strings == NULL)
1623 if (bfd_read (strings + STRING_SIZE_SIZE,
1624 strsize - STRING_SIZE_SIZE, 1, abfd)
1625 != strsize - STRING_SIZE_SIZE)
1631 obj_coff_strings (abfd) = strings;
1636 /* Free up the external symbols and strings read from a COFF file. */
1639 _bfd_coff_free_symbols (abfd)
1642 if (obj_coff_external_syms (abfd) != NULL
1643 && ! obj_coff_keep_syms (abfd))
1645 free (obj_coff_external_syms (abfd));
1646 obj_coff_external_syms (abfd) = NULL;
1648 if (obj_coff_strings (abfd) != NULL
1649 && ! obj_coff_keep_strings (abfd))
1651 free (obj_coff_strings (abfd));
1652 obj_coff_strings (abfd) = NULL;
1657 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1658 knit the symbol names into a normalized form. By normalized here I
1659 mean that all symbols have an n_offset pointer that points to a null-
1660 terminated string. */
1662 combined_entry_type *
1663 coff_get_normalized_symtab (abfd)
1666 combined_entry_type *internal;
1667 combined_entry_type *internal_ptr;
1668 combined_entry_type *symbol_ptr;
1669 combined_entry_type *internal_end;
1670 bfd_size_type symesz;
1673 const char *string_table = NULL;
1674 char *debug_section = NULL;
1677 if (obj_raw_syments (abfd) != NULL)
1678 return obj_raw_syments (abfd);
1680 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1681 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1682 if (internal == NULL && size != 0)
1684 internal_end = internal + obj_raw_syment_count (abfd);
1686 if (! _bfd_coff_get_external_symbols (abfd))
1689 raw_src = (char *) obj_coff_external_syms (abfd);
1691 /* mark the end of the symbols */
1692 symesz = bfd_coff_symesz (abfd);
1693 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1695 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1696 probably possible. If one shows up, it will probably kill us. */
1698 /* Swap all the raw entries */
1699 for (internal_ptr = internal;
1701 raw_src += symesz, internal_ptr++)
1705 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1706 (PTR) & internal_ptr->u.syment);
1707 symbol_ptr = internal_ptr;
1710 i < symbol_ptr->u.syment.n_numaux;
1715 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1716 symbol_ptr->u.syment.n_type,
1717 symbol_ptr->u.syment.n_sclass,
1718 i, symbol_ptr->u.syment.n_numaux,
1719 &(internal_ptr->u.auxent));
1720 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1725 /* Free the raw symbols, but not the strings (if we have them). */
1726 obj_coff_keep_strings (abfd) = true;
1727 if (! _bfd_coff_free_symbols (abfd))
1730 for (internal_ptr = internal; internal_ptr < internal_end;
1733 if (internal_ptr->u.syment.n_sclass == C_FILE
1734 && internal_ptr->u.syment.n_numaux > 0)
1736 /* make a file symbol point to the name in the auxent, since
1737 the text ".file" is redundant */
1738 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1740 /* the filename is a long one, point into the string table */
1741 if (string_table == NULL)
1743 string_table = _bfd_coff_read_string_table (abfd);
1744 if (string_table == NULL)
1748 internal_ptr->u.syment._n._n_n._n_offset =
1751 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1755 /* Ordinary short filename, put into memory anyway. The
1756 Microsoft PE tools sometimes store a filename in
1757 multiple AUX entries. */
1758 if (internal_ptr->u.syment.n_numaux > 1
1759 && coff_data (abfd)->pe)
1761 internal_ptr->u.syment._n._n_n._n_offset =
1764 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1765 internal_ptr->u.syment.n_numaux * symesz));
1769 internal_ptr->u.syment._n._n_n._n_offset =
1772 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1773 bfd_coff_filnmlen (abfd)));
1779 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1781 /* This is a "short" name. Make it long. */
1782 unsigned long i = 0;
1783 char *newstring = NULL;
1785 /* find the length of this string without walking into memory
1787 for (i = 0; i < 8; ++i)
1789 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1792 } /* if end of string */
1793 } /* possible lengths of this string. */
1795 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1797 memset (newstring, 0, i);
1798 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1799 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1800 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1802 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1803 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1804 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1806 /* Long name already. Point symbol at the string in the
1808 if (string_table == NULL)
1810 string_table = _bfd_coff_read_string_table (abfd);
1811 if (string_table == NULL)
1814 internal_ptr->u.syment._n._n_n._n_offset =
1817 + internal_ptr->u.syment._n._n_n._n_offset));
1821 /* Long name in debug section. Very similar. */
1822 if (debug_section == NULL)
1823 debug_section = build_debug_section (abfd);
1824 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1825 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1828 internal_ptr += internal_ptr->u.syment.n_numaux;
1831 obj_raw_syments (abfd) = internal;
1832 BFD_ASSERT (obj_raw_syment_count (abfd)
1833 == (unsigned int) (internal_ptr - internal));
1836 } /* coff_get_normalized_symtab() */
1839 coff_get_reloc_upper_bound (abfd, asect)
1843 if (bfd_get_format (abfd) != bfd_object)
1845 bfd_set_error (bfd_error_invalid_operation);
1848 return (asect->reloc_count + 1) * sizeof (arelent *);
1852 coff_make_empty_symbol (abfd)
1855 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1858 memset (new, 0, sizeof *new);
1859 new->symbol.section = 0;
1861 new->lineno = (alent *) NULL;
1862 new->done_lineno = false;
1863 new->symbol.the_bfd = abfd;
1864 return &new->symbol;
1867 /* Make a debugging symbol. */
1870 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1872 PTR ptr ATTRIBUTE_UNUSED;
1873 unsigned long sz ATTRIBUTE_UNUSED;
1875 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1878 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1879 (but shouldn't be a constant). */
1880 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1883 new->symbol.section = bfd_abs_section_ptr;
1884 new->symbol.flags = BSF_DEBUGGING;
1885 new->lineno = (alent *) NULL;
1886 new->done_lineno = false;
1887 new->symbol.the_bfd = abfd;
1888 return &new->symbol;
1893 coff_get_symbol_info (abfd, symbol, ret)
1898 bfd_symbol_info (symbol, ret);
1899 if (coffsymbol (symbol)->native != NULL
1900 && coffsymbol (symbol)->native->fix_value)
1902 combined_entry_type *psym;
1904 psym = ((combined_entry_type *)
1905 coffsymbol (symbol)->native->u.syment.n_value);
1906 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1910 /* Return the COFF syment for a symbol. */
1913 bfd_coff_get_syment (abfd, symbol, psyment)
1916 struct internal_syment *psyment;
1918 coff_symbol_type *csym;
1920 csym = coff_symbol_from (abfd, symbol);
1921 if (csym == NULL || csym->native == NULL)
1923 bfd_set_error (bfd_error_invalid_operation);
1927 *psyment = csym->native->u.syment;
1929 if (csym->native->fix_value)
1930 psyment->n_value = ((combined_entry_type *) psyment->n_value
1931 - obj_raw_syments (abfd));
1933 /* FIXME: We should handle fix_line here. */
1938 /* Return the COFF auxent for a symbol. */
1941 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1945 union internal_auxent *pauxent;
1947 coff_symbol_type *csym;
1948 combined_entry_type *ent;
1950 csym = coff_symbol_from (abfd, symbol);
1953 || csym->native == NULL
1954 || indx >= csym->native->u.syment.n_numaux)
1956 bfd_set_error (bfd_error_invalid_operation);
1960 ent = csym->native + indx + 1;
1962 *pauxent = ent->u.auxent;
1965 pauxent->x_sym.x_tagndx.l =
1966 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1967 - obj_raw_syments (abfd));
1970 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1971 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1972 - obj_raw_syments (abfd));
1974 if (ent->fix_scnlen)
1975 pauxent->x_csect.x_scnlen.l =
1976 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1977 - obj_raw_syments (abfd));
1982 /* Print out information about COFF symbol. */
1985 coff_print_symbol (abfd, filep, symbol, how)
1989 bfd_print_symbol_type how;
1991 FILE *file = (FILE *) filep;
1995 case bfd_print_symbol_name:
1996 fprintf (file, "%s", symbol->name);
1999 case bfd_print_symbol_more:
2000 fprintf (file, "coff %s %s",
2001 coffsymbol (symbol)->native ? "n" : "g",
2002 coffsymbol (symbol)->lineno ? "l" : " ");
2005 case bfd_print_symbol_all:
2006 if (coffsymbol (symbol)->native)
2010 combined_entry_type *combined = coffsymbol (symbol)->native;
2011 combined_entry_type *root = obj_raw_syments (abfd);
2012 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2014 fprintf (file, "[%3ld]", (long) (combined - root));
2016 if (! combined->fix_value)
2017 val = (unsigned long) combined->u.syment.n_value;
2019 val = ((unsigned long)
2020 ((combined_entry_type *) combined->u.syment.n_value
2024 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2025 combined->u.syment.n_scnum,
2026 combined->u.syment.n_flags,
2027 combined->u.syment.n_type,
2028 combined->u.syment.n_sclass,
2029 combined->u.syment.n_numaux,
2033 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2035 combined_entry_type *auxp = combined + aux + 1;
2039 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2041 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2043 fprintf (file, "\n");
2045 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2048 switch (combined->u.syment.n_sclass)
2051 fprintf (file, "File ");
2055 if (combined->u.syment.n_type == T_NULL)
2056 /* probably a section symbol? */
2058 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2059 (long) auxp->u.auxent.x_scn.x_scnlen,
2060 auxp->u.auxent.x_scn.x_nreloc,
2061 auxp->u.auxent.x_scn.x_nlinno);
2062 if (auxp->u.auxent.x_scn.x_checksum != 0
2063 || auxp->u.auxent.x_scn.x_associated != 0
2064 || auxp->u.auxent.x_scn.x_comdat != 0)
2065 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2066 auxp->u.auxent.x_scn.x_checksum,
2067 auxp->u.auxent.x_scn.x_associated,
2068 auxp->u.auxent.x_scn.x_comdat);
2071 /* else fall through */
2073 if (ISFCN (combined->u.syment.n_type))
2076 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2078 auxp->u.auxent.x_sym.x_misc.x_fsize,
2079 auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr,
2082 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2084 : auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l));
2087 /* else fall through */
2089 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2090 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2091 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2094 fprintf (file, " endndx %ld",
2096 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2104 fprintf (file, "\n%s :", l->u.sym->name);
2106 while (l->line_number)
2108 fprintf (file, "\n%4d : 0x%lx",
2111 (l->u.offset + symbol->section->vma)));
2118 bfd_print_symbol_vandf ((PTR) file, symbol);
2119 fprintf (file, " %-5s %s %s %s",
2120 symbol->section->name,
2121 coffsymbol (symbol)->native ? "n" : "g",
2122 coffsymbol (symbol)->lineno ? "l" : " ",
2128 /* Return whether a symbol name implies a local symbol. In COFF,
2129 local symbols generally start with ``.L''. Most targets use this
2130 function for the is_local_label_name entry point, but some may
2134 _bfd_coff_is_local_label_name (abfd, name)
2135 bfd *abfd ATTRIBUTE_UNUSED;
2138 return name[0] == '.' && name[1] == 'L';
2141 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2142 section, calculate and return the name of the source file and the line
2143 nearest to the wanted location. */
2146 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2147 functionname_ptr, line_ptr)
2152 CONST char **filename_ptr;
2153 CONST char **functionname_ptr;
2154 unsigned int *line_ptr;
2158 unsigned int line_base;
2159 coff_data_type *cof = coff_data (abfd);
2160 /* Run through the raw syments if available */
2161 combined_entry_type *p;
2162 combined_entry_type *pend;
2164 struct coff_section_tdata *sec_data;
2166 /* Before looking through the symbol table, try to use a .stab
2167 section to find the information. */
2168 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2169 &found, filename_ptr,
2170 functionname_ptr, line_ptr,
2171 &coff_data (abfd)->line_info))
2177 *functionname_ptr = 0;
2180 /* Don't try and find line numbers in a non coff file */
2181 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2187 /* Find the first C_FILE symbol. */
2188 p = cof->raw_syments;
2192 pend = p + cof->raw_syment_count;
2195 if (p->u.syment.n_sclass == C_FILE)
2197 p += 1 + p->u.syment.n_numaux;
2205 /* Look through the C_FILE symbols to find the best one. */
2206 sec_vma = bfd_get_section_vma (abfd, section);
2207 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2208 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2211 combined_entry_type *p2;
2213 for (p2 = p + 1 + p->u.syment.n_numaux;
2215 p2 += 1 + p2->u.syment.n_numaux)
2217 if (p2->u.syment.n_scnum > 0
2219 == coff_section_from_bfd_index (abfd,
2220 p2->u.syment.n_scnum)))
2222 if (p2->u.syment.n_sclass == C_FILE)
2229 /* We use <= MAXDIFF here so that if we get a zero length
2230 file, we actually use the next file entry. */
2232 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2233 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2235 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2236 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2239 /* Avoid endless loops on erroneous files by ensuring that
2240 we always move forward in the file. */
2241 if (p - cof->raw_syments >= p->u.syment.n_value)
2244 p = cof->raw_syments + p->u.syment.n_value;
2245 if (p > pend || p->u.syment.n_sclass != C_FILE)
2250 /* Now wander though the raw linenumbers of the section */
2251 /* If we have been called on this section before, and the offset we
2252 want is further down then we can prime the lookup loop. */
2253 sec_data = coff_section_data (abfd, section);
2254 if (sec_data != NULL
2256 && offset >= sec_data->offset)
2259 *functionname_ptr = sec_data->function;
2260 line_base = sec_data->line_base;
2268 if (section->lineno != NULL)
2270 bfd_vma last_value = 0;
2272 l = §ion->lineno[i];
2274 for (; i < section->lineno_count; i++)
2276 if (l->line_number == 0)
2278 /* Get the symbol this line number points at */
2279 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2280 if (coff->symbol.value > offset)
2282 *functionname_ptr = coff->symbol.name;
2283 last_value = coff->symbol.value;
2286 combined_entry_type *s = coff->native;
2287 s = s + 1 + s->u.syment.n_numaux;
2289 /* In XCOFF a debugging symbol can follow the
2291 if (s->u.syment.n_scnum == N_DEBUG)
2292 s = s + 1 + s->u.syment.n_numaux;
2294 /* S should now point to the .bf of the function. */
2295 if (s->u.syment.n_numaux)
2297 /* The linenumber is stored in the auxent. */
2298 union internal_auxent *a = &((s + 1)->u.auxent);
2299 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2300 *line_ptr = line_base;
2306 if (l->u.offset > offset)
2308 *line_ptr = l->line_number + line_base - 1;
2313 /* If we fell off the end of the loop, then assume that this
2314 symbol has no line number info. Otherwise, symbols with no
2315 line number info get reported with the line number of the
2316 last line of the last symbol which does have line number
2317 info. We use 0x100 as a slop to account for cases where the
2318 last line has executable code. */
2319 if (i >= section->lineno_count
2321 && offset - last_value > 0x100)
2323 *functionname_ptr = NULL;
2328 /* Cache the results for the next call. */
2329 if (sec_data == NULL && section->owner == abfd)
2331 section->used_by_bfd =
2332 ((PTR) bfd_zalloc (abfd,
2333 sizeof (struct coff_section_tdata)));
2334 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2336 if (sec_data != NULL)
2338 sec_data->offset = offset;
2340 sec_data->function = *functionname_ptr;
2341 sec_data->line_base = line_base;
2348 coff_sizeof_headers (abfd, reloc)
2356 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2360 size = bfd_coff_filhsz (abfd);
2363 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2367 /* Change the class of a coff symbol held by BFD. */
2369 bfd_coff_set_symbol_class (abfd, symbol, class)
2374 coff_symbol_type * csym;
2376 csym = coff_symbol_from (abfd, symbol);
2379 bfd_set_error (bfd_error_invalid_operation);
2382 else if (csym->native == NULL)
2384 /* This is an alien symbol which no native coff backend data.
2385 We cheat here by creating a fake native entry for it and
2386 then filling in the class. This code is based on that in
2387 coff_write_alien_symbol(). */
2389 combined_entry_type * native;
2391 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2395 memset (native, 0, sizeof (* native));
2397 native->u.syment.n_type = T_NULL;
2398 native->u.syment.n_sclass = class;
2400 if (bfd_is_und_section (symbol->section))
2402 native->u.syment.n_scnum = N_UNDEF;
2403 native->u.syment.n_value = symbol->value;
2405 else if (bfd_is_com_section (symbol->section))
2407 native->u.syment.n_scnum = N_UNDEF;
2408 native->u.syment.n_value = symbol->value;
2412 native->u.syment.n_scnum =
2413 symbol->section->output_section->target_index;
2414 native->u.syment.n_value = (symbol->value
2415 + symbol->section->output_offset);
2416 if (! obj_pe (abfd))
2417 native->u.syment.n_value += symbol->section->output_section->vma;
2419 /* Copy the any flags from the the file header into the symbol.
2421 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2424 csym->native = native;
2428 csym->native->u.syment.n_sclass = class;