* ld-mmix/b-fixo2.d, ld-mmix/bpo-18.d, ld-mmix/bpo-18m.d,
[binutils.git] / ld / ldlang.c
blobfccab0d681af0745030ce6f3e8f3e16d76017bcc
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.
6 This file is part of GLD, the Gnu Linker.
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.
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.
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. */
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"
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"
43 #ifndef offsetof
44 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
45 #endif
47 /* Locals variables. */
48 static struct obstack stat_obstack;
50 #define obstack_chunk_alloc xmalloc
51 #define obstack_chunk_free free
52 static const char *startup_file;
53 static lang_statement_list_type input_file_chain;
54 static bfd_boolean placed_commons = FALSE;
55 static lang_output_section_statement_type *default_common_section;
56 static bfd_boolean map_option_f;
57 static bfd_vma print_dot;
58 static lang_input_statement_type *first_file;
59 static const char *current_target;
60 static const char *output_target;
61 static lang_statement_list_type statement_list;
62 static struct lang_phdr *lang_phdr_list;
64 /* Forward declarations. */
65 static lang_statement_union_type *new_statement
66 PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*) (lang_statement_union_type *),
69 lang_statement_union_type *));
70 static lang_input_statement_type *new_afile
71 PARAMS ((const char *, lang_input_file_enum_type, const char *,
72 bfd_boolean));
73 static lang_memory_region_type *lang_memory_default
74 PARAMS ((asection *));
75 static void lang_map_flags
76 PARAMS ((flagword));
77 static void init_os
78 PARAMS ((lang_output_section_statement_type *));
79 static void exp_init_os
80 PARAMS ((etree_type *));
81 static void section_already_linked
82 PARAMS ((bfd *, asection *, PTR));
83 static struct bfd_hash_entry *already_linked_newfunc
84 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
85 static void already_linked_table_init
86 PARAMS ((void));
87 static void already_linked_table_free
88 PARAMS ((void));
89 static bfd_boolean wildcardp
90 PARAMS ((const char *));
91 static lang_statement_union_type *wild_sort
92 PARAMS ((lang_wild_statement_type *, struct wildcard_list *,
93 lang_input_statement_type *, asection *));
94 static void output_section_callback
95 PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
96 lang_input_statement_type *, PTR));
97 static lang_input_statement_type *lookup_name
98 PARAMS ((const char *));
99 static bfd_boolean load_symbols
100 PARAMS ((lang_input_statement_type *, lang_statement_list_type *));
101 static void wild
102 PARAMS ((lang_wild_statement_type *,
103 const char *, lang_output_section_statement_type *));
104 static bfd *open_output
105 PARAMS ((const char *));
106 static void ldlang_open_output
107 PARAMS ((lang_statement_union_type *));
108 static void open_input_bfds
109 PARAMS ((lang_statement_union_type *, bfd_boolean));
110 static void lang_reasonable_defaults
111 PARAMS ((void));
112 static void insert_undefined
113 PARAMS ((const char *));
114 static void lang_place_undefineds
115 PARAMS ((void));
116 static void map_input_to_output_sections
117 PARAMS ((lang_statement_union_type *, const char *,
118 lang_output_section_statement_type *));
119 static void strip_excluded_output_sections
120 PARAMS ((void));
121 static void print_output_section_statement
122 PARAMS ((lang_output_section_statement_type *));
123 static void print_assignment
124 PARAMS ((lang_assignment_statement_type *,
125 lang_output_section_statement_type *));
126 static void print_input_statement
127 PARAMS ((lang_input_statement_type *));
128 static bfd_boolean print_one_symbol
129 PARAMS ((struct bfd_link_hash_entry *, PTR));
130 static void print_input_section
131 PARAMS ((lang_input_section_type *));
132 static void print_fill_statement
133 PARAMS ((lang_fill_statement_type *));
134 static void print_data_statement
135 PARAMS ((lang_data_statement_type *));
136 static void print_address_statement
137 PARAMS ((lang_address_statement_type *));
138 static void print_reloc_statement
139 PARAMS ((lang_reloc_statement_type *));
140 static void print_padding_statement
141 PARAMS ((lang_padding_statement_type *));
142 static void print_wild_statement
143 PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
144 static void print_group
145 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
146 static void print_statement
147 PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
148 static void print_statement_list
149 PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
150 static void print_statements
151 PARAMS ((void));
152 static void insert_pad
153 PARAMS ((lang_statement_union_type **, fill_type *,
154 unsigned int, asection *, bfd_vma));
155 static bfd_vma size_input_section
156 PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
157 fill_type *, bfd_vma));
158 static void lang_finish
159 PARAMS ((void));
160 static void ignore_bfd_errors
161 PARAMS ((const char *, ...));
162 static void lang_check
163 PARAMS ((void));
164 static void lang_common
165 PARAMS ((void));
166 static bfd_boolean lang_one_common
167 PARAMS ((struct bfd_link_hash_entry *, PTR));
168 static void lang_place_orphans
169 PARAMS ((void));
170 static int topower
171 PARAMS ((int));
172 static void lang_set_startof
173 PARAMS ((void));
174 static void gc_section_callback
175 PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
176 lang_input_statement_type *, PTR));
177 static void lang_get_regions
178 PARAMS ((struct memory_region_struct **, struct memory_region_struct **,
179 const char *, const char *, int));
180 static void lang_record_phdrs
181 PARAMS ((void));
182 static void lang_gc_wild
183 PARAMS ((lang_wild_statement_type *));
184 static void lang_gc_sections_1
185 PARAMS ((lang_statement_union_type *));
186 static void lang_gc_sections
187 PARAMS ((void));
188 static int lang_vers_match_lang_c
189 PARAMS ((struct bfd_elf_version_expr *, const char *));
190 static int lang_vers_match_lang_cplusplus
191 PARAMS ((struct bfd_elf_version_expr *, const char *));
192 static int lang_vers_match_lang_java
193 PARAMS ((struct bfd_elf_version_expr *, const char *));
194 static void lang_do_version_exports_section
195 PARAMS ((void));
196 static void lang_check_section_addresses
197 PARAMS ((void));
198 static void os_region_check
199 PARAMS ((lang_output_section_statement_type *,
200 struct memory_region_struct *, etree_type *, bfd_vma));
201 static 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));
205 typedef void (*callback_t)
206 PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
207 lang_input_statement_type *, PTR));
208 static void walk_wild
209 PARAMS ((lang_wild_statement_type *, callback_t, PTR));
210 static void walk_wild_section
211 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
212 callback_t, PTR));
213 static void walk_wild_file
214 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
215 callback_t, PTR));
216 static int get_target
217 PARAMS ((const bfd_target *, PTR));
218 static void stricpy
219 PARAMS ((char *, char *));
220 static void strcut
221 PARAMS ((char *, char *));
222 static int name_compare
223 PARAMS ((char *, char *));
224 static int closest_target_match
225 PARAMS ((const bfd_target *, PTR));
226 static char * get_first_input_target
227 PARAMS ((void));
229 /* Exported variables. */
230 lang_output_section_statement_type *abs_output_section;
231 lang_statement_list_type lang_output_section_statement;
232 lang_statement_list_type *stat_ptr = &statement_list;
233 lang_statement_list_type file_chain = { NULL, NULL };
234 struct bfd_sym_chain entry_symbol = { NULL, NULL };
235 const char *entry_section = ".text";
236 bfd_boolean entry_from_cmdline;
237 bfd_boolean lang_has_input_file = FALSE;
238 bfd_boolean had_output_filename = FALSE;
239 bfd_boolean lang_float_flag = FALSE;
240 bfd_boolean delete_output_file_on_failure = FALSE;
241 struct lang_nocrossrefs *nocrossref_list;
242 struct unique_sections *unique_section_list;
243 static bfd_boolean ldlang_sysrooted_script = FALSE;
245 etree_type *base; /* Relocation base - or null */
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
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)
258 #define outside_section_address(q) \
259 ((q)->output_offset + (q)->output_section->vma)
261 #define outside_symbol_address(q) \
262 ((q)->value + outside_section_address (q->section))
264 #define SECTION_NAME_MAP_LENGTH (16)
267 stat_alloc (size)
268 size_t size;
270 return obstack_alloc (&stat_obstack, size);
273 bfd_boolean
274 unique_section_p (secnam)
275 const char *secnam;
277 struct unique_sections *unam;
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)
284 return TRUE;
287 return FALSE;
290 /* Generic traversal routines for finding matching sections. */
292 static void
293 walk_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;
299 asection *s;
301 if (file->just_syms_flag)
302 return;
304 for (s = file->the_bfd->sections; s != NULL; s = s->next)
306 struct wildcard_list *sec;
308 sec = ptr->section_list;
309 if (sec == NULL)
310 (*callback) (ptr, sec, s, file, data);
312 while (sec != NULL)
314 bfd_boolean skip = FALSE;
315 struct name_list *list_tmp;
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)
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;
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)
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;
343 if (skip)
344 break;
347 if (!skip && sec->spec.name != NULL)
349 const char *sname = bfd_get_section_name (file->the_bfd, s);
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;
357 if (!skip)
358 (*callback) (ptr, sec, s, file, data);
360 sec = sec->next;
365 /* Handle a wild statement for a single file F. */
367 static void
368 walk_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;
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
379 bfd *member;
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)
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)
393 walk_wild_section (s,
394 (lang_input_statement_type *) member->usrdata,
395 callback, data);
398 member = bfd_openr_next_archived_file (f->the_bfd, member);
403 static void
404 walk_wild (s, callback, data)
405 lang_wild_statement_type *s;
406 callback_t callback;
407 PTR data;
409 const char *file_spec = s->filename;
411 if (file_spec == NULL)
413 /* Perform the iteration over all files in the list. */
414 LANG_FOR_EACH_INPUT_STATEMENT (f)
416 walk_wild_file (s, f, callback, data);
419 else if (wildcardp (file_spec))
421 LANG_FOR_EACH_INPUT_STATEMENT (f)
423 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
424 walk_wild_file (s, f, callback, data);
427 else
429 lang_input_statement_type *f;
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);
438 /* lang_for_each_statement walks the parse tree and calls the provided
439 function for each node. */
441 static void
442 lang_for_each_statement_worker (func, s)
443 void (*func) PARAMS ((lang_statement_union_type *));
444 lang_statement_union_type *s;
446 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
448 func (s);
450 switch (s->header.type)
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;
488 void
489 lang_for_each_statement (func)
490 void (*func) PARAMS ((lang_statement_union_type *));
492 lang_for_each_statement_worker (func, statement_list.head);
495 /*----------------------------------------------------------------------*/
497 void
498 lang_list_init (list)
499 lang_statement_list_type *list;
501 list->head = (lang_statement_union_type *) NULL;
502 list->tail = &list->head;
505 /* Build a new statement node for the parse tree. */
507 static lang_statement_union_type *
508 new_statement (type, size, list)
509 enum statement_enum type;
510 size_t size;
511 lang_statement_list_type *list;
513 lang_statement_union_type *new = (lang_statement_union_type *)
514 stat_alloc (size);
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;
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.
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. */
531 static lang_input_statement_type *
532 new_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;
538 lang_input_statement_type *p;
540 if (add_to_list)
541 p = new_stat (lang_input_statement, stat_ptr);
542 else
544 p = ((lang_input_statement_type *)
545 stat_alloc (sizeof (lang_input_statement_type)));
546 p->header.next = NULL;
549 lang_has_input_file = TRUE;
550 p->target = target;
551 p->sysrooted = FALSE;
552 switch (file_type)
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 ();
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;
620 lang_input_statement_type *
621 lang_add_input_file (name, file_type, target)
622 const char *name;
623 lang_input_file_enum_type file_type;
624 const char *target;
626 lang_has_input_file = TRUE;
627 return new_afile (name, file_type, target, TRUE);
630 /* Build enough state so that the parser can build its tree. */
632 void
633 lang_init ()
635 obstack_begin (&stat_obstack, 1000);
637 stat_ptr = &statement_list;
639 lang_list_init (stat_ptr);
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);
650 abs_output_section->bfd_section = bfd_abs_section_ptr;
654 /*----------------------------------------------------------------------
655 A region is an area of memory declared with the
656 MEMORY { name:org=exp, len=exp ... }
657 syntax.
659 We maintain a list of all the regions here.
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. */
664 static lang_memory_region_type *lang_memory_region_list;
665 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
667 lang_memory_region_type *
668 lang_memory_region_lookup (name)
669 const char *const name;
671 lang_memory_region_type *p;
673 /* NAME is NULL for LMA memspecs if no region was specified. */
674 if (name == NULL)
675 return NULL;
677 for (p = lang_memory_region_list;
678 p != (lang_memory_region_type *) NULL;
679 p = p->next)
681 if (strcmp (p->name, name) == 0)
683 return p;
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)
696 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
698 return lang_memory_region_list;
701 #endif
704 lang_memory_region_type *new =
705 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
707 new->name = xstrdup (name);
708 new->next = (lang_memory_region_type *) NULL;
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;
719 return new;
723 static lang_memory_region_type *
724 lang_memory_default (section)
725 asection *section;
727 lang_memory_region_type *p;
729 flagword sec_flags = section->flags;
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;
735 for (p = lang_memory_region_list;
736 p != (lang_memory_region_type *) NULL;
737 p = p->next)
739 if ((p->flags & sec_flags) != 0
740 && (p->not_flags & sec_flags) == 0)
742 return p;
745 return lang_memory_region_lookup ("*default*");
748 lang_output_section_statement_type *
749 lang_output_section_find (name)
750 const char *const name;
752 lang_statement_union_type *u;
753 lang_output_section_statement_type *lookup;
755 for (u = lang_output_section_statement.head;
756 u != (lang_statement_union_type *) NULL;
757 u = lookup->next)
759 lookup = &u->output_section_statement;
760 if (strcmp (name, lookup->name) == 0)
762 return lookup;
765 return (lang_output_section_statement_type *) NULL;
768 lang_output_section_statement_type *
769 lang_output_section_statement_lookup (name)
770 const char *const name;
772 lang_output_section_statement_type *lookup;
774 lookup = lang_output_section_find (name);
775 if (lookup == (lang_output_section_statement_type *) NULL)
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;
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);
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;
801 lang_statement_append (&lang_output_section_statement,
802 (lang_statement_union_type *) lookup,
803 &lookup->next);
805 return lookup;
808 static void
809 lang_map_flags (flag)
810 flagword flag;
812 if (flag & SEC_ALLOC)
813 minfo ("a");
815 if (flag & SEC_CODE)
816 minfo ("x");
818 if (flag & SEC_READONLY)
819 minfo ("r");
821 if (flag & SEC_DATA)
822 minfo ("w");
824 if (flag & SEC_LOAD)
825 minfo ("l");
828 void
829 lang_map ()
831 lang_memory_region_type *m;
833 minfo (_("\nMemory Configuration\n\n"));
834 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
835 _("Name"), _("Origin"), _("Length"), _("Attributes"));
837 for (m = lang_memory_region_list;
838 m != (lang_memory_region_type *) NULL;
839 m = m->next)
841 char buf[100];
842 int len;
844 fprintf (config.map_file, "%-16s ", m->name);
846 sprintf_vma (buf, m->origin);
847 minfo ("0x%s ", buf);
848 len = strlen (buf);
849 while (len < 16)
851 print_space ();
852 ++len;
855 minfo ("0x%V", m->length);
856 if (m->flags || m->not_flags)
858 #ifndef BFD64
859 minfo (" ");
860 #endif
861 if (m->flags)
863 print_space ();
864 lang_map_flags (m->flags);
867 if (m->not_flags)
869 minfo (" !");
870 lang_map_flags (m->not_flags);
874 print_nl ();
877 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
879 print_statements ();
882 /* Initialize an output section. */
884 static void
885 init_os (s)
886 lang_output_section_statement_type *s;
888 section_userdata_type *new;
890 if (s->bfd_section != NULL)
891 return;
893 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
894 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
896 new = ((section_userdata_type *)
897 stat_alloc (sizeof (section_userdata_type)));
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)
904 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
905 output_bfd->xvec->name, s->name);
907 s->bfd_section->output_section = s->bfd_section;
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;
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);
919 if (s->load_base != NULL)
920 exp_init_os (s->load_base);
923 /* Make sure that all output sections mentioned in an expression are
924 initialized. */
926 static void
927 exp_init_os (exp)
928 etree_type *exp;
930 switch (exp->type.node_class)
932 case etree_assign:
933 exp_init_os (exp->assign.src);
934 break;
936 case etree_binary:
937 exp_init_os (exp->binary.lhs);
938 exp_init_os (exp->binary.rhs);
939 break;
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;
947 case etree_unary:
948 exp_init_os (exp->unary.child);
949 break;
951 case etree_name:
952 switch (exp->type.node_code)
954 case ADDR:
955 case LOADADDR:
956 case SIZEOF:
958 lang_output_section_statement_type *os;
960 os = lang_output_section_find (exp->name.name);
961 if (os != NULL && os->bfd_section == NULL)
962 init_os (os);
965 break;
967 default:
968 break;
972 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
973 once into the output. This routine checks each section, and
974 arrange to discard it if a section of the same name has already
975 been linked. If the section has COMDAT information, then it uses
976 that to decide whether the section should be included. This code
977 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
978 that is, it does not depend solely upon the section name.
979 section_already_linked is called via bfd_map_over_sections. */
981 /* This is the shape of the elements inside the already_linked hash
982 table. It maps a name onto a list of already_linked elements with
983 the same name. It's possible to get more than one element in a
984 list if the COMDAT sections have different names. */
986 struct already_linked_hash_entry
988 struct bfd_hash_entry root;
989 struct already_linked *entry;
992 struct already_linked
994 struct already_linked *next;
995 asection *sec;
998 /* The hash table. */
1000 static struct bfd_hash_table already_linked_table;
1002 static void
1003 section_already_linked (abfd, sec, data)
1004 bfd *abfd;
1005 asection *sec;
1006 PTR data;
1008 lang_input_statement_type *entry = (lang_input_statement_type *) data;
1009 flagword flags;
1010 const char *name;
1011 struct already_linked *l;
1012 struct already_linked_hash_entry *already_linked_list;
1014 /* If we are only reading symbols from this object, then we want to
1015 discard all sections. */
1016 if (entry->just_syms_flag)
1018 bfd_link_just_syms (sec, &link_info);
1019 return;
1022 flags = bfd_get_section_flags (abfd, sec);
1024 if ((flags & SEC_LINK_ONCE) == 0)
1025 return;
1027 /* FIXME: When doing a relocatable link, we may have trouble
1028 copying relocations in other sections that refer to local symbols
1029 in the section being discarded. Those relocations will have to
1030 be converted somehow; as of this writing I'm not sure that any of
1031 the backends handle that correctly.
1033 It is tempting to instead not discard link once sections when
1034 doing a relocatable link (technically, they should be discarded
1035 whenever we are building constructors). However, that fails,
1036 because the linker winds up combining all the link once sections
1037 into a single large link once section, which defeats the purpose
1038 of having link once sections in the first place.
1040 Also, not merging link once sections in a relocatable link
1041 causes trouble for MIPS ELF, which relies on link once semantics
1042 to handle the .reginfo section correctly. */
1044 name = bfd_get_section_name (abfd, sec);
1046 already_linked_list =
1047 ((struct already_linked_hash_entry *)
1048 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
1050 for (l = already_linked_list->entry; l != NULL; l = l->next)
1052 if (sec->comdat == NULL
1053 || l->sec->comdat == NULL
1054 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
1056 /* The section has already been linked. See if we should
1057 issue a warning. */
1058 switch (flags & SEC_LINK_DUPLICATES)
1060 default:
1061 abort ();
1063 case SEC_LINK_DUPLICATES_DISCARD:
1064 break;
1066 case SEC_LINK_DUPLICATES_ONE_ONLY:
1067 if (sec->comdat == NULL)
1068 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
1069 abfd, name);
1070 else
1071 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
1072 abfd, name, sec->comdat->name);
1073 break;
1075 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
1076 /* FIXME: We should really dig out the contents of both
1077 sections and memcmp them. The COFF/PE spec says that
1078 the Microsoft linker does not implement this
1079 correctly, so I'm not going to bother doing it
1080 either. */
1081 /* Fall through. */
1082 case SEC_LINK_DUPLICATES_SAME_SIZE:
1083 if (bfd_section_size (abfd, sec)
1084 != bfd_section_size (l->sec->owner, l->sec))
1085 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
1086 abfd, name);
1087 break;
1090 /* Set the output_section field so that lang_add_section
1091 does not create a lang_input_section structure for this
1092 section. */
1093 sec->output_section = bfd_abs_section_ptr;
1095 if (flags & SEC_GROUP)
1096 bfd_discard_group (abfd, sec);
1098 return;
1102 /* This is the first section with this name. Record it. Allocate
1103 the memory from the same obstack as the hash table is kept in. */
1105 l = ((struct already_linked *)
1106 bfd_hash_allocate (&already_linked_table, sizeof *l));
1108 l->sec = sec;
1109 l->next = already_linked_list->entry;
1110 already_linked_list->entry = l;
1113 /* Support routines for the hash table used by section_already_linked,
1114 initialize the table, fill in an entry and remove the table. */
1116 static struct bfd_hash_entry *
1117 already_linked_newfunc (entry, table, string)
1118 struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
1119 struct bfd_hash_table *table;
1120 const char *string ATTRIBUTE_UNUSED;
1122 struct already_linked_hash_entry *ret =
1123 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1125 ret->entry = NULL;
1127 return (struct bfd_hash_entry *) ret;
1130 static void
1131 already_linked_table_init ()
1133 if (! bfd_hash_table_init_n (&already_linked_table,
1134 already_linked_newfunc,
1135 42))
1136 einfo (_("%P%F: Failed to create hash table\n"));
1139 static void
1140 already_linked_table_free ()
1142 bfd_hash_table_free (&already_linked_table);
1145 /* The wild routines.
1147 These expand statements like *(.text) and foo.o to a list of
1148 explicit actions, like foo.o(.text), bar.o(.text) and
1149 foo.o(.text, .data). */
1151 /* Return TRUE if the PATTERN argument is a wildcard pattern.
1152 Although backslashes are treated specially if a pattern contains
1153 wildcards, we do not consider the mere presence of a backslash to
1154 be enough to cause the pattern to be treated as a wildcard.
1155 That lets us handle DOS filenames more naturally. */
1157 static bfd_boolean
1158 wildcardp (pattern)
1159 const char *pattern;
1161 const char *s;
1163 for (s = pattern; *s != '\0'; ++s)
1164 if (*s == '?'
1165 || *s == '*'
1166 || *s == '[')
1167 return TRUE;
1168 return FALSE;
1171 /* Add SECTION to the output section OUTPUT. Do this by creating a
1172 lang_input_section statement which is placed at PTR. FILE is the
1173 input file which holds SECTION. */
1175 void
1176 lang_add_section (ptr, section, output, file)
1177 lang_statement_list_type *ptr;
1178 asection *section;
1179 lang_output_section_statement_type *output;
1180 lang_input_statement_type *file;
1182 flagword flags;
1183 bfd_boolean discard;
1185 flags = bfd_get_section_flags (section->owner, section);
1187 discard = FALSE;
1189 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1190 link. Discard debugging sections marked with SEC_EXCLUDE on a
1191 relocatable link too. */
1192 if ((flags & SEC_EXCLUDE) != 0
1193 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocateable))
1194 discard = TRUE;
1196 /* Discard input sections which are assigned to a section named
1197 DISCARD_SECTION_NAME. */
1198 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1199 discard = TRUE;
1201 /* Discard debugging sections if we are stripping debugging
1202 information. */
1203 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1204 && (flags & SEC_DEBUGGING) != 0)
1205 discard = TRUE;
1207 if (discard)
1209 if (section->output_section == NULL)
1211 /* This prevents future calls from assigning this section. */
1212 section->output_section = bfd_abs_section_ptr;
1214 return;
1217 if (section->output_section == NULL)
1219 bfd_boolean first;
1220 lang_input_section_type *new;
1221 flagword flags;
1223 if (output->bfd_section == NULL)
1224 init_os (output);
1226 first = ! output->bfd_section->linker_has_input;
1227 output->bfd_section->linker_has_input = 1;
1229 /* Add a section reference to the list. */
1230 new = new_stat (lang_input_section, ptr);
1232 new->section = section;
1233 new->ifile = file;
1234 section->output_section = output->bfd_section;
1236 flags = section->flags;
1238 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1239 to an output section, because we want to be able to include a
1240 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1241 section (I don't know why we want to do this, but we do).
1242 build_link_order in ldwrite.c handles this case by turning
1243 the embedded SEC_NEVER_LOAD section into a fill. */
1245 flags &= ~ SEC_NEVER_LOAD;
1247 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1248 already been processed. One reason to do this is that on pe
1249 format targets, .text$foo sections go into .text and it's odd
1250 to see .text with SEC_LINK_ONCE set. */
1252 if (! link_info.relocateable)
1253 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1255 /* If this is not the first input section, and the SEC_READONLY
1256 flag is not currently set, then don't set it just because the
1257 input section has it set. */
1259 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1260 flags &= ~ SEC_READONLY;
1262 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1263 if (! first
1264 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1265 != (flags & (SEC_MERGE | SEC_STRINGS))
1266 || ((flags & SEC_MERGE)
1267 && section->output_section->entsize != section->entsize)))
1269 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1270 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1273 /* For now make .tbss normal section. */
1274 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocateable)
1275 flags |= SEC_LOAD;
1277 section->output_section->flags |= flags;
1279 if (flags & SEC_MERGE)
1280 section->output_section->entsize = section->entsize;
1282 /* If SEC_READONLY is not set in the input section, then clear
1283 it from the output section. */
1284 if ((section->flags & SEC_READONLY) == 0)
1285 section->output_section->flags &= ~SEC_READONLY;
1287 switch (output->sectype)
1289 case normal_section:
1290 break;
1291 case dsect_section:
1292 case copy_section:
1293 case info_section:
1294 case overlay_section:
1295 output->bfd_section->flags &= ~SEC_ALLOC;
1296 break;
1297 case noload_section:
1298 output->bfd_section->flags &= ~SEC_LOAD;
1299 output->bfd_section->flags |= SEC_NEVER_LOAD;
1300 break;
1303 /* Copy over SEC_SMALL_DATA. */
1304 if (section->flags & SEC_SMALL_DATA)
1305 section->output_section->flags |= SEC_SMALL_DATA;
1307 if (section->alignment_power > output->bfd_section->alignment_power)
1308 output->bfd_section->alignment_power = section->alignment_power;
1310 /* If supplied an alignment, then force it. */
1311 if (output->section_alignment != -1)
1312 output->bfd_section->alignment_power = output->section_alignment;
1314 if (section->flags & SEC_BLOCK)
1316 section->output_section->flags |= SEC_BLOCK;
1317 /* FIXME: This value should really be obtained from the bfd... */
1318 output->block_value = 128;
1323 /* Handle wildcard sorting. This returns the lang_input_section which
1324 should follow the one we are going to create for SECTION and FILE,
1325 based on the sorting requirements of WILD. It returns NULL if the
1326 new section should just go at the end of the current list. */
1328 static lang_statement_union_type *
1329 wild_sort (wild, sec, file, section)
1330 lang_wild_statement_type *wild;
1331 struct wildcard_list *sec;
1332 lang_input_statement_type *file;
1333 asection *section;
1335 const char *section_name;
1336 lang_statement_union_type *l;
1338 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1339 return NULL;
1341 section_name = bfd_get_section_name (file->the_bfd, section);
1342 for (l = wild->children.head; l != NULL; l = l->header.next)
1344 lang_input_section_type *ls;
1346 if (l->header.type != lang_input_section_enum)
1347 continue;
1348 ls = &l->input_section;
1350 /* Sorting by filename takes precedence over sorting by section
1351 name. */
1353 if (wild->filenames_sorted)
1355 const char *fn, *ln;
1356 bfd_boolean fa, la;
1357 int i;
1359 /* The PE support for the .idata section as generated by
1360 dlltool assumes that files will be sorted by the name of
1361 the archive and then the name of the file within the
1362 archive. */
1364 if (file->the_bfd != NULL
1365 && bfd_my_archive (file->the_bfd) != NULL)
1367 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1368 fa = TRUE;
1370 else
1372 fn = file->filename;
1373 fa = FALSE;
1376 if (ls->ifile->the_bfd != NULL
1377 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1379 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1380 la = TRUE;
1382 else
1384 ln = ls->ifile->filename;
1385 la = FALSE;
1388 i = strcmp (fn, ln);
1389 if (i > 0)
1390 continue;
1391 else if (i < 0)
1392 break;
1394 if (fa || la)
1396 if (fa)
1397 fn = file->filename;
1398 if (la)
1399 ln = ls->ifile->filename;
1401 i = strcmp (fn, ln);
1402 if (i > 0)
1403 continue;
1404 else if (i < 0)
1405 break;
1409 /* Here either the files are not sorted by name, or we are
1410 looking at the sections for this file. */
1412 if (sec != NULL && sec->spec.sorted)
1414 if (strcmp (section_name,
1415 bfd_get_section_name (ls->ifile->the_bfd,
1416 ls->section))
1417 < 0)
1418 break;
1422 return l;
1425 /* Expand a wild statement for a particular FILE. SECTION may be
1426 NULL, in which case it is a wild card. */
1428 static void
1429 output_section_callback (ptr, sec, section, file, output)
1430 lang_wild_statement_type *ptr;
1431 struct wildcard_list *sec;
1432 asection *section;
1433 lang_input_statement_type *file;
1434 PTR output;
1436 lang_statement_union_type *before;
1438 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1439 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1440 return;
1442 /* If the wild pattern was marked KEEP, the member sections
1443 should be as well. */
1444 if (ptr->keep_sections)
1445 section->flags |= SEC_KEEP;
1447 before = wild_sort (ptr, sec, file, section);
1449 /* Here BEFORE points to the lang_input_section which
1450 should follow the one we are about to add. If BEFORE
1451 is NULL, then the section should just go at the end
1452 of the current list. */
1454 if (before == NULL)
1455 lang_add_section (&ptr->children, section,
1456 (lang_output_section_statement_type *) output,
1457 file);
1458 else
1460 lang_statement_list_type list;
1461 lang_statement_union_type **pp;
1463 lang_list_init (&list);
1464 lang_add_section (&list, section,
1465 (lang_output_section_statement_type *) output,
1466 file);
1468 /* If we are discarding the section, LIST.HEAD will
1469 be NULL. */
1470 if (list.head != NULL)
1472 ASSERT (list.head->header.next == NULL);
1474 for (pp = &ptr->children.head;
1475 *pp != before;
1476 pp = &(*pp)->header.next)
1477 ASSERT (*pp != NULL);
1479 list.head->header.next = *pp;
1480 *pp = list.head;
1485 /* This is passed a file name which must have been seen already and
1486 added to the statement tree. We will see if it has been opened
1487 already and had its symbols read. If not then we'll read it. */
1489 static lang_input_statement_type *
1490 lookup_name (name)
1491 const char *name;
1493 lang_input_statement_type *search;
1495 for (search = (lang_input_statement_type *) input_file_chain.head;
1496 search != (lang_input_statement_type *) NULL;
1497 search = (lang_input_statement_type *) search->next_real_file)
1499 if (search->filename == (char *) NULL && name == (char *) NULL)
1500 return search;
1501 if (search->filename != (char *) NULL
1502 && name != (char *) NULL
1503 && strcmp (search->filename, name) == 0)
1504 break;
1507 if (search == (lang_input_statement_type *) NULL)
1508 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1509 FALSE);
1511 /* If we have already added this file, or this file is not real
1512 (FIXME: can that ever actually happen?) or the name is NULL
1513 (FIXME: can that ever actually happen?) don't add this file. */
1514 if (search->loaded
1515 || ! search->real
1516 || search->filename == (const char *) NULL)
1517 return search;
1519 if (! load_symbols (search, (lang_statement_list_type *) NULL))
1520 return NULL;
1522 return search;
1525 /* Get the symbols for an input file. */
1527 static bfd_boolean
1528 load_symbols (entry, place)
1529 lang_input_statement_type *entry;
1530 lang_statement_list_type *place;
1532 char **matching;
1534 if (entry->loaded)
1535 return TRUE;
1537 ldfile_open_file (entry);
1539 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1540 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1542 bfd_error_type err;
1543 lang_statement_list_type *hold;
1544 bfd_boolean bad_load = TRUE;
1545 bfd_boolean save_ldlang_sysrooted_script;
1547 err = bfd_get_error ();
1549 /* See if the emulation has some special knowledge. */
1550 if (ldemul_unrecognized_file (entry))
1551 return TRUE;
1553 if (err == bfd_error_file_ambiguously_recognized)
1555 char **p;
1557 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1558 einfo (_("%B: matching formats:"), entry->the_bfd);
1559 for (p = matching; *p != NULL; p++)
1560 einfo (" %s", *p);
1561 einfo ("%F\n");
1563 else if (err != bfd_error_file_not_recognized
1564 || place == NULL)
1565 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1566 else
1567 bad_load = FALSE;
1569 bfd_close (entry->the_bfd);
1570 entry->the_bfd = NULL;
1572 /* Try to interpret the file as a linker script. */
1573 ldfile_open_command_file (entry->filename);
1575 hold = stat_ptr;
1576 stat_ptr = place;
1577 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1578 ldlang_sysrooted_script = entry->sysrooted;
1580 ldfile_assumed_script = TRUE;
1581 parser_input = input_script;
1582 yyparse ();
1583 ldfile_assumed_script = FALSE;
1585 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1586 stat_ptr = hold;
1588 return ! bad_load;
1591 if (ldemul_recognized_file (entry))
1592 return TRUE;
1594 /* We don't call ldlang_add_file for an archive. Instead, the
1595 add_symbols entry point will call ldlang_add_file, via the
1596 add_archive_element callback, for each element of the archive
1597 which is used. */
1598 switch (bfd_get_format (entry->the_bfd))
1600 default:
1601 break;
1603 case bfd_object:
1604 ldlang_add_file (entry);
1605 if (trace_files || trace_file_tries)
1606 info_msg ("%I\n", entry);
1607 break;
1609 case bfd_archive:
1610 if (entry->whole_archive)
1612 bfd *member = NULL;
1613 bfd_boolean loaded = TRUE;
1615 for (;;)
1617 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1619 if (member == NULL)
1620 break;
1622 if (! bfd_check_format (member, bfd_object))
1624 einfo (_("%F%B: member %B in archive is not an object\n"),
1625 entry->the_bfd, member);
1626 loaded = FALSE;
1629 if (! ((*link_info.callbacks->add_archive_element)
1630 (&link_info, member, "--whole-archive")))
1631 abort ();
1633 if (! bfd_link_add_symbols (member, &link_info))
1635 einfo (_("%F%B: could not read symbols: %E\n"), member);
1636 loaded = FALSE;
1640 entry->loaded = loaded;
1641 return loaded;
1643 break;
1646 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1647 entry->loaded = TRUE;
1648 else
1649 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1651 return entry->loaded;
1654 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1655 may be NULL, indicating that it is a wildcard. Separate
1656 lang_input_section statements are created for each part of the
1657 expansion; they are added after the wild statement S. OUTPUT is
1658 the output section. */
1660 static void
1661 wild (s, target, output)
1662 lang_wild_statement_type *s;
1663 const char *target ATTRIBUTE_UNUSED;
1664 lang_output_section_statement_type *output;
1666 struct wildcard_list *sec;
1668 walk_wild (s, output_section_callback, (PTR) output);
1670 for (sec = s->section_list; sec != NULL; sec = sec->next)
1672 if (default_common_section != NULL)
1673 break;
1674 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1676 /* Remember the section that common is going to in case we
1677 later get something which doesn't know where to put it. */
1678 default_common_section = output;
1683 /* Return TRUE iff target is the sought target. */
1685 static int
1686 get_target (target, data)
1687 const bfd_target *target;
1688 PTR data;
1690 const char *sought = (const char *) data;
1692 return strcmp (target->name, sought) == 0;
1695 /* Like strcpy() but convert to lower case as well. */
1697 static void
1698 stricpy (dest, src)
1699 char *dest;
1700 char *src;
1702 char c;
1704 while ((c = *src++) != 0)
1705 *dest++ = TOLOWER (c);
1707 *dest = 0;
1710 /* Remove the first occurrence of needle (if any) in haystack
1711 from haystack. */
1713 static void
1714 strcut (haystack, needle)
1715 char *haystack;
1716 char *needle;
1718 haystack = strstr (haystack, needle);
1720 if (haystack)
1722 char *src;
1724 for (src = haystack + strlen (needle); *src;)
1725 *haystack++ = *src++;
1727 *haystack = 0;
1731 /* Compare two target format name strings.
1732 Return a value indicating how "similar" they are. */
1734 static int
1735 name_compare (first, second)
1736 char *first;
1737 char *second;
1739 char *copy1;
1740 char *copy2;
1741 int result;
1743 copy1 = xmalloc (strlen (first) + 1);
1744 copy2 = xmalloc (strlen (second) + 1);
1746 /* Convert the names to lower case. */
1747 stricpy (copy1, first);
1748 stricpy (copy2, second);
1750 /* Remove and endian strings from the name. */
1751 strcut (copy1, "big");
1752 strcut (copy1, "little");
1753 strcut (copy2, "big");
1754 strcut (copy2, "little");
1756 /* Return a value based on how many characters match,
1757 starting from the beginning. If both strings are
1758 the same then return 10 * their length. */
1759 for (result = 0; copy1[result] == copy2[result]; result++)
1760 if (copy1[result] == 0)
1762 result *= 10;
1763 break;
1766 free (copy1);
1767 free (copy2);
1769 return result;
1772 /* Set by closest_target_match() below. */
1773 static const bfd_target *winner;
1775 /* Scan all the valid bfd targets looking for one that has the endianness
1776 requirement that was specified on the command line, and is the nearest
1777 match to the original output target. */
1779 static int
1780 closest_target_match (target, data)
1781 const bfd_target *target;
1782 PTR data;
1784 const bfd_target *original = (const bfd_target *) data;
1786 if (command_line.endian == ENDIAN_BIG
1787 && target->byteorder != BFD_ENDIAN_BIG)
1788 return 0;
1790 if (command_line.endian == ENDIAN_LITTLE
1791 && target->byteorder != BFD_ENDIAN_LITTLE)
1792 return 0;
1794 /* Must be the same flavour. */
1795 if (target->flavour != original->flavour)
1796 return 0;
1798 /* If we have not found a potential winner yet, then record this one. */
1799 if (winner == NULL)
1801 winner = target;
1802 return 0;
1805 /* Oh dear, we now have two potential candidates for a successful match.
1806 Compare their names and choose the better one. */
1807 if (name_compare (target->name, original->name)
1808 > name_compare (winner->name, original->name))
1809 winner = target;
1811 /* Keep on searching until wqe have checked them all. */
1812 return 0;
1815 /* Return the BFD target format of the first input file. */
1817 static char *
1818 get_first_input_target ()
1820 char *target = NULL;
1822 LANG_FOR_EACH_INPUT_STATEMENT (s)
1824 if (s->header.type == lang_input_statement_enum
1825 && s->real)
1827 ldfile_open_file (s);
1829 if (s->the_bfd != NULL
1830 && bfd_check_format (s->the_bfd, bfd_object))
1832 target = bfd_get_target (s->the_bfd);
1834 if (target != NULL)
1835 break;
1840 return target;
1843 const char *
1844 lang_get_output_target ()
1846 const char *target;
1848 /* Has the user told us which output format to use? */
1849 if (output_target != (char *) NULL)
1850 return output_target;
1852 /* No - has the current target been set to something other than
1853 the default? */
1854 if (current_target != default_target)
1855 return current_target;
1857 /* No - can we determine the format of the first input file? */
1858 target = get_first_input_target ();
1859 if (target != NULL)
1860 return target;
1862 /* Failed - use the default output target. */
1863 return default_target;
1866 /* Open the output file. */
1868 static bfd *
1869 open_output (name)
1870 const char *name;
1872 bfd *output;
1874 output_target = lang_get_output_target ();
1876 /* Has the user requested a particular endianness on the command
1877 line? */
1878 if (command_line.endian != ENDIAN_UNSET)
1880 const bfd_target *target;
1881 enum bfd_endian desired_endian;
1883 /* Get the chosen target. */
1884 target = bfd_search_for_target (get_target, (PTR) output_target);
1886 /* If the target is not supported, we cannot do anything. */
1887 if (target != NULL)
1889 if (command_line.endian == ENDIAN_BIG)
1890 desired_endian = BFD_ENDIAN_BIG;
1891 else
1892 desired_endian = BFD_ENDIAN_LITTLE;
1894 /* See if the target has the wrong endianness. This should
1895 not happen if the linker script has provided big and
1896 little endian alternatives, but some scrips don't do
1897 this. */
1898 if (target->byteorder != desired_endian)
1900 /* If it does, then see if the target provides
1901 an alternative with the correct endianness. */
1902 if (target->alternative_target != NULL
1903 && (target->alternative_target->byteorder == desired_endian))
1904 output_target = target->alternative_target->name;
1905 else
1907 /* Try to find a target as similar as possible to
1908 the default target, but which has the desired
1909 endian characteristic. */
1910 (void) bfd_search_for_target (closest_target_match,
1911 (PTR) target);
1913 /* Oh dear - we could not find any targets that
1914 satisfy our requirements. */
1915 if (winner == NULL)
1916 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1917 else
1918 output_target = winner->name;
1924 output = bfd_openw (name, output_target);
1926 if (output == (bfd *) NULL)
1928 if (bfd_get_error () == bfd_error_invalid_target)
1929 einfo (_("%P%F: target %s not found\n"), output_target);
1931 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1934 delete_output_file_on_failure = TRUE;
1936 #if 0
1937 output->flags |= D_PAGED;
1938 #endif
1940 if (! bfd_set_format (output, bfd_object))
1941 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1942 if (! bfd_set_arch_mach (output,
1943 ldfile_output_architecture,
1944 ldfile_output_machine))
1945 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1947 link_info.hash = bfd_link_hash_table_create (output);
1948 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1949 einfo (_("%P%F: can not create link hash table: %E\n"));
1951 bfd_set_gp_size (output, g_switch_value);
1952 return output;
1955 static void
1956 ldlang_open_output (statement)
1957 lang_statement_union_type *statement;
1959 switch (statement->header.type)
1961 case lang_output_statement_enum:
1962 ASSERT (output_bfd == (bfd *) NULL);
1963 output_bfd = open_output (statement->output_statement.name);
1964 ldemul_set_output_arch ();
1965 if (config.magic_demand_paged && !link_info.relocateable)
1966 output_bfd->flags |= D_PAGED;
1967 else
1968 output_bfd->flags &= ~D_PAGED;
1969 if (config.text_read_only)
1970 output_bfd->flags |= WP_TEXT;
1971 else
1972 output_bfd->flags &= ~WP_TEXT;
1973 if (link_info.traditional_format)
1974 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1975 else
1976 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1977 break;
1979 case lang_target_statement_enum:
1980 current_target = statement->target_statement.target;
1981 break;
1982 default:
1983 break;
1987 /* Open all the input files. */
1989 static void
1990 open_input_bfds (s, force)
1991 lang_statement_union_type *s;
1992 bfd_boolean force;
1994 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
1996 switch (s->header.type)
1998 case lang_constructors_statement_enum:
1999 open_input_bfds (constructor_list.head, force);
2000 break;
2001 case lang_output_section_statement_enum:
2002 open_input_bfds (s->output_section_statement.children.head, force);
2003 break;
2004 case lang_wild_statement_enum:
2005 /* Maybe we should load the file's symbols. */
2006 if (s->wild_statement.filename
2007 && ! wildcardp (s->wild_statement.filename))
2008 (void) lookup_name (s->wild_statement.filename);
2009 open_input_bfds (s->wild_statement.children.head, force);
2010 break;
2011 case lang_group_statement_enum:
2013 struct bfd_link_hash_entry *undefs;
2015 /* We must continually search the entries in the group
2016 until no new symbols are added to the list of undefined
2017 symbols. */
2021 undefs = link_info.hash->undefs_tail;
2022 open_input_bfds (s->group_statement.children.head, TRUE);
2024 while (undefs != link_info.hash->undefs_tail);
2026 break;
2027 case lang_target_statement_enum:
2028 current_target = s->target_statement.target;
2029 break;
2030 case lang_input_statement_enum:
2031 if (s->input_statement.real)
2033 lang_statement_list_type add;
2035 s->input_statement.target = current_target;
2037 /* If we are being called from within a group, and this
2038 is an archive which has already been searched, then
2039 force it to be researched unless the whole archive
2040 has been loaded already. */
2041 if (force
2042 && !s->input_statement.whole_archive
2043 && s->input_statement.loaded
2044 && bfd_check_format (s->input_statement.the_bfd,
2045 bfd_archive))
2046 s->input_statement.loaded = FALSE;
2048 lang_list_init (&add);
2050 if (! load_symbols (&s->input_statement, &add))
2051 config.make_executable = FALSE;
2053 if (add.head != NULL)
2055 *add.tail = s->header.next;
2056 s->header.next = add.head;
2059 break;
2060 default:
2061 break;
2066 /* If there are [COMMONS] statements, put a wild one into the bss
2067 section. */
2069 static void
2070 lang_reasonable_defaults ()
2072 #if 0
2073 lang_output_section_statement_lookup (".text");
2074 lang_output_section_statement_lookup (".data");
2076 default_common_section = lang_output_section_statement_lookup (".bss");
2078 if (!placed_commons)
2080 lang_wild_statement_type *new =
2081 new_stat (lang_wild_statement,
2082 &default_common_section->children);
2084 new->section_name = "COMMON";
2085 new->filename = (char *) NULL;
2086 lang_list_init (&new->children);
2088 #endif
2091 /* Add the supplied name to the symbol table as an undefined reference.
2092 This is a two step process as the symbol table doesn't even exist at
2093 the time the ld command line is processed. First we put the name
2094 on a list, then, once the output file has been opened, transfer the
2095 name to the symbol table. */
2097 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2099 #define ldlang_undef_chain_list_head entry_symbol.next
2101 void
2102 ldlang_add_undef (name)
2103 const char *const name;
2105 ldlang_undef_chain_list_type *new =
2106 ((ldlang_undef_chain_list_type *)
2107 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
2109 new->next = ldlang_undef_chain_list_head;
2110 ldlang_undef_chain_list_head = new;
2112 new->name = xstrdup (name);
2114 if (output_bfd != NULL)
2115 insert_undefined (new->name);
2118 /* Insert NAME as undefined in the symbol table. */
2120 static void
2121 insert_undefined (name)
2122 const char *name;
2124 struct bfd_link_hash_entry *h;
2126 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2127 if (h == (struct bfd_link_hash_entry *) NULL)
2128 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2129 if (h->type == bfd_link_hash_new)
2131 h->type = bfd_link_hash_undefined;
2132 h->u.undef.abfd = NULL;
2133 bfd_link_add_undef (link_info.hash, h);
2137 /* Run through the list of undefineds created above and place them
2138 into the linker hash table as undefined symbols belonging to the
2139 script file. */
2141 static void
2142 lang_place_undefineds ()
2144 ldlang_undef_chain_list_type *ptr;
2146 for (ptr = ldlang_undef_chain_list_head;
2147 ptr != (ldlang_undef_chain_list_type *) NULL;
2148 ptr = ptr->next)
2150 insert_undefined (ptr->name);
2154 /* Open input files and attach to output sections. */
2156 static void
2157 map_input_to_output_sections (s, target, output_section_statement)
2158 lang_statement_union_type *s;
2159 const char *target;
2160 lang_output_section_statement_type *output_section_statement;
2162 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2164 switch (s->header.type)
2166 case lang_wild_statement_enum:
2167 wild (&s->wild_statement, target, output_section_statement);
2168 break;
2169 case lang_constructors_statement_enum:
2170 map_input_to_output_sections (constructor_list.head,
2171 target,
2172 output_section_statement);
2173 break;
2174 case lang_output_section_statement_enum:
2175 map_input_to_output_sections (s->output_section_statement.children.head,
2176 target,
2177 &s->output_section_statement);
2178 break;
2179 case lang_output_statement_enum:
2180 break;
2181 case lang_target_statement_enum:
2182 target = s->target_statement.target;
2183 break;
2184 case lang_group_statement_enum:
2185 map_input_to_output_sections (s->group_statement.children.head,
2186 target,
2187 output_section_statement);
2188 break;
2189 case lang_fill_statement_enum:
2190 case lang_input_section_enum:
2191 case lang_object_symbols_statement_enum:
2192 case lang_data_statement_enum:
2193 case lang_reloc_statement_enum:
2194 case lang_padding_statement_enum:
2195 case lang_input_statement_enum:
2196 if (output_section_statement != NULL
2197 && output_section_statement->bfd_section == NULL)
2198 init_os (output_section_statement);
2199 break;
2200 case lang_assignment_statement_enum:
2201 if (output_section_statement != NULL
2202 && output_section_statement->bfd_section == NULL)
2203 init_os (output_section_statement);
2205 /* Make sure that any sections mentioned in the assignment
2206 are initialized. */
2207 exp_init_os (s->assignment_statement.exp);
2208 break;
2209 case lang_afile_asection_pair_statement_enum:
2210 FAIL ();
2211 break;
2212 case lang_address_statement_enum:
2213 /* Mark the specified section with the supplied address. */
2215 lang_output_section_statement_type *os =
2216 lang_output_section_statement_lookup
2217 (s->address_statement.section_name);
2219 if (os->bfd_section == NULL)
2220 init_os (os);
2221 os->addr_tree = s->address_statement.address;
2223 break;
2228 /* An output section might have been removed after its statement was
2229 added. For example, ldemul_before_allocation can remove dynamic
2230 sections if they turn out to be not needed. Clean them up here. */
2232 static void
2233 strip_excluded_output_sections ()
2235 lang_statement_union_type *u;
2237 for (u = lang_output_section_statement.head;
2238 u != NULL;
2239 u = u->output_section_statement.next)
2241 lang_output_section_statement_type *os;
2242 asection *s;
2244 os = &u->output_section_statement;
2245 s = os->bfd_section;
2246 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2248 asection **p;
2250 os->bfd_section = NULL;
2252 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2253 if (*p == s)
2255 bfd_section_list_remove (output_bfd, p);
2256 output_bfd->section_count--;
2257 break;
2263 static void
2264 print_output_section_statement (output_section_statement)
2265 lang_output_section_statement_type *output_section_statement;
2267 asection *section = output_section_statement->bfd_section;
2268 int len;
2270 if (output_section_statement != abs_output_section)
2272 minfo ("\n%s", output_section_statement->name);
2274 if (section != NULL)
2276 print_dot = section->vma;
2278 len = strlen (output_section_statement->name);
2279 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2281 print_nl ();
2282 len = 0;
2284 while (len < SECTION_NAME_MAP_LENGTH)
2286 print_space ();
2287 ++len;
2290 minfo ("0x%V %W", section->vma, section->_raw_size);
2292 if (output_section_statement->load_base != NULL)
2294 bfd_vma addr;
2296 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2297 "load base", lang_final_phase_enum);
2298 minfo (_(" load address 0x%V"), addr);
2302 print_nl ();
2305 print_statement_list (output_section_statement->children.head,
2306 output_section_statement);
2309 static void
2310 print_assignment (assignment, output_section)
2311 lang_assignment_statement_type *assignment;
2312 lang_output_section_statement_type *output_section;
2314 int i;
2315 etree_value_type result;
2317 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2318 print_space ();
2320 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2321 lang_final_phase_enum, print_dot, &print_dot);
2322 if (result.valid_p)
2324 const char *dst;
2325 bfd_vma value;
2327 value = result.value + result.section->bfd_section->vma;
2328 dst = assignment->exp->assign.dst;
2330 minfo ("0x%V", value);
2331 if (dst[0] == '.' && dst[1] == 0)
2332 print_dot = value;
2334 else
2336 minfo ("*undef* ");
2337 #ifdef BFD64
2338 minfo (" ");
2339 #endif
2342 minfo (" ");
2344 exp_print_tree (assignment->exp);
2346 print_nl ();
2349 static void
2350 print_input_statement (statm)
2351 lang_input_statement_type *statm;
2353 if (statm->filename != (char *) NULL)
2355 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2359 /* Print all symbols defined in a particular section. This is called
2360 via bfd_link_hash_traverse. */
2362 static bfd_boolean
2363 print_one_symbol (hash_entry, ptr)
2364 struct bfd_link_hash_entry *hash_entry;
2365 PTR ptr;
2367 asection *sec = (asection *) ptr;
2369 if ((hash_entry->type == bfd_link_hash_defined
2370 || hash_entry->type == bfd_link_hash_defweak)
2371 && sec == hash_entry->u.def.section)
2373 int i;
2375 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2376 print_space ();
2377 minfo ("0x%V ",
2378 (hash_entry->u.def.value
2379 + hash_entry->u.def.section->output_offset
2380 + hash_entry->u.def.section->output_section->vma));
2382 minfo (" %T\n", hash_entry->root.string);
2385 return TRUE;
2388 /* Print information about an input section to the map file. */
2390 static void
2391 print_input_section (in)
2392 lang_input_section_type *in;
2394 asection *i = in->section;
2395 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2396 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2397 ldfile_output_machine);
2398 if (size != 0)
2400 print_space ();
2402 minfo ("%s", i->name);
2404 if (i->output_section != NULL)
2406 int len;
2408 len = 1 + strlen (i->name);
2409 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2411 print_nl ();
2412 len = 0;
2414 while (len < SECTION_NAME_MAP_LENGTH)
2416 print_space ();
2417 ++len;
2420 minfo ("0x%V %W %B\n",
2421 i->output_section->vma + i->output_offset, size / opb,
2422 i->owner);
2424 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2426 len = SECTION_NAME_MAP_LENGTH + 3;
2427 #ifdef BFD64
2428 len += 16;
2429 #else
2430 len += 8;
2431 #endif
2432 while (len > 0)
2434 print_space ();
2435 --len;
2438 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2441 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
2443 print_dot = i->output_section->vma + i->output_offset + size / opb;
2448 static void
2449 print_fill_statement (fill)
2450 lang_fill_statement_type *fill;
2452 size_t size;
2453 unsigned char *p;
2454 fputs (" FILL mask 0x", config.map_file);
2455 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2456 fprintf (config.map_file, "%02x", *p);
2457 fputs ("\n", config.map_file);
2460 static void
2461 print_data_statement (data)
2462 lang_data_statement_type *data;
2464 int i;
2465 bfd_vma addr;
2466 bfd_size_type size;
2467 const char *name;
2468 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2469 ldfile_output_machine);
2471 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2472 print_space ();
2474 addr = data->output_vma;
2475 if (data->output_section != NULL)
2476 addr += data->output_section->vma;
2478 switch (data->type)
2480 default:
2481 abort ();
2482 case BYTE:
2483 size = BYTE_SIZE;
2484 name = "BYTE";
2485 break;
2486 case SHORT:
2487 size = SHORT_SIZE;
2488 name = "SHORT";
2489 break;
2490 case LONG:
2491 size = LONG_SIZE;
2492 name = "LONG";
2493 break;
2494 case QUAD:
2495 size = QUAD_SIZE;
2496 name = "QUAD";
2497 break;
2498 case SQUAD:
2499 size = QUAD_SIZE;
2500 name = "SQUAD";
2501 break;
2504 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2506 if (data->exp->type.node_class != etree_value)
2508 print_space ();
2509 exp_print_tree (data->exp);
2512 print_nl ();
2514 print_dot = addr + size / opb;
2518 /* Print an address statement. These are generated by options like
2519 -Ttext. */
2521 static void
2522 print_address_statement (address)
2523 lang_address_statement_type *address;
2525 minfo (_("Address of section %s set to "), address->section_name);
2526 exp_print_tree (address->address);
2527 print_nl ();
2530 /* Print a reloc statement. */
2532 static void
2533 print_reloc_statement (reloc)
2534 lang_reloc_statement_type *reloc;
2536 int i;
2537 bfd_vma addr;
2538 bfd_size_type size;
2539 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2540 ldfile_output_machine);
2542 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2543 print_space ();
2545 addr = reloc->output_vma;
2546 if (reloc->output_section != NULL)
2547 addr += reloc->output_section->vma;
2549 size = bfd_get_reloc_size (reloc->howto);
2551 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2553 if (reloc->name != NULL)
2554 minfo ("%s+", reloc->name);
2555 else
2556 minfo ("%s+", reloc->section->name);
2558 exp_print_tree (reloc->addend_exp);
2560 print_nl ();
2562 print_dot = addr + size / opb;
2565 static void
2566 print_padding_statement (s)
2567 lang_padding_statement_type *s;
2569 int len;
2570 bfd_vma addr;
2571 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2572 ldfile_output_machine);
2574 minfo (" *fill*");
2576 len = sizeof " *fill*" - 1;
2577 while (len < SECTION_NAME_MAP_LENGTH)
2579 print_space ();
2580 ++len;
2583 addr = s->output_offset;
2584 if (s->output_section != NULL)
2585 addr += s->output_section->vma;
2586 minfo ("0x%V %W ", addr, s->size);
2588 if (s->fill->size != 0)
2590 size_t size;
2591 unsigned char *p;
2592 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2593 fprintf (config.map_file, "%02x", *p);
2596 print_nl ();
2598 print_dot = addr + s->size / opb;
2601 static void
2602 print_wild_statement (w, os)
2603 lang_wild_statement_type *w;
2604 lang_output_section_statement_type *os;
2606 struct wildcard_list *sec;
2608 print_space ();
2610 if (w->filenames_sorted)
2611 minfo ("SORT(");
2612 if (w->filename != NULL)
2613 minfo ("%s", w->filename);
2614 else
2615 minfo ("*");
2616 if (w->filenames_sorted)
2617 minfo (")");
2619 minfo ("(");
2620 for (sec = w->section_list; sec; sec = sec->next)
2622 if (sec->spec.sorted)
2623 minfo ("SORT(");
2624 if (sec->spec.exclude_name_list != NULL)
2626 name_list *tmp;
2627 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2628 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2629 minfo (" %s", tmp->name);
2630 minfo (") ");
2632 if (sec->spec.name != NULL)
2633 minfo ("%s", sec->spec.name);
2634 else
2635 minfo ("*");
2636 if (sec->spec.sorted)
2637 minfo (")");
2638 if (sec->next)
2639 minfo (" ");
2641 minfo (")");
2643 print_nl ();
2645 print_statement_list (w->children.head, os);
2648 /* Print a group statement. */
2650 static void
2651 print_group (s, os)
2652 lang_group_statement_type *s;
2653 lang_output_section_statement_type *os;
2655 fprintf (config.map_file, "START GROUP\n");
2656 print_statement_list (s->children.head, os);
2657 fprintf (config.map_file, "END GROUP\n");
2660 /* Print the list of statements in S.
2661 This can be called for any statement type. */
2663 static void
2664 print_statement_list (s, os)
2665 lang_statement_union_type *s;
2666 lang_output_section_statement_type *os;
2668 while (s != NULL)
2670 print_statement (s, os);
2671 s = s->header.next;
2675 /* Print the first statement in statement list S.
2676 This can be called for any statement type. */
2678 static void
2679 print_statement (s, os)
2680 lang_statement_union_type *s;
2681 lang_output_section_statement_type *os;
2683 switch (s->header.type)
2685 default:
2686 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2687 FAIL ();
2688 break;
2689 case lang_constructors_statement_enum:
2690 if (constructor_list.head != NULL)
2692 if (constructors_sorted)
2693 minfo (" SORT (CONSTRUCTORS)\n");
2694 else
2695 minfo (" CONSTRUCTORS\n");
2696 print_statement_list (constructor_list.head, os);
2698 break;
2699 case lang_wild_statement_enum:
2700 print_wild_statement (&s->wild_statement, os);
2701 break;
2702 case lang_address_statement_enum:
2703 print_address_statement (&s->address_statement);
2704 break;
2705 case lang_object_symbols_statement_enum:
2706 minfo (" CREATE_OBJECT_SYMBOLS\n");
2707 break;
2708 case lang_fill_statement_enum:
2709 print_fill_statement (&s->fill_statement);
2710 break;
2711 case lang_data_statement_enum:
2712 print_data_statement (&s->data_statement);
2713 break;
2714 case lang_reloc_statement_enum:
2715 print_reloc_statement (&s->reloc_statement);
2716 break;
2717 case lang_input_section_enum:
2718 print_input_section (&s->input_section);
2719 break;
2720 case lang_padding_statement_enum:
2721 print_padding_statement (&s->padding_statement);
2722 break;
2723 case lang_output_section_statement_enum:
2724 print_output_section_statement (&s->output_section_statement);
2725 break;
2726 case lang_assignment_statement_enum:
2727 print_assignment (&s->assignment_statement, os);
2728 break;
2729 case lang_target_statement_enum:
2730 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2731 break;
2732 case lang_output_statement_enum:
2733 minfo ("OUTPUT(%s", s->output_statement.name);
2734 if (output_target != NULL)
2735 minfo (" %s", output_target);
2736 minfo (")\n");
2737 break;
2738 case lang_input_statement_enum:
2739 print_input_statement (&s->input_statement);
2740 break;
2741 case lang_group_statement_enum:
2742 print_group (&s->group_statement, os);
2743 break;
2744 case lang_afile_asection_pair_statement_enum:
2745 FAIL ();
2746 break;
2750 static void
2751 print_statements ()
2753 print_statement_list (statement_list.head, abs_output_section);
2756 /* Print the first N statements in statement list S to STDERR.
2757 If N == 0, nothing is printed.
2758 If N < 0, the entire list is printed.
2759 Intended to be called from GDB. */
2761 void
2762 dprint_statement (s, n)
2763 lang_statement_union_type *s;
2764 int n;
2766 FILE *map_save = config.map_file;
2768 config.map_file = stderr;
2770 if (n < 0)
2771 print_statement_list (s, abs_output_section);
2772 else
2774 while (s && --n >= 0)
2776 print_statement (s, abs_output_section);
2777 s = s->header.next;
2781 config.map_file = map_save;
2784 static void
2785 insert_pad (ptr, fill, alignment_needed, output_section, dot)
2786 lang_statement_union_type **ptr;
2787 fill_type *fill;
2788 unsigned int alignment_needed;
2789 asection *output_section;
2790 bfd_vma dot;
2792 static fill_type zero_fill = { 1, { 0 } };
2793 lang_statement_union_type *pad;
2795 pad = ((lang_statement_union_type *)
2796 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2797 if (ptr != &statement_list.head
2798 && pad->header.type == lang_padding_statement_enum
2799 && pad->padding_statement.output_section == output_section)
2801 /* Use the existing pad statement. The above test on output
2802 section is probably redundant, but it doesn't hurt to check. */
2804 else
2806 /* Make a new padding statement, linked into existing chain. */
2807 pad = ((lang_statement_union_type *)
2808 stat_alloc (sizeof (lang_padding_statement_type)));
2809 pad->header.next = *ptr;
2810 *ptr = pad;
2811 pad->header.type = lang_padding_statement_enum;
2812 pad->padding_statement.output_section = output_section;
2813 if (fill == (fill_type *) 0)
2814 fill = &zero_fill;
2815 pad->padding_statement.fill = fill;
2817 pad->padding_statement.output_offset = dot - output_section->vma;
2818 pad->padding_statement.size = alignment_needed;
2819 output_section->_raw_size += alignment_needed;
2822 /* Work out how much this section will move the dot point. */
2824 static bfd_vma
2825 size_input_section (this_ptr, output_section_statement, fill, dot)
2826 lang_statement_union_type **this_ptr;
2827 lang_output_section_statement_type *output_section_statement;
2828 fill_type *fill;
2829 bfd_vma dot;
2831 lang_input_section_type *is = &((*this_ptr)->input_section);
2832 asection *i = is->section;
2834 if (!is->ifile->just_syms_flag)
2836 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2837 ldfile_output_machine);
2838 unsigned int alignment_needed;
2839 asection *o;
2841 /* Align this section first to the input sections requirement,
2842 then to the output section's requirement. If this alignment
2843 is greater than any seen before, then record it too. Perform
2844 the alignment by inserting a magic 'padding' statement. */
2846 if (output_section_statement->subsection_alignment != -1)
2847 i->alignment_power = output_section_statement->subsection_alignment;
2849 o = output_section_statement->bfd_section;
2850 if (o->alignment_power < i->alignment_power)
2851 o->alignment_power = i->alignment_power;
2853 alignment_needed = align_power (dot, i->alignment_power) - dot;
2855 if (alignment_needed != 0)
2857 insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2858 dot += alignment_needed;
2861 /* Remember where in the output section this input section goes. */
2863 i->output_offset = dot - o->vma;
2865 /* Mark how big the output section must be to contain this now. */
2866 if (i->_cooked_size != 0)
2867 dot += i->_cooked_size / opb;
2868 else
2869 dot += i->_raw_size / opb;
2870 o->_raw_size = (dot - o->vma) * opb;
2872 else
2874 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2877 return dot;
2880 #define IGNORE_SECTION(bfd, s) \
2881 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2882 != (SEC_ALLOC | SEC_LOAD)) \
2883 || bfd_section_size (bfd, s) == 0)
2885 /* Check to see if any allocated sections overlap with other allocated
2886 sections. This can happen when the linker script specifically specifies
2887 the output section addresses of the two sections. */
2889 static void
2890 lang_check_section_addresses ()
2892 asection *s;
2893 unsigned opb = bfd_octets_per_byte (output_bfd);
2895 /* Scan all sections in the output list. */
2896 for (s = output_bfd->sections; s != NULL; s = s->next)
2898 asection *os;
2900 /* Ignore sections which are not loaded or which have no contents. */
2901 if (IGNORE_SECTION (output_bfd, s))
2902 continue;
2904 /* Once we reach section 's' stop our seach. This prevents two
2905 warning messages from being produced, one for 'section A overlaps
2906 section B' and one for 'section B overlaps section A'. */
2907 for (os = output_bfd->sections; os != s; os = os->next)
2909 bfd_vma s_start;
2910 bfd_vma s_end;
2911 bfd_vma os_start;
2912 bfd_vma os_end;
2914 /* Only consider loadable sections with real contents. */
2915 if (IGNORE_SECTION (output_bfd, os))
2916 continue;
2918 /* We must check the sections' LMA addresses not their
2919 VMA addresses because overlay sections can have
2920 overlapping VMAs but they must have distinct LMAs. */
2921 s_start = bfd_section_lma (output_bfd, s);
2922 os_start = bfd_section_lma (output_bfd, os);
2923 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2924 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2926 /* Look for an overlap. */
2927 if ((s_end < os_start) || (s_start > os_end))
2928 continue;
2930 einfo (
2931 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2932 s->name, s_start, s_end, os->name, os_start, os_end);
2934 /* Once we have found one overlap for this section,
2935 stop looking for others. */
2936 break;
2941 /* Make sure the new address is within the region. We explicitly permit the
2942 current address to be at the exact end of the region when the address is
2943 non-zero, in case the region is at the end of addressable memory and the
2944 calculation wraps around. */
2946 static void
2947 os_region_check (os, region, tree, base)
2948 lang_output_section_statement_type *os;
2949 struct memory_region_struct *region;
2950 etree_type *tree;
2951 bfd_vma base;
2953 if ((region->current < region->origin
2954 || (region->current - region->origin > region->length))
2955 && ((region->current != region->origin + region->length)
2956 || base == 0))
2958 if (tree != (etree_type *) NULL)
2960 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2961 region->current,
2962 os->bfd_section->owner,
2963 os->bfd_section->name,
2964 region->name);
2966 else
2968 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2969 region->name,
2970 os->bfd_section->owner,
2971 os->bfd_section->name);
2973 /* Reset the region pointer. */
2974 region->current = region->origin;
2978 /* Set the sizes for all the output sections. */
2980 static bfd_vma
2981 lang_size_sections_1 (s, output_section_statement, prev, fill, dot, relax,
2982 check_regions)
2983 lang_statement_union_type *s;
2984 lang_output_section_statement_type *output_section_statement;
2985 lang_statement_union_type **prev;
2986 fill_type *fill;
2987 bfd_vma dot;
2988 bfd_boolean *relax;
2989 bfd_boolean check_regions;
2991 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2992 ldfile_output_machine);
2994 /* Size up the sections from their constituent parts. */
2995 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2997 switch (s->header.type)
2999 case lang_output_section_statement_enum:
3001 bfd_vma after;
3002 lang_output_section_statement_type *os;
3004 os = &s->output_section_statement;
3005 if (os->bfd_section == NULL)
3006 /* This section was never actually created. */
3007 break;
3009 /* If this is a COFF shared library section, use the size and
3010 address from the input section. FIXME: This is COFF
3011 specific; it would be cleaner if there were some other way
3012 to do this, but nothing simple comes to mind. */
3013 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3015 asection *input;
3017 if (os->children.head == NULL
3018 || os->children.head->header.next != NULL
3019 || os->children.head->header.type != lang_input_section_enum)
3020 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
3021 os->name);
3023 input = os->children.head->input_section.section;
3024 bfd_set_section_vma (os->bfd_section->owner,
3025 os->bfd_section,
3026 bfd_section_vma (input->owner, input));
3027 os->bfd_section->_raw_size = input->_raw_size;
3028 break;
3031 if (bfd_is_abs_section (os->bfd_section))
3033 /* No matter what happens, an abs section starts at zero. */
3034 ASSERT (os->bfd_section->vma == 0);
3036 else
3038 if (os->addr_tree == (etree_type *) NULL)
3040 /* No address specified for this section, get one
3041 from the region specification. */
3042 if (os->region == (lang_memory_region_type *) NULL
3043 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
3044 & (SEC_ALLOC | SEC_LOAD)) != 0)
3045 && os->region->name[0] == '*'
3046 && strcmp (os->region->name, "*default*") == 0))
3048 os->region = lang_memory_default (os->bfd_section);
3051 /* If a loadable section is using the default memory
3052 region, and some non default memory regions were
3053 defined, issue an error message. */
3054 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
3055 & (SEC_ALLOC | SEC_LOAD)) != 0
3056 && (bfd_get_section_flags (output_bfd, os->bfd_section)
3057 & SEC_NEVER_LOAD) == 0
3058 && ! link_info.relocateable
3059 && check_regions
3060 && strcmp (os->region->name, "*default*") == 0
3061 && lang_memory_region_list != NULL
3062 && (strcmp (lang_memory_region_list->name,
3063 "*default*") != 0
3064 || lang_memory_region_list->next != NULL))
3066 /* By default this is an error rather than just a
3067 warning because if we allocate the section to the
3068 default memory region we can end up creating an
3069 excessivly large binary, or even seg faulting when
3070 attmepting to perform a negative seek. See
3071 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
3072 for an example of this. This behaviour can be
3073 overridden by the using the --no-check-sections
3074 switch. */
3075 if (command_line.check_section_addresses)
3076 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
3077 bfd_get_section_name (output_bfd,
3078 os->bfd_section));
3079 else
3080 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
3081 bfd_get_section_name (output_bfd,
3082 os->bfd_section));
3085 dot = os->region->current;
3087 if (os->section_alignment == -1)
3089 bfd_vma olddot;
3091 olddot = dot;
3092 dot = align_power (dot,
3093 os->bfd_section->alignment_power);
3095 if (dot != olddot && config.warn_section_align)
3096 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
3097 os->name, (unsigned int) (dot - olddot));
3100 else
3102 etree_value_type r;
3104 r = exp_fold_tree (os->addr_tree,
3105 abs_output_section,
3106 lang_allocating_phase_enum,
3107 dot, &dot);
3108 if (!r.valid_p)
3109 einfo (_("%F%S: non constant address expression for section %s\n"),
3110 os->name);
3112 dot = r.value + r.section->bfd_section->vma;
3115 /* The section starts here.
3116 First, align to what the section needs. */
3118 if (os->section_alignment != -1)
3119 dot = align_power (dot, os->section_alignment);
3121 bfd_set_section_vma (0, os->bfd_section, dot);
3123 os->bfd_section->output_offset = 0;
3126 lang_size_sections_1 (os->children.head, os, &os->children.head,
3127 os->fill, dot, relax, check_regions);
3129 /* Put the section within the requested block size, or
3130 align at the block boundary. */
3131 after = align_n (os->bfd_section->vma
3132 + os->bfd_section->_raw_size / opb,
3133 (bfd_vma) os->block_value);
3135 if (bfd_is_abs_section (os->bfd_section))
3136 ASSERT (after == os->bfd_section->vma);
3137 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
3138 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
3139 && ! link_info.relocateable)
3140 os->bfd_section->_raw_size = 0;
3141 else
3142 os->bfd_section->_raw_size =
3143 (after - os->bfd_section->vma) * opb;
3145 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3146 os->processed = TRUE;
3148 if (os->update_dot_tree != 0)
3149 exp_fold_tree (os->update_dot_tree, abs_output_section,
3150 lang_allocating_phase_enum, dot, &dot);
3152 /* Update dot in the region ?
3153 We only do this if the section is going to be allocated,
3154 since unallocated sections do not contribute to the region's
3155 overall size in memory.
3157 If the SEC_NEVER_LOAD bit is not set, it will affect the
3158 addresses of sections after it. We have to update
3159 dot. */
3160 if (os->region != (lang_memory_region_type *) NULL
3161 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3162 & SEC_NEVER_LOAD) == 0
3163 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3164 & (SEC_ALLOC | SEC_LOAD))))
3166 os->region->current = dot;
3168 if (check_regions)
3169 /* Make sure the new address is within the region. */
3170 os_region_check (os, os->region, os->addr_tree,
3171 os->bfd_section->vma);
3173 /* If there's no load address specified, use the run
3174 region as the load region. */
3175 if (os->lma_region == NULL && os->load_base == NULL)
3176 os->lma_region = os->region;
3178 if (os->lma_region != NULL && os->lma_region != os->region)
3180 /* Set load_base, which will be handled later. */
3181 os->load_base = exp_intop (os->lma_region->current);
3182 os->lma_region->current +=
3183 os->bfd_section->_raw_size / opb;
3184 if (check_regions)
3185 os_region_check (os, os->lma_region, NULL,
3186 os->bfd_section->lma);
3190 break;
3192 case lang_constructors_statement_enum:
3193 dot = lang_size_sections_1 (constructor_list.head,
3194 output_section_statement,
3195 &s->wild_statement.children.head,
3196 fill, dot, relax, check_regions);
3197 break;
3199 case lang_data_statement_enum:
3201 unsigned int size = 0;
3203 s->data_statement.output_vma =
3204 dot - output_section_statement->bfd_section->vma;
3205 s->data_statement.output_section =
3206 output_section_statement->bfd_section;
3208 switch (s->data_statement.type)
3210 default:
3211 abort ();
3212 case QUAD:
3213 case SQUAD:
3214 size = QUAD_SIZE;
3215 break;
3216 case LONG:
3217 size = LONG_SIZE;
3218 break;
3219 case SHORT:
3220 size = SHORT_SIZE;
3221 break;
3222 case BYTE:
3223 size = BYTE_SIZE;
3224 break;
3226 if (size < opb)
3227 size = opb;
3228 dot += size / opb;
3229 output_section_statement->bfd_section->_raw_size += size;
3230 /* The output section gets contents, and then we inspect for
3231 any flags set in the input script which override any ALLOC. */
3232 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3233 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3235 output_section_statement->bfd_section->flags |=
3236 SEC_ALLOC | SEC_LOAD;
3239 break;
3241 case lang_reloc_statement_enum:
3243 int size;
3245 s->reloc_statement.output_vma =
3246 dot - output_section_statement->bfd_section->vma;
3247 s->reloc_statement.output_section =
3248 output_section_statement->bfd_section;
3249 size = bfd_get_reloc_size (s->reloc_statement.howto);
3250 dot += size / opb;
3251 output_section_statement->bfd_section->_raw_size += size;
3253 break;
3255 case lang_wild_statement_enum:
3257 dot = lang_size_sections_1 (s->wild_statement.children.head,
3258 output_section_statement,
3259 &s->wild_statement.children.head,
3260 fill, dot, relax, check_regions);
3262 break;
3264 case lang_object_symbols_statement_enum:
3265 link_info.create_object_symbols_section =
3266 output_section_statement->bfd_section;
3267 break;
3268 case lang_output_statement_enum:
3269 case lang_target_statement_enum:
3270 break;
3271 case lang_input_section_enum:
3273 asection *i;
3275 i = (*prev)->input_section.section;
3276 if (! relax)
3278 if (i->_cooked_size == 0)
3279 i->_cooked_size = i->_raw_size;
3281 else
3283 bfd_boolean again;
3285 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3286 einfo (_("%P%F: can't relax section: %E\n"));
3287 if (again)
3288 *relax = TRUE;
3290 dot = size_input_section (prev, output_section_statement,
3291 output_section_statement->fill, dot);
3293 break;
3294 case lang_input_statement_enum:
3295 break;
3296 case lang_fill_statement_enum:
3297 s->fill_statement.output_section =
3298 output_section_statement->bfd_section;
3300 fill = s->fill_statement.fill;
3301 break;
3302 case lang_assignment_statement_enum:
3304 bfd_vma newdot = dot;
3306 exp_fold_tree (s->assignment_statement.exp,
3307 output_section_statement,
3308 lang_allocating_phase_enum,
3309 dot,
3310 &newdot);
3312 if (newdot != dot)
3314 if (output_section_statement == abs_output_section)
3316 /* If we don't have an output section, then just adjust
3317 the default memory address. */
3318 lang_memory_region_lookup ("*default*")->current = newdot;
3320 else
3322 /* Insert a pad after this statement. We can't
3323 put the pad before when relaxing, in case the
3324 assignment references dot. */
3325 insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3326 output_section_statement->bfd_section, dot);
3328 /* Don't neuter the pad below when relaxing. */
3329 s = s->header.next;
3332 dot = newdot;
3335 break;
3337 case lang_padding_statement_enum:
3338 /* If this is the first time lang_size_sections is called,
3339 we won't have any padding statements. If this is the
3340 second or later passes when relaxing, we should allow
3341 padding to shrink. If padding is needed on this pass, it
3342 will be added back in. */
3343 s->padding_statement.size = 0;
3345 /* Make sure output_offset is valid. If relaxation shrinks
3346 the section and this pad isn't needed, it's possible to
3347 have output_offset larger than the final size of the
3348 section. bfd_set_section_contents will complain even for
3349 a pad size of zero. */
3350 s->padding_statement.output_offset
3351 = dot - output_section_statement->bfd_section->vma;
3352 break;
3354 case lang_group_statement_enum:
3355 dot = lang_size_sections_1 (s->group_statement.children.head,
3356 output_section_statement,
3357 &s->group_statement.children.head,
3358 fill, dot, relax, check_regions);
3359 break;
3361 default:
3362 FAIL ();
3363 break;
3365 /* We can only get here when relaxing is turned on. */
3366 case lang_address_statement_enum:
3367 break;
3369 prev = &s->header.next;
3371 return dot;
3374 bfd_vma
3375 lang_size_sections (s, output_section_statement, prev, fill, dot, relax,
3376 check_regions)
3377 lang_statement_union_type *s;
3378 lang_output_section_statement_type *output_section_statement;
3379 lang_statement_union_type **prev;
3380 fill_type *fill;
3381 bfd_vma dot;
3382 bfd_boolean *relax;
3383 bfd_boolean check_regions;
3385 bfd_vma result;
3387 exp_data_seg.phase = exp_dataseg_none;
3388 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3389 dot, relax, check_regions);
3390 if (exp_data_seg.phase == exp_dataseg_end_seen)
3392 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3393 a page could be saved in the data segment. */
3394 bfd_vma first, last;
3396 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3397 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3398 if (first && last
3399 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3400 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3401 && first + last <= exp_data_seg.pagesize)
3403 exp_data_seg.phase = exp_dataseg_adjust;
3404 result = lang_size_sections_1 (s, output_section_statement, prev,
3405 fill, dot, relax, check_regions);
3409 return result;
3412 bfd_vma
3413 lang_do_assignments (s, output_section_statement, fill, dot)
3414 lang_statement_union_type *s;
3415 lang_output_section_statement_type *output_section_statement;
3416 fill_type *fill;
3417 bfd_vma dot;
3419 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3420 ldfile_output_machine);
3422 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3424 switch (s->header.type)
3426 case lang_constructors_statement_enum:
3427 dot = lang_do_assignments (constructor_list.head,
3428 output_section_statement,
3429 fill,
3430 dot);
3431 break;
3433 case lang_output_section_statement_enum:
3435 lang_output_section_statement_type *os;
3437 os = &(s->output_section_statement);
3438 if (os->bfd_section != NULL)
3440 dot = os->bfd_section->vma;
3441 (void) lang_do_assignments (os->children.head, os,
3442 os->fill, dot);
3443 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3446 if (os->load_base)
3448 /* If nothing has been placed into the output section then
3449 it won't have a bfd_section. */
3450 if (os->bfd_section)
3452 os->bfd_section->lma
3453 = exp_get_abs_int (os->load_base, 0, "load base",
3454 lang_final_phase_enum);
3458 break;
3459 case lang_wild_statement_enum:
3461 dot = lang_do_assignments (s->wild_statement.children.head,
3462 output_section_statement,
3463 fill, dot);
3465 break;
3467 case lang_object_symbols_statement_enum:
3468 case lang_output_statement_enum:
3469 case lang_target_statement_enum:
3470 #if 0
3471 case lang_common_statement_enum:
3472 #endif
3473 break;
3474 case lang_data_statement_enum:
3476 etree_value_type value;
3478 value = exp_fold_tree (s->data_statement.exp,
3479 abs_output_section,
3480 lang_final_phase_enum, dot, &dot);
3481 s->data_statement.value = value.value;
3482 if (!value.valid_p)
3483 einfo (_("%F%P: invalid data statement\n"));
3486 unsigned int size;
3487 switch (s->data_statement.type)
3489 default:
3490 abort ();
3491 case QUAD:
3492 case SQUAD:
3493 size = QUAD_SIZE;
3494 break;
3495 case LONG:
3496 size = LONG_SIZE;
3497 break;
3498 case SHORT:
3499 size = SHORT_SIZE;
3500 break;
3501 case BYTE:
3502 size = BYTE_SIZE;
3503 break;
3505 if (size < opb)
3506 size = opb;
3507 dot += size / opb;
3509 break;
3511 case lang_reloc_statement_enum:
3513 etree_value_type value;
3515 value = exp_fold_tree (s->reloc_statement.addend_exp,
3516 abs_output_section,
3517 lang_final_phase_enum, dot, &dot);
3518 s->reloc_statement.addend_value = value.value;
3519 if (!value.valid_p)
3520 einfo (_("%F%P: invalid reloc statement\n"));
3522 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
3523 break;
3525 case lang_input_section_enum:
3527 asection *in = s->input_section.section;
3529 if (in->_cooked_size != 0)
3530 dot += in->_cooked_size / opb;
3531 else
3532 dot += in->_raw_size / opb;
3534 break;
3536 case lang_input_statement_enum:
3537 break;
3538 case lang_fill_statement_enum:
3539 fill = s->fill_statement.fill;
3540 break;
3541 case lang_assignment_statement_enum:
3543 exp_fold_tree (s->assignment_statement.exp,
3544 output_section_statement,
3545 lang_final_phase_enum,
3546 dot,
3547 &dot);
3550 break;
3551 case lang_padding_statement_enum:
3552 dot += s->padding_statement.size / opb;
3553 break;
3555 case lang_group_statement_enum:
3556 dot = lang_do_assignments (s->group_statement.children.head,
3557 output_section_statement,
3558 fill, dot);
3560 break;
3562 default:
3563 FAIL ();
3564 break;
3565 case lang_address_statement_enum:
3566 break;
3570 return dot;
3573 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3574 operator .startof. (section_name), it produces an undefined symbol
3575 .startof.section_name. Similarly, when it sees
3576 .sizeof. (section_name), it produces an undefined symbol
3577 .sizeof.section_name. For all the output sections, we look for
3578 such symbols, and set them to the correct value. */
3580 static void
3581 lang_set_startof ()
3583 asection *s;
3585 if (link_info.relocateable)
3586 return;
3588 for (s = output_bfd->sections; s != NULL; s = s->next)
3590 const char *secname;
3591 char *buf;
3592 struct bfd_link_hash_entry *h;
3594 secname = bfd_get_section_name (output_bfd, s);
3595 buf = xmalloc (10 + strlen (secname));
3597 sprintf (buf, ".startof.%s", secname);
3598 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3599 if (h != NULL && h->type == bfd_link_hash_undefined)
3601 h->type = bfd_link_hash_defined;
3602 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3603 h->u.def.section = bfd_abs_section_ptr;
3606 sprintf (buf, ".sizeof.%s", secname);
3607 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3608 if (h != NULL && h->type == bfd_link_hash_undefined)
3610 unsigned opb;
3612 opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3613 ldfile_output_machine);
3614 h->type = bfd_link_hash_defined;
3615 if (s->_cooked_size != 0)
3616 h->u.def.value = s->_cooked_size / opb;
3617 else
3618 h->u.def.value = s->_raw_size / opb;
3619 h->u.def.section = bfd_abs_section_ptr;
3622 free (buf);
3626 static void
3627 lang_finish ()
3629 struct bfd_link_hash_entry *h;
3630 bfd_boolean warn;
3632 if (link_info.relocateable || link_info.shared)
3633 warn = FALSE;
3634 else
3635 warn = TRUE;
3637 if (entry_symbol.name == (const char *) NULL)
3639 /* No entry has been specified. Look for start, but don't warn
3640 if we don't find it. */
3641 entry_symbol.name = "start";
3642 warn = FALSE;
3645 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3646 FALSE, FALSE, TRUE);
3647 if (h != (struct bfd_link_hash_entry *) NULL
3648 && (h->type == bfd_link_hash_defined
3649 || h->type == bfd_link_hash_defweak)
3650 && h->u.def.section->output_section != NULL)
3652 bfd_vma val;
3654 val = (h->u.def.value
3655 + bfd_get_section_vma (output_bfd,
3656 h->u.def.section->output_section)
3657 + h->u.def.section->output_offset);
3658 if (! bfd_set_start_address (output_bfd, val))
3659 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3661 else
3663 bfd_vma val;
3664 const char *send;
3666 /* We couldn't find the entry symbol. Try parsing it as a
3667 number. */
3668 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3669 if (*send == '\0')
3671 if (! bfd_set_start_address (output_bfd, val))
3672 einfo (_("%P%F: can't set start address\n"));
3674 else
3676 asection *ts;
3678 /* Can't find the entry symbol, and it's not a number. Use
3679 the first address in the text section. */
3680 ts = bfd_get_section_by_name (output_bfd, entry_section);
3681 if (ts != (asection *) NULL)
3683 if (warn)
3684 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3685 entry_symbol.name,
3686 bfd_get_section_vma (output_bfd, ts));
3687 if (! bfd_set_start_address (output_bfd,
3688 bfd_get_section_vma (output_bfd,
3689 ts)))
3690 einfo (_("%P%F: can't set start address\n"));
3692 else
3694 if (warn)
3695 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3696 entry_symbol.name);
3702 /* This is a small function used when we want to ignore errors from
3703 BFD. */
3705 static void
3706 #ifdef ANSI_PROTOTYPES
3707 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3708 #else
3709 ignore_bfd_errors (s)
3710 const char *s ATTRIBUTE_UNUSED;
3711 #endif
3713 /* Don't do anything. */
3716 /* Check that the architecture of all the input files is compatible
3717 with the output file. Also call the backend to let it do any
3718 other checking that is needed. */
3720 static void
3721 lang_check ()
3723 lang_statement_union_type *file;
3724 bfd *input_bfd;
3725 const bfd_arch_info_type *compatible;
3727 for (file = file_chain.head;
3728 file != (lang_statement_union_type *) NULL;
3729 file = file->input_statement.next)
3731 input_bfd = file->input_statement.the_bfd;
3732 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3733 command_line.accept_unknown_input_arch);
3735 /* In general it is not possible to perform a relocatable
3736 link between differing object formats when the input
3737 file has relocations, because the relocations in the
3738 input format may not have equivalent representations in
3739 the output format (and besides BFD does not translate
3740 relocs for other link purposes than a final link). */
3741 if ((link_info.relocateable || link_info.emitrelocations)
3742 && (compatible == NULL
3743 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3744 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3746 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3747 bfd_get_target (input_bfd), input_bfd,
3748 bfd_get_target (output_bfd), output_bfd);
3749 /* einfo with %F exits. */
3752 if (compatible == NULL)
3754 if (command_line.warn_mismatch)
3755 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3756 bfd_printable_name (input_bfd), input_bfd,
3757 bfd_printable_name (output_bfd));
3759 else if (bfd_count_sections (input_bfd))
3761 /* If the input bfd has no contents, it shouldn't set the
3762 private data of the output bfd. */
3764 bfd_error_handler_type pfn = NULL;
3766 /* If we aren't supposed to warn about mismatched input
3767 files, temporarily set the BFD error handler to a
3768 function which will do nothing. We still want to call
3769 bfd_merge_private_bfd_data, since it may set up
3770 information which is needed in the output file. */
3771 if (! command_line.warn_mismatch)
3772 pfn = bfd_set_error_handler (ignore_bfd_errors);
3773 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3775 if (command_line.warn_mismatch)
3776 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3777 input_bfd);
3779 if (! command_line.warn_mismatch)
3780 bfd_set_error_handler (pfn);
3785 /* Look through all the global common symbols and attach them to the
3786 correct section. The -sort-common command line switch may be used
3787 to roughly sort the entries by size. */
3789 static void
3790 lang_common ()
3792 if (command_line.inhibit_common_definition)
3793 return;
3794 if (link_info.relocateable
3795 && ! command_line.force_common_definition)
3796 return;
3798 if (! config.sort_common)
3799 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3800 else
3802 int power;
3804 for (power = 4; power >= 0; power--)
3805 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3806 (PTR) &power);
3810 /* Place one common symbol in the correct section. */
3812 static bfd_boolean
3813 lang_one_common (h, info)
3814 struct bfd_link_hash_entry *h;
3815 PTR info;
3817 unsigned int power_of_two;
3818 bfd_vma size;
3819 asection *section;
3820 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3821 ldfile_output_machine);
3823 if (h->type != bfd_link_hash_common)
3824 return TRUE;
3826 size = h->u.c.size;
3827 power_of_two = h->u.c.p->alignment_power;
3829 if (config.sort_common
3830 && power_of_two < (unsigned int) *(int *) info)
3831 return TRUE;
3833 section = h->u.c.p->section;
3835 /* Increase the size of the section. */
3836 section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3837 (bfd_vma) 1 << power_of_two) * opb;
3839 /* Adjust the alignment if necessary. */
3840 if (power_of_two > section->alignment_power)
3841 section->alignment_power = power_of_two;
3843 /* Change the symbol from common to defined. */
3844 h->type = bfd_link_hash_defined;
3845 h->u.def.section = section;
3846 h->u.def.value = section->_cooked_size;
3848 /* Increase the size of the section. */
3849 section->_cooked_size += size;
3851 /* Make sure the section is allocated in memory, and make sure that
3852 it is no longer a common section. */
3853 section->flags |= SEC_ALLOC;
3854 section->flags &= ~SEC_IS_COMMON;
3856 if (config.map_file != NULL)
3858 static bfd_boolean header_printed;
3859 int len;
3860 char *name;
3861 char buf[50];
3863 if (! header_printed)
3865 minfo (_("\nAllocating common symbols\n"));
3866 minfo (_("Common symbol size file\n\n"));
3867 header_printed = TRUE;
3870 name = demangle (h->root.string);
3871 minfo ("%s", name);
3872 len = strlen (name);
3873 free (name);
3875 if (len >= 19)
3877 print_nl ();
3878 len = 0;
3880 while (len < 20)
3882 print_space ();
3883 ++len;
3886 minfo ("0x");
3887 if (size <= 0xffffffff)
3888 sprintf (buf, "%lx", (unsigned long) size);
3889 else
3890 sprintf_vma (buf, size);
3891 minfo ("%s", buf);
3892 len = strlen (buf);
3894 while (len < 16)
3896 print_space ();
3897 ++len;
3900 minfo ("%B\n", section->owner);
3903 return TRUE;
3906 /* Run through the input files and ensure that every input section has
3907 somewhere to go. If one is found without a destination then create
3908 an input request and place it into the statement tree. */
3910 static void
3911 lang_place_orphans ()
3913 LANG_FOR_EACH_INPUT_STATEMENT (file)
3915 asection *s;
3917 for (s = file->the_bfd->sections;
3918 s != (asection *) NULL;
3919 s = s->next)
3921 if (s->output_section == (asection *) NULL)
3923 /* This section of the file is not attached, root
3924 around for a sensible place for it to go. */
3926 if (file->just_syms_flag)
3928 abort ();
3930 else if (strcmp (s->name, "COMMON") == 0)
3932 /* This is a lonely common section which must have
3933 come from an archive. We attach to the section
3934 with the wildcard. */
3935 if (! link_info.relocateable
3936 || command_line.force_common_definition)
3938 if (default_common_section == NULL)
3940 #if 0
3941 /* This message happens when using the
3942 svr3.ifile linker script, so I have
3943 disabled it. */
3944 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3945 #endif
3946 default_common_section =
3947 lang_output_section_statement_lookup (".bss");
3950 lang_add_section (&default_common_section->children, s,
3951 default_common_section, file);
3954 else if (ldemul_place_orphan (file, s))
3956 else
3958 lang_output_section_statement_type *os;
3960 os = lang_output_section_statement_lookup (s->name);
3961 lang_add_section (&os->children, s, os, file);
3968 void
3969 lang_set_flags (ptr, flags, invert)
3970 lang_memory_region_type *ptr;
3971 const char *flags;
3972 int invert;
3974 flagword *ptr_flags;
3976 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3977 while (*flags)
3979 switch (*flags)
3981 case 'A': case 'a':
3982 *ptr_flags |= SEC_ALLOC;
3983 break;
3985 case 'R': case 'r':
3986 *ptr_flags |= SEC_READONLY;
3987 break;
3989 case 'W': case 'w':
3990 *ptr_flags |= SEC_DATA;
3991 break;
3993 case 'X': case 'x':
3994 *ptr_flags |= SEC_CODE;
3995 break;
3997 case 'L': case 'l':
3998 case 'I': case 'i':
3999 *ptr_flags |= SEC_LOAD;
4000 break;
4002 default:
4003 einfo (_("%P%F: invalid syntax in flags\n"));
4004 break;
4006 flags++;
4010 /* Call a function on each input file. This function will be called
4011 on an archive, but not on the elements. */
4013 void
4014 lang_for_each_input_file (func)
4015 void (*func) PARAMS ((lang_input_statement_type *));
4017 lang_input_statement_type *f;
4019 for (f = (lang_input_statement_type *) input_file_chain.head;
4020 f != NULL;
4021 f = (lang_input_statement_type *) f->next_real_file)
4022 func (f);
4025 /* Call a function on each file. The function will be called on all
4026 the elements of an archive which are included in the link, but will
4027 not be called on the archive file itself. */
4029 void
4030 lang_for_each_file (func)
4031 void (*func) PARAMS ((lang_input_statement_type *));
4033 LANG_FOR_EACH_INPUT_STATEMENT (f)
4035 func (f);
4039 #if 0
4041 /* Not used. */
4043 void
4044 lang_for_each_input_section (func)
4045 void (*func) PARAMS ((bfd *ab, asection *as));
4047 LANG_FOR_EACH_INPUT_STATEMENT (f)
4049 asection *s;
4051 for (s = f->the_bfd->sections;
4052 s != (asection *) NULL;
4053 s = s->next)
4055 func (f->the_bfd, s);
4060 #endif
4062 void
4063 ldlang_add_file (entry)
4064 lang_input_statement_type *entry;
4066 bfd **pp;
4068 lang_statement_append (&file_chain,
4069 (lang_statement_union_type *) entry,
4070 &entry->next);
4072 /* The BFD linker needs to have a list of all input BFDs involved in
4073 a link. */
4074 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
4075 ASSERT (entry->the_bfd != output_bfd);
4076 for (pp = &link_info.input_bfds;
4077 *pp != (bfd *) NULL;
4078 pp = &(*pp)->link_next)
4080 *pp = entry->the_bfd;
4081 entry->the_bfd->usrdata = (PTR) entry;
4082 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4084 /* Look through the sections and check for any which should not be
4085 included in the link. We need to do this now, so that we can
4086 notice when the backend linker tries to report multiple
4087 definition errors for symbols which are in sections we aren't
4088 going to link. FIXME: It might be better to entirely ignore
4089 symbols which are defined in sections which are going to be
4090 discarded. This would require modifying the backend linker for
4091 each backend which might set the SEC_LINK_ONCE flag. If we do
4092 this, we should probably handle SEC_EXCLUDE in the same way. */
4094 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
4097 void
4098 lang_add_output (name, from_script)
4099 const char *name;
4100 int from_script;
4102 /* Make -o on command line override OUTPUT in script. */
4103 if (!had_output_filename || !from_script)
4105 output_filename = name;
4106 had_output_filename = TRUE;
4110 static lang_output_section_statement_type *current_section;
4112 static int
4113 topower (x)
4114 int x;
4116 unsigned int i = 1;
4117 int l;
4119 if (x < 0)
4120 return -1;
4122 for (l = 0; l < 32; l++)
4124 if (i >= (unsigned int) x)
4125 return l;
4126 i <<= 1;
4129 return 0;
4132 lang_output_section_statement_type *
4133 lang_enter_output_section_statement (output_section_statement_name,
4134 address_exp, sectype, block_value,
4135 align, subalign, ebase)
4136 const char *output_section_statement_name;
4137 etree_type *address_exp;
4138 enum section_type sectype;
4139 bfd_vma block_value;
4140 etree_type *align;
4141 etree_type *subalign;
4142 etree_type *ebase;
4144 lang_output_section_statement_type *os;
4146 current_section =
4147 os =
4148 lang_output_section_statement_lookup (output_section_statement_name);
4150 /* Add this statement to tree. */
4151 #if 0
4152 add_statement (lang_output_section_statement_enum,
4153 output_section_statement);
4154 #endif
4155 /* Make next things chain into subchain of this. */
4157 if (os->addr_tree == (etree_type *) NULL)
4159 os->addr_tree = address_exp;
4161 os->sectype = sectype;
4162 if (sectype != noload_section)
4163 os->flags = SEC_NO_FLAGS;
4164 else
4165 os->flags = SEC_NEVER_LOAD;
4166 os->block_value = block_value ? block_value : 1;
4167 stat_ptr = &os->children;
4169 os->subsection_alignment =
4170 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4171 os->section_alignment =
4172 topower (exp_get_value_int (align, -1, "section alignment", 0));
4174 os->load_base = ebase;
4175 return os;
4178 void
4179 lang_final ()
4181 lang_output_statement_type *new =
4182 new_stat (lang_output_statement, stat_ptr);
4184 new->name = output_filename;
4187 /* Reset the current counters in the regions. */
4189 void
4190 lang_reset_memory_regions ()
4192 lang_memory_region_type *p = lang_memory_region_list;
4193 asection *o;
4195 for (p = lang_memory_region_list;
4196 p != (lang_memory_region_type *) NULL;
4197 p = p->next)
4199 p->old_length = (bfd_size_type) (p->current - p->origin);
4200 p->current = p->origin;
4203 for (o = output_bfd->sections; o != NULL; o = o->next)
4204 o->_raw_size = 0;
4207 /* If the wild pattern was marked KEEP, the member sections
4208 should be as well. */
4210 static void
4211 gc_section_callback (ptr, sec, section, file, data)
4212 lang_wild_statement_type *ptr;
4213 struct wildcard_list *sec ATTRIBUTE_UNUSED;
4214 asection *section;
4215 lang_input_statement_type *file ATTRIBUTE_UNUSED;
4216 PTR data ATTRIBUTE_UNUSED;
4218 if (ptr->keep_sections)
4219 section->flags |= SEC_KEEP;
4222 /* Handle a wild statement, marking it against GC. */
4224 static void
4225 lang_gc_wild (s)
4226 lang_wild_statement_type *s;
4228 walk_wild (s, gc_section_callback, NULL);
4231 /* Iterate over sections marking them against GC. */
4233 static void
4234 lang_gc_sections_1 (s)
4235 lang_statement_union_type *s;
4237 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
4239 switch (s->header.type)
4241 case lang_wild_statement_enum:
4242 lang_gc_wild (&s->wild_statement);
4243 break;
4244 case lang_constructors_statement_enum:
4245 lang_gc_sections_1 (constructor_list.head);
4246 break;
4247 case lang_output_section_statement_enum:
4248 lang_gc_sections_1 (s->output_section_statement.children.head);
4249 break;
4250 case lang_group_statement_enum:
4251 lang_gc_sections_1 (s->group_statement.children.head);
4252 break;
4253 default:
4254 break;
4259 static void
4260 lang_gc_sections ()
4262 struct bfd_link_hash_entry *h;
4263 ldlang_undef_chain_list_type *ulist;
4265 /* Keep all sections so marked in the link script. */
4267 lang_gc_sections_1 (statement_list.head);
4269 /* Keep all sections containing symbols undefined on the command-line,
4270 and the section containing the entry symbol. */
4272 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4274 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4275 FALSE, FALSE, FALSE);
4277 if (h != (struct bfd_link_hash_entry *) NULL
4278 && (h->type == bfd_link_hash_defined
4279 || h->type == bfd_link_hash_defweak)
4280 && ! bfd_is_abs_section (h->u.def.section))
4282 h->u.def.section->flags |= SEC_KEEP;
4286 bfd_gc_sections (output_bfd, &link_info);
4289 void
4290 lang_process ()
4292 lang_reasonable_defaults ();
4293 current_target = default_target;
4295 /* Open the output file. */
4296 lang_for_each_statement (ldlang_open_output);
4298 ldemul_create_output_section_statements ();
4300 /* Add to the hash table all undefineds on the command line. */
4301 lang_place_undefineds ();
4303 already_linked_table_init ();
4305 /* Create a bfd for each input file. */
4306 current_target = default_target;
4307 open_input_bfds (statement_list.head, FALSE);
4309 link_info.gc_sym_list = &entry_symbol;
4310 if (entry_symbol.name == NULL)
4311 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4313 ldemul_after_open ();
4315 already_linked_table_free ();
4317 /* Make sure that we're not mixing architectures. We call this
4318 after all the input files have been opened, but before we do any
4319 other processing, so that any operations merge_private_bfd_data
4320 does on the output file will be known during the rest of the
4321 link. */
4322 lang_check ();
4324 /* Handle .exports instead of a version script if we're told to do so. */
4325 if (command_line.version_exports_section)
4326 lang_do_version_exports_section ();
4328 /* Build all sets based on the information gathered from the input
4329 files. */
4330 ldctor_build_sets ();
4332 /* Remove unreferenced sections if asked to. */
4333 if (command_line.gc_sections)
4334 lang_gc_sections ();
4336 /* If there were any SEC_MERGE sections, finish their merging, so that
4337 section sizes can be computed. This has to be done after GC of sections,
4338 so that GCed sections are not merged, but before assigning output
4339 sections, since removing whole input sections is hard then. */
4340 bfd_merge_sections (output_bfd, &link_info);
4342 /* Size up the common data. */
4343 lang_common ();
4345 /* Run through the contours of the script and attach input sections
4346 to the correct output sections. */
4347 map_input_to_output_sections (statement_list.head, (char *) NULL,
4348 (lang_output_section_statement_type *) NULL);
4350 /* Find any sections not attached explicitly and handle them. */
4351 lang_place_orphans ();
4353 if (! link_info.relocateable)
4355 /* Look for a text section and set the readonly attribute in it. */
4356 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4358 if (found != (asection *) NULL)
4360 if (config.text_read_only)
4361 found->flags |= SEC_READONLY;
4362 else
4363 found->flags &= ~SEC_READONLY;
4367 /* Do anything special before sizing sections. This is where ELF
4368 and other back-ends size dynamic sections. */
4369 ldemul_before_allocation ();
4371 if (!link_info.relocateable)
4372 strip_excluded_output_sections ();
4374 /* We must record the program headers before we try to fix the
4375 section positions, since they will affect SIZEOF_HEADERS. */
4376 lang_record_phdrs ();
4378 /* Size up the sections. */
4379 lang_size_sections (statement_list.head,
4380 abs_output_section,
4381 &statement_list.head, 0, (bfd_vma) 0, NULL,
4382 command_line.relax ? FALSE : TRUE);
4384 /* Now run around and relax if we can. */
4385 if (command_line.relax)
4387 /* Keep relaxing until bfd_relax_section gives up. */
4388 bfd_boolean relax_again;
4392 lang_reset_memory_regions ();
4394 relax_again = FALSE;
4396 /* Note: pe-dll.c does something like this also. If you find
4397 you need to change this code, you probably need to change
4398 pe-dll.c also. DJ */
4400 /* Do all the assignments with our current guesses as to
4401 section sizes. */
4402 lang_do_assignments (statement_list.head,
4403 abs_output_section,
4404 (fill_type *) 0, (bfd_vma) 0);
4406 /* Perform another relax pass - this time we know where the
4407 globals are, so can make a better guess. */
4408 lang_size_sections (statement_list.head,
4409 abs_output_section,
4410 &statement_list.head, 0, (bfd_vma) 0,
4411 &relax_again, FALSE);
4413 /* If the normal relax is done and the relax finalize pass
4414 is not performed yet, we perform another relax pass. */
4415 if (!relax_again && !link_info.relax_finalizing)
4417 link_info.relax_finalizing = TRUE;
4418 relax_again = TRUE;
4421 while (relax_again);
4423 /* Final extra sizing to report errors. */
4424 lang_reset_memory_regions ();
4425 lang_do_assignments (statement_list.head,
4426 abs_output_section,
4427 (fill_type *) 0, (bfd_vma) 0);
4428 lang_size_sections (statement_list.head,
4429 abs_output_section,
4430 & statement_list.head, 0, (bfd_vma) 0,
4431 NULL, TRUE);
4434 /* See if anything special should be done now we know how big
4435 everything is. */
4436 ldemul_after_allocation ();
4438 /* Fix any .startof. or .sizeof. symbols. */
4439 lang_set_startof ();
4441 /* Do all the assignments, now that we know the final resting places
4442 of all the symbols. */
4444 lang_do_assignments (statement_list.head,
4445 abs_output_section,
4446 (fill_type *) 0, (bfd_vma) 0);
4448 /* Make sure that the section addresses make sense. */
4449 if (! link_info.relocateable
4450 && command_line.check_section_addresses)
4451 lang_check_section_addresses ();
4453 /* Final stuffs. */
4455 ldemul_finish ();
4456 lang_finish ();
4459 /* EXPORTED TO YACC */
4461 void
4462 lang_add_wild (filespec, section_list, keep_sections)
4463 struct wildcard_spec *filespec;
4464 struct wildcard_list *section_list;
4465 bfd_boolean keep_sections;
4467 struct wildcard_list *curr, *next;
4468 lang_wild_statement_type *new;
4470 /* Reverse the list as the parser puts it back to front. */
4471 for (curr = section_list, section_list = NULL;
4472 curr != NULL;
4473 section_list = curr, curr = next)
4475 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4476 placed_commons = TRUE;
4478 next = curr->next;
4479 curr->next = section_list;
4482 if (filespec != NULL && filespec->name != NULL)
4484 if (strcmp (filespec->name, "*") == 0)
4485 filespec->name = NULL;
4486 else if (! wildcardp (filespec->name))
4487 lang_has_input_file = TRUE;
4490 new = new_stat (lang_wild_statement, stat_ptr);
4491 new->filename = NULL;
4492 new->filenames_sorted = FALSE;
4493 if (filespec != NULL)
4495 new->filename = filespec->name;
4496 new->filenames_sorted = filespec->sorted;
4498 new->section_list = section_list;
4499 new->keep_sections = keep_sections;
4500 lang_list_init (&new->children);
4503 void
4504 lang_section_start (name, address)
4505 const char *name;
4506 etree_type *address;
4508 lang_address_statement_type *ad;
4510 ad = new_stat (lang_address_statement, stat_ptr);
4511 ad->section_name = name;
4512 ad->address = address;
4515 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4516 because of a -e argument on the command line, or zero if this is
4517 called by ENTRY in a linker script. Command line arguments take
4518 precedence. */
4520 void
4521 lang_add_entry (name, cmdline)
4522 const char *name;
4523 bfd_boolean cmdline;
4525 if (entry_symbol.name == NULL
4526 || cmdline
4527 || ! entry_from_cmdline)
4529 entry_symbol.name = name;
4530 entry_from_cmdline = cmdline;
4534 void
4535 lang_add_target (name)
4536 const char *name;
4538 lang_target_statement_type *new = new_stat (lang_target_statement,
4539 stat_ptr);
4541 new->target = name;
4545 void
4546 lang_add_map (name)
4547 const char *name;
4549 while (*name)
4551 switch (*name)
4553 case 'F':
4554 map_option_f = TRUE;
4555 break;
4557 name++;
4561 void
4562 lang_add_fill (fill)
4563 fill_type *fill;
4565 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4566 stat_ptr);
4568 new->fill = fill;
4571 void
4572 lang_add_data (type, exp)
4573 int type;
4574 union etree_union *exp;
4577 lang_data_statement_type *new = new_stat (lang_data_statement,
4578 stat_ptr);
4580 new->exp = exp;
4581 new->type = type;
4585 /* Create a new reloc statement. RELOC is the BFD relocation type to
4586 generate. HOWTO is the corresponding howto structure (we could
4587 look this up, but the caller has already done so). SECTION is the
4588 section to generate a reloc against, or NAME is the name of the
4589 symbol to generate a reloc against. Exactly one of SECTION and
4590 NAME must be NULL. ADDEND is an expression for the addend. */
4592 void
4593 lang_add_reloc (reloc, howto, section, name, addend)
4594 bfd_reloc_code_real_type reloc;
4595 reloc_howto_type *howto;
4596 asection *section;
4597 const char *name;
4598 union etree_union *addend;
4600 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4602 p->reloc = reloc;
4603 p->howto = howto;
4604 p->section = section;
4605 p->name = name;
4606 p->addend_exp = addend;
4608 p->addend_value = 0;
4609 p->output_section = NULL;
4610 p->output_vma = 0;
4613 lang_assignment_statement_type *
4614 lang_add_assignment (exp)
4615 etree_type *exp;
4617 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4618 stat_ptr);
4620 new->exp = exp;
4621 return new;
4624 void
4625 lang_add_attribute (attribute)
4626 enum statement_enum attribute;
4628 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4631 void
4632 lang_startup (name)
4633 const char *name;
4635 if (startup_file != (char *) NULL)
4637 einfo (_("%P%Fmultiple STARTUP files\n"));
4639 first_file->filename = name;
4640 first_file->local_sym_name = name;
4641 first_file->real = TRUE;
4643 startup_file = name;
4646 void
4647 lang_float (maybe)
4648 bfd_boolean maybe;
4650 lang_float_flag = maybe;
4654 /* Work out the load- and run-time regions from a script statement, and
4655 store them in *LMA_REGION and *REGION respectively.
4657 MEMSPEC is the name of the run-time region, or "*default*" if the
4658 statement didn't specify one. LMA_MEMSPEC is the name of the
4659 load-time region, or null if the statement didn't specify one.
4660 HAVE_LMA_P is TRUE if the statement had an explicit load address.
4662 It is an error to specify both a load region and a load address. */
4664 static void
4665 lang_get_regions (region, lma_region, memspec, lma_memspec, have_lma_p)
4666 struct memory_region_struct **region, **lma_region;
4667 const char *memspec, *lma_memspec;
4668 int have_lma_p;
4670 *lma_region = lang_memory_region_lookup (lma_memspec);
4672 /* If no runtime region has been given, but the load region has
4673 been, use the load region. */
4674 if (lma_memspec != 0 && strcmp (memspec, "*default*") == 0)
4675 *region = *lma_region;
4676 else
4677 *region = lang_memory_region_lookup (memspec);
4679 if (have_lma_p && lma_memspec != 0)
4680 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4683 void
4684 lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
4685 fill_type *fill;
4686 const char *memspec;
4687 struct lang_output_section_phdr_list *phdrs;
4688 const char *lma_memspec;
4690 lang_get_regions (&current_section->region,
4691 &current_section->lma_region,
4692 memspec, lma_memspec,
4693 current_section->load_base != 0);
4694 current_section->fill = fill;
4695 current_section->phdrs = phdrs;
4696 stat_ptr = &statement_list;
4699 /* Create an absolute symbol with the given name with the value of the
4700 address of first byte of the section named.
4702 If the symbol already exists, then do nothing. */
4704 void
4705 lang_abs_symbol_at_beginning_of (secname, name)
4706 const char *secname;
4707 const char *name;
4709 struct bfd_link_hash_entry *h;
4711 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4712 if (h == (struct bfd_link_hash_entry *) NULL)
4713 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4715 if (h->type == bfd_link_hash_new
4716 || h->type == bfd_link_hash_undefined)
4718 asection *sec;
4720 h->type = bfd_link_hash_defined;
4722 sec = bfd_get_section_by_name (output_bfd, secname);
4723 if (sec == (asection *) NULL)
4724 h->u.def.value = 0;
4725 else
4726 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4728 h->u.def.section = bfd_abs_section_ptr;
4732 /* Create an absolute symbol with the given name with the value of the
4733 address of the first byte after the end of the section named.
4735 If the symbol already exists, then do nothing. */
4737 void
4738 lang_abs_symbol_at_end_of (secname, name)
4739 const char *secname;
4740 const char *name;
4742 struct bfd_link_hash_entry *h;
4744 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4745 if (h == (struct bfd_link_hash_entry *) NULL)
4746 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4748 if (h->type == bfd_link_hash_new
4749 || h->type == bfd_link_hash_undefined)
4751 asection *sec;
4753 h->type = bfd_link_hash_defined;
4755 sec = bfd_get_section_by_name (output_bfd, secname);
4756 if (sec == (asection *) NULL)
4757 h->u.def.value = 0;
4758 else
4759 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4760 + bfd_section_size (output_bfd, sec) /
4761 bfd_octets_per_byte (output_bfd));
4763 h->u.def.section = bfd_abs_section_ptr;
4767 void
4768 lang_statement_append (list, element, field)
4769 lang_statement_list_type *list;
4770 lang_statement_union_type *element;
4771 lang_statement_union_type **field;
4773 *(list->tail) = element;
4774 list->tail = field;
4777 /* Set the output format type. -oformat overrides scripts. */
4779 void
4780 lang_add_output_format (format, big, little, from_script)
4781 const char *format;
4782 const char *big;
4783 const char *little;
4784 int from_script;
4786 if (output_target == NULL || !from_script)
4788 if (command_line.endian == ENDIAN_BIG
4789 && big != NULL)
4790 format = big;
4791 else if (command_line.endian == ENDIAN_LITTLE
4792 && little != NULL)
4793 format = little;
4795 output_target = format;
4799 /* Enter a group. This creates a new lang_group_statement, and sets
4800 stat_ptr to build new statements within the group. */
4802 void
4803 lang_enter_group ()
4805 lang_group_statement_type *g;
4807 g = new_stat (lang_group_statement, stat_ptr);
4808 lang_list_init (&g->children);
4809 stat_ptr = &g->children;
4812 /* Leave a group. This just resets stat_ptr to start writing to the
4813 regular list of statements again. Note that this will not work if
4814 groups can occur inside anything else which can adjust stat_ptr,
4815 but currently they can't. */
4817 void
4818 lang_leave_group ()
4820 stat_ptr = &statement_list;
4823 /* Add a new program header. This is called for each entry in a PHDRS
4824 command in a linker script. */
4826 void
4827 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4828 const char *name;
4829 etree_type *type;
4830 bfd_boolean filehdr;
4831 bfd_boolean phdrs;
4832 etree_type *at;
4833 etree_type *flags;
4835 struct lang_phdr *n, **pp;
4837 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4838 n->next = NULL;
4839 n->name = name;
4840 n->type = exp_get_value_int (type, 0, "program header type",
4841 lang_final_phase_enum);
4842 n->filehdr = filehdr;
4843 n->phdrs = phdrs;
4844 n->at = at;
4845 n->flags = flags;
4847 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4849 *pp = n;
4852 /* Record the program header information in the output BFD. FIXME: We
4853 should not be calling an ELF specific function here. */
4855 static void
4856 lang_record_phdrs ()
4858 unsigned int alc;
4859 asection **secs;
4860 struct lang_output_section_phdr_list *last;
4861 struct lang_phdr *l;
4862 lang_statement_union_type *u;
4864 alc = 10;
4865 secs = (asection **) xmalloc (alc * sizeof (asection *));
4866 last = NULL;
4867 for (l = lang_phdr_list; l != NULL; l = l->next)
4869 unsigned int c;
4870 flagword flags;
4871 bfd_vma at;
4873 c = 0;
4874 for (u = lang_output_section_statement.head;
4875 u != NULL;
4876 u = u->output_section_statement.next)
4878 lang_output_section_statement_type *os;
4879 struct lang_output_section_phdr_list *pl;
4881 os = &u->output_section_statement;
4883 pl = os->phdrs;
4884 if (pl != NULL)
4885 last = pl;
4886 else
4888 if (os->sectype == noload_section
4889 || os->bfd_section == NULL
4890 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4891 continue;
4892 pl = last;
4895 if (os->bfd_section == NULL)
4896 continue;
4898 for (; pl != NULL; pl = pl->next)
4900 if (strcmp (pl->name, l->name) == 0)
4902 if (c >= alc)
4904 alc *= 2;
4905 secs = ((asection **)
4906 xrealloc (secs, alc * sizeof (asection *)));
4908 secs[c] = os->bfd_section;
4909 ++c;
4910 pl->used = TRUE;
4915 if (l->flags == NULL)
4916 flags = 0;
4917 else
4918 flags = exp_get_vma (l->flags, 0, "phdr flags",
4919 lang_final_phase_enum);
4921 if (l->at == NULL)
4922 at = 0;
4923 else
4924 at = exp_get_vma (l->at, 0, "phdr load address",
4925 lang_final_phase_enum);
4927 if (! bfd_record_phdr (output_bfd, l->type,
4928 l->flags != NULL, flags, l->at != NULL,
4929 at, l->filehdr, l->phdrs, c, secs))
4930 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4933 free (secs);
4935 /* Make sure all the phdr assignments succeeded. */
4936 for (u = lang_output_section_statement.head;
4937 u != NULL;
4938 u = u->output_section_statement.next)
4940 struct lang_output_section_phdr_list *pl;
4942 if (u->output_section_statement.bfd_section == NULL)
4943 continue;
4945 for (pl = u->output_section_statement.phdrs;
4946 pl != NULL;
4947 pl = pl->next)
4948 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4949 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4950 u->output_section_statement.name, pl->name);
4954 /* Record a list of sections which may not be cross referenced. */
4956 void
4957 lang_add_nocrossref (l)
4958 struct lang_nocrossref *l;
4960 struct lang_nocrossrefs *n;
4962 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4963 n->next = nocrossref_list;
4964 n->list = l;
4965 nocrossref_list = n;
4967 /* Set notice_all so that we get informed about all symbols. */
4968 link_info.notice_all = TRUE;
4971 /* Overlay handling. We handle overlays with some static variables. */
4973 /* The overlay virtual address. */
4974 static etree_type *overlay_vma;
4976 /* An expression for the maximum section size seen so far. */
4977 static etree_type *overlay_max;
4979 /* A list of all the sections in this overlay. */
4981 struct overlay_list {
4982 struct overlay_list *next;
4983 lang_output_section_statement_type *os;
4986 static struct overlay_list *overlay_list;
4988 /* Start handling an overlay. */
4990 void
4991 lang_enter_overlay (vma_expr)
4992 etree_type *vma_expr;
4994 /* The grammar should prevent nested overlays from occurring. */
4995 ASSERT (overlay_vma == NULL && overlay_max == NULL);
4997 overlay_vma = vma_expr;
5000 /* Start a section in an overlay. We handle this by calling
5001 lang_enter_output_section_statement with the correct VMA.
5002 lang_leave_overlay sets up the LMA and memory regions. */
5004 void
5005 lang_enter_overlay_section (name)
5006 const char *name;
5008 struct overlay_list *n;
5009 etree_type *size;
5011 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5012 0, 0, 0, 0);
5014 /* If this is the first section, then base the VMA of future
5015 sections on this one. This will work correctly even if `.' is
5016 used in the addresses. */
5017 if (overlay_list == NULL)
5018 overlay_vma = exp_nameop (ADDR, name);
5020 /* Remember the section. */
5021 n = (struct overlay_list *) xmalloc (sizeof *n);
5022 n->os = current_section;
5023 n->next = overlay_list;
5024 overlay_list = n;
5026 size = exp_nameop (SIZEOF, name);
5028 /* Arrange to work out the maximum section end address. */
5029 if (overlay_max == NULL)
5030 overlay_max = size;
5031 else
5032 overlay_max = exp_binop (MAX_K, overlay_max, size);
5035 /* Finish a section in an overlay. There isn't any special to do
5036 here. */
5038 void
5039 lang_leave_overlay_section (fill, phdrs)
5040 fill_type *fill;
5041 struct lang_output_section_phdr_list *phdrs;
5043 const char *name;
5044 char *clean, *s2;
5045 const char *s1;
5046 char *buf;
5048 name = current_section->name;
5050 /* For now, assume that "*default*" is the run-time memory region and
5051 that no load-time region has been specified. It doesn't really
5052 matter what we say here, since lang_leave_overlay will override it. */
5053 lang_leave_output_section_statement (fill, "*default*", phdrs, 0);
5055 /* Define the magic symbols. */
5057 clean = xmalloc (strlen (name) + 1);
5058 s2 = clean;
5059 for (s1 = name; *s1 != '\0'; s1++)
5060 if (ISALNUM (*s1) || *s1 == '_')
5061 *s2++ = *s1;
5062 *s2 = '\0';
5064 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5065 sprintf (buf, "__load_start_%s", clean);
5066 lang_add_assignment (exp_assop ('=', buf,
5067 exp_nameop (LOADADDR, name)));
5069 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5070 sprintf (buf, "__load_stop_%s", clean);
5071 lang_add_assignment (exp_assop ('=', buf,
5072 exp_binop ('+',
5073 exp_nameop (LOADADDR, name),
5074 exp_nameop (SIZEOF, name))));
5076 free (clean);
5079 /* Finish an overlay. If there are any overlay wide settings, this
5080 looks through all the sections in the overlay and sets them. */
5082 void
5083 lang_leave_overlay (lma_expr, nocrossrefs, fill, memspec, phdrs, lma_memspec)
5084 etree_type *lma_expr;
5085 int nocrossrefs;
5086 fill_type *fill;
5087 const char *memspec;
5088 struct lang_output_section_phdr_list *phdrs;
5089 const char *lma_memspec;
5091 lang_memory_region_type *region;
5092 lang_memory_region_type *lma_region;
5093 struct overlay_list *l;
5094 struct lang_nocrossref *nocrossref;
5096 lang_get_regions (&region, &lma_region,
5097 memspec, lma_memspec,
5098 lma_expr != 0);
5100 nocrossref = NULL;
5102 /* After setting the size of the last section, set '.' to end of the
5103 overlay region. */
5104 if (overlay_list != NULL)
5105 overlay_list->os->update_dot_tree
5106 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5108 l = overlay_list;
5109 while (l != NULL)
5111 struct overlay_list *next;
5113 if (fill != (fill_type *) 0 && l->os->fill == (fill_type *) 0)
5114 l->os->fill = fill;
5116 l->os->region = region;
5117 l->os->lma_region = lma_region;
5119 /* The first section has the load address specified in the
5120 OVERLAY statement. The rest are worked out from that.
5121 The base address is not needed (and should be null) if
5122 an LMA region was specified. */
5123 if (l->next == 0)
5124 l->os->load_base = lma_expr;
5125 else if (lma_region == 0)
5126 l->os->load_base = exp_binop ('+',
5127 exp_nameop (LOADADDR, l->next->os->name),
5128 exp_nameop (SIZEOF, l->next->os->name));
5130 if (phdrs != NULL && l->os->phdrs == NULL)
5131 l->os->phdrs = phdrs;
5133 if (nocrossrefs)
5135 struct lang_nocrossref *nc;
5137 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
5138 nc->name = l->os->name;
5139 nc->next = nocrossref;
5140 nocrossref = nc;
5143 next = l->next;
5144 free (l);
5145 l = next;
5148 if (nocrossref != NULL)
5149 lang_add_nocrossref (nocrossref);
5151 overlay_vma = NULL;
5152 overlay_list = NULL;
5153 overlay_max = NULL;
5156 /* Version handling. This is only useful for ELF. */
5158 /* This global variable holds the version tree that we build. */
5160 struct bfd_elf_version_tree *lang_elf_version_info;
5162 static int
5163 lang_vers_match_lang_c (expr, sym)
5164 struct bfd_elf_version_expr *expr;
5165 const char *sym;
5167 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5168 return 1;
5169 return fnmatch (expr->pattern, sym, 0) == 0;
5172 static int
5173 lang_vers_match_lang_cplusplus (expr, sym)
5174 struct bfd_elf_version_expr *expr;
5175 const char *sym;
5177 char *alt_sym;
5178 int result;
5180 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5181 return 1;
5183 alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
5184 if (!alt_sym)
5186 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
5187 Should we early out FALSE in this case? */
5188 result = fnmatch (expr->pattern, sym, 0) == 0;
5190 else
5192 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5193 free (alt_sym);
5196 return result;
5199 static int
5200 lang_vers_match_lang_java (expr, sym)
5201 struct bfd_elf_version_expr *expr;
5202 const char *sym;
5204 char *alt_sym;
5205 int result;
5207 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5208 return 1;
5210 alt_sym = cplus_demangle (sym, DMGL_JAVA);
5211 if (!alt_sym)
5213 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
5214 Should we early out FALSE in this case? */
5215 result = fnmatch (expr->pattern, sym, 0) == 0;
5217 else
5219 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5220 free (alt_sym);
5223 return result;
5226 /* This is called for each variable name or match expression. */
5228 struct bfd_elf_version_expr *
5229 lang_new_vers_pattern (orig, new, lang)
5230 struct bfd_elf_version_expr *orig;
5231 const char *new;
5232 const char *lang;
5234 struct bfd_elf_version_expr *ret;
5236 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
5237 ret->next = orig;
5238 ret->pattern = new;
5239 ret->symver = 0;
5240 ret->script = 0;
5242 if (lang == NULL || strcasecmp (lang, "C") == 0)
5243 ret->match = lang_vers_match_lang_c;
5244 else if (strcasecmp (lang, "C++") == 0)
5245 ret->match = lang_vers_match_lang_cplusplus;
5246 else if (strcasecmp (lang, "Java") == 0)
5247 ret->match = lang_vers_match_lang_java;
5248 else
5250 einfo (_("%X%P: unknown language `%s' in version information\n"),
5251 lang);
5252 ret->match = lang_vers_match_lang_c;
5255 return ldemul_new_vers_pattern (ret);
5258 /* This is called for each set of variable names and match
5259 expressions. */
5261 struct bfd_elf_version_tree *
5262 lang_new_vers_node (globals, locals)
5263 struct bfd_elf_version_expr *globals;
5264 struct bfd_elf_version_expr *locals;
5266 struct bfd_elf_version_tree *ret;
5268 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
5269 ret->next = NULL;
5270 ret->name = NULL;
5271 ret->vernum = 0;
5272 ret->globals = globals;
5273 ret->locals = locals;
5274 ret->deps = NULL;
5275 ret->name_indx = (unsigned int) -1;
5276 ret->used = 0;
5277 return ret;
5280 /* This static variable keeps track of version indices. */
5282 static int version_index;
5284 /* This is called when we know the name and dependencies of the
5285 version. */
5287 void
5288 lang_register_vers_node (name, version, deps)
5289 const char *name;
5290 struct bfd_elf_version_tree *version;
5291 struct bfd_elf_version_deps *deps;
5293 struct bfd_elf_version_tree *t, **pp;
5294 struct bfd_elf_version_expr *e1;
5296 if (name == NULL)
5297 name = "";
5299 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5300 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5302 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5303 free (version);
5304 return;
5307 /* Make sure this node has a unique name. */
5308 for (t = lang_elf_version_info; t != NULL; t = t->next)
5309 if (strcmp (t->name, name) == 0)
5310 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5312 /* Check the global and local match names, and make sure there
5313 aren't any duplicates. */
5315 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
5317 for (t = lang_elf_version_info; t != NULL; t = t->next)
5319 struct bfd_elf_version_expr *e2;
5321 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
5322 if (strcmp (e1->pattern, e2->pattern) == 0)
5323 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5324 e1->pattern);
5328 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
5330 for (t = lang_elf_version_info; t != NULL; t = t->next)
5332 struct bfd_elf_version_expr *e2;
5334 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
5335 if (strcmp (e1->pattern, e2->pattern) == 0)
5336 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5337 e1->pattern);
5341 version->deps = deps;
5342 version->name = name;
5343 if (name[0] != '\0')
5345 ++version_index;
5346 version->vernum = version_index;
5348 else
5349 version->vernum = 0;
5351 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5353 *pp = version;
5356 /* This is called when we see a version dependency. */
5358 struct bfd_elf_version_deps *
5359 lang_add_vers_depend (list, name)
5360 struct bfd_elf_version_deps *list;
5361 const char *name;
5363 struct bfd_elf_version_deps *ret;
5364 struct bfd_elf_version_tree *t;
5366 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
5367 ret->next = list;
5369 for (t = lang_elf_version_info; t != NULL; t = t->next)
5371 if (strcmp (t->name, name) == 0)
5373 ret->version_needed = t;
5374 return ret;
5378 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5380 return ret;
5383 static void
5384 lang_do_version_exports_section ()
5386 struct bfd_elf_version_expr *greg = NULL, *lreg;
5388 LANG_FOR_EACH_INPUT_STATEMENT (is)
5390 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5391 char *contents, *p;
5392 bfd_size_type len;
5394 if (sec == NULL)
5395 continue;
5397 len = bfd_section_size (is->the_bfd, sec);
5398 contents = xmalloc (len);
5399 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5400 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5402 p = contents;
5403 while (p < contents + len)
5405 greg = lang_new_vers_pattern (greg, p, NULL);
5406 p = strchr (p, '\0') + 1;
5409 /* Do not free the contents, as we used them creating the regex. */
5411 /* Do not include this section in the link. */
5412 bfd_set_section_flags (is->the_bfd, sec,
5413 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5416 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5417 lang_register_vers_node (command_line.version_exports_section,
5418 lang_new_vers_node (greg, lreg), NULL);
5421 void
5422 lang_add_unique (name)
5423 const char *name;
5425 struct unique_sections *ent;
5427 for (ent = unique_section_list; ent; ent = ent->next)
5428 if (strcmp (ent->name, name) == 0)
5429 return;
5431 ent = (struct unique_sections *) xmalloc (sizeof *ent);
5432 ent->name = xstrdup (name);
5433 ent->next = unique_section_list;
5434 unique_section_list = ent;