1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 boolean coff_write_symbol
PARAMS ((bfd
*, asymbol
*,
45 combined_entry_type
*,
47 static boolean coff_write_alien_symbol
PARAMS ((bfd
*, asymbol
*,
49 static boolean coff_write_native_symbol
PARAMS ((bfd
*, coff_symbol_type
*,
52 #define STRING_SIZE_SIZE (4)
54 /* Take a section header read from a coff file (in HOST byte order),
55 and make a BFD "section" out of it. This is used by ECOFF. */
57 make_a_section_from_file (abfd
, hdr
, target_index
)
59 struct internal_scnhdr
*hdr
;
60 unsigned int target_index
;
62 asection
*return_section
;
65 /* Assorted wastage to null-terminate the name, thanks AT&T! */
66 name
= bfd_alloc (abfd
, sizeof (hdr
->s_name
) + 1);
69 bfd_set_error (bfd_error_no_memory
);
72 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
73 name
[sizeof (hdr
->s_name
)] = 0;
75 return_section
= bfd_make_section (abfd
, name
);
76 if (return_section
== NULL
)
77 return_section
= bfd_coff_make_section_hook (abfd
, name
);
79 /* Handle several sections of the same name. For example, if an executable
80 has two .bss sections, GDB better be able to find both of them
82 if (return_section
== NULL
)
83 return_section
= bfd_make_section_anyway (abfd
, name
);
85 if (return_section
== NULL
)
88 /* s_paddr is presumed to be = to s_vaddr */
90 return_section
->vma
= hdr
->s_vaddr
;
91 return_section
->lma
= return_section
->vma
;
92 return_section
->_raw_size
= hdr
->s_size
;
93 return_section
->filepos
= hdr
->s_scnptr
;
94 return_section
->rel_filepos
= hdr
->s_relptr
;
95 return_section
->reloc_count
= hdr
->s_nreloc
;
97 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
99 return_section
->line_filepos
= hdr
->s_lnnoptr
;
101 return_section
->lineno_count
= hdr
->s_nlnno
;
102 return_section
->userdata
= NULL
;
103 return_section
->next
= (asection
*) NULL
;
104 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
106 return_section
->target_index
= target_index
;
108 /* At least on i386-coff, the line number count for a shared library
109 section must be ignored. */
110 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
111 return_section
->lineno_count
= 0;
113 if (hdr
->s_nreloc
!= 0)
114 return_section
->flags
|= SEC_RELOC
;
115 /* FIXME: should this check 'hdr->s_size > 0' */
116 if (hdr
->s_scnptr
!= 0)
117 return_section
->flags
|= SEC_HAS_CONTENTS
;
121 /* Read in a COFF object and make it into a BFD. This is used by
124 static const bfd_target
*
125 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
128 struct internal_filehdr
*internal_f
;
129 struct internal_aouthdr
*internal_a
;
131 flagword oflags
= abfd
->flags
;
132 bfd_vma ostart
= bfd_get_start_address (abfd
);
134 size_t readsize
; /* length of file_info */
136 char *external_sections
;
138 if (!(internal_f
->f_flags
& F_RELFLG
))
139 abfd
->flags
|= HAS_RELOC
;
140 if ((internal_f
->f_flags
& F_EXEC
))
141 abfd
->flags
|= EXEC_P
;
142 if (!(internal_f
->f_flags
& F_LNNO
))
143 abfd
->flags
|= HAS_LINENO
;
144 if (!(internal_f
->f_flags
& F_LSYMS
))
145 abfd
->flags
|= HAS_LOCALS
;
147 /* FIXME: How can we set D_PAGED correctly? */
148 if ((internal_f
->f_flags
& F_EXEC
) != 0)
149 abfd
->flags
|= D_PAGED
;
151 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
152 if (internal_f
->f_nsyms
)
153 abfd
->flags
|= HAS_SYMS
;
155 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
156 bfd_get_start_address (abfd
) = internal_a
->entry
;
158 bfd_get_start_address (abfd
) = 0;
160 /* Set up the tdata area. ECOFF uses its own routine, and overrides
162 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
166 scnhsz
= bfd_coff_scnhsz (abfd
);
167 readsize
= nscns
* scnhsz
;
168 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
169 if (!external_sections
)
171 bfd_set_error (bfd_error_no_memory
);
175 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
178 /* Now copy data as required; construct all asections etc */
182 for (i
= 0; i
< nscns
; i
++)
184 struct internal_scnhdr tmp
;
185 bfd_coff_swap_scnhdr_in (abfd
,
186 (PTR
) (external_sections
+ i
* scnhsz
),
188 make_a_section_from_file (abfd
, &tmp
, i
+ 1);
192 /* make_abs_section (abfd); */
194 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
200 bfd_release (abfd
, tdata
);
201 abfd
->flags
= oflags
;
202 bfd_get_start_address (abfd
) = ostart
;
203 return (const bfd_target
*) NULL
;
206 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
207 not a COFF file. This is also used by ECOFF. */
217 struct internal_filehdr internal_f
;
218 struct internal_aouthdr internal_a
;
220 /* figure out how much to read */
221 filhsz
= bfd_coff_filhsz (abfd
);
222 aoutsz
= bfd_coff_aoutsz (abfd
);
224 filehdr
= bfd_alloc (abfd
, filhsz
);
227 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
229 if (bfd_get_error () != bfd_error_system_call
)
230 bfd_set_error (bfd_error_wrong_format
);
233 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
234 bfd_release (abfd
, filehdr
);
236 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
238 bfd_set_error (bfd_error_wrong_format
);
241 nscns
= internal_f
.f_nscns
;
243 if (internal_f
.f_opthdr
)
247 opthdr
= bfd_alloc (abfd
, aoutsz
);
250 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
254 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
257 /* Seek past the opt hdr stuff */
258 if (bfd_seek (abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
262 return coff_real_object_p (abfd
, nscns
, &internal_f
,
263 (internal_f
.f_opthdr
!= 0
265 : (struct internal_aouthdr
*) NULL
));
268 /* Get the BFD section from a COFF symbol section number. */
271 coff_section_from_bfd_index (abfd
, index
)
275 struct sec
*answer
= abfd
->sections
;
278 return bfd_abs_section_ptr
;
279 if (index
== N_UNDEF
)
280 return bfd_und_section_ptr
;
281 if (index
== N_DEBUG
)
282 return bfd_abs_section_ptr
;
286 if (answer
->target_index
== index
)
288 answer
= answer
->next
;
291 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
292 has a bad symbol table in biglitpow.o. */
293 return bfd_und_section_ptr
;
296 /* Get the upper bound of a COFF symbol table. */
299 coff_get_symtab_upper_bound (abfd
)
302 if (!bfd_coff_slurp_symbol_table (abfd
))
305 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
309 /* Canonicalize a COFF symbol table. */
312 coff_get_symtab (abfd
, alocation
)
316 unsigned int counter
;
317 coff_symbol_type
*symbase
;
318 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
320 if (!bfd_coff_slurp_symbol_table (abfd
))
323 symbase
= obj_symbols (abfd
);
324 counter
= bfd_get_symcount (abfd
);
325 while (counter
-- > 0)
326 *location
++ = symbase
++;
330 return bfd_get_symcount (abfd
);
333 /* Set lineno_count for the output sections of a COFF file. */
336 coff_count_linenumbers (abfd
)
339 unsigned int limit
= bfd_get_symcount (abfd
);
347 /* This may be from the backend linker, in which case the
348 lineno_count in the sections is correct. */
349 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
350 total
+= s
->lineno_count
;
354 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
355 BFD_ASSERT (s
->lineno_count
== 0);
357 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
359 asymbol
*q_maybe
= *p
;
361 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
363 coff_symbol_type
*q
= coffsymbol (q_maybe
);
365 if (q
->lineno
!= NULL
)
367 /* This symbol has line numbers. Increment the owning
368 section's linenumber count. */
369 alent
*l
= q
->lineno
;
371 ++q
->symbol
.section
->output_section
->lineno_count
;
374 while (l
->line_number
!= 0)
377 ++q
->symbol
.section
->output_section
->lineno_count
;
387 /* Takes a bfd and a symbol, returns a pointer to the coff specific
388 area of the symbol if there is one. */
392 coff_symbol_from (ignore_abfd
, symbol
)
396 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
397 return (coff_symbol_type
*) NULL
;
399 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
400 return (coff_symbol_type
*) NULL
;
402 return (coff_symbol_type
*) symbol
;
406 fixup_symbol_value (coff_symbol_ptr
, syment
)
407 coff_symbol_type
*coff_symbol_ptr
;
408 struct internal_syment
*syment
;
411 /* Normalize the symbol flags */
412 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
414 /* a common symbol is undefined with a value */
415 syment
->n_scnum
= N_UNDEF
;
416 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
418 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
420 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
422 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
424 syment
->n_scnum
= N_UNDEF
;
429 if (coff_symbol_ptr
->symbol
.section
)
432 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
435 coff_symbol_ptr
->symbol
.value
+
436 coff_symbol_ptr
->symbol
.section
->output_offset
+
437 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
442 /* This can happen, but I don't know why yet (steve@cygnus.com) */
443 syment
->n_scnum
= N_ABS
;
444 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
449 /* Run through all the symbols in the symbol table and work out what
450 their indexes into the symbol table will be when output.
452 Coff requires that each C_FILE symbol points to the next one in the
453 chain, and that the last one points to the first external symbol. We
457 coff_renumber_symbols (bfd_ptr
, first_undef
)
461 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
462 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
463 unsigned int native_index
= 0;
464 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
465 unsigned int symbol_index
;
467 /* COFF demands that undefined symbols come after all other symbols.
468 Since we don't need to impose this extra knowledge on all our
469 client programs, deal with that here. Sort the symbol table;
470 just move the undefined symbols to the end, leaving the rest
471 alone. The O'Reilly book says that defined global symbols come
472 at the end before the undefined symbols, so we do that here as
474 /* @@ Do we have some condition we could test for, so we don't always
475 have to do this? I don't think relocatability is quite right, but
476 I'm not certain. [raeburn:19920508.1711EST] */
481 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
483 * (symbol_count
+ 1));
486 bfd_set_error (bfd_error_no_memory
);
489 bfd_ptr
->outsymbols
= newsyms
;
490 for (i
= 0; i
< symbol_count
; i
++)
491 if (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
492 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
496 *newsyms
++ = symbol_ptr_ptr
[i
];
498 for (i
= 0; i
< symbol_count
; i
++)
499 if (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
500 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
504 *newsyms
++ = symbol_ptr_ptr
[i
];
506 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
508 for (i
= 0; i
< symbol_count
; i
++)
509 if (bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
510 *newsyms
++ = symbol_ptr_ptr
[i
];
511 *newsyms
= (asymbol
*) NULL
;
512 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
515 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
517 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
518 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
519 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
521 combined_entry_type
*s
= coff_symbol_ptr
->native
;
524 if (s
->u
.syment
.n_sclass
== C_FILE
)
526 if (last_file
!= (struct internal_syment
*) NULL
)
527 last_file
->n_value
= native_index
;
528 last_file
= &(s
->u
.syment
);
533 /* Modify the symbol values according to their section and
536 fixup_symbol_value (coff_symbol_ptr
, &(s
->u
.syment
));
538 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
539 s
[i
].offset
= native_index
++;
546 obj_conv_table_size (bfd_ptr
) = native_index
;
551 /* Run thorough the symbol table again, and fix it so that all
552 pointers to entries are changed to the entries' index in the output
556 coff_mangle_symbols (bfd_ptr
)
559 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
560 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
561 unsigned int symbol_index
;
563 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
565 coff_symbol_type
*coff_symbol_ptr
=
566 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
568 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
571 combined_entry_type
*s
= coff_symbol_ptr
->native
;
575 /* FIXME: We should use a union here. */
576 s
->u
.syment
.n_value
=
577 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
580 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
582 combined_entry_type
*a
= s
+ i
+ 1;
585 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
586 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
591 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
592 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
597 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
598 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
606 static bfd_size_type string_size
;
607 static bfd_size_type debug_string_size
;
608 static asection
*debug_string_section
;
611 coff_fix_symbol_name (abfd
, symbol
, native
)
614 combined_entry_type
*native
;
616 unsigned int name_length
;
617 union internal_auxent
*auxent
;
618 char *name
= (char *) (symbol
->name
);
620 if (name
== (char *) NULL
)
622 /* coff symbols always have names, so we'll make one up */
623 symbol
->name
= "strange";
624 name
= (char *) symbol
->name
;
626 name_length
= strlen (name
);
628 if (native
->u
.syment
.n_sclass
== C_FILE
)
630 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
631 auxent
= &(native
+ 1)->u
.auxent
;
633 if (bfd_coff_long_filenames (abfd
))
635 if (name_length
<= FILNMLEN
)
637 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
641 auxent
->x_file
.x_n
.x_offset
= string_size
+ STRING_SIZE_SIZE
;
642 auxent
->x_file
.x_n
.x_zeroes
= 0;
643 string_size
+= name_length
+ 1;
648 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
649 if (name_length
> FILNMLEN
)
651 name
[FILNMLEN
] = '\0';
656 { /* NOT A C_FILE SYMBOL */
657 if (name_length
<= SYMNMLEN
)
659 /* This name will fit into the symbol neatly */
660 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
662 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
664 native
->u
.syment
._n
._n_n
._n_offset
= string_size
+ STRING_SIZE_SIZE
;
665 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
666 string_size
+= name_length
+ 1;
673 /* This name should be written into the .debug section. For
674 some reason each name is preceded by a two byte length
675 and also followed by a null byte. FIXME: We assume that
676 the .debug section has already been created, and that it
678 if (debug_string_section
== (asection
*) NULL
)
679 debug_string_section
= bfd_get_section_by_name (abfd
, ".debug");
680 filepos
= bfd_tell (abfd
);
681 bfd_put_16 (abfd
, name_length
+ 1, buf
);
682 if (!bfd_set_section_contents (abfd
,
683 debug_string_section
,
685 (file_ptr
) debug_string_size
,
687 || !bfd_set_section_contents (abfd
,
688 debug_string_section
,
690 (file_ptr
) debug_string_size
+ 2,
691 (bfd_size_type
) name_length
+ 1))
693 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
695 native
->u
.syment
._n
._n_n
._n_offset
= debug_string_size
+ 2;
696 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
697 debug_string_size
+= name_length
+ 3;
702 /* We need to keep track of the symbol index so that when we write out
703 the relocs we can get the index for a symbol. This method is a
706 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
708 /* Write a symbol out to a COFF file. */
711 coff_write_symbol (abfd
, symbol
, native
, written
)
714 combined_entry_type
*native
;
715 unsigned int *written
;
717 unsigned int numaux
= native
->u
.syment
.n_numaux
;
718 int type
= native
->u
.syment
.n_type
;
719 int class = native
->u
.syment
.n_sclass
;
721 bfd_size_type symesz
;
723 if (native
->u
.syment
.n_sclass
== C_FILE
)
724 symbol
->flags
|= BSF_DEBUGGING
;
726 if (symbol
->flags
& BSF_DEBUGGING
)
728 native
->u
.syment
.n_scnum
= N_DEBUG
;
730 else if (bfd_is_abs_section (symbol
->section
))
732 native
->u
.syment
.n_scnum
= N_ABS
;
734 else if (bfd_is_und_section (symbol
->section
))
736 native
->u
.syment
.n_scnum
= N_UNDEF
;
740 native
->u
.syment
.n_scnum
=
741 symbol
->section
->output_section
->target_index
;
744 coff_fix_symbol_name (abfd
, symbol
, native
);
746 symesz
= bfd_coff_symesz (abfd
);
747 buf
= bfd_alloc (abfd
, symesz
);
750 bfd_set_error (bfd_error_no_memory
);
753 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
754 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
756 bfd_release (abfd
, buf
);
758 if (native
->u
.syment
.n_numaux
> 0)
760 bfd_size_type auxesz
;
763 auxesz
= bfd_coff_auxesz (abfd
);
764 buf
= bfd_alloc (abfd
, auxesz
);
767 bfd_set_error (bfd_error_no_memory
);
770 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
772 bfd_coff_swap_aux_out (abfd
,
773 &((native
+ j
+ 1)->u
.auxent
),
777 native
->u
.syment
.n_numaux
,
779 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
782 bfd_release (abfd
, buf
);
785 /* Store the index for use when we write out the relocs. */
786 set_index (symbol
, *written
);
788 *written
+= numaux
+ 1;
792 /* Write out a symbol to a COFF file that does not come from a COFF
793 file originally. This symbol may have been created by the linker,
794 or we may be linking a non COFF file to a COFF file. */
797 coff_write_alien_symbol (abfd
, symbol
, written
)
800 unsigned int *written
;
802 combined_entry_type
*native
;
803 combined_entry_type dummy
;
806 native
->u
.syment
.n_type
= T_NULL
;
807 native
->u
.syment
.n_flags
= 0;
808 if (bfd_is_und_section (symbol
->section
))
810 native
->u
.syment
.n_scnum
= N_UNDEF
;
811 native
->u
.syment
.n_value
= symbol
->value
;
813 else if (bfd_is_com_section (symbol
->section
))
815 native
->u
.syment
.n_scnum
= N_UNDEF
;
816 native
->u
.syment
.n_value
= symbol
->value
;
818 else if (symbol
->flags
& BSF_DEBUGGING
)
820 /* There isn't much point to writing out a debugging symbol
821 unless we are prepared to convert it into COFF debugging
822 format. So, we just ignore them. We must clobber the symbol
823 name to keep it from being put in the string table. */
829 native
->u
.syment
.n_scnum
=
830 symbol
->section
->output_section
->target_index
;
831 native
->u
.syment
.n_value
= (symbol
->value
832 + symbol
->section
->output_section
->vma
833 + symbol
->section
->output_offset
);
835 /* Copy the any flags from the the file header into the symbol.
838 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
839 if (c
!= (coff_symbol_type
*) NULL
)
840 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
844 native
->u
.syment
.n_type
= 0;
845 if (symbol
->flags
& BSF_LOCAL
)
846 native
->u
.syment
.n_sclass
= C_STAT
;
848 native
->u
.syment
.n_sclass
= C_EXT
;
849 native
->u
.syment
.n_numaux
= 0;
851 return coff_write_symbol (abfd
, symbol
, native
, written
);
854 /* Write a native symbol to a COFF file. */
857 coff_write_native_symbol (abfd
, symbol
, written
)
859 coff_symbol_type
*symbol
;
860 unsigned int *written
;
862 combined_entry_type
*native
= symbol
->native
;
863 alent
*lineno
= symbol
->lineno
;
865 /* If this symbol has an associated line number, we must store the
866 symbol index in the line number field. We also tag the auxent to
867 point to the right place in the lineno table. */
868 if (lineno
&& !symbol
->done_lineno
)
870 unsigned int count
= 0;
871 lineno
[count
].u
.offset
= *written
;
872 if (native
->u
.syment
.n_numaux
)
874 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
876 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
877 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
880 /* Count and relocate all other linenumbers. */
882 while (lineno
[count
].line_number
!= 0)
886 I've been told this, but still need proof:
887 > The second bug is also in `bfd/coffcode.h'. This bug
888 > causes the linker to screw up the pc-relocations for
889 > all the line numbers in COFF code. This bug isn't only
890 > specific to A29K implementations, but affects all
891 > systems using COFF format binaries. Note that in COFF
892 > object files, the line number core offsets output by
893 > the assembler are relative to the start of each
894 > procedure, not to the start of the .text section. This
895 > patch relocates the line numbers relative to the
896 > `native->u.syment.n_value' instead of the section
898 > modular!olson@cs.arizona.edu (Jon Olson)
900 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
902 lineno
[count
].u
.offset
+=
903 (symbol
->symbol
.section
->output_section
->vma
904 + symbol
->symbol
.section
->output_offset
);
908 symbol
->done_lineno
= true;
910 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
911 count
* bfd_coff_linesz (abfd
);
914 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
);
917 /* Write out the COFF symbols. */
920 coff_write_symbols (abfd
)
924 unsigned int limit
= bfd_get_symcount (abfd
);
925 unsigned int written
= 0;
929 debug_string_size
= 0;
931 /* Seek to the right place */
932 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
935 /* Output all the symbols we have */
938 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
940 asymbol
*symbol
= *p
;
941 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
943 if (c_symbol
== (coff_symbol_type
*) NULL
944 || c_symbol
->native
== (combined_entry_type
*) NULL
)
946 if (!coff_write_alien_symbol (abfd
, symbol
, &written
))
951 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
))
956 obj_raw_syment_count (abfd
) = written
;
958 /* Now write out strings */
960 if (string_size
!= 0)
962 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
963 bfd_byte buffer
[STRING_SIZE_SIZE
];
965 #if STRING_SIZE_SIZE == 4
966 bfd_h_put_32 (abfd
, size
, buffer
);
968 #error Change bfd_h_put_32
970 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
972 for (p
= abfd
->outsymbols
, i
= 0;
977 size_t name_length
= strlen (q
->name
);
978 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
981 /* Figure out whether the symbol name should go in the string
982 table. Symbol names that are short enough are stored
983 directly in the syment structure. File names permit a
984 different, longer, length in the syment structure. On
985 XCOFF, some symbol names are stored in the .debug section
986 rather than in the string table. */
989 || c_symbol
->native
== NULL
)
991 /* This is not a COFF symbol, so it certainly is not a
992 file name, nor does it go in the .debug section. */
995 else if (bfd_coff_symname_in_debug (abfd
,
996 &c_symbol
->native
->u
.syment
))
998 /* This symbol name is in the XCOFF .debug section.
999 Don't write it into the string table. */
1000 maxlen
= name_length
;
1002 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
)
1007 if (name_length
> maxlen
)
1009 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1017 /* We would normally not write anything here, but we'll write
1018 out 4 so that any stupid coff reader which tries to read the
1019 string table even when there isn't one won't croak. */
1020 unsigned int size
= STRING_SIZE_SIZE
;
1021 bfd_byte buffer
[STRING_SIZE_SIZE
];
1023 #if STRING_SIZE_SIZE == 4
1024 bfd_h_put_32 (abfd
, size
, buffer
);
1026 #error Change bfd_h_put_32
1028 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1029 != STRING_SIZE_SIZE
)
1033 /* Make sure the .debug section was created to be the correct size.
1034 We should create it ourselves on the fly, but we don't because
1035 BFD won't let us write to any section until we know how large all
1036 the sections are. We could still do it by making another pass
1037 over the symbols. FIXME. */
1038 BFD_ASSERT (debug_string_size
== 0
1039 || (debug_string_section
!= (asection
*) NULL
1040 && (BFD_ALIGN (debug_string_size
,
1041 1 << debug_string_section
->alignment_power
)
1042 == bfd_section_size (abfd
, debug_string_section
))));
1048 coff_write_linenumbers (abfd
)
1052 bfd_size_type linesz
;
1055 linesz
= bfd_coff_linesz (abfd
);
1056 buff
= bfd_alloc (abfd
, linesz
);
1059 bfd_set_error (bfd_error_no_memory
);
1062 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1064 if (s
->lineno_count
)
1066 asymbol
**q
= abfd
->outsymbols
;
1067 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1069 /* Find all the linenumbers in this section */
1073 if (p
->section
->output_section
== s
)
1076 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1077 (bfd_asymbol_bfd (p
), p
));
1080 /* Found a linenumber entry, output */
1081 struct internal_lineno out
;
1082 memset ((PTR
) & out
, 0, sizeof (out
));
1084 out
.l_addr
.l_symndx
= l
->u
.offset
;
1085 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1086 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1089 while (l
->line_number
)
1091 out
.l_lnno
= l
->line_number
;
1092 out
.l_addr
.l_symndx
= l
->u
.offset
;
1093 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1094 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1104 bfd_release (abfd
, buff
);
1110 coff_get_lineno (ignore_abfd
, symbol
)
1114 return coffsymbol (symbol
)->lineno
;
1118 coff_section_symbol (abfd
, name
)
1122 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1124 combined_entry_type
*csym
;
1127 csym
= coff_symbol_from (abfd
, sym
)->native
;
1128 /* Make sure back-end COFF stuff is there. */
1133 coff_symbol_type sym
;
1134 /* @@FIXME This shouldn't use a fixed size!! */
1135 combined_entry_type e
[10];
1138 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
1141 bfd_set_error (bfd_error_no_error
);
1144 memset ((char *) f
, 0, sizeof (*f
));
1145 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1147 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1148 csym
[0].u
.syment
.n_numaux
= 1;
1149 /* SF_SET_STATICS (sym); @@ ??? */
1150 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1151 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1152 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1154 if (sec
->output_section
== NULL
)
1156 sec
->output_section
= sec
;
1157 sec
->output_offset
= 0;
1163 /* This function transforms the offsets into the symbol table into
1164 pointers to syments. */
1167 coff_pointerize_aux (abfd
, table_base
, type
, class, auxent
)
1169 combined_entry_type
*table_base
;
1172 combined_entry_type
*auxent
;
1174 /* Don't bother if this is a file or a section */
1175 if (class == C_STAT
&& type
== T_NULL
)
1177 if (class == C_FILE
)
1180 /* Otherwise patch up */
1181 #define N_TMASK coff_data (abfd)->local_n_tmask
1182 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1183 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
)
1184 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1186 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1188 + auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
);
1189 auxent
->fix_end
= 1;
1191 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1192 generate one, so we must be careful to ignore it. */
1193 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1195 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1196 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1197 auxent
->fix_tag
= 1;
1201 /* Allocate space for the ".debug" section, and read it.
1202 We did not read the debug section until now, because
1203 we didn't want to go to the trouble until someone needed it. */
1206 build_debug_section (abfd
)
1209 char *debug_section
;
1212 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1216 bfd_set_error (bfd_error_no_debug_section
);
1220 debug_section
= (PTR
) bfd_alloc (abfd
,
1221 bfd_get_section_size_before_reloc (sect
));
1222 if (debug_section
== NULL
)
1224 bfd_set_error (bfd_error_no_memory
);
1228 /* Seek to the beginning of the `.debug' section and read it.
1229 Save the current position first; it is needed by our caller.
1230 Then read debug section and reset the file pointer. */
1232 position
= bfd_tell (abfd
);
1233 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1234 || (bfd_read (debug_section
,
1235 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1236 != bfd_get_section_size_before_reloc (sect
))
1237 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1239 return debug_section
;
1243 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1244 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1245 be \0-terminated. */
1247 copy_name (abfd
, name
, maxlen
)
1255 for (len
= 0; len
< maxlen
; ++len
)
1257 if (name
[len
] == '\0')
1263 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1265 bfd_set_error (bfd_error_no_memory
);
1268 strncpy (newname
, name
, len
);
1269 newname
[len
] = '\0';
1273 /* Read in the external symbols. */
1276 _bfd_coff_get_external_symbols (abfd
)
1279 bfd_size_type symesz
;
1283 if (obj_coff_external_syms (abfd
) != NULL
)
1286 symesz
= bfd_coff_symesz (abfd
);
1288 size
= obj_raw_syment_count (abfd
) * symesz
;
1290 syms
= malloc (size
);
1291 if (syms
== NULL
&& size
!= 0)
1293 bfd_set_error (bfd_error_no_memory
);
1297 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1298 || bfd_read (syms
, size
, 1, abfd
) != size
)
1305 obj_coff_external_syms (abfd
) = syms
;
1310 /* Read in the external strings. The strings are not loaded until
1311 they are needed. This is because we have no simple way of
1312 detecting a missing string table in an archive. */
1315 _bfd_coff_read_string_table (abfd
)
1318 char extstrsize
[STRING_SIZE_SIZE
];
1322 if (obj_coff_strings (abfd
) != NULL
)
1323 return obj_coff_strings (abfd
);
1326 (obj_sym_filepos (abfd
)
1327 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1331 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1333 if (bfd_get_error () != bfd_error_file_truncated
)
1336 /* There is no string table. */
1337 strsize
= STRING_SIZE_SIZE
;
1341 #if STRING_SIZE_SIZE == 4
1342 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1344 #error Change bfd_h_get_32
1348 strings
= malloc (strsize
);
1349 if (strings
== NULL
)
1351 bfd_set_error (bfd_error_no_memory
);
1355 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1356 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1357 != strsize
- STRING_SIZE_SIZE
)
1363 obj_coff_strings (abfd
) = strings
;
1368 /* Free up the external symbols and strings read from a COFF file. */
1371 _bfd_coff_free_symbols (abfd
)
1374 if (obj_coff_external_syms (abfd
) != NULL
1375 && ! obj_coff_keep_syms (abfd
))
1377 free (obj_coff_external_syms (abfd
));
1378 obj_coff_external_syms (abfd
) = NULL
;
1380 if (obj_coff_strings (abfd
) != NULL
1381 && ! obj_coff_keep_strings (abfd
))
1383 free (obj_coff_strings (abfd
));
1384 obj_coff_strings (abfd
) = NULL
;
1389 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1390 knit the symbol names into a normalized form. By normalized here I
1391 mean that all symbols have an n_offset pointer that points to a null-
1392 terminated string. */
1394 combined_entry_type
*
1395 coff_get_normalized_symtab (abfd
)
1398 combined_entry_type
*internal
;
1399 combined_entry_type
*internal_ptr
;
1400 combined_entry_type
*symbol_ptr
;
1401 combined_entry_type
*internal_end
;
1402 bfd_size_type symesz
;
1405 const char *string_table
= NULL
;
1406 char *debug_section
= NULL
;
1409 if (obj_raw_syments (abfd
) != NULL
)
1410 return obj_raw_syments (abfd
);
1412 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1413 internal
= (combined_entry_type
*) bfd_alloc (abfd
, size
);
1414 if (internal
== NULL
&& size
!= 0)
1416 bfd_set_error (bfd_error_no_memory
);
1419 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1421 if (! _bfd_coff_get_external_symbols (abfd
))
1424 raw_src
= (char *) obj_coff_external_syms (abfd
);
1426 /* mark the end of the symbols */
1427 symesz
= bfd_coff_symesz (abfd
);
1428 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1430 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1431 probably possible. If one shows up, it will probably kill us. */
1433 /* Swap all the raw entries */
1434 for (internal_ptr
= internal
;
1436 raw_src
+= symesz
, internal_ptr
++)
1440 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1441 (PTR
) & internal_ptr
->u
.syment
);
1442 internal_ptr
->fix_value
= 0;
1443 internal_ptr
->fix_tag
= 0;
1444 internal_ptr
->fix_end
= 0;
1445 internal_ptr
->fix_scnlen
= 0;
1446 symbol_ptr
= internal_ptr
;
1449 i
< symbol_ptr
->u
.syment
.n_numaux
;
1455 internal_ptr
->fix_value
= 0;
1456 internal_ptr
->fix_tag
= 0;
1457 internal_ptr
->fix_end
= 0;
1458 internal_ptr
->fix_scnlen
= 0;
1459 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1460 symbol_ptr
->u
.syment
.n_type
,
1461 symbol_ptr
->u
.syment
.n_sclass
,
1462 i
, symbol_ptr
->u
.syment
.n_numaux
,
1463 &(internal_ptr
->u
.auxent
));
1464 /* Remember that bal entries arn't pointerized */
1465 if (i
!= 1 || symbol_ptr
->u
.syment
.n_sclass
!= C_LEAFPROC
)
1468 coff_pointerize_aux (abfd
,
1470 symbol_ptr
->u
.syment
.n_type
,
1471 symbol_ptr
->u
.syment
.n_sclass
,
1478 /* Free the raw symbols, but not the strings (if we have them). */
1479 obj_coff_keep_strings (abfd
) = true;
1480 if (! _bfd_coff_free_symbols (abfd
))
1483 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1486 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1487 && internal_ptr
->u
.syment
.n_numaux
> 0)
1489 /* make a file symbol point to the name in the auxent, since
1490 the text ".file" is redundant */
1491 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1493 /* the filename is a long one, point into the string table */
1494 if (string_table
== NULL
)
1496 string_table
= _bfd_coff_read_string_table (abfd
);
1497 if (string_table
== NULL
)
1501 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1504 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1508 /* ordinary short filename, put into memory anyway */
1509 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1510 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1516 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1518 /* This is a "short" name. Make it long. */
1519 unsigned long i
= 0;
1520 char *newstring
= NULL
;
1522 /* find the length of this string without walking into memory
1524 for (i
= 0; i
< 8; ++i
)
1526 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1529 } /* if end of string */
1530 } /* possible lengths of this string. */
1532 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1534 bfd_set_error (bfd_error_no_memory
);
1537 memset (newstring
, 0, i
);
1538 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1539 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1540 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1542 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1543 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1544 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1546 /* Long name already. Point symbol at the string in the
1548 if (string_table
== NULL
)
1550 string_table
= _bfd_coff_read_string_table (abfd
);
1551 if (string_table
== NULL
)
1554 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1557 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1561 /* Long name in debug section. Very similar. */
1562 if (debug_section
== NULL
)
1563 debug_section
= build_debug_section (abfd
);
1564 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1565 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1568 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1571 obj_raw_syments (abfd
) = internal
;
1572 BFD_ASSERT (obj_raw_syment_count (abfd
)
1573 == (unsigned int) (internal_ptr
- internal
));
1576 } /* coff_get_normalized_symtab() */
1579 coff_get_reloc_upper_bound (abfd
, asect
)
1583 if (bfd_get_format (abfd
) != bfd_object
)
1585 bfd_set_error (bfd_error_invalid_operation
);
1588 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1592 coff_make_empty_symbol (abfd
)
1595 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1598 bfd_set_error (bfd_error_no_memory
);
1601 memset (new, 0, sizeof *new);
1602 new->symbol
.section
= 0;
1604 new->lineno
= (alent
*) NULL
;
1605 new->done_lineno
= false;
1606 new->symbol
.the_bfd
= abfd
;
1607 return &new->symbol
;
1610 /* Make a debugging symbol. */
1613 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1618 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1621 bfd_set_error (bfd_error_no_memory
);
1624 /* @@ This shouldn't be using a constant multiplier. */
1625 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1628 bfd_set_error (bfd_error_no_memory
);
1631 new->symbol
.section
= bfd_abs_section_ptr
;
1632 new->symbol
.flags
= BSF_DEBUGGING
;
1633 new->lineno
= (alent
*) NULL
;
1634 new->done_lineno
= false;
1635 new->symbol
.the_bfd
= abfd
;
1636 return &new->symbol
;
1641 coff_get_symbol_info (abfd
, symbol
, ret
)
1646 bfd_symbol_info (symbol
, ret
);
1649 /* Print out information about COFF symbol. */
1652 coff_print_symbol (abfd
, filep
, symbol
, how
)
1656 bfd_print_symbol_type how
;
1658 FILE *file
= (FILE *) filep
;
1662 case bfd_print_symbol_name
:
1663 fprintf (file
, "%s", symbol
->name
);
1666 case bfd_print_symbol_more
:
1667 fprintf (file
, "coff %s %s",
1668 coffsymbol (symbol
)->native
? "n" : "g",
1669 coffsymbol (symbol
)->lineno
? "l" : " ");
1672 case bfd_print_symbol_all
:
1673 if (coffsymbol (symbol
)->native
)
1676 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1677 combined_entry_type
*root
= obj_raw_syments (abfd
);
1678 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1680 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1683 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1684 combined
->u
.syment
.n_scnum
,
1685 combined
->u
.syment
.n_flags
,
1686 combined
->u
.syment
.n_type
,
1687 combined
->u
.syment
.n_sclass
,
1688 combined
->u
.syment
.n_numaux
,
1689 (unsigned long) combined
->u
.syment
.n_value
,
1692 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1694 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1698 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1700 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1702 fprintf (file
, "\n");
1703 switch (combined
->u
.syment
.n_sclass
)
1706 fprintf (file
, "File ");
1710 if (combined
->u
.syment
.n_type
== T_NULL
)
1711 /* probably a section symbol? */
1713 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1714 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
1715 auxp
->u
.auxent
.x_scn
.x_nreloc
,
1716 auxp
->u
.auxent
.x_scn
.x_nlinno
);
1719 /* else fall through */
1722 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1723 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1724 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1727 fprintf (file
, " endndx %ld",
1729 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1737 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1739 while (l
->line_number
)
1741 fprintf (file
, "\n%4d : 0x%lx",
1744 (l
->u
.offset
+ symbol
->section
->vma
)));
1751 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1752 fprintf (file
, " %-5s %s %s %s",
1753 symbol
->section
->name
,
1754 coffsymbol (symbol
)->native
? "n" : "g",
1755 coffsymbol (symbol
)->lineno
? "l" : " ",
1761 /* Provided a BFD, a section and an offset into the section, calculate
1762 and return the name of the source file and the line nearest to the
1767 coff_find_nearest_line (abfd
, section
, ignore_symbols
, offset
, filename_ptr
,
1768 functionname_ptr
, line_ptr
)
1771 asymbol
**ignore_symbols
;
1773 CONST
char **filename_ptr
;
1774 CONST
char **functionname_ptr
;
1775 unsigned int *line_ptr
;
1777 static bfd
*cache_abfd
;
1778 static asection
*cache_section
;
1779 static bfd_vma cache_offset
;
1780 static unsigned int cache_i
;
1781 static CONST
char *cache_function
;
1782 static unsigned int line_base
= 0;
1785 coff_data_type
*cof
= coff_data (abfd
);
1786 /* Run through the raw syments if available */
1787 combined_entry_type
*p
;
1788 combined_entry_type
*pend
;
1793 *functionname_ptr
= 0;
1796 /* Don't try and find line numbers in a non coff file */
1797 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1803 /* Find the first C_FILE symbol. */
1804 p
= cof
->raw_syments
;
1805 pend
= p
+ cof
->raw_syment_count
;
1808 if (p
->u
.syment
.n_sclass
== C_FILE
)
1810 p
+= 1 + p
->u
.syment
.n_numaux
;
1817 /* Look through the C_FILE symbols to find the best one. */
1818 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1819 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
1822 combined_entry_type
*p2
;
1824 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
1826 p2
+= 1 + p2
->u
.syment
.n_numaux
)
1828 if (p2
->u
.syment
.n_scnum
> 0
1830 == coff_section_from_bfd_index (abfd
,
1831 p2
->u
.syment
.n_scnum
)))
1833 if (p2
->u
.syment
.n_sclass
== C_FILE
)
1841 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
1842 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
1844 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1845 maxdiff
= offset
- p2
->u
.syment
.n_value
;
1848 /* Avoid endless loops on erroneous files by ensuring that
1849 we always move forward in the file. */
1850 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
1853 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
1854 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
1859 /* Now wander though the raw linenumbers of the section */
1860 /* If this is the same BFD as we were previously called with and
1861 this is the same section, and the offset we want is further down
1862 then we can prime the lookup loop. */
1863 if (abfd
== cache_abfd
&&
1864 section
== cache_section
&&
1865 offset
>= cache_offset
)
1868 *functionname_ptr
= cache_function
;
1874 l
= §ion
->lineno
[i
];
1876 for (; i
< section
->lineno_count
; i
++)
1878 if (l
->line_number
== 0)
1880 /* Get the symbol this line number points at */
1881 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
1882 if (coff
->symbol
.value
> offset
)
1884 *functionname_ptr
= coff
->symbol
.name
;
1887 combined_entry_type
*s
= coff
->native
;
1888 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1890 /* In XCOFF a debugging symbol can follow the function
1892 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
1893 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1896 S should now point to the .bf of the function
1898 if (s
->u
.syment
.n_numaux
)
1901 The linenumber is stored in the auxent
1903 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1904 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1905 *line_ptr
= line_base
;
1911 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
1913 *line_ptr
= l
->line_number
+ line_base
- 1;
1919 cache_section
= section
;
1920 cache_offset
= offset
;
1922 cache_function
= *functionname_ptr
;
1928 coff_sizeof_headers (abfd
, reloc
)
1936 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1940 size
= bfd_coff_filhsz (abfd
);
1943 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);