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