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