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