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