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