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