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