xref: /netbsd-src/external/gpl3/binutils.old/dist/ld/ldlang.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
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_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
89 };
90 
91 typedef struct lang_statement_header_struct
92 {
93   /* Next pointer for statement_list statement list.  */
94   union lang_statement_union *next;
95   enum statement_enum type;
96 } lang_statement_header_type;
97 
98 typedef struct
99 {
100   lang_statement_header_type header;
101   union etree_union *exp;
102 } lang_assignment_statement_type;
103 
104 typedef struct lang_target_statement_struct
105 {
106   lang_statement_header_type header;
107   const char *target;
108 } lang_target_statement_type;
109 
110 typedef struct lang_output_statement_struct
111 {
112   lang_statement_header_type header;
113   const char *name;
114 } lang_output_statement_type;
115 
116 /* Section types specified in a linker script.  */
117 
118 enum section_type
119 {
120   normal_section,
121   first_overlay_section,
122   overlay_section,
123   noload_section,
124   noalloc_section,
125   type_section,
126   readonly_section,
127   typed_readonly_section
128 };
129 
130 /* This structure holds a list of program headers describing
131    segments in which this section should be placed.  */
132 
133 typedef struct lang_output_section_phdr_list
134 {
135   struct lang_output_section_phdr_list *next;
136   const char *name;
137   bool used;
138 } lang_output_section_phdr_list;
139 
140 typedef struct lang_output_section_statement_struct
141 {
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;
146   const char *name;
147   asection *bfd_section;
148   lang_memory_region_type *region;
149   lang_memory_region_type *lma_region;
150   fill_type *fill;
151   union etree_union *addr_tree;
152   union etree_union *load_base;
153   union etree_union *section_alignment;
154   union etree_union *subsection_alignment;
155 
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;
161 
162   lang_output_section_phdr_list *phdrs;
163 
164   /* Used by ELF SHF_LINK_ORDER sorting.  */
165   void *data;
166 
167   unsigned int block_value;
168   int constraint;
169   flagword flags;
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;
187 
188 typedef struct
189 {
190   lang_statement_header_type header;
191   fill_type *fill;
192   int size;
193   asection *output_section;
194 } lang_fill_statement_type;
195 
196 typedef struct
197 {
198   lang_statement_header_type header;
199   unsigned int type;
200   union etree_union *exp;
201   bfd_vma value;
202   asection *output_section;
203   bfd_vma output_offset;
204 } lang_data_statement_type;
205 
206 /* Generate a reloc in the output file.  */
207 
208 typedef struct
209 {
210   lang_statement_header_type header;
211 
212   /* Reloc to generate.  */
213   bfd_reloc_code_real_type reloc;
214 
215   /* Reloc howto structure.  */
216   reloc_howto_type *howto;
217 
218   /* Section to generate reloc against.
219      Exactly one of section and name must be NULL.  */
220   asection *section;
221 
222   /* Name of symbol to generate reloc against.
223      Exactly one of section and name must be NULL.  */
224   const char *name;
225 
226   /* Expression for addend.  */
227   union etree_union *addend_exp;
228 
229   /* Resolved addend.  */
230   bfd_vma addend_value;
231 
232   /* Output section where reloc should be performed.  */
233   asection *output_section;
234 
235   /* Offset within output section.  */
236   bfd_vma output_offset;
237 } lang_reloc_statement_type;
238 
239 struct lang_input_statement_flags
240 {
241   /* 1 means this file was specified in a -l option.  */
242   unsigned int maybe_archive : 1;
243 
244   /* 1 means this file was specified in a -l:namespec option.  */
245   unsigned int full_name_provided : 1;
246 
247   /* 1 means search a set of directories for this file.  */
248   unsigned int search_dirs : 1;
249 
250   /* 1 means this was found when processing a script in the sysroot.  */
251   unsigned int sysrooted : 1;
252 
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;
257 
258   /* Whether to search for this entry as a dynamic archive.  */
259   unsigned int dynamic : 1;
260 
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;
265 
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;
269 
270   /* Whether to include the entire contents of an archive.  */
271   unsigned int whole_archive : 1;
272 
273   /* Set when bfd opening is successful.  */
274   unsigned int loaded : 1;
275 
276   unsigned int real : 1;
277 
278   /* Set if the file does not exist.  */
279   unsigned int missing_file : 1;
280 
281   /* Set if reloading an archive or --as-needed lib.  */
282   unsigned int reload : 1;
283 
284 #if BFD_SUPPORTS_PLUGINS
285   /* Set if the file was claimed by a plugin.  */
286   unsigned int claimed : 1;
287 
288   /* Set if the file was claimed from an archive.  */
289   unsigned int claim_archive : 1;
290 
291   /* Set if added by the lto plugin add_input_file callback.  */
292   unsigned int lto_output : 1;
293 #endif /* BFD_SUPPORTS_PLUGINS */
294 
295   /* Head of list of pushed flags.  */
296   struct lang_input_statement_flags *pushed;
297 };
298 
299 typedef struct lang_input_statement_struct
300 {
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;
311 
312   bfd *the_bfd;
313 
314   ctf_archive_t *the_ctf;
315 
316   struct flag_info *section_flag_list;
317 
318   /* Next pointer for file_chain statement list.  */
319   struct lang_input_statement_struct *next;
320 
321   /* Next pointer for input_file_chain statement list.  */
322   struct lang_input_statement_struct *next_real_file;
323 
324   const char *target;
325 
326   struct lang_input_statement_flags flags;
327 } lang_input_statement_type;
328 
329 typedef struct
330 {
331   lang_statement_header_type header;
332   asection *section;
333   void *pattern;
334 } lang_input_section_type;
335 
336 struct map_symbol_def {
337   struct bfd_link_hash_entry *entry;
338   struct map_symbol_def *next;
339 };
340 
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
344 {
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;
349 
350 static inline bool
bfd_input_just_syms(const bfd * abfd)351 bfd_input_just_syms (const bfd *abfd)
352 {
353   lang_input_statement_type *is = bfd_usrdata (abfd);
354   return is != NULL && is->flags.just_syms;
355 }
356 
357 typedef struct lang_wild_statement_struct lang_wild_statement_type;
358 
359 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
360 			    asection *, lang_input_statement_type *, void *);
361 
362 typedef void (*walk_wild_section_handler_t) (lang_wild_statement_type *,
363 					     lang_input_statement_type *,
364 					     callback_t callback,
365 					     void *data);
366 
367 typedef bool (*lang_match_sec_type_func) (bfd *, const asection *,
368 					  bfd *, const asection *);
369 
370 /* Binary search tree structure to efficiently sort sections by
371    name.  */
372 typedef struct lang_section_bst
373 {
374   asection *section;
375   void *pattern;
376   struct lang_section_bst *left;
377   struct lang_section_bst *right;
378 } lang_section_bst_type;
379 
380 struct lang_wild_statement_struct
381 {
382   lang_statement_header_type header;
383   const char *filename;
384   bool filenames_sorted;
385   struct wildcard_list *section_list;
386   bool keep_sections;
387   lang_statement_list_type children;
388   struct name_list *exclude_name_list;
389 
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;
394 };
395 
396 typedef struct lang_address_statement_struct
397 {
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;
403 
404 typedef struct
405 {
406   lang_statement_header_type header;
407   bfd_vma output_offset;
408   bfd_size_type size;
409   asection *output_section;
410   fill_type *fill;
411 } lang_padding_statement_type;
412 
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.  */
417 
418 typedef struct
419 {
420   lang_statement_header_type header;
421   lang_statement_list_type children;
422 } lang_group_statement_type;
423 
424 typedef struct
425 {
426   lang_statement_header_type header;
427   const char *where;
428   bool is_before;
429 } lang_insert_statement_type;
430 
431 typedef union lang_statement_union
432 {
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;
449 
450 /* This structure holds information about a program header, from the
451    PHDRS command in the linker script.  */
452 
453 struct lang_phdr
454 {
455   struct lang_phdr *next;
456   const char *name;
457   unsigned long type;
458   bool filehdr;
459   bool phdrs;
460   etree_type *at;
461   etree_type *flags;
462 };
463 
464 /* This structure is used to hold a list of sections which may not
465    cross reference each other.  */
466 
467 typedef struct lang_nocrossref
468 {
469   struct lang_nocrossref *next;
470   const char *name;
471 } lang_nocrossref_type;
472 
473 /* The list of nocrossref lists.  */
474 
475 struct lang_nocrossrefs
476 {
477   struct lang_nocrossrefs *next;
478   lang_nocrossref_type *list;
479   bool onlyfirst;
480 };
481 
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.  */
484 
485 struct unique_sections
486 {
487   struct unique_sections *next;
488   const char *name;
489 };
490 
491 /* Used by place_orphan to keep track of orphan sections and statements.  */
492 
493 struct orphan_save
494 {
495   const char *name;
496   flagword flags;
497   lang_output_section_statement_type *os;
498   asection **section;
499   lang_statement_union_type **stmt;
500   lang_output_section_statement_type **os_tail;
501 };
502 
503 struct asneeded_minfo
504 {
505   struct asneeded_minfo *next;
506   const char *soname;
507   bfd *ref;
508   const char *name;
509 };
510 
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;
521 
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;
527 
528 extern struct bfd_elf_dynamic_list **current_dynamic_list_p;
529 
530 extern int lang_statement_iteration;
531 extern struct asneeded_minfo **asneeded_list_tail;
532 
533 extern void (*output_bfd_hash_table_free_fn) (struct bfd_link_hash_table *);
534 
535 extern void lang_init
536   (void);
537 extern void lang_finish
538   (void);
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 *);
543 extern void lang_map
544   (void);
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
553   (void);
554 extern void lang_relax_sections
555   (bool);
556 extern void lang_process
557   (void);
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
563   (const char *);
564 extern void lang_add_target
565   (const char *);
566 extern void lang_add_wild
567   (struct wildcard_spec *, struct wildcard_list *, bool);
568 extern void lang_add_map
569   (const char *);
570 extern void lang_add_fill
571   (fill_type *);
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
577   (const char *);
578 extern void lang_float
579   (bool);
580 extern void lang_leave_output_section_statement
581   (fill_type *, const char *, lang_output_section_phdr_list *,
582    const char *);
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
588   (void);
589 extern void lang_do_assignments
590   (lang_phase_type);
591 extern asection *section_for_dot
592   (void);
593 
594 #define LANG_FOR_EACH_INPUT_STATEMENT(statement)			\
595   lang_input_statement_type *statement;					\
596   for (statement = (lang_input_statement_type *) file_chain.head;	\
597        statement != NULL;						\
598        statement = statement->next)
599 
600 #define lang_output_section_find(NAME) \
601   lang_output_section_statement_lookup (NAME, 0, 0)
602 
603 extern void lang_process
604   (void);
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
616   (const char *);
617 extern lang_output_section_statement_type *lang_output_section_get
618   (const asection *);
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
626   (const char *const);
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
634   (void);
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
645   (size_t);
646 extern void strip_excluded_output_sections
647   (void);
648 extern void lang_clear_os_map
649   (void);
650 extern void dprint_statement
651   (lang_statement_union_type *, int);
652 extern void lang_size_sections
653   (bool *, bool);
654 extern void one_lang_size_sections_pass
655   (bool *, bool);
656 extern void lang_add_insert
657   (const char *, int);
658 extern void lang_enter_group
659   (void);
660 extern void lang_leave_group
661   (void);
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 *,
667    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
675   (const char *);
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 *);
681 
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
695   (const char *);
696 extern const char *lang_get_output_target
697   (void);
698 extern void add_excluded_libs (const char *);
699 extern bool load_symbols
700   (lang_input_statement_type *, lang_statement_list_type *);
701 
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
709   (void);
710 extern bool
711 ldlang_override_segment_assignment
712   (struct bfd_link_info *, bfd *, asection *, asection *, bool);
713 
714 extern void
715 lang_ld_feature (char *);
716 
717 extern void
718 lang_print_memory_usage (void);
719 
720 extern void
721 lang_add_gc_name (const char *);
722 
723 extern bool
724 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr);
725 
726 #endif
727