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