ldlang.c revision 107492
133965Sjdp/* Linker command language support.
278828Sobrien   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
389857Sobrien   2001, 2002
438889Sjdp   Free Software Foundation, Inc.
533965Sjdp
633965SjdpThis file is part of GLD, the Gnu Linker.
733965Sjdp
833965SjdpGLD is free software; you can redistribute it and/or modify
933965Sjdpit under the terms of the GNU General Public License as published by
1060484Sobrienthe Free Software Foundation; either version 2, or (at your option)
1133965Sjdpany later version.
1233965Sjdp
1333965SjdpGLD is distributed in the hope that it will be useful,
1433965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1533965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1633965SjdpGNU General Public License for more details.
1733965Sjdp
1833965SjdpYou should have received a copy of the GNU General Public License
1933965Sjdpalong with GLD; see the file COPYING.  If not, write to the Free
2033965SjdpSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA
2133965Sjdp02111-1307, USA.  */
2233965Sjdp
2333965Sjdp#include "bfd.h"
2433965Sjdp#include "sysdep.h"
2533965Sjdp#include "libiberty.h"
2689857Sobrien#include "safe-ctype.h"
2733965Sjdp#include "obstack.h"
2833965Sjdp#include "bfdlink.h"
2933965Sjdp
3033965Sjdp#include "ld.h"
3133965Sjdp#include "ldmain.h"
3233965Sjdp#include "ldexp.h"
3333965Sjdp#include "ldlang.h"
34107492Sobrien#include <ldgram.h>
3533965Sjdp#include "ldlex.h"
3633965Sjdp#include "ldmisc.h"
3733965Sjdp#include "ldctor.h"
3833965Sjdp#include "ldfile.h"
3977298Sobrien#include "ldemul.h"
4033965Sjdp#include "fnmatch.h"
4160484Sobrien#include "demangle.h"
4233965Sjdp
4333965Sjdp/* FORWARDS */
4478828Sobrienstatic lang_statement_union_type *new_statement
4578828Sobrien  PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
4633965Sjdp
4733965Sjdp/* LOCALS */
4833965Sjdpstatic struct obstack stat_obstack;
4933965Sjdp
5033965Sjdp#define obstack_chunk_alloc xmalloc
5133965Sjdp#define obstack_chunk_free free
5277298Sobrienstatic const char *startup_file;
5333965Sjdpstatic lang_statement_list_type input_file_chain;
5433965Sjdpstatic boolean placed_commons = false;
5533965Sjdpstatic lang_output_section_statement_type *default_common_section;
5633965Sjdpstatic boolean map_option_f;
5733965Sjdpstatic bfd_vma print_dot;
5833965Sjdpstatic lang_input_statement_type *first_file;
5977298Sobrienstatic const char *current_target;
6077298Sobrienstatic const char *output_target;
6133965Sjdpstatic lang_statement_list_type statement_list;
6233965Sjdpstatic struct lang_phdr *lang_phdr_list;
6333965Sjdp
6433965Sjdpstatic void lang_for_each_statement_worker
6577298Sobrien  PARAMS ((void (*) (lang_statement_union_type *),
6677298Sobrien	   lang_statement_union_type *));
6733965Sjdpstatic lang_input_statement_type *new_afile
6877298Sobrien  PARAMS ((const char *, lang_input_file_enum_type, const char *, boolean));
6977298Sobrienstatic lang_memory_region_type *lang_memory_default PARAMS ((asection *));
7077298Sobrienstatic void lang_map_flags PARAMS ((flagword));
7177298Sobrienstatic void init_os PARAMS ((lang_output_section_statement_type *));
7233965Sjdpstatic void exp_init_os PARAMS ((etree_type *));
7333965Sjdpstatic void section_already_linked PARAMS ((bfd *, asection *, PTR));
7460484Sobrienstatic struct bfd_hash_entry *already_linked_newfunc
7577298Sobrien  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
7660484Sobrienstatic void already_linked_table_init PARAMS ((void));
7760484Sobrienstatic void already_linked_table_free PARAMS ((void));
7833965Sjdpstatic boolean wildcardp PARAMS ((const char *));
7960484Sobrienstatic lang_statement_union_type *wild_sort
8089857Sobrien  PARAMS ((lang_wild_statement_type *, struct wildcard_list *,
8189857Sobrien	   lang_input_statement_type *, asection *));
8277298Sobrienstatic void output_section_callback
8389857Sobrien  PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
8477298Sobrien	   lang_input_statement_type *, PTR));
8577298Sobrienstatic lang_input_statement_type *lookup_name PARAMS ((const char *));
8689857Sobrienstatic boolean load_symbols
8777298Sobrien  PARAMS ((lang_input_statement_type *, lang_statement_list_type *));
8877298Sobrienstatic void wild
8989857Sobrien  PARAMS ((lang_wild_statement_type *,
9077298Sobrien	   const char *, lang_output_section_statement_type *));
9177298Sobrienstatic bfd *open_output PARAMS ((const char *));
9277298Sobrienstatic void ldlang_open_output PARAMS ((lang_statement_union_type *));
9377298Sobrienstatic void open_input_bfds PARAMS ((lang_statement_union_type *, boolean));
9433965Sjdpstatic void lang_reasonable_defaults PARAMS ((void));
9594536Sobrienstatic void insert_undefined PARAMS ((const char *));
9633965Sjdpstatic void lang_place_undefineds PARAMS ((void));
9733965Sjdpstatic void map_input_to_output_sections
9877298Sobrien  PARAMS ((lang_statement_union_type *, const char *,
9977298Sobrien	   lang_output_section_statement_type *));
100104834Sobrienstatic void strip_excluded_output_sections PARAMS ((void));
10133965Sjdpstatic void print_output_section_statement
10277298Sobrien  PARAMS ((lang_output_section_statement_type *));
10333965Sjdpstatic void print_assignment
10477298Sobrien  PARAMS ((lang_assignment_statement_type *,
10577298Sobrien	   lang_output_section_statement_type *));
10677298Sobrienstatic void print_input_statement PARAMS ((lang_input_statement_type *));
10733965Sjdpstatic boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
10877298Sobrienstatic void print_input_section PARAMS ((lang_input_section_type *));
10977298Sobrienstatic void print_fill_statement PARAMS ((lang_fill_statement_type *));
11077298Sobrienstatic void print_data_statement PARAMS ((lang_data_statement_type *));
11133965Sjdpstatic void print_address_statement PARAMS ((lang_address_statement_type *));
11277298Sobrienstatic void print_reloc_statement PARAMS ((lang_reloc_statement_type *));
11377298Sobrienstatic void print_padding_statement PARAMS ((lang_padding_statement_type *));
11433965Sjdpstatic void print_wild_statement
11577298Sobrien  PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
11633965Sjdpstatic void print_group
11733965Sjdp  PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
11877298Sobrienstatic void print_statement
11977298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
12077298Sobrienstatic void print_statement_list
12177298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
12233965Sjdpstatic void print_statements PARAMS ((void));
12389857Sobrienstatic void insert_pad
124104834Sobrien  PARAMS ((lang_statement_union_type **, fill_type *,
12577298Sobrien	   unsigned int, asection *, bfd_vma));
12633965Sjdpstatic bfd_vma size_input_section
12777298Sobrien  PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
128104834Sobrien	   fill_type *, bfd_vma));
12933965Sjdpstatic void lang_finish PARAMS ((void));
13038889Sjdpstatic void ignore_bfd_errors PARAMS ((const char *, ...));
13133965Sjdpstatic void lang_check PARAMS ((void));
13233965Sjdpstatic void lang_common PARAMS ((void));
13333965Sjdpstatic boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
13433965Sjdpstatic void lang_place_orphans PARAMS ((void));
13533965Sjdpstatic int topower PARAMS ((int));
13633965Sjdpstatic void lang_set_startof PARAMS ((void));
13777298Sobrienstatic void gc_section_callback
13889857Sobrien  PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
13977298Sobrien	   lang_input_statement_type *, PTR));
140104834Sobrienstatic void lang_get_regions PARAMS ((struct memory_region_struct **,
141104834Sobrien				      struct memory_region_struct **,
142104834Sobrien				      const char *, const char *, int));
14333965Sjdpstatic void lang_record_phdrs PARAMS ((void));
14489857Sobrienstatic void lang_gc_wild PARAMS ((lang_wild_statement_type *));
14560484Sobrienstatic void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
14660484Sobrienstatic void lang_gc_sections PARAMS ((void));
14777298Sobrienstatic int lang_vers_match_lang_c
14877298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14977298Sobrienstatic int lang_vers_match_lang_cplusplus
15077298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
15177298Sobrienstatic int lang_vers_match_lang_java
15277298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
15360484Sobrienstatic void lang_do_version_exports_section PARAMS ((void));
15460484Sobrienstatic void lang_check_section_addresses PARAMS ((void));
15577298Sobrienstatic void os_region_check
15677298Sobrien  PARAMS ((lang_output_section_statement_type *,
15777298Sobrien	   struct memory_region_struct *, etree_type *, bfd_vma));
158104834Sobrienstatic bfd_vma lang_size_sections_1
159104834Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *,
160104834Sobrien	   lang_statement_union_type **, fill_type *, bfd_vma, boolean *));
16133965Sjdp
16260484Sobrientypedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
16389857Sobrien				    struct wildcard_list *,
16489857Sobrien				    asection *,
16589857Sobrien				    lang_input_statement_type *,
16677298Sobrien				    PTR));
16777298Sobrienstatic void walk_wild
16889857Sobrien  PARAMS ((lang_wild_statement_type *, callback_t, PTR));
16960484Sobrienstatic void walk_wild_section
17089857Sobrien  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
17189857Sobrien	   callback_t, PTR));
17260484Sobrienstatic void walk_wild_file
17389857Sobrien  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
17489857Sobrien	   callback_t, PTR));
17560484Sobrien
17677298Sobrienstatic int    get_target PARAMS ((const bfd_target *, PTR));
17760484Sobrienstatic void   stricpy PARAMS ((char *, char *));
17860484Sobrienstatic void   strcut PARAMS ((char *, char *));
17960484Sobrienstatic int    name_compare PARAMS ((char *, char *));
18077298Sobrienstatic int    closest_target_match PARAMS ((const bfd_target *, PTR));
18160484Sobrienstatic char * get_first_input_target PARAMS ((void));
18277298Sobrien
18333965Sjdp/* EXPORTS */
18433965Sjdplang_output_section_statement_type *abs_output_section;
18568765Sobrienlang_statement_list_type lang_output_section_statement;
18633965Sjdplang_statement_list_type *stat_ptr = &statement_list;
18760484Sobrienlang_statement_list_type file_chain = { NULL, NULL };
188104834Sobrienstruct bfd_sym_chain entry_symbol = { NULL, NULL };
18991041Sobrienconst char *entry_section = ".text";
19033965Sjdpboolean entry_from_cmdline;
19133965Sjdpboolean lang_has_input_file = false;
19233965Sjdpboolean had_output_filename = false;
19333965Sjdpboolean lang_float_flag = false;
19433965Sjdpboolean delete_output_file_on_failure = false;
19533965Sjdpstruct lang_nocrossrefs *nocrossref_list;
19677298Sobrienstruct unique_sections *unique_section_list;
19733965Sjdp
19833965Sjdpetree_type *base; /* Relocation base - or null */
19933965Sjdp
20078828Sobrien#if defined (__STDC__) || defined (ALMOST_STDC)
20133965Sjdp#define cat(a,b) a##b
20233965Sjdp#else
20333965Sjdp#define cat(a,b) a/**/b
20433965Sjdp#endif
20533965Sjdp
20678828Sobrien/* Don't beautify the line below with "innocent" whitespace, it breaks
20778828Sobrien   the K&R C preprocessor!  */
20878828Sobrien#define new_stat(x, y) \
20978828Sobrien  (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
21033965Sjdp
21178828Sobrien#define outside_section_address(q) \
21278828Sobrien  ((q)->output_offset + (q)->output_section->vma)
21333965Sjdp
21478828Sobrien#define outside_symbol_address(q) \
21578828Sobrien  ((q)->value + outside_section_address (q->section))
21633965Sjdp
21733965Sjdp#define SECTION_NAME_MAP_LENGTH (16)
21833965Sjdp
21933965SjdpPTR
22033965Sjdpstat_alloc (size)
22133965Sjdp     size_t size;
22233965Sjdp{
22333965Sjdp  return obstack_alloc (&stat_obstack, size);
22433965Sjdp}
22533965Sjdp
22677298Sobrienboolean
22777298Sobrienunique_section_p (secnam)
22877298Sobrien     const char *secnam;
22977298Sobrien{
23077298Sobrien  struct unique_sections *unam;
23160484Sobrien
23277298Sobrien  for (unam = unique_section_list; unam; unam = unam->next)
23377298Sobrien    if (wildcardp (unam->name)
23477298Sobrien	? fnmatch (unam->name, secnam, 0) == 0
23577298Sobrien	: strcmp (unam->name, secnam) == 0)
23677298Sobrien      {
23777298Sobrien	return true;
23877298Sobrien      }
23977298Sobrien
24077298Sobrien  return false;
24177298Sobrien}
24277298Sobrien
24377298Sobrien/* Generic traversal routines for finding matching sections.  */
24477298Sobrien
24560484Sobrienstatic void
24689857Sobrienwalk_wild_section (ptr, file, callback, data)
24760484Sobrien     lang_wild_statement_type *ptr;
24860484Sobrien     lang_input_statement_type *file;
24960484Sobrien     callback_t callback;
25077298Sobrien     PTR data;
25160484Sobrien{
25289857Sobrien  asection *s;
25389857Sobrien
25489857Sobrien  if (file->just_syms_flag)
25589857Sobrien    return;
25689857Sobrien
25789857Sobrien  for (s = file->the_bfd->sections; s != NULL; s = s->next)
25860484Sobrien    {
25989857Sobrien      struct wildcard_list *sec;
26089857Sobrien
26189857Sobrien      sec = ptr->section_list;
26289857Sobrien      if (sec == NULL)
26389857Sobrien	(*callback) (ptr, sec, s, file, data);
26489857Sobrien
26589857Sobrien      while (sec != NULL)
26677298Sobrien	{
26789857Sobrien	  boolean skip = false;
26889857Sobrien	  struct name_list *list_tmp;
26960484Sobrien
27089857Sobrien	  /* Don't process sections from files which were
27189857Sobrien	     excluded.  */
27289857Sobrien	  for (list_tmp = sec->spec.exclude_name_list;
27389857Sobrien	       list_tmp;
27489857Sobrien	       list_tmp = list_tmp->next)
27589857Sobrien	    {
27689857Sobrien	      if (wildcardp (list_tmp->name))
27789857Sobrien		skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
27889857Sobrien	      else
27989857Sobrien		skip = strcmp (list_tmp->name, file->filename) == 0;
28060484Sobrien
28189857Sobrien	      /* If this file is part of an archive, and the archive is
28289857Sobrien		 excluded, exclude this file.  */
28389857Sobrien	      if (! skip && file->the_bfd != NULL
28489857Sobrien		  && file->the_bfd->my_archive != NULL
28589857Sobrien		  && file->the_bfd->my_archive->filename != NULL)
28689857Sobrien		{
28789857Sobrien		  if (wildcardp (list_tmp->name))
28889857Sobrien		    skip = fnmatch (list_tmp->name,
28989857Sobrien				    file->the_bfd->my_archive->filename,
29089857Sobrien				    0) == 0;
29189857Sobrien		  else
29289857Sobrien		    skip = strcmp (list_tmp->name,
29389857Sobrien				   file->the_bfd->my_archive->filename) == 0;
29489857Sobrien		}
29560484Sobrien
29689857Sobrien	      if (skip)
29789857Sobrien		break;
29889857Sobrien	    }
29960484Sobrien
30089857Sobrien	  if (!skip && sec->spec.name != NULL)
30189857Sobrien	    {
30289857Sobrien	      const char *sname = bfd_get_section_name (file->the_bfd, s);
30360484Sobrien
30489857Sobrien	      if (wildcardp (sec->spec.name))
30589857Sobrien		skip = fnmatch (sec->spec.name, sname, 0) != 0;
30689857Sobrien	      else
30789857Sobrien		skip = strcmp (sec->spec.name, sname) != 0;
30889857Sobrien	    }
30960484Sobrien
31089857Sobrien	  if (!skip)
31189857Sobrien	    (*callback) (ptr, sec, s, file, data);
31260484Sobrien
31389857Sobrien	  sec = sec->next;
31460484Sobrien	}
31560484Sobrien    }
31660484Sobrien}
31760484Sobrien
31860484Sobrien/* Handle a wild statement for a single file F.  */
31960484Sobrien
32060484Sobrienstatic void
32189857Sobrienwalk_wild_file (s, f, callback, data)
32260484Sobrien     lang_wild_statement_type *s;
32360484Sobrien     lang_input_statement_type *f;
32460484Sobrien     callback_t callback;
32577298Sobrien     PTR data;
32660484Sobrien{
32760484Sobrien  if (f->the_bfd == NULL
32860484Sobrien      || ! bfd_check_format (f->the_bfd, bfd_archive))
32989857Sobrien    walk_wild_section (s, f, callback, data);
33060484Sobrien  else
33160484Sobrien    {
33260484Sobrien      bfd *member;
33360484Sobrien
33460484Sobrien      /* This is an archive file.  We must map each member of the
33560484Sobrien	 archive separately.  */
33660484Sobrien      member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
33760484Sobrien      while (member != NULL)
33860484Sobrien	{
33960484Sobrien	  /* When lookup_name is called, it will call the add_symbols
34060484Sobrien	     entry point for the archive.  For each element of the
34160484Sobrien	     archive which is included, BFD will call ldlang_add_file,
34260484Sobrien	     which will set the usrdata field of the member to the
34360484Sobrien	     lang_input_statement.  */
34460484Sobrien	  if (member->usrdata != NULL)
34560484Sobrien	    {
34689857Sobrien	      walk_wild_section (s,
34760484Sobrien				 (lang_input_statement_type *) member->usrdata,
34860484Sobrien				 callback, data);
34960484Sobrien	    }
35060484Sobrien
35160484Sobrien	  member = bfd_openr_next_archived_file (f->the_bfd, member);
35260484Sobrien	}
35360484Sobrien    }
35460484Sobrien}
35560484Sobrien
35660484Sobrienstatic void
35789857Sobrienwalk_wild (s, callback, data)
35860484Sobrien     lang_wild_statement_type *s;
35960484Sobrien     callback_t callback;
36077298Sobrien     PTR data;
36160484Sobrien{
36289857Sobrien  const char *file_spec = s->filename;
36389857Sobrien
36489857Sobrien  if (file_spec == NULL)
36560484Sobrien    {
36660484Sobrien      /* Perform the iteration over all files in the list.  */
36760484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
36860484Sobrien	{
36989857Sobrien	  walk_wild_file (s, f, callback, data);
37060484Sobrien	}
37160484Sobrien    }
37289857Sobrien  else if (wildcardp (file_spec))
37360484Sobrien    {
37460484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
37560484Sobrien	{
37689857Sobrien	  if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
37789857Sobrien	    walk_wild_file (s, f, callback, data);
37860484Sobrien	}
37960484Sobrien    }
38060484Sobrien  else
38160484Sobrien    {
38260484Sobrien      lang_input_statement_type *f;
38360484Sobrien
38460484Sobrien      /* Perform the iteration over a single file.  */
38589857Sobrien      f = lookup_name (file_spec);
38689857Sobrien      if (f)
38789857Sobrien	walk_wild_file (s, f, callback, data);
38860484Sobrien    }
38977298Sobrien}
39033965Sjdp
39177298Sobrien/* lang_for_each_statement walks the parse tree and calls the provided
39277298Sobrien   function for each node.  */
39377298Sobrien
39433965Sjdpstatic void
39533965Sjdplang_for_each_statement_worker (func, s)
39633965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
39733965Sjdp     lang_statement_union_type *s;
39833965Sjdp{
39989857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
40033965Sjdp    {
40133965Sjdp      func (s);
40233965Sjdp
40333965Sjdp      switch (s->header.type)
40433965Sjdp	{
40533965Sjdp	case lang_constructors_statement_enum:
40633965Sjdp	  lang_for_each_statement_worker (func, constructor_list.head);
40733965Sjdp	  break;
40833965Sjdp	case lang_output_section_statement_enum:
40933965Sjdp	  lang_for_each_statement_worker
41033965Sjdp	    (func,
41133965Sjdp	     s->output_section_statement.children.head);
41233965Sjdp	  break;
41333965Sjdp	case lang_wild_statement_enum:
41433965Sjdp	  lang_for_each_statement_worker
41533965Sjdp	    (func,
41633965Sjdp	     s->wild_statement.children.head);
41733965Sjdp	  break;
41833965Sjdp	case lang_group_statement_enum:
41933965Sjdp	  lang_for_each_statement_worker (func,
42033965Sjdp					  s->group_statement.children.head);
42133965Sjdp	  break;
42233965Sjdp	case lang_data_statement_enum:
42333965Sjdp	case lang_reloc_statement_enum:
42433965Sjdp	case lang_object_symbols_statement_enum:
42533965Sjdp	case lang_output_statement_enum:
42633965Sjdp	case lang_target_statement_enum:
42733965Sjdp	case lang_input_section_enum:
42833965Sjdp	case lang_input_statement_enum:
42933965Sjdp	case lang_assignment_statement_enum:
43033965Sjdp	case lang_padding_statement_enum:
43133965Sjdp	case lang_address_statement_enum:
43233965Sjdp	case lang_fill_statement_enum:
43333965Sjdp	  break;
43433965Sjdp	default:
43533965Sjdp	  FAIL ();
43633965Sjdp	  break;
43733965Sjdp	}
43833965Sjdp    }
43933965Sjdp}
44033965Sjdp
44133965Sjdpvoid
44233965Sjdplang_for_each_statement (func)
44333965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
44433965Sjdp{
44577298Sobrien  lang_for_each_statement_worker (func, statement_list.head);
44633965Sjdp}
44733965Sjdp
44833965Sjdp/*----------------------------------------------------------------------*/
44977298Sobrien
45033965Sjdpvoid
45133965Sjdplang_list_init (list)
45233965Sjdp     lang_statement_list_type *list;
45333965Sjdp{
45433965Sjdp  list->head = (lang_statement_union_type *) NULL;
45533965Sjdp  list->tail = &list->head;
45633965Sjdp}
45733965Sjdp
45877298Sobrien/* Build a new statement node for the parse tree.  */
45933965Sjdp
46077298Sobrienstatic lang_statement_union_type *
46133965Sjdpnew_statement (type, size, list)
46233965Sjdp     enum statement_enum type;
46333965Sjdp     size_t size;
46477298Sobrien     lang_statement_list_type *list;
46533965Sjdp{
46633965Sjdp  lang_statement_union_type *new = (lang_statement_union_type *)
46733965Sjdp  stat_alloc (size);
46833965Sjdp
46933965Sjdp  new->header.type = type;
47033965Sjdp  new->header.next = (lang_statement_union_type *) NULL;
47133965Sjdp  lang_statement_append (list, new, &new->header.next);
47233965Sjdp  return new;
47333965Sjdp}
47433965Sjdp
47577298Sobrien/* Build a new input file node for the language.  There are several
47677298Sobrien   ways in which we treat an input file, eg, we only look at symbols,
47777298Sobrien   or prefix it with a -l etc.
47833965Sjdp
47977298Sobrien   We can be supplied with requests for input files more than once;
48077298Sobrien   they may, for example be split over serveral lines like foo.o(.text)
48178828Sobrien   foo.o(.data) etc, so when asked for a file we check that we haven't
48277298Sobrien   got it already so we don't duplicate the bfd.  */
48333965Sjdp
48433965Sjdpstatic lang_input_statement_type *
48533965Sjdpnew_afile (name, file_type, target, add_to_list)
48677298Sobrien     const char *name;
48733965Sjdp     lang_input_file_enum_type file_type;
48877298Sobrien     const char *target;
48933965Sjdp     boolean add_to_list;
49033965Sjdp{
49133965Sjdp  lang_input_statement_type *p;
49233965Sjdp
49333965Sjdp  if (add_to_list)
49433965Sjdp    p = new_stat (lang_input_statement, stat_ptr);
49533965Sjdp  else
49633965Sjdp    {
49733965Sjdp      p = ((lang_input_statement_type *)
49833965Sjdp	   stat_alloc (sizeof (lang_input_statement_type)));
49933965Sjdp      p->header.next = NULL;
50033965Sjdp    }
50133965Sjdp
50233965Sjdp  lang_has_input_file = true;
50333965Sjdp  p->target = target;
50433965Sjdp  switch (file_type)
50533965Sjdp    {
50633965Sjdp    case lang_input_file_is_symbols_only_enum:
50733965Sjdp      p->filename = name;
50833965Sjdp      p->is_archive = false;
50933965Sjdp      p->real = true;
51033965Sjdp      p->local_sym_name = name;
51133965Sjdp      p->just_syms_flag = true;
51233965Sjdp      p->search_dirs_flag = false;
51333965Sjdp      break;
51433965Sjdp    case lang_input_file_is_fake_enum:
51533965Sjdp      p->filename = name;
51633965Sjdp      p->is_archive = false;
51733965Sjdp      p->real = false;
51833965Sjdp      p->local_sym_name = name;
51933965Sjdp      p->just_syms_flag = false;
52033965Sjdp      p->search_dirs_flag = false;
52133965Sjdp      break;
52233965Sjdp    case lang_input_file_is_l_enum:
52333965Sjdp      p->is_archive = true;
52433965Sjdp      p->filename = name;
52533965Sjdp      p->real = true;
52633965Sjdp      p->local_sym_name = concat ("-l", name, (const char *) NULL);
52733965Sjdp      p->just_syms_flag = false;
52833965Sjdp      p->search_dirs_flag = true;
52933965Sjdp      break;
53033965Sjdp    case lang_input_file_is_marker_enum:
53133965Sjdp      p->filename = name;
53233965Sjdp      p->is_archive = false;
53333965Sjdp      p->real = false;
53433965Sjdp      p->local_sym_name = name;
53533965Sjdp      p->just_syms_flag = false;
53633965Sjdp      p->search_dirs_flag = true;
53733965Sjdp      break;
53833965Sjdp    case lang_input_file_is_search_file_enum:
53933965Sjdp      p->filename = name;
54033965Sjdp      p->is_archive = false;
54133965Sjdp      p->real = true;
54233965Sjdp      p->local_sym_name = name;
54333965Sjdp      p->just_syms_flag = false;
54433965Sjdp      p->search_dirs_flag = true;
54533965Sjdp      break;
54633965Sjdp    case lang_input_file_is_file_enum:
54733965Sjdp      p->filename = name;
54833965Sjdp      p->is_archive = false;
54933965Sjdp      p->real = true;
55033965Sjdp      p->local_sym_name = name;
55133965Sjdp      p->just_syms_flag = false;
55233965Sjdp      p->search_dirs_flag = false;
55333965Sjdp      break;
55433965Sjdp    default:
55533965Sjdp      FAIL ();
55633965Sjdp    }
55733965Sjdp  p->the_bfd = (bfd *) NULL;
55833965Sjdp  p->asymbols = (asymbol **) NULL;
55933965Sjdp  p->next_real_file = (lang_statement_union_type *) NULL;
56033965Sjdp  p->next = (lang_statement_union_type *) NULL;
56133965Sjdp  p->symbol_count = 0;
56233965Sjdp  p->dynamic = config.dynamic_link;
56333965Sjdp  p->whole_archive = whole_archive;
56433965Sjdp  p->loaded = false;
56533965Sjdp  lang_statement_append (&input_file_chain,
56633965Sjdp			 (lang_statement_union_type *) p,
56733965Sjdp			 &p->next_real_file);
56833965Sjdp  return p;
56933965Sjdp}
57033965Sjdp
57133965Sjdplang_input_statement_type *
57233965Sjdplang_add_input_file (name, file_type, target)
57377298Sobrien     const char *name;
57433965Sjdp     lang_input_file_enum_type file_type;
57577298Sobrien     const char *target;
57633965Sjdp{
57733965Sjdp  lang_has_input_file = true;
57833965Sjdp  return new_afile (name, file_type, target, true);
57933965Sjdp}
58033965Sjdp
58177298Sobrien/* Build enough state so that the parser can build its tree.  */
58277298Sobrien
58333965Sjdpvoid
58433965Sjdplang_init ()
58533965Sjdp{
58633965Sjdp  obstack_begin (&stat_obstack, 1000);
58733965Sjdp
58833965Sjdp  stat_ptr = &statement_list;
58933965Sjdp
59033965Sjdp  lang_list_init (stat_ptr);
59133965Sjdp
59233965Sjdp  lang_list_init (&input_file_chain);
59333965Sjdp  lang_list_init (&lang_output_section_statement);
59433965Sjdp  lang_list_init (&file_chain);
59533965Sjdp  first_file = lang_add_input_file ((char *) NULL,
59633965Sjdp				    lang_input_file_is_marker_enum,
59733965Sjdp				    (char *) NULL);
59877298Sobrien  abs_output_section =
59977298Sobrien    lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
60033965Sjdp
60133965Sjdp  abs_output_section->bfd_section = bfd_abs_section_ptr;
60233965Sjdp
60333965Sjdp}
60433965Sjdp
60533965Sjdp/*----------------------------------------------------------------------
60677298Sobrien  A region is an area of memory declared with the
60777298Sobrien  MEMORY {  name:org=exp, len=exp ... }
60877298Sobrien  syntax.
60933965Sjdp
61077298Sobrien  We maintain a list of all the regions here.
61133965Sjdp
61277298Sobrien  If no regions are specified in the script, then the default is used
61377298Sobrien  which is created when looked up to be the entire data space.  */
61433965Sjdp
61533965Sjdpstatic lang_memory_region_type *lang_memory_region_list;
61633965Sjdpstatic lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
61733965Sjdp
61833965Sjdplang_memory_region_type *
61933965Sjdplang_memory_region_lookup (name)
62077298Sobrien     const char *const name;
62133965Sjdp{
62238889Sjdp  lang_memory_region_type *p;
62333965Sjdp
624104834Sobrien  /* NAME is NULL for LMA memspecs if no region was specified.  */
625104834Sobrien  if (name == NULL)
626104834Sobrien    return NULL;
627104834Sobrien
62833965Sjdp  for (p = lang_memory_region_list;
62933965Sjdp       p != (lang_memory_region_type *) NULL;
63033965Sjdp       p = p->next)
63133965Sjdp    {
63233965Sjdp      if (strcmp (p->name, name) == 0)
63333965Sjdp	{
63433965Sjdp	  return p;
63533965Sjdp	}
63633965Sjdp    }
63733965Sjdp
63833965Sjdp#if 0
63933965Sjdp  /* This code used to always use the first region in the list as the
64033965Sjdp     default region.  I changed it to instead use a region
64133965Sjdp     encompassing all of memory as the default region.  This permits
64233965Sjdp     NOLOAD sections to work reasonably without requiring a region.
64333965Sjdp     People should specify what region they mean, if they really want
64433965Sjdp     a region.  */
64533965Sjdp  if (strcmp (name, "*default*") == 0)
64633965Sjdp    {
64733965Sjdp      if (lang_memory_region_list != (lang_memory_region_type *) NULL)
64833965Sjdp	{
64933965Sjdp	  return lang_memory_region_list;
65033965Sjdp	}
65133965Sjdp    }
65233965Sjdp#endif
65333965Sjdp
65433965Sjdp  {
65533965Sjdp    lang_memory_region_type *new =
65633965Sjdp    (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
65733965Sjdp
65878828Sobrien    new->name = xstrdup (name);
65933965Sjdp    new->next = (lang_memory_region_type *) NULL;
66033965Sjdp
66133965Sjdp    *lang_memory_region_list_tail = new;
66233965Sjdp    lang_memory_region_list_tail = &new->next;
66333965Sjdp    new->origin = 0;
66438889Sjdp    new->flags = 0;
66538889Sjdp    new->not_flags = 0;
66677298Sobrien    new->length = ~(bfd_size_type) 0;
66733965Sjdp    new->current = 0;
66833965Sjdp    new->had_full_message = false;
66933965Sjdp
67033965Sjdp    return new;
67133965Sjdp  }
67233965Sjdp}
67333965Sjdp
67477298Sobrienstatic lang_memory_region_type *
67538889Sjdplang_memory_default (section)
67638889Sjdp     asection *section;
67738889Sjdp{
67838889Sjdp  lang_memory_region_type *p;
67938889Sjdp
68038889Sjdp  flagword sec_flags = section->flags;
68138889Sjdp
68238889Sjdp  /* Override SEC_DATA to mean a writable section.  */
68338889Sjdp  if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
68438889Sjdp    sec_flags |= SEC_DATA;
68538889Sjdp
68638889Sjdp  for (p = lang_memory_region_list;
68738889Sjdp       p != (lang_memory_region_type *) NULL;
68838889Sjdp       p = p->next)
68938889Sjdp    {
69038889Sjdp      if ((p->flags & sec_flags) != 0
69138889Sjdp	  && (p->not_flags & sec_flags) == 0)
69238889Sjdp	{
69338889Sjdp	  return p;
69438889Sjdp	}
69538889Sjdp    }
69638889Sjdp  return lang_memory_region_lookup ("*default*");
69738889Sjdp}
69838889Sjdp
69933965Sjdplang_output_section_statement_type *
70033965Sjdplang_output_section_find (name)
70177298Sobrien     const char *const name;
70233965Sjdp{
70333965Sjdp  lang_statement_union_type *u;
70433965Sjdp  lang_output_section_statement_type *lookup;
70533965Sjdp
70633965Sjdp  for (u = lang_output_section_statement.head;
70733965Sjdp       u != (lang_statement_union_type *) NULL;
70833965Sjdp       u = lookup->next)
70933965Sjdp    {
71033965Sjdp      lookup = &u->output_section_statement;
71133965Sjdp      if (strcmp (name, lookup->name) == 0)
71233965Sjdp	{
71333965Sjdp	  return lookup;
71433965Sjdp	}
71533965Sjdp    }
71633965Sjdp  return (lang_output_section_statement_type *) NULL;
71733965Sjdp}
71833965Sjdp
71933965Sjdplang_output_section_statement_type *
72033965Sjdplang_output_section_statement_lookup (name)
72177298Sobrien     const char *const name;
72233965Sjdp{
72333965Sjdp  lang_output_section_statement_type *lookup;
72433965Sjdp
72533965Sjdp  lookup = lang_output_section_find (name);
72633965Sjdp  if (lookup == (lang_output_section_statement_type *) NULL)
72733965Sjdp    {
72833965Sjdp
72933965Sjdp      lookup = (lang_output_section_statement_type *)
73033965Sjdp	new_stat (lang_output_section_statement, stat_ptr);
73133965Sjdp      lookup->region = (lang_memory_region_type *) NULL;
73260484Sobrien      lookup->lma_region = (lang_memory_region_type *) NULL;
733104834Sobrien      lookup->fill = (fill_type *) 0;
73433965Sjdp      lookup->block_value = 1;
73533965Sjdp      lookup->name = name;
73633965Sjdp
73733965Sjdp      lookup->next = (lang_statement_union_type *) NULL;
73833965Sjdp      lookup->bfd_section = (asection *) NULL;
73933965Sjdp      lookup->processed = false;
74033965Sjdp      lookup->sectype = normal_section;
74133965Sjdp      lookup->addr_tree = (etree_type *) NULL;
74233965Sjdp      lang_list_init (&lookup->children);
74333965Sjdp
74477298Sobrien      lookup->memspec = (const char *) NULL;
74533965Sjdp      lookup->flags = 0;
74633965Sjdp      lookup->subsection_alignment = -1;
74733965Sjdp      lookup->section_alignment = -1;
74833965Sjdp      lookup->load_base = (union etree_union *) NULL;
749104834Sobrien      lookup->update_dot_tree = NULL;
75033965Sjdp      lookup->phdrs = NULL;
75133965Sjdp
75233965Sjdp      lang_statement_append (&lang_output_section_statement,
75333965Sjdp			     (lang_statement_union_type *) lookup,
75433965Sjdp			     &lookup->next);
75533965Sjdp    }
75633965Sjdp  return lookup;
75733965Sjdp}
75833965Sjdp
75938889Sjdpstatic void
76038889Sjdplang_map_flags (flag)
76138889Sjdp     flagword flag;
76238889Sjdp{
76338889Sjdp  if (flag & SEC_ALLOC)
76438889Sjdp    minfo ("a");
76538889Sjdp
76638889Sjdp  if (flag & SEC_CODE)
76738889Sjdp    minfo ("x");
76838889Sjdp
76938889Sjdp  if (flag & SEC_READONLY)
77038889Sjdp    minfo ("r");
77138889Sjdp
77238889Sjdp  if (flag & SEC_DATA)
77338889Sjdp    minfo ("w");
77438889Sjdp
77538889Sjdp  if (flag & SEC_LOAD)
77638889Sjdp    minfo ("l");
77738889Sjdp}
77838889Sjdp
77933965Sjdpvoid
78033965Sjdplang_map ()
78133965Sjdp{
78233965Sjdp  lang_memory_region_type *m;
78333965Sjdp
78460484Sobrien  minfo (_("\nMemory Configuration\n\n"));
78538889Sjdp  fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
78660484Sobrien	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
78733965Sjdp
78833965Sjdp  for (m = lang_memory_region_list;
78933965Sjdp       m != (lang_memory_region_type *) NULL;
79033965Sjdp       m = m->next)
79133965Sjdp    {
79233965Sjdp      char buf[100];
79333965Sjdp      int len;
79433965Sjdp
79533965Sjdp      fprintf (config.map_file, "%-16s ", m->name);
79633965Sjdp
79733965Sjdp      sprintf_vma (buf, m->origin);
79833965Sjdp      minfo ("0x%s ", buf);
79933965Sjdp      len = strlen (buf);
80033965Sjdp      while (len < 16)
80133965Sjdp	{
80233965Sjdp	  print_space ();
80333965Sjdp	  ++len;
80433965Sjdp	}
80533965Sjdp
80638889Sjdp      minfo ("0x%V", m->length);
80738889Sjdp      if (m->flags || m->not_flags)
80838889Sjdp	{
80938889Sjdp#ifndef BFD64
81038889Sjdp	  minfo ("        ");
81138889Sjdp#endif
81238889Sjdp	  if (m->flags)
81338889Sjdp	    {
81438889Sjdp	      print_space ();
81538889Sjdp	      lang_map_flags (m->flags);
81638889Sjdp	    }
81738889Sjdp
81838889Sjdp	  if (m->not_flags)
81938889Sjdp	    {
82038889Sjdp	      minfo (" !");
82138889Sjdp	      lang_map_flags (m->not_flags);
82238889Sjdp	    }
82338889Sjdp	}
82438889Sjdp
82538889Sjdp      print_nl ();
82633965Sjdp    }
82733965Sjdp
82860484Sobrien  fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
82933965Sjdp
83033965Sjdp  print_statements ();
83133965Sjdp}
83233965Sjdp
83333965Sjdp/* Initialize an output section.  */
83433965Sjdp
83533965Sjdpstatic void
83633965Sjdpinit_os (s)
83733965Sjdp     lang_output_section_statement_type *s;
83833965Sjdp{
83933965Sjdp  section_userdata_type *new;
84033965Sjdp
84133965Sjdp  if (s->bfd_section != NULL)
84233965Sjdp    return;
84333965Sjdp
84433965Sjdp  if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
84589857Sobrien    einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
84633965Sjdp
84733965Sjdp  new = ((section_userdata_type *)
84833965Sjdp	 stat_alloc (sizeof (section_userdata_type)));
84933965Sjdp
85033965Sjdp  s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
85133965Sjdp  if (s->bfd_section == (asection *) NULL)
85233965Sjdp    s->bfd_section = bfd_make_section (output_bfd, s->name);
85333965Sjdp  if (s->bfd_section == (asection *) NULL)
85433965Sjdp    {
85560484Sobrien      einfo (_("%P%F: output format %s cannot represent section called %s\n"),
85633965Sjdp	     output_bfd->xvec->name, s->name);
85733965Sjdp    }
85833965Sjdp  s->bfd_section->output_section = s->bfd_section;
85933965Sjdp
86077298Sobrien  /* We initialize an output sections output offset to minus its own
86177298Sobrien     vma to allow us to output a section through itself.  */
86233965Sjdp  s->bfd_section->output_offset = 0;
86333965Sjdp  get_userdata (s->bfd_section) = (PTR) new;
86433965Sjdp
86533965Sjdp  /* If there is a base address, make sure that any sections it might
86633965Sjdp     mention are initialized.  */
86733965Sjdp  if (s->addr_tree != NULL)
86833965Sjdp    exp_init_os (s->addr_tree);
86933965Sjdp}
87033965Sjdp
87133965Sjdp/* Make sure that all output sections mentioned in an expression are
87233965Sjdp   initialized.  */
87333965Sjdp
87433965Sjdpstatic void
87533965Sjdpexp_init_os (exp)
87633965Sjdp     etree_type *exp;
87733965Sjdp{
87833965Sjdp  switch (exp->type.node_class)
87933965Sjdp    {
88033965Sjdp    case etree_assign:
88133965Sjdp      exp_init_os (exp->assign.src);
88233965Sjdp      break;
88333965Sjdp
88433965Sjdp    case etree_binary:
88533965Sjdp      exp_init_os (exp->binary.lhs);
88633965Sjdp      exp_init_os (exp->binary.rhs);
88733965Sjdp      break;
88833965Sjdp
88933965Sjdp    case etree_trinary:
89033965Sjdp      exp_init_os (exp->trinary.cond);
89133965Sjdp      exp_init_os (exp->trinary.lhs);
89233965Sjdp      exp_init_os (exp->trinary.rhs);
89333965Sjdp      break;
89433965Sjdp
89533965Sjdp    case etree_unary:
89633965Sjdp      exp_init_os (exp->unary.child);
89733965Sjdp      break;
89833965Sjdp
89933965Sjdp    case etree_name:
90033965Sjdp      switch (exp->type.node_code)
90133965Sjdp	{
90233965Sjdp	case ADDR:
90333965Sjdp	case LOADADDR:
90433965Sjdp	case SIZEOF:
90533965Sjdp	  {
90633965Sjdp	    lang_output_section_statement_type *os;
90733965Sjdp
90833965Sjdp	    os = lang_output_section_find (exp->name.name);
90933965Sjdp	    if (os != NULL && os->bfd_section == NULL)
91033965Sjdp	      init_os (os);
91133965Sjdp	  }
91233965Sjdp	}
91333965Sjdp      break;
91433965Sjdp
91533965Sjdp    default:
91633965Sjdp      break;
91733965Sjdp    }
91833965Sjdp}
91960484Sobrien
92033965Sjdp/* Sections marked with the SEC_LINK_ONCE flag should only be linked
92160484Sobrien   once into the output.  This routine checks each section, and
92260484Sobrien   arrange to discard it if a section of the same name has already
92360484Sobrien   been linked.  If the section has COMDAT information, then it uses
92460484Sobrien   that to decide whether the section should be included.  This code
92560484Sobrien   assumes that all relevant sections have the SEC_LINK_ONCE flag set;
92660484Sobrien   that is, it does not depend solely upon the section name.
92760484Sobrien   section_already_linked is called via bfd_map_over_sections.  */
92833965Sjdp
92960484Sobrien/* This is the shape of the elements inside the already_linked hash
93060484Sobrien   table. It maps a name onto a list of already_linked elements with
93160484Sobrien   the same name.  It's possible to get more than one element in a
93260484Sobrien   list if the COMDAT sections have different names.  */
93360484Sobrien
93477298Sobrienstruct already_linked_hash_entry
93560484Sobrien{
93660484Sobrien  struct bfd_hash_entry root;
93760484Sobrien  struct already_linked *entry;
93860484Sobrien};
93960484Sobrien
94077298Sobrienstruct already_linked
94160484Sobrien{
94260484Sobrien  struct already_linked *next;
94360484Sobrien  asection *sec;
94460484Sobrien};
94560484Sobrien
94660484Sobrien/* The hash table.  */
94760484Sobrien
94860484Sobrienstatic struct bfd_hash_table already_linked_table;
94960484Sobrien
95033965Sjdpstatic void
95133965Sjdpsection_already_linked (abfd, sec, data)
95233965Sjdp     bfd *abfd;
95333965Sjdp     asection *sec;
95433965Sjdp     PTR data;
95533965Sjdp{
95633965Sjdp  lang_input_statement_type *entry = (lang_input_statement_type *) data;
95733965Sjdp  flagword flags;
95833965Sjdp  const char *name;
95960484Sobrien  struct already_linked *l;
96060484Sobrien  struct already_linked_hash_entry *already_linked_list;
96133965Sjdp
96233965Sjdp  /* If we are only reading symbols from this object, then we want to
96333965Sjdp     discard all sections.  */
96433965Sjdp  if (entry->just_syms_flag)
96533965Sjdp    {
966104834Sobrien      bfd_link_just_syms (sec, &link_info);
96733965Sjdp      return;
96833965Sjdp    }
96933965Sjdp
97033965Sjdp  flags = bfd_get_section_flags (abfd, sec);
97133965Sjdp
97233965Sjdp  if ((flags & SEC_LINK_ONCE) == 0)
97333965Sjdp    return;
97433965Sjdp
97577298Sobrien  /* FIXME: When doing a relocatable link, we may have trouble
97660484Sobrien     copying relocations in other sections that refer to local symbols
97760484Sobrien     in the section being discarded.  Those relocations will have to
97860484Sobrien     be converted somehow; as of this writing I'm not sure that any of
97960484Sobrien     the backends handle that correctly.
98060484Sobrien
98160484Sobrien     It is tempting to instead not discard link once sections when
98277298Sobrien     doing a relocatable link (technically, they should be discarded
98360484Sobrien     whenever we are building constructors).  However, that fails,
98460484Sobrien     because the linker winds up combining all the link once sections
98560484Sobrien     into a single large link once section, which defeats the purpose
98660484Sobrien     of having link once sections in the first place.
98760484Sobrien
98877298Sobrien     Also, not merging link once sections in a relocatable link
989104834Sobrien     causes trouble for MIPS ELF, which relies on link once semantics
99060484Sobrien     to handle the .reginfo section correctly.  */
99160484Sobrien
99233965Sjdp  name = bfd_get_section_name (abfd, sec);
99333965Sjdp
99477298Sobrien  already_linked_list =
99560484Sobrien    ((struct already_linked_hash_entry *)
99660484Sobrien     bfd_hash_lookup (&already_linked_table, name, true, false));
99760484Sobrien
99877298Sobrien  for (l = already_linked_list->entry; l != NULL; l = l->next)
99933965Sjdp    {
100060484Sobrien      if (sec->comdat == NULL
100160484Sobrien	  || l->sec->comdat == NULL
100260484Sobrien	  || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
100333965Sjdp	{
100433965Sjdp	  /* The section has already been linked.  See if we should
100533965Sjdp             issue a warning.  */
100633965Sjdp	  switch (flags & SEC_LINK_DUPLICATES)
100733965Sjdp	    {
100833965Sjdp	    default:
100933965Sjdp	      abort ();
101033965Sjdp
101133965Sjdp	    case SEC_LINK_DUPLICATES_DISCARD:
101233965Sjdp	      break;
101333965Sjdp
101433965Sjdp	    case SEC_LINK_DUPLICATES_ONE_ONLY:
101560484Sobrien	      if (sec->comdat == NULL)
101660484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
101760484Sobrien		       abfd, name);
101860484Sobrien	      else
101960484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
102060484Sobrien		       abfd, name, sec->comdat->name);
102133965Sjdp	      break;
102233965Sjdp
102333965Sjdp	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
102433965Sjdp	      /* FIXME: We should really dig out the contents of both
102533965Sjdp                 sections and memcmp them.  The COFF/PE spec says that
102633965Sjdp                 the Microsoft linker does not implement this
102733965Sjdp                 correctly, so I'm not going to bother doing it
102833965Sjdp                 either.  */
102933965Sjdp	      /* Fall through.  */
103033965Sjdp	    case SEC_LINK_DUPLICATES_SAME_SIZE:
103133965Sjdp	      if (bfd_section_size (abfd, sec)
103233965Sjdp		  != bfd_section_size (l->sec->owner, l->sec))
103360484Sobrien		einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
103433965Sjdp		       abfd, name);
103533965Sjdp	      break;
103633965Sjdp	    }
103733965Sjdp
103889857Sobrien	  /* Set the output_section field so that lang_add_section
103989857Sobrien	     does not create a lang_input_section structure for this
104089857Sobrien	     section.  */
104133965Sjdp	  sec->output_section = bfd_abs_section_ptr;
104233965Sjdp
1043104834Sobrien	  if (flags & SEC_GROUP)
1044104834Sobrien	    bfd_discard_group (abfd, sec);
1045104834Sobrien
104633965Sjdp	  return;
104733965Sjdp	}
104833965Sjdp    }
104933965Sjdp
105060484Sobrien  /* This is the first section with this name.  Record it.  Allocate
105160484Sobrien     the memory from the same obstack as the hash table is kept in.  */
105233965Sjdp
105377298Sobrien  l = ((struct already_linked *)
105460484Sobrien       bfd_hash_allocate (&already_linked_table, sizeof *l));
105560484Sobrien
105633965Sjdp  l->sec = sec;
105760484Sobrien  l->next = already_linked_list->entry;
105860484Sobrien  already_linked_list->entry = l;
105933965Sjdp}
106060484Sobrien
106160484Sobrien/* Support routines for the hash table used by section_already_linked,
106260484Sobrien   initialize the table, fill in an entry and remove the table.  */
106360484Sobrien
106460484Sobrienstatic struct bfd_hash_entry *
106560484Sobrienalready_linked_newfunc (entry, table, string)
106660484Sobrien     struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
106760484Sobrien     struct bfd_hash_table *table;
106860484Sobrien     const char *string ATTRIBUTE_UNUSED;
106960484Sobrien{
107077298Sobrien  struct already_linked_hash_entry *ret =
107160484Sobrien    bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
107260484Sobrien
107360484Sobrien  ret->entry = NULL;
107460484Sobrien
107560484Sobrien  return (struct bfd_hash_entry *) ret;
107660484Sobrien}
107760484Sobrien
107860484Sobrienstatic void
107960484Sobrienalready_linked_table_init ()
108060484Sobrien{
108160484Sobrien  if (! bfd_hash_table_init_n (&already_linked_table,
108260484Sobrien			       already_linked_newfunc,
108360484Sobrien			       42))
108460484Sobrien    einfo (_("%P%F: Failed to create hash table\n"));
108560484Sobrien}
108660484Sobrien
108760484Sobrienstatic void
108860484Sobrienalready_linked_table_free ()
108960484Sobrien{
109060484Sobrien  bfd_hash_table_free (&already_linked_table);
109160484Sobrien}
109233965Sjdp
109333965Sjdp/* The wild routines.
109433965Sjdp
109533965Sjdp   These expand statements like *(.text) and foo.o to a list of
109633965Sjdp   explicit actions, like foo.o(.text), bar.o(.text) and
109733965Sjdp   foo.o(.text, .data).  */
109833965Sjdp
109938889Sjdp/* Return true if the PATTERN argument is a wildcard pattern.
110038889Sjdp   Although backslashes are treated specially if a pattern contains
110138889Sjdp   wildcards, we do not consider the mere presence of a backslash to
110289857Sobrien   be enough to cause the pattern to be treated as a wildcard.
110338889Sjdp   That lets us handle DOS filenames more naturally.  */
110433965Sjdp
110533965Sjdpstatic boolean
110633965Sjdpwildcardp (pattern)
110733965Sjdp     const char *pattern;
110833965Sjdp{
110933965Sjdp  const char *s;
111033965Sjdp
111133965Sjdp  for (s = pattern; *s != '\0'; ++s)
111233965Sjdp    if (*s == '?'
111333965Sjdp	|| *s == '*'
111433965Sjdp	|| *s == '[')
111533965Sjdp      return true;
111633965Sjdp  return false;
111733965Sjdp}
111833965Sjdp
111933965Sjdp/* Add SECTION to the output section OUTPUT.  Do this by creating a
112033965Sjdp   lang_input_section statement which is placed at PTR.  FILE is the
112133965Sjdp   input file which holds SECTION.  */
112233965Sjdp
112333965Sjdpvoid
112489857Sobrienlang_add_section (ptr, section, output, file)
112533965Sjdp     lang_statement_list_type *ptr;
112633965Sjdp     asection *section;
112733965Sjdp     lang_output_section_statement_type *output;
112833965Sjdp     lang_input_statement_type *file;
112933965Sjdp{
113033965Sjdp  flagword flags;
113133965Sjdp  boolean discard;
113233965Sjdp
113333965Sjdp  flags = bfd_get_section_flags (section->owner, section);
113433965Sjdp
113533965Sjdp  discard = false;
113633965Sjdp
113733965Sjdp  /* If we are doing a final link, discard sections marked with
113833965Sjdp     SEC_EXCLUDE.  */
113933965Sjdp  if (! link_info.relocateable
114033965Sjdp      && (flags & SEC_EXCLUDE) != 0)
114133965Sjdp    discard = true;
114233965Sjdp
114333965Sjdp  /* Discard input sections which are assigned to a section named
114433965Sjdp     DISCARD_SECTION_NAME.  */
114533965Sjdp  if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
114633965Sjdp    discard = true;
114733965Sjdp
114833965Sjdp  /* Discard debugging sections if we are stripping debugging
114933965Sjdp     information.  */
115033965Sjdp  if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
115133965Sjdp      && (flags & SEC_DEBUGGING) != 0)
115233965Sjdp    discard = true;
115333965Sjdp
115433965Sjdp  if (discard)
115533965Sjdp    {
115633965Sjdp      if (section->output_section == NULL)
115733965Sjdp	{
115833965Sjdp	  /* This prevents future calls from assigning this section.  */
115933965Sjdp	  section->output_section = bfd_abs_section_ptr;
116033965Sjdp	}
116133965Sjdp      return;
116233965Sjdp    }
116333965Sjdp
116433965Sjdp  if (section->output_section == NULL)
116533965Sjdp    {
116638889Sjdp      boolean first;
116733965Sjdp      lang_input_section_type *new;
116838889Sjdp      flagword flags;
116933965Sjdp
117033965Sjdp      if (output->bfd_section == NULL)
117178828Sobrien	init_os (output);
117233965Sjdp
117378828Sobrien      first = ! output->bfd_section->linker_has_input;
117478828Sobrien      output->bfd_section->linker_has_input = 1;
117578828Sobrien
117677298Sobrien      /* Add a section reference to the list.  */
117733965Sjdp      new = new_stat (lang_input_section, ptr);
117833965Sjdp
117933965Sjdp      new->section = section;
118033965Sjdp      new->ifile = file;
118133965Sjdp      section->output_section = output->bfd_section;
118233965Sjdp
118338889Sjdp      flags = section->flags;
118438889Sjdp
118533965Sjdp      /* We don't copy the SEC_NEVER_LOAD flag from an input section
118633965Sjdp	 to an output section, because we want to be able to include a
118733965Sjdp	 SEC_NEVER_LOAD section in the middle of an otherwise loaded
118833965Sjdp	 section (I don't know why we want to do this, but we do).
118933965Sjdp	 build_link_order in ldwrite.c handles this case by turning
119038889Sjdp	 the embedded SEC_NEVER_LOAD section into a fill.  */
119133965Sjdp
119238889Sjdp      flags &= ~ SEC_NEVER_LOAD;
119333965Sjdp
119438889Sjdp      /* If final link, don't copy the SEC_LINK_ONCE flags, they've
119538889Sjdp	 already been processed.  One reason to do this is that on pe
119638889Sjdp	 format targets, .text$foo sections go into .text and it's odd
119738889Sjdp	 to see .text with SEC_LINK_ONCE set.  */
119833965Sjdp
119938889Sjdp      if (! link_info.relocateable)
120038889Sjdp	flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
120138889Sjdp
120238889Sjdp      /* If this is not the first input section, and the SEC_READONLY
120338889Sjdp         flag is not currently set, then don't set it just because the
120438889Sjdp         input section has it set.  */
120538889Sjdp
120638889Sjdp      if (! first && (section->output_section->flags & SEC_READONLY) == 0)
120738889Sjdp	flags &= ~ SEC_READONLY;
120838889Sjdp
120989857Sobrien      /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
121089857Sobrien      if (! first
121189857Sobrien	  && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
121289857Sobrien	      != (flags & (SEC_MERGE | SEC_STRINGS))
121389857Sobrien	      || ((flags & SEC_MERGE)
121489857Sobrien		  && section->output_section->entsize != section->entsize)))
121589857Sobrien	{
121689857Sobrien	  section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
121789857Sobrien	  flags &= ~ (SEC_MERGE | SEC_STRINGS);
121889857Sobrien	}
121989857Sobrien
1220104834Sobrien      /* For now make .tbss normal section.  */
1221104834Sobrien      if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocateable)
1222104834Sobrien	flags |= SEC_LOAD;
1223104834Sobrien
122438889Sjdp      section->output_section->flags |= flags;
122538889Sjdp
122689857Sobrien      if (flags & SEC_MERGE)
122789857Sobrien	section->output_section->entsize = section->entsize;
122889857Sobrien
122938889Sjdp      /* If SEC_READONLY is not set in the input section, then clear
123038889Sjdp         it from the output section.  */
123138889Sjdp      if ((section->flags & SEC_READONLY) == 0)
123238889Sjdp	section->output_section->flags &= ~SEC_READONLY;
123338889Sjdp
123433965Sjdp      switch (output->sectype)
123533965Sjdp	{
123633965Sjdp	case normal_section:
123733965Sjdp	  break;
123833965Sjdp	case dsect_section:
123933965Sjdp	case copy_section:
124033965Sjdp	case info_section:
124133965Sjdp	case overlay_section:
124233965Sjdp	  output->bfd_section->flags &= ~SEC_ALLOC;
124333965Sjdp	  break;
124433965Sjdp	case noload_section:
124533965Sjdp	  output->bfd_section->flags &= ~SEC_LOAD;
124633965Sjdp	  output->bfd_section->flags |= SEC_NEVER_LOAD;
124733965Sjdp	  break;
124833965Sjdp	}
124933965Sjdp
125060484Sobrien      /* Copy over SEC_SMALL_DATA.  */
125160484Sobrien      if (section->flags & SEC_SMALL_DATA)
125260484Sobrien	section->output_section->flags |= SEC_SMALL_DATA;
125360484Sobrien
125433965Sjdp      if (section->alignment_power > output->bfd_section->alignment_power)
125533965Sjdp	output->bfd_section->alignment_power = section->alignment_power;
125633965Sjdp
125733965Sjdp      /* If supplied an aligment, then force it.  */
125833965Sjdp      if (output->section_alignment != -1)
125933965Sjdp	output->bfd_section->alignment_power = output->section_alignment;
126077298Sobrien
126177298Sobrien      if (section->flags & SEC_BLOCK)
126277298Sobrien	{
126377298Sobrien	  section->output_section->flags |= SEC_BLOCK;
126477298Sobrien	  /* FIXME: This value should really be obtained from the bfd...  */
126577298Sobrien	  output->block_value = 128;
126677298Sobrien	}
126733965Sjdp    }
126833965Sjdp}
126933965Sjdp
127060484Sobrien/* Handle wildcard sorting.  This returns the lang_input_section which
127160484Sobrien   should follow the one we are going to create for SECTION and FILE,
127260484Sobrien   based on the sorting requirements of WILD.  It returns NULL if the
127360484Sobrien   new section should just go at the end of the current list.  */
127433965Sjdp
127560484Sobrienstatic lang_statement_union_type *
127689857Sobrienwild_sort (wild, sec, file, section)
127760484Sobrien     lang_wild_statement_type *wild;
127889857Sobrien     struct wildcard_list *sec;
127933965Sjdp     lang_input_statement_type *file;
128060484Sobrien     asection *section;
128133965Sjdp{
128260484Sobrien  const char *section_name;
128360484Sobrien  lang_statement_union_type *l;
128460484Sobrien
128589857Sobrien  if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
128660484Sobrien    return NULL;
128760484Sobrien
128860484Sobrien  section_name = bfd_get_section_name (file->the_bfd, section);
128989857Sobrien  for (l = wild->children.head; l != NULL; l = l->header.next)
129033965Sjdp    {
129160484Sobrien      lang_input_section_type *ls;
129233965Sjdp
129360484Sobrien      if (l->header.type != lang_input_section_enum)
129460484Sobrien	continue;
129560484Sobrien      ls = &l->input_section;
129633965Sjdp
129760484Sobrien      /* Sorting by filename takes precedence over sorting by section
129860484Sobrien         name.  */
129960484Sobrien
130060484Sobrien      if (wild->filenames_sorted)
130133965Sjdp	{
130260484Sobrien	  const char *fn, *ln;
130360484Sobrien	  boolean fa, la;
130460484Sobrien	  int i;
130533965Sjdp
130660484Sobrien	  /* The PE support for the .idata section as generated by
130760484Sobrien             dlltool assumes that files will be sorted by the name of
130860484Sobrien             the archive and then the name of the file within the
130960484Sobrien             archive.  */
131033965Sjdp
131160484Sobrien	  if (file->the_bfd != NULL
131260484Sobrien	      && bfd_my_archive (file->the_bfd) != NULL)
131360484Sobrien	    {
131460484Sobrien	      fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
131560484Sobrien	      fa = true;
131660484Sobrien	    }
131760484Sobrien	  else
131860484Sobrien	    {
131960484Sobrien	      fn = file->filename;
132060484Sobrien	      fa = false;
132160484Sobrien	    }
132233965Sjdp
132360484Sobrien	  if (ls->ifile->the_bfd != NULL
132460484Sobrien	      && bfd_my_archive (ls->ifile->the_bfd) != NULL)
132560484Sobrien	    {
132660484Sobrien	      ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
132760484Sobrien	      la = true;
132860484Sobrien	    }
132933965Sjdp	  else
133033965Sjdp	    {
133160484Sobrien	      ln = ls->ifile->filename;
133260484Sobrien	      la = false;
133360484Sobrien	    }
133433965Sjdp
133560484Sobrien	  i = strcmp (fn, ln);
133660484Sobrien	  if (i > 0)
133760484Sobrien	    continue;
133860484Sobrien	  else if (i < 0)
133960484Sobrien	    break;
134060484Sobrien
134160484Sobrien	  if (fa || la)
134260484Sobrien	    {
134360484Sobrien	      if (fa)
134460484Sobrien		fn = file->filename;
134560484Sobrien	      if (la)
134660484Sobrien		ln = ls->ifile->filename;
134760484Sobrien
134860484Sobrien	      i = strcmp (fn, ln);
134960484Sobrien	      if (i > 0)
135060484Sobrien		continue;
135160484Sobrien	      else if (i < 0)
135260484Sobrien		break;
135333965Sjdp	    }
135433965Sjdp	}
135560484Sobrien
135660484Sobrien      /* Here either the files are not sorted by name, or we are
135760484Sobrien         looking at the sections for this file.  */
135860484Sobrien
135989857Sobrien      if (sec != NULL && sec->spec.sorted)
136060484Sobrien	{
136160484Sobrien	  if (strcmp (section_name,
136260484Sobrien		      bfd_get_section_name (ls->ifile->the_bfd,
136360484Sobrien					    ls->section))
136460484Sobrien	      < 0)
136560484Sobrien	    break;
136660484Sobrien	}
136733965Sjdp    }
136860484Sobrien
136960484Sobrien  return l;
137033965Sjdp}
137133965Sjdp
137260484Sobrien/* Expand a wild statement for a particular FILE.  SECTION may be
137360484Sobrien   NULL, in which case it is a wild card.  */
137460484Sobrien
137560484Sobrienstatic void
137689857Sobrienoutput_section_callback (ptr, sec, section, file, output)
137760484Sobrien     lang_wild_statement_type *ptr;
137889857Sobrien     struct wildcard_list *sec;
137960484Sobrien     asection *section;
138060484Sobrien     lang_input_statement_type *file;
138177298Sobrien     PTR output;
138260484Sobrien{
138360484Sobrien  lang_statement_union_type *before;
138477298Sobrien
138589857Sobrien  /* Exclude sections that match UNIQUE_SECTION_LIST.  */
138689857Sobrien  if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
138789857Sobrien    return;
138889857Sobrien
138960484Sobrien  /* If the wild pattern was marked KEEP, the member sections
139060484Sobrien     should be as well.  */
139160484Sobrien  if (ptr->keep_sections)
139260484Sobrien    section->flags |= SEC_KEEP;
139377298Sobrien
139489857Sobrien  before = wild_sort (ptr, sec, file, section);
139577298Sobrien
139660484Sobrien  /* Here BEFORE points to the lang_input_section which
139760484Sobrien     should follow the one we are about to add.  If BEFORE
139860484Sobrien     is NULL, then the section should just go at the end
139960484Sobrien     of the current list.  */
140077298Sobrien
140160484Sobrien  if (before == NULL)
140289857Sobrien    lang_add_section (&ptr->children, section,
140389857Sobrien		      (lang_output_section_statement_type *) output,
140489857Sobrien		      file);
140560484Sobrien  else
140660484Sobrien    {
140760484Sobrien      lang_statement_list_type list;
140860484Sobrien      lang_statement_union_type **pp;
140977298Sobrien
141060484Sobrien      lang_list_init (&list);
141189857Sobrien      lang_add_section (&list, section,
141289857Sobrien			(lang_output_section_statement_type *) output,
141389857Sobrien			file);
141477298Sobrien
141560484Sobrien      /* If we are discarding the section, LIST.HEAD will
141660484Sobrien	 be NULL.  */
141760484Sobrien      if (list.head != NULL)
141860484Sobrien	{
141989857Sobrien	  ASSERT (list.head->header.next == NULL);
142077298Sobrien
142160484Sobrien	  for (pp = &ptr->children.head;
142260484Sobrien	       *pp != before;
142389857Sobrien	       pp = &(*pp)->header.next)
142460484Sobrien	    ASSERT (*pp != NULL);
142577298Sobrien
142689857Sobrien	  list.head->header.next = *pp;
142760484Sobrien	  *pp = list.head;
142860484Sobrien	}
142960484Sobrien    }
143060484Sobrien}
143160484Sobrien
143233965Sjdp/* This is passed a file name which must have been seen already and
143333965Sjdp   added to the statement tree.  We will see if it has been opened
143433965Sjdp   already and had its symbols read.  If not then we'll read it.  */
143533965Sjdp
143633965Sjdpstatic lang_input_statement_type *
143733965Sjdplookup_name (name)
143833965Sjdp     const char *name;
143933965Sjdp{
144033965Sjdp  lang_input_statement_type *search;
144133965Sjdp
144233965Sjdp  for (search = (lang_input_statement_type *) input_file_chain.head;
144333965Sjdp       search != (lang_input_statement_type *) NULL;
144433965Sjdp       search = (lang_input_statement_type *) search->next_real_file)
144533965Sjdp    {
144633965Sjdp      if (search->filename == (char *) NULL && name == (char *) NULL)
144733965Sjdp	return search;
144833965Sjdp      if (search->filename != (char *) NULL
144933965Sjdp	  && name != (char *) NULL
145033965Sjdp	  && strcmp (search->filename, name) == 0)
145133965Sjdp	break;
145233965Sjdp    }
145333965Sjdp
145433965Sjdp  if (search == (lang_input_statement_type *) NULL)
145533965Sjdp    search = new_afile (name, lang_input_file_is_file_enum, default_target,
145633965Sjdp			false);
145733965Sjdp
145833965Sjdp  /* If we have already added this file, or this file is not real
145933965Sjdp     (FIXME: can that ever actually happen?) or the name is NULL
146033965Sjdp     (FIXME: can that ever actually happen?) don't add this file.  */
146133965Sjdp  if (search->loaded
146233965Sjdp      || ! search->real
146333965Sjdp      || search->filename == (const char *) NULL)
146433965Sjdp    return search;
146533965Sjdp
146689857Sobrien  if (! load_symbols (search, (lang_statement_list_type *) NULL))
146789857Sobrien    return NULL;
146833965Sjdp
146933965Sjdp  return search;
147033965Sjdp}
147133965Sjdp
147233965Sjdp/* Get the symbols for an input file.  */
147333965Sjdp
147489857Sobrienstatic boolean
147533965Sjdpload_symbols (entry, place)
147633965Sjdp     lang_input_statement_type *entry;
147733965Sjdp     lang_statement_list_type *place;
147833965Sjdp{
147933965Sjdp  char **matching;
148033965Sjdp
148133965Sjdp  if (entry->loaded)
148289857Sobrien    return true;
148333965Sjdp
148433965Sjdp  ldfile_open_file (entry);
148533965Sjdp
148633965Sjdp  if (! bfd_check_format (entry->the_bfd, bfd_archive)
148733965Sjdp      && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
148833965Sjdp    {
148933965Sjdp      bfd_error_type err;
149033965Sjdp      lang_statement_list_type *hold;
149189857Sobrien      boolean bad_load = true;
1492104834Sobrien
149333965Sjdp      err = bfd_get_error ();
149478828Sobrien
149578828Sobrien      /* See if the emulation has some special knowledge.  */
149678828Sobrien      if (ldemul_unrecognized_file (entry))
149789857Sobrien	return true;
149878828Sobrien
149933965Sjdp      if (err == bfd_error_file_ambiguously_recognized)
150033965Sjdp	{
150133965Sjdp	  char **p;
150233965Sjdp
150360484Sobrien	  einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
150460484Sobrien	  einfo (_("%B: matching formats:"), entry->the_bfd);
150533965Sjdp	  for (p = matching; *p != NULL; p++)
150633965Sjdp	    einfo (" %s", *p);
150733965Sjdp	  einfo ("%F\n");
150833965Sjdp	}
150933965Sjdp      else if (err != bfd_error_file_not_recognized
151033965Sjdp	       || place == NULL)
151189857Sobrien	  einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
151289857Sobrien      else
151389857Sobrien	bad_load = false;
1514104834Sobrien
151533965Sjdp      bfd_close (entry->the_bfd);
151633965Sjdp      entry->the_bfd = NULL;
151733965Sjdp
151833965Sjdp      /* Try to interpret the file as a linker script.  */
151933965Sjdp      ldfile_open_command_file (entry->filename);
152033965Sjdp
152133965Sjdp      hold = stat_ptr;
152233965Sjdp      stat_ptr = place;
152333965Sjdp
152433965Sjdp      ldfile_assumed_script = true;
152533965Sjdp      parser_input = input_script;
152633965Sjdp      yyparse ();
152733965Sjdp      ldfile_assumed_script = false;
152833965Sjdp
152933965Sjdp      stat_ptr = hold;
153033965Sjdp
153189857Sobrien      return ! bad_load;
153233965Sjdp    }
153333965Sjdp
153460484Sobrien  if (ldemul_recognized_file (entry))
153589857Sobrien    return true;
153660484Sobrien
153733965Sjdp  /* We don't call ldlang_add_file for an archive.  Instead, the
153833965Sjdp     add_symbols entry point will call ldlang_add_file, via the
153933965Sjdp     add_archive_element callback, for each element of the archive
154033965Sjdp     which is used.  */
154133965Sjdp  switch (bfd_get_format (entry->the_bfd))
154233965Sjdp    {
154333965Sjdp    default:
154433965Sjdp      break;
154533965Sjdp
154633965Sjdp    case bfd_object:
154733965Sjdp      ldlang_add_file (entry);
154833965Sjdp      if (trace_files || trace_file_tries)
154933965Sjdp	info_msg ("%I\n", entry);
155033965Sjdp      break;
155133965Sjdp
155233965Sjdp    case bfd_archive:
155333965Sjdp      if (entry->whole_archive)
155433965Sjdp	{
1555104834Sobrien	  bfd *member = NULL;
155689857Sobrien	  boolean loaded = true;
155789857Sobrien
155889857Sobrien	  for (;;)
155933965Sjdp	    {
156089857Sobrien	      member = bfd_openr_next_archived_file (entry->the_bfd, member);
156189857Sobrien
156289857Sobrien	      if (member == NULL)
156389857Sobrien		break;
1564104834Sobrien
156533965Sjdp	      if (! bfd_check_format (member, bfd_object))
156689857Sobrien		{
156789857Sobrien		  einfo (_("%F%B: member %B in archive is not an object\n"),
156889857Sobrien			 entry->the_bfd, member);
156989857Sobrien		  loaded = false;
157089857Sobrien		}
157189857Sobrien
157233965Sjdp	      if (! ((*link_info.callbacks->add_archive_element)
157333965Sjdp		     (&link_info, member, "--whole-archive")))
157433965Sjdp		abort ();
157589857Sobrien
157633965Sjdp	      if (! bfd_link_add_symbols (member, &link_info))
157789857Sobrien		{
157889857Sobrien		  einfo (_("%F%B: could not read symbols: %E\n"), member);
157989857Sobrien		  loaded = false;
158089857Sobrien		}
158133965Sjdp	    }
158233965Sjdp
158389857Sobrien	  entry->loaded = loaded;
158489857Sobrien	  return loaded;
158533965Sjdp	}
158689857Sobrien      break;
158733965Sjdp    }
158833965Sjdp
158989857Sobrien  if (bfd_link_add_symbols (entry->the_bfd, &link_info))
159089857Sobrien    entry->loaded = true;
159189857Sobrien  else
159260484Sobrien    einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
159333965Sjdp
159489857Sobrien  return entry->loaded;
159533965Sjdp}
159633965Sjdp
159789857Sobrien/* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
159889857Sobrien   may be NULL, indicating that it is a wildcard.  Separate
159989857Sobrien   lang_input_section statements are created for each part of the
160089857Sobrien   expansion; they are added after the wild statement S.  OUTPUT is
160189857Sobrien   the output section.  */
160233965Sjdp
160333965Sjdpstatic void
160489857Sobrienwild (s, target, output)
160533965Sjdp     lang_wild_statement_type *s;
160660484Sobrien     const char *target ATTRIBUTE_UNUSED;
160733965Sjdp     lang_output_section_statement_type *output;
160833965Sjdp{
160989857Sobrien  struct wildcard_list *sec;
161033965Sjdp
161189857Sobrien  walk_wild (s, output_section_callback, (PTR) output);
161289857Sobrien
161389857Sobrien  for (sec = s->section_list; sec != NULL; sec = sec->next)
161433965Sjdp    {
161589857Sobrien      if (default_common_section != NULL)
161689857Sobrien	break;
161789857Sobrien      if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
161889857Sobrien	{
161989857Sobrien	  /* Remember the section that common is going to in case we
1620104834Sobrien	     later get something which doesn't know where to put it.  */
162189857Sobrien	  default_common_section = output;
162289857Sobrien	}
162333965Sjdp    }
162460484Sobrien}
162560484Sobrien
162660484Sobrien/* Return true iff target is the sought target.  */
162777298Sobrien
162860484Sobrienstatic int
162960484Sobrienget_target (target, data)
163077298Sobrien     const bfd_target *target;
163177298Sobrien     PTR data;
163260484Sobrien{
163377298Sobrien  const char *sought = (const char *) data;
163477298Sobrien
163560484Sobrien  return strcmp (target->name, sought) == 0;
163660484Sobrien}
163760484Sobrien
163860484Sobrien/* Like strcpy() but convert to lower case as well.  */
163977298Sobrien
164060484Sobrienstatic void
164160484Sobrienstricpy (dest, src)
164277298Sobrien     char *dest;
164377298Sobrien     char *src;
164460484Sobrien{
164560484Sobrien  char c;
164677298Sobrien
164777298Sobrien  while ((c = *src++) != 0)
164889857Sobrien    *dest++ = TOLOWER (c);
164960484Sobrien
165077298Sobrien  *dest = 0;
165160484Sobrien}
165260484Sobrien
165360484Sobrien/* Remove the first occurance of needle (if any) in haystack
165460484Sobrien   from haystack.  */
165577298Sobrien
165660484Sobrienstatic void
165760484Sobrienstrcut (haystack, needle)
165877298Sobrien     char *haystack;
165977298Sobrien     char *needle;
166060484Sobrien{
166160484Sobrien  haystack = strstr (haystack, needle);
166277298Sobrien
166360484Sobrien  if (haystack)
166433965Sjdp    {
166577298Sobrien      char *src;
166660484Sobrien
166777298Sobrien      for (src = haystack + strlen (needle); *src;)
166877298Sobrien	*haystack++ = *src++;
166977298Sobrien
167077298Sobrien      *haystack = 0;
167133965Sjdp    }
167260484Sobrien}
167333965Sjdp
167460484Sobrien/* Compare two target format name strings.
167560484Sobrien   Return a value indicating how "similar" they are.  */
167677298Sobrien
167760484Sobrienstatic int
167860484Sobrienname_compare (first, second)
167977298Sobrien     char *first;
168077298Sobrien     char *second;
168160484Sobrien{
168277298Sobrien  char *copy1;
168377298Sobrien  char *copy2;
168477298Sobrien  int result;
168577298Sobrien
168660484Sobrien  copy1 = xmalloc (strlen (first) + 1);
168760484Sobrien  copy2 = xmalloc (strlen (second) + 1);
168860484Sobrien
168960484Sobrien  /* Convert the names to lower case.  */
169060484Sobrien  stricpy (copy1, first);
169160484Sobrien  stricpy (copy2, second);
169260484Sobrien
169360484Sobrien  /* Remove and endian strings from the name.  */
169460484Sobrien  strcut (copy1, "big");
169560484Sobrien  strcut (copy1, "little");
169660484Sobrien  strcut (copy2, "big");
169760484Sobrien  strcut (copy2, "little");
169860484Sobrien
169960484Sobrien  /* Return a value based on how many characters match,
170060484Sobrien     starting from the beginning.   If both strings are
170160484Sobrien     the same then return 10 * their length.  */
170277298Sobrien  for (result = 0; copy1[result] == copy2[result]; result++)
170377298Sobrien    if (copy1[result] == 0)
170460484Sobrien      {
170560484Sobrien	result *= 10;
170660484Sobrien	break;
170760484Sobrien      }
170877298Sobrien
170960484Sobrien  free (copy1);
171060484Sobrien  free (copy2);
171160484Sobrien
171260484Sobrien  return result;
171360484Sobrien}
171460484Sobrien
171560484Sobrien/* Set by closest_target_match() below.  */
171677298Sobrienstatic const bfd_target *winner;
171760484Sobrien
171860484Sobrien/* Scan all the valid bfd targets looking for one that has the endianness
171960484Sobrien   requirement that was specified on the command line, and is the nearest
172060484Sobrien   match to the original output target.  */
172177298Sobrien
172260484Sobrienstatic int
172360484Sobrienclosest_target_match (target, data)
172477298Sobrien     const bfd_target *target;
172577298Sobrien     PTR data;
172660484Sobrien{
172777298Sobrien  const bfd_target *original = (const bfd_target *) data;
172877298Sobrien
172977298Sobrien  if (command_line.endian == ENDIAN_BIG
173077298Sobrien      && target->byteorder != BFD_ENDIAN_BIG)
173160484Sobrien    return 0;
173277298Sobrien
173377298Sobrien  if (command_line.endian == ENDIAN_LITTLE
173477298Sobrien      && target->byteorder != BFD_ENDIAN_LITTLE)
173560484Sobrien    return 0;
173660484Sobrien
173760484Sobrien  /* Must be the same flavour.  */
173860484Sobrien  if (target->flavour != original->flavour)
173960484Sobrien    return 0;
174060484Sobrien
174160484Sobrien  /* If we have not found a potential winner yet, then record this one.  */
174260484Sobrien  if (winner == NULL)
174333965Sjdp    {
174460484Sobrien      winner = target;
174560484Sobrien      return 0;
174633965Sjdp    }
174760484Sobrien
174860484Sobrien  /* Oh dear, we now have two potential candidates for a successful match.
174977298Sobrien     Compare their names and choose the better one.  */
175078828Sobrien  if (name_compare (target->name, original->name)
175178828Sobrien      > name_compare (winner->name, original->name))
175260484Sobrien    winner = target;
175360484Sobrien
175460484Sobrien  /* Keep on searching until wqe have checked them all.  */
175560484Sobrien  return 0;
175633965Sjdp}
175733965Sjdp
175860484Sobrien/* Return the BFD target format of the first input file.  */
175977298Sobrien
176060484Sobrienstatic char *
176160484Sobrienget_first_input_target ()
176260484Sobrien{
176377298Sobrien  char *target = NULL;
176460484Sobrien
176560484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (s)
176660484Sobrien    {
176760484Sobrien      if (s->header.type == lang_input_statement_enum
176860484Sobrien	  && s->real)
176960484Sobrien	{
177060484Sobrien	  ldfile_open_file (s);
177177298Sobrien
177260484Sobrien	  if (s->the_bfd != NULL
177360484Sobrien	      && bfd_check_format (s->the_bfd, bfd_object))
177460484Sobrien	    {
177560484Sobrien	      target = bfd_get_target (s->the_bfd);
177677298Sobrien
177760484Sobrien	      if (target != NULL)
177860484Sobrien		break;
177960484Sobrien	    }
178060484Sobrien	}
178160484Sobrien    }
178277298Sobrien
178360484Sobrien  return target;
178460484Sobrien}
178560484Sobrien
1786107492Sobrienconst char *
1787107492Sobrienlang_get_output_target ()
1788107492Sobrien{
1789107492Sobrien  const char *target;
1790107492Sobrien
1791107492Sobrien  /* Has the user told us which output format to use?  */
1792107492Sobrien  if (output_target != (char *) NULL)
1793107492Sobrien    return output_target;
1794107492Sobrien
1795107492Sobrien  /* No - has the current target been set to something other than
1796107492Sobrien     the default?  */
1797107492Sobrien  if (current_target != default_target)
1798107492Sobrien    return current_target;
1799107492Sobrien
1800107492Sobrien  /* No - can we determine the format of the first input file?  */
1801107492Sobrien  target = get_first_input_target ();
1802107492Sobrien  if (target != NULL)
1803107492Sobrien    return target;
1804107492Sobrien
1805107492Sobrien  /* Failed - use the default output target.  */
1806107492Sobrien  return default_target;
1807107492Sobrien}
1808107492Sobrien
180933965Sjdp/* Open the output file.  */
181033965Sjdp
181133965Sjdpstatic bfd *
181233965Sjdpopen_output (name)
181377298Sobrien     const char *name;
181433965Sjdp{
181577298Sobrien  bfd *output;
181633965Sjdp
1817107492Sobrien  output_target = lang_get_output_target ();
181860484Sobrien
181977298Sobrien  /* Has the user requested a particular endianness on the command
182077298Sobrien     line?  */
182160484Sobrien  if (command_line.endian != ENDIAN_UNSET)
182260484Sobrien    {
182377298Sobrien      const bfd_target *target;
182460484Sobrien      enum bfd_endian desired_endian;
182560484Sobrien
182660484Sobrien      /* Get the chosen target.  */
182777298Sobrien      target = bfd_search_for_target (get_target, (PTR) output_target);
182860484Sobrien
182977298Sobrien      /* If the target is not supported, we cannot do anything.  */
183077298Sobrien      if (target != NULL)
183160484Sobrien	{
183277298Sobrien	  if (command_line.endian == ENDIAN_BIG)
183377298Sobrien	    desired_endian = BFD_ENDIAN_BIG;
183460484Sobrien	  else
183577298Sobrien	    desired_endian = BFD_ENDIAN_LITTLE;
183677298Sobrien
183777298Sobrien	  /* See if the target has the wrong endianness.  This should
183877298Sobrien	     not happen if the linker script has provided big and
183977298Sobrien	     little endian alternatives, but some scrips don't do
184077298Sobrien	     this.  */
184177298Sobrien	  if (target->byteorder != desired_endian)
184260484Sobrien	    {
184377298Sobrien	      /* If it does, then see if the target provides
184477298Sobrien		 an alternative with the correct endianness.  */
184577298Sobrien	      if (target->alternative_target != NULL
184677298Sobrien		  && (target->alternative_target->byteorder == desired_endian))
184777298Sobrien		output_target = target->alternative_target->name;
184860484Sobrien	      else
184977298Sobrien		{
185077298Sobrien		  /* Try to find a target as similar as possible to
185177298Sobrien		     the default target, but which has the desired
185277298Sobrien		     endian characteristic.  */
185378828Sobrien		  (void) bfd_search_for_target (closest_target_match,
185478828Sobrien						(PTR) target);
185577298Sobrien
185677298Sobrien		  /* Oh dear - we could not find any targets that
185777298Sobrien		     satisfy our requirements.  */
185877298Sobrien		  if (winner == NULL)
185977298Sobrien		    einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
186077298Sobrien		  else
186177298Sobrien		    output_target = winner->name;
186277298Sobrien		}
186360484Sobrien	    }
186460484Sobrien	}
186560484Sobrien    }
186677298Sobrien
186733965Sjdp  output = bfd_openw (name, output_target);
186833965Sjdp
186933965Sjdp  if (output == (bfd *) NULL)
187033965Sjdp    {
187133965Sjdp      if (bfd_get_error () == bfd_error_invalid_target)
187260484Sobrien	einfo (_("%P%F: target %s not found\n"), output_target);
187360484Sobrien
187460484Sobrien      einfo (_("%P%F: cannot open output file %s: %E\n"), name);
187533965Sjdp    }
187633965Sjdp
187733965Sjdp  delete_output_file_on_failure = true;
187833965Sjdp
187977298Sobrien#if 0
188077298Sobrien  output->flags |= D_PAGED;
188177298Sobrien#endif
188233965Sjdp
188333965Sjdp  if (! bfd_set_format (output, bfd_object))
188460484Sobrien    einfo (_("%P%F:%s: can not make object file: %E\n"), name);
188533965Sjdp  if (! bfd_set_arch_mach (output,
188633965Sjdp			   ldfile_output_architecture,
188733965Sjdp			   ldfile_output_machine))
188860484Sobrien    einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
188933965Sjdp
189033965Sjdp  link_info.hash = bfd_link_hash_table_create (output);
189133965Sjdp  if (link_info.hash == (struct bfd_link_hash_table *) NULL)
189260484Sobrien    einfo (_("%P%F: can not create link hash table: %E\n"));
189333965Sjdp
189433965Sjdp  bfd_set_gp_size (output, g_switch_value);
189533965Sjdp  return output;
189633965Sjdp}
189733965Sjdp
189833965Sjdpstatic void
189933965Sjdpldlang_open_output (statement)
190077298Sobrien     lang_statement_union_type *statement;
190133965Sjdp{
190233965Sjdp  switch (statement->header.type)
190333965Sjdp    {
190433965Sjdp    case lang_output_statement_enum:
190533965Sjdp      ASSERT (output_bfd == (bfd *) NULL);
190633965Sjdp      output_bfd = open_output (statement->output_statement.name);
190733965Sjdp      ldemul_set_output_arch ();
190833965Sjdp      if (config.magic_demand_paged && !link_info.relocateable)
190933965Sjdp	output_bfd->flags |= D_PAGED;
191033965Sjdp      else
191133965Sjdp	output_bfd->flags &= ~D_PAGED;
191233965Sjdp      if (config.text_read_only)
191333965Sjdp	output_bfd->flags |= WP_TEXT;
191433965Sjdp      else
191533965Sjdp	output_bfd->flags &= ~WP_TEXT;
191633965Sjdp      if (link_info.traditional_format)
191733965Sjdp	output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
191833965Sjdp      else
191933965Sjdp	output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
192033965Sjdp      break;
192133965Sjdp
192233965Sjdp    case lang_target_statement_enum:
192333965Sjdp      current_target = statement->target_statement.target;
192433965Sjdp      break;
192533965Sjdp    default:
192633965Sjdp      break;
192733965Sjdp    }
192833965Sjdp}
192933965Sjdp
193033965Sjdp/* Open all the input files.  */
193133965Sjdp
193233965Sjdpstatic void
193333965Sjdpopen_input_bfds (s, force)
193433965Sjdp     lang_statement_union_type *s;
193533965Sjdp     boolean force;
193633965Sjdp{
193789857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
193833965Sjdp    {
193933965Sjdp      switch (s->header.type)
194033965Sjdp	{
194133965Sjdp	case lang_constructors_statement_enum:
194233965Sjdp	  open_input_bfds (constructor_list.head, force);
194333965Sjdp	  break;
194433965Sjdp	case lang_output_section_statement_enum:
194533965Sjdp	  open_input_bfds (s->output_section_statement.children.head, force);
194633965Sjdp	  break;
194733965Sjdp	case lang_wild_statement_enum:
194877298Sobrien	  /* Maybe we should load the file's symbols.  */
194933965Sjdp	  if (s->wild_statement.filename
195033965Sjdp	      && ! wildcardp (s->wild_statement.filename))
195133965Sjdp	    (void) lookup_name (s->wild_statement.filename);
195233965Sjdp	  open_input_bfds (s->wild_statement.children.head, force);
195333965Sjdp	  break;
195433965Sjdp	case lang_group_statement_enum:
195533965Sjdp	  {
195633965Sjdp	    struct bfd_link_hash_entry *undefs;
195733965Sjdp
195833965Sjdp	    /* We must continually search the entries in the group
195977298Sobrien	       until no new symbols are added to the list of undefined
196077298Sobrien	       symbols.  */
196133965Sjdp
196233965Sjdp	    do
196333965Sjdp	      {
196433965Sjdp		undefs = link_info.hash->undefs_tail;
196533965Sjdp		open_input_bfds (s->group_statement.children.head, true);
196633965Sjdp	      }
196733965Sjdp	    while (undefs != link_info.hash->undefs_tail);
196833965Sjdp	  }
196933965Sjdp	  break;
197033965Sjdp	case lang_target_statement_enum:
197133965Sjdp	  current_target = s->target_statement.target;
197233965Sjdp	  break;
197333965Sjdp	case lang_input_statement_enum:
197460484Sobrien	  if (s->input_statement.real)
197533965Sjdp	    {
197633965Sjdp	      lang_statement_list_type add;
197733965Sjdp
197833965Sjdp	      s->input_statement.target = current_target;
197933965Sjdp
198033965Sjdp	      /* If we are being called from within a group, and this
198133965Sjdp                 is an archive which has already been searched, then
198277298Sobrien                 force it to be researched unless the whole archive
198377298Sobrien		 has been loaded already.  */
198433965Sjdp	      if (force
198577298Sobrien		  && !s->input_statement.whole_archive
198633965Sjdp		  && s->input_statement.loaded
198733965Sjdp		  && bfd_check_format (s->input_statement.the_bfd,
198833965Sjdp				       bfd_archive))
198933965Sjdp		s->input_statement.loaded = false;
199033965Sjdp
199133965Sjdp	      lang_list_init (&add);
199233965Sjdp
199389857Sobrien	      if (! load_symbols (&s->input_statement, &add))
199489857Sobrien		config.make_executable = false;
199533965Sjdp
199633965Sjdp	      if (add.head != NULL)
199733965Sjdp		{
199889857Sobrien		  *add.tail = s->header.next;
199989857Sobrien		  s->header.next = add.head;
200033965Sjdp		}
200133965Sjdp	    }
200233965Sjdp	  break;
200333965Sjdp	default:
200433965Sjdp	  break;
200533965Sjdp	}
200633965Sjdp    }
200733965Sjdp}
200833965Sjdp
200977298Sobrien/* If there are [COMMONS] statements, put a wild one into the bss
201077298Sobrien   section.  */
201133965Sjdp
201233965Sjdpstatic void
201333965Sjdplang_reasonable_defaults ()
201433965Sjdp{
201533965Sjdp#if 0
201633965Sjdp  lang_output_section_statement_lookup (".text");
201733965Sjdp  lang_output_section_statement_lookup (".data");
201833965Sjdp
201977298Sobrien  default_common_section = lang_output_section_statement_lookup (".bss");
202033965Sjdp
2021107492Sobrien  if (!placed_commons)
202233965Sjdp    {
202333965Sjdp      lang_wild_statement_type *new =
202433965Sjdp      new_stat (lang_wild_statement,
202533965Sjdp		&default_common_section->children);
202633965Sjdp
202733965Sjdp      new->section_name = "COMMON";
202833965Sjdp      new->filename = (char *) NULL;
202933965Sjdp      lang_list_init (&new->children);
203033965Sjdp    }
203133965Sjdp#endif
203233965Sjdp}
203333965Sjdp
203477298Sobrien/* Add the supplied name to the symbol table as an undefined reference.
203594536Sobrien   This is a two step process as the symbol table doesn't even exist at
203694536Sobrien   the time the ld command line is processed.  First we put the name
203794536Sobrien   on a list, then, once the output file has been opened, transfer the
203894536Sobrien   name to the symbol table.  */
203994536Sobrien
2040104834Sobrientypedef struct bfd_sym_chain ldlang_undef_chain_list_type;
204133965Sjdp
2042104834Sobrien#define ldlang_undef_chain_list_head entry_symbol.next
204333965Sjdp
204433965Sjdpvoid
204533965Sjdpldlang_add_undef (name)
204677298Sobrien     const char *const name;
204733965Sjdp{
204833965Sjdp  ldlang_undef_chain_list_type *new =
204933965Sjdp    ((ldlang_undef_chain_list_type *)
205033965Sjdp     stat_alloc (sizeof (ldlang_undef_chain_list_type)));
205133965Sjdp
205233965Sjdp  new->next = ldlang_undef_chain_list_head;
205333965Sjdp  ldlang_undef_chain_list_head = new;
205433965Sjdp
205578828Sobrien  new->name = xstrdup (name);
205694536Sobrien
205794536Sobrien  if (output_bfd != NULL)
205894536Sobrien    insert_undefined (new->name);
205933965Sjdp}
206033965Sjdp
206194536Sobrien/* Insert NAME as undefined in the symbol table.  */
206294536Sobrien
206394536Sobrienstatic void
206494536Sobrieninsert_undefined (name)
206594536Sobrien     const char *name;
206694536Sobrien{
206794536Sobrien  struct bfd_link_hash_entry *h;
206894536Sobrien
206994536Sobrien  h = bfd_link_hash_lookup (link_info.hash, name, true, false, true);
207094536Sobrien  if (h == (struct bfd_link_hash_entry *) NULL)
207194536Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
207294536Sobrien  if (h->type == bfd_link_hash_new)
207394536Sobrien    {
207494536Sobrien      h->type = bfd_link_hash_undefined;
207594536Sobrien      h->u.undef.abfd = NULL;
207694536Sobrien      bfd_link_add_undef (link_info.hash, h);
207794536Sobrien    }
207894536Sobrien}
207994536Sobrien
208033965Sjdp/* Run through the list of undefineds created above and place them
208133965Sjdp   into the linker hash table as undefined symbols belonging to the
208277298Sobrien   script file.  */
208377298Sobrien
208433965Sjdpstatic void
208533965Sjdplang_place_undefineds ()
208633965Sjdp{
208733965Sjdp  ldlang_undef_chain_list_type *ptr;
208833965Sjdp
208933965Sjdp  for (ptr = ldlang_undef_chain_list_head;
209033965Sjdp       ptr != (ldlang_undef_chain_list_type *) NULL;
209133965Sjdp       ptr = ptr->next)
209233965Sjdp    {
209394536Sobrien      insert_undefined (ptr->name);
209433965Sjdp    }
209533965Sjdp}
209633965Sjdp
209777298Sobrien/* Open input files and attatch to output sections.  */
209877298Sobrien
209933965Sjdpstatic void
210033965Sjdpmap_input_to_output_sections (s, target, output_section_statement)
210177298Sobrien     lang_statement_union_type *s;
210277298Sobrien     const char *target;
210377298Sobrien     lang_output_section_statement_type *output_section_statement;
210433965Sjdp{
210589857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
210633965Sjdp    {
210733965Sjdp      switch (s->header.type)
210833965Sjdp	{
210933965Sjdp	case lang_wild_statement_enum:
211089857Sobrien	  wild (&s->wild_statement, target, output_section_statement);
211133965Sjdp	  break;
211233965Sjdp	case lang_constructors_statement_enum:
211333965Sjdp	  map_input_to_output_sections (constructor_list.head,
211433965Sjdp					target,
211533965Sjdp					output_section_statement);
211633965Sjdp	  break;
211733965Sjdp	case lang_output_section_statement_enum:
211833965Sjdp	  map_input_to_output_sections (s->output_section_statement.children.head,
211933965Sjdp					target,
212033965Sjdp					&s->output_section_statement);
212133965Sjdp	  break;
212233965Sjdp	case lang_output_statement_enum:
212333965Sjdp	  break;
212433965Sjdp	case lang_target_statement_enum:
212533965Sjdp	  target = s->target_statement.target;
212633965Sjdp	  break;
212733965Sjdp	case lang_group_statement_enum:
212833965Sjdp	  map_input_to_output_sections (s->group_statement.children.head,
212933965Sjdp					target,
213033965Sjdp					output_section_statement);
213133965Sjdp	  break;
213233965Sjdp	case lang_fill_statement_enum:
213333965Sjdp	case lang_input_section_enum:
213433965Sjdp	case lang_object_symbols_statement_enum:
213533965Sjdp	case lang_data_statement_enum:
213633965Sjdp	case lang_reloc_statement_enum:
213733965Sjdp	case lang_padding_statement_enum:
213833965Sjdp	case lang_input_statement_enum:
213933965Sjdp	  if (output_section_statement != NULL
214033965Sjdp	      && output_section_statement->bfd_section == NULL)
214133965Sjdp	    init_os (output_section_statement);
214233965Sjdp	  break;
214333965Sjdp	case lang_assignment_statement_enum:
214433965Sjdp	  if (output_section_statement != NULL
214533965Sjdp	      && output_section_statement->bfd_section == NULL)
214633965Sjdp	    init_os (output_section_statement);
214733965Sjdp
214833965Sjdp	  /* Make sure that any sections mentioned in the assignment
214977298Sobrien	     are initialized.  */
215033965Sjdp	  exp_init_os (s->assignment_statement.exp);
215133965Sjdp	  break;
215233965Sjdp	case lang_afile_asection_pair_statement_enum:
215333965Sjdp	  FAIL ();
215433965Sjdp	  break;
215533965Sjdp	case lang_address_statement_enum:
215677298Sobrien	  /* Mark the specified section with the supplied address.  */
215733965Sjdp	  {
215833965Sjdp	    lang_output_section_statement_type *os =
215933965Sjdp	      lang_output_section_statement_lookup
216033965Sjdp		(s->address_statement.section_name);
216133965Sjdp
216233965Sjdp	    if (os->bfd_section == NULL)
216333965Sjdp	      init_os (os);
216433965Sjdp	    os->addr_tree = s->address_statement.address;
216533965Sjdp	  }
216633965Sjdp	  break;
216733965Sjdp	}
216833965Sjdp    }
216933965Sjdp}
217033965Sjdp
2171104834Sobrien/* An output section might have been removed after its statement was
2172104834Sobrien   added.  For example, ldemul_before_allocation can remove dynamic
2173104834Sobrien   sections if they turn out to be not needed.  Clean them up here.  */
2174104834Sobrien
217533965Sjdpstatic void
2176104834Sobrienstrip_excluded_output_sections ()
2177104834Sobrien{
2178104834Sobrien  lang_statement_union_type *u;
2179104834Sobrien
2180104834Sobrien  for (u = lang_output_section_statement.head;
2181104834Sobrien       u != NULL;
2182104834Sobrien       u = u->output_section_statement.next)
2183104834Sobrien    {
2184104834Sobrien      lang_output_section_statement_type *os;
2185104834Sobrien      asection *s;
2186104834Sobrien
2187104834Sobrien      os = &u->output_section_statement;
2188104834Sobrien      s = os->bfd_section;
2189104834Sobrien      if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2190104834Sobrien	{
2191104834Sobrien	  asection **p;
2192104834Sobrien
2193104834Sobrien	  os->bfd_section = NULL;
2194104834Sobrien
2195104834Sobrien	  for (p = &output_bfd->sections; *p; p = &(*p)->next)
2196104834Sobrien	    if (*p == s)
2197104834Sobrien	      {
2198104834Sobrien		bfd_section_list_remove (output_bfd, p);
2199104834Sobrien		output_bfd->section_count--;
2200104834Sobrien		break;
2201104834Sobrien	      }
2202104834Sobrien	}
2203104834Sobrien    }
2204104834Sobrien}
2205104834Sobrien
2206104834Sobrienstatic void
220733965Sjdpprint_output_section_statement (output_section_statement)
220877298Sobrien     lang_output_section_statement_type *output_section_statement;
220933965Sjdp{
221033965Sjdp  asection *section = output_section_statement->bfd_section;
221133965Sjdp  int len;
221233965Sjdp
221333965Sjdp  if (output_section_statement != abs_output_section)
221433965Sjdp    {
221533965Sjdp      minfo ("\n%s", output_section_statement->name);
221633965Sjdp
221733965Sjdp      if (section != NULL)
221833965Sjdp	{
221933965Sjdp	  print_dot = section->vma;
222033965Sjdp
222133965Sjdp	  len = strlen (output_section_statement->name);
222233965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
222333965Sjdp	    {
222433965Sjdp	      print_nl ();
222533965Sjdp	      len = 0;
222633965Sjdp	    }
222733965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
222833965Sjdp	    {
222933965Sjdp	      print_space ();
223033965Sjdp	      ++len;
223133965Sjdp	    }
223233965Sjdp
223333965Sjdp	  minfo ("0x%V %W", section->vma, section->_raw_size);
223433965Sjdp
223533965Sjdp	  if (output_section_statement->load_base != NULL)
223633965Sjdp	    {
223733965Sjdp	      bfd_vma addr;
223833965Sjdp
223933965Sjdp	      addr = exp_get_abs_int (output_section_statement->load_base, 0,
224033965Sjdp				      "load base", lang_final_phase_enum);
224160484Sobrien	      minfo (_(" load address 0x%V"), addr);
224233965Sjdp	    }
224333965Sjdp	}
224433965Sjdp
224533965Sjdp      print_nl ();
224633965Sjdp    }
224733965Sjdp
224833965Sjdp  print_statement_list (output_section_statement->children.head,
224933965Sjdp			output_section_statement);
225033965Sjdp}
225133965Sjdp
225233965Sjdpstatic void
225333965Sjdpprint_assignment (assignment, output_section)
225477298Sobrien     lang_assignment_statement_type *assignment;
225577298Sobrien     lang_output_section_statement_type *output_section;
225633965Sjdp{
225733965Sjdp  int i;
225833965Sjdp  etree_value_type result;
225933965Sjdp
226033965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
226133965Sjdp    print_space ();
226233965Sjdp
226333965Sjdp  result = exp_fold_tree (assignment->exp->assign.src, output_section,
226433965Sjdp			  lang_final_phase_enum, print_dot, &print_dot);
226560484Sobrien  if (result.valid_p)
2266104834Sobrien    {
2267104834Sobrien      const char *dst;
2268104834Sobrien      bfd_vma value;
2269104834Sobrien
2270104834Sobrien      value = result.value + result.section->bfd_section->vma;
2271104834Sobrien      dst = assignment->exp->assign.dst;
2272104834Sobrien
2273104834Sobrien      minfo ("0x%V", value);
2274104834Sobrien      if (dst[0] == '.' && dst[1] == 0)
2275104834Sobrien	print_dot = value;
2276104834Sobrien    }
227733965Sjdp  else
227833965Sjdp    {
227933965Sjdp      minfo ("*undef*   ");
228033965Sjdp#ifdef BFD64
228133965Sjdp      minfo ("        ");
228233965Sjdp#endif
228333965Sjdp    }
228433965Sjdp
228533965Sjdp  minfo ("                ");
228633965Sjdp
228733965Sjdp  exp_print_tree (assignment->exp);
228833965Sjdp
228933965Sjdp  print_nl ();
229033965Sjdp}
229133965Sjdp
229233965Sjdpstatic void
229333965Sjdpprint_input_statement (statm)
229477298Sobrien     lang_input_statement_type *statm;
229533965Sjdp{
229633965Sjdp  if (statm->filename != (char *) NULL)
229733965Sjdp    {
229833965Sjdp      fprintf (config.map_file, "LOAD %s\n", statm->filename);
229933965Sjdp    }
230033965Sjdp}
230133965Sjdp
230233965Sjdp/* Print all symbols defined in a particular section.  This is called
230333965Sjdp   via bfd_link_hash_traverse.  */
230433965Sjdp
230577298Sobrienstatic boolean
230633965Sjdpprint_one_symbol (hash_entry, ptr)
230733965Sjdp     struct bfd_link_hash_entry *hash_entry;
230833965Sjdp     PTR ptr;
230933965Sjdp{
231033965Sjdp  asection *sec = (asection *) ptr;
231133965Sjdp
231233965Sjdp  if ((hash_entry->type == bfd_link_hash_defined
231333965Sjdp       || hash_entry->type == bfd_link_hash_defweak)
231433965Sjdp      && sec == hash_entry->u.def.section)
231533965Sjdp    {
231633965Sjdp      int i;
231733965Sjdp
231833965Sjdp      for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
231933965Sjdp	print_space ();
232033965Sjdp      minfo ("0x%V   ",
232133965Sjdp	     (hash_entry->u.def.value
232233965Sjdp	      + hash_entry->u.def.section->output_offset
232333965Sjdp	      + hash_entry->u.def.section->output_section->vma));
232433965Sjdp
232533965Sjdp      minfo ("             %T\n", hash_entry->root.string);
232633965Sjdp    }
232733965Sjdp
232833965Sjdp  return true;
232933965Sjdp}
233033965Sjdp
233133965Sjdp/* Print information about an input section to the map file.  */
233233965Sjdp
233333965Sjdpstatic void
233433965Sjdpprint_input_section (in)
233577298Sobrien     lang_input_section_type *in;
233633965Sjdp{
233733965Sjdp  asection *i = in->section;
233833965Sjdp  bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
233977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
234060484Sobrien						ldfile_output_machine);
234133965Sjdp  if (size != 0)
234233965Sjdp    {
234333965Sjdp      print_space ();
234433965Sjdp
234533965Sjdp      minfo ("%s", i->name);
234633965Sjdp
234733965Sjdp      if (i->output_section != NULL)
234833965Sjdp	{
234933965Sjdp	  int len;
235033965Sjdp
235133965Sjdp	  len = 1 + strlen (i->name);
235233965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
235333965Sjdp	    {
235433965Sjdp	      print_nl ();
235533965Sjdp	      len = 0;
235633965Sjdp	    }
235733965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
235833965Sjdp	    {
235933965Sjdp	      print_space ();
236033965Sjdp	      ++len;
236133965Sjdp	    }
236233965Sjdp
236333965Sjdp	  minfo ("0x%V %W %B\n",
236460484Sobrien		 i->output_section->vma + i->output_offset, size / opb,
236533965Sjdp		 i->owner);
236633965Sjdp
236733965Sjdp	  if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
236833965Sjdp	    {
236933965Sjdp	      len = SECTION_NAME_MAP_LENGTH + 3;
237033965Sjdp#ifdef BFD64
237133965Sjdp	      len += 16;
237233965Sjdp#else
237333965Sjdp	      len += 8;
237433965Sjdp#endif
237533965Sjdp	      while (len > 0)
237633965Sjdp		{
237733965Sjdp		  print_space ();
237833965Sjdp		  --len;
237933965Sjdp		}
238033965Sjdp
238160484Sobrien	      minfo (_("%W (size before relaxing)\n"), i->_raw_size);
238233965Sjdp	    }
238333965Sjdp
238433965Sjdp	  bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
238533965Sjdp
238660484Sobrien	  print_dot = i->output_section->vma + i->output_offset + size / opb;
238733965Sjdp	}
238833965Sjdp    }
238933965Sjdp}
239033965Sjdp
239133965Sjdpstatic void
239233965Sjdpprint_fill_statement (fill)
239377298Sobrien     lang_fill_statement_type *fill;
239433965Sjdp{
2395104834Sobrien  size_t size;
2396104834Sobrien  unsigned char *p;
2397104834Sobrien  fputs (" FILL mask 0x", config.map_file);
2398104834Sobrien  for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2399104834Sobrien    fprintf (config.map_file, "%02x", *p);
2400104834Sobrien  fputs ("\n", config.map_file);
240133965Sjdp}
240233965Sjdp
240333965Sjdpstatic void
240433965Sjdpprint_data_statement (data)
240577298Sobrien     lang_data_statement_type *data;
240633965Sjdp{
240733965Sjdp  int i;
240833965Sjdp  bfd_vma addr;
240933965Sjdp  bfd_size_type size;
241033965Sjdp  const char *name;
241177298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
241260484Sobrien						ldfile_output_machine);
241333965Sjdp
241433965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
241533965Sjdp    print_space ();
241633965Sjdp
241733965Sjdp  addr = data->output_vma;
241833965Sjdp  if (data->output_section != NULL)
241933965Sjdp    addr += data->output_section->vma;
242033965Sjdp
242133965Sjdp  switch (data->type)
242233965Sjdp    {
242333965Sjdp    default:
242433965Sjdp      abort ();
242533965Sjdp    case BYTE:
242633965Sjdp      size = BYTE_SIZE;
242733965Sjdp      name = "BYTE";
242833965Sjdp      break;
242933965Sjdp    case SHORT:
243033965Sjdp      size = SHORT_SIZE;
243133965Sjdp      name = "SHORT";
243233965Sjdp      break;
243333965Sjdp    case LONG:
243433965Sjdp      size = LONG_SIZE;
243533965Sjdp      name = "LONG";
243633965Sjdp      break;
243733965Sjdp    case QUAD:
243833965Sjdp      size = QUAD_SIZE;
243933965Sjdp      name = "QUAD";
244033965Sjdp      break;
244138889Sjdp    case SQUAD:
244238889Sjdp      size = QUAD_SIZE;
244338889Sjdp      name = "SQUAD";
244438889Sjdp      break;
244533965Sjdp    }
244633965Sjdp
244733965Sjdp  minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
244833965Sjdp
244933965Sjdp  if (data->exp->type.node_class != etree_value)
245033965Sjdp    {
245133965Sjdp      print_space ();
245233965Sjdp      exp_print_tree (data->exp);
245333965Sjdp    }
245433965Sjdp
245533965Sjdp  print_nl ();
245633965Sjdp
245760484Sobrien  print_dot = addr + size / opb;
245860484Sobrien
245933965Sjdp}
246033965Sjdp
246133965Sjdp/* Print an address statement.  These are generated by options like
246233965Sjdp   -Ttext.  */
246333965Sjdp
246433965Sjdpstatic void
246533965Sjdpprint_address_statement (address)
246633965Sjdp     lang_address_statement_type *address;
246733965Sjdp{
246860484Sobrien  minfo (_("Address of section %s set to "), address->section_name);
246933965Sjdp  exp_print_tree (address->address);
247033965Sjdp  print_nl ();
247133965Sjdp}
247233965Sjdp
247333965Sjdp/* Print a reloc statement.  */
247433965Sjdp
247533965Sjdpstatic void
247633965Sjdpprint_reloc_statement (reloc)
247733965Sjdp     lang_reloc_statement_type *reloc;
247833965Sjdp{
247933965Sjdp  int i;
248033965Sjdp  bfd_vma addr;
248133965Sjdp  bfd_size_type size;
248277298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
248377298Sobrien						ldfile_output_machine);
248433965Sjdp
248533965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
248633965Sjdp    print_space ();
248733965Sjdp
248833965Sjdp  addr = reloc->output_vma;
248933965Sjdp  if (reloc->output_section != NULL)
249033965Sjdp    addr += reloc->output_section->vma;
249133965Sjdp
249233965Sjdp  size = bfd_get_reloc_size (reloc->howto);
249333965Sjdp
249433965Sjdp  minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
249533965Sjdp
249633965Sjdp  if (reloc->name != NULL)
249733965Sjdp    minfo ("%s+", reloc->name);
249833965Sjdp  else
249933965Sjdp    minfo ("%s+", reloc->section->name);
250033965Sjdp
250133965Sjdp  exp_print_tree (reloc->addend_exp);
250233965Sjdp
250333965Sjdp  print_nl ();
250433965Sjdp
250560484Sobrien  print_dot = addr + size / opb;
250677298Sobrien}
250733965Sjdp
250833965Sjdpstatic void
250933965Sjdpprint_padding_statement (s)
251033965Sjdp     lang_padding_statement_type *s;
251133965Sjdp{
251233965Sjdp  int len;
251333965Sjdp  bfd_vma addr;
251477298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
251577298Sobrien						ldfile_output_machine);
251633965Sjdp
251733965Sjdp  minfo (" *fill*");
251833965Sjdp
251933965Sjdp  len = sizeof " *fill*" - 1;
252033965Sjdp  while (len < SECTION_NAME_MAP_LENGTH)
252133965Sjdp    {
252233965Sjdp      print_space ();
252333965Sjdp      ++len;
252433965Sjdp    }
252533965Sjdp
252633965Sjdp  addr = s->output_offset;
252733965Sjdp  if (s->output_section != NULL)
252833965Sjdp    addr += s->output_section->vma;
2529104834Sobrien  minfo ("0x%V %W ", addr, s->size);
253033965Sjdp
2531104834Sobrien  if (s->fill->size != 0)
2532104834Sobrien    {
2533104834Sobrien      size_t size;
2534104834Sobrien      unsigned char *p;
2535104834Sobrien      for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2536104834Sobrien	fprintf (config.map_file, "%02x", *p);
2537104834Sobrien    }
253833965Sjdp
253933965Sjdp  print_nl ();
254033965Sjdp
254160484Sobrien  print_dot = addr + s->size / opb;
254233965Sjdp}
254333965Sjdp
254433965Sjdpstatic void
254533965Sjdpprint_wild_statement (w, os)
254677298Sobrien     lang_wild_statement_type *w;
254777298Sobrien     lang_output_section_statement_type *os;
254833965Sjdp{
254989857Sobrien  struct wildcard_list *sec;
255089857Sobrien
255133965Sjdp  print_space ();
255233965Sjdp
255360484Sobrien  if (w->filenames_sorted)
255460484Sobrien    minfo ("SORT(");
255577298Sobrien  if (w->filename != NULL)
255633965Sjdp    minfo ("%s", w->filename);
255733965Sjdp  else
255833965Sjdp    minfo ("*");
255960484Sobrien  if (w->filenames_sorted)
256060484Sobrien    minfo (")");
256133965Sjdp
256260484Sobrien  minfo ("(");
256389857Sobrien  for (sec = w->section_list; sec; sec = sec->next)
256489857Sobrien    {
256589857Sobrien      if (sec->spec.sorted)
256689857Sobrien	minfo ("SORT(");
256789857Sobrien      if (sec->spec.exclude_name_list != NULL)
256889857Sobrien	{
256989857Sobrien	  name_list *tmp;
2570102729Sobrien	  minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
257189857Sobrien	  for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2572102729Sobrien	    minfo (" %s", tmp->name);
2573102729Sobrien	  minfo (") ");
257489857Sobrien	}
257589857Sobrien      if (sec->spec.name != NULL)
257689857Sobrien	minfo ("%s", sec->spec.name);
257789857Sobrien      else
257889857Sobrien	minfo ("*");
257989857Sobrien      if (sec->spec.sorted)
258089857Sobrien	minfo (")");
2581102729Sobrien      if (sec->next)
2582102729Sobrien	minfo (" ");
258389857Sobrien    }
258460484Sobrien  minfo (")");
258533965Sjdp
258633965Sjdp  print_nl ();
258733965Sjdp
258833965Sjdp  print_statement_list (w->children.head, os);
258933965Sjdp}
259033965Sjdp
259133965Sjdp/* Print a group statement.  */
259233965Sjdp
259333965Sjdpstatic void
259433965Sjdpprint_group (s, os)
259533965Sjdp     lang_group_statement_type *s;
259633965Sjdp     lang_output_section_statement_type *os;
259733965Sjdp{
259833965Sjdp  fprintf (config.map_file, "START GROUP\n");
259933965Sjdp  print_statement_list (s->children.head, os);
260033965Sjdp  fprintf (config.map_file, "END GROUP\n");
260133965Sjdp}
260233965Sjdp
260333965Sjdp/* Print the list of statements in S.
260433965Sjdp   This can be called for any statement type.  */
260533965Sjdp
260633965Sjdpstatic void
260733965Sjdpprint_statement_list (s, os)
260833965Sjdp     lang_statement_union_type *s;
260933965Sjdp     lang_output_section_statement_type *os;
261033965Sjdp{
261133965Sjdp  while (s != NULL)
261233965Sjdp    {
261333965Sjdp      print_statement (s, os);
261489857Sobrien      s = s->header.next;
261533965Sjdp    }
261633965Sjdp}
261733965Sjdp
261833965Sjdp/* Print the first statement in statement list S.
261933965Sjdp   This can be called for any statement type.  */
262033965Sjdp
262133965Sjdpstatic void
262233965Sjdpprint_statement (s, os)
262333965Sjdp     lang_statement_union_type *s;
262433965Sjdp     lang_output_section_statement_type *os;
262533965Sjdp{
262633965Sjdp  switch (s->header.type)
262733965Sjdp    {
262833965Sjdp    default:
262960484Sobrien      fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
263033965Sjdp      FAIL ();
263133965Sjdp      break;
263233965Sjdp    case lang_constructors_statement_enum:
263333965Sjdp      if (constructor_list.head != NULL)
263433965Sjdp	{
263560484Sobrien	  if (constructors_sorted)
263660484Sobrien	    minfo (" SORT (CONSTRUCTORS)\n");
263760484Sobrien	  else
263860484Sobrien	    minfo (" CONSTRUCTORS\n");
263933965Sjdp	  print_statement_list (constructor_list.head, os);
264033965Sjdp	}
264133965Sjdp      break;
264233965Sjdp    case lang_wild_statement_enum:
264333965Sjdp      print_wild_statement (&s->wild_statement, os);
264433965Sjdp      break;
264533965Sjdp    case lang_address_statement_enum:
264633965Sjdp      print_address_statement (&s->address_statement);
264733965Sjdp      break;
264833965Sjdp    case lang_object_symbols_statement_enum:
264933965Sjdp      minfo (" CREATE_OBJECT_SYMBOLS\n");
265033965Sjdp      break;
265133965Sjdp    case lang_fill_statement_enum:
265233965Sjdp      print_fill_statement (&s->fill_statement);
265333965Sjdp      break;
265433965Sjdp    case lang_data_statement_enum:
265533965Sjdp      print_data_statement (&s->data_statement);
265633965Sjdp      break;
265733965Sjdp    case lang_reloc_statement_enum:
265833965Sjdp      print_reloc_statement (&s->reloc_statement);
265933965Sjdp      break;
266033965Sjdp    case lang_input_section_enum:
266133965Sjdp      print_input_section (&s->input_section);
266233965Sjdp      break;
266333965Sjdp    case lang_padding_statement_enum:
266433965Sjdp      print_padding_statement (&s->padding_statement);
266533965Sjdp      break;
266633965Sjdp    case lang_output_section_statement_enum:
266733965Sjdp      print_output_section_statement (&s->output_section_statement);
266833965Sjdp      break;
266933965Sjdp    case lang_assignment_statement_enum:
267033965Sjdp      print_assignment (&s->assignment_statement, os);
267133965Sjdp      break;
267233965Sjdp    case lang_target_statement_enum:
267333965Sjdp      fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
267433965Sjdp      break;
267533965Sjdp    case lang_output_statement_enum:
267633965Sjdp      minfo ("OUTPUT(%s", s->output_statement.name);
267733965Sjdp      if (output_target != NULL)
267833965Sjdp	minfo (" %s", output_target);
267933965Sjdp      minfo (")\n");
268033965Sjdp      break;
268133965Sjdp    case lang_input_statement_enum:
268233965Sjdp      print_input_statement (&s->input_statement);
268333965Sjdp      break;
268433965Sjdp    case lang_group_statement_enum:
268533965Sjdp      print_group (&s->group_statement, os);
268633965Sjdp      break;
268733965Sjdp    case lang_afile_asection_pair_statement_enum:
268833965Sjdp      FAIL ();
268933965Sjdp      break;
269033965Sjdp    }
269133965Sjdp}
269233965Sjdp
269333965Sjdpstatic void
269433965Sjdpprint_statements ()
269533965Sjdp{
269633965Sjdp  print_statement_list (statement_list.head, abs_output_section);
269733965Sjdp}
269833965Sjdp
269933965Sjdp/* Print the first N statements in statement list S to STDERR.
270033965Sjdp   If N == 0, nothing is printed.
270133965Sjdp   If N < 0, the entire list is printed.
270233965Sjdp   Intended to be called from GDB.  */
270333965Sjdp
270433965Sjdpvoid
270533965Sjdpdprint_statement (s, n)
270677298Sobrien     lang_statement_union_type *s;
270733965Sjdp     int n;
270833965Sjdp{
270933965Sjdp  FILE *map_save = config.map_file;
271033965Sjdp
271133965Sjdp  config.map_file = stderr;
271233965Sjdp
271333965Sjdp  if (n < 0)
271433965Sjdp    print_statement_list (s, abs_output_section);
271533965Sjdp  else
271633965Sjdp    {
271733965Sjdp      while (s && --n >= 0)
271833965Sjdp	{
271933965Sjdp	  print_statement (s, abs_output_section);
272089857Sobrien	  s = s->header.next;
272133965Sjdp	}
272233965Sjdp    }
272333965Sjdp
272433965Sjdp  config.map_file = map_save;
272533965Sjdp}
272633965Sjdp
272789857Sobrienstatic void
272889857Sobrieninsert_pad (ptr, fill, alignment_needed, output_section, dot)
272989857Sobrien     lang_statement_union_type **ptr;
2730104834Sobrien     fill_type *fill;
273189857Sobrien     unsigned int alignment_needed;
273289857Sobrien     asection *output_section;
273333965Sjdp     bfd_vma dot;
273433965Sjdp{
2735104834Sobrien  static fill_type zero_fill = { 1, { 0 } };
273689857Sobrien  lang_statement_union_type *pad;
273733965Sjdp
273889857Sobrien  pad = ((lang_statement_union_type *)
273989857Sobrien	 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
274089857Sobrien  if (ptr != &statement_list.head
274189857Sobrien      && pad->header.type == lang_padding_statement_enum
274289857Sobrien      && pad->padding_statement.output_section == output_section)
274333965Sjdp    {
274489857Sobrien      /* Use the existing pad statement.  The above test on output
274589857Sobrien	 section is probably redundant, but it doesn't hurt to check.  */
274633965Sjdp    }
274789857Sobrien  else
274833965Sjdp    {
274989857Sobrien      /* Make a new padding statement, linked into existing chain.  */
275089857Sobrien      pad = ((lang_statement_union_type *)
275189857Sobrien	     stat_alloc (sizeof (lang_padding_statement_type)));
275289857Sobrien      pad->header.next = *ptr;
275389857Sobrien      *ptr = pad;
275489857Sobrien      pad->header.type = lang_padding_statement_enum;
275589857Sobrien      pad->padding_statement.output_section = output_section;
2756104834Sobrien      if (fill == (fill_type *) 0)
2757104834Sobrien	fill = &zero_fill;
275889857Sobrien      pad->padding_statement.fill = fill;
275933965Sjdp    }
276089857Sobrien  pad->padding_statement.output_offset = dot - output_section->vma;
276189857Sobrien  pad->padding_statement.size = alignment_needed;
276289857Sobrien  output_section->_raw_size += alignment_needed;
276333965Sjdp}
276433965Sjdp
276577298Sobrien/* Work out how much this section will move the dot point.  */
276677298Sobrien
276733965Sjdpstatic bfd_vma
276889857Sobriensize_input_section (this_ptr, output_section_statement, fill, dot)
276977298Sobrien     lang_statement_union_type **this_ptr;
277077298Sobrien     lang_output_section_statement_type *output_section_statement;
2771104834Sobrien     fill_type *fill;
277233965Sjdp     bfd_vma dot;
277333965Sjdp{
277433965Sjdp  lang_input_section_type *is = &((*this_ptr)->input_section);
277533965Sjdp  asection *i = is->section;
277633965Sjdp
2777107492Sobrien  if (!is->ifile->just_syms_flag)
277833965Sjdp    {
277989857Sobrien      unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
278089857Sobrien						    ldfile_output_machine);
278189857Sobrien      unsigned int alignment_needed;
278289857Sobrien      asection *o;
278389857Sobrien
278489857Sobrien      /* Align this section first to the input sections requirement,
278589857Sobrien	 then to the output section's requirement.  If this alignment
278689857Sobrien	 is greater than any seen before, then record it too.  Perform
278789857Sobrien	 the alignment by inserting a magic 'padding' statement.  */
278889857Sobrien
278933965Sjdp      if (output_section_statement->subsection_alignment != -1)
279089857Sobrien	i->alignment_power = output_section_statement->subsection_alignment;
279133965Sjdp
279289857Sobrien      o = output_section_statement->bfd_section;
279389857Sobrien      if (o->alignment_power < i->alignment_power)
279489857Sobrien	o->alignment_power = i->alignment_power;
279533965Sjdp
279689857Sobrien      alignment_needed = align_power (dot, i->alignment_power) - dot;
279789857Sobrien
279889857Sobrien      if (alignment_needed != 0)
279989857Sobrien	{
280089857Sobrien	  insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
280189857Sobrien	  dot += alignment_needed;
280289857Sobrien	}
280389857Sobrien
280477298Sobrien      /* Remember where in the output section this input section goes.  */
280533965Sjdp
280689857Sobrien      i->output_offset = dot - o->vma;
280733965Sjdp
280877298Sobrien      /* Mark how big the output section must be to contain this now.  */
280933965Sjdp      if (i->_cooked_size != 0)
281060484Sobrien	dot += i->_cooked_size / opb;
281133965Sjdp      else
281260484Sobrien	dot += i->_raw_size / opb;
281389857Sobrien      o->_raw_size = (dot - o->vma) * opb;
281433965Sjdp    }
281533965Sjdp  else
281633965Sjdp    {
281733965Sjdp      i->output_offset = i->vma - output_section_statement->bfd_section->vma;
281833965Sjdp    }
281933965Sjdp
282033965Sjdp  return dot;
282133965Sjdp}
282233965Sjdp
282360484Sobrien#define IGNORE_SECTION(bfd, s) \
282478828Sobrien  (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD))	\
282578828Sobrien    != (SEC_ALLOC | SEC_LOAD))					\
282660484Sobrien   || bfd_section_size (bfd, s) == 0)
282760484Sobrien
282860484Sobrien/* Check to see if any allocated sections overlap with other allocated
282960484Sobrien   sections.  This can happen when the linker script specifically specifies
283060484Sobrien   the output section addresses of the two sections.  */
283177298Sobrien
283260484Sobrienstatic void
283360484Sobrienlang_check_section_addresses ()
283460484Sobrien{
283577298Sobrien  asection *s;
283660484Sobrien  unsigned opb = bfd_octets_per_byte (output_bfd);
283760484Sobrien
283860484Sobrien  /* Scan all sections in the output list.  */
283960484Sobrien  for (s = output_bfd->sections; s != NULL; s = s->next)
284060484Sobrien    {
284177298Sobrien      asection *os;
284277298Sobrien
284360484Sobrien      /* Ignore sections which are not loaded or which have no contents.  */
284460484Sobrien      if (IGNORE_SECTION (output_bfd, s))
284560484Sobrien	continue;
284677298Sobrien
284760484Sobrien      /* Once we reach section 's' stop our seach.  This prevents two
284860484Sobrien	 warning messages from being produced, one for 'section A overlaps
284960484Sobrien	 section B' and one for 'section B overlaps section A'.  */
285060484Sobrien      for (os = output_bfd->sections; os != s; os = os->next)
285160484Sobrien	{
285260484Sobrien	  bfd_vma s_start;
285360484Sobrien	  bfd_vma s_end;
285460484Sobrien	  bfd_vma os_start;
285560484Sobrien	  bfd_vma os_end;
285677298Sobrien
285760484Sobrien	  /* Only consider loadable sections with real contents.  */
285860484Sobrien	  if (IGNORE_SECTION (output_bfd, os))
285960484Sobrien	    continue;
286060484Sobrien
286160484Sobrien	  /* We must check the sections' LMA addresses not their
286260484Sobrien	     VMA addresses because overlay sections can have
286360484Sobrien	     overlapping VMAs but they must have distinct LMAs.  */
286460484Sobrien	  s_start  = bfd_section_lma (output_bfd, s);
286560484Sobrien	  os_start = bfd_section_lma (output_bfd, os);
286660484Sobrien	  s_end    = s_start  + bfd_section_size (output_bfd, s) / opb - 1;
286760484Sobrien	  os_end   = os_start + bfd_section_size (output_bfd, os) / opb - 1;
286877298Sobrien
286960484Sobrien	  /* Look for an overlap.  */
287060484Sobrien	  if ((s_end < os_start) || (s_start > os_end))
287160484Sobrien	    continue;
287277298Sobrien
287360484Sobrien	  einfo (
287460484Sobrien_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
287560484Sobrien		 s->name, s_start, s_end, os->name, os_start, os_end);
287677298Sobrien
287760484Sobrien	  /* Once we have found one overlap for this section,
287860484Sobrien	     stop looking for others.  */
287960484Sobrien	  break;
288060484Sobrien	}
288160484Sobrien    }
288260484Sobrien}
288360484Sobrien
288460484Sobrien/* Make sure the new address is within the region.  We explicitly permit the
288560484Sobrien   current address to be at the exact end of the region when the address is
288660484Sobrien   non-zero, in case the region is at the end of addressable memory and the
288777298Sobrien   calculation wraps around.  */
288860484Sobrien
288960484Sobrienstatic void
289060484Sobrienos_region_check (os, region, tree, base)
289177298Sobrien     lang_output_section_statement_type *os;
289277298Sobrien     struct memory_region_struct *region;
289377298Sobrien     etree_type *tree;
289477298Sobrien     bfd_vma base;
289560484Sobrien{
289660484Sobrien  if ((region->current < region->origin
289760484Sobrien       || (region->current - region->origin > region->length))
289860484Sobrien      && ((region->current != region->origin + region->length)
2899104834Sobrien	  || base == 0))
290060484Sobrien    {
290160484Sobrien      if (tree != (etree_type *) NULL)
2902104834Sobrien	{
2903104834Sobrien	  einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2904104834Sobrien		 region->current,
2905104834Sobrien		 os->bfd_section->owner,
2906104834Sobrien		 os->bfd_section->name,
2907104834Sobrien		 region->name);
2908104834Sobrien	}
290960484Sobrien      else
2910104834Sobrien	{
2911104834Sobrien	  einfo (_("%X%P: region %s is full (%B section %s)\n"),
2912104834Sobrien		 region->name,
2913104834Sobrien		 os->bfd_section->owner,
2914104834Sobrien		 os->bfd_section->name);
2915104834Sobrien	}
291660484Sobrien      /* Reset the region pointer.  */
291760484Sobrien      region->current = region->origin;
291860484Sobrien    }
291960484Sobrien}
292060484Sobrien
292133965Sjdp/* Set the sizes for all the output sections.  */
292233965Sjdp
2923104834Sobrienstatic bfd_vma
2924104834Sobrienlang_size_sections_1 (s, output_section_statement, prev, fill, dot, relax)
292577298Sobrien     lang_statement_union_type *s;
292677298Sobrien     lang_output_section_statement_type *output_section_statement;
292777298Sobrien     lang_statement_union_type **prev;
2928104834Sobrien     fill_type *fill;
292933965Sjdp     bfd_vma dot;
293089857Sobrien     boolean *relax;
293133965Sjdp{
293277298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
293360484Sobrien						ldfile_output_machine);
293460484Sobrien
293560484Sobrien  /* Size up the sections from their constituent parts.  */
293689857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
293733965Sjdp    {
293860484Sobrien      switch (s->header.type)
293960484Sobrien	{
294060484Sobrien	case lang_output_section_statement_enum:
294160484Sobrien	  {
294260484Sobrien	    bfd_vma after;
294378828Sobrien	    lang_output_section_statement_type *os;
294433965Sjdp
294578828Sobrien	    os = &s->output_section_statement;
294660484Sobrien	    if (os->bfd_section == NULL)
294760484Sobrien	      /* This section was never actually created.  */
294860484Sobrien	      break;
294933965Sjdp
295060484Sobrien	    /* If this is a COFF shared library section, use the size and
295160484Sobrien	       address from the input section.  FIXME: This is COFF
295260484Sobrien	       specific; it would be cleaner if there were some other way
295360484Sobrien	       to do this, but nothing simple comes to mind.  */
295460484Sobrien	    if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
295560484Sobrien	      {
295677298Sobrien		asection *input;
295733965Sjdp
295860484Sobrien		if (os->children.head == NULL
295989857Sobrien		    || os->children.head->header.next != NULL
296060484Sobrien		    || os->children.head->header.type != lang_input_section_enum)
296160484Sobrien		  einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
296260484Sobrien			 os->name);
296333965Sjdp
296460484Sobrien		input = os->children.head->input_section.section;
296560484Sobrien		bfd_set_section_vma (os->bfd_section->owner,
296660484Sobrien				     os->bfd_section,
296760484Sobrien				     bfd_section_vma (input->owner, input));
296860484Sobrien		os->bfd_section->_raw_size = input->_raw_size;
296960484Sobrien		break;
297060484Sobrien	      }
297133965Sjdp
297260484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
297360484Sobrien	      {
297460484Sobrien		/* No matter what happens, an abs section starts at zero.  */
297560484Sobrien		ASSERT (os->bfd_section->vma == 0);
297660484Sobrien	      }
297760484Sobrien	    else
297860484Sobrien	      {
297960484Sobrien		if (os->addr_tree == (etree_type *) NULL)
298060484Sobrien		  {
298160484Sobrien		    /* No address specified for this section, get one
298260484Sobrien		       from the region specification.  */
298360484Sobrien		    if (os->region == (lang_memory_region_type *) NULL
298460484Sobrien			|| (((bfd_get_section_flags (output_bfd, os->bfd_section)
298560484Sobrien			      & (SEC_ALLOC | SEC_LOAD)) != 0)
298660484Sobrien			    && os->region->name[0] == '*'
298760484Sobrien			    && strcmp (os->region->name, "*default*") == 0))
298860484Sobrien		      {
298960484Sobrien			os->region = lang_memory_default (os->bfd_section);
299060484Sobrien		      }
299133965Sjdp
299260484Sobrien		    /* If a loadable section is using the default memory
299360484Sobrien		       region, and some non default memory regions were
299460484Sobrien		       defined, issue a warning.  */
299560484Sobrien		    if ((bfd_get_section_flags (output_bfd, os->bfd_section)
299660484Sobrien			 & (SEC_ALLOC | SEC_LOAD)) != 0
299799461Sobrien			&& (bfd_get_section_flags (output_bfd, os->bfd_section)
299899461Sobrien			    & SEC_NEVER_LOAD) == 0
299960484Sobrien			&& ! link_info.relocateable
300060484Sobrien			&& strcmp (os->region->name, "*default*") == 0
300160484Sobrien			&& lang_memory_region_list != NULL
300278828Sobrien			&& (strcmp (lang_memory_region_list->name,
300378828Sobrien				    "*default*") != 0
300460484Sobrien			    || lang_memory_region_list->next != NULL))
300560484Sobrien		      einfo (_("%P: warning: no memory region specified for section `%s'\n"),
300678828Sobrien			     bfd_get_section_name (output_bfd,
300778828Sobrien						   os->bfd_section));
300838889Sjdp
300960484Sobrien		    dot = os->region->current;
301077298Sobrien
301160484Sobrien		    if (os->section_alignment == -1)
301260484Sobrien		      {
301360484Sobrien			bfd_vma olddot;
301438889Sjdp
301560484Sobrien			olddot = dot;
301678828Sobrien			dot = align_power (dot,
301778828Sobrien					   os->bfd_section->alignment_power);
301833965Sjdp
301960484Sobrien			if (dot != olddot && config.warn_section_align)
302060484Sobrien			  einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
302160484Sobrien				 os->name, (unsigned int) (dot - olddot));
302260484Sobrien		      }
302360484Sobrien		  }
302460484Sobrien		else
302560484Sobrien		  {
302660484Sobrien		    etree_value_type r;
302733965Sjdp
302860484Sobrien		    r = exp_fold_tree (os->addr_tree,
302960484Sobrien				       abs_output_section,
303060484Sobrien				       lang_allocating_phase_enum,
303160484Sobrien				       dot, &dot);
3032107492Sobrien		    if (!r.valid_p)
3033107492Sobrien		      einfo (_("%F%S: non constant address expression for section %s\n"),
3034107492Sobrien			     os->name);
3035107492Sobrien
303660484Sobrien		    dot = r.value + r.section->bfd_section->vma;
303760484Sobrien		  }
303877298Sobrien
303960484Sobrien		/* The section starts here.
304060484Sobrien		   First, align to what the section needs.  */
304133965Sjdp
304260484Sobrien		if (os->section_alignment != -1)
304360484Sobrien		  dot = align_power (dot, os->section_alignment);
304433965Sjdp
304560484Sobrien		bfd_set_section_vma (0, os->bfd_section, dot);
304677298Sobrien
304760484Sobrien		os->bfd_section->output_offset = 0;
304860484Sobrien	      }
304933965Sjdp
3050104834Sobrien	    lang_size_sections_1 (os->children.head, os, &os->children.head,
3051104834Sobrien				  os->fill, dot, relax);
305277298Sobrien
305377298Sobrien	    /* Put the section within the requested block size, or
305477298Sobrien	       align at the block boundary.  */
3055104834Sobrien	    after = align_n (os->bfd_section->vma
305660484Sobrien			     + os->bfd_section->_raw_size / opb,
305760484Sobrien			     (bfd_vma) os->block_value);
305833965Sjdp
305960484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
306060484Sobrien	      ASSERT (after == os->bfd_section->vma);
3061104834Sobrien	    else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
3062104834Sobrien		     && (os->bfd_section->flags & SEC_THREAD_LOCAL)
3063104834Sobrien		     && ! link_info.relocateable)
3064104834Sobrien	      os->bfd_section->_raw_size = 0;
306560484Sobrien	    else
306677298Sobrien	      os->bfd_section->_raw_size =
306777298Sobrien		(after - os->bfd_section->vma) * opb;
3068104834Sobrien
306960484Sobrien	    dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
307060484Sobrien	    os->processed = true;
307133965Sjdp
3072104834Sobrien	    if (os->update_dot_tree != 0)
3073104834Sobrien	      exp_fold_tree (os->update_dot_tree, abs_output_section,
3074104834Sobrien			     lang_allocating_phase_enum, dot, &dot);
3075104834Sobrien
307660484Sobrien	    /* Update dot in the region ?
307760484Sobrien	       We only do this if the section is going to be allocated,
307860484Sobrien	       since unallocated sections do not contribute to the region's
307960484Sobrien	       overall size in memory.
308077298Sobrien
308160484Sobrien	       If the SEC_NEVER_LOAD bit is not set, it will affect the
308260484Sobrien	       addresses of sections after it. We have to update
308360484Sobrien	       dot.  */
308460484Sobrien	    if (os->region != (lang_memory_region_type *) NULL
308560484Sobrien		&& ((bfd_get_section_flags (output_bfd, os->bfd_section)
308660484Sobrien		     & SEC_NEVER_LOAD) == 0
308760484Sobrien		    || (bfd_get_section_flags (output_bfd, os->bfd_section)
308860484Sobrien			& (SEC_ALLOC | SEC_LOAD))))
308960484Sobrien	      {
309060484Sobrien		os->region->current = dot;
309177298Sobrien
309260484Sobrien		/* Make sure the new address is within the region.  */
309377298Sobrien		os_region_check (os, os->region, os->addr_tree,
309477298Sobrien				 os->bfd_section->vma);
309533965Sjdp
309677298Sobrien		/* If there's no load address specified, use the run
309777298Sobrien		   region as the load region.  */
309877298Sobrien		if (os->lma_region == NULL && os->load_base == NULL)
309977298Sobrien		  os->lma_region = os->region;
310033965Sjdp
3101104834Sobrien		if (os->lma_region != NULL && os->lma_region != os->region)
310277298Sobrien		  {
3103104834Sobrien		    /* Set load_base, which will be handled later.  */
3104104834Sobrien		    os->load_base = exp_intop (os->lma_region->current);
3105104834Sobrien		    os->lma_region->current +=
3106104834Sobrien		      os->bfd_section->_raw_size / opb;
3107104834Sobrien		    os_region_check (os, os->lma_region, NULL,
3108104834Sobrien				     os->bfd_section->lma);
310977298Sobrien		  }
311060484Sobrien	      }
311160484Sobrien	  }
311260484Sobrien	  break;
311333965Sjdp
311460484Sobrien	case lang_constructors_statement_enum:
3115104834Sobrien	  dot = lang_size_sections_1 (constructor_list.head,
3116104834Sobrien				      output_section_statement,
3117104834Sobrien				      &s->wild_statement.children.head,
3118104834Sobrien				      fill, dot, relax);
311960484Sobrien	  break;
312033965Sjdp
312160484Sobrien	case lang_data_statement_enum:
312260484Sobrien	  {
312360484Sobrien	    unsigned int size = 0;
312433965Sjdp
312577298Sobrien	    s->data_statement.output_vma =
312677298Sobrien	      dot - output_section_statement->bfd_section->vma;
312760484Sobrien	    s->data_statement.output_section =
312860484Sobrien	      output_section_statement->bfd_section;
312933965Sjdp
313060484Sobrien	    switch (s->data_statement.type)
313160484Sobrien	      {
313277298Sobrien	      default:
313377298Sobrien		abort ();
313460484Sobrien	      case QUAD:
313560484Sobrien	      case SQUAD:
313660484Sobrien		size = QUAD_SIZE;
313760484Sobrien		break;
313860484Sobrien	      case LONG:
313960484Sobrien		size = LONG_SIZE;
314060484Sobrien		break;
314160484Sobrien	      case SHORT:
314260484Sobrien		size = SHORT_SIZE;
314360484Sobrien		break;
314460484Sobrien	      case BYTE:
314560484Sobrien		size = BYTE_SIZE;
314660484Sobrien		break;
314760484Sobrien	      }
314877298Sobrien	    if (size < opb)
314977298Sobrien	      size = opb;
315060484Sobrien	    dot += size / opb;
315160484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
315260484Sobrien	    /* The output section gets contents, and then we inspect for
315360484Sobrien	       any flags set in the input script which override any ALLOC.  */
315460484Sobrien	    output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
315577298Sobrien	    if (!(output_section_statement->flags & SEC_NEVER_LOAD))
315677298Sobrien	      {
315777298Sobrien		output_section_statement->bfd_section->flags |=
315877298Sobrien		  SEC_ALLOC | SEC_LOAD;
315977298Sobrien	      }
316060484Sobrien	  }
316160484Sobrien	  break;
316233965Sjdp
316360484Sobrien	case lang_reloc_statement_enum:
316460484Sobrien	  {
316560484Sobrien	    int size;
316633965Sjdp
316760484Sobrien	    s->reloc_statement.output_vma =
316860484Sobrien	      dot - output_section_statement->bfd_section->vma;
316960484Sobrien	    s->reloc_statement.output_section =
317060484Sobrien	      output_section_statement->bfd_section;
317160484Sobrien	    size = bfd_get_reloc_size (s->reloc_statement.howto);
317260484Sobrien	    dot += size / opb;
317360484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
317460484Sobrien	  }
317560484Sobrien	  break;
317677298Sobrien
317760484Sobrien	case lang_wild_statement_enum:
317833965Sjdp
3179104834Sobrien	  dot = lang_size_sections_1 (s->wild_statement.children.head,
3180104834Sobrien				      output_section_statement,
3181104834Sobrien				      &s->wild_statement.children.head,
3182104834Sobrien				      fill, dot, relax);
318333965Sjdp
318460484Sobrien	  break;
318533965Sjdp
318660484Sobrien	case lang_object_symbols_statement_enum:
318760484Sobrien	  link_info.create_object_symbols_section =
318860484Sobrien	    output_section_statement->bfd_section;
318960484Sobrien	  break;
319060484Sobrien	case lang_output_statement_enum:
319160484Sobrien	case lang_target_statement_enum:
319260484Sobrien	  break;
319360484Sobrien	case lang_input_section_enum:
319433965Sjdp	  {
319560484Sobrien	    asection *i;
319633965Sjdp
319760484Sobrien	    i = (*prev)->input_section.section;
319860484Sobrien	    if (! relax)
319960484Sobrien	      {
320060484Sobrien		if (i->_cooked_size == 0)
320160484Sobrien		  i->_cooked_size = i->_raw_size;
320260484Sobrien	      }
320360484Sobrien	    else
320460484Sobrien	      {
320560484Sobrien		boolean again;
320660484Sobrien
320760484Sobrien		if (! bfd_relax_section (i->owner, i, &link_info, &again))
320860484Sobrien		  einfo (_("%P%F: can't relax section: %E\n"));
320960484Sobrien		if (again)
321089857Sobrien		  *relax = true;
321160484Sobrien	      }
321289857Sobrien	    dot = size_input_section (prev, output_section_statement,
321389857Sobrien				      output_section_statement->fill, dot);
321433965Sjdp	  }
321560484Sobrien	  break;
321660484Sobrien	case lang_input_statement_enum:
321760484Sobrien	  break;
321860484Sobrien	case lang_fill_statement_enum:
321977298Sobrien	  s->fill_statement.output_section =
322077298Sobrien	    output_section_statement->bfd_section;
322133965Sjdp
322260484Sobrien	  fill = s->fill_statement.fill;
322360484Sobrien	  break;
322460484Sobrien	case lang_assignment_statement_enum:
322560484Sobrien	  {
322660484Sobrien	    bfd_vma newdot = dot;
322733965Sjdp
322860484Sobrien	    exp_fold_tree (s->assignment_statement.exp,
322960484Sobrien			   output_section_statement,
323060484Sobrien			   lang_allocating_phase_enum,
323160484Sobrien			   dot,
323260484Sobrien			   &newdot);
323333965Sjdp
323460484Sobrien	    if (newdot != dot)
323560484Sobrien	      {
323660484Sobrien		if (output_section_statement == abs_output_section)
323760484Sobrien		  {
323860484Sobrien		    /* If we don't have an output section, then just adjust
323960484Sobrien		       the default memory address.  */
324060484Sobrien		    lang_memory_region_lookup ("*default*")->current = newdot;
324160484Sobrien		  }
324289857Sobrien		else
324360484Sobrien		  {
324489857Sobrien		    /* Insert a pad after this statement.  We can't
324589857Sobrien		       put the pad before when relaxing, in case the
324689857Sobrien		       assignment references dot.  */
324789857Sobrien		    insert_pad (&s->header.next, fill, (newdot - dot) * opb,
324889857Sobrien				output_section_statement->bfd_section, dot);
324933965Sjdp
325089857Sobrien		    /* Don't neuter the pad below when relaxing.  */
325189857Sobrien		    s = s->header.next;
325260484Sobrien		  }
325333965Sjdp
325460484Sobrien		dot = newdot;
325560484Sobrien	      }
325660484Sobrien	  }
325760484Sobrien	  break;
325833965Sjdp
325960484Sobrien	case lang_padding_statement_enum:
326089857Sobrien	  /* If this is the first time lang_size_sections is called,
326189857Sobrien	     we won't have any padding statements.  If this is the
326289857Sobrien	     second or later passes when relaxing, we should allow
326389857Sobrien	     padding to shrink.  If padding is needed on this pass, it
326489857Sobrien	     will be added back in.  */
326589857Sobrien	  s->padding_statement.size = 0;
326689857Sobrien
326789857Sobrien	  /* Make sure output_offset is valid.  If relaxation shrinks
326889857Sobrien	     the section and this pad isn't needed, it's possible to
326989857Sobrien	     have output_offset larger than the final size of the
327089857Sobrien	     section.  bfd_set_section_contents will complain even for
327189857Sobrien	     a pad size of zero.  */
327289857Sobrien	  s->padding_statement.output_offset
327389857Sobrien	    = dot - output_section_statement->bfd_section->vma;
327460484Sobrien	  break;
327533965Sjdp
327660484Sobrien	case lang_group_statement_enum:
3277104834Sobrien	  dot = lang_size_sections_1 (s->group_statement.children.head,
3278104834Sobrien				      output_section_statement,
3279104834Sobrien				      &s->group_statement.children.head,
3280104834Sobrien				      fill, dot, relax);
328160484Sobrien	  break;
328233965Sjdp
328360484Sobrien	default:
328460484Sobrien	  FAIL ();
328560484Sobrien	  break;
328633965Sjdp
328789857Sobrien	  /* We can only get here when relaxing is turned on.  */
328860484Sobrien	case lang_address_statement_enum:
328960484Sobrien	  break;
329060484Sobrien	}
329160484Sobrien      prev = &s->header.next;
329233965Sjdp    }
329333965Sjdp  return dot;
329433965Sjdp}
329533965Sjdp
329633965Sjdpbfd_vma
3297104834Sobrienlang_size_sections (s, output_section_statement, prev, fill, dot, relax)
3298104834Sobrien     lang_statement_union_type *s;
3299104834Sobrien     lang_output_section_statement_type *output_section_statement;
3300104834Sobrien     lang_statement_union_type **prev;
3301104834Sobrien     fill_type *fill;
3302104834Sobrien     bfd_vma dot;
3303104834Sobrien     boolean *relax;
3304104834Sobrien{
3305104834Sobrien  bfd_vma result;
3306104834Sobrien
3307104834Sobrien  exp_data_seg.phase = exp_dataseg_none;
3308104834Sobrien  result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3309104834Sobrien				 dot, relax);
3310104834Sobrien  if (exp_data_seg.phase == exp_dataseg_end_seen)
3311104834Sobrien    {
3312104834Sobrien      /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3313104834Sobrien	 a page could be saved in the data segment.  */
3314104834Sobrien      bfd_vma first, last;
3315104834Sobrien
3316104834Sobrien      first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3317104834Sobrien      last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3318104834Sobrien      if (first && last
3319104834Sobrien	  && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3320104834Sobrien	      != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3321104834Sobrien	  && first + last <= exp_data_seg.pagesize)
3322104834Sobrien	{
3323104834Sobrien	  exp_data_seg.phase = exp_dataseg_adjust;
3324104834Sobrien	  result = lang_size_sections_1 (s, output_section_statement, prev,
3325104834Sobrien					 fill, dot, relax);
3326104834Sobrien	}
3327104834Sobrien    }
3328104834Sobrien
3329104834Sobrien  return result;
3330104834Sobrien}
3331104834Sobrien
3332104834Sobrienbfd_vma
333333965Sjdplang_do_assignments (s, output_section_statement, fill, dot)
333477298Sobrien     lang_statement_union_type *s;
333577298Sobrien     lang_output_section_statement_type *output_section_statement;
3336104834Sobrien     fill_type *fill;
333733965Sjdp     bfd_vma dot;
333833965Sjdp{
333977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
334077298Sobrien						ldfile_output_machine);
334160484Sobrien
334289857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
334333965Sjdp    {
334433965Sjdp      switch (s->header.type)
334533965Sjdp	{
334633965Sjdp	case lang_constructors_statement_enum:
334733965Sjdp	  dot = lang_do_assignments (constructor_list.head,
334833965Sjdp				     output_section_statement,
334933965Sjdp				     fill,
335033965Sjdp				     dot);
335133965Sjdp	  break;
335233965Sjdp
335333965Sjdp	case lang_output_section_statement_enum:
335433965Sjdp	  {
335578828Sobrien	    lang_output_section_statement_type *os;
335633965Sjdp
335778828Sobrien	    os = &(s->output_section_statement);
335833965Sjdp	    if (os->bfd_section != NULL)
335933965Sjdp	      {
336033965Sjdp		dot = os->bfd_section->vma;
336133965Sjdp		(void) lang_do_assignments (os->children.head, os,
336233965Sjdp					    os->fill, dot);
336360484Sobrien		dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
336460484Sobrien
336533965Sjdp	      }
336677298Sobrien	    if (os->load_base)
336733965Sjdp	      {
336833965Sjdp		/* If nothing has been placed into the output section then
336977298Sobrien		   it won't have a bfd_section.  */
337077298Sobrien		if (os->bfd_section)
337133965Sjdp		  {
337277298Sobrien		    os->bfd_section->lma
337377298Sobrien		      = exp_get_abs_int (os->load_base, 0, "load base",
337477298Sobrien					 lang_final_phase_enum);
337533965Sjdp		  }
337633965Sjdp	      }
337733965Sjdp	  }
337833965Sjdp	  break;
337933965Sjdp	case lang_wild_statement_enum:
338033965Sjdp
338133965Sjdp	  dot = lang_do_assignments (s->wild_statement.children.head,
338233965Sjdp				     output_section_statement,
338333965Sjdp				     fill, dot);
338433965Sjdp
338533965Sjdp	  break;
338633965Sjdp
338733965Sjdp	case lang_object_symbols_statement_enum:
338833965Sjdp	case lang_output_statement_enum:
338933965Sjdp	case lang_target_statement_enum:
339033965Sjdp#if 0
339133965Sjdp	case lang_common_statement_enum:
339233965Sjdp#endif
339333965Sjdp	  break;
339433965Sjdp	case lang_data_statement_enum:
339533965Sjdp	  {
339633965Sjdp	    etree_value_type value;
339733965Sjdp
339833965Sjdp	    value = exp_fold_tree (s->data_statement.exp,
339933965Sjdp				   abs_output_section,
340033965Sjdp				   lang_final_phase_enum, dot, &dot);
340133965Sjdp	    s->data_statement.value = value.value;
3402107492Sobrien	    if (!value.valid_p)
340360484Sobrien	      einfo (_("%F%P: invalid data statement\n"));
340433965Sjdp	  }
3405104834Sobrien	  {
3406104834Sobrien	    unsigned int size;
340777298Sobrien	    switch (s->data_statement.type)
340877298Sobrien	      {
340977298Sobrien	      default:
341077298Sobrien		abort ();
341177298Sobrien	      case QUAD:
341277298Sobrien	      case SQUAD:
341377298Sobrien		size = QUAD_SIZE;
341477298Sobrien		break;
341577298Sobrien	      case LONG:
341677298Sobrien		size = LONG_SIZE;
341777298Sobrien		break;
341877298Sobrien	      case SHORT:
341977298Sobrien		size = SHORT_SIZE;
342077298Sobrien		break;
342177298Sobrien	      case BYTE:
342277298Sobrien		size = BYTE_SIZE;
342377298Sobrien		break;
342477298Sobrien	      }
342577298Sobrien	    if (size < opb)
342677298Sobrien	      size = opb;
342777298Sobrien	    dot += size / opb;
342877298Sobrien	  }
342933965Sjdp	  break;
343033965Sjdp
343133965Sjdp	case lang_reloc_statement_enum:
343233965Sjdp	  {
343333965Sjdp	    etree_value_type value;
343433965Sjdp
343533965Sjdp	    value = exp_fold_tree (s->reloc_statement.addend_exp,
343633965Sjdp				   abs_output_section,
343733965Sjdp				   lang_final_phase_enum, dot, &dot);
343833965Sjdp	    s->reloc_statement.addend_value = value.value;
3439107492Sobrien	    if (!value.valid_p)
344060484Sobrien	      einfo (_("%F%P: invalid reloc statement\n"));
344133965Sjdp	  }
344260484Sobrien	  dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
344333965Sjdp	  break;
344433965Sjdp
344533965Sjdp	case lang_input_section_enum:
344633965Sjdp	  {
344733965Sjdp	    asection *in = s->input_section.section;
344833965Sjdp
344933965Sjdp	    if (in->_cooked_size != 0)
345060484Sobrien	      dot += in->_cooked_size / opb;
345133965Sjdp	    else
345260484Sobrien	      dot += in->_raw_size / opb;
345333965Sjdp	  }
345433965Sjdp	  break;
345533965Sjdp
345633965Sjdp	case lang_input_statement_enum:
345733965Sjdp	  break;
345833965Sjdp	case lang_fill_statement_enum:
345933965Sjdp	  fill = s->fill_statement.fill;
346033965Sjdp	  break;
346133965Sjdp	case lang_assignment_statement_enum:
346233965Sjdp	  {
346333965Sjdp	    exp_fold_tree (s->assignment_statement.exp,
346433965Sjdp			   output_section_statement,
346533965Sjdp			   lang_final_phase_enum,
346633965Sjdp			   dot,
346733965Sjdp			   &dot);
346833965Sjdp	  }
346933965Sjdp
347033965Sjdp	  break;
347133965Sjdp	case lang_padding_statement_enum:
347260484Sobrien	  dot += s->padding_statement.size / opb;
347333965Sjdp	  break;
347433965Sjdp
347533965Sjdp	case lang_group_statement_enum:
347633965Sjdp	  dot = lang_do_assignments (s->group_statement.children.head,
347733965Sjdp				     output_section_statement,
347833965Sjdp				     fill, dot);
347933965Sjdp
348033965Sjdp	  break;
348133965Sjdp
348233965Sjdp	default:
348333965Sjdp	  FAIL ();
348433965Sjdp	  break;
348533965Sjdp	case lang_address_statement_enum:
348633965Sjdp	  break;
348733965Sjdp	}
348833965Sjdp
348933965Sjdp    }
349033965Sjdp  return dot;
349133965Sjdp}
349233965Sjdp
349333965Sjdp/* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
349433965Sjdp   operator .startof. (section_name), it produces an undefined symbol
349533965Sjdp   .startof.section_name.  Similarly, when it sees
349633965Sjdp   .sizeof. (section_name), it produces an undefined symbol
349733965Sjdp   .sizeof.section_name.  For all the output sections, we look for
349833965Sjdp   such symbols, and set them to the correct value.  */
349933965Sjdp
350033965Sjdpstatic void
350133965Sjdplang_set_startof ()
350233965Sjdp{
350333965Sjdp  asection *s;
350433965Sjdp
350533965Sjdp  if (link_info.relocateable)
350633965Sjdp    return;
350733965Sjdp
350833965Sjdp  for (s = output_bfd->sections; s != NULL; s = s->next)
350933965Sjdp    {
351033965Sjdp      const char *secname;
351133965Sjdp      char *buf;
351233965Sjdp      struct bfd_link_hash_entry *h;
351333965Sjdp
351433965Sjdp      secname = bfd_get_section_name (output_bfd, s);
351533965Sjdp      buf = xmalloc (10 + strlen (secname));
351633965Sjdp
351733965Sjdp      sprintf (buf, ".startof.%s", secname);
351833965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
351933965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
352033965Sjdp	{
352133965Sjdp	  h->type = bfd_link_hash_defined;
352233965Sjdp	  h->u.def.value = bfd_get_section_vma (output_bfd, s);
352333965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
352433965Sjdp	}
352533965Sjdp
352633965Sjdp      sprintf (buf, ".sizeof.%s", secname);
352733965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
352833965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
352933965Sjdp	{
3530104834Sobrien	  unsigned opb;
353178828Sobrien
3532104834Sobrien	  opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
353378828Sobrien					       ldfile_output_machine);
353433965Sjdp	  h->type = bfd_link_hash_defined;
353533965Sjdp	  if (s->_cooked_size != 0)
353660484Sobrien	    h->u.def.value = s->_cooked_size / opb;
353733965Sjdp	  else
353860484Sobrien	    h->u.def.value = s->_raw_size / opb;
353933965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
354033965Sjdp	}
354133965Sjdp
354233965Sjdp      free (buf);
354333965Sjdp    }
354433965Sjdp}
354533965Sjdp
354633965Sjdpstatic void
354733965Sjdplang_finish ()
354833965Sjdp{
354933965Sjdp  struct bfd_link_hash_entry *h;
355033965Sjdp  boolean warn;
355133965Sjdp
355233965Sjdp  if (link_info.relocateable || link_info.shared)
355333965Sjdp    warn = false;
355433965Sjdp  else
355533965Sjdp    warn = true;
355633965Sjdp
3557104834Sobrien  if (entry_symbol.name == (const char *) NULL)
355833965Sjdp    {
355933965Sjdp      /* No entry has been specified.  Look for start, but don't warn
356033965Sjdp	 if we don't find it.  */
3561104834Sobrien      entry_symbol.name = "start";
356233965Sjdp      warn = false;
356333965Sjdp    }
356433965Sjdp
3565104834Sobrien  h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3566104834Sobrien			    false, false, true);
356733965Sjdp  if (h != (struct bfd_link_hash_entry *) NULL
356833965Sjdp      && (h->type == bfd_link_hash_defined
356933965Sjdp	  || h->type == bfd_link_hash_defweak)
357033965Sjdp      && h->u.def.section->output_section != NULL)
357133965Sjdp    {
357233965Sjdp      bfd_vma val;
357333965Sjdp
357433965Sjdp      val = (h->u.def.value
357533965Sjdp	     + bfd_get_section_vma (output_bfd,
357633965Sjdp				    h->u.def.section->output_section)
357733965Sjdp	     + h->u.def.section->output_offset);
357833965Sjdp      if (! bfd_set_start_address (output_bfd, val))
3579104834Sobrien	einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
358033965Sjdp    }
358133965Sjdp  else
358233965Sjdp    {
358360484Sobrien      bfd_vma val;
358477298Sobrien      const char *send;
358533965Sjdp
358660484Sobrien      /* We couldn't find the entry symbol.  Try parsing it as a
358760484Sobrien         number.  */
3588104834Sobrien      val = bfd_scan_vma (entry_symbol.name, &send, 0);
358960484Sobrien      if (*send == '\0')
359033965Sjdp	{
359160484Sobrien	  if (! bfd_set_start_address (output_bfd, val))
359260484Sobrien	    einfo (_("%P%F: can't set start address\n"));
359333965Sjdp	}
359433965Sjdp      else
359533965Sjdp	{
359660484Sobrien	  asection *ts;
359760484Sobrien
359860484Sobrien	  /* Can't find the entry symbol, and it's not a number.  Use
359960484Sobrien	     the first address in the text section.  */
360091041Sobrien	  ts = bfd_get_section_by_name (output_bfd, entry_section);
360160484Sobrien	  if (ts != (asection *) NULL)
360260484Sobrien	    {
360360484Sobrien	      if (warn)
360460484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3605104834Sobrien		       entry_symbol.name,
3606104834Sobrien		       bfd_get_section_vma (output_bfd, ts));
360760484Sobrien	      if (! bfd_set_start_address (output_bfd,
360860484Sobrien					   bfd_get_section_vma (output_bfd,
360960484Sobrien								ts)))
361060484Sobrien		einfo (_("%P%F: can't set start address\n"));
361160484Sobrien	    }
361260484Sobrien	  else
361360484Sobrien	    {
361460484Sobrien	      if (warn)
361560484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3616104834Sobrien		       entry_symbol.name);
361760484Sobrien	    }
361833965Sjdp	}
361933965Sjdp    }
362033965Sjdp}
362133965Sjdp
362238889Sjdp/* This is a small function used when we want to ignore errors from
362338889Sjdp   BFD.  */
362438889Sjdp
362538889Sjdpstatic void
362638889Sjdp#ifdef ANSI_PROTOTYPES
362760484Sobrienignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
362838889Sjdp#else
362938889Sjdpignore_bfd_errors (s)
363060484Sobrien     const char *s ATTRIBUTE_UNUSED;
363138889Sjdp#endif
363238889Sjdp{
363338889Sjdp  /* Don't do anything.  */
363438889Sjdp}
363538889Sjdp
363633965Sjdp/* Check that the architecture of all the input files is compatible
363733965Sjdp   with the output file.  Also call the backend to let it do any
363833965Sjdp   other checking that is needed.  */
363933965Sjdp
364033965Sjdpstatic void
364133965Sjdplang_check ()
364233965Sjdp{
364333965Sjdp  lang_statement_union_type *file;
364433965Sjdp  bfd *input_bfd;
364577298Sobrien  const bfd_arch_info_type *compatible;
364633965Sjdp
364733965Sjdp  for (file = file_chain.head;
364833965Sjdp       file != (lang_statement_union_type *) NULL;
364933965Sjdp       file = file->input_statement.next)
365033965Sjdp    {
365133965Sjdp      input_bfd = file->input_statement.the_bfd;
365294536Sobrien      compatible = bfd_arch_get_compatible (input_bfd, output_bfd);
365394536Sobrien
365494536Sobrien      /* In general it is not possible to perform a relocatable
365594536Sobrien	 link between differing object formats when the input
365694536Sobrien	 file has relocations, because the relocations in the
365794536Sobrien	 input format may not have equivalent representations in
365894536Sobrien	 the output format (and besides BFD does not translate
365994536Sobrien	 relocs for other link purposes than a final link).  */
366094536Sobrien      if ((link_info.relocateable || link_info.emitrelocations)
366194536Sobrien	  && (compatible == NULL
366294536Sobrien	      || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
366394536Sobrien	  && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
366494536Sobrien	{
366594536Sobrien	  einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
366694536Sobrien		 bfd_get_target (input_bfd), input_bfd,
366794536Sobrien		 bfd_get_target (output_bfd), output_bfd);
366894536Sobrien	  /* einfo with %F exits.  */
366994536Sobrien	}
367094536Sobrien
367133965Sjdp      if (compatible == NULL)
367238889Sjdp	{
367338889Sjdp	  if (command_line.warn_mismatch)
367460484Sobrien	    einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
367538889Sjdp		   bfd_printable_name (input_bfd), input_bfd,
367638889Sjdp		   bfd_printable_name (output_bfd));
367738889Sjdp	}
367877298Sobrien      else if (bfd_count_sections (input_bfd))
367938889Sjdp	{
368077298Sobrien	  /* If the input bfd has no contents, it shouldn't set the
3681104834Sobrien	     private data of the output bfd.  */
368277298Sobrien
368338889Sjdp	  bfd_error_handler_type pfn = NULL;
368433965Sjdp
368538889Sjdp	  /* If we aren't supposed to warn about mismatched input
368638889Sjdp             files, temporarily set the BFD error handler to a
368738889Sjdp             function which will do nothing.  We still want to call
368838889Sjdp             bfd_merge_private_bfd_data, since it may set up
368938889Sjdp             information which is needed in the output file.  */
369038889Sjdp	  if (! command_line.warn_mismatch)
369138889Sjdp	    pfn = bfd_set_error_handler (ignore_bfd_errors);
369238889Sjdp	  if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
369338889Sjdp	    {
369438889Sjdp	      if (command_line.warn_mismatch)
369560484Sobrien		einfo (_("%E%X: failed to merge target specific data of file %B\n"),
369638889Sjdp		       input_bfd);
369738889Sjdp	    }
369838889Sjdp	  if (! command_line.warn_mismatch)
369938889Sjdp	    bfd_set_error_handler (pfn);
370038889Sjdp	}
370133965Sjdp    }
370233965Sjdp}
370333965Sjdp
370433965Sjdp/* Look through all the global common symbols and attach them to the
370533965Sjdp   correct section.  The -sort-common command line switch may be used
370633965Sjdp   to roughly sort the entries by size.  */
370733965Sjdp
370833965Sjdpstatic void
370933965Sjdplang_common ()
371033965Sjdp{
371189857Sobrien  if (command_line.inhibit_common_definition)
371289857Sobrien    return;
371333965Sjdp  if (link_info.relocateable
371433965Sjdp      && ! command_line.force_common_definition)
371533965Sjdp    return;
371633965Sjdp
371733965Sjdp  if (! config.sort_common)
371833965Sjdp    bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
371933965Sjdp  else
372033965Sjdp    {
372133965Sjdp      int power;
372233965Sjdp
372333965Sjdp      for (power = 4; power >= 0; power--)
372433965Sjdp	bfd_link_hash_traverse (link_info.hash, lang_one_common,
372533965Sjdp				(PTR) &power);
372633965Sjdp    }
372733965Sjdp}
372833965Sjdp
372933965Sjdp/* Place one common symbol in the correct section.  */
373033965Sjdp
373133965Sjdpstatic boolean
373233965Sjdplang_one_common (h, info)
373333965Sjdp     struct bfd_link_hash_entry *h;
373433965Sjdp     PTR info;
373533965Sjdp{
373633965Sjdp  unsigned int power_of_two;
373733965Sjdp  bfd_vma size;
373833965Sjdp  asection *section;
373977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
374077298Sobrien						ldfile_output_machine);
374133965Sjdp
374233965Sjdp  if (h->type != bfd_link_hash_common)
374333965Sjdp    return true;
374433965Sjdp
374533965Sjdp  size = h->u.c.size;
374633965Sjdp  power_of_two = h->u.c.p->alignment_power;
374733965Sjdp
374833965Sjdp  if (config.sort_common
374933965Sjdp      && power_of_two < (unsigned int) *(int *) info)
375033965Sjdp    return true;
375133965Sjdp
375233965Sjdp  section = h->u.c.p->section;
375333965Sjdp
375433965Sjdp  /* Increase the size of the section.  */
3755104834Sobrien  section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3756104834Sobrien				   (bfd_vma) 1 << power_of_two) * opb;
375733965Sjdp
375833965Sjdp  /* Adjust the alignment if necessary.  */
375933965Sjdp  if (power_of_two > section->alignment_power)
376033965Sjdp    section->alignment_power = power_of_two;
376133965Sjdp
376233965Sjdp  /* Change the symbol from common to defined.  */
376333965Sjdp  h->type = bfd_link_hash_defined;
376433965Sjdp  h->u.def.section = section;
376560484Sobrien  h->u.def.value = section->_cooked_size;
376633965Sjdp
376733965Sjdp  /* Increase the size of the section.  */
376860484Sobrien  section->_cooked_size += size;
376933965Sjdp
377033965Sjdp  /* Make sure the section is allocated in memory, and make sure that
377133965Sjdp     it is no longer a common section.  */
377233965Sjdp  section->flags |= SEC_ALLOC;
377377298Sobrien  section->flags &= ~SEC_IS_COMMON;
377433965Sjdp
377533965Sjdp  if (config.map_file != NULL)
377633965Sjdp    {
377733965Sjdp      static boolean header_printed;
377833965Sjdp      int len;
377933965Sjdp      char *name;
378033965Sjdp      char buf[50];
378133965Sjdp
378233965Sjdp      if (! header_printed)
378333965Sjdp	{
378460484Sobrien	  minfo (_("\nAllocating common symbols\n"));
378560484Sobrien	  minfo (_("Common symbol       size              file\n\n"));
378633965Sjdp	  header_printed = true;
378733965Sjdp	}
378833965Sjdp
378933965Sjdp      name = demangle (h->root.string);
379033965Sjdp      minfo ("%s", name);
379133965Sjdp      len = strlen (name);
379233965Sjdp      free (name);
379333965Sjdp
379433965Sjdp      if (len >= 19)
379533965Sjdp	{
379633965Sjdp	  print_nl ();
379733965Sjdp	  len = 0;
379833965Sjdp	}
379933965Sjdp      while (len < 20)
380033965Sjdp	{
380133965Sjdp	  print_space ();
380233965Sjdp	  ++len;
380333965Sjdp	}
380433965Sjdp
380533965Sjdp      minfo ("0x");
380633965Sjdp      if (size <= 0xffffffff)
380733965Sjdp	sprintf (buf, "%lx", (unsigned long) size);
380833965Sjdp      else
380933965Sjdp	sprintf_vma (buf, size);
381033965Sjdp      minfo ("%s", buf);
381133965Sjdp      len = strlen (buf);
381233965Sjdp
381333965Sjdp      while (len < 16)
381433965Sjdp	{
381533965Sjdp	  print_space ();
381633965Sjdp	  ++len;
381733965Sjdp	}
381833965Sjdp
381933965Sjdp      minfo ("%B\n", section->owner);
382033965Sjdp    }
382133965Sjdp
382233965Sjdp  return true;
382333965Sjdp}
382433965Sjdp
382577298Sobrien/* Run through the input files and ensure that every input section has
382677298Sobrien   somewhere to go.  If one is found without a destination then create
382777298Sobrien   an input request and place it into the statement tree.  */
382833965Sjdp
382933965Sjdpstatic void
383033965Sjdplang_place_orphans ()
383133965Sjdp{
383260484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (file)
383333965Sjdp    {
383433965Sjdp      asection *s;
383533965Sjdp
383633965Sjdp      for (s = file->the_bfd->sections;
383733965Sjdp	   s != (asection *) NULL;
383833965Sjdp	   s = s->next)
383933965Sjdp	{
384033965Sjdp	  if (s->output_section == (asection *) NULL)
384133965Sjdp	    {
384233965Sjdp	      /* This section of the file is not attatched, root
384377298Sobrien	         around for a sensible place for it to go.  */
384433965Sjdp
384533965Sjdp	      if (file->just_syms_flag)
384633965Sjdp		{
3847104834Sobrien		  abort ();
384833965Sjdp		}
384933965Sjdp	      else if (strcmp (s->name, "COMMON") == 0)
385033965Sjdp		{
385133965Sjdp		  /* This is a lonely common section which must have
385233965Sjdp		     come from an archive.  We attach to the section
385333965Sjdp		     with the wildcard.  */
385433965Sjdp		  if (! link_info.relocateable
385533965Sjdp		      || command_line.force_common_definition)
385633965Sjdp		    {
385733965Sjdp		      if (default_common_section == NULL)
385833965Sjdp			{
385933965Sjdp#if 0
386033965Sjdp			  /* This message happens when using the
386133965Sjdp                             svr3.ifile linker script, so I have
386233965Sjdp                             disabled it.  */
386360484Sobrien			  info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
386433965Sjdp#endif
386533965Sjdp			  default_common_section =
386633965Sjdp			    lang_output_section_statement_lookup (".bss");
386733965Sjdp
386833965Sjdp			}
386989857Sobrien		      lang_add_section (&default_common_section->children, s,
387089857Sobrien					default_common_section, file);
387133965Sjdp		    }
387233965Sjdp		}
387333965Sjdp	      else if (ldemul_place_orphan (file, s))
387433965Sjdp		;
387533965Sjdp	      else
387633965Sjdp		{
387789857Sobrien		  lang_output_section_statement_type *os;
387833965Sjdp
387989857Sobrien		  os = lang_output_section_statement_lookup (s->name);
388089857Sobrien		  lang_add_section (&os->children, s, os, file);
388133965Sjdp		}
388233965Sjdp	    }
388333965Sjdp	}
388433965Sjdp    }
388533965Sjdp}
388633965Sjdp
388733965Sjdpvoid
388860484Sobrienlang_set_flags (ptr, flags, invert)
388938889Sjdp     lang_memory_region_type *ptr;
389077298Sobrien     const char *flags;
389160484Sobrien     int invert;
389233965Sjdp{
389360484Sobrien  flagword *ptr_flags;
389433965Sjdp
389560484Sobrien  ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
389633965Sjdp  while (*flags)
389733965Sjdp    {
389833965Sjdp      switch (*flags)
389933965Sjdp	{
390038889Sjdp	case 'A': case 'a':
390138889Sjdp	  *ptr_flags |= SEC_ALLOC;
390233965Sjdp	  break;
390338889Sjdp
390438889Sjdp	case 'R': case 'r':
390538889Sjdp	  *ptr_flags |= SEC_READONLY;
390633965Sjdp	  break;
390738889Sjdp
390838889Sjdp	case 'W': case 'w':
390938889Sjdp	  *ptr_flags |= SEC_DATA;
391033965Sjdp	  break;
391138889Sjdp
391238889Sjdp	case 'X': case 'x':
391338889Sjdp	  *ptr_flags |= SEC_CODE;
391438889Sjdp	  break;
391538889Sjdp
391638889Sjdp	case 'L': case 'l':
391738889Sjdp	case 'I': case 'i':
391838889Sjdp	  *ptr_flags |= SEC_LOAD;
391938889Sjdp	  break;
392038889Sjdp
392133965Sjdp	default:
392260484Sobrien	  einfo (_("%P%F: invalid syntax in flags\n"));
392333965Sjdp	  break;
392433965Sjdp	}
392533965Sjdp      flags++;
392633965Sjdp    }
392733965Sjdp}
392833965Sjdp
392933965Sjdp/* Call a function on each input file.  This function will be called
393033965Sjdp   on an archive, but not on the elements.  */
393133965Sjdp
393233965Sjdpvoid
393333965Sjdplang_for_each_input_file (func)
393433965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
393533965Sjdp{
393633965Sjdp  lang_input_statement_type *f;
393733965Sjdp
393833965Sjdp  for (f = (lang_input_statement_type *) input_file_chain.head;
393933965Sjdp       f != NULL;
394033965Sjdp       f = (lang_input_statement_type *) f->next_real_file)
394133965Sjdp    func (f);
394233965Sjdp}
394333965Sjdp
394433965Sjdp/* Call a function on each file.  The function will be called on all
394533965Sjdp   the elements of an archive which are included in the link, but will
394633965Sjdp   not be called on the archive file itself.  */
394733965Sjdp
394833965Sjdpvoid
394933965Sjdplang_for_each_file (func)
395033965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
395133965Sjdp{
395260484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
395333965Sjdp    {
395433965Sjdp      func (f);
395533965Sjdp    }
395633965Sjdp}
395733965Sjdp
395833965Sjdp#if 0
395933965Sjdp
396033965Sjdp/* Not used.  */
396133965Sjdp
396233965Sjdpvoid
396333965Sjdplang_for_each_input_section (func)
396477298Sobrien     void (*func) PARAMS ((bfd *ab, asection *as));
396533965Sjdp{
396660484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
396733965Sjdp    {
396877298Sobrien      asection *s;
396933965Sjdp
397033965Sjdp      for (s = f->the_bfd->sections;
397133965Sjdp	   s != (asection *) NULL;
397233965Sjdp	   s = s->next)
397333965Sjdp	{
397433965Sjdp	  func (f->the_bfd, s);
397533965Sjdp	}
397633965Sjdp    }
397733965Sjdp}
397833965Sjdp
397933965Sjdp#endif
398033965Sjdp
398133965Sjdpvoid
398233965Sjdpldlang_add_file (entry)
398377298Sobrien     lang_input_statement_type *entry;
398433965Sjdp{
398533965Sjdp  bfd **pp;
398633965Sjdp
398733965Sjdp  lang_statement_append (&file_chain,
398833965Sjdp			 (lang_statement_union_type *) entry,
398933965Sjdp			 &entry->next);
399033965Sjdp
399133965Sjdp  /* The BFD linker needs to have a list of all input BFDs involved in
399233965Sjdp     a link.  */
399333965Sjdp  ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
399433965Sjdp  ASSERT (entry->the_bfd != output_bfd);
399533965Sjdp  for (pp = &link_info.input_bfds;
399633965Sjdp       *pp != (bfd *) NULL;
399733965Sjdp       pp = &(*pp)->link_next)
399833965Sjdp    ;
399933965Sjdp  *pp = entry->the_bfd;
400033965Sjdp  entry->the_bfd->usrdata = (PTR) entry;
400133965Sjdp  bfd_set_gp_size (entry->the_bfd, g_switch_value);
400233965Sjdp
400333965Sjdp  /* Look through the sections and check for any which should not be
400433965Sjdp     included in the link.  We need to do this now, so that we can
400533965Sjdp     notice when the backend linker tries to report multiple
400633965Sjdp     definition errors for symbols which are in sections we aren't
400733965Sjdp     going to link.  FIXME: It might be better to entirely ignore
400833965Sjdp     symbols which are defined in sections which are going to be
400933965Sjdp     discarded.  This would require modifying the backend linker for
401033965Sjdp     each backend which might set the SEC_LINK_ONCE flag.  If we do
401133965Sjdp     this, we should probably handle SEC_EXCLUDE in the same way.  */
401233965Sjdp
401333965Sjdp  bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
401433965Sjdp}
401533965Sjdp
401633965Sjdpvoid
401733965Sjdplang_add_output (name, from_script)
401877298Sobrien     const char *name;
401933965Sjdp     int from_script;
402033965Sjdp{
402133965Sjdp  /* Make -o on command line override OUTPUT in script.  */
4022107492Sobrien  if (!had_output_filename || !from_script)
402333965Sjdp    {
402433965Sjdp      output_filename = name;
402533965Sjdp      had_output_filename = true;
402633965Sjdp    }
402733965Sjdp}
402833965Sjdp
402933965Sjdpstatic lang_output_section_statement_type *current_section;
403033965Sjdp
403133965Sjdpstatic int
403233965Sjdptopower (x)
403333965Sjdp     int x;
403433965Sjdp{
403533965Sjdp  unsigned int i = 1;
403633965Sjdp  int l;
403733965Sjdp
403833965Sjdp  if (x < 0)
403933965Sjdp    return -1;
404033965Sjdp
404177298Sobrien  for (l = 0; l < 32; l++)
404233965Sjdp    {
404333965Sjdp      if (i >= (unsigned int) x)
404433965Sjdp	return l;
404533965Sjdp      i <<= 1;
404633965Sjdp    }
404733965Sjdp
404833965Sjdp  return 0;
404933965Sjdp}
405033965Sjdp
405168765Sobrienlang_output_section_statement_type *
405233965Sjdplang_enter_output_section_statement (output_section_statement_name,
405333965Sjdp				     address_exp, sectype, block_value,
405433965Sjdp				     align, subalign, ebase)
405533965Sjdp     const char *output_section_statement_name;
405677298Sobrien     etree_type *address_exp;
405733965Sjdp     enum section_type sectype;
405833965Sjdp     bfd_vma block_value;
405933965Sjdp     etree_type *align;
406033965Sjdp     etree_type *subalign;
406133965Sjdp     etree_type *ebase;
406233965Sjdp{
406333965Sjdp  lang_output_section_statement_type *os;
406433965Sjdp
406533965Sjdp  current_section =
406633965Sjdp   os =
406733965Sjdp    lang_output_section_statement_lookup (output_section_statement_name);
406833965Sjdp
406977298Sobrien  /* Add this statement to tree.  */
407077298Sobrien#if 0
407177298Sobrien  add_statement (lang_output_section_statement_enum,
407277298Sobrien		 output_section_statement);
407377298Sobrien#endif
407477298Sobrien  /* Make next things chain into subchain of this.  */
407533965Sjdp
407677298Sobrien  if (os->addr_tree == (etree_type *) NULL)
407777298Sobrien    {
407877298Sobrien      os->addr_tree = address_exp;
407977298Sobrien    }
408033965Sjdp  os->sectype = sectype;
408133965Sjdp  if (sectype != noload_section)
408233965Sjdp    os->flags = SEC_NO_FLAGS;
408333965Sjdp  else
408433965Sjdp    os->flags = SEC_NEVER_LOAD;
408533965Sjdp  os->block_value = block_value ? block_value : 1;
408633965Sjdp  stat_ptr = &os->children;
408733965Sjdp
408877298Sobrien  os->subsection_alignment =
408977298Sobrien    topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
409077298Sobrien  os->section_alignment =
409177298Sobrien    topower (exp_get_value_int (align, -1, "section alignment", 0));
409233965Sjdp
409333965Sjdp  os->load_base = ebase;
409468765Sobrien  return os;
409533965Sjdp}
409633965Sjdp
409733965Sjdpvoid
409833965Sjdplang_final ()
409933965Sjdp{
410033965Sjdp  lang_output_statement_type *new =
410133965Sjdp    new_stat (lang_output_statement, stat_ptr);
410233965Sjdp
410333965Sjdp  new->name = output_filename;
410433965Sjdp}
410533965Sjdp
410677298Sobrien/* Reset the current counters in the regions.  */
410777298Sobrien
410891041Sobrienvoid
410991041Sobrienlang_reset_memory_regions ()
411033965Sjdp{
411133965Sjdp  lang_memory_region_type *p = lang_memory_region_list;
411289857Sobrien  asection *o;
411333965Sjdp
411433965Sjdp  for (p = lang_memory_region_list;
411533965Sjdp       p != (lang_memory_region_type *) NULL;
411633965Sjdp       p = p->next)
411733965Sjdp    {
411833965Sjdp      p->old_length = (bfd_size_type) (p->current - p->origin);
411933965Sjdp      p->current = p->origin;
412033965Sjdp    }
412189857Sobrien
412289857Sobrien  for (o = output_bfd->sections; o != NULL; o = o->next)
412389857Sobrien    o->_raw_size = 0;
412433965Sjdp}
412533965Sjdp
412689857Sobrien/* If the wild pattern was marked KEEP, the member sections
412789857Sobrien   should be as well.  */
412860484Sobrien
412960484Sobrienstatic void
413089857Sobriengc_section_callback (ptr, sec, section, file, data)
413160484Sobrien     lang_wild_statement_type *ptr;
413289857Sobrien     struct wildcard_list *sec ATTRIBUTE_UNUSED;
413360484Sobrien     asection *section;
413460484Sobrien     lang_input_statement_type *file ATTRIBUTE_UNUSED;
413577298Sobrien     PTR data ATTRIBUTE_UNUSED;
413660484Sobrien{
413760484Sobrien  if (ptr->keep_sections)
413860484Sobrien    section->flags |= SEC_KEEP;
413960484Sobrien}
414060484Sobrien
414189857Sobrien/* Handle a wild statement, marking it against GC.  */
414260484Sobrien
414360484Sobrienstatic void
414489857Sobrienlang_gc_wild (s)
414560484Sobrien     lang_wild_statement_type *s;
414660484Sobrien{
414789857Sobrien  walk_wild (s, gc_section_callback, NULL);
414860484Sobrien}
414960484Sobrien
415060484Sobrien/* Iterate over sections marking them against GC.  */
415160484Sobrien
415260484Sobrienstatic void
415360484Sobrienlang_gc_sections_1 (s)
415477298Sobrien     lang_statement_union_type *s;
415560484Sobrien{
415689857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
415760484Sobrien    {
415860484Sobrien      switch (s->header.type)
415960484Sobrien	{
416060484Sobrien	case lang_wild_statement_enum:
416189857Sobrien	  lang_gc_wild (&s->wild_statement);
416260484Sobrien	  break;
416360484Sobrien	case lang_constructors_statement_enum:
416460484Sobrien	  lang_gc_sections_1 (constructor_list.head);
416560484Sobrien	  break;
416660484Sobrien	case lang_output_section_statement_enum:
416760484Sobrien	  lang_gc_sections_1 (s->output_section_statement.children.head);
416860484Sobrien	  break;
416960484Sobrien	case lang_group_statement_enum:
417060484Sobrien	  lang_gc_sections_1 (s->group_statement.children.head);
417160484Sobrien	  break;
417260484Sobrien	default:
417360484Sobrien	  break;
417460484Sobrien	}
417560484Sobrien    }
417660484Sobrien}
417760484Sobrien
417860484Sobrienstatic void
417960484Sobrienlang_gc_sections ()
418060484Sobrien{
418160484Sobrien  struct bfd_link_hash_entry *h;
4182104834Sobrien  ldlang_undef_chain_list_type *ulist;
418360484Sobrien
418460484Sobrien  /* Keep all sections so marked in the link script.  */
418560484Sobrien
418660484Sobrien  lang_gc_sections_1 (statement_list.head);
418760484Sobrien
4188104834Sobrien  /* Keep all sections containing symbols undefined on the command-line,
4189104834Sobrien     and the section containing the entry symbol.  */
419077298Sobrien
4191104834Sobrien  for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
419260484Sobrien    {
419377298Sobrien      h = bfd_link_hash_lookup (link_info.hash, ulist->name,
419460484Sobrien				false, false, false);
419560484Sobrien
419660484Sobrien      if (h != (struct bfd_link_hash_entry *) NULL
419777298Sobrien	  && (h->type == bfd_link_hash_defined
419877298Sobrien	      || h->type == bfd_link_hash_defweak)
419960484Sobrien	  && ! bfd_is_abs_section (h->u.def.section))
420060484Sobrien	{
420160484Sobrien	  h->u.def.section->flags |= SEC_KEEP;
420260484Sobrien	}
420360484Sobrien    }
420460484Sobrien
420560484Sobrien  bfd_gc_sections (output_bfd, &link_info);
420660484Sobrien}
420760484Sobrien
420833965Sjdpvoid
420933965Sjdplang_process ()
421033965Sjdp{
421133965Sjdp  lang_reasonable_defaults ();
421233965Sjdp  current_target = default_target;
421333965Sjdp
421477298Sobrien  /* Open the output file.  */
421577298Sobrien  lang_for_each_statement (ldlang_open_output);
421633965Sjdp
421733965Sjdp  ldemul_create_output_section_statements ();
421833965Sjdp
421977298Sobrien  /* Add to the hash table all undefineds on the command line.  */
422033965Sjdp  lang_place_undefineds ();
422133965Sjdp
422260484Sobrien  already_linked_table_init ();
422360484Sobrien
422477298Sobrien  /* Create a bfd for each input file.  */
422533965Sjdp  current_target = default_target;
422633965Sjdp  open_input_bfds (statement_list.head, false);
422733965Sjdp
4228104834Sobrien  link_info.gc_sym_list = &entry_symbol;
4229104834Sobrien  if (entry_symbol.name == NULL)
4230104834Sobrien    link_info.gc_sym_list = ldlang_undef_chain_list_head;
4231104834Sobrien
423233965Sjdp  ldemul_after_open ();
423333965Sjdp
423460484Sobrien  already_linked_table_free ();
423560484Sobrien
423633965Sjdp  /* Make sure that we're not mixing architectures.  We call this
423733965Sjdp     after all the input files have been opened, but before we do any
423833965Sjdp     other processing, so that any operations merge_private_bfd_data
423933965Sjdp     does on the output file will be known during the rest of the
424033965Sjdp     link.  */
424133965Sjdp  lang_check ();
424233965Sjdp
424360484Sobrien  /* Handle .exports instead of a version script if we're told to do so.  */
424460484Sobrien  if (command_line.version_exports_section)
424560484Sobrien    lang_do_version_exports_section ();
424660484Sobrien
424733965Sjdp  /* Build all sets based on the information gathered from the input
424833965Sjdp     files.  */
424933965Sjdp  ldctor_build_sets ();
425033965Sjdp
425160484Sobrien  /* Remove unreferenced sections if asked to.  */
425260484Sobrien  if (command_line.gc_sections)
425360484Sobrien    lang_gc_sections ();
425460484Sobrien
425589857Sobrien  /* If there were any SEC_MERGE sections, finish their merging, so that
425689857Sobrien     section sizes can be computed.  This has to be done after GC of sections,
425789857Sobrien     so that GCed sections are not merged, but before assigning output
425889857Sobrien     sections, since removing whole input sections is hard then.  */
425989857Sobrien  bfd_merge_sections (output_bfd, &link_info);
426089857Sobrien
426177298Sobrien  /* Size up the common data.  */
426233965Sjdp  lang_common ();
426333965Sjdp
426433965Sjdp  /* Run through the contours of the script and attach input sections
426577298Sobrien     to the correct output sections.  */
426633965Sjdp  map_input_to_output_sections (statement_list.head, (char *) NULL,
426733965Sjdp				(lang_output_section_statement_type *) NULL);
426833965Sjdp
426977298Sobrien  /* Find any sections not attached explicitly and handle them.  */
427033965Sjdp  lang_place_orphans ();
427133965Sjdp
427294536Sobrien  if (! link_info.relocateable)
427394536Sobrien    {
427494536Sobrien      /* Look for a text section and set the readonly attribute in it.  */
427594536Sobrien      asection *found = bfd_get_section_by_name (output_bfd, ".text");
427694536Sobrien
427794536Sobrien      if (found != (asection *) NULL)
427894536Sobrien	{
427994536Sobrien	  if (config.text_read_only)
428094536Sobrien	    found->flags |= SEC_READONLY;
428194536Sobrien	  else
428294536Sobrien	    found->flags &= ~SEC_READONLY;
428394536Sobrien	}
428494536Sobrien    }
428594536Sobrien
428694536Sobrien  /* Do anything special before sizing sections.  This is where ELF
428794536Sobrien     and other back-ends size dynamic sections.  */
428833965Sjdp  ldemul_before_allocation ();
428933965Sjdp
4290104834Sobrien  if (!link_info.relocateable)
4291104834Sobrien    strip_excluded_output_sections ();
4292104834Sobrien
429333965Sjdp  /* We must record the program headers before we try to fix the
429433965Sjdp     section positions, since they will affect SIZEOF_HEADERS.  */
429533965Sjdp  lang_record_phdrs ();
429633965Sjdp
429789857Sobrien  /* Size up the sections.  */
429889857Sobrien  lang_size_sections (statement_list.head,
429989857Sobrien		      abs_output_section,
430089857Sobrien		      &statement_list.head, 0, (bfd_vma) 0, NULL);
430189857Sobrien
430277298Sobrien  /* Now run around and relax if we can.  */
430333965Sjdp  if (command_line.relax)
430433965Sjdp    {
430589857Sobrien      /* Keep relaxing until bfd_relax_section gives up.  */
430689857Sobrien      boolean relax_again;
430733965Sjdp
430833965Sjdp      do
430933965Sjdp	{
431091041Sobrien	  lang_reset_memory_regions ();
431133965Sjdp
431233965Sjdp	  relax_again = false;
431333965Sjdp
431460484Sobrien	  /* Note: pe-dll.c does something like this also.  If you find
431560484Sobrien	     you need to change this code, you probably need to change
431677298Sobrien	     pe-dll.c also.  DJ  */
431760484Sobrien
431833965Sjdp	  /* Do all the assignments with our current guesses as to
431933965Sjdp	     section sizes.  */
432033965Sjdp	  lang_do_assignments (statement_list.head,
432133965Sjdp			       abs_output_section,
4322104834Sobrien			       (fill_type *) 0, (bfd_vma) 0);
432333965Sjdp
432433965Sjdp	  /* Perform another relax pass - this time we know where the
4325104834Sobrien	     globals are, so can make a better guess.  */
432633965Sjdp	  lang_size_sections (statement_list.head,
432733965Sjdp			      abs_output_section,
4328104834Sobrien			      &statement_list.head, 0, (bfd_vma) 0,
432989857Sobrien			      &relax_again);
433033965Sjdp	}
433133965Sjdp      while (relax_again);
433233965Sjdp    }
433333965Sjdp
433433965Sjdp  /* See if anything special should be done now we know how big
433533965Sjdp     everything is.  */
433633965Sjdp  ldemul_after_allocation ();
433733965Sjdp
433833965Sjdp  /* Fix any .startof. or .sizeof. symbols.  */
433933965Sjdp  lang_set_startof ();
434033965Sjdp
434177298Sobrien  /* Do all the assignments, now that we know the final resting places
434277298Sobrien     of all the symbols.  */
434333965Sjdp
434433965Sjdp  lang_do_assignments (statement_list.head,
434533965Sjdp		       abs_output_section,
4346104834Sobrien		       (fill_type *) 0, (bfd_vma) 0);
434733965Sjdp
434860484Sobrien  /* Make sure that the section addresses make sense.  */
434960484Sobrien  if (! link_info.relocateable
435060484Sobrien      && command_line.check_section_addresses)
435160484Sobrien    lang_check_section_addresses ();
435233965Sjdp
435377298Sobrien  /* Final stuffs.  */
435477298Sobrien
435533965Sjdp  ldemul_finish ();
435633965Sjdp  lang_finish ();
435733965Sjdp}
435833965Sjdp
435933965Sjdp/* EXPORTED TO YACC */
436033965Sjdp
436133965Sjdpvoid
436289857Sobrienlang_add_wild (filespec, section_list, keep_sections)
436389857Sobrien     struct wildcard_spec *filespec;
436489857Sobrien     struct wildcard_list *section_list;
436560484Sobrien     boolean keep_sections;
436633965Sjdp{
436789857Sobrien  struct wildcard_list *curr, *next;
436889857Sobrien  lang_wild_statement_type *new;
436933965Sjdp
437089857Sobrien  /* Reverse the list as the parser puts it back to front.  */
437189857Sobrien  for (curr = section_list, section_list = NULL;
437289857Sobrien       curr != NULL;
437389857Sobrien       section_list = curr, curr = next)
437433965Sjdp    {
437589857Sobrien      if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
437689857Sobrien	placed_commons = true;
437789857Sobrien
437889857Sobrien      next = curr->next;
437989857Sobrien      curr->next = section_list;
438033965Sjdp    }
438189857Sobrien
438289857Sobrien  if (filespec != NULL && filespec->name != NULL)
438333965Sjdp    {
438489857Sobrien      if (strcmp (filespec->name, "*") == 0)
438589857Sobrien	filespec->name = NULL;
438689857Sobrien      else if (! wildcardp (filespec->name))
438789857Sobrien	lang_has_input_file = true;
438833965Sjdp    }
438989857Sobrien
439089857Sobrien  new = new_stat (lang_wild_statement, stat_ptr);
439189857Sobrien  new->filename = NULL;
439289857Sobrien  new->filenames_sorted = false;
439389857Sobrien  if (filespec != NULL)
439489857Sobrien    {
439589857Sobrien      new->filename = filespec->name;
439689857Sobrien      new->filenames_sorted = filespec->sorted;
439789857Sobrien    }
439889857Sobrien  new->section_list = section_list;
439960484Sobrien  new->keep_sections = keep_sections;
440033965Sjdp  lang_list_init (&new->children);
440133965Sjdp}
440233965Sjdp
440333965Sjdpvoid
440433965Sjdplang_section_start (name, address)
440577298Sobrien     const char *name;
440677298Sobrien     etree_type *address;
440733965Sjdp{
440878828Sobrien  lang_address_statement_type *ad;
440933965Sjdp
441078828Sobrien  ad = new_stat (lang_address_statement, stat_ptr);
441133965Sjdp  ad->section_name = name;
441233965Sjdp  ad->address = address;
441333965Sjdp}
441433965Sjdp
441533965Sjdp/* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
441633965Sjdp   because of a -e argument on the command line, or zero if this is
441733965Sjdp   called by ENTRY in a linker script.  Command line arguments take
441833965Sjdp   precedence.  */
441933965Sjdp
442033965Sjdpvoid
442133965Sjdplang_add_entry (name, cmdline)
442277298Sobrien     const char *name;
442333965Sjdp     boolean cmdline;
442433965Sjdp{
4425104834Sobrien  if (entry_symbol.name == NULL
442633965Sjdp      || cmdline
442733965Sjdp      || ! entry_from_cmdline)
442833965Sjdp    {
4429104834Sobrien      entry_symbol.name = name;
443033965Sjdp      entry_from_cmdline = cmdline;
443133965Sjdp    }
443233965Sjdp}
443333965Sjdp
443433965Sjdpvoid
443533965Sjdplang_add_target (name)
443677298Sobrien     const char *name;
443733965Sjdp{
443833965Sjdp  lang_target_statement_type *new = new_stat (lang_target_statement,
443933965Sjdp					      stat_ptr);
444033965Sjdp
444133965Sjdp  new->target = name;
444233965Sjdp
444333965Sjdp}
444433965Sjdp
444533965Sjdpvoid
444633965Sjdplang_add_map (name)
444777298Sobrien     const char *name;
444833965Sjdp{
444933965Sjdp  while (*name)
445033965Sjdp    {
445133965Sjdp      switch (*name)
445233965Sjdp	{
445377298Sobrien	case 'F':
445433965Sjdp	  map_option_f = true;
445533965Sjdp	  break;
445633965Sjdp	}
445733965Sjdp      name++;
445833965Sjdp    }
445933965Sjdp}
446033965Sjdp
446133965Sjdpvoid
4462104834Sobrienlang_add_fill (fill)
4463104834Sobrien     fill_type *fill;
446433965Sjdp{
446533965Sjdp  lang_fill_statement_type *new = new_stat (lang_fill_statement,
446633965Sjdp					    stat_ptr);
446733965Sjdp
4468104834Sobrien  new->fill = fill;
446933965Sjdp}
447033965Sjdp
447133965Sjdpvoid
447233965Sjdplang_add_data (type, exp)
447333965Sjdp     int type;
447433965Sjdp     union etree_union *exp;
447533965Sjdp{
447633965Sjdp
447733965Sjdp  lang_data_statement_type *new = new_stat (lang_data_statement,
447833965Sjdp					    stat_ptr);
447933965Sjdp
448033965Sjdp  new->exp = exp;
448133965Sjdp  new->type = type;
448233965Sjdp
448333965Sjdp}
448433965Sjdp
448533965Sjdp/* Create a new reloc statement.  RELOC is the BFD relocation type to
448633965Sjdp   generate.  HOWTO is the corresponding howto structure (we could
448733965Sjdp   look this up, but the caller has already done so).  SECTION is the
448833965Sjdp   section to generate a reloc against, or NAME is the name of the
448933965Sjdp   symbol to generate a reloc against.  Exactly one of SECTION and
449033965Sjdp   NAME must be NULL.  ADDEND is an expression for the addend.  */
449133965Sjdp
449233965Sjdpvoid
449333965Sjdplang_add_reloc (reloc, howto, section, name, addend)
449433965Sjdp     bfd_reloc_code_real_type reloc;
449533965Sjdp     reloc_howto_type *howto;
449633965Sjdp     asection *section;
449733965Sjdp     const char *name;
449833965Sjdp     union etree_union *addend;
449933965Sjdp{
450033965Sjdp  lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
450177298Sobrien
450233965Sjdp  p->reloc = reloc;
450333965Sjdp  p->howto = howto;
450433965Sjdp  p->section = section;
450533965Sjdp  p->name = name;
450633965Sjdp  p->addend_exp = addend;
450733965Sjdp
450833965Sjdp  p->addend_value = 0;
450933965Sjdp  p->output_section = NULL;
451033965Sjdp  p->output_vma = 0;
451133965Sjdp}
451233965Sjdp
451360484Sobrienlang_assignment_statement_type *
451433965Sjdplang_add_assignment (exp)
451577298Sobrien     etree_type *exp;
451633965Sjdp{
451733965Sjdp  lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
451833965Sjdp						  stat_ptr);
451933965Sjdp
452033965Sjdp  new->exp = exp;
452160484Sobrien  return new;
452233965Sjdp}
452333965Sjdp
452433965Sjdpvoid
452533965Sjdplang_add_attribute (attribute)
452633965Sjdp     enum statement_enum attribute;
452733965Sjdp{
452833965Sjdp  new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
452933965Sjdp}
453033965Sjdp
453133965Sjdpvoid
453233965Sjdplang_startup (name)
453377298Sobrien     const char *name;
453433965Sjdp{
453533965Sjdp  if (startup_file != (char *) NULL)
453633965Sjdp    {
453760484Sobrien      einfo (_("%P%Fmultiple STARTUP files\n"));
453833965Sjdp    }
453933965Sjdp  first_file->filename = name;
454033965Sjdp  first_file->local_sym_name = name;
454133965Sjdp  first_file->real = true;
454233965Sjdp
454333965Sjdp  startup_file = name;
454433965Sjdp}
454533965Sjdp
454633965Sjdpvoid
454733965Sjdplang_float (maybe)
454833965Sjdp     boolean maybe;
454933965Sjdp{
455033965Sjdp  lang_float_flag = maybe;
455133965Sjdp}
455233965Sjdp
4553104834Sobrien
4554104834Sobrien/* Work out the load- and run-time regions from a script statement, and
4555104834Sobrien   store them in *LMA_REGION and *REGION respectively.
4556104834Sobrien
4557104834Sobrien   MEMSPEC is the name of the run-time region, or "*default*" if the
4558104834Sobrien   statement didn't specify one.  LMA_MEMSPEC is the name of the
4559104834Sobrien   load-time region, or null if the statement didn't specify one.
4560104834Sobrien   HAVE_LMA_P is true if the statement had an explicit load address.
4561104834Sobrien
4562104834Sobrien   It is an error to specify both a load region and a load address.  */
4563104834Sobrien
4564104834Sobrienstatic void
4565104834Sobrienlang_get_regions (region, lma_region, memspec, lma_memspec, have_lma_p)
4566104834Sobrien     struct memory_region_struct **region, **lma_region;
4567104834Sobrien     const char *memspec, *lma_memspec;
4568104834Sobrien     int have_lma_p;
4569104834Sobrien{
4570104834Sobrien  *lma_region = lang_memory_region_lookup (lma_memspec);
4571104834Sobrien
4572104834Sobrien  /* If no runtime region has been given, but the load region has
4573104834Sobrien     been, use the load region.  */
4574104834Sobrien  if (lma_memspec != 0 && strcmp (memspec, "*default*") == 0)
4575104834Sobrien    *region = *lma_region;
4576104834Sobrien  else
4577104834Sobrien    *region = lang_memory_region_lookup (memspec);
4578104834Sobrien
4579104834Sobrien  if (have_lma_p && lma_memspec != 0)
4580104834Sobrien    einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4581104834Sobrien}
4582104834Sobrien
458333965Sjdpvoid
458460484Sobrienlang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
4585104834Sobrien     fill_type *fill;
458633965Sjdp     const char *memspec;
458733965Sjdp     struct lang_output_section_phdr_list *phdrs;
458860484Sobrien     const char *lma_memspec;
458933965Sjdp{
4590104834Sobrien  lang_get_regions (&current_section->region,
4591104834Sobrien		    &current_section->lma_region,
4592104834Sobrien		    memspec, lma_memspec,
4593104834Sobrien		    current_section->load_base != 0);
459433965Sjdp  current_section->fill = fill;
459533965Sjdp  current_section->phdrs = phdrs;
459633965Sjdp  stat_ptr = &statement_list;
459733965Sjdp}
459833965Sjdp
459977298Sobrien/* Create an absolute symbol with the given name with the value of the
460077298Sobrien   address of first byte of the section named.
460133965Sjdp
460277298Sobrien   If the symbol already exists, then do nothing.  */
460377298Sobrien
460433965Sjdpvoid
460533965Sjdplang_abs_symbol_at_beginning_of (secname, name)
460633965Sjdp     const char *secname;
460733965Sjdp     const char *name;
460833965Sjdp{
460933965Sjdp  struct bfd_link_hash_entry *h;
461033965Sjdp
461133965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
461233965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
461360484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
461433965Sjdp
461533965Sjdp  if (h->type == bfd_link_hash_new
461633965Sjdp      || h->type == bfd_link_hash_undefined)
461733965Sjdp    {
461833965Sjdp      asection *sec;
461933965Sjdp
462033965Sjdp      h->type = bfd_link_hash_defined;
462133965Sjdp
462233965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
462333965Sjdp      if (sec == (asection *) NULL)
462433965Sjdp	h->u.def.value = 0;
462533965Sjdp      else
462633965Sjdp	h->u.def.value = bfd_get_section_vma (output_bfd, sec);
462733965Sjdp
462833965Sjdp      h->u.def.section = bfd_abs_section_ptr;
462933965Sjdp    }
463033965Sjdp}
463133965Sjdp
463277298Sobrien/* Create an absolute symbol with the given name with the value of the
463377298Sobrien   address of the first byte after the end of the section named.
463433965Sjdp
463577298Sobrien   If the symbol already exists, then do nothing.  */
463677298Sobrien
463733965Sjdpvoid
463833965Sjdplang_abs_symbol_at_end_of (secname, name)
463933965Sjdp     const char *secname;
464033965Sjdp     const char *name;
464133965Sjdp{
464233965Sjdp  struct bfd_link_hash_entry *h;
464333965Sjdp
464433965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
464533965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
464660484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
464733965Sjdp
464833965Sjdp  if (h->type == bfd_link_hash_new
464933965Sjdp      || h->type == bfd_link_hash_undefined)
465033965Sjdp    {
465133965Sjdp      asection *sec;
465233965Sjdp
465333965Sjdp      h->type = bfd_link_hash_defined;
465433965Sjdp
465533965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
465633965Sjdp      if (sec == (asection *) NULL)
465733965Sjdp	h->u.def.value = 0;
465833965Sjdp      else
465933965Sjdp	h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
466060484Sobrien			  + bfd_section_size (output_bfd, sec) /
466160484Sobrien                          bfd_octets_per_byte (output_bfd));
466233965Sjdp
466333965Sjdp      h->u.def.section = bfd_abs_section_ptr;
466433965Sjdp    }
466533965Sjdp}
466633965Sjdp
466733965Sjdpvoid
466833965Sjdplang_statement_append (list, element, field)
466977298Sobrien     lang_statement_list_type *list;
467077298Sobrien     lang_statement_union_type *element;
467177298Sobrien     lang_statement_union_type **field;
467233965Sjdp{
467333965Sjdp  *(list->tail) = element;
467433965Sjdp  list->tail = field;
467533965Sjdp}
467633965Sjdp
467733965Sjdp/* Set the output format type.  -oformat overrides scripts.  */
467833965Sjdp
467933965Sjdpvoid
468033965Sjdplang_add_output_format (format, big, little, from_script)
468133965Sjdp     const char *format;
468233965Sjdp     const char *big;
468333965Sjdp     const char *little;
468433965Sjdp     int from_script;
468533965Sjdp{
468633965Sjdp  if (output_target == NULL || !from_script)
468733965Sjdp    {
468833965Sjdp      if (command_line.endian == ENDIAN_BIG
468933965Sjdp	  && big != NULL)
469033965Sjdp	format = big;
469133965Sjdp      else if (command_line.endian == ENDIAN_LITTLE
469233965Sjdp	       && little != NULL)
469333965Sjdp	format = little;
469433965Sjdp
469533965Sjdp      output_target = format;
469633965Sjdp    }
469733965Sjdp}
469833965Sjdp
469933965Sjdp/* Enter a group.  This creates a new lang_group_statement, and sets
470033965Sjdp   stat_ptr to build new statements within the group.  */
470133965Sjdp
470233965Sjdpvoid
470333965Sjdplang_enter_group ()
470433965Sjdp{
470533965Sjdp  lang_group_statement_type *g;
470633965Sjdp
470733965Sjdp  g = new_stat (lang_group_statement, stat_ptr);
470833965Sjdp  lang_list_init (&g->children);
470933965Sjdp  stat_ptr = &g->children;
471033965Sjdp}
471133965Sjdp
471233965Sjdp/* Leave a group.  This just resets stat_ptr to start writing to the
471333965Sjdp   regular list of statements again.  Note that this will not work if
471433965Sjdp   groups can occur inside anything else which can adjust stat_ptr,
471533965Sjdp   but currently they can't.  */
471633965Sjdp
471733965Sjdpvoid
471833965Sjdplang_leave_group ()
471933965Sjdp{
472033965Sjdp  stat_ptr = &statement_list;
472133965Sjdp}
472233965Sjdp
472333965Sjdp/* Add a new program header.  This is called for each entry in a PHDRS
472433965Sjdp   command in a linker script.  */
472533965Sjdp
472633965Sjdpvoid
472733965Sjdplang_new_phdr (name, type, filehdr, phdrs, at, flags)
472833965Sjdp     const char *name;
472933965Sjdp     etree_type *type;
473033965Sjdp     boolean filehdr;
473133965Sjdp     boolean phdrs;
473233965Sjdp     etree_type *at;
473333965Sjdp     etree_type *flags;
473433965Sjdp{
473533965Sjdp  struct lang_phdr *n, **pp;
473633965Sjdp
473733965Sjdp  n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
473833965Sjdp  n->next = NULL;
473933965Sjdp  n->name = name;
474033965Sjdp  n->type = exp_get_value_int (type, 0, "program header type",
474133965Sjdp			       lang_final_phase_enum);
474233965Sjdp  n->filehdr = filehdr;
474333965Sjdp  n->phdrs = phdrs;
474433965Sjdp  n->at = at;
474533965Sjdp  n->flags = flags;
474633965Sjdp
474733965Sjdp  for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
474833965Sjdp    ;
474933965Sjdp  *pp = n;
475033965Sjdp}
475133965Sjdp
475233965Sjdp/* Record the program header information in the output BFD.  FIXME: We
475333965Sjdp   should not be calling an ELF specific function here.  */
475433965Sjdp
475533965Sjdpstatic void
475633965Sjdplang_record_phdrs ()
475733965Sjdp{
475833965Sjdp  unsigned int alc;
475933965Sjdp  asection **secs;
476033965Sjdp  struct lang_output_section_phdr_list *last;
476133965Sjdp  struct lang_phdr *l;
476233965Sjdp  lang_statement_union_type *u;
476333965Sjdp
476433965Sjdp  alc = 10;
476533965Sjdp  secs = (asection **) xmalloc (alc * sizeof (asection *));
476633965Sjdp  last = NULL;
476733965Sjdp  for (l = lang_phdr_list; l != NULL; l = l->next)
476833965Sjdp    {
476933965Sjdp      unsigned int c;
477033965Sjdp      flagword flags;
477133965Sjdp      bfd_vma at;
477233965Sjdp
477333965Sjdp      c = 0;
477433965Sjdp      for (u = lang_output_section_statement.head;
477533965Sjdp	   u != NULL;
477633965Sjdp	   u = u->output_section_statement.next)
477733965Sjdp	{
477833965Sjdp	  lang_output_section_statement_type *os;
477933965Sjdp	  struct lang_output_section_phdr_list *pl;
478033965Sjdp
478133965Sjdp	  os = &u->output_section_statement;
478233965Sjdp
478333965Sjdp	  pl = os->phdrs;
478433965Sjdp	  if (pl != NULL)
478533965Sjdp	    last = pl;
478633965Sjdp	  else
478733965Sjdp	    {
478833965Sjdp	      if (os->sectype == noload_section
478933965Sjdp		  || os->bfd_section == NULL
479033965Sjdp		  || (os->bfd_section->flags & SEC_ALLOC) == 0)
479133965Sjdp		continue;
479233965Sjdp	      pl = last;
479333965Sjdp	    }
479433965Sjdp
479533965Sjdp	  if (os->bfd_section == NULL)
479633965Sjdp	    continue;
479733965Sjdp
479833965Sjdp	  for (; pl != NULL; pl = pl->next)
479933965Sjdp	    {
480033965Sjdp	      if (strcmp (pl->name, l->name) == 0)
480133965Sjdp		{
480233965Sjdp		  if (c >= alc)
480333965Sjdp		    {
480433965Sjdp		      alc *= 2;
480533965Sjdp		      secs = ((asection **)
480633965Sjdp			      xrealloc (secs, alc * sizeof (asection *)));
480733965Sjdp		    }
480833965Sjdp		  secs[c] = os->bfd_section;
480933965Sjdp		  ++c;
481033965Sjdp		  pl->used = true;
481133965Sjdp		}
481233965Sjdp	    }
481333965Sjdp	}
481433965Sjdp
481533965Sjdp      if (l->flags == NULL)
481633965Sjdp	flags = 0;
481733965Sjdp      else
481833965Sjdp	flags = exp_get_vma (l->flags, 0, "phdr flags",
481933965Sjdp			     lang_final_phase_enum);
482033965Sjdp
482133965Sjdp      if (l->at == NULL)
482233965Sjdp	at = 0;
482333965Sjdp      else
482433965Sjdp	at = exp_get_vma (l->at, 0, "phdr load address",
482533965Sjdp			  lang_final_phase_enum);
482633965Sjdp
482733965Sjdp      if (! bfd_record_phdr (output_bfd, l->type,
482878828Sobrien			     l->flags != NULL, flags, l->at != NULL,
482933965Sjdp			     at, l->filehdr, l->phdrs, c, secs))
483060484Sobrien	einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
483133965Sjdp    }
483233965Sjdp
483333965Sjdp  free (secs);
483433965Sjdp
483533965Sjdp  /* Make sure all the phdr assignments succeeded.  */
483633965Sjdp  for (u = lang_output_section_statement.head;
483733965Sjdp       u != NULL;
483833965Sjdp       u = u->output_section_statement.next)
483933965Sjdp    {
484033965Sjdp      struct lang_output_section_phdr_list *pl;
484133965Sjdp
484233965Sjdp      if (u->output_section_statement.bfd_section == NULL)
484333965Sjdp	continue;
484433965Sjdp
484533965Sjdp      for (pl = u->output_section_statement.phdrs;
484633965Sjdp	   pl != NULL;
484733965Sjdp	   pl = pl->next)
484833965Sjdp	if (! pl->used && strcmp (pl->name, "NONE") != 0)
484960484Sobrien	  einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
485033965Sjdp		 u->output_section_statement.name, pl->name);
485133965Sjdp    }
485233965Sjdp}
485333965Sjdp
485433965Sjdp/* Record a list of sections which may not be cross referenced.  */
485533965Sjdp
485633965Sjdpvoid
485733965Sjdplang_add_nocrossref (l)
485833965Sjdp     struct lang_nocrossref *l;
485933965Sjdp{
486033965Sjdp  struct lang_nocrossrefs *n;
486133965Sjdp
486233965Sjdp  n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
486333965Sjdp  n->next = nocrossref_list;
486433965Sjdp  n->list = l;
486533965Sjdp  nocrossref_list = n;
486633965Sjdp
486733965Sjdp  /* Set notice_all so that we get informed about all symbols.  */
486833965Sjdp  link_info.notice_all = true;
486933965Sjdp}
487033965Sjdp
487133965Sjdp/* Overlay handling.  We handle overlays with some static variables.  */
487233965Sjdp
487333965Sjdp/* The overlay virtual address.  */
487433965Sjdpstatic etree_type *overlay_vma;
487533965Sjdp
487633965Sjdp/* An expression for the maximum section size seen so far.  */
487733965Sjdpstatic etree_type *overlay_max;
487833965Sjdp
487933965Sjdp/* A list of all the sections in this overlay.  */
488033965Sjdp
488177298Sobrienstruct overlay_list {
488233965Sjdp  struct overlay_list *next;
488333965Sjdp  lang_output_section_statement_type *os;
488433965Sjdp};
488533965Sjdp
488633965Sjdpstatic struct overlay_list *overlay_list;
488733965Sjdp
488833965Sjdp/* Start handling an overlay.  */
488933965Sjdp
489033965Sjdpvoid
4891104834Sobrienlang_enter_overlay (vma_expr)
489233965Sjdp     etree_type *vma_expr;
489333965Sjdp{
489433965Sjdp  /* The grammar should prevent nested overlays from occurring.  */
4895104834Sobrien  ASSERT (overlay_vma == NULL && overlay_max == NULL);
489633965Sjdp
489733965Sjdp  overlay_vma = vma_expr;
489833965Sjdp}
489933965Sjdp
490033965Sjdp/* Start a section in an overlay.  We handle this by calling
4901104834Sobrien   lang_enter_output_section_statement with the correct VMA.
4902104834Sobrien   lang_leave_overlay sets up the LMA and memory regions.  */
490333965Sjdp
490433965Sjdpvoid
490533965Sjdplang_enter_overlay_section (name)
490633965Sjdp     const char *name;
490733965Sjdp{
490833965Sjdp  struct overlay_list *n;
490933965Sjdp  etree_type *size;
491033965Sjdp
491133965Sjdp  lang_enter_output_section_statement (name, overlay_vma, normal_section,
4912104834Sobrien				       0, 0, 0, 0);
491333965Sjdp
4914104834Sobrien  /* If this is the first section, then base the VMA of future
491533965Sjdp     sections on this one.  This will work correctly even if `.' is
491633965Sjdp     used in the addresses.  */
491733965Sjdp  if (overlay_list == NULL)
4918104834Sobrien    overlay_vma = exp_nameop (ADDR, name);
491933965Sjdp
492033965Sjdp  /* Remember the section.  */
492133965Sjdp  n = (struct overlay_list *) xmalloc (sizeof *n);
492233965Sjdp  n->os = current_section;
492333965Sjdp  n->next = overlay_list;
492433965Sjdp  overlay_list = n;
492533965Sjdp
492633965Sjdp  size = exp_nameop (SIZEOF, name);
492733965Sjdp
492833965Sjdp  /* Arrange to work out the maximum section end address.  */
492933965Sjdp  if (overlay_max == NULL)
493033965Sjdp    overlay_max = size;
493133965Sjdp  else
493260484Sobrien    overlay_max = exp_binop (MAX_K, overlay_max, size);
493333965Sjdp}
493433965Sjdp
493533965Sjdp/* Finish a section in an overlay.  There isn't any special to do
493633965Sjdp   here.  */
493733965Sjdp
493833965Sjdpvoid
493933965Sjdplang_leave_overlay_section (fill, phdrs)
4940104834Sobrien     fill_type *fill;
494133965Sjdp     struct lang_output_section_phdr_list *phdrs;
494233965Sjdp{
494333965Sjdp  const char *name;
494433965Sjdp  char *clean, *s2;
494533965Sjdp  const char *s1;
494633965Sjdp  char *buf;
494733965Sjdp
494833965Sjdp  name = current_section->name;
494933965Sjdp
4950104834Sobrien  /* For now, assume that "*default*" is the run-time memory region and
4951104834Sobrien     that no load-time region has been specified.  It doesn't really
4952104834Sobrien     matter what we say here, since lang_leave_overlay will override it.  */
4953104834Sobrien  lang_leave_output_section_statement (fill, "*default*", phdrs, 0);
495433965Sjdp
495533965Sjdp  /* Define the magic symbols.  */
495633965Sjdp
495733965Sjdp  clean = xmalloc (strlen (name) + 1);
495833965Sjdp  s2 = clean;
495933965Sjdp  for (s1 = name; *s1 != '\0'; s1++)
496089857Sobrien    if (ISALNUM (*s1) || *s1 == '_')
496133965Sjdp      *s2++ = *s1;
496233965Sjdp  *s2 = '\0';
496333965Sjdp
496433965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_start_");
496533965Sjdp  sprintf (buf, "__load_start_%s", clean);
496633965Sjdp  lang_add_assignment (exp_assop ('=', buf,
496733965Sjdp				  exp_nameop (LOADADDR, name)));
496833965Sjdp
496933965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
497033965Sjdp  sprintf (buf, "__load_stop_%s", clean);
497133965Sjdp  lang_add_assignment (exp_assop ('=', buf,
497233965Sjdp				  exp_binop ('+',
497333965Sjdp					     exp_nameop (LOADADDR, name),
497433965Sjdp					     exp_nameop (SIZEOF, name))));
497533965Sjdp
497633965Sjdp  free (clean);
497733965Sjdp}
497833965Sjdp
497933965Sjdp/* Finish an overlay.  If there are any overlay wide settings, this
498033965Sjdp   looks through all the sections in the overlay and sets them.  */
498133965Sjdp
498233965Sjdpvoid
4983104834Sobrienlang_leave_overlay (lma_expr, nocrossrefs, fill, memspec, phdrs, lma_memspec)
4984104834Sobrien     etree_type *lma_expr;
4985104834Sobrien     int nocrossrefs;
4986104834Sobrien     fill_type *fill;
498733965Sjdp     const char *memspec;
498833965Sjdp     struct lang_output_section_phdr_list *phdrs;
498960484Sobrien     const char *lma_memspec;
499033965Sjdp{
499133965Sjdp  lang_memory_region_type *region;
499260484Sobrien  lang_memory_region_type *lma_region;
499333965Sjdp  struct overlay_list *l;
499433965Sjdp  struct lang_nocrossref *nocrossref;
499533965Sjdp
4996104834Sobrien  lang_get_regions (&region, &lma_region,
4997104834Sobrien		    memspec, lma_memspec,
4998104834Sobrien		    lma_expr != 0);
499980016Sobrien
5000104834Sobrien  nocrossref = NULL;
500133965Sjdp
5002104834Sobrien  /* After setting the size of the last section, set '.' to end of the
5003104834Sobrien     overlay region.  */
5004104834Sobrien  if (overlay_list != NULL)
5005104834Sobrien    overlay_list->os->update_dot_tree
5006104834Sobrien      = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
500760484Sobrien
500833965Sjdp  l = overlay_list;
500933965Sjdp  while (l != NULL)
501033965Sjdp    {
501133965Sjdp      struct overlay_list *next;
501233965Sjdp
5013104834Sobrien      if (fill != (fill_type *) 0 && l->os->fill == (fill_type *) 0)
501433965Sjdp	l->os->fill = fill;
501580016Sobrien
5016104834Sobrien      l->os->region = region;
5017104834Sobrien      l->os->lma_region = lma_region;
501880016Sobrien
5019104834Sobrien      /* The first section has the load address specified in the
5020104834Sobrien	 OVERLAY statement.  The rest are worked out from that.
5021104834Sobrien	 The base address is not needed (and should be null) if
5022104834Sobrien	 an LMA region was specified.  */
5023104834Sobrien      if (l->next == 0)
5024104834Sobrien	l->os->load_base = lma_expr;
5025104834Sobrien      else if (lma_region == 0)
5026104834Sobrien	l->os->load_base = exp_binop ('+',
5027104834Sobrien				      exp_nameop (LOADADDR, l->next->os->name),
5028104834Sobrien				      exp_nameop (SIZEOF, l->next->os->name));
502980016Sobrien
503033965Sjdp      if (phdrs != NULL && l->os->phdrs == NULL)
503133965Sjdp	l->os->phdrs = phdrs;
503233965Sjdp
5033104834Sobrien      if (nocrossrefs)
503433965Sjdp	{
503533965Sjdp	  struct lang_nocrossref *nc;
503633965Sjdp
503733965Sjdp	  nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
503833965Sjdp	  nc->name = l->os->name;
503933965Sjdp	  nc->next = nocrossref;
504033965Sjdp	  nocrossref = nc;
504133965Sjdp	}
504233965Sjdp
504333965Sjdp      next = l->next;
504433965Sjdp      free (l);
504533965Sjdp      l = next;
504633965Sjdp    }
504733965Sjdp
504833965Sjdp  if (nocrossref != NULL)
504933965Sjdp    lang_add_nocrossref (nocrossref);
505033965Sjdp
505133965Sjdp  overlay_vma = NULL;
505233965Sjdp  overlay_list = NULL;
505333965Sjdp  overlay_max = NULL;
505433965Sjdp}
505533965Sjdp
505633965Sjdp/* Version handling.  This is only useful for ELF.  */
505733965Sjdp
505833965Sjdp/* This global variable holds the version tree that we build.  */
505933965Sjdp
506033965Sjdpstruct bfd_elf_version_tree *lang_elf_version_info;
506133965Sjdp
506260484Sobrienstatic int
506360484Sobrienlang_vers_match_lang_c (expr, sym)
506460484Sobrien     struct bfd_elf_version_expr *expr;
506560484Sobrien     const char *sym;
506660484Sobrien{
506760484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
506860484Sobrien    return 1;
506960484Sobrien  return fnmatch (expr->pattern, sym, 0) == 0;
507060484Sobrien}
507160484Sobrien
507260484Sobrienstatic int
507360484Sobrienlang_vers_match_lang_cplusplus (expr, sym)
507460484Sobrien     struct bfd_elf_version_expr *expr;
507560484Sobrien     const char *sym;
507660484Sobrien{
507760484Sobrien  char *alt_sym;
507860484Sobrien  int result;
507960484Sobrien
508060484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
508160484Sobrien    return 1;
508260484Sobrien
508377298Sobrien  alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
508460484Sobrien  if (!alt_sym)
508560484Sobrien    {
508660484Sobrien      /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
508760484Sobrien	 Should we early out false in this case?  */
508860484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
508960484Sobrien    }
509060484Sobrien  else
509160484Sobrien    {
509260484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
509360484Sobrien      free (alt_sym);
509460484Sobrien    }
509560484Sobrien
509660484Sobrien  return result;
509760484Sobrien}
509860484Sobrien
509960484Sobrienstatic int
510060484Sobrienlang_vers_match_lang_java (expr, sym)
510160484Sobrien     struct bfd_elf_version_expr *expr;
510260484Sobrien     const char *sym;
510360484Sobrien{
510460484Sobrien  char *alt_sym;
510560484Sobrien  int result;
510660484Sobrien
510760484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
510860484Sobrien    return 1;
510960484Sobrien
511077298Sobrien  alt_sym = cplus_demangle (sym, DMGL_JAVA);
511160484Sobrien  if (!alt_sym)
511260484Sobrien    {
511360484Sobrien      /* cplus_demangle (also) returns NULL when it is not a Java symbol.
511460484Sobrien	 Should we early out false in this case?  */
511560484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
511660484Sobrien    }
511760484Sobrien  else
511860484Sobrien    {
511960484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
512060484Sobrien      free (alt_sym);
512160484Sobrien    }
512260484Sobrien
512360484Sobrien  return result;
512460484Sobrien}
512560484Sobrien
512633965Sjdp/* This is called for each variable name or match expression.  */
512733965Sjdp
512833965Sjdpstruct bfd_elf_version_expr *
512991041Sobrienlang_new_vers_pattern (orig, new, lang)
513033965Sjdp     struct bfd_elf_version_expr *orig;
513133965Sjdp     const char *new;
513260484Sobrien     const char *lang;
513333965Sjdp{
513433965Sjdp  struct bfd_elf_version_expr *ret;
513533965Sjdp
513633965Sjdp  ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
513733965Sjdp  ret->next = orig;
513860484Sobrien  ret->pattern = new;
5139104834Sobrien  ret->symver = 0;
5140104834Sobrien  ret->script = 0;
514160484Sobrien
514260484Sobrien  if (lang == NULL || strcasecmp (lang, "C") == 0)
514360484Sobrien    ret->match = lang_vers_match_lang_c;
514460484Sobrien  else if (strcasecmp (lang, "C++") == 0)
514560484Sobrien    ret->match = lang_vers_match_lang_cplusplus;
514660484Sobrien  else if (strcasecmp (lang, "Java") == 0)
514760484Sobrien    ret->match = lang_vers_match_lang_java;
514860484Sobrien  else
514960484Sobrien    {
515060484Sobrien      einfo (_("%X%P: unknown language `%s' in version information\n"),
515160484Sobrien	     lang);
515260484Sobrien      ret->match = lang_vers_match_lang_c;
515360484Sobrien    }
515460484Sobrien
5155104834Sobrien  return ldemul_new_vers_pattern (ret);
515633965Sjdp}
515733965Sjdp
515833965Sjdp/* This is called for each set of variable names and match
515933965Sjdp   expressions.  */
516033965Sjdp
516133965Sjdpstruct bfd_elf_version_tree *
516233965Sjdplang_new_vers_node (globals, locals)
516333965Sjdp     struct bfd_elf_version_expr *globals;
516433965Sjdp     struct bfd_elf_version_expr *locals;
516533965Sjdp{
516633965Sjdp  struct bfd_elf_version_tree *ret;
516733965Sjdp
516833965Sjdp  ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
516933965Sjdp  ret->next = NULL;
517033965Sjdp  ret->name = NULL;
517133965Sjdp  ret->vernum = 0;
517233965Sjdp  ret->globals = globals;
517333965Sjdp  ret->locals = locals;
517433965Sjdp  ret->deps = NULL;
517533965Sjdp  ret->name_indx = (unsigned int) -1;
517633965Sjdp  ret->used = 0;
517733965Sjdp  return ret;
517833965Sjdp}
517933965Sjdp
518033965Sjdp/* This static variable keeps track of version indices.  */
518133965Sjdp
518233965Sjdpstatic int version_index;
518333965Sjdp
518433965Sjdp/* This is called when we know the name and dependencies of the
518533965Sjdp   version.  */
518633965Sjdp
518733965Sjdpvoid
518833965Sjdplang_register_vers_node (name, version, deps)
518933965Sjdp     const char *name;
519033965Sjdp     struct bfd_elf_version_tree *version;
519133965Sjdp     struct bfd_elf_version_deps *deps;
519233965Sjdp{
519333965Sjdp  struct bfd_elf_version_tree *t, **pp;
519433965Sjdp  struct bfd_elf_version_expr *e1;
519533965Sjdp
519689857Sobrien  if (name == NULL)
519789857Sobrien    name = "";
519889857Sobrien
519989857Sobrien  if ((name[0] == '\0' && lang_elf_version_info != NULL)
520089857Sobrien      || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
520189857Sobrien    {
520289857Sobrien      einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
520389857Sobrien      return;
520489857Sobrien    }
520589857Sobrien
520633965Sjdp  /* Make sure this node has a unique name.  */
520733965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
520833965Sjdp    if (strcmp (t->name, name) == 0)
520960484Sobrien      einfo (_("%X%P: duplicate version tag `%s'\n"), name);
521033965Sjdp
521133965Sjdp  /* Check the global and local match names, and make sure there
521233965Sjdp     aren't any duplicates.  */
521333965Sjdp
521433965Sjdp  for (e1 = version->globals; e1 != NULL; e1 = e1->next)
521533965Sjdp    {
521633965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
521733965Sjdp	{
521833965Sjdp	  struct bfd_elf_version_expr *e2;
521933965Sjdp
522033965Sjdp	  for (e2 = t->locals; e2 != NULL; e2 = e2->next)
522160484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
522260484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
522360484Sobrien		     e1->pattern);
522433965Sjdp	}
522533965Sjdp    }
522633965Sjdp
522733965Sjdp  for (e1 = version->locals; e1 != NULL; e1 = e1->next)
522833965Sjdp    {
522933965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
523033965Sjdp	{
523133965Sjdp	  struct bfd_elf_version_expr *e2;
523233965Sjdp
523333965Sjdp	  for (e2 = t->globals; e2 != NULL; e2 = e2->next)
523460484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
523560484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
523660484Sobrien		     e1->pattern);
523733965Sjdp	}
523833965Sjdp    }
523933965Sjdp
524033965Sjdp  version->deps = deps;
524133965Sjdp  version->name = name;
524289857Sobrien  if (name[0] != '\0')
524389857Sobrien    {
524489857Sobrien      ++version_index;
524589857Sobrien      version->vernum = version_index;
524689857Sobrien    }
524789857Sobrien  else
524889857Sobrien    version->vernum = 0;
524933965Sjdp
525033965Sjdp  for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
525133965Sjdp    ;
525233965Sjdp  *pp = version;
525333965Sjdp}
525433965Sjdp
525533965Sjdp/* This is called when we see a version dependency.  */
525633965Sjdp
525733965Sjdpstruct bfd_elf_version_deps *
525833965Sjdplang_add_vers_depend (list, name)
525933965Sjdp     struct bfd_elf_version_deps *list;
526033965Sjdp     const char *name;
526133965Sjdp{
526233965Sjdp  struct bfd_elf_version_deps *ret;
526333965Sjdp  struct bfd_elf_version_tree *t;
526433965Sjdp
526533965Sjdp  ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
526633965Sjdp  ret->next = list;
526733965Sjdp
526833965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
526933965Sjdp    {
527033965Sjdp      if (strcmp (t->name, name) == 0)
527133965Sjdp	{
527233965Sjdp	  ret->version_needed = t;
527333965Sjdp	  return ret;
527433965Sjdp	}
527533965Sjdp    }
527633965Sjdp
527760484Sobrien  einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
527833965Sjdp
527933965Sjdp  return ret;
528033965Sjdp}
528160484Sobrien
528260484Sobrienstatic void
528360484Sobrienlang_do_version_exports_section ()
528460484Sobrien{
528560484Sobrien  struct bfd_elf_version_expr *greg = NULL, *lreg;
528660484Sobrien
528760484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (is)
528860484Sobrien    {
528960484Sobrien      asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
529060484Sobrien      char *contents, *p;
529160484Sobrien      bfd_size_type len;
529260484Sobrien
529360484Sobrien      if (sec == NULL)
5294104834Sobrien	continue;
529560484Sobrien
529660484Sobrien      len = bfd_section_size (is->the_bfd, sec);
529760484Sobrien      contents = xmalloc (len);
529860484Sobrien      if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
529989857Sobrien	einfo (_("%X%P: unable to read .exports section contents\n"), sec);
530060484Sobrien
530160484Sobrien      p = contents;
530277298Sobrien      while (p < contents + len)
530360484Sobrien	{
530491041Sobrien	  greg = lang_new_vers_pattern (greg, p, NULL);
530560484Sobrien	  p = strchr (p, '\0') + 1;
530660484Sobrien	}
530760484Sobrien
530860484Sobrien      /* Do not free the contents, as we used them creating the regex.  */
530960484Sobrien
531060484Sobrien      /* Do not include this section in the link.  */
531160484Sobrien      bfd_set_section_flags (is->the_bfd, sec,
531260484Sobrien	bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
531360484Sobrien    }
531460484Sobrien
531591041Sobrien  lreg = lang_new_vers_pattern (NULL, "*", NULL);
531660484Sobrien  lang_register_vers_node (command_line.version_exports_section,
531760484Sobrien			   lang_new_vers_node (greg, lreg), NULL);
531860484Sobrien}
531977298Sobrien
532077298Sobrienvoid
532177298Sobrienlang_add_unique (name)
532277298Sobrien     const char *name;
532377298Sobrien{
532477298Sobrien  struct unique_sections *ent;
532577298Sobrien
532677298Sobrien  for (ent = unique_section_list; ent; ent = ent->next)
532777298Sobrien    if (strcmp (ent->name, name) == 0)
532877298Sobrien      return;
532977298Sobrien
533077298Sobrien  ent = (struct unique_sections *) xmalloc (sizeof *ent);
533177298Sobrien  ent->name = xstrdup (name);
533277298Sobrien  ent->next = unique_section_list;
533377298Sobrien  unique_section_list = ent;
533477298Sobrien}
5335