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