1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993 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"
45 static asection bfd_debug_section = { "*DEBUG*" };
47 /* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
50 DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
52 struct internal_scnhdr *hdr AND
53 unsigned int target_index)
55 asection *return_section;
58 /* Assorted wastage to null-terminate the name, thanks AT&T! */
59 name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
61 bfd_error = no_memory;
64 strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
65 name[sizeof (hdr->s_name)] = 0;
67 return_section = bfd_make_section(abfd, name);
68 if (return_section == NULL)
69 return_section = bfd_coff_make_section_hook (abfd, name);
70 if (return_section == NULL)
73 /* s_paddr is presumed to be = to s_vaddr */
75 return_section->vma = hdr->s_vaddr;
76 return_section->_raw_size = hdr->s_size;
77 return_section->filepos = hdr->s_scnptr;
78 return_section->rel_filepos = hdr->s_relptr;
79 return_section->reloc_count = hdr->s_nreloc;
81 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
83 return_section->line_filepos = hdr->s_lnnoptr;
85 return_section->lineno_count = hdr->s_nlnno;
86 return_section->userdata = NULL;
87 return_section->next = (asection *) NULL;
88 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
90 return_section->target_index = target_index;
92 /* At least on i386-coff, the line number count for a shared library
93 section must be ignored. */
94 if ((return_section->flags & SEC_SHARED_LIBRARY) != 0)
95 return_section->lineno_count = 0;
97 if (hdr->s_nreloc != 0)
98 return_section->flags |= SEC_RELOC;
99 /* FIXME: should this check 'hdr->s_size > 0' */
100 if (hdr->s_scnptr != 0)
101 return_section->flags |= SEC_HAS_CONTENTS;
105 /* Read in a COFF object and make it into a BFD. This is used by
110 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
113 struct internal_filehdr *internal_f AND
114 struct internal_aouthdr *internal_a)
117 size_t readsize; /* length of file_info */
119 char *external_sections;
121 /* Build a play area */
122 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
126 scnhsz = bfd_coff_scnhsz (abfd);
127 readsize = nscns * scnhsz;
128 external_sections = (char *)bfd_alloc(abfd, readsize);
130 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
134 /* Now copy data as required; construct all asections etc */
137 for (i = 0; i < nscns; i++) {
138 struct internal_scnhdr tmp;
139 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
141 make_a_section_from_file(abfd,&tmp, i+1);
145 /* make_abs_section(abfd);*/
147 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
150 if (!(internal_f->f_flags & F_RELFLG))
151 abfd->flags |= HAS_RELOC;
152 if ((internal_f->f_flags & F_EXEC))
153 abfd->flags |= EXEC_P;
154 if (!(internal_f->f_flags & F_LNNO))
155 abfd->flags |= HAS_LINENO;
156 if (!(internal_f->f_flags & F_LSYMS))
157 abfd->flags |= HAS_LOCALS;
160 bfd_get_symcount(abfd) = internal_f->f_nsyms;
161 if (internal_f->f_nsyms)
162 abfd->flags |= HAS_SYMS;
164 if (internal_a != (struct internal_aouthdr *) NULL)
165 bfd_get_start_address (abfd) = internal_a->entry;
167 bfd_get_start_address (abfd) = 0;
171 bfd_release(abfd, tdata);
172 return (bfd_target *)NULL;
175 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
176 not a COFF file. This is also used by ECOFF. */
179 DEFUN(coff_object_p,(abfd),
186 struct internal_filehdr internal_f;
187 struct internal_aouthdr internal_a;
189 bfd_error = system_call_error;
191 /* figure out how much to read */
192 filhsz = bfd_coff_filhsz (abfd);
193 aoutsz = bfd_coff_aoutsz (abfd);
195 filehdr = bfd_alloc (abfd, filhsz);
198 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
200 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
201 bfd_release (abfd, filehdr);
203 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
204 bfd_error = wrong_format;
207 nscns =internal_f.f_nscns;
209 if (internal_f.f_opthdr) {
212 opthdr = bfd_alloc (abfd, aoutsz);
215 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
218 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
221 /* Seek past the opt hdr stuff */
222 bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
224 return coff_real_object_p(abfd, nscns, &internal_f,
225 (internal_f.f_opthdr != 0
227 : (struct internal_aouthdr *) NULL));
230 /* Get the BFD section from a COFF symbol section number. */
233 DEFUN(coff_section_from_bfd_index,(abfd, index),
237 struct sec *answer = abfd->sections;
241 return &bfd_abs_section;
243 if (index == N_UNDEF)
245 return &bfd_und_section;
249 return &bfd_debug_section;
254 if (answer->target_index == index)
256 answer = answer->next;
259 return &bfd_und_section; /* For gcc -W and lint. Never executed. */
262 /* Get the upper bound of a COFF symbol table. */
265 coff_get_symtab_upper_bound(abfd)
268 if (!bfd_coff_slurp_symbol_table(abfd))
271 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
275 /* Canonicalize a COFF symbol table. */
278 DEFUN(coff_get_symtab, (abfd, alocation),
282 unsigned int counter = 0;
283 coff_symbol_type *symbase;
284 coff_symbol_type **location = (coff_symbol_type **) (alocation);
285 if (!bfd_coff_slurp_symbol_table(abfd))
288 symbase = obj_symbols(abfd);
289 while (counter < bfd_get_symcount(abfd))
291 /* This nasty code looks at the symbol to decide whether or
292 not it is descibes a constructor/destructor entry point. It
293 is structured this way to (hopefully) speed non matches */
295 if (0 && symbase->symbol.name[9] == '$')
297 bfd_constructor_entry(abfd,
298 (asymbol **)location,
299 symbase->symbol.name[10] == 'I' ?
303 *(location++) = symbase++;
307 return bfd_get_symcount(abfd);
310 /* Set lineno_count for the output sections of a COFF file. */
313 DEFUN(coff_count_linenumbers,(abfd),
316 unsigned int limit = bfd_get_symcount(abfd);
321 asection *s = abfd->sections->output_section;
323 BFD_ASSERT(s->lineno_count == 0);
329 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
330 asymbol *q_maybe = *p;
331 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
332 coff_symbol_type *q = coffsymbol(q_maybe);
335 This symbol has a linenumber, increment the owning
336 section's linenumber count
338 alent *l = q->lineno;
339 q->symbol.section->output_section->lineno_count++;
342 while (l->line_number) {
344 q->symbol.section->output_section->lineno_count++;
353 /* Takes a bfd and a symbol, returns a pointer to the coff specific
354 area of the symbol if there is one. */
357 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
361 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
362 return (coff_symbol_type *)NULL;
364 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
365 return (coff_symbol_type *)NULL;
367 return (coff_symbol_type *) symbol;
371 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
372 coff_symbol_type *coff_symbol_ptr AND
373 struct internal_syment *syment)
376 /* Normalize the symbol flags */
377 if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
378 /* a common symbol is undefined with a value */
379 syment->n_scnum = N_UNDEF;
380 syment->n_value = coff_symbol_ptr->symbol.value;
382 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
383 syment->n_value = coff_symbol_ptr->symbol.value;
385 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
386 syment->n_scnum = N_UNDEF;
390 if (coff_symbol_ptr->symbol.section) {
392 coff_symbol_ptr->symbol.section->output_section->target_index;
395 coff_symbol_ptr->symbol.value +
396 coff_symbol_ptr->symbol.section->output_offset +
397 coff_symbol_ptr->symbol.section->output_section->vma;
402 syment->n_scnum = N_ABS;
403 syment->n_value = coff_symbol_ptr->symbol.value;
408 /* run through all the symbols in the symbol table and work out what
409 their indexes into the symbol table will be when output
411 Coff requires that each C_FILE symbol points to the next one in the
412 chain, and that the last one points to the first external symbol. We
417 DEFUN(coff_renumber_symbols,(bfd_ptr),
420 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
421 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
422 unsigned int native_index = 0;
423 struct internal_syment *last_file = (struct internal_syment *)NULL;
424 unsigned int symbol_index;
426 /* COFF demands that undefined symbols come after all other symbols.
427 Since we don't need to impose this extra knowledge on all our client
428 programs, deal with that here. Sort the symbol table; just move the
429 undefined symbols to the end, leaving the rest alone. */
430 /* @@ Do we have some condition we could test for, so we don't always
431 have to do this? I don't think relocatability is quite right, but
432 I'm not certain. [raeburn:19920508.1711EST] */
437 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
439 * (symbol_count + 1));
440 bfd_ptr->outsymbols = newsyms;
441 for (i = 0; i < symbol_count; i++)
442 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
443 *newsyms++ = symbol_ptr_ptr[i];
444 for (i = 0; i < symbol_count; i++)
445 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
446 *newsyms++ = symbol_ptr_ptr[i];
447 *newsyms = (asymbol *) NULL;
448 symbol_ptr_ptr = bfd_ptr->outsymbols;
451 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
453 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
454 if (coff_symbol_ptr && coff_symbol_ptr->native) {
455 combined_entry_type *s = coff_symbol_ptr->native;
458 if (s->u.syment.n_sclass == C_FILE)
460 if (last_file != (struct internal_syment *)NULL) {
461 last_file->n_value = native_index;
463 last_file = &(s->u.syment);
467 /* Modify the symbol values according to their section and
470 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
472 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
473 s[i].offset = native_index ++;
480 obj_conv_table_size (bfd_ptr) = native_index;
484 Run thorough the symbol table again, and fix it so that all pointers to
485 entries are changed to the entries' index in the output symbol table.
489 DEFUN(coff_mangle_symbols,(bfd_ptr),
492 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
493 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
494 unsigned int symbol_index;
496 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
498 coff_symbol_type *coff_symbol_ptr =
499 coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
501 if (coff_symbol_ptr && coff_symbol_ptr->native) {
503 combined_entry_type *s = coff_symbol_ptr->native;
505 for (i = 0; i < s->u.syment.n_numaux ; i++) {
506 combined_entry_type *a = s + i + 1;
508 a->u.auxent.x_sym.x_tagndx.l =
509 a->u.auxent.x_sym.x_tagndx.p->offset;
513 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
514 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
524 static int string_size;
527 DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
530 combined_entry_type *native)
532 unsigned int name_length;
533 union internal_auxent *auxent;
534 char * name = ( char *)(symbol->name);
536 if (name == (char *) NULL) {
537 /* coff symbols always have names, so we'll make one up */
538 symbol->name = "strange";
539 name = (char *)symbol->name;
541 name_length = strlen(name);
543 if (native->u.syment.n_sclass == C_FILE) {
544 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
545 auxent = &(native+1)->u.auxent;
547 if (bfd_coff_long_filenames (abfd)) {
548 if (name_length <= FILNMLEN) {
549 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
552 auxent->x_file.x_n.x_offset = string_size + 4;
553 auxent->x_file.x_n.x_zeroes = 0;
554 string_size += name_length + 1;
558 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
559 if (name_length > FILNMLEN) {
560 name[FILNMLEN] = '\0';
565 { /* NOT A C_FILE SYMBOL */
566 if (name_length <= SYMNMLEN) {
567 /* This name will fit into the symbol neatly */
568 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
571 native->u.syment._n._n_n._n_offset = string_size + 4;
572 native->u.syment._n._n_n._n_zeroes = 0;
573 string_size += name_length + 1;
578 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
581 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
584 combined_entry_type *native AND
585 unsigned int written)
587 unsigned int numaux = native->u.syment.n_numaux;
588 int type = native->u.syment.n_type;
589 int class = native->u.syment.n_sclass;
591 bfd_size_type symesz;
593 /* @@ bfd_debug_section isn't accessible outside this file, but we know
594 that C_FILE symbols belong there. So move them. */
595 if (native->u.syment.n_sclass == C_FILE)
596 symbol->section = &bfd_debug_section;
598 if (symbol->section == &bfd_abs_section)
600 native->u.syment.n_scnum = N_ABS;
602 else if (symbol->section == &bfd_debug_section)
604 native->u.syment.n_scnum = N_DEBUG;
606 else if (symbol->section == &bfd_und_section)
608 native->u.syment.n_scnum = N_UNDEF;
612 native->u.syment.n_scnum =
613 symbol->section->output_section->target_index;
617 coff_fix_symbol_name(abfd, symbol, native);
619 symesz = bfd_coff_symesz (abfd);
620 buf = bfd_alloc (abfd, symesz);
621 bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
622 bfd_write(buf, 1, symesz, abfd);
623 bfd_release (abfd, buf);
625 if (native->u.syment.n_numaux > 0)
627 bfd_size_type auxesz;
630 auxesz = bfd_coff_auxesz (abfd);
631 buf = bfd_alloc (abfd, auxesz);
632 for (j = 0; j < native->u.syment.n_numaux; j++)
634 bfd_coff_swap_aux_out(abfd,
635 &((native + j + 1)->u.auxent),
639 bfd_write(buf, 1, auxesz, abfd);
641 bfd_release (abfd, buf);
644 Reuse somewhere in the symbol to keep the index
646 set_index(symbol, written);
647 return written + 1 + numaux;
652 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
655 unsigned int written)
658 This symbol has been created by the loader, or come from a non
659 coff format. It has no native element to inherit, make our
662 combined_entry_type *native;
663 combined_entry_type dummy;
665 native->u.syment.n_type = T_NULL;
666 native->u.syment.n_flags = 0;
667 if (symbol->section == &bfd_und_section)
669 native->u.syment.n_scnum = N_UNDEF;
670 native->u.syment.n_value = symbol->value;
672 else if (bfd_is_com_section (symbol->section))
674 native->u.syment.n_scnum = N_UNDEF;
675 native->u.syment.n_value = symbol->value;
679 else if (symbol->flags & BSF_DEBUGGING) {
681 remove name so it doesn't take up any space
686 native->u.syment.n_scnum = symbol->section->output_section->target_index;
687 native->u.syment.n_value = symbol->value +
688 symbol->section->output_section->vma +
689 symbol->section->output_offset;
690 /* Copy the any flags from the the file hdr into the symbol */
692 coff_symbol_type *c = coff_symbol_from(abfd, symbol);
693 if (c != (coff_symbol_type *)NULL) {
694 native->u.syment.n_flags = bfd_asymbol_bfd(&c->symbol)->flags;
699 native->u.syment.n_type = 0;
700 if (symbol->flags & BSF_LOCAL)
701 native->u.syment.n_sclass = C_STAT;
703 native->u.syment.n_sclass = C_EXT;
704 native->u.syment.n_numaux = 0;
706 return coff_write_symbol(abfd, symbol, native, written);
710 DEFUN(coff_write_native_symbol,(abfd, symbol, written),
712 coff_symbol_type *symbol AND
713 unsigned int written)
716 Does this symbol have an ascociated line number - if so then
717 make it remember this symbol index. Also tag the auxent of
718 this symbol to point to the right place in the lineno table
720 combined_entry_type *native = symbol->native;
722 alent *lineno = symbol->lineno;
724 if (lineno && !symbol->done_lineno) {
725 unsigned int count = 0;
726 lineno[count].u.offset = written;
727 if (native->u.syment.n_numaux) {
728 union internal_auxent *a = &((native+1)->u.auxent);
730 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
731 symbol->symbol.section->output_section->moving_line_filepos;
734 And count and relocate all other linenumbers
738 while (lineno[count].line_number) {
741 I've been told this, but still need proof:
742 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
743 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
744 > only specific to A29K implementations, but affects all systems using COFF
745 > format binaries. Note that in COFF object files, the line number core offsets
746 > output by the assembler are relative to the start of each procedure, not
747 > to the start of the .text section. This patch relocates the line numbers
748 > relative to the `native->u.syment.n_value' instead of the section virtual
751 lineno[count].u.offset += native->u.syment.n_value;
754 lineno[count].u.offset +=
755 symbol->symbol.section->output_section->vma +
756 symbol->symbol.section->output_offset;
760 symbol->done_lineno = true;
762 symbol->symbol.section->output_section->moving_line_filepos +=
763 count * bfd_coff_linesz (abfd);
765 return coff_write_symbol(abfd, &( symbol->symbol), native,written);
769 DEFUN(coff_write_symbols,(abfd),
773 unsigned int limit = bfd_get_symcount(abfd);
774 unsigned int written = 0;
781 /* Seek to the right place */
782 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
784 /* Output all the symbols we have */
787 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
789 asymbol *symbol = *p;
790 coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
792 if (c_symbol == (coff_symbol_type *) NULL ||
793 c_symbol->native == (combined_entry_type *)NULL)
795 written = coff_write_alien_symbol(abfd, symbol, written);
799 written = coff_write_native_symbol(abfd, c_symbol, written);
804 bfd_get_symcount(abfd) = written;
806 /* Now write out strings */
808 if (string_size != 0)
810 unsigned int size = string_size + 4;
813 bfd_h_put_32(abfd, size, buffer);
814 bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
815 for (p = abfd->outsymbols, i = 0;
820 size_t name_length = strlen(q->name);
822 coff_symbol_type* c_symbol = coff_symbol_from(abfd, q);
823 maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
824 (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
827 if (name_length > maxlen) {
828 bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
833 /* We would normally not write anything here, but we'll write
834 out 4 so that any stupid coff reader which tries to read
835 the string table even when there isn't one won't croak. */
836 unsigned int size = 4;
839 bfd_h_put_32 (abfd, size, buffer);
840 bfd_write((PTR) buffer, 1, sizeof (buffer), abfd);
845 DEFUN(coff_write_linenumbers,(abfd),
849 bfd_size_type linesz;
852 linesz = bfd_coff_linesz (abfd);
853 buff = bfd_alloc (abfd, linesz);
854 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
855 if (s->lineno_count) {
856 asymbol **q = abfd->outsymbols;
857 bfd_seek(abfd, s->line_filepos, SEEK_SET);
858 /* Find all the linenumbers in this section */
861 if (p->section->output_section == s) {
863 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
865 /* Found a linenumber entry, output */
866 struct internal_lineno out;
867 memset( (PTR)&out, 0, sizeof(out));
869 out.l_addr.l_symndx = l->u.offset;
870 bfd_coff_swap_lineno_out(abfd, &out, buff);
871 bfd_write(buff, 1, linesz, abfd);
873 while (l->line_number) {
874 out.l_lnno = l->line_number;
875 out.l_addr.l_symndx = l->u.offset;
876 bfd_coff_swap_lineno_out(abfd, &out, buff);
877 bfd_write(buff, 1, linesz, abfd);
886 bfd_release (abfd, buff);
890 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
894 return coffsymbol(symbol)->lineno;
898 coff_section_symbol (abfd, name)
902 asection *sec = bfd_make_section_old_way (abfd, name);
904 combined_entry_type *csym;
907 if (coff_symbol_from (abfd, sym))
908 csym = coff_symbol_from (abfd, sym)->native;
911 /* Make sure back-end COFF stuff is there. */
915 coff_symbol_type sym;
916 /* @@FIXME This shouldn't use a fixed size!! */
917 combined_entry_type e[10];
920 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
921 memset ((char *) f, 0, sizeof (*f));
922 coff_symbol_from (abfd, sym)->native = csym = f->e;
924 csym[0].u.syment.n_sclass = C_STAT;
925 csym[0].u.syment.n_numaux = 1;
926 /* SF_SET_STATICS (sym); @@ ??? */
929 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
930 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
931 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
935 csym[1].u.auxent.x_scn.x_scnlen = 0;
936 csym[1].u.auxent.x_scn.x_nreloc = 0;
937 csym[1].u.auxent.x_scn.x_nlinno = 0;
942 /* This function transforms the offsets into the symbol table into
943 pointers to syments. */
946 DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
948 combined_entry_type *table_base AND
951 combined_entry_type *auxent)
953 /* Don't bother if this is a file or a section */
954 if (class == C_STAT && type == T_NULL) return;
955 if (class == C_FILE) return;
957 /* Otherwise patch up */
958 #define N_TMASK coff_data (abfd)->local_n_tmask
959 #define N_BTSHFT coff_data (abfd)->local_n_btshft
960 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
961 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
962 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
965 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
966 generate one, so we must be careful to ignore it. */
967 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
968 auxent->u.auxent.x_sym.x_tagndx.p =
969 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
975 DEFUN(build_string_table,(abfd),
978 char string_table_size_buffer[4];
979 unsigned int string_table_size;
982 /* At this point we should be "seek"'d to the end of the
983 symbols === the symbol table size. */
984 if (bfd_read((char *) string_table_size_buffer,
985 sizeof(string_table_size_buffer),
986 1, abfd) != sizeof(string_table_size)) {
987 bfd_error = system_call_error;
991 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
993 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
994 bfd_error = no_memory;
996 } /* on mallocation error */
997 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
998 bfd_error = system_call_error;
1001 return string_table;
1004 /* Allocate space for the ".debug" section, and read it.
1005 We did not read the debug section until now, because
1006 we didn't want to go to the trouble until someone needed it. */
1009 DEFUN(build_debug_section,(abfd),
1012 char *debug_section;
1015 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1018 bfd_error = no_debug_section;
1022 debug_section = (PTR) bfd_alloc (abfd,
1023 bfd_get_section_size_before_reloc (sect));
1024 if (debug_section == NULL) {
1025 bfd_error = no_memory;
1029 /* Seek to the beginning of the `.debug' section and read it.
1030 Save the current position first; it is needed by our caller.
1031 Then read debug section and reset the file pointer. */
1033 position = bfd_tell (abfd);
1034 bfd_seek (abfd, sect->filepos, SEEK_SET);
1035 if (bfd_read (debug_section,
1036 bfd_get_section_size_before_reloc (sect), 1, abfd)
1037 != bfd_get_section_size_before_reloc(sect)) {
1038 bfd_error = system_call_error;
1041 bfd_seek (abfd, position, SEEK_SET);
1042 return debug_section;
1046 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1047 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1048 be \0-terminated. */
1050 DEFUN(copy_name,(abfd, name, maxlen),
1058 for (len = 0; len < maxlen; ++len) {
1059 if (name[len] == '\0') {
1064 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1065 bfd_error = no_memory;
1068 strncpy(newname, name, len);
1069 newname[len] = '\0';
1073 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1074 knit the symbol names into a normalized form. By normalized here I
1075 mean that all symbols have an n_offset pointer that points to a null-
1076 terminated string. */
1078 combined_entry_type *
1079 DEFUN(coff_get_normalized_symtab,(abfd),
1082 combined_entry_type *internal;
1083 combined_entry_type *internal_ptr;
1084 combined_entry_type *symbol_ptr;
1085 combined_entry_type *internal_end;
1086 bfd_size_type symesz;
1090 char *string_table = NULL;
1091 char *debug_section = NULL;
1094 unsigned int raw_size;
1095 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1096 return obj_raw_syments(abfd);
1098 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1099 bfd_error = no_symbols;
1103 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1104 internal_end = internal + bfd_get_symcount(abfd);
1106 symesz = bfd_coff_symesz (abfd);
1107 raw_size = bfd_get_symcount(abfd) * symesz;
1108 raw = bfd_alloc(abfd,raw_size);
1110 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1111 || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
1112 bfd_error = system_call_error;
1115 /* mark the end of the symbols */
1116 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1118 FIXME SOMEDAY. A string table size of zero is very weird, but
1119 probably possible. If one shows up, it will probably kill us.
1122 /* Swap all the raw entries */
1123 for (raw_src = (char *) raw, internal_ptr = internal;
1125 raw_src += symesz, internal_ptr++) {
1128 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1129 internal_ptr->fix_tag = 0;
1130 internal_ptr->fix_end = 0;
1131 symbol_ptr = internal_ptr;
1134 i < symbol_ptr->u.syment.n_numaux;
1140 internal_ptr->fix_tag = 0;
1141 internal_ptr->fix_end = 0;
1142 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1143 symbol_ptr->u.syment.n_type,
1144 symbol_ptr->u.syment.n_sclass,
1145 &(internal_ptr->u.auxent));
1146 /* Remember that bal entries arn't pointerized */
1147 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1150 coff_pointerize_aux(abfd,
1152 symbol_ptr->u.syment.n_type,
1153 symbol_ptr->u.syment.n_sclass,
1160 /* Free all the raw stuff */
1161 bfd_release(abfd, raw);
1163 for (internal_ptr = internal; internal_ptr < internal_end;
1166 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1167 /* make a file symbol point to the name in the auxent, since
1168 the text ".file" is redundant */
1169 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1170 /* the filename is a long one, point into the string table */
1171 if (string_table == NULL) {
1172 string_table = build_string_table(abfd);
1175 internal_ptr->u.syment._n._n_n._n_offset =
1176 (long) (string_table - 4 +
1177 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1180 /* ordinary short filename, put into memory anyway */
1181 internal_ptr->u.syment._n._n_n._n_offset = (long)
1182 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1187 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1188 /* This is a "short" name. Make it long. */
1189 unsigned long i = 0;
1190 char *newstring = NULL;
1192 /* find the length of this string without walking into memory
1194 for (i = 0; i < 8; ++i) {
1195 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1197 } /* if end of string */
1198 } /* possible lengths of this string. */
1200 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1201 bfd_error = no_memory;
1204 memset(newstring, 0, i);
1205 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1206 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1207 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1209 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1210 /* Long name already. Point symbol at the string in the table. */
1211 if (string_table == NULL) {
1212 string_table = build_string_table(abfd);
1214 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1215 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1218 /* Long name in debug section. Very similar. */
1219 if (debug_section == NULL) {
1220 debug_section = build_debug_section(abfd);
1222 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1223 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1226 internal_ptr += internal_ptr->u.syment.n_numaux;
1229 obj_raw_syments(abfd) = internal;
1230 obj_raw_syment_count(abfd) = internal_ptr - internal;
1233 } /* coff_get_normalized_symtab() */
1236 DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
1240 if (bfd_get_format(abfd) != bfd_object) {
1241 bfd_error = invalid_operation;
1244 return (asect->reloc_count + 1) * sizeof(arelent *);
1248 DEFUN (coff_make_empty_symbol, (abfd),
1251 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1253 bfd_error = no_memory;
1256 new->symbol.section = 0;
1258 new->lineno = (alent *) NULL;
1259 new->done_lineno = false;
1260 new->symbol.the_bfd = abfd;
1261 return &new->symbol;
1264 /* Make a debugging symbol. */
1267 coff_bfd_make_debug_symbol (abfd, ptr, sz)
1272 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1274 bfd_error = no_memory;
1277 /* @@ This shouldn't be using a constant multiplier. */
1278 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1279 new->symbol.section = &bfd_debug_section;
1280 new->lineno = (alent *) NULL;
1281 new->done_lineno = false;
1282 new->symbol.the_bfd = abfd;
1283 return &new->symbol;
1287 coff_get_symbol_info (abfd, symbol, ret)
1292 bfd_symbol_info (symbol, ret);
1295 /* Print out information about COFF symbol. */
1298 coff_print_symbol (abfd, filep, symbol, how)
1302 bfd_print_symbol_type how;
1304 FILE *file = (FILE *) filep;
1308 case bfd_print_symbol_name:
1309 fprintf (file, "%s", symbol->name);
1312 case bfd_print_symbol_more:
1313 fprintf (file, "coff %s %s",
1314 coffsymbol(symbol)->native ? "n" : "g",
1315 coffsymbol(symbol)->lineno ? "l" : " ");
1318 case bfd_print_symbol_all:
1319 if (coffsymbol(symbol)->native)
1322 combined_entry_type *combined = coffsymbol (symbol)->native;
1323 combined_entry_type *root = obj_raw_syments (abfd);
1324 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1326 fprintf (file,"[%3d]", combined - root);
1329 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08x %s",
1330 combined->u.syment.n_scnum,
1331 combined->u.syment.n_flags,
1332 combined->u.syment.n_type,
1333 combined->u.syment.n_sclass,
1334 combined->u.syment.n_numaux,
1335 combined->u.syment.n_value,
1338 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1340 combined_entry_type *auxp = combined + aux + 1;
1344 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1346 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1348 fprintf (file, "\n");
1349 switch (combined->u.syment.n_sclass)
1352 fprintf (file, "File ");
1356 fprintf (file, "AUX lnno %d size 0x%x tagndx %d",
1357 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1358 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1366 fprintf (file, "\n%s :", l->u.sym->name);
1368 while (l->line_number)
1370 fprintf (file, "\n%4d : 0x%x",
1379 bfd_print_symbol_vandf ((PTR) file, symbol);
1380 fprintf (file, " %-5s %s %s %s",
1381 symbol->section->name,
1382 coffsymbol(symbol)->native ? "n" : "g",
1383 coffsymbol(symbol)->lineno ? "l" : " ",
1389 /* Provided a BFD, a section and an offset into the section, calculate
1390 and return the name of the source file and the line nearest to the
1394 DEFUN(coff_find_nearest_line,(abfd,
1402 asection *section AND
1403 asymbol **ignore_symbols AND
1405 CONST char **filename_ptr AND
1406 CONST char **functionname_ptr AND
1407 unsigned int *line_ptr)
1409 static bfd *cache_abfd;
1410 static asection *cache_section;
1411 static bfd_vma cache_offset;
1412 static unsigned int cache_i;
1413 static CONST char *cache_function;
1414 static unsigned int line_base = 0;
1417 coff_data_type *cof = coff_data(abfd);
1418 /* Run through the raw syments if available */
1419 combined_entry_type *p;
1424 *functionname_ptr = 0;
1427 /* Don't try and find line numbers in a non coff file */
1428 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1434 p = cof->raw_syments;
1436 for (i = 0; i < cof->raw_syment_count; i++) {
1437 if (p->u.syment.n_sclass == C_FILE) {
1438 /* File name has been moved into symbol */
1439 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1442 p += 1 + p->u.syment.n_numaux;
1444 /* Now wander though the raw linenumbers of the section */
1446 If this is the same BFD as we were previously called with and this is
1447 the same section, and the offset we want is further down then we can
1448 prime the lookup loop
1450 if (abfd == cache_abfd &&
1451 section == cache_section &&
1452 offset >= cache_offset) {
1454 *functionname_ptr = cache_function;
1459 l = §ion->lineno[i];
1461 for (; i < section->lineno_count; i++) {
1462 if (l->line_number == 0) {
1463 /* Get the symbol this line number points at */
1464 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1465 if (coff->symbol.value > offset)
1467 *functionname_ptr = coff->symbol.name;
1469 combined_entry_type *s = coff->native;
1470 s = s + 1 + s->u.syment.n_numaux;
1472 S should now point to the .bf of the function
1474 if (s->u.syment.n_numaux) {
1476 The linenumber is stored in the auxent
1478 union internal_auxent *a = &((s + 1)->u.auxent);
1479 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1480 *line_ptr = line_base;
1485 if (l->u.offset > offset)
1487 *line_ptr = l->line_number + line_base - 1;
1493 cache_section = section;
1494 cache_offset = offset;
1496 cache_function = *functionname_ptr;
1502 DEFUN(coff_sizeof_headers,(abfd, reloc),
1508 if (reloc == false) {
1509 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1512 size = bfd_coff_filhsz (abfd);
1515 size += abfd->section_count * bfd_coff_scnhsz (abfd);