readelf: Add option to display the names of sections referenced by symbols.
[binutils-gdb.git] / bfd / libcoff-in.h
blobb70710895f15751a1c2eaec6bb6b8dbeee43fbbc
1 /* BFD COFF object file private structure.
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 #ifndef _LIBCOFF_H
23 #define _LIBCOFF_H 1
25 #include "bfdlink.h"
26 #include "coff-bfd.h"
27 #include "hashtab.h"
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
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_convert(bfd) (coff_data (bfd)->conversion_table)
44 #define obj_conv_table_size(bfd) (coff_data (bfd)->conv_table_size)
45 #define obj_coff_external_syms(bfd) (coff_data (bfd)->external_syms)
46 #define obj_coff_keep_syms(bfd) (coff_data (bfd)->keep_syms)
47 #define obj_coff_strings(bfd) (coff_data (bfd)->strings)
48 #define obj_coff_strings_len(bfd) (coff_data (bfd)->strings_len)
49 #define obj_coff_keep_strings(bfd) (coff_data (bfd)->keep_strings)
50 #define obj_coff_sym_hashes(bfd) (coff_data (bfd)->sym_hashes)
51 #define obj_coff_strings_written(bfd) (coff_data (bfd)->strings_written)
52 #define obj_coff_local_toc_table(bfd) (coff_data (bfd)->local_toc_sym_map)
54 /* `Tdata' information kept for COFF files. */
56 typedef struct coff_tdata
58 struct coff_symbol_struct *symbols; /* Symtab for input bfd. */
59 unsigned int *conversion_table;
60 int conv_table_size;
61 file_ptr sym_filepos;
63 struct coff_ptr_struct *raw_syments;
64 unsigned long raw_syment_count;
66 /* These are only valid once writing has begun. */
67 unsigned long int relocbase;
69 /* These members communicate important constants about the symbol table
70 to GDB's symbol-reading code. These `constants' unfortunately vary
71 from coff implementation to implementation... */
72 unsigned local_n_btmask;
73 unsigned local_n_btshft;
74 unsigned local_n_tmask;
75 unsigned local_n_tshift;
76 unsigned local_symesz;
77 unsigned local_auxesz;
78 unsigned local_linesz;
80 /* The unswapped external symbols. May be NULL. Read by
81 _bfd_coff_get_external_symbols. */
82 void * external_syms;
84 /* The string table. May be NULL. Read by
85 _bfd_coff_read_string_table. */
86 char *strings;
87 /* The length of the strings table. For error checking. */
88 bfd_size_type strings_len;
90 /* Set if long section names are supported. A writable copy of the COFF
91 backend flag _bfd_coff_long_section_names. */
92 bool long_section_names;
94 /* If this is TRUE, the external_syms may not be freed. */
95 bool keep_syms;
96 /* If this is TRUE, the strings may not be freed. */
97 bool keep_strings;
98 /* If this is TRUE, the strings have been written out already. */
99 bool strings_written;
101 /* Is this a GO32 coff file? */
102 bool go32;
104 /* Is this a PE format coff file? */
105 bool pe;
107 /* Copy of some of the f_flags bits in the COFF filehdr structure,
108 used by ARM code. */
109 flagword flags;
111 /* Used by the COFF backend linker. */
112 struct coff_link_hash_entry **sym_hashes;
114 /* Used by the pe linker for PowerPC. */
115 int *local_toc_sym_map;
117 struct bfd_link_info *link_info;
119 /* Used by coff_find_nearest_line. */
120 void * line_info;
122 /* A place to stash dwarf2 info for this bfd. */
123 void * dwarf2_find_line_info;
125 /* The timestamp from the COFF file header. */
126 long timestamp;
128 /* A stub (extra data prepended before the COFF image) and its size.
129 Used by coff-go32-exe, it contains executable data that loads the
130 COFF object into memory. */
131 char * stub;
132 bfd_size_type stub_size;
134 /* Hash table containing sections by target index. */
135 htab_t section_by_target_index;
137 /* Hash table containing sections by index. */
138 htab_t section_by_index;
140 } coff_data_type;
142 /* Tdata for pe image files. */
143 typedef struct pe_tdata
145 coff_data_type coff;
146 struct internal_extra_pe_aouthdr pe_opthdr;
147 int dll;
148 int has_reloc_section;
149 int dont_strip_reloc;
150 char dos_message[64];
151 /* The timestamp to insert into the output file.
152 If the timestamp is -1 then the current time is used. */
153 int timestamp;
154 bool (*in_reloc_p) (bfd *, reloc_howto_type *);
155 flagword real_flags;
157 /* Build-id info. */
158 struct
160 bool (*after_write_object_contents) (bfd *);
161 const char *style;
162 asection *sec;
163 } build_id;
165 htab_t comdat_hash;
166 } pe_data_type;
168 #define pe_data(bfd) ((bfd)->tdata.pe_obj_data)
170 struct comdat_hash_entry
172 int target_index;
173 struct internal_syment isym;
174 char *symname;
175 flagword sec_flags;
176 char *comdat_name;
177 long comdat_symbol;
180 /* Tdata for XCOFF files. */
182 struct xcoff_tdata
184 /* Basic COFF information. */
185 coff_data_type coff;
187 /* TRUE if this is an XCOFF64 file. */
188 bool xcoff64;
190 /* TRUE if a large a.out header should be generated. */
191 bool full_aouthdr;
193 /* TOC value. */
194 bfd_vma toc;
196 /* Index of section holding TOC. */
197 int sntoc;
199 /* Index of section holding entry point. */
200 int snentry;
202 /* .text alignment from optional header. */
203 int text_align_power;
205 /* .data alignment from optional header. */
206 int data_align_power;
208 /* modtype from optional header. */
209 short modtype;
211 /* cputype from optional header. */
212 short cputype;
214 /* maxdata from optional header. */
215 bfd_vma maxdata;
217 /* maxstack from optional header. */
218 bfd_vma maxstack;
220 /* Used by the XCOFF backend linker. */
221 asection **csects;
222 long *debug_indices;
223 unsigned int *lineno_counts;
224 unsigned int import_file_id;
227 #define xcoff_data(abfd) ((abfd)->tdata.xcoff_obj_data)
229 /* We take the address of the first element of an asymbol to ensure that the
230 macro is only ever applied to an asymbol. */
231 #define coffsymbol(asymbol) ((coff_symbol_type *)(&((asymbol)->the_bfd)))
233 /* Tdata for sections in XCOFF files. This is used by the linker. */
235 struct xcoff_section_tdata
237 /* Used for XCOFF csects created by the linker; points to the real
238 XCOFF section which contains this csect. */
239 asection *enclosing;
240 /* The lineno_count field for the enclosing section, because we are
241 going to clobber it there. */
242 unsigned int lineno_count;
243 /* The first and last symbol indices for symbols used by this csect. */
244 unsigned long first_symndx;
245 unsigned long last_symndx;
248 /* An accessor macro the xcoff_section_tdata structure. */
249 #define xcoff_section_data(abfd, sec) \
250 ((struct xcoff_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
252 /* Tdata for sections in PE files. */
254 struct pei_section_tdata
256 /* The virtual size of the section. */
257 bfd_size_type virt_size;
258 /* The PE section flags. */
259 long pe_flags;
262 /* An accessor macro for the pei_section_tdata structure. */
263 #define pei_section_data(abfd, sec) \
264 ((struct pei_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
266 /* COFF linker hash table entries. */
268 struct coff_link_hash_entry
270 struct bfd_link_hash_entry root;
272 /* Symbol index in output file. This is initialized to -1. It is
273 set to -2 if the symbol is used by a reloc. It is set to -3 if
274 this symbol is defined in a discarded section. */
275 long indx;
277 /* Symbol type. */
278 unsigned short type;
280 /* Symbol class. */
281 unsigned char symbol_class;
283 /* Number of auxiliary entries. */
284 char numaux;
286 /* BFD to take auxiliary entries from. */
287 bfd *auxbfd;
289 /* Pointer to array of auxiliary entries, if any. */
290 union internal_auxent *aux;
292 /* Flag word; legal values follow. */
293 unsigned short coff_link_hash_flags;
294 /* Symbol is a PE section symbol. */
295 #define COFF_LINK_HASH_PE_SECTION_SYMBOL (01)
298 /* COFF linker hash table. */
300 struct coff_link_hash_table
302 struct bfd_link_hash_table root;
303 /* A pointer to information used to link stabs in sections. */
304 struct stab_info stab_info;
305 /* Hash table that maps undecorated names to their respective
306 * decorated coff_link_hash_entry as found in fixup_stdcalls */
307 struct bfd_hash_table decoration_hash;
310 struct coff_reloc_cookie
312 struct internal_reloc * rels;
313 struct internal_reloc * rel;
314 struct internal_reloc * relend;
315 struct coff_symbol_struct * symbols; /* Symtab for input bfd. */
316 bfd * abfd;
317 struct coff_link_hash_entry ** sym_hashes;
320 /* Look up an entry in a COFF linker hash table. */
322 #define coff_link_hash_lookup(table, string, create, copy, follow) \
323 ((struct coff_link_hash_entry *) \
324 bfd_link_hash_lookup (&(table)->root, (string), (create), \
325 (copy), (follow)))
327 /* Traverse a COFF linker hash table. */
329 #define coff_link_hash_traverse(table, func, info) \
330 (bfd_link_hash_traverse \
331 (&(table)->root, \
332 (bool (*) (struct bfd_link_hash_entry *, void *)) (func), \
333 (info)))
335 /* Get the COFF linker hash table from a link_info structure. */
337 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
339 struct decoration_hash_entry
341 struct bfd_hash_entry root;
342 struct bfd_link_hash_entry *decorated_link;
345 /* Functions in coffgen.c. */
346 extern void coff_object_cleanup
347 (bfd *);
348 extern bfd_cleanup coff_real_object_p
349 (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
350 extern bfd_cleanup coff_object_p
351 (bfd *);
352 extern struct bfd_section *coff_section_from_bfd_index
353 (bfd *, int);
354 extern long coff_get_symtab_upper_bound
355 (bfd *);
356 extern long coff_canonicalize_symtab
357 (bfd *, asymbol **);
358 extern int coff_count_linenumbers
359 (bfd *);
360 extern bool coff_renumber_symbols
361 (bfd *, int *);
362 extern void coff_mangle_symbols
363 (bfd *);
364 extern bool coff_write_symbols
365 (bfd *);
366 extern bool coff_write_alien_symbol
367 (bfd *, asymbol *, struct internal_syment *, bfd_vma *,
368 struct bfd_strtab_hash *, bool, asection **, bfd_size_type *);
369 extern bool coff_write_linenumbers
370 (bfd *);
371 extern alent *coff_get_lineno
372 (bfd *, asymbol *);
373 extern asymbol *coff_section_symbol
374 (bfd *, char *);
375 extern bool _bfd_coff_get_external_symbols
376 (bfd *);
377 extern const char *_bfd_coff_read_string_table
378 (bfd *);
379 extern bool _bfd_coff_free_symbols
380 (bfd *);
381 extern struct coff_ptr_struct *coff_get_normalized_symtab
382 (bfd *);
383 extern long coff_get_reloc_upper_bound
384 (bfd *, sec_ptr);
385 extern asymbol *coff_make_empty_symbol
386 (bfd *);
387 extern void coff_print_symbol
388 (bfd *, void * filep, asymbol *, bfd_print_symbol_type);
389 extern void coff_get_symbol_info
390 (bfd *, asymbol *, symbol_info *ret);
391 #define coff_get_symbol_version_string \
392 _bfd_nosymbols_get_symbol_version_string
393 extern bool _bfd_coff_is_local_label_name
394 (bfd *, const char *);
395 extern asymbol *coff_bfd_make_debug_symbol
396 (bfd *);
397 extern bool coff_find_nearest_line
398 (bfd *, asymbol **, asection *, bfd_vma,
399 const char **, const char **, unsigned int *, unsigned int *);
400 #define coff_find_nearest_line_with_alt \
401 _bfd_nosymbols_find_nearest_line_with_alt
402 #define coff_find_line _bfd_nosymbols_find_line
403 struct dwarf_debug_section;
404 extern bool coff_find_nearest_line_with_names
405 (bfd *, asymbol **, asection *, bfd_vma, const char **, const char **,
406 unsigned int *, const struct dwarf_debug_section *);
407 extern bool coff_find_inliner_info
408 (bfd *, const char **, const char **, unsigned int *);
409 extern int coff_sizeof_headers
410 (bfd *, struct bfd_link_info *);
411 extern bool bfd_coff_reloc16_relax_section
412 (bfd *, asection *, struct bfd_link_info *, bool *);
413 extern bfd_byte *bfd_coff_reloc16_get_relocated_section_contents
414 (bfd *, struct bfd_link_info *, struct bfd_link_order *,
415 bfd_byte *, bool, asymbol **);
416 extern bfd_vma bfd_coff_reloc16_get_value
417 (arelent *, struct bfd_link_info *, asection *);
418 extern void bfd_perform_slip
419 (bfd *, unsigned int, asection *, bfd_vma);
420 extern bool _bfd_coff_free_cached_info
421 (bfd *);
423 /* Functions and types in cofflink.c. */
425 #define STRING_SIZE_SIZE 4
427 /* We use a hash table to merge identical enum, struct, and union
428 definitions in the linker. */
430 /* Information we keep for a single element (an enum value, a
431 structure or union field) in the debug merge hash table. */
433 struct coff_debug_merge_element
435 /* Next element. */
436 struct coff_debug_merge_element *next;
438 /* Name. */
439 const char *name;
441 /* Type. */
442 unsigned int type;
444 /* Symbol index for complex type. */
445 long tagndx;
448 /* A linked list of debug merge entries for a given name. */
450 struct coff_debug_merge_type
452 /* Next type with the same name. */
453 struct coff_debug_merge_type *next;
455 /* Class of type. */
456 int type_class;
458 /* Symbol index where this type is defined. */
459 long indx;
461 /* List of elements. */
462 struct coff_debug_merge_element *elements;
465 /* Information we store in the debug merge hash table. */
467 struct coff_debug_merge_hash_entry
469 struct bfd_hash_entry root;
471 /* A list of types with this name. */
472 struct coff_debug_merge_type *types;
475 /* The debug merge hash table. */
477 struct coff_debug_merge_hash_table
479 struct bfd_hash_table root;
482 /* Initialize a COFF debug merge hash table. */
484 #define coff_debug_merge_hash_table_init(table) \
485 (bfd_hash_table_init (&(table)->root, _bfd_coff_debug_merge_hash_newfunc, \
486 sizeof (struct coff_debug_merge_hash_entry)))
488 /* Free a COFF debug merge hash table. */
490 #define coff_debug_merge_hash_table_free(table) \
491 (bfd_hash_table_free (&(table)->root))
493 /* Look up an entry in a COFF debug merge hash table. */
495 #define coff_debug_merge_hash_lookup(table, string, create, copy) \
496 ((struct coff_debug_merge_hash_entry *) \
497 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
499 /* Information we keep for each section in the output file when doing
500 a relocatable link. */
502 struct coff_link_section_info
504 /* The relocs to be output. */
505 struct internal_reloc *relocs;
506 /* For each reloc against a global symbol whose index was not known
507 when the reloc was handled, the global hash table entry. */
508 struct coff_link_hash_entry **rel_hashes;
511 /* Information that we pass around while doing the final link step. */
513 struct coff_final_link_info
515 /* General link information. */
516 struct bfd_link_info *info;
517 /* Output BFD. */
518 bfd *output_bfd;
519 /* Used to indicate failure in traversal routine. */
520 bool failed;
521 /* If doing "task linking" set only during the time when we want the
522 global symbol writer to convert the storage class of defined global
523 symbols from global to static. */
524 bool global_to_static;
525 /* Hash table for long symbol names. */
526 struct bfd_strtab_hash *strtab;
527 /* When doing a relocatable link, an array of information kept for
528 each output section, indexed by the target_index field. */
529 struct coff_link_section_info *section_info;
530 /* Symbol index of last C_FILE symbol (-1 if none). */
531 long last_file_index;
532 /* Contents of last C_FILE symbol. */
533 struct internal_syment last_file;
534 /* Symbol index of first aux entry of last .bf symbol with an empty
535 endndx field (-1 if none). */
536 long last_bf_index;
537 /* Contents of last_bf_index aux entry. */
538 union internal_auxent last_bf;
539 /* Hash table used to merge debug information. */
540 struct coff_debug_merge_hash_table debug_merge;
541 /* Buffer large enough to hold swapped symbols of any input file. */
542 struct internal_syment *internal_syms;
543 /* Buffer large enough to hold sections of symbols of any input file. */
544 asection **sec_ptrs;
545 /* Buffer large enough to hold output indices of symbols of any
546 input file. */
547 long *sym_indices;
548 /* Buffer large enough to hold output symbols for any input file. */
549 bfd_byte *outsyms;
550 /* Buffer large enough to hold external line numbers for any input
551 section. */
552 bfd_byte *linenos;
553 /* Buffer large enough to hold any input section. */
554 bfd_byte *contents;
555 /* Buffer large enough to hold external relocs of any input section. */
556 bfd_byte *external_relocs;
557 /* Buffer large enough to hold swapped relocs of any input section. */
558 struct internal_reloc *internal_relocs;
561 /* Most COFF variants have no way to record the alignment of a
562 section. This struct is used to set a specific alignment based on
563 the name of the section. */
565 struct coff_section_alignment_entry
567 /* The section name. */
568 const char *name;
570 /* This is either (unsigned int) -1, indicating that the section
571 name must match exactly, or it is the number of letters which
572 must match at the start of the name. */
573 unsigned int comparison_length;
575 /* These macros may be used to fill in the first two fields in a
576 structure initialization. */
577 #define COFF_SECTION_NAME_EXACT_MATCH(name) (name), ((unsigned int) -1)
578 #define COFF_SECTION_NAME_PARTIAL_MATCH(name) (name), (sizeof (name) - 1)
580 /* Only use this entry if the default section alignment for this
581 target is at least that much (as a power of two). If this field
582 is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
583 unsigned int default_alignment_min;
585 /* Only use this entry if the default section alignment for this
586 target is no greater than this (as a power of two). If this
587 field is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
588 unsigned int default_alignment_max;
590 #define COFF_ALIGNMENT_FIELD_EMPTY ((unsigned int) -1)
592 /* The desired alignment for this section (as a power of two). */
593 unsigned int alignment_power;
596 extern struct bfd_hash_entry *_decoration_hash_newfunc
597 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
598 extern struct bfd_hash_entry *_bfd_coff_link_hash_newfunc
599 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
600 extern bool _bfd_coff_link_hash_table_init
601 (struct coff_link_hash_table *, bfd *,
602 struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
603 struct bfd_hash_table *,
604 const char *),
605 unsigned int);
606 extern struct bfd_link_hash_table *_bfd_coff_link_hash_table_create
607 (bfd *);
608 extern const char *_bfd_coff_internal_syment_name
609 (bfd *, const struct internal_syment *, char *);
610 extern bool _bfd_coff_section_already_linked
611 (bfd *, asection *, struct bfd_link_info *);
612 extern bool _bfd_coff_link_add_symbols
613 (bfd *, struct bfd_link_info *);
614 extern bool _bfd_coff_final_link
615 (bfd *, struct bfd_link_info *);
616 extern struct internal_reloc *_bfd_coff_read_internal_relocs
617 (bfd *, asection *, bool, bfd_byte *, bool,
618 struct internal_reloc *);
619 extern bool _bfd_coff_generic_relocate_section
620 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
621 struct internal_reloc *, struct internal_syment *, asection **);
622 extern struct bfd_hash_entry *_bfd_coff_debug_merge_hash_newfunc
623 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
624 extern bool _bfd_coff_write_global_sym
625 (struct bfd_hash_entry *, void *);
626 extern bool _bfd_coff_write_task_globals
627 (struct coff_link_hash_entry *, void *);
628 extern bool _bfd_coff_link_input_bfd
629 (struct coff_final_link_info *, bfd *);
630 extern bool _bfd_coff_reloc_link_order
631 (bfd *, struct coff_final_link_info *, asection *,
632 struct bfd_link_order *);
633 extern bool bfd_coff_gc_sections
634 (bfd *, struct bfd_link_info *);
635 extern const char *bfd_coff_group_name
636 (bfd *, const asection *);
638 #define coff_get_section_contents_in_window \
639 _bfd_generic_get_section_contents_in_window
641 /* Functions in xcofflink.c. */
643 extern long _bfd_xcoff_get_dynamic_symtab_upper_bound
644 (bfd *);
645 extern long _bfd_xcoff_canonicalize_dynamic_symtab
646 (bfd *, asymbol **);
647 extern long _bfd_xcoff_get_dynamic_reloc_upper_bound
648 (bfd *);
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
652 (bfd *);
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 **);