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