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