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