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