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