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. */
21 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
22 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
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 *));
57 #define STRING_SIZE_SIZE (4)
59 /* Take a section header read from a coff file (in HOST byte order),
60 and make a BFD "section" out of it. This is used by ECOFF. */
62 make_a_section_from_file (abfd, hdr, target_index)
64 struct internal_scnhdr *hdr;
65 unsigned int target_index;
67 asection *return_section;
70 /* Assorted wastage to null-terminate the name, thanks AT&T! */
71 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
74 bfd_set_error (bfd_error_no_memory);
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 (abfd, name);
81 if (return_section == NULL)
82 return_section = bfd_coff_make_section_hook (abfd, name);
84 /* Handle several sections of the same name. For example, if an executable
85 has two .bss sections, GDB better be able to find both of them
87 if (return_section == NULL)
88 return_section = bfd_make_section_anyway (abfd, name);
90 if (return_section == NULL)
93 /* s_paddr is presumed to be = to s_vaddr */
95 return_section->vma = hdr->s_vaddr;
96 return_section->lma = return_section->vma;
97 return_section->_raw_size = hdr->s_size;
98 return_section->filepos = hdr->s_scnptr;
99 return_section->rel_filepos = hdr->s_relptr;
100 return_section->reloc_count = hdr->s_nreloc;
102 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
104 return_section->line_filepos = hdr->s_lnnoptr;
106 return_section->lineno_count = hdr->s_nlnno;
107 return_section->userdata = NULL;
108 return_section->next = (asection *) NULL;
109 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
111 return_section->target_index = target_index;
113 /* At least on i386-coff, the line number count for a shared library
114 section must be ignored. */
115 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
116 return_section->lineno_count = 0;
118 if (hdr->s_nreloc != 0)
119 return_section->flags |= SEC_RELOC;
120 /* FIXME: should this check 'hdr->s_size > 0' */
121 if (hdr->s_scnptr != 0)
122 return_section->flags |= SEC_HAS_CONTENTS;
126 /* Read in a COFF object and make it into a BFD. This is used by
129 static const bfd_target *
130 coff_real_object_p (abfd, nscns, internal_f, internal_a)
133 struct internal_filehdr *internal_f;
134 struct internal_aouthdr *internal_a;
136 flagword oflags = abfd->flags;
137 bfd_vma ostart = bfd_get_start_address (abfd);
139 size_t readsize; /* length of file_info */
141 char *external_sections;
143 if (!(internal_f->f_flags & F_RELFLG))
144 abfd->flags |= HAS_RELOC;
145 if ((internal_f->f_flags & F_EXEC))
146 abfd->flags |= EXEC_P;
147 if (!(internal_f->f_flags & F_LNNO))
148 abfd->flags |= HAS_LINENO;
149 if (!(internal_f->f_flags & F_LSYMS))
150 abfd->flags |= HAS_LOCALS;
152 /* FIXME: How can we set D_PAGED correctly? */
153 if ((internal_f->f_flags & F_EXEC) != 0)
154 abfd->flags |= D_PAGED;
156 bfd_get_symcount (abfd) = internal_f->f_nsyms;
157 if (internal_f->f_nsyms)
158 abfd->flags |= HAS_SYMS;
160 if (internal_a != (struct internal_aouthdr *) NULL)
161 bfd_get_start_address (abfd) = internal_a->entry;
163 bfd_get_start_address (abfd) = 0;
165 /* Set up the tdata area. ECOFF uses its own routine, and overrides
167 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
171 scnhsz = bfd_coff_scnhsz (abfd);
172 readsize = nscns * scnhsz;
173 external_sections = (char *) bfd_alloc (abfd, readsize);
174 if (!external_sections)
176 bfd_set_error (bfd_error_no_memory);
180 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
183 /* Now copy data as required; construct all asections etc */
187 for (i = 0; i < nscns; i++)
189 struct internal_scnhdr tmp;
190 bfd_coff_swap_scnhdr_in (abfd,
191 (PTR) (external_sections + i * scnhsz),
193 make_a_section_from_file (abfd, &tmp, i + 1);
197 /* make_abs_section (abfd); */
199 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
205 bfd_release (abfd, tdata);
206 abfd->flags = oflags;
207 bfd_get_start_address (abfd) = ostart;
208 return (const bfd_target *) NULL;
211 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
212 not a COFF file. This is also used by ECOFF. */
222 struct internal_filehdr internal_f;
223 struct internal_aouthdr internal_a;
225 /* figure out how much to read */
226 filhsz = bfd_coff_filhsz (abfd);
227 aoutsz = bfd_coff_aoutsz (abfd);
229 filehdr = bfd_alloc (abfd, filhsz);
232 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
234 if (bfd_get_error () != bfd_error_system_call)
235 bfd_set_error (bfd_error_wrong_format);
238 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
239 bfd_release (abfd, filehdr);
241 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
243 bfd_set_error (bfd_error_wrong_format);
246 nscns = internal_f.f_nscns;
248 if (internal_f.f_opthdr)
252 opthdr = bfd_alloc (abfd, aoutsz);
255 if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
259 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
262 /* Seek past the opt hdr stuff */
263 if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
267 return coff_real_object_p (abfd, nscns, &internal_f,
268 (internal_f.f_opthdr != 0
270 : (struct internal_aouthdr *) NULL));
273 /* Get the BFD section from a COFF symbol section number. */
276 coff_section_from_bfd_index (abfd, index)
280 struct sec *answer = abfd->sections;
283 return bfd_abs_section_ptr;
284 if (index == N_UNDEF)
285 return bfd_und_section_ptr;
286 if (index == N_DEBUG)
287 return bfd_abs_section_ptr;
291 if (answer->target_index == index)
293 answer = answer->next;
296 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
297 has a bad symbol table in biglitpow.o. */
298 return bfd_und_section_ptr;
301 /* Get the upper bound of a COFF symbol table. */
304 coff_get_symtab_upper_bound (abfd)
307 if (!bfd_coff_slurp_symbol_table (abfd))
310 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
314 /* Canonicalize a COFF symbol table. */
317 coff_get_symtab (abfd, alocation)
321 unsigned int counter;
322 coff_symbol_type *symbase;
323 coff_symbol_type **location = (coff_symbol_type **) alocation;
325 if (!bfd_coff_slurp_symbol_table (abfd))
328 symbase = obj_symbols (abfd);
329 counter = bfd_get_symcount (abfd);
330 while (counter-- > 0)
331 *location++ = symbase++;
335 return bfd_get_symcount (abfd);
338 /* Set lineno_count for the output sections of a COFF file. */
341 coff_count_linenumbers (abfd)
344 unsigned int limit = bfd_get_symcount (abfd);
352 /* This may be from the backend linker, in which case the
353 lineno_count in the sections is correct. */
354 for (s = abfd->sections; s != NULL; s = s->next)
355 total += s->lineno_count;
359 for (s = abfd->sections; s != NULL; s = s->next)
360 BFD_ASSERT (s->lineno_count == 0);
362 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
364 asymbol *q_maybe = *p;
366 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
368 coff_symbol_type *q = coffsymbol (q_maybe);
370 if (q->lineno != NULL)
372 /* This symbol has line numbers. Increment the owning
373 section's linenumber count. */
374 alent *l = q->lineno;
376 ++q->symbol.section->output_section->lineno_count;
379 while (l->line_number != 0)
382 ++q->symbol.section->output_section->lineno_count;
392 /* Takes a bfd and a symbol, returns a pointer to the coff specific
393 area of the symbol if there is one. */
397 coff_symbol_from (ignore_abfd, symbol)
401 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
402 return (coff_symbol_type *) NULL;
404 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
405 return (coff_symbol_type *) NULL;
407 return (coff_symbol_type *) symbol;
411 fixup_symbol_value (coff_symbol_ptr, syment)
412 coff_symbol_type *coff_symbol_ptr;
413 struct internal_syment *syment;
416 /* Normalize the symbol flags */
417 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
419 /* a common symbol is undefined with a value */
420 syment->n_scnum = N_UNDEF;
421 syment->n_value = coff_symbol_ptr->symbol.value;
423 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
425 syment->n_value = coff_symbol_ptr->symbol.value;
427 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
429 syment->n_scnum = N_UNDEF;
434 if (coff_symbol_ptr->symbol.section)
437 coff_symbol_ptr->symbol.section->output_section->target_index;
440 coff_symbol_ptr->symbol.value +
441 coff_symbol_ptr->symbol.section->output_offset +
442 coff_symbol_ptr->symbol.section->output_section->vma;
447 /* This can happen, but I don't know why yet (steve@cygnus.com) */
448 syment->n_scnum = N_ABS;
449 syment->n_value = coff_symbol_ptr->symbol.value;
454 /* Run through all the symbols in the symbol table and work out what
455 their indexes into the symbol table will be when output.
457 Coff requires that each C_FILE symbol points to the next one in the
458 chain, and that the last one points to the first external symbol. We
462 coff_renumber_symbols (bfd_ptr, first_undef)
466 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
467 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
468 unsigned int native_index = 0;
469 struct internal_syment *last_file = (struct internal_syment *) NULL;
470 unsigned int symbol_index;
472 /* COFF demands that undefined symbols come after all other symbols.
473 Since we don't need to impose this extra knowledge on all our
474 client programs, deal with that here. Sort the symbol table;
475 just move the undefined symbols to the end, leaving the rest
476 alone. The O'Reilly book says that defined global symbols come
477 at the end before the undefined symbols, so we do that here as
479 /* @@ Do we have some condition we could test for, so we don't always
480 have to do this? I don't think relocatability is quite right, but
481 I'm not certain. [raeburn:19920508.1711EST] */
486 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
488 * (symbol_count + 1));
491 bfd_set_error (bfd_error_no_memory);
494 bfd_ptr->outsymbols = newsyms;
495 for (i = 0; i < symbol_count; i++)
496 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
497 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
501 *newsyms++ = symbol_ptr_ptr[i];
503 for (i = 0; i < symbol_count; i++)
504 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
505 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
509 *newsyms++ = symbol_ptr_ptr[i];
511 *first_undef = newsyms - bfd_ptr->outsymbols;
513 for (i = 0; i < symbol_count; i++)
514 if (bfd_is_und_section (symbol_ptr_ptr[i]->section))
515 *newsyms++ = symbol_ptr_ptr[i];
516 *newsyms = (asymbol *) NULL;
517 symbol_ptr_ptr = bfd_ptr->outsymbols;
520 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
522 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
523 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
524 if (coff_symbol_ptr && coff_symbol_ptr->native)
526 combined_entry_type *s = coff_symbol_ptr->native;
529 if (s->u.syment.n_sclass == C_FILE)
531 if (last_file != (struct internal_syment *) NULL)
532 last_file->n_value = native_index;
533 last_file = &(s->u.syment);
538 /* Modify the symbol values according to their section and
541 fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
543 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
544 s[i].offset = native_index++;
551 obj_conv_table_size (bfd_ptr) = native_index;
556 /* Run thorough the symbol table again, and fix it so that all
557 pointers to entries are changed to the entries' index in the output
561 coff_mangle_symbols (bfd_ptr)
564 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
565 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
566 unsigned int symbol_index;
568 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
570 coff_symbol_type *coff_symbol_ptr =
571 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
573 if (coff_symbol_ptr && coff_symbol_ptr->native)
576 combined_entry_type *s = coff_symbol_ptr->native;
580 /* FIXME: We should use a union here. */
581 s->u.syment.n_value =
582 ((combined_entry_type *) s->u.syment.n_value)->offset;
585 for (i = 0; i < s->u.syment.n_numaux; i++)
587 combined_entry_type *a = s + i + 1;
590 a->u.auxent.x_sym.x_tagndx.l =
591 a->u.auxent.x_sym.x_tagndx.p->offset;
596 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
597 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
602 a->u.auxent.x_csect.x_scnlen.l =
603 a->u.auxent.x_csect.x_scnlen.p->offset;
612 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
613 debug_string_section_p, debug_string_size_p)
616 combined_entry_type *native;
617 bfd_size_type *string_size_p;
618 asection **debug_string_section_p;
619 bfd_size_type *debug_string_size_p;
621 unsigned int name_length;
622 union internal_auxent *auxent;
623 char *name = (char *) (symbol->name);
625 if (name == (char *) NULL)
627 /* coff symbols always have names, so we'll make one up */
628 symbol->name = "strange";
629 name = (char *) symbol->name;
631 name_length = strlen (name);
633 if (native->u.syment.n_sclass == C_FILE)
635 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
636 auxent = &(native + 1)->u.auxent;
638 if (bfd_coff_long_filenames (abfd))
640 if (name_length <= FILNMLEN)
642 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
646 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
647 auxent->x_file.x_n.x_zeroes = 0;
648 *string_size_p += name_length + 1;
653 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
654 if (name_length > FILNMLEN)
656 name[FILNMLEN] = '\0';
661 { /* NOT A C_FILE SYMBOL */
662 if (name_length <= SYMNMLEN)
664 /* This name will fit into the symbol neatly */
665 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
667 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
669 native->u.syment._n._n_n._n_offset = (*string_size_p
671 native->u.syment._n._n_n._n_zeroes = 0;
672 *string_size_p += name_length + 1;
679 /* This name should be written into the .debug section. For
680 some reason each name is preceded by a two byte length
681 and also followed by a null byte. FIXME: We assume that
682 the .debug section has already been created, and that it
684 if (*debug_string_section_p == (asection *) NULL)
685 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
686 filepos = bfd_tell (abfd);
687 bfd_put_16 (abfd, name_length + 1, buf);
688 if (!bfd_set_section_contents (abfd,
689 *debug_string_section_p,
691 (file_ptr) *debug_string_size_p,
693 || !bfd_set_section_contents (abfd,
694 *debug_string_section_p,
696 ((file_ptr) *debug_string_size_p
698 (bfd_size_type) name_length + 1))
700 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
702 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
703 native->u.syment._n._n_n._n_zeroes = 0;
704 *debug_string_size_p += name_length + 3;
709 /* We need to keep track of the symbol index so that when we write out
710 the relocs we can get the index for a symbol. This method is a
713 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
715 /* Write a symbol out to a COFF file. */
718 coff_write_symbol (abfd, symbol, native, written, string_size_p,
719 debug_string_section_p, debug_string_size_p)
722 combined_entry_type *native;
723 unsigned int *written;
724 bfd_size_type *string_size_p;
725 asection **debug_string_section_p;
726 bfd_size_type *debug_string_size_p;
728 unsigned int numaux = native->u.syment.n_numaux;
729 int type = native->u.syment.n_type;
730 int class = native->u.syment.n_sclass;
732 bfd_size_type symesz;
734 if (native->u.syment.n_sclass == C_FILE)
735 symbol->flags |= BSF_DEBUGGING;
737 if (symbol->flags & BSF_DEBUGGING)
739 native->u.syment.n_scnum = N_DEBUG;
741 else if (bfd_is_abs_section (symbol->section))
743 native->u.syment.n_scnum = N_ABS;
745 else if (bfd_is_und_section (symbol->section))
747 native->u.syment.n_scnum = N_UNDEF;
751 native->u.syment.n_scnum =
752 symbol->section->output_section->target_index;
755 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
756 debug_string_section_p, debug_string_size_p);
758 symesz = bfd_coff_symesz (abfd);
759 buf = bfd_alloc (abfd, symesz);
762 bfd_set_error (bfd_error_no_memory);
765 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
766 if (bfd_write (buf, 1, symesz, abfd) != symesz)
768 bfd_release (abfd, buf);
770 if (native->u.syment.n_numaux > 0)
772 bfd_size_type auxesz;
775 auxesz = bfd_coff_auxesz (abfd);
776 buf = bfd_alloc (abfd, auxesz);
779 bfd_set_error (bfd_error_no_memory);
782 for (j = 0; j < native->u.syment.n_numaux; j++)
784 bfd_coff_swap_aux_out (abfd,
785 &((native + j + 1)->u.auxent),
789 native->u.syment.n_numaux,
791 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
794 bfd_release (abfd, buf);
797 /* Store the index for use when we write out the relocs. */
798 set_index (symbol, *written);
800 *written += numaux + 1;
804 /* Write out a symbol to a COFF file that does not come from a COFF
805 file originally. This symbol may have been created by the linker,
806 or we may be linking a non COFF file to a COFF file. */
809 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
810 debug_string_section_p, debug_string_size_p)
813 unsigned int *written;
814 bfd_size_type *string_size_p;
815 asection **debug_string_section_p;
816 bfd_size_type *debug_string_size_p;
818 combined_entry_type *native;
819 combined_entry_type dummy;
822 native->u.syment.n_type = T_NULL;
823 native->u.syment.n_flags = 0;
824 if (bfd_is_und_section (symbol->section))
826 native->u.syment.n_scnum = N_UNDEF;
827 native->u.syment.n_value = symbol->value;
829 else if (bfd_is_com_section (symbol->section))
831 native->u.syment.n_scnum = N_UNDEF;
832 native->u.syment.n_value = symbol->value;
834 else if (symbol->flags & BSF_DEBUGGING)
836 /* There isn't much point to writing out a debugging symbol
837 unless we are prepared to convert it into COFF debugging
838 format. So, we just ignore them. We must clobber the symbol
839 name to keep it from being put in the string table. */
845 native->u.syment.n_scnum =
846 symbol->section->output_section->target_index;
847 native->u.syment.n_value = (symbol->value
848 + symbol->section->output_section->vma
849 + symbol->section->output_offset);
851 /* Copy the any flags from the the file header into the symbol.
854 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
855 if (c != (coff_symbol_type *) NULL)
856 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
860 native->u.syment.n_type = 0;
861 if (symbol->flags & BSF_LOCAL)
862 native->u.syment.n_sclass = C_STAT;
864 native->u.syment.n_sclass = C_EXT;
865 native->u.syment.n_numaux = 0;
867 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
868 debug_string_section_p, debug_string_size_p);
871 /* Write a native symbol to a COFF file. */
874 coff_write_native_symbol (abfd, symbol, written, string_size_p,
875 debug_string_section_p, debug_string_size_p)
877 coff_symbol_type *symbol;
878 unsigned int *written;
879 bfd_size_type *string_size_p;
880 asection **debug_string_section_p;
881 bfd_size_type *debug_string_size_p;
883 combined_entry_type *native = symbol->native;
884 alent *lineno = symbol->lineno;
886 /* If this symbol has an associated line number, we must store the
887 symbol index in the line number field. We also tag the auxent to
888 point to the right place in the lineno table. */
889 if (lineno && !symbol->done_lineno)
891 unsigned int count = 0;
892 lineno[count].u.offset = *written;
893 if (native->u.syment.n_numaux)
895 union internal_auxent *a = &((native + 1)->u.auxent);
897 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
898 symbol->symbol.section->output_section->moving_line_filepos;
901 /* Count and relocate all other linenumbers. */
903 while (lineno[count].line_number != 0)
907 I've been told this, but still need proof:
908 > The second bug is also in `bfd/coffcode.h'. This bug
909 > causes the linker to screw up the pc-relocations for
910 > all the line numbers in COFF code. This bug isn't only
911 > specific to A29K implementations, but affects all
912 > systems using COFF format binaries. Note that in COFF
913 > object files, the line number core offsets output by
914 > the assembler are relative to the start of each
915 > procedure, not to the start of the .text section. This
916 > patch relocates the line numbers relative to the
917 > `native->u.syment.n_value' instead of the section
919 > modular!olson@cs.arizona.edu (Jon Olson)
921 lineno[count].u.offset += native->u.syment.n_value;
923 lineno[count].u.offset +=
924 (symbol->symbol.section->output_section->vma
925 + symbol->symbol.section->output_offset);
929 symbol->done_lineno = true;
931 symbol->symbol.section->output_section->moving_line_filepos +=
932 count * bfd_coff_linesz (abfd);
935 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
936 string_size_p, debug_string_section_p,
937 debug_string_size_p);
940 /* Write out the COFF symbols. */
943 coff_write_symbols (abfd)
946 bfd_size_type string_size;
947 asection *debug_string_section;
948 bfd_size_type debug_string_size;
950 unsigned int limit = bfd_get_symcount (abfd);
951 unsigned int written = 0;
955 debug_string_section = NULL;
956 debug_string_size = 0;
958 /* Seek to the right place */
959 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
962 /* Output all the symbols we have */
965 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
967 asymbol *symbol = *p;
968 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
970 if (c_symbol == (coff_symbol_type *) NULL
971 || c_symbol->native == (combined_entry_type *) NULL)
973 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
974 &debug_string_section,
980 if (!coff_write_native_symbol (abfd, c_symbol, &written,
981 &string_size, &debug_string_section,
987 obj_raw_syment_count (abfd) = written;
989 /* Now write out strings */
991 if (string_size != 0)
993 unsigned int size = string_size + STRING_SIZE_SIZE;
994 bfd_byte buffer[STRING_SIZE_SIZE];
996 #if STRING_SIZE_SIZE == 4
997 bfd_h_put_32 (abfd, size, buffer);
999 #error Change bfd_h_put_32
1001 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1003 for (p = abfd->outsymbols, i = 0;
1008 size_t name_length = strlen (q->name);
1009 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1012 /* Figure out whether the symbol name should go in the string
1013 table. Symbol names that are short enough are stored
1014 directly in the syment structure. File names permit a
1015 different, longer, length in the syment structure. On
1016 XCOFF, some symbol names are stored in the .debug section
1017 rather than in the string table. */
1019 if (c_symbol == NULL
1020 || c_symbol->native == NULL)
1022 /* This is not a COFF symbol, so it certainly is not a
1023 file name, nor does it go in the .debug section. */
1026 else if (bfd_coff_symname_in_debug (abfd,
1027 &c_symbol->native->u.syment))
1029 /* This symbol name is in the XCOFF .debug section.
1030 Don't write it into the string table. */
1031 maxlen = name_length;
1033 else if (c_symbol->native->u.syment.n_sclass == C_FILE)
1038 if (name_length > maxlen)
1040 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1048 /* We would normally not write anything here, but we'll write
1049 out 4 so that any stupid coff reader which tries to read the
1050 string table even when there isn't one won't croak. */
1051 unsigned int size = STRING_SIZE_SIZE;
1052 bfd_byte buffer[STRING_SIZE_SIZE];
1054 #if STRING_SIZE_SIZE == 4
1055 bfd_h_put_32 (abfd, size, buffer);
1057 #error Change bfd_h_put_32
1059 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1060 != STRING_SIZE_SIZE)
1064 /* Make sure the .debug section was created to be the correct size.
1065 We should create it ourselves on the fly, but we don't because
1066 BFD won't let us write to any section until we know how large all
1067 the sections are. We could still do it by making another pass
1068 over the symbols. FIXME. */
1069 BFD_ASSERT (debug_string_size == 0
1070 || (debug_string_section != (asection *) NULL
1071 && (BFD_ALIGN (debug_string_size,
1072 1 << debug_string_section->alignment_power)
1073 == bfd_section_size (abfd, debug_string_section))));
1079 coff_write_linenumbers (abfd)
1083 bfd_size_type linesz;
1086 linesz = bfd_coff_linesz (abfd);
1087 buff = bfd_alloc (abfd, linesz);
1090 bfd_set_error (bfd_error_no_memory);
1093 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1095 if (s->lineno_count)
1097 asymbol **q = abfd->outsymbols;
1098 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1100 /* Find all the linenumbers in this section */
1104 if (p->section->output_section == s)
1107 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1108 (bfd_asymbol_bfd (p), p));
1111 /* Found a linenumber entry, output */
1112 struct internal_lineno out;
1113 memset ((PTR) & out, 0, sizeof (out));
1115 out.l_addr.l_symndx = l->u.offset;
1116 bfd_coff_swap_lineno_out (abfd, &out, buff);
1117 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1120 while (l->line_number)
1122 out.l_lnno = l->line_number;
1123 out.l_addr.l_symndx = l->u.offset;
1124 bfd_coff_swap_lineno_out (abfd, &out, buff);
1125 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1135 bfd_release (abfd, buff);
1141 coff_get_lineno (ignore_abfd, symbol)
1145 return coffsymbol (symbol)->lineno;
1149 coff_section_symbol (abfd, name)
1153 asection *sec = bfd_make_section_old_way (abfd, name);
1155 combined_entry_type *csym;
1158 csym = coff_symbol_from (abfd, sym)->native;
1159 /* Make sure back-end COFF stuff is there. */
1164 coff_symbol_type sym;
1165 /* @@FIXME This shouldn't use a fixed size!! */
1166 combined_entry_type e[10];
1169 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1172 bfd_set_error (bfd_error_no_error);
1175 memset ((char *) f, 0, sizeof (*f));
1176 coff_symbol_from (abfd, sym)->native = csym = f->e;
1178 csym[0].u.syment.n_sclass = C_STAT;
1179 csym[0].u.syment.n_numaux = 1;
1180 /* SF_SET_STATICS (sym); @@ ??? */
1181 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1182 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1183 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1185 if (sec->output_section == NULL)
1187 sec->output_section = sec;
1188 sec->output_offset = 0;
1194 /* This function transforms the offsets into the symbol table into
1195 pointers to syments. */
1198 coff_pointerize_aux (abfd, table_base, type, class, auxent)
1200 combined_entry_type *table_base;
1203 combined_entry_type *auxent;
1205 /* Don't bother if this is a file or a section */
1206 if (class == C_STAT && type == T_NULL)
1208 if (class == C_FILE)
1211 /* Otherwise patch up */
1212 #define N_TMASK coff_data (abfd)->local_n_tmask
1213 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1214 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK)
1215 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1217 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1219 + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l);
1220 auxent->fix_end = 1;
1222 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1223 generate one, so we must be careful to ignore it. */
1224 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1226 auxent->u.auxent.x_sym.x_tagndx.p =
1227 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1228 auxent->fix_tag = 1;
1232 /* Allocate space for the ".debug" section, and read it.
1233 We did not read the debug section until now, because
1234 we didn't want to go to the trouble until someone needed it. */
1237 build_debug_section (abfd)
1240 char *debug_section;
1243 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1247 bfd_set_error (bfd_error_no_debug_section);
1251 debug_section = (PTR) bfd_alloc (abfd,
1252 bfd_get_section_size_before_reloc (sect));
1253 if (debug_section == NULL)
1255 bfd_set_error (bfd_error_no_memory);
1259 /* Seek to the beginning of the `.debug' section and read it.
1260 Save the current position first; it is needed by our caller.
1261 Then read debug section and reset the file pointer. */
1263 position = bfd_tell (abfd);
1264 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1265 || (bfd_read (debug_section,
1266 bfd_get_section_size_before_reloc (sect), 1, abfd)
1267 != bfd_get_section_size_before_reloc (sect))
1268 || bfd_seek (abfd, position, SEEK_SET) != 0)
1270 return debug_section;
1274 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1275 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1276 be \0-terminated. */
1278 copy_name (abfd, name, maxlen)
1286 for (len = 0; len < maxlen; ++len)
1288 if (name[len] == '\0')
1294 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1296 bfd_set_error (bfd_error_no_memory);
1299 strncpy (newname, name, len);
1300 newname[len] = '\0';
1304 /* Read in the external symbols. */
1307 _bfd_coff_get_external_symbols (abfd)
1310 bfd_size_type symesz;
1314 if (obj_coff_external_syms (abfd) != NULL)
1317 symesz = bfd_coff_symesz (abfd);
1319 size = obj_raw_syment_count (abfd) * symesz;
1321 syms = malloc (size);
1322 if (syms == NULL && size != 0)
1324 bfd_set_error (bfd_error_no_memory);
1328 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1329 || bfd_read (syms, size, 1, abfd) != size)
1336 obj_coff_external_syms (abfd) = syms;
1341 /* Read in the external strings. The strings are not loaded until
1342 they are needed. This is because we have no simple way of
1343 detecting a missing string table in an archive. */
1346 _bfd_coff_read_string_table (abfd)
1349 char extstrsize[STRING_SIZE_SIZE];
1353 if (obj_coff_strings (abfd) != NULL)
1354 return obj_coff_strings (abfd);
1357 (obj_sym_filepos (abfd)
1358 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1362 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1364 if (bfd_get_error () != bfd_error_file_truncated)
1367 /* There is no string table. */
1368 strsize = STRING_SIZE_SIZE;
1372 #if STRING_SIZE_SIZE == 4
1373 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1375 #error Change bfd_h_get_32
1379 strings = malloc (strsize);
1380 if (strings == NULL)
1382 bfd_set_error (bfd_error_no_memory);
1386 if (bfd_read (strings + STRING_SIZE_SIZE,
1387 strsize - STRING_SIZE_SIZE, 1, abfd)
1388 != strsize - STRING_SIZE_SIZE)
1394 obj_coff_strings (abfd) = strings;
1399 /* Free up the external symbols and strings read from a COFF file. */
1402 _bfd_coff_free_symbols (abfd)
1405 if (obj_coff_external_syms (abfd) != NULL
1406 && ! obj_coff_keep_syms (abfd))
1408 free (obj_coff_external_syms (abfd));
1409 obj_coff_external_syms (abfd) = NULL;
1411 if (obj_coff_strings (abfd) != NULL
1412 && ! obj_coff_keep_strings (abfd))
1414 free (obj_coff_strings (abfd));
1415 obj_coff_strings (abfd) = NULL;
1420 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1421 knit the symbol names into a normalized form. By normalized here I
1422 mean that all symbols have an n_offset pointer that points to a null-
1423 terminated string. */
1425 combined_entry_type *
1426 coff_get_normalized_symtab (abfd)
1429 combined_entry_type *internal;
1430 combined_entry_type *internal_ptr;
1431 combined_entry_type *symbol_ptr;
1432 combined_entry_type *internal_end;
1433 bfd_size_type symesz;
1436 const char *string_table = NULL;
1437 char *debug_section = NULL;
1440 if (obj_raw_syments (abfd) != NULL)
1441 return obj_raw_syments (abfd);
1443 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1444 internal = (combined_entry_type *) bfd_alloc (abfd, size);
1445 if (internal == NULL && size != 0)
1447 bfd_set_error (bfd_error_no_memory);
1450 internal_end = internal + obj_raw_syment_count (abfd);
1452 if (! _bfd_coff_get_external_symbols (abfd))
1455 raw_src = (char *) obj_coff_external_syms (abfd);
1457 /* mark the end of the symbols */
1458 symesz = bfd_coff_symesz (abfd);
1459 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1461 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1462 probably possible. If one shows up, it will probably kill us. */
1464 /* Swap all the raw entries */
1465 for (internal_ptr = internal;
1467 raw_src += symesz, internal_ptr++)
1471 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1472 (PTR) & internal_ptr->u.syment);
1473 internal_ptr->fix_value = 0;
1474 internal_ptr->fix_tag = 0;
1475 internal_ptr->fix_end = 0;
1476 internal_ptr->fix_scnlen = 0;
1477 symbol_ptr = internal_ptr;
1480 i < symbol_ptr->u.syment.n_numaux;
1486 internal_ptr->fix_value = 0;
1487 internal_ptr->fix_tag = 0;
1488 internal_ptr->fix_end = 0;
1489 internal_ptr->fix_scnlen = 0;
1490 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1491 symbol_ptr->u.syment.n_type,
1492 symbol_ptr->u.syment.n_sclass,
1493 i, symbol_ptr->u.syment.n_numaux,
1494 &(internal_ptr->u.auxent));
1495 /* Remember that bal entries arn't pointerized */
1496 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1499 coff_pointerize_aux (abfd,
1501 symbol_ptr->u.syment.n_type,
1502 symbol_ptr->u.syment.n_sclass,
1509 /* Free the raw symbols, but not the strings (if we have them). */
1510 obj_coff_keep_strings (abfd) = true;
1511 if (! _bfd_coff_free_symbols (abfd))
1514 for (internal_ptr = internal; internal_ptr < internal_end;
1517 if (internal_ptr->u.syment.n_sclass == C_FILE
1518 && internal_ptr->u.syment.n_numaux > 0)
1520 /* make a file symbol point to the name in the auxent, since
1521 the text ".file" is redundant */
1522 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1524 /* the filename is a long one, point into the string table */
1525 if (string_table == NULL)
1527 string_table = _bfd_coff_read_string_table (abfd);
1528 if (string_table == NULL)
1532 internal_ptr->u.syment._n._n_n._n_offset =
1535 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1539 /* ordinary short filename, put into memory anyway */
1540 internal_ptr->u.syment._n._n_n._n_offset = (long)
1541 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1547 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1549 /* This is a "short" name. Make it long. */
1550 unsigned long i = 0;
1551 char *newstring = NULL;
1553 /* find the length of this string without walking into memory
1555 for (i = 0; i < 8; ++i)
1557 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1560 } /* if end of string */
1561 } /* possible lengths of this string. */
1563 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1565 bfd_set_error (bfd_error_no_memory);
1568 memset (newstring, 0, i);
1569 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1570 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1571 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1573 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1574 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1575 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1577 /* Long name already. Point symbol at the string in the
1579 if (string_table == NULL)
1581 string_table = _bfd_coff_read_string_table (abfd);
1582 if (string_table == NULL)
1585 internal_ptr->u.syment._n._n_n._n_offset =
1588 + internal_ptr->u.syment._n._n_n._n_offset));
1592 /* Long name in debug section. Very similar. */
1593 if (debug_section == NULL)
1594 debug_section = build_debug_section (abfd);
1595 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1596 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1599 internal_ptr += internal_ptr->u.syment.n_numaux;
1602 obj_raw_syments (abfd) = internal;
1603 BFD_ASSERT (obj_raw_syment_count (abfd)
1604 == (unsigned int) (internal_ptr - internal));
1607 } /* coff_get_normalized_symtab() */
1610 coff_get_reloc_upper_bound (abfd, asect)
1614 if (bfd_get_format (abfd) != bfd_object)
1616 bfd_set_error (bfd_error_invalid_operation);
1619 return (asect->reloc_count + 1) * sizeof (arelent *);
1623 coff_make_empty_symbol (abfd)
1626 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1629 bfd_set_error (bfd_error_no_memory);
1632 memset (new, 0, sizeof *new);
1633 new->symbol.section = 0;
1635 new->lineno = (alent *) NULL;
1636 new->done_lineno = false;
1637 new->symbol.the_bfd = abfd;
1638 return &new->symbol;
1641 /* Make a debugging symbol. */
1644 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1649 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1652 bfd_set_error (bfd_error_no_memory);
1655 /* @@ This shouldn't be using a constant multiplier. */
1656 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1659 bfd_set_error (bfd_error_no_memory);
1662 new->symbol.section = bfd_abs_section_ptr;
1663 new->symbol.flags = BSF_DEBUGGING;
1664 new->lineno = (alent *) NULL;
1665 new->done_lineno = false;
1666 new->symbol.the_bfd = abfd;
1667 return &new->symbol;
1672 coff_get_symbol_info (abfd, symbol, ret)
1677 bfd_symbol_info (symbol, ret);
1680 /* Print out information about COFF symbol. */
1683 coff_print_symbol (abfd, filep, symbol, how)
1687 bfd_print_symbol_type how;
1689 FILE *file = (FILE *) filep;
1693 case bfd_print_symbol_name:
1694 fprintf (file, "%s", symbol->name);
1697 case bfd_print_symbol_more:
1698 fprintf (file, "coff %s %s",
1699 coffsymbol (symbol)->native ? "n" : "g",
1700 coffsymbol (symbol)->lineno ? "l" : " ");
1703 case bfd_print_symbol_all:
1704 if (coffsymbol (symbol)->native)
1707 combined_entry_type *combined = coffsymbol (symbol)->native;
1708 combined_entry_type *root = obj_raw_syments (abfd);
1709 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1711 fprintf (file, "[%3ld]", (long) (combined - root));
1714 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1715 combined->u.syment.n_scnum,
1716 combined->u.syment.n_flags,
1717 combined->u.syment.n_type,
1718 combined->u.syment.n_sclass,
1719 combined->u.syment.n_numaux,
1720 (unsigned long) combined->u.syment.n_value,
1723 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1725 combined_entry_type *auxp = combined + aux + 1;
1729 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1731 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1733 fprintf (file, "\n");
1734 switch (combined->u.syment.n_sclass)
1737 fprintf (file, "File ");
1741 if (combined->u.syment.n_type == T_NULL)
1742 /* probably a section symbol? */
1744 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1745 (long) auxp->u.auxent.x_scn.x_scnlen,
1746 auxp->u.auxent.x_scn.x_nreloc,
1747 auxp->u.auxent.x_scn.x_nlinno);
1750 /* else fall through */
1753 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1754 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1755 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1758 fprintf (file, " endndx %ld",
1760 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1768 fprintf (file, "\n%s :", l->u.sym->name);
1770 while (l->line_number)
1772 fprintf (file, "\n%4d : 0x%lx",
1775 (l->u.offset + symbol->section->vma)));
1782 bfd_print_symbol_vandf ((PTR) file, symbol);
1783 fprintf (file, " %-5s %s %s %s",
1784 symbol->section->name,
1785 coffsymbol (symbol)->native ? "n" : "g",
1786 coffsymbol (symbol)->lineno ? "l" : " ",
1792 /* Provided a BFD, a section and an offset into the section, calculate
1793 and return the name of the source file and the line nearest to the
1798 coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1799 functionname_ptr, line_ptr)
1802 asymbol **ignore_symbols;
1804 CONST char **filename_ptr;
1805 CONST char **functionname_ptr;
1806 unsigned int *line_ptr;
1809 unsigned int line_base;
1810 coff_data_type *cof = coff_data (abfd);
1811 /* Run through the raw syments if available */
1812 combined_entry_type *p;
1813 combined_entry_type *pend;
1815 struct coff_section_tdata *sec_data;
1818 *functionname_ptr = 0;
1821 /* Don't try and find line numbers in a non coff file */
1822 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1828 /* Find the first C_FILE symbol. */
1829 p = cof->raw_syments;
1830 pend = p + cof->raw_syment_count;
1833 if (p->u.syment.n_sclass == C_FILE)
1835 p += 1 + p->u.syment.n_numaux;
1842 /* Look through the C_FILE symbols to find the best one. */
1843 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1844 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
1847 combined_entry_type *p2;
1849 for (p2 = p + 1 + p->u.syment.n_numaux;
1851 p2 += 1 + p2->u.syment.n_numaux)
1853 if (p2->u.syment.n_scnum > 0
1855 == coff_section_from_bfd_index (abfd,
1856 p2->u.syment.n_scnum)))
1858 if (p2->u.syment.n_sclass == C_FILE)
1866 && offset >= (bfd_vma) p2->u.syment.n_value
1867 && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
1869 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1870 maxdiff = offset - p2->u.syment.n_value;
1873 /* Avoid endless loops on erroneous files by ensuring that
1874 we always move forward in the file. */
1875 if (p - cof->raw_syments >= p->u.syment.n_value)
1878 p = cof->raw_syments + p->u.syment.n_value;
1879 if (p > pend || p->u.syment.n_sclass != C_FILE)
1884 /* Now wander though the raw linenumbers of the section */
1885 /* If we have been called on this section before, and the offset we
1886 want is further down then we can prime the lookup loop. */
1887 sec_data = coff_section_data (abfd, section);
1888 if (sec_data != NULL
1890 && offset >= sec_data->offset)
1893 *functionname_ptr = sec_data->function;
1894 line_base = sec_data->line_base;
1902 l = §ion->lineno[i];
1904 for (; i < section->lineno_count; i++)
1906 if (l->line_number == 0)
1908 /* Get the symbol this line number points at */
1909 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1910 if (coff->symbol.value > offset)
1912 *functionname_ptr = coff->symbol.name;
1915 combined_entry_type *s = coff->native;
1916 s = s + 1 + s->u.syment.n_numaux;
1918 /* In XCOFF a debugging symbol can follow the function
1920 if (s->u.syment.n_scnum == N_DEBUG)
1921 s = s + 1 + s->u.syment.n_numaux;
1924 S should now point to the .bf of the function
1926 if (s->u.syment.n_numaux)
1929 The linenumber is stored in the auxent
1931 union internal_auxent *a = &((s + 1)->u.auxent);
1932 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1933 *line_ptr = line_base;
1939 if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
1941 *line_ptr = l->line_number + line_base - 1;
1946 /* Cache the results for the next call. */
1947 if (sec_data == NULL)
1949 section->used_by_bfd =
1950 ((PTR) bfd_zalloc (abfd,
1951 sizeof (struct coff_section_tdata)));
1952 sec_data = section->used_by_bfd;
1954 if (sec_data != NULL)
1956 sec_data->offset = offset;
1958 sec_data->function = *functionname_ptr;
1959 sec_data->line_base = line_base;
1966 coff_sizeof_headers (abfd, reloc)
1974 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1978 size = bfd_coff_filhsz (abfd);
1981 size += abfd->section_count * bfd_coff_scnhsz (abfd);