ld/
[deliverable/binutils-gdb.git] / ld / ldlang.c
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006
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 flagword flags)
1864 {
1865 if (s->bfd_section != NULL)
1866 return;
1867
1868 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1869 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1870
1871 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1872 if (s->bfd_section == NULL)
1873 s->bfd_section = bfd_make_section_with_flags (output_bfd, s->name,
1874 flags);
1875 if (s->bfd_section == NULL)
1876 {
1877 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1878 output_bfd->xvec->name, s->name);
1879 }
1880 s->bfd_section->output_section = s->bfd_section;
1881 s->bfd_section->output_offset = 0;
1882
1883 if (!command_line.reduce_memory_overheads)
1884 {
1885 fat_section_userdata_type *new
1886 = stat_alloc (sizeof (fat_section_userdata_type));
1887 memset (new, 0, sizeof (fat_section_userdata_type));
1888 get_userdata (s->bfd_section) = new;
1889 }
1890
1891 /* If there is a base address, make sure that any sections it might
1892 mention are initialized. */
1893 if (s->addr_tree != NULL)
1894 exp_init_os (s->addr_tree);
1895
1896 if (s->load_base != NULL)
1897 exp_init_os (s->load_base);
1898
1899 /* If supplied an alignment, set it. */
1900 if (s->section_alignment != -1)
1901 s->bfd_section->alignment_power = s->section_alignment;
1902
1903 if (isec)
1904 bfd_init_private_section_data (isec->owner, isec,
1905 output_bfd, s->bfd_section,
1906 &link_info);
1907 }
1908
1909 /* Make sure that all output sections mentioned in an expression are
1910 initialized. */
1911
1912 static void
1913 exp_init_os (etree_type *exp)
1914 {
1915 switch (exp->type.node_class)
1916 {
1917 case etree_assign:
1918 case etree_provide:
1919 exp_init_os (exp->assign.src);
1920 break;
1921
1922 case etree_binary:
1923 exp_init_os (exp->binary.lhs);
1924 exp_init_os (exp->binary.rhs);
1925 break;
1926
1927 case etree_trinary:
1928 exp_init_os (exp->trinary.cond);
1929 exp_init_os (exp->trinary.lhs);
1930 exp_init_os (exp->trinary.rhs);
1931 break;
1932
1933 case etree_assert:
1934 exp_init_os (exp->assert_s.child);
1935 break;
1936
1937 case etree_unary:
1938 exp_init_os (exp->unary.child);
1939 break;
1940
1941 case etree_name:
1942 switch (exp->type.node_code)
1943 {
1944 case ADDR:
1945 case LOADADDR:
1946 case SIZEOF:
1947 {
1948 lang_output_section_statement_type *os;
1949
1950 os = lang_output_section_find (exp->name.name);
1951 if (os != NULL && os->bfd_section == NULL)
1952 init_os (os, NULL, 0);
1953 }
1954 }
1955 break;
1956
1957 default:
1958 break;
1959 }
1960 }
1961 \f
1962 static void
1963 section_already_linked (bfd *abfd, asection *sec, void *data)
1964 {
1965 lang_input_statement_type *entry = data;
1966
1967 /* If we are only reading symbols from this object, then we want to
1968 discard all sections. */
1969 if (entry->just_syms_flag)
1970 {
1971 bfd_link_just_syms (abfd, sec, &link_info);
1972 return;
1973 }
1974
1975 if (!(abfd->flags & DYNAMIC))
1976 bfd_section_already_linked (abfd, sec);
1977 }
1978 \f
1979 /* The wild routines.
1980
1981 These expand statements like *(.text) and foo.o to a list of
1982 explicit actions, like foo.o(.text), bar.o(.text) and
1983 foo.o(.text, .data). */
1984
1985 /* Add SECTION to the output section OUTPUT. Do this by creating a
1986 lang_input_section statement which is placed at PTR. FILE is the
1987 input file which holds SECTION. */
1988
1989 void
1990 lang_add_section (lang_statement_list_type *ptr,
1991 asection *section,
1992 lang_output_section_statement_type *output)
1993 {
1994 flagword flags = section->flags;
1995 bfd_boolean discard;
1996
1997 /* Discard sections marked with SEC_EXCLUDE. */
1998 discard = (flags & SEC_EXCLUDE) != 0;
1999
2000 /* Discard input sections which are assigned to a section named
2001 DISCARD_SECTION_NAME. */
2002 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2003 discard = TRUE;
2004
2005 /* Discard debugging sections if we are stripping debugging
2006 information. */
2007 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2008 && (flags & SEC_DEBUGGING) != 0)
2009 discard = TRUE;
2010
2011 if (discard)
2012 {
2013 if (section->output_section == NULL)
2014 {
2015 /* This prevents future calls from assigning this section. */
2016 section->output_section = bfd_abs_section_ptr;
2017 }
2018 return;
2019 }
2020
2021 if (section->output_section == NULL)
2022 {
2023 bfd_boolean first;
2024 lang_input_section_type *new;
2025 flagword flags;
2026
2027 flags = section->flags;
2028
2029 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2030 to an output section, because we want to be able to include a
2031 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2032 section (I don't know why we want to do this, but we do).
2033 build_link_order in ldwrite.c handles this case by turning
2034 the embedded SEC_NEVER_LOAD section into a fill. */
2035
2036 flags &= ~ SEC_NEVER_LOAD;
2037
2038 switch (output->sectype)
2039 {
2040 case normal_section:
2041 break;
2042 case noalloc_section:
2043 flags &= ~SEC_ALLOC;
2044 break;
2045 case noload_section:
2046 flags &= ~SEC_LOAD;
2047 flags |= SEC_NEVER_LOAD;
2048 break;
2049 }
2050
2051 if (output->bfd_section == NULL)
2052 init_os (output, section, flags);
2053
2054 first = ! output->bfd_section->linker_has_input;
2055 output->bfd_section->linker_has_input = 1;
2056
2057 if (!link_info.relocatable
2058 && !stripped_excluded_sections)
2059 {
2060 asection *s = output->bfd_section->map_tail.s;
2061 output->bfd_section->map_tail.s = section;
2062 section->map_head.s = NULL;
2063 section->map_tail.s = s;
2064 if (s != NULL)
2065 s->map_head.s = section;
2066 else
2067 output->bfd_section->map_head.s = section;
2068 }
2069
2070 /* Add a section reference to the list. */
2071 new = new_stat (lang_input_section, ptr);
2072
2073 new->section = section;
2074 section->output_section = output->bfd_section;
2075
2076 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2077 already been processed. One reason to do this is that on pe
2078 format targets, .text$foo sections go into .text and it's odd
2079 to see .text with SEC_LINK_ONCE set. */
2080
2081 if (! link_info.relocatable)
2082 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2083
2084 /* If this is not the first input section, and the SEC_READONLY
2085 flag is not currently set, then don't set it just because the
2086 input section has it set. */
2087
2088 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2089 flags &= ~ SEC_READONLY;
2090
2091 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2092 if (! first
2093 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2094 != (flags & (SEC_MERGE | SEC_STRINGS))
2095 || ((flags & SEC_MERGE)
2096 && output->bfd_section->entsize != section->entsize)))
2097 {
2098 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2099 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2100 }
2101
2102 output->bfd_section->flags |= flags;
2103
2104 if (flags & SEC_MERGE)
2105 output->bfd_section->entsize = section->entsize;
2106
2107 /* If SEC_READONLY is not set in the input section, then clear
2108 it from the output section. */
2109 if ((section->flags & SEC_READONLY) == 0)
2110 output->bfd_section->flags &= ~SEC_READONLY;
2111
2112 /* Copy over SEC_SMALL_DATA. */
2113 if (section->flags & SEC_SMALL_DATA)
2114 output->bfd_section->flags |= SEC_SMALL_DATA;
2115
2116 if (section->alignment_power > output->bfd_section->alignment_power)
2117 output->bfd_section->alignment_power = section->alignment_power;
2118
2119 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2120 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2121 {
2122 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2123 /* FIXME: This value should really be obtained from the bfd... */
2124 output->block_value = 128;
2125 }
2126 }
2127 }
2128
2129 /* Handle wildcard sorting. This returns the lang_input_section which
2130 should follow the one we are going to create for SECTION and FILE,
2131 based on the sorting requirements of WILD. It returns NULL if the
2132 new section should just go at the end of the current list. */
2133
2134 static lang_statement_union_type *
2135 wild_sort (lang_wild_statement_type *wild,
2136 struct wildcard_list *sec,
2137 lang_input_statement_type *file,
2138 asection *section)
2139 {
2140 const char *section_name;
2141 lang_statement_union_type *l;
2142
2143 if (!wild->filenames_sorted
2144 && (sec == NULL || sec->spec.sorted == none))
2145 return NULL;
2146
2147 section_name = bfd_get_section_name (file->the_bfd, section);
2148 for (l = wild->children.head; l != NULL; l = l->header.next)
2149 {
2150 lang_input_section_type *ls;
2151
2152 if (l->header.type != lang_input_section_enum)
2153 continue;
2154 ls = &l->input_section;
2155
2156 /* Sorting by filename takes precedence over sorting by section
2157 name. */
2158
2159 if (wild->filenames_sorted)
2160 {
2161 const char *fn, *ln;
2162 bfd_boolean fa, la;
2163 int i;
2164
2165 /* The PE support for the .idata section as generated by
2166 dlltool assumes that files will be sorted by the name of
2167 the archive and then the name of the file within the
2168 archive. */
2169
2170 if (file->the_bfd != NULL
2171 && bfd_my_archive (file->the_bfd) != NULL)
2172 {
2173 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2174 fa = TRUE;
2175 }
2176 else
2177 {
2178 fn = file->filename;
2179 fa = FALSE;
2180 }
2181
2182 if (bfd_my_archive (ls->section->owner) != NULL)
2183 {
2184 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2185 la = TRUE;
2186 }
2187 else
2188 {
2189 ln = ls->section->owner->filename;
2190 la = FALSE;
2191 }
2192
2193 i = strcmp (fn, ln);
2194 if (i > 0)
2195 continue;
2196 else if (i < 0)
2197 break;
2198
2199 if (fa || la)
2200 {
2201 if (fa)
2202 fn = file->filename;
2203 if (la)
2204 ln = ls->section->owner->filename;
2205
2206 i = strcmp (fn, ln);
2207 if (i > 0)
2208 continue;
2209 else if (i < 0)
2210 break;
2211 }
2212 }
2213
2214 /* Here either the files are not sorted by name, or we are
2215 looking at the sections for this file. */
2216
2217 if (sec != NULL && sec->spec.sorted != none)
2218 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2219 break;
2220 }
2221
2222 return l;
2223 }
2224
2225 /* Expand a wild statement for a particular FILE. SECTION may be
2226 NULL, in which case it is a wild card. */
2227
2228 static void
2229 output_section_callback (lang_wild_statement_type *ptr,
2230 struct wildcard_list *sec,
2231 asection *section,
2232 lang_input_statement_type *file,
2233 void *output)
2234 {
2235 lang_statement_union_type *before;
2236
2237 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2238 if (unique_section_p (section))
2239 return;
2240
2241 before = wild_sort (ptr, sec, file, section);
2242
2243 /* Here BEFORE points to the lang_input_section which
2244 should follow the one we are about to add. If BEFORE
2245 is NULL, then the section should just go at the end
2246 of the current list. */
2247
2248 if (before == NULL)
2249 lang_add_section (&ptr->children, section,
2250 (lang_output_section_statement_type *) output);
2251 else
2252 {
2253 lang_statement_list_type list;
2254 lang_statement_union_type **pp;
2255
2256 lang_list_init (&list);
2257 lang_add_section (&list, section,
2258 (lang_output_section_statement_type *) output);
2259
2260 /* If we are discarding the section, LIST.HEAD will
2261 be NULL. */
2262 if (list.head != NULL)
2263 {
2264 ASSERT (list.head->header.next == NULL);
2265
2266 for (pp = &ptr->children.head;
2267 *pp != before;
2268 pp = &(*pp)->header.next)
2269 ASSERT (*pp != NULL);
2270
2271 list.head->header.next = *pp;
2272 *pp = list.head;
2273 }
2274 }
2275 }
2276
2277 /* Check if all sections in a wild statement for a particular FILE
2278 are readonly. */
2279
2280 static void
2281 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2282 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2283 asection *section,
2284 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2285 void *data)
2286 {
2287 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2288 if (unique_section_p (section))
2289 return;
2290
2291 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2292 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2293 }
2294
2295 /* This is passed a file name which must have been seen already and
2296 added to the statement tree. We will see if it has been opened
2297 already and had its symbols read. If not then we'll read it. */
2298
2299 static lang_input_statement_type *
2300 lookup_name (const char *name)
2301 {
2302 lang_input_statement_type *search;
2303
2304 for (search = (lang_input_statement_type *) input_file_chain.head;
2305 search != NULL;
2306 search = (lang_input_statement_type *) search->next_real_file)
2307 {
2308 /* Use the local_sym_name as the name of the file that has
2309 already been loaded as filename might have been transformed
2310 via the search directory lookup mechanism. */
2311 const char *filename = search->local_sym_name;
2312
2313 if (filename != NULL
2314 && strcmp (filename, name) == 0)
2315 break;
2316 }
2317
2318 if (search == NULL)
2319 search = new_afile (name, lang_input_file_is_search_file_enum,
2320 default_target, FALSE);
2321
2322 /* If we have already added this file, or this file is not real
2323 don't add this file. */
2324 if (search->loaded || !search->real)
2325 return search;
2326
2327 if (! load_symbols (search, NULL))
2328 return NULL;
2329
2330 return search;
2331 }
2332
2333 /* Save LIST as a list of libraries whose symbols should not be exported. */
2334
2335 struct excluded_lib
2336 {
2337 char *name;
2338 struct excluded_lib *next;
2339 };
2340 static struct excluded_lib *excluded_libs;
2341
2342 void
2343 add_excluded_libs (const char *list)
2344 {
2345 const char *p = list, *end;
2346
2347 while (*p != '\0')
2348 {
2349 struct excluded_lib *entry;
2350 end = strpbrk (p, ",:");
2351 if (end == NULL)
2352 end = p + strlen (p);
2353 entry = xmalloc (sizeof (*entry));
2354 entry->next = excluded_libs;
2355 entry->name = xmalloc (end - p + 1);
2356 memcpy (entry->name, p, end - p);
2357 entry->name[end - p] = '\0';
2358 excluded_libs = entry;
2359 if (*end == '\0')
2360 break;
2361 p = end + 1;
2362 }
2363 }
2364
2365 static void
2366 check_excluded_libs (bfd *abfd)
2367 {
2368 struct excluded_lib *lib = excluded_libs;
2369
2370 while (lib)
2371 {
2372 int len = strlen (lib->name);
2373 const char *filename = lbasename (abfd->filename);
2374
2375 if (strcmp (lib->name, "ALL") == 0)
2376 {
2377 abfd->no_export = TRUE;
2378 return;
2379 }
2380
2381 if (strncmp (lib->name, filename, len) == 0
2382 && (filename[len] == '\0'
2383 || (filename[len] == '.' && filename[len + 1] == 'a'
2384 && filename[len + 2] == '\0')))
2385 {
2386 abfd->no_export = TRUE;
2387 return;
2388 }
2389
2390 lib = lib->next;
2391 }
2392 }
2393
2394 /* Get the symbols for an input file. */
2395
2396 static bfd_boolean
2397 load_symbols (lang_input_statement_type *entry,
2398 lang_statement_list_type *place)
2399 {
2400 char **matching;
2401
2402 if (entry->loaded)
2403 return TRUE;
2404
2405 ldfile_open_file (entry);
2406
2407 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2408 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2409 {
2410 bfd_error_type err;
2411 lang_statement_list_type *hold;
2412 bfd_boolean bad_load = TRUE;
2413 bfd_boolean save_ldlang_sysrooted_script;
2414 bfd_boolean save_as_needed, save_add_needed;
2415
2416 err = bfd_get_error ();
2417
2418 /* See if the emulation has some special knowledge. */
2419 if (ldemul_unrecognized_file (entry))
2420 return TRUE;
2421
2422 if (err == bfd_error_file_ambiguously_recognized)
2423 {
2424 char **p;
2425
2426 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2427 einfo (_("%B: matching formats:"), entry->the_bfd);
2428 for (p = matching; *p != NULL; p++)
2429 einfo (" %s", *p);
2430 einfo ("%F\n");
2431 }
2432 else if (err != bfd_error_file_not_recognized
2433 || place == NULL)
2434 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2435 else
2436 bad_load = FALSE;
2437
2438 bfd_close (entry->the_bfd);
2439 entry->the_bfd = NULL;
2440
2441 /* Try to interpret the file as a linker script. */
2442 ldfile_open_command_file (entry->filename);
2443
2444 hold = stat_ptr;
2445 stat_ptr = place;
2446 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2447 ldlang_sysrooted_script = entry->sysrooted;
2448 save_as_needed = as_needed;
2449 as_needed = entry->as_needed;
2450 save_add_needed = add_needed;
2451 add_needed = entry->add_needed;
2452
2453 ldfile_assumed_script = TRUE;
2454 parser_input = input_script;
2455 /* We want to use the same -Bdynamic/-Bstatic as the one for
2456 ENTRY. */
2457 config.dynamic_link = entry->dynamic;
2458 yyparse ();
2459 ldfile_assumed_script = FALSE;
2460
2461 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2462 as_needed = save_as_needed;
2463 add_needed = save_add_needed;
2464 stat_ptr = hold;
2465
2466 return ! bad_load;
2467 }
2468
2469 if (ldemul_recognized_file (entry))
2470 return TRUE;
2471
2472 /* We don't call ldlang_add_file for an archive. Instead, the
2473 add_symbols entry point will call ldlang_add_file, via the
2474 add_archive_element callback, for each element of the archive
2475 which is used. */
2476 switch (bfd_get_format (entry->the_bfd))
2477 {
2478 default:
2479 break;
2480
2481 case bfd_object:
2482 ldlang_add_file (entry);
2483 if (trace_files || trace_file_tries)
2484 info_msg ("%I\n", entry);
2485 break;
2486
2487 case bfd_archive:
2488 check_excluded_libs (entry->the_bfd);
2489
2490 if (entry->whole_archive)
2491 {
2492 bfd *member = NULL;
2493 bfd_boolean loaded = TRUE;
2494
2495 for (;;)
2496 {
2497 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2498
2499 if (member == NULL)
2500 break;
2501
2502 if (! bfd_check_format (member, bfd_object))
2503 {
2504 einfo (_("%F%B: member %B in archive is not an object\n"),
2505 entry->the_bfd, member);
2506 loaded = FALSE;
2507 }
2508
2509 if (! ((*link_info.callbacks->add_archive_element)
2510 (&link_info, member, "--whole-archive")))
2511 abort ();
2512
2513 if (! bfd_link_add_symbols (member, &link_info))
2514 {
2515 einfo (_("%F%B: could not read symbols: %E\n"), member);
2516 loaded = FALSE;
2517 }
2518 }
2519
2520 entry->loaded = loaded;
2521 return loaded;
2522 }
2523 break;
2524 }
2525
2526 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2527 entry->loaded = TRUE;
2528 else
2529 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2530
2531 return entry->loaded;
2532 }
2533
2534 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2535 may be NULL, indicating that it is a wildcard. Separate
2536 lang_input_section statements are created for each part of the
2537 expansion; they are added after the wild statement S. OUTPUT is
2538 the output section. */
2539
2540 static void
2541 wild (lang_wild_statement_type *s,
2542 const char *target ATTRIBUTE_UNUSED,
2543 lang_output_section_statement_type *output)
2544 {
2545 struct wildcard_list *sec;
2546
2547 if (s->handler_data[0]
2548 && s->handler_data[0]->spec.sorted == by_name
2549 && !s->filenames_sorted)
2550 {
2551 lang_section_bst_type *tree;
2552
2553 walk_wild (s, output_section_callback_fast, output);
2554
2555 tree = (lang_section_bst_type *) s->handler_data[1];
2556 if (tree)
2557 output_section_callback_tree_to_list (s, tree, output);
2558 s->handler_data[1] = NULL;
2559 }
2560 else
2561 walk_wild (s, output_section_callback, output);
2562
2563 if (default_common_section == NULL)
2564 for (sec = s->section_list; sec != NULL; sec = sec->next)
2565 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2566 {
2567 /* Remember the section that common is going to in case we
2568 later get something which doesn't know where to put it. */
2569 default_common_section = output;
2570 break;
2571 }
2572 }
2573
2574 /* Return TRUE iff target is the sought target. */
2575
2576 static int
2577 get_target (const bfd_target *target, void *data)
2578 {
2579 const char *sought = data;
2580
2581 return strcmp (target->name, sought) == 0;
2582 }
2583
2584 /* Like strcpy() but convert to lower case as well. */
2585
2586 static void
2587 stricpy (char *dest, char *src)
2588 {
2589 char c;
2590
2591 while ((c = *src++) != 0)
2592 *dest++ = TOLOWER (c);
2593
2594 *dest = 0;
2595 }
2596
2597 /* Remove the first occurrence of needle (if any) in haystack
2598 from haystack. */
2599
2600 static void
2601 strcut (char *haystack, char *needle)
2602 {
2603 haystack = strstr (haystack, needle);
2604
2605 if (haystack)
2606 {
2607 char *src;
2608
2609 for (src = haystack + strlen (needle); *src;)
2610 *haystack++ = *src++;
2611
2612 *haystack = 0;
2613 }
2614 }
2615
2616 /* Compare two target format name strings.
2617 Return a value indicating how "similar" they are. */
2618
2619 static int
2620 name_compare (char *first, char *second)
2621 {
2622 char *copy1;
2623 char *copy2;
2624 int result;
2625
2626 copy1 = xmalloc (strlen (first) + 1);
2627 copy2 = xmalloc (strlen (second) + 1);
2628
2629 /* Convert the names to lower case. */
2630 stricpy (copy1, first);
2631 stricpy (copy2, second);
2632
2633 /* Remove size and endian strings from the name. */
2634 strcut (copy1, "big");
2635 strcut (copy1, "little");
2636 strcut (copy2, "big");
2637 strcut (copy2, "little");
2638
2639 /* Return a value based on how many characters match,
2640 starting from the beginning. If both strings are
2641 the same then return 10 * their length. */
2642 for (result = 0; copy1[result] == copy2[result]; result++)
2643 if (copy1[result] == 0)
2644 {
2645 result *= 10;
2646 break;
2647 }
2648
2649 free (copy1);
2650 free (copy2);
2651
2652 return result;
2653 }
2654
2655 /* Set by closest_target_match() below. */
2656 static const bfd_target *winner;
2657
2658 /* Scan all the valid bfd targets looking for one that has the endianness
2659 requirement that was specified on the command line, and is the nearest
2660 match to the original output target. */
2661
2662 static int
2663 closest_target_match (const bfd_target *target, void *data)
2664 {
2665 const bfd_target *original = data;
2666
2667 if (command_line.endian == ENDIAN_BIG
2668 && target->byteorder != BFD_ENDIAN_BIG)
2669 return 0;
2670
2671 if (command_line.endian == ENDIAN_LITTLE
2672 && target->byteorder != BFD_ENDIAN_LITTLE)
2673 return 0;
2674
2675 /* Must be the same flavour. */
2676 if (target->flavour != original->flavour)
2677 return 0;
2678
2679 /* If we have not found a potential winner yet, then record this one. */
2680 if (winner == NULL)
2681 {
2682 winner = target;
2683 return 0;
2684 }
2685
2686 /* Oh dear, we now have two potential candidates for a successful match.
2687 Compare their names and choose the better one. */
2688 if (name_compare (target->name, original->name)
2689 > name_compare (winner->name, original->name))
2690 winner = target;
2691
2692 /* Keep on searching until wqe have checked them all. */
2693 return 0;
2694 }
2695
2696 /* Return the BFD target format of the first input file. */
2697
2698 static char *
2699 get_first_input_target (void)
2700 {
2701 char *target = NULL;
2702
2703 LANG_FOR_EACH_INPUT_STATEMENT (s)
2704 {
2705 if (s->header.type == lang_input_statement_enum
2706 && s->real)
2707 {
2708 ldfile_open_file (s);
2709
2710 if (s->the_bfd != NULL
2711 && bfd_check_format (s->the_bfd, bfd_object))
2712 {
2713 target = bfd_get_target (s->the_bfd);
2714
2715 if (target != NULL)
2716 break;
2717 }
2718 }
2719 }
2720
2721 return target;
2722 }
2723
2724 const char *
2725 lang_get_output_target (void)
2726 {
2727 const char *target;
2728
2729 /* Has the user told us which output format to use? */
2730 if (output_target != NULL)
2731 return output_target;
2732
2733 /* No - has the current target been set to something other than
2734 the default? */
2735 if (current_target != default_target)
2736 return current_target;
2737
2738 /* No - can we determine the format of the first input file? */
2739 target = get_first_input_target ();
2740 if (target != NULL)
2741 return target;
2742
2743 /* Failed - use the default output target. */
2744 return default_target;
2745 }
2746
2747 /* Open the output file. */
2748
2749 static bfd *
2750 open_output (const char *name)
2751 {
2752 bfd *output;
2753
2754 output_target = lang_get_output_target ();
2755
2756 /* Has the user requested a particular endianness on the command
2757 line? */
2758 if (command_line.endian != ENDIAN_UNSET)
2759 {
2760 const bfd_target *target;
2761 enum bfd_endian desired_endian;
2762
2763 /* Get the chosen target. */
2764 target = bfd_search_for_target (get_target, (void *) output_target);
2765
2766 /* If the target is not supported, we cannot do anything. */
2767 if (target != NULL)
2768 {
2769 if (command_line.endian == ENDIAN_BIG)
2770 desired_endian = BFD_ENDIAN_BIG;
2771 else
2772 desired_endian = BFD_ENDIAN_LITTLE;
2773
2774 /* See if the target has the wrong endianness. This should
2775 not happen if the linker script has provided big and
2776 little endian alternatives, but some scrips don't do
2777 this. */
2778 if (target->byteorder != desired_endian)
2779 {
2780 /* If it does, then see if the target provides
2781 an alternative with the correct endianness. */
2782 if (target->alternative_target != NULL
2783 && (target->alternative_target->byteorder == desired_endian))
2784 output_target = target->alternative_target->name;
2785 else
2786 {
2787 /* Try to find a target as similar as possible to
2788 the default target, but which has the desired
2789 endian characteristic. */
2790 bfd_search_for_target (closest_target_match,
2791 (void *) target);
2792
2793 /* Oh dear - we could not find any targets that
2794 satisfy our requirements. */
2795 if (winner == NULL)
2796 einfo (_("%P: warning: could not find any targets"
2797 " that match endianness requirement\n"));
2798 else
2799 output_target = winner->name;
2800 }
2801 }
2802 }
2803 }
2804
2805 output = bfd_openw (name, output_target);
2806
2807 if (output == NULL)
2808 {
2809 if (bfd_get_error () == bfd_error_invalid_target)
2810 einfo (_("%P%F: target %s not found\n"), output_target);
2811
2812 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2813 }
2814
2815 delete_output_file_on_failure = TRUE;
2816
2817 if (! bfd_set_format (output, bfd_object))
2818 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2819 if (! bfd_set_arch_mach (output,
2820 ldfile_output_architecture,
2821 ldfile_output_machine))
2822 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2823
2824 link_info.hash = bfd_link_hash_table_create (output);
2825 if (link_info.hash == NULL)
2826 einfo (_("%P%F: can not create hash table: %E\n"));
2827
2828 bfd_set_gp_size (output, g_switch_value);
2829 return output;
2830 }
2831
2832 static void
2833 ldlang_open_output (lang_statement_union_type *statement)
2834 {
2835 switch (statement->header.type)
2836 {
2837 case lang_output_statement_enum:
2838 ASSERT (output_bfd == NULL);
2839 output_bfd = open_output (statement->output_statement.name);
2840 ldemul_set_output_arch ();
2841 if (config.magic_demand_paged && !link_info.relocatable)
2842 output_bfd->flags |= D_PAGED;
2843 else
2844 output_bfd->flags &= ~D_PAGED;
2845 if (config.text_read_only)
2846 output_bfd->flags |= WP_TEXT;
2847 else
2848 output_bfd->flags &= ~WP_TEXT;
2849 if (link_info.traditional_format)
2850 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2851 else
2852 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2853 break;
2854
2855 case lang_target_statement_enum:
2856 current_target = statement->target_statement.target;
2857 break;
2858 default:
2859 break;
2860 }
2861 }
2862
2863 /* Convert between addresses in bytes and sizes in octets.
2864 For currently supported targets, octets_per_byte is always a power
2865 of two, so we can use shifts. */
2866 #define TO_ADDR(X) ((X) >> opb_shift)
2867 #define TO_SIZE(X) ((X) << opb_shift)
2868
2869 /* Support the above. */
2870 static unsigned int opb_shift = 0;
2871
2872 static void
2873 init_opb (void)
2874 {
2875 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2876 ldfile_output_machine);
2877 opb_shift = 0;
2878 if (x > 1)
2879 while ((x & 1) == 0)
2880 {
2881 x >>= 1;
2882 ++opb_shift;
2883 }
2884 ASSERT (x == 1);
2885 }
2886
2887 /* Open all the input files. */
2888
2889 static void
2890 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2891 {
2892 for (; s != NULL; s = s->header.next)
2893 {
2894 switch (s->header.type)
2895 {
2896 case lang_constructors_statement_enum:
2897 open_input_bfds (constructor_list.head, force);
2898 break;
2899 case lang_output_section_statement_enum:
2900 open_input_bfds (s->output_section_statement.children.head, force);
2901 break;
2902 case lang_wild_statement_enum:
2903 /* Maybe we should load the file's symbols. */
2904 if (s->wild_statement.filename
2905 && ! wildcardp (s->wild_statement.filename))
2906 lookup_name (s->wild_statement.filename);
2907 open_input_bfds (s->wild_statement.children.head, force);
2908 break;
2909 case lang_group_statement_enum:
2910 {
2911 struct bfd_link_hash_entry *undefs;
2912
2913 /* We must continually search the entries in the group
2914 until no new symbols are added to the list of undefined
2915 symbols. */
2916
2917 do
2918 {
2919 undefs = link_info.hash->undefs_tail;
2920 open_input_bfds (s->group_statement.children.head, TRUE);
2921 }
2922 while (undefs != link_info.hash->undefs_tail);
2923 }
2924 break;
2925 case lang_target_statement_enum:
2926 current_target = s->target_statement.target;
2927 break;
2928 case lang_input_statement_enum:
2929 if (s->input_statement.real)
2930 {
2931 lang_statement_list_type add;
2932
2933 s->input_statement.target = current_target;
2934
2935 /* If we are being called from within a group, and this
2936 is an archive which has already been searched, then
2937 force it to be researched unless the whole archive
2938 has been loaded already. */
2939 if (force
2940 && !s->input_statement.whole_archive
2941 && s->input_statement.loaded
2942 && bfd_check_format (s->input_statement.the_bfd,
2943 bfd_archive))
2944 s->input_statement.loaded = FALSE;
2945
2946 lang_list_init (&add);
2947
2948 if (! load_symbols (&s->input_statement, &add))
2949 config.make_executable = FALSE;
2950
2951 if (add.head != NULL)
2952 {
2953 *add.tail = s->header.next;
2954 s->header.next = add.head;
2955 }
2956 }
2957 break;
2958 default:
2959 break;
2960 }
2961 }
2962 }
2963
2964 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2965
2966 void
2967 lang_track_definedness (const char *name)
2968 {
2969 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2970 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2971 }
2972
2973 /* New-function for the definedness hash table. */
2974
2975 static struct bfd_hash_entry *
2976 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2977 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2978 const char *name ATTRIBUTE_UNUSED)
2979 {
2980 struct lang_definedness_hash_entry *ret
2981 = (struct lang_definedness_hash_entry *) entry;
2982
2983 if (ret == NULL)
2984 ret = (struct lang_definedness_hash_entry *)
2985 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2986
2987 if (ret == NULL)
2988 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2989
2990 ret->iteration = -1;
2991 return &ret->root;
2992 }
2993
2994 /* Return the iteration when the definition of NAME was last updated. A
2995 value of -1 means that the symbol is not defined in the linker script
2996 or the command line, but may be defined in the linker symbol table. */
2997
2998 int
2999 lang_symbol_definition_iteration (const char *name)
3000 {
3001 struct lang_definedness_hash_entry *defentry
3002 = (struct lang_definedness_hash_entry *)
3003 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3004
3005 /* We've already created this one on the presence of DEFINED in the
3006 script, so it can't be NULL unless something is borked elsewhere in
3007 the code. */
3008 if (defentry == NULL)
3009 FAIL ();
3010
3011 return defentry->iteration;
3012 }
3013
3014 /* Update the definedness state of NAME. */
3015
3016 void
3017 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3018 {
3019 struct lang_definedness_hash_entry *defentry
3020 = (struct lang_definedness_hash_entry *)
3021 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3022
3023 /* We don't keep track of symbols not tested with DEFINED. */
3024 if (defentry == NULL)
3025 return;
3026
3027 /* If the symbol was already defined, and not from an earlier statement
3028 iteration, don't update the definedness iteration, because that'd
3029 make the symbol seem defined in the linker script at this point, and
3030 it wasn't; it was defined in some object. If we do anyway, DEFINED
3031 would start to yield false before this point and the construct "sym =
3032 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3033 in an object. */
3034 if (h->type != bfd_link_hash_undefined
3035 && h->type != bfd_link_hash_common
3036 && h->type != bfd_link_hash_new
3037 && defentry->iteration == -1)
3038 return;
3039
3040 defentry->iteration = lang_statement_iteration;
3041 }
3042
3043 /* Add the supplied name to the symbol table as an undefined reference.
3044 This is a two step process as the symbol table doesn't even exist at
3045 the time the ld command line is processed. First we put the name
3046 on a list, then, once the output file has been opened, transfer the
3047 name to the symbol table. */
3048
3049 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3050
3051 #define ldlang_undef_chain_list_head entry_symbol.next
3052
3053 void
3054 ldlang_add_undef (const char *const name)
3055 {
3056 ldlang_undef_chain_list_type *new =
3057 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3058
3059 new->next = ldlang_undef_chain_list_head;
3060 ldlang_undef_chain_list_head = new;
3061
3062 new->name = xstrdup (name);
3063
3064 if (output_bfd != NULL)
3065 insert_undefined (new->name);
3066 }
3067
3068 /* Insert NAME as undefined in the symbol table. */
3069
3070 static void
3071 insert_undefined (const char *name)
3072 {
3073 struct bfd_link_hash_entry *h;
3074
3075 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3076 if (h == NULL)
3077 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3078 if (h->type == bfd_link_hash_new)
3079 {
3080 h->type = bfd_link_hash_undefined;
3081 h->u.undef.abfd = NULL;
3082 bfd_link_add_undef (link_info.hash, h);
3083 }
3084 }
3085
3086 /* Run through the list of undefineds created above and place them
3087 into the linker hash table as undefined symbols belonging to the
3088 script file. */
3089
3090 static void
3091 lang_place_undefineds (void)
3092 {
3093 ldlang_undef_chain_list_type *ptr;
3094
3095 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3096 insert_undefined (ptr->name);
3097 }
3098
3099 /* Check for all readonly or some readwrite sections. */
3100
3101 static void
3102 check_input_sections
3103 (lang_statement_union_type *s,
3104 lang_output_section_statement_type *output_section_statement)
3105 {
3106 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3107 {
3108 switch (s->header.type)
3109 {
3110 case lang_wild_statement_enum:
3111 walk_wild (&s->wild_statement, check_section_callback,
3112 output_section_statement);
3113 if (! output_section_statement->all_input_readonly)
3114 return;
3115 break;
3116 case lang_constructors_statement_enum:
3117 check_input_sections (constructor_list.head,
3118 output_section_statement);
3119 if (! output_section_statement->all_input_readonly)
3120 return;
3121 break;
3122 case lang_group_statement_enum:
3123 check_input_sections (s->group_statement.children.head,
3124 output_section_statement);
3125 if (! output_section_statement->all_input_readonly)
3126 return;
3127 break;
3128 default:
3129 break;
3130 }
3131 }
3132 }
3133
3134 /* Update wildcard statements if needed. */
3135
3136 static void
3137 update_wild_statements (lang_statement_union_type *s)
3138 {
3139 struct wildcard_list *sec;
3140
3141 switch (sort_section)
3142 {
3143 default:
3144 FAIL ();
3145
3146 case none:
3147 break;
3148
3149 case by_name:
3150 case by_alignment:
3151 for (; s != NULL; s = s->header.next)
3152 {
3153 switch (s->header.type)
3154 {
3155 default:
3156 break;
3157
3158 case lang_wild_statement_enum:
3159 sec = s->wild_statement.section_list;
3160 for (sec = s->wild_statement.section_list; sec != NULL;
3161 sec = sec->next)
3162 {
3163 switch (sec->spec.sorted)
3164 {
3165 case none:
3166 sec->spec.sorted = sort_section;
3167 break;
3168 case by_name:
3169 if (sort_section == by_alignment)
3170 sec->spec.sorted = by_name_alignment;
3171 break;
3172 case by_alignment:
3173 if (sort_section == by_name)
3174 sec->spec.sorted = by_alignment_name;
3175 break;
3176 default:
3177 break;
3178 }
3179 }
3180 break;
3181
3182 case lang_constructors_statement_enum:
3183 update_wild_statements (constructor_list.head);
3184 break;
3185
3186 case lang_output_section_statement_enum:
3187 update_wild_statements
3188 (s->output_section_statement.children.head);
3189 break;
3190
3191 case lang_group_statement_enum:
3192 update_wild_statements (s->group_statement.children.head);
3193 break;
3194 }
3195 }
3196 break;
3197 }
3198 }
3199
3200 /* Open input files and attach to output sections. */
3201
3202 static void
3203 map_input_to_output_sections
3204 (lang_statement_union_type *s, const char *target,
3205 lang_output_section_statement_type *os)
3206 {
3207 flagword flags;
3208
3209 for (; s != NULL; s = s->header.next)
3210 {
3211 switch (s->header.type)
3212 {
3213 case lang_wild_statement_enum:
3214 wild (&s->wild_statement, target, os);
3215 break;
3216 case lang_constructors_statement_enum:
3217 map_input_to_output_sections (constructor_list.head,
3218 target,
3219 os);
3220 break;
3221 case lang_output_section_statement_enum:
3222 if (s->output_section_statement.constraint)
3223 {
3224 if (s->output_section_statement.constraint != ONLY_IF_RW
3225 && s->output_section_statement.constraint != ONLY_IF_RO)
3226 break;
3227 s->output_section_statement.all_input_readonly = TRUE;
3228 check_input_sections (s->output_section_statement.children.head,
3229 &s->output_section_statement);
3230 if ((s->output_section_statement.all_input_readonly
3231 && s->output_section_statement.constraint == ONLY_IF_RW)
3232 || (!s->output_section_statement.all_input_readonly
3233 && s->output_section_statement.constraint == ONLY_IF_RO))
3234 {
3235 s->output_section_statement.constraint = -1;
3236 break;
3237 }
3238 }
3239
3240 map_input_to_output_sections (s->output_section_statement.children.head,
3241 target,
3242 &s->output_section_statement);
3243 break;
3244 case lang_output_statement_enum:
3245 break;
3246 case lang_target_statement_enum:
3247 target = s->target_statement.target;
3248 break;
3249 case lang_group_statement_enum:
3250 map_input_to_output_sections (s->group_statement.children.head,
3251 target,
3252 os);
3253 break;
3254 case lang_data_statement_enum:
3255 /* Make sure that any sections mentioned in the expression
3256 are initialized. */
3257 exp_init_os (s->data_statement.exp);
3258 flags = SEC_HAS_CONTENTS;
3259 /* The output section gets contents, and then we inspect for
3260 any flags set in the input script which override any ALLOC. */
3261 if (!(os->flags & SEC_NEVER_LOAD))
3262 flags |= SEC_ALLOC | SEC_LOAD;
3263 if (os->bfd_section == NULL)
3264 init_os (os, NULL, flags);
3265 else
3266 os->bfd_section->flags |= flags;
3267 break;
3268 case lang_input_section_enum:
3269 break;
3270 case lang_fill_statement_enum:
3271 case lang_object_symbols_statement_enum:
3272 case lang_reloc_statement_enum:
3273 case lang_padding_statement_enum:
3274 case lang_input_statement_enum:
3275 if (os != NULL && os->bfd_section == NULL)
3276 init_os (os, NULL, 0);
3277 break;
3278 case lang_assignment_statement_enum:
3279 if (os != NULL && os->bfd_section == NULL)
3280 init_os (os, NULL, 0);
3281
3282 /* Make sure that any sections mentioned in the assignment
3283 are initialized. */
3284 exp_init_os (s->assignment_statement.exp);
3285 break;
3286 case lang_afile_asection_pair_statement_enum:
3287 FAIL ();
3288 break;
3289 case lang_address_statement_enum:
3290 /* Mark the specified section with the supplied address.
3291
3292 If this section was actually a segment marker, then the
3293 directive is ignored if the linker script explicitly
3294 processed the segment marker. Originally, the linker
3295 treated segment directives (like -Ttext on the
3296 command-line) as section directives. We honor the
3297 section directive semantics for backwards compatibilty;
3298 linker scripts that do not specifically check for
3299 SEGMENT_START automatically get the old semantics. */
3300 if (!s->address_statement.segment
3301 || !s->address_statement.segment->used)
3302 {
3303 lang_output_section_statement_type *aos
3304 = (lang_output_section_statement_lookup
3305 (s->address_statement.section_name));
3306
3307 if (aos->bfd_section == NULL)
3308 init_os (aos, NULL, 0);
3309 aos->addr_tree = s->address_statement.address;
3310 }
3311 break;
3312 }
3313 }
3314 }
3315
3316 /* An output section might have been removed after its statement was
3317 added. For example, ldemul_before_allocation can remove dynamic
3318 sections if they turn out to be not needed. Clean them up here. */
3319
3320 void
3321 strip_excluded_output_sections (void)
3322 {
3323 lang_output_section_statement_type *os;
3324
3325 /* Run lang_size_sections (if not already done). */
3326 if (expld.phase != lang_mark_phase_enum)
3327 {
3328 expld.phase = lang_mark_phase_enum;
3329 expld.dataseg.phase = exp_dataseg_none;
3330 one_lang_size_sections_pass (NULL, FALSE);
3331 lang_reset_memory_regions ();
3332 }
3333
3334 for (os = &lang_output_section_statement.head->output_section_statement;
3335 os != NULL;
3336 os = os->next)
3337 {
3338 asection *output_section;
3339 bfd_boolean exclude;
3340
3341 if (os->constraint == -1)
3342 continue;
3343
3344 output_section = os->bfd_section;
3345 if (output_section == NULL)
3346 continue;
3347
3348 exclude = (output_section->rawsize == 0
3349 && (output_section->flags & SEC_KEEP) == 0
3350 && !bfd_section_removed_from_list (output_bfd,
3351 output_section));
3352
3353 /* Some sections have not yet been sized, notably .gnu.version,
3354 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3355 input sections, so don't drop output sections that have such
3356 input sections unless they are also marked SEC_EXCLUDE. */
3357 if (exclude && output_section->map_head.s != NULL)
3358 {
3359 asection *s;
3360
3361 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3362 if ((s->flags & SEC_LINKER_CREATED) != 0
3363 && (s->flags & SEC_EXCLUDE) == 0)
3364 {
3365 exclude = FALSE;
3366 break;
3367 }
3368 }
3369
3370 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3371 output_section->map_head.link_order = NULL;
3372 output_section->map_tail.link_order = NULL;
3373
3374 if (exclude)
3375 {
3376 /* We don't set bfd_section to NULL since bfd_section of the
3377 removed output section statement may still be used. */
3378 os->ignored = TRUE;
3379 output_section->flags |= SEC_EXCLUDE;
3380 bfd_section_list_remove (output_bfd, output_section);
3381 output_bfd->section_count--;
3382 }
3383 }
3384
3385 /* Stop future calls to lang_add_section from messing with map_head
3386 and map_tail link_order fields. */
3387 stripped_excluded_sections = TRUE;
3388 }
3389
3390 static void
3391 print_output_section_statement
3392 (lang_output_section_statement_type *output_section_statement)
3393 {
3394 asection *section = output_section_statement->bfd_section;
3395 int len;
3396
3397 if (output_section_statement != abs_output_section)
3398 {
3399 minfo ("\n%s", output_section_statement->name);
3400
3401 if (section != NULL)
3402 {
3403 print_dot = section->vma;
3404
3405 len = strlen (output_section_statement->name);
3406 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3407 {
3408 print_nl ();
3409 len = 0;
3410 }
3411 while (len < SECTION_NAME_MAP_LENGTH)
3412 {
3413 print_space ();
3414 ++len;
3415 }
3416
3417 minfo ("0x%V %W", section->vma, section->size);
3418
3419 if (output_section_statement->load_base != NULL)
3420 {
3421 bfd_vma addr;
3422
3423 addr = exp_get_abs_int (output_section_statement->load_base, 0,
3424 "load base");
3425 minfo (_(" load address 0x%V"), addr);
3426 }
3427 }
3428
3429 print_nl ();
3430 }
3431
3432 print_statement_list (output_section_statement->children.head,
3433 output_section_statement);
3434 }
3435
3436 /* Scan for the use of the destination in the right hand side
3437 of an expression. In such cases we will not compute the
3438 correct expression, since the value of DST that is used on
3439 the right hand side will be its final value, not its value
3440 just before this expression is evaluated. */
3441
3442 static bfd_boolean
3443 scan_for_self_assignment (const char * dst, etree_type * rhs)
3444 {
3445 if (rhs == NULL || dst == NULL)
3446 return FALSE;
3447
3448 switch (rhs->type.node_class)
3449 {
3450 case etree_binary:
3451 return scan_for_self_assignment (dst, rhs->binary.lhs)
3452 || scan_for_self_assignment (dst, rhs->binary.rhs);
3453
3454 case etree_trinary:
3455 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3456 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3457
3458 case etree_assign:
3459 case etree_provided:
3460 case etree_provide:
3461 if (strcmp (dst, rhs->assign.dst) == 0)
3462 return TRUE;
3463 return scan_for_self_assignment (dst, rhs->assign.src);
3464
3465 case etree_unary:
3466 return scan_for_self_assignment (dst, rhs->unary.child);
3467
3468 case etree_value:
3469 if (rhs->value.str)
3470 return strcmp (dst, rhs->value.str) == 0;
3471 return FALSE;
3472
3473 case etree_name:
3474 if (rhs->name.name)
3475 return strcmp (dst, rhs->name.name) == 0;
3476 return FALSE;
3477
3478 default:
3479 break;
3480 }
3481
3482 return FALSE;
3483 }
3484
3485
3486 static void
3487 print_assignment (lang_assignment_statement_type *assignment,
3488 lang_output_section_statement_type *output_section)
3489 {
3490 unsigned int i;
3491 bfd_boolean is_dot;
3492 bfd_boolean computation_is_valid = TRUE;
3493 etree_type *tree;
3494
3495 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3496 print_space ();
3497
3498 if (assignment->exp->type.node_class == etree_assert)
3499 {
3500 is_dot = FALSE;
3501 tree = assignment->exp->assert_s.child;
3502 computation_is_valid = TRUE;
3503 }
3504 else
3505 {
3506 const char *dst = assignment->exp->assign.dst;
3507
3508 is_dot = (dst[0] == '.' && dst[1] == 0);
3509 tree = assignment->exp->assign.src;
3510 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3511 }
3512
3513 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3514 if (expld.result.valid_p)
3515 {
3516 bfd_vma value;
3517
3518 if (computation_is_valid)
3519 {
3520 value = expld.result.value;
3521
3522 if (expld.result.section)
3523 value += expld.result.section->vma;
3524
3525 minfo ("0x%V", value);
3526 if (is_dot)
3527 print_dot = value;
3528 }
3529 else
3530 {
3531 struct bfd_link_hash_entry *h;
3532
3533 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3534 FALSE, FALSE, TRUE);
3535 if (h)
3536 {
3537 value = h->u.def.value;
3538
3539 if (expld.result.section)
3540 value += expld.result.section->vma;
3541
3542 minfo ("[0x%V]", value);
3543 }
3544 else
3545 minfo ("[unresolved]");
3546 }
3547 }
3548 else
3549 {
3550 minfo ("*undef* ");
3551 #ifdef BFD64
3552 minfo (" ");
3553 #endif
3554 }
3555
3556 minfo (" ");
3557 exp_print_tree (assignment->exp);
3558 print_nl ();
3559 }
3560
3561 static void
3562 print_input_statement (lang_input_statement_type *statm)
3563 {
3564 if (statm->filename != NULL)
3565 {
3566 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3567 }
3568 }
3569
3570 /* Print all symbols defined in a particular section. This is called
3571 via bfd_link_hash_traverse, or by print_all_symbols. */
3572
3573 static bfd_boolean
3574 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3575 {
3576 asection *sec = ptr;
3577
3578 if ((hash_entry->type == bfd_link_hash_defined
3579 || hash_entry->type == bfd_link_hash_defweak)
3580 && sec == hash_entry->u.def.section)
3581 {
3582 int i;
3583
3584 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3585 print_space ();
3586 minfo ("0x%V ",
3587 (hash_entry->u.def.value
3588 + hash_entry->u.def.section->output_offset
3589 + hash_entry->u.def.section->output_section->vma));
3590
3591 minfo (" %T\n", hash_entry->root.string);
3592 }
3593
3594 return TRUE;
3595 }
3596
3597 static void
3598 print_all_symbols (asection *sec)
3599 {
3600 struct fat_user_section_struct *ud = get_userdata (sec);
3601 struct map_symbol_def *def;
3602
3603 if (!ud)
3604 return;
3605
3606 *ud->map_symbol_def_tail = 0;
3607 for (def = ud->map_symbol_def_head; def; def = def->next)
3608 print_one_symbol (def->entry, sec);
3609 }
3610
3611 /* Print information about an input section to the map file. */
3612
3613 static void
3614 print_input_section (asection *i)
3615 {
3616 bfd_size_type size = i->size;
3617 int len;
3618 bfd_vma addr;
3619
3620 init_opb ();
3621
3622 print_space ();
3623 minfo ("%s", i->name);
3624
3625 len = 1 + strlen (i->name);
3626 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3627 {
3628 print_nl ();
3629 len = 0;
3630 }
3631 while (len < SECTION_NAME_MAP_LENGTH)
3632 {
3633 print_space ();
3634 ++len;
3635 }
3636
3637 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3638 addr = i->output_section->vma + i->output_offset;
3639 else
3640 {
3641 addr = print_dot;
3642 size = 0;
3643 }
3644
3645 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3646
3647 if (size != i->rawsize && i->rawsize != 0)
3648 {
3649 len = SECTION_NAME_MAP_LENGTH + 3;
3650 #ifdef BFD64
3651 len += 16;
3652 #else
3653 len += 8;
3654 #endif
3655 while (len > 0)
3656 {
3657 print_space ();
3658 --len;
3659 }
3660
3661 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3662 }
3663
3664 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3665 {
3666 if (command_line.reduce_memory_overheads)
3667 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3668 else
3669 print_all_symbols (i);
3670
3671 print_dot = addr + TO_ADDR (size);
3672 }
3673 }
3674
3675 static void
3676 print_fill_statement (lang_fill_statement_type *fill)
3677 {
3678 size_t size;
3679 unsigned char *p;
3680 fputs (" FILL mask 0x", config.map_file);
3681 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3682 fprintf (config.map_file, "%02x", *p);
3683 fputs ("\n", config.map_file);
3684 }
3685
3686 static void
3687 print_data_statement (lang_data_statement_type *data)
3688 {
3689 int i;
3690 bfd_vma addr;
3691 bfd_size_type size;
3692 const char *name;
3693
3694 init_opb ();
3695 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3696 print_space ();
3697
3698 addr = data->output_offset;
3699 if (data->output_section != NULL)
3700 addr += data->output_section->vma;
3701
3702 switch (data->type)
3703 {
3704 default:
3705 abort ();
3706 case BYTE:
3707 size = BYTE_SIZE;
3708 name = "BYTE";
3709 break;
3710 case SHORT:
3711 size = SHORT_SIZE;
3712 name = "SHORT";
3713 break;
3714 case LONG:
3715 size = LONG_SIZE;
3716 name = "LONG";
3717 break;
3718 case QUAD:
3719 size = QUAD_SIZE;
3720 name = "QUAD";
3721 break;
3722 case SQUAD:
3723 size = QUAD_SIZE;
3724 name = "SQUAD";
3725 break;
3726 }
3727
3728 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3729
3730 if (data->exp->type.node_class != etree_value)
3731 {
3732 print_space ();
3733 exp_print_tree (data->exp);
3734 }
3735
3736 print_nl ();
3737
3738 print_dot = addr + TO_ADDR (size);
3739 }
3740
3741 /* Print an address statement. These are generated by options like
3742 -Ttext. */
3743
3744 static void
3745 print_address_statement (lang_address_statement_type *address)
3746 {
3747 minfo (_("Address of section %s set to "), address->section_name);
3748 exp_print_tree (address->address);
3749 print_nl ();
3750 }
3751
3752 /* Print a reloc statement. */
3753
3754 static void
3755 print_reloc_statement (lang_reloc_statement_type *reloc)
3756 {
3757 int i;
3758 bfd_vma addr;
3759 bfd_size_type size;
3760
3761 init_opb ();
3762 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3763 print_space ();
3764
3765 addr = reloc->output_offset;
3766 if (reloc->output_section != NULL)
3767 addr += reloc->output_section->vma;
3768
3769 size = bfd_get_reloc_size (reloc->howto);
3770
3771 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3772
3773 if (reloc->name != NULL)
3774 minfo ("%s+", reloc->name);
3775 else
3776 minfo ("%s+", reloc->section->name);
3777
3778 exp_print_tree (reloc->addend_exp);
3779
3780 print_nl ();
3781
3782 print_dot = addr + TO_ADDR (size);
3783 }
3784
3785 static void
3786 print_padding_statement (lang_padding_statement_type *s)
3787 {
3788 int len;
3789 bfd_vma addr;
3790
3791 init_opb ();
3792 minfo (" *fill*");
3793
3794 len = sizeof " *fill*" - 1;
3795 while (len < SECTION_NAME_MAP_LENGTH)
3796 {
3797 print_space ();
3798 ++len;
3799 }
3800
3801 addr = s->output_offset;
3802 if (s->output_section != NULL)
3803 addr += s->output_section->vma;
3804 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3805
3806 if (s->fill->size != 0)
3807 {
3808 size_t size;
3809 unsigned char *p;
3810 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3811 fprintf (config.map_file, "%02x", *p);
3812 }
3813
3814 print_nl ();
3815
3816 print_dot = addr + TO_ADDR (s->size);
3817 }
3818
3819 static void
3820 print_wild_statement (lang_wild_statement_type *w,
3821 lang_output_section_statement_type *os)
3822 {
3823 struct wildcard_list *sec;
3824
3825 print_space ();
3826
3827 if (w->filenames_sorted)
3828 minfo ("SORT(");
3829 if (w->filename != NULL)
3830 minfo ("%s", w->filename);
3831 else
3832 minfo ("*");
3833 if (w->filenames_sorted)
3834 minfo (")");
3835
3836 minfo ("(");
3837 for (sec = w->section_list; sec; sec = sec->next)
3838 {
3839 if (sec->spec.sorted)
3840 minfo ("SORT(");
3841 if (sec->spec.exclude_name_list != NULL)
3842 {
3843 name_list *tmp;
3844 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3845 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3846 minfo (" %s", tmp->name);
3847 minfo (") ");
3848 }
3849 if (sec->spec.name != NULL)
3850 minfo ("%s", sec->spec.name);
3851 else
3852 minfo ("*");
3853 if (sec->spec.sorted)
3854 minfo (")");
3855 if (sec->next)
3856 minfo (" ");
3857 }
3858 minfo (")");
3859
3860 print_nl ();
3861
3862 print_statement_list (w->children.head, os);
3863 }
3864
3865 /* Print a group statement. */
3866
3867 static void
3868 print_group (lang_group_statement_type *s,
3869 lang_output_section_statement_type *os)
3870 {
3871 fprintf (config.map_file, "START GROUP\n");
3872 print_statement_list (s->children.head, os);
3873 fprintf (config.map_file, "END GROUP\n");
3874 }
3875
3876 /* Print the list of statements in S.
3877 This can be called for any statement type. */
3878
3879 static void
3880 print_statement_list (lang_statement_union_type *s,
3881 lang_output_section_statement_type *os)
3882 {
3883 while (s != NULL)
3884 {
3885 print_statement (s, os);
3886 s = s->header.next;
3887 }
3888 }
3889
3890 /* Print the first statement in statement list S.
3891 This can be called for any statement type. */
3892
3893 static void
3894 print_statement (lang_statement_union_type *s,
3895 lang_output_section_statement_type *os)
3896 {
3897 switch (s->header.type)
3898 {
3899 default:
3900 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3901 FAIL ();
3902 break;
3903 case lang_constructors_statement_enum:
3904 if (constructor_list.head != NULL)
3905 {
3906 if (constructors_sorted)
3907 minfo (" SORT (CONSTRUCTORS)\n");
3908 else
3909 minfo (" CONSTRUCTORS\n");
3910 print_statement_list (constructor_list.head, os);
3911 }
3912 break;
3913 case lang_wild_statement_enum:
3914 print_wild_statement (&s->wild_statement, os);
3915 break;
3916 case lang_address_statement_enum:
3917 print_address_statement (&s->address_statement);
3918 break;
3919 case lang_object_symbols_statement_enum:
3920 minfo (" CREATE_OBJECT_SYMBOLS\n");
3921 break;
3922 case lang_fill_statement_enum:
3923 print_fill_statement (&s->fill_statement);
3924 break;
3925 case lang_data_statement_enum:
3926 print_data_statement (&s->data_statement);
3927 break;
3928 case lang_reloc_statement_enum:
3929 print_reloc_statement (&s->reloc_statement);
3930 break;
3931 case lang_input_section_enum:
3932 print_input_section (s->input_section.section);
3933 break;
3934 case lang_padding_statement_enum:
3935 print_padding_statement (&s->padding_statement);
3936 break;
3937 case lang_output_section_statement_enum:
3938 print_output_section_statement (&s->output_section_statement);
3939 break;
3940 case lang_assignment_statement_enum:
3941 print_assignment (&s->assignment_statement, os);
3942 break;
3943 case lang_target_statement_enum:
3944 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3945 break;
3946 case lang_output_statement_enum:
3947 minfo ("OUTPUT(%s", s->output_statement.name);
3948 if (output_target != NULL)
3949 minfo (" %s", output_target);
3950 minfo (")\n");
3951 break;
3952 case lang_input_statement_enum:
3953 print_input_statement (&s->input_statement);
3954 break;
3955 case lang_group_statement_enum:
3956 print_group (&s->group_statement, os);
3957 break;
3958 case lang_afile_asection_pair_statement_enum:
3959 FAIL ();
3960 break;
3961 }
3962 }
3963
3964 static void
3965 print_statements (void)
3966 {
3967 print_statement_list (statement_list.head, abs_output_section);
3968 }
3969
3970 /* Print the first N statements in statement list S to STDERR.
3971 If N == 0, nothing is printed.
3972 If N < 0, the entire list is printed.
3973 Intended to be called from GDB. */
3974
3975 void
3976 dprint_statement (lang_statement_union_type *s, int n)
3977 {
3978 FILE *map_save = config.map_file;
3979
3980 config.map_file = stderr;
3981
3982 if (n < 0)
3983 print_statement_list (s, abs_output_section);
3984 else
3985 {
3986 while (s && --n >= 0)
3987 {
3988 print_statement (s, abs_output_section);
3989 s = s->header.next;
3990 }
3991 }
3992
3993 config.map_file = map_save;
3994 }
3995
3996 static void
3997 insert_pad (lang_statement_union_type **ptr,
3998 fill_type *fill,
3999 unsigned int alignment_needed,
4000 asection *output_section,
4001 bfd_vma dot)
4002 {
4003 static fill_type zero_fill = { 1, { 0 } };
4004 lang_statement_union_type *pad = NULL;
4005
4006 if (ptr != &statement_list.head)
4007 pad = ((lang_statement_union_type *)
4008 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4009 if (pad != NULL
4010 && pad->header.type == lang_padding_statement_enum
4011 && pad->padding_statement.output_section == output_section)
4012 {
4013 /* Use the existing pad statement. */
4014 }
4015 else if ((pad = *ptr) != NULL
4016 && pad->header.type == lang_padding_statement_enum
4017 && pad->padding_statement.output_section == output_section)
4018 {
4019 /* Use the existing pad statement. */
4020 }
4021 else
4022 {
4023 /* Make a new padding statement, linked into existing chain. */
4024 pad = stat_alloc (sizeof (lang_padding_statement_type));
4025 pad->header.next = *ptr;
4026 *ptr = pad;
4027 pad->header.type = lang_padding_statement_enum;
4028 pad->padding_statement.output_section = output_section;
4029 if (fill == NULL)
4030 fill = &zero_fill;
4031 pad->padding_statement.fill = fill;
4032 }
4033 pad->padding_statement.output_offset = dot - output_section->vma;
4034 pad->padding_statement.size = alignment_needed;
4035 output_section->size += alignment_needed;
4036 }
4037
4038 /* Work out how much this section will move the dot point. */
4039
4040 static bfd_vma
4041 size_input_section
4042 (lang_statement_union_type **this_ptr,
4043 lang_output_section_statement_type *output_section_statement,
4044 fill_type *fill,
4045 bfd_vma dot)
4046 {
4047 lang_input_section_type *is = &((*this_ptr)->input_section);
4048 asection *i = is->section;
4049
4050 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4051 && (i->flags & SEC_EXCLUDE) == 0)
4052 {
4053 unsigned int alignment_needed;
4054 asection *o;
4055
4056 /* Align this section first to the input sections requirement,
4057 then to the output section's requirement. If this alignment
4058 is greater than any seen before, then record it too. Perform
4059 the alignment by inserting a magic 'padding' statement. */
4060
4061 if (output_section_statement->subsection_alignment != -1)
4062 i->alignment_power = output_section_statement->subsection_alignment;
4063
4064 o = output_section_statement->bfd_section;
4065 if (o->alignment_power < i->alignment_power)
4066 o->alignment_power = i->alignment_power;
4067
4068 alignment_needed = align_power (dot, i->alignment_power) - dot;
4069
4070 if (alignment_needed != 0)
4071 {
4072 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4073 dot += alignment_needed;
4074 }
4075
4076 /* Remember where in the output section this input section goes. */
4077
4078 i->output_offset = dot - o->vma;
4079
4080 /* Mark how big the output section must be to contain this now. */
4081 dot += TO_ADDR (i->size);
4082 o->size = TO_SIZE (dot - o->vma);
4083 }
4084 else
4085 {
4086 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4087 }
4088
4089 return dot;
4090 }
4091
4092 static int
4093 sort_sections_by_lma (const void *arg1, const void *arg2)
4094 {
4095 const asection *sec1 = *(const asection **) arg1;
4096 const asection *sec2 = *(const asection **) arg2;
4097
4098 if (bfd_section_lma (sec1->owner, sec1)
4099 < bfd_section_lma (sec2->owner, sec2))
4100 return -1;
4101 else if (bfd_section_lma (sec1->owner, sec1)
4102 > bfd_section_lma (sec2->owner, sec2))
4103 return 1;
4104
4105 return 0;
4106 }
4107
4108 #define IGNORE_SECTION(s) \
4109 ((s->flags & SEC_NEVER_LOAD) != 0 \
4110 || (s->flags & SEC_ALLOC) == 0 \
4111 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4112 && (s->flags & SEC_LOAD) == 0))
4113
4114 /* Check to see if any allocated sections overlap with other allocated
4115 sections. This can happen if a linker script specifies the output
4116 section addresses of the two sections. */
4117
4118 static void
4119 lang_check_section_addresses (void)
4120 {
4121 asection *s, *os;
4122 asection **sections, **spp;
4123 unsigned int count;
4124 bfd_vma s_start;
4125 bfd_vma s_end;
4126 bfd_vma os_start;
4127 bfd_vma os_end;
4128 bfd_size_type amt;
4129
4130 if (bfd_count_sections (output_bfd) <= 1)
4131 return;
4132
4133 amt = bfd_count_sections (output_bfd) * sizeof (asection *);
4134 sections = xmalloc (amt);
4135
4136 /* Scan all sections in the output list. */
4137 count = 0;
4138 for (s = output_bfd->sections; s != NULL; s = s->next)
4139 {
4140 /* Only consider loadable sections with real contents. */
4141 if (IGNORE_SECTION (s) || s->size == 0)
4142 continue;
4143
4144 sections[count] = s;
4145 count++;
4146 }
4147
4148 if (count <= 1)
4149 return;
4150
4151 qsort (sections, (size_t) count, sizeof (asection *),
4152 sort_sections_by_lma);
4153
4154 spp = sections;
4155 s = *spp++;
4156 s_start = bfd_section_lma (output_bfd, s);
4157 s_end = s_start + TO_ADDR (s->size) - 1;
4158 for (count--; count; count--)
4159 {
4160 /* We must check the sections' LMA addresses not their VMA
4161 addresses because overlay sections can have overlapping VMAs
4162 but they must have distinct LMAs. */
4163 os = s;
4164 os_start = s_start;
4165 os_end = s_end;
4166 s = *spp++;
4167 s_start = bfd_section_lma (output_bfd, s);
4168 s_end = s_start + TO_ADDR (s->size) - 1;
4169
4170 /* Look for an overlap. */
4171 if (s_end >= os_start && s_start <= os_end)
4172 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4173 s->name, s_start, s_end, os->name, os_start, os_end);
4174 }
4175
4176 free (sections);
4177 }
4178
4179 /* Make sure the new address is within the region. We explicitly permit the
4180 current address to be at the exact end of the region when the address is
4181 non-zero, in case the region is at the end of addressable memory and the
4182 calculation wraps around. */
4183
4184 static void
4185 os_region_check (lang_output_section_statement_type *os,
4186 lang_memory_region_type *region,
4187 etree_type *tree,
4188 bfd_vma base)
4189 {
4190 if ((region->current < region->origin
4191 || (region->current - region->origin > region->length))
4192 && ((region->current != region->origin + region->length)
4193 || base == 0))
4194 {
4195 if (tree != NULL)
4196 {
4197 einfo (_("%X%P: address 0x%v of %B section %s"
4198 " is not within region %s\n"),
4199 region->current,
4200 os->bfd_section->owner,
4201 os->bfd_section->name,
4202 region->name);
4203 }
4204 else
4205 {
4206 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4207 region->name,
4208 os->bfd_section->owner,
4209 os->bfd_section->name);
4210 }
4211 /* Reset the region pointer. */
4212 region->current = region->origin;
4213 }
4214 }
4215
4216 /* Set the sizes for all the output sections. */
4217
4218 static bfd_vma
4219 lang_size_sections_1
4220 (lang_statement_union_type *s,
4221 lang_output_section_statement_type *output_section_statement,
4222 lang_statement_union_type **prev,
4223 fill_type *fill,
4224 bfd_vma dot,
4225 bfd_boolean *relax,
4226 bfd_boolean check_regions)
4227 {
4228 /* Size up the sections from their constituent parts. */
4229 for (; s != NULL; s = s->header.next)
4230 {
4231 switch (s->header.type)
4232 {
4233 case lang_output_section_statement_enum:
4234 {
4235 bfd_vma newdot, after;
4236 lang_output_section_statement_type *os;
4237
4238 os = &s->output_section_statement;
4239 if (os->addr_tree != NULL)
4240 {
4241 os->processed = FALSE;
4242 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4243
4244 if (!expld.result.valid_p
4245 && expld.phase != lang_mark_phase_enum)
4246 einfo (_("%F%S: non constant or forward reference"
4247 " address expression for section %s\n"),
4248 os->name);
4249
4250 dot = expld.result.value + expld.result.section->vma;
4251 }
4252
4253 if (os->bfd_section == NULL)
4254 /* This section was removed or never actually created. */
4255 break;
4256
4257 /* If this is a COFF shared library section, use the size and
4258 address from the input section. FIXME: This is COFF
4259 specific; it would be cleaner if there were some other way
4260 to do this, but nothing simple comes to mind. */
4261 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
4262 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
4263 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4264 {
4265 asection *input;
4266
4267 if (os->children.head == NULL
4268 || os->children.head->header.next != NULL
4269 || (os->children.head->header.type
4270 != lang_input_section_enum))
4271 einfo (_("%P%X: Internal error on COFF shared library"
4272 " section %s\n"), os->name);
4273
4274 input = os->children.head->input_section.section;
4275 bfd_set_section_vma (os->bfd_section->owner,
4276 os->bfd_section,
4277 bfd_section_vma (input->owner, input));
4278 os->bfd_section->size = input->size;
4279 break;
4280 }
4281
4282 newdot = dot;
4283 if (bfd_is_abs_section (os->bfd_section))
4284 {
4285 /* No matter what happens, an abs section starts at zero. */
4286 ASSERT (os->bfd_section->vma == 0);
4287 }
4288 else
4289 {
4290 int align;
4291
4292 if (os->addr_tree == NULL)
4293 {
4294 /* No address specified for this section, get one
4295 from the region specification. */
4296 if (os->region == NULL
4297 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4298 && os->region->name[0] == '*'
4299 && strcmp (os->region->name,
4300 DEFAULT_MEMORY_REGION) == 0))
4301 {
4302 os->region = lang_memory_default (os->bfd_section);
4303 }
4304
4305 /* If a loadable section is using the default memory
4306 region, and some non default memory regions were
4307 defined, issue an error message. */
4308 if (!os->ignored
4309 && !IGNORE_SECTION (os->bfd_section)
4310 && ! link_info.relocatable
4311 && check_regions
4312 && strcmp (os->region->name,
4313 DEFAULT_MEMORY_REGION) == 0
4314 && lang_memory_region_list != NULL
4315 && (strcmp (lang_memory_region_list->name,
4316 DEFAULT_MEMORY_REGION) != 0
4317 || lang_memory_region_list->next != NULL)
4318 && expld.phase != lang_mark_phase_enum)
4319 {
4320 /* By default this is an error rather than just a
4321 warning because if we allocate the section to the
4322 default memory region we can end up creating an
4323 excessively large binary, or even seg faulting when
4324 attempting to perform a negative seek. See
4325 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4326 for an example of this. This behaviour can be
4327 overridden by the using the --no-check-sections
4328 switch. */
4329 if (command_line.check_section_addresses)
4330 einfo (_("%P%F: error: no memory region specified"
4331 " for loadable section `%s'\n"),
4332 bfd_get_section_name (output_bfd,
4333 os->bfd_section));
4334 else
4335 einfo (_("%P: warning: no memory region specified"
4336 " for loadable section `%s'\n"),
4337 bfd_get_section_name (output_bfd,
4338 os->bfd_section));
4339 }
4340
4341 newdot = os->region->current;
4342 align = os->bfd_section->alignment_power;
4343 }
4344 else
4345 align = os->section_alignment;
4346
4347 /* Align to what the section needs. */
4348 if (align > 0)
4349 {
4350 bfd_vma savedot = newdot;
4351 newdot = align_power (newdot, align);
4352
4353 if (newdot != savedot
4354 && (config.warn_section_align
4355 || os->addr_tree != NULL)
4356 && expld.phase != lang_mark_phase_enum)
4357 einfo (_("%P: warning: changing start of section"
4358 " %s by %lu bytes\n"),
4359 os->name, (unsigned long) (newdot - savedot));
4360 }
4361
4362 bfd_set_section_vma (0, os->bfd_section, newdot);
4363
4364 os->bfd_section->output_offset = 0;
4365 }
4366
4367 lang_size_sections_1 (os->children.head, os, &os->children.head,
4368 os->fill, newdot, relax, check_regions);
4369
4370 os->processed = TRUE;
4371
4372 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4373 {
4374 ASSERT (os->bfd_section->size == 0);
4375 break;
4376 }
4377
4378 dot = os->bfd_section->vma;
4379
4380 /* Put the section within the requested block size, or
4381 align at the block boundary. */
4382 after = ((dot
4383 + TO_ADDR (os->bfd_section->size)
4384 + os->block_value - 1)
4385 & - (bfd_vma) os->block_value);
4386
4387 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4388
4389 /* .tbss sections effectively have zero size. */
4390 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4391 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4392 || link_info.relocatable)
4393 dot += TO_ADDR (os->bfd_section->size);
4394
4395 if (os->update_dot_tree != 0)
4396 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4397
4398 /* Update dot in the region ?
4399 We only do this if the section is going to be allocated,
4400 since unallocated sections do not contribute to the region's
4401 overall size in memory.
4402
4403 If the SEC_NEVER_LOAD bit is not set, it will affect the
4404 addresses of sections after it. We have to update
4405 dot. */
4406 if (os->region != NULL
4407 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4408 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4409 {
4410 os->region->current = dot;
4411
4412 if (check_regions)
4413 /* Make sure the new address is within the region. */
4414 os_region_check (os, os->region, os->addr_tree,
4415 os->bfd_section->vma);
4416
4417 if (os->lma_region != NULL && os->lma_region != os->region)
4418 {
4419 /* Set load_base, which will be handled later. */
4420 os->load_base = exp_intop (os->lma_region->current);
4421 os->lma_region->current +=
4422 TO_ADDR (os->bfd_section->size);
4423
4424 if (check_regions)
4425 os_region_check (os, os->lma_region, NULL,
4426 os->lma_region->current);
4427 }
4428 }
4429 }
4430 break;
4431
4432 case lang_constructors_statement_enum:
4433 dot = lang_size_sections_1 (constructor_list.head,
4434 output_section_statement,
4435 &s->wild_statement.children.head,
4436 fill, dot, relax, check_regions);
4437 break;
4438
4439 case lang_data_statement_enum:
4440 {
4441 unsigned int size = 0;
4442
4443 s->data_statement.output_offset =
4444 dot - output_section_statement->bfd_section->vma;
4445 s->data_statement.output_section =
4446 output_section_statement->bfd_section;
4447
4448 /* We might refer to provided symbols in the expression, and
4449 need to mark them as needed. */
4450 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4451
4452 switch (s->data_statement.type)
4453 {
4454 default:
4455 abort ();
4456 case QUAD:
4457 case SQUAD:
4458 size = QUAD_SIZE;
4459 break;
4460 case LONG:
4461 size = LONG_SIZE;
4462 break;
4463 case SHORT:
4464 size = SHORT_SIZE;
4465 break;
4466 case BYTE:
4467 size = BYTE_SIZE;
4468 break;
4469 }
4470 if (size < TO_SIZE ((unsigned) 1))
4471 size = TO_SIZE ((unsigned) 1);
4472 dot += TO_ADDR (size);
4473 output_section_statement->bfd_section->size += size;
4474 }
4475 break;
4476
4477 case lang_reloc_statement_enum:
4478 {
4479 int size;
4480
4481 s->reloc_statement.output_offset =
4482 dot - output_section_statement->bfd_section->vma;
4483 s->reloc_statement.output_section =
4484 output_section_statement->bfd_section;
4485 size = bfd_get_reloc_size (s->reloc_statement.howto);
4486 dot += TO_ADDR (size);
4487 output_section_statement->bfd_section->size += size;
4488 }
4489 break;
4490
4491 case lang_wild_statement_enum:
4492 dot = lang_size_sections_1 (s->wild_statement.children.head,
4493 output_section_statement,
4494 &s->wild_statement.children.head,
4495 fill, dot, relax, check_regions);
4496 break;
4497
4498 case lang_object_symbols_statement_enum:
4499 link_info.create_object_symbols_section =
4500 output_section_statement->bfd_section;
4501 break;
4502
4503 case lang_output_statement_enum:
4504 case lang_target_statement_enum:
4505 break;
4506
4507 case lang_input_section_enum:
4508 {
4509 asection *i;
4510
4511 i = (*prev)->input_section.section;
4512 if (relax)
4513 {
4514 bfd_boolean again;
4515
4516 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4517 einfo (_("%P%F: can't relax section: %E\n"));
4518 if (again)
4519 *relax = TRUE;
4520 }
4521 dot = size_input_section (prev, output_section_statement,
4522 output_section_statement->fill, dot);
4523 }
4524 break;
4525
4526 case lang_input_statement_enum:
4527 break;
4528
4529 case lang_fill_statement_enum:
4530 s->fill_statement.output_section =
4531 output_section_statement->bfd_section;
4532
4533 fill = s->fill_statement.fill;
4534 break;
4535
4536 case lang_assignment_statement_enum:
4537 {
4538 bfd_vma newdot = dot;
4539
4540 exp_fold_tree (s->assignment_statement.exp,
4541 output_section_statement->bfd_section,
4542 &newdot);
4543
4544 if (newdot != dot && !output_section_statement->ignored)
4545 {
4546 if (output_section_statement == abs_output_section)
4547 {
4548 /* If we don't have an output section, then just adjust
4549 the default memory address. */
4550 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4551 FALSE)->current = newdot;
4552 }
4553 else
4554 {
4555 /* Insert a pad after this statement. We can't
4556 put the pad before when relaxing, in case the
4557 assignment references dot. */
4558 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4559 output_section_statement->bfd_section, dot);
4560
4561 /* Don't neuter the pad below when relaxing. */
4562 s = s->header.next;
4563
4564 /* If dot is advanced, this implies that the section
4565 should have space allocated to it, unless the
4566 user has explicitly stated that the section
4567 should never be loaded. */
4568 if (!(output_section_statement->flags
4569 & (SEC_NEVER_LOAD | SEC_ALLOC)))
4570 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4571 }
4572 dot = newdot;
4573 }
4574 }
4575 break;
4576
4577 case lang_padding_statement_enum:
4578 /* If this is the first time lang_size_sections is called,
4579 we won't have any padding statements. If this is the
4580 second or later passes when relaxing, we should allow
4581 padding to shrink. If padding is needed on this pass, it
4582 will be added back in. */
4583 s->padding_statement.size = 0;
4584
4585 /* Make sure output_offset is valid. If relaxation shrinks
4586 the section and this pad isn't needed, it's possible to
4587 have output_offset larger than the final size of the
4588 section. bfd_set_section_contents will complain even for
4589 a pad size of zero. */
4590 s->padding_statement.output_offset
4591 = dot - output_section_statement->bfd_section->vma;
4592 break;
4593
4594 case lang_group_statement_enum:
4595 dot = lang_size_sections_1 (s->group_statement.children.head,
4596 output_section_statement,
4597 &s->group_statement.children.head,
4598 fill, dot, relax, check_regions);
4599 break;
4600
4601 default:
4602 FAIL ();
4603 break;
4604
4605 /* We can only get here when relaxing is turned on. */
4606 case lang_address_statement_enum:
4607 break;
4608 }
4609 prev = &s->header.next;
4610 }
4611 return dot;
4612 }
4613
4614 void
4615 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
4616 {
4617 lang_statement_iteration++;
4618 lang_size_sections_1 (statement_list.head, abs_output_section,
4619 &statement_list.head, 0, 0, relax, check_regions);
4620 }
4621
4622 void
4623 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4624 {
4625 expld.phase = lang_allocating_phase_enum;
4626 expld.dataseg.phase = exp_dataseg_none;
4627
4628 one_lang_size_sections_pass (relax, check_regions);
4629 if (expld.dataseg.phase == exp_dataseg_end_seen
4630 && link_info.relro && expld.dataseg.relro_end)
4631 {
4632 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4633 to put expld.dataseg.relro on a (common) page boundary. */
4634 bfd_vma old_min_base, relro_end, maxpage;
4635
4636 expld.dataseg.phase = exp_dataseg_relro_adjust;
4637 old_min_base = expld.dataseg.min_base;
4638 maxpage = expld.dataseg.maxpagesize;
4639 expld.dataseg.base += (-expld.dataseg.relro_end
4640 & (expld.dataseg.pagesize - 1));
4641 /* Compute the expected PT_GNU_RELRO segment end. */
4642 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
4643 & ~(expld.dataseg.pagesize - 1);
4644 if (old_min_base + maxpage < expld.dataseg.base)
4645 {
4646 expld.dataseg.base -= maxpage;
4647 relro_end -= maxpage;
4648 }
4649 lang_reset_memory_regions ();
4650 one_lang_size_sections_pass (relax, check_regions);
4651 if (expld.dataseg.relro_end > relro_end)
4652 {
4653 /* The alignment of sections between DATA_SEGMENT_ALIGN
4654 and DATA_SEGMENT_RELRO_END caused huge padding to be
4655 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4656 asection *sec;
4657 unsigned int max_alignment_power = 0;
4658
4659 /* Find maximum alignment power of sections between
4660 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4661 for (sec = output_bfd->sections; sec; sec = sec->next)
4662 if (sec->vma >= expld.dataseg.base
4663 && sec->vma < expld.dataseg.relro_end
4664 && sec->alignment_power > max_alignment_power)
4665 max_alignment_power = sec->alignment_power;
4666
4667 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
4668 {
4669 if (expld.dataseg.base - (1 << max_alignment_power)
4670 < old_min_base)
4671 expld.dataseg.base += expld.dataseg.pagesize;
4672 expld.dataseg.base -= (1 << max_alignment_power);
4673 lang_reset_memory_regions ();
4674 one_lang_size_sections_pass (relax, check_regions);
4675 }
4676 }
4677 link_info.relro_start = expld.dataseg.base;
4678 link_info.relro_end = expld.dataseg.relro_end;
4679 }
4680 else if (expld.dataseg.phase == exp_dataseg_end_seen)
4681 {
4682 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4683 a page could be saved in the data segment. */
4684 bfd_vma first, last;
4685
4686 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
4687 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
4688 if (first && last
4689 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
4690 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
4691 && first + last <= expld.dataseg.pagesize)
4692 {
4693 expld.dataseg.phase = exp_dataseg_adjust;
4694 lang_reset_memory_regions ();
4695 one_lang_size_sections_pass (relax, check_regions);
4696 }
4697 }
4698
4699 expld.phase = lang_final_phase_enum;
4700 }
4701
4702 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4703
4704 static bfd_vma
4705 lang_do_assignments_1 (lang_statement_union_type *s,
4706 lang_output_section_statement_type *current_os,
4707 fill_type *fill,
4708 bfd_vma dot)
4709 {
4710 for (; s != NULL; s = s->header.next)
4711 {
4712 switch (s->header.type)
4713 {
4714 case lang_constructors_statement_enum:
4715 dot = lang_do_assignments_1 (constructor_list.head,
4716 current_os, fill, dot);
4717 break;
4718
4719 case lang_output_section_statement_enum:
4720 {
4721 lang_output_section_statement_type *os;
4722
4723 os = &(s->output_section_statement);
4724 if (os->bfd_section != NULL && !os->ignored)
4725 {
4726 lang_memory_region_type *r;
4727
4728 dot = os->bfd_section->vma;
4729 r = os->region;
4730 if (r == NULL)
4731 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4732
4733 if (os->load_base)
4734 os->bfd_section->lma
4735 = exp_get_abs_int (os->load_base, 0, "load base");
4736 else if (r->last_os != NULL)
4737 {
4738 asection *last;
4739 bfd_vma lma;
4740
4741 last = r->last_os->output_section_statement.bfd_section;
4742
4743 /* If the current vma overlaps the previous section,
4744 then set the current lma to that at the end of
4745 the previous section. The previous section was
4746 probably an overlay. */
4747 if ((dot >= last->vma
4748 && dot < last->vma + last->size)
4749 || (last->vma >= dot
4750 && last->vma < dot + os->bfd_section->size))
4751 lma = last->lma + last->size;
4752
4753 /* Otherwise, keep the same lma to vma relationship
4754 as the previous section. */
4755 else
4756 lma = dot + last->lma - last->vma;
4757
4758 if (os->section_alignment != -1)
4759 lma = align_power (lma, os->section_alignment);
4760 os->bfd_section->lma = lma;
4761 }
4762
4763 lang_do_assignments_1 (os->children.head,
4764 os, os->fill, dot);
4765
4766 /* .tbss sections effectively have zero size. */
4767 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4768 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4769 || link_info.relocatable)
4770 {
4771 dot += TO_ADDR (os->bfd_section->size);
4772
4773 /* Keep track of normal sections using the default
4774 lma region. We use this to set the lma for
4775 following sections. Overlays or other linker
4776 script assignment to lma might mean that the
4777 default lma == vma is incorrect. */
4778 if (!link_info.relocatable
4779 && os->lma_region == NULL)
4780 r->last_os = s;
4781 }
4782 }
4783 }
4784 break;
4785
4786 case lang_wild_statement_enum:
4787
4788 dot = lang_do_assignments_1 (s->wild_statement.children.head,
4789 current_os, fill, dot);
4790 break;
4791
4792 case lang_object_symbols_statement_enum:
4793 case lang_output_statement_enum:
4794 case lang_target_statement_enum:
4795 break;
4796
4797 case lang_data_statement_enum:
4798 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4799 if (expld.result.valid_p)
4800 s->data_statement.value = (expld.result.value
4801 + expld.result.section->vma);
4802 else
4803 einfo (_("%F%P: invalid data statement\n"));
4804 {
4805 unsigned int size;
4806 switch (s->data_statement.type)
4807 {
4808 default:
4809 abort ();
4810 case QUAD:
4811 case SQUAD:
4812 size = QUAD_SIZE;
4813 break;
4814 case LONG:
4815 size = LONG_SIZE;
4816 break;
4817 case SHORT:
4818 size = SHORT_SIZE;
4819 break;
4820 case BYTE:
4821 size = BYTE_SIZE;
4822 break;
4823 }
4824 if (size < TO_SIZE ((unsigned) 1))
4825 size = TO_SIZE ((unsigned) 1);
4826 dot += TO_ADDR (size);
4827 }
4828 break;
4829
4830 case lang_reloc_statement_enum:
4831 exp_fold_tree (s->reloc_statement.addend_exp,
4832 bfd_abs_section_ptr, &dot);
4833 if (expld.result.valid_p)
4834 s->reloc_statement.addend_value = expld.result.value;
4835 else
4836 einfo (_("%F%P: invalid reloc statement\n"));
4837 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4838 break;
4839
4840 case lang_input_section_enum:
4841 {
4842 asection *in = s->input_section.section;
4843
4844 if ((in->flags & SEC_EXCLUDE) == 0)
4845 dot += TO_ADDR (in->size);
4846 }
4847 break;
4848
4849 case lang_input_statement_enum:
4850 break;
4851
4852 case lang_fill_statement_enum:
4853 fill = s->fill_statement.fill;
4854 break;
4855
4856 case lang_assignment_statement_enum:
4857 exp_fold_tree (s->assignment_statement.exp,
4858 current_os->bfd_section,
4859 &dot);
4860 break;
4861
4862 case lang_padding_statement_enum:
4863 dot += TO_ADDR (s->padding_statement.size);
4864 break;
4865
4866 case lang_group_statement_enum:
4867 dot = lang_do_assignments_1 (s->group_statement.children.head,
4868 current_os, fill, dot);
4869 break;
4870
4871 default:
4872 FAIL ();
4873 break;
4874
4875 case lang_address_statement_enum:
4876 break;
4877 }
4878 }
4879 return dot;
4880 }
4881
4882 void
4883 lang_do_assignments (void)
4884 {
4885 lang_statement_iteration++;
4886 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
4887 }
4888
4889 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4890 operator .startof. (section_name), it produces an undefined symbol
4891 .startof.section_name. Similarly, when it sees
4892 .sizeof. (section_name), it produces an undefined symbol
4893 .sizeof.section_name. For all the output sections, we look for
4894 such symbols, and set them to the correct value. */
4895
4896 static void
4897 lang_set_startof (void)
4898 {
4899 asection *s;
4900
4901 if (link_info.relocatable)
4902 return;
4903
4904 for (s = output_bfd->sections; s != NULL; s = s->next)
4905 {
4906 const char *secname;
4907 char *buf;
4908 struct bfd_link_hash_entry *h;
4909
4910 secname = bfd_get_section_name (output_bfd, s);
4911 buf = xmalloc (10 + strlen (secname));
4912
4913 sprintf (buf, ".startof.%s", secname);
4914 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4915 if (h != NULL && h->type == bfd_link_hash_undefined)
4916 {
4917 h->type = bfd_link_hash_defined;
4918 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4919 h->u.def.section = bfd_abs_section_ptr;
4920 }
4921
4922 sprintf (buf, ".sizeof.%s", secname);
4923 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4924 if (h != NULL && h->type == bfd_link_hash_undefined)
4925 {
4926 h->type = bfd_link_hash_defined;
4927 h->u.def.value = TO_ADDR (s->size);
4928 h->u.def.section = bfd_abs_section_ptr;
4929 }
4930
4931 free (buf);
4932 }
4933 }
4934
4935 static void
4936 lang_end (void)
4937 {
4938 struct bfd_link_hash_entry *h;
4939 bfd_boolean warn;
4940
4941 if (link_info.relocatable || link_info.shared)
4942 warn = FALSE;
4943 else
4944 warn = TRUE;
4945
4946 if (entry_symbol.name == NULL)
4947 {
4948 /* No entry has been specified. Look for the default entry, but
4949 don't warn if we don't find it. */
4950 entry_symbol.name = entry_symbol_default;
4951 warn = FALSE;
4952 }
4953
4954 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4955 FALSE, FALSE, TRUE);
4956 if (h != NULL
4957 && (h->type == bfd_link_hash_defined
4958 || h->type == bfd_link_hash_defweak)
4959 && h->u.def.section->output_section != NULL)
4960 {
4961 bfd_vma val;
4962
4963 val = (h->u.def.value
4964 + bfd_get_section_vma (output_bfd,
4965 h->u.def.section->output_section)
4966 + h->u.def.section->output_offset);
4967 if (! bfd_set_start_address (output_bfd, val))
4968 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4969 }
4970 else
4971 {
4972 bfd_vma val;
4973 const char *send;
4974
4975 /* We couldn't find the entry symbol. Try parsing it as a
4976 number. */
4977 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4978 if (*send == '\0')
4979 {
4980 if (! bfd_set_start_address (output_bfd, val))
4981 einfo (_("%P%F: can't set start address\n"));
4982 }
4983 else
4984 {
4985 asection *ts;
4986
4987 /* Can't find the entry symbol, and it's not a number. Use
4988 the first address in the text section. */
4989 ts = bfd_get_section_by_name (output_bfd, entry_section);
4990 if (ts != NULL)
4991 {
4992 if (warn)
4993 einfo (_("%P: warning: cannot find entry symbol %s;"
4994 " defaulting to %V\n"),
4995 entry_symbol.name,
4996 bfd_get_section_vma (output_bfd, ts));
4997 if (! bfd_set_start_address (output_bfd,
4998 bfd_get_section_vma (output_bfd,
4999 ts)))
5000 einfo (_("%P%F: can't set start address\n"));
5001 }
5002 else
5003 {
5004 if (warn)
5005 einfo (_("%P: warning: cannot find entry symbol %s;"
5006 " not setting start address\n"),
5007 entry_symbol.name);
5008 }
5009 }
5010 }
5011
5012 /* Don't bfd_hash_table_free (&lang_definedness_table);
5013 map file output may result in a call of lang_track_definedness. */
5014 }
5015
5016 /* This is a small function used when we want to ignore errors from
5017 BFD. */
5018
5019 static void
5020 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5021 {
5022 /* Don't do anything. */
5023 }
5024
5025 /* Check that the architecture of all the input files is compatible
5026 with the output file. Also call the backend to let it do any
5027 other checking that is needed. */
5028
5029 static void
5030 lang_check (void)
5031 {
5032 lang_statement_union_type *file;
5033 bfd *input_bfd;
5034 const bfd_arch_info_type *compatible;
5035
5036 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5037 {
5038 input_bfd = file->input_statement.the_bfd;
5039 compatible
5040 = bfd_arch_get_compatible (input_bfd, output_bfd,
5041 command_line.accept_unknown_input_arch);
5042
5043 /* In general it is not possible to perform a relocatable
5044 link between differing object formats when the input
5045 file has relocations, because the relocations in the
5046 input format may not have equivalent representations in
5047 the output format (and besides BFD does not translate
5048 relocs for other link purposes than a final link). */
5049 if ((link_info.relocatable || link_info.emitrelocations)
5050 && (compatible == NULL
5051 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
5052 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5053 {
5054 einfo (_("%P%F: Relocatable linking with relocations from"
5055 " format %s (%B) to format %s (%B) is not supported\n"),
5056 bfd_get_target (input_bfd), input_bfd,
5057 bfd_get_target (output_bfd), output_bfd);
5058 /* einfo with %F exits. */
5059 }
5060
5061 if (compatible == NULL)
5062 {
5063 if (command_line.warn_mismatch)
5064 einfo (_("%P: warning: %s architecture of input file `%B'"
5065 " is incompatible with %s output\n"),
5066 bfd_printable_name (input_bfd), input_bfd,
5067 bfd_printable_name (output_bfd));
5068 }
5069 else if (bfd_count_sections (input_bfd))
5070 {
5071 /* If the input bfd has no contents, it shouldn't set the
5072 private data of the output bfd. */
5073
5074 bfd_error_handler_type pfn = NULL;
5075
5076 /* If we aren't supposed to warn about mismatched input
5077 files, temporarily set the BFD error handler to a
5078 function which will do nothing. We still want to call
5079 bfd_merge_private_bfd_data, since it may set up
5080 information which is needed in the output file. */
5081 if (! command_line.warn_mismatch)
5082 pfn = bfd_set_error_handler (ignore_bfd_errors);
5083 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
5084 {
5085 if (command_line.warn_mismatch)
5086 einfo (_("%P%X: failed to merge target specific data"
5087 " of file %B\n"), input_bfd);
5088 }
5089 if (! command_line.warn_mismatch)
5090 bfd_set_error_handler (pfn);
5091 }
5092 }
5093 }
5094
5095 /* Look through all the global common symbols and attach them to the
5096 correct section. The -sort-common command line switch may be used
5097 to roughly sort the entries by size. */
5098
5099 static void
5100 lang_common (void)
5101 {
5102 if (command_line.inhibit_common_definition)
5103 return;
5104 if (link_info.relocatable
5105 && ! command_line.force_common_definition)
5106 return;
5107
5108 if (! config.sort_common)
5109 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5110 else
5111 {
5112 int power;
5113
5114 for (power = 4; power >= 0; power--)
5115 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5116 }
5117 }
5118
5119 /* Place one common symbol in the correct section. */
5120
5121 static bfd_boolean
5122 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5123 {
5124 unsigned int power_of_two;
5125 bfd_vma size;
5126 asection *section;
5127
5128 if (h->type != bfd_link_hash_common)
5129 return TRUE;
5130
5131 size = h->u.c.size;
5132 power_of_two = h->u.c.p->alignment_power;
5133
5134 if (config.sort_common
5135 && power_of_two < (unsigned int) *(int *) info)
5136 return TRUE;
5137
5138 section = h->u.c.p->section;
5139
5140 /* Increase the size of the section to align the common sym. */
5141 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5142 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
5143
5144 /* Adjust the alignment if necessary. */
5145 if (power_of_two > section->alignment_power)
5146 section->alignment_power = power_of_two;
5147
5148 /* Change the symbol from common to defined. */
5149 h->type = bfd_link_hash_defined;
5150 h->u.def.section = section;
5151 h->u.def.value = section->size;
5152
5153 /* Increase the size of the section. */
5154 section->size += size;
5155
5156 /* Make sure the section is allocated in memory, and make sure that
5157 it is no longer a common section. */
5158 section->flags |= SEC_ALLOC;
5159 section->flags &= ~SEC_IS_COMMON;
5160
5161 if (config.map_file != NULL)
5162 {
5163 static bfd_boolean header_printed;
5164 int len;
5165 char *name;
5166 char buf[50];
5167
5168 if (! header_printed)
5169 {
5170 minfo (_("\nAllocating common symbols\n"));
5171 minfo (_("Common symbol size file\n\n"));
5172 header_printed = TRUE;
5173 }
5174
5175 name = demangle (h->root.string);
5176 minfo ("%s", name);
5177 len = strlen (name);
5178 free (name);
5179
5180 if (len >= 19)
5181 {
5182 print_nl ();
5183 len = 0;
5184 }
5185 while (len < 20)
5186 {
5187 print_space ();
5188 ++len;
5189 }
5190
5191 minfo ("0x");
5192 if (size <= 0xffffffff)
5193 sprintf (buf, "%lx", (unsigned long) size);
5194 else
5195 sprintf_vma (buf, size);
5196 minfo ("%s", buf);
5197 len = strlen (buf);
5198
5199 while (len < 16)
5200 {
5201 print_space ();
5202 ++len;
5203 }
5204
5205 minfo ("%B\n", section->owner);
5206 }
5207
5208 return TRUE;
5209 }
5210
5211 /* Run through the input files and ensure that every input section has
5212 somewhere to go. If one is found without a destination then create
5213 an input request and place it into the statement tree. */
5214
5215 static void
5216 lang_place_orphans (void)
5217 {
5218 LANG_FOR_EACH_INPUT_STATEMENT (file)
5219 {
5220 asection *s;
5221
5222 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5223 {
5224 if (s->output_section == NULL)
5225 {
5226 /* This section of the file is not attached, root
5227 around for a sensible place for it to go. */
5228
5229 if (file->just_syms_flag)
5230 bfd_link_just_syms (file->the_bfd, s, &link_info);
5231 else if ((s->flags & SEC_EXCLUDE) != 0)
5232 s->output_section = bfd_abs_section_ptr;
5233 else if (strcmp (s->name, "COMMON") == 0)
5234 {
5235 /* This is a lonely common section which must have
5236 come from an archive. We attach to the section
5237 with the wildcard. */
5238 if (! link_info.relocatable
5239 || command_line.force_common_definition)
5240 {
5241 if (default_common_section == NULL)
5242 {
5243 default_common_section =
5244 lang_output_section_statement_lookup (".bss");
5245
5246 }
5247 lang_add_section (&default_common_section->children, s,
5248 default_common_section);
5249 }
5250 }
5251 else if (ldemul_place_orphan (s))
5252 ;
5253 else
5254 {
5255 lang_output_section_statement_type *os;
5256
5257 os = lang_output_section_statement_lookup (s->name);
5258 lang_add_section (&os->children, s, os);
5259 }
5260 }
5261 }
5262 }
5263 }
5264
5265 void
5266 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5267 {
5268 flagword *ptr_flags;
5269
5270 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5271 while (*flags)
5272 {
5273 switch (*flags)
5274 {
5275 case 'A': case 'a':
5276 *ptr_flags |= SEC_ALLOC;
5277 break;
5278
5279 case 'R': case 'r':
5280 *ptr_flags |= SEC_READONLY;
5281 break;
5282
5283 case 'W': case 'w':
5284 *ptr_flags |= SEC_DATA;
5285 break;
5286
5287 case 'X': case 'x':
5288 *ptr_flags |= SEC_CODE;
5289 break;
5290
5291 case 'L': case 'l':
5292 case 'I': case 'i':
5293 *ptr_flags |= SEC_LOAD;
5294 break;
5295
5296 default:
5297 einfo (_("%P%F: invalid syntax in flags\n"));
5298 break;
5299 }
5300 flags++;
5301 }
5302 }
5303
5304 /* Call a function on each input file. This function will be called
5305 on an archive, but not on the elements. */
5306
5307 void
5308 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5309 {
5310 lang_input_statement_type *f;
5311
5312 for (f = (lang_input_statement_type *) input_file_chain.head;
5313 f != NULL;
5314 f = (lang_input_statement_type *) f->next_real_file)
5315 func (f);
5316 }
5317
5318 /* Call a function on each file. The function will be called on all
5319 the elements of an archive which are included in the link, but will
5320 not be called on the archive file itself. */
5321
5322 void
5323 lang_for_each_file (void (*func) (lang_input_statement_type *))
5324 {
5325 LANG_FOR_EACH_INPUT_STATEMENT (f)
5326 {
5327 func (f);
5328 }
5329 }
5330
5331 void
5332 ldlang_add_file (lang_input_statement_type *entry)
5333 {
5334 bfd **pp;
5335
5336 lang_statement_append (&file_chain,
5337 (lang_statement_union_type *) entry,
5338 &entry->next);
5339
5340 /* The BFD linker needs to have a list of all input BFDs involved in
5341 a link. */
5342 ASSERT (entry->the_bfd->link_next == NULL);
5343 ASSERT (entry->the_bfd != output_bfd);
5344 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
5345 ;
5346 *pp = entry->the_bfd;
5347 entry->the_bfd->usrdata = entry;
5348 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5349
5350 /* Look through the sections and check for any which should not be
5351 included in the link. We need to do this now, so that we can
5352 notice when the backend linker tries to report multiple
5353 definition errors for symbols which are in sections we aren't
5354 going to link. FIXME: It might be better to entirely ignore
5355 symbols which are defined in sections which are going to be
5356 discarded. This would require modifying the backend linker for
5357 each backend which might set the SEC_LINK_ONCE flag. If we do
5358 this, we should probably handle SEC_EXCLUDE in the same way. */
5359
5360 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5361 }
5362
5363 void
5364 lang_add_output (const char *name, int from_script)
5365 {
5366 /* Make -o on command line override OUTPUT in script. */
5367 if (!had_output_filename || !from_script)
5368 {
5369 output_filename = name;
5370 had_output_filename = TRUE;
5371 }
5372 }
5373
5374 static lang_output_section_statement_type *current_section;
5375
5376 static int
5377 topower (int x)
5378 {
5379 unsigned int i = 1;
5380 int l;
5381
5382 if (x < 0)
5383 return -1;
5384
5385 for (l = 0; l < 32; l++)
5386 {
5387 if (i >= (unsigned int) x)
5388 return l;
5389 i <<= 1;
5390 }
5391
5392 return 0;
5393 }
5394
5395 lang_output_section_statement_type *
5396 lang_enter_output_section_statement (const char *output_section_statement_name,
5397 etree_type *address_exp,
5398 enum section_type sectype,
5399 etree_type *align,
5400 etree_type *subalign,
5401 etree_type *ebase,
5402 int constraint)
5403 {
5404 lang_output_section_statement_type *os;
5405
5406 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5407 constraint);
5408 current_section = os;
5409
5410 /* Make next things chain into subchain of this. */
5411
5412 if (os->addr_tree == NULL)
5413 {
5414 os->addr_tree = address_exp;
5415 }
5416 os->sectype = sectype;
5417 if (sectype != noload_section)
5418 os->flags = SEC_NO_FLAGS;
5419 else
5420 os->flags = SEC_NEVER_LOAD;
5421 os->block_value = 1;
5422 stat_ptr = &os->children;
5423
5424 os->subsection_alignment =
5425 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5426 os->section_alignment =
5427 topower (exp_get_value_int (align, -1, "section alignment"));
5428
5429 os->load_base = ebase;
5430 return os;
5431 }
5432
5433 void
5434 lang_final (void)
5435 {
5436 lang_output_statement_type *new;
5437
5438 new = new_stat (lang_output_statement, stat_ptr);
5439 new->name = output_filename;
5440 }
5441
5442 /* Reset the current counters in the regions. */
5443
5444 void
5445 lang_reset_memory_regions (void)
5446 {
5447 lang_memory_region_type *p = lang_memory_region_list;
5448 asection *o;
5449 lang_output_section_statement_type *os;
5450
5451 for (p = lang_memory_region_list; p != NULL; p = p->next)
5452 {
5453 p->current = p->origin;
5454 p->last_os = NULL;
5455 }
5456
5457 for (os = &lang_output_section_statement.head->output_section_statement;
5458 os != NULL;
5459 os = os->next)
5460 os->processed = FALSE;
5461
5462 for (o = output_bfd->sections; o != NULL; o = o->next)
5463 {
5464 /* Save the last size for possible use by bfd_relax_section. */
5465 o->rawsize = o->size;
5466 o->size = 0;
5467 }
5468 }
5469
5470 /* Worker for lang_gc_sections_1. */
5471
5472 static void
5473 gc_section_callback (lang_wild_statement_type *ptr,
5474 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5475 asection *section,
5476 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5477 void *data ATTRIBUTE_UNUSED)
5478 {
5479 /* If the wild pattern was marked KEEP, the member sections
5480 should be as well. */
5481 if (ptr->keep_sections)
5482 section->flags |= SEC_KEEP;
5483 }
5484
5485 /* Iterate over sections marking them against GC. */
5486
5487 static void
5488 lang_gc_sections_1 (lang_statement_union_type *s)
5489 {
5490 for (; s != NULL; s = s->header.next)
5491 {
5492 switch (s->header.type)
5493 {
5494 case lang_wild_statement_enum:
5495 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5496 break;
5497 case lang_constructors_statement_enum:
5498 lang_gc_sections_1 (constructor_list.head);
5499 break;
5500 case lang_output_section_statement_enum:
5501 lang_gc_sections_1 (s->output_section_statement.children.head);
5502 break;
5503 case lang_group_statement_enum:
5504 lang_gc_sections_1 (s->group_statement.children.head);
5505 break;
5506 default:
5507 break;
5508 }
5509 }
5510 }
5511
5512 static void
5513 lang_gc_sections (void)
5514 {
5515 struct bfd_link_hash_entry *h;
5516 ldlang_undef_chain_list_type *ulist;
5517
5518 /* Keep all sections so marked in the link script. */
5519
5520 lang_gc_sections_1 (statement_list.head);
5521
5522 /* Keep all sections containing symbols undefined on the command-line,
5523 and the section containing the entry symbol. */
5524
5525 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
5526 {
5527 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
5528 FALSE, FALSE, FALSE);
5529
5530 if (h != NULL
5531 && (h->type == bfd_link_hash_defined
5532 || h->type == bfd_link_hash_defweak)
5533 && ! bfd_is_abs_section (h->u.def.section))
5534 {
5535 h->u.def.section->flags |= SEC_KEEP;
5536 }
5537 }
5538
5539 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5540 the special case of debug info. (See bfd/stabs.c)
5541 Twiddle the flag here, to simplify later linker code. */
5542 if (link_info.relocatable)
5543 {
5544 LANG_FOR_EACH_INPUT_STATEMENT (f)
5545 {
5546 asection *sec;
5547 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5548 if ((sec->flags & SEC_DEBUGGING) == 0)
5549 sec->flags &= ~SEC_EXCLUDE;
5550 }
5551 }
5552
5553 if (link_info.gc_sections)
5554 bfd_gc_sections (output_bfd, &link_info);
5555 }
5556
5557 /* Relax all sections until bfd_relax_section gives up. */
5558
5559 static void
5560 relax_sections (void)
5561 {
5562 /* Keep relaxing until bfd_relax_section gives up. */
5563 bfd_boolean relax_again;
5564
5565 do
5566 {
5567 relax_again = FALSE;
5568
5569 /* Note: pe-dll.c does something like this also. If you find
5570 you need to change this code, you probably need to change
5571 pe-dll.c also. DJ */
5572
5573 /* Do all the assignments with our current guesses as to
5574 section sizes. */
5575 lang_do_assignments ();
5576
5577 /* We must do this after lang_do_assignments, because it uses
5578 size. */
5579 lang_reset_memory_regions ();
5580
5581 /* Perform another relax pass - this time we know where the
5582 globals are, so can make a better guess. */
5583 lang_size_sections (&relax_again, FALSE);
5584 }
5585 while (relax_again);
5586 }
5587
5588 void
5589 lang_process (void)
5590 {
5591 current_target = default_target;
5592
5593 /* Open the output file. */
5594 lang_for_each_statement (ldlang_open_output);
5595 init_opb ();
5596
5597 ldemul_create_output_section_statements ();
5598
5599 /* Add to the hash table all undefineds on the command line. */
5600 lang_place_undefineds ();
5601
5602 if (!bfd_section_already_linked_table_init ())
5603 einfo (_("%P%F: Failed to create hash table\n"));
5604
5605 /* Create a bfd for each input file. */
5606 current_target = default_target;
5607 open_input_bfds (statement_list.head, FALSE);
5608
5609 link_info.gc_sym_list = &entry_symbol;
5610 if (entry_symbol.name == NULL)
5611 link_info.gc_sym_list = ldlang_undef_chain_list_head;
5612
5613 ldemul_after_open ();
5614
5615 bfd_section_already_linked_table_free ();
5616
5617 /* Make sure that we're not mixing architectures. We call this
5618 after all the input files have been opened, but before we do any
5619 other processing, so that any operations merge_private_bfd_data
5620 does on the output file will be known during the rest of the
5621 link. */
5622 lang_check ();
5623
5624 /* Handle .exports instead of a version script if we're told to do so. */
5625 if (command_line.version_exports_section)
5626 lang_do_version_exports_section ();
5627
5628 /* Build all sets based on the information gathered from the input
5629 files. */
5630 ldctor_build_sets ();
5631
5632 /* Remove unreferenced sections if asked to. */
5633 lang_gc_sections ();
5634
5635 /* Size up the common data. */
5636 lang_common ();
5637
5638 /* Update wild statements. */
5639 update_wild_statements (statement_list.head);
5640
5641 /* Run through the contours of the script and attach input sections
5642 to the correct output sections. */
5643 map_input_to_output_sections (statement_list.head, NULL, NULL);
5644
5645 /* Find any sections not attached explicitly and handle them. */
5646 lang_place_orphans ();
5647
5648 if (! link_info.relocatable)
5649 {
5650 asection *found;
5651
5652 /* Merge SEC_MERGE sections. This has to be done after GC of
5653 sections, so that GCed sections are not merged, but before
5654 assigning dynamic symbols, since removing whole input sections
5655 is hard then. */
5656 bfd_merge_sections (output_bfd, &link_info);
5657
5658 /* Look for a text section and set the readonly attribute in it. */
5659 found = bfd_get_section_by_name (output_bfd, ".text");
5660
5661 if (found != NULL)
5662 {
5663 if (config.text_read_only)
5664 found->flags |= SEC_READONLY;
5665 else
5666 found->flags &= ~SEC_READONLY;
5667 }
5668 }
5669
5670 /* Do anything special before sizing sections. This is where ELF
5671 and other back-ends size dynamic sections. */
5672 ldemul_before_allocation ();
5673
5674 /* We must record the program headers before we try to fix the
5675 section positions, since they will affect SIZEOF_HEADERS. */
5676 lang_record_phdrs ();
5677
5678 /* Size up the sections. */
5679 lang_size_sections (NULL, !command_line.relax);
5680
5681 /* Now run around and relax if we can. */
5682 if (command_line.relax)
5683 {
5684 /* We may need more than one relaxation pass. */
5685 int i = link_info.relax_pass;
5686
5687 /* The backend can use it to determine the current pass. */
5688 link_info.relax_pass = 0;
5689
5690 while (i--)
5691 {
5692 relax_sections ();
5693 link_info.relax_pass++;
5694 }
5695
5696 /* Final extra sizing to report errors. */
5697 lang_do_assignments ();
5698 lang_reset_memory_regions ();
5699 lang_size_sections (NULL, TRUE);
5700 }
5701
5702 /* See if anything special should be done now we know how big
5703 everything is. */
5704 ldemul_after_allocation ();
5705
5706 /* Fix any .startof. or .sizeof. symbols. */
5707 lang_set_startof ();
5708
5709 /* Do all the assignments, now that we know the final resting places
5710 of all the symbols. */
5711
5712 lang_do_assignments ();
5713
5714 ldemul_finish ();
5715
5716 /* Make sure that the section addresses make sense. */
5717 if (! link_info.relocatable
5718 && command_line.check_section_addresses)
5719 lang_check_section_addresses ();
5720
5721 lang_end ();
5722 }
5723
5724 /* EXPORTED TO YACC */
5725
5726 void
5727 lang_add_wild (struct wildcard_spec *filespec,
5728 struct wildcard_list *section_list,
5729 bfd_boolean keep_sections)
5730 {
5731 struct wildcard_list *curr, *next;
5732 lang_wild_statement_type *new;
5733
5734 /* Reverse the list as the parser puts it back to front. */
5735 for (curr = section_list, section_list = NULL;
5736 curr != NULL;
5737 section_list = curr, curr = next)
5738 {
5739 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
5740 placed_commons = TRUE;
5741
5742 next = curr->next;
5743 curr->next = section_list;
5744 }
5745
5746 if (filespec != NULL && filespec->name != NULL)
5747 {
5748 if (strcmp (filespec->name, "*") == 0)
5749 filespec->name = NULL;
5750 else if (! wildcardp (filespec->name))
5751 lang_has_input_file = TRUE;
5752 }
5753
5754 new = new_stat (lang_wild_statement, stat_ptr);
5755 new->filename = NULL;
5756 new->filenames_sorted = FALSE;
5757 if (filespec != NULL)
5758 {
5759 new->filename = filespec->name;
5760 new->filenames_sorted = filespec->sorted == by_name;
5761 }
5762 new->section_list = section_list;
5763 new->keep_sections = keep_sections;
5764 lang_list_init (&new->children);
5765 analyze_walk_wild_section_handler (new);
5766 }
5767
5768 void
5769 lang_section_start (const char *name, etree_type *address,
5770 const segment_type *segment)
5771 {
5772 lang_address_statement_type *ad;
5773
5774 ad = new_stat (lang_address_statement, stat_ptr);
5775 ad->section_name = name;
5776 ad->address = address;
5777 ad->segment = segment;
5778 }
5779
5780 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5781 because of a -e argument on the command line, or zero if this is
5782 called by ENTRY in a linker script. Command line arguments take
5783 precedence. */
5784
5785 void
5786 lang_add_entry (const char *name, bfd_boolean cmdline)
5787 {
5788 if (entry_symbol.name == NULL
5789 || cmdline
5790 || ! entry_from_cmdline)
5791 {
5792 entry_symbol.name = name;
5793 entry_from_cmdline = cmdline;
5794 }
5795 }
5796
5797 /* Set the default start symbol to NAME. .em files should use this,
5798 not lang_add_entry, to override the use of "start" if neither the
5799 linker script nor the command line specifies an entry point. NAME
5800 must be permanently allocated. */
5801 void
5802 lang_default_entry (const char *name)
5803 {
5804 entry_symbol_default = name;
5805 }
5806
5807 void
5808 lang_add_target (const char *name)
5809 {
5810 lang_target_statement_type *new;
5811
5812 new = new_stat (lang_target_statement, stat_ptr);
5813 new->target = name;
5814 }
5815
5816 void
5817 lang_add_map (const char *name)
5818 {
5819 while (*name)
5820 {
5821 switch (*name)
5822 {
5823 case 'F':
5824 map_option_f = TRUE;
5825 break;
5826 }
5827 name++;
5828 }
5829 }
5830
5831 void
5832 lang_add_fill (fill_type *fill)
5833 {
5834 lang_fill_statement_type *new;
5835
5836 new = new_stat (lang_fill_statement, stat_ptr);
5837 new->fill = fill;
5838 }
5839
5840 void
5841 lang_add_data (int type, union etree_union *exp)
5842 {
5843 lang_data_statement_type *new;
5844
5845 new = new_stat (lang_data_statement, stat_ptr);
5846 new->exp = exp;
5847 new->type = type;
5848 }
5849
5850 /* Create a new reloc statement. RELOC is the BFD relocation type to
5851 generate. HOWTO is the corresponding howto structure (we could
5852 look this up, but the caller has already done so). SECTION is the
5853 section to generate a reloc against, or NAME is the name of the
5854 symbol to generate a reloc against. Exactly one of SECTION and
5855 NAME must be NULL. ADDEND is an expression for the addend. */
5856
5857 void
5858 lang_add_reloc (bfd_reloc_code_real_type reloc,
5859 reloc_howto_type *howto,
5860 asection *section,
5861 const char *name,
5862 union etree_union *addend)
5863 {
5864 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5865
5866 p->reloc = reloc;
5867 p->howto = howto;
5868 p->section = section;
5869 p->name = name;
5870 p->addend_exp = addend;
5871
5872 p->addend_value = 0;
5873 p->output_section = NULL;
5874 p->output_offset = 0;
5875 }
5876
5877 lang_assignment_statement_type *
5878 lang_add_assignment (etree_type *exp)
5879 {
5880 lang_assignment_statement_type *new;
5881
5882 new = new_stat (lang_assignment_statement, stat_ptr);
5883 new->exp = exp;
5884 return new;
5885 }
5886
5887 void
5888 lang_add_attribute (enum statement_enum attribute)
5889 {
5890 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
5891 }
5892
5893 void
5894 lang_startup (const char *name)
5895 {
5896 if (startup_file != NULL)
5897 {
5898 einfo (_("%P%F: multiple STARTUP files\n"));
5899 }
5900 first_file->filename = name;
5901 first_file->local_sym_name = name;
5902 first_file->real = TRUE;
5903
5904 startup_file = name;
5905 }
5906
5907 void
5908 lang_float (bfd_boolean maybe)
5909 {
5910 lang_float_flag = maybe;
5911 }
5912
5913
5914 /* Work out the load- and run-time regions from a script statement, and
5915 store them in *LMA_REGION and *REGION respectively.
5916
5917 MEMSPEC is the name of the run-time region, or the value of
5918 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5919 LMA_MEMSPEC is the name of the load-time region, or null if the
5920 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5921 had an explicit load address.
5922
5923 It is an error to specify both a load region and a load address. */
5924
5925 static void
5926 lang_get_regions (lang_memory_region_type **region,
5927 lang_memory_region_type **lma_region,
5928 const char *memspec,
5929 const char *lma_memspec,
5930 bfd_boolean have_lma,
5931 bfd_boolean have_vma)
5932 {
5933 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5934
5935 /* If no runtime region or VMA has been specified, but the load region
5936 has been specified, then use the load region for the runtime region
5937 as well. */
5938 if (lma_memspec != NULL
5939 && ! have_vma
5940 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5941 *region = *lma_region;
5942 else
5943 *region = lang_memory_region_lookup (memspec, FALSE);
5944
5945 if (have_lma && lma_memspec != 0)
5946 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5947 }
5948
5949 void
5950 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5951 lang_output_section_phdr_list *phdrs,
5952 const char *lma_memspec)
5953 {
5954 lang_get_regions (&current_section->region,
5955 &current_section->lma_region,
5956 memspec, lma_memspec,
5957 current_section->load_base != NULL,
5958 current_section->addr_tree != NULL);
5959 current_section->fill = fill;
5960 current_section->phdrs = phdrs;
5961 stat_ptr = &statement_list;
5962 }
5963
5964 /* Create an absolute symbol with the given name with the value of the
5965 address of first byte of the section named.
5966
5967 If the symbol already exists, then do nothing. */
5968
5969 void
5970 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5971 {
5972 struct bfd_link_hash_entry *h;
5973
5974 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5975 if (h == NULL)
5976 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5977
5978 if (h->type == bfd_link_hash_new
5979 || h->type == bfd_link_hash_undefined)
5980 {
5981 asection *sec;
5982
5983 h->type = bfd_link_hash_defined;
5984
5985 sec = bfd_get_section_by_name (output_bfd, secname);
5986 if (sec == NULL)
5987 h->u.def.value = 0;
5988 else
5989 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5990
5991 h->u.def.section = bfd_abs_section_ptr;
5992 }
5993 }
5994
5995 /* Create an absolute symbol with the given name with the value of the
5996 address of the first byte after the end of the section named.
5997
5998 If the symbol already exists, then do nothing. */
5999
6000 void
6001 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6002 {
6003 struct bfd_link_hash_entry *h;
6004
6005 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6006 if (h == NULL)
6007 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6008
6009 if (h->type == bfd_link_hash_new
6010 || h->type == bfd_link_hash_undefined)
6011 {
6012 asection *sec;
6013
6014 h->type = bfd_link_hash_defined;
6015
6016 sec = bfd_get_section_by_name (output_bfd, secname);
6017 if (sec == NULL)
6018 h->u.def.value = 0;
6019 else
6020 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
6021 + TO_ADDR (sec->size));
6022
6023 h->u.def.section = bfd_abs_section_ptr;
6024 }
6025 }
6026
6027 void
6028 lang_statement_append (lang_statement_list_type *list,
6029 lang_statement_union_type *element,
6030 lang_statement_union_type **field)
6031 {
6032 *(list->tail) = element;
6033 list->tail = field;
6034 }
6035
6036 /* Set the output format type. -oformat overrides scripts. */
6037
6038 void
6039 lang_add_output_format (const char *format,
6040 const char *big,
6041 const char *little,
6042 int from_script)
6043 {
6044 if (output_target == NULL || !from_script)
6045 {
6046 if (command_line.endian == ENDIAN_BIG
6047 && big != NULL)
6048 format = big;
6049 else if (command_line.endian == ENDIAN_LITTLE
6050 && little != NULL)
6051 format = little;
6052
6053 output_target = format;
6054 }
6055 }
6056
6057 /* Enter a group. This creates a new lang_group_statement, and sets
6058 stat_ptr to build new statements within the group. */
6059
6060 void
6061 lang_enter_group (void)
6062 {
6063 lang_group_statement_type *g;
6064
6065 g = new_stat (lang_group_statement, stat_ptr);
6066 lang_list_init (&g->children);
6067 stat_ptr = &g->children;
6068 }
6069
6070 /* Leave a group. This just resets stat_ptr to start writing to the
6071 regular list of statements again. Note that this will not work if
6072 groups can occur inside anything else which can adjust stat_ptr,
6073 but currently they can't. */
6074
6075 void
6076 lang_leave_group (void)
6077 {
6078 stat_ptr = &statement_list;
6079 }
6080
6081 /* Add a new program header. This is called for each entry in a PHDRS
6082 command in a linker script. */
6083
6084 void
6085 lang_new_phdr (const char *name,
6086 etree_type *type,
6087 bfd_boolean filehdr,
6088 bfd_boolean phdrs,
6089 etree_type *at,
6090 etree_type *flags)
6091 {
6092 struct lang_phdr *n, **pp;
6093
6094 n = stat_alloc (sizeof (struct lang_phdr));
6095 n->next = NULL;
6096 n->name = name;
6097 n->type = exp_get_value_int (type, 0, "program header type");
6098 n->filehdr = filehdr;
6099 n->phdrs = phdrs;
6100 n->at = at;
6101 n->flags = flags;
6102
6103 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6104 ;
6105 *pp = n;
6106 }
6107
6108 /* Record the program header information in the output BFD. FIXME: We
6109 should not be calling an ELF specific function here. */
6110
6111 static void
6112 lang_record_phdrs (void)
6113 {
6114 unsigned int alc;
6115 asection **secs;
6116 lang_output_section_phdr_list *last;
6117 struct lang_phdr *l;
6118 lang_output_section_statement_type *os;
6119
6120 alc = 10;
6121 secs = xmalloc (alc * sizeof (asection *));
6122 last = NULL;
6123 for (l = lang_phdr_list; l != NULL; l = l->next)
6124 {
6125 unsigned int c;
6126 flagword flags;
6127 bfd_vma at;
6128
6129 c = 0;
6130 for (os = &lang_output_section_statement.head->output_section_statement;
6131 os != NULL;
6132 os = os->next)
6133 {
6134 lang_output_section_phdr_list *pl;
6135
6136 if (os->constraint == -1)
6137 continue;
6138
6139 pl = os->phdrs;
6140 if (pl != NULL)
6141 last = pl;
6142 else
6143 {
6144 if (os->sectype == noload_section
6145 || os->bfd_section == NULL
6146 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6147 continue;
6148 pl = last;
6149 }
6150
6151 if (os->bfd_section == NULL)
6152 continue;
6153
6154 for (; pl != NULL; pl = pl->next)
6155 {
6156 if (strcmp (pl->name, l->name) == 0)
6157 {
6158 if (c >= alc)
6159 {
6160 alc *= 2;
6161 secs = xrealloc (secs, alc * sizeof (asection *));
6162 }
6163 secs[c] = os->bfd_section;
6164 ++c;
6165 pl->used = TRUE;
6166 }
6167 }
6168 }
6169
6170 if (l->flags == NULL)
6171 flags = 0;
6172 else
6173 flags = exp_get_vma (l->flags, 0, "phdr flags");
6174
6175 if (l->at == NULL)
6176 at = 0;
6177 else
6178 at = exp_get_vma (l->at, 0, "phdr load address");
6179
6180 if (! bfd_record_phdr (output_bfd, l->type,
6181 l->flags != NULL, flags, l->at != NULL,
6182 at, l->filehdr, l->phdrs, c, secs))
6183 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6184 }
6185
6186 free (secs);
6187
6188 /* Make sure all the phdr assignments succeeded. */
6189 for (os = &lang_output_section_statement.head->output_section_statement;
6190 os != NULL;
6191 os = os->next)
6192 {
6193 lang_output_section_phdr_list *pl;
6194
6195 if (os->constraint == -1
6196 || os->bfd_section == NULL)
6197 continue;
6198
6199 for (pl = os->phdrs;
6200 pl != NULL;
6201 pl = pl->next)
6202 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6203 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6204 os->name, pl->name);
6205 }
6206 }
6207
6208 /* Record a list of sections which may not be cross referenced. */
6209
6210 void
6211 lang_add_nocrossref (lang_nocrossref_type *l)
6212 {
6213 struct lang_nocrossrefs *n;
6214
6215 n = xmalloc (sizeof *n);
6216 n->next = nocrossref_list;
6217 n->list = l;
6218 nocrossref_list = n;
6219
6220 /* Set notice_all so that we get informed about all symbols. */
6221 link_info.notice_all = TRUE;
6222 }
6223 \f
6224 /* Overlay handling. We handle overlays with some static variables. */
6225
6226 /* The overlay virtual address. */
6227 static etree_type *overlay_vma;
6228 /* And subsection alignment. */
6229 static etree_type *overlay_subalign;
6230
6231 /* An expression for the maximum section size seen so far. */
6232 static etree_type *overlay_max;
6233
6234 /* A list of all the sections in this overlay. */
6235
6236 struct overlay_list {
6237 struct overlay_list *next;
6238 lang_output_section_statement_type *os;
6239 };
6240
6241 static struct overlay_list *overlay_list;
6242
6243 /* Start handling an overlay. */
6244
6245 void
6246 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6247 {
6248 /* The grammar should prevent nested overlays from occurring. */
6249 ASSERT (overlay_vma == NULL
6250 && overlay_subalign == NULL
6251 && overlay_max == NULL);
6252
6253 overlay_vma = vma_expr;
6254 overlay_subalign = subalign;
6255 }
6256
6257 /* Start a section in an overlay. We handle this by calling
6258 lang_enter_output_section_statement with the correct VMA.
6259 lang_leave_overlay sets up the LMA and memory regions. */
6260
6261 void
6262 lang_enter_overlay_section (const char *name)
6263 {
6264 struct overlay_list *n;
6265 etree_type *size;
6266
6267 lang_enter_output_section_statement (name, overlay_vma, normal_section,
6268 0, overlay_subalign, 0, 0);
6269
6270 /* If this is the first section, then base the VMA of future
6271 sections on this one. This will work correctly even if `.' is
6272 used in the addresses. */
6273 if (overlay_list == NULL)
6274 overlay_vma = exp_nameop (ADDR, name);
6275
6276 /* Remember the section. */
6277 n = xmalloc (sizeof *n);
6278 n->os = current_section;
6279 n->next = overlay_list;
6280 overlay_list = n;
6281
6282 size = exp_nameop (SIZEOF, name);
6283
6284 /* Arrange to work out the maximum section end address. */
6285 if (overlay_max == NULL)
6286 overlay_max = size;
6287 else
6288 overlay_max = exp_binop (MAX_K, overlay_max, size);
6289 }
6290
6291 /* Finish a section in an overlay. There isn't any special to do
6292 here. */
6293
6294 void
6295 lang_leave_overlay_section (fill_type *fill,
6296 lang_output_section_phdr_list *phdrs)
6297 {
6298 const char *name;
6299 char *clean, *s2;
6300 const char *s1;
6301 char *buf;
6302
6303 name = current_section->name;
6304
6305 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6306 region and that no load-time region has been specified. It doesn't
6307 really matter what we say here, since lang_leave_overlay will
6308 override it. */
6309 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6310
6311 /* Define the magic symbols. */
6312
6313 clean = xmalloc (strlen (name) + 1);
6314 s2 = clean;
6315 for (s1 = name; *s1 != '\0'; s1++)
6316 if (ISALNUM (*s1) || *s1 == '_')
6317 *s2++ = *s1;
6318 *s2 = '\0';
6319
6320 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6321 sprintf (buf, "__load_start_%s", clean);
6322 lang_add_assignment (exp_assop ('=', buf,
6323 exp_nameop (LOADADDR, name)));
6324
6325 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6326 sprintf (buf, "__load_stop_%s", clean);
6327 lang_add_assignment (exp_assop ('=', buf,
6328 exp_binop ('+',
6329 exp_nameop (LOADADDR, name),
6330 exp_nameop (SIZEOF, name))));
6331
6332 free (clean);
6333 }
6334
6335 /* Finish an overlay. If there are any overlay wide settings, this
6336 looks through all the sections in the overlay and sets them. */
6337
6338 void
6339 lang_leave_overlay (etree_type *lma_expr,
6340 int nocrossrefs,
6341 fill_type *fill,
6342 const char *memspec,
6343 lang_output_section_phdr_list *phdrs,
6344 const char *lma_memspec)
6345 {
6346 lang_memory_region_type *region;
6347 lang_memory_region_type *lma_region;
6348 struct overlay_list *l;
6349 lang_nocrossref_type *nocrossref;
6350
6351 lang_get_regions (&region, &lma_region,
6352 memspec, lma_memspec,
6353 lma_expr != NULL, FALSE);
6354
6355 nocrossref = NULL;
6356
6357 /* After setting the size of the last section, set '.' to end of the
6358 overlay region. */
6359 if (overlay_list != NULL)
6360 overlay_list->os->update_dot_tree
6361 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6362
6363 l = overlay_list;
6364 while (l != NULL)
6365 {
6366 struct overlay_list *next;
6367
6368 if (fill != NULL && l->os->fill == NULL)
6369 l->os->fill = fill;
6370
6371 l->os->region = region;
6372 l->os->lma_region = lma_region;
6373
6374 /* The first section has the load address specified in the
6375 OVERLAY statement. The rest are worked out from that.
6376 The base address is not needed (and should be null) if
6377 an LMA region was specified. */
6378 if (l->next == 0)
6379 l->os->load_base = lma_expr;
6380
6381 if (phdrs != NULL && l->os->phdrs == NULL)
6382 l->os->phdrs = phdrs;
6383
6384 if (nocrossrefs)
6385 {
6386 lang_nocrossref_type *nc;
6387
6388 nc = xmalloc (sizeof *nc);
6389 nc->name = l->os->name;
6390 nc->next = nocrossref;
6391 nocrossref = nc;
6392 }
6393
6394 next = l->next;
6395 free (l);
6396 l = next;
6397 }
6398
6399 if (nocrossref != NULL)
6400 lang_add_nocrossref (nocrossref);
6401
6402 overlay_vma = NULL;
6403 overlay_list = NULL;
6404 overlay_max = NULL;
6405 }
6406 \f
6407 /* Version handling. This is only useful for ELF. */
6408
6409 /* This global variable holds the version tree that we build. */
6410
6411 struct bfd_elf_version_tree *lang_elf_version_info;
6412
6413 /* If PREV is NULL, return first version pattern matching particular symbol.
6414 If PREV is non-NULL, return first version pattern matching particular
6415 symbol after PREV (previously returned by lang_vers_match). */
6416
6417 static struct bfd_elf_version_expr *
6418 lang_vers_match (struct bfd_elf_version_expr_head *head,
6419 struct bfd_elf_version_expr *prev,
6420 const char *sym)
6421 {
6422 const char *cxx_sym = sym;
6423 const char *java_sym = sym;
6424 struct bfd_elf_version_expr *expr = NULL;
6425
6426 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6427 {
6428 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
6429 if (!cxx_sym)
6430 cxx_sym = sym;
6431 }
6432 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6433 {
6434 java_sym = cplus_demangle (sym, DMGL_JAVA);
6435 if (!java_sym)
6436 java_sym = sym;
6437 }
6438
6439 if (head->htab && (prev == NULL || prev->symbol))
6440 {
6441 struct bfd_elf_version_expr e;
6442
6443 switch (prev ? prev->mask : 0)
6444 {
6445 case 0:
6446 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6447 {
6448 e.symbol = sym;
6449 expr = htab_find (head->htab, &e);
6450 while (expr && strcmp (expr->symbol, sym) == 0)
6451 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6452 goto out_ret;
6453 else
6454 expr = expr->next;
6455 }
6456 /* Fallthrough */
6457 case BFD_ELF_VERSION_C_TYPE:
6458 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6459 {
6460 e.symbol = cxx_sym;
6461 expr = htab_find (head->htab, &e);
6462 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6463 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6464 goto out_ret;
6465 else
6466 expr = expr->next;
6467 }
6468 /* Fallthrough */
6469 case BFD_ELF_VERSION_CXX_TYPE:
6470 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6471 {
6472 e.symbol = java_sym;
6473 expr = htab_find (head->htab, &e);
6474 while (expr && strcmp (expr->symbol, java_sym) == 0)
6475 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6476 goto out_ret;
6477 else
6478 expr = expr->next;
6479 }
6480 /* Fallthrough */
6481 default:
6482 break;
6483 }
6484 }
6485
6486 /* Finally, try the wildcards. */
6487 if (prev == NULL || prev->symbol)
6488 expr = head->remaining;
6489 else
6490 expr = prev->next;
6491 for (; expr; expr = expr->next)
6492 {
6493 const char *s;
6494
6495 if (!expr->pattern)
6496 continue;
6497
6498 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6499 break;
6500
6501 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6502 s = java_sym;
6503 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6504 s = cxx_sym;
6505 else
6506 s = sym;
6507 if (fnmatch (expr->pattern, s, 0) == 0)
6508 break;
6509 }
6510
6511 out_ret:
6512 if (cxx_sym != sym)
6513 free ((char *) cxx_sym);
6514 if (java_sym != sym)
6515 free ((char *) java_sym);
6516 return expr;
6517 }
6518
6519 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6520 return a string pointing to the symbol name. */
6521
6522 static const char *
6523 realsymbol (const char *pattern)
6524 {
6525 const char *p;
6526 bfd_boolean changed = FALSE, backslash = FALSE;
6527 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6528
6529 for (p = pattern, s = symbol; *p != '\0'; ++p)
6530 {
6531 /* It is a glob pattern only if there is no preceding
6532 backslash. */
6533 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
6534 {
6535 free (symbol);
6536 return NULL;
6537 }
6538
6539 if (backslash)
6540 {
6541 /* Remove the preceding backslash. */
6542 *(s - 1) = *p;
6543 changed = TRUE;
6544 }
6545 else
6546 *s++ = *p;
6547
6548 backslash = *p == '\\';
6549 }
6550
6551 if (changed)
6552 {
6553 *s = '\0';
6554 return symbol;
6555 }
6556 else
6557 {
6558 free (symbol);
6559 return pattern;
6560 }
6561 }
6562
6563 /* This is called for each variable name or match expression. NEW is
6564 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6565 pattern to be matched against symbol names. */
6566
6567 struct bfd_elf_version_expr *
6568 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
6569 const char *new,
6570 const char *lang,
6571 bfd_boolean literal_p)
6572 {
6573 struct bfd_elf_version_expr *ret;
6574
6575 ret = xmalloc (sizeof *ret);
6576 ret->next = orig;
6577 ret->pattern = literal_p ? NULL : new;
6578 ret->symver = 0;
6579 ret->script = 0;
6580 ret->symbol = literal_p ? new : realsymbol (new);
6581
6582 if (lang == NULL || strcasecmp (lang, "C") == 0)
6583 ret->mask = BFD_ELF_VERSION_C_TYPE;
6584 else if (strcasecmp (lang, "C++") == 0)
6585 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
6586 else if (strcasecmp (lang, "Java") == 0)
6587 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
6588 else
6589 {
6590 einfo (_("%X%P: unknown language `%s' in version information\n"),
6591 lang);
6592 ret->mask = BFD_ELF_VERSION_C_TYPE;
6593 }
6594
6595 return ldemul_new_vers_pattern (ret);
6596 }
6597
6598 /* This is called for each set of variable names and match
6599 expressions. */
6600
6601 struct bfd_elf_version_tree *
6602 lang_new_vers_node (struct bfd_elf_version_expr *globals,
6603 struct bfd_elf_version_expr *locals)
6604 {
6605 struct bfd_elf_version_tree *ret;
6606
6607 ret = xcalloc (1, sizeof *ret);
6608 ret->globals.list = globals;
6609 ret->locals.list = locals;
6610 ret->match = lang_vers_match;
6611 ret->name_indx = (unsigned int) -1;
6612 return ret;
6613 }
6614
6615 /* This static variable keeps track of version indices. */
6616
6617 static int version_index;
6618
6619 static hashval_t
6620 version_expr_head_hash (const void *p)
6621 {
6622 const struct bfd_elf_version_expr *e = p;
6623
6624 return htab_hash_string (e->symbol);
6625 }
6626
6627 static int
6628 version_expr_head_eq (const void *p1, const void *p2)
6629 {
6630 const struct bfd_elf_version_expr *e1 = p1;
6631 const struct bfd_elf_version_expr *e2 = p2;
6632
6633 return strcmp (e1->symbol, e2->symbol) == 0;
6634 }
6635
6636 static void
6637 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
6638 {
6639 size_t count = 0;
6640 struct bfd_elf_version_expr *e, *next;
6641 struct bfd_elf_version_expr **list_loc, **remaining_loc;
6642
6643 for (e = head->list; e; e = e->next)
6644 {
6645 if (e->symbol)
6646 count++;
6647 head->mask |= e->mask;
6648 }
6649
6650 if (count)
6651 {
6652 head->htab = htab_create (count * 2, version_expr_head_hash,
6653 version_expr_head_eq, NULL);
6654 list_loc = &head->list;
6655 remaining_loc = &head->remaining;
6656 for (e = head->list; e; e = next)
6657 {
6658 next = e->next;
6659 if (!e->symbol)
6660 {
6661 *remaining_loc = e;
6662 remaining_loc = &e->next;
6663 }
6664 else
6665 {
6666 void **loc = htab_find_slot (head->htab, e, INSERT);
6667
6668 if (*loc)
6669 {
6670 struct bfd_elf_version_expr *e1, *last;
6671
6672 e1 = *loc;
6673 last = NULL;
6674 do
6675 {
6676 if (e1->mask == e->mask)
6677 {
6678 last = NULL;
6679 break;
6680 }
6681 last = e1;
6682 e1 = e1->next;
6683 }
6684 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
6685
6686 if (last == NULL)
6687 {
6688 /* This is a duplicate. */
6689 /* FIXME: Memory leak. Sometimes pattern is not
6690 xmalloced alone, but in larger chunk of memory. */
6691 /* free (e->symbol); */
6692 free (e);
6693 }
6694 else
6695 {
6696 e->next = last->next;
6697 last->next = e;
6698 }
6699 }
6700 else
6701 {
6702 *loc = e;
6703 *list_loc = e;
6704 list_loc = &e->next;
6705 }
6706 }
6707 }
6708 *remaining_loc = NULL;
6709 *list_loc = head->remaining;
6710 }
6711 else
6712 head->remaining = head->list;
6713 }
6714
6715 /* This is called when we know the name and dependencies of the
6716 version. */
6717
6718 void
6719 lang_register_vers_node (const char *name,
6720 struct bfd_elf_version_tree *version,
6721 struct bfd_elf_version_deps *deps)
6722 {
6723 struct bfd_elf_version_tree *t, **pp;
6724 struct bfd_elf_version_expr *e1;
6725
6726 if (name == NULL)
6727 name = "";
6728
6729 if ((name[0] == '\0' && lang_elf_version_info != NULL)
6730 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
6731 {
6732 einfo (_("%X%P: anonymous version tag cannot be combined"
6733 " with other version tags\n"));
6734 free (version);
6735 return;
6736 }
6737
6738 /* Make sure this node has a unique name. */
6739 for (t = lang_elf_version_info; t != NULL; t = t->next)
6740 if (strcmp (t->name, name) == 0)
6741 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
6742
6743 lang_finalize_version_expr_head (&version->globals);
6744 lang_finalize_version_expr_head (&version->locals);
6745
6746 /* Check the global and local match names, and make sure there
6747 aren't any duplicates. */
6748
6749 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
6750 {
6751 for (t = lang_elf_version_info; t != NULL; t = t->next)
6752 {
6753 struct bfd_elf_version_expr *e2;
6754
6755 if (t->locals.htab && e1->symbol)
6756 {
6757 e2 = htab_find (t->locals.htab, e1);
6758 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6759 {
6760 if (e1->mask == e2->mask)
6761 einfo (_("%X%P: duplicate expression `%s'"
6762 " in version information\n"), e1->symbol);
6763 e2 = e2->next;
6764 }
6765 }
6766 else if (!e1->symbol)
6767 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
6768 if (strcmp (e1->pattern, e2->pattern) == 0
6769 && e1->mask == e2->mask)
6770 einfo (_("%X%P: duplicate expression `%s'"
6771 " in version information\n"), e1->pattern);
6772 }
6773 }
6774
6775 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
6776 {
6777 for (t = lang_elf_version_info; t != NULL; t = t->next)
6778 {
6779 struct bfd_elf_version_expr *e2;
6780
6781 if (t->globals.htab && e1->symbol)
6782 {
6783 e2 = htab_find (t->globals.htab, e1);
6784 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6785 {
6786 if (e1->mask == e2->mask)
6787 einfo (_("%X%P: duplicate expression `%s'"
6788 " in version information\n"),
6789 e1->symbol);
6790 e2 = e2->next;
6791 }
6792 }
6793 else if (!e1->symbol)
6794 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6795 if (strcmp (e1->pattern, e2->pattern) == 0
6796 && e1->mask == e2->mask)
6797 einfo (_("%X%P: duplicate expression `%s'"
6798 " in version information\n"), e1->pattern);
6799 }
6800 }
6801
6802 version->deps = deps;
6803 version->name = name;
6804 if (name[0] != '\0')
6805 {
6806 ++version_index;
6807 version->vernum = version_index;
6808 }
6809 else
6810 version->vernum = 0;
6811
6812 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6813 ;
6814 *pp = version;
6815 }
6816
6817 /* This is called when we see a version dependency. */
6818
6819 struct bfd_elf_version_deps *
6820 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6821 {
6822 struct bfd_elf_version_deps *ret;
6823 struct bfd_elf_version_tree *t;
6824
6825 ret = xmalloc (sizeof *ret);
6826 ret->next = list;
6827
6828 for (t = lang_elf_version_info; t != NULL; t = t->next)
6829 {
6830 if (strcmp (t->name, name) == 0)
6831 {
6832 ret->version_needed = t;
6833 return ret;
6834 }
6835 }
6836
6837 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6838
6839 return ret;
6840 }
6841
6842 static void
6843 lang_do_version_exports_section (void)
6844 {
6845 struct bfd_elf_version_expr *greg = NULL, *lreg;
6846
6847 LANG_FOR_EACH_INPUT_STATEMENT (is)
6848 {
6849 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6850 char *contents, *p;
6851 bfd_size_type len;
6852
6853 if (sec == NULL)
6854 continue;
6855
6856 len = sec->size;
6857 contents = xmalloc (len);
6858 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6859 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6860
6861 p = contents;
6862 while (p < contents + len)
6863 {
6864 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
6865 p = strchr (p, '\0') + 1;
6866 }
6867
6868 /* Do not free the contents, as we used them creating the regex. */
6869
6870 /* Do not include this section in the link. */
6871 sec->flags |= SEC_EXCLUDE;
6872 }
6873
6874 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
6875 lang_register_vers_node (command_line.version_exports_section,
6876 lang_new_vers_node (greg, lreg), NULL);
6877 }
6878
6879 void
6880 lang_add_unique (const char *name)
6881 {
6882 struct unique_sections *ent;
6883
6884 for (ent = unique_section_list; ent; ent = ent->next)
6885 if (strcmp (ent->name, name) == 0)
6886 return;
6887
6888 ent = xmalloc (sizeof *ent);
6889 ent->name = xstrdup (name);
6890 ent->next = unique_section_list;
6891 unique_section_list = ent;
6892 }
This page took 0.181218 seconds and 5 git commands to generate.