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