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
;
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
;
410 typedef struct lang_address_statement_struct
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
;
420 lang_statement_header_type header
;
421 bfd_vma output_offset
;
423 asection
*output_section
;
425 } lang_padding_statement_type
;
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. */
434 lang_statement_header_type header
;
435 lang_statement_list_type children
;
436 } lang_group_statement_type
;
440 lang_statement_header_type header
;
443 } lang_insert_statement_type
;
445 typedef union lang_statement_union
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
;
465 /* This structure holds information about a program header, from the
466 PHDRS command in the linker script. */
470 struct lang_phdr
*next
;
479 /* This structure is used to hold a list of sections which may not
480 cross reference each other. */
482 typedef struct lang_nocrossref
484 struct lang_nocrossref
*next
;
486 } lang_nocrossref_type
;
488 /* The list of nocrossref lists. */
490 struct lang_nocrossrefs
492 struct lang_nocrossrefs
*next
;
493 lang_nocrossref_type
*list
;
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. */
500 struct unique_sections
502 struct unique_sections
*next
;
506 /* Used by place_orphan to keep track of orphan sections and statements. */
512 lang_output_section_statement_type
*os
;
514 lang_statement_union_type
**stmt
;
515 lang_output_section_statement_type
**os_tail
;
518 struct asneeded_minfo
520 struct asneeded_minfo
*next
;
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
;
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
;
543 extern struct bfd_elf_dynamic_list
**current_dynamic_list_p
;
545 extern int lang_statement_iteration
;
546 extern struct asneeded_minfo
**asneeded_list_tail
;
548 extern void (*output_bfd_hash_table_free_fn
) (struct bfd_link_hash_table
*);
550 extern void lang_init
552 extern void lang_finish
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 *);
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
569 extern void lang_relax_sections
571 extern void lang_process
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
579 extern void lang_add_target
581 extern void lang_add_wild
582 (struct wildcard_spec
*, struct wildcard_list
*, bool);
583 extern void lang_add_map
585 extern void lang_add_fill
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
593 extern void lang_float
595 extern void lang_leave_output_section_statement
596 (fill_type
*, const char *, lang_output_section_phdr_list
*,
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
604 extern void lang_do_assignments
606 extern asection
*section_for_dot
609 #define LANG_FOR_EACH_INPUT_STATEMENT(statement) \
610 lang_input_statement_type *statement; \
611 for (statement = (lang_input_statement_type *) file_chain.head; \
613 statement = statement->next)
615 #define lang_output_section_find(NAME) \
616 lang_output_section_statement_lookup (NAME, 0, 0)
618 extern void lang_process
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
632 extern lang_output_section_statement_type
*lang_output_section_get
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
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
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
661 extern void strip_excluded_output_sections
663 extern void lang_clear_os_map
665 extern void dprint_statement
666 (lang_statement_union_type
*, int);
667 extern void lang_size_sections
669 extern void one_lang_size_sections_pass
671 extern void lang_add_insert
673 extern void lang_enter_group
675 extern void lang_leave_group
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
*,
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
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 *);
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
711 extern const char *lang_get_output_target
713 extern void add_excluded_libs (const char *);
714 extern bool load_symbols
715 (lang_input_statement_type
*, lang_statement_list_type
*);
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
726 ldlang_override_segment_assignment
727 (struct bfd_link_info
*, bfd
*, asection
*, asection
*, bool);
730 lang_ld_feature (char *);
733 lang_print_memory_usage (void);
736 lang_add_gc_name (const char *);
739 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
);