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