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