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