ldlang.c revision 91041
1/* Linker command language support.
2   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3   2001, 2002
4   Free Software Foundation, Inc.
5
6This file is part of GLD, the Gnu Linker.
7
8GLD is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GLD is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GLD; see the file COPYING.  If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA.  */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libiberty.h"
26#include "safe-ctype.h"
27#include "obstack.h"
28#include "bfdlink.h"
29
30#include "ld.h"
31#include "ldmain.h"
32#include "ldgram.h"
33#include "ldexp.h"
34#include "ldlang.h"
35#include "ldlex.h"
36#include "ldmisc.h"
37#include "ldctor.h"
38#include "ldfile.h"
39#include "ldemul.h"
40#include "fnmatch.h"
41#include "demangle.h"
42
43/* FORWARDS */
44static lang_statement_union_type *new_statement
45  PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
46
47/* LOCALS */
48static struct obstack stat_obstack;
49
50#define obstack_chunk_alloc xmalloc
51#define obstack_chunk_free free
52static const char *startup_file;
53static lang_statement_list_type input_file_chain;
54static boolean placed_commons = false;
55static lang_output_section_statement_type *default_common_section;
56static boolean map_option_f;
57static bfd_vma print_dot;
58static lang_input_statement_type *first_file;
59static const char *current_target;
60static const char *output_target;
61static lang_statement_list_type statement_list;
62static struct lang_phdr *lang_phdr_list;
63
64static void lang_for_each_statement_worker
65  PARAMS ((void (*) (lang_statement_union_type *),
66	   lang_statement_union_type *));
67static lang_input_statement_type *new_afile
68  PARAMS ((const char *, lang_input_file_enum_type, const char *, boolean));
69static lang_memory_region_type *lang_memory_default PARAMS ((asection *));
70static void lang_map_flags PARAMS ((flagword));
71static void init_os PARAMS ((lang_output_section_statement_type *));
72static void exp_init_os PARAMS ((etree_type *));
73static void section_already_linked PARAMS ((bfd *, asection *, PTR));
74static struct bfd_hash_entry *already_linked_newfunc
75  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
76static void already_linked_table_init PARAMS ((void));
77static void already_linked_table_free PARAMS ((void));
78static boolean wildcardp PARAMS ((const char *));
79static lang_statement_union_type *wild_sort
80  PARAMS ((lang_wild_statement_type *, struct wildcard_list *,
81	   lang_input_statement_type *, asection *));
82static void output_section_callback
83  PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
84	   lang_input_statement_type *, PTR));
85static lang_input_statement_type *lookup_name PARAMS ((const char *));
86static boolean load_symbols
87  PARAMS ((lang_input_statement_type *, lang_statement_list_type *));
88static void wild
89  PARAMS ((lang_wild_statement_type *,
90	   const char *, lang_output_section_statement_type *));
91static bfd *open_output PARAMS ((const char *));
92static void ldlang_open_output PARAMS ((lang_statement_union_type *));
93static void open_input_bfds PARAMS ((lang_statement_union_type *, boolean));
94static void lang_reasonable_defaults PARAMS ((void));
95static void lang_place_undefineds PARAMS ((void));
96static void map_input_to_output_sections
97  PARAMS ((lang_statement_union_type *, const char *,
98	   lang_output_section_statement_type *));
99static void print_output_section_statement
100  PARAMS ((lang_output_section_statement_type *));
101static void print_assignment
102  PARAMS ((lang_assignment_statement_type *,
103	   lang_output_section_statement_type *));
104static void print_input_statement PARAMS ((lang_input_statement_type *));
105static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
106static void print_input_section PARAMS ((lang_input_section_type *));
107static void print_fill_statement PARAMS ((lang_fill_statement_type *));
108static void print_data_statement PARAMS ((lang_data_statement_type *));
109static void print_address_statement PARAMS ((lang_address_statement_type *));
110static void print_reloc_statement PARAMS ((lang_reloc_statement_type *));
111static void print_padding_statement PARAMS ((lang_padding_statement_type *));
112static void print_wild_statement
113  PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
114static void print_group
115  PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
116static void print_statement
117  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
118static void print_statement_list
119  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
120static void print_statements PARAMS ((void));
121static void insert_pad
122  PARAMS ((lang_statement_union_type **, fill_type,
123	   unsigned int, asection *, bfd_vma));
124static bfd_vma size_input_section
125  PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
126	   fill_type, bfd_vma));
127static void lang_finish PARAMS ((void));
128static void ignore_bfd_errors PARAMS ((const char *, ...));
129static void lang_check PARAMS ((void));
130static void lang_common PARAMS ((void));
131static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
132static void lang_place_orphans PARAMS ((void));
133static int topower PARAMS ((int));
134static void lang_set_startof PARAMS ((void));
135static void gc_section_callback
136  PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
137	   lang_input_statement_type *, PTR));
138static void lang_record_phdrs PARAMS ((void));
139static void lang_gc_wild PARAMS ((lang_wild_statement_type *));
140static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
141static void lang_gc_sections PARAMS ((void));
142static int lang_vers_match_lang_c
143  PARAMS ((struct bfd_elf_version_expr *, const char *));
144static int lang_vers_match_lang_cplusplus
145  PARAMS ((struct bfd_elf_version_expr *, const char *));
146static int lang_vers_match_lang_java
147  PARAMS ((struct bfd_elf_version_expr *, const char *));
148static void lang_do_version_exports_section PARAMS ((void));
149static void lang_check_section_addresses PARAMS ((void));
150static void os_region_check
151  PARAMS ((lang_output_section_statement_type *,
152	   struct memory_region_struct *, etree_type *, bfd_vma));
153
154typedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
155				    struct wildcard_list *,
156				    asection *,
157				    lang_input_statement_type *,
158				    PTR));
159static void walk_wild
160  PARAMS ((lang_wild_statement_type *, callback_t, PTR));
161static void walk_wild_section
162  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
163	   callback_t, PTR));
164static void walk_wild_file
165  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
166	   callback_t, PTR));
167
168static int    get_target PARAMS ((const bfd_target *, PTR));
169static void   stricpy PARAMS ((char *, char *));
170static void   strcut PARAMS ((char *, char *));
171static int    name_compare PARAMS ((char *, char *));
172static int    closest_target_match PARAMS ((const bfd_target *, PTR));
173static char * get_first_input_target PARAMS ((void));
174
175/* EXPORTS */
176lang_output_section_statement_type *abs_output_section;
177lang_statement_list_type lang_output_section_statement;
178lang_statement_list_type *stat_ptr = &statement_list;
179lang_statement_list_type file_chain = { NULL, NULL };
180const char *entry_symbol = NULL;
181const char *entry_section = ".text";
182boolean entry_from_cmdline;
183boolean lang_has_input_file = false;
184boolean had_output_filename = false;
185boolean lang_float_flag = false;
186boolean delete_output_file_on_failure = false;
187struct lang_nocrossrefs *nocrossref_list;
188struct unique_sections *unique_section_list;
189
190etree_type *base; /* Relocation base - or null */
191
192#if defined (__STDC__) || defined (ALMOST_STDC)
193#define cat(a,b) a##b
194#else
195#define cat(a,b) a/**/b
196#endif
197
198/* Don't beautify the line below with "innocent" whitespace, it breaks
199   the K&R C preprocessor!  */
200#define new_stat(x, y) \
201  (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
202
203#define outside_section_address(q) \
204  ((q)->output_offset + (q)->output_section->vma)
205
206#define outside_symbol_address(q) \
207  ((q)->value + outside_section_address (q->section))
208
209#define SECTION_NAME_MAP_LENGTH (16)
210
211PTR
212stat_alloc (size)
213     size_t size;
214{
215  return obstack_alloc (&stat_obstack, size);
216}
217
218boolean
219unique_section_p (secnam)
220     const char *secnam;
221{
222  struct unique_sections *unam;
223
224  for (unam = unique_section_list; unam; unam = unam->next)
225    if (wildcardp (unam->name)
226	? fnmatch (unam->name, secnam, 0) == 0
227	: strcmp (unam->name, secnam) == 0)
228      {
229	return true;
230      }
231
232  return false;
233}
234
235/* Generic traversal routines for finding matching sections.  */
236
237static void
238walk_wild_section (ptr, file, callback, data)
239     lang_wild_statement_type *ptr;
240     lang_input_statement_type *file;
241     callback_t callback;
242     PTR data;
243{
244  asection *s;
245
246  if (file->just_syms_flag)
247    return;
248
249  for (s = file->the_bfd->sections; s != NULL; s = s->next)
250    {
251      struct wildcard_list *sec;
252
253      sec = ptr->section_list;
254      if (sec == NULL)
255	(*callback) (ptr, sec, s, file, data);
256
257      while (sec != NULL)
258	{
259	  boolean skip = false;
260	  struct name_list *list_tmp;
261
262	  /* Don't process sections from files which were
263	     excluded.  */
264	  for (list_tmp = sec->spec.exclude_name_list;
265	       list_tmp;
266	       list_tmp = list_tmp->next)
267	    {
268	      if (wildcardp (list_tmp->name))
269		skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
270	      else
271		skip = strcmp (list_tmp->name, file->filename) == 0;
272
273	      /* If this file is part of an archive, and the archive is
274		 excluded, exclude this file.  */
275	      if (! skip && file->the_bfd != NULL
276		  && file->the_bfd->my_archive != NULL
277		  && file->the_bfd->my_archive->filename != NULL)
278		{
279		  if (wildcardp (list_tmp->name))
280		    skip = fnmatch (list_tmp->name,
281				    file->the_bfd->my_archive->filename,
282				    0) == 0;
283		  else
284		    skip = strcmp (list_tmp->name,
285				   file->the_bfd->my_archive->filename) == 0;
286		}
287
288	      if (skip)
289		break;
290	    }
291
292	  if (!skip && sec->spec.name != NULL)
293	    {
294	      const char *sname = bfd_get_section_name (file->the_bfd, s);
295
296	      if (wildcardp (sec->spec.name))
297		skip = fnmatch (sec->spec.name, sname, 0) != 0;
298	      else
299		skip = strcmp (sec->spec.name, sname) != 0;
300	    }
301
302	  if (!skip)
303	    (*callback) (ptr, sec, s, file, data);
304
305	  sec = sec->next;
306	}
307    }
308}
309
310/* Handle a wild statement for a single file F.  */
311
312static void
313walk_wild_file (s, f, callback, data)
314     lang_wild_statement_type *s;
315     lang_input_statement_type *f;
316     callback_t callback;
317     PTR data;
318{
319  if (f->the_bfd == NULL
320      || ! bfd_check_format (f->the_bfd, bfd_archive))
321    walk_wild_section (s, f, callback, data);
322  else
323    {
324      bfd *member;
325
326      /* This is an archive file.  We must map each member of the
327	 archive separately.  */
328      member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
329      while (member != NULL)
330	{
331	  /* When lookup_name is called, it will call the add_symbols
332	     entry point for the archive.  For each element of the
333	     archive which is included, BFD will call ldlang_add_file,
334	     which will set the usrdata field of the member to the
335	     lang_input_statement.  */
336	  if (member->usrdata != NULL)
337	    {
338	      walk_wild_section (s,
339				 (lang_input_statement_type *) member->usrdata,
340				 callback, data);
341	    }
342
343	  member = bfd_openr_next_archived_file (f->the_bfd, member);
344	}
345    }
346}
347
348static void
349walk_wild (s, callback, data)
350     lang_wild_statement_type *s;
351     callback_t callback;
352     PTR data;
353{
354  const char *file_spec = s->filename;
355
356  if (file_spec == NULL)
357    {
358      /* Perform the iteration over all files in the list.  */
359      LANG_FOR_EACH_INPUT_STATEMENT (f)
360	{
361	  walk_wild_file (s, f, callback, data);
362	}
363    }
364  else if (wildcardp (file_spec))
365    {
366      LANG_FOR_EACH_INPUT_STATEMENT (f)
367	{
368	  if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
369	    walk_wild_file (s, f, callback, data);
370	}
371    }
372  else
373    {
374      lang_input_statement_type *f;
375
376      /* Perform the iteration over a single file.  */
377      f = lookup_name (file_spec);
378      if (f)
379	walk_wild_file (s, f, callback, data);
380    }
381}
382
383/* lang_for_each_statement walks the parse tree and calls the provided
384   function for each node.  */
385
386static void
387lang_for_each_statement_worker (func, s)
388     void (*func) PARAMS ((lang_statement_union_type *));
389     lang_statement_union_type *s;
390{
391  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
392    {
393      func (s);
394
395      switch (s->header.type)
396	{
397	case lang_constructors_statement_enum:
398	  lang_for_each_statement_worker (func, constructor_list.head);
399	  break;
400	case lang_output_section_statement_enum:
401	  lang_for_each_statement_worker
402	    (func,
403	     s->output_section_statement.children.head);
404	  break;
405	case lang_wild_statement_enum:
406	  lang_for_each_statement_worker
407	    (func,
408	     s->wild_statement.children.head);
409	  break;
410	case lang_group_statement_enum:
411	  lang_for_each_statement_worker (func,
412					  s->group_statement.children.head);
413	  break;
414	case lang_data_statement_enum:
415	case lang_reloc_statement_enum:
416	case lang_object_symbols_statement_enum:
417	case lang_output_statement_enum:
418	case lang_target_statement_enum:
419	case lang_input_section_enum:
420	case lang_input_statement_enum:
421	case lang_assignment_statement_enum:
422	case lang_padding_statement_enum:
423	case lang_address_statement_enum:
424	case lang_fill_statement_enum:
425	  break;
426	default:
427	  FAIL ();
428	  break;
429	}
430    }
431}
432
433void
434lang_for_each_statement (func)
435     void (*func) PARAMS ((lang_statement_union_type *));
436{
437  lang_for_each_statement_worker (func, statement_list.head);
438}
439
440/*----------------------------------------------------------------------*/
441
442void
443lang_list_init (list)
444     lang_statement_list_type *list;
445{
446  list->head = (lang_statement_union_type *) NULL;
447  list->tail = &list->head;
448}
449
450/* Build a new statement node for the parse tree.  */
451
452static lang_statement_union_type *
453new_statement (type, size, list)
454     enum statement_enum type;
455     size_t size;
456     lang_statement_list_type *list;
457{
458  lang_statement_union_type *new = (lang_statement_union_type *)
459  stat_alloc (size);
460
461  new->header.type = type;
462  new->header.next = (lang_statement_union_type *) NULL;
463  lang_statement_append (list, new, &new->header.next);
464  return new;
465}
466
467/* Build a new input file node for the language.  There are several
468   ways in which we treat an input file, eg, we only look at symbols,
469   or prefix it with a -l etc.
470
471   We can be supplied with requests for input files more than once;
472   they may, for example be split over serveral lines like foo.o(.text)
473   foo.o(.data) etc, so when asked for a file we check that we haven't
474   got it already so we don't duplicate the bfd.  */
475
476static lang_input_statement_type *
477new_afile (name, file_type, target, add_to_list)
478     const char *name;
479     lang_input_file_enum_type file_type;
480     const char *target;
481     boolean add_to_list;
482{
483  lang_input_statement_type *p;
484
485  if (add_to_list)
486    p = new_stat (lang_input_statement, stat_ptr);
487  else
488    {
489      p = ((lang_input_statement_type *)
490	   stat_alloc (sizeof (lang_input_statement_type)));
491      p->header.next = NULL;
492    }
493
494  lang_has_input_file = true;
495  p->target = target;
496  switch (file_type)
497    {
498    case lang_input_file_is_symbols_only_enum:
499      p->filename = name;
500      p->is_archive = false;
501      p->real = true;
502      p->local_sym_name = name;
503      p->just_syms_flag = true;
504      p->search_dirs_flag = false;
505      break;
506    case lang_input_file_is_fake_enum:
507      p->filename = name;
508      p->is_archive = false;
509      p->real = false;
510      p->local_sym_name = name;
511      p->just_syms_flag = false;
512      p->search_dirs_flag = false;
513      break;
514    case lang_input_file_is_l_enum:
515      p->is_archive = true;
516      p->filename = name;
517      p->real = true;
518      p->local_sym_name = concat ("-l", name, (const char *) NULL);
519      p->just_syms_flag = false;
520      p->search_dirs_flag = true;
521      break;
522    case lang_input_file_is_marker_enum:
523      p->filename = name;
524      p->is_archive = false;
525      p->real = false;
526      p->local_sym_name = name;
527      p->just_syms_flag = false;
528      p->search_dirs_flag = true;
529      break;
530    case lang_input_file_is_search_file_enum:
531      p->filename = name;
532      p->is_archive = false;
533      p->real = true;
534      p->local_sym_name = name;
535      p->just_syms_flag = false;
536      p->search_dirs_flag = true;
537      break;
538    case lang_input_file_is_file_enum:
539      p->filename = name;
540      p->is_archive = false;
541      p->real = true;
542      p->local_sym_name = name;
543      p->just_syms_flag = false;
544      p->search_dirs_flag = false;
545      break;
546    default:
547      FAIL ();
548    }
549  p->the_bfd = (bfd *) NULL;
550  p->asymbols = (asymbol **) NULL;
551  p->next_real_file = (lang_statement_union_type *) NULL;
552  p->next = (lang_statement_union_type *) NULL;
553  p->symbol_count = 0;
554  p->dynamic = config.dynamic_link;
555  p->whole_archive = whole_archive;
556  p->loaded = false;
557  lang_statement_append (&input_file_chain,
558			 (lang_statement_union_type *) p,
559			 &p->next_real_file);
560  return p;
561}
562
563lang_input_statement_type *
564lang_add_input_file (name, file_type, target)
565     const char *name;
566     lang_input_file_enum_type file_type;
567     const char *target;
568{
569  lang_has_input_file = true;
570  return new_afile (name, file_type, target, true);
571}
572
573/* Build enough state so that the parser can build its tree.  */
574
575void
576lang_init ()
577{
578  obstack_begin (&stat_obstack, 1000);
579
580  stat_ptr = &statement_list;
581
582  lang_list_init (stat_ptr);
583
584  lang_list_init (&input_file_chain);
585  lang_list_init (&lang_output_section_statement);
586  lang_list_init (&file_chain);
587  first_file = lang_add_input_file ((char *) NULL,
588				    lang_input_file_is_marker_enum,
589				    (char *) NULL);
590  abs_output_section =
591    lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
592
593  abs_output_section->bfd_section = bfd_abs_section_ptr;
594
595}
596
597/*----------------------------------------------------------------------
598  A region is an area of memory declared with the
599  MEMORY {  name:org=exp, len=exp ... }
600  syntax.
601
602  We maintain a list of all the regions here.
603
604  If no regions are specified in the script, then the default is used
605  which is created when looked up to be the entire data space.  */
606
607static lang_memory_region_type *lang_memory_region_list;
608static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
609
610lang_memory_region_type *
611lang_memory_region_lookup (name)
612     const char *const name;
613{
614  lang_memory_region_type *p;
615
616  for (p = lang_memory_region_list;
617       p != (lang_memory_region_type *) NULL;
618       p = p->next)
619    {
620      if (strcmp (p->name, name) == 0)
621	{
622	  return p;
623	}
624    }
625
626#if 0
627  /* This code used to always use the first region in the list as the
628     default region.  I changed it to instead use a region
629     encompassing all of memory as the default region.  This permits
630     NOLOAD sections to work reasonably without requiring a region.
631     People should specify what region they mean, if they really want
632     a region.  */
633  if (strcmp (name, "*default*") == 0)
634    {
635      if (lang_memory_region_list != (lang_memory_region_type *) NULL)
636	{
637	  return lang_memory_region_list;
638	}
639    }
640#endif
641
642  {
643    lang_memory_region_type *new =
644    (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
645
646    new->name = xstrdup (name);
647    new->next = (lang_memory_region_type *) NULL;
648
649    *lang_memory_region_list_tail = new;
650    lang_memory_region_list_tail = &new->next;
651    new->origin = 0;
652    new->flags = 0;
653    new->not_flags = 0;
654    new->length = ~(bfd_size_type) 0;
655    new->current = 0;
656    new->had_full_message = false;
657
658    return new;
659  }
660}
661
662static lang_memory_region_type *
663lang_memory_default (section)
664     asection *section;
665{
666  lang_memory_region_type *p;
667
668  flagword sec_flags = section->flags;
669
670  /* Override SEC_DATA to mean a writable section.  */
671  if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
672    sec_flags |= SEC_DATA;
673
674  for (p = lang_memory_region_list;
675       p != (lang_memory_region_type *) NULL;
676       p = p->next)
677    {
678      if ((p->flags & sec_flags) != 0
679	  && (p->not_flags & sec_flags) == 0)
680	{
681	  return p;
682	}
683    }
684  return lang_memory_region_lookup ("*default*");
685}
686
687lang_output_section_statement_type *
688lang_output_section_find (name)
689     const char *const name;
690{
691  lang_statement_union_type *u;
692  lang_output_section_statement_type *lookup;
693
694  for (u = lang_output_section_statement.head;
695       u != (lang_statement_union_type *) NULL;
696       u = lookup->next)
697    {
698      lookup = &u->output_section_statement;
699      if (strcmp (name, lookup->name) == 0)
700	{
701	  return lookup;
702	}
703    }
704  return (lang_output_section_statement_type *) NULL;
705}
706
707lang_output_section_statement_type *
708lang_output_section_statement_lookup (name)
709     const char *const name;
710{
711  lang_output_section_statement_type *lookup;
712
713  lookup = lang_output_section_find (name);
714  if (lookup == (lang_output_section_statement_type *) NULL)
715    {
716
717      lookup = (lang_output_section_statement_type *)
718	new_stat (lang_output_section_statement, stat_ptr);
719      lookup->region = (lang_memory_region_type *) NULL;
720      lookup->lma_region = (lang_memory_region_type *) NULL;
721      lookup->fill = 0;
722      lookup->block_value = 1;
723      lookup->name = name;
724
725      lookup->next = (lang_statement_union_type *) NULL;
726      lookup->bfd_section = (asection *) NULL;
727      lookup->processed = false;
728      lookup->sectype = normal_section;
729      lookup->addr_tree = (etree_type *) NULL;
730      lang_list_init (&lookup->children);
731
732      lookup->memspec = (const char *) NULL;
733      lookup->flags = 0;
734      lookup->subsection_alignment = -1;
735      lookup->section_alignment = -1;
736      lookup->load_base = (union etree_union *) NULL;
737      lookup->phdrs = NULL;
738
739      lang_statement_append (&lang_output_section_statement,
740			     (lang_statement_union_type *) lookup,
741			     &lookup->next);
742    }
743  return lookup;
744}
745
746static void
747lang_map_flags (flag)
748     flagword flag;
749{
750  if (flag & SEC_ALLOC)
751    minfo ("a");
752
753  if (flag & SEC_CODE)
754    minfo ("x");
755
756  if (flag & SEC_READONLY)
757    minfo ("r");
758
759  if (flag & SEC_DATA)
760    minfo ("w");
761
762  if (flag & SEC_LOAD)
763    minfo ("l");
764}
765
766void
767lang_map ()
768{
769  lang_memory_region_type *m;
770
771  minfo (_("\nMemory Configuration\n\n"));
772  fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
773	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
774
775  for (m = lang_memory_region_list;
776       m != (lang_memory_region_type *) NULL;
777       m = m->next)
778    {
779      char buf[100];
780      int len;
781
782      fprintf (config.map_file, "%-16s ", m->name);
783
784      sprintf_vma (buf, m->origin);
785      minfo ("0x%s ", buf);
786      len = strlen (buf);
787      while (len < 16)
788	{
789	  print_space ();
790	  ++len;
791	}
792
793      minfo ("0x%V", m->length);
794      if (m->flags || m->not_flags)
795	{
796#ifndef BFD64
797	  minfo ("        ");
798#endif
799	  if (m->flags)
800	    {
801	      print_space ();
802	      lang_map_flags (m->flags);
803	    }
804
805	  if (m->not_flags)
806	    {
807	      minfo (" !");
808	      lang_map_flags (m->not_flags);
809	    }
810	}
811
812      print_nl ();
813    }
814
815  fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
816
817  print_statements ();
818}
819
820/* Initialize an output section.  */
821
822static void
823init_os (s)
824     lang_output_section_statement_type *s;
825{
826  section_userdata_type *new;
827
828  if (s->bfd_section != NULL)
829    return;
830
831  if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
832    einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
833
834  new = ((section_userdata_type *)
835	 stat_alloc (sizeof (section_userdata_type)));
836
837  s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
838  if (s->bfd_section == (asection *) NULL)
839    s->bfd_section = bfd_make_section (output_bfd, s->name);
840  if (s->bfd_section == (asection *) NULL)
841    {
842      einfo (_("%P%F: output format %s cannot represent section called %s\n"),
843	     output_bfd->xvec->name, s->name);
844    }
845  s->bfd_section->output_section = s->bfd_section;
846
847  /* We initialize an output sections output offset to minus its own
848     vma to allow us to output a section through itself.  */
849  s->bfd_section->output_offset = 0;
850  get_userdata (s->bfd_section) = (PTR) new;
851
852  /* If there is a base address, make sure that any sections it might
853     mention are initialized.  */
854  if (s->addr_tree != NULL)
855    exp_init_os (s->addr_tree);
856}
857
858/* Make sure that all output sections mentioned in an expression are
859   initialized.  */
860
861static void
862exp_init_os (exp)
863     etree_type *exp;
864{
865  switch (exp->type.node_class)
866    {
867    case etree_assign:
868      exp_init_os (exp->assign.src);
869      break;
870
871    case etree_binary:
872      exp_init_os (exp->binary.lhs);
873      exp_init_os (exp->binary.rhs);
874      break;
875
876    case etree_trinary:
877      exp_init_os (exp->trinary.cond);
878      exp_init_os (exp->trinary.lhs);
879      exp_init_os (exp->trinary.rhs);
880      break;
881
882    case etree_unary:
883      exp_init_os (exp->unary.child);
884      break;
885
886    case etree_name:
887      switch (exp->type.node_code)
888	{
889	case ADDR:
890	case LOADADDR:
891	case SIZEOF:
892	  {
893	    lang_output_section_statement_type *os;
894
895	    os = lang_output_section_find (exp->name.name);
896	    if (os != NULL && os->bfd_section == NULL)
897	      init_os (os);
898	  }
899	}
900      break;
901
902    default:
903      break;
904    }
905}
906
907/* Sections marked with the SEC_LINK_ONCE flag should only be linked
908   once into the output.  This routine checks each section, and
909   arrange to discard it if a section of the same name has already
910   been linked.  If the section has COMDAT information, then it uses
911   that to decide whether the section should be included.  This code
912   assumes that all relevant sections have the SEC_LINK_ONCE flag set;
913   that is, it does not depend solely upon the section name.
914   section_already_linked is called via bfd_map_over_sections.  */
915
916/* This is the shape of the elements inside the already_linked hash
917   table. It maps a name onto a list of already_linked elements with
918   the same name.  It's possible to get more than one element in a
919   list if the COMDAT sections have different names.  */
920
921struct already_linked_hash_entry
922{
923  struct bfd_hash_entry root;
924  struct already_linked *entry;
925};
926
927struct already_linked
928{
929  struct already_linked *next;
930  asection *sec;
931};
932
933/* The hash table.  */
934
935static struct bfd_hash_table already_linked_table;
936
937static void
938section_already_linked (abfd, sec, data)
939     bfd *abfd;
940     asection *sec;
941     PTR data;
942{
943  lang_input_statement_type *entry = (lang_input_statement_type *) data;
944  flagword flags;
945  const char *name;
946  struct already_linked *l;
947  struct already_linked_hash_entry *already_linked_list;
948
949  /* If we are only reading symbols from this object, then we want to
950     discard all sections.  */
951  if (entry->just_syms_flag)
952    {
953      sec->output_section = bfd_abs_section_ptr;
954      sec->output_offset = sec->vma;
955      return;
956    }
957
958  flags = bfd_get_section_flags (abfd, sec);
959
960  if ((flags & SEC_LINK_ONCE) == 0)
961    return;
962
963  /* FIXME: When doing a relocatable link, we may have trouble
964     copying relocations in other sections that refer to local symbols
965     in the section being discarded.  Those relocations will have to
966     be converted somehow; as of this writing I'm not sure that any of
967     the backends handle that correctly.
968
969     It is tempting to instead not discard link once sections when
970     doing a relocatable link (technically, they should be discarded
971     whenever we are building constructors).  However, that fails,
972     because the linker winds up combining all the link once sections
973     into a single large link once section, which defeats the purpose
974     of having link once sections in the first place.
975
976     Also, not merging link once sections in a relocatable link
977     causes trouble for MIPS ELF, which relies in link once semantics
978     to handle the .reginfo section correctly.  */
979
980  name = bfd_get_section_name (abfd, sec);
981
982  already_linked_list =
983    ((struct already_linked_hash_entry *)
984     bfd_hash_lookup (&already_linked_table, name, true, false));
985
986  for (l = already_linked_list->entry; l != NULL; l = l->next)
987    {
988      if (sec->comdat == NULL
989	  || l->sec->comdat == NULL
990	  || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
991	{
992	  /* The section has already been linked.  See if we should
993             issue a warning.  */
994	  switch (flags & SEC_LINK_DUPLICATES)
995	    {
996	    default:
997	      abort ();
998
999	    case SEC_LINK_DUPLICATES_DISCARD:
1000	      break;
1001
1002	    case SEC_LINK_DUPLICATES_ONE_ONLY:
1003	      if (sec->comdat == NULL)
1004		einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
1005		       abfd, name);
1006	      else
1007		einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
1008		       abfd, name, sec->comdat->name);
1009	      break;
1010
1011	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
1012	      /* FIXME: We should really dig out the contents of both
1013                 sections and memcmp them.  The COFF/PE spec says that
1014                 the Microsoft linker does not implement this
1015                 correctly, so I'm not going to bother doing it
1016                 either.  */
1017	      /* Fall through.  */
1018	    case SEC_LINK_DUPLICATES_SAME_SIZE:
1019	      if (bfd_section_size (abfd, sec)
1020		  != bfd_section_size (l->sec->owner, l->sec))
1021		einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
1022		       abfd, name);
1023	      break;
1024	    }
1025
1026	  /* Set the output_section field so that lang_add_section
1027	     does not create a lang_input_section structure for this
1028	     section.  */
1029	  sec->output_section = bfd_abs_section_ptr;
1030
1031	  return;
1032	}
1033    }
1034
1035  /* This is the first section with this name.  Record it.  Allocate
1036     the memory from the same obstack as the hash table is kept in.  */
1037
1038  l = ((struct already_linked *)
1039       bfd_hash_allocate (&already_linked_table, sizeof *l));
1040
1041  l->sec = sec;
1042  l->next = already_linked_list->entry;
1043  already_linked_list->entry = l;
1044}
1045
1046/* Support routines for the hash table used by section_already_linked,
1047   initialize the table, fill in an entry and remove the table.  */
1048
1049static struct bfd_hash_entry *
1050already_linked_newfunc (entry, table, string)
1051     struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
1052     struct bfd_hash_table *table;
1053     const char *string ATTRIBUTE_UNUSED;
1054{
1055  struct already_linked_hash_entry *ret =
1056    bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1057
1058  ret->entry = NULL;
1059
1060  return (struct bfd_hash_entry *) ret;
1061}
1062
1063static void
1064already_linked_table_init ()
1065{
1066  if (! bfd_hash_table_init_n (&already_linked_table,
1067			       already_linked_newfunc,
1068			       42))
1069    einfo (_("%P%F: Failed to create hash table\n"));
1070}
1071
1072static void
1073already_linked_table_free ()
1074{
1075  bfd_hash_table_free (&already_linked_table);
1076}
1077
1078/* The wild routines.
1079
1080   These expand statements like *(.text) and foo.o to a list of
1081   explicit actions, like foo.o(.text), bar.o(.text) and
1082   foo.o(.text, .data).  */
1083
1084/* Return true if the PATTERN argument is a wildcard pattern.
1085   Although backslashes are treated specially if a pattern contains
1086   wildcards, we do not consider the mere presence of a backslash to
1087   be enough to cause the pattern to be treated as a wildcard.
1088   That lets us handle DOS filenames more naturally.  */
1089
1090static boolean
1091wildcardp (pattern)
1092     const char *pattern;
1093{
1094  const char *s;
1095
1096  for (s = pattern; *s != '\0'; ++s)
1097    if (*s == '?'
1098	|| *s == '*'
1099	|| *s == '[')
1100      return true;
1101  return false;
1102}
1103
1104/* Add SECTION to the output section OUTPUT.  Do this by creating a
1105   lang_input_section statement which is placed at PTR.  FILE is the
1106   input file which holds SECTION.  */
1107
1108void
1109lang_add_section (ptr, section, output, file)
1110     lang_statement_list_type *ptr;
1111     asection *section;
1112     lang_output_section_statement_type *output;
1113     lang_input_statement_type *file;
1114{
1115  flagword flags;
1116  boolean discard;
1117
1118  flags = bfd_get_section_flags (section->owner, section);
1119
1120  discard = false;
1121
1122  /* If we are doing a final link, discard sections marked with
1123     SEC_EXCLUDE.  */
1124  if (! link_info.relocateable
1125      && (flags & SEC_EXCLUDE) != 0)
1126    discard = true;
1127
1128  /* Discard input sections which are assigned to a section named
1129     DISCARD_SECTION_NAME.  */
1130  if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1131    discard = true;
1132
1133  /* Discard debugging sections if we are stripping debugging
1134     information.  */
1135  if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1136      && (flags & SEC_DEBUGGING) != 0)
1137    discard = true;
1138
1139  if (discard)
1140    {
1141      if (section->output_section == NULL)
1142	{
1143	  /* This prevents future calls from assigning this section.  */
1144	  section->output_section = bfd_abs_section_ptr;
1145	}
1146      return;
1147    }
1148
1149  if (section->output_section == NULL)
1150    {
1151      boolean first;
1152      lang_input_section_type *new;
1153      flagword flags;
1154
1155      if (output->bfd_section == NULL)
1156	init_os (output);
1157
1158      first = ! output->bfd_section->linker_has_input;
1159      output->bfd_section->linker_has_input = 1;
1160
1161      /* Add a section reference to the list.  */
1162      new = new_stat (lang_input_section, ptr);
1163
1164      new->section = section;
1165      new->ifile = file;
1166      section->output_section = output->bfd_section;
1167
1168      flags = section->flags;
1169
1170      /* We don't copy the SEC_NEVER_LOAD flag from an input section
1171	 to an output section, because we want to be able to include a
1172	 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1173	 section (I don't know why we want to do this, but we do).
1174	 build_link_order in ldwrite.c handles this case by turning
1175	 the embedded SEC_NEVER_LOAD section into a fill.  */
1176
1177      flags &= ~ SEC_NEVER_LOAD;
1178
1179      /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1180	 already been processed.  One reason to do this is that on pe
1181	 format targets, .text$foo sections go into .text and it's odd
1182	 to see .text with SEC_LINK_ONCE set.  */
1183
1184      if (! link_info.relocateable)
1185	flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1186
1187      /* If this is not the first input section, and the SEC_READONLY
1188         flag is not currently set, then don't set it just because the
1189         input section has it set.  */
1190
1191      if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1192	flags &= ~ SEC_READONLY;
1193
1194      /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
1195      if (! first
1196	  && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1197	      != (flags & (SEC_MERGE | SEC_STRINGS))
1198	      || ((flags & SEC_MERGE)
1199		  && section->output_section->entsize != section->entsize)))
1200	{
1201	  section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1202	  flags &= ~ (SEC_MERGE | SEC_STRINGS);
1203	}
1204
1205      section->output_section->flags |= flags;
1206
1207      if (flags & SEC_MERGE)
1208	section->output_section->entsize = section->entsize;
1209
1210      /* If SEC_READONLY is not set in the input section, then clear
1211         it from the output section.  */
1212      if ((section->flags & SEC_READONLY) == 0)
1213	section->output_section->flags &= ~SEC_READONLY;
1214
1215      switch (output->sectype)
1216	{
1217	case normal_section:
1218	  break;
1219	case dsect_section:
1220	case copy_section:
1221	case info_section:
1222	case overlay_section:
1223	  output->bfd_section->flags &= ~SEC_ALLOC;
1224	  break;
1225	case noload_section:
1226	  output->bfd_section->flags &= ~SEC_LOAD;
1227	  output->bfd_section->flags |= SEC_NEVER_LOAD;
1228	  break;
1229	}
1230
1231      /* Copy over SEC_SMALL_DATA.  */
1232      if (section->flags & SEC_SMALL_DATA)
1233	section->output_section->flags |= SEC_SMALL_DATA;
1234
1235      if (section->alignment_power > output->bfd_section->alignment_power)
1236	output->bfd_section->alignment_power = section->alignment_power;
1237
1238      /* If supplied an aligment, then force it.  */
1239      if (output->section_alignment != -1)
1240	output->bfd_section->alignment_power = output->section_alignment;
1241
1242      if (section->flags & SEC_BLOCK)
1243	{
1244	  section->output_section->flags |= SEC_BLOCK;
1245	  /* FIXME: This value should really be obtained from the bfd...  */
1246	  output->block_value = 128;
1247	}
1248    }
1249}
1250
1251/* Handle wildcard sorting.  This returns the lang_input_section which
1252   should follow the one we are going to create for SECTION and FILE,
1253   based on the sorting requirements of WILD.  It returns NULL if the
1254   new section should just go at the end of the current list.  */
1255
1256static lang_statement_union_type *
1257wild_sort (wild, sec, file, section)
1258     lang_wild_statement_type *wild;
1259     struct wildcard_list *sec;
1260     lang_input_statement_type *file;
1261     asection *section;
1262{
1263  const char *section_name;
1264  lang_statement_union_type *l;
1265
1266  if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1267    return NULL;
1268
1269  section_name = bfd_get_section_name (file->the_bfd, section);
1270  for (l = wild->children.head; l != NULL; l = l->header.next)
1271    {
1272      lang_input_section_type *ls;
1273
1274      if (l->header.type != lang_input_section_enum)
1275	continue;
1276      ls = &l->input_section;
1277
1278      /* Sorting by filename takes precedence over sorting by section
1279         name.  */
1280
1281      if (wild->filenames_sorted)
1282	{
1283	  const char *fn, *ln;
1284	  boolean fa, la;
1285	  int i;
1286
1287	  /* The PE support for the .idata section as generated by
1288             dlltool assumes that files will be sorted by the name of
1289             the archive and then the name of the file within the
1290             archive.  */
1291
1292	  if (file->the_bfd != NULL
1293	      && bfd_my_archive (file->the_bfd) != NULL)
1294	    {
1295	      fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1296	      fa = true;
1297	    }
1298	  else
1299	    {
1300	      fn = file->filename;
1301	      fa = false;
1302	    }
1303
1304	  if (ls->ifile->the_bfd != NULL
1305	      && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1306	    {
1307	      ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1308	      la = true;
1309	    }
1310	  else
1311	    {
1312	      ln = ls->ifile->filename;
1313	      la = false;
1314	    }
1315
1316	  i = strcmp (fn, ln);
1317	  if (i > 0)
1318	    continue;
1319	  else if (i < 0)
1320	    break;
1321
1322	  if (fa || la)
1323	    {
1324	      if (fa)
1325		fn = file->filename;
1326	      if (la)
1327		ln = ls->ifile->filename;
1328
1329	      i = strcmp (fn, ln);
1330	      if (i > 0)
1331		continue;
1332	      else if (i < 0)
1333		break;
1334	    }
1335	}
1336
1337      /* Here either the files are not sorted by name, or we are
1338         looking at the sections for this file.  */
1339
1340      if (sec != NULL && sec->spec.sorted)
1341	{
1342	  if (strcmp (section_name,
1343		      bfd_get_section_name (ls->ifile->the_bfd,
1344					    ls->section))
1345	      < 0)
1346	    break;
1347	}
1348    }
1349
1350  return l;
1351}
1352
1353/* Expand a wild statement for a particular FILE.  SECTION may be
1354   NULL, in which case it is a wild card.  */
1355
1356static void
1357output_section_callback (ptr, sec, section, file, output)
1358     lang_wild_statement_type *ptr;
1359     struct wildcard_list *sec;
1360     asection *section;
1361     lang_input_statement_type *file;
1362     PTR output;
1363{
1364  lang_statement_union_type *before;
1365
1366  /* Exclude sections that match UNIQUE_SECTION_LIST.  */
1367  if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1368    return;
1369
1370  /* If the wild pattern was marked KEEP, the member sections
1371     should be as well.  */
1372  if (ptr->keep_sections)
1373    section->flags |= SEC_KEEP;
1374
1375  before = wild_sort (ptr, sec, file, section);
1376
1377  /* Here BEFORE points to the lang_input_section which
1378     should follow the one we are about to add.  If BEFORE
1379     is NULL, then the section should just go at the end
1380     of the current list.  */
1381
1382  if (before == NULL)
1383    lang_add_section (&ptr->children, section,
1384		      (lang_output_section_statement_type *) output,
1385		      file);
1386  else
1387    {
1388      lang_statement_list_type list;
1389      lang_statement_union_type **pp;
1390
1391      lang_list_init (&list);
1392      lang_add_section (&list, section,
1393			(lang_output_section_statement_type *) output,
1394			file);
1395
1396      /* If we are discarding the section, LIST.HEAD will
1397	 be NULL.  */
1398      if (list.head != NULL)
1399	{
1400	  ASSERT (list.head->header.next == NULL);
1401
1402	  for (pp = &ptr->children.head;
1403	       *pp != before;
1404	       pp = &(*pp)->header.next)
1405	    ASSERT (*pp != NULL);
1406
1407	  list.head->header.next = *pp;
1408	  *pp = list.head;
1409	}
1410    }
1411}
1412
1413/* This is passed a file name which must have been seen already and
1414   added to the statement tree.  We will see if it has been opened
1415   already and had its symbols read.  If not then we'll read it.  */
1416
1417static lang_input_statement_type *
1418lookup_name (name)
1419     const char *name;
1420{
1421  lang_input_statement_type *search;
1422
1423  for (search = (lang_input_statement_type *) input_file_chain.head;
1424       search != (lang_input_statement_type *) NULL;
1425       search = (lang_input_statement_type *) search->next_real_file)
1426    {
1427      if (search->filename == (char *) NULL && name == (char *) NULL)
1428	return search;
1429      if (search->filename != (char *) NULL
1430	  && name != (char *) NULL
1431	  && strcmp (search->filename, name) == 0)
1432	break;
1433    }
1434
1435  if (search == (lang_input_statement_type *) NULL)
1436    search = new_afile (name, lang_input_file_is_file_enum, default_target,
1437			false);
1438
1439  /* If we have already added this file, or this file is not real
1440     (FIXME: can that ever actually happen?) or the name is NULL
1441     (FIXME: can that ever actually happen?) don't add this file.  */
1442  if (search->loaded
1443      || ! search->real
1444      || search->filename == (const char *) NULL)
1445    return search;
1446
1447  if (! load_symbols (search, (lang_statement_list_type *) NULL))
1448    return NULL;
1449
1450  return search;
1451}
1452
1453/* Get the symbols for an input file.  */
1454
1455static boolean
1456load_symbols (entry, place)
1457     lang_input_statement_type *entry;
1458     lang_statement_list_type *place;
1459{
1460  char **matching;
1461
1462  if (entry->loaded)
1463    return true;
1464
1465  ldfile_open_file (entry);
1466
1467  if (! bfd_check_format (entry->the_bfd, bfd_archive)
1468      && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1469    {
1470      bfd_error_type err;
1471      lang_statement_list_type *hold;
1472      boolean bad_load = true;
1473
1474      err = bfd_get_error ();
1475
1476      /* See if the emulation has some special knowledge.  */
1477      if (ldemul_unrecognized_file (entry))
1478	return true;
1479
1480      if (err == bfd_error_file_ambiguously_recognized)
1481	{
1482	  char **p;
1483
1484	  einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1485	  einfo (_("%B: matching formats:"), entry->the_bfd);
1486	  for (p = matching; *p != NULL; p++)
1487	    einfo (" %s", *p);
1488	  einfo ("%F\n");
1489	}
1490      else if (err != bfd_error_file_not_recognized
1491	       || place == NULL)
1492	  einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1493      else
1494	bad_load = false;
1495
1496      bfd_close (entry->the_bfd);
1497      entry->the_bfd = NULL;
1498
1499      /* Try to interpret the file as a linker script.  */
1500      ldfile_open_command_file (entry->filename);
1501
1502      hold = stat_ptr;
1503      stat_ptr = place;
1504
1505      ldfile_assumed_script = true;
1506      parser_input = input_script;
1507      yyparse ();
1508      ldfile_assumed_script = false;
1509
1510      stat_ptr = hold;
1511
1512      return ! bad_load;
1513    }
1514
1515  if (ldemul_recognized_file (entry))
1516    return true;
1517
1518  /* We don't call ldlang_add_file for an archive.  Instead, the
1519     add_symbols entry point will call ldlang_add_file, via the
1520     add_archive_element callback, for each element of the archive
1521     which is used.  */
1522  switch (bfd_get_format (entry->the_bfd))
1523    {
1524    default:
1525      break;
1526
1527    case bfd_object:
1528      ldlang_add_file (entry);
1529      if (trace_files || trace_file_tries)
1530	info_msg ("%I\n", entry);
1531      break;
1532
1533    case bfd_archive:
1534      if (entry->whole_archive)
1535	{
1536	  bfd * member = NULL;
1537	  boolean loaded = true;
1538
1539	  for (;;)
1540	    {
1541	      member = bfd_openr_next_archived_file (entry->the_bfd, member);
1542
1543	      if (member == NULL)
1544		break;
1545
1546	      if (! bfd_check_format (member, bfd_object))
1547		{
1548		  einfo (_("%F%B: member %B in archive is not an object\n"),
1549			 entry->the_bfd, member);
1550		  loaded = false;
1551		}
1552
1553	      if (! ((*link_info.callbacks->add_archive_element)
1554		     (&link_info, member, "--whole-archive")))
1555		abort ();
1556
1557	      if (! bfd_link_add_symbols (member, &link_info))
1558		{
1559		  einfo (_("%F%B: could not read symbols: %E\n"), member);
1560		  loaded = false;
1561		}
1562	    }
1563
1564	  entry->loaded = loaded;
1565	  return loaded;
1566	}
1567      break;
1568    }
1569
1570  if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1571    entry->loaded = true;
1572  else
1573    einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1574
1575  return entry->loaded;
1576}
1577
1578/* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
1579   may be NULL, indicating that it is a wildcard.  Separate
1580   lang_input_section statements are created for each part of the
1581   expansion; they are added after the wild statement S.  OUTPUT is
1582   the output section.  */
1583
1584static void
1585wild (s, target, output)
1586     lang_wild_statement_type *s;
1587     const char *target ATTRIBUTE_UNUSED;
1588     lang_output_section_statement_type *output;
1589{
1590  struct wildcard_list *sec;
1591
1592  walk_wild (s, output_section_callback, (PTR) output);
1593
1594  for (sec = s->section_list; sec != NULL; sec = sec->next)
1595    {
1596      if (default_common_section != NULL)
1597	break;
1598      if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1599	{
1600	  /* Remember the section that common is going to in case we
1601	     later get something which doesn't know where to put it.  */
1602	  default_common_section = output;
1603	}
1604    }
1605}
1606
1607/* Return true iff target is the sought target.  */
1608
1609static int
1610get_target (target, data)
1611     const bfd_target *target;
1612     PTR data;
1613{
1614  const char *sought = (const char *) data;
1615
1616  return strcmp (target->name, sought) == 0;
1617}
1618
1619/* Like strcpy() but convert to lower case as well.  */
1620
1621static void
1622stricpy (dest, src)
1623     char *dest;
1624     char *src;
1625{
1626  char c;
1627
1628  while ((c = *src++) != 0)
1629    *dest++ = TOLOWER (c);
1630
1631  *dest = 0;
1632}
1633
1634/* Remove the first occurance of needle (if any) in haystack
1635   from haystack.  */
1636
1637static void
1638strcut (haystack, needle)
1639     char *haystack;
1640     char *needle;
1641{
1642  haystack = strstr (haystack, needle);
1643
1644  if (haystack)
1645    {
1646      char *src;
1647
1648      for (src = haystack + strlen (needle); *src;)
1649	*haystack++ = *src++;
1650
1651      *haystack = 0;
1652    }
1653}
1654
1655/* Compare two target format name strings.
1656   Return a value indicating how "similar" they are.  */
1657
1658static int
1659name_compare (first, second)
1660     char *first;
1661     char *second;
1662{
1663  char *copy1;
1664  char *copy2;
1665  int result;
1666
1667  copy1 = xmalloc (strlen (first) + 1);
1668  copy2 = xmalloc (strlen (second) + 1);
1669
1670  /* Convert the names to lower case.  */
1671  stricpy (copy1, first);
1672  stricpy (copy2, second);
1673
1674  /* Remove and endian strings from the name.  */
1675  strcut (copy1, "big");
1676  strcut (copy1, "little");
1677  strcut (copy2, "big");
1678  strcut (copy2, "little");
1679
1680  /* Return a value based on how many characters match,
1681     starting from the beginning.   If both strings are
1682     the same then return 10 * their length.  */
1683  for (result = 0; copy1[result] == copy2[result]; result++)
1684    if (copy1[result] == 0)
1685      {
1686	result *= 10;
1687	break;
1688      }
1689
1690  free (copy1);
1691  free (copy2);
1692
1693  return result;
1694}
1695
1696/* Set by closest_target_match() below.  */
1697static const bfd_target *winner;
1698
1699/* Scan all the valid bfd targets looking for one that has the endianness
1700   requirement that was specified on the command line, and is the nearest
1701   match to the original output target.  */
1702
1703static int
1704closest_target_match (target, data)
1705     const bfd_target *target;
1706     PTR data;
1707{
1708  const bfd_target *original = (const bfd_target *) data;
1709
1710  if (command_line.endian == ENDIAN_BIG
1711      && target->byteorder != BFD_ENDIAN_BIG)
1712    return 0;
1713
1714  if (command_line.endian == ENDIAN_LITTLE
1715      && target->byteorder != BFD_ENDIAN_LITTLE)
1716    return 0;
1717
1718  /* Must be the same flavour.  */
1719  if (target->flavour != original->flavour)
1720    return 0;
1721
1722  /* If we have not found a potential winner yet, then record this one.  */
1723  if (winner == NULL)
1724    {
1725      winner = target;
1726      return 0;
1727    }
1728
1729  /* Oh dear, we now have two potential candidates for a successful match.
1730     Compare their names and choose the better one.  */
1731  if (name_compare (target->name, original->name)
1732      > name_compare (winner->name, original->name))
1733    winner = target;
1734
1735  /* Keep on searching until wqe have checked them all.  */
1736  return 0;
1737}
1738
1739/* Return the BFD target format of the first input file.  */
1740
1741static char *
1742get_first_input_target ()
1743{
1744  char *target = NULL;
1745
1746  LANG_FOR_EACH_INPUT_STATEMENT (s)
1747    {
1748      if (s->header.type == lang_input_statement_enum
1749	  && s->real)
1750	{
1751	  ldfile_open_file (s);
1752
1753	  if (s->the_bfd != NULL
1754	      && bfd_check_format (s->the_bfd, bfd_object))
1755	    {
1756	      target = bfd_get_target (s->the_bfd);
1757
1758	      if (target != NULL)
1759		break;
1760	    }
1761	}
1762    }
1763
1764  return target;
1765}
1766
1767/* Open the output file.  */
1768
1769static bfd *
1770open_output (name)
1771     const char *name;
1772{
1773  bfd *output;
1774
1775  /* Has the user told us which output format to use?  */
1776  if (output_target == (char *) NULL)
1777    {
1778      /* No - has the current target been set to something other than
1779         the default?  */
1780      if (current_target != default_target)
1781	output_target = current_target;
1782
1783      /* No - can we determine the format of the first input file?  */
1784      else
1785	{
1786	  output_target = get_first_input_target ();
1787
1788	  /* Failed - use the default output target.  */
1789	  if (output_target == NULL)
1790	    output_target = default_target;
1791	}
1792    }
1793
1794  /* Has the user requested a particular endianness on the command
1795     line?  */
1796  if (command_line.endian != ENDIAN_UNSET)
1797    {
1798      const bfd_target *target;
1799      enum bfd_endian desired_endian;
1800
1801      /* Get the chosen target.  */
1802      target = bfd_search_for_target (get_target, (PTR) output_target);
1803
1804      /* If the target is not supported, we cannot do anything.  */
1805      if (target != NULL)
1806	{
1807	  if (command_line.endian == ENDIAN_BIG)
1808	    desired_endian = BFD_ENDIAN_BIG;
1809	  else
1810	    desired_endian = BFD_ENDIAN_LITTLE;
1811
1812	  /* See if the target has the wrong endianness.  This should
1813	     not happen if the linker script has provided big and
1814	     little endian alternatives, but some scrips don't do
1815	     this.  */
1816	  if (target->byteorder != desired_endian)
1817	    {
1818	      /* If it does, then see if the target provides
1819		 an alternative with the correct endianness.  */
1820	      if (target->alternative_target != NULL
1821		  && (target->alternative_target->byteorder == desired_endian))
1822		output_target = target->alternative_target->name;
1823	      else
1824		{
1825		  /* Try to find a target as similar as possible to
1826		     the default target, but which has the desired
1827		     endian characteristic.  */
1828		  (void) bfd_search_for_target (closest_target_match,
1829						(PTR) target);
1830
1831		  /* Oh dear - we could not find any targets that
1832		     satisfy our requirements.  */
1833		  if (winner == NULL)
1834		    einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1835		  else
1836		    output_target = winner->name;
1837		}
1838	    }
1839	}
1840    }
1841
1842  output = bfd_openw (name, output_target);
1843
1844  if (output == (bfd *) NULL)
1845    {
1846      if (bfd_get_error () == bfd_error_invalid_target)
1847	einfo (_("%P%F: target %s not found\n"), output_target);
1848
1849      einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1850    }
1851
1852  delete_output_file_on_failure = true;
1853
1854#if 0
1855  output->flags |= D_PAGED;
1856#endif
1857
1858  if (! bfd_set_format (output, bfd_object))
1859    einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1860  if (! bfd_set_arch_mach (output,
1861			   ldfile_output_architecture,
1862			   ldfile_output_machine))
1863    einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1864
1865  link_info.hash = bfd_link_hash_table_create (output);
1866  if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1867    einfo (_("%P%F: can not create link hash table: %E\n"));
1868
1869  bfd_set_gp_size (output, g_switch_value);
1870  return output;
1871}
1872
1873static void
1874ldlang_open_output (statement)
1875     lang_statement_union_type *statement;
1876{
1877  switch (statement->header.type)
1878    {
1879    case lang_output_statement_enum:
1880      ASSERT (output_bfd == (bfd *) NULL);
1881      output_bfd = open_output (statement->output_statement.name);
1882      ldemul_set_output_arch ();
1883      if (config.magic_demand_paged && !link_info.relocateable)
1884	output_bfd->flags |= D_PAGED;
1885      else
1886	output_bfd->flags &= ~D_PAGED;
1887      if (config.text_read_only)
1888	output_bfd->flags |= WP_TEXT;
1889      else
1890	output_bfd->flags &= ~WP_TEXT;
1891      if (link_info.traditional_format)
1892	output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1893      else
1894	output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1895      break;
1896
1897    case lang_target_statement_enum:
1898      current_target = statement->target_statement.target;
1899      break;
1900    default:
1901      break;
1902    }
1903}
1904
1905/* Open all the input files.  */
1906
1907static void
1908open_input_bfds (s, force)
1909     lang_statement_union_type *s;
1910     boolean force;
1911{
1912  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
1913    {
1914      switch (s->header.type)
1915	{
1916	case lang_constructors_statement_enum:
1917	  open_input_bfds (constructor_list.head, force);
1918	  break;
1919	case lang_output_section_statement_enum:
1920	  open_input_bfds (s->output_section_statement.children.head, force);
1921	  break;
1922	case lang_wild_statement_enum:
1923	  /* Maybe we should load the file's symbols.  */
1924	  if (s->wild_statement.filename
1925	      && ! wildcardp (s->wild_statement.filename))
1926	    (void) lookup_name (s->wild_statement.filename);
1927	  open_input_bfds (s->wild_statement.children.head, force);
1928	  break;
1929	case lang_group_statement_enum:
1930	  {
1931	    struct bfd_link_hash_entry *undefs;
1932
1933	    /* We must continually search the entries in the group
1934	       until no new symbols are added to the list of undefined
1935	       symbols.  */
1936
1937	    do
1938	      {
1939		undefs = link_info.hash->undefs_tail;
1940		open_input_bfds (s->group_statement.children.head, true);
1941	      }
1942	    while (undefs != link_info.hash->undefs_tail);
1943	  }
1944	  break;
1945	case lang_target_statement_enum:
1946	  current_target = s->target_statement.target;
1947	  break;
1948	case lang_input_statement_enum:
1949	  if (s->input_statement.real)
1950	    {
1951	      lang_statement_list_type add;
1952
1953	      s->input_statement.target = current_target;
1954
1955	      /* If we are being called from within a group, and this
1956                 is an archive which has already been searched, then
1957                 force it to be researched unless the whole archive
1958		 has been loaded already.  */
1959	      if (force
1960		  && !s->input_statement.whole_archive
1961		  && s->input_statement.loaded
1962		  && bfd_check_format (s->input_statement.the_bfd,
1963				       bfd_archive))
1964		s->input_statement.loaded = false;
1965
1966	      lang_list_init (&add);
1967
1968	      if (! load_symbols (&s->input_statement, &add))
1969		config.make_executable = false;
1970
1971	      if (add.head != NULL)
1972		{
1973		  *add.tail = s->header.next;
1974		  s->header.next = add.head;
1975		}
1976	    }
1977	  break;
1978	default:
1979	  break;
1980	}
1981    }
1982}
1983
1984/* If there are [COMMONS] statements, put a wild one into the bss
1985   section.  */
1986
1987static void
1988lang_reasonable_defaults ()
1989{
1990#if 0
1991  lang_output_section_statement_lookup (".text");
1992  lang_output_section_statement_lookup (".data");
1993
1994  default_common_section = lang_output_section_statement_lookup (".bss");
1995
1996  if (placed_commons == false)
1997    {
1998      lang_wild_statement_type *new =
1999      new_stat (lang_wild_statement,
2000		&default_common_section->children);
2001
2002      new->section_name = "COMMON";
2003      new->filename = (char *) NULL;
2004      lang_list_init (&new->children);
2005    }
2006#endif
2007}
2008
2009/* Add the supplied name to the symbol table as an undefined reference.
2010   Remove items from the chain as we open input bfds.  */
2011typedef struct ldlang_undef_chain_list
2012{
2013  struct ldlang_undef_chain_list *next;
2014  char *name;
2015}                       ldlang_undef_chain_list_type;
2016
2017static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
2018
2019void
2020ldlang_add_undef (name)
2021     const char *const name;
2022{
2023  ldlang_undef_chain_list_type *new =
2024    ((ldlang_undef_chain_list_type *)
2025     stat_alloc (sizeof (ldlang_undef_chain_list_type)));
2026
2027  new->next = ldlang_undef_chain_list_head;
2028  ldlang_undef_chain_list_head = new;
2029
2030  new->name = xstrdup (name);
2031}
2032
2033/* Run through the list of undefineds created above and place them
2034   into the linker hash table as undefined symbols belonging to the
2035   script file.  */
2036
2037static void
2038lang_place_undefineds ()
2039{
2040  ldlang_undef_chain_list_type *ptr;
2041
2042  for (ptr = ldlang_undef_chain_list_head;
2043       ptr != (ldlang_undef_chain_list_type *) NULL;
2044       ptr = ptr->next)
2045    {
2046      struct bfd_link_hash_entry *h;
2047
2048      h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
2049      if (h == (struct bfd_link_hash_entry *) NULL)
2050	einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2051      if (h->type == bfd_link_hash_new)
2052	{
2053	  h->type = bfd_link_hash_undefined;
2054	  h->u.undef.abfd = NULL;
2055	  bfd_link_add_undef (link_info.hash, h);
2056	}
2057    }
2058}
2059
2060/* Open input files and attatch to output sections.  */
2061
2062static void
2063map_input_to_output_sections (s, target, output_section_statement)
2064     lang_statement_union_type *s;
2065     const char *target;
2066     lang_output_section_statement_type *output_section_statement;
2067{
2068  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2069    {
2070      switch (s->header.type)
2071	{
2072	case lang_wild_statement_enum:
2073	  wild (&s->wild_statement, target, output_section_statement);
2074	  break;
2075	case lang_constructors_statement_enum:
2076	  map_input_to_output_sections (constructor_list.head,
2077					target,
2078					output_section_statement);
2079	  break;
2080	case lang_output_section_statement_enum:
2081	  map_input_to_output_sections (s->output_section_statement.children.head,
2082					target,
2083					&s->output_section_statement);
2084	  break;
2085	case lang_output_statement_enum:
2086	  break;
2087	case lang_target_statement_enum:
2088	  target = s->target_statement.target;
2089	  break;
2090	case lang_group_statement_enum:
2091	  map_input_to_output_sections (s->group_statement.children.head,
2092					target,
2093					output_section_statement);
2094	  break;
2095	case lang_fill_statement_enum:
2096	case lang_input_section_enum:
2097	case lang_object_symbols_statement_enum:
2098	case lang_data_statement_enum:
2099	case lang_reloc_statement_enum:
2100	case lang_padding_statement_enum:
2101	case lang_input_statement_enum:
2102	  if (output_section_statement != NULL
2103	      && output_section_statement->bfd_section == NULL)
2104	    init_os (output_section_statement);
2105	  break;
2106	case lang_assignment_statement_enum:
2107	  if (output_section_statement != NULL
2108	      && output_section_statement->bfd_section == NULL)
2109	    init_os (output_section_statement);
2110
2111	  /* Make sure that any sections mentioned in the assignment
2112	     are initialized.  */
2113	  exp_init_os (s->assignment_statement.exp);
2114	  break;
2115	case lang_afile_asection_pair_statement_enum:
2116	  FAIL ();
2117	  break;
2118	case lang_address_statement_enum:
2119	  /* Mark the specified section with the supplied address.  */
2120	  {
2121	    lang_output_section_statement_type *os =
2122	      lang_output_section_statement_lookup
2123		(s->address_statement.section_name);
2124
2125	    if (os->bfd_section == NULL)
2126	      init_os (os);
2127	    os->addr_tree = s->address_statement.address;
2128	  }
2129	  break;
2130	}
2131    }
2132}
2133
2134static void
2135print_output_section_statement (output_section_statement)
2136     lang_output_section_statement_type *output_section_statement;
2137{
2138  asection *section = output_section_statement->bfd_section;
2139  int len;
2140
2141  if (output_section_statement != abs_output_section)
2142    {
2143      minfo ("\n%s", output_section_statement->name);
2144
2145      if (section != NULL)
2146	{
2147	  print_dot = section->vma;
2148
2149	  len = strlen (output_section_statement->name);
2150	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
2151	    {
2152	      print_nl ();
2153	      len = 0;
2154	    }
2155	  while (len < SECTION_NAME_MAP_LENGTH)
2156	    {
2157	      print_space ();
2158	      ++len;
2159	    }
2160
2161	  minfo ("0x%V %W", section->vma, section->_raw_size);
2162
2163	  if (output_section_statement->load_base != NULL)
2164	    {
2165	      bfd_vma addr;
2166
2167	      addr = exp_get_abs_int (output_section_statement->load_base, 0,
2168				      "load base", lang_final_phase_enum);
2169	      minfo (_(" load address 0x%V"), addr);
2170	    }
2171	}
2172
2173      print_nl ();
2174    }
2175
2176  print_statement_list (output_section_statement->children.head,
2177			output_section_statement);
2178}
2179
2180static void
2181print_assignment (assignment, output_section)
2182     lang_assignment_statement_type *assignment;
2183     lang_output_section_statement_type *output_section;
2184{
2185  int i;
2186  etree_value_type result;
2187
2188  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2189    print_space ();
2190
2191  result = exp_fold_tree (assignment->exp->assign.src, output_section,
2192			  lang_final_phase_enum, print_dot, &print_dot);
2193  if (result.valid_p)
2194    minfo ("0x%V", result.value + result.section->bfd_section->vma);
2195  else
2196    {
2197      minfo ("*undef*   ");
2198#ifdef BFD64
2199      minfo ("        ");
2200#endif
2201    }
2202
2203  minfo ("                ");
2204
2205  exp_print_tree (assignment->exp);
2206
2207  print_nl ();
2208}
2209
2210static void
2211print_input_statement (statm)
2212     lang_input_statement_type *statm;
2213{
2214  if (statm->filename != (char *) NULL)
2215    {
2216      fprintf (config.map_file, "LOAD %s\n", statm->filename);
2217    }
2218}
2219
2220/* Print all symbols defined in a particular section.  This is called
2221   via bfd_link_hash_traverse.  */
2222
2223static boolean
2224print_one_symbol (hash_entry, ptr)
2225     struct bfd_link_hash_entry *hash_entry;
2226     PTR ptr;
2227{
2228  asection *sec = (asection *) ptr;
2229
2230  if ((hash_entry->type == bfd_link_hash_defined
2231       || hash_entry->type == bfd_link_hash_defweak)
2232      && sec == hash_entry->u.def.section)
2233    {
2234      int i;
2235
2236      for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2237	print_space ();
2238      minfo ("0x%V   ",
2239	     (hash_entry->u.def.value
2240	      + hash_entry->u.def.section->output_offset
2241	      + hash_entry->u.def.section->output_section->vma));
2242
2243      minfo ("             %T\n", hash_entry->root.string);
2244    }
2245
2246  return true;
2247}
2248
2249/* Print information about an input section to the map file.  */
2250
2251static void
2252print_input_section (in)
2253     lang_input_section_type *in;
2254{
2255  asection *i = in->section;
2256  bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2257  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2258						ldfile_output_machine);
2259  if (size != 0)
2260    {
2261      print_space ();
2262
2263      minfo ("%s", i->name);
2264
2265      if (i->output_section != NULL)
2266	{
2267	  int len;
2268
2269	  len = 1 + strlen (i->name);
2270	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
2271	    {
2272	      print_nl ();
2273	      len = 0;
2274	    }
2275	  while (len < SECTION_NAME_MAP_LENGTH)
2276	    {
2277	      print_space ();
2278	      ++len;
2279	    }
2280
2281	  minfo ("0x%V %W %B\n",
2282		 i->output_section->vma + i->output_offset, size / opb,
2283		 i->owner);
2284
2285	  if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2286	    {
2287	      len = SECTION_NAME_MAP_LENGTH + 3;
2288#ifdef BFD64
2289	      len += 16;
2290#else
2291	      len += 8;
2292#endif
2293	      while (len > 0)
2294		{
2295		  print_space ();
2296		  --len;
2297		}
2298
2299	      minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2300	    }
2301
2302	  bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
2303
2304	  print_dot = i->output_section->vma + i->output_offset + size / opb;
2305	}
2306    }
2307}
2308
2309static void
2310print_fill_statement (fill)
2311     lang_fill_statement_type *fill;
2312{
2313  fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
2314}
2315
2316static void
2317print_data_statement (data)
2318     lang_data_statement_type *data;
2319{
2320  int i;
2321  bfd_vma addr;
2322  bfd_size_type size;
2323  const char *name;
2324  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2325						ldfile_output_machine);
2326
2327  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2328    print_space ();
2329
2330  addr = data->output_vma;
2331  if (data->output_section != NULL)
2332    addr += data->output_section->vma;
2333
2334  switch (data->type)
2335    {
2336    default:
2337      abort ();
2338    case BYTE:
2339      size = BYTE_SIZE;
2340      name = "BYTE";
2341      break;
2342    case SHORT:
2343      size = SHORT_SIZE;
2344      name = "SHORT";
2345      break;
2346    case LONG:
2347      size = LONG_SIZE;
2348      name = "LONG";
2349      break;
2350    case QUAD:
2351      size = QUAD_SIZE;
2352      name = "QUAD";
2353      break;
2354    case SQUAD:
2355      size = QUAD_SIZE;
2356      name = "SQUAD";
2357      break;
2358    }
2359
2360  minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2361
2362  if (data->exp->type.node_class != etree_value)
2363    {
2364      print_space ();
2365      exp_print_tree (data->exp);
2366    }
2367
2368  print_nl ();
2369
2370  print_dot = addr + size / opb;
2371
2372}
2373
2374/* Print an address statement.  These are generated by options like
2375   -Ttext.  */
2376
2377static void
2378print_address_statement (address)
2379     lang_address_statement_type *address;
2380{
2381  minfo (_("Address of section %s set to "), address->section_name);
2382  exp_print_tree (address->address);
2383  print_nl ();
2384}
2385
2386/* Print a reloc statement.  */
2387
2388static void
2389print_reloc_statement (reloc)
2390     lang_reloc_statement_type *reloc;
2391{
2392  int i;
2393  bfd_vma addr;
2394  bfd_size_type size;
2395  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2396						ldfile_output_machine);
2397
2398  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2399    print_space ();
2400
2401  addr = reloc->output_vma;
2402  if (reloc->output_section != NULL)
2403    addr += reloc->output_section->vma;
2404
2405  size = bfd_get_reloc_size (reloc->howto);
2406
2407  minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2408
2409  if (reloc->name != NULL)
2410    minfo ("%s+", reloc->name);
2411  else
2412    minfo ("%s+", reloc->section->name);
2413
2414  exp_print_tree (reloc->addend_exp);
2415
2416  print_nl ();
2417
2418  print_dot = addr + size / opb;
2419}
2420
2421static void
2422print_padding_statement (s)
2423     lang_padding_statement_type *s;
2424{
2425  int len;
2426  bfd_vma addr;
2427  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2428						ldfile_output_machine);
2429
2430  minfo (" *fill*");
2431
2432  len = sizeof " *fill*" - 1;
2433  while (len < SECTION_NAME_MAP_LENGTH)
2434    {
2435      print_space ();
2436      ++len;
2437    }
2438
2439  addr = s->output_offset;
2440  if (s->output_section != NULL)
2441    addr += s->output_section->vma;
2442  minfo ("0x%V %W", addr, s->size);
2443
2444  if (s->fill != 0)
2445    minfo (" %u", s->fill);
2446
2447  print_nl ();
2448
2449  print_dot = addr + s->size / opb;
2450}
2451
2452static void
2453print_wild_statement (w, os)
2454     lang_wild_statement_type *w;
2455     lang_output_section_statement_type *os;
2456{
2457  struct wildcard_list *sec;
2458
2459  print_space ();
2460
2461  if (w->filenames_sorted)
2462    minfo ("SORT(");
2463  if (w->filename != NULL)
2464    minfo ("%s", w->filename);
2465  else
2466    minfo ("*");
2467  if (w->filenames_sorted)
2468    minfo (")");
2469
2470  minfo ("(");
2471  for (sec = w->section_list; sec; sec = sec->next)
2472    {
2473      if (sec->spec.sorted)
2474	minfo ("SORT(");
2475      if (sec->spec.exclude_name_list != NULL)
2476	{
2477	  name_list *tmp;
2478	  minfo ("EXCLUDE_FILE ( %s", sec->spec.exclude_name_list->name);
2479	  for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2480	    minfo (", %s", tmp->name);
2481	  minfo (")");
2482	}
2483      if (sec->spec.name != NULL)
2484	minfo ("%s", sec->spec.name);
2485      else
2486	minfo ("*");
2487      if (sec->spec.sorted)
2488	minfo (")");
2489    }
2490  minfo (")");
2491
2492  print_nl ();
2493
2494  print_statement_list (w->children.head, os);
2495}
2496
2497/* Print a group statement.  */
2498
2499static void
2500print_group (s, os)
2501     lang_group_statement_type *s;
2502     lang_output_section_statement_type *os;
2503{
2504  fprintf (config.map_file, "START GROUP\n");
2505  print_statement_list (s->children.head, os);
2506  fprintf (config.map_file, "END GROUP\n");
2507}
2508
2509/* Print the list of statements in S.
2510   This can be called for any statement type.  */
2511
2512static void
2513print_statement_list (s, os)
2514     lang_statement_union_type *s;
2515     lang_output_section_statement_type *os;
2516{
2517  while (s != NULL)
2518    {
2519      print_statement (s, os);
2520      s = s->header.next;
2521    }
2522}
2523
2524/* Print the first statement in statement list S.
2525   This can be called for any statement type.  */
2526
2527static void
2528print_statement (s, os)
2529     lang_statement_union_type *s;
2530     lang_output_section_statement_type *os;
2531{
2532  switch (s->header.type)
2533    {
2534    default:
2535      fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2536      FAIL ();
2537      break;
2538    case lang_constructors_statement_enum:
2539      if (constructor_list.head != NULL)
2540	{
2541	  if (constructors_sorted)
2542	    minfo (" SORT (CONSTRUCTORS)\n");
2543	  else
2544	    minfo (" CONSTRUCTORS\n");
2545	  print_statement_list (constructor_list.head, os);
2546	}
2547      break;
2548    case lang_wild_statement_enum:
2549      print_wild_statement (&s->wild_statement, os);
2550      break;
2551    case lang_address_statement_enum:
2552      print_address_statement (&s->address_statement);
2553      break;
2554    case lang_object_symbols_statement_enum:
2555      minfo (" CREATE_OBJECT_SYMBOLS\n");
2556      break;
2557    case lang_fill_statement_enum:
2558      print_fill_statement (&s->fill_statement);
2559      break;
2560    case lang_data_statement_enum:
2561      print_data_statement (&s->data_statement);
2562      break;
2563    case lang_reloc_statement_enum:
2564      print_reloc_statement (&s->reloc_statement);
2565      break;
2566    case lang_input_section_enum:
2567      print_input_section (&s->input_section);
2568      break;
2569    case lang_padding_statement_enum:
2570      print_padding_statement (&s->padding_statement);
2571      break;
2572    case lang_output_section_statement_enum:
2573      print_output_section_statement (&s->output_section_statement);
2574      break;
2575    case lang_assignment_statement_enum:
2576      print_assignment (&s->assignment_statement, os);
2577      break;
2578    case lang_target_statement_enum:
2579      fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2580      break;
2581    case lang_output_statement_enum:
2582      minfo ("OUTPUT(%s", s->output_statement.name);
2583      if (output_target != NULL)
2584	minfo (" %s", output_target);
2585      minfo (")\n");
2586      break;
2587    case lang_input_statement_enum:
2588      print_input_statement (&s->input_statement);
2589      break;
2590    case lang_group_statement_enum:
2591      print_group (&s->group_statement, os);
2592      break;
2593    case lang_afile_asection_pair_statement_enum:
2594      FAIL ();
2595      break;
2596    }
2597}
2598
2599static void
2600print_statements ()
2601{
2602  print_statement_list (statement_list.head, abs_output_section);
2603}
2604
2605/* Print the first N statements in statement list S to STDERR.
2606   If N == 0, nothing is printed.
2607   If N < 0, the entire list is printed.
2608   Intended to be called from GDB.  */
2609
2610void
2611dprint_statement (s, n)
2612     lang_statement_union_type *s;
2613     int n;
2614{
2615  FILE *map_save = config.map_file;
2616
2617  config.map_file = stderr;
2618
2619  if (n < 0)
2620    print_statement_list (s, abs_output_section);
2621  else
2622    {
2623      while (s && --n >= 0)
2624	{
2625	  print_statement (s, abs_output_section);
2626	  s = s->header.next;
2627	}
2628    }
2629
2630  config.map_file = map_save;
2631}
2632
2633static void
2634insert_pad (ptr, fill, alignment_needed, output_section, dot)
2635     lang_statement_union_type **ptr;
2636     fill_type fill;
2637     unsigned int alignment_needed;
2638     asection *output_section;
2639     bfd_vma dot;
2640{
2641  lang_statement_union_type *pad;
2642
2643  pad = ((lang_statement_union_type *)
2644	 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2645  if (ptr != &statement_list.head
2646      && pad->header.type == lang_padding_statement_enum
2647      && pad->padding_statement.output_section == output_section)
2648    {
2649      /* Use the existing pad statement.  The above test on output
2650	 section is probably redundant, but it doesn't hurt to check.  */
2651    }
2652  else
2653    {
2654      /* Make a new padding statement, linked into existing chain.  */
2655      pad = ((lang_statement_union_type *)
2656	     stat_alloc (sizeof (lang_padding_statement_type)));
2657      pad->header.next = *ptr;
2658      *ptr = pad;
2659      pad->header.type = lang_padding_statement_enum;
2660      pad->padding_statement.output_section = output_section;
2661      pad->padding_statement.fill = fill;
2662    }
2663  pad->padding_statement.output_offset = dot - output_section->vma;
2664  pad->padding_statement.size = alignment_needed;
2665  output_section->_raw_size += alignment_needed;
2666}
2667
2668/* Work out how much this section will move the dot point.  */
2669
2670static bfd_vma
2671size_input_section (this_ptr, output_section_statement, fill, dot)
2672     lang_statement_union_type **this_ptr;
2673     lang_output_section_statement_type *output_section_statement;
2674     fill_type fill;
2675     bfd_vma dot;
2676{
2677  lang_input_section_type *is = &((*this_ptr)->input_section);
2678  asection *i = is->section;
2679
2680  if (is->ifile->just_syms_flag == false)
2681    {
2682      unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2683						    ldfile_output_machine);
2684      unsigned int alignment_needed;
2685      asection *o;
2686
2687      /* Align this section first to the input sections requirement,
2688	 then to the output section's requirement.  If this alignment
2689	 is greater than any seen before, then record it too.  Perform
2690	 the alignment by inserting a magic 'padding' statement.  */
2691
2692      if (output_section_statement->subsection_alignment != -1)
2693	i->alignment_power = output_section_statement->subsection_alignment;
2694
2695      o = output_section_statement->bfd_section;
2696      if (o->alignment_power < i->alignment_power)
2697	o->alignment_power = i->alignment_power;
2698
2699      alignment_needed = align_power (dot, i->alignment_power) - dot;
2700
2701      if (alignment_needed != 0)
2702	{
2703	  insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2704	  dot += alignment_needed;
2705	}
2706
2707      /* Remember where in the output section this input section goes.  */
2708
2709      i->output_offset = dot - o->vma;
2710
2711      /* Mark how big the output section must be to contain this now.  */
2712      if (i->_cooked_size != 0)
2713	dot += i->_cooked_size / opb;
2714      else
2715	dot += i->_raw_size / opb;
2716      o->_raw_size = (dot - o->vma) * opb;
2717    }
2718  else
2719    {
2720      i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2721    }
2722
2723  return dot;
2724}
2725
2726#define IGNORE_SECTION(bfd, s) \
2727  (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD))	\
2728    != (SEC_ALLOC | SEC_LOAD))					\
2729   || bfd_section_size (bfd, s) == 0)
2730
2731/* Check to see if any allocated sections overlap with other allocated
2732   sections.  This can happen when the linker script specifically specifies
2733   the output section addresses of the two sections.  */
2734
2735static void
2736lang_check_section_addresses ()
2737{
2738  asection *s;
2739  unsigned opb = bfd_octets_per_byte (output_bfd);
2740
2741  /* Scan all sections in the output list.  */
2742  for (s = output_bfd->sections; s != NULL; s = s->next)
2743    {
2744      asection *os;
2745
2746      /* Ignore sections which are not loaded or which have no contents.  */
2747      if (IGNORE_SECTION (output_bfd, s))
2748	continue;
2749
2750      /* Once we reach section 's' stop our seach.  This prevents two
2751	 warning messages from being produced, one for 'section A overlaps
2752	 section B' and one for 'section B overlaps section A'.  */
2753      for (os = output_bfd->sections; os != s; os = os->next)
2754	{
2755	  bfd_vma s_start;
2756	  bfd_vma s_end;
2757	  bfd_vma os_start;
2758	  bfd_vma os_end;
2759
2760	  /* Only consider loadable sections with real contents.  */
2761	  if (IGNORE_SECTION (output_bfd, os))
2762	    continue;
2763
2764	  /* We must check the sections' LMA addresses not their
2765	     VMA addresses because overlay sections can have
2766	     overlapping VMAs but they must have distinct LMAs.  */
2767	  s_start  = bfd_section_lma (output_bfd, s);
2768	  os_start = bfd_section_lma (output_bfd, os);
2769	  s_end    = s_start  + bfd_section_size (output_bfd, s) / opb - 1;
2770	  os_end   = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2771
2772	  /* Look for an overlap.  */
2773	  if ((s_end < os_start) || (s_start > os_end))
2774	    continue;
2775
2776	  einfo (
2777_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2778		 s->name, s_start, s_end, os->name, os_start, os_end);
2779
2780	  /* Once we have found one overlap for this section,
2781	     stop looking for others.  */
2782	  break;
2783	}
2784    }
2785}
2786
2787/* Make sure the new address is within the region.  We explicitly permit the
2788   current address to be at the exact end of the region when the address is
2789   non-zero, in case the region is at the end of addressable memory and the
2790   calculation wraps around.  */
2791
2792static void
2793os_region_check (os, region, tree, base)
2794     lang_output_section_statement_type *os;
2795     struct memory_region_struct *region;
2796     etree_type *tree;
2797     bfd_vma base;
2798{
2799  if ((region->current < region->origin
2800       || (region->current - region->origin > region->length))
2801      && ((region->current != region->origin + region->length)
2802           || base == 0))
2803    {
2804      if (tree != (etree_type *) NULL)
2805        {
2806          einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2807                 region->current,
2808                 os->bfd_section->owner,
2809                 os->bfd_section->name,
2810                 region->name);
2811        }
2812      else
2813        {
2814          einfo (_("%X%P: region %s is full (%B section %s)\n"),
2815                 region->name,
2816                 os->bfd_section->owner,
2817                 os->bfd_section->name);
2818        }
2819      /* Reset the region pointer.  */
2820      region->current = region->origin;
2821    }
2822}
2823
2824/* Set the sizes for all the output sections.  */
2825
2826bfd_vma
2827lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2828     lang_statement_union_type *s;
2829     lang_output_section_statement_type *output_section_statement;
2830     lang_statement_union_type **prev;
2831     fill_type fill;
2832     bfd_vma dot;
2833     boolean *relax;
2834{
2835  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2836						ldfile_output_machine);
2837
2838  /* Size up the sections from their constituent parts.  */
2839  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2840    {
2841      switch (s->header.type)
2842	{
2843	case lang_output_section_statement_enum:
2844	  {
2845	    bfd_vma after;
2846	    lang_output_section_statement_type *os;
2847
2848	    os = &s->output_section_statement;
2849	    if (os->bfd_section == NULL)
2850	      /* This section was never actually created.  */
2851	      break;
2852
2853	    /* If this is a COFF shared library section, use the size and
2854	       address from the input section.  FIXME: This is COFF
2855	       specific; it would be cleaner if there were some other way
2856	       to do this, but nothing simple comes to mind.  */
2857	    if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2858	      {
2859		asection *input;
2860
2861		if (os->children.head == NULL
2862		    || os->children.head->header.next != NULL
2863		    || os->children.head->header.type != lang_input_section_enum)
2864		  einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2865			 os->name);
2866
2867		input = os->children.head->input_section.section;
2868		bfd_set_section_vma (os->bfd_section->owner,
2869				     os->bfd_section,
2870				     bfd_section_vma (input->owner, input));
2871		os->bfd_section->_raw_size = input->_raw_size;
2872		break;
2873	      }
2874
2875	    if (bfd_is_abs_section (os->bfd_section))
2876	      {
2877		/* No matter what happens, an abs section starts at zero.  */
2878		ASSERT (os->bfd_section->vma == 0);
2879	      }
2880	    else
2881	      {
2882		if (os->addr_tree == (etree_type *) NULL)
2883		  {
2884		    /* No address specified for this section, get one
2885		       from the region specification.  */
2886		    if (os->region == (lang_memory_region_type *) NULL
2887			|| (((bfd_get_section_flags (output_bfd, os->bfd_section)
2888			      & (SEC_ALLOC | SEC_LOAD)) != 0)
2889			    && os->region->name[0] == '*'
2890			    && strcmp (os->region->name, "*default*") == 0))
2891		      {
2892			os->region = lang_memory_default (os->bfd_section);
2893		      }
2894
2895		    /* If a loadable section is using the default memory
2896		       region, and some non default memory regions were
2897		       defined, issue a warning.  */
2898		    if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2899			 & (SEC_ALLOC | SEC_LOAD)) != 0
2900			&& ! link_info.relocateable
2901			&& strcmp (os->region->name, "*default*") == 0
2902			&& lang_memory_region_list != NULL
2903			&& (strcmp (lang_memory_region_list->name,
2904				    "*default*") != 0
2905			    || lang_memory_region_list->next != NULL))
2906		      einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2907			     bfd_get_section_name (output_bfd,
2908						   os->bfd_section));
2909
2910		    dot = os->region->current;
2911
2912		    if (os->section_alignment == -1)
2913		      {
2914			bfd_vma olddot;
2915
2916			olddot = dot;
2917			dot = align_power (dot,
2918					   os->bfd_section->alignment_power);
2919
2920			if (dot != olddot && config.warn_section_align)
2921			  einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2922				 os->name, (unsigned int) (dot - olddot));
2923		      }
2924		  }
2925		else
2926		  {
2927		    etree_value_type r;
2928
2929		    r = exp_fold_tree (os->addr_tree,
2930				       abs_output_section,
2931				       lang_allocating_phase_enum,
2932				       dot, &dot);
2933		    if (r.valid_p == false)
2934		      {
2935			einfo (_("%F%S: non constant address expression for section %s\n"),
2936			       os->name);
2937		      }
2938		    dot = r.value + r.section->bfd_section->vma;
2939		  }
2940
2941		/* The section starts here.
2942		   First, align to what the section needs.  */
2943
2944		if (os->section_alignment != -1)
2945		  dot = align_power (dot, os->section_alignment);
2946
2947		bfd_set_section_vma (0, os->bfd_section, dot);
2948
2949		os->bfd_section->output_offset = 0;
2950	      }
2951
2952	    lang_size_sections (os->children.head, os, &os->children.head,
2953				os->fill, dot, relax);
2954
2955	    /* Put the section within the requested block size, or
2956	       align at the block boundary.  */
2957	    after = ALIGN_N (os->bfd_section->vma
2958			     + os->bfd_section->_raw_size / opb,
2959			     /* The coercion here is important, see ld.h.  */
2960			     (bfd_vma) os->block_value);
2961
2962	    if (bfd_is_abs_section (os->bfd_section))
2963	      ASSERT (after == os->bfd_section->vma);
2964	    else
2965	      os->bfd_section->_raw_size =
2966		(after - os->bfd_section->vma) * opb;
2967	    dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
2968	    os->processed = true;
2969
2970	    /* Update dot in the region ?
2971	       We only do this if the section is going to be allocated,
2972	       since unallocated sections do not contribute to the region's
2973	       overall size in memory.
2974
2975	       If the SEC_NEVER_LOAD bit is not set, it will affect the
2976	       addresses of sections after it. We have to update
2977	       dot.  */
2978	    if (os->region != (lang_memory_region_type *) NULL
2979		&& ((bfd_get_section_flags (output_bfd, os->bfd_section)
2980		     & SEC_NEVER_LOAD) == 0
2981		    || (bfd_get_section_flags (output_bfd, os->bfd_section)
2982			& (SEC_ALLOC | SEC_LOAD))))
2983	      {
2984		os->region->current = dot;
2985
2986		/* Make sure the new address is within the region.  */
2987		os_region_check (os, os->region, os->addr_tree,
2988				 os->bfd_section->vma);
2989
2990		/* If there's no load address specified, use the run
2991		   region as the load region.  */
2992		if (os->lma_region == NULL && os->load_base == NULL)
2993		  os->lma_region = os->region;
2994
2995		if (os->lma_region != NULL)
2996		  {
2997		    if (os->load_base != NULL)
2998		      {
2999			einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
3000		      }
3001		    else
3002		      {
3003			/* Don't allocate twice.  */
3004			if (os->lma_region != os->region)
3005			  {
3006			    /* Set load_base, which will be handled later.  */
3007			    os->load_base =
3008			      exp_intop (os->lma_region->current);
3009			    os->lma_region->current +=
3010			      os->bfd_section->_raw_size / opb;
3011			    os_region_check (os, os->lma_region, NULL,
3012					     os->bfd_section->lma);
3013			  }
3014		      }
3015		  }
3016	      }
3017	  }
3018	  break;
3019
3020	case lang_constructors_statement_enum:
3021	  dot = lang_size_sections (constructor_list.head,
3022				    output_section_statement,
3023				    &s->wild_statement.children.head,
3024				    fill, dot, relax);
3025	  break;
3026
3027	case lang_data_statement_enum:
3028	  {
3029	    unsigned int size = 0;
3030
3031	    s->data_statement.output_vma =
3032	      dot - output_section_statement->bfd_section->vma;
3033	    s->data_statement.output_section =
3034	      output_section_statement->bfd_section;
3035
3036	    switch (s->data_statement.type)
3037	      {
3038	      default:
3039		abort ();
3040	      case QUAD:
3041	      case SQUAD:
3042		size = QUAD_SIZE;
3043		break;
3044	      case LONG:
3045		size = LONG_SIZE;
3046		break;
3047	      case SHORT:
3048		size = SHORT_SIZE;
3049		break;
3050	      case BYTE:
3051		size = BYTE_SIZE;
3052		break;
3053	      }
3054	    if (size < opb)
3055	      size = opb;
3056	    dot += size / opb;
3057	    output_section_statement->bfd_section->_raw_size += size;
3058	    /* The output section gets contents, and then we inspect for
3059	       any flags set in the input script which override any ALLOC.  */
3060	    output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3061	    if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3062	      {
3063		output_section_statement->bfd_section->flags |=
3064		  SEC_ALLOC | SEC_LOAD;
3065	      }
3066	  }
3067	  break;
3068
3069	case lang_reloc_statement_enum:
3070	  {
3071	    int size;
3072
3073	    s->reloc_statement.output_vma =
3074	      dot - output_section_statement->bfd_section->vma;
3075	    s->reloc_statement.output_section =
3076	      output_section_statement->bfd_section;
3077	    size = bfd_get_reloc_size (s->reloc_statement.howto);
3078	    dot += size / opb;
3079	    output_section_statement->bfd_section->_raw_size += size;
3080	  }
3081	  break;
3082
3083	case lang_wild_statement_enum:
3084
3085	  dot = lang_size_sections (s->wild_statement.children.head,
3086				    output_section_statement,
3087				    &s->wild_statement.children.head,
3088				    fill, dot, relax);
3089
3090	  break;
3091
3092	case lang_object_symbols_statement_enum:
3093	  link_info.create_object_symbols_section =
3094	    output_section_statement->bfd_section;
3095	  break;
3096	case lang_output_statement_enum:
3097	case lang_target_statement_enum:
3098	  break;
3099	case lang_input_section_enum:
3100	  {
3101	    asection *i;
3102
3103	    i = (*prev)->input_section.section;
3104	    if (! relax)
3105	      {
3106		if (i->_cooked_size == 0)
3107		  i->_cooked_size = i->_raw_size;
3108	      }
3109	    else
3110	      {
3111		boolean again;
3112
3113		if (! bfd_relax_section (i->owner, i, &link_info, &again))
3114		  einfo (_("%P%F: can't relax section: %E\n"));
3115		if (again)
3116		  *relax = true;
3117	      }
3118	    dot = size_input_section (prev, output_section_statement,
3119				      output_section_statement->fill, dot);
3120	  }
3121	  break;
3122	case lang_input_statement_enum:
3123	  break;
3124	case lang_fill_statement_enum:
3125	  s->fill_statement.output_section =
3126	    output_section_statement->bfd_section;
3127
3128	  fill = s->fill_statement.fill;
3129	  break;
3130	case lang_assignment_statement_enum:
3131	  {
3132	    bfd_vma newdot = dot;
3133
3134	    exp_fold_tree (s->assignment_statement.exp,
3135			   output_section_statement,
3136			   lang_allocating_phase_enum,
3137			   dot,
3138			   &newdot);
3139
3140	    if (newdot != dot)
3141	      {
3142		if (output_section_statement == abs_output_section)
3143		  {
3144		    /* If we don't have an output section, then just adjust
3145		       the default memory address.  */
3146		    lang_memory_region_lookup ("*default*")->current = newdot;
3147		  }
3148		else
3149		  {
3150		    /* Insert a pad after this statement.  We can't
3151		       put the pad before when relaxing, in case the
3152		       assignment references dot.  */
3153		    insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3154				output_section_statement->bfd_section, dot);
3155
3156		    /* Don't neuter the pad below when relaxing.  */
3157		    s = s->header.next;
3158		  }
3159
3160		dot = newdot;
3161	      }
3162	  }
3163	  break;
3164
3165	case lang_padding_statement_enum:
3166	  /* If this is the first time lang_size_sections is called,
3167	     we won't have any padding statements.  If this is the
3168	     second or later passes when relaxing, we should allow
3169	     padding to shrink.  If padding is needed on this pass, it
3170	     will be added back in.  */
3171	  s->padding_statement.size = 0;
3172
3173	  /* Make sure output_offset is valid.  If relaxation shrinks
3174	     the section and this pad isn't needed, it's possible to
3175	     have output_offset larger than the final size of the
3176	     section.  bfd_set_section_contents will complain even for
3177	     a pad size of zero.  */
3178	  s->padding_statement.output_offset
3179	    = dot - output_section_statement->bfd_section->vma;
3180	  break;
3181
3182	case lang_group_statement_enum:
3183	  dot = lang_size_sections (s->group_statement.children.head,
3184				    output_section_statement,
3185				    &s->group_statement.children.head,
3186				    fill, dot, relax);
3187	  break;
3188
3189	default:
3190	  FAIL ();
3191	  break;
3192
3193	  /* We can only get here when relaxing is turned on.  */
3194	case lang_address_statement_enum:
3195	  break;
3196	}
3197      prev = &s->header.next;
3198    }
3199  return dot;
3200}
3201
3202bfd_vma
3203lang_do_assignments (s, output_section_statement, fill, dot)
3204     lang_statement_union_type *s;
3205     lang_output_section_statement_type *output_section_statement;
3206     fill_type fill;
3207     bfd_vma dot;
3208{
3209  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3210						ldfile_output_machine);
3211
3212  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3213    {
3214      switch (s->header.type)
3215	{
3216	case lang_constructors_statement_enum:
3217	  dot = lang_do_assignments (constructor_list.head,
3218				     output_section_statement,
3219				     fill,
3220				     dot);
3221	  break;
3222
3223	case lang_output_section_statement_enum:
3224	  {
3225	    lang_output_section_statement_type *os;
3226
3227	    os = &(s->output_section_statement);
3228	    if (os->bfd_section != NULL)
3229	      {
3230		dot = os->bfd_section->vma;
3231		(void) lang_do_assignments (os->children.head, os,
3232					    os->fill, dot);
3233		dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3234
3235	      }
3236	    if (os->load_base)
3237	      {
3238		/* If nothing has been placed into the output section then
3239		   it won't have a bfd_section.  */
3240		if (os->bfd_section)
3241		  {
3242		    os->bfd_section->lma
3243		      = exp_get_abs_int (os->load_base, 0, "load base",
3244					 lang_final_phase_enum);
3245		  }
3246	      }
3247	  }
3248	  break;
3249	case lang_wild_statement_enum:
3250
3251	  dot = lang_do_assignments (s->wild_statement.children.head,
3252				     output_section_statement,
3253				     fill, dot);
3254
3255	  break;
3256
3257	case lang_object_symbols_statement_enum:
3258	case lang_output_statement_enum:
3259	case lang_target_statement_enum:
3260#if 0
3261	case lang_common_statement_enum:
3262#endif
3263	  break;
3264	case lang_data_statement_enum:
3265	  {
3266	    etree_value_type value;
3267
3268	    value = exp_fold_tree (s->data_statement.exp,
3269				   abs_output_section,
3270				   lang_final_phase_enum, dot, &dot);
3271	    s->data_statement.value = value.value;
3272	    if (value.valid_p == false)
3273	      einfo (_("%F%P: invalid data statement\n"));
3274	  }
3275          {
3276            unsigned int size;
3277	    switch (s->data_statement.type)
3278	      {
3279	      default:
3280		abort ();
3281	      case QUAD:
3282	      case SQUAD:
3283		size = QUAD_SIZE;
3284		break;
3285	      case LONG:
3286		size = LONG_SIZE;
3287		break;
3288	      case SHORT:
3289		size = SHORT_SIZE;
3290		break;
3291	      case BYTE:
3292		size = BYTE_SIZE;
3293		break;
3294	      }
3295	    if (size < opb)
3296	      size = opb;
3297	    dot += size / opb;
3298	  }
3299	  break;
3300
3301	case lang_reloc_statement_enum:
3302	  {
3303	    etree_value_type value;
3304
3305	    value = exp_fold_tree (s->reloc_statement.addend_exp,
3306				   abs_output_section,
3307				   lang_final_phase_enum, dot, &dot);
3308	    s->reloc_statement.addend_value = value.value;
3309	    if (value.valid_p == false)
3310	      einfo (_("%F%P: invalid reloc statement\n"));
3311	  }
3312	  dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
3313	  break;
3314
3315	case lang_input_section_enum:
3316	  {
3317	    asection *in = s->input_section.section;
3318
3319	    if (in->_cooked_size != 0)
3320	      dot += in->_cooked_size / opb;
3321	    else
3322	      dot += in->_raw_size / opb;
3323	  }
3324	  break;
3325
3326	case lang_input_statement_enum:
3327	  break;
3328	case lang_fill_statement_enum:
3329	  fill = s->fill_statement.fill;
3330	  break;
3331	case lang_assignment_statement_enum:
3332	  {
3333	    exp_fold_tree (s->assignment_statement.exp,
3334			   output_section_statement,
3335			   lang_final_phase_enum,
3336			   dot,
3337			   &dot);
3338	  }
3339
3340	  break;
3341	case lang_padding_statement_enum:
3342	  dot += s->padding_statement.size / opb;
3343	  break;
3344
3345	case lang_group_statement_enum:
3346	  dot = lang_do_assignments (s->group_statement.children.head,
3347				     output_section_statement,
3348				     fill, dot);
3349
3350	  break;
3351
3352	default:
3353	  FAIL ();
3354	  break;
3355	case lang_address_statement_enum:
3356	  break;
3357	}
3358
3359    }
3360  return dot;
3361}
3362
3363/* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
3364   operator .startof. (section_name), it produces an undefined symbol
3365   .startof.section_name.  Similarly, when it sees
3366   .sizeof. (section_name), it produces an undefined symbol
3367   .sizeof.section_name.  For all the output sections, we look for
3368   such symbols, and set them to the correct value.  */
3369
3370static void
3371lang_set_startof ()
3372{
3373  asection *s;
3374
3375  if (link_info.relocateable)
3376    return;
3377
3378  for (s = output_bfd->sections; s != NULL; s = s->next)
3379    {
3380      const char *secname;
3381      char *buf;
3382      struct bfd_link_hash_entry *h;
3383
3384      secname = bfd_get_section_name (output_bfd, s);
3385      buf = xmalloc (10 + strlen (secname));
3386
3387      sprintf (buf, ".startof.%s", secname);
3388      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3389      if (h != NULL && h->type == bfd_link_hash_undefined)
3390	{
3391	  h->type = bfd_link_hash_defined;
3392	  h->u.def.value = bfd_get_section_vma (output_bfd, s);
3393	  h->u.def.section = bfd_abs_section_ptr;
3394	}
3395
3396      sprintf (buf, ".sizeof.%s", secname);
3397      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3398      if (h != NULL && h->type == bfd_link_hash_undefined)
3399	{
3400          unsigned opb;
3401
3402          opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3403					       ldfile_output_machine);
3404	  h->type = bfd_link_hash_defined;
3405	  if (s->_cooked_size != 0)
3406	    h->u.def.value = s->_cooked_size / opb;
3407	  else
3408	    h->u.def.value = s->_raw_size / opb;
3409	  h->u.def.section = bfd_abs_section_ptr;
3410	}
3411
3412      free (buf);
3413    }
3414}
3415
3416static void
3417lang_finish ()
3418{
3419  struct bfd_link_hash_entry *h;
3420  boolean warn;
3421
3422  if (link_info.relocateable || link_info.shared)
3423    warn = false;
3424  else
3425    warn = true;
3426
3427  if (entry_symbol == (char *) NULL)
3428    {
3429      /* No entry has been specified.  Look for start, but don't warn
3430	 if we don't find it.  */
3431      entry_symbol = "start";
3432      warn = false;
3433    }
3434
3435  h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
3436  if (h != (struct bfd_link_hash_entry *) NULL
3437      && (h->type == bfd_link_hash_defined
3438	  || h->type == bfd_link_hash_defweak)
3439      && h->u.def.section->output_section != NULL)
3440    {
3441      bfd_vma val;
3442
3443      val = (h->u.def.value
3444	     + bfd_get_section_vma (output_bfd,
3445				    h->u.def.section->output_section)
3446	     + h->u.def.section->output_offset);
3447      if (! bfd_set_start_address (output_bfd, val))
3448	einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
3449    }
3450  else
3451    {
3452      bfd_vma val;
3453      const char *send;
3454
3455      /* We couldn't find the entry symbol.  Try parsing it as a
3456         number.  */
3457      val = bfd_scan_vma (entry_symbol, &send, 0);
3458      if (*send == '\0')
3459	{
3460	  if (! bfd_set_start_address (output_bfd, val))
3461	    einfo (_("%P%F: can't set start address\n"));
3462	}
3463      else
3464	{
3465	  asection *ts;
3466
3467	  /* Can't find the entry symbol, and it's not a number.  Use
3468	     the first address in the text section.  */
3469	  ts = bfd_get_section_by_name (output_bfd, entry_section);
3470	  if (ts != (asection *) NULL)
3471	    {
3472	      if (warn)
3473		einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3474		       entry_symbol, bfd_get_section_vma (output_bfd, ts));
3475	      if (! bfd_set_start_address (output_bfd,
3476					   bfd_get_section_vma (output_bfd,
3477								ts)))
3478		einfo (_("%P%F: can't set start address\n"));
3479	    }
3480	  else
3481	    {
3482	      if (warn)
3483		einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3484		       entry_symbol);
3485	    }
3486	}
3487    }
3488}
3489
3490/* This is a small function used when we want to ignore errors from
3491   BFD.  */
3492
3493static void
3494#ifdef ANSI_PROTOTYPES
3495ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3496#else
3497ignore_bfd_errors (s)
3498     const char *s ATTRIBUTE_UNUSED;
3499#endif
3500{
3501  /* Don't do anything.  */
3502}
3503
3504/* Check that the architecture of all the input files is compatible
3505   with the output file.  Also call the backend to let it do any
3506   other checking that is needed.  */
3507
3508static void
3509lang_check ()
3510{
3511  lang_statement_union_type *file;
3512  bfd *input_bfd;
3513  const bfd_arch_info_type *compatible;
3514
3515  for (file = file_chain.head;
3516       file != (lang_statement_union_type *) NULL;
3517       file = file->input_statement.next)
3518    {
3519      input_bfd = file->input_statement.the_bfd;
3520      compatible = bfd_arch_get_compatible (input_bfd,
3521					    output_bfd);
3522      if (compatible == NULL)
3523	{
3524	  if (command_line.warn_mismatch)
3525	    einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3526		   bfd_printable_name (input_bfd), input_bfd,
3527		   bfd_printable_name (output_bfd));
3528	}
3529      else if (link_info.relocateable
3530	       /* In general it is not possible to perform a relocatable
3531		  link between differing object formats when the input
3532		  file has relocations, because the relocations in the
3533		  input format may not have equivalent representations in
3534		  the output format (and besides BFD does not translate
3535		  relocs for other link purposes than a final link).  */
3536	       && bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd)
3537	       && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3538	einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3539	       bfd_get_target (input_bfd), input_bfd,
3540	       bfd_get_target (output_bfd), output_bfd);
3541      else if (bfd_count_sections (input_bfd))
3542	{
3543	  /* If the input bfd has no contents, it shouldn't set the
3544	     private data of the output bfd. */
3545
3546	  bfd_error_handler_type pfn = NULL;
3547
3548	  /* If we aren't supposed to warn about mismatched input
3549             files, temporarily set the BFD error handler to a
3550             function which will do nothing.  We still want to call
3551             bfd_merge_private_bfd_data, since it may set up
3552             information which is needed in the output file.  */
3553	  if (! command_line.warn_mismatch)
3554	    pfn = bfd_set_error_handler (ignore_bfd_errors);
3555	  if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3556	    {
3557	      if (command_line.warn_mismatch)
3558		einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3559		       input_bfd);
3560	    }
3561	  if (! command_line.warn_mismatch)
3562	    bfd_set_error_handler (pfn);
3563	}
3564    }
3565}
3566
3567/* Look through all the global common symbols and attach them to the
3568   correct section.  The -sort-common command line switch may be used
3569   to roughly sort the entries by size.  */
3570
3571static void
3572lang_common ()
3573{
3574  if (command_line.inhibit_common_definition)
3575    return;
3576  if (link_info.relocateable
3577      && ! command_line.force_common_definition)
3578    return;
3579
3580  if (! config.sort_common)
3581    bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3582  else
3583    {
3584      int power;
3585
3586      for (power = 4; power >= 0; power--)
3587	bfd_link_hash_traverse (link_info.hash, lang_one_common,
3588				(PTR) &power);
3589    }
3590}
3591
3592/* Place one common symbol in the correct section.  */
3593
3594static boolean
3595lang_one_common (h, info)
3596     struct bfd_link_hash_entry *h;
3597     PTR info;
3598{
3599  unsigned int power_of_two;
3600  bfd_vma size;
3601  asection *section;
3602  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3603						ldfile_output_machine);
3604
3605  if (h->type != bfd_link_hash_common)
3606    return true;
3607
3608  size = h->u.c.size;
3609  power_of_two = h->u.c.p->alignment_power;
3610
3611  if (config.sort_common
3612      && power_of_two < (unsigned int) *(int *) info)
3613    return true;
3614
3615  section = h->u.c.p->section;
3616
3617  /* Increase the size of the section.  */
3618  section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb,
3619				   (bfd_size_type) (1 << power_of_two)) * opb;
3620
3621  /* Adjust the alignment if necessary.  */
3622  if (power_of_two > section->alignment_power)
3623    section->alignment_power = power_of_two;
3624
3625  /* Change the symbol from common to defined.  */
3626  h->type = bfd_link_hash_defined;
3627  h->u.def.section = section;
3628  h->u.def.value = section->_cooked_size;
3629
3630  /* Increase the size of the section.  */
3631  section->_cooked_size += size;
3632
3633  /* Make sure the section is allocated in memory, and make sure that
3634     it is no longer a common section.  */
3635  section->flags |= SEC_ALLOC;
3636  section->flags &= ~SEC_IS_COMMON;
3637
3638  if (config.map_file != NULL)
3639    {
3640      static boolean header_printed;
3641      int len;
3642      char *name;
3643      char buf[50];
3644
3645      if (! header_printed)
3646	{
3647	  minfo (_("\nAllocating common symbols\n"));
3648	  minfo (_("Common symbol       size              file\n\n"));
3649	  header_printed = true;
3650	}
3651
3652      name = demangle (h->root.string);
3653      minfo ("%s", name);
3654      len = strlen (name);
3655      free (name);
3656
3657      if (len >= 19)
3658	{
3659	  print_nl ();
3660	  len = 0;
3661	}
3662      while (len < 20)
3663	{
3664	  print_space ();
3665	  ++len;
3666	}
3667
3668      minfo ("0x");
3669      if (size <= 0xffffffff)
3670	sprintf (buf, "%lx", (unsigned long) size);
3671      else
3672	sprintf_vma (buf, size);
3673      minfo ("%s", buf);
3674      len = strlen (buf);
3675
3676      while (len < 16)
3677	{
3678	  print_space ();
3679	  ++len;
3680	}
3681
3682      minfo ("%B\n", section->owner);
3683    }
3684
3685  return true;
3686}
3687
3688/* Run through the input files and ensure that every input section has
3689   somewhere to go.  If one is found without a destination then create
3690   an input request and place it into the statement tree.  */
3691
3692static void
3693lang_place_orphans ()
3694{
3695  LANG_FOR_EACH_INPUT_STATEMENT (file)
3696    {
3697      asection *s;
3698
3699      for (s = file->the_bfd->sections;
3700	   s != (asection *) NULL;
3701	   s = s->next)
3702	{
3703	  if (s->output_section == (asection *) NULL)
3704	    {
3705	      /* This section of the file is not attatched, root
3706	         around for a sensible place for it to go.  */
3707
3708	      if (file->just_syms_flag)
3709		{
3710		  /* We are only retrieving symbol values from this
3711                     file.  We want the symbols to act as though the
3712                     values in the file are absolute.  */
3713		  s->output_section = bfd_abs_section_ptr;
3714		  s->output_offset = s->vma;
3715		}
3716	      else if (strcmp (s->name, "COMMON") == 0)
3717		{
3718		  /* This is a lonely common section which must have
3719		     come from an archive.  We attach to the section
3720		     with the wildcard.  */
3721		  if (! link_info.relocateable
3722		      || command_line.force_common_definition)
3723		    {
3724		      if (default_common_section == NULL)
3725			{
3726#if 0
3727			  /* This message happens when using the
3728                             svr3.ifile linker script, so I have
3729                             disabled it.  */
3730			  info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3731#endif
3732			  default_common_section =
3733			    lang_output_section_statement_lookup (".bss");
3734
3735			}
3736		      lang_add_section (&default_common_section->children, s,
3737					default_common_section, file);
3738		    }
3739		}
3740	      else if (ldemul_place_orphan (file, s))
3741		;
3742	      else
3743		{
3744		  lang_output_section_statement_type *os;
3745
3746		  os = lang_output_section_statement_lookup (s->name);
3747		  lang_add_section (&os->children, s, os, file);
3748		}
3749	    }
3750	}
3751    }
3752}
3753
3754void
3755lang_set_flags (ptr, flags, invert)
3756     lang_memory_region_type *ptr;
3757     const char *flags;
3758     int invert;
3759{
3760  flagword *ptr_flags;
3761
3762  ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3763  while (*flags)
3764    {
3765      switch (*flags)
3766	{
3767	case 'A': case 'a':
3768	  *ptr_flags |= SEC_ALLOC;
3769	  break;
3770
3771	case 'R': case 'r':
3772	  *ptr_flags |= SEC_READONLY;
3773	  break;
3774
3775	case 'W': case 'w':
3776	  *ptr_flags |= SEC_DATA;
3777	  break;
3778
3779	case 'X': case 'x':
3780	  *ptr_flags |= SEC_CODE;
3781	  break;
3782
3783	case 'L': case 'l':
3784	case 'I': case 'i':
3785	  *ptr_flags |= SEC_LOAD;
3786	  break;
3787
3788	default:
3789	  einfo (_("%P%F: invalid syntax in flags\n"));
3790	  break;
3791	}
3792      flags++;
3793    }
3794}
3795
3796/* Call a function on each input file.  This function will be called
3797   on an archive, but not on the elements.  */
3798
3799void
3800lang_for_each_input_file (func)
3801     void (*func) PARAMS ((lang_input_statement_type *));
3802{
3803  lang_input_statement_type *f;
3804
3805  for (f = (lang_input_statement_type *) input_file_chain.head;
3806       f != NULL;
3807       f = (lang_input_statement_type *) f->next_real_file)
3808    func (f);
3809}
3810
3811/* Call a function on each file.  The function will be called on all
3812   the elements of an archive which are included in the link, but will
3813   not be called on the archive file itself.  */
3814
3815void
3816lang_for_each_file (func)
3817     void (*func) PARAMS ((lang_input_statement_type *));
3818{
3819  LANG_FOR_EACH_INPUT_STATEMENT (f)
3820    {
3821      func (f);
3822    }
3823}
3824
3825#if 0
3826
3827/* Not used.  */
3828
3829void
3830lang_for_each_input_section (func)
3831     void (*func) PARAMS ((bfd *ab, asection *as));
3832{
3833  LANG_FOR_EACH_INPUT_STATEMENT (f)
3834    {
3835      asection *s;
3836
3837      for (s = f->the_bfd->sections;
3838	   s != (asection *) NULL;
3839	   s = s->next)
3840	{
3841	  func (f->the_bfd, s);
3842	}
3843    }
3844}
3845
3846#endif
3847
3848void
3849ldlang_add_file (entry)
3850     lang_input_statement_type *entry;
3851{
3852  bfd **pp;
3853
3854  lang_statement_append (&file_chain,
3855			 (lang_statement_union_type *) entry,
3856			 &entry->next);
3857
3858  /* The BFD linker needs to have a list of all input BFDs involved in
3859     a link.  */
3860  ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3861  ASSERT (entry->the_bfd != output_bfd);
3862  for (pp = &link_info.input_bfds;
3863       *pp != (bfd *) NULL;
3864       pp = &(*pp)->link_next)
3865    ;
3866  *pp = entry->the_bfd;
3867  entry->the_bfd->usrdata = (PTR) entry;
3868  bfd_set_gp_size (entry->the_bfd, g_switch_value);
3869
3870  /* Look through the sections and check for any which should not be
3871     included in the link.  We need to do this now, so that we can
3872     notice when the backend linker tries to report multiple
3873     definition errors for symbols which are in sections we aren't
3874     going to link.  FIXME: It might be better to entirely ignore
3875     symbols which are defined in sections which are going to be
3876     discarded.  This would require modifying the backend linker for
3877     each backend which might set the SEC_LINK_ONCE flag.  If we do
3878     this, we should probably handle SEC_EXCLUDE in the same way.  */
3879
3880  bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3881}
3882
3883void
3884lang_add_output (name, from_script)
3885     const char *name;
3886     int from_script;
3887{
3888  /* Make -o on command line override OUTPUT in script.  */
3889  if (had_output_filename == false || !from_script)
3890    {
3891      output_filename = name;
3892      had_output_filename = true;
3893    }
3894}
3895
3896static lang_output_section_statement_type *current_section;
3897
3898static int
3899topower (x)
3900     int x;
3901{
3902  unsigned int i = 1;
3903  int l;
3904
3905  if (x < 0)
3906    return -1;
3907
3908  for (l = 0; l < 32; l++)
3909    {
3910      if (i >= (unsigned int) x)
3911	return l;
3912      i <<= 1;
3913    }
3914
3915  return 0;
3916}
3917
3918lang_output_section_statement_type *
3919lang_enter_output_section_statement (output_section_statement_name,
3920				     address_exp, sectype, block_value,
3921				     align, subalign, ebase)
3922     const char *output_section_statement_name;
3923     etree_type *address_exp;
3924     enum section_type sectype;
3925     bfd_vma block_value;
3926     etree_type *align;
3927     etree_type *subalign;
3928     etree_type *ebase;
3929{
3930  lang_output_section_statement_type *os;
3931
3932  current_section =
3933   os =
3934    lang_output_section_statement_lookup (output_section_statement_name);
3935
3936  /* Add this statement to tree.  */
3937#if 0
3938  add_statement (lang_output_section_statement_enum,
3939		 output_section_statement);
3940#endif
3941  /* Make next things chain into subchain of this.  */
3942
3943  if (os->addr_tree == (etree_type *) NULL)
3944    {
3945      os->addr_tree = address_exp;
3946    }
3947  os->sectype = sectype;
3948  if (sectype != noload_section)
3949    os->flags = SEC_NO_FLAGS;
3950  else
3951    os->flags = SEC_NEVER_LOAD;
3952  os->block_value = block_value ? block_value : 1;
3953  stat_ptr = &os->children;
3954
3955  os->subsection_alignment =
3956    topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
3957  os->section_alignment =
3958    topower (exp_get_value_int (align, -1, "section alignment", 0));
3959
3960  os->load_base = ebase;
3961  return os;
3962}
3963
3964void
3965lang_final ()
3966{
3967  lang_output_statement_type *new =
3968    new_stat (lang_output_statement, stat_ptr);
3969
3970  new->name = output_filename;
3971}
3972
3973/* Reset the current counters in the regions.  */
3974
3975void
3976lang_reset_memory_regions ()
3977{
3978  lang_memory_region_type *p = lang_memory_region_list;
3979  asection *o;
3980
3981  for (p = lang_memory_region_list;
3982       p != (lang_memory_region_type *) NULL;
3983       p = p->next)
3984    {
3985      p->old_length = (bfd_size_type) (p->current - p->origin);
3986      p->current = p->origin;
3987    }
3988
3989  for (o = output_bfd->sections; o != NULL; o = o->next)
3990    o->_raw_size = 0;
3991}
3992
3993/* If the wild pattern was marked KEEP, the member sections
3994   should be as well.  */
3995
3996static void
3997gc_section_callback (ptr, sec, section, file, data)
3998     lang_wild_statement_type *ptr;
3999     struct wildcard_list *sec ATTRIBUTE_UNUSED;
4000     asection *section;
4001     lang_input_statement_type *file ATTRIBUTE_UNUSED;
4002     PTR data ATTRIBUTE_UNUSED;
4003{
4004  if (ptr->keep_sections)
4005    section->flags |= SEC_KEEP;
4006}
4007
4008/* Handle a wild statement, marking it against GC.  */
4009
4010static void
4011lang_gc_wild (s)
4012     lang_wild_statement_type *s;
4013{
4014  walk_wild (s, gc_section_callback, NULL);
4015}
4016
4017/* Iterate over sections marking them against GC.  */
4018
4019static void
4020lang_gc_sections_1 (s)
4021     lang_statement_union_type *s;
4022{
4023  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
4024    {
4025      switch (s->header.type)
4026	{
4027	case lang_wild_statement_enum:
4028	  lang_gc_wild (&s->wild_statement);
4029	  break;
4030	case lang_constructors_statement_enum:
4031	  lang_gc_sections_1 (constructor_list.head);
4032	  break;
4033	case lang_output_section_statement_enum:
4034	  lang_gc_sections_1 (s->output_section_statement.children.head);
4035	  break;
4036	case lang_group_statement_enum:
4037	  lang_gc_sections_1 (s->group_statement.children.head);
4038	  break;
4039	default:
4040	  break;
4041	}
4042    }
4043}
4044
4045static void
4046lang_gc_sections ()
4047{
4048  struct bfd_link_hash_entry *h;
4049  ldlang_undef_chain_list_type *ulist, fake_list_start;
4050
4051  /* Keep all sections so marked in the link script.  */
4052
4053  lang_gc_sections_1 (statement_list.head);
4054
4055  /* Keep all sections containing symbols undefined on the command-line.
4056     Handle the entry symbol at the same time.  */
4057
4058  if (entry_symbol != NULL)
4059    {
4060      fake_list_start.next = ldlang_undef_chain_list_head;
4061      fake_list_start.name = (char *) entry_symbol;
4062      ulist = &fake_list_start;
4063    }
4064  else
4065    ulist = ldlang_undef_chain_list_head;
4066
4067  for (; ulist; ulist = ulist->next)
4068    {
4069      h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4070				false, false, false);
4071
4072      if (h != (struct bfd_link_hash_entry *) NULL
4073	  && (h->type == bfd_link_hash_defined
4074	      || h->type == bfd_link_hash_defweak)
4075	  && ! bfd_is_abs_section (h->u.def.section))
4076	{
4077	  h->u.def.section->flags |= SEC_KEEP;
4078	}
4079    }
4080
4081  bfd_gc_sections (output_bfd, &link_info);
4082}
4083
4084void
4085lang_process ()
4086{
4087  lang_reasonable_defaults ();
4088  current_target = default_target;
4089
4090  /* Open the output file.  */
4091  lang_for_each_statement (ldlang_open_output);
4092
4093  ldemul_create_output_section_statements ();
4094
4095  /* Add to the hash table all undefineds on the command line.  */
4096  lang_place_undefineds ();
4097
4098  already_linked_table_init ();
4099
4100  /* Create a bfd for each input file.  */
4101  current_target = default_target;
4102  open_input_bfds (statement_list.head, false);
4103
4104  ldemul_after_open ();
4105
4106  already_linked_table_free ();
4107
4108  /* Make sure that we're not mixing architectures.  We call this
4109     after all the input files have been opened, but before we do any
4110     other processing, so that any operations merge_private_bfd_data
4111     does on the output file will be known during the rest of the
4112     link.  */
4113  lang_check ();
4114
4115  /* Handle .exports instead of a version script if we're told to do so.  */
4116  if (command_line.version_exports_section)
4117    lang_do_version_exports_section ();
4118
4119  /* Build all sets based on the information gathered from the input
4120     files.  */
4121  ldctor_build_sets ();
4122
4123  /* Remove unreferenced sections if asked to.  */
4124  if (command_line.gc_sections)
4125    lang_gc_sections ();
4126
4127  /* If there were any SEC_MERGE sections, finish their merging, so that
4128     section sizes can be computed.  This has to be done after GC of sections,
4129     so that GCed sections are not merged, but before assigning output
4130     sections, since removing whole input sections is hard then.  */
4131  bfd_merge_sections (output_bfd, &link_info);
4132
4133  /* Size up the common data.  */
4134  lang_common ();
4135
4136  /* Run through the contours of the script and attach input sections
4137     to the correct output sections.  */
4138  map_input_to_output_sections (statement_list.head, (char *) NULL,
4139				(lang_output_section_statement_type *) NULL);
4140
4141  /* Find any sections not attached explicitly and handle them.  */
4142  lang_place_orphans ();
4143
4144  ldemul_before_allocation ();
4145
4146  /* We must record the program headers before we try to fix the
4147     section positions, since they will affect SIZEOF_HEADERS.  */
4148  lang_record_phdrs ();
4149
4150  /* Size up the sections.  */
4151  lang_size_sections (statement_list.head,
4152		      abs_output_section,
4153		      &statement_list.head, 0, (bfd_vma) 0, NULL);
4154
4155  /* Now run around and relax if we can.  */
4156  if (command_line.relax)
4157    {
4158      /* Keep relaxing until bfd_relax_section gives up.  */
4159      boolean relax_again;
4160
4161      do
4162	{
4163	  lang_reset_memory_regions ();
4164
4165	  relax_again = false;
4166
4167	  /* Note: pe-dll.c does something like this also.  If you find
4168	     you need to change this code, you probably need to change
4169	     pe-dll.c also.  DJ  */
4170
4171	  /* Do all the assignments with our current guesses as to
4172	     section sizes.  */
4173	  lang_do_assignments (statement_list.head,
4174			       abs_output_section,
4175			       (fill_type) 0, (bfd_vma) 0);
4176
4177	  /* Perform another relax pass - this time we know where the
4178	     globals are, so can make better guess.  */
4179	  lang_size_sections (statement_list.head,
4180			      abs_output_section,
4181			      &(statement_list.head), 0, (bfd_vma) 0,
4182			      &relax_again);
4183	}
4184      while (relax_again);
4185    }
4186
4187  /* See if anything special should be done now we know how big
4188     everything is.  */
4189  ldemul_after_allocation ();
4190
4191  /* Fix any .startof. or .sizeof. symbols.  */
4192  lang_set_startof ();
4193
4194  /* Do all the assignments, now that we know the final resting places
4195     of all the symbols.  */
4196
4197  lang_do_assignments (statement_list.head,
4198		       abs_output_section,
4199		       (fill_type) 0, (bfd_vma) 0);
4200
4201  /* Make sure that the section addresses make sense.  */
4202  if (! link_info.relocateable
4203      && command_line.check_section_addresses)
4204    lang_check_section_addresses ();
4205
4206  /* Final stuffs.  */
4207
4208  ldemul_finish ();
4209  lang_finish ();
4210}
4211
4212/* EXPORTED TO YACC */
4213
4214void
4215lang_add_wild (filespec, section_list, keep_sections)
4216     struct wildcard_spec *filespec;
4217     struct wildcard_list *section_list;
4218     boolean keep_sections;
4219{
4220  struct wildcard_list *curr, *next;
4221  lang_wild_statement_type *new;
4222
4223  /* Reverse the list as the parser puts it back to front.  */
4224  for (curr = section_list, section_list = NULL;
4225       curr != NULL;
4226       section_list = curr, curr = next)
4227    {
4228      if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4229	placed_commons = true;
4230
4231      next = curr->next;
4232      curr->next = section_list;
4233    }
4234
4235  if (filespec != NULL && filespec->name != NULL)
4236    {
4237      if (strcmp (filespec->name, "*") == 0)
4238	filespec->name = NULL;
4239      else if (! wildcardp (filespec->name))
4240	lang_has_input_file = true;
4241    }
4242
4243  new = new_stat (lang_wild_statement, stat_ptr);
4244  new->filename = NULL;
4245  new->filenames_sorted = false;
4246  if (filespec != NULL)
4247    {
4248      new->filename = filespec->name;
4249      new->filenames_sorted = filespec->sorted;
4250    }
4251  new->section_list = section_list;
4252  new->keep_sections = keep_sections;
4253  lang_list_init (&new->children);
4254}
4255
4256void
4257lang_section_start (name, address)
4258     const char *name;
4259     etree_type *address;
4260{
4261  lang_address_statement_type *ad;
4262
4263  ad = new_stat (lang_address_statement, stat_ptr);
4264  ad->section_name = name;
4265  ad->address = address;
4266}
4267
4268/* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
4269   because of a -e argument on the command line, or zero if this is
4270   called by ENTRY in a linker script.  Command line arguments take
4271   precedence.  */
4272
4273void
4274lang_add_entry (name, cmdline)
4275     const char *name;
4276     boolean cmdline;
4277{
4278  if (entry_symbol == NULL
4279      || cmdline
4280      || ! entry_from_cmdline)
4281    {
4282      entry_symbol = name;
4283      entry_from_cmdline = cmdline;
4284    }
4285}
4286
4287void
4288lang_add_target (name)
4289     const char *name;
4290{
4291  lang_target_statement_type *new = new_stat (lang_target_statement,
4292					      stat_ptr);
4293
4294  new->target = name;
4295
4296}
4297
4298void
4299lang_add_map (name)
4300     const char *name;
4301{
4302  while (*name)
4303    {
4304      switch (*name)
4305	{
4306	case 'F':
4307	  map_option_f = true;
4308	  break;
4309	}
4310      name++;
4311    }
4312}
4313
4314void
4315lang_add_fill (exp)
4316     int exp;
4317{
4318  lang_fill_statement_type *new = new_stat (lang_fill_statement,
4319					    stat_ptr);
4320
4321  new->fill = exp;
4322}
4323
4324void
4325lang_add_data (type, exp)
4326     int type;
4327     union etree_union *exp;
4328{
4329
4330  lang_data_statement_type *new = new_stat (lang_data_statement,
4331					    stat_ptr);
4332
4333  new->exp = exp;
4334  new->type = type;
4335
4336}
4337
4338/* Create a new reloc statement.  RELOC is the BFD relocation type to
4339   generate.  HOWTO is the corresponding howto structure (we could
4340   look this up, but the caller has already done so).  SECTION is the
4341   section to generate a reloc against, or NAME is the name of the
4342   symbol to generate a reloc against.  Exactly one of SECTION and
4343   NAME must be NULL.  ADDEND is an expression for the addend.  */
4344
4345void
4346lang_add_reloc (reloc, howto, section, name, addend)
4347     bfd_reloc_code_real_type reloc;
4348     reloc_howto_type *howto;
4349     asection *section;
4350     const char *name;
4351     union etree_union *addend;
4352{
4353  lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4354
4355  p->reloc = reloc;
4356  p->howto = howto;
4357  p->section = section;
4358  p->name = name;
4359  p->addend_exp = addend;
4360
4361  p->addend_value = 0;
4362  p->output_section = NULL;
4363  p->output_vma = 0;
4364}
4365
4366lang_assignment_statement_type *
4367lang_add_assignment (exp)
4368     etree_type *exp;
4369{
4370  lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4371						  stat_ptr);
4372
4373  new->exp = exp;
4374  return new;
4375}
4376
4377void
4378lang_add_attribute (attribute)
4379     enum statement_enum attribute;
4380{
4381  new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4382}
4383
4384void
4385lang_startup (name)
4386     const char *name;
4387{
4388  if (startup_file != (char *) NULL)
4389    {
4390      einfo (_("%P%Fmultiple STARTUP files\n"));
4391    }
4392  first_file->filename = name;
4393  first_file->local_sym_name = name;
4394  first_file->real = true;
4395
4396  startup_file = name;
4397}
4398
4399void
4400lang_float (maybe)
4401     boolean maybe;
4402{
4403  lang_float_flag = maybe;
4404}
4405
4406void
4407lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
4408     bfd_vma fill;
4409     const char *memspec;
4410     struct lang_output_section_phdr_list *phdrs;
4411     const char *lma_memspec;
4412{
4413  current_section->fill = fill;
4414  current_section->region = lang_memory_region_lookup (memspec);
4415  if (strcmp (lma_memspec, "*default*") != 0)
4416    {
4417      current_section->lma_region = lang_memory_region_lookup (lma_memspec);
4418      /* If no runtime region has been given, but the load region has
4419         been, use the load region.  */
4420      if (strcmp (memspec, "*default*") == 0)
4421        current_section->region = lang_memory_region_lookup (lma_memspec);
4422    }
4423  current_section->phdrs = phdrs;
4424  stat_ptr = &statement_list;
4425}
4426
4427/* Create an absolute symbol with the given name with the value of the
4428   address of first byte of the section named.
4429
4430   If the symbol already exists, then do nothing.  */
4431
4432void
4433lang_abs_symbol_at_beginning_of (secname, name)
4434     const char *secname;
4435     const char *name;
4436{
4437  struct bfd_link_hash_entry *h;
4438
4439  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4440  if (h == (struct bfd_link_hash_entry *) NULL)
4441    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4442
4443  if (h->type == bfd_link_hash_new
4444      || h->type == bfd_link_hash_undefined)
4445    {
4446      asection *sec;
4447
4448      h->type = bfd_link_hash_defined;
4449
4450      sec = bfd_get_section_by_name (output_bfd, secname);
4451      if (sec == (asection *) NULL)
4452	h->u.def.value = 0;
4453      else
4454	h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4455
4456      h->u.def.section = bfd_abs_section_ptr;
4457    }
4458}
4459
4460/* Create an absolute symbol with the given name with the value of the
4461   address of the first byte after the end of the section named.
4462
4463   If the symbol already exists, then do nothing.  */
4464
4465void
4466lang_abs_symbol_at_end_of (secname, name)
4467     const char *secname;
4468     const char *name;
4469{
4470  struct bfd_link_hash_entry *h;
4471
4472  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4473  if (h == (struct bfd_link_hash_entry *) NULL)
4474    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4475
4476  if (h->type == bfd_link_hash_new
4477      || h->type == bfd_link_hash_undefined)
4478    {
4479      asection *sec;
4480
4481      h->type = bfd_link_hash_defined;
4482
4483      sec = bfd_get_section_by_name (output_bfd, secname);
4484      if (sec == (asection *) NULL)
4485	h->u.def.value = 0;
4486      else
4487	h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4488			  + bfd_section_size (output_bfd, sec) /
4489                          bfd_octets_per_byte (output_bfd));
4490
4491      h->u.def.section = bfd_abs_section_ptr;
4492    }
4493}
4494
4495void
4496lang_statement_append (list, element, field)
4497     lang_statement_list_type *list;
4498     lang_statement_union_type *element;
4499     lang_statement_union_type **field;
4500{
4501  *(list->tail) = element;
4502  list->tail = field;
4503}
4504
4505/* Set the output format type.  -oformat overrides scripts.  */
4506
4507void
4508lang_add_output_format (format, big, little, from_script)
4509     const char *format;
4510     const char *big;
4511     const char *little;
4512     int from_script;
4513{
4514  if (output_target == NULL || !from_script)
4515    {
4516      if (command_line.endian == ENDIAN_BIG
4517	  && big != NULL)
4518	format = big;
4519      else if (command_line.endian == ENDIAN_LITTLE
4520	       && little != NULL)
4521	format = little;
4522
4523      output_target = format;
4524    }
4525}
4526
4527/* Enter a group.  This creates a new lang_group_statement, and sets
4528   stat_ptr to build new statements within the group.  */
4529
4530void
4531lang_enter_group ()
4532{
4533  lang_group_statement_type *g;
4534
4535  g = new_stat (lang_group_statement, stat_ptr);
4536  lang_list_init (&g->children);
4537  stat_ptr = &g->children;
4538}
4539
4540/* Leave a group.  This just resets stat_ptr to start writing to the
4541   regular list of statements again.  Note that this will not work if
4542   groups can occur inside anything else which can adjust stat_ptr,
4543   but currently they can't.  */
4544
4545void
4546lang_leave_group ()
4547{
4548  stat_ptr = &statement_list;
4549}
4550
4551/* Add a new program header.  This is called for each entry in a PHDRS
4552   command in a linker script.  */
4553
4554void
4555lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4556     const char *name;
4557     etree_type *type;
4558     boolean filehdr;
4559     boolean phdrs;
4560     etree_type *at;
4561     etree_type *flags;
4562{
4563  struct lang_phdr *n, **pp;
4564
4565  n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4566  n->next = NULL;
4567  n->name = name;
4568  n->type = exp_get_value_int (type, 0, "program header type",
4569			       lang_final_phase_enum);
4570  n->filehdr = filehdr;
4571  n->phdrs = phdrs;
4572  n->at = at;
4573  n->flags = flags;
4574
4575  for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4576    ;
4577  *pp = n;
4578}
4579
4580/* Record the program header information in the output BFD.  FIXME: We
4581   should not be calling an ELF specific function here.  */
4582
4583static void
4584lang_record_phdrs ()
4585{
4586  unsigned int alc;
4587  asection **secs;
4588  struct lang_output_section_phdr_list *last;
4589  struct lang_phdr *l;
4590  lang_statement_union_type *u;
4591
4592  alc = 10;
4593  secs = (asection **) xmalloc (alc * sizeof (asection *));
4594  last = NULL;
4595  for (l = lang_phdr_list; l != NULL; l = l->next)
4596    {
4597      unsigned int c;
4598      flagword flags;
4599      bfd_vma at;
4600
4601      c = 0;
4602      for (u = lang_output_section_statement.head;
4603	   u != NULL;
4604	   u = u->output_section_statement.next)
4605	{
4606	  lang_output_section_statement_type *os;
4607	  struct lang_output_section_phdr_list *pl;
4608
4609	  os = &u->output_section_statement;
4610
4611	  pl = os->phdrs;
4612	  if (pl != NULL)
4613	    last = pl;
4614	  else
4615	    {
4616	      if (os->sectype == noload_section
4617		  || os->bfd_section == NULL
4618		  || (os->bfd_section->flags & SEC_ALLOC) == 0)
4619		continue;
4620	      pl = last;
4621	    }
4622
4623	  if (os->bfd_section == NULL)
4624	    continue;
4625
4626	  for (; pl != NULL; pl = pl->next)
4627	    {
4628	      if (strcmp (pl->name, l->name) == 0)
4629		{
4630		  if (c >= alc)
4631		    {
4632		      alc *= 2;
4633		      secs = ((asection **)
4634			      xrealloc (secs, alc * sizeof (asection *)));
4635		    }
4636		  secs[c] = os->bfd_section;
4637		  ++c;
4638		  pl->used = true;
4639		}
4640	    }
4641	}
4642
4643      if (l->flags == NULL)
4644	flags = 0;
4645      else
4646	flags = exp_get_vma (l->flags, 0, "phdr flags",
4647			     lang_final_phase_enum);
4648
4649      if (l->at == NULL)
4650	at = 0;
4651      else
4652	at = exp_get_vma (l->at, 0, "phdr load address",
4653			  lang_final_phase_enum);
4654
4655      if (! bfd_record_phdr (output_bfd, l->type,
4656			     l->flags != NULL, flags, l->at != NULL,
4657			     at, l->filehdr, l->phdrs, c, secs))
4658	einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4659    }
4660
4661  free (secs);
4662
4663  /* Make sure all the phdr assignments succeeded.  */
4664  for (u = lang_output_section_statement.head;
4665       u != NULL;
4666       u = u->output_section_statement.next)
4667    {
4668      struct lang_output_section_phdr_list *pl;
4669
4670      if (u->output_section_statement.bfd_section == NULL)
4671	continue;
4672
4673      for (pl = u->output_section_statement.phdrs;
4674	   pl != NULL;
4675	   pl = pl->next)
4676	if (! pl->used && strcmp (pl->name, "NONE") != 0)
4677	  einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4678		 u->output_section_statement.name, pl->name);
4679    }
4680}
4681
4682/* Record a list of sections which may not be cross referenced.  */
4683
4684void
4685lang_add_nocrossref (l)
4686     struct lang_nocrossref *l;
4687{
4688  struct lang_nocrossrefs *n;
4689
4690  n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4691  n->next = nocrossref_list;
4692  n->list = l;
4693  nocrossref_list = n;
4694
4695  /* Set notice_all so that we get informed about all symbols.  */
4696  link_info.notice_all = true;
4697}
4698
4699/* Overlay handling.  We handle overlays with some static variables.  */
4700
4701/* The overlay virtual address.  */
4702static etree_type *overlay_vma;
4703
4704/* The overlay load address.  */
4705static etree_type *overlay_lma;
4706
4707/* Whether nocrossrefs is set for this overlay.  */
4708static int overlay_nocrossrefs;
4709
4710/* An expression for the maximum section size seen so far.  */
4711static etree_type *overlay_max;
4712
4713/* A list of all the sections in this overlay.  */
4714
4715struct overlay_list {
4716  struct overlay_list *next;
4717  lang_output_section_statement_type *os;
4718};
4719
4720static struct overlay_list *overlay_list;
4721
4722/* Start handling an overlay.  */
4723
4724void
4725lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4726     etree_type *vma_expr;
4727     etree_type *lma_expr;
4728     int nocrossrefs;
4729{
4730  /* The grammar should prevent nested overlays from occurring.  */
4731  ASSERT (overlay_vma == NULL
4732	  && overlay_lma == NULL
4733	  && overlay_list == NULL
4734	  && overlay_max == NULL);
4735
4736  overlay_vma = vma_expr;
4737  overlay_lma = lma_expr;
4738  overlay_nocrossrefs = nocrossrefs;
4739}
4740
4741/* Start a section in an overlay.  We handle this by calling
4742   lang_enter_output_section_statement with the correct VMA and LMA.  */
4743
4744void
4745lang_enter_overlay_section (name)
4746     const char *name;
4747{
4748  struct overlay_list *n;
4749  etree_type *size;
4750
4751  lang_enter_output_section_statement (name, overlay_vma, normal_section,
4752				       0, 0, 0, overlay_lma);
4753
4754  /* If this is the first section, then base the VMA and LMA of future
4755     sections on this one.  This will work correctly even if `.' is
4756     used in the addresses.  */
4757  if (overlay_list == NULL)
4758    {
4759      overlay_vma = exp_nameop (ADDR, name);
4760      overlay_lma = exp_nameop (LOADADDR, name);
4761    }
4762
4763  /* Remember the section.  */
4764  n = (struct overlay_list *) xmalloc (sizeof *n);
4765  n->os = current_section;
4766  n->next = overlay_list;
4767  overlay_list = n;
4768
4769  size = exp_nameop (SIZEOF, name);
4770
4771  /* Adjust the LMA for the next section.  */
4772  overlay_lma = exp_binop ('+', overlay_lma, size);
4773
4774  /* Arrange to work out the maximum section end address.  */
4775  if (overlay_max == NULL)
4776    overlay_max = size;
4777  else
4778    overlay_max = exp_binop (MAX_K, overlay_max, size);
4779}
4780
4781/* Finish a section in an overlay.  There isn't any special to do
4782   here.  */
4783
4784void
4785lang_leave_overlay_section (fill, phdrs)
4786     bfd_vma fill;
4787     struct lang_output_section_phdr_list *phdrs;
4788{
4789  const char *name;
4790  char *clean, *s2;
4791  const char *s1;
4792  char *buf;
4793
4794  name = current_section->name;
4795
4796  lang_leave_output_section_statement (fill, "*default*",
4797                                       phdrs, "*default*");
4798
4799  /* Define the magic symbols.  */
4800
4801  clean = xmalloc (strlen (name) + 1);
4802  s2 = clean;
4803  for (s1 = name; *s1 != '\0'; s1++)
4804    if (ISALNUM (*s1) || *s1 == '_')
4805      *s2++ = *s1;
4806  *s2 = '\0';
4807
4808  buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4809  sprintf (buf, "__load_start_%s", clean);
4810  lang_add_assignment (exp_assop ('=', buf,
4811				  exp_nameop (LOADADDR, name)));
4812
4813  buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4814  sprintf (buf, "__load_stop_%s", clean);
4815  lang_add_assignment (exp_assop ('=', buf,
4816				  exp_binop ('+',
4817					     exp_nameop (LOADADDR, name),
4818					     exp_nameop (SIZEOF, name))));
4819
4820  free (clean);
4821}
4822
4823/* Finish an overlay.  If there are any overlay wide settings, this
4824   looks through all the sections in the overlay and sets them.  */
4825
4826void
4827lang_leave_overlay (fill, memspec, phdrs, lma_memspec)
4828     bfd_vma fill;
4829     const char *memspec;
4830     struct lang_output_section_phdr_list *phdrs;
4831     const char *lma_memspec;
4832{
4833  lang_memory_region_type *region;
4834  lang_memory_region_type * default_region;
4835  lang_memory_region_type *lma_region;
4836  struct overlay_list *l;
4837  struct lang_nocrossref *nocrossref;
4838
4839  default_region = lang_memory_region_lookup ("*default*");
4840
4841  if (memspec == NULL)
4842    region = NULL;
4843  else
4844    region = lang_memory_region_lookup (memspec);
4845
4846  if (lma_memspec == NULL)
4847    lma_region = NULL;
4848  else
4849    lma_region = lang_memory_region_lookup (lma_memspec);
4850
4851  nocrossref = NULL;
4852
4853  l = overlay_list;
4854  while (l != NULL)
4855    {
4856      struct overlay_list *next;
4857
4858      if (fill != 0 && l->os->fill == 0)
4859	l->os->fill = fill;
4860
4861      /* Assign a region to the sections, if one has been specified.
4862	 Override the assignment of the default section, but not
4863	 other sections.  */
4864      if (region != NULL &&
4865	  (l->os->region == NULL ||
4866	   l->os->region == default_region))
4867	l->os->region = region;
4868
4869      /* We only set lma_region for the first overlay section, as
4870	 subsequent overlay sections will have load_base set relative
4871	 to the first section.  Also, don't set lma_region if
4872	 load_base is specified.  FIXME:  There should really be a test
4873	 that `AT ( LDADDR )' doesn't conflict with `AT >LMA_REGION'
4874	 rather than letting LDADDR simply override LMA_REGION.  */
4875      if (lma_region != NULL && l->os->lma_region == NULL
4876	  && l->next == NULL && l->os->load_base == NULL)
4877	l->os->lma_region = lma_region;
4878
4879      if (phdrs != NULL && l->os->phdrs == NULL)
4880	l->os->phdrs = phdrs;
4881
4882      if (overlay_nocrossrefs)
4883	{
4884	  struct lang_nocrossref *nc;
4885
4886	  nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4887	  nc->name = l->os->name;
4888	  nc->next = nocrossref;
4889	  nocrossref = nc;
4890	}
4891
4892      next = l->next;
4893      free (l);
4894      l = next;
4895    }
4896
4897  if (nocrossref != NULL)
4898    lang_add_nocrossref (nocrossref);
4899
4900  /* Update . for the end of the overlay.  */
4901  lang_add_assignment (exp_assop ('=', ".",
4902				  exp_binop ('+', overlay_vma, overlay_max)));
4903
4904  overlay_vma = NULL;
4905  overlay_lma = NULL;
4906  overlay_nocrossrefs = 0;
4907  overlay_list = NULL;
4908  overlay_max = NULL;
4909}
4910
4911/* Version handling.  This is only useful for ELF.  */
4912
4913/* This global variable holds the version tree that we build.  */
4914
4915struct bfd_elf_version_tree *lang_elf_version_info;
4916
4917static int
4918lang_vers_match_lang_c (expr, sym)
4919     struct bfd_elf_version_expr *expr;
4920     const char *sym;
4921{
4922  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4923    return 1;
4924  return fnmatch (expr->pattern, sym, 0) == 0;
4925}
4926
4927static int
4928lang_vers_match_lang_cplusplus (expr, sym)
4929     struct bfd_elf_version_expr *expr;
4930     const char *sym;
4931{
4932  char *alt_sym;
4933  int result;
4934
4935  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4936    return 1;
4937
4938  alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
4939  if (!alt_sym)
4940    {
4941      /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4942	 Should we early out false in this case?  */
4943      result = fnmatch (expr->pattern, sym, 0) == 0;
4944    }
4945  else
4946    {
4947      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4948      free (alt_sym);
4949    }
4950
4951  return result;
4952}
4953
4954static int
4955lang_vers_match_lang_java (expr, sym)
4956     struct bfd_elf_version_expr *expr;
4957     const char *sym;
4958{
4959  char *alt_sym;
4960  int result;
4961
4962  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4963    return 1;
4964
4965  alt_sym = cplus_demangle (sym, DMGL_JAVA);
4966  if (!alt_sym)
4967    {
4968      /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4969	 Should we early out false in this case?  */
4970      result = fnmatch (expr->pattern, sym, 0) == 0;
4971    }
4972  else
4973    {
4974      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4975      free (alt_sym);
4976    }
4977
4978  return result;
4979}
4980
4981/* This is called for each variable name or match expression.  */
4982
4983struct bfd_elf_version_expr *
4984lang_new_vers_pattern (orig, new, lang)
4985     struct bfd_elf_version_expr *orig;
4986     const char *new;
4987     const char *lang;
4988{
4989  struct bfd_elf_version_expr *ret;
4990
4991  ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4992  ret->next = orig;
4993  ret->pattern = new;
4994
4995  if (lang == NULL || strcasecmp (lang, "C") == 0)
4996    ret->match = lang_vers_match_lang_c;
4997  else if (strcasecmp (lang, "C++") == 0)
4998    ret->match = lang_vers_match_lang_cplusplus;
4999  else if (strcasecmp (lang, "Java") == 0)
5000    ret->match = lang_vers_match_lang_java;
5001  else
5002    {
5003      einfo (_("%X%P: unknown language `%s' in version information\n"),
5004	     lang);
5005      ret->match = lang_vers_match_lang_c;
5006    }
5007
5008  return ret;
5009}
5010
5011/* This is called for each set of variable names and match
5012   expressions.  */
5013
5014struct bfd_elf_version_tree *
5015lang_new_vers_node (globals, locals)
5016     struct bfd_elf_version_expr *globals;
5017     struct bfd_elf_version_expr *locals;
5018{
5019  struct bfd_elf_version_tree *ret;
5020
5021  ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
5022  ret->next = NULL;
5023  ret->name = NULL;
5024  ret->vernum = 0;
5025  ret->globals = globals;
5026  ret->locals = locals;
5027  ret->deps = NULL;
5028  ret->name_indx = (unsigned int) -1;
5029  ret->used = 0;
5030  return ret;
5031}
5032
5033/* This static variable keeps track of version indices.  */
5034
5035static int version_index;
5036
5037/* This is called when we know the name and dependencies of the
5038   version.  */
5039
5040void
5041lang_register_vers_node (name, version, deps)
5042     const char *name;
5043     struct bfd_elf_version_tree *version;
5044     struct bfd_elf_version_deps *deps;
5045{
5046  struct bfd_elf_version_tree *t, **pp;
5047  struct bfd_elf_version_expr *e1;
5048
5049  if (name == NULL)
5050    name = "";
5051
5052  if ((name[0] == '\0' && lang_elf_version_info != NULL)
5053      || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5054    {
5055      einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5056      return;
5057    }
5058
5059  /* Make sure this node has a unique name.  */
5060  for (t = lang_elf_version_info; t != NULL; t = t->next)
5061    if (strcmp (t->name, name) == 0)
5062      einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5063
5064  /* Check the global and local match names, and make sure there
5065     aren't any duplicates.  */
5066
5067  for (e1 = version->globals; e1 != NULL; e1 = e1->next)
5068    {
5069      for (t = lang_elf_version_info; t != NULL; t = t->next)
5070	{
5071	  struct bfd_elf_version_expr *e2;
5072
5073	  for (e2 = t->locals; e2 != NULL; e2 = e2->next)
5074	    if (strcmp (e1->pattern, e2->pattern) == 0)
5075	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5076		     e1->pattern);
5077	}
5078    }
5079
5080  for (e1 = version->locals; e1 != NULL; e1 = e1->next)
5081    {
5082      for (t = lang_elf_version_info; t != NULL; t = t->next)
5083	{
5084	  struct bfd_elf_version_expr *e2;
5085
5086	  for (e2 = t->globals; e2 != NULL; e2 = e2->next)
5087	    if (strcmp (e1->pattern, e2->pattern) == 0)
5088	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5089		     e1->pattern);
5090	}
5091    }
5092
5093  version->deps = deps;
5094  version->name = name;
5095  if (name[0] != '\0')
5096    {
5097      ++version_index;
5098      version->vernum = version_index;
5099    }
5100  else
5101    version->vernum = 0;
5102
5103  for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5104    ;
5105  *pp = version;
5106}
5107
5108/* This is called when we see a version dependency.  */
5109
5110struct bfd_elf_version_deps *
5111lang_add_vers_depend (list, name)
5112     struct bfd_elf_version_deps *list;
5113     const char *name;
5114{
5115  struct bfd_elf_version_deps *ret;
5116  struct bfd_elf_version_tree *t;
5117
5118  ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
5119  ret->next = list;
5120
5121  for (t = lang_elf_version_info; t != NULL; t = t->next)
5122    {
5123      if (strcmp (t->name, name) == 0)
5124	{
5125	  ret->version_needed = t;
5126	  return ret;
5127	}
5128    }
5129
5130  einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5131
5132  return ret;
5133}
5134
5135static void
5136lang_do_version_exports_section ()
5137{
5138  struct bfd_elf_version_expr *greg = NULL, *lreg;
5139
5140  LANG_FOR_EACH_INPUT_STATEMENT (is)
5141    {
5142      asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5143      char *contents, *p;
5144      bfd_size_type len;
5145
5146      if (sec == NULL)
5147        continue;
5148
5149      len = bfd_section_size (is->the_bfd, sec);
5150      contents = xmalloc (len);
5151      if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5152	einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5153
5154      p = contents;
5155      while (p < contents + len)
5156	{
5157	  greg = lang_new_vers_pattern (greg, p, NULL);
5158	  p = strchr (p, '\0') + 1;
5159	}
5160
5161      /* Do not free the contents, as we used them creating the regex.  */
5162
5163      /* Do not include this section in the link.  */
5164      bfd_set_section_flags (is->the_bfd, sec,
5165	bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5166    }
5167
5168  lreg = lang_new_vers_pattern (NULL, "*", NULL);
5169  lang_register_vers_node (command_line.version_exports_section,
5170			   lang_new_vers_node (greg, lreg), NULL);
5171}
5172
5173void
5174lang_add_unique (name)
5175     const char *name;
5176{
5177  struct unique_sections *ent;
5178
5179  for (ent = unique_section_list; ent; ent = ent->next)
5180    if (strcmp (ent->name, name) == 0)
5181      return;
5182
5183  ent = (struct unique_sections *) xmalloc (sizeof *ent);
5184  ent->name = xstrdup (name);
5185  ent->next = unique_section_list;
5186  unique_section_list = ent;
5187}
5188