source: branches/libc-0.6/src/binutils/ld/ldlang.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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