1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2023 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"
46 /* Take a section header read from a coff file (in HOST byte order),
47 and make a BFD "section" out of it. This is used by ECOFF. */
50 make_a_section_from_file (bfd
*abfd
,
51 struct internal_scnhdr
*hdr
,
52 unsigned int target_index
)
61 /* Handle long section names as in PE. On reading, we want to
62 accept long names if the format permits them at all, regardless
63 of the current state of the flag that dictates if we would generate
64 them in outputs; this construct checks if that is the case by
65 attempting to set the flag, without changing its state; the call
66 will fail for formats that do not support long names at all. */
67 if (bfd_coff_set_long_section_names (abfd
, bfd_coff_long_section_names (abfd
))
68 && hdr
->s_name
[0] == '/')
75 /* Flag that this BFD uses long names, even though the format might
76 expect them to be off by default. This won't directly affect the
77 format of any output BFD created from this one, but the information
78 can be used to decide what to do. */
79 bfd_coff_set_long_section_names (abfd
, true);
80 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
81 buf
[SCNNMLEN
- 1] = '\0';
82 strindex
= strtol (buf
, &p
, 10);
83 if (*p
== '\0' && strindex
>= 0)
85 strings
= _bfd_coff_read_string_table (abfd
);
88 if ((bfd_size_type
)(strindex
+ 2) >= obj_coff_strings_len (abfd
))
91 name
= (char *) bfd_alloc (abfd
,
92 (bfd_size_type
) strlen (strings
) + 1 + 1);
95 strcpy (name
, strings
);
101 /* Assorted wastage to null-terminate the name, thanks AT&T! */
102 name
= (char *) bfd_alloc (abfd
,
103 (bfd_size_type
) sizeof (hdr
->s_name
) + 1 + 1);
106 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
107 name
[sizeof (hdr
->s_name
)] = 0;
110 newsect
= bfd_make_section_anyway (abfd
, name
);
114 newsect
->vma
= hdr
->s_vaddr
;
115 newsect
->lma
= hdr
->s_paddr
;
116 newsect
->size
= hdr
->s_size
;
117 newsect
->filepos
= hdr
->s_scnptr
;
118 newsect
->rel_filepos
= hdr
->s_relptr
;
119 newsect
->reloc_count
= hdr
->s_nreloc
;
121 bfd_coff_set_alignment_hook (abfd
, newsect
, hdr
);
123 newsect
->line_filepos
= hdr
->s_lnnoptr
;
125 newsect
->lineno_count
= hdr
->s_nlnno
;
126 newsect
->userdata
= NULL
;
127 newsect
->next
= NULL
;
128 newsect
->target_index
= target_index
;
130 if (!bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, newsect
, &flags
))
133 /* At least on i386-coff, the line number count for a shared library
134 section must be ignored. */
135 if ((flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
136 newsect
->lineno_count
= 0;
138 if (hdr
->s_nreloc
!= 0)
140 /* FIXME: should this check 'hdr->s_size > 0'. */
141 if (hdr
->s_scnptr
!= 0)
142 flags
|= SEC_HAS_CONTENTS
;
144 newsect
->flags
= flags
;
146 /* Compress/decompress DWARF debug sections. */
147 if ((flags
& SEC_DEBUGGING
) != 0
148 && (flags
& SEC_HAS_CONTENTS
) != 0
149 && (startswith (name
, ".debug_")
150 || startswith (name
, ".zdebug_")
151 || startswith (name
, ".gnu.debuglto_.debug_")
152 || startswith (name
, ".gnu.linkonce.wi.")))
154 enum { nothing
, compress
, decompress
} action
= nothing
;
156 if (bfd_is_section_compressed (abfd
, newsect
))
158 /* Compressed section. Check if we should decompress. */
159 if ((abfd
->flags
& BFD_DECOMPRESS
))
164 /* Normal section. Check if we should compress. */
165 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
169 if (action
== compress
)
171 if (!bfd_init_section_compress_status (abfd
, newsect
))
174 /* xgettext:c-format */
175 (_("%pB: unable to compress section %s"), abfd
, name
);
179 else if (action
== decompress
)
181 if (!bfd_init_section_decompress_status (abfd
, newsect
))
184 /* xgettext:c-format */
185 (_("%pB: unable to decompress section %s"), abfd
, name
);
188 if (abfd
->is_linker_input
191 /* Rename section from .zdebug_* to .debug_* so that ld
192 scripts will see this section as a debug section. */
193 char *new_name
= bfd_zdebug_name_to_debug (abfd
, name
);
194 if (new_name
== NULL
)
196 bfd_rename_section (newsect
, new_name
);
204 /* Read in a COFF object and make it into a BFD. This is used by
207 coff_real_object_p (bfd
*,
209 struct internal_filehdr
*,
210 struct internal_aouthdr
*);
212 coff_real_object_p (bfd
*abfd
,
214 struct internal_filehdr
*internal_f
,
215 struct internal_aouthdr
*internal_a
)
217 flagword oflags
= abfd
->flags
;
218 bfd_vma ostart
= bfd_get_start_address (abfd
);
221 bfd_size_type readsize
; /* Length of file_info. */
223 char *external_sections
;
225 if (!(internal_f
->f_flags
& F_RELFLG
))
226 abfd
->flags
|= HAS_RELOC
;
227 if ((internal_f
->f_flags
& F_EXEC
))
228 abfd
->flags
|= EXEC_P
;
229 if (!(internal_f
->f_flags
& F_LNNO
))
230 abfd
->flags
|= HAS_LINENO
;
231 if (!(internal_f
->f_flags
& F_LSYMS
))
232 abfd
->flags
|= HAS_LOCALS
;
234 /* FIXME: How can we set D_PAGED correctly? */
235 if ((internal_f
->f_flags
& F_EXEC
) != 0)
236 abfd
->flags
|= D_PAGED
;
238 abfd
->symcount
= internal_f
->f_nsyms
;
239 if (internal_f
->f_nsyms
)
240 abfd
->flags
|= HAS_SYMS
;
242 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
243 abfd
->start_address
= internal_a
->entry
;
245 abfd
->start_address
= 0;
247 /* Set up the tdata area. ECOFF uses its own routine, and overrides
249 tdata_save
= abfd
->tdata
.any
;
250 tdata
= bfd_coff_mkobject_hook (abfd
, (void *) internal_f
, (void *) internal_a
);
254 scnhsz
= bfd_coff_scnhsz (abfd
);
255 readsize
= (bfd_size_type
) nscns
* scnhsz
;
256 external_sections
= (char *) _bfd_alloc_and_read (abfd
, readsize
, readsize
);
257 if (!external_sections
)
260 /* Set the arch/mach *before* swapping in sections; section header swapping
261 may depend on arch/mach info. */
262 if (! bfd_coff_set_arch_mach_hook (abfd
, (void *) internal_f
))
265 /* Now copy data as required; construct all asections etc. */
269 for (i
= 0; i
< nscns
; i
++)
271 struct internal_scnhdr tmp
;
272 bfd_coff_swap_scnhdr_in (abfd
,
273 (void *) (external_sections
+ i
* scnhsz
),
275 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
280 _bfd_coff_free_symbols (abfd
);
281 return _bfd_no_cleanup
;
284 _bfd_coff_free_symbols (abfd
);
285 bfd_release (abfd
, tdata
);
287 abfd
->tdata
.any
= tdata_save
;
288 abfd
->flags
= oflags
;
289 abfd
->start_address
= ostart
;
293 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
294 not a COFF file. This is also used by ECOFF. */
297 coff_object_p (bfd
*abfd
)
299 bfd_size_type filhsz
;
300 bfd_size_type aoutsz
;
303 struct internal_filehdr internal_f
;
304 struct internal_aouthdr internal_a
;
306 /* Figure out how much to read. */
307 filhsz
= bfd_coff_filhsz (abfd
);
308 aoutsz
= bfd_coff_aoutsz (abfd
);
310 filehdr
= _bfd_alloc_and_read (abfd
, filhsz
, filhsz
);
313 if (bfd_get_error () != bfd_error_system_call
)
314 bfd_set_error (bfd_error_wrong_format
);
317 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
318 bfd_release (abfd
, filehdr
);
320 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
321 (less than aoutsz) used in object files and AOUTSZ (equal to
322 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
323 expects this header to be aoutsz bytes in length, so we use that
324 value in the call to bfd_alloc below. But we must be careful to
325 only read in f_opthdr bytes in the call to bfd_bread. We should
326 also attempt to catch corrupt or non-COFF binaries with a strange
327 value for f_opthdr. */
328 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
329 || internal_f
.f_opthdr
> aoutsz
)
331 bfd_set_error (bfd_error_wrong_format
);
334 nscns
= internal_f
.f_nscns
;
336 if (internal_f
.f_opthdr
)
340 opthdr
= _bfd_alloc_and_read (abfd
, aoutsz
, internal_f
.f_opthdr
);
343 /* PR 17512: file: 11056-1136-0.004. */
344 if (internal_f
.f_opthdr
< aoutsz
)
345 memset (((char *) opthdr
) + internal_f
.f_opthdr
, 0,
346 aoutsz
- internal_f
.f_opthdr
);
348 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (void *) &internal_a
);
349 bfd_release (abfd
, opthdr
);
352 return coff_real_object_p (abfd
, nscns
, &internal_f
,
353 (internal_f
.f_opthdr
!= 0
355 : (struct internal_aouthdr
*) NULL
));
358 /* Get the BFD section from a COFF symbol section number. */
361 coff_section_from_bfd_index (bfd
*abfd
, int section_index
)
363 struct bfd_section
*answer
= abfd
->sections
;
365 if (section_index
== N_ABS
)
366 return bfd_abs_section_ptr
;
367 if (section_index
== N_UNDEF
)
368 return bfd_und_section_ptr
;
369 if (section_index
== N_DEBUG
)
370 return bfd_abs_section_ptr
;
374 if (answer
->target_index
== section_index
)
376 answer
= answer
->next
;
379 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
380 has a bad symbol table in biglitpow.o. */
381 return bfd_und_section_ptr
;
384 /* Get the upper bound of a COFF symbol table. */
387 coff_get_symtab_upper_bound (bfd
*abfd
)
389 if (!bfd_coff_slurp_symbol_table (abfd
))
392 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
395 /* Canonicalize a COFF symbol table. */
398 coff_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
400 unsigned int counter
;
401 coff_symbol_type
*symbase
;
402 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
404 if (!bfd_coff_slurp_symbol_table (abfd
))
407 symbase
= obj_symbols (abfd
);
408 counter
= bfd_get_symcount (abfd
);
409 while (counter
-- > 0)
410 *location
++ = symbase
++;
414 return bfd_get_symcount (abfd
);
417 /* Get the name of a symbol. The caller must pass in a buffer of size
421 _bfd_coff_internal_syment_name (bfd
*abfd
,
422 const struct internal_syment
*sym
,
425 /* FIXME: It's not clear this will work correctly if sizeof
427 if (sym
->_n
._n_n
._n_zeroes
!= 0
428 || sym
->_n
._n_n
._n_offset
== 0)
430 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
431 buf
[SYMNMLEN
] = '\0';
438 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
439 strings
= obj_coff_strings (abfd
);
442 strings
= _bfd_coff_read_string_table (abfd
);
446 /* PR 17910: Only check for string overflow if the length has been set.
447 Some DLLs, eg those produced by Visual Studio, may not set the length field. */
448 if (obj_coff_strings_len (abfd
) > 0
449 && sym
->_n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
))
451 return strings
+ sym
->_n
._n_n
._n_offset
;
455 /* Read in and swap the relocs. This returns a buffer holding the
456 relocs for section SEC in file ABFD. If CACHE is TRUE and
457 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
458 the function is called again. If EXTERNAL_RELOCS is not NULL, it
459 is a buffer large enough to hold the unswapped relocs. If
460 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
461 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
462 value must be INTERNAL_RELOCS. The function returns NULL on error. */
464 struct internal_reloc
*
465 _bfd_coff_read_internal_relocs (bfd
*abfd
,
468 bfd_byte
*external_relocs
,
469 bool require_internal
,
470 struct internal_reloc
*internal_relocs
)
473 bfd_byte
*free_external
= NULL
;
474 struct internal_reloc
*free_internal
= NULL
;
477 struct internal_reloc
*irel
;
480 if (sec
->reloc_count
== 0)
481 return internal_relocs
; /* Nothing to do. */
483 if (coff_section_data (abfd
, sec
) != NULL
484 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
486 if (! require_internal
)
487 return coff_section_data (abfd
, sec
)->relocs
;
488 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
489 sec
->reloc_count
* sizeof (struct internal_reloc
));
490 return internal_relocs
;
493 relsz
= bfd_coff_relsz (abfd
);
495 amt
= sec
->reloc_count
* relsz
;
496 if (external_relocs
== NULL
)
498 free_external
= (bfd_byte
*) bfd_malloc (amt
);
499 if (free_external
== NULL
)
501 external_relocs
= free_external
;
504 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
505 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
508 if (internal_relocs
== NULL
)
510 amt
= sec
->reloc_count
;
511 amt
*= sizeof (struct internal_reloc
);
512 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
513 if (free_internal
== NULL
)
515 internal_relocs
= free_internal
;
518 /* Swap in the relocs. */
519 erel
= external_relocs
;
520 erel_end
= erel
+ relsz
* sec
->reloc_count
;
521 irel
= internal_relocs
;
522 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
523 bfd_coff_swap_reloc_in (abfd
, (void *) erel
, (void *) irel
);
525 free (free_external
);
526 free_external
= NULL
;
528 if (cache
&& free_internal
!= NULL
)
530 if (coff_section_data (abfd
, sec
) == NULL
)
532 amt
= sizeof (struct coff_section_tdata
);
533 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
534 if (sec
->used_by_bfd
== NULL
)
536 coff_section_data (abfd
, sec
)->contents
= NULL
;
538 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
541 return internal_relocs
;
544 free (free_external
);
545 free (free_internal
);
549 /* Set lineno_count for the output sections of a COFF file. */
552 coff_count_linenumbers (bfd
*abfd
)
554 unsigned int limit
= bfd_get_symcount (abfd
);
562 /* This may be from the backend linker, in which case the
563 lineno_count in the sections is correct. */
564 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
565 total
+= s
->lineno_count
;
569 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
570 BFD_ASSERT (s
->lineno_count
== 0);
572 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
574 asymbol
*q_maybe
= *p
;
576 if (bfd_asymbol_bfd (q_maybe
) != NULL
577 && bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
579 coff_symbol_type
*q
= coffsymbol (q_maybe
);
581 /* The AIX 4.1 compiler can sometimes generate line numbers
582 attached to debugging symbols. We try to simply ignore
584 if (q
->lineno
!= NULL
585 && q
->symbol
.section
->owner
!= NULL
)
587 /* This symbol has line numbers. Increment the owning
588 section's linenumber count. */
589 alent
*l
= q
->lineno
;
593 asection
* sec
= q
->symbol
.section
->output_section
;
595 /* Do not try to update fields in read-only sections. */
596 if (! bfd_is_const_section (sec
))
597 sec
->lineno_count
++;
602 while (l
->line_number
!= 0);
611 fixup_symbol_value (bfd
*abfd
,
612 coff_symbol_type
*coff_symbol_ptr
,
613 struct internal_syment
*syment
)
615 /* Normalize the symbol flags. */
616 if (coff_symbol_ptr
->symbol
.section
617 && bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
619 /* A common symbol is undefined with a value. */
620 syment
->n_scnum
= N_UNDEF
;
621 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
623 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
624 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
626 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
628 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
630 syment
->n_scnum
= N_UNDEF
;
633 /* FIXME: Do we need to handle the absolute section here? */
636 if (coff_symbol_ptr
->symbol
.section
)
639 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
641 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
642 + coff_symbol_ptr
->symbol
.section
->output_offset
);
645 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
646 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
647 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
653 /* This can happen, but I don't know why yet (steve@cygnus.com) */
654 syment
->n_scnum
= N_ABS
;
655 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
660 /* Run through all the symbols in the symbol table and work out what
661 their indexes into the symbol table will be when output.
663 Coff requires that each C_FILE symbol points to the next one in the
664 chain, and that the last one points to the first external symbol. We
668 coff_renumber_symbols (bfd
*bfd_ptr
, int *first_undef
)
670 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
671 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
672 unsigned int native_index
= 0;
673 struct internal_syment
*last_file
= NULL
;
674 unsigned int symbol_index
;
676 /* COFF demands that undefined symbols come after all other symbols.
677 Since we don't need to impose this extra knowledge on all our
678 client programs, deal with that here. Sort the symbol table;
679 just move the undefined symbols to the end, leaving the rest
680 alone. The O'Reilly book says that defined global symbols come
681 at the end before the undefined symbols, so we do that here as
683 /* @@ Do we have some condition we could test for, so we don't always
684 have to do this? I don't think relocatability is quite right, but
685 I'm not certain. [raeburn:19920508.1711EST] */
691 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
692 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
695 bfd_ptr
->outsymbols
= newsyms
;
696 for (i
= 0; i
< symbol_count
; i
++)
697 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
698 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
699 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
700 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
701 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
703 *newsyms
++ = symbol_ptr_ptr
[i
];
705 for (i
= 0; i
< symbol_count
; i
++)
706 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
707 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
708 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
709 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
710 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
712 *newsyms
++ = symbol_ptr_ptr
[i
];
714 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
716 for (i
= 0; i
< symbol_count
; i
++)
717 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
718 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
719 *newsyms
++ = symbol_ptr_ptr
[i
];
720 *newsyms
= (asymbol
*) NULL
;
721 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
724 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
726 coff_symbol_type
*coff_symbol_ptr
;
728 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
729 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
730 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
732 combined_entry_type
*s
= coff_symbol_ptr
->native
;
735 BFD_ASSERT (s
->is_sym
);
736 if (s
->u
.syment
.n_sclass
== C_FILE
)
738 if (last_file
!= NULL
)
739 last_file
->n_value
= native_index
;
740 last_file
= &(s
->u
.syment
);
743 /* Modify the symbol values according to their section and
745 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
747 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
748 s
[i
].offset
= native_index
++;
754 obj_conv_table_size (bfd_ptr
) = native_index
;
759 /* Run thorough the symbol table again, and fix it so that all
760 pointers to entries are changed to the entries' index in the output
764 coff_mangle_symbols (bfd
*bfd_ptr
)
766 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
767 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
768 unsigned int symbol_index
;
770 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
772 coff_symbol_type
*coff_symbol_ptr
;
774 coff_symbol_ptr
= coff_symbol_from (symbol_ptr_ptr
[symbol_index
]);
775 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
778 combined_entry_type
*s
= coff_symbol_ptr
->native
;
780 BFD_ASSERT (s
->is_sym
);
783 /* FIXME: We should use a union here. */
784 s
->u
.syment
.n_value
=
785 (uintptr_t) ((combined_entry_type
*)
786 (uintptr_t) s
->u
.syment
.n_value
)->offset
;
791 /* The value is the offset into the line number entries
792 for the symbol's section. On output, the symbol's
793 section should be N_DEBUG. */
794 s
->u
.syment
.n_value
=
795 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
796 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
797 coff_symbol_ptr
->symbol
.section
=
798 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
799 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
801 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
803 combined_entry_type
*a
= s
+ i
+ 1;
805 BFD_ASSERT (! a
->is_sym
);
808 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
809 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
814 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
815 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
820 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
821 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
830 coff_write_auxent_fname (bfd
*abfd
,
832 union internal_auxent
*auxent
,
833 struct bfd_strtab_hash
*strtab
,
836 unsigned int str_length
= strlen (str
);
837 unsigned int filnmlen
= bfd_coff_filnmlen (abfd
);
839 if (bfd_coff_long_filenames (abfd
))
841 if (str_length
<= filnmlen
)
842 strncpy (auxent
->x_file
.x_n
.x_fname
, str
, filnmlen
);
845 bfd_size_type indx
= _bfd_stringtab_add (strtab
, str
, hash
, false);
847 if (indx
== (bfd_size_type
) -1)
850 auxent
->x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
851 auxent
->x_file
.x_n
.x_n
.x_zeroes
= 0;
856 strncpy (auxent
->x_file
.x_n
.x_fname
, str
, filnmlen
);
857 if (str_length
> filnmlen
)
858 str
[filnmlen
] = '\0';
865 coff_fix_symbol_name (bfd
*abfd
,
867 combined_entry_type
*native
,
868 struct bfd_strtab_hash
*strtab
,
870 asection
**debug_string_section_p
,
871 bfd_size_type
*debug_string_size_p
)
873 unsigned int name_length
;
874 char *name
= (char *) (symbol
->name
);
879 /* COFF symbols always have names, so we'll make one up. */
880 symbol
->name
= "strange";
881 name
= (char *) symbol
->name
;
883 name_length
= strlen (name
);
885 BFD_ASSERT (native
->is_sym
);
886 if (native
->u
.syment
.n_sclass
== C_FILE
887 && native
->u
.syment
.n_numaux
> 0)
889 if (bfd_coff_force_symnames_in_strings (abfd
))
891 indx
= _bfd_stringtab_add (strtab
, ".file", hash
, false);
892 if (indx
== (bfd_size_type
) -1)
895 native
->u
.syment
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
896 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
899 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
901 BFD_ASSERT (! (native
+ 1)->is_sym
);
902 if (!coff_write_auxent_fname (abfd
, name
, &(native
+ 1)->u
.auxent
,
908 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
909 /* This name will fit into the symbol neatly. */
910 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
912 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
914 indx
= _bfd_stringtab_add (strtab
, name
, hash
, false);
915 if (indx
== (bfd_size_type
) -1)
918 native
->u
.syment
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
919 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
925 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
927 /* This name should be written into the .debug section. For
928 some reason each name is preceded by a two byte length
929 and also followed by a null byte. FIXME: We assume that
930 the .debug section has already been created, and that it
932 if (*debug_string_section_p
== (asection
*) NULL
)
933 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
934 filepos
= bfd_tell (abfd
);
936 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
938 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
940 if (!bfd_set_section_contents (abfd
,
941 *debug_string_section_p
,
943 (file_ptr
) *debug_string_size_p
,
944 (bfd_size_type
) prefix_len
)
945 || !bfd_set_section_contents (abfd
,
946 *debug_string_section_p
,
947 (void *) symbol
->name
,
948 (file_ptr
) (*debug_string_size_p
950 (bfd_size_type
) name_length
+ 1))
952 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
954 native
->u
.syment
._n
._n_n
._n_offset
=
955 *debug_string_size_p
+ prefix_len
;
956 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
957 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
964 /* We need to keep track of the symbol index so that when we write out
965 the relocs we can get the index for a symbol. This method is a
968 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
970 /* Write a symbol out to a COFF file. */
973 coff_write_symbol (bfd
*abfd
,
975 combined_entry_type
*native
,
977 struct bfd_strtab_hash
*strtab
,
979 asection
**debug_string_section_p
,
980 bfd_size_type
*debug_string_size_p
)
982 unsigned int numaux
= native
->u
.syment
.n_numaux
;
983 int type
= native
->u
.syment
.n_type
;
984 int n_sclass
= (int) native
->u
.syment
.n_sclass
;
985 asection
*output_section
= symbol
->section
->output_section
986 ? symbol
->section
->output_section
989 bfd_size_type symesz
;
991 BFD_ASSERT (native
->is_sym
);
993 if (native
->u
.syment
.n_sclass
== C_FILE
)
994 symbol
->flags
|= BSF_DEBUGGING
;
996 if (symbol
->flags
& BSF_DEBUGGING
997 && bfd_is_abs_section (symbol
->section
))
998 native
->u
.syment
.n_scnum
= N_DEBUG
;
1000 else if (bfd_is_abs_section (symbol
->section
))
1001 native
->u
.syment
.n_scnum
= N_ABS
;
1003 else if (bfd_is_und_section (symbol
->section
))
1004 native
->u
.syment
.n_scnum
= N_UNDEF
;
1007 native
->u
.syment
.n_scnum
=
1008 output_section
->target_index
;
1010 if (!coff_fix_symbol_name (abfd
, symbol
, native
, strtab
, hash
,
1011 debug_string_section_p
, debug_string_size_p
))
1014 symesz
= bfd_coff_symesz (abfd
);
1015 buf
= bfd_alloc (abfd
, symesz
);
1018 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
1019 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
1021 bfd_release (abfd
, buf
);
1023 if (native
->u
.syment
.n_numaux
> 0)
1025 bfd_size_type auxesz
;
1028 auxesz
= bfd_coff_auxesz (abfd
);
1029 buf
= bfd_alloc (abfd
, auxesz
);
1032 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
1034 BFD_ASSERT (! (native
+ j
+ 1)->is_sym
);
1036 /* Adjust auxent only if this isn't the filename
1038 if (native
->u
.syment
.n_sclass
== C_FILE
1039 && (native
+ j
+ 1)->u
.auxent
.x_file
.x_ftype
1040 && (native
+ j
+ 1)->extrap
)
1041 coff_write_auxent_fname (abfd
, (char *) (native
+ j
+ 1)->extrap
,
1042 &(native
+ j
+ 1)->u
.auxent
, strtab
, hash
);
1044 bfd_coff_swap_aux_out (abfd
,
1045 &((native
+ j
+ 1)->u
.auxent
),
1046 type
, n_sclass
, (int) j
,
1047 native
->u
.syment
.n_numaux
,
1049 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
1052 bfd_release (abfd
, buf
);
1055 /* Store the index for use when we write out the relocs. */
1056 set_index (symbol
, *written
);
1058 *written
+= numaux
+ 1;
1062 /* Write out a symbol to a COFF file that does not come from a COFF
1063 file originally. This symbol may have been created by the linker,
1064 or we may be linking a non COFF file to a COFF file. */
1067 coff_write_alien_symbol (bfd
*abfd
,
1069 struct internal_syment
*isym
,
1071 struct bfd_strtab_hash
*strtab
,
1073 asection
**debug_string_section_p
,
1074 bfd_size_type
*debug_string_size_p
)
1076 combined_entry_type
*native
;
1077 combined_entry_type dummy
[2];
1078 asection
*output_section
= symbol
->section
->output_section
1079 ? symbol
->section
->output_section
1081 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1084 if ((!link_info
|| link_info
->strip_discarded
)
1085 && !bfd_is_abs_section (symbol
->section
)
1086 && symbol
->section
->output_section
== bfd_abs_section_ptr
)
1090 memset (isym
, 0, sizeof (*isym
));
1093 memset (dummy
, 0, sizeof dummy
);
1095 native
->is_sym
= true;
1096 native
[1].is_sym
= false;
1097 native
->u
.syment
.n_type
= T_NULL
;
1098 native
->u
.syment
.n_flags
= 0;
1099 native
->u
.syment
.n_numaux
= 0;
1100 if (bfd_is_und_section (symbol
->section
))
1102 native
->u
.syment
.n_scnum
= N_UNDEF
;
1103 native
->u
.syment
.n_value
= symbol
->value
;
1105 else if (bfd_is_com_section (symbol
->section
))
1107 native
->u
.syment
.n_scnum
= N_UNDEF
;
1108 native
->u
.syment
.n_value
= symbol
->value
;
1110 else if (symbol
->flags
& BSF_FILE
)
1112 native
->u
.syment
.n_scnum
= N_DEBUG
;
1113 native
->u
.syment
.n_numaux
= 1;
1115 else if (symbol
->flags
& BSF_DEBUGGING
)
1117 /* There isn't much point to writing out a debugging symbol
1118 unless we are prepared to convert it into COFF debugging
1119 format. So, we just ignore them. We must clobber the symbol
1120 name to keep it from being put in the string table. */
1123 memset (isym
, 0, sizeof (*isym
));
1128 native
->u
.syment
.n_scnum
= output_section
->target_index
;
1129 native
->u
.syment
.n_value
= (symbol
->value
1130 + symbol
->section
->output_offset
);
1131 if (! obj_pe (abfd
))
1132 native
->u
.syment
.n_value
+= output_section
->vma
;
1134 /* Copy the any flags from the file header into the symbol.
1137 coff_symbol_type
*c
= coff_symbol_from (symbol
);
1138 if (c
!= (coff_symbol_type
*) NULL
)
1139 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1143 native
->u
.syment
.n_type
= 0;
1144 if (symbol
->flags
& BSF_FILE
)
1145 native
->u
.syment
.n_sclass
= C_FILE
;
1146 else if (symbol
->flags
& BSF_LOCAL
)
1147 native
->u
.syment
.n_sclass
= C_STAT
;
1148 else if (symbol
->flags
& BSF_WEAK
)
1149 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1151 native
->u
.syment
.n_sclass
= C_EXT
;
1153 ret
= coff_write_symbol (abfd
, symbol
, native
, written
, strtab
, hash
,
1154 debug_string_section_p
, debug_string_size_p
);
1156 *isym
= native
->u
.syment
;
1160 /* Write a native symbol to a COFF file. */
1163 coff_write_native_symbol (bfd
*abfd
,
1164 coff_symbol_type
*symbol
,
1166 struct bfd_strtab_hash
*strtab
,
1167 asection
**debug_string_section_p
,
1168 bfd_size_type
*debug_string_size_p
)
1170 combined_entry_type
*native
= symbol
->native
;
1171 alent
*lineno
= symbol
->lineno
;
1172 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1174 if ((!link_info
|| link_info
->strip_discarded
)
1175 && !bfd_is_abs_section (symbol
->symbol
.section
)
1176 && symbol
->symbol
.section
->output_section
== bfd_abs_section_ptr
)
1178 symbol
->symbol
.name
= "";
1182 BFD_ASSERT (native
->is_sym
);
1183 /* If this symbol has an associated line number, we must store the
1184 symbol index in the line number field. We also tag the auxent to
1185 point to the right place in the lineno table. */
1186 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1188 unsigned int count
= 0;
1190 lineno
[count
].u
.offset
= *written
;
1191 if (native
->u
.syment
.n_numaux
)
1193 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1195 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1196 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1199 /* Count and relocate all other linenumbers. */
1201 while (lineno
[count
].line_number
!= 0)
1203 lineno
[count
].u
.offset
+=
1204 (symbol
->symbol
.section
->output_section
->vma
1205 + symbol
->symbol
.section
->output_offset
);
1208 symbol
->done_lineno
= true;
1210 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1211 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1212 count
* bfd_coff_linesz (abfd
);
1215 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1216 strtab
, true, debug_string_section_p
,
1217 debug_string_size_p
);
1221 null_error_handler (const char *fmt ATTRIBUTE_UNUSED
,
1222 va_list ap ATTRIBUTE_UNUSED
)
1226 /* Write out the COFF symbols. */
1229 coff_write_symbols (bfd
*abfd
)
1231 struct bfd_strtab_hash
*strtab
;
1232 asection
*debug_string_section
;
1233 bfd_size_type debug_string_size
;
1235 unsigned int limit
= bfd_get_symcount (abfd
);
1236 bfd_vma written
= 0;
1239 debug_string_section
= NULL
;
1240 debug_string_size
= 0;
1242 strtab
= _bfd_stringtab_init ();
1246 /* If this target supports long section names, they must be put into
1247 the string table. This is supported by PE. This code must
1248 handle section names just as they are handled in
1249 coff_write_object_contents. This is why we pass hash as FALSE below. */
1250 if (bfd_coff_long_section_names (abfd
))
1254 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1255 if (strlen (o
->name
) > SCNNMLEN
1256 && _bfd_stringtab_add (strtab
, o
->name
, false, false)
1257 == (bfd_size_type
) -1)
1261 /* Seek to the right place. */
1262 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1265 /* Output all the symbols we have. */
1267 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1269 asymbol
*symbol
= *p
;
1270 coff_symbol_type
*c_symbol
= coff_symbol_from (symbol
);
1272 if (c_symbol
== (coff_symbol_type
*) NULL
1273 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1275 if (!coff_write_alien_symbol (abfd
, symbol
, NULL
, &written
,
1276 strtab
, true, &debug_string_section
,
1277 &debug_string_size
))
1282 if (coff_backend_info (abfd
)->_bfd_coff_classify_symbol
!= NULL
)
1284 bfd_error_handler_type current_error_handler
;
1285 enum coff_symbol_classification sym_class
;
1286 unsigned char *n_sclass
;
1288 /* Suppress error reporting by bfd_coff_classify_symbol.
1289 Error messages can be generated when we are processing a local
1290 symbol which has no associated section and we do not have to
1291 worry about this, all we need to know is that it is local. */
1292 current_error_handler
= bfd_set_error_handler (null_error_handler
);
1293 BFD_ASSERT (c_symbol
->native
->is_sym
);
1294 sym_class
= bfd_coff_classify_symbol (abfd
,
1295 &c_symbol
->native
->u
.syment
);
1296 (void) bfd_set_error_handler (current_error_handler
);
1298 n_sclass
= &c_symbol
->native
->u
.syment
.n_sclass
;
1300 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1301 we cannot retain the existing sclass from the original symbol.
1302 Weak symbols only have one valid sclass, so just set it always.
1303 If it is not local class and should be, set it C_STAT.
1304 If it is global and not classified as global, or if it is
1305 weak (which is also classified as global), set it C_EXT. */
1307 if (symbol
->flags
& BSF_WEAK
)
1308 *n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1309 else if (symbol
->flags
& BSF_LOCAL
&& sym_class
!= COFF_SYMBOL_LOCAL
)
1311 else if (symbol
->flags
& BSF_GLOBAL
1312 && (sym_class
!= COFF_SYMBOL_GLOBAL
1314 || *n_sclass
== C_NT_WEAK
1316 || *n_sclass
== C_WEAKEXT
))
1317 c_symbol
->native
->u
.syment
.n_sclass
= C_EXT
;
1320 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1321 strtab
, &debug_string_section
,
1322 &debug_string_size
))
1327 obj_raw_syment_count (abfd
) = written
;
1329 /* Now write out strings.
1331 We would normally not write anything here if there are no strings, but
1332 we'll write out 4 so that any stupid coff reader which tries to read the
1333 string table even when there isn't one won't croak. */
1335 bfd_byte buffer
[STRING_SIZE_SIZE
];
1337 #if STRING_SIZE_SIZE == 4
1338 H_PUT_32 (abfd
, _bfd_stringtab_size (strtab
) + STRING_SIZE_SIZE
, buffer
);
1340 #error Change H_PUT_32
1342 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1346 if (! _bfd_stringtab_emit (abfd
, strtab
))
1350 _bfd_stringtab_free (strtab
);
1352 /* Make sure the .debug section was created to be the correct size.
1353 We should create it ourselves on the fly, but we don't because
1354 BFD won't let us write to any section until we know how large all
1355 the sections are. We could still do it by making another pass
1356 over the symbols. FIXME. */
1357 BFD_ASSERT (debug_string_size
== 0
1358 || (debug_string_section
!= (asection
*) NULL
1359 && (BFD_ALIGN (debug_string_size
,
1360 1 << debug_string_section
->alignment_power
)
1361 == debug_string_section
->size
)));
1367 coff_write_linenumbers (bfd
*abfd
)
1370 bfd_size_type linesz
;
1373 linesz
= bfd_coff_linesz (abfd
);
1374 buff
= bfd_alloc (abfd
, linesz
);
1377 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1379 if (s
->lineno_count
)
1381 asymbol
**q
= abfd
->outsymbols
;
1382 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1384 /* Find all the linenumbers in this section. */
1388 if (p
->section
->output_section
== s
)
1391 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1392 (bfd_asymbol_bfd (p
), p
));
1395 /* Found a linenumber entry, output. */
1396 struct internal_lineno out
;
1398 memset ((void *) & out
, 0, sizeof (out
));
1400 out
.l_addr
.l_symndx
= l
->u
.offset
;
1401 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1402 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1406 while (l
->line_number
)
1408 out
.l_lnno
= l
->line_number
;
1409 out
.l_addr
.l_symndx
= l
->u
.offset
;
1410 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1411 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1422 bfd_release (abfd
, buff
);
1427 coff_get_lineno (bfd
*ignore_abfd ATTRIBUTE_UNUSED
, asymbol
*symbol
)
1429 return coffsymbol (symbol
)->lineno
;
1432 /* This function transforms the offsets into the symbol table into
1433 pointers to syments. */
1436 coff_pointerize_aux (bfd
*abfd
,
1437 combined_entry_type
*table_base
,
1438 combined_entry_type
*symbol
,
1439 unsigned int indaux
,
1440 combined_entry_type
*auxent
,
1441 combined_entry_type
*table_end
)
1443 unsigned int type
= symbol
->u
.syment
.n_type
;
1444 unsigned int n_sclass
= symbol
->u
.syment
.n_sclass
;
1446 BFD_ASSERT (symbol
->is_sym
);
1447 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1449 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1450 (abfd
, table_base
, symbol
, indaux
, auxent
))
1454 /* Don't bother if this is a file or a section. */
1455 if (n_sclass
== C_STAT
&& type
== T_NULL
)
1457 if (n_sclass
== C_FILE
)
1459 if (n_sclass
== C_DWARF
)
1462 BFD_ASSERT (! auxent
->is_sym
);
1463 /* Otherwise patch up. */
1464 #define N_TMASK coff_data (abfd)->local_n_tmask
1465 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1467 if ((ISFCN (type
) || ISTAG (n_sclass
) || n_sclass
== C_BLOCK
1468 || n_sclass
== C_FCN
)
1469 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0
1470 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
1471 < (long) obj_raw_syment_count (abfd
)
1472 && table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
1475 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1476 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1477 auxent
->fix_end
= 1;
1480 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1481 generate one, so we must be careful to ignore it. */
1482 if ((unsigned long) auxent
->u
.auxent
.x_sym
.x_tagndx
.l
1483 < obj_raw_syment_count (abfd
)
1484 && table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
< table_end
)
1486 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1487 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1488 auxent
->fix_tag
= 1;
1492 /* Allocate space for the ".debug" section, and read it.
1493 We did not read the debug section until now, because
1494 we didn't want to go to the trouble until someone needed it. */
1497 build_debug_section (bfd
*abfd
, asection
** sect_return
)
1499 char *debug_section
;
1501 bfd_size_type sec_size
;
1503 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1507 bfd_set_error (bfd_error_no_debug_section
);
1511 /* Seek to the beginning of the `.debug' section and read it.
1512 Save the current position first; it is needed by our caller.
1513 Then read debug section and reset the file pointer. */
1515 position
= bfd_tell (abfd
);
1516 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0)
1519 sec_size
= sect
->size
;
1520 debug_section
= (char *) _bfd_alloc_and_read (abfd
, sec_size
+ 1, sec_size
);
1521 if (debug_section
== NULL
)
1523 debug_section
[sec_size
] = 0;
1525 if (bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1528 * sect_return
= sect
;
1529 return debug_section
;
1532 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1533 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1534 be \0-terminated. */
1537 copy_name (bfd
*abfd
, char *name
, size_t maxlen
)
1542 for (len
= 0; len
< maxlen
; ++len
)
1543 if (name
[len
] == '\0')
1546 if ((newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1549 strncpy (newname
, name
, len
);
1550 newname
[len
] = '\0';
1554 /* Read in the external symbols. */
1557 _bfd_coff_get_external_symbols (bfd
*abfd
)
1563 if (obj_coff_external_syms (abfd
) != NULL
)
1566 symesz
= bfd_coff_symesz (abfd
);
1567 if (_bfd_mul_overflow (obj_raw_syment_count (abfd
), symesz
, &size
))
1569 bfd_set_error (bfd_error_file_truncated
);
1576 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1578 syms
= _bfd_malloc_and_read (abfd
, size
, size
);
1579 obj_coff_external_syms (abfd
) = syms
;
1580 return syms
!= NULL
;
1583 /* Read in the external strings. The strings are not loaded until
1584 they are needed. This is because we have no simple way of
1585 detecting a missing string table in an archive. If the strings
1586 are loaded then the STRINGS and STRINGS_LEN fields in the
1587 coff_tdata structure will be set. */
1590 _bfd_coff_read_string_table (bfd
*abfd
)
1592 char extstrsize
[STRING_SIZE_SIZE
];
1593 bfd_size_type strsize
;
1600 if (obj_coff_strings (abfd
) != NULL
)
1601 return obj_coff_strings (abfd
);
1603 if (obj_sym_filepos (abfd
) == 0)
1605 bfd_set_error (bfd_error_no_symbols
);
1609 symesz
= bfd_coff_symesz (abfd
);
1610 pos
= obj_sym_filepos (abfd
);
1611 if (_bfd_mul_overflow (obj_raw_syment_count (abfd
), symesz
, &size
)
1612 || pos
+ size
< pos
)
1614 bfd_set_error (bfd_error_file_truncated
);
1618 if (bfd_seek (abfd
, pos
+ size
, SEEK_SET
) != 0)
1621 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1622 != sizeof extstrsize
)
1624 if (bfd_get_error () != bfd_error_file_truncated
)
1627 /* There is no string table. */
1628 strsize
= STRING_SIZE_SIZE
;
1632 #if STRING_SIZE_SIZE == 4
1633 strsize
= H_GET_32 (abfd
, extstrsize
);
1635 #error Change H_GET_32
1639 filesize
= bfd_get_file_size (abfd
);
1640 if (strsize
< STRING_SIZE_SIZE
1641 || (filesize
!= 0 && strsize
> filesize
))
1644 /* xgettext: c-format */
1645 (_("%pB: bad string table size %" PRIu64
), abfd
, (uint64_t) strsize
);
1646 bfd_set_error (bfd_error_bad_value
);
1650 strings
= (char *) bfd_malloc (strsize
+ 1);
1651 if (strings
== NULL
)
1654 /* PR 17521 file: 079-54929-0.004.
1655 A corrupt file could contain an index that points into the first
1656 STRING_SIZE_SIZE bytes of the string table, so make sure that
1658 memset (strings
, 0, STRING_SIZE_SIZE
);
1660 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1661 != strsize
- STRING_SIZE_SIZE
)
1667 obj_coff_strings (abfd
) = strings
;
1668 obj_coff_strings_len (abfd
) = strsize
;
1669 /* Terminate the string table, just in case. */
1670 strings
[strsize
] = 0;
1674 /* Free up the external symbols and strings read from a COFF file. */
1677 _bfd_coff_free_symbols (bfd
*abfd
)
1679 if (! bfd_family_coff (abfd
))
1682 if (obj_coff_external_syms (abfd
) != NULL
1683 && ! obj_coff_keep_syms (abfd
))
1685 free (obj_coff_external_syms (abfd
));
1686 obj_coff_external_syms (abfd
) = NULL
;
1689 if (obj_coff_strings (abfd
) != NULL
1690 && ! obj_coff_keep_strings (abfd
))
1692 free (obj_coff_strings (abfd
));
1693 obj_coff_strings (abfd
) = NULL
;
1694 obj_coff_strings_len (abfd
) = 0;
1700 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1701 knit the symbol names into a normalized form. By normalized here I
1702 mean that all symbols have an n_offset pointer that points to a null-
1703 terminated string. */
1705 combined_entry_type
*
1706 coff_get_normalized_symtab (bfd
*abfd
)
1708 combined_entry_type
*internal
;
1709 combined_entry_type
*internal_ptr
;
1710 combined_entry_type
*symbol_ptr
;
1711 combined_entry_type
*internal_end
;
1715 const char *string_table
= NULL
;
1716 asection
* debug_sec
= NULL
;
1717 char *debug_sec_data
= NULL
;
1720 if (obj_raw_syments (abfd
) != NULL
)
1721 return obj_raw_syments (abfd
);
1723 if (! _bfd_coff_get_external_symbols (abfd
))
1726 size
= obj_raw_syment_count (abfd
);
1727 /* Check for integer overflow. */
1728 if (size
> (bfd_size_type
) -1 / sizeof (combined_entry_type
))
1730 size
*= sizeof (combined_entry_type
);
1731 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1732 if (internal
== NULL
&& size
!= 0)
1734 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1736 raw_src
= (char *) obj_coff_external_syms (abfd
);
1738 /* Mark the end of the symbols. */
1739 symesz
= bfd_coff_symesz (abfd
);
1740 raw_end
= PTR_ADD (raw_src
, obj_raw_syment_count (abfd
) * symesz
);
1742 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1743 probably possible. If one shows up, it will probably kill us. */
1745 /* Swap all the raw entries. */
1746 for (internal_ptr
= internal
;
1748 raw_src
+= symesz
, internal_ptr
++)
1752 bfd_coff_swap_sym_in (abfd
, (void *) raw_src
,
1753 (void *) & internal_ptr
->u
.syment
);
1754 symbol_ptr
= internal_ptr
;
1755 internal_ptr
->is_sym
= true;
1757 /* PR 17512: Prevent buffer overrun. */
1758 if (symbol_ptr
->u
.syment
.n_numaux
> ((raw_end
- 1) - raw_src
) / symesz
)
1760 bfd_release (abfd
, internal
);
1765 i
< symbol_ptr
->u
.syment
.n_numaux
;
1771 bfd_coff_swap_aux_in (abfd
, (void *) raw_src
,
1772 symbol_ptr
->u
.syment
.n_type
,
1773 symbol_ptr
->u
.syment
.n_sclass
,
1774 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1775 &(internal_ptr
->u
.auxent
));
1777 internal_ptr
->is_sym
= false;
1778 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1779 internal_ptr
, internal_end
);
1783 /* Free the raw symbols. */
1784 if (obj_coff_external_syms (abfd
) != NULL
1785 && ! obj_coff_keep_syms (abfd
))
1787 free (obj_coff_external_syms (abfd
));
1788 obj_coff_external_syms (abfd
) = NULL
;
1791 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1794 BFD_ASSERT (internal_ptr
->is_sym
);
1796 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1797 && internal_ptr
->u
.syment
.n_numaux
> 0)
1799 combined_entry_type
* aux
= internal_ptr
+ 1;
1801 /* Make a file symbol point to the name in the auxent, since
1802 the text ".file" is redundant. */
1803 BFD_ASSERT (! aux
->is_sym
);
1805 if (aux
->u
.auxent
.x_file
.x_n
.x_n
.x_zeroes
== 0)
1807 /* The filename is a long one, point into the string table. */
1808 if (string_table
== NULL
)
1810 string_table
= _bfd_coff_read_string_table (abfd
);
1811 if (string_table
== NULL
)
1815 if ((bfd_size_type
)(aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
)
1816 >= obj_coff_strings_len (abfd
))
1817 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1818 (uintptr_t) _("<corrupt>");
1820 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1821 (uintptr_t) (string_table
1822 + aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
);
1826 /* Ordinary short filename, put into memory anyway. The
1827 Microsoft PE tools sometimes store a filename in
1828 multiple AUX entries. */
1829 if (internal_ptr
->u
.syment
.n_numaux
> 1 && obj_pe (abfd
))
1830 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1833 aux
->u
.auxent
.x_file
.x_n
.x_fname
,
1834 internal_ptr
->u
.syment
.n_numaux
* symesz
));
1836 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1839 aux
->u
.auxent
.x_file
.x_n
.x_fname
,
1840 (size_t) bfd_coff_filnmlen (abfd
)));
1843 /* Normalize other strings available in C_FILE aux entries. */
1845 for (int numaux
= 1; numaux
< internal_ptr
->u
.syment
.n_numaux
; numaux
++)
1847 aux
= internal_ptr
+ numaux
+ 1;
1848 BFD_ASSERT (! aux
->is_sym
);
1850 if (aux
->u
.auxent
.x_file
.x_n
.x_n
.x_zeroes
== 0)
1852 /* The string information is a long one, point into the string table. */
1853 if (string_table
== NULL
)
1855 string_table
= _bfd_coff_read_string_table (abfd
);
1856 if (string_table
== NULL
)
1860 if ((bfd_size_type
)(aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
)
1861 >= obj_coff_strings_len (abfd
))
1862 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1863 (uintptr_t) _("<corrupt>");
1865 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1866 (uintptr_t) (string_table
1867 + (aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
));
1870 aux
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
=
1873 aux
->u
.auxent
.x_file
.x_n
.x_fname
,
1874 (size_t) bfd_coff_filnmlen (abfd
)));
1880 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1882 /* This is a "short" name. Make it long. */
1886 /* Find the length of this string without walking into memory
1888 for (i
= 0; i
< 8; ++i
)
1889 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1892 newstring
= (char *) bfd_zalloc (abfd
, (bfd_size_type
) (i
+ 1));
1893 if (newstring
== NULL
)
1895 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1896 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (uintptr_t) newstring
;
1897 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1899 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1900 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (uintptr_t) "";
1901 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1903 /* Long name already. Point symbol at the string in the
1905 if (string_table
== NULL
)
1907 string_table
= _bfd_coff_read_string_table (abfd
);
1908 if (string_table
== NULL
)
1911 if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
>= obj_coff_strings_len (abfd
)
1912 || string_table
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
< string_table
)
1913 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1914 (uintptr_t) _("<corrupt>");
1916 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1917 ((uintptr_t) (string_table
1918 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1922 /* Long name in debug section. Very similar. */
1923 if (debug_sec_data
== NULL
)
1924 debug_sec_data
= build_debug_section (abfd
, & debug_sec
);
1925 if (debug_sec_data
!= NULL
)
1927 BFD_ASSERT (debug_sec
!= NULL
);
1928 /* PR binutils/17512: Catch out of range offsets into the debug data. */
1929 if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
> debug_sec
->size
1930 || debug_sec_data
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
< debug_sec_data
)
1931 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1932 (uintptr_t) _("<corrupt>");
1934 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1935 (uintptr_t) (debug_sec_data
1936 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1939 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (uintptr_t) "";
1942 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1945 obj_raw_syments (abfd
) = internal
;
1946 BFD_ASSERT (obj_raw_syment_count (abfd
)
1947 == (unsigned int) (internal_ptr
- internal
));
1953 coff_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
1957 count
= asect
->reloc_count
;
1958 if (count
>= LONG_MAX
/ sizeof (arelent
*)
1959 || _bfd_mul_overflow (count
, bfd_coff_relsz (abfd
), &raw
))
1961 bfd_set_error (bfd_error_file_too_big
);
1964 if (!bfd_write_p (abfd
))
1966 ufile_ptr filesize
= bfd_get_file_size (abfd
);
1967 if (filesize
!= 0 && raw
> filesize
)
1969 bfd_set_error (bfd_error_file_truncated
);
1973 return (count
+ 1) * sizeof (arelent
*);
1977 coff_make_empty_symbol (bfd
*abfd
)
1979 size_t amt
= sizeof (coff_symbol_type
);
1980 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
1982 if (new_symbol
== NULL
)
1984 new_symbol
->symbol
.section
= 0;
1985 new_symbol
->native
= NULL
;
1986 new_symbol
->lineno
= NULL
;
1987 new_symbol
->done_lineno
= false;
1988 new_symbol
->symbol
.the_bfd
= abfd
;
1990 return & new_symbol
->symbol
;
1993 /* Make a debugging symbol. */
1996 coff_bfd_make_debug_symbol (bfd
*abfd
,
1997 void * ptr ATTRIBUTE_UNUSED
,
1998 unsigned long sz ATTRIBUTE_UNUSED
)
2000 size_t amt
= sizeof (coff_symbol_type
);
2001 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
2003 if (new_symbol
== NULL
)
2005 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2006 (but shouldn't be a constant). */
2007 amt
= sizeof (combined_entry_type
) * 10;
2008 new_symbol
->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2009 if (!new_symbol
->native
)
2011 new_symbol
->native
->is_sym
= true;
2012 new_symbol
->symbol
.section
= bfd_abs_section_ptr
;
2013 new_symbol
->symbol
.flags
= BSF_DEBUGGING
;
2014 new_symbol
->lineno
= NULL
;
2015 new_symbol
->done_lineno
= false;
2016 new_symbol
->symbol
.the_bfd
= abfd
;
2018 return & new_symbol
->symbol
;
2022 coff_get_symbol_info (bfd
*abfd
, asymbol
*symbol
, symbol_info
*ret
)
2024 bfd_symbol_info (symbol
, ret
);
2026 if (coffsymbol (symbol
)->native
!= NULL
2027 && coffsymbol (symbol
)->native
->fix_value
2028 && coffsymbol (symbol
)->native
->is_sym
)
2030 = (((uintptr_t) coffsymbol (symbol
)->native
->u
.syment
.n_value
2031 - (uintptr_t) obj_raw_syments (abfd
))
2032 / sizeof (combined_entry_type
));
2035 /* Print out information about COFF symbol. */
2038 coff_print_symbol (bfd
*abfd
,
2041 bfd_print_symbol_type how
)
2043 FILE * file
= (FILE *) filep
;
2047 case bfd_print_symbol_name
:
2048 fprintf (file
, "%s", symbol
->name
);
2051 case bfd_print_symbol_more
:
2052 fprintf (file
, "coff %s %s",
2053 coffsymbol (symbol
)->native
? "n" : "g",
2054 coffsymbol (symbol
)->lineno
? "l" : " ");
2057 case bfd_print_symbol_all
:
2058 if (coffsymbol (symbol
)->native
)
2062 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2063 combined_entry_type
*root
= obj_raw_syments (abfd
);
2064 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2066 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2068 /* PR 17512: file: 079-33786-0.001:0.1. */
2069 if (combined
< obj_raw_syments (abfd
)
2070 || combined
>= obj_raw_syments (abfd
) + obj_raw_syment_count (abfd
))
2072 fprintf (file
, _("<corrupt info> %s"), symbol
->name
);
2076 BFD_ASSERT (combined
->is_sym
);
2077 if (! combined
->fix_value
)
2078 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2080 val
= (((uintptr_t) combined
->u
.syment
.n_value
- (uintptr_t) root
)
2081 / sizeof (combined_entry_type
));
2083 fprintf (file
, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2084 combined
->u
.syment
.n_scnum
,
2085 combined
->u
.syment
.n_flags
,
2086 combined
->u
.syment
.n_type
,
2087 combined
->u
.syment
.n_sclass
,
2088 combined
->u
.syment
.n_numaux
);
2089 bfd_fprintf_vma (abfd
, file
, val
);
2090 fprintf (file
, " %s", symbol
->name
);
2092 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2094 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2097 BFD_ASSERT (! auxp
->is_sym
);
2099 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2101 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2103 fprintf (file
, "\n");
2105 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2108 switch (combined
->u
.syment
.n_sclass
)
2111 fprintf (file
, "File ");
2112 /* Add additional information if this isn't the filename
2114 if (auxp
->u
.auxent
.x_file
.x_ftype
)
2115 fprintf (file
, "ftype %d fname \"%s\"",
2116 auxp
->u
.auxent
.x_file
.x_ftype
,
2117 (char *) auxp
->u
.auxent
.x_file
.x_n
.x_n
.x_offset
);
2121 fprintf (file
, "AUX scnlen 0x%lx nreloc %ld",
2122 (unsigned long) auxp
->u
.auxent
.x_sect
.x_scnlen
,
2123 auxp
->u
.auxent
.x_sect
.x_nreloc
);
2127 if (combined
->u
.syment
.n_type
== T_NULL
)
2128 /* Probably a section symbol ? */
2130 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2131 (unsigned long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2132 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2133 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2134 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2135 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2136 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2137 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2138 auxp
->u
.auxent
.x_scn
.x_checksum
,
2139 auxp
->u
.auxent
.x_scn
.x_associated
,
2140 auxp
->u
.auxent
.x_scn
.x_comdat
);
2146 if (ISFCN (combined
->u
.syment
.n_type
))
2151 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2154 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2155 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2157 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2159 (unsigned long) auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2165 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2166 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2167 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2170 fprintf (file
, " endndx %ld",
2172 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2180 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2182 while (l
->line_number
)
2184 if (l
->line_number
> 0)
2186 fprintf (file
, "\n%4d : ", l
->line_number
);
2187 bfd_fprintf_vma (abfd
, file
, l
->u
.offset
+ symbol
->section
->vma
);
2195 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
2196 fprintf (file
, " %-5s %s %s %s",
2197 symbol
->section
->name
,
2198 coffsymbol (symbol
)->native
? "n" : "g",
2199 coffsymbol (symbol
)->lineno
? "l" : " ",
2205 /* Return whether a symbol name implies a local symbol. In COFF,
2206 local symbols generally start with ``.L''. Most targets use this
2207 function for the is_local_label_name entry point, but some may
2211 _bfd_coff_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
2214 return name
[0] == '.' && name
[1] == 'L';
2217 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2218 section, calculate and return the name of the source file and the line
2219 nearest to the wanted location. */
2222 coff_find_nearest_line_with_names (bfd
*abfd
,
2226 const char **filename_ptr
,
2227 const char **functionname_ptr
,
2228 unsigned int *line_ptr
,
2229 const struct dwarf_debug_section
*debug_sections
)
2233 unsigned int line_base
;
2234 coff_data_type
*cof
= coff_data (abfd
);
2235 /* Run through the raw syments if available. */
2236 combined_entry_type
*p
;
2237 combined_entry_type
*pend
;
2239 struct coff_section_tdata
*sec_data
;
2242 /* Before looking through the symbol table, try to use a .stab
2243 section to find the information. */
2244 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2245 &found
, filename_ptr
,
2246 functionname_ptr
, line_ptr
,
2247 &coff_data(abfd
)->line_info
))
2253 /* Also try examining DWARF2 debugging information. */
2254 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
2255 filename_ptr
, functionname_ptr
,
2256 line_ptr
, NULL
, debug_sections
,
2257 &coff_data(abfd
)->dwarf2_find_line_info
))
2260 sec_data
= coff_section_data (abfd
, section
);
2262 /* If the DWARF lookup failed, but there is DWARF information available
2263 then the problem might be that the file has been rebased. This tool
2264 changes the VMAs of all the sections, but it does not update the DWARF
2265 information. So try again, using a bias against the address sought. */
2266 if (coff_data (abfd
)->dwarf2_find_line_info
!= NULL
)
2268 bfd_signed_vma bias
= 0;
2270 /* Create a cache of the result for the next call. */
2271 if (sec_data
== NULL
&& section
->owner
== abfd
)
2273 amt
= sizeof (struct coff_section_tdata
);
2274 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2275 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2278 if (sec_data
!= NULL
&& sec_data
->saved_bias
)
2279 bias
= sec_data
->bias
;
2282 bias
= _bfd_dwarf2_find_symbol_bias (symbols
,
2283 & coff_data (abfd
)->dwarf2_find_line_info
);
2287 sec_data
->saved_bias
= true;
2288 sec_data
->bias
= bias
;
2293 && _bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
,
2295 filename_ptr
, functionname_ptr
,
2296 line_ptr
, NULL
, debug_sections
,
2297 &coff_data(abfd
)->dwarf2_find_line_info
))
2302 *functionname_ptr
= 0;
2305 /* Don't try and find line numbers in a non coff file. */
2306 if (!bfd_family_coff (abfd
))
2312 /* Find the first C_FILE symbol. */
2313 p
= cof
->raw_syments
;
2317 pend
= p
+ cof
->raw_syment_count
;
2320 BFD_ASSERT (p
->is_sym
);
2321 if (p
->u
.syment
.n_sclass
== C_FILE
)
2323 p
+= 1 + p
->u
.syment
.n_numaux
;
2331 /* Look through the C_FILE symbols to find the best one. */
2332 sec_vma
= bfd_section_vma (section
);
2333 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2334 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2338 combined_entry_type
*p2
;
2340 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2342 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2344 BFD_ASSERT (p2
->is_sym
);
2345 if (p2
->u
.syment
.n_scnum
> 0
2347 == coff_section_from_bfd_index (abfd
,
2348 p2
->u
.syment
.n_scnum
)))
2350 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2359 file_addr
= (bfd_vma
) p2
->u
.syment
.n_value
;
2360 /* PR 11512: Include the section address of the function name symbol. */
2361 if (p2
->u
.syment
.n_scnum
> 0)
2362 file_addr
+= coff_section_from_bfd_index (abfd
,
2363 p2
->u
.syment
.n_scnum
)->vma
;
2364 /* We use <= MAXDIFF here so that if we get a zero length
2365 file, we actually use the next file entry. */
2367 && offset
+ sec_vma
>= file_addr
2368 && offset
+ sec_vma
- file_addr
<= maxdiff
)
2370 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2371 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2374 if (p
->u
.syment
.n_value
>= cof
->raw_syment_count
)
2377 /* Avoid endless loops on erroneous files by ensuring that
2378 we always move forward in the file. */
2379 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2382 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2383 if (!p
->is_sym
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2388 if (section
->lineno_count
== 0)
2390 *functionname_ptr
= NULL
;
2395 /* Now wander though the raw linenumbers of the section.
2396 If we have been called on this section before, and the offset
2397 we want is further down then we can prime the lookup loop. */
2398 if (sec_data
!= NULL
2400 && offset
>= sec_data
->offset
)
2403 *functionname_ptr
= sec_data
->function
;
2404 line_base
= sec_data
->line_base
;
2412 if (section
->lineno
!= NULL
)
2414 bfd_vma last_value
= 0;
2416 l
= §ion
->lineno
[i
];
2418 for (; i
< section
->lineno_count
; i
++)
2420 if (l
->line_number
== 0)
2422 /* Get the symbol this line number points at. */
2423 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2424 if (coff
->symbol
.value
> offset
)
2427 *functionname_ptr
= coff
->symbol
.name
;
2428 last_value
= coff
->symbol
.value
;
2431 combined_entry_type
*s
= coff
->native
;
2433 BFD_ASSERT (s
->is_sym
);
2434 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2436 /* In XCOFF a debugging symbol can follow the
2438 if (((size_t) ((char *) s
- (char *) obj_raw_syments (abfd
))
2439 < obj_raw_syment_count (abfd
) * sizeof (*s
))
2440 && s
->u
.syment
.n_scnum
== N_DEBUG
)
2441 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2443 /* S should now point to the .bf of the function. */
2444 if (((size_t) ((char *) s
- (char *) obj_raw_syments (abfd
))
2445 < obj_raw_syment_count (abfd
) * sizeof (*s
))
2446 && s
->u
.syment
.n_numaux
)
2448 /* The linenumber is stored in the auxent. */
2449 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2451 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2452 *line_ptr
= line_base
;
2458 if (l
->u
.offset
> offset
)
2460 *line_ptr
= l
->line_number
+ line_base
- 1;
2465 /* If we fell off the end of the loop, then assume that this
2466 symbol has no line number info. Otherwise, symbols with no
2467 line number info get reported with the line number of the
2468 last line of the last symbol which does have line number
2469 info. We use 0x100 as a slop to account for cases where the
2470 last line has executable code. */
2471 if (i
>= section
->lineno_count
2473 && offset
- last_value
> 0x100)
2475 *functionname_ptr
= NULL
;
2480 /* Cache the results for the next call. */
2481 if (sec_data
== NULL
&& section
->owner
== abfd
)
2483 amt
= sizeof (struct coff_section_tdata
);
2484 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2485 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2488 if (sec_data
!= NULL
)
2490 sec_data
->offset
= offset
;
2491 sec_data
->i
= i
- 1;
2492 sec_data
->function
= *functionname_ptr
;
2493 sec_data
->line_base
= line_base
;
2500 coff_find_nearest_line (bfd
*abfd
,
2504 const char **filename_ptr
,
2505 const char **functionname_ptr
,
2506 unsigned int *line_ptr
,
2507 unsigned int *discriminator_ptr
)
2509 if (discriminator_ptr
)
2510 *discriminator_ptr
= 0;
2511 return coff_find_nearest_line_with_names (abfd
, symbols
, section
, offset
,
2512 filename_ptr
, functionname_ptr
,
2513 line_ptr
, dwarf_debug_sections
);
2517 coff_find_inliner_info (bfd
*abfd
,
2518 const char **filename_ptr
,
2519 const char **functionname_ptr
,
2520 unsigned int *line_ptr
)
2524 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
2525 functionname_ptr
, line_ptr
,
2526 &coff_data(abfd
)->dwarf2_find_line_info
);
2531 coff_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2535 if (!bfd_link_relocatable (info
))
2536 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2538 size
= bfd_coff_filhsz (abfd
);
2540 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2544 /* Change the class of a coff symbol held by BFD. */
2547 bfd_coff_set_symbol_class (bfd
* abfd
,
2549 unsigned int symbol_class
)
2551 coff_symbol_type
* csym
;
2553 csym
= coff_symbol_from (symbol
);
2556 bfd_set_error (bfd_error_invalid_operation
);
2559 else if (csym
->native
== NULL
)
2561 /* This is an alien symbol which no native coff backend data.
2562 We cheat here by creating a fake native entry for it and
2563 then filling in the class. This code is based on that in
2564 coff_write_alien_symbol(). */
2566 combined_entry_type
* native
;
2567 size_t amt
= sizeof (* native
);
2569 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2573 native
->is_sym
= true;
2574 native
->u
.syment
.n_type
= T_NULL
;
2575 native
->u
.syment
.n_sclass
= symbol_class
;
2577 if (bfd_is_und_section (symbol
->section
))
2579 native
->u
.syment
.n_scnum
= N_UNDEF
;
2580 native
->u
.syment
.n_value
= symbol
->value
;
2582 else if (bfd_is_com_section (symbol
->section
))
2584 native
->u
.syment
.n_scnum
= N_UNDEF
;
2585 native
->u
.syment
.n_value
= symbol
->value
;
2589 native
->u
.syment
.n_scnum
=
2590 symbol
->section
->output_section
->target_index
;
2591 native
->u
.syment
.n_value
= (symbol
->value
2592 + symbol
->section
->output_offset
);
2593 if (! obj_pe (abfd
))
2594 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2596 /* Copy the any flags from the file header into the symbol.
2598 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2601 csym
->native
= native
;
2604 csym
->native
->u
.syment
.n_sclass
= symbol_class
;
2610 _bfd_coff_section_already_linked (bfd
*abfd
,
2612 struct bfd_link_info
*info
)
2615 const char *name
, *key
;
2616 struct bfd_section_already_linked
*l
;
2617 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
2618 struct coff_comdat_info
*s_comdat
;
2620 if (sec
->output_section
== bfd_abs_section_ptr
)
2624 if ((flags
& SEC_LINK_ONCE
) == 0)
2627 /* The COFF backend linker doesn't support group sections. */
2628 if ((flags
& SEC_GROUP
) != 0)
2631 name
= bfd_section_name (sec
);
2632 s_comdat
= bfd_coff_get_comdat_section (abfd
, sec
);
2634 if (s_comdat
!= NULL
)
2635 key
= s_comdat
->name
;
2638 if (startswith (name
, ".gnu.linkonce.")
2639 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
2642 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2643 .xdata$<key> and .pdata$<key> only the first of which has a
2644 comdat key. Should these all match the LTO IR key? */
2648 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
2650 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
2652 struct coff_comdat_info
*l_comdat
;
2654 l_comdat
= bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
);
2656 /* The section names must match, and both sections must be
2657 comdat and have the same comdat name, or both sections must
2658 be non-comdat. LTO IR plugin sections are an exception. They
2659 are always named .gnu.linkonce.t.<key> (<key> is some string)
2660 and match any comdat section with comdat name of <key>, and
2661 any linkonce section with the same suffix, ie.
2662 .gnu.linkonce.*.<key>. */
2663 if (((s_comdat
!= NULL
) == (l_comdat
!= NULL
)
2664 && strcmp (name
, l
->sec
->name
) == 0)
2665 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
2666 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
2668 /* The section has already been linked. See if we should
2670 return _bfd_handle_already_linked (sec
, l
, info
);
2674 /* This is the first section with this name. Record it. */
2675 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
2676 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
2680 /* Initialize COOKIE for input bfd ABFD. */
2683 init_reloc_cookie (struct coff_reloc_cookie
*cookie
,
2684 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2687 /* Sometimes the symbol table does not yet have been loaded here. */
2688 bfd_coff_slurp_symbol_table (abfd
);
2690 cookie
->abfd
= abfd
;
2691 cookie
->sym_hashes
= obj_coff_sym_hashes (abfd
);
2693 cookie
->symbols
= obj_symbols (abfd
);
2698 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2701 fini_reloc_cookie (struct coff_reloc_cookie
*cookie ATTRIBUTE_UNUSED
,
2702 bfd
*abfd ATTRIBUTE_UNUSED
)
2704 /* Nothing to do. */
2707 /* Initialize the relocation information in COOKIE for input section SEC
2708 of input bfd ABFD. */
2711 init_reloc_cookie_rels (struct coff_reloc_cookie
*cookie
,
2712 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2716 if (sec
->reloc_count
== 0)
2718 cookie
->rels
= NULL
;
2719 cookie
->relend
= NULL
;
2724 cookie
->rels
= _bfd_coff_read_internal_relocs (abfd
, sec
, false, NULL
,
2727 if (cookie
->rels
== NULL
)
2730 cookie
->rel
= cookie
->rels
;
2731 cookie
->relend
= (cookie
->rels
+ sec
->reloc_count
);
2735 /* Free the memory allocated by init_reloc_cookie_rels,
2739 fini_reloc_cookie_rels (struct coff_reloc_cookie
*cookie
,
2743 /* PR 20401. The relocs may not have been cached, so check first.
2744 If the relocs were loaded by init_reloc_cookie_rels() then this
2745 will be the case. FIXME: Would performance be improved if the
2746 relocs *were* cached ? */
2747 && coff_section_data (NULL
, sec
)
2748 && coff_section_data (NULL
, sec
)->relocs
!= cookie
->rels
)
2749 free (cookie
->rels
);
2752 /* Initialize the whole of COOKIE for input section SEC. */
2755 init_reloc_cookie_for_section (struct coff_reloc_cookie
*cookie
,
2756 struct bfd_link_info
*info
,
2759 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
2762 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
2764 fini_reloc_cookie (cookie
, sec
->owner
);
2770 /* Free the memory allocated by init_reloc_cookie_for_section,
2774 fini_reloc_cookie_for_section (struct coff_reloc_cookie
*cookie
,
2777 fini_reloc_cookie_rels (cookie
, sec
);
2778 fini_reloc_cookie (cookie
, sec
->owner
);
2782 _bfd_coff_gc_mark_hook (asection
*sec
,
2783 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2784 struct internal_reloc
*rel ATTRIBUTE_UNUSED
,
2785 struct coff_link_hash_entry
*h
,
2786 struct internal_syment
*sym
)
2790 switch (h
->root
.type
)
2792 case bfd_link_hash_defined
:
2793 case bfd_link_hash_defweak
:
2794 return h
->root
.u
.def
.section
;
2796 case bfd_link_hash_common
:
2797 return h
->root
.u
.c
.p
->section
;
2799 case bfd_link_hash_undefweak
:
2800 if (h
->symbol_class
== C_NT_WEAK
&& h
->numaux
== 1)
2802 /* PE weak externals. A weak symbol may include an auxiliary
2803 record indicating that if the weak symbol is not resolved,
2804 another external symbol is used instead. */
2805 struct coff_link_hash_entry
*h2
=
2806 h
->auxbfd
->tdata
.coff_obj_data
->sym_hashes
[
2807 h
->aux
->x_sym
.x_tagndx
.l
];
2809 if (h2
&& h2
->root
.type
!= bfd_link_hash_undefined
)
2810 return h2
->root
.u
.def
.section
;
2814 case bfd_link_hash_undefined
:
2821 return coff_section_from_bfd_index (sec
->owner
, sym
->n_scnum
);
2824 /* COOKIE->rel describes a relocation against section SEC, which is
2825 a section we've decided to keep. Return the section that contains
2826 the relocation symbol, or NULL if no section contains it. */
2829 _bfd_coff_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
2830 coff_gc_mark_hook_fn gc_mark_hook
,
2831 struct coff_reloc_cookie
*cookie
)
2833 struct coff_link_hash_entry
*h
;
2835 h
= cookie
->sym_hashes
[cookie
->rel
->r_symndx
];
2838 while (h
->root
.type
== bfd_link_hash_indirect
2839 || h
->root
.type
== bfd_link_hash_warning
)
2840 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2842 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
2845 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
2847 + obj_convert (sec
->owner
)[cookie
->rel
->r_symndx
])->native
->u
.syment
);
2850 static bool _bfd_coff_gc_mark
2851 (struct bfd_link_info
*, asection
*, coff_gc_mark_hook_fn
);
2853 /* COOKIE->rel describes a relocation against section SEC, which is
2854 a section we've decided to keep. Mark the section that contains
2855 the relocation symbol. */
2858 _bfd_coff_gc_mark_reloc (struct bfd_link_info
*info
,
2860 coff_gc_mark_hook_fn gc_mark_hook
,
2861 struct coff_reloc_cookie
*cookie
)
2865 rsec
= _bfd_coff_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
2866 if (rsec
&& !rsec
->gc_mark
)
2868 if (bfd_get_flavour (rsec
->owner
) != bfd_target_coff_flavour
)
2870 else if (!_bfd_coff_gc_mark (info
, rsec
, gc_mark_hook
))
2876 /* The mark phase of garbage collection. For a given section, mark
2877 it and any sections in this section's group, and all the sections
2878 which define symbols to which it refers. */
2881 _bfd_coff_gc_mark (struct bfd_link_info
*info
,
2883 coff_gc_mark_hook_fn gc_mark_hook
)
2889 /* Look through the section relocs. */
2890 if ((sec
->flags
& SEC_RELOC
) != 0
2891 && sec
->reloc_count
> 0)
2893 struct coff_reloc_cookie cookie
;
2895 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
2899 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
2901 if (!_bfd_coff_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
2907 fini_reloc_cookie_for_section (&cookie
, sec
);
2915 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info
*info
,
2916 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
2920 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
2925 if (bfd_get_flavour (ibfd
) != bfd_target_coff_flavour
)
2928 /* Ensure all linker created sections are kept, and see whether
2929 any other section is already marked. */
2931 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
2933 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
2935 else if (isec
->gc_mark
)
2939 /* If no section in this file will be kept, then we can
2940 toss out debug sections. */
2944 /* Keep debug and special sections like .comment when they are
2945 not part of a group, or when we have single-member groups. */
2946 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
2947 if ((isec
->flags
& SEC_DEBUGGING
) != 0
2948 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
2954 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
2957 coff_gc_sweep_symbol (struct coff_link_hash_entry
*h
,
2958 void *data ATTRIBUTE_UNUSED
)
2960 if (h
->root
.type
== bfd_link_hash_warning
)
2961 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2963 if ((h
->root
.type
== bfd_link_hash_defined
2964 || h
->root
.type
== bfd_link_hash_defweak
)
2965 && !h
->root
.u
.def
.section
->gc_mark
2966 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
2968 /* Do our best to hide the symbol. */
2969 h
->root
.u
.def
.section
= bfd_und_section_ptr
;
2970 h
->symbol_class
= C_HIDDEN
;
2976 /* The sweep phase of garbage collection. Remove all garbage sections. */
2978 typedef bool (*gc_sweep_hook_fn
)
2979 (bfd
*, struct bfd_link_info
*, asection
*, const struct internal_reloc
*);
2982 coff_gc_sweep (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_link_info
*info
)
2986 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
2990 if (bfd_get_flavour (sub
) != bfd_target_coff_flavour
)
2993 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2995 /* Keep debug and special sections. */
2996 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
2997 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
2999 else if (startswith (o
->name
, ".idata")
3000 || startswith (o
->name
, ".pdata")
3001 || startswith (o
->name
, ".xdata")
3002 || startswith (o
->name
, ".rsrc"))
3008 /* Skip sweeping sections already excluded. */
3009 if (o
->flags
& SEC_EXCLUDE
)
3012 /* Since this is early in the link process, it is simple
3013 to remove a section from the output. */
3014 o
->flags
|= SEC_EXCLUDE
;
3016 if (info
->print_gc_sections
&& o
->size
!= 0)
3017 /* xgettext: c-format */
3018 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3022 /* But we also have to update some of the relocation
3023 info we collected before. */
3025 && (o
->flags
& SEC_RELOC
) != 0
3026 && o
->reloc_count
> 0
3027 && !bfd_is_abs_section (o
->output_section
))
3029 struct internal_reloc
*internal_relocs
;
3033 = _bfd_coff_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
3035 if (internal_relocs
== NULL
)
3038 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
3040 if (coff_section_data (o
)->relocs
!= internal_relocs
)
3041 free (internal_relocs
);
3050 /* Remove the symbols that were in the swept sections from the dynamic
3052 coff_link_hash_traverse (coff_hash_table (info
), coff_gc_sweep_symbol
,
3058 /* Keep all sections containing symbols undefined on the command-line,
3059 and the section containing the entry symbol. */
3062 _bfd_coff_gc_keep (struct bfd_link_info
*info
)
3064 struct bfd_sym_chain
*sym
;
3066 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
3068 struct coff_link_hash_entry
*h
;
3070 h
= coff_link_hash_lookup (coff_hash_table (info
), sym
->name
,
3071 false, false, false);
3074 && (h
->root
.type
== bfd_link_hash_defined
3075 || h
->root
.type
== bfd_link_hash_defweak
)
3076 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
3077 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
3081 /* Do mark and sweep of unused sections. */
3084 bfd_coff_gc_sections (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_link_info
*info
)
3088 /* FIXME: Should we implement this? */
3090 const bfd_coff_backend_data
*bed
= coff_backend_info (abfd
);
3092 if (!bed
->can_gc_sections
3093 || !is_coff_hash_table (info
->hash
))
3095 _bfd_error_handler(_("warning: gc-sections option ignored"));
3100 _bfd_coff_gc_keep (info
);
3102 /* Grovel through relocs to find out who stays ... */
3103 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3107 if (bfd_get_flavour (sub
) != bfd_target_coff_flavour
)
3110 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3112 if (((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
3113 || startswith (o
->name
, ".vectors")
3114 || startswith (o
->name
, ".ctors")
3115 || startswith (o
->name
, ".dtors"))
3118 if (!_bfd_coff_gc_mark (info
, o
, _bfd_coff_gc_mark_hook
))
3124 /* Allow the backend to mark additional target specific sections. */
3125 _bfd_coff_gc_mark_extra_sections (info
, _bfd_coff_gc_mark_hook
);
3127 /* ... and mark SEC_EXCLUDE for those that go. */
3128 return coff_gc_sweep (abfd
, info
);
3131 /* Return name used to identify a comdat group. */
3134 bfd_coff_group_name (bfd
*abfd
, const asection
*sec
)
3136 struct coff_comdat_info
*ci
= bfd_coff_get_comdat_section (abfd
, sec
);
3143 _bfd_coff_close_and_cleanup (bfd
*abfd
)
3145 struct coff_tdata
*tdata
= coff_data (abfd
);
3150 Do not clear the keep_syms and keep_strings flags.
3151 These may have been set by pe_ILF_build_a_bfd() indicating
3152 that the syms and strings pointers are not to be freed. */
3153 if (bfd_get_format (abfd
) == bfd_object
3154 && bfd_family_coff (abfd
)
3155 && !_bfd_coff_free_symbols (abfd
))
3158 if (bfd_get_format (abfd
) == bfd_object
3159 || bfd_get_format (abfd
) == bfd_core
)
3161 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
3162 _bfd_stab_cleanup (abfd
, &tdata
->line_info
);
3165 return _bfd_generic_close_and_cleanup (abfd
);