DIGEST: ldlang.*: add timestamp
[binutils-gdb.git] / ld / ldlang.c
1 /* Linker command language support.
2 Copyright (C) 1991-2023 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 #include "sysdep.h"
22 #include <limits.h>
23 #include <time.h>
24 #include "bfd.h"
25 #include "libiberty.h"
26 #include "filenames.h"
27 #include "safe-ctype.h"
28 #include "obstack.h"
29 #include "bfdlink.h"
30 #include "ctf-api.h"
31
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldexp.h"
35 #include "ldlang.h"
36 #include <ldgram.h>
37 #include "ldlex.h"
38 #include "ldmisc.h"
39 #include "ldctor.h"
40 #include "ldfile.h"
41 #include "ldemul.h"
42 #include "fnmatch.h"
43 #include "demangle.h"
44 #include "hashtab.h"
45 #include "elf-bfd.h"
46 #if BFD_SUPPORTS_PLUGINS
47 #include "plugin.h"
48 #endif /* BFD_SUPPORTS_PLUGINS */
49
50 #ifndef offsetof
51 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
52 #endif
53
54 /* Convert between addresses in bytes and sizes in octets.
55 For currently supported targets, octets_per_byte is always a power
56 of two, so we can use shifts. */
57 #define TO_ADDR(X) ((X) >> opb_shift)
58 #define TO_SIZE(X) ((X) << opb_shift)
59
60 /* Local variables. */
61 static struct obstack stat_obstack;
62 static struct obstack map_obstack;
63 static struct obstack pt_obstack;
64
65 #define obstack_chunk_alloc xmalloc
66 #define obstack_chunk_free free
67 static const char *entry_symbol_default = "start";
68 static bool map_head_is_link_order = false;
69 static lang_output_section_statement_type *default_common_section;
70 static bool map_option_f;
71 static bfd_vma print_dot;
72 static lang_input_statement_type *first_file;
73 static const char *current_target;
74 static lang_statement_list_type *stat_save[10];
75 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
76 static struct unique_sections *unique_section_list;
77 static struct asneeded_minfo *asneeded_list_head;
78 static unsigned int opb_shift = 0;
79
80 /* Forward declarations. */
81 static void exp_init_os (etree_type *);
82 static lang_input_statement_type *lookup_name (const char *);
83 static void insert_undefined (const char *);
84 static bool sort_def_symbol (struct bfd_link_hash_entry *, void *);
85 static lang_statement_union_type *new_statement (enum statement_enum type,
86 size_t size,
87 lang_statement_list_type *list);
88 static void print_statement (lang_statement_union_type *,
89 lang_output_section_statement_type *);
90 static void print_statement_list (lang_statement_union_type *,
91 lang_output_section_statement_type *);
92 static void print_statements (void);
93 static void print_input_section (asection *, bool);
94 static bool lang_one_common (struct bfd_link_hash_entry *, void *);
95 static void lang_record_phdrs (void);
96 static void lang_do_version_exports_section (void);
97 static void lang_finalize_version_expr_head
98 (struct bfd_elf_version_expr_head *);
99 static void lang_do_memory_regions (bool);
100
101 /* Exported variables. */
102 const char *output_target;
103 lang_output_section_statement_type *abs_output_section;
104 /* Header for list of statements corresponding to any files involved in the
105 link, either specified from the command-line or added implicitely (eg.
106 archive member used to resolved undefined symbol, wildcard statement from
107 linker script, etc.). Next pointer is in next field of a
108 lang_statement_header_type (reached via header field in a
109 lang_statement_union). */
110 lang_statement_list_type statement_list;
111 lang_statement_list_type lang_os_list;
112 lang_statement_list_type *stat_ptr = &statement_list;
113 /* Header for list of statements corresponding to files used in the final
114 executable. This can be either object file specified on the command-line
115 or library member resolving an undefined reference. Next pointer is in next
116 field of a lang_input_statement_type (reached via input_statement field in a
117 lang_statement_union). */
118 lang_statement_list_type file_chain = { NULL, NULL };
119 /* Header for list of statements corresponding to files specified on the
120 command-line for linking. It thus contains real object files and archive
121 but not archive members. Next pointer is in next_real_file field of a
122 lang_input_statement_type statement (reached via input_statement field in a
123 lang_statement_union). */
124 lang_statement_list_type input_file_chain;
125 static const char *current_input_file;
126 struct bfd_elf_dynamic_list **current_dynamic_list_p;
127 struct bfd_sym_chain entry_symbol = { NULL, NULL };
128 const char *entry_section = ".text";
129 struct lang_input_statement_flags input_flags;
130 bool entry_from_cmdline;
131 bool lang_has_input_file = false;
132 bool had_output_filename = false;
133 bool lang_float_flag = false;
134 bool delete_output_file_on_failure = false;
135 struct lang_phdr *lang_phdr_list;
136 struct lang_nocrossrefs *nocrossref_list;
137 struct asneeded_minfo **asneeded_list_tail;
138 #ifdef ENABLE_LIBCTF
139 static ctf_dict_t *ctf_output;
140 #endif
141
142 /* Functions that traverse the linker script and might evaluate
143 DEFINED() need to increment this at the start of the traversal. */
144 int lang_statement_iteration = 0;
145
146 /* Count times through one_lang_size_sections_pass after mark phase. */
147 static int lang_sizing_iteration = 0;
148
149 /* Return TRUE if the PATTERN argument is a wildcard pattern.
150 Although backslashes are treated specially if a pattern contains
151 wildcards, we do not consider the mere presence of a backslash to
152 be enough to cause the pattern to be treated as a wildcard.
153 That lets us handle DOS filenames more naturally. */
154 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
155
156 #define new_stat(x, y) \
157 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
158
159 #define outside_section_address(q) \
160 ((q)->output_offset + (q)->output_section->vma)
161
162 #define outside_symbol_address(q) \
163 ((q)->value + outside_section_address (q->section))
164
165 /* CTF sections smaller than this are not compressed: compression of
166 dictionaries this small doesn't gain much, and this lets consumers mmap the
167 sections directly out of the ELF file and use them with no decompression
168 overhead if they want to. */
169 #define CTF_COMPRESSION_THRESHOLD 4096
170
171 void *
172 stat_alloc (size_t size)
173 {
174 return obstack_alloc (&stat_obstack, size);
175 }
176
177 /* Code for handling simple wildcards without going through fnmatch,
178 which can be expensive because of charset translations etc. */
179
180 /* A simple wild is a literal string followed by a single '*',
181 where the literal part is at least 4 characters long. */
182
183 static bool
184 is_simple_wild (const char *name)
185 {
186 size_t len = strcspn (name, "*?[");
187 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
188 }
189
190 static bool
191 match_simple_wild (const char *pattern, const char *name)
192 {
193 /* The first four characters of the pattern are guaranteed valid
194 non-wildcard characters. So we can go faster. */
195 if (pattern[0] != name[0] || pattern[1] != name[1]
196 || pattern[2] != name[2] || pattern[3] != name[3])
197 return false;
198
199 pattern += 4;
200 name += 4;
201 while (*pattern != '*')
202 if (*name++ != *pattern++)
203 return false;
204
205 return true;
206 }
207
208 static int
209 name_match (const char *pattern, const char *name)
210 {
211 if (is_simple_wild (pattern))
212 return !match_simple_wild (pattern, name);
213 if (wildcardp (pattern))
214 return fnmatch (pattern, name, 0);
215 return strcmp (pattern, name);
216 }
217
218 /* Given an analyzed wildcard_spec SPEC, match it against NAME,
219 returns zero on a match, non-zero if there's no match. */
220
221 static int
222 spec_match (const struct wildcard_spec *spec, const char *name)
223 {
224 size_t nl = spec->namelen;
225 size_t pl = spec->prefixlen;
226 size_t sl = spec->suffixlen;
227 size_t inputlen = strlen (name);
228 int r;
229
230 if (pl)
231 {
232 if (inputlen < pl)
233 return 1;
234
235 r = memcmp (spec->name, name, pl);
236 if (r)
237 return r;
238 }
239
240 if (sl)
241 {
242 if (inputlen < sl)
243 return 1;
244
245 r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl);
246 if (r)
247 return r;
248 }
249
250 if (nl == pl + sl + 1 && spec->name[pl] == '*')
251 return 0;
252
253 if (nl > pl)
254 return fnmatch (spec->name + pl, name + pl, 0);
255
256 if (inputlen >= nl)
257 return name[nl];
258
259 return 0;
260 }
261
262 static char *
263 ldirname (const char *name)
264 {
265 const char *base = lbasename (name);
266 char *dirname;
267
268 while (base > name && IS_DIR_SEPARATOR (base[-1]))
269 --base;
270 if (base == name)
271 return strdup (".");
272 dirname = strdup (name);
273 dirname[base - name] = '\0';
274 return dirname;
275 }
276
277 /* If PATTERN is of the form archive:file, return a pointer to the
278 separator. If not, return NULL. */
279
280 static char *
281 archive_path (const char *pattern)
282 {
283 char *p = NULL;
284
285 if (link_info.path_separator == 0)
286 return p;
287
288 p = strchr (pattern, link_info.path_separator);
289 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
290 if (p == NULL || link_info.path_separator != ':')
291 return p;
292
293 /* Assume a match on the second char is part of drive specifier,
294 as in "c:\silly.dos". */
295 if (p == pattern + 1 && ISALPHA (*pattern))
296 p = strchr (p + 1, link_info.path_separator);
297 #endif
298 return p;
299 }
300
301 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
302 return whether F matches FILE_SPEC. */
303
304 static bool
305 input_statement_is_archive_path (const char *file_spec, char *sep,
306 lang_input_statement_type *f)
307 {
308 bool match = false;
309
310 if ((*(sep + 1) == 0
311 || name_match (sep + 1, f->filename) == 0)
312 && ((sep != file_spec)
313 == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
314 {
315 match = true;
316
317 if (sep != file_spec)
318 {
319 const char *aname = bfd_get_filename (f->the_bfd->my_archive);
320 *sep = 0;
321 match = name_match (file_spec, aname) == 0;
322 *sep = link_info.path_separator;
323 }
324 }
325 return match;
326 }
327
328 static bool
329 unique_section_p (const asection *sec,
330 const lang_output_section_statement_type *os)
331 {
332 struct unique_sections *unam;
333 const char *secnam;
334
335 if (!link_info.resolve_section_groups
336 && sec->owner != NULL
337 && bfd_is_group_section (sec->owner, sec))
338 return !(os != NULL
339 && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
340
341 secnam = sec->name;
342 for (unam = unique_section_list; unam; unam = unam->next)
343 if (name_match (unam->name, secnam) == 0)
344 return true;
345
346 return false;
347 }
348
349 /* Generic traversal routines for finding matching sections. */
350
351 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
352 false. */
353
354 static bool
355 walk_wild_file_in_exclude_list (struct name_list *exclude_list,
356 lang_input_statement_type *file)
357 {
358 struct name_list *list_tmp;
359
360 for (list_tmp = exclude_list;
361 list_tmp;
362 list_tmp = list_tmp->next)
363 {
364 char *p = archive_path (list_tmp->name);
365
366 if (p != NULL)
367 {
368 if (input_statement_is_archive_path (list_tmp->name, p, file))
369 return true;
370 }
371
372 else if (name_match (list_tmp->name, file->filename) == 0)
373 return true;
374
375 /* FIXME: Perhaps remove the following at some stage? Matching
376 unadorned archives like this was never documented and has
377 been superceded by the archive:path syntax. */
378 else if (file->the_bfd != NULL
379 && file->the_bfd->my_archive != NULL
380 && name_match (list_tmp->name,
381 bfd_get_filename (file->the_bfd->my_archive)) == 0)
382 return true;
383 }
384
385 return false;
386 }
387
388 /* Add SECTION (from input FILE) to the list of matching sections
389 within PTR (the matching wildcard is SEC). */
390
391 static void
392 add_matching_section (lang_wild_statement_type *ptr,
393 struct wildcard_list *sec,
394 asection *section,
395 lang_input_statement_type *file)
396 {
397 lang_input_matcher_type *new_section;
398 /* Add a section reference to the list. */
399 new_section = new_stat (lang_input_matcher, &ptr->matching_sections);
400 new_section->section = section;
401 new_section->pattern = sec;
402 new_section->input_stmt = file;
403 }
404
405 /* Process section S (from input file FILE) in relation to wildcard
406 statement PTR. We already know that a prefix of the name of S matches
407 some wildcard in PTR's wildcard list. Here we check if the filename
408 matches as well (if it's specified) and if any of the wildcards in fact
409 does match. */
410
411 static void
412 walk_wild_section_match (lang_wild_statement_type *ptr,
413 lang_input_statement_type *file,
414 asection *s)
415 {
416 struct wildcard_list *sec;
417 const char *file_spec = ptr->filename;
418 char *p;
419
420 /* Check if filenames match. */
421 if (file_spec == NULL)
422 ;
423 else if ((p = archive_path (file_spec)) != NULL)
424 {
425 if (!input_statement_is_archive_path (file_spec, p, file))
426 return;
427 }
428 else if (wildcardp (file_spec))
429 {
430 if (fnmatch (file_spec, file->filename, 0) != 0)
431 return;
432 }
433 else
434 {
435 lang_input_statement_type *f;
436 /* Perform the iteration over a single file. */
437 f = lookup_name (file_spec);
438 if (f != file)
439 return;
440 }
441
442 /* If filename is excluded we're done. */
443 if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file))
444 return;
445
446 /* Check section name against each wildcard spec. If there's no
447 wildcard all sections match. */
448 sec = ptr->section_list;
449 if (sec == NULL)
450 add_matching_section (ptr, sec, s, file);
451 else
452 {
453 const char *sname = bfd_section_name (s);
454 for (; sec != NULL; sec = sec->next)
455 {
456 if (sec->spec.name != NULL
457 && spec_match (&sec->spec, sname) != 0)
458 continue;
459
460 /* Don't process sections from files which were excluded. */
461 if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list,
462 file))
463 add_matching_section (ptr, sec, s, file);
464 }
465 }
466 }
467
468 /* Return the numerical value of the init_priority attribute from
469 section name NAME. */
470
471 static int
472 get_init_priority (const asection *sec)
473 {
474 const char *name = bfd_section_name (sec);
475 const char *dot;
476
477 /* GCC uses the following section names for the init_priority
478 attribute with numerical values 101 to 65535 inclusive. A
479 lower value means a higher priority.
480
481 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
482 decimal numerical value of the init_priority attribute.
483 The order of execution in .init_array is forward and
484 .fini_array is backward.
485 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
486 decimal numerical value of the init_priority attribute.
487 The order of execution in .ctors is backward and .dtors
488 is forward.
489
490 .init_array.NNNNN sections would normally be placed in an output
491 .init_array section, .fini_array.NNNNN in .fini_array,
492 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
493 we should sort by increasing number (and could just use
494 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
495 being placed in .init_array (which may also contain
496 .init_array.NNNNN sections) or .dtors.NNNNN sections are being
497 placed in .fini_array then we need to extract the init_priority
498 attribute and sort on that. */
499 dot = strrchr (name, '.');
500 if (dot != NULL && ISDIGIT (dot[1]))
501 {
502 char *end;
503 unsigned long init_priority = strtoul (dot + 1, &end, 10);
504 if (*end == 0)
505 {
506 if (dot == name + 6
507 && (strncmp (name, ".ctors", 6) == 0
508 || strncmp (name, ".dtors", 6) == 0))
509 init_priority = 65535 - init_priority;
510 if (init_priority <= INT_MAX)
511 return init_priority;
512 }
513 }
514 return -1;
515 }
516
517 /* Compare sections ASEC and BSEC according to SORT. */
518
519 static int
520 compare_section (sort_type sort, asection *asec, asection *bsec)
521 {
522 int ret;
523 int a_priority, b_priority;
524
525 switch (sort)
526 {
527 default:
528 abort ();
529
530 case by_init_priority:
531 a_priority = get_init_priority (asec);
532 b_priority = get_init_priority (bsec);
533 if (a_priority < 0 || b_priority < 0)
534 goto sort_by_name;
535 ret = a_priority - b_priority;
536 if (ret)
537 break;
538 else
539 goto sort_by_name;
540
541 case by_alignment_name:
542 ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
543 if (ret)
544 break;
545 /* Fall through. */
546
547 case by_name:
548 sort_by_name:
549 ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
550 break;
551
552 case by_name_alignment:
553 ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
554 if (ret)
555 break;
556 /* Fall through. */
557
558 case by_alignment:
559 ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
560 break;
561 }
562
563 return ret;
564 }
565
566 /* PE puts the sort key in the input statement. */
567
568 static const char *
569 sort_filename (bfd *abfd)
570 {
571 lang_input_statement_type *is = bfd_usrdata (abfd);
572 if (is->sort_key)
573 return is->sort_key;
574 return bfd_get_filename (abfd);
575 }
576
577 /* Handle wildcard sorting. This returns the place in a binary search tree
578 where this FILE:SECTION should be inserted for wild statement WILD where
579 the spec SEC was the matching one. The tree is later linearized. */
580
581 static lang_section_bst_type **
582 wild_sort (lang_wild_statement_type *wild,
583 struct wildcard_list *sec,
584 lang_input_statement_type *file,
585 asection *section)
586 {
587 lang_section_bst_type **tree;
588
589 if (!wild->filenames_sorted
590 && (sec == NULL || sec->spec.sorted == none
591 || sec->spec.sorted == by_none))
592 {
593 /* We might be called even if _this_ spec doesn't need sorting,
594 in which case we simply append at the right end of tree. */
595 return wild->rightmost;
596 }
597
598 tree = &wild->tree;
599 while (*tree)
600 {
601 /* Sorting by filename takes precedence over sorting by section
602 name. */
603
604 if (wild->filenames_sorted)
605 {
606 const char *fn, *ln;
607 bool fa, la;
608 int i;
609 asection *lsec = (*tree)->section;
610
611 /* The PE support for the .idata section as generated by
612 dlltool assumes that files will be sorted by the name of
613 the archive and then the name of the file within the
614 archive. */
615
616 fa = file->the_bfd->my_archive != NULL;
617 if (fa)
618 fn = sort_filename (file->the_bfd->my_archive);
619 else
620 fn = sort_filename (file->the_bfd);
621
622 la = lsec->owner->my_archive != NULL;
623 if (la)
624 ln = sort_filename (lsec->owner->my_archive);
625 else
626 ln = sort_filename (lsec->owner);
627
628 i = filename_cmp (fn, ln);
629 if (i > 0)
630 { tree = &((*tree)->right); continue; }
631 else if (i < 0)
632 { tree = &((*tree)->left); continue; }
633
634 if (fa || la)
635 {
636 if (fa)
637 fn = sort_filename (file->the_bfd);
638 if (la)
639 ln = sort_filename (lsec->owner);
640
641 i = filename_cmp (fn, ln);
642 if (i > 0)
643 { tree = &((*tree)->right); continue; }
644 else if (i < 0)
645 { tree = &((*tree)->left); continue; }
646 }
647 }
648
649 /* Here either the files are not sorted by name, or we are
650 looking at the sections for this file. */
651
652 /* Find the correct node to append this section. */
653 if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none
654 && compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
655 tree = &((*tree)->left);
656 else
657 tree = &((*tree)->right);
658 }
659
660 return tree;
661 }
662
663 /* Use wild_sort to build a BST to sort sections. */
664
665 static void
666 output_section_callback_sort (lang_wild_statement_type *ptr,
667 struct wildcard_list *sec,
668 asection *section,
669 lang_input_statement_type *file,
670 void *output)
671 {
672 lang_section_bst_type *node;
673 lang_section_bst_type **tree;
674 lang_output_section_statement_type *os;
675
676 os = (lang_output_section_statement_type *) output;
677
678 if (unique_section_p (section, os))
679 return;
680
681 node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
682 node->left = 0;
683 node->right = 0;
684 node->section = section;
685 node->pattern = ptr->section_list;
686
687 tree = wild_sort (ptr, sec, file, section);
688 if (tree != NULL)
689 {
690 *tree = node;
691 if (tree == ptr->rightmost)
692 ptr->rightmost = &node->right;
693 }
694 }
695
696 /* Convert a sorted sections' BST back to list form. */
697
698 static void
699 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
700 lang_section_bst_type *tree,
701 void *output)
702 {
703 if (tree->left)
704 output_section_callback_tree_to_list (ptr, tree->left, output);
705
706 lang_add_section (&ptr->children, tree->section, tree->pattern,
707 ptr->section_flag_list,
708 (lang_output_section_statement_type *) output);
709
710 if (tree->right)
711 output_section_callback_tree_to_list (ptr, tree->right, output);
712
713 free (tree);
714 }
715
716 \f
717 /* Sections are matched against wildcard statements via a prefix tree.
718 The prefix tree holds prefixes of all matching patterns (up to the first
719 wildcard character), and the wild statement from which those patterns
720 came. When matching a section name against the tree we're walking through
721 the tree character by character. Each statement we hit is one that
722 potentially matches. This is checked by actually going through the
723 (glob) matching routines.
724
725 When the section name turns out to actually match we record that section
726 in the wild statements list of matching sections. */
727
728 /* A prefix can be matched by multiple statement, so we need a list of them. */
729 struct wild_stmt_list
730 {
731 lang_wild_statement_type *stmt;
732 struct wild_stmt_list *next;
733 };
734
735 /* The prefix tree itself. */
736 struct prefixtree
737 {
738 /* The list of all children (linked via .next). */
739 struct prefixtree *child;
740 struct prefixtree *next;
741 /* This tree node is responsible for the prefix of parent plus 'c'. */
742 char c;
743 /* The statements that potentially can match this prefix. */
744 struct wild_stmt_list *stmt;
745 };
746
747 /* We always have a root node in the prefix tree. It corresponds to the
748 empty prefix. E.g. a glob like "*" would sit in this root. */
749 static struct prefixtree the_root, *ptroot = &the_root;
750
751 /* Given a prefix tree in *TREE, corresponding to prefix P, find or
752 INSERT the tree node corresponding to prefix P+C. */
753
754 static struct prefixtree *
755 get_prefix_tree (struct prefixtree **tree, char c, bool insert)
756 {
757 struct prefixtree *t;
758 for (t = *tree; t; t = t->next)
759 if (t->c == c)
760 return t;
761 if (!insert)
762 return NULL;
763 t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t);
764 t->child = NULL;
765 t->next = *tree;
766 t->c = c;
767 t->stmt = NULL;
768 *tree = t;
769 return t;
770 }
771
772 /* Add STMT to the set of statements that can be matched by the prefix
773 corresponding to prefix tree T. */
774
775 static void
776 pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt)
777 {
778 struct wild_stmt_list *sl, **psl;
779 sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl);
780 sl->stmt = stmt;
781 sl->next = NULL;
782 psl = &t->stmt;
783 while (*psl)
784 psl = &(*psl)->next;
785 *psl = sl;
786 }
787
788 /* Insert STMT into the global prefix tree. */
789
790 static void
791 insert_prefix_tree (lang_wild_statement_type *stmt)
792 {
793 struct wildcard_list *sec;
794 struct prefixtree *t;
795
796 if (!stmt->section_list)
797 {
798 /* If we have no section_list (no wildcards in the wild STMT),
799 then every section name will match, so add this to the root. */
800 pt_add_stmt (ptroot, stmt);
801 return;
802 }
803
804 for (sec = stmt->section_list; sec; sec = sec->next)
805 {
806 const char *name = sec->spec.name ? sec->spec.name : "*";
807 char c;
808 t = ptroot;
809 for (; (c = *name); name++)
810 {
811 if (c == '*' || c == '[' || c == '?')
812 break;
813 t = get_prefix_tree (&t->child, c, true);
814 }
815 /* If we hit a glob character, the matching prefix is what we saw
816 until now. If we hit the end of pattern (hence it's no glob) then
817 we can do better: we only need to record a match when a section name
818 completely matches, not merely a prefix, so record the trailing 0
819 as well. */
820 if (!c)
821 t = get_prefix_tree (&t->child, 0, true);
822 pt_add_stmt (t, stmt);
823 }
824 }
825
826 /* Dump T indented by INDENT spaces. */
827
828 static void
829 debug_prefix_tree_rec (struct prefixtree *t, int indent)
830 {
831 for (; t; t = t->next)
832 {
833 struct wild_stmt_list *sl;
834 printf ("%*s %c", indent, "", t->c);
835 for (sl = t->stmt; sl; sl = sl->next)
836 {
837 struct wildcard_list *curr;
838 printf (" %p ", sl->stmt);
839 for (curr = sl->stmt->section_list; curr; curr = curr->next)
840 printf ("%s ", curr->spec.name ? curr->spec.name : "*");
841 }
842 printf ("\n");
843 debug_prefix_tree_rec (t->child, indent + 2);
844 }
845 }
846
847 /* Dump the global prefix tree. */
848
849 static void
850 debug_prefix_tree (void)
851 {
852 debug_prefix_tree_rec (ptroot, 2);
853 }
854
855 /* Like strcspn() but start to look from the end to beginning of
856 S. Returns the length of the suffix of S consisting entirely
857 of characters not in REJECT. */
858
859 static size_t
860 rstrcspn (const char *s, const char *reject)
861 {
862 size_t len = strlen (s), sufflen = 0;
863 while (len--)
864 {
865 char c = s[len];
866 if (strchr (reject, c) != 0)
867 break;
868 sufflen++;
869 }
870 return sufflen;
871 }
872
873 /* Analyze the wildcards in wild statement PTR to setup various
874 things for quick matching. */
875
876 static void
877 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
878 {
879 struct wildcard_list *sec;
880
881 ptr->tree = NULL;
882 ptr->rightmost = &ptr->tree;
883
884 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
885 {
886 if (sec->spec.name)
887 {
888 sec->spec.namelen = strlen (sec->spec.name);
889 sec->spec.prefixlen = strcspn (sec->spec.name, "?*[");
890 sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen,
891 "?*]");
892 }
893 else
894 sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0;
895 }
896
897 insert_prefix_tree (ptr);
898 }
899
900 /* Match all sections from FILE against the global prefix tree,
901 and record them into each wild statement that has a match. */
902
903 static void
904 resolve_wild_sections (lang_input_statement_type *file)
905 {
906 asection *s;
907
908 if (file->flags.just_syms)
909 return;
910
911 for (s = file->the_bfd->sections; s != NULL; s = s->next)
912 {
913 const char *sname = bfd_section_name (s);
914 char c = 1;
915 struct prefixtree *t = ptroot;
916 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
917 do
918 {
919 if (t->stmt)
920 {
921 struct wild_stmt_list *sl;
922 for (sl = t->stmt; sl; sl = sl->next)
923 {
924 walk_wild_section_match (sl->stmt, file, s);
925 //printf (" ZZZ maybe place into %p\n", sl->stmt);
926 }
927 }
928 if (!c)
929 break;
930 c = *sname++;
931 t = get_prefix_tree (&t->child, c, false);
932 }
933 while (t);
934 }
935 }
936
937 /* Match all sections from all input files against the global prefix tree. */
938
939 static void
940 resolve_wilds (void)
941 {
942 LANG_FOR_EACH_INPUT_STATEMENT (f)
943 {
944 //printf("XXX %s\n", f->filename);
945 if (f->the_bfd == NULL
946 || !bfd_check_format (f->the_bfd, bfd_archive))
947 resolve_wild_sections (f);
948 else
949 {
950 bfd *member;
951
952 /* This is an archive file. We must map each member of the
953 archive separately. */
954 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
955 while (member != NULL)
956 {
957 /* When lookup_name is called, it will call the add_symbols
958 entry point for the archive. For each element of the
959 archive which is included, BFD will call ldlang_add_file,
960 which will set the usrdata field of the member to the
961 lang_input_statement. */
962 if (bfd_usrdata (member) != NULL)
963 resolve_wild_sections (bfd_usrdata (member));
964
965 member = bfd_openr_next_archived_file (f->the_bfd, member);
966 }
967 }
968 }
969 }
970
971 /* For each input section that matches wild statement S calls
972 CALLBACK with DATA. */
973
974 static void
975 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
976 {
977 lang_statement_union_type *l;
978
979 for (l = s->matching_sections.head; l; l = l->header.next)
980 {
981 (*callback) (s, l->input_matcher.pattern, l->input_matcher.section,
982 l->input_matcher.input_stmt, data);
983 }
984 }
985
986 /* lang_for_each_statement walks the parse tree and calls the provided
987 function for each node, except those inside output section statements
988 with constraint set to -1. */
989
990 void
991 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
992 lang_statement_union_type *s)
993 {
994 for (; s != NULL; s = s->header.next)
995 {
996 func (s);
997
998 switch (s->header.type)
999 {
1000 case lang_constructors_statement_enum:
1001 lang_for_each_statement_worker (func, constructor_list.head);
1002 break;
1003 case lang_output_section_statement_enum:
1004 if (s->output_section_statement.constraint != -1)
1005 lang_for_each_statement_worker
1006 (func, s->output_section_statement.children.head);
1007 break;
1008 case lang_wild_statement_enum:
1009 lang_for_each_statement_worker (func,
1010 s->wild_statement.children.head);
1011 break;
1012 case lang_group_statement_enum:
1013 lang_for_each_statement_worker (func,
1014 s->group_statement.children.head);
1015 break;
1016 case lang_data_statement_enum:
1017 case lang_reloc_statement_enum:
1018 case lang_object_symbols_statement_enum:
1019 case lang_output_statement_enum:
1020 case lang_target_statement_enum:
1021 case lang_input_section_enum:
1022 case lang_input_statement_enum:
1023 case lang_assignment_statement_enum:
1024 case lang_padding_statement_enum:
1025 case lang_address_statement_enum:
1026 case lang_fill_statement_enum:
1027 case lang_insert_statement_enum:
1028 break;
1029 default:
1030 FAIL ();
1031 break;
1032 }
1033 }
1034 }
1035
1036 void
1037 lang_for_each_statement (void (*func) (lang_statement_union_type *))
1038 {
1039 lang_for_each_statement_worker (func, statement_list.head);
1040 }
1041
1042 /*----------------------------------------------------------------------*/
1043
1044 void
1045 lang_list_init (lang_statement_list_type *list)
1046 {
1047 list->head = NULL;
1048 list->tail = &list->head;
1049 }
1050
1051 static void
1052 lang_statement_append (lang_statement_list_type *list,
1053 void *element,
1054 void *field)
1055 {
1056 *(list->tail) = element;
1057 list->tail = field;
1058 }
1059
1060 void
1061 push_stat_ptr (lang_statement_list_type *new_ptr)
1062 {
1063 if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
1064 abort ();
1065 *stat_save_ptr++ = stat_ptr;
1066 stat_ptr = new_ptr;
1067 }
1068
1069 void
1070 pop_stat_ptr (void)
1071 {
1072 if (stat_save_ptr <= stat_save)
1073 abort ();
1074 stat_ptr = *--stat_save_ptr;
1075 }
1076
1077 /* Build a new statement node for the parse tree. */
1078
1079 static lang_statement_union_type *
1080 new_statement (enum statement_enum type,
1081 size_t size,
1082 lang_statement_list_type *list)
1083 {
1084 lang_statement_union_type *new_stmt;
1085
1086 new_stmt = stat_alloc (size);
1087 new_stmt->header.type = type;
1088 new_stmt->header.next = NULL;
1089 lang_statement_append (list, new_stmt, &new_stmt->header.next);
1090 return new_stmt;
1091 }
1092
1093 /* Build a new input file node for the language. There are several
1094 ways in which we treat an input file, eg, we only look at symbols,
1095 or prefix it with a -l etc.
1096
1097 We can be supplied with requests for input files more than once;
1098 they may, for example be split over several lines like foo.o(.text)
1099 foo.o(.data) etc, so when asked for a file we check that we haven't
1100 got it already so we don't duplicate the bfd. */
1101
1102 static lang_input_statement_type *
1103 new_afile (const char *name,
1104 lang_input_file_enum_type file_type,
1105 const char *target,
1106 const char *from_filename)
1107 {
1108 lang_input_statement_type *p;
1109
1110 lang_has_input_file = true;
1111
1112 p = new_stat (lang_input_statement, stat_ptr);
1113 memset (&p->the_bfd, 0,
1114 sizeof (*p) - offsetof (lang_input_statement_type, the_bfd));
1115 p->extra_search_path = NULL;
1116 p->target = target;
1117 p->flags.dynamic = input_flags.dynamic;
1118 p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic;
1119 p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular;
1120 p->flags.whole_archive = input_flags.whole_archive;
1121 p->flags.sysrooted = input_flags.sysrooted;
1122 p->sort_key = NULL;
1123
1124 switch (file_type)
1125 {
1126 case lang_input_file_is_symbols_only_enum:
1127 p->filename = name;
1128 p->local_sym_name = name;
1129 p->flags.real = true;
1130 p->flags.just_syms = true;
1131 break;
1132 case lang_input_file_is_fake_enum:
1133 p->filename = name;
1134 p->local_sym_name = name;
1135 break;
1136 case lang_input_file_is_l_enum:
1137 if (name[0] == ':' && name[1] != '\0')
1138 {
1139 p->filename = name + 1;
1140 p->flags.full_name_provided = true;
1141 }
1142 else
1143 p->filename = name;
1144 p->local_sym_name = concat ("-l", name, (const char *) NULL);
1145 p->flags.maybe_archive = true;
1146 p->flags.real = true;
1147 p->flags.search_dirs = true;
1148 break;
1149 case lang_input_file_is_marker_enum:
1150 p->filename = name;
1151 p->local_sym_name = name;
1152 p->flags.search_dirs = true;
1153 break;
1154 case lang_input_file_is_search_file_enum:
1155 p->filename = name;
1156 p->local_sym_name = name;
1157 /* If name is a relative path, search the directory of the current linker
1158 script first. */
1159 if (from_filename && !IS_ABSOLUTE_PATH (name))
1160 p->extra_search_path = ldirname (from_filename);
1161 p->flags.real = true;
1162 p->flags.search_dirs = true;
1163 break;
1164 case lang_input_file_is_file_enum:
1165 p->filename = name;
1166 p->local_sym_name = name;
1167 p->flags.real = true;
1168 break;
1169 default:
1170 FAIL ();
1171 }
1172
1173 lang_statement_append (&input_file_chain, p, &p->next_real_file);
1174 return p;
1175 }
1176
1177 lang_input_statement_type *
1178 lang_add_input_file (const char *name,
1179 lang_input_file_enum_type file_type,
1180 const char *target)
1181 {
1182 if (name != NULL
1183 && (*name == '=' || startswith (name, "$SYSROOT")))
1184 {
1185 lang_input_statement_type *ret;
1186 char *sysrooted_name
1187 = concat (ld_sysroot,
1188 name + (*name == '=' ? 1 : strlen ("$SYSROOT")),
1189 (const char *) NULL);
1190
1191 /* We've now forcibly prepended the sysroot, making the input
1192 file independent of the context. Therefore, temporarily
1193 force a non-sysrooted context for this statement, so it won't
1194 get the sysroot prepended again when opened. (N.B. if it's a
1195 script, any child nodes with input files starting with "/"
1196 will be handled as "sysrooted" as they'll be found to be
1197 within the sysroot subdirectory.) */
1198 unsigned int outer_sysrooted = input_flags.sysrooted;
1199 input_flags.sysrooted = 0;
1200 ret = new_afile (sysrooted_name, file_type, target, NULL);
1201 input_flags.sysrooted = outer_sysrooted;
1202 return ret;
1203 }
1204
1205 return new_afile (name, file_type, target, current_input_file);
1206 }
1207
1208 struct out_section_hash_entry
1209 {
1210 struct bfd_hash_entry root;
1211 lang_statement_union_type s;
1212 };
1213
1214 /* The hash table. */
1215
1216 static struct bfd_hash_table output_section_statement_table;
1217
1218 /* Support routines for the hash table used by lang_output_section_find,
1219 initialize the table, fill in an entry and remove the table. */
1220
1221 static struct bfd_hash_entry *
1222 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1223 struct bfd_hash_table *table,
1224 const char *string)
1225 {
1226 lang_output_section_statement_type **nextp;
1227 struct out_section_hash_entry *ret;
1228
1229 if (entry == NULL)
1230 {
1231 entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
1232 sizeof (*ret));
1233 if (entry == NULL)
1234 return entry;
1235 }
1236
1237 entry = bfd_hash_newfunc (entry, table, string);
1238 if (entry == NULL)
1239 return entry;
1240
1241 ret = (struct out_section_hash_entry *) entry;
1242 memset (&ret->s, 0, sizeof (ret->s));
1243 ret->s.header.type = lang_output_section_statement_enum;
1244 ret->s.output_section_statement.subsection_alignment = NULL;
1245 ret->s.output_section_statement.section_alignment = NULL;
1246 ret->s.output_section_statement.block_value = 1;
1247 lang_list_init (&ret->s.output_section_statement.children);
1248 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1249
1250 /* For every output section statement added to the list, except the
1251 first one, lang_os_list.tail points to the "next"
1252 field of the last element of the list. */
1253 if (lang_os_list.head != NULL)
1254 ret->s.output_section_statement.prev
1255 = ((lang_output_section_statement_type *)
1256 ((char *) lang_os_list.tail
1257 - offsetof (lang_output_section_statement_type, next)));
1258
1259 /* GCC's strict aliasing rules prevent us from just casting the
1260 address, so we store the pointer in a variable and cast that
1261 instead. */
1262 nextp = &ret->s.output_section_statement.next;
1263 lang_statement_append (&lang_os_list, &ret->s, nextp);
1264 return &ret->root;
1265 }
1266
1267 static void
1268 output_section_statement_table_init (void)
1269 {
1270 if (!bfd_hash_table_init_n (&output_section_statement_table,
1271 output_section_statement_newfunc,
1272 sizeof (struct out_section_hash_entry),
1273 61))
1274 einfo (_("%F%P: can not create hash table: %E\n"));
1275 }
1276
1277 static void
1278 output_section_statement_table_free (void)
1279 {
1280 bfd_hash_table_free (&output_section_statement_table);
1281 }
1282
1283 /* Build enough state so that the parser can build its tree. */
1284
1285 void
1286 lang_init (void)
1287 {
1288 obstack_begin (&stat_obstack, 1000);
1289 obstack_init (&pt_obstack);
1290
1291 stat_ptr = &statement_list;
1292
1293 output_section_statement_table_init ();
1294
1295 lang_list_init (stat_ptr);
1296
1297 lang_list_init (&input_file_chain);
1298 lang_list_init (&lang_os_list);
1299 lang_list_init (&file_chain);
1300 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1301 NULL);
1302 abs_output_section =
1303 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1);
1304
1305 abs_output_section->bfd_section = bfd_abs_section_ptr;
1306
1307 asneeded_list_head = NULL;
1308 asneeded_list_tail = &asneeded_list_head;
1309 }
1310
1311 void
1312 lang_finish (void)
1313 {
1314 output_section_statement_table_free ();
1315 }
1316
1317 /*----------------------------------------------------------------------
1318 A region is an area of memory declared with the
1319 MEMORY { name:org=exp, len=exp ... }
1320 syntax.
1321
1322 We maintain a list of all the regions here.
1323
1324 If no regions are specified in the script, then the default is used
1325 which is created when looked up to be the entire data space.
1326
1327 If create is true we are creating a region inside a MEMORY block.
1328 In this case it is probably an error to create a region that has
1329 already been created. If we are not inside a MEMORY block it is
1330 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1331 and so we issue a warning.
1332
1333 Each region has at least one name. The first name is either
1334 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1335 alias names to an existing region within a script with
1336 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1337 region. */
1338
1339 static lang_memory_region_type *lang_memory_region_list;
1340 static lang_memory_region_type **lang_memory_region_list_tail
1341 = &lang_memory_region_list;
1342
1343 lang_memory_region_type *
1344 lang_memory_region_lookup (const char *const name, bool create)
1345 {
1346 lang_memory_region_name *n;
1347 lang_memory_region_type *r;
1348 lang_memory_region_type *new_region;
1349
1350 /* NAME is NULL for LMA memspecs if no region was specified. */
1351 if (name == NULL)
1352 return NULL;
1353
1354 for (r = lang_memory_region_list; r != NULL; r = r->next)
1355 for (n = &r->name_list; n != NULL; n = n->next)
1356 if (strcmp (n->name, name) == 0)
1357 {
1358 if (create)
1359 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
1360 NULL, name);
1361 return r;
1362 }
1363
1364 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1365 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
1366 NULL, name);
1367
1368 new_region = stat_alloc (sizeof (lang_memory_region_type));
1369
1370 new_region->name_list.name = xstrdup (name);
1371 new_region->name_list.next = NULL;
1372 new_region->next = NULL;
1373 new_region->origin_exp = NULL;
1374 new_region->origin = 0;
1375 new_region->length_exp = NULL;
1376 new_region->length = ~(bfd_size_type) 0;
1377 new_region->current = 0;
1378 new_region->last_os = NULL;
1379 new_region->flags = 0;
1380 new_region->not_flags = 0;
1381 new_region->had_full_message = false;
1382
1383 *lang_memory_region_list_tail = new_region;
1384 lang_memory_region_list_tail = &new_region->next;
1385
1386 return new_region;
1387 }
1388
1389 void
1390 lang_memory_region_alias (const char *alias, const char *region_name)
1391 {
1392 lang_memory_region_name *n;
1393 lang_memory_region_type *r;
1394 lang_memory_region_type *region;
1395
1396 /* The default region must be unique. This ensures that it is not necessary
1397 to iterate through the name list if someone wants the check if a region is
1398 the default memory region. */
1399 if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
1400 || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
1401 einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL);
1402
1403 /* Look for the target region and check if the alias is not already
1404 in use. */
1405 region = NULL;
1406 for (r = lang_memory_region_list; r != NULL; r = r->next)
1407 for (n = &r->name_list; n != NULL; n = n->next)
1408 {
1409 if (region == NULL && strcmp (n->name, region_name) == 0)
1410 region = r;
1411 if (strcmp (n->name, alias) == 0)
1412 einfo (_("%F%P:%pS: error: redefinition of memory region "
1413 "alias `%s'\n"),
1414 NULL, alias);
1415 }
1416
1417 /* Check if the target region exists. */
1418 if (region == NULL)
1419 einfo (_("%F%P:%pS: error: memory region `%s' "
1420 "for alias `%s' does not exist\n"),
1421 NULL, region_name, alias);
1422
1423 /* Add alias to region name list. */
1424 n = stat_alloc (sizeof (lang_memory_region_name));
1425 n->name = xstrdup (alias);
1426 n->next = region->name_list.next;
1427 region->name_list.next = n;
1428 }
1429
1430 static lang_memory_region_type *
1431 lang_memory_default (asection *section)
1432 {
1433 lang_memory_region_type *p;
1434
1435 flagword sec_flags = section->flags;
1436
1437 /* Override SEC_DATA to mean a writable section. */
1438 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1439 sec_flags |= SEC_DATA;
1440
1441 for (p = lang_memory_region_list; p != NULL; p = p->next)
1442 {
1443 if ((p->flags & sec_flags) != 0
1444 && (p->not_flags & sec_flags) == 0)
1445 {
1446 return p;
1447 }
1448 }
1449 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false);
1450 }
1451
1452 /* Get the output section statement directly from the userdata. */
1453
1454 lang_output_section_statement_type *
1455 lang_output_section_get (const asection *output_section)
1456 {
1457 return bfd_section_userdata (output_section);
1458 }
1459
1460 /* Find or create an output_section_statement with the given NAME.
1461 If CONSTRAINT is non-zero match one with that constraint, otherwise
1462 match any non-negative constraint. If CREATE is 0 return NULL when
1463 no match exists. If CREATE is 1, create an output_section_statement
1464 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
1465 always make a new output_section_statement. */
1466
1467 lang_output_section_statement_type *
1468 lang_output_section_statement_lookup (const char *name,
1469 int constraint,
1470 int create)
1471 {
1472 struct out_section_hash_entry *entry;
1473
1474 entry = ((struct out_section_hash_entry *)
1475 bfd_hash_lookup (&output_section_statement_table, name,
1476 create != 0, false));
1477 if (entry == NULL)
1478 {
1479 if (create)
1480 einfo (_("%F%P: failed creating section `%s': %E\n"), name);
1481 return NULL;
1482 }
1483
1484 if (entry->s.output_section_statement.name != NULL)
1485 {
1486 /* We have a section of this name, but it might not have the correct
1487 constraint. */
1488 struct out_section_hash_entry *last_ent;
1489
1490 name = entry->s.output_section_statement.name;
1491 do
1492 {
1493 if (create != 2
1494 && !(create && constraint == SPECIAL)
1495 && (constraint == entry->s.output_section_statement.constraint
1496 || (constraint == 0
1497 && entry->s.output_section_statement.constraint >= 0)))
1498 return &entry->s.output_section_statement;
1499 last_ent = entry;
1500 entry = (struct out_section_hash_entry *) entry->root.next;
1501 }
1502 while (entry != NULL
1503 && name == entry->s.output_section_statement.name);
1504
1505 if (!create)
1506 return NULL;
1507
1508 entry
1509 = ((struct out_section_hash_entry *)
1510 output_section_statement_newfunc (NULL,
1511 &output_section_statement_table,
1512 name));
1513 if (entry == NULL)
1514 {
1515 einfo (_("%F%P: failed creating section `%s': %E\n"), name);
1516 return NULL;
1517 }
1518 entry->root = last_ent->root;
1519 last_ent->root.next = &entry->root;
1520 }
1521
1522 entry->s.output_section_statement.name = name;
1523 entry->s.output_section_statement.constraint = constraint;
1524 entry->s.output_section_statement.dup_output = (create == 2
1525 || constraint == SPECIAL);
1526 return &entry->s.output_section_statement;
1527 }
1528
1529 /* Find the next output_section_statement with the same name as OS.
1530 If CONSTRAINT is non-zero, find one with that constraint otherwise
1531 match any non-negative constraint. */
1532
1533 lang_output_section_statement_type *
1534 next_matching_output_section_statement (lang_output_section_statement_type *os,
1535 int constraint)
1536 {
1537 /* All output_section_statements are actually part of a
1538 struct out_section_hash_entry. */
1539 struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
1540 ((char *) os
1541 - offsetof (struct out_section_hash_entry, s.output_section_statement));
1542 const char *name = os->name;
1543
1544 ASSERT (name == entry->root.string);
1545 do
1546 {
1547 entry = (struct out_section_hash_entry *) entry->root.next;
1548 if (entry == NULL
1549 || name != entry->s.output_section_statement.name)
1550 return NULL;
1551 }
1552 while (constraint != entry->s.output_section_statement.constraint
1553 && (constraint != 0
1554 || entry->s.output_section_statement.constraint < 0));
1555
1556 return &entry->s.output_section_statement;
1557 }
1558
1559 /* A variant of lang_output_section_find used by place_orphan.
1560 Returns the output statement that should precede a new output
1561 statement for SEC. If an exact match is found on certain flags,
1562 sets *EXACT too. */
1563
1564 lang_output_section_statement_type *
1565 lang_output_section_find_by_flags (const asection *sec,
1566 flagword sec_flags,
1567 lang_output_section_statement_type **exact,
1568 lang_match_sec_type_func match_type)
1569 {
1570 lang_output_section_statement_type *first, *look, *found;
1571 flagword look_flags, differ;
1572
1573 /* We know the first statement on this list is *ABS*. May as well
1574 skip it. */
1575 first = (void *) lang_os_list.head;
1576 first = first->next;
1577
1578 /* First try for an exact match. */
1579 found = NULL;
1580 for (look = first; look; look = look->next)
1581 {
1582 look_flags = look->flags;
1583 if (look->bfd_section != NULL)
1584 {
1585 look_flags = look->bfd_section->flags;
1586 if (match_type && !match_type (link_info.output_bfd,
1587 look->bfd_section,
1588 sec->owner, sec))
1589 continue;
1590 }
1591 differ = look_flags ^ sec_flags;
1592 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1593 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1594 found = look;
1595 }
1596 if (found != NULL)
1597 {
1598 if (exact != NULL)
1599 *exact = found;
1600 return found;
1601 }
1602
1603 if ((sec_flags & SEC_CODE) != 0
1604 && (sec_flags & SEC_ALLOC) != 0)
1605 {
1606 /* Try for a rw code section. */
1607 for (look = first; look; look = look->next)
1608 {
1609 look_flags = look->flags;
1610 if (look->bfd_section != NULL)
1611 {
1612 look_flags = look->bfd_section->flags;
1613 if (match_type && !match_type (link_info.output_bfd,
1614 look->bfd_section,
1615 sec->owner, sec))
1616 continue;
1617 }
1618 differ = look_flags ^ sec_flags;
1619 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1620 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1621 found = look;
1622 }
1623 }
1624 else if ((sec_flags & SEC_READONLY) != 0
1625 && (sec_flags & SEC_ALLOC) != 0)
1626 {
1627 /* .rodata can go after .text, .sdata2 after .rodata. */
1628 for (look = first; look; look = look->next)
1629 {
1630 look_flags = look->flags;
1631 if (look->bfd_section != NULL)
1632 {
1633 look_flags = look->bfd_section->flags;
1634 if (match_type && !match_type (link_info.output_bfd,
1635 look->bfd_section,
1636 sec->owner, sec))
1637 continue;
1638 }
1639 differ = look_flags ^ sec_flags;
1640 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1641 | SEC_READONLY | SEC_SMALL_DATA))
1642 || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1643 | SEC_READONLY))
1644 && !(look_flags & SEC_SMALL_DATA)))
1645 found = look;
1646 }
1647 }
1648 else if ((sec_flags & SEC_THREAD_LOCAL) != 0
1649 && (sec_flags & SEC_ALLOC) != 0)
1650 {
1651 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
1652 as if it were a loaded section, and don't use match_type. */
1653 bool seen_thread_local = false;
1654
1655 match_type = NULL;
1656 for (look = first; look; look = look->next)
1657 {
1658 look_flags = look->flags;
1659 if (look->bfd_section != NULL)
1660 look_flags = look->bfd_section->flags;
1661
1662 differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS);
1663 if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC)))
1664 {
1665 /* .tdata and .tbss must be adjacent and in that order. */
1666 if (!(look_flags & SEC_LOAD)
1667 && (sec_flags & SEC_LOAD))
1668 /* ..so if we're at a .tbss section and we're placing
1669 a .tdata section stop looking and return the
1670 previous section. */
1671 break;
1672 found = look;
1673 seen_thread_local = true;
1674 }
1675 else if (seen_thread_local)
1676 break;
1677 else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD)))
1678 found = look;
1679 }
1680 }
1681 else if ((sec_flags & SEC_SMALL_DATA) != 0
1682 && (sec_flags & SEC_ALLOC) != 0)
1683 {
1684 /* .sdata goes after .data, .sbss after .sdata. */
1685 for (look = first; look; look = look->next)
1686 {
1687 look_flags = look->flags;
1688 if (look->bfd_section != NULL)
1689 {
1690 look_flags = look->bfd_section->flags;
1691 if (match_type && !match_type (link_info.output_bfd,
1692 look->bfd_section,
1693 sec->owner, sec))
1694 continue;
1695 }
1696 differ = look_flags ^ sec_flags;
1697 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1698 | SEC_THREAD_LOCAL))
1699 || ((look_flags & SEC_SMALL_DATA)
1700 && !(sec_flags & SEC_HAS_CONTENTS)))
1701 found = look;
1702 }
1703 }
1704 else if ((sec_flags & SEC_HAS_CONTENTS) != 0
1705 && (sec_flags & SEC_ALLOC) != 0)
1706 {
1707 /* .data goes after .rodata. */
1708 for (look = first; look; look = look->next)
1709 {
1710 look_flags = look->flags;
1711 if (look->bfd_section != NULL)
1712 {
1713 look_flags = look->bfd_section->flags;
1714 if (match_type && !match_type (link_info.output_bfd,
1715 look->bfd_section,
1716 sec->owner, sec))
1717 continue;
1718 }
1719 differ = look_flags ^ sec_flags;
1720 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1721 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1722 found = look;
1723 }
1724 }
1725 else if ((sec_flags & SEC_ALLOC) != 0)
1726 {
1727 /* .bss goes after any other alloc section. */
1728 for (look = first; look; look = look->next)
1729 {
1730 look_flags = look->flags;
1731 if (look->bfd_section != NULL)
1732 {
1733 look_flags = look->bfd_section->flags;
1734 if (match_type && !match_type (link_info.output_bfd,
1735 look->bfd_section,
1736 sec->owner, sec))
1737 continue;
1738 }
1739 differ = look_flags ^ sec_flags;
1740 if (!(differ & SEC_ALLOC))
1741 found = look;
1742 }
1743 }
1744 else
1745 {
1746 /* non-alloc go last. */
1747 for (look = first; look; look = look->next)
1748 {
1749 look_flags = look->flags;
1750 if (look->bfd_section != NULL)
1751 look_flags = look->bfd_section->flags;
1752 differ = look_flags ^ sec_flags;
1753 if (!(differ & SEC_DEBUGGING))
1754 found = look;
1755 }
1756 return found;
1757 }
1758
1759 if (found || !match_type)
1760 return found;
1761
1762 return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL);
1763 }
1764
1765 /* Find the last output section before given output statement.
1766 Used by place_orphan. */
1767
1768 static asection *
1769 output_prev_sec_find (lang_output_section_statement_type *os)
1770 {
1771 lang_output_section_statement_type *lookup;
1772
1773 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1774 {
1775 if (lookup->constraint < 0)
1776 continue;
1777
1778 if (lookup->bfd_section != NULL)
1779 return lookup->bfd_section;
1780 }
1781
1782 return NULL;
1783 }
1784
1785 /* Look for a suitable place for a new output section statement. The
1786 idea is to skip over anything that might be inside a SECTIONS {}
1787 statement in a script, before we find another output section
1788 statement. Assignments to "dot" before an output section statement
1789 are assumed to belong to it, except in two cases; The first
1790 assignment to dot, and assignments before non-alloc sections.
1791 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1792 similar assignments that set the initial address, or we might
1793 insert non-alloc note sections among assignments setting end of
1794 image symbols. */
1795
1796 static lang_statement_union_type **
1797 insert_os_after (lang_statement_union_type *after)
1798 {
1799 lang_statement_union_type **where;
1800 lang_statement_union_type **assign = NULL;
1801 bool ignore_first;
1802
1803 ignore_first = after == lang_os_list.head;
1804
1805 for (where = &after->header.next;
1806 *where != NULL;
1807 where = &(*where)->header.next)
1808 {
1809 switch ((*where)->header.type)
1810 {
1811 case lang_assignment_statement_enum:
1812 if (assign == NULL)
1813 {
1814 lang_assignment_statement_type *ass;
1815
1816 ass = &(*where)->assignment_statement;
1817 if (ass->exp->type.node_class != etree_assert
1818 && ass->exp->assign.dst[0] == '.'
1819 && ass->exp->assign.dst[1] == 0)
1820 {
1821 if (!ignore_first)
1822 assign = where;
1823 ignore_first = false;
1824 }
1825 }
1826 continue;
1827 case lang_wild_statement_enum:
1828 case lang_input_section_enum:
1829 case lang_object_symbols_statement_enum:
1830 case lang_fill_statement_enum:
1831 case lang_data_statement_enum:
1832 case lang_reloc_statement_enum:
1833 case lang_padding_statement_enum:
1834 case lang_constructors_statement_enum:
1835 assign = NULL;
1836 ignore_first = false;
1837 continue;
1838 case lang_output_section_statement_enum:
1839 if (assign != NULL)
1840 {
1841 asection *s = (*where)->output_section_statement.bfd_section;
1842
1843 if (s == NULL
1844 || s->map_head.s == NULL
1845 || (s->flags & SEC_ALLOC) != 0)
1846 where = assign;
1847 }
1848 break;
1849 case lang_input_statement_enum:
1850 case lang_address_statement_enum:
1851 case lang_target_statement_enum:
1852 case lang_output_statement_enum:
1853 case lang_group_statement_enum:
1854 case lang_insert_statement_enum:
1855 continue;
1856 case lang_input_matcher_enum:
1857 FAIL ();
1858 }
1859 break;
1860 }
1861
1862 return where;
1863 }
1864
1865 lang_output_section_statement_type *
1866 lang_insert_orphan (asection *s,
1867 const char *secname,
1868 int constraint,
1869 lang_output_section_statement_type *after,
1870 struct orphan_save *place,
1871 etree_type *address,
1872 lang_statement_list_type *add_child)
1873 {
1874 lang_statement_list_type add;
1875 lang_output_section_statement_type *os;
1876 lang_output_section_statement_type **os_tail;
1877
1878 /* If we have found an appropriate place for the output section
1879 statements for this orphan, add them to our own private list,
1880 inserting them later into the global statement list. */
1881 if (after != NULL)
1882 {
1883 lang_list_init (&add);
1884 push_stat_ptr (&add);
1885 }
1886
1887 if (bfd_link_relocatable (&link_info)
1888 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1889 address = exp_intop (0);
1890
1891 os_tail = (lang_output_section_statement_type **) lang_os_list.tail;
1892 os = lang_enter_output_section_statement (
1893 secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0);
1894
1895 if (add_child == NULL)
1896 add_child = &os->children;
1897 lang_add_section (add_child, s, NULL, NULL, os);
1898
1899 if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
1900 {
1901 const char *region = (after->region
1902 ? after->region->name_list.name
1903 : DEFAULT_MEMORY_REGION);
1904 const char *lma_region = (after->lma_region
1905 ? after->lma_region->name_list.name
1906 : NULL);
1907 lang_leave_output_section_statement (NULL, region, after->phdrs,
1908 lma_region);
1909 }
1910 else
1911 lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
1912 NULL);
1913
1914 /* Restore the global list pointer. */
1915 if (after != NULL)
1916 pop_stat_ptr ();
1917
1918 if (after != NULL && os->bfd_section != NULL)
1919 {
1920 asection *snew, *as;
1921 bool place_after = place->stmt == NULL;
1922 bool insert_after = true;
1923
1924 snew = os->bfd_section;
1925
1926 /* Shuffle the bfd section list to make the output file look
1927 neater. This is really only cosmetic. */
1928 if (place->section == NULL
1929 && after != (void *) lang_os_list.head)
1930 {
1931 asection *bfd_section = after->bfd_section;
1932
1933 /* If the output statement hasn't been used to place any input
1934 sections (and thus doesn't have an output bfd_section),
1935 look for the closest prior output statement having an
1936 output section. */
1937 if (bfd_section == NULL)
1938 bfd_section = output_prev_sec_find (after);
1939
1940 if (bfd_section != NULL
1941 && bfd_section->owner != NULL
1942 && bfd_section != snew)
1943 place->section = &bfd_section->next;
1944 }
1945
1946 if (place->section == NULL)
1947 place->section = &link_info.output_bfd->sections;
1948
1949 as = *place->section;
1950
1951 if (!as)
1952 {
1953 /* Put the section at the end of the list. */
1954
1955 /* Unlink the section. */
1956 bfd_section_list_remove (link_info.output_bfd, snew);
1957
1958 /* Now tack it back on in the right place. */
1959 bfd_section_list_append (link_info.output_bfd, snew);
1960 }
1961 else if ((bfd_get_flavour (link_info.output_bfd)
1962 == bfd_target_elf_flavour)
1963 && (bfd_get_flavour (s->owner)
1964 == bfd_target_elf_flavour)
1965 && ((elf_section_type (s) == SHT_NOTE
1966 && (s->flags & SEC_LOAD) != 0)
1967 || (elf_section_type (as) == SHT_NOTE
1968 && (as->flags & SEC_LOAD) != 0)))
1969 {
1970 /* Make sure that output note sections are grouped and sorted
1971 by alignments when inserting a note section or insert a
1972 section after a note section, */
1973 asection *sec;
1974 /* A specific section after which the output note section
1975 should be placed. */
1976 asection *after_sec;
1977 /* True if we need to insert the orphan section after a
1978 specific section to maintain output note section order. */
1979 bool after_sec_note = false;
1980
1981 static asection *first_orphan_note = NULL;
1982
1983 /* Group and sort output note section by alignments in
1984 ascending order. */
1985 after_sec = NULL;
1986 if (elf_section_type (s) == SHT_NOTE
1987 && (s->flags & SEC_LOAD) != 0)
1988 {
1989 /* Search from the beginning for the last output note
1990 section with equal or larger alignments. NB: Don't
1991 place orphan note section after non-note sections. */
1992
1993 first_orphan_note = NULL;
1994 for (sec = link_info.output_bfd->sections;
1995 (sec != NULL
1996 && !bfd_is_abs_section (sec));
1997 sec = sec->next)
1998 if (sec != snew
1999 && elf_section_type (sec) == SHT_NOTE
2000 && (sec->flags & SEC_LOAD) != 0)
2001 {
2002 if (!first_orphan_note)
2003 first_orphan_note = sec;
2004 if (sec->alignment_power >= s->alignment_power)
2005 after_sec = sec;
2006 }
2007 else if (first_orphan_note)
2008 {
2009 /* Stop if there is non-note section after the first
2010 orphan note section. */
2011 break;
2012 }
2013
2014 /* If this will be the first orphan note section, it can
2015 be placed at the default location. */
2016 after_sec_note = first_orphan_note != NULL;
2017 if (after_sec == NULL && after_sec_note)
2018 {
2019 /* If all output note sections have smaller
2020 alignments, place the section before all
2021 output orphan note sections. */
2022 after_sec = first_orphan_note;
2023 insert_after = false;
2024 }
2025 }
2026 else if (first_orphan_note)
2027 {
2028 /* Don't place non-note sections in the middle of orphan
2029 note sections. */
2030 after_sec_note = true;
2031 after_sec = as;
2032 for (sec = as->next;
2033 (sec != NULL
2034 && !bfd_is_abs_section (sec));
2035 sec = sec->next)
2036 if (elf_section_type (sec) == SHT_NOTE
2037 && (sec->flags & SEC_LOAD) != 0)
2038 after_sec = sec;
2039 }
2040
2041 if (after_sec_note)
2042 {
2043 if (after_sec)
2044 {
2045 /* Search forward to insert OS after AFTER_SEC output
2046 statement. */
2047 lang_output_section_statement_type *stmt, *next;
2048 bool found = false;
2049 for (stmt = after; stmt != NULL; stmt = next)
2050 {
2051 next = stmt->next;
2052 if (insert_after)
2053 {
2054 if (stmt->bfd_section == after_sec)
2055 {
2056 place_after = true;
2057 found = true;
2058 after = stmt;
2059 break;
2060 }
2061 }
2062 else
2063 {
2064 /* If INSERT_AFTER is FALSE, place OS before
2065 AFTER_SEC output statement. */
2066 if (next && next->bfd_section == after_sec)
2067 {
2068 place_after = true;
2069 found = true;
2070 after = stmt;
2071 break;
2072 }
2073 }
2074 }
2075
2076 /* Search backward to insert OS after AFTER_SEC output
2077 statement. */
2078 if (!found)
2079 for (stmt = after; stmt != NULL; stmt = stmt->prev)
2080 {
2081 if (insert_after)
2082 {
2083 if (stmt->bfd_section == after_sec)
2084 {
2085 place_after = true;
2086 after = stmt;
2087 break;
2088 }
2089 }
2090 else
2091 {
2092 /* If INSERT_AFTER is FALSE, place OS before
2093 AFTER_SEC output statement. */
2094 if (stmt->next->bfd_section == after_sec)
2095 {
2096 place_after = true;
2097 after = stmt;
2098 break;
2099 }
2100 }
2101 }
2102 }
2103
2104 if (after_sec == NULL
2105 || (insert_after && after_sec->next != snew)
2106 || (!insert_after && after_sec->prev != snew))
2107 {
2108 /* Unlink the section. */
2109 bfd_section_list_remove (link_info.output_bfd, snew);
2110
2111 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
2112 prepend SNEW. */
2113 if (after_sec)
2114 {
2115 if (insert_after)
2116 bfd_section_list_insert_after (link_info.output_bfd,
2117 after_sec, snew);
2118 else
2119 bfd_section_list_insert_before (link_info.output_bfd,
2120 after_sec, snew);
2121 }
2122 else
2123 bfd_section_list_prepend (link_info.output_bfd, snew);
2124 }
2125 }
2126 else if (as != snew && as->prev != snew)
2127 {
2128 /* Unlink the section. */
2129 bfd_section_list_remove (link_info.output_bfd, snew);
2130
2131 /* Now tack it back on in the right place. */
2132 bfd_section_list_insert_before (link_info.output_bfd,
2133 as, snew);
2134 }
2135 }
2136 else if (as != snew && as->prev != snew)
2137 {
2138 /* Unlink the section. */
2139 bfd_section_list_remove (link_info.output_bfd, snew);
2140
2141 /* Now tack it back on in the right place. */
2142 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
2143 }
2144
2145 /* Save the end of this list. Further ophans of this type will
2146 follow the one we've just added. */
2147 place->section = &snew->next;
2148
2149 /* The following is non-cosmetic. We try to put the output
2150 statements in some sort of reasonable order here, because they
2151 determine the final load addresses of the orphan sections.
2152 In addition, placing output statements in the wrong order may
2153 require extra segments. For instance, given a typical
2154 situation of all read-only sections placed in one segment and
2155 following that a segment containing all the read-write
2156 sections, we wouldn't want to place an orphan read/write
2157 section before or amongst the read-only ones. */
2158 if (add.head != NULL)
2159 {
2160 lang_output_section_statement_type *newly_added_os;
2161
2162 /* Place OS after AFTER if AFTER_NOTE is TRUE. */
2163 if (place_after)
2164 {
2165 lang_statement_union_type **where;
2166
2167 where = insert_os_after ((lang_statement_union_type *) after);
2168 *add.tail = *where;
2169 *where = add.head;
2170
2171 place->os_tail = &after->next;
2172 }
2173 else
2174 {
2175 /* Put it after the last orphan statement we added. */
2176 *add.tail = *place->stmt;
2177 *place->stmt = add.head;
2178 }
2179
2180 /* Fix the global list pointer if we happened to tack our
2181 new list at the tail. */
2182 if (*stat_ptr->tail == add.head)
2183 stat_ptr->tail = add.tail;
2184
2185 /* Save the end of this list. */
2186 place->stmt = add.tail;
2187
2188 /* Do the same for the list of output section statements. */
2189 newly_added_os = *os_tail;
2190 *os_tail = NULL;
2191 newly_added_os->prev = (lang_output_section_statement_type *)
2192 ((char *) place->os_tail
2193 - offsetof (lang_output_section_statement_type, next));
2194 newly_added_os->next = *place->os_tail;
2195 if (newly_added_os->next != NULL)
2196 newly_added_os->next->prev = newly_added_os;
2197 *place->os_tail = newly_added_os;
2198 place->os_tail = &newly_added_os->next;
2199
2200 /* Fixing the global list pointer here is a little different.
2201 We added to the list in lang_enter_output_section_statement,
2202 trimmed off the new output_section_statment above when
2203 assigning *os_tail = NULL, but possibly added it back in
2204 the same place when assigning *place->os_tail. */
2205 if (*os_tail == NULL)
2206 lang_os_list.tail = (lang_statement_union_type **) os_tail;
2207 }
2208 }
2209 return os;
2210 }
2211
2212 static void
2213 lang_print_asneeded (void)
2214 {
2215 struct asneeded_minfo *m;
2216
2217 if (asneeded_list_head == NULL)
2218 return;
2219
2220 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
2221
2222 for (m = asneeded_list_head; m != NULL; m = m->next)
2223 {
2224 int len;
2225
2226 minfo ("%s", m->soname);
2227 len = strlen (m->soname);
2228
2229 if (len >= 29)
2230 {
2231 print_nl ();
2232 len = 0;
2233 }
2234 print_spaces (30 - len);
2235
2236 if (m->ref != NULL)
2237 minfo ("%pB ", m->ref);
2238 minfo ("(%pT)\n", m->name);
2239 }
2240 }
2241
2242 static void
2243 lang_map_flags (flagword flag)
2244 {
2245 if (flag & SEC_ALLOC)
2246 minfo ("a");
2247
2248 if (flag & SEC_CODE)
2249 minfo ("x");
2250
2251 if (flag & SEC_READONLY)
2252 minfo ("r");
2253
2254 if (flag & SEC_DATA)
2255 minfo ("w");
2256
2257 if (flag & SEC_LOAD)
2258 minfo ("l");
2259 }
2260
2261 void
2262 lang_map (void)
2263 {
2264 lang_memory_region_type *m;
2265 bool dis_header_printed = false;
2266
2267 LANG_FOR_EACH_INPUT_STATEMENT (file)
2268 {
2269 asection *s;
2270
2271 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
2272 || file->flags.just_syms)
2273 continue;
2274
2275 if (config.print_map_discarded)
2276 for (s = file->the_bfd->sections; s != NULL; s = s->next)
2277 if ((s->output_section == NULL
2278 || s->output_section->owner != link_info.output_bfd)
2279 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
2280 {
2281 if (! dis_header_printed)
2282 {
2283 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
2284 dis_header_printed = true;
2285 }
2286
2287 print_input_section (s, true);
2288 }
2289 }
2290
2291 minfo (_("\nMemory Configuration\n\n"));
2292 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
2293 _("Name"), _("Origin"), _("Length"), _("Attributes"));
2294
2295 for (m = lang_memory_region_list; m != NULL; m = m->next)
2296 {
2297 fprintf (config.map_file, "%-16s", m->name_list.name);
2298
2299 char buf[32];
2300 bfd_sprintf_vma (link_info.output_bfd, buf, m->origin);
2301 fprintf (config.map_file, " 0x%-16s", buf);
2302 bfd_sprintf_vma (link_info.output_bfd, buf, m->length);
2303 fprintf (config.map_file,
2304 " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf);
2305 if (m->flags)
2306 lang_map_flags (m->flags);
2307
2308 if (m->not_flags)
2309 {
2310 minfo ("!");
2311 lang_map_flags (m->not_flags);
2312 }
2313
2314 print_nl ();
2315 }
2316
2317 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
2318
2319 if (!link_info.reduce_memory_overheads)
2320 {
2321 obstack_begin (&map_obstack, 1000);
2322 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
2323 }
2324 expld.phase = lang_fixed_phase_enum;
2325 lang_statement_iteration++;
2326 print_statements ();
2327
2328 ldemul_extra_map_file_text (link_info.output_bfd, &link_info,
2329 config.map_file);
2330 }
2331
2332 static bool
2333 sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
2334 void *info ATTRIBUTE_UNUSED)
2335 {
2336 if ((hash_entry->type == bfd_link_hash_defined
2337 || hash_entry->type == bfd_link_hash_defweak)
2338 && hash_entry->u.def.section->owner != link_info.output_bfd
2339 && hash_entry->u.def.section->owner != NULL)
2340 {
2341 input_section_userdata_type *ud;
2342 struct map_symbol_def *def;
2343
2344 ud = bfd_section_userdata (hash_entry->u.def.section);
2345 if (!ud)
2346 {
2347 ud = stat_alloc (sizeof (*ud));
2348 bfd_set_section_userdata (hash_entry->u.def.section, ud);
2349 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2350 ud->map_symbol_def_count = 0;
2351 }
2352 else if (!ud->map_symbol_def_tail)
2353 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
2354
2355 def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
2356 def->entry = hash_entry;
2357 *(ud->map_symbol_def_tail) = def;
2358 ud->map_symbol_def_tail = &def->next;
2359 ud->map_symbol_def_count++;
2360 }
2361 return true;
2362 }
2363
2364 /* Initialize an output section. */
2365
2366 static void
2367 init_os (lang_output_section_statement_type *s, flagword flags)
2368 {
2369 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
2370 einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
2371
2372 if (!s->dup_output)
2373 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
2374 if (s->bfd_section == NULL)
2375 s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
2376 s->name, flags);
2377 if (s->bfd_section == NULL)
2378 {
2379 einfo (_("%F%P: output format %s cannot represent section"
2380 " called %s: %E\n"),
2381 link_info.output_bfd->xvec->name, s->name);
2382 }
2383 s->bfd_section->output_section = s->bfd_section;
2384 s->bfd_section->output_offset = 0;
2385
2386 /* Set the userdata of the output section to the output section
2387 statement to avoid lookup. */
2388 bfd_set_section_userdata (s->bfd_section, s);
2389
2390 /* If there is a base address, make sure that any sections it might
2391 mention are initialized. */
2392 if (s->addr_tree != NULL)
2393 exp_init_os (s->addr_tree);
2394
2395 if (s->load_base != NULL)
2396 exp_init_os (s->load_base);
2397
2398 /* If supplied an alignment, set it. */
2399 if (s->section_alignment != NULL)
2400 s->bfd_section->alignment_power = exp_get_power (s->section_alignment,
2401 "section alignment");
2402 }
2403
2404 /* Make sure that all output sections mentioned in an expression are
2405 initialized. */
2406
2407 static void
2408 exp_init_os (etree_type *exp)
2409 {
2410 switch (exp->type.node_class)
2411 {
2412 case etree_assign:
2413 case etree_provide:
2414 case etree_provided:
2415 exp_init_os (exp->assign.src);
2416 break;
2417
2418 case etree_binary:
2419 exp_init_os (exp->binary.lhs);
2420 exp_init_os (exp->binary.rhs);
2421 break;
2422
2423 case etree_trinary:
2424 exp_init_os (exp->trinary.cond);
2425 exp_init_os (exp->trinary.lhs);
2426 exp_init_os (exp->trinary.rhs);
2427 break;
2428
2429 case etree_assert:
2430 exp_init_os (exp->assert_s.child);
2431 break;
2432
2433 case etree_unary:
2434 exp_init_os (exp->unary.child);
2435 break;
2436
2437 case etree_name:
2438 switch (exp->type.node_code)
2439 {
2440 case ADDR:
2441 case LOADADDR:
2442 {
2443 lang_output_section_statement_type *os;
2444
2445 os = lang_output_section_find (exp->name.name);
2446 if (os != NULL && os->bfd_section == NULL)
2447 init_os (os, 0);
2448 }
2449 }
2450 break;
2451
2452 default:
2453 break;
2454 }
2455 }
2456 \f
2457 static void
2458 section_already_linked (bfd *abfd, asection *sec, void *data)
2459 {
2460 lang_input_statement_type *entry = (lang_input_statement_type *) data;
2461
2462 /* If we are only reading symbols from this object, then we want to
2463 discard all sections. */
2464 if (entry->flags.just_syms)
2465 {
2466 bfd_link_just_syms (abfd, sec, &link_info);
2467 return;
2468 }
2469
2470 /* Deal with SHF_EXCLUDE ELF sections. */
2471 if (!bfd_link_relocatable (&link_info)
2472 && (abfd->flags & BFD_PLUGIN) == 0
2473 && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2474 sec->output_section = bfd_abs_section_ptr;
2475
2476 if (!(abfd->flags & DYNAMIC))
2477 bfd_section_already_linked (abfd, sec, &link_info);
2478 }
2479 \f
2480
2481 /* Returns true if SECTION is one we know will be discarded based on its
2482 section flags, otherwise returns false. */
2483
2484 static bool
2485 lang_discard_section_p (asection *section)
2486 {
2487 bool discard;
2488 flagword flags = section->flags;
2489
2490 /* Discard sections marked with SEC_EXCLUDE. */
2491 discard = (flags & SEC_EXCLUDE) != 0;
2492
2493 /* Discard the group descriptor sections when we're finally placing the
2494 sections from within the group. */
2495 if ((flags & SEC_GROUP) != 0
2496 && link_info.resolve_section_groups)
2497 discard = true;
2498
2499 /* Discard debugging sections if we are stripping debugging
2500 information. */
2501 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2502 && (flags & SEC_DEBUGGING) != 0)
2503 discard = true;
2504
2505 return discard;
2506 }
2507
2508 /* The wild routines.
2509
2510 These expand statements like *(.text) and foo.o to a list of
2511 explicit actions, like foo.o(.text), bar.o(.text) and
2512 foo.o(.text, .data). */
2513
2514 /* Add SECTION to the output section OUTPUT. Do this by creating a
2515 lang_input_section statement which is placed at PTR. */
2516
2517 void
2518 lang_add_section (lang_statement_list_type *ptr,
2519 asection *section,
2520 struct wildcard_list *pattern,
2521 struct flag_info *sflag_info,
2522 lang_output_section_statement_type *output)
2523 {
2524 flagword flags = section->flags;
2525
2526 bool discard;
2527 lang_input_section_type *new_section;
2528 bfd *abfd = link_info.output_bfd;
2529
2530 /* Is this section one we know should be discarded? */
2531 discard = lang_discard_section_p (section);
2532
2533 /* Discard input sections which are assigned to a section named
2534 DISCARD_SECTION_NAME. */
2535 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2536 discard = true;
2537
2538 if (discard)
2539 {
2540 if (section->output_section == NULL)
2541 {
2542 /* This prevents future calls from assigning this section. */
2543 section->output_section = bfd_abs_section_ptr;
2544 }
2545 else if (link_info.non_contiguous_regions_warnings)
2546 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
2547 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
2548 NULL, section, section->owner);
2549
2550 return;
2551 }
2552
2553 if (sflag_info)
2554 {
2555 bool keep;
2556
2557 keep = bfd_lookup_section_flags (&link_info, sflag_info, section);
2558 if (!keep)
2559 return;
2560 }
2561
2562 if (section->output_section != NULL)
2563 {
2564 if (!link_info.non_contiguous_regions)
2565 return;
2566
2567 /* SECTION has already been handled in a special way
2568 (eg. LINK_ONCE): skip it. */
2569 if (bfd_is_abs_section (section->output_section))
2570 return;
2571
2572 /* Already assigned to the same output section, do not process
2573 it again, to avoid creating loops between duplicate sections
2574 later. */
2575 if (section->output_section == output->bfd_section)
2576 return;
2577
2578 if (link_info.non_contiguous_regions_warnings && output->bfd_section)
2579 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
2580 "change behaviour for section `%pA' from `%pB' (assigned to "
2581 "%pA, but additional match: %pA)\n"),
2582 NULL, section, section->owner, section->output_section,
2583 output->bfd_section);
2584
2585 /* SECTION has already been assigned to an output section, but
2586 the user allows it to be mapped to another one in case it
2587 overflows. We'll later update the actual output section in
2588 size_input_section as appropriate. */
2589 }
2590
2591 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2592 to an output section, because we want to be able to include a
2593 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2594 section (I don't know why we want to do this, but we do).
2595 build_link_order in ldwrite.c handles this case by turning
2596 the embedded SEC_NEVER_LOAD section into a fill. */
2597 flags &= ~ SEC_NEVER_LOAD;
2598
2599 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2600 already been processed. One reason to do this is that on pe
2601 format targets, .text$foo sections go into .text and it's odd
2602 to see .text with SEC_LINK_ONCE set. */
2603 if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP))
2604 {
2605 if (link_info.resolve_section_groups)
2606 flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
2607 else
2608 flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC);
2609 }
2610 else if (!bfd_link_relocatable (&link_info))
2611 flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
2612
2613 switch (output->sectype)
2614 {
2615 case normal_section:
2616 case overlay_section:
2617 case first_overlay_section:
2618 case type_section:
2619 break;
2620 case noalloc_section:
2621 flags &= ~SEC_ALLOC;
2622 break;
2623 case typed_readonly_section:
2624 case readonly_section:
2625 flags |= SEC_READONLY;
2626 break;
2627 case noload_section:
2628 flags &= ~SEC_LOAD;
2629 flags |= SEC_NEVER_LOAD;
2630 /* Unfortunately GNU ld has managed to evolve two different
2631 meanings to NOLOAD in scripts. ELF gets a .bss style noload,
2632 alloc, no contents section. All others get a noload, noalloc
2633 section. */
2634 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
2635 flags &= ~SEC_HAS_CONTENTS;
2636 else
2637 flags &= ~SEC_ALLOC;
2638 break;
2639 }
2640
2641 if (output->bfd_section == NULL)
2642 init_os (output, flags);
2643
2644 /* If SEC_READONLY is not set in the input section, then clear
2645 it from the output section. */
2646 output->bfd_section->flags &= flags | ~SEC_READONLY;
2647
2648 if (output->bfd_section->linker_has_input)
2649 {
2650 /* Only set SEC_READONLY flag on the first input section. */
2651 flags &= ~ SEC_READONLY;
2652
2653 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2654 if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2655 != (flags & (SEC_MERGE | SEC_STRINGS))
2656 || ((flags & SEC_MERGE) != 0
2657 && output->bfd_section->entsize != section->entsize))
2658 {
2659 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2660 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2661 }
2662 }
2663 output->bfd_section->flags |= flags;
2664
2665 if (!output->bfd_section->linker_has_input)
2666 {
2667 output->bfd_section->linker_has_input = 1;
2668 /* This must happen after flags have been updated. The output
2669 section may have been created before we saw its first input
2670 section, eg. for a data statement. */
2671 bfd_init_private_section_data (section->owner, section,
2672 link_info.output_bfd,
2673 output->bfd_section,
2674 &link_info);
2675 if ((flags & SEC_MERGE) != 0)
2676 output->bfd_section->entsize = section->entsize;
2677 }
2678
2679 if ((flags & SEC_TIC54X_BLOCK) != 0
2680 && bfd_get_arch (section->owner) == bfd_arch_tic54x)
2681 {
2682 /* FIXME: This value should really be obtained from the bfd... */
2683 output->block_value = 128;
2684 }
2685
2686 /* When a .ctors section is placed in .init_array it must be copied
2687 in reverse order. Similarly for .dtors. Set that up. */
2688 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
2689 && ((startswith (section->name, ".ctors")
2690 && strcmp (output->bfd_section->name, ".init_array") == 0)
2691 || (startswith (section->name, ".dtors")
2692 && strcmp (output->bfd_section->name, ".fini_array") == 0))
2693 && (section->name[6] == 0 || section->name[6] == '.'))
2694 section->flags |= SEC_ELF_REVERSE_COPY;
2695
2696 if (section->alignment_power > output->bfd_section->alignment_power)
2697 output->bfd_section->alignment_power = section->alignment_power;
2698
2699 section->output_section = output->bfd_section;
2700
2701 if (!map_head_is_link_order)
2702 {
2703 asection *s = output->bfd_section->map_tail.s;
2704 output->bfd_section->map_tail.s = section;
2705 section->map_head.s = NULL;
2706 section->map_tail.s = s;
2707 if (s != NULL)
2708 s->map_head.s = section;
2709 else
2710 output->bfd_section->map_head.s = section;
2711 }
2712
2713 /* Add a section reference to the list. */
2714 new_section = new_stat (lang_input_section, ptr);
2715 new_section->section = section;
2716 new_section->pattern = pattern;
2717 }
2718
2719 /* Expand a wild statement for a particular FILE. SECTION may be
2720 NULL, in which case it is a wild card. This assumes that the
2721 wild statement doesn't need any sorting (of filenames or sections). */
2722
2723 static void
2724 output_section_callback_nosort (lang_wild_statement_type *ptr,
2725 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2726 asection *section,
2727 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2728 void *output)
2729 {
2730 lang_output_section_statement_type *os;
2731
2732 os = (lang_output_section_statement_type *) output;
2733
2734 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2735 if (unique_section_p (section, os))
2736 return;
2737
2738 lang_add_section (&ptr->children, section, ptr->section_list,
2739 ptr->section_flag_list, os);
2740 }
2741
2742 /* Check if all sections in a wild statement for a particular FILE
2743 are readonly. */
2744
2745 static void
2746 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2747 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2748 asection *section,
2749 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2750 void *output)
2751 {
2752 lang_output_section_statement_type *os;
2753
2754 os = (lang_output_section_statement_type *) output;
2755
2756 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2757 if (unique_section_p (section, os))
2758 return;
2759
2760 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2761 os->all_input_readonly = false;
2762 }
2763
2764 /* This is passed a file name which must have been seen already and
2765 added to the statement tree. We will see if it has been opened
2766 already and had its symbols read. If not then we'll read it. */
2767
2768 static lang_input_statement_type *
2769 lookup_name (const char *name)
2770 {
2771 lang_input_statement_type *search;
2772
2773 for (search = (void *) input_file_chain.head;
2774 search != NULL;
2775 search = search->next_real_file)
2776 {
2777 /* Use the local_sym_name as the name of the file that has
2778 already been loaded as filename might have been transformed
2779 via the search directory lookup mechanism. */
2780 const char *filename = search->local_sym_name;
2781
2782 if (filename != NULL
2783 && filename_cmp (filename, name) == 0)
2784 break;
2785 }
2786
2787 if (search == NULL)
2788 {
2789 /* Arrange to splice the input statement added by new_afile into
2790 statement_list after the current input_file_chain tail.
2791 We know input_file_chain is not an empty list, and that
2792 lookup_name was called via open_input_bfds. Later calls to
2793 lookup_name should always match an existing input_statement. */
2794 lang_statement_union_type **tail = stat_ptr->tail;
2795 lang_statement_union_type **after
2796 = (void *) ((char *) input_file_chain.tail
2797 - offsetof (lang_input_statement_type, next_real_file)
2798 + offsetof (lang_input_statement_type, header.next));
2799 lang_statement_union_type *rest = *after;
2800 stat_ptr->tail = after;
2801 search = new_afile (name, lang_input_file_is_search_file_enum,
2802 default_target, NULL);
2803 *stat_ptr->tail = rest;
2804 if (*tail == NULL)
2805 stat_ptr->tail = tail;
2806 }
2807
2808 /* If we have already added this file, or this file is not real
2809 don't add this file. */
2810 if (search->flags.loaded || !search->flags.real)
2811 return search;
2812
2813 if (!load_symbols (search, NULL))
2814 return NULL;
2815
2816 return search;
2817 }
2818
2819 /* Save LIST as a list of libraries whose symbols should not be exported. */
2820
2821 struct excluded_lib
2822 {
2823 char *name;
2824 struct excluded_lib *next;
2825 };
2826 static struct excluded_lib *excluded_libs;
2827
2828 void
2829 add_excluded_libs (const char *list)
2830 {
2831 const char *p = list, *end;
2832
2833 while (*p != '\0')
2834 {
2835 struct excluded_lib *entry;
2836 end = strpbrk (p, ",:");
2837 if (end == NULL)
2838 end = p + strlen (p);
2839 entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
2840 entry->next = excluded_libs;
2841 entry->name = (char *) xmalloc (end - p + 1);
2842 memcpy (entry->name, p, end - p);
2843 entry->name[end - p] = '\0';
2844 excluded_libs = entry;
2845 if (*end == '\0')
2846 break;
2847 p = end + 1;
2848 }
2849 }
2850
2851 static void
2852 check_excluded_libs (bfd *abfd)
2853 {
2854 struct excluded_lib *lib = excluded_libs;
2855
2856 while (lib)
2857 {
2858 int len = strlen (lib->name);
2859 const char *filename = lbasename (bfd_get_filename (abfd));
2860
2861 if (strcmp (lib->name, "ALL") == 0)
2862 {
2863 abfd->no_export = true;
2864 return;
2865 }
2866
2867 if (filename_ncmp (lib->name, filename, len) == 0
2868 && (filename[len] == '\0'
2869 || (filename[len] == '.' && filename[len + 1] == 'a'
2870 && filename[len + 2] == '\0')))
2871 {
2872 abfd->no_export = true;
2873 return;
2874 }
2875
2876 lib = lib->next;
2877 }
2878 }
2879
2880 /* Get the symbols for an input file. */
2881
2882 bool
2883 load_symbols (lang_input_statement_type *entry,
2884 lang_statement_list_type *place)
2885 {
2886 char **matching;
2887
2888 if (entry->flags.loaded)
2889 return true;
2890
2891 ldfile_open_file (entry);
2892
2893 /* Do not process further if the file was missing. */
2894 if (entry->flags.missing_file)
2895 return true;
2896
2897 if (trace_files || verbose)
2898 info_msg ("%pI\n", entry);
2899
2900 if (!bfd_check_format (entry->the_bfd, bfd_archive)
2901 && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2902 {
2903 bfd_error_type err;
2904 struct lang_input_statement_flags save_flags;
2905 extern FILE *yyin;
2906
2907 err = bfd_get_error ();
2908
2909 /* See if the emulation has some special knowledge. */
2910 if (ldemul_unrecognized_file (entry))
2911 {
2912 if (err == bfd_error_file_ambiguously_recognized)
2913 free (matching);
2914 return true;
2915 }
2916
2917 if (err == bfd_error_file_ambiguously_recognized)
2918 {
2919 char **p;
2920
2921 einfo (_("%P: %pB: file not recognized: %E;"
2922 " matching formats:"), entry->the_bfd);
2923 for (p = matching; *p != NULL; p++)
2924 einfo (" %s", *p);
2925 free (matching);
2926 einfo ("%F\n");
2927 }
2928 else if (err != bfd_error_file_not_recognized
2929 || place == NULL)
2930 einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd);
2931
2932 bfd_close (entry->the_bfd);
2933 entry->the_bfd = NULL;
2934
2935 /* Try to interpret the file as a linker script. */
2936 save_flags = input_flags;
2937 ldfile_open_command_file (entry->filename);
2938
2939 push_stat_ptr (place);
2940 input_flags.add_DT_NEEDED_for_regular
2941 = entry->flags.add_DT_NEEDED_for_regular;
2942 input_flags.add_DT_NEEDED_for_dynamic
2943 = entry->flags.add_DT_NEEDED_for_dynamic;
2944 input_flags.whole_archive = entry->flags.whole_archive;
2945 input_flags.dynamic = entry->flags.dynamic;
2946
2947 ldfile_assumed_script = true;
2948 parser_input = input_script;
2949 current_input_file = entry->filename;
2950 yyparse ();
2951 current_input_file = NULL;
2952 ldfile_assumed_script = false;
2953
2954 /* missing_file is sticky. sysrooted will already have been
2955 restored when seeing EOF in yyparse, but no harm to restore
2956 again. */
2957 save_flags.missing_file |= input_flags.missing_file;
2958 input_flags = save_flags;
2959 pop_stat_ptr ();
2960 fclose (yyin);
2961 yyin = NULL;
2962 entry->flags.loaded = true;
2963
2964 return true;
2965 }
2966
2967 if (ldemul_recognized_file (entry))
2968 return true;
2969
2970 /* We don't call ldlang_add_file for an archive. Instead, the
2971 add_symbols entry point will call ldlang_add_file, via the
2972 add_archive_element callback, for each element of the archive
2973 which is used. */
2974 switch (bfd_get_format (entry->the_bfd))
2975 {
2976 default:
2977 break;
2978
2979 case bfd_object:
2980 if (!entry->flags.reload)
2981 ldlang_add_file (entry);
2982 break;
2983
2984 case bfd_archive:
2985 check_excluded_libs (entry->the_bfd);
2986
2987 bfd_set_usrdata (entry->the_bfd, entry);
2988 if (entry->flags.whole_archive)
2989 {
2990 bfd *member = NULL;
2991 bool loaded = true;
2992
2993 for (;;)
2994 {
2995 bfd *subsbfd;
2996 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2997
2998 if (member == NULL)
2999 break;
3000
3001 if (!bfd_check_format (member, bfd_object))
3002 {
3003 einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
3004 entry->the_bfd, member);
3005 loaded = false;
3006 }
3007
3008 subsbfd = member;
3009 if (!(*link_info.callbacks
3010 ->add_archive_element) (&link_info, member,
3011 "--whole-archive", &subsbfd))
3012 abort ();
3013
3014 /* Potentially, the add_archive_element hook may have set a
3015 substitute BFD for us. */
3016 if (!bfd_link_add_symbols (subsbfd, &link_info))
3017 {
3018 einfo (_("%F%P: %pB: error adding symbols: %E\n"), member);
3019 loaded = false;
3020 }
3021 }
3022
3023 entry->flags.loaded = loaded;
3024 return loaded;
3025 }
3026 break;
3027 }
3028
3029 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
3030 entry->flags.loaded = true;
3031 else
3032 einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd);
3033
3034 return entry->flags.loaded;
3035 }
3036
3037 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
3038 may be NULL, indicating that it is a wildcard. Separate
3039 lang_input_section statements are created for each part of the
3040 expansion; they are added after the wild statement S. OUTPUT is
3041 the output section. */
3042
3043 static void
3044 wild (lang_wild_statement_type *s,
3045 const char *target ATTRIBUTE_UNUSED,
3046 lang_output_section_statement_type *output)
3047 {
3048 struct wildcard_list *sec;
3049
3050 if (s->filenames_sorted || s->any_specs_sorted)
3051 {
3052 lang_section_bst_type *tree;
3053
3054 walk_wild (s, output_section_callback_sort, output);
3055
3056 tree = s->tree;
3057 if (tree)
3058 {
3059 output_section_callback_tree_to_list (s, tree, output);
3060 s->tree = NULL;
3061 s->rightmost = &s->tree;
3062 }
3063 }
3064 else
3065 walk_wild (s, output_section_callback_nosort, output);
3066
3067 if (default_common_section == NULL)
3068 for (sec = s->section_list; sec != NULL; sec = sec->next)
3069 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
3070 {
3071 /* Remember the section that common is going to in case we
3072 later get something which doesn't know where to put it. */
3073 default_common_section = output;
3074 break;
3075 }
3076 }
3077
3078 /* Return TRUE iff target is the sought target. */
3079
3080 static int
3081 get_target (const bfd_target *target, void *data)
3082 {
3083 const char *sought = (const char *) data;
3084
3085 return strcmp (target->name, sought) == 0;
3086 }
3087
3088 /* Like strcpy() but convert to lower case as well. */
3089
3090 static void
3091 stricpy (char *dest, const char *src)
3092 {
3093 char c;
3094
3095 while ((c = *src++) != 0)
3096 *dest++ = TOLOWER (c);
3097
3098 *dest = 0;
3099 }
3100
3101 /* Remove the first occurrence of needle (if any) in haystack
3102 from haystack. */
3103
3104 static void
3105 strcut (char *haystack, const char *needle)
3106 {
3107 haystack = strstr (haystack, needle);
3108
3109 if (haystack)
3110 {
3111 char *src;
3112
3113 for (src = haystack + strlen (needle); *src;)
3114 *haystack++ = *src++;
3115
3116 *haystack = 0;
3117 }
3118 }
3119
3120 /* Compare two target format name strings.
3121 Return a value indicating how "similar" they are. */
3122
3123 static int
3124 name_compare (const char *first, const char *second)
3125 {
3126 char *copy1;
3127 char *copy2;
3128 int result;
3129
3130 copy1 = (char *) xmalloc (strlen (first) + 1);
3131 copy2 = (char *) xmalloc (strlen (second) + 1);
3132
3133 /* Convert the names to lower case. */
3134 stricpy (copy1, first);
3135 stricpy (copy2, second);
3136
3137 /* Remove size and endian strings from the name. */
3138 strcut (copy1, "big");
3139 strcut (copy1, "little");
3140 strcut (copy2, "big");
3141 strcut (copy2, "little");
3142
3143 /* Return a value based on how many characters match,
3144 starting from the beginning. If both strings are
3145 the same then return 10 * their length. */
3146 for (result = 0; copy1[result] == copy2[result]; result++)
3147 if (copy1[result] == 0)
3148 {
3149 result *= 10;
3150 break;
3151 }
3152
3153 free (copy1);
3154 free (copy2);
3155
3156 return result;
3157 }
3158
3159 /* Set by closest_target_match() below. */
3160 static const bfd_target *winner;
3161
3162 /* Scan all the valid bfd targets looking for one that has the endianness
3163 requirement that was specified on the command line, and is the nearest
3164 match to the original output target. */
3165
3166 static int
3167 closest_target_match (const bfd_target *target, void *data)
3168 {
3169 const bfd_target *original = (const bfd_target *) data;
3170
3171 if (command_line.endian == ENDIAN_BIG
3172 && target->byteorder != BFD_ENDIAN_BIG)
3173 return 0;
3174
3175 if (command_line.endian == ENDIAN_LITTLE
3176 && target->byteorder != BFD_ENDIAN_LITTLE)
3177 return 0;
3178
3179 /* Must be the same flavour. */
3180 if (target->flavour != original->flavour)
3181 return 0;
3182
3183 /* Ignore generic big and little endian elf vectors. */
3184 if (strcmp (target->name, "elf32-big") == 0
3185 || strcmp (target->name, "elf64-big") == 0
3186 || strcmp (target->name, "elf32-little") == 0
3187 || strcmp (target->name, "elf64-little") == 0)
3188 return 0;
3189
3190 /* If we have not found a potential winner yet, then record this one. */
3191 if (winner == NULL)
3192 {
3193 winner = target;
3194 return 0;
3195 }
3196
3197 /* Oh dear, we now have two potential candidates for a successful match.
3198 Compare their names and choose the better one. */
3199 if (name_compare (target->name, original->name)
3200 > name_compare (winner->name, original->name))
3201 winner = target;
3202
3203 /* Keep on searching until wqe have checked them all. */
3204 return 0;
3205 }
3206
3207 /* Return the BFD target format of the first input file. */
3208
3209 static const char *
3210 get_first_input_target (void)
3211 {
3212 const char *target = NULL;
3213
3214 LANG_FOR_EACH_INPUT_STATEMENT (s)
3215 {
3216 if (s->header.type == lang_input_statement_enum
3217 && s->flags.real)
3218 {
3219 ldfile_open_file (s);
3220
3221 if (s->the_bfd != NULL
3222 && bfd_check_format (s->the_bfd, bfd_object))
3223 {
3224 target = bfd_get_target (s->the_bfd);
3225
3226 if (target != NULL)
3227 break;
3228 }
3229 }
3230 }
3231
3232 return target;
3233 }
3234
3235 const char *
3236 lang_get_output_target (void)
3237 {
3238 const char *target;
3239
3240 /* Has the user told us which output format to use? */
3241 if (output_target != NULL)
3242 return output_target;
3243
3244 /* No - has the current target been set to something other than
3245 the default? */
3246 if (current_target != default_target && current_target != NULL)
3247 return current_target;
3248
3249 /* No - can we determine the format of the first input file? */
3250 target = get_first_input_target ();
3251 if (target != NULL)
3252 return target;
3253
3254 /* Failed - use the default output target. */
3255 return default_target;
3256 }
3257
3258 /* Open the output file. */
3259
3260 static void
3261 open_output (const char *name)
3262 {
3263 lang_input_statement_type *f;
3264 char *out = lrealpath (name);
3265
3266 for (f = (void *) input_file_chain.head;
3267 f != NULL;
3268 f = f->next_real_file)
3269 if (f->flags.real)
3270 {
3271 char *in = lrealpath (f->local_sym_name);
3272 if (filename_cmp (in, out) == 0)
3273 einfo (_("%F%P: input file '%s' is the same as output file\n"),
3274 f->filename);
3275 free (in);
3276 }
3277 free (out);
3278
3279 output_target = lang_get_output_target ();
3280
3281 /* Has the user requested a particular endianness on the command
3282 line? */
3283 if (command_line.endian != ENDIAN_UNSET)
3284 {
3285 /* Get the chosen target. */
3286 const bfd_target *target
3287 = bfd_iterate_over_targets (get_target, (void *) output_target);
3288
3289 /* If the target is not supported, we cannot do anything. */
3290 if (target != NULL)
3291 {
3292 enum bfd_endian desired_endian;
3293
3294 if (command_line.endian == ENDIAN_BIG)
3295 desired_endian = BFD_ENDIAN_BIG;
3296 else
3297 desired_endian = BFD_ENDIAN_LITTLE;
3298
3299 /* See if the target has the wrong endianness. This should
3300 not happen if the linker script has provided big and
3301 little endian alternatives, but some scrips don't do
3302 this. */
3303 if (target->byteorder != desired_endian)
3304 {
3305 /* If it does, then see if the target provides
3306 an alternative with the correct endianness. */
3307 if (target->alternative_target != NULL
3308 && (target->alternative_target->byteorder == desired_endian))
3309 output_target = target->alternative_target->name;
3310 else
3311 {
3312 /* Try to find a target as similar as possible to
3313 the default target, but which has the desired
3314 endian characteristic. */
3315 bfd_iterate_over_targets (closest_target_match,
3316 (void *) target);
3317
3318 /* Oh dear - we could not find any targets that
3319 satisfy our requirements. */
3320 if (winner == NULL)
3321 einfo (_("%P: warning: could not find any targets"
3322 " that match endianness requirement\n"));
3323 else
3324 output_target = winner->name;
3325 }
3326 }
3327 }
3328 }
3329
3330 link_info.output_bfd = bfd_openw (name, output_target);
3331
3332 if (link_info.output_bfd == NULL)
3333 {
3334 if (bfd_get_error () == bfd_error_invalid_target)
3335 einfo (_("%F%P: target %s not found\n"), output_target);
3336
3337 einfo (_("%F%P: cannot open output file %s: %E\n"), name);
3338 }
3339
3340 delete_output_file_on_failure = true;
3341
3342 if (!bfd_set_format (link_info.output_bfd, bfd_object))
3343 einfo (_("%F%P: %s: can not make object file: %E\n"), name);
3344 if (!bfd_set_arch_mach (link_info.output_bfd,
3345 ldfile_output_architecture,
3346 ldfile_output_machine))
3347 einfo (_("%F%P: %s: can not set architecture: %E\n"), name);
3348
3349 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
3350 if (link_info.hash == NULL)
3351 einfo (_("%F%P: can not create hash table: %E\n"));
3352
3353 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
3354 }
3355
3356 static void
3357 ldlang_open_output (lang_statement_union_type *statement)
3358 {
3359 switch (statement->header.type)
3360 {
3361 case lang_output_statement_enum:
3362 ASSERT (link_info.output_bfd == NULL);
3363 open_output (statement->output_statement.name);
3364 ldemul_set_output_arch ();
3365 if (config.magic_demand_paged
3366 && !bfd_link_relocatable (&link_info))
3367 link_info.output_bfd->flags |= D_PAGED;
3368 else
3369 link_info.output_bfd->flags &= ~D_PAGED;
3370 if (config.text_read_only)
3371 link_info.output_bfd->flags |= WP_TEXT;
3372 else
3373 link_info.output_bfd->flags &= ~WP_TEXT;
3374 if (link_info.traditional_format)
3375 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
3376 else
3377 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
3378 break;
3379
3380 case lang_target_statement_enum:
3381 current_target = statement->target_statement.target;
3382 break;
3383 default:
3384 break;
3385 }
3386 }
3387
3388 static void
3389 init_opb (asection *s)
3390 {
3391 unsigned int x;
3392
3393 opb_shift = 0;
3394 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
3395 && s != NULL
3396 && (s->flags & SEC_ELF_OCTETS) != 0)
3397 return;
3398
3399 x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3400 ldfile_output_machine);
3401 if (x > 1)
3402 while ((x & 1) == 0)
3403 {
3404 x >>= 1;
3405 ++opb_shift;
3406 }
3407 ASSERT (x == 1);
3408 }
3409
3410 /* Open all the input files. */
3411
3412 enum open_bfd_mode
3413 {
3414 OPEN_BFD_NORMAL = 0,
3415 OPEN_BFD_FORCE = 1,
3416 OPEN_BFD_RESCAN = 2
3417 };
3418 #if BFD_SUPPORTS_PLUGINS
3419 static lang_input_statement_type *plugin_insert = NULL;
3420 static struct bfd_link_hash_entry *plugin_undefs = NULL;
3421 #endif
3422
3423 static void
3424 open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
3425 {
3426 for (; s != NULL; s = s->header.next)
3427 {
3428 switch (s->header.type)
3429 {
3430 case lang_constructors_statement_enum:
3431 open_input_bfds (constructor_list.head, mode);
3432 break;
3433 case lang_output_section_statement_enum:
3434 open_input_bfds (s->output_section_statement.children.head, mode);
3435 break;
3436 case lang_wild_statement_enum:
3437 /* Maybe we should load the file's symbols. */
3438 if ((mode & OPEN_BFD_RESCAN) == 0
3439 && s->wild_statement.filename
3440 && !wildcardp (s->wild_statement.filename)
3441 && !archive_path (s->wild_statement.filename))
3442 lookup_name (s->wild_statement.filename);
3443 open_input_bfds (s->wild_statement.children.head, mode);
3444 break;
3445 case lang_group_statement_enum:
3446 {
3447 struct bfd_link_hash_entry *undefs;
3448 #if BFD_SUPPORTS_PLUGINS
3449 lang_input_statement_type *plugin_insert_save;
3450 #endif
3451
3452 /* We must continually search the entries in the group
3453 until no new symbols are added to the list of undefined
3454 symbols. */
3455
3456 do
3457 {
3458 #if BFD_SUPPORTS_PLUGINS
3459 plugin_insert_save = plugin_insert;
3460 #endif
3461 undefs = link_info.hash->undefs_tail;
3462 open_input_bfds (s->group_statement.children.head,
3463 mode | OPEN_BFD_FORCE);
3464 }
3465 while (undefs != link_info.hash->undefs_tail
3466 #if BFD_SUPPORTS_PLUGINS
3467 /* Objects inserted by a plugin, which are loaded
3468 before we hit this loop, may have added new
3469 undefs. */
3470 || (plugin_insert != plugin_insert_save && plugin_undefs)
3471 #endif
3472 );
3473 }
3474 break;
3475 case lang_target_statement_enum:
3476 current_target = s->target_statement.target;
3477 break;
3478 case lang_input_statement_enum:
3479 if (s->input_statement.flags.real)
3480 {
3481 lang_statement_union_type **os_tail;
3482 lang_statement_list_type add;
3483 bfd *abfd;
3484
3485 s->input_statement.target = current_target;
3486
3487 /* If we are being called from within a group, and this
3488 is an archive which has already been searched, then
3489 force it to be researched unless the whole archive
3490 has been loaded already. Do the same for a rescan.
3491 Likewise reload --as-needed shared libs. */
3492 if (mode != OPEN_BFD_NORMAL
3493 #if BFD_SUPPORTS_PLUGINS
3494 && ((mode & OPEN_BFD_RESCAN) == 0
3495 || plugin_insert == NULL)
3496 #endif
3497 && s->input_statement.flags.loaded
3498 && (abfd = s->input_statement.the_bfd) != NULL
3499 && ((bfd_get_format (abfd) == bfd_archive
3500 && !s->input_statement.flags.whole_archive)
3501 || (bfd_get_format (abfd) == bfd_object
3502 && ((abfd->flags) & DYNAMIC) != 0
3503 && s->input_statement.flags.add_DT_NEEDED_for_regular
3504 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
3505 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)))
3506 {
3507 s->input_statement.flags.loaded = false;
3508 s->input_statement.flags.reload = true;
3509 }
3510
3511 os_tail = lang_os_list.tail;
3512 lang_list_init (&add);
3513
3514 if (!load_symbols (&s->input_statement, &add))
3515 config.make_executable = false;
3516
3517 if (add.head != NULL)
3518 {
3519 /* If this was a script with output sections then
3520 tack any added statements on to the end of the
3521 list. This avoids having to reorder the output
3522 section statement list. Very likely the user
3523 forgot -T, and whatever we do here will not meet
3524 naive user expectations. */
3525 if (os_tail != lang_os_list.tail)
3526 {
3527 einfo (_("%P: warning: %s contains output sections;"
3528 " did you forget -T?\n"),
3529 s->input_statement.filename);
3530 *stat_ptr->tail = add.head;
3531 stat_ptr->tail = add.tail;
3532 }
3533 else
3534 {
3535 *add.tail = s->header.next;
3536 s->header.next = add.head;
3537 }
3538 }
3539 }
3540 #if BFD_SUPPORTS_PLUGINS
3541 /* If we have found the point at which a plugin added new
3542 files, clear plugin_insert to enable archive rescan. */
3543 if (&s->input_statement == plugin_insert)
3544 plugin_insert = NULL;
3545 #endif
3546 break;
3547 case lang_assignment_statement_enum:
3548 if (s->assignment_statement.exp->type.node_class != etree_assert)
3549 exp_fold_tree_no_dot (s->assignment_statement.exp);
3550 break;
3551 default:
3552 break;
3553 }
3554 }
3555
3556 /* Exit if any of the files were missing. */
3557 if (input_flags.missing_file)
3558 einfo ("%F");
3559 }
3560
3561 #ifdef ENABLE_LIBCTF
3562 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
3563 that happened specifically at CTF open time. */
3564 static void
3565 lang_ctf_errs_warnings (ctf_dict_t *fp)
3566 {
3567 ctf_next_t *i = NULL;
3568 char *text;
3569 int is_warning;
3570 int err;
3571
3572 while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL)
3573 {
3574 einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"),
3575 text);
3576 free (text);
3577 }
3578 if (err != ECTF_NEXT_END)
3579 {
3580 einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
3581 ctf_errmsg (err));
3582 }
3583
3584 /* `err' returns errors from the error/warning iterator in particular.
3585 These never assert. But if we have an fp, that could have recorded
3586 an assertion failure: assert if it has done so. */
3587 ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL);
3588 }
3589
3590 /* Open the CTF sections in the input files with libctf: if any were opened,
3591 create a fake input file that we'll write the merged CTF data to later
3592 on. */
3593
3594 static void
3595 ldlang_open_ctf (void)
3596 {
3597 int any_ctf = 0;
3598 int err;
3599
3600 LANG_FOR_EACH_INPUT_STATEMENT (file)
3601 {
3602 asection *sect;
3603
3604 /* Incoming files from the compiler have a single ctf_dict_t in them
3605 (which is presented to us by the libctf API in a ctf_archive_t
3606 wrapper): files derived from a previous relocatable link have a CTF
3607 archive containing possibly many CTF files. */
3608
3609 if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL)
3610 {
3611 if (err != ECTF_NOCTFDATA)
3612 {
3613 lang_ctf_errs_warnings (NULL);
3614 einfo (_("%P: warning: CTF section in %pB not loaded; "
3615 "its types will be discarded: %s\n"), file->the_bfd,
3616 ctf_errmsg (err));
3617 }
3618 continue;
3619 }
3620
3621 /* Prevent the contents of this section from being written, while
3622 requiring the section itself to be duplicated in the output, but only
3623 once. */
3624 /* This section must exist if ctf_bfdopen() succeeded. */
3625 sect = bfd_get_section_by_name (file->the_bfd, ".ctf");
3626 sect->size = 0;
3627 sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED;
3628
3629 if (any_ctf)
3630 sect->flags |= SEC_EXCLUDE;
3631 any_ctf = 1;
3632 }
3633
3634 if (!any_ctf)
3635 {
3636 ctf_output = NULL;
3637 return;
3638 }
3639
3640 if ((ctf_output = ctf_create (&err)) != NULL)
3641 return;
3642
3643 einfo (_("%P: warning: CTF output not created: `%s'\n"),
3644 ctf_errmsg (err));
3645
3646 LANG_FOR_EACH_INPUT_STATEMENT (errfile)
3647 ctf_close (errfile->the_ctf);
3648 }
3649
3650 /* Merge together CTF sections. After this, only the symtab-dependent
3651 function and data object sections need adjustment. */
3652
3653 static void
3654 lang_merge_ctf (void)
3655 {
3656 asection *output_sect;
3657 int flags = 0;
3658
3659 if (!ctf_output)
3660 return;
3661
3662 output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
3663
3664 /* If the section was discarded, don't waste time merging. */
3665 if (output_sect == NULL)
3666 {
3667 ctf_dict_close (ctf_output);
3668 ctf_output = NULL;
3669
3670 LANG_FOR_EACH_INPUT_STATEMENT (file)
3671 {
3672 ctf_close (file->the_ctf);
3673 file->the_ctf = NULL;
3674 }
3675 return;
3676 }
3677
3678 LANG_FOR_EACH_INPUT_STATEMENT (file)
3679 {
3680 if (!file->the_ctf)
3681 continue;
3682
3683 /* Takes ownership of file->the_ctf. */
3684 if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0)
3685 {
3686 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
3687 file->the_bfd, ctf_errmsg (ctf_errno (ctf_output)));
3688 ctf_close (file->the_ctf);
3689 file->the_ctf = NULL;
3690 continue;
3691 }
3692 }
3693
3694 if (!config.ctf_share_duplicated)
3695 flags = CTF_LINK_SHARE_UNCONFLICTED;
3696 else
3697 flags = CTF_LINK_SHARE_DUPLICATED;
3698 if (!config.ctf_variables)
3699 flags |= CTF_LINK_OMIT_VARIABLES_SECTION;
3700 if (bfd_link_relocatable (&link_info))
3701 flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS;
3702
3703 if (ctf_link (ctf_output, flags) < 0)
3704 {
3705 lang_ctf_errs_warnings (ctf_output);
3706 einfo (_("%P: warning: CTF linking failed; "
3707 "output will have no CTF section: %s\n"),
3708 ctf_errmsg (ctf_errno (ctf_output)));
3709 if (output_sect)
3710 {
3711 output_sect->size = 0;
3712 output_sect->flags |= SEC_EXCLUDE;
3713 }
3714 }
3715 /* Output any lingering errors that didn't come from ctf_link. */
3716 lang_ctf_errs_warnings (ctf_output);
3717 }
3718
3719 /* Let the emulation acquire strings from the dynamic strtab to help it optimize
3720 the CTF, if supported. */
3721
3722 void
3723 ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab)
3724 {
3725 ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab);
3726 }
3727
3728 /* Inform the emulation about the addition of a new dynamic symbol, in BFD
3729 internal format. */
3730 void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym)
3731 {
3732 ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym);
3733 }
3734
3735 /* Write out the CTF section. Called early, if the emulation isn't going to
3736 need to dedup against the strtab and symtab, then possibly called from the
3737 target linker code if the dedup has happened. */
3738 static void
3739 lang_write_ctf (int late)
3740 {
3741 size_t output_size;
3742 asection *output_sect;
3743
3744 if (!ctf_output)
3745 return;
3746
3747 if (late)
3748 {
3749 /* Emit CTF late if this emulation says it can do so. */
3750 if (ldemul_emit_ctf_early ())
3751 return;
3752 }
3753 else
3754 {
3755 if (!ldemul_emit_ctf_early ())
3756 return;
3757 }
3758
3759 /* Inform the emulation that all the symbols that will be received have
3760 been. */
3761
3762 ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL);
3763
3764 /* Emit CTF. */
3765
3766 output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
3767 if (output_sect)
3768 {
3769 output_sect->contents = ctf_link_write (ctf_output, &output_size,
3770 CTF_COMPRESSION_THRESHOLD);
3771 output_sect->size = output_size;
3772 output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP;
3773
3774 lang_ctf_errs_warnings (ctf_output);
3775 if (!output_sect->contents)
3776 {
3777 einfo (_("%P: warning: CTF section emission failed; "
3778 "output will have no CTF section: %s\n"),
3779 ctf_errmsg (ctf_errno (ctf_output)));
3780 output_sect->size = 0;
3781 output_sect->flags |= SEC_EXCLUDE;
3782 }
3783 }
3784
3785 /* This also closes every CTF input file used in the link. */
3786 ctf_dict_close (ctf_output);
3787 ctf_output = NULL;
3788
3789 LANG_FOR_EACH_INPUT_STATEMENT (file)
3790 file->the_ctf = NULL;
3791 }
3792
3793 /* Write out the CTF section late, if the emulation needs that. */
3794
3795 void
3796 ldlang_write_ctf_late (void)
3797 {
3798 /* Trigger a "late call", if the emulation needs one. */
3799
3800 lang_write_ctf (1);
3801 }
3802 #else
3803 static void
3804 ldlang_open_ctf (void)
3805 {
3806 LANG_FOR_EACH_INPUT_STATEMENT (file)
3807 {
3808 asection *sect;
3809
3810 /* If built without CTF, warn and delete all CTF sections from the output.
3811 (The alternative would be to simply concatenate them, which does not
3812 yield a valid CTF section.) */
3813
3814 if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL)
3815 {
3816 einfo (_("%P: warning: CTF section in %pB not linkable: "
3817 "%P was built without support for CTF\n"), file->the_bfd);
3818 sect->size = 0;
3819 sect->flags |= SEC_EXCLUDE;
3820 }
3821 }
3822 }
3823
3824 static void lang_merge_ctf (void) {}
3825 void
3826 ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab
3827 ATTRIBUTE_UNUSED) {}
3828 void
3829 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED,
3830 struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {}
3831 static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {}
3832 void ldlang_write_ctf_late (void) {}
3833 #endif
3834
3835 /* Add the supplied name to the symbol table as an undefined reference.
3836 This is a two step process as the symbol table doesn't even exist at
3837 the time the ld command line is processed. First we put the name
3838 on a list, then, once the output file has been opened, transfer the
3839 name to the symbol table. */
3840
3841 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3842
3843 #define ldlang_undef_chain_list_head entry_symbol.next
3844
3845 void
3846 ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED)
3847 {
3848 ldlang_undef_chain_list_type *new_undef;
3849
3850 new_undef = stat_alloc (sizeof (*new_undef));
3851 new_undef->next = ldlang_undef_chain_list_head;
3852 ldlang_undef_chain_list_head = new_undef;
3853
3854 new_undef->name = xstrdup (name);
3855
3856 if (link_info.output_bfd != NULL)
3857 insert_undefined (new_undef->name);
3858 }
3859
3860 /* Insert NAME as undefined in the symbol table. */
3861
3862 static void
3863 insert_undefined (const char *name)
3864 {
3865 struct bfd_link_hash_entry *h;
3866
3867 h = bfd_link_hash_lookup (link_info.hash, name, true, false, true);
3868 if (h == NULL)
3869 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
3870 if (h->type == bfd_link_hash_new)
3871 {
3872 h->type = bfd_link_hash_undefined;
3873 h->u.undef.abfd = NULL;
3874 h->non_ir_ref_regular = true;
3875 bfd_link_add_undef (link_info.hash, h);
3876 }
3877 }
3878
3879 /* Run through the list of undefineds created above and place them
3880 into the linker hash table as undefined symbols belonging to the
3881 script file. */
3882
3883 static void
3884 lang_place_undefineds (void)
3885 {
3886 ldlang_undef_chain_list_type *ptr;
3887
3888 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3889 insert_undefined (ptr->name);
3890 }
3891
3892 /* Mark -u symbols against garbage collection. */
3893
3894 static void
3895 lang_mark_undefineds (void)
3896 {
3897 ldlang_undef_chain_list_type *ptr;
3898
3899 if (is_elf_hash_table (link_info.hash))
3900 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3901 {
3902 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
3903 bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true);
3904 if (h != NULL)
3905 h->mark = 1;
3906 }
3907 }
3908
3909 /* Structure used to build the list of symbols that the user has required
3910 be defined. */
3911
3912 struct require_defined_symbol
3913 {
3914 const char *name;
3915 struct require_defined_symbol *next;
3916 };
3917
3918 /* The list of symbols that the user has required be defined. */
3919
3920 static struct require_defined_symbol *require_defined_symbol_list;
3921
3922 /* Add a new symbol NAME to the list of symbols that are required to be
3923 defined. */
3924
3925 void
3926 ldlang_add_require_defined (const char *const name)
3927 {
3928 struct require_defined_symbol *ptr;
3929
3930 ldlang_add_undef (name, true);
3931 ptr = stat_alloc (sizeof (*ptr));
3932 ptr->next = require_defined_symbol_list;
3933 ptr->name = strdup (name);
3934 require_defined_symbol_list = ptr;
3935 }
3936
3937 /* Check that all symbols the user required to be defined, are defined,
3938 raise an error if we find a symbol that is not defined. */
3939
3940 static void
3941 ldlang_check_require_defined_symbols (void)
3942 {
3943 struct require_defined_symbol *ptr;
3944
3945 for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next)
3946 {
3947 struct bfd_link_hash_entry *h;
3948
3949 h = bfd_link_hash_lookup (link_info.hash, ptr->name,
3950 false, false, true);
3951 if (h == NULL
3952 || (h->type != bfd_link_hash_defined
3953 && h->type != bfd_link_hash_defweak))
3954 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name);
3955 }
3956 }
3957
3958 /* Check for all readonly or some readwrite sections. */
3959
3960 static void
3961 check_input_sections
3962 (lang_statement_union_type *s,
3963 lang_output_section_statement_type *output_section_statement)
3964 {
3965 for (; s != NULL; s = s->header.next)
3966 {
3967 switch (s->header.type)
3968 {
3969 case lang_wild_statement_enum:
3970 walk_wild (&s->wild_statement, check_section_callback,
3971 output_section_statement);
3972 if (!output_section_statement->all_input_readonly)
3973 return;
3974 break;
3975 case lang_constructors_statement_enum:
3976 check_input_sections (constructor_list.head,
3977 output_section_statement);
3978 if (!output_section_statement->all_input_readonly)
3979 return;
3980 break;
3981 case lang_group_statement_enum:
3982 check_input_sections (s->group_statement.children.head,
3983 output_section_statement);
3984 if (!output_section_statement->all_input_readonly)
3985 return;
3986 break;
3987 default:
3988 break;
3989 }
3990 }
3991 }
3992
3993 /* Update wildcard statements if needed. */
3994
3995 static void
3996 update_wild_statements (lang_statement_union_type *s)
3997 {
3998 struct wildcard_list *sec;
3999
4000 switch (sort_section)
4001 {
4002 default:
4003 FAIL ();
4004
4005 case none:
4006 break;
4007
4008 case by_name:
4009 case by_alignment:
4010 for (; s != NULL; s = s->header.next)
4011 {
4012 switch (s->header.type)
4013 {
4014 default:
4015 break;
4016
4017 case lang_wild_statement_enum:
4018 for (sec = s->wild_statement.section_list; sec != NULL;
4019 sec = sec->next)
4020 /* Don't sort .init/.fini sections. */
4021 if (strcmp (sec->spec.name, ".init") != 0
4022 && strcmp (sec->spec.name, ".fini") != 0)
4023 {
4024 switch (sec->spec.sorted)
4025 {
4026 case none:
4027 sec->spec.sorted = sort_section;
4028 break;
4029 case by_name:
4030 if (sort_section == by_alignment)
4031 sec->spec.sorted = by_name_alignment;
4032 break;
4033 case by_alignment:
4034 if (sort_section == by_name)
4035 sec->spec.sorted = by_alignment_name;
4036 break;
4037 default:
4038 break;
4039 }
4040 s->wild_statement.any_specs_sorted = true;
4041 }
4042 break;
4043
4044 case lang_constructors_statement_enum:
4045 update_wild_statements (constructor_list.head);
4046 break;
4047
4048 case lang_output_section_statement_enum:
4049 update_wild_statements
4050 (s->output_section_statement.children.head);
4051 break;
4052
4053 case lang_group_statement_enum:
4054 update_wild_statements (s->group_statement.children.head);
4055 break;
4056 }
4057 }
4058 break;
4059 }
4060 }
4061
4062 /* Open input files and attach to output sections. */
4063
4064 static void
4065 map_input_to_output_sections
4066 (lang_statement_union_type *s, const char *target,
4067 lang_output_section_statement_type *os)
4068 {
4069 for (; s != NULL; s = s->header.next)
4070 {
4071 lang_output_section_statement_type *tos;
4072 flagword flags;
4073 unsigned int type = 0;
4074
4075 switch (s->header.type)
4076 {
4077 case lang_wild_statement_enum:
4078 wild (&s->wild_statement, target, os);
4079 break;
4080 case lang_constructors_statement_enum:
4081 map_input_to_output_sections (constructor_list.head,
4082 target,
4083 os);
4084 break;
4085 case lang_output_section_statement_enum:
4086 tos = &s->output_section_statement;
4087 if (tos->constraint == ONLY_IF_RW
4088 || tos->constraint == ONLY_IF_RO)
4089 {
4090 tos->all_input_readonly = true;
4091 check_input_sections (tos->children.head, tos);
4092 if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
4093 tos->constraint = -1;
4094 }
4095 if (tos->constraint >= 0)
4096 map_input_to_output_sections (tos->children.head,
4097 target,
4098 tos);
4099 break;
4100 case lang_output_statement_enum:
4101 break;
4102 case lang_target_statement_enum:
4103 target = s->target_statement.target;
4104 break;
4105 case lang_group_statement_enum:
4106 map_input_to_output_sections (s->group_statement.children.head,
4107 target,
4108 os);
4109 break;
4110 case lang_data_statement_enum:
4111 /* Make sure that any sections mentioned in the expression
4112 are initialized. */
4113 exp_init_os (s->data_statement.exp);
4114 /* The output section gets CONTENTS, ALLOC and LOAD, but
4115 these may be overridden by the script. */
4116 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
4117 switch (os->sectype)
4118 {
4119 case normal_section:
4120 case overlay_section:
4121 case first_overlay_section:
4122 break;
4123 case noalloc_section:
4124 flags = SEC_HAS_CONTENTS;
4125 break;
4126 case readonly_section:
4127 flags |= SEC_READONLY;
4128 break;
4129 case typed_readonly_section:
4130 flags |= SEC_READONLY;
4131 /* Fall through. */
4132 case type_section:
4133 if (os->sectype_value->type.node_class == etree_name
4134 && os->sectype_value->type.node_code == NAME)
4135 {
4136 const char *name = os->sectype_value->name.name;
4137 if (strcmp (name, "SHT_PROGBITS") == 0)
4138 type = SHT_PROGBITS;
4139 else if (strcmp (name, "SHT_STRTAB") == 0)
4140 type = SHT_STRTAB;
4141 else if (strcmp (name, "SHT_NOTE") == 0)
4142 type = SHT_NOTE;
4143 else if (strcmp (name, "SHT_NOBITS") == 0)
4144 type = SHT_NOBITS;
4145 else if (strcmp (name, "SHT_INIT_ARRAY") == 0)
4146 type = SHT_INIT_ARRAY;
4147 else if (strcmp (name, "SHT_FINI_ARRAY") == 0)
4148 type = SHT_FINI_ARRAY;
4149 else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0)
4150 type = SHT_PREINIT_ARRAY;
4151 else
4152 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4153 os->name);
4154 }
4155 else
4156 {
4157 exp_fold_tree_no_dot (os->sectype_value);
4158 if (expld.result.valid_p)
4159 type = expld.result.value;
4160 else
4161 einfo (_ ("%F%P: invalid type for output section `%s'\n"),
4162 os->name);
4163 }
4164 break;
4165 case noload_section:
4166 if (bfd_get_flavour (link_info.output_bfd)
4167 == bfd_target_elf_flavour)
4168 flags = SEC_NEVER_LOAD | SEC_ALLOC;
4169 else
4170 flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
4171 break;
4172 }
4173 if (os->bfd_section == NULL)
4174 init_os (os, flags | SEC_READONLY);
4175 else
4176 os->bfd_section->flags |= flags;
4177 os->bfd_section->type = type;
4178 break;
4179 case lang_input_section_enum:
4180 break;
4181 case lang_fill_statement_enum:
4182 case lang_object_symbols_statement_enum:
4183 case lang_reloc_statement_enum:
4184 case lang_padding_statement_enum:
4185 case lang_input_statement_enum:
4186 if (os != NULL && os->bfd_section == NULL)
4187 init_os (os, 0);
4188 break;
4189 case lang_assignment_statement_enum:
4190 if (os != NULL && os->bfd_section == NULL)
4191 init_os (os, 0);
4192
4193 /* Make sure that any sections mentioned in the assignment
4194 are initialized. */
4195 exp_init_os (s->assignment_statement.exp);
4196 break;
4197 case lang_address_statement_enum:
4198 /* Mark the specified section with the supplied address.
4199 If this section was actually a segment marker, then the
4200 directive is ignored if the linker script explicitly
4201 processed the segment marker. Originally, the linker
4202 treated segment directives (like -Ttext on the
4203 command-line) as section directives. We honor the
4204 section directive semantics for backwards compatibility;
4205 linker scripts that do not specifically check for
4206 SEGMENT_START automatically get the old semantics. */
4207 if (!s->address_statement.segment
4208 || !s->address_statement.segment->used)
4209 {
4210 const char *name = s->address_statement.section_name;
4211
4212 /* Create the output section statement here so that
4213 orphans with a set address will be placed after other
4214 script sections. If we let the orphan placement code
4215 place them in amongst other sections then the address
4216 will affect following script sections, which is
4217 likely to surprise naive users. */
4218 tos = lang_output_section_statement_lookup (name, 0, 1);
4219 tos->addr_tree = s->address_statement.address;
4220 if (tos->bfd_section == NULL)
4221 init_os (tos, 0);
4222 }
4223 break;
4224 case lang_insert_statement_enum:
4225 break;
4226 case lang_input_matcher_enum:
4227 FAIL ();
4228 }
4229 }
4230 }
4231
4232 /* An insert statement snips out all the linker statements from the
4233 start of the list and places them after the output section
4234 statement specified by the insert. This operation is complicated
4235 by the fact that we keep a doubly linked list of output section
4236 statements as well as the singly linked list of all statements.
4237 FIXME someday: Twiddling with the list not only moves statements
4238 from the user's script but also input and group statements that are
4239 built from command line object files and --start-group. We only
4240 get away with this because the list pointers used by file_chain
4241 and input_file_chain are not reordered, and processing via
4242 statement_list after this point mostly ignores input statements.
4243 One exception is the map file, where LOAD and START GROUP/END GROUP
4244 can end up looking odd. */
4245
4246 static void
4247 process_insert_statements (lang_statement_union_type **start)
4248 {
4249 lang_statement_union_type **s;
4250 lang_output_section_statement_type *first_os = NULL;
4251 lang_output_section_statement_type *last_os = NULL;
4252 lang_output_section_statement_type *os;
4253
4254 s = start;
4255 while (*s != NULL)
4256 {
4257 if ((*s)->header.type == lang_output_section_statement_enum)
4258 {
4259 /* Keep pointers to the first and last output section
4260 statement in the sequence we may be about to move. */
4261 os = &(*s)->output_section_statement;
4262
4263 ASSERT (last_os == NULL || last_os->next == os);
4264 last_os = os;
4265
4266 /* Set constraint negative so that lang_output_section_find
4267 won't match this output section statement. At this
4268 stage in linking constraint has values in the range
4269 [-1, ONLY_IN_RW]. */
4270 last_os->constraint = -2 - last_os->constraint;
4271 if (first_os == NULL)
4272 first_os = last_os;
4273 }
4274 else if ((*s)->header.type == lang_group_statement_enum)
4275 {
4276 /* A user might put -T between --start-group and
4277 --end-group. One way this odd construct might arise is
4278 from a wrapper around ld to change library search
4279 behaviour. For example:
4280 #! /bin/sh
4281 exec real_ld --start-group "$@" --end-group
4282 This isn't completely unreasonable so go looking inside a
4283 group statement for insert statements. */
4284 process_insert_statements (&(*s)->group_statement.children.head);
4285 }
4286 else if ((*s)->header.type == lang_insert_statement_enum)
4287 {
4288 lang_insert_statement_type *i = &(*s)->insert_statement;
4289 lang_output_section_statement_type *where;
4290 lang_statement_union_type **ptr;
4291 lang_statement_union_type *first;
4292
4293 if (link_info.non_contiguous_regions)
4294 {
4295 einfo (_("warning: INSERT statement in linker script is "
4296 "incompatible with --enable-non-contiguous-regions.\n"));
4297 }
4298
4299 where = lang_output_section_find (i->where);
4300 if (where != NULL && i->is_before)
4301 {
4302 do
4303 where = where->prev;
4304 while (where != NULL && where->constraint < 0);
4305 }
4306 if (where == NULL)
4307 {
4308 einfo (_("%F%P: %s not found for insert\n"), i->where);
4309 return;
4310 }
4311
4312 /* Deal with reordering the output section statement list. */
4313 if (last_os != NULL)
4314 {
4315 asection *first_sec, *last_sec;
4316 struct lang_output_section_statement_struct **next;
4317
4318 /* Snip out the output sections we are moving. */
4319 first_os->prev->next = last_os->next;
4320 if (last_os->next == NULL)
4321 {
4322 next = &first_os->prev->next;
4323 lang_os_list.tail = (lang_statement_union_type **) next;
4324 }
4325 else
4326 last_os->next->prev = first_os->prev;
4327 /* Add them in at the new position. */
4328 last_os->next = where->next;
4329 if (where->next == NULL)
4330 {
4331 next = &last_os->next;
4332 lang_os_list.tail = (lang_statement_union_type **) next;
4333 }
4334 else
4335 where->next->prev = last_os;
4336 first_os->prev = where;
4337 where->next = first_os;
4338
4339 /* Move the bfd sections in the same way. */
4340 first_sec = NULL;
4341 last_sec = NULL;
4342 for (os = first_os; os != NULL; os = os->next)
4343 {
4344 os->constraint = -2 - os->constraint;
4345 if (os->bfd_section != NULL
4346 && os->bfd_section->owner != NULL)
4347 {
4348 last_sec = os->bfd_section;
4349 if (first_sec == NULL)
4350 first_sec = last_sec;
4351 }
4352 if (os == last_os)
4353 break;
4354 }
4355 if (last_sec != NULL)
4356 {
4357 asection *sec = where->bfd_section;
4358 if (sec == NULL)
4359 sec = output_prev_sec_find (where);
4360
4361 /* The place we want to insert must come after the
4362 sections we are moving. So if we find no
4363 section or if the section is the same as our
4364 last section, then no move is needed. */
4365 if (sec != NULL && sec != last_sec)
4366 {
4367 /* Trim them off. */
4368 if (first_sec->prev != NULL)
4369 first_sec->prev->next = last_sec->next;
4370 else
4371 link_info.output_bfd->sections = last_sec->next;
4372 if (last_sec->next != NULL)
4373 last_sec->next->prev = first_sec->prev;
4374 else
4375 link_info.output_bfd->section_last = first_sec->prev;
4376 /* Add back. */
4377 if (sec->owner == NULL)
4378 /* SEC is the absolute section, from the
4379 first dummy output section statement. Add
4380 back the sections we trimmed off to the
4381 start of the bfd sections. */
4382 sec = NULL;
4383 if (sec != NULL)
4384 last_sec->next = sec->next;
4385 else
4386 last_sec->next = link_info.output_bfd->sections;
4387 if (last_sec->next != NULL)
4388 last_sec->next->prev = last_sec;
4389 else
4390 link_info.output_bfd->section_last = last_sec;
4391 first_sec->prev = sec;
4392 if (first_sec->prev != NULL)
4393 first_sec->prev->next = first_sec;
4394 else
4395 link_info.output_bfd->sections = first_sec;
4396 }
4397 }
4398 }
4399
4400 lang_statement_union_type *after = (void *) where;
4401 if (where == &lang_os_list.head->output_section_statement
4402 && where->next == first_os)
4403 {
4404 /* PR30155. Handle a corner case where the statement
4405 list is something like the following:
4406 . LOAD t.o
4407 . .data 0x0000000000000000 0x0
4408 . [0x0000000000000000] b = .
4409 . *(.data)
4410 . .data 0x0000000000000000 0x0 t.o
4411 . 0x0000000000000000 0x4 LONG 0x0
4412 . INSERT BEFORE .text.start
4413 . [0x0000000000000004] a = .
4414 . .text.start 0x0000000000000000 0x0
4415 . [0x0000000000000000] c = .
4416 . OUTPUT(a.out elf64-x86-64)
4417 Here we do not want to allow insert_os_after to
4418 choose a point inside the list we are moving.
4419 That would lose the list. Instead, let
4420 insert_os_after work from the INSERT, which in this
4421 particular example will result in inserting after
4422 the assignment "a = .". */
4423 after = *s;
4424 }
4425 ptr = insert_os_after (after);
4426 /* Snip everything from the start of the list, up to and
4427 including the insert statement we are currently processing. */
4428 first = *start;
4429 *start = (*s)->header.next;
4430 /* Add them back where they belong, minus the insert. */
4431 *s = *ptr;
4432 if (*s == NULL)
4433 statement_list.tail = s;
4434 *ptr = first;
4435 s = start;
4436 first_os = NULL;
4437 last_os = NULL;
4438 continue;
4439 }
4440 s = &(*s)->header.next;
4441 }
4442
4443 /* Undo constraint twiddling. */
4444 for (os = first_os; os != NULL; os = os->next)
4445 {
4446 os->constraint = -2 - os->constraint;
4447 if (os == last_os)
4448 break;
4449 }
4450 }
4451
4452 /* An output section might have been removed after its statement was
4453 added. For example, ldemul_before_allocation can remove dynamic
4454 sections if they turn out to be not needed. Clean them up here. */
4455
4456 void
4457 strip_excluded_output_sections (void)
4458 {
4459 lang_output_section_statement_type *os;
4460
4461 /* Run lang_size_sections (if not already done). */
4462 if (expld.phase != lang_mark_phase_enum)
4463 {
4464 expld.phase = lang_mark_phase_enum;
4465 expld.dataseg.phase = exp_seg_none;
4466 one_lang_size_sections_pass (NULL, false);
4467 lang_reset_memory_regions ();
4468 }
4469
4470 for (os = (void *) lang_os_list.head;
4471 os != NULL;
4472 os = os->next)
4473 {
4474 asection *output_section;
4475 bool exclude;
4476
4477 if (os->constraint < 0)
4478 continue;
4479
4480 output_section = os->bfd_section;
4481 if (output_section == NULL)
4482 continue;
4483
4484 exclude = (output_section->rawsize == 0
4485 && (output_section->flags & SEC_KEEP) == 0
4486 && !bfd_section_removed_from_list (link_info.output_bfd,
4487 output_section));
4488
4489 /* Some sections have not yet been sized, notably .gnu.version,
4490 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
4491 input sections, so don't drop output sections that have such
4492 input sections unless they are also marked SEC_EXCLUDE. */
4493 if (exclude && output_section->map_head.s != NULL)
4494 {
4495 asection *s;
4496
4497 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
4498 if ((s->flags & SEC_EXCLUDE) == 0
4499 && ((s->flags & SEC_LINKER_CREATED) != 0
4500 || link_info.emitrelocations))
4501 {
4502 exclude = false;
4503 break;
4504 }
4505 }
4506
4507 if (exclude)
4508 {
4509 /* We don't set bfd_section to NULL since bfd_section of the
4510 removed output section statement may still be used. */
4511 if (!os->update_dot)
4512 os->ignored = true;
4513 output_section->flags |= SEC_EXCLUDE;
4514 bfd_section_list_remove (link_info.output_bfd, output_section);
4515 link_info.output_bfd->section_count--;
4516 }
4517 }
4518 }
4519
4520 /* Called from ldwrite to clear out asection.map_head and
4521 asection.map_tail for use as link_orders in ldwrite. */
4522
4523 void
4524 lang_clear_os_map (void)
4525 {
4526 lang_output_section_statement_type *os;
4527
4528 if (map_head_is_link_order)
4529 return;
4530
4531 for (os = (void *) lang_os_list.head;
4532 os != NULL;
4533 os = os->next)
4534 {
4535 asection *output_section;
4536
4537 if (os->constraint < 0)
4538 continue;
4539
4540 output_section = os->bfd_section;
4541 if (output_section == NULL)
4542 continue;
4543
4544 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
4545 output_section->map_head.link_order = NULL;
4546 output_section->map_tail.link_order = NULL;
4547 }
4548
4549 /* Stop future calls to lang_add_section from messing with map_head
4550 and map_tail link_order fields. */
4551 map_head_is_link_order = true;
4552 }
4553
4554 static void
4555 print_output_section_statement
4556 (lang_output_section_statement_type *output_section_statement)
4557 {
4558 asection *section = output_section_statement->bfd_section;
4559 int len;
4560
4561 if (output_section_statement != abs_output_section)
4562 {
4563 minfo ("\n%s", output_section_statement->name);
4564
4565 if (section != NULL)
4566 {
4567 print_dot = section->vma;
4568
4569 len = strlen (output_section_statement->name);
4570 if (len >= SECTION_NAME_MAP_LENGTH - 1)
4571 {
4572 print_nl ();
4573 len = 0;
4574 }
4575 print_spaces (SECTION_NAME_MAP_LENGTH - len);
4576
4577 minfo ("0x%V %W", section->vma, TO_ADDR (section->size));
4578
4579 if (section->vma != section->lma)
4580 minfo (_(" load address 0x%V"), section->lma);
4581
4582 if (output_section_statement->update_dot_tree != NULL)
4583 exp_fold_tree (output_section_statement->update_dot_tree,
4584 bfd_abs_section_ptr, &print_dot);
4585 }
4586
4587 print_nl ();
4588 }
4589
4590 print_statement_list (output_section_statement->children.head,
4591 output_section_statement);
4592 }
4593
4594 static void
4595 print_assignment (lang_assignment_statement_type *assignment,
4596 lang_output_section_statement_type *output_section)
4597 {
4598 bool is_dot;
4599 etree_type *tree;
4600 asection *osec;
4601
4602 print_spaces (SECTION_NAME_MAP_LENGTH);
4603
4604 if (assignment->exp->type.node_class == etree_assert)
4605 {
4606 is_dot = false;
4607 tree = assignment->exp->assert_s.child;
4608 }
4609 else
4610 {
4611 const char *dst = assignment->exp->assign.dst;
4612
4613 is_dot = (dst[0] == '.' && dst[1] == 0);
4614 tree = assignment->exp;
4615 }
4616
4617 osec = output_section->bfd_section;
4618 if (osec == NULL)
4619 osec = bfd_abs_section_ptr;
4620
4621 if (assignment->exp->type.node_class != etree_provide)
4622 exp_fold_tree (tree, osec, &print_dot);
4623 else
4624 expld.result.valid_p = false;
4625
4626 char buf[32];
4627 const char *str = buf;
4628 if (expld.result.valid_p)
4629 {
4630 bfd_vma value;
4631
4632 if (assignment->exp->type.node_class == etree_assert
4633 || is_dot
4634 || expld.assign_name != NULL)
4635 {
4636 value = expld.result.value;
4637
4638 if (expld.result.section != NULL)
4639 value += expld.result.section->vma;
4640
4641 buf[0] = '0';
4642 buf[1] = 'x';
4643 bfd_sprintf_vma (link_info.output_bfd, buf + 2, value);
4644 if (is_dot)
4645 print_dot = value;
4646 }
4647 else
4648 {
4649 struct bfd_link_hash_entry *h;
4650
4651 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
4652 false, false, true);
4653 if (h != NULL
4654 && (h->type == bfd_link_hash_defined
4655 || h->type == bfd_link_hash_defweak))
4656 {
4657 value = h->u.def.value;
4658 value += h->u.def.section->output_section->vma;
4659 value += h->u.def.section->output_offset;
4660
4661 buf[0] = '[';
4662 buf[1] = '0';
4663 buf[2] = 'x';
4664 bfd_sprintf_vma (link_info.output_bfd, buf + 3, value);
4665 strcat (buf, "]");
4666 }
4667 else
4668 str = "[unresolved]";
4669 }
4670 }
4671 else
4672 {
4673 if (assignment->exp->type.node_class == etree_provide)
4674 str = "[!provide]";
4675 else
4676 str = "*undef*";
4677 }
4678 expld.assign_name = NULL;
4679
4680 fprintf (config.map_file, "%-34s", str);
4681 exp_print_tree (assignment->exp);
4682 print_nl ();
4683 }
4684
4685 static void
4686 print_input_statement (lang_input_statement_type *statm)
4687 {
4688 if (statm->filename != NULL)
4689 fprintf (config.map_file, "LOAD %s\n", statm->filename);
4690 }
4691
4692 /* Print all symbols defined in a particular section. This is called
4693 via bfd_link_hash_traverse, or by print_all_symbols. */
4694
4695 bool
4696 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
4697 {
4698 asection *sec = (asection *) ptr;
4699
4700 if ((hash_entry->type == bfd_link_hash_defined
4701 || hash_entry->type == bfd_link_hash_defweak)
4702 && sec == hash_entry->u.def.section)
4703 {
4704 print_spaces (SECTION_NAME_MAP_LENGTH);
4705 minfo ("0x%V ",
4706 (hash_entry->u.def.value
4707 + hash_entry->u.def.section->output_offset
4708 + hash_entry->u.def.section->output_section->vma));
4709
4710 minfo (" %pT\n", hash_entry->root.string);
4711 }
4712
4713 return true;
4714 }
4715
4716 static int
4717 hash_entry_addr_cmp (const void *a, const void *b)
4718 {
4719 const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
4720 const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
4721
4722 if (l->u.def.value < r->u.def.value)
4723 return -1;
4724 else if (l->u.def.value > r->u.def.value)
4725 return 1;
4726 else
4727 return 0;
4728 }
4729
4730 static void
4731 print_all_symbols (asection *sec)
4732 {
4733 input_section_userdata_type *ud = bfd_section_userdata (sec);
4734 struct map_symbol_def *def;
4735 struct bfd_link_hash_entry **entries;
4736 unsigned int i;
4737
4738 if (!ud)
4739 return;
4740
4741 *ud->map_symbol_def_tail = 0;
4742
4743 /* Sort the symbols by address. */
4744 entries = (struct bfd_link_hash_entry **)
4745 obstack_alloc (&map_obstack,
4746 ud->map_symbol_def_count * sizeof (*entries));
4747
4748 for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
4749 entries[i] = def->entry;
4750
4751 qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
4752 hash_entry_addr_cmp);
4753
4754 /* Print the symbols. */
4755 for (i = 0; i < ud->map_symbol_def_count; i++)
4756 ldemul_print_symbol (entries[i], sec);
4757
4758 obstack_free (&map_obstack, entries);
4759 }
4760
4761 /* Print information about an input section to the map file. */
4762
4763 static void
4764 print_input_section (asection *i, bool is_discarded)
4765 {
4766 bfd_size_type size = i->size;
4767 int len;
4768 bfd_vma addr;
4769
4770 init_opb (i);
4771
4772 minfo (" %s", i->name);
4773
4774 len = 1 + strlen (i->name);
4775 if (len >= SECTION_NAME_MAP_LENGTH - 1)
4776 {
4777 print_nl ();
4778 len = 0;
4779 }
4780 print_spaces (SECTION_NAME_MAP_LENGTH - len);
4781
4782 if (i->output_section != NULL
4783 && i->output_section->owner == link_info.output_bfd)
4784 addr = i->output_section->vma + i->output_offset;
4785 else
4786 {
4787 addr = print_dot;
4788 if (!is_discarded)
4789 size = 0;
4790 }
4791
4792 char buf[32];
4793 bfd_sprintf_vma (link_info.output_bfd, buf, addr);
4794 minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner);
4795
4796 if (size != i->rawsize && i->rawsize != 0)
4797 {
4798 len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf);
4799 print_spaces (len);
4800 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize));
4801 }
4802
4803 if (i->output_section != NULL
4804 && i->output_section->owner == link_info.output_bfd)
4805 {
4806 if (link_info.reduce_memory_overheads)
4807 bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i);
4808 else
4809 print_all_symbols (i);
4810
4811 /* Update print_dot, but make sure that we do not move it
4812 backwards - this could happen if we have overlays and a
4813 later overlay is shorter than an earier one. */
4814 if (addr + TO_ADDR (size) > print_dot)
4815 print_dot = addr + TO_ADDR (size);
4816 }
4817 }
4818
4819 static void
4820 print_fill_statement (lang_fill_statement_type *fill)
4821 {
4822 size_t size;
4823 unsigned char *p;
4824 fputs (" FILL mask 0x", config.map_file);
4825 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
4826 fprintf (config.map_file, "%02x", *p);
4827 fputs ("\n", config.map_file);
4828 }
4829
4830 static void
4831 print_data_statement (lang_data_statement_type *data)
4832 {
4833 bfd_vma addr;
4834 bfd_size_type size;
4835 const char *name;
4836
4837 init_opb (data->output_section);
4838 print_spaces (SECTION_NAME_MAP_LENGTH);
4839
4840 addr = data->output_offset;
4841 if (data->output_section != NULL)
4842 addr += data->output_section->vma;
4843
4844 switch (data->type)
4845 {
4846 default:
4847 abort ();
4848 case BYTE:
4849 size = BYTE_SIZE;
4850 name = "BYTE";
4851 break;
4852 case SHORT:
4853 size = SHORT_SIZE;
4854 name = "SHORT";
4855 break;
4856 case LONG:
4857 size = LONG_SIZE;
4858 name = "LONG";
4859 break;
4860 case QUAD:
4861 size = QUAD_SIZE;
4862 name = "QUAD";
4863 break;
4864 case SQUAD:
4865 size = QUAD_SIZE;
4866 name = "SQUAD";
4867 break;
4868 }
4869
4870 if (size < TO_SIZE ((unsigned) 1))
4871 size = TO_SIZE ((unsigned) 1);
4872 minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value);
4873
4874 if (data->exp->type.node_class != etree_value)
4875 {
4876 print_space ();
4877 exp_print_tree (data->exp);
4878 }
4879
4880 print_nl ();
4881
4882 print_dot = addr + TO_ADDR (size);
4883 }
4884
4885 /* Print an address statement. These are generated by options like
4886 -Ttext. */
4887
4888 static void
4889 print_address_statement (lang_address_statement_type *address)
4890 {
4891 minfo (_("Address of section %s set to "), address->section_name);
4892 exp_print_tree (address->address);
4893 print_nl ();
4894 }
4895
4896 /* Print a reloc statement. */
4897
4898 static void
4899 print_reloc_statement (lang_reloc_statement_type *reloc)
4900 {
4901 bfd_vma addr;
4902 bfd_size_type size;
4903
4904 init_opb (reloc->output_section);
4905 print_spaces (SECTION_NAME_MAP_LENGTH);
4906
4907 addr = reloc->output_offset;
4908 if (reloc->output_section != NULL)
4909 addr += reloc->output_section->vma;
4910
4911 size = bfd_get_reloc_size (reloc->howto);
4912
4913 minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name);
4914
4915 if (reloc->name != NULL)
4916 minfo ("%s+", reloc->name);
4917 else
4918 minfo ("%s+", reloc->section->name);
4919
4920 exp_print_tree (reloc->addend_exp);
4921
4922 print_nl ();
4923
4924 print_dot = addr + TO_ADDR (size);
4925 }
4926
4927 static void
4928 print_padding_statement (lang_padding_statement_type *s)
4929 {
4930 int len;
4931 bfd_vma addr;
4932
4933 init_opb (s->output_section);
4934 minfo (" *fill*");
4935
4936 len = sizeof " *fill*" - 1;
4937 print_spaces (SECTION_NAME_MAP_LENGTH - len);
4938
4939 addr = s->output_offset;
4940 if (s->output_section != NULL)
4941 addr += s->output_section->vma;
4942 minfo ("0x%V %W ", addr, TO_ADDR (s->size));
4943
4944 if (s->fill->size != 0)
4945 {
4946 size_t size;
4947 unsigned char *p;
4948 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
4949 fprintf (config.map_file, "%02x", *p);
4950 }
4951
4952 print_nl ();
4953
4954 print_dot = addr + TO_ADDR (s->size);
4955 }
4956
4957 static void
4958 print_wild_statement (lang_wild_statement_type *w,
4959 lang_output_section_statement_type *os)
4960 {
4961 struct wildcard_list *sec;
4962
4963 print_space ();
4964
4965 if (w->exclude_name_list)
4966 {
4967 name_list *tmp;
4968 minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name);
4969 for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next)
4970 minfo (" %s", tmp->name);
4971 minfo (") ");
4972 }
4973
4974 if (w->filenames_sorted)
4975 minfo ("SORT_BY_NAME(");
4976 if (w->filename != NULL)
4977 minfo ("%s", w->filename);
4978 else
4979 minfo ("*");
4980 if (w->filenames_sorted)
4981 minfo (")");
4982
4983 minfo ("(");
4984 for (sec = w->section_list; sec; sec = sec->next)
4985 {
4986 int closing_paren = 0;
4987
4988 switch (sec->spec.sorted)
4989 {
4990 case none:
4991 break;
4992
4993 case by_name:
4994 minfo ("SORT_BY_NAME(");
4995 closing_paren = 1;
4996 break;
4997
4998 case by_alignment:
4999 minfo ("SORT_BY_ALIGNMENT(");
5000 closing_paren = 1;
5001 break;
5002
5003 case by_name_alignment:
5004 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
5005 closing_paren = 2;
5006 break;
5007
5008 case by_alignment_name:
5009 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
5010 closing_paren = 2;
5011 break;
5012
5013 case by_none:
5014 minfo ("SORT_NONE(");
5015 closing_paren = 1;
5016 break;
5017
5018 case by_init_priority:
5019 minfo ("SORT_BY_INIT_PRIORITY(");
5020 closing_paren = 1;
5021 break;
5022 }
5023
5024 if (sec->spec.exclude_name_list != NULL)
5025 {
5026 name_list *tmp;
5027 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
5028 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
5029 minfo (" %s", tmp->name);
5030 minfo (") ");
5031 }
5032 if (sec->spec.name != NULL)
5033 minfo ("%s", sec->spec.name);
5034 else
5035 minfo ("*");
5036 for (;closing_paren > 0; closing_paren--)
5037 minfo (")");
5038 if (sec->next)
5039 minfo (" ");
5040 }
5041 minfo (")");
5042
5043 print_nl ();
5044
5045 print_statement_list (w->children.head, os);
5046 }
5047
5048 /* Print a group statement. */
5049
5050 static void
5051 print_group (lang_group_statement_type *s,
5052 lang_output_section_statement_type *os)
5053 {
5054 fprintf (config.map_file, "START GROUP\n");
5055 print_statement_list (s->children.head, os);
5056 fprintf (config.map_file, "END GROUP\n");
5057 }
5058
5059 /* Print the list of statements in S.
5060 This can be called for any statement type. */
5061
5062 static void
5063 print_statement_list (lang_statement_union_type *s,
5064 lang_output_section_statement_type *os)
5065 {
5066 while (s != NULL)
5067 {
5068 print_statement (s, os);
5069 s = s->header.next;
5070 }
5071 }
5072
5073 /* Print the first statement in statement list S.
5074 This can be called for any statement type. */
5075
5076 static void
5077 print_statement (lang_statement_union_type *s,
5078 lang_output_section_statement_type *os)
5079 {
5080 switch (s->header.type)
5081 {
5082 default:
5083 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
5084 FAIL ();
5085 break;
5086 case lang_constructors_statement_enum:
5087 if (constructor_list.head != NULL)
5088 {
5089 if (constructors_sorted)
5090 minfo (" SORT (CONSTRUCTORS)\n");
5091 else
5092 minfo (" CONSTRUCTORS\n");
5093 print_statement_list (constructor_list.head, os);
5094 }
5095 break;
5096 case lang_wild_statement_enum:
5097 print_wild_statement (&s->wild_statement, os);
5098 break;
5099 case lang_address_statement_enum:
5100 print_address_statement (&s->address_statement);
5101 break;
5102 case lang_object_symbols_statement_enum:
5103 minfo (" CREATE_OBJECT_SYMBOLS\n");
5104 break;
5105 case lang_fill_statement_enum:
5106 print_fill_statement (&s->fill_statement);
5107 break;
5108 case lang_data_statement_enum:
5109 print_data_statement (&s->data_statement);
5110 break;
5111 case lang_reloc_statement_enum:
5112 print_reloc_statement (&s->reloc_statement);
5113 break;
5114 case lang_input_section_enum:
5115 print_input_section (s->input_section.section, false);
5116 break;
5117 case lang_padding_statement_enum:
5118 print_padding_statement (&s->padding_statement);
5119 break;
5120 case lang_output_section_statement_enum:
5121 print_output_section_statement (&s->output_section_statement);
5122 break;
5123 case lang_assignment_statement_enum:
5124 print_assignment (&s->assignment_statement, os);
5125 break;
5126 case lang_target_statement_enum:
5127 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
5128 break;
5129 case lang_output_statement_enum:
5130 minfo ("OUTPUT(%s", s->output_statement.name);
5131 if (output_target != NULL)
5132 minfo (" %s", output_target);
5133 minfo (")\n");
5134 break;
5135 case lang_input_statement_enum:
5136 print_input_statement (&s->input_statement);
5137 break;
5138 case lang_group_statement_enum:
5139 print_group (&s->group_statement, os);
5140 break;
5141 case lang_insert_statement_enum:
5142 minfo ("INSERT %s %s\n",
5143 s->insert_statement.is_before ? "BEFORE" : "AFTER",
5144 s->insert_statement.where);
5145 break;
5146 }
5147 }
5148
5149 static void
5150 print_statements (void)
5151 {
5152 print_statement_list (statement_list.head, abs_output_section);
5153 }
5154
5155 /* Print the first N statements in statement list S to STDERR.
5156 If N == 0, nothing is printed.
5157 If N < 0, the entire list is printed.
5158 Intended to be called from GDB. */
5159
5160 void
5161 dprint_statement (lang_statement_union_type *s, int n)
5162 {
5163 FILE *map_save = config.map_file;
5164
5165 config.map_file = stderr;
5166
5167 if (n < 0)
5168 print_statement_list (s, abs_output_section);
5169 else
5170 {
5171 while (s && --n >= 0)
5172 {
5173 print_statement (s, abs_output_section);
5174 s = s->header.next;
5175 }
5176 }
5177
5178 config.map_file = map_save;
5179 }
5180
5181 static void
5182 insert_pad (lang_statement_union_type **ptr,
5183 fill_type *fill,
5184 bfd_size_type alignment_needed,
5185 asection *output_section,
5186 bfd_vma dot)
5187 {
5188 static fill_type zero_fill;
5189 lang_statement_union_type *pad = NULL;
5190
5191 if (ptr != &statement_list.head)
5192 pad = ((lang_statement_union_type *)
5193 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
5194 if (pad != NULL
5195 && pad->header.type == lang_padding_statement_enum
5196 && pad->padding_statement.output_section == output_section)
5197 {
5198 /* Use the existing pad statement. */
5199 }
5200 else if ((pad = *ptr) != NULL
5201 && pad->header.type == lang_padding_statement_enum
5202 && pad->padding_statement.output_section == output_section)
5203 {
5204 /* Use the existing pad statement. */
5205 }
5206 else
5207 {
5208 /* Make a new padding statement, linked into existing chain. */
5209 pad = stat_alloc (sizeof (lang_padding_statement_type));
5210 pad->header.next = *ptr;
5211 *ptr = pad;
5212 pad->header.type = lang_padding_statement_enum;
5213 pad->padding_statement.output_section = output_section;
5214 if (fill == NULL)
5215 fill = &zero_fill;
5216 pad->padding_statement.fill = fill;
5217 }
5218 pad->padding_statement.output_offset = dot - output_section->vma;
5219 pad->padding_statement.size = alignment_needed;
5220 if (!(output_section->flags & SEC_FIXED_SIZE))
5221 output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed)
5222 - output_section->vma);
5223 }
5224
5225 /* Work out how much this section will move the dot point. */
5226
5227 static bfd_vma
5228 size_input_section
5229 (lang_statement_union_type **this_ptr,
5230 lang_output_section_statement_type *output_section_statement,
5231 fill_type *fill,
5232 bool *removed,
5233 bfd_vma dot)
5234 {
5235 lang_input_section_type *is = &((*this_ptr)->input_section);
5236 asection *i = is->section;
5237 asection *o = output_section_statement->bfd_section;
5238 *removed = 0;
5239
5240 if (link_info.non_contiguous_regions)
5241 {
5242 /* If the input section I has already been successfully assigned
5243 to an output section other than O, don't bother with it and
5244 let the caller remove it from the list. Keep processing in
5245 case we have already handled O, because the repeated passes
5246 have reinitialized its size. */
5247 if (i->already_assigned && i->already_assigned != o)
5248 {
5249 *removed = 1;
5250 return dot;
5251 }
5252 }
5253
5254 if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5255 i->output_offset = i->vma - o->vma;
5256 else if (((i->flags & SEC_EXCLUDE) != 0)
5257 || output_section_statement->ignored)
5258 i->output_offset = dot - o->vma;
5259 else
5260 {
5261 bfd_size_type alignment_needed;
5262
5263 /* Align this section first to the input sections requirement,
5264 then to the output section's requirement. If this alignment
5265 is greater than any seen before, then record it too. Perform
5266 the alignment by inserting a magic 'padding' statement. */
5267
5268 if (output_section_statement->subsection_alignment != NULL)
5269 i->alignment_power
5270 = exp_get_power (output_section_statement->subsection_alignment,
5271 "subsection alignment");
5272
5273 if (o->alignment_power < i->alignment_power)
5274 o->alignment_power = i->alignment_power;
5275
5276 alignment_needed = align_power (dot, i->alignment_power) - dot;
5277
5278 if (alignment_needed != 0)
5279 {
5280 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
5281 dot += alignment_needed;
5282 }
5283
5284 if (link_info.non_contiguous_regions)
5285 {
5286 /* If I would overflow O, let the caller remove I from the
5287 list. */
5288 if (output_section_statement->region)
5289 {
5290 bfd_vma end = output_section_statement->region->origin
5291 + output_section_statement->region->length;
5292
5293 if (dot + TO_ADDR (i->size) > end)
5294 {
5295 if (i->flags & SEC_LINKER_CREATED)
5296 einfo (_("%F%P: Output section `%pA' not large enough for "
5297 "the linker-created stubs section `%pA'.\n"),
5298 i->output_section, i);
5299
5300 if (i->rawsize && i->rawsize != i->size)
5301 einfo (_("%F%P: Relaxation not supported with "
5302 "--enable-non-contiguous-regions (section `%pA' "
5303 "would overflow `%pA' after it changed size).\n"),
5304 i, i->output_section);
5305
5306 *removed = 1;
5307 dot = end;
5308 i->output_section = NULL;
5309 return dot;
5310 }
5311 }
5312 }
5313
5314 /* Remember where in the output section this input section goes. */
5315 i->output_offset = dot - o->vma;
5316
5317 /* Mark how big the output section must be to contain this now. */
5318 dot += TO_ADDR (i->size);
5319 if (!(o->flags & SEC_FIXED_SIZE))
5320 o->size = TO_SIZE (dot - o->vma);
5321
5322 if (link_info.non_contiguous_regions)
5323 {
5324 /* Record that I was successfully assigned to O, and update
5325 its actual output section too. */
5326 i->already_assigned = o;
5327 i->output_section = o;
5328 }
5329 }
5330
5331 return dot;
5332 }
5333
5334 struct check_sec
5335 {
5336 asection *sec;
5337 bool warned;
5338 };
5339
5340 static int
5341 sort_sections_by_lma (const void *arg1, const void *arg2)
5342 {
5343 const asection *sec1 = ((const struct check_sec *) arg1)->sec;
5344 const asection *sec2 = ((const struct check_sec *) arg2)->sec;
5345
5346 if (sec1->lma < sec2->lma)
5347 return -1;
5348 else if (sec1->lma > sec2->lma)
5349 return 1;
5350 else if (sec1->id < sec2->id)
5351 return -1;
5352 else if (sec1->id > sec2->id)
5353 return 1;
5354
5355 return 0;
5356 }
5357
5358 static int
5359 sort_sections_by_vma (const void *arg1, const void *arg2)
5360 {
5361 const asection *sec1 = ((const struct check_sec *) arg1)->sec;
5362 const asection *sec2 = ((const struct check_sec *) arg2)->sec;
5363
5364 if (sec1->vma < sec2->vma)
5365 return -1;
5366 else if (sec1->vma > sec2->vma)
5367 return 1;
5368 else if (sec1->id < sec2->id)
5369 return -1;
5370 else if (sec1->id > sec2->id)
5371 return 1;
5372
5373 return 0;
5374 }
5375
5376 #define IS_TBSS(s) \
5377 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
5378
5379 #define IGNORE_SECTION(s) \
5380 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
5381
5382 /* Check to see if any allocated sections overlap with other allocated
5383 sections. This can happen if a linker script specifies the output
5384 section addresses of the two sections. Also check whether any memory
5385 region has overflowed. */
5386
5387 static void
5388 lang_check_section_addresses (void)
5389 {
5390 asection *s, *p;
5391 struct check_sec *sections;
5392 size_t i, count;
5393 bfd_vma addr_mask;
5394 bfd_vma s_start;
5395 bfd_vma s_end;
5396 bfd_vma p_start = 0;
5397 bfd_vma p_end = 0;
5398 lang_memory_region_type *m;
5399 bool overlays;
5400
5401 /* Detect address space overflow on allocated sections. */
5402 addr_mask = ((bfd_vma) 1 <<
5403 (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1;
5404 addr_mask = (addr_mask << 1) + 1;
5405 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5406 if ((s->flags & SEC_ALLOC) != 0)
5407 {
5408 s_end = (s->vma + s->size) & addr_mask;
5409 if (s_end != 0 && s_end < (s->vma & addr_mask))
5410 einfo (_("%X%P: section %s VMA wraps around address space\n"),
5411 s->name);
5412 else
5413 {
5414 s_end = (s->lma + s->size) & addr_mask;
5415 if (s_end != 0 && s_end < (s->lma & addr_mask))
5416 einfo (_("%X%P: section %s LMA wraps around address space\n"),
5417 s->name);
5418 }
5419 }
5420
5421 if (bfd_count_sections (link_info.output_bfd) <= 1)
5422 return;
5423
5424 count = bfd_count_sections (link_info.output_bfd);
5425 sections = XNEWVEC (struct check_sec, count);
5426
5427 /* Scan all sections in the output list. */
5428 count = 0;
5429 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5430 {
5431 if (IGNORE_SECTION (s)
5432 || s->size == 0)
5433 continue;
5434
5435 sections[count].sec = s;
5436 sections[count].warned = false;
5437 count++;
5438 }
5439
5440 if (count <= 1)
5441 {
5442 free (sections);
5443 return;
5444 }
5445
5446 qsort (sections, count, sizeof (*sections), sort_sections_by_lma);
5447
5448 /* First check section LMAs. There should be no overlap of LMAs on
5449 loadable sections, even with overlays. */
5450 for (p = NULL, i = 0; i < count; i++)
5451 {
5452 s = sections[i].sec;
5453 init_opb (s);
5454 if ((s->flags & SEC_LOAD) != 0)
5455 {
5456 s_start = s->lma;
5457 s_end = s_start + TO_ADDR (s->size) - 1;
5458
5459 /* Look for an overlap. We have sorted sections by lma, so
5460 we know that s_start >= p_start. Besides the obvious
5461 case of overlap when the current section starts before
5462 the previous one ends, we also must have overlap if the
5463 previous section wraps around the address space. */
5464 if (p != NULL
5465 && (s_start <= p_end
5466 || p_end < p_start))
5467 {
5468 einfo (_("%X%P: section %s LMA [%V,%V]"
5469 " overlaps section %s LMA [%V,%V]\n"),
5470 s->name, s_start, s_end, p->name, p_start, p_end);
5471 sections[i].warned = true;
5472 }
5473 p = s;
5474 p_start = s_start;
5475 p_end = s_end;
5476 }
5477 }
5478
5479 /* If any non-zero size allocated section (excluding tbss) starts at
5480 exactly the same VMA as another such section, then we have
5481 overlays. Overlays generated by the OVERLAY keyword will have
5482 this property. It is possible to intentionally generate overlays
5483 that fail this test, but it would be unusual. */
5484 qsort (sections, count, sizeof (*sections), sort_sections_by_vma);
5485 overlays = false;
5486 p_start = sections[0].sec->vma;
5487 for (i = 1; i < count; i++)
5488 {
5489 s_start = sections[i].sec->vma;
5490 if (p_start == s_start)
5491 {
5492 overlays = true;
5493 break;
5494 }
5495 p_start = s_start;
5496 }
5497
5498 /* Now check section VMAs if no overlays were detected. */
5499 if (!overlays)
5500 {
5501 for (p = NULL, i = 0; i < count; i++)
5502 {
5503 s = sections[i].sec;
5504 init_opb (s);
5505 s_start = s->vma;
5506 s_end = s_start + TO_ADDR (s->size) - 1;
5507
5508 if (p != NULL
5509 && !sections[i].warned
5510 && (s_start <= p_end
5511 || p_end < p_start))
5512 einfo (_("%X%P: section %s VMA [%V,%V]"
5513 " overlaps section %s VMA [%V,%V]\n"),
5514 s->name, s_start, s_end, p->name, p_start, p_end);
5515 p = s;
5516 p_start = s_start;
5517 p_end = s_end;
5518 }
5519 }
5520
5521 free (sections);
5522
5523 /* If any memory region has overflowed, report by how much.
5524 We do not issue this diagnostic for regions that had sections
5525 explicitly placed outside their bounds; os_region_check's
5526 diagnostics are adequate for that case.
5527
5528 FIXME: It is conceivable that m->current - (m->origin + m->length)
5529 might overflow a 32-bit integer. There is, alas, no way to print
5530 a bfd_vma quantity in decimal. */
5531 for (m = lang_memory_region_list; m; m = m->next)
5532 if (m->had_full_message)
5533 {
5534 unsigned long over = m->current - (m->origin + m->length);
5535 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
5536 "%X%P: region `%s' overflowed by %lu bytes\n",
5537 over),
5538 m->name_list.name, over);
5539 }
5540 }
5541
5542 /* Make sure the new address is within the region. We explicitly permit the
5543 current address to be at the exact end of the region when the address is
5544 non-zero, in case the region is at the end of addressable memory and the
5545 calculation wraps around. */
5546
5547 static void
5548 os_region_check (lang_output_section_statement_type *os,
5549 lang_memory_region_type *region,
5550 etree_type *tree,
5551 bfd_vma rbase)
5552 {
5553 if ((region->current < region->origin
5554 || (region->current - region->origin > region->length))
5555 && ((region->current != region->origin + region->length)
5556 || rbase == 0))
5557 {
5558 if (tree != NULL)
5559 {
5560 einfo (_("%X%P: address 0x%v of %pB section `%s'"
5561 " is not within region `%s'\n"),
5562 region->current,
5563 os->bfd_section->owner,
5564 os->bfd_section->name,
5565 region->name_list.name);
5566 }
5567 else if (!region->had_full_message)
5568 {
5569 region->had_full_message = true;
5570
5571 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
5572 os->bfd_section->owner,
5573 os->bfd_section->name,
5574 region->name_list.name);
5575 }
5576 }
5577 }
5578
5579 static void
5580 ldlang_check_relro_region (lang_statement_union_type *s)
5581 {
5582 seg_align_type *seg = &expld.dataseg;
5583
5584 if (seg->relro == exp_seg_relro_start)
5585 {
5586 if (!seg->relro_start_stat)
5587 seg->relro_start_stat = s;
5588 else
5589 {
5590 ASSERT (seg->relro_start_stat == s);
5591 }
5592 }
5593 else if (seg->relro == exp_seg_relro_end)
5594 {
5595 if (!seg->relro_end_stat)
5596 seg->relro_end_stat = s;
5597 else
5598 {
5599 ASSERT (seg->relro_end_stat == s);
5600 }
5601 }
5602 }
5603
5604 /* Set the sizes for all the output sections. */
5605
5606 static bfd_vma
5607 lang_size_sections_1
5608 (lang_statement_union_type **prev,
5609 lang_output_section_statement_type *output_section_statement,
5610 fill_type *fill,
5611 bfd_vma dot,
5612 bool *relax,
5613 bool check_regions)
5614 {
5615 lang_statement_union_type *s;
5616 lang_statement_union_type *prev_s = NULL;
5617 bool removed_prev_s = false;
5618
5619 /* Size up the sections from their constituent parts. */
5620 for (s = *prev; s != NULL; prev_s = s, s = s->header.next)
5621 {
5622 bool removed = false;
5623
5624 switch (s->header.type)
5625 {
5626 case lang_output_section_statement_enum:
5627 {
5628 bfd_vma newdot, after, dotdelta;
5629 lang_output_section_statement_type *os;
5630 lang_memory_region_type *r;
5631 int section_alignment = 0;
5632
5633 os = &s->output_section_statement;
5634 init_opb (os->bfd_section);
5635 if (os->constraint == -1)
5636 break;
5637
5638 /* FIXME: We shouldn't need to zero section vmas for ld -r
5639 here, in lang_insert_orphan, or in the default linker scripts.
5640 This is covering for coff backend linker bugs. See PR6945. */
5641 if (os->addr_tree == NULL
5642 && bfd_link_relocatable (&link_info)
5643 && (bfd_get_flavour (link_info.output_bfd)
5644 == bfd_target_coff_flavour))
5645 os->addr_tree = exp_intop (0);
5646 if (os->addr_tree != NULL)
5647 {
5648 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
5649
5650 if (expld.result.valid_p)
5651 {
5652 dot = expld.result.value;
5653 if (expld.result.section != NULL)
5654 dot += expld.result.section->vma;
5655 }
5656 else if (expld.phase != lang_mark_phase_enum)
5657 einfo (_("%F%P:%pS: non constant or forward reference"
5658 " address expression for section %s\n"),
5659 os->addr_tree, os->name);
5660 }
5661
5662 if (os->bfd_section == NULL)
5663 /* This section was removed or never actually created. */
5664 break;
5665
5666 /* If this is a COFF shared library section, use the size and
5667 address from the input section. FIXME: This is COFF
5668 specific; it would be cleaner if there were some other way
5669 to do this, but nothing simple comes to mind. */
5670 if (((bfd_get_flavour (link_info.output_bfd)
5671 == bfd_target_ecoff_flavour)
5672 || (bfd_get_flavour (link_info.output_bfd)
5673 == bfd_target_coff_flavour))
5674 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
5675 {
5676 asection *input;
5677
5678 if (os->children.head == NULL
5679 || os->children.head->header.next != NULL
5680 || (os->children.head->header.type
5681 != lang_input_section_enum))
5682 einfo (_("%X%P: internal error on COFF shared library"
5683 " section %s\n"), os->name);
5684
5685 input = os->children.head->input_section.section;
5686 bfd_set_section_vma (os->bfd_section,
5687 bfd_section_vma (input));
5688 if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
5689 os->bfd_section->size = input->size;
5690 break;
5691 }
5692
5693 newdot = dot;
5694 dotdelta = 0;
5695 if (bfd_is_abs_section (os->bfd_section))
5696 {
5697 /* No matter what happens, an abs section starts at zero. */
5698 ASSERT (os->bfd_section->vma == 0);
5699 }
5700 else
5701 {
5702 if (os->addr_tree == NULL)
5703 {
5704 /* No address specified for this section, get one
5705 from the region specification. */
5706 if (os->region == NULL
5707 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
5708 && os->region->name_list.name[0] == '*'
5709 && strcmp (os->region->name_list.name,
5710 DEFAULT_MEMORY_REGION) == 0))
5711 {
5712 os->region = lang_memory_default (os->bfd_section);
5713 }
5714
5715 /* If a loadable section is using the default memory
5716 region, and some non default memory regions were
5717 defined, issue an error message. */
5718 if (!os->ignored
5719 && !IGNORE_SECTION (os->bfd_section)
5720 && !bfd_link_relocatable (&link_info)
5721 && check_regions
5722 && strcmp (os->region->name_list.name,
5723 DEFAULT_MEMORY_REGION) == 0
5724 && lang_memory_region_list != NULL
5725 && (strcmp (lang_memory_region_list->name_list.name,
5726 DEFAULT_MEMORY_REGION) != 0
5727 || lang_memory_region_list->next != NULL)
5728 && lang_sizing_iteration == 1)
5729 {
5730 /* By default this is an error rather than just a
5731 warning because if we allocate the section to the
5732 default memory region we can end up creating an
5733 excessively large binary, or even seg faulting when
5734 attempting to perform a negative seek. See
5735 sources.redhat.com/ml/binutils/2003-04/msg00423.html
5736 for an example of this. This behaviour can be
5737 overridden by the using the --no-check-sections
5738 switch. */
5739 if (command_line.check_section_addresses)
5740 einfo (_("%F%P: error: no memory region specified"
5741 " for loadable section `%s'\n"),
5742 bfd_section_name (os->bfd_section));
5743 else
5744 einfo (_("%P: warning: no memory region specified"
5745 " for loadable section `%s'\n"),
5746 bfd_section_name (os->bfd_section));
5747 }
5748
5749 newdot = os->region->current;
5750 section_alignment = os->bfd_section->alignment_power;
5751 }
5752 else
5753 section_alignment = exp_get_power (os->section_alignment,
5754 "section alignment");
5755
5756 /* Align to what the section needs. */
5757 if (section_alignment > 0)
5758 {
5759 bfd_vma savedot = newdot;
5760 bfd_vma diff = 0;
5761
5762 newdot = align_power (newdot, section_alignment);
5763 dotdelta = newdot - savedot;
5764
5765 if (lang_sizing_iteration == 1)
5766 diff = dotdelta;
5767 else if (lang_sizing_iteration > 1)
5768 {
5769 /* Only report adjustments that would change
5770 alignment from what we have already reported. */
5771 diff = newdot - os->bfd_section->vma;
5772 if (!(diff & (((bfd_vma) 1 << section_alignment) - 1)))
5773 diff = 0;
5774 }
5775 if (diff != 0
5776 && (config.warn_section_align
5777 || os->addr_tree != NULL))
5778 einfo (_("%P: warning: "
5779 "start of section %s changed by %ld\n"),
5780 os->name, (long) diff);
5781 }
5782
5783 bfd_set_section_vma (os->bfd_section, newdot);
5784
5785 os->bfd_section->output_offset = 0;
5786 }
5787
5788 lang_size_sections_1 (&os->children.head, os,
5789 os->fill, newdot, relax, check_regions);
5790
5791 os->processed_vma = true;
5792
5793 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
5794 /* Except for some special linker created sections,
5795 no output section should change from zero size
5796 after strip_excluded_output_sections. A non-zero
5797 size on an ignored section indicates that some
5798 input section was not sized early enough. */
5799 ASSERT (os->bfd_section->size == 0);
5800 else
5801 {
5802 dot = os->bfd_section->vma;
5803
5804 /* Put the section within the requested block size, or
5805 align at the block boundary. */
5806 after = ((dot
5807 + TO_ADDR (os->bfd_section->size)
5808 + os->block_value - 1)
5809 & - (bfd_vma) os->block_value);
5810
5811 if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
5812 os->bfd_section->size = TO_SIZE (after
5813 - os->bfd_section->vma);
5814 }
5815
5816 /* Set section lma. */
5817 r = os->region;
5818 if (r == NULL)
5819 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false);
5820
5821 if (os->load_base)
5822 {
5823 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
5824 os->bfd_section->lma = lma;
5825 }
5826 else if (os->lma_region != NULL)
5827 {
5828 bfd_vma lma = os->lma_region->current;
5829
5830 if (os->align_lma_with_input)
5831 lma += dotdelta;
5832 else
5833 {
5834 /* When LMA_REGION is the same as REGION, align the LMA
5835 as we did for the VMA, possibly including alignment
5836 from the bfd section. If a different region, then
5837 only align according to the value in the output
5838 statement. */
5839 if (os->lma_region != os->region)
5840 section_alignment = exp_get_power (os->section_alignment,
5841 "section alignment");
5842 if (section_alignment > 0)
5843 lma = align_power (lma, section_alignment);
5844 }
5845 os->bfd_section->lma = lma;
5846 }
5847 else if (r->last_os != NULL
5848 && (os->bfd_section->flags & SEC_ALLOC) != 0)
5849 {
5850 bfd_vma lma;
5851 asection *last;
5852
5853 last = r->last_os->output_section_statement.bfd_section;
5854
5855 /* A backwards move of dot should be accompanied by
5856 an explicit assignment to the section LMA (ie.
5857 os->load_base set) because backwards moves can
5858 create overlapping LMAs. */
5859 if (dot < last->vma
5860 && os->bfd_section->size != 0
5861 && dot + TO_ADDR (os->bfd_section->size) <= last->vma)
5862 {
5863 /* If dot moved backwards then leave lma equal to
5864 vma. This is the old default lma, which might
5865 just happen to work when the backwards move is
5866 sufficiently large. Nag if this changes anything,
5867 so people can fix their linker scripts. */
5868
5869 if (last->vma != last->lma)
5870 einfo (_("%P: warning: dot moved backwards "
5871 "before `%s'\n"), os->name);
5872 }
5873 else
5874 {
5875 /* If this is an overlay, set the current lma to that
5876 at the end of the previous section. */
5877 if (os->sectype == overlay_section)
5878 lma = last->lma + TO_ADDR (last->size);
5879
5880 /* Otherwise, keep the same lma to vma relationship
5881 as the previous section. */
5882 else
5883 lma = os->bfd_section->vma + last->lma - last->vma;
5884
5885 if (section_alignment > 0)
5886 lma = align_power (lma, section_alignment);
5887 os->bfd_section->lma = lma;
5888 }
5889 }
5890 os->processed_lma = true;
5891
5892 /* Keep track of normal sections using the default
5893 lma region. We use this to set the lma for
5894 following sections. Overlays or other linker
5895 script assignment to lma might mean that the
5896 default lma == vma is incorrect.
5897 To avoid warnings about dot moving backwards when using
5898 -Ttext, don't start tracking sections until we find one
5899 of non-zero size or with lma set differently to vma.
5900 Do this tracking before we short-cut the loop so that we
5901 track changes for the case where the section size is zero,
5902 but the lma is set differently to the vma. This is
5903 important, if an orphan section is placed after an
5904 otherwise empty output section that has an explicit lma
5905 set, we want that lma reflected in the orphans lma. */
5906 if (((!IGNORE_SECTION (os->bfd_section)
5907 && (os->bfd_section->size != 0
5908 || (r->last_os == NULL
5909 && os->bfd_section->vma != os->bfd_section->lma)
5910 || (r->last_os != NULL
5911 && dot >= (r->last_os->output_section_statement
5912 .bfd_section->vma))))
5913 || os->sectype == first_overlay_section)
5914 && os->lma_region == NULL
5915 && !bfd_link_relocatable (&link_info))
5916 r->last_os = s;
5917
5918 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
5919 break;
5920
5921 /* .tbss sections effectively have zero size. */
5922 if (!IS_TBSS (os->bfd_section)
5923 || bfd_link_relocatable (&link_info))
5924 dotdelta = TO_ADDR (os->bfd_section->size);
5925 else
5926 dotdelta = 0;
5927 dot += dotdelta;
5928
5929 if (os->update_dot_tree != 0)
5930 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5931
5932 /* Update dot in the region ?
5933 We only do this if the section is going to be allocated,
5934 since unallocated sections do not contribute to the region's
5935 overall size in memory. */
5936 if (os->region != NULL
5937 && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))
5938 {
5939 os->region->current = dot;
5940
5941 if (check_regions)
5942 /* Make sure the new address is within the region. */
5943 os_region_check (os, os->region, os->addr_tree,
5944 os->bfd_section->vma);
5945
5946 if (os->lma_region != NULL && os->lma_region != os->region
5947 && ((os->bfd_section->flags & SEC_LOAD)
5948 || os->align_lma_with_input))
5949 {
5950 os->lma_region->current = os->bfd_section->lma + dotdelta;
5951
5952 if (check_regions)
5953 os_region_check (os, os->lma_region, NULL,
5954 os->bfd_section->lma);
5955 }
5956 }
5957 }
5958 break;
5959
5960 case lang_constructors_statement_enum:
5961 dot = lang_size_sections_1 (&constructor_list.head,
5962 output_section_statement,
5963 fill, dot, relax, check_regions);
5964 break;
5965
5966 case lang_data_statement_enum:
5967 {
5968 unsigned int size = 0;
5969
5970 s->data_statement.output_offset =
5971 dot - output_section_statement->bfd_section->vma;
5972 s->data_statement.output_section =
5973 output_section_statement->bfd_section;
5974
5975 /* We might refer to provided symbols in the expression, and
5976 need to mark them as needed. */
5977 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5978
5979 switch (s->data_statement.type)
5980 {
5981 default:
5982 abort ();
5983 case QUAD:
5984 case SQUAD:
5985 size = QUAD_SIZE;
5986 break;
5987 case LONG:
5988 size = LONG_SIZE;
5989 break;
5990 case SHORT:
5991 size = SHORT_SIZE;
5992 break;
5993 case BYTE:
5994 size = BYTE_SIZE;
5995 break;
5996 }
5997 if (size < TO_SIZE ((unsigned) 1))
5998 size = TO_SIZE ((unsigned) 1);
5999 dot += TO_ADDR (size);
6000 if (!(output_section_statement->bfd_section->flags
6001 & SEC_FIXED_SIZE))
6002 output_section_statement->bfd_section->size
6003 = TO_SIZE (dot - output_section_statement->bfd_section->vma);
6004
6005 }
6006 break;
6007
6008 case lang_reloc_statement_enum:
6009 {
6010 int size;
6011
6012 s->reloc_statement.output_offset =
6013 dot - output_section_statement->bfd_section->vma;
6014 s->reloc_statement.output_section =
6015 output_section_statement->bfd_section;
6016 size = bfd_get_reloc_size (s->reloc_statement.howto);
6017 dot += TO_ADDR (size);
6018 if (!(output_section_statement->bfd_section->flags
6019 & SEC_FIXED_SIZE))
6020 output_section_statement->bfd_section->size
6021 = TO_SIZE (dot - output_section_statement->bfd_section->vma);
6022 }
6023 break;
6024
6025 case lang_wild_statement_enum:
6026 dot = lang_size_sections_1 (&s->wild_statement.children.head,
6027 output_section_statement,
6028 fill, dot, relax, check_regions);
6029 break;
6030
6031 case lang_object_symbols_statement_enum:
6032 link_info.create_object_symbols_section
6033 = output_section_statement->bfd_section;
6034 output_section_statement->bfd_section->flags |= SEC_KEEP;
6035 break;
6036
6037 case lang_output_statement_enum:
6038 case lang_target_statement_enum:
6039 break;
6040
6041 case lang_input_section_enum:
6042 {
6043 asection *i;
6044
6045 i = s->input_section.section;
6046 if (relax)
6047 {
6048 bool again;
6049
6050 if (!bfd_relax_section (i->owner, i, &link_info, &again))
6051 einfo (_("%F%P: can't relax section: %E\n"));
6052 if (again)
6053 *relax = true;
6054 }
6055 dot = size_input_section (prev, output_section_statement,
6056 fill, &removed, dot);
6057 }
6058 break;
6059
6060 case lang_input_statement_enum:
6061 break;
6062
6063 case lang_fill_statement_enum:
6064 s->fill_statement.output_section =
6065 output_section_statement->bfd_section;
6066
6067 fill = s->fill_statement.fill;
6068 break;
6069
6070 case lang_assignment_statement_enum:
6071 {
6072 bfd_vma newdot = dot;
6073 etree_type *tree = s->assignment_statement.exp;
6074
6075 expld.dataseg.relro = exp_seg_relro_none;
6076
6077 exp_fold_tree (tree,
6078 output_section_statement->bfd_section,
6079 &newdot);
6080
6081 ldlang_check_relro_region (s);
6082
6083 expld.dataseg.relro = exp_seg_relro_none;
6084
6085 /* This symbol may be relative to this section. */
6086 if ((tree->type.node_class == etree_provided
6087 || tree->type.node_class == etree_assign)
6088 && (tree->assign.dst [0] != '.'
6089 || tree->assign.dst [1] != '\0'))
6090 output_section_statement->update_dot = 1;
6091
6092 if (!output_section_statement->ignored)
6093 {
6094 if (output_section_statement == abs_output_section)
6095 {
6096 /* If we don't have an output section, then just adjust
6097 the default memory address. */
6098 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
6099 false)->current = newdot;
6100 }
6101 else if (newdot != dot)
6102 {
6103 /* Insert a pad after this statement. We can't
6104 put the pad before when relaxing, in case the
6105 assignment references dot. */
6106 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
6107 output_section_statement->bfd_section, dot);
6108
6109 /* Don't neuter the pad below when relaxing. */
6110 s = s->header.next;
6111
6112 /* If dot is advanced, this implies that the section
6113 should have space allocated to it, unless the
6114 user has explicitly stated that the section
6115 should not be allocated. */
6116 if (output_section_statement->sectype != noalloc_section
6117 && (output_section_statement->sectype != noload_section
6118 || (bfd_get_flavour (link_info.output_bfd)
6119 == bfd_target_elf_flavour)))
6120 output_section_statement->bfd_section->flags |= SEC_ALLOC;
6121 }
6122 dot = newdot;
6123 }
6124 }
6125 break;
6126
6127 case lang_padding_statement_enum:
6128 /* If this is the first time lang_size_sections is called,
6129 we won't have any padding statements. If this is the
6130 second or later passes when relaxing, we should allow
6131 padding to shrink. If padding is needed on this pass, it
6132 will be added back in. */
6133 s->padding_statement.size = 0;
6134
6135 /* Make sure output_offset is valid. If relaxation shrinks
6136 the section and this pad isn't needed, it's possible to
6137 have output_offset larger than the final size of the
6138 section. bfd_set_section_contents will complain even for
6139 a pad size of zero. */
6140 s->padding_statement.output_offset
6141 = dot - output_section_statement->bfd_section->vma;
6142 break;
6143
6144 case lang_group_statement_enum:
6145 dot = lang_size_sections_1 (&s->group_statement.children.head,
6146 output_section_statement,
6147 fill, dot, relax, check_regions);
6148 break;
6149
6150 case lang_insert_statement_enum:
6151 break;
6152
6153 /* We can only get here when relaxing is turned on. */
6154 case lang_address_statement_enum:
6155 break;
6156
6157 default:
6158 FAIL ();
6159 break;
6160 }
6161
6162 /* If an input section doesn't fit in the current output
6163 section, remove it from the list. Handle the case where we
6164 have to remove an input_section statement here: there is a
6165 special case to remove the first element of the list. */
6166 if (link_info.non_contiguous_regions && removed)
6167 {
6168 /* If we removed the first element during the previous
6169 iteration, override the loop assignment of prev_s. */
6170 if (removed_prev_s)
6171 prev_s = NULL;
6172
6173 if (prev_s)
6174 {
6175 /* If there was a real previous input section, just skip
6176 the current one. */
6177 prev_s->header.next=s->header.next;
6178 s = prev_s;
6179 removed_prev_s = false;
6180 }
6181 else
6182 {
6183 /* Remove the first input section of the list. */
6184 *prev = s->header.next;
6185 removed_prev_s = true;
6186 }
6187
6188 /* Move to next element, unless we removed the head of the
6189 list. */
6190 if (!removed_prev_s)
6191 prev = &s->header.next;
6192 }
6193 else
6194 {
6195 prev = &s->header.next;
6196 removed_prev_s = false;
6197 }
6198 }
6199 return dot;
6200 }
6201
6202 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
6203 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
6204 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
6205 segments. We are allowed an opportunity to override this decision. */
6206
6207 bool
6208 ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6209 bfd *abfd ATTRIBUTE_UNUSED,
6210 asection *current_section,
6211 asection *previous_section,
6212 bool new_segment)
6213 {
6214 lang_output_section_statement_type *cur;
6215 lang_output_section_statement_type *prev;
6216
6217 /* The checks below are only necessary when the BFD library has decided
6218 that the two sections ought to be placed into the same segment. */
6219 if (new_segment)
6220 return true;
6221
6222 /* Paranoia checks. */
6223 if (current_section == NULL || previous_section == NULL)
6224 return new_segment;
6225
6226 /* If this flag is set, the target never wants code and non-code
6227 sections comingled in the same segment. */
6228 if (config.separate_code
6229 && ((current_section->flags ^ previous_section->flags) & SEC_CODE))
6230 return true;
6231
6232 /* Find the memory regions associated with the two sections.
6233 We call lang_output_section_find() here rather than scanning the list
6234 of output sections looking for a matching section pointer because if
6235 we have a large number of sections then a hash lookup is faster. */
6236 cur = lang_output_section_find (current_section->name);
6237 prev = lang_output_section_find (previous_section->name);
6238
6239 /* More paranoia. */
6240 if (cur == NULL || prev == NULL)
6241 return new_segment;
6242
6243 /* If the regions are different then force the sections to live in
6244 different segments. See the email thread starting at the following
6245 URL for the reasons why this is necessary:
6246 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
6247 return cur->region != prev->region;
6248 }
6249
6250 void
6251 one_lang_size_sections_pass (bool *relax, bool check_regions)
6252 {
6253 lang_statement_iteration++;
6254 if (expld.phase != lang_mark_phase_enum)
6255 lang_sizing_iteration++;
6256 lang_size_sections_1 (&statement_list.head, abs_output_section,
6257 0, 0, relax, check_regions);
6258 }
6259
6260 static bool
6261 lang_size_segment (void)
6262 {
6263 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
6264 a page could be saved in the data segment. */
6265 seg_align_type *seg = &expld.dataseg;
6266 bfd_vma first, last;
6267
6268 first = -seg->base & (seg->commonpagesize - 1);
6269 last = seg->end & (seg->commonpagesize - 1);
6270 if (first && last
6271 && ((seg->base & ~(seg->commonpagesize - 1))
6272 != (seg->end & ~(seg->commonpagesize - 1)))
6273 && first + last <= seg->commonpagesize)
6274 {
6275 seg->phase = exp_seg_adjust;
6276 return true;
6277 }
6278
6279 seg->phase = exp_seg_done;
6280 return false;
6281 }
6282
6283 static bfd_vma
6284 lang_size_relro_segment_1 (void)
6285 {
6286 seg_align_type *seg = &expld.dataseg;
6287 bfd_vma relro_end, desired_end;
6288 asection *sec;
6289
6290 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
6291 relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize;
6292
6293 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
6294 desired_end = relro_end - seg->relro_offset;
6295
6296 /* For sections in the relro segment.. */
6297 for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev)
6298 if ((sec->flags & SEC_ALLOC) != 0
6299 && sec->vma >= seg->base
6300 && sec->vma < seg->relro_end - seg->relro_offset)
6301 {
6302 /* Where do we want to put this section so that it ends as
6303 desired? */
6304 bfd_vma start, end, bump;
6305
6306 end = start = sec->vma;
6307 if (!IS_TBSS (sec))
6308 end += TO_ADDR (sec->size);
6309 bump = desired_end - end;
6310 /* We'd like to increase START by BUMP, but we must heed
6311 alignment so the increase might be less than optimum. */
6312 start += bump;
6313 start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1);
6314 /* This is now the desired end for the previous section. */
6315 desired_end = start;
6316 }
6317
6318 seg->phase = exp_seg_relro_adjust;
6319 ASSERT (desired_end >= seg->base);
6320 seg->base = desired_end;
6321 return relro_end;
6322 }
6323
6324 static bool
6325 lang_size_relro_segment (bool *relax, bool check_regions)
6326 {
6327 bool do_reset = false;
6328
6329 if (link_info.relro && expld.dataseg.relro_end)
6330 {
6331 bfd_vma data_initial_base = expld.dataseg.base;
6332 bfd_vma data_relro_end = lang_size_relro_segment_1 ();
6333
6334 lang_reset_memory_regions ();
6335 one_lang_size_sections_pass (relax, check_regions);
6336
6337 /* Assignments to dot, or to output section address in a user
6338 script have increased padding over the original. Revert. */
6339 if (expld.dataseg.relro_end > data_relro_end)
6340 {
6341 expld.dataseg.base = data_initial_base;
6342 do_reset = true;
6343 }
6344 }
6345 else if (lang_size_segment ())
6346 do_reset = true;
6347
6348 return do_reset;
6349 }
6350
6351 void
6352 lang_size_sections (bool *relax, bool check_regions)
6353 {
6354 expld.phase = lang_allocating_phase_enum;
6355 expld.dataseg.phase = exp_seg_none;
6356
6357 one_lang_size_sections_pass (relax, check_regions);
6358
6359 if (expld.dataseg.phase != exp_seg_end_seen)
6360 expld.dataseg.phase = exp_seg_done;
6361
6362 if (expld.dataseg.phase == exp_seg_end_seen)
6363 {
6364 bool do_reset
6365 = lang_size_relro_segment (relax, check_regions);
6366
6367 if (do_reset)
6368 {
6369 lang_reset_memory_regions ();
6370 one_lang_size_sections_pass (relax, check_regions);
6371 }
6372
6373 if (link_info.relro && expld.dataseg.relro_end)
6374 {
6375 link_info.relro_start = expld.dataseg.base;
6376 link_info.relro_end = expld.dataseg.relro_end;
6377 }
6378 }
6379 }
6380
6381 static lang_output_section_statement_type *current_section;
6382 static lang_assignment_statement_type *current_assign;
6383 static bool prefer_next_section;
6384
6385 /* Worker function for lang_do_assignments. Recursiveness goes here. */
6386
6387 static bfd_vma
6388 lang_do_assignments_1 (lang_statement_union_type *s,
6389 lang_output_section_statement_type *current_os,
6390 fill_type *fill,
6391 bfd_vma dot,
6392 bool *found_end)
6393 {
6394 for (; s != NULL; s = s->header.next)
6395 {
6396 switch (s->header.type)
6397 {
6398 case lang_constructors_statement_enum:
6399 dot = lang_do_assignments_1 (constructor_list.head,
6400 current_os, fill, dot, found_end);
6401 break;
6402
6403 case lang_output_section_statement_enum:
6404 {
6405 lang_output_section_statement_type *os;
6406 bfd_vma newdot;
6407
6408 os = &(s->output_section_statement);
6409 os->after_end = *found_end;
6410 init_opb (os->bfd_section);
6411 newdot = dot;
6412 if (os->bfd_section != NULL)
6413 {
6414 if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0)
6415 {
6416 current_section = os;
6417 prefer_next_section = false;
6418 }
6419 newdot = os->bfd_section->vma;
6420 }
6421 newdot = lang_do_assignments_1 (os->children.head,
6422 os, os->fill, newdot, found_end);
6423 if (!os->ignored)
6424 {
6425 if (os->bfd_section != NULL)
6426 {
6427 newdot = os->bfd_section->vma;
6428
6429 /* .tbss sections effectively have zero size. */
6430 if (!IS_TBSS (os->bfd_section)
6431 || bfd_link_relocatable (&link_info))
6432 newdot += TO_ADDR (os->bfd_section->size);
6433
6434 if (os->update_dot_tree != NULL)
6435 exp_fold_tree (os->update_dot_tree,
6436 bfd_abs_section_ptr, &newdot);
6437 }
6438 dot = newdot;
6439 }
6440 }
6441 break;
6442
6443 case lang_wild_statement_enum:
6444
6445 dot = lang_do_assignments_1 (s->wild_statement.children.head,
6446 current_os, fill, dot, found_end);
6447 break;
6448
6449 case lang_object_symbols_statement_enum:
6450 case lang_output_statement_enum:
6451 case lang_target_statement_enum:
6452 break;
6453
6454 case lang_data_statement_enum:
6455 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
6456 if (expld.result.valid_p)
6457 {
6458 s->data_statement.value = expld.result.value;
6459 if (expld.result.section != NULL)
6460 s->data_statement.value += expld.result.section->vma;
6461 }
6462 else if (expld.phase == lang_final_phase_enum)
6463 einfo (_("%F%P: invalid data statement\n"));
6464 {
6465 unsigned int size;
6466 switch (s->data_statement.type)
6467 {
6468 default:
6469 abort ();
6470 case QUAD:
6471 case SQUAD:
6472 size = QUAD_SIZE;
6473 break;
6474 case LONG:
6475 size = LONG_SIZE;
6476 break;
6477 case SHORT:
6478 size = SHORT_SIZE;
6479 break;
6480 case BYTE:
6481 size = BYTE_SIZE;
6482 break;
6483 }
6484 if (size < TO_SIZE ((unsigned) 1))
6485 size = TO_SIZE ((unsigned) 1);
6486 dot += TO_ADDR (size);
6487 }
6488 break;
6489
6490 case lang_reloc_statement_enum:
6491 exp_fold_tree (s->reloc_statement.addend_exp,
6492 bfd_abs_section_ptr, &dot);
6493 if (expld.result.valid_p)
6494 s->reloc_statement.addend_value = expld.result.value;
6495 else if (expld.phase == lang_final_phase_enum)
6496 einfo (_("%F%P: invalid reloc statement\n"));
6497 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
6498 break;
6499
6500 case lang_input_section_enum:
6501 {
6502 asection *in = s->input_section.section;
6503
6504 if ((in->flags & SEC_EXCLUDE) == 0)
6505 dot += TO_ADDR (in->size);
6506 }
6507 break;
6508
6509 case lang_input_statement_enum:
6510 break;
6511
6512 case lang_fill_statement_enum:
6513 fill = s->fill_statement.fill;
6514 break;
6515
6516 case lang_assignment_statement_enum:
6517 current_assign = &s->assignment_statement;
6518 if (current_assign->exp->type.node_class != etree_assert)
6519 {
6520 const char *p = current_assign->exp->assign.dst;
6521
6522 if (current_os == abs_output_section && p[0] == '.' && p[1] == 0)
6523 prefer_next_section = true;
6524
6525 while (*p == '_')
6526 ++p;
6527 if (strcmp (p, "end") == 0)
6528 *found_end = true;
6529 }
6530 exp_fold_tree (s->assignment_statement.exp,
6531 (current_os->bfd_section != NULL
6532 ? current_os->bfd_section : bfd_und_section_ptr),
6533 &dot);
6534 break;
6535
6536 case lang_padding_statement_enum:
6537 dot += TO_ADDR (s->padding_statement.size);
6538 break;
6539
6540 case lang_group_statement_enum:
6541 dot = lang_do_assignments_1 (s->group_statement.children.head,
6542 current_os, fill, dot, found_end);
6543 break;
6544
6545 case lang_insert_statement_enum:
6546 break;
6547
6548 case lang_address_statement_enum:
6549 break;
6550
6551 default:
6552 FAIL ();
6553 break;
6554 }
6555 }
6556 return dot;
6557 }
6558
6559 void
6560 lang_do_assignments (lang_phase_type phase)
6561 {
6562 bool found_end = false;
6563
6564 current_section = NULL;
6565 prefer_next_section = false;
6566 expld.phase = phase;
6567 lang_statement_iteration++;
6568 lang_do_assignments_1 (statement_list.head,
6569 abs_output_section, NULL, 0, &found_end);
6570 }
6571
6572 /* For an assignment statement outside of an output section statement,
6573 choose the best of neighbouring output sections to use for values
6574 of "dot". */
6575
6576 asection *
6577 section_for_dot (void)
6578 {
6579 asection *s;
6580
6581 /* Assignments belong to the previous output section, unless there
6582 has been an assignment to "dot", in which case following
6583 assignments belong to the next output section. (The assumption
6584 is that an assignment to "dot" is setting up the address for the
6585 next output section.) Except that past the assignment to "_end"
6586 we always associate with the previous section. This exception is
6587 for targets like SH that define an alloc .stack or other
6588 weirdness after non-alloc sections. */
6589 if (current_section == NULL || prefer_next_section)
6590 {
6591 lang_statement_union_type *stmt;
6592 lang_output_section_statement_type *os;
6593
6594 for (stmt = (lang_statement_union_type *) current_assign;
6595 stmt != NULL;
6596 stmt = stmt->header.next)
6597 if (stmt->header.type == lang_output_section_statement_enum)
6598 break;
6599
6600 os = stmt ? &stmt->output_section_statement : NULL;
6601 while (os != NULL
6602 && !os->after_end
6603 && (os->bfd_section == NULL
6604 || (os->bfd_section->flags & SEC_EXCLUDE) != 0
6605 || bfd_section_removed_from_list (link_info.output_bfd,
6606 os->bfd_section)))
6607 os = os->next;
6608
6609 if (current_section == NULL || os == NULL || !os->after_end)
6610 {
6611 if (os != NULL)
6612 s = os->bfd_section;
6613 else
6614 s = link_info.output_bfd->section_last;
6615 while (s != NULL
6616 && ((s->flags & SEC_ALLOC) == 0
6617 || (s->flags & SEC_THREAD_LOCAL) != 0))
6618 s = s->prev;
6619 if (s != NULL)
6620 return s;
6621
6622 return bfd_abs_section_ptr;
6623 }
6624 }
6625
6626 s = current_section->bfd_section;
6627
6628 /* The section may have been stripped. */
6629 while (s != NULL
6630 && ((s->flags & SEC_EXCLUDE) != 0
6631 || (s->flags & SEC_ALLOC) == 0
6632 || (s->flags & SEC_THREAD_LOCAL) != 0
6633 || bfd_section_removed_from_list (link_info.output_bfd, s)))
6634 s = s->prev;
6635 if (s == NULL)
6636 s = link_info.output_bfd->sections;
6637 while (s != NULL
6638 && ((s->flags & SEC_ALLOC) == 0
6639 || (s->flags & SEC_THREAD_LOCAL) != 0))
6640 s = s->next;
6641 if (s != NULL)
6642 return s;
6643
6644 return bfd_abs_section_ptr;
6645 }
6646
6647 /* Array of __start/__stop/.startof./.sizeof/ symbols. */
6648
6649 static struct bfd_link_hash_entry **start_stop_syms;
6650 static size_t start_stop_count = 0;
6651 static size_t start_stop_alloc = 0;
6652
6653 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
6654 to start_stop_syms. */
6655
6656 static void
6657 lang_define_start_stop (const char *symbol, asection *sec)
6658 {
6659 struct bfd_link_hash_entry *h;
6660
6661 h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec);
6662 if (h != NULL)
6663 {
6664 if (start_stop_count == start_stop_alloc)
6665 {
6666 start_stop_alloc = 2 * start_stop_alloc + 10;
6667 start_stop_syms
6668 = xrealloc (start_stop_syms,
6669 start_stop_alloc * sizeof (*start_stop_syms));
6670 }
6671 start_stop_syms[start_stop_count++] = h;
6672 }
6673 }
6674
6675 /* Check for input sections whose names match references to
6676 __start_SECNAME or __stop_SECNAME symbols. Give the symbols
6677 preliminary definitions. */
6678
6679 static void
6680 lang_init_start_stop (void)
6681 {
6682 bfd *abfd;
6683 asection *s;
6684 char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd);
6685
6686 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
6687 for (s = abfd->sections; s != NULL; s = s->next)
6688 {
6689 const char *ps;
6690 const char *secname = s->name;
6691
6692 for (ps = secname; *ps != '\0'; ps++)
6693 if (!ISALNUM ((unsigned char) *ps) && *ps != '_')
6694 break;
6695 if (*ps == '\0')
6696 {
6697 char *symbol = (char *) xmalloc (10 + strlen (secname));
6698
6699 symbol[0] = leading_char;
6700 sprintf (symbol + (leading_char != 0), "__start_%s", secname);
6701 lang_define_start_stop (symbol, s);
6702
6703 symbol[1] = leading_char;
6704 memcpy (symbol + 1 + (leading_char != 0), "__stop", 6);
6705 lang_define_start_stop (symbol + 1, s);
6706
6707 free (symbol);
6708 }
6709 }
6710 }
6711
6712 /* Iterate over start_stop_syms. */
6713
6714 static void
6715 foreach_start_stop (void (*func) (struct bfd_link_hash_entry *))
6716 {
6717 size_t i;
6718
6719 for (i = 0; i < start_stop_count; ++i)
6720 func (start_stop_syms[i]);
6721 }
6722
6723 /* __start and __stop symbols are only supposed to be defined by the
6724 linker for orphan sections, but we now extend that to sections that
6725 map to an output section of the same name. The symbols were
6726 defined early for --gc-sections, before we mapped input to output
6727 sections, so undo those that don't satisfy this rule. */
6728
6729 static void
6730 undef_start_stop (struct bfd_link_hash_entry *h)
6731 {
6732 if (h->ldscript_def)
6733 return;
6734
6735 if (h->u.def.section->output_section == NULL
6736 || h->u.def.section->output_section->owner != link_info.output_bfd
6737 || strcmp (h->u.def.section->name,
6738 h->u.def.section->output_section->name) != 0)
6739 {
6740 asection *sec = bfd_get_section_by_name (link_info.output_bfd,
6741 h->u.def.section->name);
6742 if (sec != NULL)
6743 {
6744 /* When there are more than one input sections with the same
6745 section name, SECNAME, linker picks the first one to define
6746 __start_SECNAME and __stop_SECNAME symbols. When the first
6747 input section is removed by comdat group, we need to check
6748 if there is still an output section with section name
6749 SECNAME. */
6750 asection *i;
6751 for (i = sec->map_head.s; i != NULL; i = i->map_head.s)
6752 if (strcmp (h->u.def.section->name, i->name) == 0)
6753 {
6754 h->u.def.section = i;
6755 return;
6756 }
6757 }
6758 h->type = bfd_link_hash_undefined;
6759 h->u.undef.abfd = NULL;
6760 if (is_elf_hash_table (link_info.hash))
6761 {
6762 const struct elf_backend_data *bed;
6763 struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
6764 unsigned int was_forced = eh->forced_local;
6765
6766 bed = get_elf_backend_data (link_info.output_bfd);
6767 (*bed->elf_backend_hide_symbol) (&link_info, eh, true);
6768 if (!eh->ref_regular_nonweak)
6769 h->type = bfd_link_hash_undefweak;
6770 eh->def_regular = 0;
6771 eh->forced_local = was_forced;
6772 }
6773 }
6774 }
6775
6776 static void
6777 lang_undef_start_stop (void)
6778 {
6779 foreach_start_stop (undef_start_stop);
6780 }
6781
6782 /* Check for output sections whose names match references to
6783 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
6784 preliminary definitions. */
6785
6786 static void
6787 lang_init_startof_sizeof (void)
6788 {
6789 asection *s;
6790
6791 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
6792 {
6793 const char *secname = s->name;
6794 char *symbol = (char *) xmalloc (10 + strlen (secname));
6795
6796 sprintf (symbol, ".startof.%s", secname);
6797 lang_define_start_stop (symbol, s);
6798
6799 memcpy (symbol + 1, ".size", 5);
6800 lang_define_start_stop (symbol + 1, s);
6801 free (symbol);
6802 }
6803 }
6804
6805 /* Set .startof., .sizeof., __start and __stop symbols final values. */
6806
6807 static void
6808 set_start_stop (struct bfd_link_hash_entry *h)
6809 {
6810 if (h->ldscript_def
6811 || h->type != bfd_link_hash_defined)
6812 return;
6813
6814 if (h->root.string[0] == '.')
6815 {
6816 /* .startof. or .sizeof. symbol.
6817 .startof. already has final value. */
6818 if (h->root.string[2] == 'i')
6819 {
6820 /* .sizeof. */
6821 h->u.def.value = TO_ADDR (h->u.def.section->size);
6822 h->u.def.section = bfd_abs_section_ptr;
6823 }
6824 }
6825 else
6826 {
6827 /* __start or __stop symbol. */
6828 int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0;
6829
6830 h->u.def.section = h->u.def.section->output_section;
6831 if (h->root.string[4 + has_lead] == 'o')
6832 {
6833 /* __stop_ */
6834 h->u.def.value = TO_ADDR (h->u.def.section->size);
6835 }
6836 }
6837 }
6838
6839 static void
6840 lang_finalize_start_stop (void)
6841 {
6842 foreach_start_stop (set_start_stop);
6843 }
6844
6845 static void
6846 lang_symbol_tweaks (void)
6847 {
6848 /* Give initial values for __start and __stop symbols, so that ELF
6849 gc_sections will keep sections referenced by these symbols. Must
6850 be done before lang_do_assignments. */
6851 if (config.build_constructors)
6852 lang_init_start_stop ();
6853
6854 /* Make __ehdr_start hidden, and set def_regular even though it is
6855 likely undefined at this stage. For lang_check_relocs. */
6856 if (is_elf_hash_table (link_info.hash)
6857 && !bfd_link_relocatable (&link_info))
6858 {
6859 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
6860 bfd_link_hash_lookup (link_info.hash, "__ehdr_start",
6861 false, false, true);
6862
6863 /* Only adjust the export class if the symbol was referenced
6864 and not defined, otherwise leave it alone. */
6865 if (h != NULL
6866 && (h->root.type == bfd_link_hash_new
6867 || h->root.type == bfd_link_hash_undefined
6868 || h->root.type == bfd_link_hash_undefweak
6869 || h->root.type == bfd_link_hash_common))
6870 {
6871 const struct elf_backend_data *bed;
6872 bed = get_elf_backend_data (link_info.output_bfd);
6873 (*bed->elf_backend_hide_symbol) (&link_info, h, true);
6874 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
6875 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6876 h->def_regular = 1;
6877 h->root.linker_def = 1;
6878 h->root.rel_from_abs = 1;
6879 }
6880 }
6881 }
6882
6883 static void
6884 lang_end (void)
6885 {
6886 struct bfd_link_hash_entry *h;
6887 bool warn;
6888
6889 if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections)
6890 || bfd_link_dll (&link_info))
6891 warn = entry_from_cmdline;
6892 else
6893 warn = true;
6894
6895 /* Force the user to specify a root when generating a relocatable with
6896 --gc-sections, unless --gc-keep-exported was also given. */
6897 if (bfd_link_relocatable (&link_info)
6898 && link_info.gc_sections
6899 && !link_info.gc_keep_exported)
6900 {
6901 struct bfd_sym_chain *sym;
6902
6903 for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next)
6904 {
6905 h = bfd_link_hash_lookup (link_info.hash, sym->name,
6906 false, false, false);
6907 if (h != NULL
6908 && (h->type == bfd_link_hash_defined
6909 || h->type == bfd_link_hash_defweak)
6910 && !bfd_is_const_section (h->u.def.section))
6911 break;
6912 }
6913 if (!sym)
6914 einfo (_("%F%P: --gc-sections requires a defined symbol root "
6915 "specified by -e or -u\n"));
6916 }
6917
6918 if (entry_symbol.name == NULL)
6919 {
6920 /* No entry has been specified. Look for the default entry, but
6921 don't warn if we don't find it. */
6922 entry_symbol.name = entry_symbol_default;
6923 warn = false;
6924 }
6925
6926 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
6927 false, false, true);
6928 if (h != NULL
6929 && (h->type == bfd_link_hash_defined
6930 || h->type == bfd_link_hash_defweak)
6931 && h->u.def.section->output_section != NULL)
6932 {
6933 bfd_vma val;
6934
6935 val = (h->u.def.value
6936 + bfd_section_vma (h->u.def.section->output_section)
6937 + h->u.def.section->output_offset);
6938 if (!bfd_set_start_address (link_info.output_bfd, val))
6939 einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name);
6940 }
6941 else
6942 {
6943 bfd_vma val;
6944 const char *send;
6945
6946 /* We couldn't find the entry symbol. Try parsing it as a
6947 number. */
6948 val = bfd_scan_vma (entry_symbol.name, &send, 0);
6949 if (*send == '\0')
6950 {
6951 if (!bfd_set_start_address (link_info.output_bfd, val))
6952 einfo (_("%F%P: can't set start address\n"));
6953 }
6954 /* BZ 2004952: Only use the start of the entry section for executables. */
6955 else if bfd_link_executable (&link_info)
6956 {
6957 asection *ts;
6958
6959 /* Can't find the entry symbol, and it's not a number. Use
6960 the first address in the text section. */
6961 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
6962 if (ts != NULL)
6963 {
6964 if (warn)
6965 einfo (_("%P: warning: cannot find entry symbol %s;"
6966 " defaulting to %V\n"),
6967 entry_symbol.name,
6968 bfd_section_vma (ts));
6969 if (!bfd_set_start_address (link_info.output_bfd,
6970 bfd_section_vma (ts)))
6971 einfo (_("%F%P: can't set start address\n"));
6972 }
6973 else
6974 {
6975 if (warn)
6976 einfo (_("%P: warning: cannot find entry symbol %s;"
6977 " not setting start address\n"),
6978 entry_symbol.name);
6979 }
6980 }
6981 else
6982 {
6983 if (warn)
6984 einfo (_("%P: warning: cannot find entry symbol %s;"
6985 " not setting start address\n"),
6986 entry_symbol.name);
6987 }
6988 }
6989 }
6990
6991 /* This is a small function used when we want to ignore errors from
6992 BFD. */
6993
6994 static void
6995 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED,
6996 va_list ap ATTRIBUTE_UNUSED)
6997 {
6998 /* Don't do anything. */
6999 }
7000
7001 /* Check that the architecture of all the input files is compatible
7002 with the output file. Also call the backend to let it do any
7003 other checking that is needed. */
7004
7005 static void
7006 lang_check (void)
7007 {
7008 lang_input_statement_type *file;
7009 bfd *input_bfd;
7010 const bfd_arch_info_type *compatible;
7011
7012 for (file = (void *) file_chain.head;
7013 file != NULL;
7014 file = file->next)
7015 {
7016 #if BFD_SUPPORTS_PLUGINS
7017 /* Don't check format of files claimed by plugin. */
7018 if (file->flags.claimed)
7019 continue;
7020 #endif /* BFD_SUPPORTS_PLUGINS */
7021 input_bfd = file->the_bfd;
7022 compatible
7023 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
7024 command_line.accept_unknown_input_arch);
7025
7026 /* In general it is not possible to perform a relocatable
7027 link between differing object formats when the input
7028 file has relocations, because the relocations in the
7029 input format may not have equivalent representations in
7030 the output format (and besides BFD does not translate
7031 relocs for other link purposes than a final link). */
7032 if (!file->flags.just_syms
7033 && (bfd_link_relocatable (&link_info)
7034 || link_info.emitrelocations)
7035 && (compatible == NULL
7036 || (bfd_get_flavour (input_bfd)
7037 != bfd_get_flavour (link_info.output_bfd)))
7038 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
7039 {
7040 einfo (_("%F%P: relocatable linking with relocations from"
7041 " format %s (%pB) to format %s (%pB) is not supported\n"),
7042 bfd_get_target (input_bfd), input_bfd,
7043 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
7044 /* einfo with %F exits. */
7045 }
7046
7047 if (compatible == NULL)
7048 {
7049 if (command_line.warn_mismatch)
7050 einfo (_("%X%P: %s architecture of input file `%pB'"
7051 " is incompatible with %s output\n"),
7052 bfd_printable_name (input_bfd), input_bfd,
7053 bfd_printable_name (link_info.output_bfd));
7054 }
7055
7056 /* If the input bfd has no contents, it shouldn't set the
7057 private data of the output bfd. */
7058 else if (!file->flags.just_syms
7059 && ((input_bfd->flags & DYNAMIC) != 0
7060 || bfd_count_sections (input_bfd) != 0))
7061 {
7062 bfd_error_handler_type pfn = NULL;
7063
7064 /* If we aren't supposed to warn about mismatched input
7065 files, temporarily set the BFD error handler to a
7066 function which will do nothing. We still want to call
7067 bfd_merge_private_bfd_data, since it may set up
7068 information which is needed in the output file. */
7069 if (!command_line.warn_mismatch)
7070 pfn = bfd_set_error_handler (ignore_bfd_errors);
7071 if (!bfd_merge_private_bfd_data (input_bfd, &link_info))
7072 {
7073 if (command_line.warn_mismatch)
7074 einfo (_("%X%P: failed to merge target specific data"
7075 " of file %pB\n"), input_bfd);
7076 }
7077 if (!command_line.warn_mismatch)
7078 bfd_set_error_handler (pfn);
7079 }
7080 }
7081 }
7082
7083 /* Look through all the global common symbols and attach them to the
7084 correct section. The -sort-common command line switch may be used
7085 to roughly sort the entries by alignment. */
7086
7087 static void
7088 lang_common (void)
7089 {
7090 if (link_info.inhibit_common_definition)
7091 return;
7092 if (bfd_link_relocatable (&link_info)
7093 && !command_line.force_common_definition)
7094 return;
7095
7096 if (!config.sort_common)
7097 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
7098 else
7099 {
7100 unsigned int power;
7101
7102 if (config.sort_common == sort_descending)
7103 {
7104 for (power = 4; power > 0; power--)
7105 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7106
7107 power = 0;
7108 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7109 }
7110 else
7111 {
7112 for (power = 0; power <= 4; power++)
7113 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7114
7115 power = (unsigned int) -1;
7116 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
7117 }
7118 }
7119 }
7120
7121 /* Place one common symbol in the correct section. */
7122
7123 static bool
7124 lang_one_common (struct bfd_link_hash_entry *h, void *info)
7125 {
7126 unsigned int power_of_two;
7127 bfd_vma size;
7128 asection *section;
7129
7130 if (h->type != bfd_link_hash_common)
7131 return true;
7132
7133 size = h->u.c.size;
7134 power_of_two = h->u.c.p->alignment_power;
7135
7136 if (config.sort_common == sort_descending
7137 && power_of_two < *(unsigned int *) info)
7138 return true;
7139 else if (config.sort_common == sort_ascending
7140 && power_of_two > *(unsigned int *) info)
7141 return true;
7142
7143 section = h->u.c.p->section;
7144 if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
7145 einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
7146 h->root.string);
7147
7148 if (config.map_file != NULL)
7149 {
7150 static bool header_printed;
7151 int len;
7152 char *name;
7153 char buf[32];
7154
7155 if (!header_printed)
7156 {
7157 minfo (_("\nAllocating common symbols\n"));
7158 minfo (_("Common symbol size file\n\n"));
7159 header_printed = true;
7160 }
7161
7162 name = bfd_demangle (link_info.output_bfd, h->root.string,
7163 DMGL_ANSI | DMGL_PARAMS);
7164 if (name == NULL)
7165 {
7166 minfo ("%s", h->root.string);
7167 len = strlen (h->root.string);
7168 }
7169 else
7170 {
7171 minfo ("%s", name);
7172 len = strlen (name);
7173 free (name);
7174 }
7175
7176 if (len >= 19)
7177 {
7178 print_nl ();
7179 len = 0;
7180 }
7181
7182 sprintf (buf, "%" PRIx64, (uint64_t) size);
7183 fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf);
7184
7185 minfo ("%pB\n", section->owner);
7186 }
7187
7188 return true;
7189 }
7190
7191 /* Handle a single orphan section S, placing the orphan into an appropriate
7192 output section. The effects of the --orphan-handling command line
7193 option are handled here. */
7194
7195 static void
7196 ldlang_place_orphan (asection *s)
7197 {
7198 if (config.orphan_handling == orphan_handling_discard)
7199 {
7200 lang_output_section_statement_type *os;
7201 os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1);
7202 if (os->addr_tree == NULL
7203 && (bfd_link_relocatable (&link_info)
7204 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
7205 os->addr_tree = exp_intop (0);
7206 lang_add_section (&os->children, s, NULL, NULL, os);
7207 }
7208 else
7209 {
7210 lang_output_section_statement_type *os;
7211 const char *name = s->name;
7212 int constraint = 0;
7213
7214 if (config.orphan_handling == orphan_handling_error)
7215 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
7216 s, s->owner);
7217
7218 if (config.unique_orphan_sections || unique_section_p (s, NULL))
7219 constraint = SPECIAL;
7220
7221 os = ldemul_place_orphan (s, name, constraint);
7222 if (os == NULL)
7223 {
7224 os = lang_output_section_statement_lookup (name, constraint, 1);
7225 if (os->addr_tree == NULL
7226 && (bfd_link_relocatable (&link_info)
7227 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
7228 os->addr_tree = exp_intop (0);
7229 lang_add_section (&os->children, s, NULL, NULL, os);
7230 }
7231
7232 if (config.orphan_handling == orphan_handling_warn)
7233 einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
7234 "placed in section `%s'\n"),
7235 s, s->owner, os->name);
7236 }
7237 }
7238
7239 /* Run through the input files and ensure that every input section has
7240 somewhere to go. If one is found without a destination then create
7241 an input request and place it into the statement tree. */
7242
7243 static void
7244 lang_place_orphans (void)
7245 {
7246 LANG_FOR_EACH_INPUT_STATEMENT (file)
7247 {
7248 asection *s;
7249
7250 for (s = file->the_bfd->sections; s != NULL; s = s->next)
7251 {
7252 if (s->output_section == NULL)
7253 {
7254 /* This section of the file is not attached, root
7255 around for a sensible place for it to go. */
7256
7257 if (file->flags.just_syms)
7258 bfd_link_just_syms (file->the_bfd, s, &link_info);
7259 else if (lang_discard_section_p (s))
7260 s->output_section = bfd_abs_section_ptr;
7261 else if (strcmp (s->name, "COMMON") == 0)
7262 {
7263 /* This is a lonely common section which must have
7264 come from an archive. We attach to the section
7265 with the wildcard. */
7266 if (!bfd_link_relocatable (&link_info)
7267 || command_line.force_common_definition)
7268 {
7269 if (default_common_section == NULL)
7270 default_common_section
7271 = lang_output_section_statement_lookup (".bss", 0, 1);
7272 lang_add_section (&default_common_section->children, s,
7273 NULL, NULL, default_common_section);
7274 }
7275 }
7276 else
7277 ldlang_place_orphan (s);
7278 }
7279 }
7280 }
7281 }
7282
7283 void
7284 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
7285 {
7286 flagword *ptr_flags;
7287
7288 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
7289
7290 while (*flags)
7291 {
7292 switch (*flags)
7293 {
7294 /* PR 17900: An exclamation mark in the attributes reverses
7295 the sense of any of the attributes that follow. */
7296 case '!':
7297 invert = !invert;
7298 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
7299 break;
7300
7301 case 'A': case 'a':
7302 *ptr_flags |= SEC_ALLOC;
7303 break;
7304
7305 case 'R': case 'r':
7306 *ptr_flags |= SEC_READONLY;
7307 break;
7308
7309 case 'W': case 'w':
7310 *ptr_flags |= SEC_DATA;
7311 break;
7312
7313 case 'X': case 'x':
7314 *ptr_flags |= SEC_CODE;
7315 break;
7316
7317 case 'L': case 'l':
7318 case 'I': case 'i':
7319 *ptr_flags |= SEC_LOAD;
7320 break;
7321
7322 default:
7323 einfo (_("%F%P: invalid character %c (%d) in flags\n"),
7324 *flags, *flags);
7325 break;
7326 }
7327 flags++;
7328 }
7329 }
7330
7331 /* Call a function on each real input file. This function will be
7332 called on an archive, but not on the elements. */
7333
7334 void
7335 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
7336 {
7337 lang_input_statement_type *f;
7338
7339 for (f = (void *) input_file_chain.head;
7340 f != NULL;
7341 f = f->next_real_file)
7342 if (f->flags.real)
7343 func (f);
7344 }
7345
7346 /* Call a function on each real file. The function will be called on
7347 all the elements of an archive which are included in the link, but
7348 will not be called on the archive file itself. */
7349
7350 void
7351 lang_for_each_file (void (*func) (lang_input_statement_type *))
7352 {
7353 LANG_FOR_EACH_INPUT_STATEMENT (f)
7354 {
7355 if (f->flags.real)
7356 func (f);
7357 }
7358 }
7359
7360 void
7361 ldlang_add_file (lang_input_statement_type *entry)
7362 {
7363 lang_statement_append (&file_chain, entry, &entry->next);
7364
7365 /* The BFD linker needs to have a list of all input BFDs involved in
7366 a link. */
7367 ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next
7368 && entry->the_bfd->link.next == NULL);
7369 ASSERT (entry->the_bfd != link_info.output_bfd);
7370
7371 *link_info.input_bfds_tail = entry->the_bfd;
7372 link_info.input_bfds_tail = &entry->the_bfd->link.next;
7373 bfd_set_usrdata (entry->the_bfd, entry);
7374 bfd_set_gp_size (entry->the_bfd, g_switch_value);
7375
7376 /* Look through the sections and check for any which should not be
7377 included in the link. We need to do this now, so that we can
7378 notice when the backend linker tries to report multiple
7379 definition errors for symbols which are in sections we aren't
7380 going to link. FIXME: It might be better to entirely ignore
7381 symbols which are defined in sections which are going to be
7382 discarded. This would require modifying the backend linker for
7383 each backend which might set the SEC_LINK_ONCE flag. If we do
7384 this, we should probably handle SEC_EXCLUDE in the same way. */
7385
7386 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
7387 }
7388
7389 void
7390 lang_add_output (const char *name, int from_script)
7391 {
7392 /* Make -o on command line override OUTPUT in script. */
7393 if (!had_output_filename || !from_script)
7394 {
7395 output_filename = name;
7396 had_output_filename = true;
7397 }
7398 }
7399
7400 lang_output_section_statement_type *
7401 lang_enter_output_section_statement (const char *output_section_statement_name,
7402 etree_type *address_exp,
7403 enum section_type sectype,
7404 etree_type *sectype_value,
7405 etree_type *align,
7406 etree_type *subalign,
7407 etree_type *ebase,
7408 int constraint,
7409 int align_with_input)
7410 {
7411 lang_output_section_statement_type *os;
7412
7413 os = lang_output_section_statement_lookup (output_section_statement_name,
7414 constraint, 2);
7415 current_section = os;
7416
7417 if (os->addr_tree == NULL)
7418 {
7419 os->addr_tree = address_exp;
7420 }
7421 os->sectype = sectype;
7422 if (sectype == type_section || sectype == typed_readonly_section)
7423 os->sectype_value = sectype_value;
7424 else if (sectype == noload_section)
7425 os->flags = SEC_NEVER_LOAD;
7426 else
7427 os->flags = SEC_NO_FLAGS;
7428 os->block_value = 1;
7429
7430 /* Make next things chain into subchain of this. */
7431 push_stat_ptr (&os->children);
7432
7433 os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT;
7434 if (os->align_lma_with_input && align != NULL)
7435 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
7436 NULL);
7437
7438 os->subsection_alignment = subalign;
7439 os->section_alignment = align;
7440
7441 os->load_base = ebase;
7442 return os;
7443 }
7444
7445 void
7446 lang_final (void)
7447 {
7448 lang_output_statement_type *new_stmt;
7449
7450 new_stmt = new_stat (lang_output_statement, stat_ptr);
7451 new_stmt->name = output_filename;
7452 }
7453
7454 /* Reset the current counters in the regions. */
7455
7456 void
7457 lang_reset_memory_regions (void)
7458 {
7459 lang_memory_region_type *p = lang_memory_region_list;
7460 asection *o;
7461 lang_output_section_statement_type *os;
7462
7463 for (p = lang_memory_region_list; p != NULL; p = p->next)
7464 {
7465 p->current = p->origin;
7466 p->last_os = NULL;
7467 }
7468
7469 for (os = (void *) lang_os_list.head;
7470 os != NULL;
7471 os = os->next)
7472 {
7473 os->processed_vma = false;
7474 os->processed_lma = false;
7475 }
7476
7477 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
7478 {
7479 /* Save the last size for possible use by bfd_relax_section. */
7480 o->rawsize = o->size;
7481 if (!(o->flags & SEC_FIXED_SIZE))
7482 o->size = 0;
7483 }
7484 }
7485
7486 /* Worker for lang_gc_sections_1. */
7487
7488 static void
7489 gc_section_callback (lang_wild_statement_type *ptr,
7490 struct wildcard_list *sec ATTRIBUTE_UNUSED,
7491 asection *section,
7492 lang_input_statement_type *file ATTRIBUTE_UNUSED,
7493 void *data ATTRIBUTE_UNUSED)
7494 {
7495 /* If the wild pattern was marked KEEP, the member sections
7496 should be as well. */
7497 if (ptr->keep_sections)
7498 section->flags |= SEC_KEEP;
7499 }
7500
7501 /* Iterate over sections marking them against GC. */
7502
7503 static void
7504 lang_gc_sections_1 (lang_statement_union_type *s)
7505 {
7506 for (; s != NULL; s = s->header.next)
7507 {
7508 switch (s->header.type)
7509 {
7510 case lang_wild_statement_enum:
7511 walk_wild (&s->wild_statement, gc_section_callback, NULL);
7512 break;
7513 case lang_constructors_statement_enum:
7514 lang_gc_sections_1 (constructor_list.head);
7515 break;
7516 case lang_output_section_statement_enum:
7517 lang_gc_sections_1 (s->output_section_statement.children.head);
7518 break;
7519 case lang_group_statement_enum:
7520 lang_gc_sections_1 (s->group_statement.children.head);
7521 break;
7522 default:
7523 break;
7524 }
7525 }
7526 }
7527
7528 static void
7529 lang_gc_sections (void)
7530 {
7531 /* Keep all sections so marked in the link script. */
7532 lang_gc_sections_1 (statement_list.head);
7533
7534 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
7535 the special case of .stabstr debug info. (See bfd/stabs.c)
7536 Twiddle the flag here, to simplify later linker code. */
7537 if (bfd_link_relocatable (&link_info))
7538 {
7539 LANG_FOR_EACH_INPUT_STATEMENT (f)
7540 {
7541 asection *sec;
7542 #if BFD_SUPPORTS_PLUGINS
7543 if (f->flags.claimed)
7544 continue;
7545 #endif
7546 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
7547 if ((sec->flags & SEC_DEBUGGING) == 0
7548 || strcmp (sec->name, ".stabstr") != 0)
7549 sec->flags &= ~SEC_EXCLUDE;
7550 }
7551 }
7552
7553 if (link_info.gc_sections)
7554 bfd_gc_sections (link_info.output_bfd, &link_info);
7555 }
7556
7557 /* Worker for lang_find_relro_sections_1. */
7558
7559 static void
7560 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
7561 struct wildcard_list *sec ATTRIBUTE_UNUSED,
7562 asection *section,
7563 lang_input_statement_type *file ATTRIBUTE_UNUSED,
7564 void *data)
7565 {
7566 /* Discarded, excluded and ignored sections effectively have zero
7567 size. */
7568 if (section->output_section != NULL
7569 && section->output_section->owner == link_info.output_bfd
7570 && (section->output_section->flags & SEC_EXCLUDE) == 0
7571 && !IGNORE_SECTION (section)
7572 && section->size != 0)
7573 {
7574 bool *has_relro_section = (bool *) data;
7575 *has_relro_section = true;
7576 }
7577 }
7578
7579 /* Iterate over sections for relro sections. */
7580
7581 static void
7582 lang_find_relro_sections_1 (lang_statement_union_type *s,
7583 bool *has_relro_section)
7584 {
7585 if (*has_relro_section)
7586 return;
7587
7588 for (; s != NULL; s = s->header.next)
7589 {
7590 if (s == expld.dataseg.relro_end_stat)
7591 break;
7592
7593 switch (s->header.type)
7594 {
7595 case lang_wild_statement_enum:
7596 walk_wild (&s->wild_statement,
7597 find_relro_section_callback,
7598 has_relro_section);
7599 break;
7600 case lang_constructors_statement_enum:
7601 lang_find_relro_sections_1 (constructor_list.head,
7602 has_relro_section);
7603 break;
7604 case lang_output_section_statement_enum:
7605 lang_find_relro_sections_1 (s->output_section_statement.children.head,
7606 has_relro_section);
7607 break;
7608 case lang_group_statement_enum:
7609 lang_find_relro_sections_1 (s->group_statement.children.head,
7610 has_relro_section);
7611 break;
7612 default:
7613 break;
7614 }
7615 }
7616 }
7617
7618 static void
7619 lang_find_relro_sections (void)
7620 {
7621 bool has_relro_section = false;
7622
7623 /* Check all sections in the link script. */
7624
7625 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
7626 &has_relro_section);
7627
7628 if (!has_relro_section)
7629 link_info.relro = false;
7630 }
7631
7632 /* Relax all sections until bfd_relax_section gives up. */
7633
7634 void
7635 lang_relax_sections (bool need_layout)
7636 {
7637 /* NB: Also enable relaxation to layout sections for DT_RELR. */
7638 if (RELAXATION_ENABLED || link_info.enable_dt_relr)
7639 {
7640 /* We may need more than one relaxation pass. */
7641 int i = link_info.relax_pass;
7642
7643 /* The backend can use it to determine the current pass. */
7644 link_info.relax_pass = 0;
7645
7646 while (i--)
7647 {
7648 /* Keep relaxing until bfd_relax_section gives up. */
7649 bool relax_again;
7650
7651 link_info.relax_trip = -1;
7652 do
7653 {
7654 link_info.relax_trip++;
7655
7656 /* Note: pe-dll.c does something like this also. If you find
7657 you need to change this code, you probably need to change
7658 pe-dll.c also. DJ */
7659
7660 /* Do all the assignments with our current guesses as to
7661 section sizes. */
7662 lang_do_assignments (lang_assigning_phase_enum);
7663
7664 /* We must do this after lang_do_assignments, because it uses
7665 size. */
7666 lang_reset_memory_regions ();
7667
7668 /* Perform another relax pass - this time we know where the
7669 globals are, so can make a better guess. */
7670 relax_again = false;
7671 lang_size_sections (&relax_again, false);
7672 }
7673 while (relax_again);
7674
7675 link_info.relax_pass++;
7676 }
7677 need_layout = true;
7678 }
7679
7680 if (need_layout)
7681 {
7682 /* Final extra sizing to report errors. */
7683 lang_do_assignments (lang_assigning_phase_enum);
7684 lang_reset_memory_regions ();
7685 lang_size_sections (NULL, true);
7686 }
7687 }
7688
7689 #if BFD_SUPPORTS_PLUGINS
7690 /* Find the insert point for the plugin's replacement files. We
7691 place them after the first claimed real object file, or if the
7692 first claimed object is an archive member, after the last real
7693 object file immediately preceding the archive. In the event
7694 no objects have been claimed at all, we return the first dummy
7695 object file on the list as the insert point; that works, but
7696 the callee must be careful when relinking the file_chain as it
7697 is not actually on that chain, only the statement_list and the
7698 input_file list; in that case, the replacement files must be
7699 inserted at the head of the file_chain. */
7700
7701 static lang_input_statement_type *
7702 find_replacements_insert_point (bool *before)
7703 {
7704 lang_input_statement_type *claim1, *lastobject;
7705 lastobject = (void *) input_file_chain.head;
7706 for (claim1 = (void *) file_chain.head;
7707 claim1 != NULL;
7708 claim1 = claim1->next)
7709 {
7710 if (claim1->flags.claimed)
7711 {
7712 *before = claim1->flags.claim_archive;
7713 return claim1->flags.claim_archive ? lastobject : claim1;
7714 }
7715 /* Update lastobject if this is a real object file. */
7716 if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL)
7717 lastobject = claim1;
7718 }
7719 /* No files were claimed by the plugin. Choose the last object
7720 file found on the list (maybe the first, dummy entry) as the
7721 insert point. */
7722 *before = false;
7723 return lastobject;
7724 }
7725
7726 /* Find where to insert ADD, an archive element or shared library
7727 added during a rescan. */
7728
7729 static lang_input_statement_type **
7730 find_rescan_insertion (lang_input_statement_type *add)
7731 {
7732 bfd *add_bfd = add->the_bfd;
7733 lang_input_statement_type *f;
7734 lang_input_statement_type *last_loaded = NULL;
7735 lang_input_statement_type *before = NULL;
7736 lang_input_statement_type **iter = NULL;
7737
7738 if (add_bfd->my_archive != NULL)
7739 add_bfd = add_bfd->my_archive;
7740
7741 /* First look through the input file chain, to find an object file
7742 before the one we've rescanned. Normal object files always
7743 appear on both the input file chain and the file chain, so this
7744 lets us get quickly to somewhere near the correct place on the
7745 file chain if it is full of archive elements. Archives don't
7746 appear on the file chain, but if an element has been extracted
7747 then their input_statement->next points at it. */
7748 for (f = (void *) input_file_chain.head;
7749 f != NULL;
7750 f = f->next_real_file)
7751 {
7752 if (f->the_bfd == add_bfd)
7753 {
7754 before = last_loaded;
7755 if (f->next != NULL)
7756 return &f->next->next;
7757 }
7758 if (f->the_bfd != NULL && f->next != NULL)
7759 last_loaded = f;
7760 }
7761
7762 for (iter = before ? &before->next : &file_chain.head->input_statement.next;
7763 *iter != NULL;
7764 iter = &(*iter)->next)
7765 if (!(*iter)->flags.claim_archive
7766 && (*iter)->the_bfd->my_archive == NULL)
7767 break;
7768
7769 return iter;
7770 }
7771
7772 /* Insert SRCLIST into DESTLIST after given element by chaining
7773 on FIELD as the next-pointer. (Counterintuitively does not need
7774 a pointer to the actual after-node itself, just its chain field.) */
7775
7776 static void
7777 lang_list_insert_after (lang_statement_list_type *destlist,
7778 lang_statement_list_type *srclist,
7779 lang_statement_union_type **field)
7780 {
7781 *(srclist->tail) = *field;
7782 *field = srclist->head;
7783 if (destlist->tail == field)
7784 destlist->tail = srclist->tail;
7785 }
7786
7787 /* Detach new nodes added to DESTLIST since the time ORIGLIST
7788 was taken as a copy of it and leave them in ORIGLIST. */
7789
7790 static void
7791 lang_list_remove_tail (lang_statement_list_type *destlist,
7792 lang_statement_list_type *origlist)
7793 {
7794 union lang_statement_union **savetail;
7795 /* Check that ORIGLIST really is an earlier state of DESTLIST. */
7796 ASSERT (origlist->head == destlist->head);
7797 savetail = origlist->tail;
7798 origlist->head = *(savetail);
7799 origlist->tail = destlist->tail;
7800 destlist->tail = savetail;
7801 *savetail = NULL;
7802 }
7803
7804 static lang_statement_union_type **
7805 find_next_input_statement (lang_statement_union_type **s)
7806 {
7807 for ( ; *s; s = &(*s)->header.next)
7808 {
7809 lang_statement_union_type **t;
7810 switch ((*s)->header.type)
7811 {
7812 case lang_input_statement_enum:
7813 return s;
7814 case lang_wild_statement_enum:
7815 t = &(*s)->wild_statement.children.head;
7816 break;
7817 case lang_group_statement_enum:
7818 t = &(*s)->group_statement.children.head;
7819 break;
7820 case lang_output_section_statement_enum:
7821 t = &(*s)->output_section_statement.children.head;
7822 break;
7823 default:
7824 continue;
7825 }
7826 t = find_next_input_statement (t);
7827 if (*t)
7828 return t;
7829 }
7830 return s;
7831 }
7832 #endif /* BFD_SUPPORTS_PLUGINS */
7833
7834 /* Add NAME to the list of garbage collection entry points. */
7835
7836 void
7837 lang_add_gc_name (const char *name)
7838 {
7839 struct bfd_sym_chain *sym;
7840
7841 if (name == NULL)
7842 return;
7843
7844 sym = stat_alloc (sizeof (*sym));
7845
7846 sym->next = link_info.gc_sym_list;
7847 sym->name = name;
7848 link_info.gc_sym_list = sym;
7849 }
7850
7851 /* Check relocations. */
7852
7853 static void
7854 lang_check_relocs (void)
7855 {
7856 if (link_info.check_relocs_after_open_input)
7857 {
7858 bfd *abfd;
7859
7860 for (abfd = link_info.input_bfds;
7861 abfd != (bfd *) NULL; abfd = abfd->link.next)
7862 if (!bfd_link_check_relocs (abfd, &link_info))
7863 {
7864 /* No object output, fail return. */
7865 config.make_executable = false;
7866 /* Note: we do not abort the loop, but rather
7867 continue the scan in case there are other
7868 bad relocations to report. */
7869 }
7870 }
7871 }
7872
7873 /* Look through all output sections looking for places where we can
7874 propagate forward the lma region. */
7875
7876 static void
7877 lang_propagate_lma_regions (void)
7878 {
7879 lang_output_section_statement_type *os;
7880
7881 for (os = (void *) lang_os_list.head;
7882 os != NULL;
7883 os = os->next)
7884 {
7885 if (os->prev != NULL
7886 && os->lma_region == NULL
7887 && os->load_base == NULL
7888 && os->addr_tree == NULL
7889 && os->region == os->prev->region)
7890 os->lma_region = os->prev->lma_region;
7891 }
7892 }
7893
7894 static void
7895 warn_non_contiguous_discards (void)
7896 {
7897 LANG_FOR_EACH_INPUT_STATEMENT (file)
7898 {
7899 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
7900 || file->flags.just_syms)
7901 continue;
7902
7903 for (asection *s = file->the_bfd->sections; s != NULL; s = s->next)
7904 if (s->output_section == NULL
7905 && (s->flags & SEC_LINKER_CREATED) == 0)
7906 einfo (_("%P: warning: --enable-non-contiguous-regions "
7907 "discards section `%pA' from `%pB'\n"),
7908 s, file->the_bfd);
7909 }
7910 }
7911
7912 static void
7913 reset_one_wild (lang_statement_union_type *statement)
7914 {
7915 if (statement->header.type == lang_wild_statement_enum)
7916 {
7917 lang_wild_statement_type *stmt = &statement->wild_statement;
7918 lang_list_init (&stmt->matching_sections);
7919 }
7920 }
7921
7922 static void
7923 reset_resolved_wilds (void)
7924 {
7925 lang_for_each_statement (reset_one_wild);
7926 }
7927
7928 void
7929 lang_process (void)
7930 {
7931 /* Finalize dynamic list. */
7932 if (link_info.dynamic_list)
7933 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
7934
7935 current_target = default_target;
7936
7937 /* Open the output file. */
7938 lang_for_each_statement (ldlang_open_output);
7939 init_opb (NULL);
7940
7941 ldemul_create_output_section_statements ();
7942
7943 /* Add to the hash table all undefineds on the command line. */
7944 lang_place_undefineds ();
7945
7946 if (!bfd_section_already_linked_table_init ())
7947 einfo (_("%F%P: can not create hash table: %E\n"));
7948
7949 /* A first pass through the memory regions ensures that if any region
7950 references a symbol for its origin or length then this symbol will be
7951 added to the symbol table. Having these symbols in the symbol table
7952 means that when we call open_input_bfds PROVIDE statements will
7953 trigger to provide any needed symbols. The regions origins and
7954 lengths are not assigned as a result of this call. */
7955 lang_do_memory_regions (false);
7956
7957 /* Create a bfd for each input file. */
7958 current_target = default_target;
7959 lang_statement_iteration++;
7960 open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
7961
7962 /* Now that open_input_bfds has processed assignments and provide
7963 statements we can give values to symbolic origin/length now. */
7964 lang_do_memory_regions (true);
7965
7966 #if BFD_SUPPORTS_PLUGINS
7967 if (link_info.lto_plugin_active)
7968 {
7969 lang_statement_list_type added;
7970 lang_statement_list_type files, inputfiles;
7971
7972 ldemul_before_plugin_all_symbols_read ();
7973
7974 /* Now all files are read, let the plugin(s) decide if there
7975 are any more to be added to the link before we call the
7976 emulation's after_open hook. We create a private list of
7977 input statements for this purpose, which we will eventually
7978 insert into the global statement list after the first claimed
7979 file. */
7980 added = *stat_ptr;
7981 /* We need to manipulate all three chains in synchrony. */
7982 files = file_chain;
7983 inputfiles = input_file_chain;
7984 if (plugin_call_all_symbols_read ())
7985 einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
7986 plugin_error_plugin ());
7987 link_info.lto_all_symbols_read = true;
7988 /* Open any newly added files, updating the file chains. */
7989 plugin_undefs = link_info.hash->undefs_tail;
7990 open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
7991 if (plugin_undefs == link_info.hash->undefs_tail)
7992 plugin_undefs = NULL;
7993 /* Restore the global list pointer now they have all been added. */
7994 lang_list_remove_tail (stat_ptr, &added);
7995 /* And detach the fresh ends of the file lists. */
7996 lang_list_remove_tail (&file_chain, &files);
7997 lang_list_remove_tail (&input_file_chain, &inputfiles);
7998 /* Were any new files added? */
7999 if (added.head != NULL)
8000 {
8001 /* If so, we will insert them into the statement list immediately
8002 after the first input file that was claimed by the plugin,
8003 unless that file was an archive in which case it is inserted
8004 immediately before. */
8005 bool before;
8006 lang_statement_union_type **prev;
8007 plugin_insert = find_replacements_insert_point (&before);
8008 /* If a plugin adds input files without having claimed any, we
8009 don't really have a good idea where to place them. Just putting
8010 them at the start or end of the list is liable to leave them
8011 outside the crtbegin...crtend range. */
8012 ASSERT (plugin_insert != NULL);
8013 /* Splice the new statement list into the old one. */
8014 prev = &plugin_insert->header.next;
8015 if (before)
8016 {
8017 prev = find_next_input_statement (prev);
8018 if (*prev != (void *) plugin_insert->next_real_file)
8019 {
8020 /* We didn't find the expected input statement.
8021 Fall back to adding after plugin_insert. */
8022 prev = &plugin_insert->header.next;
8023 }
8024 }
8025 lang_list_insert_after (stat_ptr, &added, prev);
8026 /* Likewise for the file chains. */
8027 lang_list_insert_after (&input_file_chain, &inputfiles,
8028 (void *) &plugin_insert->next_real_file);
8029 /* We must be careful when relinking file_chain; we may need to
8030 insert the new files at the head of the list if the insert
8031 point chosen is the dummy first input file. */
8032 if (plugin_insert->filename)
8033 lang_list_insert_after (&file_chain, &files,
8034 (void *) &plugin_insert->next);
8035 else
8036 lang_list_insert_after (&file_chain, &files, &file_chain.head);
8037
8038 /* Rescan archives in case new undefined symbols have appeared. */
8039 files = file_chain;
8040 lang_statement_iteration++;
8041 open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
8042 lang_list_remove_tail (&file_chain, &files);
8043 while (files.head != NULL)
8044 {
8045 lang_input_statement_type **insert;
8046 lang_input_statement_type **iter, *temp;
8047 bfd *my_arch;
8048
8049 insert = find_rescan_insertion (&files.head->input_statement);
8050 /* All elements from an archive can be added at once. */
8051 iter = &files.head->input_statement.next;
8052 my_arch = files.head->input_statement.the_bfd->my_archive;
8053 if (my_arch != NULL)
8054 for (; *iter != NULL; iter = &(*iter)->next)
8055 if ((*iter)->the_bfd->my_archive != my_arch)
8056 break;
8057 temp = *insert;
8058 *insert = &files.head->input_statement;
8059 files.head = (lang_statement_union_type *) *iter;
8060 *iter = temp;
8061 if (file_chain.tail == (lang_statement_union_type **) insert)
8062 file_chain.tail = (lang_statement_union_type **) iter;
8063 if (my_arch != NULL)
8064 {
8065 lang_input_statement_type *parent = bfd_usrdata (my_arch);
8066 if (parent != NULL)
8067 parent->next = (lang_input_statement_type *)
8068 ((char *) iter
8069 - offsetof (lang_input_statement_type, next));
8070 }
8071 }
8072 }
8073 }
8074 #endif /* BFD_SUPPORTS_PLUGINS */
8075
8076 /* Make sure that nobody has tried to add a symbol to this list
8077 before now. */
8078 ASSERT (link_info.gc_sym_list == NULL);
8079
8080 link_info.gc_sym_list = &entry_symbol;
8081
8082 if (entry_symbol.name == NULL)
8083 {
8084 link_info.gc_sym_list = ldlang_undef_chain_list_head;
8085
8086 /* entry_symbol is normally initialied by a ENTRY definition in the
8087 linker script or the -e command line option. But if neither of
8088 these have been used, the target specific backend may still have
8089 provided an entry symbol via a call to lang_default_entry().
8090 Unfortunately this value will not be processed until lang_end()
8091 is called, long after this function has finished. So detect this
8092 case here and add the target's entry symbol to the list of starting
8093 points for garbage collection resolution. */
8094 lang_add_gc_name (entry_symbol_default);
8095 }
8096
8097 lang_add_gc_name (link_info.init_function);
8098 lang_add_gc_name (link_info.fini_function);
8099
8100 ldemul_after_open ();
8101 if (config.map_file != NULL)
8102 lang_print_asneeded ();
8103
8104 ldlang_open_ctf ();
8105
8106 bfd_section_already_linked_table_free ();
8107
8108 /* Make sure that we're not mixing architectures. We call this
8109 after all the input files have been opened, but before we do any
8110 other processing, so that any operations merge_private_bfd_data
8111 does on the output file will be known during the rest of the
8112 link. */
8113 lang_check ();
8114
8115 /* Handle .exports instead of a version script if we're told to do so. */
8116 if (command_line.version_exports_section)
8117 lang_do_version_exports_section ();
8118
8119 /* Build all sets based on the information gathered from the input
8120 files. */
8121 ldctor_build_sets ();
8122
8123 lang_symbol_tweaks ();
8124
8125 /* PR 13683: We must rerun the assignments prior to running garbage
8126 collection in order to make sure that all symbol aliases are resolved. */
8127 lang_do_assignments (lang_mark_phase_enum);
8128 expld.phase = lang_first_phase_enum;
8129
8130 /* Size up the common data. */
8131 lang_common ();
8132
8133 if (0)
8134 debug_prefix_tree ();
8135
8136 resolve_wilds ();
8137
8138 /* Remove unreferenced sections if asked to. */
8139 lang_gc_sections ();
8140
8141 lang_mark_undefineds ();
8142
8143 /* Check relocations. */
8144 lang_check_relocs ();
8145
8146 ldemul_after_check_relocs ();
8147
8148 /* There might have been new sections created (e.g. as result of
8149 checking relocs to need a .got, or suchlike), so to properly order
8150 them into our lists of matching sections reset them here. */
8151 reset_resolved_wilds ();
8152 resolve_wilds ();
8153
8154 /* Update wild statements in case the user gave --sort-section.
8155 Note how the option might have come after the linker script and
8156 so couldn't have been set when the wild statements were created. */
8157 update_wild_statements (statement_list.head);
8158
8159 /* Run through the contours of the script and attach input sections
8160 to the correct output sections. */
8161 lang_statement_iteration++;
8162 map_input_to_output_sections (statement_list.head, NULL, NULL);
8163
8164 /* Start at the statement immediately after the special abs_section
8165 output statement, so that it isn't reordered. */
8166 process_insert_statements (&lang_os_list.head->header.next);
8167
8168 ldemul_before_place_orphans ();
8169
8170 /* Find any sections not attached explicitly and handle them. */
8171 lang_place_orphans ();
8172
8173 if (!bfd_link_relocatable (&link_info))
8174 {
8175 asection *found;
8176
8177 /* Merge SEC_MERGE sections. This has to be done after GC of
8178 sections, so that GCed sections are not merged, but before
8179 assigning dynamic symbols, since removing whole input sections
8180 is hard then. */
8181 bfd_merge_sections (link_info.output_bfd, &link_info);
8182
8183 /* Look for a text section and set the readonly attribute in it. */
8184 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
8185
8186 if (found != NULL)
8187 {
8188 if (config.text_read_only)
8189 found->flags |= SEC_READONLY;
8190 else
8191 found->flags &= ~SEC_READONLY;
8192 }
8193 }
8194
8195 /* Merge together CTF sections. After this, only the symtab-dependent
8196 function and data object sections need adjustment. */
8197 lang_merge_ctf ();
8198
8199 /* Emit the CTF, iff the emulation doesn't need to do late emission after
8200 examining things laid out late, like the strtab. */
8201 lang_write_ctf (0);
8202
8203 /* Copy forward lma regions for output sections in same lma region. */
8204 lang_propagate_lma_regions ();
8205
8206 /* Defining __start/__stop symbols early for --gc-sections to work
8207 around a glibc build problem can result in these symbols being
8208 defined when they should not be. Fix them now. */
8209 if (config.build_constructors)
8210 lang_undef_start_stop ();
8211
8212 /* Define .startof./.sizeof. symbols with preliminary values before
8213 dynamic symbols are created. */
8214 if (!bfd_link_relocatable (&link_info))
8215 lang_init_startof_sizeof ();
8216
8217 /* Do anything special before sizing sections. This is where ELF
8218 and other back-ends size dynamic sections. */
8219 ldemul_before_allocation ();
8220
8221 /* We must record the program headers before we try to fix the
8222 section positions, since they will affect SIZEOF_HEADERS. */
8223 lang_record_phdrs ();
8224
8225 /* Check relro sections. */
8226 if (link_info.relro && !bfd_link_relocatable (&link_info))
8227 lang_find_relro_sections ();
8228
8229 /* Size up the sections. */
8230 lang_size_sections (NULL, !RELAXATION_ENABLED);
8231
8232 /* See if anything special should be done now we know how big
8233 everything is. This is where relaxation is done. */
8234 ldemul_after_allocation ();
8235
8236 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
8237 lang_finalize_start_stop ();
8238
8239 /* Do all the assignments again, to report errors. Assignment
8240 statements are processed multiple times, updating symbols; In
8241 open_input_bfds, lang_do_assignments, and lang_size_sections.
8242 Since lang_relax_sections calls lang_do_assignments, symbols are
8243 also updated in ldemul_after_allocation. */
8244 lang_do_assignments (lang_final_phase_enum);
8245
8246 ldemul_finish ();
8247
8248 /* Convert absolute symbols to section relative. */
8249 ldexp_finalize_syms ();
8250
8251 /* Make sure that the section addresses make sense. */
8252 if (command_line.check_section_addresses)
8253 lang_check_section_addresses ();
8254
8255 if (link_info.non_contiguous_regions
8256 && link_info.non_contiguous_regions_warnings)
8257 warn_non_contiguous_discards ();
8258
8259 /* Check any required symbols are known. */
8260 ldlang_check_require_defined_symbols ();
8261
8262 lang_end ();
8263 }
8264
8265 /* EXPORTED TO YACC */
8266
8267 void
8268 lang_add_wild (struct wildcard_spec *filespec,
8269 struct wildcard_list *section_list,
8270 bool keep_sections)
8271 {
8272 struct wildcard_list *curr, *next;
8273 lang_wild_statement_type *new_stmt;
8274 bool any_specs_sorted = false;
8275
8276 /* Reverse the list as the parser puts it back to front. */
8277 for (curr = section_list, section_list = NULL;
8278 curr != NULL;
8279 section_list = curr, curr = next)
8280 {
8281 if (curr->spec.sorted != none && curr->spec.sorted != by_none)
8282 any_specs_sorted = true;
8283 next = curr->next;
8284 curr->next = section_list;
8285 }
8286
8287 if (filespec != NULL && filespec->name != NULL)
8288 {
8289 if (strcmp (filespec->name, "*") == 0)
8290 filespec->name = NULL;
8291 else if (!wildcardp (filespec->name))
8292 lang_has_input_file = true;
8293 }
8294
8295 new_stmt = new_stat (lang_wild_statement, stat_ptr);
8296 new_stmt->filename = NULL;
8297 new_stmt->filenames_sorted = false;
8298 new_stmt->any_specs_sorted = any_specs_sorted;
8299 new_stmt->section_flag_list = NULL;
8300 new_stmt->exclude_name_list = NULL;
8301 if (filespec != NULL)
8302 {
8303 new_stmt->filename = filespec->name;
8304 new_stmt->filenames_sorted = filespec->sorted == by_name;
8305 new_stmt->section_flag_list = filespec->section_flag_list;
8306 new_stmt->exclude_name_list = filespec->exclude_name_list;
8307 }
8308 new_stmt->section_list = section_list;
8309 new_stmt->keep_sections = keep_sections;
8310 lang_list_init (&new_stmt->children);
8311 lang_list_init (&new_stmt->matching_sections);
8312 analyze_walk_wild_section_handler (new_stmt);
8313 if (0)
8314 {
8315 printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*");
8316 for (curr = new_stmt->section_list; curr; curr = curr->next)
8317 printf ("%s ", curr->spec.name ? curr->spec.name : "*");
8318 printf (")\n");
8319 }
8320 }
8321
8322 void
8323 lang_section_start (const char *name, etree_type *address,
8324 const segment_type *segment)
8325 {
8326 lang_address_statement_type *ad;
8327
8328 ad = new_stat (lang_address_statement, stat_ptr);
8329 ad->section_name = name;
8330 ad->address = address;
8331 ad->segment = segment;
8332 }
8333
8334 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
8335 because of a -e argument on the command line, or zero if this is
8336 called by ENTRY in a linker script. Command line arguments take
8337 precedence. */
8338
8339 void
8340 lang_add_entry (const char *name, bool cmdline)
8341 {
8342 if (entry_symbol.name == NULL
8343 || cmdline
8344 || !entry_from_cmdline)
8345 {
8346 entry_symbol.name = name;
8347 entry_from_cmdline = cmdline;
8348 }
8349 }
8350
8351 /* Set the default start symbol to NAME. .em files should use this,
8352 not lang_add_entry, to override the use of "start" if neither the
8353 linker script nor the command line specifies an entry point. NAME
8354 must be permanently allocated. */
8355 void
8356 lang_default_entry (const char *name)
8357 {
8358 entry_symbol_default = name;
8359 }
8360
8361 void
8362 lang_add_target (const char *name)
8363 {
8364 lang_target_statement_type *new_stmt;
8365
8366 new_stmt = new_stat (lang_target_statement, stat_ptr);
8367 new_stmt->target = name;
8368 }
8369
8370 void
8371 lang_add_map (const char *name)
8372 {
8373 while (*name)
8374 {
8375 switch (*name)
8376 {
8377 case 'F':
8378 map_option_f = true;
8379 break;
8380 }
8381 name++;
8382 }
8383 }
8384
8385 void
8386 lang_add_fill (fill_type *fill)
8387 {
8388 lang_fill_statement_type *new_stmt;
8389
8390 new_stmt = new_stat (lang_fill_statement, stat_ptr);
8391 new_stmt->fill = fill;
8392 }
8393
8394 void
8395 lang_add_data (int type, union etree_union *exp)
8396 {
8397 lang_data_statement_type *new_stmt;
8398
8399 new_stmt = new_stat (lang_data_statement, stat_ptr);
8400 new_stmt->exp = exp;
8401 new_stmt->type = type;
8402 }
8403
8404 /* Convert escape codes in S.
8405 Supports \n, \r, \t and \NNN octals.
8406 Returns a copy of S in a malloc'ed buffer. */
8407
8408 static char *
8409 convert_string (const char * s)
8410 {
8411 size_t len = strlen (s);
8412 size_t i;
8413 bool escape = false;
8414 char * buffer = malloc (len + 1);
8415 char * b;
8416
8417 for (i = 0, b = buffer; i < len; i++)
8418 {
8419 char c = *s++;
8420
8421 if (escape)
8422 {
8423 switch (c)
8424 {
8425 default:
8426 /* Ignore the escape. */
8427 break;
8428
8429 case 'n': c = '\n'; break;
8430 case 'r': c = '\r'; break;
8431 case 't': c = '\t'; break;
8432
8433 case '0':
8434 case '1':
8435 case '2':
8436 case '3':
8437 case '4':
8438 case '5':
8439 case '6':
8440 case '7':
8441 /* We have an octal number. */
8442 {
8443 unsigned int value = c - '0';
8444
8445 c = *s;
8446 if ((c >= '0') && (c <= '7'))
8447 {
8448 value <<= 3;
8449 value += (c - '0');
8450 i++;
8451 s++;
8452
8453 c = *s;
8454 if ((c >= '0') && (c <= '7'))
8455 {
8456 value <<= 3;
8457 value += (c - '0');
8458 i++;
8459 s++;
8460 }
8461 }
8462
8463 if (value > 0xff)
8464 {
8465 /* octal: \777 is treated as '\077' + '7' */
8466 value >>= 3;
8467 i--;
8468 s--;
8469 }
8470
8471 c = value;
8472 }
8473 break;
8474 }
8475 escape = false;
8476 }
8477 else
8478 {
8479 if (c == '\\')
8480 {
8481 escape = true;
8482 continue;
8483 }
8484 }
8485
8486 * b ++ = c;
8487 }
8488
8489 * b = 0;
8490 return buffer;
8491 }
8492
8493 void
8494 lang_add_string (size_t size, const char *s)
8495 {
8496 size_t len;
8497 size_t i;
8498 char * string;
8499
8500 string = convert_string (s);
8501 len = strlen (string);
8502
8503 /* Check if it is ASCIZ command (len == 0) */
8504 if (size == 0)
8505 /* Make sure that we include the terminating nul byte. */
8506 size = len + 1;
8507 else if (len >= size)
8508 {
8509 len = size - 1;
8510
8511 einfo (_("%P:%pS: warning: ASCII string does not fit in allocated space,"
8512 " truncated\n"), NULL);
8513 }
8514
8515 for (i = 0 ; i < len ; i++)
8516 lang_add_data (BYTE, exp_intop (string[i]));
8517
8518 while (i++ < size)
8519 lang_add_data (BYTE, exp_intop ('\0'));
8520
8521 free (string);
8522 }
8523
8524 /* Store the time of linking in the image */
8525 void
8526 lang_add_timestamp (void)
8527 {
8528 lang_add_data (QUAD, exp_intop ((bfd_vma) time (0)));
8529 }
8530
8531 /* Create a new reloc statement. RELOC is the BFD relocation type to
8532 generate. HOWTO is the corresponding howto structure (we could
8533 look this up, but the caller has already done so). SECTION is the
8534 section to generate a reloc against, or NAME is the name of the
8535 symbol to generate a reloc against. Exactly one of SECTION and
8536 NAME must be NULL. ADDEND is an expression for the addend. */
8537
8538 void
8539 lang_add_reloc (bfd_reloc_code_real_type reloc,
8540 reloc_howto_type *howto,
8541 asection *section,
8542 const char *name,
8543 union etree_union *addend)
8544 {
8545 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
8546
8547 p->reloc = reloc;
8548 p->howto = howto;
8549 p->section = section;
8550 p->name = name;
8551 p->addend_exp = addend;
8552
8553 p->addend_value = 0;
8554 p->output_section = NULL;
8555 p->output_offset = 0;
8556 }
8557
8558 lang_assignment_statement_type *
8559 lang_add_assignment (etree_type *exp)
8560 {
8561 lang_assignment_statement_type *new_stmt;
8562
8563 new_stmt = new_stat (lang_assignment_statement, stat_ptr);
8564 new_stmt->exp = exp;
8565 return new_stmt;
8566 }
8567
8568 void
8569 lang_add_attribute (enum statement_enum attribute)
8570 {
8571 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
8572 }
8573
8574 void
8575 lang_startup (const char *name)
8576 {
8577 if (first_file->filename != NULL)
8578 {
8579 einfo (_("%F%P: multiple STARTUP files\n"));
8580 }
8581 first_file->filename = name;
8582 first_file->local_sym_name = name;
8583 first_file->flags.real = true;
8584 }
8585
8586 void
8587 lang_float (bool maybe)
8588 {
8589 lang_float_flag = maybe;
8590 }
8591
8592
8593 /* Work out the load- and run-time regions from a script statement, and
8594 store them in *LMA_REGION and *REGION respectively.
8595
8596 MEMSPEC is the name of the run-time region, or the value of
8597 DEFAULT_MEMORY_REGION if the statement didn't specify one.
8598 LMA_MEMSPEC is the name of the load-time region, or null if the
8599 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
8600 had an explicit load address.
8601
8602 It is an error to specify both a load region and a load address. */
8603
8604 static void
8605 lang_get_regions (lang_memory_region_type **region,
8606 lang_memory_region_type **lma_region,
8607 const char *memspec,
8608 const char *lma_memspec,
8609 bool have_lma,
8610 bool have_vma)
8611 {
8612 *lma_region = lang_memory_region_lookup (lma_memspec, false);
8613
8614 /* If no runtime region or VMA has been specified, but the load region
8615 has been specified, then use the load region for the runtime region
8616 as well. */
8617 if (lma_memspec != NULL
8618 && !have_vma
8619 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
8620 *region = *lma_region;
8621 else
8622 *region = lang_memory_region_lookup (memspec, false);
8623
8624 if (have_lma && lma_memspec != 0)
8625 einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
8626 NULL);
8627 }
8628
8629 void
8630 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
8631 lang_output_section_phdr_list *phdrs,
8632 const char *lma_memspec)
8633 {
8634 lang_get_regions (&current_section->region,
8635 &current_section->lma_region,
8636 memspec, lma_memspec,
8637 current_section->load_base != NULL,
8638 current_section->addr_tree != NULL);
8639
8640 current_section->fill = fill;
8641 current_section->phdrs = phdrs;
8642 pop_stat_ptr ();
8643 }
8644
8645 /* Set the output format type. -oformat overrides scripts. */
8646
8647 void
8648 lang_add_output_format (const char *format,
8649 const char *big,
8650 const char *little,
8651 int from_script)
8652 {
8653 if (output_target == NULL || !from_script)
8654 {
8655 if (command_line.endian == ENDIAN_BIG
8656 && big != NULL)
8657 format = big;
8658 else if (command_line.endian == ENDIAN_LITTLE
8659 && little != NULL)
8660 format = little;
8661
8662 output_target = format;
8663 }
8664 }
8665
8666 void
8667 lang_add_insert (const char *where, int is_before)
8668 {
8669 lang_insert_statement_type *new_stmt;
8670
8671 new_stmt = new_stat (lang_insert_statement, stat_ptr);
8672 new_stmt->where = where;
8673 new_stmt->is_before = is_before;
8674 saved_script_handle = previous_script_handle;
8675 }
8676
8677 /* Enter a group. This creates a new lang_group_statement, and sets
8678 stat_ptr to build new statements within the group. */
8679
8680 void
8681 lang_enter_group (void)
8682 {
8683 lang_group_statement_type *g;
8684
8685 g = new_stat (lang_group_statement, stat_ptr);
8686 lang_list_init (&g->children);
8687 push_stat_ptr (&g->children);
8688 }
8689
8690 /* Leave a group. This just resets stat_ptr to start writing to the
8691 regular list of statements again. Note that this will not work if
8692 groups can occur inside anything else which can adjust stat_ptr,
8693 but currently they can't. */
8694
8695 void
8696 lang_leave_group (void)
8697 {
8698 pop_stat_ptr ();
8699 }
8700
8701 /* Add a new program header. This is called for each entry in a PHDRS
8702 command in a linker script. */
8703
8704 void
8705 lang_new_phdr (const char *name,
8706 etree_type *type,
8707 bool filehdr,
8708 bool phdrs,
8709 etree_type *at,
8710 etree_type *flags)
8711 {
8712 struct lang_phdr *n, **pp;
8713 bool hdrs;
8714
8715 n = stat_alloc (sizeof (struct lang_phdr));
8716 n->next = NULL;
8717 n->name = name;
8718 n->type = exp_get_vma (type, 0, "program header type");
8719 n->filehdr = filehdr;
8720 n->phdrs = phdrs;
8721 n->at = at;
8722 n->flags = flags;
8723
8724 hdrs = n->type == 1 && (phdrs || filehdr);
8725
8726 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
8727 if (hdrs
8728 && (*pp)->type == 1
8729 && !((*pp)->filehdr || (*pp)->phdrs))
8730 {
8731 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
8732 " when prior PT_LOAD headers lack them\n"), NULL);
8733 hdrs = false;
8734 }
8735
8736 *pp = n;
8737 }
8738
8739 /* Record the program header information in the output BFD. FIXME: We
8740 should not be calling an ELF specific function here. */
8741
8742 static void
8743 lang_record_phdrs (void)
8744 {
8745 unsigned int alc;
8746 asection **secs;
8747 lang_output_section_phdr_list *last;
8748 struct lang_phdr *l;
8749 lang_output_section_statement_type *os;
8750
8751 alc = 10;
8752 secs = (asection **) xmalloc (alc * sizeof (asection *));
8753 last = NULL;
8754
8755 for (l = lang_phdr_list; l != NULL; l = l->next)
8756 {
8757 unsigned int c;
8758 flagword flags;
8759 bfd_vma at;
8760
8761 c = 0;
8762 for (os = (void *) lang_os_list.head;
8763 os != NULL;
8764 os = os->next)
8765 {
8766 lang_output_section_phdr_list *pl;
8767
8768 if (os->constraint < 0)
8769 continue;
8770
8771 pl = os->phdrs;
8772 if (pl != NULL)
8773 last = pl;
8774 else
8775 {
8776 if (os->sectype == noload_section
8777 || os->bfd_section == NULL
8778 || (os->bfd_section->flags & SEC_ALLOC) == 0)
8779 continue;
8780
8781 /* Don't add orphans to PT_INTERP header. */
8782 if (l->type == 3)
8783 continue;
8784
8785 if (last == NULL)
8786 {
8787 lang_output_section_statement_type *tmp_os;
8788
8789 /* If we have not run across a section with a program
8790 header assigned to it yet, then scan forwards to find
8791 one. This prevents inconsistencies in the linker's
8792 behaviour when a script has specified just a single
8793 header and there are sections in that script which are
8794 not assigned to it, and which occur before the first
8795 use of that header. See here for more details:
8796 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
8797 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
8798 if (tmp_os->phdrs)
8799 {
8800 last = tmp_os->phdrs;
8801 break;
8802 }
8803 if (last == NULL)
8804 einfo (_("%F%P: no sections assigned to phdrs\n"));
8805 }
8806 pl = last;
8807 }
8808
8809 if (os->bfd_section == NULL)
8810 continue;
8811
8812 for (; pl != NULL; pl = pl->next)
8813 {
8814 if (strcmp (pl->name, l->name) == 0)
8815 {
8816 if (c >= alc)
8817 {
8818 alc *= 2;
8819 secs = (asection **) xrealloc (secs,
8820 alc * sizeof (asection *));
8821 }
8822 secs[c] = os->bfd_section;
8823 ++c;
8824 pl->used = true;
8825 }
8826 }
8827 }
8828
8829 if (l->flags == NULL)
8830 flags = 0;
8831 else
8832 flags = exp_get_vma (l->flags, 0, "phdr flags");
8833
8834 if (l->at == NULL)
8835 at = 0;
8836 else
8837 at = exp_get_vma (l->at, 0, "phdr load address");
8838
8839 if (!bfd_record_phdr (link_info.output_bfd, l->type,
8840 l->flags != NULL, flags, l->at != NULL,
8841 at, l->filehdr, l->phdrs, c, secs))
8842 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
8843 }
8844
8845 free (secs);
8846
8847 /* Make sure all the phdr assignments succeeded. */
8848 for (os = (void *) lang_os_list.head;
8849 os != NULL;
8850 os = os->next)
8851 {
8852 lang_output_section_phdr_list *pl;
8853
8854 if (os->constraint < 0
8855 || os->bfd_section == NULL)
8856 continue;
8857
8858 for (pl = os->phdrs;
8859 pl != NULL;
8860 pl = pl->next)
8861 if (!pl->used && strcmp (pl->name, "NONE") != 0)
8862 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
8863 os->name, pl->name);
8864 }
8865 }
8866
8867 /* Record a list of sections which may not be cross referenced. */
8868
8869 void
8870 lang_add_nocrossref (lang_nocrossref_type *l)
8871 {
8872 struct lang_nocrossrefs *n;
8873
8874 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
8875 n->next = nocrossref_list;
8876 n->list = l;
8877 n->onlyfirst = false;
8878 nocrossref_list = n;
8879
8880 /* Set notice_all so that we get informed about all symbols. */
8881 link_info.notice_all = true;
8882 }
8883
8884 /* Record a section that cannot be referenced from a list of sections. */
8885
8886 void
8887 lang_add_nocrossref_to (lang_nocrossref_type *l)
8888 {
8889 lang_add_nocrossref (l);
8890 nocrossref_list->onlyfirst = true;
8891 }
8892 \f
8893 /* Overlay handling. We handle overlays with some static variables. */
8894
8895 /* The overlay virtual address. */
8896 static etree_type *overlay_vma;
8897 /* And subsection alignment. */
8898 static etree_type *overlay_subalign;
8899
8900 /* An expression for the maximum section size seen so far. */
8901 static etree_type *overlay_max;
8902
8903 /* A list of all the sections in this overlay. */
8904
8905 struct overlay_list {
8906 struct overlay_list *next;
8907 lang_output_section_statement_type *os;
8908 };
8909
8910 static struct overlay_list *overlay_list;
8911
8912 /* Start handling an overlay. */
8913
8914 void
8915 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
8916 {
8917 /* The grammar should prevent nested overlays from occurring. */
8918 ASSERT (overlay_vma == NULL
8919 && overlay_subalign == NULL
8920 && overlay_max == NULL);
8921
8922 overlay_vma = vma_expr;
8923 overlay_subalign = subalign;
8924 }
8925
8926 /* Start a section in an overlay. We handle this by calling
8927 lang_enter_output_section_statement with the correct VMA.
8928 lang_leave_overlay sets up the LMA and memory regions. */
8929
8930 void
8931 lang_enter_overlay_section (const char *name)
8932 {
8933 struct overlay_list *n;
8934 etree_type *size;
8935
8936 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
8937 0, 0, overlay_subalign, 0, 0, 0);
8938
8939 /* If this is the first section, then base the VMA of future
8940 sections on this one. This will work correctly even if `.' is
8941 used in the addresses. */
8942 if (overlay_list == NULL)
8943 overlay_vma = exp_nameop (ADDR, name);
8944
8945 /* Remember the section. */
8946 n = (struct overlay_list *) xmalloc (sizeof *n);
8947 n->os = current_section;
8948 n->next = overlay_list;
8949 overlay_list = n;
8950
8951 size = exp_nameop (SIZEOF, name);
8952
8953 /* Arrange to work out the maximum section end address. */
8954 if (overlay_max == NULL)
8955 overlay_max = size;
8956 else
8957 overlay_max = exp_binop (MAX_K, overlay_max, size);
8958 }
8959
8960 /* Finish a section in an overlay. There isn't any special to do
8961 here. */
8962
8963 void
8964 lang_leave_overlay_section (fill_type *fill,
8965 lang_output_section_phdr_list *phdrs)
8966 {
8967 const char *name;
8968 char *clean, *s2;
8969 const char *s1;
8970 char *buf;
8971
8972 name = current_section->name;
8973
8974 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
8975 region and that no load-time region has been specified. It doesn't
8976 really matter what we say here, since lang_leave_overlay will
8977 override it. */
8978 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
8979
8980 /* Define the magic symbols. */
8981
8982 clean = (char *) xmalloc (strlen (name) + 1);
8983 s2 = clean;
8984 for (s1 = name; *s1 != '\0'; s1++)
8985 if (ISALNUM (*s1) || *s1 == '_')
8986 *s2++ = *s1;
8987 *s2 = '\0';
8988
8989 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
8990 sprintf (buf, "__load_start_%s", clean);
8991 lang_add_assignment (exp_provide (buf,
8992 exp_nameop (LOADADDR, name),
8993 false));
8994
8995 buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
8996 sprintf (buf, "__load_stop_%s", clean);
8997 lang_add_assignment (exp_provide (buf,
8998 exp_binop ('+',
8999 exp_nameop (LOADADDR, name),
9000 exp_nameop (SIZEOF, name)),
9001 false));
9002
9003 free (clean);
9004 }
9005
9006 /* Finish an overlay. If there are any overlay wide settings, this
9007 looks through all the sections in the overlay and sets them. */
9008
9009 void
9010 lang_leave_overlay (etree_type *lma_expr,
9011 int nocrossrefs,
9012 fill_type *fill,
9013 const char *memspec,
9014 lang_output_section_phdr_list *phdrs,
9015 const char *lma_memspec)
9016 {
9017 lang_memory_region_type *region;
9018 lang_memory_region_type *lma_region;
9019 struct overlay_list *l;
9020 lang_nocrossref_type *nocrossref;
9021
9022 lang_get_regions (&region, &lma_region,
9023 memspec, lma_memspec,
9024 lma_expr != NULL, false);
9025
9026 nocrossref = NULL;
9027
9028 /* After setting the size of the last section, set '.' to end of the
9029 overlay region. */
9030 if (overlay_list != NULL)
9031 {
9032 overlay_list->os->update_dot = 1;
9033 overlay_list->os->update_dot_tree
9034 = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false);
9035 }
9036
9037 l = overlay_list;
9038 while (l != NULL)
9039 {
9040 struct overlay_list *next;
9041
9042 if (fill != NULL && l->os->fill == NULL)
9043 l->os->fill = fill;
9044
9045 l->os->region = region;
9046 l->os->lma_region = lma_region;
9047
9048 /* The first section has the load address specified in the
9049 OVERLAY statement. The rest are worked out from that.
9050 The base address is not needed (and should be null) if
9051 an LMA region was specified. */
9052 if (l->next == 0)
9053 {
9054 l->os->load_base = lma_expr;
9055 l->os->sectype = first_overlay_section;
9056 }
9057 if (phdrs != NULL && l->os->phdrs == NULL)
9058 l->os->phdrs = phdrs;
9059
9060 if (nocrossrefs)
9061 {
9062 lang_nocrossref_type *nc;
9063
9064 nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
9065 nc->name = l->os->name;
9066 nc->next = nocrossref;
9067 nocrossref = nc;
9068 }
9069
9070 next = l->next;
9071 free (l);
9072 l = next;
9073 }
9074
9075 if (nocrossref != NULL)
9076 lang_add_nocrossref (nocrossref);
9077
9078 overlay_vma = NULL;
9079 overlay_list = NULL;
9080 overlay_max = NULL;
9081 overlay_subalign = NULL;
9082 }
9083 \f
9084 /* Version handling. This is only useful for ELF. */
9085
9086 /* If PREV is NULL, return first version pattern matching particular symbol.
9087 If PREV is non-NULL, return first version pattern matching particular
9088 symbol after PREV (previously returned by lang_vers_match). */
9089
9090 static struct bfd_elf_version_expr *
9091 lang_vers_match (struct bfd_elf_version_expr_head *head,
9092 struct bfd_elf_version_expr *prev,
9093 const char *sym)
9094 {
9095 const char *c_sym;
9096 const char *cxx_sym = sym;
9097 const char *java_sym = sym;
9098 struct bfd_elf_version_expr *expr = NULL;
9099 enum demangling_styles curr_style;
9100
9101 curr_style = CURRENT_DEMANGLING_STYLE;
9102 cplus_demangle_set_style (no_demangling);
9103 c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS);
9104 if (!c_sym)
9105 c_sym = sym;
9106 cplus_demangle_set_style (curr_style);
9107
9108 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
9109 {
9110 cxx_sym = bfd_demangle (link_info.output_bfd, sym,
9111 DMGL_PARAMS | DMGL_ANSI);
9112 if (!cxx_sym)
9113 cxx_sym = sym;
9114 }
9115 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
9116 {
9117 java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA);
9118 if (!java_sym)
9119 java_sym = sym;
9120 }
9121
9122 if (head->htab && (prev == NULL || prev->literal))
9123 {
9124 struct bfd_elf_version_expr e;
9125
9126 switch (prev ? prev->mask : 0)
9127 {
9128 case 0:
9129 if (head->mask & BFD_ELF_VERSION_C_TYPE)
9130 {
9131 e.pattern = c_sym;
9132 expr = (struct bfd_elf_version_expr *)
9133 htab_find ((htab_t) head->htab, &e);
9134 while (expr && strcmp (expr->pattern, c_sym) == 0)
9135 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
9136 goto out_ret;
9137 else
9138 expr = expr->next;
9139 }
9140 /* Fallthrough */
9141 case BFD_ELF_VERSION_C_TYPE:
9142 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
9143 {
9144 e.pattern = cxx_sym;
9145 expr = (struct bfd_elf_version_expr *)
9146 htab_find ((htab_t) head->htab, &e);
9147 while (expr && strcmp (expr->pattern, cxx_sym) == 0)
9148 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
9149 goto out_ret;
9150 else
9151 expr = expr->next;
9152 }
9153 /* Fallthrough */
9154 case BFD_ELF_VERSION_CXX_TYPE:
9155 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
9156 {
9157 e.pattern = java_sym;
9158 expr = (struct bfd_elf_version_expr *)
9159 htab_find ((htab_t) head->htab, &e);
9160 while (expr && strcmp (expr->pattern, java_sym) == 0)
9161 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
9162 goto out_ret;
9163 else
9164 expr = expr->next;
9165 }
9166 /* Fallthrough */
9167 default:
9168 break;
9169 }
9170 }
9171
9172 /* Finally, try the wildcards. */
9173 if (prev == NULL || prev->literal)
9174 expr = head->remaining;
9175 else
9176 expr = prev->next;
9177 for (; expr; expr = expr->next)
9178 {
9179 const char *s;
9180
9181 if (!expr->pattern)
9182 continue;
9183
9184 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
9185 break;
9186
9187 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
9188 s = java_sym;
9189 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
9190 s = cxx_sym;
9191 else
9192 s = c_sym;
9193 if (fnmatch (expr->pattern, s, 0) == 0)
9194 break;
9195 }
9196
9197 out_ret:
9198 if (c_sym != sym)
9199 free ((char *) c_sym);
9200 if (cxx_sym != sym)
9201 free ((char *) cxx_sym);
9202 if (java_sym != sym)
9203 free ((char *) java_sym);
9204 return expr;
9205 }
9206
9207 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
9208 return a pointer to the symbol name with any backslash quotes removed. */
9209
9210 static const char *
9211 realsymbol (const char *pattern)
9212 {
9213 const char *p;
9214 bool changed = false, backslash = false;
9215 char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
9216
9217 for (p = pattern, s = symbol; *p != '\0'; ++p)
9218 {
9219 /* It is a glob pattern only if there is no preceding
9220 backslash. */
9221 if (backslash)
9222 {
9223 /* Remove the preceding backslash. */
9224 *(s - 1) = *p;
9225 backslash = false;
9226 changed = true;
9227 }
9228 else
9229 {
9230 if (*p == '?' || *p == '*' || *p == '[')
9231 {
9232 free (symbol);
9233 return NULL;
9234 }
9235
9236 *s++ = *p;
9237 backslash = *p == '\\';
9238 }
9239 }
9240
9241 if (changed)
9242 {
9243 *s = '\0';
9244 return symbol;
9245 }
9246 else
9247 {
9248 free (symbol);
9249 return pattern;
9250 }
9251 }
9252
9253 /* This is called for each variable name or match expression. NEW_NAME is
9254 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
9255 pattern to be matched against symbol names. */
9256
9257 struct bfd_elf_version_expr *
9258 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
9259 const char *new_name,
9260 const char *lang,
9261 bool literal_p)
9262 {
9263 struct bfd_elf_version_expr *ret;
9264
9265 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
9266 ret->next = orig;
9267 ret->symver = 0;
9268 ret->script = 0;
9269 ret->literal = true;
9270 ret->pattern = literal_p ? new_name : realsymbol (new_name);
9271 if (ret->pattern == NULL)
9272 {
9273 ret->pattern = new_name;
9274 ret->literal = false;
9275 }
9276
9277 if (lang == NULL || strcasecmp (lang, "C") == 0)
9278 ret->mask = BFD_ELF_VERSION_C_TYPE;
9279 else if (strcasecmp (lang, "C++") == 0)
9280 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
9281 else if (strcasecmp (lang, "Java") == 0)
9282 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
9283 else
9284 {
9285 einfo (_("%X%P: unknown language `%s' in version information\n"),
9286 lang);
9287 ret->mask = BFD_ELF_VERSION_C_TYPE;
9288 }
9289
9290 return ldemul_new_vers_pattern (ret);
9291 }
9292
9293 /* This is called for each set of variable names and match
9294 expressions. */
9295
9296 struct bfd_elf_version_tree *
9297 lang_new_vers_node (struct bfd_elf_version_expr *globals,
9298 struct bfd_elf_version_expr *locals)
9299 {
9300 struct bfd_elf_version_tree *ret;
9301
9302 ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
9303 ret->globals.list = globals;
9304 ret->locals.list = locals;
9305 ret->match = lang_vers_match;
9306 ret->name_indx = (unsigned int) -1;
9307 return ret;
9308 }
9309
9310 /* This static variable keeps track of version indices. */
9311
9312 static int version_index;
9313
9314 static hashval_t
9315 version_expr_head_hash (const void *p)
9316 {
9317 const struct bfd_elf_version_expr *e =
9318 (const struct bfd_elf_version_expr *) p;
9319
9320 return htab_hash_string (e->pattern);
9321 }
9322
9323 static int
9324 version_expr_head_eq (const void *p1, const void *p2)
9325 {
9326 const struct bfd_elf_version_expr *e1 =
9327 (const struct bfd_elf_version_expr *) p1;
9328 const struct bfd_elf_version_expr *e2 =
9329 (const struct bfd_elf_version_expr *) p2;
9330
9331 return strcmp (e1->pattern, e2->pattern) == 0;
9332 }
9333
9334 static void
9335 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
9336 {
9337 size_t count = 0;
9338 struct bfd_elf_version_expr *e, *next;
9339 struct bfd_elf_version_expr **list_loc, **remaining_loc;
9340
9341 for (e = head->list; e; e = e->next)
9342 {
9343 if (e->literal)
9344 count++;
9345 head->mask |= e->mask;
9346 }
9347
9348 if (count)
9349 {
9350 head->htab = htab_create (count * 2, version_expr_head_hash,
9351 version_expr_head_eq, NULL);
9352 list_loc = &head->list;
9353 remaining_loc = &head->remaining;
9354 for (e = head->list; e; e = next)
9355 {
9356 next = e->next;
9357 if (!e->literal)
9358 {
9359 *remaining_loc = e;
9360 remaining_loc = &e->next;
9361 }
9362 else
9363 {
9364 void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
9365
9366 if (*loc)
9367 {
9368 struct bfd_elf_version_expr *e1, *last;
9369
9370 e1 = (struct bfd_elf_version_expr *) *loc;
9371 last = NULL;
9372 do
9373 {
9374 if (e1->mask == e->mask)
9375 {
9376 last = NULL;
9377 break;
9378 }
9379 last = e1;
9380 e1 = e1->next;
9381 }
9382 while (e1 && strcmp (e1->pattern, e->pattern) == 0);
9383
9384 if (last == NULL)
9385 {
9386 /* This is a duplicate. */
9387 /* FIXME: Memory leak. Sometimes pattern is not
9388 xmalloced alone, but in larger chunk of memory. */
9389 /* free (e->pattern); */
9390 free (e);
9391 }
9392 else
9393 {
9394 e->next = last->next;
9395 last->next = e;
9396 }
9397 }
9398 else
9399 {
9400 *loc = e;
9401 *list_loc = e;
9402 list_loc = &e->next;
9403 }
9404 }
9405 }
9406 *remaining_loc = NULL;
9407 *list_loc = head->remaining;
9408 }
9409 else
9410 head->remaining = head->list;
9411 }
9412
9413 /* This is called when we know the name and dependencies of the
9414 version. */
9415
9416 void
9417 lang_register_vers_node (const char *name,
9418 struct bfd_elf_version_tree *version,
9419 struct bfd_elf_version_deps *deps)
9420 {
9421 struct bfd_elf_version_tree *t, **pp;
9422 struct bfd_elf_version_expr *e1;
9423
9424 if (name == NULL)
9425 name = "";
9426
9427 if (link_info.version_info != NULL
9428 && (name[0] == '\0' || link_info.version_info->name[0] == '\0'))
9429 {
9430 einfo (_("%X%P: anonymous version tag cannot be combined"
9431 " with other version tags\n"));
9432 free (version);
9433 return;
9434 }
9435
9436 /* Make sure this node has a unique name. */
9437 for (t = link_info.version_info; t != NULL; t = t->next)
9438 if (strcmp (t->name, name) == 0)
9439 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
9440
9441 lang_finalize_version_expr_head (&version->globals);
9442 lang_finalize_version_expr_head (&version->locals);
9443
9444 /* Check the global and local match names, and make sure there
9445 aren't any duplicates. */
9446
9447 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
9448 {
9449 for (t = link_info.version_info; t != NULL; t = t->next)
9450 {
9451 struct bfd_elf_version_expr *e2;
9452
9453 if (t->locals.htab && e1->literal)
9454 {
9455 e2 = (struct bfd_elf_version_expr *)
9456 htab_find ((htab_t) t->locals.htab, e1);
9457 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
9458 {
9459 if (e1->mask == e2->mask)
9460 einfo (_("%X%P: duplicate expression `%s'"
9461 " in version information\n"), e1->pattern);
9462 e2 = e2->next;
9463 }
9464 }
9465 else if (!e1->literal)
9466 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
9467 if (strcmp (e1->pattern, e2->pattern) == 0
9468 && e1->mask == e2->mask)
9469 einfo (_("%X%P: duplicate expression `%s'"
9470 " in version information\n"), e1->pattern);
9471 }
9472 }
9473
9474 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
9475 {
9476 for (t = link_info.version_info; t != NULL; t = t->next)
9477 {
9478 struct bfd_elf_version_expr *e2;
9479
9480 if (t->globals.htab && e1->literal)
9481 {
9482 e2 = (struct bfd_elf_version_expr *)
9483 htab_find ((htab_t) t->globals.htab, e1);
9484 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
9485 {
9486 if (e1->mask == e2->mask)
9487 einfo (_("%X%P: duplicate expression `%s'"
9488 " in version information\n"),
9489 e1->pattern);
9490 e2 = e2->next;
9491 }
9492 }
9493 else if (!e1->literal)
9494 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
9495 if (strcmp (e1->pattern, e2->pattern) == 0
9496 && e1->mask == e2->mask)
9497 einfo (_("%X%P: duplicate expression `%s'"
9498 " in version information\n"), e1->pattern);
9499 }
9500 }
9501
9502 version->deps = deps;
9503 version->name = name;
9504 if (name[0] != '\0')
9505 {
9506 ++version_index;
9507 version->vernum = version_index;
9508 }
9509 else
9510 version->vernum = 0;
9511
9512 for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next)
9513 ;
9514 *pp = version;
9515 }
9516
9517 /* This is called when we see a version dependency. */
9518
9519 struct bfd_elf_version_deps *
9520 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
9521 {
9522 struct bfd_elf_version_deps *ret;
9523 struct bfd_elf_version_tree *t;
9524
9525 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
9526 ret->next = list;
9527
9528 for (t = link_info.version_info; t != NULL; t = t->next)
9529 {
9530 if (strcmp (t->name, name) == 0)
9531 {
9532 ret->version_needed = t;
9533 return ret;
9534 }
9535 }
9536
9537 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
9538
9539 ret->version_needed = NULL;
9540 return ret;
9541 }
9542
9543 static void
9544 lang_do_version_exports_section (void)
9545 {
9546 struct bfd_elf_version_expr *greg = NULL, *lreg;
9547
9548 LANG_FOR_EACH_INPUT_STATEMENT (is)
9549 {
9550 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
9551 char *contents, *p;
9552 bfd_size_type len;
9553
9554 if (sec == NULL)
9555 continue;
9556
9557 len = sec->size;
9558 contents = (char *) xmalloc (len);
9559 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
9560 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
9561
9562 p = contents;
9563 while (p < contents + len)
9564 {
9565 greg = lang_new_vers_pattern (greg, p, NULL, false);
9566 p = strchr (p, '\0') + 1;
9567 }
9568
9569 /* Do not free the contents, as we used them creating the regex. */
9570
9571 /* Do not include this section in the link. */
9572 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
9573 }
9574
9575 lreg = lang_new_vers_pattern (NULL, "*", NULL, false);
9576 lang_register_vers_node (command_line.version_exports_section,
9577 lang_new_vers_node (greg, lreg), NULL);
9578 }
9579
9580 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
9581 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
9582 thrown, however, references to symbols in the origin and length fields
9583 will be pushed into the symbol table, this allows PROVIDE statements to
9584 then provide these symbols. This function is called a second time with
9585 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
9586 data structures, and throw errors if missing symbols are encountered. */
9587
9588 static void
9589 lang_do_memory_regions (bool update_regions_p)
9590 {
9591 lang_memory_region_type *r = lang_memory_region_list;
9592
9593 for (; r != NULL; r = r->next)
9594 {
9595 if (r->origin_exp)
9596 {
9597 exp_fold_tree_no_dot (r->origin_exp);
9598 if (update_regions_p)
9599 {
9600 if (expld.result.valid_p)
9601 {
9602 r->origin = expld.result.value;
9603 r->current = r->origin;
9604 }
9605 else
9606 einfo (_("%P: invalid origin for memory region %s\n"),
9607 r->name_list.name);
9608 }
9609 }
9610 if (r->length_exp)
9611 {
9612 exp_fold_tree_no_dot (r->length_exp);
9613 if (update_regions_p)
9614 {
9615 if (expld.result.valid_p)
9616 r->length = expld.result.value;
9617 else
9618 einfo (_("%P: invalid length for memory region %s\n"),
9619 r->name_list.name);
9620 }
9621 }
9622 }
9623 }
9624
9625 void
9626 lang_add_unique (const char *name)
9627 {
9628 struct unique_sections *ent;
9629
9630 for (ent = unique_section_list; ent; ent = ent->next)
9631 if (strcmp (ent->name, name) == 0)
9632 return;
9633
9634 ent = (struct unique_sections *) xmalloc (sizeof *ent);
9635 ent->name = xstrdup (name);
9636 ent->next = unique_section_list;
9637 unique_section_list = ent;
9638 }
9639
9640 /* Append the list of dynamic symbols to the existing one. */
9641
9642 void
9643 lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p,
9644 struct bfd_elf_version_expr *dynamic)
9645 {
9646 if (*list_p)
9647 {
9648 struct bfd_elf_version_expr *tail;
9649 for (tail = dynamic; tail->next != NULL; tail = tail->next)
9650 ;
9651 tail->next = (*list_p)->head.list;
9652 (*list_p)->head.list = dynamic;
9653 }
9654 else
9655 {
9656 struct bfd_elf_dynamic_list *d;
9657
9658 d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
9659 d->head.list = dynamic;
9660 d->match = lang_vers_match;
9661 *list_p = d;
9662 }
9663 }
9664
9665 /* Append the list of C++ typeinfo dynamic symbols to the existing
9666 one. */
9667
9668 void
9669 lang_append_dynamic_list_cpp_typeinfo (void)
9670 {
9671 const char *symbols[] =
9672 {
9673 "typeinfo name for*",
9674 "typeinfo for*"
9675 };
9676 struct bfd_elf_version_expr *dynamic = NULL;
9677 unsigned int i;
9678
9679 for (i = 0; i < ARRAY_SIZE (symbols); i++)
9680 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
9681 false);
9682
9683 lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
9684 }
9685
9686 /* Append the list of C++ operator new and delete dynamic symbols to the
9687 existing one. */
9688
9689 void
9690 lang_append_dynamic_list_cpp_new (void)
9691 {
9692 const char *symbols[] =
9693 {
9694 "operator new*",
9695 "operator delete*"
9696 };
9697 struct bfd_elf_version_expr *dynamic = NULL;
9698 unsigned int i;
9699
9700 for (i = 0; i < ARRAY_SIZE (symbols); i++)
9701 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
9702 false);
9703
9704 lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
9705 }
9706
9707 /* Scan a space and/or comma separated string of features. */
9708
9709 void
9710 lang_ld_feature (char *str)
9711 {
9712 char *p, *q;
9713
9714 p = str;
9715 while (*p)
9716 {
9717 char sep;
9718 while (*p == ',' || ISSPACE (*p))
9719 ++p;
9720 if (!*p)
9721 break;
9722 q = p + 1;
9723 while (*q && *q != ',' && !ISSPACE (*q))
9724 ++q;
9725 sep = *q;
9726 *q = 0;
9727 if (strcasecmp (p, "SANE_EXPR") == 0)
9728 config.sane_expr = true;
9729 else
9730 einfo (_("%X%P: unknown feature `%s'\n"), p);
9731 *q = sep;
9732 p = q;
9733 }
9734 }
9735
9736 /* Pretty print memory amount. */
9737
9738 static void
9739 lang_print_memory_size (uint64_t sz)
9740 {
9741 if ((sz & 0x3fffffff) == 0)
9742 printf ("%10" PRIu64 " GB", sz >> 30);
9743 else if ((sz & 0xfffff) == 0)
9744 printf ("%10" PRIu64 " MB", sz >> 20);
9745 else if ((sz & 0x3ff) == 0)
9746 printf ("%10" PRIu64 " KB", sz >> 10);
9747 else
9748 printf (" %10" PRIu64 " B", sz);
9749 }
9750
9751 /* Implement --print-memory-usage: disply per region memory usage. */
9752
9753 void
9754 lang_print_memory_usage (void)
9755 {
9756 lang_memory_region_type *r;
9757
9758 printf ("Memory region Used Size Region Size %%age Used\n");
9759 for (r = lang_memory_region_list; r->next != NULL; r = r->next)
9760 {
9761 bfd_vma used_length = r->current - r->origin;
9762
9763 printf ("%16s: ",r->name_list.name);
9764 lang_print_memory_size (used_length);
9765 lang_print_memory_size (r->length);
9766
9767 if (r->length != 0)
9768 {
9769 double percent = used_length * 100.0 / r->length;
9770 printf (" %6.2f%%", percent);
9771 }
9772 printf ("\n");
9773 }
9774 }