1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 boolean coff_write_symbol PARAMS ((bfd *, asymbol *,
45 combined_entry_type *,
47 static boolean coff_write_alien_symbol PARAMS ((bfd *, asymbol *,
49 static boolean coff_write_native_symbol PARAMS ((bfd *, coff_symbol_type *,
52 static asection bfd_debug_section = { "*DEBUG*" };
54 /* Take a section header read from a coff file (in HOST byte order),
55 and make a BFD "section" out of it. This is used by ECOFF. */
57 make_a_section_from_file (abfd, hdr, target_index)
59 struct internal_scnhdr *hdr;
60 unsigned int target_index;
62 asection *return_section;
65 /* Assorted wastage to null-terminate the name, thanks AT&T! */
66 name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
68 bfd_set_error (bfd_error_no_memory);
71 strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
72 name[sizeof (hdr->s_name)] = 0;
74 return_section = bfd_make_section(abfd, name);
75 if (return_section == NULL)
76 return_section = bfd_coff_make_section_hook (abfd, name);
78 /* Handle several sections of the same name. For example, if an executable
79 has two .bss sections, GDB better be able to find both of them
81 if (return_section == NULL)
82 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->_raw_size = hdr->s_size;
91 return_section->filepos = hdr->s_scnptr;
92 return_section->rel_filepos = hdr->s_relptr;
93 return_section->reloc_count = hdr->s_nreloc;
95 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
97 return_section->line_filepos = hdr->s_lnnoptr;
99 return_section->lineno_count = hdr->s_nlnno;
100 return_section->userdata = NULL;
101 return_section->next = (asection *) NULL;
102 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
104 return_section->target_index = target_index;
106 /* At least on i386-coff, the line number count for a shared library
107 section must be ignored. */
108 if ((return_section->flags & SEC_SHARED_LIBRARY) != 0)
109 return_section->lineno_count = 0;
111 if (hdr->s_nreloc != 0)
112 return_section->flags |= SEC_RELOC;
113 /* FIXME: should this check 'hdr->s_size > 0' */
114 if (hdr->s_scnptr != 0)
115 return_section->flags |= SEC_HAS_CONTENTS;
119 /* Read in a COFF object and make it into a BFD. This is used by
124 coff_real_object_p (abfd, nscns, internal_f, internal_a)
127 struct internal_filehdr *internal_f;
128 struct internal_aouthdr *internal_a;
131 size_t readsize; /* length of file_info */
133 char *external_sections;
135 /* Build a play area */
136 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
140 scnhsz = bfd_coff_scnhsz (abfd);
141 readsize = nscns * scnhsz;
142 external_sections = (char *)bfd_alloc(abfd, readsize);
143 if (!external_sections)
145 bfd_set_error (bfd_error_no_memory);
149 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
153 /* Now copy data as required; construct all asections etc */
156 for (i = 0; i < nscns; i++) {
157 struct internal_scnhdr tmp;
158 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
160 make_a_section_from_file(abfd,&tmp, i+1);
164 /* make_abs_section(abfd);*/
166 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
169 if (!(internal_f->f_flags & F_RELFLG))
170 abfd->flags |= HAS_RELOC;
171 if ((internal_f->f_flags & F_EXEC))
172 abfd->flags |= EXEC_P;
173 if (!(internal_f->f_flags & F_LNNO))
174 abfd->flags |= HAS_LINENO;
175 if (!(internal_f->f_flags & F_LSYMS))
176 abfd->flags |= HAS_LOCALS;
179 bfd_get_symcount(abfd) = internal_f->f_nsyms;
180 if (internal_f->f_nsyms)
181 abfd->flags |= HAS_SYMS;
183 if (internal_a != (struct internal_aouthdr *) NULL)
184 bfd_get_start_address (abfd) = internal_a->entry;
186 bfd_get_start_address (abfd) = 0;
190 bfd_release(abfd, tdata);
191 return (bfd_target *)NULL;
194 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
195 not a COFF file. This is also used by ECOFF. */
205 struct internal_filehdr internal_f;
206 struct internal_aouthdr internal_a;
208 /* figure out how much to read */
209 filhsz = bfd_coff_filhsz (abfd);
210 aoutsz = bfd_coff_aoutsz (abfd);
212 filehdr = bfd_alloc (abfd, filhsz);
215 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
217 if (bfd_get_error () != bfd_error_system_call)
218 bfd_set_error (bfd_error_wrong_format);
221 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
222 bfd_release (abfd, filehdr);
224 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
225 bfd_set_error (bfd_error_wrong_format);
228 nscns =internal_f.f_nscns;
230 if (internal_f.f_opthdr) {
233 opthdr = bfd_alloc (abfd, aoutsz);
236 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
239 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
242 /* Seek past the opt hdr stuff */
243 bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
245 return coff_real_object_p(abfd, nscns, &internal_f,
246 (internal_f.f_opthdr != 0
248 : (struct internal_aouthdr *) NULL));
251 /* Get the BFD section from a COFF symbol section number. */
254 coff_section_from_bfd_index (abfd, index)
258 struct sec *answer = abfd->sections;
262 return &bfd_abs_section;
264 if (index == N_UNDEF)
266 return &bfd_und_section;
270 return &bfd_debug_section;
275 if (answer->target_index == index)
277 answer = answer->next;
280 return &bfd_und_section; /* For gcc -W and lint. Never executed. */
283 /* Get the upper bound of a COFF symbol table. */
286 coff_get_symtab_upper_bound(abfd)
289 if (!bfd_coff_slurp_symbol_table(abfd))
292 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
296 /* Canonicalize a COFF symbol table. */
299 coff_get_symtab (abfd, alocation)
303 unsigned int counter = 0;
304 coff_symbol_type *symbase;
305 coff_symbol_type **location = (coff_symbol_type **) (alocation);
306 if (!bfd_coff_slurp_symbol_table(abfd))
309 symbase = obj_symbols(abfd);
310 while (counter < bfd_get_symcount(abfd))
312 /* This nasty code looks at the symbol to decide whether or
313 not it is descibes a constructor/destructor entry point. It
314 is structured this way to (hopefully) speed non matches */
316 if (0 && symbase->symbol.name[9] == '$')
318 bfd_constructor_entry(abfd,
319 (asymbol **)location,
320 symbase->symbol.name[10] == 'I' ?
324 *(location++) = symbase++;
328 return bfd_get_symcount(abfd);
331 /* Set lineno_count for the output sections of a COFF file. */
334 coff_count_linenumbers (abfd)
337 unsigned int limit = bfd_get_symcount(abfd);
342 asection *s = abfd->sections->output_section;
344 BFD_ASSERT(s->lineno_count == 0);
350 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
351 asymbol *q_maybe = *p;
352 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
353 coff_symbol_type *q = coffsymbol(q_maybe);
356 This symbol has a linenumber, increment the owning
357 section's linenumber count
359 alent *l = q->lineno;
360 q->symbol.section->output_section->lineno_count++;
363 while (l->line_number) {
365 q->symbol.section->output_section->lineno_count++;
374 /* Takes a bfd and a symbol, returns a pointer to the coff specific
375 area of the symbol if there is one. */
379 coff_symbol_from (ignore_abfd, symbol)
383 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
384 return (coff_symbol_type *)NULL;
386 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
387 return (coff_symbol_type *)NULL;
389 return (coff_symbol_type *) symbol;
393 fixup_symbol_value (coff_symbol_ptr, syment)
394 coff_symbol_type *coff_symbol_ptr;
395 struct internal_syment *syment;
398 /* Normalize the symbol flags */
399 if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
400 /* a common symbol is undefined with a value */
401 syment->n_scnum = N_UNDEF;
402 syment->n_value = coff_symbol_ptr->symbol.value;
404 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
405 syment->n_value = coff_symbol_ptr->symbol.value;
407 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
408 syment->n_scnum = N_UNDEF;
412 if (coff_symbol_ptr->symbol.section) {
414 coff_symbol_ptr->symbol.section->output_section->target_index;
417 coff_symbol_ptr->symbol.value +
418 coff_symbol_ptr->symbol.section->output_offset +
419 coff_symbol_ptr->symbol.section->output_section->vma;
423 /* This can happen, but I don't know why yet (steve@cygnus.com) */
424 syment->n_scnum = N_ABS;
425 syment->n_value = coff_symbol_ptr->symbol.value;
430 /* run through all the symbols in the symbol table and work out what
431 their indexes into the symbol table will be when output
433 Coff requires that each C_FILE symbol points to the next one in the
434 chain, and that the last one points to the first external symbol. We
439 coff_renumber_symbols (bfd_ptr)
442 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
443 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
444 unsigned int native_index = 0;
445 struct internal_syment *last_file = (struct internal_syment *)NULL;
446 unsigned int symbol_index;
448 /* COFF demands that undefined symbols come after all other symbols.
449 Since we don't need to impose this extra knowledge on all our client
450 programs, deal with that here. Sort the symbol table; just move the
451 undefined symbols to the end, leaving the rest alone. */
452 /* @@ Do we have some condition we could test for, so we don't always
453 have to do this? I don't think relocatability is quite right, but
454 I'm not certain. [raeburn:19920508.1711EST] */
459 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
461 * (symbol_count + 1));
464 bfd_set_error (bfd_error_no_memory);
467 bfd_ptr->outsymbols = newsyms;
468 for (i = 0; i < symbol_count; i++)
469 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
470 *newsyms++ = symbol_ptr_ptr[i];
471 for (i = 0; i < symbol_count; i++)
472 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
473 *newsyms++ = symbol_ptr_ptr[i];
474 *newsyms = (asymbol *) NULL;
475 symbol_ptr_ptr = bfd_ptr->outsymbols;
478 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
480 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
481 if (coff_symbol_ptr && coff_symbol_ptr->native) {
482 combined_entry_type *s = coff_symbol_ptr->native;
485 if (s->u.syment.n_sclass == C_FILE)
487 if (last_file != (struct internal_syment *)NULL) {
488 last_file->n_value = native_index;
490 last_file = &(s->u.syment);
494 /* Modify the symbol values according to their section and
497 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
499 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
500 s[i].offset = native_index ++;
507 obj_conv_table_size (bfd_ptr) = native_index;
512 Run thorough the symbol table again, and fix it so that all pointers to
513 entries are changed to the entries' index in the output symbol table.
517 coff_mangle_symbols (bfd_ptr)
520 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
521 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
522 unsigned int symbol_index;
524 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
526 coff_symbol_type *coff_symbol_ptr =
527 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
529 if (coff_symbol_ptr && coff_symbol_ptr->native)
532 combined_entry_type *s = coff_symbol_ptr->native;
536 /* FIXME: We should use a union here. */
537 s->u.syment.n_value =
538 ((combined_entry_type *) s->u.syment.n_value)->offset;
541 for (i = 0; i < s->u.syment.n_numaux ; i++)
543 combined_entry_type *a = s + i + 1;
546 a->u.auxent.x_sym.x_tagndx.l =
547 a->u.auxent.x_sym.x_tagndx.p->offset;
552 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
553 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
558 a->u.auxent.x_csect.x_scnlen.l =
559 a->u.auxent.x_csect.x_scnlen.p->offset;
567 static bfd_size_type string_size;
568 static bfd_size_type debug_string_size;
569 static asection *debug_string_section;
572 coff_fix_symbol_name (abfd, symbol, native)
575 combined_entry_type *native;
577 unsigned int name_length;
578 union internal_auxent *auxent;
579 char * name = ( char *)(symbol->name);
581 if (name == (char *) NULL) {
582 /* coff symbols always have names, so we'll make one up */
583 symbol->name = "strange";
584 name = (char *)symbol->name;
586 name_length = strlen(name);
588 if (native->u.syment.n_sclass == C_FILE) {
589 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
590 auxent = &(native+1)->u.auxent;
592 if (bfd_coff_long_filenames (abfd)) {
593 if (name_length <= FILNMLEN) {
594 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
597 auxent->x_file.x_n.x_offset = string_size + 4;
598 auxent->x_file.x_n.x_zeroes = 0;
599 string_size += name_length + 1;
603 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
604 if (name_length > FILNMLEN) {
605 name[FILNMLEN] = '\0';
610 { /* NOT A C_FILE SYMBOL */
611 if (name_length <= SYMNMLEN)
613 /* This name will fit into the symbol neatly */
614 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
616 else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment))
618 native->u.syment._n._n_n._n_offset = string_size + 4;
619 native->u.syment._n._n_n._n_zeroes = 0;
620 string_size += name_length + 1;
627 /* This name should be written into the .debug section. For
628 some reason each name is preceded by a two byte length
629 and also followed by a null byte. FIXME: We assume that
630 the .debug section has already been created, and that it
632 if (debug_string_section == (asection *) NULL)
633 debug_string_section = bfd_get_section_by_name (abfd, ".debug");
634 filepos = bfd_tell (abfd);
635 bfd_put_16 (abfd, name_length + 1, buf);
636 if (! bfd_set_section_contents (abfd,
637 debug_string_section,
639 (file_ptr) debug_string_size,
641 || ! bfd_set_section_contents (abfd,
642 debug_string_section,
644 (file_ptr) debug_string_size + 2,
645 (bfd_size_type) name_length + 1))
647 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
649 native->u.syment._n._n_n._n_offset = debug_string_size + 2;
650 native->u.syment._n._n_n._n_zeroes = 0;
651 debug_string_size += name_length + 3;
656 /* We need to keep track of the symbol index so that when we write out
657 the relocs we can get the index for a symbol. This method is a
660 #define set_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
662 /* Write a symbol out to a COFF file. */
665 coff_write_symbol (abfd, symbol, native, written)
668 combined_entry_type *native;
669 unsigned int *written;
671 unsigned int numaux = native->u.syment.n_numaux;
672 int type = native->u.syment.n_type;
673 int class = native->u.syment.n_sclass;
675 bfd_size_type symesz;
677 /* @@ bfd_debug_section isn't accessible outside this file, but we
678 know that C_FILE symbols belong there. So move them. */
679 if (native->u.syment.n_sclass == C_FILE)
680 symbol->section = &bfd_debug_section;
682 if (symbol->section == &bfd_abs_section)
684 native->u.syment.n_scnum = N_ABS;
686 else if (symbol->section == &bfd_debug_section)
688 native->u.syment.n_scnum = N_DEBUG;
690 else if (symbol->section == &bfd_und_section)
692 native->u.syment.n_scnum = N_UNDEF;
696 native->u.syment.n_scnum =
697 symbol->section->output_section->target_index;
700 coff_fix_symbol_name (abfd, symbol, native);
702 symesz = bfd_coff_symesz (abfd);
703 buf = bfd_alloc (abfd, symesz);
706 bfd_set_error (bfd_error_no_memory);
709 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
710 if (bfd_write (buf, 1, symesz, abfd) != symesz)
712 bfd_release (abfd, buf);
714 if (native->u.syment.n_numaux > 0)
716 bfd_size_type auxesz;
719 auxesz = bfd_coff_auxesz (abfd);
720 buf = bfd_alloc (abfd, auxesz);
723 bfd_set_error (bfd_error_no_memory);
726 for (j = 0; j < native->u.syment.n_numaux; j++)
728 bfd_coff_swap_aux_out (abfd,
729 &((native + j + 1)->u.auxent),
733 native->u.syment.n_numaux,
735 if (bfd_write (buf, 1, auxesz, abfd)!= auxesz)
738 bfd_release (abfd, buf);
741 /* Store the index for use when we write out the relocs. */
742 set_index (symbol, *written);
744 *written += numaux + 1;
748 /* Write out a symbol to a COFF file that does not come from a COFF
749 file originally. This symbol may have been created by the linker,
750 or we may be linking a non COFF file to a COFF file. */
753 coff_write_alien_symbol (abfd, symbol, written)
756 unsigned int *written;
758 combined_entry_type *native;
759 combined_entry_type dummy;
762 native->u.syment.n_type = T_NULL;
763 native->u.syment.n_flags = 0;
764 if (symbol->section == &bfd_und_section)
766 native->u.syment.n_scnum = N_UNDEF;
767 native->u.syment.n_value = symbol->value;
769 else if (bfd_is_com_section (symbol->section))
771 native->u.syment.n_scnum = N_UNDEF;
772 native->u.syment.n_value = symbol->value;
774 else if (symbol->flags & BSF_DEBUGGING)
776 /* Remove the symbol name so that it does not take up any space.
777 COFF won't know what to do with it anyhow. */
782 native->u.syment.n_scnum =
783 symbol->section->output_section->target_index;
784 native->u.syment.n_value = (symbol->value
785 + symbol->section->output_section->vma
786 + symbol->section->output_offset);
788 /* Copy the any flags from the the file header into the symbol.
791 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
792 if (c != (coff_symbol_type *) NULL)
793 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
797 native->u.syment.n_type = 0;
798 if (symbol->flags & BSF_LOCAL)
799 native->u.syment.n_sclass = C_STAT;
801 native->u.syment.n_sclass = C_EXT;
802 native->u.syment.n_numaux = 0;
804 return coff_write_symbol (abfd, symbol, native, written);
807 /* Write a native symbol to a COFF file. */
810 coff_write_native_symbol (abfd, symbol, written)
812 coff_symbol_type *symbol;
813 unsigned int *written;
815 combined_entry_type *native = symbol->native;
816 alent *lineno = symbol->lineno;
818 /* If this symbol has an associated line number, we must store the
819 symbol index in the line number field. We also tag the auxent to
820 point to the right place in the lineno table. */
821 if (lineno && !symbol->done_lineno)
823 unsigned int count = 0;
824 lineno[count].u.offset = *written;
825 if (native->u.syment.n_numaux)
827 union internal_auxent *a = &((native+1)->u.auxent);
829 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
830 symbol->symbol.section->output_section->moving_line_filepos;
833 /* Count and relocate all other linenumbers. */
835 while (lineno[count].line_number != 0)
839 I've been told this, but still need proof:
840 > The second bug is also in `bfd/coffcode.h'. This bug
841 > causes the linker to screw up the pc-relocations for
842 > all the line numbers in COFF code. This bug isn't only
843 > specific to A29K implementations, but affects all
844 > systems using COFF format binaries. Note that in COFF
845 > object files, the line number core offsets output by
846 > the assembler are relative to the start of each
847 > procedure, not to the start of the .text section. This
848 > patch relocates the line numbers relative to the
849 > `native->u.syment.n_value' instead of the section
851 > modular!olson@cs.arizona.edu (Jon Olson)
853 lineno[count].u.offset += native->u.syment.n_value;
855 lineno[count].u.offset +=
856 (symbol->symbol.section->output_section->vma
857 + symbol->symbol.section->output_offset);
861 symbol->done_lineno = true;
863 symbol->symbol.section->output_section->moving_line_filepos +=
864 count * bfd_coff_linesz (abfd);
867 return coff_write_symbol (abfd, &(symbol->symbol), native, written);
870 /* Write out the COFF symbols. */
873 coff_write_symbols (abfd)
877 unsigned int limit = bfd_get_symcount(abfd);
878 unsigned int written = 0;
882 debug_string_size = 0;
884 /* Seek to the right place */
885 if (bfd_seek (abfd, obj_sym_filepos(abfd), SEEK_SET) != 0)
888 /* Output all the symbols we have */
891 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
893 asymbol *symbol = *p;
894 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
896 if (c_symbol == (coff_symbol_type *) NULL
897 || c_symbol->native == (combined_entry_type *)NULL)
899 if (! coff_write_alien_symbol (abfd, symbol, &written))
904 if (! coff_write_native_symbol (abfd, c_symbol, &written))
909 bfd_get_symcount (abfd) = written;
911 /* Now write out strings */
913 if (string_size != 0)
915 unsigned int size = string_size + 4;
918 bfd_h_put_32 (abfd, size, buffer);
919 bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd);
920 for (p = abfd->outsymbols, i = 0;
925 size_t name_length = strlen (q->name);
926 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
929 /* Figure out whether the symbol name should go in the string
930 table. Symbol names that are short enough are stored
931 directly in the syment structure. File names permit a
932 different, longer, length in the syment structure. On
933 XCOFF, some symbol names are stored in the .debug section
934 rather than in the string table. */
937 || c_symbol->native == NULL)
939 /* This is not a COFF symbol, so it certainly is not a
940 file name, nor does it go in the .debug section. */
943 else if (bfd_coff_symname_in_debug (abfd,
944 &c_symbol->native->u.syment))
946 /* This symbol name is in the XCOFF .debug section.
947 Don't write it into the string table. */
948 maxlen = name_length;
950 else if (c_symbol->native->u.syment.n_sclass == C_FILE)
955 if (name_length > maxlen)
957 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
965 /* We would normally not write anything here, but we'll write
966 out 4 so that any stupid coff reader which tries to read the
967 string table even when there isn't one won't croak. */
968 unsigned int size = 4;
971 bfd_h_put_32 (abfd, size, buffer);
972 if (bfd_write ((PTR) buffer, 1, 4, abfd) != 4)
976 /* Make sure the .debug section was created to be the correct size.
977 We should create it ourselves on the fly, but we don't because
978 BFD won't let us write to any section until we know how large all
979 the sections are. We could still do it by making another pass
980 over the symbols. FIXME. */
981 BFD_ASSERT (debug_string_size == 0
982 || (debug_string_section != (asection *) NULL
983 && (BFD_ALIGN (debug_string_size,
984 1 << debug_string_section->alignment_power)
985 == bfd_section_size (abfd, debug_string_section))));
991 coff_write_linenumbers (abfd)
995 bfd_size_type linesz;
998 linesz = bfd_coff_linesz (abfd);
999 buff = bfd_alloc (abfd, linesz);
1002 bfd_set_error (bfd_error_no_memory);
1005 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
1006 if (s->lineno_count) {
1007 asymbol **q = abfd->outsymbols;
1008 bfd_seek(abfd, s->line_filepos, SEEK_SET);
1009 /* Find all the linenumbers in this section */
1012 if (p->section->output_section == s) {
1014 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
1016 /* Found a linenumber entry, output */
1017 struct internal_lineno out;
1018 memset( (PTR)&out, 0, sizeof(out));
1020 out.l_addr.l_symndx = l->u.offset;
1021 bfd_coff_swap_lineno_out(abfd, &out, buff);
1022 bfd_write(buff, 1, linesz, abfd);
1024 while (l->line_number) {
1025 out.l_lnno = l->line_number;
1026 out.l_addr.l_symndx = l->u.offset;
1027 bfd_coff_swap_lineno_out(abfd, &out, buff);
1028 bfd_write(buff, 1, linesz, abfd);
1037 bfd_release (abfd, buff);
1043 coff_get_lineno (ignore_abfd, symbol)
1047 return coffsymbol(symbol)->lineno;
1051 coff_section_symbol (abfd, name)
1055 asection *sec = bfd_make_section_old_way (abfd, name);
1057 combined_entry_type *csym;
1060 csym = coff_symbol_from (abfd, sym)->native;
1061 /* Make sure back-end COFF stuff is there. */
1065 coff_symbol_type sym;
1066 /* @@FIXME This shouldn't use a fixed size!! */
1067 combined_entry_type e[10];
1070 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1073 bfd_set_error (bfd_error_no_error);
1076 memset ((char *) f, 0, sizeof (*f));
1077 coff_symbol_from (abfd, sym)->native = csym = f->e;
1079 csym[0].u.syment.n_sclass = C_STAT;
1080 csym[0].u.syment.n_numaux = 1;
1081 /* SF_SET_STATICS (sym); @@ ??? */
1082 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1083 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1084 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1086 if (sec->output_section == NULL)
1088 sec->output_section = sec;
1089 sec->output_offset = 0;
1095 /* This function transforms the offsets into the symbol table into
1096 pointers to syments. */
1099 coff_pointerize_aux (abfd, table_base, type, class, auxent)
1101 combined_entry_type *table_base;
1104 combined_entry_type *auxent;
1106 /* Don't bother if this is a file or a section */
1107 if (class == C_STAT && type == T_NULL) return;
1108 if (class == C_FILE) return;
1110 /* Otherwise patch up */
1111 #define N_TMASK coff_data (abfd)->local_n_tmask
1112 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1113 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
1114 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
1115 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1116 auxent->fix_end = 1;
1118 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1119 generate one, so we must be careful to ignore it. */
1120 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
1121 auxent->u.auxent.x_sym.x_tagndx.p =
1122 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1123 auxent->fix_tag = 1;
1128 build_string_table (abfd)
1131 char string_table_size_buffer[4];
1132 unsigned int string_table_size;
1135 /* At this point we should be "seek"'d to the end of the
1136 symbols === the symbol table size. */
1137 if (bfd_read((char *) string_table_size_buffer,
1138 sizeof(string_table_size_buffer),
1139 1, abfd) != sizeof(string_table_size))
1142 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
1144 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
1145 bfd_set_error (bfd_error_no_memory);
1147 } /* on mallocation error */
1148 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size)
1150 return string_table;
1153 /* Allocate space for the ".debug" section, and read it.
1154 We did not read the debug section until now, because
1155 we didn't want to go to the trouble until someone needed it. */
1158 build_debug_section (abfd)
1161 char *debug_section;
1164 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1167 bfd_set_error (bfd_error_no_debug_section);
1171 debug_section = (PTR) bfd_alloc (abfd,
1172 bfd_get_section_size_before_reloc (sect));
1173 if (debug_section == NULL) {
1174 bfd_set_error (bfd_error_no_memory);
1178 /* Seek to the beginning of the `.debug' section and read it.
1179 Save the current position first; it is needed by our caller.
1180 Then read debug section and reset the file pointer. */
1182 position = bfd_tell (abfd);
1183 bfd_seek (abfd, sect->filepos, SEEK_SET);
1184 if (bfd_read (debug_section,
1185 bfd_get_section_size_before_reloc (sect), 1, abfd)
1186 != bfd_get_section_size_before_reloc(sect))
1188 bfd_seek (abfd, position, SEEK_SET);
1189 return debug_section;
1193 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1194 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1195 be \0-terminated. */
1197 copy_name (abfd, name, maxlen)
1205 for (len = 0; len < maxlen; ++len) {
1206 if (name[len] == '\0') {
1211 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1212 bfd_set_error (bfd_error_no_memory);
1215 strncpy(newname, name, len);
1216 newname[len] = '\0';
1220 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1221 knit the symbol names into a normalized form. By normalized here I
1222 mean that all symbols have an n_offset pointer that points to a null-
1223 terminated string. */
1225 combined_entry_type *
1226 coff_get_normalized_symtab (abfd)
1229 combined_entry_type *internal;
1230 combined_entry_type *internal_ptr;
1231 combined_entry_type *symbol_ptr;
1232 combined_entry_type *internal_end;
1233 bfd_size_type symesz;
1237 char *string_table = NULL;
1238 char *debug_section = NULL;
1241 unsigned int raw_size;
1242 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1243 return obj_raw_syments(abfd);
1245 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1246 bfd_set_error (bfd_error_no_symbols);
1250 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1253 bfd_set_error (bfd_error_no_memory);
1256 internal_end = internal + bfd_get_symcount(abfd);
1258 symesz = bfd_coff_symesz (abfd);
1259 raw_size = bfd_get_symcount(abfd) * symesz;
1260 raw = bfd_alloc(abfd,raw_size);
1263 bfd_set_error (bfd_error_no_memory);
1267 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1268 || bfd_read(raw, raw_size, 1, abfd) != raw_size)
1270 /* mark the end of the symbols */
1271 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1273 FIXME SOMEDAY. A string table size of zero is very weird, but
1274 probably possible. If one shows up, it will probably kill us.
1277 /* Swap all the raw entries */
1278 for (raw_src = (char *) raw, internal_ptr = internal;
1280 raw_src += symesz, internal_ptr++) {
1283 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1284 internal_ptr->fix_value = 0;
1285 internal_ptr->fix_tag = 0;
1286 internal_ptr->fix_end = 0;
1287 internal_ptr->fix_scnlen = 0;
1288 symbol_ptr = internal_ptr;
1291 i < symbol_ptr->u.syment.n_numaux;
1297 internal_ptr->fix_value = 0;
1298 internal_ptr->fix_tag = 0;
1299 internal_ptr->fix_end = 0;
1300 internal_ptr->fix_scnlen = 0;
1301 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1302 symbol_ptr->u.syment.n_type,
1303 symbol_ptr->u.syment.n_sclass,
1304 i, symbol_ptr->u.syment.n_numaux,
1305 &(internal_ptr->u.auxent));
1306 /* Remember that bal entries arn't pointerized */
1307 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1310 coff_pointerize_aux(abfd,
1312 symbol_ptr->u.syment.n_type,
1313 symbol_ptr->u.syment.n_sclass,
1320 /* Free all the raw stuff */
1321 bfd_release(abfd, raw);
1323 for (internal_ptr = internal; internal_ptr < internal_end;
1326 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1327 /* make a file symbol point to the name in the auxent, since
1328 the text ".file" is redundant */
1329 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1330 /* the filename is a long one, point into the string table */
1331 if (string_table == NULL) {
1332 string_table = build_string_table(abfd);
1335 internal_ptr->u.syment._n._n_n._n_offset =
1336 (long) (string_table - 4 +
1337 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1340 /* ordinary short filename, put into memory anyway */
1341 internal_ptr->u.syment._n._n_n._n_offset = (long)
1342 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1347 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1348 /* This is a "short" name. Make it long. */
1349 unsigned long i = 0;
1350 char *newstring = NULL;
1352 /* find the length of this string without walking into memory
1354 for (i = 0; i < 8; ++i) {
1355 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1357 } /* if end of string */
1358 } /* possible lengths of this string. */
1360 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1361 bfd_set_error (bfd_error_no_memory);
1364 memset(newstring, 0, i);
1365 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1366 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1367 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1369 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1370 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1371 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1372 /* Long name already. Point symbol at the string in the table. */
1373 if (string_table == NULL) {
1374 string_table = build_string_table(abfd);
1376 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1377 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1380 /* Long name in debug section. Very similar. */
1381 if (debug_section == NULL) {
1382 debug_section = build_debug_section(abfd);
1384 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1385 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1388 internal_ptr += internal_ptr->u.syment.n_numaux;
1391 obj_raw_syments(abfd) = internal;
1392 obj_raw_syment_count(abfd) = internal_ptr - internal;
1395 } /* coff_get_normalized_symtab() */
1398 coff_get_reloc_upper_bound (abfd, asect)
1402 if (bfd_get_format(abfd) != bfd_object) {
1403 bfd_set_error (bfd_error_invalid_operation);
1406 return (asect->reloc_count + 1) * sizeof(arelent *);
1410 coff_make_empty_symbol (abfd)
1413 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1415 bfd_set_error (bfd_error_no_memory);
1418 memset (new, 0, sizeof *new);
1419 new->symbol.section = 0;
1421 new->lineno = (alent *) NULL;
1422 new->done_lineno = false;
1423 new->symbol.the_bfd = abfd;
1424 return &new->symbol;
1427 /* Make a debugging symbol. */
1430 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1435 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1437 bfd_set_error (bfd_error_no_memory);
1440 /* @@ This shouldn't be using a constant multiplier. */
1441 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1444 bfd_set_error (bfd_error_no_memory);
1447 new->symbol.section = &bfd_debug_section;
1448 new->lineno = (alent *) NULL;
1449 new->done_lineno = false;
1450 new->symbol.the_bfd = abfd;
1451 return &new->symbol;
1456 coff_get_symbol_info (abfd, symbol, ret)
1461 bfd_symbol_info (symbol, ret);
1464 /* Print out information about COFF symbol. */
1467 coff_print_symbol (abfd, filep, symbol, how)
1471 bfd_print_symbol_type how;
1473 FILE *file = (FILE *) filep;
1477 case bfd_print_symbol_name:
1478 fprintf (file, "%s", symbol->name);
1481 case bfd_print_symbol_more:
1482 fprintf (file, "coff %s %s",
1483 coffsymbol(symbol)->native ? "n" : "g",
1484 coffsymbol(symbol)->lineno ? "l" : " ");
1487 case bfd_print_symbol_all:
1488 if (coffsymbol(symbol)->native)
1491 combined_entry_type *combined = coffsymbol (symbol)->native;
1492 combined_entry_type *root = obj_raw_syments (abfd);
1493 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1495 fprintf (file,"[%3d]", combined - root);
1498 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
1499 combined->u.syment.n_scnum,
1500 combined->u.syment.n_flags,
1501 combined->u.syment.n_type,
1502 combined->u.syment.n_sclass,
1503 combined->u.syment.n_numaux,
1504 (unsigned long) combined->u.syment.n_value,
1507 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1509 combined_entry_type *auxp = combined + aux + 1;
1513 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1515 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1517 fprintf (file, "\n");
1518 switch (combined->u.syment.n_sclass)
1521 fprintf (file, "File ");
1525 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1526 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1527 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1535 fprintf (file, "\n%s :", l->u.sym->name);
1537 while (l->line_number)
1539 fprintf (file, "\n%4d : 0x%lx",
1542 (l->u.offset + symbol->section->vma)));
1549 bfd_print_symbol_vandf ((PTR) file, symbol);
1550 fprintf (file, " %-5s %s %s %s",
1551 symbol->section->name,
1552 coffsymbol(symbol)->native ? "n" : "g",
1553 coffsymbol(symbol)->lineno ? "l" : " ",
1559 /* Provided a BFD, a section and an offset into the section, calculate
1560 and return the name of the source file and the line nearest to the
1565 coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1566 functionname_ptr, line_ptr)
1569 asymbol **ignore_symbols;
1571 CONST char **filename_ptr;
1572 CONST char **functionname_ptr;
1573 unsigned int *line_ptr;
1575 static bfd *cache_abfd;
1576 static asection *cache_section;
1577 static bfd_vma cache_offset;
1578 static unsigned int cache_i;
1579 static CONST char *cache_function;
1580 static unsigned int line_base = 0;
1583 coff_data_type *cof = coff_data(abfd);
1584 /* Run through the raw syments if available */
1585 combined_entry_type *p;
1590 *functionname_ptr = 0;
1593 /* Don't try and find line numbers in a non coff file */
1594 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1600 p = cof->raw_syments;
1602 for (i = 0; i < cof->raw_syment_count; i++) {
1603 if (p->u.syment.n_sclass == C_FILE) {
1604 /* File name has been moved into symbol */
1605 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1608 p += 1 + p->u.syment.n_numaux;
1610 /* Now wander though the raw linenumbers of the section */
1612 If this is the same BFD as we were previously called with and this is
1613 the same section, and the offset we want is further down then we can
1614 prime the lookup loop
1616 if (abfd == cache_abfd &&
1617 section == cache_section &&
1618 offset >= cache_offset) {
1620 *functionname_ptr = cache_function;
1625 l = §ion->lineno[i];
1627 for (; i < section->lineno_count; i++) {
1628 if (l->line_number == 0) {
1629 /* Get the symbol this line number points at */
1630 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1631 if (coff->symbol.value > offset)
1633 *functionname_ptr = coff->symbol.name;
1635 combined_entry_type *s = coff->native;
1636 s = s + 1 + s->u.syment.n_numaux;
1638 S should now point to the .bf of the function
1640 if (s->u.syment.n_numaux) {
1642 The linenumber is stored in the auxent
1644 union internal_auxent *a = &((s + 1)->u.auxent);
1645 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1646 *line_ptr = line_base;
1651 if (l->u.offset > offset)
1653 *line_ptr = l->line_number + line_base - 1;
1659 cache_section = section;
1660 cache_offset = offset;
1662 cache_function = *functionname_ptr;
1668 coff_sizeof_headers (abfd, reloc)
1674 if (reloc == false) {
1675 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1678 size = bfd_coff_filhsz (abfd);
1681 size += abfd->section_count * bfd_coff_scnhsz (abfd);