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