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. */
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 asection bfd_debug_section = { "*DEBUG*" };
46 /* Take a section header read from a coff file (in HOST byte order),
47 and make a BFD "section" out of it. This is used by ECOFF. */
49 DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
51 struct internal_scnhdr *hdr AND
52 unsigned int target_index)
54 asection *return_section;
57 /* Assorted wastage to null-terminate the name, thanks AT&T! */
58 name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
60 bfd_set_error (bfd_error_no_memory);
63 strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
64 name[sizeof (hdr->s_name)] = 0;
66 return_section = bfd_make_section(abfd, name);
67 if (return_section == NULL)
68 return_section = bfd_coff_make_section_hook (abfd, name);
70 /* Handle several sections of the same name. For example, if an executable
71 has two .bss sections, GDB better be able to find both of them
73 if (return_section == NULL)
74 return_section = bfd_make_section_anyway (abfd, name);
76 if (return_section == NULL)
79 /* s_paddr is presumed to be = to s_vaddr */
81 return_section->vma = hdr->s_vaddr;
82 return_section->_raw_size = hdr->s_size;
83 return_section->filepos = hdr->s_scnptr;
84 return_section->rel_filepos = hdr->s_relptr;
85 return_section->reloc_count = hdr->s_nreloc;
87 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
89 return_section->line_filepos = hdr->s_lnnoptr;
91 return_section->lineno_count = hdr->s_nlnno;
92 return_section->userdata = NULL;
93 return_section->next = (asection *) NULL;
94 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
96 return_section->target_index = target_index;
98 /* At least on i386-coff, the line number count for a shared library
99 section must be ignored. */
100 if ((return_section->flags & SEC_SHARED_LIBRARY) != 0)
101 return_section->lineno_count = 0;
103 if (hdr->s_nreloc != 0)
104 return_section->flags |= SEC_RELOC;
105 /* FIXME: should this check 'hdr->s_size > 0' */
106 if (hdr->s_scnptr != 0)
107 return_section->flags |= SEC_HAS_CONTENTS;
111 /* Read in a COFF object and make it into a BFD. This is used by
116 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
119 struct internal_filehdr *internal_f AND
120 struct internal_aouthdr *internal_a)
123 size_t readsize; /* length of file_info */
125 char *external_sections;
127 /* Build a play area */
128 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
132 scnhsz = bfd_coff_scnhsz (abfd);
133 readsize = nscns * scnhsz;
134 external_sections = (char *)bfd_alloc(abfd, readsize);
135 if (!external_sections)
137 bfd_set_error (bfd_error_no_memory);
141 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
145 /* Now copy data as required; construct all asections etc */
148 for (i = 0; i < nscns; i++) {
149 struct internal_scnhdr tmp;
150 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
152 make_a_section_from_file(abfd,&tmp, i+1);
156 /* make_abs_section(abfd);*/
158 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
161 if (!(internal_f->f_flags & F_RELFLG))
162 abfd->flags |= HAS_RELOC;
163 if ((internal_f->f_flags & F_EXEC))
164 abfd->flags |= EXEC_P;
165 if (!(internal_f->f_flags & F_LNNO))
166 abfd->flags |= HAS_LINENO;
167 if (!(internal_f->f_flags & F_LSYMS))
168 abfd->flags |= HAS_LOCALS;
171 bfd_get_symcount(abfd) = internal_f->f_nsyms;
172 if (internal_f->f_nsyms)
173 abfd->flags |= HAS_SYMS;
175 if (internal_a != (struct internal_aouthdr *) NULL)
176 bfd_get_start_address (abfd) = internal_a->entry;
178 bfd_get_start_address (abfd) = 0;
182 bfd_release(abfd, tdata);
183 return (bfd_target *)NULL;
186 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
187 not a COFF file. This is also used by ECOFF. */
190 DEFUN(coff_object_p,(abfd),
197 struct internal_filehdr internal_f;
198 struct internal_aouthdr internal_a;
200 bfd_set_error (bfd_error_system_call);
202 /* figure out how much to read */
203 filhsz = bfd_coff_filhsz (abfd);
204 aoutsz = bfd_coff_aoutsz (abfd);
206 filehdr = bfd_alloc (abfd, filhsz);
209 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
211 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
212 bfd_release (abfd, filehdr);
214 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
215 bfd_set_error (bfd_error_wrong_format);
218 nscns =internal_f.f_nscns;
220 if (internal_f.f_opthdr) {
223 opthdr = bfd_alloc (abfd, aoutsz);
226 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
229 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
232 /* Seek past the opt hdr stuff */
233 bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
235 return coff_real_object_p(abfd, nscns, &internal_f,
236 (internal_f.f_opthdr != 0
238 : (struct internal_aouthdr *) NULL));
241 /* Get the BFD section from a COFF symbol section number. */
244 DEFUN(coff_section_from_bfd_index,(abfd, index),
248 struct sec *answer = abfd->sections;
252 return &bfd_abs_section;
254 if (index == N_UNDEF)
256 return &bfd_und_section;
260 return &bfd_debug_section;
265 if (answer->target_index == index)
267 answer = answer->next;
270 return &bfd_und_section; /* For gcc -W and lint. Never executed. */
273 /* Get the upper bound of a COFF symbol table. */
276 coff_get_symtab_upper_bound(abfd)
279 if (!bfd_coff_slurp_symbol_table(abfd))
282 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
286 /* Canonicalize a COFF symbol table. */
289 DEFUN(coff_get_symtab, (abfd, alocation),
293 unsigned int counter = 0;
294 coff_symbol_type *symbase;
295 coff_symbol_type **location = (coff_symbol_type **) (alocation);
296 if (!bfd_coff_slurp_symbol_table(abfd))
299 symbase = obj_symbols(abfd);
300 while (counter < bfd_get_symcount(abfd))
302 /* This nasty code looks at the symbol to decide whether or
303 not it is descibes a constructor/destructor entry point. It
304 is structured this way to (hopefully) speed non matches */
306 if (0 && symbase->symbol.name[9] == '$')
308 bfd_constructor_entry(abfd,
309 (asymbol **)location,
310 symbase->symbol.name[10] == 'I' ?
314 *(location++) = symbase++;
318 return bfd_get_symcount(abfd);
321 /* Set lineno_count for the output sections of a COFF file. */
324 DEFUN(coff_count_linenumbers,(abfd),
327 unsigned int limit = bfd_get_symcount(abfd);
332 asection *s = abfd->sections->output_section;
334 BFD_ASSERT(s->lineno_count == 0);
340 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
341 asymbol *q_maybe = *p;
342 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
343 coff_symbol_type *q = coffsymbol(q_maybe);
346 This symbol has a linenumber, increment the owning
347 section's linenumber count
349 alent *l = q->lineno;
350 q->symbol.section->output_section->lineno_count++;
353 while (l->line_number) {
355 q->symbol.section->output_section->lineno_count++;
364 /* Takes a bfd and a symbol, returns a pointer to the coff specific
365 area of the symbol if there is one. */
369 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
373 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
374 return (coff_symbol_type *)NULL;
376 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
377 return (coff_symbol_type *)NULL;
379 return (coff_symbol_type *) symbol;
383 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
384 coff_symbol_type *coff_symbol_ptr AND
385 struct internal_syment *syment)
388 /* Normalize the symbol flags */
389 if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
390 /* a common symbol is undefined with a value */
391 syment->n_scnum = N_UNDEF;
392 syment->n_value = coff_symbol_ptr->symbol.value;
394 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
395 syment->n_value = coff_symbol_ptr->symbol.value;
397 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
398 syment->n_scnum = N_UNDEF;
402 if (coff_symbol_ptr->symbol.section) {
404 coff_symbol_ptr->symbol.section->output_section->target_index;
407 coff_symbol_ptr->symbol.value +
408 coff_symbol_ptr->symbol.section->output_offset +
409 coff_symbol_ptr->symbol.section->output_section->vma;
414 syment->n_scnum = N_ABS;
415 syment->n_value = coff_symbol_ptr->symbol.value;
420 /* run through all the symbols in the symbol table and work out what
421 their indexes into the symbol table will be when output
423 Coff requires that each C_FILE symbol points to the next one in the
424 chain, and that the last one points to the first external symbol. We
429 DEFUN(coff_renumber_symbols,(bfd_ptr),
432 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
433 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
434 unsigned int native_index = 0;
435 struct internal_syment *last_file = (struct internal_syment *)NULL;
436 unsigned int symbol_index;
438 /* COFF demands that undefined symbols come after all other symbols.
439 Since we don't need to impose this extra knowledge on all our client
440 programs, deal with that here. Sort the symbol table; just move the
441 undefined symbols to the end, leaving the rest alone. */
442 /* @@ Do we have some condition we could test for, so we don't always
443 have to do this? I don't think relocatability is quite right, but
444 I'm not certain. [raeburn:19920508.1711EST] */
449 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
451 * (symbol_count + 1));
454 bfd_set_error (bfd_error_no_memory);
457 bfd_ptr->outsymbols = newsyms;
458 for (i = 0; i < symbol_count; i++)
459 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
460 *newsyms++ = symbol_ptr_ptr[i];
461 for (i = 0; i < symbol_count; i++)
462 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
463 *newsyms++ = symbol_ptr_ptr[i];
464 *newsyms = (asymbol *) NULL;
465 symbol_ptr_ptr = bfd_ptr->outsymbols;
468 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
470 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
471 if (coff_symbol_ptr && coff_symbol_ptr->native) {
472 combined_entry_type *s = coff_symbol_ptr->native;
475 if (s->u.syment.n_sclass == C_FILE)
477 if (last_file != (struct internal_syment *)NULL) {
478 last_file->n_value = native_index;
480 last_file = &(s->u.syment);
484 /* Modify the symbol values according to their section and
487 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
489 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
490 s[i].offset = native_index ++;
497 obj_conv_table_size (bfd_ptr) = native_index;
502 Run thorough the symbol table again, and fix it so that all pointers to
503 entries are changed to the entries' index in the output symbol table.
507 coff_mangle_symbols (bfd_ptr)
510 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
511 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
512 unsigned int symbol_index;
514 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
516 coff_symbol_type *coff_symbol_ptr =
517 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
519 if (coff_symbol_ptr && coff_symbol_ptr->native)
522 combined_entry_type *s = coff_symbol_ptr->native;
526 /* FIXME: We should use a union here. */
527 s->u.syment.n_value =
528 ((combined_entry_type *) s->u.syment.n_value)->offset;
531 for (i = 0; i < s->u.syment.n_numaux ; i++)
533 combined_entry_type *a = s + i + 1;
536 a->u.auxent.x_sym.x_tagndx.l =
537 a->u.auxent.x_sym.x_tagndx.p->offset;
542 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
543 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
548 a->u.auxent.x_csect.x_scnlen.l =
549 a->u.auxent.x_csect.x_scnlen.p->offset;
557 static bfd_size_type string_size;
558 static bfd_size_type debug_string_size;
559 static asection *debug_string_section;
562 DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
565 combined_entry_type *native)
567 unsigned int name_length;
568 union internal_auxent *auxent;
569 char * name = ( char *)(symbol->name);
571 if (name == (char *) NULL) {
572 /* coff symbols always have names, so we'll make one up */
573 symbol->name = "strange";
574 name = (char *)symbol->name;
576 name_length = strlen(name);
578 if (native->u.syment.n_sclass == C_FILE) {
579 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
580 auxent = &(native+1)->u.auxent;
582 if (bfd_coff_long_filenames (abfd)) {
583 if (name_length <= FILNMLEN) {
584 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
587 auxent->x_file.x_n.x_offset = string_size + 4;
588 auxent->x_file.x_n.x_zeroes = 0;
589 string_size += name_length + 1;
593 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
594 if (name_length > FILNMLEN) {
595 name[FILNMLEN] = '\0';
600 { /* NOT A C_FILE SYMBOL */
601 if (name_length <= SYMNMLEN)
603 /* This name will fit into the symbol neatly */
604 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
606 else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment))
608 native->u.syment._n._n_n._n_offset = string_size + 4;
609 native->u.syment._n._n_n._n_zeroes = 0;
610 string_size += name_length + 1;
617 /* This name should be written into the .debug section. For
618 some reason each name is preceded by a two byte length
619 and also followed by a null byte. FIXME: We assume that
620 the .debug section has already been created, and that it
622 if (debug_string_section == (asection *) NULL)
623 debug_string_section = bfd_get_section_by_name (abfd, ".debug");
624 filepos = bfd_tell (abfd);
625 bfd_put_16 (abfd, name_length + 1, buf);
626 if (! bfd_set_section_contents (abfd,
627 debug_string_section,
629 (file_ptr) debug_string_size,
631 || ! bfd_set_section_contents (abfd,
632 debug_string_section,
634 (file_ptr) debug_string_size + 2,
635 (bfd_size_type) name_length + 1))
637 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
639 native->u.syment._n._n_n._n_offset = debug_string_size + 2;
640 native->u.syment._n._n_n._n_zeroes = 0;
641 debug_string_size += name_length + 3;
646 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
649 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
652 combined_entry_type *native AND
653 unsigned int written)
655 unsigned int numaux = native->u.syment.n_numaux;
656 int type = native->u.syment.n_type;
657 int class = native->u.syment.n_sclass;
659 bfd_size_type symesz;
661 /* @@ bfd_debug_section isn't accessible outside this file, but we know
662 that C_FILE symbols belong there. So move them. */
663 if (native->u.syment.n_sclass == C_FILE)
664 symbol->section = &bfd_debug_section;
666 if (symbol->section == &bfd_abs_section)
668 native->u.syment.n_scnum = N_ABS;
670 else if (symbol->section == &bfd_debug_section)
672 native->u.syment.n_scnum = N_DEBUG;
674 else if (symbol->section == &bfd_und_section)
676 native->u.syment.n_scnum = N_UNDEF;
680 native->u.syment.n_scnum =
681 symbol->section->output_section->target_index;
685 coff_fix_symbol_name(abfd, symbol, native);
687 symesz = bfd_coff_symesz (abfd);
688 buf = bfd_alloc (abfd, symesz);
691 bfd_set_error (bfd_error_no_memory);
694 bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
695 bfd_write(buf, 1, symesz, abfd);
696 bfd_release (abfd, buf);
698 if (native->u.syment.n_numaux > 0)
700 bfd_size_type auxesz;
703 auxesz = bfd_coff_auxesz (abfd);
704 buf = bfd_alloc (abfd, auxesz);
707 bfd_set_error (bfd_error_no_memory);
710 for (j = 0; j < native->u.syment.n_numaux; j++)
712 bfd_coff_swap_aux_out(abfd,
713 &((native + j + 1)->u.auxent),
717 native->u.syment.n_numaux,
719 bfd_write(buf, 1, auxesz, abfd);
721 bfd_release (abfd, buf);
724 Reuse somewhere in the symbol to keep the index
726 set_index(symbol, written);
727 return written + 1 + numaux;
732 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
735 unsigned int written)
738 This symbol has been created by the loader, or come from a non
739 coff format. It has no native element to inherit, make our
742 combined_entry_type *native;
743 combined_entry_type dummy;
745 native->u.syment.n_type = T_NULL;
746 native->u.syment.n_flags = 0;
747 if (symbol->section == &bfd_und_section)
749 native->u.syment.n_scnum = N_UNDEF;
750 native->u.syment.n_value = symbol->value;
752 else if (bfd_is_com_section (symbol->section))
754 native->u.syment.n_scnum = N_UNDEF;
755 native->u.syment.n_value = symbol->value;
759 else if (symbol->flags & BSF_DEBUGGING) {
761 remove name so it doesn't take up any space
766 native->u.syment.n_scnum = symbol->section->output_section->target_index;
767 native->u.syment.n_value = symbol->value +
768 symbol->section->output_section->vma +
769 symbol->section->output_offset;
770 /* Copy the any flags from the the file hdr into the symbol */
772 coff_symbol_type *c = coff_symbol_from(abfd, symbol);
773 if (c != (coff_symbol_type *)NULL) {
774 native->u.syment.n_flags = bfd_asymbol_bfd(&c->symbol)->flags;
779 native->u.syment.n_type = 0;
780 if (symbol->flags & BSF_LOCAL)
781 native->u.syment.n_sclass = C_STAT;
783 native->u.syment.n_sclass = C_EXT;
784 native->u.syment.n_numaux = 0;
786 return coff_write_symbol(abfd, symbol, native, written);
790 DEFUN(coff_write_native_symbol,(abfd, symbol, written),
792 coff_symbol_type *symbol AND
793 unsigned int written)
796 Does this symbol have an ascociated line number - if so then
797 make it remember this symbol index. Also tag the auxent of
798 this symbol to point to the right place in the lineno table
800 combined_entry_type *native = symbol->native;
802 alent *lineno = symbol->lineno;
804 if (lineno && !symbol->done_lineno) {
805 unsigned int count = 0;
806 lineno[count].u.offset = written;
807 if (native->u.syment.n_numaux) {
808 union internal_auxent *a = &((native+1)->u.auxent);
810 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
811 symbol->symbol.section->output_section->moving_line_filepos;
814 And count and relocate all other linenumbers
818 while (lineno[count].line_number) {
821 I've been told this, but still need proof:
822 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
823 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
824 > only specific to A29K implementations, but affects all systems using COFF
825 > format binaries. Note that in COFF object files, the line number core offsets
826 > output by the assembler are relative to the start of each procedure, not
827 > to the start of the .text section. This patch relocates the line numbers
828 > relative to the `native->u.syment.n_value' instead of the section virtual
831 lineno[count].u.offset += native->u.syment.n_value;
834 lineno[count].u.offset +=
835 symbol->symbol.section->output_section->vma +
836 symbol->symbol.section->output_offset;
840 symbol->done_lineno = true;
842 symbol->symbol.section->output_section->moving_line_filepos +=
843 count * bfd_coff_linesz (abfd);
845 return coff_write_symbol(abfd, &( symbol->symbol), native,written);
849 DEFUN(coff_write_symbols,(abfd),
853 unsigned int limit = bfd_get_symcount(abfd);
854 unsigned int written = 0;
859 debug_string_size = 0;
861 /* Seek to the right place */
862 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
864 /* Output all the symbols we have */
867 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
869 asymbol *symbol = *p;
870 coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
872 if (c_symbol == (coff_symbol_type *) NULL ||
873 c_symbol->native == (combined_entry_type *)NULL)
875 written = coff_write_alien_symbol(abfd, symbol, written);
879 written = coff_write_native_symbol(abfd, c_symbol, written);
884 bfd_get_symcount(abfd) = written;
886 /* Now write out strings */
888 if (string_size != 0)
890 unsigned int size = string_size + 4;
893 bfd_h_put_32(abfd, size, buffer);
894 bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
895 for (p = abfd->outsymbols, i = 0;
900 size_t name_length = strlen(q->name);
902 coff_symbol_type* c_symbol = coff_symbol_from(abfd, q);
903 maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
904 (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
907 if (name_length > maxlen
908 && ! bfd_coff_symname_in_debug (abfd,
909 &c_symbol->native->u.syment))
910 bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
914 /* We would normally not write anything here, but we'll write
915 out 4 so that any stupid coff reader which tries to read
916 the string table even when there isn't one won't croak. */
917 unsigned int size = 4;
920 bfd_h_put_32 (abfd, size, buffer);
921 bfd_write((PTR) buffer, 1, sizeof (buffer), abfd);
924 BFD_ASSERT (debug_string_size == 0
925 || (debug_string_section != (asection *) NULL
926 && (BFD_ALIGN (debug_string_size,
927 1 << debug_string_section->alignment_power)
928 == bfd_section_size (abfd, debug_string_section))));
932 DEFUN(coff_write_linenumbers,(abfd),
936 bfd_size_type linesz;
939 linesz = bfd_coff_linesz (abfd);
940 buff = bfd_alloc (abfd, linesz);
943 bfd_set_error (bfd_error_no_memory);
946 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
947 if (s->lineno_count) {
948 asymbol **q = abfd->outsymbols;
949 bfd_seek(abfd, s->line_filepos, SEEK_SET);
950 /* Find all the linenumbers in this section */
953 if (p->section->output_section == s) {
955 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
957 /* Found a linenumber entry, output */
958 struct internal_lineno out;
959 memset( (PTR)&out, 0, sizeof(out));
961 out.l_addr.l_symndx = l->u.offset;
962 bfd_coff_swap_lineno_out(abfd, &out, buff);
963 bfd_write(buff, 1, linesz, abfd);
965 while (l->line_number) {
966 out.l_lnno = l->line_number;
967 out.l_addr.l_symndx = l->u.offset;
968 bfd_coff_swap_lineno_out(abfd, &out, buff);
969 bfd_write(buff, 1, linesz, abfd);
978 bfd_release (abfd, buff);
984 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
988 return coffsymbol(symbol)->lineno;
992 coff_section_symbol (abfd, name)
996 asection *sec = bfd_make_section_old_way (abfd, name);
998 combined_entry_type *csym;
1001 csym = coff_symbol_from (abfd, sym)->native;
1002 /* Make sure back-end COFF stuff is there. */
1006 coff_symbol_type sym;
1007 /* @@FIXME This shouldn't use a fixed size!! */
1008 combined_entry_type e[10];
1011 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
1014 bfd_set_error (bfd_error_no_error);
1017 memset ((char *) f, 0, sizeof (*f));
1018 coff_symbol_from (abfd, sym)->native = csym = f->e;
1020 csym[0].u.syment.n_sclass = C_STAT;
1021 csym[0].u.syment.n_numaux = 1;
1022 /* SF_SET_STATICS (sym); @@ ??? */
1023 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1024 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1025 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1027 if (sec->output_section == NULL)
1029 sec->output_section = sec;
1030 sec->output_offset = 0;
1036 /* This function transforms the offsets into the symbol table into
1037 pointers to syments. */
1040 DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
1042 combined_entry_type *table_base AND
1045 combined_entry_type *auxent)
1047 /* Don't bother if this is a file or a section */
1048 if (class == C_STAT && type == T_NULL) return;
1049 if (class == C_FILE) return;
1051 /* Otherwise patch up */
1052 #define N_TMASK coff_data (abfd)->local_n_tmask
1053 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1054 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
1055 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
1056 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1057 auxent->fix_end = 1;
1059 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1060 generate one, so we must be careful to ignore it. */
1061 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
1062 auxent->u.auxent.x_sym.x_tagndx.p =
1063 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1064 auxent->fix_tag = 1;
1069 DEFUN(build_string_table,(abfd),
1072 char string_table_size_buffer[4];
1073 unsigned int string_table_size;
1076 /* At this point we should be "seek"'d to the end of the
1077 symbols === the symbol table size. */
1078 if (bfd_read((char *) string_table_size_buffer,
1079 sizeof(string_table_size_buffer),
1080 1, abfd) != sizeof(string_table_size)) {
1081 bfd_set_error (bfd_error_system_call);
1085 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
1087 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
1088 bfd_set_error (bfd_error_no_memory);
1090 } /* on mallocation error */
1091 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
1092 bfd_set_error (bfd_error_system_call);
1095 return string_table;
1098 /* Allocate space for the ".debug" section, and read it.
1099 We did not read the debug section until now, because
1100 we didn't want to go to the trouble until someone needed it. */
1103 DEFUN(build_debug_section,(abfd),
1106 char *debug_section;
1109 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1112 bfd_set_error (bfd_error_no_debug_section);
1116 debug_section = (PTR) bfd_alloc (abfd,
1117 bfd_get_section_size_before_reloc (sect));
1118 if (debug_section == NULL) {
1119 bfd_set_error (bfd_error_no_memory);
1123 /* Seek to the beginning of the `.debug' section and read it.
1124 Save the current position first; it is needed by our caller.
1125 Then read debug section and reset the file pointer. */
1127 position = bfd_tell (abfd);
1128 bfd_seek (abfd, sect->filepos, SEEK_SET);
1129 if (bfd_read (debug_section,
1130 bfd_get_section_size_before_reloc (sect), 1, abfd)
1131 != bfd_get_section_size_before_reloc(sect)) {
1132 bfd_set_error (bfd_error_system_call);
1135 bfd_seek (abfd, position, SEEK_SET);
1136 return debug_section;
1140 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1141 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1142 be \0-terminated. */
1144 DEFUN(copy_name,(abfd, name, maxlen),
1152 for (len = 0; len < maxlen; ++len) {
1153 if (name[len] == '\0') {
1158 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1159 bfd_set_error (bfd_error_no_memory);
1162 strncpy(newname, name, len);
1163 newname[len] = '\0';
1167 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1168 knit the symbol names into a normalized form. By normalized here I
1169 mean that all symbols have an n_offset pointer that points to a null-
1170 terminated string. */
1172 combined_entry_type *
1173 DEFUN(coff_get_normalized_symtab,(abfd),
1176 combined_entry_type *internal;
1177 combined_entry_type *internal_ptr;
1178 combined_entry_type *symbol_ptr;
1179 combined_entry_type *internal_end;
1180 bfd_size_type symesz;
1184 char *string_table = NULL;
1185 char *debug_section = NULL;
1188 unsigned int raw_size;
1189 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1190 return obj_raw_syments(abfd);
1192 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1193 bfd_set_error (bfd_error_no_symbols);
1197 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1200 bfd_set_error (bfd_error_no_memory);
1203 internal_end = internal + bfd_get_symcount(abfd);
1205 symesz = bfd_coff_symesz (abfd);
1206 raw_size = bfd_get_symcount(abfd) * symesz;
1207 raw = bfd_alloc(abfd,raw_size);
1210 bfd_set_error (bfd_error_no_memory);
1214 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1215 || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
1216 bfd_set_error (bfd_error_system_call);
1219 /* mark the end of the symbols */
1220 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1222 FIXME SOMEDAY. A string table size of zero is very weird, but
1223 probably possible. If one shows up, it will probably kill us.
1226 /* Swap all the raw entries */
1227 for (raw_src = (char *) raw, internal_ptr = internal;
1229 raw_src += symesz, internal_ptr++) {
1232 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1233 internal_ptr->fix_value = 0;
1234 internal_ptr->fix_tag = 0;
1235 internal_ptr->fix_end = 0;
1236 internal_ptr->fix_scnlen = 0;
1237 symbol_ptr = internal_ptr;
1240 i < symbol_ptr->u.syment.n_numaux;
1246 internal_ptr->fix_value = 0;
1247 internal_ptr->fix_tag = 0;
1248 internal_ptr->fix_end = 0;
1249 internal_ptr->fix_scnlen = 0;
1250 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1251 symbol_ptr->u.syment.n_type,
1252 symbol_ptr->u.syment.n_sclass,
1253 i, symbol_ptr->u.syment.n_numaux,
1254 &(internal_ptr->u.auxent));
1255 /* Remember that bal entries arn't pointerized */
1256 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1259 coff_pointerize_aux(abfd,
1261 symbol_ptr->u.syment.n_type,
1262 symbol_ptr->u.syment.n_sclass,
1269 /* Free all the raw stuff */
1270 bfd_release(abfd, raw);
1272 for (internal_ptr = internal; internal_ptr < internal_end;
1275 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1276 /* make a file symbol point to the name in the auxent, since
1277 the text ".file" is redundant */
1278 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1279 /* the filename is a long one, point into the string table */
1280 if (string_table == NULL) {
1281 string_table = build_string_table(abfd);
1284 internal_ptr->u.syment._n._n_n._n_offset =
1285 (long) (string_table - 4 +
1286 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1289 /* ordinary short filename, put into memory anyway */
1290 internal_ptr->u.syment._n._n_n._n_offset = (long)
1291 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1296 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1297 /* This is a "short" name. Make it long. */
1298 unsigned long i = 0;
1299 char *newstring = NULL;
1301 /* find the length of this string without walking into memory
1303 for (i = 0; i < 8; ++i) {
1304 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1306 } /* if end of string */
1307 } /* possible lengths of this string. */
1309 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1310 bfd_set_error (bfd_error_no_memory);
1313 memset(newstring, 0, i);
1314 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1315 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1316 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1318 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1319 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1320 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1321 /* Long name already. Point symbol at the string in the table. */
1322 if (string_table == NULL) {
1323 string_table = build_string_table(abfd);
1325 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1326 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1329 /* Long name in debug section. Very similar. */
1330 if (debug_section == NULL) {
1331 debug_section = build_debug_section(abfd);
1333 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1334 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1337 internal_ptr += internal_ptr->u.syment.n_numaux;
1340 obj_raw_syments(abfd) = internal;
1341 obj_raw_syment_count(abfd) = internal_ptr - internal;
1344 } /* coff_get_normalized_symtab() */
1347 DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
1351 if (bfd_get_format(abfd) != bfd_object) {
1352 bfd_set_error (bfd_error_invalid_operation);
1355 return (asect->reloc_count + 1) * sizeof(arelent *);
1359 DEFUN (coff_make_empty_symbol, (abfd),
1362 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1364 bfd_set_error (bfd_error_no_memory);
1367 memset (new, 0, sizeof *new);
1368 new->symbol.section = 0;
1370 new->lineno = (alent *) NULL;
1371 new->done_lineno = false;
1372 new->symbol.the_bfd = abfd;
1373 return &new->symbol;
1376 /* Make a debugging symbol. */
1379 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1384 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1386 bfd_set_error (bfd_error_no_memory);
1389 /* @@ This shouldn't be using a constant multiplier. */
1390 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1393 bfd_set_error (bfd_error_no_memory);
1396 new->symbol.section = &bfd_debug_section;
1397 new->lineno = (alent *) NULL;
1398 new->done_lineno = false;
1399 new->symbol.the_bfd = abfd;
1400 return &new->symbol;
1405 coff_get_symbol_info (abfd, symbol, ret)
1410 bfd_symbol_info (symbol, ret);
1413 /* Print out information about COFF symbol. */
1416 coff_print_symbol (abfd, filep, symbol, how)
1420 bfd_print_symbol_type how;
1422 FILE *file = (FILE *) filep;
1426 case bfd_print_symbol_name:
1427 fprintf (file, "%s", symbol->name);
1430 case bfd_print_symbol_more:
1431 fprintf (file, "coff %s %s",
1432 coffsymbol(symbol)->native ? "n" : "g",
1433 coffsymbol(symbol)->lineno ? "l" : " ");
1436 case bfd_print_symbol_all:
1437 if (coffsymbol(symbol)->native)
1440 combined_entry_type *combined = coffsymbol (symbol)->native;
1441 combined_entry_type *root = obj_raw_syments (abfd);
1442 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1444 fprintf (file,"[%3d]", combined - root);
1447 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
1448 combined->u.syment.n_scnum,
1449 combined->u.syment.n_flags,
1450 combined->u.syment.n_type,
1451 combined->u.syment.n_sclass,
1452 combined->u.syment.n_numaux,
1453 (unsigned long) combined->u.syment.n_value,
1456 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1458 combined_entry_type *auxp = combined + aux + 1;
1462 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1464 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1466 fprintf (file, "\n");
1467 switch (combined->u.syment.n_sclass)
1470 fprintf (file, "File ");
1474 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
1475 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1476 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1484 fprintf (file, "\n%s :", l->u.sym->name);
1486 while (l->line_number)
1488 fprintf (file, "\n%4d : 0x%lx",
1491 (l->u.offset + symbol->section->vma)));
1498 bfd_print_symbol_vandf ((PTR) file, symbol);
1499 fprintf (file, " %-5s %s %s %s",
1500 symbol->section->name,
1501 coffsymbol(symbol)->native ? "n" : "g",
1502 coffsymbol(symbol)->lineno ? "l" : " ",
1508 /* Provided a BFD, a section and an offset into the section, calculate
1509 and return the name of the source file and the line nearest to the
1514 DEFUN(coff_find_nearest_line,(abfd,
1522 asection *section AND
1523 asymbol **ignore_symbols AND
1525 CONST char **filename_ptr AND
1526 CONST char **functionname_ptr AND
1527 unsigned int *line_ptr)
1529 static bfd *cache_abfd;
1530 static asection *cache_section;
1531 static bfd_vma cache_offset;
1532 static unsigned int cache_i;
1533 static CONST char *cache_function;
1534 static unsigned int line_base = 0;
1537 coff_data_type *cof = coff_data(abfd);
1538 /* Run through the raw syments if available */
1539 combined_entry_type *p;
1544 *functionname_ptr = 0;
1547 /* Don't try and find line numbers in a non coff file */
1548 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1554 p = cof->raw_syments;
1556 for (i = 0; i < cof->raw_syment_count; i++) {
1557 if (p->u.syment.n_sclass == C_FILE) {
1558 /* File name has been moved into symbol */
1559 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1562 p += 1 + p->u.syment.n_numaux;
1564 /* Now wander though the raw linenumbers of the section */
1566 If this is the same BFD as we were previously called with and this is
1567 the same section, and the offset we want is further down then we can
1568 prime the lookup loop
1570 if (abfd == cache_abfd &&
1571 section == cache_section &&
1572 offset >= cache_offset) {
1574 *functionname_ptr = cache_function;
1579 l = §ion->lineno[i];
1581 for (; i < section->lineno_count; i++) {
1582 if (l->line_number == 0) {
1583 /* Get the symbol this line number points at */
1584 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1585 if (coff->symbol.value > offset)
1587 *functionname_ptr = coff->symbol.name;
1589 combined_entry_type *s = coff->native;
1590 s = s + 1 + s->u.syment.n_numaux;
1592 S should now point to the .bf of the function
1594 if (s->u.syment.n_numaux) {
1596 The linenumber is stored in the auxent
1598 union internal_auxent *a = &((s + 1)->u.auxent);
1599 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1600 *line_ptr = line_base;
1605 if (l->u.offset > offset)
1607 *line_ptr = l->line_number + line_base - 1;
1613 cache_section = section;
1614 cache_offset = offset;
1616 cache_function = *functionname_ptr;
1622 DEFUN(coff_sizeof_headers,(abfd, reloc),
1628 if (reloc == false) {
1629 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1632 size = bfd_coff_filhsz (abfd);
1635 size += abfd->section_count * bfd_coff_scnhsz (abfd);