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