1/* ldlang.h - linker command language support
2   Copyright (C) 1991-2020 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
26typedef enum
27{
28  lang_input_file_is_l_enum,
29  lang_input_file_is_symbols_only_enum,
30  lang_input_file_is_marker_enum,
31  lang_input_file_is_fake_enum,
32  lang_input_file_is_search_file_enum,
33  lang_input_file_is_file_enum
34} lang_input_file_enum_type;
35
36struct _fill_type
37{
38  size_t size;
39  unsigned char data[1];
40};
41
42typedef struct statement_list
43{
44  union lang_statement_union *  head;
45  union lang_statement_union ** tail;
46} lang_statement_list_type;
47
48typedef struct memory_region_name_struct
49{
50  const char * name;
51  struct memory_region_name_struct * next;
52} lang_memory_region_name;
53
54typedef struct memory_region_struct
55{
56  lang_memory_region_name name_list;
57  struct memory_region_struct *next;
58  union etree_union *origin_exp;
59  bfd_vma origin;
60  bfd_size_type length;
61  union etree_union *length_exp;
62  bfd_vma current;
63  union lang_statement_union *last_os;
64  flagword flags;
65  flagword not_flags;
66  bfd_boolean had_full_message;
67} lang_memory_region_type;
68
69enum statement_enum
70{
71  lang_address_statement_enum,
72  lang_assignment_statement_enum,
73  lang_data_statement_enum,
74  lang_fill_statement_enum,
75  lang_group_statement_enum,
76  lang_input_section_enum,
77  lang_input_statement_enum,
78  lang_insert_statement_enum,
79  lang_output_section_statement_enum,
80  lang_output_statement_enum,
81  lang_padding_statement_enum,
82  lang_reloc_statement_enum,
83  lang_target_statement_enum,
84  lang_wild_statement_enum,
85  lang_constructors_statement_enum,
86  lang_object_symbols_statement_enum
87};
88
89typedef struct lang_statement_header_struct
90{
91  /* Next pointer for statement_list statement list.  */
92  union lang_statement_union *next;
93  enum statement_enum type;
94} lang_statement_header_type;
95
96typedef struct
97{
98  lang_statement_header_type header;
99  union etree_union *exp;
100} lang_assignment_statement_type;
101
102typedef struct lang_target_statement_struct
103{
104  lang_statement_header_type header;
105  const char *target;
106} lang_target_statement_type;
107
108typedef struct lang_output_statement_struct
109{
110  lang_statement_header_type header;
111  const char *name;
112} lang_output_statement_type;
113
114/* Section types specified in a linker script.  */
115
116enum section_type
117{
118  normal_section,
119  first_overlay_section,
120  overlay_section,
121  noload_section,
122  noalloc_section
123};
124
125/* This structure holds a list of program headers describing
126   segments in which this section should be placed.  */
127
128typedef struct lang_output_section_phdr_list
129{
130  struct lang_output_section_phdr_list *next;
131  const char *name;
132  bfd_boolean used;
133} lang_output_section_phdr_list;
134
135typedef struct lang_output_section_statement_struct
136{
137  lang_statement_header_type header;
138  lang_statement_list_type children;
139  struct lang_output_section_statement_struct *next;
140  struct lang_output_section_statement_struct *prev;
141  const char *name;
142  asection *bfd_section;
143  lang_memory_region_type *region;
144  lang_memory_region_type *lma_region;
145  fill_type *fill;
146  union etree_union *addr_tree;
147  union etree_union *load_base;
148  union etree_union *section_alignment;
149  union etree_union *subsection_alignment;
150
151  /* If non-null, an expression to evaluate after setting the section's
152     size.  The expression is evaluated inside REGION (above) with '.'
153     set to the end of the section.  Used in the last overlay section
154     to move '.' past all the overlaid sections.  */
155  union etree_union *update_dot_tree;
156
157  lang_output_section_phdr_list *phdrs;
158
159  unsigned int block_value;
160  int constraint;
161  flagword flags;
162  enum section_type sectype;
163  unsigned int processed_vma : 1;
164  unsigned int processed_lma : 1;
165  unsigned int all_input_readonly : 1;
166  /* If this section should be ignored.  */
167  unsigned int ignored : 1;
168  /* If this section should update "dot".  Prevents section being ignored.  */
169  unsigned int update_dot : 1;
170  /* If this section is after assignment to _end.  */
171  unsigned int after_end : 1;
172  /* If this section uses the alignment of its input sections.  */
173  unsigned int align_lma_with_input : 1;
174} lang_output_section_statement_type;
175
176typedef struct
177{
178  lang_statement_header_type header;
179  fill_type *fill;
180  int size;
181  asection *output_section;
182} lang_fill_statement_type;
183
184typedef struct
185{
186  lang_statement_header_type header;
187  unsigned int type;
188  union etree_union *exp;
189  bfd_vma value;
190  asection *output_section;
191  bfd_vma output_offset;
192} lang_data_statement_type;
193
194/* Generate a reloc in the output file.  */
195
196typedef struct
197{
198  lang_statement_header_type header;
199
200  /* Reloc to generate.  */
201  bfd_reloc_code_real_type reloc;
202
203  /* Reloc howto structure.  */
204  reloc_howto_type *howto;
205
206  /* Section to generate reloc against.
207     Exactly one of section and name must be NULL.  */
208  asection *section;
209
210  /* Name of symbol to generate reloc against.
211     Exactly one of section and name must be NULL.  */
212  const char *name;
213
214  /* Expression for addend.  */
215  union etree_union *addend_exp;
216
217  /* Resolved addend.  */
218  bfd_vma addend_value;
219
220  /* Output section where reloc should be performed.  */
221  asection *output_section;
222
223  /* Offset within output section.  */
224  bfd_vma output_offset;
225} lang_reloc_statement_type;
226
227struct lang_input_statement_flags
228{
229  /* 1 means this file was specified in a -l option.  */
230  unsigned int maybe_archive : 1;
231
232  /* 1 means this file was specified in a -l:namespec option.  */
233  unsigned int full_name_provided : 1;
234
235  /* 1 means search a set of directories for this file.  */
236  unsigned int search_dirs : 1;
237
238  /* 1 means this was found when processing a script in the sysroot.  */
239  unsigned int sysrooted : 1;
240
241  /* 1 means this is base file of incremental load.
242     Do not load this file's text or data.
243     Also default text_start to after this file's bss.  */
244  unsigned int just_syms : 1;
245
246  /* Whether to search for this entry as a dynamic archive.  */
247  unsigned int dynamic : 1;
248
249  /* Set if a DT_NEEDED tag should be added not just for the dynamic library
250     explicitly given by this entry but also for any dynamic libraries in
251     this entry's needed list.  */
252  unsigned int add_DT_NEEDED_for_dynamic : 1;
253
254  /* Set if this entry should cause a DT_NEEDED tag only when some
255     regular file references its symbols (ie. --as-needed is in effect).  */
256  unsigned int add_DT_NEEDED_for_regular : 1;
257
258  /* Whether to include the entire contents of an archive.  */
259  unsigned int whole_archive : 1;
260
261  /* Set when bfd opening is successful.  */
262  unsigned int loaded : 1;
263
264  unsigned int real : 1;
265
266  /* Set if the file does not exist.  */
267  unsigned int missing_file : 1;
268
269  /* Set if reloading an archive or --as-needed lib.  */
270  unsigned int reload : 1;
271
272#ifdef ENABLE_PLUGINS
273  /* Set if the file was claimed by a plugin.  */
274  unsigned int claimed : 1;
275
276  /* Set if the file was claimed from an archive.  */
277  unsigned int claim_archive : 1;
278
279  /* Set if added by the lto plugin add_input_file callback.  */
280  unsigned int lto_output : 1;
281#endif /* ENABLE_PLUGINS */
282
283  /* Head of list of pushed flags.  */
284  struct lang_input_statement_flags *pushed;
285};
286
287typedef struct lang_input_statement_struct
288{
289  lang_statement_header_type header;
290  /* Name of this file.  */
291  const char *filename;
292  /* Name to use for the symbol giving address of text start.
293     Usually the same as filename, but for a file spec'd with
294     -l this is the -l switch itself rather than the filename.  */
295  const char *local_sym_name;
296
297  bfd *the_bfd;
298
299  ctf_archive_t *the_ctf;
300
301  struct flag_info *section_flag_list;
302
303  /* Next pointer for file_chain statement list.  */
304  struct lang_input_statement_struct *next;
305
306  /* Next pointer for input_file_chain statement list.  */
307  struct lang_input_statement_struct *next_real_file;
308
309  const char *target;
310
311  struct lang_input_statement_flags flags;
312} lang_input_statement_type;
313
314typedef struct
315{
316  lang_statement_header_type header;
317  asection *section;
318} lang_input_section_type;
319
320struct map_symbol_def {
321  struct bfd_link_hash_entry *entry;
322  struct map_symbol_def *next;
323};
324
325/* For input sections, when writing a map file: head / tail of a linked
326   list of hash table entries for symbols defined in this section.  */
327typedef struct input_section_userdata_struct
328{
329  struct map_symbol_def *map_symbol_def_head;
330  struct map_symbol_def **map_symbol_def_tail;
331  unsigned long map_symbol_def_count;
332} input_section_userdata_type;
333
334static inline bfd_boolean
335bfd_input_just_syms (const bfd *abfd)
336{
337  lang_input_statement_type *is = bfd_usrdata (abfd);
338  return is != NULL && is->flags.just_syms;
339}
340
341typedef struct lang_wild_statement_struct lang_wild_statement_type;
342
343typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
344			    asection *, struct flag_info *,
345			    lang_input_statement_type *, void *);
346
347typedef void (*walk_wild_section_handler_t) (lang_wild_statement_type *,
348					     lang_input_statement_type *,
349					     callback_t callback,
350					     void *data);
351
352typedef bfd_boolean (*lang_match_sec_type_func) (bfd *, const asection *,
353						 bfd *, const asection *);
354
355/* Binary search tree structure to efficiently sort sections by
356   name.  */
357typedef struct lang_section_bst
358{
359  asection *section;
360  struct lang_section_bst *left;
361  struct lang_section_bst *right;
362} lang_section_bst_type;
363
364struct lang_wild_statement_struct
365{
366  lang_statement_header_type header;
367  const char *filename;
368  bfd_boolean filenames_sorted;
369  struct wildcard_list *section_list;
370  bfd_boolean keep_sections;
371  lang_statement_list_type children;
372  struct name_list *exclude_name_list;
373
374  walk_wild_section_handler_t walk_wild_section_handler;
375  struct wildcard_list *handler_data[4];
376  lang_section_bst_type *tree;
377  struct flag_info *section_flag_list;
378};
379
380typedef struct lang_address_statement_struct
381{
382  lang_statement_header_type header;
383  const char *section_name;
384  union etree_union *address;
385  const segment_type *segment;
386} lang_address_statement_type;
387
388typedef struct
389{
390  lang_statement_header_type header;
391  bfd_vma output_offset;
392  bfd_size_type size;
393  asection *output_section;
394  fill_type *fill;
395} lang_padding_statement_type;
396
397/* A group statement collects a set of libraries together.  The
398   libraries are searched multiple times, until no new undefined
399   symbols are found.  The effect is to search a group of libraries as
400   though they were a single library.  */
401
402typedef struct
403{
404  lang_statement_header_type header;
405  lang_statement_list_type children;
406} lang_group_statement_type;
407
408typedef struct
409{
410  lang_statement_header_type header;
411  const char *where;
412  bfd_boolean is_before;
413} lang_insert_statement_type;
414
415typedef union lang_statement_union
416{
417  lang_statement_header_type header;
418  lang_address_statement_type address_statement;
419  lang_assignment_statement_type assignment_statement;
420  lang_data_statement_type data_statement;
421  lang_fill_statement_type fill_statement;
422  lang_group_statement_type group_statement;
423  lang_input_section_type input_section;
424  lang_input_statement_type input_statement;
425  lang_insert_statement_type insert_statement;
426  lang_output_section_statement_type output_section_statement;
427  lang_output_statement_type output_statement;
428  lang_padding_statement_type padding_statement;
429  lang_reloc_statement_type reloc_statement;
430  lang_target_statement_type target_statement;
431  lang_wild_statement_type wild_statement;
432} lang_statement_union_type;
433
434/* This structure holds information about a program header, from the
435   PHDRS command in the linker script.  */
436
437struct lang_phdr
438{
439  struct lang_phdr *next;
440  const char *name;
441  unsigned long type;
442  bfd_boolean filehdr;
443  bfd_boolean phdrs;
444  etree_type *at;
445  etree_type *flags;
446};
447
448/* This structure is used to hold a list of sections which may not
449   cross reference each other.  */
450
451typedef struct lang_nocrossref
452{
453  struct lang_nocrossref *next;
454  const char *name;
455} lang_nocrossref_type;
456
457/* The list of nocrossref lists.  */
458
459struct lang_nocrossrefs
460{
461  struct lang_nocrossrefs *next;
462  lang_nocrossref_type *list;
463  bfd_boolean onlyfirst;
464};
465
466/* This structure is used to hold a list of input section names which
467   will not match an output section in the linker script.  */
468
469struct unique_sections
470{
471  struct unique_sections *next;
472  const char *name;
473};
474
475/* Used by place_orphan to keep track of orphan sections and statements.  */
476
477struct orphan_save
478{
479  const char *name;
480  flagword flags;
481  lang_output_section_statement_type *os;
482  asection **section;
483  lang_statement_union_type **stmt;
484  lang_output_section_statement_type **os_tail;
485};
486
487struct asneeded_minfo
488{
489  struct asneeded_minfo *next;
490  const char *soname;
491  bfd *ref;
492  const char *name;
493};
494
495extern struct lang_phdr *lang_phdr_list;
496extern struct lang_nocrossrefs *nocrossref_list;
497extern const char *output_target;
498extern lang_output_section_statement_type *abs_output_section;
499extern lang_statement_list_type lang_os_list;
500extern struct lang_input_statement_flags input_flags;
501extern bfd_boolean lang_has_input_file;
502extern lang_statement_list_type *stat_ptr;
503extern bfd_boolean delete_output_file_on_failure;
504
505extern struct bfd_sym_chain entry_symbol;
506extern const char *entry_section;
507extern bfd_boolean entry_from_cmdline;
508extern lang_statement_list_type file_chain;
509extern lang_statement_list_type input_file_chain;
510
511extern int lang_statement_iteration;
512extern struct asneeded_minfo **asneeded_list_tail;
513
514extern void (*output_bfd_hash_table_free_fn) (struct bfd_link_hash_table *);
515
516extern void lang_init
517  (void);
518extern void lang_finish
519  (void);
520extern lang_memory_region_type * lang_memory_region_lookup
521  (const char * const, bfd_boolean);
522extern void lang_memory_region_alias
523  (const char *, const char *);
524extern void lang_map
525  (void);
526extern void lang_set_flags
527  (lang_memory_region_type *, const char *, int);
528extern void lang_add_output
529  (const char *, int from_script);
530extern lang_output_section_statement_type *lang_enter_output_section_statement
531  (const char *, etree_type *, enum section_type, etree_type *, etree_type *,
532   etree_type *, int, int);
533extern void lang_final
534  (void);
535extern void lang_relax_sections
536  (bfd_boolean);
537extern void lang_process
538  (void);
539extern void lang_section_start
540  (const char *, union etree_union *, const segment_type *);
541extern void lang_add_entry
542  (const char *, bfd_boolean);
543extern void lang_default_entry
544  (const char *);
545extern void lang_add_target
546  (const char *);
547extern void lang_add_wild
548  (struct wildcard_spec *, struct wildcard_list *, bfd_boolean);
549extern void lang_add_map
550  (const char *);
551extern void lang_add_fill
552  (fill_type *);
553extern lang_assignment_statement_type *lang_add_assignment
554  (union etree_union *);
555extern void lang_add_attribute
556  (enum statement_enum);
557extern void lang_startup
558  (const char *);
559extern void lang_float
560  (bfd_boolean);
561extern void lang_leave_output_section_statement
562  (fill_type *, const char *, lang_output_section_phdr_list *,
563   const char *);
564extern void lang_for_each_input_file
565  (void (*dothis) (lang_input_statement_type *));
566extern void lang_for_each_file
567  (void (*dothis) (lang_input_statement_type *));
568extern void lang_reset_memory_regions
569  (void);
570extern void lang_do_assignments
571  (lang_phase_type);
572extern asection *section_for_dot
573  (void);
574
575#define LANG_FOR_EACH_INPUT_STATEMENT(statement)			\
576  lang_input_statement_type *statement;					\
577  for (statement = (lang_input_statement_type *) file_chain.head;	\
578       statement != NULL;						\
579       statement = statement->next)
580
581#define lang_output_section_find(NAME) \
582  lang_output_section_statement_lookup (NAME, 0, FALSE)
583
584extern void lang_process
585  (void);
586extern void ldlang_add_file
587  (lang_input_statement_type *);
588extern lang_output_section_statement_type *lang_output_section_find_by_flags
589  (const asection *, flagword, lang_output_section_statement_type **,
590   lang_match_sec_type_func);
591extern lang_output_section_statement_type *lang_insert_orphan
592  (asection *, const char *, int, lang_output_section_statement_type *,
593   struct orphan_save *, etree_type *, lang_statement_list_type *);
594extern lang_input_statement_type *lang_add_input_file
595  (const char *, lang_input_file_enum_type, const char *);
596extern void lang_add_keepsyms_file
597  (const char *);
598extern lang_output_section_statement_type *lang_output_section_get
599  (const asection *);
600extern lang_output_section_statement_type *lang_output_section_statement_lookup
601  (const char *, int, bfd_boolean);
602extern lang_output_section_statement_type *next_matching_output_section_statement
603  (lang_output_section_statement_type *, int);
604extern void ldlang_add_undef
605  (const char *const, bfd_boolean);
606extern void ldlang_add_require_defined
607  (const char *const);
608extern void lang_add_output_format
609  (const char *, const char *, const char *, int);
610extern void lang_list_init
611  (lang_statement_list_type *);
612extern void push_stat_ptr
613  (lang_statement_list_type *);
614extern void pop_stat_ptr
615  (void);
616extern void lang_add_data
617  (int type, union etree_union *);
618extern void lang_add_reloc
619  (bfd_reloc_code_real_type, reloc_howto_type *, asection *, const char *,
620   union etree_union *);
621extern void lang_for_each_statement
622  (void (*) (lang_statement_union_type *));
623extern void lang_for_each_statement_worker
624  (void (*) (lang_statement_union_type *), lang_statement_union_type *);
625extern void *stat_alloc
626  (size_t);
627extern void strip_excluded_output_sections
628  (void);
629extern void lang_clear_os_map
630  (void);
631extern void dprint_statement
632  (lang_statement_union_type *, int);
633extern void lang_size_sections
634  (bfd_boolean *, bfd_boolean);
635extern void one_lang_size_sections_pass
636  (bfd_boolean *, bfd_boolean);
637extern void lang_add_insert
638  (const char *, int);
639extern void lang_enter_group
640  (void);
641extern void lang_leave_group
642  (void);
643extern void lang_add_section
644  (lang_statement_list_type *, asection *,
645   struct flag_info *, lang_output_section_statement_type *);
646extern void lang_new_phdr
647  (const char *, etree_type *, bfd_boolean, bfd_boolean, etree_type *,
648   etree_type *);
649extern void lang_add_nocrossref
650  (lang_nocrossref_type *);
651extern void lang_add_nocrossref_to
652  (lang_nocrossref_type *);
653extern void lang_enter_overlay
654  (etree_type *, etree_type *);
655extern void lang_enter_overlay_section
656  (const char *);
657extern void lang_leave_overlay_section
658  (fill_type *, lang_output_section_phdr_list *);
659extern void lang_leave_overlay
660  (etree_type *, int, fill_type *, const char *,
661   lang_output_section_phdr_list *, const char *);
662
663extern struct bfd_elf_version_expr *lang_new_vers_pattern
664  (struct bfd_elf_version_expr *, const char *, const char *, bfd_boolean);
665extern struct bfd_elf_version_tree *lang_new_vers_node
666  (struct bfd_elf_version_expr *, struct bfd_elf_version_expr *);
667extern struct bfd_elf_version_deps *lang_add_vers_depend
668  (struct bfd_elf_version_deps *, const char *);
669extern void lang_register_vers_node
670  (const char *, struct bfd_elf_version_tree *, struct bfd_elf_version_deps *);
671extern void lang_append_dynamic_list (struct bfd_elf_version_expr *);
672extern void lang_append_dynamic_list_cpp_typeinfo (void);
673extern void lang_append_dynamic_list_cpp_new (void);
674extern void lang_add_unique
675  (const char *);
676extern const char *lang_get_output_target
677  (void);
678extern void add_excluded_libs (const char *);
679extern bfd_boolean load_symbols
680  (lang_input_statement_type *, lang_statement_list_type *);
681
682struct elf_sym_strtab;
683struct elf_strtab_hash;
684extern void ldlang_ctf_apply_strsym
685  (struct elf_sym_strtab *, bfd_size_type, struct elf_strtab_hash *);
686extern void ldlang_write_ctf_late
687  (void);
688extern bfd_boolean
689ldlang_override_segment_assignment
690  (struct bfd_link_info *, bfd *, asection *, asection *, bfd_boolean);
691
692extern void
693lang_ld_feature (char *);
694
695extern void
696lang_print_memory_usage (void);
697
698extern void
699lang_add_gc_name (const char *);
700
701#endif
702