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