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