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