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