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