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