* ldlang.c (new_afile): Don't pass unadorned NULL to concat.
[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;
ff7a0acf 947 p->local_sym_name = concat ("-l", name, (const char *) 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;
29183214 3393 struct lang_output_section_statement_struct **next;
53d25da6
AM
3394
3395 /* Snip out the output sections we are moving. */
3396 first_os->prev->next = last_os->next;
3397 if (last_os->next == NULL)
29183214
L
3398 {
3399 next = &first_os->prev->next;
3400 lang_output_section_statement.tail
3401 = (lang_statement_union_type **) next;
3402 }
53d25da6
AM
3403 else
3404 last_os->next->prev = first_os->prev;
3405 /* Add them in at the new position. */
3406 last_os->next = where->next;
3407 if (where->next == NULL)
29183214
L
3408 {
3409 next = &last_os->next;
3410 lang_output_section_statement.tail
3411 = (lang_statement_union_type **) next;
3412 }
53d25da6
AM
3413 else
3414 where->next->prev = last_os;
3415 first_os->prev = where;
3416 where->next = first_os;
3417
3418 /* Move the bfd sections in the same way. */
3419 first_sec = NULL;
3420 last_sec = NULL;
3421 for (os = first_os; os != NULL; os = os->next)
3422 {
3423 if (os->bfd_section != NULL
3424 && os->bfd_section->owner != NULL)
3425 {
3426 last_sec = os->bfd_section;
3427 if (first_sec == NULL)
3428 first_sec = last_sec;
3429 }
3430 if (os == last_os)
3431 break;
3432 }
3433 if (last_sec != NULL)
3434 {
3435 asection *sec = where->bfd_section;
3436 if (sec == NULL)
3437 sec = output_prev_sec_find (where);
3438
3439 /* The place we want to insert must come after the
3440 sections we are moving. So if we find no
3441 section or if the section is the same as our
3442 last section, then no move is needed. */
3443 if (sec != NULL && sec != last_sec)
3444 {
3445 /* Trim them off. */
3446 if (first_sec->prev != NULL)
3447 first_sec->prev->next = last_sec->next;
3448 else
3449 output_bfd->sections = last_sec->next;
3450 if (last_sec->next != NULL)
3451 last_sec->next->prev = first_sec->prev;
3452 else
3453 output_bfd->section_last = first_sec->prev;
3454 /* Add back. */
3455 last_sec->next = sec->next;
3456 if (sec->next != NULL)
3457 sec->next->prev = last_sec;
3458 else
3459 output_bfd->section_last = last_sec;
3460 first_sec->prev = sec;
3461 sec->next = first_sec;
3462 }
3463 }
3464
3465 first_os = NULL;
3466 last_os = NULL;
3467 }
3468
3469 ptr = insert_os_after (where);
3470 /* Snip everything after the abs_section output statement we
3471 know is at the start of the list, up to and including
3472 the insert statement we are currently processing. */
3473 first = lang_output_section_statement.head->header.next;
3474 lang_output_section_statement.head->header.next = (*s)->header.next;
3475 /* Add them back where they belong. */
3476 *s = *ptr;
3477 if (*s == NULL)
3478 statement_list.tail = s;
3479 *ptr = first;
3480 s = &lang_output_section_statement.head;
252b5132
RH
3481 }
3482 }
3483}
3484
4bd5a393
AM
3485/* An output section might have been removed after its statement was
3486 added. For example, ldemul_before_allocation can remove dynamic
3487 sections if they turn out to be not needed. Clean them up here. */
3488
8423293d 3489void
1579bae1 3490strip_excluded_output_sections (void)
4bd5a393 3491{
afd7a018 3492 lang_output_section_statement_type *os;
4bd5a393 3493
046183de 3494 /* Run lang_size_sections (if not already done). */
e9ee469a
AM
3495 if (expld.phase != lang_mark_phase_enum)
3496 {
3497 expld.phase = lang_mark_phase_enum;
3498 expld.dataseg.phase = exp_dataseg_none;
3499 one_lang_size_sections_pass (NULL, FALSE);
3500 lang_reset_memory_regions ();
3501 }
3502
afd7a018
AM
3503 for (os = &lang_output_section_statement.head->output_section_statement;
3504 os != NULL;
3505 os = os->next)
4bd5a393 3506 {
75ff4589
L
3507 asection *output_section;
3508 bfd_boolean exclude;
4bd5a393 3509
0841712e
JJ
3510 if (os->constraint == -1)
3511 continue;
8423293d 3512
75ff4589
L
3513 output_section = os->bfd_section;
3514 if (output_section == NULL)
8423293d
AM
3515 continue;
3516
32124d5b
AM
3517 exclude = (output_section->rawsize == 0
3518 && (output_section->flags & SEC_KEEP) == 0
3519 && !bfd_section_removed_from_list (output_bfd,
3520 output_section));
3521
3522 /* Some sections have not yet been sized, notably .gnu.version,
3523 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3524 input sections, so don't drop output sections that have such
3525 input sections unless they are also marked SEC_EXCLUDE. */
3526 if (exclude && output_section->map_head.s != NULL)
75ff4589
L
3527 {
3528 asection *s;
8423293d 3529
32124d5b
AM
3530 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3531 if ((s->flags & SEC_LINKER_CREATED) != 0
3532 && (s->flags & SEC_EXCLUDE) == 0)
e9ee469a
AM
3533 {
3534 exclude = FALSE;
3535 break;
3536 }
75ff4589 3537 }
8423293d 3538
32124d5b
AM
3539 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3540 output_section->map_head.link_order = NULL;
3541 output_section->map_tail.link_order = NULL;
3542
3543 if (exclude)
4bd5a393 3544 {
e9ee469a
AM
3545 /* We don't set bfd_section to NULL since bfd_section of the
3546 removed output section statement may still be used. */
3d263d86
AM
3547 if (!os->section_relative_symbol
3548 && !os->update_dot_tree)
74541ad4 3549 os->ignored = TRUE;
e9ee469a 3550 output_section->flags |= SEC_EXCLUDE;
32124d5b
AM
3551 bfd_section_list_remove (output_bfd, output_section);
3552 output_bfd->section_count--;
4bd5a393
AM
3553 }
3554 }
8423293d
AM
3555
3556 /* Stop future calls to lang_add_section from messing with map_head
3557 and map_tail link_order fields. */
3558 stripped_excluded_sections = TRUE;
4bd5a393
AM
3559}
3560
252b5132 3561static void
1579bae1
AM
3562print_output_section_statement
3563 (lang_output_section_statement_type *output_section_statement)
252b5132
RH
3564{
3565 asection *section = output_section_statement->bfd_section;
3566 int len;
3567
3568 if (output_section_statement != abs_output_section)
3569 {
3570 minfo ("\n%s", output_section_statement->name);
3571
3572 if (section != NULL)
3573 {
3574 print_dot = section->vma;
3575
3576 len = strlen (output_section_statement->name);
3577 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3578 {
3579 print_nl ();
3580 len = 0;
3581 }
3582 while (len < SECTION_NAME_MAP_LENGTH)
3583 {
3584 print_space ();
3585 ++len;
3586 }
3587
eea6121a 3588 minfo ("0x%V %W", section->vma, section->size);
252b5132 3589
67f744f3
AM
3590 if (section->vma != section->lma)
3591 minfo (_(" load address 0x%V"), section->lma);
252b5132
RH
3592 }
3593
3594 print_nl ();
3595 }
3596
3597 print_statement_list (output_section_statement->children.head,
3598 output_section_statement);
3599}
3600
3b83e13a
NC
3601/* Scan for the use of the destination in the right hand side
3602 of an expression. In such cases we will not compute the
3603 correct expression, since the value of DST that is used on
3604 the right hand side will be its final value, not its value
3605 just before this expression is evaluated. */
329c1c86 3606
3b83e13a
NC
3607static bfd_boolean
3608scan_for_self_assignment (const char * dst, etree_type * rhs)
3609{
3610 if (rhs == NULL || dst == NULL)
3611 return FALSE;
3612
3613 switch (rhs->type.node_class)
3614 {
3615 case etree_binary:
3616 return scan_for_self_assignment (dst, rhs->binary.lhs)
3617 || scan_for_self_assignment (dst, rhs->binary.rhs);
3618
3619 case etree_trinary:
3620 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3621 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3622
3623 case etree_assign:
3624 case etree_provided:
3625 case etree_provide:
3626 if (strcmp (dst, rhs->assign.dst) == 0)
3627 return TRUE;
3628 return scan_for_self_assignment (dst, rhs->assign.src);
3629
3630 case etree_unary:
3631 return scan_for_self_assignment (dst, rhs->unary.child);
3632
3633 case etree_value:
3634 if (rhs->value.str)
3635 return strcmp (dst, rhs->value.str) == 0;
3636 return FALSE;
3637
3638 case etree_name:
3639 if (rhs->name.name)
3640 return strcmp (dst, rhs->name.name) == 0;
3641 return FALSE;
3642
3643 default:
3644 break;
3645 }
3646
3647 return FALSE;
3648}
3649
3650
252b5132 3651static void
1579bae1
AM
3652print_assignment (lang_assignment_statement_type *assignment,
3653 lang_output_section_statement_type *output_section)
252b5132 3654{
3b83e13a
NC
3655 unsigned int i;
3656 bfd_boolean is_dot;
3657 bfd_boolean computation_is_valid = TRUE;
afd7a018 3658 etree_type *tree;
252b5132
RH
3659
3660 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3661 print_space ();
3662
afd7a018
AM
3663 if (assignment->exp->type.node_class == etree_assert)
3664 {
3b83e13a 3665 is_dot = FALSE;
afd7a018 3666 tree = assignment->exp->assert_s.child;
3b83e13a 3667 computation_is_valid = TRUE;
afd7a018
AM
3668 }
3669 else
3670 {
3671 const char *dst = assignment->exp->assign.dst;
3b83e13a
NC
3672
3673 is_dot = (dst[0] == '.' && dst[1] == 0);
afd7a018 3674 tree = assignment->exp->assign.src;
3b83e13a 3675 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
afd7a018
AM
3676 }
3677
e9ee469a
AM
3678 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3679 if (expld.result.valid_p)
7b17f854 3680 {
7b17f854
RS
3681 bfd_vma value;
3682
3b83e13a
NC
3683 if (computation_is_valid)
3684 {
e9ee469a 3685 value = expld.result.value;
10dbd1f3 3686
e9ee469a
AM
3687 if (expld.result.section)
3688 value += expld.result.section->vma;
7b17f854 3689
3b83e13a
NC
3690 minfo ("0x%V", value);
3691 if (is_dot)
3692 print_dot = value;
3693 }
3694 else
3695 {
3696 struct bfd_link_hash_entry *h;
3697
3698 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3699 FALSE, FALSE, TRUE);
3700 if (h)
3701 {
10dbd1f3
NC
3702 value = h->u.def.value;
3703
e9ee469a 3704 if (expld.result.section)
a1438fce 3705 value += expld.result.section->vma;
3b83e13a
NC
3706
3707 minfo ("[0x%V]", value);
3708 }
3709 else
3710 minfo ("[unresolved]");
3711 }
7b17f854 3712 }
252b5132
RH
3713 else
3714 {
3715 minfo ("*undef* ");
3716#ifdef BFD64
3717 minfo (" ");
3718#endif
3719 }
3720
3721 minfo (" ");
252b5132 3722 exp_print_tree (assignment->exp);
252b5132
RH
3723 print_nl ();
3724}
3725
3726static void
1579bae1 3727print_input_statement (lang_input_statement_type *statm)
252b5132 3728{
1579bae1 3729 if (statm->filename != NULL)
252b5132
RH
3730 {
3731 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3732 }
3733}
3734
3735/* Print all symbols defined in a particular section. This is called
35835446 3736 via bfd_link_hash_traverse, or by print_all_symbols. */
252b5132 3737
b34976b6 3738static bfd_boolean
1579bae1 3739print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
252b5132 3740{
1579bae1 3741 asection *sec = ptr;
252b5132
RH
3742
3743 if ((hash_entry->type == bfd_link_hash_defined
3744 || hash_entry->type == bfd_link_hash_defweak)
3745 && sec == hash_entry->u.def.section)
3746 {
3747 int i;
3748
3749 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3750 print_space ();
3751 minfo ("0x%V ",
3752 (hash_entry->u.def.value
3753 + hash_entry->u.def.section->output_offset
3754 + hash_entry->u.def.section->output_section->vma));
3755
3756 minfo (" %T\n", hash_entry->root.string);
3757 }
3758
b34976b6 3759 return TRUE;
252b5132
RH
3760}
3761
35835446 3762static void
e0f6802f 3763print_all_symbols (asection *sec)
35835446
JR
3764{
3765 struct fat_user_section_struct *ud = get_userdata (sec);
3766 struct map_symbol_def *def;
3767
afd7a018
AM
3768 if (!ud)
3769 return;
3770
35835446
JR
3771 *ud->map_symbol_def_tail = 0;
3772 for (def = ud->map_symbol_def_head; def; def = def->next)
3773 print_one_symbol (def->entry, sec);
3774}
3775
252b5132
RH
3776/* Print information about an input section to the map file. */
3777
3778static void
4d4920ec 3779print_input_section (asection *i)
252b5132 3780{
eea6121a 3781 bfd_size_type size = i->size;
abe6ac95
AM
3782 int len;
3783 bfd_vma addr;
e5caa5e0
AM
3784
3785 init_opb ();
4d4920ec 3786
abe6ac95
AM
3787 print_space ();
3788 minfo ("%s", i->name);
252b5132 3789
abe6ac95
AM
3790 len = 1 + strlen (i->name);
3791 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3792 {
3793 print_nl ();
3794 len = 0;
3795 }
3796 while (len < SECTION_NAME_MAP_LENGTH)
3797 {
57ceae94 3798 print_space ();
abe6ac95
AM
3799 ++len;
3800 }
252b5132 3801
abe6ac95
AM
3802 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3803 addr = i->output_section->vma + i->output_offset;
3804 else
3805 {
3806 addr = print_dot;
3807 size = 0;
3808 }
252b5132 3809
abe6ac95 3810 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
252b5132 3811
abe6ac95
AM
3812 if (size != i->rawsize && i->rawsize != 0)
3813 {
3814 len = SECTION_NAME_MAP_LENGTH + 3;
252b5132 3815#ifdef BFD64
abe6ac95 3816 len += 16;
252b5132 3817#else
abe6ac95 3818 len += 8;
252b5132 3819#endif
abe6ac95
AM
3820 while (len > 0)
3821 {
3822 print_space ();
3823 --len;
57ceae94
AM
3824 }
3825
abe6ac95
AM
3826 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3827 }
252b5132 3828
abe6ac95
AM
3829 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3830 {
c0f00686 3831 if (link_info.reduce_memory_overheads)
abe6ac95
AM
3832 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3833 else
3834 print_all_symbols (i);
3835
3836 print_dot = addr + TO_ADDR (size);
252b5132
RH
3837 }
3838}
3839
3840static void
1579bae1 3841print_fill_statement (lang_fill_statement_type *fill)
252b5132 3842{
2c382fb6
AM
3843 size_t size;
3844 unsigned char *p;
3845 fputs (" FILL mask 0x", config.map_file);
3846 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3847 fprintf (config.map_file, "%02x", *p);
3848 fputs ("\n", config.map_file);
252b5132
RH
3849}
3850
3851static void
1579bae1 3852print_data_statement (lang_data_statement_type *data)
252b5132
RH
3853{
3854 int i;
3855 bfd_vma addr;
3856 bfd_size_type size;
3857 const char *name;
3858
e5caa5e0 3859 init_opb ();
252b5132
RH
3860 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3861 print_space ();
3862
7fabd029 3863 addr = data->output_offset;
252b5132
RH
3864 if (data->output_section != NULL)
3865 addr += data->output_section->vma;
3866
3867 switch (data->type)
3868 {
3869 default:
3870 abort ();
3871 case BYTE:
3872 size = BYTE_SIZE;
3873 name = "BYTE";
3874 break;
3875 case SHORT:
3876 size = SHORT_SIZE;
3877 name = "SHORT";
3878 break;
3879 case LONG:
3880 size = LONG_SIZE;
3881 name = "LONG";
3882 break;
3883 case QUAD:
3884 size = QUAD_SIZE;
3885 name = "QUAD";
3886 break;
3887 case SQUAD:
3888 size = QUAD_SIZE;
3889 name = "SQUAD";
3890 break;
3891 }
3892
3893 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3894
3895 if (data->exp->type.node_class != etree_value)
3896 {
3897 print_space ();
3898 exp_print_tree (data->exp);
3899 }
3900
3901 print_nl ();
3902
e5caa5e0 3903 print_dot = addr + TO_ADDR (size);
252b5132
RH
3904}
3905
3906/* Print an address statement. These are generated by options like
3907 -Ttext. */
3908
3909static void
1579bae1 3910print_address_statement (lang_address_statement_type *address)
252b5132
RH
3911{
3912 minfo (_("Address of section %s set to "), address->section_name);
3913 exp_print_tree (address->address);
3914 print_nl ();
3915}
3916
3917/* Print a reloc statement. */
3918
3919static void
1579bae1 3920print_reloc_statement (lang_reloc_statement_type *reloc)
252b5132
RH
3921{
3922 int i;
3923 bfd_vma addr;
3924 bfd_size_type size;
3925
e5caa5e0 3926 init_opb ();
252b5132
RH
3927 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3928 print_space ();
3929
7fabd029 3930 addr = reloc->output_offset;
252b5132
RH
3931 if (reloc->output_section != NULL)
3932 addr += reloc->output_section->vma;
3933
3934 size = bfd_get_reloc_size (reloc->howto);
3935
3936 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3937
3938 if (reloc->name != NULL)
3939 minfo ("%s+", reloc->name);
3940 else
3941 minfo ("%s+", reloc->section->name);
3942
3943 exp_print_tree (reloc->addend_exp);
3944
3945 print_nl ();
3946
e5caa5e0 3947 print_dot = addr + TO_ADDR (size);
5f992e62 3948}
252b5132
RH
3949
3950static void
1579bae1 3951print_padding_statement (lang_padding_statement_type *s)
252b5132
RH
3952{
3953 int len;
3954 bfd_vma addr;
3955
e5caa5e0 3956 init_opb ();
252b5132
RH
3957 minfo (" *fill*");
3958
3959 len = sizeof " *fill*" - 1;
3960 while (len < SECTION_NAME_MAP_LENGTH)
3961 {
3962 print_space ();
3963 ++len;
3964 }
3965
3966 addr = s->output_offset;
3967 if (s->output_section != NULL)
3968 addr += s->output_section->vma;
5f9b8920 3969 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
252b5132 3970
2c382fb6
AM
3971 if (s->fill->size != 0)
3972 {
3973 size_t size;
3974 unsigned char *p;
3975 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3976 fprintf (config.map_file, "%02x", *p);
3977 }
252b5132
RH
3978
3979 print_nl ();
3980
e5caa5e0 3981 print_dot = addr + TO_ADDR (s->size);
252b5132
RH
3982}
3983
3984static void
1579bae1
AM
3985print_wild_statement (lang_wild_statement_type *w,
3986 lang_output_section_statement_type *os)
252b5132 3987{
b6bf44ba
AM
3988 struct wildcard_list *sec;
3989
252b5132
RH
3990 print_space ();
3991
3992 if (w->filenames_sorted)
3993 minfo ("SORT(");
08da4cac 3994 if (w->filename != NULL)
252b5132
RH
3995 minfo ("%s", w->filename);
3996 else
3997 minfo ("*");
3998 if (w->filenames_sorted)
3999 minfo (")");
4000
4001 minfo ("(");
b6bf44ba
AM
4002 for (sec = w->section_list; sec; sec = sec->next)
4003 {
4004 if (sec->spec.sorted)
4005 minfo ("SORT(");
4006 if (sec->spec.exclude_name_list != NULL)
4007 {
4008 name_list *tmp;
34786259 4009 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
b6bf44ba 4010 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
34786259
AM
4011 minfo (" %s", tmp->name);
4012 minfo (") ");
b6bf44ba
AM
4013 }
4014 if (sec->spec.name != NULL)
4015 minfo ("%s", sec->spec.name);
4016 else
4017 minfo ("*");
4018 if (sec->spec.sorted)
4019 minfo (")");
34786259
AM
4020 if (sec->next)
4021 minfo (" ");
b6bf44ba 4022 }
252b5132
RH
4023 minfo (")");
4024
4025 print_nl ();
4026
4027 print_statement_list (w->children.head, os);
4028}
4029
4030/* Print a group statement. */
4031
4032static void
1579bae1
AM
4033print_group (lang_group_statement_type *s,
4034 lang_output_section_statement_type *os)
252b5132
RH
4035{
4036 fprintf (config.map_file, "START GROUP\n");
4037 print_statement_list (s->children.head, os);
4038 fprintf (config.map_file, "END GROUP\n");
4039}
4040
4041/* Print the list of statements in S.
4042 This can be called for any statement type. */
4043
4044static void
1579bae1
AM
4045print_statement_list (lang_statement_union_type *s,
4046 lang_output_section_statement_type *os)
252b5132
RH
4047{
4048 while (s != NULL)
4049 {
4050 print_statement (s, os);
bba1a0c0 4051 s = s->header.next;
252b5132
RH
4052 }
4053}
4054
4055/* Print the first statement in statement list S.
4056 This can be called for any statement type. */
4057
4058static void
1579bae1
AM
4059print_statement (lang_statement_union_type *s,
4060 lang_output_section_statement_type *os)
252b5132
RH
4061{
4062 switch (s->header.type)
4063 {
4064 default:
4065 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4066 FAIL ();
4067 break;
4068 case lang_constructors_statement_enum:
4069 if (constructor_list.head != NULL)
4070 {
4071 if (constructors_sorted)
4072 minfo (" SORT (CONSTRUCTORS)\n");
4073 else
4074 minfo (" CONSTRUCTORS\n");
4075 print_statement_list (constructor_list.head, os);
4076 }
4077 break;
4078 case lang_wild_statement_enum:
4079 print_wild_statement (&s->wild_statement, os);
4080 break;
4081 case lang_address_statement_enum:
4082 print_address_statement (&s->address_statement);
4083 break;
4084 case lang_object_symbols_statement_enum:
4085 minfo (" CREATE_OBJECT_SYMBOLS\n");
4086 break;
4087 case lang_fill_statement_enum:
4088 print_fill_statement (&s->fill_statement);
4089 break;
4090 case lang_data_statement_enum:
4091 print_data_statement (&s->data_statement);
4092 break;
4093 case lang_reloc_statement_enum:
4094 print_reloc_statement (&s->reloc_statement);
4095 break;
4096 case lang_input_section_enum:
4d4920ec 4097 print_input_section (s->input_section.section);
252b5132
RH
4098 break;
4099 case lang_padding_statement_enum:
4100 print_padding_statement (&s->padding_statement);
4101 break;
4102 case lang_output_section_statement_enum:
4103 print_output_section_statement (&s->output_section_statement);
4104 break;
4105 case lang_assignment_statement_enum:
4106 print_assignment (&s->assignment_statement, os);
4107 break;
4108 case lang_target_statement_enum:
4109 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4110 break;
4111 case lang_output_statement_enum:
4112 minfo ("OUTPUT(%s", s->output_statement.name);
4113 if (output_target != NULL)
4114 minfo (" %s", output_target);
4115 minfo (")\n");
4116 break;
4117 case lang_input_statement_enum:
4118 print_input_statement (&s->input_statement);
4119 break;
4120 case lang_group_statement_enum:
4121 print_group (&s->group_statement, os);
4122 break;
53d25da6
AM
4123 case lang_insert_statement_enum:
4124 minfo ("INSERT %s %s\n",
4125 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4126 s->insert_statement.where);
4127 break;
252b5132
RH
4128 }
4129}
4130
4131static void
1579bae1 4132print_statements (void)
252b5132
RH
4133{
4134 print_statement_list (statement_list.head, abs_output_section);
4135}
4136
4137/* Print the first N statements in statement list S to STDERR.
4138 If N == 0, nothing is printed.
4139 If N < 0, the entire list is printed.
4140 Intended to be called from GDB. */
4141
4142void
1579bae1 4143dprint_statement (lang_statement_union_type *s, int n)
252b5132
RH
4144{
4145 FILE *map_save = config.map_file;
4146
4147 config.map_file = stderr;
4148
4149 if (n < 0)
4150 print_statement_list (s, abs_output_section);
4151 else
4152 {
4153 while (s && --n >= 0)
4154 {
4155 print_statement (s, abs_output_section);
bba1a0c0 4156 s = s->header.next;
252b5132
RH
4157 }
4158 }
4159
4160 config.map_file = map_save;
4161}
4162
b3327aad 4163static void
1579bae1
AM
4164insert_pad (lang_statement_union_type **ptr,
4165 fill_type *fill,
4166 unsigned int alignment_needed,
4167 asection *output_section,
4168 bfd_vma dot)
252b5132 4169{
2c382fb6 4170 static fill_type zero_fill = { 1, { 0 } };
e9ee469a 4171 lang_statement_union_type *pad = NULL;
b3327aad 4172
e9ee469a
AM
4173 if (ptr != &statement_list.head)
4174 pad = ((lang_statement_union_type *)
4175 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4176 if (pad != NULL
4177 && pad->header.type == lang_padding_statement_enum
4178 && pad->padding_statement.output_section == output_section)
4179 {
4180 /* Use the existing pad statement. */
4181 }
4182 else if ((pad = *ptr) != NULL
2af02257 4183 && pad->header.type == lang_padding_statement_enum
b3327aad 4184 && pad->padding_statement.output_section == output_section)
252b5132 4185 {
e9ee469a 4186 /* Use the existing pad statement. */
252b5132 4187 }
b3327aad 4188 else
252b5132 4189 {
b3327aad 4190 /* Make a new padding statement, linked into existing chain. */
1579bae1 4191 pad = stat_alloc (sizeof (lang_padding_statement_type));
b3327aad
AM
4192 pad->header.next = *ptr;
4193 *ptr = pad;
4194 pad->header.type = lang_padding_statement_enum;
4195 pad->padding_statement.output_section = output_section;
1579bae1 4196 if (fill == NULL)
2c382fb6 4197 fill = &zero_fill;
b3327aad 4198 pad->padding_statement.fill = fill;
252b5132 4199 }
b3327aad
AM
4200 pad->padding_statement.output_offset = dot - output_section->vma;
4201 pad->padding_statement.size = alignment_needed;
eea6121a 4202 output_section->size += alignment_needed;
252b5132
RH
4203}
4204
08da4cac
KH
4205/* Work out how much this section will move the dot point. */
4206
252b5132 4207static bfd_vma
6feb9908
AM
4208size_input_section
4209 (lang_statement_union_type **this_ptr,
4210 lang_output_section_statement_type *output_section_statement,
4211 fill_type *fill,
4212 bfd_vma dot)
252b5132
RH
4213{
4214 lang_input_section_type *is = &((*this_ptr)->input_section);
4215 asection *i = is->section;
4216
7b986e99
AM
4217 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4218 && (i->flags & SEC_EXCLUDE) == 0)
252b5132 4219 {
b3327aad
AM
4220 unsigned int alignment_needed;
4221 asection *o;
4222
4223 /* Align this section first to the input sections requirement,
4224 then to the output section's requirement. If this alignment
4225 is greater than any seen before, then record it too. Perform
4226 the alignment by inserting a magic 'padding' statement. */
4227
252b5132 4228 if (output_section_statement->subsection_alignment != -1)
b3327aad
AM
4229 i->alignment_power = output_section_statement->subsection_alignment;
4230
4231 o = output_section_statement->bfd_section;
4232 if (o->alignment_power < i->alignment_power)
4233 o->alignment_power = i->alignment_power;
252b5132 4234
b3327aad
AM
4235 alignment_needed = align_power (dot, i->alignment_power) - dot;
4236
4237 if (alignment_needed != 0)
4238 {
e5caa5e0 4239 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
b3327aad
AM
4240 dot += alignment_needed;
4241 }
252b5132 4242
08da4cac 4243 /* Remember where in the output section this input section goes. */
252b5132 4244
b3327aad 4245 i->output_offset = dot - o->vma;
252b5132 4246
08da4cac 4247 /* Mark how big the output section must be to contain this now. */
eea6121a
AM
4248 dot += TO_ADDR (i->size);
4249 o->size = TO_SIZE (dot - o->vma);
252b5132
RH
4250 }
4251 else
4252 {
4253 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4254 }
4255
4256 return dot;
4257}
4258
5daa8fe7
L
4259static int
4260sort_sections_by_lma (const void *arg1, const void *arg2)
4261{
4262 const asection *sec1 = *(const asection **) arg1;
4263 const asection *sec2 = *(const asection **) arg2;
4264
4265 if (bfd_section_lma (sec1->owner, sec1)
4266 < bfd_section_lma (sec2->owner, sec2))
4267 return -1;
4268 else if (bfd_section_lma (sec1->owner, sec1)
4269 > bfd_section_lma (sec2->owner, sec2))
4270 return 1;
7f6a71ff
JM
4271 else if (sec1->id < sec2->id)
4272 return -1;
4273 else if (sec1->id > sec2->id)
4274 return 1;
5daa8fe7
L
4275
4276 return 0;
4277}
4278
eea6121a 4279#define IGNORE_SECTION(s) \
afd7a018
AM
4280 ((s->flags & SEC_NEVER_LOAD) != 0 \
4281 || (s->flags & SEC_ALLOC) == 0 \
4282 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
de7f8cc8 4283 && (s->flags & SEC_LOAD) == 0))
d1778b88 4284
252b5132 4285/* Check to see if any allocated sections overlap with other allocated
afd7a018 4286 sections. This can happen if a linker script specifies the output
20e56351
DJ
4287 section addresses of the two sections. Also check whether any memory
4288 region has overflowed. */
08da4cac 4289
252b5132 4290static void
1579bae1 4291lang_check_section_addresses (void)
252b5132 4292{
5daa8fe7
L
4293 asection *s, *os;
4294 asection **sections, **spp;
4295 unsigned int count;
4296 bfd_vma s_start;
4297 bfd_vma s_end;
4298 bfd_vma os_start;
4299 bfd_vma os_end;
4300 bfd_size_type amt;
20e56351 4301 lang_memory_region_type *m;
5daa8fe7
L
4302
4303 if (bfd_count_sections (output_bfd) <= 1)
4304 return;
4305
4306 amt = bfd_count_sections (output_bfd) * sizeof (asection *);
4307 sections = xmalloc (amt);
252b5132
RH
4308
4309 /* Scan all sections in the output list. */
5daa8fe7 4310 count = 0;
252b5132 4311 for (s = output_bfd->sections; s != NULL; s = s->next)
33275c22 4312 {
5daa8fe7 4313 /* Only consider loadable sections with real contents. */
de7f8cc8 4314 if (IGNORE_SECTION (s) || s->size == 0)
33275c22 4315 continue;
5f992e62 4316
5daa8fe7
L
4317 sections[count] = s;
4318 count++;
4319 }
329c1c86 4320
5daa8fe7
L
4321 if (count <= 1)
4322 return;
5f992e62 4323
5daa8fe7
L
4324 qsort (sections, (size_t) count, sizeof (asection *),
4325 sort_sections_by_lma);
5f992e62 4326
5daa8fe7
L
4327 spp = sections;
4328 s = *spp++;
4329 s_start = bfd_section_lma (output_bfd, s);
4330 s_end = s_start + TO_ADDR (s->size) - 1;
4331 for (count--; count; count--)
4332 {
4333 /* We must check the sections' LMA addresses not their VMA
4334 addresses because overlay sections can have overlapping VMAs
4335 but they must have distinct LMAs. */
4336 os = s;
329c1c86 4337 os_start = s_start;
5daa8fe7
L
4338 os_end = s_end;
4339 s = *spp++;
4340 s_start = bfd_section_lma (output_bfd, s);
4341 s_end = s_start + TO_ADDR (s->size) - 1;
5f992e62 4342
5daa8fe7
L
4343 /* Look for an overlap. */
4344 if (s_end >= os_start && s_start <= os_end)
4345 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4346 s->name, s_start, s_end, os->name, os_start, os_end);
33275c22 4347 }
5daa8fe7
L
4348
4349 free (sections);
20e56351
DJ
4350
4351 /* If any memory region has overflowed, report by how much.
4352 We do not issue this diagnostic for regions that had sections
4353 explicitly placed outside their bounds; os_region_check's
4354 diagnostics are adequate for that case.
4355
4356 FIXME: It is conceivable that m->current - (m->origin + m->length)
4357 might overflow a 32-bit integer. There is, alas, no way to print
4358 a bfd_vma quantity in decimal. */
4359 for (m = lang_memory_region_list; m; m = m->next)
4360 if (m->had_full_message)
4361 einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
4362 m->name, (long)(m->current - (m->origin + m->length)));
4363
252b5132
RH
4364}
4365
562d3460
TW
4366/* Make sure the new address is within the region. We explicitly permit the
4367 current address to be at the exact end of the region when the address is
4368 non-zero, in case the region is at the end of addressable memory and the
5f992e62 4369 calculation wraps around. */
562d3460
TW
4370
4371static void
1579bae1 4372os_region_check (lang_output_section_statement_type *os,
6bdafbeb 4373 lang_memory_region_type *region,
1579bae1
AM
4374 etree_type *tree,
4375 bfd_vma base)
562d3460
TW
4376{
4377 if ((region->current < region->origin
4378 || (region->current - region->origin > region->length))
4379 && ((region->current != region->origin + region->length)
b7a26f91 4380 || base == 0))
562d3460 4381 {
1579bae1 4382 if (tree != NULL)
b7a26f91 4383 {
6feb9908
AM
4384 einfo (_("%X%P: address 0x%v of %B section %s"
4385 " is not within region %s\n"),
b7a26f91
KH
4386 region->current,
4387 os->bfd_section->owner,
4388 os->bfd_section->name,
4389 region->name);
4390 }
20e56351 4391 else if (!region->had_full_message)
b7a26f91 4392 {
20e56351
DJ
4393 region->had_full_message = TRUE;
4394
4395 einfo (_("%X%P: %B section %s will not fit in region %s\n"),
b7a26f91 4396 os->bfd_section->owner,
20e56351
DJ
4397 os->bfd_section->name,
4398 region->name);
b7a26f91 4399 }
562d3460
TW
4400 }
4401}
4402
252b5132
RH
4403/* Set the sizes for all the output sections. */
4404
2d20f7bf 4405static bfd_vma
1579bae1
AM
4406lang_size_sections_1
4407 (lang_statement_union_type *s,
4408 lang_output_section_statement_type *output_section_statement,
4409 lang_statement_union_type **prev,
4410 fill_type *fill,
4411 bfd_vma dot,
4412 bfd_boolean *relax,
4413 bfd_boolean check_regions)
252b5132
RH
4414{
4415 /* Size up the sections from their constituent parts. */
1579bae1 4416 for (; s != NULL; s = s->header.next)
252b5132
RH
4417 {
4418 switch (s->header.type)
4419 {
4420 case lang_output_section_statement_enum:
4421 {
e9ee469a 4422 bfd_vma newdot, after;
d1778b88 4423 lang_output_section_statement_type *os;
cde9e0be 4424 lang_memory_region_type *r;
252b5132 4425
d1778b88 4426 os = &s->output_section_statement;
a5df8c84
AM
4427 if (os->addr_tree != NULL)
4428 {
cde9e0be 4429 os->processed_vma = FALSE;
a5df8c84 4430 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
a5df8c84 4431
3bf9618b
AM
4432 if (expld.result.valid_p)
4433 dot = expld.result.value + expld.result.section->vma;
4434 else if (expld.phase != lang_mark_phase_enum)
a5df8c84
AM
4435 einfo (_("%F%S: non constant or forward reference"
4436 " address expression for section %s\n"),
4437 os->name);
a5df8c84
AM
4438 }
4439
e9ee469a 4440 if (os->bfd_section == NULL)
75ff4589 4441 /* This section was removed or never actually created. */
252b5132
RH
4442 break;
4443
4444 /* If this is a COFF shared library section, use the size and
4445 address from the input section. FIXME: This is COFF
4446 specific; it would be cleaner if there were some other way
4447 to do this, but nothing simple comes to mind. */
ebe372c1
L
4448 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
4449 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
4450 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
252b5132 4451 {
08da4cac 4452 asection *input;
252b5132
RH
4453
4454 if (os->children.head == NULL
bba1a0c0 4455 || os->children.head->header.next != NULL
6feb9908
AM
4456 || (os->children.head->header.type
4457 != lang_input_section_enum))
4458 einfo (_("%P%X: Internal error on COFF shared library"
4459 " section %s\n"), os->name);
252b5132
RH
4460
4461 input = os->children.head->input_section.section;
4462 bfd_set_section_vma (os->bfd_section->owner,
4463 os->bfd_section,
4464 bfd_section_vma (input->owner, input));
eea6121a 4465 os->bfd_section->size = input->size;
252b5132
RH
4466 break;
4467 }
4468
a5df8c84 4469 newdot = dot;
252b5132
RH
4470 if (bfd_is_abs_section (os->bfd_section))
4471 {
4472 /* No matter what happens, an abs section starts at zero. */
4473 ASSERT (os->bfd_section->vma == 0);
4474 }
4475 else
4476 {
94b50910 4477 int align;
7270c5ed 4478
1579bae1 4479 if (os->addr_tree == NULL)
252b5132
RH
4480 {
4481 /* No address specified for this section, get one
4482 from the region specification. */
1579bae1 4483 if (os->region == NULL
6feb9908 4484 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
252b5132 4485 && os->region->name[0] == '*'
6feb9908
AM
4486 && strcmp (os->region->name,
4487 DEFAULT_MEMORY_REGION) == 0))
252b5132
RH
4488 {
4489 os->region = lang_memory_default (os->bfd_section);
4490 }
4491
4492 /* If a loadable section is using the default memory
4493 region, and some non default memory regions were
66184979 4494 defined, issue an error message. */
f0636a44
AM
4495 if (!os->ignored
4496 && !IGNORE_SECTION (os->bfd_section)
1049f94e 4497 && ! link_info.relocatable
cf888e70 4498 && check_regions
6feb9908
AM
4499 && strcmp (os->region->name,
4500 DEFAULT_MEMORY_REGION) == 0
252b5132 4501 && lang_memory_region_list != NULL
d1778b88 4502 && (strcmp (lang_memory_region_list->name,
a747ee4d 4503 DEFAULT_MEMORY_REGION) != 0
e9ee469a
AM
4504 || lang_memory_region_list->next != NULL)
4505 && expld.phase != lang_mark_phase_enum)
66184979
NC
4506 {
4507 /* By default this is an error rather than just a
4508 warning because if we allocate the section to the
4509 default memory region we can end up creating an
07f3b6ad
KH
4510 excessively large binary, or even seg faulting when
4511 attempting to perform a negative seek. See
6feb9908 4512 sources.redhat.com/ml/binutils/2003-04/msg00423.html
66184979
NC
4513 for an example of this. This behaviour can be
4514 overridden by the using the --no-check-sections
4515 switch. */
4516 if (command_line.check_section_addresses)
6feb9908
AM
4517 einfo (_("%P%F: error: no memory region specified"
4518 " for loadable section `%s'\n"),
66184979
NC
4519 bfd_get_section_name (output_bfd,
4520 os->bfd_section));
4521 else
6feb9908
AM
4522 einfo (_("%P: warning: no memory region specified"
4523 " for loadable section `%s'\n"),
66184979
NC
4524 bfd_get_section_name (output_bfd,
4525 os->bfd_section));
4526 }
252b5132 4527
e9ee469a 4528 newdot = os->region->current;
94b50910 4529 align = os->bfd_section->alignment_power;
252b5132 4530 }
94b50910
AM
4531 else
4532 align = os->section_alignment;
5f992e62 4533
7270c5ed 4534 /* Align to what the section needs. */
94b50910
AM
4535 if (align > 0)
4536 {
4537 bfd_vma savedot = newdot;
4538 newdot = align_power (newdot, align);
252b5132 4539
94b50910
AM
4540 if (newdot != savedot
4541 && (config.warn_section_align
4542 || os->addr_tree != NULL)
4543 && expld.phase != lang_mark_phase_enum)
4544 einfo (_("%P: warning: changing start of section"
4545 " %s by %lu bytes\n"),
4546 os->name, (unsigned long) (newdot - savedot));
4547 }
252b5132 4548
e9ee469a 4549 bfd_set_section_vma (0, os->bfd_section, newdot);
5f992e62 4550
252b5132
RH
4551 os->bfd_section->output_offset = 0;
4552 }
4553
2d20f7bf 4554 lang_size_sections_1 (os->children.head, os, &os->children.head,
e9ee469a
AM
4555 os->fill, newdot, relax, check_regions);
4556
cde9e0be 4557 os->processed_vma = TRUE;
e9ee469a
AM
4558
4559 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
2f475487
AM
4560 /* Except for some special linker created sections,
4561 no output section should change from zero size
4562 after strip_excluded_output_sections. A non-zero
4563 size on an ignored section indicates that some
4564 input section was not sized early enough. */
4565 ASSERT (os->bfd_section->size == 0);
cde9e0be 4566 else
e9ee469a 4567 {
cde9e0be
AM
4568 dot = os->bfd_section->vma;
4569
4570 /* Put the section within the requested block size, or
4571 align at the block boundary. */
4572 after = ((dot
4573 + TO_ADDR (os->bfd_section->size)
4574 + os->block_value - 1)
4575 & - (bfd_vma) os->block_value);
4576
4577 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4578 }
4579
4580 /* Set section lma. */
4581 r = os->region;
4582 if (r == NULL)
4583 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4584
4585 if (os->load_base)
4586 {
4587 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4588 os->bfd_section->lma = lma;
e9ee469a 4589 }
cde9e0be
AM
4590 else if (os->region != NULL
4591 && os->lma_region != NULL
4592 && os->lma_region != os->region)
4593 {
4594 bfd_vma lma = os->lma_region->current;
e9ee469a 4595
cde9e0be
AM
4596 if (os->section_alignment != -1)
4597 lma = align_power (lma, os->section_alignment);
4598 os->bfd_section->lma = lma;
4599 }
dc0b6aa0
AM
4600 else if (r->last_os != NULL
4601 && (os->bfd_section->flags & SEC_ALLOC) != 0)
cde9e0be
AM
4602 {
4603 bfd_vma lma;
4604 asection *last;
4605
4606 last = r->last_os->output_section_statement.bfd_section;
dc0b6aa0
AM
4607
4608 /* A backwards move of dot should be accompanied by
4609 an explicit assignment to the section LMA (ie.
91eb6c46 4610 os->load_base set) because backwards moves can
dc0b6aa0 4611 create overlapping LMAs. */
264b6205 4612 if (dot < last->vma
91eb6c46
AM
4613 && os->bfd_section->size != 0
4614 && dot + os->bfd_section->size <= last->vma)
dc0b6aa0 4615 {
dc0b6aa0
AM
4616 /* If dot moved backwards then leave lma equal to
4617 vma. This is the old default lma, which might
4618 just happen to work when the backwards move is
91eb6c46
AM
4619 sufficiently large. Nag if this changes anything,
4620 so people can fix their linker scripts. */
4621
4622 if (last->vma != last->lma)
4623 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4624 os->name);
dc0b6aa0
AM
4625 }
4626 else
cde9e0be 4627 {
152d792f
AM
4628 /* If this is an overlay, set the current lma to that
4629 at the end of the previous section. */
4630 if (os->sectype == overlay_section)
cde9e0be
AM
4631 lma = last->lma + last->size;
4632
4633 /* Otherwise, keep the same lma to vma relationship
4634 as the previous section. */
4635 else
4636 lma = dot + last->lma - last->vma;
4637
4638 if (os->section_alignment != -1)
4639 lma = align_power (lma, os->section_alignment);
4640 os->bfd_section->lma = lma;
4641 }
4642 }
4643 os->processed_lma = TRUE;
5f992e62 4644
cde9e0be
AM
4645 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4646 break;
252b5132 4647
cde9e0be
AM
4648 /* Keep track of normal sections using the default
4649 lma region. We use this to set the lma for
4650 following sections. Overlays or other linker
4651 script assignment to lma might mean that the
dc0b6aa0
AM
4652 default lma == vma is incorrect.
4653 To avoid warnings about dot moving backwards when using
4654 -Ttext, don't start tracking sections until we find one
4655 of non-zero size or with lma set differently to vma. */
cde9e0be
AM
4656 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4657 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
dc0b6aa0
AM
4658 && (os->bfd_section->flags & SEC_ALLOC) != 0
4659 && (os->bfd_section->size != 0
264b6205
AM
4660 || (r->last_os == NULL
4661 && os->bfd_section->vma != os->bfd_section->lma)
4662 || (r->last_os != NULL
4663 && dot >= (r->last_os->output_section_statement
ea0c3cd5 4664 .bfd_section->vma)))
cde9e0be
AM
4665 && os->lma_region == NULL
4666 && !link_info.relocatable)
4667 r->last_os = s;
9f88b410 4668
e5caec89
NS
4669 /* .tbss sections effectively have zero size. */
4670 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4671 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4672 || link_info.relocatable)
eea6121a 4673 dot += TO_ADDR (os->bfd_section->size);
e5caec89 4674
9f88b410 4675 if (os->update_dot_tree != 0)
e9ee469a 4676 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
9f88b410 4677
252b5132
RH
4678 /* Update dot in the region ?
4679 We only do this if the section is going to be allocated,
4680 since unallocated sections do not contribute to the region's
13392b77 4681 overall size in memory.
5f992e62 4682
cce4c4c5
NC
4683 If the SEC_NEVER_LOAD bit is not set, it will affect the
4684 addresses of sections after it. We have to update
4685 dot. */
1579bae1 4686 if (os->region != NULL
6feb9908
AM
4687 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4688 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
252b5132
RH
4689 {
4690 os->region->current = dot;
5f992e62 4691
cf888e70
NC
4692 if (check_regions)
4693 /* Make sure the new address is within the region. */
4694 os_region_check (os, os->region, os->addr_tree,
4695 os->bfd_section->vma);
08da4cac 4696
ee3cc2e2 4697 if (os->lma_region != NULL && os->lma_region != os->region)
08da4cac 4698 {
cde9e0be
AM
4699 os->lma_region->current
4700 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
66e28d60 4701
cf888e70
NC
4702 if (check_regions)
4703 os_region_check (os, os->lma_region, NULL,
cde9e0be 4704 os->bfd_section->lma);
08da4cac 4705 }
252b5132
RH
4706 }
4707 }
4708 break;
4709
4710 case lang_constructors_statement_enum:
2d20f7bf
JJ
4711 dot = lang_size_sections_1 (constructor_list.head,
4712 output_section_statement,
4713 &s->wild_statement.children.head,
cf888e70 4714 fill, dot, relax, check_regions);
252b5132
RH
4715 break;
4716
4717 case lang_data_statement_enum:
4718 {
4719 unsigned int size = 0;
4720
7fabd029 4721 s->data_statement.output_offset =
08da4cac 4722 dot - output_section_statement->bfd_section->vma;
252b5132
RH
4723 s->data_statement.output_section =
4724 output_section_statement->bfd_section;
4725
1b493742
NS
4726 /* We might refer to provided symbols in the expression, and
4727 need to mark them as needed. */
e9ee469a 4728 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
1b493742 4729
252b5132
RH
4730 switch (s->data_statement.type)
4731 {
08da4cac
KH
4732 default:
4733 abort ();
252b5132
RH
4734 case QUAD:
4735 case SQUAD:
4736 size = QUAD_SIZE;
4737 break;
4738 case LONG:
4739 size = LONG_SIZE;
4740 break;
4741 case SHORT:
4742 size = SHORT_SIZE;
4743 break;
4744 case BYTE:
4745 size = BYTE_SIZE;
4746 break;
4747 }
e5caa5e0
AM
4748 if (size < TO_SIZE ((unsigned) 1))
4749 size = TO_SIZE ((unsigned) 1);
4750 dot += TO_ADDR (size);
eea6121a 4751 output_section_statement->bfd_section->size += size;
252b5132
RH
4752 }
4753 break;
4754
4755 case lang_reloc_statement_enum:
4756 {
4757 int size;
4758
7fabd029 4759 s->reloc_statement.output_offset =
252b5132
RH
4760 dot - output_section_statement->bfd_section->vma;
4761 s->reloc_statement.output_section =
4762 output_section_statement->bfd_section;
4763 size = bfd_get_reloc_size (s->reloc_statement.howto);
e5caa5e0 4764 dot += TO_ADDR (size);
eea6121a 4765 output_section_statement->bfd_section->size += size;
252b5132
RH
4766 }
4767 break;
5f992e62 4768
252b5132 4769 case lang_wild_statement_enum:
2d20f7bf
JJ
4770 dot = lang_size_sections_1 (s->wild_statement.children.head,
4771 output_section_statement,
4772 &s->wild_statement.children.head,
cf888e70 4773 fill, dot, relax, check_regions);
252b5132
RH
4774 break;
4775
4776 case lang_object_symbols_statement_enum:
4777 link_info.create_object_symbols_section =
4778 output_section_statement->bfd_section;
4779 break;
e9ee469a 4780
252b5132
RH
4781 case lang_output_statement_enum:
4782 case lang_target_statement_enum:
4783 break;
e9ee469a 4784
252b5132
RH
4785 case lang_input_section_enum:
4786 {
4787 asection *i;
4788
4789 i = (*prev)->input_section.section;
eea6121a 4790 if (relax)
252b5132 4791 {
b34976b6 4792 bfd_boolean again;
252b5132
RH
4793
4794 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4795 einfo (_("%P%F: can't relax section: %E\n"));
4796 if (again)
b34976b6 4797 *relax = TRUE;
252b5132 4798 }
b3327aad
AM
4799 dot = size_input_section (prev, output_section_statement,
4800 output_section_statement->fill, dot);
252b5132
RH
4801 }
4802 break;
e9ee469a 4803
252b5132
RH
4804 case lang_input_statement_enum:
4805 break;
e9ee469a 4806
252b5132 4807 case lang_fill_statement_enum:
08da4cac
KH
4808 s->fill_statement.output_section =
4809 output_section_statement->bfd_section;
252b5132
RH
4810
4811 fill = s->fill_statement.fill;
4812 break;
e9ee469a 4813
252b5132
RH
4814 case lang_assignment_statement_enum:
4815 {
4816 bfd_vma newdot = dot;
49c13adb 4817 etree_type *tree = s->assignment_statement.exp;
252b5132 4818
b10a8ae0
L
4819 expld.dataseg.relro = exp_dataseg_relro_none;
4820
49c13adb 4821 exp_fold_tree (tree,
408082ec 4822 output_section_statement->bfd_section,
252b5132
RH
4823 &newdot);
4824
b10a8ae0
L
4825 if (expld.dataseg.relro == exp_dataseg_relro_start)
4826 {
4827 if (!expld.dataseg.relro_start_stat)
4828 expld.dataseg.relro_start_stat = s;
4829 else
4830 {
4831 ASSERT (expld.dataseg.relro_start_stat == s);
4832 }
4833 }
4834 else if (expld.dataseg.relro == exp_dataseg_relro_end)
4835 {
4836 if (!expld.dataseg.relro_end_stat)
4837 expld.dataseg.relro_end_stat = s;
4838 else
4839 {
4840 ASSERT (expld.dataseg.relro_end_stat == s);
4841 }
4842 }
4843 expld.dataseg.relro = exp_dataseg_relro_none;
4844
49c13adb 4845 /* This symbol is relative to this section. */
a14a5de3 4846 if ((tree->type.node_class == etree_provided
49c13adb
L
4847 || tree->type.node_class == etree_assign)
4848 && (tree->assign.dst [0] != '.'
4849 || tree->assign.dst [1] != '\0'))
4850 output_section_statement->section_relative_symbol = 1;
4851
85852e36 4852 if (!output_section_statement->ignored)
252b5132 4853 {
252b5132
RH
4854 if (output_section_statement == abs_output_section)
4855 {
4856 /* If we don't have an output section, then just adjust
4857 the default memory address. */
6feb9908
AM
4858 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4859 FALSE)->current = newdot;
252b5132 4860 }
85852e36 4861 else if (newdot != dot)
252b5132 4862 {
b3327aad
AM
4863 /* Insert a pad after this statement. We can't
4864 put the pad before when relaxing, in case the
4865 assignment references dot. */
e5caa5e0 4866 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
b3327aad
AM
4867 output_section_statement->bfd_section, dot);
4868
4869 /* Don't neuter the pad below when relaxing. */
4870 s = s->header.next;
9dfc8ab2 4871
e9ee469a
AM
4872 /* If dot is advanced, this implies that the section
4873 should have space allocated to it, unless the
4874 user has explicitly stated that the section
4875 should never be loaded. */
14ac84b4 4876 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
e9ee469a
AM
4877 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4878 }
252b5132
RH
4879 dot = newdot;
4880 }
4881 }
4882 break;
4883
4884 case lang_padding_statement_enum:
c0c330a7
AM
4885 /* If this is the first time lang_size_sections is called,
4886 we won't have any padding statements. If this is the
4887 second or later passes when relaxing, we should allow
4888 padding to shrink. If padding is needed on this pass, it
4889 will be added back in. */
4890 s->padding_statement.size = 0;
6e814ff8
AM
4891
4892 /* Make sure output_offset is valid. If relaxation shrinks
4893 the section and this pad isn't needed, it's possible to
4894 have output_offset larger than the final size of the
4895 section. bfd_set_section_contents will complain even for
4896 a pad size of zero. */
4897 s->padding_statement.output_offset
4898 = dot - output_section_statement->bfd_section->vma;
252b5132
RH
4899 break;
4900
4901 case lang_group_statement_enum:
2d20f7bf
JJ
4902 dot = lang_size_sections_1 (s->group_statement.children.head,
4903 output_section_statement,
4904 &s->group_statement.children.head,
cf888e70 4905 fill, dot, relax, check_regions);
252b5132
RH
4906 break;
4907
53d25da6 4908 case lang_insert_statement_enum:
252b5132
RH
4909 break;
4910
c0c330a7 4911 /* We can only get here when relaxing is turned on. */
252b5132
RH
4912 case lang_address_statement_enum:
4913 break;
53d25da6
AM
4914
4915 default:
4916 FAIL ();
4917 break;
252b5132
RH
4918 }
4919 prev = &s->header.next;
4920 }
4921 return dot;
4922}
4923
591a748a
NC
4924/* Callback routine that is used in _bfd_elf_map_sections_to_segments.
4925 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
4926 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
4927 segments. We are allowed an opportunity to override this decision. */
2889e75b
NC
4928
4929bfd_boolean
4930ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
4931 bfd * abfd ATTRIBUTE_UNUSED,
4932 asection * current_section,
4933 asection * previous_section,
4934 bfd_boolean new_segment)
4935{
4936 lang_output_section_statement_type * cur;
4937 lang_output_section_statement_type * prev;
591a748a
NC
4938
4939 /* The checks below are only necessary when the BFD library has decided
4940 that the two sections ought to be placed into the same segment. */
2889e75b
NC
4941 if (new_segment)
4942 return TRUE;
4943
4944 /* Paranoia checks. */
4945 if (current_section == NULL || previous_section == NULL)
4946 return new_segment;
4947
4948 /* Find the memory regions associated with the two sections.
4949 We call lang_output_section_find() here rather than scanning the list
4950 of output sections looking for a matching section pointer because if
591a748a 4951 we have a large number of sections then a hash lookup is faster. */
2889e75b
NC
4952 cur = lang_output_section_find (current_section->name);
4953 prev = lang_output_section_find (previous_section->name);
4954
591a748a 4955 /* More paranoia. */
2889e75b
NC
4956 if (cur == NULL || prev == NULL)
4957 return new_segment;
4958
4959 /* If the regions are different then force the sections to live in
591a748a
NC
4960 different segments. See the email thread starting at the following
4961 URL for the reasons why this is necessary:
2889e75b
NC
4962 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
4963 return cur->region != prev->region;
4964}
4965
e9ee469a
AM
4966void
4967one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
2d20f7bf 4968{
420e579c 4969 lang_statement_iteration++;
e9ee469a
AM
4970 lang_size_sections_1 (statement_list.head, abs_output_section,
4971 &statement_list.head, 0, 0, relax, check_regions);
4972}
420e579c 4973
e9ee469a
AM
4974void
4975lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4976{
4977 expld.phase = lang_allocating_phase_enum;
4978 expld.dataseg.phase = exp_dataseg_none;
4979
4980 one_lang_size_sections_pass (relax, check_regions);
4981 if (expld.dataseg.phase == exp_dataseg_end_seen
4982 && link_info.relro && expld.dataseg.relro_end)
8c37241b
JJ
4983 {
4984 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
e9ee469a 4985 to put expld.dataseg.relro on a (common) page boundary. */
e3070fef 4986 bfd_vma old_min_base, relro_end, maxpage;
8c37241b 4987
e9ee469a
AM
4988 expld.dataseg.phase = exp_dataseg_relro_adjust;
4989 old_min_base = expld.dataseg.min_base;
4990 maxpage = expld.dataseg.maxpagesize;
4991 expld.dataseg.base += (-expld.dataseg.relro_end
4992 & (expld.dataseg.pagesize - 1));
a4f5ad88 4993 /* Compute the expected PT_GNU_RELRO segment end. */
e9ee469a
AM
4994 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
4995 & ~(expld.dataseg.pagesize - 1);
4996 if (old_min_base + maxpage < expld.dataseg.base)
e3070fef 4997 {
e9ee469a 4998 expld.dataseg.base -= maxpage;
e3070fef
JJ
4999 relro_end -= maxpage;
5000 }
f86a8756 5001 lang_reset_memory_regions ();
e9ee469a
AM
5002 one_lang_size_sections_pass (relax, check_regions);
5003 if (expld.dataseg.relro_end > relro_end)
a4f5ad88
JJ
5004 {
5005 /* The alignment of sections between DATA_SEGMENT_ALIGN
5006 and DATA_SEGMENT_RELRO_END caused huge padding to be
5007 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
5008 asection *sec;
5009 unsigned int max_alignment_power = 0;
5010
5011 /* Find maximum alignment power of sections between
5012 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5013 for (sec = output_bfd->sections; sec; sec = sec->next)
e9ee469a
AM
5014 if (sec->vma >= expld.dataseg.base
5015 && sec->vma < expld.dataseg.relro_end
a4f5ad88
JJ
5016 && sec->alignment_power > max_alignment_power)
5017 max_alignment_power = sec->alignment_power;
5018
e9ee469a 5019 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
a4f5ad88 5020 {
e9ee469a 5021 if (expld.dataseg.base - (1 << max_alignment_power)
e3070fef 5022 < old_min_base)
e9ee469a
AM
5023 expld.dataseg.base += expld.dataseg.pagesize;
5024 expld.dataseg.base -= (1 << max_alignment_power);
f86a8756 5025 lang_reset_memory_regions ();
e9ee469a 5026 one_lang_size_sections_pass (relax, check_regions);
a4f5ad88
JJ
5027 }
5028 }
e9ee469a
AM
5029 link_info.relro_start = expld.dataseg.base;
5030 link_info.relro_end = expld.dataseg.relro_end;
8c37241b 5031 }
e9ee469a 5032 else if (expld.dataseg.phase == exp_dataseg_end_seen)
2d20f7bf
JJ
5033 {
5034 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5035 a page could be saved in the data segment. */
5036 bfd_vma first, last;
5037
e9ee469a
AM
5038 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5039 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
2d20f7bf 5040 if (first && last
e9ee469a
AM
5041 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5042 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5043 && first + last <= expld.dataseg.pagesize)
2d20f7bf 5044 {
e9ee469a 5045 expld.dataseg.phase = exp_dataseg_adjust;
f86a8756 5046 lang_reset_memory_regions ();
e9ee469a 5047 one_lang_size_sections_pass (relax, check_regions);
2d20f7bf
JJ
5048 }
5049 }
5050
e9ee469a 5051 expld.phase = lang_final_phase_enum;
2d20f7bf
JJ
5052}
5053
420e579c
HPN
5054/* Worker function for lang_do_assignments. Recursiveness goes here. */
5055
5056static bfd_vma
66e28d60
AM
5057lang_do_assignments_1 (lang_statement_union_type *s,
5058 lang_output_section_statement_type *current_os,
5059 fill_type *fill,
5060 bfd_vma dot)
252b5132 5061{
1579bae1 5062 for (; s != NULL; s = s->header.next)
252b5132
RH
5063 {
5064 switch (s->header.type)
5065 {
5066 case lang_constructors_statement_enum:
420e579c 5067 dot = lang_do_assignments_1 (constructor_list.head,
66e28d60 5068 current_os, fill, dot);
252b5132
RH
5069 break;
5070
5071 case lang_output_section_statement_enum:
5072 {
d1778b88 5073 lang_output_section_statement_type *os;
252b5132 5074
d1778b88 5075 os = &(s->output_section_statement);
75ff4589 5076 if (os->bfd_section != NULL && !os->ignored)
252b5132
RH
5077 {
5078 dot = os->bfd_section->vma;
66e28d60 5079
cde9e0be 5080 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
66e28d60 5081
3737f867
JJ
5082 /* .tbss sections effectively have zero size. */
5083 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5084 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5085 || link_info.relocatable)
cde9e0be 5086 dot += TO_ADDR (os->bfd_section->size);
252b5132
RH
5087 }
5088 }
5089 break;
e9ee469a 5090
252b5132
RH
5091 case lang_wild_statement_enum:
5092
420e579c 5093 dot = lang_do_assignments_1 (s->wild_statement.children.head,
66e28d60 5094 current_os, fill, dot);
252b5132
RH
5095 break;
5096
5097 case lang_object_symbols_statement_enum:
5098 case lang_output_statement_enum:
5099 case lang_target_statement_enum:
252b5132 5100 break;
e9ee469a 5101
252b5132 5102 case lang_data_statement_enum:
e9ee469a
AM
5103 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5104 if (expld.result.valid_p)
5105 s->data_statement.value = (expld.result.value
5106 + expld.result.section->vma);
5107 else
5108 einfo (_("%F%P: invalid data statement\n"));
b7a26f91
KH
5109 {
5110 unsigned int size;
08da4cac
KH
5111 switch (s->data_statement.type)
5112 {
5113 default:
5114 abort ();
5115 case QUAD:
5116 case SQUAD:
5117 size = QUAD_SIZE;
5118 break;
5119 case LONG:
5120 size = LONG_SIZE;
5121 break;
5122 case SHORT:
5123 size = SHORT_SIZE;
5124 break;
5125 case BYTE:
5126 size = BYTE_SIZE;
5127 break;
5128 }
e5caa5e0
AM
5129 if (size < TO_SIZE ((unsigned) 1))
5130 size = TO_SIZE ((unsigned) 1);
5131 dot += TO_ADDR (size);
08da4cac 5132 }
252b5132
RH
5133 break;
5134
5135 case lang_reloc_statement_enum:
e9ee469a
AM
5136 exp_fold_tree (s->reloc_statement.addend_exp,
5137 bfd_abs_section_ptr, &dot);
5138 if (expld.result.valid_p)
5139 s->reloc_statement.addend_value = expld.result.value;
5140 else
5141 einfo (_("%F%P: invalid reloc statement\n"));
e5caa5e0 5142 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
252b5132
RH
5143 break;
5144
5145 case lang_input_section_enum:
5146 {
5147 asection *in = s->input_section.section;
5148
57ceae94 5149 if ((in->flags & SEC_EXCLUDE) == 0)
eea6121a 5150 dot += TO_ADDR (in->size);
252b5132
RH
5151 }
5152 break;
5153
5154 case lang_input_statement_enum:
5155 break;
e9ee469a 5156
252b5132
RH
5157 case lang_fill_statement_enum:
5158 fill = s->fill_statement.fill;
5159 break;
252b5132 5160
e9ee469a
AM
5161 case lang_assignment_statement_enum:
5162 exp_fold_tree (s->assignment_statement.exp,
66e28d60 5163 current_os->bfd_section,
e9ee469a 5164 &dot);
252b5132 5165 break;
e9ee469a 5166
252b5132 5167 case lang_padding_statement_enum:
e5caa5e0 5168 dot += TO_ADDR (s->padding_statement.size);
252b5132
RH
5169 break;
5170
5171 case lang_group_statement_enum:
420e579c 5172 dot = lang_do_assignments_1 (s->group_statement.children.head,
66e28d60 5173 current_os, fill, dot);
252b5132
RH
5174 break;
5175
53d25da6 5176 case lang_insert_statement_enum:
252b5132 5177 break;
e9ee469a 5178
252b5132
RH
5179 case lang_address_statement_enum:
5180 break;
53d25da6
AM
5181
5182 default:
5183 FAIL ();
5184 break;
252b5132 5185 }
252b5132
RH
5186 }
5187 return dot;
5188}
5189
f2241121 5190void
e9ee469a 5191lang_do_assignments (void)
420e579c 5192{
420e579c 5193 lang_statement_iteration++;
e9ee469a 5194 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
420e579c
HPN
5195}
5196
252b5132
RH
5197/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5198 operator .startof. (section_name), it produces an undefined symbol
5199 .startof.section_name. Similarly, when it sees
5200 .sizeof. (section_name), it produces an undefined symbol
5201 .sizeof.section_name. For all the output sections, we look for
5202 such symbols, and set them to the correct value. */
5203
5204static void
1579bae1 5205lang_set_startof (void)
252b5132
RH
5206{
5207 asection *s;
5208
1049f94e 5209 if (link_info.relocatable)
252b5132
RH
5210 return;
5211
5212 for (s = output_bfd->sections; s != NULL; s = s->next)
5213 {
5214 const char *secname;
5215 char *buf;
5216 struct bfd_link_hash_entry *h;
5217
5218 secname = bfd_get_section_name (output_bfd, s);
5219 buf = xmalloc (10 + strlen (secname));
5220
5221 sprintf (buf, ".startof.%s", secname);
b34976b6 5222 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
5223 if (h != NULL && h->type == bfd_link_hash_undefined)
5224 {
5225 h->type = bfd_link_hash_defined;
5226 h->u.def.value = bfd_get_section_vma (output_bfd, s);
5227 h->u.def.section = bfd_abs_section_ptr;
5228 }
5229
5230 sprintf (buf, ".sizeof.%s", secname);
b34976b6 5231 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
5232 if (h != NULL && h->type == bfd_link_hash_undefined)
5233 {
5234 h->type = bfd_link_hash_defined;
eea6121a 5235 h->u.def.value = TO_ADDR (s->size);
252b5132
RH
5236 h->u.def.section = bfd_abs_section_ptr;
5237 }
5238
5239 free (buf);
5240 }
5241}
5242
5243static void
750877ba 5244lang_end (void)
252b5132
RH
5245{
5246 struct bfd_link_hash_entry *h;
b34976b6 5247 bfd_boolean warn;
252b5132 5248
ac69cbc6
TG
5249 if ((link_info.relocatable && !link_info.gc_sections)
5250 || link_info.shared)
71934f94 5251 warn = entry_from_cmdline;
252b5132 5252 else
b34976b6 5253 warn = TRUE;
252b5132 5254
ac69cbc6
TG
5255 /* Force the user to specify a root when generating a relocatable with
5256 --gc-sections. */
5257 if (link_info.gc_sections && link_info.relocatable
5258 && (entry_symbol.name == NULL
5259 && ldlang_undef_chain_list_head == NULL))
5260 einfo (_("%P%F: gc-sections requires either an entry or "
5261 "an undefined symbol\n"));
5262
1579bae1 5263 if (entry_symbol.name == NULL)
252b5132 5264 {
a359509e
ZW
5265 /* No entry has been specified. Look for the default entry, but
5266 don't warn if we don't find it. */
5267 entry_symbol.name = entry_symbol_default;
b34976b6 5268 warn = FALSE;
252b5132
RH
5269 }
5270
e3e942e9 5271 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
b34976b6 5272 FALSE, FALSE, TRUE);
1579bae1 5273 if (h != NULL
252b5132
RH
5274 && (h->type == bfd_link_hash_defined
5275 || h->type == bfd_link_hash_defweak)
5276 && h->u.def.section->output_section != NULL)
5277 {
5278 bfd_vma val;
5279
5280 val = (h->u.def.value
5281 + bfd_get_section_vma (output_bfd,
5282 h->u.def.section->output_section)
5283 + h->u.def.section->output_offset);
5284 if (! bfd_set_start_address (output_bfd, val))
e3e942e9 5285 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
252b5132
RH
5286 }
5287 else
5288 {
5289 bfd_vma val;
5f992e62 5290 const char *send;
252b5132
RH
5291
5292 /* We couldn't find the entry symbol. Try parsing it as a
afd7a018 5293 number. */
e3e942e9 5294 val = bfd_scan_vma (entry_symbol.name, &send, 0);
252b5132
RH
5295 if (*send == '\0')
5296 {
5297 if (! bfd_set_start_address (output_bfd, val))
5298 einfo (_("%P%F: can't set start address\n"));
5299 }
5300 else
5301 {
5302 asection *ts;
5303
5304 /* Can't find the entry symbol, and it's not a number. Use
5305 the first address in the text section. */
1e281515 5306 ts = bfd_get_section_by_name (output_bfd, entry_section);
1579bae1 5307 if (ts != NULL)
252b5132
RH
5308 {
5309 if (warn)
6feb9908
AM
5310 einfo (_("%P: warning: cannot find entry symbol %s;"
5311 " defaulting to %V\n"),
e3e942e9
AM
5312 entry_symbol.name,
5313 bfd_get_section_vma (output_bfd, ts));
252b5132
RH
5314 if (! bfd_set_start_address (output_bfd,
5315 bfd_get_section_vma (output_bfd,
5316 ts)))
5317 einfo (_("%P%F: can't set start address\n"));
5318 }
5319 else
5320 {
5321 if (warn)
6feb9908
AM
5322 einfo (_("%P: warning: cannot find entry symbol %s;"
5323 " not setting start address\n"),
e3e942e9 5324 entry_symbol.name);
252b5132
RH
5325 }
5326 }
5327 }
420e579c 5328
7115639b
AM
5329 /* Don't bfd_hash_table_free (&lang_definedness_table);
5330 map file output may result in a call of lang_track_definedness. */
252b5132
RH
5331}
5332
5333/* This is a small function used when we want to ignore errors from
5334 BFD. */
5335
5336static void
87f2a346 5337ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
252b5132
RH
5338{
5339 /* Don't do anything. */
5340}
5341
5342/* Check that the architecture of all the input files is compatible
5343 with the output file. Also call the backend to let it do any
5344 other checking that is needed. */
5345
5346static void
1579bae1 5347lang_check (void)
252b5132
RH
5348{
5349 lang_statement_union_type *file;
5350 bfd *input_bfd;
5f992e62 5351 const bfd_arch_info_type *compatible;
252b5132 5352
1579bae1 5353 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
252b5132
RH
5354 {
5355 input_bfd = file->input_statement.the_bfd;
6feb9908
AM
5356 compatible
5357 = bfd_arch_get_compatible (input_bfd, output_bfd,
5358 command_line.accept_unknown_input_arch);
30cba025
AM
5359
5360 /* In general it is not possible to perform a relocatable
5361 link between differing object formats when the input
5362 file has relocations, because the relocations in the
5363 input format may not have equivalent representations in
5364 the output format (and besides BFD does not translate
5365 relocs for other link purposes than a final link). */
1049f94e 5366 if ((link_info.relocatable || link_info.emitrelocations)
30cba025 5367 && (compatible == NULL
d35a52e2 5368 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
30cba025
AM
5369 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5370 {
6feb9908
AM
5371 einfo (_("%P%F: Relocatable linking with relocations from"
5372 " format %s (%B) to format %s (%B) is not supported\n"),
30cba025
AM
5373 bfd_get_target (input_bfd), input_bfd,
5374 bfd_get_target (output_bfd), output_bfd);
5375 /* einfo with %F exits. */
5376 }
5377
252b5132
RH
5378 if (compatible == NULL)
5379 {
5380 if (command_line.warn_mismatch)
9e7750f2 5381 einfo (_("%P%X: %s architecture of input file `%B'"
6feb9908 5382 " is incompatible with %s output\n"),
252b5132
RH
5383 bfd_printable_name (input_bfd), input_bfd,
5384 bfd_printable_name (output_bfd));
5385 }
b9247304 5386 else if (bfd_count_sections (input_bfd))
252b5132 5387 {
b9247304 5388 /* If the input bfd has no contents, it shouldn't set the
b7a26f91 5389 private data of the output bfd. */
b9247304 5390
252b5132
RH
5391 bfd_error_handler_type pfn = NULL;
5392
5393 /* If we aren't supposed to warn about mismatched input
afd7a018
AM
5394 files, temporarily set the BFD error handler to a
5395 function which will do nothing. We still want to call
5396 bfd_merge_private_bfd_data, since it may set up
5397 information which is needed in the output file. */
252b5132
RH
5398 if (! command_line.warn_mismatch)
5399 pfn = bfd_set_error_handler (ignore_bfd_errors);
5400 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
5401 {
5402 if (command_line.warn_mismatch)
6feb9908
AM
5403 einfo (_("%P%X: failed to merge target specific data"
5404 " of file %B\n"), input_bfd);
252b5132
RH
5405 }
5406 if (! command_line.warn_mismatch)
5407 bfd_set_error_handler (pfn);
5408 }
5409 }
5410}
5411
5412/* Look through all the global common symbols and attach them to the
5413 correct section. The -sort-common command line switch may be used
5414 to roughly sort the entries by size. */
5415
5416static void
1579bae1 5417lang_common (void)
252b5132 5418{
4818e05f
AM
5419 if (command_line.inhibit_common_definition)
5420 return;
1049f94e 5421 if (link_info.relocatable
252b5132
RH
5422 && ! command_line.force_common_definition)
5423 return;
5424
5425 if (! config.sort_common)
1579bae1 5426 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
252b5132
RH
5427 else
5428 {
5429 int power;
5430
5431 for (power = 4; power >= 0; power--)
1579bae1 5432 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
252b5132
RH
5433 }
5434}
5435
5436/* Place one common symbol in the correct section. */
5437
b34976b6 5438static bfd_boolean
1579bae1 5439lang_one_common (struct bfd_link_hash_entry *h, void *info)
252b5132
RH
5440{
5441 unsigned int power_of_two;
5442 bfd_vma size;
5443 asection *section;
5444
5445 if (h->type != bfd_link_hash_common)
b34976b6 5446 return TRUE;
252b5132
RH
5447
5448 size = h->u.c.size;
5449 power_of_two = h->u.c.p->alignment_power;
5450
5451 if (config.sort_common
5452 && power_of_two < (unsigned int) *(int *) info)
b34976b6 5453 return TRUE;
252b5132
RH
5454
5455 section = h->u.c.p->section;
5456
e5caa5e0 5457 /* Increase the size of the section to align the common sym. */
eea6121a
AM
5458 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5459 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
252b5132
RH
5460
5461 /* Adjust the alignment if necessary. */
5462 if (power_of_two > section->alignment_power)
5463 section->alignment_power = power_of_two;
5464
5465 /* Change the symbol from common to defined. */
5466 h->type = bfd_link_hash_defined;
5467 h->u.def.section = section;
eea6121a 5468 h->u.def.value = section->size;
252b5132
RH
5469
5470 /* Increase the size of the section. */
eea6121a 5471 section->size += size;
252b5132
RH
5472
5473 /* Make sure the section is allocated in memory, and make sure that
5474 it is no longer a common section. */
5475 section->flags |= SEC_ALLOC;
08da4cac 5476 section->flags &= ~SEC_IS_COMMON;
252b5132
RH
5477
5478 if (config.map_file != NULL)
5479 {
b34976b6 5480 static bfd_boolean header_printed;
252b5132
RH
5481 int len;
5482 char *name;
5483 char buf[50];
5484
5485 if (! header_printed)
5486 {
5487 minfo (_("\nAllocating common symbols\n"));
5488 minfo (_("Common symbol size file\n\n"));
b34976b6 5489 header_printed = TRUE;
252b5132
RH
5490 }
5491
73705ac3
AM
5492 name = bfd_demangle (output_bfd, h->root.string,
5493 DMGL_ANSI | DMGL_PARAMS);
d7d4c8de
AM
5494 if (name == NULL)
5495 {
5496 minfo ("%s", h->root.string);
5497 len = strlen (h->root.string);
5498 }
5499 else
5500 {
5501 minfo ("%s", name);
5502 len = strlen (name);
5503 free (name);
5504 }
252b5132
RH
5505
5506 if (len >= 19)
5507 {
5508 print_nl ();
5509 len = 0;
5510 }
5511 while (len < 20)
5512 {
5513 print_space ();
5514 ++len;
5515 }
5516
5517 minfo ("0x");
5518 if (size <= 0xffffffff)
5519 sprintf (buf, "%lx", (unsigned long) size);
5520 else
5521 sprintf_vma (buf, size);
5522 minfo ("%s", buf);
5523 len = strlen (buf);
5524
5525 while (len < 16)
5526 {
5527 print_space ();
5528 ++len;
5529 }
5530
5531 minfo ("%B\n", section->owner);
5532 }
5533
b34976b6 5534 return TRUE;
252b5132
RH
5535}
5536
08da4cac
KH
5537/* Run through the input files and ensure that every input section has
5538 somewhere to go. If one is found without a destination then create
5539 an input request and place it into the statement tree. */
252b5132
RH
5540
5541static void
1579bae1 5542lang_place_orphans (void)
252b5132 5543{
e50d8076 5544 LANG_FOR_EACH_INPUT_STATEMENT (file)
252b5132
RH
5545 {
5546 asection *s;
5547
1579bae1 5548 for (s = file->the_bfd->sections; s != NULL; s = s->next)
252b5132 5549 {
1579bae1 5550 if (s->output_section == NULL)
252b5132 5551 {
396a2467 5552 /* This section of the file is not attached, root
afd7a018 5553 around for a sensible place for it to go. */
252b5132
RH
5554
5555 if (file->just_syms_flag)
1449d79b
AM
5556 bfd_link_just_syms (file->the_bfd, s, &link_info);
5557 else if ((s->flags & SEC_EXCLUDE) != 0)
164e712d 5558 s->output_section = bfd_abs_section_ptr;
252b5132
RH
5559 else if (strcmp (s->name, "COMMON") == 0)
5560 {
5561 /* This is a lonely common section which must have
5562 come from an archive. We attach to the section
5563 with the wildcard. */
1049f94e 5564 if (! link_info.relocatable
252b5132
RH
5565 || command_line.force_common_definition)
5566 {
5567 if (default_common_section == NULL)
5568 {
252b5132
RH
5569 default_common_section =
5570 lang_output_section_statement_lookup (".bss");
5571
5572 }
39dcfe18 5573 lang_add_section (&default_common_section->children, s,
7b986e99 5574 default_common_section);
252b5132
RH
5575 }
5576 }
7b986e99 5577 else if (ldemul_place_orphan (s))
252b5132
RH
5578 ;
5579 else
5580 {
39dcfe18 5581 lang_output_section_statement_type *os;
252b5132 5582
39dcfe18 5583 os = lang_output_section_statement_lookup (s->name);
7b986e99 5584 lang_add_section (&os->children, s, os);
252b5132
RH
5585 }
5586 }
5587 }
5588 }
5589}
5590
252b5132 5591void
1579bae1 5592lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
252b5132 5593{
aa8804e4 5594 flagword *ptr_flags;
252b5132 5595
aa8804e4 5596 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
252b5132
RH
5597 while (*flags)
5598 {
5599 switch (*flags)
5600 {
252b5132
RH
5601 case 'A': case 'a':
5602 *ptr_flags |= SEC_ALLOC;
5603 break;
5604
5605 case 'R': case 'r':
5606 *ptr_flags |= SEC_READONLY;
5607 break;
5608
5609 case 'W': case 'w':
5610 *ptr_flags |= SEC_DATA;
5611 break;
5612
5613 case 'X': case 'x':
5614 *ptr_flags |= SEC_CODE;
5615 break;
5616
5617 case 'L': case 'l':
5618 case 'I': case 'i':
5619 *ptr_flags |= SEC_LOAD;
5620 break;
5621
5622 default:
5623 einfo (_("%P%F: invalid syntax in flags\n"));
5624 break;
5625 }
5626 flags++;
5627 }
5628}
5629
5630/* Call a function on each input file. This function will be called
5631 on an archive, but not on the elements. */
5632
5633void
1579bae1 5634lang_for_each_input_file (void (*func) (lang_input_statement_type *))
252b5132
RH
5635{
5636 lang_input_statement_type *f;
5637
5638 for (f = (lang_input_statement_type *) input_file_chain.head;
5639 f != NULL;
5640 f = (lang_input_statement_type *) f->next_real_file)
5641 func (f);
5642}
5643
5644/* Call a function on each file. The function will be called on all
5645 the elements of an archive which are included in the link, but will
5646 not be called on the archive file itself. */
5647
5648void
1579bae1 5649lang_for_each_file (void (*func) (lang_input_statement_type *))
252b5132 5650{
e50d8076 5651 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132
RH
5652 {
5653 func (f);
5654 }
5655}
5656
252b5132 5657void
1579bae1 5658ldlang_add_file (lang_input_statement_type *entry)
252b5132 5659{
252b5132
RH
5660 lang_statement_append (&file_chain,
5661 (lang_statement_union_type *) entry,
5662 &entry->next);
5663
5664 /* The BFD linker needs to have a list of all input BFDs involved in
5665 a link. */
1579bae1 5666 ASSERT (entry->the_bfd->link_next == NULL);
252b5132 5667 ASSERT (entry->the_bfd != output_bfd);
a9a4c53e
AM
5668
5669 *link_info.input_bfds_tail = entry->the_bfd;
5670 link_info.input_bfds_tail = &entry->the_bfd->link_next;
1579bae1 5671 entry->the_bfd->usrdata = entry;
252b5132
RH
5672 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5673
5674 /* Look through the sections and check for any which should not be
5675 included in the link. We need to do this now, so that we can
5676 notice when the backend linker tries to report multiple
5677 definition errors for symbols which are in sections we aren't
5678 going to link. FIXME: It might be better to entirely ignore
5679 symbols which are defined in sections which are going to be
5680 discarded. This would require modifying the backend linker for
5681 each backend which might set the SEC_LINK_ONCE flag. If we do
5682 this, we should probably handle SEC_EXCLUDE in the same way. */
5683
1579bae1 5684 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
252b5132
RH
5685}
5686
5687void
1579bae1 5688lang_add_output (const char *name, int from_script)
252b5132
RH
5689{
5690 /* Make -o on command line override OUTPUT in script. */
7c519c12 5691 if (!had_output_filename || !from_script)
252b5132
RH
5692 {
5693 output_filename = name;
b34976b6 5694 had_output_filename = TRUE;
252b5132
RH
5695 }
5696}
5697
252b5132
RH
5698static lang_output_section_statement_type *current_section;
5699
5700static int
1579bae1 5701topower (int x)
252b5132
RH
5702{
5703 unsigned int i = 1;
5704 int l;
5705
5706 if (x < 0)
5707 return -1;
5708
5f992e62 5709 for (l = 0; l < 32; l++)
252b5132
RH
5710 {
5711 if (i >= (unsigned int) x)
5712 return l;
5713 i <<= 1;
5714 }
5715
5716 return 0;
5717}
5718
aea4bd9d 5719lang_output_section_statement_type *
1579bae1
AM
5720lang_enter_output_section_statement (const char *output_section_statement_name,
5721 etree_type *address_exp,
5722 enum section_type sectype,
1579bae1
AM
5723 etree_type *align,
5724 etree_type *subalign,
0841712e
JJ
5725 etree_type *ebase,
5726 int constraint)
252b5132
RH
5727{
5728 lang_output_section_statement_type *os;
5729
bd4d42c1
AM
5730 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5731 constraint);
5732 current_section = os;
252b5132 5733
08da4cac 5734 /* Make next things chain into subchain of this. */
252b5132 5735
1579bae1 5736 if (os->addr_tree == NULL)
08da4cac
KH
5737 {
5738 os->addr_tree = address_exp;
5739 }
252b5132
RH
5740 os->sectype = sectype;
5741 if (sectype != noload_section)
5742 os->flags = SEC_NO_FLAGS;
5743 else
5744 os->flags = SEC_NEVER_LOAD;
e5caa5e0 5745 os->block_value = 1;
252b5132
RH
5746 stat_ptr = &os->children;
5747
08da4cac 5748 os->subsection_alignment =
e9ee469a 5749 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
08da4cac 5750 os->section_alignment =
e9ee469a 5751 topower (exp_get_value_int (align, -1, "section alignment"));
252b5132
RH
5752
5753 os->load_base = ebase;
aea4bd9d 5754 return os;
252b5132
RH
5755}
5756
252b5132 5757void
1579bae1 5758lang_final (void)
252b5132 5759{
bd4d42c1 5760 lang_output_statement_type *new;
252b5132 5761
bd4d42c1 5762 new = new_stat (lang_output_statement, stat_ptr);
252b5132
RH
5763 new->name = output_filename;
5764}
5765
08da4cac
KH
5766/* Reset the current counters in the regions. */
5767
e3dc8847 5768void
1579bae1 5769lang_reset_memory_regions (void)
252b5132
RH
5770{
5771 lang_memory_region_type *p = lang_memory_region_list;
b3327aad 5772 asection *o;
e9ee469a 5773 lang_output_section_statement_type *os;
252b5132 5774
1579bae1 5775 for (p = lang_memory_region_list; p != NULL; p = p->next)
252b5132 5776 {
252b5132 5777 p->current = p->origin;
66e28d60 5778 p->last_os = NULL;
252b5132 5779 }
b3327aad 5780
e9ee469a
AM
5781 for (os = &lang_output_section_statement.head->output_section_statement;
5782 os != NULL;
5783 os = os->next)
cde9e0be
AM
5784 {
5785 os->processed_vma = FALSE;
5786 os->processed_lma = FALSE;
5787 }
e9ee469a 5788
b3327aad 5789 for (o = output_bfd->sections; o != NULL; o = o->next)
1a23a9e6
AM
5790 {
5791 /* Save the last size for possible use by bfd_relax_section. */
5792 o->rawsize = o->size;
5793 o->size = 0;
5794 }
252b5132
RH
5795}
5796
164e712d 5797/* Worker for lang_gc_sections_1. */
252b5132
RH
5798
5799static void
1579bae1
AM
5800gc_section_callback (lang_wild_statement_type *ptr,
5801 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5802 asection *section,
5803 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5804 void *data ATTRIBUTE_UNUSED)
252b5132 5805{
164e712d
AM
5806 /* If the wild pattern was marked KEEP, the member sections
5807 should be as well. */
4dec4d4e
RH
5808 if (ptr->keep_sections)
5809 section->flags |= SEC_KEEP;
252b5132
RH
5810}
5811
252b5132
RH
5812/* Iterate over sections marking them against GC. */
5813
5814static void
1579bae1 5815lang_gc_sections_1 (lang_statement_union_type *s)
252b5132 5816{
1579bae1 5817 for (; s != NULL; s = s->header.next)
252b5132
RH
5818 {
5819 switch (s->header.type)
5820 {
5821 case lang_wild_statement_enum:
164e712d 5822 walk_wild (&s->wild_statement, gc_section_callback, NULL);
abc6ab0a 5823 break;
252b5132
RH
5824 case lang_constructors_statement_enum:
5825 lang_gc_sections_1 (constructor_list.head);
5826 break;
5827 case lang_output_section_statement_enum:
5828 lang_gc_sections_1 (s->output_section_statement.children.head);
5829 break;
5830 case lang_group_statement_enum:
5831 lang_gc_sections_1 (s->group_statement.children.head);
5832 break;
5833 default:
5834 break;
5835 }
5836 }
5837}
5838
5839static void
1579bae1 5840lang_gc_sections (void)
252b5132 5841{
252b5132
RH
5842 /* Keep all sections so marked in the link script. */
5843
5844 lang_gc_sections_1 (statement_list.head);
5845
9ca57817
AM
5846 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5847 the special case of debug info. (See bfd/stabs.c)
5848 Twiddle the flag here, to simplify later linker code. */
5849 if (link_info.relocatable)
5850 {
5851 LANG_FOR_EACH_INPUT_STATEMENT (f)
5852 {
5853 asection *sec;
5854 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5855 if ((sec->flags & SEC_DEBUGGING) == 0)
5856 sec->flags &= ~SEC_EXCLUDE;
5857 }
5858 }
5859
57316bff 5860 if (link_info.gc_sections)
164e712d 5861 bfd_gc_sections (output_bfd, &link_info);
252b5132
RH
5862}
5863
b10a8ae0
L
5864/* Worker for lang_find_relro_sections_1. */
5865
5866static void
5867find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
5868 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5869 asection *section,
5870 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5871 void *data)
5872{
5873 /* Discarded, excluded and ignored sections effectively have zero
5874 size. */
5875 if (section->output_section != NULL
5876 && section->output_section->owner == output_bfd
5877 && (section->output_section->flags & SEC_EXCLUDE) == 0
5878 && !IGNORE_SECTION (section)
5879 && section->size != 0)
5880 {
5881 bfd_boolean *has_relro_section = (bfd_boolean *) data;
5882 *has_relro_section = TRUE;
5883 }
5884}
5885
5886/* Iterate over sections for relro sections. */
5887
5888static void
5889lang_find_relro_sections_1 (lang_statement_union_type *s,
5890 bfd_boolean *has_relro_section)
5891{
5892 if (*has_relro_section)
5893 return;
5894
5895 for (; s != NULL; s = s->header.next)
5896 {
5897 if (s == expld.dataseg.relro_end_stat)
5898 break;
5899
5900 switch (s->header.type)
5901 {
5902 case lang_wild_statement_enum:
5903 walk_wild (&s->wild_statement,
5904 find_relro_section_callback,
5905 has_relro_section);
5906 break;
5907 case lang_constructors_statement_enum:
5908 lang_find_relro_sections_1 (constructor_list.head,
5909 has_relro_section);
5910 break;
5911 case lang_output_section_statement_enum:
5912 lang_find_relro_sections_1 (s->output_section_statement.children.head,
5913 has_relro_section);
5914 break;
5915 case lang_group_statement_enum:
5916 lang_find_relro_sections_1 (s->group_statement.children.head,
5917 has_relro_section);
5918 break;
5919 default:
5920 break;
5921 }
5922 }
5923}
5924
5925static void
5926lang_find_relro_sections (void)
5927{
5928 bfd_boolean has_relro_section = FALSE;
5929
5930 /* Check all sections in the link script. */
5931
5932 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
5933 &has_relro_section);
5934
5935 if (!has_relro_section)
5936 link_info.relro = FALSE;
5937}
5938
fbbc3759
L
5939/* Relax all sections until bfd_relax_section gives up. */
5940
5941static void
5942relax_sections (void)
5943{
5944 /* Keep relaxing until bfd_relax_section gives up. */
5945 bfd_boolean relax_again;
5946
4a828aab 5947 link_info.relax_trip = -1;
fbbc3759
L
5948 do
5949 {
329c1c86 5950 relax_again = FALSE;
4a828aab 5951 link_info.relax_trip++;
fbbc3759
L
5952
5953 /* Note: pe-dll.c does something like this also. If you find
5954 you need to change this code, you probably need to change
5955 pe-dll.c also. DJ */
5956
5957 /* Do all the assignments with our current guesses as to
5958 section sizes. */
5959 lang_do_assignments ();
5960
5961 /* We must do this after lang_do_assignments, because it uses
5962 size. */
5963 lang_reset_memory_regions ();
5964
5965 /* Perform another relax pass - this time we know where the
5966 globals are, so can make a better guess. */
5967 lang_size_sections (&relax_again, FALSE);
5968 }
5969 while (relax_again);
5970}
5971
252b5132 5972void
1579bae1 5973lang_process (void)
252b5132 5974{
55255dae 5975 /* Finalize dynamic list. */
40b36307
L
5976 if (link_info.dynamic_list)
5977 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
55255dae 5978
252b5132
RH
5979 current_target = default_target;
5980
08da4cac
KH
5981 /* Open the output file. */
5982 lang_for_each_statement (ldlang_open_output);
e5caa5e0 5983 init_opb ();
252b5132
RH
5984
5985 ldemul_create_output_section_statements ();
5986
08da4cac 5987 /* Add to the hash table all undefineds on the command line. */
252b5132
RH
5988 lang_place_undefineds ();
5989
082b7297
L
5990 if (!bfd_section_already_linked_table_init ())
5991 einfo (_("%P%F: Failed to create hash table\n"));
9503fd87 5992
08da4cac 5993 /* Create a bfd for each input file. */
252b5132 5994 current_target = default_target;
b34976b6 5995 open_input_bfds (statement_list.head, FALSE);
252b5132 5996
e3e942e9
AM
5997 link_info.gc_sym_list = &entry_symbol;
5998 if (entry_symbol.name == NULL)
5999 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6000
252b5132
RH
6001 ldemul_after_open ();
6002
082b7297 6003 bfd_section_already_linked_table_free ();
9503fd87 6004
252b5132
RH
6005 /* Make sure that we're not mixing architectures. We call this
6006 after all the input files have been opened, but before we do any
6007 other processing, so that any operations merge_private_bfd_data
6008 does on the output file will be known during the rest of the
6009 link. */
6010 lang_check ();
6011
6012 /* Handle .exports instead of a version script if we're told to do so. */
6013 if (command_line.version_exports_section)
6014 lang_do_version_exports_section ();
6015
6016 /* Build all sets based on the information gathered from the input
6017 files. */
6018 ldctor_build_sets ();
6019
6020 /* Remove unreferenced sections if asked to. */
164e712d 6021 lang_gc_sections ();
252b5132 6022
08da4cac 6023 /* Size up the common data. */
252b5132
RH
6024 lang_common ();
6025
bcaa7b3e
L
6026 /* Update wild statements. */
6027 update_wild_statements (statement_list.head);
6028
252b5132 6029 /* Run through the contours of the script and attach input sections
08da4cac 6030 to the correct output sections. */
1579bae1 6031 map_input_to_output_sections (statement_list.head, NULL, NULL);
252b5132 6032
53d25da6
AM
6033 process_insert_statements ();
6034
08da4cac 6035 /* Find any sections not attached explicitly and handle them. */
252b5132
RH
6036 lang_place_orphans ();
6037
1049f94e 6038 if (! link_info.relocatable)
862120bd 6039 {
57ceae94
AM
6040 asection *found;
6041
6042 /* Merge SEC_MERGE sections. This has to be done after GC of
6043 sections, so that GCed sections are not merged, but before
6044 assigning dynamic symbols, since removing whole input sections
6045 is hard then. */
6046 bfd_merge_sections (output_bfd, &link_info);
6047
862120bd 6048 /* Look for a text section and set the readonly attribute in it. */
57ceae94 6049 found = bfd_get_section_by_name (output_bfd, ".text");
862120bd 6050
1579bae1 6051 if (found != NULL)
862120bd
AM
6052 {
6053 if (config.text_read_only)
6054 found->flags |= SEC_READONLY;
6055 else
6056 found->flags &= ~SEC_READONLY;
6057 }
6058 }
6059
6060 /* Do anything special before sizing sections. This is where ELF
6061 and other back-ends size dynamic sections. */
252b5132
RH
6062 ldemul_before_allocation ();
6063
6064 /* We must record the program headers before we try to fix the
6065 section positions, since they will affect SIZEOF_HEADERS. */
6066 lang_record_phdrs ();
6067
b10a8ae0
L
6068 /* Check relro sections. */
6069 if (link_info.relro && ! link_info.relocatable)
6070 lang_find_relro_sections ();
6071
b3327aad 6072 /* Size up the sections. */
e9ee469a 6073 lang_size_sections (NULL, !command_line.relax);
b3327aad 6074
08da4cac 6075 /* Now run around and relax if we can. */
252b5132
RH
6076 if (command_line.relax)
6077 {
fbbc3759
L
6078 /* We may need more than one relaxation pass. */
6079 int i = link_info.relax_pass;
252b5132 6080
fbbc3759
L
6081 /* The backend can use it to determine the current pass. */
6082 link_info.relax_pass = 0;
252b5132 6083
fbbc3759
L
6084 while (i--)
6085 {
6086 relax_sections ();
6087 link_info.relax_pass++;
252b5132 6088 }
cf888e70
NC
6089
6090 /* Final extra sizing to report errors. */
e9ee469a 6091 lang_do_assignments ();
5a46fe39 6092 lang_reset_memory_regions ();
e9ee469a 6093 lang_size_sections (NULL, TRUE);
252b5132 6094 }
252b5132
RH
6095
6096 /* See if anything special should be done now we know how big
6097 everything is. */
6098 ldemul_after_allocation ();
6099
6100 /* Fix any .startof. or .sizeof. symbols. */
6101 lang_set_startof ();
6102
08da4cac
KH
6103 /* Do all the assignments, now that we know the final resting places
6104 of all the symbols. */
252b5132 6105
e9ee469a 6106 lang_do_assignments ();
252b5132 6107
8ded5a0f
AM
6108 ldemul_finish ();
6109
252b5132 6110 /* Make sure that the section addresses make sense. */
1049f94e 6111 if (! link_info.relocatable
252b5132
RH
6112 && command_line.check_section_addresses)
6113 lang_check_section_addresses ();
5f992e62 6114
750877ba 6115 lang_end ();
252b5132
RH
6116}
6117
6118/* EXPORTED TO YACC */
6119
6120void
1579bae1
AM
6121lang_add_wild (struct wildcard_spec *filespec,
6122 struct wildcard_list *section_list,
6123 bfd_boolean keep_sections)
252b5132 6124{
b6bf44ba
AM
6125 struct wildcard_list *curr, *next;
6126 lang_wild_statement_type *new;
6127
6128 /* Reverse the list as the parser puts it back to front. */
6129 for (curr = section_list, section_list = NULL;
6130 curr != NULL;
6131 section_list = curr, curr = next)
6132 {
6133 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
b34976b6 6134 placed_commons = TRUE;
252b5132 6135
b6bf44ba
AM
6136 next = curr->next;
6137 curr->next = section_list;
6138 }
6139
6140 if (filespec != NULL && filespec->name != NULL)
252b5132 6141 {
b6bf44ba
AM
6142 if (strcmp (filespec->name, "*") == 0)
6143 filespec->name = NULL;
6144 else if (! wildcardp (filespec->name))
b34976b6 6145 lang_has_input_file = TRUE;
252b5132 6146 }
b6bf44ba
AM
6147
6148 new = new_stat (lang_wild_statement, stat_ptr);
6149 new->filename = NULL;
b34976b6 6150 new->filenames_sorted = FALSE;
b6bf44ba 6151 if (filespec != NULL)
252b5132 6152 {
b6bf44ba 6153 new->filename = filespec->name;
bcaa7b3e 6154 new->filenames_sorted = filespec->sorted == by_name;
252b5132 6155 }
b6bf44ba 6156 new->section_list = section_list;
252b5132 6157 new->keep_sections = keep_sections;
252b5132 6158 lang_list_init (&new->children);
72223188 6159 analyze_walk_wild_section_handler (new);
252b5132
RH
6160}
6161
6162void
ba916c8a
MM
6163lang_section_start (const char *name, etree_type *address,
6164 const segment_type *segment)
252b5132 6165{
d1778b88 6166 lang_address_statement_type *ad;
252b5132 6167
d1778b88 6168 ad = new_stat (lang_address_statement, stat_ptr);
252b5132
RH
6169 ad->section_name = name;
6170 ad->address = address;
ba916c8a 6171 ad->segment = segment;
252b5132
RH
6172}
6173
6174/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6175 because of a -e argument on the command line, or zero if this is
6176 called by ENTRY in a linker script. Command line arguments take
6177 precedence. */
6178
6179void
1579bae1 6180lang_add_entry (const char *name, bfd_boolean cmdline)
252b5132 6181{
e3e942e9 6182 if (entry_symbol.name == NULL
252b5132
RH
6183 || cmdline
6184 || ! entry_from_cmdline)
6185 {
e3e942e9 6186 entry_symbol.name = name;
252b5132
RH
6187 entry_from_cmdline = cmdline;
6188 }
6189}
6190
a359509e
ZW
6191/* Set the default start symbol to NAME. .em files should use this,
6192 not lang_add_entry, to override the use of "start" if neither the
6193 linker script nor the command line specifies an entry point. NAME
6194 must be permanently allocated. */
6195void
6196lang_default_entry (const char *name)
6197{
6198 entry_symbol_default = name;
6199}
6200
252b5132 6201void
1579bae1 6202lang_add_target (const char *name)
252b5132 6203{
bd4d42c1 6204 lang_target_statement_type *new;
252b5132 6205
bd4d42c1 6206 new = new_stat (lang_target_statement, stat_ptr);
252b5132 6207 new->target = name;
252b5132
RH
6208}
6209
6210void
1579bae1 6211lang_add_map (const char *name)
252b5132
RH
6212{
6213 while (*name)
6214 {
6215 switch (*name)
6216 {
08da4cac 6217 case 'F':
b34976b6 6218 map_option_f = TRUE;
252b5132
RH
6219 break;
6220 }
6221 name++;
6222 }
6223}
6224
6225void
1579bae1 6226lang_add_fill (fill_type *fill)
252b5132 6227{
bd4d42c1 6228 lang_fill_statement_type *new;
252b5132 6229
bd4d42c1 6230 new = new_stat (lang_fill_statement, stat_ptr);
2c382fb6 6231 new->fill = fill;
252b5132
RH
6232}
6233
6234void
1579bae1 6235lang_add_data (int type, union etree_union *exp)
252b5132 6236{
bd4d42c1 6237 lang_data_statement_type *new;
252b5132 6238
bd4d42c1 6239 new = new_stat (lang_data_statement, stat_ptr);
252b5132
RH
6240 new->exp = exp;
6241 new->type = type;
252b5132
RH
6242}
6243
6244/* Create a new reloc statement. RELOC is the BFD relocation type to
6245 generate. HOWTO is the corresponding howto structure (we could
6246 look this up, but the caller has already done so). SECTION is the
6247 section to generate a reloc against, or NAME is the name of the
6248 symbol to generate a reloc against. Exactly one of SECTION and
6249 NAME must be NULL. ADDEND is an expression for the addend. */
6250
6251void
1579bae1
AM
6252lang_add_reloc (bfd_reloc_code_real_type reloc,
6253 reloc_howto_type *howto,
6254 asection *section,
6255 const char *name,
6256 union etree_union *addend)
252b5132
RH
6257{
6258 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5f992e62 6259
252b5132
RH
6260 p->reloc = reloc;
6261 p->howto = howto;
6262 p->section = section;
6263 p->name = name;
6264 p->addend_exp = addend;
6265
6266 p->addend_value = 0;
6267 p->output_section = NULL;
7fabd029 6268 p->output_offset = 0;
252b5132
RH
6269}
6270
6271lang_assignment_statement_type *
1579bae1 6272lang_add_assignment (etree_type *exp)
252b5132 6273{
bd4d42c1 6274 lang_assignment_statement_type *new;
252b5132 6275
bd4d42c1 6276 new = new_stat (lang_assignment_statement, stat_ptr);
252b5132
RH
6277 new->exp = exp;
6278 return new;
6279}
6280
6281void
1579bae1 6282lang_add_attribute (enum statement_enum attribute)
252b5132 6283{
bd4d42c1 6284 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
252b5132
RH
6285}
6286
6287void
1579bae1 6288lang_startup (const char *name)
252b5132 6289{
1579bae1 6290 if (startup_file != NULL)
252b5132 6291 {
4520f868 6292 einfo (_("%P%F: multiple STARTUP files\n"));
252b5132
RH
6293 }
6294 first_file->filename = name;
6295 first_file->local_sym_name = name;
b34976b6 6296 first_file->real = TRUE;
252b5132
RH
6297
6298 startup_file = name;
6299}
6300
6301void
1579bae1 6302lang_float (bfd_boolean maybe)
252b5132
RH
6303{
6304 lang_float_flag = maybe;
6305}
6306
ee3cc2e2
RS
6307
6308/* Work out the load- and run-time regions from a script statement, and
6309 store them in *LMA_REGION and *REGION respectively.
6310
a747ee4d
NC
6311 MEMSPEC is the name of the run-time region, or the value of
6312 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6313 LMA_MEMSPEC is the name of the load-time region, or null if the
6314 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6315 had an explicit load address.
ee3cc2e2
RS
6316
6317 It is an error to specify both a load region and a load address. */
6318
6319static void
6bdafbeb
NC
6320lang_get_regions (lang_memory_region_type **region,
6321 lang_memory_region_type **lma_region,
1579bae1
AM
6322 const char *memspec,
6323 const char *lma_memspec,
6bdafbeb
NC
6324 bfd_boolean have_lma,
6325 bfd_boolean have_vma)
ee3cc2e2 6326{
a747ee4d 6327 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
ee3cc2e2 6328
6feb9908
AM
6329 /* If no runtime region or VMA has been specified, but the load region
6330 has been specified, then use the load region for the runtime region
6331 as well. */
6bdafbeb
NC
6332 if (lma_memspec != NULL
6333 && ! have_vma
6334 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
ee3cc2e2
RS
6335 *region = *lma_region;
6336 else
a747ee4d 6337 *region = lang_memory_region_lookup (memspec, FALSE);
ee3cc2e2 6338
6bdafbeb 6339 if (have_lma && lma_memspec != 0)
ee3cc2e2
RS
6340 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6341}
6342
252b5132 6343void
6bdafbeb
NC
6344lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6345 lang_output_section_phdr_list *phdrs,
6346 const char *lma_memspec)
252b5132 6347{
ee3cc2e2
RS
6348 lang_get_regions (&current_section->region,
6349 &current_section->lma_region,
6350 memspec, lma_memspec,
6bdafbeb
NC
6351 current_section->load_base != NULL,
6352 current_section->addr_tree != NULL);
252b5132 6353 current_section->fill = fill;
252b5132
RH
6354 current_section->phdrs = phdrs;
6355 stat_ptr = &statement_list;
6356}
6357
08da4cac
KH
6358/* Create an absolute symbol with the given name with the value of the
6359 address of first byte of the section named.
6360
6361 If the symbol already exists, then do nothing. */
252b5132 6362
252b5132 6363void
1579bae1 6364lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
252b5132
RH
6365{
6366 struct bfd_link_hash_entry *h;
6367
b34976b6 6368 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 6369 if (h == NULL)
252b5132
RH
6370 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6371
6372 if (h->type == bfd_link_hash_new
6373 || h->type == bfd_link_hash_undefined)
6374 {
6375 asection *sec;
6376
6377 h->type = bfd_link_hash_defined;
6378
6379 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 6380 if (sec == NULL)
252b5132
RH
6381 h->u.def.value = 0;
6382 else
6383 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
6384
6385 h->u.def.section = bfd_abs_section_ptr;
6386 }
6387}
6388
08da4cac
KH
6389/* Create an absolute symbol with the given name with the value of the
6390 address of the first byte after the end of the section named.
6391
6392 If the symbol already exists, then do nothing. */
252b5132 6393
252b5132 6394void
1579bae1 6395lang_abs_symbol_at_end_of (const char *secname, const char *name)
252b5132
RH
6396{
6397 struct bfd_link_hash_entry *h;
6398
b34976b6 6399 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 6400 if (h == NULL)
252b5132
RH
6401 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6402
6403 if (h->type == bfd_link_hash_new
6404 || h->type == bfd_link_hash_undefined)
6405 {
6406 asection *sec;
6407
6408 h->type = bfd_link_hash_defined;
6409
6410 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 6411 if (sec == NULL)
252b5132
RH
6412 h->u.def.value = 0;
6413 else
6414 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
eea6121a 6415 + TO_ADDR (sec->size));
252b5132
RH
6416
6417 h->u.def.section = bfd_abs_section_ptr;
6418 }
6419}
6420
6421void
1579bae1
AM
6422lang_statement_append (lang_statement_list_type *list,
6423 lang_statement_union_type *element,
6424 lang_statement_union_type **field)
252b5132
RH
6425{
6426 *(list->tail) = element;
6427 list->tail = field;
6428}
6429
6430/* Set the output format type. -oformat overrides scripts. */
6431
6432void
1579bae1
AM
6433lang_add_output_format (const char *format,
6434 const char *big,
6435 const char *little,
6436 int from_script)
252b5132
RH
6437{
6438 if (output_target == NULL || !from_script)
6439 {
6440 if (command_line.endian == ENDIAN_BIG
6441 && big != NULL)
6442 format = big;
6443 else if (command_line.endian == ENDIAN_LITTLE
6444 && little != NULL)
6445 format = little;
6446
6447 output_target = format;
6448 }
6449}
6450
53d25da6
AM
6451void
6452lang_add_insert (const char *where, int is_before)
6453{
6454 lang_insert_statement_type *new;
6455
6456 new = new_stat (lang_insert_statement, stat_ptr);
6457 new->where = where;
6458 new->is_before = is_before;
6459 saved_script_handle = previous_script_handle;
6460}
6461
252b5132
RH
6462/* Enter a group. This creates a new lang_group_statement, and sets
6463 stat_ptr to build new statements within the group. */
6464
6465void
1579bae1 6466lang_enter_group (void)
252b5132
RH
6467{
6468 lang_group_statement_type *g;
6469
6470 g = new_stat (lang_group_statement, stat_ptr);
6471 lang_list_init (&g->children);
6472 stat_ptr = &g->children;
6473}
6474
6475/* Leave a group. This just resets stat_ptr to start writing to the
6476 regular list of statements again. Note that this will not work if
6477 groups can occur inside anything else which can adjust stat_ptr,
6478 but currently they can't. */
6479
6480void
1579bae1 6481lang_leave_group (void)
252b5132
RH
6482{
6483 stat_ptr = &statement_list;
6484}
6485
6486/* Add a new program header. This is called for each entry in a PHDRS
6487 command in a linker script. */
6488
6489void
1579bae1
AM
6490lang_new_phdr (const char *name,
6491 etree_type *type,
6492 bfd_boolean filehdr,
6493 bfd_boolean phdrs,
6494 etree_type *at,
6495 etree_type *flags)
252b5132
RH
6496{
6497 struct lang_phdr *n, **pp;
6498
1579bae1 6499 n = stat_alloc (sizeof (struct lang_phdr));
252b5132
RH
6500 n->next = NULL;
6501 n->name = name;
e9ee469a 6502 n->type = exp_get_value_int (type, 0, "program header type");
252b5132
RH
6503 n->filehdr = filehdr;
6504 n->phdrs = phdrs;
6505 n->at = at;
6506 n->flags = flags;
6507
6508 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6509 ;
6510 *pp = n;
6511}
6512
6513/* Record the program header information in the output BFD. FIXME: We
6514 should not be calling an ELF specific function here. */
6515
6516static void
1579bae1 6517lang_record_phdrs (void)
252b5132
RH
6518{
6519 unsigned int alc;
6520 asection **secs;
6bdafbeb 6521 lang_output_section_phdr_list *last;
252b5132 6522 struct lang_phdr *l;
afd7a018 6523 lang_output_section_statement_type *os;
252b5132
RH
6524
6525 alc = 10;
1579bae1 6526 secs = xmalloc (alc * sizeof (asection *));
252b5132 6527 last = NULL;
591a748a 6528
252b5132
RH
6529 for (l = lang_phdr_list; l != NULL; l = l->next)
6530 {
6531 unsigned int c;
6532 flagword flags;
6533 bfd_vma at;
6534
6535 c = 0;
afd7a018
AM
6536 for (os = &lang_output_section_statement.head->output_section_statement;
6537 os != NULL;
6538 os = os->next)
252b5132 6539 {
6bdafbeb 6540 lang_output_section_phdr_list *pl;
252b5132 6541
0841712e
JJ
6542 if (os->constraint == -1)
6543 continue;
252b5132
RH
6544
6545 pl = os->phdrs;
6546 if (pl != NULL)
6547 last = pl;
6548 else
6549 {
6550 if (os->sectype == noload_section
6551 || os->bfd_section == NULL
6552 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6553 continue;
591a748a 6554
e9442572 6555 if (last == NULL)
591a748a
NC
6556 {
6557 lang_output_section_statement_type * tmp_os;
6558
6559 /* If we have not run across a section with a program
6560 header assigned to it yet, then scan forwards to find
6561 one. This prevents inconsistencies in the linker's
6562 behaviour when a script has specified just a single
6563 header and there are sections in that script which are
6564 not assigned to it, and which occur before the first
6565 use of that header. See here for more details:
6566 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6567 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
6568 if (tmp_os->phdrs)
e9442572
AM
6569 {
6570 last = tmp_os->phdrs;
6571 break;
6572 }
6573 if (last == NULL)
6574 einfo (_("%F%P: no sections assigned to phdrs\n"));
591a748a 6575 }
e9442572 6576 pl = last;
252b5132
RH
6577 }
6578
6579 if (os->bfd_section == NULL)
6580 continue;
6581
6582 for (; pl != NULL; pl = pl->next)
6583 {
6584 if (strcmp (pl->name, l->name) == 0)
6585 {
6586 if (c >= alc)
6587 {
6588 alc *= 2;
1579bae1 6589 secs = xrealloc (secs, alc * sizeof (asection *));
252b5132
RH
6590 }
6591 secs[c] = os->bfd_section;
6592 ++c;
b34976b6 6593 pl->used = TRUE;
252b5132
RH
6594 }
6595 }
6596 }
6597
6598 if (l->flags == NULL)
6599 flags = 0;
6600 else
e9ee469a 6601 flags = exp_get_vma (l->flags, 0, "phdr flags");
252b5132
RH
6602
6603 if (l->at == NULL)
6604 at = 0;
6605 else
e9ee469a 6606 at = exp_get_vma (l->at, 0, "phdr load address");
252b5132
RH
6607
6608 if (! bfd_record_phdr (output_bfd, l->type,
d1778b88 6609 l->flags != NULL, flags, l->at != NULL,
252b5132
RH
6610 at, l->filehdr, l->phdrs, c, secs))
6611 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6612 }
6613
6614 free (secs);
6615
6616 /* Make sure all the phdr assignments succeeded. */
afd7a018
AM
6617 for (os = &lang_output_section_statement.head->output_section_statement;
6618 os != NULL;
6619 os = os->next)
252b5132 6620 {
6bdafbeb 6621 lang_output_section_phdr_list *pl;
252b5132 6622
afd7a018
AM
6623 if (os->constraint == -1
6624 || os->bfd_section == NULL)
252b5132
RH
6625 continue;
6626
afd7a018 6627 for (pl = os->phdrs;
252b5132
RH
6628 pl != NULL;
6629 pl = pl->next)
6630 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6631 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
afd7a018 6632 os->name, pl->name);
252b5132
RH
6633 }
6634}
6635
6636/* Record a list of sections which may not be cross referenced. */
6637
6638void
6bdafbeb 6639lang_add_nocrossref (lang_nocrossref_type *l)
252b5132
RH
6640{
6641 struct lang_nocrossrefs *n;
6642
1579bae1 6643 n = xmalloc (sizeof *n);
252b5132
RH
6644 n->next = nocrossref_list;
6645 n->list = l;
6646 nocrossref_list = n;
6647
6648 /* Set notice_all so that we get informed about all symbols. */
b34976b6 6649 link_info.notice_all = TRUE;
252b5132
RH
6650}
6651\f
6652/* Overlay handling. We handle overlays with some static variables. */
6653
6654/* The overlay virtual address. */
6655static etree_type *overlay_vma;
7e7d5768
AM
6656/* And subsection alignment. */
6657static etree_type *overlay_subalign;
252b5132 6658
252b5132
RH
6659/* An expression for the maximum section size seen so far. */
6660static etree_type *overlay_max;
6661
6662/* A list of all the sections in this overlay. */
6663
89cdebba 6664struct overlay_list {
252b5132
RH
6665 struct overlay_list *next;
6666 lang_output_section_statement_type *os;
6667};
6668
6669static struct overlay_list *overlay_list;
6670
6671/* Start handling an overlay. */
6672
6673void
7e7d5768 6674lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
252b5132
RH
6675{
6676 /* The grammar should prevent nested overlays from occurring. */
7e7d5768
AM
6677 ASSERT (overlay_vma == NULL
6678 && overlay_subalign == NULL
6679 && overlay_max == NULL);
252b5132
RH
6680
6681 overlay_vma = vma_expr;
7e7d5768 6682 overlay_subalign = subalign;
252b5132
RH
6683}
6684
6685/* Start a section in an overlay. We handle this by calling
9f88b410
RS
6686 lang_enter_output_section_statement with the correct VMA.
6687 lang_leave_overlay sets up the LMA and memory regions. */
252b5132
RH
6688
6689void
1579bae1 6690lang_enter_overlay_section (const char *name)
252b5132
RH
6691{
6692 struct overlay_list *n;
6693 etree_type *size;
6694
152d792f 6695 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
0841712e 6696 0, overlay_subalign, 0, 0);
252b5132 6697
9f88b410 6698 /* If this is the first section, then base the VMA of future
252b5132
RH
6699 sections on this one. This will work correctly even if `.' is
6700 used in the addresses. */
6701 if (overlay_list == NULL)
9f88b410 6702 overlay_vma = exp_nameop (ADDR, name);
252b5132
RH
6703
6704 /* Remember the section. */
1579bae1 6705 n = xmalloc (sizeof *n);
252b5132
RH
6706 n->os = current_section;
6707 n->next = overlay_list;
6708 overlay_list = n;
6709
6710 size = exp_nameop (SIZEOF, name);
6711
252b5132
RH
6712 /* Arrange to work out the maximum section end address. */
6713 if (overlay_max == NULL)
6714 overlay_max = size;
6715 else
6716 overlay_max = exp_binop (MAX_K, overlay_max, size);
6717}
6718
6719/* Finish a section in an overlay. There isn't any special to do
6720 here. */
6721
6722void
1579bae1 6723lang_leave_overlay_section (fill_type *fill,
6bdafbeb 6724 lang_output_section_phdr_list *phdrs)
252b5132
RH
6725{
6726 const char *name;
6727 char *clean, *s2;
6728 const char *s1;
6729 char *buf;
6730
6731 name = current_section->name;
6732
a747ee4d
NC
6733 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6734 region and that no load-time region has been specified. It doesn't
6735 really matter what we say here, since lang_leave_overlay will
6736 override it. */
6737 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
252b5132
RH
6738
6739 /* Define the magic symbols. */
6740
6741 clean = xmalloc (strlen (name) + 1);
6742 s2 = clean;
6743 for (s1 = name; *s1 != '\0'; s1++)
3882b010 6744 if (ISALNUM (*s1) || *s1 == '_')
252b5132
RH
6745 *s2++ = *s1;
6746 *s2 = '\0';
6747
6748 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6749 sprintf (buf, "__load_start_%s", clean);
34711ca3
AM
6750 lang_add_assignment (exp_provide (buf,
6751 exp_nameop (LOADADDR, name),
6752 FALSE));
252b5132
RH
6753
6754 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6755 sprintf (buf, "__load_stop_%s", clean);
34711ca3
AM
6756 lang_add_assignment (exp_provide (buf,
6757 exp_binop ('+',
6758 exp_nameop (LOADADDR, name),
6759 exp_nameop (SIZEOF, name)),
6760 FALSE));
252b5132
RH
6761
6762 free (clean);
6763}
6764
6765/* Finish an overlay. If there are any overlay wide settings, this
6766 looks through all the sections in the overlay and sets them. */
6767
6768void
1579bae1
AM
6769lang_leave_overlay (etree_type *lma_expr,
6770 int nocrossrefs,
6771 fill_type *fill,
6772 const char *memspec,
6bdafbeb 6773 lang_output_section_phdr_list *phdrs,
1579bae1 6774 const char *lma_memspec)
252b5132
RH
6775{
6776 lang_memory_region_type *region;
562d3460 6777 lang_memory_region_type *lma_region;
252b5132 6778 struct overlay_list *l;
6bdafbeb 6779 lang_nocrossref_type *nocrossref;
252b5132 6780
ee3cc2e2
RS
6781 lang_get_regions (&region, &lma_region,
6782 memspec, lma_memspec,
6bdafbeb 6783 lma_expr != NULL, FALSE);
562d3460 6784
252b5132
RH
6785 nocrossref = NULL;
6786
9f88b410
RS
6787 /* After setting the size of the last section, set '.' to end of the
6788 overlay region. */
6789 if (overlay_list != NULL)
6790 overlay_list->os->update_dot_tree
6791 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6792
252b5132
RH
6793 l = overlay_list;
6794 while (l != NULL)
6795 {
6796 struct overlay_list *next;
6797
1579bae1 6798 if (fill != NULL && l->os->fill == NULL)
252b5132 6799 l->os->fill = fill;
1545243b 6800
9f88b410
RS
6801 l->os->region = region;
6802 l->os->lma_region = lma_region;
6803
6804 /* The first section has the load address specified in the
6805 OVERLAY statement. The rest are worked out from that.
6806 The base address is not needed (and should be null) if
6807 an LMA region was specified. */
6808 if (l->next == 0)
152d792f
AM
6809 {
6810 l->os->load_base = lma_expr;
6811 l->os->sectype = normal_section;
6812 }
252b5132
RH
6813 if (phdrs != NULL && l->os->phdrs == NULL)
6814 l->os->phdrs = phdrs;
6815
9f88b410 6816 if (nocrossrefs)
252b5132 6817 {
6bdafbeb 6818 lang_nocrossref_type *nc;
252b5132 6819
1579bae1 6820 nc = xmalloc (sizeof *nc);
252b5132
RH
6821 nc->name = l->os->name;
6822 nc->next = nocrossref;
6823 nocrossref = nc;
6824 }
6825
6826 next = l->next;
6827 free (l);
6828 l = next;
6829 }
6830
6831 if (nocrossref != NULL)
6832 lang_add_nocrossref (nocrossref);
6833
252b5132 6834 overlay_vma = NULL;
252b5132
RH
6835 overlay_list = NULL;
6836 overlay_max = NULL;
6837}
6838\f
6839/* Version handling. This is only useful for ELF. */
6840
6841/* This global variable holds the version tree that we build. */
6842
6843struct bfd_elf_version_tree *lang_elf_version_info;
6844
108ba305
JJ
6845/* If PREV is NULL, return first version pattern matching particular symbol.
6846 If PREV is non-NULL, return first version pattern matching particular
6847 symbol after PREV (previously returned by lang_vers_match). */
252b5132 6848
108ba305
JJ
6849static struct bfd_elf_version_expr *
6850lang_vers_match (struct bfd_elf_version_expr_head *head,
6851 struct bfd_elf_version_expr *prev,
6852 const char *sym)
252b5132 6853{
108ba305
JJ
6854 const char *cxx_sym = sym;
6855 const char *java_sym = sym;
6856 struct bfd_elf_version_expr *expr = NULL;
252b5132 6857
108ba305 6858 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
252b5132 6859 {
2f97444a 6860 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
108ba305
JJ
6861 if (!cxx_sym)
6862 cxx_sym = sym;
252b5132 6863 }
df816215 6864 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
252b5132 6865 {
108ba305
JJ
6866 java_sym = cplus_demangle (sym, DMGL_JAVA);
6867 if (!java_sym)
6868 java_sym = sym;
252b5132
RH
6869 }
6870
5e35cbc2 6871 if (head->htab && (prev == NULL || prev->symbol))
252b5132 6872 {
108ba305
JJ
6873 struct bfd_elf_version_expr e;
6874
6875 switch (prev ? prev->mask : 0)
6876 {
6877 case 0:
6878 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6879 {
5e35cbc2 6880 e.symbol = sym;
108ba305 6881 expr = htab_find (head->htab, &e);
5e35cbc2 6882 while (expr && strcmp (expr->symbol, sym) == 0)
108ba305
JJ
6883 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6884 goto out_ret;
86043bbb
MM
6885 else
6886 expr = expr->next;
108ba305
JJ
6887 }
6888 /* Fallthrough */
6889 case BFD_ELF_VERSION_C_TYPE:
6890 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6891 {
5e35cbc2 6892 e.symbol = cxx_sym;
108ba305 6893 expr = htab_find (head->htab, &e);
7a995eb3 6894 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
108ba305
JJ
6895 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6896 goto out_ret;
86043bbb
MM
6897 else
6898 expr = expr->next;
108ba305
JJ
6899 }
6900 /* Fallthrough */
6901 case BFD_ELF_VERSION_CXX_TYPE:
6902 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6903 {
5e35cbc2 6904 e.symbol = java_sym;
108ba305 6905 expr = htab_find (head->htab, &e);
7a995eb3 6906 while (expr && strcmp (expr->symbol, java_sym) == 0)
108ba305
JJ
6907 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6908 goto out_ret;
86043bbb
MM
6909 else
6910 expr = expr->next;
108ba305
JJ
6911 }
6912 /* Fallthrough */
6913 default:
6914 break;
6915 }
252b5132 6916 }
108ba305
JJ
6917
6918 /* Finally, try the wildcards. */
5e35cbc2 6919 if (prev == NULL || prev->symbol)
108ba305 6920 expr = head->remaining;
252b5132 6921 else
108ba305 6922 expr = prev->next;
86043bbb 6923 for (; expr; expr = expr->next)
252b5132 6924 {
108ba305
JJ
6925 const char *s;
6926
86043bbb
MM
6927 if (!expr->pattern)
6928 continue;
6929
108ba305
JJ
6930 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6931 break;
6932
6933 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6934 s = java_sym;
6935 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6936 s = cxx_sym;
6937 else
6938 s = sym;
5e35cbc2 6939 if (fnmatch (expr->pattern, s, 0) == 0)
108ba305 6940 break;
252b5132
RH
6941 }
6942
108ba305
JJ
6943out_ret:
6944 if (cxx_sym != sym)
6945 free ((char *) cxx_sym);
6946 if (java_sym != sym)
6947 free ((char *) java_sym);
6948 return expr;
252b5132
RH
6949}
6950
5e35cbc2
L
6951/* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6952 return a string pointing to the symbol name. */
6953
6954static const char *
6955realsymbol (const char *pattern)
6956{
6957 const char *p;
6958 bfd_boolean changed = FALSE, backslash = FALSE;
6959 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6960
6961 for (p = pattern, s = symbol; *p != '\0'; ++p)
6962 {
6963 /* It is a glob pattern only if there is no preceding
6964 backslash. */
6965 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
6966 {
6967 free (symbol);
6968 return NULL;
6969 }
6970
6971 if (backslash)
6972 {
6973 /* Remove the preceding backslash. */
6974 *(s - 1) = *p;
6975 changed = TRUE;
6976 }
6977 else
6978 *s++ = *p;
6979
6980 backslash = *p == '\\';
6981 }
6982
6983 if (changed)
6984 {
6985 *s = '\0';
6986 return symbol;
6987 }
6988 else
6989 {
6990 free (symbol);
6991 return pattern;
6992 }
6993}
6994
86043bbb
MM
6995/* This is called for each variable name or match expression. NEW is
6996 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6997 pattern to be matched against symbol names. */
252b5132
RH
6998
6999struct bfd_elf_version_expr *
1579bae1
AM
7000lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7001 const char *new,
86043bbb
MM
7002 const char *lang,
7003 bfd_boolean literal_p)
252b5132
RH
7004{
7005 struct bfd_elf_version_expr *ret;
7006
1579bae1 7007 ret = xmalloc (sizeof *ret);
252b5132 7008 ret->next = orig;
86043bbb 7009 ret->pattern = literal_p ? NULL : new;
31941635
L
7010 ret->symver = 0;
7011 ret->script = 0;
86043bbb 7012 ret->symbol = literal_p ? new : realsymbol (new);
252b5132
RH
7013
7014 if (lang == NULL || strcasecmp (lang, "C") == 0)
108ba305 7015 ret->mask = BFD_ELF_VERSION_C_TYPE;
252b5132 7016 else if (strcasecmp (lang, "C++") == 0)
108ba305 7017 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
252b5132 7018 else if (strcasecmp (lang, "Java") == 0)
108ba305 7019 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
252b5132
RH
7020 else
7021 {
7022 einfo (_("%X%P: unknown language `%s' in version information\n"),
7023 lang);
108ba305 7024 ret->mask = BFD_ELF_VERSION_C_TYPE;
252b5132
RH
7025 }
7026
fac1652d 7027 return ldemul_new_vers_pattern (ret);
252b5132
RH
7028}
7029
7030/* This is called for each set of variable names and match
7031 expressions. */
7032
7033struct bfd_elf_version_tree *
1579bae1
AM
7034lang_new_vers_node (struct bfd_elf_version_expr *globals,
7035 struct bfd_elf_version_expr *locals)
252b5132
RH
7036{
7037 struct bfd_elf_version_tree *ret;
7038
108ba305
JJ
7039 ret = xcalloc (1, sizeof *ret);
7040 ret->globals.list = globals;
7041 ret->locals.list = locals;
7042 ret->match = lang_vers_match;
252b5132 7043 ret->name_indx = (unsigned int) -1;
252b5132
RH
7044 return ret;
7045}
7046
7047/* This static variable keeps track of version indices. */
7048
7049static int version_index;
7050
108ba305
JJ
7051static hashval_t
7052version_expr_head_hash (const void *p)
7053{
7054 const struct bfd_elf_version_expr *e = p;
7055
5e35cbc2 7056 return htab_hash_string (e->symbol);
108ba305
JJ
7057}
7058
7059static int
7060version_expr_head_eq (const void *p1, const void *p2)
7061{
7062 const struct bfd_elf_version_expr *e1 = p1;
7063 const struct bfd_elf_version_expr *e2 = p2;
7064
5e35cbc2 7065 return strcmp (e1->symbol, e2->symbol) == 0;
108ba305
JJ
7066}
7067
7068static void
7069lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7070{
7071 size_t count = 0;
7072 struct bfd_elf_version_expr *e, *next;
7073 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7074
7075 for (e = head->list; e; e = e->next)
7076 {
5e35cbc2 7077 if (e->symbol)
108ba305
JJ
7078 count++;
7079 head->mask |= e->mask;
7080 }
7081
7082 if (count)
7083 {
7084 head->htab = htab_create (count * 2, version_expr_head_hash,
7085 version_expr_head_eq, NULL);
7086 list_loc = &head->list;
7087 remaining_loc = &head->remaining;
7088 for (e = head->list; e; e = next)
7089 {
7090 next = e->next;
5e35cbc2 7091 if (!e->symbol)
108ba305
JJ
7092 {
7093 *remaining_loc = e;
7094 remaining_loc = &e->next;
7095 }
7096 else
7097 {
7098 void **loc = htab_find_slot (head->htab, e, INSERT);
7099
7100 if (*loc)
7101 {
7102 struct bfd_elf_version_expr *e1, *last;
7103
7104 e1 = *loc;
7105 last = NULL;
7106 do
7107 {
7108 if (e1->mask == e->mask)
7109 {
7110 last = NULL;
7111 break;
7112 }
7113 last = e1;
7114 e1 = e1->next;
7115 }
5e35cbc2 7116 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
108ba305
JJ
7117
7118 if (last == NULL)
7119 {
7120 /* This is a duplicate. */
7121 /* FIXME: Memory leak. Sometimes pattern is not
7122 xmalloced alone, but in larger chunk of memory. */
5e35cbc2 7123 /* free (e->symbol); */
108ba305
JJ
7124 free (e);
7125 }
7126 else
7127 {
7128 e->next = last->next;
7129 last->next = e;
7130 }
7131 }
7132 else
7133 {
7134 *loc = e;
7135 *list_loc = e;
7136 list_loc = &e->next;
7137 }
7138 }
7139 }
7140 *remaining_loc = NULL;
7141 *list_loc = head->remaining;
7142 }
7143 else
7144 head->remaining = head->list;
7145}
7146
252b5132
RH
7147/* This is called when we know the name and dependencies of the
7148 version. */
7149
7150void
1579bae1
AM
7151lang_register_vers_node (const char *name,
7152 struct bfd_elf_version_tree *version,
7153 struct bfd_elf_version_deps *deps)
252b5132
RH
7154{
7155 struct bfd_elf_version_tree *t, **pp;
7156 struct bfd_elf_version_expr *e1;
7157
6b9b879a
JJ
7158 if (name == NULL)
7159 name = "";
7160
7161 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7162 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7163 {
6feb9908
AM
7164 einfo (_("%X%P: anonymous version tag cannot be combined"
7165 " with other version tags\n"));
5ed6aba4 7166 free (version);
6b9b879a
JJ
7167 return;
7168 }
7169
252b5132
RH
7170 /* Make sure this node has a unique name. */
7171 for (t = lang_elf_version_info; t != NULL; t = t->next)
7172 if (strcmp (t->name, name) == 0)
7173 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7174
108ba305
JJ
7175 lang_finalize_version_expr_head (&version->globals);
7176 lang_finalize_version_expr_head (&version->locals);
7177
252b5132
RH
7178 /* Check the global and local match names, and make sure there
7179 aren't any duplicates. */
7180
108ba305 7181 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
252b5132
RH
7182 {
7183 for (t = lang_elf_version_info; t != NULL; t = t->next)
7184 {
7185 struct bfd_elf_version_expr *e2;
7186
5e35cbc2 7187 if (t->locals.htab && e1->symbol)
108ba305
JJ
7188 {
7189 e2 = htab_find (t->locals.htab, e1);
5e35cbc2 7190 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
108ba305
JJ
7191 {
7192 if (e1->mask == e2->mask)
6feb9908
AM
7193 einfo (_("%X%P: duplicate expression `%s'"
7194 " in version information\n"), e1->symbol);
108ba305
JJ
7195 e2 = e2->next;
7196 }
7197 }
5e35cbc2 7198 else if (!e1->symbol)
108ba305 7199 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
6feb9908
AM
7200 if (strcmp (e1->pattern, e2->pattern) == 0
7201 && e1->mask == e2->mask)
7202 einfo (_("%X%P: duplicate expression `%s'"
7203 " in version information\n"), e1->pattern);
252b5132
RH
7204 }
7205 }
7206
108ba305 7207 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
252b5132
RH
7208 {
7209 for (t = lang_elf_version_info; t != NULL; t = t->next)
7210 {
7211 struct bfd_elf_version_expr *e2;
7212
5e35cbc2 7213 if (t->globals.htab && e1->symbol)
108ba305
JJ
7214 {
7215 e2 = htab_find (t->globals.htab, e1);
5e35cbc2 7216 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
108ba305
JJ
7217 {
7218 if (e1->mask == e2->mask)
6feb9908
AM
7219 einfo (_("%X%P: duplicate expression `%s'"
7220 " in version information\n"),
5e35cbc2 7221 e1->symbol);
108ba305
JJ
7222 e2 = e2->next;
7223 }
7224 }
5e35cbc2 7225 else if (!e1->symbol)
108ba305 7226 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6feb9908
AM
7227 if (strcmp (e1->pattern, e2->pattern) == 0
7228 && e1->mask == e2->mask)
7229 einfo (_("%X%P: duplicate expression `%s'"
7230 " in version information\n"), e1->pattern);
252b5132
RH
7231 }
7232 }
7233
7234 version->deps = deps;
7235 version->name = name;
6b9b879a
JJ
7236 if (name[0] != '\0')
7237 {
7238 ++version_index;
7239 version->vernum = version_index;
7240 }
7241 else
7242 version->vernum = 0;
252b5132
RH
7243
7244 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7245 ;
7246 *pp = version;
7247}
7248
7249/* This is called when we see a version dependency. */
7250
7251struct bfd_elf_version_deps *
1579bae1 7252lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
252b5132
RH
7253{
7254 struct bfd_elf_version_deps *ret;
7255 struct bfd_elf_version_tree *t;
7256
1579bae1 7257 ret = xmalloc (sizeof *ret);
252b5132
RH
7258 ret->next = list;
7259
7260 for (t = lang_elf_version_info; t != NULL; t = t->next)
7261 {
7262 if (strcmp (t->name, name) == 0)
7263 {
7264 ret->version_needed = t;
7265 return ret;
7266 }
7267 }
7268
7269 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7270
7271 return ret;
7272}
7273
7274static void
1579bae1 7275lang_do_version_exports_section (void)
252b5132
RH
7276{
7277 struct bfd_elf_version_expr *greg = NULL, *lreg;
7278
7279 LANG_FOR_EACH_INPUT_STATEMENT (is)
7280 {
7281 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7282 char *contents, *p;
7283 bfd_size_type len;
7284
7285 if (sec == NULL)
b7a26f91 7286 continue;
252b5132 7287
eea6121a 7288 len = sec->size;
252b5132
RH
7289 contents = xmalloc (len);
7290 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6f9efd97 7291 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
252b5132
RH
7292
7293 p = contents;
89cdebba 7294 while (p < contents + len)
252b5132 7295 {
86043bbb 7296 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
252b5132
RH
7297 p = strchr (p, '\0') + 1;
7298 }
7299
7300 /* Do not free the contents, as we used them creating the regex. */
7301
7302 /* Do not include this section in the link. */
a14a5de3 7303 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
252b5132
RH
7304 }
7305
86043bbb 7306 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
252b5132
RH
7307 lang_register_vers_node (command_line.version_exports_section,
7308 lang_new_vers_node (greg, lreg), NULL);
7309}
577a0623
AM
7310
7311void
1579bae1 7312lang_add_unique (const char *name)
577a0623
AM
7313{
7314 struct unique_sections *ent;
7315
7316 for (ent = unique_section_list; ent; ent = ent->next)
7317 if (strcmp (ent->name, name) == 0)
7318 return;
7319
1579bae1 7320 ent = xmalloc (sizeof *ent);
577a0623
AM
7321 ent->name = xstrdup (name);
7322 ent->next = unique_section_list;
7323 unique_section_list = ent;
7324}
55255dae
L
7325
7326/* Append the list of dynamic symbols to the existing one. */
7327
7328void
7329lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7330{
40b36307 7331 if (link_info.dynamic_list)
55255dae 7332 {
07806542
JJ
7333 struct bfd_elf_version_expr *tail;
7334 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7335 ;
40b36307
L
7336 tail->next = link_info.dynamic_list->head.list;
7337 link_info.dynamic_list->head.list = dynamic;
55255dae
L
7338 }
7339 else
7340 {
7341 struct bfd_elf_dynamic_list *d;
7342
7343 d = xcalloc (1, sizeof *d);
7344 d->head.list = dynamic;
7345 d->match = lang_vers_match;
40b36307 7346 link_info.dynamic_list = d;
55255dae
L
7347 }
7348}
7349
7350/* Append the list of C++ typeinfo dynamic symbols to the existing
7351 one. */
7352
7353void
7354lang_append_dynamic_list_cpp_typeinfo (void)
7355{
7356 const char * symbols [] =
7357 {
7358 "typeinfo name for*",
7359 "typeinfo for*"
7360 };
7361 struct bfd_elf_version_expr *dynamic = NULL;
7362 unsigned int i;
7363
7364 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7365 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7366 FALSE);
7367
7368 lang_append_dynamic_list (dynamic);
7369}
40b36307
L
7370
7371/* Append the list of C++ operator new and delete dynamic symbols to the
7372 existing one. */
7373
7374void
7375lang_append_dynamic_list_cpp_new (void)
7376{
7377 const char * symbols [] =
7378 {
7379 "operator new*",
7380 "operator delete*"
7381 };
7382 struct bfd_elf_version_expr *dynamic = NULL;
7383 unsigned int i;
7384
7385 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7386 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7387 FALSE);
7388
7389 lang_append_dynamic_list (dynamic);
7390}
This page took 0.907724 seconds and 4 git commands to generate.