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