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