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