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