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