daily update
[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,
cf888e70 3 2001, 2002, 2003
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"
42
7abb6dea 43#ifndef offsetof
cf888e70 44#define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
7abb6dea
AM
45#endif
46
cf888e70 47/* Locals variables. */
252b5132
RH
48static struct obstack stat_obstack;
49
50#define obstack_chunk_alloc xmalloc
51#define obstack_chunk_free free
5f992e62 52static const char *startup_file;
252b5132 53static lang_statement_list_type input_file_chain;
b34976b6 54static bfd_boolean placed_commons = FALSE;
252b5132 55static lang_output_section_statement_type *default_common_section;
b34976b6 56static bfd_boolean map_option_f;
252b5132
RH
57static bfd_vma print_dot;
58static lang_input_statement_type *first_file;
5f992e62
AM
59static const char *current_target;
60static const char *output_target;
252b5132
RH
61static lang_statement_list_type statement_list;
62static struct lang_phdr *lang_phdr_list;
420e579c 63static struct bfd_hash_table lang_definedness_table;
252b5132 64
cf888e70 65/* Forward declarations. */
1579bae1
AM
66static void exp_init_os (etree_type *);
67static bfd_boolean wildcardp (const char *);
68static lang_input_statement_type *lookup_name (const char *);
69static bfd_boolean load_symbols (lang_input_statement_type *,
70 lang_statement_list_type *);
420e579c
HPN
71static struct bfd_hash_entry *lang_definedness_newfunc
72 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
1579bae1
AM
73static void insert_undefined (const char *);
74static void print_statement (lang_statement_union_type *,
75 lang_output_section_statement_type *);
76static void print_statement_list (lang_statement_union_type *,
77 lang_output_section_statement_type *);
78static void print_statements (void);
79static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
80static void lang_record_phdrs (void);
81static void lang_do_version_exports_section (void);
82
83typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
84 asection *, lang_input_statement_type *, void *);
5f992e62 85
cf888e70 86/* Exported variables. */
252b5132 87lang_output_section_statement_type *abs_output_section;
aea4bd9d 88lang_statement_list_type lang_output_section_statement;
252b5132 89lang_statement_list_type *stat_ptr = &statement_list;
87f2a346 90lang_statement_list_type file_chain = { NULL, NULL };
e3e942e9 91struct bfd_sym_chain entry_symbol = { NULL, NULL };
1e281515 92const char *entry_section = ".text";
b34976b6
AM
93bfd_boolean entry_from_cmdline;
94bfd_boolean lang_has_input_file = FALSE;
95bfd_boolean had_output_filename = FALSE;
96bfd_boolean lang_float_flag = FALSE;
97bfd_boolean delete_output_file_on_failure = FALSE;
252b5132 98struct lang_nocrossrefs *nocrossref_list;
577a0623 99struct unique_sections *unique_section_list;
e3f2db7f 100static bfd_boolean ldlang_sysrooted_script = FALSE;
420e579c 101int lang_statement_iteration = 0;
252b5132
RH
102
103etree_type *base; /* Relocation base - or null */
104
d1778b88 105#define new_stat(x, y) \
1579bae1 106 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
252b5132 107
d1778b88
AM
108#define outside_section_address(q) \
109 ((q)->output_offset + (q)->output_section->vma)
252b5132 110
d1778b88
AM
111#define outside_symbol_address(q) \
112 ((q)->value + outside_section_address (q->section))
252b5132
RH
113
114#define SECTION_NAME_MAP_LENGTH (16)
115
1579bae1
AM
116void *
117stat_alloc (size_t size)
252b5132
RH
118{
119 return obstack_alloc (&stat_obstack, size);
120}
121
b34976b6 122bfd_boolean
1579bae1 123unique_section_p (const char *secnam)
577a0623
AM
124{
125 struct unique_sections *unam;
126
127 for (unam = unique_section_list; unam; unam = unam->next)
128 if (wildcardp (unam->name)
129 ? fnmatch (unam->name, secnam, 0) == 0
130 : strcmp (unam->name, secnam) == 0)
131 {
b34976b6 132 return TRUE;
577a0623
AM
133 }
134
b34976b6 135 return FALSE;
577a0623
AM
136}
137
08da4cac 138/* Generic traversal routines for finding matching sections. */
4dec4d4e
RH
139
140static void
1579bae1
AM
141walk_wild_section (lang_wild_statement_type *ptr,
142 lang_input_statement_type *file,
143 callback_t callback,
144 void *data)
4dec4d4e 145{
b6bf44ba
AM
146 asection *s;
147
148 if (file->just_syms_flag)
149 return;
150
151 for (s = file->the_bfd->sections; s != NULL; s = s->next)
4dec4d4e 152 {
b6bf44ba
AM
153 struct wildcard_list *sec;
154
155 sec = ptr->section_list;
2181f54f
AM
156 if (sec == NULL)
157 (*callback) (ptr, sec, s, file, data);
158
159 while (sec != NULL)
08da4cac 160 {
b34976b6 161 bfd_boolean skip = FALSE;
2181f54f
AM
162 struct name_list *list_tmp;
163
164 /* Don't process sections from files which were
165 excluded. */
166 for (list_tmp = sec->spec.exclude_name_list;
167 list_tmp;
168 list_tmp = list_tmp->next)
169 {
170 if (wildcardp (list_tmp->name))
171 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
172 else
173 skip = strcmp (list_tmp->name, file->filename) == 0;
4dec4d4e 174
34efb449
AO
175 /* If this file is part of an archive, and the archive is
176 excluded, exclude this file. */
177 if (! skip && file->the_bfd != NULL
178 && file->the_bfd->my_archive != NULL
179 && file->the_bfd->my_archive->filename != NULL)
180 {
181 if (wildcardp (list_tmp->name))
182 skip = fnmatch (list_tmp->name,
183 file->the_bfd->my_archive->filename,
184 0) == 0;
185 else
186 skip = strcmp (list_tmp->name,
187 file->the_bfd->my_archive->filename) == 0;
188 }
189
2181f54f
AM
190 if (skip)
191 break;
192 }
193
194 if (!skip && sec->spec.name != NULL)
b6bf44ba 195 {
2181f54f
AM
196 const char *sname = bfd_get_section_name (file->the_bfd, s);
197
198 if (wildcardp (sec->spec.name))
199 skip = fnmatch (sec->spec.name, sname, 0) != 0;
200 else
201 skip = strcmp (sec->spec.name, sname) != 0;
b6bf44ba 202 }
4dec4d4e 203
b6bf44ba
AM
204 if (!skip)
205 (*callback) (ptr, sec, s, file, data);
4dec4d4e 206
2181f54f 207 sec = sec->next;
4dec4d4e
RH
208 }
209 }
210}
211
212/* Handle a wild statement for a single file F. */
213
214static void
1579bae1
AM
215walk_wild_file (lang_wild_statement_type *s,
216 lang_input_statement_type *f,
217 callback_t callback,
218 void *data)
4dec4d4e
RH
219{
220 if (f->the_bfd == NULL
221 || ! bfd_check_format (f->the_bfd, bfd_archive))
b6bf44ba 222 walk_wild_section (s, f, callback, data);
4dec4d4e
RH
223 else
224 {
225 bfd *member;
226
227 /* This is an archive file. We must map each member of the
228 archive separately. */
1579bae1 229 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
4dec4d4e
RH
230 while (member != NULL)
231 {
232 /* When lookup_name is called, it will call the add_symbols
233 entry point for the archive. For each element of the
234 archive which is included, BFD will call ldlang_add_file,
235 which will set the usrdata field of the member to the
236 lang_input_statement. */
237 if (member->usrdata != NULL)
238 {
1579bae1 239 walk_wild_section (s, member->usrdata, callback, data);
4dec4d4e
RH
240 }
241
242 member = bfd_openr_next_archived_file (f->the_bfd, member);
243 }
244 }
245}
246
247static void
1579bae1 248walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
4dec4d4e 249{
b6bf44ba
AM
250 const char *file_spec = s->filename;
251
252 if (file_spec == NULL)
4dec4d4e
RH
253 {
254 /* Perform the iteration over all files in the list. */
e50d8076 255 LANG_FOR_EACH_INPUT_STATEMENT (f)
4dec4d4e 256 {
b6bf44ba 257 walk_wild_file (s, f, callback, data);
4dec4d4e
RH
258 }
259 }
b6bf44ba 260 else if (wildcardp (file_spec))
4dec4d4e 261 {
e50d8076 262 LANG_FOR_EACH_INPUT_STATEMENT (f)
4dec4d4e 263 {
b6bf44ba
AM
264 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
265 walk_wild_file (s, f, callback, data);
4dec4d4e
RH
266 }
267 }
268 else
269 {
e50d8076
NC
270 lang_input_statement_type *f;
271
4dec4d4e 272 /* Perform the iteration over a single file. */
b6bf44ba 273 f = lookup_name (file_spec);
6770ec8c 274 if (f)
b6bf44ba 275 walk_wild_file (s, f, callback, data);
4dec4d4e 276 }
5f992e62
AM
277}
278
08da4cac
KH
279/* lang_for_each_statement walks the parse tree and calls the provided
280 function for each node. */
252b5132
RH
281
282static void
1579bae1
AM
283lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
284 lang_statement_union_type *s)
252b5132 285{
1579bae1 286 for (; s != NULL; s = s->header.next)
252b5132
RH
287 {
288 func (s);
289
290 switch (s->header.type)
291 {
292 case lang_constructors_statement_enum:
293 lang_for_each_statement_worker (func, constructor_list.head);
294 break;
295 case lang_output_section_statement_enum:
296 lang_for_each_statement_worker
297 (func,
298 s->output_section_statement.children.head);
299 break;
300 case lang_wild_statement_enum:
301 lang_for_each_statement_worker
302 (func,
303 s->wild_statement.children.head);
304 break;
305 case lang_group_statement_enum:
306 lang_for_each_statement_worker (func,
307 s->group_statement.children.head);
308 break;
309 case lang_data_statement_enum:
310 case lang_reloc_statement_enum:
311 case lang_object_symbols_statement_enum:
312 case lang_output_statement_enum:
313 case lang_target_statement_enum:
314 case lang_input_section_enum:
315 case lang_input_statement_enum:
316 case lang_assignment_statement_enum:
317 case lang_padding_statement_enum:
318 case lang_address_statement_enum:
319 case lang_fill_statement_enum:
320 break;
321 default:
322 FAIL ();
323 break;
324 }
325 }
326}
327
328void
1579bae1 329lang_for_each_statement (void (*func) (lang_statement_union_type *))
252b5132 330{
08da4cac 331 lang_for_each_statement_worker (func, statement_list.head);
252b5132
RH
332}
333
334/*----------------------------------------------------------------------*/
08da4cac 335
252b5132 336void
1579bae1 337lang_list_init (lang_statement_list_type *list)
252b5132 338{
1579bae1 339 list->head = NULL;
252b5132
RH
340 list->tail = &list->head;
341}
342
08da4cac 343/* Build a new statement node for the parse tree. */
252b5132 344
08da4cac 345static lang_statement_union_type *
1579bae1
AM
346new_statement (enum statement_enum type,
347 size_t size,
348 lang_statement_list_type *list)
252b5132 349{
1579bae1 350 lang_statement_union_type *new;
252b5132 351
1579bae1 352 new = stat_alloc (size);
252b5132 353 new->header.type = type;
1579bae1 354 new->header.next = NULL;
252b5132
RH
355 lang_statement_append (list, new, &new->header.next);
356 return new;
357}
358
08da4cac
KH
359/* Build a new input file node for the language. There are several
360 ways in which we treat an input file, eg, we only look at symbols,
361 or prefix it with a -l etc.
252b5132 362
08da4cac 363 We can be supplied with requests for input files more than once;
396a2467 364 they may, for example be split over several lines like foo.o(.text)
d1778b88 365 foo.o(.data) etc, so when asked for a file we check that we haven't
08da4cac 366 got it already so we don't duplicate the bfd. */
252b5132 367
252b5132 368static lang_input_statement_type *
1579bae1
AM
369new_afile (const char *name,
370 lang_input_file_enum_type file_type,
371 const char *target,
372 bfd_boolean add_to_list)
252b5132
RH
373{
374 lang_input_statement_type *p;
375
376 if (add_to_list)
377 p = new_stat (lang_input_statement, stat_ptr);
378 else
379 {
1579bae1 380 p = stat_alloc (sizeof (lang_input_statement_type));
252b5132
RH
381 p->header.next = NULL;
382 }
383
b34976b6 384 lang_has_input_file = TRUE;
252b5132 385 p->target = target;
e3f2db7f 386 p->sysrooted = FALSE;
252b5132
RH
387 switch (file_type)
388 {
389 case lang_input_file_is_symbols_only_enum:
390 p->filename = name;
b34976b6
AM
391 p->is_archive = FALSE;
392 p->real = TRUE;
252b5132 393 p->local_sym_name = name;
b34976b6
AM
394 p->just_syms_flag = TRUE;
395 p->search_dirs_flag = FALSE;
252b5132
RH
396 break;
397 case lang_input_file_is_fake_enum:
398 p->filename = name;
b34976b6
AM
399 p->is_archive = FALSE;
400 p->real = FALSE;
252b5132 401 p->local_sym_name = name;
b34976b6
AM
402 p->just_syms_flag = FALSE;
403 p->search_dirs_flag = FALSE;
252b5132
RH
404 break;
405 case lang_input_file_is_l_enum:
b34976b6 406 p->is_archive = TRUE;
252b5132 407 p->filename = name;
b34976b6 408 p->real = TRUE;
1579bae1 409 p->local_sym_name = concat ("-l", name, NULL);
b34976b6
AM
410 p->just_syms_flag = FALSE;
411 p->search_dirs_flag = TRUE;
252b5132
RH
412 break;
413 case lang_input_file_is_marker_enum:
414 p->filename = name;
b34976b6
AM
415 p->is_archive = FALSE;
416 p->real = FALSE;
252b5132 417 p->local_sym_name = name;
b34976b6
AM
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = TRUE;
252b5132
RH
420 break;
421 case lang_input_file_is_search_file_enum:
e3f2db7f 422 p->sysrooted = ldlang_sysrooted_script;
252b5132 423 p->filename = name;
b34976b6
AM
424 p->is_archive = FALSE;
425 p->real = TRUE;
252b5132 426 p->local_sym_name = name;
b34976b6
AM
427 p->just_syms_flag = FALSE;
428 p->search_dirs_flag = TRUE;
252b5132
RH
429 break;
430 case lang_input_file_is_file_enum:
431 p->filename = name;
b34976b6
AM
432 p->is_archive = FALSE;
433 p->real = TRUE;
252b5132 434 p->local_sym_name = name;
b34976b6
AM
435 p->just_syms_flag = FALSE;
436 p->search_dirs_flag = FALSE;
252b5132
RH
437 break;
438 default:
439 FAIL ();
440 }
1579bae1
AM
441 p->the_bfd = NULL;
442 p->asymbols = NULL;
443 p->next_real_file = NULL;
444 p->next = NULL;
252b5132
RH
445 p->symbol_count = 0;
446 p->dynamic = config.dynamic_link;
447 p->whole_archive = whole_archive;
b34976b6 448 p->loaded = FALSE;
252b5132
RH
449 lang_statement_append (&input_file_chain,
450 (lang_statement_union_type *) p,
451 &p->next_real_file);
452 return p;
453}
454
455lang_input_statement_type *
1579bae1
AM
456lang_add_input_file (const char *name,
457 lang_input_file_enum_type file_type,
458 const char *target)
252b5132 459{
b34976b6
AM
460 lang_has_input_file = TRUE;
461 return new_afile (name, file_type, target, TRUE);
252b5132
RH
462}
463
08da4cac
KH
464/* Build enough state so that the parser can build its tree. */
465
252b5132 466void
1579bae1 467lang_init (void)
252b5132
RH
468{
469 obstack_begin (&stat_obstack, 1000);
470
471 stat_ptr = &statement_list;
472
473 lang_list_init (stat_ptr);
474
475 lang_list_init (&input_file_chain);
476 lang_list_init (&lang_output_section_statement);
477 lang_list_init (&file_chain);
1579bae1
AM
478 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
479 NULL);
08da4cac
KH
480 abs_output_section =
481 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
252b5132
RH
482
483 abs_output_section->bfd_section = bfd_abs_section_ptr;
420e579c
HPN
484
485 /* The value "3" is ad-hoc, somewhat related to the expected number of
486 DEFINED expressions in a linker script. For most default linker
487 scripts, there are none. Why a hash table then? Well, it's somewhat
488 simpler to re-use working machinery than using a linked list in terms
489 of code-complexity here in ld, besides the initialization which just
490 looks like other code here. */
491 if (bfd_hash_table_init_n (&lang_definedness_table,
492 lang_definedness_newfunc, 3) != TRUE)
493 einfo (_("%P%F: out of memory during initialization"));
494
495 /* Callers of exp_fold_tree need to increment this. */
496 lang_statement_iteration = 0;
252b5132
RH
497}
498
499/*----------------------------------------------------------------------
08da4cac
KH
500 A region is an area of memory declared with the
501 MEMORY { name:org=exp, len=exp ... }
502 syntax.
252b5132 503
08da4cac 504 We maintain a list of all the regions here.
252b5132 505
08da4cac 506 If no regions are specified in the script, then the default is used
a747ee4d
NC
507 which is created when looked up to be the entire data space.
508
509 If create is true we are creating a region inside a MEMORY block.
510 In this case it is probably an error to create a region that has
511 already been created. If we are not inside a MEMORY block it is
512 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
513 and so we issue a warning. */
252b5132
RH
514
515static lang_memory_region_type *lang_memory_region_list;
516static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
517
518lang_memory_region_type *
a747ee4d 519lang_memory_region_lookup (const char *const name, bfd_boolean create)
252b5132
RH
520{
521 lang_memory_region_type *p;
1579bae1 522 lang_memory_region_type *new;
252b5132 523
9f88b410
RS
524 /* NAME is NULL for LMA memspecs if no region was specified. */
525 if (name == NULL)
526 return NULL;
527
1579bae1 528 for (p = lang_memory_region_list; p != NULL; p = p->next)
a747ee4d
NC
529 if (strcmp (p->name, name) == 0)
530 {
531 if (create)
532 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name);
1579bae1 533 return p;
a747ee4d 534 }
252b5132
RH
535
536#if 0
537 /* This code used to always use the first region in the list as the
538 default region. I changed it to instead use a region
539 encompassing all of memory as the default region. This permits
540 NOLOAD sections to work reasonably without requiring a region.
541 People should specify what region they mean, if they really want
542 a region. */
a747ee4d 543 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
252b5132 544 {
1579bae1
AM
545 if (lang_memory_region_list != NULL)
546 return lang_memory_region_list;
252b5132
RH
547 }
548#endif
549
a747ee4d
NC
550 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
551 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
552
1579bae1 553 new = stat_alloc (sizeof (lang_memory_region_type));
252b5132 554
1579bae1
AM
555 new->name = xstrdup (name);
556 new->next = NULL;
252b5132 557
1579bae1
AM
558 *lang_memory_region_list_tail = new;
559 lang_memory_region_list_tail = &new->next;
560 new->origin = 0;
561 new->flags = 0;
562 new->not_flags = 0;
563 new->length = ~(bfd_size_type) 0;
564 new->current = 0;
565 new->had_full_message = FALSE;
252b5132 566
1579bae1 567 return new;
252b5132
RH
568}
569
5f992e62 570static lang_memory_region_type *
1579bae1 571lang_memory_default (asection *section)
252b5132
RH
572{
573 lang_memory_region_type *p;
574
575 flagword sec_flags = section->flags;
576
577 /* Override SEC_DATA to mean a writable section. */
578 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
579 sec_flags |= SEC_DATA;
580
1579bae1 581 for (p = lang_memory_region_list; p != NULL; p = p->next)
252b5132
RH
582 {
583 if ((p->flags & sec_flags) != 0
584 && (p->not_flags & sec_flags) == 0)
585 {
586 return p;
587 }
588 }
a747ee4d 589 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
252b5132
RH
590}
591
592lang_output_section_statement_type *
1579bae1 593lang_output_section_find (const char *const name)
252b5132
RH
594{
595 lang_statement_union_type *u;
596 lang_output_section_statement_type *lookup;
597
1579bae1 598 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
252b5132
RH
599 {
600 lookup = &u->output_section_statement;
601 if (strcmp (name, lookup->name) == 0)
1579bae1 602 return lookup;
252b5132 603 }
1579bae1 604 return NULL;
252b5132
RH
605}
606
607lang_output_section_statement_type *
1579bae1 608lang_output_section_statement_lookup (const char *const name)
252b5132
RH
609{
610 lang_output_section_statement_type *lookup;
611
612 lookup = lang_output_section_find (name);
1579bae1 613 if (lookup == NULL)
252b5132 614 {
1579bae1
AM
615 lookup = new_stat (lang_output_section_statement, stat_ptr);
616 lookup->region = NULL;
617 lookup->lma_region = NULL;
618 lookup->fill = 0;
252b5132
RH
619 lookup->block_value = 1;
620 lookup->name = name;
621
1579bae1
AM
622 lookup->next = NULL;
623 lookup->bfd_section = NULL;
b34976b6 624 lookup->processed = FALSE;
252b5132 625 lookup->sectype = normal_section;
1579bae1 626 lookup->addr_tree = NULL;
252b5132
RH
627 lang_list_init (&lookup->children);
628
1579bae1 629 lookup->memspec = NULL;
252b5132
RH
630 lookup->flags = 0;
631 lookup->subsection_alignment = -1;
632 lookup->section_alignment = -1;
1579bae1 633 lookup->load_base = NULL;
9f88b410 634 lookup->update_dot_tree = NULL;
252b5132
RH
635 lookup->phdrs = NULL;
636
637 lang_statement_append (&lang_output_section_statement,
638 (lang_statement_union_type *) lookup,
639 &lookup->next);
640 }
641 return lookup;
642}
643
644static void
1579bae1 645lang_map_flags (flagword flag)
252b5132
RH
646{
647 if (flag & SEC_ALLOC)
648 minfo ("a");
649
650 if (flag & SEC_CODE)
651 minfo ("x");
652
653 if (flag & SEC_READONLY)
654 minfo ("r");
655
656 if (flag & SEC_DATA)
657 minfo ("w");
658
659 if (flag & SEC_LOAD)
660 minfo ("l");
661}
662
663void
1579bae1 664lang_map (void)
252b5132
RH
665{
666 lang_memory_region_type *m;
667
668 minfo (_("\nMemory Configuration\n\n"));
669 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
670 _("Name"), _("Origin"), _("Length"), _("Attributes"));
671
1579bae1 672 for (m = lang_memory_region_list; m != NULL; m = m->next)
252b5132
RH
673 {
674 char buf[100];
675 int len;
676
677 fprintf (config.map_file, "%-16s ", m->name);
678
679 sprintf_vma (buf, m->origin);
680 minfo ("0x%s ", buf);
681 len = strlen (buf);
682 while (len < 16)
683 {
684 print_space ();
685 ++len;
686 }
687
688 minfo ("0x%V", m->length);
689 if (m->flags || m->not_flags)
690 {
691#ifndef BFD64
692 minfo (" ");
693#endif
694 if (m->flags)
695 {
696 print_space ();
697 lang_map_flags (m->flags);
698 }
699
700 if (m->not_flags)
701 {
702 minfo (" !");
703 lang_map_flags (m->not_flags);
704 }
705 }
706
707 print_nl ();
708 }
709
710 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
711
712 print_statements ();
713}
714
715/* Initialize an output section. */
716
717static void
1579bae1 718init_os (lang_output_section_statement_type *s)
252b5132
RH
719{
720 section_userdata_type *new;
721
722 if (s->bfd_section != NULL)
723 return;
724
725 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
6f9efd97 726 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
252b5132 727
1579bae1 728 new = stat_alloc (sizeof (section_userdata_type));
252b5132
RH
729
730 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1579bae1 731 if (s->bfd_section == NULL)
252b5132 732 s->bfd_section = bfd_make_section (output_bfd, s->name);
1579bae1 733 if (s->bfd_section == NULL)
252b5132
RH
734 {
735 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
736 output_bfd->xvec->name, s->name);
737 }
738 s->bfd_section->output_section = s->bfd_section;
739
08da4cac
KH
740 /* We initialize an output sections output offset to minus its own
741 vma to allow us to output a section through itself. */
252b5132 742 s->bfd_section->output_offset = 0;
1579bae1 743 get_userdata (s->bfd_section) = new;
252b5132
RH
744
745 /* If there is a base address, make sure that any sections it might
746 mention are initialized. */
747 if (s->addr_tree != NULL)
748 exp_init_os (s->addr_tree);
18794b0c
AM
749
750 if (s->load_base != NULL)
751 exp_init_os (s->load_base);
252b5132
RH
752}
753
754/* Make sure that all output sections mentioned in an expression are
755 initialized. */
756
757static void
1579bae1 758exp_init_os (etree_type *exp)
252b5132
RH
759{
760 switch (exp->type.node_class)
761 {
762 case etree_assign:
763 exp_init_os (exp->assign.src);
764 break;
765
766 case etree_binary:
767 exp_init_os (exp->binary.lhs);
768 exp_init_os (exp->binary.rhs);
769 break;
770
771 case etree_trinary:
772 exp_init_os (exp->trinary.cond);
773 exp_init_os (exp->trinary.lhs);
774 exp_init_os (exp->trinary.rhs);
775 break;
776
777 case etree_unary:
778 exp_init_os (exp->unary.child);
779 break;
780
781 case etree_name:
782 switch (exp->type.node_code)
783 {
784 case ADDR:
785 case LOADADDR:
786 case SIZEOF:
787 {
788 lang_output_section_statement_type *os;
789
790 os = lang_output_section_find (exp->name.name);
791 if (os != NULL && os->bfd_section == NULL)
792 init_os (os);
793 }
794 }
795 break;
796
797 default:
798 break;
799 }
800}
9503fd87 801\f
252b5132 802/* Sections marked with the SEC_LINK_ONCE flag should only be linked
9503fd87
ILT
803 once into the output. This routine checks each section, and
804 arrange to discard it if a section of the same name has already
bb8fe706
ILT
805 been linked. If the section has COMDAT information, then it uses
806 that to decide whether the section should be included. This code
807 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
9503fd87
ILT
808 that is, it does not depend solely upon the section name.
809 section_already_linked is called via bfd_map_over_sections. */
810
811/* This is the shape of the elements inside the already_linked hash
812 table. It maps a name onto a list of already_linked elements with
813 the same name. It's possible to get more than one element in a
814 list if the COMDAT sections have different names. */
815
5f992e62 816struct already_linked_hash_entry
9503fd87
ILT
817{
818 struct bfd_hash_entry root;
819 struct already_linked *entry;
820};
821
5f992e62 822struct already_linked
9503fd87
ILT
823{
824 struct already_linked *next;
825 asection *sec;
826};
827
828/* The hash table. */
829
830static struct bfd_hash_table already_linked_table;
252b5132 831
252b5132 832static void
1579bae1 833section_already_linked (bfd *abfd, asection *sec, void *data)
252b5132 834{
1579bae1 835 lang_input_statement_type *entry = data;
252b5132
RH
836 flagword flags;
837 const char *name;
9503fd87
ILT
838 struct already_linked *l;
839 struct already_linked_hash_entry *already_linked_list;
252b5132
RH
840
841 /* If we are only reading symbols from this object, then we want to
842 discard all sections. */
843 if (entry->just_syms_flag)
844 {
c2c01aa7 845 bfd_link_just_syms (sec, &link_info);
252b5132
RH
846 return;
847 }
848
849 flags = bfd_get_section_flags (abfd, sec);
850
851 if ((flags & SEC_LINK_ONCE) == 0)
852 return;
853
577a0623 854 /* FIXME: When doing a relocatable link, we may have trouble
e361c369
ILT
855 copying relocations in other sections that refer to local symbols
856 in the section being discarded. Those relocations will have to
857 be converted somehow; as of this writing I'm not sure that any of
858 the backends handle that correctly.
859
860 It is tempting to instead not discard link once sections when
577a0623 861 doing a relocatable link (technically, they should be discarded
e361c369
ILT
862 whenever we are building constructors). However, that fails,
863 because the linker winds up combining all the link once sections
864 into a single large link once section, which defeats the purpose
865 of having link once sections in the first place.
866
577a0623 867 Also, not merging link once sections in a relocatable link
9ad85d9b 868 causes trouble for MIPS ELF, which relies on link once semantics
e361c369
ILT
869 to handle the .reginfo section correctly. */
870
252b5132
RH
871 name = bfd_get_section_name (abfd, sec);
872
5f992e62 873 already_linked_list =
9503fd87 874 ((struct already_linked_hash_entry *)
b34976b6 875 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
9503fd87 876
08da4cac 877 for (l = already_linked_list->entry; l != NULL; l = l->next)
252b5132 878 {
9503fd87
ILT
879 if (sec->comdat == NULL
880 || l->sec->comdat == NULL
881 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
252b5132
RH
882 {
883 /* The section has already been linked. See if we should
884 issue a warning. */
885 switch (flags & SEC_LINK_DUPLICATES)
886 {
887 default:
888 abort ();
889
890 case SEC_LINK_DUPLICATES_DISCARD:
891 break;
892
893 case SEC_LINK_DUPLICATES_ONE_ONLY:
bb8fe706
ILT
894 if (sec->comdat == NULL)
895 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
896 abfd, name);
897 else
898 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
899 abfd, name, sec->comdat->name);
252b5132
RH
900 break;
901
902 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
903 /* FIXME: We should really dig out the contents of both
904 sections and memcmp them. The COFF/PE spec says that
905 the Microsoft linker does not implement this
906 correctly, so I'm not going to bother doing it
907 either. */
908 /* Fall through. */
909 case SEC_LINK_DUPLICATES_SAME_SIZE:
910 if (bfd_section_size (abfd, sec)
911 != bfd_section_size (l->sec->owner, l->sec))
912 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
913 abfd, name);
914 break;
915 }
916
39dcfe18
AM
917 /* Set the output_section field so that lang_add_section
918 does not create a lang_input_section structure for this
f97b9cb8
L
919 section. Since there might be a symbol in the section
920 being discarded, we must retain a pointer to the section
921 which we are really going to use. */
252b5132 922 sec->output_section = bfd_abs_section_ptr;
f97b9cb8 923 sec->kept_section = l->sec;
252b5132 924
9ad85d9b
AM
925 if (flags & SEC_GROUP)
926 bfd_discard_group (abfd, sec);
927
252b5132
RH
928 return;
929 }
930 }
931
9503fd87
ILT
932 /* This is the first section with this name. Record it. Allocate
933 the memory from the same obstack as the hash table is kept in. */
934
1579bae1 935 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
252b5132 936
252b5132 937 l->sec = sec;
9503fd87
ILT
938 l->next = already_linked_list->entry;
939 already_linked_list->entry = l;
940}
941
942/* Support routines for the hash table used by section_already_linked,
943 initialize the table, fill in an entry and remove the table. */
944
945static struct bfd_hash_entry *
1579bae1
AM
946already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
947 struct bfd_hash_table *table,
948 const char *string ATTRIBUTE_UNUSED)
9503fd87 949{
5f992e62 950 struct already_linked_hash_entry *ret =
9503fd87
ILT
951 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
952
953 ret->entry = NULL;
954
1579bae1 955 return &ret->root;
9503fd87
ILT
956}
957
958static void
1579bae1 959already_linked_table_init (void)
9503fd87
ILT
960{
961 if (! bfd_hash_table_init_n (&already_linked_table,
962 already_linked_newfunc,
963 42))
964 einfo (_("%P%F: Failed to create hash table\n"));
965}
966
967static void
1579bae1 968already_linked_table_free (void)
9503fd87
ILT
969{
970 bfd_hash_table_free (&already_linked_table);
252b5132
RH
971}
972\f
973/* The wild routines.
974
975 These expand statements like *(.text) and foo.o to a list of
976 explicit actions, like foo.o(.text), bar.o(.text) and
977 foo.o(.text, .data). */
978
b34976b6 979/* Return TRUE if the PATTERN argument is a wildcard pattern.
252b5132
RH
980 Although backslashes are treated specially if a pattern contains
981 wildcards, we do not consider the mere presence of a backslash to
ca0c1d3e 982 be enough to cause the pattern to be treated as a wildcard.
252b5132
RH
983 That lets us handle DOS filenames more naturally. */
984
b34976b6 985static bfd_boolean
1579bae1 986wildcardp (const char *pattern)
252b5132
RH
987{
988 const char *s;
989
990 for (s = pattern; *s != '\0'; ++s)
991 if (*s == '?'
992 || *s == '*'
993 || *s == '[')
b34976b6
AM
994 return TRUE;
995 return FALSE;
252b5132
RH
996}
997
998/* Add SECTION to the output section OUTPUT. Do this by creating a
999 lang_input_section statement which is placed at PTR. FILE is the
1000 input file which holds SECTION. */
1001
1002void
1579bae1
AM
1003lang_add_section (lang_statement_list_type *ptr,
1004 asection *section,
1005 lang_output_section_statement_type *output,
1006 lang_input_statement_type *file)
252b5132
RH
1007{
1008 flagword flags;
b34976b6 1009 bfd_boolean discard;
252b5132
RH
1010
1011 flags = bfd_get_section_flags (section->owner, section);
1012
b34976b6 1013 discard = FALSE;
252b5132 1014
c4c41219
AM
1015 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1016 link. Discard debugging sections marked with SEC_EXCLUDE on a
1017 relocatable link too. */
1018 if ((flags & SEC_EXCLUDE) != 0
1049f94e 1019 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
b34976b6 1020 discard = TRUE;
252b5132
RH
1021
1022 /* Discard input sections which are assigned to a section named
1023 DISCARD_SECTION_NAME. */
1024 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
b34976b6 1025 discard = TRUE;
252b5132
RH
1026
1027 /* Discard debugging sections if we are stripping debugging
1028 information. */
1029 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1030 && (flags & SEC_DEBUGGING) != 0)
b34976b6 1031 discard = TRUE;
252b5132
RH
1032
1033 if (discard)
1034 {
1035 if (section->output_section == NULL)
1036 {
1037 /* This prevents future calls from assigning this section. */
1038 section->output_section = bfd_abs_section_ptr;
1039 }
1040 return;
1041 }
1042
1043 if (section->output_section == NULL)
1044 {
b34976b6 1045 bfd_boolean first;
252b5132
RH
1046 lang_input_section_type *new;
1047 flagword flags;
1048
1049 if (output->bfd_section == NULL)
d1778b88
AM
1050 init_os (output);
1051
1052 first = ! output->bfd_section->linker_has_input;
1053 output->bfd_section->linker_has_input = 1;
252b5132 1054
08da4cac 1055 /* Add a section reference to the list. */
252b5132
RH
1056 new = new_stat (lang_input_section, ptr);
1057
1058 new->section = section;
1059 new->ifile = file;
1060 section->output_section = output->bfd_section;
1061
1062 flags = section->flags;
1063
1064 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1065 to an output section, because we want to be able to include a
1066 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1067 section (I don't know why we want to do this, but we do).
1068 build_link_order in ldwrite.c handles this case by turning
1069 the embedded SEC_NEVER_LOAD section into a fill. */
1070
1071 flags &= ~ SEC_NEVER_LOAD;
1072
1073 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1074 already been processed. One reason to do this is that on pe
1075 format targets, .text$foo sections go into .text and it's odd
1076 to see .text with SEC_LINK_ONCE set. */
1077
1049f94e 1078 if (! link_info.relocatable)
252b5132
RH
1079 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1080
1081 /* If this is not the first input section, and the SEC_READONLY
1082 flag is not currently set, then don't set it just because the
1083 input section has it set. */
1084
1085 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1086 flags &= ~ SEC_READONLY;
1087
f5fa8ca2
JJ
1088 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1089 if (! first
1090 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1091 != (flags & (SEC_MERGE | SEC_STRINGS))
1092 || ((flags & SEC_MERGE)
1093 && section->output_section->entsize != section->entsize)))
1094 {
1095 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1096 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1097 }
1098
13ae64f3 1099 /* For now make .tbss normal section. */
1049f94e 1100 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocatable)
13ae64f3
JJ
1101 flags |= SEC_LOAD;
1102
252b5132
RH
1103 section->output_section->flags |= flags;
1104
f5fa8ca2
JJ
1105 if (flags & SEC_MERGE)
1106 section->output_section->entsize = section->entsize;
1107
252b5132
RH
1108 /* If SEC_READONLY is not set in the input section, then clear
1109 it from the output section. */
1110 if ((section->flags & SEC_READONLY) == 0)
1111 section->output_section->flags &= ~SEC_READONLY;
1112
1113 switch (output->sectype)
1114 {
1115 case normal_section:
1116 break;
1117 case dsect_section:
1118 case copy_section:
1119 case info_section:
1120 case overlay_section:
1121 output->bfd_section->flags &= ~SEC_ALLOC;
1122 break;
1123 case noload_section:
1124 output->bfd_section->flags &= ~SEC_LOAD;
1125 output->bfd_section->flags |= SEC_NEVER_LOAD;
1126 break;
1127 }
1128
667f5177
ILT
1129 /* Copy over SEC_SMALL_DATA. */
1130 if (section->flags & SEC_SMALL_DATA)
1131 section->output_section->flags |= SEC_SMALL_DATA;
9e41f973 1132
252b5132
RH
1133 if (section->alignment_power > output->bfd_section->alignment_power)
1134 output->bfd_section->alignment_power = section->alignment_power;
1135
396a2467 1136 /* If supplied an alignment, then force it. */
252b5132
RH
1137 if (output->section_alignment != -1)
1138 output->bfd_section->alignment_power = output->section_alignment;
74459f0e
TW
1139
1140 if (section->flags & SEC_BLOCK)
08da4cac
KH
1141 {
1142 section->output_section->flags |= SEC_BLOCK;
1143 /* FIXME: This value should really be obtained from the bfd... */
1144 output->block_value = 128;
1145 }
252b5132
RH
1146 }
1147}
1148
1149/* Handle wildcard sorting. This returns the lang_input_section which
1150 should follow the one we are going to create for SECTION and FILE,
1151 based on the sorting requirements of WILD. It returns NULL if the
1152 new section should just go at the end of the current list. */
1153
1154static lang_statement_union_type *
1579bae1
AM
1155wild_sort (lang_wild_statement_type *wild,
1156 struct wildcard_list *sec,
1157 lang_input_statement_type *file,
1158 asection *section)
252b5132
RH
1159{
1160 const char *section_name;
1161 lang_statement_union_type *l;
1162
b6bf44ba 1163 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
252b5132
RH
1164 return NULL;
1165
1166 section_name = bfd_get_section_name (file->the_bfd, section);
bba1a0c0 1167 for (l = wild->children.head; l != NULL; l = l->header.next)
252b5132
RH
1168 {
1169 lang_input_section_type *ls;
1170
1171 if (l->header.type != lang_input_section_enum)
1172 continue;
1173 ls = &l->input_section;
1174
1175 /* Sorting by filename takes precedence over sorting by section
1176 name. */
1177
1178 if (wild->filenames_sorted)
1179 {
1180 const char *fn, *ln;
b34976b6 1181 bfd_boolean fa, la;
252b5132
RH
1182 int i;
1183
1184 /* The PE support for the .idata section as generated by
1185 dlltool assumes that files will be sorted by the name of
1186 the archive and then the name of the file within the
1187 archive. */
1188
1189 if (file->the_bfd != NULL
1190 && bfd_my_archive (file->the_bfd) != NULL)
1191 {
1192 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
b34976b6 1193 fa = TRUE;
252b5132
RH
1194 }
1195 else
1196 {
1197 fn = file->filename;
b34976b6 1198 fa = FALSE;
252b5132
RH
1199 }
1200
1201 if (ls->ifile->the_bfd != NULL
1202 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1203 {
1204 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
b34976b6 1205 la = TRUE;
252b5132
RH
1206 }
1207 else
1208 {
1209 ln = ls->ifile->filename;
b34976b6 1210 la = FALSE;
252b5132
RH
1211 }
1212
1213 i = strcmp (fn, ln);
1214 if (i > 0)
1215 continue;
1216 else if (i < 0)
1217 break;
1218
1219 if (fa || la)
1220 {
1221 if (fa)
1222 fn = file->filename;
1223 if (la)
1224 ln = ls->ifile->filename;
1225
1226 i = strcmp (fn, ln);
1227 if (i > 0)
1228 continue;
1229 else if (i < 0)
1230 break;
1231 }
1232 }
1233
1234 /* Here either the files are not sorted by name, or we are
1235 looking at the sections for this file. */
1236
b6bf44ba 1237 if (sec != NULL && sec->spec.sorted)
252b5132
RH
1238 {
1239 if (strcmp (section_name,
1240 bfd_get_section_name (ls->ifile->the_bfd,
1241 ls->section))
1242 < 0)
1243 break;
1244 }
1245 }
1246
1247 return l;
1248}
1249
1250/* Expand a wild statement for a particular FILE. SECTION may be
1251 NULL, in which case it is a wild card. */
1252
1253static void
1579bae1
AM
1254output_section_callback (lang_wild_statement_type *ptr,
1255 struct wildcard_list *sec,
1256 asection *section,
1257 lang_input_statement_type *file,
1258 void *output)
4dec4d4e
RH
1259{
1260 lang_statement_union_type *before;
5f992e62 1261
b6bf44ba
AM
1262 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1263 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1264 return;
1265
4dec4d4e
RH
1266 /* If the wild pattern was marked KEEP, the member sections
1267 should be as well. */
1268 if (ptr->keep_sections)
1269 section->flags |= SEC_KEEP;
5f992e62 1270
b6bf44ba 1271 before = wild_sort (ptr, sec, file, section);
5f992e62 1272
4dec4d4e
RH
1273 /* Here BEFORE points to the lang_input_section which
1274 should follow the one we are about to add. If BEFORE
1275 is NULL, then the section should just go at the end
1276 of the current list. */
5f992e62 1277
4dec4d4e 1278 if (before == NULL)
39dcfe18
AM
1279 lang_add_section (&ptr->children, section,
1280 (lang_output_section_statement_type *) output,
1281 file);
4dec4d4e 1282 else
252b5132 1283 {
4dec4d4e
RH
1284 lang_statement_list_type list;
1285 lang_statement_union_type **pp;
5f992e62 1286
4dec4d4e 1287 lang_list_init (&list);
39dcfe18
AM
1288 lang_add_section (&list, section,
1289 (lang_output_section_statement_type *) output,
1290 file);
5f992e62 1291
4dec4d4e
RH
1292 /* If we are discarding the section, LIST.HEAD will
1293 be NULL. */
1294 if (list.head != NULL)
252b5132 1295 {
bba1a0c0 1296 ASSERT (list.head->header.next == NULL);
5f992e62 1297
4dec4d4e
RH
1298 for (pp = &ptr->children.head;
1299 *pp != before;
bba1a0c0 1300 pp = &(*pp)->header.next)
4dec4d4e 1301 ASSERT (*pp != NULL);
5f992e62 1302
bba1a0c0 1303 list.head->header.next = *pp;
4dec4d4e 1304 *pp = list.head;
252b5132
RH
1305 }
1306 }
1307}
1308
1309/* This is passed a file name which must have been seen already and
1310 added to the statement tree. We will see if it has been opened
1311 already and had its symbols read. If not then we'll read it. */
1312
1313static lang_input_statement_type *
1579bae1 1314lookup_name (const char *name)
252b5132
RH
1315{
1316 lang_input_statement_type *search;
1317
1318 for (search = (lang_input_statement_type *) input_file_chain.head;
1579bae1 1319 search != NULL;
252b5132
RH
1320 search = (lang_input_statement_type *) search->next_real_file)
1321 {
1579bae1 1322 if (search->filename == NULL && name == NULL)
252b5132 1323 return search;
1579bae1
AM
1324 if (search->filename != NULL
1325 && name != NULL
252b5132
RH
1326 && strcmp (search->filename, name) == 0)
1327 break;
1328 }
1329
1579bae1 1330 if (search == NULL)
252b5132 1331 search = new_afile (name, lang_input_file_is_file_enum, default_target,
b34976b6 1332 FALSE);
252b5132
RH
1333
1334 /* If we have already added this file, or this file is not real
1335 (FIXME: can that ever actually happen?) or the name is NULL
1336 (FIXME: can that ever actually happen?) don't add this file. */
1337 if (search->loaded
1338 || ! search->real
1579bae1 1339 || search->filename == NULL)
252b5132
RH
1340 return search;
1341
1579bae1 1342 if (! load_symbols (search, NULL))
6770ec8c 1343 return NULL;
252b5132
RH
1344
1345 return search;
1346}
1347
1348/* Get the symbols for an input file. */
1349
b34976b6 1350static bfd_boolean
1579bae1
AM
1351load_symbols (lang_input_statement_type *entry,
1352 lang_statement_list_type *place)
252b5132
RH
1353{
1354 char **matching;
1355
1356 if (entry->loaded)
b34976b6 1357 return TRUE;
252b5132
RH
1358
1359 ldfile_open_file (entry);
1360
1361 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1362 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1363 {
1364 bfd_error_type err;
1365 lang_statement_list_type *hold;
b34976b6 1366 bfd_boolean bad_load = TRUE;
e3f2db7f 1367 bfd_boolean save_ldlang_sysrooted_script;
b7a26f91 1368
252b5132 1369 err = bfd_get_error ();
884fb58e
NC
1370
1371 /* See if the emulation has some special knowledge. */
1372 if (ldemul_unrecognized_file (entry))
b34976b6 1373 return TRUE;
884fb58e 1374
252b5132
RH
1375 if (err == bfd_error_file_ambiguously_recognized)
1376 {
1377 char **p;
1378
1379 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1380 einfo (_("%B: matching formats:"), entry->the_bfd);
1381 for (p = matching; *p != NULL; p++)
1382 einfo (" %s", *p);
1383 einfo ("%F\n");
1384 }
1385 else if (err != bfd_error_file_not_recognized
1386 || place == NULL)
6770ec8c
NC
1387 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1388 else
b34976b6 1389 bad_load = FALSE;
b7a26f91 1390
252b5132
RH
1391 bfd_close (entry->the_bfd);
1392 entry->the_bfd = NULL;
1393
252b5132 1394 /* Try to interpret the file as a linker script. */
252b5132
RH
1395 ldfile_open_command_file (entry->filename);
1396
1397 hold = stat_ptr;
1398 stat_ptr = place;
e3f2db7f
AO
1399 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1400 ldlang_sysrooted_script = entry->sysrooted;
252b5132 1401
b34976b6 1402 ldfile_assumed_script = TRUE;
252b5132
RH
1403 parser_input = input_script;
1404 yyparse ();
b34976b6 1405 ldfile_assumed_script = FALSE;
252b5132 1406
e3f2db7f 1407 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
252b5132
RH
1408 stat_ptr = hold;
1409
6770ec8c 1410 return ! bad_load;
252b5132
RH
1411 }
1412
1413 if (ldemul_recognized_file (entry))
b34976b6 1414 return TRUE;
252b5132
RH
1415
1416 /* We don't call ldlang_add_file for an archive. Instead, the
1417 add_symbols entry point will call ldlang_add_file, via the
1418 add_archive_element callback, for each element of the archive
1419 which is used. */
1420 switch (bfd_get_format (entry->the_bfd))
1421 {
1422 default:
1423 break;
1424
1425 case bfd_object:
1426 ldlang_add_file (entry);
1427 if (trace_files || trace_file_tries)
1428 info_msg ("%I\n", entry);
1429 break;
1430
1431 case bfd_archive:
1432 if (entry->whole_archive)
1433 {
b7a26f91 1434 bfd *member = NULL;
b34976b6 1435 bfd_boolean loaded = TRUE;
6770ec8c
NC
1436
1437 for (;;)
252b5132 1438 {
6770ec8c
NC
1439 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1440
1441 if (member == NULL)
1442 break;
b7a26f91 1443
252b5132 1444 if (! bfd_check_format (member, bfd_object))
6770ec8c
NC
1445 {
1446 einfo (_("%F%B: member %B in archive is not an object\n"),
1447 entry->the_bfd, member);
b34976b6 1448 loaded = FALSE;
6770ec8c
NC
1449 }
1450
252b5132
RH
1451 if (! ((*link_info.callbacks->add_archive_element)
1452 (&link_info, member, "--whole-archive")))
1453 abort ();
6770ec8c 1454
252b5132 1455 if (! bfd_link_add_symbols (member, &link_info))
6770ec8c
NC
1456 {
1457 einfo (_("%F%B: could not read symbols: %E\n"), member);
b34976b6 1458 loaded = FALSE;
6770ec8c 1459 }
252b5132
RH
1460 }
1461
6770ec8c
NC
1462 entry->loaded = loaded;
1463 return loaded;
252b5132 1464 }
6770ec8c 1465 break;
252b5132
RH
1466 }
1467
03bdc404 1468 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
b34976b6 1469 entry->loaded = TRUE;
6770ec8c 1470 else
252b5132
RH
1471 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1472
6770ec8c 1473 return entry->loaded;
252b5132
RH
1474}
1475
b6bf44ba
AM
1476/* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1477 may be NULL, indicating that it is a wildcard. Separate
1478 lang_input_section statements are created for each part of the
1479 expansion; they are added after the wild statement S. OUTPUT is
1480 the output section. */
252b5132
RH
1481
1482static void
1579bae1
AM
1483wild (lang_wild_statement_type *s,
1484 const char *target ATTRIBUTE_UNUSED,
1485 lang_output_section_statement_type *output)
252b5132 1486{
b6bf44ba 1487 struct wildcard_list *sec;
252b5132 1488
1579bae1 1489 walk_wild (s, output_section_callback, output);
b6bf44ba
AM
1490
1491 for (sec = s->section_list; sec != NULL; sec = sec->next)
252b5132 1492 {
b6bf44ba
AM
1493 if (default_common_section != NULL)
1494 break;
1495 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1496 {
1497 /* Remember the section that common is going to in case we
b7a26f91 1498 later get something which doesn't know where to put it. */
b6bf44ba
AM
1499 default_common_section = output;
1500 }
252b5132
RH
1501 }
1502}
1503
b34976b6 1504/* Return TRUE iff target is the sought target. */
08da4cac 1505
e50d8076 1506static int
1579bae1 1507get_target (const bfd_target *target, void *data)
e50d8076 1508{
1579bae1 1509 const char *sought = data;
5f992e62 1510
e50d8076
NC
1511 return strcmp (target->name, sought) == 0;
1512}
1513
1514/* Like strcpy() but convert to lower case as well. */
08da4cac 1515
e50d8076 1516static void
1579bae1 1517stricpy (char *dest, char *src)
e50d8076
NC
1518{
1519 char c;
5f992e62 1520
08da4cac 1521 while ((c = *src++) != 0)
3882b010 1522 *dest++ = TOLOWER (c);
e50d8076 1523
08da4cac 1524 *dest = 0;
e50d8076
NC
1525}
1526
396a2467 1527/* Remove the first occurrence of needle (if any) in haystack
e50d8076 1528 from haystack. */
08da4cac 1529
e50d8076 1530static void
1579bae1 1531strcut (char *haystack, char *needle)
e50d8076
NC
1532{
1533 haystack = strstr (haystack, needle);
5f992e62 1534
e50d8076
NC
1535 if (haystack)
1536 {
08da4cac 1537 char *src;
e50d8076 1538
08da4cac
KH
1539 for (src = haystack + strlen (needle); *src;)
1540 *haystack++ = *src++;
5f992e62 1541
08da4cac 1542 *haystack = 0;
e50d8076
NC
1543 }
1544}
1545
1546/* Compare two target format name strings.
1547 Return a value indicating how "similar" they are. */
08da4cac 1548
e50d8076 1549static int
1579bae1 1550name_compare (char *first, char *second)
e50d8076 1551{
08da4cac
KH
1552 char *copy1;
1553 char *copy2;
1554 int result;
5f992e62 1555
e50d8076
NC
1556 copy1 = xmalloc (strlen (first) + 1);
1557 copy2 = xmalloc (strlen (second) + 1);
1558
1559 /* Convert the names to lower case. */
1560 stricpy (copy1, first);
1561 stricpy (copy2, second);
1562
1579bae1 1563 /* Remove size and endian strings from the name. */
e50d8076
NC
1564 strcut (copy1, "big");
1565 strcut (copy1, "little");
1566 strcut (copy2, "big");
1567 strcut (copy2, "little");
1568
1569 /* Return a value based on how many characters match,
1570 starting from the beginning. If both strings are
1571 the same then return 10 * their length. */
08da4cac
KH
1572 for (result = 0; copy1[result] == copy2[result]; result++)
1573 if (copy1[result] == 0)
e50d8076
NC
1574 {
1575 result *= 10;
1576 break;
1577 }
5f992e62 1578
e50d8076
NC
1579 free (copy1);
1580 free (copy2);
1581
1582 return result;
1583}
1584
1585/* Set by closest_target_match() below. */
08da4cac 1586static const bfd_target *winner;
e50d8076
NC
1587
1588/* Scan all the valid bfd targets looking for one that has the endianness
1589 requirement that was specified on the command line, and is the nearest
1590 match to the original output target. */
08da4cac 1591
e50d8076 1592static int
1579bae1 1593closest_target_match (const bfd_target *target, void *data)
e50d8076 1594{
1579bae1 1595 const bfd_target *original = data;
5f992e62 1596
08da4cac
KH
1597 if (command_line.endian == ENDIAN_BIG
1598 && target->byteorder != BFD_ENDIAN_BIG)
e50d8076 1599 return 0;
5f992e62 1600
08da4cac
KH
1601 if (command_line.endian == ENDIAN_LITTLE
1602 && target->byteorder != BFD_ENDIAN_LITTLE)
e50d8076
NC
1603 return 0;
1604
1605 /* Must be the same flavour. */
1606 if (target->flavour != original->flavour)
1607 return 0;
1608
1609 /* If we have not found a potential winner yet, then record this one. */
1610 if (winner == NULL)
1611 {
1612 winner = target;
1613 return 0;
1614 }
1615
1616 /* Oh dear, we now have two potential candidates for a successful match.
4de2d33d 1617 Compare their names and choose the better one. */
d1778b88
AM
1618 if (name_compare (target->name, original->name)
1619 > name_compare (winner->name, original->name))
e50d8076
NC
1620 winner = target;
1621
1622 /* Keep on searching until wqe have checked them all. */
1623 return 0;
1624}
1625
1626/* Return the BFD target format of the first input file. */
08da4cac 1627
e50d8076 1628static char *
1579bae1 1629get_first_input_target (void)
e50d8076 1630{
08da4cac 1631 char *target = NULL;
e50d8076
NC
1632
1633 LANG_FOR_EACH_INPUT_STATEMENT (s)
1634 {
1635 if (s->header.type == lang_input_statement_enum
1636 && s->real)
1637 {
1638 ldfile_open_file (s);
5f992e62 1639
e50d8076
NC
1640 if (s->the_bfd != NULL
1641 && bfd_check_format (s->the_bfd, bfd_object))
1642 {
1643 target = bfd_get_target (s->the_bfd);
5f992e62 1644
e50d8076
NC
1645 if (target != NULL)
1646 break;
1647 }
1648 }
1649 }
5f992e62 1650
e50d8076
NC
1651 return target;
1652}
1653
599917b8 1654const char *
1579bae1 1655lang_get_output_target (void)
599917b8
JJ
1656{
1657 const char *target;
1658
1659 /* Has the user told us which output format to use? */
1579bae1 1660 if (output_target != NULL)
599917b8
JJ
1661 return output_target;
1662
1663 /* No - has the current target been set to something other than
1664 the default? */
1665 if (current_target != default_target)
1666 return current_target;
1667
1668 /* No - can we determine the format of the first input file? */
1669 target = get_first_input_target ();
1670 if (target != NULL)
1671 return target;
1672
1673 /* Failed - use the default output target. */
1674 return default_target;
1675}
1676
252b5132
RH
1677/* Open the output file. */
1678
1679static bfd *
1579bae1 1680open_output (const char *name)
252b5132 1681{
08da4cac 1682 bfd *output;
252b5132 1683
599917b8 1684 output_target = lang_get_output_target ();
5f992e62 1685
08da4cac
KH
1686 /* Has the user requested a particular endianness on the command
1687 line? */
e50d8076
NC
1688 if (command_line.endian != ENDIAN_UNSET)
1689 {
08da4cac 1690 const bfd_target *target;
1b69a0bf 1691 enum bfd_endian desired_endian;
e50d8076
NC
1692
1693 /* Get the chosen target. */
1579bae1 1694 target = bfd_search_for_target (get_target, (void *) output_target);
e50d8076 1695
c13b1b77
NC
1696 /* If the target is not supported, we cannot do anything. */
1697 if (target != NULL)
e50d8076 1698 {
c13b1b77
NC
1699 if (command_line.endian == ENDIAN_BIG)
1700 desired_endian = BFD_ENDIAN_BIG;
e50d8076 1701 else
c13b1b77 1702 desired_endian = BFD_ENDIAN_LITTLE;
5f992e62
AM
1703
1704 /* See if the target has the wrong endianness. This should
1705 not happen if the linker script has provided big and
1706 little endian alternatives, but some scrips don't do
1707 this. */
c13b1b77 1708 if (target->byteorder != desired_endian)
e50d8076 1709 {
c13b1b77
NC
1710 /* If it does, then see if the target provides
1711 an alternative with the correct endianness. */
1712 if (target->alternative_target != NULL
1713 && (target->alternative_target->byteorder == desired_endian))
1714 output_target = target->alternative_target->name;
e50d8076 1715 else
c13b1b77 1716 {
5f992e62
AM
1717 /* Try to find a target as similar as possible to
1718 the default target, but which has the desired
1719 endian characteristic. */
1579bae1
AM
1720 bfd_search_for_target (closest_target_match,
1721 (void *) target);
5f992e62
AM
1722
1723 /* Oh dear - we could not find any targets that
1724 satisfy our requirements. */
c13b1b77
NC
1725 if (winner == NULL)
1726 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1727 else
1728 output_target = winner->name;
1729 }
e50d8076
NC
1730 }
1731 }
252b5132 1732 }
5f992e62 1733
252b5132
RH
1734 output = bfd_openw (name, output_target);
1735
1579bae1 1736 if (output == NULL)
252b5132
RH
1737 {
1738 if (bfd_get_error () == bfd_error_invalid_target)
e50d8076
NC
1739 einfo (_("%P%F: target %s not found\n"), output_target);
1740
252b5132
RH
1741 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1742 }
1743
b34976b6 1744 delete_output_file_on_failure = TRUE;
252b5132 1745
08da4cac
KH
1746#if 0
1747 output->flags |= D_PAGED;
1748#endif
252b5132
RH
1749
1750 if (! bfd_set_format (output, bfd_object))
1751 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1752 if (! bfd_set_arch_mach (output,
1753 ldfile_output_architecture,
1754 ldfile_output_machine))
1755 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1756
1757 link_info.hash = bfd_link_hash_table_create (output);
1579bae1 1758 if (link_info.hash == NULL)
252b5132
RH
1759 einfo (_("%P%F: can not create link hash table: %E\n"));
1760
1761 bfd_set_gp_size (output, g_switch_value);
1762 return output;
1763}
1764
252b5132 1765static void
1579bae1 1766ldlang_open_output (lang_statement_union_type *statement)
252b5132
RH
1767{
1768 switch (statement->header.type)
1769 {
1770 case lang_output_statement_enum:
1579bae1 1771 ASSERT (output_bfd == NULL);
252b5132
RH
1772 output_bfd = open_output (statement->output_statement.name);
1773 ldemul_set_output_arch ();
1049f94e 1774 if (config.magic_demand_paged && !link_info.relocatable)
252b5132
RH
1775 output_bfd->flags |= D_PAGED;
1776 else
1777 output_bfd->flags &= ~D_PAGED;
1778 if (config.text_read_only)
1779 output_bfd->flags |= WP_TEXT;
1780 else
1781 output_bfd->flags &= ~WP_TEXT;
1782 if (link_info.traditional_format)
1783 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1784 else
1785 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1786 break;
1787
1788 case lang_target_statement_enum:
1789 current_target = statement->target_statement.target;
1790 break;
1791 default:
1792 break;
1793 }
1794}
1795
1796/* Open all the input files. */
1797
1798static void
1579bae1 1799open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
252b5132 1800{
1579bae1 1801 for (; s != NULL; s = s->header.next)
252b5132
RH
1802 {
1803 switch (s->header.type)
1804 {
1805 case lang_constructors_statement_enum:
1806 open_input_bfds (constructor_list.head, force);
1807 break;
1808 case lang_output_section_statement_enum:
1809 open_input_bfds (s->output_section_statement.children.head, force);
1810 break;
1811 case lang_wild_statement_enum:
08da4cac 1812 /* Maybe we should load the file's symbols. */
252b5132
RH
1813 if (s->wild_statement.filename
1814 && ! wildcardp (s->wild_statement.filename))
1815 (void) lookup_name (s->wild_statement.filename);
1816 open_input_bfds (s->wild_statement.children.head, force);
1817 break;
1818 case lang_group_statement_enum:
1819 {
1820 struct bfd_link_hash_entry *undefs;
1821
1822 /* We must continually search the entries in the group
08da4cac
KH
1823 until no new symbols are added to the list of undefined
1824 symbols. */
252b5132
RH
1825
1826 do
1827 {
1828 undefs = link_info.hash->undefs_tail;
b34976b6 1829 open_input_bfds (s->group_statement.children.head, TRUE);
252b5132
RH
1830 }
1831 while (undefs != link_info.hash->undefs_tail);
1832 }
1833 break;
1834 case lang_target_statement_enum:
1835 current_target = s->target_statement.target;
1836 break;
1837 case lang_input_statement_enum:
e50d8076 1838 if (s->input_statement.real)
252b5132
RH
1839 {
1840 lang_statement_list_type add;
1841
1842 s->input_statement.target = current_target;
1843
1844 /* If we are being called from within a group, and this
1845 is an archive which has already been searched, then
cd4c806a
L
1846 force it to be researched unless the whole archive
1847 has been loaded already. */
252b5132 1848 if (force
cd4c806a 1849 && !s->input_statement.whole_archive
252b5132
RH
1850 && s->input_statement.loaded
1851 && bfd_check_format (s->input_statement.the_bfd,
1852 bfd_archive))
b34976b6 1853 s->input_statement.loaded = FALSE;
252b5132 1854
d1778b88 1855 lang_list_init (&add);
1276aefa 1856
6770ec8c 1857 if (! load_symbols (&s->input_statement, &add))
b34976b6 1858 config.make_executable = FALSE;
252b5132
RH
1859
1860 if (add.head != NULL)
1861 {
bba1a0c0
AM
1862 *add.tail = s->header.next;
1863 s->header.next = add.head;
252b5132
RH
1864 }
1865 }
1866 break;
1867 default:
1868 break;
1869 }
1870 }
1871}
1872
08da4cac
KH
1873/* If there are [COMMONS] statements, put a wild one into the bss
1874 section. */
252b5132
RH
1875
1876static void
1579bae1 1877lang_reasonable_defaults (void)
252b5132
RH
1878{
1879#if 0
1880 lang_output_section_statement_lookup (".text");
1881 lang_output_section_statement_lookup (".data");
1882
08da4cac 1883 default_common_section = lang_output_section_statement_lookup (".bss");
252b5132 1884
7c519c12 1885 if (!placed_commons)
252b5132
RH
1886 {
1887 lang_wild_statement_type *new =
1888 new_stat (lang_wild_statement,
1889 &default_common_section->children);
1890
1891 new->section_name = "COMMON";
1579bae1 1892 new->filename = NULL;
252b5132
RH
1893 lang_list_init (&new->children);
1894 }
1895#endif
252b5132
RH
1896}
1897
420e579c
HPN
1898/* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1899
1900void
1901lang_track_definedness (const char *name)
1902{
1903 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1904 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1905}
1906
1907/* New-function for the definedness hash table. */
1908
1909static struct bfd_hash_entry *
1910lang_definedness_newfunc (struct bfd_hash_entry *entry,
1911 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1912 const char *name ATTRIBUTE_UNUSED)
1913{
1914 struct lang_definedness_hash_entry *ret
1915 = (struct lang_definedness_hash_entry *) entry;
1916
1917 if (ret == NULL)
1918 ret = (struct lang_definedness_hash_entry *)
1919 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1920
1921 if (ret == NULL)
1922 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1923
1924 ret->iteration = -1;
1925 return &ret->root;
1926}
1927
1928/* Return the iteration when the definition of NAME was last updated. A
1929 value of -1 means that the symbol is not defined in the linker script
1930 or the command line, but may be defined in the linker symbol table. */
1931
1932int
1933lang_symbol_definition_iteration (const char *name)
1934{
1935 struct lang_definedness_hash_entry *defentry
1936 = (struct lang_definedness_hash_entry *)
1937 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1938
1939 /* We've already created this one on the presence of DEFINED in the
1940 script, so it can't be NULL unless something is borked elsewhere in
1941 the code. */
1942 if (defentry == NULL)
1943 FAIL ();
1944
1945 return defentry->iteration;
1946}
1947
1948/* Update the definedness state of NAME. */
1949
1950void
1951lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1952{
1953 struct lang_definedness_hash_entry *defentry
1954 = (struct lang_definedness_hash_entry *)
1955 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1956
1957 /* We don't keep track of symbols not tested with DEFINED. */
1958 if (defentry == NULL)
1959 return;
1960
1961 /* If the symbol was already defined, and not from an earlier statement
1962 iteration, don't update the definedness iteration, because that'd
1963 make the symbol seem defined in the linker script at this point, and
1964 it wasn't; it was defined in some object. If we do anyway, DEFINED
1965 would start to yield false before this point and the construct "sym =
1966 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1967 in an object. */
1968 if (h->type != bfd_link_hash_undefined
1969 && h->type != bfd_link_hash_common
1970 && h->type != bfd_link_hash_new
1971 && defentry->iteration == -1)
1972 return;
1973
1974 defentry->iteration = lang_statement_iteration;
1975}
1976
08da4cac 1977/* Add the supplied name to the symbol table as an undefined reference.
fcf0e35b
AM
1978 This is a two step process as the symbol table doesn't even exist at
1979 the time the ld command line is processed. First we put the name
1980 on a list, then, once the output file has been opened, transfer the
1981 name to the symbol table. */
1982
e3e942e9 1983typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
252b5132 1984
e3e942e9 1985#define ldlang_undef_chain_list_head entry_symbol.next
252b5132
RH
1986
1987void
1579bae1 1988ldlang_add_undef (const char *const name)
252b5132
RH
1989{
1990 ldlang_undef_chain_list_type *new =
1579bae1 1991 stat_alloc (sizeof (ldlang_undef_chain_list_type));
252b5132
RH
1992
1993 new->next = ldlang_undef_chain_list_head;
1994 ldlang_undef_chain_list_head = new;
1995
d1b2b2dc 1996 new->name = xstrdup (name);
fcf0e35b
AM
1997
1998 if (output_bfd != NULL)
1999 insert_undefined (new->name);
2000}
2001
2002/* Insert NAME as undefined in the symbol table. */
2003
2004static void
1579bae1 2005insert_undefined (const char *name)
fcf0e35b
AM
2006{
2007 struct bfd_link_hash_entry *h;
2008
b34976b6 2009 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
1579bae1 2010 if (h == NULL)
fcf0e35b
AM
2011 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2012 if (h->type == bfd_link_hash_new)
2013 {
2014 h->type = bfd_link_hash_undefined;
2015 h->u.undef.abfd = NULL;
2016 bfd_link_add_undef (link_info.hash, h);
2017 }
252b5132
RH
2018}
2019
2020/* Run through the list of undefineds created above and place them
2021 into the linker hash table as undefined symbols belonging to the
08da4cac
KH
2022 script file. */
2023
252b5132 2024static void
1579bae1 2025lang_place_undefineds (void)
252b5132
RH
2026{
2027 ldlang_undef_chain_list_type *ptr;
2028
1579bae1
AM
2029 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2030 insert_undefined (ptr->name);
252b5132
RH
2031}
2032
396a2467 2033/* Open input files and attach to output sections. */
08da4cac 2034
252b5132 2035static void
1579bae1
AM
2036map_input_to_output_sections
2037 (lang_statement_union_type *s, const char *target,
2038 lang_output_section_statement_type *output_section_statement)
252b5132 2039{
1579bae1 2040 for (; s != NULL; s = s->header.next)
252b5132
RH
2041 {
2042 switch (s->header.type)
2043 {
252b5132 2044 case lang_wild_statement_enum:
b6bf44ba 2045 wild (&s->wild_statement, target, output_section_statement);
abc6ab0a 2046 break;
252b5132
RH
2047 case lang_constructors_statement_enum:
2048 map_input_to_output_sections (constructor_list.head,
2049 target,
2050 output_section_statement);
2051 break;
2052 case lang_output_section_statement_enum:
2053 map_input_to_output_sections (s->output_section_statement.children.head,
2054 target,
2055 &s->output_section_statement);
2056 break;
2057 case lang_output_statement_enum:
2058 break;
2059 case lang_target_statement_enum:
2060 target = s->target_statement.target;
2061 break;
2062 case lang_group_statement_enum:
2063 map_input_to_output_sections (s->group_statement.children.head,
2064 target,
2065 output_section_statement);
2066 break;
2067 case lang_fill_statement_enum:
2068 case lang_input_section_enum:
2069 case lang_object_symbols_statement_enum:
2070 case lang_data_statement_enum:
2071 case lang_reloc_statement_enum:
2072 case lang_padding_statement_enum:
2073 case lang_input_statement_enum:
2074 if (output_section_statement != NULL
2075 && output_section_statement->bfd_section == NULL)
2076 init_os (output_section_statement);
2077 break;
2078 case lang_assignment_statement_enum:
2079 if (output_section_statement != NULL
2080 && output_section_statement->bfd_section == NULL)
2081 init_os (output_section_statement);
2082
2083 /* Make sure that any sections mentioned in the assignment
08da4cac 2084 are initialized. */
252b5132
RH
2085 exp_init_os (s->assignment_statement.exp);
2086 break;
2087 case lang_afile_asection_pair_statement_enum:
2088 FAIL ();
2089 break;
2090 case lang_address_statement_enum:
08da4cac 2091 /* Mark the specified section with the supplied address. */
252b5132
RH
2092 {
2093 lang_output_section_statement_type *os =
2094 lang_output_section_statement_lookup
2095 (s->address_statement.section_name);
2096
2097 if (os->bfd_section == NULL)
2098 init_os (os);
2099 os->addr_tree = s->address_statement.address;
2100 }
2101 break;
2102 }
2103 }
2104}
2105
4bd5a393
AM
2106/* An output section might have been removed after its statement was
2107 added. For example, ldemul_before_allocation can remove dynamic
2108 sections if they turn out to be not needed. Clean them up here. */
2109
2110static void
1579bae1 2111strip_excluded_output_sections (void)
4bd5a393
AM
2112{
2113 lang_statement_union_type *u;
2114
2115 for (u = lang_output_section_statement.head;
2116 u != NULL;
2117 u = u->output_section_statement.next)
2118 {
2119 lang_output_section_statement_type *os;
2120 asection *s;
2121
2122 os = &u->output_section_statement;
2123 s = os->bfd_section;
2124 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2125 {
2126 asection **p;
2127
2128 os->bfd_section = NULL;
2129
2130 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2131 if (*p == s)
2132 {
2133 bfd_section_list_remove (output_bfd, p);
2134 output_bfd->section_count--;
2135 break;
2136 }
2137 }
2138 }
2139}
2140
252b5132 2141static void
1579bae1
AM
2142print_output_section_statement
2143 (lang_output_section_statement_type *output_section_statement)
252b5132
RH
2144{
2145 asection *section = output_section_statement->bfd_section;
2146 int len;
2147
2148 if (output_section_statement != abs_output_section)
2149 {
2150 minfo ("\n%s", output_section_statement->name);
2151
2152 if (section != NULL)
2153 {
2154 print_dot = section->vma;
2155
2156 len = strlen (output_section_statement->name);
2157 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2158 {
2159 print_nl ();
2160 len = 0;
2161 }
2162 while (len < SECTION_NAME_MAP_LENGTH)
2163 {
2164 print_space ();
2165 ++len;
2166 }
2167
2168 minfo ("0x%V %W", section->vma, section->_raw_size);
2169
2170 if (output_section_statement->load_base != NULL)
2171 {
2172 bfd_vma addr;
2173
2174 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2175 "load base", lang_final_phase_enum);
2176 minfo (_(" load address 0x%V"), addr);
2177 }
2178 }
2179
2180 print_nl ();
2181 }
2182
2183 print_statement_list (output_section_statement->children.head,
2184 output_section_statement);
2185}
2186
2187static void
1579bae1
AM
2188print_assignment (lang_assignment_statement_type *assignment,
2189 lang_output_section_statement_type *output_section)
252b5132
RH
2190{
2191 int i;
2192 etree_value_type result;
2193
2194 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2195 print_space ();
2196
2197 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2198 lang_final_phase_enum, print_dot, &print_dot);
2199 if (result.valid_p)
7b17f854
RS
2200 {
2201 const char *dst;
2202 bfd_vma value;
2203
2204 value = result.value + result.section->bfd_section->vma;
2205 dst = assignment->exp->assign.dst;
2206
2207 minfo ("0x%V", value);
2208 if (dst[0] == '.' && dst[1] == 0)
2209 print_dot = value;
2210 }
252b5132
RH
2211 else
2212 {
2213 minfo ("*undef* ");
2214#ifdef BFD64
2215 minfo (" ");
2216#endif
2217 }
2218
2219 minfo (" ");
2220
2221 exp_print_tree (assignment->exp);
2222
2223 print_nl ();
2224}
2225
2226static void
1579bae1 2227print_input_statement (lang_input_statement_type *statm)
252b5132 2228{
1579bae1 2229 if (statm->filename != NULL)
252b5132
RH
2230 {
2231 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2232 }
2233}
2234
2235/* Print all symbols defined in a particular section. This is called
2236 via bfd_link_hash_traverse. */
2237
b34976b6 2238static bfd_boolean
1579bae1 2239print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
252b5132 2240{
1579bae1 2241 asection *sec = ptr;
252b5132
RH
2242
2243 if ((hash_entry->type == bfd_link_hash_defined
2244 || hash_entry->type == bfd_link_hash_defweak)
2245 && sec == hash_entry->u.def.section)
2246 {
2247 int i;
2248
2249 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2250 print_space ();
2251 minfo ("0x%V ",
2252 (hash_entry->u.def.value
2253 + hash_entry->u.def.section->output_offset
2254 + hash_entry->u.def.section->output_section->vma));
2255
2256 minfo (" %T\n", hash_entry->root.string);
2257 }
2258
b34976b6 2259 return TRUE;
252b5132
RH
2260}
2261
2262/* Print information about an input section to the map file. */
2263
2264static void
1579bae1 2265print_input_section (lang_input_section_type *in)
252b5132
RH
2266{
2267 asection *i = in->section;
2268 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
5f992e62 2269 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2270 ldfile_output_machine);
252b5132
RH
2271 if (size != 0)
2272 {
2273 print_space ();
2274
2275 minfo ("%s", i->name);
2276
2277 if (i->output_section != NULL)
2278 {
2279 int len;
2280
2281 len = 1 + strlen (i->name);
2282 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2283 {
2284 print_nl ();
2285 len = 0;
2286 }
2287 while (len < SECTION_NAME_MAP_LENGTH)
2288 {
2289 print_space ();
2290 ++len;
2291 }
2292
2293 minfo ("0x%V %W %B\n",
4cbfc3ac 2294 i->output_section->vma + i->output_offset, size / opb,
252b5132
RH
2295 i->owner);
2296
2297 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2298 {
2299 len = SECTION_NAME_MAP_LENGTH + 3;
2300#ifdef BFD64
2301 len += 16;
2302#else
2303 len += 8;
2304#endif
2305 while (len > 0)
2306 {
2307 print_space ();
2308 --len;
2309 }
2310
2311 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2312 }
2313
1579bae1 2314 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
252b5132 2315
4cbfc3ac 2316 print_dot = i->output_section->vma + i->output_offset + size / opb;
252b5132
RH
2317 }
2318 }
2319}
2320
2321static void
1579bae1 2322print_fill_statement (lang_fill_statement_type *fill)
252b5132 2323{
2c382fb6
AM
2324 size_t size;
2325 unsigned char *p;
2326 fputs (" FILL mask 0x", config.map_file);
2327 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2328 fprintf (config.map_file, "%02x", *p);
2329 fputs ("\n", config.map_file);
252b5132
RH
2330}
2331
2332static void
1579bae1 2333print_data_statement (lang_data_statement_type *data)
252b5132
RH
2334{
2335 int i;
2336 bfd_vma addr;
2337 bfd_size_type size;
2338 const char *name;
5f992e62 2339 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2340 ldfile_output_machine);
252b5132
RH
2341
2342 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2343 print_space ();
2344
2345 addr = data->output_vma;
2346 if (data->output_section != NULL)
2347 addr += data->output_section->vma;
2348
2349 switch (data->type)
2350 {
2351 default:
2352 abort ();
2353 case BYTE:
2354 size = BYTE_SIZE;
2355 name = "BYTE";
2356 break;
2357 case SHORT:
2358 size = SHORT_SIZE;
2359 name = "SHORT";
2360 break;
2361 case LONG:
2362 size = LONG_SIZE;
2363 name = "LONG";
2364 break;
2365 case QUAD:
2366 size = QUAD_SIZE;
2367 name = "QUAD";
2368 break;
2369 case SQUAD:
2370 size = QUAD_SIZE;
2371 name = "SQUAD";
2372 break;
2373 }
2374
2375 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2376
2377 if (data->exp->type.node_class != etree_value)
2378 {
2379 print_space ();
2380 exp_print_tree (data->exp);
2381 }
2382
2383 print_nl ();
2384
4cbfc3ac
TW
2385 print_dot = addr + size / opb;
2386
252b5132
RH
2387}
2388
2389/* Print an address statement. These are generated by options like
2390 -Ttext. */
2391
2392static void
1579bae1 2393print_address_statement (lang_address_statement_type *address)
252b5132
RH
2394{
2395 minfo (_("Address of section %s set to "), address->section_name);
2396 exp_print_tree (address->address);
2397 print_nl ();
2398}
2399
2400/* Print a reloc statement. */
2401
2402static void
1579bae1 2403print_reloc_statement (lang_reloc_statement_type *reloc)
252b5132
RH
2404{
2405 int i;
2406 bfd_vma addr;
2407 bfd_size_type size;
5f992e62
AM
2408 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2409 ldfile_output_machine);
252b5132
RH
2410
2411 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2412 print_space ();
2413
2414 addr = reloc->output_vma;
2415 if (reloc->output_section != NULL)
2416 addr += reloc->output_section->vma;
2417
2418 size = bfd_get_reloc_size (reloc->howto);
2419
2420 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2421
2422 if (reloc->name != NULL)
2423 minfo ("%s+", reloc->name);
2424 else
2425 minfo ("%s+", reloc->section->name);
2426
2427 exp_print_tree (reloc->addend_exp);
2428
2429 print_nl ();
2430
4cbfc3ac 2431 print_dot = addr + size / opb;
5f992e62 2432}
252b5132
RH
2433
2434static void
1579bae1 2435print_padding_statement (lang_padding_statement_type *s)
252b5132
RH
2436{
2437 int len;
2438 bfd_vma addr;
5f992e62
AM
2439 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2440 ldfile_output_machine);
252b5132
RH
2441
2442 minfo (" *fill*");
2443
2444 len = sizeof " *fill*" - 1;
2445 while (len < SECTION_NAME_MAP_LENGTH)
2446 {
2447 print_space ();
2448 ++len;
2449 }
2450
2451 addr = s->output_offset;
2452 if (s->output_section != NULL)
2453 addr += s->output_section->vma;
2c382fb6 2454 minfo ("0x%V %W ", addr, s->size);
252b5132 2455
2c382fb6
AM
2456 if (s->fill->size != 0)
2457 {
2458 size_t size;
2459 unsigned char *p;
2460 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2461 fprintf (config.map_file, "%02x", *p);
2462 }
252b5132
RH
2463
2464 print_nl ();
2465
4cbfc3ac 2466 print_dot = addr + s->size / opb;
252b5132
RH
2467}
2468
2469static void
1579bae1
AM
2470print_wild_statement (lang_wild_statement_type *w,
2471 lang_output_section_statement_type *os)
252b5132 2472{
b6bf44ba
AM
2473 struct wildcard_list *sec;
2474
252b5132
RH
2475 print_space ();
2476
2477 if (w->filenames_sorted)
2478 minfo ("SORT(");
08da4cac 2479 if (w->filename != NULL)
252b5132
RH
2480 minfo ("%s", w->filename);
2481 else
2482 minfo ("*");
2483 if (w->filenames_sorted)
2484 minfo (")");
2485
2486 minfo ("(");
b6bf44ba
AM
2487 for (sec = w->section_list; sec; sec = sec->next)
2488 {
2489 if (sec->spec.sorted)
2490 minfo ("SORT(");
2491 if (sec->spec.exclude_name_list != NULL)
2492 {
2493 name_list *tmp;
34786259 2494 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
b6bf44ba 2495 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
34786259
AM
2496 minfo (" %s", tmp->name);
2497 minfo (") ");
b6bf44ba
AM
2498 }
2499 if (sec->spec.name != NULL)
2500 minfo ("%s", sec->spec.name);
2501 else
2502 minfo ("*");
2503 if (sec->spec.sorted)
2504 minfo (")");
34786259
AM
2505 if (sec->next)
2506 minfo (" ");
b6bf44ba 2507 }
252b5132
RH
2508 minfo (")");
2509
2510 print_nl ();
2511
2512 print_statement_list (w->children.head, os);
2513}
2514
2515/* Print a group statement. */
2516
2517static void
1579bae1
AM
2518print_group (lang_group_statement_type *s,
2519 lang_output_section_statement_type *os)
252b5132
RH
2520{
2521 fprintf (config.map_file, "START GROUP\n");
2522 print_statement_list (s->children.head, os);
2523 fprintf (config.map_file, "END GROUP\n");
2524}
2525
2526/* Print the list of statements in S.
2527 This can be called for any statement type. */
2528
2529static void
1579bae1
AM
2530print_statement_list (lang_statement_union_type *s,
2531 lang_output_section_statement_type *os)
252b5132
RH
2532{
2533 while (s != NULL)
2534 {
2535 print_statement (s, os);
bba1a0c0 2536 s = s->header.next;
252b5132
RH
2537 }
2538}
2539
2540/* Print the first statement in statement list S.
2541 This can be called for any statement type. */
2542
2543static void
1579bae1
AM
2544print_statement (lang_statement_union_type *s,
2545 lang_output_section_statement_type *os)
252b5132
RH
2546{
2547 switch (s->header.type)
2548 {
2549 default:
2550 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2551 FAIL ();
2552 break;
2553 case lang_constructors_statement_enum:
2554 if (constructor_list.head != NULL)
2555 {
2556 if (constructors_sorted)
2557 minfo (" SORT (CONSTRUCTORS)\n");
2558 else
2559 minfo (" CONSTRUCTORS\n");
2560 print_statement_list (constructor_list.head, os);
2561 }
2562 break;
2563 case lang_wild_statement_enum:
2564 print_wild_statement (&s->wild_statement, os);
2565 break;
2566 case lang_address_statement_enum:
2567 print_address_statement (&s->address_statement);
2568 break;
2569 case lang_object_symbols_statement_enum:
2570 minfo (" CREATE_OBJECT_SYMBOLS\n");
2571 break;
2572 case lang_fill_statement_enum:
2573 print_fill_statement (&s->fill_statement);
2574 break;
2575 case lang_data_statement_enum:
2576 print_data_statement (&s->data_statement);
2577 break;
2578 case lang_reloc_statement_enum:
2579 print_reloc_statement (&s->reloc_statement);
2580 break;
2581 case lang_input_section_enum:
2582 print_input_section (&s->input_section);
2583 break;
2584 case lang_padding_statement_enum:
2585 print_padding_statement (&s->padding_statement);
2586 break;
2587 case lang_output_section_statement_enum:
2588 print_output_section_statement (&s->output_section_statement);
2589 break;
2590 case lang_assignment_statement_enum:
2591 print_assignment (&s->assignment_statement, os);
2592 break;
2593 case lang_target_statement_enum:
2594 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2595 break;
2596 case lang_output_statement_enum:
2597 minfo ("OUTPUT(%s", s->output_statement.name);
2598 if (output_target != NULL)
2599 minfo (" %s", output_target);
2600 minfo (")\n");
2601 break;
2602 case lang_input_statement_enum:
2603 print_input_statement (&s->input_statement);
2604 break;
2605 case lang_group_statement_enum:
2606 print_group (&s->group_statement, os);
2607 break;
2608 case lang_afile_asection_pair_statement_enum:
2609 FAIL ();
2610 break;
2611 }
2612}
2613
2614static void
1579bae1 2615print_statements (void)
252b5132
RH
2616{
2617 print_statement_list (statement_list.head, abs_output_section);
2618}
2619
2620/* Print the first N statements in statement list S to STDERR.
2621 If N == 0, nothing is printed.
2622 If N < 0, the entire list is printed.
2623 Intended to be called from GDB. */
2624
2625void
1579bae1 2626dprint_statement (lang_statement_union_type *s, int n)
252b5132
RH
2627{
2628 FILE *map_save = config.map_file;
2629
2630 config.map_file = stderr;
2631
2632 if (n < 0)
2633 print_statement_list (s, abs_output_section);
2634 else
2635 {
2636 while (s && --n >= 0)
2637 {
2638 print_statement (s, abs_output_section);
bba1a0c0 2639 s = s->header.next;
252b5132
RH
2640 }
2641 }
2642
2643 config.map_file = map_save;
2644}
2645
b3327aad 2646static void
1579bae1
AM
2647insert_pad (lang_statement_union_type **ptr,
2648 fill_type *fill,
2649 unsigned int alignment_needed,
2650 asection *output_section,
2651 bfd_vma dot)
252b5132 2652{
2c382fb6 2653 static fill_type zero_fill = { 1, { 0 } };
b3327aad 2654 lang_statement_union_type *pad;
b3327aad 2655
c0c330a7 2656 pad = ((lang_statement_union_type *)
2af02257 2657 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
b3327aad 2658 if (ptr != &statement_list.head
2af02257 2659 && pad->header.type == lang_padding_statement_enum
b3327aad 2660 && pad->padding_statement.output_section == output_section)
252b5132 2661 {
b3327aad
AM
2662 /* Use the existing pad statement. The above test on output
2663 section is probably redundant, but it doesn't hurt to check. */
252b5132 2664 }
b3327aad 2665 else
252b5132 2666 {
b3327aad 2667 /* Make a new padding statement, linked into existing chain. */
1579bae1 2668 pad = stat_alloc (sizeof (lang_padding_statement_type));
b3327aad
AM
2669 pad->header.next = *ptr;
2670 *ptr = pad;
2671 pad->header.type = lang_padding_statement_enum;
2672 pad->padding_statement.output_section = output_section;
1579bae1 2673 if (fill == NULL)
2c382fb6 2674 fill = &zero_fill;
b3327aad 2675 pad->padding_statement.fill = fill;
252b5132 2676 }
b3327aad
AM
2677 pad->padding_statement.output_offset = dot - output_section->vma;
2678 pad->padding_statement.size = alignment_needed;
2679 output_section->_raw_size += alignment_needed;
252b5132
RH
2680}
2681
08da4cac
KH
2682/* Work out how much this section will move the dot point. */
2683
252b5132 2684static bfd_vma
1579bae1
AM
2685size_input_section (lang_statement_union_type **this_ptr,
2686 lang_output_section_statement_type *output_section_statement,
2687 fill_type *fill,
2688 bfd_vma dot)
252b5132
RH
2689{
2690 lang_input_section_type *is = &((*this_ptr)->input_section);
2691 asection *i = is->section;
2692
7c519c12 2693 if (!is->ifile->just_syms_flag)
252b5132 2694 {
b3327aad
AM
2695 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2696 ldfile_output_machine);
2697 unsigned int alignment_needed;
2698 asection *o;
2699
2700 /* Align this section first to the input sections requirement,
2701 then to the output section's requirement. If this alignment
2702 is greater than any seen before, then record it too. Perform
2703 the alignment by inserting a magic 'padding' statement. */
2704
252b5132 2705 if (output_section_statement->subsection_alignment != -1)
b3327aad
AM
2706 i->alignment_power = output_section_statement->subsection_alignment;
2707
2708 o = output_section_statement->bfd_section;
2709 if (o->alignment_power < i->alignment_power)
2710 o->alignment_power = i->alignment_power;
252b5132 2711
b3327aad
AM
2712 alignment_needed = align_power (dot, i->alignment_power) - dot;
2713
2714 if (alignment_needed != 0)
2715 {
2716 insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2717 dot += alignment_needed;
2718 }
252b5132 2719
08da4cac 2720 /* Remember where in the output section this input section goes. */
252b5132 2721
b3327aad 2722 i->output_offset = dot - o->vma;
252b5132 2723
08da4cac 2724 /* Mark how big the output section must be to contain this now. */
252b5132 2725 if (i->_cooked_size != 0)
4cbfc3ac 2726 dot += i->_cooked_size / opb;
252b5132 2727 else
4cbfc3ac 2728 dot += i->_raw_size / opb;
b3327aad 2729 o->_raw_size = (dot - o->vma) * opb;
252b5132
RH
2730 }
2731 else
2732 {
2733 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2734 }
2735
2736 return dot;
2737}
2738
33275c22 2739#define IGNORE_SECTION(bfd, s) \
d1778b88
AM
2740 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2741 != (SEC_ALLOC | SEC_LOAD)) \
33275c22 2742 || bfd_section_size (bfd, s) == 0)
d1778b88 2743
252b5132
RH
2744/* Check to see if any allocated sections overlap with other allocated
2745 sections. This can happen when the linker script specifically specifies
2746 the output section addresses of the two sections. */
08da4cac 2747
252b5132 2748static void
1579bae1 2749lang_check_section_addresses (void)
252b5132 2750{
08da4cac 2751 asection *s;
f6af82bd 2752 unsigned opb = bfd_octets_per_byte (output_bfd);
252b5132
RH
2753
2754 /* Scan all sections in the output list. */
2755 for (s = output_bfd->sections; s != NULL; s = s->next)
33275c22 2756 {
08da4cac 2757 asection *os;
5f992e62 2758
33275c22
NC
2759 /* Ignore sections which are not loaded or which have no contents. */
2760 if (IGNORE_SECTION (output_bfd, s))
2761 continue;
5f992e62 2762
33275c22
NC
2763 /* Once we reach section 's' stop our seach. This prevents two
2764 warning messages from being produced, one for 'section A overlaps
2765 section B' and one for 'section B overlaps section A'. */
2766 for (os = output_bfd->sections; os != s; os = os->next)
2767 {
2768 bfd_vma s_start;
2769 bfd_vma s_end;
2770 bfd_vma os_start;
2771 bfd_vma os_end;
5f992e62 2772
33275c22
NC
2773 /* Only consider loadable sections with real contents. */
2774 if (IGNORE_SECTION (output_bfd, os))
2775 continue;
252b5132 2776
33275c22
NC
2777 /* We must check the sections' LMA addresses not their
2778 VMA addresses because overlay sections can have
2779 overlapping VMAs but they must have distinct LMAs. */
2780 s_start = bfd_section_lma (output_bfd, s);
2781 os_start = bfd_section_lma (output_bfd, os);
9e4ed18c
TW
2782 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2783 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
5f992e62 2784
33275c22
NC
2785 /* Look for an overlap. */
2786 if ((s_end < os_start) || (s_start > os_end))
2787 continue;
5f992e62 2788
33275c22 2789 einfo (
252b5132 2790_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
33275c22 2791 s->name, s_start, s_end, os->name, os_start, os_end);
5f992e62 2792
33275c22
NC
2793 /* Once we have found one overlap for this section,
2794 stop looking for others. */
2795 break;
2796 }
2797 }
252b5132
RH
2798}
2799
562d3460
TW
2800/* Make sure the new address is within the region. We explicitly permit the
2801 current address to be at the exact end of the region when the address is
2802 non-zero, in case the region is at the end of addressable memory and the
5f992e62 2803 calculation wraps around. */
562d3460
TW
2804
2805static void
1579bae1
AM
2806os_region_check (lang_output_section_statement_type *os,
2807 struct memory_region_struct *region,
2808 etree_type *tree,
2809 bfd_vma base)
562d3460
TW
2810{
2811 if ((region->current < region->origin
2812 || (region->current - region->origin > region->length))
2813 && ((region->current != region->origin + region->length)
b7a26f91 2814 || base == 0))
562d3460 2815 {
1579bae1 2816 if (tree != NULL)
b7a26f91
KH
2817 {
2818 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2819 region->current,
2820 os->bfd_section->owner,
2821 os->bfd_section->name,
2822 region->name);
2823 }
562d3460 2824 else
b7a26f91
KH
2825 {
2826 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2827 region->name,
2828 os->bfd_section->owner,
2829 os->bfd_section->name);
2830 }
562d3460
TW
2831 /* Reset the region pointer. */
2832 region->current = region->origin;
2833 }
2834}
2835
252b5132
RH
2836/* Set the sizes for all the output sections. */
2837
2d20f7bf 2838static bfd_vma
1579bae1
AM
2839lang_size_sections_1
2840 (lang_statement_union_type *s,
2841 lang_output_section_statement_type *output_section_statement,
2842 lang_statement_union_type **prev,
2843 fill_type *fill,
2844 bfd_vma dot,
2845 bfd_boolean *relax,
2846 bfd_boolean check_regions)
252b5132 2847{
5f992e62 2848 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2849 ldfile_output_machine);
4cbfc3ac 2850
252b5132 2851 /* Size up the sections from their constituent parts. */
1579bae1 2852 for (; s != NULL; s = s->header.next)
252b5132
RH
2853 {
2854 switch (s->header.type)
2855 {
2856 case lang_output_section_statement_enum:
2857 {
2858 bfd_vma after;
d1778b88 2859 lang_output_section_statement_type *os;
252b5132 2860
d1778b88 2861 os = &s->output_section_statement;
252b5132
RH
2862 if (os->bfd_section == NULL)
2863 /* This section was never actually created. */
2864 break;
2865
2866 /* If this is a COFF shared library section, use the size and
2867 address from the input section. FIXME: This is COFF
2868 specific; it would be cleaner if there were some other way
2869 to do this, but nothing simple comes to mind. */
2870 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2871 {
08da4cac 2872 asection *input;
252b5132
RH
2873
2874 if (os->children.head == NULL
bba1a0c0 2875 || os->children.head->header.next != NULL
252b5132
RH
2876 || os->children.head->header.type != lang_input_section_enum)
2877 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2878 os->name);
2879
2880 input = os->children.head->input_section.section;
2881 bfd_set_section_vma (os->bfd_section->owner,
2882 os->bfd_section,
2883 bfd_section_vma (input->owner, input));
2884 os->bfd_section->_raw_size = input->_raw_size;
2885 break;
2886 }
2887
2888 if (bfd_is_abs_section (os->bfd_section))
2889 {
2890 /* No matter what happens, an abs section starts at zero. */
2891 ASSERT (os->bfd_section->vma == 0);
2892 }
2893 else
2894 {
1579bae1 2895 if (os->addr_tree == NULL)
252b5132
RH
2896 {
2897 /* No address specified for this section, get one
2898 from the region specification. */
1579bae1 2899 if (os->region == NULL
252b5132
RH
2900 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2901 & (SEC_ALLOC | SEC_LOAD)) != 0)
2902 && os->region->name[0] == '*'
a747ee4d 2903 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
252b5132
RH
2904 {
2905 os->region = lang_memory_default (os->bfd_section);
2906 }
2907
2908 /* If a loadable section is using the default memory
2909 region, and some non default memory regions were
66184979 2910 defined, issue an error message. */
a7f18fb3 2911 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
d08a14c3
AM
2912 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2913 & SEC_NEVER_LOAD) == 0
1049f94e 2914 && ! link_info.relocatable
cf888e70 2915 && check_regions
a747ee4d 2916 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
252b5132 2917 && lang_memory_region_list != NULL
d1778b88 2918 && (strcmp (lang_memory_region_list->name,
a747ee4d 2919 DEFAULT_MEMORY_REGION) != 0
252b5132 2920 || lang_memory_region_list->next != NULL))
66184979
NC
2921 {
2922 /* By default this is an error rather than just a
2923 warning because if we allocate the section to the
2924 default memory region we can end up creating an
2925 excessivly large binary, or even seg faulting when
2926 attmepting to perform a negative seek. See
1579bae1 2927 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
66184979
NC
2928 for an example of this. This behaviour can be
2929 overridden by the using the --no-check-sections
2930 switch. */
2931 if (command_line.check_section_addresses)
2932 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2933 bfd_get_section_name (output_bfd,
2934 os->bfd_section));
2935 else
2936 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2937 bfd_get_section_name (output_bfd,
2938 os->bfd_section));
2939 }
252b5132
RH
2940
2941 dot = os->region->current;
5f992e62 2942
252b5132
RH
2943 if (os->section_alignment == -1)
2944 {
2945 bfd_vma olddot;
2946
2947 olddot = dot;
d1778b88
AM
2948 dot = align_power (dot,
2949 os->bfd_section->alignment_power);
252b5132
RH
2950
2951 if (dot != olddot && config.warn_section_align)
2952 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2953 os->name, (unsigned int) (dot - olddot));
2954 }
2955 }
2956 else
2957 {
2958 etree_value_type r;
2959
2960 r = exp_fold_tree (os->addr_tree,
2961 abs_output_section,
2962 lang_allocating_phase_enum,
2963 dot, &dot);
7c519c12
AM
2964 if (!r.valid_p)
2965 einfo (_("%F%S: non constant address expression for section %s\n"),
2966 os->name);
2967
252b5132
RH
2968 dot = r.value + r.section->bfd_section->vma;
2969 }
5f992e62 2970
252b5132
RH
2971 /* The section starts here.
2972 First, align to what the section needs. */
2973
2974 if (os->section_alignment != -1)
2975 dot = align_power (dot, os->section_alignment);
2976
2977 bfd_set_section_vma (0, os->bfd_section, dot);
5f992e62 2978
252b5132
RH
2979 os->bfd_section->output_offset = 0;
2980 }
2981
2d20f7bf 2982 lang_size_sections_1 (os->children.head, os, &os->children.head,
cf888e70 2983 os->fill, dot, relax, check_regions);
5f992e62 2984
08da4cac
KH
2985 /* Put the section within the requested block size, or
2986 align at the block boundary. */
c553bb91 2987 after = align_n (os->bfd_section->vma
32edc927 2988 + os->bfd_section->_raw_size / opb,
252b5132
RH
2989 (bfd_vma) os->block_value);
2990
2991 if (bfd_is_abs_section (os->bfd_section))
2992 ASSERT (after == os->bfd_section->vma);
13ae64f3 2993 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
704afa60 2994 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
1049f94e 2995 && ! link_info.relocatable)
13ae64f3 2996 os->bfd_section->_raw_size = 0;
252b5132 2997 else
5f992e62 2998 os->bfd_section->_raw_size =
08da4cac 2999 (after - os->bfd_section->vma) * opb;
9f88b410 3000
4cbfc3ac 3001 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
b34976b6 3002 os->processed = TRUE;
252b5132 3003
9f88b410
RS
3004 if (os->update_dot_tree != 0)
3005 exp_fold_tree (os->update_dot_tree, abs_output_section,
3006 lang_allocating_phase_enum, dot, &dot);
3007
252b5132
RH
3008 /* Update dot in the region ?
3009 We only do this if the section is going to be allocated,
3010 since unallocated sections do not contribute to the region's
13392b77 3011 overall size in memory.
5f992e62 3012
cce4c4c5
NC
3013 If the SEC_NEVER_LOAD bit is not set, it will affect the
3014 addresses of sections after it. We have to update
3015 dot. */
1579bae1 3016 if (os->region != NULL
cce4c4c5
NC
3017 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3018 & SEC_NEVER_LOAD) == 0
3019 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3020 & (SEC_ALLOC | SEC_LOAD))))
252b5132
RH
3021 {
3022 os->region->current = dot;
5f992e62 3023
cf888e70
NC
3024 if (check_regions)
3025 /* Make sure the new address is within the region. */
3026 os_region_check (os, os->region, os->addr_tree,
3027 os->bfd_section->vma);
08da4cac
KH
3028
3029 /* If there's no load address specified, use the run
3030 region as the load region. */
3031 if (os->lma_region == NULL && os->load_base == NULL)
3032 os->lma_region = os->region;
3033
ee3cc2e2 3034 if (os->lma_region != NULL && os->lma_region != os->region)
08da4cac 3035 {
ee3cc2e2
RS
3036 /* Set load_base, which will be handled later. */
3037 os->load_base = exp_intop (os->lma_region->current);
3038 os->lma_region->current +=
3039 os->bfd_section->_raw_size / opb;
cf888e70
NC
3040 if (check_regions)
3041 os_region_check (os, os->lma_region, NULL,
3042 os->bfd_section->lma);
08da4cac 3043 }
252b5132
RH
3044 }
3045 }
3046 break;
3047
3048 case lang_constructors_statement_enum:
2d20f7bf
JJ
3049 dot = lang_size_sections_1 (constructor_list.head,
3050 output_section_statement,
3051 &s->wild_statement.children.head,
cf888e70 3052 fill, dot, relax, check_regions);
252b5132
RH
3053 break;
3054
3055 case lang_data_statement_enum:
3056 {
3057 unsigned int size = 0;
3058
08da4cac
KH
3059 s->data_statement.output_vma =
3060 dot - output_section_statement->bfd_section->vma;
252b5132
RH
3061 s->data_statement.output_section =
3062 output_section_statement->bfd_section;
3063
3064 switch (s->data_statement.type)
3065 {
08da4cac
KH
3066 default:
3067 abort ();
252b5132
RH
3068 case QUAD:
3069 case SQUAD:
3070 size = QUAD_SIZE;
3071 break;
3072 case LONG:
3073 size = LONG_SIZE;
3074 break;
3075 case SHORT:
3076 size = SHORT_SIZE;
3077 break;
3078 case BYTE:
3079 size = BYTE_SIZE;
3080 break;
3081 }
08da4cac
KH
3082 if (size < opb)
3083 size = opb;
4cbfc3ac 3084 dot += size / opb;
252b5132
RH
3085 output_section_statement->bfd_section->_raw_size += size;
3086 /* The output section gets contents, and then we inspect for
3087 any flags set in the input script which override any ALLOC. */
3088 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
08da4cac
KH
3089 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3090 {
3091 output_section_statement->bfd_section->flags |=
3092 SEC_ALLOC | SEC_LOAD;
3093 }
252b5132
RH
3094 }
3095 break;
3096
3097 case lang_reloc_statement_enum:
3098 {
3099 int size;
3100
3101 s->reloc_statement.output_vma =
3102 dot - output_section_statement->bfd_section->vma;
3103 s->reloc_statement.output_section =
3104 output_section_statement->bfd_section;
3105 size = bfd_get_reloc_size (s->reloc_statement.howto);
4cbfc3ac 3106 dot += size / opb;
252b5132
RH
3107 output_section_statement->bfd_section->_raw_size += size;
3108 }
3109 break;
5f992e62 3110
252b5132
RH
3111 case lang_wild_statement_enum:
3112
2d20f7bf
JJ
3113 dot = lang_size_sections_1 (s->wild_statement.children.head,
3114 output_section_statement,
3115 &s->wild_statement.children.head,
cf888e70 3116 fill, dot, relax, check_regions);
252b5132
RH
3117
3118 break;
3119
3120 case lang_object_symbols_statement_enum:
3121 link_info.create_object_symbols_section =
3122 output_section_statement->bfd_section;
3123 break;
3124 case lang_output_statement_enum:
3125 case lang_target_statement_enum:
3126 break;
3127 case lang_input_section_enum:
3128 {
3129 asection *i;
3130
3131 i = (*prev)->input_section.section;
3132 if (! relax)
3133 {
3134 if (i->_cooked_size == 0)
3135 i->_cooked_size = i->_raw_size;
3136 }
3137 else
3138 {
b34976b6 3139 bfd_boolean again;
252b5132
RH
3140
3141 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3142 einfo (_("%P%F: can't relax section: %E\n"));
3143 if (again)
b34976b6 3144 *relax = TRUE;
252b5132 3145 }
b3327aad
AM
3146 dot = size_input_section (prev, output_section_statement,
3147 output_section_statement->fill, dot);
252b5132
RH
3148 }
3149 break;
3150 case lang_input_statement_enum:
3151 break;
3152 case lang_fill_statement_enum:
08da4cac
KH
3153 s->fill_statement.output_section =
3154 output_section_statement->bfd_section;
252b5132
RH
3155
3156 fill = s->fill_statement.fill;
3157 break;
3158 case lang_assignment_statement_enum:
3159 {
3160 bfd_vma newdot = dot;
3161
3162 exp_fold_tree (s->assignment_statement.exp,
3163 output_section_statement,
3164 lang_allocating_phase_enum,
3165 dot,
3166 &newdot);
3167
3168 if (newdot != dot)
3169 {
252b5132
RH
3170 if (output_section_statement == abs_output_section)
3171 {
3172 /* If we don't have an output section, then just adjust
3173 the default memory address. */
a747ee4d 3174 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
252b5132 3175 }
b3327aad 3176 else
252b5132 3177 {
b3327aad
AM
3178 /* Insert a pad after this statement. We can't
3179 put the pad before when relaxing, in case the
3180 assignment references dot. */
3181 insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3182 output_section_statement->bfd_section, dot);
3183
3184 /* Don't neuter the pad below when relaxing. */
3185 s = s->header.next;
252b5132
RH
3186 }
3187
3188 dot = newdot;
3189 }
3190 }
3191 break;
3192
3193 case lang_padding_statement_enum:
c0c330a7
AM
3194 /* If this is the first time lang_size_sections is called,
3195 we won't have any padding statements. If this is the
3196 second or later passes when relaxing, we should allow
3197 padding to shrink. If padding is needed on this pass, it
3198 will be added back in. */
3199 s->padding_statement.size = 0;
6e814ff8
AM
3200
3201 /* Make sure output_offset is valid. If relaxation shrinks
3202 the section and this pad isn't needed, it's possible to
3203 have output_offset larger than the final size of the
3204 section. bfd_set_section_contents will complain even for
3205 a pad size of zero. */
3206 s->padding_statement.output_offset
3207 = dot - output_section_statement->bfd_section->vma;
252b5132
RH
3208 break;
3209
3210 case lang_group_statement_enum:
2d20f7bf
JJ
3211 dot = lang_size_sections_1 (s->group_statement.children.head,
3212 output_section_statement,
3213 &s->group_statement.children.head,
cf888e70 3214 fill, dot, relax, check_regions);
252b5132
RH
3215 break;
3216
3217 default:
3218 FAIL ();
3219 break;
3220
c0c330a7 3221 /* We can only get here when relaxing is turned on. */
252b5132
RH
3222 case lang_address_statement_enum:
3223 break;
3224 }
3225 prev = &s->header.next;
3226 }
3227 return dot;
3228}
3229
2d20f7bf 3230bfd_vma
1579bae1
AM
3231lang_size_sections
3232 (lang_statement_union_type *s,
3233 lang_output_section_statement_type *output_section_statement,
3234 lang_statement_union_type **prev,
3235 fill_type *fill,
3236 bfd_vma dot,
3237 bfd_boolean *relax,
3238 bfd_boolean check_regions)
2d20f7bf
JJ
3239{
3240 bfd_vma result;
86d3c9a8 3241 asection *o;
2d20f7bf 3242
420e579c
HPN
3243 /* Callers of exp_fold_tree need to increment this. */
3244 lang_statement_iteration++;
3245
2d20f7bf
JJ
3246 exp_data_seg.phase = exp_dataseg_none;
3247 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
cf888e70 3248 dot, relax, check_regions);
2d20f7bf
JJ
3249 if (exp_data_seg.phase == exp_dataseg_end_seen)
3250 {
3251 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3252 a page could be saved in the data segment. */
3253 bfd_vma first, last;
3254
3255 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3256 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3257 if (first && last
3258 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3259 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3260 && first + last <= exp_data_seg.pagesize)
3261 {
3262 exp_data_seg.phase = exp_dataseg_adjust;
3263 result = lang_size_sections_1 (s, output_section_statement, prev,
cf888e70 3264 fill, dot, relax, check_regions);
2d20f7bf
JJ
3265 }
3266 }
3267
86d3c9a8
HPN
3268 /* Some backend relaxers want to refer to the output section size. Give
3269 them a section size that does not change on the next call while they
3270 relax. We can't set this at top because lang_reset_memory_regions
3271 which is called before we get here, sets _raw_size to 0 on relaxing
3272 rounds. */
3273 for (o = output_bfd->sections; o != NULL; o = o->next)
3274 o->_cooked_size = o->_raw_size;
3275
2d20f7bf
JJ
3276 return result;
3277}
3278
420e579c
HPN
3279/* Worker function for lang_do_assignments. Recursiveness goes here. */
3280
3281static bfd_vma
3282lang_do_assignments_1
1579bae1
AM
3283 (lang_statement_union_type *s,
3284 lang_output_section_statement_type *output_section_statement,
3285 fill_type *fill,
3286 bfd_vma dot)
252b5132 3287{
5f992e62
AM
3288 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3289 ldfile_output_machine);
4cbfc3ac 3290
1579bae1 3291 for (; s != NULL; s = s->header.next)
252b5132
RH
3292 {
3293 switch (s->header.type)
3294 {
3295 case lang_constructors_statement_enum:
420e579c
HPN
3296 dot = lang_do_assignments_1 (constructor_list.head,
3297 output_section_statement,
3298 fill,
3299 dot);
252b5132
RH
3300 break;
3301
3302 case lang_output_section_statement_enum:
3303 {
d1778b88 3304 lang_output_section_statement_type *os;
252b5132 3305
d1778b88 3306 os = &(s->output_section_statement);
252b5132
RH
3307 if (os->bfd_section != NULL)
3308 {
3309 dot = os->bfd_section->vma;
420e579c
HPN
3310 (void) lang_do_assignments_1 (os->children.head, os,
3311 os->fill, dot);
4cbfc3ac
TW
3312 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3313
252b5132 3314 }
c13b1b77 3315 if (os->load_base)
252b5132
RH
3316 {
3317 /* If nothing has been placed into the output section then
4de2d33d 3318 it won't have a bfd_section. */
5f992e62 3319 if (os->bfd_section)
252b5132 3320 {
5f992e62 3321 os->bfd_section->lma
08da4cac
KH
3322 = exp_get_abs_int (os->load_base, 0, "load base",
3323 lang_final_phase_enum);
252b5132
RH
3324 }
3325 }
3326 }
3327 break;
3328 case lang_wild_statement_enum:
3329
420e579c
HPN
3330 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3331 output_section_statement,
3332 fill, dot);
252b5132
RH
3333
3334 break;
3335
3336 case lang_object_symbols_statement_enum:
3337 case lang_output_statement_enum:
3338 case lang_target_statement_enum:
3339#if 0
3340 case lang_common_statement_enum:
3341#endif
3342 break;
3343 case lang_data_statement_enum:
3344 {
3345 etree_value_type value;
3346
3347 value = exp_fold_tree (s->data_statement.exp,
3348 abs_output_section,
3349 lang_final_phase_enum, dot, &dot);
3350 s->data_statement.value = value.value;
7c519c12 3351 if (!value.valid_p)
252b5132
RH
3352 einfo (_("%F%P: invalid data statement\n"));
3353 }
b7a26f91
KH
3354 {
3355 unsigned int size;
08da4cac
KH
3356 switch (s->data_statement.type)
3357 {
3358 default:
3359 abort ();
3360 case QUAD:
3361 case SQUAD:
3362 size = QUAD_SIZE;
3363 break;
3364 case LONG:
3365 size = LONG_SIZE;
3366 break;
3367 case SHORT:
3368 size = SHORT_SIZE;
3369 break;
3370 case BYTE:
3371 size = BYTE_SIZE;
3372 break;
3373 }
3374 if (size < opb)
3375 size = opb;
3376 dot += size / opb;
3377 }
252b5132
RH
3378 break;
3379
3380 case lang_reloc_statement_enum:
3381 {
3382 etree_value_type value;
3383
3384 value = exp_fold_tree (s->reloc_statement.addend_exp,
3385 abs_output_section,
3386 lang_final_phase_enum, dot, &dot);
3387 s->reloc_statement.addend_value = value.value;
7c519c12 3388 if (!value.valid_p)
252b5132
RH
3389 einfo (_("%F%P: invalid reloc statement\n"));
3390 }
4cbfc3ac 3391 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
252b5132
RH
3392 break;
3393
3394 case lang_input_section_enum:
3395 {
3396 asection *in = s->input_section.section;
3397
3398 if (in->_cooked_size != 0)
4cbfc3ac 3399 dot += in->_cooked_size / opb;
252b5132 3400 else
4cbfc3ac 3401 dot += in->_raw_size / opb;
252b5132
RH
3402 }
3403 break;
3404
3405 case lang_input_statement_enum:
3406 break;
3407 case lang_fill_statement_enum:
3408 fill = s->fill_statement.fill;
3409 break;
3410 case lang_assignment_statement_enum:
3411 {
3412 exp_fold_tree (s->assignment_statement.exp,
3413 output_section_statement,
3414 lang_final_phase_enum,
3415 dot,
3416 &dot);
3417 }
3418
3419 break;
3420 case lang_padding_statement_enum:
4cbfc3ac 3421 dot += s->padding_statement.size / opb;
252b5132
RH
3422 break;
3423
3424 case lang_group_statement_enum:
420e579c
HPN
3425 dot = lang_do_assignments_1 (s->group_statement.children.head,
3426 output_section_statement,
3427 fill, dot);
252b5132
RH
3428
3429 break;
3430
3431 default:
3432 FAIL ();
3433 break;
3434 case lang_address_statement_enum:
3435 break;
3436 }
3437
3438 }
3439 return dot;
3440}
3441
420e579c
HPN
3442bfd_vma
3443lang_do_assignments (lang_statement_union_type *s,
3444 lang_output_section_statement_type
3445 *output_section_statement,
3446 fill_type *fill,
3447 bfd_vma dot)
3448{
3449 /* Callers of exp_fold_tree need to increment this. */
3450 lang_statement_iteration++;
3451 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3452}
3453
252b5132
RH
3454/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3455 operator .startof. (section_name), it produces an undefined symbol
3456 .startof.section_name. Similarly, when it sees
3457 .sizeof. (section_name), it produces an undefined symbol
3458 .sizeof.section_name. For all the output sections, we look for
3459 such symbols, and set them to the correct value. */
3460
3461static void
1579bae1 3462lang_set_startof (void)
252b5132
RH
3463{
3464 asection *s;
3465
1049f94e 3466 if (link_info.relocatable)
252b5132
RH
3467 return;
3468
3469 for (s = output_bfd->sections; s != NULL; s = s->next)
3470 {
3471 const char *secname;
3472 char *buf;
3473 struct bfd_link_hash_entry *h;
3474
3475 secname = bfd_get_section_name (output_bfd, s);
3476 buf = xmalloc (10 + strlen (secname));
3477
3478 sprintf (buf, ".startof.%s", secname);
b34976b6 3479 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
3480 if (h != NULL && h->type == bfd_link_hash_undefined)
3481 {
3482 h->type = bfd_link_hash_defined;
3483 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3484 h->u.def.section = bfd_abs_section_ptr;
3485 }
3486
3487 sprintf (buf, ".sizeof.%s", secname);
b34976b6 3488 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
3489 if (h != NULL && h->type == bfd_link_hash_undefined)
3490 {
b7a26f91 3491 unsigned opb;
d1778b88 3492
b7a26f91 3493 opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
d1778b88 3494 ldfile_output_machine);
252b5132
RH
3495 h->type = bfd_link_hash_defined;
3496 if (s->_cooked_size != 0)
4cbfc3ac 3497 h->u.def.value = s->_cooked_size / opb;
252b5132 3498 else
4cbfc3ac 3499 h->u.def.value = s->_raw_size / opb;
252b5132
RH
3500 h->u.def.section = bfd_abs_section_ptr;
3501 }
3502
3503 free (buf);
3504 }
3505}
3506
3507static void
1579bae1 3508lang_finish (void)
252b5132
RH
3509{
3510 struct bfd_link_hash_entry *h;
b34976b6 3511 bfd_boolean warn;
252b5132 3512
1049f94e 3513 if (link_info.relocatable || link_info.shared)
b34976b6 3514 warn = FALSE;
252b5132 3515 else
b34976b6 3516 warn = TRUE;
252b5132 3517
1579bae1 3518 if (entry_symbol.name == NULL)
252b5132
RH
3519 {
3520 /* No entry has been specified. Look for start, but don't warn
3521 if we don't find it. */
e3e942e9 3522 entry_symbol.name = "start";
b34976b6 3523 warn = FALSE;
252b5132
RH
3524 }
3525
e3e942e9 3526 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
b34976b6 3527 FALSE, FALSE, TRUE);
1579bae1 3528 if (h != NULL
252b5132
RH
3529 && (h->type == bfd_link_hash_defined
3530 || h->type == bfd_link_hash_defweak)
3531 && h->u.def.section->output_section != NULL)
3532 {
3533 bfd_vma val;
3534
3535 val = (h->u.def.value
3536 + bfd_get_section_vma (output_bfd,
3537 h->u.def.section->output_section)
3538 + h->u.def.section->output_offset);
3539 if (! bfd_set_start_address (output_bfd, val))
e3e942e9 3540 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
252b5132
RH
3541 }
3542 else
3543 {
3544 bfd_vma val;
5f992e62 3545 const char *send;
252b5132
RH
3546
3547 /* We couldn't find the entry symbol. Try parsing it as a
3548 number. */
e3e942e9 3549 val = bfd_scan_vma (entry_symbol.name, &send, 0);
252b5132
RH
3550 if (*send == '\0')
3551 {
3552 if (! bfd_set_start_address (output_bfd, val))
3553 einfo (_("%P%F: can't set start address\n"));
3554 }
3555 else
3556 {
3557 asection *ts;
3558
3559 /* Can't find the entry symbol, and it's not a number. Use
3560 the first address in the text section. */
1e281515 3561 ts = bfd_get_section_by_name (output_bfd, entry_section);
1579bae1 3562 if (ts != NULL)
252b5132
RH
3563 {
3564 if (warn)
3565 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
e3e942e9
AM
3566 entry_symbol.name,
3567 bfd_get_section_vma (output_bfd, ts));
252b5132
RH
3568 if (! bfd_set_start_address (output_bfd,
3569 bfd_get_section_vma (output_bfd,
3570 ts)))
3571 einfo (_("%P%F: can't set start address\n"));
3572 }
3573 else
3574 {
3575 if (warn)
3576 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
e3e942e9 3577 entry_symbol.name);
252b5132
RH
3578 }
3579 }
3580 }
420e579c
HPN
3581
3582 bfd_hash_table_free (&lang_definedness_table);
252b5132
RH
3583}
3584
3585/* This is a small function used when we want to ignore errors from
3586 BFD. */
3587
3588static void
87f2a346 3589ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
252b5132
RH
3590{
3591 /* Don't do anything. */
3592}
3593
3594/* Check that the architecture of all the input files is compatible
3595 with the output file. Also call the backend to let it do any
3596 other checking that is needed. */
3597
3598static void
1579bae1 3599lang_check (void)
252b5132
RH
3600{
3601 lang_statement_union_type *file;
3602 bfd *input_bfd;
5f992e62 3603 const bfd_arch_info_type *compatible;
252b5132 3604
1579bae1 3605 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
252b5132
RH
3606 {
3607 input_bfd = file->input_statement.the_bfd;
312b768e
NC
3608 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3609 command_line.accept_unknown_input_arch);
30cba025
AM
3610
3611 /* In general it is not possible to perform a relocatable
3612 link between differing object formats when the input
3613 file has relocations, because the relocations in the
3614 input format may not have equivalent representations in
3615 the output format (and besides BFD does not translate
3616 relocs for other link purposes than a final link). */
1049f94e 3617 if ((link_info.relocatable || link_info.emitrelocations)
30cba025 3618 && (compatible == NULL
d35a52e2 3619 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
30cba025
AM
3620 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3621 {
3622 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3623 bfd_get_target (input_bfd), input_bfd,
3624 bfd_get_target (output_bfd), output_bfd);
3625 /* einfo with %F exits. */
3626 }
3627
252b5132
RH
3628 if (compatible == NULL)
3629 {
3630 if (command_line.warn_mismatch)
3631 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3632 bfd_printable_name (input_bfd), input_bfd,
3633 bfd_printable_name (output_bfd));
3634 }
b9247304 3635 else if (bfd_count_sections (input_bfd))
252b5132 3636 {
b9247304 3637 /* If the input bfd has no contents, it shouldn't set the
b7a26f91 3638 private data of the output bfd. */
b9247304 3639
252b5132
RH
3640 bfd_error_handler_type pfn = NULL;
3641
3642 /* If we aren't supposed to warn about mismatched input
3643 files, temporarily set the BFD error handler to a
3644 function which will do nothing. We still want to call
3645 bfd_merge_private_bfd_data, since it may set up
3646 information which is needed in the output file. */
3647 if (! command_line.warn_mismatch)
3648 pfn = bfd_set_error_handler (ignore_bfd_errors);
3649 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3650 {
3651 if (command_line.warn_mismatch)
3652 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3653 input_bfd);
3654 }
3655 if (! command_line.warn_mismatch)
3656 bfd_set_error_handler (pfn);
3657 }
3658 }
3659}
3660
3661/* Look through all the global common symbols and attach them to the
3662 correct section. The -sort-common command line switch may be used
3663 to roughly sort the entries by size. */
3664
3665static void
1579bae1 3666lang_common (void)
252b5132 3667{
4818e05f
AM
3668 if (command_line.inhibit_common_definition)
3669 return;
1049f94e 3670 if (link_info.relocatable
252b5132
RH
3671 && ! command_line.force_common_definition)
3672 return;
3673
3674 if (! config.sort_common)
1579bae1 3675 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
252b5132
RH
3676 else
3677 {
3678 int power;
3679
3680 for (power = 4; power >= 0; power--)
1579bae1 3681 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
252b5132
RH
3682 }
3683}
3684
3685/* Place one common symbol in the correct section. */
3686
b34976b6 3687static bfd_boolean
1579bae1 3688lang_one_common (struct bfd_link_hash_entry *h, void *info)
252b5132
RH
3689{
3690 unsigned int power_of_two;
3691 bfd_vma size;
3692 asection *section;
5f992e62
AM
3693 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3694 ldfile_output_machine);
252b5132
RH
3695
3696 if (h->type != bfd_link_hash_common)
b34976b6 3697 return TRUE;
252b5132
RH
3698
3699 size = h->u.c.size;
3700 power_of_two = h->u.c.p->alignment_power;
3701
3702 if (config.sort_common
3703 && power_of_two < (unsigned int) *(int *) info)
b34976b6 3704 return TRUE;
252b5132
RH
3705
3706 section = h->u.c.p->section;
3707
3708 /* Increase the size of the section. */
c553bb91
AM
3709 section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3710 (bfd_vma) 1 << power_of_two) * opb;
252b5132
RH
3711
3712 /* Adjust the alignment if necessary. */
3713 if (power_of_two > section->alignment_power)
3714 section->alignment_power = power_of_two;
3715
3716 /* Change the symbol from common to defined. */
3717 h->type = bfd_link_hash_defined;
3718 h->u.def.section = section;
3719 h->u.def.value = section->_cooked_size;
3720
3721 /* Increase the size of the section. */
3722 section->_cooked_size += size;
3723
3724 /* Make sure the section is allocated in memory, and make sure that
3725 it is no longer a common section. */
3726 section->flags |= SEC_ALLOC;
08da4cac 3727 section->flags &= ~SEC_IS_COMMON;
252b5132
RH
3728
3729 if (config.map_file != NULL)
3730 {
b34976b6 3731 static bfd_boolean header_printed;
252b5132
RH
3732 int len;
3733 char *name;
3734 char buf[50];
3735
3736 if (! header_printed)
3737 {
3738 minfo (_("\nAllocating common symbols\n"));
3739 minfo (_("Common symbol size file\n\n"));
b34976b6 3740 header_printed = TRUE;
252b5132
RH
3741 }
3742
3743 name = demangle (h->root.string);
3744 minfo ("%s", name);
3745 len = strlen (name);
3746 free (name);
3747
3748 if (len >= 19)
3749 {
3750 print_nl ();
3751 len = 0;
3752 }
3753 while (len < 20)
3754 {
3755 print_space ();
3756 ++len;
3757 }
3758
3759 minfo ("0x");
3760 if (size <= 0xffffffff)
3761 sprintf (buf, "%lx", (unsigned long) size);
3762 else
3763 sprintf_vma (buf, size);
3764 minfo ("%s", buf);
3765 len = strlen (buf);
3766
3767 while (len < 16)
3768 {
3769 print_space ();
3770 ++len;
3771 }
3772
3773 minfo ("%B\n", section->owner);
3774 }
3775
b34976b6 3776 return TRUE;
252b5132
RH
3777}
3778
08da4cac
KH
3779/* Run through the input files and ensure that every input section has
3780 somewhere to go. If one is found without a destination then create
3781 an input request and place it into the statement tree. */
252b5132
RH
3782
3783static void
1579bae1 3784lang_place_orphans (void)
252b5132 3785{
e50d8076 3786 LANG_FOR_EACH_INPUT_STATEMENT (file)
252b5132
RH
3787 {
3788 asection *s;
3789
1579bae1 3790 for (s = file->the_bfd->sections; s != NULL; s = s->next)
252b5132 3791 {
1579bae1 3792 if (s->output_section == NULL)
252b5132 3793 {
396a2467 3794 /* This section of the file is not attached, root
08da4cac 3795 around for a sensible place for it to go. */
252b5132
RH
3796
3797 if (file->just_syms_flag)
3798 {
c2c01aa7 3799 abort ();
252b5132
RH
3800 }
3801 else if (strcmp (s->name, "COMMON") == 0)
3802 {
3803 /* This is a lonely common section which must have
3804 come from an archive. We attach to the section
3805 with the wildcard. */
1049f94e 3806 if (! link_info.relocatable
252b5132
RH
3807 || command_line.force_common_definition)
3808 {
3809 if (default_common_section == NULL)
3810 {
3811#if 0
3812 /* This message happens when using the
3813 svr3.ifile linker script, so I have
3814 disabled it. */
3815 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3816#endif
3817 default_common_section =
3818 lang_output_section_statement_lookup (".bss");
3819
3820 }
39dcfe18
AM
3821 lang_add_section (&default_common_section->children, s,
3822 default_common_section, file);
252b5132
RH
3823 }
3824 }
3825 else if (ldemul_place_orphan (file, s))
3826 ;
3827 else
3828 {
39dcfe18 3829 lang_output_section_statement_type *os;
252b5132 3830
39dcfe18
AM
3831 os = lang_output_section_statement_lookup (s->name);
3832 lang_add_section (&os->children, s, os, file);
252b5132
RH
3833 }
3834 }
3835 }
3836 }
3837}
3838
252b5132 3839void
1579bae1 3840lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
252b5132 3841{
aa8804e4 3842 flagword *ptr_flags;
252b5132 3843
aa8804e4 3844 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
252b5132
RH
3845 while (*flags)
3846 {
3847 switch (*flags)
3848 {
252b5132
RH
3849 case 'A': case 'a':
3850 *ptr_flags |= SEC_ALLOC;
3851 break;
3852
3853 case 'R': case 'r':
3854 *ptr_flags |= SEC_READONLY;
3855 break;
3856
3857 case 'W': case 'w':
3858 *ptr_flags |= SEC_DATA;
3859 break;
3860
3861 case 'X': case 'x':
3862 *ptr_flags |= SEC_CODE;
3863 break;
3864
3865 case 'L': case 'l':
3866 case 'I': case 'i':
3867 *ptr_flags |= SEC_LOAD;
3868 break;
3869
3870 default:
3871 einfo (_("%P%F: invalid syntax in flags\n"));
3872 break;
3873 }
3874 flags++;
3875 }
3876}
3877
3878/* Call a function on each input file. This function will be called
3879 on an archive, but not on the elements. */
3880
3881void
1579bae1 3882lang_for_each_input_file (void (*func) (lang_input_statement_type *))
252b5132
RH
3883{
3884 lang_input_statement_type *f;
3885
3886 for (f = (lang_input_statement_type *) input_file_chain.head;
3887 f != NULL;
3888 f = (lang_input_statement_type *) f->next_real_file)
3889 func (f);
3890}
3891
3892/* Call a function on each file. The function will be called on all
3893 the elements of an archive which are included in the link, but will
3894 not be called on the archive file itself. */
3895
3896void
1579bae1 3897lang_for_each_file (void (*func) (lang_input_statement_type *))
252b5132 3898{
e50d8076 3899 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132
RH
3900 {
3901 func (f);
3902 }
3903}
3904
3905#if 0
3906
3907/* Not used. */
3908
3909void
1579bae1 3910lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
252b5132 3911{
e50d8076 3912 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132 3913 {
08da4cac 3914 asection *s;
252b5132 3915
1579bae1
AM
3916 for (s = f->the_bfd->sections; s != NULL; s = s->next)
3917 func (f->the_bfd, s);
252b5132
RH
3918 }
3919}
3920
3921#endif
3922
3923void
1579bae1 3924ldlang_add_file (lang_input_statement_type *entry)
252b5132
RH
3925{
3926 bfd **pp;
3927
3928 lang_statement_append (&file_chain,
3929 (lang_statement_union_type *) entry,
3930 &entry->next);
3931
3932 /* The BFD linker needs to have a list of all input BFDs involved in
3933 a link. */
1579bae1 3934 ASSERT (entry->the_bfd->link_next == NULL);
252b5132 3935 ASSERT (entry->the_bfd != output_bfd);
1579bae1 3936 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
252b5132
RH
3937 ;
3938 *pp = entry->the_bfd;
1579bae1 3939 entry->the_bfd->usrdata = entry;
252b5132
RH
3940 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3941
3942 /* Look through the sections and check for any which should not be
3943 included in the link. We need to do this now, so that we can
3944 notice when the backend linker tries to report multiple
3945 definition errors for symbols which are in sections we aren't
3946 going to link. FIXME: It might be better to entirely ignore
3947 symbols which are defined in sections which are going to be
3948 discarded. This would require modifying the backend linker for
3949 each backend which might set the SEC_LINK_ONCE flag. If we do
3950 this, we should probably handle SEC_EXCLUDE in the same way. */
3951
1579bae1 3952 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
252b5132
RH
3953}
3954
3955void
1579bae1 3956lang_add_output (const char *name, int from_script)
252b5132
RH
3957{
3958 /* Make -o on command line override OUTPUT in script. */
7c519c12 3959 if (!had_output_filename || !from_script)
252b5132
RH
3960 {
3961 output_filename = name;
b34976b6 3962 had_output_filename = TRUE;
252b5132
RH
3963 }
3964}
3965
252b5132
RH
3966static lang_output_section_statement_type *current_section;
3967
3968static int
1579bae1 3969topower (int x)
252b5132
RH
3970{
3971 unsigned int i = 1;
3972 int l;
3973
3974 if (x < 0)
3975 return -1;
3976
5f992e62 3977 for (l = 0; l < 32; l++)
252b5132
RH
3978 {
3979 if (i >= (unsigned int) x)
3980 return l;
3981 i <<= 1;
3982 }
3983
3984 return 0;
3985}
3986
aea4bd9d 3987lang_output_section_statement_type *
1579bae1
AM
3988lang_enter_output_section_statement (const char *output_section_statement_name,
3989 etree_type *address_exp,
3990 enum section_type sectype,
3991 bfd_vma block_value,
3992 etree_type *align,
3993 etree_type *subalign,
3994 etree_type *ebase)
252b5132
RH
3995{
3996 lang_output_section_statement_type *os;
3997
3998 current_section =
3999 os =
4000 lang_output_section_statement_lookup (output_section_statement_name);
4001
08da4cac
KH
4002 /* Add this statement to tree. */
4003#if 0
4004 add_statement (lang_output_section_statement_enum,
4005 output_section_statement);
4006#endif
4007 /* Make next things chain into subchain of this. */
252b5132 4008
1579bae1 4009 if (os->addr_tree == NULL)
08da4cac
KH
4010 {
4011 os->addr_tree = address_exp;
4012 }
252b5132
RH
4013 os->sectype = sectype;
4014 if (sectype != noload_section)
4015 os->flags = SEC_NO_FLAGS;
4016 else
4017 os->flags = SEC_NEVER_LOAD;
4018 os->block_value = block_value ? block_value : 1;
4019 stat_ptr = &os->children;
4020
08da4cac
KH
4021 os->subsection_alignment =
4022 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4023 os->section_alignment =
4024 topower (exp_get_value_int (align, -1, "section alignment", 0));
252b5132
RH
4025
4026 os->load_base = ebase;
aea4bd9d 4027 return os;
252b5132
RH
4028}
4029
252b5132 4030void
1579bae1 4031lang_final (void)
252b5132
RH
4032{
4033 lang_output_statement_type *new =
4034 new_stat (lang_output_statement, stat_ptr);
4035
4036 new->name = output_filename;
4037}
4038
08da4cac
KH
4039/* Reset the current counters in the regions. */
4040
e3dc8847 4041void
1579bae1 4042lang_reset_memory_regions (void)
252b5132
RH
4043{
4044 lang_memory_region_type *p = lang_memory_region_list;
b3327aad 4045 asection *o;
252b5132 4046
1579bae1 4047 for (p = lang_memory_region_list; p != NULL; p = p->next)
252b5132
RH
4048 {
4049 p->old_length = (bfd_size_type) (p->current - p->origin);
4050 p->current = p->origin;
4051 }
b3327aad
AM
4052
4053 for (o = output_bfd->sections; o != NULL; o = o->next)
4054 o->_raw_size = 0;
252b5132
RH
4055}
4056
b6bf44ba
AM
4057/* If the wild pattern was marked KEEP, the member sections
4058 should be as well. */
252b5132
RH
4059
4060static void
1579bae1
AM
4061gc_section_callback (lang_wild_statement_type *ptr,
4062 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4063 asection *section,
4064 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4065 void *data ATTRIBUTE_UNUSED)
252b5132 4066{
4dec4d4e
RH
4067 if (ptr->keep_sections)
4068 section->flags |= SEC_KEEP;
252b5132
RH
4069}
4070
b6bf44ba 4071/* Handle a wild statement, marking it against GC. */
252b5132
RH
4072
4073static void
1579bae1 4074lang_gc_wild (lang_wild_statement_type *s)
252b5132 4075{
b6bf44ba 4076 walk_wild (s, gc_section_callback, NULL);
252b5132
RH
4077}
4078
4079/* Iterate over sections marking them against GC. */
4080
4081static void
1579bae1 4082lang_gc_sections_1 (lang_statement_union_type *s)
252b5132 4083{
1579bae1 4084 for (; s != NULL; s = s->header.next)
252b5132
RH
4085 {
4086 switch (s->header.type)
4087 {
4088 case lang_wild_statement_enum:
b6bf44ba 4089 lang_gc_wild (&s->wild_statement);
abc6ab0a 4090 break;
252b5132
RH
4091 case lang_constructors_statement_enum:
4092 lang_gc_sections_1 (constructor_list.head);
4093 break;
4094 case lang_output_section_statement_enum:
4095 lang_gc_sections_1 (s->output_section_statement.children.head);
4096 break;
4097 case lang_group_statement_enum:
4098 lang_gc_sections_1 (s->group_statement.children.head);
4099 break;
4100 default:
4101 break;
4102 }
4103 }
4104}
4105
4106static void
1579bae1 4107lang_gc_sections (void)
252b5132
RH
4108{
4109 struct bfd_link_hash_entry *h;
e3e942e9 4110 ldlang_undef_chain_list_type *ulist;
252b5132
RH
4111
4112 /* Keep all sections so marked in the link script. */
4113
4114 lang_gc_sections_1 (statement_list.head);
4115
e3e942e9
AM
4116 /* Keep all sections containing symbols undefined on the command-line,
4117 and the section containing the entry symbol. */
252b5132 4118
e3e942e9 4119 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
252b5132 4120 {
5f992e62 4121 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
b34976b6 4122 FALSE, FALSE, FALSE);
252b5132 4123
1579bae1 4124 if (h != NULL
08da4cac
KH
4125 && (h->type == bfd_link_hash_defined
4126 || h->type == bfd_link_hash_defweak)
252b5132
RH
4127 && ! bfd_is_abs_section (h->u.def.section))
4128 {
4129 h->u.def.section->flags |= SEC_KEEP;
4130 }
4131 }
4132
4133 bfd_gc_sections (output_bfd, &link_info);
4134}
4135
4136void
1579bae1 4137lang_process (void)
252b5132
RH
4138{
4139 lang_reasonable_defaults ();
4140 current_target = default_target;
4141
08da4cac
KH
4142 /* Open the output file. */
4143 lang_for_each_statement (ldlang_open_output);
252b5132
RH
4144
4145 ldemul_create_output_section_statements ();
4146
08da4cac 4147 /* Add to the hash table all undefineds on the command line. */
252b5132
RH
4148 lang_place_undefineds ();
4149
9503fd87
ILT
4150 already_linked_table_init ();
4151
08da4cac 4152 /* Create a bfd for each input file. */
252b5132 4153 current_target = default_target;
b34976b6 4154 open_input_bfds (statement_list.head, FALSE);
252b5132 4155
e3e942e9
AM
4156 link_info.gc_sym_list = &entry_symbol;
4157 if (entry_symbol.name == NULL)
4158 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4159
252b5132
RH
4160 ldemul_after_open ();
4161
9503fd87
ILT
4162 already_linked_table_free ();
4163
252b5132
RH
4164 /* Make sure that we're not mixing architectures. We call this
4165 after all the input files have been opened, but before we do any
4166 other processing, so that any operations merge_private_bfd_data
4167 does on the output file will be known during the rest of the
4168 link. */
4169 lang_check ();
4170
4171 /* Handle .exports instead of a version script if we're told to do so. */
4172 if (command_line.version_exports_section)
4173 lang_do_version_exports_section ();
4174
4175 /* Build all sets based on the information gathered from the input
4176 files. */
4177 ldctor_build_sets ();
4178
4179 /* Remove unreferenced sections if asked to. */
4180 if (command_line.gc_sections)
4181 lang_gc_sections ();
4182
8550eb6e
JJ
4183 /* If there were any SEC_MERGE sections, finish their merging, so that
4184 section sizes can be computed. This has to be done after GC of sections,
4185 so that GCed sections are not merged, but before assigning output
4186 sections, since removing whole input sections is hard then. */
4187 bfd_merge_sections (output_bfd, &link_info);
4188
08da4cac 4189 /* Size up the common data. */
252b5132
RH
4190 lang_common ();
4191
4192 /* Run through the contours of the script and attach input sections
08da4cac 4193 to the correct output sections. */
1579bae1 4194 map_input_to_output_sections (statement_list.head, NULL, NULL);
252b5132 4195
08da4cac 4196 /* Find any sections not attached explicitly and handle them. */
252b5132
RH
4197 lang_place_orphans ();
4198
1049f94e 4199 if (! link_info.relocatable)
862120bd
AM
4200 {
4201 /* Look for a text section and set the readonly attribute in it. */
4202 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4203
1579bae1 4204 if (found != NULL)
862120bd
AM
4205 {
4206 if (config.text_read_only)
4207 found->flags |= SEC_READONLY;
4208 else
4209 found->flags &= ~SEC_READONLY;
4210 }
4211 }
4212
4213 /* Do anything special before sizing sections. This is where ELF
4214 and other back-ends size dynamic sections. */
252b5132
RH
4215 ldemul_before_allocation ();
4216
1049f94e 4217 if (!link_info.relocatable)
4bd5a393
AM
4218 strip_excluded_output_sections ();
4219
252b5132
RH
4220 /* We must record the program headers before we try to fix the
4221 section positions, since they will affect SIZEOF_HEADERS. */
4222 lang_record_phdrs ();
4223
b3327aad 4224 /* Size up the sections. */
1579bae1
AM
4225 lang_size_sections (statement_list.head, abs_output_section,
4226 &statement_list.head, 0, 0, NULL,
cf888e70 4227 command_line.relax ? FALSE : TRUE);
b3327aad 4228
08da4cac 4229 /* Now run around and relax if we can. */
252b5132
RH
4230 if (command_line.relax)
4231 {
252b5132 4232 /* Keep relaxing until bfd_relax_section gives up. */
b34976b6 4233 bfd_boolean relax_again;
b3327aad 4234
252b5132
RH
4235 do
4236 {
e3dc8847 4237 lang_reset_memory_regions ();
252b5132 4238
b34976b6 4239 relax_again = FALSE;
252b5132
RH
4240
4241 /* Note: pe-dll.c does something like this also. If you find
4242 you need to change this code, you probably need to change
08da4cac 4243 pe-dll.c also. DJ */
252b5132
RH
4244
4245 /* Do all the assignments with our current guesses as to
4246 section sizes. */
1579bae1
AM
4247 lang_do_assignments (statement_list.head, abs_output_section,
4248 NULL, 0);
252b5132
RH
4249
4250 /* Perform another relax pass - this time we know where the
0d6d936f 4251 globals are, so can make a better guess. */
1579bae1
AM
4252 lang_size_sections (statement_list.head, abs_output_section,
4253 &statement_list.head, 0, 0, &relax_again, FALSE);
c7996ad6
L
4254
4255 /* If the normal relax is done and the relax finalize pass
4256 is not performed yet, we perform another relax pass. */
4257 if (!relax_again && !link_info.relax_finalizing)
4258 {
4259 link_info.relax_finalizing = TRUE;
4260 relax_again = TRUE;
4261 }
252b5132
RH
4262 }
4263 while (relax_again);
cf888e70
NC
4264
4265 /* Final extra sizing to report errors. */
4266 lang_reset_memory_regions ();
1579bae1
AM
4267 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4268 lang_size_sections (statement_list.head, abs_output_section,
4269 &statement_list.head, 0, 0, NULL, TRUE);
252b5132 4270 }
252b5132
RH
4271
4272 /* See if anything special should be done now we know how big
4273 everything is. */
4274 ldemul_after_allocation ();
4275
4276 /* Fix any .startof. or .sizeof. symbols. */
4277 lang_set_startof ();
4278
08da4cac
KH
4279 /* Do all the assignments, now that we know the final resting places
4280 of all the symbols. */
252b5132 4281
1579bae1 4282 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
252b5132
RH
4283
4284 /* Make sure that the section addresses make sense. */
1049f94e 4285 if (! link_info.relocatable
252b5132
RH
4286 && command_line.check_section_addresses)
4287 lang_check_section_addresses ();
5f992e62 4288
08da4cac 4289 /* Final stuffs. */
252b5132
RH
4290
4291 ldemul_finish ();
4292 lang_finish ();
4293}
4294
4295/* EXPORTED TO YACC */
4296
4297void
1579bae1
AM
4298lang_add_wild (struct wildcard_spec *filespec,
4299 struct wildcard_list *section_list,
4300 bfd_boolean keep_sections)
252b5132 4301{
b6bf44ba
AM
4302 struct wildcard_list *curr, *next;
4303 lang_wild_statement_type *new;
4304
4305 /* Reverse the list as the parser puts it back to front. */
4306 for (curr = section_list, section_list = NULL;
4307 curr != NULL;
4308 section_list = curr, curr = next)
4309 {
4310 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
b34976b6 4311 placed_commons = TRUE;
252b5132 4312
b6bf44ba
AM
4313 next = curr->next;
4314 curr->next = section_list;
4315 }
4316
4317 if (filespec != NULL && filespec->name != NULL)
252b5132 4318 {
b6bf44ba
AM
4319 if (strcmp (filespec->name, "*") == 0)
4320 filespec->name = NULL;
4321 else if (! wildcardp (filespec->name))
b34976b6 4322 lang_has_input_file = TRUE;
252b5132 4323 }
b6bf44ba
AM
4324
4325 new = new_stat (lang_wild_statement, stat_ptr);
4326 new->filename = NULL;
b34976b6 4327 new->filenames_sorted = FALSE;
b6bf44ba 4328 if (filespec != NULL)
252b5132 4329 {
b6bf44ba
AM
4330 new->filename = filespec->name;
4331 new->filenames_sorted = filespec->sorted;
252b5132 4332 }
b6bf44ba 4333 new->section_list = section_list;
252b5132 4334 new->keep_sections = keep_sections;
252b5132
RH
4335 lang_list_init (&new->children);
4336}
4337
4338void
1579bae1 4339lang_section_start (const char *name, etree_type *address)
252b5132 4340{
d1778b88 4341 lang_address_statement_type *ad;
252b5132 4342
d1778b88 4343 ad = new_stat (lang_address_statement, stat_ptr);
252b5132
RH
4344 ad->section_name = name;
4345 ad->address = address;
4346}
4347
4348/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4349 because of a -e argument on the command line, or zero if this is
4350 called by ENTRY in a linker script. Command line arguments take
4351 precedence. */
4352
4353void
1579bae1 4354lang_add_entry (const char *name, bfd_boolean cmdline)
252b5132 4355{
e3e942e9 4356 if (entry_symbol.name == NULL
252b5132
RH
4357 || cmdline
4358 || ! entry_from_cmdline)
4359 {
e3e942e9 4360 entry_symbol.name = name;
252b5132
RH
4361 entry_from_cmdline = cmdline;
4362 }
4363}
4364
4365void
1579bae1 4366lang_add_target (const char *name)
252b5132
RH
4367{
4368 lang_target_statement_type *new = new_stat (lang_target_statement,
4369 stat_ptr);
4370
4371 new->target = name;
4372
4373}
4374
4375void
1579bae1 4376lang_add_map (const char *name)
252b5132
RH
4377{
4378 while (*name)
4379 {
4380 switch (*name)
4381 {
08da4cac 4382 case 'F':
b34976b6 4383 map_option_f = TRUE;
252b5132
RH
4384 break;
4385 }
4386 name++;
4387 }
4388}
4389
4390void
1579bae1 4391lang_add_fill (fill_type *fill)
252b5132
RH
4392{
4393 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4394 stat_ptr);
4395
2c382fb6 4396 new->fill = fill;
252b5132
RH
4397}
4398
4399void
1579bae1 4400lang_add_data (int type, union etree_union *exp)
252b5132
RH
4401{
4402
4403 lang_data_statement_type *new = new_stat (lang_data_statement,
4404 stat_ptr);
4405
4406 new->exp = exp;
4407 new->type = type;
4408
4409}
4410
4411/* Create a new reloc statement. RELOC is the BFD relocation type to
4412 generate. HOWTO is the corresponding howto structure (we could
4413 look this up, but the caller has already done so). SECTION is the
4414 section to generate a reloc against, or NAME is the name of the
4415 symbol to generate a reloc against. Exactly one of SECTION and
4416 NAME must be NULL. ADDEND is an expression for the addend. */
4417
4418void
1579bae1
AM
4419lang_add_reloc (bfd_reloc_code_real_type reloc,
4420 reloc_howto_type *howto,
4421 asection *section,
4422 const char *name,
4423 union etree_union *addend)
252b5132
RH
4424{
4425 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5f992e62 4426
252b5132
RH
4427 p->reloc = reloc;
4428 p->howto = howto;
4429 p->section = section;
4430 p->name = name;
4431 p->addend_exp = addend;
4432
4433 p->addend_value = 0;
4434 p->output_section = NULL;
4435 p->output_vma = 0;
4436}
4437
4438lang_assignment_statement_type *
1579bae1 4439lang_add_assignment (etree_type *exp)
252b5132
RH
4440{
4441 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4442 stat_ptr);
4443
4444 new->exp = exp;
4445 return new;
4446}
4447
4448void
1579bae1 4449lang_add_attribute (enum statement_enum attribute)
252b5132
RH
4450{
4451 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4452}
4453
4454void
1579bae1 4455lang_startup (const char *name)
252b5132 4456{
1579bae1 4457 if (startup_file != NULL)
252b5132
RH
4458 {
4459 einfo (_("%P%Fmultiple STARTUP files\n"));
4460 }
4461 first_file->filename = name;
4462 first_file->local_sym_name = name;
b34976b6 4463 first_file->real = TRUE;
252b5132
RH
4464
4465 startup_file = name;
4466}
4467
4468void
1579bae1 4469lang_float (bfd_boolean maybe)
252b5132
RH
4470{
4471 lang_float_flag = maybe;
4472}
4473
ee3cc2e2
RS
4474
4475/* Work out the load- and run-time regions from a script statement, and
4476 store them in *LMA_REGION and *REGION respectively.
4477
a747ee4d
NC
4478 MEMSPEC is the name of the run-time region, or the value of
4479 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4480 LMA_MEMSPEC is the name of the load-time region, or null if the
4481 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4482 had an explicit load address.
ee3cc2e2
RS
4483
4484 It is an error to specify both a load region and a load address. */
4485
4486static void
1579bae1
AM
4487lang_get_regions (struct memory_region_struct **region,
4488 struct memory_region_struct **lma_region,
4489 const char *memspec,
4490 const char *lma_memspec,
4491 int have_lma_p)
ee3cc2e2 4492{
a747ee4d 4493 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
ee3cc2e2
RS
4494
4495 /* If no runtime region has been given, but the load region has
4496 been, use the load region. */
a747ee4d 4497 if (lma_memspec != 0 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
ee3cc2e2
RS
4498 *region = *lma_region;
4499 else
a747ee4d 4500 *region = lang_memory_region_lookup (memspec, FALSE);
ee3cc2e2
RS
4501
4502 if (have_lma_p && lma_memspec != 0)
4503 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4504}
4505
252b5132 4506void
1579bae1
AM
4507lang_leave_output_section_statement
4508 (fill_type *fill, const char *memspec,
4509 struct lang_output_section_phdr_list *phdrs, const char *lma_memspec)
252b5132 4510{
ee3cc2e2
RS
4511 lang_get_regions (&current_section->region,
4512 &current_section->lma_region,
4513 memspec, lma_memspec,
4514 current_section->load_base != 0);
252b5132 4515 current_section->fill = fill;
252b5132
RH
4516 current_section->phdrs = phdrs;
4517 stat_ptr = &statement_list;
4518}
4519
08da4cac
KH
4520/* Create an absolute symbol with the given name with the value of the
4521 address of first byte of the section named.
4522
4523 If the symbol already exists, then do nothing. */
252b5132 4524
252b5132 4525void
1579bae1 4526lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
252b5132
RH
4527{
4528 struct bfd_link_hash_entry *h;
4529
b34976b6 4530 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 4531 if (h == NULL)
252b5132
RH
4532 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4533
4534 if (h->type == bfd_link_hash_new
4535 || h->type == bfd_link_hash_undefined)
4536 {
4537 asection *sec;
4538
4539 h->type = bfd_link_hash_defined;
4540
4541 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 4542 if (sec == NULL)
252b5132
RH
4543 h->u.def.value = 0;
4544 else
4545 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4546
4547 h->u.def.section = bfd_abs_section_ptr;
4548 }
4549}
4550
08da4cac
KH
4551/* Create an absolute symbol with the given name with the value of the
4552 address of the first byte after the end of the section named.
4553
4554 If the symbol already exists, then do nothing. */
252b5132 4555
252b5132 4556void
1579bae1 4557lang_abs_symbol_at_end_of (const char *secname, const char *name)
252b5132
RH
4558{
4559 struct bfd_link_hash_entry *h;
4560
b34976b6 4561 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 4562 if (h == NULL)
252b5132
RH
4563 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4564
4565 if (h->type == bfd_link_hash_new
4566 || h->type == bfd_link_hash_undefined)
4567 {
4568 asection *sec;
4569
4570 h->type = bfd_link_hash_defined;
4571
4572 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 4573 if (sec == NULL)
252b5132
RH
4574 h->u.def.value = 0;
4575 else
4576 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4cbfc3ac
TW
4577 + bfd_section_size (output_bfd, sec) /
4578 bfd_octets_per_byte (output_bfd));
252b5132
RH
4579
4580 h->u.def.section = bfd_abs_section_ptr;
4581 }
4582}
4583
4584void
1579bae1
AM
4585lang_statement_append (lang_statement_list_type *list,
4586 lang_statement_union_type *element,
4587 lang_statement_union_type **field)
252b5132
RH
4588{
4589 *(list->tail) = element;
4590 list->tail = field;
4591}
4592
4593/* Set the output format type. -oformat overrides scripts. */
4594
4595void
1579bae1
AM
4596lang_add_output_format (const char *format,
4597 const char *big,
4598 const char *little,
4599 int from_script)
252b5132
RH
4600{
4601 if (output_target == NULL || !from_script)
4602 {
4603 if (command_line.endian == ENDIAN_BIG
4604 && big != NULL)
4605 format = big;
4606 else if (command_line.endian == ENDIAN_LITTLE
4607 && little != NULL)
4608 format = little;
4609
4610 output_target = format;
4611 }
4612}
4613
4614/* Enter a group. This creates a new lang_group_statement, and sets
4615 stat_ptr to build new statements within the group. */
4616
4617void
1579bae1 4618lang_enter_group (void)
252b5132
RH
4619{
4620 lang_group_statement_type *g;
4621
4622 g = new_stat (lang_group_statement, stat_ptr);
4623 lang_list_init (&g->children);
4624 stat_ptr = &g->children;
4625}
4626
4627/* Leave a group. This just resets stat_ptr to start writing to the
4628 regular list of statements again. Note that this will not work if
4629 groups can occur inside anything else which can adjust stat_ptr,
4630 but currently they can't. */
4631
4632void
1579bae1 4633lang_leave_group (void)
252b5132
RH
4634{
4635 stat_ptr = &statement_list;
4636}
4637
4638/* Add a new program header. This is called for each entry in a PHDRS
4639 command in a linker script. */
4640
4641void
1579bae1
AM
4642lang_new_phdr (const char *name,
4643 etree_type *type,
4644 bfd_boolean filehdr,
4645 bfd_boolean phdrs,
4646 etree_type *at,
4647 etree_type *flags)
252b5132
RH
4648{
4649 struct lang_phdr *n, **pp;
4650
1579bae1 4651 n = stat_alloc (sizeof (struct lang_phdr));
252b5132
RH
4652 n->next = NULL;
4653 n->name = name;
4654 n->type = exp_get_value_int (type, 0, "program header type",
4655 lang_final_phase_enum);
4656 n->filehdr = filehdr;
4657 n->phdrs = phdrs;
4658 n->at = at;
4659 n->flags = flags;
4660
4661 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4662 ;
4663 *pp = n;
4664}
4665
4666/* Record the program header information in the output BFD. FIXME: We
4667 should not be calling an ELF specific function here. */
4668
4669static void
1579bae1 4670lang_record_phdrs (void)
252b5132
RH
4671{
4672 unsigned int alc;
4673 asection **secs;
4674 struct lang_output_section_phdr_list *last;
4675 struct lang_phdr *l;
4676 lang_statement_union_type *u;
4677
4678 alc = 10;
1579bae1 4679 secs = xmalloc (alc * sizeof (asection *));
252b5132
RH
4680 last = NULL;
4681 for (l = lang_phdr_list; l != NULL; l = l->next)
4682 {
4683 unsigned int c;
4684 flagword flags;
4685 bfd_vma at;
4686
4687 c = 0;
4688 for (u = lang_output_section_statement.head;
4689 u != NULL;
4690 u = u->output_section_statement.next)
4691 {
4692 lang_output_section_statement_type *os;
4693 struct lang_output_section_phdr_list *pl;
4694
4695 os = &u->output_section_statement;
4696
4697 pl = os->phdrs;
4698 if (pl != NULL)
4699 last = pl;
4700 else
4701 {
4702 if (os->sectype == noload_section
4703 || os->bfd_section == NULL
4704 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4705 continue;
4706 pl = last;
4707 }
4708
4709 if (os->bfd_section == NULL)
4710 continue;
4711
4712 for (; pl != NULL; pl = pl->next)
4713 {
4714 if (strcmp (pl->name, l->name) == 0)
4715 {
4716 if (c >= alc)
4717 {
4718 alc *= 2;
1579bae1 4719 secs = xrealloc (secs, alc * sizeof (asection *));
252b5132
RH
4720 }
4721 secs[c] = os->bfd_section;
4722 ++c;
b34976b6 4723 pl->used = TRUE;
252b5132
RH
4724 }
4725 }
4726 }
4727
4728 if (l->flags == NULL)
4729 flags = 0;
4730 else
4731 flags = exp_get_vma (l->flags, 0, "phdr flags",
4732 lang_final_phase_enum);
4733
4734 if (l->at == NULL)
4735 at = 0;
4736 else
4737 at = exp_get_vma (l->at, 0, "phdr load address",
4738 lang_final_phase_enum);
4739
4740 if (! bfd_record_phdr (output_bfd, l->type,
d1778b88 4741 l->flags != NULL, flags, l->at != NULL,
252b5132
RH
4742 at, l->filehdr, l->phdrs, c, secs))
4743 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4744 }
4745
4746 free (secs);
4747
4748 /* Make sure all the phdr assignments succeeded. */
4749 for (u = lang_output_section_statement.head;
4750 u != NULL;
4751 u = u->output_section_statement.next)
4752 {
4753 struct lang_output_section_phdr_list *pl;
4754
4755 if (u->output_section_statement.bfd_section == NULL)
4756 continue;
4757
4758 for (pl = u->output_section_statement.phdrs;
4759 pl != NULL;
4760 pl = pl->next)
4761 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4762 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4763 u->output_section_statement.name, pl->name);
4764 }
4765}
4766
4767/* Record a list of sections which may not be cross referenced. */
4768
4769void
1579bae1 4770lang_add_nocrossref (struct lang_nocrossref *l)
252b5132
RH
4771{
4772 struct lang_nocrossrefs *n;
4773
1579bae1 4774 n = xmalloc (sizeof *n);
252b5132
RH
4775 n->next = nocrossref_list;
4776 n->list = l;
4777 nocrossref_list = n;
4778
4779 /* Set notice_all so that we get informed about all symbols. */
b34976b6 4780 link_info.notice_all = TRUE;
252b5132
RH
4781}
4782\f
4783/* Overlay handling. We handle overlays with some static variables. */
4784
4785/* The overlay virtual address. */
4786static etree_type *overlay_vma;
7e7d5768
AM
4787/* And subsection alignment. */
4788static etree_type *overlay_subalign;
252b5132 4789
252b5132
RH
4790/* An expression for the maximum section size seen so far. */
4791static etree_type *overlay_max;
4792
4793/* A list of all the sections in this overlay. */
4794
89cdebba 4795struct overlay_list {
252b5132
RH
4796 struct overlay_list *next;
4797 lang_output_section_statement_type *os;
4798};
4799
4800static struct overlay_list *overlay_list;
4801
4802/* Start handling an overlay. */
4803
4804void
7e7d5768 4805lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
252b5132
RH
4806{
4807 /* The grammar should prevent nested overlays from occurring. */
7e7d5768
AM
4808 ASSERT (overlay_vma == NULL
4809 && overlay_subalign == NULL
4810 && overlay_max == NULL);
252b5132
RH
4811
4812 overlay_vma = vma_expr;
7e7d5768 4813 overlay_subalign = subalign;
252b5132
RH
4814}
4815
4816/* Start a section in an overlay. We handle this by calling
9f88b410
RS
4817 lang_enter_output_section_statement with the correct VMA.
4818 lang_leave_overlay sets up the LMA and memory regions. */
252b5132
RH
4819
4820void
1579bae1 4821lang_enter_overlay_section (const char *name)
252b5132
RH
4822{
4823 struct overlay_list *n;
4824 etree_type *size;
4825
4826 lang_enter_output_section_statement (name, overlay_vma, normal_section,
7e7d5768 4827 0, 0, overlay_subalign, 0);
252b5132 4828
9f88b410 4829 /* If this is the first section, then base the VMA of future
252b5132
RH
4830 sections on this one. This will work correctly even if `.' is
4831 used in the addresses. */
4832 if (overlay_list == NULL)
9f88b410 4833 overlay_vma = exp_nameop (ADDR, name);
252b5132
RH
4834
4835 /* Remember the section. */
1579bae1 4836 n = xmalloc (sizeof *n);
252b5132
RH
4837 n->os = current_section;
4838 n->next = overlay_list;
4839 overlay_list = n;
4840
4841 size = exp_nameop (SIZEOF, name);
4842
252b5132
RH
4843 /* Arrange to work out the maximum section end address. */
4844 if (overlay_max == NULL)
4845 overlay_max = size;
4846 else
4847 overlay_max = exp_binop (MAX_K, overlay_max, size);
4848}
4849
4850/* Finish a section in an overlay. There isn't any special to do
4851 here. */
4852
4853void
1579bae1
AM
4854lang_leave_overlay_section (fill_type *fill,
4855 struct lang_output_section_phdr_list *phdrs)
252b5132
RH
4856{
4857 const char *name;
4858 char *clean, *s2;
4859 const char *s1;
4860 char *buf;
4861
4862 name = current_section->name;
4863
a747ee4d
NC
4864 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4865 region and that no load-time region has been specified. It doesn't
4866 really matter what we say here, since lang_leave_overlay will
4867 override it. */
4868 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
252b5132
RH
4869
4870 /* Define the magic symbols. */
4871
4872 clean = xmalloc (strlen (name) + 1);
4873 s2 = clean;
4874 for (s1 = name; *s1 != '\0'; s1++)
3882b010 4875 if (ISALNUM (*s1) || *s1 == '_')
252b5132
RH
4876 *s2++ = *s1;
4877 *s2 = '\0';
4878
4879 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4880 sprintf (buf, "__load_start_%s", clean);
4881 lang_add_assignment (exp_assop ('=', buf,
4882 exp_nameop (LOADADDR, name)));
4883
4884 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4885 sprintf (buf, "__load_stop_%s", clean);
4886 lang_add_assignment (exp_assop ('=', buf,
4887 exp_binop ('+',
4888 exp_nameop (LOADADDR, name),
4889 exp_nameop (SIZEOF, name))));
4890
4891 free (clean);
4892}
4893
4894/* Finish an overlay. If there are any overlay wide settings, this
4895 looks through all the sections in the overlay and sets them. */
4896
4897void
1579bae1
AM
4898lang_leave_overlay (etree_type *lma_expr,
4899 int nocrossrefs,
4900 fill_type *fill,
4901 const char *memspec,
4902 struct lang_output_section_phdr_list *phdrs,
4903 const char *lma_memspec)
252b5132
RH
4904{
4905 lang_memory_region_type *region;
562d3460 4906 lang_memory_region_type *lma_region;
252b5132
RH
4907 struct overlay_list *l;
4908 struct lang_nocrossref *nocrossref;
4909
ee3cc2e2
RS
4910 lang_get_regions (&region, &lma_region,
4911 memspec, lma_memspec,
4912 lma_expr != 0);
562d3460 4913
252b5132
RH
4914 nocrossref = NULL;
4915
9f88b410
RS
4916 /* After setting the size of the last section, set '.' to end of the
4917 overlay region. */
4918 if (overlay_list != NULL)
4919 overlay_list->os->update_dot_tree
4920 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4921
252b5132
RH
4922 l = overlay_list;
4923 while (l != NULL)
4924 {
4925 struct overlay_list *next;
4926
1579bae1 4927 if (fill != NULL && l->os->fill == NULL)
252b5132 4928 l->os->fill = fill;
1545243b 4929
9f88b410
RS
4930 l->os->region = region;
4931 l->os->lma_region = lma_region;
4932
4933 /* The first section has the load address specified in the
4934 OVERLAY statement. The rest are worked out from that.
4935 The base address is not needed (and should be null) if
4936 an LMA region was specified. */
4937 if (l->next == 0)
4938 l->os->load_base = lma_expr;
4939 else if (lma_region == 0)
4940 l->os->load_base = exp_binop ('+',
4941 exp_nameop (LOADADDR, l->next->os->name),
4942 exp_nameop (SIZEOF, l->next->os->name));
1545243b 4943
252b5132
RH
4944 if (phdrs != NULL && l->os->phdrs == NULL)
4945 l->os->phdrs = phdrs;
4946
9f88b410 4947 if (nocrossrefs)
252b5132
RH
4948 {
4949 struct lang_nocrossref *nc;
4950
1579bae1 4951 nc = xmalloc (sizeof *nc);
252b5132
RH
4952 nc->name = l->os->name;
4953 nc->next = nocrossref;
4954 nocrossref = nc;
4955 }
4956
4957 next = l->next;
4958 free (l);
4959 l = next;
4960 }
4961
4962 if (nocrossref != NULL)
4963 lang_add_nocrossref (nocrossref);
4964
252b5132 4965 overlay_vma = NULL;
252b5132
RH
4966 overlay_list = NULL;
4967 overlay_max = NULL;
4968}
4969\f
4970/* Version handling. This is only useful for ELF. */
4971
4972/* This global variable holds the version tree that we build. */
4973
4974struct bfd_elf_version_tree *lang_elf_version_info;
4975
4976static int
1579bae1
AM
4977lang_vers_match_lang_c (struct bfd_elf_version_expr *expr,
4978 const char *sym)
252b5132
RH
4979{
4980 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4981 return 1;
4982 return fnmatch (expr->pattern, sym, 0) == 0;
4983}
4984
4985static int
1579bae1
AM
4986lang_vers_match_lang_cplusplus (struct bfd_elf_version_expr *expr,
4987 const char *sym)
252b5132
RH
4988{
4989 char *alt_sym;
4990 int result;
4991
4992 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4993 return 1;
4994
08da4cac 4995 alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
252b5132
RH
4996 if (!alt_sym)
4997 {
4998 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
b34976b6 4999 Should we early out FALSE in this case? */
252b5132
RH
5000 result = fnmatch (expr->pattern, sym, 0) == 0;
5001 }
5002 else
5003 {
5004 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5005 free (alt_sym);
5006 }
5007
5008 return result;
5009}
5010
5011static int
1579bae1
AM
5012lang_vers_match_lang_java (struct bfd_elf_version_expr *expr,
5013 const char *sym)
252b5132
RH
5014{
5015 char *alt_sym;
5016 int result;
5017
5018 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5019 return 1;
5020
08da4cac 5021 alt_sym = cplus_demangle (sym, DMGL_JAVA);
252b5132
RH
5022 if (!alt_sym)
5023 {
5024 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
b34976b6 5025 Should we early out FALSE in this case? */
252b5132
RH
5026 result = fnmatch (expr->pattern, sym, 0) == 0;
5027 }
5028 else
5029 {
5030 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5031 free (alt_sym);
5032 }
5033
5034 return result;
5035}
5036
5037/* This is called for each variable name or match expression. */
5038
5039struct bfd_elf_version_expr *
1579bae1
AM
5040lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5041 const char *new,
5042 const char *lang)
252b5132
RH
5043{
5044 struct bfd_elf_version_expr *ret;
5045
1579bae1 5046 ret = xmalloc (sizeof *ret);
252b5132
RH
5047 ret->next = orig;
5048 ret->pattern = new;
31941635
L
5049 ret->symver = 0;
5050 ret->script = 0;
252b5132
RH
5051
5052 if (lang == NULL || strcasecmp (lang, "C") == 0)
5053 ret->match = lang_vers_match_lang_c;
5054 else if (strcasecmp (lang, "C++") == 0)
5055 ret->match = lang_vers_match_lang_cplusplus;
5056 else if (strcasecmp (lang, "Java") == 0)
5057 ret->match = lang_vers_match_lang_java;
5058 else
5059 {
5060 einfo (_("%X%P: unknown language `%s' in version information\n"),
5061 lang);
5062 ret->match = lang_vers_match_lang_c;
5063 }
5064
fac1652d 5065 return ldemul_new_vers_pattern (ret);
252b5132
RH
5066}
5067
5068/* This is called for each set of variable names and match
5069 expressions. */
5070
5071struct bfd_elf_version_tree *
1579bae1
AM
5072lang_new_vers_node (struct bfd_elf_version_expr *globals,
5073 struct bfd_elf_version_expr *locals)
252b5132
RH
5074{
5075 struct bfd_elf_version_tree *ret;
5076
1579bae1 5077 ret = xmalloc (sizeof *ret);
252b5132
RH
5078 ret->next = NULL;
5079 ret->name = NULL;
5080 ret->vernum = 0;
5081 ret->globals = globals;
5082 ret->locals = locals;
5083 ret->deps = NULL;
5084 ret->name_indx = (unsigned int) -1;
5085 ret->used = 0;
5086 return ret;
5087}
5088
5089/* This static variable keeps track of version indices. */
5090
5091static int version_index;
5092
5093/* This is called when we know the name and dependencies of the
5094 version. */
5095
5096void
1579bae1
AM
5097lang_register_vers_node (const char *name,
5098 struct bfd_elf_version_tree *version,
5099 struct bfd_elf_version_deps *deps)
252b5132
RH
5100{
5101 struct bfd_elf_version_tree *t, **pp;
5102 struct bfd_elf_version_expr *e1;
5103
6b9b879a
JJ
5104 if (name == NULL)
5105 name = "";
5106
5107 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5108 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5109 {
5110 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5ed6aba4 5111 free (version);
6b9b879a
JJ
5112 return;
5113 }
5114
252b5132
RH
5115 /* Make sure this node has a unique name. */
5116 for (t = lang_elf_version_info; t != NULL; t = t->next)
5117 if (strcmp (t->name, name) == 0)
5118 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5119
5120 /* Check the global and local match names, and make sure there
5121 aren't any duplicates. */
5122
5123 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
5124 {
5125 for (t = lang_elf_version_info; t != NULL; t = t->next)
5126 {
5127 struct bfd_elf_version_expr *e2;
5128
5129 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
5130 if (strcmp (e1->pattern, e2->pattern) == 0)
5131 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5132 e1->pattern);
5133 }
5134 }
5135
5136 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
5137 {
5138 for (t = lang_elf_version_info; t != NULL; t = t->next)
5139 {
5140 struct bfd_elf_version_expr *e2;
5141
5142 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
5143 if (strcmp (e1->pattern, e2->pattern) == 0)
5144 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5145 e1->pattern);
5146 }
5147 }
5148
5149 version->deps = deps;
5150 version->name = name;
6b9b879a
JJ
5151 if (name[0] != '\0')
5152 {
5153 ++version_index;
5154 version->vernum = version_index;
5155 }
5156 else
5157 version->vernum = 0;
252b5132
RH
5158
5159 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5160 ;
5161 *pp = version;
5162}
5163
5164/* This is called when we see a version dependency. */
5165
5166struct bfd_elf_version_deps *
1579bae1 5167lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
252b5132
RH
5168{
5169 struct bfd_elf_version_deps *ret;
5170 struct bfd_elf_version_tree *t;
5171
1579bae1 5172 ret = xmalloc (sizeof *ret);
252b5132
RH
5173 ret->next = list;
5174
5175 for (t = lang_elf_version_info; t != NULL; t = t->next)
5176 {
5177 if (strcmp (t->name, name) == 0)
5178 {
5179 ret->version_needed = t;
5180 return ret;
5181 }
5182 }
5183
5184 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5185
5186 return ret;
5187}
5188
5189static void
1579bae1 5190lang_do_version_exports_section (void)
252b5132
RH
5191{
5192 struct bfd_elf_version_expr *greg = NULL, *lreg;
5193
5194 LANG_FOR_EACH_INPUT_STATEMENT (is)
5195 {
5196 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5197 char *contents, *p;
5198 bfd_size_type len;
5199
5200 if (sec == NULL)
b7a26f91 5201 continue;
252b5132
RH
5202
5203 len = bfd_section_size (is->the_bfd, sec);
5204 contents = xmalloc (len);
5205 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6f9efd97 5206 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
252b5132
RH
5207
5208 p = contents;
89cdebba 5209 while (p < contents + len)
252b5132 5210 {
313e35ee 5211 greg = lang_new_vers_pattern (greg, p, NULL);
252b5132
RH
5212 p = strchr (p, '\0') + 1;
5213 }
5214
5215 /* Do not free the contents, as we used them creating the regex. */
5216
5217 /* Do not include this section in the link. */
5218 bfd_set_section_flags (is->the_bfd, sec,
5219 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5220 }
5221
313e35ee 5222 lreg = lang_new_vers_pattern (NULL, "*", NULL);
252b5132
RH
5223 lang_register_vers_node (command_line.version_exports_section,
5224 lang_new_vers_node (greg, lreg), NULL);
5225}
577a0623
AM
5226
5227void
1579bae1 5228lang_add_unique (const char *name)
577a0623
AM
5229{
5230 struct unique_sections *ent;
5231
5232 for (ent = unique_section_list; ent; ent = ent->next)
5233 if (strcmp (ent->name, name) == 0)
5234 return;
5235
1579bae1 5236 ent = xmalloc (sizeof *ent);
577a0623
AM
5237 ent->name = xstrdup (name);
5238 ent->next = unique_section_list;
5239 unique_section_list = ent;
5240}
This page took 0.517719 seconds and 4 git commands to generate.