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, name);
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_COFF_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
122 static const bfd_target *
123 coff_real_object_p (abfd, nscns, internal_f, internal_a)
126 struct internal_filehdr *internal_f;
127 struct internal_aouthdr *internal_a;
130 size_t readsize; /* length of file_info */
132 char *external_sections;
134 /* Build a play area */
135 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
139 scnhsz = bfd_coff_scnhsz (abfd);
140 readsize = nscns * scnhsz;
141 external_sections = (char *)bfd_alloc(abfd, readsize);
142 if (!external_sections)
144 bfd_set_error (bfd_error_no_memory);
148 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
152 /* Now copy data as required; construct all asections etc */
155 for (i = 0; i < nscns; i++) {
156 struct internal_scnhdr tmp;
157 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
159 make_a_section_from_file(abfd,&tmp, i+1);
163 /* make_abs_section(abfd);*/
165 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
168 if (!(internal_f->f_flags & F_RELFLG))
169 abfd->flags |= HAS_RELOC;
170 if ((internal_f->f_flags & F_EXEC))
171 abfd->flags |= EXEC_P;
172 if (!(internal_f->f_flags & F_LNNO))
173 abfd->flags |= HAS_LINENO;
174 if (!(internal_f->f_flags & F_LSYMS))
175 abfd->flags |= HAS_LOCALS;
177 /* FIXME: How can we set D_PAGED correctly? */
178 if ((internal_f->f_flags & F_EXEC) != 0)
179 abfd->flags |= D_PAGED;
181 bfd_get_symcount(abfd) = internal_f->f_nsyms;
182 if (internal_f->f_nsyms)
183 abfd->flags |= HAS_SYMS;
185 if (internal_a != (struct internal_aouthdr *) NULL)
186 bfd_get_start_address (abfd) = internal_a->entry;
188 bfd_get_start_address (abfd) = 0;
192 bfd_release(abfd, tdata);
193 return (const bfd_target *)NULL;
196 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
197 not a COFF file. This is also used by ECOFF. */
207 struct internal_filehdr internal_f;
208 struct internal_aouthdr internal_a;
210 /* figure out how much to read */
211 filhsz = bfd_coff_filhsz (abfd);
212 aoutsz = bfd_coff_aoutsz (abfd);
214 filehdr = bfd_alloc (abfd, filhsz);
217 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
219 if (bfd_get_error () != bfd_error_system_call)
220 bfd_set_error (bfd_error_wrong_format);
223 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
224 bfd_release (abfd, filehdr);
226 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
227 bfd_set_error (bfd_error_wrong_format);
230 nscns =internal_f.f_nscns;
232 if (internal_f.f_opthdr) {
235 opthdr = bfd_alloc (abfd, aoutsz);
238 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
241 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
244 /* Seek past the opt hdr stuff */
245 if (bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
249 return coff_real_object_p(abfd, nscns, &internal_f,
250 (internal_f.f_opthdr != 0
252 : (struct internal_aouthdr *) NULL));
255 /* Get the BFD section from a COFF symbol section number. */
258 coff_section_from_bfd_index (abfd, index)
262 struct sec *answer = abfd->sections;
266 return bfd_abs_section_ptr;
268 if (index == N_UNDEF)
270 return bfd_und_section_ptr;
274 return &bfd_debug_section;
279 if (answer->target_index == index)
281 answer = answer->next;
284 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
285 has a bad symbol table in biglitpow.o. */
286 return bfd_und_section_ptr;
289 /* Get the upper bound of a COFF symbol table. */
292 coff_get_symtab_upper_bound(abfd)
295 if (!bfd_coff_slurp_symbol_table(abfd))
298 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
302 /* Canonicalize a COFF symbol table. */
305 coff_get_symtab (abfd, alocation)
309 unsigned int counter = 0;
310 coff_symbol_type *symbase;
311 coff_symbol_type **location = (coff_symbol_type **) (alocation);
312 if (!bfd_coff_slurp_symbol_table(abfd))
315 symbase = obj_symbols(abfd);
316 while (counter < bfd_get_symcount(abfd))
318 /* This nasty code looks at the symbol to decide whether or
319 not it is descibes a constructor/destructor entry point. It
320 is structured this way to (hopefully) speed non matches */
322 if (0 && symbase->symbol.name[9] == '$')
324 bfd_constructor_entry(abfd,
325 (asymbol **)location,
326 symbase->symbol.name[10] == 'I' ?
330 *(location++) = symbase++;
334 return bfd_get_symcount(abfd);
337 /* Set lineno_count for the output sections of a COFF file. */
340 coff_count_linenumbers (abfd)
343 unsigned int limit = bfd_get_symcount(abfd);
348 asection *s = abfd->sections->output_section;
350 BFD_ASSERT(s->lineno_count == 0);
356 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
357 asymbol *q_maybe = *p;
358 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
359 coff_symbol_type *q = coffsymbol(q_maybe);
362 This symbol has a linenumber, increment the owning
363 section's linenumber count
365 alent *l = q->lineno;
366 q->symbol.section->output_section->lineno_count++;
369 while (l->line_number) {
371 q->symbol.section->output_section->lineno_count++;
380 /* Takes a bfd and a symbol, returns a pointer to the coff specific
381 area of the symbol if there is one. */
385 coff_symbol_from (ignore_abfd, symbol)
389 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
390 return (coff_symbol_type *)NULL;
392 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
393 return (coff_symbol_type *)NULL;
395 return (coff_symbol_type *) symbol;
399 fixup_symbol_value (coff_symbol_ptr, syment)
400 coff_symbol_type *coff_symbol_ptr;
401 struct internal_syment *syment;
404 /* Normalize the symbol flags */
405 if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
406 /* a common symbol is undefined with a value */
407 syment->n_scnum = N_UNDEF;
408 syment->n_value = coff_symbol_ptr->symbol.value;
410 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
411 syment->n_value = coff_symbol_ptr->symbol.value;
413 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) {
414 syment->n_scnum = N_UNDEF;
418 if (coff_symbol_ptr->symbol.section) {
420 coff_symbol_ptr->symbol.section->output_section->target_index;
423 coff_symbol_ptr->symbol.value +
424 coff_symbol_ptr->symbol.section->output_offset +
425 coff_symbol_ptr->symbol.section->output_section->vma;
429 /* This can happen, but I don't know why yet (steve@cygnus.com) */
430 syment->n_scnum = N_ABS;
431 syment->n_value = coff_symbol_ptr->symbol.value;
436 /* run through all the symbols in the symbol table and work out what
437 their indexes into the symbol table will be when output
439 Coff requires that each C_FILE symbol points to the next one in the
440 chain, and that the last one points to the first external symbol. We
445 coff_renumber_symbols (bfd_ptr)
448 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
449 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
450 unsigned int native_index = 0;
451 struct internal_syment *last_file = (struct internal_syment *)NULL;
452 unsigned int symbol_index;
454 /* COFF demands that undefined symbols come after all other symbols.
455 Since we don't need to impose this extra knowledge on all our client
456 programs, deal with that here. Sort the symbol table; just move the
457 undefined symbols to the end, leaving the rest alone. */
458 /* @@ Do we have some condition we could test for, so we don't always
459 have to do this? I don't think relocatability is quite right, but
460 I'm not certain. [raeburn:19920508.1711EST] */
465 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
467 * (symbol_count + 1));
470 bfd_set_error (bfd_error_no_memory);
473 bfd_ptr->outsymbols = newsyms;
474 for (i = 0; i < symbol_count; i++)
475 if (! bfd_is_und_section (symbol_ptr_ptr[i]->section))
476 *newsyms++ = symbol_ptr_ptr[i];
477 for (i = 0; i < symbol_count; i++)
478 if (bfd_is_und_section (symbol_ptr_ptr[i]->section))
479 *newsyms++ = symbol_ptr_ptr[i];
480 *newsyms = (asymbol *) NULL;
481 symbol_ptr_ptr = bfd_ptr->outsymbols;
484 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
486 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
487 if (coff_symbol_ptr && coff_symbol_ptr->native) {
488 combined_entry_type *s = coff_symbol_ptr->native;
491 if (s->u.syment.n_sclass == C_FILE)
493 if (last_file != (struct internal_syment *)NULL) {
494 last_file->n_value = native_index;
496 last_file = &(s->u.syment);
500 /* Modify the symbol values according to their section and
503 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
505 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
506 s[i].offset = native_index ++;
513 obj_conv_table_size (bfd_ptr) = native_index;
518 Run thorough the symbol table again, and fix it so that all pointers to
519 entries are changed to the entries' index in the output symbol table.
523 coff_mangle_symbols (bfd_ptr)
526 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
527 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
528 unsigned int symbol_index;
530 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
532 coff_symbol_type *coff_symbol_ptr =
533 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
535 if (coff_symbol_ptr && coff_symbol_ptr->native)
538 combined_entry_type *s = coff_symbol_ptr->native;
542 /* FIXME: We should use a union here. */
543 s->u.syment.n_value =
544 ((combined_entry_type *) s->u.syment.n_value)->offset;
547 for (i = 0; i < s->u.syment.n_numaux ; i++)
549 combined_entry_type *a = s + i + 1;
552 a->u.auxent.x_sym.x_tagndx.l =
553 a->u.auxent.x_sym.x_tagndx.p->offset;
558 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
559 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
564 a->u.auxent.x_csect.x_scnlen.l =
565 a->u.auxent.x_csect.x_scnlen.p->offset;
573 static bfd_size_type string_size;
574 static bfd_size_type debug_string_size;
575 static asection *debug_string_section;
578 coff_fix_symbol_name (abfd, symbol, native)
581 combined_entry_type *native;
583 unsigned int name_length;
584 union internal_auxent *auxent;
585 char * name = ( char *)(symbol->name);
587 if (name == (char *) NULL) {
588 /* coff symbols always have names, so we'll make one up */
589 symbol->name = "strange";
590 name = (char *)symbol->name;
592 name_length = strlen(name);
594 if (native->u.syment.n_sclass == C_FILE) {
595 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
596 auxent = &(native+1)->u.auxent;
598 if (bfd_coff_long_filenames (abfd)) {
599 if (name_length <= FILNMLEN) {
600 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
603 auxent->x_file.x_n.x_offset = string_size + 4;
604 auxent->x_file.x_n.x_zeroes = 0;
605 string_size += name_length + 1;
609 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
610 if (name_length > FILNMLEN) {
611 name[FILNMLEN] = '\0';
616 { /* NOT A C_FILE SYMBOL */
617 if (name_length <= SYMNMLEN)
619 /* This name will fit into the symbol neatly */
620 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
622 else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment))
624 native->u.syment._n._n_n._n_offset = string_size + 4;
625 native->u.syment._n._n_n._n_zeroes = 0;
626 string_size += name_length + 1;
633 /* This name should be written into the .debug section. For
634 some reason each name is preceded by a two byte length
635 and also followed by a null byte. FIXME: We assume that
636 the .debug section has already been created, and that it
638 if (debug_string_section == (asection *) NULL)
639 debug_string_section = bfd_get_section_by_name (abfd, ".debug");
640 filepos = bfd_tell (abfd);
641 bfd_put_16 (abfd, name_length + 1, buf);
642 if (! bfd_set_section_contents (abfd,
643 debug_string_section,
645 (file_ptr) debug_string_size,
647 || ! bfd_set_section_contents (abfd,
648 debug_string_section,
650 (file_ptr) debug_string_size + 2,
651 (bfd_size_type) name_length + 1))
653 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
655 native->u.syment._n._n_n._n_offset = debug_string_size + 2;
656 native->u.syment._n._n_n._n_zeroes = 0;
657 debug_string_size += name_length + 3;
662 /* We need to keep track of the symbol index so that when we write out
663 the relocs we can get the index for a symbol. This method is a
666 #define set_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
668 /* Write a symbol out to a COFF file. */
671 coff_write_symbol (abfd, symbol, native, written)
674 combined_entry_type *native;
675 unsigned int *written;
677 unsigned int numaux = native->u.syment.n_numaux;
678 int type = native->u.syment.n_type;
679 int class = native->u.syment.n_sclass;
681 bfd_size_type symesz;
683 /* @@ bfd_debug_section isn't accessible outside this file, but we
684 know that C_FILE symbols belong there. So move them. */
685 if (native->u.syment.n_sclass == C_FILE)
686 symbol->section = &bfd_debug_section;
688 if (bfd_is_abs_section (symbol->section))
690 native->u.syment.n_scnum = N_ABS;
692 else if (symbol->section == &bfd_debug_section)
694 native->u.syment.n_scnum = N_DEBUG;
696 else if (bfd_is_und_section (symbol->section))
698 native->u.syment.n_scnum = N_UNDEF;
702 native->u.syment.n_scnum =
703 symbol->section->output_section->target_index;
706 coff_fix_symbol_name (abfd, symbol, native);
708 symesz = bfd_coff_symesz (abfd);
709 buf = bfd_alloc (abfd, symesz);
712 bfd_set_error (bfd_error_no_memory);
715 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
716 if (bfd_write (buf, 1, symesz, abfd) != symesz)
718 bfd_release (abfd, buf);
720 if (native->u.syment.n_numaux > 0)
722 bfd_size_type auxesz;
725 auxesz = bfd_coff_auxesz (abfd);
726 buf = bfd_alloc (abfd, auxesz);
729 bfd_set_error (bfd_error_no_memory);
732 for (j = 0; j < native->u.syment.n_numaux; j++)
734 bfd_coff_swap_aux_out (abfd,
735 &((native + j + 1)->u.auxent),
739 native->u.syment.n_numaux,
741 if (bfd_write (buf, 1, auxesz, abfd)!= auxesz)
744 bfd_release (abfd, buf);
747 /* Store the index for use when we write out the relocs. */
748 set_index (symbol, *written);
750 *written += numaux + 1;
754 /* Write out a symbol to a COFF file that does not come from a COFF
755 file originally. This symbol may have been created by the linker,
756 or we may be linking a non COFF file to a COFF file. */
759 coff_write_alien_symbol (abfd, symbol, written)
762 unsigned int *written;
764 combined_entry_type *native;
765 combined_entry_type dummy;
768 native->u.syment.n_type = T_NULL;
769 native->u.syment.n_flags = 0;
770 if (bfd_is_und_section (symbol->section))
772 native->u.syment.n_scnum = N_UNDEF;
773 native->u.syment.n_value = symbol->value;
775 else if (bfd_is_com_section (symbol->section))
777 native->u.syment.n_scnum = N_UNDEF;
778 native->u.syment.n_value = symbol->value;
780 else if (symbol->flags & BSF_DEBUGGING)
782 /* Remove the symbol name so that it does not take up any space.
783 COFF won't know what to do with it anyhow. */
788 native->u.syment.n_scnum =
789 symbol->section->output_section->target_index;
790 native->u.syment.n_value = (symbol->value
791 + symbol->section->output_section->vma
792 + symbol->section->output_offset);
794 /* Copy the any flags from the the file header into the symbol.
797 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
798 if (c != (coff_symbol_type *) NULL)
799 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
803 native->u.syment.n_type = 0;
804 if (symbol->flags & BSF_LOCAL)
805 native->u.syment.n_sclass = C_STAT;
807 native->u.syment.n_sclass = C_EXT;
808 native->u.syment.n_numaux = 0;
810 return coff_write_symbol (abfd, symbol, native, written);
813 /* Write a native symbol to a COFF file. */
816 coff_write_native_symbol (abfd, symbol, written)
818 coff_symbol_type *symbol;
819 unsigned int *written;
821 combined_entry_type *native = symbol->native;
822 alent *lineno = symbol->lineno;
824 /* If this symbol has an associated line number, we must store the
825 symbol index in the line number field. We also tag the auxent to
826 point to the right place in the lineno table. */
827 if (lineno && !symbol->done_lineno)
829 unsigned int count = 0;
830 lineno[count].u.offset = *written;
831 if (native->u.syment.n_numaux)
833 union internal_auxent *a = &((native+1)->u.auxent);
835 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
836 symbol->symbol.section->output_section->moving_line_filepos;
839 /* Count and relocate all other linenumbers. */
841 while (lineno[count].line_number != 0)
845 I've been told this, but still need proof:
846 > The second bug is also in `bfd/coffcode.h'. This bug
847 > causes the linker to screw up the pc-relocations for
848 > all the line numbers in COFF code. This bug isn't only
849 > specific to A29K implementations, but affects all
850 > systems using COFF format binaries. Note that in COFF
851 > object files, the line number core offsets output by
852 > the assembler are relative to the start of each
853 > procedure, not to the start of the .text section. This
854 > patch relocates the line numbers relative to the
855 > `native->u.syment.n_value' instead of the section
857 > modular!olson@cs.arizona.edu (Jon Olson)
859 lineno[count].u.offset += native->u.syment.n_value;
861 lineno[count].u.offset +=
862 (symbol->symbol.section->output_section->vma
863 + symbol->symbol.section->output_offset);
867 symbol->done_lineno = true;
869 symbol->symbol.section->output_section->moving_line_filepos +=
870 count * bfd_coff_linesz (abfd);
873 return coff_write_symbol (abfd, &(symbol->symbol), native, written);
876 /* Write out the COFF symbols. */
879 coff_write_symbols (abfd)
883 unsigned int limit = bfd_get_symcount(abfd);
884 unsigned int written = 0;
888 debug_string_size = 0;
890 /* Seek to the right place */
891 if (bfd_seek (abfd, obj_sym_filepos(abfd), SEEK_SET) != 0)
894 /* Output all the symbols we have */
897 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
899 asymbol *symbol = *p;
900 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
902 if (c_symbol == (coff_symbol_type *) NULL
903 || c_symbol->native == (combined_entry_type *)NULL)
905 if (! coff_write_alien_symbol (abfd, symbol, &written))
910 if (! coff_write_native_symbol (abfd, c_symbol, &written))
915 bfd_get_symcount (abfd) = written;
917 /* Now write out strings */
919 if (string_size != 0)
921 unsigned int size = string_size + 4;
924 bfd_h_put_32 (abfd, size, buffer);
925 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
927 for (p = abfd->outsymbols, i = 0;
932 size_t name_length = strlen (q->name);
933 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
936 /* Figure out whether the symbol name should go in the string
937 table. Symbol names that are short enough are stored
938 directly in the syment structure. File names permit a
939 different, longer, length in the syment structure. On
940 XCOFF, some symbol names are stored in the .debug section
941 rather than in the string table. */
944 || c_symbol->native == NULL)
946 /* This is not a COFF symbol, so it certainly is not a
947 file name, nor does it go in the .debug section. */
950 else if (bfd_coff_symname_in_debug (abfd,
951 &c_symbol->native->u.syment))
953 /* This symbol name is in the XCOFF .debug section.
954 Don't write it into the string table. */
955 maxlen = name_length;
957 else if (c_symbol->native->u.syment.n_sclass == C_FILE)
962 if (name_length > maxlen)
964 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
972 /* We would normally not write anything here, but we'll write
973 out 4 so that any stupid coff reader which tries to read the
974 string table even when there isn't one won't croak. */
975 unsigned int size = 4;
978 bfd_h_put_32 (abfd, size, buffer);
979 if (bfd_write ((PTR) buffer, 1, 4, abfd) != 4)
983 /* Make sure the .debug section was created to be the correct size.
984 We should create it ourselves on the fly, but we don't because
985 BFD won't let us write to any section until we know how large all
986 the sections are. We could still do it by making another pass
987 over the symbols. FIXME. */
988 BFD_ASSERT (debug_string_size == 0
989 || (debug_string_section != (asection *) NULL
990 && (BFD_ALIGN (debug_string_size,
991 1 << debug_string_section->alignment_power)
992 == bfd_section_size (abfd, debug_string_section))));
998 coff_write_linenumbers (abfd)
1002 bfd_size_type linesz;
1005 linesz = bfd_coff_linesz (abfd);
1006 buff = bfd_alloc (abfd, linesz);
1009 bfd_set_error (bfd_error_no_memory);
1012 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
1013 if (s->lineno_count) {
1014 asymbol **q = abfd->outsymbols;
1015 if (bfd_seek(abfd, s->line_filepos, SEEK_SET) != 0)
1017 /* Find all the linenumbers in this section */
1020 if (p->section->output_section == s) {
1022 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
1024 /* Found a linenumber entry, output */
1025 struct internal_lineno out;
1026 memset( (PTR)&out, 0, sizeof(out));
1028 out.l_addr.l_symndx = l->u.offset;
1029 bfd_coff_swap_lineno_out(abfd, &out, buff);
1030 if (bfd_write(buff, 1, linesz, abfd) != linesz)
1033 while (l->line_number) {
1034 out.l_lnno = l->line_number;
1035 out.l_addr.l_symndx = l->u.offset;
1036 bfd_coff_swap_lineno_out(abfd, &out, buff);
1037 if (bfd_write(buff, 1, linesz, abfd) != linesz)
1047 bfd_release (abfd, buff);
1053 coff_get_lineno (ignore_abfd, symbol)
1057 return coffsymbol(symbol)->lineno;
1061 coff_section_symbol (abfd, name)
1065 asection *sec = bfd_make_section_old_way (abfd, name);
1067 combined_entry_type *csym;
1070 csym = coff_symbol_from (abfd, sym)->native;
1071 /* Make sure back-end COFF stuff is there. */
1075 coff_symbol_type sym;
1076 /* @@FIXME This shouldn't use a fixed size!! */
1077 combined_entry_type e[10];
1080 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1083 bfd_set_error (bfd_error_no_error);
1086 memset ((char *) f, 0, sizeof (*f));
1087 coff_symbol_from (abfd, sym)->native = csym = f->e;
1089 csym[0].u.syment.n_sclass = C_STAT;
1090 csym[0].u.syment.n_numaux = 1;
1091 /* SF_SET_STATICS (sym); @@ ??? */
1092 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1093 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1094 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1096 if (sec->output_section == NULL)
1098 sec->output_section = sec;
1099 sec->output_offset = 0;
1105 /* This function transforms the offsets into the symbol table into
1106 pointers to syments. */
1109 coff_pointerize_aux (abfd, table_base, type, class, auxent)
1111 combined_entry_type *table_base;
1114 combined_entry_type *auxent;
1116 /* Don't bother if this is a file or a section */
1117 if (class == C_STAT && type == T_NULL) return;
1118 if (class == C_FILE) return;
1120 /* Otherwise patch up */
1121 #define N_TMASK coff_data (abfd)->local_n_tmask
1122 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1123 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
1124 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
1125 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1126 auxent->fix_end = 1;
1128 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1129 generate one, so we must be careful to ignore it. */
1130 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
1131 auxent->u.auxent.x_sym.x_tagndx.p =
1132 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1133 auxent->fix_tag = 1;
1138 build_string_table (abfd)
1141 char string_table_size_buffer[4];
1142 unsigned int string_table_size;
1145 /* At this point we should be "seek"'d to the end of the
1146 symbols === the symbol table size. */
1147 if (bfd_read((char *) string_table_size_buffer,
1148 sizeof(string_table_size_buffer),
1149 1, abfd) != sizeof(string_table_size))
1152 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
1154 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
1155 bfd_set_error (bfd_error_no_memory);
1157 } /* on mallocation error */
1158 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size)
1160 return string_table;
1163 /* Allocate space for the ".debug" section, and read it.
1164 We did not read the debug section until now, because
1165 we didn't want to go to the trouble until someone needed it. */
1168 build_debug_section (abfd)
1171 char *debug_section;
1174 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1177 bfd_set_error (bfd_error_no_debug_section);
1181 debug_section = (PTR) bfd_alloc (abfd,
1182 bfd_get_section_size_before_reloc (sect));
1183 if (debug_section == NULL) {
1184 bfd_set_error (bfd_error_no_memory);
1188 /* Seek to the beginning of the `.debug' section and read it.
1189 Save the current position first; it is needed by our caller.
1190 Then read debug section and reset the file pointer. */
1192 position = bfd_tell (abfd);
1193 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1194 || (bfd_read (debug_section,
1195 bfd_get_section_size_before_reloc (sect), 1, abfd)
1196 != bfd_get_section_size_before_reloc(sect))
1197 || bfd_seek (abfd, position, SEEK_SET) != 0)
1199 return debug_section;
1203 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1204 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1205 be \0-terminated. */
1207 copy_name (abfd, name, maxlen)
1215 for (len = 0; len < maxlen; ++len) {
1216 if (name[len] == '\0') {
1221 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1222 bfd_set_error (bfd_error_no_memory);
1225 strncpy(newname, name, len);
1226 newname[len] = '\0';
1230 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1231 knit the symbol names into a normalized form. By normalized here I
1232 mean that all symbols have an n_offset pointer that points to a null-
1233 terminated string. */
1235 combined_entry_type *
1236 coff_get_normalized_symtab (abfd)
1239 combined_entry_type *internal;
1240 combined_entry_type *internal_ptr;
1241 combined_entry_type *symbol_ptr;
1242 combined_entry_type *internal_end;
1243 bfd_size_type symesz;
1247 char *string_table = NULL;
1248 char *debug_section = NULL;
1251 unsigned int raw_size;
1252 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1253 return obj_raw_syments(abfd);
1255 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1256 bfd_set_error (bfd_error_no_symbols);
1260 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1263 bfd_set_error (bfd_error_no_memory);
1266 internal_end = internal + bfd_get_symcount(abfd);
1268 symesz = bfd_coff_symesz (abfd);
1269 raw_size = bfd_get_symcount(abfd) * symesz;
1270 raw = bfd_alloc(abfd,raw_size);
1273 bfd_set_error (bfd_error_no_memory);
1277 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1278 || bfd_read(raw, raw_size, 1, abfd) != raw_size)
1280 /* mark the end of the symbols */
1281 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1283 FIXME SOMEDAY. A string table size of zero is very weird, but
1284 probably possible. If one shows up, it will probably kill us.
1287 /* Swap all the raw entries */
1288 for (raw_src = (char *) raw, internal_ptr = internal;
1290 raw_src += symesz, internal_ptr++) {
1293 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1294 internal_ptr->fix_value = 0;
1295 internal_ptr->fix_tag = 0;
1296 internal_ptr->fix_end = 0;
1297 internal_ptr->fix_scnlen = 0;
1298 symbol_ptr = internal_ptr;
1301 i < symbol_ptr->u.syment.n_numaux;
1307 internal_ptr->fix_value = 0;
1308 internal_ptr->fix_tag = 0;
1309 internal_ptr->fix_end = 0;
1310 internal_ptr->fix_scnlen = 0;
1311 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1312 symbol_ptr->u.syment.n_type,
1313 symbol_ptr->u.syment.n_sclass,
1314 i, symbol_ptr->u.syment.n_numaux,
1315 &(internal_ptr->u.auxent));
1316 /* Remember that bal entries arn't pointerized */
1317 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1320 coff_pointerize_aux(abfd,
1322 symbol_ptr->u.syment.n_type,
1323 symbol_ptr->u.syment.n_sclass,
1330 /* Free all the raw stuff */
1331 bfd_release(abfd, raw);
1333 for (internal_ptr = internal; internal_ptr < internal_end;
1336 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1337 /* make a file symbol point to the name in the auxent, since
1338 the text ".file" is redundant */
1339 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1340 /* the filename is a long one, point into the string table */
1341 if (string_table == NULL) {
1342 string_table = build_string_table(abfd);
1345 internal_ptr->u.syment._n._n_n._n_offset =
1346 (long) (string_table - 4 +
1347 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1350 /* ordinary short filename, put into memory anyway */
1351 internal_ptr->u.syment._n._n_n._n_offset = (long)
1352 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1357 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1358 /* This is a "short" name. Make it long. */
1359 unsigned long i = 0;
1360 char *newstring = NULL;
1362 /* find the length of this string without walking into memory
1364 for (i = 0; i < 8; ++i) {
1365 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1367 } /* if end of string */
1368 } /* possible lengths of this string. */
1370 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1371 bfd_set_error (bfd_error_no_memory);
1374 memset(newstring, 0, i);
1375 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1376 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1377 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1379 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1380 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1381 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1382 /* Long name already. Point symbol at the string in the table. */
1383 if (string_table == NULL) {
1384 string_table = build_string_table(abfd);
1386 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1387 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1390 /* Long name in debug section. Very similar. */
1391 if (debug_section == NULL) {
1392 debug_section = build_debug_section(abfd);
1394 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1395 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1398 internal_ptr += internal_ptr->u.syment.n_numaux;
1401 obj_raw_syments(abfd) = internal;
1402 obj_raw_syment_count(abfd) = internal_ptr - internal;
1405 } /* coff_get_normalized_symtab() */
1408 coff_get_reloc_upper_bound (abfd, asect)
1412 if (bfd_get_format(abfd) != bfd_object) {
1413 bfd_set_error (bfd_error_invalid_operation);
1416 return (asect->reloc_count + 1) * sizeof(arelent *);
1420 coff_make_empty_symbol (abfd)
1423 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1425 bfd_set_error (bfd_error_no_memory);
1428 memset (new, 0, sizeof *new);
1429 new->symbol.section = 0;
1431 new->lineno = (alent *) NULL;
1432 new->done_lineno = false;
1433 new->symbol.the_bfd = abfd;
1434 return &new->symbol;
1437 /* Make a debugging symbol. */
1440 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1445 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1447 bfd_set_error (bfd_error_no_memory);
1450 /* @@ This shouldn't be using a constant multiplier. */
1451 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1454 bfd_set_error (bfd_error_no_memory);
1457 new->symbol.section = &bfd_debug_section;
1458 new->lineno = (alent *) NULL;
1459 new->done_lineno = false;
1460 new->symbol.the_bfd = abfd;
1461 return &new->symbol;
1466 coff_get_symbol_info (abfd, symbol, ret)
1471 bfd_symbol_info (symbol, ret);
1474 /* Print out information about COFF symbol. */
1477 coff_print_symbol (abfd, filep, symbol, how)
1481 bfd_print_symbol_type how;
1483 FILE *file = (FILE *) filep;
1487 case bfd_print_symbol_name:
1488 fprintf (file, "%s", symbol->name);
1491 case bfd_print_symbol_more:
1492 fprintf (file, "coff %s %s",
1493 coffsymbol(symbol)->native ? "n" : "g",
1494 coffsymbol(symbol)->lineno ? "l" : " ");
1497 case bfd_print_symbol_all:
1498 if (coffsymbol(symbol)->native)
1501 combined_entry_type *combined = coffsymbol (symbol)->native;
1502 combined_entry_type *root = obj_raw_syments (abfd);
1503 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1505 fprintf (file,"[%3d]", combined - root);
1508 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
1509 combined->u.syment.n_scnum,
1510 combined->u.syment.n_flags,
1511 combined->u.syment.n_type,
1512 combined->u.syment.n_sclass,
1513 combined->u.syment.n_numaux,
1514 (unsigned long) combined->u.syment.n_value,
1517 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1519 combined_entry_type *auxp = combined + aux + 1;
1523 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1525 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1527 fprintf (file, "\n");
1528 switch (combined->u.syment.n_sclass)
1531 fprintf (file, "File ");
1535 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1536 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1537 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1545 fprintf (file, "\n%s :", l->u.sym->name);
1547 while (l->line_number)
1549 fprintf (file, "\n%4d : 0x%lx",
1552 (l->u.offset + symbol->section->vma)));
1559 bfd_print_symbol_vandf ((PTR) file, symbol);
1560 fprintf (file, " %-5s %s %s %s",
1561 symbol->section->name,
1562 coffsymbol(symbol)->native ? "n" : "g",
1563 coffsymbol(symbol)->lineno ? "l" : " ",
1569 /* Provided a BFD, a section and an offset into the section, calculate
1570 and return the name of the source file and the line nearest to the
1575 coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1576 functionname_ptr, line_ptr)
1579 asymbol **ignore_symbols;
1581 CONST char **filename_ptr;
1582 CONST char **functionname_ptr;
1583 unsigned int *line_ptr;
1585 static bfd *cache_abfd;
1586 static asection *cache_section;
1587 static bfd_vma cache_offset;
1588 static unsigned int cache_i;
1589 static CONST char *cache_function;
1590 static unsigned int line_base = 0;
1593 coff_data_type *cof = coff_data(abfd);
1594 /* Run through the raw syments if available */
1595 combined_entry_type *p;
1600 *functionname_ptr = 0;
1603 /* Don't try and find line numbers in a non coff file */
1604 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1610 p = cof->raw_syments;
1612 for (i = 0; i < cof->raw_syment_count; i++) {
1613 if (p->u.syment.n_sclass == C_FILE) {
1614 /* File name has been moved into symbol */
1615 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1618 p += 1 + p->u.syment.n_numaux;
1620 /* Now wander though the raw linenumbers of the section */
1622 If this is the same BFD as we were previously called with and this is
1623 the same section, and the offset we want is further down then we can
1624 prime the lookup loop
1626 if (abfd == cache_abfd &&
1627 section == cache_section &&
1628 offset >= cache_offset) {
1630 *functionname_ptr = cache_function;
1635 l = §ion->lineno[i];
1637 for (; i < section->lineno_count; i++) {
1638 if (l->line_number == 0) {
1639 /* Get the symbol this line number points at */
1640 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1641 if (coff->symbol.value > offset)
1643 *functionname_ptr = coff->symbol.name;
1645 combined_entry_type *s = coff->native;
1646 s = s + 1 + s->u.syment.n_numaux;
1648 S should now point to the .bf of the function
1650 if (s->u.syment.n_numaux) {
1652 The linenumber is stored in the auxent
1654 union internal_auxent *a = &((s + 1)->u.auxent);
1655 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1656 *line_ptr = line_base;
1661 if (l->u.offset > offset)
1663 *line_ptr = l->line_number + line_base - 1;
1669 cache_section = section;
1670 cache_offset = offset;
1672 cache_function = *functionname_ptr;
1678 coff_sizeof_headers (abfd, reloc)
1684 if (reloc == false) {
1685 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1688 size = bfd_coff_filhsz (abfd);
1691 size += abfd->section_count * bfd_coff_scnhsz (abfd);