1 /* BFD COFF object file private structure.
2 Copyright (C) 1990-2023 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 #ifndef _LIBCOFF_H
23 #define _LIBCOFF_H 1
24
25 #include "bfdlink.h"
26 #include "coff-bfd.h"
27 #include "hashtab.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /* Object file tdata; access macros. */
34
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)
53
54 /* `Tdata' information kept for COFF files. */
55
56 typedef struct coff_tdata
57 {
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;
62
63 struct coff_ptr_struct *raw_syments;
64 unsigned long raw_syment_count;
65
66 /* These are only valid once writing has begun. */
67 unsigned long int relocbase;
68
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;
79
80 /* The unswapped external symbols. May be NULL. Read by
81 _bfd_coff_get_external_symbols. */
82 void * external_syms;
83
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;
89
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;
93
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;
100
101 /* Is this a GO32 coff file? */
102 bool go32;
103
104 /* Is this a PE format coff file? */
105 bool pe;
106
107 /* Copy of some of the f_flags bits in the COFF filehdr structure,
108 used by ARM code. */
109 flagword flags;
110
111 /* Used by the COFF backend linker. */
112 struct coff_link_hash_entry **sym_hashes;
113
114 /* Used by the pe linker for PowerPC. */
115 int *local_toc_sym_map;
116
117 struct bfd_link_info *link_info;
118
119 /* Used by coff_find_nearest_line. */
120 void * line_info;
121
122 /* A place to stash dwarf2 info for this bfd. */
123 void * dwarf2_find_line_info;
124
125 /* The timestamp from the COFF file header. */
126 long timestamp;
127
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;
133
134 /* Hash table containing sections by target index. */
135 htab_t section_by_target_index;
136
137 /* Hash table containing sections by index. */
138 htab_t section_by_index;
139
140 } coff_data_type;
141
142 /* Tdata for pe image files. */
143 typedef struct pe_tdata
144 {
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 int dos_message[16];
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;
156
157 /* Build-id info. */
158 struct
159 {
160 bool (*after_write_object_contents) (bfd *);
161 const char *style;
162 asection *sec;
163 } build_id;
164 } pe_data_type;
165
166 #define pe_data(bfd) ((bfd)->tdata.pe_obj_data)
167
168 /* Tdata for XCOFF files. */
169
170 struct xcoff_tdata
171 {
172 /* Basic COFF information. */
173 coff_data_type coff;
174
175 /* TRUE if this is an XCOFF64 file. */
176 bool xcoff64;
177
178 /* TRUE if a large a.out header should be generated. */
179 bool full_aouthdr;
180
181 /* TOC value. */
182 bfd_vma toc;
183
184 /* Index of section holding TOC. */
185 int sntoc;
186
187 /* Index of section holding entry point. */
188 int snentry;
189
190 /* .text alignment from optional header. */
191 int text_align_power;
192
193 /* .data alignment from optional header. */
194 int data_align_power;
195
196 /* modtype from optional header. */
197 short modtype;
198
199 /* cputype from optional header. */
200 short cputype;
201
202 /* maxdata from optional header. */
203 bfd_vma maxdata;
204
205 /* maxstack from optional header. */
206 bfd_vma maxstack;
207
208 /* Used by the XCOFF backend linker. */
209 asection **csects;
210 long *debug_indices;
211 unsigned int *lineno_counts;
212 unsigned int import_file_id;
213 };
214
215 #define xcoff_data(abfd) ((abfd)->tdata.xcoff_obj_data)
216
217 /* We take the address of the first element of an asymbol to ensure that the
218 macro is only ever applied to an asymbol. */
219 #define coffsymbol(asymbol) ((coff_symbol_type *)(&((asymbol)->the_bfd)))
220
221 /* Tdata for sections in XCOFF files. This is used by the linker. */
222
223 struct xcoff_section_tdata
224 {
225 /* Used for XCOFF csects created by the linker; points to the real
226 XCOFF section which contains this csect. */
227 asection *enclosing;
228 /* The lineno_count field for the enclosing section, because we are
229 going to clobber it there. */
230 unsigned int lineno_count;
231 /* The first and last symbol indices for symbols used by this csect. */
232 unsigned long first_symndx;
233 unsigned long last_symndx;
234 };
235
236 /* An accessor macro the xcoff_section_tdata structure. */
237 #define xcoff_section_data(abfd, sec) \
238 ((struct xcoff_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
239
240 /* Tdata for sections in PE files. */
241
242 struct pei_section_tdata
243 {
244 /* The virtual size of the section. */
245 bfd_size_type virt_size;
246 /* The PE section flags. */
247 long pe_flags;
248 };
249
250 /* An accessor macro for the pei_section_tdata structure. */
251 #define pei_section_data(abfd, sec) \
252 ((struct pei_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
253
254 /* COFF linker hash table entries. */
255
256 struct coff_link_hash_entry
257 {
258 struct bfd_link_hash_entry root;
259
260 /* Symbol index in output file. This is initialized to -1. It is
261 set to -2 if the symbol is used by a reloc. It is set to -3 if
262 this symbol is defined in a discarded section. */
263 long indx;
264
265 /* Symbol type. */
266 unsigned short type;
267
268 /* Symbol class. */
269 unsigned char symbol_class;
270
271 /* Number of auxiliary entries. */
272 char numaux;
273
274 /* BFD to take auxiliary entries from. */
275 bfd *auxbfd;
276
277 /* Pointer to array of auxiliary entries, if any. */
278 union internal_auxent *aux;
279
280 /* Flag word; legal values follow. */
281 unsigned short coff_link_hash_flags;
282 /* Symbol is a PE section symbol. */
283 #define COFF_LINK_HASH_PE_SECTION_SYMBOL (01)
284 };
285
286 /* COFF linker hash table. */
287
288 struct coff_link_hash_table
289 {
290 struct bfd_link_hash_table root;
291 /* A pointer to information used to link stabs in sections. */
292 struct stab_info stab_info;
293 /* Hash table that maps undecorated names to their respective
294 * decorated coff_link_hash_entry as found in fixup_stdcalls */
295 struct bfd_hash_table decoration_hash;
296 };
297
298 struct coff_reloc_cookie
299 {
300 struct internal_reloc * rels;
301 struct internal_reloc * rel;
302 struct internal_reloc * relend;
303 struct coff_symbol_struct * symbols; /* Symtab for input bfd. */
304 bfd * abfd;
305 struct coff_link_hash_entry ** sym_hashes;
306 };
307
308 /* Look up an entry in a COFF linker hash table. */
309
310 #define coff_link_hash_lookup(table, string, create, copy, follow) \
311 ((struct coff_link_hash_entry *) \
312 bfd_link_hash_lookup (&(table)->root, (string), (create), \
313 (copy), (follow)))
314
315 /* Traverse a COFF linker hash table. */
316
317 #define coff_link_hash_traverse(table, func, info) \
318 (bfd_link_hash_traverse \
319 (&(table)->root, \
320 (bool (*) (struct bfd_link_hash_entry *, void *)) (func), \
321 (info)))
322
323 /* Get the COFF linker hash table from a link_info structure. */
324
325 #define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
326
327 struct decoration_hash_entry
328 {
329 struct bfd_hash_entry root;
330 struct bfd_link_hash_entry *decorated_link;
331 };
332
333 /* Functions in coffgen.c. */
334 extern void coff_object_cleanup
335 (bfd *);
336 extern bfd_cleanup coff_real_object_p
337 (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
338 extern bfd_cleanup coff_object_p
339 (bfd *);
340 extern struct bfd_section *coff_section_from_bfd_index
341 (bfd *, int);
342 extern long coff_get_symtab_upper_bound
343 (bfd *);
344 extern long coff_canonicalize_symtab
345 (bfd *, asymbol **);
346 extern int coff_count_linenumbers
347 (bfd *);
348 extern bool coff_renumber_symbols
349 (bfd *, int *);
350 extern void coff_mangle_symbols
351 (bfd *);
352 extern bool coff_write_symbols
353 (bfd *);
354 extern bool coff_write_alien_symbol
355 (bfd *, asymbol *, struct internal_syment *, bfd_vma *,
356 struct bfd_strtab_hash *, bool, asection **, bfd_size_type *);
357 extern bool coff_write_linenumbers
358 (bfd *);
359 extern alent *coff_get_lineno
360 (bfd *, asymbol *);
361 extern asymbol *coff_section_symbol
362 (bfd *, char *);
363 extern bool _bfd_coff_get_external_symbols
364 (bfd *);
365 extern const char *_bfd_coff_read_string_table
366 (bfd *);
367 extern bool _bfd_coff_free_symbols
368 (bfd *);
369 extern struct coff_ptr_struct *coff_get_normalized_symtab
370 (bfd *);
371 extern long coff_get_reloc_upper_bound
372 (bfd *, sec_ptr);
373 extern asymbol *coff_make_empty_symbol
374 (bfd *);
375 extern void coff_print_symbol
376 (bfd *, void * filep, asymbol *, bfd_print_symbol_type);
377 extern void coff_get_symbol_info
378 (bfd *, asymbol *, symbol_info *ret);
379 #define coff_get_symbol_version_string \
380 _bfd_nosymbols_get_symbol_version_string
381 extern bool _bfd_coff_is_local_label_name
382 (bfd *, const char *);
383 extern asymbol *coff_bfd_make_debug_symbol
384 (bfd *);
385 extern bool coff_find_nearest_line
386 (bfd *, asymbol **, asection *, bfd_vma,
387 const char **, const char **, unsigned int *, unsigned int *);
388 #define coff_find_nearest_line_with_alt \
389 _bfd_nosymbols_find_nearest_line_with_alt
390 #define coff_find_line _bfd_nosymbols_find_line
391 struct dwarf_debug_section;
392 extern bool coff_find_nearest_line_with_names
393 (bfd *, asymbol **, asection *, bfd_vma, const char **, const char **,
394 unsigned int *, const struct dwarf_debug_section *);
395 extern bool coff_find_inliner_info
396 (bfd *, const char **, const char **, unsigned int *);
397 extern int coff_sizeof_headers
398 (bfd *, struct bfd_link_info *);
399 extern bool bfd_coff_reloc16_relax_section
400 (bfd *, asection *, struct bfd_link_info *, bool *);
401 extern bfd_byte *bfd_coff_reloc16_get_relocated_section_contents
402 (bfd *, struct bfd_link_info *, struct bfd_link_order *,
403 bfd_byte *, bool, asymbol **);
404 extern bfd_vma bfd_coff_reloc16_get_value
405 (arelent *, struct bfd_link_info *, asection *);
406 extern void bfd_perform_slip
407 (bfd *, unsigned int, asection *, bfd_vma);
408 extern bool _bfd_coff_free_cached_info
409 (bfd *);
410
411 /* Functions and types in cofflink.c. */
412
413 #define STRING_SIZE_SIZE 4
414
415 /* We use a hash table to merge identical enum, struct, and union
416 definitions in the linker. */
417
418 /* Information we keep for a single element (an enum value, a
419 structure or union field) in the debug merge hash table. */
420
421 struct coff_debug_merge_element
422 {
423 /* Next element. */
424 struct coff_debug_merge_element *next;
425
426 /* Name. */
427 const char *name;
428
429 /* Type. */
430 unsigned int type;
431
432 /* Symbol index for complex type. */
433 long tagndx;
434 };
435
436 /* A linked list of debug merge entries for a given name. */
437
438 struct coff_debug_merge_type
439 {
440 /* Next type with the same name. */
441 struct coff_debug_merge_type *next;
442
443 /* Class of type. */
444 int type_class;
445
446 /* Symbol index where this type is defined. */
447 long indx;
448
449 /* List of elements. */
450 struct coff_debug_merge_element *elements;
451 };
452
453 /* Information we store in the debug merge hash table. */
454
455 struct coff_debug_merge_hash_entry
456 {
457 struct bfd_hash_entry root;
458
459 /* A list of types with this name. */
460 struct coff_debug_merge_type *types;
461 };
462
463 /* The debug merge hash table. */
464
465 struct coff_debug_merge_hash_table
466 {
467 struct bfd_hash_table root;
468 };
469
470 /* Initialize a COFF debug merge hash table. */
471
472 #define coff_debug_merge_hash_table_init(table) \
473 (bfd_hash_table_init (&(table)->root, _bfd_coff_debug_merge_hash_newfunc, \
474 sizeof (struct coff_debug_merge_hash_entry)))
475
476 /* Free a COFF debug merge hash table. */
477
478 #define coff_debug_merge_hash_table_free(table) \
479 (bfd_hash_table_free (&(table)->root))
480
481 /* Look up an entry in a COFF debug merge hash table. */
482
483 #define coff_debug_merge_hash_lookup(table, string, create, copy) \
484 ((struct coff_debug_merge_hash_entry *) \
485 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
486
487 /* Information we keep for each section in the output file when doing
488 a relocatable link. */
489
490 struct coff_link_section_info
491 {
492 /* The relocs to be output. */
493 struct internal_reloc *relocs;
494 /* For each reloc against a global symbol whose index was not known
495 when the reloc was handled, the global hash table entry. */
496 struct coff_link_hash_entry **rel_hashes;
497 };
498
499 /* Information that we pass around while doing the final link step. */
500
501 struct coff_final_link_info
502 {
503 /* General link information. */
504 struct bfd_link_info *info;
505 /* Output BFD. */
506 bfd *output_bfd;
507 /* Used to indicate failure in traversal routine. */
508 bool failed;
509 /* If doing "task linking" set only during the time when we want the
510 global symbol writer to convert the storage class of defined global
511 symbols from global to static. */
512 bool global_to_static;
513 /* Hash table for long symbol names. */
514 struct bfd_strtab_hash *strtab;
515 /* When doing a relocatable link, an array of information kept for
516 each output section, indexed by the target_index field. */
517 struct coff_link_section_info *section_info;
518 /* Symbol index of last C_FILE symbol (-1 if none). */
519 long last_file_index;
520 /* Contents of last C_FILE symbol. */
521 struct internal_syment last_file;
522 /* Symbol index of first aux entry of last .bf symbol with an empty
523 endndx field (-1 if none). */
524 long last_bf_index;
525 /* Contents of last_bf_index aux entry. */
526 union internal_auxent last_bf;
527 /* Hash table used to merge debug information. */
528 struct coff_debug_merge_hash_table debug_merge;
529 /* Buffer large enough to hold swapped symbols of any input file. */
530 struct internal_syment *internal_syms;
531 /* Buffer large enough to hold sections of symbols of any input file. */
532 asection **sec_ptrs;
533 /* Buffer large enough to hold output indices of symbols of any
534 input file. */
535 long *sym_indices;
536 /* Buffer large enough to hold output symbols for any input file. */
537 bfd_byte *outsyms;
538 /* Buffer large enough to hold external line numbers for any input
539 section. */
540 bfd_byte *linenos;
541 /* Buffer large enough to hold any input section. */
542 bfd_byte *contents;
543 /* Buffer large enough to hold external relocs of any input section. */
544 bfd_byte *external_relocs;
545 /* Buffer large enough to hold swapped relocs of any input section. */
546 struct internal_reloc *internal_relocs;
547 };
548
549 /* Most COFF variants have no way to record the alignment of a
550 section. This struct is used to set a specific alignment based on
551 the name of the section. */
552
553 struct coff_section_alignment_entry
554 {
555 /* The section name. */
556 const char *name;
557
558 /* This is either (unsigned int) -1, indicating that the section
559 name must match exactly, or it is the number of letters which
560 must match at the start of the name. */
561 unsigned int comparison_length;
562
563 /* These macros may be used to fill in the first two fields in a
564 structure initialization. */
565 #define COFF_SECTION_NAME_EXACT_MATCH(name) (name), ((unsigned int) -1)
566 #define COFF_SECTION_NAME_PARTIAL_MATCH(name) (name), (sizeof (name) - 1)
567
568 /* Only use this entry if the default section alignment for this
569 target is at least that much (as a power of two). If this field
570 is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
571 unsigned int default_alignment_min;
572
573 /* Only use this entry if the default section alignment for this
574 target is no greater than this (as a power of two). If this
575 field is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored. */
576 unsigned int default_alignment_max;
577
578 #define COFF_ALIGNMENT_FIELD_EMPTY ((unsigned int) -1)
579
580 /* The desired alignment for this section (as a power of two). */
581 unsigned int alignment_power;
582 };
583
584 extern struct bfd_hash_entry *_decoration_hash_newfunc
585 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
586 extern struct bfd_hash_entry *_bfd_coff_link_hash_newfunc
587 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
588 extern bool _bfd_coff_link_hash_table_init
589 (struct coff_link_hash_table *, bfd *,
590 struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
591 struct bfd_hash_table *,
592 const char *),
593 unsigned int);
594 extern struct bfd_link_hash_table *_bfd_coff_link_hash_table_create
595 (bfd *);
596 extern const char *_bfd_coff_internal_syment_name
597 (bfd *, const struct internal_syment *, char *);
598 extern bool _bfd_coff_section_already_linked
599 (bfd *, asection *, struct bfd_link_info *);
600 extern bool _bfd_coff_link_add_symbols
601 (bfd *, struct bfd_link_info *);
602 extern bool _bfd_coff_final_link
603 (bfd *, struct bfd_link_info *);
604 extern struct internal_reloc *_bfd_coff_read_internal_relocs
605 (bfd *, asection *, bool, bfd_byte *, bool,
606 struct internal_reloc *);
607 extern bool _bfd_coff_generic_relocate_section
608 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
609 struct internal_reloc *, struct internal_syment *, asection **);
610 extern struct bfd_hash_entry *_bfd_coff_debug_merge_hash_newfunc
611 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
612 extern bool _bfd_coff_write_global_sym
613 (struct bfd_hash_entry *, void *);
614 extern bool _bfd_coff_write_task_globals
615 (struct coff_link_hash_entry *, void *);
616 extern bool _bfd_coff_link_input_bfd
617 (struct coff_final_link_info *, bfd *);
618 extern bool _bfd_coff_reloc_link_order
619 (bfd *, struct coff_final_link_info *, asection *,
620 struct bfd_link_order *);
621 extern bool bfd_coff_gc_sections
622 (bfd *, struct bfd_link_info *);
623 extern const char *bfd_coff_group_name
624 (bfd *, const asection *);
625
626 #define coff_get_section_contents_in_window \
627 _bfd_generic_get_section_contents_in_window
628
629 /* Functions in xcofflink.c. */
630
631 extern long _bfd_xcoff_get_dynamic_symtab_upper_bound
632 (bfd *);
633 extern long _bfd_xcoff_canonicalize_dynamic_symtab
634 (bfd *, asymbol **);
635 extern long _bfd_xcoff_get_dynamic_reloc_upper_bound
636 (bfd *);
637 extern long _bfd_xcoff_canonicalize_dynamic_reloc
638 (bfd *, arelent **, asymbol **);
639 extern struct bfd_link_hash_table *_bfd_xcoff_bfd_link_hash_table_create
640 (bfd *);
641 extern bool _bfd_xcoff_bfd_link_add_symbols
642 (bfd *, struct bfd_link_info *);
643 extern bool _bfd_xcoff_bfd_final_link
644 (bfd *, struct bfd_link_info *);
645 extern bool _bfd_xcoff_define_common_symbol
646 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *);
647 extern bool _bfd_ppc_xcoff_relocate_section
648 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
649 struct internal_reloc *, struct internal_syment *, asection **);