xref: /netbsd-src/external/gpl3/binutils/dist/ld/ldlang.h (revision dd7241df2fae9da4ea2bd20a68f001fa86ecf909)
1 /* ldlang.h - linker command language support
2    Copyright (C) 1991-2024 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
bfd_input_just_syms(const bfd * abfd)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   lang_statement_list_type    children;
395   lang_statement_list_type    matching_sections;
396   lang_section_bst_type *     tree;
397   lang_section_bst_type **    rightmost;
398   struct wildcard_list *      section_list;
399   struct name_list *          exclude_name_list;
400   struct flag_info *          section_flag_list;
401   const char *                filename;
402   bool                        filenames_sorted;
403   bool                        filenames_reversed;
404   bool                        any_specs_sorted;
405   bool                        keep_sections;
406 };
407 
408 typedef struct lang_address_statement_struct
409 {
410   lang_statement_header_type header;
411   const char *section_name;
412   union etree_union *address;
413   const segment_type *segment;
414 } lang_address_statement_type;
415 
416 typedef struct
417 {
418   lang_statement_header_type header;
419   bfd_vma output_offset;
420   bfd_size_type size;
421   asection *output_section;
422   fill_type *fill;
423 } lang_padding_statement_type;
424 
425 /* A group statement collects a set of libraries together.  The
426    libraries are searched multiple times, until no new undefined
427    symbols are found.  The effect is to search a group of libraries as
428    though they were a single library.  */
429 
430 typedef struct
431 {
432   lang_statement_header_type header;
433   lang_statement_list_type children;
434 } lang_group_statement_type;
435 
436 typedef struct
437 {
438   lang_statement_header_type header;
439   const char *where;
440   bool is_before;
441 } lang_insert_statement_type;
442 
443 typedef union lang_statement_union
444 {
445   lang_statement_header_type header;
446   lang_address_statement_type address_statement;
447   lang_assignment_statement_type assignment_statement;
448   lang_data_statement_type data_statement;
449   lang_fill_statement_type fill_statement;
450   lang_group_statement_type group_statement;
451   lang_input_section_type input_section;
452   lang_input_matcher_type input_matcher;
453   lang_input_statement_type input_statement;
454   lang_insert_statement_type insert_statement;
455   lang_output_section_statement_type output_section_statement;
456   lang_output_statement_type output_statement;
457   lang_padding_statement_type padding_statement;
458   lang_reloc_statement_type reloc_statement;
459   lang_target_statement_type target_statement;
460   lang_wild_statement_type wild_statement;
461 } lang_statement_union_type;
462 
463 /* This structure holds information about a program header, from the
464    PHDRS command in the linker script.  */
465 
466 struct lang_phdr
467 {
468   struct lang_phdr *next;
469   const char *name;
470   unsigned long type;
471   bool filehdr;
472   bool phdrs;
473   etree_type *at;
474   etree_type *flags;
475 };
476 
477 /* This structure is used to hold a list of sections which may not
478    cross reference each other.  */
479 
480 typedef struct lang_nocrossref
481 {
482   struct lang_nocrossref *next;
483   const char *name;
484 } lang_nocrossref_type;
485 
486 /* The list of nocrossref lists.  */
487 
488 struct lang_nocrossrefs
489 {
490   struct lang_nocrossrefs *next;
491   lang_nocrossref_type *list;
492   bool onlyfirst;
493 };
494 
495 /* This structure is used to hold a list of input section names which
496    will not match an output section in the linker script.  */
497 
498 struct unique_sections
499 {
500   struct unique_sections *next;
501   const char *name;
502 };
503 
504 /* Used by place_orphan to keep track of orphan sections and statements.  */
505 
506 struct orphan_save
507 {
508   const char *name;
509   flagword flags;
510   lang_output_section_statement_type *os;
511   asection **section;
512   lang_statement_union_type **stmt;
513   lang_output_section_statement_type **os_tail;
514 };
515 
516 struct asneeded_minfo
517 {
518   struct asneeded_minfo *next;
519   const char *soname;
520   bfd *ref;
521   const char *name;
522 };
523 
524 extern struct lang_phdr *lang_phdr_list;
525 extern struct lang_nocrossrefs *nocrossref_list;
526 extern const char *output_target;
527 extern lang_output_section_statement_type *abs_output_section;
528 extern lang_statement_list_type lang_os_list;
529 extern struct lang_input_statement_flags input_flags;
530 extern bool lang_has_input_file;
531 extern lang_statement_list_type statement_list;
532 extern lang_statement_list_type *stat_ptr;
533 extern bool delete_output_file_on_failure;
534 extern bool enable_linker_version;
535 
536 extern struct bfd_sym_chain entry_symbol;
537 extern const char *entry_section;
538 extern bool entry_from_cmdline;
539 extern lang_statement_list_type file_chain;
540 extern lang_statement_list_type input_file_chain;
541 
542 extern struct bfd_elf_dynamic_list **current_dynamic_list_p;
543 
544 extern int lang_statement_iteration;
545 extern struct asneeded_minfo **asneeded_list_tail;
546 
547 extern void (*output_bfd_hash_table_free_fn) (struct bfd_link_hash_table *);
548 
549 extern void lang_init
550   (void);
551 extern void lang_finish
552   (void);
553 extern lang_memory_region_type * lang_memory_region_lookup
554   (const char * const, bool);
555 extern void lang_memory_region_alias
556   (const char *, const char *);
557 extern void lang_map
558   (void);
559 extern void lang_set_flags
560   (lang_memory_region_type *, const char *, int);
561 extern void lang_add_output
562   (const char *, int from_script);
563 extern lang_output_section_statement_type *lang_enter_output_section_statement
564   (const char *, etree_type *, enum section_type, etree_type *, etree_type *,
565    etree_type *, etree_type *, int, int);
566 extern void lang_final
567   (void);
568 extern void lang_relax_sections
569   (bool);
570 extern void lang_process
571   (void);
572 extern void lang_section_start
573   (const char *, union etree_union *, const segment_type *);
574 extern void lang_add_entry
575   (const char *, bool);
576 extern void lang_default_entry
577   (const char *);
578 extern void lang_add_target
579   (const char *);
580 extern void lang_add_wild
581   (struct wildcard_spec *, struct wildcard_list *, bool);
582 extern void lang_add_map
583   (const char *);
584 extern void lang_add_fill
585   (fill_type *);
586 extern lang_assignment_statement_type *lang_add_assignment
587   (union etree_union *);
588 extern void lang_add_attribute
589   (enum statement_enum);
590 extern void lang_startup
591   (const char *);
592 extern void lang_float
593   (bool);
594 extern void lang_leave_output_section_statement
595   (fill_type *, const char *, lang_output_section_phdr_list *,
596    const char *);
597 extern void lang_for_each_input_file
598   (void (*dothis) (lang_input_statement_type *));
599 extern void lang_for_each_file
600   (void (*dothis) (lang_input_statement_type *));
601 extern void lang_reset_memory_regions
602   (void);
603 extern void lang_do_assignments
604   (lang_phase_type);
605 extern asection *section_for_dot
606   (void);
607 
608 #define LANG_FOR_EACH_INPUT_STATEMENT(statement)			\
609   lang_input_statement_type *statement;					\
610   for (statement = (lang_input_statement_type *) file_chain.head;	\
611        statement != NULL;						\
612        statement = statement->next)
613 
614 #define lang_output_section_find(NAME) \
615   lang_output_section_statement_lookup (NAME, 0, 0)
616 
617 extern void lang_process
618   (void);
619 extern void ldlang_add_file
620   (lang_input_statement_type *);
621 extern lang_output_section_statement_type *lang_output_section_find_by_flags
622   (const asection *, flagword, lang_output_section_statement_type **,
623    lang_match_sec_type_func);
624 extern lang_output_section_statement_type *lang_insert_orphan
625   (asection *, const char *, int, lang_output_section_statement_type *,
626    struct orphan_save *, etree_type *, lang_statement_list_type *);
627 extern lang_input_statement_type *lang_add_input_file
628   (const char *, lang_input_file_enum_type, const char *);
629 extern void lang_add_keepsyms_file
630   (const char *);
631 extern lang_output_section_statement_type *lang_output_section_get
632   (const asection *);
633 extern lang_output_section_statement_type *lang_output_section_statement_lookup
634   (const char *, int, int);
635 extern lang_output_section_statement_type *next_matching_output_section_statement
636   (lang_output_section_statement_type *, int);
637 extern void ldlang_add_undef
638   (const char *const, bool);
639 extern void ldlang_add_require_defined
640   (const char *const);
641 extern void lang_add_output_format
642   (const char *, const char *, const char *, int);
643 extern void lang_list_init
644   (lang_statement_list_type *);
645 extern void push_stat_ptr
646   (lang_statement_list_type *);
647 extern void pop_stat_ptr
648   (void);
649 extern void lang_add_data
650   (int, union etree_union *);
651 extern void lang_add_string
652   (const char *);
653 extern void lang_add_reloc
654   (bfd_reloc_code_real_type, reloc_howto_type *, asection *, const char *,
655    union etree_union *);
656 extern void lang_for_each_statement
657   (void (*) (lang_statement_union_type *));
658 extern void lang_for_each_statement_worker
659   (void (*) (lang_statement_union_type *), lang_statement_union_type *);
660 extern void *stat_alloc
661   (size_t);
662 extern void strip_excluded_output_sections
663   (void);
664 extern void lang_clear_os_map
665   (void);
666 extern void dprint_statement
667   (lang_statement_union_type *, int);
668 extern void lang_size_sections
669   (bool *, bool);
670 extern void one_lang_size_sections_pass
671   (bool *, bool);
672 extern void lang_add_insert
673   (const char *, int);
674 extern void lang_enter_group
675   (void);
676 extern void lang_leave_group
677   (void);
678 extern void lang_add_section
679   (lang_statement_list_type *, asection *, struct wildcard_list *,
680    struct flag_info *, lang_output_section_statement_type *);
681 extern void lang_new_phdr
682   (const char *, etree_type *, bool, bool, etree_type *,
683    etree_type *);
684 extern void lang_add_nocrossref
685   (lang_nocrossref_type *);
686 extern void lang_add_nocrossref_to
687   (lang_nocrossref_type *);
688 extern void lang_enter_overlay
689   (etree_type *, etree_type *);
690 extern void lang_enter_overlay_section
691   (const char *);
692 extern void lang_leave_overlay_section
693   (fill_type *, lang_output_section_phdr_list *);
694 extern void lang_leave_overlay
695   (etree_type *, int, fill_type *, const char *,
696    lang_output_section_phdr_list *, const char *);
697 
698 extern struct bfd_elf_version_expr *lang_new_vers_pattern
699   (struct bfd_elf_version_expr *, const char *, const char *, bool);
700 extern struct bfd_elf_version_tree *lang_new_vers_node
701   (struct bfd_elf_version_expr *, struct bfd_elf_version_expr *);
702 extern struct bfd_elf_version_deps *lang_add_vers_depend
703   (struct bfd_elf_version_deps *, const char *);
704 extern void lang_register_vers_node
705   (const char *, struct bfd_elf_version_tree *, struct bfd_elf_version_deps *);
706 extern void lang_append_dynamic_list (struct bfd_elf_dynamic_list **,
707 				      struct bfd_elf_version_expr *);
708 extern void lang_append_dynamic_list_cpp_typeinfo (void);
709 extern void lang_append_dynamic_list_cpp_new (void);
710 extern void lang_add_unique
711   (const char *);
712 extern const char *lang_get_output_target
713   (void);
714 extern void add_excluded_libs (const char *);
715 extern bool load_symbols
716   (lang_input_statement_type *, lang_statement_list_type *);
717 
718 struct elf_sym_strtab;
719 struct elf_strtab_hash;
720 extern void ldlang_ctf_acquire_strings
721   (struct elf_strtab_hash *);
722 extern void ldlang_ctf_new_dynsym
723   (int symidx, struct elf_internal_sym *);
724 extern void ldlang_write_ctf_late
725   (void);
726 extern bool
727 ldlang_override_segment_assignment
728   (struct bfd_link_info *, bfd *, asection *, asection *, bool);
729 
730 extern void
731 lang_ld_feature (char *);
732 
733 extern void
734 lang_print_memory_usage (void);
735 
736 extern void
737 lang_add_gc_name (const char *);
738 
739 extern bool
740 print_one_symbol (struct bfd_link_hash_entry *, void *);
741 
742 extern void lang_add_version_string
743   (void);
744 #endif
745