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