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