1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2024 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
43 #include "coff/internal.h"
47 /* Extract a long section name at STRINDEX and copy it to the bfd objstack.
48 Return NULL in case of error. */
51 extract_long_section_name(bfd
*abfd
, unsigned long strindex
)
56 strings
= _bfd_coff_read_string_table (abfd
);
59 if ((bfd_size_type
)(strindex
+ 2) >= obj_coff_strings_len (abfd
))
62 name
= (char *) bfd_alloc (abfd
, (bfd_size_type
) strlen (strings
) + 1);
65 strcpy (name
, strings
);
70 /* Decode a base 64 coded string at STR of length LEN, and write the result
71 to RES. Return true on success.
72 Return false in case of invalid character or overflow. */
75 decode_base64 (const char *str
, unsigned len
, uint32_t *res
)
81 for (i
= 0; i
< len
; i
++)
86 if (c
>= 'A' && c
<= 'Z')
88 else if (c
>= 'a' && c
<= 'z')
90 else if (c
>= '0' && c
<= '9')
99 /* Check for overflow. */
100 if ((val
>> 26) != 0)
103 val
= (val
<< 6) + d
;
110 /* Take a section header read from a coff file (in HOST byte order),
111 and make a BFD "section" out of it. This is used by ECOFF. */
114 make_a_section_from_file (bfd
*abfd
,
115 struct internal_scnhdr
*hdr
,
116 unsigned int target_index
)
125 /* Handle long section names as in PE. On reading, we want to
126 accept long names if the format permits them at all, regardless
127 of the current state of the flag that dictates if we would generate
128 them in outputs; this construct checks if that is the case by
129 attempting to set the flag, without changing its state; the call
130 will fail for formats that do not support long names at all. */
131 if (bfd_coff_set_long_section_names (abfd
, bfd_coff_long_section_names (abfd
))
132 && hdr
->s_name
[0] == '/')
134 /* Flag that this BFD uses long names, even though the format might
135 expect them to be off by default. This won't directly affect the
136 format of any output BFD created from this one, but the information
137 can be used to decide what to do. */
138 bfd_coff_set_long_section_names (abfd
, true);
140 if (hdr
->s_name
[1] == '/')
142 /* LLVM extension: the '/' is followed by another '/' and then by
143 the index in the strtab encoded in base64 without NUL at the
147 /* Decode the index. No overflow is expected as the string table
148 length is at most 2^32 - 1 (the length is written on the first
150 Also, contrary to RFC 4648, all the characters must be decoded,
151 there is no padding. */
152 if (!decode_base64 (hdr
->s_name
+ 2, SCNNMLEN
- 2, &strindex
))
155 name
= extract_long_section_name (abfd
, strindex
);
161 /* PE classic long section name. The '/' is followed by the index
162 in the strtab. The index is formatted as a decimal string. */
167 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
168 buf
[SCNNMLEN
- 1] = '\0';
169 strindex
= strtol (buf
, &p
, 10);
170 if (*p
== '\0' && strindex
>= 0)
172 name
= extract_long_section_name (abfd
, strindex
);
181 /* Assorted wastage to null-terminate the name, thanks AT&T! */
182 name
= (char *) bfd_alloc (abfd
,
183 (bfd_size_type
) sizeof (hdr
->s_name
) + 1 + 1);
186 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
187 name
[sizeof (hdr
->s_name
)] = 0;
190 newsect
= bfd_make_section_anyway (abfd
, name
);
194 newsect
->vma
= hdr
->s_vaddr
;
195 newsect
->lma
= hdr
->s_paddr
;
196 newsect
->size
= hdr
->s_size
;
197 newsect
->filepos
= hdr
->s_scnptr
;
198 newsect
->rel_filepos
= hdr
->s_relptr
;
199 newsect
->reloc_count
= hdr
->s_nreloc
;
201 bfd_coff_set_alignment_hook (abfd
, newsect
, hdr
);
203 newsect
->line_filepos
= hdr
->s_lnnoptr
;
205 newsect
->lineno_count
= hdr
->s_nlnno
;
206 newsect
->userdata
= NULL
;
207 newsect
->next
= NULL
;
208 newsect
->target_index
= target_index
;
210 if (!bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, newsect
, &flags
))
213 /* At least on i386-coff, the line number count for a shared library
214 section must be ignored. */
215 if ((flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
216 newsect
->lineno_count
= 0;
218 if (hdr
->s_nreloc
!= 0)
220 /* FIXME: should this check 'hdr->s_size > 0'. */
221 if (hdr
->s_scnptr
!= 0)
222 flags
|= SEC_HAS_CONTENTS
;
224 newsect
->flags
= flags
;
226 /* Compress/decompress DWARF debug sections. */
227 if ((flags
& SEC_DEBUGGING
) != 0
228 && (flags
& SEC_HAS_CONTENTS
) != 0
229 && (startswith (name
, ".debug_")
230 || startswith (name
, ".zdebug_")
231 || startswith (name
, ".gnu.debuglto_.debug_")
232 || startswith (name
, ".gnu.linkonce.wi.")))
234 enum { nothing
, compress
, decompress
} action
= nothing
;
236 if (bfd_is_section_compressed (abfd
, newsect
))
238 /* Compressed section. Check if we should decompress. */
239 if ((abfd
->flags
& BFD_DECOMPRESS
))
244 /* Normal section. Check if we should compress. */
245 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
249 if (action
== compress
)
251 if (!bfd_init_section_compress_status (abfd
, newsect
))
254 /* xgettext:c-format */
255 (_("%pB: unable to compress section %s"), abfd
, name
);
259 else if (action
== decompress
)
261 if (!bfd_init_section_decompress_status (abfd
, newsect
))
264 /* xgettext:c-format */
265 (_("%pB: unable to decompress section %s"), abfd
, name
);
268 if (abfd
->is_linker_input
271 /* Rename section from .zdebug_* to .debug_* so that ld
272 scripts will see this section as a debug section. */
273 char *new_name
= bfd_zdebug_name_to_debug (abfd
, name
);
274 if (new_name
== NULL
)
276 bfd_rename_section (newsect
, new_name
);
285 coff_object_cleanup (bfd
*abfd
)
287 struct coff_tdata
*td
= coff_data (abfd
);
290 if (td
->section_by_index
)
291 htab_delete (td
->section_by_index
);
292 if (td
->section_by_target_index
)
293 htab_delete (td
->section_by_target_index
);
294 if (obj_pe (abfd
) && pe_data (abfd
)->comdat_hash
)
295 htab_delete (pe_data (abfd
)->comdat_hash
);
299 /* Read in a COFF object and make it into a BFD. This is used by
302 coff_real_object_p (bfd
*abfd
,
304 struct internal_filehdr
*internal_f
,
305 struct internal_aouthdr
*internal_a
)
307 flagword oflags
= abfd
->flags
;
308 bfd_vma ostart
= bfd_get_start_address (abfd
);
311 bfd_size_type readsize
; /* Length of file_info. */
313 char *external_sections
;
315 if (!(internal_f
->f_flags
& F_RELFLG
))
316 abfd
->flags
|= HAS_RELOC
;
317 if ((internal_f
->f_flags
& F_EXEC
))
318 abfd
->flags
|= EXEC_P
;
319 if (!(internal_f
->f_flags
& F_LNNO
))
320 abfd
->flags
|= HAS_LINENO
;
321 if (!(internal_f
->f_flags
& F_LSYMS
))
322 abfd
->flags
|= HAS_LOCALS
;
324 /* FIXME: How can we set D_PAGED correctly? */
325 if ((internal_f
->f_flags
& F_EXEC
) != 0)
326 abfd
->flags
|= D_PAGED
;
328 abfd
->symcount
= internal_f
->f_nsyms
;
329 if (internal_f
->f_nsyms
)
330 abfd
->flags
|= HAS_SYMS
;
332 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
333 abfd
->start_address
= internal_a
->entry
;
335 abfd
->start_address
= 0;
337 /* Set up the tdata area. ECOFF uses its own routine, and overrides
339 tdata_save
= abfd
->tdata
.any
;
340 tdata
= bfd_coff_mkobject_hook (abfd
, (void *) internal_f
, (void *) internal_a
);
344 scnhsz
= bfd_coff_scnhsz (abfd
);
345 readsize
= (bfd_size_type
) nscns
* scnhsz
;
346 external_sections
= (char *) _bfd_alloc_and_read (abfd
, readsize
, readsize
);
347 if (!external_sections
)
350 /* Set the arch/mach *before* swapping in sections; section header swapping
351 may depend on arch/mach info. */
352 if (! bfd_coff_set_arch_mach_hook (abfd
, (void *) internal_f
))
355 /* Now copy data as required; construct all asections etc. */
359 for (i
= 0; i
< nscns
; i
++)
361 struct internal_scnhdr tmp
;
362 bfd_coff_swap_scnhdr_in (abfd
,
363 (void *) (external_sections
+ i
* scnhsz
),
365 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
370 _bfd_coff_free_symbols (abfd
);
371 return coff_object_cleanup
;
374 coff_object_cleanup (abfd
);
375 _bfd_coff_free_symbols (abfd
);
376 bfd_release (abfd
, tdata
);
378 abfd
->tdata
.any
= tdata_save
;
379 abfd
->flags
= oflags
;
380 abfd
->start_address
= ostart
;
384 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
385 not a COFF file. This is also used by ECOFF. */
388 coff_object_p (bfd
*abfd
)
390 bfd_size_type filhsz
;
391 bfd_size_type aoutsz
;
394 struct internal_filehdr internal_f
;
395 struct internal_aouthdr internal_a
;
397 /* Figure out how much to read. */
398 filhsz
= bfd_coff_filhsz (abfd
);
399 aoutsz
= bfd_coff_aoutsz (abfd
);
401 filehdr
= _bfd_alloc_and_read (abfd
, filhsz
, filhsz
);
404 if (bfd_get_error () != bfd_error_system_call
)
405 bfd_set_error (bfd_error_wrong_format
);
408 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
409 bfd_release (abfd
, filehdr
);
411 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
412 (less than aoutsz) used in object files and AOUTSZ (equal to
413 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
414 expects this header to be aoutsz bytes in length, so we use that
415 value in the call to bfd_alloc below. But we must be careful to
416 only read in f_opthdr bytes in the call to bfd_read. We should
417 also attempt to catch corrupt or non-COFF binaries with a strange
418 value for f_opthdr. */
419 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
420 || internal_f
.f_opthdr
> aoutsz
)
422 bfd_set_error (bfd_error_wrong_format
);
425 nscns
= internal_f
.f_nscns
;
427 if (internal_f
.f_opthdr
)
431 opthdr
= _bfd_alloc_and_read (abfd
, aoutsz
, internal_f
.f_opthdr
);
434 /* PR 17512: file: 11056-1136-0.004. */
435 if (internal_f
.f_opthdr
< aoutsz
)
436 memset (((char *) opthdr
) + internal_f
.f_opthdr
, 0,
437 aoutsz
- internal_f
.f_opthdr
);
439 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (void *) &internal_a
);
440 bfd_release (abfd
, opthdr
);
443 return coff_real_object_p (abfd
, nscns
, &internal_f
,
444 (internal_f
.f_opthdr
!= 0
446 : (struct internal_aouthdr
*) NULL
));
450 htab_hash_section_target_index (const void * entry
)
452 const struct bfd_section
* sec
= entry
;
453 return sec
->target_index
;
457 htab_eq_section_target_index (const void * e1
, const void * e2
)
459 const struct bfd_section
* sec1
= e1
;
460 const struct bfd_section
* sec2
= e2
;
461 return sec1
->target_index
== sec2
->target_index
;
464 /* Get the BFD section from a COFF symbol section number. */
467 coff_section_from_bfd_index (bfd
*abfd
, int section_index
)
469 if (section_index
== N_ABS
)
470 return bfd_abs_section_ptr
;
471 if (section_index
== N_UNDEF
)
472 return bfd_und_section_ptr
;
473 if (section_index
== N_DEBUG
)
474 return bfd_abs_section_ptr
;
476 struct bfd_section
*answer
;
477 htab_t table
= coff_data (abfd
)->section_by_target_index
;
481 table
= htab_create (10, htab_hash_section_target_index
,
482 htab_eq_section_target_index
, NULL
);
484 return bfd_und_section_ptr
;
485 coff_data (abfd
)->section_by_target_index
= table
;
488 if (htab_elements (table
) == 0)
490 for (answer
= abfd
->sections
; answer
; answer
= answer
->next
)
492 void **slot
= htab_find_slot (table
, answer
, INSERT
);
494 return bfd_und_section_ptr
;
499 struct bfd_section needle
;
500 needle
.target_index
= section_index
;
502 answer
= htab_find (table
, &needle
);
506 /* Cover the unlikely case of sections added after the first call to
508 for (answer
= abfd
->sections
; answer
; answer
= answer
->next
)
509 if (answer
->target_index
== section_index
)
511 void **slot
= htab_find_slot (table
, answer
, INSERT
);
517 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
518 has a bad symbol table in biglitpow.o. */
519 return bfd_und_section_ptr
;
522 /* Get the upper bound of a COFF symbol table. */
525 coff_get_symtab_upper_bound (bfd
*abfd
)
527 if (!bfd_coff_slurp_symbol_table (abfd
))
530 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
533 /* Canonicalize a COFF symbol table. */
536 coff_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
538 unsigned int counter
;
539 coff_symbol_type
*symbase
;
540 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
542 if (!bfd_coff_slurp_symbol_table (abfd
))
545 symbase
= obj_symbols (abfd
);
546 counter
= bfd_get_symcount (abfd
);
547 while (counter
-- > 0)
548 *location
++ = symbase
++;
552 return bfd_get_symcount (abfd
);
555 /* Get the name of a symbol. The caller must pass in a buffer of size
559 _bfd_coff_internal_syment_name (bfd
*abfd
,
560 const struct internal_syment
*sym
,
563 /* FIXME: It's not clear this will work correctly if sizeof
565 if (sym
->_n
._n_n
._n_zeroes
!= 0
566 || sym
->_n
._n_n
._n_offset
== 0)
568 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
569 buf
[SYMNMLEN
] = '\0';
576 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
577 strings
= obj_coff_strings (abfd
);
580 strings
= _bfd_coff_read_string_table (abfd
);
584 if (sym
->_n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
))
586 return strings
+ sym
->_n
._n_n
._n_offset
;
590 /* Read in and swap the relocs. This returns a buffer holding the
591 relocs for section SEC in file ABFD. If CACHE is TRUE and
592 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
593 the function is called again. If EXTERNAL_RELOCS is not NULL, it
594 is a buffer large enough to hold the unswapped relocs. If
595 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
596 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
597 value must be INTERNAL_RELOCS. The function returns NULL on error. */
599 struct internal_reloc
*
600 _bfd_coff_read_internal_relocs (bfd
*abfd
,
603 bfd_byte
*external_relocs
,
604 bool require_internal
,
605 struct internal_reloc
*internal_relocs
)
608 bfd_byte
*free_external
= NULL
;
609 struct internal_reloc
*free_internal
= NULL
;
612 struct internal_reloc
*irel
;
615 if (sec
->reloc_count
== 0)
616 return internal_relocs
; /* Nothing to do. */
618 if (coff_section_data (abfd
, sec
) != NULL
619 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
621 if (! require_internal
)
622 return coff_section_data (abfd
, sec
)->relocs
;
623 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
624 sec
->reloc_count
* sizeof (struct internal_reloc
));
625 return internal_relocs
;
628 relsz
= bfd_coff_relsz (abfd
);
630 amt
= sec
->reloc_count
* relsz
;
631 if (external_relocs
== NULL
)
633 free_external
= (bfd_byte
*) bfd_malloc (amt
);
634 if (free_external
== NULL
)
636 external_relocs
= free_external
;
639 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
640 || bfd_read (external_relocs
, amt
, abfd
) != amt
)
643 if (internal_relocs
== NULL
)
645 amt
= sec
->reloc_count
;
646 amt
*= sizeof (struct internal_reloc
);
647 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
648 if (free_internal
== NULL
)
650 internal_relocs
= free_internal
;
653 /* Swap in the relocs. */
654 erel
= external_relocs
;
655 erel_end
= erel
+ relsz
* sec
->reloc_count
;
656 irel
= internal_relocs
;
657 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
658 bfd_coff_swap_reloc_in (abfd
, (void *) erel
, (void *) irel
);
660 free (free_external
);
661 free_external
= NULL
;
663 if (cache
&& free_internal
!= NULL
)
665 if (coff_section_data (abfd
, sec
) == NULL
)
667 amt
= sizeof (struct coff_section_tdata
);
668 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
669 if (sec
->used_by_bfd
== NULL
)
671 coff_section_data (abfd
, sec
)->contents
= NULL
;
673 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
676 return internal_relocs
;
679 free (free_external
);
680 free (free_internal
);
684 /* Set lineno_count for the output sections of a COFF file. */
687 coff_count_linenumbers (bfd
*abfd
)
689 unsigned int limit
= bfd_get_symcount (abfd
);
697 /* This may be from the backend linker, in which case the
698 lineno_count in the sections is correct. */
699 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
700 total
+= s
->lineno_count
;
704 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
705 BFD_ASSERT (s
->lineno_count
== 0);
707 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
709 asymbol
*q_maybe
= *p
;
711 if (bfd_asymbol_bfd (q_maybe
) != NULL
712 && bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
714 coff_symbol_type
*q
= coffsymbol (q_maybe
);
716 /* The AIX 4.1 compiler can sometimes generate line numbers
717 attached to debugging symbols. We try to simply ignore
719 if (q
->lineno
!= NULL
720 && q
->symbol
.section
->owner
!= NULL
)
722 /* This symbol has line numbers. Increment the owning
723 section's linenumber count. */
724 alent
*l
= q
->lineno
;
728 asection
* sec
= q
->symbol
.section
->output_section
;
730 /* Do not try to update fields in read-only sections. */
731 if (! bfd_is_const_section (sec
))
732 sec
->lineno_count
++;
737 while (l
->line_number
!= 0);
746 fixup_symbol_value (bfd
*abfd
,
747 coff_symbol_type
*coff_symbol_ptr
,
748 struct internal_syment
*syment
)
750 /* Normalize the symbol flags. */
751 if (coff_symbol_ptr
->symbol
.section
752 && bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
754 /* A common symbol is undefined with a value. */
755 syment
->n_scnum
= N_UNDEF
;
756 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
758 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
759 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
761 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
763 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
765 syment
->n_scnum
= N_UNDEF
;
768 /* FIXME: Do we need to handle the absolute section here? */
771 if (coff_symbol_ptr
->symbol
.section
)
774 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
776 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
777 + coff_symbol_ptr
->symbol
.section
->output_offset
);
780 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
781 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
782 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
788 /* This can happen, but I don't know why yet (steve@cygnus.com) */
789 syment
->n_scnum
= N_ABS
;
790 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
795 /* Run through all the symbols in the symbol table and work out what
796 their indexes into the symbol table will be when output.
798 Coff requires that each C_FILE symbol points to the next one in the
799 chain, and that the last one points to the first external symbol. We
803 coff_renumber_symbols (bfd
*bfd_ptr
, int *first_undef
)
805 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
806 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
807 unsigned int native_index
= 0;
808 struct internal_syment
*last_file
= NULL
;
809 unsigned int symbol_index
;
811 /* COFF demands that undefined symbols come after all other symbols.
812 Since we don't need to impose this extra knowledge on all our
813 client programs, deal with that here. Sort the symbol table;
814 just move the undefined symbols to the end, leaving the rest
815 alone. The O'Reilly book says that defined global symbols come
816 at the end before the undefined symbols, so we do that here as
818 /* @@ Do we have some condition we could test for, so we don't always
819 have to do this? I don't think relocatability is quite right, but
820 I'm not certain. [raeburn:19920508.1711EST] */
826 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
827 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
830 bfd_ptr
->outsymbols
= newsyms
;
831 for (i
= 0; i
< symbol_count
; i
++)
832 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
833 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
834 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
835 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
836 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
838 *newsyms
++ = symbol_ptr_ptr
[i
];
840 for (i
= 0; i
< symbol_count
; i
++)
841 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
842 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
843 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
844 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
845 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
847 *newsyms
++ = symbol_ptr_ptr
[i
];
849 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
851 for (i
= 0; i
< symbol_count
; i
++)
852 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
853 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
854 *newsyms
++ = symbol_ptr_ptr
[i
];
855 *newsyms
= (asymbol
*) NULL
;
856 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
859 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
861 coff_symbol_type
*coff_symbol_ptr
;
863 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
864 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
865 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
867 combined_entry_type
*s
= coff_symbol_ptr
->native
;
870 BFD_ASSERT (s
->is_sym
);
871 if (s
->u
.syment
.n_sclass
== C_FILE
)
873 if (last_file
!= NULL
)
874 last_file
->n_value
= native_index
;
875 last_file
= &(s
->u
.syment
);
878 /* Modify the symbol values according to their section and
880 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
882 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
883 s
[i
].offset
= native_index
++;
889 obj_conv_table_size (bfd_ptr
) = native_index
;
894 /* Run thorough the symbol table again, and fix it so that all
895 pointers to entries are changed to the entries' index in the output
899 coff_mangle_symbols (bfd
*bfd_ptr
)
901 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
902 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
903 unsigned int symbol_index
;
905 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
907 coff_symbol_type
*coff_symbol_ptr
;
909 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
910 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
913 combined_entry_type
*s
= coff_symbol_ptr
->native
;
915 BFD_ASSERT (s
->is_sym
);
918 /* FIXME: We should use a union here. */
919 s
->u
.syment
.n_value
=
920 (uintptr_t) ((combined_entry_type
*)
921 (uintptr_t) s
->u
.syment
.n_value
)->offset
;
926 /* The value is the offset into the line number entries
927 for the symbol's section. On output, the symbol's
928 section should be N_DEBUG. */
929 s
->u
.syment
.n_value
=
930 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
931 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
932 coff_symbol_ptr
->symbol
.section
=
933 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
934 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
936 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
938 combined_entry_type
*a
= s
+ i
+ 1;
940 BFD_ASSERT (! a
->is_sym
);
943 a
->u
.auxent
.x_sym
.x_tagndx
.u32
=
944 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
949 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
=
950 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
955 a
->u
.auxent
.x_csect
.x_scnlen
.u64
=
956 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
965 coff_write_auxent_fname (bfd
*abfd
,
967 union internal_auxent
*auxent
,
968 struct bfd_strtab_hash
*strtab
,
971 unsigned int str_length
= strlen (str
);
972 unsigned int filnmlen
= bfd_coff_filnmlen (abfd
);
974 if (bfd_coff_long_filenames (abfd
))
976 if (str_length
<= filnmlen
)
977 strncpy (auxent
->x_file
.x_n
.x_fname
, str
, filnmlen
);
980 bfd_size_type indx
= _bfd_stringtab_add (strtab
, str
, hash
, false);
982 if (indx
== (bfd_size_type
) -1)
985 auxent
->x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
986 auxent
->x_file
.x_n
.x_n
.x_zeroes
= 0;
991 strncpy (auxent
->x_file
.x_n
.x_fname
, str
, filnmlen
);
992 if (str_length
> filnmlen
)
993 str
[filnmlen
] = '\0';
1000 coff_fix_symbol_name (bfd
*abfd
,
1002 combined_entry_type
*native
,
1003 struct bfd_strtab_hash
*strtab
,
1005 asection
**debug_string_section_p
,
1006 bfd_size_type
*debug_string_size_p
)
1008 unsigned int name_length
;
1009 char *name
= (char *) (symbol
->name
);
1014 /* COFF symbols always have names, so we'll make one up. */
1015 symbol
->name
= "strange";
1016 name
= (char *) symbol
->name
;
1018 name_length
= strlen (name
);
1020 BFD_ASSERT (native
->is_sym
);
1021 if (native
->u
.syment
.n_sclass
== C_FILE
1022 && native
->u
.syment
.n_numaux
> 0)
1024 if (bfd_coff_force_symnames_in_strings (abfd
))
1026 indx
= _bfd_stringtab_add (strtab
, ".file", hash
, false);
1027 if (indx
== (bfd_size_type
) -1)
1030 native
->u
.syment
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1031 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1034 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
1036 BFD_ASSERT (! (native
+ 1)->is_sym
);
1037 if (!coff_write_auxent_fname (abfd
, name
, &(native
+ 1)->u
.auxent
,
1043 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
1044 /* This name will fit into the symbol neatly. */
1045 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
1047 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
1049 indx
= _bfd_stringtab_add (strtab
, name
, hash
, false);
1050 if (indx
== (bfd_size_type
) -1)
1053 native
->u
.syment
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1054 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1060 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
1062 /* This name should be written into the .debug section. For
1063 some reason each name is preceded by a two byte length
1064 and also followed by a null byte. FIXME: We assume that
1065 the .debug section has already been created, and that it
1067 if (*debug_string_section_p
== (asection
*) NULL
)
1068 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
1069 filepos
= bfd_tell (abfd
);
1070 if (prefix_len
== 4)
1071 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
1073 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
1075 if (!bfd_set_section_contents (abfd
,
1076 *debug_string_section_p
,
1078 (file_ptr
) *debug_string_size_p
,
1079 (bfd_size_type
) prefix_len
)
1080 || !bfd_set_section_contents (abfd
,
1081 *debug_string_section_p
,
1082 (void *) symbol
->name
,
1083 (file_ptr
) (*debug_string_size_p
1085 (bfd_size_type
) name_length
+ 1))
1087 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
1089 native
->u
.syment
._n
._n_n
._n_offset
=
1090 *debug_string_size_p
+ prefix_len
;
1091 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1092 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
1099 /* We need to keep track of the symbol index so that when we write out
1100 the relocs we can get the index for a symbol. This method is a
1103 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
1105 /* Write a symbol out to a COFF file. */
1108 coff_write_symbol (bfd
*abfd
,
1110 combined_entry_type
*native
,
1112 struct bfd_strtab_hash
*strtab
,
1114 asection
**debug_string_section_p
,
1115 bfd_size_type
*debug_string_size_p
)
1117 unsigned int numaux
= native
->u
.syment
.n_numaux
;
1118 int type
= native
->u
.syment
.n_type
;
1119 int n_sclass
= (int) native
->u
.syment
.n_sclass
;
1120 asection
*output_section
= symbol
->section
->output_section
1121 ? symbol
->section
->output_section
1124 bfd_size_type symesz
;
1126 BFD_ASSERT (native
->is_sym
);
1128 if (native
->u
.syment
.n_sclass
== C_FILE
)
1129 symbol
->flags
|= BSF_DEBUGGING
;
1131 if (symbol
->flags
& BSF_DEBUGGING
1132 && bfd_is_abs_section (symbol
->section
))
1133 native
->u
.syment
.n_scnum
= N_DEBUG
;
1135 else if (bfd_is_abs_section (symbol
->section
))
1136 native
->u
.syment
.n_scnum
= N_ABS
;
1138 else if (bfd_is_und_section (symbol
->section
))
1139 native
->u
.syment
.n_scnum
= N_UNDEF
;
1142 native
->u
.syment
.n_scnum
=
1143 output_section
->target_index
;
1145 if (!coff_fix_symbol_name (abfd
, symbol
, native
, strtab
, hash
,
1146 debug_string_section_p
, debug_string_size_p
))
1149 symesz
= bfd_coff_symesz (abfd
);
1150 buf
= bfd_alloc (abfd
, symesz
);
1153 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
1154 if (bfd_write (buf
, symesz
, abfd
) != symesz
)
1156 bfd_release (abfd
, buf
);
1158 if (native
->u
.syment
.n_numaux
> 0)
1160 bfd_size_type auxesz
;
1163 auxesz
= bfd_coff_auxesz (abfd
);
1164 buf
= bfd_alloc (abfd
, auxesz
);
1167 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
1169 BFD_ASSERT (! (native
+ j
+ 1)->is_sym
);
1171 /* Adjust auxent only if this isn't the filename
1173 if (native
->u
.syment
.n_sclass
== C_FILE
1174 && (native
+ j
+ 1)->u
.auxent
.x_file
.x_ftype
1175 && (native
+ j
+ 1)->extrap
)
1176 coff_write_auxent_fname (abfd
, (char *) (native
+ j
+ 1)->extrap
,
1177 &(native
+ j
+ 1)->u
.auxent
, strtab
, hash
);
1179 bfd_coff_swap_aux_out (abfd
,
1180 &((native
+ j
+ 1)->u
.auxent
),
1181 type
, n_sclass
, (int) j
,
1182 native
->u
.syment
.n_numaux
,
1184 if (bfd_write (buf
, auxesz
, abfd
) != auxesz
)
1187 bfd_release (abfd
, buf
);
1190 /* Store the index for use when we write out the relocs. */
1191 set_index (symbol
, *written
);
1193 *written
+= numaux
+ 1;
1197 /* Write out a symbol to a COFF file that does not come from a COFF
1198 file originally. This symbol may have been created by the linker,
1199 or we may be linking a non COFF file to a COFF file. */
1202 coff_write_alien_symbol (bfd
*abfd
,
1204 struct internal_syment
*isym
,
1206 struct bfd_strtab_hash
*strtab
,
1208 asection
**debug_string_section_p
,
1209 bfd_size_type
*debug_string_size_p
)
1211 combined_entry_type
*native
;
1212 combined_entry_type dummy
[2];
1213 asection
*output_section
= symbol
->section
->output_section
1214 ? symbol
->section
->output_section
1216 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1219 if ((!link_info
|| link_info
->strip_discarded
)
1220 && !bfd_is_abs_section (symbol
->section
)
1221 && symbol
->section
->output_section
== bfd_abs_section_ptr
)
1225 memset (isym
, 0, sizeof (*isym
));
1228 memset (dummy
, 0, sizeof dummy
);
1230 native
->is_sym
= true;
1231 native
[1].is_sym
= false;
1232 native
->u
.syment
.n_type
= T_NULL
;
1233 native
->u
.syment
.n_flags
= 0;
1234 native
->u
.syment
.n_numaux
= 0;
1235 if (bfd_is_und_section (symbol
->section
))
1237 native
->u
.syment
.n_scnum
= N_UNDEF
;
1238 native
->u
.syment
.n_value
= symbol
->value
;
1240 else if (bfd_is_com_section (symbol
->section
))
1242 native
->u
.syment
.n_scnum
= N_UNDEF
;
1243 native
->u
.syment
.n_value
= symbol
->value
;
1245 else if (symbol
->flags
& BSF_FILE
)
1247 native
->u
.syment
.n_scnum
= N_DEBUG
;
1248 native
->u
.syment
.n_numaux
= 1;
1250 else if (symbol
->flags
& BSF_DEBUGGING
)
1252 /* There isn't much point to writing out a debugging symbol
1253 unless we are prepared to convert it into COFF debugging
1254 format. So, we just ignore them. We must clobber the symbol
1255 name to keep it from being put in the string table. */
1258 memset (isym
, 0, sizeof (*isym
));
1263 native
->u
.syment
.n_scnum
= output_section
->target_index
;
1264 native
->u
.syment
.n_value
= (symbol
->value
1265 + symbol
->section
->output_offset
);
1266 if (! obj_pe (abfd
))
1267 native
->u
.syment
.n_value
+= output_section
->vma
;
1269 /* Copy the any flags from the file header into the symbol.
1272 coff_symbol_type
*c
= coff_symbol_from (symbol
);
1273 if (c
!= (coff_symbol_type
*) NULL
)
1274 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1278 native
->u
.syment
.n_type
= 0;
1279 if (symbol
->flags
& BSF_FILE
)
1280 native
->u
.syment
.n_sclass
= C_FILE
;
1281 else if (symbol
->flags
& BSF_LOCAL
)
1282 native
->u
.syment
.n_sclass
= C_STAT
;
1283 else if (symbol
->flags
& BSF_WEAK
)
1284 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1286 native
->u
.syment
.n_sclass
= C_EXT
;
1288 ret
= coff_write_symbol (abfd
, symbol
, native
, written
, strtab
, hash
,
1289 debug_string_section_p
, debug_string_size_p
);
1291 *isym
= native
->u
.syment
;
1295 /* Write a native symbol to a COFF file. */
1298 coff_write_native_symbol (bfd
*abfd
,
1299 coff_symbol_type
*symbol
,
1301 struct bfd_strtab_hash
*strtab
,
1302 asection
**debug_string_section_p
,
1303 bfd_size_type
*debug_string_size_p
)
1305 combined_entry_type
*native
= symbol
->native
;
1306 alent
*lineno
= symbol
->lineno
;
1307 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1309 if ((!link_info
|| link_info
->strip_discarded
)
1310 && !bfd_is_abs_section (symbol
->symbol
.section
)
1311 && symbol
->symbol
.section
->output_section
== bfd_abs_section_ptr
)
1313 symbol
->symbol
.name
= "";
1317 BFD_ASSERT (native
->is_sym
);
1318 /* If this symbol has an associated line number, we must store the
1319 symbol index in the line number field. We also tag the auxent to
1320 point to the right place in the lineno table. */
1321 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1323 unsigned int count
= 0;
1325 lineno
[count
].u
.offset
= *written
;
1326 if (native
->u
.syment
.n_numaux
)
1328 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1330 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1331 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1334 /* Count and relocate all other linenumbers. */
1336 while (lineno
[count
].line_number
!= 0)
1338 lineno
[count
].u
.offset
+=
1339 (symbol
->symbol
.section
->output_section
->vma
1340 + symbol
->symbol
.section
->output_offset
);
1343 symbol
->done_lineno
= true;
1345 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1346 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1347 count
* bfd_coff_linesz (abfd
);
1350 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1351 strtab
, true, debug_string_section_p
,
1352 debug_string_size_p
);
1356 null_error_handler (const char *fmt ATTRIBUTE_UNUSED
,
1357 va_list ap ATTRIBUTE_UNUSED
)
1361 /* Write out the COFF symbols. */
1364 coff_write_symbols (bfd
*abfd
)
1366 struct bfd_strtab_hash
*strtab
;
1367 asection
*debug_string_section
;
1368 bfd_size_type debug_string_size
;
1370 unsigned int limit
= bfd_get_symcount (abfd
);
1371 bfd_vma written
= 0;
1374 debug_string_section
= NULL
;
1375 debug_string_size
= 0;
1377 strtab
= _bfd_stringtab_init ();
1381 /* If this target supports long section names, they must be put into
1382 the string table. This is supported by PE. This code must
1383 handle section names just as they are handled in
1384 coff_write_object_contents. This is why we pass hash as FALSE below. */
1385 if (bfd_coff_long_section_names (abfd
))
1389 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1390 if (strlen (o
->name
) > SCNNMLEN
1391 && _bfd_stringtab_add (strtab
, o
->name
, false, false)
1392 == (bfd_size_type
) -1)
1396 /* Seek to the right place. */
1397 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1400 /* Output all the symbols we have. */
1402 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1404 asymbol
*symbol
= *p
;
1405 coff_symbol_type
*c_symbol
= coff_symbol_from (symbol
);
1407 if (c_symbol
== (coff_symbol_type
*) NULL
1408 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1410 if (!coff_write_alien_symbol (abfd
, symbol
, NULL
, &written
,
1411 strtab
, true, &debug_string_section
,
1412 &debug_string_size
))
1417 if (coff_backend_info (abfd
)->_bfd_coff_classify_symbol
!= NULL
)
1419 bfd_error_handler_type current_error_handler
;
1420 enum coff_symbol_classification sym_class
;
1421 unsigned char *n_sclass
;
1423 /* Suppress error reporting by bfd_coff_classify_symbol.
1424 Error messages can be generated when we are processing a local
1425 symbol which has no associated section and we do not have to
1426 worry about this, all we need to know is that it is local. */
1427 current_error_handler
= bfd_set_error_handler (null_error_handler
);
1428 BFD_ASSERT (c_symbol
->native
->is_sym
);
1429 sym_class
= bfd_coff_classify_symbol (abfd
,
1430 &c_symbol
->native
->u
.syment
);
1431 (void) bfd_set_error_handler (current_error_handler
);
1433 n_sclass
= &c_symbol
->native
->u
.syment
.n_sclass
;
1435 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1436 we cannot retain the existing sclass from the original symbol.
1437 Weak symbols only have one valid sclass, so just set it always.
1438 If it is not local class and should be, set it C_STAT.
1439 If it is global and not classified as global, or if it is
1440 weak (which is also classified as global), set it C_EXT. */
1442 if (symbol
->flags
& BSF_WEAK
)
1443 *n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1444 else if (symbol
->flags
& BSF_LOCAL
&& sym_class
!= COFF_SYMBOL_LOCAL
)
1446 else if (symbol
->flags
& BSF_GLOBAL
1447 && (sym_class
!= COFF_SYMBOL_GLOBAL
1449 || *n_sclass
== C_NT_WEAK
1451 || *n_sclass
== C_WEAKEXT
))
1452 c_symbol
->native
->u
.syment
.n_sclass
= C_EXT
;
1455 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1456 strtab
, &debug_string_section
,
1457 &debug_string_size
))
1462 obj_raw_syment_count (abfd
) = written
;
1464 /* Now write out strings.
1466 We would normally not write anything here if there are no strings, but
1467 we'll write out 4 so that any stupid coff reader which tries to read the
1468 string table even when there isn't one won't croak. */
1470 bfd_byte buffer
[STRING_SIZE_SIZE
];
1472 #if STRING_SIZE_SIZE == 4
1473 H_PUT_32 (abfd
, _bfd_stringtab_size (strtab
) + STRING_SIZE_SIZE
, buffer
);
1475 #error Change H_PUT_32
1477 if (bfd_write (buffer
, sizeof (buffer
), abfd
) != sizeof (buffer
))
1480 if (! _bfd_stringtab_emit (abfd
, strtab
))
1484 _bfd_stringtab_free (strtab
);
1486 /* Make sure the .debug section was created to be the correct size.
1487 We should create it ourselves on the fly, but we don't because
1488 BFD won't let us write to any section until we know how large all
1489 the sections are. We could still do it by making another pass
1490 over the symbols. FIXME. */
1491 BFD_ASSERT (debug_string_size
== 0
1492 || (debug_string_section
!= (asection
*) NULL
1493 && (BFD_ALIGN (debug_string_size
,
1494 1 << debug_string_section
->alignment_power
)
1495 == debug_string_section
->size
)));
1501 coff_write_linenumbers (bfd
*abfd
)
1504 bfd_size_type linesz
;
1507 linesz
= bfd_coff_linesz (abfd
);
1508 buff
= bfd_alloc (abfd
, linesz
);
1511 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1513 if (s
->lineno_count
)
1515 asymbol
**q
= abfd
->outsymbols
;
1516 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1518 /* Find all the linenumbers in this section. */
1522 if (p
->section
->output_section
== s
)
1525 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1526 (bfd_asymbol_bfd (p
), p
));
1529 /* Found a linenumber entry, output. */
1530 struct internal_lineno out
;
1532 memset ((void *) & out
, 0, sizeof (out
));
1534 out
.l_addr
.l_symndx
= l
->u
.offset
;
1535 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1536 if (bfd_write (buff
, linesz
, abfd
) != linesz
)
1539 while (l
->line_number
)
1541 out
.l_lnno
= l
->line_number
;
1542 out
.l_addr
.l_symndx
= l
->u
.offset
;
1543 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1544 if (bfd_write (buff
, linesz
, abfd
) != linesz
)
1554 bfd_release (abfd
, buff
);
1559 coff_get_lineno (bfd
*ignore_abfd ATTRIBUTE_UNUSED
, asymbol
*symbol
)
1561 return coffsymbol (symbol
)->lineno
;
1564 /* This function transforms the offsets into the symbol table into
1565 pointers to syments. */
1568 coff_pointerize_aux (bfd
*abfd
,
1569 combined_entry_type
*table_base
,
1570 combined_entry_type
*symbol
,
1571 unsigned int indaux
,
1572 combined_entry_type
*auxent
)
1574 unsigned int type
= symbol
->u
.syment
.n_type
;
1575 unsigned int n_sclass
= symbol
->u
.syment
.n_sclass
;
1577 BFD_ASSERT (symbol
->is_sym
);
1578 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1580 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1581 (abfd
, table_base
, symbol
, indaux
, auxent
))
1585 /* Don't bother if this is a file or a section. */
1586 if (n_sclass
== C_STAT
&& type
== T_NULL
)
1588 if (n_sclass
== C_FILE
)
1590 if (n_sclass
== C_DWARF
)
1593 BFD_ASSERT (! auxent
->is_sym
);
1594 /* Otherwise patch up. */
1595 #define N_TMASK coff_data (abfd)->local_n_tmask
1596 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1598 if ((ISFCN (type
) || ISTAG (n_sclass
) || n_sclass
== C_BLOCK
1599 || n_sclass
== C_FCN
)
1600 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
> 0
1601 && (auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
1602 < obj_raw_syment_count (abfd
)))
1604 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1605 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
;
1606 auxent
->fix_end
= 1;
1609 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1610 generate one, so we must be careful to ignore it. */
1611 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.u32
< obj_raw_syment_count (abfd
))
1613 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1614 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.u32
;
1615 auxent
->fix_tag
= 1;
1619 /* Allocate space for the ".debug" section, and read it.
1620 We did not read the debug section until now, because
1621 we didn't want to go to the trouble until someone needed it. */
1624 build_debug_section (bfd
*abfd
, asection
** sect_return
)
1626 char *debug_section
;
1628 bfd_size_type sec_size
;
1630 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1634 bfd_set_error (bfd_error_no_debug_section
);
1638 /* Seek to the beginning of the `.debug' section and read it.
1639 Save the current position first; it is needed by our caller.
1640 Then read debug section and reset the file pointer. */
1642 position
= bfd_tell (abfd
);
1643 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0)
1646 sec_size
= sect
->size
;
1647 debug_section
= (char *) _bfd_alloc_and_read (abfd
, sec_size
+ 1, sec_size
);
1648 if (debug_section
== NULL
)
1650 debug_section
[sec_size
] = 0;
1652 if (bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1655 * sect_return
= sect
;
1656 return debug_section
;
1659 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1660 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1661 be \0-terminated. */
1664 copy_name (bfd
*abfd
, char *name
, size_t maxlen
)
1669 for (len
= 0; len
< maxlen
; ++len
)
1670 if (name
[len
] == '\0')
1673 if ((newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1676 strncpy (newname
, name
, len
);
1677 newname
[len
] = '\0';
1681 /* Read in the external symbols. */
1684 _bfd_coff_get_external_symbols (bfd
*abfd
)
1691 if (obj_coff_external_syms (abfd
) != NULL
)
1694 symesz
= bfd_coff_symesz (abfd
);
1695 if (_bfd_mul_overflow (obj_raw_syment_count (abfd
), symesz
, &size
))
1697 bfd_set_error (bfd_error_file_truncated
);
1704 filesize
= bfd_get_file_size (abfd
);
1706 && ((ufile_ptr
) obj_sym_filepos (abfd
) > filesize
1707 || size
> filesize
- obj_sym_filepos (abfd
)))
1709 bfd_set_error (bfd_error_file_truncated
);
1713 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1715 syms
= _bfd_malloc_and_read (abfd
, size
, size
);
1716 obj_coff_external_syms (abfd
) = syms
;
1717 return syms
!= NULL
;
1720 /* Read in the external strings. The strings are not loaded until
1721 they are needed. This is because we have no simple way of
1722 detecting a missing string table in an archive. If the strings
1723 are loaded then the STRINGS and STRINGS_LEN fields in the
1724 coff_tdata structure will be set. */
1727 _bfd_coff_read_string_table (bfd
*abfd
)
1729 char extstrsize
[STRING_SIZE_SIZE
];
1730 bfd_size_type strsize
;
1737 if (obj_coff_strings (abfd
) != NULL
)
1738 return obj_coff_strings (abfd
);
1740 if (obj_sym_filepos (abfd
) == 0)
1742 bfd_set_error (bfd_error_no_symbols
);
1746 symesz
= bfd_coff_symesz (abfd
);
1747 pos
= obj_sym_filepos (abfd
);
1748 if (_bfd_mul_overflow (obj_raw_syment_count (abfd
), symesz
, &size
)
1749 || pos
+ size
< pos
)
1751 bfd_set_error (bfd_error_file_truncated
);
1755 if (bfd_seek (abfd
, pos
+ size
, SEEK_SET
) != 0)
1758 if (bfd_read (extstrsize
, sizeof extstrsize
, abfd
) != sizeof extstrsize
)
1760 if (bfd_get_error () != bfd_error_file_truncated
)
1763 /* There is no string table. */
1764 strsize
= STRING_SIZE_SIZE
;
1768 #if STRING_SIZE_SIZE == 4
1769 strsize
= H_GET_32 (abfd
, extstrsize
);
1771 #error Change H_GET_32
1775 filesize
= bfd_get_file_size (abfd
);
1776 if (strsize
< STRING_SIZE_SIZE
1777 || (filesize
!= 0 && strsize
> filesize
))
1780 /* xgettext: c-format */
1781 (_("%pB: bad string table size %" PRIu64
), abfd
, (uint64_t) strsize
);
1782 bfd_set_error (bfd_error_bad_value
);
1786 strings
= (char *) bfd_malloc (strsize
+ 1);
1787 if (strings
== NULL
)
1790 /* PR 17521 file: 079-54929-0.004.
1791 A corrupt file could contain an index that points into the first
1792 STRING_SIZE_SIZE bytes of the string table, so make sure that
1794 memset (strings
, 0, STRING_SIZE_SIZE
);
1796 if (bfd_read (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1797 != strsize
- STRING_SIZE_SIZE
)
1803 obj_coff_strings (abfd
) = strings
;
1804 obj_coff_strings_len (abfd
) = strsize
;
1805 /* Terminate the string table, just in case. */
1806 strings
[strsize
] = 0;
1810 /* Free up the external symbols and strings read from a COFF file. */
1813 _bfd_coff_free_symbols (bfd
*abfd
)
1815 if (! bfd_family_coff (abfd
))
1818 if (obj_coff_external_syms (abfd
) != NULL
1819 && ! obj_coff_keep_syms (abfd
))
1821 free (obj_coff_external_syms (abfd
));
1822 obj_coff_external_syms (abfd
) = NULL
;
1825 if (obj_coff_strings (abfd
) != NULL
1826 && ! obj_coff_keep_strings (abfd
))
1828 free (obj_coff_strings (abfd
));
1829 obj_coff_strings (abfd
) = NULL
;
1830 obj_coff_strings_len (abfd
) = 0;
1836 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1837 knit the symbol names into a normalized form. By normalized here I
1838 mean that all symbols have an n_offset pointer that points to a null-
1839 terminated string. */
1841 combined_entry_type
*
1842 coff_get_normalized_symtab (bfd
*abfd
)
1844 combined_entry_type
*internal
;
1845 combined_entry_type
*internal_ptr
;
1849 const char *string_table
= NULL
;
1850 asection
* debug_sec
= NULL
;
1851 char *debug_sec_data
= NULL
;
1854 if (obj_raw_syments (abfd
) != NULL
)
1855 return obj_raw_syments (abfd
);
1857 if (! _bfd_coff_get_external_symbols (abfd
))
1860 size
= obj_raw_syment_count (abfd
);
1861 /* Check for integer overflow. */
1862 if (size
> (bfd_size_type
) -1 / sizeof (combined_entry_type
))
1864 size
*= sizeof (combined_entry_type
);
1865 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1866 if (internal
== NULL
&& size
!= 0)
1869 raw_src
= (char *) obj_coff_external_syms (abfd
);
1871 /* Mark the end of the symbols. */
1872 symesz
= bfd_coff_symesz (abfd
);
1873 raw_end
= PTR_ADD (raw_src
, obj_raw_syment_count (abfd
) * symesz
);
1875 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1876 probably possible. If one shows up, it will probably kill us. */
1878 /* Swap all the raw entries. */
1879 for (internal_ptr
= internal
;
1881 raw_src
+= symesz
, internal_ptr
++)
1885 bfd_coff_swap_sym_in (abfd
, (void *) raw_src
,
1886 (void *) & internal_ptr
->u
.syment
);
1887 internal_ptr
->is_sym
= true;
1888 combined_entry_type
*sym
= internal_ptr
;
1890 /* PR 17512: Prevent buffer overrun. */
1891 if (sym
->u
.syment
.n_numaux
> ((raw_end
- 1) - raw_src
) / symesz
)
1894 for (i
= 0; i
< sym
->u
.syment
.n_numaux
; i
++)
1899 bfd_coff_swap_aux_in (abfd
, (void *) raw_src
,
1900 sym
->u
.syment
.n_type
,
1901 sym
->u
.syment
.n_sclass
,
1902 (int) i
, sym
->u
.syment
.n_numaux
,
1903 &(internal_ptr
->u
.auxent
));
1905 internal_ptr
->is_sym
= false;
1906 coff_pointerize_aux (abfd
, internal
, sym
, i
, internal_ptr
);
1909 if (sym
->u
.syment
.n_sclass
== C_FILE
1910 && sym
->u
.syment
.n_numaux
> 0)
1912 combined_entry_type
* aux
= sym
+ 1;
1914 /* Make a file symbol point to the name in the auxent, since
1915 the text ".file" is redundant. */
1916 BFD_ASSERT (! aux
->is_sym
);
1918 if (aux
->u
.auxent
.x_file
.x_n
.x_n
.x_zeroes
== 0)
1920 /* The filename is a long one, point into the string table. */
1921 if (string_table
== NULL
)
1923 string_table
= _bfd_coff_read_string_table (abfd
);
1924 if (string_table
== NULL
)
1928 if ((bfd_size_type
) aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
1929 >= obj_coff_strings_len (abfd
))
1930 sym
->u
.syment
._n
._n_n
._n_offset
=
1931 (uintptr_t) _("<corrupt>");
1933 sym
->u
.syment
._n
._n_n
._n_offset
=
1934 (uintptr_t) (string_table
1935 + aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
);
1939 /* Ordinary short filename, put into memory anyway. The
1940 Microsoft PE tools sometimes store a filename in
1941 multiple AUX entries. */
1944 if (sym
->u
.syment
.n_numaux
> 1 && obj_pe (abfd
))
1946 len
= sym
->u
.syment
.n_numaux
* symesz
;
1947 src
= raw_src
- (len
- symesz
);
1951 len
= bfd_coff_filnmlen (abfd
);
1952 src
= aux
->u
.auxent
.x_file
.x_n
.x_fname
;
1954 sym
->u
.syment
._n
._n_n
._n_offset
=
1955 (uintptr_t) copy_name (abfd
, src
, len
);
1958 /* Normalize other strings available in C_FILE aux entries. */
1960 for (int numaux
= 1;
1961 numaux
< sym
->u
.syment
.n_numaux
;
1964 aux
= sym
+ numaux
+ 1;
1965 BFD_ASSERT (! aux
->is_sym
);
1967 if (aux
->u
.auxent
.x_file
.x_n
.x_n
.x_zeroes
== 0)
1969 /* The string information is a long one, point
1970 into the string table. */
1971 if (string_table
== NULL
)
1973 string_table
= _bfd_coff_read_string_table (abfd
);
1974 if (string_table
== NULL
)
1978 if ((bfd_size_type
) aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
1979 >= obj_coff_strings_len (abfd
))
1980 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1981 (uintptr_t) _("<corrupt>");
1983 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1984 (uintptr_t) (string_table
1985 + aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
);
1988 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1991 aux
->u
.auxent
.x_file
.x_n
.x_fname
,
1992 bfd_coff_filnmlen (abfd
)));
1998 if (sym
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
2000 /* This is a "short" name. Make it long. */
2003 /* Find the length of this string without walking into memory
2005 for (i
= 0; i
< SYMNMLEN
; ++i
)
2006 if (sym
->u
.syment
._n
._n_name
[i
] == '\0')
2009 newstring
= bfd_alloc (abfd
, i
+ 1);
2010 if (newstring
== NULL
)
2012 memcpy (newstring
, sym
->u
.syment
._n
._n_name
, i
);
2014 sym
->u
.syment
._n
._n_n
._n_offset
= (uintptr_t) newstring
;
2015 sym
->u
.syment
._n
._n_n
._n_zeroes
= 0;
2017 else if (sym
->u
.syment
._n
._n_n
._n_offset
== 0)
2018 sym
->u
.syment
._n
._n_n
._n_offset
= (uintptr_t) "";
2019 else if (!bfd_coff_symname_in_debug (abfd
, &sym
->u
.syment
))
2021 /* Long name already. Point symbol at the string in the
2023 if (string_table
== NULL
)
2025 string_table
= _bfd_coff_read_string_table (abfd
);
2026 if (string_table
== NULL
)
2029 if (sym
->u
.syment
._n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
))
2030 sym
->u
.syment
._n
._n_n
._n_offset
=
2031 (uintptr_t) _("<corrupt>");
2033 sym
->u
.syment
._n
._n_n
._n_offset
=
2034 (uintptr_t) (string_table
2035 + sym
->u
.syment
._n
._n_n
._n_offset
);
2039 /* Long name in debug section. Very similar. */
2040 if (debug_sec_data
== NULL
)
2042 debug_sec_data
= build_debug_section (abfd
, &debug_sec
);
2043 if (debug_sec_data
== NULL
)
2046 /* PR binutils/17512: Catch out of range offsets into
2048 if (sym
->u
.syment
._n
._n_n
._n_offset
>= debug_sec
->size
)
2049 sym
->u
.syment
._n
._n_n
._n_offset
=
2050 (uintptr_t) _("<corrupt>");
2052 sym
->u
.syment
._n
._n_n
._n_offset
=
2053 (uintptr_t) (debug_sec_data
2054 + sym
->u
.syment
._n
._n_n
._n_offset
);
2059 /* Free the raw symbols. */
2060 if (obj_coff_external_syms (abfd
) != NULL
2061 && ! obj_coff_keep_syms (abfd
))
2063 free (obj_coff_external_syms (abfd
));
2064 obj_coff_external_syms (abfd
) = NULL
;
2067 obj_raw_syments (abfd
) = internal
;
2068 BFD_ASSERT (obj_raw_syment_count (abfd
)
2069 == (size_t) (internal_ptr
- internal
));
2075 coff_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
2079 count
= asect
->reloc_count
;
2080 if (count
>= LONG_MAX
/ sizeof (arelent
*)
2081 || _bfd_mul_overflow (count
, bfd_coff_relsz (abfd
), &raw
))
2083 bfd_set_error (bfd_error_file_too_big
);
2086 if (!bfd_write_p (abfd
))
2088 ufile_ptr filesize
= bfd_get_file_size (abfd
);
2089 if (filesize
!= 0 && raw
> filesize
)
2091 bfd_set_error (bfd_error_file_truncated
);
2095 return (count
+ 1) * sizeof (arelent
*);
2099 coff_make_empty_symbol (bfd
*abfd
)
2101 size_t amt
= sizeof (coff_symbol_type
);
2102 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
2104 if (new_symbol
== NULL
)
2106 new_symbol
->symbol
.section
= 0;
2107 new_symbol
->native
= NULL
;
2108 new_symbol
->lineno
= NULL
;
2109 new_symbol
->done_lineno
= false;
2110 new_symbol
->symbol
.the_bfd
= abfd
;
2112 return & new_symbol
->symbol
;
2115 /* Make a debugging symbol. */
2118 coff_bfd_make_debug_symbol (bfd
*abfd
)
2120 size_t amt
= sizeof (coff_symbol_type
);
2121 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
2123 if (new_symbol
== NULL
)
2125 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2126 (but shouldn't be a constant). */
2127 amt
= sizeof (combined_entry_type
) * 10;
2128 new_symbol
->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2129 if (!new_symbol
->native
)
2131 new_symbol
->native
->is_sym
= true;
2132 new_symbol
->symbol
.section
= bfd_abs_section_ptr
;
2133 new_symbol
->symbol
.flags
= BSF_DEBUGGING
;
2134 new_symbol
->lineno
= NULL
;
2135 new_symbol
->done_lineno
= false;
2136 new_symbol
->symbol
.the_bfd
= abfd
;
2138 return & new_symbol
->symbol
;
2142 coff_get_symbol_info (bfd
*abfd
, asymbol
*symbol
, symbol_info
*ret
)
2144 bfd_symbol_info (symbol
, ret
);
2146 if (coffsymbol (symbol
)->native
!= NULL
2147 && coffsymbol (symbol
)->native
->fix_value
2148 && coffsymbol (symbol
)->native
->is_sym
)
2150 = (((uintptr_t) coffsymbol (symbol
)->native
->u
.syment
.n_value
2151 - (uintptr_t) obj_raw_syments (abfd
))
2152 / sizeof (combined_entry_type
));
2155 /* Print out information about COFF symbol. */
2158 coff_print_symbol (bfd
*abfd
,
2161 bfd_print_symbol_type how
)
2163 FILE * file
= (FILE *) filep
;
2167 case bfd_print_symbol_name
:
2168 fprintf (file
, "%s", symbol
->name
);
2171 case bfd_print_symbol_more
:
2172 fprintf (file
, "coff %s %s",
2173 coffsymbol (symbol
)->native
? "n" : "g",
2174 coffsymbol (symbol
)->lineno
? "l" : " ");
2177 case bfd_print_symbol_all
:
2178 if (coffsymbol (symbol
)->native
)
2182 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2183 combined_entry_type
*root
= obj_raw_syments (abfd
);
2184 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2186 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2188 /* PR 17512: file: 079-33786-0.001:0.1. */
2189 if (combined
< obj_raw_syments (abfd
)
2190 || combined
>= obj_raw_syments (abfd
) + obj_raw_syment_count (abfd
))
2192 fprintf (file
, _("<corrupt info> %s"), symbol
->name
);
2196 BFD_ASSERT (combined
->is_sym
);
2197 if (! combined
->fix_value
)
2198 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2200 val
= (((uintptr_t) combined
->u
.syment
.n_value
- (uintptr_t) root
)
2201 / sizeof (combined_entry_type
));
2203 fprintf (file
, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2204 combined
->u
.syment
.n_scnum
,
2205 combined
->u
.syment
.n_flags
,
2206 combined
->u
.syment
.n_type
,
2207 combined
->u
.syment
.n_sclass
,
2208 combined
->u
.syment
.n_numaux
);
2209 bfd_fprintf_vma (abfd
, file
, val
);
2210 fprintf (file
, " %s", symbol
->name
);
2212 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2214 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2217 BFD_ASSERT (! auxp
->is_sym
);
2219 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2221 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.u32
;
2223 fprintf (file
, "\n");
2225 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2228 switch (combined
->u
.syment
.n_sclass
)
2231 fprintf (file
, "File ");
2232 /* Add additional information if this isn't the filename
2234 if (auxp
->u
.auxent
.x_file
.x_ftype
)
2235 fprintf (file
, "ftype %d fname \"%s\"",
2236 auxp
->u
.auxent
.x_file
.x_ftype
,
2237 (char *) auxp
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
);
2241 fprintf (file
, "AUX scnlen %#" PRIx64
" nreloc %" PRId64
,
2242 auxp
->u
.auxent
.x_sect
.x_scnlen
,
2243 auxp
->u
.auxent
.x_sect
.x_nreloc
);
2247 if (combined
->u
.syment
.n_type
== T_NULL
)
2248 /* Probably a section symbol ? */
2250 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2251 (unsigned long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2252 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2253 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2254 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2255 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2256 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2257 fprintf (file
, " checksum 0x%x assoc %d comdat %d",
2258 auxp
->u
.auxent
.x_scn
.x_checksum
,
2259 auxp
->u
.auxent
.x_scn
.x_associated
,
2260 auxp
->u
.auxent
.x_scn
.x_comdat
);
2266 if (ISFCN (combined
->u
.syment
.n_type
))
2271 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2274 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
;
2275 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2277 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2279 (unsigned long) auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2285 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2286 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2287 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2290 fprintf (file
, " endndx %ld",
2292 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2300 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2302 while (l
->line_number
)
2304 if (l
->line_number
> 0)
2306 fprintf (file
, "\n%4d : ", l
->line_number
);
2307 bfd_fprintf_vma (abfd
, file
, l
->u
.offset
+ symbol
->section
->vma
);
2315 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
2316 fprintf (file
, " %-5s %s %s %s",
2317 symbol
->section
->name
,
2318 coffsymbol (symbol
)->native
? "n" : "g",
2319 coffsymbol (symbol
)->lineno
? "l" : " ",
2325 /* Return whether a symbol name implies a local symbol. In COFF,
2326 local symbols generally start with ``.L''. Most targets use this
2327 function for the is_local_label_name entry point, but some may
2331 _bfd_coff_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
2334 return name
[0] == '.' && name
[1] == 'L';
2337 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2338 section, calculate and return the name of the source file and the line
2339 nearest to the wanted location. */
2342 coff_find_nearest_line_with_names (bfd
*abfd
,
2346 const char **filename_ptr
,
2347 const char **functionname_ptr
,
2348 unsigned int *line_ptr
,
2349 const struct dwarf_debug_section
*debug_sections
)
2353 unsigned int line_base
;
2354 coff_data_type
*cof
= coff_data (abfd
);
2355 /* Run through the raw syments if available. */
2356 combined_entry_type
*p
;
2357 combined_entry_type
*pend
;
2359 struct coff_section_tdata
*sec_data
;
2362 /* Before looking through the symbol table, try to use a .stab
2363 section to find the information. */
2364 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2365 &found
, filename_ptr
,
2366 functionname_ptr
, line_ptr
,
2367 &coff_data(abfd
)->line_info
))
2373 /* Also try examining DWARF2 debugging information. */
2374 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
2375 filename_ptr
, functionname_ptr
,
2376 line_ptr
, NULL
, debug_sections
,
2377 &coff_data(abfd
)->dwarf2_find_line_info
))
2380 sec_data
= coff_section_data (abfd
, section
);
2382 /* If the DWARF lookup failed, but there is DWARF information available
2383 then the problem might be that the file has been rebased. This tool
2384 changes the VMAs of all the sections, but it does not update the DWARF
2385 information. So try again, using a bias against the address sought. */
2386 if (coff_data (abfd
)->dwarf2_find_line_info
!= NULL
)
2388 bfd_signed_vma bias
= 0;
2390 /* Create a cache of the result for the next call. */
2391 if (sec_data
== NULL
&& section
->owner
== abfd
)
2393 amt
= sizeof (struct coff_section_tdata
);
2394 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2395 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2398 if (sec_data
!= NULL
&& sec_data
->saved_bias
)
2399 bias
= sec_data
->bias
;
2402 bias
= _bfd_dwarf2_find_symbol_bias (symbols
,
2403 & coff_data (abfd
)->dwarf2_find_line_info
);
2407 sec_data
->saved_bias
= true;
2408 sec_data
->bias
= bias
;
2413 && _bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
,
2415 filename_ptr
, functionname_ptr
,
2416 line_ptr
, NULL
, debug_sections
,
2417 &coff_data(abfd
)->dwarf2_find_line_info
))
2422 *functionname_ptr
= 0;
2425 /* Don't try and find line numbers in a non coff file. */
2426 if (!bfd_family_coff (abfd
))
2432 /* Find the first C_FILE symbol. */
2433 p
= cof
->raw_syments
;
2437 pend
= p
+ cof
->raw_syment_count
;
2440 BFD_ASSERT (p
->is_sym
);
2441 if (p
->u
.syment
.n_sclass
== C_FILE
)
2443 p
+= 1 + p
->u
.syment
.n_numaux
;
2451 /* Look through the C_FILE symbols to find the best one. */
2452 sec_vma
= bfd_section_vma (section
);
2453 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2454 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2458 combined_entry_type
*p2
;
2460 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2462 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2464 BFD_ASSERT (p2
->is_sym
);
2465 if (p2
->u
.syment
.n_scnum
> 0
2467 == coff_section_from_bfd_index (abfd
,
2468 p2
->u
.syment
.n_scnum
)))
2470 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2479 file_addr
= (bfd_vma
) p2
->u
.syment
.n_value
;
2480 /* PR 11512: Include the section address of the function name symbol. */
2481 if (p2
->u
.syment
.n_scnum
> 0)
2482 file_addr
+= coff_section_from_bfd_index (abfd
,
2483 p2
->u
.syment
.n_scnum
)->vma
;
2484 /* We use <= MAXDIFF here so that if we get a zero length
2485 file, we actually use the next file entry. */
2487 && offset
+ sec_vma
>= file_addr
2488 && offset
+ sec_vma
- file_addr
<= maxdiff
)
2490 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2491 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2494 if (p
->u
.syment
.n_value
>= cof
->raw_syment_count
)
2497 /* Avoid endless loops on erroneous files by ensuring that
2498 we always move forward in the file. */
2499 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2502 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2503 if (!p
->is_sym
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2508 if (section
->lineno_count
== 0)
2510 *functionname_ptr
= NULL
;
2515 /* Now wander though the raw linenumbers of the section.
2516 If we have been called on this section before, and the offset
2517 we want is further down then we can prime the lookup loop. */
2518 if (sec_data
!= NULL
2520 && offset
>= sec_data
->offset
)
2523 *functionname_ptr
= sec_data
->function
;
2524 line_base
= sec_data
->line_base
;
2532 if (section
->lineno
!= NULL
)
2534 bfd_vma last_value
= 0;
2536 l
= §ion
->lineno
[i
];
2538 for (; i
< section
->lineno_count
; i
++)
2540 if (l
->line_number
== 0)
2542 /* Get the symbol this line number points at. */
2543 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2544 if (coff
->symbol
.value
> offset
)
2547 *functionname_ptr
= coff
->symbol
.name
;
2548 last_value
= coff
->symbol
.value
;
2551 combined_entry_type
*s
= coff
->native
;
2553 BFD_ASSERT (s
->is_sym
);
2554 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2556 /* In XCOFF a debugging symbol can follow the
2558 if (((size_t) ((char *) s
- (char *) obj_raw_syments (abfd
))
2559 < obj_raw_syment_count (abfd
) * sizeof (*s
))
2560 && s
->u
.syment
.n_scnum
== N_DEBUG
)
2561 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2563 /* S should now point to the .bf of the function. */
2564 if (((size_t) ((char *) s
- (char *) obj_raw_syments (abfd
))
2565 < obj_raw_syment_count (abfd
) * sizeof (*s
))
2566 && s
->u
.syment
.n_numaux
)
2568 /* The linenumber is stored in the auxent. */
2569 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2571 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2572 *line_ptr
= line_base
;
2578 if (l
->u
.offset
> offset
)
2580 *line_ptr
= l
->line_number
+ line_base
- 1;
2585 /* If we fell off the end of the loop, then assume that this
2586 symbol has no line number info. Otherwise, symbols with no
2587 line number info get reported with the line number of the
2588 last line of the last symbol which does have line number
2589 info. We use 0x100 as a slop to account for cases where the
2590 last line has executable code. */
2591 if (i
>= section
->lineno_count
2593 && offset
- last_value
> 0x100)
2595 *functionname_ptr
= NULL
;
2600 /* Cache the results for the next call. */
2601 if (sec_data
== NULL
&& section
->owner
== abfd
)
2603 amt
= sizeof (struct coff_section_tdata
);
2604 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2605 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2608 if (sec_data
!= NULL
)
2610 sec_data
->offset
= offset
;
2611 sec_data
->i
= i
- 1;
2612 sec_data
->function
= *functionname_ptr
;
2613 sec_data
->line_base
= line_base
;
2620 coff_find_nearest_line (bfd
*abfd
,
2624 const char **filename_ptr
,
2625 const char **functionname_ptr
,
2626 unsigned int *line_ptr
,
2627 unsigned int *discriminator_ptr
)
2629 if (discriminator_ptr
)
2630 *discriminator_ptr
= 0;
2631 return coff_find_nearest_line_with_names (abfd
, symbols
, section
, offset
,
2632 filename_ptr
, functionname_ptr
,
2633 line_ptr
, dwarf_debug_sections
);
2637 coff_find_inliner_info (bfd
*abfd
,
2638 const char **filename_ptr
,
2639 const char **functionname_ptr
,
2640 unsigned int *line_ptr
)
2644 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
2645 functionname_ptr
, line_ptr
,
2646 &coff_data(abfd
)->dwarf2_find_line_info
);
2651 coff_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2655 if (!bfd_link_relocatable (info
))
2656 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2658 size
= bfd_coff_filhsz (abfd
);
2660 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2664 /* Change the class of a coff symbol held by BFD. */
2667 bfd_coff_set_symbol_class (bfd
* abfd
,
2669 unsigned int symbol_class
)
2671 coff_symbol_type
* csym
;
2673 csym
= coff_symbol_from (symbol
);
2676 bfd_set_error (bfd_error_invalid_operation
);
2679 else if (csym
->native
== NULL
)
2681 /* This is an alien symbol which no native coff backend data.
2682 We cheat here by creating a fake native entry for it and
2683 then filling in the class. This code is based on that in
2684 coff_write_alien_symbol(). */
2686 combined_entry_type
* native
;
2687 size_t amt
= sizeof (* native
);
2689 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2693 native
->is_sym
= true;
2694 native
->u
.syment
.n_type
= T_NULL
;
2695 native
->u
.syment
.n_sclass
= symbol_class
;
2697 if (bfd_is_und_section (symbol
->section
))
2699 native
->u
.syment
.n_scnum
= N_UNDEF
;
2700 native
->u
.syment
.n_value
= symbol
->value
;
2702 else if (bfd_is_com_section (symbol
->section
))
2704 native
->u
.syment
.n_scnum
= N_UNDEF
;
2705 native
->u
.syment
.n_value
= symbol
->value
;
2709 native
->u
.syment
.n_scnum
=
2710 symbol
->section
->output_section
->target_index
;
2711 native
->u
.syment
.n_value
= (symbol
->value
2712 + symbol
->section
->output_offset
);
2713 if (! obj_pe (abfd
))
2714 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2716 /* Copy the any flags from the file header into the symbol.
2718 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2721 csym
->native
= native
;
2724 csym
->native
->u
.syment
.n_sclass
= symbol_class
;
2730 _bfd_coff_section_already_linked (bfd
*abfd
,
2732 struct bfd_link_info
*info
)
2735 const char *name
, *key
;
2736 struct bfd_section_already_linked
*l
;
2737 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
2738 struct coff_comdat_info
*s_comdat
;
2740 if (sec
->output_section
== bfd_abs_section_ptr
)
2744 if ((flags
& SEC_LINK_ONCE
) == 0)
2747 /* The COFF backend linker doesn't support group sections. */
2748 if ((flags
& SEC_GROUP
) != 0)
2751 name
= bfd_section_name (sec
);
2752 s_comdat
= bfd_coff_get_comdat_section (abfd
, sec
);
2754 if (s_comdat
!= NULL
)
2755 key
= s_comdat
->name
;
2758 if (startswith (name
, ".gnu.linkonce.")
2759 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
2762 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2763 .xdata$<key> and .pdata$<key> only the first of which has a
2764 comdat key. Should these all match the LTO IR key? */
2768 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
2770 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
2772 struct coff_comdat_info
*l_comdat
;
2774 l_comdat
= bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
);
2776 /* The section names must match, and both sections must be
2777 comdat and have the same comdat name, or both sections must
2778 be non-comdat. LTO IR plugin sections are an exception. They
2779 are always named .gnu.linkonce.t.<key> (<key> is some string)
2780 and match any comdat section with comdat name of <key>, and
2781 any linkonce section with the same suffix, ie.
2782 .gnu.linkonce.*.<key>. */
2783 if (((s_comdat
!= NULL
) == (l_comdat
!= NULL
)
2784 && strcmp (name
, l
->sec
->name
) == 0)
2785 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
2786 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
2788 /* The section has already been linked. See if we should
2790 return _bfd_handle_already_linked (sec
, l
, info
);
2794 /* This is the first section with this name. Record it. */
2795 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
2796 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
2800 /* Initialize COOKIE for input bfd ABFD. */
2803 init_reloc_cookie (struct coff_reloc_cookie
*cookie
,
2804 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2807 /* Sometimes the symbol table does not yet have been loaded here. */
2808 bfd_coff_slurp_symbol_table (abfd
);
2810 cookie
->abfd
= abfd
;
2811 cookie
->sym_hashes
= obj_coff_sym_hashes (abfd
);
2813 cookie
->symbols
= obj_symbols (abfd
);
2818 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2821 fini_reloc_cookie (struct coff_reloc_cookie
*cookie ATTRIBUTE_UNUSED
,
2822 bfd
*abfd ATTRIBUTE_UNUSED
)
2824 /* Nothing to do. */
2827 /* Initialize the relocation information in COOKIE for input section SEC
2828 of input bfd ABFD. */
2831 init_reloc_cookie_rels (struct coff_reloc_cookie
*cookie
,
2832 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2836 if (sec
->reloc_count
== 0)
2838 cookie
->rels
= NULL
;
2839 cookie
->relend
= NULL
;
2844 cookie
->rels
= _bfd_coff_read_internal_relocs (abfd
, sec
, false, NULL
,
2847 if (cookie
->rels
== NULL
)
2850 cookie
->rel
= cookie
->rels
;
2851 cookie
->relend
= (cookie
->rels
+ sec
->reloc_count
);
2855 /* Free the memory allocated by init_reloc_cookie_rels,
2859 fini_reloc_cookie_rels (struct coff_reloc_cookie
*cookie
,
2863 /* PR 20401. The relocs may not have been cached, so check first.
2864 If the relocs were loaded by init_reloc_cookie_rels() then this
2865 will be the case. FIXME: Would performance be improved if the
2866 relocs *were* cached ? */
2867 && coff_section_data (NULL
, sec
)
2868 && coff_section_data (NULL
, sec
)->relocs
!= cookie
->rels
)
2869 free (cookie
->rels
);
2872 /* Initialize the whole of COOKIE for input section SEC. */
2875 init_reloc_cookie_for_section (struct coff_reloc_cookie
*cookie
,
2876 struct bfd_link_info
*info
,
2879 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
2882 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
2884 fini_reloc_cookie (cookie
, sec
->owner
);
2890 /* Free the memory allocated by init_reloc_cookie_for_section,
2894 fini_reloc_cookie_for_section (struct coff_reloc_cookie
*cookie
,
2897 fini_reloc_cookie_rels (cookie
, sec
);
2898 fini_reloc_cookie (cookie
, sec
->owner
);
2902 _bfd_coff_gc_mark_hook (asection
*sec
,
2903 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2904 struct internal_reloc
*rel ATTRIBUTE_UNUSED
,
2905 struct coff_link_hash_entry
*h
,
2906 struct internal_syment
*sym
)
2910 switch (h
->root
.type
)
2912 case bfd_link_hash_defined
:
2913 case bfd_link_hash_defweak
:
2914 return h
->root
.u
.def
.section
;
2916 case bfd_link_hash_common
:
2917 return h
->root
.u
.c
.p
->section
;
2919 case bfd_link_hash_undefweak
:
2920 if (h
->symbol_class
== C_NT_WEAK
&& h
->numaux
== 1)
2922 /* PE weak externals. A weak symbol may include an auxiliary
2923 record indicating that if the weak symbol is not resolved,
2924 another external symbol is used instead. */
2925 struct coff_link_hash_entry
*h2
=
2926 h
->auxbfd
->tdata
.coff_obj_data
->sym_hashes
2927 [h
->aux
->x_sym
.x_tagndx
.u32
];
2929 if (h2
&& h2
->root
.type
!= bfd_link_hash_undefined
)
2930 return h2
->root
.u
.def
.section
;
2934 case bfd_link_hash_undefined
:
2941 return coff_section_from_bfd_index (sec
->owner
, sym
->n_scnum
);
2944 /* COOKIE->rel describes a relocation against section SEC, which is
2945 a section we've decided to keep. Return the section that contains
2946 the relocation symbol, or NULL if no section contains it. */
2949 _bfd_coff_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
2950 coff_gc_mark_hook_fn gc_mark_hook
,
2951 struct coff_reloc_cookie
*cookie
)
2953 struct coff_link_hash_entry
*h
;
2955 h
= cookie
->sym_hashes
[cookie
->rel
->r_symndx
];
2958 while (h
->root
.type
== bfd_link_hash_indirect
2959 || h
->root
.type
== bfd_link_hash_warning
)
2960 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2962 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
2965 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
2967 + obj_convert (sec
->owner
)[cookie
->rel
->r_symndx
])->native
->u
.syment
);
2970 static bool _bfd_coff_gc_mark
2971 (struct bfd_link_info
*, asection
*, coff_gc_mark_hook_fn
);
2973 /* COOKIE->rel describes a relocation against section SEC, which is
2974 a section we've decided to keep. Mark the section that contains
2975 the relocation symbol. */
2978 _bfd_coff_gc_mark_reloc (struct bfd_link_info
*info
,
2980 coff_gc_mark_hook_fn gc_mark_hook
,
2981 struct coff_reloc_cookie
*cookie
)
2985 rsec
= _bfd_coff_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
2986 if (rsec
&& !rsec
->gc_mark
)
2988 if (bfd_get_flavour (rsec
->owner
) != bfd_target_coff_flavour
)
2990 else if (!_bfd_coff_gc_mark (info
, rsec
, gc_mark_hook
))
2996 /* The mark phase of garbage collection. For a given section, mark
2997 it and any sections in this section's group, and all the sections
2998 which define symbols to which it refers. */
3001 _bfd_coff_gc_mark (struct bfd_link_info
*info
,
3003 coff_gc_mark_hook_fn gc_mark_hook
)
3009 /* Look through the section relocs. */
3010 if ((sec
->flags
& SEC_RELOC
) != 0
3011 && sec
->reloc_count
> 0)
3013 struct coff_reloc_cookie cookie
;
3015 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
3019 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
3021 if (!_bfd_coff_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
3027 fini_reloc_cookie_for_section (&cookie
, sec
);
3035 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info
*info
,
3036 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
3040 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3045 if (bfd_get_flavour (ibfd
) != bfd_target_coff_flavour
)
3048 /* Ensure all linker created sections are kept, and see whether
3049 any other section is already marked. */
3051 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
3053 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
3055 else if (isec
->gc_mark
)
3059 /* If no section in this file will be kept, then we can
3060 toss out debug sections. */
3064 /* Keep debug and special sections like .comment when they are
3065 not part of a group, or when we have single-member groups. */
3066 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
3067 if ((isec
->flags
& SEC_DEBUGGING
) != 0
3068 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
3074 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
3077 coff_gc_sweep_symbol (struct coff_link_hash_entry
*h
,
3078 void *data ATTRIBUTE_UNUSED
)
3080 if (h
->root
.type
== bfd_link_hash_warning
)
3081 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
3083 if ((h
->root
.type
== bfd_link_hash_defined
3084 || h
->root
.type
== bfd_link_hash_defweak
)
3085 && !h
->root
.u
.def
.section
->gc_mark
3086 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
3088 /* Do our best to hide the symbol. */
3089 h
->root
.u
.def
.section
= bfd_und_section_ptr
;
3090 h
->symbol_class
= C_HIDDEN
;
3096 /* The sweep phase of garbage collection. Remove all garbage sections. */
3098 typedef bool (*gc_sweep_hook_fn
)
3099 (bfd
*, struct bfd_link_info
*, asection
*, const struct internal_reloc
*);
3102 coff_gc_sweep (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_link_info
*info
)
3106 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3110 if (bfd_get_flavour (sub
) != bfd_target_coff_flavour
)
3113 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3115 /* Keep debug and special sections. */
3116 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
3117 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
3119 else if (startswith (o
->name
, ".idata")
3120 || startswith (o
->name
, ".pdata")
3121 || startswith (o
->name
, ".xdata")
3122 || startswith (o
->name
, ".rsrc"))
3128 /* Skip sweeping sections already excluded. */
3129 if (o
->flags
& SEC_EXCLUDE
)
3132 /* Since this is early in the link process, it is simple
3133 to remove a section from the output. */
3134 o
->flags
|= SEC_EXCLUDE
;
3136 if (info
->print_gc_sections
&& o
->size
!= 0)
3137 /* xgettext: c-format */
3138 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3142 /* But we also have to update some of the relocation
3143 info we collected before. */
3145 && (o
->flags
& SEC_RELOC
) != 0
3146 && o
->reloc_count
> 0
3147 && !bfd_is_abs_section (o
->output_section
))
3149 struct internal_reloc
*internal_relocs
;
3153 = _bfd_coff_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
3155 if (internal_relocs
== NULL
)
3158 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
3160 if (coff_section_data (o
)->relocs
!= internal_relocs
)
3161 free (internal_relocs
);
3170 /* Remove the symbols that were in the swept sections from the dynamic
3172 coff_link_hash_traverse (coff_hash_table (info
), coff_gc_sweep_symbol
,
3178 /* Keep all sections containing symbols undefined on the command-line,
3179 and the section containing the entry symbol. */
3182 _bfd_coff_gc_keep (struct bfd_link_info
*info
)
3184 struct bfd_sym_chain
*sym
;
3186 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
3188 struct coff_link_hash_entry
*h
;
3190 h
= coff_link_hash_lookup (coff_hash_table (info
), sym
->name
,
3191 false, false, false);
3194 && (h
->root
.type
== bfd_link_hash_defined
3195 || h
->root
.type
== bfd_link_hash_defweak
)
3196 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
3197 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
3201 /* Do mark and sweep of unused sections. */
3204 bfd_coff_gc_sections (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_link_info
*info
)
3208 /* FIXME: Should we implement this? */
3210 const bfd_coff_backend_data
*bed
= coff_backend_info (abfd
);
3212 if (!bed
->can_gc_sections
3213 || !is_coff_hash_table (info
->hash
))
3215 _bfd_error_handler(_("warning: gc-sections option ignored"));
3220 _bfd_coff_gc_keep (info
);
3222 /* Grovel through relocs to find out who stays ... */
3223 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3227 if (bfd_get_flavour (sub
) != bfd_target_coff_flavour
)
3230 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3232 if (((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
3233 || startswith (o
->name
, ".vectors")
3234 || startswith (o
->name
, ".ctors")
3235 || startswith (o
->name
, ".dtors"))
3238 if (!_bfd_coff_gc_mark (info
, o
, _bfd_coff_gc_mark_hook
))
3244 /* Allow the backend to mark additional target specific sections. */
3245 _bfd_coff_gc_mark_extra_sections (info
, _bfd_coff_gc_mark_hook
);
3247 /* ... and mark SEC_EXCLUDE for those that go. */
3248 return coff_gc_sweep (abfd
, info
);
3251 /* Return name used to identify a comdat group. */
3254 bfd_coff_group_name (bfd
*abfd
, const asection
*sec
)
3256 struct coff_comdat_info
*ci
= bfd_coff_get_comdat_section (abfd
, sec
);
3263 _bfd_coff_free_cached_info (bfd
*abfd
)
3265 struct coff_tdata
*tdata
;
3267 if (bfd_family_coff (abfd
)
3268 && (bfd_get_format (abfd
) == bfd_object
3269 || bfd_get_format (abfd
) == bfd_core
)
3270 && (tdata
= coff_data (abfd
)) != NULL
)
3272 if (tdata
->section_by_index
)
3274 htab_delete (tdata
->section_by_index
);
3275 tdata
->section_by_index
= NULL
;
3278 if (tdata
->section_by_target_index
)
3280 htab_delete (tdata
->section_by_target_index
);
3281 tdata
->section_by_target_index
= NULL
;
3284 if (obj_pe (abfd
) && pe_data (abfd
)->comdat_hash
)
3286 htab_delete (pe_data (abfd
)->comdat_hash
);
3287 pe_data (abfd
)->comdat_hash
= NULL
;
3290 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
3291 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
3294 Do not clear the keep_syms and keep_strings flags.
3295 These may have been set by pe_ILF_build_a_bfd() indicating
3296 that the syms and strings pointers are not to be freed. */
3297 if (!_bfd_coff_free_symbols (abfd
))
3301 return _bfd_generic_bfd_free_cached_info (abfd
);