1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
24 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
26 /* This file contains COFF code that is not dependent on any
27 particular COFF target. There is only one version of this file in
28 libbfd.a, so no target specific code may be put in here. Or, to
31 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33 If you need to add some target specific behaviour, add a new hook
34 function to bfd_coff_backend_data.
36 Some of these functions are also called by the ECOFF routines.
37 Those functions may not use any COFF specific information, such as
43 #include "coff/internal.h"
46 static void coff_fix_symbol_name
47 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
48 asection **, bfd_size_type *));
49 static boolean coff_write_symbol
50 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_vma *,
51 bfd_size_type *, asection **, bfd_size_type *));
52 static boolean coff_write_alien_symbol
53 PARAMS ((bfd *, asymbol *, bfd_vma *, bfd_size_type *,
54 asection **, bfd_size_type *));
55 static boolean coff_write_native_symbol
56 PARAMS ((bfd *, coff_symbol_type *, bfd_vma *, bfd_size_type *,
57 asection **, bfd_size_type *));
58 static void coff_pointerize_aux
59 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
60 unsigned int, combined_entry_type *));
61 static boolean make_a_section_from_file
62 PARAMS ((bfd *, struct internal_scnhdr *, unsigned int));
63 static const bfd_target *coff_real_object_p
64 PARAMS ((bfd *, unsigned, struct internal_filehdr *,
65 struct internal_aouthdr *));
66 static void fixup_symbol_value
67 PARAMS ((bfd *, coff_symbol_type *, struct internal_syment *));
68 static char *build_debug_section
70 static char *copy_name
71 PARAMS ((bfd *, char *, size_t));
73 #define STRING_SIZE_SIZE (4)
75 /* Take a section header read from a coff file (in HOST byte order),
76 and make a BFD "section" out of it. This is used by ECOFF. */
78 make_a_section_from_file (abfd, hdr, target_index)
80 struct internal_scnhdr *hdr;
81 unsigned int target_index;
83 asection *return_section;
85 boolean result = true;
90 /* Handle long section names as in PE. */
91 if (bfd_coff_long_section_names (abfd)
92 && hdr->s_name[0] == '/')
99 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
100 buf[SCNNMLEN - 1] = '\0';
101 strindex = strtol (buf, &p, 10);
102 if (*p == '\0' && strindex >= 0)
104 strings = _bfd_coff_read_string_table (abfd);
107 /* FIXME: For extra safety, we should make sure that
108 strindex does not run us past the end, but right now we
109 don't know the length of the string table. */
111 name = bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
114 strcpy (name, strings);
120 /* Assorted wastage to null-terminate the name, thanks AT&T! */
121 name = bfd_alloc (abfd, (bfd_size_type) sizeof (hdr->s_name) + 1);
124 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
125 name[sizeof (hdr->s_name)] = 0;
128 return_section = bfd_make_section_anyway (abfd, name);
129 if (return_section == NULL)
132 return_section->vma = hdr->s_vaddr;
133 return_section->lma = hdr->s_paddr;
134 return_section->_raw_size = hdr->s_size;
135 return_section->filepos = hdr->s_scnptr;
136 return_section->rel_filepos = hdr->s_relptr;
137 return_section->reloc_count = hdr->s_nreloc;
139 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
141 return_section->line_filepos = hdr->s_lnnoptr;
143 return_section->lineno_count = hdr->s_nlnno;
144 return_section->userdata = NULL;
145 return_section->next = (asection *) NULL;
146 return_section->target_index = target_index;
148 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
152 return_section->flags = flags;
154 /* At least on i386-coff, the line number count for a shared library
155 section must be ignored. */
156 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
157 return_section->lineno_count = 0;
159 if (hdr->s_nreloc != 0)
160 return_section->flags |= SEC_RELOC;
161 /* FIXME: should this check 'hdr->s_size > 0' */
162 if (hdr->s_scnptr != 0)
163 return_section->flags |= SEC_HAS_CONTENTS;
168 /* Read in a COFF object and make it into a BFD. This is used by
171 static const bfd_target *
172 coff_real_object_p (abfd, nscns, internal_f, internal_a)
175 struct internal_filehdr *internal_f;
176 struct internal_aouthdr *internal_a;
178 flagword oflags = abfd->flags;
179 bfd_vma ostart = bfd_get_start_address (abfd);
182 bfd_size_type readsize; /* length of file_info */
184 char *external_sections;
186 if (!(internal_f->f_flags & F_RELFLG))
187 abfd->flags |= HAS_RELOC;
188 if ((internal_f->f_flags & F_EXEC))
189 abfd->flags |= EXEC_P;
190 if (!(internal_f->f_flags & F_LNNO))
191 abfd->flags |= HAS_LINENO;
192 if (!(internal_f->f_flags & F_LSYMS))
193 abfd->flags |= HAS_LOCALS;
195 /* FIXME: How can we set D_PAGED correctly? */
196 if ((internal_f->f_flags & F_EXEC) != 0)
197 abfd->flags |= D_PAGED;
199 bfd_get_symcount (abfd) = internal_f->f_nsyms;
200 if (internal_f->f_nsyms)
201 abfd->flags |= HAS_SYMS;
203 if (internal_a != (struct internal_aouthdr *) NULL)
204 bfd_get_start_address (abfd) = internal_a->entry;
206 bfd_get_start_address (abfd) = 0;
208 /* Set up the tdata area. ECOFF uses its own routine, and overrides
210 tdata_save = abfd->tdata.any;
211 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
215 scnhsz = bfd_coff_scnhsz (abfd);
216 readsize = (bfd_size_type) nscns * scnhsz;
217 external_sections = (char *) bfd_alloc (abfd, readsize);
218 if (!external_sections)
221 if (bfd_bread ((PTR) external_sections, readsize, abfd) != readsize)
224 /* Set the arch/mach *before* swapping in sections; section header swapping
225 may depend on arch/mach info. */
226 if (! bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f))
229 /* Now copy data as required; construct all asections etc */
233 for (i = 0; i < nscns; i++)
235 struct internal_scnhdr tmp;
236 bfd_coff_swap_scnhdr_in (abfd,
237 (PTR) (external_sections + i * scnhsz),
239 if (! make_a_section_from_file (abfd, &tmp, i + 1))
244 /* make_abs_section (abfd); */
249 bfd_release (abfd, tdata);
251 abfd->tdata.any = tdata_save;
252 abfd->flags = oflags;
253 bfd_get_start_address (abfd) = ostart;
254 return (const bfd_target *) NULL;
257 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
258 not a COFF file. This is also used by ECOFF. */
264 bfd_size_type filhsz;
265 bfd_size_type aoutsz;
268 struct internal_filehdr internal_f;
269 struct internal_aouthdr internal_a;
271 /* figure out how much to read */
272 filhsz = bfd_coff_filhsz (abfd);
273 aoutsz = bfd_coff_aoutsz (abfd);
275 filehdr = bfd_alloc (abfd, filhsz);
278 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
280 if (bfd_get_error () != bfd_error_system_call)
281 bfd_set_error (bfd_error_wrong_format);
282 bfd_release (abfd, filehdr);
285 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
286 bfd_release (abfd, filehdr);
288 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
289 (less than aoutsz) used in object files and AOUTSZ (equal to
290 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
291 expects this header to be aoutsz bytes in length, so we use that
292 value in the call to bfd_alloc below. But we must be careful to
293 only read in f_opthdr bytes in the call to bfd_bread. We should
294 also attempt to catch corrupt or non-COFF binaries with a strange
295 value for f_opthdr. */
296 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
297 || internal_f.f_opthdr > aoutsz)
299 bfd_set_error (bfd_error_wrong_format);
302 nscns = internal_f.f_nscns;
304 if (internal_f.f_opthdr)
308 opthdr = bfd_alloc (abfd, aoutsz);
311 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
312 != internal_f.f_opthdr)
314 bfd_release (abfd, opthdr);
317 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
318 bfd_release (abfd, opthdr);
321 return coff_real_object_p (abfd, nscns, &internal_f,
322 (internal_f.f_opthdr != 0
324 : (struct internal_aouthdr *) NULL));
327 /* Get the BFD section from a COFF symbol section number. */
330 coff_section_from_bfd_index (abfd, index)
334 struct sec *answer = abfd->sections;
337 return bfd_abs_section_ptr;
338 if (index == N_UNDEF)
339 return bfd_und_section_ptr;
340 if (index == N_DEBUG)
341 return bfd_abs_section_ptr;
345 if (answer->target_index == index)
347 answer = answer->next;
350 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
351 has a bad symbol table in biglitpow.o. */
352 return bfd_und_section_ptr;
355 /* Get the upper bound of a COFF symbol table. */
358 coff_get_symtab_upper_bound (abfd)
361 if (!bfd_coff_slurp_symbol_table (abfd))
364 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
367 /* Canonicalize a COFF symbol table. */
370 coff_get_symtab (abfd, alocation)
374 unsigned int counter;
375 coff_symbol_type *symbase;
376 coff_symbol_type **location = (coff_symbol_type **) alocation;
378 if (!bfd_coff_slurp_symbol_table (abfd))
381 symbase = obj_symbols (abfd);
382 counter = bfd_get_symcount (abfd);
383 while (counter-- > 0)
384 *location++ = symbase++;
388 return bfd_get_symcount (abfd);
391 /* Get the name of a symbol. The caller must pass in a buffer of size
395 _bfd_coff_internal_syment_name (abfd, sym, buf)
397 const struct internal_syment *sym;
400 /* FIXME: It's not clear this will work correctly if sizeof
402 if (sym->_n._n_n._n_zeroes != 0
403 || sym->_n._n_n._n_offset == 0)
405 memcpy (buf, sym->_n._n_name, SYMNMLEN);
406 buf[SYMNMLEN] = '\0';
413 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
414 strings = obj_coff_strings (abfd);
417 strings = _bfd_coff_read_string_table (abfd);
421 return strings + sym->_n._n_n._n_offset;
425 /* Read in and swap the relocs. This returns a buffer holding the
426 relocs for section SEC in file ABFD. If CACHE is true and
427 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
428 the function is called again. If EXTERNAL_RELOCS is not NULL, it
429 is a buffer large enough to hold the unswapped relocs. If
430 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
431 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
432 value must be INTERNAL_RELOCS. The function returns NULL on error. */
434 struct internal_reloc *
435 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
436 require_internal, internal_relocs)
440 bfd_byte *external_relocs;
441 boolean require_internal;
442 struct internal_reloc *internal_relocs;
445 bfd_byte *free_external = NULL;
446 struct internal_reloc *free_internal = NULL;
449 struct internal_reloc *irel;
452 if (coff_section_data (abfd, sec) != NULL
453 && coff_section_data (abfd, sec)->relocs != NULL)
455 if (! require_internal)
456 return coff_section_data (abfd, sec)->relocs;
457 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
458 sec->reloc_count * sizeof (struct internal_reloc));
459 return internal_relocs;
462 relsz = bfd_coff_relsz (abfd);
464 amt = sec->reloc_count * relsz;
465 if (external_relocs == NULL)
467 free_external = (bfd_byte *) bfd_malloc (amt);
468 if (free_external == NULL && sec->reloc_count > 0)
470 external_relocs = free_external;
473 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
474 || bfd_bread (external_relocs, amt, abfd) != amt)
477 if (internal_relocs == NULL)
479 amt = sec->reloc_count;
480 amt *= sizeof (struct internal_reloc);
481 free_internal = (struct internal_reloc *) bfd_malloc (amt);
482 if (free_internal == NULL && sec->reloc_count > 0)
484 internal_relocs = free_internal;
487 /* Swap in the relocs. */
488 erel = external_relocs;
489 erel_end = erel + relsz * sec->reloc_count;
490 irel = internal_relocs;
491 for (; erel < erel_end; erel += relsz, irel++)
492 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
494 if (free_external != NULL)
496 free (free_external);
497 free_external = NULL;
500 if (cache && free_internal != NULL)
502 if (coff_section_data (abfd, sec) == NULL)
504 amt = sizeof (struct coff_section_tdata);
505 sec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
506 if (sec->used_by_bfd == NULL)
508 coff_section_data (abfd, sec)->contents = NULL;
510 coff_section_data (abfd, sec)->relocs = free_internal;
513 return internal_relocs;
516 if (free_external != NULL)
517 free (free_external);
518 if (free_internal != NULL)
519 free (free_internal);
523 /* Set lineno_count for the output sections of a COFF file. */
526 coff_count_linenumbers (abfd)
529 unsigned int limit = bfd_get_symcount (abfd);
537 /* This may be from the backend linker, in which case the
538 lineno_count in the sections is correct. */
539 for (s = abfd->sections; s != NULL; s = s->next)
540 total += s->lineno_count;
544 for (s = abfd->sections; s != NULL; s = s->next)
545 BFD_ASSERT (s->lineno_count == 0);
547 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
549 asymbol *q_maybe = *p;
551 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
553 coff_symbol_type *q = coffsymbol (q_maybe);
555 /* The AIX 4.1 compiler can sometimes generate line numbers
556 attached to debugging symbols. We try to simply ignore
558 if (q->lineno != NULL
559 && q->symbol.section->owner != NULL)
561 /* This symbol has line numbers. Increment the owning
562 section's linenumber count. */
563 alent *l = q->lineno;
567 asection * sec = q->symbol.section->output_section;
569 /* Do not try to update fields in read-only sections. */
570 if (! bfd_is_const_section (sec))
571 sec->lineno_count ++;
576 while (l->line_number != 0);
584 /* Takes a bfd and a symbol, returns a pointer to the coff specific
585 area of the symbol if there is one. */
588 coff_symbol_from (ignore_abfd, symbol)
589 bfd *ignore_abfd ATTRIBUTE_UNUSED;
592 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
593 return (coff_symbol_type *) NULL;
595 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
596 return (coff_symbol_type *) NULL;
598 return (coff_symbol_type *) symbol;
602 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
604 coff_symbol_type *coff_symbol_ptr;
605 struct internal_syment *syment;
608 /* Normalize the symbol flags */
609 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
611 /* a common symbol is undefined with a value */
612 syment->n_scnum = N_UNDEF;
613 syment->n_value = coff_symbol_ptr->symbol.value;
615 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
616 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
618 syment->n_value = coff_symbol_ptr->symbol.value;
620 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
622 syment->n_scnum = N_UNDEF;
625 /* FIXME: Do we need to handle the absolute section here? */
628 if (coff_symbol_ptr->symbol.section)
631 coff_symbol_ptr->symbol.section->output_section->target_index;
633 syment->n_value = (coff_symbol_ptr->symbol.value
634 + coff_symbol_ptr->symbol.section->output_offset);
637 syment->n_value += (syment->n_sclass == C_STATLAB)
638 ? coff_symbol_ptr->symbol.section->output_section->lma
639 : coff_symbol_ptr->symbol.section->output_section->vma;
645 /* This can happen, but I don't know why yet (steve@cygnus.com) */
646 syment->n_scnum = N_ABS;
647 syment->n_value = coff_symbol_ptr->symbol.value;
652 /* Run through all the symbols in the symbol table and work out what
653 their indexes into the symbol table will be when output.
655 Coff requires that each C_FILE symbol points to the next one in the
656 chain, and that the last one points to the first external symbol. We
660 coff_renumber_symbols (bfd_ptr, first_undef)
664 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
665 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
666 unsigned int native_index = 0;
667 struct internal_syment *last_file = (struct internal_syment *) NULL;
668 unsigned int symbol_index;
670 /* COFF demands that undefined symbols come after all other symbols.
671 Since we don't need to impose this extra knowledge on all our
672 client programs, deal with that here. Sort the symbol table;
673 just move the undefined symbols to the end, leaving the rest
674 alone. The O'Reilly book says that defined global symbols come
675 at the end before the undefined symbols, so we do that here as
677 /* @@ Do we have some condition we could test for, so we don't always
678 have to do this? I don't think relocatability is quite right, but
679 I'm not certain. [raeburn:19920508.1711EST] */
685 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
686 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
689 bfd_ptr->outsymbols = newsyms;
690 for (i = 0; i < symbol_count; i++)
691 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
692 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
693 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
694 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
695 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
697 *newsyms++ = symbol_ptr_ptr[i];
699 for (i = 0; i < symbol_count; i++)
700 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
701 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
702 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
703 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
704 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
706 *newsyms++ = symbol_ptr_ptr[i];
708 *first_undef = newsyms - bfd_ptr->outsymbols;
710 for (i = 0; i < symbol_count; i++)
711 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
712 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
713 *newsyms++ = symbol_ptr_ptr[i];
714 *newsyms = (asymbol *) NULL;
715 symbol_ptr_ptr = bfd_ptr->outsymbols;
718 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
720 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
721 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
722 if (coff_symbol_ptr && coff_symbol_ptr->native)
724 combined_entry_type *s = coff_symbol_ptr->native;
727 if (s->u.syment.n_sclass == C_FILE)
729 if (last_file != (struct internal_syment *) NULL)
730 last_file->n_value = native_index;
731 last_file = &(s->u.syment);
736 /* Modify the symbol values according to their section and
739 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
741 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
742 s[i].offset = native_index++;
749 obj_conv_table_size (bfd_ptr) = native_index;
754 /* Run thorough the symbol table again, and fix it so that all
755 pointers to entries are changed to the entries' index in the output
759 coff_mangle_symbols (bfd_ptr)
762 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
763 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
764 unsigned int symbol_index;
766 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
768 coff_symbol_type *coff_symbol_ptr =
769 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
771 if (coff_symbol_ptr && coff_symbol_ptr->native)
774 combined_entry_type *s = coff_symbol_ptr->native;
778 /* FIXME: We should use a union here. */
779 s->u.syment.n_value =
780 (bfd_vma)((combined_entry_type *)
781 ((unsigned long) s->u.syment.n_value))->offset;
786 /* The value is the offset into the line number entries
787 for the symbol's section. On output, the symbol's
788 section should be N_DEBUG. */
789 s->u.syment.n_value =
790 (coff_symbol_ptr->symbol.section->output_section->line_filepos
791 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
792 coff_symbol_ptr->symbol.section =
793 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
794 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
796 for (i = 0; i < s->u.syment.n_numaux; i++)
798 combined_entry_type *a = s + i + 1;
801 a->u.auxent.x_sym.x_tagndx.l =
802 a->u.auxent.x_sym.x_tagndx.p->offset;
807 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
808 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
813 a->u.auxent.x_csect.x_scnlen.l =
814 a->u.auxent.x_csect.x_scnlen.p->offset;
823 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
824 debug_string_section_p, debug_string_size_p)
827 combined_entry_type *native;
828 bfd_size_type *string_size_p;
829 asection **debug_string_section_p;
830 bfd_size_type *debug_string_size_p;
832 unsigned int name_length;
833 union internal_auxent *auxent;
834 char *name = (char *) (symbol->name);
836 if (name == (char *) NULL)
838 /* coff symbols always have names, so we'll make one up */
839 symbol->name = "strange";
840 name = (char *) symbol->name;
842 name_length = strlen (name);
844 if (native->u.syment.n_sclass == C_FILE
845 && native->u.syment.n_numaux > 0)
847 unsigned int filnmlen;
849 if (bfd_coff_force_symnames_in_strings (abfd))
851 native->u.syment._n._n_n._n_offset =
852 (*string_size_p + STRING_SIZE_SIZE);
853 native->u.syment._n._n_n._n_zeroes = 0;
854 *string_size_p += 6; /* strlen(".file") + 1 */
857 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
859 auxent = &(native + 1)->u.auxent;
861 filnmlen = bfd_coff_filnmlen (abfd);
863 if (bfd_coff_long_filenames (abfd))
865 if (name_length <= filnmlen)
867 strncpy (auxent->x_file.x_fname, name, filnmlen);
871 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
872 auxent->x_file.x_n.x_zeroes = 0;
873 *string_size_p += name_length + 1;
878 strncpy (auxent->x_file.x_fname, name, filnmlen);
879 if (name_length > filnmlen)
880 name[filnmlen] = '\0';
885 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
887 /* This name will fit into the symbol neatly */
888 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
890 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
892 native->u.syment._n._n_n._n_offset = (*string_size_p
894 native->u.syment._n._n_n._n_zeroes = 0;
895 *string_size_p += name_length + 1;
901 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
903 /* This name should be written into the .debug section. For
904 some reason each name is preceded by a two byte length
905 and also followed by a null byte. FIXME: We assume that
906 the .debug section has already been created, and that it
908 if (*debug_string_section_p == (asection *) NULL)
909 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
910 filepos = bfd_tell (abfd);
912 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
914 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
916 if (!bfd_set_section_contents (abfd,
917 *debug_string_section_p,
919 (file_ptr) *debug_string_size_p,
920 (bfd_size_type) prefix_len)
921 || !bfd_set_section_contents (abfd,
922 *debug_string_section_p,
924 (file_ptr) (*debug_string_size_p
926 (bfd_size_type) name_length + 1))
928 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
930 native->u.syment._n._n_n._n_offset =
931 *debug_string_size_p + prefix_len;
932 native->u.syment._n._n_n._n_zeroes = 0;
933 *debug_string_size_p += name_length + 1 + prefix_len;
938 /* We need to keep track of the symbol index so that when we write out
939 the relocs we can get the index for a symbol. This method is a
942 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
944 /* Write a symbol out to a COFF file. */
947 coff_write_symbol (abfd, symbol, native, written, string_size_p,
948 debug_string_section_p, debug_string_size_p)
951 combined_entry_type *native;
953 bfd_size_type *string_size_p;
954 asection **debug_string_section_p;
955 bfd_size_type *debug_string_size_p;
957 unsigned int numaux = native->u.syment.n_numaux;
958 int type = native->u.syment.n_type;
959 int class = native->u.syment.n_sclass;
961 bfd_size_type symesz;
963 if (native->u.syment.n_sclass == C_FILE)
964 symbol->flags |= BSF_DEBUGGING;
966 if (symbol->flags & BSF_DEBUGGING
967 && bfd_is_abs_section (symbol->section))
969 native->u.syment.n_scnum = N_DEBUG;
971 else if (bfd_is_abs_section (symbol->section))
973 native->u.syment.n_scnum = N_ABS;
975 else if (bfd_is_und_section (symbol->section))
977 native->u.syment.n_scnum = N_UNDEF;
981 native->u.syment.n_scnum =
982 symbol->section->output_section->target_index;
985 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
986 debug_string_section_p, debug_string_size_p);
988 symesz = bfd_coff_symesz (abfd);
989 buf = bfd_alloc (abfd, symesz);
992 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
993 if (bfd_bwrite (buf, symesz, abfd) != symesz)
995 bfd_release (abfd, buf);
997 if (native->u.syment.n_numaux > 0)
999 bfd_size_type auxesz;
1002 auxesz = bfd_coff_auxesz (abfd);
1003 buf = bfd_alloc (abfd, auxesz);
1006 for (j = 0; j < native->u.syment.n_numaux; j++)
1008 bfd_coff_swap_aux_out (abfd,
1009 &((native + j + 1)->u.auxent),
1013 native->u.syment.n_numaux,
1015 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1018 bfd_release (abfd, buf);
1021 /* Store the index for use when we write out the relocs. */
1022 set_index (symbol, *written);
1024 *written += numaux + 1;
1028 /* Write out a symbol to a COFF file that does not come from a COFF
1029 file originally. This symbol may have been created by the linker,
1030 or we may be linking a non COFF file to a COFF file. */
1033 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
1034 debug_string_section_p, debug_string_size_p)
1038 bfd_size_type *string_size_p;
1039 asection **debug_string_section_p;
1040 bfd_size_type *debug_string_size_p;
1042 combined_entry_type *native;
1043 combined_entry_type dummy;
1046 native->u.syment.n_type = T_NULL;
1047 native->u.syment.n_flags = 0;
1048 if (bfd_is_und_section (symbol->section))
1050 native->u.syment.n_scnum = N_UNDEF;
1051 native->u.syment.n_value = symbol->value;
1053 else if (bfd_is_com_section (symbol->section))
1055 native->u.syment.n_scnum = N_UNDEF;
1056 native->u.syment.n_value = symbol->value;
1058 else if (symbol->flags & BSF_DEBUGGING)
1060 /* There isn't much point to writing out a debugging symbol
1061 unless we are prepared to convert it into COFF debugging
1062 format. So, we just ignore them. We must clobber the symbol
1063 name to keep it from being put in the string table. */
1069 native->u.syment.n_scnum =
1070 symbol->section->output_section->target_index;
1071 native->u.syment.n_value = (symbol->value
1072 + symbol->section->output_offset);
1073 if (! obj_pe (abfd))
1074 native->u.syment.n_value += symbol->section->output_section->vma;
1076 /* Copy the any flags from the file header into the symbol.
1079 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1080 if (c != (coff_symbol_type *) NULL)
1081 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1085 native->u.syment.n_type = 0;
1086 if (symbol->flags & BSF_LOCAL)
1087 native->u.syment.n_sclass = C_STAT;
1088 else if (symbol->flags & BSF_WEAK)
1089 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1091 native->u.syment.n_sclass = C_EXT;
1092 native->u.syment.n_numaux = 0;
1094 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1095 debug_string_section_p, debug_string_size_p);
1098 /* Write a native symbol to a COFF file. */
1101 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1102 debug_string_section_p, debug_string_size_p)
1104 coff_symbol_type *symbol;
1106 bfd_size_type *string_size_p;
1107 asection **debug_string_section_p;
1108 bfd_size_type *debug_string_size_p;
1110 combined_entry_type *native = symbol->native;
1111 alent *lineno = symbol->lineno;
1113 /* If this symbol has an associated line number, we must store the
1114 symbol index in the line number field. We also tag the auxent to
1115 point to the right place in the lineno table. */
1116 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1118 unsigned int count = 0;
1119 lineno[count].u.offset = *written;
1120 if (native->u.syment.n_numaux)
1122 union internal_auxent *a = &((native + 1)->u.auxent);
1124 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1125 symbol->symbol.section->output_section->moving_line_filepos;
1128 /* Count and relocate all other linenumbers. */
1130 while (lineno[count].line_number != 0)
1134 I've been told this, but still need proof:
1135 > The second bug is also in `bfd/coffcode.h'. This bug
1136 > causes the linker to screw up the pc-relocations for
1137 > all the line numbers in COFF code. This bug isn't only
1138 > specific to A29K implementations, but affects all
1139 > systems using COFF format binaries. Note that in COFF
1140 > object files, the line number core offsets output by
1141 > the assembler are relative to the start of each
1142 > procedure, not to the start of the .text section. This
1143 > patch relocates the line numbers relative to the
1144 > `native->u.syment.n_value' instead of the section
1146 > modular!olson@cs.arizona.edu (Jon Olson)
1148 lineno[count].u.offset += native->u.syment.n_value;
1150 lineno[count].u.offset +=
1151 (symbol->symbol.section->output_section->vma
1152 + symbol->symbol.section->output_offset);
1156 symbol->done_lineno = true;
1158 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1159 symbol->symbol.section->output_section->moving_line_filepos +=
1160 count * bfd_coff_linesz (abfd);
1163 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1164 string_size_p, debug_string_section_p,
1165 debug_string_size_p);
1168 /* Write out the COFF symbols. */
1171 coff_write_symbols (abfd)
1174 bfd_size_type string_size;
1175 asection *debug_string_section;
1176 bfd_size_type debug_string_size;
1178 unsigned int limit = bfd_get_symcount (abfd);
1179 bfd_signed_vma written = 0;
1183 debug_string_section = NULL;
1184 debug_string_size = 0;
1186 /* If this target supports long section names, they must be put into
1187 the string table. This is supported by PE. This code must
1188 handle section names just as they are handled in
1189 coff_write_object_contents. */
1190 if (bfd_coff_long_section_names (abfd))
1194 for (o = abfd->sections; o != NULL; o = o->next)
1198 len = strlen (o->name);
1200 string_size += len + 1;
1204 /* Seek to the right place */
1205 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1208 /* Output all the symbols we have */
1211 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1213 asymbol *symbol = *p;
1214 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1216 if (c_symbol == (coff_symbol_type *) NULL
1217 || c_symbol->native == (combined_entry_type *) NULL)
1219 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1220 &debug_string_section,
1221 &debug_string_size))
1226 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1227 &string_size, &debug_string_section,
1228 &debug_string_size))
1233 obj_raw_syment_count (abfd) = written;
1235 /* Now write out strings */
1237 if (string_size != 0)
1239 unsigned int size = string_size + STRING_SIZE_SIZE;
1240 bfd_byte buffer[STRING_SIZE_SIZE];
1242 #if STRING_SIZE_SIZE == 4
1243 H_PUT_32 (abfd, size, buffer);
1245 #error Change H_PUT_32
1247 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) sizeof (buffer), abfd)
1251 /* Handle long section names. This code must handle section
1252 names just as they are handled in coff_write_object_contents. */
1253 if (bfd_coff_long_section_names (abfd))
1257 for (o = abfd->sections; o != NULL; o = o->next)
1261 len = strlen (o->name);
1264 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1271 for (p = abfd->outsymbols, i = 0;
1276 size_t name_length = strlen (q->name);
1277 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1280 /* Figure out whether the symbol name should go in the string
1281 table. Symbol names that are short enough are stored
1282 directly in the syment structure. File names permit a
1283 different, longer, length in the syment structure. On
1284 XCOFF, some symbol names are stored in the .debug section
1285 rather than in the string table. */
1287 if (c_symbol == NULL
1288 || c_symbol->native == NULL)
1290 /* This is not a COFF symbol, so it certainly is not a
1291 file name, nor does it go in the .debug section. */
1292 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1294 else if (bfd_coff_symname_in_debug (abfd,
1295 &c_symbol->native->u.syment))
1297 /* This symbol name is in the XCOFF .debug section.
1298 Don't write it into the string table. */
1299 maxlen = name_length;
1301 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1302 && c_symbol->native->u.syment.n_numaux > 0)
1304 if (bfd_coff_force_symnames_in_strings (abfd))
1306 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1309 maxlen = bfd_coff_filnmlen (abfd);
1312 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1314 if (name_length > maxlen)
1316 if (bfd_bwrite ((PTR) (q->name), (bfd_size_type) name_length + 1,
1317 abfd) != name_length + 1)
1324 /* We would normally not write anything here, but we'll write
1325 out 4 so that any stupid coff reader which tries to read the
1326 string table even when there isn't one won't croak. */
1327 unsigned int size = STRING_SIZE_SIZE;
1328 bfd_byte buffer[STRING_SIZE_SIZE];
1330 #if STRING_SIZE_SIZE == 4
1331 H_PUT_32 (abfd, size, buffer);
1333 #error Change H_PUT_32
1335 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1336 != STRING_SIZE_SIZE)
1340 /* Make sure the .debug section was created to be the correct size.
1341 We should create it ourselves on the fly, but we don't because
1342 BFD won't let us write to any section until we know how large all
1343 the sections are. We could still do it by making another pass
1344 over the symbols. FIXME. */
1345 BFD_ASSERT (debug_string_size == 0
1346 || (debug_string_section != (asection *) NULL
1347 && (BFD_ALIGN (debug_string_size,
1348 1 << debug_string_section->alignment_power)
1349 == bfd_section_size (abfd, debug_string_section))));
1355 coff_write_linenumbers (abfd)
1359 bfd_size_type linesz;
1362 linesz = bfd_coff_linesz (abfd);
1363 buff = bfd_alloc (abfd, linesz);
1366 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1368 if (s->lineno_count)
1370 asymbol **q = abfd->outsymbols;
1371 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1373 /* Find all the linenumbers in this section */
1377 if (p->section->output_section == s)
1380 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1381 (bfd_asymbol_bfd (p), p));
1384 /* Found a linenumber entry, output */
1385 struct internal_lineno out;
1386 memset ((PTR) & out, 0, sizeof (out));
1388 out.l_addr.l_symndx = l->u.offset;
1389 bfd_coff_swap_lineno_out (abfd, &out, buff);
1390 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1394 while (l->line_number)
1396 out.l_lnno = l->line_number;
1397 out.l_addr.l_symndx = l->u.offset;
1398 bfd_coff_swap_lineno_out (abfd, &out, buff);
1399 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1410 bfd_release (abfd, buff);
1415 coff_get_lineno (ignore_abfd, symbol)
1416 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1419 return coffsymbol (symbol)->lineno;
1424 /* This is only called from coff_add_missing_symbols, which has been
1428 coff_section_symbol (abfd, name)
1432 asection *sec = bfd_make_section_old_way (abfd, name);
1434 combined_entry_type *csym;
1437 csym = coff_symbol_from (abfd, sym)->native;
1438 /* Make sure back-end COFF stuff is there. */
1443 coff_symbol_type sym;
1444 /* @@FIXME This shouldn't use a fixed size!! */
1445 combined_entry_type e[10];
1449 f = (struct foo *) bfd_zalloc (abfd, (bfd_size_type) sizeof (*f));
1452 bfd_set_error (bfd_error_no_error);
1455 coff_symbol_from (abfd, sym)->native = csym = f->e;
1457 csym[0].u.syment.n_sclass = C_STAT;
1458 csym[0].u.syment.n_numaux = 1;
1459 /* SF_SET_STATICS (sym); @@ ??? */
1460 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1461 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1462 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1464 if (sec->output_section == NULL)
1466 sec->output_section = sec;
1467 sec->output_offset = 0;
1475 /* This function transforms the offsets into the symbol table into
1476 pointers to syments. */
1479 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1481 combined_entry_type *table_base;
1482 combined_entry_type *symbol;
1483 unsigned int indaux;
1484 combined_entry_type *auxent;
1486 unsigned int type = symbol->u.syment.n_type;
1487 unsigned int class = symbol->u.syment.n_sclass;
1489 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1491 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1492 (abfd, table_base, symbol, indaux, auxent))
1496 /* Don't bother if this is a file or a section */
1497 if (class == C_STAT && type == T_NULL)
1499 if (class == C_FILE)
1502 /* Otherwise patch up */
1503 #define N_TMASK coff_data (abfd)->local_n_tmask
1504 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1505 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1506 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1508 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1509 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1510 auxent->fix_end = 1;
1512 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1513 generate one, so we must be careful to ignore it. */
1514 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1516 auxent->u.auxent.x_sym.x_tagndx.p =
1517 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1518 auxent->fix_tag = 1;
1522 /* Allocate space for the ".debug" section, and read it.
1523 We did not read the debug section until now, because
1524 we didn't want to go to the trouble until someone needed it. */
1527 build_debug_section (abfd)
1530 char *debug_section;
1532 bfd_size_type sec_size;
1534 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1538 bfd_set_error (bfd_error_no_debug_section);
1542 sec_size = bfd_get_section_size_before_reloc (sect);
1543 debug_section = (PTR) bfd_alloc (abfd, sec_size);
1544 if (debug_section == NULL)
1547 /* Seek to the beginning of the `.debug' section and read it.
1548 Save the current position first; it is needed by our caller.
1549 Then read debug section and reset the file pointer. */
1551 position = bfd_tell (abfd);
1552 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1553 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1554 || bfd_seek (abfd, position, SEEK_SET) != 0)
1556 return debug_section;
1559 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1560 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1561 be \0-terminated. */
1563 copy_name (abfd, name, maxlen)
1571 for (len = 0; len < maxlen; ++len)
1573 if (name[len] == '\0')
1579 if ((newname = (PTR) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1581 strncpy (newname, name, len);
1582 newname[len] = '\0';
1586 /* Read in the external symbols. */
1589 _bfd_coff_get_external_symbols (abfd)
1592 bfd_size_type symesz;
1596 if (obj_coff_external_syms (abfd) != NULL)
1599 symesz = bfd_coff_symesz (abfd);
1601 size = obj_raw_syment_count (abfd) * symesz;
1603 syms = (PTR) bfd_malloc (size);
1604 if (syms == NULL && size != 0)
1607 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1608 || bfd_bread (syms, size, abfd) != size)
1615 obj_coff_external_syms (abfd) = syms;
1620 /* Read in the external strings. The strings are not loaded until
1621 they are needed. This is because we have no simple way of
1622 detecting a missing string table in an archive. */
1625 _bfd_coff_read_string_table (abfd)
1628 char extstrsize[STRING_SIZE_SIZE];
1629 bfd_size_type strsize;
1633 if (obj_coff_strings (abfd) != NULL)
1634 return obj_coff_strings (abfd);
1636 if (obj_sym_filepos (abfd) == 0)
1638 bfd_set_error (bfd_error_no_symbols);
1642 pos = obj_sym_filepos (abfd);
1643 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1644 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1647 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1648 != sizeof extstrsize)
1650 if (bfd_get_error () != bfd_error_file_truncated)
1653 /* There is no string table. */
1654 strsize = STRING_SIZE_SIZE;
1658 #if STRING_SIZE_SIZE == 4
1659 strsize = H_GET_32 (abfd, extstrsize);
1661 #error Change H_GET_32
1665 if (strsize < STRING_SIZE_SIZE)
1667 (*_bfd_error_handler)
1668 (_("%s: bad string table size %lu"), bfd_archive_filename (abfd),
1669 (unsigned long) strsize);
1670 bfd_set_error (bfd_error_bad_value);
1674 strings = (char *) bfd_malloc (strsize);
1675 if (strings == NULL)
1678 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1679 != strsize - STRING_SIZE_SIZE)
1685 obj_coff_strings (abfd) = strings;
1690 /* Free up the external symbols and strings read from a COFF file. */
1693 _bfd_coff_free_symbols (abfd)
1696 if (obj_coff_external_syms (abfd) != NULL
1697 && ! obj_coff_keep_syms (abfd))
1699 free (obj_coff_external_syms (abfd));
1700 obj_coff_external_syms (abfd) = NULL;
1702 if (obj_coff_strings (abfd) != NULL
1703 && ! obj_coff_keep_strings (abfd))
1705 free (obj_coff_strings (abfd));
1706 obj_coff_strings (abfd) = NULL;
1711 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1712 knit the symbol names into a normalized form. By normalized here I
1713 mean that all symbols have an n_offset pointer that points to a null-
1714 terminated string. */
1716 combined_entry_type *
1717 coff_get_normalized_symtab (abfd)
1720 combined_entry_type *internal;
1721 combined_entry_type *internal_ptr;
1722 combined_entry_type *symbol_ptr;
1723 combined_entry_type *internal_end;
1727 const char *string_table = NULL;
1728 char *debug_section = NULL;
1731 if (obj_raw_syments (abfd) != NULL)
1732 return obj_raw_syments (abfd);
1734 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1735 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1736 if (internal == NULL && size != 0)
1738 internal_end = internal + obj_raw_syment_count (abfd);
1740 if (! _bfd_coff_get_external_symbols (abfd))
1743 raw_src = (char *) obj_coff_external_syms (abfd);
1745 /* mark the end of the symbols */
1746 symesz = bfd_coff_symesz (abfd);
1747 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1749 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1750 probably possible. If one shows up, it will probably kill us. */
1752 /* Swap all the raw entries */
1753 for (internal_ptr = internal;
1755 raw_src += symesz, internal_ptr++)
1759 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1760 (PTR) & internal_ptr->u.syment);
1761 symbol_ptr = internal_ptr;
1764 i < symbol_ptr->u.syment.n_numaux;
1769 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1770 symbol_ptr->u.syment.n_type,
1771 symbol_ptr->u.syment.n_sclass,
1772 (int) i, symbol_ptr->u.syment.n_numaux,
1773 &(internal_ptr->u.auxent));
1774 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1779 /* Free the raw symbols, but not the strings (if we have them). */
1780 obj_coff_keep_strings (abfd) = true;
1781 if (! _bfd_coff_free_symbols (abfd))
1784 for (internal_ptr = internal; internal_ptr < internal_end;
1787 if (internal_ptr->u.syment.n_sclass == C_FILE
1788 && internal_ptr->u.syment.n_numaux > 0)
1790 /* make a file symbol point to the name in the auxent, since
1791 the text ".file" is redundant */
1792 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1794 /* the filename is a long one, point into the string table */
1795 if (string_table == NULL)
1797 string_table = _bfd_coff_read_string_table (abfd);
1798 if (string_table == NULL)
1802 internal_ptr->u.syment._n._n_n._n_offset =
1805 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1809 /* Ordinary short filename, put into memory anyway. The
1810 Microsoft PE tools sometimes store a filename in
1811 multiple AUX entries. */
1812 if (internal_ptr->u.syment.n_numaux > 1
1813 && coff_data (abfd)->pe)
1815 internal_ptr->u.syment._n._n_n._n_offset =
1818 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1819 internal_ptr->u.syment.n_numaux * symesz));
1823 internal_ptr->u.syment._n._n_n._n_offset =
1826 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1827 (size_t) bfd_coff_filnmlen (abfd)));
1833 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1835 /* This is a "short" name. Make it long. */
1839 /* find the length of this string without walking into memory
1841 for (i = 0; i < 8; ++i)
1842 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1845 newstring = (PTR) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1846 if (newstring == NULL)
1848 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1849 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1850 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1852 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1853 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1854 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1856 /* Long name already. Point symbol at the string in the
1858 if (string_table == NULL)
1860 string_table = _bfd_coff_read_string_table (abfd);
1861 if (string_table == NULL)
1864 internal_ptr->u.syment._n._n_n._n_offset =
1867 + internal_ptr->u.syment._n._n_n._n_offset));
1871 /* Long name in debug section. Very similar. */
1872 if (debug_section == NULL)
1873 debug_section = build_debug_section (abfd);
1874 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1875 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1878 internal_ptr += internal_ptr->u.syment.n_numaux;
1881 obj_raw_syments (abfd) = internal;
1882 BFD_ASSERT (obj_raw_syment_count (abfd)
1883 == (unsigned int) (internal_ptr - internal));
1886 } /* coff_get_normalized_symtab() */
1889 coff_get_reloc_upper_bound (abfd, asect)
1893 if (bfd_get_format (abfd) != bfd_object)
1895 bfd_set_error (bfd_error_invalid_operation);
1898 return (asect->reloc_count + 1) * sizeof (arelent *);
1902 coff_make_empty_symbol (abfd)
1905 bfd_size_type amt = sizeof (coff_symbol_type);
1906 coff_symbol_type *new = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1909 new->symbol.section = 0;
1911 new->lineno = (alent *) NULL;
1912 new->done_lineno = false;
1913 new->symbol.the_bfd = abfd;
1914 return &new->symbol;
1917 /* Make a debugging symbol. */
1920 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1922 PTR ptr ATTRIBUTE_UNUSED;
1923 unsigned long sz ATTRIBUTE_UNUSED;
1925 bfd_size_type amt = sizeof (coff_symbol_type);
1926 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, amt);
1929 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1930 (but shouldn't be a constant). */
1931 amt = sizeof (combined_entry_type) * 10;
1932 new->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1935 new->symbol.section = bfd_abs_section_ptr;
1936 new->symbol.flags = BSF_DEBUGGING;
1937 new->lineno = (alent *) NULL;
1938 new->done_lineno = false;
1939 new->symbol.the_bfd = abfd;
1940 return &new->symbol;
1944 coff_get_symbol_info (abfd, symbol, ret)
1949 bfd_symbol_info (symbol, ret);
1950 if (coffsymbol (symbol)->native != NULL
1951 && coffsymbol (symbol)->native->fix_value)
1953 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1954 (unsigned long) obj_raw_syments (abfd);
1958 /* Return the COFF syment for a symbol. */
1961 bfd_coff_get_syment (abfd, symbol, psyment)
1964 struct internal_syment *psyment;
1966 coff_symbol_type *csym;
1968 csym = coff_symbol_from (abfd, symbol);
1969 if (csym == NULL || csym->native == NULL)
1971 bfd_set_error (bfd_error_invalid_operation);
1975 *psyment = csym->native->u.syment;
1977 if (csym->native->fix_value)
1978 psyment->n_value = psyment->n_value -
1979 (unsigned long) obj_raw_syments (abfd);
1981 /* FIXME: We should handle fix_line here. */
1986 /* Return the COFF auxent for a symbol. */
1989 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1993 union internal_auxent *pauxent;
1995 coff_symbol_type *csym;
1996 combined_entry_type *ent;
1998 csym = coff_symbol_from (abfd, symbol);
2001 || csym->native == NULL
2002 || indx >= csym->native->u.syment.n_numaux)
2004 bfd_set_error (bfd_error_invalid_operation);
2008 ent = csym->native + indx + 1;
2010 *pauxent = ent->u.auxent;
2013 pauxent->x_sym.x_tagndx.l =
2014 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2015 - obj_raw_syments (abfd));
2018 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2019 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2020 - obj_raw_syments (abfd));
2022 if (ent->fix_scnlen)
2023 pauxent->x_csect.x_scnlen.l =
2024 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2025 - obj_raw_syments (abfd));
2030 /* Print out information about COFF symbol. */
2033 coff_print_symbol (abfd, filep, symbol, how)
2037 bfd_print_symbol_type how;
2039 FILE *file = (FILE *) filep;
2043 case bfd_print_symbol_name:
2044 fprintf (file, "%s", symbol->name);
2047 case bfd_print_symbol_more:
2048 fprintf (file, "coff %s %s",
2049 coffsymbol (symbol)->native ? "n" : "g",
2050 coffsymbol (symbol)->lineno ? "l" : " ");
2053 case bfd_print_symbol_all:
2054 if (coffsymbol (symbol)->native)
2058 combined_entry_type *combined = coffsymbol (symbol)->native;
2059 combined_entry_type *root = obj_raw_syments (abfd);
2060 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2062 fprintf (file, "[%3ld]", (long) (combined - root));
2064 if (! combined->fix_value)
2065 val = (bfd_vma) combined->u.syment.n_value;
2067 val = combined->u.syment.n_value - (unsigned long) root;
2071 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2072 combined->u.syment.n_scnum,
2073 combined->u.syment.n_flags,
2074 combined->u.syment.n_type,
2075 combined->u.syment.n_sclass,
2076 combined->u.syment.n_numaux,
2077 (unsigned long) val,
2080 /* Print out the wide, 64 bit, symbol value */
2082 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2083 combined->u.syment.n_scnum,
2084 combined->u.syment.n_flags,
2085 combined->u.syment.n_type,
2086 combined->u.syment.n_sclass,
2087 combined->u.syment.n_numaux,
2092 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2094 combined_entry_type *auxp = combined + aux + 1;
2098 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2100 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2102 fprintf (file, "\n");
2104 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2107 switch (combined->u.syment.n_sclass)
2110 fprintf (file, "File ");
2114 if (combined->u.syment.n_type == T_NULL)
2115 /* probably a section symbol? */
2117 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2118 (long) auxp->u.auxent.x_scn.x_scnlen,
2119 auxp->u.auxent.x_scn.x_nreloc,
2120 auxp->u.auxent.x_scn.x_nlinno);
2121 if (auxp->u.auxent.x_scn.x_checksum != 0
2122 || auxp->u.auxent.x_scn.x_associated != 0
2123 || auxp->u.auxent.x_scn.x_comdat != 0)
2124 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2125 auxp->u.auxent.x_scn.x_checksum,
2126 auxp->u.auxent.x_scn.x_associated,
2127 auxp->u.auxent.x_scn.x_comdat);
2130 /* else fall through */
2132 if (ISFCN (combined->u.syment.n_type))
2137 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2140 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2141 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2143 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2144 tagndx, auxp->u.auxent.x_sym.x_misc.x_fsize,
2148 /* else fall through */
2150 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2151 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2152 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2155 fprintf (file, " endndx %ld",
2157 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2165 fprintf (file, "\n%s :", l->u.sym->name);
2167 while (l->line_number)
2169 fprintf (file, "\n%4d : 0x%lx",
2172 (l->u.offset + symbol->section->vma)));
2179 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
2180 fprintf (file, " %-5s %s %s %s",
2181 symbol->section->name,
2182 coffsymbol (symbol)->native ? "n" : "g",
2183 coffsymbol (symbol)->lineno ? "l" : " ",
2189 /* Return whether a symbol name implies a local symbol. In COFF,
2190 local symbols generally start with ``.L''. Most targets use this
2191 function for the is_local_label_name entry point, but some may
2195 _bfd_coff_is_local_label_name (abfd, name)
2196 bfd *abfd ATTRIBUTE_UNUSED;
2199 return (boolean) (name[0] == '.' && name[1] == 'L');
2202 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2203 section, calculate and return the name of the source file and the line
2204 nearest to the wanted location. */
2207 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2208 functionname_ptr, line_ptr)
2213 const char **filename_ptr;
2214 const char **functionname_ptr;
2215 unsigned int *line_ptr;
2219 unsigned int line_base;
2220 coff_data_type *cof = coff_data (abfd);
2221 /* Run through the raw syments if available */
2222 combined_entry_type *p;
2223 combined_entry_type *pend;
2225 struct coff_section_tdata *sec_data;
2228 /* Before looking through the symbol table, try to use a .stab
2229 section to find the information. */
2230 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2231 &found, filename_ptr,
2232 functionname_ptr, line_ptr,
2233 &coff_data(abfd)->line_info))
2239 /* Also try examining DWARF2 debugging information. */
2240 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2241 filename_ptr, functionname_ptr,
2243 &coff_data(abfd)->dwarf2_find_line_info))
2247 *functionname_ptr = 0;
2250 /* Don't try and find line numbers in a non coff file */
2251 if (!bfd_family_coff (abfd))
2257 /* Find the first C_FILE symbol. */
2258 p = cof->raw_syments;
2262 pend = p + cof->raw_syment_count;
2265 if (p->u.syment.n_sclass == C_FILE)
2267 p += 1 + p->u.syment.n_numaux;
2275 /* Look through the C_FILE symbols to find the best one. */
2276 sec_vma = bfd_get_section_vma (abfd, section);
2277 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2278 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2281 combined_entry_type *p2;
2283 for (p2 = p + 1 + p->u.syment.n_numaux;
2285 p2 += 1 + p2->u.syment.n_numaux)
2287 if (p2->u.syment.n_scnum > 0
2289 == coff_section_from_bfd_index (abfd,
2290 p2->u.syment.n_scnum)))
2292 if (p2->u.syment.n_sclass == C_FILE)
2299 /* We use <= MAXDIFF here so that if we get a zero length
2300 file, we actually use the next file entry. */
2302 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2303 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2305 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2306 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2309 /* Avoid endless loops on erroneous files by ensuring that
2310 we always move forward in the file. */
2311 if (p >= cof->raw_syments + p->u.syment.n_value)
2314 p = cof->raw_syments + p->u.syment.n_value;
2315 if (p > pend || p->u.syment.n_sclass != C_FILE)
2320 /* Now wander though the raw linenumbers of the section */
2321 /* If we have been called on this section before, and the offset we
2322 want is further down then we can prime the lookup loop. */
2323 sec_data = coff_section_data (abfd, section);
2324 if (sec_data != NULL
2326 && offset >= sec_data->offset)
2329 *functionname_ptr = sec_data->function;
2330 line_base = sec_data->line_base;
2338 if (section->lineno != NULL)
2340 bfd_vma last_value = 0;
2342 l = §ion->lineno[i];
2344 for (; i < section->lineno_count; i++)
2346 if (l->line_number == 0)
2348 /* Get the symbol this line number points at */
2349 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2350 if (coff->symbol.value > offset)
2352 *functionname_ptr = coff->symbol.name;
2353 last_value = coff->symbol.value;
2356 combined_entry_type *s = coff->native;
2357 s = s + 1 + s->u.syment.n_numaux;
2359 /* In XCOFF a debugging symbol can follow the
2361 if (s->u.syment.n_scnum == N_DEBUG)
2362 s = s + 1 + s->u.syment.n_numaux;
2364 /* S should now point to the .bf of the function. */
2365 if (s->u.syment.n_numaux)
2367 /* The linenumber is stored in the auxent. */
2368 union internal_auxent *a = &((s + 1)->u.auxent);
2369 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2370 *line_ptr = line_base;
2376 if (l->u.offset > offset)
2378 *line_ptr = l->line_number + line_base - 1;
2383 /* If we fell off the end of the loop, then assume that this
2384 symbol has no line number info. Otherwise, symbols with no
2385 line number info get reported with the line number of the
2386 last line of the last symbol which does have line number
2387 info. We use 0x100 as a slop to account for cases where the
2388 last line has executable code. */
2389 if (i >= section->lineno_count
2391 && offset - last_value > 0x100)
2393 *functionname_ptr = NULL;
2398 /* Cache the results for the next call. */
2399 if (sec_data == NULL && section->owner == abfd)
2401 amt = sizeof (struct coff_section_tdata);
2402 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
2403 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2405 if (sec_data != NULL)
2407 sec_data->offset = offset;
2409 sec_data->function = *functionname_ptr;
2410 sec_data->line_base = line_base;
2417 coff_sizeof_headers (abfd, reloc)
2425 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2429 size = bfd_coff_filhsz (abfd);
2432 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2436 /* Change the class of a coff symbol held by BFD. */
2438 bfd_coff_set_symbol_class (abfd, symbol, class)
2443 coff_symbol_type * csym;
2445 csym = coff_symbol_from (abfd, symbol);
2448 bfd_set_error (bfd_error_invalid_operation);
2451 else if (csym->native == NULL)
2453 /* This is an alien symbol which no native coff backend data.
2454 We cheat here by creating a fake native entry for it and
2455 then filling in the class. This code is based on that in
2456 coff_write_alien_symbol(). */
2458 combined_entry_type * native;
2459 bfd_size_type amt = sizeof (* native);
2461 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2465 native->u.syment.n_type = T_NULL;
2466 native->u.syment.n_sclass = class;
2468 if (bfd_is_und_section (symbol->section))
2470 native->u.syment.n_scnum = N_UNDEF;
2471 native->u.syment.n_value = symbol->value;
2473 else if (bfd_is_com_section (symbol->section))
2475 native->u.syment.n_scnum = N_UNDEF;
2476 native->u.syment.n_value = symbol->value;
2480 native->u.syment.n_scnum =
2481 symbol->section->output_section->target_index;
2482 native->u.syment.n_value = (symbol->value
2483 + symbol->section->output_offset);
2484 if (! obj_pe (abfd))
2485 native->u.syment.n_value += symbol->section->output_section->vma;
2487 /* Copy the any flags from the file header into the symbol.
2489 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2492 csym->native = native;
2496 csym->native->u.syment.n_sclass = class;