section-select: Lazily resolve section matches
[binutils-gdb.git] / ld / ldlang.h
1 /* ldlang.h - linker command language support
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
3
4 This file is part of the GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #ifndef LDLANG_H
22 #define LDLANG_H
23
24 #define DEFAULT_MEMORY_REGION "*default*"
25
26 #define SECTION_NAME_MAP_LENGTH (16)
27
28 typedef enum
29 {
30 lang_input_file_is_l_enum,
31 lang_input_file_is_symbols_only_enum,
32 lang_input_file_is_marker_enum,
33 lang_input_file_is_fake_enum,
34 lang_input_file_is_search_file_enum,
35 lang_input_file_is_file_enum
36 } lang_input_file_enum_type;
37
38 struct _fill_type
39 {
40 size_t size;
41 unsigned char data[1];
42 };
43
44 typedef struct statement_list
45 {
46 union lang_statement_union * head;
47 union lang_statement_union ** tail;
48 } lang_statement_list_type;
49
50 typedef struct memory_region_name_struct
51 {
52 const char * name;
53 struct memory_region_name_struct * next;
54 } lang_memory_region_name;
55
56 typedef struct memory_region_struct
57 {
58 lang_memory_region_name name_list;
59 struct memory_region_struct *next;
60 union etree_union *origin_exp;
61 bfd_vma origin;
62 bfd_size_type length;
63 union etree_union *length_exp;
64 bfd_vma current;
65 union lang_statement_union *last_os;
66 flagword flags;
67 flagword not_flags;
68 bool had_full_message;
69 } lang_memory_region_type;
70
71 enum statement_enum
72 {
73 lang_address_statement_enum,
74 lang_assignment_statement_enum,
75 lang_data_statement_enum,
76 lang_fill_statement_enum,
77 lang_group_statement_enum,
78 lang_input_section_enum,
79 lang_input_matcher_enum,
80 lang_input_statement_enum,
81 lang_insert_statement_enum,
82 lang_output_section_statement_enum,
83 lang_output_statement_enum,
84 lang_padding_statement_enum,
85 lang_reloc_statement_enum,
86 lang_target_statement_enum,
87 lang_wild_statement_enum,
88 lang_constructors_statement_enum,
89 lang_object_symbols_statement_enum
90 };
91
92 typedef struct lang_statement_header_struct
93 {
94 /* Next pointer for statement_list statement list. */
95 union lang_statement_union *next;
96 enum statement_enum type;
97 } lang_statement_header_type;
98
99 typedef struct
100 {
101 lang_statement_header_type header;
102 union etree_union *exp;
103 } lang_assignment_statement_type;
104
105 typedef struct lang_target_statement_struct
106 {
107 lang_statement_header_type header;
108 const char *target;
109 } lang_target_statement_type;
110
111 typedef struct lang_output_statement_struct
112 {
113 lang_statement_header_type header;
114 const char *name;
115 } lang_output_statement_type;
116
117 /* Section types specified in a linker script. */
118
119 enum section_type
120 {
121 normal_section,
122 first_overlay_section,
123 overlay_section,
124 noload_section,
125 noalloc_section,
126 type_section,
127 readonly_section,
128 typed_readonly_section
129 };
130
131 /* This structure holds a list of program headers describing
132 segments in which this section should be placed. */
133
134 typedef struct lang_output_section_phdr_list
135 {
136 struct lang_output_section_phdr_list *next;
137 const char *name;
138 bool used;
139 } lang_output_section_phdr_list;
140
141 typedef struct lang_output_section_statement_struct
142 {
143 lang_statement_header_type header;
144 lang_statement_list_type children;
145 struct lang_output_section_statement_struct *next;
146 struct lang_output_section_statement_struct *prev;
147 const char *name;
148 asection *bfd_section;
149 lang_memory_region_type *region;
150 lang_memory_region_type *lma_region;
151 fill_type *fill;
152 union etree_union *addr_tree;
153 union etree_union *load_base;
154 union etree_union *section_alignment;
155 union etree_union *subsection_alignment;
156
157 /* If non-null, an expression to evaluate after setting the section's
158 size. The expression is evaluated inside REGION (above) with '.'
159 set to the end of the section. Used in the last overlay section
160 to move '.' past all the overlaid sections. */
161 union etree_union *update_dot_tree;
162
163 lang_output_section_phdr_list *phdrs;
164
165 /* Used by ELF SHF_LINK_ORDER sorting. */
166 void *data;
167
168 unsigned int block_value;
169 int constraint;
170 flagword flags;
171 enum section_type sectype;
172 etree_type *sectype_value;
173 unsigned int processed_vma : 1;
174 unsigned int processed_lma : 1;
175 unsigned int all_input_readonly : 1;
176 /* If this section should be ignored. */
177 unsigned int ignored : 1;
178 /* If this section should update "dot". Prevents section being ignored. */
179 unsigned int update_dot : 1;
180 /* If this section is after assignment to _end. */
181 unsigned int after_end : 1;
182 /* If this section uses the alignment of its input sections. */
183 unsigned int align_lma_with_input : 1;
184 /* If script has duplicate output section statements of the same name
185 create duplicate output sections. */
186 unsigned int dup_output : 1;
187 } lang_output_section_statement_type;
188
189 typedef struct
190 {
191 lang_statement_header_type header;
192 fill_type *fill;
193 int size;
194 asection *output_section;
195 } lang_fill_statement_type;
196
197 typedef struct
198 {
199 lang_statement_header_type header;
200 unsigned int type;
201 union etree_union *exp;
202 bfd_vma value;
203 asection *output_section;
204 bfd_vma output_offset;
205 } lang_data_statement_type;
206
207 /* Generate a reloc in the output file. */
208
209 typedef struct
210 {
211 lang_statement_header_type header;
212
213 /* Reloc to generate. */
214 bfd_reloc_code_real_type reloc;
215
216 /* Reloc howto structure. */
217 reloc_howto_type *howto;
218
219 /* Section to generate reloc against.
220 Exactly one of section and name must be NULL. */
221 asection *section;
222
223 /* Name of symbol to generate reloc against.
224 Exactly one of section and name must be NULL. */
225 const char *name;
226
227 /* Expression for addend. */
228 union etree_union *addend_exp;
229
230 /* Resolved addend. */
231 bfd_vma addend_value;
232
233 /* Output section where reloc should be performed. */
234 asection *output_section;
235
236 /* Offset within output section. */
237 bfd_vma output_offset;
238 } lang_reloc_statement_type;
239
240 struct lang_input_statement_flags
241 {
242 /* 1 means this file was specified in a -l option. */
243 unsigned int maybe_archive : 1;
244
245 /* 1 means this file was specified in a -l:namespec option. */
246 unsigned int full_name_provided : 1;
247
248 /* 1 means search a set of directories for this file. */
249 unsigned int search_dirs : 1;
250
251 /* 1 means this was found when processing a script in the sysroot. */
252 unsigned int sysrooted : 1;
253
254 /* 1 means this is base file of incremental load.
255 Do not load this file's text or data.
256 Also default text_start to after this file's bss. */
257 unsigned int just_syms : 1;
258
259 /* Whether to search for this entry as a dynamic archive. */
260 unsigned int dynamic : 1;
261
262 /* Set if a DT_NEEDED tag should be added not just for the dynamic library
263 explicitly given by this entry but also for any dynamic libraries in
264 this entry's needed list. */
265 unsigned int add_DT_NEEDED_for_dynamic : 1;
266
267 /* Set if this entry should cause a DT_NEEDED tag only when some
268 regular file references its symbols (ie. --as-needed is in effect). */
269 unsigned int add_DT_NEEDED_for_regular : 1;
270
271 /* Whether to include the entire contents of an archive. */
272 unsigned int whole_archive : 1;
273
274 /* Set when bfd opening is successful. */
275 unsigned int loaded : 1;
276
277 unsigned int real : 1;
278
279 /* Set if the file does not exist. */
280 unsigned int missing_file : 1;
281
282 /* Set if reloading an archive or --as-needed lib. */
283 unsigned int reload : 1;
284
285 #if BFD_SUPPORTS_PLUGINS
286 /* Set if the file was claimed by a plugin. */
287 unsigned int claimed : 1;
288
289 /* Set if the file was claimed from an archive. */
290 unsigned int claim_archive : 1;
291
292 /* Set if added by the lto plugin add_input_file callback. */
293 unsigned int lto_output : 1;
294 #endif /* BFD_SUPPORTS_PLUGINS */
295
296 /* Head of list of pushed flags. */
297 struct lang_input_statement_flags *pushed;
298 };
299
300 typedef struct lang_input_statement_struct
301 {
302 lang_statement_header_type header;
303 /* Name of this file. */
304 const char *filename;
305 /* Name to use for the symbol giving address of text start.
306 Usually the same as filename, but for a file spec'd with
307 -l this is the -l switch itself rather than the filename. */
308 const char *local_sym_name;
309 /* Name to use when sorting. */
310 const char *sort_key;
311 /* Extra search path. Used to find a file relative to the
312 directory of the current linker script. */
313 const char *extra_search_path;
314
315 bfd *the_bfd;
316
317 ctf_archive_t *the_ctf;
318
319 struct flag_info *section_flag_list;
320
321 /* Next pointer for file_chain statement list. */
322 struct lang_input_statement_struct *next;
323
324 /* Next pointer for input_file_chain statement list. */
325 struct lang_input_statement_struct *next_real_file;
326
327 const char *target;
328
329 struct lang_input_statement_flags flags;
330 } lang_input_statement_type;
331
332 typedef struct
333 {
334 lang_statement_header_type header;
335 asection *section;
336 void *pattern;
337 } lang_input_section_type;
338
339 typedef struct
340 {
341 lang_statement_header_type header;
342 asection *section;
343 void *pattern;
344 lang_input_statement_type *input_stmt;
345 } lang_input_matcher_type;
346
347 struct map_symbol_def {
348 struct bfd_link_hash_entry *entry;
349 struct map_symbol_def *next;
350 };
351
352 /* For input sections, when writing a map file: head / tail of a linked
353 list of hash table entries for symbols defined in this section. */
354 typedef struct input_section_userdata_struct
355 {
356 struct map_symbol_def *map_symbol_def_head;
357 struct map_symbol_def **map_symbol_def_tail;
358 unsigned long map_symbol_def_count;
359 } input_section_userdata_type;
360
361 static inline bool
362 bfd_input_just_syms (const bfd *abfd)
363 {
364 lang_input_statement_type *is = bfd_usrdata (abfd);
365 return is != NULL && is->flags.just_syms;
366 }
367
368 typedef struct lang_wild_statement_struct lang_wild_statement_type;
369
370 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
371 asection *, lang_input_statement_type *, void *);
372
373 typedef void (*walk_wild_section_handler_t) (lang_wild_statement_type *,
374 lang_input_statement_type *,
375 callback_t callback,
376 void *data);
377
378 typedef bool (*lang_match_sec_type_func) (bfd *, const asection *,
379 bfd *, const asection *);
380
381 /* Binary search tree structure to efficiently sort sections by
382 name. */
383 typedef struct lang_section_bst
384 {
385 asection *section;
386 void *pattern;
387 struct lang_section_bst *left;
388 struct lang_section_bst *right;
389 } lang_section_bst_type;
390
391 struct lang_wild_statement_struct
392 {
393 lang_statement_header_type header;
394 const char *filename;
395 bool filenames_sorted;
396 bool any_specs_sorted;
397 struct wildcard_list *section_list;
398 bool keep_sections;
399 lang_statement_list_type children;
400 struct name_list *exclude_name_list;
401 lang_statement_list_type matching_sections;
402 bool resolved;
403
404 walk_wild_section_handler_t walk_wild_section_handler;
405 struct wildcard_list *handler_data[4];
406 lang_section_bst_type *tree, **rightmost;
407 struct flag_info *section_flag_list;
408 };
409
410 typedef struct lang_address_statement_struct
411 {
412 lang_statement_header_type header;
413 const char *section_name;
414 union etree_union *address;
415 const segment_type *segment;
416 } lang_address_statement_type;
417
418 typedef struct
419 {
420 lang_statement_header_type header;
421 bfd_vma output_offset;
422 bfd_size_type size;
423 asection *output_section;
424 fill_type *fill;
425 } lang_padding_statement_type;
426
427 /* A group statement collects a set of libraries together. The
428 libraries are searched multiple times, until no new undefined
429 symbols are found. The effect is to search a group of libraries as
430 though they were a single library. */
431
432 typedef struct
433 {
434 lang_statement_header_type header;
435 lang_statement_list_type children;
436 } lang_group_statement_type;
437
438 typedef struct
439 {
440 lang_statement_header_type header;
441 const char *where;
442 bool is_before;
443 } lang_insert_statement_type;
444
445 typedef union lang_statement_union
446 {
447 lang_statement_header_type header;
448 lang_address_statement_type address_statement;
449 lang_assignment_statement_type assignment_statement;
450 lang_data_statement_type data_statement;
451 lang_fill_statement_type fill_statement;
452 lang_group_statement_type group_statement;
453 lang_input_section_type input_section;
454 lang_input_matcher_type input_matcher;
455 lang_input_statement_type input_statement;
456 lang_insert_statement_type insert_statement;
457 lang_output_section_statement_type output_section_statement;
458 lang_output_statement_type output_statement;
459 lang_padding_statement_type padding_statement;
460 lang_reloc_statement_type reloc_statement;
461 lang_target_statement_type target_statement;
462 lang_wild_statement_type wild_statement;
463 } lang_statement_union_type;
464
465 /* This structure holds information about a program header, from the
466 PHDRS command in the linker script. */
467
468 struct lang_phdr
469 {
470 struct lang_phdr *next;
471 const char *name;
472 unsigned long type;
473 bool filehdr;
474 bool phdrs;
475 etree_type *at;
476 etree_type *flags;
477 };
478
479 /* This structure is used to hold a list of sections which may not
480 cross reference each other. */
481
482 typedef struct lang_nocrossref
483 {
484 struct lang_nocrossref *next;
485 const char *name;
486 } lang_nocrossref_type;
487
488 /* The list of nocrossref lists. */
489
490 struct lang_nocrossrefs
491 {
492 struct lang_nocrossrefs *next;
493 lang_nocrossref_type *list;
494 bool onlyfirst;
495 };
496
497 /* This structure is used to hold a list of input section names which
498 will not match an output section in the linker script. */
499
500 struct unique_sections
501 {
502 struct unique_sections *next;
503 const char *name;
504 };
505
506 /* Used by place_orphan to keep track of orphan sections and statements. */
507
508 struct orphan_save
509 {
510 const char *name;
511 flagword flags;
512 lang_output_section_statement_type *os;
513 asection **section;
514 lang_statement_union_type **stmt;
515 lang_output_section_statement_type **os_tail;
516 };
517
518 struct asneeded_minfo
519 {
520 struct asneeded_minfo *next;
521 const char *soname;
522 bfd *ref;
523 const char *name;
524 };
525
526 extern struct lang_phdr *lang_phdr_list;
527 extern struct lang_nocrossrefs *nocrossref_list;
528 extern const char *output_target;
529 extern lang_output_section_statement_type *abs_output_section;
530 extern lang_statement_list_type lang_os_list;
531 extern struct lang_input_statement_flags input_flags;
532 extern bool lang_has_input_file;
533 extern lang_statement_list_type statement_list;
534 extern lang_statement_list_type *stat_ptr;
535 extern bool delete_output_file_on_failure;
536
537 extern struct bfd_sym_chain entry_symbol;
538 extern const char *entry_section;
539 extern bool entry_from_cmdline;
540 extern lang_statement_list_type file_chain;
541 extern lang_statement_list_type input_file_chain;
542
543 extern struct bfd_elf_dynamic_list **current_dynamic_list_p;
544
545 extern int lang_statement_iteration;
546 extern struct asneeded_minfo **asneeded_list_tail;
547
548 extern void (*output_bfd_hash_table_free_fn) (struct bfd_link_hash_table *);
549
550 extern void lang_init
551 (void);
552 extern void lang_finish
553 (void);
554 extern lang_memory_region_type * lang_memory_region_lookup
555 (const char * const, bool);
556 extern void lang_memory_region_alias
557 (const char *, const char *);
558 extern void lang_map
559 (void);
560 extern void lang_set_flags
561 (lang_memory_region_type *, const char *, int);
562 extern void lang_add_output
563 (const char *, int from_script);
564 extern lang_output_section_statement_type *lang_enter_output_section_statement
565 (const char *, etree_type *, enum section_type, etree_type *, etree_type *,
566 etree_type *, etree_type *, int, int);
567 extern void lang_final
568 (void);
569 extern void lang_relax_sections
570 (bool);
571 extern void lang_process
572 (void);
573 extern void lang_section_start
574 (const char *, union etree_union *, const segment_type *);
575 extern void lang_add_entry
576 (const char *, bool);
577 extern void lang_default_entry
578 (const char *);
579 extern void lang_add_target
580 (const char *);
581 extern void lang_add_wild
582 (struct wildcard_spec *, struct wildcard_list *, bool);
583 extern void lang_add_map
584 (const char *);
585 extern void lang_add_fill
586 (fill_type *);
587 extern lang_assignment_statement_type *lang_add_assignment
588 (union etree_union *);
589 extern void lang_add_attribute
590 (enum statement_enum);
591 extern void lang_startup
592 (const char *);
593 extern void lang_float
594 (bool);
595 extern void lang_leave_output_section_statement
596 (fill_type *, const char *, lang_output_section_phdr_list *,
597 const char *);
598 extern void lang_for_each_input_file
599 (void (*dothis) (lang_input_statement_type *));
600 extern void lang_for_each_file
601 (void (*dothis) (lang_input_statement_type *));
602 extern void lang_reset_memory_regions
603 (void);
604 extern void lang_do_assignments
605 (lang_phase_type);
606 extern asection *section_for_dot
607 (void);
608
609 #define LANG_FOR_EACH_INPUT_STATEMENT(statement) \
610 lang_input_statement_type *statement; \
611 for (statement = (lang_input_statement_type *) file_chain.head; \
612 statement != NULL; \
613 statement = statement->next)
614
615 #define lang_output_section_find(NAME) \
616 lang_output_section_statement_lookup (NAME, 0, 0)
617
618 extern void lang_process
619 (void);
620 extern void ldlang_add_file
621 (lang_input_statement_type *);
622 extern lang_output_section_statement_type *lang_output_section_find_by_flags
623 (const asection *, flagword, lang_output_section_statement_type **,
624 lang_match_sec_type_func);
625 extern lang_output_section_statement_type *lang_insert_orphan
626 (asection *, const char *, int, lang_output_section_statement_type *,
627 struct orphan_save *, etree_type *, lang_statement_list_type *);
628 extern lang_input_statement_type *lang_add_input_file
629 (const char *, lang_input_file_enum_type, const char *);
630 extern void lang_add_keepsyms_file
631 (const char *);
632 extern lang_output_section_statement_type *lang_output_section_get
633 (const asection *);
634 extern lang_output_section_statement_type *lang_output_section_statement_lookup
635 (const char *, int, int);
636 extern lang_output_section_statement_type *next_matching_output_section_statement
637 (lang_output_section_statement_type *, int);
638 extern void ldlang_add_undef
639 (const char *const, bool);
640 extern void ldlang_add_require_defined
641 (const char *const);
642 extern void lang_add_output_format
643 (const char *, const char *, const char *, int);
644 extern void lang_list_init
645 (lang_statement_list_type *);
646 extern void push_stat_ptr
647 (lang_statement_list_type *);
648 extern void pop_stat_ptr
649 (void);
650 extern void lang_add_data
651 (int type, union etree_union *);
652 extern void lang_add_reloc
653 (bfd_reloc_code_real_type, reloc_howto_type *, asection *, const char *,
654 union etree_union *);
655 extern void lang_for_each_statement
656 (void (*) (lang_statement_union_type *));
657 extern void lang_for_each_statement_worker
658 (void (*) (lang_statement_union_type *), lang_statement_union_type *);
659 extern void *stat_alloc
660 (size_t);
661 extern void strip_excluded_output_sections
662 (void);
663 extern void lang_clear_os_map
664 (void);
665 extern void dprint_statement
666 (lang_statement_union_type *, int);
667 extern void lang_size_sections
668 (bool *, bool);
669 extern void one_lang_size_sections_pass
670 (bool *, bool);
671 extern void lang_add_insert
672 (const char *, int);
673 extern void lang_enter_group
674 (void);
675 extern void lang_leave_group
676 (void);
677 extern void lang_add_section
678 (lang_statement_list_type *, asection *, struct wildcard_list *,
679 struct flag_info *, lang_output_section_statement_type *);
680 extern void lang_new_phdr
681 (const char *, etree_type *, bool, bool, etree_type *,
682 etree_type *);
683 extern void lang_add_nocrossref
684 (lang_nocrossref_type *);
685 extern void lang_add_nocrossref_to
686 (lang_nocrossref_type *);
687 extern void lang_enter_overlay
688 (etree_type *, etree_type *);
689 extern void lang_enter_overlay_section
690 (const char *);
691 extern void lang_leave_overlay_section
692 (fill_type *, lang_output_section_phdr_list *);
693 extern void lang_leave_overlay
694 (etree_type *, int, fill_type *, const char *,
695 lang_output_section_phdr_list *, const char *);
696
697 extern struct bfd_elf_version_expr *lang_new_vers_pattern
698 (struct bfd_elf_version_expr *, const char *, const char *, bool);
699 extern struct bfd_elf_version_tree *lang_new_vers_node
700 (struct bfd_elf_version_expr *, struct bfd_elf_version_expr *);
701 extern struct bfd_elf_version_deps *lang_add_vers_depend
702 (struct bfd_elf_version_deps *, const char *);
703 extern void lang_register_vers_node
704 (const char *, struct bfd_elf_version_tree *, struct bfd_elf_version_deps *);
705 extern void lang_append_dynamic_list (struct bfd_elf_dynamic_list **,
706 struct bfd_elf_version_expr *);
707 extern void lang_append_dynamic_list_cpp_typeinfo (void);
708 extern void lang_append_dynamic_list_cpp_new (void);
709 extern void lang_add_unique
710 (const char *);
711 extern const char *lang_get_output_target
712 (void);
713 extern void add_excluded_libs (const char *);
714 extern bool load_symbols
715 (lang_input_statement_type *, lang_statement_list_type *);
716
717 struct elf_sym_strtab;
718 struct elf_strtab_hash;
719 extern void ldlang_ctf_acquire_strings
720 (struct elf_strtab_hash *);
721 extern void ldlang_ctf_new_dynsym
722 (int symidx, struct elf_internal_sym *);
723 extern void ldlang_write_ctf_late
724 (void);
725 extern bool
726 ldlang_override_segment_assignment
727 (struct bfd_link_info *, bfd *, asection *, asection *, bool);
728
729 extern void
730 lang_ld_feature (char *);
731
732 extern void
733 lang_print_memory_usage (void);
734
735 extern void
736 lang_add_gc_name (const char *);
737
738 extern bool
739 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr);
740
741 #endif