* coff-i860.c (coff_i860_reloc_nyi): Return bfd_reloc_not_supported.
[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,
cf888e70 3 2001, 2002, 2003
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
1802/* Open all the input files. */
1803
1804static void
1579bae1 1805open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
252b5132 1806{
1579bae1 1807 for (; s != NULL; s = s->header.next)
252b5132
RH
1808 {
1809 switch (s->header.type)
1810 {
1811 case lang_constructors_statement_enum:
1812 open_input_bfds (constructor_list.head, force);
1813 break;
1814 case lang_output_section_statement_enum:
1815 open_input_bfds (s->output_section_statement.children.head, force);
1816 break;
1817 case lang_wild_statement_enum:
08da4cac 1818 /* Maybe we should load the file's symbols. */
252b5132
RH
1819 if (s->wild_statement.filename
1820 && ! wildcardp (s->wild_statement.filename))
1821 (void) lookup_name (s->wild_statement.filename);
1822 open_input_bfds (s->wild_statement.children.head, force);
1823 break;
1824 case lang_group_statement_enum:
1825 {
1826 struct bfd_link_hash_entry *undefs;
1827
1828 /* We must continually search the entries in the group
08da4cac
KH
1829 until no new symbols are added to the list of undefined
1830 symbols. */
252b5132
RH
1831
1832 do
1833 {
1834 undefs = link_info.hash->undefs_tail;
b34976b6 1835 open_input_bfds (s->group_statement.children.head, TRUE);
252b5132
RH
1836 }
1837 while (undefs != link_info.hash->undefs_tail);
1838 }
1839 break;
1840 case lang_target_statement_enum:
1841 current_target = s->target_statement.target;
1842 break;
1843 case lang_input_statement_enum:
e50d8076 1844 if (s->input_statement.real)
252b5132
RH
1845 {
1846 lang_statement_list_type add;
1847
1848 s->input_statement.target = current_target;
1849
1850 /* If we are being called from within a group, and this
1851 is an archive which has already been searched, then
cd4c806a
L
1852 force it to be researched unless the whole archive
1853 has been loaded already. */
252b5132 1854 if (force
cd4c806a 1855 && !s->input_statement.whole_archive
252b5132
RH
1856 && s->input_statement.loaded
1857 && bfd_check_format (s->input_statement.the_bfd,
1858 bfd_archive))
b34976b6 1859 s->input_statement.loaded = FALSE;
252b5132 1860
d1778b88 1861 lang_list_init (&add);
1276aefa 1862
6770ec8c 1863 if (! load_symbols (&s->input_statement, &add))
b34976b6 1864 config.make_executable = FALSE;
252b5132
RH
1865
1866 if (add.head != NULL)
1867 {
bba1a0c0
AM
1868 *add.tail = s->header.next;
1869 s->header.next = add.head;
252b5132
RH
1870 }
1871 }
1872 break;
1873 default:
1874 break;
1875 }
1876 }
1877}
1878
08da4cac
KH
1879/* If there are [COMMONS] statements, put a wild one into the bss
1880 section. */
252b5132
RH
1881
1882static void
1579bae1 1883lang_reasonable_defaults (void)
252b5132
RH
1884{
1885#if 0
1886 lang_output_section_statement_lookup (".text");
1887 lang_output_section_statement_lookup (".data");
1888
08da4cac 1889 default_common_section = lang_output_section_statement_lookup (".bss");
252b5132 1890
7c519c12 1891 if (!placed_commons)
252b5132
RH
1892 {
1893 lang_wild_statement_type *new =
1894 new_stat (lang_wild_statement,
1895 &default_common_section->children);
1896
1897 new->section_name = "COMMON";
1579bae1 1898 new->filename = NULL;
252b5132
RH
1899 lang_list_init (&new->children);
1900 }
1901#endif
252b5132
RH
1902}
1903
420e579c
HPN
1904/* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1905
1906void
1907lang_track_definedness (const char *name)
1908{
1909 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1910 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1911}
1912
1913/* New-function for the definedness hash table. */
1914
1915static struct bfd_hash_entry *
1916lang_definedness_newfunc (struct bfd_hash_entry *entry,
1917 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1918 const char *name ATTRIBUTE_UNUSED)
1919{
1920 struct lang_definedness_hash_entry *ret
1921 = (struct lang_definedness_hash_entry *) entry;
1922
1923 if (ret == NULL)
1924 ret = (struct lang_definedness_hash_entry *)
1925 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1926
1927 if (ret == NULL)
1928 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1929
1930 ret->iteration = -1;
1931 return &ret->root;
1932}
1933
1934/* Return the iteration when the definition of NAME was last updated. A
1935 value of -1 means that the symbol is not defined in the linker script
1936 or the command line, but may be defined in the linker symbol table. */
1937
1938int
1939lang_symbol_definition_iteration (const char *name)
1940{
1941 struct lang_definedness_hash_entry *defentry
1942 = (struct lang_definedness_hash_entry *)
1943 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1944
1945 /* We've already created this one on the presence of DEFINED in the
1946 script, so it can't be NULL unless something is borked elsewhere in
1947 the code. */
1948 if (defentry == NULL)
1949 FAIL ();
1950
1951 return defentry->iteration;
1952}
1953
1954/* Update the definedness state of NAME. */
1955
1956void
1957lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1958{
1959 struct lang_definedness_hash_entry *defentry
1960 = (struct lang_definedness_hash_entry *)
1961 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1962
1963 /* We don't keep track of symbols not tested with DEFINED. */
1964 if (defentry == NULL)
1965 return;
1966
1967 /* If the symbol was already defined, and not from an earlier statement
1968 iteration, don't update the definedness iteration, because that'd
1969 make the symbol seem defined in the linker script at this point, and
1970 it wasn't; it was defined in some object. If we do anyway, DEFINED
1971 would start to yield false before this point and the construct "sym =
1972 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1973 in an object. */
1974 if (h->type != bfd_link_hash_undefined
1975 && h->type != bfd_link_hash_common
1976 && h->type != bfd_link_hash_new
1977 && defentry->iteration == -1)
1978 return;
1979
1980 defentry->iteration = lang_statement_iteration;
1981}
1982
08da4cac 1983/* Add the supplied name to the symbol table as an undefined reference.
fcf0e35b
AM
1984 This is a two step process as the symbol table doesn't even exist at
1985 the time the ld command line is processed. First we put the name
1986 on a list, then, once the output file has been opened, transfer the
1987 name to the symbol table. */
1988
e3e942e9 1989typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
252b5132 1990
e3e942e9 1991#define ldlang_undef_chain_list_head entry_symbol.next
252b5132
RH
1992
1993void
1579bae1 1994ldlang_add_undef (const char *const name)
252b5132
RH
1995{
1996 ldlang_undef_chain_list_type *new =
1579bae1 1997 stat_alloc (sizeof (ldlang_undef_chain_list_type));
252b5132
RH
1998
1999 new->next = ldlang_undef_chain_list_head;
2000 ldlang_undef_chain_list_head = new;
2001
d1b2b2dc 2002 new->name = xstrdup (name);
fcf0e35b
AM
2003
2004 if (output_bfd != NULL)
2005 insert_undefined (new->name);
2006}
2007
2008/* Insert NAME as undefined in the symbol table. */
2009
2010static void
1579bae1 2011insert_undefined (const char *name)
fcf0e35b
AM
2012{
2013 struct bfd_link_hash_entry *h;
2014
b34976b6 2015 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
1579bae1 2016 if (h == NULL)
fcf0e35b
AM
2017 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2018 if (h->type == bfd_link_hash_new)
2019 {
2020 h->type = bfd_link_hash_undefined;
2021 h->u.undef.abfd = NULL;
2022 bfd_link_add_undef (link_info.hash, h);
2023 }
252b5132
RH
2024}
2025
2026/* Run through the list of undefineds created above and place them
2027 into the linker hash table as undefined symbols belonging to the
08da4cac
KH
2028 script file. */
2029
252b5132 2030static void
1579bae1 2031lang_place_undefineds (void)
252b5132
RH
2032{
2033 ldlang_undef_chain_list_type *ptr;
2034
1579bae1
AM
2035 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2036 insert_undefined (ptr->name);
252b5132
RH
2037}
2038
396a2467 2039/* Open input files and attach to output sections. */
08da4cac 2040
252b5132 2041static void
1579bae1
AM
2042map_input_to_output_sections
2043 (lang_statement_union_type *s, const char *target,
2044 lang_output_section_statement_type *output_section_statement)
252b5132 2045{
1579bae1 2046 for (; s != NULL; s = s->header.next)
252b5132
RH
2047 {
2048 switch (s->header.type)
2049 {
252b5132 2050 case lang_wild_statement_enum:
b6bf44ba 2051 wild (&s->wild_statement, target, output_section_statement);
abc6ab0a 2052 break;
252b5132
RH
2053 case lang_constructors_statement_enum:
2054 map_input_to_output_sections (constructor_list.head,
2055 target,
2056 output_section_statement);
2057 break;
2058 case lang_output_section_statement_enum:
2059 map_input_to_output_sections (s->output_section_statement.children.head,
2060 target,
2061 &s->output_section_statement);
2062 break;
2063 case lang_output_statement_enum:
2064 break;
2065 case lang_target_statement_enum:
2066 target = s->target_statement.target;
2067 break;
2068 case lang_group_statement_enum:
2069 map_input_to_output_sections (s->group_statement.children.head,
2070 target,
2071 output_section_statement);
2072 break;
2073 case lang_fill_statement_enum:
2074 case lang_input_section_enum:
2075 case lang_object_symbols_statement_enum:
2076 case lang_data_statement_enum:
2077 case lang_reloc_statement_enum:
2078 case lang_padding_statement_enum:
2079 case lang_input_statement_enum:
2080 if (output_section_statement != NULL
2081 && output_section_statement->bfd_section == NULL)
2082 init_os (output_section_statement);
2083 break;
2084 case lang_assignment_statement_enum:
2085 if (output_section_statement != NULL
2086 && output_section_statement->bfd_section == NULL)
2087 init_os (output_section_statement);
2088
2089 /* Make sure that any sections mentioned in the assignment
08da4cac 2090 are initialized. */
252b5132
RH
2091 exp_init_os (s->assignment_statement.exp);
2092 break;
2093 case lang_afile_asection_pair_statement_enum:
2094 FAIL ();
2095 break;
2096 case lang_address_statement_enum:
08da4cac 2097 /* Mark the specified section with the supplied address. */
252b5132
RH
2098 {
2099 lang_output_section_statement_type *os =
2100 lang_output_section_statement_lookup
2101 (s->address_statement.section_name);
2102
2103 if (os->bfd_section == NULL)
2104 init_os (os);
2105 os->addr_tree = s->address_statement.address;
2106 }
2107 break;
2108 }
2109 }
2110}
2111
4bd5a393
AM
2112/* An output section might have been removed after its statement was
2113 added. For example, ldemul_before_allocation can remove dynamic
2114 sections if they turn out to be not needed. Clean them up here. */
2115
2116static void
1579bae1 2117strip_excluded_output_sections (void)
4bd5a393
AM
2118{
2119 lang_statement_union_type *u;
2120
2121 for (u = lang_output_section_statement.head;
2122 u != NULL;
2123 u = u->output_section_statement.next)
2124 {
2125 lang_output_section_statement_type *os;
2126 asection *s;
2127
2128 os = &u->output_section_statement;
2129 s = os->bfd_section;
2130 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2131 {
2132 asection **p;
2133
2134 os->bfd_section = NULL;
2135
2136 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2137 if (*p == s)
2138 {
2139 bfd_section_list_remove (output_bfd, p);
2140 output_bfd->section_count--;
2141 break;
2142 }
2143 }
2144 }
2145}
2146
252b5132 2147static void
1579bae1
AM
2148print_output_section_statement
2149 (lang_output_section_statement_type *output_section_statement)
252b5132
RH
2150{
2151 asection *section = output_section_statement->bfd_section;
2152 int len;
2153
2154 if (output_section_statement != abs_output_section)
2155 {
2156 minfo ("\n%s", output_section_statement->name);
2157
2158 if (section != NULL)
2159 {
2160 print_dot = section->vma;
2161
2162 len = strlen (output_section_statement->name);
2163 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2164 {
2165 print_nl ();
2166 len = 0;
2167 }
2168 while (len < SECTION_NAME_MAP_LENGTH)
2169 {
2170 print_space ();
2171 ++len;
2172 }
2173
2174 minfo ("0x%V %W", section->vma, section->_raw_size);
2175
2176 if (output_section_statement->load_base != NULL)
2177 {
2178 bfd_vma addr;
2179
2180 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2181 "load base", lang_final_phase_enum);
2182 minfo (_(" load address 0x%V"), addr);
2183 }
2184 }
2185
2186 print_nl ();
2187 }
2188
2189 print_statement_list (output_section_statement->children.head,
2190 output_section_statement);
2191}
2192
2193static void
1579bae1
AM
2194print_assignment (lang_assignment_statement_type *assignment,
2195 lang_output_section_statement_type *output_section)
252b5132
RH
2196{
2197 int i;
2198 etree_value_type result;
2199
2200 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2201 print_space ();
2202
2203 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2204 lang_final_phase_enum, print_dot, &print_dot);
2205 if (result.valid_p)
7b17f854
RS
2206 {
2207 const char *dst;
2208 bfd_vma value;
2209
2210 value = result.value + result.section->bfd_section->vma;
2211 dst = assignment->exp->assign.dst;
2212
2213 minfo ("0x%V", value);
2214 if (dst[0] == '.' && dst[1] == 0)
2215 print_dot = value;
2216 }
252b5132
RH
2217 else
2218 {
2219 minfo ("*undef* ");
2220#ifdef BFD64
2221 minfo (" ");
2222#endif
2223 }
2224
2225 minfo (" ");
2226
2227 exp_print_tree (assignment->exp);
2228
2229 print_nl ();
2230}
2231
2232static void
1579bae1 2233print_input_statement (lang_input_statement_type *statm)
252b5132 2234{
1579bae1 2235 if (statm->filename != NULL)
252b5132
RH
2236 {
2237 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2238 }
2239}
2240
2241/* Print all symbols defined in a particular section. This is called
2242 via bfd_link_hash_traverse. */
2243
b34976b6 2244static bfd_boolean
1579bae1 2245print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
252b5132 2246{
1579bae1 2247 asection *sec = ptr;
252b5132
RH
2248
2249 if ((hash_entry->type == bfd_link_hash_defined
2250 || hash_entry->type == bfd_link_hash_defweak)
2251 && sec == hash_entry->u.def.section)
2252 {
2253 int i;
2254
2255 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2256 print_space ();
2257 minfo ("0x%V ",
2258 (hash_entry->u.def.value
2259 + hash_entry->u.def.section->output_offset
2260 + hash_entry->u.def.section->output_section->vma));
2261
2262 minfo (" %T\n", hash_entry->root.string);
2263 }
2264
b34976b6 2265 return TRUE;
252b5132
RH
2266}
2267
2268/* Print information about an input section to the map file. */
2269
2270static void
1579bae1 2271print_input_section (lang_input_section_type *in)
252b5132
RH
2272{
2273 asection *i = in->section;
2274 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
5f992e62 2275 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2276 ldfile_output_machine);
252b5132
RH
2277 if (size != 0)
2278 {
2279 print_space ();
2280
2281 minfo ("%s", i->name);
2282
2283 if (i->output_section != NULL)
2284 {
2285 int len;
2286
2287 len = 1 + strlen (i->name);
2288 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2289 {
2290 print_nl ();
2291 len = 0;
2292 }
2293 while (len < SECTION_NAME_MAP_LENGTH)
2294 {
2295 print_space ();
2296 ++len;
2297 }
2298
2299 minfo ("0x%V %W %B\n",
4cbfc3ac 2300 i->output_section->vma + i->output_offset, size / opb,
252b5132
RH
2301 i->owner);
2302
2303 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2304 {
2305 len = SECTION_NAME_MAP_LENGTH + 3;
2306#ifdef BFD64
2307 len += 16;
2308#else
2309 len += 8;
2310#endif
2311 while (len > 0)
2312 {
2313 print_space ();
2314 --len;
2315 }
2316
2317 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2318 }
2319
1579bae1 2320 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
252b5132 2321
4cbfc3ac 2322 print_dot = i->output_section->vma + i->output_offset + size / opb;
252b5132
RH
2323 }
2324 }
2325}
2326
2327static void
1579bae1 2328print_fill_statement (lang_fill_statement_type *fill)
252b5132 2329{
2c382fb6
AM
2330 size_t size;
2331 unsigned char *p;
2332 fputs (" FILL mask 0x", config.map_file);
2333 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2334 fprintf (config.map_file, "%02x", *p);
2335 fputs ("\n", config.map_file);
252b5132
RH
2336}
2337
2338static void
1579bae1 2339print_data_statement (lang_data_statement_type *data)
252b5132
RH
2340{
2341 int i;
2342 bfd_vma addr;
2343 bfd_size_type size;
2344 const char *name;
5f992e62 2345 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2346 ldfile_output_machine);
252b5132
RH
2347
2348 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2349 print_space ();
2350
2351 addr = data->output_vma;
2352 if (data->output_section != NULL)
2353 addr += data->output_section->vma;
2354
2355 switch (data->type)
2356 {
2357 default:
2358 abort ();
2359 case BYTE:
2360 size = BYTE_SIZE;
2361 name = "BYTE";
2362 break;
2363 case SHORT:
2364 size = SHORT_SIZE;
2365 name = "SHORT";
2366 break;
2367 case LONG:
2368 size = LONG_SIZE;
2369 name = "LONG";
2370 break;
2371 case QUAD:
2372 size = QUAD_SIZE;
2373 name = "QUAD";
2374 break;
2375 case SQUAD:
2376 size = QUAD_SIZE;
2377 name = "SQUAD";
2378 break;
2379 }
2380
2381 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2382
2383 if (data->exp->type.node_class != etree_value)
2384 {
2385 print_space ();
2386 exp_print_tree (data->exp);
2387 }
2388
2389 print_nl ();
2390
4cbfc3ac
TW
2391 print_dot = addr + size / opb;
2392
252b5132
RH
2393}
2394
2395/* Print an address statement. These are generated by options like
2396 -Ttext. */
2397
2398static void
1579bae1 2399print_address_statement (lang_address_statement_type *address)
252b5132
RH
2400{
2401 minfo (_("Address of section %s set to "), address->section_name);
2402 exp_print_tree (address->address);
2403 print_nl ();
2404}
2405
2406/* Print a reloc statement. */
2407
2408static void
1579bae1 2409print_reloc_statement (lang_reloc_statement_type *reloc)
252b5132
RH
2410{
2411 int i;
2412 bfd_vma addr;
2413 bfd_size_type size;
5f992e62
AM
2414 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2415 ldfile_output_machine);
252b5132
RH
2416
2417 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2418 print_space ();
2419
2420 addr = reloc->output_vma;
2421 if (reloc->output_section != NULL)
2422 addr += reloc->output_section->vma;
2423
2424 size = bfd_get_reloc_size (reloc->howto);
2425
2426 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2427
2428 if (reloc->name != NULL)
2429 minfo ("%s+", reloc->name);
2430 else
2431 minfo ("%s+", reloc->section->name);
2432
2433 exp_print_tree (reloc->addend_exp);
2434
2435 print_nl ();
2436
4cbfc3ac 2437 print_dot = addr + size / opb;
5f992e62 2438}
252b5132
RH
2439
2440static void
1579bae1 2441print_padding_statement (lang_padding_statement_type *s)
252b5132
RH
2442{
2443 int len;
2444 bfd_vma addr;
5f992e62
AM
2445 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2446 ldfile_output_machine);
252b5132
RH
2447
2448 minfo (" *fill*");
2449
2450 len = sizeof " *fill*" - 1;
2451 while (len < SECTION_NAME_MAP_LENGTH)
2452 {
2453 print_space ();
2454 ++len;
2455 }
2456
2457 addr = s->output_offset;
2458 if (s->output_section != NULL)
2459 addr += s->output_section->vma;
2c382fb6 2460 minfo ("0x%V %W ", addr, s->size);
252b5132 2461
2c382fb6
AM
2462 if (s->fill->size != 0)
2463 {
2464 size_t size;
2465 unsigned char *p;
2466 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2467 fprintf (config.map_file, "%02x", *p);
2468 }
252b5132
RH
2469
2470 print_nl ();
2471
4cbfc3ac 2472 print_dot = addr + s->size / opb;
252b5132
RH
2473}
2474
2475static void
1579bae1
AM
2476print_wild_statement (lang_wild_statement_type *w,
2477 lang_output_section_statement_type *os)
252b5132 2478{
b6bf44ba
AM
2479 struct wildcard_list *sec;
2480
252b5132
RH
2481 print_space ();
2482
2483 if (w->filenames_sorted)
2484 minfo ("SORT(");
08da4cac 2485 if (w->filename != NULL)
252b5132
RH
2486 minfo ("%s", w->filename);
2487 else
2488 minfo ("*");
2489 if (w->filenames_sorted)
2490 minfo (")");
2491
2492 minfo ("(");
b6bf44ba
AM
2493 for (sec = w->section_list; sec; sec = sec->next)
2494 {
2495 if (sec->spec.sorted)
2496 minfo ("SORT(");
2497 if (sec->spec.exclude_name_list != NULL)
2498 {
2499 name_list *tmp;
34786259 2500 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
b6bf44ba 2501 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
34786259
AM
2502 minfo (" %s", tmp->name);
2503 minfo (") ");
b6bf44ba
AM
2504 }
2505 if (sec->spec.name != NULL)
2506 minfo ("%s", sec->spec.name);
2507 else
2508 minfo ("*");
2509 if (sec->spec.sorted)
2510 minfo (")");
34786259
AM
2511 if (sec->next)
2512 minfo (" ");
b6bf44ba 2513 }
252b5132
RH
2514 minfo (")");
2515
2516 print_nl ();
2517
2518 print_statement_list (w->children.head, os);
2519}
2520
2521/* Print a group statement. */
2522
2523static void
1579bae1
AM
2524print_group (lang_group_statement_type *s,
2525 lang_output_section_statement_type *os)
252b5132
RH
2526{
2527 fprintf (config.map_file, "START GROUP\n");
2528 print_statement_list (s->children.head, os);
2529 fprintf (config.map_file, "END GROUP\n");
2530}
2531
2532/* Print the list of statements in S.
2533 This can be called for any statement type. */
2534
2535static void
1579bae1
AM
2536print_statement_list (lang_statement_union_type *s,
2537 lang_output_section_statement_type *os)
252b5132
RH
2538{
2539 while (s != NULL)
2540 {
2541 print_statement (s, os);
bba1a0c0 2542 s = s->header.next;
252b5132
RH
2543 }
2544}
2545
2546/* Print the first statement in statement list S.
2547 This can be called for any statement type. */
2548
2549static void
1579bae1
AM
2550print_statement (lang_statement_union_type *s,
2551 lang_output_section_statement_type *os)
252b5132
RH
2552{
2553 switch (s->header.type)
2554 {
2555 default:
2556 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2557 FAIL ();
2558 break;
2559 case lang_constructors_statement_enum:
2560 if (constructor_list.head != NULL)
2561 {
2562 if (constructors_sorted)
2563 minfo (" SORT (CONSTRUCTORS)\n");
2564 else
2565 minfo (" CONSTRUCTORS\n");
2566 print_statement_list (constructor_list.head, os);
2567 }
2568 break;
2569 case lang_wild_statement_enum:
2570 print_wild_statement (&s->wild_statement, os);
2571 break;
2572 case lang_address_statement_enum:
2573 print_address_statement (&s->address_statement);
2574 break;
2575 case lang_object_symbols_statement_enum:
2576 minfo (" CREATE_OBJECT_SYMBOLS\n");
2577 break;
2578 case lang_fill_statement_enum:
2579 print_fill_statement (&s->fill_statement);
2580 break;
2581 case lang_data_statement_enum:
2582 print_data_statement (&s->data_statement);
2583 break;
2584 case lang_reloc_statement_enum:
2585 print_reloc_statement (&s->reloc_statement);
2586 break;
2587 case lang_input_section_enum:
2588 print_input_section (&s->input_section);
2589 break;
2590 case lang_padding_statement_enum:
2591 print_padding_statement (&s->padding_statement);
2592 break;
2593 case lang_output_section_statement_enum:
2594 print_output_section_statement (&s->output_section_statement);
2595 break;
2596 case lang_assignment_statement_enum:
2597 print_assignment (&s->assignment_statement, os);
2598 break;
2599 case lang_target_statement_enum:
2600 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2601 break;
2602 case lang_output_statement_enum:
2603 minfo ("OUTPUT(%s", s->output_statement.name);
2604 if (output_target != NULL)
2605 minfo (" %s", output_target);
2606 minfo (")\n");
2607 break;
2608 case lang_input_statement_enum:
2609 print_input_statement (&s->input_statement);
2610 break;
2611 case lang_group_statement_enum:
2612 print_group (&s->group_statement, os);
2613 break;
2614 case lang_afile_asection_pair_statement_enum:
2615 FAIL ();
2616 break;
2617 }
2618}
2619
2620static void
1579bae1 2621print_statements (void)
252b5132
RH
2622{
2623 print_statement_list (statement_list.head, abs_output_section);
2624}
2625
2626/* Print the first N statements in statement list S to STDERR.
2627 If N == 0, nothing is printed.
2628 If N < 0, the entire list is printed.
2629 Intended to be called from GDB. */
2630
2631void
1579bae1 2632dprint_statement (lang_statement_union_type *s, int n)
252b5132
RH
2633{
2634 FILE *map_save = config.map_file;
2635
2636 config.map_file = stderr;
2637
2638 if (n < 0)
2639 print_statement_list (s, abs_output_section);
2640 else
2641 {
2642 while (s && --n >= 0)
2643 {
2644 print_statement (s, abs_output_section);
bba1a0c0 2645 s = s->header.next;
252b5132
RH
2646 }
2647 }
2648
2649 config.map_file = map_save;
2650}
2651
b3327aad 2652static void
1579bae1
AM
2653insert_pad (lang_statement_union_type **ptr,
2654 fill_type *fill,
2655 unsigned int alignment_needed,
2656 asection *output_section,
2657 bfd_vma dot)
252b5132 2658{
2c382fb6 2659 static fill_type zero_fill = { 1, { 0 } };
b3327aad 2660 lang_statement_union_type *pad;
b3327aad 2661
c0c330a7 2662 pad = ((lang_statement_union_type *)
2af02257 2663 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
b3327aad 2664 if (ptr != &statement_list.head
2af02257 2665 && pad->header.type == lang_padding_statement_enum
b3327aad 2666 && pad->padding_statement.output_section == output_section)
252b5132 2667 {
b3327aad
AM
2668 /* Use the existing pad statement. The above test on output
2669 section is probably redundant, but it doesn't hurt to check. */
252b5132 2670 }
b3327aad 2671 else
252b5132 2672 {
b3327aad 2673 /* Make a new padding statement, linked into existing chain. */
1579bae1 2674 pad = stat_alloc (sizeof (lang_padding_statement_type));
b3327aad
AM
2675 pad->header.next = *ptr;
2676 *ptr = pad;
2677 pad->header.type = lang_padding_statement_enum;
2678 pad->padding_statement.output_section = output_section;
1579bae1 2679 if (fill == NULL)
2c382fb6 2680 fill = &zero_fill;
b3327aad 2681 pad->padding_statement.fill = fill;
252b5132 2682 }
b3327aad
AM
2683 pad->padding_statement.output_offset = dot - output_section->vma;
2684 pad->padding_statement.size = alignment_needed;
2685 output_section->_raw_size += alignment_needed;
252b5132
RH
2686}
2687
08da4cac
KH
2688/* Work out how much this section will move the dot point. */
2689
252b5132 2690static bfd_vma
1579bae1
AM
2691size_input_section (lang_statement_union_type **this_ptr,
2692 lang_output_section_statement_type *output_section_statement,
2693 fill_type *fill,
2694 bfd_vma dot)
252b5132
RH
2695{
2696 lang_input_section_type *is = &((*this_ptr)->input_section);
2697 asection *i = is->section;
2698
7c519c12 2699 if (!is->ifile->just_syms_flag)
252b5132 2700 {
b3327aad
AM
2701 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2702 ldfile_output_machine);
2703 unsigned int alignment_needed;
2704 asection *o;
2705
2706 /* Align this section first to the input sections requirement,
2707 then to the output section's requirement. If this alignment
2708 is greater than any seen before, then record it too. Perform
2709 the alignment by inserting a magic 'padding' statement. */
2710
252b5132 2711 if (output_section_statement->subsection_alignment != -1)
b3327aad
AM
2712 i->alignment_power = output_section_statement->subsection_alignment;
2713
2714 o = output_section_statement->bfd_section;
2715 if (o->alignment_power < i->alignment_power)
2716 o->alignment_power = i->alignment_power;
252b5132 2717
b3327aad
AM
2718 alignment_needed = align_power (dot, i->alignment_power) - dot;
2719
2720 if (alignment_needed != 0)
2721 {
2722 insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2723 dot += alignment_needed;
2724 }
252b5132 2725
08da4cac 2726 /* Remember where in the output section this input section goes. */
252b5132 2727
b3327aad 2728 i->output_offset = dot - o->vma;
252b5132 2729
08da4cac 2730 /* Mark how big the output section must be to contain this now. */
252b5132 2731 if (i->_cooked_size != 0)
4cbfc3ac 2732 dot += i->_cooked_size / opb;
252b5132 2733 else
4cbfc3ac 2734 dot += i->_raw_size / opb;
b3327aad 2735 o->_raw_size = (dot - o->vma) * opb;
252b5132
RH
2736 }
2737 else
2738 {
2739 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2740 }
2741
2742 return dot;
2743}
2744
33275c22 2745#define IGNORE_SECTION(bfd, s) \
d1778b88
AM
2746 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2747 != (SEC_ALLOC | SEC_LOAD)) \
33275c22 2748 || bfd_section_size (bfd, s) == 0)
d1778b88 2749
252b5132
RH
2750/* Check to see if any allocated sections overlap with other allocated
2751 sections. This can happen when the linker script specifically specifies
2752 the output section addresses of the two sections. */
08da4cac 2753
252b5132 2754static void
1579bae1 2755lang_check_section_addresses (void)
252b5132 2756{
08da4cac 2757 asection *s;
f6af82bd 2758 unsigned opb = bfd_octets_per_byte (output_bfd);
252b5132
RH
2759
2760 /* Scan all sections in the output list. */
2761 for (s = output_bfd->sections; s != NULL; s = s->next)
33275c22 2762 {
08da4cac 2763 asection *os;
5f992e62 2764
33275c22
NC
2765 /* Ignore sections which are not loaded or which have no contents. */
2766 if (IGNORE_SECTION (output_bfd, s))
2767 continue;
5f992e62 2768
33275c22
NC
2769 /* Once we reach section 's' stop our seach. This prevents two
2770 warning messages from being produced, one for 'section A overlaps
2771 section B' and one for 'section B overlaps section A'. */
2772 for (os = output_bfd->sections; os != s; os = os->next)
2773 {
2774 bfd_vma s_start;
2775 bfd_vma s_end;
2776 bfd_vma os_start;
2777 bfd_vma os_end;
5f992e62 2778
33275c22
NC
2779 /* Only consider loadable sections with real contents. */
2780 if (IGNORE_SECTION (output_bfd, os))
2781 continue;
252b5132 2782
33275c22
NC
2783 /* We must check the sections' LMA addresses not their
2784 VMA addresses because overlay sections can have
2785 overlapping VMAs but they must have distinct LMAs. */
2786 s_start = bfd_section_lma (output_bfd, s);
2787 os_start = bfd_section_lma (output_bfd, os);
9e4ed18c
TW
2788 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2789 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
5f992e62 2790
33275c22
NC
2791 /* Look for an overlap. */
2792 if ((s_end < os_start) || (s_start > os_end))
2793 continue;
5f992e62 2794
33275c22 2795 einfo (
252b5132 2796_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
33275c22 2797 s->name, s_start, s_end, os->name, os_start, os_end);
5f992e62 2798
33275c22
NC
2799 /* Once we have found one overlap for this section,
2800 stop looking for others. */
2801 break;
2802 }
2803 }
252b5132
RH
2804}
2805
562d3460
TW
2806/* Make sure the new address is within the region. We explicitly permit the
2807 current address to be at the exact end of the region when the address is
2808 non-zero, in case the region is at the end of addressable memory and the
5f992e62 2809 calculation wraps around. */
562d3460
TW
2810
2811static void
1579bae1
AM
2812os_region_check (lang_output_section_statement_type *os,
2813 struct memory_region_struct *region,
2814 etree_type *tree,
2815 bfd_vma base)
562d3460
TW
2816{
2817 if ((region->current < region->origin
2818 || (region->current - region->origin > region->length))
2819 && ((region->current != region->origin + region->length)
b7a26f91 2820 || base == 0))
562d3460 2821 {
1579bae1 2822 if (tree != NULL)
b7a26f91
KH
2823 {
2824 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2825 region->current,
2826 os->bfd_section->owner,
2827 os->bfd_section->name,
2828 region->name);
2829 }
562d3460 2830 else
b7a26f91
KH
2831 {
2832 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2833 region->name,
2834 os->bfd_section->owner,
2835 os->bfd_section->name);
2836 }
562d3460
TW
2837 /* Reset the region pointer. */
2838 region->current = region->origin;
2839 }
2840}
2841
252b5132
RH
2842/* Set the sizes for all the output sections. */
2843
2d20f7bf 2844static bfd_vma
1579bae1
AM
2845lang_size_sections_1
2846 (lang_statement_union_type *s,
2847 lang_output_section_statement_type *output_section_statement,
2848 lang_statement_union_type **prev,
2849 fill_type *fill,
2850 bfd_vma dot,
2851 bfd_boolean *relax,
2852 bfd_boolean check_regions)
252b5132 2853{
5f992e62 2854 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
f6af82bd 2855 ldfile_output_machine);
4cbfc3ac 2856
252b5132 2857 /* Size up the sections from their constituent parts. */
1579bae1 2858 for (; s != NULL; s = s->header.next)
252b5132
RH
2859 {
2860 switch (s->header.type)
2861 {
2862 case lang_output_section_statement_enum:
2863 {
2864 bfd_vma after;
d1778b88 2865 lang_output_section_statement_type *os;
252b5132 2866
d1778b88 2867 os = &s->output_section_statement;
252b5132
RH
2868 if (os->bfd_section == NULL)
2869 /* This section was never actually created. */
2870 break;
2871
2872 /* If this is a COFF shared library section, use the size and
2873 address from the input section. FIXME: This is COFF
2874 specific; it would be cleaner if there were some other way
2875 to do this, but nothing simple comes to mind. */
2876 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2877 {
08da4cac 2878 asection *input;
252b5132
RH
2879
2880 if (os->children.head == NULL
bba1a0c0 2881 || os->children.head->header.next != NULL
252b5132
RH
2882 || os->children.head->header.type != lang_input_section_enum)
2883 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2884 os->name);
2885
2886 input = os->children.head->input_section.section;
2887 bfd_set_section_vma (os->bfd_section->owner,
2888 os->bfd_section,
2889 bfd_section_vma (input->owner, input));
2890 os->bfd_section->_raw_size = input->_raw_size;
2891 break;
2892 }
2893
2894 if (bfd_is_abs_section (os->bfd_section))
2895 {
2896 /* No matter what happens, an abs section starts at zero. */
2897 ASSERT (os->bfd_section->vma == 0);
2898 }
2899 else
2900 {
1579bae1 2901 if (os->addr_tree == NULL)
252b5132
RH
2902 {
2903 /* No address specified for this section, get one
2904 from the region specification. */
1579bae1 2905 if (os->region == NULL
252b5132
RH
2906 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2907 & (SEC_ALLOC | SEC_LOAD)) != 0)
2908 && os->region->name[0] == '*'
a747ee4d 2909 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
252b5132
RH
2910 {
2911 os->region = lang_memory_default (os->bfd_section);
2912 }
2913
2914 /* If a loadable section is using the default memory
2915 region, and some non default memory regions were
66184979 2916 defined, issue an error message. */
a7f18fb3 2917 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
d08a14c3
AM
2918 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2919 & SEC_NEVER_LOAD) == 0
1049f94e 2920 && ! link_info.relocatable
cf888e70 2921 && check_regions
a747ee4d 2922 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
252b5132 2923 && lang_memory_region_list != NULL
d1778b88 2924 && (strcmp (lang_memory_region_list->name,
a747ee4d 2925 DEFAULT_MEMORY_REGION) != 0
252b5132 2926 || lang_memory_region_list->next != NULL))
66184979
NC
2927 {
2928 /* By default this is an error rather than just a
2929 warning because if we allocate the section to the
2930 default memory region we can end up creating an
07f3b6ad
KH
2931 excessively large binary, or even seg faulting when
2932 attempting to perform a negative seek. See
1579bae1 2933 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
66184979
NC
2934 for an example of this. This behaviour can be
2935 overridden by the using the --no-check-sections
2936 switch. */
2937 if (command_line.check_section_addresses)
2938 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2939 bfd_get_section_name (output_bfd,
2940 os->bfd_section));
2941 else
2942 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2943 bfd_get_section_name (output_bfd,
2944 os->bfd_section));
2945 }
252b5132
RH
2946
2947 dot = os->region->current;
5f992e62 2948
252b5132
RH
2949 if (os->section_alignment == -1)
2950 {
2951 bfd_vma olddot;
2952
2953 olddot = dot;
d1778b88
AM
2954 dot = align_power (dot,
2955 os->bfd_section->alignment_power);
252b5132
RH
2956
2957 if (dot != olddot && config.warn_section_align)
2958 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2959 os->name, (unsigned int) (dot - olddot));
2960 }
2961 }
2962 else
2963 {
2964 etree_value_type r;
2965
2966 r = exp_fold_tree (os->addr_tree,
2967 abs_output_section,
2968 lang_allocating_phase_enum,
2969 dot, &dot);
7c519c12
AM
2970 if (!r.valid_p)
2971 einfo (_("%F%S: non constant address expression for section %s\n"),
2972 os->name);
2973
252b5132
RH
2974 dot = r.value + r.section->bfd_section->vma;
2975 }
5f992e62 2976
252b5132
RH
2977 /* The section starts here.
2978 First, align to what the section needs. */
2979
2980 if (os->section_alignment != -1)
2981 dot = align_power (dot, os->section_alignment);
2982
2983 bfd_set_section_vma (0, os->bfd_section, dot);
5f992e62 2984
252b5132
RH
2985 os->bfd_section->output_offset = 0;
2986 }
2987
2d20f7bf 2988 lang_size_sections_1 (os->children.head, os, &os->children.head,
cf888e70 2989 os->fill, dot, relax, check_regions);
5f992e62 2990
08da4cac
KH
2991 /* Put the section within the requested block size, or
2992 align at the block boundary. */
c553bb91 2993 after = align_n (os->bfd_section->vma
32edc927 2994 + os->bfd_section->_raw_size / opb,
252b5132
RH
2995 (bfd_vma) os->block_value);
2996
2997 if (bfd_is_abs_section (os->bfd_section))
2998 ASSERT (after == os->bfd_section->vma);
13ae64f3 2999 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
704afa60 3000 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
1049f94e 3001 && ! link_info.relocatable)
13ae64f3 3002 os->bfd_section->_raw_size = 0;
252b5132 3003 else
5f992e62 3004 os->bfd_section->_raw_size =
08da4cac 3005 (after - os->bfd_section->vma) * opb;
9f88b410 3006
4cbfc3ac 3007 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
b34976b6 3008 os->processed = TRUE;
252b5132 3009
9f88b410
RS
3010 if (os->update_dot_tree != 0)
3011 exp_fold_tree (os->update_dot_tree, abs_output_section,
3012 lang_allocating_phase_enum, dot, &dot);
3013
252b5132
RH
3014 /* Update dot in the region ?
3015 We only do this if the section is going to be allocated,
3016 since unallocated sections do not contribute to the region's
13392b77 3017 overall size in memory.
5f992e62 3018
cce4c4c5
NC
3019 If the SEC_NEVER_LOAD bit is not set, it will affect the
3020 addresses of sections after it. We have to update
3021 dot. */
1579bae1 3022 if (os->region != NULL
cce4c4c5
NC
3023 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3024 & SEC_NEVER_LOAD) == 0
3025 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3026 & (SEC_ALLOC | SEC_LOAD))))
252b5132
RH
3027 {
3028 os->region->current = dot;
5f992e62 3029
cf888e70
NC
3030 if (check_regions)
3031 /* Make sure the new address is within the region. */
3032 os_region_check (os, os->region, os->addr_tree,
3033 os->bfd_section->vma);
08da4cac
KH
3034
3035 /* If there's no load address specified, use the run
3036 region as the load region. */
3037 if (os->lma_region == NULL && os->load_base == NULL)
3038 os->lma_region = os->region;
3039
ee3cc2e2 3040 if (os->lma_region != NULL && os->lma_region != os->region)
08da4cac 3041 {
ee3cc2e2
RS
3042 /* Set load_base, which will be handled later. */
3043 os->load_base = exp_intop (os->lma_region->current);
3044 os->lma_region->current +=
3045 os->bfd_section->_raw_size / opb;
cf888e70
NC
3046 if (check_regions)
3047 os_region_check (os, os->lma_region, NULL,
3048 os->bfd_section->lma);
08da4cac 3049 }
252b5132
RH
3050 }
3051 }
3052 break;
3053
3054 case lang_constructors_statement_enum:
2d20f7bf
JJ
3055 dot = lang_size_sections_1 (constructor_list.head,
3056 output_section_statement,
3057 &s->wild_statement.children.head,
cf888e70 3058 fill, dot, relax, check_regions);
252b5132
RH
3059 break;
3060
3061 case lang_data_statement_enum:
3062 {
3063 unsigned int size = 0;
3064
08da4cac
KH
3065 s->data_statement.output_vma =
3066 dot - output_section_statement->bfd_section->vma;
252b5132
RH
3067 s->data_statement.output_section =
3068 output_section_statement->bfd_section;
3069
3070 switch (s->data_statement.type)
3071 {
08da4cac
KH
3072 default:
3073 abort ();
252b5132
RH
3074 case QUAD:
3075 case SQUAD:
3076 size = QUAD_SIZE;
3077 break;
3078 case LONG:
3079 size = LONG_SIZE;
3080 break;
3081 case SHORT:
3082 size = SHORT_SIZE;
3083 break;
3084 case BYTE:
3085 size = BYTE_SIZE;
3086 break;
3087 }
08da4cac
KH
3088 if (size < opb)
3089 size = opb;
4cbfc3ac 3090 dot += size / opb;
252b5132
RH
3091 output_section_statement->bfd_section->_raw_size += size;
3092 /* The output section gets contents, and then we inspect for
3093 any flags set in the input script which override any ALLOC. */
3094 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
08da4cac
KH
3095 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3096 {
3097 output_section_statement->bfd_section->flags |=
3098 SEC_ALLOC | SEC_LOAD;
3099 }
252b5132
RH
3100 }
3101 break;
3102
3103 case lang_reloc_statement_enum:
3104 {
3105 int size;
3106
3107 s->reloc_statement.output_vma =
3108 dot - output_section_statement->bfd_section->vma;
3109 s->reloc_statement.output_section =
3110 output_section_statement->bfd_section;
3111 size = bfd_get_reloc_size (s->reloc_statement.howto);
4cbfc3ac 3112 dot += size / opb;
252b5132
RH
3113 output_section_statement->bfd_section->_raw_size += size;
3114 }
3115 break;
5f992e62 3116
252b5132
RH
3117 case lang_wild_statement_enum:
3118
2d20f7bf
JJ
3119 dot = lang_size_sections_1 (s->wild_statement.children.head,
3120 output_section_statement,
3121 &s->wild_statement.children.head,
cf888e70 3122 fill, dot, relax, check_regions);
252b5132
RH
3123
3124 break;
3125
3126 case lang_object_symbols_statement_enum:
3127 link_info.create_object_symbols_section =
3128 output_section_statement->bfd_section;
3129 break;
3130 case lang_output_statement_enum:
3131 case lang_target_statement_enum:
3132 break;
3133 case lang_input_section_enum:
3134 {
3135 asection *i;
3136
3137 i = (*prev)->input_section.section;
3138 if (! relax)
3139 {
3140 if (i->_cooked_size == 0)
3141 i->_cooked_size = i->_raw_size;
3142 }
3143 else
3144 {
b34976b6 3145 bfd_boolean again;
252b5132
RH
3146
3147 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3148 einfo (_("%P%F: can't relax section: %E\n"));
3149 if (again)
b34976b6 3150 *relax = TRUE;
252b5132 3151 }
b3327aad
AM
3152 dot = size_input_section (prev, output_section_statement,
3153 output_section_statement->fill, dot);
252b5132
RH
3154 }
3155 break;
3156 case lang_input_statement_enum:
3157 break;
3158 case lang_fill_statement_enum:
08da4cac
KH
3159 s->fill_statement.output_section =
3160 output_section_statement->bfd_section;
252b5132
RH
3161
3162 fill = s->fill_statement.fill;
3163 break;
3164 case lang_assignment_statement_enum:
3165 {
3166 bfd_vma newdot = dot;
3167
3168 exp_fold_tree (s->assignment_statement.exp,
3169 output_section_statement,
3170 lang_allocating_phase_enum,
3171 dot,
3172 &newdot);
3173
3174 if (newdot != dot)
3175 {
252b5132
RH
3176 if (output_section_statement == abs_output_section)
3177 {
3178 /* If we don't have an output section, then just adjust
3179 the default memory address. */
a747ee4d 3180 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
252b5132 3181 }
b3327aad 3182 else
252b5132 3183 {
b3327aad
AM
3184 /* Insert a pad after this statement. We can't
3185 put the pad before when relaxing, in case the
3186 assignment references dot. */
3187 insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3188 output_section_statement->bfd_section, dot);
3189
3190 /* Don't neuter the pad below when relaxing. */
3191 s = s->header.next;
252b5132
RH
3192 }
3193
3194 dot = newdot;
3195 }
3196 }
3197 break;
3198
3199 case lang_padding_statement_enum:
c0c330a7
AM
3200 /* If this is the first time lang_size_sections is called,
3201 we won't have any padding statements. If this is the
3202 second or later passes when relaxing, we should allow
3203 padding to shrink. If padding is needed on this pass, it
3204 will be added back in. */
3205 s->padding_statement.size = 0;
6e814ff8
AM
3206
3207 /* Make sure output_offset is valid. If relaxation shrinks
3208 the section and this pad isn't needed, it's possible to
3209 have output_offset larger than the final size of the
3210 section. bfd_set_section_contents will complain even for
3211 a pad size of zero. */
3212 s->padding_statement.output_offset
3213 = dot - output_section_statement->bfd_section->vma;
252b5132
RH
3214 break;
3215
3216 case lang_group_statement_enum:
2d20f7bf
JJ
3217 dot = lang_size_sections_1 (s->group_statement.children.head,
3218 output_section_statement,
3219 &s->group_statement.children.head,
cf888e70 3220 fill, dot, relax, check_regions);
252b5132
RH
3221 break;
3222
3223 default:
3224 FAIL ();
3225 break;
3226
c0c330a7 3227 /* We can only get here when relaxing is turned on. */
252b5132
RH
3228 case lang_address_statement_enum:
3229 break;
3230 }
3231 prev = &s->header.next;
3232 }
3233 return dot;
3234}
3235
2d20f7bf 3236bfd_vma
1579bae1
AM
3237lang_size_sections
3238 (lang_statement_union_type *s,
3239 lang_output_section_statement_type *output_section_statement,
3240 lang_statement_union_type **prev,
3241 fill_type *fill,
3242 bfd_vma dot,
3243 bfd_boolean *relax,
3244 bfd_boolean check_regions)
2d20f7bf
JJ
3245{
3246 bfd_vma result;
86d3c9a8 3247 asection *o;
2d20f7bf 3248
420e579c
HPN
3249 /* Callers of exp_fold_tree need to increment this. */
3250 lang_statement_iteration++;
3251
2d20f7bf
JJ
3252 exp_data_seg.phase = exp_dataseg_none;
3253 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
cf888e70 3254 dot, relax, check_regions);
2d20f7bf
JJ
3255 if (exp_data_seg.phase == exp_dataseg_end_seen)
3256 {
3257 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3258 a page could be saved in the data segment. */
3259 bfd_vma first, last;
3260
3261 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3262 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3263 if (first && last
3264 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3265 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3266 && first + last <= exp_data_seg.pagesize)
3267 {
3268 exp_data_seg.phase = exp_dataseg_adjust;
3269 result = lang_size_sections_1 (s, output_section_statement, prev,
cf888e70 3270 fill, dot, relax, check_regions);
2d20f7bf
JJ
3271 }
3272 }
3273
86d3c9a8
HPN
3274 /* Some backend relaxers want to refer to the output section size. Give
3275 them a section size that does not change on the next call while they
3276 relax. We can't set this at top because lang_reset_memory_regions
3277 which is called before we get here, sets _raw_size to 0 on relaxing
3278 rounds. */
3279 for (o = output_bfd->sections; o != NULL; o = o->next)
3280 o->_cooked_size = o->_raw_size;
3281
2d20f7bf
JJ
3282 return result;
3283}
3284
420e579c
HPN
3285/* Worker function for lang_do_assignments. Recursiveness goes here. */
3286
3287static bfd_vma
3288lang_do_assignments_1
1579bae1
AM
3289 (lang_statement_union_type *s,
3290 lang_output_section_statement_type *output_section_statement,
3291 fill_type *fill,
3292 bfd_vma dot)
252b5132 3293{
5f992e62
AM
3294 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3295 ldfile_output_machine);
4cbfc3ac 3296
1579bae1 3297 for (; s != NULL; s = s->header.next)
252b5132
RH
3298 {
3299 switch (s->header.type)
3300 {
3301 case lang_constructors_statement_enum:
420e579c
HPN
3302 dot = lang_do_assignments_1 (constructor_list.head,
3303 output_section_statement,
3304 fill,
3305 dot);
252b5132
RH
3306 break;
3307
3308 case lang_output_section_statement_enum:
3309 {
d1778b88 3310 lang_output_section_statement_type *os;
252b5132 3311
d1778b88 3312 os = &(s->output_section_statement);
252b5132
RH
3313 if (os->bfd_section != NULL)
3314 {
3315 dot = os->bfd_section->vma;
420e579c
HPN
3316 (void) lang_do_assignments_1 (os->children.head, os,
3317 os->fill, dot);
4cbfc3ac
TW
3318 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3319
252b5132 3320 }
c13b1b77 3321 if (os->load_base)
252b5132
RH
3322 {
3323 /* If nothing has been placed into the output section then
4de2d33d 3324 it won't have a bfd_section. */
5f992e62 3325 if (os->bfd_section)
252b5132 3326 {
5f992e62 3327 os->bfd_section->lma
08da4cac
KH
3328 = exp_get_abs_int (os->load_base, 0, "load base",
3329 lang_final_phase_enum);
252b5132
RH
3330 }
3331 }
3332 }
3333 break;
3334 case lang_wild_statement_enum:
3335
420e579c
HPN
3336 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3337 output_section_statement,
3338 fill, dot);
252b5132
RH
3339
3340 break;
3341
3342 case lang_object_symbols_statement_enum:
3343 case lang_output_statement_enum:
3344 case lang_target_statement_enum:
3345#if 0
3346 case lang_common_statement_enum:
3347#endif
3348 break;
3349 case lang_data_statement_enum:
3350 {
3351 etree_value_type value;
3352
3353 value = exp_fold_tree (s->data_statement.exp,
3354 abs_output_section,
3355 lang_final_phase_enum, dot, &dot);
3356 s->data_statement.value = value.value;
7c519c12 3357 if (!value.valid_p)
252b5132
RH
3358 einfo (_("%F%P: invalid data statement\n"));
3359 }
b7a26f91
KH
3360 {
3361 unsigned int size;
08da4cac
KH
3362 switch (s->data_statement.type)
3363 {
3364 default:
3365 abort ();
3366 case QUAD:
3367 case SQUAD:
3368 size = QUAD_SIZE;
3369 break;
3370 case LONG:
3371 size = LONG_SIZE;
3372 break;
3373 case SHORT:
3374 size = SHORT_SIZE;
3375 break;
3376 case BYTE:
3377 size = BYTE_SIZE;
3378 break;
3379 }
3380 if (size < opb)
3381 size = opb;
3382 dot += size / opb;
3383 }
252b5132
RH
3384 break;
3385
3386 case lang_reloc_statement_enum:
3387 {
3388 etree_value_type value;
3389
3390 value = exp_fold_tree (s->reloc_statement.addend_exp,
3391 abs_output_section,
3392 lang_final_phase_enum, dot, &dot);
3393 s->reloc_statement.addend_value = value.value;
7c519c12 3394 if (!value.valid_p)
252b5132
RH
3395 einfo (_("%F%P: invalid reloc statement\n"));
3396 }
4cbfc3ac 3397 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
252b5132
RH
3398 break;
3399
3400 case lang_input_section_enum:
3401 {
3402 asection *in = s->input_section.section;
3403
3404 if (in->_cooked_size != 0)
4cbfc3ac 3405 dot += in->_cooked_size / opb;
252b5132 3406 else
4cbfc3ac 3407 dot += in->_raw_size / opb;
252b5132
RH
3408 }
3409 break;
3410
3411 case lang_input_statement_enum:
3412 break;
3413 case lang_fill_statement_enum:
3414 fill = s->fill_statement.fill;
3415 break;
3416 case lang_assignment_statement_enum:
3417 {
3418 exp_fold_tree (s->assignment_statement.exp,
3419 output_section_statement,
3420 lang_final_phase_enum,
3421 dot,
3422 &dot);
3423 }
3424
3425 break;
3426 case lang_padding_statement_enum:
4cbfc3ac 3427 dot += s->padding_statement.size / opb;
252b5132
RH
3428 break;
3429
3430 case lang_group_statement_enum:
420e579c
HPN
3431 dot = lang_do_assignments_1 (s->group_statement.children.head,
3432 output_section_statement,
3433 fill, dot);
252b5132
RH
3434
3435 break;
3436
3437 default:
3438 FAIL ();
3439 break;
3440 case lang_address_statement_enum:
3441 break;
3442 }
3443
3444 }
3445 return dot;
3446}
3447
420e579c
HPN
3448bfd_vma
3449lang_do_assignments (lang_statement_union_type *s,
3450 lang_output_section_statement_type
3451 *output_section_statement,
3452 fill_type *fill,
3453 bfd_vma dot)
3454{
3455 /* Callers of exp_fold_tree need to increment this. */
3456 lang_statement_iteration++;
3457 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3458}
3459
252b5132
RH
3460/* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3461 operator .startof. (section_name), it produces an undefined symbol
3462 .startof.section_name. Similarly, when it sees
3463 .sizeof. (section_name), it produces an undefined symbol
3464 .sizeof.section_name. For all the output sections, we look for
3465 such symbols, and set them to the correct value. */
3466
3467static void
1579bae1 3468lang_set_startof (void)
252b5132
RH
3469{
3470 asection *s;
3471
1049f94e 3472 if (link_info.relocatable)
252b5132
RH
3473 return;
3474
3475 for (s = output_bfd->sections; s != NULL; s = s->next)
3476 {
3477 const char *secname;
3478 char *buf;
3479 struct bfd_link_hash_entry *h;
3480
3481 secname = bfd_get_section_name (output_bfd, s);
3482 buf = xmalloc (10 + strlen (secname));
3483
3484 sprintf (buf, ".startof.%s", secname);
b34976b6 3485 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
3486 if (h != NULL && h->type == bfd_link_hash_undefined)
3487 {
3488 h->type = bfd_link_hash_defined;
3489 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3490 h->u.def.section = bfd_abs_section_ptr;
3491 }
3492
3493 sprintf (buf, ".sizeof.%s", secname);
b34976b6 3494 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
252b5132
RH
3495 if (h != NULL && h->type == bfd_link_hash_undefined)
3496 {
b7a26f91 3497 unsigned opb;
d1778b88 3498
b7a26f91 3499 opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
d1778b88 3500 ldfile_output_machine);
252b5132
RH
3501 h->type = bfd_link_hash_defined;
3502 if (s->_cooked_size != 0)
4cbfc3ac 3503 h->u.def.value = s->_cooked_size / opb;
252b5132 3504 else
4cbfc3ac 3505 h->u.def.value = s->_raw_size / opb;
252b5132
RH
3506 h->u.def.section = bfd_abs_section_ptr;
3507 }
3508
3509 free (buf);
3510 }
3511}
3512
3513static void
1579bae1 3514lang_finish (void)
252b5132
RH
3515{
3516 struct bfd_link_hash_entry *h;
b34976b6 3517 bfd_boolean warn;
252b5132 3518
1049f94e 3519 if (link_info.relocatable || link_info.shared)
b34976b6 3520 warn = FALSE;
252b5132 3521 else
b34976b6 3522 warn = TRUE;
252b5132 3523
1579bae1 3524 if (entry_symbol.name == NULL)
252b5132
RH
3525 {
3526 /* No entry has been specified. Look for start, but don't warn
3527 if we don't find it. */
e3e942e9 3528 entry_symbol.name = "start";
b34976b6 3529 warn = FALSE;
252b5132
RH
3530 }
3531
e3e942e9 3532 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
b34976b6 3533 FALSE, FALSE, TRUE);
1579bae1 3534 if (h != NULL
252b5132
RH
3535 && (h->type == bfd_link_hash_defined
3536 || h->type == bfd_link_hash_defweak)
3537 && h->u.def.section->output_section != NULL)
3538 {
3539 bfd_vma val;
3540
3541 val = (h->u.def.value
3542 + bfd_get_section_vma (output_bfd,
3543 h->u.def.section->output_section)
3544 + h->u.def.section->output_offset);
3545 if (! bfd_set_start_address (output_bfd, val))
e3e942e9 3546 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
252b5132
RH
3547 }
3548 else
3549 {
3550 bfd_vma val;
5f992e62 3551 const char *send;
252b5132
RH
3552
3553 /* We couldn't find the entry symbol. Try parsing it as a
3554 number. */
e3e942e9 3555 val = bfd_scan_vma (entry_symbol.name, &send, 0);
252b5132
RH
3556 if (*send == '\0')
3557 {
3558 if (! bfd_set_start_address (output_bfd, val))
3559 einfo (_("%P%F: can't set start address\n"));
3560 }
3561 else
3562 {
3563 asection *ts;
3564
3565 /* Can't find the entry symbol, and it's not a number. Use
3566 the first address in the text section. */
1e281515 3567 ts = bfd_get_section_by_name (output_bfd, entry_section);
1579bae1 3568 if (ts != NULL)
252b5132
RH
3569 {
3570 if (warn)
3571 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
e3e942e9
AM
3572 entry_symbol.name,
3573 bfd_get_section_vma (output_bfd, ts));
252b5132
RH
3574 if (! bfd_set_start_address (output_bfd,
3575 bfd_get_section_vma (output_bfd,
3576 ts)))
3577 einfo (_("%P%F: can't set start address\n"));
3578 }
3579 else
3580 {
3581 if (warn)
3582 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
e3e942e9 3583 entry_symbol.name);
252b5132
RH
3584 }
3585 }
3586 }
420e579c
HPN
3587
3588 bfd_hash_table_free (&lang_definedness_table);
252b5132
RH
3589}
3590
3591/* This is a small function used when we want to ignore errors from
3592 BFD. */
3593
3594static void
87f2a346 3595ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
252b5132
RH
3596{
3597 /* Don't do anything. */
3598}
3599
3600/* Check that the architecture of all the input files is compatible
3601 with the output file. Also call the backend to let it do any
3602 other checking that is needed. */
3603
3604static void
1579bae1 3605lang_check (void)
252b5132
RH
3606{
3607 lang_statement_union_type *file;
3608 bfd *input_bfd;
5f992e62 3609 const bfd_arch_info_type *compatible;
252b5132 3610
1579bae1 3611 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
252b5132
RH
3612 {
3613 input_bfd = file->input_statement.the_bfd;
312b768e
NC
3614 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3615 command_line.accept_unknown_input_arch);
30cba025
AM
3616
3617 /* In general it is not possible to perform a relocatable
3618 link between differing object formats when the input
3619 file has relocations, because the relocations in the
3620 input format may not have equivalent representations in
3621 the output format (and besides BFD does not translate
3622 relocs for other link purposes than a final link). */
1049f94e 3623 if ((link_info.relocatable || link_info.emitrelocations)
30cba025 3624 && (compatible == NULL
d35a52e2 3625 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
30cba025
AM
3626 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3627 {
3628 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3629 bfd_get_target (input_bfd), input_bfd,
3630 bfd_get_target (output_bfd), output_bfd);
3631 /* einfo with %F exits. */
3632 }
3633
252b5132
RH
3634 if (compatible == NULL)
3635 {
3636 if (command_line.warn_mismatch)
3637 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3638 bfd_printable_name (input_bfd), input_bfd,
3639 bfd_printable_name (output_bfd));
3640 }
b9247304 3641 else if (bfd_count_sections (input_bfd))
252b5132 3642 {
b9247304 3643 /* If the input bfd has no contents, it shouldn't set the
b7a26f91 3644 private data of the output bfd. */
b9247304 3645
252b5132
RH
3646 bfd_error_handler_type pfn = NULL;
3647
3648 /* If we aren't supposed to warn about mismatched input
3649 files, temporarily set the BFD error handler to a
3650 function which will do nothing. We still want to call
3651 bfd_merge_private_bfd_data, since it may set up
3652 information which is needed in the output file. */
3653 if (! command_line.warn_mismatch)
3654 pfn = bfd_set_error_handler (ignore_bfd_errors);
3655 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3656 {
3657 if (command_line.warn_mismatch)
3658 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3659 input_bfd);
3660 }
3661 if (! command_line.warn_mismatch)
3662 bfd_set_error_handler (pfn);
3663 }
3664 }
3665}
3666
3667/* Look through all the global common symbols and attach them to the
3668 correct section. The -sort-common command line switch may be used
3669 to roughly sort the entries by size. */
3670
3671static void
1579bae1 3672lang_common (void)
252b5132 3673{
4818e05f
AM
3674 if (command_line.inhibit_common_definition)
3675 return;
1049f94e 3676 if (link_info.relocatable
252b5132
RH
3677 && ! command_line.force_common_definition)
3678 return;
3679
3680 if (! config.sort_common)
1579bae1 3681 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
252b5132
RH
3682 else
3683 {
3684 int power;
3685
3686 for (power = 4; power >= 0; power--)
1579bae1 3687 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
252b5132
RH
3688 }
3689}
3690
3691/* Place one common symbol in the correct section. */
3692
b34976b6 3693static bfd_boolean
1579bae1 3694lang_one_common (struct bfd_link_hash_entry *h, void *info)
252b5132
RH
3695{
3696 unsigned int power_of_two;
3697 bfd_vma size;
3698 asection *section;
5f992e62
AM
3699 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3700 ldfile_output_machine);
252b5132
RH
3701
3702 if (h->type != bfd_link_hash_common)
b34976b6 3703 return TRUE;
252b5132
RH
3704
3705 size = h->u.c.size;
3706 power_of_two = h->u.c.p->alignment_power;
3707
3708 if (config.sort_common
3709 && power_of_two < (unsigned int) *(int *) info)
b34976b6 3710 return TRUE;
252b5132
RH
3711
3712 section = h->u.c.p->section;
3713
3714 /* Increase the size of the section. */
c553bb91
AM
3715 section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3716 (bfd_vma) 1 << power_of_two) * opb;
252b5132
RH
3717
3718 /* Adjust the alignment if necessary. */
3719 if (power_of_two > section->alignment_power)
3720 section->alignment_power = power_of_two;
3721
3722 /* Change the symbol from common to defined. */
3723 h->type = bfd_link_hash_defined;
3724 h->u.def.section = section;
3725 h->u.def.value = section->_cooked_size;
3726
3727 /* Increase the size of the section. */
3728 section->_cooked_size += size;
3729
3730 /* Make sure the section is allocated in memory, and make sure that
3731 it is no longer a common section. */
3732 section->flags |= SEC_ALLOC;
08da4cac 3733 section->flags &= ~SEC_IS_COMMON;
252b5132
RH
3734
3735 if (config.map_file != NULL)
3736 {
b34976b6 3737 static bfd_boolean header_printed;
252b5132
RH
3738 int len;
3739 char *name;
3740 char buf[50];
3741
3742 if (! header_printed)
3743 {
3744 minfo (_("\nAllocating common symbols\n"));
3745 minfo (_("Common symbol size file\n\n"));
b34976b6 3746 header_printed = TRUE;
252b5132
RH
3747 }
3748
3749 name = demangle (h->root.string);
3750 minfo ("%s", name);
3751 len = strlen (name);
3752 free (name);
3753
3754 if (len >= 19)
3755 {
3756 print_nl ();
3757 len = 0;
3758 }
3759 while (len < 20)
3760 {
3761 print_space ();
3762 ++len;
3763 }
3764
3765 minfo ("0x");
3766 if (size <= 0xffffffff)
3767 sprintf (buf, "%lx", (unsigned long) size);
3768 else
3769 sprintf_vma (buf, size);
3770 minfo ("%s", buf);
3771 len = strlen (buf);
3772
3773 while (len < 16)
3774 {
3775 print_space ();
3776 ++len;
3777 }
3778
3779 minfo ("%B\n", section->owner);
3780 }
3781
b34976b6 3782 return TRUE;
252b5132
RH
3783}
3784
08da4cac
KH
3785/* Run through the input files and ensure that every input section has
3786 somewhere to go. If one is found without a destination then create
3787 an input request and place it into the statement tree. */
252b5132
RH
3788
3789static void
1579bae1 3790lang_place_orphans (void)
252b5132 3791{
e50d8076 3792 LANG_FOR_EACH_INPUT_STATEMENT (file)
252b5132
RH
3793 {
3794 asection *s;
3795
1579bae1 3796 for (s = file->the_bfd->sections; s != NULL; s = s->next)
252b5132 3797 {
1579bae1 3798 if (s->output_section == NULL)
252b5132 3799 {
396a2467 3800 /* This section of the file is not attached, root
08da4cac 3801 around for a sensible place for it to go. */
252b5132
RH
3802
3803 if (file->just_syms_flag)
3804 {
c2c01aa7 3805 abort ();
252b5132
RH
3806 }
3807 else if (strcmp (s->name, "COMMON") == 0)
3808 {
3809 /* This is a lonely common section which must have
3810 come from an archive. We attach to the section
3811 with the wildcard. */
1049f94e 3812 if (! link_info.relocatable
252b5132
RH
3813 || command_line.force_common_definition)
3814 {
3815 if (default_common_section == NULL)
3816 {
3817#if 0
3818 /* This message happens when using the
3819 svr3.ifile linker script, so I have
3820 disabled it. */
3821 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3822#endif
3823 default_common_section =
3824 lang_output_section_statement_lookup (".bss");
3825
3826 }
39dcfe18
AM
3827 lang_add_section (&default_common_section->children, s,
3828 default_common_section, file);
252b5132
RH
3829 }
3830 }
3831 else if (ldemul_place_orphan (file, s))
3832 ;
3833 else
3834 {
39dcfe18 3835 lang_output_section_statement_type *os;
252b5132 3836
39dcfe18
AM
3837 os = lang_output_section_statement_lookup (s->name);
3838 lang_add_section (&os->children, s, os, file);
252b5132
RH
3839 }
3840 }
3841 }
3842 }
3843}
3844
252b5132 3845void
1579bae1 3846lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
252b5132 3847{
aa8804e4 3848 flagword *ptr_flags;
252b5132 3849
aa8804e4 3850 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
252b5132
RH
3851 while (*flags)
3852 {
3853 switch (*flags)
3854 {
252b5132
RH
3855 case 'A': case 'a':
3856 *ptr_flags |= SEC_ALLOC;
3857 break;
3858
3859 case 'R': case 'r':
3860 *ptr_flags |= SEC_READONLY;
3861 break;
3862
3863 case 'W': case 'w':
3864 *ptr_flags |= SEC_DATA;
3865 break;
3866
3867 case 'X': case 'x':
3868 *ptr_flags |= SEC_CODE;
3869 break;
3870
3871 case 'L': case 'l':
3872 case 'I': case 'i':
3873 *ptr_flags |= SEC_LOAD;
3874 break;
3875
3876 default:
3877 einfo (_("%P%F: invalid syntax in flags\n"));
3878 break;
3879 }
3880 flags++;
3881 }
3882}
3883
3884/* Call a function on each input file. This function will be called
3885 on an archive, but not on the elements. */
3886
3887void
1579bae1 3888lang_for_each_input_file (void (*func) (lang_input_statement_type *))
252b5132
RH
3889{
3890 lang_input_statement_type *f;
3891
3892 for (f = (lang_input_statement_type *) input_file_chain.head;
3893 f != NULL;
3894 f = (lang_input_statement_type *) f->next_real_file)
3895 func (f);
3896}
3897
3898/* Call a function on each file. The function will be called on all
3899 the elements of an archive which are included in the link, but will
3900 not be called on the archive file itself. */
3901
3902void
1579bae1 3903lang_for_each_file (void (*func) (lang_input_statement_type *))
252b5132 3904{
e50d8076 3905 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132
RH
3906 {
3907 func (f);
3908 }
3909}
3910
3911#if 0
3912
3913/* Not used. */
3914
3915void
1579bae1 3916lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
252b5132 3917{
e50d8076 3918 LANG_FOR_EACH_INPUT_STATEMENT (f)
252b5132 3919 {
08da4cac 3920 asection *s;
252b5132 3921
1579bae1
AM
3922 for (s = f->the_bfd->sections; s != NULL; s = s->next)
3923 func (f->the_bfd, s);
252b5132
RH
3924 }
3925}
3926
3927#endif
3928
3929void
1579bae1 3930ldlang_add_file (lang_input_statement_type *entry)
252b5132
RH
3931{
3932 bfd **pp;
3933
3934 lang_statement_append (&file_chain,
3935 (lang_statement_union_type *) entry,
3936 &entry->next);
3937
3938 /* The BFD linker needs to have a list of all input BFDs involved in
3939 a link. */
1579bae1 3940 ASSERT (entry->the_bfd->link_next == NULL);
252b5132 3941 ASSERT (entry->the_bfd != output_bfd);
1579bae1 3942 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
252b5132
RH
3943 ;
3944 *pp = entry->the_bfd;
1579bae1 3945 entry->the_bfd->usrdata = entry;
252b5132
RH
3946 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3947
3948 /* Look through the sections and check for any which should not be
3949 included in the link. We need to do this now, so that we can
3950 notice when the backend linker tries to report multiple
3951 definition errors for symbols which are in sections we aren't
3952 going to link. FIXME: It might be better to entirely ignore
3953 symbols which are defined in sections which are going to be
3954 discarded. This would require modifying the backend linker for
3955 each backend which might set the SEC_LINK_ONCE flag. If we do
3956 this, we should probably handle SEC_EXCLUDE in the same way. */
3957
1579bae1 3958 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
252b5132
RH
3959}
3960
3961void
1579bae1 3962lang_add_output (const char *name, int from_script)
252b5132
RH
3963{
3964 /* Make -o on command line override OUTPUT in script. */
7c519c12 3965 if (!had_output_filename || !from_script)
252b5132
RH
3966 {
3967 output_filename = name;
b34976b6 3968 had_output_filename = TRUE;
252b5132
RH
3969 }
3970}
3971
252b5132
RH
3972static lang_output_section_statement_type *current_section;
3973
3974static int
1579bae1 3975topower (int x)
252b5132
RH
3976{
3977 unsigned int i = 1;
3978 int l;
3979
3980 if (x < 0)
3981 return -1;
3982
5f992e62 3983 for (l = 0; l < 32; l++)
252b5132
RH
3984 {
3985 if (i >= (unsigned int) x)
3986 return l;
3987 i <<= 1;
3988 }
3989
3990 return 0;
3991}
3992
aea4bd9d 3993lang_output_section_statement_type *
1579bae1
AM
3994lang_enter_output_section_statement (const char *output_section_statement_name,
3995 etree_type *address_exp,
3996 enum section_type sectype,
3997 bfd_vma block_value,
3998 etree_type *align,
3999 etree_type *subalign,
4000 etree_type *ebase)
252b5132
RH
4001{
4002 lang_output_section_statement_type *os;
4003
4004 current_section =
4005 os =
4006 lang_output_section_statement_lookup (output_section_statement_name);
4007
08da4cac
KH
4008 /* Add this statement to tree. */
4009#if 0
4010 add_statement (lang_output_section_statement_enum,
4011 output_section_statement);
4012#endif
4013 /* Make next things chain into subchain of this. */
252b5132 4014
1579bae1 4015 if (os->addr_tree == NULL)
08da4cac
KH
4016 {
4017 os->addr_tree = address_exp;
4018 }
252b5132
RH
4019 os->sectype = sectype;
4020 if (sectype != noload_section)
4021 os->flags = SEC_NO_FLAGS;
4022 else
4023 os->flags = SEC_NEVER_LOAD;
4024 os->block_value = block_value ? block_value : 1;
4025 stat_ptr = &os->children;
4026
08da4cac
KH
4027 os->subsection_alignment =
4028 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4029 os->section_alignment =
4030 topower (exp_get_value_int (align, -1, "section alignment", 0));
252b5132
RH
4031
4032 os->load_base = ebase;
aea4bd9d 4033 return os;
252b5132
RH
4034}
4035
252b5132 4036void
1579bae1 4037lang_final (void)
252b5132
RH
4038{
4039 lang_output_statement_type *new =
4040 new_stat (lang_output_statement, stat_ptr);
4041
4042 new->name = output_filename;
4043}
4044
08da4cac
KH
4045/* Reset the current counters in the regions. */
4046
e3dc8847 4047void
1579bae1 4048lang_reset_memory_regions (void)
252b5132
RH
4049{
4050 lang_memory_region_type *p = lang_memory_region_list;
b3327aad 4051 asection *o;
252b5132 4052
1579bae1 4053 for (p = lang_memory_region_list; p != NULL; p = p->next)
252b5132
RH
4054 {
4055 p->old_length = (bfd_size_type) (p->current - p->origin);
4056 p->current = p->origin;
4057 }
b3327aad
AM
4058
4059 for (o = output_bfd->sections; o != NULL; o = o->next)
4060 o->_raw_size = 0;
252b5132
RH
4061}
4062
b6bf44ba
AM
4063/* If the wild pattern was marked KEEP, the member sections
4064 should be as well. */
252b5132
RH
4065
4066static void
1579bae1
AM
4067gc_section_callback (lang_wild_statement_type *ptr,
4068 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4069 asection *section,
4070 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4071 void *data ATTRIBUTE_UNUSED)
252b5132 4072{
4dec4d4e
RH
4073 if (ptr->keep_sections)
4074 section->flags |= SEC_KEEP;
252b5132
RH
4075}
4076
b6bf44ba 4077/* Handle a wild statement, marking it against GC. */
252b5132
RH
4078
4079static void
1579bae1 4080lang_gc_wild (lang_wild_statement_type *s)
252b5132 4081{
b6bf44ba 4082 walk_wild (s, gc_section_callback, NULL);
252b5132
RH
4083}
4084
4085/* Iterate over sections marking them against GC. */
4086
4087static void
1579bae1 4088lang_gc_sections_1 (lang_statement_union_type *s)
252b5132 4089{
1579bae1 4090 for (; s != NULL; s = s->header.next)
252b5132
RH
4091 {
4092 switch (s->header.type)
4093 {
4094 case lang_wild_statement_enum:
b6bf44ba 4095 lang_gc_wild (&s->wild_statement);
abc6ab0a 4096 break;
252b5132
RH
4097 case lang_constructors_statement_enum:
4098 lang_gc_sections_1 (constructor_list.head);
4099 break;
4100 case lang_output_section_statement_enum:
4101 lang_gc_sections_1 (s->output_section_statement.children.head);
4102 break;
4103 case lang_group_statement_enum:
4104 lang_gc_sections_1 (s->group_statement.children.head);
4105 break;
4106 default:
4107 break;
4108 }
4109 }
4110}
4111
4112static void
1579bae1 4113lang_gc_sections (void)
252b5132
RH
4114{
4115 struct bfd_link_hash_entry *h;
e3e942e9 4116 ldlang_undef_chain_list_type *ulist;
252b5132
RH
4117
4118 /* Keep all sections so marked in the link script. */
4119
4120 lang_gc_sections_1 (statement_list.head);
4121
e3e942e9
AM
4122 /* Keep all sections containing symbols undefined on the command-line,
4123 and the section containing the entry symbol. */
252b5132 4124
e3e942e9 4125 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
252b5132 4126 {
5f992e62 4127 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
b34976b6 4128 FALSE, FALSE, FALSE);
252b5132 4129
1579bae1 4130 if (h != NULL
08da4cac
KH
4131 && (h->type == bfd_link_hash_defined
4132 || h->type == bfd_link_hash_defweak)
252b5132
RH
4133 && ! bfd_is_abs_section (h->u.def.section))
4134 {
4135 h->u.def.section->flags |= SEC_KEEP;
4136 }
4137 }
4138
4139 bfd_gc_sections (output_bfd, &link_info);
4140}
4141
4142void
1579bae1 4143lang_process (void)
252b5132
RH
4144{
4145 lang_reasonable_defaults ();
4146 current_target = default_target;
4147
08da4cac
KH
4148 /* Open the output file. */
4149 lang_for_each_statement (ldlang_open_output);
252b5132
RH
4150
4151 ldemul_create_output_section_statements ();
4152
08da4cac 4153 /* Add to the hash table all undefineds on the command line. */
252b5132
RH
4154 lang_place_undefineds ();
4155
9503fd87
ILT
4156 already_linked_table_init ();
4157
08da4cac 4158 /* Create a bfd for each input file. */
252b5132 4159 current_target = default_target;
b34976b6 4160 open_input_bfds (statement_list.head, FALSE);
252b5132 4161
e3e942e9
AM
4162 link_info.gc_sym_list = &entry_symbol;
4163 if (entry_symbol.name == NULL)
4164 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4165
252b5132
RH
4166 ldemul_after_open ();
4167
9503fd87
ILT
4168 already_linked_table_free ();
4169
252b5132
RH
4170 /* Make sure that we're not mixing architectures. We call this
4171 after all the input files have been opened, but before we do any
4172 other processing, so that any operations merge_private_bfd_data
4173 does on the output file will be known during the rest of the
4174 link. */
4175 lang_check ();
4176
4177 /* Handle .exports instead of a version script if we're told to do so. */
4178 if (command_line.version_exports_section)
4179 lang_do_version_exports_section ();
4180
4181 /* Build all sets based on the information gathered from the input
4182 files. */
4183 ldctor_build_sets ();
4184
4185 /* Remove unreferenced sections if asked to. */
4186 if (command_line.gc_sections)
4187 lang_gc_sections ();
4188
8550eb6e
JJ
4189 /* If there were any SEC_MERGE sections, finish their merging, so that
4190 section sizes can be computed. This has to be done after GC of sections,
4191 so that GCed sections are not merged, but before assigning output
4192 sections, since removing whole input sections is hard then. */
4193 bfd_merge_sections (output_bfd, &link_info);
4194
08da4cac 4195 /* Size up the common data. */
252b5132
RH
4196 lang_common ();
4197
4198 /* Run through the contours of the script and attach input sections
08da4cac 4199 to the correct output sections. */
1579bae1 4200 map_input_to_output_sections (statement_list.head, NULL, NULL);
252b5132 4201
08da4cac 4202 /* Find any sections not attached explicitly and handle them. */
252b5132
RH
4203 lang_place_orphans ();
4204
1049f94e 4205 if (! link_info.relocatable)
862120bd
AM
4206 {
4207 /* Look for a text section and set the readonly attribute in it. */
4208 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4209
1579bae1 4210 if (found != NULL)
862120bd
AM
4211 {
4212 if (config.text_read_only)
4213 found->flags |= SEC_READONLY;
4214 else
4215 found->flags &= ~SEC_READONLY;
4216 }
4217 }
4218
4219 /* Do anything special before sizing sections. This is where ELF
4220 and other back-ends size dynamic sections. */
252b5132
RH
4221 ldemul_before_allocation ();
4222
1049f94e 4223 if (!link_info.relocatable)
4bd5a393
AM
4224 strip_excluded_output_sections ();
4225
252b5132
RH
4226 /* We must record the program headers before we try to fix the
4227 section positions, since they will affect SIZEOF_HEADERS. */
4228 lang_record_phdrs ();
4229
b3327aad 4230 /* Size up the sections. */
1579bae1
AM
4231 lang_size_sections (statement_list.head, abs_output_section,
4232 &statement_list.head, 0, 0, NULL,
cf888e70 4233 command_line.relax ? FALSE : TRUE);
b3327aad 4234
08da4cac 4235 /* Now run around and relax if we can. */
252b5132
RH
4236 if (command_line.relax)
4237 {
252b5132 4238 /* Keep relaxing until bfd_relax_section gives up. */
b34976b6 4239 bfd_boolean relax_again;
b3327aad 4240
252b5132
RH
4241 do
4242 {
b34976b6 4243 relax_again = FALSE;
252b5132
RH
4244
4245 /* Note: pe-dll.c does something like this also. If you find
4246 you need to change this code, you probably need to change
08da4cac 4247 pe-dll.c also. DJ */
252b5132
RH
4248
4249 /* Do all the assignments with our current guesses as to
4250 section sizes. */
1579bae1
AM
4251 lang_do_assignments (statement_list.head, abs_output_section,
4252 NULL, 0);
252b5132 4253
5a46fe39
JW
4254 /* We must do this after lang_do_assignments, because it uses
4255 _raw_size. */
4256 lang_reset_memory_regions ();
4257
252b5132 4258 /* Perform another relax pass - this time we know where the
0d6d936f 4259 globals are, so can make a better guess. */
1579bae1
AM
4260 lang_size_sections (statement_list.head, abs_output_section,
4261 &statement_list.head, 0, 0, &relax_again, FALSE);
c7996ad6
L
4262
4263 /* If the normal relax is done and the relax finalize pass
4264 is not performed yet, we perform another relax pass. */
4265 if (!relax_again && !link_info.relax_finalizing)
4266 {
4267 link_info.relax_finalizing = TRUE;
4268 relax_again = TRUE;
4269 }
252b5132
RH
4270 }
4271 while (relax_again);
cf888e70
NC
4272
4273 /* Final extra sizing to report errors. */
1579bae1 4274 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
5a46fe39 4275 lang_reset_memory_regions ();
1579bae1
AM
4276 lang_size_sections (statement_list.head, abs_output_section,
4277 &statement_list.head, 0, 0, NULL, TRUE);
252b5132 4278 }
252b5132
RH
4279
4280 /* See if anything special should be done now we know how big
4281 everything is. */
4282 ldemul_after_allocation ();
4283
4284 /* Fix any .startof. or .sizeof. symbols. */
4285 lang_set_startof ();
4286
08da4cac
KH
4287 /* Do all the assignments, now that we know the final resting places
4288 of all the symbols. */
252b5132 4289
1579bae1 4290 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
252b5132
RH
4291
4292 /* Make sure that the section addresses make sense. */
1049f94e 4293 if (! link_info.relocatable
252b5132
RH
4294 && command_line.check_section_addresses)
4295 lang_check_section_addresses ();
5f992e62 4296
08da4cac 4297 /* Final stuffs. */
252b5132
RH
4298
4299 ldemul_finish ();
4300 lang_finish ();
4301}
4302
4303/* EXPORTED TO YACC */
4304
4305void
1579bae1
AM
4306lang_add_wild (struct wildcard_spec *filespec,
4307 struct wildcard_list *section_list,
4308 bfd_boolean keep_sections)
252b5132 4309{
b6bf44ba
AM
4310 struct wildcard_list *curr, *next;
4311 lang_wild_statement_type *new;
4312
4313 /* Reverse the list as the parser puts it back to front. */
4314 for (curr = section_list, section_list = NULL;
4315 curr != NULL;
4316 section_list = curr, curr = next)
4317 {
4318 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
b34976b6 4319 placed_commons = TRUE;
252b5132 4320
b6bf44ba
AM
4321 next = curr->next;
4322 curr->next = section_list;
4323 }
4324
4325 if (filespec != NULL && filespec->name != NULL)
252b5132 4326 {
b6bf44ba
AM
4327 if (strcmp (filespec->name, "*") == 0)
4328 filespec->name = NULL;
4329 else if (! wildcardp (filespec->name))
b34976b6 4330 lang_has_input_file = TRUE;
252b5132 4331 }
b6bf44ba
AM
4332
4333 new = new_stat (lang_wild_statement, stat_ptr);
4334 new->filename = NULL;
b34976b6 4335 new->filenames_sorted = FALSE;
b6bf44ba 4336 if (filespec != NULL)
252b5132 4337 {
b6bf44ba
AM
4338 new->filename = filespec->name;
4339 new->filenames_sorted = filespec->sorted;
252b5132 4340 }
b6bf44ba 4341 new->section_list = section_list;
252b5132 4342 new->keep_sections = keep_sections;
252b5132
RH
4343 lang_list_init (&new->children);
4344}
4345
4346void
1579bae1 4347lang_section_start (const char *name, etree_type *address)
252b5132 4348{
d1778b88 4349 lang_address_statement_type *ad;
252b5132 4350
d1778b88 4351 ad = new_stat (lang_address_statement, stat_ptr);
252b5132
RH
4352 ad->section_name = name;
4353 ad->address = address;
4354}
4355
4356/* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4357 because of a -e argument on the command line, or zero if this is
4358 called by ENTRY in a linker script. Command line arguments take
4359 precedence. */
4360
4361void
1579bae1 4362lang_add_entry (const char *name, bfd_boolean cmdline)
252b5132 4363{
e3e942e9 4364 if (entry_symbol.name == NULL
252b5132
RH
4365 || cmdline
4366 || ! entry_from_cmdline)
4367 {
e3e942e9 4368 entry_symbol.name = name;
252b5132
RH
4369 entry_from_cmdline = cmdline;
4370 }
4371}
4372
4373void
1579bae1 4374lang_add_target (const char *name)
252b5132
RH
4375{
4376 lang_target_statement_type *new = new_stat (lang_target_statement,
4377 stat_ptr);
4378
4379 new->target = name;
4380
4381}
4382
4383void
1579bae1 4384lang_add_map (const char *name)
252b5132
RH
4385{
4386 while (*name)
4387 {
4388 switch (*name)
4389 {
08da4cac 4390 case 'F':
b34976b6 4391 map_option_f = TRUE;
252b5132
RH
4392 break;
4393 }
4394 name++;
4395 }
4396}
4397
4398void
1579bae1 4399lang_add_fill (fill_type *fill)
252b5132
RH
4400{
4401 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4402 stat_ptr);
4403
2c382fb6 4404 new->fill = fill;
252b5132
RH
4405}
4406
4407void
1579bae1 4408lang_add_data (int type, union etree_union *exp)
252b5132
RH
4409{
4410
4411 lang_data_statement_type *new = new_stat (lang_data_statement,
4412 stat_ptr);
4413
4414 new->exp = exp;
4415 new->type = type;
4416
4417}
4418
4419/* Create a new reloc statement. RELOC is the BFD relocation type to
4420 generate. HOWTO is the corresponding howto structure (we could
4421 look this up, but the caller has already done so). SECTION is the
4422 section to generate a reloc against, or NAME is the name of the
4423 symbol to generate a reloc against. Exactly one of SECTION and
4424 NAME must be NULL. ADDEND is an expression for the addend. */
4425
4426void
1579bae1
AM
4427lang_add_reloc (bfd_reloc_code_real_type reloc,
4428 reloc_howto_type *howto,
4429 asection *section,
4430 const char *name,
4431 union etree_union *addend)
252b5132
RH
4432{
4433 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5f992e62 4434
252b5132
RH
4435 p->reloc = reloc;
4436 p->howto = howto;
4437 p->section = section;
4438 p->name = name;
4439 p->addend_exp = addend;
4440
4441 p->addend_value = 0;
4442 p->output_section = NULL;
4443 p->output_vma = 0;
4444}
4445
4446lang_assignment_statement_type *
1579bae1 4447lang_add_assignment (etree_type *exp)
252b5132
RH
4448{
4449 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4450 stat_ptr);
4451
4452 new->exp = exp;
4453 return new;
4454}
4455
4456void
1579bae1 4457lang_add_attribute (enum statement_enum attribute)
252b5132
RH
4458{
4459 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4460}
4461
4462void
1579bae1 4463lang_startup (const char *name)
252b5132 4464{
1579bae1 4465 if (startup_file != NULL)
252b5132
RH
4466 {
4467 einfo (_("%P%Fmultiple STARTUP files\n"));
4468 }
4469 first_file->filename = name;
4470 first_file->local_sym_name = name;
b34976b6 4471 first_file->real = TRUE;
252b5132
RH
4472
4473 startup_file = name;
4474}
4475
4476void
1579bae1 4477lang_float (bfd_boolean maybe)
252b5132
RH
4478{
4479 lang_float_flag = maybe;
4480}
4481
ee3cc2e2
RS
4482
4483/* Work out the load- and run-time regions from a script statement, and
4484 store them in *LMA_REGION and *REGION respectively.
4485
a747ee4d
NC
4486 MEMSPEC is the name of the run-time region, or the value of
4487 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4488 LMA_MEMSPEC is the name of the load-time region, or null if the
4489 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4490 had an explicit load address.
ee3cc2e2
RS
4491
4492 It is an error to specify both a load region and a load address. */
4493
4494static void
1579bae1
AM
4495lang_get_regions (struct memory_region_struct **region,
4496 struct memory_region_struct **lma_region,
4497 const char *memspec,
4498 const char *lma_memspec,
4499 int have_lma_p)
ee3cc2e2 4500{
a747ee4d 4501 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
ee3cc2e2
RS
4502
4503 /* If no runtime region has been given, but the load region has
4504 been, use the load region. */
a747ee4d 4505 if (lma_memspec != 0 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
ee3cc2e2
RS
4506 *region = *lma_region;
4507 else
a747ee4d 4508 *region = lang_memory_region_lookup (memspec, FALSE);
ee3cc2e2
RS
4509
4510 if (have_lma_p && lma_memspec != 0)
4511 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4512}
4513
252b5132 4514void
1579bae1
AM
4515lang_leave_output_section_statement
4516 (fill_type *fill, const char *memspec,
4517 struct lang_output_section_phdr_list *phdrs, const char *lma_memspec)
252b5132 4518{
ee3cc2e2
RS
4519 lang_get_regions (&current_section->region,
4520 &current_section->lma_region,
4521 memspec, lma_memspec,
4522 current_section->load_base != 0);
252b5132 4523 current_section->fill = fill;
252b5132
RH
4524 current_section->phdrs = phdrs;
4525 stat_ptr = &statement_list;
4526}
4527
08da4cac
KH
4528/* Create an absolute symbol with the given name with the value of the
4529 address of first byte of the section named.
4530
4531 If the symbol already exists, then do nothing. */
252b5132 4532
252b5132 4533void
1579bae1 4534lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
252b5132
RH
4535{
4536 struct bfd_link_hash_entry *h;
4537
b34976b6 4538 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 4539 if (h == NULL)
252b5132
RH
4540 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4541
4542 if (h->type == bfd_link_hash_new
4543 || h->type == bfd_link_hash_undefined)
4544 {
4545 asection *sec;
4546
4547 h->type = bfd_link_hash_defined;
4548
4549 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 4550 if (sec == NULL)
252b5132
RH
4551 h->u.def.value = 0;
4552 else
4553 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4554
4555 h->u.def.section = bfd_abs_section_ptr;
4556 }
4557}
4558
08da4cac
KH
4559/* Create an absolute symbol with the given name with the value of the
4560 address of the first byte after the end of the section named.
4561
4562 If the symbol already exists, then do nothing. */
252b5132 4563
252b5132 4564void
1579bae1 4565lang_abs_symbol_at_end_of (const char *secname, const char *name)
252b5132
RH
4566{
4567 struct bfd_link_hash_entry *h;
4568
b34976b6 4569 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
1579bae1 4570 if (h == NULL)
252b5132
RH
4571 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4572
4573 if (h->type == bfd_link_hash_new
4574 || h->type == bfd_link_hash_undefined)
4575 {
4576 asection *sec;
4577
4578 h->type = bfd_link_hash_defined;
4579
4580 sec = bfd_get_section_by_name (output_bfd, secname);
1579bae1 4581 if (sec == NULL)
252b5132
RH
4582 h->u.def.value = 0;
4583 else
4584 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4cbfc3ac
TW
4585 + bfd_section_size (output_bfd, sec) /
4586 bfd_octets_per_byte (output_bfd));
252b5132
RH
4587
4588 h->u.def.section = bfd_abs_section_ptr;
4589 }
4590}
4591
4592void
1579bae1
AM
4593lang_statement_append (lang_statement_list_type *list,
4594 lang_statement_union_type *element,
4595 lang_statement_union_type **field)
252b5132
RH
4596{
4597 *(list->tail) = element;
4598 list->tail = field;
4599}
4600
4601/* Set the output format type. -oformat overrides scripts. */
4602
4603void
1579bae1
AM
4604lang_add_output_format (const char *format,
4605 const char *big,
4606 const char *little,
4607 int from_script)
252b5132
RH
4608{
4609 if (output_target == NULL || !from_script)
4610 {
4611 if (command_line.endian == ENDIAN_BIG
4612 && big != NULL)
4613 format = big;
4614 else if (command_line.endian == ENDIAN_LITTLE
4615 && little != NULL)
4616 format = little;
4617
4618 output_target = format;
4619 }
4620}
4621
4622/* Enter a group. This creates a new lang_group_statement, and sets
4623 stat_ptr to build new statements within the group. */
4624
4625void
1579bae1 4626lang_enter_group (void)
252b5132
RH
4627{
4628 lang_group_statement_type *g;
4629
4630 g = new_stat (lang_group_statement, stat_ptr);
4631 lang_list_init (&g->children);
4632 stat_ptr = &g->children;
4633}
4634
4635/* Leave a group. This just resets stat_ptr to start writing to the
4636 regular list of statements again. Note that this will not work if
4637 groups can occur inside anything else which can adjust stat_ptr,
4638 but currently they can't. */
4639
4640void
1579bae1 4641lang_leave_group (void)
252b5132
RH
4642{
4643 stat_ptr = &statement_list;
4644}
4645
4646/* Add a new program header. This is called for each entry in a PHDRS
4647 command in a linker script. */
4648
4649void
1579bae1
AM
4650lang_new_phdr (const char *name,
4651 etree_type *type,
4652 bfd_boolean filehdr,
4653 bfd_boolean phdrs,
4654 etree_type *at,
4655 etree_type *flags)
252b5132
RH
4656{
4657 struct lang_phdr *n, **pp;
4658
1579bae1 4659 n = stat_alloc (sizeof (struct lang_phdr));
252b5132
RH
4660 n->next = NULL;
4661 n->name = name;
4662 n->type = exp_get_value_int (type, 0, "program header type",
4663 lang_final_phase_enum);
4664 n->filehdr = filehdr;
4665 n->phdrs = phdrs;
4666 n->at = at;
4667 n->flags = flags;
4668
4669 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4670 ;
4671 *pp = n;
4672}
4673
4674/* Record the program header information in the output BFD. FIXME: We
4675 should not be calling an ELF specific function here. */
4676
4677static void
1579bae1 4678lang_record_phdrs (void)
252b5132
RH
4679{
4680 unsigned int alc;
4681 asection **secs;
4682 struct lang_output_section_phdr_list *last;
4683 struct lang_phdr *l;
4684 lang_statement_union_type *u;
4685
4686 alc = 10;
1579bae1 4687 secs = xmalloc (alc * sizeof (asection *));
252b5132
RH
4688 last = NULL;
4689 for (l = lang_phdr_list; l != NULL; l = l->next)
4690 {
4691 unsigned int c;
4692 flagword flags;
4693 bfd_vma at;
4694
4695 c = 0;
4696 for (u = lang_output_section_statement.head;
4697 u != NULL;
4698 u = u->output_section_statement.next)
4699 {
4700 lang_output_section_statement_type *os;
4701 struct lang_output_section_phdr_list *pl;
4702
4703 os = &u->output_section_statement;
4704
4705 pl = os->phdrs;
4706 if (pl != NULL)
4707 last = pl;
4708 else
4709 {
4710 if (os->sectype == noload_section
4711 || os->bfd_section == NULL
4712 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4713 continue;
4714 pl = last;
4715 }
4716
4717 if (os->bfd_section == NULL)
4718 continue;
4719
4720 for (; pl != NULL; pl = pl->next)
4721 {
4722 if (strcmp (pl->name, l->name) == 0)
4723 {
4724 if (c >= alc)
4725 {
4726 alc *= 2;
1579bae1 4727 secs = xrealloc (secs, alc * sizeof (asection *));
252b5132
RH
4728 }
4729 secs[c] = os->bfd_section;
4730 ++c;
b34976b6 4731 pl->used = TRUE;
252b5132
RH
4732 }
4733 }
4734 }
4735
4736 if (l->flags == NULL)
4737 flags = 0;
4738 else
4739 flags = exp_get_vma (l->flags, 0, "phdr flags",
4740 lang_final_phase_enum);
4741
4742 if (l->at == NULL)
4743 at = 0;
4744 else
4745 at = exp_get_vma (l->at, 0, "phdr load address",
4746 lang_final_phase_enum);
4747
4748 if (! bfd_record_phdr (output_bfd, l->type,
d1778b88 4749 l->flags != NULL, flags, l->at != NULL,
252b5132
RH
4750 at, l->filehdr, l->phdrs, c, secs))
4751 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4752 }
4753
4754 free (secs);
4755
4756 /* Make sure all the phdr assignments succeeded. */
4757 for (u = lang_output_section_statement.head;
4758 u != NULL;
4759 u = u->output_section_statement.next)
4760 {
4761 struct lang_output_section_phdr_list *pl;
4762
4763 if (u->output_section_statement.bfd_section == NULL)
4764 continue;
4765
4766 for (pl = u->output_section_statement.phdrs;
4767 pl != NULL;
4768 pl = pl->next)
4769 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4770 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4771 u->output_section_statement.name, pl->name);
4772 }
4773}
4774
4775/* Record a list of sections which may not be cross referenced. */
4776
4777void
1579bae1 4778lang_add_nocrossref (struct lang_nocrossref *l)
252b5132
RH
4779{
4780 struct lang_nocrossrefs *n;
4781
1579bae1 4782 n = xmalloc (sizeof *n);
252b5132
RH
4783 n->next = nocrossref_list;
4784 n->list = l;
4785 nocrossref_list = n;
4786
4787 /* Set notice_all so that we get informed about all symbols. */
b34976b6 4788 link_info.notice_all = TRUE;
252b5132
RH
4789}
4790\f
4791/* Overlay handling. We handle overlays with some static variables. */
4792
4793/* The overlay virtual address. */
4794static etree_type *overlay_vma;
7e7d5768
AM
4795/* And subsection alignment. */
4796static etree_type *overlay_subalign;
252b5132 4797
252b5132
RH
4798/* An expression for the maximum section size seen so far. */
4799static etree_type *overlay_max;
4800
4801/* A list of all the sections in this overlay. */
4802
89cdebba 4803struct overlay_list {
252b5132
RH
4804 struct overlay_list *next;
4805 lang_output_section_statement_type *os;
4806};
4807
4808static struct overlay_list *overlay_list;
4809
4810/* Start handling an overlay. */
4811
4812void
7e7d5768 4813lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
252b5132
RH
4814{
4815 /* The grammar should prevent nested overlays from occurring. */
7e7d5768
AM
4816 ASSERT (overlay_vma == NULL
4817 && overlay_subalign == NULL
4818 && overlay_max == NULL);
252b5132
RH
4819
4820 overlay_vma = vma_expr;
7e7d5768 4821 overlay_subalign = subalign;
252b5132
RH
4822}
4823
4824/* Start a section in an overlay. We handle this by calling
9f88b410
RS
4825 lang_enter_output_section_statement with the correct VMA.
4826 lang_leave_overlay sets up the LMA and memory regions. */
252b5132
RH
4827
4828void
1579bae1 4829lang_enter_overlay_section (const char *name)
252b5132
RH
4830{
4831 struct overlay_list *n;
4832 etree_type *size;
4833
4834 lang_enter_output_section_statement (name, overlay_vma, normal_section,
7e7d5768 4835 0, 0, overlay_subalign, 0);
252b5132 4836
9f88b410 4837 /* If this is the first section, then base the VMA of future
252b5132
RH
4838 sections on this one. This will work correctly even if `.' is
4839 used in the addresses. */
4840 if (overlay_list == NULL)
9f88b410 4841 overlay_vma = exp_nameop (ADDR, name);
252b5132
RH
4842
4843 /* Remember the section. */
1579bae1 4844 n = xmalloc (sizeof *n);
252b5132
RH
4845 n->os = current_section;
4846 n->next = overlay_list;
4847 overlay_list = n;
4848
4849 size = exp_nameop (SIZEOF, name);
4850
252b5132
RH
4851 /* Arrange to work out the maximum section end address. */
4852 if (overlay_max == NULL)
4853 overlay_max = size;
4854 else
4855 overlay_max = exp_binop (MAX_K, overlay_max, size);
4856}
4857
4858/* Finish a section in an overlay. There isn't any special to do
4859 here. */
4860
4861void
1579bae1
AM
4862lang_leave_overlay_section (fill_type *fill,
4863 struct lang_output_section_phdr_list *phdrs)
252b5132
RH
4864{
4865 const char *name;
4866 char *clean, *s2;
4867 const char *s1;
4868 char *buf;
4869
4870 name = current_section->name;
4871
a747ee4d
NC
4872 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4873 region and that no load-time region has been specified. It doesn't
4874 really matter what we say here, since lang_leave_overlay will
4875 override it. */
4876 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
252b5132
RH
4877
4878 /* Define the magic symbols. */
4879
4880 clean = xmalloc (strlen (name) + 1);
4881 s2 = clean;
4882 for (s1 = name; *s1 != '\0'; s1++)
3882b010 4883 if (ISALNUM (*s1) || *s1 == '_')
252b5132
RH
4884 *s2++ = *s1;
4885 *s2 = '\0';
4886
4887 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4888 sprintf (buf, "__load_start_%s", clean);
4889 lang_add_assignment (exp_assop ('=', buf,
4890 exp_nameop (LOADADDR, name)));
4891
4892 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4893 sprintf (buf, "__load_stop_%s", clean);
4894 lang_add_assignment (exp_assop ('=', buf,
4895 exp_binop ('+',
4896 exp_nameop (LOADADDR, name),
4897 exp_nameop (SIZEOF, name))));
4898
4899 free (clean);
4900}
4901
4902/* Finish an overlay. If there are any overlay wide settings, this
4903 looks through all the sections in the overlay and sets them. */
4904
4905void
1579bae1
AM
4906lang_leave_overlay (etree_type *lma_expr,
4907 int nocrossrefs,
4908 fill_type *fill,
4909 const char *memspec,
4910 struct lang_output_section_phdr_list *phdrs,
4911 const char *lma_memspec)
252b5132
RH
4912{
4913 lang_memory_region_type *region;
562d3460 4914 lang_memory_region_type *lma_region;
252b5132
RH
4915 struct overlay_list *l;
4916 struct lang_nocrossref *nocrossref;
4917
ee3cc2e2
RS
4918 lang_get_regions (&region, &lma_region,
4919 memspec, lma_memspec,
4920 lma_expr != 0);
562d3460 4921
252b5132
RH
4922 nocrossref = NULL;
4923
9f88b410
RS
4924 /* After setting the size of the last section, set '.' to end of the
4925 overlay region. */
4926 if (overlay_list != NULL)
4927 overlay_list->os->update_dot_tree
4928 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4929
252b5132
RH
4930 l = overlay_list;
4931 while (l != NULL)
4932 {
4933 struct overlay_list *next;
4934
1579bae1 4935 if (fill != NULL && l->os->fill == NULL)
252b5132 4936 l->os->fill = fill;
1545243b 4937
9f88b410
RS
4938 l->os->region = region;
4939 l->os->lma_region = lma_region;
4940
4941 /* The first section has the load address specified in the
4942 OVERLAY statement. The rest are worked out from that.
4943 The base address is not needed (and should be null) if
4944 an LMA region was specified. */
4945 if (l->next == 0)
4946 l->os->load_base = lma_expr;
4947 else if (lma_region == 0)
4948 l->os->load_base = exp_binop ('+',
4949 exp_nameop (LOADADDR, l->next->os->name),
4950 exp_nameop (SIZEOF, l->next->os->name));
1545243b 4951
252b5132
RH
4952 if (phdrs != NULL && l->os->phdrs == NULL)
4953 l->os->phdrs = phdrs;
4954
9f88b410 4955 if (nocrossrefs)
252b5132
RH
4956 {
4957 struct lang_nocrossref *nc;
4958
1579bae1 4959 nc = xmalloc (sizeof *nc);
252b5132
RH
4960 nc->name = l->os->name;
4961 nc->next = nocrossref;
4962 nocrossref = nc;
4963 }
4964
4965 next = l->next;
4966 free (l);
4967 l = next;
4968 }
4969
4970 if (nocrossref != NULL)
4971 lang_add_nocrossref (nocrossref);
4972
252b5132 4973 overlay_vma = NULL;
252b5132
RH
4974 overlay_list = NULL;
4975 overlay_max = NULL;
4976}
4977\f
4978/* Version handling. This is only useful for ELF. */
4979
4980/* This global variable holds the version tree that we build. */
4981
4982struct bfd_elf_version_tree *lang_elf_version_info;
4983
108ba305
JJ
4984/* If PREV is NULL, return first version pattern matching particular symbol.
4985 If PREV is non-NULL, return first version pattern matching particular
4986 symbol after PREV (previously returned by lang_vers_match). */
252b5132 4987
108ba305
JJ
4988static struct bfd_elf_version_expr *
4989lang_vers_match (struct bfd_elf_version_expr_head *head,
4990 struct bfd_elf_version_expr *prev,
4991 const char *sym)
252b5132 4992{
108ba305
JJ
4993 const char *cxx_sym = sym;
4994 const char *java_sym = sym;
4995 struct bfd_elf_version_expr *expr = NULL;
252b5132 4996
108ba305 4997 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
252b5132 4998 {
108ba305
JJ
4999 cxx_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
5000 if (!cxx_sym)
5001 cxx_sym = sym;
252b5132 5002 }
df816215 5003 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
252b5132 5004 {
108ba305
JJ
5005 java_sym = cplus_demangle (sym, DMGL_JAVA);
5006 if (!java_sym)
5007 java_sym = sym;
252b5132
RH
5008 }
5009
5e35cbc2 5010 if (head->htab && (prev == NULL || prev->symbol))
252b5132 5011 {
108ba305
JJ
5012 struct bfd_elf_version_expr e;
5013
5014 switch (prev ? prev->mask : 0)
5015 {
5016 case 0:
5017 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5018 {
5e35cbc2 5019 e.symbol = sym;
108ba305 5020 expr = htab_find (head->htab, &e);
5e35cbc2 5021 while (expr && strcmp (expr->symbol, sym) == 0)
108ba305
JJ
5022 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5023 goto out_ret;
5024 else
5025 expr = expr->next;
5026 }
5027 /* Fallthrough */
5028 case BFD_ELF_VERSION_C_TYPE:
5029 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5030 {
5e35cbc2 5031 e.symbol = cxx_sym;
108ba305 5032 expr = htab_find (head->htab, &e);
7a995eb3 5033 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
108ba305
JJ
5034 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5035 goto out_ret;
5036 else
5037 expr = expr->next;
5038 }
5039 /* Fallthrough */
5040 case BFD_ELF_VERSION_CXX_TYPE:
5041 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5042 {
5e35cbc2 5043 e.symbol = java_sym;
108ba305 5044 expr = htab_find (head->htab, &e);
7a995eb3 5045 while (expr && strcmp (expr->symbol, java_sym) == 0)
108ba305
JJ
5046 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5047 goto out_ret;
5048 else
5049 expr = expr->next;
5050 }
5051 /* Fallthrough */
5052 default:
5053 break;
5054 }
252b5132 5055 }
108ba305
JJ
5056
5057 /* Finally, try the wildcards. */
5e35cbc2 5058 if (prev == NULL || prev->symbol)
108ba305 5059 expr = head->remaining;
252b5132 5060 else
108ba305
JJ
5061 expr = prev->next;
5062 while (expr)
252b5132 5063 {
108ba305
JJ
5064 const char *s;
5065
5066 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5067 break;
5068
5069 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5070 s = java_sym;
5071 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5072 s = cxx_sym;
5073 else
5074 s = sym;
5e35cbc2 5075 if (fnmatch (expr->pattern, s, 0) == 0)
108ba305
JJ
5076 break;
5077 expr = expr->next;
252b5132
RH
5078 }
5079
108ba305
JJ
5080out_ret:
5081 if (cxx_sym != sym)
5082 free ((char *) cxx_sym);
5083 if (java_sym != sym)
5084 free ((char *) java_sym);
5085 return expr;
252b5132
RH
5086}
5087
5e35cbc2
L
5088/* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5089 return a string pointing to the symbol name. */
5090
5091static const char *
5092realsymbol (const char *pattern)
5093{
5094 const char *p;
5095 bfd_boolean changed = FALSE, backslash = FALSE;
5096 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5097
5098 for (p = pattern, s = symbol; *p != '\0'; ++p)
5099 {
5100 /* It is a glob pattern only if there is no preceding
5101 backslash. */
5102 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5103 {
5104 free (symbol);
5105 return NULL;
5106 }
5107
5108 if (backslash)
5109 {
5110 /* Remove the preceding backslash. */
5111 *(s - 1) = *p;
5112 changed = TRUE;
5113 }
5114 else
5115 *s++ = *p;
5116
5117 backslash = *p == '\\';
5118 }
5119
5120 if (changed)
5121 {
5122 *s = '\0';
5123 return symbol;
5124 }
5125 else
5126 {
5127 free (symbol);
5128 return pattern;
5129 }
5130}
5131
252b5132
RH
5132/* This is called for each variable name or match expression. */
5133
5134struct bfd_elf_version_expr *
1579bae1
AM
5135lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5136 const char *new,
5137 const char *lang)
252b5132
RH
5138{
5139 struct bfd_elf_version_expr *ret;
5140
1579bae1 5141 ret = xmalloc (sizeof *ret);
252b5132
RH
5142 ret->next = orig;
5143 ret->pattern = new;
31941635
L
5144 ret->symver = 0;
5145 ret->script = 0;
5e35cbc2 5146 ret->symbol = realsymbol (new);
252b5132
RH
5147
5148 if (lang == NULL || strcasecmp (lang, "C") == 0)
108ba305 5149 ret->mask = BFD_ELF_VERSION_C_TYPE;
252b5132 5150 else if (strcasecmp (lang, "C++") == 0)
108ba305 5151 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
252b5132 5152 else if (strcasecmp (lang, "Java") == 0)
108ba305 5153 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
252b5132
RH
5154 else
5155 {
5156 einfo (_("%X%P: unknown language `%s' in version information\n"),
5157 lang);
108ba305 5158 ret->mask = BFD_ELF_VERSION_C_TYPE;
252b5132
RH
5159 }
5160
fac1652d 5161 return ldemul_new_vers_pattern (ret);
252b5132
RH
5162}
5163
5164/* This is called for each set of variable names and match
5165 expressions. */
5166
5167struct bfd_elf_version_tree *
1579bae1
AM
5168lang_new_vers_node (struct bfd_elf_version_expr *globals,
5169 struct bfd_elf_version_expr *locals)
252b5132
RH
5170{
5171 struct bfd_elf_version_tree *ret;
5172
108ba305
JJ
5173 ret = xcalloc (1, sizeof *ret);
5174 ret->globals.list = globals;
5175 ret->locals.list = locals;
5176 ret->match = lang_vers_match;
252b5132 5177 ret->name_indx = (unsigned int) -1;
252b5132
RH
5178 return ret;
5179}
5180
5181/* This static variable keeps track of version indices. */
5182
5183static int version_index;
5184
108ba305
JJ
5185static hashval_t
5186version_expr_head_hash (const void *p)
5187{
5188 const struct bfd_elf_version_expr *e = p;
5189
5e35cbc2 5190 return htab_hash_string (e->symbol);
108ba305
JJ
5191}
5192
5193static int
5194version_expr_head_eq (const void *p1, const void *p2)
5195{
5196 const struct bfd_elf_version_expr *e1 = p1;
5197 const struct bfd_elf_version_expr *e2 = p2;
5198
5e35cbc2 5199 return strcmp (e1->symbol, e2->symbol) == 0;
108ba305
JJ
5200}
5201
5202static void
5203lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5204{
5205 size_t count = 0;
5206 struct bfd_elf_version_expr *e, *next;
5207 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5208
5209 for (e = head->list; e; e = e->next)
5210 {
5e35cbc2 5211 if (e->symbol)
108ba305
JJ
5212 count++;
5213 head->mask |= e->mask;
5214 }
5215
5216 if (count)
5217 {
5218 head->htab = htab_create (count * 2, version_expr_head_hash,
5219 version_expr_head_eq, NULL);
5220 list_loc = &head->list;
5221 remaining_loc = &head->remaining;
5222 for (e = head->list; e; e = next)
5223 {
5224 next = e->next;
5e35cbc2 5225 if (!e->symbol)
108ba305
JJ
5226 {
5227 *remaining_loc = e;
5228 remaining_loc = &e->next;
5229 }
5230 else
5231 {
5232 void **loc = htab_find_slot (head->htab, e, INSERT);
5233
5234 if (*loc)
5235 {
5236 struct bfd_elf_version_expr *e1, *last;
5237
5238 e1 = *loc;
5239 last = NULL;
5240 do
5241 {
5242 if (e1->mask == e->mask)
5243 {
5244 last = NULL;
5245 break;
5246 }
5247 last = e1;
5248 e1 = e1->next;
5249 }
5e35cbc2 5250 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
108ba305
JJ
5251
5252 if (last == NULL)
5253 {
5254 /* This is a duplicate. */
5255 /* FIXME: Memory leak. Sometimes pattern is not
5256 xmalloced alone, but in larger chunk of memory. */
5e35cbc2 5257 /* free (e->symbol); */
108ba305
JJ
5258 free (e);
5259 }
5260 else
5261 {
5262 e->next = last->next;
5263 last->next = e;
5264 }
5265 }
5266 else
5267 {
5268 *loc = e;
5269 *list_loc = e;
5270 list_loc = &e->next;
5271 }
5272 }
5273 }
5274 *remaining_loc = NULL;
5275 *list_loc = head->remaining;
5276 }
5277 else
5278 head->remaining = head->list;
5279}
5280
252b5132
RH
5281/* This is called when we know the name and dependencies of the
5282 version. */
5283
5284void
1579bae1
AM
5285lang_register_vers_node (const char *name,
5286 struct bfd_elf_version_tree *version,
5287 struct bfd_elf_version_deps *deps)
252b5132
RH
5288{
5289 struct bfd_elf_version_tree *t, **pp;
5290 struct bfd_elf_version_expr *e1;
5291
6b9b879a
JJ
5292 if (name == NULL)
5293 name = "";
5294
5295 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5296 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5297 {
5298 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5ed6aba4 5299 free (version);
6b9b879a
JJ
5300 return;
5301 }
5302
252b5132
RH
5303 /* Make sure this node has a unique name. */
5304 for (t = lang_elf_version_info; t != NULL; t = t->next)
5305 if (strcmp (t->name, name) == 0)
5306 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5307
108ba305
JJ
5308 lang_finalize_version_expr_head (&version->globals);
5309 lang_finalize_version_expr_head (&version->locals);
5310
252b5132
RH
5311 /* Check the global and local match names, and make sure there
5312 aren't any duplicates. */
5313
108ba305 5314 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
252b5132
RH
5315 {
5316 for (t = lang_elf_version_info; t != NULL; t = t->next)
5317 {
5318 struct bfd_elf_version_expr *e2;
5319
5e35cbc2 5320 if (t->locals.htab && e1->symbol)
108ba305
JJ
5321 {
5322 e2 = htab_find (t->locals.htab, e1);
5e35cbc2 5323 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
108ba305
JJ
5324 {
5325 if (e1->mask == e2->mask)
5326 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5e35cbc2 5327 e1->symbol);
108ba305
JJ
5328 e2 = e2->next;
5329 }
5330 }
5e35cbc2 5331 else if (!e1->symbol)
108ba305
JJ
5332 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5333 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5334 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5335 e1->pattern);
252b5132
RH
5336 }
5337 }
5338
108ba305 5339 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
252b5132
RH
5340 {
5341 for (t = lang_elf_version_info; t != NULL; t = t->next)
5342 {
5343 struct bfd_elf_version_expr *e2;
5344
5e35cbc2 5345 if (t->globals.htab && e1->symbol)
108ba305
JJ
5346 {
5347 e2 = htab_find (t->globals.htab, e1);
5e35cbc2 5348 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
108ba305
JJ
5349 {
5350 if (e1->mask == e2->mask)
5351 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5e35cbc2 5352 e1->symbol);
108ba305
JJ
5353 e2 = e2->next;
5354 }
5355 }
5e35cbc2 5356 else if (!e1->symbol)
108ba305
JJ
5357 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5358 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5359 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5360 e1->pattern);
252b5132
RH
5361 }
5362 }
5363
5364 version->deps = deps;
5365 version->name = name;
6b9b879a
JJ
5366 if (name[0] != '\0')
5367 {
5368 ++version_index;
5369 version->vernum = version_index;
5370 }
5371 else
5372 version->vernum = 0;
252b5132
RH
5373
5374 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5375 ;
5376 *pp = version;
5377}
5378
5379/* This is called when we see a version dependency. */
5380
5381struct bfd_elf_version_deps *
1579bae1 5382lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
252b5132
RH
5383{
5384 struct bfd_elf_version_deps *ret;
5385 struct bfd_elf_version_tree *t;
5386
1579bae1 5387 ret = xmalloc (sizeof *ret);
252b5132
RH
5388 ret->next = list;
5389
5390 for (t = lang_elf_version_info; t != NULL; t = t->next)
5391 {
5392 if (strcmp (t->name, name) == 0)
5393 {
5394 ret->version_needed = t;
5395 return ret;
5396 }
5397 }
5398
5399 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5400
5401 return ret;
5402}
5403
5404static void
1579bae1 5405lang_do_version_exports_section (void)
252b5132
RH
5406{
5407 struct bfd_elf_version_expr *greg = NULL, *lreg;
5408
5409 LANG_FOR_EACH_INPUT_STATEMENT (is)
5410 {
5411 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5412 char *contents, *p;
5413 bfd_size_type len;
5414
5415 if (sec == NULL)
b7a26f91 5416 continue;
252b5132
RH
5417
5418 len = bfd_section_size (is->the_bfd, sec);
5419 contents = xmalloc (len);
5420 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6f9efd97 5421 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
252b5132
RH
5422
5423 p = contents;
89cdebba 5424 while (p < contents + len)
252b5132 5425 {
313e35ee 5426 greg = lang_new_vers_pattern (greg, p, NULL);
252b5132
RH
5427 p = strchr (p, '\0') + 1;
5428 }
5429
5430 /* Do not free the contents, as we used them creating the regex. */
5431
5432 /* Do not include this section in the link. */
5433 bfd_set_section_flags (is->the_bfd, sec,
5434 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5435 }
5436
313e35ee 5437 lreg = lang_new_vers_pattern (NULL, "*", NULL);
252b5132
RH
5438 lang_register_vers_node (command_line.version_exports_section,
5439 lang_new_vers_node (greg, lreg), NULL);
5440}
577a0623
AM
5441
5442void
1579bae1 5443lang_add_unique (const char *name)
577a0623
AM
5444{
5445 struct unique_sections *ent;
5446
5447 for (ent = unique_section_list; ent; ent = ent->next)
5448 if (strcmp (ent->name, name) == 0)
5449 return;
5450
1579bae1 5451 ent = xmalloc (sizeof *ent);
577a0623
AM
5452 ent->name = xstrdup (name);
5453 ent->next = unique_section_list;
5454 unique_section_list = ent;
5455}
This page took 0.512498 seconds and 4 git commands to generate.