]> Git Repo - binutils.git/blob - ld/ldlang.c
added PowerPC PE targetting stuff
[binutils.git] / ld / ldlang.c
1 /* Linker command language support.
2    Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
3
4 This file is part of GLD, the Gnu Linker.
5
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfdlink.h"
24
25 #include "ld.h"
26 #include "ldmain.h"
27 #include "ldgram.h"
28 #include "ldexp.h"
29 #include "ldlang.h"
30 #include "ldemul.h"
31 #include "ldlex.h"
32 #include "ldmisc.h"
33 #include "ldctor.h"
34 #include "ldfile.h"
35
36 /* FORWARDS */
37 static void print_statements PARAMS ((void));
38 static void print_statement PARAMS ((lang_statement_union_type *,
39                                       lang_output_section_statement_type *));
40 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
41                                                          size_t,
42                                                          lang_statement_list_type*));
43
44
45 /* LOCALS */
46 static struct obstack stat_obstack;
47
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
50 static CONST char *startup_file;
51 static lang_statement_list_type input_file_chain;
52 static boolean placed_commons = false;
53 static lang_output_section_statement_type *default_common_section;
54 static boolean map_option_f;
55 static bfd_vma print_dot;
56 static lang_input_statement_type *first_file;
57 static lang_statement_list_type lang_output_section_statement;
58 static CONST char *current_target;
59 static CONST char *output_target;
60 static int longest_section_name = 8;
61 static lang_statement_list_type statement_list;
62
63 static void print_size PARAMS ((size_t value));
64 static void print_alignment PARAMS ((unsigned int value));
65 static void print_fill PARAMS ((fill_type value));
66 static void print_section PARAMS ((const char *name));
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 print_flags PARAMS ((int *ignore_flags));
74 static void init_os PARAMS ((lang_output_section_statement_type *s));
75 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
76                                   const char *section,
77                                   lang_input_statement_type *file,
78                                   lang_output_section_statement_type *output));
79 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
80 static void load_symbols PARAMS ((lang_input_statement_type *entry,
81                                   lang_statement_list_type *));
82 static void wild PARAMS ((lang_wild_statement_type *s,
83                           const char *section, const char *file,
84                           const char *target,
85                           lang_output_section_statement_type *output));
86 static bfd *open_output PARAMS ((const char *name));
87 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
88 static void open_input_bfds
89   PARAMS ((lang_statement_union_type *statement, boolean));
90 static void lang_reasonable_defaults PARAMS ((void));
91 static void lang_place_undefineds PARAMS ((void));
92 static void map_input_to_output_sections
93   PARAMS ((lang_statement_union_type *s,
94            const char *target,
95            lang_output_section_statement_type *output_section_statement));
96 static void print_output_section_statement
97   PARAMS ((lang_output_section_statement_type *output_section_statement));
98 static void print_assignment
99   PARAMS ((lang_assignment_statement_type *assignment,
100            lang_output_section_statement_type *output_section));
101 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
102 static void print_input_section PARAMS ((lang_input_section_type *in));
103 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
104 static void print_data_statement PARAMS ((lang_data_statement_type *data));
105 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
106 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
107 static void print_wild_statement
108   PARAMS ((lang_wild_statement_type *w,
109            lang_output_section_statement_type *os));
110 static void print_group
111   PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
112 static void print_statement PARAMS ((lang_statement_union_type *s,
113                                      lang_output_section_statement_type *os));
114 static void print_statements PARAMS ((void));
115 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
116                                    fill_type fill, unsigned int power,
117                                    asection *output_section_statement,
118                                    bfd_vma dot));
119 static bfd_vma size_input_section
120   PARAMS ((lang_statement_union_type **this_ptr,
121            lang_output_section_statement_type *output_section_statement,
122            fill_type fill, bfd_vma dot, boolean relax));
123 static void lang_finish PARAMS ((void));
124 static void lang_check PARAMS ((void));
125 static void lang_common PARAMS ((void));
126 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
127 static void lang_place_orphans PARAMS ((void));
128 static int topower PARAMS ((int));
129 static void lang_set_startof PARAMS ((void));
130 static void reset_memory_regions PARAMS ((void));
131
132 /* EXPORTS */
133 lang_output_section_statement_type *abs_output_section;
134 lang_statement_list_type *stat_ptr = &statement_list;
135 lang_statement_list_type file_chain = { 0 };
136 static const char *entry_symbol = 0;
137 boolean entry_from_cmdline;
138 boolean lang_has_input_file = false;
139 boolean had_output_filename = false;
140 boolean lang_float_flag = false;
141 boolean delete_output_file_on_failure = false;
142
143 etree_type *base; /* Relocation base - or null */
144
145
146 #if defined(__STDC__) || defined(ALMOST_STDC)
147 #define cat(a,b) a##b
148 #else
149 #define cat(a,b) a/**/b
150 #endif
151
152 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
153
154 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
155
156 #define outside_symbol_address(q) ((q)->value +   outside_section_address(q->section))
157
158 PTR
159 stat_alloc (size)
160      size_t size;
161 {
162   return obstack_alloc (&stat_obstack, size);
163 }
164
165 static void
166 print_size (value)
167      size_t value;
168 {
169   fprintf (config.map_file, "%5x", (unsigned) value);
170 }
171
172 static void
173 print_alignment (value)
174      unsigned int value;
175 {
176   fprintf (config.map_file, "2**%1u", value);
177 }
178
179 static void
180 print_fill (value)
181      fill_type value;
182 {
183   fprintf (config.map_file, "%04x", (unsigned) value);
184 }
185
186 static void
187 print_section (name)
188      CONST char *name;
189 {
190   fprintf (config.map_file, "%*s", -longest_section_name, name);
191 }
192
193 /*----------------------------------------------------------------------
194   lang_for_each_statement walks the parse tree and calls the provided
195   function for each node
196 */
197
198 static void
199 lang_for_each_statement_worker (func, s)
200      void (*func) PARAMS ((lang_statement_union_type *));
201      lang_statement_union_type *s;
202 {
203   for (; s != (lang_statement_union_type *) NULL; s = s->next)
204     {
205       func (s);
206
207       switch (s->header.type)
208         {
209         case lang_constructors_statement_enum:
210           lang_for_each_statement_worker (func, constructor_list.head);
211           break;
212         case lang_output_section_statement_enum:
213           lang_for_each_statement_worker
214             (func,
215              s->output_section_statement.children.head);
216           break;
217         case lang_wild_statement_enum:
218           lang_for_each_statement_worker
219             (func,
220              s->wild_statement.children.head);
221           break;
222         case lang_group_statement_enum:
223           lang_for_each_statement_worker (func,
224                                           s->group_statement.children.head);
225           break;
226         case lang_data_statement_enum:
227         case lang_reloc_statement_enum:
228         case lang_object_symbols_statement_enum:
229         case lang_output_statement_enum:
230         case lang_target_statement_enum:
231         case lang_input_section_enum:
232         case lang_input_statement_enum:
233         case lang_assignment_statement_enum:
234         case lang_padding_statement_enum:
235         case lang_address_statement_enum:
236           break;
237         default:
238           FAIL ();
239           break;
240         }
241     }
242 }
243
244 void
245 lang_for_each_statement (func)
246      void (*func) PARAMS ((lang_statement_union_type *));
247 {
248   lang_for_each_statement_worker (func,
249                                   statement_list.head);
250 }
251
252 /*----------------------------------------------------------------------*/
253 void
254 lang_list_init (list)
255      lang_statement_list_type *list;
256 {
257   list->head = (lang_statement_union_type *) NULL;
258   list->tail = &list->head;
259 }
260
261 /*----------------------------------------------------------------------
262
263   build a new statement node for the parse tree
264
265  */
266
267 static
268 lang_statement_union_type *
269 new_statement (type, size, list)
270      enum statement_enum type;
271      size_t size;
272      lang_statement_list_type * list;
273 {
274   lang_statement_union_type *new = (lang_statement_union_type *)
275   stat_alloc (size);
276
277   new->header.type = type;
278   new->header.next = (lang_statement_union_type *) NULL;
279   lang_statement_append (list, new, &new->header.next);
280   return new;
281 }
282
283 /*
284   Build a new input file node for the language. There are several ways
285   in which we treat an input file, eg, we only look at symbols, or
286   prefix it with a -l etc.
287
288   We can be supplied with requests for input files more than once;
289   they may, for example be split over serveral lines like foo.o(.text)
290   foo.o(.data) etc, so when asked for a file we check that we havn't
291   got it already so we don't duplicate the bfd.
292
293  */
294 static lang_input_statement_type *
295 new_afile (name, file_type, target, add_to_list)
296      CONST char *name;
297      lang_input_file_enum_type file_type;
298      CONST char *target;
299      boolean add_to_list;
300 {
301   lang_input_statement_type *p;
302
303   if (add_to_list)
304     p = new_stat (lang_input_statement, stat_ptr);
305   else
306     {
307       p = ((lang_input_statement_type *)
308            stat_alloc (sizeof (lang_input_statement_type)));
309       p->header.next = NULL;
310     }
311
312   lang_has_input_file = true;
313   p->target = target;
314   p->complained = false;
315   switch (file_type)
316     {
317     case lang_input_file_is_symbols_only_enum:
318       p->filename = name;
319       p->is_archive = false;
320       p->real = true;
321       p->local_sym_name = name;
322       p->just_syms_flag = true;
323       p->search_dirs_flag = false;
324       break;
325     case lang_input_file_is_fake_enum:
326       p->filename = name;
327       p->is_archive = false;
328       p->real = false;
329       p->local_sym_name = name;
330       p->just_syms_flag = false;
331       p->search_dirs_flag = false;
332       break;
333     case lang_input_file_is_l_enum:
334       p->is_archive = true;
335       p->filename = name;
336       p->real = true;
337       p->local_sym_name = concat ("-l", name, (const char *) NULL);
338       p->just_syms_flag = false;
339       p->search_dirs_flag = true;
340       break;
341     case lang_input_file_is_marker_enum:
342       p->filename = name;
343       p->is_archive = false;
344       p->real = false;
345       p->local_sym_name = name;
346       p->just_syms_flag = false;
347       p->search_dirs_flag = true;
348       break;
349     case lang_input_file_is_search_file_enum:
350       p->filename = name;
351       p->is_archive = false;
352       p->real = true;
353       p->local_sym_name = name;
354       p->just_syms_flag = false;
355       p->search_dirs_flag = true;
356       break;
357     case lang_input_file_is_file_enum:
358       p->filename = name;
359       p->is_archive = false;
360       p->real = true;
361       p->local_sym_name = name;
362       p->just_syms_flag = false;
363       p->search_dirs_flag = false;
364       break;
365     default:
366       FAIL ();
367     }
368   p->the_bfd = (bfd *) NULL;
369   p->asymbols = (asymbol **) NULL;
370   p->next_real_file = (lang_statement_union_type *) NULL;
371   p->next = (lang_statement_union_type *) NULL;
372   p->symbol_count = 0;
373   p->common_output_section = (asection *) NULL;
374   p->dynamic = config.dynamic_link;
375   p->loaded = false;
376   lang_statement_append (&input_file_chain,
377                          (lang_statement_union_type *) p,
378                          &p->next_real_file);
379   return p;
380 }
381
382 lang_input_statement_type *
383 lang_add_input_file (name, file_type, target)
384      CONST char *name;
385      lang_input_file_enum_type file_type;
386      CONST char *target;
387 {
388   lang_has_input_file = true;
389   return new_afile (name, file_type, target, true);
390 }
391
392 /* Build enough state so that the parser can build its tree */
393 void
394 lang_init ()
395 {
396   obstack_begin (&stat_obstack, 1000);
397
398   stat_ptr = &statement_list;
399
400   lang_list_init (stat_ptr);
401
402   lang_list_init (&input_file_chain);
403   lang_list_init (&lang_output_section_statement);
404   lang_list_init (&file_chain);
405   first_file = lang_add_input_file ((char *) NULL,
406                                     lang_input_file_is_marker_enum,
407                                     (char *) NULL);
408   abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
409
410   abs_output_section->bfd_section = bfd_abs_section_ptr;
411
412 }
413
414 /*----------------------------------------------------------------------
415  A region is an area of memory declared with the
416  MEMORY {  name:org=exp, len=exp ... }
417  syntax.
418
419  We maintain a list of all the regions here
420
421  If no regions are specified in the script, then the default is used
422  which is created when looked up to be the entire data space
423 */
424
425 static lang_memory_region_type *lang_memory_region_list;
426 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
427
428 lang_memory_region_type *
429 lang_memory_region_lookup (name)
430      CONST char *CONST name;
431 {
432
433   lang_memory_region_type *p = lang_memory_region_list;
434
435   for (p = lang_memory_region_list;
436        p != (lang_memory_region_type *) NULL;
437        p = p->next)
438     {
439       if (strcmp (p->name, name) == 0)
440         {
441           return p;
442         }
443     }
444
445 #if 0
446   /* This code used to always use the first region in the list as the
447      default region.  I changed it to instead use a region
448      encompassing all of memory as the default region.  This permits
449      NOLOAD sections to work reasonably without requiring a region.
450      People should specify what region they mean, if they really want
451      a region.  */
452   if (strcmp (name, "*default*") == 0)
453     {
454       if (lang_memory_region_list != (lang_memory_region_type *) NULL)
455         {
456           return lang_memory_region_list;
457         }
458     }
459 #endif
460
461   {
462     lang_memory_region_type *new =
463     (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
464
465     new->name = buystring (name);
466     new->next = (lang_memory_region_type *) NULL;
467
468     *lang_memory_region_list_tail = new;
469     lang_memory_region_list_tail = &new->next;
470     new->origin = 0;
471     new->length = ~(bfd_size_type)0;
472     new->current = 0;
473     new->had_full_message = false;
474
475     return new;
476   }
477 }
478
479
480 lang_output_section_statement_type *
481 lang_output_section_find (name)
482      CONST char *CONST name;
483 {
484   lang_statement_union_type *u;
485   lang_output_section_statement_type *lookup;
486
487   for (u = lang_output_section_statement.head;
488        u != (lang_statement_union_type *) NULL;
489        u = lookup->next)
490     {
491       lookup = &u->output_section_statement;
492       if (strcmp (name, lookup->name) == 0)
493         {
494           return lookup;
495         }
496     }
497   return (lang_output_section_statement_type *) NULL;
498 }
499
500 lang_output_section_statement_type *
501 lang_output_section_statement_lookup (name)
502      CONST char *CONST name;
503 {
504   lang_output_section_statement_type *lookup;
505
506   lookup = lang_output_section_find (name);
507   if (lookup == (lang_output_section_statement_type *) NULL)
508     {
509
510       lookup = (lang_output_section_statement_type *)
511         new_stat (lang_output_section_statement, stat_ptr);
512       lookup->region = (lang_memory_region_type *) NULL;
513       lookup->fill = 0;
514       lookup->block_value = 1;
515       lookup->name = name;
516
517       lookup->next = (lang_statement_union_type *) NULL;
518       lookup->bfd_section = (asection *) NULL;
519       lookup->processed = false;
520       lookup->loadable = 1;
521       lookup->addr_tree = (etree_type *) NULL;
522       lang_list_init (&lookup->children);
523
524       lookup->memspec = (CONST char *) NULL;
525       lookup->flags = 0;
526       lookup->subsection_alignment = -1;
527       lookup->section_alignment = -1;
528       lookup->load_base = (union etree_union *) NULL;
529
530       lang_statement_append (&lang_output_section_statement,
531                              (lang_statement_union_type *) lookup,
532                              &lookup->next);
533     }
534   return lookup;
535 }
536
537 /*ARGSUSED*/
538 static void
539 print_flags (ignore_flags)
540      int *ignore_flags;
541 {
542   fprintf (config.map_file, "(");
543 #if 0
544   if (flags->flag_read)
545     fprintf (outfile, "R");
546   if (flags->flag_write)
547     fprintf (outfile, "W");
548   if (flags->flag_executable)
549     fprintf (outfile, "X");
550   if (flags->flag_loadable)
551     fprintf (outfile, "L");
552 #endif
553  fprintf (config.map_file, ")");
554 }
555
556 void
557 lang_map ()
558 {
559   lang_memory_region_type *m;
560
561   fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
562 #ifdef BFD64
563   fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
564 #else
565   fprintf (config.map_file,
566            "name\t\torigin   length   r_size   c_size    is    attributes\n");
567
568 #endif
569   for (m = lang_memory_region_list;
570        m != (lang_memory_region_type *) NULL;
571        m = m->next)
572     {
573       fprintf (config.map_file, "%-16s", m->name);
574       print_address (m->origin);
575       print_space ();
576       print_address ((bfd_vma)m->length);
577       print_space ();
578       print_address ((bfd_vma)m->old_length);
579       print_space();
580       print_address (m->current - m->origin);
581       print_space();
582       if (m->old_length)
583        fprintf (config.map_file, " %2d%%  ",
584                 (int) ((m->current - m->origin) * 100 / m->old_length));
585       print_flags (&m->flags);
586       fprintf (config.map_file, "\n");
587     }
588   fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
589   fprintf (config.map_file, "output   input     virtual\n");
590   fprintf (config.map_file, "section  section   address    tsize\n\n");
591
592   print_statements ();
593
594 }
595
596 /*
597  *
598  */
599 static void
600 init_os (s)
601      lang_output_section_statement_type * s;
602 {
603 /*  asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
604   section_userdata_type *new =
605   (section_userdata_type *)
606   stat_alloc (sizeof (section_userdata_type));
607
608   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
609   if (s->bfd_section == (asection *) NULL)
610     s->bfd_section = bfd_make_section (output_bfd, s->name);
611   if (s->bfd_section == (asection *) NULL)
612     {
613       einfo ("%P%F: output format %s cannot represent section called %s\n",
614              output_bfd->xvec->name, s->name);
615     }
616   s->bfd_section->output_section = s->bfd_section;
617
618   /* We initialize an output sections output offset to minus its own */
619   /* vma to allow us to output a section through itself */
620   s->bfd_section->output_offset = 0;
621   get_userdata (s->bfd_section) = (PTR) new;
622
623 }
624
625 /***********************************************************************
626   The wild routines.
627
628   These expand statements like *(.text) and foo.o to a list of
629   explicit actions, like foo.o(.text), bar.o(.text) and
630   foo.o(.text,.data) .
631
632   The toplevel routine, wild, takes a statement, section, file and
633   target. If either the section or file is null it is taken to be the
634   wildcard. Seperate lang_input_section statements are created for
635   each part of the expanstion, and placed after the statement provided.
636
637 */
638
639 void
640 wild_doit (ptr, section, output, file)
641      lang_statement_list_type * ptr;
642      asection * section;
643      lang_output_section_statement_type * output;
644      lang_input_statement_type * file;
645 {
646   if (output->bfd_section == (asection *) NULL)
647     init_os (output);
648
649   if (section != (asection *) NULL
650       && section->output_section == (asection *) NULL)
651   {
652     /* Add a section reference to the list */
653     lang_input_section_type *new = new_stat (lang_input_section, ptr);
654
655     new->section = section;
656     new->ifile = file;
657     section->output_section = output->bfd_section;
658
659     /* We don't copy the SEC_NEVER_LOAD flag from an input section to
660        an output section, because we want to be able to include a
661        SEC_NEVER_LOAD section in the middle of an otherwise loaded
662        section (I don't know why we want to do this, but we do).
663        build_link_order in ldwrite.c handles this case by turning the
664        embedded SEC_NEVER_LOAD section into a fill.  */
665     section->output_section->flags |=
666       section->flags & (flagword) (~ SEC_NEVER_LOAD);
667
668     if (!output->loadable) 
669     {
670       /* Turn off load flag */
671       output->bfd_section->flags &= ~SEC_LOAD;
672       output->bfd_section->flags |= SEC_NEVER_LOAD;
673     }
674     if (section->alignment_power > output->bfd_section->alignment_power)
675     {
676       output->bfd_section->alignment_power = section->alignment_power;
677     }
678     /* If supplied an aligmnet, then force it */
679     if (output->section_alignment != -1)
680     {
681       output->bfd_section->alignment_power = output->section_alignment;
682     }
683   }
684 }
685
686 static void
687 wild_section (ptr, section, file, output)
688      lang_wild_statement_type *ptr;
689      const char *section;
690      lang_input_statement_type *file;
691      lang_output_section_statement_type *output;
692 {
693   if (file->just_syms_flag == false)
694     {
695       register asection *s;
696
697       for (s = file->the_bfd->sections; s != NULL; s = s->next)
698         {
699           /* Attach all sections named SECTION.  If SECTION is NULL,
700              then attach all sections.
701
702              Previously, if SECTION was NULL, this code did not call
703              wild_doit if the SEC_IS_COMMON flag was set for the
704              section.  I did not understand that, and I took it out.
705              [email protected].  */
706
707           if (section == NULL
708               || strcmp (bfd_get_section_name (file->the_bfd, s),
709                          section) == 0)
710             wild_doit (&ptr->children, s, output, file);
711         }
712     }
713 }
714
715 /* passed a file name (which must have been seen already and added to
716    the statement tree. We will see if it has been opened already and
717    had its symbols read. If not then we'll read it.
718
719    Archives are pecuilar here. We may open them once, but if they do
720    not define anything we need at the time, they won't have all their
721    symbols read. If we need them later, we'll have to redo it.  */
722 static lang_input_statement_type *
723 lookup_name (name)
724      CONST char *name;
725 {
726   lang_input_statement_type *search;
727
728   for (search = (lang_input_statement_type *) input_file_chain.head;
729        search != (lang_input_statement_type *) NULL;
730        search = (lang_input_statement_type *) search->next_real_file)
731     {
732       if (search->filename == (char *) NULL && name == (char *) NULL)
733         return search;
734       if (search->filename != (char *) NULL
735           && name != (char *) NULL
736           && strcmp (search->filename, name) == 0)
737         break;
738     }
739
740   if (search == (lang_input_statement_type *) NULL)
741     search = new_afile (name, lang_input_file_is_file_enum, default_target,
742                         false);
743
744   /* If we have already added this file, or this file is not real
745      (FIXME: can that ever actually happen?) or the name is NULL
746      (FIXME: can that ever actually happen?) don't add this file.  */
747   if (search->loaded
748       || ! search->real
749       || search->filename == (const char *) NULL)
750     return search;
751
752   load_symbols (search, (lang_statement_list_type *) NULL);
753
754   return search;
755 }
756
757 /* Get the symbols for an input file.  */
758
759 static void
760 load_symbols (entry, place)
761      lang_input_statement_type *entry;
762      lang_statement_list_type *place;
763 {
764   char **matching;
765
766   if (entry->loaded)
767     return;
768
769   ldfile_open_file (entry);
770
771   if (! bfd_check_format (entry->the_bfd, bfd_archive)
772       && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
773     {
774       bfd_error_type err;
775       lang_statement_list_type *hold;
776
777       err = bfd_get_error ();
778       if (err == bfd_error_file_ambiguously_recognized)
779         {
780           char **p;
781
782           einfo ("%B: file not recognized: %E\n", entry->the_bfd);
783           einfo ("%B: matching formats:", entry->the_bfd);
784           for (p = matching; *p != NULL; p++)
785             einfo (" %s", *p);
786           einfo ("%F\n");
787         }
788       else if (err != bfd_error_file_not_recognized
789                || place == NULL)
790         einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
791
792       /* Try to interpret the file as a linker script.  */
793
794       bfd_close (entry->the_bfd);
795       entry->the_bfd = NULL;
796
797       ldfile_open_command_file (entry->filename);
798
799       hold = stat_ptr;
800       stat_ptr = place;
801
802       ldfile_assumed_script = true;
803       parser_input = input_script;
804       yyparse ();
805       ldfile_assumed_script = false;
806
807       stat_ptr = hold;
808
809       return;
810     }
811
812   /* We don't call ldlang_add_file for an archive.  Instead, the
813      add_symbols entry point will call ldlang_add_file, via the
814      add_archive_element callback, for each element of the archive
815      which is used.  */
816   switch (bfd_get_format (entry->the_bfd))
817     {
818     default:
819       break;
820
821     case bfd_object:
822       ldlang_add_file (entry);
823       if (trace_files || trace_file_tries)
824         info_msg ("%I\n", entry);
825       break;
826
827     case bfd_archive:
828       if (whole_archive)
829         {
830           bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
831                                                       (bfd *) NULL);
832           while (member != NULL)
833             {
834               if (! bfd_check_format (member, bfd_object))
835                 einfo ("%F%B: object %B in archive is not object\n",
836                        entry->the_bfd, member);
837               if (! ((*link_info.callbacks->add_archive_element)
838                      (&link_info, member, "-whole-archive")))
839                 abort ();
840               if (! bfd_link_add_symbols (member, &link_info))
841                 einfo ("%F%B: could not read symbols: %E\n", member);
842               member = bfd_openr_next_archived_file (entry->the_bfd,
843                                                      member);
844             }
845
846           entry->loaded = true;
847
848           return;
849         }
850     }
851
852   if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
853     einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
854
855   entry->loaded = true;
856 }
857
858 static void
859 wild (s, section, file, target, output)
860      lang_wild_statement_type * s;
861      CONST char *section;
862      CONST char *file;
863      CONST char *target;
864      lang_output_section_statement_type * output;
865 {
866   lang_input_statement_type *f;
867
868   if (file == (char *) NULL)
869     {
870       /* Perform the iteration over all files in the list */
871       for (f = (lang_input_statement_type *) file_chain.head;
872            f != (lang_input_statement_type *) NULL;
873            f = (lang_input_statement_type *) f->next)
874         {
875           wild_section (s, section, f, output);
876         }
877     }
878   else
879     {
880       /* Perform the iteration over a single file */
881       wild_section (s, section, lookup_name (file), output);
882     }
883   if (section != (char *) NULL
884       && strcmp (section, "COMMON") == 0
885    && default_common_section == (lang_output_section_statement_type *) NULL)
886     {
887       /* Remember the section that common is going to incase we later
888          get something which doesn't know where to put it */
889       default_common_section = output;
890     }
891 }
892
893 /*
894   read in all the files
895   */
896
897 static bfd *
898 open_output (name)
899      CONST char *name;
900 {
901   bfd *output;
902
903   if (output_target == (char *) NULL)
904     {
905       if (current_target != (char *) NULL)
906         output_target = current_target;
907       else
908         output_target = default_target;
909     }
910   output = bfd_openw (name, output_target);
911
912   if (output == (bfd *) NULL)
913     {
914       if (bfd_get_error () == bfd_error_invalid_target)
915         {
916           einfo ("%P%F: target %s not found\n", output_target);
917         }
918       einfo ("%P%F: cannot open output file %s: %E\n", name);
919     }
920
921   delete_output_file_on_failure = 1;
922
923   /*  output->flags |= D_PAGED;*/
924
925   if (! bfd_set_format (output, bfd_object))
926     einfo ("%P%F:%s: can not make object file: %E\n", name);
927   if (! bfd_set_arch_mach (output,
928                            ldfile_output_architecture,
929                            ldfile_output_machine))
930     einfo ("%P%F:%s: can not set architecture: %E\n", name);
931
932   link_info.hash = bfd_link_hash_table_create (output);
933   if (link_info.hash == (struct bfd_link_hash_table *) NULL)
934     einfo ("%P%F: can not create link hash table: %E\n");
935
936   bfd_set_gp_size (output, g_switch_value);
937   return output;
938 }
939
940
941
942
943 static void
944 ldlang_open_output (statement)
945      lang_statement_union_type * statement;
946 {
947   switch (statement->header.type)
948     {
949     case lang_output_statement_enum:
950       ASSERT (output_bfd == (bfd *) NULL);
951       output_bfd = open_output (statement->output_statement.name);
952       ldemul_set_output_arch ();
953       if (config.magic_demand_paged && !link_info.relocateable)
954         output_bfd->flags |= D_PAGED;
955       else
956         output_bfd->flags &= ~D_PAGED;
957       if (config.text_read_only)
958         output_bfd->flags |= WP_TEXT;
959       else
960         output_bfd->flags &= ~WP_TEXT;
961       if (config.traditional_format)
962         output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
963       else
964         output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
965       break;
966
967     case lang_target_statement_enum:
968       current_target = statement->target_statement.target;
969       break;
970     default:
971       break;
972     }
973 }
974
975 /* Open all the input files.  */
976
977 static void
978 open_input_bfds (s, force)
979      lang_statement_union_type *s;
980      boolean force;
981 {
982   for (; s != (lang_statement_union_type *) NULL; s = s->next)
983     {
984       switch (s->header.type)
985         {
986         case lang_constructors_statement_enum:
987           open_input_bfds (constructor_list.head, force);
988           break;
989         case lang_output_section_statement_enum:
990           open_input_bfds (s->output_section_statement.children.head, force);
991           break;
992         case lang_wild_statement_enum:
993           /* Maybe we should load the file's symbols */
994           if (s->wild_statement.filename)
995             (void) lookup_name (s->wild_statement.filename);
996           open_input_bfds (s->wild_statement.children.head, force);
997           break;
998         case lang_group_statement_enum:
999           {
1000             struct bfd_link_hash_entry *undefs;
1001
1002             /* We must continually search the entries in the group
1003                until no new symbols are added to the list of undefined
1004                symbols.  */
1005
1006             do
1007               {
1008                 undefs = link_info.hash->undefs_tail;
1009                 open_input_bfds (s->group_statement.children.head, true);
1010               }
1011             while (undefs != link_info.hash->undefs_tail);
1012           }
1013           break;
1014         case lang_target_statement_enum:
1015           current_target = s->target_statement.target;
1016           break;
1017         case lang_input_statement_enum:
1018           if (s->input_statement.real == true)
1019             {
1020               lang_statement_list_type add;
1021
1022               s->input_statement.target = current_target;
1023
1024               /* If we are being called from within a group, and this
1025                  is an archive which has already been searched, then
1026                  force it to be researched.  */
1027               if (force
1028                   && s->input_statement.loaded
1029                   && bfd_check_format (s->input_statement.the_bfd,
1030                                        bfd_archive))
1031                 s->input_statement.loaded = false;
1032
1033               lang_list_init (&add);
1034
1035               load_symbols (&s->input_statement, &add);
1036
1037               if (add.head != NULL)
1038                 {
1039                   *add.tail = s->next;
1040                   s->next = add.head;
1041                 }
1042             }
1043           break;
1044         default:
1045           break;
1046         }
1047     }
1048 }
1049
1050 /* If there are [COMMONS] statements, put a wild one into the bss section */
1051
1052 static void
1053 lang_reasonable_defaults ()
1054 {
1055 #if 0
1056   lang_output_section_statement_lookup (".text");
1057   lang_output_section_statement_lookup (".data");
1058
1059   default_common_section =
1060     lang_output_section_statement_lookup (".bss");
1061
1062
1063   if (placed_commons == false)
1064     {
1065       lang_wild_statement_type *new =
1066       new_stat (lang_wild_statement,
1067                 &default_common_section->children);
1068
1069       new->section_name = "COMMON";
1070       new->filename = (char *) NULL;
1071       lang_list_init (&new->children);
1072     }
1073 #endif
1074
1075 }
1076
1077 /*
1078  Add the supplied name to the symbol table as an undefined reference.
1079  Remove items from the chain as we open input bfds
1080  */
1081 typedef struct ldlang_undef_chain_list
1082 {
1083   struct ldlang_undef_chain_list *next;
1084   char *name;
1085 }                       ldlang_undef_chain_list_type;
1086
1087 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1088
1089 void
1090 ldlang_add_undef (name)
1091      CONST char *CONST name;
1092 {
1093   ldlang_undef_chain_list_type *new =
1094     ((ldlang_undef_chain_list_type *)
1095      stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1096
1097   new->next = ldlang_undef_chain_list_head;
1098   ldlang_undef_chain_list_head = new;
1099
1100   new->name = buystring (name);
1101 }
1102
1103 /* Run through the list of undefineds created above and place them
1104    into the linker hash table as undefined symbols belonging to the
1105    script file.
1106 */
1107 static void
1108 lang_place_undefineds ()
1109 {
1110   ldlang_undef_chain_list_type *ptr;
1111
1112   for (ptr = ldlang_undef_chain_list_head;
1113        ptr != (ldlang_undef_chain_list_type *) NULL;
1114        ptr = ptr->next)
1115     {
1116       struct bfd_link_hash_entry *h;
1117
1118       h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1119       if (h == (struct bfd_link_hash_entry *) NULL)
1120         einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1121       if (h->type == bfd_link_hash_new)
1122         {
1123           h->type = bfd_link_hash_undefined;
1124           h->u.undef.abfd = NULL;
1125           bfd_link_add_undef (link_info.hash, h);
1126         }
1127     }
1128 }
1129
1130 /* Open input files and attatch to output sections */
1131 static void
1132 map_input_to_output_sections (s, target, output_section_statement)
1133      lang_statement_union_type * s;
1134      CONST char *target;
1135      lang_output_section_statement_type * output_section_statement;
1136 {
1137   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1138     {
1139       switch (s->header.type)
1140         {
1141
1142
1143         case lang_wild_statement_enum:
1144           wild (&s->wild_statement, s->wild_statement.section_name,
1145                 s->wild_statement.filename, target,
1146                 output_section_statement);
1147
1148           break;
1149         case lang_constructors_statement_enum:
1150           map_input_to_output_sections (constructor_list.head,
1151                                         target,
1152                                         output_section_statement);
1153           break;
1154         case lang_output_section_statement_enum:
1155           map_input_to_output_sections (s->output_section_statement.children.head,
1156                                         target,
1157                                         &s->output_section_statement);
1158           break;
1159         case lang_output_statement_enum:
1160           break;
1161         case lang_target_statement_enum:
1162           target = s->target_statement.target;
1163           break;
1164         case lang_group_statement_enum:
1165           map_input_to_output_sections (s->group_statement.children.head,
1166                                         target,
1167                                         output_section_statement);
1168           break;
1169         case lang_fill_statement_enum:
1170         case lang_input_section_enum:
1171         case lang_object_symbols_statement_enum:
1172         case lang_data_statement_enum:
1173         case lang_reloc_statement_enum:
1174         case lang_assignment_statement_enum:
1175         case lang_padding_statement_enum:
1176         case lang_input_statement_enum:
1177           if (output_section_statement != NULL
1178               && output_section_statement->bfd_section == NULL)
1179             init_os (output_section_statement);
1180           break;
1181         case lang_afile_asection_pair_statement_enum:
1182           FAIL ();
1183           break;
1184         case lang_address_statement_enum:
1185           /* Mark the specified section with the supplied address */
1186           {
1187             lang_output_section_statement_type *os =
1188               lang_output_section_statement_lookup
1189                 (s->address_statement.section_name);
1190
1191             if (os->bfd_section == NULL)
1192               init_os (os);
1193             os->addr_tree = s->address_statement.address;
1194           }
1195           break;
1196         }
1197     }
1198 }
1199
1200 static void
1201 print_output_section_statement (output_section_statement)
1202      lang_output_section_statement_type * output_section_statement;
1203 {
1204   asection *section = output_section_statement->bfd_section;
1205
1206   print_nl ();
1207   print_section (output_section_statement->name);
1208
1209
1210   if (section)
1211   {
1212     print_dot = section->vma;
1213     print_space ();
1214     print_section ("");
1215     print_space ();
1216     print_address (section->vma);
1217     print_space ();
1218     print_size (section->_raw_size);
1219     print_space ();
1220     print_size(section->_cooked_size);
1221     print_space ();
1222     print_alignment (section->alignment_power);
1223     print_space ();
1224 #if 0
1225     fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1226     print_flags (stdout, &output_section_statement->flags);
1227 #endif
1228     if (section->flags & SEC_LOAD)
1229      fprintf (config.map_file, "load ");
1230     if (section->flags & SEC_ALLOC)
1231      fprintf (config.map_file, "alloc ");
1232     if (section->flags & SEC_RELOC)
1233      fprintf (config.map_file, "reloc ");
1234     if (section->flags & SEC_HAS_CONTENTS)
1235      fprintf (config.map_file, "contents ");
1236
1237   }
1238   else
1239   {
1240     fprintf (config.map_file, " (no attached output section)");
1241   }
1242   print_nl ();
1243   if (output_section_statement->load_base)
1244     {
1245       bfd_vma b = exp_get_abs_int(output_section_statement->load_base,
1246                                 0, "output base", lang_final_phase_enum);
1247       fprintf (config.map_file, "Output address   ");
1248       fprintf_vma (config.map_file, b);
1249       fprintf (config.map_file, "\n");
1250     }
1251   if (output_section_statement->section_alignment >= 0
1252       || output_section_statement->subsection_alignment >= 0) 
1253   {
1254     fprintf (config.map_file, "\t\t\t\t\tforced alignment ");
1255     if (output_section_statement->section_alignment >= 0) 
1256     {
1257       fprintf (config.map_file, "section 2**%d ",output_section_statement->section_alignment );
1258     }
1259     if ( output_section_statement->subsection_alignment >= 0) 
1260     {
1261       fprintf (config.map_file, "subsection 2**%d ",output_section_statement->subsection_alignment );
1262     }
1263   
1264     print_nl ();
1265   }
1266   print_statement (output_section_statement->children.head,
1267                    output_section_statement);
1268
1269 }
1270
1271 static void
1272 print_assignment (assignment, output_section)
1273      lang_assignment_statement_type * assignment;
1274      lang_output_section_statement_type * output_section;
1275 {
1276   etree_value_type result;
1277
1278   print_section ("");
1279   print_space ();
1280   print_section ("");
1281   print_space ();
1282   print_address (print_dot);
1283   print_space ();
1284   result = exp_fold_tree (assignment->exp->assign.src,
1285                           output_section,
1286                           lang_final_phase_enum,
1287                           print_dot,
1288                           &print_dot);
1289
1290   if (result.valid)
1291     {
1292       print_address (result.value);
1293     }
1294   else
1295     {
1296       fprintf (config.map_file, "*undefined*");
1297     }
1298   print_space ();
1299   exp_print_tree (assignment->exp);
1300
1301   fprintf (config.map_file, "\n");
1302 }
1303
1304 static void
1305 print_input_statement (statm)
1306      lang_input_statement_type * statm;
1307 {
1308   if (statm->filename != (char *) NULL)
1309     {
1310       fprintf (config.map_file, "LOAD %s\n", statm->filename);
1311     }
1312 }
1313
1314 /* Print all the defined symbols for the abfd provided by in the supplied
1315    section.
1316 */
1317
1318 static boolean 
1319 print_one_symbol (hash_entry, ptr)
1320 struct  bfd_link_hash_entry *hash_entry;
1321 PTR ptr;
1322 {
1323   asection * sec = (asection *)ptr;
1324
1325   if (hash_entry->type == bfd_link_hash_defined
1326       || hash_entry->type == bfd_link_hash_defweak)
1327     {
1328       if (sec == hash_entry->u.def.section) {
1329         print_section ("");
1330         fprintf (config.map_file, " ");
1331         print_section ("");
1332         fprintf (config.map_file, " ");
1333         print_address (hash_entry->u.def.value + outside_section_address (sec));
1334         fprintf (config.map_file, "              %s", hash_entry->root.string);
1335         print_nl ();
1336       }
1337     }
1338
1339   return true;
1340 }
1341
1342 static void
1343 print_input_section (in)
1344      lang_input_section_type * in;
1345 {
1346   asection *i = in->section;
1347   bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1348
1349   if (size != 0)
1350     {
1351       print_section ("");
1352       fprintf (config.map_file, " ");
1353       print_section (i->name);
1354       fprintf (config.map_file, " ");
1355       if (i->output_section)
1356         {
1357           print_address (i->output_section->vma + i->output_offset);
1358           fprintf (config.map_file, " ");
1359           print_size (i->_raw_size);
1360           fprintf (config.map_file, " ");
1361           print_size(i->_cooked_size);
1362           fprintf (config.map_file, " ");
1363           print_alignment (i->alignment_power);
1364           fprintf (config.map_file, " ");
1365           if (in->ifile)
1366             {
1367
1368               bfd *abfd = in->ifile->the_bfd;
1369
1370               if (in->ifile->just_syms_flag == true)
1371                 {
1372                   fprintf (config.map_file, "symbols only ");
1373                 }
1374
1375               fprintf (config.map_file, " %s ", abfd->xvec->name);
1376               if (abfd->my_archive != (bfd *) NULL)
1377                 {
1378                   fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1379                            abfd->filename);
1380                 }
1381               else
1382                 {
1383                   fprintf (config.map_file, "%s", abfd->filename);
1384                 }
1385               fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1386               print_nl ();
1387
1388               /* Print all the symbols */
1389               bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1390             }
1391           else
1392             {
1393               print_nl ();
1394             }
1395
1396
1397           print_dot = outside_section_address (i) + size;
1398         }
1399       else
1400         {
1401           fprintf (config.map_file, "No output section allocated\n");
1402         }
1403     }
1404 }
1405
1406 static void
1407 print_fill_statement (fill)
1408      lang_fill_statement_type * fill;
1409 {
1410   fprintf (config.map_file, "FILL mask ");
1411   print_fill (fill->fill);
1412 }
1413
1414 static void
1415 print_data_statement (data)
1416      lang_data_statement_type * data;
1417 {
1418 /*  bfd_vma value; */
1419   print_section ("");
1420   print_space ();
1421   print_section ("");
1422   print_space ();
1423 /*  ASSERT(print_dot == data->output_vma);*/
1424
1425   print_address (data->output_vma + data->output_section->vma);
1426   print_space ();
1427   print_address (data->value);
1428   print_space ();
1429   switch (data->type)
1430     {
1431     case BYTE:
1432       fprintf (config.map_file, "BYTE ");
1433       print_dot += BYTE_SIZE;
1434       break;
1435     case SHORT:
1436       fprintf (config.map_file, "SHORT ");
1437       print_dot += SHORT_SIZE;
1438       break;
1439     case LONG:
1440       fprintf (config.map_file, "LONG ");
1441       print_dot += LONG_SIZE;
1442       break;
1443     case QUAD:
1444       fprintf (config.map_file, "QUAD ");
1445       print_dot += QUAD_SIZE;
1446       break;
1447     }
1448
1449   exp_print_tree (data->exp);
1450
1451   fprintf (config.map_file, "\n");
1452 }
1453
1454 /* Print a reloc statement.  */
1455
1456 static void
1457 print_reloc_statement (reloc)
1458      lang_reloc_statement_type *reloc;
1459 {
1460   print_section ("");
1461   print_space ();
1462   print_section ("");
1463   print_space ();
1464
1465 /*  ASSERT(print_dot == data->output_vma);*/
1466
1467   print_address (reloc->output_vma + reloc->output_section->vma);
1468   print_space ();
1469   print_address (reloc->addend_value);
1470   print_space ();
1471
1472   fprintf (config.map_file, "RELOC %s ", reloc->howto->name);
1473
1474   print_dot += bfd_get_reloc_size (reloc->howto);
1475
1476   exp_print_tree (reloc->addend_exp);
1477
1478   fprintf (config.map_file, "\n");
1479 }  
1480
1481 static void
1482 print_padding_statement (s)
1483      lang_padding_statement_type * s;
1484 {
1485   print_section ("");
1486   print_space ();
1487   print_section ("*fill*");
1488   print_space ();
1489   print_address (s->output_offset + s->output_section->vma);
1490   print_space ();
1491   print_size (s->size);
1492   print_space ();
1493   print_fill (s->fill);
1494   print_nl ();
1495
1496   print_dot = s->output_offset + s->output_section->vma + s->size;
1497
1498 }
1499
1500 static void
1501 print_wild_statement (w, os)
1502      lang_wild_statement_type * w;
1503      lang_output_section_statement_type * os;
1504 {
1505   fprintf (config.map_file, " from ");
1506   if (w->filename != (char *) NULL)
1507     {
1508       fprintf (config.map_file, "%s", w->filename);
1509     }
1510   else
1511     {
1512       fprintf (config.map_file, "*");
1513     }
1514   if (w->section_name != (char *) NULL)
1515     {
1516       fprintf (config.map_file, "(%s)", w->section_name);
1517     }
1518   else
1519     {
1520       fprintf (config.map_file, "(*)");
1521     }
1522   print_nl ();
1523   print_statement (w->children.head, os);
1524
1525 }
1526
1527 /* Print a group statement.  */
1528
1529 static void
1530 print_group (s, os)
1531      lang_group_statement_type *s;
1532      lang_output_section_statement_type *os;
1533 {
1534   fprintf (config.map_file, "START GROUP\n");
1535   print_statement (s->children.head, os);
1536   fprintf (config.map_file, "END GROUP\n");
1537 }
1538
1539 static void
1540 print_statement (s, os)
1541      lang_statement_union_type * s;
1542      lang_output_section_statement_type * os;
1543 {
1544   while (s)
1545     {
1546       switch (s->header.type)
1547         {
1548           case lang_constructors_statement_enum:
1549           fprintf (config.map_file, "constructors:\n");
1550           print_statement (constructor_list.head, os);
1551           break;
1552         case lang_wild_statement_enum:
1553           print_wild_statement (&s->wild_statement, os);
1554           break;
1555         default:
1556           fprintf (config.map_file, "Fail with %d\n", s->header.type);
1557           FAIL ();
1558           break;
1559         case lang_address_statement_enum:
1560           fprintf (config.map_file, "address\n");
1561           break;
1562         case lang_object_symbols_statement_enum:
1563           fprintf (config.map_file, "object symbols\n");
1564           break;
1565         case lang_fill_statement_enum:
1566           print_fill_statement (&s->fill_statement);
1567           break;
1568         case lang_data_statement_enum:
1569           print_data_statement (&s->data_statement);
1570           break;
1571         case lang_reloc_statement_enum:
1572           print_reloc_statement (&s->reloc_statement);
1573           break;
1574         case lang_input_section_enum:
1575           print_input_section (&s->input_section);
1576           break;
1577         case lang_padding_statement_enum:
1578           print_padding_statement (&s->padding_statement);
1579           break;
1580         case lang_output_section_statement_enum:
1581           print_output_section_statement (&s->output_section_statement);
1582           break;
1583         case lang_assignment_statement_enum:
1584           print_assignment (&s->assignment_statement,
1585                             os);
1586           break;
1587         case lang_target_statement_enum:
1588           fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1589           break;
1590         case lang_output_statement_enum:
1591           fprintf (config.map_file, "OUTPUT(%s %s)\n",
1592                    s->output_statement.name,
1593                    output_target ? output_target : "");
1594           break;
1595         case lang_input_statement_enum:
1596           print_input_statement (&s->input_statement);
1597           break;
1598         case lang_group_statement_enum:
1599           print_group (&s->group_statement, os);
1600           break;
1601         case lang_afile_asection_pair_statement_enum:
1602           FAIL ();
1603           break;
1604         }
1605       s = s->next;
1606     }
1607 }
1608
1609
1610 static void
1611 print_statements ()
1612 {
1613   print_statement (statement_list.head,
1614                    abs_output_section);
1615
1616 }
1617
1618 static bfd_vma
1619 insert_pad (this_ptr, fill, power, output_section_statement, dot)
1620      lang_statement_union_type ** this_ptr;
1621      fill_type fill;
1622      unsigned int power;
1623      asection * output_section_statement;
1624      bfd_vma dot;
1625 {
1626   /* Align this section first to the
1627      input sections requirement, then
1628      to the output section's requirement.
1629      If this alignment is > than any seen before,
1630      then record it too. Perform the alignment by
1631      inserting a magic 'padding' statement.
1632      */
1633
1634   unsigned int alignment_needed = align_power (dot, power) - dot;
1635
1636   if (alignment_needed != 0)
1637     {
1638       lang_statement_union_type *new =
1639         ((lang_statement_union_type *)
1640          stat_alloc (sizeof (lang_padding_statement_type)));
1641
1642       /* Link into existing chain */
1643       new->header.next = *this_ptr;
1644       *this_ptr = new;
1645       new->header.type = lang_padding_statement_enum;
1646       new->padding_statement.output_section = output_section_statement;
1647       new->padding_statement.output_offset =
1648         dot - output_section_statement->vma;
1649       new->padding_statement.fill = fill;
1650       new->padding_statement.size = alignment_needed;
1651     }
1652
1653
1654   /* Remember the most restrictive alignment */
1655   if (power > output_section_statement->alignment_power)
1656     {
1657       output_section_statement->alignment_power = power;
1658     }
1659   output_section_statement->_raw_size += alignment_needed;
1660   return alignment_needed + dot;
1661
1662 }
1663
1664 /* Work out how much this section will move the dot point */
1665 static bfd_vma
1666 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1667      lang_statement_union_type ** this_ptr;
1668      lang_output_section_statement_type * output_section_statement;
1669      fill_type fill;
1670      bfd_vma dot;
1671      boolean relax;
1672 {
1673   lang_input_section_type *is = &((*this_ptr)->input_section);
1674   asection *i = is->section;
1675
1676   if (is->ifile->just_syms_flag == false)
1677     {
1678       if (output_section_statement->subsection_alignment != -1)
1679        i->alignment_power =
1680         output_section_statement->subsection_alignment;
1681
1682       dot = insert_pad (this_ptr, fill, i->alignment_power,
1683                         output_section_statement->bfd_section, dot);
1684
1685       /* Remember where in the output section this input section goes */
1686
1687       i->output_offset = dot - output_section_statement->bfd_section->vma;
1688
1689       /* Mark how big the output section must be to contain this now
1690          */
1691       if (i->_cooked_size != 0)
1692         dot += i->_cooked_size;
1693       else
1694         dot += i->_raw_size;
1695       output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1696     }
1697   else
1698     {
1699       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1700     }
1701
1702   return dot;
1703 }
1704
1705 /* This variable indicates whether bfd_relax_section should be called
1706    again.  */
1707
1708 static boolean relax_again;
1709
1710 /* Set the sizes for all the output sections.  */
1711
1712 bfd_vma
1713 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1714      lang_statement_union_type * s;
1715      lang_output_section_statement_type * output_section_statement;
1716      lang_statement_union_type ** prev;
1717      fill_type fill;
1718      bfd_vma dot;
1719      boolean relax;
1720 {
1721   /* Size up the sections from their constituent parts */
1722   for (; s != (lang_statement_union_type *) NULL; s = s->next)
1723   {
1724     switch (s->header.type)
1725     {
1726
1727      case lang_output_section_statement_enum:
1728      {
1729        bfd_vma after;
1730        lang_output_section_statement_type *os = &s->output_section_statement;
1731
1732        if (os->bfd_section == NULL)
1733          {
1734            /* This section was never actually created.  */
1735            break;
1736          }
1737
1738        /* If this is a COFF shared library section, use the size and
1739           address from the input section.  FIXME: This is COFF
1740           specific; it would be cleaner if there were some other way
1741           to do this, but nothing simple comes to mind.  */
1742        if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
1743          {
1744            asection *input;
1745
1746            if (os->children.head == NULL
1747                || os->children.head->next != NULL
1748                || os->children.head->header.type != lang_input_section_enum)
1749              einfo ("%P%X: Internal error on COFF shared library section %s",
1750                     os->name);
1751
1752            input = os->children.head->input_section.section;
1753            bfd_set_section_vma (os->bfd_section->owner,
1754                                 os->bfd_section,
1755                                 bfd_section_vma (input->owner, input));
1756            os->bfd_section->_raw_size = input->_raw_size;
1757            break;
1758          }
1759
1760        if (bfd_is_abs_section (os->bfd_section))
1761        {
1762          /* No matter what happens, an abs section starts at zero */
1763          ASSERT (os->bfd_section->vma == 0);
1764        }
1765        else
1766        {
1767          if (os->addr_tree == (etree_type *) NULL)
1768          {
1769            /* No address specified for this section, get one
1770               from the region specification
1771               */
1772            if (os->region == (lang_memory_region_type *) NULL)
1773            {
1774              os->region = lang_memory_region_lookup ("*default*");
1775            }
1776            dot = os->region->current;
1777            if (os->section_alignment == -1)
1778              dot = align_power (dot, os->bfd_section->alignment_power);
1779          }
1780          else
1781          {
1782            etree_value_type r;
1783
1784            r = exp_fold_tree (os->addr_tree,
1785                               abs_output_section,
1786                               lang_allocating_phase_enum,
1787                               dot, &dot);
1788            if (r.valid == false)
1789            {
1790              einfo ("%F%S: non constant address expression for section %s\n",
1791                     os->name);
1792            }
1793            dot = r.value;
1794          }
1795          /* The section starts here */
1796          /* First, align to what the section needs */
1797
1798          if (os->section_alignment != -1)
1799            dot = align_power (dot, os->section_alignment);
1800
1801          bfd_set_section_vma (0, os->bfd_section, dot);
1802          
1803          os->bfd_section->output_offset = 0;
1804        }
1805
1806        (void) lang_size_sections (os->children.head, os, &os->children.head,
1807                                   os->fill, dot, relax);
1808        /* Ignore the size of the input sections, use the vma and size to */
1809        /* align against */
1810
1811        after = ALIGN_N (os->bfd_section->vma +
1812                         os->bfd_section->_raw_size,
1813                         /* The coercion here is important, see ld.h.  */
1814                         (bfd_vma) os->block_value);
1815
1816        if (bfd_is_abs_section (os->bfd_section))
1817          ASSERT (after == os->bfd_section->vma);
1818        else
1819          os->bfd_section->_raw_size = after - os->bfd_section->vma;
1820        dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1821        os->processed = true;
1822
1823        /* Replace into region ? */
1824        if (os->region != (lang_memory_region_type *) NULL)
1825          {
1826            os->region->current = dot;
1827            /* Make sure this isn't silly.  */
1828            if ((os->region->current < os->region->origin)
1829                || (os->region->current
1830                    > os->region->origin + os->region->length))
1831              {
1832                if (os->addr_tree != (etree_type *) NULL)
1833                  {
1834                    einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
1835                           os->region->current,
1836                           os->bfd_section->owner,
1837                           os->bfd_section->name,
1838                           os->region->name);
1839                  }
1840                else
1841                  {
1842                    einfo ("%X%P: region %s is full (%B section %s)\n",
1843                           os->region->name,
1844                           os->bfd_section->owner,
1845                           os->bfd_section->name);
1846                  }
1847                /* Reset the region pointer.  */
1848                os->region->current = os->region->origin;
1849              }
1850          }
1851      }
1852      break;
1853
1854      case lang_constructors_statement_enum:
1855       dot = lang_size_sections (constructor_list.head,
1856                                 output_section_statement,
1857                                 &s->wild_statement.children.head,
1858                                 fill,
1859                                 dot, relax);
1860       break;
1861
1862      case lang_data_statement_enum:
1863      {
1864        unsigned int size = 0;
1865
1866        s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1867        s->data_statement.output_section =
1868         output_section_statement->bfd_section;
1869
1870        switch (s->data_statement.type)
1871        {
1872         case QUAD:
1873          size = QUAD_SIZE;
1874          break;
1875         case LONG:
1876          size = LONG_SIZE;
1877          break;
1878         case SHORT:
1879          size = SHORT_SIZE;
1880          break;
1881         case BYTE:
1882          size = BYTE_SIZE;
1883          break;
1884
1885        }
1886        dot += size;
1887        output_section_statement->bfd_section->_raw_size += size;
1888        /* The output section gets contents, and then we inspect for
1889           any flags set in the input script which override any ALLOC */
1890        output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
1891        if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
1892          output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
1893        }
1894      }
1895       break;
1896
1897      case lang_reloc_statement_enum:
1898      {
1899        int size;
1900
1901        s->reloc_statement.output_vma =
1902          dot - output_section_statement->bfd_section->vma;
1903        s->reloc_statement.output_section =
1904          output_section_statement->bfd_section;
1905        size = bfd_get_reloc_size (s->reloc_statement.howto);
1906        dot += size;
1907        output_section_statement->bfd_section->_raw_size += size;
1908      }
1909      break;
1910      
1911      case lang_wild_statement_enum:
1912
1913       dot = lang_size_sections (s->wild_statement.children.head,
1914                                 output_section_statement,
1915                                 &s->wild_statement.children.head,
1916
1917                                 fill, dot, relax);
1918
1919       break;
1920
1921      case lang_object_symbols_statement_enum:
1922       link_info.create_object_symbols_section =
1923         output_section_statement->bfd_section;
1924       break;
1925      case lang_output_statement_enum:
1926      case lang_target_statement_enum:
1927       break;
1928      case lang_input_section_enum:
1929       {
1930         asection *i;
1931
1932         i = (*prev)->input_section.section;
1933         if (! relax)
1934           i->_cooked_size = i->_raw_size;
1935         else
1936           {
1937             boolean again;
1938
1939             if (! bfd_relax_section (i->owner, i, &link_info, &again))
1940               einfo ("%P%F: can't relax section: %E\n");
1941             if (again)
1942               relax_again = true;
1943           }
1944         dot = size_input_section (prev,
1945                                   output_section_statement,
1946                                   output_section_statement->fill,
1947                                   dot, relax);
1948       }
1949       break;
1950      case lang_input_statement_enum:
1951       break;
1952      case lang_fill_statement_enum:
1953       s->fill_statement.output_section = output_section_statement->bfd_section;
1954
1955       fill = s->fill_statement.fill;
1956       break;
1957      case lang_assignment_statement_enum:
1958      {
1959        bfd_vma newdot = dot;
1960
1961        exp_fold_tree (s->assignment_statement.exp,
1962                       output_section_statement,
1963                       lang_allocating_phase_enum,
1964                       dot,
1965                       &newdot);
1966
1967        if (newdot != dot && !relax)
1968          {
1969            /* The assignment changed dot.  Insert a pad.  */
1970            if (output_section_statement == abs_output_section)
1971              {
1972                /* If we don't have an output section, then just adjust
1973                   the default memory address.  */
1974                lang_memory_region_lookup ("*default*")->current = newdot;
1975              }
1976            else
1977              {
1978                lang_statement_union_type *new =
1979                  ((lang_statement_union_type *)
1980                   stat_alloc (sizeof (lang_padding_statement_type)));
1981
1982                /* Link into existing chain */
1983                new->header.next = *prev;
1984                *prev = new;
1985                new->header.type = lang_padding_statement_enum;
1986                new->padding_statement.output_section =
1987                  output_section_statement->bfd_section;
1988                new->padding_statement.output_offset =
1989                  dot - output_section_statement->bfd_section->vma;
1990                new->padding_statement.fill = fill;
1991                new->padding_statement.size = newdot - dot;
1992                output_section_statement->bfd_section->_raw_size +=
1993                  new->padding_statement.size;
1994              }
1995
1996            dot = newdot;
1997          }
1998      }
1999      break;
2000
2001    case lang_padding_statement_enum:
2002      /* If we are relaxing, and this is not the first pass, some
2003         padding statements may have been inserted during previous
2004         passes.  We may have to move the padding statement to a new
2005         location if dot has a different value at this point in this
2006         pass than it did at this point in the previous pass.  */
2007      s->padding_statement.output_offset =
2008        dot - output_section_statement->bfd_section->vma;
2009      dot += s->padding_statement.size;
2010      break;
2011
2012      case lang_group_statement_enum:
2013        dot = lang_size_sections (s->group_statement.children.head,
2014                                  output_section_statement,
2015                                  &s->group_statement.children.head,
2016                                  fill, dot, relax);
2017        break;
2018
2019      default:
2020       FAIL ();
2021       break;
2022
2023       /* This can only get here when relaxing is turned on */
2024
2025      case lang_address_statement_enum:
2026       break;
2027     }
2028     prev = &s->header.next;
2029   }
2030   return dot;
2031 }
2032
2033 bfd_vma
2034 lang_do_assignments (s, output_section_statement, fill, dot)
2035      lang_statement_union_type * s;
2036      lang_output_section_statement_type * output_section_statement;
2037      fill_type fill;
2038      bfd_vma dot;
2039 {
2040   for (; s != (lang_statement_union_type *) NULL; s = s->next)
2041     {
2042       switch (s->header.type)
2043         {
2044         case lang_constructors_statement_enum:
2045           dot = lang_do_assignments (constructor_list.head,
2046                                      output_section_statement,
2047                                      fill,
2048                                      dot);
2049           break;
2050
2051         case lang_output_section_statement_enum:
2052           {
2053             lang_output_section_statement_type *os =
2054               &(s->output_section_statement);
2055
2056             if (os->bfd_section != NULL)
2057               {
2058                 dot = os->bfd_section->vma;
2059                 (void) lang_do_assignments (os->children.head, os,
2060                                             os->fill, dot);
2061                 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2062               }
2063             if (os->load_base) 
2064               {
2065                 /* If nothing has been placed into the output section then
2066                    it won't have a bfd_section. */
2067                 if (os->bfd_section) 
2068                   {
2069                     os->bfd_section->lma 
2070                       = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2071                   }
2072               }
2073           }
2074           break;
2075         case lang_wild_statement_enum:
2076
2077           dot = lang_do_assignments (s->wild_statement.children.head,
2078                                      output_section_statement,
2079                                      fill, dot);
2080
2081           break;
2082
2083         case lang_object_symbols_statement_enum:
2084         case lang_output_statement_enum:
2085         case lang_target_statement_enum:
2086 #if 0
2087         case lang_common_statement_enum:
2088 #endif
2089           break;
2090         case lang_data_statement_enum:
2091           {
2092             etree_value_type value;
2093
2094             value = exp_fold_tree (s->data_statement.exp,
2095                                    abs_output_section,
2096                                    lang_final_phase_enum, dot, &dot);
2097             s->data_statement.value = value.value;
2098             if (value.valid == false)
2099               einfo ("%F%P: invalid data statement\n");
2100           }
2101           switch (s->data_statement.type)
2102             {
2103             case QUAD:
2104               dot += QUAD_SIZE;
2105               break;
2106             case LONG:
2107               dot += LONG_SIZE;
2108               break;
2109             case SHORT:
2110               dot += SHORT_SIZE;
2111               break;
2112             case BYTE:
2113               dot += BYTE_SIZE;
2114               break;
2115             }
2116           break;
2117
2118         case lang_reloc_statement_enum:
2119           {
2120             etree_value_type value;
2121
2122             value = exp_fold_tree (s->reloc_statement.addend_exp,
2123                                    abs_output_section,
2124                                    lang_final_phase_enum, dot, &dot);
2125             s->reloc_statement.addend_value = value.value;
2126             if (value.valid == false)
2127               einfo ("%F%P: invalid reloc statement\n");
2128           }
2129           dot += bfd_get_reloc_size (s->reloc_statement.howto);
2130           break;
2131
2132         case lang_input_section_enum:
2133           {
2134             asection *in = s->input_section.section;
2135
2136             if (in->_cooked_size != 0)
2137               dot += in->_cooked_size;
2138             else
2139               dot += in->_raw_size;
2140           }
2141           break;
2142
2143         case lang_input_statement_enum:
2144           break;
2145         case lang_fill_statement_enum:
2146           fill = s->fill_statement.fill;
2147           break;
2148         case lang_assignment_statement_enum:
2149           {
2150             exp_fold_tree (s->assignment_statement.exp,
2151                            output_section_statement,
2152                            lang_final_phase_enum,
2153                            dot,
2154                            &dot);
2155           }
2156
2157           break;
2158         case lang_padding_statement_enum:
2159           dot += s->padding_statement.size;
2160           break;
2161
2162         case lang_group_statement_enum:
2163           dot = lang_do_assignments (s->group_statement.children.head,
2164                                      output_section_statement,
2165                                      fill, dot);
2166
2167           break;
2168
2169         default:
2170           FAIL ();
2171           break;
2172         case lang_address_statement_enum:
2173           break;
2174         }
2175
2176     }
2177   return dot;
2178 }
2179
2180 /* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
2181    operator .startof. (section_name), it produces an undefined symbol
2182    .startof.section_name.  Similarly, when it sees
2183    .sizeof. (section_name), it produces an undefined symbol
2184    .sizeof.section_name.  For all the output sections, we look for
2185    such symbols, and set them to the correct value.  */
2186
2187 static void
2188 lang_set_startof ()
2189 {
2190   asection *s;
2191
2192   for (s = output_bfd->sections; s != NULL; s = s->next)
2193     {
2194       const char *secname;
2195       char *buf;
2196       struct bfd_link_hash_entry *h;
2197
2198       secname = bfd_get_section_name (output_bfd, s);
2199       buf = xmalloc (10 + strlen (secname));
2200
2201       sprintf (buf, ".startof.%s", secname);
2202       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2203       if (h != NULL && h->type == bfd_link_hash_undefined)
2204         {
2205           h->type = bfd_link_hash_defined;
2206           h->u.def.value = bfd_get_section_vma (output_bfd, s);
2207           h->u.def.section = bfd_abs_section_ptr;
2208         }
2209
2210       sprintf (buf, ".sizeof.%s", secname);
2211       h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2212       if (h != NULL && h->type == bfd_link_hash_undefined)
2213         {
2214           h->type = bfd_link_hash_defined;
2215           if (s->_cooked_size != 0)
2216             h->u.def.value = s->_cooked_size;
2217           else
2218             h->u.def.value = s->_raw_size;
2219           h->u.def.section = bfd_abs_section_ptr;
2220         }
2221
2222       free (buf);
2223     }
2224 }
2225
2226 static void
2227 lang_finish ()
2228 {
2229   struct bfd_link_hash_entry *h;
2230   boolean warn;
2231
2232   if (link_info.relocateable || link_info.shared)
2233     warn = false;
2234   else
2235     warn = true;
2236
2237   if (entry_symbol == (char *) NULL)
2238     {
2239       /* No entry has been specified.  Look for start, but don't warn
2240          if we don't find it.  */
2241       entry_symbol = "start";
2242       warn = false;
2243     }
2244
2245   h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2246   if (h != (struct bfd_link_hash_entry *) NULL
2247       && (h->type == bfd_link_hash_defined
2248           || h->type == bfd_link_hash_defweak))
2249     {
2250       bfd_vma val;
2251
2252       val = (h->u.def.value
2253              + bfd_get_section_vma (output_bfd,
2254                                     h->u.def.section->output_section)
2255              + h->u.def.section->output_offset);
2256       if (! bfd_set_start_address (output_bfd, val))
2257         einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2258     }
2259   else
2260     {
2261       asection *ts;
2262
2263       /* Can't find the entry symbol.  Use the first address in the
2264          text section.  */
2265       ts = bfd_get_section_by_name (output_bfd, ".text");
2266       if (ts != (asection *) NULL)
2267         {
2268           if (warn)
2269             einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2270                    entry_symbol, bfd_get_section_vma (output_bfd, ts));
2271           if (! bfd_set_start_address (output_bfd,
2272                                        bfd_get_section_vma (output_bfd, ts)))
2273             einfo ("%P%F: can't set start address\n");
2274         }
2275       else
2276         {
2277           if (warn)
2278             einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2279                    entry_symbol);
2280         }
2281     }
2282 }
2283
2284 /* Check that the architecture of all the input files is compatible
2285    with the output file.  Also call the backend to let it do any
2286    other checking that is needed.  */
2287
2288 static void
2289 lang_check ()
2290 {
2291   lang_statement_union_type *file;
2292   bfd *input_bfd;
2293   CONST bfd_arch_info_type *compatible;
2294
2295   for (file = file_chain.head;
2296        file != (lang_statement_union_type *) NULL;
2297        file = file->input_statement.next)
2298     {
2299       input_bfd = file->input_statement.the_bfd;
2300       compatible = bfd_arch_get_compatible (input_bfd,
2301                                             output_bfd);
2302       if (compatible == NULL)
2303         einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2304                bfd_printable_name (input_bfd), input_bfd,
2305                bfd_printable_name (output_bfd));
2306
2307       else
2308         bfd_merge_private_bfd_data (input_bfd, output_bfd);
2309     }
2310 }
2311
2312 /* Look through all the global common symbols and attach them to the
2313    correct section.  The -sort-common command line switch may be used
2314    to roughly sort the entries by size.  */
2315
2316 static void
2317 lang_common ()
2318 {
2319   if (link_info.relocateable
2320       && ! command_line.force_common_definition)
2321     return;
2322
2323   if (! config.sort_common)
2324     bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2325   else
2326     {
2327       int power;
2328
2329       for (power = 4; power >= 0; power--)
2330         bfd_link_hash_traverse (link_info.hash, lang_one_common,
2331                                 (PTR) &power);
2332     }
2333 }
2334
2335 /* Place one common symbol in the correct section.  */
2336
2337 static boolean
2338 lang_one_common (h, info)
2339      struct bfd_link_hash_entry *h;
2340      PTR info;
2341 {
2342   unsigned int power_of_two;
2343   bfd_vma size;
2344   asection *section;
2345
2346   if (h->type != bfd_link_hash_common)
2347     return true;
2348
2349   size = h->u.c.size;
2350   power_of_two = h->u.c.p->alignment_power;
2351
2352   if (config.sort_common
2353       && power_of_two < (unsigned int) *(int *) info)
2354     return true;
2355
2356   section = h->u.c.p->section;
2357
2358   /* Increase the size of the section.  */
2359   section->_raw_size = ALIGN_N (section->_raw_size,
2360                                 (bfd_size_type) (1 << power_of_two));
2361
2362   /* Adjust the alignment if necessary.  */
2363   if (power_of_two > section->alignment_power)
2364     section->alignment_power = power_of_two;
2365
2366   /* Change the symbol from common to defined.  */
2367   h->type = bfd_link_hash_defined;
2368   h->u.def.section = section;
2369   h->u.def.value = section->_raw_size;
2370
2371   /* Increase the size of the section.  */
2372   section->_raw_size += size;
2373
2374   /* Make sure the section is allocated in memory.  */
2375   section->flags |= SEC_ALLOC;
2376
2377   if (config.map_file != NULL)
2378     fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2379              h->root.string, (unsigned long) size,
2380              (unsigned long) h->u.def.value, section->owner->filename);
2381
2382   return true;
2383 }
2384
2385 /*
2386 run through the input files and ensure that every input
2387 section has somewhere to go. If one is found without
2388 a destination then create an input request and place it
2389 into the statement tree.
2390 */
2391
2392 static void
2393 lang_place_orphans ()
2394 {
2395   lang_input_statement_type *file;
2396
2397   for (file = (lang_input_statement_type *) file_chain.head;
2398        file != (lang_input_statement_type *) NULL;
2399        file = (lang_input_statement_type *) file->next)
2400     {
2401       asection *s;
2402
2403       for (s = file->the_bfd->sections;
2404            s != (asection *) NULL;
2405            s = s->next)
2406         {
2407           if (s->output_section == (asection *) NULL)
2408             {
2409               /* This section of the file is not attatched, root
2410                  around for a sensible place for it to go */
2411
2412               if (file->just_syms_flag)
2413                 {
2414                   /* We are only retrieving symbol values from this
2415                      file.  We want the symbols to act as though the
2416                      values in the file are absolute.  */
2417                   s->output_section = bfd_abs_section_ptr;
2418                   s->output_offset = s->vma;
2419                 }
2420               else if (file->common_section == s)
2421                 {
2422                   /* This is a lonely common section which must
2423                      have come from an archive. We attatch to the
2424                      section with the wildcard  */
2425                   if (! link_info.relocateable
2426                       && ! command_line.force_common_definition)
2427                     {
2428                       if (default_common_section ==
2429                           (lang_output_section_statement_type *) NULL)
2430                         {
2431                           info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2432
2433                           default_common_section =
2434                             lang_output_section_statement_lookup (".bss");
2435
2436                         }
2437                       wild_doit (&default_common_section->children, s,
2438                                  default_common_section, file);
2439                     }
2440                 }
2441               else if (ldemul_place_orphan (file, s))
2442                 ;
2443               else
2444                 {
2445                   lang_output_section_statement_type *os =
2446                   lang_output_section_statement_lookup (s->name);
2447
2448                   wild_doit (&os->children, s, os, file);
2449                 }
2450             }
2451         }
2452     }
2453 }
2454
2455
2456 void
2457 lang_set_flags (ptr, flags)
2458      int *ptr;
2459      CONST char *flags;
2460 {
2461   boolean state = false;
2462
2463   *ptr = 0;
2464   while (*flags)
2465     {
2466       if (*flags == '!')
2467         {
2468           state = false;
2469           flags++;
2470         }
2471       else
2472         state = true;
2473       switch (*flags)
2474         {
2475         case 'R':
2476           /*      ptr->flag_read = state; */
2477           break;
2478         case 'W':
2479           /*      ptr->flag_write = state; */
2480           break;
2481         case 'X':
2482           /*      ptr->flag_executable= state;*/
2483           break;
2484         case 'L':
2485         case 'I':
2486           /*      ptr->flag_loadable= state;*/
2487           break;
2488         default:
2489           einfo ("%P%F: invalid syntax in flags\n");
2490           break;
2491         }
2492       flags++;
2493     }
2494 }
2495
2496 /* Call a function on each input file.  This function will be called
2497    on an archive, but not on the elements.  */
2498
2499 void
2500 lang_for_each_input_file (func)
2501      void (*func) PARAMS ((lang_input_statement_type *));
2502 {
2503   lang_input_statement_type *f;
2504
2505   for (f = (lang_input_statement_type *) input_file_chain.head;
2506        f != NULL;
2507        f = (lang_input_statement_type *) f->next_real_file)
2508     func (f);
2509 }
2510
2511 /* Call a function on each file.  The function will be called on all
2512    the elements of an archive which are included in the link, but will
2513    not be called on the archive file itself.  */
2514
2515 void
2516 lang_for_each_file (func)
2517      void (*func) PARAMS ((lang_input_statement_type *));
2518 {
2519   lang_input_statement_type *f;
2520
2521   for (f = (lang_input_statement_type *) file_chain.head;
2522        f != (lang_input_statement_type *) NULL;
2523        f = (lang_input_statement_type *) f->next)
2524     {
2525       func (f);
2526     }
2527 }
2528
2529 #if 0
2530
2531 /* Not used.  */
2532
2533 void
2534 lang_for_each_input_section (func)
2535      void (*func) PARAMS ((bfd * ab, asection * as));
2536 {
2537   lang_input_statement_type *f;
2538
2539   for (f = (lang_input_statement_type *) file_chain.head;
2540        f != (lang_input_statement_type *) NULL;
2541        f = (lang_input_statement_type *) f->next)
2542     {
2543       asection *s;
2544
2545       for (s = f->the_bfd->sections;
2546            s != (asection *) NULL;
2547            s = s->next)
2548         {
2549           func (f->the_bfd, s);
2550         }
2551     }
2552 }
2553
2554 #endif
2555
2556 void
2557 ldlang_add_file (entry)
2558      lang_input_statement_type * entry;
2559 {
2560   bfd **pp;
2561
2562   lang_statement_append (&file_chain,
2563                          (lang_statement_union_type *) entry,
2564                          &entry->next);
2565
2566   /* The BFD linker needs to have a list of all input BFDs involved in
2567      a link.  */
2568   ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2569   ASSERT (entry->the_bfd != output_bfd);
2570   for (pp = &link_info.input_bfds;
2571        *pp != (bfd *) NULL;
2572        pp = &(*pp)->link_next)
2573     ;
2574   *pp = entry->the_bfd;
2575   entry->the_bfd->usrdata = (PTR) entry;
2576   bfd_set_gp_size (entry->the_bfd, g_switch_value);
2577 }
2578
2579 void
2580 lang_add_output (name, from_script)
2581      CONST char *name;
2582      int from_script;
2583 {
2584   /* Make -o on command line override OUTPUT in script.  */
2585   if (had_output_filename == false || !from_script)
2586     {
2587       output_filename = name;
2588       had_output_filename = true;
2589     }
2590 }
2591
2592
2593 static lang_output_section_statement_type *current_section;
2594
2595 static int
2596 topower (x)
2597      int x;
2598 {
2599   unsigned int i = 1;
2600   int l;
2601
2602   if (x < 0)
2603     return -1;
2604
2605   for (l = 0; l < 32; l++) 
2606     {
2607       if (i >= (unsigned int) x)
2608         return l;
2609       i <<= 1;
2610     }
2611
2612   return 0;
2613 }
2614 void
2615 lang_enter_output_section_statement (output_section_statement_name,
2616                                      address_exp, flags, block_value,
2617                                      align, subalign, ebase)
2618      const char *output_section_statement_name;
2619      etree_type * address_exp;
2620      int flags;
2621      bfd_vma block_value;
2622      etree_type *align;
2623      etree_type *subalign;
2624      etree_type *ebase;
2625 {
2626   lang_output_section_statement_type *os;
2627
2628   current_section =
2629    os =
2630     lang_output_section_statement_lookup (output_section_statement_name);
2631
2632
2633
2634   /* Add this statement to tree */
2635   /*  add_statement(lang_output_section_statement_enum,
2636       output_section_statement);*/
2637   /* Make next things chain into subchain of this */
2638
2639   if (os->addr_tree ==
2640       (etree_type *) NULL)
2641   {
2642     os->addr_tree =
2643      address_exp;
2644   }
2645   os->flags = flags;
2646   if (flags & SEC_NEVER_LOAD)
2647    os->loadable = 0;
2648   else
2649    os->loadable = 1;
2650   os->block_value = block_value ? block_value : 1;
2651   stat_ptr = &os->children;
2652
2653   os->subsection_alignment = topower(
2654    exp_get_value_int(subalign, -1,
2655                      "subsection alignment",
2656                      0));
2657   os->section_alignment = topower(
2658    exp_get_value_int(align, -1,
2659                      "section alignment", 0));
2660
2661   os->load_base = ebase;
2662 }
2663
2664
2665 void
2666 lang_final ()
2667 {
2668   lang_output_statement_type *new =
2669     new_stat (lang_output_statement, stat_ptr);
2670
2671   new->name = output_filename;
2672 }
2673
2674 /* Reset the current counters in the regions */
2675 static void
2676 reset_memory_regions ()
2677 {
2678   lang_memory_region_type *p = lang_memory_region_list;
2679
2680   for (p = lang_memory_region_list;
2681        p != (lang_memory_region_type *) NULL;
2682        p = p->next)
2683     {
2684       p->old_length = (bfd_size_type) (p->current - p->origin);
2685       p->current = p->origin;
2686     }
2687 }
2688
2689 void
2690 lang_process ()
2691 {
2692   lang_reasonable_defaults ();
2693   current_target = default_target;
2694
2695   lang_for_each_statement (ldlang_open_output); /* Open the output file */
2696
2697   ldemul_create_output_section_statements ();
2698
2699   /* Add to the hash table all undefineds on the command line */
2700   lang_place_undefineds ();
2701
2702   /* Create a bfd for each input file */
2703   current_target = default_target;
2704   open_input_bfds (statement_list.head, false);
2705
2706   ldemul_after_open ();
2707
2708   /* Build all sets based on the information gathered from the input
2709      files.  */
2710   ldctor_build_sets ();
2711
2712   /* Size up the common data */
2713   lang_common ();
2714
2715   /* Run through the contours of the script and attatch input sections
2716      to the correct output sections
2717      */
2718   map_input_to_output_sections (statement_list.head, (char *) NULL,
2719                                 (lang_output_section_statement_type *) NULL);
2720
2721
2722   /* Find any sections not attatched explicitly and handle them */
2723   lang_place_orphans ();
2724
2725   ldemul_before_allocation ();
2726
2727   /* Now run around and relax if we can */
2728   if (command_line.relax)
2729     {
2730       /* First time round is a trial run to get the 'worst case'
2731          addresses of the objects if there was no relaxing.  */
2732       lang_size_sections (statement_list.head,
2733                           abs_output_section,
2734                           &(statement_list.head), 0, (bfd_vma) 0, false);
2735
2736       /* Keep relaxing until bfd_relax_section gives up.  */
2737       do
2738         {
2739           reset_memory_regions ();
2740
2741           relax_again = false;
2742
2743           /* Do all the assignments with our current guesses as to
2744              section sizes.  */
2745           lang_do_assignments (statement_list.head,
2746                                abs_output_section,
2747                                (fill_type) 0, (bfd_vma) 0);
2748
2749           /* Perform another relax pass - this time we know where the
2750              globals are, so can make better guess.  */
2751           lang_size_sections (statement_list.head,
2752                               abs_output_section,
2753                               &(statement_list.head), 0, (bfd_vma) 0, true);
2754         }
2755       while (relax_again);
2756     }
2757   else
2758     {
2759       /* Size up the sections.  */
2760       lang_size_sections (statement_list.head,
2761                           abs_output_section,
2762                           &(statement_list.head), 0, (bfd_vma) 0, false);
2763     }
2764
2765   /* See if anything special should be done now we know how big
2766      everything is.  */
2767   ldemul_after_allocation ();
2768
2769   /* Fix any .startof. or .sizeof. symbols.  */
2770   lang_set_startof ();
2771
2772   /* Do all the assignments, now that we know the final restingplaces
2773      of all the symbols */
2774
2775   lang_do_assignments (statement_list.head,
2776                        abs_output_section,
2777                        (fill_type) 0, (bfd_vma) 0);
2778
2779   /* Make sure that we're not mixing architectures */
2780
2781   lang_check ();
2782
2783   /* Final stuffs */
2784
2785   ldemul_finish ();
2786   lang_finish ();
2787 }
2788
2789 /* EXPORTED TO YACC */
2790
2791 void
2792 lang_add_wild (section_name, filename)
2793      CONST char *CONST section_name;
2794      CONST char *CONST filename;
2795 {
2796   lang_wild_statement_type *new = new_stat (lang_wild_statement,
2797                                             stat_ptr);
2798
2799   if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2800     {
2801       placed_commons = true;
2802     }
2803   if (filename != (char *) NULL)
2804     {
2805       lang_has_input_file = true;
2806     }
2807   new->section_name = section_name;
2808   new->filename = filename;
2809   lang_list_init (&new->children);
2810 }
2811
2812 void
2813 lang_section_start (name, address)
2814      CONST char *name;
2815      etree_type * address;
2816 {
2817   lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2818
2819   ad->section_name = name;
2820   ad->address = address;
2821 }
2822
2823 /* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
2824    because of a -e argument on the command line, or zero if this is
2825    called by ENTRY in a linker script.  Command line arguments take
2826    precedence.  */
2827
2828 /* WINDOWS_NT.  When an entry point has been specified, we will also force
2829    this symbol to be defined by calling ldlang_add_undef (equivalent to 
2830    having switch -u entry_name on the command line).  The reason we do
2831    this is so that the user doesn't have to because they would have to use
2832    the -u switch if they were specifying an entry point other than 
2833    _mainCRTStartup.  Specifically, if creating a windows application, entry
2834    point _WinMainCRTStartup must be specified.
2835      What I have found for non console applications (entry not _mainCRTStartup)
2836    is that the .obj that contains mainCRTStartup is brought in since it is
2837    the first encountered in libc.lib and it has other symbols in it which will
2838    be pulled in by the link process.  To avoid this, adding -u with the entry
2839    point name specified forces the correct .obj to be used.  We can avoid
2840    making the user do this by always adding the entry point name as an
2841    undefined symbol.  */
2842
2843 void
2844 lang_add_entry (name, cmdline)
2845      CONST char *name;
2846      boolean cmdline;
2847 {
2848   if (entry_symbol == NULL
2849       || cmdline
2850       || ! entry_from_cmdline)
2851     {
2852       entry_symbol = name;
2853       entry_from_cmdline = cmdline;
2854     }
2855 #if 0 
2856   /* don't do this yet.  It seems to work (the executables run), but the 
2857      image created is very different from what I was getting before indicating
2858      that something else is being pulled in.  When everything else is working,
2859      then try to put this back in to see if it will do the right thing for
2860      other more complicated applications */
2861   ldlang_add_undef (name);
2862 #endif
2863 }
2864
2865 void
2866 lang_add_target (name)
2867      CONST char *name;
2868 {
2869   lang_target_statement_type *new = new_stat (lang_target_statement,
2870                                               stat_ptr);
2871
2872   new->target = name;
2873
2874 }
2875
2876 void
2877 lang_add_map (name)
2878      CONST char *name;
2879 {
2880   while (*name)
2881     {
2882       switch (*name)
2883         {
2884           case 'F':
2885           map_option_f = true;
2886           break;
2887         }
2888       name++;
2889     }
2890 }
2891
2892 void
2893 lang_add_fill (exp)
2894      int exp;
2895 {
2896   lang_fill_statement_type *new = new_stat (lang_fill_statement,
2897                                             stat_ptr);
2898
2899   new->fill = exp;
2900 }
2901
2902 void
2903 lang_add_data (type, exp)
2904      int type;
2905      union etree_union *exp;
2906 {
2907
2908   lang_data_statement_type *new = new_stat (lang_data_statement,
2909                                             stat_ptr);
2910
2911   new->exp = exp;
2912   new->type = type;
2913
2914 }
2915
2916 /* Create a new reloc statement.  RELOC is the BFD relocation type to
2917    generate.  HOWTO is the corresponding howto structure (we could
2918    look this up, but the caller has already done so).  SECTION is the
2919    section to generate a reloc against, or NAME is the name of the
2920    symbol to generate a reloc against.  Exactly one of SECTION and
2921    NAME must be NULL.  ADDEND is an expression for the addend.  */
2922
2923 void
2924 lang_add_reloc (reloc, howto, section, name, addend)
2925      bfd_reloc_code_real_type reloc;
2926      reloc_howto_type *howto;
2927      asection *section;
2928      const char *name;
2929      union etree_union *addend;
2930 {
2931   lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
2932   
2933   p->reloc = reloc;
2934   p->howto = howto;
2935   p->section = section;
2936   p->name = name;
2937   p->addend_exp = addend;
2938
2939   p->addend_value = 0;
2940   p->output_section = NULL;
2941   p->output_vma = 0;
2942 }
2943
2944 void
2945 lang_add_assignment (exp)
2946      etree_type * exp;
2947 {
2948   lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2949                                                   stat_ptr);
2950
2951   new->exp = exp;
2952 }
2953
2954 void
2955 lang_add_attribute (attribute)
2956      enum statement_enum attribute;
2957 {
2958   new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2959 }
2960
2961 void
2962 lang_startup (name)
2963      CONST char *name;
2964 {
2965   if (startup_file != (char *) NULL)
2966     {
2967       einfo ("%P%Fmultiple STARTUP files\n");
2968     }
2969   first_file->filename = name;
2970   first_file->local_sym_name = name;
2971   first_file->real = true;
2972
2973   startup_file = name;
2974 }
2975
2976 void
2977 lang_float (maybe)
2978      boolean maybe;
2979 {
2980   lang_float_flag = maybe;
2981 }
2982
2983 void
2984 lang_leave_output_section_statement (fill, memspec)
2985      bfd_vma fill;
2986      CONST char *memspec;
2987 {
2988   current_section->fill = fill;
2989   current_section->region = lang_memory_region_lookup (memspec);
2990   stat_ptr = &statement_list;
2991 }
2992
2993 /*
2994  Create an absolute symbol with the given name with the value of the
2995  address of first byte of the section named.
2996
2997  If the symbol already exists, then do nothing.
2998 */
2999 void
3000 lang_abs_symbol_at_beginning_of (secname, name)
3001      const char *secname;
3002      const char *name;
3003 {
3004   struct bfd_link_hash_entry *h;
3005
3006   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3007   if (h == (struct bfd_link_hash_entry *) NULL)
3008     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3009
3010   if (h->type == bfd_link_hash_new
3011       || h->type == bfd_link_hash_undefined)
3012     {
3013       asection *sec;
3014
3015       h->type = bfd_link_hash_defined;
3016
3017       sec = bfd_get_section_by_name (output_bfd, secname);
3018       if (sec == (asection *) NULL)
3019         h->u.def.value = 0;
3020       else
3021         h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3022
3023       h->u.def.section = bfd_abs_section_ptr;
3024     }
3025 }
3026
3027 /*
3028  Create an absolute symbol with the given name with the value of the
3029  address of the first byte after the end of the section named.
3030
3031  If the symbol already exists, then do nothing.
3032 */
3033 void
3034 lang_abs_symbol_at_end_of (secname, name)
3035      const char *secname;
3036      const char *name;
3037 {
3038   struct bfd_link_hash_entry *h;
3039
3040   h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3041   if (h == (struct bfd_link_hash_entry *) NULL)
3042     einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3043
3044   if (h->type == bfd_link_hash_new
3045       || h->type == bfd_link_hash_undefined)
3046     {
3047       asection *sec;
3048
3049       h->type = bfd_link_hash_defined;
3050
3051       sec = bfd_get_section_by_name (output_bfd, secname);
3052       if (sec == (asection *) NULL)
3053         h->u.def.value = 0;
3054       else
3055         h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3056                           + bfd_section_size (output_bfd, sec));
3057
3058       h->u.def.section = bfd_abs_section_ptr;
3059     }
3060 }
3061
3062 void
3063 lang_statement_append (list, element, field)
3064      lang_statement_list_type * list;
3065      lang_statement_union_type * element;
3066      lang_statement_union_type ** field;
3067 {
3068   *(list->tail) = element;
3069   list->tail = field;
3070 }
3071
3072 /* Set the output format type.  -oformat overrides scripts.  */
3073
3074 void
3075 lang_add_output_format (format, big, little, from_script)
3076      const char *format;
3077      const char *big;
3078      const char *little;
3079      int from_script;
3080 {
3081   if (output_target == NULL || !from_script)
3082     {
3083       if (command_line.endian == ENDIAN_BIG
3084           && big != NULL)
3085         format = big;
3086       else if (command_line.endian == ENDIAN_LITTLE
3087                && little != NULL)
3088         format = little;
3089
3090       output_target = format;
3091     }
3092 }
3093
3094 /* Enter a group.  This creates a new lang_group_statement, and sets
3095    stat_ptr to build new statements within the group.  */
3096
3097 void
3098 lang_enter_group ()
3099 {
3100   lang_group_statement_type *g;
3101
3102   g = new_stat (lang_group_statement, stat_ptr);
3103   lang_list_init (&g->children);
3104   stat_ptr = &g->children;
3105 }
3106
3107 /* Leave a group.  This just resets stat_ptr to start writing to the
3108    regular list of statements again.  Note that this will not work if
3109    groups can occur inside anything else which can adjust stat_ptr,
3110    but currently they can't.  */
3111
3112 void
3113 lang_leave_group ()
3114 {
3115   stat_ptr = &statement_list;
3116 }
This page took 0.191676 seconds and 4 git commands to generate.