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