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