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