1 /* ldlang.h - linker command language support
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
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.
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.
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. */
24 #define DEFAULT_MEMORY_REGION "*default*"
26 #define SECTION_NAME_MAP_LENGTH (16)
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
;
41 unsigned char data
[1];
44 typedef struct statement_list
46 union lang_statement_union
* head
;
47 union lang_statement_union
** tail
;
48 } lang_statement_list_type
;
50 typedef struct memory_region_name_struct
53 struct memory_region_name_struct
* next
;
54 } lang_memory_region_name
;
56 typedef struct memory_region_struct
58 lang_memory_region_name name_list
;
59 struct memory_region_struct
*next
;
60 union etree_union
*origin_exp
;
63 union etree_union
*length_exp
;
65 union lang_statement_union
*last_os
;
68 bool had_full_message
;
69 } lang_memory_region_type
;
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_statement_enum
,
80 lang_insert_statement_enum
,
81 lang_output_section_statement_enum
,
82 lang_output_statement_enum
,
83 lang_padding_statement_enum
,
84 lang_reloc_statement_enum
,
85 lang_target_statement_enum
,
86 lang_wild_statement_enum
,
87 lang_constructors_statement_enum
,
88 lang_object_symbols_statement_enum
91 typedef struct lang_statement_header_struct
93 /* Next pointer for statement_list statement list. */
94 union lang_statement_union
*next
;
95 enum statement_enum type
;
96 } lang_statement_header_type
;
100 lang_statement_header_type header
;
101 union etree_union
*exp
;
102 } lang_assignment_statement_type
;
104 typedef struct lang_target_statement_struct
106 lang_statement_header_type header
;
108 } lang_target_statement_type
;
110 typedef struct lang_output_statement_struct
112 lang_statement_header_type header
;
114 } lang_output_statement_type
;
116 /* Section types specified in a linker script. */
121 first_overlay_section
,
127 typed_readonly_section
130 /* This structure holds a list of program headers describing
131 segments in which this section should be placed. */
133 typedef struct lang_output_section_phdr_list
135 struct lang_output_section_phdr_list
*next
;
138 } lang_output_section_phdr_list
;
140 typedef struct lang_output_section_statement_struct
142 lang_statement_header_type header
;
143 lang_statement_list_type children
;
144 struct lang_output_section_statement_struct
*next
;
145 struct lang_output_section_statement_struct
*prev
;
147 asection
*bfd_section
;
148 lang_memory_region_type
*region
;
149 lang_memory_region_type
*lma_region
;
151 union etree_union
*addr_tree
;
152 union etree_union
*load_base
;
153 union etree_union
*section_alignment
;
154 union etree_union
*subsection_alignment
;
156 /* If non-null, an expression to evaluate after setting the section's
157 size. The expression is evaluated inside REGION (above) with '.'
158 set to the end of the section. Used in the last overlay section
159 to move '.' past all the overlaid sections. */
160 union etree_union
*update_dot_tree
;
162 lang_output_section_phdr_list
*phdrs
;
164 /* Used by ELF SHF_LINK_ORDER sorting. */
167 unsigned int block_value
;
170 enum section_type sectype
;
171 etree_type
*sectype_value
;
172 unsigned int processed_vma
: 1;
173 unsigned int processed_lma
: 1;
174 unsigned int all_input_readonly
: 1;
175 /* If this section should be ignored. */
176 unsigned int ignored
: 1;
177 /* If this section should update "dot". Prevents section being ignored. */
178 unsigned int update_dot
: 1;
179 /* If this section is after assignment to _end. */
180 unsigned int after_end
: 1;
181 /* If this section uses the alignment of its input sections. */
182 unsigned int align_lma_with_input
: 1;
183 /* If script has duplicate output section statements of the same name
184 create duplicate output sections. */
185 unsigned int dup_output
: 1;
186 } lang_output_section_statement_type
;
190 lang_statement_header_type header
;
193 asection
*output_section
;
194 } lang_fill_statement_type
;
198 lang_statement_header_type header
;
200 union etree_union
*exp
;
202 asection
*output_section
;
203 bfd_vma output_offset
;
204 } lang_data_statement_type
;
206 /* Generate a reloc in the output file. */
210 lang_statement_header_type header
;
212 /* Reloc to generate. */
213 bfd_reloc_code_real_type reloc
;
215 /* Reloc howto structure. */
216 reloc_howto_type
*howto
;
218 /* Section to generate reloc against.
219 Exactly one of section and name must be NULL. */
222 /* Name of symbol to generate reloc against.
223 Exactly one of section and name must be NULL. */
226 /* Expression for addend. */
227 union etree_union
*addend_exp
;
229 /* Resolved addend. */
230 bfd_vma addend_value
;
232 /* Output section where reloc should be performed. */
233 asection
*output_section
;
235 /* Offset within output section. */
236 bfd_vma output_offset
;
237 } lang_reloc_statement_type
;
239 struct lang_input_statement_flags
241 /* 1 means this file was specified in a -l option. */
242 unsigned int maybe_archive
: 1;
244 /* 1 means this file was specified in a -l:namespec option. */
245 unsigned int full_name_provided
: 1;
247 /* 1 means search a set of directories for this file. */
248 unsigned int search_dirs
: 1;
250 /* 1 means this was found when processing a script in the sysroot. */
251 unsigned int sysrooted
: 1;
253 /* 1 means this is base file of incremental load.
254 Do not load this file's text or data.
255 Also default text_start to after this file's bss. */
256 unsigned int just_syms
: 1;
258 /* Whether to search for this entry as a dynamic archive. */
259 unsigned int dynamic
: 1;
261 /* Set if a DT_NEEDED tag should be added not just for the dynamic library
262 explicitly given by this entry but also for any dynamic libraries in
263 this entry's needed list. */
264 unsigned int add_DT_NEEDED_for_dynamic
: 1;
266 /* Set if this entry should cause a DT_NEEDED tag only when some
267 regular file references its symbols (ie. --as-needed is in effect). */
268 unsigned int add_DT_NEEDED_for_regular
: 1;
270 /* Whether to include the entire contents of an archive. */
271 unsigned int whole_archive
: 1;
273 /* Set when bfd opening is successful. */
274 unsigned int loaded
: 1;
276 unsigned int real
: 1;
278 /* Set if the file does not exist. */
279 unsigned int missing_file
: 1;
281 /* Set if reloading an archive or --as-needed lib. */
282 unsigned int reload
: 1;
284 #if BFD_SUPPORTS_PLUGINS
285 /* Set if the file was claimed by a plugin. */
286 unsigned int claimed
: 1;
288 /* Set if the file was claimed from an archive. */
289 unsigned int claim_archive
: 1;
291 /* Set if added by the lto plugin add_input_file callback. */
292 unsigned int lto_output
: 1;
293 #endif /* BFD_SUPPORTS_PLUGINS */
295 /* Head of list of pushed flags. */
296 struct lang_input_statement_flags
*pushed
;
299 typedef struct lang_input_statement_struct
301 lang_statement_header_type header
;
302 /* Name of this file. */
303 const char *filename
;
304 /* Name to use for the symbol giving address of text start.
305 Usually the same as filename, but for a file spec'd with
306 -l this is the -l switch itself rather than the filename. */
307 const char *local_sym_name
;
308 /* Extra search path. Used to find a file relative to the
309 directory of the current linker script. */
310 const char *extra_search_path
;
314 ctf_archive_t
*the_ctf
;
316 struct flag_info
*section_flag_list
;
318 /* Next pointer for file_chain statement list. */
319 struct lang_input_statement_struct
*next
;
321 /* Next pointer for input_file_chain statement list. */
322 struct lang_input_statement_struct
*next_real_file
;
326 struct lang_input_statement_flags flags
;
327 } lang_input_statement_type
;
331 lang_statement_header_type header
;
334 } lang_input_section_type
;
336 struct map_symbol_def
{
337 struct bfd_link_hash_entry
*entry
;
338 struct map_symbol_def
*next
;
341 /* For input sections, when writing a map file: head / tail of a linked
342 list of hash table entries for symbols defined in this section. */
343 typedef struct input_section_userdata_struct
345 struct map_symbol_def
*map_symbol_def_head
;
346 struct map_symbol_def
**map_symbol_def_tail
;
347 unsigned long map_symbol_def_count
;
348 } input_section_userdata_type
;
351 bfd_input_just_syms (const bfd
*abfd
)
353 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
354 return is
!= NULL
&& is
->flags
.just_syms
;
357 typedef struct lang_wild_statement_struct lang_wild_statement_type
;
359 typedef void (*callback_t
) (lang_wild_statement_type
*, struct wildcard_list
*,
360 asection
*, lang_input_statement_type
*, void *);
362 typedef void (*walk_wild_section_handler_t
) (lang_wild_statement_type
*,
363 lang_input_statement_type
*,
367 typedef bool (*lang_match_sec_type_func
) (bfd
*, const asection
*,
368 bfd
*, const asection
*);
370 /* Binary search tree structure to efficiently sort sections by
372 typedef struct lang_section_bst
376 struct lang_section_bst
*left
;
377 struct lang_section_bst
*right
;
378 } lang_section_bst_type
;
380 struct lang_wild_statement_struct
382 lang_statement_header_type header
;
383 const char *filename
;
384 bool filenames_sorted
;
385 struct wildcard_list
*section_list
;
387 lang_statement_list_type children
;
388 struct name_list
*exclude_name_list
;
390 walk_wild_section_handler_t walk_wild_section_handler
;
391 struct wildcard_list
*handler_data
[4];
392 lang_section_bst_type
*tree
;
393 struct flag_info
*section_flag_list
;
396 typedef struct lang_address_statement_struct
398 lang_statement_header_type header
;
399 const char *section_name
;
400 union etree_union
*address
;
401 const segment_type
*segment
;
402 } lang_address_statement_type
;
406 lang_statement_header_type header
;
407 bfd_vma output_offset
;
409 asection
*output_section
;
411 } lang_padding_statement_type
;
413 /* A group statement collects a set of libraries together. The
414 libraries are searched multiple times, until no new undefined
415 symbols are found. The effect is to search a group of libraries as
416 though they were a single library. */
420 lang_statement_header_type header
;
421 lang_statement_list_type children
;
422 } lang_group_statement_type
;
426 lang_statement_header_type header
;
429 } lang_insert_statement_type
;
431 typedef union lang_statement_union
433 lang_statement_header_type header
;
434 lang_address_statement_type address_statement
;
435 lang_assignment_statement_type assignment_statement
;
436 lang_data_statement_type data_statement
;
437 lang_fill_statement_type fill_statement
;
438 lang_group_statement_type group_statement
;
439 lang_input_section_type input_section
;
440 lang_input_statement_type input_statement
;
441 lang_insert_statement_type insert_statement
;
442 lang_output_section_statement_type output_section_statement
;
443 lang_output_statement_type output_statement
;
444 lang_padding_statement_type padding_statement
;
445 lang_reloc_statement_type reloc_statement
;
446 lang_target_statement_type target_statement
;
447 lang_wild_statement_type wild_statement
;
448 } lang_statement_union_type
;
450 /* This structure holds information about a program header, from the
451 PHDRS command in the linker script. */
455 struct lang_phdr
*next
;
464 /* This structure is used to hold a list of sections which may not
465 cross reference each other. */
467 typedef struct lang_nocrossref
469 struct lang_nocrossref
*next
;
471 } lang_nocrossref_type
;
473 /* The list of nocrossref lists. */
475 struct lang_nocrossrefs
477 struct lang_nocrossrefs
*next
;
478 lang_nocrossref_type
*list
;
482 /* This structure is used to hold a list of input section names which
483 will not match an output section in the linker script. */
485 struct unique_sections
487 struct unique_sections
*next
;
491 /* Used by place_orphan to keep track of orphan sections and statements. */
497 lang_output_section_statement_type
*os
;
499 lang_statement_union_type
**stmt
;
500 lang_output_section_statement_type
**os_tail
;
503 struct asneeded_minfo
505 struct asneeded_minfo
*next
;
511 extern struct lang_phdr
*lang_phdr_list
;
512 extern struct lang_nocrossrefs
*nocrossref_list
;
513 extern const char *output_target
;
514 extern lang_output_section_statement_type
*abs_output_section
;
515 extern lang_statement_list_type lang_os_list
;
516 extern struct lang_input_statement_flags input_flags
;
517 extern bool lang_has_input_file
;
518 extern lang_statement_list_type statement_list
;
519 extern lang_statement_list_type
*stat_ptr
;
520 extern bool delete_output_file_on_failure
;
522 extern struct bfd_sym_chain entry_symbol
;
523 extern const char *entry_section
;
524 extern bool entry_from_cmdline
;
525 extern lang_statement_list_type file_chain
;
526 extern lang_statement_list_type input_file_chain
;
528 extern struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
530 extern int lang_statement_iteration
;
531 extern struct asneeded_minfo
**asneeded_list_tail
;
533 extern void (*output_bfd_hash_table_free_fn
) (struct bfd_link_hash_table
*);
535 extern void lang_init
537 extern void lang_finish
539 extern lang_memory_region_type
* lang_memory_region_lookup
540 (const char * const, bool);
541 extern void lang_memory_region_alias
542 (const char *, const char *);
545 extern void lang_set_flags
546 (lang_memory_region_type
*, const char *, int);
547 extern void lang_add_output
548 (const char *, int from_script
);
549 extern lang_output_section_statement_type
*lang_enter_output_section_statement
550 (const char *, etree_type
*, enum section_type
, etree_type
*, etree_type
*,
551 etree_type
*, etree_type
*, int, int);
552 extern void lang_final
554 extern void lang_relax_sections
556 extern void lang_process
558 extern void lang_section_start
559 (const char *, union etree_union
*, const segment_type
*);
560 extern void lang_add_entry
561 (const char *, bool);
562 extern void lang_default_entry
564 extern void lang_add_target
566 extern void lang_add_wild
567 (struct wildcard_spec
*, struct wildcard_list
*, bool);
568 extern void lang_add_map
570 extern void lang_add_fill
572 extern lang_assignment_statement_type
*lang_add_assignment
573 (union etree_union
*);
574 extern void lang_add_attribute
575 (enum statement_enum
);
576 extern void lang_startup
578 extern void lang_float
580 extern void lang_leave_output_section_statement
581 (fill_type
*, const char *, lang_output_section_phdr_list
*,
583 extern void lang_for_each_input_file
584 (void (*dothis
) (lang_input_statement_type
*));
585 extern void lang_for_each_file
586 (void (*dothis
) (lang_input_statement_type
*));
587 extern void lang_reset_memory_regions
589 extern void lang_do_assignments
591 extern asection
*section_for_dot
594 #define LANG_FOR_EACH_INPUT_STATEMENT(statement) \
595 lang_input_statement_type *statement; \
596 for (statement = (lang_input_statement_type *) file_chain.head; \
598 statement = statement->next)
600 #define lang_output_section_find(NAME) \
601 lang_output_section_statement_lookup (NAME, 0, 0)
603 extern void lang_process
605 extern void ldlang_add_file
606 (lang_input_statement_type
*);
607 extern lang_output_section_statement_type
*lang_output_section_find_by_flags
608 (const asection
*, flagword
, lang_output_section_statement_type
**,
609 lang_match_sec_type_func
);
610 extern lang_output_section_statement_type
*lang_insert_orphan
611 (asection
*, const char *, int, lang_output_section_statement_type
*,
612 struct orphan_save
*, etree_type
*, lang_statement_list_type
*);
613 extern lang_input_statement_type
*lang_add_input_file
614 (const char *, lang_input_file_enum_type
, const char *);
615 extern void lang_add_keepsyms_file
617 extern lang_output_section_statement_type
*lang_output_section_get
619 extern lang_output_section_statement_type
*lang_output_section_statement_lookup
620 (const char *, int, int);
621 extern lang_output_section_statement_type
*next_matching_output_section_statement
622 (lang_output_section_statement_type
*, int);
623 extern void ldlang_add_undef
624 (const char *const, bool);
625 extern void ldlang_add_require_defined
627 extern void lang_add_output_format
628 (const char *, const char *, const char *, int);
629 extern void lang_list_init
630 (lang_statement_list_type
*);
631 extern void push_stat_ptr
632 (lang_statement_list_type
*);
633 extern void pop_stat_ptr
635 extern void lang_add_data
636 (int type
, union etree_union
*);
637 extern void lang_add_reloc
638 (bfd_reloc_code_real_type
, reloc_howto_type
*, asection
*, const char *,
639 union etree_union
*);
640 extern void lang_for_each_statement
641 (void (*) (lang_statement_union_type
*));
642 extern void lang_for_each_statement_worker
643 (void (*) (lang_statement_union_type
*), lang_statement_union_type
*);
644 extern void *stat_alloc
646 extern void strip_excluded_output_sections
648 extern void lang_clear_os_map
650 extern void dprint_statement
651 (lang_statement_union_type
*, int);
652 extern void lang_size_sections
654 extern void one_lang_size_sections_pass
656 extern void lang_add_insert
658 extern void lang_enter_group
660 extern void lang_leave_group
662 extern void lang_add_section
663 (lang_statement_list_type
*, asection
*, struct wildcard_list
*,
664 struct flag_info
*, lang_output_section_statement_type
*);
665 extern void lang_new_phdr
666 (const char *, etree_type
*, bool, bool, etree_type
*,
668 extern void lang_add_nocrossref
669 (lang_nocrossref_type
*);
670 extern void lang_add_nocrossref_to
671 (lang_nocrossref_type
*);
672 extern void lang_enter_overlay
673 (etree_type
*, etree_type
*);
674 extern void lang_enter_overlay_section
676 extern void lang_leave_overlay_section
677 (fill_type
*, lang_output_section_phdr_list
*);
678 extern void lang_leave_overlay
679 (etree_type
*, int, fill_type
*, const char *,
680 lang_output_section_phdr_list
*, const char *);
682 extern struct bfd_elf_version_expr
*lang_new_vers_pattern
683 (struct bfd_elf_version_expr
*, const char *, const char *, bool);
684 extern struct bfd_elf_version_tree
*lang_new_vers_node
685 (struct bfd_elf_version_expr
*, struct bfd_elf_version_expr
*);
686 extern struct bfd_elf_version_deps
*lang_add_vers_depend
687 (struct bfd_elf_version_deps
*, const char *);
688 extern void lang_register_vers_node
689 (const char *, struct bfd_elf_version_tree
*, struct bfd_elf_version_deps
*);
690 extern void lang_append_dynamic_list (struct bfd_elf_dynamic_list
**,
691 struct bfd_elf_version_expr
*);
692 extern void lang_append_dynamic_list_cpp_typeinfo (void);
693 extern void lang_append_dynamic_list_cpp_new (void);
694 extern void lang_add_unique
696 extern const char *lang_get_output_target
698 extern void add_excluded_libs (const char *);
699 extern bool load_symbols
700 (lang_input_statement_type
*, lang_statement_list_type
*);
702 struct elf_sym_strtab
;
703 struct elf_strtab_hash
;
704 extern void ldlang_ctf_acquire_strings
705 (struct elf_strtab_hash
*);
706 extern void ldlang_ctf_new_dynsym
707 (int symidx
, struct elf_internal_sym
*);
708 extern void ldlang_write_ctf_late
711 ldlang_override_segment_assignment
712 (struct bfd_link_info
*, bfd
*, asection
*, asection
*, bool);
715 lang_ld_feature (char *);
718 lang_print_memory_usage (void);
721 lang_add_gc_name (const char *);
724 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
);