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