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