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