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