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