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