Abort if the filename to be added matches the linker output filename.
[binutils.git] / ld / ldlang.c
bloba7a393298b6e26c30f4d00e4b1858b7e47ba99dd
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002
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 /* FORWARDS */
48 static lang_statement_union_type *new_statement
49 PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
51 /* LOCALS */
52 static struct obstack stat_obstack;
54 #define obstack_chunk_alloc xmalloc
55 #define obstack_chunk_free free
56 static const char *startup_file;
57 static lang_statement_list_type input_file_chain;
58 static boolean placed_commons = false;
59 static lang_output_section_statement_type *default_common_section;
60 static boolean map_option_f;
61 static bfd_vma print_dot;
62 static lang_input_statement_type *first_file;
63 static const char *current_target;
64 static const char *output_target;
65 static lang_statement_list_type statement_list;
66 static struct lang_phdr *lang_phdr_list;
68 static void lang_for_each_statement_worker
69 PARAMS ((void (*) (lang_statement_union_type *),
70 lang_statement_union_type *));
71 static lang_input_statement_type *new_afile
72 PARAMS ((const char *, lang_input_file_enum_type, const char *, boolean));
73 static lang_memory_region_type *lang_memory_default PARAMS ((asection *));
74 static void lang_map_flags PARAMS ((flagword));
75 static void init_os PARAMS ((lang_output_section_statement_type *));
76 static void exp_init_os PARAMS ((etree_type *));
77 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
78 static struct bfd_hash_entry *already_linked_newfunc
79 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
80 static void already_linked_table_init PARAMS ((void));
81 static void already_linked_table_free PARAMS ((void));
82 static boolean wildcardp PARAMS ((const char *));
83 static lang_statement_union_type *wild_sort
84 PARAMS ((lang_wild_statement_type *, struct wildcard_list *,
85 lang_input_statement_type *, asection *));
86 static void output_section_callback
87 PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
88 lang_input_statement_type *, PTR));
89 static lang_input_statement_type *lookup_name PARAMS ((const char *));
90 static boolean load_symbols
91 PARAMS ((lang_input_statement_type *, lang_statement_list_type *));
92 static void wild
93 PARAMS ((lang_wild_statement_type *,
94 const char *, lang_output_section_statement_type *));
95 static bfd *open_output PARAMS ((const char *));
96 static void ldlang_open_output PARAMS ((lang_statement_union_type *));
97 static void open_input_bfds PARAMS ((lang_statement_union_type *, boolean));
98 static void lang_reasonable_defaults PARAMS ((void));
99 static void insert_undefined PARAMS ((const char *));
100 static void lang_place_undefineds PARAMS ((void));
101 static void map_input_to_output_sections
102 PARAMS ((lang_statement_union_type *, const char *,
103 lang_output_section_statement_type *));
104 static void strip_excluded_output_sections PARAMS ((void));
105 static void print_output_section_statement
106 PARAMS ((lang_output_section_statement_type *));
107 static void print_assignment
108 PARAMS ((lang_assignment_statement_type *,
109 lang_output_section_statement_type *));
110 static void print_input_statement PARAMS ((lang_input_statement_type *));
111 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
112 static void print_input_section PARAMS ((lang_input_section_type *));
113 static void print_fill_statement PARAMS ((lang_fill_statement_type *));
114 static void print_data_statement PARAMS ((lang_data_statement_type *));
115 static void print_address_statement PARAMS ((lang_address_statement_type *));
116 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *));
117 static void print_padding_statement PARAMS ((lang_padding_statement_type *));
118 static void print_wild_statement
119 PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
120 static void print_group
121 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
122 static void print_statement
123 PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
124 static void print_statement_list
125 PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
126 static void print_statements PARAMS ((void));
127 static void insert_pad
128 PARAMS ((lang_statement_union_type **, fill_type *,
129 unsigned int, asection *, bfd_vma));
130 static bfd_vma size_input_section
131 PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
132 fill_type *, bfd_vma));
133 static void lang_finish PARAMS ((void));
134 static void ignore_bfd_errors PARAMS ((const char *, ...));
135 static void lang_check PARAMS ((void));
136 static void lang_common PARAMS ((void));
137 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
138 static void lang_place_orphans PARAMS ((void));
139 static int topower PARAMS ((int));
140 static void lang_set_startof PARAMS ((void));
141 static void gc_section_callback
142 PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
143 lang_input_statement_type *, PTR));
144 static void lang_get_regions PARAMS ((struct memory_region_struct **,
145 struct memory_region_struct **,
146 const char *, const char *, int));
147 static void lang_record_phdrs PARAMS ((void));
148 static void lang_gc_wild PARAMS ((lang_wild_statement_type *));
149 static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
150 static void lang_gc_sections PARAMS ((void));
151 static int lang_vers_match_lang_c
152 PARAMS ((struct bfd_elf_version_expr *, const char *));
153 static int lang_vers_match_lang_cplusplus
154 PARAMS ((struct bfd_elf_version_expr *, const char *));
155 static int lang_vers_match_lang_java
156 PARAMS ((struct bfd_elf_version_expr *, const char *));
157 static void lang_do_version_exports_section PARAMS ((void));
158 static void lang_check_section_addresses PARAMS ((void));
159 static void os_region_check
160 PARAMS ((lang_output_section_statement_type *,
161 struct memory_region_struct *, etree_type *, bfd_vma));
162 static bfd_vma lang_size_sections_1
163 PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *,
164 lang_statement_union_type **, fill_type *, bfd_vma, boolean *));
166 typedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
167 struct wildcard_list *,
168 asection *,
169 lang_input_statement_type *,
170 PTR));
171 static void walk_wild
172 PARAMS ((lang_wild_statement_type *, callback_t, PTR));
173 static void walk_wild_section
174 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
175 callback_t, PTR));
176 static void walk_wild_file
177 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
178 callback_t, PTR));
180 static int get_target PARAMS ((const bfd_target *, PTR));
181 static void stricpy PARAMS ((char *, char *));
182 static void strcut PARAMS ((char *, char *));
183 static int name_compare PARAMS ((char *, char *));
184 static int closest_target_match PARAMS ((const bfd_target *, PTR));
185 static char * get_first_input_target PARAMS ((void));
187 /* EXPORTS */
188 lang_output_section_statement_type *abs_output_section;
189 lang_statement_list_type lang_output_section_statement;
190 lang_statement_list_type *stat_ptr = &statement_list;
191 lang_statement_list_type file_chain = { NULL, NULL };
192 struct bfd_sym_chain entry_symbol = { NULL, NULL };
193 const char *entry_section = ".text";
194 boolean entry_from_cmdline;
195 boolean lang_has_input_file = false;
196 boolean had_output_filename = false;
197 boolean lang_float_flag = false;
198 boolean delete_output_file_on_failure = false;
199 struct lang_nocrossrefs *nocrossref_list;
200 struct unique_sections *unique_section_list;
202 etree_type *base; /* Relocation base - or null */
204 #if defined (__STDC__) || defined (ALMOST_STDC)
205 #define cat(a,b) a##b
206 #else
207 #define cat(a,b) a/**/b
208 #endif
210 /* Don't beautify the line below with "innocent" whitespace, it breaks
211 the K&R C preprocessor! */
212 #define new_stat(x, y) \
213 (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
215 #define outside_section_address(q) \
216 ((q)->output_offset + (q)->output_section->vma)
218 #define outside_symbol_address(q) \
219 ((q)->value + outside_section_address (q->section))
221 #define SECTION_NAME_MAP_LENGTH (16)
224 stat_alloc (size)
225 size_t size;
227 return obstack_alloc (&stat_obstack, size);
230 boolean
231 unique_section_p (secnam)
232 const char *secnam;
234 struct unique_sections *unam;
236 for (unam = unique_section_list; unam; unam = unam->next)
237 if (wildcardp (unam->name)
238 ? fnmatch (unam->name, secnam, 0) == 0
239 : strcmp (unam->name, secnam) == 0)
241 return true;
244 return false;
247 /* Generic traversal routines for finding matching sections. */
249 static void
250 walk_wild_section (ptr, file, callback, data)
251 lang_wild_statement_type *ptr;
252 lang_input_statement_type *file;
253 callback_t callback;
254 PTR data;
256 asection *s;
258 if (file->just_syms_flag)
259 return;
261 for (s = file->the_bfd->sections; s != NULL; s = s->next)
263 struct wildcard_list *sec;
265 sec = ptr->section_list;
266 if (sec == NULL)
267 (*callback) (ptr, sec, s, file, data);
269 while (sec != NULL)
271 boolean skip = false;
272 struct name_list *list_tmp;
274 /* Don't process sections from files which were
275 excluded. */
276 for (list_tmp = sec->spec.exclude_name_list;
277 list_tmp;
278 list_tmp = list_tmp->next)
280 if (wildcardp (list_tmp->name))
281 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
282 else
283 skip = strcmp (list_tmp->name, file->filename) == 0;
285 /* If this file is part of an archive, and the archive is
286 excluded, exclude this file. */
287 if (! skip && file->the_bfd != NULL
288 && file->the_bfd->my_archive != NULL
289 && file->the_bfd->my_archive->filename != NULL)
291 if (wildcardp (list_tmp->name))
292 skip = fnmatch (list_tmp->name,
293 file->the_bfd->my_archive->filename,
294 0) == 0;
295 else
296 skip = strcmp (list_tmp->name,
297 file->the_bfd->my_archive->filename) == 0;
300 if (skip)
301 break;
304 if (!skip && sec->spec.name != NULL)
306 const char *sname = bfd_get_section_name (file->the_bfd, s);
308 if (wildcardp (sec->spec.name))
309 skip = fnmatch (sec->spec.name, sname, 0) != 0;
310 else
311 skip = strcmp (sec->spec.name, sname) != 0;
314 if (!skip)
315 (*callback) (ptr, sec, s, file, data);
317 sec = sec->next;
322 /* Handle a wild statement for a single file F. */
324 static void
325 walk_wild_file (s, f, callback, data)
326 lang_wild_statement_type *s;
327 lang_input_statement_type *f;
328 callback_t callback;
329 PTR data;
331 if (f->the_bfd == NULL
332 || ! bfd_check_format (f->the_bfd, bfd_archive))
333 walk_wild_section (s, f, callback, data);
334 else
336 bfd *member;
338 /* This is an archive file. We must map each member of the
339 archive separately. */
340 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
341 while (member != NULL)
343 /* When lookup_name is called, it will call the add_symbols
344 entry point for the archive. For each element of the
345 archive which is included, BFD will call ldlang_add_file,
346 which will set the usrdata field of the member to the
347 lang_input_statement. */
348 if (member->usrdata != NULL)
350 walk_wild_section (s,
351 (lang_input_statement_type *) member->usrdata,
352 callback, data);
355 member = bfd_openr_next_archived_file (f->the_bfd, member);
360 static void
361 walk_wild (s, callback, data)
362 lang_wild_statement_type *s;
363 callback_t callback;
364 PTR data;
366 const char *file_spec = s->filename;
368 if (file_spec == NULL)
370 /* Perform the iteration over all files in the list. */
371 LANG_FOR_EACH_INPUT_STATEMENT (f)
373 walk_wild_file (s, f, callback, data);
376 else if (wildcardp (file_spec))
378 LANG_FOR_EACH_INPUT_STATEMENT (f)
380 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
381 walk_wild_file (s, f, callback, data);
384 else
386 lang_input_statement_type *f;
388 /* Perform the iteration over a single file. */
389 f = lookup_name (file_spec);
390 if (f)
391 walk_wild_file (s, f, callback, data);
395 /* lang_for_each_statement walks the parse tree and calls the provided
396 function for each node. */
398 static void
399 lang_for_each_statement_worker (func, s)
400 void (*func) PARAMS ((lang_statement_union_type *));
401 lang_statement_union_type *s;
403 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
405 func (s);
407 switch (s->header.type)
409 case lang_constructors_statement_enum:
410 lang_for_each_statement_worker (func, constructor_list.head);
411 break;
412 case lang_output_section_statement_enum:
413 lang_for_each_statement_worker
414 (func,
415 s->output_section_statement.children.head);
416 break;
417 case lang_wild_statement_enum:
418 lang_for_each_statement_worker
419 (func,
420 s->wild_statement.children.head);
421 break;
422 case lang_group_statement_enum:
423 lang_for_each_statement_worker (func,
424 s->group_statement.children.head);
425 break;
426 case lang_data_statement_enum:
427 case lang_reloc_statement_enum:
428 case lang_object_symbols_statement_enum:
429 case lang_output_statement_enum:
430 case lang_target_statement_enum:
431 case lang_input_section_enum:
432 case lang_input_statement_enum:
433 case lang_assignment_statement_enum:
434 case lang_padding_statement_enum:
435 case lang_address_statement_enum:
436 case lang_fill_statement_enum:
437 break;
438 default:
439 FAIL ();
440 break;
445 void
446 lang_for_each_statement (func)
447 void (*func) PARAMS ((lang_statement_union_type *));
449 lang_for_each_statement_worker (func, statement_list.head);
452 /*----------------------------------------------------------------------*/
454 void
455 lang_list_init (list)
456 lang_statement_list_type *list;
458 list->head = (lang_statement_union_type *) NULL;
459 list->tail = &list->head;
462 /* Check if a file exist in the input_file_chain list. */
464 boolean
465 lang_file_exists (name)
466 const char *name;
468 lang_input_statement_type *p;
470 if (name == NULL)
471 return false;
473 for (p = (lang_input_statement_type *) input_file_chain.head;
474 p != (lang_input_statement_type *) NULL;
475 p = (lang_input_statement_type *) p->next_real_file)
476 if (p->filename != (char *) NULL
477 && strcmp (p->filename, name) == 0)
478 return true;
480 return false;
483 /* Build a new statement node for the parse tree. */
485 static lang_statement_union_type *
486 new_statement (type, size, list)
487 enum statement_enum type;
488 size_t size;
489 lang_statement_list_type *list;
491 lang_statement_union_type *new = (lang_statement_union_type *)
492 stat_alloc (size);
494 new->header.type = type;
495 new->header.next = (lang_statement_union_type *) NULL;
496 lang_statement_append (list, new, &new->header.next);
497 return new;
500 /* Build a new input file node for the language. There are several
501 ways in which we treat an input file, eg, we only look at symbols,
502 or prefix it with a -l etc.
504 We can be supplied with requests for input files more than once;
505 they may, for example be split over serveral lines like foo.o(.text)
506 foo.o(.data) etc, so when asked for a file we check that we haven't
507 got it already so we don't duplicate the bfd. */
509 static lang_input_statement_type *
510 new_afile (name, file_type, target, add_to_list)
511 const char *name;
512 lang_input_file_enum_type file_type;
513 const char *target;
514 boolean add_to_list;
516 lang_input_statement_type *p;
518 /* We abort if an input file name is identical with the output file name. */
519 if (name != NULL && output_filename != NULL
520 && !strcmp (name, output_filename))
521 einfo ("%P%F: input file %s is also the output file!\n", name);
523 if (add_to_list)
524 p = new_stat (lang_input_statement, stat_ptr);
525 else
527 p = ((lang_input_statement_type *)
528 stat_alloc (sizeof (lang_input_statement_type)));
529 p->header.next = NULL;
532 lang_has_input_file = true;
533 p->target = target;
534 switch (file_type)
536 case lang_input_file_is_symbols_only_enum:
537 p->filename = name;
538 p->is_archive = false;
539 p->real = true;
540 p->local_sym_name = name;
541 p->just_syms_flag = true;
542 p->search_dirs_flag = false;
543 break;
544 case lang_input_file_is_fake_enum:
545 p->filename = name;
546 p->is_archive = false;
547 p->real = false;
548 p->local_sym_name = name;
549 p->just_syms_flag = false;
550 p->search_dirs_flag = false;
551 break;
552 case lang_input_file_is_l_enum:
553 p->is_archive = true;
554 p->filename = name;
555 p->real = true;
556 p->local_sym_name = concat ("-l", name, (const char *) NULL);
557 p->just_syms_flag = false;
558 p->search_dirs_flag = true;
559 break;
560 case lang_input_file_is_marker_enum:
561 p->filename = name;
562 p->is_archive = false;
563 p->real = false;
564 p->local_sym_name = name;
565 p->just_syms_flag = false;
566 p->search_dirs_flag = true;
567 break;
568 case lang_input_file_is_search_file_enum:
569 p->filename = name;
570 p->is_archive = false;
571 p->real = true;
572 p->local_sym_name = name;
573 p->just_syms_flag = false;
574 p->search_dirs_flag = true;
575 break;
576 case lang_input_file_is_file_enum:
577 p->filename = name;
578 p->is_archive = false;
579 p->real = true;
580 p->local_sym_name = name;
581 p->just_syms_flag = false;
582 p->search_dirs_flag = false;
583 break;
584 default:
585 FAIL ();
587 p->the_bfd = (bfd *) NULL;
588 p->asymbols = (asymbol **) NULL;
589 p->next_real_file = (lang_statement_union_type *) NULL;
590 p->next = (lang_statement_union_type *) NULL;
591 p->symbol_count = 0;
592 p->dynamic = config.dynamic_link;
593 p->whole_archive = whole_archive;
594 p->loaded = false;
595 lang_statement_append (&input_file_chain,
596 (lang_statement_union_type *) p,
597 &p->next_real_file);
598 return p;
601 lang_input_statement_type *
602 lang_add_input_file (name, file_type, target)
603 const char *name;
604 lang_input_file_enum_type file_type;
605 const char *target;
607 lang_has_input_file = true;
608 return new_afile (name, file_type, target, true);
611 /* Build enough state so that the parser can build its tree. */
613 void
614 lang_init ()
616 obstack_begin (&stat_obstack, 1000);
618 stat_ptr = &statement_list;
620 lang_list_init (stat_ptr);
622 lang_list_init (&input_file_chain);
623 lang_list_init (&lang_output_section_statement);
624 lang_list_init (&file_chain);
625 first_file = lang_add_input_file ((char *) NULL,
626 lang_input_file_is_marker_enum,
627 (char *) NULL);
628 abs_output_section =
629 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
631 abs_output_section->bfd_section = bfd_abs_section_ptr;
635 /*----------------------------------------------------------------------
636 A region is an area of memory declared with the
637 MEMORY { name:org=exp, len=exp ... }
638 syntax.
640 We maintain a list of all the regions here.
642 If no regions are specified in the script, then the default is used
643 which is created when looked up to be the entire data space. */
645 static lang_memory_region_type *lang_memory_region_list;
646 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
648 lang_memory_region_type *
649 lang_memory_region_lookup (name)
650 const char *const name;
652 lang_memory_region_type *p;
654 /* NAME is NULL for LMA memspecs if no region was specified. */
655 if (name == NULL)
656 return NULL;
658 for (p = lang_memory_region_list;
659 p != (lang_memory_region_type *) NULL;
660 p = p->next)
662 if (strcmp (p->name, name) == 0)
664 return p;
668 #if 0
669 /* This code used to always use the first region in the list as the
670 default region. I changed it to instead use a region
671 encompassing all of memory as the default region. This permits
672 NOLOAD sections to work reasonably without requiring a region.
673 People should specify what region they mean, if they really want
674 a region. */
675 if (strcmp (name, "*default*") == 0)
677 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
679 return lang_memory_region_list;
682 #endif
685 lang_memory_region_type *new =
686 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
688 new->name = xstrdup (name);
689 new->next = (lang_memory_region_type *) NULL;
691 *lang_memory_region_list_tail = new;
692 lang_memory_region_list_tail = &new->next;
693 new->origin = 0;
694 new->flags = 0;
695 new->not_flags = 0;
696 new->length = ~(bfd_size_type) 0;
697 new->current = 0;
698 new->had_full_message = false;
700 return new;
704 static lang_memory_region_type *
705 lang_memory_default (section)
706 asection *section;
708 lang_memory_region_type *p;
710 flagword sec_flags = section->flags;
712 /* Override SEC_DATA to mean a writable section. */
713 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
714 sec_flags |= SEC_DATA;
716 for (p = lang_memory_region_list;
717 p != (lang_memory_region_type *) NULL;
718 p = p->next)
720 if ((p->flags & sec_flags) != 0
721 && (p->not_flags & sec_flags) == 0)
723 return p;
726 return lang_memory_region_lookup ("*default*");
729 lang_output_section_statement_type *
730 lang_output_section_find (name)
731 const char *const name;
733 lang_statement_union_type *u;
734 lang_output_section_statement_type *lookup;
736 for (u = lang_output_section_statement.head;
737 u != (lang_statement_union_type *) NULL;
738 u = lookup->next)
740 lookup = &u->output_section_statement;
741 if (strcmp (name, lookup->name) == 0)
743 return lookup;
746 return (lang_output_section_statement_type *) NULL;
749 lang_output_section_statement_type *
750 lang_output_section_statement_lookup (name)
751 const char *const name;
753 lang_output_section_statement_type *lookup;
755 lookup = lang_output_section_find (name);
756 if (lookup == (lang_output_section_statement_type *) NULL)
759 lookup = (lang_output_section_statement_type *)
760 new_stat (lang_output_section_statement, stat_ptr);
761 lookup->region = (lang_memory_region_type *) NULL;
762 lookup->lma_region = (lang_memory_region_type *) NULL;
763 lookup->fill = (fill_type *) 0;
764 lookup->block_value = 1;
765 lookup->name = name;
767 lookup->next = (lang_statement_union_type *) NULL;
768 lookup->bfd_section = (asection *) NULL;
769 lookup->processed = false;
770 lookup->sectype = normal_section;
771 lookup->addr_tree = (etree_type *) NULL;
772 lang_list_init (&lookup->children);
774 lookup->memspec = (const char *) NULL;
775 lookup->flags = 0;
776 lookup->subsection_alignment = -1;
777 lookup->section_alignment = -1;
778 lookup->load_base = (union etree_union *) NULL;
779 lookup->update_dot_tree = NULL;
780 lookup->phdrs = NULL;
782 lang_statement_append (&lang_output_section_statement,
783 (lang_statement_union_type *) lookup,
784 &lookup->next);
786 return lookup;
789 static void
790 lang_map_flags (flag)
791 flagword flag;
793 if (flag & SEC_ALLOC)
794 minfo ("a");
796 if (flag & SEC_CODE)
797 minfo ("x");
799 if (flag & SEC_READONLY)
800 minfo ("r");
802 if (flag & SEC_DATA)
803 minfo ("w");
805 if (flag & SEC_LOAD)
806 minfo ("l");
809 void
810 lang_map ()
812 lang_memory_region_type *m;
814 minfo (_("\nMemory Configuration\n\n"));
815 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
816 _("Name"), _("Origin"), _("Length"), _("Attributes"));
818 for (m = lang_memory_region_list;
819 m != (lang_memory_region_type *) NULL;
820 m = m->next)
822 char buf[100];
823 int len;
825 fprintf (config.map_file, "%-16s ", m->name);
827 sprintf_vma (buf, m->origin);
828 minfo ("0x%s ", buf);
829 len = strlen (buf);
830 while (len < 16)
832 print_space ();
833 ++len;
836 minfo ("0x%V", m->length);
837 if (m->flags || m->not_flags)
839 #ifndef BFD64
840 minfo (" ");
841 #endif
842 if (m->flags)
844 print_space ();
845 lang_map_flags (m->flags);
848 if (m->not_flags)
850 minfo (" !");
851 lang_map_flags (m->not_flags);
855 print_nl ();
858 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
860 print_statements ();
863 /* Initialize an output section. */
865 static void
866 init_os (s)
867 lang_output_section_statement_type *s;
869 section_userdata_type *new;
871 if (s->bfd_section != NULL)
872 return;
874 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
875 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
877 new = ((section_userdata_type *)
878 stat_alloc (sizeof (section_userdata_type)));
880 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
881 if (s->bfd_section == (asection *) NULL)
882 s->bfd_section = bfd_make_section (output_bfd, s->name);
883 if (s->bfd_section == (asection *) NULL)
885 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
886 output_bfd->xvec->name, s->name);
888 s->bfd_section->output_section = s->bfd_section;
890 /* We initialize an output sections output offset to minus its own
891 vma to allow us to output a section through itself. */
892 s->bfd_section->output_offset = 0;
893 get_userdata (s->bfd_section) = (PTR) new;
895 /* If there is a base address, make sure that any sections it might
896 mention are initialized. */
897 if (s->addr_tree != NULL)
898 exp_init_os (s->addr_tree);
901 /* Make sure that all output sections mentioned in an expression are
902 initialized. */
904 static void
905 exp_init_os (exp)
906 etree_type *exp;
908 switch (exp->type.node_class)
910 case etree_assign:
911 exp_init_os (exp->assign.src);
912 break;
914 case etree_binary:
915 exp_init_os (exp->binary.lhs);
916 exp_init_os (exp->binary.rhs);
917 break;
919 case etree_trinary:
920 exp_init_os (exp->trinary.cond);
921 exp_init_os (exp->trinary.lhs);
922 exp_init_os (exp->trinary.rhs);
923 break;
925 case etree_unary:
926 exp_init_os (exp->unary.child);
927 break;
929 case etree_name:
930 switch (exp->type.node_code)
932 case ADDR:
933 case LOADADDR:
934 case SIZEOF:
936 lang_output_section_statement_type *os;
938 os = lang_output_section_find (exp->name.name);
939 if (os != NULL && os->bfd_section == NULL)
940 init_os (os);
943 break;
945 default:
946 break;
950 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
951 once into the output. This routine checks each section, and
952 arrange to discard it if a section of the same name has already
953 been linked. If the section has COMDAT information, then it uses
954 that to decide whether the section should be included. This code
955 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
956 that is, it does not depend solely upon the section name.
957 section_already_linked is called via bfd_map_over_sections. */
959 /* This is the shape of the elements inside the already_linked hash
960 table. It maps a name onto a list of already_linked elements with
961 the same name. It's possible to get more than one element in a
962 list if the COMDAT sections have different names. */
964 struct already_linked_hash_entry
966 struct bfd_hash_entry root;
967 struct already_linked *entry;
970 struct already_linked
972 struct already_linked *next;
973 asection *sec;
976 /* The hash table. */
978 static struct bfd_hash_table already_linked_table;
980 static void
981 section_already_linked (abfd, sec, data)
982 bfd *abfd;
983 asection *sec;
984 PTR data;
986 lang_input_statement_type *entry = (lang_input_statement_type *) data;
987 flagword flags;
988 const char *name;
989 struct already_linked *l;
990 struct already_linked_hash_entry *already_linked_list;
992 /* If we are only reading symbols from this object, then we want to
993 discard all sections. */
994 if (entry->just_syms_flag)
996 bfd_link_just_syms (sec, &link_info);
997 return;
1000 flags = bfd_get_section_flags (abfd, sec);
1002 if ((flags & SEC_LINK_ONCE) == 0)
1003 return;
1005 /* FIXME: When doing a relocatable link, we may have trouble
1006 copying relocations in other sections that refer to local symbols
1007 in the section being discarded. Those relocations will have to
1008 be converted somehow; as of this writing I'm not sure that any of
1009 the backends handle that correctly.
1011 It is tempting to instead not discard link once sections when
1012 doing a relocatable link (technically, they should be discarded
1013 whenever we are building constructors). However, that fails,
1014 because the linker winds up combining all the link once sections
1015 into a single large link once section, which defeats the purpose
1016 of having link once sections in the first place.
1018 Also, not merging link once sections in a relocatable link
1019 causes trouble for MIPS ELF, which relies on link once semantics
1020 to handle the .reginfo section correctly. */
1022 name = bfd_get_section_name (abfd, sec);
1024 already_linked_list =
1025 ((struct already_linked_hash_entry *)
1026 bfd_hash_lookup (&already_linked_table, name, true, false));
1028 for (l = already_linked_list->entry; l != NULL; l = l->next)
1030 if (sec->comdat == NULL
1031 || l->sec->comdat == NULL
1032 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
1034 /* The section has already been linked. See if we should
1035 issue a warning. */
1036 switch (flags & SEC_LINK_DUPLICATES)
1038 default:
1039 abort ();
1041 case SEC_LINK_DUPLICATES_DISCARD:
1042 break;
1044 case SEC_LINK_DUPLICATES_ONE_ONLY:
1045 if (sec->comdat == NULL)
1046 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
1047 abfd, name);
1048 else
1049 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
1050 abfd, name, sec->comdat->name);
1051 break;
1053 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
1054 /* FIXME: We should really dig out the contents of both
1055 sections and memcmp them. The COFF/PE spec says that
1056 the Microsoft linker does not implement this
1057 correctly, so I'm not going to bother doing it
1058 either. */
1059 /* Fall through. */
1060 case SEC_LINK_DUPLICATES_SAME_SIZE:
1061 if (bfd_section_size (abfd, sec)
1062 != bfd_section_size (l->sec->owner, l->sec))
1063 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
1064 abfd, name);
1065 break;
1068 /* Set the output_section field so that lang_add_section
1069 does not create a lang_input_section structure for this
1070 section. */
1071 sec->output_section = bfd_abs_section_ptr;
1073 if (flags & SEC_GROUP)
1074 bfd_discard_group (abfd, sec);
1076 return;
1080 /* This is the first section with this name. Record it. Allocate
1081 the memory from the same obstack as the hash table is kept in. */
1083 l = ((struct already_linked *)
1084 bfd_hash_allocate (&already_linked_table, sizeof *l));
1086 l->sec = sec;
1087 l->next = already_linked_list->entry;
1088 already_linked_list->entry = l;
1091 /* Support routines for the hash table used by section_already_linked,
1092 initialize the table, fill in an entry and remove the table. */
1094 static struct bfd_hash_entry *
1095 already_linked_newfunc (entry, table, string)
1096 struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
1097 struct bfd_hash_table *table;
1098 const char *string ATTRIBUTE_UNUSED;
1100 struct already_linked_hash_entry *ret =
1101 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1103 ret->entry = NULL;
1105 return (struct bfd_hash_entry *) ret;
1108 static void
1109 already_linked_table_init ()
1111 if (! bfd_hash_table_init_n (&already_linked_table,
1112 already_linked_newfunc,
1113 42))
1114 einfo (_("%P%F: Failed to create hash table\n"));
1117 static void
1118 already_linked_table_free ()
1120 bfd_hash_table_free (&already_linked_table);
1123 /* The wild routines.
1125 These expand statements like *(.text) and foo.o to a list of
1126 explicit actions, like foo.o(.text), bar.o(.text) and
1127 foo.o(.text, .data). */
1129 /* Return true if the PATTERN argument is a wildcard pattern.
1130 Although backslashes are treated specially if a pattern contains
1131 wildcards, we do not consider the mere presence of a backslash to
1132 be enough to cause the pattern to be treated as a wildcard.
1133 That lets us handle DOS filenames more naturally. */
1135 static boolean
1136 wildcardp (pattern)
1137 const char *pattern;
1139 const char *s;
1141 for (s = pattern; *s != '\0'; ++s)
1142 if (*s == '?'
1143 || *s == '*'
1144 || *s == '[')
1145 return true;
1146 return false;
1149 /* Add SECTION to the output section OUTPUT. Do this by creating a
1150 lang_input_section statement which is placed at PTR. FILE is the
1151 input file which holds SECTION. */
1153 void
1154 lang_add_section (ptr, section, output, file)
1155 lang_statement_list_type *ptr;
1156 asection *section;
1157 lang_output_section_statement_type *output;
1158 lang_input_statement_type *file;
1160 flagword flags;
1161 boolean discard;
1163 flags = bfd_get_section_flags (section->owner, section);
1165 discard = false;
1167 /* If we are doing a final link, discard sections marked with
1168 SEC_EXCLUDE. */
1169 if (! link_info.relocateable
1170 && (flags & SEC_EXCLUDE) != 0)
1171 discard = true;
1173 /* Discard input sections which are assigned to a section named
1174 DISCARD_SECTION_NAME. */
1175 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1176 discard = true;
1178 /* Discard debugging sections if we are stripping debugging
1179 information. */
1180 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1181 && (flags & SEC_DEBUGGING) != 0)
1182 discard = true;
1184 if (discard)
1186 if (section->output_section == NULL)
1188 /* This prevents future calls from assigning this section. */
1189 section->output_section = bfd_abs_section_ptr;
1191 return;
1194 if (section->output_section == NULL)
1196 boolean first;
1197 lang_input_section_type *new;
1198 flagword flags;
1200 if (output->bfd_section == NULL)
1201 init_os (output);
1203 first = ! output->bfd_section->linker_has_input;
1204 output->bfd_section->linker_has_input = 1;
1206 /* Add a section reference to the list. */
1207 new = new_stat (lang_input_section, ptr);
1209 new->section = section;
1210 new->ifile = file;
1211 section->output_section = output->bfd_section;
1213 flags = section->flags;
1215 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1216 to an output section, because we want to be able to include a
1217 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1218 section (I don't know why we want to do this, but we do).
1219 build_link_order in ldwrite.c handles this case by turning
1220 the embedded SEC_NEVER_LOAD section into a fill. */
1222 flags &= ~ SEC_NEVER_LOAD;
1224 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1225 already been processed. One reason to do this is that on pe
1226 format targets, .text$foo sections go into .text and it's odd
1227 to see .text with SEC_LINK_ONCE set. */
1229 if (! link_info.relocateable)
1230 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1232 /* If this is not the first input section, and the SEC_READONLY
1233 flag is not currently set, then don't set it just because the
1234 input section has it set. */
1236 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1237 flags &= ~ SEC_READONLY;
1239 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1240 if (! first
1241 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1242 != (flags & (SEC_MERGE | SEC_STRINGS))
1243 || ((flags & SEC_MERGE)
1244 && section->output_section->entsize != section->entsize)))
1246 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1247 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1250 /* For now make .tbss normal section. */
1251 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocateable)
1252 flags |= SEC_LOAD;
1254 section->output_section->flags |= flags;
1256 if (flags & SEC_MERGE)
1257 section->output_section->entsize = section->entsize;
1259 /* If SEC_READONLY is not set in the input section, then clear
1260 it from the output section. */
1261 if ((section->flags & SEC_READONLY) == 0)
1262 section->output_section->flags &= ~SEC_READONLY;
1264 switch (output->sectype)
1266 case normal_section:
1267 break;
1268 case dsect_section:
1269 case copy_section:
1270 case info_section:
1271 case overlay_section:
1272 output->bfd_section->flags &= ~SEC_ALLOC;
1273 break;
1274 case noload_section:
1275 output->bfd_section->flags &= ~SEC_LOAD;
1276 output->bfd_section->flags |= SEC_NEVER_LOAD;
1277 break;
1280 /* Copy over SEC_SMALL_DATA. */
1281 if (section->flags & SEC_SMALL_DATA)
1282 section->output_section->flags |= SEC_SMALL_DATA;
1284 if (section->alignment_power > output->bfd_section->alignment_power)
1285 output->bfd_section->alignment_power = section->alignment_power;
1287 /* If supplied an aligment, then force it. */
1288 if (output->section_alignment != -1)
1289 output->bfd_section->alignment_power = output->section_alignment;
1291 if (section->flags & SEC_BLOCK)
1293 section->output_section->flags |= SEC_BLOCK;
1294 /* FIXME: This value should really be obtained from the bfd... */
1295 output->block_value = 128;
1300 /* Handle wildcard sorting. This returns the lang_input_section which
1301 should follow the one we are going to create for SECTION and FILE,
1302 based on the sorting requirements of WILD. It returns NULL if the
1303 new section should just go at the end of the current list. */
1305 static lang_statement_union_type *
1306 wild_sort (wild, sec, file, section)
1307 lang_wild_statement_type *wild;
1308 struct wildcard_list *sec;
1309 lang_input_statement_type *file;
1310 asection *section;
1312 const char *section_name;
1313 lang_statement_union_type *l;
1315 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1316 return NULL;
1318 section_name = bfd_get_section_name (file->the_bfd, section);
1319 for (l = wild->children.head; l != NULL; l = l->header.next)
1321 lang_input_section_type *ls;
1323 if (l->header.type != lang_input_section_enum)
1324 continue;
1325 ls = &l->input_section;
1327 /* Sorting by filename takes precedence over sorting by section
1328 name. */
1330 if (wild->filenames_sorted)
1332 const char *fn, *ln;
1333 boolean fa, la;
1334 int i;
1336 /* The PE support for the .idata section as generated by
1337 dlltool assumes that files will be sorted by the name of
1338 the archive and then the name of the file within the
1339 archive. */
1341 if (file->the_bfd != NULL
1342 && bfd_my_archive (file->the_bfd) != NULL)
1344 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1345 fa = true;
1347 else
1349 fn = file->filename;
1350 fa = false;
1353 if (ls->ifile->the_bfd != NULL
1354 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1356 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1357 la = true;
1359 else
1361 ln = ls->ifile->filename;
1362 la = false;
1365 i = strcmp (fn, ln);
1366 if (i > 0)
1367 continue;
1368 else if (i < 0)
1369 break;
1371 if (fa || la)
1373 if (fa)
1374 fn = file->filename;
1375 if (la)
1376 ln = ls->ifile->filename;
1378 i = strcmp (fn, ln);
1379 if (i > 0)
1380 continue;
1381 else if (i < 0)
1382 break;
1386 /* Here either the files are not sorted by name, or we are
1387 looking at the sections for this file. */
1389 if (sec != NULL && sec->spec.sorted)
1391 if (strcmp (section_name,
1392 bfd_get_section_name (ls->ifile->the_bfd,
1393 ls->section))
1394 < 0)
1395 break;
1399 return l;
1402 /* Expand a wild statement for a particular FILE. SECTION may be
1403 NULL, in which case it is a wild card. */
1405 static void
1406 output_section_callback (ptr, sec, section, file, output)
1407 lang_wild_statement_type *ptr;
1408 struct wildcard_list *sec;
1409 asection *section;
1410 lang_input_statement_type *file;
1411 PTR output;
1413 lang_statement_union_type *before;
1415 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1416 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1417 return;
1419 /* If the wild pattern was marked KEEP, the member sections
1420 should be as well. */
1421 if (ptr->keep_sections)
1422 section->flags |= SEC_KEEP;
1424 before = wild_sort (ptr, sec, file, section);
1426 /* Here BEFORE points to the lang_input_section which
1427 should follow the one we are about to add. If BEFORE
1428 is NULL, then the section should just go at the end
1429 of the current list. */
1431 if (before == NULL)
1432 lang_add_section (&ptr->children, section,
1433 (lang_output_section_statement_type *) output,
1434 file);
1435 else
1437 lang_statement_list_type list;
1438 lang_statement_union_type **pp;
1440 lang_list_init (&list);
1441 lang_add_section (&list, section,
1442 (lang_output_section_statement_type *) output,
1443 file);
1445 /* If we are discarding the section, LIST.HEAD will
1446 be NULL. */
1447 if (list.head != NULL)
1449 ASSERT (list.head->header.next == NULL);
1451 for (pp = &ptr->children.head;
1452 *pp != before;
1453 pp = &(*pp)->header.next)
1454 ASSERT (*pp != NULL);
1456 list.head->header.next = *pp;
1457 *pp = list.head;
1462 /* This is passed a file name which must have been seen already and
1463 added to the statement tree. We will see if it has been opened
1464 already and had its symbols read. If not then we'll read it. */
1466 static lang_input_statement_type *
1467 lookup_name (name)
1468 const char *name;
1470 lang_input_statement_type *search;
1472 for (search = (lang_input_statement_type *) input_file_chain.head;
1473 search != (lang_input_statement_type *) NULL;
1474 search = (lang_input_statement_type *) search->next_real_file)
1476 if (search->filename == (char *) NULL && name == (char *) NULL)
1477 return search;
1478 if (search->filename != (char *) NULL
1479 && name != (char *) NULL
1480 && strcmp (search->filename, name) == 0)
1481 break;
1484 if (search == (lang_input_statement_type *) NULL)
1485 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1486 false);
1488 /* If we have already added this file, or this file is not real
1489 (FIXME: can that ever actually happen?) or the name is NULL
1490 (FIXME: can that ever actually happen?) don't add this file. */
1491 if (search->loaded
1492 || ! search->real
1493 || search->filename == (const char *) NULL)
1494 return search;
1496 if (! load_symbols (search, (lang_statement_list_type *) NULL))
1497 return NULL;
1499 return search;
1502 /* Get the symbols for an input file. */
1504 static boolean
1505 load_symbols (entry, place)
1506 lang_input_statement_type *entry;
1507 lang_statement_list_type *place;
1509 char **matching;
1511 if (entry->loaded)
1512 return true;
1514 ldfile_open_file (entry);
1516 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1517 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1519 bfd_error_type err;
1520 lang_statement_list_type *hold;
1521 boolean bad_load = true;
1523 err = bfd_get_error ();
1525 /* See if the emulation has some special knowledge. */
1526 if (ldemul_unrecognized_file (entry))
1527 return true;
1529 if (err == bfd_error_file_ambiguously_recognized)
1531 char **p;
1533 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1534 einfo (_("%B: matching formats:"), entry->the_bfd);
1535 for (p = matching; *p != NULL; p++)
1536 einfo (" %s", *p);
1537 einfo ("%F\n");
1539 else if (err != bfd_error_file_not_recognized
1540 || place == NULL)
1541 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1542 else
1543 bad_load = false;
1545 bfd_close (entry->the_bfd);
1546 entry->the_bfd = NULL;
1548 /* Try to interpret the file as a linker script. */
1549 ldfile_open_command_file (entry->filename);
1551 hold = stat_ptr;
1552 stat_ptr = place;
1554 ldfile_assumed_script = true;
1555 parser_input = input_script;
1556 yyparse ();
1557 ldfile_assumed_script = false;
1559 stat_ptr = hold;
1561 return ! bad_load;
1564 if (ldemul_recognized_file (entry))
1565 return true;
1567 /* We don't call ldlang_add_file for an archive. Instead, the
1568 add_symbols entry point will call ldlang_add_file, via the
1569 add_archive_element callback, for each element of the archive
1570 which is used. */
1571 switch (bfd_get_format (entry->the_bfd))
1573 default:
1574 break;
1576 case bfd_object:
1577 ldlang_add_file (entry);
1578 if (trace_files || trace_file_tries)
1579 info_msg ("%I\n", entry);
1580 break;
1582 case bfd_archive:
1583 if (entry->whole_archive)
1585 bfd *member = NULL;
1586 boolean loaded = true;
1588 for (;;)
1590 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1592 if (member == NULL)
1593 break;
1595 if (! bfd_check_format (member, bfd_object))
1597 einfo (_("%F%B: member %B in archive is not an object\n"),
1598 entry->the_bfd, member);
1599 loaded = false;
1602 if (! ((*link_info.callbacks->add_archive_element)
1603 (&link_info, member, "--whole-archive")))
1604 abort ();
1606 if (! bfd_link_add_symbols (member, &link_info))
1608 einfo (_("%F%B: could not read symbols: %E\n"), member);
1609 loaded = false;
1613 entry->loaded = loaded;
1614 return loaded;
1616 break;
1619 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1620 entry->loaded = true;
1621 else
1622 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1624 return entry->loaded;
1627 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1628 may be NULL, indicating that it is a wildcard. Separate
1629 lang_input_section statements are created for each part of the
1630 expansion; they are added after the wild statement S. OUTPUT is
1631 the output section. */
1633 static void
1634 wild (s, target, output)
1635 lang_wild_statement_type *s;
1636 const char *target ATTRIBUTE_UNUSED;
1637 lang_output_section_statement_type *output;
1639 struct wildcard_list *sec;
1641 walk_wild (s, output_section_callback, (PTR) output);
1643 for (sec = s->section_list; sec != NULL; sec = sec->next)
1645 if (default_common_section != NULL)
1646 break;
1647 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1649 /* Remember the section that common is going to in case we
1650 later get something which doesn't know where to put it. */
1651 default_common_section = output;
1656 /* Return true iff target is the sought target. */
1658 static int
1659 get_target (target, data)
1660 const bfd_target *target;
1661 PTR data;
1663 const char *sought = (const char *) data;
1665 return strcmp (target->name, sought) == 0;
1668 /* Like strcpy() but convert to lower case as well. */
1670 static void
1671 stricpy (dest, src)
1672 char *dest;
1673 char *src;
1675 char c;
1677 while ((c = *src++) != 0)
1678 *dest++ = TOLOWER (c);
1680 *dest = 0;
1683 /* Remove the first occurance of needle (if any) in haystack
1684 from haystack. */
1686 static void
1687 strcut (haystack, needle)
1688 char *haystack;
1689 char *needle;
1691 haystack = strstr (haystack, needle);
1693 if (haystack)
1695 char *src;
1697 for (src = haystack + strlen (needle); *src;)
1698 *haystack++ = *src++;
1700 *haystack = 0;
1704 /* Compare two target format name strings.
1705 Return a value indicating how "similar" they are. */
1707 static int
1708 name_compare (first, second)
1709 char *first;
1710 char *second;
1712 char *copy1;
1713 char *copy2;
1714 int result;
1716 copy1 = xmalloc (strlen (first) + 1);
1717 copy2 = xmalloc (strlen (second) + 1);
1719 /* Convert the names to lower case. */
1720 stricpy (copy1, first);
1721 stricpy (copy2, second);
1723 /* Remove and endian strings from the name. */
1724 strcut (copy1, "big");
1725 strcut (copy1, "little");
1726 strcut (copy2, "big");
1727 strcut (copy2, "little");
1729 /* Return a value based on how many characters match,
1730 starting from the beginning. If both strings are
1731 the same then return 10 * their length. */
1732 for (result = 0; copy1[result] == copy2[result]; result++)
1733 if (copy1[result] == 0)
1735 result *= 10;
1736 break;
1739 free (copy1);
1740 free (copy2);
1742 return result;
1745 /* Set by closest_target_match() below. */
1746 static const bfd_target *winner;
1748 /* Scan all the valid bfd targets looking for one that has the endianness
1749 requirement that was specified on the command line, and is the nearest
1750 match to the original output target. */
1752 static int
1753 closest_target_match (target, data)
1754 const bfd_target *target;
1755 PTR data;
1757 const bfd_target *original = (const bfd_target *) data;
1759 if (command_line.endian == ENDIAN_BIG
1760 && target->byteorder != BFD_ENDIAN_BIG)
1761 return 0;
1763 if (command_line.endian == ENDIAN_LITTLE
1764 && target->byteorder != BFD_ENDIAN_LITTLE)
1765 return 0;
1767 /* Must be the same flavour. */
1768 if (target->flavour != original->flavour)
1769 return 0;
1771 /* If we have not found a potential winner yet, then record this one. */
1772 if (winner == NULL)
1774 winner = target;
1775 return 0;
1778 /* Oh dear, we now have two potential candidates for a successful match.
1779 Compare their names and choose the better one. */
1780 if (name_compare (target->name, original->name)
1781 > name_compare (winner->name, original->name))
1782 winner = target;
1784 /* Keep on searching until wqe have checked them all. */
1785 return 0;
1788 /* Return the BFD target format of the first input file. */
1790 static char *
1791 get_first_input_target ()
1793 char *target = NULL;
1795 LANG_FOR_EACH_INPUT_STATEMENT (s)
1797 if (s->header.type == lang_input_statement_enum
1798 && s->real)
1800 ldfile_open_file (s);
1802 if (s->the_bfd != NULL
1803 && bfd_check_format (s->the_bfd, bfd_object))
1805 target = bfd_get_target (s->the_bfd);
1807 if (target != NULL)
1808 break;
1813 return target;
1816 /* Open the output file. */
1818 static bfd *
1819 open_output (name)
1820 const char *name;
1822 bfd *output;
1824 /* Has the user told us which output format to use? */
1825 if (output_target == (char *) NULL)
1827 /* No - has the current target been set to something other than
1828 the default? */
1829 if (current_target != default_target)
1830 output_target = current_target;
1832 /* No - can we determine the format of the first input file? */
1833 else
1835 output_target = get_first_input_target ();
1837 /* Failed - use the default output target. */
1838 if (output_target == NULL)
1839 output_target = default_target;
1843 /* Has the user requested a particular endianness on the command
1844 line? */
1845 if (command_line.endian != ENDIAN_UNSET)
1847 const bfd_target *target;
1848 enum bfd_endian desired_endian;
1850 /* Get the chosen target. */
1851 target = bfd_search_for_target (get_target, (PTR) output_target);
1853 /* If the target is not supported, we cannot do anything. */
1854 if (target != NULL)
1856 if (command_line.endian == ENDIAN_BIG)
1857 desired_endian = BFD_ENDIAN_BIG;
1858 else
1859 desired_endian = BFD_ENDIAN_LITTLE;
1861 /* See if the target has the wrong endianness. This should
1862 not happen if the linker script has provided big and
1863 little endian alternatives, but some scrips don't do
1864 this. */
1865 if (target->byteorder != desired_endian)
1867 /* If it does, then see if the target provides
1868 an alternative with the correct endianness. */
1869 if (target->alternative_target != NULL
1870 && (target->alternative_target->byteorder == desired_endian))
1871 output_target = target->alternative_target->name;
1872 else
1874 /* Try to find a target as similar as possible to
1875 the default target, but which has the desired
1876 endian characteristic. */
1877 (void) bfd_search_for_target (closest_target_match,
1878 (PTR) target);
1880 /* Oh dear - we could not find any targets that
1881 satisfy our requirements. */
1882 if (winner == NULL)
1883 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1884 else
1885 output_target = winner->name;
1891 output = bfd_openw (name, output_target);
1893 if (output == (bfd *) NULL)
1895 if (bfd_get_error () == bfd_error_invalid_target)
1896 einfo (_("%P%F: target %s not found\n"), output_target);
1898 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1901 delete_output_file_on_failure = true;
1903 #if 0
1904 output->flags |= D_PAGED;
1905 #endif
1907 if (! bfd_set_format (output, bfd_object))
1908 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1909 if (! bfd_set_arch_mach (output,
1910 ldfile_output_architecture,
1911 ldfile_output_machine))
1912 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1914 link_info.hash = bfd_link_hash_table_create (output);
1915 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1916 einfo (_("%P%F: can not create link hash table: %E\n"));
1918 bfd_set_gp_size (output, g_switch_value);
1919 return output;
1922 static void
1923 ldlang_open_output (statement)
1924 lang_statement_union_type *statement;
1926 switch (statement->header.type)
1928 case lang_output_statement_enum:
1929 ASSERT (output_bfd == (bfd *) NULL);
1930 output_bfd = open_output (statement->output_statement.name);
1931 ldemul_set_output_arch ();
1932 if (config.magic_demand_paged && !link_info.relocateable)
1933 output_bfd->flags |= D_PAGED;
1934 else
1935 output_bfd->flags &= ~D_PAGED;
1936 if (config.text_read_only)
1937 output_bfd->flags |= WP_TEXT;
1938 else
1939 output_bfd->flags &= ~WP_TEXT;
1940 if (link_info.traditional_format)
1941 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1942 else
1943 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1944 break;
1946 case lang_target_statement_enum:
1947 current_target = statement->target_statement.target;
1948 break;
1949 default:
1950 break;
1954 /* Open all the input files. */
1956 static void
1957 open_input_bfds (s, force)
1958 lang_statement_union_type *s;
1959 boolean force;
1961 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
1963 switch (s->header.type)
1965 case lang_constructors_statement_enum:
1966 open_input_bfds (constructor_list.head, force);
1967 break;
1968 case lang_output_section_statement_enum:
1969 open_input_bfds (s->output_section_statement.children.head, force);
1970 break;
1971 case lang_wild_statement_enum:
1972 /* Maybe we should load the file's symbols. */
1973 if (s->wild_statement.filename
1974 && ! wildcardp (s->wild_statement.filename))
1975 (void) lookup_name (s->wild_statement.filename);
1976 open_input_bfds (s->wild_statement.children.head, force);
1977 break;
1978 case lang_group_statement_enum:
1980 struct bfd_link_hash_entry *undefs;
1982 /* We must continually search the entries in the group
1983 until no new symbols are added to the list of undefined
1984 symbols. */
1988 undefs = link_info.hash->undefs_tail;
1989 open_input_bfds (s->group_statement.children.head, true);
1991 while (undefs != link_info.hash->undefs_tail);
1993 break;
1994 case lang_target_statement_enum:
1995 current_target = s->target_statement.target;
1996 break;
1997 case lang_input_statement_enum:
1998 if (s->input_statement.real)
2000 lang_statement_list_type add;
2002 s->input_statement.target = current_target;
2004 /* If we are being called from within a group, and this
2005 is an archive which has already been searched, then
2006 force it to be researched unless the whole archive
2007 has been loaded already. */
2008 if (force
2009 && !s->input_statement.whole_archive
2010 && s->input_statement.loaded
2011 && bfd_check_format (s->input_statement.the_bfd,
2012 bfd_archive))
2013 s->input_statement.loaded = false;
2015 lang_list_init (&add);
2017 if (! load_symbols (&s->input_statement, &add))
2018 config.make_executable = false;
2020 if (add.head != NULL)
2022 *add.tail = s->header.next;
2023 s->header.next = add.head;
2026 break;
2027 default:
2028 break;
2033 /* If there are [COMMONS] statements, put a wild one into the bss
2034 section. */
2036 static void
2037 lang_reasonable_defaults ()
2039 #if 0
2040 lang_output_section_statement_lookup (".text");
2041 lang_output_section_statement_lookup (".data");
2043 default_common_section = lang_output_section_statement_lookup (".bss");
2045 if (placed_commons == false)
2047 lang_wild_statement_type *new =
2048 new_stat (lang_wild_statement,
2049 &default_common_section->children);
2051 new->section_name = "COMMON";
2052 new->filename = (char *) NULL;
2053 lang_list_init (&new->children);
2055 #endif
2058 /* Add the supplied name to the symbol table as an undefined reference.
2059 This is a two step process as the symbol table doesn't even exist at
2060 the time the ld command line is processed. First we put the name
2061 on a list, then, once the output file has been opened, transfer the
2062 name to the symbol table. */
2064 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2066 #define ldlang_undef_chain_list_head entry_symbol.next
2068 void
2069 ldlang_add_undef (name)
2070 const char *const name;
2072 ldlang_undef_chain_list_type *new =
2073 ((ldlang_undef_chain_list_type *)
2074 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
2076 new->next = ldlang_undef_chain_list_head;
2077 ldlang_undef_chain_list_head = new;
2079 new->name = xstrdup (name);
2081 if (output_bfd != NULL)
2082 insert_undefined (new->name);
2085 /* Insert NAME as undefined in the symbol table. */
2087 static void
2088 insert_undefined (name)
2089 const char *name;
2091 struct bfd_link_hash_entry *h;
2093 h = bfd_link_hash_lookup (link_info.hash, name, true, false, true);
2094 if (h == (struct bfd_link_hash_entry *) NULL)
2095 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2096 if (h->type == bfd_link_hash_new)
2098 h->type = bfd_link_hash_undefined;
2099 h->u.undef.abfd = NULL;
2100 bfd_link_add_undef (link_info.hash, h);
2104 /* Run through the list of undefineds created above and place them
2105 into the linker hash table as undefined symbols belonging to the
2106 script file. */
2108 static void
2109 lang_place_undefineds ()
2111 ldlang_undef_chain_list_type *ptr;
2113 for (ptr = ldlang_undef_chain_list_head;
2114 ptr != (ldlang_undef_chain_list_type *) NULL;
2115 ptr = ptr->next)
2117 insert_undefined (ptr->name);
2121 /* Open input files and attatch to output sections. */
2123 static void
2124 map_input_to_output_sections (s, target, output_section_statement)
2125 lang_statement_union_type *s;
2126 const char *target;
2127 lang_output_section_statement_type *output_section_statement;
2129 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2131 switch (s->header.type)
2133 case lang_wild_statement_enum:
2134 wild (&s->wild_statement, target, output_section_statement);
2135 break;
2136 case lang_constructors_statement_enum:
2137 map_input_to_output_sections (constructor_list.head,
2138 target,
2139 output_section_statement);
2140 break;
2141 case lang_output_section_statement_enum:
2142 map_input_to_output_sections (s->output_section_statement.children.head,
2143 target,
2144 &s->output_section_statement);
2145 break;
2146 case lang_output_statement_enum:
2147 break;
2148 case lang_target_statement_enum:
2149 target = s->target_statement.target;
2150 break;
2151 case lang_group_statement_enum:
2152 map_input_to_output_sections (s->group_statement.children.head,
2153 target,
2154 output_section_statement);
2155 break;
2156 case lang_fill_statement_enum:
2157 case lang_input_section_enum:
2158 case lang_object_symbols_statement_enum:
2159 case lang_data_statement_enum:
2160 case lang_reloc_statement_enum:
2161 case lang_padding_statement_enum:
2162 case lang_input_statement_enum:
2163 if (output_section_statement != NULL
2164 && output_section_statement->bfd_section == NULL)
2165 init_os (output_section_statement);
2166 break;
2167 case lang_assignment_statement_enum:
2168 if (output_section_statement != NULL
2169 && output_section_statement->bfd_section == NULL)
2170 init_os (output_section_statement);
2172 /* Make sure that any sections mentioned in the assignment
2173 are initialized. */
2174 exp_init_os (s->assignment_statement.exp);
2175 break;
2176 case lang_afile_asection_pair_statement_enum:
2177 FAIL ();
2178 break;
2179 case lang_address_statement_enum:
2180 /* Mark the specified section with the supplied address. */
2182 lang_output_section_statement_type *os =
2183 lang_output_section_statement_lookup
2184 (s->address_statement.section_name);
2186 if (os->bfd_section == NULL)
2187 init_os (os);
2188 os->addr_tree = s->address_statement.address;
2190 break;
2195 /* An output section might have been removed after its statement was
2196 added. For example, ldemul_before_allocation can remove dynamic
2197 sections if they turn out to be not needed. Clean them up here. */
2199 static void
2200 strip_excluded_output_sections ()
2202 lang_statement_union_type *u;
2204 for (u = lang_output_section_statement.head;
2205 u != NULL;
2206 u = u->output_section_statement.next)
2208 lang_output_section_statement_type *os;
2209 asection *s;
2211 os = &u->output_section_statement;
2212 s = os->bfd_section;
2213 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2215 asection **p;
2217 os->bfd_section = NULL;
2219 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2220 if (*p == s)
2222 bfd_section_list_remove (output_bfd, p);
2223 output_bfd->section_count--;
2224 break;
2230 static void
2231 print_output_section_statement (output_section_statement)
2232 lang_output_section_statement_type *output_section_statement;
2234 asection *section = output_section_statement->bfd_section;
2235 int len;
2237 if (output_section_statement != abs_output_section)
2239 minfo ("\n%s", output_section_statement->name);
2241 if (section != NULL)
2243 print_dot = section->vma;
2245 len = strlen (output_section_statement->name);
2246 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2248 print_nl ();
2249 len = 0;
2251 while (len < SECTION_NAME_MAP_LENGTH)
2253 print_space ();
2254 ++len;
2257 minfo ("0x%V %W", section->vma, section->_raw_size);
2259 if (output_section_statement->load_base != NULL)
2261 bfd_vma addr;
2263 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2264 "load base", lang_final_phase_enum);
2265 minfo (_(" load address 0x%V"), addr);
2269 print_nl ();
2272 print_statement_list (output_section_statement->children.head,
2273 output_section_statement);
2276 static void
2277 print_assignment (assignment, output_section)
2278 lang_assignment_statement_type *assignment;
2279 lang_output_section_statement_type *output_section;
2281 int i;
2282 etree_value_type result;
2284 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2285 print_space ();
2287 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2288 lang_final_phase_enum, print_dot, &print_dot);
2289 if (result.valid_p)
2291 const char *dst;
2292 bfd_vma value;
2294 value = result.value + result.section->bfd_section->vma;
2295 dst = assignment->exp->assign.dst;
2297 minfo ("0x%V", value);
2298 if (dst[0] == '.' && dst[1] == 0)
2299 print_dot = value;
2301 else
2303 minfo ("*undef* ");
2304 #ifdef BFD64
2305 minfo (" ");
2306 #endif
2309 minfo (" ");
2311 exp_print_tree (assignment->exp);
2313 print_nl ();
2316 static void
2317 print_input_statement (statm)
2318 lang_input_statement_type *statm;
2320 if (statm->filename != (char *) NULL)
2322 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2326 /* Print all symbols defined in a particular section. This is called
2327 via bfd_link_hash_traverse. */
2329 static boolean
2330 print_one_symbol (hash_entry, ptr)
2331 struct bfd_link_hash_entry *hash_entry;
2332 PTR ptr;
2334 asection *sec = (asection *) ptr;
2336 if ((hash_entry->type == bfd_link_hash_defined
2337 || hash_entry->type == bfd_link_hash_defweak)
2338 && sec == hash_entry->u.def.section)
2340 int i;
2342 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2343 print_space ();
2344 minfo ("0x%V ",
2345 (hash_entry->u.def.value
2346 + hash_entry->u.def.section->output_offset
2347 + hash_entry->u.def.section->output_section->vma));
2349 minfo (" %T\n", hash_entry->root.string);
2352 return true;
2355 /* Print information about an input section to the map file. */
2357 static void
2358 print_input_section (in)
2359 lang_input_section_type *in;
2361 asection *i = in->section;
2362 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2363 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2364 ldfile_output_machine);
2365 if (size != 0)
2367 print_space ();
2369 minfo ("%s", i->name);
2371 if (i->output_section != NULL)
2373 int len;
2375 len = 1 + strlen (i->name);
2376 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2378 print_nl ();
2379 len = 0;
2381 while (len < SECTION_NAME_MAP_LENGTH)
2383 print_space ();
2384 ++len;
2387 minfo ("0x%V %W %B\n",
2388 i->output_section->vma + i->output_offset, size / opb,
2389 i->owner);
2391 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2393 len = SECTION_NAME_MAP_LENGTH + 3;
2394 #ifdef BFD64
2395 len += 16;
2396 #else
2397 len += 8;
2398 #endif
2399 while (len > 0)
2401 print_space ();
2402 --len;
2405 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2408 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
2410 print_dot = i->output_section->vma + i->output_offset + size / opb;
2415 static void
2416 print_fill_statement (fill)
2417 lang_fill_statement_type *fill;
2419 size_t size;
2420 unsigned char *p;
2421 fputs (" FILL mask 0x", config.map_file);
2422 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2423 fprintf (config.map_file, "%02x", *p);
2424 fputs ("\n", config.map_file);
2427 static void
2428 print_data_statement (data)
2429 lang_data_statement_type *data;
2431 int i;
2432 bfd_vma addr;
2433 bfd_size_type size;
2434 const char *name;
2435 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2436 ldfile_output_machine);
2438 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2439 print_space ();
2441 addr = data->output_vma;
2442 if (data->output_section != NULL)
2443 addr += data->output_section->vma;
2445 switch (data->type)
2447 default:
2448 abort ();
2449 case BYTE:
2450 size = BYTE_SIZE;
2451 name = "BYTE";
2452 break;
2453 case SHORT:
2454 size = SHORT_SIZE;
2455 name = "SHORT";
2456 break;
2457 case LONG:
2458 size = LONG_SIZE;
2459 name = "LONG";
2460 break;
2461 case QUAD:
2462 size = QUAD_SIZE;
2463 name = "QUAD";
2464 break;
2465 case SQUAD:
2466 size = QUAD_SIZE;
2467 name = "SQUAD";
2468 break;
2471 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2473 if (data->exp->type.node_class != etree_value)
2475 print_space ();
2476 exp_print_tree (data->exp);
2479 print_nl ();
2481 print_dot = addr + size / opb;
2485 /* Print an address statement. These are generated by options like
2486 -Ttext. */
2488 static void
2489 print_address_statement (address)
2490 lang_address_statement_type *address;
2492 minfo (_("Address of section %s set to "), address->section_name);
2493 exp_print_tree (address->address);
2494 print_nl ();
2497 /* Print a reloc statement. */
2499 static void
2500 print_reloc_statement (reloc)
2501 lang_reloc_statement_type *reloc;
2503 int i;
2504 bfd_vma addr;
2505 bfd_size_type size;
2506 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2507 ldfile_output_machine);
2509 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2510 print_space ();
2512 addr = reloc->output_vma;
2513 if (reloc->output_section != NULL)
2514 addr += reloc->output_section->vma;
2516 size = bfd_get_reloc_size (reloc->howto);
2518 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2520 if (reloc->name != NULL)
2521 minfo ("%s+", reloc->name);
2522 else
2523 minfo ("%s+", reloc->section->name);
2525 exp_print_tree (reloc->addend_exp);
2527 print_nl ();
2529 print_dot = addr + size / opb;
2532 static void
2533 print_padding_statement (s)
2534 lang_padding_statement_type *s;
2536 int len;
2537 bfd_vma addr;
2538 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2539 ldfile_output_machine);
2541 minfo (" *fill*");
2543 len = sizeof " *fill*" - 1;
2544 while (len < SECTION_NAME_MAP_LENGTH)
2546 print_space ();
2547 ++len;
2550 addr = s->output_offset;
2551 if (s->output_section != NULL)
2552 addr += s->output_section->vma;
2553 minfo ("0x%V %W ", addr, s->size);
2555 if (s->fill->size != 0)
2557 size_t size;
2558 unsigned char *p;
2559 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2560 fprintf (config.map_file, "%02x", *p);
2563 print_nl ();
2565 print_dot = addr + s->size / opb;
2568 static void
2569 print_wild_statement (w, os)
2570 lang_wild_statement_type *w;
2571 lang_output_section_statement_type *os;
2573 struct wildcard_list *sec;
2575 print_space ();
2577 if (w->filenames_sorted)
2578 minfo ("SORT(");
2579 if (w->filename != NULL)
2580 minfo ("%s", w->filename);
2581 else
2582 minfo ("*");
2583 if (w->filenames_sorted)
2584 minfo (")");
2586 minfo ("(");
2587 for (sec = w->section_list; sec; sec = sec->next)
2589 if (sec->spec.sorted)
2590 minfo ("SORT(");
2591 if (sec->spec.exclude_name_list != NULL)
2593 name_list *tmp;
2594 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2595 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2596 minfo (" %s", tmp->name);
2597 minfo (") ");
2599 if (sec->spec.name != NULL)
2600 minfo ("%s", sec->spec.name);
2601 else
2602 minfo ("*");
2603 if (sec->spec.sorted)
2604 minfo (")");
2605 if (sec->next)
2606 minfo (" ");
2608 minfo (")");
2610 print_nl ();
2612 print_statement_list (w->children.head, os);
2615 /* Print a group statement. */
2617 static void
2618 print_group (s, os)
2619 lang_group_statement_type *s;
2620 lang_output_section_statement_type *os;
2622 fprintf (config.map_file, "START GROUP\n");
2623 print_statement_list (s->children.head, os);
2624 fprintf (config.map_file, "END GROUP\n");
2627 /* Print the list of statements in S.
2628 This can be called for any statement type. */
2630 static void
2631 print_statement_list (s, os)
2632 lang_statement_union_type *s;
2633 lang_output_section_statement_type *os;
2635 while (s != NULL)
2637 print_statement (s, os);
2638 s = s->header.next;
2642 /* Print the first statement in statement list S.
2643 This can be called for any statement type. */
2645 static void
2646 print_statement (s, os)
2647 lang_statement_union_type *s;
2648 lang_output_section_statement_type *os;
2650 switch (s->header.type)
2652 default:
2653 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2654 FAIL ();
2655 break;
2656 case lang_constructors_statement_enum:
2657 if (constructor_list.head != NULL)
2659 if (constructors_sorted)
2660 minfo (" SORT (CONSTRUCTORS)\n");
2661 else
2662 minfo (" CONSTRUCTORS\n");
2663 print_statement_list (constructor_list.head, os);
2665 break;
2666 case lang_wild_statement_enum:
2667 print_wild_statement (&s->wild_statement, os);
2668 break;
2669 case lang_address_statement_enum:
2670 print_address_statement (&s->address_statement);
2671 break;
2672 case lang_object_symbols_statement_enum:
2673 minfo (" CREATE_OBJECT_SYMBOLS\n");
2674 break;
2675 case lang_fill_statement_enum:
2676 print_fill_statement (&s->fill_statement);
2677 break;
2678 case lang_data_statement_enum:
2679 print_data_statement (&s->data_statement);
2680 break;
2681 case lang_reloc_statement_enum:
2682 print_reloc_statement (&s->reloc_statement);
2683 break;
2684 case lang_input_section_enum:
2685 print_input_section (&s->input_section);
2686 break;
2687 case lang_padding_statement_enum:
2688 print_padding_statement (&s->padding_statement);
2689 break;
2690 case lang_output_section_statement_enum:
2691 print_output_section_statement (&s->output_section_statement);
2692 break;
2693 case lang_assignment_statement_enum:
2694 print_assignment (&s->assignment_statement, os);
2695 break;
2696 case lang_target_statement_enum:
2697 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2698 break;
2699 case lang_output_statement_enum:
2700 minfo ("OUTPUT(%s", s->output_statement.name);
2701 if (output_target != NULL)
2702 minfo (" %s", output_target);
2703 minfo (")\n");
2704 break;
2705 case lang_input_statement_enum:
2706 print_input_statement (&s->input_statement);
2707 break;
2708 case lang_group_statement_enum:
2709 print_group (&s->group_statement, os);
2710 break;
2711 case lang_afile_asection_pair_statement_enum:
2712 FAIL ();
2713 break;
2717 static void
2718 print_statements ()
2720 print_statement_list (statement_list.head, abs_output_section);
2723 /* Print the first N statements in statement list S to STDERR.
2724 If N == 0, nothing is printed.
2725 If N < 0, the entire list is printed.
2726 Intended to be called from GDB. */
2728 void
2729 dprint_statement (s, n)
2730 lang_statement_union_type *s;
2731 int n;
2733 FILE *map_save = config.map_file;
2735 config.map_file = stderr;
2737 if (n < 0)
2738 print_statement_list (s, abs_output_section);
2739 else
2741 while (s && --n >= 0)
2743 print_statement (s, abs_output_section);
2744 s = s->header.next;
2748 config.map_file = map_save;
2751 static void
2752 insert_pad (ptr, fill, alignment_needed, output_section, dot)
2753 lang_statement_union_type **ptr;
2754 fill_type *fill;
2755 unsigned int alignment_needed;
2756 asection *output_section;
2757 bfd_vma dot;
2759 static fill_type zero_fill = { 1, { 0 } };
2760 lang_statement_union_type *pad;
2762 pad = ((lang_statement_union_type *)
2763 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2764 if (ptr != &statement_list.head
2765 && pad->header.type == lang_padding_statement_enum
2766 && pad->padding_statement.output_section == output_section)
2768 /* Use the existing pad statement. The above test on output
2769 section is probably redundant, but it doesn't hurt to check. */
2771 else
2773 /* Make a new padding statement, linked into existing chain. */
2774 pad = ((lang_statement_union_type *)
2775 stat_alloc (sizeof (lang_padding_statement_type)));
2776 pad->header.next = *ptr;
2777 *ptr = pad;
2778 pad->header.type = lang_padding_statement_enum;
2779 pad->padding_statement.output_section = output_section;
2780 if (fill == (fill_type *) 0)
2781 fill = &zero_fill;
2782 pad->padding_statement.fill = fill;
2784 pad->padding_statement.output_offset = dot - output_section->vma;
2785 pad->padding_statement.size = alignment_needed;
2786 output_section->_raw_size += alignment_needed;
2789 /* Work out how much this section will move the dot point. */
2791 static bfd_vma
2792 size_input_section (this_ptr, output_section_statement, fill, dot)
2793 lang_statement_union_type **this_ptr;
2794 lang_output_section_statement_type *output_section_statement;
2795 fill_type *fill;
2796 bfd_vma dot;
2798 lang_input_section_type *is = &((*this_ptr)->input_section);
2799 asection *i = is->section;
2801 if (is->ifile->just_syms_flag == false)
2803 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2804 ldfile_output_machine);
2805 unsigned int alignment_needed;
2806 asection *o;
2808 /* Align this section first to the input sections requirement,
2809 then to the output section's requirement. If this alignment
2810 is greater than any seen before, then record it too. Perform
2811 the alignment by inserting a magic 'padding' statement. */
2813 if (output_section_statement->subsection_alignment != -1)
2814 i->alignment_power = output_section_statement->subsection_alignment;
2816 o = output_section_statement->bfd_section;
2817 if (o->alignment_power < i->alignment_power)
2818 o->alignment_power = i->alignment_power;
2820 alignment_needed = align_power (dot, i->alignment_power) - dot;
2822 if (alignment_needed != 0)
2824 insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2825 dot += alignment_needed;
2828 /* Remember where in the output section this input section goes. */
2830 i->output_offset = dot - o->vma;
2832 /* Mark how big the output section must be to contain this now. */
2833 if (i->_cooked_size != 0)
2834 dot += i->_cooked_size / opb;
2835 else
2836 dot += i->_raw_size / opb;
2837 o->_raw_size = (dot - o->vma) * opb;
2839 else
2841 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2844 return dot;
2847 #define IGNORE_SECTION(bfd, s) \
2848 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2849 != (SEC_ALLOC | SEC_LOAD)) \
2850 || bfd_section_size (bfd, s) == 0)
2852 /* Check to see if any allocated sections overlap with other allocated
2853 sections. This can happen when the linker script specifically specifies
2854 the output section addresses of the two sections. */
2856 static void
2857 lang_check_section_addresses ()
2859 asection *s;
2860 unsigned opb = bfd_octets_per_byte (output_bfd);
2862 /* Scan all sections in the output list. */
2863 for (s = output_bfd->sections; s != NULL; s = s->next)
2865 asection *os;
2867 /* Ignore sections which are not loaded or which have no contents. */
2868 if (IGNORE_SECTION (output_bfd, s))
2869 continue;
2871 /* Once we reach section 's' stop our seach. This prevents two
2872 warning messages from being produced, one for 'section A overlaps
2873 section B' and one for 'section B overlaps section A'. */
2874 for (os = output_bfd->sections; os != s; os = os->next)
2876 bfd_vma s_start;
2877 bfd_vma s_end;
2878 bfd_vma os_start;
2879 bfd_vma os_end;
2881 /* Only consider loadable sections with real contents. */
2882 if (IGNORE_SECTION (output_bfd, os))
2883 continue;
2885 /* We must check the sections' LMA addresses not their
2886 VMA addresses because overlay sections can have
2887 overlapping VMAs but they must have distinct LMAs. */
2888 s_start = bfd_section_lma (output_bfd, s);
2889 os_start = bfd_section_lma (output_bfd, os);
2890 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2891 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2893 /* Look for an overlap. */
2894 if ((s_end < os_start) || (s_start > os_end))
2895 continue;
2897 einfo (
2898 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2899 s->name, s_start, s_end, os->name, os_start, os_end);
2901 /* Once we have found one overlap for this section,
2902 stop looking for others. */
2903 break;
2908 /* Make sure the new address is within the region. We explicitly permit the
2909 current address to be at the exact end of the region when the address is
2910 non-zero, in case the region is at the end of addressable memory and the
2911 calculation wraps around. */
2913 static void
2914 os_region_check (os, region, tree, base)
2915 lang_output_section_statement_type *os;
2916 struct memory_region_struct *region;
2917 etree_type *tree;
2918 bfd_vma base;
2920 if ((region->current < region->origin
2921 || (region->current - region->origin > region->length))
2922 && ((region->current != region->origin + region->length)
2923 || base == 0))
2925 if (tree != (etree_type *) NULL)
2927 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2928 region->current,
2929 os->bfd_section->owner,
2930 os->bfd_section->name,
2931 region->name);
2933 else
2935 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2936 region->name,
2937 os->bfd_section->owner,
2938 os->bfd_section->name);
2940 /* Reset the region pointer. */
2941 region->current = region->origin;
2945 /* Set the sizes for all the output sections. */
2947 static bfd_vma
2948 lang_size_sections_1 (s, output_section_statement, prev, fill, dot, relax)
2949 lang_statement_union_type *s;
2950 lang_output_section_statement_type *output_section_statement;
2951 lang_statement_union_type **prev;
2952 fill_type *fill;
2953 bfd_vma dot;
2954 boolean *relax;
2956 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2957 ldfile_output_machine);
2959 /* Size up the sections from their constituent parts. */
2960 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2962 switch (s->header.type)
2964 case lang_output_section_statement_enum:
2966 bfd_vma after;
2967 lang_output_section_statement_type *os;
2969 os = &s->output_section_statement;
2970 if (os->bfd_section == NULL)
2971 /* This section was never actually created. */
2972 break;
2974 /* If this is a COFF shared library section, use the size and
2975 address from the input section. FIXME: This is COFF
2976 specific; it would be cleaner if there were some other way
2977 to do this, but nothing simple comes to mind. */
2978 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2980 asection *input;
2982 if (os->children.head == NULL
2983 || os->children.head->header.next != NULL
2984 || os->children.head->header.type != lang_input_section_enum)
2985 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2986 os->name);
2988 input = os->children.head->input_section.section;
2989 bfd_set_section_vma (os->bfd_section->owner,
2990 os->bfd_section,
2991 bfd_section_vma (input->owner, input));
2992 os->bfd_section->_raw_size = input->_raw_size;
2993 break;
2996 if (bfd_is_abs_section (os->bfd_section))
2998 /* No matter what happens, an abs section starts at zero. */
2999 ASSERT (os->bfd_section->vma == 0);
3001 else
3003 if (os->addr_tree == (etree_type *) NULL)
3005 /* No address specified for this section, get one
3006 from the region specification. */
3007 if (os->region == (lang_memory_region_type *) NULL
3008 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
3009 & (SEC_ALLOC | SEC_LOAD)) != 0)
3010 && os->region->name[0] == '*'
3011 && strcmp (os->region->name, "*default*") == 0))
3013 os->region = lang_memory_default (os->bfd_section);
3016 /* If a loadable section is using the default memory
3017 region, and some non default memory regions were
3018 defined, issue a warning. */
3019 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
3020 & (SEC_ALLOC | SEC_LOAD)) != 0
3021 && (bfd_get_section_flags (output_bfd, os->bfd_section)
3022 & SEC_NEVER_LOAD) == 0
3023 && ! link_info.relocateable
3024 && strcmp (os->region->name, "*default*") == 0
3025 && lang_memory_region_list != NULL
3026 && (strcmp (lang_memory_region_list->name,
3027 "*default*") != 0
3028 || lang_memory_region_list->next != NULL))
3029 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
3030 bfd_get_section_name (output_bfd,
3031 os->bfd_section));
3033 dot = os->region->current;
3035 if (os->section_alignment == -1)
3037 bfd_vma olddot;
3039 olddot = dot;
3040 dot = align_power (dot,
3041 os->bfd_section->alignment_power);
3043 if (dot != olddot && config.warn_section_align)
3044 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
3045 os->name, (unsigned int) (dot - olddot));
3048 else
3050 etree_value_type r;
3052 r = exp_fold_tree (os->addr_tree,
3053 abs_output_section,
3054 lang_allocating_phase_enum,
3055 dot, &dot);
3056 if (r.valid_p == false)
3058 einfo (_("%F%S: non constant address expression for section %s\n"),
3059 os->name);
3061 dot = r.value + r.section->bfd_section->vma;
3064 /* The section starts here.
3065 First, align to what the section needs. */
3067 if (os->section_alignment != -1)
3068 dot = align_power (dot, os->section_alignment);
3070 bfd_set_section_vma (0, os->bfd_section, dot);
3072 os->bfd_section->output_offset = 0;
3075 lang_size_sections_1 (os->children.head, os, &os->children.head,
3076 os->fill, dot, relax);
3078 /* Put the section within the requested block size, or
3079 align at the block boundary. */
3080 after = align_n (os->bfd_section->vma
3081 + os->bfd_section->_raw_size / opb,
3082 (bfd_vma) os->block_value);
3084 if (bfd_is_abs_section (os->bfd_section))
3085 ASSERT (after == os->bfd_section->vma);
3086 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
3087 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
3088 && ! link_info.relocateable)
3089 os->bfd_section->_raw_size = 0;
3090 else
3091 os->bfd_section->_raw_size =
3092 (after - os->bfd_section->vma) * opb;
3094 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3095 os->processed = true;
3097 if (os->update_dot_tree != 0)
3098 exp_fold_tree (os->update_dot_tree, abs_output_section,
3099 lang_allocating_phase_enum, dot, &dot);
3101 /* Update dot in the region ?
3102 We only do this if the section is going to be allocated,
3103 since unallocated sections do not contribute to the region's
3104 overall size in memory.
3106 If the SEC_NEVER_LOAD bit is not set, it will affect the
3107 addresses of sections after it. We have to update
3108 dot. */
3109 if (os->region != (lang_memory_region_type *) NULL
3110 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3111 & SEC_NEVER_LOAD) == 0
3112 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3113 & (SEC_ALLOC | SEC_LOAD))))
3115 os->region->current = dot;
3117 /* Make sure the new address is within the region. */
3118 os_region_check (os, os->region, os->addr_tree,
3119 os->bfd_section->vma);
3121 /* If there's no load address specified, use the run
3122 region as the load region. */
3123 if (os->lma_region == NULL && os->load_base == NULL)
3124 os->lma_region = os->region;
3126 if (os->lma_region != NULL && os->lma_region != os->region)
3128 /* Set load_base, which will be handled later. */
3129 os->load_base = exp_intop (os->lma_region->current);
3130 os->lma_region->current +=
3131 os->bfd_section->_raw_size / opb;
3132 os_region_check (os, os->lma_region, NULL,
3133 os->bfd_section->lma);
3137 break;
3139 case lang_constructors_statement_enum:
3140 dot = lang_size_sections_1 (constructor_list.head,
3141 output_section_statement,
3142 &s->wild_statement.children.head,
3143 fill, dot, relax);
3144 break;
3146 case lang_data_statement_enum:
3148 unsigned int size = 0;
3150 s->data_statement.output_vma =
3151 dot - output_section_statement->bfd_section->vma;
3152 s->data_statement.output_section =
3153 output_section_statement->bfd_section;
3155 switch (s->data_statement.type)
3157 default:
3158 abort ();
3159 case QUAD:
3160 case SQUAD:
3161 size = QUAD_SIZE;
3162 break;
3163 case LONG:
3164 size = LONG_SIZE;
3165 break;
3166 case SHORT:
3167 size = SHORT_SIZE;
3168 break;
3169 case BYTE:
3170 size = BYTE_SIZE;
3171 break;
3173 if (size < opb)
3174 size = opb;
3175 dot += size / opb;
3176 output_section_statement->bfd_section->_raw_size += size;
3177 /* The output section gets contents, and then we inspect for
3178 any flags set in the input script which override any ALLOC. */
3179 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3180 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3182 output_section_statement->bfd_section->flags |=
3183 SEC_ALLOC | SEC_LOAD;
3186 break;
3188 case lang_reloc_statement_enum:
3190 int size;
3192 s->reloc_statement.output_vma =
3193 dot - output_section_statement->bfd_section->vma;
3194 s->reloc_statement.output_section =
3195 output_section_statement->bfd_section;
3196 size = bfd_get_reloc_size (s->reloc_statement.howto);
3197 dot += size / opb;
3198 output_section_statement->bfd_section->_raw_size += size;
3200 break;
3202 case lang_wild_statement_enum:
3204 dot = lang_size_sections_1 (s->wild_statement.children.head,
3205 output_section_statement,
3206 &s->wild_statement.children.head,
3207 fill, dot, relax);
3209 break;
3211 case lang_object_symbols_statement_enum:
3212 link_info.create_object_symbols_section =
3213 output_section_statement->bfd_section;
3214 break;
3215 case lang_output_statement_enum:
3216 case lang_target_statement_enum:
3217 break;
3218 case lang_input_section_enum:
3220 asection *i;
3222 i = (*prev)->input_section.section;
3223 if (! relax)
3225 if (i->_cooked_size == 0)
3226 i->_cooked_size = i->_raw_size;
3228 else
3230 boolean again;
3232 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3233 einfo (_("%P%F: can't relax section: %E\n"));
3234 if (again)
3235 *relax = true;
3237 dot = size_input_section (prev, output_section_statement,
3238 output_section_statement->fill, dot);
3240 break;
3241 case lang_input_statement_enum:
3242 break;
3243 case lang_fill_statement_enum:
3244 s->fill_statement.output_section =
3245 output_section_statement->bfd_section;
3247 fill = s->fill_statement.fill;
3248 break;
3249 case lang_assignment_statement_enum:
3251 bfd_vma newdot = dot;
3253 exp_fold_tree (s->assignment_statement.exp,
3254 output_section_statement,
3255 lang_allocating_phase_enum,
3256 dot,
3257 &newdot);
3259 if (newdot != dot)
3261 if (output_section_statement == abs_output_section)
3263 /* If we don't have an output section, then just adjust
3264 the default memory address. */
3265 lang_memory_region_lookup ("*default*")->current = newdot;
3267 else
3269 /* Insert a pad after this statement. We can't
3270 put the pad before when relaxing, in case the
3271 assignment references dot. */
3272 insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3273 output_section_statement->bfd_section, dot);
3275 /* Don't neuter the pad below when relaxing. */
3276 s = s->header.next;
3279 dot = newdot;
3282 break;
3284 case lang_padding_statement_enum:
3285 /* If this is the first time lang_size_sections is called,
3286 we won't have any padding statements. If this is the
3287 second or later passes when relaxing, we should allow
3288 padding to shrink. If padding is needed on this pass, it
3289 will be added back in. */
3290 s->padding_statement.size = 0;
3292 /* Make sure output_offset is valid. If relaxation shrinks
3293 the section and this pad isn't needed, it's possible to
3294 have output_offset larger than the final size of the
3295 section. bfd_set_section_contents will complain even for
3296 a pad size of zero. */
3297 s->padding_statement.output_offset
3298 = dot - output_section_statement->bfd_section->vma;
3299 break;
3301 case lang_group_statement_enum:
3302 dot = lang_size_sections_1 (s->group_statement.children.head,
3303 output_section_statement,
3304 &s->group_statement.children.head,
3305 fill, dot, relax);
3306 break;
3308 default:
3309 FAIL ();
3310 break;
3312 /* We can only get here when relaxing is turned on. */
3313 case lang_address_statement_enum:
3314 break;
3316 prev = &s->header.next;
3318 return dot;
3321 bfd_vma
3322 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
3323 lang_statement_union_type *s;
3324 lang_output_section_statement_type *output_section_statement;
3325 lang_statement_union_type **prev;
3326 fill_type *fill;
3327 bfd_vma dot;
3328 boolean *relax;
3330 bfd_vma result;
3332 exp_data_seg.phase = exp_dataseg_none;
3333 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3334 dot, relax);
3335 if (exp_data_seg.phase == exp_dataseg_end_seen)
3337 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3338 a page could be saved in the data segment. */
3339 bfd_vma first, last;
3341 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3342 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3343 if (first && last
3344 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3345 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3346 && first + last <= exp_data_seg.pagesize)
3348 exp_data_seg.phase = exp_dataseg_adjust;
3349 result = lang_size_sections_1 (s, output_section_statement, prev,
3350 fill, dot, relax);
3354 return result;
3357 bfd_vma
3358 lang_do_assignments (s, output_section_statement, fill, dot)
3359 lang_statement_union_type *s;
3360 lang_output_section_statement_type *output_section_statement;
3361 fill_type *fill;
3362 bfd_vma dot;
3364 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3365 ldfile_output_machine);
3367 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3369 switch (s->header.type)
3371 case lang_constructors_statement_enum:
3372 dot = lang_do_assignments (constructor_list.head,
3373 output_section_statement,
3374 fill,
3375 dot);
3376 break;
3378 case lang_output_section_statement_enum:
3380 lang_output_section_statement_type *os;
3382 os = &(s->output_section_statement);
3383 if (os->bfd_section != NULL)
3385 dot = os->bfd_section->vma;
3386 (void) lang_do_assignments (os->children.head, os,
3387 os->fill, dot);
3388 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3391 if (os->load_base)
3393 /* If nothing has been placed into the output section then
3394 it won't have a bfd_section. */
3395 if (os->bfd_section)
3397 os->bfd_section->lma
3398 = exp_get_abs_int (os->load_base, 0, "load base",
3399 lang_final_phase_enum);
3403 break;
3404 case lang_wild_statement_enum:
3406 dot = lang_do_assignments (s->wild_statement.children.head,
3407 output_section_statement,
3408 fill, dot);
3410 break;
3412 case lang_object_symbols_statement_enum:
3413 case lang_output_statement_enum:
3414 case lang_target_statement_enum:
3415 #if 0
3416 case lang_common_statement_enum:
3417 #endif
3418 break;
3419 case lang_data_statement_enum:
3421 etree_value_type value;
3423 value = exp_fold_tree (s->data_statement.exp,
3424 abs_output_section,
3425 lang_final_phase_enum, dot, &dot);
3426 s->data_statement.value = value.value;
3427 if (value.valid_p == false)
3428 einfo (_("%F%P: invalid data statement\n"));
3431 unsigned int size;
3432 switch (s->data_statement.type)
3434 default:
3435 abort ();
3436 case QUAD:
3437 case SQUAD:
3438 size = QUAD_SIZE;
3439 break;
3440 case LONG:
3441 size = LONG_SIZE;
3442 break;
3443 case SHORT:
3444 size = SHORT_SIZE;
3445 break;
3446 case BYTE:
3447 size = BYTE_SIZE;
3448 break;
3450 if (size < opb)
3451 size = opb;
3452 dot += size / opb;
3454 break;
3456 case lang_reloc_statement_enum:
3458 etree_value_type value;
3460 value = exp_fold_tree (s->reloc_statement.addend_exp,
3461 abs_output_section,
3462 lang_final_phase_enum, dot, &dot);
3463 s->reloc_statement.addend_value = value.value;
3464 if (value.valid_p == false)
3465 einfo (_("%F%P: invalid reloc statement\n"));
3467 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
3468 break;
3470 case lang_input_section_enum:
3472 asection *in = s->input_section.section;
3474 if (in->_cooked_size != 0)
3475 dot += in->_cooked_size / opb;
3476 else
3477 dot += in->_raw_size / opb;
3479 break;
3481 case lang_input_statement_enum:
3482 break;
3483 case lang_fill_statement_enum:
3484 fill = s->fill_statement.fill;
3485 break;
3486 case lang_assignment_statement_enum:
3488 exp_fold_tree (s->assignment_statement.exp,
3489 output_section_statement,
3490 lang_final_phase_enum,
3491 dot,
3492 &dot);
3495 break;
3496 case lang_padding_statement_enum:
3497 dot += s->padding_statement.size / opb;
3498 break;
3500 case lang_group_statement_enum:
3501 dot = lang_do_assignments (s->group_statement.children.head,
3502 output_section_statement,
3503 fill, dot);
3505 break;
3507 default:
3508 FAIL ();
3509 break;
3510 case lang_address_statement_enum:
3511 break;
3515 return dot;
3518 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3519 operator .startof. (section_name), it produces an undefined symbol
3520 .startof.section_name. Similarly, when it sees
3521 .sizeof. (section_name), it produces an undefined symbol
3522 .sizeof.section_name. For all the output sections, we look for
3523 such symbols, and set them to the correct value. */
3525 static void
3526 lang_set_startof ()
3528 asection *s;
3530 if (link_info.relocateable)
3531 return;
3533 for (s = output_bfd->sections; s != NULL; s = s->next)
3535 const char *secname;
3536 char *buf;
3537 struct bfd_link_hash_entry *h;
3539 secname = bfd_get_section_name (output_bfd, s);
3540 buf = xmalloc (10 + strlen (secname));
3542 sprintf (buf, ".startof.%s", secname);
3543 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3544 if (h != NULL && h->type == bfd_link_hash_undefined)
3546 h->type = bfd_link_hash_defined;
3547 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3548 h->u.def.section = bfd_abs_section_ptr;
3551 sprintf (buf, ".sizeof.%s", secname);
3552 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3553 if (h != NULL && h->type == bfd_link_hash_undefined)
3555 unsigned opb;
3557 opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3558 ldfile_output_machine);
3559 h->type = bfd_link_hash_defined;
3560 if (s->_cooked_size != 0)
3561 h->u.def.value = s->_cooked_size / opb;
3562 else
3563 h->u.def.value = s->_raw_size / opb;
3564 h->u.def.section = bfd_abs_section_ptr;
3567 free (buf);
3571 static void
3572 lang_finish ()
3574 struct bfd_link_hash_entry *h;
3575 boolean warn;
3577 if (link_info.relocateable || link_info.shared)
3578 warn = false;
3579 else
3580 warn = true;
3582 if (entry_symbol.name == (const char *) NULL)
3584 /* No entry has been specified. Look for start, but don't warn
3585 if we don't find it. */
3586 entry_symbol.name = "start";
3587 warn = false;
3590 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3591 false, false, true);
3592 if (h != (struct bfd_link_hash_entry *) NULL
3593 && (h->type == bfd_link_hash_defined
3594 || h->type == bfd_link_hash_defweak)
3595 && h->u.def.section->output_section != NULL)
3597 bfd_vma val;
3599 val = (h->u.def.value
3600 + bfd_get_section_vma (output_bfd,
3601 h->u.def.section->output_section)
3602 + h->u.def.section->output_offset);
3603 if (! bfd_set_start_address (output_bfd, val))
3604 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3606 else
3608 bfd_vma val;
3609 const char *send;
3611 /* We couldn't find the entry symbol. Try parsing it as a
3612 number. */
3613 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3614 if (*send == '\0')
3616 if (! bfd_set_start_address (output_bfd, val))
3617 einfo (_("%P%F: can't set start address\n"));
3619 else
3621 asection *ts;
3623 /* Can't find the entry symbol, and it's not a number. Use
3624 the first address in the text section. */
3625 ts = bfd_get_section_by_name (output_bfd, entry_section);
3626 if (ts != (asection *) NULL)
3628 if (warn)
3629 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3630 entry_symbol.name,
3631 bfd_get_section_vma (output_bfd, ts));
3632 if (! bfd_set_start_address (output_bfd,
3633 bfd_get_section_vma (output_bfd,
3634 ts)))
3635 einfo (_("%P%F: can't set start address\n"));
3637 else
3639 if (warn)
3640 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3641 entry_symbol.name);
3647 /* This is a small function used when we want to ignore errors from
3648 BFD. */
3650 static void
3651 #ifdef ANSI_PROTOTYPES
3652 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3653 #else
3654 ignore_bfd_errors (s)
3655 const char *s ATTRIBUTE_UNUSED;
3656 #endif
3658 /* Don't do anything. */
3661 /* Check that the architecture of all the input files is compatible
3662 with the output file. Also call the backend to let it do any
3663 other checking that is needed. */
3665 static void
3666 lang_check ()
3668 lang_statement_union_type *file;
3669 bfd *input_bfd;
3670 const bfd_arch_info_type *compatible;
3672 for (file = file_chain.head;
3673 file != (lang_statement_union_type *) NULL;
3674 file = file->input_statement.next)
3676 input_bfd = file->input_statement.the_bfd;
3677 compatible = bfd_arch_get_compatible (input_bfd, output_bfd);
3679 /* In general it is not possible to perform a relocatable
3680 link between differing object formats when the input
3681 file has relocations, because the relocations in the
3682 input format may not have equivalent representations in
3683 the output format (and besides BFD does not translate
3684 relocs for other link purposes than a final link). */
3685 if ((link_info.relocateable || link_info.emitrelocations)
3686 && (compatible == NULL
3687 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3688 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3690 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3691 bfd_get_target (input_bfd), input_bfd,
3692 bfd_get_target (output_bfd), output_bfd);
3693 /* einfo with %F exits. */
3696 if (compatible == NULL)
3698 if (command_line.warn_mismatch)
3699 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3700 bfd_printable_name (input_bfd), input_bfd,
3701 bfd_printable_name (output_bfd));
3703 else if (bfd_count_sections (input_bfd))
3705 /* If the input bfd has no contents, it shouldn't set the
3706 private data of the output bfd. */
3708 bfd_error_handler_type pfn = NULL;
3710 /* If we aren't supposed to warn about mismatched input
3711 files, temporarily set the BFD error handler to a
3712 function which will do nothing. We still want to call
3713 bfd_merge_private_bfd_data, since it may set up
3714 information which is needed in the output file. */
3715 if (! command_line.warn_mismatch)
3716 pfn = bfd_set_error_handler (ignore_bfd_errors);
3717 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3719 if (command_line.warn_mismatch)
3720 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3721 input_bfd);
3723 if (! command_line.warn_mismatch)
3724 bfd_set_error_handler (pfn);
3729 /* Look through all the global common symbols and attach them to the
3730 correct section. The -sort-common command line switch may be used
3731 to roughly sort the entries by size. */
3733 static void
3734 lang_common ()
3736 if (command_line.inhibit_common_definition)
3737 return;
3738 if (link_info.relocateable
3739 && ! command_line.force_common_definition)
3740 return;
3742 if (! config.sort_common)
3743 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3744 else
3746 int power;
3748 for (power = 4; power >= 0; power--)
3749 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3750 (PTR) &power);
3754 /* Place one common symbol in the correct section. */
3756 static boolean
3757 lang_one_common (h, info)
3758 struct bfd_link_hash_entry *h;
3759 PTR info;
3761 unsigned int power_of_two;
3762 bfd_vma size;
3763 asection *section;
3764 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3765 ldfile_output_machine);
3767 if (h->type != bfd_link_hash_common)
3768 return true;
3770 size = h->u.c.size;
3771 power_of_two = h->u.c.p->alignment_power;
3773 if (config.sort_common
3774 && power_of_two < (unsigned int) *(int *) info)
3775 return true;
3777 section = h->u.c.p->section;
3779 /* Increase the size of the section. */
3780 section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3781 (bfd_vma) 1 << power_of_two) * opb;
3783 /* Adjust the alignment if necessary. */
3784 if (power_of_two > section->alignment_power)
3785 section->alignment_power = power_of_two;
3787 /* Change the symbol from common to defined. */
3788 h->type = bfd_link_hash_defined;
3789 h->u.def.section = section;
3790 h->u.def.value = section->_cooked_size;
3792 /* Increase the size of the section. */
3793 section->_cooked_size += size;
3795 /* Make sure the section is allocated in memory, and make sure that
3796 it is no longer a common section. */
3797 section->flags |= SEC_ALLOC;
3798 section->flags &= ~SEC_IS_COMMON;
3800 if (config.map_file != NULL)
3802 static boolean header_printed;
3803 int len;
3804 char *name;
3805 char buf[50];
3807 if (! header_printed)
3809 minfo (_("\nAllocating common symbols\n"));
3810 minfo (_("Common symbol size file\n\n"));
3811 header_printed = true;
3814 name = demangle (h->root.string);
3815 minfo ("%s", name);
3816 len = strlen (name);
3817 free (name);
3819 if (len >= 19)
3821 print_nl ();
3822 len = 0;
3824 while (len < 20)
3826 print_space ();
3827 ++len;
3830 minfo ("0x");
3831 if (size <= 0xffffffff)
3832 sprintf (buf, "%lx", (unsigned long) size);
3833 else
3834 sprintf_vma (buf, size);
3835 minfo ("%s", buf);
3836 len = strlen (buf);
3838 while (len < 16)
3840 print_space ();
3841 ++len;
3844 minfo ("%B\n", section->owner);
3847 return true;
3850 /* Run through the input files and ensure that every input section has
3851 somewhere to go. If one is found without a destination then create
3852 an input request and place it into the statement tree. */
3854 static void
3855 lang_place_orphans ()
3857 LANG_FOR_EACH_INPUT_STATEMENT (file)
3859 asection *s;
3861 for (s = file->the_bfd->sections;
3862 s != (asection *) NULL;
3863 s = s->next)
3865 if (s->output_section == (asection *) NULL)
3867 /* This section of the file is not attatched, root
3868 around for a sensible place for it to go. */
3870 if (file->just_syms_flag)
3872 abort ();
3874 else if (strcmp (s->name, "COMMON") == 0)
3876 /* This is a lonely common section which must have
3877 come from an archive. We attach to the section
3878 with the wildcard. */
3879 if (! link_info.relocateable
3880 || command_line.force_common_definition)
3882 if (default_common_section == NULL)
3884 #if 0
3885 /* This message happens when using the
3886 svr3.ifile linker script, so I have
3887 disabled it. */
3888 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3889 #endif
3890 default_common_section =
3891 lang_output_section_statement_lookup (".bss");
3894 lang_add_section (&default_common_section->children, s,
3895 default_common_section, file);
3898 else if (ldemul_place_orphan (file, s))
3900 else
3902 lang_output_section_statement_type *os;
3904 os = lang_output_section_statement_lookup (s->name);
3905 lang_add_section (&os->children, s, os, file);
3912 void
3913 lang_set_flags (ptr, flags, invert)
3914 lang_memory_region_type *ptr;
3915 const char *flags;
3916 int invert;
3918 flagword *ptr_flags;
3920 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3921 while (*flags)
3923 switch (*flags)
3925 case 'A': case 'a':
3926 *ptr_flags |= SEC_ALLOC;
3927 break;
3929 case 'R': case 'r':
3930 *ptr_flags |= SEC_READONLY;
3931 break;
3933 case 'W': case 'w':
3934 *ptr_flags |= SEC_DATA;
3935 break;
3937 case 'X': case 'x':
3938 *ptr_flags |= SEC_CODE;
3939 break;
3941 case 'L': case 'l':
3942 case 'I': case 'i':
3943 *ptr_flags |= SEC_LOAD;
3944 break;
3946 default:
3947 einfo (_("%P%F: invalid syntax in flags\n"));
3948 break;
3950 flags++;
3954 /* Call a function on each input file. This function will be called
3955 on an archive, but not on the elements. */
3957 void
3958 lang_for_each_input_file (func)
3959 void (*func) PARAMS ((lang_input_statement_type *));
3961 lang_input_statement_type *f;
3963 for (f = (lang_input_statement_type *) input_file_chain.head;
3964 f != NULL;
3965 f = (lang_input_statement_type *) f->next_real_file)
3966 func (f);
3969 /* Call a function on each file. The function will be called on all
3970 the elements of an archive which are included in the link, but will
3971 not be called on the archive file itself. */
3973 void
3974 lang_for_each_file (func)
3975 void (*func) PARAMS ((lang_input_statement_type *));
3977 LANG_FOR_EACH_INPUT_STATEMENT (f)
3979 func (f);
3983 #if 0
3985 /* Not used. */
3987 void
3988 lang_for_each_input_section (func)
3989 void (*func) PARAMS ((bfd *ab, asection *as));
3991 LANG_FOR_EACH_INPUT_STATEMENT (f)
3993 asection *s;
3995 for (s = f->the_bfd->sections;
3996 s != (asection *) NULL;
3997 s = s->next)
3999 func (f->the_bfd, s);
4004 #endif
4006 void
4007 ldlang_add_file (entry)
4008 lang_input_statement_type *entry;
4010 bfd **pp;
4012 lang_statement_append (&file_chain,
4013 (lang_statement_union_type *) entry,
4014 &entry->next);
4016 /* The BFD linker needs to have a list of all input BFDs involved in
4017 a link. */
4018 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
4019 ASSERT (entry->the_bfd != output_bfd);
4020 for (pp = &link_info.input_bfds;
4021 *pp != (bfd *) NULL;
4022 pp = &(*pp)->link_next)
4024 *pp = entry->the_bfd;
4025 entry->the_bfd->usrdata = (PTR) entry;
4026 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4028 /* Look through the sections and check for any which should not be
4029 included in the link. We need to do this now, so that we can
4030 notice when the backend linker tries to report multiple
4031 definition errors for symbols which are in sections we aren't
4032 going to link. FIXME: It might be better to entirely ignore
4033 symbols which are defined in sections which are going to be
4034 discarded. This would require modifying the backend linker for
4035 each backend which might set the SEC_LINK_ONCE flag. If we do
4036 this, we should probably handle SEC_EXCLUDE in the same way. */
4038 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
4041 void
4042 lang_add_output (name, from_script)
4043 const char *name;
4044 int from_script;
4046 /* Make -o on command line override OUTPUT in script. */
4047 if (had_output_filename == false || !from_script)
4049 output_filename = name;
4050 had_output_filename = true;
4054 static lang_output_section_statement_type *current_section;
4056 static int
4057 topower (x)
4058 int x;
4060 unsigned int i = 1;
4061 int l;
4063 if (x < 0)
4064 return -1;
4066 for (l = 0; l < 32; l++)
4068 if (i >= (unsigned int) x)
4069 return l;
4070 i <<= 1;
4073 return 0;
4076 lang_output_section_statement_type *
4077 lang_enter_output_section_statement (output_section_statement_name,
4078 address_exp, sectype, block_value,
4079 align, subalign, ebase)
4080 const char *output_section_statement_name;
4081 etree_type *address_exp;
4082 enum section_type sectype;
4083 bfd_vma block_value;
4084 etree_type *align;
4085 etree_type *subalign;
4086 etree_type *ebase;
4088 lang_output_section_statement_type *os;
4090 current_section =
4091 os =
4092 lang_output_section_statement_lookup (output_section_statement_name);
4094 /* Add this statement to tree. */
4095 #if 0
4096 add_statement (lang_output_section_statement_enum,
4097 output_section_statement);
4098 #endif
4099 /* Make next things chain into subchain of this. */
4101 if (os->addr_tree == (etree_type *) NULL)
4103 os->addr_tree = address_exp;
4105 os->sectype = sectype;
4106 if (sectype != noload_section)
4107 os->flags = SEC_NO_FLAGS;
4108 else
4109 os->flags = SEC_NEVER_LOAD;
4110 os->block_value = block_value ? block_value : 1;
4111 stat_ptr = &os->children;
4113 os->subsection_alignment =
4114 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4115 os->section_alignment =
4116 topower (exp_get_value_int (align, -1, "section alignment", 0));
4118 os->load_base = ebase;
4119 return os;
4122 void
4123 lang_final ()
4125 lang_output_statement_type *new =
4126 new_stat (lang_output_statement, stat_ptr);
4128 new->name = output_filename;
4131 /* Reset the current counters in the regions. */
4133 void
4134 lang_reset_memory_regions ()
4136 lang_memory_region_type *p = lang_memory_region_list;
4137 asection *o;
4139 for (p = lang_memory_region_list;
4140 p != (lang_memory_region_type *) NULL;
4141 p = p->next)
4143 p->old_length = (bfd_size_type) (p->current - p->origin);
4144 p->current = p->origin;
4147 for (o = output_bfd->sections; o != NULL; o = o->next)
4148 o->_raw_size = 0;
4151 /* If the wild pattern was marked KEEP, the member sections
4152 should be as well. */
4154 static void
4155 gc_section_callback (ptr, sec, section, file, data)
4156 lang_wild_statement_type *ptr;
4157 struct wildcard_list *sec ATTRIBUTE_UNUSED;
4158 asection *section;
4159 lang_input_statement_type *file ATTRIBUTE_UNUSED;
4160 PTR data ATTRIBUTE_UNUSED;
4162 if (ptr->keep_sections)
4163 section->flags |= SEC_KEEP;
4166 /* Handle a wild statement, marking it against GC. */
4168 static void
4169 lang_gc_wild (s)
4170 lang_wild_statement_type *s;
4172 walk_wild (s, gc_section_callback, NULL);
4175 /* Iterate over sections marking them against GC. */
4177 static void
4178 lang_gc_sections_1 (s)
4179 lang_statement_union_type *s;
4181 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
4183 switch (s->header.type)
4185 case lang_wild_statement_enum:
4186 lang_gc_wild (&s->wild_statement);
4187 break;
4188 case lang_constructors_statement_enum:
4189 lang_gc_sections_1 (constructor_list.head);
4190 break;
4191 case lang_output_section_statement_enum:
4192 lang_gc_sections_1 (s->output_section_statement.children.head);
4193 break;
4194 case lang_group_statement_enum:
4195 lang_gc_sections_1 (s->group_statement.children.head);
4196 break;
4197 default:
4198 break;
4203 static void
4204 lang_gc_sections ()
4206 struct bfd_link_hash_entry *h;
4207 ldlang_undef_chain_list_type *ulist;
4209 /* Keep all sections so marked in the link script. */
4211 lang_gc_sections_1 (statement_list.head);
4213 /* Keep all sections containing symbols undefined on the command-line,
4214 and the section containing the entry symbol. */
4216 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4218 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4219 false, false, false);
4221 if (h != (struct bfd_link_hash_entry *) NULL
4222 && (h->type == bfd_link_hash_defined
4223 || h->type == bfd_link_hash_defweak)
4224 && ! bfd_is_abs_section (h->u.def.section))
4226 h->u.def.section->flags |= SEC_KEEP;
4230 bfd_gc_sections (output_bfd, &link_info);
4233 void
4234 lang_process ()
4236 lang_reasonable_defaults ();
4237 current_target = default_target;
4239 /* Open the output file. */
4240 lang_for_each_statement (ldlang_open_output);
4242 ldemul_create_output_section_statements ();
4244 /* Add to the hash table all undefineds on the command line. */
4245 lang_place_undefineds ();
4247 already_linked_table_init ();
4249 /* Create a bfd for each input file. */
4250 current_target = default_target;
4251 open_input_bfds (statement_list.head, false);
4253 link_info.gc_sym_list = &entry_symbol;
4254 if (entry_symbol.name == NULL)
4255 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4257 ldemul_after_open ();
4259 already_linked_table_free ();
4261 /* Make sure that we're not mixing architectures. We call this
4262 after all the input files have been opened, but before we do any
4263 other processing, so that any operations merge_private_bfd_data
4264 does on the output file will be known during the rest of the
4265 link. */
4266 lang_check ();
4268 /* Handle .exports instead of a version script if we're told to do so. */
4269 if (command_line.version_exports_section)
4270 lang_do_version_exports_section ();
4272 /* Build all sets based on the information gathered from the input
4273 files. */
4274 ldctor_build_sets ();
4276 /* Remove unreferenced sections if asked to. */
4277 if (command_line.gc_sections)
4278 lang_gc_sections ();
4280 /* If there were any SEC_MERGE sections, finish their merging, so that
4281 section sizes can be computed. This has to be done after GC of sections,
4282 so that GCed sections are not merged, but before assigning output
4283 sections, since removing whole input sections is hard then. */
4284 bfd_merge_sections (output_bfd, &link_info);
4286 /* Size up the common data. */
4287 lang_common ();
4289 /* Run through the contours of the script and attach input sections
4290 to the correct output sections. */
4291 map_input_to_output_sections (statement_list.head, (char *) NULL,
4292 (lang_output_section_statement_type *) NULL);
4294 /* Find any sections not attached explicitly and handle them. */
4295 lang_place_orphans ();
4297 if (! link_info.relocateable)
4299 /* Look for a text section and set the readonly attribute in it. */
4300 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4302 if (found != (asection *) NULL)
4304 if (config.text_read_only)
4305 found->flags |= SEC_READONLY;
4306 else
4307 found->flags &= ~SEC_READONLY;
4311 /* Do anything special before sizing sections. This is where ELF
4312 and other back-ends size dynamic sections. */
4313 ldemul_before_allocation ();
4315 if (!link_info.relocateable)
4316 strip_excluded_output_sections ();
4318 /* We must record the program headers before we try to fix the
4319 section positions, since they will affect SIZEOF_HEADERS. */
4320 lang_record_phdrs ();
4322 /* Size up the sections. */
4323 lang_size_sections (statement_list.head,
4324 abs_output_section,
4325 &statement_list.head, 0, (bfd_vma) 0, NULL);
4327 /* Now run around and relax if we can. */
4328 if (command_line.relax)
4330 /* Keep relaxing until bfd_relax_section gives up. */
4331 boolean relax_again;
4335 lang_reset_memory_regions ();
4337 relax_again = false;
4339 /* Note: pe-dll.c does something like this also. If you find
4340 you need to change this code, you probably need to change
4341 pe-dll.c also. DJ */
4343 /* Do all the assignments with our current guesses as to
4344 section sizes. */
4345 lang_do_assignments (statement_list.head,
4346 abs_output_section,
4347 (fill_type *) 0, (bfd_vma) 0);
4349 /* Perform another relax pass - this time we know where the
4350 globals are, so can make a better guess. */
4351 lang_size_sections (statement_list.head,
4352 abs_output_section,
4353 &statement_list.head, 0, (bfd_vma) 0,
4354 &relax_again);
4356 while (relax_again);
4359 /* See if anything special should be done now we know how big
4360 everything is. */
4361 ldemul_after_allocation ();
4363 /* Fix any .startof. or .sizeof. symbols. */
4364 lang_set_startof ();
4366 /* Do all the assignments, now that we know the final resting places
4367 of all the symbols. */
4369 lang_do_assignments (statement_list.head,
4370 abs_output_section,
4371 (fill_type *) 0, (bfd_vma) 0);
4373 /* Make sure that the section addresses make sense. */
4374 if (! link_info.relocateable
4375 && command_line.check_section_addresses)
4376 lang_check_section_addresses ();
4378 /* Final stuffs. */
4380 ldemul_finish ();
4381 lang_finish ();
4384 /* EXPORTED TO YACC */
4386 void
4387 lang_add_wild (filespec, section_list, keep_sections)
4388 struct wildcard_spec *filespec;
4389 struct wildcard_list *section_list;
4390 boolean keep_sections;
4392 struct wildcard_list *curr, *next;
4393 lang_wild_statement_type *new;
4395 /* Reverse the list as the parser puts it back to front. */
4396 for (curr = section_list, section_list = NULL;
4397 curr != NULL;
4398 section_list = curr, curr = next)
4400 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4401 placed_commons = true;
4403 next = curr->next;
4404 curr->next = section_list;
4407 if (filespec != NULL && filespec->name != NULL)
4409 if (strcmp (filespec->name, "*") == 0)
4410 filespec->name = NULL;
4411 else if (! wildcardp (filespec->name))
4412 lang_has_input_file = true;
4415 new = new_stat (lang_wild_statement, stat_ptr);
4416 new->filename = NULL;
4417 new->filenames_sorted = false;
4418 if (filespec != NULL)
4420 new->filename = filespec->name;
4421 new->filenames_sorted = filespec->sorted;
4423 new->section_list = section_list;
4424 new->keep_sections = keep_sections;
4425 lang_list_init (&new->children);
4428 void
4429 lang_section_start (name, address)
4430 const char *name;
4431 etree_type *address;
4433 lang_address_statement_type *ad;
4435 ad = new_stat (lang_address_statement, stat_ptr);
4436 ad->section_name = name;
4437 ad->address = address;
4440 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4441 because of a -e argument on the command line, or zero if this is
4442 called by ENTRY in a linker script. Command line arguments take
4443 precedence. */
4445 void
4446 lang_add_entry (name, cmdline)
4447 const char *name;
4448 boolean cmdline;
4450 if (entry_symbol.name == NULL
4451 || cmdline
4452 || ! entry_from_cmdline)
4454 entry_symbol.name = name;
4455 entry_from_cmdline = cmdline;
4459 void
4460 lang_add_target (name)
4461 const char *name;
4463 lang_target_statement_type *new = new_stat (lang_target_statement,
4464 stat_ptr);
4466 new->target = name;
4470 void
4471 lang_add_map (name)
4472 const char *name;
4474 while (*name)
4476 switch (*name)
4478 case 'F':
4479 map_option_f = true;
4480 break;
4482 name++;
4486 void
4487 lang_add_fill (fill)
4488 fill_type *fill;
4490 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4491 stat_ptr);
4493 new->fill = fill;
4496 void
4497 lang_add_data (type, exp)
4498 int type;
4499 union etree_union *exp;
4502 lang_data_statement_type *new = new_stat (lang_data_statement,
4503 stat_ptr);
4505 new->exp = exp;
4506 new->type = type;
4510 /* Create a new reloc statement. RELOC is the BFD relocation type to
4511 generate. HOWTO is the corresponding howto structure (we could
4512 look this up, but the caller has already done so). SECTION is the
4513 section to generate a reloc against, or NAME is the name of the
4514 symbol to generate a reloc against. Exactly one of SECTION and
4515 NAME must be NULL. ADDEND is an expression for the addend. */
4517 void
4518 lang_add_reloc (reloc, howto, section, name, addend)
4519 bfd_reloc_code_real_type reloc;
4520 reloc_howto_type *howto;
4521 asection *section;
4522 const char *name;
4523 union etree_union *addend;
4525 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4527 p->reloc = reloc;
4528 p->howto = howto;
4529 p->section = section;
4530 p->name = name;
4531 p->addend_exp = addend;
4533 p->addend_value = 0;
4534 p->output_section = NULL;
4535 p->output_vma = 0;
4538 lang_assignment_statement_type *
4539 lang_add_assignment (exp)
4540 etree_type *exp;
4542 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4543 stat_ptr);
4545 new->exp = exp;
4546 return new;
4549 void
4550 lang_add_attribute (attribute)
4551 enum statement_enum attribute;
4553 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4556 void
4557 lang_startup (name)
4558 const char *name;
4560 if (startup_file != (char *) NULL)
4562 einfo (_("%P%Fmultiple STARTUP files\n"));
4564 first_file->filename = name;
4565 first_file->local_sym_name = name;
4566 first_file->real = true;
4568 startup_file = name;
4571 void
4572 lang_float (maybe)
4573 boolean maybe;
4575 lang_float_flag = maybe;
4579 /* Work out the load- and run-time regions from a script statement, and
4580 store them in *LMA_REGION and *REGION respectively.
4582 MEMSPEC is the name of the run-time region, or "*default*" if the
4583 statement didn't specify one. LMA_MEMSPEC is the name of the
4584 load-time region, or null if the statement didn't specify one.
4585 HAVE_LMA_P is true if the statement had an explicit load address.
4587 It is an error to specify both a load region and a load address. */
4589 static void
4590 lang_get_regions (region, lma_region, memspec, lma_memspec, have_lma_p)
4591 struct memory_region_struct **region, **lma_region;
4592 const char *memspec, *lma_memspec;
4593 int have_lma_p;
4595 *lma_region = lang_memory_region_lookup (lma_memspec);
4597 /* If no runtime region has been given, but the load region has
4598 been, use the load region. */
4599 if (lma_memspec != 0 && strcmp (memspec, "*default*") == 0)
4600 *region = *lma_region;
4601 else
4602 *region = lang_memory_region_lookup (memspec);
4604 if (have_lma_p && lma_memspec != 0)
4605 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4608 void
4609 lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
4610 fill_type *fill;
4611 const char *memspec;
4612 struct lang_output_section_phdr_list *phdrs;
4613 const char *lma_memspec;
4615 lang_get_regions (&current_section->region,
4616 &current_section->lma_region,
4617 memspec, lma_memspec,
4618 current_section->load_base != 0);
4619 current_section->fill = fill;
4620 current_section->phdrs = phdrs;
4621 stat_ptr = &statement_list;
4624 /* Create an absolute symbol with the given name with the value of the
4625 address of first byte of the section named.
4627 If the symbol already exists, then do nothing. */
4629 void
4630 lang_abs_symbol_at_beginning_of (secname, name)
4631 const char *secname;
4632 const char *name;
4634 struct bfd_link_hash_entry *h;
4636 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4637 if (h == (struct bfd_link_hash_entry *) NULL)
4638 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4640 if (h->type == bfd_link_hash_new
4641 || h->type == bfd_link_hash_undefined)
4643 asection *sec;
4645 h->type = bfd_link_hash_defined;
4647 sec = bfd_get_section_by_name (output_bfd, secname);
4648 if (sec == (asection *) NULL)
4649 h->u.def.value = 0;
4650 else
4651 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4653 h->u.def.section = bfd_abs_section_ptr;
4657 /* Create an absolute symbol with the given name with the value of the
4658 address of the first byte after the end of the section named.
4660 If the symbol already exists, then do nothing. */
4662 void
4663 lang_abs_symbol_at_end_of (secname, name)
4664 const char *secname;
4665 const char *name;
4667 struct bfd_link_hash_entry *h;
4669 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4670 if (h == (struct bfd_link_hash_entry *) NULL)
4671 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4673 if (h->type == bfd_link_hash_new
4674 || h->type == bfd_link_hash_undefined)
4676 asection *sec;
4678 h->type = bfd_link_hash_defined;
4680 sec = bfd_get_section_by_name (output_bfd, secname);
4681 if (sec == (asection *) NULL)
4682 h->u.def.value = 0;
4683 else
4684 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4685 + bfd_section_size (output_bfd, sec) /
4686 bfd_octets_per_byte (output_bfd));
4688 h->u.def.section = bfd_abs_section_ptr;
4692 void
4693 lang_statement_append (list, element, field)
4694 lang_statement_list_type *list;
4695 lang_statement_union_type *element;
4696 lang_statement_union_type **field;
4698 *(list->tail) = element;
4699 list->tail = field;
4702 /* Set the output format type. -oformat overrides scripts. */
4704 void
4705 lang_add_output_format (format, big, little, from_script)
4706 const char *format;
4707 const char *big;
4708 const char *little;
4709 int from_script;
4711 if (output_target == NULL || !from_script)
4713 if (command_line.endian == ENDIAN_BIG
4714 && big != NULL)
4715 format = big;
4716 else if (command_line.endian == ENDIAN_LITTLE
4717 && little != NULL)
4718 format = little;
4720 output_target = format;
4724 /* Enter a group. This creates a new lang_group_statement, and sets
4725 stat_ptr to build new statements within the group. */
4727 void
4728 lang_enter_group ()
4730 lang_group_statement_type *g;
4732 g = new_stat (lang_group_statement, stat_ptr);
4733 lang_list_init (&g->children);
4734 stat_ptr = &g->children;
4737 /* Leave a group. This just resets stat_ptr to start writing to the
4738 regular list of statements again. Note that this will not work if
4739 groups can occur inside anything else which can adjust stat_ptr,
4740 but currently they can't. */
4742 void
4743 lang_leave_group ()
4745 stat_ptr = &statement_list;
4748 /* Add a new program header. This is called for each entry in a PHDRS
4749 command in a linker script. */
4751 void
4752 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4753 const char *name;
4754 etree_type *type;
4755 boolean filehdr;
4756 boolean phdrs;
4757 etree_type *at;
4758 etree_type *flags;
4760 struct lang_phdr *n, **pp;
4762 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4763 n->next = NULL;
4764 n->name = name;
4765 n->type = exp_get_value_int (type, 0, "program header type",
4766 lang_final_phase_enum);
4767 n->filehdr = filehdr;
4768 n->phdrs = phdrs;
4769 n->at = at;
4770 n->flags = flags;
4772 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4774 *pp = n;
4777 /* Record the program header information in the output BFD. FIXME: We
4778 should not be calling an ELF specific function here. */
4780 static void
4781 lang_record_phdrs ()
4783 unsigned int alc;
4784 asection **secs;
4785 struct lang_output_section_phdr_list *last;
4786 struct lang_phdr *l;
4787 lang_statement_union_type *u;
4789 alc = 10;
4790 secs = (asection **) xmalloc (alc * sizeof (asection *));
4791 last = NULL;
4792 for (l = lang_phdr_list; l != NULL; l = l->next)
4794 unsigned int c;
4795 flagword flags;
4796 bfd_vma at;
4798 c = 0;
4799 for (u = lang_output_section_statement.head;
4800 u != NULL;
4801 u = u->output_section_statement.next)
4803 lang_output_section_statement_type *os;
4804 struct lang_output_section_phdr_list *pl;
4806 os = &u->output_section_statement;
4808 pl = os->phdrs;
4809 if (pl != NULL)
4810 last = pl;
4811 else
4813 if (os->sectype == noload_section
4814 || os->bfd_section == NULL
4815 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4816 continue;
4817 pl = last;
4820 if (os->bfd_section == NULL)
4821 continue;
4823 for (; pl != NULL; pl = pl->next)
4825 if (strcmp (pl->name, l->name) == 0)
4827 if (c >= alc)
4829 alc *= 2;
4830 secs = ((asection **)
4831 xrealloc (secs, alc * sizeof (asection *)));
4833 secs[c] = os->bfd_section;
4834 ++c;
4835 pl->used = true;
4840 if (l->flags == NULL)
4841 flags = 0;
4842 else
4843 flags = exp_get_vma (l->flags, 0, "phdr flags",
4844 lang_final_phase_enum);
4846 if (l->at == NULL)
4847 at = 0;
4848 else
4849 at = exp_get_vma (l->at, 0, "phdr load address",
4850 lang_final_phase_enum);
4852 if (! bfd_record_phdr (output_bfd, l->type,
4853 l->flags != NULL, flags, l->at != NULL,
4854 at, l->filehdr, l->phdrs, c, secs))
4855 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4858 free (secs);
4860 /* Make sure all the phdr assignments succeeded. */
4861 for (u = lang_output_section_statement.head;
4862 u != NULL;
4863 u = u->output_section_statement.next)
4865 struct lang_output_section_phdr_list *pl;
4867 if (u->output_section_statement.bfd_section == NULL)
4868 continue;
4870 for (pl = u->output_section_statement.phdrs;
4871 pl != NULL;
4872 pl = pl->next)
4873 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4874 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4875 u->output_section_statement.name, pl->name);
4879 /* Record a list of sections which may not be cross referenced. */
4881 void
4882 lang_add_nocrossref (l)
4883 struct lang_nocrossref *l;
4885 struct lang_nocrossrefs *n;
4887 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4888 n->next = nocrossref_list;
4889 n->list = l;
4890 nocrossref_list = n;
4892 /* Set notice_all so that we get informed about all symbols. */
4893 link_info.notice_all = true;
4896 /* Overlay handling. We handle overlays with some static variables. */
4898 /* The overlay virtual address. */
4899 static etree_type *overlay_vma;
4901 /* An expression for the maximum section size seen so far. */
4902 static etree_type *overlay_max;
4904 /* A list of all the sections in this overlay. */
4906 struct overlay_list {
4907 struct overlay_list *next;
4908 lang_output_section_statement_type *os;
4911 static struct overlay_list *overlay_list;
4913 /* Start handling an overlay. */
4915 void
4916 lang_enter_overlay (vma_expr)
4917 etree_type *vma_expr;
4919 /* The grammar should prevent nested overlays from occurring. */
4920 ASSERT (overlay_vma == NULL && overlay_max == NULL);
4922 overlay_vma = vma_expr;
4925 /* Start a section in an overlay. We handle this by calling
4926 lang_enter_output_section_statement with the correct VMA.
4927 lang_leave_overlay sets up the LMA and memory regions. */
4929 void
4930 lang_enter_overlay_section (name)
4931 const char *name;
4933 struct overlay_list *n;
4934 etree_type *size;
4936 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4937 0, 0, 0, 0);
4939 /* If this is the first section, then base the VMA of future
4940 sections on this one. This will work correctly even if `.' is
4941 used in the addresses. */
4942 if (overlay_list == NULL)
4943 overlay_vma = exp_nameop (ADDR, name);
4945 /* Remember the section. */
4946 n = (struct overlay_list *) xmalloc (sizeof *n);
4947 n->os = current_section;
4948 n->next = overlay_list;
4949 overlay_list = n;
4951 size = exp_nameop (SIZEOF, name);
4953 /* Arrange to work out the maximum section end address. */
4954 if (overlay_max == NULL)
4955 overlay_max = size;
4956 else
4957 overlay_max = exp_binop (MAX_K, overlay_max, size);
4960 /* Finish a section in an overlay. There isn't any special to do
4961 here. */
4963 void
4964 lang_leave_overlay_section (fill, phdrs)
4965 fill_type *fill;
4966 struct lang_output_section_phdr_list *phdrs;
4968 const char *name;
4969 char *clean, *s2;
4970 const char *s1;
4971 char *buf;
4973 name = current_section->name;
4975 /* For now, assume that "*default*" is the run-time memory region and
4976 that no load-time region has been specified. It doesn't really
4977 matter what we say here, since lang_leave_overlay will override it. */
4978 lang_leave_output_section_statement (fill, "*default*", phdrs, 0);
4980 /* Define the magic symbols. */
4982 clean = xmalloc (strlen (name) + 1);
4983 s2 = clean;
4984 for (s1 = name; *s1 != '\0'; s1++)
4985 if (ISALNUM (*s1) || *s1 == '_')
4986 *s2++ = *s1;
4987 *s2 = '\0';
4989 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4990 sprintf (buf, "__load_start_%s", clean);
4991 lang_add_assignment (exp_assop ('=', buf,
4992 exp_nameop (LOADADDR, name)));
4994 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4995 sprintf (buf, "__load_stop_%s", clean);
4996 lang_add_assignment (exp_assop ('=', buf,
4997 exp_binop ('+',
4998 exp_nameop (LOADADDR, name),
4999 exp_nameop (SIZEOF, name))));
5001 free (clean);
5004 /* Finish an overlay. If there are any overlay wide settings, this
5005 looks through all the sections in the overlay and sets them. */
5007 void
5008 lang_leave_overlay (lma_expr, nocrossrefs, fill, memspec, phdrs, lma_memspec)
5009 etree_type *lma_expr;
5010 int nocrossrefs;
5011 fill_type *fill;
5012 const char *memspec;
5013 struct lang_output_section_phdr_list *phdrs;
5014 const char *lma_memspec;
5016 lang_memory_region_type *region;
5017 lang_memory_region_type *lma_region;
5018 struct overlay_list *l;
5019 struct lang_nocrossref *nocrossref;
5021 lang_get_regions (&region, &lma_region,
5022 memspec, lma_memspec,
5023 lma_expr != 0);
5025 nocrossref = NULL;
5027 /* After setting the size of the last section, set '.' to end of the
5028 overlay region. */
5029 if (overlay_list != NULL)
5030 overlay_list->os->update_dot_tree
5031 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5033 l = overlay_list;
5034 while (l != NULL)
5036 struct overlay_list *next;
5038 if (fill != (fill_type *) 0 && l->os->fill == (fill_type *) 0)
5039 l->os->fill = fill;
5041 l->os->region = region;
5042 l->os->lma_region = lma_region;
5044 /* The first section has the load address specified in the
5045 OVERLAY statement. The rest are worked out from that.
5046 The base address is not needed (and should be null) if
5047 an LMA region was specified. */
5048 if (l->next == 0)
5049 l->os->load_base = lma_expr;
5050 else if (lma_region == 0)
5051 l->os->load_base = exp_binop ('+',
5052 exp_nameop (LOADADDR, l->next->os->name),
5053 exp_nameop (SIZEOF, l->next->os->name));
5055 if (phdrs != NULL && l->os->phdrs == NULL)
5056 l->os->phdrs = phdrs;
5058 if (nocrossrefs)
5060 struct lang_nocrossref *nc;
5062 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
5063 nc->name = l->os->name;
5064 nc->next = nocrossref;
5065 nocrossref = nc;
5068 next = l->next;
5069 free (l);
5070 l = next;
5073 if (nocrossref != NULL)
5074 lang_add_nocrossref (nocrossref);
5076 overlay_vma = NULL;
5077 overlay_list = NULL;
5078 overlay_max = NULL;
5081 /* Version handling. This is only useful for ELF. */
5083 /* This global variable holds the version tree that we build. */
5085 struct bfd_elf_version_tree *lang_elf_version_info;
5087 static int
5088 lang_vers_match_lang_c (expr, sym)
5089 struct bfd_elf_version_expr *expr;
5090 const char *sym;
5092 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5093 return 1;
5094 return fnmatch (expr->pattern, sym, 0) == 0;
5097 static int
5098 lang_vers_match_lang_cplusplus (expr, sym)
5099 struct bfd_elf_version_expr *expr;
5100 const char *sym;
5102 char *alt_sym;
5103 int result;
5105 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5106 return 1;
5108 alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
5109 if (!alt_sym)
5111 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
5112 Should we early out false in this case? */
5113 result = fnmatch (expr->pattern, sym, 0) == 0;
5115 else
5117 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5118 free (alt_sym);
5121 return result;
5124 static int
5125 lang_vers_match_lang_java (expr, sym)
5126 struct bfd_elf_version_expr *expr;
5127 const char *sym;
5129 char *alt_sym;
5130 int result;
5132 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5133 return 1;
5135 alt_sym = cplus_demangle (sym, DMGL_JAVA);
5136 if (!alt_sym)
5138 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
5139 Should we early out false in this case? */
5140 result = fnmatch (expr->pattern, sym, 0) == 0;
5142 else
5144 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5145 free (alt_sym);
5148 return result;
5151 /* This is called for each variable name or match expression. */
5153 struct bfd_elf_version_expr *
5154 lang_new_vers_pattern (orig, new, lang)
5155 struct bfd_elf_version_expr *orig;
5156 const char *new;
5157 const char *lang;
5159 struct bfd_elf_version_expr *ret;
5161 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
5162 ret->next = orig;
5163 ret->pattern = new;
5164 ret->symver = 0;
5165 ret->script = 0;
5167 if (lang == NULL || strcasecmp (lang, "C") == 0)
5168 ret->match = lang_vers_match_lang_c;
5169 else if (strcasecmp (lang, "C++") == 0)
5170 ret->match = lang_vers_match_lang_cplusplus;
5171 else if (strcasecmp (lang, "Java") == 0)
5172 ret->match = lang_vers_match_lang_java;
5173 else
5175 einfo (_("%X%P: unknown language `%s' in version information\n"),
5176 lang);
5177 ret->match = lang_vers_match_lang_c;
5180 return ldemul_new_vers_pattern (ret);
5183 /* This is called for each set of variable names and match
5184 expressions. */
5186 struct bfd_elf_version_tree *
5187 lang_new_vers_node (globals, locals)
5188 struct bfd_elf_version_expr *globals;
5189 struct bfd_elf_version_expr *locals;
5191 struct bfd_elf_version_tree *ret;
5193 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
5194 ret->next = NULL;
5195 ret->name = NULL;
5196 ret->vernum = 0;
5197 ret->globals = globals;
5198 ret->locals = locals;
5199 ret->deps = NULL;
5200 ret->name_indx = (unsigned int) -1;
5201 ret->used = 0;
5202 return ret;
5205 /* This static variable keeps track of version indices. */
5207 static int version_index;
5209 /* This is called when we know the name and dependencies of the
5210 version. */
5212 void
5213 lang_register_vers_node (name, version, deps)
5214 const char *name;
5215 struct bfd_elf_version_tree *version;
5216 struct bfd_elf_version_deps *deps;
5218 struct bfd_elf_version_tree *t, **pp;
5219 struct bfd_elf_version_expr *e1;
5221 if (name == NULL)
5222 name = "";
5224 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5225 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5227 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5228 return;
5231 /* Make sure this node has a unique name. */
5232 for (t = lang_elf_version_info; t != NULL; t = t->next)
5233 if (strcmp (t->name, name) == 0)
5234 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5236 /* Check the global and local match names, and make sure there
5237 aren't any duplicates. */
5239 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
5241 for (t = lang_elf_version_info; t != NULL; t = t->next)
5243 struct bfd_elf_version_expr *e2;
5245 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
5246 if (strcmp (e1->pattern, e2->pattern) == 0)
5247 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5248 e1->pattern);
5252 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
5254 for (t = lang_elf_version_info; t != NULL; t = t->next)
5256 struct bfd_elf_version_expr *e2;
5258 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
5259 if (strcmp (e1->pattern, e2->pattern) == 0)
5260 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5261 e1->pattern);
5265 version->deps = deps;
5266 version->name = name;
5267 if (name[0] != '\0')
5269 ++version_index;
5270 version->vernum = version_index;
5272 else
5273 version->vernum = 0;
5275 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5277 *pp = version;
5280 /* This is called when we see a version dependency. */
5282 struct bfd_elf_version_deps *
5283 lang_add_vers_depend (list, name)
5284 struct bfd_elf_version_deps *list;
5285 const char *name;
5287 struct bfd_elf_version_deps *ret;
5288 struct bfd_elf_version_tree *t;
5290 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
5291 ret->next = list;
5293 for (t = lang_elf_version_info; t != NULL; t = t->next)
5295 if (strcmp (t->name, name) == 0)
5297 ret->version_needed = t;
5298 return ret;
5302 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5304 return ret;
5307 static void
5308 lang_do_version_exports_section ()
5310 struct bfd_elf_version_expr *greg = NULL, *lreg;
5312 LANG_FOR_EACH_INPUT_STATEMENT (is)
5314 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5315 char *contents, *p;
5316 bfd_size_type len;
5318 if (sec == NULL)
5319 continue;
5321 len = bfd_section_size (is->the_bfd, sec);
5322 contents = xmalloc (len);
5323 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5324 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5326 p = contents;
5327 while (p < contents + len)
5329 greg = lang_new_vers_pattern (greg, p, NULL);
5330 p = strchr (p, '\0') + 1;
5333 /* Do not free the contents, as we used them creating the regex. */
5335 /* Do not include this section in the link. */
5336 bfd_set_section_flags (is->the_bfd, sec,
5337 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5340 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5341 lang_register_vers_node (command_line.version_exports_section,
5342 lang_new_vers_node (greg, lreg), NULL);
5345 void
5346 lang_add_unique (name)
5347 const char *name;
5349 struct unique_sections *ent;
5351 for (ent = unique_section_list; ent; ent = ent->next)
5352 if (strcmp (ent->name, name) == 0)
5353 return;
5355 ent = (struct unique_sections *) xmalloc (sizeof *ent);
5356 ent->name = xstrdup (name);
5357 ent->next = unique_section_list;
5358 unique_section_list = ent;