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