1 /* BFD COFF object file private structure.
2 Copyright (C) 1990-2025 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. */
33 /* Object file tdata; access macros. */
35 #define coff_data(bfd) ((bfd)->tdata.coff_obj_data)
36 #define obj_pe(bfd) (coff_data (bfd)->pe)
37 #define obj_go32(bfd) (coff_data (bfd)->go32)
38 #define obj_symbols(bfd) (coff_data (bfd)->symbols)
39 #define obj_sym_filepos(bfd) (coff_data (bfd)->sym_filepos)
40 #define obj_relocbase(bfd) (coff_data (bfd)->relocbase)
41 #define obj_raw_syments(bfd) (coff_data (bfd)->raw_syments)
42 #define obj_raw_syment_count(bfd) (coff_data (bfd)->raw_syment_count)
43 #define obj_coff_keep_raw_syms(bfd) (coff_data (bfd)->keep_raw_syms)
44 #define obj_convert(bfd) (coff_data (bfd)->conversion_table)
45 #define obj_conv_table_size(bfd) (coff_data (bfd)->conv_table_size)
46 #define obj_coff_external_syms(bfd) (coff_data (bfd)->external_syms)
47 #define obj_coff_keep_syms(bfd) (coff_data (bfd)->keep_syms)
48 #define obj_coff_strings(bfd) (coff_data (bfd)->strings)
49 #define obj_coff_strings_len(bfd) (coff_data (bfd)->strings_len)
50 #define obj_coff_keep_strings(bfd) (coff_data (bfd)->keep_strings)
51 #define obj_coff_sym_hashes(bfd) (coff_data (bfd)->sym_hashes)
52 #define obj_coff_strings_written(bfd) (coff_data (bfd)->strings_written)
53 #define obj_coff_local_toc_table(bfd) (coff_data (bfd)->local_toc_sym_map)
55 /* `Tdata' information kept for COFF files. */
57 typedef struct coff_tdata
59 struct coff_symbol_struct
*symbols
; /* Symtab for input bfd. */
60 unsigned int *conversion_table
;
64 struct coff_ptr_struct
*raw_syments
;
65 unsigned long raw_syment_count
;
67 /* These are only valid once writing has begun. */
68 unsigned long int relocbase
;
70 /* These members communicate important constants about the symbol table
71 to GDB's symbol-reading code. These `constants' unfortunately vary
72 from coff implementation to implementation... */
73 unsigned local_n_btmask
;
74 unsigned local_n_btshft
;
75 unsigned local_n_tmask
;
76 unsigned local_n_tshift
;
77 unsigned local_symesz
;
78 unsigned local_auxesz
;
79 unsigned local_linesz
;
81 /* The unswapped external symbols. May be NULL. Read by
82 _bfd_coff_get_external_symbols. */
85 /* The string table. May be NULL. Read by
86 _bfd_coff_read_string_table. */
88 /* The length of the strings table. For error checking. */
89 bfd_size_type strings_len
;
91 /* Set if long section names are supported. A writable copy of the COFF
92 backend flag _bfd_coff_long_section_names. */
93 bool long_section_names
;
95 /* If this is TRUE, raw_syments may not be released. */
97 /* If this is TRUE, the external_syms may not be freed. */
99 /* If this is TRUE, the strings may not be freed. */
101 /* If this is TRUE, the strings have been written out already. */
102 bool strings_written
;
104 /* Is this a GO32 coff file? */
107 /* Is this a PE format coff file? */
110 /* Copy of some of the f_flags bits in the COFF filehdr structure,
114 /* Used by the COFF backend linker. */
115 struct coff_link_hash_entry
**sym_hashes
;
117 /* Used by the pe linker for PowerPC. */
118 int *local_toc_sym_map
;
120 struct bfd_link_info
*link_info
;
122 /* Used by coff_find_nearest_line. */
125 /* A place to stash dwarf2 info for this bfd. */
126 void * dwarf2_find_line_info
;
128 /* The timestamp from the COFF file header. */
131 /* A stub (extra data prepended before the COFF image) and its size.
132 Used by coff-go32-exe, it contains executable data that loads the
133 COFF object into memory. */
135 bfd_size_type stub_size
;
137 /* Hash table containing sections by target index. */
138 htab_t section_by_target_index
;
140 /* Hash table containing sections by index. */
141 htab_t section_by_index
;
145 /* Tdata for pe image files. */
146 typedef struct pe_tdata
149 struct internal_extra_pe_aouthdr pe_opthdr
;
151 int has_reloc_section
;
152 int dont_strip_reloc
;
153 char dos_message
[64];
154 /* The timestamp to insert into the output file.
155 If the timestamp is -1 then the current time is used. */
157 bool (*in_reloc_p
) (bfd
*, reloc_howto_type
*);
163 bool (*after_write_object_contents
) (bfd
*);
171 #define pe_data(bfd) ((bfd)->tdata.pe_obj_data)
173 struct comdat_hash_entry
176 struct internal_syment isym
;
183 /* Tdata for XCOFF files. */
187 /* Basic COFF information. */
190 /* TRUE if this is an XCOFF64 file. */
193 /* TRUE if a large a.out header should be generated. */
199 /* Index of section holding TOC. */
202 /* Index of section holding entry point. */
205 /* .text alignment from optional header. */
206 int text_align_power
;
208 /* .data alignment from optional header. */
209 int data_align_power
;
211 /* modtype from optional header. */
214 /* cputype from optional header. */
217 /* maxdata from optional header. */
220 /* maxstack from optional header. */
223 /* Used by the XCOFF backend linker. */
226 unsigned int *lineno_counts
;
227 unsigned int import_file_id
;
230 #define xcoff_data(abfd) ((abfd)->tdata.xcoff_obj_data)
232 /* We take the address of the first element of an asymbol to ensure that the
233 macro is only ever applied to an asymbol. */
234 #define coffsymbol(asymbol) ((coff_symbol_type *)(&((asymbol)->the_bfd)))
236 /* Tdata for sections in XCOFF files. This is used by the linker. */
238 struct xcoff_section_tdata
240 /* Used for XCOFF csects created by the linker; points to the real
241 XCOFF section which contains this csect. */
243 /* The lineno_count field for the enclosing section, because we are
244 going to clobber it there. */
245 unsigned int lineno_count
;
246 /* The first and last symbol indices for symbols used by this csect. */
247 unsigned long first_symndx
;
248 unsigned long last_symndx
;
251 /* An accessor macro the xcoff_section_tdata structure. */
252 #define xcoff_section_data(abfd, sec) \
253 ((struct xcoff_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
255 /* Tdata for sections in PE files. */
257 struct pei_section_tdata
259 /* The virtual size of the section. */
260 bfd_size_type virt_size
;
261 /* The PE section flags. */
265 /* An accessor macro for the pei_section_tdata structure. */
266 #define pei_section_data(abfd, sec) \
267 ((struct pei_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
269 /* COFF linker hash table entries. */
271 struct coff_link_hash_entry
273 struct bfd_link_hash_entry root
;
275 /* Symbol index in output file. This is initialized to -1. It is
276 set to -2 if the symbol is used by a reloc. It is set to -3 if
277 this symbol is defined in a discarded section. */
284 unsigned char symbol_class
;
286 /* Number of auxiliary entries. */
289 /* BFD to take auxiliary entries from. */
292 /* Pointer to array of auxiliary entries, if any. */
293 union internal_auxent
*aux
;
295 /* Flag word; legal values follow. */
296 unsigned short coff_link_hash_flags
;
297 /* Symbol is a PE section symbol. */
298 #define COFF_LINK_HASH_PE_SECTION_SYMBOL (01)
301 /* COFF linker hash table. */
303 struct coff_link_hash_table
305 struct bfd_link_hash_table root
;
306 /* A pointer to information used to link stabs in sections. */
307 struct stab_info stab_info
;
308 /* Hash table that maps undecorated names to their respective
309 * decorated coff_link_hash_entry as found in fixup_stdcalls */
310 struct bfd_hash_table decoration_hash
;
313 struct coff_reloc_cookie
315 struct internal_reloc
* rels
;
316 struct internal_reloc
* rel
;
317 struct internal_reloc
* relend
;
318 struct coff_symbol_struct
* symbols
; /* Symtab for input bfd. */
320 struct coff_link_hash_entry
** sym_hashes
;
323 /* Look up an entry in a COFF linker hash table. */
325 #define coff_link_hash_lookup(table, string, create, copy, follow) \
326 ((struct coff_link_hash_entry *) \
327 bfd_link_hash_lookup (&(table)->root, (string), (create), \
330 /* Traverse a COFF linker hash table. */
332 #define coff_link_hash_traverse(table, func, info) \
333 (bfd_link_hash_traverse \
335 (bool (*) (struct bfd_link_hash_entry *, void *)) (func), \
338 /* Get the COFF linker hash table from a link_info structure. */
340 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
342 struct decoration_hash_entry
344 struct bfd_hash_entry root
;
345 struct bfd_link_hash_entry
*decorated_link
;
348 /* Functions in coffgen.c. */
349 extern void coff_object_cleanup
351 extern bfd_cleanup coff_real_object_p
352 (bfd
*, unsigned, struct internal_filehdr
*, struct internal_aouthdr
*);
353 extern bfd_cleanup coff_object_p
355 extern struct bfd_section
*coff_section_from_bfd_index
357 extern long coff_get_symtab_upper_bound
359 extern long coff_canonicalize_symtab
361 extern int coff_count_linenumbers
363 extern bool coff_renumber_symbols
365 extern void coff_mangle_symbols
367 extern bool coff_write_symbols
369 extern bool coff_write_alien_symbol
370 (bfd
*, asymbol
*, struct internal_syment
*, bfd_vma
*,
371 struct bfd_strtab_hash
*, bool, asection
**, bfd_size_type
*);
372 extern bool coff_write_linenumbers
374 extern alent
*coff_get_lineno
376 extern asymbol
*coff_section_symbol
378 extern bool _bfd_coff_get_external_symbols
380 extern const char *_bfd_coff_read_string_table
382 extern bool _bfd_coff_free_symbols
384 extern struct coff_ptr_struct
*coff_get_normalized_symtab
386 extern long coff_get_reloc_upper_bound
388 extern asymbol
*coff_make_empty_symbol
390 extern void coff_print_symbol
391 (bfd
*, void * filep
, asymbol
*, bfd_print_symbol_type
);
392 extern void coff_get_symbol_info
393 (bfd
*, asymbol
*, symbol_info
*ret
);
394 #define coff_get_symbol_version_string \
395 _bfd_nosymbols_get_symbol_version_string
396 extern bool _bfd_coff_is_local_label_name
397 (bfd
*, const char *);
398 extern asymbol
*coff_bfd_make_debug_symbol
400 extern bool coff_find_nearest_line
401 (bfd
*, asymbol
**, asection
*, bfd_vma
,
402 const char **, const char **, unsigned int *, unsigned int *);
403 #define coff_find_nearest_line_with_alt \
404 _bfd_nosymbols_find_nearest_line_with_alt
405 #define coff_find_line _bfd_nosymbols_find_line
406 struct dwarf_debug_section
;
407 extern bool coff_find_nearest_line_with_names
408 (bfd
*, asymbol
**, asection
*, bfd_vma
, const char **, const char **,
409 unsigned int *, const struct dwarf_debug_section
*);
410 extern bool coff_find_inliner_info
411 (bfd
*, const char **, const char **, unsigned int *);
412 extern int coff_sizeof_headers
413 (bfd
*, struct bfd_link_info
*);
414 extern bool bfd_coff_reloc16_relax_section
415 (bfd
*, asection
*, struct bfd_link_info
*, bool *);
416 extern bfd_byte
*bfd_coff_reloc16_get_relocated_section_contents
417 (bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
418 bfd_byte
*, bool, asymbol
**);
419 extern bfd_vma bfd_coff_reloc16_get_value
420 (arelent
*, struct bfd_link_info
*, asection
*);
421 extern void bfd_perform_slip
422 (bfd
*, unsigned int, asection
*, bfd_vma
);
423 extern bool _bfd_coff_free_cached_info
426 /* Functions and types in cofflink.c. */
428 #define STRING_SIZE_SIZE 4
430 /* We use a hash table to merge identical enum, struct, and union
431 definitions in the linker. */
433 /* Information we keep for a single element (an enum value, a
434 structure or union field) in the debug merge hash table. */
436 struct coff_debug_merge_element
439 struct coff_debug_merge_element
*next
;
447 /* Symbol index for complex type. */
451 /* A linked list of debug merge entries for a given name. */
453 struct coff_debug_merge_type
455 /* Next type with the same name. */
456 struct coff_debug_merge_type
*next
;
461 /* Symbol index where this type is defined. */
464 /* List of elements. */
465 struct coff_debug_merge_element
*elements
;
468 /* Information we store in the debug merge hash table. */
470 struct coff_debug_merge_hash_entry
472 struct bfd_hash_entry root
;
474 /* A list of types with this name. */
475 struct coff_debug_merge_type
*types
;
478 /* The debug merge hash table. */
480 struct coff_debug_merge_hash_table
482 struct bfd_hash_table root
;
485 /* Initialize a COFF debug merge hash table. */
487 #define coff_debug_merge_hash_table_init(table) \
488 (bfd_hash_table_init (&(table)->root, _bfd_coff_debug_merge_hash_newfunc, \
489 sizeof (struct coff_debug_merge_hash_entry)))
491 /* Free a COFF debug merge hash table. */
493 #define coff_debug_merge_hash_table_free(table) \
494 (bfd_hash_table_free (&(table)->root))
496 /* Look up an entry in a COFF debug merge hash table. */
498 #define coff_debug_merge_hash_lookup(table, string, create, copy) \
499 ((struct coff_debug_merge_hash_entry *) \
500 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
502 /* Information we keep for each section in the output file when doing
503 a relocatable link. */
505 struct coff_link_section_info
507 /* The relocs to be output. */
508 struct internal_reloc
*relocs
;
509 /* For each reloc against a global symbol whose index was not known
510 when the reloc was handled, the global hash table entry. */
511 struct coff_link_hash_entry
**rel_hashes
;
514 /* Information that we pass around while doing the final link step. */
516 struct coff_final_link_info
518 /* General link information. */
519 struct bfd_link_info
*info
;
522 /* Used to indicate failure in traversal routine. */
524 /* If doing "task linking" set only during the time when we want the
525 global symbol writer to convert the storage class of defined global
526 symbols from global to static. */
527 bool global_to_static
;
528 /* Hash table for long symbol names. */
529 struct bfd_strtab_hash
*strtab
;
530 /* When doing a relocatable link, an array of information kept for
531 each output section, indexed by the target_index field. */
532 struct coff_link_section_info
*section_info
;
533 /* Symbol index of last C_FILE symbol (-1 if none). */
534 long last_file_index
;
535 /* Contents of last C_FILE symbol. */
536 struct internal_syment last_file
;
537 /* Symbol index of first aux entry of last .bf symbol with an empty
538 endndx field (-1 if none). */
540 /* Contents of last_bf_index aux entry. */
541 union internal_auxent last_bf
;
542 /* Hash table used to merge debug information. */
543 struct coff_debug_merge_hash_table debug_merge
;
544 /* Buffer large enough to hold swapped symbols of any input file. */
545 struct internal_syment
*internal_syms
;
546 /* Buffer large enough to hold sections of symbols of any input file. */
548 /* Buffer large enough to hold output indices of symbols of any
551 /* Buffer large enough to hold output symbols for any input file. */
553 /* Buffer large enough to hold external line numbers for any input
556 /* Buffer large enough to hold any input section. */
558 /* Buffer large enough to hold external relocs of any input section. */
559 bfd_byte
*external_relocs
;
560 /* Buffer large enough to hold swapped relocs of any input section. */
561 struct internal_reloc
*internal_relocs
;
564 /* Most COFF variants have no way to record the alignment of a
565 section. This struct is used to set a specific alignment based on
566 the name of the section. */
568 struct coff_section_alignment_entry
570 /* The section name. */
573 /* This is either (unsigned int) -1, indicating that the section
574 name must match exactly, or it is the number of letters which
575 must match at the start of the name. */
576 unsigned int comparison_length
;
578 /* These macros may be used to fill in the first two fields in a
579 structure initialization. */
580 #define COFF_SECTION_NAME_EXACT_MATCH(name) (name), ((unsigned int) -1)
581 #define COFF_SECTION_NAME_PARTIAL_MATCH(name) (name), (sizeof (name) - 1)
583 /* Only use this entry if the default section alignment for this
584 target is at least that much (as a power of two). If this field
585 is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
586 unsigned int default_alignment_min
;
588 /* Only use this entry if the default section alignment for this
589 target is no greater than this (as a power of two). If this
590 field is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
591 unsigned int default_alignment_max
;
593 #define COFF_ALIGNMENT_FIELD_EMPTY ((unsigned int) -1)
595 /* The desired alignment for this section (as a power of two). */
596 unsigned int alignment_power
;
599 extern struct bfd_hash_entry
*_decoration_hash_newfunc
600 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
601 extern struct bfd_hash_entry
*_bfd_coff_link_hash_newfunc
602 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
603 extern bool _bfd_coff_link_hash_table_init
604 (struct coff_link_hash_table
*, bfd
*,
605 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
606 struct bfd_hash_table
*,
609 extern struct bfd_link_hash_table
*_bfd_coff_link_hash_table_create
611 extern const char *_bfd_coff_internal_syment_name
612 (bfd
*, const struct internal_syment
*, char *);
613 extern bool _bfd_coff_section_already_linked
614 (bfd
*, asection
*, struct bfd_link_info
*);
615 extern bool _bfd_coff_link_add_symbols
616 (bfd
*, struct bfd_link_info
*);
617 extern bool _bfd_coff_final_link
618 (bfd
*, struct bfd_link_info
*);
619 extern struct internal_reloc
*_bfd_coff_read_internal_relocs
620 (bfd
*, asection
*, bool, bfd_byte
*, bool,
621 struct internal_reloc
*);
622 extern bool _bfd_coff_generic_relocate_section
623 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
624 struct internal_reloc
*, struct internal_syment
*, asection
**);
625 extern struct bfd_hash_entry
*_bfd_coff_debug_merge_hash_newfunc
626 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
627 extern bool _bfd_coff_write_global_sym
628 (struct bfd_hash_entry
*, void *);
629 extern bool _bfd_coff_write_task_globals
630 (struct coff_link_hash_entry
*, void *);
631 extern bool _bfd_coff_link_input_bfd
632 (struct coff_final_link_info
*, bfd
*);
633 extern bool _bfd_coff_reloc_link_order
634 (bfd
*, struct coff_final_link_info
*, asection
*,
635 struct bfd_link_order
*);
636 extern bool bfd_coff_gc_sections
637 (bfd
*, struct bfd_link_info
*);
638 extern const char *bfd_coff_group_name
639 (bfd
*, const asection
*);
641 /* Functions in xcofflink.c. */
643 extern long _bfd_xcoff_get_dynamic_symtab_upper_bound
645 extern long _bfd_xcoff_canonicalize_dynamic_symtab
647 extern long _bfd_xcoff_get_dynamic_reloc_upper_bound
649 extern long _bfd_xcoff_canonicalize_dynamic_reloc
650 (bfd
*, arelent
**, asymbol
**);
651 extern struct bfd_link_hash_table
*_bfd_xcoff_bfd_link_hash_table_create
653 extern bool _bfd_xcoff_bfd_link_add_symbols
654 (bfd
*, struct bfd_link_info
*);
655 extern bool _bfd_xcoff_bfd_final_link
656 (bfd
*, struct bfd_link_info
*);
657 extern bool _bfd_xcoff_define_common_symbol
658 (bfd
*, struct bfd_link_info
*, struct bfd_link_hash_entry
*);
659 extern bool _bfd_ppc_xcoff_relocate_section
660 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
661 struct internal_reloc
*, struct internal_syment
*, asection
**);