1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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. */
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->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
145 return_section->target_index = target_index;
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 /* Now copy data as required; construct all asections etc */
218 for (i = 0; i < nscns; i++)
220 struct internal_scnhdr tmp;
221 bfd_coff_swap_scnhdr_in (abfd,
222 (PTR) (external_sections + i * scnhsz),
224 if (! make_a_section_from_file (abfd, &tmp, i + 1))
229 /* make_abs_section (abfd); */
231 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
237 bfd_release (abfd, tdata);
238 abfd->flags = oflags;
239 bfd_get_start_address (abfd) = ostart;
240 return (const bfd_target *) NULL;
243 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
244 not a COFF file. This is also used by ECOFF. */
254 struct internal_filehdr internal_f;
255 struct internal_aouthdr internal_a;
257 /* figure out how much to read */
258 filhsz = bfd_coff_filhsz (abfd);
259 aoutsz = bfd_coff_aoutsz (abfd);
261 filehdr = bfd_alloc (abfd, filhsz);
264 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
266 if (bfd_get_error () != bfd_error_system_call)
267 bfd_set_error (bfd_error_wrong_format);
270 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
271 bfd_release (abfd, filehdr);
273 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
275 bfd_set_error (bfd_error_wrong_format);
278 nscns = internal_f.f_nscns;
280 if (internal_f.f_opthdr)
284 opthdr = bfd_alloc (abfd, aoutsz);
287 if (bfd_read (opthdr, 1, internal_f.f_opthdr, abfd)
288 != internal_f.f_opthdr)
292 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) &internal_a);
295 return coff_real_object_p (abfd, nscns, &internal_f,
296 (internal_f.f_opthdr != 0
298 : (struct internal_aouthdr *) NULL));
301 /* Get the BFD section from a COFF symbol section number. */
304 coff_section_from_bfd_index (abfd, index)
308 struct sec *answer = abfd->sections;
311 return bfd_abs_section_ptr;
312 if (index == N_UNDEF)
313 return bfd_und_section_ptr;
314 if (index == N_DEBUG)
315 return bfd_abs_section_ptr;
319 if (answer->target_index == index)
321 answer = answer->next;
324 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
325 has a bad symbol table in biglitpow.o. */
326 return bfd_und_section_ptr;
329 /* Get the upper bound of a COFF symbol table. */
332 coff_get_symtab_upper_bound (abfd)
335 if (!bfd_coff_slurp_symbol_table (abfd))
338 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
342 /* Canonicalize a COFF symbol table. */
345 coff_get_symtab (abfd, alocation)
349 unsigned int counter;
350 coff_symbol_type *symbase;
351 coff_symbol_type **location = (coff_symbol_type **) alocation;
353 if (!bfd_coff_slurp_symbol_table (abfd))
356 symbase = obj_symbols (abfd);
357 counter = bfd_get_symcount (abfd);
358 while (counter-- > 0)
359 *location++ = symbase++;
363 return bfd_get_symcount (abfd);
366 /* Get the name of a symbol. The caller must pass in a buffer of size
370 _bfd_coff_internal_syment_name (abfd, sym, buf)
372 const struct internal_syment *sym;
375 /* FIXME: It's not clear this will work correctly if sizeof
377 if (sym->_n._n_n._n_zeroes != 0
378 || sym->_n._n_n._n_offset == 0)
380 memcpy (buf, sym->_n._n_name, SYMNMLEN);
381 buf[SYMNMLEN] = '\0';
388 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
389 strings = obj_coff_strings (abfd);
392 strings = _bfd_coff_read_string_table (abfd);
396 return strings + sym->_n._n_n._n_offset;
400 /* Read in and swap the relocs. This returns a buffer holding the
401 relocs for section SEC in file ABFD. If CACHE is true and
402 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
403 the function is called again. If EXTERNAL_RELOCS is not NULL, it
404 is a buffer large enough to hold the unswapped relocs. If
405 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
406 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
407 value must be INTERNAL_RELOCS. The function returns NULL on error. */
409 struct internal_reloc *
410 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
411 require_internal, internal_relocs)
415 bfd_byte *external_relocs;
416 boolean require_internal;
417 struct internal_reloc *internal_relocs;
420 bfd_byte *free_external = NULL;
421 struct internal_reloc *free_internal = NULL;
424 struct internal_reloc *irel;
426 if (coff_section_data (abfd, sec) != NULL
427 && coff_section_data (abfd, sec)->relocs != NULL)
429 if (! require_internal)
430 return coff_section_data (abfd, sec)->relocs;
431 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
432 sec->reloc_count * sizeof (struct internal_reloc));
433 return internal_relocs;
436 relsz = bfd_coff_relsz (abfd);
438 if (external_relocs == NULL)
440 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
441 if (free_external == NULL && sec->reloc_count > 0)
443 external_relocs = free_external;
446 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
447 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
448 != relsz * sec->reloc_count))
451 if (internal_relocs == NULL)
453 free_internal = ((struct internal_reloc *)
454 bfd_malloc (sec->reloc_count
455 * sizeof (struct internal_reloc)));
456 if (free_internal == NULL && sec->reloc_count > 0)
458 internal_relocs = free_internal;
461 /* Swap in the relocs. */
462 erel = external_relocs;
463 erel_end = erel + relsz * sec->reloc_count;
464 irel = internal_relocs;
465 for (; erel < erel_end; erel += relsz, irel++)
466 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
468 if (free_external != NULL)
470 free (free_external);
471 free_external = NULL;
474 if (cache && free_internal != NULL)
476 if (coff_section_data (abfd, sec) == NULL)
479 (PTR) bfd_zalloc (abfd,
480 sizeof (struct coff_section_tdata));
481 if (sec->used_by_bfd == NULL)
483 coff_section_data (abfd, sec)->contents = NULL;
485 coff_section_data (abfd, sec)->relocs = free_internal;
488 return internal_relocs;
491 if (free_external != NULL)
492 free (free_external);
493 if (free_internal != NULL)
494 free (free_internal);
498 /* Set lineno_count for the output sections of a COFF file. */
501 coff_count_linenumbers (abfd)
504 unsigned int limit = bfd_get_symcount (abfd);
512 /* This may be from the backend linker, in which case the
513 lineno_count in the sections is correct. */
514 for (s = abfd->sections; s != NULL; s = s->next)
515 total += s->lineno_count;
519 for (s = abfd->sections; s != NULL; s = s->next)
520 BFD_ASSERT (s->lineno_count == 0);
522 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
524 asymbol *q_maybe = *p;
526 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
528 coff_symbol_type *q = coffsymbol (q_maybe);
530 /* The AIX 4.1 compiler can sometimes generate line numbers
531 attached to debugging symbols. We try to simply ignore
533 if (q->lineno != NULL
534 && q->symbol.section->owner != NULL)
536 /* This symbol has line numbers. Increment the owning
537 section's linenumber count. */
538 alent *l = q->lineno;
540 ++q->symbol.section->output_section->lineno_count;
543 while (l->line_number != 0)
546 ++q->symbol.section->output_section->lineno_count;
556 /* Takes a bfd and a symbol, returns a pointer to the coff specific
557 area of the symbol if there is one. */
561 coff_symbol_from (ignore_abfd, symbol)
562 bfd *ignore_abfd ATTRIBUTE_UNUSED;
565 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
566 return (coff_symbol_type *) NULL;
568 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
569 return (coff_symbol_type *) NULL;
571 return (coff_symbol_type *) symbol;
575 fixup_symbol_value (abfd, coff_symbol_ptr, syment)
577 coff_symbol_type *coff_symbol_ptr;
578 struct internal_syment *syment;
581 /* Normalize the symbol flags */
582 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
584 /* a common symbol is undefined with a value */
585 syment->n_scnum = N_UNDEF;
586 syment->n_value = coff_symbol_ptr->symbol.value;
588 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
590 syment->n_value = coff_symbol_ptr->symbol.value;
592 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
594 syment->n_scnum = N_UNDEF;
597 /* FIXME: Do we need to handle the absolute section here? */
600 if (coff_symbol_ptr->symbol.section)
603 coff_symbol_ptr->symbol.section->output_section->target_index;
605 syment->n_value = (coff_symbol_ptr->symbol.value
606 + coff_symbol_ptr->symbol.section->output_offset);
609 coff_symbol_ptr->symbol.section->output_section->vma;
615 syment->n_scnum = N_ABS;
616 syment->n_value = coff_symbol_ptr->symbol.value;
621 /* Run through all the symbols in the symbol table and work out what
622 their indexes into the symbol table will be when output.
624 Coff requires that each C_FILE symbol points to the next one in the
625 chain, and that the last one points to the first external symbol. We
629 coff_renumber_symbols (bfd_ptr, first_undef)
633 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
634 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
635 unsigned int native_index = 0;
636 struct internal_syment *last_file = (struct internal_syment *) NULL;
637 unsigned int symbol_index;
639 /* COFF demands that undefined symbols come after all other symbols.
640 Since we don't need to impose this extra knowledge on all our
641 client programs, deal with that here. Sort the symbol table;
642 just move the undefined symbols to the end, leaving the rest
643 alone. The O'Reilly book says that defined global symbols come
644 at the end before the undefined symbols, so we do that here as
646 /* @@ Do we have some condition we could test for, so we don't always
647 have to do this? I don't think relocatability is quite right, but
648 I'm not certain. [raeburn:19920508.1711EST] */
653 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
654 sizeof (asymbol *) * (symbol_count + 1));
657 bfd_ptr->outsymbols = newsyms;
658 for (i = 0; i < symbol_count; i++)
659 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
660 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
661 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
662 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
663 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
665 *newsyms++ = symbol_ptr_ptr[i];
667 for (i = 0; i < symbol_count; i++)
668 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
669 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
670 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
671 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
672 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
674 *newsyms++ = symbol_ptr_ptr[i];
676 *first_undef = newsyms - bfd_ptr->outsymbols;
678 for (i = 0; i < symbol_count; i++)
679 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
680 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
681 *newsyms++ = symbol_ptr_ptr[i];
682 *newsyms = (asymbol *) NULL;
683 symbol_ptr_ptr = bfd_ptr->outsymbols;
686 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
688 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
689 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
690 if (coff_symbol_ptr && coff_symbol_ptr->native)
692 combined_entry_type *s = coff_symbol_ptr->native;
695 if (s->u.syment.n_sclass == C_FILE)
697 if (last_file != (struct internal_syment *) NULL)
698 last_file->n_value = native_index;
699 last_file = &(s->u.syment);
704 /* Modify the symbol values according to their section and
707 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
709 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
710 s[i].offset = native_index++;
717 obj_conv_table_size (bfd_ptr) = native_index;
722 /* Run thorough the symbol table again, and fix it so that all
723 pointers to entries are changed to the entries' index in the output
727 coff_mangle_symbols (bfd_ptr)
730 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
731 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
732 unsigned int symbol_index;
734 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
736 coff_symbol_type *coff_symbol_ptr =
737 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
739 if (coff_symbol_ptr && coff_symbol_ptr->native)
742 combined_entry_type *s = coff_symbol_ptr->native;
746 /* FIXME: We should use a union here. */
747 s->u.syment.n_value =
748 ((combined_entry_type *) s->u.syment.n_value)->offset;
753 /* The value is the offset into the line number entries
754 for the symbol's section. On output, the symbol's
755 section should be N_DEBUG. */
756 s->u.syment.n_value =
757 (coff_symbol_ptr->symbol.section->output_section->line_filepos
758 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
759 coff_symbol_ptr->symbol.section =
760 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
761 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
763 for (i = 0; i < s->u.syment.n_numaux; i++)
765 combined_entry_type *a = s + i + 1;
768 a->u.auxent.x_sym.x_tagndx.l =
769 a->u.auxent.x_sym.x_tagndx.p->offset;
774 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
775 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
780 a->u.auxent.x_csect.x_scnlen.l =
781 a->u.auxent.x_csect.x_scnlen.p->offset;
790 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
791 debug_string_section_p, debug_string_size_p)
794 combined_entry_type *native;
795 bfd_size_type *string_size_p;
796 asection **debug_string_section_p;
797 bfd_size_type *debug_string_size_p;
799 unsigned int name_length;
800 union internal_auxent *auxent;
801 char *name = (char *) (symbol->name);
803 if (name == (char *) NULL)
805 /* coff symbols always have names, so we'll make one up */
806 symbol->name = "strange";
807 name = (char *) symbol->name;
809 name_length = strlen (name);
811 if (native->u.syment.n_sclass == C_FILE
812 && native->u.syment.n_numaux > 0)
814 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
815 auxent = &(native + 1)->u.auxent;
817 if (bfd_coff_long_filenames (abfd))
819 if (name_length <= FILNMLEN)
821 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
825 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
826 auxent->x_file.x_n.x_zeroes = 0;
827 *string_size_p += name_length + 1;
832 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
833 if (name_length > FILNMLEN)
835 name[FILNMLEN] = '\0';
841 if (name_length <= SYMNMLEN)
843 /* This name will fit into the symbol neatly */
844 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
846 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
848 native->u.syment._n._n_n._n_offset = (*string_size_p
850 native->u.syment._n._n_n._n_zeroes = 0;
851 *string_size_p += name_length + 1;
858 /* This name should be written into the .debug section. For
859 some reason each name is preceded by a two byte length
860 and also followed by a null byte. FIXME: We assume that
861 the .debug section has already been created, and that it
863 if (*debug_string_section_p == (asection *) NULL)
864 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
865 filepos = bfd_tell (abfd);
866 bfd_put_16 (abfd, name_length + 1, buf);
867 if (!bfd_set_section_contents (abfd,
868 *debug_string_section_p,
870 (file_ptr) *debug_string_size_p,
872 || !bfd_set_section_contents (abfd,
873 *debug_string_section_p,
875 ((file_ptr) *debug_string_size_p
877 (bfd_size_type) name_length + 1))
879 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
881 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
882 native->u.syment._n._n_n._n_zeroes = 0;
883 *debug_string_size_p += name_length + 3;
888 /* We need to keep track of the symbol index so that when we write out
889 the relocs we can get the index for a symbol. This method is a
892 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
894 /* Write a symbol out to a COFF file. */
897 coff_write_symbol (abfd, symbol, native, written, string_size_p,
898 debug_string_section_p, debug_string_size_p)
901 combined_entry_type *native;
902 unsigned int *written;
903 bfd_size_type *string_size_p;
904 asection **debug_string_section_p;
905 bfd_size_type *debug_string_size_p;
907 unsigned int numaux = native->u.syment.n_numaux;
908 int type = native->u.syment.n_type;
909 int class = native->u.syment.n_sclass;
911 bfd_size_type symesz;
913 if (native->u.syment.n_sclass == C_FILE)
914 symbol->flags |= BSF_DEBUGGING;
916 if (symbol->flags & BSF_DEBUGGING
917 && bfd_is_abs_section (symbol->section))
919 native->u.syment.n_scnum = N_DEBUG;
921 else if (bfd_is_abs_section (symbol->section))
923 native->u.syment.n_scnum = N_ABS;
925 else if (bfd_is_und_section (symbol->section))
927 native->u.syment.n_scnum = N_UNDEF;
931 native->u.syment.n_scnum =
932 symbol->section->output_section->target_index;
935 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
936 debug_string_section_p, debug_string_size_p);
938 symesz = bfd_coff_symesz (abfd);
939 buf = bfd_alloc (abfd, symesz);
942 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
943 if (bfd_write (buf, 1, symesz, abfd) != symesz)
945 bfd_release (abfd, buf);
947 if (native->u.syment.n_numaux > 0)
949 bfd_size_type auxesz;
952 auxesz = bfd_coff_auxesz (abfd);
953 buf = bfd_alloc (abfd, auxesz);
956 for (j = 0; j < native->u.syment.n_numaux; j++)
958 bfd_coff_swap_aux_out (abfd,
959 &((native + j + 1)->u.auxent),
963 native->u.syment.n_numaux,
965 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
968 bfd_release (abfd, buf);
971 /* Store the index for use when we write out the relocs. */
972 set_index (symbol, *written);
974 *written += numaux + 1;
978 /* Write out a symbol to a COFF file that does not come from a COFF
979 file originally. This symbol may have been created by the linker,
980 or we may be linking a non COFF file to a COFF file. */
983 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
984 debug_string_section_p, debug_string_size_p)
987 unsigned int *written;
988 bfd_size_type *string_size_p;
989 asection **debug_string_section_p;
990 bfd_size_type *debug_string_size_p;
992 combined_entry_type *native;
993 combined_entry_type dummy;
996 native->u.syment.n_type = T_NULL;
997 native->u.syment.n_flags = 0;
998 if (bfd_is_und_section (symbol->section))
1000 native->u.syment.n_scnum = N_UNDEF;
1001 native->u.syment.n_value = symbol->value;
1003 else if (bfd_is_com_section (symbol->section))
1005 native->u.syment.n_scnum = N_UNDEF;
1006 native->u.syment.n_value = symbol->value;
1008 else if (symbol->flags & BSF_DEBUGGING)
1010 /* There isn't much point to writing out a debugging symbol
1011 unless we are prepared to convert it into COFF debugging
1012 format. So, we just ignore them. We must clobber the symbol
1013 name to keep it from being put in the string table. */
1019 native->u.syment.n_scnum =
1020 symbol->section->output_section->target_index;
1021 native->u.syment.n_value = (symbol->value
1022 + symbol->section->output_offset);
1023 if (! obj_pe (abfd))
1024 native->u.syment.n_value += symbol->section->output_section->vma;
1026 /* Copy the any flags from the the file header into the symbol.
1029 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1030 if (c != (coff_symbol_type *) NULL)
1031 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1035 native->u.syment.n_type = 0;
1036 if (symbol->flags & BSF_LOCAL)
1037 native->u.syment.n_sclass = C_STAT;
1038 else if (symbol->flags & BSF_WEAK)
1039 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1041 native->u.syment.n_sclass = C_EXT;
1042 native->u.syment.n_numaux = 0;
1044 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1045 debug_string_section_p, debug_string_size_p);
1048 /* Write a native symbol to a COFF file. */
1051 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1052 debug_string_section_p, debug_string_size_p)
1054 coff_symbol_type *symbol;
1055 unsigned int *written;
1056 bfd_size_type *string_size_p;
1057 asection **debug_string_section_p;
1058 bfd_size_type *debug_string_size_p;
1060 combined_entry_type *native = symbol->native;
1061 alent *lineno = symbol->lineno;
1063 /* If this symbol has an associated line number, we must store the
1064 symbol index in the line number field. We also tag the auxent to
1065 point to the right place in the lineno table. */
1066 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1068 unsigned int count = 0;
1069 lineno[count].u.offset = *written;
1070 if (native->u.syment.n_numaux)
1072 union internal_auxent *a = &((native + 1)->u.auxent);
1074 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1075 symbol->symbol.section->output_section->moving_line_filepos;
1078 /* Count and relocate all other linenumbers. */
1080 while (lineno[count].line_number != 0)
1084 I've been told this, but still need proof:
1085 > The second bug is also in `bfd/coffcode.h'. This bug
1086 > causes the linker to screw up the pc-relocations for
1087 > all the line numbers in COFF code. This bug isn't only
1088 > specific to A29K implementations, but affects all
1089 > systems using COFF format binaries. Note that in COFF
1090 > object files, the line number core offsets output by
1091 > the assembler are relative to the start of each
1092 > procedure, not to the start of the .text section. This
1093 > patch relocates the line numbers relative to the
1094 > `native->u.syment.n_value' instead of the section
1098 lineno[count].u.offset += native->u.syment.n_value;
1100 lineno[count].u.offset +=
1101 (symbol->symbol.section->output_section->vma
1102 + symbol->symbol.section->output_offset);
1106 symbol->done_lineno = true;
1108 symbol->symbol.section->output_section->moving_line_filepos +=
1109 count * bfd_coff_linesz (abfd);
1112 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1113 string_size_p, debug_string_section_p,
1114 debug_string_size_p);
1117 /* Write out the COFF symbols. */
1120 coff_write_symbols (abfd)
1123 bfd_size_type string_size;
1124 asection *debug_string_section;
1125 bfd_size_type debug_string_size;
1127 unsigned int limit = bfd_get_symcount (abfd);
1128 unsigned int written = 0;
1132 debug_string_section = NULL;
1133 debug_string_size = 0;
1135 /* If this target supports long section names, they must be put into
1136 the string table. This is supported by PE. This code must
1137 handle section names just as they are handled in
1138 coff_write_object_contents. */
1139 if (bfd_coff_long_section_names (abfd))
1143 for (o = abfd->sections; o != NULL; o = o->next)
1147 len = strlen (o->name);
1149 string_size += len + 1;
1153 /* Seek to the right place */
1154 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1157 /* Output all the symbols we have */
1160 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1162 asymbol *symbol = *p;
1163 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1165 if (c_symbol == (coff_symbol_type *) NULL
1166 || c_symbol->native == (combined_entry_type *) NULL)
1168 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1169 &debug_string_section,
1170 &debug_string_size))
1175 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1176 &string_size, &debug_string_section,
1177 &debug_string_size))
1182 obj_raw_syment_count (abfd) = written;
1184 /* Now write out strings */
1186 if (string_size != 0)
1188 unsigned int size = string_size + STRING_SIZE_SIZE;
1189 bfd_byte buffer[STRING_SIZE_SIZE];
1191 #if STRING_SIZE_SIZE == 4
1192 bfd_h_put_32 (abfd, size, buffer);
1194 #error Change bfd_h_put_32
1196 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1199 /* Handle long section names. This code must handle section
1200 names just as they are handled in coff_write_object_contents. */
1201 if (bfd_coff_long_section_names (abfd))
1205 for (o = abfd->sections; o != NULL; o = o->next)
1209 len = strlen (o->name);
1212 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1218 for (p = abfd->outsymbols, i = 0;
1223 size_t name_length = strlen (q->name);
1224 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1227 /* Figure out whether the symbol name should go in the string
1228 table. Symbol names that are short enough are stored
1229 directly in the syment structure. File names permit a
1230 different, longer, length in the syment structure. On
1231 XCOFF, some symbol names are stored in the .debug section
1232 rather than in the string table. */
1234 if (c_symbol == NULL
1235 || c_symbol->native == NULL)
1237 /* This is not a COFF symbol, so it certainly is not a
1238 file name, nor does it go in the .debug section. */
1241 else if (bfd_coff_symname_in_debug (abfd,
1242 &c_symbol->native->u.syment))
1244 /* This symbol name is in the XCOFF .debug section.
1245 Don't write it into the string table. */
1246 maxlen = name_length;
1248 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1249 && c_symbol->native->u.syment.n_numaux > 0)
1254 if (name_length > maxlen)
1256 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1264 /* We would normally not write anything here, but we'll write
1265 out 4 so that any stupid coff reader which tries to read the
1266 string table even when there isn't one won't croak. */
1267 unsigned int size = STRING_SIZE_SIZE;
1268 bfd_byte buffer[STRING_SIZE_SIZE];
1270 #if STRING_SIZE_SIZE == 4
1271 bfd_h_put_32 (abfd, size, buffer);
1273 #error Change bfd_h_put_32
1275 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1276 != STRING_SIZE_SIZE)
1280 /* Make sure the .debug section was created to be the correct size.
1281 We should create it ourselves on the fly, but we don't because
1282 BFD won't let us write to any section until we know how large all
1283 the sections are. We could still do it by making another pass
1284 over the symbols. FIXME. */
1285 BFD_ASSERT (debug_string_size == 0
1286 || (debug_string_section != (asection *) NULL
1287 && (BFD_ALIGN (debug_string_size,
1288 1 << debug_string_section->alignment_power)
1289 == bfd_section_size (abfd, debug_string_section))));
1295 coff_write_linenumbers (abfd)
1299 bfd_size_type linesz;
1302 linesz = bfd_coff_linesz (abfd);
1303 buff = bfd_alloc (abfd, linesz);
1306 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1308 if (s->lineno_count)
1310 asymbol **q = abfd->outsymbols;
1311 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1313 /* Find all the linenumbers in this section */
1317 if (p->section->output_section == s)
1320 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1321 (bfd_asymbol_bfd (p), p));
1324 /* Found a linenumber entry, output */
1325 struct internal_lineno out;
1326 memset ((PTR) & out, 0, sizeof (out));
1328 out.l_addr.l_symndx = l->u.offset;
1329 bfd_coff_swap_lineno_out (abfd, &out, buff);
1330 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1333 while (l->line_number)
1335 out.l_lnno = l->line_number;
1336 out.l_addr.l_symndx = l->u.offset;
1337 bfd_coff_swap_lineno_out (abfd, &out, buff);
1338 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1348 bfd_release (abfd, buff);
1354 coff_get_lineno (ignore_abfd, symbol)
1355 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1358 return coffsymbol (symbol)->lineno;
1363 /* This is only called from coff_add_missing_symbols, which has been
1367 coff_section_symbol (abfd, name)
1371 asection *sec = bfd_make_section_old_way (abfd, name);
1373 combined_entry_type *csym;
1376 csym = coff_symbol_from (abfd, sym)->native;
1377 /* Make sure back-end COFF stuff is there. */
1382 coff_symbol_type sym;
1383 /* @@FIXME This shouldn't use a fixed size!! */
1384 combined_entry_type e[10];
1387 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1390 bfd_set_error (bfd_error_no_error);
1393 memset ((char *) f, 0, sizeof (*f));
1394 coff_symbol_from (abfd, sym)->native = csym = f->e;
1396 csym[0].u.syment.n_sclass = C_STAT;
1397 csym[0].u.syment.n_numaux = 1;
1398 /* SF_SET_STATICS (sym); @@ ??? */
1399 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1400 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1401 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1403 if (sec->output_section == NULL)
1405 sec->output_section = sec;
1406 sec->output_offset = 0;
1414 /* This function transforms the offsets into the symbol table into
1415 pointers to syments. */
1418 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1420 combined_entry_type *table_base;
1421 combined_entry_type *symbol;
1422 unsigned int indaux;
1423 combined_entry_type *auxent;
1425 unsigned int type = symbol->u.syment.n_type;
1426 unsigned int class = symbol->u.syment.n_sclass;
1428 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1430 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1431 (abfd, table_base, symbol, indaux, auxent))
1435 /* Don't bother if this is a file or a section */
1436 if (class == C_STAT && type == T_NULL)
1438 if (class == C_FILE)
1441 /* Otherwise patch up */
1442 #define N_TMASK coff_data (abfd)->local_n_tmask
1443 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1444 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1445 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1447 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1448 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1449 auxent->fix_end = 1;
1451 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1452 generate one, so we must be careful to ignore it. */
1453 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1455 auxent->u.auxent.x_sym.x_tagndx.p =
1456 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1457 auxent->fix_tag = 1;
1461 /* Allocate space for the ".debug" section, and read it.
1462 We did not read the debug section until now, because
1463 we didn't want to go to the trouble until someone needed it. */
1466 build_debug_section (abfd)
1469 char *debug_section;
1472 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1476 bfd_set_error (bfd_error_no_debug_section);
1480 debug_section = (PTR) bfd_alloc (abfd,
1481 bfd_get_section_size_before_reloc (sect));
1482 if (debug_section == NULL)
1485 /* Seek to the beginning of the `.debug' section and read it.
1486 Save the current position first; it is needed by our caller.
1487 Then read debug section and reset the file pointer. */
1489 position = bfd_tell (abfd);
1490 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1491 || (bfd_read (debug_section,
1492 bfd_get_section_size_before_reloc (sect), 1, abfd)
1493 != bfd_get_section_size_before_reloc (sect))
1494 || bfd_seek (abfd, position, SEEK_SET) != 0)
1496 return debug_section;
1500 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1501 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1502 be \0-terminated. */
1504 copy_name (abfd, name, maxlen)
1512 for (len = 0; len < maxlen; ++len)
1514 if (name[len] == '\0')
1520 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1522 strncpy (newname, name, len);
1523 newname[len] = '\0';
1527 /* Read in the external symbols. */
1530 _bfd_coff_get_external_symbols (abfd)
1533 bfd_size_type symesz;
1537 if (obj_coff_external_syms (abfd) != NULL)
1540 symesz = bfd_coff_symesz (abfd);
1542 size = obj_raw_syment_count (abfd) * symesz;
1544 syms = (PTR) bfd_malloc (size);
1545 if (syms == NULL && size != 0)
1548 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1549 || bfd_read (syms, size, 1, abfd) != size)
1556 obj_coff_external_syms (abfd) = syms;
1561 /* Read in the external strings. The strings are not loaded until
1562 they are needed. This is because we have no simple way of
1563 detecting a missing string table in an archive. */
1566 _bfd_coff_read_string_table (abfd)
1569 char extstrsize[STRING_SIZE_SIZE];
1573 if (obj_coff_strings (abfd) != NULL)
1574 return obj_coff_strings (abfd);
1576 if (obj_sym_filepos (abfd) == 0)
1578 bfd_set_error (bfd_error_no_symbols);
1583 (obj_sym_filepos (abfd)
1584 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1588 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1590 if (bfd_get_error () != bfd_error_file_truncated)
1593 /* There is no string table. */
1594 strsize = STRING_SIZE_SIZE;
1598 #if STRING_SIZE_SIZE == 4
1599 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1601 #error Change bfd_h_get_32
1605 if (strsize < STRING_SIZE_SIZE)
1607 (*_bfd_error_handler)
1608 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1609 (unsigned long) strsize);
1610 bfd_set_error (bfd_error_bad_value);
1614 strings = (char *) bfd_malloc (strsize);
1615 if (strings == NULL)
1618 if (bfd_read (strings + STRING_SIZE_SIZE,
1619 strsize - STRING_SIZE_SIZE, 1, abfd)
1620 != strsize - STRING_SIZE_SIZE)
1626 obj_coff_strings (abfd) = strings;
1631 /* Free up the external symbols and strings read from a COFF file. */
1634 _bfd_coff_free_symbols (abfd)
1637 if (obj_coff_external_syms (abfd) != NULL
1638 && ! obj_coff_keep_syms (abfd))
1640 free (obj_coff_external_syms (abfd));
1641 obj_coff_external_syms (abfd) = NULL;
1643 if (obj_coff_strings (abfd) != NULL
1644 && ! obj_coff_keep_strings (abfd))
1646 free (obj_coff_strings (abfd));
1647 obj_coff_strings (abfd) = NULL;
1652 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1653 knit the symbol names into a normalized form. By normalized here I
1654 mean that all symbols have an n_offset pointer that points to a null-
1655 terminated string. */
1657 combined_entry_type *
1658 coff_get_normalized_symtab (abfd)
1661 combined_entry_type *internal;
1662 combined_entry_type *internal_ptr;
1663 combined_entry_type *symbol_ptr;
1664 combined_entry_type *internal_end;
1665 bfd_size_type symesz;
1668 const char *string_table = NULL;
1669 char *debug_section = NULL;
1672 if (obj_raw_syments (abfd) != NULL)
1673 return obj_raw_syments (abfd);
1675 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1676 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1677 if (internal == NULL && size != 0)
1679 internal_end = internal + obj_raw_syment_count (abfd);
1681 if (! _bfd_coff_get_external_symbols (abfd))
1684 raw_src = (char *) obj_coff_external_syms (abfd);
1686 /* mark the end of the symbols */
1687 symesz = bfd_coff_symesz (abfd);
1688 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1690 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1691 probably possible. If one shows up, it will probably kill us. */
1693 /* Swap all the raw entries */
1694 for (internal_ptr = internal;
1696 raw_src += symesz, internal_ptr++)
1700 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1701 (PTR) & internal_ptr->u.syment);
1702 symbol_ptr = internal_ptr;
1705 i < symbol_ptr->u.syment.n_numaux;
1710 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1711 symbol_ptr->u.syment.n_type,
1712 symbol_ptr->u.syment.n_sclass,
1713 i, symbol_ptr->u.syment.n_numaux,
1714 &(internal_ptr->u.auxent));
1715 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1720 /* Free the raw symbols, but not the strings (if we have them). */
1721 obj_coff_keep_strings (abfd) = true;
1722 if (! _bfd_coff_free_symbols (abfd))
1725 for (internal_ptr = internal; internal_ptr < internal_end;
1728 if (internal_ptr->u.syment.n_sclass == C_FILE
1729 && internal_ptr->u.syment.n_numaux > 0)
1731 /* make a file symbol point to the name in the auxent, since
1732 the text ".file" is redundant */
1733 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1735 /* the filename is a long one, point into the string table */
1736 if (string_table == NULL)
1738 string_table = _bfd_coff_read_string_table (abfd);
1739 if (string_table == NULL)
1743 internal_ptr->u.syment._n._n_n._n_offset =
1746 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1750 /* Ordinary short filename, put into memory anyway. The
1751 Microsoft PE tools sometimes store a filename in
1752 multiple AUX entries. */
1753 if (internal_ptr->u.syment.n_numaux > 1
1754 && coff_data (abfd)->pe)
1756 internal_ptr->u.syment._n._n_n._n_offset =
1759 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1760 internal_ptr->u.syment.n_numaux * symesz));
1764 internal_ptr->u.syment._n._n_n._n_offset =
1767 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1774 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1776 /* This is a "short" name. Make it long. */
1777 unsigned long i = 0;
1778 char *newstring = NULL;
1780 /* find the length of this string without walking into memory
1782 for (i = 0; i < 8; ++i)
1784 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1787 } /* if end of string */
1788 } /* possible lengths of this string. */
1790 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1792 memset (newstring, 0, i);
1793 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1794 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1795 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1797 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1798 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1799 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1801 /* Long name already. Point symbol at the string in the
1803 if (string_table == NULL)
1805 string_table = _bfd_coff_read_string_table (abfd);
1806 if (string_table == NULL)
1809 internal_ptr->u.syment._n._n_n._n_offset =
1812 + internal_ptr->u.syment._n._n_n._n_offset));
1816 /* Long name in debug section. Very similar. */
1817 if (debug_section == NULL)
1818 debug_section = build_debug_section (abfd);
1819 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1820 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1823 internal_ptr += internal_ptr->u.syment.n_numaux;
1826 obj_raw_syments (abfd) = internal;
1827 BFD_ASSERT (obj_raw_syment_count (abfd)
1828 == (unsigned int) (internal_ptr - internal));
1831 } /* coff_get_normalized_symtab() */
1834 coff_get_reloc_upper_bound (abfd, asect)
1838 if (bfd_get_format (abfd) != bfd_object)
1840 bfd_set_error (bfd_error_invalid_operation);
1843 return (asect->reloc_count + 1) * sizeof (arelent *);
1847 coff_make_empty_symbol (abfd)
1850 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1853 memset (new, 0, sizeof *new);
1854 new->symbol.section = 0;
1856 new->lineno = (alent *) NULL;
1857 new->done_lineno = false;
1858 new->symbol.the_bfd = abfd;
1859 return &new->symbol;
1862 /* Make a debugging symbol. */
1865 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1867 PTR ptr ATTRIBUTE_UNUSED;
1868 unsigned long sz ATTRIBUTE_UNUSED;
1870 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1873 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1874 (but shouldn't be a constant). */
1875 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1878 new->symbol.section = bfd_abs_section_ptr;
1879 new->symbol.flags = BSF_DEBUGGING;
1880 new->lineno = (alent *) NULL;
1881 new->done_lineno = false;
1882 new->symbol.the_bfd = abfd;
1883 return &new->symbol;
1888 coff_get_symbol_info (abfd, symbol, ret)
1893 bfd_symbol_info (symbol, ret);
1894 if (coffsymbol (symbol)->native != NULL
1895 && coffsymbol (symbol)->native->fix_value)
1897 combined_entry_type *psym;
1899 psym = ((combined_entry_type *)
1900 coffsymbol (symbol)->native->u.syment.n_value);
1901 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1905 /* Return the COFF syment for a symbol. */
1908 bfd_coff_get_syment (abfd, symbol, psyment)
1911 struct internal_syment *psyment;
1913 coff_symbol_type *csym;
1915 csym = coff_symbol_from (abfd, symbol);
1916 if (csym == NULL || csym->native == NULL)
1918 bfd_set_error (bfd_error_invalid_operation);
1922 *psyment = csym->native->u.syment;
1924 if (csym->native->fix_value)
1925 psyment->n_value = ((combined_entry_type *) psyment->n_value
1926 - obj_raw_syments (abfd));
1928 /* FIXME: We should handle fix_line here. */
1933 /* Return the COFF auxent for a symbol. */
1936 bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1940 union internal_auxent *pauxent;
1942 coff_symbol_type *csym;
1943 combined_entry_type *ent;
1945 csym = coff_symbol_from (abfd, symbol);
1948 || csym->native == NULL
1949 || indx >= csym->native->u.syment.n_numaux)
1951 bfd_set_error (bfd_error_invalid_operation);
1955 ent = csym->native + indx + 1;
1957 *pauxent = ent->u.auxent;
1960 pauxent->x_sym.x_tagndx.l =
1961 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1962 - obj_raw_syments (abfd));
1965 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1966 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1967 - obj_raw_syments (abfd));
1969 if (ent->fix_scnlen)
1970 pauxent->x_csect.x_scnlen.l =
1971 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1972 - obj_raw_syments (abfd));
1977 /* Print out information about COFF symbol. */
1980 coff_print_symbol (abfd, filep, symbol, how)
1984 bfd_print_symbol_type how;
1986 FILE *file = (FILE *) filep;
1990 case bfd_print_symbol_name:
1991 fprintf (file, "%s", symbol->name);
1994 case bfd_print_symbol_more:
1995 fprintf (file, "coff %s %s",
1996 coffsymbol (symbol)->native ? "n" : "g",
1997 coffsymbol (symbol)->lineno ? "l" : " ");
2000 case bfd_print_symbol_all:
2001 if (coffsymbol (symbol)->native)
2005 combined_entry_type *combined = coffsymbol (symbol)->native;
2006 combined_entry_type *root = obj_raw_syments (abfd);
2007 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2009 fprintf (file, "[%3ld]", (long) (combined - root));
2011 if (! combined->fix_value)
2012 val = (unsigned long) combined->u.syment.n_value;
2014 val = ((unsigned long)
2015 ((combined_entry_type *) combined->u.syment.n_value
2019 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2020 combined->u.syment.n_scnum,
2021 combined->u.syment.n_flags,
2022 combined->u.syment.n_type,
2023 combined->u.syment.n_sclass,
2024 combined->u.syment.n_numaux,
2028 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2030 combined_entry_type *auxp = combined + aux + 1;
2034 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2036 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2038 fprintf (file, "\n");
2040 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2043 switch (combined->u.syment.n_sclass)
2046 fprintf (file, "File ");
2050 if (combined->u.syment.n_type == T_NULL)
2051 /* probably a section symbol? */
2053 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2054 (long) auxp->u.auxent.x_scn.x_scnlen,
2055 auxp->u.auxent.x_scn.x_nreloc,
2056 auxp->u.auxent.x_scn.x_nlinno);
2057 if (auxp->u.auxent.x_scn.x_checksum != 0
2058 || auxp->u.auxent.x_scn.x_associated != 0
2059 || auxp->u.auxent.x_scn.x_comdat != 0)
2060 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2061 auxp->u.auxent.x_scn.x_checksum,
2062 auxp->u.auxent.x_scn.x_associated,
2063 auxp->u.auxent.x_scn.x_comdat);
2066 /* else fall through */
2069 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2070 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2071 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2074 fprintf (file, " endndx %ld",
2076 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2084 fprintf (file, "\n%s :", l->u.sym->name);
2086 while (l->line_number)
2088 fprintf (file, "\n%4d : 0x%lx",
2091 (l->u.offset + symbol->section->vma)));
2098 bfd_print_symbol_vandf ((PTR) file, symbol);
2099 fprintf (file, " %-5s %s %s %s",
2100 symbol->section->name,
2101 coffsymbol (symbol)->native ? "n" : "g",
2102 coffsymbol (symbol)->lineno ? "l" : " ",
2108 /* Return whether a symbol name implies a local symbol. In COFF,
2109 local symbols generally start with ``.L''. Most targets use this
2110 function for the is_local_label_name entry point, but some may
2114 _bfd_coff_is_local_label_name (abfd, name)
2115 bfd *abfd ATTRIBUTE_UNUSED;
2118 return name[0] == '.' && name[1] == 'L';
2121 /* Provided a BFD, a section and an offset into the section, calculate
2122 and return the name of the source file and the line nearest to the
2127 coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2128 functionname_ptr, line_ptr)
2133 CONST char **filename_ptr;
2134 CONST char **functionname_ptr;
2135 unsigned int *line_ptr;
2139 unsigned int line_base;
2140 coff_data_type *cof = coff_data (abfd);
2141 /* Run through the raw syments if available */
2142 combined_entry_type *p;
2143 combined_entry_type *pend;
2145 struct coff_section_tdata *sec_data;
2147 /* Before looking through the symbol table, try to use a .stab
2148 section to find the information. */
2149 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2150 &found, filename_ptr,
2151 functionname_ptr, line_ptr,
2152 &coff_data (abfd)->line_info))
2158 *functionname_ptr = 0;
2161 /* Don't try and find line numbers in a non coff file */
2162 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2168 /* Find the first C_FILE symbol. */
2169 p = cof->raw_syments;
2173 pend = p + cof->raw_syment_count;
2176 if (p->u.syment.n_sclass == C_FILE)
2178 p += 1 + p->u.syment.n_numaux;
2186 /* Look through the C_FILE symbols to find the best one. */
2187 sec_vma = bfd_get_section_vma (abfd, section);
2188 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2189 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2192 combined_entry_type *p2;
2194 for (p2 = p + 1 + p->u.syment.n_numaux;
2196 p2 += 1 + p2->u.syment.n_numaux)
2198 if (p2->u.syment.n_scnum > 0
2200 == coff_section_from_bfd_index (abfd,
2201 p2->u.syment.n_scnum)))
2203 if (p2->u.syment.n_sclass == C_FILE)
2210 /* We use <= MAXDIFF here so that if we get a zero length
2211 file, we actually use the next file entry. */
2213 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
2214 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
2216 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2217 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2220 /* Avoid endless loops on erroneous files by ensuring that
2221 we always move forward in the file. */
2222 if (p - cof->raw_syments >= p->u.syment.n_value)
2225 p = cof->raw_syments + p->u.syment.n_value;
2226 if (p > pend || p->u.syment.n_sclass != C_FILE)
2231 /* Now wander though the raw linenumbers of the section */
2232 /* If we have been called on this section before, and the offset we
2233 want is further down then we can prime the lookup loop. */
2234 sec_data = coff_section_data (abfd, section);
2235 if (sec_data != NULL
2237 && offset >= sec_data->offset)
2240 *functionname_ptr = sec_data->function;
2241 line_base = sec_data->line_base;
2249 if (section->lineno != NULL)
2251 bfd_vma last_value = 0;
2253 l = §ion->lineno[i];
2255 for (; i < section->lineno_count; i++)
2257 if (l->line_number == 0)
2259 /* Get the symbol this line number points at */
2260 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2261 if (coff->symbol.value > offset)
2263 *functionname_ptr = coff->symbol.name;
2264 last_value = coff->symbol.value;
2267 combined_entry_type *s = coff->native;
2268 s = s + 1 + s->u.syment.n_numaux;
2270 /* In XCOFF a debugging symbol can follow the
2272 if (s->u.syment.n_scnum == N_DEBUG)
2273 s = s + 1 + s->u.syment.n_numaux;
2275 /* S should now point to the .bf of the function. */
2276 if (s->u.syment.n_numaux)
2278 /* The linenumber is stored in the auxent. */
2279 union internal_auxent *a = &((s + 1)->u.auxent);
2280 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2281 *line_ptr = line_base;
2287 if (l->u.offset > offset)
2289 *line_ptr = l->line_number + line_base - 1;
2294 /* If we fell off the end of the loop, then assume that this
2295 symbol has no line number info. Otherwise, symbols with no
2296 line number info get reported with the line number of the
2297 last line of the last symbol which does have line number
2298 info. We use 0x100 as a slop to account for cases where the
2299 last line has executable code. */
2300 if (i >= section->lineno_count
2302 && offset - last_value > 0x100)
2304 *functionname_ptr = NULL;
2309 /* Cache the results for the next call. */
2310 if (sec_data == NULL && section->owner == abfd)
2312 section->used_by_bfd =
2313 ((PTR) bfd_zalloc (abfd,
2314 sizeof (struct coff_section_tdata)));
2315 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2317 if (sec_data != NULL)
2319 sec_data->offset = offset;
2321 sec_data->function = *functionname_ptr;
2322 sec_data->line_base = line_base;
2329 coff_sizeof_headers (abfd, reloc)
2337 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2341 size = bfd_coff_filhsz (abfd);
2344 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2348 /* Change the class of a coff symbol held by BFD. */
2350 bfd_coff_set_symbol_class (abfd, symbol, class)
2355 coff_symbol_type * csym;
2357 csym = coff_symbol_from (abfd, symbol);
2360 bfd_set_error (bfd_error_invalid_operation);
2363 else if (csym->native == NULL)
2365 /* This is an alien symbol which no native coff backend data.
2366 We cheat here by creating a fake native entry for it and
2367 then filling in the class. This code is based on that in
2368 coff_write_alien_symbol(). */
2370 combined_entry_type * native;
2372 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2376 memset (native, 0, sizeof (* native));
2378 native->u.syment.n_type = T_NULL;
2379 native->u.syment.n_sclass = class;
2381 if (bfd_is_und_section (symbol->section))
2383 native->u.syment.n_scnum = N_UNDEF;
2384 native->u.syment.n_value = symbol->value;
2386 else if (bfd_is_com_section (symbol->section))
2388 native->u.syment.n_scnum = N_UNDEF;
2389 native->u.syment.n_value = symbol->value;
2393 native->u.syment.n_scnum =
2394 symbol->section->output_section->target_index;
2395 native->u.syment.n_value = (symbol->value
2396 + symbol->section->output_offset);
2397 if (! obj_pe (abfd))
2398 native->u.syment.n_value += symbol->section->output_section->vma;
2400 /* Copy the any flags from the the file header into the symbol.
2402 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2405 csym->native = native;
2409 csym->native->u.syment.n_sclass = class;