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