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