ldlang.c revision 91041
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 "ldgram.h"
3333965Sjdp#include "ldexp.h"
3433965Sjdp#include "ldlang.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));
9533965Sjdpstatic void lang_place_undefineds PARAMS ((void));
9633965Sjdpstatic void map_input_to_output_sections
9777298Sobrien  PARAMS ((lang_statement_union_type *, const char *,
9877298Sobrien	   lang_output_section_statement_type *));
9933965Sjdpstatic void print_output_section_statement
10077298Sobrien  PARAMS ((lang_output_section_statement_type *));
10133965Sjdpstatic void print_assignment
10277298Sobrien  PARAMS ((lang_assignment_statement_type *,
10377298Sobrien	   lang_output_section_statement_type *));
10477298Sobrienstatic void print_input_statement PARAMS ((lang_input_statement_type *));
10533965Sjdpstatic boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
10677298Sobrienstatic void print_input_section PARAMS ((lang_input_section_type *));
10777298Sobrienstatic void print_fill_statement PARAMS ((lang_fill_statement_type *));
10877298Sobrienstatic void print_data_statement PARAMS ((lang_data_statement_type *));
10933965Sjdpstatic void print_address_statement PARAMS ((lang_address_statement_type *));
11077298Sobrienstatic void print_reloc_statement PARAMS ((lang_reloc_statement_type *));
11177298Sobrienstatic void print_padding_statement PARAMS ((lang_padding_statement_type *));
11233965Sjdpstatic void print_wild_statement
11377298Sobrien  PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
11433965Sjdpstatic void print_group
11533965Sjdp  PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
11677298Sobrienstatic void print_statement
11777298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
11877298Sobrienstatic void print_statement_list
11977298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
12033965Sjdpstatic void print_statements PARAMS ((void));
12189857Sobrienstatic void insert_pad
12277298Sobrien  PARAMS ((lang_statement_union_type **, fill_type,
12377298Sobrien	   unsigned int, asection *, bfd_vma));
12433965Sjdpstatic bfd_vma size_input_section
12577298Sobrien  PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
12689857Sobrien	   fill_type, bfd_vma));
12733965Sjdpstatic void lang_finish PARAMS ((void));
12838889Sjdpstatic void ignore_bfd_errors PARAMS ((const char *, ...));
12933965Sjdpstatic void lang_check PARAMS ((void));
13033965Sjdpstatic void lang_common PARAMS ((void));
13133965Sjdpstatic boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
13233965Sjdpstatic void lang_place_orphans PARAMS ((void));
13333965Sjdpstatic int topower PARAMS ((int));
13433965Sjdpstatic void lang_set_startof PARAMS ((void));
13577298Sobrienstatic void gc_section_callback
13689857Sobrien  PARAMS ((lang_wild_statement_type *, struct wildcard_list *, asection *,
13777298Sobrien	   lang_input_statement_type *, PTR));
13833965Sjdpstatic void lang_record_phdrs PARAMS ((void));
13989857Sobrienstatic void lang_gc_wild PARAMS ((lang_wild_statement_type *));
14060484Sobrienstatic void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
14160484Sobrienstatic void lang_gc_sections PARAMS ((void));
14277298Sobrienstatic int lang_vers_match_lang_c
14377298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14477298Sobrienstatic int lang_vers_match_lang_cplusplus
14577298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14677298Sobrienstatic int lang_vers_match_lang_java
14777298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14860484Sobrienstatic void lang_do_version_exports_section PARAMS ((void));
14960484Sobrienstatic void lang_check_section_addresses PARAMS ((void));
15077298Sobrienstatic void os_region_check
15177298Sobrien  PARAMS ((lang_output_section_statement_type *,
15277298Sobrien	   struct memory_region_struct *, etree_type *, bfd_vma));
15333965Sjdp
15460484Sobrientypedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
15589857Sobrien				    struct wildcard_list *,
15689857Sobrien				    asection *,
15789857Sobrien				    lang_input_statement_type *,
15877298Sobrien				    PTR));
15977298Sobrienstatic void walk_wild
16089857Sobrien  PARAMS ((lang_wild_statement_type *, callback_t, PTR));
16160484Sobrienstatic void walk_wild_section
16289857Sobrien  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
16389857Sobrien	   callback_t, PTR));
16460484Sobrienstatic void walk_wild_file
16589857Sobrien  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
16689857Sobrien	   callback_t, PTR));
16760484Sobrien
16877298Sobrienstatic int    get_target PARAMS ((const bfd_target *, PTR));
16960484Sobrienstatic void   stricpy PARAMS ((char *, char *));
17060484Sobrienstatic void   strcut PARAMS ((char *, char *));
17160484Sobrienstatic int    name_compare PARAMS ((char *, char *));
17277298Sobrienstatic int    closest_target_match PARAMS ((const bfd_target *, PTR));
17360484Sobrienstatic char * get_first_input_target PARAMS ((void));
17477298Sobrien
17533965Sjdp/* EXPORTS */
17633965Sjdplang_output_section_statement_type *abs_output_section;
17768765Sobrienlang_statement_list_type lang_output_section_statement;
17833965Sjdplang_statement_list_type *stat_ptr = &statement_list;
17960484Sobrienlang_statement_list_type file_chain = { NULL, NULL };
18033965Sjdpconst char *entry_symbol = NULL;
18191041Sobrienconst char *entry_section = ".text";
18233965Sjdpboolean entry_from_cmdline;
18333965Sjdpboolean lang_has_input_file = false;
18433965Sjdpboolean had_output_filename = false;
18533965Sjdpboolean lang_float_flag = false;
18633965Sjdpboolean delete_output_file_on_failure = false;
18733965Sjdpstruct lang_nocrossrefs *nocrossref_list;
18877298Sobrienstruct unique_sections *unique_section_list;
18933965Sjdp
19033965Sjdpetree_type *base; /* Relocation base - or null */
19133965Sjdp
19278828Sobrien#if defined (__STDC__) || defined (ALMOST_STDC)
19333965Sjdp#define cat(a,b) a##b
19433965Sjdp#else
19533965Sjdp#define cat(a,b) a/**/b
19633965Sjdp#endif
19733965Sjdp
19878828Sobrien/* Don't beautify the line below with "innocent" whitespace, it breaks
19978828Sobrien   the K&R C preprocessor!  */
20078828Sobrien#define new_stat(x, y) \
20178828Sobrien  (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
20233965Sjdp
20378828Sobrien#define outside_section_address(q) \
20478828Sobrien  ((q)->output_offset + (q)->output_section->vma)
20533965Sjdp
20678828Sobrien#define outside_symbol_address(q) \
20778828Sobrien  ((q)->value + outside_section_address (q->section))
20833965Sjdp
20933965Sjdp#define SECTION_NAME_MAP_LENGTH (16)
21033965Sjdp
21133965SjdpPTR
21233965Sjdpstat_alloc (size)
21333965Sjdp     size_t size;
21433965Sjdp{
21533965Sjdp  return obstack_alloc (&stat_obstack, size);
21633965Sjdp}
21733965Sjdp
21877298Sobrienboolean
21977298Sobrienunique_section_p (secnam)
22077298Sobrien     const char *secnam;
22177298Sobrien{
22277298Sobrien  struct unique_sections *unam;
22360484Sobrien
22477298Sobrien  for (unam = unique_section_list; unam; unam = unam->next)
22577298Sobrien    if (wildcardp (unam->name)
22677298Sobrien	? fnmatch (unam->name, secnam, 0) == 0
22777298Sobrien	: strcmp (unam->name, secnam) == 0)
22877298Sobrien      {
22977298Sobrien	return true;
23077298Sobrien      }
23177298Sobrien
23277298Sobrien  return false;
23377298Sobrien}
23477298Sobrien
23577298Sobrien/* Generic traversal routines for finding matching sections.  */
23677298Sobrien
23760484Sobrienstatic void
23889857Sobrienwalk_wild_section (ptr, file, callback, data)
23960484Sobrien     lang_wild_statement_type *ptr;
24060484Sobrien     lang_input_statement_type *file;
24160484Sobrien     callback_t callback;
24277298Sobrien     PTR data;
24360484Sobrien{
24489857Sobrien  asection *s;
24589857Sobrien
24689857Sobrien  if (file->just_syms_flag)
24789857Sobrien    return;
24889857Sobrien
24989857Sobrien  for (s = file->the_bfd->sections; s != NULL; s = s->next)
25060484Sobrien    {
25189857Sobrien      struct wildcard_list *sec;
25289857Sobrien
25389857Sobrien      sec = ptr->section_list;
25489857Sobrien      if (sec == NULL)
25589857Sobrien	(*callback) (ptr, sec, s, file, data);
25689857Sobrien
25789857Sobrien      while (sec != NULL)
25877298Sobrien	{
25989857Sobrien	  boolean skip = false;
26089857Sobrien	  struct name_list *list_tmp;
26160484Sobrien
26289857Sobrien	  /* Don't process sections from files which were
26389857Sobrien	     excluded.  */
26489857Sobrien	  for (list_tmp = sec->spec.exclude_name_list;
26589857Sobrien	       list_tmp;
26689857Sobrien	       list_tmp = list_tmp->next)
26789857Sobrien	    {
26889857Sobrien	      if (wildcardp (list_tmp->name))
26989857Sobrien		skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
27089857Sobrien	      else
27189857Sobrien		skip = strcmp (list_tmp->name, file->filename) == 0;
27260484Sobrien
27389857Sobrien	      /* If this file is part of an archive, and the archive is
27489857Sobrien		 excluded, exclude this file.  */
27589857Sobrien	      if (! skip && file->the_bfd != NULL
27689857Sobrien		  && file->the_bfd->my_archive != NULL
27789857Sobrien		  && file->the_bfd->my_archive->filename != NULL)
27889857Sobrien		{
27989857Sobrien		  if (wildcardp (list_tmp->name))
28089857Sobrien		    skip = fnmatch (list_tmp->name,
28189857Sobrien				    file->the_bfd->my_archive->filename,
28289857Sobrien				    0) == 0;
28389857Sobrien		  else
28489857Sobrien		    skip = strcmp (list_tmp->name,
28589857Sobrien				   file->the_bfd->my_archive->filename) == 0;
28689857Sobrien		}
28760484Sobrien
28889857Sobrien	      if (skip)
28989857Sobrien		break;
29089857Sobrien	    }
29160484Sobrien
29289857Sobrien	  if (!skip && sec->spec.name != NULL)
29389857Sobrien	    {
29489857Sobrien	      const char *sname = bfd_get_section_name (file->the_bfd, s);
29560484Sobrien
29689857Sobrien	      if (wildcardp (sec->spec.name))
29789857Sobrien		skip = fnmatch (sec->spec.name, sname, 0) != 0;
29889857Sobrien	      else
29989857Sobrien		skip = strcmp (sec->spec.name, sname) != 0;
30089857Sobrien	    }
30160484Sobrien
30289857Sobrien	  if (!skip)
30389857Sobrien	    (*callback) (ptr, sec, s, file, data);
30460484Sobrien
30589857Sobrien	  sec = sec->next;
30660484Sobrien	}
30760484Sobrien    }
30860484Sobrien}
30960484Sobrien
31060484Sobrien/* Handle a wild statement for a single file F.  */
31160484Sobrien
31260484Sobrienstatic void
31389857Sobrienwalk_wild_file (s, f, callback, data)
31460484Sobrien     lang_wild_statement_type *s;
31560484Sobrien     lang_input_statement_type *f;
31660484Sobrien     callback_t callback;
31777298Sobrien     PTR data;
31860484Sobrien{
31960484Sobrien  if (f->the_bfd == NULL
32060484Sobrien      || ! bfd_check_format (f->the_bfd, bfd_archive))
32189857Sobrien    walk_wild_section (s, f, callback, data);
32260484Sobrien  else
32360484Sobrien    {
32460484Sobrien      bfd *member;
32560484Sobrien
32660484Sobrien      /* This is an archive file.  We must map each member of the
32760484Sobrien	 archive separately.  */
32860484Sobrien      member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
32960484Sobrien      while (member != NULL)
33060484Sobrien	{
33160484Sobrien	  /* When lookup_name is called, it will call the add_symbols
33260484Sobrien	     entry point for the archive.  For each element of the
33360484Sobrien	     archive which is included, BFD will call ldlang_add_file,
33460484Sobrien	     which will set the usrdata field of the member to the
33560484Sobrien	     lang_input_statement.  */
33660484Sobrien	  if (member->usrdata != NULL)
33760484Sobrien	    {
33889857Sobrien	      walk_wild_section (s,
33960484Sobrien				 (lang_input_statement_type *) member->usrdata,
34060484Sobrien				 callback, data);
34160484Sobrien	    }
34260484Sobrien
34360484Sobrien	  member = bfd_openr_next_archived_file (f->the_bfd, member);
34460484Sobrien	}
34560484Sobrien    }
34660484Sobrien}
34760484Sobrien
34860484Sobrienstatic void
34989857Sobrienwalk_wild (s, callback, data)
35060484Sobrien     lang_wild_statement_type *s;
35160484Sobrien     callback_t callback;
35277298Sobrien     PTR data;
35360484Sobrien{
35489857Sobrien  const char *file_spec = s->filename;
35589857Sobrien
35689857Sobrien  if (file_spec == NULL)
35760484Sobrien    {
35860484Sobrien      /* Perform the iteration over all files in the list.  */
35960484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
36060484Sobrien	{
36189857Sobrien	  walk_wild_file (s, f, callback, data);
36260484Sobrien	}
36360484Sobrien    }
36489857Sobrien  else if (wildcardp (file_spec))
36560484Sobrien    {
36660484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
36760484Sobrien	{
36889857Sobrien	  if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
36989857Sobrien	    walk_wild_file (s, f, callback, data);
37060484Sobrien	}
37160484Sobrien    }
37260484Sobrien  else
37360484Sobrien    {
37460484Sobrien      lang_input_statement_type *f;
37560484Sobrien
37660484Sobrien      /* Perform the iteration over a single file.  */
37789857Sobrien      f = lookup_name (file_spec);
37889857Sobrien      if (f)
37989857Sobrien	walk_wild_file (s, f, callback, data);
38060484Sobrien    }
38177298Sobrien}
38233965Sjdp
38377298Sobrien/* lang_for_each_statement walks the parse tree and calls the provided
38477298Sobrien   function for each node.  */
38577298Sobrien
38633965Sjdpstatic void
38733965Sjdplang_for_each_statement_worker (func, s)
38833965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
38933965Sjdp     lang_statement_union_type *s;
39033965Sjdp{
39189857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
39233965Sjdp    {
39333965Sjdp      func (s);
39433965Sjdp
39533965Sjdp      switch (s->header.type)
39633965Sjdp	{
39733965Sjdp	case lang_constructors_statement_enum:
39833965Sjdp	  lang_for_each_statement_worker (func, constructor_list.head);
39933965Sjdp	  break;
40033965Sjdp	case lang_output_section_statement_enum:
40133965Sjdp	  lang_for_each_statement_worker
40233965Sjdp	    (func,
40333965Sjdp	     s->output_section_statement.children.head);
40433965Sjdp	  break;
40533965Sjdp	case lang_wild_statement_enum:
40633965Sjdp	  lang_for_each_statement_worker
40733965Sjdp	    (func,
40833965Sjdp	     s->wild_statement.children.head);
40933965Sjdp	  break;
41033965Sjdp	case lang_group_statement_enum:
41133965Sjdp	  lang_for_each_statement_worker (func,
41233965Sjdp					  s->group_statement.children.head);
41333965Sjdp	  break;
41433965Sjdp	case lang_data_statement_enum:
41533965Sjdp	case lang_reloc_statement_enum:
41633965Sjdp	case lang_object_symbols_statement_enum:
41733965Sjdp	case lang_output_statement_enum:
41833965Sjdp	case lang_target_statement_enum:
41933965Sjdp	case lang_input_section_enum:
42033965Sjdp	case lang_input_statement_enum:
42133965Sjdp	case lang_assignment_statement_enum:
42233965Sjdp	case lang_padding_statement_enum:
42333965Sjdp	case lang_address_statement_enum:
42433965Sjdp	case lang_fill_statement_enum:
42533965Sjdp	  break;
42633965Sjdp	default:
42733965Sjdp	  FAIL ();
42833965Sjdp	  break;
42933965Sjdp	}
43033965Sjdp    }
43133965Sjdp}
43233965Sjdp
43333965Sjdpvoid
43433965Sjdplang_for_each_statement (func)
43533965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
43633965Sjdp{
43777298Sobrien  lang_for_each_statement_worker (func, statement_list.head);
43833965Sjdp}
43933965Sjdp
44033965Sjdp/*----------------------------------------------------------------------*/
44177298Sobrien
44233965Sjdpvoid
44333965Sjdplang_list_init (list)
44433965Sjdp     lang_statement_list_type *list;
44533965Sjdp{
44633965Sjdp  list->head = (lang_statement_union_type *) NULL;
44733965Sjdp  list->tail = &list->head;
44833965Sjdp}
44933965Sjdp
45077298Sobrien/* Build a new statement node for the parse tree.  */
45133965Sjdp
45277298Sobrienstatic lang_statement_union_type *
45333965Sjdpnew_statement (type, size, list)
45433965Sjdp     enum statement_enum type;
45533965Sjdp     size_t size;
45677298Sobrien     lang_statement_list_type *list;
45733965Sjdp{
45833965Sjdp  lang_statement_union_type *new = (lang_statement_union_type *)
45933965Sjdp  stat_alloc (size);
46033965Sjdp
46133965Sjdp  new->header.type = type;
46233965Sjdp  new->header.next = (lang_statement_union_type *) NULL;
46333965Sjdp  lang_statement_append (list, new, &new->header.next);
46433965Sjdp  return new;
46533965Sjdp}
46633965Sjdp
46777298Sobrien/* Build a new input file node for the language.  There are several
46877298Sobrien   ways in which we treat an input file, eg, we only look at symbols,
46977298Sobrien   or prefix it with a -l etc.
47033965Sjdp
47177298Sobrien   We can be supplied with requests for input files more than once;
47277298Sobrien   they may, for example be split over serveral lines like foo.o(.text)
47378828Sobrien   foo.o(.data) etc, so when asked for a file we check that we haven't
47477298Sobrien   got it already so we don't duplicate the bfd.  */
47533965Sjdp
47633965Sjdpstatic lang_input_statement_type *
47733965Sjdpnew_afile (name, file_type, target, add_to_list)
47877298Sobrien     const char *name;
47933965Sjdp     lang_input_file_enum_type file_type;
48077298Sobrien     const char *target;
48133965Sjdp     boolean add_to_list;
48233965Sjdp{
48333965Sjdp  lang_input_statement_type *p;
48433965Sjdp
48533965Sjdp  if (add_to_list)
48633965Sjdp    p = new_stat (lang_input_statement, stat_ptr);
48733965Sjdp  else
48833965Sjdp    {
48933965Sjdp      p = ((lang_input_statement_type *)
49033965Sjdp	   stat_alloc (sizeof (lang_input_statement_type)));
49133965Sjdp      p->header.next = NULL;
49233965Sjdp    }
49333965Sjdp
49433965Sjdp  lang_has_input_file = true;
49533965Sjdp  p->target = target;
49633965Sjdp  switch (file_type)
49733965Sjdp    {
49833965Sjdp    case lang_input_file_is_symbols_only_enum:
49933965Sjdp      p->filename = name;
50033965Sjdp      p->is_archive = false;
50133965Sjdp      p->real = true;
50233965Sjdp      p->local_sym_name = name;
50333965Sjdp      p->just_syms_flag = true;
50433965Sjdp      p->search_dirs_flag = false;
50533965Sjdp      break;
50633965Sjdp    case lang_input_file_is_fake_enum:
50733965Sjdp      p->filename = name;
50833965Sjdp      p->is_archive = false;
50933965Sjdp      p->real = false;
51033965Sjdp      p->local_sym_name = name;
51133965Sjdp      p->just_syms_flag = false;
51233965Sjdp      p->search_dirs_flag = false;
51333965Sjdp      break;
51433965Sjdp    case lang_input_file_is_l_enum:
51533965Sjdp      p->is_archive = true;
51633965Sjdp      p->filename = name;
51733965Sjdp      p->real = true;
51833965Sjdp      p->local_sym_name = concat ("-l", name, (const char *) NULL);
51933965Sjdp      p->just_syms_flag = false;
52033965Sjdp      p->search_dirs_flag = true;
52133965Sjdp      break;
52233965Sjdp    case lang_input_file_is_marker_enum:
52333965Sjdp      p->filename = name;
52433965Sjdp      p->is_archive = false;
52533965Sjdp      p->real = false;
52633965Sjdp      p->local_sym_name = name;
52733965Sjdp      p->just_syms_flag = false;
52833965Sjdp      p->search_dirs_flag = true;
52933965Sjdp      break;
53033965Sjdp    case lang_input_file_is_search_file_enum:
53133965Sjdp      p->filename = name;
53233965Sjdp      p->is_archive = false;
53333965Sjdp      p->real = true;
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_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 = false;
54533965Sjdp      break;
54633965Sjdp    default:
54733965Sjdp      FAIL ();
54833965Sjdp    }
54933965Sjdp  p->the_bfd = (bfd *) NULL;
55033965Sjdp  p->asymbols = (asymbol **) NULL;
55133965Sjdp  p->next_real_file = (lang_statement_union_type *) NULL;
55233965Sjdp  p->next = (lang_statement_union_type *) NULL;
55333965Sjdp  p->symbol_count = 0;
55433965Sjdp  p->dynamic = config.dynamic_link;
55533965Sjdp  p->whole_archive = whole_archive;
55633965Sjdp  p->loaded = false;
55733965Sjdp  lang_statement_append (&input_file_chain,
55833965Sjdp			 (lang_statement_union_type *) p,
55933965Sjdp			 &p->next_real_file);
56033965Sjdp  return p;
56133965Sjdp}
56233965Sjdp
56333965Sjdplang_input_statement_type *
56433965Sjdplang_add_input_file (name, file_type, target)
56577298Sobrien     const char *name;
56633965Sjdp     lang_input_file_enum_type file_type;
56777298Sobrien     const char *target;
56833965Sjdp{
56933965Sjdp  lang_has_input_file = true;
57033965Sjdp  return new_afile (name, file_type, target, true);
57133965Sjdp}
57233965Sjdp
57377298Sobrien/* Build enough state so that the parser can build its tree.  */
57477298Sobrien
57533965Sjdpvoid
57633965Sjdplang_init ()
57733965Sjdp{
57833965Sjdp  obstack_begin (&stat_obstack, 1000);
57933965Sjdp
58033965Sjdp  stat_ptr = &statement_list;
58133965Sjdp
58233965Sjdp  lang_list_init (stat_ptr);
58333965Sjdp
58433965Sjdp  lang_list_init (&input_file_chain);
58533965Sjdp  lang_list_init (&lang_output_section_statement);
58633965Sjdp  lang_list_init (&file_chain);
58733965Sjdp  first_file = lang_add_input_file ((char *) NULL,
58833965Sjdp				    lang_input_file_is_marker_enum,
58933965Sjdp				    (char *) NULL);
59077298Sobrien  abs_output_section =
59177298Sobrien    lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
59233965Sjdp
59333965Sjdp  abs_output_section->bfd_section = bfd_abs_section_ptr;
59433965Sjdp
59533965Sjdp}
59633965Sjdp
59733965Sjdp/*----------------------------------------------------------------------
59877298Sobrien  A region is an area of memory declared with the
59977298Sobrien  MEMORY {  name:org=exp, len=exp ... }
60077298Sobrien  syntax.
60133965Sjdp
60277298Sobrien  We maintain a list of all the regions here.
60333965Sjdp
60477298Sobrien  If no regions are specified in the script, then the default is used
60577298Sobrien  which is created when looked up to be the entire data space.  */
60633965Sjdp
60733965Sjdpstatic lang_memory_region_type *lang_memory_region_list;
60833965Sjdpstatic lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
60933965Sjdp
61033965Sjdplang_memory_region_type *
61133965Sjdplang_memory_region_lookup (name)
61277298Sobrien     const char *const name;
61333965Sjdp{
61438889Sjdp  lang_memory_region_type *p;
61533965Sjdp
61633965Sjdp  for (p = lang_memory_region_list;
61733965Sjdp       p != (lang_memory_region_type *) NULL;
61833965Sjdp       p = p->next)
61933965Sjdp    {
62033965Sjdp      if (strcmp (p->name, name) == 0)
62133965Sjdp	{
62233965Sjdp	  return p;
62333965Sjdp	}
62433965Sjdp    }
62533965Sjdp
62633965Sjdp#if 0
62733965Sjdp  /* This code used to always use the first region in the list as the
62833965Sjdp     default region.  I changed it to instead use a region
62933965Sjdp     encompassing all of memory as the default region.  This permits
63033965Sjdp     NOLOAD sections to work reasonably without requiring a region.
63133965Sjdp     People should specify what region they mean, if they really want
63233965Sjdp     a region.  */
63333965Sjdp  if (strcmp (name, "*default*") == 0)
63433965Sjdp    {
63533965Sjdp      if (lang_memory_region_list != (lang_memory_region_type *) NULL)
63633965Sjdp	{
63733965Sjdp	  return lang_memory_region_list;
63833965Sjdp	}
63933965Sjdp    }
64033965Sjdp#endif
64133965Sjdp
64233965Sjdp  {
64333965Sjdp    lang_memory_region_type *new =
64433965Sjdp    (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
64533965Sjdp
64678828Sobrien    new->name = xstrdup (name);
64733965Sjdp    new->next = (lang_memory_region_type *) NULL;
64833965Sjdp
64933965Sjdp    *lang_memory_region_list_tail = new;
65033965Sjdp    lang_memory_region_list_tail = &new->next;
65133965Sjdp    new->origin = 0;
65238889Sjdp    new->flags = 0;
65338889Sjdp    new->not_flags = 0;
65477298Sobrien    new->length = ~(bfd_size_type) 0;
65533965Sjdp    new->current = 0;
65633965Sjdp    new->had_full_message = false;
65733965Sjdp
65833965Sjdp    return new;
65933965Sjdp  }
66033965Sjdp}
66133965Sjdp
66277298Sobrienstatic lang_memory_region_type *
66338889Sjdplang_memory_default (section)
66438889Sjdp     asection *section;
66538889Sjdp{
66638889Sjdp  lang_memory_region_type *p;
66738889Sjdp
66838889Sjdp  flagword sec_flags = section->flags;
66938889Sjdp
67038889Sjdp  /* Override SEC_DATA to mean a writable section.  */
67138889Sjdp  if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
67238889Sjdp    sec_flags |= SEC_DATA;
67338889Sjdp
67438889Sjdp  for (p = lang_memory_region_list;
67538889Sjdp       p != (lang_memory_region_type *) NULL;
67638889Sjdp       p = p->next)
67738889Sjdp    {
67838889Sjdp      if ((p->flags & sec_flags) != 0
67938889Sjdp	  && (p->not_flags & sec_flags) == 0)
68038889Sjdp	{
68138889Sjdp	  return p;
68238889Sjdp	}
68338889Sjdp    }
68438889Sjdp  return lang_memory_region_lookup ("*default*");
68538889Sjdp}
68638889Sjdp
68733965Sjdplang_output_section_statement_type *
68833965Sjdplang_output_section_find (name)
68977298Sobrien     const char *const name;
69033965Sjdp{
69133965Sjdp  lang_statement_union_type *u;
69233965Sjdp  lang_output_section_statement_type *lookup;
69333965Sjdp
69433965Sjdp  for (u = lang_output_section_statement.head;
69533965Sjdp       u != (lang_statement_union_type *) NULL;
69633965Sjdp       u = lookup->next)
69733965Sjdp    {
69833965Sjdp      lookup = &u->output_section_statement;
69933965Sjdp      if (strcmp (name, lookup->name) == 0)
70033965Sjdp	{
70133965Sjdp	  return lookup;
70233965Sjdp	}
70333965Sjdp    }
70433965Sjdp  return (lang_output_section_statement_type *) NULL;
70533965Sjdp}
70633965Sjdp
70733965Sjdplang_output_section_statement_type *
70833965Sjdplang_output_section_statement_lookup (name)
70977298Sobrien     const char *const name;
71033965Sjdp{
71133965Sjdp  lang_output_section_statement_type *lookup;
71233965Sjdp
71333965Sjdp  lookup = lang_output_section_find (name);
71433965Sjdp  if (lookup == (lang_output_section_statement_type *) NULL)
71533965Sjdp    {
71633965Sjdp
71733965Sjdp      lookup = (lang_output_section_statement_type *)
71833965Sjdp	new_stat (lang_output_section_statement, stat_ptr);
71933965Sjdp      lookup->region = (lang_memory_region_type *) NULL;
72060484Sobrien      lookup->lma_region = (lang_memory_region_type *) NULL;
72133965Sjdp      lookup->fill = 0;
72233965Sjdp      lookup->block_value = 1;
72333965Sjdp      lookup->name = name;
72433965Sjdp
72533965Sjdp      lookup->next = (lang_statement_union_type *) NULL;
72633965Sjdp      lookup->bfd_section = (asection *) NULL;
72733965Sjdp      lookup->processed = false;
72833965Sjdp      lookup->sectype = normal_section;
72933965Sjdp      lookup->addr_tree = (etree_type *) NULL;
73033965Sjdp      lang_list_init (&lookup->children);
73133965Sjdp
73277298Sobrien      lookup->memspec = (const char *) NULL;
73333965Sjdp      lookup->flags = 0;
73433965Sjdp      lookup->subsection_alignment = -1;
73533965Sjdp      lookup->section_alignment = -1;
73633965Sjdp      lookup->load_base = (union etree_union *) NULL;
73733965Sjdp      lookup->phdrs = NULL;
73833965Sjdp
73933965Sjdp      lang_statement_append (&lang_output_section_statement,
74033965Sjdp			     (lang_statement_union_type *) lookup,
74133965Sjdp			     &lookup->next);
74233965Sjdp    }
74333965Sjdp  return lookup;
74433965Sjdp}
74533965Sjdp
74638889Sjdpstatic void
74738889Sjdplang_map_flags (flag)
74838889Sjdp     flagword flag;
74938889Sjdp{
75038889Sjdp  if (flag & SEC_ALLOC)
75138889Sjdp    minfo ("a");
75238889Sjdp
75338889Sjdp  if (flag & SEC_CODE)
75438889Sjdp    minfo ("x");
75538889Sjdp
75638889Sjdp  if (flag & SEC_READONLY)
75738889Sjdp    minfo ("r");
75838889Sjdp
75938889Sjdp  if (flag & SEC_DATA)
76038889Sjdp    minfo ("w");
76138889Sjdp
76238889Sjdp  if (flag & SEC_LOAD)
76338889Sjdp    minfo ("l");
76438889Sjdp}
76538889Sjdp
76633965Sjdpvoid
76733965Sjdplang_map ()
76833965Sjdp{
76933965Sjdp  lang_memory_region_type *m;
77033965Sjdp
77160484Sobrien  minfo (_("\nMemory Configuration\n\n"));
77238889Sjdp  fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
77360484Sobrien	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
77433965Sjdp
77533965Sjdp  for (m = lang_memory_region_list;
77633965Sjdp       m != (lang_memory_region_type *) NULL;
77733965Sjdp       m = m->next)
77833965Sjdp    {
77933965Sjdp      char buf[100];
78033965Sjdp      int len;
78133965Sjdp
78233965Sjdp      fprintf (config.map_file, "%-16s ", m->name);
78333965Sjdp
78433965Sjdp      sprintf_vma (buf, m->origin);
78533965Sjdp      minfo ("0x%s ", buf);
78633965Sjdp      len = strlen (buf);
78733965Sjdp      while (len < 16)
78833965Sjdp	{
78933965Sjdp	  print_space ();
79033965Sjdp	  ++len;
79133965Sjdp	}
79233965Sjdp
79338889Sjdp      minfo ("0x%V", m->length);
79438889Sjdp      if (m->flags || m->not_flags)
79538889Sjdp	{
79638889Sjdp#ifndef BFD64
79738889Sjdp	  minfo ("        ");
79838889Sjdp#endif
79938889Sjdp	  if (m->flags)
80038889Sjdp	    {
80138889Sjdp	      print_space ();
80238889Sjdp	      lang_map_flags (m->flags);
80338889Sjdp	    }
80438889Sjdp
80538889Sjdp	  if (m->not_flags)
80638889Sjdp	    {
80738889Sjdp	      minfo (" !");
80838889Sjdp	      lang_map_flags (m->not_flags);
80938889Sjdp	    }
81038889Sjdp	}
81138889Sjdp
81238889Sjdp      print_nl ();
81333965Sjdp    }
81433965Sjdp
81560484Sobrien  fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
81633965Sjdp
81733965Sjdp  print_statements ();
81833965Sjdp}
81933965Sjdp
82033965Sjdp/* Initialize an output section.  */
82133965Sjdp
82233965Sjdpstatic void
82333965Sjdpinit_os (s)
82433965Sjdp     lang_output_section_statement_type *s;
82533965Sjdp{
82633965Sjdp  section_userdata_type *new;
82733965Sjdp
82833965Sjdp  if (s->bfd_section != NULL)
82933965Sjdp    return;
83033965Sjdp
83133965Sjdp  if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
83289857Sobrien    einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
83333965Sjdp
83433965Sjdp  new = ((section_userdata_type *)
83533965Sjdp	 stat_alloc (sizeof (section_userdata_type)));
83633965Sjdp
83733965Sjdp  s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
83833965Sjdp  if (s->bfd_section == (asection *) NULL)
83933965Sjdp    s->bfd_section = bfd_make_section (output_bfd, s->name);
84033965Sjdp  if (s->bfd_section == (asection *) NULL)
84133965Sjdp    {
84260484Sobrien      einfo (_("%P%F: output format %s cannot represent section called %s\n"),
84333965Sjdp	     output_bfd->xvec->name, s->name);
84433965Sjdp    }
84533965Sjdp  s->bfd_section->output_section = s->bfd_section;
84633965Sjdp
84777298Sobrien  /* We initialize an output sections output offset to minus its own
84877298Sobrien     vma to allow us to output a section through itself.  */
84933965Sjdp  s->bfd_section->output_offset = 0;
85033965Sjdp  get_userdata (s->bfd_section) = (PTR) new;
85133965Sjdp
85233965Sjdp  /* If there is a base address, make sure that any sections it might
85333965Sjdp     mention are initialized.  */
85433965Sjdp  if (s->addr_tree != NULL)
85533965Sjdp    exp_init_os (s->addr_tree);
85633965Sjdp}
85733965Sjdp
85833965Sjdp/* Make sure that all output sections mentioned in an expression are
85933965Sjdp   initialized.  */
86033965Sjdp
86133965Sjdpstatic void
86233965Sjdpexp_init_os (exp)
86333965Sjdp     etree_type *exp;
86433965Sjdp{
86533965Sjdp  switch (exp->type.node_class)
86633965Sjdp    {
86733965Sjdp    case etree_assign:
86833965Sjdp      exp_init_os (exp->assign.src);
86933965Sjdp      break;
87033965Sjdp
87133965Sjdp    case etree_binary:
87233965Sjdp      exp_init_os (exp->binary.lhs);
87333965Sjdp      exp_init_os (exp->binary.rhs);
87433965Sjdp      break;
87533965Sjdp
87633965Sjdp    case etree_trinary:
87733965Sjdp      exp_init_os (exp->trinary.cond);
87833965Sjdp      exp_init_os (exp->trinary.lhs);
87933965Sjdp      exp_init_os (exp->trinary.rhs);
88033965Sjdp      break;
88133965Sjdp
88233965Sjdp    case etree_unary:
88333965Sjdp      exp_init_os (exp->unary.child);
88433965Sjdp      break;
88533965Sjdp
88633965Sjdp    case etree_name:
88733965Sjdp      switch (exp->type.node_code)
88833965Sjdp	{
88933965Sjdp	case ADDR:
89033965Sjdp	case LOADADDR:
89133965Sjdp	case SIZEOF:
89233965Sjdp	  {
89333965Sjdp	    lang_output_section_statement_type *os;
89433965Sjdp
89533965Sjdp	    os = lang_output_section_find (exp->name.name);
89633965Sjdp	    if (os != NULL && os->bfd_section == NULL)
89733965Sjdp	      init_os (os);
89833965Sjdp	  }
89933965Sjdp	}
90033965Sjdp      break;
90133965Sjdp
90233965Sjdp    default:
90333965Sjdp      break;
90433965Sjdp    }
90533965Sjdp}
90660484Sobrien
90733965Sjdp/* Sections marked with the SEC_LINK_ONCE flag should only be linked
90860484Sobrien   once into the output.  This routine checks each section, and
90960484Sobrien   arrange to discard it if a section of the same name has already
91060484Sobrien   been linked.  If the section has COMDAT information, then it uses
91160484Sobrien   that to decide whether the section should be included.  This code
91260484Sobrien   assumes that all relevant sections have the SEC_LINK_ONCE flag set;
91360484Sobrien   that is, it does not depend solely upon the section name.
91460484Sobrien   section_already_linked is called via bfd_map_over_sections.  */
91533965Sjdp
91660484Sobrien/* This is the shape of the elements inside the already_linked hash
91760484Sobrien   table. It maps a name onto a list of already_linked elements with
91860484Sobrien   the same name.  It's possible to get more than one element in a
91960484Sobrien   list if the COMDAT sections have different names.  */
92060484Sobrien
92177298Sobrienstruct already_linked_hash_entry
92260484Sobrien{
92360484Sobrien  struct bfd_hash_entry root;
92460484Sobrien  struct already_linked *entry;
92560484Sobrien};
92660484Sobrien
92777298Sobrienstruct already_linked
92860484Sobrien{
92960484Sobrien  struct already_linked *next;
93060484Sobrien  asection *sec;
93160484Sobrien};
93260484Sobrien
93360484Sobrien/* The hash table.  */
93460484Sobrien
93560484Sobrienstatic struct bfd_hash_table already_linked_table;
93660484Sobrien
93733965Sjdpstatic void
93833965Sjdpsection_already_linked (abfd, sec, data)
93933965Sjdp     bfd *abfd;
94033965Sjdp     asection *sec;
94133965Sjdp     PTR data;
94233965Sjdp{
94333965Sjdp  lang_input_statement_type *entry = (lang_input_statement_type *) data;
94433965Sjdp  flagword flags;
94533965Sjdp  const char *name;
94660484Sobrien  struct already_linked *l;
94760484Sobrien  struct already_linked_hash_entry *already_linked_list;
94833965Sjdp
94933965Sjdp  /* If we are only reading symbols from this object, then we want to
95033965Sjdp     discard all sections.  */
95133965Sjdp  if (entry->just_syms_flag)
95233965Sjdp    {
95333965Sjdp      sec->output_section = bfd_abs_section_ptr;
95433965Sjdp      sec->output_offset = sec->vma;
95533965Sjdp      return;
95633965Sjdp    }
95733965Sjdp
95833965Sjdp  flags = bfd_get_section_flags (abfd, sec);
95933965Sjdp
96033965Sjdp  if ((flags & SEC_LINK_ONCE) == 0)
96133965Sjdp    return;
96233965Sjdp
96377298Sobrien  /* FIXME: When doing a relocatable link, we may have trouble
96460484Sobrien     copying relocations in other sections that refer to local symbols
96560484Sobrien     in the section being discarded.  Those relocations will have to
96660484Sobrien     be converted somehow; as of this writing I'm not sure that any of
96760484Sobrien     the backends handle that correctly.
96860484Sobrien
96960484Sobrien     It is tempting to instead not discard link once sections when
97077298Sobrien     doing a relocatable link (technically, they should be discarded
97160484Sobrien     whenever we are building constructors).  However, that fails,
97260484Sobrien     because the linker winds up combining all the link once sections
97360484Sobrien     into a single large link once section, which defeats the purpose
97460484Sobrien     of having link once sections in the first place.
97560484Sobrien
97677298Sobrien     Also, not merging link once sections in a relocatable link
97760484Sobrien     causes trouble for MIPS ELF, which relies in link once semantics
97860484Sobrien     to handle the .reginfo section correctly.  */
97960484Sobrien
98033965Sjdp  name = bfd_get_section_name (abfd, sec);
98133965Sjdp
98277298Sobrien  already_linked_list =
98360484Sobrien    ((struct already_linked_hash_entry *)
98460484Sobrien     bfd_hash_lookup (&already_linked_table, name, true, false));
98560484Sobrien
98677298Sobrien  for (l = already_linked_list->entry; l != NULL; l = l->next)
98733965Sjdp    {
98860484Sobrien      if (sec->comdat == NULL
98960484Sobrien	  || l->sec->comdat == NULL
99060484Sobrien	  || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
99133965Sjdp	{
99233965Sjdp	  /* The section has already been linked.  See if we should
99333965Sjdp             issue a warning.  */
99433965Sjdp	  switch (flags & SEC_LINK_DUPLICATES)
99533965Sjdp	    {
99633965Sjdp	    default:
99733965Sjdp	      abort ();
99833965Sjdp
99933965Sjdp	    case SEC_LINK_DUPLICATES_DISCARD:
100033965Sjdp	      break;
100133965Sjdp
100233965Sjdp	    case SEC_LINK_DUPLICATES_ONE_ONLY:
100360484Sobrien	      if (sec->comdat == NULL)
100460484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
100560484Sobrien		       abfd, name);
100660484Sobrien	      else
100760484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
100860484Sobrien		       abfd, name, sec->comdat->name);
100933965Sjdp	      break;
101033965Sjdp
101133965Sjdp	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
101233965Sjdp	      /* FIXME: We should really dig out the contents of both
101333965Sjdp                 sections and memcmp them.  The COFF/PE spec says that
101433965Sjdp                 the Microsoft linker does not implement this
101533965Sjdp                 correctly, so I'm not going to bother doing it
101633965Sjdp                 either.  */
101733965Sjdp	      /* Fall through.  */
101833965Sjdp	    case SEC_LINK_DUPLICATES_SAME_SIZE:
101933965Sjdp	      if (bfd_section_size (abfd, sec)
102033965Sjdp		  != bfd_section_size (l->sec->owner, l->sec))
102160484Sobrien		einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
102233965Sjdp		       abfd, name);
102333965Sjdp	      break;
102433965Sjdp	    }
102533965Sjdp
102689857Sobrien	  /* Set the output_section field so that lang_add_section
102789857Sobrien	     does not create a lang_input_section structure for this
102889857Sobrien	     section.  */
102933965Sjdp	  sec->output_section = bfd_abs_section_ptr;
103033965Sjdp
103133965Sjdp	  return;
103233965Sjdp	}
103333965Sjdp    }
103433965Sjdp
103560484Sobrien  /* This is the first section with this name.  Record it.  Allocate
103660484Sobrien     the memory from the same obstack as the hash table is kept in.  */
103733965Sjdp
103877298Sobrien  l = ((struct already_linked *)
103960484Sobrien       bfd_hash_allocate (&already_linked_table, sizeof *l));
104060484Sobrien
104133965Sjdp  l->sec = sec;
104260484Sobrien  l->next = already_linked_list->entry;
104360484Sobrien  already_linked_list->entry = l;
104433965Sjdp}
104560484Sobrien
104660484Sobrien/* Support routines for the hash table used by section_already_linked,
104760484Sobrien   initialize the table, fill in an entry and remove the table.  */
104860484Sobrien
104960484Sobrienstatic struct bfd_hash_entry *
105060484Sobrienalready_linked_newfunc (entry, table, string)
105160484Sobrien     struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
105260484Sobrien     struct bfd_hash_table *table;
105360484Sobrien     const char *string ATTRIBUTE_UNUSED;
105460484Sobrien{
105577298Sobrien  struct already_linked_hash_entry *ret =
105660484Sobrien    bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
105760484Sobrien
105860484Sobrien  ret->entry = NULL;
105960484Sobrien
106060484Sobrien  return (struct bfd_hash_entry *) ret;
106160484Sobrien}
106260484Sobrien
106360484Sobrienstatic void
106460484Sobrienalready_linked_table_init ()
106560484Sobrien{
106660484Sobrien  if (! bfd_hash_table_init_n (&already_linked_table,
106760484Sobrien			       already_linked_newfunc,
106860484Sobrien			       42))
106960484Sobrien    einfo (_("%P%F: Failed to create hash table\n"));
107060484Sobrien}
107160484Sobrien
107260484Sobrienstatic void
107360484Sobrienalready_linked_table_free ()
107460484Sobrien{
107560484Sobrien  bfd_hash_table_free (&already_linked_table);
107660484Sobrien}
107733965Sjdp
107833965Sjdp/* The wild routines.
107933965Sjdp
108033965Sjdp   These expand statements like *(.text) and foo.o to a list of
108133965Sjdp   explicit actions, like foo.o(.text), bar.o(.text) and
108233965Sjdp   foo.o(.text, .data).  */
108333965Sjdp
108438889Sjdp/* Return true if the PATTERN argument is a wildcard pattern.
108538889Sjdp   Although backslashes are treated specially if a pattern contains
108638889Sjdp   wildcards, we do not consider the mere presence of a backslash to
108789857Sobrien   be enough to cause the pattern to be treated as a wildcard.
108838889Sjdp   That lets us handle DOS filenames more naturally.  */
108933965Sjdp
109033965Sjdpstatic boolean
109133965Sjdpwildcardp (pattern)
109233965Sjdp     const char *pattern;
109333965Sjdp{
109433965Sjdp  const char *s;
109533965Sjdp
109633965Sjdp  for (s = pattern; *s != '\0'; ++s)
109733965Sjdp    if (*s == '?'
109833965Sjdp	|| *s == '*'
109933965Sjdp	|| *s == '[')
110033965Sjdp      return true;
110133965Sjdp  return false;
110233965Sjdp}
110333965Sjdp
110433965Sjdp/* Add SECTION to the output section OUTPUT.  Do this by creating a
110533965Sjdp   lang_input_section statement which is placed at PTR.  FILE is the
110633965Sjdp   input file which holds SECTION.  */
110733965Sjdp
110833965Sjdpvoid
110989857Sobrienlang_add_section (ptr, section, output, file)
111033965Sjdp     lang_statement_list_type *ptr;
111133965Sjdp     asection *section;
111233965Sjdp     lang_output_section_statement_type *output;
111333965Sjdp     lang_input_statement_type *file;
111433965Sjdp{
111533965Sjdp  flagword flags;
111633965Sjdp  boolean discard;
111733965Sjdp
111833965Sjdp  flags = bfd_get_section_flags (section->owner, section);
111933965Sjdp
112033965Sjdp  discard = false;
112133965Sjdp
112233965Sjdp  /* If we are doing a final link, discard sections marked with
112333965Sjdp     SEC_EXCLUDE.  */
112433965Sjdp  if (! link_info.relocateable
112533965Sjdp      && (flags & SEC_EXCLUDE) != 0)
112633965Sjdp    discard = true;
112733965Sjdp
112833965Sjdp  /* Discard input sections which are assigned to a section named
112933965Sjdp     DISCARD_SECTION_NAME.  */
113033965Sjdp  if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
113133965Sjdp    discard = true;
113233965Sjdp
113333965Sjdp  /* Discard debugging sections if we are stripping debugging
113433965Sjdp     information.  */
113533965Sjdp  if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
113633965Sjdp      && (flags & SEC_DEBUGGING) != 0)
113733965Sjdp    discard = true;
113833965Sjdp
113933965Sjdp  if (discard)
114033965Sjdp    {
114133965Sjdp      if (section->output_section == NULL)
114233965Sjdp	{
114333965Sjdp	  /* This prevents future calls from assigning this section.  */
114433965Sjdp	  section->output_section = bfd_abs_section_ptr;
114533965Sjdp	}
114633965Sjdp      return;
114733965Sjdp    }
114833965Sjdp
114933965Sjdp  if (section->output_section == NULL)
115033965Sjdp    {
115138889Sjdp      boolean first;
115233965Sjdp      lang_input_section_type *new;
115338889Sjdp      flagword flags;
115433965Sjdp
115533965Sjdp      if (output->bfd_section == NULL)
115678828Sobrien	init_os (output);
115733965Sjdp
115878828Sobrien      first = ! output->bfd_section->linker_has_input;
115978828Sobrien      output->bfd_section->linker_has_input = 1;
116078828Sobrien
116177298Sobrien      /* Add a section reference to the list.  */
116233965Sjdp      new = new_stat (lang_input_section, ptr);
116333965Sjdp
116433965Sjdp      new->section = section;
116533965Sjdp      new->ifile = file;
116633965Sjdp      section->output_section = output->bfd_section;
116733965Sjdp
116838889Sjdp      flags = section->flags;
116938889Sjdp
117033965Sjdp      /* We don't copy the SEC_NEVER_LOAD flag from an input section
117133965Sjdp	 to an output section, because we want to be able to include a
117233965Sjdp	 SEC_NEVER_LOAD section in the middle of an otherwise loaded
117333965Sjdp	 section (I don't know why we want to do this, but we do).
117433965Sjdp	 build_link_order in ldwrite.c handles this case by turning
117538889Sjdp	 the embedded SEC_NEVER_LOAD section into a fill.  */
117633965Sjdp
117738889Sjdp      flags &= ~ SEC_NEVER_LOAD;
117833965Sjdp
117938889Sjdp      /* If final link, don't copy the SEC_LINK_ONCE flags, they've
118038889Sjdp	 already been processed.  One reason to do this is that on pe
118138889Sjdp	 format targets, .text$foo sections go into .text and it's odd
118238889Sjdp	 to see .text with SEC_LINK_ONCE set.  */
118333965Sjdp
118438889Sjdp      if (! link_info.relocateable)
118538889Sjdp	flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
118638889Sjdp
118738889Sjdp      /* If this is not the first input section, and the SEC_READONLY
118838889Sjdp         flag is not currently set, then don't set it just because the
118938889Sjdp         input section has it set.  */
119038889Sjdp
119138889Sjdp      if (! first && (section->output_section->flags & SEC_READONLY) == 0)
119238889Sjdp	flags &= ~ SEC_READONLY;
119338889Sjdp
119489857Sobrien      /* Keep SEC_MERGE and SEC_STRINGS only if they are the same.  */
119589857Sobrien      if (! first
119689857Sobrien	  && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
119789857Sobrien	      != (flags & (SEC_MERGE | SEC_STRINGS))
119889857Sobrien	      || ((flags & SEC_MERGE)
119989857Sobrien		  && section->output_section->entsize != section->entsize)))
120089857Sobrien	{
120189857Sobrien	  section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
120289857Sobrien	  flags &= ~ (SEC_MERGE | SEC_STRINGS);
120389857Sobrien	}
120489857Sobrien
120538889Sjdp      section->output_section->flags |= flags;
120638889Sjdp
120789857Sobrien      if (flags & SEC_MERGE)
120889857Sobrien	section->output_section->entsize = section->entsize;
120989857Sobrien
121038889Sjdp      /* If SEC_READONLY is not set in the input section, then clear
121138889Sjdp         it from the output section.  */
121238889Sjdp      if ((section->flags & SEC_READONLY) == 0)
121338889Sjdp	section->output_section->flags &= ~SEC_READONLY;
121438889Sjdp
121533965Sjdp      switch (output->sectype)
121633965Sjdp	{
121733965Sjdp	case normal_section:
121833965Sjdp	  break;
121933965Sjdp	case dsect_section:
122033965Sjdp	case copy_section:
122133965Sjdp	case info_section:
122233965Sjdp	case overlay_section:
122333965Sjdp	  output->bfd_section->flags &= ~SEC_ALLOC;
122433965Sjdp	  break;
122533965Sjdp	case noload_section:
122633965Sjdp	  output->bfd_section->flags &= ~SEC_LOAD;
122733965Sjdp	  output->bfd_section->flags |= SEC_NEVER_LOAD;
122833965Sjdp	  break;
122933965Sjdp	}
123033965Sjdp
123160484Sobrien      /* Copy over SEC_SMALL_DATA.  */
123260484Sobrien      if (section->flags & SEC_SMALL_DATA)
123360484Sobrien	section->output_section->flags |= SEC_SMALL_DATA;
123460484Sobrien
123533965Sjdp      if (section->alignment_power > output->bfd_section->alignment_power)
123633965Sjdp	output->bfd_section->alignment_power = section->alignment_power;
123733965Sjdp
123833965Sjdp      /* If supplied an aligment, then force it.  */
123933965Sjdp      if (output->section_alignment != -1)
124033965Sjdp	output->bfd_section->alignment_power = output->section_alignment;
124177298Sobrien
124277298Sobrien      if (section->flags & SEC_BLOCK)
124377298Sobrien	{
124477298Sobrien	  section->output_section->flags |= SEC_BLOCK;
124577298Sobrien	  /* FIXME: This value should really be obtained from the bfd...  */
124677298Sobrien	  output->block_value = 128;
124777298Sobrien	}
124833965Sjdp    }
124933965Sjdp}
125033965Sjdp
125160484Sobrien/* Handle wildcard sorting.  This returns the lang_input_section which
125260484Sobrien   should follow the one we are going to create for SECTION and FILE,
125360484Sobrien   based on the sorting requirements of WILD.  It returns NULL if the
125460484Sobrien   new section should just go at the end of the current list.  */
125533965Sjdp
125660484Sobrienstatic lang_statement_union_type *
125789857Sobrienwild_sort (wild, sec, file, section)
125860484Sobrien     lang_wild_statement_type *wild;
125989857Sobrien     struct wildcard_list *sec;
126033965Sjdp     lang_input_statement_type *file;
126160484Sobrien     asection *section;
126233965Sjdp{
126360484Sobrien  const char *section_name;
126460484Sobrien  lang_statement_union_type *l;
126560484Sobrien
126689857Sobrien  if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
126760484Sobrien    return NULL;
126860484Sobrien
126960484Sobrien  section_name = bfd_get_section_name (file->the_bfd, section);
127089857Sobrien  for (l = wild->children.head; l != NULL; l = l->header.next)
127133965Sjdp    {
127260484Sobrien      lang_input_section_type *ls;
127333965Sjdp
127460484Sobrien      if (l->header.type != lang_input_section_enum)
127560484Sobrien	continue;
127660484Sobrien      ls = &l->input_section;
127733965Sjdp
127860484Sobrien      /* Sorting by filename takes precedence over sorting by section
127960484Sobrien         name.  */
128060484Sobrien
128160484Sobrien      if (wild->filenames_sorted)
128233965Sjdp	{
128360484Sobrien	  const char *fn, *ln;
128460484Sobrien	  boolean fa, la;
128560484Sobrien	  int i;
128633965Sjdp
128760484Sobrien	  /* The PE support for the .idata section as generated by
128860484Sobrien             dlltool assumes that files will be sorted by the name of
128960484Sobrien             the archive and then the name of the file within the
129060484Sobrien             archive.  */
129133965Sjdp
129260484Sobrien	  if (file->the_bfd != NULL
129360484Sobrien	      && bfd_my_archive (file->the_bfd) != NULL)
129460484Sobrien	    {
129560484Sobrien	      fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
129660484Sobrien	      fa = true;
129760484Sobrien	    }
129860484Sobrien	  else
129960484Sobrien	    {
130060484Sobrien	      fn = file->filename;
130160484Sobrien	      fa = false;
130260484Sobrien	    }
130333965Sjdp
130460484Sobrien	  if (ls->ifile->the_bfd != NULL
130560484Sobrien	      && bfd_my_archive (ls->ifile->the_bfd) != NULL)
130660484Sobrien	    {
130760484Sobrien	      ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
130860484Sobrien	      la = true;
130960484Sobrien	    }
131033965Sjdp	  else
131133965Sjdp	    {
131260484Sobrien	      ln = ls->ifile->filename;
131360484Sobrien	      la = false;
131460484Sobrien	    }
131533965Sjdp
131660484Sobrien	  i = strcmp (fn, ln);
131760484Sobrien	  if (i > 0)
131860484Sobrien	    continue;
131960484Sobrien	  else if (i < 0)
132060484Sobrien	    break;
132160484Sobrien
132260484Sobrien	  if (fa || la)
132360484Sobrien	    {
132460484Sobrien	      if (fa)
132560484Sobrien		fn = file->filename;
132660484Sobrien	      if (la)
132760484Sobrien		ln = ls->ifile->filename;
132860484Sobrien
132960484Sobrien	      i = strcmp (fn, ln);
133060484Sobrien	      if (i > 0)
133160484Sobrien		continue;
133260484Sobrien	      else if (i < 0)
133360484Sobrien		break;
133433965Sjdp	    }
133533965Sjdp	}
133660484Sobrien
133760484Sobrien      /* Here either the files are not sorted by name, or we are
133860484Sobrien         looking at the sections for this file.  */
133960484Sobrien
134089857Sobrien      if (sec != NULL && sec->spec.sorted)
134160484Sobrien	{
134260484Sobrien	  if (strcmp (section_name,
134360484Sobrien		      bfd_get_section_name (ls->ifile->the_bfd,
134460484Sobrien					    ls->section))
134560484Sobrien	      < 0)
134660484Sobrien	    break;
134760484Sobrien	}
134833965Sjdp    }
134960484Sobrien
135060484Sobrien  return l;
135133965Sjdp}
135233965Sjdp
135360484Sobrien/* Expand a wild statement for a particular FILE.  SECTION may be
135460484Sobrien   NULL, in which case it is a wild card.  */
135560484Sobrien
135660484Sobrienstatic void
135789857Sobrienoutput_section_callback (ptr, sec, section, file, output)
135860484Sobrien     lang_wild_statement_type *ptr;
135989857Sobrien     struct wildcard_list *sec;
136060484Sobrien     asection *section;
136160484Sobrien     lang_input_statement_type *file;
136277298Sobrien     PTR output;
136360484Sobrien{
136460484Sobrien  lang_statement_union_type *before;
136577298Sobrien
136689857Sobrien  /* Exclude sections that match UNIQUE_SECTION_LIST.  */
136789857Sobrien  if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
136889857Sobrien    return;
136989857Sobrien
137060484Sobrien  /* If the wild pattern was marked KEEP, the member sections
137160484Sobrien     should be as well.  */
137260484Sobrien  if (ptr->keep_sections)
137360484Sobrien    section->flags |= SEC_KEEP;
137477298Sobrien
137589857Sobrien  before = wild_sort (ptr, sec, file, section);
137677298Sobrien
137760484Sobrien  /* Here BEFORE points to the lang_input_section which
137860484Sobrien     should follow the one we are about to add.  If BEFORE
137960484Sobrien     is NULL, then the section should just go at the end
138060484Sobrien     of the current list.  */
138177298Sobrien
138260484Sobrien  if (before == NULL)
138389857Sobrien    lang_add_section (&ptr->children, section,
138489857Sobrien		      (lang_output_section_statement_type *) output,
138589857Sobrien		      file);
138660484Sobrien  else
138760484Sobrien    {
138860484Sobrien      lang_statement_list_type list;
138960484Sobrien      lang_statement_union_type **pp;
139077298Sobrien
139160484Sobrien      lang_list_init (&list);
139289857Sobrien      lang_add_section (&list, section,
139389857Sobrien			(lang_output_section_statement_type *) output,
139489857Sobrien			file);
139577298Sobrien
139660484Sobrien      /* If we are discarding the section, LIST.HEAD will
139760484Sobrien	 be NULL.  */
139860484Sobrien      if (list.head != NULL)
139960484Sobrien	{
140089857Sobrien	  ASSERT (list.head->header.next == NULL);
140177298Sobrien
140260484Sobrien	  for (pp = &ptr->children.head;
140360484Sobrien	       *pp != before;
140489857Sobrien	       pp = &(*pp)->header.next)
140560484Sobrien	    ASSERT (*pp != NULL);
140677298Sobrien
140789857Sobrien	  list.head->header.next = *pp;
140860484Sobrien	  *pp = list.head;
140960484Sobrien	}
141060484Sobrien    }
141160484Sobrien}
141260484Sobrien
141333965Sjdp/* This is passed a file name which must have been seen already and
141433965Sjdp   added to the statement tree.  We will see if it has been opened
141533965Sjdp   already and had its symbols read.  If not then we'll read it.  */
141633965Sjdp
141733965Sjdpstatic lang_input_statement_type *
141833965Sjdplookup_name (name)
141933965Sjdp     const char *name;
142033965Sjdp{
142133965Sjdp  lang_input_statement_type *search;
142233965Sjdp
142333965Sjdp  for (search = (lang_input_statement_type *) input_file_chain.head;
142433965Sjdp       search != (lang_input_statement_type *) NULL;
142533965Sjdp       search = (lang_input_statement_type *) search->next_real_file)
142633965Sjdp    {
142733965Sjdp      if (search->filename == (char *) NULL && name == (char *) NULL)
142833965Sjdp	return search;
142933965Sjdp      if (search->filename != (char *) NULL
143033965Sjdp	  && name != (char *) NULL
143133965Sjdp	  && strcmp (search->filename, name) == 0)
143233965Sjdp	break;
143333965Sjdp    }
143433965Sjdp
143533965Sjdp  if (search == (lang_input_statement_type *) NULL)
143633965Sjdp    search = new_afile (name, lang_input_file_is_file_enum, default_target,
143733965Sjdp			false);
143833965Sjdp
143933965Sjdp  /* If we have already added this file, or this file is not real
144033965Sjdp     (FIXME: can that ever actually happen?) or the name is NULL
144133965Sjdp     (FIXME: can that ever actually happen?) don't add this file.  */
144233965Sjdp  if (search->loaded
144333965Sjdp      || ! search->real
144433965Sjdp      || search->filename == (const char *) NULL)
144533965Sjdp    return search;
144633965Sjdp
144789857Sobrien  if (! load_symbols (search, (lang_statement_list_type *) NULL))
144889857Sobrien    return NULL;
144933965Sjdp
145033965Sjdp  return search;
145133965Sjdp}
145233965Sjdp
145333965Sjdp/* Get the symbols for an input file.  */
145433965Sjdp
145589857Sobrienstatic boolean
145633965Sjdpload_symbols (entry, place)
145733965Sjdp     lang_input_statement_type *entry;
145833965Sjdp     lang_statement_list_type *place;
145933965Sjdp{
146033965Sjdp  char **matching;
146133965Sjdp
146233965Sjdp  if (entry->loaded)
146389857Sobrien    return true;
146433965Sjdp
146533965Sjdp  ldfile_open_file (entry);
146633965Sjdp
146733965Sjdp  if (! bfd_check_format (entry->the_bfd, bfd_archive)
146833965Sjdp      && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
146933965Sjdp    {
147033965Sjdp      bfd_error_type err;
147133965Sjdp      lang_statement_list_type *hold;
147289857Sobrien      boolean bad_load = true;
147389857Sobrien
147433965Sjdp      err = bfd_get_error ();
147578828Sobrien
147678828Sobrien      /* See if the emulation has some special knowledge.  */
147778828Sobrien      if (ldemul_unrecognized_file (entry))
147889857Sobrien	return true;
147978828Sobrien
148033965Sjdp      if (err == bfd_error_file_ambiguously_recognized)
148133965Sjdp	{
148233965Sjdp	  char **p;
148333965Sjdp
148460484Sobrien	  einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
148560484Sobrien	  einfo (_("%B: matching formats:"), entry->the_bfd);
148633965Sjdp	  for (p = matching; *p != NULL; p++)
148733965Sjdp	    einfo (" %s", *p);
148833965Sjdp	  einfo ("%F\n");
148933965Sjdp	}
149033965Sjdp      else if (err != bfd_error_file_not_recognized
149133965Sjdp	       || place == NULL)
149289857Sobrien	  einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
149389857Sobrien      else
149489857Sobrien	bad_load = false;
149589857Sobrien
149633965Sjdp      bfd_close (entry->the_bfd);
149733965Sjdp      entry->the_bfd = NULL;
149833965Sjdp
149933965Sjdp      /* Try to interpret the file as a linker script.  */
150033965Sjdp      ldfile_open_command_file (entry->filename);
150133965Sjdp
150233965Sjdp      hold = stat_ptr;
150333965Sjdp      stat_ptr = place;
150433965Sjdp
150533965Sjdp      ldfile_assumed_script = true;
150633965Sjdp      parser_input = input_script;
150733965Sjdp      yyparse ();
150833965Sjdp      ldfile_assumed_script = false;
150933965Sjdp
151033965Sjdp      stat_ptr = hold;
151133965Sjdp
151289857Sobrien      return ! bad_load;
151333965Sjdp    }
151433965Sjdp
151560484Sobrien  if (ldemul_recognized_file (entry))
151689857Sobrien    return true;
151760484Sobrien
151833965Sjdp  /* We don't call ldlang_add_file for an archive.  Instead, the
151933965Sjdp     add_symbols entry point will call ldlang_add_file, via the
152033965Sjdp     add_archive_element callback, for each element of the archive
152133965Sjdp     which is used.  */
152233965Sjdp  switch (bfd_get_format (entry->the_bfd))
152333965Sjdp    {
152433965Sjdp    default:
152533965Sjdp      break;
152633965Sjdp
152733965Sjdp    case bfd_object:
152833965Sjdp      ldlang_add_file (entry);
152933965Sjdp      if (trace_files || trace_file_tries)
153033965Sjdp	info_msg ("%I\n", entry);
153133965Sjdp      break;
153233965Sjdp
153333965Sjdp    case bfd_archive:
153433965Sjdp      if (entry->whole_archive)
153533965Sjdp	{
153689857Sobrien	  bfd * member = NULL;
153789857Sobrien	  boolean loaded = true;
153889857Sobrien
153989857Sobrien	  for (;;)
154033965Sjdp	    {
154189857Sobrien	      member = bfd_openr_next_archived_file (entry->the_bfd, member);
154289857Sobrien
154389857Sobrien	      if (member == NULL)
154489857Sobrien		break;
154589857Sobrien
154633965Sjdp	      if (! bfd_check_format (member, bfd_object))
154789857Sobrien		{
154889857Sobrien		  einfo (_("%F%B: member %B in archive is not an object\n"),
154989857Sobrien			 entry->the_bfd, member);
155089857Sobrien		  loaded = false;
155189857Sobrien		}
155289857Sobrien
155333965Sjdp	      if (! ((*link_info.callbacks->add_archive_element)
155433965Sjdp		     (&link_info, member, "--whole-archive")))
155533965Sjdp		abort ();
155689857Sobrien
155733965Sjdp	      if (! bfd_link_add_symbols (member, &link_info))
155889857Sobrien		{
155989857Sobrien		  einfo (_("%F%B: could not read symbols: %E\n"), member);
156089857Sobrien		  loaded = false;
156189857Sobrien		}
156233965Sjdp	    }
156333965Sjdp
156489857Sobrien	  entry->loaded = loaded;
156589857Sobrien	  return loaded;
156633965Sjdp	}
156789857Sobrien      break;
156833965Sjdp    }
156933965Sjdp
157089857Sobrien  if (bfd_link_add_symbols (entry->the_bfd, &link_info))
157189857Sobrien    entry->loaded = true;
157289857Sobrien  else
157360484Sobrien    einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
157433965Sjdp
157589857Sobrien  return entry->loaded;
157633965Sjdp}
157733965Sjdp
157889857Sobrien/* Handle a wild statement.  S->FILENAME or S->SECTION_LIST or both
157989857Sobrien   may be NULL, indicating that it is a wildcard.  Separate
158089857Sobrien   lang_input_section statements are created for each part of the
158189857Sobrien   expansion; they are added after the wild statement S.  OUTPUT is
158289857Sobrien   the output section.  */
158333965Sjdp
158433965Sjdpstatic void
158589857Sobrienwild (s, target, output)
158633965Sjdp     lang_wild_statement_type *s;
158760484Sobrien     const char *target ATTRIBUTE_UNUSED;
158833965Sjdp     lang_output_section_statement_type *output;
158933965Sjdp{
159089857Sobrien  struct wildcard_list *sec;
159133965Sjdp
159289857Sobrien  walk_wild (s, output_section_callback, (PTR) output);
159389857Sobrien
159489857Sobrien  for (sec = s->section_list; sec != NULL; sec = sec->next)
159533965Sjdp    {
159689857Sobrien      if (default_common_section != NULL)
159789857Sobrien	break;
159889857Sobrien      if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
159989857Sobrien	{
160089857Sobrien	  /* Remember the section that common is going to in case we
160189857Sobrien	     later get something which doesn't know where to put it.  */
160289857Sobrien	  default_common_section = output;
160389857Sobrien	}
160433965Sjdp    }
160560484Sobrien}
160660484Sobrien
160760484Sobrien/* Return true iff target is the sought target.  */
160877298Sobrien
160960484Sobrienstatic int
161060484Sobrienget_target (target, data)
161177298Sobrien     const bfd_target *target;
161277298Sobrien     PTR data;
161360484Sobrien{
161477298Sobrien  const char *sought = (const char *) data;
161577298Sobrien
161660484Sobrien  return strcmp (target->name, sought) == 0;
161760484Sobrien}
161860484Sobrien
161960484Sobrien/* Like strcpy() but convert to lower case as well.  */
162077298Sobrien
162160484Sobrienstatic void
162260484Sobrienstricpy (dest, src)
162377298Sobrien     char *dest;
162477298Sobrien     char *src;
162560484Sobrien{
162660484Sobrien  char c;
162777298Sobrien
162877298Sobrien  while ((c = *src++) != 0)
162989857Sobrien    *dest++ = TOLOWER (c);
163060484Sobrien
163177298Sobrien  *dest = 0;
163260484Sobrien}
163360484Sobrien
163460484Sobrien/* Remove the first occurance of needle (if any) in haystack
163560484Sobrien   from haystack.  */
163677298Sobrien
163760484Sobrienstatic void
163860484Sobrienstrcut (haystack, needle)
163977298Sobrien     char *haystack;
164077298Sobrien     char *needle;
164160484Sobrien{
164260484Sobrien  haystack = strstr (haystack, needle);
164377298Sobrien
164460484Sobrien  if (haystack)
164533965Sjdp    {
164677298Sobrien      char *src;
164760484Sobrien
164877298Sobrien      for (src = haystack + strlen (needle); *src;)
164977298Sobrien	*haystack++ = *src++;
165077298Sobrien
165177298Sobrien      *haystack = 0;
165233965Sjdp    }
165360484Sobrien}
165433965Sjdp
165560484Sobrien/* Compare two target format name strings.
165660484Sobrien   Return a value indicating how "similar" they are.  */
165777298Sobrien
165860484Sobrienstatic int
165960484Sobrienname_compare (first, second)
166077298Sobrien     char *first;
166177298Sobrien     char *second;
166260484Sobrien{
166377298Sobrien  char *copy1;
166477298Sobrien  char *copy2;
166577298Sobrien  int result;
166677298Sobrien
166760484Sobrien  copy1 = xmalloc (strlen (first) + 1);
166860484Sobrien  copy2 = xmalloc (strlen (second) + 1);
166960484Sobrien
167060484Sobrien  /* Convert the names to lower case.  */
167160484Sobrien  stricpy (copy1, first);
167260484Sobrien  stricpy (copy2, second);
167360484Sobrien
167460484Sobrien  /* Remove and endian strings from the name.  */
167560484Sobrien  strcut (copy1, "big");
167660484Sobrien  strcut (copy1, "little");
167760484Sobrien  strcut (copy2, "big");
167860484Sobrien  strcut (copy2, "little");
167960484Sobrien
168060484Sobrien  /* Return a value based on how many characters match,
168160484Sobrien     starting from the beginning.   If both strings are
168260484Sobrien     the same then return 10 * their length.  */
168377298Sobrien  for (result = 0; copy1[result] == copy2[result]; result++)
168477298Sobrien    if (copy1[result] == 0)
168560484Sobrien      {
168660484Sobrien	result *= 10;
168760484Sobrien	break;
168860484Sobrien      }
168977298Sobrien
169060484Sobrien  free (copy1);
169160484Sobrien  free (copy2);
169260484Sobrien
169360484Sobrien  return result;
169460484Sobrien}
169560484Sobrien
169660484Sobrien/* Set by closest_target_match() below.  */
169777298Sobrienstatic const bfd_target *winner;
169860484Sobrien
169960484Sobrien/* Scan all the valid bfd targets looking for one that has the endianness
170060484Sobrien   requirement that was specified on the command line, and is the nearest
170160484Sobrien   match to the original output target.  */
170277298Sobrien
170360484Sobrienstatic int
170460484Sobrienclosest_target_match (target, data)
170577298Sobrien     const bfd_target *target;
170677298Sobrien     PTR data;
170760484Sobrien{
170877298Sobrien  const bfd_target *original = (const bfd_target *) data;
170977298Sobrien
171077298Sobrien  if (command_line.endian == ENDIAN_BIG
171177298Sobrien      && target->byteorder != BFD_ENDIAN_BIG)
171260484Sobrien    return 0;
171377298Sobrien
171477298Sobrien  if (command_line.endian == ENDIAN_LITTLE
171577298Sobrien      && target->byteorder != BFD_ENDIAN_LITTLE)
171660484Sobrien    return 0;
171760484Sobrien
171860484Sobrien  /* Must be the same flavour.  */
171960484Sobrien  if (target->flavour != original->flavour)
172060484Sobrien    return 0;
172160484Sobrien
172260484Sobrien  /* If we have not found a potential winner yet, then record this one.  */
172360484Sobrien  if (winner == NULL)
172433965Sjdp    {
172560484Sobrien      winner = target;
172660484Sobrien      return 0;
172733965Sjdp    }
172860484Sobrien
172960484Sobrien  /* Oh dear, we now have two potential candidates for a successful match.
173077298Sobrien     Compare their names and choose the better one.  */
173178828Sobrien  if (name_compare (target->name, original->name)
173278828Sobrien      > name_compare (winner->name, original->name))
173360484Sobrien    winner = target;
173460484Sobrien
173560484Sobrien  /* Keep on searching until wqe have checked them all.  */
173660484Sobrien  return 0;
173733965Sjdp}
173833965Sjdp
173960484Sobrien/* Return the BFD target format of the first input file.  */
174077298Sobrien
174160484Sobrienstatic char *
174260484Sobrienget_first_input_target ()
174360484Sobrien{
174477298Sobrien  char *target = NULL;
174560484Sobrien
174660484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (s)
174760484Sobrien    {
174860484Sobrien      if (s->header.type == lang_input_statement_enum
174960484Sobrien	  && s->real)
175060484Sobrien	{
175160484Sobrien	  ldfile_open_file (s);
175277298Sobrien
175360484Sobrien	  if (s->the_bfd != NULL
175460484Sobrien	      && bfd_check_format (s->the_bfd, bfd_object))
175560484Sobrien	    {
175660484Sobrien	      target = bfd_get_target (s->the_bfd);
175777298Sobrien
175860484Sobrien	      if (target != NULL)
175960484Sobrien		break;
176060484Sobrien	    }
176160484Sobrien	}
176260484Sobrien    }
176377298Sobrien
176460484Sobrien  return target;
176560484Sobrien}
176660484Sobrien
176733965Sjdp/* Open the output file.  */
176833965Sjdp
176933965Sjdpstatic bfd *
177033965Sjdpopen_output (name)
177177298Sobrien     const char *name;
177233965Sjdp{
177377298Sobrien  bfd *output;
177433965Sjdp
177577298Sobrien  /* Has the user told us which output format to use?  */
177633965Sjdp  if (output_target == (char *) NULL)
177733965Sjdp    {
177877298Sobrien      /* No - has the current target been set to something other than
177977298Sobrien         the default?  */
178060484Sobrien      if (current_target != default_target)
178133965Sjdp	output_target = current_target;
178260484Sobrien
178377298Sobrien      /* No - can we determine the format of the first input file?  */
178433965Sjdp      else
178560484Sobrien	{
178660484Sobrien	  output_target = get_first_input_target ();
178760484Sobrien
178860484Sobrien	  /* Failed - use the default output target.  */
178960484Sobrien	  if (output_target == NULL)
179060484Sobrien	    output_target = default_target;
179160484Sobrien	}
179233965Sjdp    }
179377298Sobrien
179477298Sobrien  /* Has the user requested a particular endianness on the command
179577298Sobrien     line?  */
179660484Sobrien  if (command_line.endian != ENDIAN_UNSET)
179760484Sobrien    {
179877298Sobrien      const bfd_target *target;
179960484Sobrien      enum bfd_endian desired_endian;
180060484Sobrien
180160484Sobrien      /* Get the chosen target.  */
180277298Sobrien      target = bfd_search_for_target (get_target, (PTR) output_target);
180360484Sobrien
180477298Sobrien      /* If the target is not supported, we cannot do anything.  */
180577298Sobrien      if (target != NULL)
180660484Sobrien	{
180777298Sobrien	  if (command_line.endian == ENDIAN_BIG)
180877298Sobrien	    desired_endian = BFD_ENDIAN_BIG;
180960484Sobrien	  else
181077298Sobrien	    desired_endian = BFD_ENDIAN_LITTLE;
181177298Sobrien
181277298Sobrien	  /* See if the target has the wrong endianness.  This should
181377298Sobrien	     not happen if the linker script has provided big and
181477298Sobrien	     little endian alternatives, but some scrips don't do
181577298Sobrien	     this.  */
181677298Sobrien	  if (target->byteorder != desired_endian)
181760484Sobrien	    {
181877298Sobrien	      /* If it does, then see if the target provides
181977298Sobrien		 an alternative with the correct endianness.  */
182077298Sobrien	      if (target->alternative_target != NULL
182177298Sobrien		  && (target->alternative_target->byteorder == desired_endian))
182277298Sobrien		output_target = target->alternative_target->name;
182360484Sobrien	      else
182477298Sobrien		{
182577298Sobrien		  /* Try to find a target as similar as possible to
182677298Sobrien		     the default target, but which has the desired
182777298Sobrien		     endian characteristic.  */
182878828Sobrien		  (void) bfd_search_for_target (closest_target_match,
182978828Sobrien						(PTR) target);
183077298Sobrien
183177298Sobrien		  /* Oh dear - we could not find any targets that
183277298Sobrien		     satisfy our requirements.  */
183377298Sobrien		  if (winner == NULL)
183477298Sobrien		    einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
183577298Sobrien		  else
183677298Sobrien		    output_target = winner->name;
183777298Sobrien		}
183860484Sobrien	    }
183960484Sobrien	}
184060484Sobrien    }
184177298Sobrien
184233965Sjdp  output = bfd_openw (name, output_target);
184333965Sjdp
184433965Sjdp  if (output == (bfd *) NULL)
184533965Sjdp    {
184633965Sjdp      if (bfd_get_error () == bfd_error_invalid_target)
184760484Sobrien	einfo (_("%P%F: target %s not found\n"), output_target);
184860484Sobrien
184960484Sobrien      einfo (_("%P%F: cannot open output file %s: %E\n"), name);
185033965Sjdp    }
185133965Sjdp
185233965Sjdp  delete_output_file_on_failure = true;
185333965Sjdp
185477298Sobrien#if 0
185577298Sobrien  output->flags |= D_PAGED;
185677298Sobrien#endif
185733965Sjdp
185833965Sjdp  if (! bfd_set_format (output, bfd_object))
185960484Sobrien    einfo (_("%P%F:%s: can not make object file: %E\n"), name);
186033965Sjdp  if (! bfd_set_arch_mach (output,
186133965Sjdp			   ldfile_output_architecture,
186233965Sjdp			   ldfile_output_machine))
186360484Sobrien    einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
186433965Sjdp
186533965Sjdp  link_info.hash = bfd_link_hash_table_create (output);
186633965Sjdp  if (link_info.hash == (struct bfd_link_hash_table *) NULL)
186760484Sobrien    einfo (_("%P%F: can not create link hash table: %E\n"));
186833965Sjdp
186933965Sjdp  bfd_set_gp_size (output, g_switch_value);
187033965Sjdp  return output;
187133965Sjdp}
187233965Sjdp
187333965Sjdpstatic void
187433965Sjdpldlang_open_output (statement)
187577298Sobrien     lang_statement_union_type *statement;
187633965Sjdp{
187733965Sjdp  switch (statement->header.type)
187833965Sjdp    {
187933965Sjdp    case lang_output_statement_enum:
188033965Sjdp      ASSERT (output_bfd == (bfd *) NULL);
188133965Sjdp      output_bfd = open_output (statement->output_statement.name);
188233965Sjdp      ldemul_set_output_arch ();
188333965Sjdp      if (config.magic_demand_paged && !link_info.relocateable)
188433965Sjdp	output_bfd->flags |= D_PAGED;
188533965Sjdp      else
188633965Sjdp	output_bfd->flags &= ~D_PAGED;
188733965Sjdp      if (config.text_read_only)
188833965Sjdp	output_bfd->flags |= WP_TEXT;
188933965Sjdp      else
189033965Sjdp	output_bfd->flags &= ~WP_TEXT;
189133965Sjdp      if (link_info.traditional_format)
189233965Sjdp	output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
189333965Sjdp      else
189433965Sjdp	output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
189533965Sjdp      break;
189633965Sjdp
189733965Sjdp    case lang_target_statement_enum:
189833965Sjdp      current_target = statement->target_statement.target;
189933965Sjdp      break;
190033965Sjdp    default:
190133965Sjdp      break;
190233965Sjdp    }
190333965Sjdp}
190433965Sjdp
190533965Sjdp/* Open all the input files.  */
190633965Sjdp
190733965Sjdpstatic void
190833965Sjdpopen_input_bfds (s, force)
190933965Sjdp     lang_statement_union_type *s;
191033965Sjdp     boolean force;
191133965Sjdp{
191289857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
191333965Sjdp    {
191433965Sjdp      switch (s->header.type)
191533965Sjdp	{
191633965Sjdp	case lang_constructors_statement_enum:
191733965Sjdp	  open_input_bfds (constructor_list.head, force);
191833965Sjdp	  break;
191933965Sjdp	case lang_output_section_statement_enum:
192033965Sjdp	  open_input_bfds (s->output_section_statement.children.head, force);
192133965Sjdp	  break;
192233965Sjdp	case lang_wild_statement_enum:
192377298Sobrien	  /* Maybe we should load the file's symbols.  */
192433965Sjdp	  if (s->wild_statement.filename
192533965Sjdp	      && ! wildcardp (s->wild_statement.filename))
192633965Sjdp	    (void) lookup_name (s->wild_statement.filename);
192733965Sjdp	  open_input_bfds (s->wild_statement.children.head, force);
192833965Sjdp	  break;
192933965Sjdp	case lang_group_statement_enum:
193033965Sjdp	  {
193133965Sjdp	    struct bfd_link_hash_entry *undefs;
193233965Sjdp
193333965Sjdp	    /* We must continually search the entries in the group
193477298Sobrien	       until no new symbols are added to the list of undefined
193577298Sobrien	       symbols.  */
193633965Sjdp
193733965Sjdp	    do
193833965Sjdp	      {
193933965Sjdp		undefs = link_info.hash->undefs_tail;
194033965Sjdp		open_input_bfds (s->group_statement.children.head, true);
194133965Sjdp	      }
194233965Sjdp	    while (undefs != link_info.hash->undefs_tail);
194333965Sjdp	  }
194433965Sjdp	  break;
194533965Sjdp	case lang_target_statement_enum:
194633965Sjdp	  current_target = s->target_statement.target;
194733965Sjdp	  break;
194833965Sjdp	case lang_input_statement_enum:
194960484Sobrien	  if (s->input_statement.real)
195033965Sjdp	    {
195133965Sjdp	      lang_statement_list_type add;
195233965Sjdp
195333965Sjdp	      s->input_statement.target = current_target;
195433965Sjdp
195533965Sjdp	      /* If we are being called from within a group, and this
195633965Sjdp                 is an archive which has already been searched, then
195777298Sobrien                 force it to be researched unless the whole archive
195877298Sobrien		 has been loaded already.  */
195933965Sjdp	      if (force
196077298Sobrien		  && !s->input_statement.whole_archive
196133965Sjdp		  && s->input_statement.loaded
196233965Sjdp		  && bfd_check_format (s->input_statement.the_bfd,
196333965Sjdp				       bfd_archive))
196433965Sjdp		s->input_statement.loaded = false;
196533965Sjdp
196633965Sjdp	      lang_list_init (&add);
196733965Sjdp
196889857Sobrien	      if (! load_symbols (&s->input_statement, &add))
196989857Sobrien		config.make_executable = false;
197033965Sjdp
197133965Sjdp	      if (add.head != NULL)
197233965Sjdp		{
197389857Sobrien		  *add.tail = s->header.next;
197489857Sobrien		  s->header.next = add.head;
197533965Sjdp		}
197633965Sjdp	    }
197733965Sjdp	  break;
197833965Sjdp	default:
197933965Sjdp	  break;
198033965Sjdp	}
198133965Sjdp    }
198233965Sjdp}
198333965Sjdp
198477298Sobrien/* If there are [COMMONS] statements, put a wild one into the bss
198577298Sobrien   section.  */
198633965Sjdp
198733965Sjdpstatic void
198833965Sjdplang_reasonable_defaults ()
198933965Sjdp{
199033965Sjdp#if 0
199133965Sjdp  lang_output_section_statement_lookup (".text");
199233965Sjdp  lang_output_section_statement_lookup (".data");
199333965Sjdp
199477298Sobrien  default_common_section = lang_output_section_statement_lookup (".bss");
199533965Sjdp
199633965Sjdp  if (placed_commons == false)
199733965Sjdp    {
199833965Sjdp      lang_wild_statement_type *new =
199933965Sjdp      new_stat (lang_wild_statement,
200033965Sjdp		&default_common_section->children);
200133965Sjdp
200233965Sjdp      new->section_name = "COMMON";
200333965Sjdp      new->filename = (char *) NULL;
200433965Sjdp      lang_list_init (&new->children);
200533965Sjdp    }
200633965Sjdp#endif
200733965Sjdp}
200833965Sjdp
200977298Sobrien/* Add the supplied name to the symbol table as an undefined reference.
201077298Sobrien   Remove items from the chain as we open input bfds.  */
201133965Sjdptypedef struct ldlang_undef_chain_list
201233965Sjdp{
201333965Sjdp  struct ldlang_undef_chain_list *next;
201433965Sjdp  char *name;
201533965Sjdp}                       ldlang_undef_chain_list_type;
201633965Sjdp
201733965Sjdpstatic ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
201833965Sjdp
201933965Sjdpvoid
202033965Sjdpldlang_add_undef (name)
202177298Sobrien     const char *const name;
202233965Sjdp{
202333965Sjdp  ldlang_undef_chain_list_type *new =
202433965Sjdp    ((ldlang_undef_chain_list_type *)
202533965Sjdp     stat_alloc (sizeof (ldlang_undef_chain_list_type)));
202633965Sjdp
202733965Sjdp  new->next = ldlang_undef_chain_list_head;
202833965Sjdp  ldlang_undef_chain_list_head = new;
202933965Sjdp
203078828Sobrien  new->name = xstrdup (name);
203133965Sjdp}
203233965Sjdp
203333965Sjdp/* Run through the list of undefineds created above and place them
203433965Sjdp   into the linker hash table as undefined symbols belonging to the
203577298Sobrien   script file.  */
203677298Sobrien
203733965Sjdpstatic void
203833965Sjdplang_place_undefineds ()
203933965Sjdp{
204033965Sjdp  ldlang_undef_chain_list_type *ptr;
204133965Sjdp
204233965Sjdp  for (ptr = ldlang_undef_chain_list_head;
204333965Sjdp       ptr != (ldlang_undef_chain_list_type *) NULL;
204433965Sjdp       ptr = ptr->next)
204533965Sjdp    {
204633965Sjdp      struct bfd_link_hash_entry *h;
204733965Sjdp
204833965Sjdp      h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
204933965Sjdp      if (h == (struct bfd_link_hash_entry *) NULL)
205060484Sobrien	einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
205133965Sjdp      if (h->type == bfd_link_hash_new)
205233965Sjdp	{
205333965Sjdp	  h->type = bfd_link_hash_undefined;
205433965Sjdp	  h->u.undef.abfd = NULL;
205533965Sjdp	  bfd_link_add_undef (link_info.hash, h);
205633965Sjdp	}
205733965Sjdp    }
205833965Sjdp}
205933965Sjdp
206077298Sobrien/* Open input files and attatch to output sections.  */
206177298Sobrien
206233965Sjdpstatic void
206333965Sjdpmap_input_to_output_sections (s, target, output_section_statement)
206477298Sobrien     lang_statement_union_type *s;
206577298Sobrien     const char *target;
206677298Sobrien     lang_output_section_statement_type *output_section_statement;
206733965Sjdp{
206889857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
206933965Sjdp    {
207033965Sjdp      switch (s->header.type)
207133965Sjdp	{
207233965Sjdp	case lang_wild_statement_enum:
207389857Sobrien	  wild (&s->wild_statement, target, output_section_statement);
207433965Sjdp	  break;
207533965Sjdp	case lang_constructors_statement_enum:
207633965Sjdp	  map_input_to_output_sections (constructor_list.head,
207733965Sjdp					target,
207833965Sjdp					output_section_statement);
207933965Sjdp	  break;
208033965Sjdp	case lang_output_section_statement_enum:
208133965Sjdp	  map_input_to_output_sections (s->output_section_statement.children.head,
208233965Sjdp					target,
208333965Sjdp					&s->output_section_statement);
208433965Sjdp	  break;
208533965Sjdp	case lang_output_statement_enum:
208633965Sjdp	  break;
208733965Sjdp	case lang_target_statement_enum:
208833965Sjdp	  target = s->target_statement.target;
208933965Sjdp	  break;
209033965Sjdp	case lang_group_statement_enum:
209133965Sjdp	  map_input_to_output_sections (s->group_statement.children.head,
209233965Sjdp					target,
209333965Sjdp					output_section_statement);
209433965Sjdp	  break;
209533965Sjdp	case lang_fill_statement_enum:
209633965Sjdp	case lang_input_section_enum:
209733965Sjdp	case lang_object_symbols_statement_enum:
209833965Sjdp	case lang_data_statement_enum:
209933965Sjdp	case lang_reloc_statement_enum:
210033965Sjdp	case lang_padding_statement_enum:
210133965Sjdp	case lang_input_statement_enum:
210233965Sjdp	  if (output_section_statement != NULL
210333965Sjdp	      && output_section_statement->bfd_section == NULL)
210433965Sjdp	    init_os (output_section_statement);
210533965Sjdp	  break;
210633965Sjdp	case lang_assignment_statement_enum:
210733965Sjdp	  if (output_section_statement != NULL
210833965Sjdp	      && output_section_statement->bfd_section == NULL)
210933965Sjdp	    init_os (output_section_statement);
211033965Sjdp
211133965Sjdp	  /* Make sure that any sections mentioned in the assignment
211277298Sobrien	     are initialized.  */
211333965Sjdp	  exp_init_os (s->assignment_statement.exp);
211433965Sjdp	  break;
211533965Sjdp	case lang_afile_asection_pair_statement_enum:
211633965Sjdp	  FAIL ();
211733965Sjdp	  break;
211833965Sjdp	case lang_address_statement_enum:
211977298Sobrien	  /* Mark the specified section with the supplied address.  */
212033965Sjdp	  {
212133965Sjdp	    lang_output_section_statement_type *os =
212233965Sjdp	      lang_output_section_statement_lookup
212333965Sjdp		(s->address_statement.section_name);
212433965Sjdp
212533965Sjdp	    if (os->bfd_section == NULL)
212633965Sjdp	      init_os (os);
212733965Sjdp	    os->addr_tree = s->address_statement.address;
212833965Sjdp	  }
212933965Sjdp	  break;
213033965Sjdp	}
213133965Sjdp    }
213233965Sjdp}
213333965Sjdp
213433965Sjdpstatic void
213533965Sjdpprint_output_section_statement (output_section_statement)
213677298Sobrien     lang_output_section_statement_type *output_section_statement;
213733965Sjdp{
213833965Sjdp  asection *section = output_section_statement->bfd_section;
213933965Sjdp  int len;
214033965Sjdp
214133965Sjdp  if (output_section_statement != abs_output_section)
214233965Sjdp    {
214333965Sjdp      minfo ("\n%s", output_section_statement->name);
214433965Sjdp
214533965Sjdp      if (section != NULL)
214633965Sjdp	{
214733965Sjdp	  print_dot = section->vma;
214833965Sjdp
214933965Sjdp	  len = strlen (output_section_statement->name);
215033965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
215133965Sjdp	    {
215233965Sjdp	      print_nl ();
215333965Sjdp	      len = 0;
215433965Sjdp	    }
215533965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
215633965Sjdp	    {
215733965Sjdp	      print_space ();
215833965Sjdp	      ++len;
215933965Sjdp	    }
216033965Sjdp
216133965Sjdp	  minfo ("0x%V %W", section->vma, section->_raw_size);
216233965Sjdp
216333965Sjdp	  if (output_section_statement->load_base != NULL)
216433965Sjdp	    {
216533965Sjdp	      bfd_vma addr;
216633965Sjdp
216733965Sjdp	      addr = exp_get_abs_int (output_section_statement->load_base, 0,
216833965Sjdp				      "load base", lang_final_phase_enum);
216960484Sobrien	      minfo (_(" load address 0x%V"), addr);
217033965Sjdp	    }
217133965Sjdp	}
217233965Sjdp
217333965Sjdp      print_nl ();
217433965Sjdp    }
217533965Sjdp
217633965Sjdp  print_statement_list (output_section_statement->children.head,
217733965Sjdp			output_section_statement);
217833965Sjdp}
217933965Sjdp
218033965Sjdpstatic void
218133965Sjdpprint_assignment (assignment, output_section)
218277298Sobrien     lang_assignment_statement_type *assignment;
218377298Sobrien     lang_output_section_statement_type *output_section;
218433965Sjdp{
218533965Sjdp  int i;
218633965Sjdp  etree_value_type result;
218733965Sjdp
218833965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
218933965Sjdp    print_space ();
219033965Sjdp
219133965Sjdp  result = exp_fold_tree (assignment->exp->assign.src, output_section,
219233965Sjdp			  lang_final_phase_enum, print_dot, &print_dot);
219360484Sobrien  if (result.valid_p)
219433965Sjdp    minfo ("0x%V", result.value + result.section->bfd_section->vma);
219533965Sjdp  else
219633965Sjdp    {
219733965Sjdp      minfo ("*undef*   ");
219833965Sjdp#ifdef BFD64
219933965Sjdp      minfo ("        ");
220033965Sjdp#endif
220133965Sjdp    }
220233965Sjdp
220333965Sjdp  minfo ("                ");
220433965Sjdp
220533965Sjdp  exp_print_tree (assignment->exp);
220633965Sjdp
220733965Sjdp  print_nl ();
220833965Sjdp}
220933965Sjdp
221033965Sjdpstatic void
221133965Sjdpprint_input_statement (statm)
221277298Sobrien     lang_input_statement_type *statm;
221333965Sjdp{
221433965Sjdp  if (statm->filename != (char *) NULL)
221533965Sjdp    {
221633965Sjdp      fprintf (config.map_file, "LOAD %s\n", statm->filename);
221733965Sjdp    }
221833965Sjdp}
221933965Sjdp
222033965Sjdp/* Print all symbols defined in a particular section.  This is called
222133965Sjdp   via bfd_link_hash_traverse.  */
222233965Sjdp
222377298Sobrienstatic boolean
222433965Sjdpprint_one_symbol (hash_entry, ptr)
222533965Sjdp     struct bfd_link_hash_entry *hash_entry;
222633965Sjdp     PTR ptr;
222733965Sjdp{
222833965Sjdp  asection *sec = (asection *) ptr;
222933965Sjdp
223033965Sjdp  if ((hash_entry->type == bfd_link_hash_defined
223133965Sjdp       || hash_entry->type == bfd_link_hash_defweak)
223233965Sjdp      && sec == hash_entry->u.def.section)
223333965Sjdp    {
223433965Sjdp      int i;
223533965Sjdp
223633965Sjdp      for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
223733965Sjdp	print_space ();
223833965Sjdp      minfo ("0x%V   ",
223933965Sjdp	     (hash_entry->u.def.value
224033965Sjdp	      + hash_entry->u.def.section->output_offset
224133965Sjdp	      + hash_entry->u.def.section->output_section->vma));
224233965Sjdp
224333965Sjdp      minfo ("             %T\n", hash_entry->root.string);
224433965Sjdp    }
224533965Sjdp
224633965Sjdp  return true;
224733965Sjdp}
224833965Sjdp
224933965Sjdp/* Print information about an input section to the map file.  */
225033965Sjdp
225133965Sjdpstatic void
225233965Sjdpprint_input_section (in)
225377298Sobrien     lang_input_section_type *in;
225433965Sjdp{
225533965Sjdp  asection *i = in->section;
225633965Sjdp  bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
225777298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
225860484Sobrien						ldfile_output_machine);
225933965Sjdp  if (size != 0)
226033965Sjdp    {
226133965Sjdp      print_space ();
226233965Sjdp
226333965Sjdp      minfo ("%s", i->name);
226433965Sjdp
226533965Sjdp      if (i->output_section != NULL)
226633965Sjdp	{
226733965Sjdp	  int len;
226833965Sjdp
226933965Sjdp	  len = 1 + strlen (i->name);
227033965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
227133965Sjdp	    {
227233965Sjdp	      print_nl ();
227333965Sjdp	      len = 0;
227433965Sjdp	    }
227533965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
227633965Sjdp	    {
227733965Sjdp	      print_space ();
227833965Sjdp	      ++len;
227933965Sjdp	    }
228033965Sjdp
228133965Sjdp	  minfo ("0x%V %W %B\n",
228260484Sobrien		 i->output_section->vma + i->output_offset, size / opb,
228333965Sjdp		 i->owner);
228433965Sjdp
228533965Sjdp	  if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
228633965Sjdp	    {
228733965Sjdp	      len = SECTION_NAME_MAP_LENGTH + 3;
228833965Sjdp#ifdef BFD64
228933965Sjdp	      len += 16;
229033965Sjdp#else
229133965Sjdp	      len += 8;
229233965Sjdp#endif
229333965Sjdp	      while (len > 0)
229433965Sjdp		{
229533965Sjdp		  print_space ();
229633965Sjdp		  --len;
229733965Sjdp		}
229833965Sjdp
229960484Sobrien	      minfo (_("%W (size before relaxing)\n"), i->_raw_size);
230033965Sjdp	    }
230133965Sjdp
230233965Sjdp	  bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
230333965Sjdp
230460484Sobrien	  print_dot = i->output_section->vma + i->output_offset + size / opb;
230533965Sjdp	}
230633965Sjdp    }
230733965Sjdp}
230833965Sjdp
230933965Sjdpstatic void
231033965Sjdpprint_fill_statement (fill)
231177298Sobrien     lang_fill_statement_type *fill;
231233965Sjdp{
231333965Sjdp  fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
231433965Sjdp}
231533965Sjdp
231633965Sjdpstatic void
231733965Sjdpprint_data_statement (data)
231877298Sobrien     lang_data_statement_type *data;
231933965Sjdp{
232033965Sjdp  int i;
232133965Sjdp  bfd_vma addr;
232233965Sjdp  bfd_size_type size;
232333965Sjdp  const char *name;
232477298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
232560484Sobrien						ldfile_output_machine);
232633965Sjdp
232733965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
232833965Sjdp    print_space ();
232933965Sjdp
233033965Sjdp  addr = data->output_vma;
233133965Sjdp  if (data->output_section != NULL)
233233965Sjdp    addr += data->output_section->vma;
233333965Sjdp
233433965Sjdp  switch (data->type)
233533965Sjdp    {
233633965Sjdp    default:
233733965Sjdp      abort ();
233833965Sjdp    case BYTE:
233933965Sjdp      size = BYTE_SIZE;
234033965Sjdp      name = "BYTE";
234133965Sjdp      break;
234233965Sjdp    case SHORT:
234333965Sjdp      size = SHORT_SIZE;
234433965Sjdp      name = "SHORT";
234533965Sjdp      break;
234633965Sjdp    case LONG:
234733965Sjdp      size = LONG_SIZE;
234833965Sjdp      name = "LONG";
234933965Sjdp      break;
235033965Sjdp    case QUAD:
235133965Sjdp      size = QUAD_SIZE;
235233965Sjdp      name = "QUAD";
235333965Sjdp      break;
235438889Sjdp    case SQUAD:
235538889Sjdp      size = QUAD_SIZE;
235638889Sjdp      name = "SQUAD";
235738889Sjdp      break;
235833965Sjdp    }
235933965Sjdp
236033965Sjdp  minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
236133965Sjdp
236233965Sjdp  if (data->exp->type.node_class != etree_value)
236333965Sjdp    {
236433965Sjdp      print_space ();
236533965Sjdp      exp_print_tree (data->exp);
236633965Sjdp    }
236733965Sjdp
236833965Sjdp  print_nl ();
236933965Sjdp
237060484Sobrien  print_dot = addr + size / opb;
237160484Sobrien
237233965Sjdp}
237333965Sjdp
237433965Sjdp/* Print an address statement.  These are generated by options like
237533965Sjdp   -Ttext.  */
237633965Sjdp
237733965Sjdpstatic void
237833965Sjdpprint_address_statement (address)
237933965Sjdp     lang_address_statement_type *address;
238033965Sjdp{
238160484Sobrien  minfo (_("Address of section %s set to "), address->section_name);
238233965Sjdp  exp_print_tree (address->address);
238333965Sjdp  print_nl ();
238433965Sjdp}
238533965Sjdp
238633965Sjdp/* Print a reloc statement.  */
238733965Sjdp
238833965Sjdpstatic void
238933965Sjdpprint_reloc_statement (reloc)
239033965Sjdp     lang_reloc_statement_type *reloc;
239133965Sjdp{
239233965Sjdp  int i;
239333965Sjdp  bfd_vma addr;
239433965Sjdp  bfd_size_type size;
239577298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
239677298Sobrien						ldfile_output_machine);
239733965Sjdp
239833965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
239933965Sjdp    print_space ();
240033965Sjdp
240133965Sjdp  addr = reloc->output_vma;
240233965Sjdp  if (reloc->output_section != NULL)
240333965Sjdp    addr += reloc->output_section->vma;
240433965Sjdp
240533965Sjdp  size = bfd_get_reloc_size (reloc->howto);
240633965Sjdp
240733965Sjdp  minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
240833965Sjdp
240933965Sjdp  if (reloc->name != NULL)
241033965Sjdp    minfo ("%s+", reloc->name);
241133965Sjdp  else
241233965Sjdp    minfo ("%s+", reloc->section->name);
241333965Sjdp
241433965Sjdp  exp_print_tree (reloc->addend_exp);
241533965Sjdp
241633965Sjdp  print_nl ();
241733965Sjdp
241860484Sobrien  print_dot = addr + size / opb;
241977298Sobrien}
242033965Sjdp
242133965Sjdpstatic void
242233965Sjdpprint_padding_statement (s)
242333965Sjdp     lang_padding_statement_type *s;
242433965Sjdp{
242533965Sjdp  int len;
242633965Sjdp  bfd_vma addr;
242777298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
242877298Sobrien						ldfile_output_machine);
242933965Sjdp
243033965Sjdp  minfo (" *fill*");
243133965Sjdp
243233965Sjdp  len = sizeof " *fill*" - 1;
243333965Sjdp  while (len < SECTION_NAME_MAP_LENGTH)
243433965Sjdp    {
243533965Sjdp      print_space ();
243633965Sjdp      ++len;
243733965Sjdp    }
243833965Sjdp
243933965Sjdp  addr = s->output_offset;
244033965Sjdp  if (s->output_section != NULL)
244133965Sjdp    addr += s->output_section->vma;
244233965Sjdp  minfo ("0x%V %W", addr, s->size);
244333965Sjdp
244433965Sjdp  if (s->fill != 0)
244533965Sjdp    minfo (" %u", s->fill);
244633965Sjdp
244733965Sjdp  print_nl ();
244833965Sjdp
244960484Sobrien  print_dot = addr + s->size / opb;
245033965Sjdp}
245133965Sjdp
245233965Sjdpstatic void
245333965Sjdpprint_wild_statement (w, os)
245477298Sobrien     lang_wild_statement_type *w;
245577298Sobrien     lang_output_section_statement_type *os;
245633965Sjdp{
245789857Sobrien  struct wildcard_list *sec;
245889857Sobrien
245933965Sjdp  print_space ();
246033965Sjdp
246160484Sobrien  if (w->filenames_sorted)
246260484Sobrien    minfo ("SORT(");
246377298Sobrien  if (w->filename != NULL)
246433965Sjdp    minfo ("%s", w->filename);
246533965Sjdp  else
246633965Sjdp    minfo ("*");
246760484Sobrien  if (w->filenames_sorted)
246860484Sobrien    minfo (")");
246933965Sjdp
247060484Sobrien  minfo ("(");
247189857Sobrien  for (sec = w->section_list; sec; sec = sec->next)
247289857Sobrien    {
247389857Sobrien      if (sec->spec.sorted)
247489857Sobrien	minfo ("SORT(");
247589857Sobrien      if (sec->spec.exclude_name_list != NULL)
247689857Sobrien	{
247789857Sobrien	  name_list *tmp;
247889857Sobrien	  minfo ("EXCLUDE_FILE ( %s", sec->spec.exclude_name_list->name);
247989857Sobrien	  for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
248089857Sobrien	    minfo (", %s", tmp->name);
248189857Sobrien	  minfo (")");
248289857Sobrien	}
248389857Sobrien      if (sec->spec.name != NULL)
248489857Sobrien	minfo ("%s", sec->spec.name);
248589857Sobrien      else
248689857Sobrien	minfo ("*");
248789857Sobrien      if (sec->spec.sorted)
248889857Sobrien	minfo (")");
248989857Sobrien    }
249060484Sobrien  minfo (")");
249133965Sjdp
249233965Sjdp  print_nl ();
249333965Sjdp
249433965Sjdp  print_statement_list (w->children.head, os);
249533965Sjdp}
249633965Sjdp
249733965Sjdp/* Print a group statement.  */
249833965Sjdp
249933965Sjdpstatic void
250033965Sjdpprint_group (s, os)
250133965Sjdp     lang_group_statement_type *s;
250233965Sjdp     lang_output_section_statement_type *os;
250333965Sjdp{
250433965Sjdp  fprintf (config.map_file, "START GROUP\n");
250533965Sjdp  print_statement_list (s->children.head, os);
250633965Sjdp  fprintf (config.map_file, "END GROUP\n");
250733965Sjdp}
250833965Sjdp
250933965Sjdp/* Print the list of statements in S.
251033965Sjdp   This can be called for any statement type.  */
251133965Sjdp
251233965Sjdpstatic void
251333965Sjdpprint_statement_list (s, os)
251433965Sjdp     lang_statement_union_type *s;
251533965Sjdp     lang_output_section_statement_type *os;
251633965Sjdp{
251733965Sjdp  while (s != NULL)
251833965Sjdp    {
251933965Sjdp      print_statement (s, os);
252089857Sobrien      s = s->header.next;
252133965Sjdp    }
252233965Sjdp}
252333965Sjdp
252433965Sjdp/* Print the first statement in statement list S.
252533965Sjdp   This can be called for any statement type.  */
252633965Sjdp
252733965Sjdpstatic void
252833965Sjdpprint_statement (s, os)
252933965Sjdp     lang_statement_union_type *s;
253033965Sjdp     lang_output_section_statement_type *os;
253133965Sjdp{
253233965Sjdp  switch (s->header.type)
253333965Sjdp    {
253433965Sjdp    default:
253560484Sobrien      fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
253633965Sjdp      FAIL ();
253733965Sjdp      break;
253833965Sjdp    case lang_constructors_statement_enum:
253933965Sjdp      if (constructor_list.head != NULL)
254033965Sjdp	{
254160484Sobrien	  if (constructors_sorted)
254260484Sobrien	    minfo (" SORT (CONSTRUCTORS)\n");
254360484Sobrien	  else
254460484Sobrien	    minfo (" CONSTRUCTORS\n");
254533965Sjdp	  print_statement_list (constructor_list.head, os);
254633965Sjdp	}
254733965Sjdp      break;
254833965Sjdp    case lang_wild_statement_enum:
254933965Sjdp      print_wild_statement (&s->wild_statement, os);
255033965Sjdp      break;
255133965Sjdp    case lang_address_statement_enum:
255233965Sjdp      print_address_statement (&s->address_statement);
255333965Sjdp      break;
255433965Sjdp    case lang_object_symbols_statement_enum:
255533965Sjdp      minfo (" CREATE_OBJECT_SYMBOLS\n");
255633965Sjdp      break;
255733965Sjdp    case lang_fill_statement_enum:
255833965Sjdp      print_fill_statement (&s->fill_statement);
255933965Sjdp      break;
256033965Sjdp    case lang_data_statement_enum:
256133965Sjdp      print_data_statement (&s->data_statement);
256233965Sjdp      break;
256333965Sjdp    case lang_reloc_statement_enum:
256433965Sjdp      print_reloc_statement (&s->reloc_statement);
256533965Sjdp      break;
256633965Sjdp    case lang_input_section_enum:
256733965Sjdp      print_input_section (&s->input_section);
256833965Sjdp      break;
256933965Sjdp    case lang_padding_statement_enum:
257033965Sjdp      print_padding_statement (&s->padding_statement);
257133965Sjdp      break;
257233965Sjdp    case lang_output_section_statement_enum:
257333965Sjdp      print_output_section_statement (&s->output_section_statement);
257433965Sjdp      break;
257533965Sjdp    case lang_assignment_statement_enum:
257633965Sjdp      print_assignment (&s->assignment_statement, os);
257733965Sjdp      break;
257833965Sjdp    case lang_target_statement_enum:
257933965Sjdp      fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
258033965Sjdp      break;
258133965Sjdp    case lang_output_statement_enum:
258233965Sjdp      minfo ("OUTPUT(%s", s->output_statement.name);
258333965Sjdp      if (output_target != NULL)
258433965Sjdp	minfo (" %s", output_target);
258533965Sjdp      minfo (")\n");
258633965Sjdp      break;
258733965Sjdp    case lang_input_statement_enum:
258833965Sjdp      print_input_statement (&s->input_statement);
258933965Sjdp      break;
259033965Sjdp    case lang_group_statement_enum:
259133965Sjdp      print_group (&s->group_statement, os);
259233965Sjdp      break;
259333965Sjdp    case lang_afile_asection_pair_statement_enum:
259433965Sjdp      FAIL ();
259533965Sjdp      break;
259633965Sjdp    }
259733965Sjdp}
259833965Sjdp
259933965Sjdpstatic void
260033965Sjdpprint_statements ()
260133965Sjdp{
260233965Sjdp  print_statement_list (statement_list.head, abs_output_section);
260333965Sjdp}
260433965Sjdp
260533965Sjdp/* Print the first N statements in statement list S to STDERR.
260633965Sjdp   If N == 0, nothing is printed.
260733965Sjdp   If N < 0, the entire list is printed.
260833965Sjdp   Intended to be called from GDB.  */
260933965Sjdp
261033965Sjdpvoid
261133965Sjdpdprint_statement (s, n)
261277298Sobrien     lang_statement_union_type *s;
261333965Sjdp     int n;
261433965Sjdp{
261533965Sjdp  FILE *map_save = config.map_file;
261633965Sjdp
261733965Sjdp  config.map_file = stderr;
261833965Sjdp
261933965Sjdp  if (n < 0)
262033965Sjdp    print_statement_list (s, abs_output_section);
262133965Sjdp  else
262233965Sjdp    {
262333965Sjdp      while (s && --n >= 0)
262433965Sjdp	{
262533965Sjdp	  print_statement (s, abs_output_section);
262689857Sobrien	  s = s->header.next;
262733965Sjdp	}
262833965Sjdp    }
262933965Sjdp
263033965Sjdp  config.map_file = map_save;
263133965Sjdp}
263233965Sjdp
263389857Sobrienstatic void
263489857Sobrieninsert_pad (ptr, fill, alignment_needed, output_section, dot)
263589857Sobrien     lang_statement_union_type **ptr;
263633965Sjdp     fill_type fill;
263789857Sobrien     unsigned int alignment_needed;
263889857Sobrien     asection *output_section;
263933965Sjdp     bfd_vma dot;
264033965Sjdp{
264189857Sobrien  lang_statement_union_type *pad;
264233965Sjdp
264389857Sobrien  pad = ((lang_statement_union_type *)
264489857Sobrien	 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
264589857Sobrien  if (ptr != &statement_list.head
264689857Sobrien      && pad->header.type == lang_padding_statement_enum
264789857Sobrien      && pad->padding_statement.output_section == output_section)
264833965Sjdp    {
264989857Sobrien      /* Use the existing pad statement.  The above test on output
265089857Sobrien	 section is probably redundant, but it doesn't hurt to check.  */
265133965Sjdp    }
265289857Sobrien  else
265333965Sjdp    {
265489857Sobrien      /* Make a new padding statement, linked into existing chain.  */
265589857Sobrien      pad = ((lang_statement_union_type *)
265689857Sobrien	     stat_alloc (sizeof (lang_padding_statement_type)));
265789857Sobrien      pad->header.next = *ptr;
265889857Sobrien      *ptr = pad;
265989857Sobrien      pad->header.type = lang_padding_statement_enum;
266089857Sobrien      pad->padding_statement.output_section = output_section;
266189857Sobrien      pad->padding_statement.fill = fill;
266233965Sjdp    }
266389857Sobrien  pad->padding_statement.output_offset = dot - output_section->vma;
266489857Sobrien  pad->padding_statement.size = alignment_needed;
266589857Sobrien  output_section->_raw_size += alignment_needed;
266633965Sjdp}
266733965Sjdp
266877298Sobrien/* Work out how much this section will move the dot point.  */
266977298Sobrien
267033965Sjdpstatic bfd_vma
267189857Sobriensize_input_section (this_ptr, output_section_statement, fill, dot)
267277298Sobrien     lang_statement_union_type **this_ptr;
267377298Sobrien     lang_output_section_statement_type *output_section_statement;
267433965Sjdp     fill_type fill;
267533965Sjdp     bfd_vma dot;
267633965Sjdp{
267733965Sjdp  lang_input_section_type *is = &((*this_ptr)->input_section);
267833965Sjdp  asection *i = is->section;
267933965Sjdp
268033965Sjdp  if (is->ifile->just_syms_flag == false)
268133965Sjdp    {
268289857Sobrien      unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
268389857Sobrien						    ldfile_output_machine);
268489857Sobrien      unsigned int alignment_needed;
268589857Sobrien      asection *o;
268689857Sobrien
268789857Sobrien      /* Align this section first to the input sections requirement,
268889857Sobrien	 then to the output section's requirement.  If this alignment
268989857Sobrien	 is greater than any seen before, then record it too.  Perform
269089857Sobrien	 the alignment by inserting a magic 'padding' statement.  */
269189857Sobrien
269233965Sjdp      if (output_section_statement->subsection_alignment != -1)
269389857Sobrien	i->alignment_power = output_section_statement->subsection_alignment;
269433965Sjdp
269589857Sobrien      o = output_section_statement->bfd_section;
269689857Sobrien      if (o->alignment_power < i->alignment_power)
269789857Sobrien	o->alignment_power = i->alignment_power;
269833965Sjdp
269989857Sobrien      alignment_needed = align_power (dot, i->alignment_power) - dot;
270089857Sobrien
270189857Sobrien      if (alignment_needed != 0)
270289857Sobrien	{
270389857Sobrien	  insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
270489857Sobrien	  dot += alignment_needed;
270589857Sobrien	}
270689857Sobrien
270777298Sobrien      /* Remember where in the output section this input section goes.  */
270833965Sjdp
270989857Sobrien      i->output_offset = dot - o->vma;
271033965Sjdp
271177298Sobrien      /* Mark how big the output section must be to contain this now.  */
271233965Sjdp      if (i->_cooked_size != 0)
271360484Sobrien	dot += i->_cooked_size / opb;
271433965Sjdp      else
271560484Sobrien	dot += i->_raw_size / opb;
271689857Sobrien      o->_raw_size = (dot - o->vma) * opb;
271733965Sjdp    }
271833965Sjdp  else
271933965Sjdp    {
272033965Sjdp      i->output_offset = i->vma - output_section_statement->bfd_section->vma;
272133965Sjdp    }
272233965Sjdp
272333965Sjdp  return dot;
272433965Sjdp}
272533965Sjdp
272660484Sobrien#define IGNORE_SECTION(bfd, s) \
272778828Sobrien  (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD))	\
272878828Sobrien    != (SEC_ALLOC | SEC_LOAD))					\
272960484Sobrien   || bfd_section_size (bfd, s) == 0)
273060484Sobrien
273160484Sobrien/* Check to see if any allocated sections overlap with other allocated
273260484Sobrien   sections.  This can happen when the linker script specifically specifies
273360484Sobrien   the output section addresses of the two sections.  */
273477298Sobrien
273560484Sobrienstatic void
273660484Sobrienlang_check_section_addresses ()
273760484Sobrien{
273877298Sobrien  asection *s;
273960484Sobrien  unsigned opb = bfd_octets_per_byte (output_bfd);
274060484Sobrien
274160484Sobrien  /* Scan all sections in the output list.  */
274260484Sobrien  for (s = output_bfd->sections; s != NULL; s = s->next)
274360484Sobrien    {
274477298Sobrien      asection *os;
274577298Sobrien
274660484Sobrien      /* Ignore sections which are not loaded or which have no contents.  */
274760484Sobrien      if (IGNORE_SECTION (output_bfd, s))
274860484Sobrien	continue;
274977298Sobrien
275060484Sobrien      /* Once we reach section 's' stop our seach.  This prevents two
275160484Sobrien	 warning messages from being produced, one for 'section A overlaps
275260484Sobrien	 section B' and one for 'section B overlaps section A'.  */
275360484Sobrien      for (os = output_bfd->sections; os != s; os = os->next)
275460484Sobrien	{
275560484Sobrien	  bfd_vma s_start;
275660484Sobrien	  bfd_vma s_end;
275760484Sobrien	  bfd_vma os_start;
275860484Sobrien	  bfd_vma os_end;
275977298Sobrien
276060484Sobrien	  /* Only consider loadable sections with real contents.  */
276160484Sobrien	  if (IGNORE_SECTION (output_bfd, os))
276260484Sobrien	    continue;
276360484Sobrien
276460484Sobrien	  /* We must check the sections' LMA addresses not their
276560484Sobrien	     VMA addresses because overlay sections can have
276660484Sobrien	     overlapping VMAs but they must have distinct LMAs.  */
276760484Sobrien	  s_start  = bfd_section_lma (output_bfd, s);
276860484Sobrien	  os_start = bfd_section_lma (output_bfd, os);
276960484Sobrien	  s_end    = s_start  + bfd_section_size (output_bfd, s) / opb - 1;
277060484Sobrien	  os_end   = os_start + bfd_section_size (output_bfd, os) / opb - 1;
277177298Sobrien
277260484Sobrien	  /* Look for an overlap.  */
277360484Sobrien	  if ((s_end < os_start) || (s_start > os_end))
277460484Sobrien	    continue;
277577298Sobrien
277660484Sobrien	  einfo (
277760484Sobrien_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
277860484Sobrien		 s->name, s_start, s_end, os->name, os_start, os_end);
277977298Sobrien
278060484Sobrien	  /* Once we have found one overlap for this section,
278160484Sobrien	     stop looking for others.  */
278260484Sobrien	  break;
278360484Sobrien	}
278460484Sobrien    }
278560484Sobrien}
278660484Sobrien
278760484Sobrien/* Make sure the new address is within the region.  We explicitly permit the
278860484Sobrien   current address to be at the exact end of the region when the address is
278960484Sobrien   non-zero, in case the region is at the end of addressable memory and the
279077298Sobrien   calculation wraps around.  */
279160484Sobrien
279260484Sobrienstatic void
279360484Sobrienos_region_check (os, region, tree, base)
279477298Sobrien     lang_output_section_statement_type *os;
279577298Sobrien     struct memory_region_struct *region;
279677298Sobrien     etree_type *tree;
279777298Sobrien     bfd_vma base;
279860484Sobrien{
279960484Sobrien  if ((region->current < region->origin
280060484Sobrien       || (region->current - region->origin > region->length))
280160484Sobrien      && ((region->current != region->origin + region->length)
280260484Sobrien           || base == 0))
280360484Sobrien    {
280460484Sobrien      if (tree != (etree_type *) NULL)
280560484Sobrien        {
280660484Sobrien          einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
280760484Sobrien                 region->current,
280860484Sobrien                 os->bfd_section->owner,
280960484Sobrien                 os->bfd_section->name,
281060484Sobrien                 region->name);
281160484Sobrien        }
281260484Sobrien      else
281360484Sobrien        {
281460484Sobrien          einfo (_("%X%P: region %s is full (%B section %s)\n"),
281560484Sobrien                 region->name,
281660484Sobrien                 os->bfd_section->owner,
281760484Sobrien                 os->bfd_section->name);
281860484Sobrien        }
281960484Sobrien      /* Reset the region pointer.  */
282060484Sobrien      region->current = region->origin;
282160484Sobrien    }
282260484Sobrien}
282360484Sobrien
282433965Sjdp/* Set the sizes for all the output sections.  */
282533965Sjdp
282633965Sjdpbfd_vma
282733965Sjdplang_size_sections (s, output_section_statement, prev, fill, dot, relax)
282877298Sobrien     lang_statement_union_type *s;
282977298Sobrien     lang_output_section_statement_type *output_section_statement;
283077298Sobrien     lang_statement_union_type **prev;
283133965Sjdp     fill_type fill;
283233965Sjdp     bfd_vma dot;
283389857Sobrien     boolean *relax;
283433965Sjdp{
283577298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
283660484Sobrien						ldfile_output_machine);
283760484Sobrien
283860484Sobrien  /* Size up the sections from their constituent parts.  */
283989857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
284033965Sjdp    {
284160484Sobrien      switch (s->header.type)
284260484Sobrien	{
284360484Sobrien	case lang_output_section_statement_enum:
284460484Sobrien	  {
284560484Sobrien	    bfd_vma after;
284678828Sobrien	    lang_output_section_statement_type *os;
284733965Sjdp
284878828Sobrien	    os = &s->output_section_statement;
284960484Sobrien	    if (os->bfd_section == NULL)
285060484Sobrien	      /* This section was never actually created.  */
285160484Sobrien	      break;
285233965Sjdp
285360484Sobrien	    /* If this is a COFF shared library section, use the size and
285460484Sobrien	       address from the input section.  FIXME: This is COFF
285560484Sobrien	       specific; it would be cleaner if there were some other way
285660484Sobrien	       to do this, but nothing simple comes to mind.  */
285760484Sobrien	    if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
285860484Sobrien	      {
285977298Sobrien		asection *input;
286033965Sjdp
286160484Sobrien		if (os->children.head == NULL
286289857Sobrien		    || os->children.head->header.next != NULL
286360484Sobrien		    || os->children.head->header.type != lang_input_section_enum)
286460484Sobrien		  einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
286560484Sobrien			 os->name);
286633965Sjdp
286760484Sobrien		input = os->children.head->input_section.section;
286860484Sobrien		bfd_set_section_vma (os->bfd_section->owner,
286960484Sobrien				     os->bfd_section,
287060484Sobrien				     bfd_section_vma (input->owner, input));
287160484Sobrien		os->bfd_section->_raw_size = input->_raw_size;
287260484Sobrien		break;
287360484Sobrien	      }
287433965Sjdp
287560484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
287660484Sobrien	      {
287760484Sobrien		/* No matter what happens, an abs section starts at zero.  */
287860484Sobrien		ASSERT (os->bfd_section->vma == 0);
287960484Sobrien	      }
288060484Sobrien	    else
288160484Sobrien	      {
288260484Sobrien		if (os->addr_tree == (etree_type *) NULL)
288360484Sobrien		  {
288460484Sobrien		    /* No address specified for this section, get one
288560484Sobrien		       from the region specification.  */
288660484Sobrien		    if (os->region == (lang_memory_region_type *) NULL
288760484Sobrien			|| (((bfd_get_section_flags (output_bfd, os->bfd_section)
288860484Sobrien			      & (SEC_ALLOC | SEC_LOAD)) != 0)
288960484Sobrien			    && os->region->name[0] == '*'
289060484Sobrien			    && strcmp (os->region->name, "*default*") == 0))
289160484Sobrien		      {
289260484Sobrien			os->region = lang_memory_default (os->bfd_section);
289360484Sobrien		      }
289433965Sjdp
289560484Sobrien		    /* If a loadable section is using the default memory
289660484Sobrien		       region, and some non default memory regions were
289760484Sobrien		       defined, issue a warning.  */
289860484Sobrien		    if ((bfd_get_section_flags (output_bfd, os->bfd_section)
289960484Sobrien			 & (SEC_ALLOC | SEC_LOAD)) != 0
290060484Sobrien			&& ! link_info.relocateable
290160484Sobrien			&& strcmp (os->region->name, "*default*") == 0
290260484Sobrien			&& lang_memory_region_list != NULL
290378828Sobrien			&& (strcmp (lang_memory_region_list->name,
290478828Sobrien				    "*default*") != 0
290560484Sobrien			    || lang_memory_region_list->next != NULL))
290660484Sobrien		      einfo (_("%P: warning: no memory region specified for section `%s'\n"),
290778828Sobrien			     bfd_get_section_name (output_bfd,
290878828Sobrien						   os->bfd_section));
290938889Sjdp
291060484Sobrien		    dot = os->region->current;
291177298Sobrien
291260484Sobrien		    if (os->section_alignment == -1)
291360484Sobrien		      {
291460484Sobrien			bfd_vma olddot;
291538889Sjdp
291660484Sobrien			olddot = dot;
291778828Sobrien			dot = align_power (dot,
291878828Sobrien					   os->bfd_section->alignment_power);
291933965Sjdp
292060484Sobrien			if (dot != olddot && config.warn_section_align)
292160484Sobrien			  einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
292260484Sobrien				 os->name, (unsigned int) (dot - olddot));
292360484Sobrien		      }
292460484Sobrien		  }
292560484Sobrien		else
292660484Sobrien		  {
292760484Sobrien		    etree_value_type r;
292833965Sjdp
292960484Sobrien		    r = exp_fold_tree (os->addr_tree,
293060484Sobrien				       abs_output_section,
293160484Sobrien				       lang_allocating_phase_enum,
293260484Sobrien				       dot, &dot);
293360484Sobrien		    if (r.valid_p == false)
293460484Sobrien		      {
293560484Sobrien			einfo (_("%F%S: non constant address expression for section %s\n"),
293660484Sobrien			       os->name);
293760484Sobrien		      }
293860484Sobrien		    dot = r.value + r.section->bfd_section->vma;
293960484Sobrien		  }
294077298Sobrien
294160484Sobrien		/* The section starts here.
294260484Sobrien		   First, align to what the section needs.  */
294333965Sjdp
294460484Sobrien		if (os->section_alignment != -1)
294560484Sobrien		  dot = align_power (dot, os->section_alignment);
294633965Sjdp
294760484Sobrien		bfd_set_section_vma (0, os->bfd_section, dot);
294877298Sobrien
294960484Sobrien		os->bfd_section->output_offset = 0;
295060484Sobrien	      }
295133965Sjdp
295289857Sobrien	    lang_size_sections (os->children.head, os, &os->children.head,
295389857Sobrien				os->fill, dot, relax);
295477298Sobrien
295577298Sobrien	    /* Put the section within the requested block size, or
295677298Sobrien	       align at the block boundary.  */
295760484Sobrien	    after = ALIGN_N (os->bfd_section->vma
295860484Sobrien			     + os->bfd_section->_raw_size / opb,
295960484Sobrien			     /* The coercion here is important, see ld.h.  */
296060484Sobrien			     (bfd_vma) os->block_value);
296133965Sjdp
296260484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
296360484Sobrien	      ASSERT (after == os->bfd_section->vma);
296460484Sobrien	    else
296577298Sobrien	      os->bfd_section->_raw_size =
296677298Sobrien		(after - os->bfd_section->vma) * opb;
296760484Sobrien	    dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
296860484Sobrien	    os->processed = true;
296933965Sjdp
297060484Sobrien	    /* Update dot in the region ?
297160484Sobrien	       We only do this if the section is going to be allocated,
297260484Sobrien	       since unallocated sections do not contribute to the region's
297360484Sobrien	       overall size in memory.
297477298Sobrien
297560484Sobrien	       If the SEC_NEVER_LOAD bit is not set, it will affect the
297660484Sobrien	       addresses of sections after it. We have to update
297760484Sobrien	       dot.  */
297860484Sobrien	    if (os->region != (lang_memory_region_type *) NULL
297960484Sobrien		&& ((bfd_get_section_flags (output_bfd, os->bfd_section)
298060484Sobrien		     & SEC_NEVER_LOAD) == 0
298160484Sobrien		    || (bfd_get_section_flags (output_bfd, os->bfd_section)
298260484Sobrien			& (SEC_ALLOC | SEC_LOAD))))
298360484Sobrien	      {
298460484Sobrien		os->region->current = dot;
298577298Sobrien
298660484Sobrien		/* Make sure the new address is within the region.  */
298777298Sobrien		os_region_check (os, os->region, os->addr_tree,
298877298Sobrien				 os->bfd_section->vma);
298933965Sjdp
299077298Sobrien		/* If there's no load address specified, use the run
299177298Sobrien		   region as the load region.  */
299277298Sobrien		if (os->lma_region == NULL && os->load_base == NULL)
299377298Sobrien		  os->lma_region = os->region;
299433965Sjdp
299577298Sobrien		if (os->lma_region != NULL)
299677298Sobrien		  {
299777298Sobrien		    if (os->load_base != NULL)
299877298Sobrien		      {
299977298Sobrien			einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
300077298Sobrien		      }
300177298Sobrien		    else
300277298Sobrien		      {
300377298Sobrien			/* Don't allocate twice.  */
300477298Sobrien			if (os->lma_region != os->region)
300577298Sobrien			  {
300677298Sobrien			    /* Set load_base, which will be handled later.  */
300777298Sobrien			    os->load_base =
300877298Sobrien			      exp_intop (os->lma_region->current);
300977298Sobrien			    os->lma_region->current +=
301077298Sobrien			      os->bfd_section->_raw_size / opb;
301177298Sobrien			    os_region_check (os, os->lma_region, NULL,
301277298Sobrien					     os->bfd_section->lma);
301377298Sobrien			  }
301477298Sobrien		      }
301577298Sobrien		  }
301660484Sobrien	      }
301760484Sobrien	  }
301860484Sobrien	  break;
301933965Sjdp
302060484Sobrien	case lang_constructors_statement_enum:
302160484Sobrien	  dot = lang_size_sections (constructor_list.head,
302260484Sobrien				    output_section_statement,
302360484Sobrien				    &s->wild_statement.children.head,
302489857Sobrien				    fill, dot, relax);
302560484Sobrien	  break;
302633965Sjdp
302760484Sobrien	case lang_data_statement_enum:
302860484Sobrien	  {
302960484Sobrien	    unsigned int size = 0;
303033965Sjdp
303177298Sobrien	    s->data_statement.output_vma =
303277298Sobrien	      dot - output_section_statement->bfd_section->vma;
303360484Sobrien	    s->data_statement.output_section =
303460484Sobrien	      output_section_statement->bfd_section;
303533965Sjdp
303660484Sobrien	    switch (s->data_statement.type)
303760484Sobrien	      {
303877298Sobrien	      default:
303977298Sobrien		abort ();
304060484Sobrien	      case QUAD:
304160484Sobrien	      case SQUAD:
304260484Sobrien		size = QUAD_SIZE;
304360484Sobrien		break;
304460484Sobrien	      case LONG:
304560484Sobrien		size = LONG_SIZE;
304660484Sobrien		break;
304760484Sobrien	      case SHORT:
304860484Sobrien		size = SHORT_SIZE;
304960484Sobrien		break;
305060484Sobrien	      case BYTE:
305160484Sobrien		size = BYTE_SIZE;
305260484Sobrien		break;
305360484Sobrien	      }
305477298Sobrien	    if (size < opb)
305577298Sobrien	      size = opb;
305660484Sobrien	    dot += size / opb;
305760484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
305860484Sobrien	    /* The output section gets contents, and then we inspect for
305960484Sobrien	       any flags set in the input script which override any ALLOC.  */
306060484Sobrien	    output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
306177298Sobrien	    if (!(output_section_statement->flags & SEC_NEVER_LOAD))
306277298Sobrien	      {
306377298Sobrien		output_section_statement->bfd_section->flags |=
306477298Sobrien		  SEC_ALLOC | SEC_LOAD;
306577298Sobrien	      }
306660484Sobrien	  }
306760484Sobrien	  break;
306833965Sjdp
306960484Sobrien	case lang_reloc_statement_enum:
307060484Sobrien	  {
307160484Sobrien	    int size;
307233965Sjdp
307360484Sobrien	    s->reloc_statement.output_vma =
307460484Sobrien	      dot - output_section_statement->bfd_section->vma;
307560484Sobrien	    s->reloc_statement.output_section =
307660484Sobrien	      output_section_statement->bfd_section;
307760484Sobrien	    size = bfd_get_reloc_size (s->reloc_statement.howto);
307860484Sobrien	    dot += size / opb;
307960484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
308060484Sobrien	  }
308160484Sobrien	  break;
308277298Sobrien
308360484Sobrien	case lang_wild_statement_enum:
308433965Sjdp
308560484Sobrien	  dot = lang_size_sections (s->wild_statement.children.head,
308660484Sobrien				    output_section_statement,
308760484Sobrien				    &s->wild_statement.children.head,
308860484Sobrien				    fill, dot, relax);
308933965Sjdp
309060484Sobrien	  break;
309133965Sjdp
309260484Sobrien	case lang_object_symbols_statement_enum:
309360484Sobrien	  link_info.create_object_symbols_section =
309460484Sobrien	    output_section_statement->bfd_section;
309560484Sobrien	  break;
309660484Sobrien	case lang_output_statement_enum:
309760484Sobrien	case lang_target_statement_enum:
309860484Sobrien	  break;
309960484Sobrien	case lang_input_section_enum:
310033965Sjdp	  {
310160484Sobrien	    asection *i;
310233965Sjdp
310360484Sobrien	    i = (*prev)->input_section.section;
310460484Sobrien	    if (! relax)
310560484Sobrien	      {
310660484Sobrien		if (i->_cooked_size == 0)
310760484Sobrien		  i->_cooked_size = i->_raw_size;
310860484Sobrien	      }
310960484Sobrien	    else
311060484Sobrien	      {
311160484Sobrien		boolean again;
311260484Sobrien
311360484Sobrien		if (! bfd_relax_section (i->owner, i, &link_info, &again))
311460484Sobrien		  einfo (_("%P%F: can't relax section: %E\n"));
311560484Sobrien		if (again)
311689857Sobrien		  *relax = true;
311760484Sobrien	      }
311889857Sobrien	    dot = size_input_section (prev, output_section_statement,
311989857Sobrien				      output_section_statement->fill, dot);
312033965Sjdp	  }
312160484Sobrien	  break;
312260484Sobrien	case lang_input_statement_enum:
312360484Sobrien	  break;
312460484Sobrien	case lang_fill_statement_enum:
312577298Sobrien	  s->fill_statement.output_section =
312677298Sobrien	    output_section_statement->bfd_section;
312733965Sjdp
312860484Sobrien	  fill = s->fill_statement.fill;
312960484Sobrien	  break;
313060484Sobrien	case lang_assignment_statement_enum:
313160484Sobrien	  {
313260484Sobrien	    bfd_vma newdot = dot;
313333965Sjdp
313460484Sobrien	    exp_fold_tree (s->assignment_statement.exp,
313560484Sobrien			   output_section_statement,
313660484Sobrien			   lang_allocating_phase_enum,
313760484Sobrien			   dot,
313860484Sobrien			   &newdot);
313933965Sjdp
314060484Sobrien	    if (newdot != dot)
314160484Sobrien	      {
314260484Sobrien		if (output_section_statement == abs_output_section)
314360484Sobrien		  {
314460484Sobrien		    /* If we don't have an output section, then just adjust
314560484Sobrien		       the default memory address.  */
314660484Sobrien		    lang_memory_region_lookup ("*default*")->current = newdot;
314760484Sobrien		  }
314889857Sobrien		else
314960484Sobrien		  {
315089857Sobrien		    /* Insert a pad after this statement.  We can't
315189857Sobrien		       put the pad before when relaxing, in case the
315289857Sobrien		       assignment references dot.  */
315389857Sobrien		    insert_pad (&s->header.next, fill, (newdot - dot) * opb,
315489857Sobrien				output_section_statement->bfd_section, dot);
315533965Sjdp
315689857Sobrien		    /* Don't neuter the pad below when relaxing.  */
315789857Sobrien		    s = s->header.next;
315860484Sobrien		  }
315933965Sjdp
316060484Sobrien		dot = newdot;
316160484Sobrien	      }
316260484Sobrien	  }
316360484Sobrien	  break;
316433965Sjdp
316560484Sobrien	case lang_padding_statement_enum:
316689857Sobrien	  /* If this is the first time lang_size_sections is called,
316789857Sobrien	     we won't have any padding statements.  If this is the
316889857Sobrien	     second or later passes when relaxing, we should allow
316989857Sobrien	     padding to shrink.  If padding is needed on this pass, it
317089857Sobrien	     will be added back in.  */
317189857Sobrien	  s->padding_statement.size = 0;
317289857Sobrien
317389857Sobrien	  /* Make sure output_offset is valid.  If relaxation shrinks
317489857Sobrien	     the section and this pad isn't needed, it's possible to
317589857Sobrien	     have output_offset larger than the final size of the
317689857Sobrien	     section.  bfd_set_section_contents will complain even for
317789857Sobrien	     a pad size of zero.  */
317889857Sobrien	  s->padding_statement.output_offset
317989857Sobrien	    = dot - output_section_statement->bfd_section->vma;
318060484Sobrien	  break;
318133965Sjdp
318260484Sobrien	case lang_group_statement_enum:
318360484Sobrien	  dot = lang_size_sections (s->group_statement.children.head,
318460484Sobrien				    output_section_statement,
318560484Sobrien				    &s->group_statement.children.head,
318660484Sobrien				    fill, dot, relax);
318760484Sobrien	  break;
318833965Sjdp
318960484Sobrien	default:
319060484Sobrien	  FAIL ();
319160484Sobrien	  break;
319233965Sjdp
319389857Sobrien	  /* We can only get here when relaxing is turned on.  */
319460484Sobrien	case lang_address_statement_enum:
319560484Sobrien	  break;
319660484Sobrien	}
319760484Sobrien      prev = &s->header.next;
319833965Sjdp    }
319933965Sjdp  return dot;
320033965Sjdp}
320133965Sjdp
320233965Sjdpbfd_vma
320333965Sjdplang_do_assignments (s, output_section_statement, fill, dot)
320477298Sobrien     lang_statement_union_type *s;
320577298Sobrien     lang_output_section_statement_type *output_section_statement;
320633965Sjdp     fill_type fill;
320733965Sjdp     bfd_vma dot;
320833965Sjdp{
320977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
321077298Sobrien						ldfile_output_machine);
321160484Sobrien
321289857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
321333965Sjdp    {
321433965Sjdp      switch (s->header.type)
321533965Sjdp	{
321633965Sjdp	case lang_constructors_statement_enum:
321733965Sjdp	  dot = lang_do_assignments (constructor_list.head,
321833965Sjdp				     output_section_statement,
321933965Sjdp				     fill,
322033965Sjdp				     dot);
322133965Sjdp	  break;
322233965Sjdp
322333965Sjdp	case lang_output_section_statement_enum:
322433965Sjdp	  {
322578828Sobrien	    lang_output_section_statement_type *os;
322633965Sjdp
322778828Sobrien	    os = &(s->output_section_statement);
322833965Sjdp	    if (os->bfd_section != NULL)
322933965Sjdp	      {
323033965Sjdp		dot = os->bfd_section->vma;
323133965Sjdp		(void) lang_do_assignments (os->children.head, os,
323233965Sjdp					    os->fill, dot);
323360484Sobrien		dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
323460484Sobrien
323533965Sjdp	      }
323677298Sobrien	    if (os->load_base)
323733965Sjdp	      {
323833965Sjdp		/* If nothing has been placed into the output section then
323977298Sobrien		   it won't have a bfd_section.  */
324077298Sobrien		if (os->bfd_section)
324133965Sjdp		  {
324277298Sobrien		    os->bfd_section->lma
324377298Sobrien		      = exp_get_abs_int (os->load_base, 0, "load base",
324477298Sobrien					 lang_final_phase_enum);
324533965Sjdp		  }
324633965Sjdp	      }
324733965Sjdp	  }
324833965Sjdp	  break;
324933965Sjdp	case lang_wild_statement_enum:
325033965Sjdp
325133965Sjdp	  dot = lang_do_assignments (s->wild_statement.children.head,
325233965Sjdp				     output_section_statement,
325333965Sjdp				     fill, dot);
325433965Sjdp
325533965Sjdp	  break;
325633965Sjdp
325733965Sjdp	case lang_object_symbols_statement_enum:
325833965Sjdp	case lang_output_statement_enum:
325933965Sjdp	case lang_target_statement_enum:
326033965Sjdp#if 0
326133965Sjdp	case lang_common_statement_enum:
326233965Sjdp#endif
326333965Sjdp	  break;
326433965Sjdp	case lang_data_statement_enum:
326533965Sjdp	  {
326633965Sjdp	    etree_value_type value;
326733965Sjdp
326833965Sjdp	    value = exp_fold_tree (s->data_statement.exp,
326933965Sjdp				   abs_output_section,
327033965Sjdp				   lang_final_phase_enum, dot, &dot);
327133965Sjdp	    s->data_statement.value = value.value;
327260484Sobrien	    if (value.valid_p == false)
327360484Sobrien	      einfo (_("%F%P: invalid data statement\n"));
327433965Sjdp	  }
327560484Sobrien          {
327660484Sobrien            unsigned int size;
327777298Sobrien	    switch (s->data_statement.type)
327877298Sobrien	      {
327977298Sobrien	      default:
328077298Sobrien		abort ();
328177298Sobrien	      case QUAD:
328277298Sobrien	      case SQUAD:
328377298Sobrien		size = QUAD_SIZE;
328477298Sobrien		break;
328577298Sobrien	      case LONG:
328677298Sobrien		size = LONG_SIZE;
328777298Sobrien		break;
328877298Sobrien	      case SHORT:
328977298Sobrien		size = SHORT_SIZE;
329077298Sobrien		break;
329177298Sobrien	      case BYTE:
329277298Sobrien		size = BYTE_SIZE;
329377298Sobrien		break;
329477298Sobrien	      }
329577298Sobrien	    if (size < opb)
329677298Sobrien	      size = opb;
329777298Sobrien	    dot += size / opb;
329877298Sobrien	  }
329933965Sjdp	  break;
330033965Sjdp
330133965Sjdp	case lang_reloc_statement_enum:
330233965Sjdp	  {
330333965Sjdp	    etree_value_type value;
330433965Sjdp
330533965Sjdp	    value = exp_fold_tree (s->reloc_statement.addend_exp,
330633965Sjdp				   abs_output_section,
330733965Sjdp				   lang_final_phase_enum, dot, &dot);
330833965Sjdp	    s->reloc_statement.addend_value = value.value;
330960484Sobrien	    if (value.valid_p == false)
331060484Sobrien	      einfo (_("%F%P: invalid reloc statement\n"));
331133965Sjdp	  }
331260484Sobrien	  dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
331333965Sjdp	  break;
331433965Sjdp
331533965Sjdp	case lang_input_section_enum:
331633965Sjdp	  {
331733965Sjdp	    asection *in = s->input_section.section;
331833965Sjdp
331933965Sjdp	    if (in->_cooked_size != 0)
332060484Sobrien	      dot += in->_cooked_size / opb;
332133965Sjdp	    else
332260484Sobrien	      dot += in->_raw_size / opb;
332333965Sjdp	  }
332433965Sjdp	  break;
332533965Sjdp
332633965Sjdp	case lang_input_statement_enum:
332733965Sjdp	  break;
332833965Sjdp	case lang_fill_statement_enum:
332933965Sjdp	  fill = s->fill_statement.fill;
333033965Sjdp	  break;
333133965Sjdp	case lang_assignment_statement_enum:
333233965Sjdp	  {
333333965Sjdp	    exp_fold_tree (s->assignment_statement.exp,
333433965Sjdp			   output_section_statement,
333533965Sjdp			   lang_final_phase_enum,
333633965Sjdp			   dot,
333733965Sjdp			   &dot);
333833965Sjdp	  }
333933965Sjdp
334033965Sjdp	  break;
334133965Sjdp	case lang_padding_statement_enum:
334260484Sobrien	  dot += s->padding_statement.size / opb;
334333965Sjdp	  break;
334433965Sjdp
334533965Sjdp	case lang_group_statement_enum:
334633965Sjdp	  dot = lang_do_assignments (s->group_statement.children.head,
334733965Sjdp				     output_section_statement,
334833965Sjdp				     fill, dot);
334933965Sjdp
335033965Sjdp	  break;
335133965Sjdp
335233965Sjdp	default:
335333965Sjdp	  FAIL ();
335433965Sjdp	  break;
335533965Sjdp	case lang_address_statement_enum:
335633965Sjdp	  break;
335733965Sjdp	}
335833965Sjdp
335933965Sjdp    }
336033965Sjdp  return dot;
336133965Sjdp}
336233965Sjdp
336333965Sjdp/* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
336433965Sjdp   operator .startof. (section_name), it produces an undefined symbol
336533965Sjdp   .startof.section_name.  Similarly, when it sees
336633965Sjdp   .sizeof. (section_name), it produces an undefined symbol
336733965Sjdp   .sizeof.section_name.  For all the output sections, we look for
336833965Sjdp   such symbols, and set them to the correct value.  */
336933965Sjdp
337033965Sjdpstatic void
337133965Sjdplang_set_startof ()
337233965Sjdp{
337333965Sjdp  asection *s;
337433965Sjdp
337533965Sjdp  if (link_info.relocateable)
337633965Sjdp    return;
337733965Sjdp
337833965Sjdp  for (s = output_bfd->sections; s != NULL; s = s->next)
337933965Sjdp    {
338033965Sjdp      const char *secname;
338133965Sjdp      char *buf;
338233965Sjdp      struct bfd_link_hash_entry *h;
338333965Sjdp
338433965Sjdp      secname = bfd_get_section_name (output_bfd, s);
338533965Sjdp      buf = xmalloc (10 + strlen (secname));
338633965Sjdp
338733965Sjdp      sprintf (buf, ".startof.%s", secname);
338833965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
338933965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
339033965Sjdp	{
339133965Sjdp	  h->type = bfd_link_hash_defined;
339233965Sjdp	  h->u.def.value = bfd_get_section_vma (output_bfd, s);
339333965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
339433965Sjdp	}
339533965Sjdp
339633965Sjdp      sprintf (buf, ".sizeof.%s", secname);
339733965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
339833965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
339933965Sjdp	{
340078828Sobrien          unsigned opb;
340178828Sobrien
340278828Sobrien          opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
340378828Sobrien					       ldfile_output_machine);
340433965Sjdp	  h->type = bfd_link_hash_defined;
340533965Sjdp	  if (s->_cooked_size != 0)
340660484Sobrien	    h->u.def.value = s->_cooked_size / opb;
340733965Sjdp	  else
340860484Sobrien	    h->u.def.value = s->_raw_size / opb;
340933965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
341033965Sjdp	}
341133965Sjdp
341233965Sjdp      free (buf);
341333965Sjdp    }
341433965Sjdp}
341533965Sjdp
341633965Sjdpstatic void
341733965Sjdplang_finish ()
341833965Sjdp{
341933965Sjdp  struct bfd_link_hash_entry *h;
342033965Sjdp  boolean warn;
342133965Sjdp
342233965Sjdp  if (link_info.relocateable || link_info.shared)
342333965Sjdp    warn = false;
342433965Sjdp  else
342533965Sjdp    warn = true;
342633965Sjdp
342733965Sjdp  if (entry_symbol == (char *) NULL)
342833965Sjdp    {
342933965Sjdp      /* No entry has been specified.  Look for start, but don't warn
343033965Sjdp	 if we don't find it.  */
343133965Sjdp      entry_symbol = "start";
343233965Sjdp      warn = false;
343333965Sjdp    }
343433965Sjdp
343533965Sjdp  h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
343633965Sjdp  if (h != (struct bfd_link_hash_entry *) NULL
343733965Sjdp      && (h->type == bfd_link_hash_defined
343833965Sjdp	  || h->type == bfd_link_hash_defweak)
343933965Sjdp      && h->u.def.section->output_section != NULL)
344033965Sjdp    {
344133965Sjdp      bfd_vma val;
344233965Sjdp
344333965Sjdp      val = (h->u.def.value
344433965Sjdp	     + bfd_get_section_vma (output_bfd,
344533965Sjdp				    h->u.def.section->output_section)
344633965Sjdp	     + h->u.def.section->output_offset);
344733965Sjdp      if (! bfd_set_start_address (output_bfd, val))
344860484Sobrien	einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
344933965Sjdp    }
345033965Sjdp  else
345133965Sjdp    {
345260484Sobrien      bfd_vma val;
345377298Sobrien      const char *send;
345433965Sjdp
345560484Sobrien      /* We couldn't find the entry symbol.  Try parsing it as a
345660484Sobrien         number.  */
345760484Sobrien      val = bfd_scan_vma (entry_symbol, &send, 0);
345860484Sobrien      if (*send == '\0')
345933965Sjdp	{
346060484Sobrien	  if (! bfd_set_start_address (output_bfd, val))
346160484Sobrien	    einfo (_("%P%F: can't set start address\n"));
346233965Sjdp	}
346333965Sjdp      else
346433965Sjdp	{
346560484Sobrien	  asection *ts;
346660484Sobrien
346760484Sobrien	  /* Can't find the entry symbol, and it's not a number.  Use
346860484Sobrien	     the first address in the text section.  */
346991041Sobrien	  ts = bfd_get_section_by_name (output_bfd, entry_section);
347060484Sobrien	  if (ts != (asection *) NULL)
347160484Sobrien	    {
347260484Sobrien	      if (warn)
347360484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
347460484Sobrien		       entry_symbol, bfd_get_section_vma (output_bfd, ts));
347560484Sobrien	      if (! bfd_set_start_address (output_bfd,
347660484Sobrien					   bfd_get_section_vma (output_bfd,
347760484Sobrien								ts)))
347860484Sobrien		einfo (_("%P%F: can't set start address\n"));
347960484Sobrien	    }
348060484Sobrien	  else
348160484Sobrien	    {
348260484Sobrien	      if (warn)
348360484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
348460484Sobrien		       entry_symbol);
348560484Sobrien	    }
348633965Sjdp	}
348733965Sjdp    }
348833965Sjdp}
348933965Sjdp
349038889Sjdp/* This is a small function used when we want to ignore errors from
349138889Sjdp   BFD.  */
349238889Sjdp
349338889Sjdpstatic void
349438889Sjdp#ifdef ANSI_PROTOTYPES
349560484Sobrienignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
349638889Sjdp#else
349738889Sjdpignore_bfd_errors (s)
349860484Sobrien     const char *s ATTRIBUTE_UNUSED;
349938889Sjdp#endif
350038889Sjdp{
350138889Sjdp  /* Don't do anything.  */
350238889Sjdp}
350338889Sjdp
350433965Sjdp/* Check that the architecture of all the input files is compatible
350533965Sjdp   with the output file.  Also call the backend to let it do any
350633965Sjdp   other checking that is needed.  */
350733965Sjdp
350833965Sjdpstatic void
350933965Sjdplang_check ()
351033965Sjdp{
351133965Sjdp  lang_statement_union_type *file;
351233965Sjdp  bfd *input_bfd;
351377298Sobrien  const bfd_arch_info_type *compatible;
351433965Sjdp
351533965Sjdp  for (file = file_chain.head;
351633965Sjdp       file != (lang_statement_union_type *) NULL;
351733965Sjdp       file = file->input_statement.next)
351833965Sjdp    {
351933965Sjdp      input_bfd = file->input_statement.the_bfd;
352033965Sjdp      compatible = bfd_arch_get_compatible (input_bfd,
352133965Sjdp					    output_bfd);
352233965Sjdp      if (compatible == NULL)
352338889Sjdp	{
352438889Sjdp	  if (command_line.warn_mismatch)
352560484Sobrien	    einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
352638889Sjdp		   bfd_printable_name (input_bfd), input_bfd,
352738889Sjdp		   bfd_printable_name (output_bfd));
352838889Sjdp	}
352989857Sobrien      else if (link_info.relocateable
353089857Sobrien	       /* In general it is not possible to perform a relocatable
353189857Sobrien		  link between differing object formats when the input
353289857Sobrien		  file has relocations, because the relocations in the
353389857Sobrien		  input format may not have equivalent representations in
353489857Sobrien		  the output format (and besides BFD does not translate
353589857Sobrien		  relocs for other link purposes than a final link).  */
353689857Sobrien	       && bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd)
353789857Sobrien	       && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
353889857Sobrien	einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
353989857Sobrien	       bfd_get_target (input_bfd), input_bfd,
354089857Sobrien	       bfd_get_target (output_bfd), output_bfd);
354177298Sobrien      else if (bfd_count_sections (input_bfd))
354238889Sjdp	{
354377298Sobrien	  /* If the input bfd has no contents, it shouldn't set the
354477298Sobrien	     private data of the output bfd. */
354577298Sobrien
354638889Sjdp	  bfd_error_handler_type pfn = NULL;
354733965Sjdp
354838889Sjdp	  /* If we aren't supposed to warn about mismatched input
354938889Sjdp             files, temporarily set the BFD error handler to a
355038889Sjdp             function which will do nothing.  We still want to call
355138889Sjdp             bfd_merge_private_bfd_data, since it may set up
355238889Sjdp             information which is needed in the output file.  */
355338889Sjdp	  if (! command_line.warn_mismatch)
355438889Sjdp	    pfn = bfd_set_error_handler (ignore_bfd_errors);
355538889Sjdp	  if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
355638889Sjdp	    {
355738889Sjdp	      if (command_line.warn_mismatch)
355860484Sobrien		einfo (_("%E%X: failed to merge target specific data of file %B\n"),
355938889Sjdp		       input_bfd);
356038889Sjdp	    }
356138889Sjdp	  if (! command_line.warn_mismatch)
356238889Sjdp	    bfd_set_error_handler (pfn);
356338889Sjdp	}
356433965Sjdp    }
356533965Sjdp}
356633965Sjdp
356733965Sjdp/* Look through all the global common symbols and attach them to the
356833965Sjdp   correct section.  The -sort-common command line switch may be used
356933965Sjdp   to roughly sort the entries by size.  */
357033965Sjdp
357133965Sjdpstatic void
357233965Sjdplang_common ()
357333965Sjdp{
357489857Sobrien  if (command_line.inhibit_common_definition)
357589857Sobrien    return;
357633965Sjdp  if (link_info.relocateable
357733965Sjdp      && ! command_line.force_common_definition)
357833965Sjdp    return;
357933965Sjdp
358033965Sjdp  if (! config.sort_common)
358133965Sjdp    bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
358233965Sjdp  else
358333965Sjdp    {
358433965Sjdp      int power;
358533965Sjdp
358633965Sjdp      for (power = 4; power >= 0; power--)
358733965Sjdp	bfd_link_hash_traverse (link_info.hash, lang_one_common,
358833965Sjdp				(PTR) &power);
358933965Sjdp    }
359033965Sjdp}
359133965Sjdp
359233965Sjdp/* Place one common symbol in the correct section.  */
359333965Sjdp
359433965Sjdpstatic boolean
359533965Sjdplang_one_common (h, info)
359633965Sjdp     struct bfd_link_hash_entry *h;
359733965Sjdp     PTR info;
359833965Sjdp{
359933965Sjdp  unsigned int power_of_two;
360033965Sjdp  bfd_vma size;
360133965Sjdp  asection *section;
360277298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
360377298Sobrien						ldfile_output_machine);
360433965Sjdp
360533965Sjdp  if (h->type != bfd_link_hash_common)
360633965Sjdp    return true;
360733965Sjdp
360833965Sjdp  size = h->u.c.size;
360933965Sjdp  power_of_two = h->u.c.p->alignment_power;
361033965Sjdp
361133965Sjdp  if (config.sort_common
361233965Sjdp      && power_of_two < (unsigned int) *(int *) info)
361333965Sjdp    return true;
361433965Sjdp
361533965Sjdp  section = h->u.c.p->section;
361633965Sjdp
361733965Sjdp  /* Increase the size of the section.  */
361860484Sobrien  section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb,
361960484Sobrien				   (bfd_size_type) (1 << power_of_two)) * opb;
362033965Sjdp
362133965Sjdp  /* Adjust the alignment if necessary.  */
362233965Sjdp  if (power_of_two > section->alignment_power)
362333965Sjdp    section->alignment_power = power_of_two;
362433965Sjdp
362533965Sjdp  /* Change the symbol from common to defined.  */
362633965Sjdp  h->type = bfd_link_hash_defined;
362733965Sjdp  h->u.def.section = section;
362860484Sobrien  h->u.def.value = section->_cooked_size;
362933965Sjdp
363033965Sjdp  /* Increase the size of the section.  */
363160484Sobrien  section->_cooked_size += size;
363233965Sjdp
363333965Sjdp  /* Make sure the section is allocated in memory, and make sure that
363433965Sjdp     it is no longer a common section.  */
363533965Sjdp  section->flags |= SEC_ALLOC;
363677298Sobrien  section->flags &= ~SEC_IS_COMMON;
363733965Sjdp
363833965Sjdp  if (config.map_file != NULL)
363933965Sjdp    {
364033965Sjdp      static boolean header_printed;
364133965Sjdp      int len;
364233965Sjdp      char *name;
364333965Sjdp      char buf[50];
364433965Sjdp
364533965Sjdp      if (! header_printed)
364633965Sjdp	{
364760484Sobrien	  minfo (_("\nAllocating common symbols\n"));
364860484Sobrien	  minfo (_("Common symbol       size              file\n\n"));
364933965Sjdp	  header_printed = true;
365033965Sjdp	}
365133965Sjdp
365233965Sjdp      name = demangle (h->root.string);
365333965Sjdp      minfo ("%s", name);
365433965Sjdp      len = strlen (name);
365533965Sjdp      free (name);
365633965Sjdp
365733965Sjdp      if (len >= 19)
365833965Sjdp	{
365933965Sjdp	  print_nl ();
366033965Sjdp	  len = 0;
366133965Sjdp	}
366233965Sjdp      while (len < 20)
366333965Sjdp	{
366433965Sjdp	  print_space ();
366533965Sjdp	  ++len;
366633965Sjdp	}
366733965Sjdp
366833965Sjdp      minfo ("0x");
366933965Sjdp      if (size <= 0xffffffff)
367033965Sjdp	sprintf (buf, "%lx", (unsigned long) size);
367133965Sjdp      else
367233965Sjdp	sprintf_vma (buf, size);
367333965Sjdp      minfo ("%s", buf);
367433965Sjdp      len = strlen (buf);
367533965Sjdp
367633965Sjdp      while (len < 16)
367733965Sjdp	{
367833965Sjdp	  print_space ();
367933965Sjdp	  ++len;
368033965Sjdp	}
368133965Sjdp
368233965Sjdp      minfo ("%B\n", section->owner);
368333965Sjdp    }
368433965Sjdp
368533965Sjdp  return true;
368633965Sjdp}
368733965Sjdp
368877298Sobrien/* Run through the input files and ensure that every input section has
368977298Sobrien   somewhere to go.  If one is found without a destination then create
369077298Sobrien   an input request and place it into the statement tree.  */
369133965Sjdp
369233965Sjdpstatic void
369333965Sjdplang_place_orphans ()
369433965Sjdp{
369560484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (file)
369633965Sjdp    {
369733965Sjdp      asection *s;
369833965Sjdp
369933965Sjdp      for (s = file->the_bfd->sections;
370033965Sjdp	   s != (asection *) NULL;
370133965Sjdp	   s = s->next)
370233965Sjdp	{
370333965Sjdp	  if (s->output_section == (asection *) NULL)
370433965Sjdp	    {
370533965Sjdp	      /* This section of the file is not attatched, root
370677298Sobrien	         around for a sensible place for it to go.  */
370733965Sjdp
370833965Sjdp	      if (file->just_syms_flag)
370933965Sjdp		{
371033965Sjdp		  /* We are only retrieving symbol values from this
371133965Sjdp                     file.  We want the symbols to act as though the
371233965Sjdp                     values in the file are absolute.  */
371333965Sjdp		  s->output_section = bfd_abs_section_ptr;
371433965Sjdp		  s->output_offset = s->vma;
371533965Sjdp		}
371633965Sjdp	      else if (strcmp (s->name, "COMMON") == 0)
371733965Sjdp		{
371833965Sjdp		  /* This is a lonely common section which must have
371933965Sjdp		     come from an archive.  We attach to the section
372033965Sjdp		     with the wildcard.  */
372133965Sjdp		  if (! link_info.relocateable
372233965Sjdp		      || command_line.force_common_definition)
372333965Sjdp		    {
372433965Sjdp		      if (default_common_section == NULL)
372533965Sjdp			{
372633965Sjdp#if 0
372733965Sjdp			  /* This message happens when using the
372833965Sjdp                             svr3.ifile linker script, so I have
372933965Sjdp                             disabled it.  */
373060484Sobrien			  info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
373133965Sjdp#endif
373233965Sjdp			  default_common_section =
373333965Sjdp			    lang_output_section_statement_lookup (".bss");
373433965Sjdp
373533965Sjdp			}
373689857Sobrien		      lang_add_section (&default_common_section->children, s,
373789857Sobrien					default_common_section, file);
373833965Sjdp		    }
373933965Sjdp		}
374033965Sjdp	      else if (ldemul_place_orphan (file, s))
374133965Sjdp		;
374233965Sjdp	      else
374333965Sjdp		{
374489857Sobrien		  lang_output_section_statement_type *os;
374533965Sjdp
374689857Sobrien		  os = lang_output_section_statement_lookup (s->name);
374789857Sobrien		  lang_add_section (&os->children, s, os, file);
374833965Sjdp		}
374933965Sjdp	    }
375033965Sjdp	}
375133965Sjdp    }
375233965Sjdp}
375333965Sjdp
375433965Sjdpvoid
375560484Sobrienlang_set_flags (ptr, flags, invert)
375638889Sjdp     lang_memory_region_type *ptr;
375777298Sobrien     const char *flags;
375860484Sobrien     int invert;
375933965Sjdp{
376060484Sobrien  flagword *ptr_flags;
376133965Sjdp
376260484Sobrien  ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
376333965Sjdp  while (*flags)
376433965Sjdp    {
376533965Sjdp      switch (*flags)
376633965Sjdp	{
376738889Sjdp	case 'A': case 'a':
376838889Sjdp	  *ptr_flags |= SEC_ALLOC;
376933965Sjdp	  break;
377038889Sjdp
377138889Sjdp	case 'R': case 'r':
377238889Sjdp	  *ptr_flags |= SEC_READONLY;
377333965Sjdp	  break;
377438889Sjdp
377538889Sjdp	case 'W': case 'w':
377638889Sjdp	  *ptr_flags |= SEC_DATA;
377733965Sjdp	  break;
377838889Sjdp
377938889Sjdp	case 'X': case 'x':
378038889Sjdp	  *ptr_flags |= SEC_CODE;
378138889Sjdp	  break;
378238889Sjdp
378338889Sjdp	case 'L': case 'l':
378438889Sjdp	case 'I': case 'i':
378538889Sjdp	  *ptr_flags |= SEC_LOAD;
378638889Sjdp	  break;
378738889Sjdp
378833965Sjdp	default:
378960484Sobrien	  einfo (_("%P%F: invalid syntax in flags\n"));
379033965Sjdp	  break;
379133965Sjdp	}
379233965Sjdp      flags++;
379333965Sjdp    }
379433965Sjdp}
379533965Sjdp
379633965Sjdp/* Call a function on each input file.  This function will be called
379733965Sjdp   on an archive, but not on the elements.  */
379833965Sjdp
379933965Sjdpvoid
380033965Sjdplang_for_each_input_file (func)
380133965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
380233965Sjdp{
380333965Sjdp  lang_input_statement_type *f;
380433965Sjdp
380533965Sjdp  for (f = (lang_input_statement_type *) input_file_chain.head;
380633965Sjdp       f != NULL;
380733965Sjdp       f = (lang_input_statement_type *) f->next_real_file)
380833965Sjdp    func (f);
380933965Sjdp}
381033965Sjdp
381133965Sjdp/* Call a function on each file.  The function will be called on all
381233965Sjdp   the elements of an archive which are included in the link, but will
381333965Sjdp   not be called on the archive file itself.  */
381433965Sjdp
381533965Sjdpvoid
381633965Sjdplang_for_each_file (func)
381733965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
381833965Sjdp{
381960484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
382033965Sjdp    {
382133965Sjdp      func (f);
382233965Sjdp    }
382333965Sjdp}
382433965Sjdp
382533965Sjdp#if 0
382633965Sjdp
382733965Sjdp/* Not used.  */
382833965Sjdp
382933965Sjdpvoid
383033965Sjdplang_for_each_input_section (func)
383177298Sobrien     void (*func) PARAMS ((bfd *ab, asection *as));
383233965Sjdp{
383360484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
383433965Sjdp    {
383577298Sobrien      asection *s;
383633965Sjdp
383733965Sjdp      for (s = f->the_bfd->sections;
383833965Sjdp	   s != (asection *) NULL;
383933965Sjdp	   s = s->next)
384033965Sjdp	{
384133965Sjdp	  func (f->the_bfd, s);
384233965Sjdp	}
384333965Sjdp    }
384433965Sjdp}
384533965Sjdp
384633965Sjdp#endif
384733965Sjdp
384833965Sjdpvoid
384933965Sjdpldlang_add_file (entry)
385077298Sobrien     lang_input_statement_type *entry;
385133965Sjdp{
385233965Sjdp  bfd **pp;
385333965Sjdp
385433965Sjdp  lang_statement_append (&file_chain,
385533965Sjdp			 (lang_statement_union_type *) entry,
385633965Sjdp			 &entry->next);
385733965Sjdp
385833965Sjdp  /* The BFD linker needs to have a list of all input BFDs involved in
385933965Sjdp     a link.  */
386033965Sjdp  ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
386133965Sjdp  ASSERT (entry->the_bfd != output_bfd);
386233965Sjdp  for (pp = &link_info.input_bfds;
386333965Sjdp       *pp != (bfd *) NULL;
386433965Sjdp       pp = &(*pp)->link_next)
386533965Sjdp    ;
386633965Sjdp  *pp = entry->the_bfd;
386733965Sjdp  entry->the_bfd->usrdata = (PTR) entry;
386833965Sjdp  bfd_set_gp_size (entry->the_bfd, g_switch_value);
386933965Sjdp
387033965Sjdp  /* Look through the sections and check for any which should not be
387133965Sjdp     included in the link.  We need to do this now, so that we can
387233965Sjdp     notice when the backend linker tries to report multiple
387333965Sjdp     definition errors for symbols which are in sections we aren't
387433965Sjdp     going to link.  FIXME: It might be better to entirely ignore
387533965Sjdp     symbols which are defined in sections which are going to be
387633965Sjdp     discarded.  This would require modifying the backend linker for
387733965Sjdp     each backend which might set the SEC_LINK_ONCE flag.  If we do
387833965Sjdp     this, we should probably handle SEC_EXCLUDE in the same way.  */
387933965Sjdp
388033965Sjdp  bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
388133965Sjdp}
388233965Sjdp
388333965Sjdpvoid
388433965Sjdplang_add_output (name, from_script)
388577298Sobrien     const char *name;
388633965Sjdp     int from_script;
388733965Sjdp{
388833965Sjdp  /* Make -o on command line override OUTPUT in script.  */
388933965Sjdp  if (had_output_filename == false || !from_script)
389033965Sjdp    {
389133965Sjdp      output_filename = name;
389233965Sjdp      had_output_filename = true;
389333965Sjdp    }
389433965Sjdp}
389533965Sjdp
389633965Sjdpstatic lang_output_section_statement_type *current_section;
389733965Sjdp
389833965Sjdpstatic int
389933965Sjdptopower (x)
390033965Sjdp     int x;
390133965Sjdp{
390233965Sjdp  unsigned int i = 1;
390333965Sjdp  int l;
390433965Sjdp
390533965Sjdp  if (x < 0)
390633965Sjdp    return -1;
390733965Sjdp
390877298Sobrien  for (l = 0; l < 32; l++)
390933965Sjdp    {
391033965Sjdp      if (i >= (unsigned int) x)
391133965Sjdp	return l;
391233965Sjdp      i <<= 1;
391333965Sjdp    }
391433965Sjdp
391533965Sjdp  return 0;
391633965Sjdp}
391733965Sjdp
391868765Sobrienlang_output_section_statement_type *
391933965Sjdplang_enter_output_section_statement (output_section_statement_name,
392033965Sjdp				     address_exp, sectype, block_value,
392133965Sjdp				     align, subalign, ebase)
392233965Sjdp     const char *output_section_statement_name;
392377298Sobrien     etree_type *address_exp;
392433965Sjdp     enum section_type sectype;
392533965Sjdp     bfd_vma block_value;
392633965Sjdp     etree_type *align;
392733965Sjdp     etree_type *subalign;
392833965Sjdp     etree_type *ebase;
392933965Sjdp{
393033965Sjdp  lang_output_section_statement_type *os;
393133965Sjdp
393233965Sjdp  current_section =
393333965Sjdp   os =
393433965Sjdp    lang_output_section_statement_lookup (output_section_statement_name);
393533965Sjdp
393677298Sobrien  /* Add this statement to tree.  */
393777298Sobrien#if 0
393877298Sobrien  add_statement (lang_output_section_statement_enum,
393977298Sobrien		 output_section_statement);
394077298Sobrien#endif
394177298Sobrien  /* Make next things chain into subchain of this.  */
394233965Sjdp
394377298Sobrien  if (os->addr_tree == (etree_type *) NULL)
394477298Sobrien    {
394577298Sobrien      os->addr_tree = address_exp;
394677298Sobrien    }
394733965Sjdp  os->sectype = sectype;
394833965Sjdp  if (sectype != noload_section)
394933965Sjdp    os->flags = SEC_NO_FLAGS;
395033965Sjdp  else
395133965Sjdp    os->flags = SEC_NEVER_LOAD;
395233965Sjdp  os->block_value = block_value ? block_value : 1;
395333965Sjdp  stat_ptr = &os->children;
395433965Sjdp
395577298Sobrien  os->subsection_alignment =
395677298Sobrien    topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
395777298Sobrien  os->section_alignment =
395877298Sobrien    topower (exp_get_value_int (align, -1, "section alignment", 0));
395933965Sjdp
396033965Sjdp  os->load_base = ebase;
396168765Sobrien  return os;
396233965Sjdp}
396333965Sjdp
396433965Sjdpvoid
396533965Sjdplang_final ()
396633965Sjdp{
396733965Sjdp  lang_output_statement_type *new =
396833965Sjdp    new_stat (lang_output_statement, stat_ptr);
396933965Sjdp
397033965Sjdp  new->name = output_filename;
397133965Sjdp}
397233965Sjdp
397377298Sobrien/* Reset the current counters in the regions.  */
397477298Sobrien
397591041Sobrienvoid
397691041Sobrienlang_reset_memory_regions ()
397733965Sjdp{
397833965Sjdp  lang_memory_region_type *p = lang_memory_region_list;
397989857Sobrien  asection *o;
398033965Sjdp
398133965Sjdp  for (p = lang_memory_region_list;
398233965Sjdp       p != (lang_memory_region_type *) NULL;
398333965Sjdp       p = p->next)
398433965Sjdp    {
398533965Sjdp      p->old_length = (bfd_size_type) (p->current - p->origin);
398633965Sjdp      p->current = p->origin;
398733965Sjdp    }
398889857Sobrien
398989857Sobrien  for (o = output_bfd->sections; o != NULL; o = o->next)
399089857Sobrien    o->_raw_size = 0;
399133965Sjdp}
399233965Sjdp
399389857Sobrien/* If the wild pattern was marked KEEP, the member sections
399489857Sobrien   should be as well.  */
399560484Sobrien
399660484Sobrienstatic void
399789857Sobriengc_section_callback (ptr, sec, section, file, data)
399860484Sobrien     lang_wild_statement_type *ptr;
399989857Sobrien     struct wildcard_list *sec ATTRIBUTE_UNUSED;
400060484Sobrien     asection *section;
400160484Sobrien     lang_input_statement_type *file ATTRIBUTE_UNUSED;
400277298Sobrien     PTR data ATTRIBUTE_UNUSED;
400360484Sobrien{
400460484Sobrien  if (ptr->keep_sections)
400560484Sobrien    section->flags |= SEC_KEEP;
400660484Sobrien}
400760484Sobrien
400889857Sobrien/* Handle a wild statement, marking it against GC.  */
400960484Sobrien
401060484Sobrienstatic void
401189857Sobrienlang_gc_wild (s)
401260484Sobrien     lang_wild_statement_type *s;
401360484Sobrien{
401489857Sobrien  walk_wild (s, gc_section_callback, NULL);
401560484Sobrien}
401660484Sobrien
401760484Sobrien/* Iterate over sections marking them against GC.  */
401860484Sobrien
401960484Sobrienstatic void
402060484Sobrienlang_gc_sections_1 (s)
402177298Sobrien     lang_statement_union_type *s;
402260484Sobrien{
402389857Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
402460484Sobrien    {
402560484Sobrien      switch (s->header.type)
402660484Sobrien	{
402760484Sobrien	case lang_wild_statement_enum:
402889857Sobrien	  lang_gc_wild (&s->wild_statement);
402960484Sobrien	  break;
403060484Sobrien	case lang_constructors_statement_enum:
403160484Sobrien	  lang_gc_sections_1 (constructor_list.head);
403260484Sobrien	  break;
403360484Sobrien	case lang_output_section_statement_enum:
403460484Sobrien	  lang_gc_sections_1 (s->output_section_statement.children.head);
403560484Sobrien	  break;
403660484Sobrien	case lang_group_statement_enum:
403760484Sobrien	  lang_gc_sections_1 (s->group_statement.children.head);
403860484Sobrien	  break;
403960484Sobrien	default:
404060484Sobrien	  break;
404160484Sobrien	}
404260484Sobrien    }
404360484Sobrien}
404460484Sobrien
404560484Sobrienstatic void
404660484Sobrienlang_gc_sections ()
404760484Sobrien{
404860484Sobrien  struct bfd_link_hash_entry *h;
404960484Sobrien  ldlang_undef_chain_list_type *ulist, fake_list_start;
405060484Sobrien
405160484Sobrien  /* Keep all sections so marked in the link script.  */
405260484Sobrien
405360484Sobrien  lang_gc_sections_1 (statement_list.head);
405460484Sobrien
405560484Sobrien  /* Keep all sections containing symbols undefined on the command-line.
405660484Sobrien     Handle the entry symbol at the same time.  */
405777298Sobrien
405860484Sobrien  if (entry_symbol != NULL)
405960484Sobrien    {
406060484Sobrien      fake_list_start.next = ldlang_undef_chain_list_head;
406160484Sobrien      fake_list_start.name = (char *) entry_symbol;
406260484Sobrien      ulist = &fake_list_start;
406360484Sobrien    }
406460484Sobrien  else
406560484Sobrien    ulist = ldlang_undef_chain_list_head;
406660484Sobrien
406760484Sobrien  for (; ulist; ulist = ulist->next)
406860484Sobrien    {
406977298Sobrien      h = bfd_link_hash_lookup (link_info.hash, ulist->name,
407060484Sobrien				false, false, false);
407160484Sobrien
407260484Sobrien      if (h != (struct bfd_link_hash_entry *) NULL
407377298Sobrien	  && (h->type == bfd_link_hash_defined
407477298Sobrien	      || h->type == bfd_link_hash_defweak)
407560484Sobrien	  && ! bfd_is_abs_section (h->u.def.section))
407660484Sobrien	{
407760484Sobrien	  h->u.def.section->flags |= SEC_KEEP;
407860484Sobrien	}
407960484Sobrien    }
408060484Sobrien
408160484Sobrien  bfd_gc_sections (output_bfd, &link_info);
408260484Sobrien}
408360484Sobrien
408433965Sjdpvoid
408533965Sjdplang_process ()
408633965Sjdp{
408733965Sjdp  lang_reasonable_defaults ();
408833965Sjdp  current_target = default_target;
408933965Sjdp
409077298Sobrien  /* Open the output file.  */
409177298Sobrien  lang_for_each_statement (ldlang_open_output);
409233965Sjdp
409333965Sjdp  ldemul_create_output_section_statements ();
409433965Sjdp
409577298Sobrien  /* Add to the hash table all undefineds on the command line.  */
409633965Sjdp  lang_place_undefineds ();
409733965Sjdp
409860484Sobrien  already_linked_table_init ();
409960484Sobrien
410077298Sobrien  /* Create a bfd for each input file.  */
410133965Sjdp  current_target = default_target;
410233965Sjdp  open_input_bfds (statement_list.head, false);
410333965Sjdp
410433965Sjdp  ldemul_after_open ();
410533965Sjdp
410660484Sobrien  already_linked_table_free ();
410760484Sobrien
410833965Sjdp  /* Make sure that we're not mixing architectures.  We call this
410933965Sjdp     after all the input files have been opened, but before we do any
411033965Sjdp     other processing, so that any operations merge_private_bfd_data
411133965Sjdp     does on the output file will be known during the rest of the
411233965Sjdp     link.  */
411333965Sjdp  lang_check ();
411433965Sjdp
411560484Sobrien  /* Handle .exports instead of a version script if we're told to do so.  */
411660484Sobrien  if (command_line.version_exports_section)
411760484Sobrien    lang_do_version_exports_section ();
411860484Sobrien
411933965Sjdp  /* Build all sets based on the information gathered from the input
412033965Sjdp     files.  */
412133965Sjdp  ldctor_build_sets ();
412233965Sjdp
412360484Sobrien  /* Remove unreferenced sections if asked to.  */
412460484Sobrien  if (command_line.gc_sections)
412560484Sobrien    lang_gc_sections ();
412660484Sobrien
412789857Sobrien  /* If there were any SEC_MERGE sections, finish their merging, so that
412889857Sobrien     section sizes can be computed.  This has to be done after GC of sections,
412989857Sobrien     so that GCed sections are not merged, but before assigning output
413089857Sobrien     sections, since removing whole input sections is hard then.  */
413189857Sobrien  bfd_merge_sections (output_bfd, &link_info);
413289857Sobrien
413377298Sobrien  /* Size up the common data.  */
413433965Sjdp  lang_common ();
413533965Sjdp
413633965Sjdp  /* Run through the contours of the script and attach input sections
413777298Sobrien     to the correct output sections.  */
413833965Sjdp  map_input_to_output_sections (statement_list.head, (char *) NULL,
413933965Sjdp				(lang_output_section_statement_type *) NULL);
414033965Sjdp
414177298Sobrien  /* Find any sections not attached explicitly and handle them.  */
414233965Sjdp  lang_place_orphans ();
414333965Sjdp
414433965Sjdp  ldemul_before_allocation ();
414533965Sjdp
414633965Sjdp  /* We must record the program headers before we try to fix the
414733965Sjdp     section positions, since they will affect SIZEOF_HEADERS.  */
414833965Sjdp  lang_record_phdrs ();
414933965Sjdp
415089857Sobrien  /* Size up the sections.  */
415189857Sobrien  lang_size_sections (statement_list.head,
415289857Sobrien		      abs_output_section,
415389857Sobrien		      &statement_list.head, 0, (bfd_vma) 0, NULL);
415489857Sobrien
415577298Sobrien  /* Now run around and relax if we can.  */
415633965Sjdp  if (command_line.relax)
415733965Sjdp    {
415889857Sobrien      /* Keep relaxing until bfd_relax_section gives up.  */
415989857Sobrien      boolean relax_again;
416033965Sjdp
416133965Sjdp      do
416233965Sjdp	{
416391041Sobrien	  lang_reset_memory_regions ();
416433965Sjdp
416533965Sjdp	  relax_again = false;
416633965Sjdp
416760484Sobrien	  /* Note: pe-dll.c does something like this also.  If you find
416860484Sobrien	     you need to change this code, you probably need to change
416977298Sobrien	     pe-dll.c also.  DJ  */
417060484Sobrien
417133965Sjdp	  /* Do all the assignments with our current guesses as to
417233965Sjdp	     section sizes.  */
417333965Sjdp	  lang_do_assignments (statement_list.head,
417433965Sjdp			       abs_output_section,
417533965Sjdp			       (fill_type) 0, (bfd_vma) 0);
417633965Sjdp
417733965Sjdp	  /* Perform another relax pass - this time we know where the
417833965Sjdp	     globals are, so can make better guess.  */
417933965Sjdp	  lang_size_sections (statement_list.head,
418033965Sjdp			      abs_output_section,
418189857Sobrien			      &(statement_list.head), 0, (bfd_vma) 0,
418289857Sobrien			      &relax_again);
418333965Sjdp	}
418433965Sjdp      while (relax_again);
418533965Sjdp    }
418633965Sjdp
418733965Sjdp  /* See if anything special should be done now we know how big
418833965Sjdp     everything is.  */
418933965Sjdp  ldemul_after_allocation ();
419033965Sjdp
419133965Sjdp  /* Fix any .startof. or .sizeof. symbols.  */
419233965Sjdp  lang_set_startof ();
419333965Sjdp
419477298Sobrien  /* Do all the assignments, now that we know the final resting places
419577298Sobrien     of all the symbols.  */
419633965Sjdp
419733965Sjdp  lang_do_assignments (statement_list.head,
419833965Sjdp		       abs_output_section,
419933965Sjdp		       (fill_type) 0, (bfd_vma) 0);
420033965Sjdp
420160484Sobrien  /* Make sure that the section addresses make sense.  */
420260484Sobrien  if (! link_info.relocateable
420360484Sobrien      && command_line.check_section_addresses)
420460484Sobrien    lang_check_section_addresses ();
420533965Sjdp
420677298Sobrien  /* Final stuffs.  */
420777298Sobrien
420833965Sjdp  ldemul_finish ();
420933965Sjdp  lang_finish ();
421033965Sjdp}
421133965Sjdp
421233965Sjdp/* EXPORTED TO YACC */
421333965Sjdp
421433965Sjdpvoid
421589857Sobrienlang_add_wild (filespec, section_list, keep_sections)
421689857Sobrien     struct wildcard_spec *filespec;
421789857Sobrien     struct wildcard_list *section_list;
421860484Sobrien     boolean keep_sections;
421933965Sjdp{
422089857Sobrien  struct wildcard_list *curr, *next;
422189857Sobrien  lang_wild_statement_type *new;
422233965Sjdp
422389857Sobrien  /* Reverse the list as the parser puts it back to front.  */
422489857Sobrien  for (curr = section_list, section_list = NULL;
422589857Sobrien       curr != NULL;
422689857Sobrien       section_list = curr, curr = next)
422733965Sjdp    {
422889857Sobrien      if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
422989857Sobrien	placed_commons = true;
423089857Sobrien
423189857Sobrien      next = curr->next;
423289857Sobrien      curr->next = section_list;
423333965Sjdp    }
423489857Sobrien
423589857Sobrien  if (filespec != NULL && filespec->name != NULL)
423633965Sjdp    {
423789857Sobrien      if (strcmp (filespec->name, "*") == 0)
423889857Sobrien	filespec->name = NULL;
423989857Sobrien      else if (! wildcardp (filespec->name))
424089857Sobrien	lang_has_input_file = true;
424133965Sjdp    }
424289857Sobrien
424389857Sobrien  new = new_stat (lang_wild_statement, stat_ptr);
424489857Sobrien  new->filename = NULL;
424589857Sobrien  new->filenames_sorted = false;
424689857Sobrien  if (filespec != NULL)
424789857Sobrien    {
424889857Sobrien      new->filename = filespec->name;
424989857Sobrien      new->filenames_sorted = filespec->sorted;
425089857Sobrien    }
425189857Sobrien  new->section_list = section_list;
425260484Sobrien  new->keep_sections = keep_sections;
425333965Sjdp  lang_list_init (&new->children);
425433965Sjdp}
425533965Sjdp
425633965Sjdpvoid
425733965Sjdplang_section_start (name, address)
425877298Sobrien     const char *name;
425977298Sobrien     etree_type *address;
426033965Sjdp{
426178828Sobrien  lang_address_statement_type *ad;
426233965Sjdp
426378828Sobrien  ad = new_stat (lang_address_statement, stat_ptr);
426433965Sjdp  ad->section_name = name;
426533965Sjdp  ad->address = address;
426633965Sjdp}
426733965Sjdp
426833965Sjdp/* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
426933965Sjdp   because of a -e argument on the command line, or zero if this is
427033965Sjdp   called by ENTRY in a linker script.  Command line arguments take
427133965Sjdp   precedence.  */
427233965Sjdp
427333965Sjdpvoid
427433965Sjdplang_add_entry (name, cmdline)
427577298Sobrien     const char *name;
427633965Sjdp     boolean cmdline;
427733965Sjdp{
427833965Sjdp  if (entry_symbol == NULL
427933965Sjdp      || cmdline
428033965Sjdp      || ! entry_from_cmdline)
428133965Sjdp    {
428233965Sjdp      entry_symbol = name;
428333965Sjdp      entry_from_cmdline = cmdline;
428433965Sjdp    }
428533965Sjdp}
428633965Sjdp
428733965Sjdpvoid
428833965Sjdplang_add_target (name)
428977298Sobrien     const char *name;
429033965Sjdp{
429133965Sjdp  lang_target_statement_type *new = new_stat (lang_target_statement,
429233965Sjdp					      stat_ptr);
429333965Sjdp
429433965Sjdp  new->target = name;
429533965Sjdp
429633965Sjdp}
429733965Sjdp
429833965Sjdpvoid
429933965Sjdplang_add_map (name)
430077298Sobrien     const char *name;
430133965Sjdp{
430233965Sjdp  while (*name)
430333965Sjdp    {
430433965Sjdp      switch (*name)
430533965Sjdp	{
430677298Sobrien	case 'F':
430733965Sjdp	  map_option_f = true;
430833965Sjdp	  break;
430933965Sjdp	}
431033965Sjdp      name++;
431133965Sjdp    }
431233965Sjdp}
431333965Sjdp
431433965Sjdpvoid
431533965Sjdplang_add_fill (exp)
431633965Sjdp     int exp;
431733965Sjdp{
431833965Sjdp  lang_fill_statement_type *new = new_stat (lang_fill_statement,
431933965Sjdp					    stat_ptr);
432033965Sjdp
432133965Sjdp  new->fill = exp;
432233965Sjdp}
432333965Sjdp
432433965Sjdpvoid
432533965Sjdplang_add_data (type, exp)
432633965Sjdp     int type;
432733965Sjdp     union etree_union *exp;
432833965Sjdp{
432933965Sjdp
433033965Sjdp  lang_data_statement_type *new = new_stat (lang_data_statement,
433133965Sjdp					    stat_ptr);
433233965Sjdp
433333965Sjdp  new->exp = exp;
433433965Sjdp  new->type = type;
433533965Sjdp
433633965Sjdp}
433733965Sjdp
433833965Sjdp/* Create a new reloc statement.  RELOC is the BFD relocation type to
433933965Sjdp   generate.  HOWTO is the corresponding howto structure (we could
434033965Sjdp   look this up, but the caller has already done so).  SECTION is the
434133965Sjdp   section to generate a reloc against, or NAME is the name of the
434233965Sjdp   symbol to generate a reloc against.  Exactly one of SECTION and
434333965Sjdp   NAME must be NULL.  ADDEND is an expression for the addend.  */
434433965Sjdp
434533965Sjdpvoid
434633965Sjdplang_add_reloc (reloc, howto, section, name, addend)
434733965Sjdp     bfd_reloc_code_real_type reloc;
434833965Sjdp     reloc_howto_type *howto;
434933965Sjdp     asection *section;
435033965Sjdp     const char *name;
435133965Sjdp     union etree_union *addend;
435233965Sjdp{
435333965Sjdp  lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
435477298Sobrien
435533965Sjdp  p->reloc = reloc;
435633965Sjdp  p->howto = howto;
435733965Sjdp  p->section = section;
435833965Sjdp  p->name = name;
435933965Sjdp  p->addend_exp = addend;
436033965Sjdp
436133965Sjdp  p->addend_value = 0;
436233965Sjdp  p->output_section = NULL;
436333965Sjdp  p->output_vma = 0;
436433965Sjdp}
436533965Sjdp
436660484Sobrienlang_assignment_statement_type *
436733965Sjdplang_add_assignment (exp)
436877298Sobrien     etree_type *exp;
436933965Sjdp{
437033965Sjdp  lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
437133965Sjdp						  stat_ptr);
437233965Sjdp
437333965Sjdp  new->exp = exp;
437460484Sobrien  return new;
437533965Sjdp}
437633965Sjdp
437733965Sjdpvoid
437833965Sjdplang_add_attribute (attribute)
437933965Sjdp     enum statement_enum attribute;
438033965Sjdp{
438133965Sjdp  new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
438233965Sjdp}
438333965Sjdp
438433965Sjdpvoid
438533965Sjdplang_startup (name)
438677298Sobrien     const char *name;
438733965Sjdp{
438833965Sjdp  if (startup_file != (char *) NULL)
438933965Sjdp    {
439060484Sobrien      einfo (_("%P%Fmultiple STARTUP files\n"));
439133965Sjdp    }
439233965Sjdp  first_file->filename = name;
439333965Sjdp  first_file->local_sym_name = name;
439433965Sjdp  first_file->real = true;
439533965Sjdp
439633965Sjdp  startup_file = name;
439733965Sjdp}
439833965Sjdp
439933965Sjdpvoid
440033965Sjdplang_float (maybe)
440133965Sjdp     boolean maybe;
440233965Sjdp{
440333965Sjdp  lang_float_flag = maybe;
440433965Sjdp}
440533965Sjdp
440633965Sjdpvoid
440760484Sobrienlang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
440833965Sjdp     bfd_vma fill;
440933965Sjdp     const char *memspec;
441033965Sjdp     struct lang_output_section_phdr_list *phdrs;
441160484Sobrien     const char *lma_memspec;
441233965Sjdp{
441333965Sjdp  current_section->fill = fill;
441433965Sjdp  current_section->region = lang_memory_region_lookup (memspec);
441560484Sobrien  if (strcmp (lma_memspec, "*default*") != 0)
441660484Sobrien    {
441760484Sobrien      current_section->lma_region = lang_memory_region_lookup (lma_memspec);
441877298Sobrien      /* If no runtime region has been given, but the load region has
441977298Sobrien         been, use the load region.  */
442060484Sobrien      if (strcmp (memspec, "*default*") == 0)
442160484Sobrien        current_section->region = lang_memory_region_lookup (lma_memspec);
442260484Sobrien    }
442333965Sjdp  current_section->phdrs = phdrs;
442433965Sjdp  stat_ptr = &statement_list;
442533965Sjdp}
442633965Sjdp
442777298Sobrien/* Create an absolute symbol with the given name with the value of the
442877298Sobrien   address of first byte of the section named.
442933965Sjdp
443077298Sobrien   If the symbol already exists, then do nothing.  */
443177298Sobrien
443233965Sjdpvoid
443333965Sjdplang_abs_symbol_at_beginning_of (secname, name)
443433965Sjdp     const char *secname;
443533965Sjdp     const char *name;
443633965Sjdp{
443733965Sjdp  struct bfd_link_hash_entry *h;
443833965Sjdp
443933965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
444033965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
444160484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
444233965Sjdp
444333965Sjdp  if (h->type == bfd_link_hash_new
444433965Sjdp      || h->type == bfd_link_hash_undefined)
444533965Sjdp    {
444633965Sjdp      asection *sec;
444733965Sjdp
444833965Sjdp      h->type = bfd_link_hash_defined;
444933965Sjdp
445033965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
445133965Sjdp      if (sec == (asection *) NULL)
445233965Sjdp	h->u.def.value = 0;
445333965Sjdp      else
445433965Sjdp	h->u.def.value = bfd_get_section_vma (output_bfd, sec);
445533965Sjdp
445633965Sjdp      h->u.def.section = bfd_abs_section_ptr;
445733965Sjdp    }
445833965Sjdp}
445933965Sjdp
446077298Sobrien/* Create an absolute symbol with the given name with the value of the
446177298Sobrien   address of the first byte after the end of the section named.
446233965Sjdp
446377298Sobrien   If the symbol already exists, then do nothing.  */
446477298Sobrien
446533965Sjdpvoid
446633965Sjdplang_abs_symbol_at_end_of (secname, name)
446733965Sjdp     const char *secname;
446833965Sjdp     const char *name;
446933965Sjdp{
447033965Sjdp  struct bfd_link_hash_entry *h;
447133965Sjdp
447233965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
447333965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
447460484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
447533965Sjdp
447633965Sjdp  if (h->type == bfd_link_hash_new
447733965Sjdp      || h->type == bfd_link_hash_undefined)
447833965Sjdp    {
447933965Sjdp      asection *sec;
448033965Sjdp
448133965Sjdp      h->type = bfd_link_hash_defined;
448233965Sjdp
448333965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
448433965Sjdp      if (sec == (asection *) NULL)
448533965Sjdp	h->u.def.value = 0;
448633965Sjdp      else
448733965Sjdp	h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
448860484Sobrien			  + bfd_section_size (output_bfd, sec) /
448960484Sobrien                          bfd_octets_per_byte (output_bfd));
449033965Sjdp
449133965Sjdp      h->u.def.section = bfd_abs_section_ptr;
449233965Sjdp    }
449333965Sjdp}
449433965Sjdp
449533965Sjdpvoid
449633965Sjdplang_statement_append (list, element, field)
449777298Sobrien     lang_statement_list_type *list;
449877298Sobrien     lang_statement_union_type *element;
449977298Sobrien     lang_statement_union_type **field;
450033965Sjdp{
450133965Sjdp  *(list->tail) = element;
450233965Sjdp  list->tail = field;
450333965Sjdp}
450433965Sjdp
450533965Sjdp/* Set the output format type.  -oformat overrides scripts.  */
450633965Sjdp
450733965Sjdpvoid
450833965Sjdplang_add_output_format (format, big, little, from_script)
450933965Sjdp     const char *format;
451033965Sjdp     const char *big;
451133965Sjdp     const char *little;
451233965Sjdp     int from_script;
451333965Sjdp{
451433965Sjdp  if (output_target == NULL || !from_script)
451533965Sjdp    {
451633965Sjdp      if (command_line.endian == ENDIAN_BIG
451733965Sjdp	  && big != NULL)
451833965Sjdp	format = big;
451933965Sjdp      else if (command_line.endian == ENDIAN_LITTLE
452033965Sjdp	       && little != NULL)
452133965Sjdp	format = little;
452233965Sjdp
452333965Sjdp      output_target = format;
452433965Sjdp    }
452533965Sjdp}
452633965Sjdp
452733965Sjdp/* Enter a group.  This creates a new lang_group_statement, and sets
452833965Sjdp   stat_ptr to build new statements within the group.  */
452933965Sjdp
453033965Sjdpvoid
453133965Sjdplang_enter_group ()
453233965Sjdp{
453333965Sjdp  lang_group_statement_type *g;
453433965Sjdp
453533965Sjdp  g = new_stat (lang_group_statement, stat_ptr);
453633965Sjdp  lang_list_init (&g->children);
453733965Sjdp  stat_ptr = &g->children;
453833965Sjdp}
453933965Sjdp
454033965Sjdp/* Leave a group.  This just resets stat_ptr to start writing to the
454133965Sjdp   regular list of statements again.  Note that this will not work if
454233965Sjdp   groups can occur inside anything else which can adjust stat_ptr,
454333965Sjdp   but currently they can't.  */
454433965Sjdp
454533965Sjdpvoid
454633965Sjdplang_leave_group ()
454733965Sjdp{
454833965Sjdp  stat_ptr = &statement_list;
454933965Sjdp}
455033965Sjdp
455133965Sjdp/* Add a new program header.  This is called for each entry in a PHDRS
455233965Sjdp   command in a linker script.  */
455333965Sjdp
455433965Sjdpvoid
455533965Sjdplang_new_phdr (name, type, filehdr, phdrs, at, flags)
455633965Sjdp     const char *name;
455733965Sjdp     etree_type *type;
455833965Sjdp     boolean filehdr;
455933965Sjdp     boolean phdrs;
456033965Sjdp     etree_type *at;
456133965Sjdp     etree_type *flags;
456233965Sjdp{
456333965Sjdp  struct lang_phdr *n, **pp;
456433965Sjdp
456533965Sjdp  n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
456633965Sjdp  n->next = NULL;
456733965Sjdp  n->name = name;
456833965Sjdp  n->type = exp_get_value_int (type, 0, "program header type",
456933965Sjdp			       lang_final_phase_enum);
457033965Sjdp  n->filehdr = filehdr;
457133965Sjdp  n->phdrs = phdrs;
457233965Sjdp  n->at = at;
457333965Sjdp  n->flags = flags;
457433965Sjdp
457533965Sjdp  for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
457633965Sjdp    ;
457733965Sjdp  *pp = n;
457833965Sjdp}
457933965Sjdp
458033965Sjdp/* Record the program header information in the output BFD.  FIXME: We
458133965Sjdp   should not be calling an ELF specific function here.  */
458233965Sjdp
458333965Sjdpstatic void
458433965Sjdplang_record_phdrs ()
458533965Sjdp{
458633965Sjdp  unsigned int alc;
458733965Sjdp  asection **secs;
458833965Sjdp  struct lang_output_section_phdr_list *last;
458933965Sjdp  struct lang_phdr *l;
459033965Sjdp  lang_statement_union_type *u;
459133965Sjdp
459233965Sjdp  alc = 10;
459333965Sjdp  secs = (asection **) xmalloc (alc * sizeof (asection *));
459433965Sjdp  last = NULL;
459533965Sjdp  for (l = lang_phdr_list; l != NULL; l = l->next)
459633965Sjdp    {
459733965Sjdp      unsigned int c;
459833965Sjdp      flagword flags;
459933965Sjdp      bfd_vma at;
460033965Sjdp
460133965Sjdp      c = 0;
460233965Sjdp      for (u = lang_output_section_statement.head;
460333965Sjdp	   u != NULL;
460433965Sjdp	   u = u->output_section_statement.next)
460533965Sjdp	{
460633965Sjdp	  lang_output_section_statement_type *os;
460733965Sjdp	  struct lang_output_section_phdr_list *pl;
460833965Sjdp
460933965Sjdp	  os = &u->output_section_statement;
461033965Sjdp
461133965Sjdp	  pl = os->phdrs;
461233965Sjdp	  if (pl != NULL)
461333965Sjdp	    last = pl;
461433965Sjdp	  else
461533965Sjdp	    {
461633965Sjdp	      if (os->sectype == noload_section
461733965Sjdp		  || os->bfd_section == NULL
461833965Sjdp		  || (os->bfd_section->flags & SEC_ALLOC) == 0)
461933965Sjdp		continue;
462033965Sjdp	      pl = last;
462133965Sjdp	    }
462233965Sjdp
462333965Sjdp	  if (os->bfd_section == NULL)
462433965Sjdp	    continue;
462533965Sjdp
462633965Sjdp	  for (; pl != NULL; pl = pl->next)
462733965Sjdp	    {
462833965Sjdp	      if (strcmp (pl->name, l->name) == 0)
462933965Sjdp		{
463033965Sjdp		  if (c >= alc)
463133965Sjdp		    {
463233965Sjdp		      alc *= 2;
463333965Sjdp		      secs = ((asection **)
463433965Sjdp			      xrealloc (secs, alc * sizeof (asection *)));
463533965Sjdp		    }
463633965Sjdp		  secs[c] = os->bfd_section;
463733965Sjdp		  ++c;
463833965Sjdp		  pl->used = true;
463933965Sjdp		}
464033965Sjdp	    }
464133965Sjdp	}
464233965Sjdp
464333965Sjdp      if (l->flags == NULL)
464433965Sjdp	flags = 0;
464533965Sjdp      else
464633965Sjdp	flags = exp_get_vma (l->flags, 0, "phdr flags",
464733965Sjdp			     lang_final_phase_enum);
464833965Sjdp
464933965Sjdp      if (l->at == NULL)
465033965Sjdp	at = 0;
465133965Sjdp      else
465233965Sjdp	at = exp_get_vma (l->at, 0, "phdr load address",
465333965Sjdp			  lang_final_phase_enum);
465433965Sjdp
465533965Sjdp      if (! bfd_record_phdr (output_bfd, l->type,
465678828Sobrien			     l->flags != NULL, flags, l->at != NULL,
465733965Sjdp			     at, l->filehdr, l->phdrs, c, secs))
465860484Sobrien	einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
465933965Sjdp    }
466033965Sjdp
466133965Sjdp  free (secs);
466233965Sjdp
466333965Sjdp  /* Make sure all the phdr assignments succeeded.  */
466433965Sjdp  for (u = lang_output_section_statement.head;
466533965Sjdp       u != NULL;
466633965Sjdp       u = u->output_section_statement.next)
466733965Sjdp    {
466833965Sjdp      struct lang_output_section_phdr_list *pl;
466933965Sjdp
467033965Sjdp      if (u->output_section_statement.bfd_section == NULL)
467133965Sjdp	continue;
467233965Sjdp
467333965Sjdp      for (pl = u->output_section_statement.phdrs;
467433965Sjdp	   pl != NULL;
467533965Sjdp	   pl = pl->next)
467633965Sjdp	if (! pl->used && strcmp (pl->name, "NONE") != 0)
467760484Sobrien	  einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
467833965Sjdp		 u->output_section_statement.name, pl->name);
467933965Sjdp    }
468033965Sjdp}
468133965Sjdp
468233965Sjdp/* Record a list of sections which may not be cross referenced.  */
468333965Sjdp
468433965Sjdpvoid
468533965Sjdplang_add_nocrossref (l)
468633965Sjdp     struct lang_nocrossref *l;
468733965Sjdp{
468833965Sjdp  struct lang_nocrossrefs *n;
468933965Sjdp
469033965Sjdp  n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
469133965Sjdp  n->next = nocrossref_list;
469233965Sjdp  n->list = l;
469333965Sjdp  nocrossref_list = n;
469433965Sjdp
469533965Sjdp  /* Set notice_all so that we get informed about all symbols.  */
469633965Sjdp  link_info.notice_all = true;
469733965Sjdp}
469833965Sjdp
469933965Sjdp/* Overlay handling.  We handle overlays with some static variables.  */
470033965Sjdp
470133965Sjdp/* The overlay virtual address.  */
470233965Sjdpstatic etree_type *overlay_vma;
470333965Sjdp
470433965Sjdp/* The overlay load address.  */
470533965Sjdpstatic etree_type *overlay_lma;
470633965Sjdp
470733965Sjdp/* Whether nocrossrefs is set for this overlay.  */
470833965Sjdpstatic int overlay_nocrossrefs;
470933965Sjdp
471033965Sjdp/* An expression for the maximum section size seen so far.  */
471133965Sjdpstatic etree_type *overlay_max;
471233965Sjdp
471333965Sjdp/* A list of all the sections in this overlay.  */
471433965Sjdp
471577298Sobrienstruct overlay_list {
471633965Sjdp  struct overlay_list *next;
471733965Sjdp  lang_output_section_statement_type *os;
471833965Sjdp};
471933965Sjdp
472033965Sjdpstatic struct overlay_list *overlay_list;
472133965Sjdp
472233965Sjdp/* Start handling an overlay.  */
472333965Sjdp
472433965Sjdpvoid
472533965Sjdplang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
472633965Sjdp     etree_type *vma_expr;
472733965Sjdp     etree_type *lma_expr;
472833965Sjdp     int nocrossrefs;
472933965Sjdp{
473033965Sjdp  /* The grammar should prevent nested overlays from occurring.  */
473133965Sjdp  ASSERT (overlay_vma == NULL
473233965Sjdp	  && overlay_lma == NULL
473333965Sjdp	  && overlay_list == NULL
473433965Sjdp	  && overlay_max == NULL);
473533965Sjdp
473633965Sjdp  overlay_vma = vma_expr;
473733965Sjdp  overlay_lma = lma_expr;
473833965Sjdp  overlay_nocrossrefs = nocrossrefs;
473933965Sjdp}
474033965Sjdp
474133965Sjdp/* Start a section in an overlay.  We handle this by calling
474233965Sjdp   lang_enter_output_section_statement with the correct VMA and LMA.  */
474333965Sjdp
474433965Sjdpvoid
474533965Sjdplang_enter_overlay_section (name)
474633965Sjdp     const char *name;
474733965Sjdp{
474833965Sjdp  struct overlay_list *n;
474933965Sjdp  etree_type *size;
475033965Sjdp
475133965Sjdp  lang_enter_output_section_statement (name, overlay_vma, normal_section,
475233965Sjdp				       0, 0, 0, overlay_lma);
475333965Sjdp
475433965Sjdp  /* If this is the first section, then base the VMA and LMA of future
475533965Sjdp     sections on this one.  This will work correctly even if `.' is
475633965Sjdp     used in the addresses.  */
475733965Sjdp  if (overlay_list == NULL)
475833965Sjdp    {
475933965Sjdp      overlay_vma = exp_nameop (ADDR, name);
476033965Sjdp      overlay_lma = exp_nameop (LOADADDR, name);
476133965Sjdp    }
476233965Sjdp
476333965Sjdp  /* Remember the section.  */
476433965Sjdp  n = (struct overlay_list *) xmalloc (sizeof *n);
476533965Sjdp  n->os = current_section;
476633965Sjdp  n->next = overlay_list;
476733965Sjdp  overlay_list = n;
476833965Sjdp
476933965Sjdp  size = exp_nameop (SIZEOF, name);
477033965Sjdp
477133965Sjdp  /* Adjust the LMA for the next section.  */
477233965Sjdp  overlay_lma = exp_binop ('+', overlay_lma, size);
477333965Sjdp
477433965Sjdp  /* Arrange to work out the maximum section end address.  */
477533965Sjdp  if (overlay_max == NULL)
477633965Sjdp    overlay_max = size;
477733965Sjdp  else
477860484Sobrien    overlay_max = exp_binop (MAX_K, overlay_max, size);
477933965Sjdp}
478033965Sjdp
478133965Sjdp/* Finish a section in an overlay.  There isn't any special to do
478233965Sjdp   here.  */
478333965Sjdp
478433965Sjdpvoid
478533965Sjdplang_leave_overlay_section (fill, phdrs)
478633965Sjdp     bfd_vma fill;
478733965Sjdp     struct lang_output_section_phdr_list *phdrs;
478833965Sjdp{
478933965Sjdp  const char *name;
479033965Sjdp  char *clean, *s2;
479133965Sjdp  const char *s1;
479233965Sjdp  char *buf;
479333965Sjdp
479433965Sjdp  name = current_section->name;
479533965Sjdp
479677298Sobrien  lang_leave_output_section_statement (fill, "*default*",
479760484Sobrien                                       phdrs, "*default*");
479833965Sjdp
479933965Sjdp  /* Define the magic symbols.  */
480033965Sjdp
480133965Sjdp  clean = xmalloc (strlen (name) + 1);
480233965Sjdp  s2 = clean;
480333965Sjdp  for (s1 = name; *s1 != '\0'; s1++)
480489857Sobrien    if (ISALNUM (*s1) || *s1 == '_')
480533965Sjdp      *s2++ = *s1;
480633965Sjdp  *s2 = '\0';
480733965Sjdp
480833965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_start_");
480933965Sjdp  sprintf (buf, "__load_start_%s", clean);
481033965Sjdp  lang_add_assignment (exp_assop ('=', buf,
481133965Sjdp				  exp_nameop (LOADADDR, name)));
481233965Sjdp
481333965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
481433965Sjdp  sprintf (buf, "__load_stop_%s", clean);
481533965Sjdp  lang_add_assignment (exp_assop ('=', buf,
481633965Sjdp				  exp_binop ('+',
481733965Sjdp					     exp_nameop (LOADADDR, name),
481833965Sjdp					     exp_nameop (SIZEOF, name))));
481933965Sjdp
482033965Sjdp  free (clean);
482133965Sjdp}
482233965Sjdp
482333965Sjdp/* Finish an overlay.  If there are any overlay wide settings, this
482433965Sjdp   looks through all the sections in the overlay and sets them.  */
482533965Sjdp
482633965Sjdpvoid
482760484Sobrienlang_leave_overlay (fill, memspec, phdrs, lma_memspec)
482833965Sjdp     bfd_vma fill;
482933965Sjdp     const char *memspec;
483033965Sjdp     struct lang_output_section_phdr_list *phdrs;
483160484Sobrien     const char *lma_memspec;
483233965Sjdp{
483333965Sjdp  lang_memory_region_type *region;
483480016Sobrien  lang_memory_region_type * default_region;
483560484Sobrien  lang_memory_region_type *lma_region;
483633965Sjdp  struct overlay_list *l;
483733965Sjdp  struct lang_nocrossref *nocrossref;
483833965Sjdp
483980016Sobrien  default_region = lang_memory_region_lookup ("*default*");
484080016Sobrien
484133965Sjdp  if (memspec == NULL)
484233965Sjdp    region = NULL;
484333965Sjdp  else
484433965Sjdp    region = lang_memory_region_lookup (memspec);
484533965Sjdp
484660484Sobrien  if (lma_memspec == NULL)
484760484Sobrien    lma_region = NULL;
484860484Sobrien  else
484960484Sobrien    lma_region = lang_memory_region_lookup (lma_memspec);
485060484Sobrien
485133965Sjdp  nocrossref = NULL;
485233965Sjdp
485333965Sjdp  l = overlay_list;
485433965Sjdp  while (l != NULL)
485533965Sjdp    {
485633965Sjdp      struct overlay_list *next;
485733965Sjdp
485833965Sjdp      if (fill != 0 && l->os->fill == 0)
485933965Sjdp	l->os->fill = fill;
486080016Sobrien
486180016Sobrien      /* Assign a region to the sections, if one has been specified.
486280016Sobrien	 Override the assignment of the default section, but not
486380016Sobrien	 other sections.  */
486480016Sobrien      if (region != NULL &&
486580016Sobrien	  (l->os->region == NULL ||
486680016Sobrien	   l->os->region == default_region))
486733965Sjdp	l->os->region = region;
486880016Sobrien
486977298Sobrien      /* We only set lma_region for the first overlay section, as
487077298Sobrien	 subsequent overlay sections will have load_base set relative
487177298Sobrien	 to the first section.  Also, don't set lma_region if
487277298Sobrien	 load_base is specified.  FIXME:  There should really be a test
487377298Sobrien	 that `AT ( LDADDR )' doesn't conflict with `AT >LMA_REGION'
487477298Sobrien	 rather than letting LDADDR simply override LMA_REGION.  */
487577298Sobrien      if (lma_region != NULL && l->os->lma_region == NULL
487677298Sobrien	  && l->next == NULL && l->os->load_base == NULL)
487777298Sobrien	l->os->lma_region = lma_region;
487880016Sobrien
487933965Sjdp      if (phdrs != NULL && l->os->phdrs == NULL)
488033965Sjdp	l->os->phdrs = phdrs;
488133965Sjdp
488233965Sjdp      if (overlay_nocrossrefs)
488333965Sjdp	{
488433965Sjdp	  struct lang_nocrossref *nc;
488533965Sjdp
488633965Sjdp	  nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
488733965Sjdp	  nc->name = l->os->name;
488833965Sjdp	  nc->next = nocrossref;
488933965Sjdp	  nocrossref = nc;
489033965Sjdp	}
489133965Sjdp
489233965Sjdp      next = l->next;
489333965Sjdp      free (l);
489433965Sjdp      l = next;
489533965Sjdp    }
489633965Sjdp
489733965Sjdp  if (nocrossref != NULL)
489833965Sjdp    lang_add_nocrossref (nocrossref);
489933965Sjdp
490033965Sjdp  /* Update . for the end of the overlay.  */
490133965Sjdp  lang_add_assignment (exp_assop ('=', ".",
490233965Sjdp				  exp_binop ('+', overlay_vma, overlay_max)));
490333965Sjdp
490433965Sjdp  overlay_vma = NULL;
490533965Sjdp  overlay_lma = NULL;
490633965Sjdp  overlay_nocrossrefs = 0;
490733965Sjdp  overlay_list = NULL;
490833965Sjdp  overlay_max = NULL;
490933965Sjdp}
491033965Sjdp
491133965Sjdp/* Version handling.  This is only useful for ELF.  */
491233965Sjdp
491333965Sjdp/* This global variable holds the version tree that we build.  */
491433965Sjdp
491533965Sjdpstruct bfd_elf_version_tree *lang_elf_version_info;
491633965Sjdp
491760484Sobrienstatic int
491860484Sobrienlang_vers_match_lang_c (expr, sym)
491960484Sobrien     struct bfd_elf_version_expr *expr;
492060484Sobrien     const char *sym;
492160484Sobrien{
492260484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
492360484Sobrien    return 1;
492460484Sobrien  return fnmatch (expr->pattern, sym, 0) == 0;
492560484Sobrien}
492660484Sobrien
492760484Sobrienstatic int
492860484Sobrienlang_vers_match_lang_cplusplus (expr, sym)
492960484Sobrien     struct bfd_elf_version_expr *expr;
493060484Sobrien     const char *sym;
493160484Sobrien{
493260484Sobrien  char *alt_sym;
493360484Sobrien  int result;
493460484Sobrien
493560484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
493660484Sobrien    return 1;
493760484Sobrien
493877298Sobrien  alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
493960484Sobrien  if (!alt_sym)
494060484Sobrien    {
494160484Sobrien      /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
494260484Sobrien	 Should we early out false in this case?  */
494360484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
494460484Sobrien    }
494560484Sobrien  else
494660484Sobrien    {
494760484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
494860484Sobrien      free (alt_sym);
494960484Sobrien    }
495060484Sobrien
495160484Sobrien  return result;
495260484Sobrien}
495360484Sobrien
495460484Sobrienstatic int
495560484Sobrienlang_vers_match_lang_java (expr, sym)
495660484Sobrien     struct bfd_elf_version_expr *expr;
495760484Sobrien     const char *sym;
495860484Sobrien{
495960484Sobrien  char *alt_sym;
496060484Sobrien  int result;
496160484Sobrien
496260484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
496360484Sobrien    return 1;
496460484Sobrien
496577298Sobrien  alt_sym = cplus_demangle (sym, DMGL_JAVA);
496660484Sobrien  if (!alt_sym)
496760484Sobrien    {
496860484Sobrien      /* cplus_demangle (also) returns NULL when it is not a Java symbol.
496960484Sobrien	 Should we early out false in this case?  */
497060484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
497160484Sobrien    }
497260484Sobrien  else
497360484Sobrien    {
497460484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
497560484Sobrien      free (alt_sym);
497660484Sobrien    }
497760484Sobrien
497860484Sobrien  return result;
497960484Sobrien}
498060484Sobrien
498133965Sjdp/* This is called for each variable name or match expression.  */
498233965Sjdp
498333965Sjdpstruct bfd_elf_version_expr *
498491041Sobrienlang_new_vers_pattern (orig, new, lang)
498533965Sjdp     struct bfd_elf_version_expr *orig;
498633965Sjdp     const char *new;
498760484Sobrien     const char *lang;
498833965Sjdp{
498933965Sjdp  struct bfd_elf_version_expr *ret;
499033965Sjdp
499133965Sjdp  ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
499233965Sjdp  ret->next = orig;
499360484Sobrien  ret->pattern = new;
499460484Sobrien
499560484Sobrien  if (lang == NULL || strcasecmp (lang, "C") == 0)
499660484Sobrien    ret->match = lang_vers_match_lang_c;
499760484Sobrien  else if (strcasecmp (lang, "C++") == 0)
499860484Sobrien    ret->match = lang_vers_match_lang_cplusplus;
499960484Sobrien  else if (strcasecmp (lang, "Java") == 0)
500060484Sobrien    ret->match = lang_vers_match_lang_java;
500160484Sobrien  else
500260484Sobrien    {
500360484Sobrien      einfo (_("%X%P: unknown language `%s' in version information\n"),
500460484Sobrien	     lang);
500560484Sobrien      ret->match = lang_vers_match_lang_c;
500660484Sobrien    }
500760484Sobrien
500833965Sjdp  return ret;
500933965Sjdp}
501033965Sjdp
501133965Sjdp/* This is called for each set of variable names and match
501233965Sjdp   expressions.  */
501333965Sjdp
501433965Sjdpstruct bfd_elf_version_tree *
501533965Sjdplang_new_vers_node (globals, locals)
501633965Sjdp     struct bfd_elf_version_expr *globals;
501733965Sjdp     struct bfd_elf_version_expr *locals;
501833965Sjdp{
501933965Sjdp  struct bfd_elf_version_tree *ret;
502033965Sjdp
502133965Sjdp  ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
502233965Sjdp  ret->next = NULL;
502333965Sjdp  ret->name = NULL;
502433965Sjdp  ret->vernum = 0;
502533965Sjdp  ret->globals = globals;
502633965Sjdp  ret->locals = locals;
502733965Sjdp  ret->deps = NULL;
502833965Sjdp  ret->name_indx = (unsigned int) -1;
502933965Sjdp  ret->used = 0;
503033965Sjdp  return ret;
503133965Sjdp}
503233965Sjdp
503333965Sjdp/* This static variable keeps track of version indices.  */
503433965Sjdp
503533965Sjdpstatic int version_index;
503633965Sjdp
503733965Sjdp/* This is called when we know the name and dependencies of the
503833965Sjdp   version.  */
503933965Sjdp
504033965Sjdpvoid
504133965Sjdplang_register_vers_node (name, version, deps)
504233965Sjdp     const char *name;
504333965Sjdp     struct bfd_elf_version_tree *version;
504433965Sjdp     struct bfd_elf_version_deps *deps;
504533965Sjdp{
504633965Sjdp  struct bfd_elf_version_tree *t, **pp;
504733965Sjdp  struct bfd_elf_version_expr *e1;
504833965Sjdp
504989857Sobrien  if (name == NULL)
505089857Sobrien    name = "";
505189857Sobrien
505289857Sobrien  if ((name[0] == '\0' && lang_elf_version_info != NULL)
505389857Sobrien      || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
505489857Sobrien    {
505589857Sobrien      einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
505689857Sobrien      return;
505789857Sobrien    }
505889857Sobrien
505933965Sjdp  /* Make sure this node has a unique name.  */
506033965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
506133965Sjdp    if (strcmp (t->name, name) == 0)
506260484Sobrien      einfo (_("%X%P: duplicate version tag `%s'\n"), name);
506333965Sjdp
506433965Sjdp  /* Check the global and local match names, and make sure there
506533965Sjdp     aren't any duplicates.  */
506633965Sjdp
506733965Sjdp  for (e1 = version->globals; e1 != NULL; e1 = e1->next)
506833965Sjdp    {
506933965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
507033965Sjdp	{
507133965Sjdp	  struct bfd_elf_version_expr *e2;
507233965Sjdp
507333965Sjdp	  for (e2 = t->locals; e2 != NULL; e2 = e2->next)
507460484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
507560484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
507660484Sobrien		     e1->pattern);
507733965Sjdp	}
507833965Sjdp    }
507933965Sjdp
508033965Sjdp  for (e1 = version->locals; e1 != NULL; e1 = e1->next)
508133965Sjdp    {
508233965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
508333965Sjdp	{
508433965Sjdp	  struct bfd_elf_version_expr *e2;
508533965Sjdp
508633965Sjdp	  for (e2 = t->globals; e2 != NULL; e2 = e2->next)
508760484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
508860484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
508960484Sobrien		     e1->pattern);
509033965Sjdp	}
509133965Sjdp    }
509233965Sjdp
509333965Sjdp  version->deps = deps;
509433965Sjdp  version->name = name;
509589857Sobrien  if (name[0] != '\0')
509689857Sobrien    {
509789857Sobrien      ++version_index;
509889857Sobrien      version->vernum = version_index;
509989857Sobrien    }
510089857Sobrien  else
510189857Sobrien    version->vernum = 0;
510233965Sjdp
510333965Sjdp  for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
510433965Sjdp    ;
510533965Sjdp  *pp = version;
510633965Sjdp}
510733965Sjdp
510833965Sjdp/* This is called when we see a version dependency.  */
510933965Sjdp
511033965Sjdpstruct bfd_elf_version_deps *
511133965Sjdplang_add_vers_depend (list, name)
511233965Sjdp     struct bfd_elf_version_deps *list;
511333965Sjdp     const char *name;
511433965Sjdp{
511533965Sjdp  struct bfd_elf_version_deps *ret;
511633965Sjdp  struct bfd_elf_version_tree *t;
511733965Sjdp
511833965Sjdp  ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
511933965Sjdp  ret->next = list;
512033965Sjdp
512133965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
512233965Sjdp    {
512333965Sjdp      if (strcmp (t->name, name) == 0)
512433965Sjdp	{
512533965Sjdp	  ret->version_needed = t;
512633965Sjdp	  return ret;
512733965Sjdp	}
512833965Sjdp    }
512933965Sjdp
513060484Sobrien  einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
513133965Sjdp
513233965Sjdp  return ret;
513333965Sjdp}
513460484Sobrien
513560484Sobrienstatic void
513660484Sobrienlang_do_version_exports_section ()
513760484Sobrien{
513860484Sobrien  struct bfd_elf_version_expr *greg = NULL, *lreg;
513960484Sobrien
514060484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (is)
514160484Sobrien    {
514260484Sobrien      asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
514360484Sobrien      char *contents, *p;
514460484Sobrien      bfd_size_type len;
514560484Sobrien
514660484Sobrien      if (sec == NULL)
514760484Sobrien        continue;
514860484Sobrien
514960484Sobrien      len = bfd_section_size (is->the_bfd, sec);
515060484Sobrien      contents = xmalloc (len);
515160484Sobrien      if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
515289857Sobrien	einfo (_("%X%P: unable to read .exports section contents\n"), sec);
515360484Sobrien
515460484Sobrien      p = contents;
515577298Sobrien      while (p < contents + len)
515660484Sobrien	{
515791041Sobrien	  greg = lang_new_vers_pattern (greg, p, NULL);
515860484Sobrien	  p = strchr (p, '\0') + 1;
515960484Sobrien	}
516060484Sobrien
516160484Sobrien      /* Do not free the contents, as we used them creating the regex.  */
516260484Sobrien
516360484Sobrien      /* Do not include this section in the link.  */
516460484Sobrien      bfd_set_section_flags (is->the_bfd, sec,
516560484Sobrien	bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
516660484Sobrien    }
516760484Sobrien
516891041Sobrien  lreg = lang_new_vers_pattern (NULL, "*", NULL);
516960484Sobrien  lang_register_vers_node (command_line.version_exports_section,
517060484Sobrien			   lang_new_vers_node (greg, lreg), NULL);
517160484Sobrien}
517277298Sobrien
517377298Sobrienvoid
517477298Sobrienlang_add_unique (name)
517577298Sobrien     const char *name;
517677298Sobrien{
517777298Sobrien  struct unique_sections *ent;
517877298Sobrien
517977298Sobrien  for (ent = unique_section_list; ent; ent = ent->next)
518077298Sobrien    if (strcmp (ent->name, name) == 0)
518177298Sobrien      return;
518277298Sobrien
518377298Sobrien  ent = (struct unique_sections *) xmalloc (sizeof *ent);
518477298Sobrien  ent->name = xstrdup (name);
518577298Sobrien  ent->next = unique_section_list;
518677298Sobrien  unique_section_list = ent;
518777298Sobrien}
5188