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