* ldlang.c (closest_target_match): Skip generic big and little
[deliverable/binutils-gdb.git] / ld / ldlang.c
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of the GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include <limits.h>
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libiberty.h"
28 #include "safe-ctype.h"
29 #include "obstack.h"
30 #include "bfdlink.h"
31
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldexp.h"
35 #include "ldlang.h"
36 #include <ldgram.h>
37 #include "ldlex.h"
38 #include "ldmisc.h"
39 #include "ldctor.h"
40 #include "ldfile.h"
41 #include "ldemul.h"
42 #include "fnmatch.h"
43 #include "demangle.h"
44 #include "hashtab.h"
45
46 #ifndef offsetof
47 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 #endif
49
50 /* Locals variables. */
51 static struct obstack stat_obstack;
52 static struct obstack map_obstack;
53
54 #define obstack_chunk_alloc xmalloc
55 #define obstack_chunk_free free
56 static const char *startup_file;
57 static bfd_boolean placed_commons = FALSE;
58 static bfd_boolean stripped_excluded_sections = FALSE;
59 static lang_output_section_statement_type *default_common_section;
60 static bfd_boolean map_option_f;
61 static bfd_vma print_dot;
62 static lang_input_statement_type *first_file;
63 static const char *current_target;
64 static const char *output_target;
65 static lang_statement_list_type statement_list;
66 static struct bfd_hash_table lang_definedness_table;
67
68 /* Forward declarations. */
69 static void exp_init_os (etree_type *);
70 static void init_map_userdata (bfd *, asection *, void *);
71 static lang_input_statement_type *lookup_name (const char *);
72 static struct bfd_hash_entry *lang_definedness_newfunc
73 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
74 static void insert_undefined (const char *);
75 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
76 static void print_statement (lang_statement_union_type *,
77 lang_output_section_statement_type *);
78 static void print_statement_list (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statements (void);
81 static void print_input_section (asection *);
82 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
83 static void lang_record_phdrs (void);
84 static void lang_do_version_exports_section (void);
85 static void lang_finalize_version_expr_head
86 (struct bfd_elf_version_expr_head *);
87
88 /* Exported variables. */
89 lang_output_section_statement_type *abs_output_section;
90 lang_statement_list_type lang_output_section_statement;
91 lang_statement_list_type *stat_ptr = &statement_list;
92 lang_statement_list_type file_chain = { NULL, NULL };
93 lang_statement_list_type input_file_chain;
94 struct bfd_sym_chain entry_symbol = { NULL, NULL };
95 static const char *entry_symbol_default = "start";
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_phdr *lang_phdr_list;
103 struct lang_nocrossrefs *nocrossref_list;
104 static struct unique_sections *unique_section_list;
105 static bfd_boolean ldlang_sysrooted_script = FALSE;
106
107 /* Functions that traverse the linker script and might evaluate
108 DEFINED() need to increment this. */
109 int lang_statement_iteration = 0;
110
111 etree_type *base; /* Relocation base - or null */
112
113 /* Return TRUE if the PATTERN argument is a wildcard pattern.
114 Although backslashes are treated specially if a pattern contains
115 wildcards, we do not consider the mere presence of a backslash to
116 be enough to cause the pattern to be treated as a wildcard.
117 That lets us handle DOS filenames more naturally. */
118 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
119
120 #define new_stat(x, y) \
121 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
122
123 #define outside_section_address(q) \
124 ((q)->output_offset + (q)->output_section->vma)
125
126 #define outside_symbol_address(q) \
127 ((q)->value + outside_section_address (q->section))
128
129 #define SECTION_NAME_MAP_LENGTH (16)
130
131 void *
132 stat_alloc (size_t size)
133 {
134 return obstack_alloc (&stat_obstack, size);
135 }
136
137 bfd_boolean
138 unique_section_p (const asection *sec)
139 {
140 struct unique_sections *unam;
141 const char *secnam;
142
143 if (link_info.relocatable
144 && sec->owner != NULL
145 && bfd_is_group_section (sec->owner, sec))
146 return TRUE;
147
148 secnam = sec->name;
149 for (unam = unique_section_list; unam; unam = unam->next)
150 if (wildcardp (unam->name)
151 ? fnmatch (unam->name, secnam, 0) == 0
152 : strcmp (unam->name, secnam) == 0)
153 {
154 return TRUE;
155 }
156
157 return FALSE;
158 }
159
160 /* Generic traversal routines for finding matching sections. */
161
162 /* Try processing a section against a wildcard. This just calls
163 the callback unless the filename exclusion list is present
164 and excludes the file. It's hardly ever present so this
165 function is very fast. */
166
167 static void
168 walk_wild_consider_section (lang_wild_statement_type *ptr,
169 lang_input_statement_type *file,
170 asection *s,
171 struct wildcard_list *sec,
172 callback_t callback,
173 void *data)
174 {
175 bfd_boolean skip = FALSE;
176 struct name_list *list_tmp;
177
178 /* Don't process sections from files which were
179 excluded. */
180 for (list_tmp = sec->spec.exclude_name_list;
181 list_tmp;
182 list_tmp = list_tmp->next)
183 {
184 bfd_boolean is_wildcard = wildcardp (list_tmp->name);
185 if (is_wildcard)
186 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
187 else
188 skip = strcmp (list_tmp->name, file->filename) == 0;
189
190 /* If this file is part of an archive, and the archive is
191 excluded, exclude this file. */
192 if (! skip && file->the_bfd != NULL
193 && file->the_bfd->my_archive != NULL
194 && file->the_bfd->my_archive->filename != NULL)
195 {
196 if (is_wildcard)
197 skip = fnmatch (list_tmp->name,
198 file->the_bfd->my_archive->filename,
199 0) == 0;
200 else
201 skip = strcmp (list_tmp->name,
202 file->the_bfd->my_archive->filename) == 0;
203 }
204
205 if (skip)
206 break;
207 }
208
209 if (!skip)
210 (*callback) (ptr, sec, s, file, data);
211 }
212
213 /* Lowest common denominator routine that can handle everything correctly,
214 but slowly. */
215
216 static void
217 walk_wild_section_general (lang_wild_statement_type *ptr,
218 lang_input_statement_type *file,
219 callback_t callback,
220 void *data)
221 {
222 asection *s;
223 struct wildcard_list *sec;
224
225 for (s = file->the_bfd->sections; s != NULL; s = s->next)
226 {
227 sec = ptr->section_list;
228 if (sec == NULL)
229 (*callback) (ptr, sec, s, file, data);
230
231 while (sec != NULL)
232 {
233 bfd_boolean skip = FALSE;
234
235 if (sec->spec.name != NULL)
236 {
237 const char *sname = bfd_get_section_name (file->the_bfd, s);
238
239 if (wildcardp (sec->spec.name))
240 skip = fnmatch (sec->spec.name, sname, 0) != 0;
241 else
242 skip = strcmp (sec->spec.name, sname) != 0;
243 }
244
245 if (!skip)
246 walk_wild_consider_section (ptr, file, s, sec, callback, data);
247
248 sec = sec->next;
249 }
250 }
251 }
252
253 /* Routines to find a single section given its name. If there's more
254 than one section with that name, we report that. */
255
256 typedef struct
257 {
258 asection *found_section;
259 bfd_boolean multiple_sections_found;
260 } section_iterator_callback_data;
261
262 static bfd_boolean
263 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
264 {
265 section_iterator_callback_data *d = data;
266
267 if (d->found_section != NULL)
268 {
269 d->multiple_sections_found = TRUE;
270 return TRUE;
271 }
272
273 d->found_section = s;
274 return FALSE;
275 }
276
277 static asection *
278 find_section (lang_input_statement_type *file,
279 struct wildcard_list *sec,
280 bfd_boolean *multiple_sections_found)
281 {
282 section_iterator_callback_data cb_data = { NULL, FALSE };
283
284 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
285 section_iterator_callback, &cb_data);
286 *multiple_sections_found = cb_data.multiple_sections_found;
287 return cb_data.found_section;
288 }
289
290 /* Code for handling simple wildcards without going through fnmatch,
291 which can be expensive because of charset translations etc. */
292
293 /* A simple wild is a literal string followed by a single '*',
294 where the literal part is at least 4 characters long. */
295
296 static bfd_boolean
297 is_simple_wild (const char *name)
298 {
299 size_t len = strcspn (name, "*?[");
300 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
301 }
302
303 static bfd_boolean
304 match_simple_wild (const char *pattern, const char *name)
305 {
306 /* The first four characters of the pattern are guaranteed valid
307 non-wildcard characters. So we can go faster. */
308 if (pattern[0] != name[0] || pattern[1] != name[1]
309 || pattern[2] != name[2] || pattern[3] != name[3])
310 return FALSE;
311
312 pattern += 4;
313 name += 4;
314 while (*pattern != '*')
315 if (*name++ != *pattern++)
316 return FALSE;
317
318 return TRUE;
319 }
320
321 /* Compare sections ASEC and BSEC according to SORT. */
322
323 static int
324 compare_section (sort_type sort, asection *asec, asection *bsec)
325 {
326 int ret;
327
328 switch (sort)
329 {
330 default:
331 abort ();
332
333 case by_alignment_name:
334 ret = (bfd_section_alignment (bsec->owner, bsec)
335 - bfd_section_alignment (asec->owner, asec));
336 if (ret)
337 break;
338 /* Fall through. */
339
340 case by_name:
341 ret = strcmp (bfd_get_section_name (asec->owner, asec),
342 bfd_get_section_name (bsec->owner, bsec));
343 break;
344
345 case by_name_alignment:
346 ret = strcmp (bfd_get_section_name (asec->owner, asec),
347 bfd_get_section_name (bsec->owner, bsec));
348 if (ret)
349 break;
350 /* Fall through. */
351
352 case by_alignment:
353 ret = (bfd_section_alignment (bsec->owner, bsec)
354 - bfd_section_alignment (asec->owner, asec));
355 break;
356 }
357
358 return ret;
359 }
360
361 /* Build a Binary Search Tree to sort sections, unlike insertion sort
362 used in wild_sort(). BST is considerably faster if the number of
363 of sections are large. */
364
365 static lang_section_bst_type **
366 wild_sort_fast (lang_wild_statement_type *wild,
367 struct wildcard_list *sec,
368 lang_input_statement_type *file ATTRIBUTE_UNUSED,
369 asection *section)
370 {
371 lang_section_bst_type **tree;
372
373 tree = &wild->tree;
374 if (!wild->filenames_sorted
375 && (sec == NULL || sec->spec.sorted == none))
376 {
377 /* Append at the right end of tree. */
378 while (*tree)
379 tree = &((*tree)->right);
380 return tree;
381 }
382
383 while (*tree)
384 {
385 /* Find the correct node to append this section. */
386 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
387 tree = &((*tree)->left);
388 else
389 tree = &((*tree)->right);
390 }
391
392 return tree;
393 }
394
395 /* Use wild_sort_fast to build a BST to sort sections. */
396
397 static void
398 output_section_callback_fast (lang_wild_statement_type *ptr,
399 struct wildcard_list *sec,
400 asection *section,
401 lang_input_statement_type *file,
402 void *output ATTRIBUTE_UNUSED)
403 {
404 lang_section_bst_type *node;
405 lang_section_bst_type **tree;
406
407 if (unique_section_p (section))
408 return;
409
410 node = xmalloc (sizeof (lang_section_bst_type));
411 node->left = 0;
412 node->right = 0;
413 node->section = section;
414
415 tree = wild_sort_fast (ptr, sec, file, section);
416 if (tree != NULL)
417 *tree = node;
418 }
419
420 /* Convert a sorted sections' BST back to list form. */
421
422 static void
423 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
424 lang_section_bst_type *tree,
425 void *output)
426 {
427 if (tree->left)
428 output_section_callback_tree_to_list (ptr, tree->left, output);
429
430 lang_add_section (&ptr->children, tree->section,
431 (lang_output_section_statement_type *) output);
432
433 if (tree->right)
434 output_section_callback_tree_to_list (ptr, tree->right, output);
435
436 free (tree);
437 }
438
439 /* Specialized, optimized routines for handling different kinds of
440 wildcards */
441
442 static void
443 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
444 lang_input_statement_type *file,
445 callback_t callback,
446 void *data)
447 {
448 /* We can just do a hash lookup for the section with the right name.
449 But if that lookup discovers more than one section with the name
450 (should be rare), we fall back to the general algorithm because
451 we would otherwise have to sort the sections to make sure they
452 get processed in the bfd's order. */
453 bfd_boolean multiple_sections_found;
454 struct wildcard_list *sec0 = ptr->handler_data[0];
455 asection *s0 = find_section (file, sec0, &multiple_sections_found);
456
457 if (multiple_sections_found)
458 walk_wild_section_general (ptr, file, callback, data);
459 else if (s0)
460 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
461 }
462
463 static void
464 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
465 lang_input_statement_type *file,
466 callback_t callback,
467 void *data)
468 {
469 asection *s;
470 struct wildcard_list *wildsec0 = ptr->handler_data[0];
471
472 for (s = file->the_bfd->sections; s != NULL; s = s->next)
473 {
474 const char *sname = bfd_get_section_name (file->the_bfd, s);
475 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
476
477 if (!skip)
478 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
479 }
480 }
481
482 static void
483 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
484 lang_input_statement_type *file,
485 callback_t callback,
486 void *data)
487 {
488 asection *s;
489 struct wildcard_list *sec0 = ptr->handler_data[0];
490 struct wildcard_list *wildsec1 = ptr->handler_data[1];
491 bfd_boolean multiple_sections_found;
492 asection *s0 = find_section (file, sec0, &multiple_sections_found);
493
494 if (multiple_sections_found)
495 {
496 walk_wild_section_general (ptr, file, callback, data);
497 return;
498 }
499
500 /* Note that if the section was not found, s0 is NULL and
501 we'll simply never succeed the s == s0 test below. */
502 for (s = file->the_bfd->sections; s != NULL; s = s->next)
503 {
504 /* Recall that in this code path, a section cannot satisfy more
505 than one spec, so if s == s0 then it cannot match
506 wildspec1. */
507 if (s == s0)
508 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
509 else
510 {
511 const char *sname = bfd_get_section_name (file->the_bfd, s);
512 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
513
514 if (!skip)
515 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
516 data);
517 }
518 }
519 }
520
521 static void
522 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
523 lang_input_statement_type *file,
524 callback_t callback,
525 void *data)
526 {
527 asection *s;
528 struct wildcard_list *sec0 = ptr->handler_data[0];
529 struct wildcard_list *wildsec1 = ptr->handler_data[1];
530 struct wildcard_list *wildsec2 = ptr->handler_data[2];
531 bfd_boolean multiple_sections_found;
532 asection *s0 = find_section (file, sec0, &multiple_sections_found);
533
534 if (multiple_sections_found)
535 {
536 walk_wild_section_general (ptr, file, callback, data);
537 return;
538 }
539
540 for (s = file->the_bfd->sections; s != NULL; s = s->next)
541 {
542 if (s == s0)
543 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
544 else
545 {
546 const char *sname = bfd_get_section_name (file->the_bfd, s);
547 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
548
549 if (!skip)
550 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
551 else
552 {
553 skip = !match_simple_wild (wildsec2->spec.name, sname);
554 if (!skip)
555 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
556 data);
557 }
558 }
559 }
560 }
561
562 static void
563 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
564 lang_input_statement_type *file,
565 callback_t callback,
566 void *data)
567 {
568 asection *s;
569 struct wildcard_list *sec0 = ptr->handler_data[0];
570 struct wildcard_list *sec1 = ptr->handler_data[1];
571 struct wildcard_list *wildsec2 = ptr->handler_data[2];
572 struct wildcard_list *wildsec3 = ptr->handler_data[3];
573 bfd_boolean multiple_sections_found;
574 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
575
576 if (multiple_sections_found)
577 {
578 walk_wild_section_general (ptr, file, callback, data);
579 return;
580 }
581
582 s1 = find_section (file, sec1, &multiple_sections_found);
583 if (multiple_sections_found)
584 {
585 walk_wild_section_general (ptr, file, callback, data);
586 return;
587 }
588
589 for (s = file->the_bfd->sections; s != NULL; s = s->next)
590 {
591 if (s == s0)
592 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
593 else
594 if (s == s1)
595 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
596 else
597 {
598 const char *sname = bfd_get_section_name (file->the_bfd, s);
599 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
600 sname);
601
602 if (!skip)
603 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
604 data);
605 else
606 {
607 skip = !match_simple_wild (wildsec3->spec.name, sname);
608 if (!skip)
609 walk_wild_consider_section (ptr, file, s, wildsec3,
610 callback, data);
611 }
612 }
613 }
614 }
615
616 static void
617 walk_wild_section (lang_wild_statement_type *ptr,
618 lang_input_statement_type *file,
619 callback_t callback,
620 void *data)
621 {
622 if (file->just_syms_flag)
623 return;
624
625 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
626 }
627
628 /* Returns TRUE when name1 is a wildcard spec that might match
629 something name2 can match. We're conservative: we return FALSE
630 only if the prefixes of name1 and name2 are different up to the
631 first wildcard character. */
632
633 static bfd_boolean
634 wild_spec_can_overlap (const char *name1, const char *name2)
635 {
636 size_t prefix1_len = strcspn (name1, "?*[");
637 size_t prefix2_len = strcspn (name2, "?*[");
638 size_t min_prefix_len;
639
640 /* Note that if there is no wildcard character, then we treat the
641 terminating 0 as part of the prefix. Thus ".text" won't match
642 ".text." or ".text.*", for example. */
643 if (name1[prefix1_len] == '\0')
644 prefix1_len++;
645 if (name2[prefix2_len] == '\0')
646 prefix2_len++;
647
648 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
649
650 return memcmp (name1, name2, min_prefix_len) == 0;
651 }
652
653 /* Select specialized code to handle various kinds of wildcard
654 statements. */
655
656 static void
657 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
658 {
659 int sec_count = 0;
660 int wild_name_count = 0;
661 struct wildcard_list *sec;
662 int signature;
663 int data_counter;
664
665 ptr->walk_wild_section_handler = walk_wild_section_general;
666 ptr->handler_data[0] = NULL;
667 ptr->handler_data[1] = NULL;
668 ptr->handler_data[2] = NULL;
669 ptr->handler_data[3] = NULL;
670 ptr->tree = NULL;
671
672 /* Count how many wildcard_specs there are, and how many of those
673 actually use wildcards in the name. Also, bail out if any of the
674 wildcard names are NULL. (Can this actually happen?
675 walk_wild_section used to test for it.) And bail out if any
676 of the wildcards are more complex than a simple string
677 ending in a single '*'. */
678 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
679 {
680 ++sec_count;
681 if (sec->spec.name == NULL)
682 return;
683 if (wildcardp (sec->spec.name))
684 {
685 ++wild_name_count;
686 if (!is_simple_wild (sec->spec.name))
687 return;
688 }
689 }
690
691 /* The zero-spec case would be easy to optimize but it doesn't
692 happen in practice. Likewise, more than 4 specs doesn't
693 happen in practice. */
694 if (sec_count == 0 || sec_count > 4)
695 return;
696
697 /* Check that no two specs can match the same section. */
698 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
699 {
700 struct wildcard_list *sec2;
701 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
702 {
703 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
704 return;
705 }
706 }
707
708 signature = (sec_count << 8) + wild_name_count;
709 switch (signature)
710 {
711 case 0x0100:
712 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
713 break;
714 case 0x0101:
715 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
716 break;
717 case 0x0201:
718 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
719 break;
720 case 0x0302:
721 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
722 break;
723 case 0x0402:
724 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
725 break;
726 default:
727 return;
728 }
729
730 /* Now fill the data array with pointers to the specs, first the
731 specs with non-wildcard names, then the specs with wildcard
732 names. It's OK to process the specs in different order from the
733 given order, because we've already determined that no section
734 will match more than one spec. */
735 data_counter = 0;
736 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
737 if (!wildcardp (sec->spec.name))
738 ptr->handler_data[data_counter++] = sec;
739 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
740 if (wildcardp (sec->spec.name))
741 ptr->handler_data[data_counter++] = sec;
742 }
743
744 /* Handle a wild statement for a single file F. */
745
746 static void
747 walk_wild_file (lang_wild_statement_type *s,
748 lang_input_statement_type *f,
749 callback_t callback,
750 void *data)
751 {
752 if (f->the_bfd == NULL
753 || ! bfd_check_format (f->the_bfd, bfd_archive))
754 walk_wild_section (s, f, callback, data);
755 else
756 {
757 bfd *member;
758
759 /* This is an archive file. We must map each member of the
760 archive separately. */
761 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
762 while (member != NULL)
763 {
764 /* When lookup_name is called, it will call the add_symbols
765 entry point for the archive. For each element of the
766 archive which is included, BFD will call ldlang_add_file,
767 which will set the usrdata field of the member to the
768 lang_input_statement. */
769 if (member->usrdata != NULL)
770 {
771 walk_wild_section (s, member->usrdata, callback, data);
772 }
773
774 member = bfd_openr_next_archived_file (f->the_bfd, member);
775 }
776 }
777 }
778
779 static void
780 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
781 {
782 const char *file_spec = s->filename;
783
784 if (file_spec == NULL)
785 {
786 /* Perform the iteration over all files in the list. */
787 LANG_FOR_EACH_INPUT_STATEMENT (f)
788 {
789 walk_wild_file (s, f, callback, data);
790 }
791 }
792 else if (wildcardp (file_spec))
793 {
794 LANG_FOR_EACH_INPUT_STATEMENT (f)
795 {
796 if (fnmatch (file_spec, f->filename, 0) == 0)
797 walk_wild_file (s, f, callback, data);
798 }
799 }
800 else
801 {
802 lang_input_statement_type *f;
803
804 /* Perform the iteration over a single file. */
805 f = lookup_name (file_spec);
806 if (f)
807 walk_wild_file (s, f, callback, data);
808 }
809 }
810
811 /* lang_for_each_statement walks the parse tree and calls the provided
812 function for each node. */
813
814 static void
815 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
816 lang_statement_union_type *s)
817 {
818 for (; s != NULL; s = s->header.next)
819 {
820 func (s);
821
822 switch (s->header.type)
823 {
824 case lang_constructors_statement_enum:
825 lang_for_each_statement_worker (func, constructor_list.head);
826 break;
827 case lang_output_section_statement_enum:
828 lang_for_each_statement_worker
829 (func, s->output_section_statement.children.head);
830 break;
831 case lang_wild_statement_enum:
832 lang_for_each_statement_worker (func,
833 s->wild_statement.children.head);
834 break;
835 case lang_group_statement_enum:
836 lang_for_each_statement_worker (func,
837 s->group_statement.children.head);
838 break;
839 case lang_data_statement_enum:
840 case lang_reloc_statement_enum:
841 case lang_object_symbols_statement_enum:
842 case lang_output_statement_enum:
843 case lang_target_statement_enum:
844 case lang_input_section_enum:
845 case lang_input_statement_enum:
846 case lang_assignment_statement_enum:
847 case lang_padding_statement_enum:
848 case lang_address_statement_enum:
849 case lang_fill_statement_enum:
850 case lang_insert_statement_enum:
851 break;
852 default:
853 FAIL ();
854 break;
855 }
856 }
857 }
858
859 void
860 lang_for_each_statement (void (*func) (lang_statement_union_type *))
861 {
862 lang_for_each_statement_worker (func, statement_list.head);
863 }
864
865 /*----------------------------------------------------------------------*/
866
867 void
868 lang_list_init (lang_statement_list_type *list)
869 {
870 list->head = NULL;
871 list->tail = &list->head;
872 }
873
874 /* Build a new statement node for the parse tree. */
875
876 static lang_statement_union_type *
877 new_statement (enum statement_enum type,
878 size_t size,
879 lang_statement_list_type *list)
880 {
881 lang_statement_union_type *new;
882
883 new = stat_alloc (size);
884 new->header.type = type;
885 new->header.next = NULL;
886 lang_statement_append (list, new, &new->header.next);
887 return new;
888 }
889
890 /* Build a new input file node for the language. There are several
891 ways in which we treat an input file, eg, we only look at symbols,
892 or prefix it with a -l etc.
893
894 We can be supplied with requests for input files more than once;
895 they may, for example be split over several lines like foo.o(.text)
896 foo.o(.data) etc, so when asked for a file we check that we haven't
897 got it already so we don't duplicate the bfd. */
898
899 static lang_input_statement_type *
900 new_afile (const char *name,
901 lang_input_file_enum_type file_type,
902 const char *target,
903 bfd_boolean add_to_list)
904 {
905 lang_input_statement_type *p;
906
907 if (add_to_list)
908 p = new_stat (lang_input_statement, stat_ptr);
909 else
910 {
911 p = stat_alloc (sizeof (lang_input_statement_type));
912 p->header.type = lang_input_statement_enum;
913 p->header.next = NULL;
914 }
915
916 lang_has_input_file = TRUE;
917 p->target = target;
918 p->sysrooted = FALSE;
919
920 if (file_type == lang_input_file_is_l_enum
921 && name[0] == ':' && name[1] != '\0')
922 {
923 file_type = lang_input_file_is_search_file_enum;
924 name = name + 1;
925 }
926
927 switch (file_type)
928 {
929 case lang_input_file_is_symbols_only_enum:
930 p->filename = name;
931 p->is_archive = FALSE;
932 p->real = TRUE;
933 p->local_sym_name = name;
934 p->just_syms_flag = TRUE;
935 p->search_dirs_flag = FALSE;
936 break;
937 case lang_input_file_is_fake_enum:
938 p->filename = name;
939 p->is_archive = FALSE;
940 p->real = FALSE;
941 p->local_sym_name = name;
942 p->just_syms_flag = FALSE;
943 p->search_dirs_flag = FALSE;
944 break;
945 case lang_input_file_is_l_enum:
946 p->is_archive = TRUE;
947 p->filename = name;
948 p->real = TRUE;
949 p->local_sym_name = concat ("-l", name, (const char *) NULL);
950 p->just_syms_flag = FALSE;
951 p->search_dirs_flag = TRUE;
952 break;
953 case lang_input_file_is_marker_enum:
954 p->filename = name;
955 p->is_archive = FALSE;
956 p->real = FALSE;
957 p->local_sym_name = name;
958 p->just_syms_flag = FALSE;
959 p->search_dirs_flag = TRUE;
960 break;
961 case lang_input_file_is_search_file_enum:
962 p->sysrooted = ldlang_sysrooted_script;
963 p->filename = name;
964 p->is_archive = FALSE;
965 p->real = TRUE;
966 p->local_sym_name = name;
967 p->just_syms_flag = FALSE;
968 p->search_dirs_flag = TRUE;
969 break;
970 case lang_input_file_is_file_enum:
971 p->filename = name;
972 p->is_archive = FALSE;
973 p->real = TRUE;
974 p->local_sym_name = name;
975 p->just_syms_flag = FALSE;
976 p->search_dirs_flag = FALSE;
977 break;
978 default:
979 FAIL ();
980 }
981 p->the_bfd = NULL;
982 p->asymbols = NULL;
983 p->next_real_file = NULL;
984 p->next = NULL;
985 p->symbol_count = 0;
986 p->dynamic = config.dynamic_link;
987 p->add_needed = add_needed;
988 p->as_needed = as_needed;
989 p->whole_archive = whole_archive;
990 p->loaded = FALSE;
991 lang_statement_append (&input_file_chain,
992 (lang_statement_union_type *) p,
993 &p->next_real_file);
994 return p;
995 }
996
997 lang_input_statement_type *
998 lang_add_input_file (const char *name,
999 lang_input_file_enum_type file_type,
1000 const char *target)
1001 {
1002 return new_afile (name, file_type, target, TRUE);
1003 }
1004
1005 struct out_section_hash_entry
1006 {
1007 struct bfd_hash_entry root;
1008 lang_statement_union_type s;
1009 };
1010
1011 /* The hash table. */
1012
1013 static struct bfd_hash_table output_section_statement_table;
1014
1015 /* Support routines for the hash table used by lang_output_section_find,
1016 initialize the table, fill in an entry and remove the table. */
1017
1018 static struct bfd_hash_entry *
1019 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1020 struct bfd_hash_table *table,
1021 const char *string)
1022 {
1023 lang_output_section_statement_type **nextp;
1024 struct out_section_hash_entry *ret;
1025
1026 if (entry == NULL)
1027 {
1028 entry = bfd_hash_allocate (table, sizeof (*ret));
1029 if (entry == NULL)
1030 return entry;
1031 }
1032
1033 entry = bfd_hash_newfunc (entry, table, string);
1034 if (entry == NULL)
1035 return entry;
1036
1037 ret = (struct out_section_hash_entry *) entry;
1038 memset (&ret->s, 0, sizeof (ret->s));
1039 ret->s.header.type = lang_output_section_statement_enum;
1040 ret->s.output_section_statement.subsection_alignment = -1;
1041 ret->s.output_section_statement.section_alignment = -1;
1042 ret->s.output_section_statement.block_value = 1;
1043 lang_list_init (&ret->s.output_section_statement.children);
1044 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1045
1046 /* For every output section statement added to the list, except the
1047 first one, lang_output_section_statement.tail points to the "next"
1048 field of the last element of the list. */
1049 if (lang_output_section_statement.head != NULL)
1050 ret->s.output_section_statement.prev
1051 = ((lang_output_section_statement_type *)
1052 ((char *) lang_output_section_statement.tail
1053 - offsetof (lang_output_section_statement_type, next)));
1054
1055 /* GCC's strict aliasing rules prevent us from just casting the
1056 address, so we store the pointer in a variable and cast that
1057 instead. */
1058 nextp = &ret->s.output_section_statement.next;
1059 lang_statement_append (&lang_output_section_statement,
1060 &ret->s,
1061 (lang_statement_union_type **) nextp);
1062 return &ret->root;
1063 }
1064
1065 static void
1066 output_section_statement_table_init (void)
1067 {
1068 if (!bfd_hash_table_init_n (&output_section_statement_table,
1069 output_section_statement_newfunc,
1070 sizeof (struct out_section_hash_entry),
1071 61))
1072 einfo (_("%P%F: can not create hash table: %E\n"));
1073 }
1074
1075 static void
1076 output_section_statement_table_free (void)
1077 {
1078 bfd_hash_table_free (&output_section_statement_table);
1079 }
1080
1081 /* Build enough state so that the parser can build its tree. */
1082
1083 void
1084 lang_init (void)
1085 {
1086 obstack_begin (&stat_obstack, 1000);
1087
1088 stat_ptr = &statement_list;
1089
1090 output_section_statement_table_init ();
1091
1092 lang_list_init (stat_ptr);
1093
1094 lang_list_init (&input_file_chain);
1095 lang_list_init (&lang_output_section_statement);
1096 lang_list_init (&file_chain);
1097 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1098 NULL);
1099 abs_output_section =
1100 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
1101
1102 abs_output_section->bfd_section = bfd_abs_section_ptr;
1103
1104 /* The value "3" is ad-hoc, somewhat related to the expected number of
1105 DEFINED expressions in a linker script. For most default linker
1106 scripts, there are none. Why a hash table then? Well, it's somewhat
1107 simpler to re-use working machinery than using a linked list in terms
1108 of code-complexity here in ld, besides the initialization which just
1109 looks like other code here. */
1110 if (!bfd_hash_table_init_n (&lang_definedness_table,
1111 lang_definedness_newfunc,
1112 sizeof (struct lang_definedness_hash_entry),
1113 3))
1114 einfo (_("%P%F: can not create hash table: %E\n"));
1115 }
1116
1117 void
1118 lang_finish (void)
1119 {
1120 output_section_statement_table_free ();
1121 }
1122
1123 /*----------------------------------------------------------------------
1124 A region is an area of memory declared with the
1125 MEMORY { name:org=exp, len=exp ... }
1126 syntax.
1127
1128 We maintain a list of all the regions here.
1129
1130 If no regions are specified in the script, then the default is used
1131 which is created when looked up to be the entire data space.
1132
1133 If create is true we are creating a region inside a MEMORY block.
1134 In this case it is probably an error to create a region that has
1135 already been created. If we are not inside a MEMORY block it is
1136 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1137 and so we issue a warning. */
1138
1139 static lang_memory_region_type *lang_memory_region_list;
1140 static lang_memory_region_type **lang_memory_region_list_tail
1141 = &lang_memory_region_list;
1142
1143 lang_memory_region_type *
1144 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1145 {
1146 lang_memory_region_type *p;
1147 lang_memory_region_type *new;
1148
1149 /* NAME is NULL for LMA memspecs if no region was specified. */
1150 if (name == NULL)
1151 return NULL;
1152
1153 for (p = lang_memory_region_list; p != NULL; p = p->next)
1154 if (strcmp (p->name, name) == 0)
1155 {
1156 if (create)
1157 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1158 name);
1159 return p;
1160 }
1161
1162 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1163 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
1164
1165 new = stat_alloc (sizeof (lang_memory_region_type));
1166
1167 new->name = xstrdup (name);
1168 new->next = NULL;
1169 new->origin = 0;
1170 new->length = ~(bfd_size_type) 0;
1171 new->current = 0;
1172 new->last_os = NULL;
1173 new->flags = 0;
1174 new->not_flags = 0;
1175 new->had_full_message = FALSE;
1176
1177 *lang_memory_region_list_tail = new;
1178 lang_memory_region_list_tail = &new->next;
1179
1180 return new;
1181 }
1182
1183 static lang_memory_region_type *
1184 lang_memory_default (asection *section)
1185 {
1186 lang_memory_region_type *p;
1187
1188 flagword sec_flags = section->flags;
1189
1190 /* Override SEC_DATA to mean a writable section. */
1191 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1192 sec_flags |= SEC_DATA;
1193
1194 for (p = lang_memory_region_list; p != NULL; p = p->next)
1195 {
1196 if ((p->flags & sec_flags) != 0
1197 && (p->not_flags & sec_flags) == 0)
1198 {
1199 return p;
1200 }
1201 }
1202 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1203 }
1204
1205 lang_output_section_statement_type *
1206 lang_output_section_find (const char *const name)
1207 {
1208 struct out_section_hash_entry *entry;
1209 unsigned long hash;
1210
1211 entry = ((struct out_section_hash_entry *)
1212 bfd_hash_lookup (&output_section_statement_table, name,
1213 FALSE, FALSE));
1214 if (entry == NULL)
1215 return NULL;
1216
1217 hash = entry->root.hash;
1218 do
1219 {
1220 if (entry->s.output_section_statement.constraint != -1)
1221 return &entry->s.output_section_statement;
1222 entry = (struct out_section_hash_entry *) entry->root.next;
1223 }
1224 while (entry != NULL
1225 && entry->root.hash == hash
1226 && strcmp (name, entry->s.output_section_statement.name) == 0);
1227
1228 return NULL;
1229 }
1230
1231 static lang_output_section_statement_type *
1232 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
1233 {
1234 struct out_section_hash_entry *entry;
1235 struct out_section_hash_entry *last_ent;
1236 unsigned long hash;
1237
1238 entry = ((struct out_section_hash_entry *)
1239 bfd_hash_lookup (&output_section_statement_table, name,
1240 TRUE, FALSE));
1241 if (entry == NULL)
1242 {
1243 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1244 return NULL;
1245 }
1246
1247 if (entry->s.output_section_statement.name != NULL)
1248 {
1249 /* We have a section of this name, but it might not have the correct
1250 constraint. */
1251 hash = entry->root.hash;
1252 do
1253 {
1254 if (entry->s.output_section_statement.constraint != -1
1255 && (constraint == 0
1256 || (constraint == entry->s.output_section_statement.constraint
1257 && constraint != SPECIAL)))
1258 return &entry->s.output_section_statement;
1259 last_ent = entry;
1260 entry = (struct out_section_hash_entry *) entry->root.next;
1261 }
1262 while (entry != NULL
1263 && entry->root.hash == hash
1264 && strcmp (name, entry->s.output_section_statement.name) == 0);
1265
1266 entry
1267 = ((struct out_section_hash_entry *)
1268 output_section_statement_newfunc (NULL,
1269 &output_section_statement_table,
1270 name));
1271 if (entry == NULL)
1272 {
1273 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1274 return NULL;
1275 }
1276 entry->root = last_ent->root;
1277 last_ent->root.next = &entry->root;
1278 }
1279
1280 entry->s.output_section_statement.name = name;
1281 entry->s.output_section_statement.constraint = constraint;
1282 return &entry->s.output_section_statement;
1283 }
1284
1285 lang_output_section_statement_type *
1286 lang_output_section_statement_lookup (const char *const name)
1287 {
1288 return lang_output_section_statement_lookup_1 (name, 0);
1289 }
1290
1291 /* A variant of lang_output_section_find used by place_orphan.
1292 Returns the output statement that should precede a new output
1293 statement for SEC. If an exact match is found on certain flags,
1294 sets *EXACT too. */
1295
1296 lang_output_section_statement_type *
1297 lang_output_section_find_by_flags (const asection *sec,
1298 lang_output_section_statement_type **exact,
1299 lang_match_sec_type_func match_type)
1300 {
1301 lang_output_section_statement_type *first, *look, *found;
1302 flagword flags;
1303
1304 /* We know the first statement on this list is *ABS*. May as well
1305 skip it. */
1306 first = &lang_output_section_statement.head->output_section_statement;
1307 first = first->next;
1308
1309 /* First try for an exact match. */
1310 found = NULL;
1311 for (look = first; look; look = look->next)
1312 {
1313 flags = look->flags;
1314 if (look->bfd_section != NULL)
1315 {
1316 flags = look->bfd_section->flags;
1317 if (match_type && !match_type (link_info.output_bfd,
1318 look->bfd_section,
1319 sec->owner, sec))
1320 continue;
1321 }
1322 flags ^= sec->flags;
1323 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1324 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1325 found = look;
1326 }
1327 if (found != NULL)
1328 {
1329 if (exact != NULL)
1330 *exact = found;
1331 return found;
1332 }
1333
1334 if (sec->flags & SEC_CODE)
1335 {
1336 /* Try for a rw code section. */
1337 for (look = first; look; look = look->next)
1338 {
1339 flags = look->flags;
1340 if (look->bfd_section != NULL)
1341 {
1342 flags = look->bfd_section->flags;
1343 if (match_type && !match_type (link_info.output_bfd,
1344 look->bfd_section,
1345 sec->owner, sec))
1346 continue;
1347 }
1348 flags ^= sec->flags;
1349 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1350 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1351 found = look;
1352 }
1353 }
1354 else if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
1355 {
1356 /* .rodata can go after .text, .sdata2 after .rodata. */
1357 for (look = first; look; look = look->next)
1358 {
1359 flags = look->flags;
1360 if (look->bfd_section != NULL)
1361 {
1362 flags = look->bfd_section->flags;
1363 if (match_type && !match_type (link_info.output_bfd,
1364 look->bfd_section,
1365 sec->owner, sec))
1366 continue;
1367 }
1368 flags ^= sec->flags;
1369 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1370 | SEC_READONLY))
1371 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1372 found = look;
1373 }
1374 }
1375 else if (sec->flags & SEC_SMALL_DATA)
1376 {
1377 /* .sdata goes after .data, .sbss after .sdata. */
1378 for (look = first; look; look = look->next)
1379 {
1380 flags = look->flags;
1381 if (look->bfd_section != NULL)
1382 {
1383 flags = look->bfd_section->flags;
1384 if (match_type && !match_type (link_info.output_bfd,
1385 look->bfd_section,
1386 sec->owner, sec))
1387 continue;
1388 }
1389 flags ^= sec->flags;
1390 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1391 | SEC_THREAD_LOCAL))
1392 || ((look->flags & SEC_SMALL_DATA)
1393 && !(sec->flags & SEC_HAS_CONTENTS)))
1394 found = look;
1395 }
1396 }
1397 else if (sec->flags & SEC_HAS_CONTENTS)
1398 {
1399 /* .data goes after .rodata. */
1400 for (look = first; look; look = look->next)
1401 {
1402 flags = look->flags;
1403 if (look->bfd_section != NULL)
1404 {
1405 flags = look->bfd_section->flags;
1406 if (match_type && !match_type (link_info.output_bfd,
1407 look->bfd_section,
1408 sec->owner, sec))
1409 continue;
1410 }
1411 flags ^= sec->flags;
1412 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1413 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1414 found = look;
1415 }
1416 }
1417 else
1418 {
1419 /* .bss goes last. */
1420 for (look = first; look; look = look->next)
1421 {
1422 flags = look->flags;
1423 if (look->bfd_section != NULL)
1424 {
1425 flags = look->bfd_section->flags;
1426 if (match_type && !match_type (link_info.output_bfd,
1427 look->bfd_section,
1428 sec->owner, sec))
1429 continue;
1430 }
1431 flags ^= sec->flags;
1432 if (!(flags & SEC_ALLOC))
1433 found = look;
1434 }
1435 }
1436
1437 if (found || !match_type)
1438 return found;
1439
1440 return lang_output_section_find_by_flags (sec, NULL, NULL);
1441 }
1442
1443 /* Find the last output section before given output statement.
1444 Used by place_orphan. */
1445
1446 static asection *
1447 output_prev_sec_find (lang_output_section_statement_type *os)
1448 {
1449 lang_output_section_statement_type *lookup;
1450
1451 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1452 {
1453 if (lookup->constraint == -1)
1454 continue;
1455
1456 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1457 return lookup->bfd_section;
1458 }
1459
1460 return NULL;
1461 }
1462
1463 /* Look for a suitable place for a new output section statement. The
1464 idea is to skip over anything that might be inside a SECTIONS {}
1465 statement in a script, before we find another output section
1466 statement. Assignments to "dot" before an output section statement
1467 are assumed to belong to it. An exception to this rule is made for
1468 the first assignment to dot, otherwise we might put an orphan
1469 before . = . + SIZEOF_HEADERS or similar assignments that set the
1470 initial address. */
1471
1472 static lang_statement_union_type **
1473 insert_os_after (lang_output_section_statement_type *after)
1474 {
1475 lang_statement_union_type **where;
1476 lang_statement_union_type **assign = NULL;
1477 bfd_boolean ignore_first;
1478
1479 ignore_first
1480 = after == &lang_output_section_statement.head->output_section_statement;
1481
1482 for (where = &after->header.next;
1483 *where != NULL;
1484 where = &(*where)->header.next)
1485 {
1486 switch ((*where)->header.type)
1487 {
1488 case lang_assignment_statement_enum:
1489 if (assign == NULL)
1490 {
1491 lang_assignment_statement_type *ass;
1492
1493 ass = &(*where)->assignment_statement;
1494 if (ass->exp->type.node_class != etree_assert
1495 && ass->exp->assign.dst[0] == '.'
1496 && ass->exp->assign.dst[1] == 0
1497 && !ignore_first)
1498 assign = where;
1499 }
1500 ignore_first = FALSE;
1501 continue;
1502 case lang_wild_statement_enum:
1503 case lang_input_section_enum:
1504 case lang_object_symbols_statement_enum:
1505 case lang_fill_statement_enum:
1506 case lang_data_statement_enum:
1507 case lang_reloc_statement_enum:
1508 case lang_padding_statement_enum:
1509 case lang_constructors_statement_enum:
1510 assign = NULL;
1511 continue;
1512 case lang_output_section_statement_enum:
1513 if (assign != NULL)
1514 where = assign;
1515 break;
1516 case lang_input_statement_enum:
1517 case lang_address_statement_enum:
1518 case lang_target_statement_enum:
1519 case lang_output_statement_enum:
1520 case lang_group_statement_enum:
1521 case lang_insert_statement_enum:
1522 continue;
1523 }
1524 break;
1525 }
1526
1527 return where;
1528 }
1529
1530 lang_output_section_statement_type *
1531 lang_insert_orphan (asection *s,
1532 const char *secname,
1533 lang_output_section_statement_type *after,
1534 struct orphan_save *place,
1535 etree_type *address,
1536 lang_statement_list_type *add_child)
1537 {
1538 lang_statement_list_type *old;
1539 lang_statement_list_type add;
1540 const char *ps;
1541 lang_output_section_statement_type *os;
1542 lang_output_section_statement_type **os_tail;
1543
1544 /* Start building a list of statements for this section.
1545 First save the current statement pointer. */
1546 old = stat_ptr;
1547
1548 /* If we have found an appropriate place for the output section
1549 statements for this orphan, add them to our own private list,
1550 inserting them later into the global statement list. */
1551 if (after != NULL)
1552 {
1553 stat_ptr = &add;
1554 lang_list_init (stat_ptr);
1555 }
1556
1557 ps = NULL;
1558 if (config.build_constructors)
1559 {
1560 /* If the name of the section is representable in C, then create
1561 symbols to mark the start and the end of the section. */
1562 for (ps = secname; *ps != '\0'; ps++)
1563 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1564 break;
1565 if (*ps == '\0')
1566 {
1567 char *symname;
1568 etree_type *e_align;
1569
1570 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1571 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1572 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1573 e_align = exp_unop (ALIGN_K,
1574 exp_intop ((bfd_vma) 1 << s->alignment_power));
1575 lang_add_assignment (exp_assop ('=', ".", e_align));
1576 lang_add_assignment (exp_provide (symname,
1577 exp_nameop (NAME, "."),
1578 FALSE));
1579 }
1580 }
1581
1582 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1583 address = exp_intop (0);
1584
1585 os_tail = ((lang_output_section_statement_type **)
1586 lang_output_section_statement.tail);
1587 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1588 NULL, 0);
1589
1590 if (add_child == NULL)
1591 add_child = &os->children;
1592 lang_add_section (add_child, s, os);
1593
1594 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1595
1596 if (config.build_constructors && *ps == '\0')
1597 {
1598 char *symname;
1599
1600 /* lang_leave_ouput_section_statement resets stat_ptr.
1601 Put stat_ptr back where we want it. */
1602 if (after != NULL)
1603 stat_ptr = &add;
1604
1605 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1606 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1607 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1608 lang_add_assignment (exp_provide (symname,
1609 exp_nameop (NAME, "."),
1610 FALSE));
1611 }
1612
1613 /* Restore the global list pointer. */
1614 if (after != NULL)
1615 stat_ptr = old;
1616
1617 if (after != NULL && os->bfd_section != NULL)
1618 {
1619 asection *snew, *as;
1620
1621 snew = os->bfd_section;
1622
1623 /* Shuffle the bfd section list to make the output file look
1624 neater. This is really only cosmetic. */
1625 if (place->section == NULL
1626 && after != (&lang_output_section_statement.head
1627 ->output_section_statement))
1628 {
1629 asection *bfd_section = after->bfd_section;
1630
1631 /* If the output statement hasn't been used to place any input
1632 sections (and thus doesn't have an output bfd_section),
1633 look for the closest prior output statement having an
1634 output section. */
1635 if (bfd_section == NULL)
1636 bfd_section = output_prev_sec_find (after);
1637
1638 if (bfd_section != NULL && bfd_section != snew)
1639 place->section = &bfd_section->next;
1640 }
1641
1642 if (place->section == NULL)
1643 place->section = &link_info.output_bfd->sections;
1644
1645 as = *place->section;
1646
1647 if (!as)
1648 {
1649 /* Put the section at the end of the list. */
1650
1651 /* Unlink the section. */
1652 bfd_section_list_remove (link_info.output_bfd, snew);
1653
1654 /* Now tack it back on in the right place. */
1655 bfd_section_list_append (link_info.output_bfd, snew);
1656 }
1657 else if (as != snew && as->prev != snew)
1658 {
1659 /* Unlink the section. */
1660 bfd_section_list_remove (link_info.output_bfd, snew);
1661
1662 /* Now tack it back on in the right place. */
1663 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
1664 }
1665
1666 /* Save the end of this list. Further ophans of this type will
1667 follow the one we've just added. */
1668 place->section = &snew->next;
1669
1670 /* The following is non-cosmetic. We try to put the output
1671 statements in some sort of reasonable order here, because they
1672 determine the final load addresses of the orphan sections.
1673 In addition, placing output statements in the wrong order may
1674 require extra segments. For instance, given a typical
1675 situation of all read-only sections placed in one segment and
1676 following that a segment containing all the read-write
1677 sections, we wouldn't want to place an orphan read/write
1678 section before or amongst the read-only ones. */
1679 if (add.head != NULL)
1680 {
1681 lang_output_section_statement_type *newly_added_os;
1682
1683 if (place->stmt == NULL)
1684 {
1685 lang_statement_union_type **where = insert_os_after (after);
1686
1687 *add.tail = *where;
1688 *where = add.head;
1689
1690 place->os_tail = &after->next;
1691 }
1692 else
1693 {
1694 /* Put it after the last orphan statement we added. */
1695 *add.tail = *place->stmt;
1696 *place->stmt = add.head;
1697 }
1698
1699 /* Fix the global list pointer if we happened to tack our
1700 new list at the tail. */
1701 if (*old->tail == add.head)
1702 old->tail = add.tail;
1703
1704 /* Save the end of this list. */
1705 place->stmt = add.tail;
1706
1707 /* Do the same for the list of output section statements. */
1708 newly_added_os = *os_tail;
1709 *os_tail = NULL;
1710 newly_added_os->prev = (lang_output_section_statement_type *)
1711 ((char *) place->os_tail
1712 - offsetof (lang_output_section_statement_type, next));
1713 newly_added_os->next = *place->os_tail;
1714 if (newly_added_os->next != NULL)
1715 newly_added_os->next->prev = newly_added_os;
1716 *place->os_tail = newly_added_os;
1717 place->os_tail = &newly_added_os->next;
1718
1719 /* Fixing the global list pointer here is a little different.
1720 We added to the list in lang_enter_output_section_statement,
1721 trimmed off the new output_section_statment above when
1722 assigning *os_tail = NULL, but possibly added it back in
1723 the same place when assigning *place->os_tail. */
1724 if (*os_tail == NULL)
1725 lang_output_section_statement.tail
1726 = (lang_statement_union_type **) os_tail;
1727 }
1728 }
1729 return os;
1730 }
1731
1732 static void
1733 lang_map_flags (flagword flag)
1734 {
1735 if (flag & SEC_ALLOC)
1736 minfo ("a");
1737
1738 if (flag & SEC_CODE)
1739 minfo ("x");
1740
1741 if (flag & SEC_READONLY)
1742 minfo ("r");
1743
1744 if (flag & SEC_DATA)
1745 minfo ("w");
1746
1747 if (flag & SEC_LOAD)
1748 minfo ("l");
1749 }
1750
1751 void
1752 lang_map (void)
1753 {
1754 lang_memory_region_type *m;
1755 bfd_boolean dis_header_printed = FALSE;
1756 bfd *p;
1757
1758 LANG_FOR_EACH_INPUT_STATEMENT (file)
1759 {
1760 asection *s;
1761
1762 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1763 || file->just_syms_flag)
1764 continue;
1765
1766 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1767 if ((s->output_section == NULL
1768 || s->output_section->owner != link_info.output_bfd)
1769 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
1770 {
1771 if (! dis_header_printed)
1772 {
1773 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1774 dis_header_printed = TRUE;
1775 }
1776
1777 print_input_section (s);
1778 }
1779 }
1780
1781 minfo (_("\nMemory Configuration\n\n"));
1782 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1783 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1784
1785 for (m = lang_memory_region_list; m != NULL; m = m->next)
1786 {
1787 char buf[100];
1788 int len;
1789
1790 fprintf (config.map_file, "%-16s ", m->name);
1791
1792 sprintf_vma (buf, m->origin);
1793 minfo ("0x%s ", buf);
1794 len = strlen (buf);
1795 while (len < 16)
1796 {
1797 print_space ();
1798 ++len;
1799 }
1800
1801 minfo ("0x%V", m->length);
1802 if (m->flags || m->not_flags)
1803 {
1804 #ifndef BFD64
1805 minfo (" ");
1806 #endif
1807 if (m->flags)
1808 {
1809 print_space ();
1810 lang_map_flags (m->flags);
1811 }
1812
1813 if (m->not_flags)
1814 {
1815 minfo (" !");
1816 lang_map_flags (m->not_flags);
1817 }
1818 }
1819
1820 print_nl ();
1821 }
1822
1823 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1824
1825 if (! link_info.reduce_memory_overheads)
1826 {
1827 obstack_begin (&map_obstack, 1000);
1828 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1829 bfd_map_over_sections (p, init_map_userdata, 0);
1830 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1831 }
1832 lang_statement_iteration ++;
1833 print_statements ();
1834 }
1835
1836 static void
1837 init_map_userdata (abfd, sec, data)
1838 bfd *abfd ATTRIBUTE_UNUSED;
1839 asection *sec;
1840 void *data ATTRIBUTE_UNUSED;
1841 {
1842 fat_section_userdata_type *new_data
1843 = ((fat_section_userdata_type *) (stat_alloc
1844 (sizeof (fat_section_userdata_type))));
1845
1846 ASSERT (get_userdata (sec) == NULL);
1847 get_userdata (sec) = new_data;
1848 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1849 }
1850
1851 static bfd_boolean
1852 sort_def_symbol (hash_entry, info)
1853 struct bfd_link_hash_entry *hash_entry;
1854 void *info ATTRIBUTE_UNUSED;
1855 {
1856 if (hash_entry->type == bfd_link_hash_defined
1857 || hash_entry->type == bfd_link_hash_defweak)
1858 {
1859 struct fat_user_section_struct *ud;
1860 struct map_symbol_def *def;
1861
1862 ud = get_userdata (hash_entry->u.def.section);
1863 if (! ud)
1864 {
1865 /* ??? What do we have to do to initialize this beforehand? */
1866 /* The first time we get here is bfd_abs_section... */
1867 init_map_userdata (0, hash_entry->u.def.section, 0);
1868 ud = get_userdata (hash_entry->u.def.section);
1869 }
1870 else if (!ud->map_symbol_def_tail)
1871 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1872
1873 def = obstack_alloc (&map_obstack, sizeof *def);
1874 def->entry = hash_entry;
1875 *(ud->map_symbol_def_tail) = def;
1876 ud->map_symbol_def_tail = &def->next;
1877 }
1878 return TRUE;
1879 }
1880
1881 /* Initialize an output section. */
1882
1883 static void
1884 init_os (lang_output_section_statement_type *s, asection *isec,
1885 flagword flags)
1886 {
1887 if (s->bfd_section != NULL)
1888 return;
1889
1890 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1891 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1892
1893 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
1894 if (s->bfd_section == NULL)
1895 s->bfd_section = bfd_make_section_with_flags (link_info.output_bfd,
1896 s->name, flags);
1897 if (s->bfd_section == NULL)
1898 {
1899 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1900 link_info.output_bfd->xvec->name, s->name);
1901 }
1902 s->bfd_section->output_section = s->bfd_section;
1903 s->bfd_section->output_offset = 0;
1904
1905 if (!link_info.reduce_memory_overheads)
1906 {
1907 fat_section_userdata_type *new
1908 = stat_alloc (sizeof (fat_section_userdata_type));
1909 memset (new, 0, sizeof (fat_section_userdata_type));
1910 get_userdata (s->bfd_section) = new;
1911 }
1912
1913 /* If there is a base address, make sure that any sections it might
1914 mention are initialized. */
1915 if (s->addr_tree != NULL)
1916 exp_init_os (s->addr_tree);
1917
1918 if (s->load_base != NULL)
1919 exp_init_os (s->load_base);
1920
1921 /* If supplied an alignment, set it. */
1922 if (s->section_alignment != -1)
1923 s->bfd_section->alignment_power = s->section_alignment;
1924
1925 if (isec)
1926 bfd_init_private_section_data (isec->owner, isec,
1927 link_info.output_bfd, s->bfd_section,
1928 &link_info);
1929 }
1930
1931 /* Make sure that all output sections mentioned in an expression are
1932 initialized. */
1933
1934 static void
1935 exp_init_os (etree_type *exp)
1936 {
1937 switch (exp->type.node_class)
1938 {
1939 case etree_assign:
1940 case etree_provide:
1941 exp_init_os (exp->assign.src);
1942 break;
1943
1944 case etree_binary:
1945 exp_init_os (exp->binary.lhs);
1946 exp_init_os (exp->binary.rhs);
1947 break;
1948
1949 case etree_trinary:
1950 exp_init_os (exp->trinary.cond);
1951 exp_init_os (exp->trinary.lhs);
1952 exp_init_os (exp->trinary.rhs);
1953 break;
1954
1955 case etree_assert:
1956 exp_init_os (exp->assert_s.child);
1957 break;
1958
1959 case etree_unary:
1960 exp_init_os (exp->unary.child);
1961 break;
1962
1963 case etree_name:
1964 switch (exp->type.node_code)
1965 {
1966 case ADDR:
1967 case LOADADDR:
1968 case SIZEOF:
1969 {
1970 lang_output_section_statement_type *os;
1971
1972 os = lang_output_section_find (exp->name.name);
1973 if (os != NULL && os->bfd_section == NULL)
1974 init_os (os, NULL, 0);
1975 }
1976 }
1977 break;
1978
1979 default:
1980 break;
1981 }
1982 }
1983 \f
1984 static void
1985 section_already_linked (bfd *abfd, asection *sec, void *data)
1986 {
1987 lang_input_statement_type *entry = data;
1988
1989 /* If we are only reading symbols from this object, then we want to
1990 discard all sections. */
1991 if (entry->just_syms_flag)
1992 {
1993 bfd_link_just_syms (abfd, sec, &link_info);
1994 return;
1995 }
1996
1997 if (!(abfd->flags & DYNAMIC))
1998 bfd_section_already_linked (abfd, sec, &link_info);
1999 }
2000 \f
2001 /* The wild routines.
2002
2003 These expand statements like *(.text) and foo.o to a list of
2004 explicit actions, like foo.o(.text), bar.o(.text) and
2005 foo.o(.text, .data). */
2006
2007 /* Add SECTION to the output section OUTPUT. Do this by creating a
2008 lang_input_section statement which is placed at PTR. FILE is the
2009 input file which holds SECTION. */
2010
2011 void
2012 lang_add_section (lang_statement_list_type *ptr,
2013 asection *section,
2014 lang_output_section_statement_type *output)
2015 {
2016 flagword flags = section->flags;
2017 bfd_boolean discard;
2018
2019 /* Discard sections marked with SEC_EXCLUDE. */
2020 discard = (flags & SEC_EXCLUDE) != 0;
2021
2022 /* Discard input sections which are assigned to a section named
2023 DISCARD_SECTION_NAME. */
2024 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2025 discard = TRUE;
2026
2027 /* Discard debugging sections if we are stripping debugging
2028 information. */
2029 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2030 && (flags & SEC_DEBUGGING) != 0)
2031 discard = TRUE;
2032
2033 if (discard)
2034 {
2035 if (section->output_section == NULL)
2036 {
2037 /* This prevents future calls from assigning this section. */
2038 section->output_section = bfd_abs_section_ptr;
2039 }
2040 return;
2041 }
2042
2043 if (section->output_section == NULL)
2044 {
2045 bfd_boolean first;
2046 lang_input_section_type *new;
2047 flagword flags;
2048
2049 flags = section->flags;
2050
2051 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2052 to an output section, because we want to be able to include a
2053 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2054 section (I don't know why we want to do this, but we do).
2055 build_link_order in ldwrite.c handles this case by turning
2056 the embedded SEC_NEVER_LOAD section into a fill. */
2057
2058 flags &= ~ SEC_NEVER_LOAD;
2059
2060 switch (output->sectype)
2061 {
2062 case normal_section:
2063 case overlay_section:
2064 break;
2065 case noalloc_section:
2066 flags &= ~SEC_ALLOC;
2067 break;
2068 case noload_section:
2069 flags &= ~SEC_LOAD;
2070 flags |= SEC_NEVER_LOAD;
2071 break;
2072 }
2073
2074 if (output->bfd_section == NULL)
2075 init_os (output, section, flags);
2076
2077 first = ! output->bfd_section->linker_has_input;
2078 output->bfd_section->linker_has_input = 1;
2079
2080 if (!link_info.relocatable
2081 && !stripped_excluded_sections)
2082 {
2083 asection *s = output->bfd_section->map_tail.s;
2084 output->bfd_section->map_tail.s = section;
2085 section->map_head.s = NULL;
2086 section->map_tail.s = s;
2087 if (s != NULL)
2088 s->map_head.s = section;
2089 else
2090 output->bfd_section->map_head.s = section;
2091 }
2092
2093 /* Add a section reference to the list. */
2094 new = new_stat (lang_input_section, ptr);
2095
2096 new->section = section;
2097 section->output_section = output->bfd_section;
2098
2099 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2100 already been processed. One reason to do this is that on pe
2101 format targets, .text$foo sections go into .text and it's odd
2102 to see .text with SEC_LINK_ONCE set. */
2103
2104 if (! link_info.relocatable)
2105 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2106
2107 /* If this is not the first input section, and the SEC_READONLY
2108 flag is not currently set, then don't set it just because the
2109 input section has it set. */
2110
2111 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2112 flags &= ~ SEC_READONLY;
2113
2114 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2115 if (! first
2116 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2117 != (flags & (SEC_MERGE | SEC_STRINGS))
2118 || ((flags & SEC_MERGE)
2119 && output->bfd_section->entsize != section->entsize)))
2120 {
2121 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2122 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2123 }
2124
2125 output->bfd_section->flags |= flags;
2126
2127 if (flags & SEC_MERGE)
2128 output->bfd_section->entsize = section->entsize;
2129
2130 /* If SEC_READONLY is not set in the input section, then clear
2131 it from the output section. */
2132 if ((section->flags & SEC_READONLY) == 0)
2133 output->bfd_section->flags &= ~SEC_READONLY;
2134
2135 /* Copy over SEC_SMALL_DATA. */
2136 if (section->flags & SEC_SMALL_DATA)
2137 output->bfd_section->flags |= SEC_SMALL_DATA;
2138
2139 if (section->alignment_power > output->bfd_section->alignment_power)
2140 output->bfd_section->alignment_power = section->alignment_power;
2141
2142 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2143 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2144 {
2145 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2146 /* FIXME: This value should really be obtained from the bfd... */
2147 output->block_value = 128;
2148 }
2149 }
2150 }
2151
2152 /* Handle wildcard sorting. This returns the lang_input_section which
2153 should follow the one we are going to create for SECTION and FILE,
2154 based on the sorting requirements of WILD. It returns NULL if the
2155 new section should just go at the end of the current list. */
2156
2157 static lang_statement_union_type *
2158 wild_sort (lang_wild_statement_type *wild,
2159 struct wildcard_list *sec,
2160 lang_input_statement_type *file,
2161 asection *section)
2162 {
2163 const char *section_name;
2164 lang_statement_union_type *l;
2165
2166 if (!wild->filenames_sorted
2167 && (sec == NULL || sec->spec.sorted == none))
2168 return NULL;
2169
2170 section_name = bfd_get_section_name (file->the_bfd, section);
2171 for (l = wild->children.head; l != NULL; l = l->header.next)
2172 {
2173 lang_input_section_type *ls;
2174
2175 if (l->header.type != lang_input_section_enum)
2176 continue;
2177 ls = &l->input_section;
2178
2179 /* Sorting by filename takes precedence over sorting by section
2180 name. */
2181
2182 if (wild->filenames_sorted)
2183 {
2184 const char *fn, *ln;
2185 bfd_boolean fa, la;
2186 int i;
2187
2188 /* The PE support for the .idata section as generated by
2189 dlltool assumes that files will be sorted by the name of
2190 the archive and then the name of the file within the
2191 archive. */
2192
2193 if (file->the_bfd != NULL
2194 && bfd_my_archive (file->the_bfd) != NULL)
2195 {
2196 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2197 fa = TRUE;
2198 }
2199 else
2200 {
2201 fn = file->filename;
2202 fa = FALSE;
2203 }
2204
2205 if (bfd_my_archive (ls->section->owner) != NULL)
2206 {
2207 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2208 la = TRUE;
2209 }
2210 else
2211 {
2212 ln = ls->section->owner->filename;
2213 la = FALSE;
2214 }
2215
2216 i = strcmp (fn, ln);
2217 if (i > 0)
2218 continue;
2219 else if (i < 0)
2220 break;
2221
2222 if (fa || la)
2223 {
2224 if (fa)
2225 fn = file->filename;
2226 if (la)
2227 ln = ls->section->owner->filename;
2228
2229 i = strcmp (fn, ln);
2230 if (i > 0)
2231 continue;
2232 else if (i < 0)
2233 break;
2234 }
2235 }
2236
2237 /* Here either the files are not sorted by name, or we are
2238 looking at the sections for this file. */
2239
2240 if (sec != NULL && sec->spec.sorted != none)
2241 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2242 break;
2243 }
2244
2245 return l;
2246 }
2247
2248 /* Expand a wild statement for a particular FILE. SECTION may be
2249 NULL, in which case it is a wild card. */
2250
2251 static void
2252 output_section_callback (lang_wild_statement_type *ptr,
2253 struct wildcard_list *sec,
2254 asection *section,
2255 lang_input_statement_type *file,
2256 void *output)
2257 {
2258 lang_statement_union_type *before;
2259
2260 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2261 if (unique_section_p (section))
2262 return;
2263
2264 before = wild_sort (ptr, sec, file, section);
2265
2266 /* Here BEFORE points to the lang_input_section which
2267 should follow the one we are about to add. If BEFORE
2268 is NULL, then the section should just go at the end
2269 of the current list. */
2270
2271 if (before == NULL)
2272 lang_add_section (&ptr->children, section,
2273 (lang_output_section_statement_type *) output);
2274 else
2275 {
2276 lang_statement_list_type list;
2277 lang_statement_union_type **pp;
2278
2279 lang_list_init (&list);
2280 lang_add_section (&list, section,
2281 (lang_output_section_statement_type *) output);
2282
2283 /* If we are discarding the section, LIST.HEAD will
2284 be NULL. */
2285 if (list.head != NULL)
2286 {
2287 ASSERT (list.head->header.next == NULL);
2288
2289 for (pp = &ptr->children.head;
2290 *pp != before;
2291 pp = &(*pp)->header.next)
2292 ASSERT (*pp != NULL);
2293
2294 list.head->header.next = *pp;
2295 *pp = list.head;
2296 }
2297 }
2298 }
2299
2300 /* Check if all sections in a wild statement for a particular FILE
2301 are readonly. */
2302
2303 static void
2304 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2305 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2306 asection *section,
2307 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2308 void *data)
2309 {
2310 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2311 if (unique_section_p (section))
2312 return;
2313
2314 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2315 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2316 }
2317
2318 /* This is passed a file name which must have been seen already and
2319 added to the statement tree. We will see if it has been opened
2320 already and had its symbols read. If not then we'll read it. */
2321
2322 static lang_input_statement_type *
2323 lookup_name (const char *name)
2324 {
2325 lang_input_statement_type *search;
2326
2327 for (search = (lang_input_statement_type *) input_file_chain.head;
2328 search != NULL;
2329 search = (lang_input_statement_type *) search->next_real_file)
2330 {
2331 /* Use the local_sym_name as the name of the file that has
2332 already been loaded as filename might have been transformed
2333 via the search directory lookup mechanism. */
2334 const char *filename = search->local_sym_name;
2335
2336 if (filename != NULL
2337 && strcmp (filename, name) == 0)
2338 break;
2339 }
2340
2341 if (search == NULL)
2342 search = new_afile (name, lang_input_file_is_search_file_enum,
2343 default_target, FALSE);
2344
2345 /* If we have already added this file, or this file is not real
2346 don't add this file. */
2347 if (search->loaded || !search->real)
2348 return search;
2349
2350 if (! load_symbols (search, NULL))
2351 return NULL;
2352
2353 return search;
2354 }
2355
2356 /* Save LIST as a list of libraries whose symbols should not be exported. */
2357
2358 struct excluded_lib
2359 {
2360 char *name;
2361 struct excluded_lib *next;
2362 };
2363 static struct excluded_lib *excluded_libs;
2364
2365 void
2366 add_excluded_libs (const char *list)
2367 {
2368 const char *p = list, *end;
2369
2370 while (*p != '\0')
2371 {
2372 struct excluded_lib *entry;
2373 end = strpbrk (p, ",:");
2374 if (end == NULL)
2375 end = p + strlen (p);
2376 entry = xmalloc (sizeof (*entry));
2377 entry->next = excluded_libs;
2378 entry->name = xmalloc (end - p + 1);
2379 memcpy (entry->name, p, end - p);
2380 entry->name[end - p] = '\0';
2381 excluded_libs = entry;
2382 if (*end == '\0')
2383 break;
2384 p = end + 1;
2385 }
2386 }
2387
2388 static void
2389 check_excluded_libs (bfd *abfd)
2390 {
2391 struct excluded_lib *lib = excluded_libs;
2392
2393 while (lib)
2394 {
2395 int len = strlen (lib->name);
2396 const char *filename = lbasename (abfd->filename);
2397
2398 if (strcmp (lib->name, "ALL") == 0)
2399 {
2400 abfd->no_export = TRUE;
2401 return;
2402 }
2403
2404 if (strncmp (lib->name, filename, len) == 0
2405 && (filename[len] == '\0'
2406 || (filename[len] == '.' && filename[len + 1] == 'a'
2407 && filename[len + 2] == '\0')))
2408 {
2409 abfd->no_export = TRUE;
2410 return;
2411 }
2412
2413 lib = lib->next;
2414 }
2415 }
2416
2417 /* Get the symbols for an input file. */
2418
2419 bfd_boolean
2420 load_symbols (lang_input_statement_type *entry,
2421 lang_statement_list_type *place)
2422 {
2423 char **matching;
2424
2425 if (entry->loaded)
2426 return TRUE;
2427
2428 ldfile_open_file (entry);
2429
2430 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2431 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2432 {
2433 bfd_error_type err;
2434 lang_statement_list_type *hold;
2435 bfd_boolean bad_load = TRUE;
2436 bfd_boolean save_ldlang_sysrooted_script;
2437 bfd_boolean save_as_needed, save_add_needed;
2438
2439 err = bfd_get_error ();
2440
2441 /* See if the emulation has some special knowledge. */
2442 if (ldemul_unrecognized_file (entry))
2443 return TRUE;
2444
2445 if (err == bfd_error_file_ambiguously_recognized)
2446 {
2447 char **p;
2448
2449 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2450 einfo (_("%B: matching formats:"), entry->the_bfd);
2451 for (p = matching; *p != NULL; p++)
2452 einfo (" %s", *p);
2453 einfo ("%F\n");
2454 }
2455 else if (err != bfd_error_file_not_recognized
2456 || place == NULL)
2457 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2458 else
2459 bad_load = FALSE;
2460
2461 bfd_close (entry->the_bfd);
2462 entry->the_bfd = NULL;
2463
2464 /* Try to interpret the file as a linker script. */
2465 ldfile_open_command_file (entry->filename);
2466
2467 hold = stat_ptr;
2468 stat_ptr = place;
2469 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2470 ldlang_sysrooted_script = entry->sysrooted;
2471 save_as_needed = as_needed;
2472 as_needed = entry->as_needed;
2473 save_add_needed = add_needed;
2474 add_needed = entry->add_needed;
2475
2476 ldfile_assumed_script = TRUE;
2477 parser_input = input_script;
2478 /* We want to use the same -Bdynamic/-Bstatic as the one for
2479 ENTRY. */
2480 config.dynamic_link = entry->dynamic;
2481 yyparse ();
2482 ldfile_assumed_script = FALSE;
2483
2484 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2485 as_needed = save_as_needed;
2486 add_needed = save_add_needed;
2487 stat_ptr = hold;
2488
2489 return ! bad_load;
2490 }
2491
2492 if (ldemul_recognized_file (entry))
2493 return TRUE;
2494
2495 /* We don't call ldlang_add_file for an archive. Instead, the
2496 add_symbols entry point will call ldlang_add_file, via the
2497 add_archive_element callback, for each element of the archive
2498 which is used. */
2499 switch (bfd_get_format (entry->the_bfd))
2500 {
2501 default:
2502 break;
2503
2504 case bfd_object:
2505 ldlang_add_file (entry);
2506 if (trace_files || trace_file_tries)
2507 info_msg ("%I\n", entry);
2508 break;
2509
2510 case bfd_archive:
2511 check_excluded_libs (entry->the_bfd);
2512
2513 if (entry->whole_archive)
2514 {
2515 bfd *member = NULL;
2516 bfd_boolean loaded = TRUE;
2517
2518 for (;;)
2519 {
2520 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2521
2522 if (member == NULL)
2523 break;
2524
2525 if (! bfd_check_format (member, bfd_object))
2526 {
2527 einfo (_("%F%B: member %B in archive is not an object\n"),
2528 entry->the_bfd, member);
2529 loaded = FALSE;
2530 }
2531
2532 if (! ((*link_info.callbacks->add_archive_element)
2533 (&link_info, member, "--whole-archive")))
2534 abort ();
2535
2536 if (! bfd_link_add_symbols (member, &link_info))
2537 {
2538 einfo (_("%F%B: could not read symbols: %E\n"), member);
2539 loaded = FALSE;
2540 }
2541 }
2542
2543 entry->loaded = loaded;
2544 return loaded;
2545 }
2546 break;
2547 }
2548
2549 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2550 entry->loaded = TRUE;
2551 else
2552 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2553
2554 return entry->loaded;
2555 }
2556
2557 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2558 may be NULL, indicating that it is a wildcard. Separate
2559 lang_input_section statements are created for each part of the
2560 expansion; they are added after the wild statement S. OUTPUT is
2561 the output section. */
2562
2563 static void
2564 wild (lang_wild_statement_type *s,
2565 const char *target ATTRIBUTE_UNUSED,
2566 lang_output_section_statement_type *output)
2567 {
2568 struct wildcard_list *sec;
2569
2570 if (s->handler_data[0]
2571 && s->handler_data[0]->spec.sorted == by_name
2572 && !s->filenames_sorted)
2573 {
2574 lang_section_bst_type *tree;
2575
2576 walk_wild (s, output_section_callback_fast, output);
2577
2578 tree = s->tree;
2579 if (tree)
2580 {
2581 output_section_callback_tree_to_list (s, tree, output);
2582 s->tree = NULL;
2583 }
2584 }
2585 else
2586 walk_wild (s, output_section_callback, output);
2587
2588 if (default_common_section == NULL)
2589 for (sec = s->section_list; sec != NULL; sec = sec->next)
2590 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2591 {
2592 /* Remember the section that common is going to in case we
2593 later get something which doesn't know where to put it. */
2594 default_common_section = output;
2595 break;
2596 }
2597 }
2598
2599 /* Return TRUE iff target is the sought target. */
2600
2601 static int
2602 get_target (const bfd_target *target, void *data)
2603 {
2604 const char *sought = data;
2605
2606 return strcmp (target->name, sought) == 0;
2607 }
2608
2609 /* Like strcpy() but convert to lower case as well. */
2610
2611 static void
2612 stricpy (char *dest, char *src)
2613 {
2614 char c;
2615
2616 while ((c = *src++) != 0)
2617 *dest++ = TOLOWER (c);
2618
2619 *dest = 0;
2620 }
2621
2622 /* Remove the first occurrence of needle (if any) in haystack
2623 from haystack. */
2624
2625 static void
2626 strcut (char *haystack, char *needle)
2627 {
2628 haystack = strstr (haystack, needle);
2629
2630 if (haystack)
2631 {
2632 char *src;
2633
2634 for (src = haystack + strlen (needle); *src;)
2635 *haystack++ = *src++;
2636
2637 *haystack = 0;
2638 }
2639 }
2640
2641 /* Compare two target format name strings.
2642 Return a value indicating how "similar" they are. */
2643
2644 static int
2645 name_compare (char *first, char *second)
2646 {
2647 char *copy1;
2648 char *copy2;
2649 int result;
2650
2651 copy1 = xmalloc (strlen (first) + 1);
2652 copy2 = xmalloc (strlen (second) + 1);
2653
2654 /* Convert the names to lower case. */
2655 stricpy (copy1, first);
2656 stricpy (copy2, second);
2657
2658 /* Remove size and endian strings from the name. */
2659 strcut (copy1, "big");
2660 strcut (copy1, "little");
2661 strcut (copy2, "big");
2662 strcut (copy2, "little");
2663
2664 /* Return a value based on how many characters match,
2665 starting from the beginning. If both strings are
2666 the same then return 10 * their length. */
2667 for (result = 0; copy1[result] == copy2[result]; result++)
2668 if (copy1[result] == 0)
2669 {
2670 result *= 10;
2671 break;
2672 }
2673
2674 free (copy1);
2675 free (copy2);
2676
2677 return result;
2678 }
2679
2680 /* Set by closest_target_match() below. */
2681 static const bfd_target *winner;
2682
2683 /* Scan all the valid bfd targets looking for one that has the endianness
2684 requirement that was specified on the command line, and is the nearest
2685 match to the original output target. */
2686
2687 static int
2688 closest_target_match (const bfd_target *target, void *data)
2689 {
2690 const bfd_target *original = data;
2691
2692 if (command_line.endian == ENDIAN_BIG
2693 && target->byteorder != BFD_ENDIAN_BIG)
2694 return 0;
2695
2696 if (command_line.endian == ENDIAN_LITTLE
2697 && target->byteorder != BFD_ENDIAN_LITTLE)
2698 return 0;
2699
2700 /* Must be the same flavour. */
2701 if (target->flavour != original->flavour)
2702 return 0;
2703
2704 /* Ignore generic big and little endian elf vectors. */
2705 if ( strcmp (target->name, "elf32-big") == 0
2706 || strcmp (target->name, "elf64-big") == 0
2707 || strcmp (target->name, "elf32-little") == 0
2708 || strcmp (target->name, "elf64-little") == 0)
2709 return 0;
2710
2711 /* If we have not found a potential winner yet, then record this one. */
2712 if (winner == NULL)
2713 {
2714 winner = target;
2715 return 0;
2716 }
2717
2718 /* Oh dear, we now have two potential candidates for a successful match.
2719 Compare their names and choose the better one. */
2720 if (name_compare (target->name, original->name)
2721 > name_compare (winner->name, original->name))
2722 winner = target;
2723
2724 /* Keep on searching until wqe have checked them all. */
2725 return 0;
2726 }
2727
2728 /* Return the BFD target format of the first input file. */
2729
2730 static char *
2731 get_first_input_target (void)
2732 {
2733 char *target = NULL;
2734
2735 LANG_FOR_EACH_INPUT_STATEMENT (s)
2736 {
2737 if (s->header.type == lang_input_statement_enum
2738 && s->real)
2739 {
2740 ldfile_open_file (s);
2741
2742 if (s->the_bfd != NULL
2743 && bfd_check_format (s->the_bfd, bfd_object))
2744 {
2745 target = bfd_get_target (s->the_bfd);
2746
2747 if (target != NULL)
2748 break;
2749 }
2750 }
2751 }
2752
2753 return target;
2754 }
2755
2756 const char *
2757 lang_get_output_target (void)
2758 {
2759 const char *target;
2760
2761 /* Has the user told us which output format to use? */
2762 if (output_target != NULL)
2763 return output_target;
2764
2765 /* No - has the current target been set to something other than
2766 the default? */
2767 if (current_target != default_target)
2768 return current_target;
2769
2770 /* No - can we determine the format of the first input file? */
2771 target = get_first_input_target ();
2772 if (target != NULL)
2773 return target;
2774
2775 /* Failed - use the default output target. */
2776 return default_target;
2777 }
2778
2779 /* Open the output file. */
2780
2781 static void
2782 open_output (const char *name)
2783 {
2784 output_target = lang_get_output_target ();
2785
2786 /* Has the user requested a particular endianness on the command
2787 line? */
2788 if (command_line.endian != ENDIAN_UNSET)
2789 {
2790 const bfd_target *target;
2791 enum bfd_endian desired_endian;
2792
2793 /* Get the chosen target. */
2794 target = bfd_search_for_target (get_target, (void *) output_target);
2795
2796 /* If the target is not supported, we cannot do anything. */
2797 if (target != NULL)
2798 {
2799 if (command_line.endian == ENDIAN_BIG)
2800 desired_endian = BFD_ENDIAN_BIG;
2801 else
2802 desired_endian = BFD_ENDIAN_LITTLE;
2803
2804 /* See if the target has the wrong endianness. This should
2805 not happen if the linker script has provided big and
2806 little endian alternatives, but some scrips don't do
2807 this. */
2808 if (target->byteorder != desired_endian)
2809 {
2810 /* If it does, then see if the target provides
2811 an alternative with the correct endianness. */
2812 if (target->alternative_target != NULL
2813 && (target->alternative_target->byteorder == desired_endian))
2814 output_target = target->alternative_target->name;
2815 else
2816 {
2817 /* Try to find a target as similar as possible to
2818 the default target, but which has the desired
2819 endian characteristic. */
2820 bfd_search_for_target (closest_target_match,
2821 (void *) target);
2822
2823 /* Oh dear - we could not find any targets that
2824 satisfy our requirements. */
2825 if (winner == NULL)
2826 einfo (_("%P: warning: could not find any targets"
2827 " that match endianness requirement\n"));
2828 else
2829 output_target = winner->name;
2830 }
2831 }
2832 }
2833 }
2834
2835 link_info.output_bfd = bfd_openw (name, output_target);
2836
2837 if (link_info.output_bfd == NULL)
2838 {
2839 if (bfd_get_error () == bfd_error_invalid_target)
2840 einfo (_("%P%F: target %s not found\n"), output_target);
2841
2842 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2843 }
2844
2845 delete_output_file_on_failure = TRUE;
2846
2847 if (! bfd_set_format (link_info.output_bfd, bfd_object))
2848 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2849 if (! bfd_set_arch_mach (link_info.output_bfd,
2850 ldfile_output_architecture,
2851 ldfile_output_machine))
2852 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2853
2854 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
2855 if (link_info.hash == NULL)
2856 einfo (_("%P%F: can not create hash table: %E\n"));
2857
2858 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
2859 }
2860
2861 static void
2862 ldlang_open_output (lang_statement_union_type *statement)
2863 {
2864 switch (statement->header.type)
2865 {
2866 case lang_output_statement_enum:
2867 ASSERT (link_info.output_bfd == NULL);
2868 open_output (statement->output_statement.name);
2869 ldemul_set_output_arch ();
2870 if (config.magic_demand_paged && !link_info.relocatable)
2871 link_info.output_bfd->flags |= D_PAGED;
2872 else
2873 link_info.output_bfd->flags &= ~D_PAGED;
2874 if (config.text_read_only)
2875 link_info.output_bfd->flags |= WP_TEXT;
2876 else
2877 link_info.output_bfd->flags &= ~WP_TEXT;
2878 if (link_info.traditional_format)
2879 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2880 else
2881 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2882 break;
2883
2884 case lang_target_statement_enum:
2885 current_target = statement->target_statement.target;
2886 break;
2887 default:
2888 break;
2889 }
2890 }
2891
2892 /* Convert between addresses in bytes and sizes in octets.
2893 For currently supported targets, octets_per_byte is always a power
2894 of two, so we can use shifts. */
2895 #define TO_ADDR(X) ((X) >> opb_shift)
2896 #define TO_SIZE(X) ((X) << opb_shift)
2897
2898 /* Support the above. */
2899 static unsigned int opb_shift = 0;
2900
2901 static void
2902 init_opb (void)
2903 {
2904 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2905 ldfile_output_machine);
2906 opb_shift = 0;
2907 if (x > 1)
2908 while ((x & 1) == 0)
2909 {
2910 x >>= 1;
2911 ++opb_shift;
2912 }
2913 ASSERT (x == 1);
2914 }
2915
2916 /* Open all the input files. */
2917
2918 static void
2919 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2920 {
2921 for (; s != NULL; s = s->header.next)
2922 {
2923 switch (s->header.type)
2924 {
2925 case lang_constructors_statement_enum:
2926 open_input_bfds (constructor_list.head, force);
2927 break;
2928 case lang_output_section_statement_enum:
2929 open_input_bfds (s->output_section_statement.children.head, force);
2930 break;
2931 case lang_wild_statement_enum:
2932 /* Maybe we should load the file's symbols. */
2933 if (s->wild_statement.filename
2934 && ! wildcardp (s->wild_statement.filename))
2935 lookup_name (s->wild_statement.filename);
2936 open_input_bfds (s->wild_statement.children.head, force);
2937 break;
2938 case lang_group_statement_enum:
2939 {
2940 struct bfd_link_hash_entry *undefs;
2941
2942 /* We must continually search the entries in the group
2943 until no new symbols are added to the list of undefined
2944 symbols. */
2945
2946 do
2947 {
2948 undefs = link_info.hash->undefs_tail;
2949 open_input_bfds (s->group_statement.children.head, TRUE);
2950 }
2951 while (undefs != link_info.hash->undefs_tail);
2952 }
2953 break;
2954 case lang_target_statement_enum:
2955 current_target = s->target_statement.target;
2956 break;
2957 case lang_input_statement_enum:
2958 if (s->input_statement.real)
2959 {
2960 lang_statement_list_type add;
2961
2962 s->input_statement.target = current_target;
2963
2964 /* If we are being called from within a group, and this
2965 is an archive which has already been searched, then
2966 force it to be researched unless the whole archive
2967 has been loaded already. */
2968 if (force
2969 && !s->input_statement.whole_archive
2970 && s->input_statement.loaded
2971 && bfd_check_format (s->input_statement.the_bfd,
2972 bfd_archive))
2973 s->input_statement.loaded = FALSE;
2974
2975 lang_list_init (&add);
2976
2977 if (! load_symbols (&s->input_statement, &add))
2978 config.make_executable = FALSE;
2979
2980 if (add.head != NULL)
2981 {
2982 *add.tail = s->header.next;
2983 s->header.next = add.head;
2984 }
2985 }
2986 break;
2987 default:
2988 break;
2989 }
2990 }
2991 }
2992
2993 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2994
2995 void
2996 lang_track_definedness (const char *name)
2997 {
2998 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2999 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
3000 }
3001
3002 /* New-function for the definedness hash table. */
3003
3004 static struct bfd_hash_entry *
3005 lang_definedness_newfunc (struct bfd_hash_entry *entry,
3006 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
3007 const char *name ATTRIBUTE_UNUSED)
3008 {
3009 struct lang_definedness_hash_entry *ret
3010 = (struct lang_definedness_hash_entry *) entry;
3011
3012 if (ret == NULL)
3013 ret = (struct lang_definedness_hash_entry *)
3014 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
3015
3016 if (ret == NULL)
3017 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
3018
3019 ret->iteration = -1;
3020 return &ret->root;
3021 }
3022
3023 /* Return the iteration when the definition of NAME was last updated. A
3024 value of -1 means that the symbol is not defined in the linker script
3025 or the command line, but may be defined in the linker symbol table. */
3026
3027 int
3028 lang_symbol_definition_iteration (const char *name)
3029 {
3030 struct lang_definedness_hash_entry *defentry
3031 = (struct lang_definedness_hash_entry *)
3032 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3033
3034 /* We've already created this one on the presence of DEFINED in the
3035 script, so it can't be NULL unless something is borked elsewhere in
3036 the code. */
3037 if (defentry == NULL)
3038 FAIL ();
3039
3040 return defentry->iteration;
3041 }
3042
3043 /* Update the definedness state of NAME. */
3044
3045 void
3046 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3047 {
3048 struct lang_definedness_hash_entry *defentry
3049 = (struct lang_definedness_hash_entry *)
3050 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3051
3052 /* We don't keep track of symbols not tested with DEFINED. */
3053 if (defentry == NULL)
3054 return;
3055
3056 /* If the symbol was already defined, and not from an earlier statement
3057 iteration, don't update the definedness iteration, because that'd
3058 make the symbol seem defined in the linker script at this point, and
3059 it wasn't; it was defined in some object. If we do anyway, DEFINED
3060 would start to yield false before this point and the construct "sym =
3061 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3062 in an object. */
3063 if (h->type != bfd_link_hash_undefined
3064 && h->type != bfd_link_hash_common
3065 && h->type != bfd_link_hash_new
3066 && defentry->iteration == -1)
3067 return;
3068
3069 defentry->iteration = lang_statement_iteration;
3070 }
3071
3072 /* Add the supplied name to the symbol table as an undefined reference.
3073 This is a two step process as the symbol table doesn't even exist at
3074 the time the ld command line is processed. First we put the name
3075 on a list, then, once the output file has been opened, transfer the
3076 name to the symbol table. */
3077
3078 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3079
3080 #define ldlang_undef_chain_list_head entry_symbol.next
3081
3082 void
3083 ldlang_add_undef (const char *const name)
3084 {
3085 ldlang_undef_chain_list_type *new =
3086 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3087
3088 new->next = ldlang_undef_chain_list_head;
3089 ldlang_undef_chain_list_head = new;
3090
3091 new->name = xstrdup (name);
3092
3093 if (link_info.output_bfd != NULL)
3094 insert_undefined (new->name);
3095 }
3096
3097 /* Insert NAME as undefined in the symbol table. */
3098
3099 static void
3100 insert_undefined (const char *name)
3101 {
3102 struct bfd_link_hash_entry *h;
3103
3104 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3105 if (h == NULL)
3106 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3107 if (h->type == bfd_link_hash_new)
3108 {
3109 h->type = bfd_link_hash_undefined;
3110 h->u.undef.abfd = NULL;
3111 bfd_link_add_undef (link_info.hash, h);
3112 }
3113 }
3114
3115 /* Run through the list of undefineds created above and place them
3116 into the linker hash table as undefined symbols belonging to the
3117 script file. */
3118
3119 static void
3120 lang_place_undefineds (void)
3121 {
3122 ldlang_undef_chain_list_type *ptr;
3123
3124 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3125 insert_undefined (ptr->name);
3126 }
3127
3128 /* Check for all readonly or some readwrite sections. */
3129
3130 static void
3131 check_input_sections
3132 (lang_statement_union_type *s,
3133 lang_output_section_statement_type *output_section_statement)
3134 {
3135 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3136 {
3137 switch (s->header.type)
3138 {
3139 case lang_wild_statement_enum:
3140 walk_wild (&s->wild_statement, check_section_callback,
3141 output_section_statement);
3142 if (! output_section_statement->all_input_readonly)
3143 return;
3144 break;
3145 case lang_constructors_statement_enum:
3146 check_input_sections (constructor_list.head,
3147 output_section_statement);
3148 if (! output_section_statement->all_input_readonly)
3149 return;
3150 break;
3151 case lang_group_statement_enum:
3152 check_input_sections (s->group_statement.children.head,
3153 output_section_statement);
3154 if (! output_section_statement->all_input_readonly)
3155 return;
3156 break;
3157 default:
3158 break;
3159 }
3160 }
3161 }
3162
3163 /* Update wildcard statements if needed. */
3164
3165 static void
3166 update_wild_statements (lang_statement_union_type *s)
3167 {
3168 struct wildcard_list *sec;
3169
3170 switch (sort_section)
3171 {
3172 default:
3173 FAIL ();
3174
3175 case none:
3176 break;
3177
3178 case by_name:
3179 case by_alignment:
3180 for (; s != NULL; s = s->header.next)
3181 {
3182 switch (s->header.type)
3183 {
3184 default:
3185 break;
3186
3187 case lang_wild_statement_enum:
3188 sec = s->wild_statement.section_list;
3189 for (sec = s->wild_statement.section_list; sec != NULL;
3190 sec = sec->next)
3191 {
3192 switch (sec->spec.sorted)
3193 {
3194 case none:
3195 sec->spec.sorted = sort_section;
3196 break;
3197 case by_name:
3198 if (sort_section == by_alignment)
3199 sec->spec.sorted = by_name_alignment;
3200 break;
3201 case by_alignment:
3202 if (sort_section == by_name)
3203 sec->spec.sorted = by_alignment_name;
3204 break;
3205 default:
3206 break;
3207 }
3208 }
3209 break;
3210
3211 case lang_constructors_statement_enum:
3212 update_wild_statements (constructor_list.head);
3213 break;
3214
3215 case lang_output_section_statement_enum:
3216 update_wild_statements
3217 (s->output_section_statement.children.head);
3218 break;
3219
3220 case lang_group_statement_enum:
3221 update_wild_statements (s->group_statement.children.head);
3222 break;
3223 }
3224 }
3225 break;
3226 }
3227 }
3228
3229 /* Open input files and attach to output sections. */
3230
3231 static void
3232 map_input_to_output_sections
3233 (lang_statement_union_type *s, const char *target,
3234 lang_output_section_statement_type *os)
3235 {
3236 flagword flags;
3237
3238 for (; s != NULL; s = s->header.next)
3239 {
3240 switch (s->header.type)
3241 {
3242 case lang_wild_statement_enum:
3243 wild (&s->wild_statement, target, os);
3244 break;
3245 case lang_constructors_statement_enum:
3246 map_input_to_output_sections (constructor_list.head,
3247 target,
3248 os);
3249 break;
3250 case lang_output_section_statement_enum:
3251 if (s->output_section_statement.constraint)
3252 {
3253 if (s->output_section_statement.constraint != ONLY_IF_RW
3254 && s->output_section_statement.constraint != ONLY_IF_RO)
3255 break;
3256 s->output_section_statement.all_input_readonly = TRUE;
3257 check_input_sections (s->output_section_statement.children.head,
3258 &s->output_section_statement);
3259 if ((s->output_section_statement.all_input_readonly
3260 && s->output_section_statement.constraint == ONLY_IF_RW)
3261 || (!s->output_section_statement.all_input_readonly
3262 && s->output_section_statement.constraint == ONLY_IF_RO))
3263 {
3264 s->output_section_statement.constraint = -1;
3265 break;
3266 }
3267 }
3268
3269 map_input_to_output_sections (s->output_section_statement.children.head,
3270 target,
3271 &s->output_section_statement);
3272 break;
3273 case lang_output_statement_enum:
3274 break;
3275 case lang_target_statement_enum:
3276 target = s->target_statement.target;
3277 break;
3278 case lang_group_statement_enum:
3279 map_input_to_output_sections (s->group_statement.children.head,
3280 target,
3281 os);
3282 break;
3283 case lang_data_statement_enum:
3284 /* Make sure that any sections mentioned in the expression
3285 are initialized. */
3286 exp_init_os (s->data_statement.exp);
3287 flags = SEC_HAS_CONTENTS;
3288 /* The output section gets contents, and then we inspect for
3289 any flags set in the input script which override any ALLOC. */
3290 if (!(os->flags & SEC_NEVER_LOAD))
3291 flags |= SEC_ALLOC | SEC_LOAD;
3292 if (os->bfd_section == NULL)
3293 init_os (os, NULL, flags);
3294 else
3295 os->bfd_section->flags |= flags;
3296 break;
3297 case lang_input_section_enum:
3298 break;
3299 case lang_fill_statement_enum:
3300 case lang_object_symbols_statement_enum:
3301 case lang_reloc_statement_enum:
3302 case lang_padding_statement_enum:
3303 case lang_input_statement_enum:
3304 if (os != NULL && os->bfd_section == NULL)
3305 init_os (os, NULL, 0);
3306 break;
3307 case lang_assignment_statement_enum:
3308 if (os != NULL && os->bfd_section == NULL)
3309 init_os (os, NULL, 0);
3310
3311 /* Make sure that any sections mentioned in the assignment
3312 are initialized. */
3313 exp_init_os (s->assignment_statement.exp);
3314 break;
3315 case lang_address_statement_enum:
3316 /* Mark the specified section with the supplied address.
3317
3318 If this section was actually a segment marker, then the
3319 directive is ignored if the linker script explicitly
3320 processed the segment marker. Originally, the linker
3321 treated segment directives (like -Ttext on the
3322 command-line) as section directives. We honor the
3323 section directive semantics for backwards compatibilty;
3324 linker scripts that do not specifically check for
3325 SEGMENT_START automatically get the old semantics. */
3326 if (!s->address_statement.segment
3327 || !s->address_statement.segment->used)
3328 {
3329 lang_output_section_statement_type *aos
3330 = (lang_output_section_statement_lookup
3331 (s->address_statement.section_name));
3332
3333 if (aos->bfd_section == NULL)
3334 init_os (aos, NULL, 0);
3335 aos->addr_tree = s->address_statement.address;
3336 }
3337 break;
3338 case lang_insert_statement_enum:
3339 break;
3340 }
3341 }
3342 }
3343
3344 /* An insert statement snips out all the linker statements from the
3345 start of the list and places them after the output section
3346 statement specified by the insert. This operation is complicated
3347 by the fact that we keep a doubly linked list of output section
3348 statements as well as the singly linked list of all statements. */
3349
3350 static void
3351 process_insert_statements (void)
3352 {
3353 lang_statement_union_type **s;
3354 lang_output_section_statement_type *first_os = NULL;
3355 lang_output_section_statement_type *last_os = NULL;
3356
3357 /* "start of list" is actually the statement immediately after
3358 the special abs_section output statement, so that it isn't
3359 reordered. */
3360 s = &lang_output_section_statement.head;
3361 while (*(s = &(*s)->header.next) != NULL)
3362 {
3363 if ((*s)->header.type == lang_output_section_statement_enum)
3364 {
3365 /* Keep pointers to the first and last output section
3366 statement in the sequence we may be about to move. */
3367 last_os = &(*s)->output_section_statement;
3368 if (first_os == NULL)
3369 first_os = last_os;
3370 }
3371 else if ((*s)->header.type == lang_insert_statement_enum)
3372 {
3373 lang_insert_statement_type *i = &(*s)->insert_statement;
3374 lang_output_section_statement_type *where;
3375 lang_output_section_statement_type *os;
3376 lang_statement_union_type **ptr;
3377 lang_statement_union_type *first;
3378
3379 where = lang_output_section_find (i->where);
3380 if (where != NULL && i->is_before)
3381 {
3382 do
3383 where = where->prev;
3384 while (where != NULL && where->constraint == -1);
3385 }
3386 if (where == NULL)
3387 {
3388 einfo (_("%X%P: %s not found for insert\n"), i->where);
3389 continue;
3390 }
3391 /* You can't insert into the list you are moving. */
3392 for (os = first_os; os != NULL; os = os->next)
3393 if (os == where || os == last_os)
3394 break;
3395 if (os == where)
3396 {
3397 einfo (_("%X%P: %s not found for insert\n"), i->where);
3398 continue;
3399 }
3400
3401 /* Deal with reordering the output section statement list. */
3402 if (last_os != NULL)
3403 {
3404 asection *first_sec, *last_sec;
3405 struct lang_output_section_statement_struct **next;
3406
3407 /* Snip out the output sections we are moving. */
3408 first_os->prev->next = last_os->next;
3409 if (last_os->next == NULL)
3410 {
3411 next = &first_os->prev->next;
3412 lang_output_section_statement.tail
3413 = (lang_statement_union_type **) next;
3414 }
3415 else
3416 last_os->next->prev = first_os->prev;
3417 /* Add them in at the new position. */
3418 last_os->next = where->next;
3419 if (where->next == NULL)
3420 {
3421 next = &last_os->next;
3422 lang_output_section_statement.tail
3423 = (lang_statement_union_type **) next;
3424 }
3425 else
3426 where->next->prev = last_os;
3427 first_os->prev = where;
3428 where->next = first_os;
3429
3430 /* Move the bfd sections in the same way. */
3431 first_sec = NULL;
3432 last_sec = NULL;
3433 for (os = first_os; os != NULL; os = os->next)
3434 {
3435 if (os->bfd_section != NULL
3436 && os->bfd_section->owner != NULL)
3437 {
3438 last_sec = os->bfd_section;
3439 if (first_sec == NULL)
3440 first_sec = last_sec;
3441 }
3442 if (os == last_os)
3443 break;
3444 }
3445 if (last_sec != NULL)
3446 {
3447 asection *sec = where->bfd_section;
3448 if (sec == NULL)
3449 sec = output_prev_sec_find (where);
3450
3451 /* The place we want to insert must come after the
3452 sections we are moving. So if we find no
3453 section or if the section is the same as our
3454 last section, then no move is needed. */
3455 if (sec != NULL && sec != last_sec)
3456 {
3457 /* Trim them off. */
3458 if (first_sec->prev != NULL)
3459 first_sec->prev->next = last_sec->next;
3460 else
3461 link_info.output_bfd->sections = last_sec->next;
3462 if (last_sec->next != NULL)
3463 last_sec->next->prev = first_sec->prev;
3464 else
3465 link_info.output_bfd->section_last = first_sec->prev;
3466 /* Add back. */
3467 last_sec->next = sec->next;
3468 if (sec->next != NULL)
3469 sec->next->prev = last_sec;
3470 else
3471 link_info.output_bfd->section_last = last_sec;
3472 first_sec->prev = sec;
3473 sec->next = first_sec;
3474 }
3475 }
3476
3477 first_os = NULL;
3478 last_os = NULL;
3479 }
3480
3481 ptr = insert_os_after (where);
3482 /* Snip everything after the abs_section output statement we
3483 know is at the start of the list, up to and including
3484 the insert statement we are currently processing. */
3485 first = lang_output_section_statement.head->header.next;
3486 lang_output_section_statement.head->header.next = (*s)->header.next;
3487 /* Add them back where they belong. */
3488 *s = *ptr;
3489 if (*s == NULL)
3490 statement_list.tail = s;
3491 *ptr = first;
3492 s = &lang_output_section_statement.head;
3493 }
3494 }
3495 }
3496
3497 /* An output section might have been removed after its statement was
3498 added. For example, ldemul_before_allocation can remove dynamic
3499 sections if they turn out to be not needed. Clean them up here. */
3500
3501 void
3502 strip_excluded_output_sections (void)
3503 {
3504 lang_output_section_statement_type *os;
3505
3506 /* Run lang_size_sections (if not already done). */
3507 if (expld.phase != lang_mark_phase_enum)
3508 {
3509 expld.phase = lang_mark_phase_enum;
3510 expld.dataseg.phase = exp_dataseg_none;
3511 one_lang_size_sections_pass (NULL, FALSE);
3512 lang_reset_memory_regions ();
3513 }
3514
3515 for (os = &lang_output_section_statement.head->output_section_statement;
3516 os != NULL;
3517 os = os->next)
3518 {
3519 asection *output_section;
3520 bfd_boolean exclude;
3521
3522 if (os->constraint == -1)
3523 continue;
3524
3525 output_section = os->bfd_section;
3526 if (output_section == NULL)
3527 continue;
3528
3529 exclude = (output_section->rawsize == 0
3530 && (output_section->flags & SEC_KEEP) == 0
3531 && !bfd_section_removed_from_list (link_info.output_bfd,
3532 output_section));
3533
3534 /* Some sections have not yet been sized, notably .gnu.version,
3535 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3536 input sections, so don't drop output sections that have such
3537 input sections unless they are also marked SEC_EXCLUDE. */
3538 if (exclude && output_section->map_head.s != NULL)
3539 {
3540 asection *s;
3541
3542 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3543 if ((s->flags & SEC_LINKER_CREATED) != 0
3544 && (s->flags & SEC_EXCLUDE) == 0)
3545 {
3546 exclude = FALSE;
3547 break;
3548 }
3549 }
3550
3551 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3552 output_section->map_head.link_order = NULL;
3553 output_section->map_tail.link_order = NULL;
3554
3555 if (exclude)
3556 {
3557 /* We don't set bfd_section to NULL since bfd_section of the
3558 removed output section statement may still be used. */
3559 if (!os->section_relative_symbol
3560 && !os->update_dot_tree)
3561 os->ignored = TRUE;
3562 output_section->flags |= SEC_EXCLUDE;
3563 bfd_section_list_remove (link_info.output_bfd, output_section);
3564 link_info.output_bfd->section_count--;
3565 }
3566 }
3567
3568 /* Stop future calls to lang_add_section from messing with map_head
3569 and map_tail link_order fields. */
3570 stripped_excluded_sections = TRUE;
3571 }
3572
3573 static void
3574 print_output_section_statement
3575 (lang_output_section_statement_type *output_section_statement)
3576 {
3577 asection *section = output_section_statement->bfd_section;
3578 int len;
3579
3580 if (output_section_statement != abs_output_section)
3581 {
3582 minfo ("\n%s", output_section_statement->name);
3583
3584 if (section != NULL)
3585 {
3586 print_dot = section->vma;
3587
3588 len = strlen (output_section_statement->name);
3589 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3590 {
3591 print_nl ();
3592 len = 0;
3593 }
3594 while (len < SECTION_NAME_MAP_LENGTH)
3595 {
3596 print_space ();
3597 ++len;
3598 }
3599
3600 minfo ("0x%V %W", section->vma, section->size);
3601
3602 if (section->vma != section->lma)
3603 minfo (_(" load address 0x%V"), section->lma);
3604 }
3605
3606 print_nl ();
3607 }
3608
3609 print_statement_list (output_section_statement->children.head,
3610 output_section_statement);
3611 }
3612
3613 /* Scan for the use of the destination in the right hand side
3614 of an expression. In such cases we will not compute the
3615 correct expression, since the value of DST that is used on
3616 the right hand side will be its final value, not its value
3617 just before this expression is evaluated. */
3618
3619 static bfd_boolean
3620 scan_for_self_assignment (const char * dst, etree_type * rhs)
3621 {
3622 if (rhs == NULL || dst == NULL)
3623 return FALSE;
3624
3625 switch (rhs->type.node_class)
3626 {
3627 case etree_binary:
3628 return scan_for_self_assignment (dst, rhs->binary.lhs)
3629 || scan_for_self_assignment (dst, rhs->binary.rhs);
3630
3631 case etree_trinary:
3632 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3633 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3634
3635 case etree_assign:
3636 case etree_provided:
3637 case etree_provide:
3638 if (strcmp (dst, rhs->assign.dst) == 0)
3639 return TRUE;
3640 return scan_for_self_assignment (dst, rhs->assign.src);
3641
3642 case etree_unary:
3643 return scan_for_self_assignment (dst, rhs->unary.child);
3644
3645 case etree_value:
3646 if (rhs->value.str)
3647 return strcmp (dst, rhs->value.str) == 0;
3648 return FALSE;
3649
3650 case etree_name:
3651 if (rhs->name.name)
3652 return strcmp (dst, rhs->name.name) == 0;
3653 return FALSE;
3654
3655 default:
3656 break;
3657 }
3658
3659 return FALSE;
3660 }
3661
3662
3663 static void
3664 print_assignment (lang_assignment_statement_type *assignment,
3665 lang_output_section_statement_type *output_section)
3666 {
3667 unsigned int i;
3668 bfd_boolean is_dot;
3669 bfd_boolean computation_is_valid = TRUE;
3670 etree_type *tree;
3671
3672 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3673 print_space ();
3674
3675 if (assignment->exp->type.node_class == etree_assert)
3676 {
3677 is_dot = FALSE;
3678 tree = assignment->exp->assert_s.child;
3679 computation_is_valid = TRUE;
3680 }
3681 else
3682 {
3683 const char *dst = assignment->exp->assign.dst;
3684
3685 is_dot = (dst[0] == '.' && dst[1] == 0);
3686 tree = assignment->exp->assign.src;
3687 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3688 }
3689
3690 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3691 if (expld.result.valid_p)
3692 {
3693 bfd_vma value;
3694
3695 if (computation_is_valid)
3696 {
3697 value = expld.result.value;
3698
3699 if (expld.result.section)
3700 value += expld.result.section->vma;
3701
3702 minfo ("0x%V", value);
3703 if (is_dot)
3704 print_dot = value;
3705 }
3706 else
3707 {
3708 struct bfd_link_hash_entry *h;
3709
3710 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3711 FALSE, FALSE, TRUE);
3712 if (h)
3713 {
3714 value = h->u.def.value;
3715
3716 if (expld.result.section)
3717 value += expld.result.section->vma;
3718
3719 minfo ("[0x%V]", value);
3720 }
3721 else
3722 minfo ("[unresolved]");
3723 }
3724 }
3725 else
3726 {
3727 minfo ("*undef* ");
3728 #ifdef BFD64
3729 minfo (" ");
3730 #endif
3731 }
3732
3733 minfo (" ");
3734 exp_print_tree (assignment->exp);
3735 print_nl ();
3736 }
3737
3738 static void
3739 print_input_statement (lang_input_statement_type *statm)
3740 {
3741 if ((statm->filename != NULL)
3742 && ((statm->the_bfd == NULL)
3743 ||
3744 ((statm->the_bfd->flags & BFD_LINKER_CREATED) == 0)))
3745 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3746 }
3747
3748 /* Print all symbols defined in a particular section. This is called
3749 via bfd_link_hash_traverse, or by print_all_symbols. */
3750
3751 static bfd_boolean
3752 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3753 {
3754 asection *sec = ptr;
3755
3756 if ((hash_entry->type == bfd_link_hash_defined
3757 || hash_entry->type == bfd_link_hash_defweak)
3758 && sec == hash_entry->u.def.section)
3759 {
3760 int i;
3761
3762 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3763 print_space ();
3764 minfo ("0x%V ",
3765 (hash_entry->u.def.value
3766 + hash_entry->u.def.section->output_offset
3767 + hash_entry->u.def.section->output_section->vma));
3768
3769 minfo (" %T\n", hash_entry->root.string);
3770 }
3771
3772 return TRUE;
3773 }
3774
3775 static void
3776 print_all_symbols (asection *sec)
3777 {
3778 struct fat_user_section_struct *ud = get_userdata (sec);
3779 struct map_symbol_def *def;
3780
3781 if (!ud)
3782 return;
3783
3784 *ud->map_symbol_def_tail = 0;
3785 for (def = ud->map_symbol_def_head; def; def = def->next)
3786 print_one_symbol (def->entry, sec);
3787 }
3788
3789 /* Print information about an input section to the map file. */
3790
3791 static void
3792 print_input_section (asection *i)
3793 {
3794 bfd_size_type size = i->size;
3795 int len;
3796 bfd_vma addr;
3797
3798 init_opb ();
3799
3800 print_space ();
3801 minfo ("%s", i->name);
3802
3803 len = 1 + strlen (i->name);
3804 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3805 {
3806 print_nl ();
3807 len = 0;
3808 }
3809 while (len < SECTION_NAME_MAP_LENGTH)
3810 {
3811 print_space ();
3812 ++len;
3813 }
3814
3815 if (i->output_section != NULL
3816 && i->output_section->owner == link_info.output_bfd)
3817 addr = i->output_section->vma + i->output_offset;
3818 else
3819 {
3820 addr = print_dot;
3821 size = 0;
3822 }
3823
3824 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3825
3826 if (size != i->rawsize && i->rawsize != 0)
3827 {
3828 len = SECTION_NAME_MAP_LENGTH + 3;
3829 #ifdef BFD64
3830 len += 16;
3831 #else
3832 len += 8;
3833 #endif
3834 while (len > 0)
3835 {
3836 print_space ();
3837 --len;
3838 }
3839
3840 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3841 }
3842
3843 if (i->output_section != NULL
3844 && i->output_section->owner == link_info.output_bfd)
3845 {
3846 if (link_info.reduce_memory_overheads)
3847 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3848 else
3849 print_all_symbols (i);
3850
3851 print_dot = addr + TO_ADDR (size);
3852 }
3853 }
3854
3855 static void
3856 print_fill_statement (lang_fill_statement_type *fill)
3857 {
3858 size_t size;
3859 unsigned char *p;
3860 fputs (" FILL mask 0x", config.map_file);
3861 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3862 fprintf (config.map_file, "%02x", *p);
3863 fputs ("\n", config.map_file);
3864 }
3865
3866 static void
3867 print_data_statement (lang_data_statement_type *data)
3868 {
3869 int i;
3870 bfd_vma addr;
3871 bfd_size_type size;
3872 const char *name;
3873
3874 init_opb ();
3875 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3876 print_space ();
3877
3878 addr = data->output_offset;
3879 if (data->output_section != NULL)
3880 addr += data->output_section->vma;
3881
3882 switch (data->type)
3883 {
3884 default:
3885 abort ();
3886 case BYTE:
3887 size = BYTE_SIZE;
3888 name = "BYTE";
3889 break;
3890 case SHORT:
3891 size = SHORT_SIZE;
3892 name = "SHORT";
3893 break;
3894 case LONG:
3895 size = LONG_SIZE;
3896 name = "LONG";
3897 break;
3898 case QUAD:
3899 size = QUAD_SIZE;
3900 name = "QUAD";
3901 break;
3902 case SQUAD:
3903 size = QUAD_SIZE;
3904 name = "SQUAD";
3905 break;
3906 }
3907
3908 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3909
3910 if (data->exp->type.node_class != etree_value)
3911 {
3912 print_space ();
3913 exp_print_tree (data->exp);
3914 }
3915
3916 print_nl ();
3917
3918 print_dot = addr + TO_ADDR (size);
3919 }
3920
3921 /* Print an address statement. These are generated by options like
3922 -Ttext. */
3923
3924 static void
3925 print_address_statement (lang_address_statement_type *address)
3926 {
3927 minfo (_("Address of section %s set to "), address->section_name);
3928 exp_print_tree (address->address);
3929 print_nl ();
3930 }
3931
3932 /* Print a reloc statement. */
3933
3934 static void
3935 print_reloc_statement (lang_reloc_statement_type *reloc)
3936 {
3937 int i;
3938 bfd_vma addr;
3939 bfd_size_type size;
3940
3941 init_opb ();
3942 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3943 print_space ();
3944
3945 addr = reloc->output_offset;
3946 if (reloc->output_section != NULL)
3947 addr += reloc->output_section->vma;
3948
3949 size = bfd_get_reloc_size (reloc->howto);
3950
3951 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3952
3953 if (reloc->name != NULL)
3954 minfo ("%s+", reloc->name);
3955 else
3956 minfo ("%s+", reloc->section->name);
3957
3958 exp_print_tree (reloc->addend_exp);
3959
3960 print_nl ();
3961
3962 print_dot = addr + TO_ADDR (size);
3963 }
3964
3965 static void
3966 print_padding_statement (lang_padding_statement_type *s)
3967 {
3968 int len;
3969 bfd_vma addr;
3970
3971 init_opb ();
3972 minfo (" *fill*");
3973
3974 len = sizeof " *fill*" - 1;
3975 while (len < SECTION_NAME_MAP_LENGTH)
3976 {
3977 print_space ();
3978 ++len;
3979 }
3980
3981 addr = s->output_offset;
3982 if (s->output_section != NULL)
3983 addr += s->output_section->vma;
3984 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3985
3986 if (s->fill->size != 0)
3987 {
3988 size_t size;
3989 unsigned char *p;
3990 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3991 fprintf (config.map_file, "%02x", *p);
3992 }
3993
3994 print_nl ();
3995
3996 print_dot = addr + TO_ADDR (s->size);
3997 }
3998
3999 static void
4000 print_wild_statement (lang_wild_statement_type *w,
4001 lang_output_section_statement_type *os)
4002 {
4003 struct wildcard_list *sec;
4004
4005 print_space ();
4006
4007 if (w->filenames_sorted)
4008 minfo ("SORT(");
4009 if (w->filename != NULL)
4010 minfo ("%s", w->filename);
4011 else
4012 minfo ("*");
4013 if (w->filenames_sorted)
4014 minfo (")");
4015
4016 minfo ("(");
4017 for (sec = w->section_list; sec; sec = sec->next)
4018 {
4019 if (sec->spec.sorted)
4020 minfo ("SORT(");
4021 if (sec->spec.exclude_name_list != NULL)
4022 {
4023 name_list *tmp;
4024 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
4025 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
4026 minfo (" %s", tmp->name);
4027 minfo (") ");
4028 }
4029 if (sec->spec.name != NULL)
4030 minfo ("%s", sec->spec.name);
4031 else
4032 minfo ("*");
4033 if (sec->spec.sorted)
4034 minfo (")");
4035 if (sec->next)
4036 minfo (" ");
4037 }
4038 minfo (")");
4039
4040 print_nl ();
4041
4042 print_statement_list (w->children.head, os);
4043 }
4044
4045 /* Print a group statement. */
4046
4047 static void
4048 print_group (lang_group_statement_type *s,
4049 lang_output_section_statement_type *os)
4050 {
4051 fprintf (config.map_file, "START GROUP\n");
4052 print_statement_list (s->children.head, os);
4053 fprintf (config.map_file, "END GROUP\n");
4054 }
4055
4056 /* Print the list of statements in S.
4057 This can be called for any statement type. */
4058
4059 static void
4060 print_statement_list (lang_statement_union_type *s,
4061 lang_output_section_statement_type *os)
4062 {
4063 while (s != NULL)
4064 {
4065 print_statement (s, os);
4066 s = s->header.next;
4067 }
4068 }
4069
4070 /* Print the first statement in statement list S.
4071 This can be called for any statement type. */
4072
4073 static void
4074 print_statement (lang_statement_union_type *s,
4075 lang_output_section_statement_type *os)
4076 {
4077 switch (s->header.type)
4078 {
4079 default:
4080 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4081 FAIL ();
4082 break;
4083 case lang_constructors_statement_enum:
4084 if (constructor_list.head != NULL)
4085 {
4086 if (constructors_sorted)
4087 minfo (" SORT (CONSTRUCTORS)\n");
4088 else
4089 minfo (" CONSTRUCTORS\n");
4090 print_statement_list (constructor_list.head, os);
4091 }
4092 break;
4093 case lang_wild_statement_enum:
4094 print_wild_statement (&s->wild_statement, os);
4095 break;
4096 case lang_address_statement_enum:
4097 print_address_statement (&s->address_statement);
4098 break;
4099 case lang_object_symbols_statement_enum:
4100 minfo (" CREATE_OBJECT_SYMBOLS\n");
4101 break;
4102 case lang_fill_statement_enum:
4103 print_fill_statement (&s->fill_statement);
4104 break;
4105 case lang_data_statement_enum:
4106 print_data_statement (&s->data_statement);
4107 break;
4108 case lang_reloc_statement_enum:
4109 print_reloc_statement (&s->reloc_statement);
4110 break;
4111 case lang_input_section_enum:
4112 print_input_section (s->input_section.section);
4113 break;
4114 case lang_padding_statement_enum:
4115 print_padding_statement (&s->padding_statement);
4116 break;
4117 case lang_output_section_statement_enum:
4118 print_output_section_statement (&s->output_section_statement);
4119 break;
4120 case lang_assignment_statement_enum:
4121 print_assignment (&s->assignment_statement, os);
4122 break;
4123 case lang_target_statement_enum:
4124 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4125 break;
4126 case lang_output_statement_enum:
4127 minfo ("OUTPUT(%s", s->output_statement.name);
4128 if (output_target != NULL)
4129 minfo (" %s", output_target);
4130 minfo (")\n");
4131 break;
4132 case lang_input_statement_enum:
4133 print_input_statement (&s->input_statement);
4134 break;
4135 case lang_group_statement_enum:
4136 print_group (&s->group_statement, os);
4137 break;
4138 case lang_insert_statement_enum:
4139 minfo ("INSERT %s %s\n",
4140 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4141 s->insert_statement.where);
4142 break;
4143 }
4144 }
4145
4146 static void
4147 print_statements (void)
4148 {
4149 print_statement_list (statement_list.head, abs_output_section);
4150 }
4151
4152 /* Print the first N statements in statement list S to STDERR.
4153 If N == 0, nothing is printed.
4154 If N < 0, the entire list is printed.
4155 Intended to be called from GDB. */
4156
4157 void
4158 dprint_statement (lang_statement_union_type *s, int n)
4159 {
4160 FILE *map_save = config.map_file;
4161
4162 config.map_file = stderr;
4163
4164 if (n < 0)
4165 print_statement_list (s, abs_output_section);
4166 else
4167 {
4168 while (s && --n >= 0)
4169 {
4170 print_statement (s, abs_output_section);
4171 s = s->header.next;
4172 }
4173 }
4174
4175 config.map_file = map_save;
4176 }
4177
4178 static void
4179 insert_pad (lang_statement_union_type **ptr,
4180 fill_type *fill,
4181 unsigned int alignment_needed,
4182 asection *output_section,
4183 bfd_vma dot)
4184 {
4185 static fill_type zero_fill = { 1, { 0 } };
4186 lang_statement_union_type *pad = NULL;
4187
4188 if (ptr != &statement_list.head)
4189 pad = ((lang_statement_union_type *)
4190 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4191 if (pad != NULL
4192 && pad->header.type == lang_padding_statement_enum
4193 && pad->padding_statement.output_section == output_section)
4194 {
4195 /* Use the existing pad statement. */
4196 }
4197 else if ((pad = *ptr) != NULL
4198 && pad->header.type == lang_padding_statement_enum
4199 && pad->padding_statement.output_section == output_section)
4200 {
4201 /* Use the existing pad statement. */
4202 }
4203 else
4204 {
4205 /* Make a new padding statement, linked into existing chain. */
4206 pad = stat_alloc (sizeof (lang_padding_statement_type));
4207 pad->header.next = *ptr;
4208 *ptr = pad;
4209 pad->header.type = lang_padding_statement_enum;
4210 pad->padding_statement.output_section = output_section;
4211 if (fill == NULL)
4212 fill = &zero_fill;
4213 pad->padding_statement.fill = fill;
4214 }
4215 pad->padding_statement.output_offset = dot - output_section->vma;
4216 pad->padding_statement.size = alignment_needed;
4217 output_section->size += alignment_needed;
4218 }
4219
4220 /* Work out how much this section will move the dot point. */
4221
4222 static bfd_vma
4223 size_input_section
4224 (lang_statement_union_type **this_ptr,
4225 lang_output_section_statement_type *output_section_statement,
4226 fill_type *fill,
4227 bfd_vma dot)
4228 {
4229 lang_input_section_type *is = &((*this_ptr)->input_section);
4230 asection *i = is->section;
4231
4232 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4233 && (i->flags & SEC_EXCLUDE) == 0)
4234 {
4235 unsigned int alignment_needed;
4236 asection *o;
4237
4238 /* Align this section first to the input sections requirement,
4239 then to the output section's requirement. If this alignment
4240 is greater than any seen before, then record it too. Perform
4241 the alignment by inserting a magic 'padding' statement. */
4242
4243 if (output_section_statement->subsection_alignment != -1)
4244 i->alignment_power = output_section_statement->subsection_alignment;
4245
4246 o = output_section_statement->bfd_section;
4247 if (o->alignment_power < i->alignment_power)
4248 o->alignment_power = i->alignment_power;
4249
4250 alignment_needed = align_power (dot, i->alignment_power) - dot;
4251
4252 if (alignment_needed != 0)
4253 {
4254 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4255 dot += alignment_needed;
4256 }
4257
4258 /* Remember where in the output section this input section goes. */
4259
4260 i->output_offset = dot - o->vma;
4261
4262 /* Mark how big the output section must be to contain this now. */
4263 dot += TO_ADDR (i->size);
4264 o->size = TO_SIZE (dot - o->vma);
4265 }
4266 else
4267 {
4268 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4269 }
4270
4271 return dot;
4272 }
4273
4274 static int
4275 sort_sections_by_lma (const void *arg1, const void *arg2)
4276 {
4277 const asection *sec1 = *(const asection **) arg1;
4278 const asection *sec2 = *(const asection **) arg2;
4279
4280 if (bfd_section_lma (sec1->owner, sec1)
4281 < bfd_section_lma (sec2->owner, sec2))
4282 return -1;
4283 else if (bfd_section_lma (sec1->owner, sec1)
4284 > bfd_section_lma (sec2->owner, sec2))
4285 return 1;
4286 else if (sec1->id < sec2->id)
4287 return -1;
4288 else if (sec1->id > sec2->id)
4289 return 1;
4290
4291 return 0;
4292 }
4293
4294 #define IGNORE_SECTION(s) \
4295 ((s->flags & SEC_NEVER_LOAD) != 0 \
4296 || (s->flags & SEC_ALLOC) == 0 \
4297 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4298 && (s->flags & SEC_LOAD) == 0))
4299
4300 /* Check to see if any allocated sections overlap with other allocated
4301 sections. This can happen if a linker script specifies the output
4302 section addresses of the two sections. Also check whether any memory
4303 region has overflowed. */
4304
4305 static void
4306 lang_check_section_addresses (void)
4307 {
4308 asection *s, *os;
4309 asection **sections, **spp;
4310 unsigned int count;
4311 bfd_vma s_start;
4312 bfd_vma s_end;
4313 bfd_vma os_start;
4314 bfd_vma os_end;
4315 bfd_size_type amt;
4316 lang_memory_region_type *m;
4317
4318 if (bfd_count_sections (link_info.output_bfd) <= 1)
4319 return;
4320
4321 amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
4322 sections = xmalloc (amt);
4323
4324 /* Scan all sections in the output list. */
4325 count = 0;
4326 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
4327 {
4328 /* Only consider loadable sections with real contents. */
4329 if (IGNORE_SECTION (s) || s->size == 0)
4330 continue;
4331
4332 sections[count] = s;
4333 count++;
4334 }
4335
4336 if (count <= 1)
4337 return;
4338
4339 qsort (sections, (size_t) count, sizeof (asection *),
4340 sort_sections_by_lma);
4341
4342 spp = sections;
4343 s = *spp++;
4344 s_start = bfd_section_lma (link_info.output_bfd, s);
4345 s_end = s_start + TO_ADDR (s->size) - 1;
4346 for (count--; count; count--)
4347 {
4348 /* We must check the sections' LMA addresses not their VMA
4349 addresses because overlay sections can have overlapping VMAs
4350 but they must have distinct LMAs. */
4351 os = s;
4352 os_start = s_start;
4353 os_end = s_end;
4354 s = *spp++;
4355 s_start = bfd_section_lma (link_info.output_bfd, s);
4356 s_end = s_start + TO_ADDR (s->size) - 1;
4357
4358 /* Look for an overlap. */
4359 if (s_end >= os_start && s_start <= os_end)
4360 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4361 s->name, s_start, s_end, os->name, os_start, os_end);
4362 }
4363
4364 free (sections);
4365
4366 /* If any memory region has overflowed, report by how much.
4367 We do not issue this diagnostic for regions that had sections
4368 explicitly placed outside their bounds; os_region_check's
4369 diagnostics are adequate for that case.
4370
4371 FIXME: It is conceivable that m->current - (m->origin + m->length)
4372 might overflow a 32-bit integer. There is, alas, no way to print
4373 a bfd_vma quantity in decimal. */
4374 for (m = lang_memory_region_list; m; m = m->next)
4375 if (m->had_full_message)
4376 einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
4377 m->name, (long)(m->current - (m->origin + m->length)));
4378
4379 }
4380
4381 /* Make sure the new address is within the region. We explicitly permit the
4382 current address to be at the exact end of the region when the address is
4383 non-zero, in case the region is at the end of addressable memory and the
4384 calculation wraps around. */
4385
4386 static void
4387 os_region_check (lang_output_section_statement_type *os,
4388 lang_memory_region_type *region,
4389 etree_type *tree,
4390 bfd_vma base)
4391 {
4392 if ((region->current < region->origin
4393 || (region->current - region->origin > region->length))
4394 && ((region->current != region->origin + region->length)
4395 || base == 0))
4396 {
4397 if (tree != NULL)
4398 {
4399 einfo (_("%X%P: address 0x%v of %B section %s"
4400 " is not within region %s\n"),
4401 region->current,
4402 os->bfd_section->owner,
4403 os->bfd_section->name,
4404 region->name);
4405 }
4406 else if (!region->had_full_message)
4407 {
4408 region->had_full_message = TRUE;
4409
4410 einfo (_("%X%P: %B section %s will not fit in region %s\n"),
4411 os->bfd_section->owner,
4412 os->bfd_section->name,
4413 region->name);
4414 }
4415 }
4416 }
4417
4418 /* Set the sizes for all the output sections. */
4419
4420 static bfd_vma
4421 lang_size_sections_1
4422 (lang_statement_union_type *s,
4423 lang_output_section_statement_type *output_section_statement,
4424 lang_statement_union_type **prev,
4425 fill_type *fill,
4426 bfd_vma dot,
4427 bfd_boolean *relax,
4428 bfd_boolean check_regions)
4429 {
4430 /* Size up the sections from their constituent parts. */
4431 for (; s != NULL; s = s->header.next)
4432 {
4433 switch (s->header.type)
4434 {
4435 case lang_output_section_statement_enum:
4436 {
4437 bfd_vma newdot, after;
4438 lang_output_section_statement_type *os;
4439 lang_memory_region_type *r;
4440
4441 os = &s->output_section_statement;
4442 if (os->addr_tree != NULL)
4443 {
4444 os->processed_vma = FALSE;
4445 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4446
4447 if (expld.result.valid_p)
4448 dot = expld.result.value + expld.result.section->vma;
4449 else if (expld.phase != lang_mark_phase_enum)
4450 einfo (_("%F%S: non constant or forward reference"
4451 " address expression for section %s\n"),
4452 os->name);
4453 }
4454
4455 if (os->bfd_section == NULL)
4456 /* This section was removed or never actually created. */
4457 break;
4458
4459 /* If this is a COFF shared library section, use the size and
4460 address from the input section. FIXME: This is COFF
4461 specific; it would be cleaner if there were some other way
4462 to do this, but nothing simple comes to mind. */
4463 if (((bfd_get_flavour (link_info.output_bfd)
4464 == bfd_target_ecoff_flavour)
4465 || (bfd_get_flavour (link_info.output_bfd)
4466 == bfd_target_coff_flavour))
4467 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4468 {
4469 asection *input;
4470
4471 if (os->children.head == NULL
4472 || os->children.head->header.next != NULL
4473 || (os->children.head->header.type
4474 != lang_input_section_enum))
4475 einfo (_("%P%X: Internal error on COFF shared library"
4476 " section %s\n"), os->name);
4477
4478 input = os->children.head->input_section.section;
4479 bfd_set_section_vma (os->bfd_section->owner,
4480 os->bfd_section,
4481 bfd_section_vma (input->owner, input));
4482 os->bfd_section->size = input->size;
4483 break;
4484 }
4485
4486 newdot = dot;
4487 if (bfd_is_abs_section (os->bfd_section))
4488 {
4489 /* No matter what happens, an abs section starts at zero. */
4490 ASSERT (os->bfd_section->vma == 0);
4491 }
4492 else
4493 {
4494 int align;
4495
4496 if (os->addr_tree == NULL)
4497 {
4498 /* No address specified for this section, get one
4499 from the region specification. */
4500 if (os->region == NULL
4501 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4502 && os->region->name[0] == '*'
4503 && strcmp (os->region->name,
4504 DEFAULT_MEMORY_REGION) == 0))
4505 {
4506 os->region = lang_memory_default (os->bfd_section);
4507 }
4508
4509 /* If a loadable section is using the default memory
4510 region, and some non default memory regions were
4511 defined, issue an error message. */
4512 if (!os->ignored
4513 && !IGNORE_SECTION (os->bfd_section)
4514 && ! link_info.relocatable
4515 && check_regions
4516 && strcmp (os->region->name,
4517 DEFAULT_MEMORY_REGION) == 0
4518 && lang_memory_region_list != NULL
4519 && (strcmp (lang_memory_region_list->name,
4520 DEFAULT_MEMORY_REGION) != 0
4521 || lang_memory_region_list->next != NULL)
4522 && expld.phase != lang_mark_phase_enum)
4523 {
4524 /* By default this is an error rather than just a
4525 warning because if we allocate the section to the
4526 default memory region we can end up creating an
4527 excessively large binary, or even seg faulting when
4528 attempting to perform a negative seek. See
4529 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4530 for an example of this. This behaviour can be
4531 overridden by the using the --no-check-sections
4532 switch. */
4533 if (command_line.check_section_addresses)
4534 einfo (_("%P%F: error: no memory region specified"
4535 " for loadable section `%s'\n"),
4536 bfd_get_section_name (link_info.output_bfd,
4537 os->bfd_section));
4538 else
4539 einfo (_("%P: warning: no memory region specified"
4540 " for loadable section `%s'\n"),
4541 bfd_get_section_name (link_info.output_bfd,
4542 os->bfd_section));
4543 }
4544
4545 newdot = os->region->current;
4546 align = os->bfd_section->alignment_power;
4547 }
4548 else
4549 align = os->section_alignment;
4550
4551 /* Align to what the section needs. */
4552 if (align > 0)
4553 {
4554 bfd_vma savedot = newdot;
4555 newdot = align_power (newdot, align);
4556
4557 if (newdot != savedot
4558 && (config.warn_section_align
4559 || os->addr_tree != NULL)
4560 && expld.phase != lang_mark_phase_enum)
4561 einfo (_("%P: warning: changing start of section"
4562 " %s by %lu bytes\n"),
4563 os->name, (unsigned long) (newdot - savedot));
4564 }
4565
4566 bfd_set_section_vma (0, os->bfd_section, newdot);
4567
4568 os->bfd_section->output_offset = 0;
4569 }
4570
4571 lang_size_sections_1 (os->children.head, os, &os->children.head,
4572 os->fill, newdot, relax, check_regions);
4573
4574 os->processed_vma = TRUE;
4575
4576 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4577 /* Except for some special linker created sections,
4578 no output section should change from zero size
4579 after strip_excluded_output_sections. A non-zero
4580 size on an ignored section indicates that some
4581 input section was not sized early enough. */
4582 ASSERT (os->bfd_section->size == 0);
4583 else
4584 {
4585 dot = os->bfd_section->vma;
4586
4587 /* Put the section within the requested block size, or
4588 align at the block boundary. */
4589 after = ((dot
4590 + TO_ADDR (os->bfd_section->size)
4591 + os->block_value - 1)
4592 & - (bfd_vma) os->block_value);
4593
4594 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4595 }
4596
4597 /* Set section lma. */
4598 r = os->region;
4599 if (r == NULL)
4600 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4601
4602 if (os->load_base)
4603 {
4604 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4605 os->bfd_section->lma = lma;
4606 }
4607 else if (os->lma_region != NULL)
4608 {
4609 bfd_vma lma = os->lma_region->current;
4610
4611 if (os->section_alignment != -1)
4612 lma = align_power (lma, os->section_alignment);
4613 os->bfd_section->lma = lma;
4614 }
4615 else if (r->last_os != NULL
4616 && (os->bfd_section->flags & SEC_ALLOC) != 0)
4617 {
4618 bfd_vma lma;
4619 asection *last;
4620
4621 last = r->last_os->output_section_statement.bfd_section;
4622
4623 /* A backwards move of dot should be accompanied by
4624 an explicit assignment to the section LMA (ie.
4625 os->load_base set) because backwards moves can
4626 create overlapping LMAs. */
4627 if (dot < last->vma
4628 && os->bfd_section->size != 0
4629 && dot + os->bfd_section->size <= last->vma)
4630 {
4631 /* If dot moved backwards then leave lma equal to
4632 vma. This is the old default lma, which might
4633 just happen to work when the backwards move is
4634 sufficiently large. Nag if this changes anything,
4635 so people can fix their linker scripts. */
4636
4637 if (last->vma != last->lma)
4638 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4639 os->name);
4640 }
4641 else
4642 {
4643 /* If this is an overlay, set the current lma to that
4644 at the end of the previous section. */
4645 if (os->sectype == overlay_section)
4646 lma = last->lma + last->size;
4647
4648 /* Otherwise, keep the same lma to vma relationship
4649 as the previous section. */
4650 else
4651 lma = dot + last->lma - last->vma;
4652
4653 if (os->section_alignment != -1)
4654 lma = align_power (lma, os->section_alignment);
4655 os->bfd_section->lma = lma;
4656 }
4657 }
4658 os->processed_lma = TRUE;
4659
4660 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4661 break;
4662
4663 /* Keep track of normal sections using the default
4664 lma region. We use this to set the lma for
4665 following sections. Overlays or other linker
4666 script assignment to lma might mean that the
4667 default lma == vma is incorrect.
4668 To avoid warnings about dot moving backwards when using
4669 -Ttext, don't start tracking sections until we find one
4670 of non-zero size or with lma set differently to vma. */
4671 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4672 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
4673 && (os->bfd_section->flags & SEC_ALLOC) != 0
4674 && (os->bfd_section->size != 0
4675 || (r->last_os == NULL
4676 && os->bfd_section->vma != os->bfd_section->lma)
4677 || (r->last_os != NULL
4678 && dot >= (r->last_os->output_section_statement
4679 .bfd_section->vma)))
4680 && os->lma_region == NULL
4681 && !link_info.relocatable)
4682 r->last_os = s;
4683
4684 /* .tbss sections effectively have zero size. */
4685 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4686 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4687 || link_info.relocatable)
4688 dot += TO_ADDR (os->bfd_section->size);
4689
4690 if (os->update_dot_tree != 0)
4691 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4692
4693 /* Update dot in the region ?
4694 We only do this if the section is going to be allocated,
4695 since unallocated sections do not contribute to the region's
4696 overall size in memory.
4697
4698 If the SEC_NEVER_LOAD bit is not set, it will affect the
4699 addresses of sections after it. We have to update
4700 dot. */
4701 if (os->region != NULL
4702 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4703 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4704 {
4705 os->region->current = dot;
4706
4707 if (check_regions)
4708 /* Make sure the new address is within the region. */
4709 os_region_check (os, os->region, os->addr_tree,
4710 os->bfd_section->vma);
4711
4712 if (os->lma_region != NULL && os->lma_region != os->region
4713 && (os->bfd_section->flags & SEC_LOAD))
4714 {
4715 os->lma_region->current
4716 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
4717
4718 if (check_regions)
4719 os_region_check (os, os->lma_region, NULL,
4720 os->bfd_section->lma);
4721 }
4722 }
4723 }
4724 break;
4725
4726 case lang_constructors_statement_enum:
4727 dot = lang_size_sections_1 (constructor_list.head,
4728 output_section_statement,
4729 &s->wild_statement.children.head,
4730 fill, dot, relax, check_regions);
4731 break;
4732
4733 case lang_data_statement_enum:
4734 {
4735 unsigned int size = 0;
4736
4737 s->data_statement.output_offset =
4738 dot - output_section_statement->bfd_section->vma;
4739 s->data_statement.output_section =
4740 output_section_statement->bfd_section;
4741
4742 /* We might refer to provided symbols in the expression, and
4743 need to mark them as needed. */
4744 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4745
4746 switch (s->data_statement.type)
4747 {
4748 default:
4749 abort ();
4750 case QUAD:
4751 case SQUAD:
4752 size = QUAD_SIZE;
4753 break;
4754 case LONG:
4755 size = LONG_SIZE;
4756 break;
4757 case SHORT:
4758 size = SHORT_SIZE;
4759 break;
4760 case BYTE:
4761 size = BYTE_SIZE;
4762 break;
4763 }
4764 if (size < TO_SIZE ((unsigned) 1))
4765 size = TO_SIZE ((unsigned) 1);
4766 dot += TO_ADDR (size);
4767 output_section_statement->bfd_section->size += size;
4768 }
4769 break;
4770
4771 case lang_reloc_statement_enum:
4772 {
4773 int size;
4774
4775 s->reloc_statement.output_offset =
4776 dot - output_section_statement->bfd_section->vma;
4777 s->reloc_statement.output_section =
4778 output_section_statement->bfd_section;
4779 size = bfd_get_reloc_size (s->reloc_statement.howto);
4780 dot += TO_ADDR (size);
4781 output_section_statement->bfd_section->size += size;
4782 }
4783 break;
4784
4785 case lang_wild_statement_enum:
4786 dot = lang_size_sections_1 (s->wild_statement.children.head,
4787 output_section_statement,
4788 &s->wild_statement.children.head,
4789 fill, dot, relax, check_regions);
4790 break;
4791
4792 case lang_object_symbols_statement_enum:
4793 link_info.create_object_symbols_section =
4794 output_section_statement->bfd_section;
4795 break;
4796
4797 case lang_output_statement_enum:
4798 case lang_target_statement_enum:
4799 break;
4800
4801 case lang_input_section_enum:
4802 {
4803 asection *i;
4804
4805 i = (*prev)->input_section.section;
4806 if (relax)
4807 {
4808 bfd_boolean again;
4809
4810 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4811 einfo (_("%P%F: can't relax section: %E\n"));
4812 if (again)
4813 *relax = TRUE;
4814 }
4815 dot = size_input_section (prev, output_section_statement,
4816 output_section_statement->fill, dot);
4817 }
4818 break;
4819
4820 case lang_input_statement_enum:
4821 break;
4822
4823 case lang_fill_statement_enum:
4824 s->fill_statement.output_section =
4825 output_section_statement->bfd_section;
4826
4827 fill = s->fill_statement.fill;
4828 break;
4829
4830 case lang_assignment_statement_enum:
4831 {
4832 bfd_vma newdot = dot;
4833 etree_type *tree = s->assignment_statement.exp;
4834
4835 expld.dataseg.relro = exp_dataseg_relro_none;
4836
4837 exp_fold_tree (tree,
4838 output_section_statement->bfd_section,
4839 &newdot);
4840
4841 if (expld.dataseg.relro == exp_dataseg_relro_start)
4842 {
4843 if (!expld.dataseg.relro_start_stat)
4844 expld.dataseg.relro_start_stat = s;
4845 else
4846 {
4847 ASSERT (expld.dataseg.relro_start_stat == s);
4848 }
4849 }
4850 else if (expld.dataseg.relro == exp_dataseg_relro_end)
4851 {
4852 if (!expld.dataseg.relro_end_stat)
4853 expld.dataseg.relro_end_stat = s;
4854 else
4855 {
4856 ASSERT (expld.dataseg.relro_end_stat == s);
4857 }
4858 }
4859 expld.dataseg.relro = exp_dataseg_relro_none;
4860
4861 /* This symbol is relative to this section. */
4862 if ((tree->type.node_class == etree_provided
4863 || tree->type.node_class == etree_assign)
4864 && (tree->assign.dst [0] != '.'
4865 || tree->assign.dst [1] != '\0'))
4866 output_section_statement->section_relative_symbol = 1;
4867
4868 if (!output_section_statement->ignored)
4869 {
4870 if (output_section_statement == abs_output_section)
4871 {
4872 /* If we don't have an output section, then just adjust
4873 the default memory address. */
4874 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4875 FALSE)->current = newdot;
4876 }
4877 else if (newdot != dot)
4878 {
4879 /* Insert a pad after this statement. We can't
4880 put the pad before when relaxing, in case the
4881 assignment references dot. */
4882 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4883 output_section_statement->bfd_section, dot);
4884
4885 /* Don't neuter the pad below when relaxing. */
4886 s = s->header.next;
4887
4888 /* If dot is advanced, this implies that the section
4889 should have space allocated to it, unless the
4890 user has explicitly stated that the section
4891 should never be loaded. */
4892 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
4893 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4894 }
4895 dot = newdot;
4896 }
4897 }
4898 break;
4899
4900 case lang_padding_statement_enum:
4901 /* If this is the first time lang_size_sections is called,
4902 we won't have any padding statements. If this is the
4903 second or later passes when relaxing, we should allow
4904 padding to shrink. If padding is needed on this pass, it
4905 will be added back in. */
4906 s->padding_statement.size = 0;
4907
4908 /* Make sure output_offset is valid. If relaxation shrinks
4909 the section and this pad isn't needed, it's possible to
4910 have output_offset larger than the final size of the
4911 section. bfd_set_section_contents will complain even for
4912 a pad size of zero. */
4913 s->padding_statement.output_offset
4914 = dot - output_section_statement->bfd_section->vma;
4915 break;
4916
4917 case lang_group_statement_enum:
4918 dot = lang_size_sections_1 (s->group_statement.children.head,
4919 output_section_statement,
4920 &s->group_statement.children.head,
4921 fill, dot, relax, check_regions);
4922 break;
4923
4924 case lang_insert_statement_enum:
4925 break;
4926
4927 /* We can only get here when relaxing is turned on. */
4928 case lang_address_statement_enum:
4929 break;
4930
4931 default:
4932 FAIL ();
4933 break;
4934 }
4935 prev = &s->header.next;
4936 }
4937 return dot;
4938 }
4939
4940 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
4941 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
4942 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
4943 segments. We are allowed an opportunity to override this decision. */
4944
4945 bfd_boolean
4946 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
4947 bfd * abfd ATTRIBUTE_UNUSED,
4948 asection * current_section,
4949 asection * previous_section,
4950 bfd_boolean new_segment)
4951 {
4952 lang_output_section_statement_type * cur;
4953 lang_output_section_statement_type * prev;
4954
4955 /* The checks below are only necessary when the BFD library has decided
4956 that the two sections ought to be placed into the same segment. */
4957 if (new_segment)
4958 return TRUE;
4959
4960 /* Paranoia checks. */
4961 if (current_section == NULL || previous_section == NULL)
4962 return new_segment;
4963
4964 /* Find the memory regions associated with the two sections.
4965 We call lang_output_section_find() here rather than scanning the list
4966 of output sections looking for a matching section pointer because if
4967 we have a large number of sections then a hash lookup is faster. */
4968 cur = lang_output_section_find (current_section->name);
4969 prev = lang_output_section_find (previous_section->name);
4970
4971 /* More paranoia. */
4972 if (cur == NULL || prev == NULL)
4973 return new_segment;
4974
4975 /* If the regions are different then force the sections to live in
4976 different segments. See the email thread starting at the following
4977 URL for the reasons why this is necessary:
4978 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
4979 return cur->region != prev->region;
4980 }
4981
4982 void
4983 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
4984 {
4985 lang_statement_iteration++;
4986 lang_size_sections_1 (statement_list.head, abs_output_section,
4987 &statement_list.head, 0, 0, relax, check_regions);
4988 }
4989
4990 void
4991 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4992 {
4993 expld.phase = lang_allocating_phase_enum;
4994 expld.dataseg.phase = exp_dataseg_none;
4995
4996 one_lang_size_sections_pass (relax, check_regions);
4997 if (expld.dataseg.phase == exp_dataseg_end_seen
4998 && link_info.relro && expld.dataseg.relro_end)
4999 {
5000 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5001 to put expld.dataseg.relro on a (common) page boundary. */
5002 bfd_vma old_min_base, relro_end, maxpage;
5003
5004 expld.dataseg.phase = exp_dataseg_relro_adjust;
5005 old_min_base = expld.dataseg.min_base;
5006 maxpage = expld.dataseg.maxpagesize;
5007 expld.dataseg.base += (-expld.dataseg.relro_end
5008 & (expld.dataseg.pagesize - 1));
5009 /* Compute the expected PT_GNU_RELRO segment end. */
5010 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
5011 & ~(expld.dataseg.pagesize - 1);
5012 if (old_min_base + maxpage < expld.dataseg.base)
5013 {
5014 expld.dataseg.base -= maxpage;
5015 relro_end -= maxpage;
5016 }
5017 lang_reset_memory_regions ();
5018 one_lang_size_sections_pass (relax, check_regions);
5019 if (expld.dataseg.relro_end > relro_end)
5020 {
5021 /* The alignment of sections between DATA_SEGMENT_ALIGN
5022 and DATA_SEGMENT_RELRO_END caused huge padding to be
5023 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
5024 asection *sec;
5025 unsigned int max_alignment_power = 0;
5026
5027 /* Find maximum alignment power of sections between
5028 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5029 for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
5030 if (sec->vma >= expld.dataseg.base
5031 && sec->vma < expld.dataseg.relro_end
5032 && sec->alignment_power > max_alignment_power)
5033 max_alignment_power = sec->alignment_power;
5034
5035 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
5036 {
5037 if (expld.dataseg.base - (1 << max_alignment_power)
5038 < old_min_base)
5039 expld.dataseg.base += expld.dataseg.pagesize;
5040 expld.dataseg.base -= (1 << max_alignment_power);
5041 lang_reset_memory_regions ();
5042 one_lang_size_sections_pass (relax, check_regions);
5043 }
5044 }
5045 link_info.relro_start = expld.dataseg.base;
5046 link_info.relro_end = expld.dataseg.relro_end;
5047 }
5048 else if (expld.dataseg.phase == exp_dataseg_end_seen)
5049 {
5050 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5051 a page could be saved in the data segment. */
5052 bfd_vma first, last;
5053
5054 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5055 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
5056 if (first && last
5057 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5058 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5059 && first + last <= expld.dataseg.pagesize)
5060 {
5061 expld.dataseg.phase = exp_dataseg_adjust;
5062 lang_reset_memory_regions ();
5063 one_lang_size_sections_pass (relax, check_regions);
5064 }
5065 }
5066
5067 expld.phase = lang_final_phase_enum;
5068 }
5069
5070 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5071
5072 static bfd_vma
5073 lang_do_assignments_1 (lang_statement_union_type *s,
5074 lang_output_section_statement_type *current_os,
5075 fill_type *fill,
5076 bfd_vma dot)
5077 {
5078 for (; s != NULL; s = s->header.next)
5079 {
5080 switch (s->header.type)
5081 {
5082 case lang_constructors_statement_enum:
5083 dot = lang_do_assignments_1 (constructor_list.head,
5084 current_os, fill, dot);
5085 break;
5086
5087 case lang_output_section_statement_enum:
5088 {
5089 lang_output_section_statement_type *os;
5090
5091 os = &(s->output_section_statement);
5092 if (os->bfd_section != NULL && !os->ignored)
5093 {
5094 dot = os->bfd_section->vma;
5095
5096 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
5097
5098 /* .tbss sections effectively have zero size. */
5099 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5100 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5101 || link_info.relocatable)
5102 dot += TO_ADDR (os->bfd_section->size);
5103 }
5104 }
5105 break;
5106
5107 case lang_wild_statement_enum:
5108
5109 dot = lang_do_assignments_1 (s->wild_statement.children.head,
5110 current_os, fill, dot);
5111 break;
5112
5113 case lang_object_symbols_statement_enum:
5114 case lang_output_statement_enum:
5115 case lang_target_statement_enum:
5116 break;
5117
5118 case lang_data_statement_enum:
5119 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5120 if (expld.result.valid_p)
5121 s->data_statement.value = (expld.result.value
5122 + expld.result.section->vma);
5123 else
5124 einfo (_("%F%P: invalid data statement\n"));
5125 {
5126 unsigned int size;
5127 switch (s->data_statement.type)
5128 {
5129 default:
5130 abort ();
5131 case QUAD:
5132 case SQUAD:
5133 size = QUAD_SIZE;
5134 break;
5135 case LONG:
5136 size = LONG_SIZE;
5137 break;
5138 case SHORT:
5139 size = SHORT_SIZE;
5140 break;
5141 case BYTE:
5142 size = BYTE_SIZE;
5143 break;
5144 }
5145 if (size < TO_SIZE ((unsigned) 1))
5146 size = TO_SIZE ((unsigned) 1);
5147 dot += TO_ADDR (size);
5148 }
5149 break;
5150
5151 case lang_reloc_statement_enum:
5152 exp_fold_tree (s->reloc_statement.addend_exp,
5153 bfd_abs_section_ptr, &dot);
5154 if (expld.result.valid_p)
5155 s->reloc_statement.addend_value = expld.result.value;
5156 else
5157 einfo (_("%F%P: invalid reloc statement\n"));
5158 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
5159 break;
5160
5161 case lang_input_section_enum:
5162 {
5163 asection *in = s->input_section.section;
5164
5165 if ((in->flags & SEC_EXCLUDE) == 0)
5166 dot += TO_ADDR (in->size);
5167 }
5168 break;
5169
5170 case lang_input_statement_enum:
5171 break;
5172
5173 case lang_fill_statement_enum:
5174 fill = s->fill_statement.fill;
5175 break;
5176
5177 case lang_assignment_statement_enum:
5178 exp_fold_tree (s->assignment_statement.exp,
5179 current_os->bfd_section,
5180 &dot);
5181 break;
5182
5183 case lang_padding_statement_enum:
5184 dot += TO_ADDR (s->padding_statement.size);
5185 break;
5186
5187 case lang_group_statement_enum:
5188 dot = lang_do_assignments_1 (s->group_statement.children.head,
5189 current_os, fill, dot);
5190 break;
5191
5192 case lang_insert_statement_enum:
5193 break;
5194
5195 case lang_address_statement_enum:
5196 break;
5197
5198 default:
5199 FAIL ();
5200 break;
5201 }
5202 }
5203 return dot;
5204 }
5205
5206 void
5207 lang_do_assignments (void)
5208 {
5209 lang_statement_iteration++;
5210 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
5211 }
5212
5213 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5214 operator .startof. (section_name), it produces an undefined symbol
5215 .startof.section_name. Similarly, when it sees
5216 .sizeof. (section_name), it produces an undefined symbol
5217 .sizeof.section_name. For all the output sections, we look for
5218 such symbols, and set them to the correct value. */
5219
5220 static void
5221 lang_set_startof (void)
5222 {
5223 asection *s;
5224
5225 if (link_info.relocatable)
5226 return;
5227
5228 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5229 {
5230 const char *secname;
5231 char *buf;
5232 struct bfd_link_hash_entry *h;
5233
5234 secname = bfd_get_section_name (link_info.output_bfd, s);
5235 buf = xmalloc (10 + strlen (secname));
5236
5237 sprintf (buf, ".startof.%s", secname);
5238 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5239 if (h != NULL && h->type == bfd_link_hash_undefined)
5240 {
5241 h->type = bfd_link_hash_defined;
5242 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s);
5243 h->u.def.section = bfd_abs_section_ptr;
5244 }
5245
5246 sprintf (buf, ".sizeof.%s", secname);
5247 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5248 if (h != NULL && h->type == bfd_link_hash_undefined)
5249 {
5250 h->type = bfd_link_hash_defined;
5251 h->u.def.value = TO_ADDR (s->size);
5252 h->u.def.section = bfd_abs_section_ptr;
5253 }
5254
5255 free (buf);
5256 }
5257 }
5258
5259 static void
5260 lang_end (void)
5261 {
5262 struct bfd_link_hash_entry *h;
5263 bfd_boolean warn;
5264
5265 if ((link_info.relocatable && !link_info.gc_sections)
5266 || link_info.shared)
5267 warn = entry_from_cmdline;
5268 else
5269 warn = TRUE;
5270
5271 /* Force the user to specify a root when generating a relocatable with
5272 --gc-sections. */
5273 if (link_info.gc_sections && link_info.relocatable
5274 && (entry_symbol.name == NULL
5275 && ldlang_undef_chain_list_head == NULL))
5276 einfo (_("%P%F: gc-sections requires either an entry or "
5277 "an undefined symbol\n"));
5278
5279 if (entry_symbol.name == NULL)
5280 {
5281 /* No entry has been specified. Look for the default entry, but
5282 don't warn if we don't find it. */
5283 entry_symbol.name = entry_symbol_default;
5284 warn = FALSE;
5285 }
5286
5287 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
5288 FALSE, FALSE, TRUE);
5289 if (h != NULL
5290 && (h->type == bfd_link_hash_defined
5291 || h->type == bfd_link_hash_defweak)
5292 && h->u.def.section->output_section != NULL)
5293 {
5294 bfd_vma val;
5295
5296 val = (h->u.def.value
5297 + bfd_get_section_vma (link_info.output_bfd,
5298 h->u.def.section->output_section)
5299 + h->u.def.section->output_offset);
5300 if (! bfd_set_start_address (link_info.output_bfd, val))
5301 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
5302 }
5303 else
5304 {
5305 bfd_vma val;
5306 const char *send;
5307
5308 /* We couldn't find the entry symbol. Try parsing it as a
5309 number. */
5310 val = bfd_scan_vma (entry_symbol.name, &send, 0);
5311 if (*send == '\0')
5312 {
5313 if (! bfd_set_start_address (link_info.output_bfd, val))
5314 einfo (_("%P%F: can't set start address\n"));
5315 }
5316 else
5317 {
5318 asection *ts;
5319
5320 /* Can't find the entry symbol, and it's not a number. Use
5321 the first address in the text section. */
5322 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
5323 if (ts != NULL)
5324 {
5325 if (warn)
5326 einfo (_("%P: warning: cannot find entry symbol %s;"
5327 " defaulting to %V\n"),
5328 entry_symbol.name,
5329 bfd_get_section_vma (link_info.output_bfd, ts));
5330 if (!(bfd_set_start_address
5331 (link_info.output_bfd,
5332 bfd_get_section_vma (link_info.output_bfd, ts))))
5333 einfo (_("%P%F: can't set start address\n"));
5334 }
5335 else
5336 {
5337 if (warn)
5338 einfo (_("%P: warning: cannot find entry symbol %s;"
5339 " not setting start address\n"),
5340 entry_symbol.name);
5341 }
5342 }
5343 }
5344
5345 /* Don't bfd_hash_table_free (&lang_definedness_table);
5346 map file output may result in a call of lang_track_definedness. */
5347 }
5348
5349 /* This is a small function used when we want to ignore errors from
5350 BFD. */
5351
5352 static void
5353 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5354 {
5355 /* Don't do anything. */
5356 }
5357
5358 /* Check that the architecture of all the input files is compatible
5359 with the output file. Also call the backend to let it do any
5360 other checking that is needed. */
5361
5362 static void
5363 lang_check (void)
5364 {
5365 lang_statement_union_type *file;
5366 bfd *input_bfd;
5367 const bfd_arch_info_type *compatible;
5368
5369 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5370 {
5371 input_bfd = file->input_statement.the_bfd;
5372 compatible
5373 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
5374 command_line.accept_unknown_input_arch);
5375
5376 /* In general it is not possible to perform a relocatable
5377 link between differing object formats when the input
5378 file has relocations, because the relocations in the
5379 input format may not have equivalent representations in
5380 the output format (and besides BFD does not translate
5381 relocs for other link purposes than a final link). */
5382 if ((link_info.relocatable || link_info.emitrelocations)
5383 && (compatible == NULL
5384 || (bfd_get_flavour (input_bfd)
5385 != bfd_get_flavour (link_info.output_bfd)))
5386 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5387 {
5388 einfo (_("%P%F: Relocatable linking with relocations from"
5389 " format %s (%B) to format %s (%B) is not supported\n"),
5390 bfd_get_target (input_bfd), input_bfd,
5391 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
5392 /* einfo with %F exits. */
5393 }
5394
5395 if (compatible == NULL)
5396 {
5397 if (command_line.warn_mismatch)
5398 einfo (_("%P%X: %s architecture of input file `%B'"
5399 " is incompatible with %s output\n"),
5400 bfd_printable_name (input_bfd), input_bfd,
5401 bfd_printable_name (link_info.output_bfd));
5402 }
5403 else if (bfd_count_sections (input_bfd))
5404 {
5405 /* If the input bfd has no contents, it shouldn't set the
5406 private data of the output bfd. */
5407
5408 bfd_error_handler_type pfn = NULL;
5409
5410 /* If we aren't supposed to warn about mismatched input
5411 files, temporarily set the BFD error handler to a
5412 function which will do nothing. We still want to call
5413 bfd_merge_private_bfd_data, since it may set up
5414 information which is needed in the output file. */
5415 if (! command_line.warn_mismatch)
5416 pfn = bfd_set_error_handler (ignore_bfd_errors);
5417 if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
5418 {
5419 if (command_line.warn_mismatch)
5420 einfo (_("%P%X: failed to merge target specific data"
5421 " of file %B\n"), input_bfd);
5422 }
5423 if (! command_line.warn_mismatch)
5424 bfd_set_error_handler (pfn);
5425 }
5426 }
5427 }
5428
5429 /* Look through all the global common symbols and attach them to the
5430 correct section. The -sort-common command line switch may be used
5431 to roughly sort the entries by alignment. */
5432
5433 static void
5434 lang_common (void)
5435 {
5436 if (command_line.inhibit_common_definition)
5437 return;
5438 if (link_info.relocatable
5439 && ! command_line.force_common_definition)
5440 return;
5441
5442 if (! config.sort_common)
5443 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5444 else
5445 {
5446 unsigned int power;
5447
5448 if (config.sort_common == sort_descending)
5449 {
5450 for (power = 4; power > 0; power--)
5451 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5452
5453 power = 0;
5454 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5455 }
5456 else
5457 {
5458 for (power = 0; power <= 4; power++)
5459 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5460
5461 power = UINT_MAX;
5462 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5463 }
5464 }
5465 }
5466
5467 /* Place one common symbol in the correct section. */
5468
5469 static bfd_boolean
5470 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5471 {
5472 unsigned int power_of_two;
5473 bfd_vma size;
5474 asection *section;
5475
5476 if (h->type != bfd_link_hash_common)
5477 return TRUE;
5478
5479 size = h->u.c.size;
5480 power_of_two = h->u.c.p->alignment_power;
5481
5482 if (config.sort_common == sort_descending
5483 && power_of_two < *(unsigned int *) info)
5484 return TRUE;
5485 else if (config.sort_common == sort_ascending
5486 && power_of_two > *(unsigned int *) info)
5487 return TRUE;
5488
5489 section = h->u.c.p->section;
5490
5491 /* Increase the size of the section to align the common sym. */
5492 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5493 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
5494
5495 /* Adjust the alignment if necessary. */
5496 if (power_of_two > section->alignment_power)
5497 section->alignment_power = power_of_two;
5498
5499 /* Change the symbol from common to defined. */
5500 h->type = bfd_link_hash_defined;
5501 h->u.def.section = section;
5502 h->u.def.value = section->size;
5503
5504 /* Increase the size of the section. */
5505 section->size += size;
5506
5507 /* Make sure the section is allocated in memory, and make sure that
5508 it is no longer a common section. */
5509 section->flags |= SEC_ALLOC;
5510 section->flags &= ~SEC_IS_COMMON;
5511
5512 if (config.map_file != NULL)
5513 {
5514 static bfd_boolean header_printed;
5515 int len;
5516 char *name;
5517 char buf[50];
5518
5519 if (! header_printed)
5520 {
5521 minfo (_("\nAllocating common symbols\n"));
5522 minfo (_("Common symbol size file\n\n"));
5523 header_printed = TRUE;
5524 }
5525
5526 name = bfd_demangle (link_info.output_bfd, h->root.string,
5527 DMGL_ANSI | DMGL_PARAMS);
5528 if (name == NULL)
5529 {
5530 minfo ("%s", h->root.string);
5531 len = strlen (h->root.string);
5532 }
5533 else
5534 {
5535 minfo ("%s", name);
5536 len = strlen (name);
5537 free (name);
5538 }
5539
5540 if (len >= 19)
5541 {
5542 print_nl ();
5543 len = 0;
5544 }
5545 while (len < 20)
5546 {
5547 print_space ();
5548 ++len;
5549 }
5550
5551 minfo ("0x");
5552 if (size <= 0xffffffff)
5553 sprintf (buf, "%lx", (unsigned long) size);
5554 else
5555 sprintf_vma (buf, size);
5556 minfo ("%s", buf);
5557 len = strlen (buf);
5558
5559 while (len < 16)
5560 {
5561 print_space ();
5562 ++len;
5563 }
5564
5565 minfo ("%B\n", section->owner);
5566 }
5567
5568 return TRUE;
5569 }
5570
5571 /* Run through the input files and ensure that every input section has
5572 somewhere to go. If one is found without a destination then create
5573 an input request and place it into the statement tree. */
5574
5575 static void
5576 lang_place_orphans (void)
5577 {
5578 LANG_FOR_EACH_INPUT_STATEMENT (file)
5579 {
5580 asection *s;
5581
5582 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5583 {
5584 if (s->output_section == NULL)
5585 {
5586 /* This section of the file is not attached, root
5587 around for a sensible place for it to go. */
5588
5589 if (file->just_syms_flag)
5590 bfd_link_just_syms (file->the_bfd, s, &link_info);
5591 else if ((s->flags & SEC_EXCLUDE) != 0)
5592 s->output_section = bfd_abs_section_ptr;
5593 else if (strcmp (s->name, "COMMON") == 0)
5594 {
5595 /* This is a lonely common section which must have
5596 come from an archive. We attach to the section
5597 with the wildcard. */
5598 if (! link_info.relocatable
5599 || command_line.force_common_definition)
5600 {
5601 if (default_common_section == NULL)
5602 {
5603 default_common_section =
5604 lang_output_section_statement_lookup (".bss");
5605
5606 }
5607 lang_add_section (&default_common_section->children, s,
5608 default_common_section);
5609 }
5610 }
5611 else if (ldemul_place_orphan (s))
5612 ;
5613 else
5614 {
5615 lang_output_section_statement_type *os;
5616
5617 os = lang_output_section_statement_lookup (s->name);
5618 lang_add_section (&os->children, s, os);
5619 }
5620 }
5621 }
5622 }
5623 }
5624
5625 void
5626 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5627 {
5628 flagword *ptr_flags;
5629
5630 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5631 while (*flags)
5632 {
5633 switch (*flags)
5634 {
5635 case 'A': case 'a':
5636 *ptr_flags |= SEC_ALLOC;
5637 break;
5638
5639 case 'R': case 'r':
5640 *ptr_flags |= SEC_READONLY;
5641 break;
5642
5643 case 'W': case 'w':
5644 *ptr_flags |= SEC_DATA;
5645 break;
5646
5647 case 'X': case 'x':
5648 *ptr_flags |= SEC_CODE;
5649 break;
5650
5651 case 'L': case 'l':
5652 case 'I': case 'i':
5653 *ptr_flags |= SEC_LOAD;
5654 break;
5655
5656 default:
5657 einfo (_("%P%F: invalid syntax in flags\n"));
5658 break;
5659 }
5660 flags++;
5661 }
5662 }
5663
5664 /* Call a function on each input file. This function will be called
5665 on an archive, but not on the elements. */
5666
5667 void
5668 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5669 {
5670 lang_input_statement_type *f;
5671
5672 for (f = (lang_input_statement_type *) input_file_chain.head;
5673 f != NULL;
5674 f = (lang_input_statement_type *) f->next_real_file)
5675 func (f);
5676 }
5677
5678 /* Call a function on each file. The function will be called on all
5679 the elements of an archive which are included in the link, but will
5680 not be called on the archive file itself. */
5681
5682 void
5683 lang_for_each_file (void (*func) (lang_input_statement_type *))
5684 {
5685 LANG_FOR_EACH_INPUT_STATEMENT (f)
5686 {
5687 func (f);
5688 }
5689 }
5690
5691 void
5692 ldlang_add_file (lang_input_statement_type *entry)
5693 {
5694 lang_statement_append (&file_chain,
5695 (lang_statement_union_type *) entry,
5696 &entry->next);
5697
5698 /* The BFD linker needs to have a list of all input BFDs involved in
5699 a link. */
5700 ASSERT (entry->the_bfd->link_next == NULL);
5701 ASSERT (entry->the_bfd != link_info.output_bfd);
5702
5703 *link_info.input_bfds_tail = entry->the_bfd;
5704 link_info.input_bfds_tail = &entry->the_bfd->link_next;
5705 entry->the_bfd->usrdata = entry;
5706 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5707
5708 /* Look through the sections and check for any which should not be
5709 included in the link. We need to do this now, so that we can
5710 notice when the backend linker tries to report multiple
5711 definition errors for symbols which are in sections we aren't
5712 going to link. FIXME: It might be better to entirely ignore
5713 symbols which are defined in sections which are going to be
5714 discarded. This would require modifying the backend linker for
5715 each backend which might set the SEC_LINK_ONCE flag. If we do
5716 this, we should probably handle SEC_EXCLUDE in the same way. */
5717
5718 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5719 }
5720
5721 void
5722 lang_add_output (const char *name, int from_script)
5723 {
5724 /* Make -o on command line override OUTPUT in script. */
5725 if (!had_output_filename || !from_script)
5726 {
5727 output_filename = name;
5728 had_output_filename = TRUE;
5729 }
5730 }
5731
5732 static lang_output_section_statement_type *current_section;
5733
5734 static int
5735 topower (int x)
5736 {
5737 unsigned int i = 1;
5738 int l;
5739
5740 if (x < 0)
5741 return -1;
5742
5743 for (l = 0; l < 32; l++)
5744 {
5745 if (i >= (unsigned int) x)
5746 return l;
5747 i <<= 1;
5748 }
5749
5750 return 0;
5751 }
5752
5753 lang_output_section_statement_type *
5754 lang_enter_output_section_statement (const char *output_section_statement_name,
5755 etree_type *address_exp,
5756 enum section_type sectype,
5757 etree_type *align,
5758 etree_type *subalign,
5759 etree_type *ebase,
5760 int constraint)
5761 {
5762 lang_output_section_statement_type *os;
5763
5764 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5765 constraint);
5766 current_section = os;
5767
5768 /* Make next things chain into subchain of this. */
5769
5770 if (os->addr_tree == NULL)
5771 {
5772 os->addr_tree = address_exp;
5773 }
5774 os->sectype = sectype;
5775 if (sectype != noload_section)
5776 os->flags = SEC_NO_FLAGS;
5777 else
5778 os->flags = SEC_NEVER_LOAD;
5779 os->block_value = 1;
5780 stat_ptr = &os->children;
5781
5782 os->subsection_alignment =
5783 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5784 os->section_alignment =
5785 topower (exp_get_value_int (align, -1, "section alignment"));
5786
5787 os->load_base = ebase;
5788 return os;
5789 }
5790
5791 void
5792 lang_final (void)
5793 {
5794 lang_output_statement_type *new;
5795
5796 new = new_stat (lang_output_statement, stat_ptr);
5797 new->name = output_filename;
5798 }
5799
5800 /* Reset the current counters in the regions. */
5801
5802 void
5803 lang_reset_memory_regions (void)
5804 {
5805 lang_memory_region_type *p = lang_memory_region_list;
5806 asection *o;
5807 lang_output_section_statement_type *os;
5808
5809 for (p = lang_memory_region_list; p != NULL; p = p->next)
5810 {
5811 p->current = p->origin;
5812 p->last_os = NULL;
5813 }
5814
5815 for (os = &lang_output_section_statement.head->output_section_statement;
5816 os != NULL;
5817 os = os->next)
5818 {
5819 os->processed_vma = FALSE;
5820 os->processed_lma = FALSE;
5821 }
5822
5823 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
5824 {
5825 /* Save the last size for possible use by bfd_relax_section. */
5826 o->rawsize = o->size;
5827 o->size = 0;
5828 }
5829 }
5830
5831 /* Worker for lang_gc_sections_1. */
5832
5833 static void
5834 gc_section_callback (lang_wild_statement_type *ptr,
5835 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5836 asection *section,
5837 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5838 void *data ATTRIBUTE_UNUSED)
5839 {
5840 /* If the wild pattern was marked KEEP, the member sections
5841 should be as well. */
5842 if (ptr->keep_sections)
5843 section->flags |= SEC_KEEP;
5844 }
5845
5846 /* Iterate over sections marking them against GC. */
5847
5848 static void
5849 lang_gc_sections_1 (lang_statement_union_type *s)
5850 {
5851 for (; s != NULL; s = s->header.next)
5852 {
5853 switch (s->header.type)
5854 {
5855 case lang_wild_statement_enum:
5856 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5857 break;
5858 case lang_constructors_statement_enum:
5859 lang_gc_sections_1 (constructor_list.head);
5860 break;
5861 case lang_output_section_statement_enum:
5862 lang_gc_sections_1 (s->output_section_statement.children.head);
5863 break;
5864 case lang_group_statement_enum:
5865 lang_gc_sections_1 (s->group_statement.children.head);
5866 break;
5867 default:
5868 break;
5869 }
5870 }
5871 }
5872
5873 static void
5874 lang_gc_sections (void)
5875 {
5876 /* Keep all sections so marked in the link script. */
5877
5878 lang_gc_sections_1 (statement_list.head);
5879
5880 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5881 the special case of debug info. (See bfd/stabs.c)
5882 Twiddle the flag here, to simplify later linker code. */
5883 if (link_info.relocatable)
5884 {
5885 LANG_FOR_EACH_INPUT_STATEMENT (f)
5886 {
5887 asection *sec;
5888 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5889 if ((sec->flags & SEC_DEBUGGING) == 0)
5890 sec->flags &= ~SEC_EXCLUDE;
5891 }
5892 }
5893
5894 if (link_info.gc_sections)
5895 bfd_gc_sections (link_info.output_bfd, &link_info);
5896 }
5897
5898 /* Worker for lang_find_relro_sections_1. */
5899
5900 static void
5901 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
5902 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5903 asection *section,
5904 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5905 void *data)
5906 {
5907 /* Discarded, excluded and ignored sections effectively have zero
5908 size. */
5909 if (section->output_section != NULL
5910 && section->output_section->owner == link_info.output_bfd
5911 && (section->output_section->flags & SEC_EXCLUDE) == 0
5912 && !IGNORE_SECTION (section)
5913 && section->size != 0)
5914 {
5915 bfd_boolean *has_relro_section = (bfd_boolean *) data;
5916 *has_relro_section = TRUE;
5917 }
5918 }
5919
5920 /* Iterate over sections for relro sections. */
5921
5922 static void
5923 lang_find_relro_sections_1 (lang_statement_union_type *s,
5924 bfd_boolean *has_relro_section)
5925 {
5926 if (*has_relro_section)
5927 return;
5928
5929 for (; s != NULL; s = s->header.next)
5930 {
5931 if (s == expld.dataseg.relro_end_stat)
5932 break;
5933
5934 switch (s->header.type)
5935 {
5936 case lang_wild_statement_enum:
5937 walk_wild (&s->wild_statement,
5938 find_relro_section_callback,
5939 has_relro_section);
5940 break;
5941 case lang_constructors_statement_enum:
5942 lang_find_relro_sections_1 (constructor_list.head,
5943 has_relro_section);
5944 break;
5945 case lang_output_section_statement_enum:
5946 lang_find_relro_sections_1 (s->output_section_statement.children.head,
5947 has_relro_section);
5948 break;
5949 case lang_group_statement_enum:
5950 lang_find_relro_sections_1 (s->group_statement.children.head,
5951 has_relro_section);
5952 break;
5953 default:
5954 break;
5955 }
5956 }
5957 }
5958
5959 static void
5960 lang_find_relro_sections (void)
5961 {
5962 bfd_boolean has_relro_section = FALSE;
5963
5964 /* Check all sections in the link script. */
5965
5966 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
5967 &has_relro_section);
5968
5969 if (!has_relro_section)
5970 link_info.relro = FALSE;
5971 }
5972
5973 /* Relax all sections until bfd_relax_section gives up. */
5974
5975 static void
5976 relax_sections (void)
5977 {
5978 /* Keep relaxing until bfd_relax_section gives up. */
5979 bfd_boolean relax_again;
5980
5981 link_info.relax_trip = -1;
5982 do
5983 {
5984 relax_again = FALSE;
5985 link_info.relax_trip++;
5986
5987 /* Note: pe-dll.c does something like this also. If you find
5988 you need to change this code, you probably need to change
5989 pe-dll.c also. DJ */
5990
5991 /* Do all the assignments with our current guesses as to
5992 section sizes. */
5993 lang_do_assignments ();
5994
5995 /* We must do this after lang_do_assignments, because it uses
5996 size. */
5997 lang_reset_memory_regions ();
5998
5999 /* Perform another relax pass - this time we know where the
6000 globals are, so can make a better guess. */
6001 lang_size_sections (&relax_again, FALSE);
6002 }
6003 while (relax_again);
6004 }
6005
6006 void
6007 lang_process (void)
6008 {
6009 /* Finalize dynamic list. */
6010 if (link_info.dynamic_list)
6011 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
6012
6013 current_target = default_target;
6014
6015 /* Open the output file. */
6016 lang_for_each_statement (ldlang_open_output);
6017 init_opb ();
6018
6019 ldemul_create_output_section_statements ();
6020
6021 /* Add to the hash table all undefineds on the command line. */
6022 lang_place_undefineds ();
6023
6024 if (!bfd_section_already_linked_table_init ())
6025 einfo (_("%P%F: Failed to create hash table\n"));
6026
6027 /* Create a bfd for each input file. */
6028 current_target = default_target;
6029 open_input_bfds (statement_list.head, FALSE);
6030
6031 link_info.gc_sym_list = &entry_symbol;
6032 if (entry_symbol.name == NULL)
6033 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6034
6035 ldemul_after_open ();
6036
6037 bfd_section_already_linked_table_free ();
6038
6039 /* Make sure that we're not mixing architectures. We call this
6040 after all the input files have been opened, but before we do any
6041 other processing, so that any operations merge_private_bfd_data
6042 does on the output file will be known during the rest of the
6043 link. */
6044 lang_check ();
6045
6046 /* Handle .exports instead of a version script if we're told to do so. */
6047 if (command_line.version_exports_section)
6048 lang_do_version_exports_section ();
6049
6050 /* Build all sets based on the information gathered from the input
6051 files. */
6052 ldctor_build_sets ();
6053
6054 /* Remove unreferenced sections if asked to. */
6055 lang_gc_sections ();
6056
6057 /* Size up the common data. */
6058 lang_common ();
6059
6060 /* Update wild statements. */
6061 update_wild_statements (statement_list.head);
6062
6063 /* Run through the contours of the script and attach input sections
6064 to the correct output sections. */
6065 map_input_to_output_sections (statement_list.head, NULL, NULL);
6066
6067 process_insert_statements ();
6068
6069 /* Find any sections not attached explicitly and handle them. */
6070 lang_place_orphans ();
6071
6072 if (! link_info.relocatable)
6073 {
6074 asection *found;
6075
6076 /* Merge SEC_MERGE sections. This has to be done after GC of
6077 sections, so that GCed sections are not merged, but before
6078 assigning dynamic symbols, since removing whole input sections
6079 is hard then. */
6080 bfd_merge_sections (link_info.output_bfd, &link_info);
6081
6082 /* Look for a text section and set the readonly attribute in it. */
6083 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
6084
6085 if (found != NULL)
6086 {
6087 if (config.text_read_only)
6088 found->flags |= SEC_READONLY;
6089 else
6090 found->flags &= ~SEC_READONLY;
6091 }
6092 }
6093
6094 /* Do anything special before sizing sections. This is where ELF
6095 and other back-ends size dynamic sections. */
6096 ldemul_before_allocation ();
6097
6098 /* We must record the program headers before we try to fix the
6099 section positions, since they will affect SIZEOF_HEADERS. */
6100 lang_record_phdrs ();
6101
6102 /* Check relro sections. */
6103 if (link_info.relro && ! link_info.relocatable)
6104 lang_find_relro_sections ();
6105
6106 /* Size up the sections. */
6107 lang_size_sections (NULL, !command_line.relax);
6108
6109 /* Now run around and relax if we can. */
6110 if (command_line.relax)
6111 {
6112 /* We may need more than one relaxation pass. */
6113 int i = link_info.relax_pass;
6114
6115 /* The backend can use it to determine the current pass. */
6116 link_info.relax_pass = 0;
6117
6118 while (i--)
6119 {
6120 relax_sections ();
6121 link_info.relax_pass++;
6122 }
6123
6124 /* Final extra sizing to report errors. */
6125 lang_do_assignments ();
6126 lang_reset_memory_regions ();
6127 lang_size_sections (NULL, TRUE);
6128 }
6129
6130 /* See if anything special should be done now we know how big
6131 everything is. */
6132 ldemul_after_allocation ();
6133
6134 /* Fix any .startof. or .sizeof. symbols. */
6135 lang_set_startof ();
6136
6137 /* Do all the assignments, now that we know the final resting places
6138 of all the symbols. */
6139
6140 lang_do_assignments ();
6141
6142 ldemul_finish ();
6143
6144 /* Make sure that the section addresses make sense. */
6145 if (! link_info.relocatable
6146 && command_line.check_section_addresses)
6147 lang_check_section_addresses ();
6148
6149 lang_end ();
6150 }
6151
6152 /* EXPORTED TO YACC */
6153
6154 void
6155 lang_add_wild (struct wildcard_spec *filespec,
6156 struct wildcard_list *section_list,
6157 bfd_boolean keep_sections)
6158 {
6159 struct wildcard_list *curr, *next;
6160 lang_wild_statement_type *new;
6161
6162 /* Reverse the list as the parser puts it back to front. */
6163 for (curr = section_list, section_list = NULL;
6164 curr != NULL;
6165 section_list = curr, curr = next)
6166 {
6167 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
6168 placed_commons = TRUE;
6169
6170 next = curr->next;
6171 curr->next = section_list;
6172 }
6173
6174 if (filespec != NULL && filespec->name != NULL)
6175 {
6176 if (strcmp (filespec->name, "*") == 0)
6177 filespec->name = NULL;
6178 else if (! wildcardp (filespec->name))
6179 lang_has_input_file = TRUE;
6180 }
6181
6182 new = new_stat (lang_wild_statement, stat_ptr);
6183 new->filename = NULL;
6184 new->filenames_sorted = FALSE;
6185 if (filespec != NULL)
6186 {
6187 new->filename = filespec->name;
6188 new->filenames_sorted = filespec->sorted == by_name;
6189 }
6190 new->section_list = section_list;
6191 new->keep_sections = keep_sections;
6192 lang_list_init (&new->children);
6193 analyze_walk_wild_section_handler (new);
6194 }
6195
6196 void
6197 lang_section_start (const char *name, etree_type *address,
6198 const segment_type *segment)
6199 {
6200 lang_address_statement_type *ad;
6201
6202 ad = new_stat (lang_address_statement, stat_ptr);
6203 ad->section_name = name;
6204 ad->address = address;
6205 ad->segment = segment;
6206 }
6207
6208 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6209 because of a -e argument on the command line, or zero if this is
6210 called by ENTRY in a linker script. Command line arguments take
6211 precedence. */
6212
6213 void
6214 lang_add_entry (const char *name, bfd_boolean cmdline)
6215 {
6216 if (entry_symbol.name == NULL
6217 || cmdline
6218 || ! entry_from_cmdline)
6219 {
6220 entry_symbol.name = name;
6221 entry_from_cmdline = cmdline;
6222 }
6223 }
6224
6225 /* Set the default start symbol to NAME. .em files should use this,
6226 not lang_add_entry, to override the use of "start" if neither the
6227 linker script nor the command line specifies an entry point. NAME
6228 must be permanently allocated. */
6229 void
6230 lang_default_entry (const char *name)
6231 {
6232 entry_symbol_default = name;
6233 }
6234
6235 void
6236 lang_add_target (const char *name)
6237 {
6238 lang_target_statement_type *new;
6239
6240 new = new_stat (lang_target_statement, stat_ptr);
6241 new->target = name;
6242 }
6243
6244 void
6245 lang_add_map (const char *name)
6246 {
6247 while (*name)
6248 {
6249 switch (*name)
6250 {
6251 case 'F':
6252 map_option_f = TRUE;
6253 break;
6254 }
6255 name++;
6256 }
6257 }
6258
6259 void
6260 lang_add_fill (fill_type *fill)
6261 {
6262 lang_fill_statement_type *new;
6263
6264 new = new_stat (lang_fill_statement, stat_ptr);
6265 new->fill = fill;
6266 }
6267
6268 void
6269 lang_add_data (int type, union etree_union *exp)
6270 {
6271 lang_data_statement_type *new;
6272
6273 new = new_stat (lang_data_statement, stat_ptr);
6274 new->exp = exp;
6275 new->type = type;
6276 }
6277
6278 /* Create a new reloc statement. RELOC is the BFD relocation type to
6279 generate. HOWTO is the corresponding howto structure (we could
6280 look this up, but the caller has already done so). SECTION is the
6281 section to generate a reloc against, or NAME is the name of the
6282 symbol to generate a reloc against. Exactly one of SECTION and
6283 NAME must be NULL. ADDEND is an expression for the addend. */
6284
6285 void
6286 lang_add_reloc (bfd_reloc_code_real_type reloc,
6287 reloc_howto_type *howto,
6288 asection *section,
6289 const char *name,
6290 union etree_union *addend)
6291 {
6292 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
6293
6294 p->reloc = reloc;
6295 p->howto = howto;
6296 p->section = section;
6297 p->name = name;
6298 p->addend_exp = addend;
6299
6300 p->addend_value = 0;
6301 p->output_section = NULL;
6302 p->output_offset = 0;
6303 }
6304
6305 lang_assignment_statement_type *
6306 lang_add_assignment (etree_type *exp)
6307 {
6308 lang_assignment_statement_type *new;
6309
6310 new = new_stat (lang_assignment_statement, stat_ptr);
6311 new->exp = exp;
6312 return new;
6313 }
6314
6315 void
6316 lang_add_attribute (enum statement_enum attribute)
6317 {
6318 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
6319 }
6320
6321 void
6322 lang_startup (const char *name)
6323 {
6324 if (startup_file != NULL)
6325 {
6326 einfo (_("%P%F: multiple STARTUP files\n"));
6327 }
6328 first_file->filename = name;
6329 first_file->local_sym_name = name;
6330 first_file->real = TRUE;
6331
6332 startup_file = name;
6333 }
6334
6335 void
6336 lang_float (bfd_boolean maybe)
6337 {
6338 lang_float_flag = maybe;
6339 }
6340
6341
6342 /* Work out the load- and run-time regions from a script statement, and
6343 store them in *LMA_REGION and *REGION respectively.
6344
6345 MEMSPEC is the name of the run-time region, or the value of
6346 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6347 LMA_MEMSPEC is the name of the load-time region, or null if the
6348 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6349 had an explicit load address.
6350
6351 It is an error to specify both a load region and a load address. */
6352
6353 static void
6354 lang_get_regions (lang_memory_region_type **region,
6355 lang_memory_region_type **lma_region,
6356 const char *memspec,
6357 const char *lma_memspec,
6358 bfd_boolean have_lma,
6359 bfd_boolean have_vma)
6360 {
6361 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
6362
6363 /* If no runtime region or VMA has been specified, but the load region
6364 has been specified, then use the load region for the runtime region
6365 as well. */
6366 if (lma_memspec != NULL
6367 && ! have_vma
6368 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
6369 *region = *lma_region;
6370 else
6371 *region = lang_memory_region_lookup (memspec, FALSE);
6372
6373 if (have_lma && lma_memspec != 0)
6374 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6375 }
6376
6377 void
6378 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6379 lang_output_section_phdr_list *phdrs,
6380 const char *lma_memspec)
6381 {
6382 lang_get_regions (&current_section->region,
6383 &current_section->lma_region,
6384 memspec, lma_memspec,
6385 current_section->load_base != NULL,
6386 current_section->addr_tree != NULL);
6387 current_section->fill = fill;
6388 current_section->phdrs = phdrs;
6389 stat_ptr = &statement_list;
6390 }
6391
6392 /* Create an absolute symbol with the given name with the value of the
6393 address of first byte of the section named.
6394
6395 If the symbol already exists, then do nothing. */
6396
6397 void
6398 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
6399 {
6400 struct bfd_link_hash_entry *h;
6401
6402 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6403 if (h == NULL)
6404 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6405
6406 if (h->type == bfd_link_hash_new
6407 || h->type == bfd_link_hash_undefined)
6408 {
6409 asection *sec;
6410
6411 h->type = bfd_link_hash_defined;
6412
6413 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6414 if (sec == NULL)
6415 h->u.def.value = 0;
6416 else
6417 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec);
6418
6419 h->u.def.section = bfd_abs_section_ptr;
6420 }
6421 }
6422
6423 /* Create an absolute symbol with the given name with the value of the
6424 address of the first byte after the end of the section named.
6425
6426 If the symbol already exists, then do nothing. */
6427
6428 void
6429 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6430 {
6431 struct bfd_link_hash_entry *h;
6432
6433 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6434 if (h == NULL)
6435 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6436
6437 if (h->type == bfd_link_hash_new
6438 || h->type == bfd_link_hash_undefined)
6439 {
6440 asection *sec;
6441
6442 h->type = bfd_link_hash_defined;
6443
6444 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6445 if (sec == NULL)
6446 h->u.def.value = 0;
6447 else
6448 h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec)
6449 + TO_ADDR (sec->size));
6450
6451 h->u.def.section = bfd_abs_section_ptr;
6452 }
6453 }
6454
6455 void
6456 lang_statement_append (lang_statement_list_type *list,
6457 lang_statement_union_type *element,
6458 lang_statement_union_type **field)
6459 {
6460 *(list->tail) = element;
6461 list->tail = field;
6462 }
6463
6464 /* Set the output format type. -oformat overrides scripts. */
6465
6466 void
6467 lang_add_output_format (const char *format,
6468 const char *big,
6469 const char *little,
6470 int from_script)
6471 {
6472 if (output_target == NULL || !from_script)
6473 {
6474 if (command_line.endian == ENDIAN_BIG
6475 && big != NULL)
6476 format = big;
6477 else if (command_line.endian == ENDIAN_LITTLE
6478 && little != NULL)
6479 format = little;
6480
6481 output_target = format;
6482 }
6483 }
6484
6485 void
6486 lang_add_insert (const char *where, int is_before)
6487 {
6488 lang_insert_statement_type *new;
6489
6490 new = new_stat (lang_insert_statement, stat_ptr);
6491 new->where = where;
6492 new->is_before = is_before;
6493 saved_script_handle = previous_script_handle;
6494 }
6495
6496 /* Enter a group. This creates a new lang_group_statement, and sets
6497 stat_ptr to build new statements within the group. */
6498
6499 void
6500 lang_enter_group (void)
6501 {
6502 lang_group_statement_type *g;
6503
6504 g = new_stat (lang_group_statement, stat_ptr);
6505 lang_list_init (&g->children);
6506 stat_ptr = &g->children;
6507 }
6508
6509 /* Leave a group. This just resets stat_ptr to start writing to the
6510 regular list of statements again. Note that this will not work if
6511 groups can occur inside anything else which can adjust stat_ptr,
6512 but currently they can't. */
6513
6514 void
6515 lang_leave_group (void)
6516 {
6517 stat_ptr = &statement_list;
6518 }
6519
6520 /* Add a new program header. This is called for each entry in a PHDRS
6521 command in a linker script. */
6522
6523 void
6524 lang_new_phdr (const char *name,
6525 etree_type *type,
6526 bfd_boolean filehdr,
6527 bfd_boolean phdrs,
6528 etree_type *at,
6529 etree_type *flags)
6530 {
6531 struct lang_phdr *n, **pp;
6532
6533 n = stat_alloc (sizeof (struct lang_phdr));
6534 n->next = NULL;
6535 n->name = name;
6536 n->type = exp_get_value_int (type, 0, "program header type");
6537 n->filehdr = filehdr;
6538 n->phdrs = phdrs;
6539 n->at = at;
6540 n->flags = flags;
6541
6542 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6543 ;
6544 *pp = n;
6545 }
6546
6547 /* Record the program header information in the output BFD. FIXME: We
6548 should not be calling an ELF specific function here. */
6549
6550 static void
6551 lang_record_phdrs (void)
6552 {
6553 unsigned int alc;
6554 asection **secs;
6555 lang_output_section_phdr_list *last;
6556 struct lang_phdr *l;
6557 lang_output_section_statement_type *os;
6558
6559 alc = 10;
6560 secs = xmalloc (alc * sizeof (asection *));
6561 last = NULL;
6562
6563 for (l = lang_phdr_list; l != NULL; l = l->next)
6564 {
6565 unsigned int c;
6566 flagword flags;
6567 bfd_vma at;
6568
6569 c = 0;
6570 for (os = &lang_output_section_statement.head->output_section_statement;
6571 os != NULL;
6572 os = os->next)
6573 {
6574 lang_output_section_phdr_list *pl;
6575
6576 if (os->constraint == -1)
6577 continue;
6578
6579 pl = os->phdrs;
6580 if (pl != NULL)
6581 last = pl;
6582 else
6583 {
6584 if (os->sectype == noload_section
6585 || os->bfd_section == NULL
6586 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6587 continue;
6588
6589 if (last == NULL)
6590 {
6591 lang_output_section_statement_type * tmp_os;
6592
6593 /* If we have not run across a section with a program
6594 header assigned to it yet, then scan forwards to find
6595 one. This prevents inconsistencies in the linker's
6596 behaviour when a script has specified just a single
6597 header and there are sections in that script which are
6598 not assigned to it, and which occur before the first
6599 use of that header. See here for more details:
6600 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6601 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
6602 if (tmp_os->phdrs)
6603 {
6604 last = tmp_os->phdrs;
6605 break;
6606 }
6607 if (last == NULL)
6608 einfo (_("%F%P: no sections assigned to phdrs\n"));
6609 }
6610 pl = last;
6611 }
6612
6613 if (os->bfd_section == NULL)
6614 continue;
6615
6616 for (; pl != NULL; pl = pl->next)
6617 {
6618 if (strcmp (pl->name, l->name) == 0)
6619 {
6620 if (c >= alc)
6621 {
6622 alc *= 2;
6623 secs = xrealloc (secs, alc * sizeof (asection *));
6624 }
6625 secs[c] = os->bfd_section;
6626 ++c;
6627 pl->used = TRUE;
6628 }
6629 }
6630 }
6631
6632 if (l->flags == NULL)
6633 flags = 0;
6634 else
6635 flags = exp_get_vma (l->flags, 0, "phdr flags");
6636
6637 if (l->at == NULL)
6638 at = 0;
6639 else
6640 at = exp_get_vma (l->at, 0, "phdr load address");
6641
6642 if (! bfd_record_phdr (link_info.output_bfd, l->type,
6643 l->flags != NULL, flags, l->at != NULL,
6644 at, l->filehdr, l->phdrs, c, secs))
6645 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6646 }
6647
6648 free (secs);
6649
6650 /* Make sure all the phdr assignments succeeded. */
6651 for (os = &lang_output_section_statement.head->output_section_statement;
6652 os != NULL;
6653 os = os->next)
6654 {
6655 lang_output_section_phdr_list *pl;
6656
6657 if (os->constraint == -1
6658 || os->bfd_section == NULL)
6659 continue;
6660
6661 for (pl = os->phdrs;
6662 pl != NULL;
6663 pl = pl->next)
6664 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6665 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6666 os->name, pl->name);
6667 }
6668 }
6669
6670 /* Record a list of sections which may not be cross referenced. */
6671
6672 void
6673 lang_add_nocrossref (lang_nocrossref_type *l)
6674 {
6675 struct lang_nocrossrefs *n;
6676
6677 n = xmalloc (sizeof *n);
6678 n->next = nocrossref_list;
6679 n->list = l;
6680 nocrossref_list = n;
6681
6682 /* Set notice_all so that we get informed about all symbols. */
6683 link_info.notice_all = TRUE;
6684 }
6685 \f
6686 /* Overlay handling. We handle overlays with some static variables. */
6687
6688 /* The overlay virtual address. */
6689 static etree_type *overlay_vma;
6690 /* And subsection alignment. */
6691 static etree_type *overlay_subalign;
6692
6693 /* An expression for the maximum section size seen so far. */
6694 static etree_type *overlay_max;
6695
6696 /* A list of all the sections in this overlay. */
6697
6698 struct overlay_list {
6699 struct overlay_list *next;
6700 lang_output_section_statement_type *os;
6701 };
6702
6703 static struct overlay_list *overlay_list;
6704
6705 /* Start handling an overlay. */
6706
6707 void
6708 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6709 {
6710 /* The grammar should prevent nested overlays from occurring. */
6711 ASSERT (overlay_vma == NULL
6712 && overlay_subalign == NULL
6713 && overlay_max == NULL);
6714
6715 overlay_vma = vma_expr;
6716 overlay_subalign = subalign;
6717 }
6718
6719 /* Start a section in an overlay. We handle this by calling
6720 lang_enter_output_section_statement with the correct VMA.
6721 lang_leave_overlay sets up the LMA and memory regions. */
6722
6723 void
6724 lang_enter_overlay_section (const char *name)
6725 {
6726 struct overlay_list *n;
6727 etree_type *size;
6728
6729 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
6730 0, overlay_subalign, 0, 0);
6731
6732 /* If this is the first section, then base the VMA of future
6733 sections on this one. This will work correctly even if `.' is
6734 used in the addresses. */
6735 if (overlay_list == NULL)
6736 overlay_vma = exp_nameop (ADDR, name);
6737
6738 /* Remember the section. */
6739 n = xmalloc (sizeof *n);
6740 n->os = current_section;
6741 n->next = overlay_list;
6742 overlay_list = n;
6743
6744 size = exp_nameop (SIZEOF, name);
6745
6746 /* Arrange to work out the maximum section end address. */
6747 if (overlay_max == NULL)
6748 overlay_max = size;
6749 else
6750 overlay_max = exp_binop (MAX_K, overlay_max, size);
6751 }
6752
6753 /* Finish a section in an overlay. There isn't any special to do
6754 here. */
6755
6756 void
6757 lang_leave_overlay_section (fill_type *fill,
6758 lang_output_section_phdr_list *phdrs)
6759 {
6760 const char *name;
6761 char *clean, *s2;
6762 const char *s1;
6763 char *buf;
6764
6765 name = current_section->name;
6766
6767 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6768 region and that no load-time region has been specified. It doesn't
6769 really matter what we say here, since lang_leave_overlay will
6770 override it. */
6771 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6772
6773 /* Define the magic symbols. */
6774
6775 clean = xmalloc (strlen (name) + 1);
6776 s2 = clean;
6777 for (s1 = name; *s1 != '\0'; s1++)
6778 if (ISALNUM (*s1) || *s1 == '_')
6779 *s2++ = *s1;
6780 *s2 = '\0';
6781
6782 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6783 sprintf (buf, "__load_start_%s", clean);
6784 lang_add_assignment (exp_provide (buf,
6785 exp_nameop (LOADADDR, name),
6786 FALSE));
6787
6788 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6789 sprintf (buf, "__load_stop_%s", clean);
6790 lang_add_assignment (exp_provide (buf,
6791 exp_binop ('+',
6792 exp_nameop (LOADADDR, name),
6793 exp_nameop (SIZEOF, name)),
6794 FALSE));
6795
6796 free (clean);
6797 }
6798
6799 /* Finish an overlay. If there are any overlay wide settings, this
6800 looks through all the sections in the overlay and sets them. */
6801
6802 void
6803 lang_leave_overlay (etree_type *lma_expr,
6804 int nocrossrefs,
6805 fill_type *fill,
6806 const char *memspec,
6807 lang_output_section_phdr_list *phdrs,
6808 const char *lma_memspec)
6809 {
6810 lang_memory_region_type *region;
6811 lang_memory_region_type *lma_region;
6812 struct overlay_list *l;
6813 lang_nocrossref_type *nocrossref;
6814
6815 lang_get_regions (&region, &lma_region,
6816 memspec, lma_memspec,
6817 lma_expr != NULL, FALSE);
6818
6819 nocrossref = NULL;
6820
6821 /* After setting the size of the last section, set '.' to end of the
6822 overlay region. */
6823 if (overlay_list != NULL)
6824 overlay_list->os->update_dot_tree
6825 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6826
6827 l = overlay_list;
6828 while (l != NULL)
6829 {
6830 struct overlay_list *next;
6831
6832 if (fill != NULL && l->os->fill == NULL)
6833 l->os->fill = fill;
6834
6835 l->os->region = region;
6836 l->os->lma_region = lma_region;
6837
6838 /* The first section has the load address specified in the
6839 OVERLAY statement. The rest are worked out from that.
6840 The base address is not needed (and should be null) if
6841 an LMA region was specified. */
6842 if (l->next == 0)
6843 {
6844 l->os->load_base = lma_expr;
6845 l->os->sectype = normal_section;
6846 }
6847 if (phdrs != NULL && l->os->phdrs == NULL)
6848 l->os->phdrs = phdrs;
6849
6850 if (nocrossrefs)
6851 {
6852 lang_nocrossref_type *nc;
6853
6854 nc = xmalloc (sizeof *nc);
6855 nc->name = l->os->name;
6856 nc->next = nocrossref;
6857 nocrossref = nc;
6858 }
6859
6860 next = l->next;
6861 free (l);
6862 l = next;
6863 }
6864
6865 if (nocrossref != NULL)
6866 lang_add_nocrossref (nocrossref);
6867
6868 overlay_vma = NULL;
6869 overlay_list = NULL;
6870 overlay_max = NULL;
6871 }
6872 \f
6873 /* Version handling. This is only useful for ELF. */
6874
6875 /* This global variable holds the version tree that we build. */
6876
6877 struct bfd_elf_version_tree *lang_elf_version_info;
6878
6879 /* If PREV is NULL, return first version pattern matching particular symbol.
6880 If PREV is non-NULL, return first version pattern matching particular
6881 symbol after PREV (previously returned by lang_vers_match). */
6882
6883 static struct bfd_elf_version_expr *
6884 lang_vers_match (struct bfd_elf_version_expr_head *head,
6885 struct bfd_elf_version_expr *prev,
6886 const char *sym)
6887 {
6888 const char *cxx_sym = sym;
6889 const char *java_sym = sym;
6890 struct bfd_elf_version_expr *expr = NULL;
6891
6892 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6893 {
6894 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
6895 if (!cxx_sym)
6896 cxx_sym = sym;
6897 }
6898 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6899 {
6900 java_sym = cplus_demangle (sym, DMGL_JAVA);
6901 if (!java_sym)
6902 java_sym = sym;
6903 }
6904
6905 if (head->htab && (prev == NULL || prev->symbol))
6906 {
6907 struct bfd_elf_version_expr e;
6908
6909 switch (prev ? prev->mask : 0)
6910 {
6911 case 0:
6912 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6913 {
6914 e.symbol = sym;
6915 expr = htab_find (head->htab, &e);
6916 while (expr && strcmp (expr->symbol, sym) == 0)
6917 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6918 goto out_ret;
6919 else
6920 expr = expr->next;
6921 }
6922 /* Fallthrough */
6923 case BFD_ELF_VERSION_C_TYPE:
6924 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6925 {
6926 e.symbol = cxx_sym;
6927 expr = htab_find (head->htab, &e);
6928 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6929 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6930 goto out_ret;
6931 else
6932 expr = expr->next;
6933 }
6934 /* Fallthrough */
6935 case BFD_ELF_VERSION_CXX_TYPE:
6936 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6937 {
6938 e.symbol = java_sym;
6939 expr = htab_find (head->htab, &e);
6940 while (expr && strcmp (expr->symbol, java_sym) == 0)
6941 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6942 goto out_ret;
6943 else
6944 expr = expr->next;
6945 }
6946 /* Fallthrough */
6947 default:
6948 break;
6949 }
6950 }
6951
6952 /* Finally, try the wildcards. */
6953 if (prev == NULL || prev->symbol)
6954 expr = head->remaining;
6955 else
6956 expr = prev->next;
6957 for (; expr; expr = expr->next)
6958 {
6959 const char *s;
6960
6961 if (!expr->pattern)
6962 continue;
6963
6964 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6965 break;
6966
6967 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6968 s = java_sym;
6969 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6970 s = cxx_sym;
6971 else
6972 s = sym;
6973 if (fnmatch (expr->pattern, s, 0) == 0)
6974 break;
6975 }
6976
6977 out_ret:
6978 if (cxx_sym != sym)
6979 free ((char *) cxx_sym);
6980 if (java_sym != sym)
6981 free ((char *) java_sym);
6982 return expr;
6983 }
6984
6985 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6986 return a string pointing to the symbol name. */
6987
6988 static const char *
6989 realsymbol (const char *pattern)
6990 {
6991 const char *p;
6992 bfd_boolean changed = FALSE, backslash = FALSE;
6993 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6994
6995 for (p = pattern, s = symbol; *p != '\0'; ++p)
6996 {
6997 /* It is a glob pattern only if there is no preceding
6998 backslash. */
6999 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
7000 {
7001 free (symbol);
7002 return NULL;
7003 }
7004
7005 if (backslash)
7006 {
7007 /* Remove the preceding backslash. */
7008 *(s - 1) = *p;
7009 changed = TRUE;
7010 }
7011 else
7012 *s++ = *p;
7013
7014 backslash = *p == '\\';
7015 }
7016
7017 if (changed)
7018 {
7019 *s = '\0';
7020 return symbol;
7021 }
7022 else
7023 {
7024 free (symbol);
7025 return pattern;
7026 }
7027 }
7028
7029 /* This is called for each variable name or match expression. NEW is
7030 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7031 pattern to be matched against symbol names. */
7032
7033 struct bfd_elf_version_expr *
7034 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7035 const char *new,
7036 const char *lang,
7037 bfd_boolean literal_p)
7038 {
7039 struct bfd_elf_version_expr *ret;
7040
7041 ret = xmalloc (sizeof *ret);
7042 ret->next = orig;
7043 ret->pattern = literal_p ? NULL : new;
7044 ret->symver = 0;
7045 ret->script = 0;
7046 ret->symbol = literal_p ? new : realsymbol (new);
7047
7048 if (lang == NULL || strcasecmp (lang, "C") == 0)
7049 ret->mask = BFD_ELF_VERSION_C_TYPE;
7050 else if (strcasecmp (lang, "C++") == 0)
7051 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
7052 else if (strcasecmp (lang, "Java") == 0)
7053 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
7054 else
7055 {
7056 einfo (_("%X%P: unknown language `%s' in version information\n"),
7057 lang);
7058 ret->mask = BFD_ELF_VERSION_C_TYPE;
7059 }
7060
7061 return ldemul_new_vers_pattern (ret);
7062 }
7063
7064 /* This is called for each set of variable names and match
7065 expressions. */
7066
7067 struct bfd_elf_version_tree *
7068 lang_new_vers_node (struct bfd_elf_version_expr *globals,
7069 struct bfd_elf_version_expr *locals)
7070 {
7071 struct bfd_elf_version_tree *ret;
7072
7073 ret = xcalloc (1, sizeof *ret);
7074 ret->globals.list = globals;
7075 ret->locals.list = locals;
7076 ret->match = lang_vers_match;
7077 ret->name_indx = (unsigned int) -1;
7078 return ret;
7079 }
7080
7081 /* This static variable keeps track of version indices. */
7082
7083 static int version_index;
7084
7085 static hashval_t
7086 version_expr_head_hash (const void *p)
7087 {
7088 const struct bfd_elf_version_expr *e = p;
7089
7090 return htab_hash_string (e->symbol);
7091 }
7092
7093 static int
7094 version_expr_head_eq (const void *p1, const void *p2)
7095 {
7096 const struct bfd_elf_version_expr *e1 = p1;
7097 const struct bfd_elf_version_expr *e2 = p2;
7098
7099 return strcmp (e1->symbol, e2->symbol) == 0;
7100 }
7101
7102 static void
7103 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7104 {
7105 size_t count = 0;
7106 struct bfd_elf_version_expr *e, *next;
7107 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7108
7109 for (e = head->list; e; e = e->next)
7110 {
7111 if (e->symbol)
7112 count++;
7113 head->mask |= e->mask;
7114 }
7115
7116 if (count)
7117 {
7118 head->htab = htab_create (count * 2, version_expr_head_hash,
7119 version_expr_head_eq, NULL);
7120 list_loc = &head->list;
7121 remaining_loc = &head->remaining;
7122 for (e = head->list; e; e = next)
7123 {
7124 next = e->next;
7125 if (!e->symbol)
7126 {
7127 *remaining_loc = e;
7128 remaining_loc = &e->next;
7129 }
7130 else
7131 {
7132 void **loc = htab_find_slot (head->htab, e, INSERT);
7133
7134 if (*loc)
7135 {
7136 struct bfd_elf_version_expr *e1, *last;
7137
7138 e1 = *loc;
7139 last = NULL;
7140 do
7141 {
7142 if (e1->mask == e->mask)
7143 {
7144 last = NULL;
7145 break;
7146 }
7147 last = e1;
7148 e1 = e1->next;
7149 }
7150 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
7151
7152 if (last == NULL)
7153 {
7154 /* This is a duplicate. */
7155 /* FIXME: Memory leak. Sometimes pattern is not
7156 xmalloced alone, but in larger chunk of memory. */
7157 /* free (e->symbol); */
7158 free (e);
7159 }
7160 else
7161 {
7162 e->next = last->next;
7163 last->next = e;
7164 }
7165 }
7166 else
7167 {
7168 *loc = e;
7169 *list_loc = e;
7170 list_loc = &e->next;
7171 }
7172 }
7173 }
7174 *remaining_loc = NULL;
7175 *list_loc = head->remaining;
7176 }
7177 else
7178 head->remaining = head->list;
7179 }
7180
7181 /* This is called when we know the name and dependencies of the
7182 version. */
7183
7184 void
7185 lang_register_vers_node (const char *name,
7186 struct bfd_elf_version_tree *version,
7187 struct bfd_elf_version_deps *deps)
7188 {
7189 struct bfd_elf_version_tree *t, **pp;
7190 struct bfd_elf_version_expr *e1;
7191
7192 if (name == NULL)
7193 name = "";
7194
7195 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7196 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7197 {
7198 einfo (_("%X%P: anonymous version tag cannot be combined"
7199 " with other version tags\n"));
7200 free (version);
7201 return;
7202 }
7203
7204 /* Make sure this node has a unique name. */
7205 for (t = lang_elf_version_info; t != NULL; t = t->next)
7206 if (strcmp (t->name, name) == 0)
7207 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7208
7209 lang_finalize_version_expr_head (&version->globals);
7210 lang_finalize_version_expr_head (&version->locals);
7211
7212 /* Check the global and local match names, and make sure there
7213 aren't any duplicates. */
7214
7215 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
7216 {
7217 for (t = lang_elf_version_info; t != NULL; t = t->next)
7218 {
7219 struct bfd_elf_version_expr *e2;
7220
7221 if (t->locals.htab && e1->symbol)
7222 {
7223 e2 = htab_find (t->locals.htab, e1);
7224 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
7225 {
7226 if (e1->mask == e2->mask)
7227 einfo (_("%X%P: duplicate expression `%s'"
7228 " in version information\n"), e1->symbol);
7229 e2 = e2->next;
7230 }
7231 }
7232 else if (!e1->symbol)
7233 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
7234 if (strcmp (e1->pattern, e2->pattern) == 0
7235 && e1->mask == e2->mask)
7236 einfo (_("%X%P: duplicate expression `%s'"
7237 " in version information\n"), e1->pattern);
7238 }
7239 }
7240
7241 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
7242 {
7243 for (t = lang_elf_version_info; t != NULL; t = t->next)
7244 {
7245 struct bfd_elf_version_expr *e2;
7246
7247 if (t->globals.htab && e1->symbol)
7248 {
7249 e2 = htab_find (t->globals.htab, e1);
7250 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
7251 {
7252 if (e1->mask == e2->mask)
7253 einfo (_("%X%P: duplicate expression `%s'"
7254 " in version information\n"),
7255 e1->symbol);
7256 e2 = e2->next;
7257 }
7258 }
7259 else if (!e1->symbol)
7260 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
7261 if (strcmp (e1->pattern, e2->pattern) == 0
7262 && e1->mask == e2->mask)
7263 einfo (_("%X%P: duplicate expression `%s'"
7264 " in version information\n"), e1->pattern);
7265 }
7266 }
7267
7268 version->deps = deps;
7269 version->name = name;
7270 if (name[0] != '\0')
7271 {
7272 ++version_index;
7273 version->vernum = version_index;
7274 }
7275 else
7276 version->vernum = 0;
7277
7278 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7279 ;
7280 *pp = version;
7281 }
7282
7283 /* This is called when we see a version dependency. */
7284
7285 struct bfd_elf_version_deps *
7286 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
7287 {
7288 struct bfd_elf_version_deps *ret;
7289 struct bfd_elf_version_tree *t;
7290
7291 ret = xmalloc (sizeof *ret);
7292 ret->next = list;
7293
7294 for (t = lang_elf_version_info; t != NULL; t = t->next)
7295 {
7296 if (strcmp (t->name, name) == 0)
7297 {
7298 ret->version_needed = t;
7299 return ret;
7300 }
7301 }
7302
7303 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7304
7305 return ret;
7306 }
7307
7308 static void
7309 lang_do_version_exports_section (void)
7310 {
7311 struct bfd_elf_version_expr *greg = NULL, *lreg;
7312
7313 LANG_FOR_EACH_INPUT_STATEMENT (is)
7314 {
7315 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7316 char *contents, *p;
7317 bfd_size_type len;
7318
7319 if (sec == NULL)
7320 continue;
7321
7322 len = sec->size;
7323 contents = xmalloc (len);
7324 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
7325 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
7326
7327 p = contents;
7328 while (p < contents + len)
7329 {
7330 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
7331 p = strchr (p, '\0') + 1;
7332 }
7333
7334 /* Do not free the contents, as we used them creating the regex. */
7335
7336 /* Do not include this section in the link. */
7337 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
7338 }
7339
7340 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
7341 lang_register_vers_node (command_line.version_exports_section,
7342 lang_new_vers_node (greg, lreg), NULL);
7343 }
7344
7345 void
7346 lang_add_unique (const char *name)
7347 {
7348 struct unique_sections *ent;
7349
7350 for (ent = unique_section_list; ent; ent = ent->next)
7351 if (strcmp (ent->name, name) == 0)
7352 return;
7353
7354 ent = xmalloc (sizeof *ent);
7355 ent->name = xstrdup (name);
7356 ent->next = unique_section_list;
7357 unique_section_list = ent;
7358 }
7359
7360 /* Append the list of dynamic symbols to the existing one. */
7361
7362 void
7363 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7364 {
7365 if (link_info.dynamic_list)
7366 {
7367 struct bfd_elf_version_expr *tail;
7368 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7369 ;
7370 tail->next = link_info.dynamic_list->head.list;
7371 link_info.dynamic_list->head.list = dynamic;
7372 }
7373 else
7374 {
7375 struct bfd_elf_dynamic_list *d;
7376
7377 d = xcalloc (1, sizeof *d);
7378 d->head.list = dynamic;
7379 d->match = lang_vers_match;
7380 link_info.dynamic_list = d;
7381 }
7382 }
7383
7384 /* Append the list of C++ typeinfo dynamic symbols to the existing
7385 one. */
7386
7387 void
7388 lang_append_dynamic_list_cpp_typeinfo (void)
7389 {
7390 const char * symbols [] =
7391 {
7392 "typeinfo name for*",
7393 "typeinfo for*"
7394 };
7395 struct bfd_elf_version_expr *dynamic = NULL;
7396 unsigned int i;
7397
7398 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7399 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7400 FALSE);
7401
7402 lang_append_dynamic_list (dynamic);
7403 }
7404
7405 /* Append the list of C++ operator new and delete dynamic symbols to the
7406 existing one. */
7407
7408 void
7409 lang_append_dynamic_list_cpp_new (void)
7410 {
7411 const char * symbols [] =
7412 {
7413 "operator new*",
7414 "operator delete*"
7415 };
7416 struct bfd_elf_version_expr *dynamic = NULL;
7417 unsigned int i;
7418
7419 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7420 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7421 FALSE);
7422
7423 lang_append_dynamic_list (dynamic);
7424 }
This page took 0.185041 seconds and 4 git commands to generate.