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