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