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