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