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