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