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