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
, (PTR
) internal_a
);
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 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
160 bfd_get_start_address (abfd
) = internal_a
->entry
;
162 bfd_get_start_address (abfd
) = 0;
166 bfd_release(abfd
, tdata
);
167 return (bfd_target
*)NULL
;
170 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
171 not a COFF file. This is also used by ECOFF. */
174 DEFUN(coff_object_p
,(abfd
),
181 struct internal_filehdr internal_f
;
182 struct internal_aouthdr internal_a
;
184 bfd_error
= system_call_error
;
186 /* figure out how much to read */
187 filhsz
= bfd_coff_filhsz (abfd
);
188 aoutsz
= bfd_coff_aoutsz (abfd
);
190 filehdr
= bfd_alloc (abfd
, filhsz
);
193 if (bfd_read(filehdr
, 1, filhsz
, abfd
) != filhsz
)
195 bfd_coff_swap_filehdr_in(abfd
, filehdr
, &internal_f
);
196 bfd_release (abfd
, filehdr
);
198 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false) {
199 bfd_error
= wrong_format
;
202 nscns
=internal_f
.f_nscns
;
204 if (internal_f
.f_opthdr
) {
207 opthdr
= bfd_alloc (abfd
, aoutsz
);
210 if (bfd_read(opthdr
, 1,aoutsz
, abfd
) != aoutsz
) {
213 bfd_coff_swap_aouthdr_in(abfd
, opthdr
, (PTR
)&internal_a
);
216 /* Seek past the opt hdr stuff */
217 bfd_seek(abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
);
219 return coff_real_object_p(abfd
, nscns
, &internal_f
,
220 (internal_f
.f_opthdr
!= 0
222 : (struct internal_aouthdr
*) NULL
));
225 /* Get the BFD section from a COFF symbol section number. */
228 DEFUN(coff_section_from_bfd_index
,(abfd
, index
),
232 struct sec
*answer
= abfd
->sections
;
236 return &bfd_abs_section
;
238 if (index
== N_UNDEF
)
240 return &bfd_und_section
;
244 return &bfd_debug_section
;
249 if (answer
->target_index
== index
)
251 answer
= answer
->next
;
254 return &bfd_und_section
; /* For gcc -W and lint. Never executed. */
257 /* Get the upper bound of a COFF symbol table. */
260 coff_get_symtab_upper_bound(abfd
)
263 if (!bfd_coff_slurp_symbol_table(abfd
))
266 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
270 /* Canonicalize a COFF symbol table. */
273 DEFUN(coff_get_symtab
, (abfd
, alocation
),
277 unsigned int counter
= 0;
278 coff_symbol_type
*symbase
;
279 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
280 if (!bfd_coff_slurp_symbol_table(abfd
))
283 symbase
= obj_symbols(abfd
);
284 while (counter
< bfd_get_symcount(abfd
))
286 /* This nasty code looks at the symbol to decide whether or
287 not it is descibes a constructor/destructor entry point. It
288 is structured this way to (hopefully) speed non matches */
290 if (0 && symbase
->symbol
.name
[9] == '$')
292 bfd_constructor_entry(abfd
,
293 (asymbol
**)location
,
294 symbase
->symbol
.name
[10] == 'I' ?
298 *(location
++) = symbase
++;
302 return bfd_get_symcount(abfd
);
305 /* Set lineno_count for the output sections of a COFF file. */
308 DEFUN(coff_count_linenumbers
,(abfd
),
311 unsigned int limit
= bfd_get_symcount(abfd
);
316 asection
*s
= abfd
->sections
->output_section
;
318 BFD_ASSERT(s
->lineno_count
== 0);
324 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
325 asymbol
*q_maybe
= *p
;
326 if (bfd_asymbol_flavour(q_maybe
) == bfd_target_coff_flavour
) {
327 coff_symbol_type
*q
= coffsymbol(q_maybe
);
330 This symbol has a linenumber, increment the owning
331 section's linenumber count
333 alent
*l
= q
->lineno
;
334 q
->symbol
.section
->output_section
->lineno_count
++;
337 while (l
->line_number
) {
339 q
->symbol
.section
->output_section
->lineno_count
++;
348 /* Takes a bfd and a symbol, returns a pointer to the coff specific
349 area of the symbol if there is one. */
352 DEFUN(coff_symbol_from
,(ignore_abfd
, symbol
),
356 if (bfd_asymbol_flavour(symbol
) != bfd_target_coff_flavour
)
357 return (coff_symbol_type
*)NULL
;
359 if (bfd_asymbol_bfd(symbol
)->tdata
.coff_obj_data
== (coff_data_type
*)NULL
)
360 return (coff_symbol_type
*)NULL
;
362 return (coff_symbol_type
*) symbol
;
366 DEFUN(fixup_symbol_value
,(coff_symbol_ptr
, syment
),
367 coff_symbol_type
*coff_symbol_ptr AND
368 struct internal_syment
*syment
)
371 /* Normalize the symbol flags */
372 if (coff_symbol_ptr
->symbol
.section
== &bfd_com_section
) {
373 /* a common symbol is undefined with a value */
374 syment
->n_scnum
= N_UNDEF
;
375 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
377 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
378 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
380 else if (coff_symbol_ptr
->symbol
.section
== & bfd_und_section
) {
381 syment
->n_scnum
= N_UNDEF
;
385 if (coff_symbol_ptr
->symbol
.section
) {
387 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
390 coff_symbol_ptr
->symbol
.value
+
391 coff_symbol_ptr
->symbol
.section
->output_offset
+
392 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
396 /* This can happen, but I don't know why yet (steve@cygnus.com) */
397 syment
->n_scnum
= N_ABS
;
398 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
403 /* run through all the symbols in the symbol table and work out what
404 their indexes into the symbol table will be when output
406 Coff requires that each C_FILE symbol points to the next one in the
407 chain, and that the last one points to the first external symbol. We
412 DEFUN(coff_renumber_symbols
,(bfd_ptr
),
415 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
416 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
417 unsigned int native_index
= 0;
418 struct internal_syment
*last_file
= (struct internal_syment
*)NULL
;
419 unsigned int symbol_index
;
421 /* COFF demands that undefined symbols come after all other symbols.
422 Since we don't need to impose this extra knowledge on all our client
423 programs, deal with that here. Sort the symbol table; just move the
424 undefined symbols to the end, leaving the rest alone. */
425 /* @@ Do we have some condition we could test for, so we don't always
426 have to do this? I don't think relocatability is quite right, but
427 I'm not certain. [raeburn:19920508.1711EST] */
432 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
434 * (symbol_count
+ 1));
435 bfd_ptr
->outsymbols
= newsyms
;
436 for (i
= 0; i
< symbol_count
; i
++)
437 if (symbol_ptr_ptr
[i
]->section
!= &bfd_und_section
)
438 *newsyms
++ = symbol_ptr_ptr
[i
];
439 for (i
= 0; i
< symbol_count
; i
++)
440 if (symbol_ptr_ptr
[i
]->section
== &bfd_und_section
)
441 *newsyms
++ = symbol_ptr_ptr
[i
];
442 *newsyms
= (asymbol
*) NULL
;
443 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
446 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
448 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
449 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
450 combined_entry_type
*s
= coff_symbol_ptr
->native
;
453 if (s
->u
.syment
.n_sclass
== C_FILE
)
455 if (last_file
!= (struct internal_syment
*)NULL
) {
456 last_file
->n_value
= native_index
;
458 last_file
= &(s
->u
.syment
);
462 /* Modify the symbol values according to their section and
465 fixup_symbol_value(coff_symbol_ptr
, &(s
->u
.syment
));
467 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++) {
468 s
[i
].offset
= native_index
++;
475 obj_conv_table_size (bfd_ptr
) = native_index
;
479 Run thorough the symbol table again, and fix it so that all pointers to
480 entries are changed to the entries' index in the output symbol table.
484 DEFUN(coff_mangle_symbols
,(bfd_ptr
),
487 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
488 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
489 unsigned int symbol_index
;
491 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
493 coff_symbol_type
*coff_symbol_ptr
=
494 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
496 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
498 combined_entry_type
*s
= coff_symbol_ptr
->native
;
500 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++) {
501 combined_entry_type
*a
= s
+ i
+ 1;
503 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
504 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
508 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
509 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
519 static int string_size
;
522 DEFUN(coff_fix_symbol_name
,(abfd
, symbol
, native
),
525 combined_entry_type
*native
)
527 unsigned int name_length
;
528 union internal_auxent
*auxent
;
529 char * name
= ( char *)(symbol
->name
);
531 if (name
== (char *) NULL
) {
532 /* coff symbols always have names, so we'll make one up */
533 symbol
->name
= "strange";
534 name
= (char *)symbol
->name
;
536 name_length
= strlen(name
);
538 if (native
->u
.syment
.n_sclass
== C_FILE
) {
539 strncpy(native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
540 auxent
= &(native
+1)->u
.auxent
;
542 if (bfd_coff_long_filenames (abfd
)) {
543 if (name_length
<= FILNMLEN
) {
544 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
547 auxent
->x_file
.x_n
.x_offset
= string_size
+ 4;
548 auxent
->x_file
.x_n
.x_zeroes
= 0;
549 string_size
+= name_length
+ 1;
553 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
554 if (name_length
> FILNMLEN
) {
555 name
[FILNMLEN
] = '\0';
560 { /* NOT A C_FILE SYMBOL */
561 if (name_length
<= SYMNMLEN
) {
562 /* This name will fit into the symbol neatly */
563 strncpy(native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
566 native
->u
.syment
._n
._n_n
._n_offset
= string_size
+ 4;
567 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
568 string_size
+= name_length
+ 1;
573 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
576 DEFUN(coff_write_symbol
,(abfd
, symbol
, native
, written
),
579 combined_entry_type
*native AND
580 unsigned int written
)
582 unsigned int numaux
= native
->u
.syment
.n_numaux
;
583 int type
= native
->u
.syment
.n_type
;
584 int class = native
->u
.syment
.n_sclass
;
586 bfd_size_type symesz
;
588 /* @@ bfd_debug_section isn't accessible outside this file, but we know
589 that C_FILE symbols belong there. So move them. */
590 if (native
->u
.syment
.n_sclass
== C_FILE
)
591 symbol
->section
= &bfd_debug_section
;
593 if (symbol
->section
== &bfd_abs_section
)
595 native
->u
.syment
.n_scnum
= N_ABS
;
597 else if (symbol
->section
== &bfd_debug_section
)
599 native
->u
.syment
.n_scnum
= N_DEBUG
;
601 else if (symbol
->section
== &bfd_und_section
)
603 native
->u
.syment
.n_scnum
= N_UNDEF
;
607 native
->u
.syment
.n_scnum
=
608 symbol
->section
->output_section
->target_index
;
612 coff_fix_symbol_name(abfd
, symbol
, native
);
614 symesz
= bfd_coff_symesz (abfd
);
615 buf
= bfd_alloc (abfd
, symesz
);
616 bfd_coff_swap_sym_out(abfd
, &native
->u
.syment
, buf
);
617 bfd_write(buf
, 1, symesz
, abfd
);
618 bfd_release (abfd
, buf
);
620 if (native
->u
.syment
.n_numaux
> 0)
622 bfd_size_type auxesz
;
625 auxesz
= bfd_coff_auxesz (abfd
);
626 buf
= bfd_alloc (abfd
, auxesz
);
627 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
629 bfd_coff_swap_aux_out(abfd
,
630 &((native
+ j
+ 1)->u
.auxent
),
634 bfd_write(buf
, 1, auxesz
, abfd
);
636 bfd_release (abfd
, buf
);
639 Reuse somewhere in the symbol to keep the index
641 set_index(symbol
, written
);
642 return written
+ 1 + numaux
;
647 DEFUN(coff_write_alien_symbol
,(abfd
, symbol
, written
),
650 unsigned int written
)
653 This symbol has been created by the loader, or come from a non
654 coff format. It has no native element to inherit, make our
657 combined_entry_type
*native
;
658 combined_entry_type dummy
;
660 native
->u
.syment
.n_type
= T_NULL
;
661 native
->u
.syment
.n_flags
= 0;
662 if (symbol
->section
== &bfd_und_section
)
664 native
->u
.syment
.n_scnum
= N_UNDEF
;
665 native
->u
.syment
.n_value
= symbol
->value
;
667 else if (symbol
->section
== &bfd_com_section
)
669 native
->u
.syment
.n_scnum
= N_UNDEF
;
670 native
->u
.syment
.n_value
= symbol
->value
;
674 else if (symbol
->flags
& BSF_DEBUGGING
) {
676 remove name so it doesn't take up any space
681 native
->u
.syment
.n_scnum
= symbol
->section
->output_section
->target_index
;
682 native
->u
.syment
.n_value
= symbol
->value
+
683 symbol
->section
->output_section
->vma
+
684 symbol
->section
->output_offset
;
685 /* Copy the any flags from the the file hdr into the symbol */
687 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
688 if (c
!= (coff_symbol_type
*)NULL
) {
689 native
->u
.syment
.n_flags
= bfd_asymbol_bfd(&c
->symbol
)->flags
;
694 native
->u
.syment
.n_type
= 0;
695 if (symbol
->flags
& BSF_LOCAL
)
696 native
->u
.syment
.n_sclass
= C_STAT
;
698 native
->u
.syment
.n_sclass
= C_EXT
;
699 native
->u
.syment
.n_numaux
= 0;
701 return coff_write_symbol(abfd
, symbol
, native
, written
);
705 DEFUN(coff_write_native_symbol
,(abfd
, symbol
, written
),
707 coff_symbol_type
*symbol AND
708 unsigned int written
)
711 Does this symbol have an ascociated line number - if so then
712 make it remember this symbol index. Also tag the auxent of
713 this symbol to point to the right place in the lineno table
715 combined_entry_type
*native
= symbol
->native
;
717 alent
*lineno
= symbol
->lineno
;
719 if (lineno
&& !symbol
->done_lineno
) {
720 unsigned int count
= 0;
721 lineno
[count
].u
.offset
= written
;
722 if (native
->u
.syment
.n_numaux
) {
723 union internal_auxent
*a
= &((native
+1)->u
.auxent
);
725 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
726 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
729 And count and relocate all other linenumbers
733 while (lineno
[count
].line_number
) {
736 I've been told this, but still need proof:
737 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
738 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
739 > only specific to A29K implementations, but affects all systems using COFF
740 > format binaries. Note that in COFF object files, the line number core offsets
741 > output by the assembler are relative to the start of each procedure, not
742 > to the start of the .text section. This patch relocates the line numbers
743 > relative to the `native->u.syment.n_value' instead of the section virtual
744 > address. modular!olson@cs.arizona.edu (Jon Olson)
746 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
749 lineno
[count
].u
.offset
+=
750 symbol
->symbol
.section
->output_section
->vma
+
751 symbol
->symbol
.section
->output_offset
;
755 symbol
->done_lineno
= true;
757 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
758 count
* bfd_coff_linesz (abfd
);
760 return coff_write_symbol(abfd
, &( symbol
->symbol
), native
,written
);
764 DEFUN(coff_write_symbols
,(abfd
),
768 unsigned int limit
= bfd_get_symcount(abfd
);
769 unsigned int written
= 0;
776 /* Seek to the right place */
777 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
779 /* Output all the symbols we have */
782 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
784 asymbol
*symbol
= *p
;
785 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
787 if (c_symbol
== (coff_symbol_type
*) NULL
||
788 c_symbol
->native
== (combined_entry_type
*)NULL
)
790 written
= coff_write_alien_symbol(abfd
, symbol
, written
);
794 written
= coff_write_native_symbol(abfd
, c_symbol
, written
);
799 bfd_get_symcount(abfd
) = written
;
801 /* Now write out strings */
803 if (string_size
!= 0)
805 unsigned int size
= string_size
+ 4;
808 bfd_h_put_32(abfd
, size
, buffer
);
809 bfd_write((PTR
) buffer
, 1, sizeof(buffer
), abfd
);
810 for (p
= abfd
->outsymbols
, i
= 0;
815 size_t name_length
= strlen(q
->name
);
817 coff_symbol_type
* c_symbol
= coff_symbol_from(abfd
, q
);
818 maxlen
= ((c_symbol
!= NULL
&& c_symbol
->native
!= NULL
) &&
819 (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
)) ?
822 if (name_length
> maxlen
) {
823 bfd_write((PTR
) (q
->name
), 1, name_length
+ 1, abfd
);
828 /* We would normally not write anything here, but we'll write
829 out 4 so that any stupid coff reader which tries to read
830 the string table even when there isn't one won't croak.
833 uint32e_type size
= 4;
835 bfd_write((PTR
)&size
, 1, sizeof(size
), abfd
);
841 DEFUN(coff_write_linenumbers
,(abfd
),
845 bfd_size_type linesz
;
848 linesz
= bfd_coff_linesz (abfd
);
849 buff
= bfd_alloc (abfd
, linesz
);
850 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
851 if (s
->lineno_count
) {
852 asymbol
**q
= abfd
->outsymbols
;
853 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
854 /* Find all the linenumbers in this section */
857 if (p
->section
->output_section
== s
) {
859 BFD_SEND(bfd_asymbol_bfd(p
), _get_lineno
, (bfd_asymbol_bfd(p
), p
));
861 /* Found a linenumber entry, output */
862 struct internal_lineno out
;
863 memset( (PTR
)&out
, 0, sizeof(out
));
865 out
.l_addr
.l_symndx
= l
->u
.offset
;
866 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
867 bfd_write(buff
, 1, linesz
, abfd
);
869 while (l
->line_number
) {
870 out
.l_lnno
= l
->line_number
;
871 out
.l_addr
.l_symndx
= l
->u
.offset
;
872 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
873 bfd_write(buff
, 1, linesz
, abfd
);
882 bfd_release (abfd
, buff
);
886 DEFUN(coff_get_lineno
,(ignore_abfd
, symbol
),
890 return coffsymbol(symbol
)->lineno
;
894 coff_section_symbol (abfd
, name
)
898 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
900 combined_entry_type
*csym
;
903 if (coff_symbol_from (abfd
, sym
))
904 csym
= coff_symbol_from (abfd
, sym
)->native
;
907 /* Make sure back-end COFF stuff is there. */
911 coff_symbol_type sym
;
912 /* @@FIXME This shouldn't use a fixed size!! */
913 combined_entry_type e
[10];
916 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
917 memset ((char *) f
, 0, sizeof (*f
));
918 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
920 csym
[0].u
.syment
.n_sclass
= C_STAT
;
921 csym
[0].u
.syment
.n_numaux
= 1;
922 /* SF_SET_STATICS (sym); @@ ??? */
925 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
926 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
927 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
931 csym
[1].u
.auxent
.x_scn
.x_scnlen
= 0;
932 csym
[1].u
.auxent
.x_scn
.x_nreloc
= 0;
933 csym
[1].u
.auxent
.x_scn
.x_nlinno
= 0;
938 /* This function transforms the offsets into the symbol table into
939 pointers to syments. */
942 DEFUN(coff_pointerize_aux
,(abfd
, table_base
, type
, class, auxent
),
944 combined_entry_type
*table_base AND
947 combined_entry_type
*auxent
)
949 /* Don't bother if this is a file or a section */
950 if (class == C_STAT
&& type
== T_NULL
) return;
951 if (class == C_FILE
) return;
953 /* Otherwise patch up */
954 #define N_TMASK coff_data (abfd)->local_n_tmask
955 #define N_BTSHFT coff_data (abfd)->local_n_btshft
956 if (ISFCN(type
) || ISTAG(class) || class == C_BLOCK
) {
957 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
= table_base
+
958 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
961 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
962 generate one, so we must be careful to ignore it. */
963 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0) {
964 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
965 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
971 DEFUN(build_string_table
,(abfd
),
974 char string_table_size_buffer
[4];
975 unsigned int string_table_size
;
978 /* At this point we should be "seek"'d to the end of the
979 symbols === the symbol table size. */
980 if (bfd_read((char *) string_table_size_buffer
,
981 sizeof(string_table_size_buffer
),
982 1, abfd
) != sizeof(string_table_size
)) {
983 bfd_error
= system_call_error
;
987 string_table_size
= bfd_h_get_32(abfd
, (bfd_byte
*) string_table_size_buffer
);
989 if ((string_table
= (PTR
) bfd_alloc(abfd
, string_table_size
-= 4)) == NULL
) {
990 bfd_error
= no_memory
;
992 } /* on mallocation error */
993 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
994 bfd_error
= system_call_error
;
1000 /* Allocate space for the ".debug" section, and read it.
1001 We did not read the debug section until now, because
1002 we didn't want to go to the trouble until someone needed it. */
1005 DEFUN(build_debug_section
,(abfd
),
1008 char *debug_section
;
1011 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1014 bfd_error
= no_debug_section
;
1018 debug_section
= (PTR
) bfd_alloc (abfd
,
1019 bfd_get_section_size_before_reloc (sect
));
1020 if (debug_section
== NULL
) {
1021 bfd_error
= no_memory
;
1025 /* Seek to the beginning of the `.debug' section and read it.
1026 Save the current position first; it is needed by our caller.
1027 Then read debug section and reset the file pointer. */
1029 position
= bfd_tell (abfd
);
1030 bfd_seek (abfd
, sect
->filepos
, SEEK_SET
);
1031 if (bfd_read (debug_section
,
1032 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1033 != bfd_get_section_size_before_reloc(sect
)) {
1034 bfd_error
= system_call_error
;
1037 bfd_seek (abfd
, position
, SEEK_SET
);
1038 return debug_section
;
1042 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1043 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1044 be \0-terminated. */
1046 DEFUN(copy_name
,(abfd
, name
, maxlen
),
1054 for (len
= 0; len
< maxlen
; ++len
) {
1055 if (name
[len
] == '\0') {
1060 if ((newname
= (PTR
) bfd_alloc(abfd
, len
+1)) == NULL
) {
1061 bfd_error
= no_memory
;
1064 strncpy(newname
, name
, len
);
1065 newname
[len
] = '\0';
1069 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1070 knit the symbol names into a normalized form. By normalized here I
1071 mean that all symbols have an n_offset pointer that points to a null-
1072 terminated string. */
1074 combined_entry_type
*
1075 DEFUN(coff_get_normalized_symtab
,(abfd
),
1078 combined_entry_type
*internal
;
1079 combined_entry_type
*internal_ptr
;
1080 combined_entry_type
*symbol_ptr
;
1081 combined_entry_type
*internal_end
;
1082 bfd_size_type symesz
;
1086 char *string_table
= NULL
;
1087 char *debug_section
= NULL
;
1090 unsigned int raw_size
;
1091 if (obj_raw_syments(abfd
) != (combined_entry_type
*)NULL
) {
1092 return obj_raw_syments(abfd
);
1094 if ((size
= bfd_get_symcount(abfd
) * sizeof(combined_entry_type
)) == 0) {
1095 bfd_error
= no_symbols
;
1099 internal
= (combined_entry_type
*)bfd_alloc(abfd
, size
);
1100 internal_end
= internal
+ bfd_get_symcount(abfd
);
1102 symesz
= bfd_coff_symesz (abfd
);
1103 raw_size
= bfd_get_symcount(abfd
) * symesz
;
1104 raw
= bfd_alloc(abfd
,raw_size
);
1106 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1107 || bfd_read(raw
, raw_size
, 1, abfd
) != raw_size
) {
1108 bfd_error
= system_call_error
;
1111 /* mark the end of the symbols */
1112 raw_end
= (char *) raw
+ bfd_get_symcount(abfd
) * symesz
;
1114 FIXME SOMEDAY. A string table size of zero is very weird, but
1115 probably possible. If one shows up, it will probably kill us.
1118 /* Swap all the raw entries */
1119 for (raw_src
= (char *) raw
, internal_ptr
= internal
;
1121 raw_src
+= symesz
, internal_ptr
++) {
1124 bfd_coff_swap_sym_in(abfd
, (PTR
)raw_src
, (PTR
)&internal_ptr
->u
.syment
);
1125 internal_ptr
->fix_tag
= 0;
1126 internal_ptr
->fix_end
= 0;
1127 symbol_ptr
= internal_ptr
;
1130 i
< symbol_ptr
->u
.syment
.n_numaux
;
1136 internal_ptr
->fix_tag
= 0;
1137 internal_ptr
->fix_end
= 0;
1138 bfd_coff_swap_aux_in(abfd
, (PTR
) raw_src
,
1139 symbol_ptr
->u
.syment
.n_type
,
1140 symbol_ptr
->u
.syment
.n_sclass
,
1141 &(internal_ptr
->u
.auxent
));
1142 /* Remember that bal entries arn't pointerized */
1143 if (i
!= 1 || symbol_ptr
->u
.syment
.n_sclass
!= C_LEAFPROC
)
1146 coff_pointerize_aux(abfd
,
1148 symbol_ptr
->u
.syment
.n_type
,
1149 symbol_ptr
->u
.syment
.n_sclass
,
1156 /* Free all the raw stuff */
1157 bfd_release(abfd
, raw
);
1159 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1162 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
) {
1163 /* make a file symbol point to the name in the auxent, since
1164 the text ".file" is redundant */
1165 if ((internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0) {
1166 /* the filename is a long one, point into the string table */
1167 if (string_table
== NULL
) {
1168 string_table
= build_string_table(abfd
);
1171 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1172 (int) (string_table
- 4 +
1173 (internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_offset
);
1176 /* ordinary short filename, put into memory anyway */
1177 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1178 copy_name(abfd
, (internal_ptr
+1)->u
.auxent
.x_file
.x_fname
,
1183 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0) {
1184 /* This is a "short" name. Make it long. */
1185 unsigned long i
= 0;
1186 char *newstring
= NULL
;
1188 /* find the length of this string without walking into memory
1190 for (i
= 0; i
< 8; ++i
) {
1191 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0') {
1193 } /* if end of string */
1194 } /* possible lengths of this string. */
1196 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1197 bfd_error
= no_memory
;
1200 memset(newstring
, 0, i
);
1201 strncpy(newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
-1);
1202 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int) newstring
;
1203 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1205 else if (!bfd_coff_symname_in_debug(abfd
, &internal_ptr
->u
.syment
)) {
1206 /* Long name already. Point symbol at the string in the table. */
1207 if (string_table
== NULL
) {
1208 string_table
= build_string_table(abfd
);
1210 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1211 (string_table
- 4 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1214 /* Long name in debug section. Very similar. */
1215 if (debug_section
== NULL
) {
1216 debug_section
= build_debug_section(abfd
);
1218 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (int)
1219 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1222 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1225 obj_raw_syments(abfd
) = internal
;
1228 } /* coff_get_normalized_symtab() */
1231 DEFUN (coff_get_reloc_upper_bound
, (abfd
, asect
),
1235 if (bfd_get_format(abfd
) != bfd_object
) {
1236 bfd_error
= invalid_operation
;
1239 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
1243 DEFUN (coff_make_empty_symbol
, (abfd
),
1246 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1248 bfd_error
= no_memory
;
1251 new->symbol
.section
= 0;
1253 new->lineno
= (alent
*) NULL
;
1254 new->done_lineno
= false;
1255 new->symbol
.the_bfd
= abfd
;
1256 return &new->symbol
;
1260 DEFUN (coff_make_debug_symbol
, (abfd
, ptr
, sz
),
1265 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1267 bfd_error
= no_memory
;
1270 /* @@ This shouldn't be using a constant multiplier. */
1271 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1272 new->symbol
.section
= &bfd_debug_section
;
1273 new->lineno
= (alent
*) NULL
;
1274 new->done_lineno
= false;
1275 new->symbol
.the_bfd
= abfd
;
1276 return &new->symbol
;
1280 DEFUN(coff_print_symbol
,(abfd
, filep
, symbol
, how
),
1284 bfd_print_symbol_type how
)
1286 FILE *file
= (FILE *)filep
;
1288 case bfd_print_symbol_name
:
1289 fprintf(file
, "%s", symbol
->name
);
1291 case bfd_print_symbol_more
:
1292 fprintf(file
, "coff %lx %lx", (unsigned long) coffsymbol(symbol
)->native
,
1293 (unsigned long) coffsymbol(symbol
)->lineno
);
1295 case bfd_print_symbol_nm
:
1298 CONST
char *section_name
= symbol
->section
->name
;
1299 bfd_print_symbol_vandf((PTR
) file
, symbol
);
1302 fprintf(file
, " %-5s %s %s %s",
1304 coffsymbol(symbol
)->native
? "n" : "g",
1305 coffsymbol(symbol
)->lineno
? "l" : " ",
1311 case bfd_print_symbol_all
:
1312 /* Print out the symbols in a reasonable way */
1314 CONST
char *section_name
= symbol
->section
->name
;
1317 if (coffsymbol(symbol
)->native
)
1320 combined_entry_type
*combined
= coffsymbol(symbol
)->native
;
1321 combined_entry_type
*root
= obj_raw_syments(abfd
);
1323 fprintf(file
,"[%3d]",
1327 fprintf(file
, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1328 combined
->u
.syment
.n_scnum
,
1329 combined
->u
.syment
.n_flags
,
1330 combined
->u
.syment
.n_type
,
1331 combined
->u
.syment
.n_sclass
,
1332 combined
->u
.syment
.n_numaux
,
1333 combined
->u
.syment
.n_value
,
1336 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1339 switch (combined
->u
.syment
.n_sclass
) {
1341 fprintf(file
, "File ");
1344 fprintf(file
, "AUX lnno %x size %x tagndx %x",
1345 combined
[aux
+1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1346 combined
[aux
+1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1347 combined
[aux
+1].u
.auxent
.x_sym
.x_tagndx
.l
);
1355 struct lineno_cache_entry
*l
= coffsymbol(symbol
)->lineno
;
1358 printf("\n%s :", l
->u
.sym
->name
);
1360 while (l
->line_number
)
1362 printf("\n%4d : %x",
1376 bfd_print_symbol_vandf((PTR
) file
, symbol
);
1377 fprintf(file
, " %-5s %s %s %s",
1379 coffsymbol(symbol
)->native
? "n" : "g",
1380 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 alent
*cache_l
;
1416 coff_data_type
*cof
= coff_data(abfd
);
1417 /* Run through the raw syments if available */
1418 combined_entry_type
*p
;
1420 unsigned int line_base
= 0;
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
) {
1458 l
= section
->lineno
;
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 *functionname_ptr
= coff
->symbol
.name
;
1467 combined_entry_type
*s
= coff
->native
;
1468 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1470 S should now point to the .bf of the function
1472 if (s
->u
.syment
.n_numaux
) {
1474 The linenumber is stored in the auxent
1476 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1477 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1482 if (l
->u
.offset
> offset
)
1484 *line_ptr
= l
->line_number
+ line_base
+ 1;
1490 cache_section
= section
;
1491 cache_offset
= offset
;
1499 DEFUN(coff_sizeof_headers
,(abfd
, reloc
),
1505 if (reloc
== false) {
1506 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1509 size
= bfd_coff_filhsz (abfd
);
1512 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);