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