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