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"
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 if (hdr
->s_nreloc
!= 0)
93 return_section
->flags
|= SEC_RELOC
;
94 /* FIXME: should this check 'hdr->s_size > 0' */
95 if (hdr
->s_scnptr
!= 0)
96 return_section
->flags
|= SEC_HAS_CONTENTS
;
100 /* Read in a COFF object and make it into a BFD. This is used by
105 DEFUN(coff_real_object_p
,(abfd
, nscns
, internal_f
, internal_a
),
108 struct internal_filehdr
*internal_f AND
109 struct internal_aouthdr
*internal_a
)
112 size_t readsize
; /* length of file_info */
114 char *external_sections
;
116 /* Build a play area */
117 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
);
121 scnhsz
= bfd_coff_scnhsz (abfd
);
122 readsize
= nscns
* scnhsz
;
123 external_sections
= (char *)bfd_alloc(abfd
, readsize
);
125 if (bfd_read((PTR
)external_sections
, 1, readsize
, abfd
) != readsize
) {
129 /* Now copy data as required; construct all asections etc */
132 for (i
= 0; i
< nscns
; i
++) {
133 struct internal_scnhdr tmp
;
134 bfd_coff_swap_scnhdr_in(abfd
, (PTR
) (external_sections
+ i
* scnhsz
),
136 make_a_section_from_file(abfd
,&tmp
, i
+1);
140 /* make_abs_section(abfd);*/
142 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
145 if (!(internal_f
->f_flags
& F_RELFLG
))
146 abfd
->flags
|= HAS_RELOC
;
147 if ((internal_f
->f_flags
& F_EXEC
))
148 abfd
->flags
|= EXEC_P
;
149 if (!(internal_f
->f_flags
& F_LNNO
))
150 abfd
->flags
|= HAS_LINENO
;
151 if (!(internal_f
->f_flags
& F_LSYMS
))
152 abfd
->flags
|= HAS_LOCALS
;
155 bfd_get_symcount(abfd
) = internal_f
->f_nsyms
;
156 if (internal_f
->f_nsyms
)
157 abfd
->flags
|= HAS_SYMS
;
159 bfd_get_start_address(abfd
) = internal_f
->f_opthdr
? internal_a
->entry
: 0;
163 bfd_release(abfd
, tdata
);
164 return (bfd_target
*)NULL
;
167 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
168 not a COFF file. This is also used by ECOFF. */
171 DEFUN(coff_object_p
,(abfd
),
178 struct internal_filehdr internal_f
;
179 struct internal_aouthdr internal_a
;
181 bfd_error
= system_call_error
;
183 /* figure out how much to read */
184 filhsz
= bfd_coff_filhsz (abfd
);
185 aoutsz
= bfd_coff_aoutsz (abfd
);
187 filehdr
= bfd_alloc (abfd
, filhsz
);
190 if (bfd_read(filehdr
, 1, filhsz
, abfd
) != filhsz
)
192 bfd_coff_swap_filehdr_in(abfd
, filehdr
, &internal_f
);
193 bfd_release (abfd
, filehdr
);
195 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false) {
196 bfd_error
= wrong_format
;
199 nscns
=internal_f
.f_nscns
;
201 if (internal_f
.f_opthdr
) {
204 opthdr
= bfd_alloc (abfd
, aoutsz
);
207 if (bfd_read(opthdr
, 1,aoutsz
, abfd
) != aoutsz
) {
210 bfd_coff_swap_aouthdr_in(abfd
, opthdr
, (PTR
)&internal_a
);
213 /* Seek past the opt hdr stuff */
214 bfd_seek(abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
);
216 return coff_real_object_p(abfd
, nscns
, &internal_f
, &internal_a
);
219 /* Get the BFD section from a COFF symbol section number. */
222 DEFUN(coff_section_from_bfd_index
,(abfd
, index
),
226 struct sec
*answer
= abfd
->sections
;
230 return &bfd_abs_section
;
232 if (index
== N_UNDEF
)
234 return &bfd_und_section
;
238 return &bfd_debug_section
;
243 if (answer
->target_index
== index
)
245 answer
= answer
->next
;
248 return &bfd_und_section
; /* For gcc -W and lint. Never executed. */
251 /* Get the upper bound of a COFF symbol table. */
254 coff_get_symtab_upper_bound(abfd
)
257 if (!bfd_coff_slurp_symbol_table(abfd
))
260 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
264 /* Canonicalize a COFF symbol table. */
267 DEFUN(coff_get_symtab
, (abfd
, alocation
),
271 unsigned int counter
= 0;
272 coff_symbol_type
*symbase
;
273 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
274 if (!bfd_coff_slurp_symbol_table(abfd
))
277 symbase
= obj_symbols(abfd
);
278 while (counter
< bfd_get_symcount(abfd
))
280 /* This nasty code looks at the symbol to decide whether or
281 not it is descibes a constructor/destructor entry point. It
282 is structured this way to (hopefully) speed non matches */
284 if (0 && symbase
->symbol
.name
[9] == '$')
286 bfd_constructor_entry(abfd
,
287 (asymbol
**)location
,
288 symbase
->symbol
.name
[10] == 'I' ?
292 *(location
++) = symbase
++;
296 return bfd_get_symcount(abfd
);
299 /* Set lineno_count for the output sections of a COFF file. */
302 DEFUN(coff_count_linenumbers
,(abfd
),
305 unsigned int limit
= bfd_get_symcount(abfd
);
309 asection
*s
= abfd
->sections
->output_section
;
311 BFD_ASSERT(s
->lineno_count
== 0);
317 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
318 asymbol
*q_maybe
= *p
;
319 if (bfd_asymbol_flavour(q_maybe
) == bfd_target_coff_flavour
) {
320 coff_symbol_type
*q
= coffsymbol(q_maybe
);
323 This symbol has a linenumber, increment the owning
324 section's linenumber count
326 alent
*l
= q
->lineno
;
327 q
->symbol
.section
->output_section
->lineno_count
++;
329 while (l
->line_number
) {
330 q
->symbol
.section
->output_section
->lineno_count
++;
338 /* Takes a bfd and a symbol, returns a pointer to the coff specific
339 area of the symbol if there is one. */
342 DEFUN(coff_symbol_from
,(ignore_abfd
, symbol
),
346 if (bfd_asymbol_flavour(symbol
) != bfd_target_coff_flavour
)
347 return (coff_symbol_type
*)NULL
;
349 if (bfd_asymbol_bfd(symbol
)->tdata
.coff_obj_data
== (coff_data_type
*)NULL
)
350 return (coff_symbol_type
*)NULL
;
352 return (coff_symbol_type
*) symbol
;
356 DEFUN(fixup_symbol_value
,(coff_symbol_ptr
, syment
),
357 coff_symbol_type
*coff_symbol_ptr AND
358 struct internal_syment
*syment
)
361 /* Normalize the symbol flags */
362 if (coff_symbol_ptr
->symbol
.section
== &bfd_com_section
) {
363 /* a common symbol is undefined with a value */
364 syment
->n_scnum
= N_UNDEF
;
365 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
367 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
368 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
370 else if (coff_symbol_ptr
->symbol
.section
== & bfd_und_section
) {
371 syment
->n_scnum
= N_UNDEF
;
375 if (coff_symbol_ptr
->symbol
.section
) {
377 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
380 coff_symbol_ptr
->symbol
.value
+
381 coff_symbol_ptr
->symbol
.section
->output_offset
+
382 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
386 /* This can happen, but I don't know why yet (steve@cygnus.com) */
387 syment
->n_scnum
= N_ABS
;
388 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
393 /* run through all the symbols in the symbol table and work out what
394 their indexes into the symbol table will be when output
396 Coff requires that each C_FILE symbol points to the next one in the
397 chain, and that the last one points to the first external symbol. We
402 DEFUN(coff_renumber_symbols
,(bfd_ptr
),
405 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
406 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
407 unsigned int native_index
= 0;
408 struct internal_syment
*last_file
= (struct internal_syment
*)NULL
;
409 unsigned int symbol_index
;
411 /* COFF demands that undefined symbols come after all other symbols.
412 Since we don't need to impose this extra knowledge on all our client
413 programs, deal with that here. Sort the symbol table; just move the
414 undefined symbols to the end, leaving the rest alone. */
415 /* @@ Do we have some condition we could test for, so we don't always
416 have to do this? I don't think relocatability is quite right, but
417 I'm not certain. [raeburn:19920508.1711EST] */
422 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
424 * (symbol_count
+ 1));
425 bfd_ptr
->outsymbols
= newsyms
;
426 for (i
= 0; i
< symbol_count
; i
++)
427 if (symbol_ptr_ptr
[i
]->section
!= &bfd_und_section
)
428 *newsyms
++ = symbol_ptr_ptr
[i
];
429 for (i
= 0; i
< symbol_count
; i
++)
430 if (symbol_ptr_ptr
[i
]->section
== &bfd_und_section
)
431 *newsyms
++ = symbol_ptr_ptr
[i
];
432 *newsyms
= (asymbol
*) NULL
;
433 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
436 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
438 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
439 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
440 combined_entry_type
*s
= coff_symbol_ptr
->native
;
443 if (s
->u
.syment
.n_sclass
== C_FILE
)
445 if (last_file
!= (struct internal_syment
*)NULL
) {
446 last_file
->n_value
= native_index
;
448 last_file
= &(s
->u
.syment
);
452 /* Modify the symbol values according to their section and
455 fixup_symbol_value(coff_symbol_ptr
, &(s
->u
.syment
));
457 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++) {
458 s
[i
].offset
= native_index
++;
465 obj_conv_table_size (bfd_ptr
) = native_index
;
469 Run thorough the symbol table again, and fix it so that all pointers to
470 entries are changed to the entries' index in the output symbol table.
474 DEFUN(coff_mangle_symbols
,(bfd_ptr
),
477 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
478 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
479 unsigned int symbol_index
;
481 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
483 coff_symbol_type
*coff_symbol_ptr
=
484 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
486 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
488 combined_entry_type
*s
= coff_symbol_ptr
->native
;
490 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++) {
491 combined_entry_type
*a
= s
+ i
+ 1;
493 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
494 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
498 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
499 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
509 static int string_size
;
512 DEFUN(coff_fix_symbol_name
,(abfd
, symbol
, native
),
515 combined_entry_type
*native
)
517 unsigned int name_length
;
518 union internal_auxent
*auxent
;
519 char * name
= ( char *)(symbol
->name
);
521 if (name
== (char *) NULL
) {
522 /* coff symbols always have names, so we'll make one up */
523 symbol
->name
= "strange";
524 name
= (char *)symbol
->name
;
526 name_length
= strlen(name
);
528 if (native
->u
.syment
.n_sclass
== C_FILE
) {
529 strncpy(native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
530 auxent
= &(native
+1)->u
.auxent
;
532 if (bfd_coff_long_filenames (abfd
)) {
533 if (name_length
<= FILNMLEN
) {
534 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
537 auxent
->x_file
.x_n
.x_offset
= string_size
+ 4;
538 auxent
->x_file
.x_n
.x_zeroes
= 0;
539 string_size
+= name_length
+ 1;
543 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
544 if (name_length
> FILNMLEN
) {
545 name
[FILNMLEN
] = '\0';
550 { /* NOT A C_FILE SYMBOL */
551 if (name_length
<= SYMNMLEN
) {
552 /* This name will fit into the symbol neatly */
553 strncpy(native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
556 native
->u
.syment
._n
._n_n
._n_offset
= string_size
+ 4;
557 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
558 string_size
+= name_length
+ 1;
563 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
566 DEFUN(coff_write_symbol
,(abfd
, symbol
, native
, written
),
569 combined_entry_type
*native AND
570 unsigned int written
)
572 unsigned int numaux
= native
->u
.syment
.n_numaux
;
573 int type
= native
->u
.syment
.n_type
;
574 int class = native
->u
.syment
.n_sclass
;
576 bfd_size_type symesz
;
578 /* @@ bfd_debug_section isn't accessible outside this file, but we know
579 that C_FILE symbols belong there. So move them. */
580 if (native
->u
.syment
.n_sclass
== C_FILE
)
581 symbol
->section
= &bfd_debug_section
;
583 if (symbol
->section
== &bfd_abs_section
)
585 native
->u
.syment
.n_scnum
= N_ABS
;
587 else if (symbol
->section
== &bfd_debug_section
)
589 native
->u
.syment
.n_scnum
= N_DEBUG
;
591 else if (symbol
->section
== &bfd_und_section
)
593 native
->u
.syment
.n_scnum
= N_UNDEF
;
597 native
->u
.syment
.n_scnum
=
598 symbol
->section
->output_section
->target_index
;
602 coff_fix_symbol_name(abfd
, symbol
, native
);
604 symesz
= bfd_coff_symesz (abfd
);
605 buf
= bfd_alloc (abfd
, symesz
);
606 bfd_coff_swap_sym_out(abfd
, &native
->u
.syment
, buf
);
607 bfd_write(buf
, 1, symesz
, abfd
);
608 bfd_release (abfd
, buf
);
610 if (native
->u
.syment
.n_numaux
> 0)
612 bfd_size_type auxesz
;
615 auxesz
= bfd_coff_auxesz (abfd
);
616 buf
= bfd_alloc (abfd
, auxesz
);
617 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
619 bfd_coff_swap_aux_out(abfd
,
620 &((native
+ j
+ 1)->u
.auxent
),
624 bfd_write(buf
, 1, auxesz
, abfd
);
626 bfd_release (abfd
, buf
);
629 Reuse somewhere in the symbol to keep the index
631 set_index(symbol
, written
);
632 return written
+ 1 + numaux
;
637 DEFUN(coff_write_alien_symbol
,(abfd
, symbol
, written
),
640 unsigned int written
)
643 This symbol has been created by the loader, or come from a non
644 coff format. It has no native element to inherit, make our
647 combined_entry_type
*native
;
648 combined_entry_type dummy
;
650 native
->u
.syment
.n_type
= T_NULL
;
651 native
->u
.syment
.n_flags
= 0;
652 if (symbol
->section
== &bfd_und_section
)
654 native
->u
.syment
.n_scnum
= N_UNDEF
;
655 native
->u
.syment
.n_value
= symbol
->value
;
657 else if (symbol
->section
== &bfd_com_section
)
659 native
->u
.syment
.n_scnum
= N_UNDEF
;
660 native
->u
.syment
.n_value
= symbol
->value
;
664 else if (symbol
->flags
& BSF_DEBUGGING
) {
666 remove name so it doesn't take up any space
671 native
->u
.syment
.n_scnum
= symbol
->section
->output_section
->target_index
;
672 native
->u
.syment
.n_value
= symbol
->value
+
673 symbol
->section
->output_section
->vma
+
674 symbol
->section
->output_offset
;
675 /* Copy the any flags from the the file hdr into the symbol */
677 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
678 if (c
!= (coff_symbol_type
*)NULL
) {
679 native
->u
.syment
.n_flags
= bfd_asymbol_bfd(&c
->symbol
)->flags
;
684 native
->u
.syment
.n_type
= 0;
685 if (symbol
->flags
& BSF_LOCAL
)
686 native
->u
.syment
.n_sclass
= C_STAT
;
688 native
->u
.syment
.n_sclass
= C_EXT
;
689 native
->u
.syment
.n_numaux
= 0;
691 return coff_write_symbol(abfd
, symbol
, native
, written
);
695 DEFUN(coff_write_native_symbol
,(abfd
, symbol
, written
),
697 coff_symbol_type
*symbol AND
698 unsigned int written
)
701 Does this symbol have an ascociated line number - if so then
702 make it remember this symbol index. Also tag the auxent of
703 this symbol to point to the right place in the lineno table
705 combined_entry_type
*native
= symbol
->native
;
707 alent
*lineno
= symbol
->lineno
;
709 if (lineno
&& !symbol
->done_lineno
) {
710 unsigned int count
= 0;
711 lineno
[count
].u
.offset
= written
;
712 if (native
->u
.syment
.n_numaux
) {
713 union internal_auxent
*a
= &((native
+1)->u
.auxent
);
715 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
716 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
719 And count and relocate all other linenumbers
723 while (lineno
[count
].line_number
) {
726 I've been told this, but still need proof:
727 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
728 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
729 > only specific to A29K implementations, but affects all systems using COFF
730 > format binaries. Note that in COFF object files, the line number core offsets
731 > output by the assembler are relative to the start of each procedure, not
732 > to the start of the .text section. This patch relocates the line numbers
733 > relative to the `native->u.syment.n_value' instead of the section virtual
734 > address. modular!olson@cs.arizona.edu (Jon Olson)
736 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
739 lineno
[count
].u
.offset
+=
740 symbol
->symbol
.section
->output_section
->vma
+
741 symbol
->symbol
.section
->output_offset
;
745 symbol
->done_lineno
= true;
747 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
748 count
* bfd_coff_linesz (abfd
);
750 return coff_write_symbol(abfd
, &( symbol
->symbol
), native
,written
);
754 DEFUN(coff_write_symbols
,(abfd
),
758 unsigned int limit
= bfd_get_symcount(abfd
);
759 unsigned int written
= 0;
766 /* Seek to the right place */
767 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
769 /* Output all the symbols we have */
772 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
774 asymbol
*symbol
= *p
;
775 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
777 if (c_symbol
== (coff_symbol_type
*) NULL
||
778 c_symbol
->native
== (combined_entry_type
*)NULL
)
780 written
= coff_write_alien_symbol(abfd
, symbol
, written
);
784 written
= coff_write_native_symbol(abfd
, c_symbol
, written
);
789 bfd_get_symcount(abfd
) = written
;
791 /* Now write out strings */
793 if (string_size
!= 0)
795 unsigned int size
= string_size
+ 4;
798 bfd_h_put_32(abfd
, size
, buffer
);
799 bfd_write((PTR
) buffer
, 1, sizeof(buffer
), abfd
);
800 for (p
= abfd
->outsymbols
, i
= 0;
805 size_t name_length
= strlen(q
->name
);
807 coff_symbol_type
* c_symbol
= coff_symbol_from(abfd
, q
);
808 maxlen
= ((c_symbol
!= NULL
&& c_symbol
->native
!= NULL
) &&
809 (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
)) ?
812 if (name_length
> maxlen
) {
813 bfd_write((PTR
) (q
->name
), 1, name_length
+ 1, abfd
);
818 /* We would normally not write anything here, but we'll write
819 out 4 so that any stupid coff reader which tries to read
820 the string table even when there isn't one won't croak.
823 uint32e_type size
= 4;
825 bfd_write((PTR
)&size
, 1, sizeof(size
), abfd
);
831 DEFUN(coff_write_linenumbers
,(abfd
),
835 bfd_size_type linesz
;
838 linesz
= bfd_coff_linesz (abfd
);
839 buff
= bfd_alloc (abfd
, linesz
);
840 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
841 if (s
->lineno_count
) {
842 asymbol
**q
= abfd
->outsymbols
;
843 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
844 /* Find all the linenumbers in this section */
848 BFD_SEND(bfd_asymbol_bfd(p
), _get_lineno
, (bfd_asymbol_bfd(p
), p
));
850 /* Found a linenumber entry, output */
851 struct internal_lineno out
;
852 memset( (PTR
)&out
, 0, sizeof(out
));
854 out
.l_addr
.l_symndx
= l
->u
.offset
;
855 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
856 bfd_write(buff
, 1, linesz
, abfd
);
858 while (l
->line_number
) {
859 out
.l_lnno
= l
->line_number
;
860 out
.l_addr
.l_symndx
= l
->u
.offset
;
861 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
862 bfd_write(buff
, 1, linesz
, abfd
);
870 bfd_release (abfd
, buff
);
874 DEFUN(coff_get_lineno
,(ignore_abfd
, symbol
),
878 return coffsymbol(symbol
)->lineno
;
882 coff_section_symbol (abfd
, name
)
886 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
888 combined_entry_type
*csym
;
891 if (coff_symbol_from (abfd
, sym
))
892 csym
= coff_symbol_from (abfd
, sym
)->native
;
895 /* Make sure back-end COFF stuff is there. */
899 coff_symbol_type sym
;
900 /* @@FIXME This shouldn't use a fixed size!! */
901 combined_entry_type e
[10];
904 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
905 memset ((char *) f
, 0, sizeof (*f
));
906 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
908 csym
[0].u
.syment
.n_sclass
= C_STAT
;
909 csym
[0].u
.syment
.n_numaux
= 1;
910 /* SF_SET_STATICS (sym); @@ ??? */
913 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
914 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
915 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
919 csym
[1].u
.auxent
.x_scn
.x_scnlen
= 0;
920 csym
[1].u
.auxent
.x_scn
.x_nreloc
= 0;
921 csym
[1].u
.auxent
.x_scn
.x_nlinno
= 0;
926 /* This function transforms the offsets into the symbol table into
927 pointers to syments. */
930 DEFUN(coff_pointerize_aux
,(abfd
, table_base
, type
, class, auxent
),
932 combined_entry_type
*table_base AND
935 combined_entry_type
*auxent
)
937 /* Don't bother if this is a file or a section */
938 if (class == C_STAT
&& type
== T_NULL
) return;
939 if (class == C_FILE
) return;
941 /* Otherwise patch up */
942 #define N_TMASK coff_data (abfd)->local_n_tmask
943 #define N_BTSHFT coff_data (abfd)->local_n_btshft
944 if (ISFCN(type
) || ISTAG(class) || class == C_BLOCK
) {
945 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
= table_base
+
946 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
949 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
950 generate one, so we must be careful to ignore it. */
951 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0) {
952 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
953 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
959 DEFUN(build_string_table
,(abfd
),
962 char string_table_size_buffer
[4];
963 unsigned int string_table_size
;
966 /* At this point we should be "seek"'d to the end of the
967 symbols === the symbol table size. */
968 if (bfd_read((char *) string_table_size_buffer
,
969 sizeof(string_table_size_buffer
),
970 1, abfd
) != sizeof(string_table_size
)) {
971 bfd_error
= system_call_error
;
975 string_table_size
= bfd_h_get_32(abfd
, (bfd_byte
*) string_table_size_buffer
);
977 if ((string_table
= (PTR
) bfd_alloc(abfd
, string_table_size
-= 4)) == NULL
) {
978 bfd_error
= no_memory
;
980 } /* on mallocation error */
981 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
982 bfd_error
= system_call_error
;
988 /* Allocate space for the ".debug" section, and read it.
989 We did not read the debug section until now, because
990 we didn't want to go to the trouble until someone needed it. */
993 DEFUN(build_debug_section
,(abfd
),
999 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1002 bfd_error
= no_debug_section
;
1006 debug_section
= (PTR
) bfd_alloc (abfd
,
1007 bfd_get_section_size_before_reloc (sect
));
1008 if (debug_section
== NULL
) {
1009 bfd_error
= no_memory
;
1013 /* Seek to the beginning of the `.debug' section and read it.
1014 Save the current position first; it is needed by our caller.
1015 Then read debug section and reset the file pointer. */
1017 position
= bfd_tell (abfd
);
1018 bfd_seek (abfd
, sect
->filepos
, SEEK_SET
);
1019 if (bfd_read (debug_section
,
1020 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1021 != bfd_get_section_size_before_reloc(sect
)) {
1022 bfd_error
= system_call_error
;
1025 bfd_seek (abfd
, position
, SEEK_SET
);
1026 return debug_section
;
1030 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1031 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1032 be \0-terminated. */
1034 DEFUN(copy_name
,(abfd
, name
, maxlen
),
1042 for (len
= 0; len
< maxlen
; ++len
) {
1043 if (name
[len
] == '\0') {
1048 if ((newname
= (PTR
) bfd_alloc(abfd
, len
+1)) == NULL
) {
1049 bfd_error
= no_memory
;
1052 strncpy(newname
, name
, len
);
1053 newname
[len
] = '\0';
1057 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1058 knit the symbol names into a normalized form. By normalized here I
1059 mean that all symbols have an n_offset pointer that points to a null-
1060 terminated string. */
1062 combined_entry_type
*
1063 DEFUN(coff_get_normalized_symtab
,(abfd
),
1066 combined_entry_type
*internal
;
1067 combined_entry_type
*internal_ptr
;
1068 combined_entry_type
*symbol_ptr
;
1069 combined_entry_type
*internal_end
;
1070 bfd_size_type symesz
;
1074 char *string_table
= NULL
;
1075 char *debug_section
= NULL
;
1078 unsigned int raw_size
;
1079 if (obj_raw_syments(abfd
) != (combined_entry_type
*)NULL
) {
1080 return obj_raw_syments(abfd
);
1082 if ((size
= bfd_get_symcount(abfd
) * sizeof(combined_entry_type
)) == 0) {
1083 bfd_error
= no_symbols
;
1087 internal
= (combined_entry_type
*)bfd_alloc(abfd
, size
);
1088 internal_end
= internal
+ bfd_get_symcount(abfd
);
1090 symesz
= bfd_coff_symesz (abfd
);
1091 raw_size
= bfd_get_symcount(abfd
) * symesz
;
1092 raw
= bfd_alloc(abfd
,raw_size
);
1094 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1095 || bfd_read(raw
, raw_size
, 1, abfd
) != raw_size
) {
1096 bfd_error
= system_call_error
;
1099 /* mark the end of the symbols */
1100 raw_end
= (char *) raw
+ bfd_get_symcount(abfd
) * symesz
;
1102 FIXME SOMEDAY. A string table size of zero is very weird, but
1103 probably possible. If one shows up, it will probably kill us.
1106 /* Swap all the raw entries */
1107 for (raw_src
= (char *) raw
, internal_ptr
= internal
;
1109 raw_src
+= symesz
, internal_ptr
++) {
1112 bfd_coff_swap_sym_in(abfd
, (PTR
)raw_src
, (PTR
)&internal_ptr
->u
.syment
);
1113 internal_ptr
->fix_tag
= 0;
1114 internal_ptr
->fix_end
= 0;
1115 symbol_ptr
= internal_ptr
;
1118 i
< symbol_ptr
->u
.syment
.n_numaux
;
1124 internal_ptr
->fix_tag
= 0;
1125 internal_ptr
->fix_end
= 0;
1126 bfd_coff_swap_aux_in(abfd
, (PTR
) raw_src
,
1127 symbol_ptr
->u
.syment
.n_type
,
1128 symbol_ptr
->u
.syment
.n_sclass
,
1129 &(internal_ptr
->u
.auxent
));
1130 /* Remember that bal entries arn't pointerized */
1131 if (i
!= 1 || symbol_ptr
->u
.syment
.n_sclass
!= C_LEAFPROC
)
1134 coff_pointerize_aux(abfd
,
1136 symbol_ptr
->u
.syment
.n_type
,
1137 symbol_ptr
->u
.syment
.n_sclass
,
1144 /* Free all the raw stuff */
1145 bfd_release(abfd
, raw
);
1147 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1150 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
) {
1151 /* make a file symbol point to the name in the auxent, since
1152 the text ".file" is redundant */
1153 if ((internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0) {
1154 /* the filename is a long one, point into the string table */
1155 if (string_table
== NULL
) {
1156 string_table
= build_string_table(abfd
);
1159 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1160 (int) (string_table
- 4 +
1161 (internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_offset
);
1164 /* ordinary short filename, put into memory anyway */
1165 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1166 copy_name(abfd
, (internal_ptr
+1)->u
.auxent
.x_file
.x_fname
,
1171 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0) {
1172 /* This is a "short" name. Make it long. */
1173 unsigned long i
= 0;
1174 char *newstring
= NULL
;
1176 /* find the length of this string without walking into memory
1178 for (i
= 0; i
< 8; ++i
) {
1179 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0') {
1181 } /* if end of string */
1182 } /* possible lengths of this string. */
1184 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1185 bfd_error
= no_memory
;
1188 memset(newstring
, 0, i
);
1189 strncpy(newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
-1);
1190 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int) newstring
;
1191 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1193 else if (!bfd_coff_symname_in_debug(abfd
, &internal_ptr
->u
.syment
)) {
1194 /* Long name already. Point symbol at the string in the table. */
1195 if (string_table
== NULL
) {
1196 string_table
= build_string_table(abfd
);
1198 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1199 (string_table
- 4 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1202 /* Long name in debug section. Very similar. */
1203 if (debug_section
== NULL
) {
1204 debug_section
= build_debug_section(abfd
);
1206 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1207 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1210 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1213 obj_raw_syments(abfd
) = internal
;
1216 } /* coff_get_normalized_symtab() */
1219 DEFUN (coff_get_reloc_upper_bound
, (abfd
, asect
),
1223 if (bfd_get_format(abfd
) != bfd_object
) {
1224 bfd_error
= invalid_operation
;
1227 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
1231 DEFUN (coff_make_empty_symbol
, (abfd
),
1234 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1236 bfd_error
= no_memory
;
1239 new->symbol
.section
= 0;
1241 new->lineno
= (alent
*) NULL
;
1242 new->done_lineno
= false;
1243 new->symbol
.the_bfd
= abfd
;
1244 return &new->symbol
;
1248 DEFUN (coff_make_debug_symbol
, (abfd
, ptr
, sz
),
1253 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1255 bfd_error
= no_memory
;
1258 /* @@ This shouldn't be using a constant multiplier. */
1259 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1260 new->symbol
.section
= &bfd_debug_section
;
1261 new->lineno
= (alent
*) NULL
;
1262 new->done_lineno
= false;
1263 new->symbol
.the_bfd
= abfd
;
1264 return &new->symbol
;
1268 DEFUN(coff_print_symbol
,(abfd
, filep
, symbol
, how
),
1272 bfd_print_symbol_type how
)
1274 FILE *file
= (FILE *)filep
;
1276 case bfd_print_symbol_name
:
1277 fprintf(file
, "%s", symbol
->name
);
1279 case bfd_print_symbol_more
:
1280 fprintf(file
, "coff %lx %lx", (unsigned long) coffsymbol(symbol
)->native
,
1281 (unsigned long) coffsymbol(symbol
)->lineno
);
1283 case bfd_print_symbol_nm
:
1286 CONST
char *section_name
= symbol
->section
->name
;
1287 bfd_print_symbol_vandf((PTR
) file
, symbol
);
1290 fprintf(file
, " %-5s %s %s %s",
1292 coffsymbol(symbol
)->native
? "n" : "g",
1293 coffsymbol(symbol
)->lineno
? "l" : " ",
1299 case bfd_print_symbol_all
:
1300 /* Print out the symbols in a reasonable way */
1302 CONST
char *section_name
= symbol
->section
->name
;
1305 if (coffsymbol(symbol
)->native
)
1308 combined_entry_type
*combined
= coffsymbol(symbol
)->native
;
1309 combined_entry_type
*root
= obj_raw_syments(abfd
);
1311 fprintf(file
,"[%3d]",
1315 fprintf(file
, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1316 combined
->u
.syment
.n_scnum
,
1317 combined
->u
.syment
.n_flags
,
1318 combined
->u
.syment
.n_type
,
1319 combined
->u
.syment
.n_sclass
,
1320 combined
->u
.syment
.n_numaux
,
1321 combined
->u
.syment
.n_value
,
1324 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1327 switch (combined
->u
.syment
.n_sclass
) {
1329 fprintf(file
, "File ");
1332 fprintf(file
, "AUX lnno %x size %x tagndx %x",
1333 combined
[aux
+1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1334 combined
[aux
+1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1335 combined
[aux
+1].u
.auxent
.x_sym
.x_tagndx
.l
);
1343 struct lineno_cache_entry
*l
= coffsymbol(symbol
)->lineno
;
1346 printf("\n%s :", l
->u
.sym
->name
);
1348 while (l
->line_number
)
1350 printf("\n%4d : %x",
1364 bfd_print_symbol_vandf((PTR
) file
, symbol
);
1365 fprintf(file
, " %-5s %s %s %s",
1367 coffsymbol(symbol
)->native
? "n" : "g",
1368 coffsymbol(symbol
)->lineno
? "l" : " ",
1377 /* Provided a BFD, a section and an offset into the section, calculate
1378 and return the name of the source file and the line nearest to the
1382 DEFUN(coff_find_nearest_line
,(abfd
,
1390 asection
*section AND
1391 asymbol
**ignore_symbols AND
1393 CONST
char **filename_ptr AND
1394 CONST
char **functionname_ptr AND
1395 unsigned int *line_ptr
)
1397 static bfd
*cache_abfd
;
1398 static asection
*cache_section
;
1399 static bfd_vma cache_offset
;
1400 static unsigned int cache_i
;
1401 static alent
*cache_l
;
1404 coff_data_type
*cof
= coff_data(abfd
);
1405 /* Run through the raw syments if available */
1406 combined_entry_type
*p
;
1408 unsigned int line_base
= 0;
1412 *functionname_ptr
= 0;
1415 /* Don't try and find line numbers in a non coff file */
1416 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1422 p
= cof
->raw_syments
;
1424 for (i
= 0; i
< cof
->raw_syment_count
; i
++) {
1425 if (p
->u
.syment
.n_sclass
== C_FILE
) {
1426 /* File name has been moved into symbol */
1427 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1430 p
+= 1 + p
->u
.syment
.n_numaux
;
1432 /* Now wander though the raw linenumbers of the section */
1434 If this is the same BFD as we were previously called with and this is
1435 the same section, and the offset we want is further down then we can
1436 prime the lookup loop
1438 if (abfd
== cache_abfd
&&
1439 section
== cache_section
&&
1440 offset
>= cache_offset
) {
1446 l
= section
->lineno
;
1449 for (; i
< section
->lineno_count
; i
++) {
1450 if (l
->line_number
== 0) {
1451 /* Get the symbol this line number points at */
1452 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
1453 *functionname_ptr
= coff
->symbol
.name
;
1455 combined_entry_type
*s
= coff
->native
;
1456 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1458 S should now point to the .bf of the function
1460 if (s
->u
.syment
.n_numaux
) {
1462 The linenumber is stored in the auxent
1464 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1465 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1470 if (l
->u
.offset
> offset
)
1472 *line_ptr
= l
->line_number
+ line_base
+ 1;
1478 cache_section
= section
;
1479 cache_offset
= offset
;
1487 DEFUN(coff_sizeof_headers
,(abfd
, reloc
),
1493 if (reloc
== false) {
1494 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1497 size
= bfd_coff_filhsz (abfd
);
1500 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);