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