1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2015 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
25 /* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
42 #include "coff/internal.h"
45 /* Take a section header read from a coff file (in HOST byte order),
46 and make a BFD "section" out of it. This is used by ECOFF. */
49 make_a_section_from_file (bfd
*abfd
,
50 struct internal_scnhdr
*hdr
,
51 unsigned int target_index
)
53 asection
*return_section
;
55 bfd_boolean result
= TRUE
;
60 /* Handle long section names as in PE. On reading, we want to
61 accept long names if the format permits them at all, regardless
62 of the current state of the flag that dictates if we would generate
63 them in outputs; this construct checks if that is the case by
64 attempting to set the flag, without changing its state; the call
65 will fail for formats that do not support long names at all. */
66 if (bfd_coff_set_long_section_names (abfd
, bfd_coff_long_section_names (abfd
))
67 && hdr
->s_name
[0] == '/')
74 /* Flag that this BFD uses long names, even though the format might
75 expect them to be off by default. This won't directly affect the
76 format of any output BFD created from this one, but the information
77 can be used to decide what to do. */
78 bfd_coff_set_long_section_names (abfd
, TRUE
);
79 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
80 buf
[SCNNMLEN
- 1] = '\0';
81 strindex
= strtol (buf
, &p
, 10);
82 if (*p
== '\0' && strindex
>= 0)
84 strings
= _bfd_coff_read_string_table (abfd
);
87 if ((bfd_size_type
)(strindex
+ 2) >= obj_coff_strings_len (abfd
))
90 name
= (char *) bfd_alloc (abfd
,
91 (bfd_size_type
) strlen (strings
) + 1 + 1);
94 strcpy (name
, strings
);
100 /* Assorted wastage to null-terminate the name, thanks AT&T! */
101 name
= (char *) bfd_alloc (abfd
,
102 (bfd_size_type
) sizeof (hdr
->s_name
) + 1 + 1);
105 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
106 name
[sizeof (hdr
->s_name
)] = 0;
109 return_section
= bfd_make_section_anyway (abfd
, name
);
110 if (return_section
== NULL
)
113 return_section
->vma
= hdr
->s_vaddr
;
114 return_section
->lma
= hdr
->s_paddr
;
115 return_section
->size
= hdr
->s_size
;
116 return_section
->filepos
= hdr
->s_scnptr
;
117 return_section
->rel_filepos
= hdr
->s_relptr
;
118 return_section
->reloc_count
= hdr
->s_nreloc
;
120 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
122 return_section
->line_filepos
= hdr
->s_lnnoptr
;
124 return_section
->lineno_count
= hdr
->s_nlnno
;
125 return_section
->userdata
= NULL
;
126 return_section
->next
= NULL
;
127 return_section
->target_index
= target_index
;
129 if (! bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, return_section
,
133 return_section
->flags
= flags
;
135 /* At least on i386-coff, the line number count for a shared library
136 section must be ignored. */
137 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
138 return_section
->lineno_count
= 0;
140 if (hdr
->s_nreloc
!= 0)
141 return_section
->flags
|= SEC_RELOC
;
142 /* FIXME: should this check 'hdr->s_size > 0'. */
143 if (hdr
->s_scnptr
!= 0)
144 return_section
->flags
|= SEC_HAS_CONTENTS
;
146 /* Compress/decompress DWARF debug sections with names: .debug_* and
147 .zdebug_*, after the section flags is set. */
148 if ((flags
& SEC_DEBUGGING
)
150 && ((name
[1] == 'd' && name
[6] == '_')
151 || (strlen (name
) > 8 && name
[1] == 'z' && name
[7] == '_')))
153 enum { nothing
, compress
, decompress
} action
= nothing
;
154 char *new_name
= NULL
;
156 if (bfd_is_section_compressed (abfd
, return_section
))
158 /* Compressed section. Check if we should decompress. */
159 if ((abfd
->flags
& BFD_DECOMPRESS
))
162 else if (!bfd_is_section_compressed (abfd
, return_section
))
164 /* Normal section. Check if we should compress. */
165 if ((abfd
->flags
& BFD_COMPRESS
) && return_section
->size
!= 0)
174 if (!bfd_init_section_compress_status (abfd
, return_section
))
176 (*_bfd_error_handler
)
177 (_("%B: unable to initialize compress status for section %s"),
181 if (return_section
->compress_status
== COMPRESS_SECTION_DONE
)
185 unsigned int len
= strlen (name
);
187 new_name
= bfd_alloc (abfd
, len
+ 2);
188 if (new_name
== NULL
)
192 memcpy (new_name
+ 2, name
+ 1, len
);
197 if (!bfd_init_section_decompress_status (abfd
, return_section
))
199 (*_bfd_error_handler
)
200 (_("%B: unable to initialize decompress status for section %s"),
206 unsigned int len
= strlen (name
);
208 new_name
= bfd_alloc (abfd
, len
);
209 if (new_name
== NULL
)
212 memcpy (new_name
+ 1, name
+ 2, len
- 1);
216 if (new_name
!= NULL
)
217 bfd_rename_section (abfd
, return_section
, new_name
);
223 /* Read in a COFF object and make it into a BFD. This is used by
226 coff_real_object_p (bfd
*,
228 struct internal_filehdr
*,
229 struct internal_aouthdr
*);
231 coff_real_object_p (bfd
*abfd
,
233 struct internal_filehdr
*internal_f
,
234 struct internal_aouthdr
*internal_a
)
236 flagword oflags
= abfd
->flags
;
237 bfd_vma ostart
= bfd_get_start_address (abfd
);
240 bfd_size_type readsize
; /* Length of file_info. */
242 char *external_sections
;
244 if (!(internal_f
->f_flags
& F_RELFLG
))
245 abfd
->flags
|= HAS_RELOC
;
246 if ((internal_f
->f_flags
& F_EXEC
))
247 abfd
->flags
|= EXEC_P
;
248 if (!(internal_f
->f_flags
& F_LNNO
))
249 abfd
->flags
|= HAS_LINENO
;
250 if (!(internal_f
->f_flags
& F_LSYMS
))
251 abfd
->flags
|= HAS_LOCALS
;
253 /* FIXME: How can we set D_PAGED correctly? */
254 if ((internal_f
->f_flags
& F_EXEC
) != 0)
255 abfd
->flags
|= D_PAGED
;
257 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
258 if (internal_f
->f_nsyms
)
259 abfd
->flags
|= HAS_SYMS
;
261 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
262 bfd_get_start_address (abfd
) = internal_a
->entry
;
264 bfd_get_start_address (abfd
) = 0;
266 /* Set up the tdata area. ECOFF uses its own routine, and overrides
268 tdata_save
= abfd
->tdata
.any
;
269 tdata
= bfd_coff_mkobject_hook (abfd
, (void *) internal_f
, (void *) internal_a
);
273 scnhsz
= bfd_coff_scnhsz (abfd
);
274 readsize
= (bfd_size_type
) nscns
* scnhsz
;
275 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
276 if (!external_sections
)
279 if (bfd_bread ((void *) external_sections
, readsize
, abfd
) != readsize
)
282 /* Set the arch/mach *before* swapping in sections; section header swapping
283 may depend on arch/mach info. */
284 if (! bfd_coff_set_arch_mach_hook (abfd
, (void *) internal_f
))
287 /* Now copy data as required; construct all asections etc. */
291 for (i
= 0; i
< nscns
; i
++)
293 struct internal_scnhdr tmp
;
294 bfd_coff_swap_scnhdr_in (abfd
,
295 (void *) (external_sections
+ i
* scnhsz
),
297 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
305 bfd_release (abfd
, tdata
);
307 abfd
->tdata
.any
= tdata_save
;
308 abfd
->flags
= oflags
;
309 bfd_get_start_address (abfd
) = ostart
;
310 return (const bfd_target
*) NULL
;
313 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
314 not a COFF file. This is also used by ECOFF. */
317 coff_object_p (bfd
*abfd
)
319 bfd_size_type filhsz
;
320 bfd_size_type aoutsz
;
323 struct internal_filehdr internal_f
;
324 struct internal_aouthdr internal_a
;
326 /* Figure out how much to read. */
327 filhsz
= bfd_coff_filhsz (abfd
);
328 aoutsz
= bfd_coff_aoutsz (abfd
);
330 filehdr
= bfd_alloc (abfd
, filhsz
);
333 if (bfd_bread (filehdr
, filhsz
, abfd
) != filhsz
)
335 if (bfd_get_error () != bfd_error_system_call
)
336 bfd_set_error (bfd_error_wrong_format
);
337 bfd_release (abfd
, filehdr
);
340 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
341 bfd_release (abfd
, filehdr
);
343 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
344 (less than aoutsz) used in object files and AOUTSZ (equal to
345 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
346 expects this header to be aoutsz bytes in length, so we use that
347 value in the call to bfd_alloc below. But we must be careful to
348 only read in f_opthdr bytes in the call to bfd_bread. We should
349 also attempt to catch corrupt or non-COFF binaries with a strange
350 value for f_opthdr. */
351 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
352 || internal_f
.f_opthdr
> aoutsz
)
354 bfd_set_error (bfd_error_wrong_format
);
357 nscns
= internal_f
.f_nscns
;
359 if (internal_f
.f_opthdr
)
363 opthdr
= bfd_alloc (abfd
, aoutsz
);
366 if (bfd_bread (opthdr
, (bfd_size_type
) internal_f
.f_opthdr
, abfd
)
367 != internal_f
.f_opthdr
)
369 bfd_release (abfd
, opthdr
);
372 /* PR 17512: file: 11056-1136-0.004. */
373 if (internal_f
.f_opthdr
< aoutsz
)
374 memset (((char *) opthdr
) + internal_f
.f_opthdr
, 0, aoutsz
- internal_f
.f_opthdr
);
376 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (void *) &internal_a
);
377 bfd_release (abfd
, opthdr
);
380 return coff_real_object_p (abfd
, nscns
, &internal_f
,
381 (internal_f
.f_opthdr
!= 0
383 : (struct internal_aouthdr
*) NULL
));
386 /* Get the BFD section from a COFF symbol section number. */
389 coff_section_from_bfd_index (bfd
*abfd
, int section_index
)
391 struct bfd_section
*answer
= abfd
->sections
;
393 if (section_index
== N_ABS
)
394 return bfd_abs_section_ptr
;
395 if (section_index
== N_UNDEF
)
396 return bfd_und_section_ptr
;
397 if (section_index
== N_DEBUG
)
398 return bfd_abs_section_ptr
;
402 if (answer
->target_index
== section_index
)
404 answer
= answer
->next
;
407 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
408 has a bad symbol table in biglitpow.o. */
409 return bfd_und_section_ptr
;
412 /* Get the upper bound of a COFF symbol table. */
415 coff_get_symtab_upper_bound (bfd
*abfd
)
417 if (!bfd_coff_slurp_symbol_table (abfd
))
420 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
423 /* Canonicalize a COFF symbol table. */
426 coff_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
428 unsigned int counter
;
429 coff_symbol_type
*symbase
;
430 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
432 if (!bfd_coff_slurp_symbol_table (abfd
))
435 symbase
= obj_symbols (abfd
);
436 counter
= bfd_get_symcount (abfd
);
437 while (counter
-- > 0)
438 *location
++ = symbase
++;
442 return bfd_get_symcount (abfd
);
445 /* Get the name of a symbol. The caller must pass in a buffer of size
449 _bfd_coff_internal_syment_name (bfd
*abfd
,
450 const struct internal_syment
*sym
,
453 /* FIXME: It's not clear this will work correctly if sizeof
455 if (sym
->_n
._n_n
._n_zeroes
!= 0
456 || sym
->_n
._n_n
._n_offset
== 0)
458 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
459 buf
[SYMNMLEN
] = '\0';
466 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
467 strings
= obj_coff_strings (abfd
);
470 strings
= _bfd_coff_read_string_table (abfd
);
474 /* PR 17910: Only check for string overflow if the length has been set.
475 Some DLLs, eg those produced by Visual Studio, may not set the length field. */
476 if (obj_coff_strings_len (abfd
) > 0
477 && sym
->_n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
))
479 return strings
+ sym
->_n
._n_n
._n_offset
;
483 /* Read in and swap the relocs. This returns a buffer holding the
484 relocs for section SEC in file ABFD. If CACHE is TRUE and
485 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
486 the function is called again. If EXTERNAL_RELOCS is not NULL, it
487 is a buffer large enough to hold the unswapped relocs. If
488 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
489 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
490 value must be INTERNAL_RELOCS. The function returns NULL on error. */
492 struct internal_reloc
*
493 _bfd_coff_read_internal_relocs (bfd
*abfd
,
496 bfd_byte
*external_relocs
,
497 bfd_boolean require_internal
,
498 struct internal_reloc
*internal_relocs
)
501 bfd_byte
*free_external
= NULL
;
502 struct internal_reloc
*free_internal
= NULL
;
505 struct internal_reloc
*irel
;
508 if (sec
->reloc_count
== 0)
509 return internal_relocs
; /* Nothing to do. */
511 if (coff_section_data (abfd
, sec
) != NULL
512 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
514 if (! require_internal
)
515 return coff_section_data (abfd
, sec
)->relocs
;
516 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
517 sec
->reloc_count
* sizeof (struct internal_reloc
));
518 return internal_relocs
;
521 relsz
= bfd_coff_relsz (abfd
);
523 amt
= sec
->reloc_count
* relsz
;
524 if (external_relocs
== NULL
)
526 free_external
= (bfd_byte
*) bfd_malloc (amt
);
527 if (free_external
== NULL
)
529 external_relocs
= free_external
;
532 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
533 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
536 if (internal_relocs
== NULL
)
538 amt
= sec
->reloc_count
;
539 amt
*= sizeof (struct internal_reloc
);
540 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
541 if (free_internal
== NULL
)
543 internal_relocs
= free_internal
;
546 /* Swap in the relocs. */
547 erel
= external_relocs
;
548 erel_end
= erel
+ relsz
* sec
->reloc_count
;
549 irel
= internal_relocs
;
550 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
551 bfd_coff_swap_reloc_in (abfd
, (void *) erel
, (void *) irel
);
553 if (free_external
!= NULL
)
555 free (free_external
);
556 free_external
= NULL
;
559 if (cache
&& free_internal
!= NULL
)
561 if (coff_section_data (abfd
, sec
) == NULL
)
563 amt
= sizeof (struct coff_section_tdata
);
564 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
565 if (sec
->used_by_bfd
== NULL
)
567 coff_section_data (abfd
, sec
)->contents
= NULL
;
569 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
572 return internal_relocs
;
575 if (free_external
!= NULL
)
576 free (free_external
);
577 if (free_internal
!= NULL
)
578 free (free_internal
);
582 /* Set lineno_count for the output sections of a COFF file. */
585 coff_count_linenumbers (bfd
*abfd
)
587 unsigned int limit
= bfd_get_symcount (abfd
);
595 /* This may be from the backend linker, in which case the
596 lineno_count in the sections is correct. */
597 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
598 total
+= s
->lineno_count
;
602 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
603 BFD_ASSERT (s
->lineno_count
== 0);
605 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
607 asymbol
*q_maybe
= *p
;
609 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
611 coff_symbol_type
*q
= coffsymbol (q_maybe
);
613 /* The AIX 4.1 compiler can sometimes generate line numbers
614 attached to debugging symbols. We try to simply ignore
616 if (q
->lineno
!= NULL
617 && q
->symbol
.section
->owner
!= NULL
)
619 /* This symbol has line numbers. Increment the owning
620 section's linenumber count. */
621 alent
*l
= q
->lineno
;
625 asection
* sec
= q
->symbol
.section
->output_section
;
627 /* Do not try to update fields in read-only sections. */
628 if (! bfd_is_const_section (sec
))
629 sec
->lineno_count
++;
634 while (l
->line_number
!= 0);
643 fixup_symbol_value (bfd
*abfd
,
644 coff_symbol_type
*coff_symbol_ptr
,
645 struct internal_syment
*syment
)
647 /* Normalize the symbol flags. */
648 if (coff_symbol_ptr
->symbol
.section
649 && bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
651 /* A common symbol is undefined with a value. */
652 syment
->n_scnum
= N_UNDEF
;
653 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
655 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
656 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
658 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
660 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
662 syment
->n_scnum
= N_UNDEF
;
665 /* FIXME: Do we need to handle the absolute section here? */
668 if (coff_symbol_ptr
->symbol
.section
)
671 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
673 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
674 + coff_symbol_ptr
->symbol
.section
->output_offset
);
677 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
678 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
679 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
685 /* This can happen, but I don't know why yet (steve@cygnus.com) */
686 syment
->n_scnum
= N_ABS
;
687 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
692 /* Run through all the symbols in the symbol table and work out what
693 their indexes into the symbol table will be when output.
695 Coff requires that each C_FILE symbol points to the next one in the
696 chain, and that the last one points to the first external symbol. We
700 coff_renumber_symbols (bfd
*bfd_ptr
, int *first_undef
)
702 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
703 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
704 unsigned int native_index
= 0;
705 struct internal_syment
*last_file
= NULL
;
706 unsigned int symbol_index
;
708 /* COFF demands that undefined symbols come after all other symbols.
709 Since we don't need to impose this extra knowledge on all our
710 client programs, deal with that here. Sort the symbol table;
711 just move the undefined symbols to the end, leaving the rest
712 alone. The O'Reilly book says that defined global symbols come
713 at the end before the undefined symbols, so we do that here as
715 /* @@ Do we have some condition we could test for, so we don't always
716 have to do this? I don't think relocatability is quite right, but
717 I'm not certain. [raeburn:19920508.1711EST] */
723 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
724 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
727 bfd_ptr
->outsymbols
= newsyms
;
728 for (i
= 0; i
< symbol_count
; i
++)
729 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
730 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
731 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
732 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
733 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
735 *newsyms
++ = symbol_ptr_ptr
[i
];
737 for (i
= 0; i
< symbol_count
; i
++)
738 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
739 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
740 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
741 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
742 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
744 *newsyms
++ = symbol_ptr_ptr
[i
];
746 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
748 for (i
= 0; i
< symbol_count
; i
++)
749 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
750 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
751 *newsyms
++ = symbol_ptr_ptr
[i
];
752 *newsyms
= (asymbol
*) NULL
;
753 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
756 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
758 coff_symbol_type
*coff_symbol_ptr
;
760 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
761 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
762 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
764 combined_entry_type
*s
= coff_symbol_ptr
->native
;
767 BFD_ASSERT (s
->is_sym
);
768 if (s
->u
.syment
.n_sclass
== C_FILE
)
770 if (last_file
!= NULL
)
771 last_file
->n_value
= native_index
;
772 last_file
= &(s
->u
.syment
);
775 /* Modify the symbol values according to their section and
777 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
779 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
780 s
[i
].offset
= native_index
++;
786 obj_conv_table_size (bfd_ptr
) = native_index
;
791 /* Run thorough the symbol table again, and fix it so that all
792 pointers to entries are changed to the entries' index in the output
796 coff_mangle_symbols (bfd
*bfd_ptr
)
798 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
799 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
800 unsigned int symbol_index
;
802 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
804 coff_symbol_type
*coff_symbol_ptr
;
806 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
807 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
810 combined_entry_type
*s
= coff_symbol_ptr
->native
;
812 BFD_ASSERT (s
->is_sym
);
815 /* FIXME: We should use a union here. */
816 s
->u
.syment
.n_value
=
817 (bfd_hostptr_t
) ((combined_entry_type
*)
818 ((bfd_hostptr_t
) s
->u
.syment
.n_value
))->offset
;
823 /* The value is the offset into the line number entries
824 for the symbol's section. On output, the symbol's
825 section should be N_DEBUG. */
826 s
->u
.syment
.n_value
=
827 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
828 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
829 coff_symbol_ptr
->symbol
.section
=
830 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
831 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
833 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
835 combined_entry_type
*a
= s
+ i
+ 1;
837 BFD_ASSERT (! a
->is_sym
);
840 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
841 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
846 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
847 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
852 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
853 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
862 coff_fix_symbol_name (bfd
*abfd
,
864 combined_entry_type
*native
,
865 bfd_size_type
*string_size_p
,
866 asection
**debug_string_section_p
,
867 bfd_size_type
*debug_string_size_p
)
869 unsigned int name_length
;
870 union internal_auxent
*auxent
;
871 char *name
= (char *) (symbol
->name
);
875 /* COFF symbols always have names, so we'll make one up. */
876 symbol
->name
= "strange";
877 name
= (char *) symbol
->name
;
879 name_length
= strlen (name
);
881 BFD_ASSERT (native
->is_sym
);
882 if (native
->u
.syment
.n_sclass
== C_FILE
883 && native
->u
.syment
.n_numaux
> 0)
885 unsigned int filnmlen
;
887 if (bfd_coff_force_symnames_in_strings (abfd
))
889 native
->u
.syment
._n
._n_n
._n_offset
=
890 (*string_size_p
+ STRING_SIZE_SIZE
);
891 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
892 *string_size_p
+= 6; /* strlen(".file") + 1 */
895 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
897 BFD_ASSERT (! (native
+ 1)->is_sym
);
898 auxent
= &(native
+ 1)->u
.auxent
;
900 filnmlen
= bfd_coff_filnmlen (abfd
);
902 if (bfd_coff_long_filenames (abfd
))
904 if (name_length
<= filnmlen
)
905 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
908 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
909 auxent
->x_file
.x_n
.x_zeroes
= 0;
910 *string_size_p
+= name_length
+ 1;
915 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
916 if (name_length
> filnmlen
)
917 name
[filnmlen
] = '\0';
922 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
923 /* This name will fit into the symbol neatly. */
924 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
926 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
928 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
930 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
931 *string_size_p
+= name_length
+ 1;
937 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
939 /* This name should be written into the .debug section. For
940 some reason each name is preceded by a two byte length
941 and also followed by a null byte. FIXME: We assume that
942 the .debug section has already been created, and that it
944 if (*debug_string_section_p
== (asection
*) NULL
)
945 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
946 filepos
= bfd_tell (abfd
);
948 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
950 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
952 if (!bfd_set_section_contents (abfd
,
953 *debug_string_section_p
,
955 (file_ptr
) *debug_string_size_p
,
956 (bfd_size_type
) prefix_len
)
957 || !bfd_set_section_contents (abfd
,
958 *debug_string_section_p
,
959 (void *) symbol
->name
,
960 (file_ptr
) (*debug_string_size_p
962 (bfd_size_type
) name_length
+ 1))
964 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
966 native
->u
.syment
._n
._n_n
._n_offset
=
967 *debug_string_size_p
+ prefix_len
;
968 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
969 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
974 /* We need to keep track of the symbol index so that when we write out
975 the relocs we can get the index for a symbol. This method is a
978 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
980 /* Write a symbol out to a COFF file. */
983 coff_write_symbol (bfd
*abfd
,
985 combined_entry_type
*native
,
987 bfd_size_type
*string_size_p
,
988 asection
**debug_string_section_p
,
989 bfd_size_type
*debug_string_size_p
)
991 unsigned int numaux
= native
->u
.syment
.n_numaux
;
992 int type
= native
->u
.syment
.n_type
;
993 int n_sclass
= (int) native
->u
.syment
.n_sclass
;
994 asection
*output_section
= symbol
->section
->output_section
995 ? symbol
->section
->output_section
998 bfd_size_type symesz
;
1000 BFD_ASSERT (native
->is_sym
);
1002 if (native
->u
.syment
.n_sclass
== C_FILE
)
1003 symbol
->flags
|= BSF_DEBUGGING
;
1005 if (symbol
->flags
& BSF_DEBUGGING
1006 && bfd_is_abs_section (symbol
->section
))
1007 native
->u
.syment
.n_scnum
= N_DEBUG
;
1009 else if (bfd_is_abs_section (symbol
->section
))
1010 native
->u
.syment
.n_scnum
= N_ABS
;
1012 else if (bfd_is_und_section (symbol
->section
))
1013 native
->u
.syment
.n_scnum
= N_UNDEF
;
1016 native
->u
.syment
.n_scnum
=
1017 output_section
->target_index
;
1019 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
1020 debug_string_section_p
, debug_string_size_p
);
1022 symesz
= bfd_coff_symesz (abfd
);
1023 buf
= bfd_alloc (abfd
, symesz
);
1026 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
1027 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
1029 bfd_release (abfd
, buf
);
1031 if (native
->u
.syment
.n_numaux
> 0)
1033 bfd_size_type auxesz
;
1036 auxesz
= bfd_coff_auxesz (abfd
);
1037 buf
= bfd_alloc (abfd
, auxesz
);
1040 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
1042 BFD_ASSERT (! (native
+ j
+ 1)->is_sym
);
1043 bfd_coff_swap_aux_out (abfd
,
1044 &((native
+ j
+ 1)->u
.auxent
),
1045 type
, n_sclass
, (int) j
,
1046 native
->u
.syment
.n_numaux
,
1048 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
1051 bfd_release (abfd
, buf
);
1054 /* Store the index for use when we write out the relocs. */
1055 set_index (symbol
, *written
);
1057 *written
+= numaux
+ 1;
1061 /* Write out a symbol to a COFF file that does not come from a COFF
1062 file originally. This symbol may have been created by the linker,
1063 or we may be linking a non COFF file to a COFF file. */
1066 coff_write_alien_symbol (bfd
*abfd
,
1068 struct internal_syment
*isym
,
1070 bfd_size_type
*string_size_p
,
1071 asection
**debug_string_section_p
,
1072 bfd_size_type
*debug_string_size_p
)
1074 combined_entry_type
*native
;
1075 combined_entry_type dummy
[2];
1076 asection
*output_section
= symbol
->section
->output_section
1077 ? symbol
->section
->output_section
1079 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1082 if ((!link_info
|| link_info
->strip_discarded
)
1083 && !bfd_is_abs_section (symbol
->section
)
1084 && symbol
->section
->output_section
== bfd_abs_section_ptr
)
1088 memset (isym
, 0, sizeof (*isym
));
1092 native
->is_sym
= TRUE
;
1093 native
[1].is_sym
= FALSE
;
1094 native
->u
.syment
.n_type
= T_NULL
;
1095 native
->u
.syment
.n_flags
= 0;
1096 native
->u
.syment
.n_numaux
= 0;
1097 if (bfd_is_und_section (symbol
->section
))
1099 native
->u
.syment
.n_scnum
= N_UNDEF
;
1100 native
->u
.syment
.n_value
= symbol
->value
;
1102 else if (bfd_is_com_section (symbol
->section
))
1104 native
->u
.syment
.n_scnum
= N_UNDEF
;
1105 native
->u
.syment
.n_value
= symbol
->value
;
1107 else if (symbol
->flags
& BSF_FILE
)
1109 native
->u
.syment
.n_scnum
= N_DEBUG
;
1110 native
->u
.syment
.n_numaux
= 1;
1112 else if (symbol
->flags
& BSF_DEBUGGING
)
1114 /* There isn't much point to writing out a debugging symbol
1115 unless we are prepared to convert it into COFF debugging
1116 format. So, we just ignore them. We must clobber the symbol
1117 name to keep it from being put in the string table. */
1120 memset (isym
, 0, sizeof (*isym
));
1125 native
->u
.syment
.n_scnum
= output_section
->target_index
;
1126 native
->u
.syment
.n_value
= (symbol
->value
1127 + symbol
->section
->output_offset
);
1128 if (! obj_pe (abfd
))
1129 native
->u
.syment
.n_value
+= output_section
->vma
;
1131 /* Copy the any flags from the file header into the symbol.
1134 coff_symbol_type
*c
= coff_symbol_from (symbol
);
1135 if (c
!= (coff_symbol_type
*) NULL
)
1136 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1140 native
->u
.syment
.n_type
= 0;
1141 if (symbol
->flags
& BSF_FILE
)
1142 native
->u
.syment
.n_sclass
= C_FILE
;
1143 else if (symbol
->flags
& BSF_LOCAL
)
1144 native
->u
.syment
.n_sclass
= C_STAT
;
1145 else if (symbol
->flags
& BSF_WEAK
)
1146 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1148 native
->u
.syment
.n_sclass
= C_EXT
;
1150 ret
= coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1151 debug_string_section_p
, debug_string_size_p
);
1153 *isym
= native
->u
.syment
;
1157 /* Write a native symbol to a COFF file. */
1160 coff_write_native_symbol (bfd
*abfd
,
1161 coff_symbol_type
*symbol
,
1163 bfd_size_type
*string_size_p
,
1164 asection
**debug_string_section_p
,
1165 bfd_size_type
*debug_string_size_p
)
1167 combined_entry_type
*native
= symbol
->native
;
1168 alent
*lineno
= symbol
->lineno
;
1169 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1171 if ((!link_info
|| link_info
->strip_discarded
)
1172 && !bfd_is_abs_section (symbol
->symbol
.section
)
1173 && symbol
->symbol
.section
->output_section
== bfd_abs_section_ptr
)
1175 symbol
->symbol
.name
= "";
1179 BFD_ASSERT (native
->is_sym
);
1180 /* If this symbol has an associated line number, we must store the
1181 symbol index in the line number field. We also tag the auxent to
1182 point to the right place in the lineno table. */
1183 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1185 unsigned int count
= 0;
1187 lineno
[count
].u
.offset
= *written
;
1188 if (native
->u
.syment
.n_numaux
)
1190 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1192 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1193 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1196 /* Count and relocate all other linenumbers. */
1198 while (lineno
[count
].line_number
!= 0)
1200 lineno
[count
].u
.offset
+=
1201 (symbol
->symbol
.section
->output_section
->vma
1202 + symbol
->symbol
.section
->output_offset
);
1205 symbol
->done_lineno
= TRUE
;
1207 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1208 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1209 count
* bfd_coff_linesz (abfd
);
1212 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1213 string_size_p
, debug_string_section_p
,
1214 debug_string_size_p
);
1218 null_error_handler (const char * fmt ATTRIBUTE_UNUSED
, ...)
1222 /* Write out the COFF symbols. */
1225 coff_write_symbols (bfd
*abfd
)
1227 bfd_size_type string_size
;
1228 asection
*debug_string_section
;
1229 bfd_size_type debug_string_size
;
1231 unsigned int limit
= bfd_get_symcount (abfd
);
1232 bfd_vma written
= 0;
1236 debug_string_section
= NULL
;
1237 debug_string_size
= 0;
1239 /* If this target supports long section names, they must be put into
1240 the string table. This is supported by PE. This code must
1241 handle section names just as they are handled in
1242 coff_write_object_contents. */
1243 if (bfd_coff_long_section_names (abfd
))
1247 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1251 len
= strlen (o
->name
);
1253 string_size
+= len
+ 1;
1257 /* Seek to the right place. */
1258 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1261 /* Output all the symbols we have. */
1263 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1265 asymbol
*symbol
= *p
;
1266 coff_symbol_type
*c_symbol
= coff_symbol_from (symbol
);
1268 if (c_symbol
== (coff_symbol_type
*) NULL
1269 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1271 if (!coff_write_alien_symbol (abfd
, symbol
, NULL
, &written
,
1272 &string_size
, &debug_string_section
,
1273 &debug_string_size
))
1278 if (coff_backend_info (abfd
)->_bfd_coff_classify_symbol
!= NULL
)
1280 bfd_error_handler_type current_error_handler
;
1281 enum coff_symbol_classification sym_class
;
1282 unsigned char *n_sclass
;
1284 /* Suppress error reporting by bfd_coff_classify_symbol.
1285 Error messages can be generated when we are processing a local
1286 symbol which has no associated section and we do not have to
1287 worry about this, all we need to know is that it is local. */
1288 current_error_handler
= bfd_set_error_handler (null_error_handler
);
1289 BFD_ASSERT (c_symbol
->native
->is_sym
);
1290 sym_class
= bfd_coff_classify_symbol (abfd
,
1291 &c_symbol
->native
->u
.syment
);
1292 (void) bfd_set_error_handler (current_error_handler
);
1294 n_sclass
= &c_symbol
->native
->u
.syment
.n_sclass
;
1296 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1297 we cannot retain the existing sclass from the original symbol.
1298 Weak symbols only have one valid sclass, so just set it always.
1299 If it is not local class and should be, set it C_STAT.
1300 If it is global and not classified as global, or if it is
1301 weak (which is also classified as global), set it C_EXT. */
1303 if (symbol
->flags
& BSF_WEAK
)
1304 *n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1305 else if (symbol
->flags
& BSF_LOCAL
&& sym_class
!= COFF_SYMBOL_LOCAL
)
1307 else if (symbol
->flags
& BSF_GLOBAL
1308 && (sym_class
!= COFF_SYMBOL_GLOBAL
1310 || *n_sclass
== C_NT_WEAK
1312 || *n_sclass
== C_WEAKEXT
))
1313 c_symbol
->native
->u
.syment
.n_sclass
= C_EXT
;
1316 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1317 &string_size
, &debug_string_section
,
1318 &debug_string_size
))
1323 obj_raw_syment_count (abfd
) = written
;
1325 /* Now write out strings. */
1326 if (string_size
!= 0)
1328 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1329 bfd_byte buffer
[STRING_SIZE_SIZE
];
1331 #if STRING_SIZE_SIZE == 4
1332 H_PUT_32 (abfd
, size
, buffer
);
1334 #error Change H_PUT_32
1336 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1340 /* Handle long section names. This code must handle section
1341 names just as they are handled in coff_write_object_contents. */
1342 if (bfd_coff_long_section_names (abfd
))
1346 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1350 len
= strlen (o
->name
);
1353 if (bfd_bwrite (o
->name
, (bfd_size_type
) (len
+ 1), abfd
)
1360 for (p
= abfd
->outsymbols
, i
= 0;
1365 size_t name_length
= strlen (q
->name
);
1366 coff_symbol_type
*c_symbol
= coff_symbol_from (q
);
1369 /* Figure out whether the symbol name should go in the string
1370 table. Symbol names that are short enough are stored
1371 directly in the syment structure. File names permit a
1372 different, longer, length in the syment structure. On
1373 XCOFF, some symbol names are stored in the .debug section
1374 rather than in the string table. */
1376 if (c_symbol
== NULL
1377 || c_symbol
->native
== NULL
)
1378 /* This is not a COFF symbol, so it certainly is not a
1379 file name, nor does it go in the .debug section. */
1380 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1382 else if (! c_symbol
->native
->is_sym
)
1383 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1385 else if (bfd_coff_symname_in_debug (abfd
,
1386 &c_symbol
->native
->u
.syment
))
1387 /* This symbol name is in the XCOFF .debug section.
1388 Don't write it into the string table. */
1389 maxlen
= name_length
;
1391 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1392 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1394 if (bfd_coff_force_symnames_in_strings (abfd
))
1396 if (bfd_bwrite (".file", (bfd_size_type
) 6, abfd
) != 6)
1399 maxlen
= bfd_coff_filnmlen (abfd
);
1402 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1404 if (name_length
> maxlen
)
1406 if (bfd_bwrite ((void *) (q
->name
), (bfd_size_type
) name_length
+ 1,
1407 abfd
) != name_length
+ 1)
1414 /* We would normally not write anything here, but we'll write
1415 out 4 so that any stupid coff reader which tries to read the
1416 string table even when there isn't one won't croak. */
1417 unsigned int size
= STRING_SIZE_SIZE
;
1418 bfd_byte buffer
[STRING_SIZE_SIZE
];
1420 #if STRING_SIZE_SIZE == 4
1421 H_PUT_32 (abfd
, size
, buffer
);
1423 #error Change H_PUT_32
1425 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1426 != STRING_SIZE_SIZE
)
1430 /* Make sure the .debug section was created to be the correct size.
1431 We should create it ourselves on the fly, but we don't because
1432 BFD won't let us write to any section until we know how large all
1433 the sections are. We could still do it by making another pass
1434 over the symbols. FIXME. */
1435 BFD_ASSERT (debug_string_size
== 0
1436 || (debug_string_section
!= (asection
*) NULL
1437 && (BFD_ALIGN (debug_string_size
,
1438 1 << debug_string_section
->alignment_power
)
1439 == debug_string_section
->size
)));
1445 coff_write_linenumbers (bfd
*abfd
)
1448 bfd_size_type linesz
;
1451 linesz
= bfd_coff_linesz (abfd
);
1452 buff
= bfd_alloc (abfd
, linesz
);
1455 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1457 if (s
->lineno_count
)
1459 asymbol
**q
= abfd
->outsymbols
;
1460 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1462 /* Find all the linenumbers in this section. */
1466 if (p
->section
->output_section
== s
)
1469 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1470 (bfd_asymbol_bfd (p
), p
));
1473 /* Found a linenumber entry, output. */
1474 struct internal_lineno out
;
1476 memset ((void *) & out
, 0, sizeof (out
));
1478 out
.l_addr
.l_symndx
= l
->u
.offset
;
1479 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1480 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1484 while (l
->line_number
)
1486 out
.l_lnno
= l
->line_number
;
1487 out
.l_addr
.l_symndx
= l
->u
.offset
;
1488 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1489 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1500 bfd_release (abfd
, buff
);
1505 coff_get_lineno (bfd
*ignore_abfd ATTRIBUTE_UNUSED
, asymbol
*symbol
)
1507 return coffsymbol (symbol
)->lineno
;
1510 /* This function transforms the offsets into the symbol table into
1511 pointers to syments. */
1514 coff_pointerize_aux (bfd
*abfd
,
1515 combined_entry_type
*table_base
,
1516 combined_entry_type
*symbol
,
1517 unsigned int indaux
,
1518 combined_entry_type
*auxent
)
1520 unsigned int type
= symbol
->u
.syment
.n_type
;
1521 unsigned int n_sclass
= symbol
->u
.syment
.n_sclass
;
1523 BFD_ASSERT (symbol
->is_sym
);
1524 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1526 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1527 (abfd
, table_base
, symbol
, indaux
, auxent
))
1531 /* Don't bother if this is a file or a section. */
1532 if (n_sclass
== C_STAT
&& type
== T_NULL
)
1534 if (n_sclass
== C_FILE
)
1537 BFD_ASSERT (! auxent
->is_sym
);
1538 /* Otherwise patch up. */
1539 #define N_TMASK coff_data (abfd)->local_n_tmask
1540 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1542 if ((ISFCN (type
) || ISTAG (n_sclass
) || n_sclass
== C_BLOCK
1543 || n_sclass
== C_FCN
)
1544 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1546 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1547 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1548 auxent
->fix_end
= 1;
1550 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1551 generate one, so we must be careful to ignore it. */
1552 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1554 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1555 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1556 auxent
->fix_tag
= 1;
1560 /* Allocate space for the ".debug" section, and read it.
1561 We did not read the debug section until now, because
1562 we didn't want to go to the trouble until someone needed it. */
1565 build_debug_section (bfd
*abfd
, asection
** sect_return
)
1567 char *debug_section
;
1569 bfd_size_type sec_size
;
1571 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1575 bfd_set_error (bfd_error_no_debug_section
);
1579 sec_size
= sect
->size
;
1580 debug_section
= (char *) bfd_alloc (abfd
, sec_size
);
1581 if (debug_section
== NULL
)
1584 /* Seek to the beginning of the `.debug' section and read it.
1585 Save the current position first; it is needed by our caller.
1586 Then read debug section and reset the file pointer. */
1588 position
= bfd_tell (abfd
);
1589 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1590 || bfd_bread (debug_section
, sec_size
, abfd
) != sec_size
1591 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1594 * sect_return
= sect
;
1595 return debug_section
;
1598 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1599 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1600 be \0-terminated. */
1603 copy_name (bfd
*abfd
, char *name
, size_t maxlen
)
1608 for (len
= 0; len
< maxlen
; ++len
)
1609 if (name
[len
] == '\0')
1612 if ((newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1615 strncpy (newname
, name
, len
);
1616 newname
[len
] = '\0';
1620 /* Read in the external symbols. */
1623 _bfd_coff_get_external_symbols (bfd
*abfd
)
1625 bfd_size_type symesz
;
1629 if (obj_coff_external_syms (abfd
) != NULL
)
1632 symesz
= bfd_coff_symesz (abfd
);
1634 size
= obj_raw_syment_count (abfd
) * symesz
;
1638 syms
= bfd_malloc (size
);
1642 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1643 || bfd_bread (syms
, size
, abfd
) != size
)
1650 obj_coff_external_syms (abfd
) = syms
;
1655 /* Read in the external strings. The strings are not loaded until
1656 they are needed. This is because we have no simple way of
1657 detecting a missing string table in an archive. If the strings
1658 are loaded then the STRINGS and STRINGS_LEN fields in the
1659 coff_tdata structure will be set. */
1662 _bfd_coff_read_string_table (bfd
*abfd
)
1664 char extstrsize
[STRING_SIZE_SIZE
];
1665 bfd_size_type strsize
;
1669 if (obj_coff_strings (abfd
) != NULL
)
1670 return obj_coff_strings (abfd
);
1672 if (obj_sym_filepos (abfd
) == 0)
1674 bfd_set_error (bfd_error_no_symbols
);
1678 pos
= obj_sym_filepos (abfd
);
1679 pos
+= obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
);
1680 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1683 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1684 != sizeof extstrsize
)
1686 if (bfd_get_error () != bfd_error_file_truncated
)
1689 /* There is no string table. */
1690 strsize
= STRING_SIZE_SIZE
;
1694 #if STRING_SIZE_SIZE == 4
1695 strsize
= H_GET_32 (abfd
, extstrsize
);
1697 #error Change H_GET_32
1701 if (strsize
< STRING_SIZE_SIZE
)
1703 (*_bfd_error_handler
)
1704 (_("%B: bad string table size %lu"), abfd
, (unsigned long) strsize
);
1705 bfd_set_error (bfd_error_bad_value
);
1709 strings
= (char *) bfd_malloc (strsize
+ 1);
1710 if (strings
== NULL
)
1713 /* PR 17521 file: 079-54929-0.004.
1714 A corrupt file could contain an index that points into the first
1715 STRING_SIZE_SIZE bytes of the string table, so make sure that
1717 memset (strings
, 0, STRING_SIZE_SIZE
);
1719 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1720 != strsize
- STRING_SIZE_SIZE
)
1726 obj_coff_strings (abfd
) = strings
;
1727 obj_coff_strings_len (abfd
) = strsize
;
1728 /* Terminate the string table, just in case. */
1729 strings
[strsize
] = 0;
1733 /* Free up the external symbols and strings read from a COFF file. */
1736 _bfd_coff_free_symbols (bfd
*abfd
)
1738 if (obj_coff_external_syms (abfd
) != NULL
1739 && ! obj_coff_keep_syms (abfd
))
1741 free (obj_coff_external_syms (abfd
));
1742 obj_coff_external_syms (abfd
) = NULL
;
1744 if (obj_coff_strings (abfd
) != NULL
1745 && ! obj_coff_keep_strings (abfd
))
1747 free (obj_coff_strings (abfd
));
1748 obj_coff_strings (abfd
) = NULL
;
1749 obj_coff_strings_len (abfd
) = 0;
1754 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1755 knit the symbol names into a normalized form. By normalized here I
1756 mean that all symbols have an n_offset pointer that points to a null-
1757 terminated string. */
1759 combined_entry_type
*
1760 coff_get_normalized_symtab (bfd
*abfd
)
1762 combined_entry_type
*internal
;
1763 combined_entry_type
*internal_ptr
;
1764 combined_entry_type
*symbol_ptr
;
1765 combined_entry_type
*internal_end
;
1769 const char *string_table
= NULL
;
1770 asection
* debug_sec
= NULL
;
1771 char *debug_sec_data
= NULL
;
1774 if (obj_raw_syments (abfd
) != NULL
)
1775 return obj_raw_syments (abfd
);
1777 if (! _bfd_coff_get_external_symbols (abfd
))
1780 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1781 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1782 if (internal
== NULL
&& size
!= 0)
1784 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1786 raw_src
= (char *) obj_coff_external_syms (abfd
);
1788 /* Mark the end of the symbols. */
1789 symesz
= bfd_coff_symesz (abfd
);
1790 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1792 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1793 probably possible. If one shows up, it will probably kill us. */
1795 /* Swap all the raw entries. */
1796 for (internal_ptr
= internal
;
1798 raw_src
+= symesz
, internal_ptr
++)
1802 bfd_coff_swap_sym_in (abfd
, (void *) raw_src
,
1803 (void *) & internal_ptr
->u
.syment
);
1804 symbol_ptr
= internal_ptr
;
1805 internal_ptr
->is_sym
= TRUE
;
1807 /* PR 17512: file: 1353-1166-0.004. */
1808 if (symbol_ptr
->u
.syment
.n_sclass
== C_FILE
1809 && symbol_ptr
->u
.syment
.n_numaux
> 0
1810 && raw_src
+ symesz
+ symbol_ptr
->u
.syment
.n_numaux
1811 * sizeof (union internal_auxent
) >= raw_end
)
1813 bfd_release (abfd
, internal
);
1818 i
< symbol_ptr
->u
.syment
.n_numaux
;
1822 /* PR 17512: Prevent buffer overrun. */
1823 if (internal_ptr
>= internal_end
)
1825 bfd_release (abfd
, internal
);
1831 bfd_coff_swap_aux_in (abfd
, (void *) raw_src
,
1832 symbol_ptr
->u
.syment
.n_type
,
1833 symbol_ptr
->u
.syment
.n_sclass
,
1834 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1835 &(internal_ptr
->u
.auxent
));
1837 internal_ptr
->is_sym
= FALSE
;
1838 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1843 /* Free the raw symbols, but not the strings (if we have them). */
1844 obj_coff_keep_strings (abfd
) = TRUE
;
1845 if (! _bfd_coff_free_symbols (abfd
))
1848 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1851 BFD_ASSERT (internal_ptr
->is_sym
);
1853 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1854 && internal_ptr
->u
.syment
.n_numaux
> 0)
1856 combined_entry_type
* aux
= internal_ptr
+ 1;
1858 /* Make a file symbol point to the name in the auxent, since
1859 the text ".file" is redundant. */
1860 BFD_ASSERT (! aux
->is_sym
);
1862 if (aux
->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1864 /* The filename is a long one, point into the string table. */
1865 if (string_table
== NULL
)
1867 string_table
= _bfd_coff_read_string_table (abfd
);
1868 if (string_table
== NULL
)
1872 if ((bfd_size_type
)(aux
->u
.auxent
.x_file
.x_n
.x_offset
)
1873 >= obj_coff_strings_len (abfd
))
1874 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) _("<corrupt>");
1876 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1877 (bfd_hostptr_t
) (string_table
+ (aux
->u
.auxent
.x_file
.x_n
.x_offset
));
1881 /* Ordinary short filename, put into memory anyway. The
1882 Microsoft PE tools sometimes store a filename in
1883 multiple AUX entries. */
1884 if (internal_ptr
->u
.syment
.n_numaux
> 1
1885 && coff_data (abfd
)->pe
)
1886 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1889 aux
->u
.auxent
.x_file
.x_fname
,
1890 internal_ptr
->u
.syment
.n_numaux
* symesz
);
1892 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1895 aux
->u
.auxent
.x_file
.x_fname
,
1896 (size_t) bfd_coff_filnmlen (abfd
)));
1901 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1903 /* This is a "short" name. Make it long. */
1907 /* Find the length of this string without walking into memory
1909 for (i
= 0; i
< 8; ++i
)
1910 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1913 newstring
= (char *) bfd_zalloc (abfd
, (bfd_size_type
) (i
+ 1));
1914 if (newstring
== NULL
)
1916 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1917 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) newstring
;
1918 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1920 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1921 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) "";
1922 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1924 /* Long name already. Point symbol at the string in the
1926 if (string_table
== NULL
)
1928 string_table
= _bfd_coff_read_string_table (abfd
);
1929 if (string_table
== NULL
)
1932 if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
)
1933 || string_table
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
< string_table
)
1934 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) _("<corrupt>");
1936 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1939 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1943 /* Long name in debug section. Very similar. */
1944 if (debug_sec_data
== NULL
)
1945 debug_sec_data
= build_debug_section (abfd
, & debug_sec
);
1946 if (debug_sec_data
!= NULL
)
1948 BFD_ASSERT (debug_sec
!= NULL
);
1949 /* PR binutils/17512: Catch out of range offsets into the debug data. */
1950 if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
> debug_sec
->size
1951 || debug_sec_data
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
< debug_sec_data
)
1952 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) _("<corrupt>");
1954 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
)
1955 (debug_sec_data
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1958 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) "";
1961 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1964 obj_raw_syments (abfd
) = internal
;
1965 BFD_ASSERT (obj_raw_syment_count (abfd
)
1966 == (unsigned int) (internal_ptr
- internal
));
1972 coff_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
1974 if (bfd_get_format (abfd
) != bfd_object
)
1976 bfd_set_error (bfd_error_invalid_operation
);
1979 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1983 coff_make_empty_symbol (bfd
*abfd
)
1985 bfd_size_type amt
= sizeof (coff_symbol_type
);
1986 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
1988 if (new_symbol
== NULL
)
1990 new_symbol
->symbol
.section
= 0;
1991 new_symbol
->native
= NULL
;
1992 new_symbol
->lineno
= NULL
;
1993 new_symbol
->done_lineno
= FALSE
;
1994 new_symbol
->symbol
.the_bfd
= abfd
;
1996 return & new_symbol
->symbol
;
1999 /* Make a debugging symbol. */
2002 coff_bfd_make_debug_symbol (bfd
*abfd
,
2003 void * ptr ATTRIBUTE_UNUSED
,
2004 unsigned long sz ATTRIBUTE_UNUSED
)
2006 bfd_size_type amt
= sizeof (coff_symbol_type
);
2007 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
2009 if (new_symbol
== NULL
)
2011 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2012 (but shouldn't be a constant). */
2013 amt
= sizeof (combined_entry_type
) * 10;
2014 new_symbol
->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2015 if (!new_symbol
->native
)
2017 new_symbol
->native
->is_sym
= TRUE
;
2018 new_symbol
->symbol
.section
= bfd_abs_section_ptr
;
2019 new_symbol
->symbol
.flags
= BSF_DEBUGGING
;
2020 new_symbol
->lineno
= NULL
;
2021 new_symbol
->done_lineno
= FALSE
;
2022 new_symbol
->symbol
.the_bfd
= abfd
;
2024 return & new_symbol
->symbol
;
2028 coff_get_symbol_info (bfd
*abfd
, asymbol
*symbol
, symbol_info
*ret
)
2030 bfd_symbol_info (symbol
, ret
);
2032 if (coffsymbol (symbol
)->native
!= NULL
2033 && coffsymbol (symbol
)->native
->fix_value
2034 && coffsymbol (symbol
)->native
->is_sym
)
2035 ret
->value
= coffsymbol (symbol
)->native
->u
.syment
.n_value
-
2036 (bfd_hostptr_t
) obj_raw_syments (abfd
);
2039 /* Print out information about COFF symbol. */
2042 coff_print_symbol (bfd
*abfd
,
2045 bfd_print_symbol_type how
)
2047 FILE * file
= (FILE *) filep
;
2051 case bfd_print_symbol_name
:
2052 fprintf (file
, "%s", symbol
->name
);
2055 case bfd_print_symbol_more
:
2056 fprintf (file
, "coff %s %s",
2057 coffsymbol (symbol
)->native
? "n" : "g",
2058 coffsymbol (symbol
)->lineno
? "l" : " ");
2061 case bfd_print_symbol_all
:
2062 if (coffsymbol (symbol
)->native
)
2066 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2067 combined_entry_type
*root
= obj_raw_syments (abfd
);
2068 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2070 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2072 /* PR 17512: file: 079-33786-0.001:0.1. */
2073 if (combined
< obj_raw_syments (abfd
)
2074 || combined
>= obj_raw_syments (abfd
) + obj_raw_syment_count (abfd
))
2076 fprintf (file
, _("<corrupt info> %s"), symbol
->name
);
2080 BFD_ASSERT (combined
->is_sym
);
2081 if (! combined
->fix_value
)
2082 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2084 val
= combined
->u
.syment
.n_value
- (bfd_hostptr_t
) root
;
2086 fprintf (file
, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2087 combined
->u
.syment
.n_scnum
,
2088 combined
->u
.syment
.n_flags
,
2089 combined
->u
.syment
.n_type
,
2090 combined
->u
.syment
.n_sclass
,
2091 combined
->u
.syment
.n_numaux
);
2092 bfd_fprintf_vma (abfd
, file
, val
);
2093 fprintf (file
, " %s", symbol
->name
);
2095 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2097 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2100 BFD_ASSERT (! auxp
->is_sym
);
2102 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2104 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2106 fprintf (file
, "\n");
2108 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2111 switch (combined
->u
.syment
.n_sclass
)
2114 fprintf (file
, "File ");
2118 if (combined
->u
.syment
.n_type
== T_NULL
)
2119 /* Probably a section symbol ? */
2121 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2122 (unsigned long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2123 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2124 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2125 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2126 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2127 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2128 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2129 auxp
->u
.auxent
.x_scn
.x_checksum
,
2130 auxp
->u
.auxent
.x_scn
.x_associated
,
2131 auxp
->u
.auxent
.x_scn
.x_comdat
);
2134 /* Otherwise fall through. */
2137 if (ISFCN (combined
->u
.syment
.n_type
))
2142 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2145 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2146 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2148 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2150 (unsigned long) auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2154 /* Otherwise fall through. */
2156 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2157 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2158 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2161 fprintf (file
, " endndx %ld",
2163 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2171 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2173 while (l
->line_number
)
2175 if (l
->line_number
> 0)
2177 fprintf (file
, "\n%4d : ", l
->line_number
);
2178 bfd_fprintf_vma (abfd
, file
, l
->u
.offset
+ symbol
->section
->vma
);
2186 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
2187 fprintf (file
, " %-5s %s %s %s",
2188 symbol
->section
->name
,
2189 coffsymbol (symbol
)->native
? "n" : "g",
2190 coffsymbol (symbol
)->lineno
? "l" : " ",
2196 /* Return whether a symbol name implies a local symbol. In COFF,
2197 local symbols generally start with ``.L''. Most targets use this
2198 function for the is_local_label_name entry point, but some may
2202 _bfd_coff_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
2205 return name
[0] == '.' && name
[1] == 'L';
2208 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2209 section, calculate and return the name of the source file and the line
2210 nearest to the wanted location. */
2213 coff_find_nearest_line_with_names (bfd
*abfd
,
2217 const char **filename_ptr
,
2218 const char **functionname_ptr
,
2219 unsigned int *line_ptr
,
2220 const struct dwarf_debug_section
*debug_sections
)
2224 unsigned int line_base
;
2225 coff_data_type
*cof
= coff_data (abfd
);
2226 /* Run through the raw syments if available. */
2227 combined_entry_type
*p
;
2228 combined_entry_type
*pend
;
2230 struct coff_section_tdata
*sec_data
;
2233 /* Before looking through the symbol table, try to use a .stab
2234 section to find the information. */
2235 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2236 &found
, filename_ptr
,
2237 functionname_ptr
, line_ptr
,
2238 &coff_data(abfd
)->line_info
))
2244 /* Also try examining DWARF2 debugging information. */
2245 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
2246 filename_ptr
, functionname_ptr
,
2247 line_ptr
, NULL
, debug_sections
, 0,
2248 &coff_data(abfd
)->dwarf2_find_line_info
))
2251 /* If the DWARF lookup failed, but there is DWARF information available
2252 then the problem might be that the file has been rebased. This tool
2253 changes the VMAs of all the sections, but it does not update the DWARF
2254 information. So try again, using a bias against the address sought. */
2255 if (coff_data (abfd
)->dwarf2_find_line_info
!= NULL
)
2257 bfd_signed_vma bias
;
2259 bias
= _bfd_dwarf2_find_symbol_bias (symbols
,
2260 & coff_data (abfd
)->dwarf2_find_line_info
);
2263 && _bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
,
2265 filename_ptr
, functionname_ptr
,
2266 line_ptr
, NULL
, debug_sections
, 0,
2267 &coff_data(abfd
)->dwarf2_find_line_info
))
2272 *functionname_ptr
= 0;
2275 /* Don't try and find line numbers in a non coff file. */
2276 if (!bfd_family_coff (abfd
))
2282 /* Find the first C_FILE symbol. */
2283 p
= cof
->raw_syments
;
2287 pend
= p
+ cof
->raw_syment_count
;
2290 BFD_ASSERT (p
->is_sym
);
2291 if (p
->u
.syment
.n_sclass
== C_FILE
)
2293 p
+= 1 + p
->u
.syment
.n_numaux
;
2301 /* Look through the C_FILE symbols to find the best one. */
2302 sec_vma
= bfd_get_section_vma (abfd
, section
);
2303 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2304 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2308 combined_entry_type
*p2
;
2310 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2312 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2314 BFD_ASSERT (p2
->is_sym
);
2315 if (p2
->u
.syment
.n_scnum
> 0
2317 == coff_section_from_bfd_index (abfd
,
2318 p2
->u
.syment
.n_scnum
)))
2320 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2329 file_addr
= (bfd_vma
) p2
->u
.syment
.n_value
;
2330 /* PR 11512: Include the section address of the function name symbol. */
2331 if (p2
->u
.syment
.n_scnum
> 0)
2332 file_addr
+= coff_section_from_bfd_index (abfd
,
2333 p2
->u
.syment
.n_scnum
)->vma
;
2334 /* We use <= MAXDIFF here so that if we get a zero length
2335 file, we actually use the next file entry. */
2337 && offset
+ sec_vma
>= file_addr
2338 && offset
+ sec_vma
- file_addr
<= maxdiff
)
2340 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2341 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2344 /* Avoid endless loops on erroneous files by ensuring that
2345 we always move forward in the file. */
2346 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2349 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2350 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2355 /* Now wander though the raw linenumbers of the section. */
2356 /* If we have been called on this section before, and the offset we
2357 want is further down then we can prime the lookup loop. */
2358 sec_data
= coff_section_data (abfd
, section
);
2359 if (sec_data
!= NULL
2361 && offset
>= sec_data
->offset
)
2364 *functionname_ptr
= sec_data
->function
;
2365 line_base
= sec_data
->line_base
;
2373 if (section
->lineno
!= NULL
)
2375 bfd_vma last_value
= 0;
2377 l
= §ion
->lineno
[i
];
2379 for (; i
< section
->lineno_count
; i
++)
2381 if (l
->line_number
== 0)
2383 /* Get the symbol this line number points at. */
2384 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2385 if (coff
->symbol
.value
> offset
)
2387 *functionname_ptr
= coff
->symbol
.name
;
2388 last_value
= coff
->symbol
.value
;
2391 combined_entry_type
*s
= coff
->native
;
2393 BFD_ASSERT (s
->is_sym
);
2394 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2396 /* In XCOFF a debugging symbol can follow the
2398 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2399 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2401 /* S should now point to the .bf of the function. */
2402 if (s
->u
.syment
.n_numaux
)
2404 /* The linenumber is stored in the auxent. */
2405 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2407 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2408 *line_ptr
= line_base
;
2414 if (l
->u
.offset
> offset
)
2416 *line_ptr
= l
->line_number
+ line_base
- 1;
2421 /* If we fell off the end of the loop, then assume that this
2422 symbol has no line number info. Otherwise, symbols with no
2423 line number info get reported with the line number of the
2424 last line of the last symbol which does have line number
2425 info. We use 0x100 as a slop to account for cases where the
2426 last line has executable code. */
2427 if (i
>= section
->lineno_count
2429 && offset
- last_value
> 0x100)
2431 *functionname_ptr
= NULL
;
2436 /* Cache the results for the next call. */
2437 if (sec_data
== NULL
&& section
->owner
== abfd
)
2439 amt
= sizeof (struct coff_section_tdata
);
2440 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2441 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2443 if (sec_data
!= NULL
)
2445 sec_data
->offset
= offset
;
2446 sec_data
->i
= i
- 1;
2447 sec_data
->function
= *functionname_ptr
;
2448 sec_data
->line_base
= line_base
;
2455 coff_find_nearest_line (bfd
*abfd
,
2459 const char **filename_ptr
,
2460 const char **functionname_ptr
,
2461 unsigned int *line_ptr
,
2462 unsigned int *discriminator_ptr
)
2464 if (discriminator_ptr
)
2465 *discriminator_ptr
= 0;
2466 return coff_find_nearest_line_with_names (abfd
, symbols
, section
, offset
,
2467 filename_ptr
, functionname_ptr
,
2468 line_ptr
, dwarf_debug_sections
);
2472 coff_find_inliner_info (bfd
*abfd
,
2473 const char **filename_ptr
,
2474 const char **functionname_ptr
,
2475 unsigned int *line_ptr
)
2479 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
2480 functionname_ptr
, line_ptr
,
2481 &coff_data(abfd
)->dwarf2_find_line_info
);
2486 coff_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2490 if (!info
->relocatable
)
2491 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2493 size
= bfd_coff_filhsz (abfd
);
2495 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2499 /* Change the class of a coff symbol held by BFD. */
2502 bfd_coff_set_symbol_class (bfd
* abfd
,
2504 unsigned int symbol_class
)
2506 coff_symbol_type
* csym
;
2508 csym
= coff_symbol_from (symbol
);
2511 bfd_set_error (bfd_error_invalid_operation
);
2514 else if (csym
->native
== NULL
)
2516 /* This is an alien symbol which no native coff backend data.
2517 We cheat here by creating a fake native entry for it and
2518 then filling in the class. This code is based on that in
2519 coff_write_alien_symbol(). */
2521 combined_entry_type
* native
;
2522 bfd_size_type amt
= sizeof (* native
);
2524 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2528 native
->is_sym
= TRUE
;
2529 native
->u
.syment
.n_type
= T_NULL
;
2530 native
->u
.syment
.n_sclass
= symbol_class
;
2532 if (bfd_is_und_section (symbol
->section
))
2534 native
->u
.syment
.n_scnum
= N_UNDEF
;
2535 native
->u
.syment
.n_value
= symbol
->value
;
2537 else if (bfd_is_com_section (symbol
->section
))
2539 native
->u
.syment
.n_scnum
= N_UNDEF
;
2540 native
->u
.syment
.n_value
= symbol
->value
;
2544 native
->u
.syment
.n_scnum
=
2545 symbol
->section
->output_section
->target_index
;
2546 native
->u
.syment
.n_value
= (symbol
->value
2547 + symbol
->section
->output_offset
);
2548 if (! obj_pe (abfd
))
2549 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2551 /* Copy the any flags from the file header into the symbol.
2553 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2556 csym
->native
= native
;
2559 csym
->native
->u
.syment
.n_sclass
= symbol_class
;
2565 _bfd_coff_section_already_linked (bfd
*abfd
,
2567 struct bfd_link_info
*info
)
2570 const char *name
, *key
;
2571 struct bfd_section_already_linked
*l
;
2572 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
2573 struct coff_comdat_info
*s_comdat
;
2576 if ((flags
& SEC_LINK_ONCE
) == 0)
2579 /* The COFF backend linker doesn't support group sections. */
2580 if ((flags
& SEC_GROUP
) != 0)
2583 name
= bfd_get_section_name (abfd
, sec
);
2584 s_comdat
= bfd_coff_get_comdat_section (abfd
, sec
);
2586 if (s_comdat
!= NULL
)
2587 key
= s_comdat
->name
;
2590 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
2591 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
2594 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2595 .xdata$<key> and .pdata$<key> only the first of which has a
2596 comdat key. Should these all match the LTO IR key? */
2600 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
2602 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
2604 struct coff_comdat_info
*l_comdat
;
2606 l_comdat
= bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
);
2608 /* The section names must match, and both sections must be
2609 comdat and have the same comdat name, or both sections must
2610 be non-comdat. LTO IR plugin sections are an exception. They
2611 are always named .gnu.linkonce.t.<key> (<key> is some string)
2612 and match any comdat section with comdat name of <key>, and
2613 any linkonce section with the same suffix, ie.
2614 .gnu.linkonce.*.<key>. */
2615 if (((s_comdat
!= NULL
) == (l_comdat
!= NULL
)
2616 && strcmp (name
, l
->sec
->name
) == 0)
2617 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
2619 /* The section has already been linked. See if we should
2621 return _bfd_handle_already_linked (sec
, l
, info
);
2625 /* This is the first section with this name. Record it. */
2626 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
2627 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));