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