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