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