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