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. */
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
);
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
);
136 if (bfd_read((PTR
)external_sections
, 1, readsize
, abfd
) != readsize
) {
140 /* Now copy data as required; construct all asections etc */
143 for (i
= 0; i
< nscns
; i
++) {
144 struct internal_scnhdr tmp
;
145 bfd_coff_swap_scnhdr_in(abfd
, (PTR
) (external_sections
+ i
* scnhsz
),
147 make_a_section_from_file(abfd
,&tmp
, i
+1);
151 /* make_abs_section(abfd);*/
153 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
156 if (!(internal_f
->f_flags
& F_RELFLG
))
157 abfd
->flags
|= HAS_RELOC
;
158 if ((internal_f
->f_flags
& F_EXEC
))
159 abfd
->flags
|= EXEC_P
;
160 if (!(internal_f
->f_flags
& F_LNNO
))
161 abfd
->flags
|= HAS_LINENO
;
162 if (!(internal_f
->f_flags
& F_LSYMS
))
163 abfd
->flags
|= HAS_LOCALS
;
166 bfd_get_symcount(abfd
) = internal_f
->f_nsyms
;
167 if (internal_f
->f_nsyms
)
168 abfd
->flags
|= HAS_SYMS
;
170 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
171 bfd_get_start_address (abfd
) = internal_a
->entry
;
173 bfd_get_start_address (abfd
) = 0;
177 bfd_release(abfd
, tdata
);
178 return (bfd_target
*)NULL
;
181 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
182 not a COFF file. This is also used by ECOFF. */
185 DEFUN(coff_object_p
,(abfd
),
192 struct internal_filehdr internal_f
;
193 struct internal_aouthdr internal_a
;
195 bfd_error
= system_call_error
;
197 /* figure out how much to read */
198 filhsz
= bfd_coff_filhsz (abfd
);
199 aoutsz
= bfd_coff_aoutsz (abfd
);
201 filehdr
= bfd_alloc (abfd
, filhsz
);
204 if (bfd_read(filehdr
, 1, filhsz
, abfd
) != filhsz
)
206 bfd_coff_swap_filehdr_in(abfd
, filehdr
, &internal_f
);
207 bfd_release (abfd
, filehdr
);
209 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false) {
210 bfd_error
= wrong_format
;
213 nscns
=internal_f
.f_nscns
;
215 if (internal_f
.f_opthdr
) {
218 opthdr
= bfd_alloc (abfd
, aoutsz
);
221 if (bfd_read(opthdr
, 1,aoutsz
, abfd
) != aoutsz
) {
224 bfd_coff_swap_aouthdr_in(abfd
, opthdr
, (PTR
)&internal_a
);
227 /* Seek past the opt hdr stuff */
228 bfd_seek(abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
);
230 return coff_real_object_p(abfd
, nscns
, &internal_f
,
231 (internal_f
.f_opthdr
!= 0
233 : (struct internal_aouthdr
*) NULL
));
236 /* Get the BFD section from a COFF symbol section number. */
239 DEFUN(coff_section_from_bfd_index
,(abfd
, index
),
243 struct sec
*answer
= abfd
->sections
;
247 return &bfd_abs_section
;
249 if (index
== N_UNDEF
)
251 return &bfd_und_section
;
255 return &bfd_debug_section
;
260 if (answer
->target_index
== index
)
262 answer
= answer
->next
;
265 return &bfd_und_section
; /* For gcc -W and lint. Never executed. */
268 /* Get the upper bound of a COFF symbol table. */
271 coff_get_symtab_upper_bound(abfd
)
274 if (!bfd_coff_slurp_symbol_table(abfd
))
277 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
281 /* Canonicalize a COFF symbol table. */
284 DEFUN(coff_get_symtab
, (abfd
, alocation
),
288 unsigned int counter
= 0;
289 coff_symbol_type
*symbase
;
290 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
291 if (!bfd_coff_slurp_symbol_table(abfd
))
294 symbase
= obj_symbols(abfd
);
295 while (counter
< bfd_get_symcount(abfd
))
297 /* This nasty code looks at the symbol to decide whether or
298 not it is descibes a constructor/destructor entry point. It
299 is structured this way to (hopefully) speed non matches */
301 if (0 && symbase
->symbol
.name
[9] == '$')
303 bfd_constructor_entry(abfd
,
304 (asymbol
**)location
,
305 symbase
->symbol
.name
[10] == 'I' ?
309 *(location
++) = symbase
++;
313 return bfd_get_symcount(abfd
);
316 /* Set lineno_count for the output sections of a COFF file. */
319 DEFUN(coff_count_linenumbers
,(abfd
),
322 unsigned int limit
= bfd_get_symcount(abfd
);
327 asection
*s
= abfd
->sections
->output_section
;
329 BFD_ASSERT(s
->lineno_count
== 0);
335 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
336 asymbol
*q_maybe
= *p
;
337 if (bfd_asymbol_flavour(q_maybe
) == bfd_target_coff_flavour
) {
338 coff_symbol_type
*q
= coffsymbol(q_maybe
);
341 This symbol has a linenumber, increment the owning
342 section's linenumber count
344 alent
*l
= q
->lineno
;
345 q
->symbol
.section
->output_section
->lineno_count
++;
348 while (l
->line_number
) {
350 q
->symbol
.section
->output_section
->lineno_count
++;
359 /* Takes a bfd and a symbol, returns a pointer to the coff specific
360 area of the symbol if there is one. */
363 DEFUN(coff_symbol_from
,(ignore_abfd
, symbol
),
367 if (bfd_asymbol_flavour(symbol
) != bfd_target_coff_flavour
)
368 return (coff_symbol_type
*)NULL
;
370 if (bfd_asymbol_bfd(symbol
)->tdata
.coff_obj_data
== (coff_data_type
*)NULL
)
371 return (coff_symbol_type
*)NULL
;
373 return (coff_symbol_type
*) symbol
;
377 DEFUN(fixup_symbol_value
,(coff_symbol_ptr
, syment
),
378 coff_symbol_type
*coff_symbol_ptr AND
379 struct internal_syment
*syment
)
382 /* Normalize the symbol flags */
383 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
)) {
384 /* a common symbol is undefined with a value */
385 syment
->n_scnum
= N_UNDEF
;
386 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
388 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
389 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
391 else if (coff_symbol_ptr
->symbol
.section
== & bfd_und_section
) {
392 syment
->n_scnum
= N_UNDEF
;
396 if (coff_symbol_ptr
->symbol
.section
) {
398 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
401 coff_symbol_ptr
->symbol
.value
+
402 coff_symbol_ptr
->symbol
.section
->output_offset
+
403 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
407 /* This can happen, but I don't know why yet (steve@cygnus.com) */
408 syment
->n_scnum
= N_ABS
;
409 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
414 /* run through all the symbols in the symbol table and work out what
415 their indexes into the symbol table will be when output
417 Coff requires that each C_FILE symbol points to the next one in the
418 chain, and that the last one points to the first external symbol. We
423 DEFUN(coff_renumber_symbols
,(bfd_ptr
),
426 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
427 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
428 unsigned int native_index
= 0;
429 struct internal_syment
*last_file
= (struct internal_syment
*)NULL
;
430 unsigned int symbol_index
;
432 /* COFF demands that undefined symbols come after all other symbols.
433 Since we don't need to impose this extra knowledge on all our client
434 programs, deal with that here. Sort the symbol table; just move the
435 undefined symbols to the end, leaving the rest alone. */
436 /* @@ Do we have some condition we could test for, so we don't always
437 have to do this? I don't think relocatability is quite right, but
438 I'm not certain. [raeburn:19920508.1711EST] */
443 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
445 * (symbol_count
+ 1));
446 bfd_ptr
->outsymbols
= newsyms
;
447 for (i
= 0; i
< symbol_count
; i
++)
448 if (symbol_ptr_ptr
[i
]->section
!= &bfd_und_section
)
449 *newsyms
++ = symbol_ptr_ptr
[i
];
450 for (i
= 0; i
< symbol_count
; i
++)
451 if (symbol_ptr_ptr
[i
]->section
== &bfd_und_section
)
452 *newsyms
++ = symbol_ptr_ptr
[i
];
453 *newsyms
= (asymbol
*) NULL
;
454 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
457 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
459 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
460 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
461 combined_entry_type
*s
= coff_symbol_ptr
->native
;
464 if (s
->u
.syment
.n_sclass
== C_FILE
)
466 if (last_file
!= (struct internal_syment
*)NULL
) {
467 last_file
->n_value
= native_index
;
469 last_file
= &(s
->u
.syment
);
473 /* Modify the symbol values according to their section and
476 fixup_symbol_value(coff_symbol_ptr
, &(s
->u
.syment
));
478 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++) {
479 s
[i
].offset
= native_index
++;
486 obj_conv_table_size (bfd_ptr
) = native_index
;
490 Run thorough the symbol table again, and fix it so that all pointers to
491 entries are changed to the entries' index in the output symbol table.
495 DEFUN(coff_mangle_symbols
,(bfd_ptr
),
498 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
499 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
500 unsigned int symbol_index
;
502 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
504 coff_symbol_type
*coff_symbol_ptr
=
505 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
507 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
509 combined_entry_type
*s
= coff_symbol_ptr
->native
;
511 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++) {
512 combined_entry_type
*a
= s
+ i
+ 1;
514 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
515 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
519 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
520 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
530 static int string_size
;
533 DEFUN(coff_fix_symbol_name
,(abfd
, symbol
, native
),
536 combined_entry_type
*native
)
538 unsigned int name_length
;
539 union internal_auxent
*auxent
;
540 char * name
= ( char *)(symbol
->name
);
542 if (name
== (char *) NULL
) {
543 /* coff symbols always have names, so we'll make one up */
544 symbol
->name
= "strange";
545 name
= (char *)symbol
->name
;
547 name_length
= strlen(name
);
549 if (native
->u
.syment
.n_sclass
== C_FILE
) {
550 strncpy(native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
551 auxent
= &(native
+1)->u
.auxent
;
553 if (bfd_coff_long_filenames (abfd
)) {
554 if (name_length
<= FILNMLEN
) {
555 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
558 auxent
->x_file
.x_n
.x_offset
= string_size
+ 4;
559 auxent
->x_file
.x_n
.x_zeroes
= 0;
560 string_size
+= name_length
+ 1;
564 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
565 if (name_length
> FILNMLEN
) {
566 name
[FILNMLEN
] = '\0';
571 { /* NOT A C_FILE SYMBOL */
572 if (name_length
<= SYMNMLEN
) {
573 /* This name will fit into the symbol neatly */
574 strncpy(native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
577 native
->u
.syment
._n
._n_n
._n_offset
= string_size
+ 4;
578 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
579 string_size
+= name_length
+ 1;
584 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
587 DEFUN(coff_write_symbol
,(abfd
, symbol
, native
, written
),
590 combined_entry_type
*native AND
591 unsigned int written
)
593 unsigned int numaux
= native
->u
.syment
.n_numaux
;
594 int type
= native
->u
.syment
.n_type
;
595 int class = native
->u
.syment
.n_sclass
;
597 bfd_size_type symesz
;
599 /* @@ bfd_debug_section isn't accessible outside this file, but we know
600 that C_FILE symbols belong there. So move them. */
601 if (native
->u
.syment
.n_sclass
== C_FILE
)
602 symbol
->section
= &bfd_debug_section
;
604 if (symbol
->section
== &bfd_abs_section
)
606 native
->u
.syment
.n_scnum
= N_ABS
;
608 else if (symbol
->section
== &bfd_debug_section
)
610 native
->u
.syment
.n_scnum
= N_DEBUG
;
612 else if (symbol
->section
== &bfd_und_section
)
614 native
->u
.syment
.n_scnum
= N_UNDEF
;
618 native
->u
.syment
.n_scnum
=
619 symbol
->section
->output_section
->target_index
;
623 coff_fix_symbol_name(abfd
, symbol
, native
);
625 symesz
= bfd_coff_symesz (abfd
);
626 buf
= bfd_alloc (abfd
, symesz
);
627 bfd_coff_swap_sym_out(abfd
, &native
->u
.syment
, buf
);
628 bfd_write(buf
, 1, symesz
, abfd
);
629 bfd_release (abfd
, buf
);
631 if (native
->u
.syment
.n_numaux
> 0)
633 bfd_size_type auxesz
;
636 auxesz
= bfd_coff_auxesz (abfd
);
637 buf
= bfd_alloc (abfd
, auxesz
);
638 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
640 bfd_coff_swap_aux_out(abfd
,
641 &((native
+ j
+ 1)->u
.auxent
),
645 bfd_write(buf
, 1, auxesz
, abfd
);
647 bfd_release (abfd
, buf
);
650 Reuse somewhere in the symbol to keep the index
652 set_index(symbol
, written
);
653 return written
+ 1 + numaux
;
658 DEFUN(coff_write_alien_symbol
,(abfd
, symbol
, written
),
661 unsigned int written
)
664 This symbol has been created by the loader, or come from a non
665 coff format. It has no native element to inherit, make our
668 combined_entry_type
*native
;
669 combined_entry_type dummy
;
671 native
->u
.syment
.n_type
= T_NULL
;
672 native
->u
.syment
.n_flags
= 0;
673 if (symbol
->section
== &bfd_und_section
)
675 native
->u
.syment
.n_scnum
= N_UNDEF
;
676 native
->u
.syment
.n_value
= symbol
->value
;
678 else if (bfd_is_com_section (symbol
->section
))
680 native
->u
.syment
.n_scnum
= N_UNDEF
;
681 native
->u
.syment
.n_value
= symbol
->value
;
685 else if (symbol
->flags
& BSF_DEBUGGING
) {
687 remove name so it doesn't take up any space
692 native
->u
.syment
.n_scnum
= symbol
->section
->output_section
->target_index
;
693 native
->u
.syment
.n_value
= symbol
->value
+
694 symbol
->section
->output_section
->vma
+
695 symbol
->section
->output_offset
;
696 /* Copy the any flags from the the file hdr into the symbol */
698 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
699 if (c
!= (coff_symbol_type
*)NULL
) {
700 native
->u
.syment
.n_flags
= bfd_asymbol_bfd(&c
->symbol
)->flags
;
705 native
->u
.syment
.n_type
= 0;
706 if (symbol
->flags
& BSF_LOCAL
)
707 native
->u
.syment
.n_sclass
= C_STAT
;
709 native
->u
.syment
.n_sclass
= C_EXT
;
710 native
->u
.syment
.n_numaux
= 0;
712 return coff_write_symbol(abfd
, symbol
, native
, written
);
716 DEFUN(coff_write_native_symbol
,(abfd
, symbol
, written
),
718 coff_symbol_type
*symbol AND
719 unsigned int written
)
722 Does this symbol have an ascociated line number - if so then
723 make it remember this symbol index. Also tag the auxent of
724 this symbol to point to the right place in the lineno table
726 combined_entry_type
*native
= symbol
->native
;
728 alent
*lineno
= symbol
->lineno
;
730 if (lineno
&& !symbol
->done_lineno
) {
731 unsigned int count
= 0;
732 lineno
[count
].u
.offset
= written
;
733 if (native
->u
.syment
.n_numaux
) {
734 union internal_auxent
*a
= &((native
+1)->u
.auxent
);
736 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
737 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
740 And count and relocate all other linenumbers
744 while (lineno
[count
].line_number
) {
747 I've been told this, but still need proof:
748 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
749 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
750 > only specific to A29K implementations, but affects all systems using COFF
751 > format binaries. Note that in COFF object files, the line number core offsets
752 > output by the assembler are relative to the start of each procedure, not
753 > to the start of the .text section. This patch relocates the line numbers
754 > relative to the `native->u.syment.n_value' instead of the section virtual
755 > address. modular!olson@cs.arizona.edu (Jon Olson)
757 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
760 lineno
[count
].u
.offset
+=
761 symbol
->symbol
.section
->output_section
->vma
+
762 symbol
->symbol
.section
->output_offset
;
766 symbol
->done_lineno
= true;
768 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
769 count
* bfd_coff_linesz (abfd
);
771 return coff_write_symbol(abfd
, &( symbol
->symbol
), native
,written
);
775 DEFUN(coff_write_symbols
,(abfd
),
779 unsigned int limit
= bfd_get_symcount(abfd
);
780 unsigned int written
= 0;
787 /* Seek to the right place */
788 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
790 /* Output all the symbols we have */
793 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
795 asymbol
*symbol
= *p
;
796 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
798 if (c_symbol
== (coff_symbol_type
*) NULL
||
799 c_symbol
->native
== (combined_entry_type
*)NULL
)
801 written
= coff_write_alien_symbol(abfd
, symbol
, written
);
805 written
= coff_write_native_symbol(abfd
, c_symbol
, written
);
810 bfd_get_symcount(abfd
) = written
;
812 /* Now write out strings */
814 if (string_size
!= 0)
816 unsigned int size
= string_size
+ 4;
819 bfd_h_put_32(abfd
, size
, buffer
);
820 bfd_write((PTR
) buffer
, 1, sizeof(buffer
), abfd
);
821 for (p
= abfd
->outsymbols
, i
= 0;
826 size_t name_length
= strlen(q
->name
);
828 coff_symbol_type
* c_symbol
= coff_symbol_from(abfd
, q
);
829 maxlen
= ((c_symbol
!= NULL
&& c_symbol
->native
!= NULL
) &&
830 (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
)) ?
833 if (name_length
> maxlen
) {
834 bfd_write((PTR
) (q
->name
), 1, name_length
+ 1, abfd
);
839 /* We would normally not write anything here, but we'll write
840 out 4 so that any stupid coff reader which tries to read
841 the string table even when there isn't one won't croak. */
842 unsigned int size
= 4;
845 bfd_h_put_32 (abfd
, size
, buffer
);
846 bfd_write((PTR
) buffer
, 1, sizeof (buffer
), abfd
);
851 DEFUN(coff_write_linenumbers
,(abfd
),
855 bfd_size_type linesz
;
858 linesz
= bfd_coff_linesz (abfd
);
859 buff
= bfd_alloc (abfd
, linesz
);
860 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
861 if (s
->lineno_count
) {
862 asymbol
**q
= abfd
->outsymbols
;
863 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
864 /* Find all the linenumbers in this section */
867 if (p
->section
->output_section
== s
) {
869 BFD_SEND(bfd_asymbol_bfd(p
), _get_lineno
, (bfd_asymbol_bfd(p
), p
));
871 /* Found a linenumber entry, output */
872 struct internal_lineno out
;
873 memset( (PTR
)&out
, 0, sizeof(out
));
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
);
879 while (l
->line_number
) {
880 out
.l_lnno
= l
->line_number
;
881 out
.l_addr
.l_symndx
= l
->u
.offset
;
882 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
883 bfd_write(buff
, 1, linesz
, abfd
);
892 bfd_release (abfd
, buff
);
896 DEFUN(coff_get_lineno
,(ignore_abfd
, symbol
),
900 return coffsymbol(symbol
)->lineno
;
904 coff_section_symbol (abfd
, name
)
908 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
910 combined_entry_type
*csym
;
913 csym
= coff_symbol_from (abfd
, sym
)->native
;
914 /* Make sure back-end COFF stuff is there. */
918 coff_symbol_type sym
;
919 /* @@FIXME This shouldn't use a fixed size!! */
920 combined_entry_type e
[10];
923 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
924 memset ((char *) f
, 0, sizeof (*f
));
925 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
927 csym
[0].u
.syment
.n_sclass
= C_STAT
;
928 csym
[0].u
.syment
.n_numaux
= 1;
929 /* SF_SET_STATICS (sym); @@ ??? */
930 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
931 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
932 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
934 if (sec
->output_section
== NULL
)
936 sec
->output_section
= sec
;
937 sec
->output_offset
= 0;
943 /* This function transforms the offsets into the symbol table into
944 pointers to syments. */
947 DEFUN(coff_pointerize_aux
,(abfd
, table_base
, type
, class, auxent
),
949 combined_entry_type
*table_base AND
952 combined_entry_type
*auxent
)
954 /* Don't bother if this is a file or a section */
955 if (class == C_STAT
&& type
== T_NULL
) return;
956 if (class == C_FILE
) return;
958 /* Otherwise patch up */
959 #define N_TMASK coff_data (abfd)->local_n_tmask
960 #define N_BTSHFT coff_data (abfd)->local_n_btshft
961 if (ISFCN(type
) || ISTAG(class) || class == C_BLOCK
) {
962 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
= table_base
+
963 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
966 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
967 generate one, so we must be careful to ignore it. */
968 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0) {
969 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
970 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
976 DEFUN(build_string_table
,(abfd
),
979 char string_table_size_buffer
[4];
980 unsigned int string_table_size
;
983 /* At this point we should be "seek"'d to the end of the
984 symbols === the symbol table size. */
985 if (bfd_read((char *) string_table_size_buffer
,
986 sizeof(string_table_size_buffer
),
987 1, abfd
) != sizeof(string_table_size
)) {
988 bfd_error
= system_call_error
;
992 string_table_size
= bfd_h_get_32(abfd
, (bfd_byte
*) string_table_size_buffer
);
994 if ((string_table
= (PTR
) bfd_alloc(abfd
, string_table_size
-= 4)) == NULL
) {
995 bfd_error
= no_memory
;
997 } /* on mallocation error */
998 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
999 bfd_error
= system_call_error
;
1002 return string_table
;
1005 /* Allocate space for the ".debug" section, and read it.
1006 We did not read the debug section until now, because
1007 we didn't want to go to the trouble until someone needed it. */
1010 DEFUN(build_debug_section
,(abfd
),
1013 char *debug_section
;
1016 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1019 bfd_error
= no_debug_section
;
1023 debug_section
= (PTR
) bfd_alloc (abfd
,
1024 bfd_get_section_size_before_reloc (sect
));
1025 if (debug_section
== NULL
) {
1026 bfd_error
= no_memory
;
1030 /* Seek to the beginning of the `.debug' section and read it.
1031 Save the current position first; it is needed by our caller.
1032 Then read debug section and reset the file pointer. */
1034 position
= bfd_tell (abfd
);
1035 bfd_seek (abfd
, sect
->filepos
, SEEK_SET
);
1036 if (bfd_read (debug_section
,
1037 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1038 != bfd_get_section_size_before_reloc(sect
)) {
1039 bfd_error
= system_call_error
;
1042 bfd_seek (abfd
, position
, SEEK_SET
);
1043 return debug_section
;
1047 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1048 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1049 be \0-terminated. */
1051 DEFUN(copy_name
,(abfd
, name
, maxlen
),
1059 for (len
= 0; len
< maxlen
; ++len
) {
1060 if (name
[len
] == '\0') {
1065 if ((newname
= (PTR
) bfd_alloc(abfd
, len
+1)) == NULL
) {
1066 bfd_error
= no_memory
;
1069 strncpy(newname
, name
, len
);
1070 newname
[len
] = '\0';
1074 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1075 knit the symbol names into a normalized form. By normalized here I
1076 mean that all symbols have an n_offset pointer that points to a null-
1077 terminated string. */
1079 combined_entry_type
*
1080 DEFUN(coff_get_normalized_symtab
,(abfd
),
1083 combined_entry_type
*internal
;
1084 combined_entry_type
*internal_ptr
;
1085 combined_entry_type
*symbol_ptr
;
1086 combined_entry_type
*internal_end
;
1087 bfd_size_type symesz
;
1091 char *string_table
= NULL
;
1092 char *debug_section
= NULL
;
1095 unsigned int raw_size
;
1096 if (obj_raw_syments(abfd
) != (combined_entry_type
*)NULL
) {
1097 return obj_raw_syments(abfd
);
1099 if ((size
= bfd_get_symcount(abfd
) * sizeof(combined_entry_type
)) == 0) {
1100 bfd_error
= no_symbols
;
1104 internal
= (combined_entry_type
*)bfd_alloc(abfd
, size
);
1105 internal_end
= internal
+ bfd_get_symcount(abfd
);
1107 symesz
= bfd_coff_symesz (abfd
);
1108 raw_size
= bfd_get_symcount(abfd
) * symesz
;
1109 raw
= bfd_alloc(abfd
,raw_size
);
1111 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1112 || bfd_read(raw
, raw_size
, 1, abfd
) != raw_size
) {
1113 bfd_error
= system_call_error
;
1116 /* mark the end of the symbols */
1117 raw_end
= (char *) raw
+ bfd_get_symcount(abfd
) * symesz
;
1119 FIXME SOMEDAY. A string table size of zero is very weird, but
1120 probably possible. If one shows up, it will probably kill us.
1123 /* Swap all the raw entries */
1124 for (raw_src
= (char *) raw
, internal_ptr
= internal
;
1126 raw_src
+= symesz
, internal_ptr
++) {
1129 bfd_coff_swap_sym_in(abfd
, (PTR
)raw_src
, (PTR
)&internal_ptr
->u
.syment
);
1130 internal_ptr
->fix_tag
= 0;
1131 internal_ptr
->fix_end
= 0;
1132 symbol_ptr
= internal_ptr
;
1135 i
< symbol_ptr
->u
.syment
.n_numaux
;
1141 internal_ptr
->fix_tag
= 0;
1142 internal_ptr
->fix_end
= 0;
1143 bfd_coff_swap_aux_in(abfd
, (PTR
) raw_src
,
1144 symbol_ptr
->u
.syment
.n_type
,
1145 symbol_ptr
->u
.syment
.n_sclass
,
1146 &(internal_ptr
->u
.auxent
));
1147 /* Remember that bal entries arn't pointerized */
1148 if (i
!= 1 || symbol_ptr
->u
.syment
.n_sclass
!= C_LEAFPROC
)
1151 coff_pointerize_aux(abfd
,
1153 symbol_ptr
->u
.syment
.n_type
,
1154 symbol_ptr
->u
.syment
.n_sclass
,
1161 /* Free all the raw stuff */
1162 bfd_release(abfd
, raw
);
1164 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1167 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
) {
1168 /* make a file symbol point to the name in the auxent, since
1169 the text ".file" is redundant */
1170 if ((internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0) {
1171 /* the filename is a long one, point into the string table */
1172 if (string_table
== NULL
) {
1173 string_table
= build_string_table(abfd
);
1176 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1177 (long) (string_table
- 4 +
1178 (internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_offset
);
1181 /* ordinary short filename, put into memory anyway */
1182 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1183 copy_name(abfd
, (internal_ptr
+1)->u
.auxent
.x_file
.x_fname
,
1188 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0) {
1189 /* This is a "short" name. Make it long. */
1190 unsigned long i
= 0;
1191 char *newstring
= NULL
;
1193 /* find the length of this string without walking into memory
1195 for (i
= 0; i
< 8; ++i
) {
1196 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0') {
1198 } /* if end of string */
1199 } /* possible lengths of this string. */
1201 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1202 bfd_error
= no_memory
;
1205 memset(newstring
, 0, i
);
1206 strncpy(newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
-1);
1207 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1208 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1210 else if (!bfd_coff_symname_in_debug(abfd
, &internal_ptr
->u
.syment
)) {
1211 /* Long name already. Point symbol at the string in the table. */
1212 if (string_table
== NULL
) {
1213 string_table
= build_string_table(abfd
);
1215 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1216 (string_table
- 4 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1219 /* Long name in debug section. Very similar. */
1220 if (debug_section
== NULL
) {
1221 debug_section
= build_debug_section(abfd
);
1223 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1224 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1227 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1230 obj_raw_syments(abfd
) = internal
;
1231 obj_raw_syment_count(abfd
) = internal_ptr
- internal
;
1234 } /* coff_get_normalized_symtab() */
1237 DEFUN (coff_get_reloc_upper_bound
, (abfd
, asect
),
1241 if (bfd_get_format(abfd
) != bfd_object
) {
1242 bfd_error
= invalid_operation
;
1245 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
1249 DEFUN (coff_make_empty_symbol
, (abfd
),
1252 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1254 bfd_error
= no_memory
;
1257 new->symbol
.section
= 0;
1259 new->lineno
= (alent
*) NULL
;
1260 new->done_lineno
= false;
1261 new->symbol
.the_bfd
= abfd
;
1262 return &new->symbol
;
1265 /* Make a debugging symbol. */
1268 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1273 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1275 bfd_error
= no_memory
;
1278 /* @@ This shouldn't be using a constant multiplier. */
1279 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1280 new->symbol
.section
= &bfd_debug_section
;
1281 new->lineno
= (alent
*) NULL
;
1282 new->done_lineno
= false;
1283 new->symbol
.the_bfd
= abfd
;
1284 return &new->symbol
;
1288 coff_get_symbol_info (abfd
, symbol
, ret
)
1293 bfd_symbol_info (symbol
, ret
);
1296 /* Print out information about COFF symbol. */
1299 coff_print_symbol (abfd
, filep
, symbol
, how
)
1303 bfd_print_symbol_type how
;
1305 FILE *file
= (FILE *) filep
;
1309 case bfd_print_symbol_name
:
1310 fprintf (file
, "%s", symbol
->name
);
1313 case bfd_print_symbol_more
:
1314 fprintf (file
, "coff %s %s",
1315 coffsymbol(symbol
)->native
? "n" : "g",
1316 coffsymbol(symbol
)->lineno
? "l" : " ");
1319 case bfd_print_symbol_all
:
1320 if (coffsymbol(symbol
)->native
)
1323 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1324 combined_entry_type
*root
= obj_raw_syments (abfd
);
1325 struct lineno_cache_entry
*l
= coffsymbol(symbol
)->lineno
;
1327 fprintf (file
,"[%3d]", combined
- root
);
1330 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
1331 combined
->u
.syment
.n_scnum
,
1332 combined
->u
.syment
.n_flags
,
1333 combined
->u
.syment
.n_type
,
1334 combined
->u
.syment
.n_sclass
,
1335 combined
->u
.syment
.n_numaux
,
1336 (unsigned long) combined
->u
.syment
.n_value
,
1339 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1341 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1345 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1347 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1349 fprintf (file
, "\n");
1350 switch (combined
->u
.syment
.n_sclass
)
1353 fprintf (file
, "File ");
1357 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1358 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1359 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1367 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1369 while (l
->line_number
)
1371 fprintf (file
, "\n%4d : 0x%lx",
1374 (l
->u
.offset
+ symbol
->section
->vma
)));
1381 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1382 fprintf (file
, " %-5s %s %s %s",
1383 symbol
->section
->name
,
1384 coffsymbol(symbol
)->native
? "n" : "g",
1385 coffsymbol(symbol
)->lineno
? "l" : " ",
1391 /* Provided a BFD, a section and an offset into the section, calculate
1392 and return the name of the source file and the line nearest to the
1396 DEFUN(coff_find_nearest_line
,(abfd
,
1404 asection
*section AND
1405 asymbol
**ignore_symbols AND
1407 CONST
char **filename_ptr AND
1408 CONST
char **functionname_ptr AND
1409 unsigned int *line_ptr
)
1411 static bfd
*cache_abfd
;
1412 static asection
*cache_section
;
1413 static bfd_vma cache_offset
;
1414 static unsigned int cache_i
;
1415 static CONST
char *cache_function
;
1416 static unsigned int line_base
= 0;
1419 coff_data_type
*cof
= coff_data(abfd
);
1420 /* Run through the raw syments if available */
1421 combined_entry_type
*p
;
1426 *functionname_ptr
= 0;
1429 /* Don't try and find line numbers in a non coff file */
1430 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1436 p
= cof
->raw_syments
;
1438 for (i
= 0; i
< cof
->raw_syment_count
; i
++) {
1439 if (p
->u
.syment
.n_sclass
== C_FILE
) {
1440 /* File name has been moved into symbol */
1441 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1444 p
+= 1 + p
->u
.syment
.n_numaux
;
1446 /* Now wander though the raw linenumbers of the section */
1448 If this is the same BFD as we were previously called with and this is
1449 the same section, and the offset we want is further down then we can
1450 prime the lookup loop
1452 if (abfd
== cache_abfd
&&
1453 section
== cache_section
&&
1454 offset
>= cache_offset
) {
1456 *functionname_ptr
= cache_function
;
1461 l
= §ion
->lineno
[i
];
1463 for (; i
< section
->lineno_count
; i
++) {
1464 if (l
->line_number
== 0) {
1465 /* Get the symbol this line number points at */
1466 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
1467 if (coff
->symbol
.value
> offset
)
1469 *functionname_ptr
= coff
->symbol
.name
;
1471 combined_entry_type
*s
= coff
->native
;
1472 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1474 S should now point to the .bf of the function
1476 if (s
->u
.syment
.n_numaux
) {
1478 The linenumber is stored in the auxent
1480 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1481 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1482 *line_ptr
= line_base
;
1487 if (l
->u
.offset
> offset
)
1489 *line_ptr
= l
->line_number
+ line_base
- 1;
1495 cache_section
= section
;
1496 cache_offset
= offset
;
1498 cache_function
= *functionname_ptr
;
1504 DEFUN(coff_sizeof_headers
,(abfd
, reloc
),
1510 if (reloc
== false) {
1511 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1514 size
= bfd_coff_filhsz (abfd
);
1517 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);