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 *));
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 bfd_set_error (bfd_error_no_memory);
80 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
81 name[sizeof (hdr->s_name)] = 0;
83 return_section = bfd_make_section_anyway (abfd, name);
84 if (return_section == NULL)
87 /* s_paddr is presumed to be = to s_vaddr */
89 return_section->vma = hdr->s_vaddr;
90 return_section->lma = return_section->vma;
91 return_section->_raw_size = hdr->s_size;
92 return_section->filepos = hdr->s_scnptr;
93 return_section->rel_filepos = hdr->s_relptr;
94 return_section->reloc_count = hdr->s_nreloc;
96 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
98 return_section->line_filepos = hdr->s_lnnoptr;
100 return_section->lineno_count = hdr->s_nlnno;
101 return_section->userdata = NULL;
102 return_section->next = (asection *) NULL;
103 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
105 return_section->target_index = target_index;
107 /* At least on i386-coff, the line number count for a shared library
108 section must be ignored. */
109 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
110 return_section->lineno_count = 0;
112 if (hdr->s_nreloc != 0)
113 return_section->flags |= SEC_RELOC;
114 /* FIXME: should this check 'hdr->s_size > 0' */
115 if (hdr->s_scnptr != 0)
116 return_section->flags |= SEC_HAS_CONTENTS;
120 /* Read in a COFF object and make it into a BFD. This is used by
123 static const bfd_target *
124 coff_real_object_p (abfd, nscns, internal_f, internal_a)
127 struct internal_filehdr *internal_f;
128 struct internal_aouthdr *internal_a;
130 flagword oflags = abfd->flags;
131 bfd_vma ostart = bfd_get_start_address (abfd);
133 size_t readsize; /* length of file_info */
135 char *external_sections;
137 if (!(internal_f->f_flags & F_RELFLG))
138 abfd->flags |= HAS_RELOC;
139 if ((internal_f->f_flags & F_EXEC))
140 abfd->flags |= EXEC_P;
141 if (!(internal_f->f_flags & F_LNNO))
142 abfd->flags |= HAS_LINENO;
143 if (!(internal_f->f_flags & F_LSYMS))
144 abfd->flags |= HAS_LOCALS;
146 /* FIXME: How can we set D_PAGED correctly? */
147 if ((internal_f->f_flags & F_EXEC) != 0)
148 abfd->flags |= D_PAGED;
150 bfd_get_symcount (abfd) = internal_f->f_nsyms;
151 if (internal_f->f_nsyms)
152 abfd->flags |= HAS_SYMS;
154 if (internal_a != (struct internal_aouthdr *) NULL)
155 bfd_get_start_address (abfd) = internal_a->entry;
157 bfd_get_start_address (abfd) = 0;
159 /* Set up the tdata area. ECOFF uses its own routine, and overrides
161 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
165 scnhsz = bfd_coff_scnhsz (abfd);
166 readsize = nscns * scnhsz;
167 external_sections = (char *) bfd_alloc (abfd, readsize);
168 if (!external_sections)
170 bfd_set_error (bfd_error_no_memory);
174 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
177 /* Now copy data as required; construct all asections etc */
181 for (i = 0; i < nscns; i++)
183 struct internal_scnhdr tmp;
184 bfd_coff_swap_scnhdr_in (abfd,
185 (PTR) (external_sections + i * scnhsz),
187 make_a_section_from_file (abfd, &tmp, i + 1);
191 /* make_abs_section (abfd); */
193 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
199 bfd_release (abfd, tdata);
200 abfd->flags = oflags;
201 bfd_get_start_address (abfd) = ostart;
202 return (const bfd_target *) NULL;
205 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
206 not a COFF file. This is also used by ECOFF. */
216 struct internal_filehdr internal_f;
217 struct internal_aouthdr internal_a;
219 /* figure out how much to read */
220 filhsz = bfd_coff_filhsz (abfd);
221 aoutsz = bfd_coff_aoutsz (abfd);
223 filehdr = bfd_alloc (abfd, filhsz);
226 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
228 if (bfd_get_error () != bfd_error_system_call)
229 bfd_set_error (bfd_error_wrong_format);
232 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
233 bfd_release (abfd, filehdr);
235 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
237 bfd_set_error (bfd_error_wrong_format);
240 nscns = internal_f.f_nscns;
242 if (internal_f.f_opthdr)
246 opthdr = bfd_alloc (abfd, aoutsz);
249 if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
253 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
256 /* Seek past the opt hdr stuff */
257 if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
261 return coff_real_object_p (abfd, nscns, &internal_f,
262 (internal_f.f_opthdr != 0
264 : (struct internal_aouthdr *) NULL));
267 /* Get the BFD section from a COFF symbol section number. */
270 coff_section_from_bfd_index (abfd, index)
274 struct sec *answer = abfd->sections;
277 return bfd_abs_section_ptr;
278 if (index == N_UNDEF)
279 return bfd_und_section_ptr;
280 if (index == N_DEBUG)
281 return bfd_abs_section_ptr;
285 if (answer->target_index == index)
287 answer = answer->next;
290 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
291 has a bad symbol table in biglitpow.o. */
292 return bfd_und_section_ptr;
295 /* Get the upper bound of a COFF symbol table. */
298 coff_get_symtab_upper_bound (abfd)
301 if (!bfd_coff_slurp_symbol_table (abfd))
304 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
308 /* Canonicalize a COFF symbol table. */
311 coff_get_symtab (abfd, alocation)
315 unsigned int counter;
316 coff_symbol_type *symbase;
317 coff_symbol_type **location = (coff_symbol_type **) alocation;
319 if (!bfd_coff_slurp_symbol_table (abfd))
322 symbase = obj_symbols (abfd);
323 counter = bfd_get_symcount (abfd);
324 while (counter-- > 0)
325 *location++ = symbase++;
329 return bfd_get_symcount (abfd);
332 /* Set lineno_count for the output sections of a COFF file. */
335 coff_count_linenumbers (abfd)
338 unsigned int limit = bfd_get_symcount (abfd);
346 /* This may be from the backend linker, in which case the
347 lineno_count in the sections is correct. */
348 for (s = abfd->sections; s != NULL; s = s->next)
349 total += s->lineno_count;
353 for (s = abfd->sections; s != NULL; s = s->next)
354 BFD_ASSERT (s->lineno_count == 0);
356 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
358 asymbol *q_maybe = *p;
360 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
362 coff_symbol_type *q = coffsymbol (q_maybe);
364 /* The AIX 4.1 compiler can sometimes generate line numbers
365 attached to debugging symbols. We try to simply ignore
367 if (q->lineno != NULL
368 && q->symbol.section->owner != NULL)
370 /* This symbol has line numbers. Increment the owning
371 section's linenumber count. */
372 alent *l = q->lineno;
374 ++q->symbol.section->output_section->lineno_count;
377 while (l->line_number != 0)
380 ++q->symbol.section->output_section->lineno_count;
390 /* Takes a bfd and a symbol, returns a pointer to the coff specific
391 area of the symbol if there is one. */
395 coff_symbol_from (ignore_abfd, symbol)
399 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
400 return (coff_symbol_type *) NULL;
402 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
403 return (coff_symbol_type *) NULL;
405 return (coff_symbol_type *) symbol;
409 fixup_symbol_value (coff_symbol_ptr, syment)
410 coff_symbol_type *coff_symbol_ptr;
411 struct internal_syment *syment;
414 /* Normalize the symbol flags */
415 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
417 /* a common symbol is undefined with a value */
418 syment->n_scnum = N_UNDEF;
419 syment->n_value = coff_symbol_ptr->symbol.value;
421 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
423 syment->n_value = coff_symbol_ptr->symbol.value;
425 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
427 syment->n_scnum = N_UNDEF;
432 if (coff_symbol_ptr->symbol.section)
435 coff_symbol_ptr->symbol.section->output_section->target_index;
438 coff_symbol_ptr->symbol.value +
439 coff_symbol_ptr->symbol.section->output_offset +
440 coff_symbol_ptr->symbol.section->output_section->vma;
445 /* This can happen, but I don't know why yet (steve@cygnus.com) */
446 syment->n_scnum = N_ABS;
447 syment->n_value = coff_symbol_ptr->symbol.value;
452 /* Run through all the symbols in the symbol table and work out what
453 their indexes into the symbol table will be when output.
455 Coff requires that each C_FILE symbol points to the next one in the
456 chain, and that the last one points to the first external symbol. We
460 coff_renumber_symbols (bfd_ptr, first_undef)
464 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
465 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
466 unsigned int native_index = 0;
467 struct internal_syment *last_file = (struct internal_syment *) NULL;
468 unsigned int symbol_index;
470 /* COFF demands that undefined symbols come after all other symbols.
471 Since we don't need to impose this extra knowledge on all our
472 client programs, deal with that here. Sort the symbol table;
473 just move the undefined symbols to the end, leaving the rest
474 alone. The O'Reilly book says that defined global symbols come
475 at the end before the undefined symbols, so we do that here as
477 /* @@ Do we have some condition we could test for, so we don't always
478 have to do this? I don't think relocatability is quite right, but
479 I'm not certain. [raeburn:19920508.1711EST] */
484 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
486 * (symbol_count + 1));
489 bfd_set_error (bfd_error_no_memory);
492 bfd_ptr->outsymbols = newsyms;
493 for (i = 0; i < symbol_count; i++)
494 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
495 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
496 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
498 *newsyms++ = symbol_ptr_ptr[i];
500 for (i = 0; i < symbol_count; i++)
501 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
502 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
506 *newsyms++ = symbol_ptr_ptr[i];
508 *first_undef = newsyms - bfd_ptr->outsymbols;
510 for (i = 0; i < symbol_count; i++)
511 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
512 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
513 *newsyms++ = symbol_ptr_ptr[i];
514 *newsyms = (asymbol *) NULL;
515 symbol_ptr_ptr = bfd_ptr->outsymbols;
518 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
520 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
521 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
522 if (coff_symbol_ptr && coff_symbol_ptr->native)
524 combined_entry_type *s = coff_symbol_ptr->native;
527 if (s->u.syment.n_sclass == C_FILE)
529 if (last_file != (struct internal_syment *) NULL)
530 last_file->n_value = native_index;
531 last_file = &(s->u.syment);
536 /* Modify the symbol values according to their section and
539 fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
541 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
542 s[i].offset = native_index++;
549 obj_conv_table_size (bfd_ptr) = native_index;
554 /* Run thorough the symbol table again, and fix it so that all
555 pointers to entries are changed to the entries' index in the output
559 coff_mangle_symbols (bfd_ptr)
562 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
563 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
564 unsigned int symbol_index;
566 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
568 coff_symbol_type *coff_symbol_ptr =
569 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
571 if (coff_symbol_ptr && coff_symbol_ptr->native)
574 combined_entry_type *s = coff_symbol_ptr->native;
578 /* FIXME: We should use a union here. */
579 s->u.syment.n_value =
580 ((combined_entry_type *) s->u.syment.n_value)->offset;
585 /* The value is the offset into the line number entries
586 for the symbol's section. On output, the symbol's
587 section should be N_DEBUG. */
588 s->u.syment.n_value =
589 (coff_symbol_ptr->symbol.section->output_section->line_filepos
590 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
591 coff_symbol_ptr->symbol.section =
592 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
593 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
595 for (i = 0; i < s->u.syment.n_numaux; i++)
597 combined_entry_type *a = s + i + 1;
600 a->u.auxent.x_sym.x_tagndx.l =
601 a->u.auxent.x_sym.x_tagndx.p->offset;
606 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
607 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
612 a->u.auxent.x_csect.x_scnlen.l =
613 a->u.auxent.x_csect.x_scnlen.p->offset;
622 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
623 debug_string_section_p, debug_string_size_p)
626 combined_entry_type *native;
627 bfd_size_type *string_size_p;
628 asection **debug_string_section_p;
629 bfd_size_type *debug_string_size_p;
631 unsigned int name_length;
632 union internal_auxent *auxent;
633 char *name = (char *) (symbol->name);
635 if (name == (char *) NULL)
637 /* coff symbols always have names, so we'll make one up */
638 symbol->name = "strange";
639 name = (char *) symbol->name;
641 name_length = strlen (name);
643 if (native->u.syment.n_sclass == C_FILE
644 && native->u.syment.n_numaux > 0)
646 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
647 auxent = &(native + 1)->u.auxent;
649 if (bfd_coff_long_filenames (abfd))
651 if (name_length <= FILNMLEN)
653 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
657 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
658 auxent->x_file.x_n.x_zeroes = 0;
659 *string_size_p += name_length + 1;
664 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
665 if (name_length > FILNMLEN)
667 name[FILNMLEN] = '\0';
673 if (name_length <= SYMNMLEN)
675 /* This name will fit into the symbol neatly */
676 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
678 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
680 native->u.syment._n._n_n._n_offset = (*string_size_p
682 native->u.syment._n._n_n._n_zeroes = 0;
683 *string_size_p += name_length + 1;
690 /* This name should be written into the .debug section. For
691 some reason each name is preceded by a two byte length
692 and also followed by a null byte. FIXME: We assume that
693 the .debug section has already been created, and that it
695 if (*debug_string_section_p == (asection *) NULL)
696 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
697 filepos = bfd_tell (abfd);
698 bfd_put_16 (abfd, name_length + 1, buf);
699 if (!bfd_set_section_contents (abfd,
700 *debug_string_section_p,
702 (file_ptr) *debug_string_size_p,
704 || !bfd_set_section_contents (abfd,
705 *debug_string_section_p,
707 ((file_ptr) *debug_string_size_p
709 (bfd_size_type) name_length + 1))
711 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
713 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
714 native->u.syment._n._n_n._n_zeroes = 0;
715 *debug_string_size_p += name_length + 3;
720 /* We need to keep track of the symbol index so that when we write out
721 the relocs we can get the index for a symbol. This method is a
724 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
726 /* Write a symbol out to a COFF file. */
729 coff_write_symbol (abfd, symbol, native, written, string_size_p,
730 debug_string_section_p, debug_string_size_p)
733 combined_entry_type *native;
734 unsigned int *written;
735 bfd_size_type *string_size_p;
736 asection **debug_string_section_p;
737 bfd_size_type *debug_string_size_p;
739 unsigned int numaux = native->u.syment.n_numaux;
740 int type = native->u.syment.n_type;
741 int class = native->u.syment.n_sclass;
743 bfd_size_type symesz;
745 if (native->u.syment.n_sclass == C_FILE)
746 symbol->flags |= BSF_DEBUGGING;
748 if (symbol->flags & BSF_DEBUGGING
749 && bfd_is_abs_section (symbol->section))
751 native->u.syment.n_scnum = N_DEBUG;
753 else if (bfd_is_abs_section (symbol->section))
755 native->u.syment.n_scnum = N_ABS;
757 else if (bfd_is_und_section (symbol->section))
759 native->u.syment.n_scnum = N_UNDEF;
763 native->u.syment.n_scnum =
764 symbol->section->output_section->target_index;
767 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
768 debug_string_section_p, debug_string_size_p);
770 symesz = bfd_coff_symesz (abfd);
771 buf = bfd_alloc (abfd, symesz);
774 bfd_set_error (bfd_error_no_memory);
777 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
778 if (bfd_write (buf, 1, symesz, abfd) != symesz)
780 bfd_release (abfd, buf);
782 if (native->u.syment.n_numaux > 0)
784 bfd_size_type auxesz;
787 auxesz = bfd_coff_auxesz (abfd);
788 buf = bfd_alloc (abfd, auxesz);
791 bfd_set_error (bfd_error_no_memory);
794 for (j = 0; j < native->u.syment.n_numaux; j++)
796 bfd_coff_swap_aux_out (abfd,
797 &((native + j + 1)->u.auxent),
801 native->u.syment.n_numaux,
803 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
806 bfd_release (abfd, buf);
809 /* Store the index for use when we write out the relocs. */
810 set_index (symbol, *written);
812 *written += numaux + 1;
816 /* Write out a symbol to a COFF file that does not come from a COFF
817 file originally. This symbol may have been created by the linker,
818 or we may be linking a non COFF file to a COFF file. */
821 coff_write_alien_symbol (abfd, symbol, written, string_size_p,
822 debug_string_section_p, debug_string_size_p)
825 unsigned int *written;
826 bfd_size_type *string_size_p;
827 asection **debug_string_section_p;
828 bfd_size_type *debug_string_size_p;
830 combined_entry_type *native;
831 combined_entry_type dummy;
834 native->u.syment.n_type = T_NULL;
835 native->u.syment.n_flags = 0;
836 if (bfd_is_und_section (symbol->section))
838 native->u.syment.n_scnum = N_UNDEF;
839 native->u.syment.n_value = symbol->value;
841 else if (bfd_is_com_section (symbol->section))
843 native->u.syment.n_scnum = N_UNDEF;
844 native->u.syment.n_value = symbol->value;
846 else if (symbol->flags & BSF_DEBUGGING)
848 /* There isn't much point to writing out a debugging symbol
849 unless we are prepared to convert it into COFF debugging
850 format. So, we just ignore them. We must clobber the symbol
851 name to keep it from being put in the string table. */
857 native->u.syment.n_scnum =
858 symbol->section->output_section->target_index;
859 native->u.syment.n_value = (symbol->value
860 + symbol->section->output_section->vma
861 + symbol->section->output_offset);
863 /* Copy the any flags from the the file header into the symbol.
866 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
867 if (c != (coff_symbol_type *) NULL)
868 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
872 native->u.syment.n_type = 0;
873 if (symbol->flags & BSF_LOCAL)
874 native->u.syment.n_sclass = C_STAT;
876 native->u.syment.n_sclass = C_EXT;
877 native->u.syment.n_numaux = 0;
879 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
880 debug_string_section_p, debug_string_size_p);
883 /* Write a native symbol to a COFF file. */
886 coff_write_native_symbol (abfd, symbol, written, string_size_p,
887 debug_string_section_p, debug_string_size_p)
889 coff_symbol_type *symbol;
890 unsigned int *written;
891 bfd_size_type *string_size_p;
892 asection **debug_string_section_p;
893 bfd_size_type *debug_string_size_p;
895 combined_entry_type *native = symbol->native;
896 alent *lineno = symbol->lineno;
898 /* If this symbol has an associated line number, we must store the
899 symbol index in the line number field. We also tag the auxent to
900 point to the right place in the lineno table. */
901 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
903 unsigned int count = 0;
904 lineno[count].u.offset = *written;
905 if (native->u.syment.n_numaux)
907 union internal_auxent *a = &((native + 1)->u.auxent);
909 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
910 symbol->symbol.section->output_section->moving_line_filepos;
913 /* Count and relocate all other linenumbers. */
915 while (lineno[count].line_number != 0)
919 I've been told this, but still need proof:
920 > The second bug is also in `bfd/coffcode.h'. This bug
921 > causes the linker to screw up the pc-relocations for
922 > all the line numbers in COFF code. This bug isn't only
923 > specific to A29K implementations, but affects all
924 > systems using COFF format binaries. Note that in COFF
925 > object files, the line number core offsets output by
926 > the assembler are relative to the start of each
927 > procedure, not to the start of the .text section. This
928 > patch relocates the line numbers relative to the
929 > `native->u.syment.n_value' instead of the section
931 > modular!olson@cs.arizona.edu (Jon Olson)
933 lineno[count].u.offset += native->u.syment.n_value;
935 lineno[count].u.offset +=
936 (symbol->symbol.section->output_section->vma
937 + symbol->symbol.section->output_offset);
941 symbol->done_lineno = true;
943 symbol->symbol.section->output_section->moving_line_filepos +=
944 count * bfd_coff_linesz (abfd);
947 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
948 string_size_p, debug_string_section_p,
949 debug_string_size_p);
952 /* Write out the COFF symbols. */
955 coff_write_symbols (abfd)
958 bfd_size_type string_size;
959 asection *debug_string_section;
960 bfd_size_type debug_string_size;
962 unsigned int limit = bfd_get_symcount (abfd);
963 unsigned int written = 0;
967 debug_string_section = NULL;
968 debug_string_size = 0;
970 /* Seek to the right place */
971 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
974 /* Output all the symbols we have */
977 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
979 asymbol *symbol = *p;
980 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
982 if (c_symbol == (coff_symbol_type *) NULL
983 || c_symbol->native == (combined_entry_type *) NULL)
985 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
986 &debug_string_section,
992 if (!coff_write_native_symbol (abfd, c_symbol, &written,
993 &string_size, &debug_string_section,
999 obj_raw_syment_count (abfd) = written;
1001 /* Now write out strings */
1003 if (string_size != 0)
1005 unsigned int size = string_size + STRING_SIZE_SIZE;
1006 bfd_byte buffer[STRING_SIZE_SIZE];
1008 #if STRING_SIZE_SIZE == 4
1009 bfd_h_put_32 (abfd, size, buffer);
1011 #error Change bfd_h_put_32
1013 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1015 for (p = abfd->outsymbols, i = 0;
1020 size_t name_length = strlen (q->name);
1021 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1024 /* Figure out whether the symbol name should go in the string
1025 table. Symbol names that are short enough are stored
1026 directly in the syment structure. File names permit a
1027 different, longer, length in the syment structure. On
1028 XCOFF, some symbol names are stored in the .debug section
1029 rather than in the string table. */
1031 if (c_symbol == NULL
1032 || c_symbol->native == NULL)
1034 /* This is not a COFF symbol, so it certainly is not a
1035 file name, nor does it go in the .debug section. */
1038 else if (bfd_coff_symname_in_debug (abfd,
1039 &c_symbol->native->u.syment))
1041 /* This symbol name is in the XCOFF .debug section.
1042 Don't write it into the string table. */
1043 maxlen = name_length;
1045 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1046 && c_symbol->native->u.syment.n_numaux > 0)
1051 if (name_length > maxlen)
1053 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1061 /* We would normally not write anything here, but we'll write
1062 out 4 so that any stupid coff reader which tries to read the
1063 string table even when there isn't one won't croak. */
1064 unsigned int size = STRING_SIZE_SIZE;
1065 bfd_byte buffer[STRING_SIZE_SIZE];
1067 #if STRING_SIZE_SIZE == 4
1068 bfd_h_put_32 (abfd, size, buffer);
1070 #error Change bfd_h_put_32
1072 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1073 != STRING_SIZE_SIZE)
1077 /* Make sure the .debug section was created to be the correct size.
1078 We should create it ourselves on the fly, but we don't because
1079 BFD won't let us write to any section until we know how large all
1080 the sections are. We could still do it by making another pass
1081 over the symbols. FIXME. */
1082 BFD_ASSERT (debug_string_size == 0
1083 || (debug_string_section != (asection *) NULL
1084 && (BFD_ALIGN (debug_string_size,
1085 1 << debug_string_section->alignment_power)
1086 == bfd_section_size (abfd, debug_string_section))));
1092 coff_write_linenumbers (abfd)
1096 bfd_size_type linesz;
1099 linesz = bfd_coff_linesz (abfd);
1100 buff = bfd_alloc (abfd, linesz);
1103 bfd_set_error (bfd_error_no_memory);
1106 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1108 if (s->lineno_count)
1110 asymbol **q = abfd->outsymbols;
1111 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1113 /* Find all the linenumbers in this section */
1117 if (p->section->output_section == s)
1120 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1121 (bfd_asymbol_bfd (p), p));
1124 /* Found a linenumber entry, output */
1125 struct internal_lineno out;
1126 memset ((PTR) & out, 0, sizeof (out));
1128 out.l_addr.l_symndx = l->u.offset;
1129 bfd_coff_swap_lineno_out (abfd, &out, buff);
1130 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1133 while (l->line_number)
1135 out.l_lnno = l->line_number;
1136 out.l_addr.l_symndx = l->u.offset;
1137 bfd_coff_swap_lineno_out (abfd, &out, buff);
1138 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1148 bfd_release (abfd, buff);
1154 coff_get_lineno (ignore_abfd, symbol)
1158 return coffsymbol (symbol)->lineno;
1162 coff_section_symbol (abfd, name)
1166 asection *sec = bfd_make_section_old_way (abfd, name);
1168 combined_entry_type *csym;
1171 csym = coff_symbol_from (abfd, sym)->native;
1172 /* Make sure back-end COFF stuff is there. */
1177 coff_symbol_type sym;
1178 /* @@FIXME This shouldn't use a fixed size!! */
1179 combined_entry_type e[10];
1182 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1185 bfd_set_error (bfd_error_no_error);
1188 memset ((char *) f, 0, sizeof (*f));
1189 coff_symbol_from (abfd, sym)->native = csym = f->e;
1191 csym[0].u.syment.n_sclass = C_STAT;
1192 csym[0].u.syment.n_numaux = 1;
1193 /* SF_SET_STATICS (sym); @@ ??? */
1194 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1195 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1196 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1198 if (sec->output_section == NULL)
1200 sec->output_section = sec;
1201 sec->output_offset = 0;
1207 /* This function transforms the offsets into the symbol table into
1208 pointers to syments. */
1211 coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1213 combined_entry_type *table_base;
1214 combined_entry_type *symbol;
1215 unsigned int indaux;
1216 combined_entry_type *auxent;
1218 int type = symbol->u.syment.n_type;
1219 int class = symbol->u.syment.n_sclass;
1221 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1223 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1224 (abfd, table_base, symbol, indaux, auxent))
1228 /* Don't bother if this is a file or a section */
1229 if (class == C_STAT && type == T_NULL)
1231 if (class == C_FILE)
1234 /* Otherwise patch up */
1235 #define N_TMASK coff_data (abfd)->local_n_tmask
1236 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1237 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK)
1238 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1240 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1241 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1242 auxent->fix_end = 1;
1244 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1245 generate one, so we must be careful to ignore it. */
1246 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1248 auxent->u.auxent.x_sym.x_tagndx.p =
1249 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1250 auxent->fix_tag = 1;
1254 /* Allocate space for the ".debug" section, and read it.
1255 We did not read the debug section until now, because
1256 we didn't want to go to the trouble until someone needed it. */
1259 build_debug_section (abfd)
1262 char *debug_section;
1265 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1269 bfd_set_error (bfd_error_no_debug_section);
1273 debug_section = (PTR) bfd_alloc (abfd,
1274 bfd_get_section_size_before_reloc (sect));
1275 if (debug_section == NULL)
1277 bfd_set_error (bfd_error_no_memory);
1281 /* Seek to the beginning of the `.debug' section and read it.
1282 Save the current position first; it is needed by our caller.
1283 Then read debug section and reset the file pointer. */
1285 position = bfd_tell (abfd);
1286 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1287 || (bfd_read (debug_section,
1288 bfd_get_section_size_before_reloc (sect), 1, abfd)
1289 != bfd_get_section_size_before_reloc (sect))
1290 || bfd_seek (abfd, position, SEEK_SET) != 0)
1292 return debug_section;
1296 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1297 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1298 be \0-terminated. */
1300 copy_name (abfd, name, maxlen)
1308 for (len = 0; len < maxlen; ++len)
1310 if (name[len] == '\0')
1316 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1318 bfd_set_error (bfd_error_no_memory);
1321 strncpy (newname, name, len);
1322 newname[len] = '\0';
1326 /* Read in the external symbols. */
1329 _bfd_coff_get_external_symbols (abfd)
1332 bfd_size_type symesz;
1336 if (obj_coff_external_syms (abfd) != NULL)
1339 symesz = bfd_coff_symesz (abfd);
1341 size = obj_raw_syment_count (abfd) * symesz;
1343 syms = malloc (size);
1344 if (syms == NULL && size != 0)
1346 bfd_set_error (bfd_error_no_memory);
1350 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1351 || bfd_read (syms, size, 1, abfd) != size)
1358 obj_coff_external_syms (abfd) = syms;
1363 /* Read in the external strings. The strings are not loaded until
1364 they are needed. This is because we have no simple way of
1365 detecting a missing string table in an archive. */
1368 _bfd_coff_read_string_table (abfd)
1371 char extstrsize[STRING_SIZE_SIZE];
1375 if (obj_coff_strings (abfd) != NULL)
1376 return obj_coff_strings (abfd);
1379 (obj_sym_filepos (abfd)
1380 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1384 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1386 if (bfd_get_error () != bfd_error_file_truncated)
1389 /* There is no string table. */
1390 strsize = STRING_SIZE_SIZE;
1394 #if STRING_SIZE_SIZE == 4
1395 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1397 #error Change bfd_h_get_32
1401 strings = malloc (strsize);
1402 if (strings == NULL)
1404 bfd_set_error (bfd_error_no_memory);
1408 if (bfd_read (strings + STRING_SIZE_SIZE,
1409 strsize - STRING_SIZE_SIZE, 1, abfd)
1410 != strsize - STRING_SIZE_SIZE)
1416 obj_coff_strings (abfd) = strings;
1421 /* Free up the external symbols and strings read from a COFF file. */
1424 _bfd_coff_free_symbols (abfd)
1427 if (obj_coff_external_syms (abfd) != NULL
1428 && ! obj_coff_keep_syms (abfd))
1430 free (obj_coff_external_syms (abfd));
1431 obj_coff_external_syms (abfd) = NULL;
1433 if (obj_coff_strings (abfd) != NULL
1434 && ! obj_coff_keep_strings (abfd))
1436 free (obj_coff_strings (abfd));
1437 obj_coff_strings (abfd) = NULL;
1442 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1443 knit the symbol names into a normalized form. By normalized here I
1444 mean that all symbols have an n_offset pointer that points to a null-
1445 terminated string. */
1447 combined_entry_type *
1448 coff_get_normalized_symtab (abfd)
1451 combined_entry_type *internal;
1452 combined_entry_type *internal_ptr;
1453 combined_entry_type *symbol_ptr;
1454 combined_entry_type *internal_end;
1455 bfd_size_type symesz;
1458 const char *string_table = NULL;
1459 char *debug_section = NULL;
1462 if (obj_raw_syments (abfd) != NULL)
1463 return obj_raw_syments (abfd);
1465 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1466 internal = (combined_entry_type *) bfd_alloc (abfd, size);
1467 if (internal == NULL && size != 0)
1469 bfd_set_error (bfd_error_no_memory);
1472 internal_end = internal + obj_raw_syment_count (abfd);
1474 if (! _bfd_coff_get_external_symbols (abfd))
1477 raw_src = (char *) obj_coff_external_syms (abfd);
1479 /* mark the end of the symbols */
1480 symesz = bfd_coff_symesz (abfd);
1481 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1483 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1484 probably possible. If one shows up, it will probably kill us. */
1486 /* Swap all the raw entries */
1487 for (internal_ptr = internal;
1489 raw_src += symesz, internal_ptr++)
1493 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1494 (PTR) & internal_ptr->u.syment);
1495 internal_ptr->fix_value = 0;
1496 internal_ptr->fix_tag = 0;
1497 internal_ptr->fix_end = 0;
1498 internal_ptr->fix_scnlen = 0;
1499 symbol_ptr = internal_ptr;
1502 i < symbol_ptr->u.syment.n_numaux;
1508 internal_ptr->fix_value = 0;
1509 internal_ptr->fix_tag = 0;
1510 internal_ptr->fix_end = 0;
1511 internal_ptr->fix_scnlen = 0;
1512 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1513 symbol_ptr->u.syment.n_type,
1514 symbol_ptr->u.syment.n_sclass,
1515 i, symbol_ptr->u.syment.n_numaux,
1516 &(internal_ptr->u.auxent));
1517 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1522 /* Free the raw symbols, but not the strings (if we have them). */
1523 obj_coff_keep_strings (abfd) = true;
1524 if (! _bfd_coff_free_symbols (abfd))
1527 for (internal_ptr = internal; internal_ptr < internal_end;
1530 if (internal_ptr->u.syment.n_sclass == C_FILE
1531 && internal_ptr->u.syment.n_numaux > 0)
1533 /* make a file symbol point to the name in the auxent, since
1534 the text ".file" is redundant */
1535 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1537 /* the filename is a long one, point into the string table */
1538 if (string_table == NULL)
1540 string_table = _bfd_coff_read_string_table (abfd);
1541 if (string_table == NULL)
1545 internal_ptr->u.syment._n._n_n._n_offset =
1548 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1552 /* ordinary short filename, put into memory anyway */
1553 internal_ptr->u.syment._n._n_n._n_offset = (long)
1554 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1560 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1562 /* This is a "short" name. Make it long. */
1563 unsigned long i = 0;
1564 char *newstring = NULL;
1566 /* find the length of this string without walking into memory
1568 for (i = 0; i < 8; ++i)
1570 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1573 } /* if end of string */
1574 } /* possible lengths of this string. */
1576 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1578 bfd_set_error (bfd_error_no_memory);
1581 memset (newstring, 0, i);
1582 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1583 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1584 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1586 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1587 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1588 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1590 /* Long name already. Point symbol at the string in the
1592 if (string_table == NULL)
1594 string_table = _bfd_coff_read_string_table (abfd);
1595 if (string_table == NULL)
1598 internal_ptr->u.syment._n._n_n._n_offset =
1601 + internal_ptr->u.syment._n._n_n._n_offset));
1605 /* Long name in debug section. Very similar. */
1606 if (debug_section == NULL)
1607 debug_section = build_debug_section (abfd);
1608 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1609 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1612 internal_ptr += internal_ptr->u.syment.n_numaux;
1615 obj_raw_syments (abfd) = internal;
1616 BFD_ASSERT (obj_raw_syment_count (abfd)
1617 == (unsigned int) (internal_ptr - internal));
1620 } /* coff_get_normalized_symtab() */
1623 coff_get_reloc_upper_bound (abfd, asect)
1627 if (bfd_get_format (abfd) != bfd_object)
1629 bfd_set_error (bfd_error_invalid_operation);
1632 return (asect->reloc_count + 1) * sizeof (arelent *);
1636 coff_make_empty_symbol (abfd)
1639 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1642 bfd_set_error (bfd_error_no_memory);
1645 memset (new, 0, sizeof *new);
1646 new->symbol.section = 0;
1648 new->lineno = (alent *) NULL;
1649 new->done_lineno = false;
1650 new->symbol.the_bfd = abfd;
1651 return &new->symbol;
1654 /* Make a debugging symbol. */
1657 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1662 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1665 bfd_set_error (bfd_error_no_memory);
1668 /* @@ This shouldn't be using a constant multiplier. */
1669 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1672 bfd_set_error (bfd_error_no_memory);
1675 new->symbol.section = bfd_abs_section_ptr;
1676 new->symbol.flags = BSF_DEBUGGING;
1677 new->lineno = (alent *) NULL;
1678 new->done_lineno = false;
1679 new->symbol.the_bfd = abfd;
1680 return &new->symbol;
1685 coff_get_symbol_info (abfd, symbol, ret)
1690 bfd_symbol_info (symbol, ret);
1693 /* Print out information about COFF symbol. */
1696 coff_print_symbol (abfd, filep, symbol, how)
1700 bfd_print_symbol_type how;
1702 FILE *file = (FILE *) filep;
1706 case bfd_print_symbol_name:
1707 fprintf (file, "%s", symbol->name);
1710 case bfd_print_symbol_more:
1711 fprintf (file, "coff %s %s",
1712 coffsymbol (symbol)->native ? "n" : "g",
1713 coffsymbol (symbol)->lineno ? "l" : " ");
1716 case bfd_print_symbol_all:
1717 if (coffsymbol (symbol)->native)
1721 combined_entry_type *combined = coffsymbol (symbol)->native;
1722 combined_entry_type *root = obj_raw_syments (abfd);
1723 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1725 fprintf (file, "[%3ld]", (long) (combined - root));
1727 if (! combined->fix_value)
1728 val = (unsigned long) combined->u.syment.n_value;
1730 val = ((unsigned long)
1731 ((combined_entry_type *) combined->u.syment.n_value
1735 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1736 combined->u.syment.n_scnum,
1737 combined->u.syment.n_flags,
1738 combined->u.syment.n_type,
1739 combined->u.syment.n_sclass,
1740 combined->u.syment.n_numaux,
1744 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1746 combined_entry_type *auxp = combined + aux + 1;
1750 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1752 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1754 fprintf (file, "\n");
1756 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1759 switch (combined->u.syment.n_sclass)
1762 fprintf (file, "File ");
1766 if (combined->u.syment.n_type == T_NULL)
1767 /* probably a section symbol? */
1769 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1770 (long) auxp->u.auxent.x_scn.x_scnlen,
1771 auxp->u.auxent.x_scn.x_nreloc,
1772 auxp->u.auxent.x_scn.x_nlinno);
1775 /* else fall through */
1778 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1779 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1780 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1783 fprintf (file, " endndx %ld",
1785 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1793 fprintf (file, "\n%s :", l->u.sym->name);
1795 while (l->line_number)
1797 fprintf (file, "\n%4d : 0x%lx",
1800 (l->u.offset + symbol->section->vma)));
1807 bfd_print_symbol_vandf ((PTR) file, symbol);
1808 fprintf (file, " %-5s %s %s %s",
1809 symbol->section->name,
1810 coffsymbol (symbol)->native ? "n" : "g",
1811 coffsymbol (symbol)->lineno ? "l" : " ",
1817 /* Provided a BFD, a section and an offset into the section, calculate
1818 and return the name of the source file and the line nearest to the
1823 coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1824 functionname_ptr, line_ptr)
1827 asymbol **ignore_symbols;
1829 CONST char **filename_ptr;
1830 CONST char **functionname_ptr;
1831 unsigned int *line_ptr;
1834 unsigned int line_base;
1835 coff_data_type *cof = coff_data (abfd);
1836 /* Run through the raw syments if available */
1837 combined_entry_type *p;
1838 combined_entry_type *pend;
1840 struct coff_section_tdata *sec_data;
1843 *functionname_ptr = 0;
1846 /* Don't try and find line numbers in a non coff file */
1847 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1853 /* Find the first C_FILE symbol. */
1854 p = cof->raw_syments;
1855 pend = p + cof->raw_syment_count;
1858 if (p->u.syment.n_sclass == C_FILE)
1860 p += 1 + p->u.syment.n_numaux;
1867 /* Look through the C_FILE symbols to find the best one. */
1868 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1869 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
1872 combined_entry_type *p2;
1874 for (p2 = p + 1 + p->u.syment.n_numaux;
1876 p2 += 1 + p2->u.syment.n_numaux)
1878 if (p2->u.syment.n_scnum > 0
1880 == coff_section_from_bfd_index (abfd,
1881 p2->u.syment.n_scnum)))
1883 if (p2->u.syment.n_sclass == C_FILE)
1891 && offset >= (bfd_vma) p2->u.syment.n_value
1892 && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
1894 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1895 maxdiff = offset - p2->u.syment.n_value;
1898 /* Avoid endless loops on erroneous files by ensuring that
1899 we always move forward in the file. */
1900 if (p - cof->raw_syments >= p->u.syment.n_value)
1903 p = cof->raw_syments + p->u.syment.n_value;
1904 if (p > pend || p->u.syment.n_sclass != C_FILE)
1909 /* Now wander though the raw linenumbers of the section */
1910 /* If we have been called on this section before, and the offset we
1911 want is further down then we can prime the lookup loop. */
1912 sec_data = coff_section_data (abfd, section);
1913 if (sec_data != NULL
1915 && offset >= sec_data->offset)
1918 *functionname_ptr = sec_data->function;
1919 line_base = sec_data->line_base;
1927 l = §ion->lineno[i];
1929 for (; i < section->lineno_count; i++)
1931 if (l->line_number == 0)
1933 /* Get the symbol this line number points at */
1934 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1935 if (coff->symbol.value > offset)
1937 *functionname_ptr = coff->symbol.name;
1940 combined_entry_type *s = coff->native;
1941 s = s + 1 + s->u.syment.n_numaux;
1943 /* In XCOFF a debugging symbol can follow the function
1945 if (s->u.syment.n_scnum == N_DEBUG)
1946 s = s + 1 + s->u.syment.n_numaux;
1949 S should now point to the .bf of the function
1951 if (s->u.syment.n_numaux)
1954 The linenumber is stored in the auxent
1956 union internal_auxent *a = &((s + 1)->u.auxent);
1957 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1958 *line_ptr = line_base;
1964 if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
1966 *line_ptr = l->line_number + line_base - 1;
1971 /* Cache the results for the next call. */
1972 if (sec_data == NULL)
1974 section->used_by_bfd =
1975 ((PTR) bfd_zalloc (abfd,
1976 sizeof (struct coff_section_tdata)));
1977 sec_data = section->used_by_bfd;
1979 if (sec_data != NULL)
1981 sec_data->offset = offset;
1983 sec_data->function = *functionname_ptr;
1984 sec_data->line_base = line_base;
1991 coff_sizeof_headers (abfd, reloc)
1999 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2003 size = bfd_coff_filhsz (abfd);
2006 size += abfd->section_count * bfd_coff_scnhsz (abfd);