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