1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992 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 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_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);
69 if (return_section == NULL)
72 /* s_paddr is presumed to be = to s_vaddr */
74 return_section->vma = hdr->s_vaddr;
75 return_section->_raw_size = hdr->s_size;
76 return_section->filepos = hdr->s_scnptr;
77 return_section->rel_filepos = hdr->s_relptr;
78 return_section->reloc_count = hdr->s_nreloc;
80 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
82 return_section->line_filepos = hdr->s_lnnoptr;
84 return_section->lineno_count = hdr->s_nlnno;
85 return_section->userdata = NULL;
86 return_section->next = (asection *) NULL;
87 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
89 return_section->target_index = target_index;
91 if (hdr->s_nreloc != 0)
92 return_section->flags |= SEC_RELOC;
93 /* FIXME: should this check 'hdr->s_size > 0' */
94 if (hdr->s_scnptr != 0)
95 return_section->flags |= SEC_HAS_CONTENTS;
99 /* Read in a COFF object and make it into a BFD. This is used by
104 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
107 struct internal_filehdr *internal_f AND
108 struct internal_aouthdr *internal_a)
111 size_t readsize; /* length of file_info */
113 char *external_sections;
115 /* Build a play area */
116 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f);
120 scnhsz = bfd_coff_scnhsz (abfd);
121 readsize = nscns * scnhsz;
122 external_sections = (char *)bfd_alloc(abfd, readsize);
124 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
128 /* Now copy data as required; construct all asections etc */
131 for (i = 0; i < nscns; i++) {
132 struct internal_scnhdr tmp;
133 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
135 make_a_section_from_file(abfd,&tmp, i+1);
139 /* make_abs_section(abfd);*/
141 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
144 if (!(internal_f->f_flags & F_RELFLG))
145 abfd->flags |= HAS_RELOC;
146 if ((internal_f->f_flags & F_EXEC))
147 abfd->flags |= EXEC_P;
148 if (!(internal_f->f_flags & F_LNNO))
149 abfd->flags |= HAS_LINENO;
150 if (!(internal_f->f_flags & F_LSYMS))
151 abfd->flags |= HAS_LOCALS;
154 bfd_get_symcount(abfd) = internal_f->f_nsyms;
155 if (internal_f->f_nsyms)
156 abfd->flags |= HAS_SYMS;
158 bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0;
162 bfd_release(abfd, tdata);
163 return (bfd_target *)NULL;
166 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
167 not a COFF file. This is also used by ECOFF. */
170 DEFUN(coff_object_p,(abfd),
177 struct internal_filehdr internal_f;
178 struct internal_aouthdr internal_a;
180 bfd_error = system_call_error;
182 /* figure out how much to read */
183 filhsz = bfd_coff_filhsz (abfd);
184 aoutsz = bfd_coff_aoutsz (abfd);
186 filehdr = bfd_alloc (abfd, filhsz);
189 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
191 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
192 bfd_release (abfd, filehdr);
194 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
195 bfd_error = wrong_format;
198 nscns =internal_f.f_nscns;
200 if (internal_f.f_opthdr) {
203 opthdr = bfd_alloc (abfd, aoutsz);
206 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
209 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
212 /* Seek past the opt hdr stuff */
213 bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
215 return coff_real_object_p(abfd, nscns, &internal_f, &internal_a);
218 /* Get the BFD section from a COFF symbol section number. */
221 DEFUN(coff_section_from_bfd_index,(abfd, index),
225 struct sec *answer = abfd->sections;
229 return &bfd_abs_section;
231 if (index == N_UNDEF)
233 return &bfd_und_section;
237 return &bfd_debug_section;
242 if (answer->target_index == index)
244 answer = answer->next;
247 return &bfd_und_section; /* For gcc -W and lint. Never executed. */
250 /* Get the upper bound of a COFF symbol table. */
253 coff_get_symtab_upper_bound(abfd)
256 if (!bfd_coff_slurp_symbol_table(abfd))
259 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
263 /* Canonicalize a COFF symbol table. */
266 DEFUN(coff_get_symtab, (abfd, alocation),
270 unsigned int counter = 0;
271 coff_symbol_type *symbase;
272 coff_symbol_type **location = (coff_symbol_type **) (alocation);
273 if (!bfd_coff_slurp_symbol_table(abfd))
276 symbase = obj_symbols(abfd);
277 while (counter < bfd_get_symcount(abfd))
279 /* This nasty code looks at the symbol to decide whether or
280 not it is descibes a constructor/destructor entry point. It
281 is structured this way to (hopefully) speed non matches */
283 if (0 && symbase->symbol.name[9] == '$')
285 bfd_constructor_entry(abfd,
286 (asymbol **)location,
287 symbase->symbol.name[10] == 'I' ?
291 *(location++) = symbase++;
295 return bfd_get_symcount(abfd);
298 /* Set lineno_count for the output sections of a COFF file. */
301 DEFUN(coff_count_linenumbers,(abfd),
304 unsigned int limit = bfd_get_symcount(abfd);
308 asection *s = abfd->sections->output_section;
310 BFD_ASSERT(s->lineno_count == 0);
316 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
317 asymbol *q_maybe = *p;
318 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
319 coff_symbol_type *q = coffsymbol(q_maybe);
322 This symbol has a linenumber, increment the owning
323 section's linenumber count
325 alent *l = q->lineno;
326 q->symbol.section->output_section->lineno_count++;
328 while (l->line_number) {
329 q->symbol.section->output_section->lineno_count++;
337 /* Takes a bfd and a symbol, returns a pointer to the coff specific
338 area of the symbol if there is one. */
341 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
345 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
346 return (coff_symbol_type *)NULL;
348 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
349 return (coff_symbol_type *)NULL;
351 return (coff_symbol_type *) symbol;
355 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
356 coff_symbol_type *coff_symbol_ptr AND
357 struct internal_syment *syment)
360 /* Normalize the symbol flags */
361 if (coff_symbol_ptr->symbol.section == &bfd_com_section) {
362 /* a common symbol is undefined with a value */
363 syment->n_scnum = N_UNDEF;
364 syment->n_value = coff_symbol_ptr->symbol.value;
366 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
367 syment->n_value = coff_symbol_ptr->symbol.value;
369 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
370 syment->n_scnum = N_UNDEF;
374 if (coff_symbol_ptr->symbol.section) {
376 coff_symbol_ptr->symbol.section->output_section->target_index;
379 coff_symbol_ptr->symbol.value +
380 coff_symbol_ptr->symbol.section->output_offset +
381 coff_symbol_ptr->symbol.section->output_section->vma;
385 /* This can happen, but I don't know why yet (steve@cygnus.com) */
386 syment->n_scnum = N_ABS;
387 syment->n_value = coff_symbol_ptr->symbol.value;
392 /* run through all the symbols in the symbol table and work out what
393 their indexes into the symbol table will be when output
395 Coff requires that each C_FILE symbol points to the next one in the
396 chain, and that the last one points to the first external symbol. We
401 DEFUN(coff_renumber_symbols,(bfd_ptr),
404 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
405 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
406 unsigned int native_index = 0;
407 struct internal_syment *last_file = (struct internal_syment *)NULL;
408 unsigned int symbol_index;
410 /* COFF demands that undefined symbols come after all other symbols.
411 Since we don't need to impose this extra knowledge on all our client
412 programs, deal with that here. Sort the symbol table; just move the
413 undefined symbols to the end, leaving the rest alone. */
414 /* @@ Do we have some condition we could test for, so we don't always
415 have to do this? I don't think relocatability is quite right, but
416 I'm not certain. [raeburn:19920508.1711EST] */
421 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
423 * (symbol_count + 1));
424 bfd_ptr->outsymbols = newsyms;
425 for (i = 0; i < symbol_count; i++)
426 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
427 *newsyms++ = symbol_ptr_ptr[i];
428 for (i = 0; i < symbol_count; i++)
429 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
430 *newsyms++ = symbol_ptr_ptr[i];
431 *newsyms = (asymbol *) NULL;
432 symbol_ptr_ptr = bfd_ptr->outsymbols;
435 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
437 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
438 if (coff_symbol_ptr && coff_symbol_ptr->native) {
439 combined_entry_type *s = coff_symbol_ptr->native;
442 if (s->u.syment.n_sclass == C_FILE)
444 if (last_file != (struct internal_syment *)NULL) {
445 last_file->n_value = native_index;
447 last_file = &(s->u.syment);
451 /* Modify the symbol values according to their section and
454 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
456 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
457 s[i].offset = native_index ++;
464 obj_conv_table_size (bfd_ptr) = native_index;
468 Run thorough the symbol table again, and fix it so that all pointers to
469 entries are changed to the entries' index in the output symbol table.
473 DEFUN(coff_mangle_symbols,(bfd_ptr),
476 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
477 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
478 unsigned int symbol_index;
480 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
482 coff_symbol_type *coff_symbol_ptr =
483 coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
485 if (coff_symbol_ptr && coff_symbol_ptr->native) {
487 combined_entry_type *s = coff_symbol_ptr->native;
489 for (i = 0; i < s->u.syment.n_numaux ; i++) {
490 combined_entry_type *a = s + i + 1;
492 a->u.auxent.x_sym.x_tagndx.l =
493 a->u.auxent.x_sym.x_tagndx.p->offset;
497 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
498 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
508 static int string_size;
511 DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
514 combined_entry_type *native)
516 unsigned int name_length;
517 union internal_auxent *auxent;
518 char * name = ( char *)(symbol->name);
520 if (name == (char *) NULL) {
521 /* coff symbols always have names, so we'll make one up */
522 symbol->name = "strange";
523 name = (char *)symbol->name;
525 name_length = strlen(name);
527 if (native->u.syment.n_sclass == C_FILE) {
528 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
529 auxent = &(native+1)->u.auxent;
531 if (bfd_coff_long_filenames (abfd)) {
532 if (name_length <= FILNMLEN) {
533 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
536 auxent->x_file.x_n.x_offset = string_size + 4;
537 auxent->x_file.x_n.x_zeroes = 0;
538 string_size += name_length + 1;
542 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
543 if (name_length > FILNMLEN) {
544 name[FILNMLEN] = '\0';
549 { /* NOT A C_FILE SYMBOL */
550 if (name_length <= SYMNMLEN) {
551 /* This name will fit into the symbol neatly */
552 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
555 native->u.syment._n._n_n._n_offset = string_size + 4;
556 native->u.syment._n._n_n._n_zeroes = 0;
557 string_size += name_length + 1;
562 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
565 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
568 combined_entry_type *native AND
569 unsigned int written)
571 unsigned int numaux = native->u.syment.n_numaux;
572 int type = native->u.syment.n_type;
573 int class = native->u.syment.n_sclass;
575 bfd_size_type symesz;
577 /* @@ bfd_debug_section isn't accessible outside this file, but we know
578 that C_FILE symbols belong there. So move them. */
579 if (native->u.syment.n_sclass == C_FILE)
580 symbol->section = &bfd_debug_section;
582 if (symbol->section == &bfd_abs_section)
584 native->u.syment.n_scnum = N_ABS;
586 else if (symbol->section == &bfd_debug_section)
588 native->u.syment.n_scnum = N_DEBUG;
590 else if (symbol->section == &bfd_und_section)
592 native->u.syment.n_scnum = N_UNDEF;
596 native->u.syment.n_scnum =
597 symbol->section->output_section->target_index;
601 coff_fix_symbol_name(abfd, symbol, native);
603 symesz = bfd_coff_symesz (abfd);
604 buf = bfd_alloc (abfd, symesz);
605 bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
606 bfd_write(buf, 1, symesz, abfd);
607 bfd_release (abfd, buf);
609 if (native->u.syment.n_numaux > 0)
611 bfd_size_type auxesz;
614 auxesz = bfd_coff_auxesz (abfd);
615 buf = bfd_alloc (abfd, auxesz);
616 for (j = 0; j < native->u.syment.n_numaux; j++)
618 bfd_coff_swap_aux_out(abfd,
619 &((native + j + 1)->u.auxent),
623 bfd_write(buf, 1, auxesz, abfd);
625 bfd_release (abfd, buf);
628 Reuse somewhere in the symbol to keep the index
630 set_index(symbol, written);
631 return written + 1 + numaux;
636 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
639 unsigned int written)
642 This symbol has been created by the loader, or come from a non
643 coff format. It has no native element to inherit, make our
646 combined_entry_type *native;
647 combined_entry_type dummy;
649 native->u.syment.n_type = T_NULL;
650 native->u.syment.n_flags = 0;
651 if (symbol->section == &bfd_und_section)
653 native->u.syment.n_scnum = N_UNDEF;
654 native->u.syment.n_value = symbol->value;
656 else if (symbol->section == &bfd_com_section)
658 native->u.syment.n_scnum = N_UNDEF;
659 native->u.syment.n_value = symbol->value;
663 else if (symbol->flags & BSF_DEBUGGING) {
665 remove name so it doesn't take up any space
670 native->u.syment.n_scnum = symbol->section->output_section->target_index;
671 native->u.syment.n_value = symbol->value +
672 symbol->section->output_section->vma +
673 symbol->section->output_offset;
674 /* Copy the any flags from the the file hdr into the symbol */
676 coff_symbol_type *c = coff_symbol_from(abfd, symbol);
677 if (c != (coff_symbol_type *)NULL) {
678 native->u.syment.n_flags = bfd_asymbol_bfd(&c->symbol)->flags;
683 native->u.syment.n_type = 0;
684 if (symbol->flags & BSF_LOCAL)
685 native->u.syment.n_sclass = C_STAT;
687 native->u.syment.n_sclass = C_EXT;
688 native->u.syment.n_numaux = 0;
690 return coff_write_symbol(abfd, symbol, native, written);
694 DEFUN(coff_write_native_symbol,(abfd, symbol, written),
696 coff_symbol_type *symbol AND
697 unsigned int written)
700 Does this symbol have an ascociated line number - if so then
701 make it remember this symbol index. Also tag the auxent of
702 this symbol to point to the right place in the lineno table
704 combined_entry_type *native = symbol->native;
706 alent *lineno = symbol->lineno;
708 if (lineno && !symbol->done_lineno) {
709 unsigned int count = 0;
710 lineno[count].u.offset = written;
711 if (native->u.syment.n_numaux) {
712 union internal_auxent *a = &((native+1)->u.auxent);
714 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
715 symbol->symbol.section->output_section->moving_line_filepos;
718 And count and relocate all other linenumbers
722 while (lineno[count].line_number) {
725 I've been told this, but still need proof:
726 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
727 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
728 > only specific to A29K implementations, but affects all systems using COFF
729 > format binaries. Note that in COFF object files, the line number core offsets
730 > output by the assembler are relative to the start of each procedure, not
731 > to the start of the .text section. This patch relocates the line numbers
732 > relative to the `native->u.syment.n_value' instead of the section virtual
733 > address. modular!olson@cs.arizona.edu (Jon Olson)
735 lineno[count].u.offset += native->u.syment.n_value;
738 lineno[count].u.offset +=
739 symbol->symbol.section->output_section->vma +
740 symbol->symbol.section->output_offset;
744 symbol->done_lineno = true;
746 symbol->symbol.section->output_section->moving_line_filepos +=
747 count * bfd_coff_linesz (abfd);
749 return coff_write_symbol(abfd, &( symbol->symbol), native,written);
753 DEFUN(coff_write_symbols,(abfd),
757 unsigned int limit = bfd_get_symcount(abfd);
758 unsigned int written = 0;
765 /* Seek to the right place */
766 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
768 /* Output all the symbols we have */
771 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
773 asymbol *symbol = *p;
774 coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
776 if (c_symbol == (coff_symbol_type *) NULL ||
777 c_symbol->native == (combined_entry_type *)NULL)
779 written = coff_write_alien_symbol(abfd, symbol, written);
783 written = coff_write_native_symbol(abfd, c_symbol, written);
788 bfd_get_symcount(abfd) = written;
790 /* Now write out strings */
792 if (string_size != 0)
794 unsigned int size = string_size + 4;
797 bfd_h_put_32(abfd, size, buffer);
798 bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
799 for (p = abfd->outsymbols, i = 0;
804 size_t name_length = strlen(q->name);
806 coff_symbol_type* c_symbol = coff_symbol_from(abfd, q);
807 maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
808 (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
811 if (name_length > maxlen) {
812 bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
817 /* We would normally not write anything here, but we'll write
818 out 4 so that any stupid coff reader which tries to read
819 the string table even when there isn't one won't croak.
822 uint32e_type size = 4;
824 bfd_write((PTR)&size, 1, sizeof(size), abfd);
830 DEFUN(coff_write_linenumbers,(abfd),
834 bfd_size_type linesz;
837 linesz = bfd_coff_linesz (abfd);
838 buff = bfd_alloc (abfd, linesz);
839 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
840 if (s->lineno_count) {
841 asymbol **q = abfd->outsymbols;
842 bfd_seek(abfd, s->line_filepos, SEEK_SET);
843 /* Find all the linenumbers in this section */
847 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
849 /* Found a linenumber entry, output */
850 struct internal_lineno out;
851 memset( (PTR)&out, 0, sizeof(out));
853 out.l_addr.l_symndx = l->u.offset;
854 bfd_coff_swap_lineno_out(abfd, &out, buff);
855 bfd_write(buff, 1, linesz, abfd);
857 while (l->line_number) {
858 out.l_lnno = l->line_number;
859 out.l_addr.l_symndx = l->u.offset;
860 bfd_coff_swap_lineno_out(abfd, &out, buff);
861 bfd_write(buff, 1, linesz, abfd);
869 bfd_release (abfd, buff);
873 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
877 return coffsymbol(symbol)->lineno;
881 coff_section_symbol (abfd, name)
885 asection *sec = bfd_make_section_old_way (abfd, name);
887 combined_entry_type *csym;
890 if (coff_symbol_from (abfd, sym))
891 csym = coff_symbol_from (abfd, sym)->native;
894 /* Make sure back-end COFF stuff is there. */
898 coff_symbol_type sym;
899 /* @@FIXME This shouldn't use a fixed size!! */
900 combined_entry_type e[10];
903 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
904 memset ((char *) f, 0, sizeof (*f));
905 coff_symbol_from (abfd, sym)->native = csym = f->e;
907 csym[0].u.syment.n_sclass = C_STAT;
908 csym[0].u.syment.n_numaux = 1;
909 /* SF_SET_STATICS (sym); @@ ??? */
912 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
913 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
914 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
918 csym[1].u.auxent.x_scn.x_scnlen = 0;
919 csym[1].u.auxent.x_scn.x_nreloc = 0;
920 csym[1].u.auxent.x_scn.x_nlinno = 0;
925 /* This function transforms the offsets into the symbol table into
926 pointers to syments. */
929 DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
931 combined_entry_type *table_base AND
934 combined_entry_type *auxent)
936 /* Don't bother if this is a file or a section */
937 if (class == C_STAT && type == T_NULL) return;
938 if (class == C_FILE) return;
940 /* Otherwise patch up */
941 #define N_TMASK coff_data (abfd)->local_n_tmask
942 #define N_BTSHFT coff_data (abfd)->local_n_btshft
943 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
944 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
945 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
948 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
949 generate one, so we must be careful to ignore it. */
950 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
951 auxent->u.auxent.x_sym.x_tagndx.p =
952 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
958 DEFUN(build_string_table,(abfd),
961 char string_table_size_buffer[4];
962 unsigned int string_table_size;
965 /* At this point we should be "seek"'d to the end of the
966 symbols === the symbol table size. */
967 if (bfd_read((char *) string_table_size_buffer,
968 sizeof(string_table_size_buffer),
969 1, abfd) != sizeof(string_table_size)) {
970 bfd_error = system_call_error;
974 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
976 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
977 bfd_error = no_memory;
979 } /* on mallocation error */
980 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
981 bfd_error = system_call_error;
987 /* Allocate space for the ".debug" section, and read it.
988 We did not read the debug section until now, because
989 we didn't want to go to the trouble until someone needed it. */
992 DEFUN(build_debug_section,(abfd),
998 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1001 bfd_error = no_debug_section;
1005 debug_section = (PTR) bfd_alloc (abfd,
1006 bfd_get_section_size_before_reloc (sect));
1007 if (debug_section == NULL) {
1008 bfd_error = no_memory;
1012 /* Seek to the beginning of the `.debug' section and read it.
1013 Save the current position first; it is needed by our caller.
1014 Then read debug section and reset the file pointer. */
1016 position = bfd_tell (abfd);
1017 bfd_seek (abfd, sect->filepos, SEEK_SET);
1018 if (bfd_read (debug_section,
1019 bfd_get_section_size_before_reloc (sect), 1, abfd)
1020 != bfd_get_section_size_before_reloc(sect)) {
1021 bfd_error = system_call_error;
1024 bfd_seek (abfd, position, SEEK_SET);
1025 return debug_section;
1029 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1030 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1031 be \0-terminated. */
1033 DEFUN(copy_name,(abfd, name, maxlen),
1041 for (len = 0; len < maxlen; ++len) {
1042 if (name[len] == '\0') {
1047 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1048 bfd_error = no_memory;
1051 strncpy(newname, name, len);
1052 newname[len] = '\0';
1056 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1057 knit the symbol names into a normalized form. By normalized here I
1058 mean that all symbols have an n_offset pointer that points to a null-
1059 terminated string. */
1061 combined_entry_type *
1062 DEFUN(coff_get_normalized_symtab,(abfd),
1065 combined_entry_type *internal;
1066 combined_entry_type *internal_ptr;
1067 combined_entry_type *symbol_ptr;
1068 combined_entry_type *internal_end;
1069 bfd_size_type symesz;
1073 char *string_table = NULL;
1074 char *debug_section = NULL;
1077 unsigned int raw_size;
1078 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1079 return obj_raw_syments(abfd);
1081 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1082 bfd_error = no_symbols;
1086 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1087 internal_end = internal + bfd_get_symcount(abfd);
1089 symesz = bfd_coff_symesz (abfd);
1090 raw_size = bfd_get_symcount(abfd) * symesz;
1091 raw = bfd_alloc(abfd,raw_size);
1093 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1094 || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
1095 bfd_error = system_call_error;
1098 /* mark the end of the symbols */
1099 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1101 FIXME SOMEDAY. A string table size of zero is very weird, but
1102 probably possible. If one shows up, it will probably kill us.
1105 /* Swap all the raw entries */
1106 for (raw_src = (char *) raw, internal_ptr = internal;
1108 raw_src += symesz, internal_ptr++) {
1111 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1112 internal_ptr->fix_tag = 0;
1113 internal_ptr->fix_end = 0;
1114 symbol_ptr = internal_ptr;
1117 i < symbol_ptr->u.syment.n_numaux;
1123 internal_ptr->fix_tag = 0;
1124 internal_ptr->fix_end = 0;
1125 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1126 symbol_ptr->u.syment.n_type,
1127 symbol_ptr->u.syment.n_sclass,
1128 &(internal_ptr->u.auxent));
1129 /* Remember that bal entries arn't pointerized */
1130 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1133 coff_pointerize_aux(abfd,
1135 symbol_ptr->u.syment.n_type,
1136 symbol_ptr->u.syment.n_sclass,
1143 /* Free all the raw stuff */
1144 bfd_release(abfd, raw);
1146 for (internal_ptr = internal; internal_ptr < internal_end;
1149 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1150 /* make a file symbol point to the name in the auxent, since
1151 the text ".file" is redundant */
1152 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1153 /* the filename is a long one, point into the string table */
1154 if (string_table == NULL) {
1155 string_table = build_string_table(abfd);
1158 internal_ptr->u.syment._n._n_n._n_offset =
1159 (int) (string_table - 4 +
1160 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1163 /* ordinary short filename, put into memory anyway */
1164 internal_ptr->u.syment._n._n_n._n_offset = (int)
1165 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1170 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1171 /* This is a "short" name. Make it long. */
1172 unsigned long i = 0;
1173 char *newstring = NULL;
1175 /* find the length of this string without walking into memory
1177 for (i = 0; i < 8; ++i) {
1178 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1180 } /* if end of string */
1181 } /* possible lengths of this string. */
1183 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1184 bfd_error = no_memory;
1187 memset(newstring, 0, i);
1188 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1189 internal_ptr->u.syment._n._n_n._n_offset = (int) newstring;
1190 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1192 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1193 /* Long name already. Point symbol at the string in the table. */
1194 if (string_table == NULL) {
1195 string_table = build_string_table(abfd);
1197 internal_ptr->u.syment._n._n_n._n_offset = (int)
1198 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1201 /* Long name in debug section. Very similar. */
1202 if (debug_section == NULL) {
1203 debug_section = build_debug_section(abfd);
1205 internal_ptr->u.syment._n._n_n._n_offset = (int)
1206 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1209 internal_ptr += internal_ptr->u.syment.n_numaux;
1212 obj_raw_syments(abfd) = internal;
1215 } /* coff_get_normalized_symtab() */
1218 DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
1222 if (bfd_get_format(abfd) != bfd_object) {
1223 bfd_error = invalid_operation;
1226 return (asect->reloc_count + 1) * sizeof(arelent *);
1230 DEFUN (coff_make_empty_symbol, (abfd),
1233 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1235 bfd_error = no_memory;
1238 new->symbol.section = 0;
1240 new->lineno = (alent *) NULL;
1241 new->done_lineno = false;
1242 new->symbol.the_bfd = abfd;
1243 return &new->symbol;
1247 DEFUN (coff_make_debug_symbol, (abfd, ptr, sz),
1252 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1254 bfd_error = no_memory;
1257 /* @@ This shouldn't be using a constant multiplier. */
1258 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1259 new->symbol.section = &bfd_debug_section;
1260 new->lineno = (alent *) NULL;
1261 new->done_lineno = false;
1262 new->symbol.the_bfd = abfd;
1263 return &new->symbol;
1267 DEFUN(coff_print_symbol,(abfd, filep, symbol, how),
1271 bfd_print_symbol_type how)
1273 FILE *file = (FILE *)filep;
1275 case bfd_print_symbol_name:
1276 fprintf(file, "%s", symbol->name);
1278 case bfd_print_symbol_more:
1279 fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
1280 (unsigned long) coffsymbol(symbol)->lineno);
1282 case bfd_print_symbol_nm:
1285 CONST char *section_name = symbol->section->name;
1286 bfd_print_symbol_vandf((PTR) file, symbol);
1289 fprintf(file, " %-5s %s %s %s",
1291 coffsymbol(symbol)->native ? "n" : "g",
1292 coffsymbol(symbol)->lineno ? "l" : " ",
1298 case bfd_print_symbol_all:
1299 /* Print out the symbols in a reasonable way */
1301 CONST char *section_name = symbol->section->name;
1304 if (coffsymbol(symbol)->native)
1307 combined_entry_type *combined = coffsymbol(symbol)->native;
1308 combined_entry_type *root = obj_raw_syments(abfd);
1310 fprintf(file,"[%3d]",
1314 fprintf(file, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1315 combined->u.syment.n_scnum,
1316 combined->u.syment.n_flags,
1317 combined->u.syment.n_type,
1318 combined->u.syment.n_sclass,
1319 combined->u.syment.n_numaux,
1320 combined->u.syment.n_value,
1323 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1326 switch (combined->u.syment.n_sclass) {
1328 fprintf(file, "File ");
1331 fprintf(file, "AUX lnno %x size %x tagndx %x",
1332 combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1333 combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_size,
1334 combined[aux+1].u.auxent.x_sym.x_tagndx.l);
1342 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1345 printf("\n%s :", l->u.sym->name);
1347 while (l->line_number)
1349 printf("\n%4d : %x",
1363 bfd_print_symbol_vandf((PTR) file, symbol);
1364 fprintf(file, " %-5s %s %s %s",
1366 coffsymbol(symbol)->native ? "n" : "g",
1367 coffsymbol(symbol)->lineno ? "l" : " ",
1376 /* Provided a BFD, a section and an offset into the section, calculate
1377 and return the name of the source file and the line nearest to the
1381 DEFUN(coff_find_nearest_line,(abfd,
1389 asection *section AND
1390 asymbol **ignore_symbols AND
1392 CONST char **filename_ptr AND
1393 CONST char **functionname_ptr AND
1394 unsigned int *line_ptr)
1396 static bfd *cache_abfd;
1397 static asection *cache_section;
1398 static bfd_vma cache_offset;
1399 static unsigned int cache_i;
1400 static alent *cache_l;
1403 coff_data_type *cof = coff_data(abfd);
1404 /* Run through the raw syments if available */
1405 combined_entry_type *p;
1407 unsigned int line_base = 0;
1411 *functionname_ptr = 0;
1414 /* Don't try and find line numbers in a non coff file */
1415 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1421 p = cof->raw_syments;
1423 for (i = 0; i < cof->raw_syment_count; i++) {
1424 if (p->u.syment.n_sclass == C_FILE) {
1425 /* File name has been moved into symbol */
1426 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1429 p += 1 + p->u.syment.n_numaux;
1431 /* Now wander though the raw linenumbers of the section */
1433 If this is the same BFD as we were previously called with and this is
1434 the same section, and the offset we want is further down then we can
1435 prime the lookup loop
1437 if (abfd == cache_abfd &&
1438 section == cache_section &&
1439 offset >= cache_offset) {
1445 l = section->lineno;
1448 for (; i < section->lineno_count; i++) {
1449 if (l->line_number == 0) {
1450 /* Get the symbol this line number points at */
1451 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1452 *functionname_ptr = coff->symbol.name;
1454 combined_entry_type *s = coff->native;
1455 s = s + 1 + s->u.syment.n_numaux;
1457 S should now point to the .bf of the function
1459 if (s->u.syment.n_numaux) {
1461 The linenumber is stored in the auxent
1463 union internal_auxent *a = &((s + 1)->u.auxent);
1464 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1469 if (l->u.offset > offset)
1471 *line_ptr = l->line_number + line_base + 1;
1477 cache_section = section;
1478 cache_offset = offset;
1486 DEFUN(coff_sizeof_headers,(abfd, reloc),
1492 if (reloc == false) {
1493 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1496 size = bfd_coff_filhsz (abfd);
1499 size += abfd->section_count * bfd_coff_scnhsz (abfd);