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_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
92 typedef struct lang_statement_header_struct
94 /* Next pointer for statement_list statement list. */
95 union lang_statement_union
*next
;
96 enum statement_enum type
;
97 } lang_statement_header_type
;
101 lang_statement_header_type header
;
102 union etree_union
*exp
;
103 } lang_assignment_statement_type
;
105 typedef struct lang_target_statement_struct
107 lang_statement_header_type header
;
109 } lang_target_statement_type
;
111 typedef struct lang_output_statement_struct
113 lang_statement_header_type header
;
115 } lang_output_statement_type
;
117 /* Section types specified in a linker script. */
122 first_overlay_section
,
128 typed_readonly_section
131 /* This structure holds a list of program headers describing
132 segments in which this section should be placed. */
134 typedef struct lang_output_section_phdr_list
136 struct lang_output_section_phdr_list
*next
;
139 } lang_output_section_phdr_list
;
141 typedef struct lang_output_section_statement_struct
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
;
148 asection
*bfd_section
;
149 lang_memory_region_type
*region
;
150 lang_memory_region_type
*lma_region
;
152 union etree_union
*addr_tree
;
153 union etree_union
*load_base
;
154 union etree_union
*section_alignment
;
155 union etree_union
*subsection_alignment
;
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
;
163 lang_output_section_phdr_list
*phdrs
;
165 /* Used by ELF SHF_LINK_ORDER sorting. */
168 unsigned int block_value
;
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
;
191 lang_statement_header_type header
;
194 asection
*output_section
;
195 } lang_fill_statement_type
;
199 lang_statement_header_type header
;
201 union etree_union
*exp
;
203 asection
*output_section
;
204 bfd_vma output_offset
;
205 } lang_data_statement_type
;
207 /* Generate a reloc in the output file. */
211 lang_statement_header_type header
;
213 /* Reloc to generate. */
214 bfd_reloc_code_real_type reloc
;
216 /* Reloc howto structure. */
217 reloc_howto_type
*howto
;
219 /* Section to generate reloc against.
220 Exactly one of section and name must be NULL. */
223 /* Name of symbol to generate reloc against.
224 Exactly one of section and name must be NULL. */
227 /* Expression for addend. */
228 union etree_union
*addend_exp
;
230 /* Resolved addend. */
231 bfd_vma addend_value
;
233 /* Output section where reloc should be performed. */
234 asection
*output_section
;
236 /* Offset within output section. */
237 bfd_vma output_offset
;
238 } lang_reloc_statement_type
;
240 struct lang_input_statement_flags
242 /* 1 means this file was specified in a -l option. */
243 unsigned int maybe_archive
: 1;
245 /* 1 means this file was specified in a -l:namespec option. */
246 unsigned int full_name_provided
: 1;
248 /* 1 means search a set of directories for this file. */
249 unsigned int search_dirs
: 1;
251 /* 1 means this was found when processing a script in the sysroot. */
252 unsigned int sysrooted
: 1;
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;
259 /* Whether to search for this entry as a dynamic archive. */
260 unsigned int dynamic
: 1;
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;
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;
271 /* Whether to include the entire contents of an archive. */
272 unsigned int whole_archive
: 1;
274 /* Set when bfd opening is successful. */
275 unsigned int loaded
: 1;
277 unsigned int real
: 1;
279 /* Set if the file does not exist. */
280 unsigned int missing_file
: 1;
282 /* Set if reloading an archive or --as-needed lib. */
283 unsigned int reload
: 1;
285 #if BFD_SUPPORTS_PLUGINS
286 /* Set if the file was claimed by a plugin. */
287 unsigned int claimed
: 1;
289 /* Set if the file was claimed from an archive. */
290 unsigned int claim_archive
: 1;
292 /* Set if added by the lto plugin add_input_file callback. */
293 unsigned int lto_output
: 1;
294 #endif /* BFD_SUPPORTS_PLUGINS */
296 /* Head of list of pushed flags. */
297 struct lang_input_statement_flags
*pushed
;
300 typedef struct lang_input_statement_struct
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
;
317 ctf_archive_t
*the_ctf
;
319 struct flag_info
*section_flag_list
;
321 /* Next pointer for file_chain statement list. */
322 struct lang_input_statement_struct
*next
;
324 /* Next pointer for input_file_chain statement list. */
325 struct lang_input_statement_struct
*next_real_file
;
329 struct lang_input_statement_flags flags
;
330 } lang_input_statement_type
;
334 lang_statement_header_type header
;
337 } lang_input_section_type
;
341 lang_statement_header_type header
;
344 lang_input_statement_type
*input_stmt
;
345 } lang_input_matcher_type
;
347 struct map_symbol_def
{
348 struct bfd_link_hash_entry
*entry
;
349 struct map_symbol_def
*next
;
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
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
;
362 bfd_input_just_syms (const bfd
*abfd
)
364 lang_input_statement_type
*is
= bfd_usrdata (abfd
);
365 return is
!= NULL
&& is
->flags
.just_syms
;
368 typedef struct lang_wild_statement_struct lang_wild_statement_type
;
370 typedef void (*callback_t
) (lang_wild_statement_type
*, struct wildcard_list
*,
371 asection
*, lang_input_statement_type
*, void *);
373 typedef void (*walk_wild_section_handler_t
) (lang_wild_statement_type
*,
374 lang_input_statement_type
*,
378 typedef bool (*lang_match_sec_type_func
) (bfd
*, const asection
*,
379 bfd
*, const asection
*);
381 /* Binary search tree structure to efficiently sort sections by
383 typedef struct lang_section_bst
387 struct lang_section_bst
*left
;
388 struct lang_section_bst
*right
;
389 } lang_section_bst_type
;
391 struct lang_wild_statement_struct
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
;
399 lang_statement_list_type children
;
400 struct name_list
*exclude_name_list
;
401 lang_statement_list_type matching_sections
;
403 lang_section_bst_type
*tree
, **rightmost
;
404 struct flag_info
*section_flag_list
;
407 typedef struct lang_address_statement_struct
409 lang_statement_header_type header
;
410 const char *section_name
;
411 union etree_union
*address
;
412 const segment_type
*segment
;
413 } lang_address_statement_type
;
417 lang_statement_header_type header
;
418 bfd_vma output_offset
;
420 asection
*output_section
;
422 } lang_padding_statement_type
;
424 /* A group statement collects a set of libraries together. The
425 libraries are searched multiple times, until no new undefined
426 symbols are found. The effect is to search a group of libraries as
427 though they were a single library. */
431 lang_statement_header_type header
;
432 lang_statement_list_type children
;
433 } lang_group_statement_type
;
437 lang_statement_header_type header
;
440 } lang_insert_statement_type
;
442 typedef union lang_statement_union
444 lang_statement_header_type header
;
445 lang_address_statement_type address_statement
;
446 lang_assignment_statement_type assignment_statement
;
447 lang_data_statement_type data_statement
;
448 lang_fill_statement_type fill_statement
;
449 lang_group_statement_type group_statement
;
450 lang_input_section_type input_section
;
451 lang_input_matcher_type input_matcher
;
452 lang_input_statement_type input_statement
;
453 lang_insert_statement_type insert_statement
;
454 lang_output_section_statement_type output_section_statement
;
455 lang_output_statement_type output_statement
;
456 lang_padding_statement_type padding_statement
;
457 lang_reloc_statement_type reloc_statement
;
458 lang_target_statement_type target_statement
;
459 lang_wild_statement_type wild_statement
;
460 } lang_statement_union_type
;
462 /* This structure holds information about a program header, from the
463 PHDRS command in the linker script. */
467 struct lang_phdr
*next
;
476 /* This structure is used to hold a list of sections which may not
477 cross reference each other. */
479 typedef struct lang_nocrossref
481 struct lang_nocrossref
*next
;
483 } lang_nocrossref_type
;
485 /* The list of nocrossref lists. */
487 struct lang_nocrossrefs
489 struct lang_nocrossrefs
*next
;
490 lang_nocrossref_type
*list
;
494 /* This structure is used to hold a list of input section names which
495 will not match an output section in the linker script. */
497 struct unique_sections
499 struct unique_sections
*next
;
503 /* Used by place_orphan to keep track of orphan sections and statements. */
509 lang_output_section_statement_type
*os
;
511 lang_statement_union_type
**stmt
;
512 lang_output_section_statement_type
**os_tail
;
515 struct asneeded_minfo
517 struct asneeded_minfo
*next
;
523 extern struct lang_phdr
*lang_phdr_list
;
524 extern struct lang_nocrossrefs
*nocrossref_list
;
525 extern const char *output_target
;
526 extern lang_output_section_statement_type
*abs_output_section
;
527 extern lang_statement_list_type lang_os_list
;
528 extern struct lang_input_statement_flags input_flags
;
529 extern bool lang_has_input_file
;
530 extern lang_statement_list_type statement_list
;
531 extern lang_statement_list_type
*stat_ptr
;
532 extern bool delete_output_file_on_failure
;
534 extern struct bfd_sym_chain entry_symbol
;
535 extern const char *entry_section
;
536 extern bool entry_from_cmdline
;
537 extern lang_statement_list_type file_chain
;
538 extern lang_statement_list_type input_file_chain
;
540 extern struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
542 extern int lang_statement_iteration
;
543 extern struct asneeded_minfo
**asneeded_list_tail
;
545 extern void (*output_bfd_hash_table_free_fn
) (struct bfd_link_hash_table
*);
547 extern void lang_init
549 extern void lang_finish
551 extern lang_memory_region_type
* lang_memory_region_lookup
552 (const char * const, bool);
553 extern void lang_memory_region_alias
554 (const char *, const char *);
557 extern void lang_set_flags
558 (lang_memory_region_type
*, const char *, int);
559 extern void lang_add_output
560 (const char *, int from_script
);
561 extern lang_output_section_statement_type
*lang_enter_output_section_statement
562 (const char *, etree_type
*, enum section_type
, etree_type
*, etree_type
*,
563 etree_type
*, etree_type
*, int, int);
564 extern void lang_final
566 extern void lang_relax_sections
568 extern void lang_process
570 extern void lang_section_start
571 (const char *, union etree_union
*, const segment_type
*);
572 extern void lang_add_entry
573 (const char *, bool);
574 extern void lang_default_entry
576 extern void lang_add_target
578 extern void lang_add_wild
579 (struct wildcard_spec
*, struct wildcard_list
*, bool);
580 extern void lang_add_map
582 extern void lang_add_fill
584 extern lang_assignment_statement_type
*lang_add_assignment
585 (union etree_union
*);
586 extern void lang_add_attribute
587 (enum statement_enum
);
588 extern void lang_startup
590 extern void lang_float
592 extern void lang_leave_output_section_statement
593 (fill_type
*, const char *, lang_output_section_phdr_list
*,
595 extern void lang_for_each_input_file
596 (void (*dothis
) (lang_input_statement_type
*));
597 extern void lang_for_each_file
598 (void (*dothis
) (lang_input_statement_type
*));
599 extern void lang_reset_memory_regions
601 extern void lang_do_assignments
603 extern asection
*section_for_dot
606 #define LANG_FOR_EACH_INPUT_STATEMENT(statement) \
607 lang_input_statement_type *statement; \
608 for (statement = (lang_input_statement_type *) file_chain.head; \
610 statement = statement->next)
612 #define lang_output_section_find(NAME) \
613 lang_output_section_statement_lookup (NAME, 0, 0)
615 extern void lang_process
617 extern void ldlang_add_file
618 (lang_input_statement_type
*);
619 extern lang_output_section_statement_type
*lang_output_section_find_by_flags
620 (const asection
*, flagword
, lang_output_section_statement_type
**,
621 lang_match_sec_type_func
);
622 extern lang_output_section_statement_type
*lang_insert_orphan
623 (asection
*, const char *, int, lang_output_section_statement_type
*,
624 struct orphan_save
*, etree_type
*, lang_statement_list_type
*);
625 extern lang_input_statement_type
*lang_add_input_file
626 (const char *, lang_input_file_enum_type
, const char *);
627 extern void lang_add_keepsyms_file
629 extern lang_output_section_statement_type
*lang_output_section_get
631 extern lang_output_section_statement_type
*lang_output_section_statement_lookup
632 (const char *, int, int);
633 extern lang_output_section_statement_type
*next_matching_output_section_statement
634 (lang_output_section_statement_type
*, int);
635 extern void ldlang_add_undef
636 (const char *const, bool);
637 extern void ldlang_add_require_defined
639 extern void lang_add_output_format
640 (const char *, const char *, const char *, int);
641 extern void lang_list_init
642 (lang_statement_list_type
*);
643 extern void push_stat_ptr
644 (lang_statement_list_type
*);
645 extern void pop_stat_ptr
647 extern void lang_add_data
648 (int type
, union etree_union
*);
649 extern void lang_add_reloc
650 (bfd_reloc_code_real_type
, reloc_howto_type
*, asection
*, const char *,
651 union etree_union
*);
652 extern void lang_for_each_statement
653 (void (*) (lang_statement_union_type
*));
654 extern void lang_for_each_statement_worker
655 (void (*) (lang_statement_union_type
*), lang_statement_union_type
*);
656 extern void *stat_alloc
658 extern void strip_excluded_output_sections
660 extern void lang_clear_os_map
662 extern void dprint_statement
663 (lang_statement_union_type
*, int);
664 extern void lang_size_sections
666 extern void one_lang_size_sections_pass
668 extern void lang_add_insert
670 extern void lang_enter_group
672 extern void lang_leave_group
674 extern void lang_add_section
675 (lang_statement_list_type
*, asection
*, struct wildcard_list
*,
676 struct flag_info
*, lang_output_section_statement_type
*);
677 extern void lang_new_phdr
678 (const char *, etree_type
*, bool, bool, etree_type
*,
680 extern void lang_add_nocrossref
681 (lang_nocrossref_type
*);
682 extern void lang_add_nocrossref_to
683 (lang_nocrossref_type
*);
684 extern void lang_enter_overlay
685 (etree_type
*, etree_type
*);
686 extern void lang_enter_overlay_section
688 extern void lang_leave_overlay_section
689 (fill_type
*, lang_output_section_phdr_list
*);
690 extern void lang_leave_overlay
691 (etree_type
*, int, fill_type
*, const char *,
692 lang_output_section_phdr_list
*, const char *);
694 extern struct bfd_elf_version_expr
*lang_new_vers_pattern
695 (struct bfd_elf_version_expr
*, const char *, const char *, bool);
696 extern struct bfd_elf_version_tree
*lang_new_vers_node
697 (struct bfd_elf_version_expr
*, struct bfd_elf_version_expr
*);
698 extern struct bfd_elf_version_deps
*lang_add_vers_depend
699 (struct bfd_elf_version_deps
*, const char *);
700 extern void lang_register_vers_node
701 (const char *, struct bfd_elf_version_tree
*, struct bfd_elf_version_deps
*);
702 extern void lang_append_dynamic_list (struct bfd_elf_dynamic_list
**,
703 struct bfd_elf_version_expr
*);
704 extern void lang_append_dynamic_list_cpp_typeinfo (void);
705 extern void lang_append_dynamic_list_cpp_new (void);
706 extern void lang_add_unique
708 extern const char *lang_get_output_target
710 extern void add_excluded_libs (const char *);
711 extern bool load_symbols
712 (lang_input_statement_type
*, lang_statement_list_type
*);
714 struct elf_sym_strtab
;
715 struct elf_strtab_hash
;
716 extern void ldlang_ctf_acquire_strings
717 (struct elf_strtab_hash
*);
718 extern void ldlang_ctf_new_dynsym
719 (int symidx
, struct elf_internal_sym
*);
720 extern void ldlang_write_ctf_late
723 ldlang_override_segment_assignment
724 (struct bfd_link_info
*, bfd
*, asection
*, asection
*, bool);
727 lang_ld_feature (char *);
730 lang_print_memory_usage (void);
733 lang_add_gc_name (const char *);
736 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
);