1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* This file contains COFF code that is not dependent on any
25 particular COFF target. There is only one version of this file in
26 libbfd.a, so no target specific code may be put in here. Or, to
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
41 #include "coff/internal.h"
44 static void coff_fix_symbol_name
45 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
46 asection **, bfd_size_type *));
47 static boolean coff_write_symbol
48 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
49 bfd_size_type *, asection **, bfd_size_type *));
50 static boolean coff_write_alien_symbol
51 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
52 asection **, bfd_size_type *));
53 static boolean coff_write_native_symbol
54 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
55 asection **, bfd_size_type *));
56 static void coff_pointerize_aux
57 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
58 unsigned int, combined_entry_type *));
60 #define STRING_SIZE_SIZE (4)
62 /* Take a section header read from a coff file (in HOST byte order),
63 and make a BFD "section" out of it. This is used by ECOFF. */
65 make_a_section_from_file (abfd, hdr, target_index)
67 struct internal_scnhdr *hdr;
68 unsigned int target_index;
70 asection *return_section;
73 /* Assorted wastage to null-terminate the name, thanks AT&T! */
74 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
77 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
78 name[sizeof (hdr->s_name)] = 0;
80 return_section = bfd_make_section_anyway (abfd, name);
81 if (return_section == NULL)
84 return_section->vma = hdr->s_vaddr;
85 return_section->lma = hdr->s_paddr;
86 return_section->_raw_size = hdr->s_size;
87 return_section->filepos = hdr->s_scnptr;
88 return_section->rel_filepos = hdr->s_relptr;
89 return_section->reloc_count = hdr->s_nreloc;
91 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
93 return_section->line_filepos = hdr->s_lnnoptr;
95 return_section->lineno_count = hdr->s_nlnno;
96 return_section->userdata = NULL;
97 return_section->next = (asection *) NULL;
98 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
100 return_section->target_index = target_index;
102 /* At least on i386-coff, the line number count for a shared library
103 section must be ignored. */
104 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
105 return_section->lineno_count = 0;
107 if (hdr->s_nreloc != 0)
108 return_section->flags |= SEC_RELOC;
109 /* FIXME: should this check 'hdr->s_size > 0' */
110 if (hdr->s_scnptr != 0)
111 return_section->flags |= SEC_HAS_CONTENTS;
115 /* Read in a COFF object and make it into a BFD. This is used by
118 static const bfd_target *
119 coff_real_object_p (abfd, nscns, internal_f, internal_a)
122 struct internal_filehdr *internal_f;
123 struct internal_aouthdr *internal_a;
125 flagword oflags = abfd->flags;
126 bfd_vma ostart = bfd_get_start_address (abfd);
128 size_t readsize; /* length of file_info */
130 char *external_sections;
132 if (!(internal_f->f_flags & F_RELFLG))
133 abfd->flags |= HAS_RELOC;
134 if ((internal_f->f_flags & F_EXEC))
135 abfd->flags |= EXEC_P;
136 if (!(internal_f->f_flags & F_LNNO))
137 abfd->flags |= HAS_LINENO;
138 if (!(internal_f->f_flags & F_LSYMS))
139 abfd->flags |= HAS_LOCALS;
141 /* FIXME: How can we set D_PAGED correctly? */
142 if ((internal_f->f_flags & F_EXEC) != 0)
143 abfd->flags |= D_PAGED;
145 bfd_get_symcount (abfd) = internal_f->f_nsyms;
146 if (internal_f->f_nsyms)
147 abfd->flags |= HAS_SYMS;
149 if (internal_a != (struct internal_aouthdr *) NULL)
150 bfd_get_start_address (abfd) = internal_a->entry;
152 bfd_get_start_address (abfd) = 0;
154 /* Set up the tdata area. ECOFF uses its own routine, and overrides
156 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
160 scnhsz = bfd_coff_scnhsz (abfd);
161 readsize = nscns * scnhsz;
162 external_sections = (char *) bfd_alloc (abfd, readsize);
163 if (!external_sections)
166 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
169 /* Now copy data as required; construct all asections etc */
173 for (i = 0; i < nscns; i++)
175 struct internal_scnhdr tmp;
176 bfd_coff_swap_scnhdr_in (abfd,
177 (PTR) (external_sections + i * scnhsz),
179 make_a_section_from_file (abfd, &tmp, i + 1);
183 /* make_abs_section (abfd); */
185 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
191 bfd_release (abfd, tdata);
192 abfd->flags = oflags;
193 bfd_get_start_address (abfd) = ostart;
194 return (const bfd_target *) NULL;
197 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
198 not a COFF file. This is also used by ECOFF. */
208 struct internal_filehdr internal_f;
209 struct internal_aouthdr internal_a;
211 /* figure out how much to read */
212 filhsz = bfd_coff_filhsz (abfd);
213 aoutsz = bfd_coff_aoutsz (abfd);
215 filehdr = bfd_alloc (abfd, filhsz);
218 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
220 if (bfd_get_error () != bfd_error_system_call)
221 bfd_set_error (bfd_error_wrong_format);
224 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
225 bfd_release (abfd, filehdr);
227 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
229 bfd_set_error (bfd_error_wrong_format);
232 nscns = internal_f.f_nscns;
234 if (internal_f.f_opthdr)
238 opthdr = bfd_alloc (abfd, aoutsz);
241 if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
245 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
248 /* Seek past the opt hdr stuff */
249 if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
253 return coff_real_object_p (abfd, nscns, &internal_f,
254 (internal_f.f_opthdr != 0
256 : (struct internal_aouthdr *) NULL));
259 /* Get the BFD section from a COFF symbol section number. */
262 coff_section_from_bfd_index (abfd, index)
266 struct sec *answer = abfd->sections;
269 return bfd_abs_section_ptr;
270 if (index == N_UNDEF)
271 return bfd_und_section_ptr;
272 if (index == N_DEBUG)
273 return bfd_abs_section_ptr;
277 if (answer->target_index == index)
279 answer = answer->next;
282 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
283 has a bad symbol table in biglitpow.o. */
284 return bfd_und_section_ptr;
287 /* Get the upper bound of a COFF symbol table. */
290 coff_get_symtab_upper_bound (abfd)
293 if (!bfd_coff_slurp_symbol_table (abfd))
296 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
300 /* Canonicalize a COFF symbol table. */
303 coff_get_symtab (abfd, alocation)
307 unsigned int counter;
308 coff_symbol_type *symbase;
309 coff_symbol_type **location = (coff_symbol_type **) alocation;
311 if (!bfd_coff_slurp_symbol_table (abfd))
314 symbase = obj_symbols (abfd);
315 counter = bfd_get_symcount (abfd);
316 while (counter-- > 0)
317 *location++ = symbase++;
321 return bfd_get_symcount (abfd);
324 /* Get the name of a symbol. The caller must pass in a buffer of size
328 _bfd_coff_internal_syment_name (abfd, sym, buf)
330 const struct internal_syment *sym;
333 /* FIXME: It's not clear this will work correctly if sizeof
335 if (sym->_n._n_n._n_zeroes != 0
336 || sym->_n._n_n._n_offset == 0)
338 memcpy (buf, sym->_n._n_name, SYMNMLEN);
339 buf[SYMNMLEN] = '\0';
346 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
347 strings = obj_coff_strings (abfd);
350 strings = _bfd_coff_read_string_table (abfd);
354 return strings + sym->_n._n_n._n_offset;
358 /* Read in and swap the relocs. This returns a buffer holding the
359 relocs for section SEC in file ABFD. If CACHE is true and
360 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
361 the function is called again. If EXTERNAL_RELOCS is not NULL, it
362 is a buffer large enough to hold the unswapped relocs. If
363 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
364 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
365 value must be INTERNAL_RELOCS. The function returns NULL on error. */
367 struct internal_reloc *
368 _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
369 require_internal, internal_relocs)
373 bfd_byte *external_relocs;
374 boolean require_internal;
375 struct internal_reloc *internal_relocs;
378 bfd_byte *free_external = NULL;
379 struct internal_reloc *free_internal = NULL;
382 struct internal_reloc *irel;
384 if (coff_section_data (abfd, sec) != NULL
385 && coff_section_data (abfd, sec)->relocs != NULL)
387 if (! require_internal)
388 return coff_section_data (abfd, sec)->relocs;
389 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
390 sec->reloc_count * sizeof (struct internal_reloc));
391 return internal_relocs;
394 relsz = bfd_coff_relsz (abfd);
396 if (external_relocs == NULL)
398 free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
399 if (free_external == NULL && sec->reloc_count > 0)
401 external_relocs = free_external;
404 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
405 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
406 != relsz * sec->reloc_count))
409 if (internal_relocs == NULL)
411 free_internal = ((struct internal_reloc *)
412 bfd_malloc (sec->reloc_count
413 * sizeof (struct internal_reloc)));
414 if (free_internal == NULL && sec->reloc_count > 0)
416 internal_relocs = free_internal;
419 /* Swap in the relocs. */
420 erel = external_relocs;
421 erel_end = erel + relsz * sec->reloc_count;
422 irel = internal_relocs;
423 for (; erel < erel_end; erel += relsz, irel++)
424 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
426 if (free_external != NULL)
428 free (free_external);
429 free_external = NULL;
432 if (cache && free_internal != NULL)
434 if (coff_section_data (abfd, sec) == NULL)
437 (PTR) bfd_zalloc (abfd,
438 sizeof (struct coff_section_tdata));
439 if (sec->used_by_bfd == NULL)
441 coff_section_data (abfd, sec)->contents = NULL;
443 coff_section_data (abfd, sec)->relocs = free_internal;
446 return internal_relocs;
449 if (free_external != NULL)
450 free (free_external);
451 if (free_internal != NULL)
452 free (free_internal);
456 /* Set lineno_count for the output sections of a COFF file. */
459 coff_count_linenumbers (abfd)
462 unsigned int limit = bfd_get_symcount (abfd);
470 /* This may be from the backend linker, in which case the
471 lineno_count in the sections is correct. */
472 for (s = abfd->sections; s != NULL; s = s->next)
473 total += s->lineno_count;
477 for (s = abfd->sections; s != NULL; s = s->next)
478 BFD_ASSERT (s->lineno_count == 0);
480 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
482 asymbol *q_maybe = *p;
484 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
486 coff_symbol_type *q = coffsymbol (q_maybe);
488 /* The AIX 4.1 compiler can sometimes generate line numbers
489 attached to debugging symbols. We try to simply ignore
491 if (q->lineno != NULL
492 && q->symbol.section->owner != NULL)
494 /* This symbol has line numbers. Increment the owning
495 section's linenumber count. */
496 alent *l = q->lineno;
498 ++q->symbol.section->output_section->lineno_count;
501 while (l->line_number != 0)
504 ++q->symbol.section->output_section->lineno_count;
514 /* Takes a bfd and a symbol, returns a pointer to the coff specific
515 area of the symbol if there is one. */
519 coff_symbol_from (ignore_abfd, symbol)
523 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
524 return (coff_symbol_type *) NULL;
526 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
527 return (coff_symbol_type *) NULL;
529 return (coff_symbol_type *) symbol;
533 fixup_symbol_value (coff_symbol_ptr, syment)
534 coff_symbol_type *coff_symbol_ptr;
535 struct internal_syment *syment;
538 /* Normalize the symbol flags */
539 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
541 /* a common symbol is undefined with a value */
542 syment->n_scnum = N_UNDEF;
543 syment->n_value = coff_symbol_ptr->symbol.value;
545 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
547 syment->n_value = coff_symbol_ptr->symbol.value;
549 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
551 syment->n_scnum = N_UNDEF;
556 if (coff_symbol_ptr->symbol.section)
559 coff_symbol_ptr->symbol.section->output_section->target_index;
562 coff_symbol_ptr->symbol.value +
563 coff_symbol_ptr->symbol.section->output_offset +
564 coff_symbol_ptr->symbol.section->output_section->vma;
570 syment->n_scnum = N_ABS;
571 syment->n_value = coff_symbol_ptr->symbol.value;
576 /* Run through all the symbols in the symbol table and work out what
577 their indexes into the symbol table will be when output.
579 Coff requires that each C_FILE symbol points to the next one in the
580 chain, and that the last one points to the first external symbol. We
584 coff_renumber_symbols (bfd_ptr, first_undef)
588 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
589 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
590 unsigned int native_index = 0;
591 struct internal_syment *last_file = (struct internal_syment *) NULL;
592 unsigned int symbol_index;
594 /* COFF demands that undefined symbols come after all other symbols.
595 Since we don't need to impose this extra knowledge on all our
596 client programs, deal with that here. Sort the symbol table;
597 just move the undefined symbols to the end, leaving the rest
598 alone. The O'Reilly book says that defined global symbols come
599 at the end before the undefined symbols, so we do that here as
601 /* @@ Do we have some condition we could test for, so we don't always
602 have to do this? I don't think relocatability is quite right, but
603 I'm not certain. [raeburn:19920508.1711EST] */
608 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
610 * (symbol_count + 1));
613 bfd_ptr->outsymbols = newsyms;
614 for (i = 0; i < symbol_count; i++)
615 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
616 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
617 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
618 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
620 *newsyms++ = symbol_ptr_ptr[i];
622 for (i = 0; i < symbol_count; i++)
623 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
624 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
625 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
629 *newsyms++ = symbol_ptr_ptr[i];
631 *first_undef = newsyms - bfd_ptr->outsymbols;
633 for (i = 0; i < symbol_count; i++)
634 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
635 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
636 *newsyms++ = symbol_ptr_ptr[i];
637 *newsyms = (asymbol *) NULL;
638 symbol_ptr_ptr = bfd_ptr->outsymbols;
641 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
643 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
644 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
645 if (coff_symbol_ptr && coff_symbol_ptr->native)
647 combined_entry_type *s = coff_symbol_ptr->native;
650 if (s->u.syment.n_sclass == C_FILE)
652 if (last_file != (struct internal_syment *) NULL)
653 last_file->n_value = native_index;
654 last_file = &(s->u.syment);
659 /* Modify the symbol values according to their section and
662 fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
664 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
665 s[i].offset = native_index++;
672 obj_conv_table_size (bfd_ptr) = native_index;
677 /* Run thorough the symbol table again, and fix it so that all
678 pointers to entries are changed to the entries' index in the output
682 coff_mangle_symbols (bfd_ptr)
685 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
686 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
687 unsigned int symbol_index;
689 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
691 coff_symbol_type *coff_symbol_ptr =
692 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
694 if (coff_symbol_ptr && coff_symbol_ptr->native)
697 combined_entry_type *s = coff_symbol_ptr->native;
701 /* FIXME: We should use a union here. */
702 s->u.syment.n_value =
703 ((combined_entry_type *) s->u.syment.n_value)->offset;
708 /* The value is the offset into the line number entries
709 for the symbol's section. On output, the symbol's
710 section should be N_DEBUG. */
711 s->u.syment.n_value =
712 (coff_symbol_ptr->symbol.section->output_section->line_filepos
713 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
714 coff_symbol_ptr->symbol.section =
715 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
716 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
718 for (i = 0; i < s->u.syment.n_numaux; i++)
720 combined_entry_type *a = s + i + 1;
723 a->u.auxent.x_sym.x_tagndx.l =
724 a->u.auxent.x_sym.x_tagndx.p->offset;
729 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
730 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
735 a->u.auxent.x_csect.x_scnlen.l =
736 a->u.auxent.x_csect.x_scnlen.p->offset;
745 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
746 debug_string_section_p, debug_string_size_p)
749 combined_entry_type *native;
750 bfd_size_type *string_size_p;
751 asection **debug_string_section_p;
752 bfd_size_type *debug_string_size_p;
754 unsigned int name_length;
755 union internal_auxent *auxent;
756 char *name = (char *) (symbol->name);
758 if (name == (char *) NULL)
760 /* coff symbols always have names, so we'll make one up */
761 symbol->name = "strange";
762 name = (char *) symbol->name;
764 name_length = strlen (name);
766 if (native->u.syment.n_sclass == C_FILE
767 && native->u.syment.n_numaux > 0)
769 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
770 auxent = &(native + 1)->u.auxent;
772 if (bfd_coff_long_filenames (abfd))
774 if (name_length <= FILNMLEN)
776 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
780 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
781 auxent->x_file.x_n.x_zeroes = 0;
782 *string_size_p += name_length + 1;
787 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
788 if (name_length > FILNMLEN)
790 name[FILNMLEN] = '\0';
796 if (name_length <= SYMNMLEN)
798 /* This name will fit into the symbol neatly */
799 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
801 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
803 native->u.syment._n._n_n._n_offset = (*string_size_p
805 native->u.syment._n._n_n._n_zeroes = 0;
806 *string_size_p += name_length + 1;
813 /* This name should be written into the .debug section. For
814 some reason each name is preceded by a two byte length
815 and also followed by a null byte. FIXME: We assume that
816 the .debug section has already been created, and that it
818 if (*debug_string_section_p == (asection *) NULL)
819 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
820 filepos = bfd_tell (abfd);
821 bfd_put_16 (abfd, name_length + 1, buf);
822 if (!bfd_set_section_contents (abfd,
823 *debug_string_section_p,
825 (file_ptr) *debug_string_size_p,
827 || !bfd_set_section_contents (abfd,
828 *debug_string_section_p,
830 ((file_ptr) *debug_string_size_p
832 (bfd_size_type) name_length + 1))
834 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
836 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
837 native->u.syment._n._n_n._n_zeroes = 0;
838 *debug_string_size_p += name_length + 3;
843 /* We need to keep track of the symbol index so that when we write out
844 the relocs we can get the index for a symbol. This method is a
847 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
849 /* Write a symbol out to a COFF file. */
852 coff_write_symbol (abfd, symbol, native, written, string_size_p,
853 debug_string_section_p, debug_string_size_p)
856 combined_entry_type *native;
857 unsigned int *written;
858 bfd_size_type *string_size_p;
859 asection **debug_string_section_p;
860 bfd_size_type *debug_string_size_p;
862 unsigned int numaux = native->u.syment.n_numaux;
863 int type = native->u.syment.n_type;
864 int class = native->u.syment.n_sclass;
866 bfd_size_type symesz;
868 if (native->u.syment.n_sclass == C_FILE)
869 symbol->flags |= BSF_DEBUGGING;
871 if (symbol->flags & BSF_DEBUGGING
872 && bfd_is_abs_section (symbol->section))
874 native->u.syment.n_scnum = N_DEBUG;
876 else if (bfd_is_abs_section (symbol->section))
878 native->u.syment.n_scnum = N_ABS;
880 else if (bfd_is_und_section (symbol->section))
882 native->u.syment.n_scnum = N_UNDEF;
886 native->u.syment.n_scnum =
887 symbol->section->output_section->target_index;
890 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
891 debug_string_section_p, debug_string_size_p);
893 symesz = bfd_coff_symesz (abfd);
894 buf = bfd_alloc (abfd, symesz);
897 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
898 if (bfd_write (buf, 1, symesz, abfd) != symesz)
900 bfd_release (abfd, buf);
902 if (native->u.syment.n_numaux > 0)
904 bfd_size_type auxesz;
907 auxesz = bfd_coff_auxesz (abfd);
908 buf = bfd_alloc (abfd, auxesz);
911 for (j = 0; j < native->u.syment.n_numaux; j++)
913 bfd_coff_swap_aux_out (abfd,
914 &((native + j + 1)->u.auxent),
918 native->u.syment.n_numaux,
920 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
923 bfd_release (abfd, buf);
926 /* Store the index for use when we write out the relocs. */
927 set_index (symbol, *written);
929 *written += numaux + 1;
933 /* Write out a symbol to a COFF file that does not come from a COFF
934 file originally. This symbol may have been created by the linker,
935 or we may be linking a non COFF file to a COFF file. */
938 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
939 debug_string_section_p, debug_string_size_p)
942 unsigned int *written;
943 bfd_size_type *string_size_p;
944 asection **debug_string_section_p;
945 bfd_size_type *debug_string_size_p;
947 combined_entry_type *native;
948 combined_entry_type dummy;
951 native->u.syment.n_type = T_NULL;
952 native->u.syment.n_flags = 0;
953 if (bfd_is_und_section (symbol->section))
955 native->u.syment.n_scnum = N_UNDEF;
956 native->u.syment.n_value = symbol->value;
958 else if (bfd_is_com_section (symbol->section))
960 native->u.syment.n_scnum = N_UNDEF;
961 native->u.syment.n_value = symbol->value;
963 else if (symbol->flags & BSF_DEBUGGING)
965 /* There isn't much point to writing out a debugging symbol
966 unless we are prepared to convert it into COFF debugging
967 format. So, we just ignore them. We must clobber the symbol
968 name to keep it from being put in the string table. */
974 native->u.syment.n_scnum =
975 symbol->section->output_section->target_index;
976 native->u.syment.n_value = (symbol->value
977 + symbol->section->output_section->vma
978 + symbol->section->output_offset);
980 /* Copy the any flags from the the file header into the symbol.
983 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
984 if (c != (coff_symbol_type *) NULL)
985 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
989 native->u.syment.n_type = 0;
990 if (symbol->flags & BSF_LOCAL)
991 native->u.syment.n_sclass = C_STAT;
993 native->u.syment.n_sclass = C_EXT;
994 native->u.syment.n_numaux = 0;
996 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
997 debug_string_section_p, debug_string_size_p);
1000 /* Write a native symbol to a COFF file. */
1003 coff_write_native_symbol (abfd, symbol, written, string_size_p,
1004 debug_string_section_p, debug_string_size_p)
1006 coff_symbol_type *symbol;
1007 unsigned int *written;
1008 bfd_size_type *string_size_p;
1009 asection **debug_string_section_p;
1010 bfd_size_type *debug_string_size_p;
1012 combined_entry_type *native = symbol->native;
1013 alent *lineno = symbol->lineno;
1015 /* If this symbol has an associated line number, we must store the
1016 symbol index in the line number field. We also tag the auxent to
1017 point to the right place in the lineno table. */
1018 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1020 unsigned int count = 0;
1021 lineno[count].u.offset = *written;
1022 if (native->u.syment.n_numaux)
1024 union internal_auxent *a = &((native + 1)->u.auxent);
1026 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1027 symbol->symbol.section->output_section->moving_line_filepos;
1030 /* Count and relocate all other linenumbers. */
1032 while (lineno[count].line_number != 0)
1036 I've been told this, but still need proof:
1037 > The second bug is also in `bfd/coffcode.h'. This bug
1038 > causes the linker to screw up the pc-relocations for
1039 > all the line numbers in COFF code. This bug isn't only
1040 > specific to A29K implementations, but affects all
1041 > systems using COFF format binaries. Note that in COFF
1042 > object files, the line number core offsets output by
1043 > the assembler are relative to the start of each
1044 > procedure, not to the start of the .text section. This
1045 > patch relocates the line numbers relative to the
1046 > `native->u.syment.n_value' instead of the section
1050 lineno[count].u.offset += native->u.syment.n_value;
1052 lineno[count].u.offset +=
1053 (symbol->symbol.section->output_section->vma
1054 + symbol->symbol.section->output_offset);
1058 symbol->done_lineno = true;
1060 symbol->symbol.section->output_section->moving_line_filepos +=
1061 count * bfd_coff_linesz (abfd);
1064 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1065 string_size_p, debug_string_section_p,
1066 debug_string_size_p);
1069 /* Write out the COFF symbols. */
1072 coff_write_symbols (abfd)
1075 bfd_size_type string_size;
1076 asection *debug_string_section;
1077 bfd_size_type debug_string_size;
1079 unsigned int limit = bfd_get_symcount (abfd);
1080 unsigned int written = 0;
1084 debug_string_section = NULL;
1085 debug_string_size = 0;
1087 /* Seek to the right place */
1088 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1091 /* Output all the symbols we have */
1094 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1096 asymbol *symbol = *p;
1097 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1099 if (c_symbol == (coff_symbol_type *) NULL
1100 || c_symbol->native == (combined_entry_type *) NULL)
1102 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1103 &debug_string_section,
1104 &debug_string_size))
1109 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1110 &string_size, &debug_string_section,
1111 &debug_string_size))
1116 obj_raw_syment_count (abfd) = written;
1118 /* Now write out strings */
1120 if (string_size != 0)
1122 unsigned int size = string_size + STRING_SIZE_SIZE;
1123 bfd_byte buffer[STRING_SIZE_SIZE];
1125 #if STRING_SIZE_SIZE == 4
1126 bfd_h_put_32 (abfd, size, buffer);
1128 #error Change bfd_h_put_32
1130 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1132 for (p = abfd->outsymbols, i = 0;
1137 size_t name_length = strlen (q->name);
1138 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1141 /* Figure out whether the symbol name should go in the string
1142 table. Symbol names that are short enough are stored
1143 directly in the syment structure. File names permit a
1144 different, longer, length in the syment structure. On
1145 XCOFF, some symbol names are stored in the .debug section
1146 rather than in the string table. */
1148 if (c_symbol == NULL
1149 || c_symbol->native == NULL)
1151 /* This is not a COFF symbol, so it certainly is not a
1152 file name, nor does it go in the .debug section. */
1155 else if (bfd_coff_symname_in_debug (abfd,
1156 &c_symbol->native->u.syment))
1158 /* This symbol name is in the XCOFF .debug section.
1159 Don't write it into the string table. */
1160 maxlen = name_length;
1162 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1163 && c_symbol->native->u.syment.n_numaux > 0)
1168 if (name_length > maxlen)
1170 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1178 /* We would normally not write anything here, but we'll write
1179 out 4 so that any stupid coff reader which tries to read the
1180 string table even when there isn't one won't croak. */
1181 unsigned int size = STRING_SIZE_SIZE;
1182 bfd_byte buffer[STRING_SIZE_SIZE];
1184 #if STRING_SIZE_SIZE == 4
1185 bfd_h_put_32 (abfd, size, buffer);
1187 #error Change bfd_h_put_32
1189 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1190 != STRING_SIZE_SIZE)
1194 /* Make sure the .debug section was created to be the correct size.
1195 We should create it ourselves on the fly, but we don't because
1196 BFD won't let us write to any section until we know how large all
1197 the sections are. We could still do it by making another pass
1198 over the symbols. FIXME. */
1199 BFD_ASSERT (debug_string_size == 0
1200 || (debug_string_section != (asection *) NULL
1201 && (BFD_ALIGN (debug_string_size,
1202 1 << debug_string_section->alignment_power)
1203 == bfd_section_size (abfd, debug_string_section))));
1209 coff_write_linenumbers (abfd)
1213 bfd_size_type linesz;
1216 linesz = bfd_coff_linesz (abfd);
1217 buff = bfd_alloc (abfd, linesz);
1220 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1222 if (s->lineno_count)
1224 asymbol **q = abfd->outsymbols;
1225 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1227 /* Find all the linenumbers in this section */
1231 if (p->section->output_section == s)
1234 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1235 (bfd_asymbol_bfd (p), p));
1238 /* Found a linenumber entry, output */
1239 struct internal_lineno out;
1240 memset ((PTR) & out, 0, sizeof (out));
1242 out.l_addr.l_symndx = l->u.offset;
1243 bfd_coff_swap_lineno_out (abfd, &out, buff);
1244 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1247 while (l->line_number)
1249 out.l_lnno = l->line_number;
1250 out.l_addr.l_symndx = l->u.offset;
1251 bfd_coff_swap_lineno_out (abfd, &out, buff);
1252 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1262 bfd_release (abfd, buff);
1268 coff_get_lineno (ignore_abfd, symbol)
1272 return coffsymbol (symbol)->lineno;
1276 coff_section_symbol (abfd, name)
1280 asection *sec = bfd_make_section_old_way (abfd, name);
1282 combined_entry_type *csym;
1285 csym = coff_symbol_from (abfd, sym)->native;
1286 /* Make sure back-end COFF stuff is there. */
1291 coff_symbol_type sym;
1292 /* @@FIXME This shouldn't use a fixed size!! */
1293 combined_entry_type e[10];
1296 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1299 bfd_set_error (bfd_error_no_error);
1302 memset ((char *) f, 0, sizeof (*f));
1303 coff_symbol_from (abfd, sym)->native = csym = f->e;
1305 csym[0].u.syment.n_sclass = C_STAT;
1306 csym[0].u.syment.n_numaux = 1;
1307 /* SF_SET_STATICS (sym); @@ ??? */
1308 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1309 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1310 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1312 if (sec->output_section == NULL)
1314 sec->output_section = sec;
1315 sec->output_offset = 0;
1321 /* This function transforms the offsets into the symbol table into
1322 pointers to syments. */
1325 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1327 combined_entry_type *table_base;
1328 combined_entry_type *symbol;
1329 unsigned int indaux;
1330 combined_entry_type *auxent;
1332 int type = symbol->u.syment.n_type;
1333 int class = symbol->u.syment.n_sclass;
1335 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1337 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1338 (abfd, table_base, symbol, indaux, auxent))
1342 /* Don't bother if this is a file or a section */
1343 if (class == C_STAT && type == T_NULL)
1345 if (class == C_FILE)
1348 /* Otherwise patch up */
1349 #define N_TMASK coff_data (abfd)->local_n_tmask
1350 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1351 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK)
1352 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1354 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1355 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1356 auxent->fix_end = 1;
1358 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1359 generate one, so we must be careful to ignore it. */
1360 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1362 auxent->u.auxent.x_sym.x_tagndx.p =
1363 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1364 auxent->fix_tag = 1;
1368 /* Allocate space for the ".debug" section, and read it.
1369 We did not read the debug section until now, because
1370 we didn't want to go to the trouble until someone needed it. */
1373 build_debug_section (abfd)
1376 char *debug_section;
1379 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1383 bfd_set_error (bfd_error_no_debug_section);
1387 debug_section = (PTR) bfd_alloc (abfd,
1388 bfd_get_section_size_before_reloc (sect));
1389 if (debug_section == NULL)
1392 /* Seek to the beginning of the `.debug' section and read it.
1393 Save the current position first; it is needed by our caller.
1394 Then read debug section and reset the file pointer. */
1396 position = bfd_tell (abfd);
1397 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1398 || (bfd_read (debug_section,
1399 bfd_get_section_size_before_reloc (sect), 1, abfd)
1400 != bfd_get_section_size_before_reloc (sect))
1401 || bfd_seek (abfd, position, SEEK_SET) != 0)
1403 return debug_section;
1407 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1408 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1409 be \0-terminated. */
1411 copy_name (abfd, name, maxlen)
1419 for (len = 0; len < maxlen; ++len)
1421 if (name[len] == '\0')
1427 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1429 strncpy (newname, name, len);
1430 newname[len] = '\0';
1434 /* Read in the external symbols. */
1437 _bfd_coff_get_external_symbols (abfd)
1440 bfd_size_type symesz;
1444 if (obj_coff_external_syms (abfd) != NULL)
1447 symesz = bfd_coff_symesz (abfd);
1449 size = obj_raw_syment_count (abfd) * symesz;
1451 syms = (PTR) bfd_malloc (size);
1452 if (syms == NULL && size != 0)
1455 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1456 || bfd_read (syms, size, 1, abfd) != size)
1463 obj_coff_external_syms (abfd) = syms;
1468 /* Read in the external strings. The strings are not loaded until
1469 they are needed. This is because we have no simple way of
1470 detecting a missing string table in an archive. */
1473 _bfd_coff_read_string_table (abfd)
1476 char extstrsize[STRING_SIZE_SIZE];
1480 if (obj_coff_strings (abfd) != NULL)
1481 return obj_coff_strings (abfd);
1484 (obj_sym_filepos (abfd)
1485 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1489 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1491 if (bfd_get_error () != bfd_error_file_truncated)
1494 /* There is no string table. */
1495 strsize = STRING_SIZE_SIZE;
1499 #if STRING_SIZE_SIZE == 4
1500 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1502 #error Change bfd_h_get_32
1506 if (strsize < STRING_SIZE_SIZE)
1508 (*_bfd_error_handler)
1509 ("%s: bad string table size %lu", bfd_get_filename (abfd),
1510 (unsigned long) strsize);
1511 bfd_set_error (bfd_error_bad_value);
1515 strings = (char *) bfd_malloc (strsize);
1516 if (strings == NULL)
1519 if (bfd_read (strings + STRING_SIZE_SIZE,
1520 strsize - STRING_SIZE_SIZE, 1, abfd)
1521 != strsize - STRING_SIZE_SIZE)
1527 obj_coff_strings (abfd) = strings;
1532 /* Free up the external symbols and strings read from a COFF file. */
1535 _bfd_coff_free_symbols (abfd)
1538 if (obj_coff_external_syms (abfd) != NULL
1539 && ! obj_coff_keep_syms (abfd))
1541 free (obj_coff_external_syms (abfd));
1542 obj_coff_external_syms (abfd) = NULL;
1544 if (obj_coff_strings (abfd) != NULL
1545 && ! obj_coff_keep_strings (abfd))
1547 free (obj_coff_strings (abfd));
1548 obj_coff_strings (abfd) = NULL;
1553 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1554 knit the symbol names into a normalized form. By normalized here I
1555 mean that all symbols have an n_offset pointer that points to a null-
1556 terminated string. */
1558 combined_entry_type *
1559 coff_get_normalized_symtab (abfd)
1562 combined_entry_type *internal;
1563 combined_entry_type *internal_ptr;
1564 combined_entry_type *symbol_ptr;
1565 combined_entry_type *internal_end;
1566 bfd_size_type symesz;
1569 const char *string_table = NULL;
1570 char *debug_section = NULL;
1573 if (obj_raw_syments (abfd) != NULL)
1574 return obj_raw_syments (abfd);
1576 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1577 internal = (combined_entry_type *) bfd_alloc (abfd, size);
1578 if (internal == NULL && size != 0)
1580 internal_end = internal + obj_raw_syment_count (abfd);
1582 if (! _bfd_coff_get_external_symbols (abfd))
1585 raw_src = (char *) obj_coff_external_syms (abfd);
1587 /* mark the end of the symbols */
1588 symesz = bfd_coff_symesz (abfd);
1589 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1591 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1592 probably possible. If one shows up, it will probably kill us. */
1594 /* Swap all the raw entries */
1595 for (internal_ptr = internal;
1597 raw_src += symesz, internal_ptr++)
1601 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1602 (PTR) & internal_ptr->u.syment);
1603 internal_ptr->fix_value = 0;
1604 internal_ptr->fix_tag = 0;
1605 internal_ptr->fix_end = 0;
1606 internal_ptr->fix_scnlen = 0;
1607 symbol_ptr = internal_ptr;
1610 i < symbol_ptr->u.syment.n_numaux;
1616 internal_ptr->fix_value = 0;
1617 internal_ptr->fix_tag = 0;
1618 internal_ptr->fix_end = 0;
1619 internal_ptr->fix_scnlen = 0;
1620 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1621 symbol_ptr->u.syment.n_type,
1622 symbol_ptr->u.syment.n_sclass,
1623 i, symbol_ptr->u.syment.n_numaux,
1624 &(internal_ptr->u.auxent));
1625 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1630 /* Free the raw symbols, but not the strings (if we have them). */
1631 obj_coff_keep_strings (abfd) = true;
1632 if (! _bfd_coff_free_symbols (abfd))
1635 for (internal_ptr = internal; internal_ptr < internal_end;
1638 if (internal_ptr->u.syment.n_sclass == C_FILE
1639 && internal_ptr->u.syment.n_numaux > 0)
1641 /* make a file symbol point to the name in the auxent, since
1642 the text ".file" is redundant */
1643 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1645 /* the filename is a long one, point into the string table */
1646 if (string_table == NULL)
1648 string_table = _bfd_coff_read_string_table (abfd);
1649 if (string_table == NULL)
1653 internal_ptr->u.syment._n._n_n._n_offset =
1656 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1660 /* ordinary short filename, put into memory anyway */
1661 internal_ptr->u.syment._n._n_n._n_offset = (long)
1662 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1668 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1670 /* This is a "short" name. Make it long. */
1671 unsigned long i = 0;
1672 char *newstring = NULL;
1674 /* find the length of this string without walking into memory
1676 for (i = 0; i < 8; ++i)
1678 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1681 } /* if end of string */
1682 } /* possible lengths of this string. */
1684 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1686 memset (newstring, 0, i);
1687 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1688 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1689 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1691 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1692 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1693 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1695 /* Long name already. Point symbol at the string in the
1697 if (string_table == NULL)
1699 string_table = _bfd_coff_read_string_table (abfd);
1700 if (string_table == NULL)
1703 internal_ptr->u.syment._n._n_n._n_offset =
1706 + internal_ptr->u.syment._n._n_n._n_offset));
1710 /* Long name in debug section. Very similar. */
1711 if (debug_section == NULL)
1712 debug_section = build_debug_section (abfd);
1713 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1714 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1717 internal_ptr += internal_ptr->u.syment.n_numaux;
1720 obj_raw_syments (abfd) = internal;
1721 BFD_ASSERT (obj_raw_syment_count (abfd)
1722 == (unsigned int) (internal_ptr - internal));
1725 } /* coff_get_normalized_symtab() */
1728 coff_get_reloc_upper_bound (abfd, asect)
1732 if (bfd_get_format (abfd) != bfd_object)
1734 bfd_set_error (bfd_error_invalid_operation);
1737 return (asect->reloc_count + 1) * sizeof (arelent *);
1741 coff_make_empty_symbol (abfd)
1744 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1747 memset (new, 0, sizeof *new);
1748 new->symbol.section = 0;
1750 new->lineno = (alent *) NULL;
1751 new->done_lineno = false;
1752 new->symbol.the_bfd = abfd;
1753 return &new->symbol;
1756 /* Make a debugging symbol. */
1759 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1764 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1767 /* @@ This shouldn't be using a constant multiplier. */
1768 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1771 new->symbol.section = bfd_abs_section_ptr;
1772 new->symbol.flags = BSF_DEBUGGING;
1773 new->lineno = (alent *) NULL;
1774 new->done_lineno = false;
1775 new->symbol.the_bfd = abfd;
1776 return &new->symbol;
1781 coff_get_symbol_info (abfd, symbol, ret)
1786 bfd_symbol_info (symbol, ret);
1787 if (coffsymbol (symbol)->native != NULL
1788 && coffsymbol (symbol)->native->fix_value)
1790 combined_entry_type *psym;
1792 psym = ((combined_entry_type *)
1793 coffsymbol (symbol)->native->u.syment.n_value);
1794 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1798 /* Print out information about COFF symbol. */
1801 coff_print_symbol (abfd, filep, symbol, how)
1805 bfd_print_symbol_type how;
1807 FILE *file = (FILE *) filep;
1811 case bfd_print_symbol_name:
1812 fprintf (file, "%s", symbol->name);
1815 case bfd_print_symbol_more:
1816 fprintf (file, "coff %s %s",
1817 coffsymbol (symbol)->native ? "n" : "g",
1818 coffsymbol (symbol)->lineno ? "l" : " ");
1821 case bfd_print_symbol_all:
1822 if (coffsymbol (symbol)->native)
1826 combined_entry_type *combined = coffsymbol (symbol)->native;
1827 combined_entry_type *root = obj_raw_syments (abfd);
1828 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1830 fprintf (file, "[%3ld]", (long) (combined - root));
1832 if (! combined->fix_value)
1833 val = (unsigned long) combined->u.syment.n_value;
1835 val = ((unsigned long)
1836 ((combined_entry_type *) combined->u.syment.n_value
1840 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1841 combined->u.syment.n_scnum,
1842 combined->u.syment.n_flags,
1843 combined->u.syment.n_type,
1844 combined->u.syment.n_sclass,
1845 combined->u.syment.n_numaux,
1849 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1851 combined_entry_type *auxp = combined + aux + 1;
1855 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1857 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1859 fprintf (file, "\n");
1861 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1864 switch (combined->u.syment.n_sclass)
1867 fprintf (file, "File ");
1871 if (combined->u.syment.n_type == T_NULL)
1872 /* probably a section symbol? */
1874 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1875 (long) auxp->u.auxent.x_scn.x_scnlen,
1876 auxp->u.auxent.x_scn.x_nreloc,
1877 auxp->u.auxent.x_scn.x_nlinno);
1880 /* else fall through */
1883 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1884 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1885 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1888 fprintf (file, " endndx %ld",
1890 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1898 fprintf (file, "\n%s :", l->u.sym->name);
1900 while (l->line_number)
1902 fprintf (file, "\n%4d : 0x%lx",
1905 (l->u.offset + symbol->section->vma)));
1912 bfd_print_symbol_vandf ((PTR) file, symbol);
1913 fprintf (file, " %-5s %s %s %s",
1914 symbol->section->name,
1915 coffsymbol (symbol)->native ? "n" : "g",
1916 coffsymbol (symbol)->lineno ? "l" : " ",
1922 /* Provided a BFD, a section and an offset into the section, calculate
1923 and return the name of the source file and the line nearest to the
1928 coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1929 functionname_ptr, line_ptr)
1932 asymbol **ignore_symbols;
1934 CONST char **filename_ptr;
1935 CONST char **functionname_ptr;
1936 unsigned int *line_ptr;
1939 unsigned int line_base;
1940 coff_data_type *cof = coff_data (abfd);
1941 /* Run through the raw syments if available */
1942 combined_entry_type *p;
1943 combined_entry_type *pend;
1945 struct coff_section_tdata *sec_data;
1948 *functionname_ptr = 0;
1951 /* Don't try and find line numbers in a non coff file */
1952 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1958 /* Find the first C_FILE symbol. */
1959 p = cof->raw_syments;
1960 pend = p + cof->raw_syment_count;
1963 if (p->u.syment.n_sclass == C_FILE)
1965 p += 1 + p->u.syment.n_numaux;
1972 /* Look through the C_FILE symbols to find the best one. */
1973 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1974 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
1977 combined_entry_type *p2;
1979 for (p2 = p + 1 + p->u.syment.n_numaux;
1981 p2 += 1 + p2->u.syment.n_numaux)
1983 if (p2->u.syment.n_scnum > 0
1985 == coff_section_from_bfd_index (abfd,
1986 p2->u.syment.n_scnum)))
1988 if (p2->u.syment.n_sclass == C_FILE)
1996 && offset >= (bfd_vma) p2->u.syment.n_value
1997 && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
1999 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2000 maxdiff = offset - p2->u.syment.n_value;
2003 /* Avoid endless loops on erroneous files by ensuring that
2004 we always move forward in the file. */
2005 if (p - cof->raw_syments >= p->u.syment.n_value)
2008 p = cof->raw_syments + p->u.syment.n_value;
2009 if (p > pend || p->u.syment.n_sclass != C_FILE)
2014 /* Now wander though the raw linenumbers of the section */
2015 /* If we have been called on this section before, and the offset we
2016 want is further down then we can prime the lookup loop. */
2017 sec_data = coff_section_data (abfd, section);
2018 if (sec_data != NULL
2020 && offset >= sec_data->offset)
2023 *functionname_ptr = sec_data->function;
2024 line_base = sec_data->line_base;
2032 l = §ion->lineno[i];
2034 for (; i < section->lineno_count; i++)
2036 if (l->line_number == 0)
2038 /* Get the symbol this line number points at */
2039 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2040 if (coff->symbol.value > offset)
2042 *functionname_ptr = coff->symbol.name;
2045 combined_entry_type *s = coff->native;
2046 s = s + 1 + s->u.syment.n_numaux;
2048 /* In XCOFF a debugging symbol can follow the function
2050 if (s->u.syment.n_scnum == N_DEBUG)
2051 s = s + 1 + s->u.syment.n_numaux;
2054 S should now point to the .bf of the function
2056 if (s->u.syment.n_numaux)
2059 The linenumber is stored in the auxent
2061 union internal_auxent *a = &((s + 1)->u.auxent);
2062 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2063 *line_ptr = line_base;
2069 if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
2071 *line_ptr = l->line_number + line_base - 1;
2076 /* Cache the results for the next call. */
2077 if (sec_data == NULL && section->owner == abfd)
2079 section->used_by_bfd =
2080 ((PTR) bfd_zalloc (abfd,
2081 sizeof (struct coff_section_tdata)));
2082 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2084 if (sec_data != NULL)
2086 sec_data->offset = offset;
2088 sec_data->function = *functionname_ptr;
2089 sec_data->line_base = line_base;
2096 coff_sizeof_headers (abfd, reloc)
2104 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2108 size = bfd_coff_filhsz (abfd);
2111 size += abfd->section_count * bfd_coff_scnhsz (abfd);