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