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