ldlang.c revision 80016
133965Sjdp/* Linker command language support.
278828Sobrien   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
378828Sobrien   2001
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"
2633965Sjdp#include "obstack.h"
2733965Sjdp#include "bfdlink.h"
2833965Sjdp
2933965Sjdp#include "ld.h"
3033965Sjdp#include "ldmain.h"
3133965Sjdp#include "ldgram.h"
3233965Sjdp#include "ldexp.h"
3333965Sjdp#include "ldlang.h"
3433965Sjdp#include "ldlex.h"
3533965Sjdp#include "ldmisc.h"
3633965Sjdp#include "ldctor.h"
3733965Sjdp#include "ldfile.h"
3877298Sobrien#include "ldemul.h"
3933965Sjdp#include "fnmatch.h"
4060484Sobrien#include "demangle.h"
4133965Sjdp
4233965Sjdp#include <ctype.h>
4333965Sjdp
4433965Sjdp/* FORWARDS */
4578828Sobrienstatic lang_statement_union_type *new_statement
4678828Sobrien  PARAMS ((enum statement_enum, size_t, lang_statement_list_type *));
4733965Sjdp
4833965Sjdp/* LOCALS */
4933965Sjdpstatic struct obstack stat_obstack;
5033965Sjdp
5133965Sjdp#define obstack_chunk_alloc xmalloc
5233965Sjdp#define obstack_chunk_free free
5377298Sobrienstatic const char *startup_file;
5433965Sjdpstatic lang_statement_list_type input_file_chain;
5533965Sjdpstatic boolean placed_commons = false;
5633965Sjdpstatic lang_output_section_statement_type *default_common_section;
5733965Sjdpstatic boolean map_option_f;
5833965Sjdpstatic bfd_vma print_dot;
5933965Sjdpstatic lang_input_statement_type *first_file;
6077298Sobrienstatic const char *current_target;
6177298Sobrienstatic const char *output_target;
6233965Sjdpstatic lang_statement_list_type statement_list;
6333965Sjdpstatic struct lang_phdr *lang_phdr_list;
6433965Sjdp
6533965Sjdpstatic void lang_for_each_statement_worker
6677298Sobrien  PARAMS ((void (*) (lang_statement_union_type *),
6777298Sobrien	   lang_statement_union_type *));
6833965Sjdpstatic lang_input_statement_type *new_afile
6977298Sobrien  PARAMS ((const char *, lang_input_file_enum_type, const char *, boolean));
7077298Sobrienstatic lang_memory_region_type *lang_memory_default PARAMS ((asection *));
7177298Sobrienstatic void lang_map_flags PARAMS ((flagword));
7277298Sobrienstatic void init_os PARAMS ((lang_output_section_statement_type *));
7333965Sjdpstatic void exp_init_os PARAMS ((etree_type *));
7433965Sjdpstatic void section_already_linked PARAMS ((bfd *, asection *, PTR));
7560484Sobrienstatic struct bfd_hash_entry *already_linked_newfunc
7677298Sobrien  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
7760484Sobrienstatic void already_linked_table_init PARAMS ((void));
7860484Sobrienstatic void already_linked_table_free PARAMS ((void));
7933965Sjdpstatic boolean wildcardp PARAMS ((const char *));
8060484Sobrienstatic lang_statement_union_type *wild_sort
8160484Sobrien  PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
8260484Sobrien	   asection *));
8377298Sobrienstatic void output_section_callback
8477298Sobrien  PARAMS ((lang_wild_statement_type *, asection *,
8577298Sobrien	   lang_input_statement_type *, PTR));
8677298Sobrienstatic lang_input_statement_type *lookup_name PARAMS ((const char *));
8777298Sobrienstatic void load_symbols
8877298Sobrien  PARAMS ((lang_input_statement_type *, lang_statement_list_type *));
8977298Sobrienstatic void wild
9077298Sobrien  PARAMS ((lang_wild_statement_type *, const char *, const char *,
9177298Sobrien	   const char *, lang_output_section_statement_type *));
9277298Sobrienstatic bfd *open_output PARAMS ((const char *));
9377298Sobrienstatic void ldlang_open_output PARAMS ((lang_statement_union_type *));
9477298Sobrienstatic void open_input_bfds PARAMS ((lang_statement_union_type *, boolean));
9533965Sjdpstatic void lang_reasonable_defaults PARAMS ((void));
9633965Sjdpstatic void lang_place_undefineds PARAMS ((void));
9733965Sjdpstatic void map_input_to_output_sections
9877298Sobrien  PARAMS ((lang_statement_union_type *, const char *,
9977298Sobrien	   lang_output_section_statement_type *));
10033965Sjdpstatic void print_output_section_statement
10177298Sobrien  PARAMS ((lang_output_section_statement_type *));
10233965Sjdpstatic void print_assignment
10377298Sobrien  PARAMS ((lang_assignment_statement_type *,
10477298Sobrien	   lang_output_section_statement_type *));
10577298Sobrienstatic void print_input_statement PARAMS ((lang_input_statement_type *));
10633965Sjdpstatic boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
10777298Sobrienstatic void print_input_section PARAMS ((lang_input_section_type *));
10877298Sobrienstatic void print_fill_statement PARAMS ((lang_fill_statement_type *));
10977298Sobrienstatic void print_data_statement PARAMS ((lang_data_statement_type *));
11033965Sjdpstatic void print_address_statement PARAMS ((lang_address_statement_type *));
11177298Sobrienstatic void print_reloc_statement PARAMS ((lang_reloc_statement_type *));
11277298Sobrienstatic void print_padding_statement PARAMS ((lang_padding_statement_type *));
11333965Sjdpstatic void print_wild_statement
11477298Sobrien  PARAMS ((lang_wild_statement_type *, lang_output_section_statement_type *));
11533965Sjdpstatic void print_group
11633965Sjdp  PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
11777298Sobrienstatic void print_statement
11877298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
11977298Sobrienstatic void print_statement_list
12077298Sobrien  PARAMS ((lang_statement_union_type *, lang_output_section_statement_type *));
12133965Sjdpstatic void print_statements PARAMS ((void));
12277298Sobrienstatic bfd_vma insert_pad
12377298Sobrien  PARAMS ((lang_statement_union_type **, fill_type,
12477298Sobrien	   unsigned int, asection *, bfd_vma));
12533965Sjdpstatic bfd_vma size_input_section
12677298Sobrien  PARAMS ((lang_statement_union_type **, lang_output_section_statement_type *,
12777298Sobrien	   fill_type, bfd_vma, boolean));
12833965Sjdpstatic void lang_finish PARAMS ((void));
12938889Sjdpstatic void ignore_bfd_errors PARAMS ((const char *, ...));
13033965Sjdpstatic void lang_check PARAMS ((void));
13133965Sjdpstatic void lang_common PARAMS ((void));
13233965Sjdpstatic boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
13333965Sjdpstatic void lang_place_orphans PARAMS ((void));
13433965Sjdpstatic int topower PARAMS ((int));
13533965Sjdpstatic void lang_set_startof PARAMS ((void));
13633965Sjdpstatic void reset_memory_regions PARAMS ((void));
13777298Sobrienstatic void gc_section_callback
13877298Sobrien  PARAMS ((lang_wild_statement_type *, asection *,
13977298Sobrien	   lang_input_statement_type *, PTR));
14033965Sjdpstatic void lang_record_phdrs PARAMS ((void));
14160484Sobrienstatic void lang_gc_wild
14260484Sobrien  PARAMS ((lang_wild_statement_type *, const char *, const char *));
14360484Sobrienstatic void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
14460484Sobrienstatic void lang_gc_sections PARAMS ((void));
14577298Sobrienstatic int lang_vers_match_lang_c
14677298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14777298Sobrienstatic int lang_vers_match_lang_cplusplus
14877298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
14977298Sobrienstatic int lang_vers_match_lang_java
15077298Sobrien  PARAMS ((struct bfd_elf_version_expr *, const char *));
15160484Sobrienstatic void lang_do_version_exports_section PARAMS ((void));
15260484Sobrienstatic void lang_check_section_addresses PARAMS ((void));
15377298Sobrienstatic void os_region_check
15477298Sobrien  PARAMS ((lang_output_section_statement_type *,
15577298Sobrien	   struct memory_region_struct *, etree_type *, bfd_vma));
15633965Sjdp
15760484Sobrientypedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
15860484Sobrien				    asection *, lang_input_statement_type *,
15977298Sobrien				    PTR));
16077298Sobrienstatic void walk_wild
16177298Sobrien  PARAMS ((lang_wild_statement_type *, const char *, const char *,
16277298Sobrien	 callback_t, PTR));
16360484Sobrienstatic void walk_wild_section
16460484Sobrien  PARAMS ((lang_wild_statement_type *, const char *,
16577298Sobrien	   lang_input_statement_type *, callback_t, PTR));
16660484Sobrienstatic void walk_wild_file
16760484Sobrien  PARAMS ((lang_wild_statement_type *, const char *,
16877298Sobrien	   lang_input_statement_type *, callback_t, PTR));
16960484Sobrien
17077298Sobrienstatic int    get_target PARAMS ((const bfd_target *, PTR));
17160484Sobrienstatic void   stricpy PARAMS ((char *, char *));
17260484Sobrienstatic void   strcut PARAMS ((char *, char *));
17360484Sobrienstatic int    name_compare PARAMS ((char *, char *));
17477298Sobrienstatic int    closest_target_match PARAMS ((const bfd_target *, PTR));
17560484Sobrienstatic char * get_first_input_target PARAMS ((void));
17677298Sobrien
17733965Sjdp/* EXPORTS */
17833965Sjdplang_output_section_statement_type *abs_output_section;
17968765Sobrienlang_statement_list_type lang_output_section_statement;
18033965Sjdplang_statement_list_type *stat_ptr = &statement_list;
18160484Sobrienlang_statement_list_type file_chain = { NULL, NULL };
18233965Sjdpconst char *entry_symbol = NULL;
18333965Sjdpboolean entry_from_cmdline;
18433965Sjdpboolean lang_has_input_file = false;
18533965Sjdpboolean had_output_filename = false;
18633965Sjdpboolean lang_float_flag = false;
18733965Sjdpboolean delete_output_file_on_failure = false;
18833965Sjdpstruct lang_nocrossrefs *nocrossref_list;
18977298Sobrienstruct unique_sections *unique_section_list;
19033965Sjdp
19133965Sjdpetree_type *base; /* Relocation base - or null */
19233965Sjdp
19378828Sobrien#if defined (__STDC__) || defined (ALMOST_STDC)
19433965Sjdp#define cat(a,b) a##b
19533965Sjdp#else
19633965Sjdp#define cat(a,b) a/**/b
19733965Sjdp#endif
19833965Sjdp
19978828Sobrien/* Don't beautify the line below with "innocent" whitespace, it breaks
20078828Sobrien   the K&R C preprocessor!  */
20178828Sobrien#define new_stat(x, y) \
20278828Sobrien  (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
20333965Sjdp
20478828Sobrien#define outside_section_address(q) \
20578828Sobrien  ((q)->output_offset + (q)->output_section->vma)
20633965Sjdp
20778828Sobrien#define outside_symbol_address(q) \
20878828Sobrien  ((q)->value + outside_section_address (q->section))
20933965Sjdp
21033965Sjdp#define SECTION_NAME_MAP_LENGTH (16)
21133965Sjdp
21233965SjdpPTR
21333965Sjdpstat_alloc (size)
21433965Sjdp     size_t size;
21533965Sjdp{
21633965Sjdp  return obstack_alloc (&stat_obstack, size);
21733965Sjdp}
21833965Sjdp
21977298Sobrienboolean
22077298Sobrienunique_section_p (secnam)
22177298Sobrien     const char *secnam;
22277298Sobrien{
22377298Sobrien  struct unique_sections *unam;
22460484Sobrien
22577298Sobrien  for (unam = unique_section_list; unam; unam = unam->next)
22677298Sobrien    if (wildcardp (unam->name)
22777298Sobrien	? fnmatch (unam->name, secnam, 0) == 0
22877298Sobrien	: strcmp (unam->name, secnam) == 0)
22977298Sobrien      {
23077298Sobrien	return true;
23177298Sobrien      }
23277298Sobrien
23377298Sobrien  return false;
23477298Sobrien}
23577298Sobrien
23677298Sobrien/* Generic traversal routines for finding matching sections.  */
23777298Sobrien
23860484Sobrienstatic void
23960484Sobrienwalk_wild_section (ptr, section, file, callback, data)
24060484Sobrien     lang_wild_statement_type *ptr;
24160484Sobrien     const char *section;
24260484Sobrien     lang_input_statement_type *file;
24360484Sobrien     callback_t callback;
24477298Sobrien     PTR data;
24560484Sobrien{
24677298Sobrien  /* Don't process sections from files which were excluded.  */
24760484Sobrien  if (ptr->exclude_filename_list != NULL)
24860484Sobrien    {
24960484Sobrien      struct name_list *list_tmp;
25078828Sobrien      for (list_tmp = ptr->exclude_filename_list;
25178828Sobrien	   list_tmp;
25278828Sobrien	   list_tmp = list_tmp->next)
25377298Sobrien	{
25460484Sobrien	  boolean match;
25560484Sobrien
25660484Sobrien	  if (wildcardp (list_tmp->name))
25778828Sobrien	    match = fnmatch (list_tmp->name, file->filename, 0) == 0;
25860484Sobrien	  else
25978828Sobrien	    match = strcmp (list_tmp->name, file->filename) == 0;
26060484Sobrien
26160484Sobrien	  if (match)
26260484Sobrien	    return;
26360484Sobrien	}
26460484Sobrien    }
26560484Sobrien
26660484Sobrien  if (file->just_syms_flag == false)
26760484Sobrien    {
26860484Sobrien      register asection *s;
26977298Sobrien      boolean wildcard = false;
27060484Sobrien
27177298Sobrien      if (section != NULL)
27260484Sobrien	wildcard = wildcardp (section);
27360484Sobrien
27460484Sobrien      for (s = file->the_bfd->sections; s != NULL; s = s->next)
27560484Sobrien	{
27660484Sobrien	  boolean match;
27777298Sobrien	  const char *sname = bfd_get_section_name (file->the_bfd, s);
27860484Sobrien
27960484Sobrien	  if (section == NULL)
28060484Sobrien	    match = true;
28177298Sobrien	  else if (wildcard)
28278828Sobrien	    match = fnmatch (section, sname, 0) == 0;
28360484Sobrien	  else
28478828Sobrien	    match = strcmp (section, sname) == 0;
28560484Sobrien
28677298Sobrien	  /* If this is a wild-card output section statement, exclude
28777298Sobrien	     sections that match UNIQUE_SECTION_LIST.  */
28877298Sobrien	  if (match && (data == NULL || !unique_section_p (sname)))
28960484Sobrien	    (*callback) (ptr, s, file, data);
29060484Sobrien	}
29160484Sobrien    }
29260484Sobrien}
29360484Sobrien
29460484Sobrien/* Handle a wild statement for a single file F.  */
29560484Sobrien
29660484Sobrienstatic void
29760484Sobrienwalk_wild_file (s, section, f, callback, data)
29860484Sobrien     lang_wild_statement_type *s;
29960484Sobrien     const char *section;
30060484Sobrien     lang_input_statement_type *f;
30160484Sobrien     callback_t callback;
30277298Sobrien     PTR data;
30360484Sobrien{
30460484Sobrien  if (f->the_bfd == NULL
30560484Sobrien      || ! bfd_check_format (f->the_bfd, bfd_archive))
30660484Sobrien    walk_wild_section (s, section, f, callback, data);
30760484Sobrien  else
30860484Sobrien    {
30960484Sobrien      bfd *member;
31060484Sobrien
31160484Sobrien      /* This is an archive file.  We must map each member of the
31260484Sobrien	 archive separately.  */
31360484Sobrien      member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
31460484Sobrien      while (member != NULL)
31560484Sobrien	{
31660484Sobrien	  /* When lookup_name is called, it will call the add_symbols
31760484Sobrien	     entry point for the archive.  For each element of the
31860484Sobrien	     archive which is included, BFD will call ldlang_add_file,
31960484Sobrien	     which will set the usrdata field of the member to the
32060484Sobrien	     lang_input_statement.  */
32160484Sobrien	  if (member->usrdata != NULL)
32260484Sobrien	    {
32360484Sobrien	      walk_wild_section (s, section,
32460484Sobrien				 (lang_input_statement_type *) member->usrdata,
32560484Sobrien				 callback, data);
32660484Sobrien	    }
32760484Sobrien
32860484Sobrien	  member = bfd_openr_next_archived_file (f->the_bfd, member);
32960484Sobrien	}
33060484Sobrien    }
33160484Sobrien}
33260484Sobrien
33360484Sobrienstatic void
33460484Sobrienwalk_wild (s, section, file, callback, data)
33560484Sobrien     lang_wild_statement_type *s;
33660484Sobrien     const char *section;
33760484Sobrien     const char *file;
33860484Sobrien     callback_t callback;
33977298Sobrien     PTR data;
34060484Sobrien{
34160484Sobrien  if (file == (char *) NULL)
34260484Sobrien    {
34360484Sobrien      /* Perform the iteration over all files in the list.  */
34460484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
34560484Sobrien	{
34660484Sobrien	  walk_wild_file (s, section, f, callback, data);
34760484Sobrien	}
34860484Sobrien    }
34960484Sobrien  else if (wildcardp (file))
35060484Sobrien    {
35160484Sobrien      LANG_FOR_EACH_INPUT_STATEMENT (f)
35260484Sobrien	{
35360484Sobrien	  if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
35460484Sobrien	    walk_wild_file (s, section, f, callback, data);
35560484Sobrien	}
35660484Sobrien    }
35760484Sobrien  else
35860484Sobrien    {
35960484Sobrien      lang_input_statement_type *f;
36060484Sobrien
36160484Sobrien      /* Perform the iteration over a single file.  */
36260484Sobrien      f = lookup_name (file);
36360484Sobrien      walk_wild_file (s, section, f, callback, data);
36460484Sobrien    }
36577298Sobrien}
36633965Sjdp
36777298Sobrien/* lang_for_each_statement walks the parse tree and calls the provided
36877298Sobrien   function for each node.  */
36977298Sobrien
37033965Sjdpstatic void
37133965Sjdplang_for_each_statement_worker (func, s)
37233965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
37333965Sjdp     lang_statement_union_type *s;
37433965Sjdp{
37533965Sjdp  for (; s != (lang_statement_union_type *) NULL; s = s->next)
37633965Sjdp    {
37733965Sjdp      func (s);
37833965Sjdp
37933965Sjdp      switch (s->header.type)
38033965Sjdp	{
38133965Sjdp	case lang_constructors_statement_enum:
38233965Sjdp	  lang_for_each_statement_worker (func, constructor_list.head);
38333965Sjdp	  break;
38433965Sjdp	case lang_output_section_statement_enum:
38533965Sjdp	  lang_for_each_statement_worker
38633965Sjdp	    (func,
38733965Sjdp	     s->output_section_statement.children.head);
38833965Sjdp	  break;
38933965Sjdp	case lang_wild_statement_enum:
39033965Sjdp	  lang_for_each_statement_worker
39133965Sjdp	    (func,
39233965Sjdp	     s->wild_statement.children.head);
39333965Sjdp	  break;
39433965Sjdp	case lang_group_statement_enum:
39533965Sjdp	  lang_for_each_statement_worker (func,
39633965Sjdp					  s->group_statement.children.head);
39733965Sjdp	  break;
39833965Sjdp	case lang_data_statement_enum:
39933965Sjdp	case lang_reloc_statement_enum:
40033965Sjdp	case lang_object_symbols_statement_enum:
40133965Sjdp	case lang_output_statement_enum:
40233965Sjdp	case lang_target_statement_enum:
40333965Sjdp	case lang_input_section_enum:
40433965Sjdp	case lang_input_statement_enum:
40533965Sjdp	case lang_assignment_statement_enum:
40633965Sjdp	case lang_padding_statement_enum:
40733965Sjdp	case lang_address_statement_enum:
40833965Sjdp	case lang_fill_statement_enum:
40933965Sjdp	  break;
41033965Sjdp	default:
41133965Sjdp	  FAIL ();
41233965Sjdp	  break;
41333965Sjdp	}
41433965Sjdp    }
41533965Sjdp}
41633965Sjdp
41733965Sjdpvoid
41833965Sjdplang_for_each_statement (func)
41933965Sjdp     void (*func) PARAMS ((lang_statement_union_type *));
42033965Sjdp{
42177298Sobrien  lang_for_each_statement_worker (func, statement_list.head);
42233965Sjdp}
42333965Sjdp
42433965Sjdp/*----------------------------------------------------------------------*/
42577298Sobrien
42633965Sjdpvoid
42733965Sjdplang_list_init (list)
42833965Sjdp     lang_statement_list_type *list;
42933965Sjdp{
43033965Sjdp  list->head = (lang_statement_union_type *) NULL;
43133965Sjdp  list->tail = &list->head;
43233965Sjdp}
43333965Sjdp
43477298Sobrien/* Build a new statement node for the parse tree.  */
43533965Sjdp
43677298Sobrienstatic lang_statement_union_type *
43733965Sjdpnew_statement (type, size, list)
43833965Sjdp     enum statement_enum type;
43933965Sjdp     size_t size;
44077298Sobrien     lang_statement_list_type *list;
44133965Sjdp{
44233965Sjdp  lang_statement_union_type *new = (lang_statement_union_type *)
44333965Sjdp  stat_alloc (size);
44433965Sjdp
44533965Sjdp  new->header.type = type;
44633965Sjdp  new->header.next = (lang_statement_union_type *) NULL;
44733965Sjdp  lang_statement_append (list, new, &new->header.next);
44833965Sjdp  return new;
44933965Sjdp}
45033965Sjdp
45177298Sobrien/* Build a new input file node for the language.  There are several
45277298Sobrien   ways in which we treat an input file, eg, we only look at symbols,
45377298Sobrien   or prefix it with a -l etc.
45433965Sjdp
45577298Sobrien   We can be supplied with requests for input files more than once;
45677298Sobrien   they may, for example be split over serveral lines like foo.o(.text)
45778828Sobrien   foo.o(.data) etc, so when asked for a file we check that we haven't
45877298Sobrien   got it already so we don't duplicate the bfd.  */
45933965Sjdp
46033965Sjdpstatic lang_input_statement_type *
46133965Sjdpnew_afile (name, file_type, target, add_to_list)
46277298Sobrien     const char *name;
46333965Sjdp     lang_input_file_enum_type file_type;
46477298Sobrien     const char *target;
46533965Sjdp     boolean add_to_list;
46633965Sjdp{
46733965Sjdp  lang_input_statement_type *p;
46833965Sjdp
46933965Sjdp  if (add_to_list)
47033965Sjdp    p = new_stat (lang_input_statement, stat_ptr);
47133965Sjdp  else
47233965Sjdp    {
47333965Sjdp      p = ((lang_input_statement_type *)
47433965Sjdp	   stat_alloc (sizeof (lang_input_statement_type)));
47533965Sjdp      p->header.next = NULL;
47633965Sjdp    }
47733965Sjdp
47833965Sjdp  lang_has_input_file = true;
47933965Sjdp  p->target = target;
48033965Sjdp  switch (file_type)
48133965Sjdp    {
48233965Sjdp    case lang_input_file_is_symbols_only_enum:
48333965Sjdp      p->filename = name;
48433965Sjdp      p->is_archive = false;
48533965Sjdp      p->real = true;
48633965Sjdp      p->local_sym_name = name;
48733965Sjdp      p->just_syms_flag = true;
48833965Sjdp      p->search_dirs_flag = false;
48933965Sjdp      break;
49033965Sjdp    case lang_input_file_is_fake_enum:
49133965Sjdp      p->filename = name;
49233965Sjdp      p->is_archive = false;
49333965Sjdp      p->real = false;
49433965Sjdp      p->local_sym_name = name;
49533965Sjdp      p->just_syms_flag = false;
49633965Sjdp      p->search_dirs_flag = false;
49733965Sjdp      break;
49833965Sjdp    case lang_input_file_is_l_enum:
49933965Sjdp      p->is_archive = true;
50033965Sjdp      p->filename = name;
50133965Sjdp      p->real = true;
50233965Sjdp      p->local_sym_name = concat ("-l", name, (const char *) NULL);
50333965Sjdp      p->just_syms_flag = false;
50433965Sjdp      p->search_dirs_flag = true;
50533965Sjdp      break;
50633965Sjdp    case lang_input_file_is_marker_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 = true;
51333965Sjdp      break;
51433965Sjdp    case lang_input_file_is_search_file_enum:
51533965Sjdp      p->filename = name;
51633965Sjdp      p->is_archive = false;
51733965Sjdp      p->real = true;
51833965Sjdp      p->local_sym_name = name;
51933965Sjdp      p->just_syms_flag = false;
52033965Sjdp      p->search_dirs_flag = true;
52133965Sjdp      break;
52233965Sjdp    case lang_input_file_is_file_enum:
52333965Sjdp      p->filename = name;
52433965Sjdp      p->is_archive = false;
52533965Sjdp      p->real = true;
52633965Sjdp      p->local_sym_name = name;
52733965Sjdp      p->just_syms_flag = false;
52833965Sjdp      p->search_dirs_flag = false;
52933965Sjdp      break;
53033965Sjdp    default:
53133965Sjdp      FAIL ();
53233965Sjdp    }
53333965Sjdp  p->the_bfd = (bfd *) NULL;
53433965Sjdp  p->asymbols = (asymbol **) NULL;
53533965Sjdp  p->next_real_file = (lang_statement_union_type *) NULL;
53633965Sjdp  p->next = (lang_statement_union_type *) NULL;
53733965Sjdp  p->symbol_count = 0;
53833965Sjdp  p->dynamic = config.dynamic_link;
53933965Sjdp  p->whole_archive = whole_archive;
54033965Sjdp  p->loaded = false;
54133965Sjdp  lang_statement_append (&input_file_chain,
54233965Sjdp			 (lang_statement_union_type *) p,
54333965Sjdp			 &p->next_real_file);
54433965Sjdp  return p;
54533965Sjdp}
54633965Sjdp
54733965Sjdplang_input_statement_type *
54833965Sjdplang_add_input_file (name, file_type, target)
54977298Sobrien     const char *name;
55033965Sjdp     lang_input_file_enum_type file_type;
55177298Sobrien     const char *target;
55233965Sjdp{
55333965Sjdp  lang_has_input_file = true;
55433965Sjdp  return new_afile (name, file_type, target, true);
55533965Sjdp}
55633965Sjdp
55777298Sobrien/* Build enough state so that the parser can build its tree.  */
55877298Sobrien
55933965Sjdpvoid
56033965Sjdplang_init ()
56133965Sjdp{
56233965Sjdp  obstack_begin (&stat_obstack, 1000);
56333965Sjdp
56433965Sjdp  stat_ptr = &statement_list;
56533965Sjdp
56633965Sjdp  lang_list_init (stat_ptr);
56733965Sjdp
56833965Sjdp  lang_list_init (&input_file_chain);
56933965Sjdp  lang_list_init (&lang_output_section_statement);
57033965Sjdp  lang_list_init (&file_chain);
57133965Sjdp  first_file = lang_add_input_file ((char *) NULL,
57233965Sjdp				    lang_input_file_is_marker_enum,
57333965Sjdp				    (char *) NULL);
57477298Sobrien  abs_output_section =
57577298Sobrien    lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
57633965Sjdp
57733965Sjdp  abs_output_section->bfd_section = bfd_abs_section_ptr;
57833965Sjdp
57933965Sjdp}
58033965Sjdp
58133965Sjdp/*----------------------------------------------------------------------
58277298Sobrien  A region is an area of memory declared with the
58377298Sobrien  MEMORY {  name:org=exp, len=exp ... }
58477298Sobrien  syntax.
58533965Sjdp
58677298Sobrien  We maintain a list of all the regions here.
58733965Sjdp
58877298Sobrien  If no regions are specified in the script, then the default is used
58977298Sobrien  which is created when looked up to be the entire data space.  */
59033965Sjdp
59133965Sjdpstatic lang_memory_region_type *lang_memory_region_list;
59233965Sjdpstatic lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
59333965Sjdp
59433965Sjdplang_memory_region_type *
59533965Sjdplang_memory_region_lookup (name)
59677298Sobrien     const char *const name;
59733965Sjdp{
59838889Sjdp  lang_memory_region_type *p;
59933965Sjdp
60033965Sjdp  for (p = lang_memory_region_list;
60133965Sjdp       p != (lang_memory_region_type *) NULL;
60233965Sjdp       p = p->next)
60333965Sjdp    {
60433965Sjdp      if (strcmp (p->name, name) == 0)
60533965Sjdp	{
60633965Sjdp	  return p;
60733965Sjdp	}
60833965Sjdp    }
60933965Sjdp
61033965Sjdp#if 0
61133965Sjdp  /* This code used to always use the first region in the list as the
61233965Sjdp     default region.  I changed it to instead use a region
61333965Sjdp     encompassing all of memory as the default region.  This permits
61433965Sjdp     NOLOAD sections to work reasonably without requiring a region.
61533965Sjdp     People should specify what region they mean, if they really want
61633965Sjdp     a region.  */
61733965Sjdp  if (strcmp (name, "*default*") == 0)
61833965Sjdp    {
61933965Sjdp      if (lang_memory_region_list != (lang_memory_region_type *) NULL)
62033965Sjdp	{
62133965Sjdp	  return lang_memory_region_list;
62233965Sjdp	}
62333965Sjdp    }
62433965Sjdp#endif
62533965Sjdp
62633965Sjdp  {
62733965Sjdp    lang_memory_region_type *new =
62833965Sjdp    (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
62933965Sjdp
63078828Sobrien    new->name = xstrdup (name);
63133965Sjdp    new->next = (lang_memory_region_type *) NULL;
63233965Sjdp
63333965Sjdp    *lang_memory_region_list_tail = new;
63433965Sjdp    lang_memory_region_list_tail = &new->next;
63533965Sjdp    new->origin = 0;
63638889Sjdp    new->flags = 0;
63738889Sjdp    new->not_flags = 0;
63877298Sobrien    new->length = ~(bfd_size_type) 0;
63933965Sjdp    new->current = 0;
64033965Sjdp    new->had_full_message = false;
64133965Sjdp
64233965Sjdp    return new;
64333965Sjdp  }
64433965Sjdp}
64533965Sjdp
64677298Sobrienstatic lang_memory_region_type *
64738889Sjdplang_memory_default (section)
64838889Sjdp     asection *section;
64938889Sjdp{
65038889Sjdp  lang_memory_region_type *p;
65138889Sjdp
65238889Sjdp  flagword sec_flags = section->flags;
65338889Sjdp
65438889Sjdp  /* Override SEC_DATA to mean a writable section.  */
65538889Sjdp  if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
65638889Sjdp    sec_flags |= SEC_DATA;
65738889Sjdp
65838889Sjdp  for (p = lang_memory_region_list;
65938889Sjdp       p != (lang_memory_region_type *) NULL;
66038889Sjdp       p = p->next)
66138889Sjdp    {
66238889Sjdp      if ((p->flags & sec_flags) != 0
66338889Sjdp	  && (p->not_flags & sec_flags) == 0)
66438889Sjdp	{
66538889Sjdp	  return p;
66638889Sjdp	}
66738889Sjdp    }
66838889Sjdp  return lang_memory_region_lookup ("*default*");
66938889Sjdp}
67038889Sjdp
67133965Sjdplang_output_section_statement_type *
67233965Sjdplang_output_section_find (name)
67377298Sobrien     const char *const name;
67433965Sjdp{
67533965Sjdp  lang_statement_union_type *u;
67633965Sjdp  lang_output_section_statement_type *lookup;
67733965Sjdp
67833965Sjdp  for (u = lang_output_section_statement.head;
67933965Sjdp       u != (lang_statement_union_type *) NULL;
68033965Sjdp       u = lookup->next)
68133965Sjdp    {
68233965Sjdp      lookup = &u->output_section_statement;
68333965Sjdp      if (strcmp (name, lookup->name) == 0)
68433965Sjdp	{
68533965Sjdp	  return lookup;
68633965Sjdp	}
68733965Sjdp    }
68833965Sjdp  return (lang_output_section_statement_type *) NULL;
68933965Sjdp}
69033965Sjdp
69133965Sjdplang_output_section_statement_type *
69233965Sjdplang_output_section_statement_lookup (name)
69377298Sobrien     const char *const name;
69433965Sjdp{
69533965Sjdp  lang_output_section_statement_type *lookup;
69633965Sjdp
69733965Sjdp  lookup = lang_output_section_find (name);
69833965Sjdp  if (lookup == (lang_output_section_statement_type *) NULL)
69933965Sjdp    {
70033965Sjdp
70133965Sjdp      lookup = (lang_output_section_statement_type *)
70233965Sjdp	new_stat (lang_output_section_statement, stat_ptr);
70333965Sjdp      lookup->region = (lang_memory_region_type *) NULL;
70460484Sobrien      lookup->lma_region = (lang_memory_region_type *) NULL;
70533965Sjdp      lookup->fill = 0;
70633965Sjdp      lookup->block_value = 1;
70733965Sjdp      lookup->name = name;
70833965Sjdp
70933965Sjdp      lookup->next = (lang_statement_union_type *) NULL;
71033965Sjdp      lookup->bfd_section = (asection *) NULL;
71133965Sjdp      lookup->processed = false;
71233965Sjdp      lookup->sectype = normal_section;
71333965Sjdp      lookup->addr_tree = (etree_type *) NULL;
71433965Sjdp      lang_list_init (&lookup->children);
71533965Sjdp
71677298Sobrien      lookup->memspec = (const char *) NULL;
71733965Sjdp      lookup->flags = 0;
71833965Sjdp      lookup->subsection_alignment = -1;
71933965Sjdp      lookup->section_alignment = -1;
72033965Sjdp      lookup->load_base = (union etree_union *) NULL;
72133965Sjdp      lookup->phdrs = NULL;
72233965Sjdp
72333965Sjdp      lang_statement_append (&lang_output_section_statement,
72433965Sjdp			     (lang_statement_union_type *) lookup,
72533965Sjdp			     &lookup->next);
72633965Sjdp    }
72733965Sjdp  return lookup;
72833965Sjdp}
72933965Sjdp
73038889Sjdpstatic void
73138889Sjdplang_map_flags (flag)
73238889Sjdp     flagword flag;
73338889Sjdp{
73438889Sjdp  if (flag & SEC_ALLOC)
73538889Sjdp    minfo ("a");
73638889Sjdp
73738889Sjdp  if (flag & SEC_CODE)
73838889Sjdp    minfo ("x");
73938889Sjdp
74038889Sjdp  if (flag & SEC_READONLY)
74138889Sjdp    minfo ("r");
74238889Sjdp
74338889Sjdp  if (flag & SEC_DATA)
74438889Sjdp    minfo ("w");
74538889Sjdp
74638889Sjdp  if (flag & SEC_LOAD)
74738889Sjdp    minfo ("l");
74838889Sjdp}
74938889Sjdp
75033965Sjdpvoid
75133965Sjdplang_map ()
75233965Sjdp{
75333965Sjdp  lang_memory_region_type *m;
75433965Sjdp
75560484Sobrien  minfo (_("\nMemory Configuration\n\n"));
75638889Sjdp  fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
75760484Sobrien	   _("Name"), _("Origin"), _("Length"), _("Attributes"));
75833965Sjdp
75933965Sjdp  for (m = lang_memory_region_list;
76033965Sjdp       m != (lang_memory_region_type *) NULL;
76133965Sjdp       m = m->next)
76233965Sjdp    {
76333965Sjdp      char buf[100];
76433965Sjdp      int len;
76533965Sjdp
76633965Sjdp      fprintf (config.map_file, "%-16s ", m->name);
76733965Sjdp
76833965Sjdp      sprintf_vma (buf, m->origin);
76933965Sjdp      minfo ("0x%s ", buf);
77033965Sjdp      len = strlen (buf);
77133965Sjdp      while (len < 16)
77233965Sjdp	{
77333965Sjdp	  print_space ();
77433965Sjdp	  ++len;
77533965Sjdp	}
77633965Sjdp
77738889Sjdp      minfo ("0x%V", m->length);
77838889Sjdp      if (m->flags || m->not_flags)
77938889Sjdp	{
78038889Sjdp#ifndef BFD64
78138889Sjdp	  minfo ("        ");
78238889Sjdp#endif
78338889Sjdp	  if (m->flags)
78438889Sjdp	    {
78538889Sjdp	      print_space ();
78638889Sjdp	      lang_map_flags (m->flags);
78738889Sjdp	    }
78838889Sjdp
78938889Sjdp	  if (m->not_flags)
79038889Sjdp	    {
79138889Sjdp	      minfo (" !");
79238889Sjdp	      lang_map_flags (m->not_flags);
79338889Sjdp	    }
79438889Sjdp	}
79538889Sjdp
79638889Sjdp      print_nl ();
79733965Sjdp    }
79833965Sjdp
79960484Sobrien  fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
80033965Sjdp
80133965Sjdp  print_statements ();
80233965Sjdp}
80333965Sjdp
80433965Sjdp/* Initialize an output section.  */
80533965Sjdp
80633965Sjdpstatic void
80733965Sjdpinit_os (s)
80833965Sjdp     lang_output_section_statement_type *s;
80933965Sjdp{
81033965Sjdp  section_userdata_type *new;
81133965Sjdp
81233965Sjdp  if (s->bfd_section != NULL)
81333965Sjdp    return;
81433965Sjdp
81533965Sjdp  if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
81660484Sobrien    einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
81733965Sjdp
81833965Sjdp  new = ((section_userdata_type *)
81933965Sjdp	 stat_alloc (sizeof (section_userdata_type)));
82033965Sjdp
82133965Sjdp  s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
82233965Sjdp  if (s->bfd_section == (asection *) NULL)
82333965Sjdp    s->bfd_section = bfd_make_section (output_bfd, s->name);
82433965Sjdp  if (s->bfd_section == (asection *) NULL)
82533965Sjdp    {
82660484Sobrien      einfo (_("%P%F: output format %s cannot represent section called %s\n"),
82733965Sjdp	     output_bfd->xvec->name, s->name);
82833965Sjdp    }
82933965Sjdp  s->bfd_section->output_section = s->bfd_section;
83033965Sjdp
83177298Sobrien  /* We initialize an output sections output offset to minus its own
83277298Sobrien     vma to allow us to output a section through itself.  */
83333965Sjdp  s->bfd_section->output_offset = 0;
83433965Sjdp  get_userdata (s->bfd_section) = (PTR) new;
83533965Sjdp
83633965Sjdp  /* If there is a base address, make sure that any sections it might
83733965Sjdp     mention are initialized.  */
83833965Sjdp  if (s->addr_tree != NULL)
83933965Sjdp    exp_init_os (s->addr_tree);
84033965Sjdp}
84133965Sjdp
84233965Sjdp/* Make sure that all output sections mentioned in an expression are
84333965Sjdp   initialized.  */
84433965Sjdp
84533965Sjdpstatic void
84633965Sjdpexp_init_os (exp)
84733965Sjdp     etree_type *exp;
84833965Sjdp{
84933965Sjdp  switch (exp->type.node_class)
85033965Sjdp    {
85133965Sjdp    case etree_assign:
85233965Sjdp      exp_init_os (exp->assign.src);
85333965Sjdp      break;
85433965Sjdp
85533965Sjdp    case etree_binary:
85633965Sjdp      exp_init_os (exp->binary.lhs);
85733965Sjdp      exp_init_os (exp->binary.rhs);
85833965Sjdp      break;
85933965Sjdp
86033965Sjdp    case etree_trinary:
86133965Sjdp      exp_init_os (exp->trinary.cond);
86233965Sjdp      exp_init_os (exp->trinary.lhs);
86333965Sjdp      exp_init_os (exp->trinary.rhs);
86433965Sjdp      break;
86533965Sjdp
86633965Sjdp    case etree_unary:
86733965Sjdp      exp_init_os (exp->unary.child);
86833965Sjdp      break;
86933965Sjdp
87033965Sjdp    case etree_name:
87133965Sjdp      switch (exp->type.node_code)
87233965Sjdp	{
87333965Sjdp	case ADDR:
87433965Sjdp	case LOADADDR:
87533965Sjdp	case SIZEOF:
87633965Sjdp	  {
87733965Sjdp	    lang_output_section_statement_type *os;
87833965Sjdp
87933965Sjdp	    os = lang_output_section_find (exp->name.name);
88033965Sjdp	    if (os != NULL && os->bfd_section == NULL)
88133965Sjdp	      init_os (os);
88233965Sjdp	  }
88333965Sjdp	}
88433965Sjdp      break;
88533965Sjdp
88633965Sjdp    default:
88733965Sjdp      break;
88833965Sjdp    }
88933965Sjdp}
89060484Sobrien
89133965Sjdp/* Sections marked with the SEC_LINK_ONCE flag should only be linked
89260484Sobrien   once into the output.  This routine checks each section, and
89360484Sobrien   arrange to discard it if a section of the same name has already
89460484Sobrien   been linked.  If the section has COMDAT information, then it uses
89560484Sobrien   that to decide whether the section should be included.  This code
89660484Sobrien   assumes that all relevant sections have the SEC_LINK_ONCE flag set;
89760484Sobrien   that is, it does not depend solely upon the section name.
89860484Sobrien   section_already_linked is called via bfd_map_over_sections.  */
89933965Sjdp
90060484Sobrien/* This is the shape of the elements inside the already_linked hash
90160484Sobrien   table. It maps a name onto a list of already_linked elements with
90260484Sobrien   the same name.  It's possible to get more than one element in a
90360484Sobrien   list if the COMDAT sections have different names.  */
90460484Sobrien
90577298Sobrienstruct already_linked_hash_entry
90660484Sobrien{
90760484Sobrien  struct bfd_hash_entry root;
90860484Sobrien  struct already_linked *entry;
90960484Sobrien};
91060484Sobrien
91177298Sobrienstruct already_linked
91260484Sobrien{
91360484Sobrien  struct already_linked *next;
91460484Sobrien  asection *sec;
91560484Sobrien};
91660484Sobrien
91760484Sobrien/* The hash table.  */
91860484Sobrien
91960484Sobrienstatic struct bfd_hash_table already_linked_table;
92060484Sobrien
92133965Sjdpstatic void
92233965Sjdpsection_already_linked (abfd, sec, data)
92333965Sjdp     bfd *abfd;
92433965Sjdp     asection *sec;
92533965Sjdp     PTR data;
92633965Sjdp{
92733965Sjdp  lang_input_statement_type *entry = (lang_input_statement_type *) data;
92833965Sjdp  flagword flags;
92933965Sjdp  const char *name;
93060484Sobrien  struct already_linked *l;
93160484Sobrien  struct already_linked_hash_entry *already_linked_list;
93233965Sjdp
93333965Sjdp  /* If we are only reading symbols from this object, then we want to
93433965Sjdp     discard all sections.  */
93533965Sjdp  if (entry->just_syms_flag)
93633965Sjdp    {
93733965Sjdp      sec->output_section = bfd_abs_section_ptr;
93833965Sjdp      sec->output_offset = sec->vma;
93933965Sjdp      return;
94033965Sjdp    }
94133965Sjdp
94233965Sjdp  flags = bfd_get_section_flags (abfd, sec);
94333965Sjdp
94433965Sjdp  if ((flags & SEC_LINK_ONCE) == 0)
94533965Sjdp    return;
94633965Sjdp
94777298Sobrien  /* FIXME: When doing a relocatable link, we may have trouble
94860484Sobrien     copying relocations in other sections that refer to local symbols
94960484Sobrien     in the section being discarded.  Those relocations will have to
95060484Sobrien     be converted somehow; as of this writing I'm not sure that any of
95160484Sobrien     the backends handle that correctly.
95260484Sobrien
95360484Sobrien     It is tempting to instead not discard link once sections when
95477298Sobrien     doing a relocatable link (technically, they should be discarded
95560484Sobrien     whenever we are building constructors).  However, that fails,
95660484Sobrien     because the linker winds up combining all the link once sections
95760484Sobrien     into a single large link once section, which defeats the purpose
95860484Sobrien     of having link once sections in the first place.
95960484Sobrien
96077298Sobrien     Also, not merging link once sections in a relocatable link
96160484Sobrien     causes trouble for MIPS ELF, which relies in link once semantics
96260484Sobrien     to handle the .reginfo section correctly.  */
96360484Sobrien
96433965Sjdp  name = bfd_get_section_name (abfd, sec);
96533965Sjdp
96677298Sobrien  already_linked_list =
96760484Sobrien    ((struct already_linked_hash_entry *)
96860484Sobrien     bfd_hash_lookup (&already_linked_table, name, true, false));
96960484Sobrien
97077298Sobrien  for (l = already_linked_list->entry; l != NULL; l = l->next)
97133965Sjdp    {
97260484Sobrien      if (sec->comdat == NULL
97360484Sobrien	  || l->sec->comdat == NULL
97460484Sobrien	  || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
97533965Sjdp	{
97633965Sjdp	  /* The section has already been linked.  See if we should
97733965Sjdp             issue a warning.  */
97833965Sjdp	  switch (flags & SEC_LINK_DUPLICATES)
97933965Sjdp	    {
98033965Sjdp	    default:
98133965Sjdp	      abort ();
98233965Sjdp
98333965Sjdp	    case SEC_LINK_DUPLICATES_DISCARD:
98433965Sjdp	      break;
98533965Sjdp
98633965Sjdp	    case SEC_LINK_DUPLICATES_ONE_ONLY:
98760484Sobrien	      if (sec->comdat == NULL)
98860484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
98960484Sobrien		       abfd, name);
99060484Sobrien	      else
99160484Sobrien		einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
99260484Sobrien		       abfd, name, sec->comdat->name);
99333965Sjdp	      break;
99433965Sjdp
99533965Sjdp	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
99633965Sjdp	      /* FIXME: We should really dig out the contents of both
99733965Sjdp                 sections and memcmp them.  The COFF/PE spec says that
99833965Sjdp                 the Microsoft linker does not implement this
99933965Sjdp                 correctly, so I'm not going to bother doing it
100033965Sjdp                 either.  */
100133965Sjdp	      /* Fall through.  */
100233965Sjdp	    case SEC_LINK_DUPLICATES_SAME_SIZE:
100333965Sjdp	      if (bfd_section_size (abfd, sec)
100433965Sjdp		  != bfd_section_size (l->sec->owner, l->sec))
100560484Sobrien		einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
100633965Sjdp		       abfd, name);
100733965Sjdp	      break;
100833965Sjdp	    }
100933965Sjdp
101033965Sjdp	  /* Set the output_section field so that wild_doit does not
101160484Sobrien	     create a lang_input_section structure for this section.
101260484Sobrien	     Since there might be a symbol in the section being
101360484Sobrien	     discarded, we must retain a pointer to the section which
101460484Sobrien	     we are really going to use.  */
101533965Sjdp	  sec->output_section = bfd_abs_section_ptr;
101677298Sobrien	  sec->kept_section = l->sec;
101733965Sjdp
101833965Sjdp	  return;
101933965Sjdp	}
102033965Sjdp    }
102133965Sjdp
102260484Sobrien  /* This is the first section with this name.  Record it.  Allocate
102360484Sobrien     the memory from the same obstack as the hash table is kept in.  */
102433965Sjdp
102577298Sobrien  l = ((struct already_linked *)
102660484Sobrien       bfd_hash_allocate (&already_linked_table, sizeof *l));
102760484Sobrien
102833965Sjdp  l->sec = sec;
102960484Sobrien  l->next = already_linked_list->entry;
103060484Sobrien  already_linked_list->entry = l;
103133965Sjdp}
103260484Sobrien
103360484Sobrien/* Support routines for the hash table used by section_already_linked,
103460484Sobrien   initialize the table, fill in an entry and remove the table.  */
103560484Sobrien
103660484Sobrienstatic struct bfd_hash_entry *
103760484Sobrienalready_linked_newfunc (entry, table, string)
103860484Sobrien     struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
103960484Sobrien     struct bfd_hash_table *table;
104060484Sobrien     const char *string ATTRIBUTE_UNUSED;
104160484Sobrien{
104277298Sobrien  struct already_linked_hash_entry *ret =
104360484Sobrien    bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
104460484Sobrien
104560484Sobrien  ret->entry = NULL;
104660484Sobrien
104760484Sobrien  return (struct bfd_hash_entry *) ret;
104860484Sobrien}
104960484Sobrien
105060484Sobrienstatic void
105160484Sobrienalready_linked_table_init ()
105260484Sobrien{
105360484Sobrien  if (! bfd_hash_table_init_n (&already_linked_table,
105460484Sobrien			       already_linked_newfunc,
105560484Sobrien			       42))
105660484Sobrien    einfo (_("%P%F: Failed to create hash table\n"));
105760484Sobrien}
105860484Sobrien
105960484Sobrienstatic void
106060484Sobrienalready_linked_table_free ()
106160484Sobrien{
106260484Sobrien  bfd_hash_table_free (&already_linked_table);
106360484Sobrien}
106433965Sjdp
106533965Sjdp/* The wild routines.
106633965Sjdp
106733965Sjdp   These expand statements like *(.text) and foo.o to a list of
106833965Sjdp   explicit actions, like foo.o(.text), bar.o(.text) and
106933965Sjdp   foo.o(.text, .data).  */
107033965Sjdp
107138889Sjdp/* Return true if the PATTERN argument is a wildcard pattern.
107238889Sjdp   Although backslashes are treated specially if a pattern contains
107338889Sjdp   wildcards, we do not consider the mere presence of a backslash to
107438889Sjdp   be enough to cause the the pattern to be treated as a wildcard.
107538889Sjdp   That lets us handle DOS filenames more naturally.  */
107633965Sjdp
107733965Sjdpstatic boolean
107833965Sjdpwildcardp (pattern)
107933965Sjdp     const char *pattern;
108033965Sjdp{
108133965Sjdp  const char *s;
108233965Sjdp
108333965Sjdp  for (s = pattern; *s != '\0'; ++s)
108433965Sjdp    if (*s == '?'
108533965Sjdp	|| *s == '*'
108633965Sjdp	|| *s == '[')
108733965Sjdp      return true;
108833965Sjdp  return false;
108933965Sjdp}
109033965Sjdp
109133965Sjdp/* Add SECTION to the output section OUTPUT.  Do this by creating a
109233965Sjdp   lang_input_section statement which is placed at PTR.  FILE is the
109333965Sjdp   input file which holds SECTION.  */
109433965Sjdp
109533965Sjdpvoid
109633965Sjdpwild_doit (ptr, section, output, file)
109733965Sjdp     lang_statement_list_type *ptr;
109833965Sjdp     asection *section;
109933965Sjdp     lang_output_section_statement_type *output;
110033965Sjdp     lang_input_statement_type *file;
110133965Sjdp{
110233965Sjdp  flagword flags;
110333965Sjdp  boolean discard;
110433965Sjdp
110533965Sjdp  flags = bfd_get_section_flags (section->owner, section);
110633965Sjdp
110733965Sjdp  discard = false;
110833965Sjdp
110933965Sjdp  /* If we are doing a final link, discard sections marked with
111033965Sjdp     SEC_EXCLUDE.  */
111133965Sjdp  if (! link_info.relocateable
111233965Sjdp      && (flags & SEC_EXCLUDE) != 0)
111333965Sjdp    discard = true;
111433965Sjdp
111533965Sjdp  /* Discard input sections which are assigned to a section named
111633965Sjdp     DISCARD_SECTION_NAME.  */
111733965Sjdp  if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
111833965Sjdp    discard = true;
111933965Sjdp
112033965Sjdp  /* Discard debugging sections if we are stripping debugging
112133965Sjdp     information.  */
112233965Sjdp  if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
112333965Sjdp      && (flags & SEC_DEBUGGING) != 0)
112433965Sjdp    discard = true;
112533965Sjdp
112633965Sjdp  if (discard)
112733965Sjdp    {
112833965Sjdp      if (section->output_section == NULL)
112933965Sjdp	{
113033965Sjdp	  /* This prevents future calls from assigning this section.  */
113133965Sjdp	  section->output_section = bfd_abs_section_ptr;
113233965Sjdp	}
113333965Sjdp      return;
113433965Sjdp    }
113533965Sjdp
113633965Sjdp  if (section->output_section == NULL)
113733965Sjdp    {
113838889Sjdp      boolean first;
113933965Sjdp      lang_input_section_type *new;
114038889Sjdp      flagword flags;
114133965Sjdp
114233965Sjdp      if (output->bfd_section == NULL)
114378828Sobrien	init_os (output);
114433965Sjdp
114578828Sobrien      first = ! output->bfd_section->linker_has_input;
114678828Sobrien      output->bfd_section->linker_has_input = 1;
114778828Sobrien
114877298Sobrien      /* Add a section reference to the list.  */
114933965Sjdp      new = new_stat (lang_input_section, ptr);
115033965Sjdp
115133965Sjdp      new->section = section;
115233965Sjdp      new->ifile = file;
115333965Sjdp      section->output_section = output->bfd_section;
115433965Sjdp
115538889Sjdp      flags = section->flags;
115638889Sjdp
115733965Sjdp      /* We don't copy the SEC_NEVER_LOAD flag from an input section
115833965Sjdp	 to an output section, because we want to be able to include a
115933965Sjdp	 SEC_NEVER_LOAD section in the middle of an otherwise loaded
116033965Sjdp	 section (I don't know why we want to do this, but we do).
116133965Sjdp	 build_link_order in ldwrite.c handles this case by turning
116238889Sjdp	 the embedded SEC_NEVER_LOAD section into a fill.  */
116333965Sjdp
116438889Sjdp      flags &= ~ SEC_NEVER_LOAD;
116533965Sjdp
116638889Sjdp      /* If final link, don't copy the SEC_LINK_ONCE flags, they've
116738889Sjdp	 already been processed.  One reason to do this is that on pe
116838889Sjdp	 format targets, .text$foo sections go into .text and it's odd
116938889Sjdp	 to see .text with SEC_LINK_ONCE set.  */
117033965Sjdp
117138889Sjdp      if (! link_info.relocateable)
117238889Sjdp	flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
117338889Sjdp
117438889Sjdp      /* If this is not the first input section, and the SEC_READONLY
117538889Sjdp         flag is not currently set, then don't set it just because the
117638889Sjdp         input section has it set.  */
117738889Sjdp
117838889Sjdp      if (! first && (section->output_section->flags & SEC_READONLY) == 0)
117938889Sjdp	flags &= ~ SEC_READONLY;
118038889Sjdp
118138889Sjdp      section->output_section->flags |= flags;
118238889Sjdp
118338889Sjdp      /* If SEC_READONLY is not set in the input section, then clear
118438889Sjdp         it from the output section.  */
118538889Sjdp      if ((section->flags & SEC_READONLY) == 0)
118638889Sjdp	section->output_section->flags &= ~SEC_READONLY;
118738889Sjdp
118833965Sjdp      switch (output->sectype)
118933965Sjdp	{
119033965Sjdp	case normal_section:
119133965Sjdp	  break;
119233965Sjdp	case dsect_section:
119333965Sjdp	case copy_section:
119433965Sjdp	case info_section:
119533965Sjdp	case overlay_section:
119633965Sjdp	  output->bfd_section->flags &= ~SEC_ALLOC;
119733965Sjdp	  break;
119833965Sjdp	case noload_section:
119933965Sjdp	  output->bfd_section->flags &= ~SEC_LOAD;
120033965Sjdp	  output->bfd_section->flags |= SEC_NEVER_LOAD;
120133965Sjdp	  break;
120233965Sjdp	}
120333965Sjdp
120460484Sobrien      /* Copy over SEC_SMALL_DATA.  */
120560484Sobrien      if (section->flags & SEC_SMALL_DATA)
120660484Sobrien	section->output_section->flags |= SEC_SMALL_DATA;
120760484Sobrien
120833965Sjdp      if (section->alignment_power > output->bfd_section->alignment_power)
120933965Sjdp	output->bfd_section->alignment_power = section->alignment_power;
121033965Sjdp
121133965Sjdp      /* If supplied an aligment, then force it.  */
121233965Sjdp      if (output->section_alignment != -1)
121333965Sjdp	output->bfd_section->alignment_power = output->section_alignment;
121477298Sobrien
121577298Sobrien      if (section->flags & SEC_BLOCK)
121677298Sobrien	{
121777298Sobrien	  section->output_section->flags |= SEC_BLOCK;
121877298Sobrien	  /* FIXME: This value should really be obtained from the bfd...  */
121977298Sobrien	  output->block_value = 128;
122077298Sobrien	}
122133965Sjdp    }
122233965Sjdp}
122333965Sjdp
122460484Sobrien/* Handle wildcard sorting.  This returns the lang_input_section which
122560484Sobrien   should follow the one we are going to create for SECTION and FILE,
122660484Sobrien   based on the sorting requirements of WILD.  It returns NULL if the
122760484Sobrien   new section should just go at the end of the current list.  */
122833965Sjdp
122960484Sobrienstatic lang_statement_union_type *
123060484Sobrienwild_sort (wild, file, section)
123160484Sobrien     lang_wild_statement_type *wild;
123233965Sjdp     lang_input_statement_type *file;
123360484Sobrien     asection *section;
123433965Sjdp{
123560484Sobrien  const char *section_name;
123660484Sobrien  lang_statement_union_type *l;
123760484Sobrien
123860484Sobrien  if (! wild->filenames_sorted && ! wild->sections_sorted)
123960484Sobrien    return NULL;
124060484Sobrien
124160484Sobrien  section_name = bfd_get_section_name (file->the_bfd, section);
124260484Sobrien  for (l = wild->children.head; l != NULL; l = l->next)
124333965Sjdp    {
124460484Sobrien      lang_input_section_type *ls;
124533965Sjdp
124660484Sobrien      if (l->header.type != lang_input_section_enum)
124760484Sobrien	continue;
124860484Sobrien      ls = &l->input_section;
124933965Sjdp
125060484Sobrien      /* Sorting by filename takes precedence over sorting by section
125160484Sobrien         name.  */
125260484Sobrien
125360484Sobrien      if (wild->filenames_sorted)
125433965Sjdp	{
125560484Sobrien	  const char *fn, *ln;
125660484Sobrien	  boolean fa, la;
125760484Sobrien	  int i;
125833965Sjdp
125960484Sobrien	  /* The PE support for the .idata section as generated by
126060484Sobrien             dlltool assumes that files will be sorted by the name of
126160484Sobrien             the archive and then the name of the file within the
126260484Sobrien             archive.  */
126333965Sjdp
126460484Sobrien	  if (file->the_bfd != NULL
126560484Sobrien	      && bfd_my_archive (file->the_bfd) != NULL)
126660484Sobrien	    {
126760484Sobrien	      fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
126860484Sobrien	      fa = true;
126960484Sobrien	    }
127060484Sobrien	  else
127160484Sobrien	    {
127260484Sobrien	      fn = file->filename;
127360484Sobrien	      fa = false;
127460484Sobrien	    }
127533965Sjdp
127660484Sobrien	  if (ls->ifile->the_bfd != NULL
127760484Sobrien	      && bfd_my_archive (ls->ifile->the_bfd) != NULL)
127860484Sobrien	    {
127960484Sobrien	      ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
128060484Sobrien	      la = true;
128160484Sobrien	    }
128233965Sjdp	  else
128333965Sjdp	    {
128460484Sobrien	      ln = ls->ifile->filename;
128560484Sobrien	      la = false;
128660484Sobrien	    }
128733965Sjdp
128860484Sobrien	  i = strcmp (fn, ln);
128960484Sobrien	  if (i > 0)
129060484Sobrien	    continue;
129160484Sobrien	  else if (i < 0)
129260484Sobrien	    break;
129360484Sobrien
129460484Sobrien	  if (fa || la)
129560484Sobrien	    {
129660484Sobrien	      if (fa)
129760484Sobrien		fn = file->filename;
129860484Sobrien	      if (la)
129960484Sobrien		ln = ls->ifile->filename;
130060484Sobrien
130160484Sobrien	      i = strcmp (fn, ln);
130260484Sobrien	      if (i > 0)
130360484Sobrien		continue;
130460484Sobrien	      else if (i < 0)
130560484Sobrien		break;
130633965Sjdp	    }
130733965Sjdp	}
130860484Sobrien
130960484Sobrien      /* Here either the files are not sorted by name, or we are
131060484Sobrien         looking at the sections for this file.  */
131160484Sobrien
131260484Sobrien      if (wild->sections_sorted)
131360484Sobrien	{
131460484Sobrien	  if (strcmp (section_name,
131560484Sobrien		      bfd_get_section_name (ls->ifile->the_bfd,
131660484Sobrien					    ls->section))
131760484Sobrien	      < 0)
131860484Sobrien	    break;
131960484Sobrien	}
132033965Sjdp    }
132160484Sobrien
132260484Sobrien  return l;
132333965Sjdp}
132433965Sjdp
132560484Sobrien/* Expand a wild statement for a particular FILE.  SECTION may be
132660484Sobrien   NULL, in which case it is a wild card.  */
132760484Sobrien
132860484Sobrienstatic void
132960484Sobrienoutput_section_callback (ptr, section, file, output)
133060484Sobrien     lang_wild_statement_type *ptr;
133160484Sobrien     asection *section;
133260484Sobrien     lang_input_statement_type *file;
133377298Sobrien     PTR output;
133460484Sobrien{
133560484Sobrien  lang_statement_union_type *before;
133677298Sobrien
133760484Sobrien  /* If the wild pattern was marked KEEP, the member sections
133860484Sobrien     should be as well.  */
133960484Sobrien  if (ptr->keep_sections)
134060484Sobrien    section->flags |= SEC_KEEP;
134177298Sobrien
134260484Sobrien  before = wild_sort (ptr, file, section);
134377298Sobrien
134460484Sobrien  /* Here BEFORE points to the lang_input_section which
134560484Sobrien     should follow the one we are about to add.  If BEFORE
134660484Sobrien     is NULL, then the section should just go at the end
134760484Sobrien     of the current list.  */
134877298Sobrien
134960484Sobrien  if (before == NULL)
135077298Sobrien    wild_doit (&ptr->children, section,
135177298Sobrien	       (lang_output_section_statement_type *) output,
135260484Sobrien	       file);
135360484Sobrien  else
135460484Sobrien    {
135560484Sobrien      lang_statement_list_type list;
135660484Sobrien      lang_statement_union_type **pp;
135777298Sobrien
135860484Sobrien      lang_list_init (&list);
135977298Sobrien      wild_doit (&list, section,
136077298Sobrien		 (lang_output_section_statement_type *) output,
136160484Sobrien		 file);
136277298Sobrien
136360484Sobrien      /* If we are discarding the section, LIST.HEAD will
136460484Sobrien	 be NULL.  */
136560484Sobrien      if (list.head != NULL)
136660484Sobrien	{
136760484Sobrien	  ASSERT (list.head->next == NULL);
136877298Sobrien
136960484Sobrien	  for (pp = &ptr->children.head;
137060484Sobrien	       *pp != before;
137160484Sobrien	       pp = &(*pp)->next)
137260484Sobrien	    ASSERT (*pp != NULL);
137377298Sobrien
137460484Sobrien	  list.head->next = *pp;
137560484Sobrien	  *pp = list.head;
137660484Sobrien	}
137760484Sobrien    }
137860484Sobrien}
137960484Sobrien
138033965Sjdp/* This is passed a file name which must have been seen already and
138133965Sjdp   added to the statement tree.  We will see if it has been opened
138233965Sjdp   already and had its symbols read.  If not then we'll read it.  */
138333965Sjdp
138433965Sjdpstatic lang_input_statement_type *
138533965Sjdplookup_name (name)
138633965Sjdp     const char *name;
138733965Sjdp{
138833965Sjdp  lang_input_statement_type *search;
138933965Sjdp
139033965Sjdp  for (search = (lang_input_statement_type *) input_file_chain.head;
139133965Sjdp       search != (lang_input_statement_type *) NULL;
139233965Sjdp       search = (lang_input_statement_type *) search->next_real_file)
139333965Sjdp    {
139433965Sjdp      if (search->filename == (char *) NULL && name == (char *) NULL)
139533965Sjdp	return search;
139633965Sjdp      if (search->filename != (char *) NULL
139733965Sjdp	  && name != (char *) NULL
139833965Sjdp	  && strcmp (search->filename, name) == 0)
139933965Sjdp	break;
140033965Sjdp    }
140133965Sjdp
140233965Sjdp  if (search == (lang_input_statement_type *) NULL)
140333965Sjdp    search = new_afile (name, lang_input_file_is_file_enum, default_target,
140433965Sjdp			false);
140533965Sjdp
140633965Sjdp  /* If we have already added this file, or this file is not real
140733965Sjdp     (FIXME: can that ever actually happen?) or the name is NULL
140833965Sjdp     (FIXME: can that ever actually happen?) don't add this file.  */
140933965Sjdp  if (search->loaded
141033965Sjdp      || ! search->real
141133965Sjdp      || search->filename == (const char *) NULL)
141233965Sjdp    return search;
141333965Sjdp
141433965Sjdp  load_symbols (search, (lang_statement_list_type *) NULL);
141533965Sjdp
141633965Sjdp  return search;
141733965Sjdp}
141833965Sjdp
141933965Sjdp/* Get the symbols for an input file.  */
142033965Sjdp
142133965Sjdpstatic void
142233965Sjdpload_symbols (entry, place)
142333965Sjdp     lang_input_statement_type *entry;
142433965Sjdp     lang_statement_list_type *place;
142533965Sjdp{
142633965Sjdp  char **matching;
142733965Sjdp
142833965Sjdp  if (entry->loaded)
142933965Sjdp    return;
143033965Sjdp
143133965Sjdp  ldfile_open_file (entry);
143233965Sjdp
143333965Sjdp  if (! bfd_check_format (entry->the_bfd, bfd_archive)
143433965Sjdp      && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
143533965Sjdp    {
143633965Sjdp      bfd_error_type err;
143733965Sjdp      lang_statement_list_type *hold;
143833965Sjdp
143933965Sjdp      err = bfd_get_error ();
144078828Sobrien
144178828Sobrien      /* See if the emulation has some special knowledge.  */
144278828Sobrien      if (ldemul_unrecognized_file (entry))
144378828Sobrien	return;
144478828Sobrien
144533965Sjdp      if (err == bfd_error_file_ambiguously_recognized)
144633965Sjdp	{
144733965Sjdp	  char **p;
144833965Sjdp
144960484Sobrien	  einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
145060484Sobrien	  einfo (_("%B: matching formats:"), entry->the_bfd);
145133965Sjdp	  for (p = matching; *p != NULL; p++)
145233965Sjdp	    einfo (" %s", *p);
145333965Sjdp	  einfo ("%F\n");
145433965Sjdp	}
145533965Sjdp      else if (err != bfd_error_file_not_recognized
145633965Sjdp	       || place == NULL)
145760484Sobrien	einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
145833965Sjdp
145933965Sjdp      bfd_close (entry->the_bfd);
146033965Sjdp      entry->the_bfd = NULL;
146133965Sjdp
146233965Sjdp      /* Try to interpret the file as a linker script.  */
146333965Sjdp      ldfile_open_command_file (entry->filename);
146433965Sjdp
146533965Sjdp      hold = stat_ptr;
146633965Sjdp      stat_ptr = place;
146733965Sjdp
146833965Sjdp      ldfile_assumed_script = true;
146933965Sjdp      parser_input = input_script;
147033965Sjdp      yyparse ();
147133965Sjdp      ldfile_assumed_script = false;
147233965Sjdp
147333965Sjdp      stat_ptr = hold;
147433965Sjdp
147533965Sjdp      return;
147633965Sjdp    }
147733965Sjdp
147860484Sobrien  if (ldemul_recognized_file (entry))
147960484Sobrien    return;
148060484Sobrien
148133965Sjdp  /* We don't call ldlang_add_file for an archive.  Instead, the
148233965Sjdp     add_symbols entry point will call ldlang_add_file, via the
148333965Sjdp     add_archive_element callback, for each element of the archive
148433965Sjdp     which is used.  */
148533965Sjdp  switch (bfd_get_format (entry->the_bfd))
148633965Sjdp    {
148733965Sjdp    default:
148833965Sjdp      break;
148933965Sjdp
149033965Sjdp    case bfd_object:
149133965Sjdp      ldlang_add_file (entry);
149233965Sjdp      if (trace_files || trace_file_tries)
149333965Sjdp	info_msg ("%I\n", entry);
149433965Sjdp      break;
149533965Sjdp
149633965Sjdp    case bfd_archive:
149733965Sjdp      if (entry->whole_archive)
149833965Sjdp	{
149933965Sjdp	  bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
150033965Sjdp						      (bfd *) NULL);
150133965Sjdp	  while (member != NULL)
150233965Sjdp	    {
150333965Sjdp	      if (! bfd_check_format (member, bfd_object))
150460484Sobrien		einfo (_("%F%B: object %B in archive is not object\n"),
150533965Sjdp		       entry->the_bfd, member);
150633965Sjdp	      if (! ((*link_info.callbacks->add_archive_element)
150733965Sjdp		     (&link_info, member, "--whole-archive")))
150833965Sjdp		abort ();
150933965Sjdp	      if (! bfd_link_add_symbols (member, &link_info))
151060484Sobrien		einfo (_("%F%B: could not read symbols: %E\n"), member);
151133965Sjdp	      member = bfd_openr_next_archived_file (entry->the_bfd,
151233965Sjdp						     member);
151333965Sjdp	    }
151433965Sjdp
151533965Sjdp	  entry->loaded = true;
151633965Sjdp
151733965Sjdp	  return;
151833965Sjdp	}
151933965Sjdp    }
152033965Sjdp
152133965Sjdp  if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
152260484Sobrien    einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
152333965Sjdp
152433965Sjdp  entry->loaded = true;
152533965Sjdp}
152633965Sjdp
152733965Sjdp/* Handle a wild statement.  SECTION or FILE or both may be NULL,
152833965Sjdp   indicating that it is a wildcard.  Separate lang_input_section
152933965Sjdp   statements are created for each part of the expansion; they are
153033965Sjdp   added after the wild statement S.  OUTPUT is the output section.  */
153133965Sjdp
153233965Sjdpstatic void
153333965Sjdpwild (s, section, file, target, output)
153433965Sjdp     lang_wild_statement_type *s;
153533965Sjdp     const char *section;
153633965Sjdp     const char *file;
153760484Sobrien     const char *target ATTRIBUTE_UNUSED;
153833965Sjdp     lang_output_section_statement_type *output;
153933965Sjdp{
154077298Sobrien  walk_wild (s, section, file, output_section_callback, (PTR) output);
154133965Sjdp
154260484Sobrien  if (section != (char *) NULL
154360484Sobrien      && strcmp (section, "COMMON") == 0
154460484Sobrien      && default_common_section == NULL)
154533965Sjdp    {
154660484Sobrien      /* Remember the section that common is going to in case we later
154760484Sobrien         get something which doesn't know where to put it.  */
154860484Sobrien      default_common_section = output;
154933965Sjdp    }
155060484Sobrien}
155160484Sobrien
155260484Sobrien/* Return true iff target is the sought target.  */
155377298Sobrien
155460484Sobrienstatic int
155560484Sobrienget_target (target, data)
155677298Sobrien     const bfd_target *target;
155777298Sobrien     PTR data;
155860484Sobrien{
155977298Sobrien  const char *sought = (const char *) data;
156077298Sobrien
156160484Sobrien  return strcmp (target->name, sought) == 0;
156260484Sobrien}
156360484Sobrien
156460484Sobrien/* Like strcpy() but convert to lower case as well.  */
156577298Sobrien
156660484Sobrienstatic void
156760484Sobrienstricpy (dest, src)
156877298Sobrien     char *dest;
156977298Sobrien     char *src;
157060484Sobrien{
157160484Sobrien  char c;
157277298Sobrien
157377298Sobrien  while ((c = *src++) != 0)
157433965Sjdp    {
157560484Sobrien      if (isupper ((unsigned char) c))
157660484Sobrien	c = tolower (c);
157760484Sobrien
157877298Sobrien      *dest++ = c;
157933965Sjdp    }
158060484Sobrien
158177298Sobrien  *dest = 0;
158260484Sobrien}
158360484Sobrien
158460484Sobrien/* Remove the first occurance of needle (if any) in haystack
158560484Sobrien   from haystack.  */
158677298Sobrien
158760484Sobrienstatic void
158860484Sobrienstrcut (haystack, needle)
158977298Sobrien     char *haystack;
159077298Sobrien     char *needle;
159160484Sobrien{
159260484Sobrien  haystack = strstr (haystack, needle);
159377298Sobrien
159460484Sobrien  if (haystack)
159533965Sjdp    {
159677298Sobrien      char *src;
159760484Sobrien
159877298Sobrien      for (src = haystack + strlen (needle); *src;)
159977298Sobrien	*haystack++ = *src++;
160077298Sobrien
160177298Sobrien      *haystack = 0;
160233965Sjdp    }
160360484Sobrien}
160433965Sjdp
160560484Sobrien/* Compare two target format name strings.
160660484Sobrien   Return a value indicating how "similar" they are.  */
160777298Sobrien
160860484Sobrienstatic int
160960484Sobrienname_compare (first, second)
161077298Sobrien     char *first;
161177298Sobrien     char *second;
161260484Sobrien{
161377298Sobrien  char *copy1;
161477298Sobrien  char *copy2;
161577298Sobrien  int result;
161677298Sobrien
161760484Sobrien  copy1 = xmalloc (strlen (first) + 1);
161860484Sobrien  copy2 = xmalloc (strlen (second) + 1);
161960484Sobrien
162060484Sobrien  /* Convert the names to lower case.  */
162160484Sobrien  stricpy (copy1, first);
162260484Sobrien  stricpy (copy2, second);
162360484Sobrien
162460484Sobrien  /* Remove and endian strings from the name.  */
162560484Sobrien  strcut (copy1, "big");
162660484Sobrien  strcut (copy1, "little");
162760484Sobrien  strcut (copy2, "big");
162860484Sobrien  strcut (copy2, "little");
162960484Sobrien
163060484Sobrien  /* Return a value based on how many characters match,
163160484Sobrien     starting from the beginning.   If both strings are
163260484Sobrien     the same then return 10 * their length.  */
163377298Sobrien  for (result = 0; copy1[result] == copy2[result]; result++)
163477298Sobrien    if (copy1[result] == 0)
163560484Sobrien      {
163660484Sobrien	result *= 10;
163760484Sobrien	break;
163860484Sobrien      }
163977298Sobrien
164060484Sobrien  free (copy1);
164160484Sobrien  free (copy2);
164260484Sobrien
164360484Sobrien  return result;
164460484Sobrien}
164560484Sobrien
164660484Sobrien/* Set by closest_target_match() below.  */
164777298Sobrienstatic const bfd_target *winner;
164860484Sobrien
164960484Sobrien/* Scan all the valid bfd targets looking for one that has the endianness
165060484Sobrien   requirement that was specified on the command line, and is the nearest
165160484Sobrien   match to the original output target.  */
165277298Sobrien
165360484Sobrienstatic int
165460484Sobrienclosest_target_match (target, data)
165577298Sobrien     const bfd_target *target;
165677298Sobrien     PTR data;
165760484Sobrien{
165877298Sobrien  const bfd_target *original = (const bfd_target *) data;
165977298Sobrien
166077298Sobrien  if (command_line.endian == ENDIAN_BIG
166177298Sobrien      && target->byteorder != BFD_ENDIAN_BIG)
166260484Sobrien    return 0;
166377298Sobrien
166477298Sobrien  if (command_line.endian == ENDIAN_LITTLE
166577298Sobrien      && target->byteorder != BFD_ENDIAN_LITTLE)
166660484Sobrien    return 0;
166760484Sobrien
166860484Sobrien  /* Must be the same flavour.  */
166960484Sobrien  if (target->flavour != original->flavour)
167060484Sobrien    return 0;
167160484Sobrien
167260484Sobrien  /* If we have not found a potential winner yet, then record this one.  */
167360484Sobrien  if (winner == NULL)
167433965Sjdp    {
167560484Sobrien      winner = target;
167660484Sobrien      return 0;
167733965Sjdp    }
167860484Sobrien
167960484Sobrien  /* Oh dear, we now have two potential candidates for a successful match.
168077298Sobrien     Compare their names and choose the better one.  */
168178828Sobrien  if (name_compare (target->name, original->name)
168278828Sobrien      > name_compare (winner->name, original->name))
168360484Sobrien    winner = target;
168460484Sobrien
168560484Sobrien  /* Keep on searching until wqe have checked them all.  */
168660484Sobrien  return 0;
168733965Sjdp}
168833965Sjdp
168960484Sobrien/* Return the BFD target format of the first input file.  */
169077298Sobrien
169160484Sobrienstatic char *
169260484Sobrienget_first_input_target ()
169360484Sobrien{
169477298Sobrien  char *target = NULL;
169560484Sobrien
169660484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (s)
169760484Sobrien    {
169860484Sobrien      if (s->header.type == lang_input_statement_enum
169960484Sobrien	  && s->real)
170060484Sobrien	{
170160484Sobrien	  ldfile_open_file (s);
170277298Sobrien
170360484Sobrien	  if (s->the_bfd != NULL
170460484Sobrien	      && bfd_check_format (s->the_bfd, bfd_object))
170560484Sobrien	    {
170660484Sobrien	      target = bfd_get_target (s->the_bfd);
170777298Sobrien
170860484Sobrien	      if (target != NULL)
170960484Sobrien		break;
171060484Sobrien	    }
171160484Sobrien	}
171260484Sobrien    }
171377298Sobrien
171460484Sobrien  return target;
171560484Sobrien}
171660484Sobrien
171733965Sjdp/* Open the output file.  */
171833965Sjdp
171933965Sjdpstatic bfd *
172033965Sjdpopen_output (name)
172177298Sobrien     const char *name;
172233965Sjdp{
172377298Sobrien  bfd *output;
172433965Sjdp
172577298Sobrien  /* Has the user told us which output format to use?  */
172633965Sjdp  if (output_target == (char *) NULL)
172733965Sjdp    {
172877298Sobrien      /* No - has the current target been set to something other than
172977298Sobrien         the default?  */
173060484Sobrien      if (current_target != default_target)
173133965Sjdp	output_target = current_target;
173260484Sobrien
173377298Sobrien      /* No - can we determine the format of the first input file?  */
173433965Sjdp      else
173560484Sobrien	{
173660484Sobrien	  output_target = get_first_input_target ();
173760484Sobrien
173860484Sobrien	  /* Failed - use the default output target.  */
173960484Sobrien	  if (output_target == NULL)
174060484Sobrien	    output_target = default_target;
174160484Sobrien	}
174233965Sjdp    }
174377298Sobrien
174477298Sobrien  /* Has the user requested a particular endianness on the command
174577298Sobrien     line?  */
174660484Sobrien  if (command_line.endian != ENDIAN_UNSET)
174760484Sobrien    {
174877298Sobrien      const bfd_target *target;
174960484Sobrien      enum bfd_endian desired_endian;
175060484Sobrien
175160484Sobrien      /* Get the chosen target.  */
175277298Sobrien      target = bfd_search_for_target (get_target, (PTR) output_target);
175360484Sobrien
175477298Sobrien      /* If the target is not supported, we cannot do anything.  */
175577298Sobrien      if (target != NULL)
175660484Sobrien	{
175777298Sobrien	  if (command_line.endian == ENDIAN_BIG)
175877298Sobrien	    desired_endian = BFD_ENDIAN_BIG;
175960484Sobrien	  else
176077298Sobrien	    desired_endian = BFD_ENDIAN_LITTLE;
176177298Sobrien
176277298Sobrien	  /* See if the target has the wrong endianness.  This should
176377298Sobrien	     not happen if the linker script has provided big and
176477298Sobrien	     little endian alternatives, but some scrips don't do
176577298Sobrien	     this.  */
176677298Sobrien	  if (target->byteorder != desired_endian)
176760484Sobrien	    {
176877298Sobrien	      /* If it does, then see if the target provides
176977298Sobrien		 an alternative with the correct endianness.  */
177077298Sobrien	      if (target->alternative_target != NULL
177177298Sobrien		  && (target->alternative_target->byteorder == desired_endian))
177277298Sobrien		output_target = target->alternative_target->name;
177360484Sobrien	      else
177477298Sobrien		{
177577298Sobrien		  /* Try to find a target as similar as possible to
177677298Sobrien		     the default target, but which has the desired
177777298Sobrien		     endian characteristic.  */
177878828Sobrien		  (void) bfd_search_for_target (closest_target_match,
177978828Sobrien						(PTR) target);
178077298Sobrien
178177298Sobrien		  /* Oh dear - we could not find any targets that
178277298Sobrien		     satisfy our requirements.  */
178377298Sobrien		  if (winner == NULL)
178477298Sobrien		    einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
178577298Sobrien		  else
178677298Sobrien		    output_target = winner->name;
178777298Sobrien		}
178860484Sobrien	    }
178960484Sobrien	}
179060484Sobrien    }
179177298Sobrien
179233965Sjdp  output = bfd_openw (name, output_target);
179333965Sjdp
179433965Sjdp  if (output == (bfd *) NULL)
179533965Sjdp    {
179633965Sjdp      if (bfd_get_error () == bfd_error_invalid_target)
179760484Sobrien	einfo (_("%P%F: target %s not found\n"), output_target);
179860484Sobrien
179960484Sobrien      einfo (_("%P%F: cannot open output file %s: %E\n"), name);
180033965Sjdp    }
180133965Sjdp
180233965Sjdp  delete_output_file_on_failure = true;
180333965Sjdp
180477298Sobrien#if 0
180577298Sobrien  output->flags |= D_PAGED;
180677298Sobrien#endif
180733965Sjdp
180833965Sjdp  if (! bfd_set_format (output, bfd_object))
180960484Sobrien    einfo (_("%P%F:%s: can not make object file: %E\n"), name);
181033965Sjdp  if (! bfd_set_arch_mach (output,
181133965Sjdp			   ldfile_output_architecture,
181233965Sjdp			   ldfile_output_machine))
181360484Sobrien    einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
181433965Sjdp
181533965Sjdp  link_info.hash = bfd_link_hash_table_create (output);
181633965Sjdp  if (link_info.hash == (struct bfd_link_hash_table *) NULL)
181760484Sobrien    einfo (_("%P%F: can not create link hash table: %E\n"));
181833965Sjdp
181933965Sjdp  bfd_set_gp_size (output, g_switch_value);
182033965Sjdp  return output;
182133965Sjdp}
182233965Sjdp
182333965Sjdpstatic void
182433965Sjdpldlang_open_output (statement)
182577298Sobrien     lang_statement_union_type *statement;
182633965Sjdp{
182733965Sjdp  switch (statement->header.type)
182833965Sjdp    {
182933965Sjdp    case lang_output_statement_enum:
183033965Sjdp      ASSERT (output_bfd == (bfd *) NULL);
183133965Sjdp      output_bfd = open_output (statement->output_statement.name);
183233965Sjdp      ldemul_set_output_arch ();
183333965Sjdp      if (config.magic_demand_paged && !link_info.relocateable)
183433965Sjdp	output_bfd->flags |= D_PAGED;
183533965Sjdp      else
183633965Sjdp	output_bfd->flags &= ~D_PAGED;
183733965Sjdp      if (config.text_read_only)
183833965Sjdp	output_bfd->flags |= WP_TEXT;
183933965Sjdp      else
184033965Sjdp	output_bfd->flags &= ~WP_TEXT;
184133965Sjdp      if (link_info.traditional_format)
184233965Sjdp	output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
184333965Sjdp      else
184433965Sjdp	output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
184533965Sjdp      break;
184633965Sjdp
184733965Sjdp    case lang_target_statement_enum:
184833965Sjdp      current_target = statement->target_statement.target;
184933965Sjdp      break;
185033965Sjdp    default:
185133965Sjdp      break;
185233965Sjdp    }
185333965Sjdp}
185433965Sjdp
185533965Sjdp/* Open all the input files.  */
185633965Sjdp
185733965Sjdpstatic void
185833965Sjdpopen_input_bfds (s, force)
185933965Sjdp     lang_statement_union_type *s;
186033965Sjdp     boolean force;
186133965Sjdp{
186233965Sjdp  for (; s != (lang_statement_union_type *) NULL; s = s->next)
186333965Sjdp    {
186433965Sjdp      switch (s->header.type)
186533965Sjdp	{
186633965Sjdp	case lang_constructors_statement_enum:
186733965Sjdp	  open_input_bfds (constructor_list.head, force);
186833965Sjdp	  break;
186933965Sjdp	case lang_output_section_statement_enum:
187033965Sjdp	  open_input_bfds (s->output_section_statement.children.head, force);
187133965Sjdp	  break;
187233965Sjdp	case lang_wild_statement_enum:
187377298Sobrien	  /* Maybe we should load the file's symbols.  */
187433965Sjdp	  if (s->wild_statement.filename
187533965Sjdp	      && ! wildcardp (s->wild_statement.filename))
187633965Sjdp	    (void) lookup_name (s->wild_statement.filename);
187733965Sjdp	  open_input_bfds (s->wild_statement.children.head, force);
187833965Sjdp	  break;
187933965Sjdp	case lang_group_statement_enum:
188033965Sjdp	  {
188133965Sjdp	    struct bfd_link_hash_entry *undefs;
188233965Sjdp
188333965Sjdp	    /* We must continually search the entries in the group
188477298Sobrien	       until no new symbols are added to the list of undefined
188577298Sobrien	       symbols.  */
188633965Sjdp
188733965Sjdp	    do
188833965Sjdp	      {
188933965Sjdp		undefs = link_info.hash->undefs_tail;
189033965Sjdp		open_input_bfds (s->group_statement.children.head, true);
189133965Sjdp	      }
189233965Sjdp	    while (undefs != link_info.hash->undefs_tail);
189333965Sjdp	  }
189433965Sjdp	  break;
189533965Sjdp	case lang_target_statement_enum:
189633965Sjdp	  current_target = s->target_statement.target;
189733965Sjdp	  break;
189833965Sjdp	case lang_input_statement_enum:
189960484Sobrien	  if (s->input_statement.real)
190033965Sjdp	    {
190133965Sjdp	      lang_statement_list_type add;
190233965Sjdp
190333965Sjdp	      s->input_statement.target = current_target;
190433965Sjdp
190533965Sjdp	      /* If we are being called from within a group, and this
190633965Sjdp                 is an archive which has already been searched, then
190777298Sobrien                 force it to be researched unless the whole archive
190877298Sobrien		 has been loaded already.  */
190933965Sjdp	      if (force
191077298Sobrien		  && !s->input_statement.whole_archive
191133965Sjdp		  && s->input_statement.loaded
191233965Sjdp		  && bfd_check_format (s->input_statement.the_bfd,
191333965Sjdp				       bfd_archive))
191433965Sjdp		s->input_statement.loaded = false;
191533965Sjdp
191633965Sjdp	      lang_list_init (&add);
191733965Sjdp
191833965Sjdp	      load_symbols (&s->input_statement, &add);
191933965Sjdp
192033965Sjdp	      if (add.head != NULL)
192133965Sjdp		{
192233965Sjdp		  *add.tail = s->next;
192333965Sjdp		  s->next = add.head;
192433965Sjdp		}
192533965Sjdp	    }
192633965Sjdp	  break;
192733965Sjdp	default:
192833965Sjdp	  break;
192933965Sjdp	}
193033965Sjdp    }
193133965Sjdp}
193233965Sjdp
193377298Sobrien/* If there are [COMMONS] statements, put a wild one into the bss
193477298Sobrien   section.  */
193533965Sjdp
193633965Sjdpstatic void
193733965Sjdplang_reasonable_defaults ()
193833965Sjdp{
193933965Sjdp#if 0
194033965Sjdp  lang_output_section_statement_lookup (".text");
194133965Sjdp  lang_output_section_statement_lookup (".data");
194233965Sjdp
194377298Sobrien  default_common_section = lang_output_section_statement_lookup (".bss");
194433965Sjdp
194533965Sjdp  if (placed_commons == false)
194633965Sjdp    {
194733965Sjdp      lang_wild_statement_type *new =
194833965Sjdp      new_stat (lang_wild_statement,
194933965Sjdp		&default_common_section->children);
195033965Sjdp
195133965Sjdp      new->section_name = "COMMON";
195233965Sjdp      new->filename = (char *) NULL;
195333965Sjdp      lang_list_init (&new->children);
195433965Sjdp    }
195533965Sjdp#endif
195633965Sjdp}
195733965Sjdp
195877298Sobrien/* Add the supplied name to the symbol table as an undefined reference.
195977298Sobrien   Remove items from the chain as we open input bfds.  */
196033965Sjdptypedef struct ldlang_undef_chain_list
196133965Sjdp{
196233965Sjdp  struct ldlang_undef_chain_list *next;
196333965Sjdp  char *name;
196433965Sjdp}                       ldlang_undef_chain_list_type;
196533965Sjdp
196633965Sjdpstatic ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
196733965Sjdp
196833965Sjdpvoid
196933965Sjdpldlang_add_undef (name)
197077298Sobrien     const char *const name;
197133965Sjdp{
197233965Sjdp  ldlang_undef_chain_list_type *new =
197333965Sjdp    ((ldlang_undef_chain_list_type *)
197433965Sjdp     stat_alloc (sizeof (ldlang_undef_chain_list_type)));
197533965Sjdp
197633965Sjdp  new->next = ldlang_undef_chain_list_head;
197733965Sjdp  ldlang_undef_chain_list_head = new;
197833965Sjdp
197978828Sobrien  new->name = xstrdup (name);
198033965Sjdp}
198133965Sjdp
198233965Sjdp/* Run through the list of undefineds created above and place them
198333965Sjdp   into the linker hash table as undefined symbols belonging to the
198477298Sobrien   script file.  */
198577298Sobrien
198633965Sjdpstatic void
198733965Sjdplang_place_undefineds ()
198833965Sjdp{
198933965Sjdp  ldlang_undef_chain_list_type *ptr;
199033965Sjdp
199133965Sjdp  for (ptr = ldlang_undef_chain_list_head;
199233965Sjdp       ptr != (ldlang_undef_chain_list_type *) NULL;
199333965Sjdp       ptr = ptr->next)
199433965Sjdp    {
199533965Sjdp      struct bfd_link_hash_entry *h;
199633965Sjdp
199733965Sjdp      h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
199833965Sjdp      if (h == (struct bfd_link_hash_entry *) NULL)
199960484Sobrien	einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
200033965Sjdp      if (h->type == bfd_link_hash_new)
200133965Sjdp	{
200233965Sjdp	  h->type = bfd_link_hash_undefined;
200333965Sjdp	  h->u.undef.abfd = NULL;
200433965Sjdp	  bfd_link_add_undef (link_info.hash, h);
200533965Sjdp	}
200633965Sjdp    }
200733965Sjdp}
200833965Sjdp
200977298Sobrien/* Open input files and attatch to output sections.  */
201077298Sobrien
201133965Sjdpstatic void
201233965Sjdpmap_input_to_output_sections (s, target, output_section_statement)
201377298Sobrien     lang_statement_union_type *s;
201477298Sobrien     const char *target;
201577298Sobrien     lang_output_section_statement_type *output_section_statement;
201633965Sjdp{
201733965Sjdp  for (; s != (lang_statement_union_type *) NULL; s = s->next)
201833965Sjdp    {
201933965Sjdp      switch (s->header.type)
202033965Sjdp	{
202133965Sjdp
202233965Sjdp	case lang_wild_statement_enum:
202333965Sjdp	  wild (&s->wild_statement, s->wild_statement.section_name,
202433965Sjdp		s->wild_statement.filename, target,
202533965Sjdp		output_section_statement);
202633965Sjdp
202733965Sjdp	  break;
202833965Sjdp	case lang_constructors_statement_enum:
202933965Sjdp	  map_input_to_output_sections (constructor_list.head,
203033965Sjdp					target,
203133965Sjdp					output_section_statement);
203233965Sjdp	  break;
203333965Sjdp	case lang_output_section_statement_enum:
203433965Sjdp	  map_input_to_output_sections (s->output_section_statement.children.head,
203533965Sjdp					target,
203633965Sjdp					&s->output_section_statement);
203733965Sjdp	  break;
203833965Sjdp	case lang_output_statement_enum:
203933965Sjdp	  break;
204033965Sjdp	case lang_target_statement_enum:
204133965Sjdp	  target = s->target_statement.target;
204233965Sjdp	  break;
204333965Sjdp	case lang_group_statement_enum:
204433965Sjdp	  map_input_to_output_sections (s->group_statement.children.head,
204533965Sjdp					target,
204633965Sjdp					output_section_statement);
204733965Sjdp	  break;
204833965Sjdp	case lang_fill_statement_enum:
204933965Sjdp	case lang_input_section_enum:
205033965Sjdp	case lang_object_symbols_statement_enum:
205133965Sjdp	case lang_data_statement_enum:
205233965Sjdp	case lang_reloc_statement_enum:
205333965Sjdp	case lang_padding_statement_enum:
205433965Sjdp	case lang_input_statement_enum:
205533965Sjdp	  if (output_section_statement != NULL
205633965Sjdp	      && output_section_statement->bfd_section == NULL)
205733965Sjdp	    init_os (output_section_statement);
205833965Sjdp	  break;
205933965Sjdp	case lang_assignment_statement_enum:
206033965Sjdp	  if (output_section_statement != NULL
206133965Sjdp	      && output_section_statement->bfd_section == NULL)
206233965Sjdp	    init_os (output_section_statement);
206333965Sjdp
206433965Sjdp	  /* Make sure that any sections mentioned in the assignment
206577298Sobrien	     are initialized.  */
206633965Sjdp	  exp_init_os (s->assignment_statement.exp);
206733965Sjdp	  break;
206833965Sjdp	case lang_afile_asection_pair_statement_enum:
206933965Sjdp	  FAIL ();
207033965Sjdp	  break;
207133965Sjdp	case lang_address_statement_enum:
207277298Sobrien	  /* Mark the specified section with the supplied address.  */
207333965Sjdp	  {
207433965Sjdp	    lang_output_section_statement_type *os =
207533965Sjdp	      lang_output_section_statement_lookup
207633965Sjdp		(s->address_statement.section_name);
207733965Sjdp
207833965Sjdp	    if (os->bfd_section == NULL)
207933965Sjdp	      init_os (os);
208033965Sjdp	    os->addr_tree = s->address_statement.address;
208133965Sjdp	  }
208233965Sjdp	  break;
208333965Sjdp	}
208433965Sjdp    }
208533965Sjdp}
208633965Sjdp
208733965Sjdpstatic void
208833965Sjdpprint_output_section_statement (output_section_statement)
208977298Sobrien     lang_output_section_statement_type *output_section_statement;
209033965Sjdp{
209133965Sjdp  asection *section = output_section_statement->bfd_section;
209233965Sjdp  int len;
209333965Sjdp
209433965Sjdp  if (output_section_statement != abs_output_section)
209533965Sjdp    {
209633965Sjdp      minfo ("\n%s", output_section_statement->name);
209733965Sjdp
209833965Sjdp      if (section != NULL)
209933965Sjdp	{
210033965Sjdp	  print_dot = section->vma;
210133965Sjdp
210233965Sjdp	  len = strlen (output_section_statement->name);
210333965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
210433965Sjdp	    {
210533965Sjdp	      print_nl ();
210633965Sjdp	      len = 0;
210733965Sjdp	    }
210833965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
210933965Sjdp	    {
211033965Sjdp	      print_space ();
211133965Sjdp	      ++len;
211233965Sjdp	    }
211333965Sjdp
211433965Sjdp	  minfo ("0x%V %W", section->vma, section->_raw_size);
211533965Sjdp
211633965Sjdp	  if (output_section_statement->load_base != NULL)
211733965Sjdp	    {
211833965Sjdp	      bfd_vma addr;
211933965Sjdp
212033965Sjdp	      addr = exp_get_abs_int (output_section_statement->load_base, 0,
212133965Sjdp				      "load base", lang_final_phase_enum);
212260484Sobrien	      minfo (_(" load address 0x%V"), addr);
212333965Sjdp	    }
212433965Sjdp	}
212533965Sjdp
212633965Sjdp      print_nl ();
212733965Sjdp    }
212833965Sjdp
212933965Sjdp  print_statement_list (output_section_statement->children.head,
213033965Sjdp			output_section_statement);
213133965Sjdp}
213233965Sjdp
213333965Sjdpstatic void
213433965Sjdpprint_assignment (assignment, output_section)
213577298Sobrien     lang_assignment_statement_type *assignment;
213677298Sobrien     lang_output_section_statement_type *output_section;
213733965Sjdp{
213833965Sjdp  int i;
213933965Sjdp  etree_value_type result;
214033965Sjdp
214133965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
214233965Sjdp    print_space ();
214333965Sjdp
214433965Sjdp  result = exp_fold_tree (assignment->exp->assign.src, output_section,
214533965Sjdp			  lang_final_phase_enum, print_dot, &print_dot);
214660484Sobrien  if (result.valid_p)
214733965Sjdp    minfo ("0x%V", result.value + result.section->bfd_section->vma);
214833965Sjdp  else
214933965Sjdp    {
215033965Sjdp      minfo ("*undef*   ");
215133965Sjdp#ifdef BFD64
215233965Sjdp      minfo ("        ");
215333965Sjdp#endif
215433965Sjdp    }
215533965Sjdp
215633965Sjdp  minfo ("                ");
215733965Sjdp
215833965Sjdp  exp_print_tree (assignment->exp);
215933965Sjdp
216033965Sjdp  print_nl ();
216133965Sjdp}
216233965Sjdp
216333965Sjdpstatic void
216433965Sjdpprint_input_statement (statm)
216577298Sobrien     lang_input_statement_type *statm;
216633965Sjdp{
216733965Sjdp  if (statm->filename != (char *) NULL)
216833965Sjdp    {
216933965Sjdp      fprintf (config.map_file, "LOAD %s\n", statm->filename);
217033965Sjdp    }
217133965Sjdp}
217233965Sjdp
217333965Sjdp/* Print all symbols defined in a particular section.  This is called
217433965Sjdp   via bfd_link_hash_traverse.  */
217533965Sjdp
217677298Sobrienstatic boolean
217733965Sjdpprint_one_symbol (hash_entry, ptr)
217833965Sjdp     struct bfd_link_hash_entry *hash_entry;
217933965Sjdp     PTR ptr;
218033965Sjdp{
218133965Sjdp  asection *sec = (asection *) ptr;
218233965Sjdp
218333965Sjdp  if ((hash_entry->type == bfd_link_hash_defined
218433965Sjdp       || hash_entry->type == bfd_link_hash_defweak)
218533965Sjdp      && sec == hash_entry->u.def.section)
218633965Sjdp    {
218733965Sjdp      int i;
218833965Sjdp
218933965Sjdp      for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
219033965Sjdp	print_space ();
219133965Sjdp      minfo ("0x%V   ",
219233965Sjdp	     (hash_entry->u.def.value
219333965Sjdp	      + hash_entry->u.def.section->output_offset
219433965Sjdp	      + hash_entry->u.def.section->output_section->vma));
219533965Sjdp
219633965Sjdp      minfo ("             %T\n", hash_entry->root.string);
219733965Sjdp    }
219833965Sjdp
219933965Sjdp  return true;
220033965Sjdp}
220133965Sjdp
220233965Sjdp/* Print information about an input section to the map file.  */
220333965Sjdp
220433965Sjdpstatic void
220533965Sjdpprint_input_section (in)
220677298Sobrien     lang_input_section_type *in;
220733965Sjdp{
220833965Sjdp  asection *i = in->section;
220933965Sjdp  bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
221077298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
221160484Sobrien						ldfile_output_machine);
221233965Sjdp  if (size != 0)
221333965Sjdp    {
221433965Sjdp      print_space ();
221533965Sjdp
221633965Sjdp      minfo ("%s", i->name);
221733965Sjdp
221833965Sjdp      if (i->output_section != NULL)
221933965Sjdp	{
222033965Sjdp	  int len;
222133965Sjdp
222233965Sjdp	  len = 1 + strlen (i->name);
222333965Sjdp	  if (len >= SECTION_NAME_MAP_LENGTH - 1)
222433965Sjdp	    {
222533965Sjdp	      print_nl ();
222633965Sjdp	      len = 0;
222733965Sjdp	    }
222833965Sjdp	  while (len < SECTION_NAME_MAP_LENGTH)
222933965Sjdp	    {
223033965Sjdp	      print_space ();
223133965Sjdp	      ++len;
223233965Sjdp	    }
223333965Sjdp
223433965Sjdp	  minfo ("0x%V %W %B\n",
223560484Sobrien		 i->output_section->vma + i->output_offset, size / opb,
223633965Sjdp		 i->owner);
223733965Sjdp
223833965Sjdp	  if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
223933965Sjdp	    {
224033965Sjdp	      len = SECTION_NAME_MAP_LENGTH + 3;
224133965Sjdp#ifdef BFD64
224233965Sjdp	      len += 16;
224333965Sjdp#else
224433965Sjdp	      len += 8;
224533965Sjdp#endif
224633965Sjdp	      while (len > 0)
224733965Sjdp		{
224833965Sjdp		  print_space ();
224933965Sjdp		  --len;
225033965Sjdp		}
225133965Sjdp
225260484Sobrien	      minfo (_("%W (size before relaxing)\n"), i->_raw_size);
225333965Sjdp	    }
225433965Sjdp
225533965Sjdp	  bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
225633965Sjdp
225760484Sobrien	  print_dot = i->output_section->vma + i->output_offset + size / opb;
225833965Sjdp	}
225933965Sjdp    }
226033965Sjdp}
226133965Sjdp
226233965Sjdpstatic void
226333965Sjdpprint_fill_statement (fill)
226477298Sobrien     lang_fill_statement_type *fill;
226533965Sjdp{
226633965Sjdp  fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
226733965Sjdp}
226833965Sjdp
226933965Sjdpstatic void
227033965Sjdpprint_data_statement (data)
227177298Sobrien     lang_data_statement_type *data;
227233965Sjdp{
227333965Sjdp  int i;
227433965Sjdp  bfd_vma addr;
227533965Sjdp  bfd_size_type size;
227633965Sjdp  const char *name;
227777298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
227860484Sobrien						ldfile_output_machine);
227933965Sjdp
228033965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
228133965Sjdp    print_space ();
228233965Sjdp
228333965Sjdp  addr = data->output_vma;
228433965Sjdp  if (data->output_section != NULL)
228533965Sjdp    addr += data->output_section->vma;
228633965Sjdp
228733965Sjdp  switch (data->type)
228833965Sjdp    {
228933965Sjdp    default:
229033965Sjdp      abort ();
229133965Sjdp    case BYTE:
229233965Sjdp      size = BYTE_SIZE;
229333965Sjdp      name = "BYTE";
229433965Sjdp      break;
229533965Sjdp    case SHORT:
229633965Sjdp      size = SHORT_SIZE;
229733965Sjdp      name = "SHORT";
229833965Sjdp      break;
229933965Sjdp    case LONG:
230033965Sjdp      size = LONG_SIZE;
230133965Sjdp      name = "LONG";
230233965Sjdp      break;
230333965Sjdp    case QUAD:
230433965Sjdp      size = QUAD_SIZE;
230533965Sjdp      name = "QUAD";
230633965Sjdp      break;
230738889Sjdp    case SQUAD:
230838889Sjdp      size = QUAD_SIZE;
230938889Sjdp      name = "SQUAD";
231038889Sjdp      break;
231133965Sjdp    }
231233965Sjdp
231333965Sjdp  minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
231433965Sjdp
231533965Sjdp  if (data->exp->type.node_class != etree_value)
231633965Sjdp    {
231733965Sjdp      print_space ();
231833965Sjdp      exp_print_tree (data->exp);
231933965Sjdp    }
232033965Sjdp
232133965Sjdp  print_nl ();
232233965Sjdp
232360484Sobrien  print_dot = addr + size / opb;
232460484Sobrien
232533965Sjdp}
232633965Sjdp
232733965Sjdp/* Print an address statement.  These are generated by options like
232833965Sjdp   -Ttext.  */
232933965Sjdp
233033965Sjdpstatic void
233133965Sjdpprint_address_statement (address)
233233965Sjdp     lang_address_statement_type *address;
233333965Sjdp{
233460484Sobrien  minfo (_("Address of section %s set to "), address->section_name);
233533965Sjdp  exp_print_tree (address->address);
233633965Sjdp  print_nl ();
233733965Sjdp}
233833965Sjdp
233933965Sjdp/* Print a reloc statement.  */
234033965Sjdp
234133965Sjdpstatic void
234233965Sjdpprint_reloc_statement (reloc)
234333965Sjdp     lang_reloc_statement_type *reloc;
234433965Sjdp{
234533965Sjdp  int i;
234633965Sjdp  bfd_vma addr;
234733965Sjdp  bfd_size_type size;
234877298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
234977298Sobrien						ldfile_output_machine);
235033965Sjdp
235133965Sjdp  for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
235233965Sjdp    print_space ();
235333965Sjdp
235433965Sjdp  addr = reloc->output_vma;
235533965Sjdp  if (reloc->output_section != NULL)
235633965Sjdp    addr += reloc->output_section->vma;
235733965Sjdp
235833965Sjdp  size = bfd_get_reloc_size (reloc->howto);
235933965Sjdp
236033965Sjdp  minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
236133965Sjdp
236233965Sjdp  if (reloc->name != NULL)
236333965Sjdp    minfo ("%s+", reloc->name);
236433965Sjdp  else
236533965Sjdp    minfo ("%s+", reloc->section->name);
236633965Sjdp
236733965Sjdp  exp_print_tree (reloc->addend_exp);
236833965Sjdp
236933965Sjdp  print_nl ();
237033965Sjdp
237160484Sobrien  print_dot = addr + size / opb;
237277298Sobrien}
237333965Sjdp
237433965Sjdpstatic void
237533965Sjdpprint_padding_statement (s)
237633965Sjdp     lang_padding_statement_type *s;
237733965Sjdp{
237833965Sjdp  int len;
237933965Sjdp  bfd_vma addr;
238077298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
238177298Sobrien						ldfile_output_machine);
238233965Sjdp
238333965Sjdp  minfo (" *fill*");
238433965Sjdp
238533965Sjdp  len = sizeof " *fill*" - 1;
238633965Sjdp  while (len < SECTION_NAME_MAP_LENGTH)
238733965Sjdp    {
238833965Sjdp      print_space ();
238933965Sjdp      ++len;
239033965Sjdp    }
239133965Sjdp
239233965Sjdp  addr = s->output_offset;
239333965Sjdp  if (s->output_section != NULL)
239433965Sjdp    addr += s->output_section->vma;
239533965Sjdp  minfo ("0x%V %W", addr, s->size);
239633965Sjdp
239733965Sjdp  if (s->fill != 0)
239833965Sjdp    minfo (" %u", s->fill);
239933965Sjdp
240033965Sjdp  print_nl ();
240133965Sjdp
240260484Sobrien  print_dot = addr + s->size / opb;
240333965Sjdp}
240433965Sjdp
240533965Sjdpstatic void
240633965Sjdpprint_wild_statement (w, os)
240777298Sobrien     lang_wild_statement_type *w;
240877298Sobrien     lang_output_section_statement_type *os;
240933965Sjdp{
241033965Sjdp  print_space ();
241133965Sjdp
241260484Sobrien  if (w->filenames_sorted)
241360484Sobrien    minfo ("SORT(");
241460484Sobrien  if (w->exclude_filename_list != NULL)
241560484Sobrien    {
241660484Sobrien      name_list *tmp;
241760484Sobrien      minfo ("EXCLUDE_FILE ( %s", w->exclude_filename_list->name);
241877298Sobrien      for (tmp = w->exclude_filename_list->next; tmp; tmp = tmp->next)
241977298Sobrien	minfo (", %s", tmp->name);
242060484Sobrien      minfo (")");
242177298Sobrien    }
242277298Sobrien  if (w->filename != NULL)
242333965Sjdp    minfo ("%s", w->filename);
242433965Sjdp  else
242533965Sjdp    minfo ("*");
242660484Sobrien  if (w->filenames_sorted)
242760484Sobrien    minfo (")");
242833965Sjdp
242960484Sobrien  minfo ("(");
243060484Sobrien  if (w->sections_sorted)
243160484Sobrien    minfo ("SORT(");
243233965Sjdp  if (w->section_name != NULL)
243360484Sobrien    minfo ("%s", w->section_name);
243433965Sjdp  else
243560484Sobrien    minfo ("*");
243660484Sobrien  if (w->sections_sorted)
243760484Sobrien    minfo (")");
243860484Sobrien  minfo (")");
243933965Sjdp
244033965Sjdp  print_nl ();
244133965Sjdp
244233965Sjdp  print_statement_list (w->children.head, os);
244333965Sjdp}
244433965Sjdp
244533965Sjdp/* Print a group statement.  */
244633965Sjdp
244733965Sjdpstatic void
244833965Sjdpprint_group (s, os)
244933965Sjdp     lang_group_statement_type *s;
245033965Sjdp     lang_output_section_statement_type *os;
245133965Sjdp{
245233965Sjdp  fprintf (config.map_file, "START GROUP\n");
245333965Sjdp  print_statement_list (s->children.head, os);
245433965Sjdp  fprintf (config.map_file, "END GROUP\n");
245533965Sjdp}
245633965Sjdp
245733965Sjdp/* Print the list of statements in S.
245833965Sjdp   This can be called for any statement type.  */
245933965Sjdp
246033965Sjdpstatic void
246133965Sjdpprint_statement_list (s, os)
246233965Sjdp     lang_statement_union_type *s;
246333965Sjdp     lang_output_section_statement_type *os;
246433965Sjdp{
246533965Sjdp  while (s != NULL)
246633965Sjdp    {
246733965Sjdp      print_statement (s, os);
246833965Sjdp      s = s->next;
246933965Sjdp    }
247033965Sjdp}
247133965Sjdp
247233965Sjdp/* Print the first statement in statement list S.
247333965Sjdp   This can be called for any statement type.  */
247433965Sjdp
247533965Sjdpstatic void
247633965Sjdpprint_statement (s, os)
247733965Sjdp     lang_statement_union_type *s;
247833965Sjdp     lang_output_section_statement_type *os;
247933965Sjdp{
248033965Sjdp  switch (s->header.type)
248133965Sjdp    {
248233965Sjdp    default:
248360484Sobrien      fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
248433965Sjdp      FAIL ();
248533965Sjdp      break;
248633965Sjdp    case lang_constructors_statement_enum:
248733965Sjdp      if (constructor_list.head != NULL)
248833965Sjdp	{
248960484Sobrien	  if (constructors_sorted)
249060484Sobrien	    minfo (" SORT (CONSTRUCTORS)\n");
249160484Sobrien	  else
249260484Sobrien	    minfo (" CONSTRUCTORS\n");
249333965Sjdp	  print_statement_list (constructor_list.head, os);
249433965Sjdp	}
249533965Sjdp      break;
249633965Sjdp    case lang_wild_statement_enum:
249733965Sjdp      print_wild_statement (&s->wild_statement, os);
249833965Sjdp      break;
249933965Sjdp    case lang_address_statement_enum:
250033965Sjdp      print_address_statement (&s->address_statement);
250133965Sjdp      break;
250233965Sjdp    case lang_object_symbols_statement_enum:
250333965Sjdp      minfo (" CREATE_OBJECT_SYMBOLS\n");
250433965Sjdp      break;
250533965Sjdp    case lang_fill_statement_enum:
250633965Sjdp      print_fill_statement (&s->fill_statement);
250733965Sjdp      break;
250833965Sjdp    case lang_data_statement_enum:
250933965Sjdp      print_data_statement (&s->data_statement);
251033965Sjdp      break;
251133965Sjdp    case lang_reloc_statement_enum:
251233965Sjdp      print_reloc_statement (&s->reloc_statement);
251333965Sjdp      break;
251433965Sjdp    case lang_input_section_enum:
251533965Sjdp      print_input_section (&s->input_section);
251633965Sjdp      break;
251733965Sjdp    case lang_padding_statement_enum:
251833965Sjdp      print_padding_statement (&s->padding_statement);
251933965Sjdp      break;
252033965Sjdp    case lang_output_section_statement_enum:
252133965Sjdp      print_output_section_statement (&s->output_section_statement);
252233965Sjdp      break;
252333965Sjdp    case lang_assignment_statement_enum:
252433965Sjdp      print_assignment (&s->assignment_statement, os);
252533965Sjdp      break;
252633965Sjdp    case lang_target_statement_enum:
252733965Sjdp      fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
252833965Sjdp      break;
252933965Sjdp    case lang_output_statement_enum:
253033965Sjdp      minfo ("OUTPUT(%s", s->output_statement.name);
253133965Sjdp      if (output_target != NULL)
253233965Sjdp	minfo (" %s", output_target);
253333965Sjdp      minfo (")\n");
253433965Sjdp      break;
253533965Sjdp    case lang_input_statement_enum:
253633965Sjdp      print_input_statement (&s->input_statement);
253733965Sjdp      break;
253833965Sjdp    case lang_group_statement_enum:
253933965Sjdp      print_group (&s->group_statement, os);
254033965Sjdp      break;
254133965Sjdp    case lang_afile_asection_pair_statement_enum:
254233965Sjdp      FAIL ();
254333965Sjdp      break;
254433965Sjdp    }
254533965Sjdp}
254633965Sjdp
254733965Sjdpstatic void
254833965Sjdpprint_statements ()
254933965Sjdp{
255033965Sjdp  print_statement_list (statement_list.head, abs_output_section);
255133965Sjdp}
255233965Sjdp
255333965Sjdp/* Print the first N statements in statement list S to STDERR.
255433965Sjdp   If N == 0, nothing is printed.
255533965Sjdp   If N < 0, the entire list is printed.
255633965Sjdp   Intended to be called from GDB.  */
255733965Sjdp
255833965Sjdpvoid
255933965Sjdpdprint_statement (s, n)
256077298Sobrien     lang_statement_union_type *s;
256133965Sjdp     int n;
256233965Sjdp{
256333965Sjdp  FILE *map_save = config.map_file;
256433965Sjdp
256533965Sjdp  config.map_file = stderr;
256633965Sjdp
256733965Sjdp  if (n < 0)
256833965Sjdp    print_statement_list (s, abs_output_section);
256933965Sjdp  else
257033965Sjdp    {
257133965Sjdp      while (s && --n >= 0)
257233965Sjdp	{
257333965Sjdp	  print_statement (s, abs_output_section);
257433965Sjdp	  s = s->next;
257533965Sjdp	}
257633965Sjdp    }
257733965Sjdp
257833965Sjdp  config.map_file = map_save;
257933965Sjdp}
258033965Sjdp
258133965Sjdpstatic bfd_vma
258233965Sjdpinsert_pad (this_ptr, fill, power, output_section_statement, dot)
258377298Sobrien     lang_statement_union_type **this_ptr;
258433965Sjdp     fill_type fill;
258533965Sjdp     unsigned int power;
258677298Sobrien     asection *output_section_statement;
258733965Sjdp     bfd_vma dot;
258833965Sjdp{
258933965Sjdp  /* Align this section first to the
259033965Sjdp     input sections requirement, then
259133965Sjdp     to the output section's requirement.
259233965Sjdp     If this alignment is > than any seen before,
259333965Sjdp     then record it too. Perform the alignment by
259477298Sobrien     inserting a magic 'padding' statement.  */
259533965Sjdp
259677298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
259777298Sobrien						ldfile_output_machine);
259833965Sjdp  unsigned int alignment_needed = align_power (dot, power) - dot;
259933965Sjdp
260033965Sjdp  if (alignment_needed != 0)
260133965Sjdp    {
260233965Sjdp      lang_statement_union_type *new =
260333965Sjdp	((lang_statement_union_type *)
260433965Sjdp	 stat_alloc (sizeof (lang_padding_statement_type)));
260533965Sjdp
260677298Sobrien      /* Link into existing chain.  */
260733965Sjdp      new->header.next = *this_ptr;
260833965Sjdp      *this_ptr = new;
260933965Sjdp      new->header.type = lang_padding_statement_enum;
261033965Sjdp      new->padding_statement.output_section = output_section_statement;
261133965Sjdp      new->padding_statement.output_offset =
261233965Sjdp	dot - output_section_statement->vma;
261333965Sjdp      new->padding_statement.fill = fill;
261460484Sobrien      new->padding_statement.size = alignment_needed * opb;
261533965Sjdp    }
261633965Sjdp
261777298Sobrien  /* Remember the most restrictive alignment.  */
261833965Sjdp  if (power > output_section_statement->alignment_power)
261933965Sjdp    {
262033965Sjdp      output_section_statement->alignment_power = power;
262133965Sjdp    }
262260484Sobrien  output_section_statement->_raw_size += alignment_needed * opb;
262333965Sjdp
262460484Sobrien  return dot + alignment_needed;
262533965Sjdp}
262633965Sjdp
262777298Sobrien/* Work out how much this section will move the dot point.  */
262877298Sobrien
262933965Sjdpstatic bfd_vma
263033965Sjdpsize_input_section (this_ptr, output_section_statement, fill, dot, relax)
263177298Sobrien     lang_statement_union_type **this_ptr;
263277298Sobrien     lang_output_section_statement_type *output_section_statement;
263333965Sjdp     fill_type fill;
263433965Sjdp     bfd_vma dot;
263560484Sobrien     boolean relax ATTRIBUTE_UNUSED;
263633965Sjdp{
263733965Sjdp  lang_input_section_type *is = &((*this_ptr)->input_section);
263833965Sjdp  asection *i = is->section;
263977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
264077298Sobrien						ldfile_output_machine);
264133965Sjdp
264233965Sjdp  if (is->ifile->just_syms_flag == false)
264333965Sjdp    {
264433965Sjdp      if (output_section_statement->subsection_alignment != -1)
264533965Sjdp       i->alignment_power =
264633965Sjdp	output_section_statement->subsection_alignment;
264733965Sjdp
264833965Sjdp      dot = insert_pad (this_ptr, fill, i->alignment_power,
264933965Sjdp			output_section_statement->bfd_section, dot);
265033965Sjdp
265177298Sobrien      /* Remember where in the output section this input section goes.  */
265233965Sjdp
265333965Sjdp      i->output_offset = dot - output_section_statement->bfd_section->vma;
265433965Sjdp
265577298Sobrien      /* Mark how big the output section must be to contain this now.  */
265633965Sjdp      if (i->_cooked_size != 0)
265760484Sobrien	dot += i->_cooked_size / opb;
265833965Sjdp      else
265960484Sobrien	dot += i->_raw_size / opb;
266077298Sobrien      output_section_statement->bfd_section->_raw_size =
266177298Sobrien	(dot - output_section_statement->bfd_section->vma) * opb;
266233965Sjdp    }
266333965Sjdp  else
266433965Sjdp    {
266533965Sjdp      i->output_offset = i->vma - output_section_statement->bfd_section->vma;
266633965Sjdp    }
266733965Sjdp
266833965Sjdp  return dot;
266933965Sjdp}
267033965Sjdp
267160484Sobrien#define IGNORE_SECTION(bfd, s) \
267278828Sobrien  (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD))	\
267378828Sobrien    != (SEC_ALLOC | SEC_LOAD))					\
267460484Sobrien   || bfd_section_size (bfd, s) == 0)
267560484Sobrien
267660484Sobrien/* Check to see if any allocated sections overlap with other allocated
267760484Sobrien   sections.  This can happen when the linker script specifically specifies
267860484Sobrien   the output section addresses of the two sections.  */
267977298Sobrien
268060484Sobrienstatic void
268160484Sobrienlang_check_section_addresses ()
268260484Sobrien{
268377298Sobrien  asection *s;
268460484Sobrien  unsigned opb = bfd_octets_per_byte (output_bfd);
268560484Sobrien
268660484Sobrien  /* Scan all sections in the output list.  */
268760484Sobrien  for (s = output_bfd->sections; s != NULL; s = s->next)
268860484Sobrien    {
268977298Sobrien      asection *os;
269077298Sobrien
269160484Sobrien      /* Ignore sections which are not loaded or which have no contents.  */
269260484Sobrien      if (IGNORE_SECTION (output_bfd, s))
269360484Sobrien	continue;
269477298Sobrien
269560484Sobrien      /* Once we reach section 's' stop our seach.  This prevents two
269660484Sobrien	 warning messages from being produced, one for 'section A overlaps
269760484Sobrien	 section B' and one for 'section B overlaps section A'.  */
269860484Sobrien      for (os = output_bfd->sections; os != s; os = os->next)
269960484Sobrien	{
270060484Sobrien	  bfd_vma s_start;
270160484Sobrien	  bfd_vma s_end;
270260484Sobrien	  bfd_vma os_start;
270360484Sobrien	  bfd_vma os_end;
270477298Sobrien
270560484Sobrien	  /* Only consider loadable sections with real contents.  */
270660484Sobrien	  if (IGNORE_SECTION (output_bfd, os))
270760484Sobrien	    continue;
270860484Sobrien
270960484Sobrien	  /* We must check the sections' LMA addresses not their
271060484Sobrien	     VMA addresses because overlay sections can have
271160484Sobrien	     overlapping VMAs but they must have distinct LMAs.  */
271260484Sobrien	  s_start  = bfd_section_lma (output_bfd, s);
271360484Sobrien	  os_start = bfd_section_lma (output_bfd, os);
271460484Sobrien	  s_end    = s_start  + bfd_section_size (output_bfd, s) / opb - 1;
271560484Sobrien	  os_end   = os_start + bfd_section_size (output_bfd, os) / opb - 1;
271677298Sobrien
271760484Sobrien	  /* Look for an overlap.  */
271860484Sobrien	  if ((s_end < os_start) || (s_start > os_end))
271960484Sobrien	    continue;
272077298Sobrien
272160484Sobrien	  einfo (
272260484Sobrien_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
272360484Sobrien		 s->name, s_start, s_end, os->name, os_start, os_end);
272477298Sobrien
272560484Sobrien	  /* Once we have found one overlap for this section,
272660484Sobrien	     stop looking for others.  */
272760484Sobrien	  break;
272860484Sobrien	}
272960484Sobrien    }
273060484Sobrien}
273160484Sobrien
273233965Sjdp/* This variable indicates whether bfd_relax_section should be called
273333965Sjdp   again.  */
273433965Sjdp
273533965Sjdpstatic boolean relax_again;
273633965Sjdp
273760484Sobrien/* Make sure the new address is within the region.  We explicitly permit the
273860484Sobrien   current address to be at the exact end of the region when the address is
273960484Sobrien   non-zero, in case the region is at the end of addressable memory and the
274077298Sobrien   calculation wraps around.  */
274160484Sobrien
274260484Sobrienstatic void
274360484Sobrienos_region_check (os, region, tree, base)
274477298Sobrien     lang_output_section_statement_type *os;
274577298Sobrien     struct memory_region_struct *region;
274677298Sobrien     etree_type *tree;
274777298Sobrien     bfd_vma base;
274860484Sobrien{
274960484Sobrien  if ((region->current < region->origin
275060484Sobrien       || (region->current - region->origin > region->length))
275160484Sobrien      && ((region->current != region->origin + region->length)
275260484Sobrien           || base == 0))
275360484Sobrien    {
275460484Sobrien      if (tree != (etree_type *) NULL)
275560484Sobrien        {
275660484Sobrien          einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
275760484Sobrien                 region->current,
275860484Sobrien                 os->bfd_section->owner,
275960484Sobrien                 os->bfd_section->name,
276060484Sobrien                 region->name);
276160484Sobrien        }
276260484Sobrien      else
276360484Sobrien        {
276460484Sobrien          einfo (_("%X%P: region %s is full (%B section %s)\n"),
276560484Sobrien                 region->name,
276660484Sobrien                 os->bfd_section->owner,
276760484Sobrien                 os->bfd_section->name);
276860484Sobrien        }
276960484Sobrien      /* Reset the region pointer.  */
277060484Sobrien      region->current = region->origin;
277160484Sobrien    }
277260484Sobrien}
277360484Sobrien
277433965Sjdp/* Set the sizes for all the output sections.  */
277533965Sjdp
277633965Sjdpbfd_vma
277733965Sjdplang_size_sections (s, output_section_statement, prev, fill, dot, relax)
277877298Sobrien     lang_statement_union_type *s;
277977298Sobrien     lang_output_section_statement_type *output_section_statement;
278077298Sobrien     lang_statement_union_type **prev;
278133965Sjdp     fill_type fill;
278233965Sjdp     bfd_vma dot;
278333965Sjdp     boolean relax;
278433965Sjdp{
278577298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
278660484Sobrien						ldfile_output_machine);
278760484Sobrien
278860484Sobrien  /* Size up the sections from their constituent parts.  */
278933965Sjdp  for (; s != (lang_statement_union_type *) NULL; s = s->next)
279033965Sjdp    {
279160484Sobrien      switch (s->header.type)
279260484Sobrien	{
279360484Sobrien	case lang_output_section_statement_enum:
279460484Sobrien	  {
279560484Sobrien	    bfd_vma after;
279678828Sobrien	    lang_output_section_statement_type *os;
279733965Sjdp
279878828Sobrien	    os = &s->output_section_statement;
279960484Sobrien	    if (os->bfd_section == NULL)
280060484Sobrien	      /* This section was never actually created.  */
280160484Sobrien	      break;
280233965Sjdp
280360484Sobrien	    /* If this is a COFF shared library section, use the size and
280460484Sobrien	       address from the input section.  FIXME: This is COFF
280560484Sobrien	       specific; it would be cleaner if there were some other way
280660484Sobrien	       to do this, but nothing simple comes to mind.  */
280760484Sobrien	    if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
280860484Sobrien	      {
280977298Sobrien		asection *input;
281033965Sjdp
281160484Sobrien		if (os->children.head == NULL
281260484Sobrien		    || os->children.head->next != NULL
281360484Sobrien		    || os->children.head->header.type != lang_input_section_enum)
281460484Sobrien		  einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
281560484Sobrien			 os->name);
281633965Sjdp
281760484Sobrien		input = os->children.head->input_section.section;
281860484Sobrien		bfd_set_section_vma (os->bfd_section->owner,
281960484Sobrien				     os->bfd_section,
282060484Sobrien				     bfd_section_vma (input->owner, input));
282160484Sobrien		os->bfd_section->_raw_size = input->_raw_size;
282260484Sobrien		break;
282360484Sobrien	      }
282433965Sjdp
282560484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
282660484Sobrien	      {
282760484Sobrien		/* No matter what happens, an abs section starts at zero.  */
282860484Sobrien		ASSERT (os->bfd_section->vma == 0);
282960484Sobrien	      }
283060484Sobrien	    else
283160484Sobrien	      {
283260484Sobrien		if (os->addr_tree == (etree_type *) NULL)
283360484Sobrien		  {
283460484Sobrien		    /* No address specified for this section, get one
283560484Sobrien		       from the region specification.  */
283660484Sobrien		    if (os->region == (lang_memory_region_type *) NULL
283760484Sobrien			|| (((bfd_get_section_flags (output_bfd, os->bfd_section)
283860484Sobrien			      & (SEC_ALLOC | SEC_LOAD)) != 0)
283960484Sobrien			    && os->region->name[0] == '*'
284060484Sobrien			    && strcmp (os->region->name, "*default*") == 0))
284160484Sobrien		      {
284260484Sobrien			os->region = lang_memory_default (os->bfd_section);
284360484Sobrien		      }
284433965Sjdp
284560484Sobrien		    /* If a loadable section is using the default memory
284660484Sobrien		       region, and some non default memory regions were
284760484Sobrien		       defined, issue a warning.  */
284860484Sobrien		    if ((bfd_get_section_flags (output_bfd, os->bfd_section)
284960484Sobrien			 & (SEC_ALLOC | SEC_LOAD)) != 0
285060484Sobrien			&& ! link_info.relocateable
285160484Sobrien			&& strcmp (os->region->name, "*default*") == 0
285260484Sobrien			&& lang_memory_region_list != NULL
285378828Sobrien			&& (strcmp (lang_memory_region_list->name,
285478828Sobrien				    "*default*") != 0
285560484Sobrien			    || lang_memory_region_list->next != NULL))
285660484Sobrien		      einfo (_("%P: warning: no memory region specified for section `%s'\n"),
285778828Sobrien			     bfd_get_section_name (output_bfd,
285878828Sobrien						   os->bfd_section));
285938889Sjdp
286060484Sobrien		    dot = os->region->current;
286177298Sobrien
286260484Sobrien		    if (os->section_alignment == -1)
286360484Sobrien		      {
286460484Sobrien			bfd_vma olddot;
286538889Sjdp
286660484Sobrien			olddot = dot;
286778828Sobrien			dot = align_power (dot,
286878828Sobrien					   os->bfd_section->alignment_power);
286933965Sjdp
287060484Sobrien			if (dot != olddot && config.warn_section_align)
287160484Sobrien			  einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
287260484Sobrien				 os->name, (unsigned int) (dot - olddot));
287360484Sobrien		      }
287460484Sobrien		  }
287560484Sobrien		else
287660484Sobrien		  {
287760484Sobrien		    etree_value_type r;
287833965Sjdp
287960484Sobrien		    r = exp_fold_tree (os->addr_tree,
288060484Sobrien				       abs_output_section,
288160484Sobrien				       lang_allocating_phase_enum,
288260484Sobrien				       dot, &dot);
288360484Sobrien		    if (r.valid_p == false)
288460484Sobrien		      {
288560484Sobrien			einfo (_("%F%S: non constant address expression for section %s\n"),
288660484Sobrien			       os->name);
288760484Sobrien		      }
288860484Sobrien		    dot = r.value + r.section->bfd_section->vma;
288960484Sobrien		  }
289077298Sobrien
289160484Sobrien		/* The section starts here.
289260484Sobrien		   First, align to what the section needs.  */
289333965Sjdp
289460484Sobrien		if (os->section_alignment != -1)
289560484Sobrien		  dot = align_power (dot, os->section_alignment);
289633965Sjdp
289760484Sobrien		bfd_set_section_vma (0, os->bfd_section, dot);
289877298Sobrien
289960484Sobrien		os->bfd_section->output_offset = 0;
290060484Sobrien	      }
290133965Sjdp
290277298Sobrien	    (void) lang_size_sections (os->children.head, os,
290377298Sobrien				       &os->children.head,
290460484Sobrien				       os->fill, dot, relax);
290577298Sobrien
290677298Sobrien	    /* Put the section within the requested block size, or
290777298Sobrien	       align at the block boundary.  */
290860484Sobrien	    after = ALIGN_N (os->bfd_section->vma
290960484Sobrien			     + os->bfd_section->_raw_size / opb,
291060484Sobrien			     /* The coercion here is important, see ld.h.  */
291160484Sobrien			     (bfd_vma) os->block_value);
291233965Sjdp
291360484Sobrien	    if (bfd_is_abs_section (os->bfd_section))
291460484Sobrien	      ASSERT (after == os->bfd_section->vma);
291560484Sobrien	    else
291677298Sobrien	      os->bfd_section->_raw_size =
291777298Sobrien		(after - os->bfd_section->vma) * opb;
291860484Sobrien	    dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
291960484Sobrien	    os->processed = true;
292033965Sjdp
292160484Sobrien	    /* Update dot in the region ?
292260484Sobrien	       We only do this if the section is going to be allocated,
292360484Sobrien	       since unallocated sections do not contribute to the region's
292460484Sobrien	       overall size in memory.
292577298Sobrien
292660484Sobrien	       If the SEC_NEVER_LOAD bit is not set, it will affect the
292760484Sobrien	       addresses of sections after it. We have to update
292860484Sobrien	       dot.  */
292960484Sobrien	    if (os->region != (lang_memory_region_type *) NULL
293060484Sobrien		&& ((bfd_get_section_flags (output_bfd, os->bfd_section)
293160484Sobrien		     & SEC_NEVER_LOAD) == 0
293260484Sobrien		    || (bfd_get_section_flags (output_bfd, os->bfd_section)
293360484Sobrien			& (SEC_ALLOC | SEC_LOAD))))
293460484Sobrien	      {
293560484Sobrien		os->region->current = dot;
293677298Sobrien
293760484Sobrien		/* Make sure the new address is within the region.  */
293877298Sobrien		os_region_check (os, os->region, os->addr_tree,
293977298Sobrien				 os->bfd_section->vma);
294033965Sjdp
294177298Sobrien		/* If there's no load address specified, use the run
294277298Sobrien		   region as the load region.  */
294377298Sobrien		if (os->lma_region == NULL && os->load_base == NULL)
294477298Sobrien		  os->lma_region = os->region;
294533965Sjdp
294677298Sobrien		if (os->lma_region != NULL)
294777298Sobrien		  {
294877298Sobrien		    if (os->load_base != NULL)
294977298Sobrien		      {
295077298Sobrien			einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
295177298Sobrien		      }
295277298Sobrien		    else
295377298Sobrien		      {
295477298Sobrien			/* Don't allocate twice.  */
295577298Sobrien			if (os->lma_region != os->region)
295677298Sobrien			  {
295777298Sobrien			    /* Set load_base, which will be handled later.  */
295877298Sobrien			    os->load_base =
295977298Sobrien			      exp_intop (os->lma_region->current);
296077298Sobrien			    os->lma_region->current +=
296177298Sobrien			      os->bfd_section->_raw_size / opb;
296277298Sobrien			    os_region_check (os, os->lma_region, NULL,
296377298Sobrien					     os->bfd_section->lma);
296477298Sobrien			  }
296577298Sobrien		      }
296677298Sobrien		  }
296760484Sobrien	      }
296860484Sobrien	  }
296960484Sobrien	  break;
297033965Sjdp
297160484Sobrien	case lang_constructors_statement_enum:
297260484Sobrien	  dot = lang_size_sections (constructor_list.head,
297360484Sobrien				    output_section_statement,
297460484Sobrien				    &s->wild_statement.children.head,
297560484Sobrien				    fill,
297660484Sobrien				    dot, relax);
297760484Sobrien	  break;
297833965Sjdp
297960484Sobrien	case lang_data_statement_enum:
298060484Sobrien	  {
298160484Sobrien	    unsigned int size = 0;
298233965Sjdp
298377298Sobrien	    s->data_statement.output_vma =
298477298Sobrien	      dot - output_section_statement->bfd_section->vma;
298560484Sobrien	    s->data_statement.output_section =
298660484Sobrien	      output_section_statement->bfd_section;
298733965Sjdp
298860484Sobrien	    switch (s->data_statement.type)
298960484Sobrien	      {
299077298Sobrien	      default:
299177298Sobrien		abort ();
299260484Sobrien	      case QUAD:
299360484Sobrien	      case SQUAD:
299460484Sobrien		size = QUAD_SIZE;
299560484Sobrien		break;
299660484Sobrien	      case LONG:
299760484Sobrien		size = LONG_SIZE;
299860484Sobrien		break;
299960484Sobrien	      case SHORT:
300060484Sobrien		size = SHORT_SIZE;
300160484Sobrien		break;
300260484Sobrien	      case BYTE:
300360484Sobrien		size = BYTE_SIZE;
300460484Sobrien		break;
300560484Sobrien	      }
300677298Sobrien	    if (size < opb)
300777298Sobrien	      size = opb;
300860484Sobrien	    dot += size / opb;
300960484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
301060484Sobrien	    /* The output section gets contents, and then we inspect for
301160484Sobrien	       any flags set in the input script which override any ALLOC.  */
301260484Sobrien	    output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
301377298Sobrien	    if (!(output_section_statement->flags & SEC_NEVER_LOAD))
301477298Sobrien	      {
301577298Sobrien		output_section_statement->bfd_section->flags |=
301677298Sobrien		  SEC_ALLOC | SEC_LOAD;
301777298Sobrien	      }
301860484Sobrien	  }
301960484Sobrien	  break;
302033965Sjdp
302160484Sobrien	case lang_reloc_statement_enum:
302260484Sobrien	  {
302360484Sobrien	    int size;
302433965Sjdp
302560484Sobrien	    s->reloc_statement.output_vma =
302660484Sobrien	      dot - output_section_statement->bfd_section->vma;
302760484Sobrien	    s->reloc_statement.output_section =
302860484Sobrien	      output_section_statement->bfd_section;
302960484Sobrien	    size = bfd_get_reloc_size (s->reloc_statement.howto);
303060484Sobrien	    dot += size / opb;
303160484Sobrien	    output_section_statement->bfd_section->_raw_size += size;
303260484Sobrien	  }
303360484Sobrien	  break;
303477298Sobrien
303560484Sobrien	case lang_wild_statement_enum:
303633965Sjdp
303760484Sobrien	  dot = lang_size_sections (s->wild_statement.children.head,
303860484Sobrien				    output_section_statement,
303960484Sobrien				    &s->wild_statement.children.head,
304060484Sobrien				    fill, dot, relax);
304133965Sjdp
304260484Sobrien	  break;
304333965Sjdp
304460484Sobrien	case lang_object_symbols_statement_enum:
304560484Sobrien	  link_info.create_object_symbols_section =
304660484Sobrien	    output_section_statement->bfd_section;
304760484Sobrien	  break;
304860484Sobrien	case lang_output_statement_enum:
304960484Sobrien	case lang_target_statement_enum:
305060484Sobrien	  break;
305160484Sobrien	case lang_input_section_enum:
305233965Sjdp	  {
305360484Sobrien	    asection *i;
305433965Sjdp
305560484Sobrien	    i = (*prev)->input_section.section;
305660484Sobrien	    if (! relax)
305760484Sobrien	      {
305860484Sobrien		if (i->_cooked_size == 0)
305960484Sobrien		  i->_cooked_size = i->_raw_size;
306060484Sobrien	      }
306160484Sobrien	    else
306260484Sobrien	      {
306360484Sobrien		boolean again;
306460484Sobrien
306560484Sobrien		if (! bfd_relax_section (i->owner, i, &link_info, &again))
306660484Sobrien		  einfo (_("%P%F: can't relax section: %E\n"));
306760484Sobrien		if (again)
306860484Sobrien		  relax_again = true;
306960484Sobrien	      }
307060484Sobrien	    dot = size_input_section (prev,
307160484Sobrien				      output_section_statement,
307260484Sobrien				      output_section_statement->fill,
307360484Sobrien				      dot, relax);
307433965Sjdp	  }
307560484Sobrien	  break;
307660484Sobrien	case lang_input_statement_enum:
307760484Sobrien	  break;
307860484Sobrien	case lang_fill_statement_enum:
307977298Sobrien	  s->fill_statement.output_section =
308077298Sobrien	    output_section_statement->bfd_section;
308133965Sjdp
308260484Sobrien	  fill = s->fill_statement.fill;
308360484Sobrien	  break;
308460484Sobrien	case lang_assignment_statement_enum:
308560484Sobrien	  {
308660484Sobrien	    bfd_vma newdot = dot;
308733965Sjdp
308860484Sobrien	    exp_fold_tree (s->assignment_statement.exp,
308960484Sobrien			   output_section_statement,
309060484Sobrien			   lang_allocating_phase_enum,
309160484Sobrien			   dot,
309260484Sobrien			   &newdot);
309333965Sjdp
309460484Sobrien	    if (newdot != dot)
309560484Sobrien	      {
309660484Sobrien		/* The assignment changed dot.  Insert a pad.  */
309760484Sobrien		if (output_section_statement == abs_output_section)
309860484Sobrien		  {
309960484Sobrien		    /* If we don't have an output section, then just adjust
310060484Sobrien		       the default memory address.  */
310160484Sobrien		    lang_memory_region_lookup ("*default*")->current = newdot;
310260484Sobrien		  }
310360484Sobrien		else if (!relax)
310460484Sobrien		  {
310560484Sobrien		    lang_statement_union_type *new =
310660484Sobrien		      ((lang_statement_union_type *)
310760484Sobrien		       stat_alloc (sizeof (lang_padding_statement_type)));
310833965Sjdp
310960484Sobrien		    /* Link into existing chain.  */
311060484Sobrien		    new->header.next = *prev;
311160484Sobrien		    *prev = new;
311260484Sobrien		    new->header.type = lang_padding_statement_enum;
311360484Sobrien		    new->padding_statement.output_section =
311460484Sobrien		      output_section_statement->bfd_section;
311560484Sobrien		    new->padding_statement.output_offset =
311660484Sobrien		      dot - output_section_statement->bfd_section->vma;
311760484Sobrien		    new->padding_statement.fill = fill;
311860484Sobrien		    new->padding_statement.size = (newdot - dot) * opb;
311960484Sobrien		    output_section_statement->bfd_section->_raw_size +=
312060484Sobrien		      new->padding_statement.size;
312160484Sobrien		  }
312233965Sjdp
312360484Sobrien		dot = newdot;
312460484Sobrien	      }
312560484Sobrien	  }
312660484Sobrien	  break;
312733965Sjdp
312860484Sobrien	case lang_padding_statement_enum:
312960484Sobrien	  /* If we are relaxing, and this is not the first pass, some
313060484Sobrien	     padding statements may have been inserted during previous
313160484Sobrien	     passes.  We may have to move the padding statement to a new
313260484Sobrien	     location if dot has a different value at this point in this
313360484Sobrien	     pass than it did at this point in the previous pass.  */
313460484Sobrien	  s->padding_statement.output_offset =
313560484Sobrien	    dot - output_section_statement->bfd_section->vma;
313660484Sobrien	  dot += s->padding_statement.size / opb;
313760484Sobrien	  output_section_statement->bfd_section->_raw_size +=
313860484Sobrien	    s->padding_statement.size;
313960484Sobrien	  break;
314033965Sjdp
314160484Sobrien	case lang_group_statement_enum:
314260484Sobrien	  dot = lang_size_sections (s->group_statement.children.head,
314360484Sobrien				    output_section_statement,
314460484Sobrien				    &s->group_statement.children.head,
314560484Sobrien				    fill, dot, relax);
314660484Sobrien	  break;
314733965Sjdp
314860484Sobrien	default:
314960484Sobrien	  FAIL ();
315060484Sobrien	  break;
315133965Sjdp
315260484Sobrien	  /* This can only get here when relaxing is turned on.  */
315333965Sjdp
315460484Sobrien	case lang_address_statement_enum:
315560484Sobrien	  break;
315660484Sobrien	}
315760484Sobrien      prev = &s->header.next;
315833965Sjdp    }
315933965Sjdp  return dot;
316033965Sjdp}
316133965Sjdp
316233965Sjdpbfd_vma
316333965Sjdplang_do_assignments (s, output_section_statement, fill, dot)
316477298Sobrien     lang_statement_union_type *s;
316577298Sobrien     lang_output_section_statement_type *output_section_statement;
316633965Sjdp     fill_type fill;
316733965Sjdp     bfd_vma dot;
316833965Sjdp{
316977298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
317077298Sobrien						ldfile_output_machine);
317160484Sobrien
317233965Sjdp  for (; s != (lang_statement_union_type *) NULL; s = s->next)
317333965Sjdp    {
317433965Sjdp      switch (s->header.type)
317533965Sjdp	{
317633965Sjdp	case lang_constructors_statement_enum:
317733965Sjdp	  dot = lang_do_assignments (constructor_list.head,
317833965Sjdp				     output_section_statement,
317933965Sjdp				     fill,
318033965Sjdp				     dot);
318133965Sjdp	  break;
318233965Sjdp
318333965Sjdp	case lang_output_section_statement_enum:
318433965Sjdp	  {
318578828Sobrien	    lang_output_section_statement_type *os;
318633965Sjdp
318778828Sobrien	    os = &(s->output_section_statement);
318833965Sjdp	    if (os->bfd_section != NULL)
318933965Sjdp	      {
319033965Sjdp		dot = os->bfd_section->vma;
319133965Sjdp		(void) lang_do_assignments (os->children.head, os,
319233965Sjdp					    os->fill, dot);
319360484Sobrien		dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
319460484Sobrien
319533965Sjdp	      }
319677298Sobrien	    if (os->load_base)
319733965Sjdp	      {
319833965Sjdp		/* If nothing has been placed into the output section then
319977298Sobrien		   it won't have a bfd_section.  */
320077298Sobrien		if (os->bfd_section)
320133965Sjdp		  {
320277298Sobrien		    os->bfd_section->lma
320377298Sobrien		      = exp_get_abs_int (os->load_base, 0, "load base",
320477298Sobrien					 lang_final_phase_enum);
320533965Sjdp		  }
320633965Sjdp	      }
320733965Sjdp	  }
320833965Sjdp	  break;
320933965Sjdp	case lang_wild_statement_enum:
321033965Sjdp
321133965Sjdp	  dot = lang_do_assignments (s->wild_statement.children.head,
321233965Sjdp				     output_section_statement,
321333965Sjdp				     fill, dot);
321433965Sjdp
321533965Sjdp	  break;
321633965Sjdp
321733965Sjdp	case lang_object_symbols_statement_enum:
321833965Sjdp	case lang_output_statement_enum:
321933965Sjdp	case lang_target_statement_enum:
322033965Sjdp#if 0
322133965Sjdp	case lang_common_statement_enum:
322233965Sjdp#endif
322333965Sjdp	  break;
322433965Sjdp	case lang_data_statement_enum:
322533965Sjdp	  {
322633965Sjdp	    etree_value_type value;
322733965Sjdp
322833965Sjdp	    value = exp_fold_tree (s->data_statement.exp,
322933965Sjdp				   abs_output_section,
323033965Sjdp				   lang_final_phase_enum, dot, &dot);
323133965Sjdp	    s->data_statement.value = value.value;
323260484Sobrien	    if (value.valid_p == false)
323360484Sobrien	      einfo (_("%F%P: invalid data statement\n"));
323433965Sjdp	  }
323560484Sobrien          {
323660484Sobrien            unsigned int size;
323777298Sobrien	    switch (s->data_statement.type)
323877298Sobrien	      {
323977298Sobrien	      default:
324077298Sobrien		abort ();
324177298Sobrien	      case QUAD:
324277298Sobrien	      case SQUAD:
324377298Sobrien		size = QUAD_SIZE;
324477298Sobrien		break;
324577298Sobrien	      case LONG:
324677298Sobrien		size = LONG_SIZE;
324777298Sobrien		break;
324877298Sobrien	      case SHORT:
324977298Sobrien		size = SHORT_SIZE;
325077298Sobrien		break;
325177298Sobrien	      case BYTE:
325277298Sobrien		size = BYTE_SIZE;
325377298Sobrien		break;
325477298Sobrien	      }
325577298Sobrien	    if (size < opb)
325677298Sobrien	      size = opb;
325777298Sobrien	    dot += size / opb;
325877298Sobrien	  }
325933965Sjdp	  break;
326033965Sjdp
326133965Sjdp	case lang_reloc_statement_enum:
326233965Sjdp	  {
326333965Sjdp	    etree_value_type value;
326433965Sjdp
326533965Sjdp	    value = exp_fold_tree (s->reloc_statement.addend_exp,
326633965Sjdp				   abs_output_section,
326733965Sjdp				   lang_final_phase_enum, dot, &dot);
326833965Sjdp	    s->reloc_statement.addend_value = value.value;
326960484Sobrien	    if (value.valid_p == false)
327060484Sobrien	      einfo (_("%F%P: invalid reloc statement\n"));
327133965Sjdp	  }
327260484Sobrien	  dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
327333965Sjdp	  break;
327433965Sjdp
327533965Sjdp	case lang_input_section_enum:
327633965Sjdp	  {
327733965Sjdp	    asection *in = s->input_section.section;
327833965Sjdp
327933965Sjdp	    if (in->_cooked_size != 0)
328060484Sobrien	      dot += in->_cooked_size / opb;
328133965Sjdp	    else
328260484Sobrien	      dot += in->_raw_size / opb;
328333965Sjdp	  }
328433965Sjdp	  break;
328533965Sjdp
328633965Sjdp	case lang_input_statement_enum:
328733965Sjdp	  break;
328833965Sjdp	case lang_fill_statement_enum:
328933965Sjdp	  fill = s->fill_statement.fill;
329033965Sjdp	  break;
329133965Sjdp	case lang_assignment_statement_enum:
329233965Sjdp	  {
329333965Sjdp	    exp_fold_tree (s->assignment_statement.exp,
329433965Sjdp			   output_section_statement,
329533965Sjdp			   lang_final_phase_enum,
329633965Sjdp			   dot,
329733965Sjdp			   &dot);
329833965Sjdp	  }
329933965Sjdp
330033965Sjdp	  break;
330133965Sjdp	case lang_padding_statement_enum:
330260484Sobrien	  dot += s->padding_statement.size / opb;
330333965Sjdp	  break;
330433965Sjdp
330533965Sjdp	case lang_group_statement_enum:
330633965Sjdp	  dot = lang_do_assignments (s->group_statement.children.head,
330733965Sjdp				     output_section_statement,
330833965Sjdp				     fill, dot);
330933965Sjdp
331033965Sjdp	  break;
331133965Sjdp
331233965Sjdp	default:
331333965Sjdp	  FAIL ();
331433965Sjdp	  break;
331533965Sjdp	case lang_address_statement_enum:
331633965Sjdp	  break;
331733965Sjdp	}
331833965Sjdp
331933965Sjdp    }
332033965Sjdp  return dot;
332133965Sjdp}
332233965Sjdp
332333965Sjdp/* Fix any .startof. or .sizeof. symbols.  When the assemblers see the
332433965Sjdp   operator .startof. (section_name), it produces an undefined symbol
332533965Sjdp   .startof.section_name.  Similarly, when it sees
332633965Sjdp   .sizeof. (section_name), it produces an undefined symbol
332733965Sjdp   .sizeof.section_name.  For all the output sections, we look for
332833965Sjdp   such symbols, and set them to the correct value.  */
332933965Sjdp
333033965Sjdpstatic void
333133965Sjdplang_set_startof ()
333233965Sjdp{
333333965Sjdp  asection *s;
333433965Sjdp
333533965Sjdp  if (link_info.relocateable)
333633965Sjdp    return;
333733965Sjdp
333833965Sjdp  for (s = output_bfd->sections; s != NULL; s = s->next)
333933965Sjdp    {
334033965Sjdp      const char *secname;
334133965Sjdp      char *buf;
334233965Sjdp      struct bfd_link_hash_entry *h;
334333965Sjdp
334433965Sjdp      secname = bfd_get_section_name (output_bfd, s);
334533965Sjdp      buf = xmalloc (10 + strlen (secname));
334633965Sjdp
334733965Sjdp      sprintf (buf, ".startof.%s", secname);
334833965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
334933965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
335033965Sjdp	{
335133965Sjdp	  h->type = bfd_link_hash_defined;
335233965Sjdp	  h->u.def.value = bfd_get_section_vma (output_bfd, s);
335333965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
335433965Sjdp	}
335533965Sjdp
335633965Sjdp      sprintf (buf, ".sizeof.%s", secname);
335733965Sjdp      h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
335833965Sjdp      if (h != NULL && h->type == bfd_link_hash_undefined)
335933965Sjdp	{
336078828Sobrien          unsigned opb;
336178828Sobrien
336278828Sobrien          opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
336378828Sobrien					       ldfile_output_machine);
336433965Sjdp	  h->type = bfd_link_hash_defined;
336533965Sjdp	  if (s->_cooked_size != 0)
336660484Sobrien	    h->u.def.value = s->_cooked_size / opb;
336733965Sjdp	  else
336860484Sobrien	    h->u.def.value = s->_raw_size / opb;
336933965Sjdp	  h->u.def.section = bfd_abs_section_ptr;
337033965Sjdp	}
337133965Sjdp
337233965Sjdp      free (buf);
337333965Sjdp    }
337433965Sjdp}
337533965Sjdp
337633965Sjdpstatic void
337733965Sjdplang_finish ()
337833965Sjdp{
337933965Sjdp  struct bfd_link_hash_entry *h;
338033965Sjdp  boolean warn;
338133965Sjdp
338233965Sjdp  if (link_info.relocateable || link_info.shared)
338333965Sjdp    warn = false;
338433965Sjdp  else
338533965Sjdp    warn = true;
338633965Sjdp
338733965Sjdp  if (entry_symbol == (char *) NULL)
338833965Sjdp    {
338933965Sjdp      /* No entry has been specified.  Look for start, but don't warn
339033965Sjdp	 if we don't find it.  */
339133965Sjdp      entry_symbol = "start";
339233965Sjdp      warn = false;
339333965Sjdp    }
339433965Sjdp
339533965Sjdp  h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
339633965Sjdp  if (h != (struct bfd_link_hash_entry *) NULL
339733965Sjdp      && (h->type == bfd_link_hash_defined
339833965Sjdp	  || h->type == bfd_link_hash_defweak)
339933965Sjdp      && h->u.def.section->output_section != NULL)
340033965Sjdp    {
340133965Sjdp      bfd_vma val;
340233965Sjdp
340333965Sjdp      val = (h->u.def.value
340433965Sjdp	     + bfd_get_section_vma (output_bfd,
340533965Sjdp				    h->u.def.section->output_section)
340633965Sjdp	     + h->u.def.section->output_offset);
340733965Sjdp      if (! bfd_set_start_address (output_bfd, val))
340860484Sobrien	einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
340933965Sjdp    }
341033965Sjdp  else
341133965Sjdp    {
341260484Sobrien      bfd_vma val;
341377298Sobrien      const char *send;
341433965Sjdp
341560484Sobrien      /* We couldn't find the entry symbol.  Try parsing it as a
341660484Sobrien         number.  */
341760484Sobrien      val = bfd_scan_vma (entry_symbol, &send, 0);
341860484Sobrien      if (*send == '\0')
341933965Sjdp	{
342060484Sobrien	  if (! bfd_set_start_address (output_bfd, val))
342160484Sobrien	    einfo (_("%P%F: can't set start address\n"));
342233965Sjdp	}
342333965Sjdp      else
342433965Sjdp	{
342560484Sobrien	  asection *ts;
342660484Sobrien
342760484Sobrien	  /* Can't find the entry symbol, and it's not a number.  Use
342860484Sobrien	     the first address in the text section.  */
342960484Sobrien	  ts = bfd_get_section_by_name (output_bfd, ".text");
343060484Sobrien	  if (ts != (asection *) NULL)
343160484Sobrien	    {
343260484Sobrien	      if (warn)
343360484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
343460484Sobrien		       entry_symbol, bfd_get_section_vma (output_bfd, ts));
343560484Sobrien	      if (! bfd_set_start_address (output_bfd,
343660484Sobrien					   bfd_get_section_vma (output_bfd,
343760484Sobrien								ts)))
343860484Sobrien		einfo (_("%P%F: can't set start address\n"));
343960484Sobrien	    }
344060484Sobrien	  else
344160484Sobrien	    {
344260484Sobrien	      if (warn)
344360484Sobrien		einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
344460484Sobrien		       entry_symbol);
344560484Sobrien	    }
344633965Sjdp	}
344733965Sjdp    }
344833965Sjdp}
344933965Sjdp
345038889Sjdp/* This is a small function used when we want to ignore errors from
345138889Sjdp   BFD.  */
345238889Sjdp
345338889Sjdpstatic void
345438889Sjdp#ifdef ANSI_PROTOTYPES
345560484Sobrienignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
345638889Sjdp#else
345738889Sjdpignore_bfd_errors (s)
345860484Sobrien     const char *s ATTRIBUTE_UNUSED;
345938889Sjdp#endif
346038889Sjdp{
346138889Sjdp  /* Don't do anything.  */
346238889Sjdp}
346338889Sjdp
346433965Sjdp/* Check that the architecture of all the input files is compatible
346533965Sjdp   with the output file.  Also call the backend to let it do any
346633965Sjdp   other checking that is needed.  */
346733965Sjdp
346833965Sjdpstatic void
346933965Sjdplang_check ()
347033965Sjdp{
347133965Sjdp  lang_statement_union_type *file;
347233965Sjdp  bfd *input_bfd;
347377298Sobrien  const bfd_arch_info_type *compatible;
347433965Sjdp
347533965Sjdp  for (file = file_chain.head;
347633965Sjdp       file != (lang_statement_union_type *) NULL;
347733965Sjdp       file = file->input_statement.next)
347833965Sjdp    {
347933965Sjdp      input_bfd = file->input_statement.the_bfd;
348033965Sjdp      compatible = bfd_arch_get_compatible (input_bfd,
348133965Sjdp					    output_bfd);
348233965Sjdp      if (compatible == NULL)
348338889Sjdp	{
348438889Sjdp	  if (command_line.warn_mismatch)
348560484Sobrien	    einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
348638889Sjdp		   bfd_printable_name (input_bfd), input_bfd,
348738889Sjdp		   bfd_printable_name (output_bfd));
348838889Sjdp	}
348977298Sobrien      else if (bfd_count_sections (input_bfd))
349038889Sjdp	{
349177298Sobrien	  /* If the input bfd has no contents, it shouldn't set the
349277298Sobrien	     private data of the output bfd. */
349377298Sobrien
349438889Sjdp	  bfd_error_handler_type pfn = NULL;
349533965Sjdp
349638889Sjdp	  /* If we aren't supposed to warn about mismatched input
349738889Sjdp             files, temporarily set the BFD error handler to a
349838889Sjdp             function which will do nothing.  We still want to call
349938889Sjdp             bfd_merge_private_bfd_data, since it may set up
350038889Sjdp             information which is needed in the output file.  */
350138889Sjdp	  if (! command_line.warn_mismatch)
350238889Sjdp	    pfn = bfd_set_error_handler (ignore_bfd_errors);
350338889Sjdp	  if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
350438889Sjdp	    {
350538889Sjdp	      if (command_line.warn_mismatch)
350660484Sobrien		einfo (_("%E%X: failed to merge target specific data of file %B\n"),
350738889Sjdp		       input_bfd);
350838889Sjdp	    }
350938889Sjdp	  if (! command_line.warn_mismatch)
351038889Sjdp	    bfd_set_error_handler (pfn);
351138889Sjdp	}
351233965Sjdp    }
351333965Sjdp}
351433965Sjdp
351533965Sjdp/* Look through all the global common symbols and attach them to the
351633965Sjdp   correct section.  The -sort-common command line switch may be used
351733965Sjdp   to roughly sort the entries by size.  */
351833965Sjdp
351933965Sjdpstatic void
352033965Sjdplang_common ()
352133965Sjdp{
352233965Sjdp  if (link_info.relocateable
352333965Sjdp      && ! command_line.force_common_definition)
352433965Sjdp    return;
352533965Sjdp
352633965Sjdp  if (! config.sort_common)
352733965Sjdp    bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
352833965Sjdp  else
352933965Sjdp    {
353033965Sjdp      int power;
353133965Sjdp
353233965Sjdp      for (power = 4; power >= 0; power--)
353333965Sjdp	bfd_link_hash_traverse (link_info.hash, lang_one_common,
353433965Sjdp				(PTR) &power);
353533965Sjdp    }
353633965Sjdp}
353733965Sjdp
353833965Sjdp/* Place one common symbol in the correct section.  */
353933965Sjdp
354033965Sjdpstatic boolean
354133965Sjdplang_one_common (h, info)
354233965Sjdp     struct bfd_link_hash_entry *h;
354333965Sjdp     PTR info;
354433965Sjdp{
354533965Sjdp  unsigned int power_of_two;
354633965Sjdp  bfd_vma size;
354733965Sjdp  asection *section;
354877298Sobrien  unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
354977298Sobrien						ldfile_output_machine);
355033965Sjdp
355133965Sjdp  if (h->type != bfd_link_hash_common)
355233965Sjdp    return true;
355333965Sjdp
355433965Sjdp  size = h->u.c.size;
355533965Sjdp  power_of_two = h->u.c.p->alignment_power;
355633965Sjdp
355733965Sjdp  if (config.sort_common
355833965Sjdp      && power_of_two < (unsigned int) *(int *) info)
355933965Sjdp    return true;
356033965Sjdp
356133965Sjdp  section = h->u.c.p->section;
356233965Sjdp
356333965Sjdp  /* Increase the size of the section.  */
356460484Sobrien  section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb,
356560484Sobrien				   (bfd_size_type) (1 << power_of_two)) * opb;
356633965Sjdp
356733965Sjdp  /* Adjust the alignment if necessary.  */
356833965Sjdp  if (power_of_two > section->alignment_power)
356933965Sjdp    section->alignment_power = power_of_two;
357033965Sjdp
357133965Sjdp  /* Change the symbol from common to defined.  */
357233965Sjdp  h->type = bfd_link_hash_defined;
357333965Sjdp  h->u.def.section = section;
357460484Sobrien  h->u.def.value = section->_cooked_size;
357533965Sjdp
357633965Sjdp  /* Increase the size of the section.  */
357760484Sobrien  section->_cooked_size += size;
357833965Sjdp
357933965Sjdp  /* Make sure the section is allocated in memory, and make sure that
358033965Sjdp     it is no longer a common section.  */
358133965Sjdp  section->flags |= SEC_ALLOC;
358277298Sobrien  section->flags &= ~SEC_IS_COMMON;
358333965Sjdp
358433965Sjdp  if (config.map_file != NULL)
358533965Sjdp    {
358633965Sjdp      static boolean header_printed;
358733965Sjdp      int len;
358833965Sjdp      char *name;
358933965Sjdp      char buf[50];
359033965Sjdp
359133965Sjdp      if (! header_printed)
359233965Sjdp	{
359360484Sobrien	  minfo (_("\nAllocating common symbols\n"));
359460484Sobrien	  minfo (_("Common symbol       size              file\n\n"));
359533965Sjdp	  header_printed = true;
359633965Sjdp	}
359733965Sjdp
359833965Sjdp      name = demangle (h->root.string);
359933965Sjdp      minfo ("%s", name);
360033965Sjdp      len = strlen (name);
360133965Sjdp      free (name);
360233965Sjdp
360333965Sjdp      if (len >= 19)
360433965Sjdp	{
360533965Sjdp	  print_nl ();
360633965Sjdp	  len = 0;
360733965Sjdp	}
360833965Sjdp      while (len < 20)
360933965Sjdp	{
361033965Sjdp	  print_space ();
361133965Sjdp	  ++len;
361233965Sjdp	}
361333965Sjdp
361433965Sjdp      minfo ("0x");
361533965Sjdp      if (size <= 0xffffffff)
361633965Sjdp	sprintf (buf, "%lx", (unsigned long) size);
361733965Sjdp      else
361833965Sjdp	sprintf_vma (buf, size);
361933965Sjdp      minfo ("%s", buf);
362033965Sjdp      len = strlen (buf);
362133965Sjdp
362233965Sjdp      while (len < 16)
362333965Sjdp	{
362433965Sjdp	  print_space ();
362533965Sjdp	  ++len;
362633965Sjdp	}
362733965Sjdp
362833965Sjdp      minfo ("%B\n", section->owner);
362933965Sjdp    }
363033965Sjdp
363133965Sjdp  return true;
363233965Sjdp}
363333965Sjdp
363477298Sobrien/* Run through the input files and ensure that every input section has
363577298Sobrien   somewhere to go.  If one is found without a destination then create
363677298Sobrien   an input request and place it into the statement tree.  */
363733965Sjdp
363833965Sjdpstatic void
363933965Sjdplang_place_orphans ()
364033965Sjdp{
364160484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (file)
364233965Sjdp    {
364333965Sjdp      asection *s;
364433965Sjdp
364533965Sjdp      for (s = file->the_bfd->sections;
364633965Sjdp	   s != (asection *) NULL;
364733965Sjdp	   s = s->next)
364833965Sjdp	{
364933965Sjdp	  if (s->output_section == (asection *) NULL)
365033965Sjdp	    {
365133965Sjdp	      /* This section of the file is not attatched, root
365277298Sobrien	         around for a sensible place for it to go.  */
365333965Sjdp
365433965Sjdp	      if (file->just_syms_flag)
365533965Sjdp		{
365633965Sjdp		  /* We are only retrieving symbol values from this
365733965Sjdp                     file.  We want the symbols to act as though the
365833965Sjdp                     values in the file are absolute.  */
365933965Sjdp		  s->output_section = bfd_abs_section_ptr;
366033965Sjdp		  s->output_offset = s->vma;
366133965Sjdp		}
366233965Sjdp	      else if (strcmp (s->name, "COMMON") == 0)
366333965Sjdp		{
366433965Sjdp		  /* This is a lonely common section which must have
366533965Sjdp		     come from an archive.  We attach to the section
366633965Sjdp		     with the wildcard.  */
366733965Sjdp		  if (! link_info.relocateable
366833965Sjdp		      || command_line.force_common_definition)
366933965Sjdp		    {
367033965Sjdp		      if (default_common_section == NULL)
367133965Sjdp			{
367233965Sjdp#if 0
367333965Sjdp			  /* This message happens when using the
367433965Sjdp                             svr3.ifile linker script, so I have
367533965Sjdp                             disabled it.  */
367660484Sobrien			  info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
367733965Sjdp#endif
367833965Sjdp			  default_common_section =
367933965Sjdp			    lang_output_section_statement_lookup (".bss");
368033965Sjdp
368133965Sjdp			}
368233965Sjdp		      wild_doit (&default_common_section->children, s,
368333965Sjdp				 default_common_section, file);
368433965Sjdp		    }
368533965Sjdp		}
368633965Sjdp	      else if (ldemul_place_orphan (file, s))
368733965Sjdp		;
368833965Sjdp	      else
368933965Sjdp		{
369033965Sjdp		  lang_output_section_statement_type *os =
369133965Sjdp		  lang_output_section_statement_lookup (s->name);
369233965Sjdp
369333965Sjdp		  wild_doit (&os->children, s, os, file);
369433965Sjdp		}
369533965Sjdp	    }
369633965Sjdp	}
369733965Sjdp    }
369833965Sjdp}
369933965Sjdp
370033965Sjdpvoid
370160484Sobrienlang_set_flags (ptr, flags, invert)
370238889Sjdp     lang_memory_region_type *ptr;
370377298Sobrien     const char *flags;
370460484Sobrien     int invert;
370533965Sjdp{
370660484Sobrien  flagword *ptr_flags;
370733965Sjdp
370860484Sobrien  ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
370933965Sjdp  while (*flags)
371033965Sjdp    {
371133965Sjdp      switch (*flags)
371233965Sjdp	{
371338889Sjdp	case 'A': case 'a':
371438889Sjdp	  *ptr_flags |= SEC_ALLOC;
371533965Sjdp	  break;
371638889Sjdp
371738889Sjdp	case 'R': case 'r':
371838889Sjdp	  *ptr_flags |= SEC_READONLY;
371933965Sjdp	  break;
372038889Sjdp
372138889Sjdp	case 'W': case 'w':
372238889Sjdp	  *ptr_flags |= SEC_DATA;
372333965Sjdp	  break;
372438889Sjdp
372538889Sjdp	case 'X': case 'x':
372638889Sjdp	  *ptr_flags |= SEC_CODE;
372738889Sjdp	  break;
372838889Sjdp
372938889Sjdp	case 'L': case 'l':
373038889Sjdp	case 'I': case 'i':
373138889Sjdp	  *ptr_flags |= SEC_LOAD;
373238889Sjdp	  break;
373338889Sjdp
373433965Sjdp	default:
373560484Sobrien	  einfo (_("%P%F: invalid syntax in flags\n"));
373633965Sjdp	  break;
373733965Sjdp	}
373833965Sjdp      flags++;
373933965Sjdp    }
374033965Sjdp}
374133965Sjdp
374233965Sjdp/* Call a function on each input file.  This function will be called
374333965Sjdp   on an archive, but not on the elements.  */
374433965Sjdp
374533965Sjdpvoid
374633965Sjdplang_for_each_input_file (func)
374733965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
374833965Sjdp{
374933965Sjdp  lang_input_statement_type *f;
375033965Sjdp
375133965Sjdp  for (f = (lang_input_statement_type *) input_file_chain.head;
375233965Sjdp       f != NULL;
375333965Sjdp       f = (lang_input_statement_type *) f->next_real_file)
375433965Sjdp    func (f);
375533965Sjdp}
375633965Sjdp
375733965Sjdp/* Call a function on each file.  The function will be called on all
375833965Sjdp   the elements of an archive which are included in the link, but will
375933965Sjdp   not be called on the archive file itself.  */
376033965Sjdp
376133965Sjdpvoid
376233965Sjdplang_for_each_file (func)
376333965Sjdp     void (*func) PARAMS ((lang_input_statement_type *));
376433965Sjdp{
376560484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
376633965Sjdp    {
376733965Sjdp      func (f);
376833965Sjdp    }
376933965Sjdp}
377033965Sjdp
377133965Sjdp#if 0
377233965Sjdp
377333965Sjdp/* Not used.  */
377433965Sjdp
377533965Sjdpvoid
377633965Sjdplang_for_each_input_section (func)
377777298Sobrien     void (*func) PARAMS ((bfd *ab, asection *as));
377833965Sjdp{
377960484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (f)
378033965Sjdp    {
378177298Sobrien      asection *s;
378233965Sjdp
378333965Sjdp      for (s = f->the_bfd->sections;
378433965Sjdp	   s != (asection *) NULL;
378533965Sjdp	   s = s->next)
378633965Sjdp	{
378733965Sjdp	  func (f->the_bfd, s);
378833965Sjdp	}
378933965Sjdp    }
379033965Sjdp}
379133965Sjdp
379233965Sjdp#endif
379333965Sjdp
379433965Sjdpvoid
379533965Sjdpldlang_add_file (entry)
379677298Sobrien     lang_input_statement_type *entry;
379733965Sjdp{
379833965Sjdp  bfd **pp;
379933965Sjdp
380033965Sjdp  lang_statement_append (&file_chain,
380133965Sjdp			 (lang_statement_union_type *) entry,
380233965Sjdp			 &entry->next);
380333965Sjdp
380433965Sjdp  /* The BFD linker needs to have a list of all input BFDs involved in
380533965Sjdp     a link.  */
380633965Sjdp  ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
380733965Sjdp  ASSERT (entry->the_bfd != output_bfd);
380833965Sjdp  for (pp = &link_info.input_bfds;
380933965Sjdp       *pp != (bfd *) NULL;
381033965Sjdp       pp = &(*pp)->link_next)
381133965Sjdp    ;
381233965Sjdp  *pp = entry->the_bfd;
381333965Sjdp  entry->the_bfd->usrdata = (PTR) entry;
381433965Sjdp  bfd_set_gp_size (entry->the_bfd, g_switch_value);
381533965Sjdp
381633965Sjdp  /* Look through the sections and check for any which should not be
381733965Sjdp     included in the link.  We need to do this now, so that we can
381833965Sjdp     notice when the backend linker tries to report multiple
381933965Sjdp     definition errors for symbols which are in sections we aren't
382033965Sjdp     going to link.  FIXME: It might be better to entirely ignore
382133965Sjdp     symbols which are defined in sections which are going to be
382233965Sjdp     discarded.  This would require modifying the backend linker for
382333965Sjdp     each backend which might set the SEC_LINK_ONCE flag.  If we do
382433965Sjdp     this, we should probably handle SEC_EXCLUDE in the same way.  */
382533965Sjdp
382633965Sjdp  bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
382733965Sjdp}
382833965Sjdp
382933965Sjdpvoid
383033965Sjdplang_add_output (name, from_script)
383177298Sobrien     const char *name;
383233965Sjdp     int from_script;
383333965Sjdp{
383433965Sjdp  /* Make -o on command line override OUTPUT in script.  */
383533965Sjdp  if (had_output_filename == false || !from_script)
383633965Sjdp    {
383733965Sjdp      output_filename = name;
383833965Sjdp      had_output_filename = true;
383933965Sjdp    }
384033965Sjdp}
384133965Sjdp
384233965Sjdpstatic lang_output_section_statement_type *current_section;
384333965Sjdp
384433965Sjdpstatic int
384533965Sjdptopower (x)
384633965Sjdp     int x;
384733965Sjdp{
384833965Sjdp  unsigned int i = 1;
384933965Sjdp  int l;
385033965Sjdp
385133965Sjdp  if (x < 0)
385233965Sjdp    return -1;
385333965Sjdp
385477298Sobrien  for (l = 0; l < 32; l++)
385533965Sjdp    {
385633965Sjdp      if (i >= (unsigned int) x)
385733965Sjdp	return l;
385833965Sjdp      i <<= 1;
385933965Sjdp    }
386033965Sjdp
386133965Sjdp  return 0;
386233965Sjdp}
386333965Sjdp
386468765Sobrienlang_output_section_statement_type *
386533965Sjdplang_enter_output_section_statement (output_section_statement_name,
386633965Sjdp				     address_exp, sectype, block_value,
386733965Sjdp				     align, subalign, ebase)
386833965Sjdp     const char *output_section_statement_name;
386977298Sobrien     etree_type *address_exp;
387033965Sjdp     enum section_type sectype;
387133965Sjdp     bfd_vma block_value;
387233965Sjdp     etree_type *align;
387333965Sjdp     etree_type *subalign;
387433965Sjdp     etree_type *ebase;
387533965Sjdp{
387633965Sjdp  lang_output_section_statement_type *os;
387733965Sjdp
387833965Sjdp  current_section =
387933965Sjdp   os =
388033965Sjdp    lang_output_section_statement_lookup (output_section_statement_name);
388133965Sjdp
388277298Sobrien  /* Add this statement to tree.  */
388377298Sobrien#if 0
388477298Sobrien  add_statement (lang_output_section_statement_enum,
388577298Sobrien		 output_section_statement);
388677298Sobrien#endif
388777298Sobrien  /* Make next things chain into subchain of this.  */
388833965Sjdp
388977298Sobrien  if (os->addr_tree == (etree_type *) NULL)
389077298Sobrien    {
389177298Sobrien      os->addr_tree = address_exp;
389277298Sobrien    }
389333965Sjdp  os->sectype = sectype;
389433965Sjdp  if (sectype != noload_section)
389533965Sjdp    os->flags = SEC_NO_FLAGS;
389633965Sjdp  else
389733965Sjdp    os->flags = SEC_NEVER_LOAD;
389833965Sjdp  os->block_value = block_value ? block_value : 1;
389933965Sjdp  stat_ptr = &os->children;
390033965Sjdp
390177298Sobrien  os->subsection_alignment =
390277298Sobrien    topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
390377298Sobrien  os->section_alignment =
390477298Sobrien    topower (exp_get_value_int (align, -1, "section alignment", 0));
390533965Sjdp
390633965Sjdp  os->load_base = ebase;
390768765Sobrien  return os;
390833965Sjdp}
390933965Sjdp
391033965Sjdpvoid
391133965Sjdplang_final ()
391233965Sjdp{
391333965Sjdp  lang_output_statement_type *new =
391433965Sjdp    new_stat (lang_output_statement, stat_ptr);
391533965Sjdp
391633965Sjdp  new->name = output_filename;
391733965Sjdp}
391833965Sjdp
391977298Sobrien/* Reset the current counters in the regions.  */
392077298Sobrien
392133965Sjdpstatic void
392233965Sjdpreset_memory_regions ()
392333965Sjdp{
392433965Sjdp  lang_memory_region_type *p = lang_memory_region_list;
392533965Sjdp
392633965Sjdp  for (p = lang_memory_region_list;
392733965Sjdp       p != (lang_memory_region_type *) NULL;
392833965Sjdp       p = p->next)
392933965Sjdp    {
393033965Sjdp      p->old_length = (bfd_size_type) (p->current - p->origin);
393133965Sjdp      p->current = p->origin;
393233965Sjdp    }
393333965Sjdp}
393433965Sjdp
393560484Sobrien/* Expand a wild statement for a particular FILE, marking its sections KEEP
393660484Sobrien   as needed.  SECTION may be NULL, in which case it is a wild card.  */
393760484Sobrien
393860484Sobrienstatic void
393960484Sobriengc_section_callback (ptr, section, file, data)
394060484Sobrien     lang_wild_statement_type *ptr;
394160484Sobrien     asection *section;
394260484Sobrien     lang_input_statement_type *file ATTRIBUTE_UNUSED;
394377298Sobrien     PTR data ATTRIBUTE_UNUSED;
394460484Sobrien{
394560484Sobrien  /* If the wild pattern was marked KEEP, the member sections
394660484Sobrien     should be as well.  */
394760484Sobrien  if (ptr->keep_sections)
394860484Sobrien    section->flags |= SEC_KEEP;
394960484Sobrien}
395060484Sobrien
395160484Sobrien/* Handle a wild statement, marking it against GC.  SECTION or FILE or both
395260484Sobrien   may be NULL, indicating that it is a wildcard.  */
395360484Sobrien
395460484Sobrienstatic void
395560484Sobrienlang_gc_wild (s, section, file)
395660484Sobrien     lang_wild_statement_type *s;
395760484Sobrien     const char *section;
395860484Sobrien     const char *file;
395960484Sobrien{
396060484Sobrien  walk_wild (s, section, file, gc_section_callback, NULL);
396160484Sobrien}
396260484Sobrien
396360484Sobrien/* Iterate over sections marking them against GC.  */
396460484Sobrien
396560484Sobrienstatic void
396660484Sobrienlang_gc_sections_1 (s)
396777298Sobrien     lang_statement_union_type *s;
396860484Sobrien{
396960484Sobrien  for (; s != (lang_statement_union_type *) NULL; s = s->next)
397060484Sobrien    {
397160484Sobrien      switch (s->header.type)
397260484Sobrien	{
397360484Sobrien	case lang_wild_statement_enum:
397460484Sobrien	  lang_gc_wild (&s->wild_statement,
397560484Sobrien			s->wild_statement.section_name,
397660484Sobrien			s->wild_statement.filename);
397760484Sobrien	  break;
397860484Sobrien	case lang_constructors_statement_enum:
397960484Sobrien	  lang_gc_sections_1 (constructor_list.head);
398060484Sobrien	  break;
398160484Sobrien	case lang_output_section_statement_enum:
398260484Sobrien	  lang_gc_sections_1 (s->output_section_statement.children.head);
398360484Sobrien	  break;
398460484Sobrien	case lang_group_statement_enum:
398560484Sobrien	  lang_gc_sections_1 (s->group_statement.children.head);
398660484Sobrien	  break;
398760484Sobrien	default:
398860484Sobrien	  break;
398960484Sobrien	}
399060484Sobrien    }
399160484Sobrien}
399260484Sobrien
399360484Sobrienstatic void
399460484Sobrienlang_gc_sections ()
399560484Sobrien{
399660484Sobrien  struct bfd_link_hash_entry *h;
399760484Sobrien  ldlang_undef_chain_list_type *ulist, fake_list_start;
399860484Sobrien
399960484Sobrien  /* Keep all sections so marked in the link script.  */
400060484Sobrien
400160484Sobrien  lang_gc_sections_1 (statement_list.head);
400260484Sobrien
400360484Sobrien  /* Keep all sections containing symbols undefined on the command-line.
400460484Sobrien     Handle the entry symbol at the same time.  */
400577298Sobrien
400660484Sobrien  if (entry_symbol != NULL)
400760484Sobrien    {
400860484Sobrien      fake_list_start.next = ldlang_undef_chain_list_head;
400960484Sobrien      fake_list_start.name = (char *) entry_symbol;
401060484Sobrien      ulist = &fake_list_start;
401160484Sobrien    }
401260484Sobrien  else
401360484Sobrien    ulist = ldlang_undef_chain_list_head;
401460484Sobrien
401560484Sobrien  for (; ulist; ulist = ulist->next)
401660484Sobrien    {
401777298Sobrien      h = bfd_link_hash_lookup (link_info.hash, ulist->name,
401860484Sobrien				false, false, false);
401960484Sobrien
402060484Sobrien      if (h != (struct bfd_link_hash_entry *) NULL
402177298Sobrien	  && (h->type == bfd_link_hash_defined
402277298Sobrien	      || h->type == bfd_link_hash_defweak)
402360484Sobrien	  && ! bfd_is_abs_section (h->u.def.section))
402460484Sobrien	{
402560484Sobrien	  h->u.def.section->flags |= SEC_KEEP;
402660484Sobrien	}
402760484Sobrien    }
402860484Sobrien
402960484Sobrien  bfd_gc_sections (output_bfd, &link_info);
403060484Sobrien}
403160484Sobrien
403233965Sjdpvoid
403333965Sjdplang_process ()
403433965Sjdp{
403533965Sjdp  lang_reasonable_defaults ();
403633965Sjdp  current_target = default_target;
403733965Sjdp
403877298Sobrien  /* Open the output file.  */
403977298Sobrien  lang_for_each_statement (ldlang_open_output);
404033965Sjdp
404133965Sjdp  ldemul_create_output_section_statements ();
404233965Sjdp
404377298Sobrien  /* Add to the hash table all undefineds on the command line.  */
404433965Sjdp  lang_place_undefineds ();
404533965Sjdp
404660484Sobrien  already_linked_table_init ();
404760484Sobrien
404877298Sobrien  /* Create a bfd for each input file.  */
404933965Sjdp  current_target = default_target;
405033965Sjdp  open_input_bfds (statement_list.head, false);
405133965Sjdp
405233965Sjdp  ldemul_after_open ();
405333965Sjdp
405460484Sobrien  already_linked_table_free ();
405560484Sobrien
405633965Sjdp  /* Make sure that we're not mixing architectures.  We call this
405733965Sjdp     after all the input files have been opened, but before we do any
405833965Sjdp     other processing, so that any operations merge_private_bfd_data
405933965Sjdp     does on the output file will be known during the rest of the
406033965Sjdp     link.  */
406133965Sjdp  lang_check ();
406233965Sjdp
406360484Sobrien  /* Handle .exports instead of a version script if we're told to do so.  */
406460484Sobrien  if (command_line.version_exports_section)
406560484Sobrien    lang_do_version_exports_section ();
406660484Sobrien
406733965Sjdp  /* Build all sets based on the information gathered from the input
406833965Sjdp     files.  */
406933965Sjdp  ldctor_build_sets ();
407033965Sjdp
407160484Sobrien  /* Remove unreferenced sections if asked to.  */
407260484Sobrien  if (command_line.gc_sections)
407360484Sobrien    lang_gc_sections ();
407460484Sobrien
407577298Sobrien  /* Size up the common data.  */
407633965Sjdp  lang_common ();
407733965Sjdp
407833965Sjdp  /* Run through the contours of the script and attach input sections
407977298Sobrien     to the correct output sections.  */
408033965Sjdp  map_input_to_output_sections (statement_list.head, (char *) NULL,
408133965Sjdp				(lang_output_section_statement_type *) NULL);
408233965Sjdp
408377298Sobrien  /* Find any sections not attached explicitly and handle them.  */
408433965Sjdp  lang_place_orphans ();
408533965Sjdp
408633965Sjdp  ldemul_before_allocation ();
408733965Sjdp
408833965Sjdp  /* We must record the program headers before we try to fix the
408933965Sjdp     section positions, since they will affect SIZEOF_HEADERS.  */
409033965Sjdp  lang_record_phdrs ();
409133965Sjdp
409277298Sobrien  /* Now run around and relax if we can.  */
409333965Sjdp  if (command_line.relax)
409433965Sjdp    {
409533965Sjdp      /* First time round is a trial run to get the 'worst case'
409633965Sjdp	 addresses of the objects if there was no relaxing.  */
409733965Sjdp      lang_size_sections (statement_list.head,
409833965Sjdp			  abs_output_section,
409933965Sjdp			  &(statement_list.head), 0, (bfd_vma) 0, false);
410033965Sjdp
410133965Sjdp      /* Keep relaxing until bfd_relax_section gives up.  */
410233965Sjdp      do
410333965Sjdp	{
410433965Sjdp	  reset_memory_regions ();
410533965Sjdp
410633965Sjdp	  relax_again = false;
410733965Sjdp
410860484Sobrien	  /* Note: pe-dll.c does something like this also.  If you find
410960484Sobrien	     you need to change this code, you probably need to change
411077298Sobrien	     pe-dll.c also.  DJ  */
411160484Sobrien
411233965Sjdp	  /* Do all the assignments with our current guesses as to
411333965Sjdp	     section sizes.  */
411433965Sjdp	  lang_do_assignments (statement_list.head,
411533965Sjdp			       abs_output_section,
411633965Sjdp			       (fill_type) 0, (bfd_vma) 0);
411733965Sjdp
411833965Sjdp	  /* Perform another relax pass - this time we know where the
411933965Sjdp	     globals are, so can make better guess.  */
412033965Sjdp	  lang_size_sections (statement_list.head,
412133965Sjdp			      abs_output_section,
412233965Sjdp			      &(statement_list.head), 0, (bfd_vma) 0, true);
412333965Sjdp	}
412433965Sjdp      while (relax_again);
412533965Sjdp    }
412633965Sjdp  else
412733965Sjdp    {
412833965Sjdp      /* Size up the sections.  */
412933965Sjdp      lang_size_sections (statement_list.head,
413033965Sjdp			  abs_output_section,
413133965Sjdp			  &(statement_list.head), 0, (bfd_vma) 0, false);
413233965Sjdp    }
413333965Sjdp
413433965Sjdp  /* See if anything special should be done now we know how big
413533965Sjdp     everything is.  */
413633965Sjdp  ldemul_after_allocation ();
413733965Sjdp
413833965Sjdp  /* Fix any .startof. or .sizeof. symbols.  */
413933965Sjdp  lang_set_startof ();
414033965Sjdp
414177298Sobrien  /* Do all the assignments, now that we know the final resting places
414277298Sobrien     of all the symbols.  */
414333965Sjdp
414433965Sjdp  lang_do_assignments (statement_list.head,
414533965Sjdp		       abs_output_section,
414633965Sjdp		       (fill_type) 0, (bfd_vma) 0);
414733965Sjdp
414860484Sobrien  /* Make sure that the section addresses make sense.  */
414960484Sobrien  if (! link_info.relocateable
415060484Sobrien      && command_line.check_section_addresses)
415160484Sobrien    lang_check_section_addresses ();
415233965Sjdp
415377298Sobrien  /* Final stuffs.  */
415477298Sobrien
415533965Sjdp  ldemul_finish ();
415633965Sjdp  lang_finish ();
415733965Sjdp}
415833965Sjdp
415933965Sjdp/* EXPORTED TO YACC */
416033965Sjdp
416133965Sjdpvoid
416260484Sobrienlang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
416360484Sobrien	       keep_sections, exclude_filename_list)
416460484Sobrien     const char *const section_name;
416560484Sobrien     boolean sections_sorted;
416660484Sobrien     const char *const filename;
416760484Sobrien     boolean filenames_sorted;
416860484Sobrien     boolean keep_sections;
416960484Sobrien     struct name_list *exclude_filename_list;
417033965Sjdp{
417133965Sjdp  lang_wild_statement_type *new = new_stat (lang_wild_statement,
417233965Sjdp					    stat_ptr);
417333965Sjdp
417433965Sjdp  if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
417533965Sjdp    {
417633965Sjdp      placed_commons = true;
417733965Sjdp    }
417860484Sobrien  if (filename != NULL && ! wildcardp (filename))
417933965Sjdp    {
418033965Sjdp      lang_has_input_file = true;
418133965Sjdp    }
418233965Sjdp  new->section_name = section_name;
418360484Sobrien  new->sections_sorted = sections_sorted;
418433965Sjdp  new->filename = filename;
418560484Sobrien  new->filenames_sorted = filenames_sorted;
418660484Sobrien  new->keep_sections = keep_sections;
418760484Sobrien  new->exclude_filename_list = exclude_filename_list;
418833965Sjdp  lang_list_init (&new->children);
418933965Sjdp}
419033965Sjdp
419133965Sjdpvoid
419233965Sjdplang_section_start (name, address)
419377298Sobrien     const char *name;
419477298Sobrien     etree_type *address;
419533965Sjdp{
419678828Sobrien  lang_address_statement_type *ad;
419733965Sjdp
419878828Sobrien  ad = new_stat (lang_address_statement, stat_ptr);
419933965Sjdp  ad->section_name = name;
420033965Sjdp  ad->address = address;
420133965Sjdp}
420233965Sjdp
420333965Sjdp/* Set the start symbol to NAME.  CMDLINE is nonzero if this is called
420433965Sjdp   because of a -e argument on the command line, or zero if this is
420533965Sjdp   called by ENTRY in a linker script.  Command line arguments take
420633965Sjdp   precedence.  */
420733965Sjdp
420833965Sjdpvoid
420933965Sjdplang_add_entry (name, cmdline)
421077298Sobrien     const char *name;
421133965Sjdp     boolean cmdline;
421233965Sjdp{
421333965Sjdp  if (entry_symbol == NULL
421433965Sjdp      || cmdline
421533965Sjdp      || ! entry_from_cmdline)
421633965Sjdp    {
421733965Sjdp      entry_symbol = name;
421833965Sjdp      entry_from_cmdline = cmdline;
421933965Sjdp    }
422033965Sjdp}
422133965Sjdp
422233965Sjdpvoid
422333965Sjdplang_add_target (name)
422477298Sobrien     const char *name;
422533965Sjdp{
422633965Sjdp  lang_target_statement_type *new = new_stat (lang_target_statement,
422733965Sjdp					      stat_ptr);
422833965Sjdp
422933965Sjdp  new->target = name;
423033965Sjdp
423133965Sjdp}
423233965Sjdp
423333965Sjdpvoid
423433965Sjdplang_add_map (name)
423577298Sobrien     const char *name;
423633965Sjdp{
423733965Sjdp  while (*name)
423833965Sjdp    {
423933965Sjdp      switch (*name)
424033965Sjdp	{
424177298Sobrien	case 'F':
424233965Sjdp	  map_option_f = true;
424333965Sjdp	  break;
424433965Sjdp	}
424533965Sjdp      name++;
424633965Sjdp    }
424733965Sjdp}
424833965Sjdp
424933965Sjdpvoid
425033965Sjdplang_add_fill (exp)
425133965Sjdp     int exp;
425233965Sjdp{
425333965Sjdp  lang_fill_statement_type *new = new_stat (lang_fill_statement,
425433965Sjdp					    stat_ptr);
425533965Sjdp
425633965Sjdp  new->fill = exp;
425733965Sjdp}
425833965Sjdp
425933965Sjdpvoid
426033965Sjdplang_add_data (type, exp)
426133965Sjdp     int type;
426233965Sjdp     union etree_union *exp;
426333965Sjdp{
426433965Sjdp
426533965Sjdp  lang_data_statement_type *new = new_stat (lang_data_statement,
426633965Sjdp					    stat_ptr);
426733965Sjdp
426833965Sjdp  new->exp = exp;
426933965Sjdp  new->type = type;
427033965Sjdp
427133965Sjdp}
427233965Sjdp
427333965Sjdp/* Create a new reloc statement.  RELOC is the BFD relocation type to
427433965Sjdp   generate.  HOWTO is the corresponding howto structure (we could
427533965Sjdp   look this up, but the caller has already done so).  SECTION is the
427633965Sjdp   section to generate a reloc against, or NAME is the name of the
427733965Sjdp   symbol to generate a reloc against.  Exactly one of SECTION and
427833965Sjdp   NAME must be NULL.  ADDEND is an expression for the addend.  */
427933965Sjdp
428033965Sjdpvoid
428133965Sjdplang_add_reloc (reloc, howto, section, name, addend)
428233965Sjdp     bfd_reloc_code_real_type reloc;
428333965Sjdp     reloc_howto_type *howto;
428433965Sjdp     asection *section;
428533965Sjdp     const char *name;
428633965Sjdp     union etree_union *addend;
428733965Sjdp{
428833965Sjdp  lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
428977298Sobrien
429033965Sjdp  p->reloc = reloc;
429133965Sjdp  p->howto = howto;
429233965Sjdp  p->section = section;
429333965Sjdp  p->name = name;
429433965Sjdp  p->addend_exp = addend;
429533965Sjdp
429633965Sjdp  p->addend_value = 0;
429733965Sjdp  p->output_section = NULL;
429833965Sjdp  p->output_vma = 0;
429933965Sjdp}
430033965Sjdp
430160484Sobrienlang_assignment_statement_type *
430233965Sjdplang_add_assignment (exp)
430377298Sobrien     etree_type *exp;
430433965Sjdp{
430533965Sjdp  lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
430633965Sjdp						  stat_ptr);
430733965Sjdp
430833965Sjdp  new->exp = exp;
430960484Sobrien  return new;
431033965Sjdp}
431133965Sjdp
431233965Sjdpvoid
431333965Sjdplang_add_attribute (attribute)
431433965Sjdp     enum statement_enum attribute;
431533965Sjdp{
431633965Sjdp  new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
431733965Sjdp}
431833965Sjdp
431933965Sjdpvoid
432033965Sjdplang_startup (name)
432177298Sobrien     const char *name;
432233965Sjdp{
432333965Sjdp  if (startup_file != (char *) NULL)
432433965Sjdp    {
432560484Sobrien      einfo (_("%P%Fmultiple STARTUP files\n"));
432633965Sjdp    }
432733965Sjdp  first_file->filename = name;
432833965Sjdp  first_file->local_sym_name = name;
432933965Sjdp  first_file->real = true;
433033965Sjdp
433133965Sjdp  startup_file = name;
433233965Sjdp}
433333965Sjdp
433433965Sjdpvoid
433533965Sjdplang_float (maybe)
433633965Sjdp     boolean maybe;
433733965Sjdp{
433833965Sjdp  lang_float_flag = maybe;
433933965Sjdp}
434033965Sjdp
434133965Sjdpvoid
434260484Sobrienlang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
434333965Sjdp     bfd_vma fill;
434433965Sjdp     const char *memspec;
434533965Sjdp     struct lang_output_section_phdr_list *phdrs;
434660484Sobrien     const char *lma_memspec;
434733965Sjdp{
434833965Sjdp  current_section->fill = fill;
434933965Sjdp  current_section->region = lang_memory_region_lookup (memspec);
435060484Sobrien  if (strcmp (lma_memspec, "*default*") != 0)
435160484Sobrien    {
435260484Sobrien      current_section->lma_region = lang_memory_region_lookup (lma_memspec);
435377298Sobrien      /* If no runtime region has been given, but the load region has
435477298Sobrien         been, use the load region.  */
435560484Sobrien      if (strcmp (memspec, "*default*") == 0)
435660484Sobrien        current_section->region = lang_memory_region_lookup (lma_memspec);
435760484Sobrien    }
435833965Sjdp  current_section->phdrs = phdrs;
435933965Sjdp  stat_ptr = &statement_list;
436033965Sjdp}
436133965Sjdp
436277298Sobrien/* Create an absolute symbol with the given name with the value of the
436377298Sobrien   address of first byte of the section named.
436433965Sjdp
436577298Sobrien   If the symbol already exists, then do nothing.  */
436677298Sobrien
436733965Sjdpvoid
436833965Sjdplang_abs_symbol_at_beginning_of (secname, name)
436933965Sjdp     const char *secname;
437033965Sjdp     const char *name;
437133965Sjdp{
437233965Sjdp  struct bfd_link_hash_entry *h;
437333965Sjdp
437433965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
437533965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
437660484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
437733965Sjdp
437833965Sjdp  if (h->type == bfd_link_hash_new
437933965Sjdp      || h->type == bfd_link_hash_undefined)
438033965Sjdp    {
438133965Sjdp      asection *sec;
438233965Sjdp
438333965Sjdp      h->type = bfd_link_hash_defined;
438433965Sjdp
438533965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
438633965Sjdp      if (sec == (asection *) NULL)
438733965Sjdp	h->u.def.value = 0;
438833965Sjdp      else
438933965Sjdp	h->u.def.value = bfd_get_section_vma (output_bfd, sec);
439033965Sjdp
439133965Sjdp      h->u.def.section = bfd_abs_section_ptr;
439233965Sjdp    }
439333965Sjdp}
439433965Sjdp
439577298Sobrien/* Create an absolute symbol with the given name with the value of the
439677298Sobrien   address of the first byte after the end of the section named.
439733965Sjdp
439877298Sobrien   If the symbol already exists, then do nothing.  */
439977298Sobrien
440033965Sjdpvoid
440133965Sjdplang_abs_symbol_at_end_of (secname, name)
440233965Sjdp     const char *secname;
440333965Sjdp     const char *name;
440433965Sjdp{
440533965Sjdp  struct bfd_link_hash_entry *h;
440633965Sjdp
440733965Sjdp  h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
440833965Sjdp  if (h == (struct bfd_link_hash_entry *) NULL)
440960484Sobrien    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
441033965Sjdp
441133965Sjdp  if (h->type == bfd_link_hash_new
441233965Sjdp      || h->type == bfd_link_hash_undefined)
441333965Sjdp    {
441433965Sjdp      asection *sec;
441533965Sjdp
441633965Sjdp      h->type = bfd_link_hash_defined;
441733965Sjdp
441833965Sjdp      sec = bfd_get_section_by_name (output_bfd, secname);
441933965Sjdp      if (sec == (asection *) NULL)
442033965Sjdp	h->u.def.value = 0;
442133965Sjdp      else
442233965Sjdp	h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
442360484Sobrien			  + bfd_section_size (output_bfd, sec) /
442460484Sobrien                          bfd_octets_per_byte (output_bfd));
442533965Sjdp
442633965Sjdp      h->u.def.section = bfd_abs_section_ptr;
442733965Sjdp    }
442833965Sjdp}
442933965Sjdp
443033965Sjdpvoid
443133965Sjdplang_statement_append (list, element, field)
443277298Sobrien     lang_statement_list_type *list;
443377298Sobrien     lang_statement_union_type *element;
443477298Sobrien     lang_statement_union_type **field;
443533965Sjdp{
443633965Sjdp  *(list->tail) = element;
443733965Sjdp  list->tail = field;
443833965Sjdp}
443933965Sjdp
444033965Sjdp/* Set the output format type.  -oformat overrides scripts.  */
444133965Sjdp
444233965Sjdpvoid
444333965Sjdplang_add_output_format (format, big, little, from_script)
444433965Sjdp     const char *format;
444533965Sjdp     const char *big;
444633965Sjdp     const char *little;
444733965Sjdp     int from_script;
444833965Sjdp{
444933965Sjdp  if (output_target == NULL || !from_script)
445033965Sjdp    {
445133965Sjdp      if (command_line.endian == ENDIAN_BIG
445233965Sjdp	  && big != NULL)
445333965Sjdp	format = big;
445433965Sjdp      else if (command_line.endian == ENDIAN_LITTLE
445533965Sjdp	       && little != NULL)
445633965Sjdp	format = little;
445733965Sjdp
445833965Sjdp      output_target = format;
445933965Sjdp    }
446033965Sjdp}
446133965Sjdp
446233965Sjdp/* Enter a group.  This creates a new lang_group_statement, and sets
446333965Sjdp   stat_ptr to build new statements within the group.  */
446433965Sjdp
446533965Sjdpvoid
446633965Sjdplang_enter_group ()
446733965Sjdp{
446833965Sjdp  lang_group_statement_type *g;
446933965Sjdp
447033965Sjdp  g = new_stat (lang_group_statement, stat_ptr);
447133965Sjdp  lang_list_init (&g->children);
447233965Sjdp  stat_ptr = &g->children;
447333965Sjdp}
447433965Sjdp
447533965Sjdp/* Leave a group.  This just resets stat_ptr to start writing to the
447633965Sjdp   regular list of statements again.  Note that this will not work if
447733965Sjdp   groups can occur inside anything else which can adjust stat_ptr,
447833965Sjdp   but currently they can't.  */
447933965Sjdp
448033965Sjdpvoid
448133965Sjdplang_leave_group ()
448233965Sjdp{
448333965Sjdp  stat_ptr = &statement_list;
448433965Sjdp}
448533965Sjdp
448633965Sjdp/* Add a new program header.  This is called for each entry in a PHDRS
448733965Sjdp   command in a linker script.  */
448833965Sjdp
448933965Sjdpvoid
449033965Sjdplang_new_phdr (name, type, filehdr, phdrs, at, flags)
449133965Sjdp     const char *name;
449233965Sjdp     etree_type *type;
449333965Sjdp     boolean filehdr;
449433965Sjdp     boolean phdrs;
449533965Sjdp     etree_type *at;
449633965Sjdp     etree_type *flags;
449733965Sjdp{
449833965Sjdp  struct lang_phdr *n, **pp;
449933965Sjdp
450033965Sjdp  n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
450133965Sjdp  n->next = NULL;
450233965Sjdp  n->name = name;
450333965Sjdp  n->type = exp_get_value_int (type, 0, "program header type",
450433965Sjdp			       lang_final_phase_enum);
450533965Sjdp  n->filehdr = filehdr;
450633965Sjdp  n->phdrs = phdrs;
450733965Sjdp  n->at = at;
450833965Sjdp  n->flags = flags;
450933965Sjdp
451033965Sjdp  for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
451133965Sjdp    ;
451233965Sjdp  *pp = n;
451333965Sjdp}
451433965Sjdp
451533965Sjdp/* Record the program header information in the output BFD.  FIXME: We
451633965Sjdp   should not be calling an ELF specific function here.  */
451733965Sjdp
451833965Sjdpstatic void
451933965Sjdplang_record_phdrs ()
452033965Sjdp{
452133965Sjdp  unsigned int alc;
452233965Sjdp  asection **secs;
452333965Sjdp  struct lang_output_section_phdr_list *last;
452433965Sjdp  struct lang_phdr *l;
452533965Sjdp  lang_statement_union_type *u;
452633965Sjdp
452733965Sjdp  alc = 10;
452833965Sjdp  secs = (asection **) xmalloc (alc * sizeof (asection *));
452933965Sjdp  last = NULL;
453033965Sjdp  for (l = lang_phdr_list; l != NULL; l = l->next)
453133965Sjdp    {
453233965Sjdp      unsigned int c;
453333965Sjdp      flagword flags;
453433965Sjdp      bfd_vma at;
453533965Sjdp
453633965Sjdp      c = 0;
453733965Sjdp      for (u = lang_output_section_statement.head;
453833965Sjdp	   u != NULL;
453933965Sjdp	   u = u->output_section_statement.next)
454033965Sjdp	{
454133965Sjdp	  lang_output_section_statement_type *os;
454233965Sjdp	  struct lang_output_section_phdr_list *pl;
454333965Sjdp
454433965Sjdp	  os = &u->output_section_statement;
454533965Sjdp
454633965Sjdp	  pl = os->phdrs;
454733965Sjdp	  if (pl != NULL)
454833965Sjdp	    last = pl;
454933965Sjdp	  else
455033965Sjdp	    {
455133965Sjdp	      if (os->sectype == noload_section
455233965Sjdp		  || os->bfd_section == NULL
455333965Sjdp		  || (os->bfd_section->flags & SEC_ALLOC) == 0)
455433965Sjdp		continue;
455533965Sjdp	      pl = last;
455633965Sjdp	    }
455733965Sjdp
455833965Sjdp	  if (os->bfd_section == NULL)
455933965Sjdp	    continue;
456033965Sjdp
456133965Sjdp	  for (; pl != NULL; pl = pl->next)
456233965Sjdp	    {
456333965Sjdp	      if (strcmp (pl->name, l->name) == 0)
456433965Sjdp		{
456533965Sjdp		  if (c >= alc)
456633965Sjdp		    {
456733965Sjdp		      alc *= 2;
456833965Sjdp		      secs = ((asection **)
456933965Sjdp			      xrealloc (secs, alc * sizeof (asection *)));
457033965Sjdp		    }
457133965Sjdp		  secs[c] = os->bfd_section;
457233965Sjdp		  ++c;
457333965Sjdp		  pl->used = true;
457433965Sjdp		}
457533965Sjdp	    }
457633965Sjdp	}
457733965Sjdp
457833965Sjdp      if (l->flags == NULL)
457933965Sjdp	flags = 0;
458033965Sjdp      else
458133965Sjdp	flags = exp_get_vma (l->flags, 0, "phdr flags",
458233965Sjdp			     lang_final_phase_enum);
458333965Sjdp
458433965Sjdp      if (l->at == NULL)
458533965Sjdp	at = 0;
458633965Sjdp      else
458733965Sjdp	at = exp_get_vma (l->at, 0, "phdr load address",
458833965Sjdp			  lang_final_phase_enum);
458933965Sjdp
459033965Sjdp      if (! bfd_record_phdr (output_bfd, l->type,
459178828Sobrien			     l->flags != NULL, flags, l->at != NULL,
459233965Sjdp			     at, l->filehdr, l->phdrs, c, secs))
459360484Sobrien	einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
459433965Sjdp    }
459533965Sjdp
459633965Sjdp  free (secs);
459733965Sjdp
459833965Sjdp  /* Make sure all the phdr assignments succeeded.  */
459933965Sjdp  for (u = lang_output_section_statement.head;
460033965Sjdp       u != NULL;
460133965Sjdp       u = u->output_section_statement.next)
460233965Sjdp    {
460333965Sjdp      struct lang_output_section_phdr_list *pl;
460433965Sjdp
460533965Sjdp      if (u->output_section_statement.bfd_section == NULL)
460633965Sjdp	continue;
460733965Sjdp
460833965Sjdp      for (pl = u->output_section_statement.phdrs;
460933965Sjdp	   pl != NULL;
461033965Sjdp	   pl = pl->next)
461133965Sjdp	if (! pl->used && strcmp (pl->name, "NONE") != 0)
461260484Sobrien	  einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
461333965Sjdp		 u->output_section_statement.name, pl->name);
461433965Sjdp    }
461533965Sjdp}
461633965Sjdp
461733965Sjdp/* Record a list of sections which may not be cross referenced.  */
461833965Sjdp
461933965Sjdpvoid
462033965Sjdplang_add_nocrossref (l)
462133965Sjdp     struct lang_nocrossref *l;
462233965Sjdp{
462333965Sjdp  struct lang_nocrossrefs *n;
462433965Sjdp
462533965Sjdp  n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
462633965Sjdp  n->next = nocrossref_list;
462733965Sjdp  n->list = l;
462833965Sjdp  nocrossref_list = n;
462933965Sjdp
463033965Sjdp  /* Set notice_all so that we get informed about all symbols.  */
463133965Sjdp  link_info.notice_all = true;
463233965Sjdp}
463333965Sjdp
463433965Sjdp/* Overlay handling.  We handle overlays with some static variables.  */
463533965Sjdp
463633965Sjdp/* The overlay virtual address.  */
463733965Sjdpstatic etree_type *overlay_vma;
463833965Sjdp
463933965Sjdp/* The overlay load address.  */
464033965Sjdpstatic etree_type *overlay_lma;
464133965Sjdp
464233965Sjdp/* Whether nocrossrefs is set for this overlay.  */
464333965Sjdpstatic int overlay_nocrossrefs;
464433965Sjdp
464533965Sjdp/* An expression for the maximum section size seen so far.  */
464633965Sjdpstatic etree_type *overlay_max;
464733965Sjdp
464833965Sjdp/* A list of all the sections in this overlay.  */
464933965Sjdp
465077298Sobrienstruct overlay_list {
465133965Sjdp  struct overlay_list *next;
465233965Sjdp  lang_output_section_statement_type *os;
465333965Sjdp};
465433965Sjdp
465533965Sjdpstatic struct overlay_list *overlay_list;
465633965Sjdp
465733965Sjdp/* Start handling an overlay.  */
465833965Sjdp
465933965Sjdpvoid
466033965Sjdplang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
466133965Sjdp     etree_type *vma_expr;
466233965Sjdp     etree_type *lma_expr;
466333965Sjdp     int nocrossrefs;
466433965Sjdp{
466533965Sjdp  /* The grammar should prevent nested overlays from occurring.  */
466633965Sjdp  ASSERT (overlay_vma == NULL
466733965Sjdp	  && overlay_lma == NULL
466833965Sjdp	  && overlay_list == NULL
466933965Sjdp	  && overlay_max == NULL);
467033965Sjdp
467133965Sjdp  overlay_vma = vma_expr;
467233965Sjdp  overlay_lma = lma_expr;
467333965Sjdp  overlay_nocrossrefs = nocrossrefs;
467433965Sjdp}
467533965Sjdp
467633965Sjdp/* Start a section in an overlay.  We handle this by calling
467733965Sjdp   lang_enter_output_section_statement with the correct VMA and LMA.  */
467833965Sjdp
467933965Sjdpvoid
468033965Sjdplang_enter_overlay_section (name)
468133965Sjdp     const char *name;
468233965Sjdp{
468333965Sjdp  struct overlay_list *n;
468433965Sjdp  etree_type *size;
468533965Sjdp
468633965Sjdp  lang_enter_output_section_statement (name, overlay_vma, normal_section,
468733965Sjdp				       0, 0, 0, overlay_lma);
468833965Sjdp
468933965Sjdp  /* If this is the first section, then base the VMA and LMA of future
469033965Sjdp     sections on this one.  This will work correctly even if `.' is
469133965Sjdp     used in the addresses.  */
469233965Sjdp  if (overlay_list == NULL)
469333965Sjdp    {
469433965Sjdp      overlay_vma = exp_nameop (ADDR, name);
469533965Sjdp      overlay_lma = exp_nameop (LOADADDR, name);
469633965Sjdp    }
469733965Sjdp
469833965Sjdp  /* Remember the section.  */
469933965Sjdp  n = (struct overlay_list *) xmalloc (sizeof *n);
470033965Sjdp  n->os = current_section;
470133965Sjdp  n->next = overlay_list;
470233965Sjdp  overlay_list = n;
470333965Sjdp
470433965Sjdp  size = exp_nameop (SIZEOF, name);
470533965Sjdp
470633965Sjdp  /* Adjust the LMA for the next section.  */
470733965Sjdp  overlay_lma = exp_binop ('+', overlay_lma, size);
470833965Sjdp
470933965Sjdp  /* Arrange to work out the maximum section end address.  */
471033965Sjdp  if (overlay_max == NULL)
471133965Sjdp    overlay_max = size;
471233965Sjdp  else
471360484Sobrien    overlay_max = exp_binop (MAX_K, overlay_max, size);
471433965Sjdp}
471533965Sjdp
471633965Sjdp/* Finish a section in an overlay.  There isn't any special to do
471733965Sjdp   here.  */
471833965Sjdp
471933965Sjdpvoid
472033965Sjdplang_leave_overlay_section (fill, phdrs)
472133965Sjdp     bfd_vma fill;
472233965Sjdp     struct lang_output_section_phdr_list *phdrs;
472333965Sjdp{
472433965Sjdp  const char *name;
472533965Sjdp  char *clean, *s2;
472633965Sjdp  const char *s1;
472733965Sjdp  char *buf;
472833965Sjdp
472933965Sjdp  name = current_section->name;
473033965Sjdp
473177298Sobrien  lang_leave_output_section_statement (fill, "*default*",
473260484Sobrien                                       phdrs, "*default*");
473333965Sjdp
473433965Sjdp  /* Define the magic symbols.  */
473533965Sjdp
473633965Sjdp  clean = xmalloc (strlen (name) + 1);
473733965Sjdp  s2 = clean;
473833965Sjdp  for (s1 = name; *s1 != '\0'; s1++)
473938889Sjdp    if (isalnum ((unsigned char) *s1) || *s1 == '_')
474033965Sjdp      *s2++ = *s1;
474133965Sjdp  *s2 = '\0';
474233965Sjdp
474333965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_start_");
474433965Sjdp  sprintf (buf, "__load_start_%s", clean);
474533965Sjdp  lang_add_assignment (exp_assop ('=', buf,
474633965Sjdp				  exp_nameop (LOADADDR, name)));
474733965Sjdp
474833965Sjdp  buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
474933965Sjdp  sprintf (buf, "__load_stop_%s", clean);
475033965Sjdp  lang_add_assignment (exp_assop ('=', buf,
475133965Sjdp				  exp_binop ('+',
475233965Sjdp					     exp_nameop (LOADADDR, name),
475333965Sjdp					     exp_nameop (SIZEOF, name))));
475433965Sjdp
475533965Sjdp  free (clean);
475633965Sjdp}
475733965Sjdp
475833965Sjdp/* Finish an overlay.  If there are any overlay wide settings, this
475933965Sjdp   looks through all the sections in the overlay and sets them.  */
476033965Sjdp
476133965Sjdpvoid
476260484Sobrienlang_leave_overlay (fill, memspec, phdrs, lma_memspec)
476333965Sjdp     bfd_vma fill;
476433965Sjdp     const char *memspec;
476533965Sjdp     struct lang_output_section_phdr_list *phdrs;
476660484Sobrien     const char *lma_memspec;
476733965Sjdp{
476833965Sjdp  lang_memory_region_type *region;
476980016Sobrien  lang_memory_region_type * default_region;
477060484Sobrien  lang_memory_region_type *lma_region;
477133965Sjdp  struct overlay_list *l;
477233965Sjdp  struct lang_nocrossref *nocrossref;
477333965Sjdp
477480016Sobrien  default_region = lang_memory_region_lookup ("*default*");
477580016Sobrien
477633965Sjdp  if (memspec == NULL)
477733965Sjdp    region = NULL;
477833965Sjdp  else
477933965Sjdp    region = lang_memory_region_lookup (memspec);
478033965Sjdp
478160484Sobrien  if (lma_memspec == NULL)
478260484Sobrien    lma_region = NULL;
478360484Sobrien  else
478460484Sobrien    lma_region = lang_memory_region_lookup (lma_memspec);
478560484Sobrien
478633965Sjdp  nocrossref = NULL;
478733965Sjdp
478833965Sjdp  l = overlay_list;
478933965Sjdp  while (l != NULL)
479033965Sjdp    {
479133965Sjdp      struct overlay_list *next;
479233965Sjdp
479333965Sjdp      if (fill != 0 && l->os->fill == 0)
479433965Sjdp	l->os->fill = fill;
479580016Sobrien
479680016Sobrien      /* Assign a region to the sections, if one has been specified.
479780016Sobrien	 Override the assignment of the default section, but not
479880016Sobrien	 other sections.  */
479980016Sobrien      if (region != NULL &&
480080016Sobrien	  (l->os->region == NULL ||
480180016Sobrien	   l->os->region == default_region))
480233965Sjdp	l->os->region = region;
480380016Sobrien
480477298Sobrien      /* We only set lma_region for the first overlay section, as
480577298Sobrien	 subsequent overlay sections will have load_base set relative
480677298Sobrien	 to the first section.  Also, don't set lma_region if
480777298Sobrien	 load_base is specified.  FIXME:  There should really be a test
480877298Sobrien	 that `AT ( LDADDR )' doesn't conflict with `AT >LMA_REGION'
480977298Sobrien	 rather than letting LDADDR simply override LMA_REGION.  */
481077298Sobrien      if (lma_region != NULL && l->os->lma_region == NULL
481177298Sobrien	  && l->next == NULL && l->os->load_base == NULL)
481277298Sobrien	l->os->lma_region = lma_region;
481380016Sobrien
481433965Sjdp      if (phdrs != NULL && l->os->phdrs == NULL)
481533965Sjdp	l->os->phdrs = phdrs;
481633965Sjdp
481733965Sjdp      if (overlay_nocrossrefs)
481833965Sjdp	{
481933965Sjdp	  struct lang_nocrossref *nc;
482033965Sjdp
482133965Sjdp	  nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
482233965Sjdp	  nc->name = l->os->name;
482333965Sjdp	  nc->next = nocrossref;
482433965Sjdp	  nocrossref = nc;
482533965Sjdp	}
482633965Sjdp
482733965Sjdp      next = l->next;
482833965Sjdp      free (l);
482933965Sjdp      l = next;
483033965Sjdp    }
483133965Sjdp
483233965Sjdp  if (nocrossref != NULL)
483333965Sjdp    lang_add_nocrossref (nocrossref);
483433965Sjdp
483533965Sjdp  /* Update . for the end of the overlay.  */
483633965Sjdp  lang_add_assignment (exp_assop ('=', ".",
483733965Sjdp				  exp_binop ('+', overlay_vma, overlay_max)));
483833965Sjdp
483933965Sjdp  overlay_vma = NULL;
484033965Sjdp  overlay_lma = NULL;
484133965Sjdp  overlay_nocrossrefs = 0;
484233965Sjdp  overlay_list = NULL;
484333965Sjdp  overlay_max = NULL;
484433965Sjdp}
484533965Sjdp
484633965Sjdp/* Version handling.  This is only useful for ELF.  */
484733965Sjdp
484833965Sjdp/* This global variable holds the version tree that we build.  */
484933965Sjdp
485033965Sjdpstruct bfd_elf_version_tree *lang_elf_version_info;
485133965Sjdp
485260484Sobrienstatic int
485360484Sobrienlang_vers_match_lang_c (expr, sym)
485460484Sobrien     struct bfd_elf_version_expr *expr;
485560484Sobrien     const char *sym;
485660484Sobrien{
485760484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
485860484Sobrien    return 1;
485960484Sobrien  return fnmatch (expr->pattern, sym, 0) == 0;
486060484Sobrien}
486160484Sobrien
486260484Sobrienstatic int
486360484Sobrienlang_vers_match_lang_cplusplus (expr, sym)
486460484Sobrien     struct bfd_elf_version_expr *expr;
486560484Sobrien     const char *sym;
486660484Sobrien{
486760484Sobrien  char *alt_sym;
486860484Sobrien  int result;
486960484Sobrien
487060484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
487160484Sobrien    return 1;
487260484Sobrien
487377298Sobrien  alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
487460484Sobrien  if (!alt_sym)
487560484Sobrien    {
487660484Sobrien      /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
487760484Sobrien	 Should we early out false in this case?  */
487860484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
487960484Sobrien    }
488060484Sobrien  else
488160484Sobrien    {
488260484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
488360484Sobrien      free (alt_sym);
488460484Sobrien    }
488560484Sobrien
488660484Sobrien  return result;
488760484Sobrien}
488860484Sobrien
488960484Sobrienstatic int
489060484Sobrienlang_vers_match_lang_java (expr, sym)
489160484Sobrien     struct bfd_elf_version_expr *expr;
489260484Sobrien     const char *sym;
489360484Sobrien{
489460484Sobrien  char *alt_sym;
489560484Sobrien  int result;
489660484Sobrien
489760484Sobrien  if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
489860484Sobrien    return 1;
489960484Sobrien
490077298Sobrien  alt_sym = cplus_demangle (sym, DMGL_JAVA);
490160484Sobrien  if (!alt_sym)
490260484Sobrien    {
490360484Sobrien      /* cplus_demangle (also) returns NULL when it is not a Java symbol.
490460484Sobrien	 Should we early out false in this case?  */
490560484Sobrien      result = fnmatch (expr->pattern, sym, 0) == 0;
490660484Sobrien    }
490760484Sobrien  else
490860484Sobrien    {
490960484Sobrien      result = fnmatch (expr->pattern, alt_sym, 0) == 0;
491060484Sobrien      free (alt_sym);
491160484Sobrien    }
491260484Sobrien
491360484Sobrien  return result;
491460484Sobrien}
491560484Sobrien
491633965Sjdp/* This is called for each variable name or match expression.  */
491733965Sjdp
491833965Sjdpstruct bfd_elf_version_expr *
491960484Sobrienlang_new_vers_regex (orig, new, lang)
492033965Sjdp     struct bfd_elf_version_expr *orig;
492133965Sjdp     const char *new;
492260484Sobrien     const char *lang;
492333965Sjdp{
492433965Sjdp  struct bfd_elf_version_expr *ret;
492533965Sjdp
492633965Sjdp  ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
492733965Sjdp  ret->next = orig;
492860484Sobrien  ret->pattern = new;
492960484Sobrien
493060484Sobrien  if (lang == NULL || strcasecmp (lang, "C") == 0)
493160484Sobrien    ret->match = lang_vers_match_lang_c;
493260484Sobrien  else if (strcasecmp (lang, "C++") == 0)
493360484Sobrien    ret->match = lang_vers_match_lang_cplusplus;
493460484Sobrien  else if (strcasecmp (lang, "Java") == 0)
493560484Sobrien    ret->match = lang_vers_match_lang_java;
493660484Sobrien  else
493760484Sobrien    {
493860484Sobrien      einfo (_("%X%P: unknown language `%s' in version information\n"),
493960484Sobrien	     lang);
494060484Sobrien      ret->match = lang_vers_match_lang_c;
494160484Sobrien    }
494260484Sobrien
494333965Sjdp  return ret;
494433965Sjdp}
494533965Sjdp
494633965Sjdp/* This is called for each set of variable names and match
494733965Sjdp   expressions.  */
494833965Sjdp
494933965Sjdpstruct bfd_elf_version_tree *
495033965Sjdplang_new_vers_node (globals, locals)
495133965Sjdp     struct bfd_elf_version_expr *globals;
495233965Sjdp     struct bfd_elf_version_expr *locals;
495333965Sjdp{
495433965Sjdp  struct bfd_elf_version_tree *ret;
495533965Sjdp
495633965Sjdp  ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
495733965Sjdp  ret->next = NULL;
495833965Sjdp  ret->name = NULL;
495933965Sjdp  ret->vernum = 0;
496033965Sjdp  ret->globals = globals;
496133965Sjdp  ret->locals = locals;
496233965Sjdp  ret->deps = NULL;
496333965Sjdp  ret->name_indx = (unsigned int) -1;
496433965Sjdp  ret->used = 0;
496533965Sjdp  return ret;
496633965Sjdp}
496733965Sjdp
496833965Sjdp/* This static variable keeps track of version indices.  */
496933965Sjdp
497033965Sjdpstatic int version_index;
497133965Sjdp
497233965Sjdp/* This is called when we know the name and dependencies of the
497333965Sjdp   version.  */
497433965Sjdp
497533965Sjdpvoid
497633965Sjdplang_register_vers_node (name, version, deps)
497733965Sjdp     const char *name;
497833965Sjdp     struct bfd_elf_version_tree *version;
497933965Sjdp     struct bfd_elf_version_deps *deps;
498033965Sjdp{
498133965Sjdp  struct bfd_elf_version_tree *t, **pp;
498233965Sjdp  struct bfd_elf_version_expr *e1;
498333965Sjdp
498433965Sjdp  /* Make sure this node has a unique name.  */
498533965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
498633965Sjdp    if (strcmp (t->name, name) == 0)
498760484Sobrien      einfo (_("%X%P: duplicate version tag `%s'\n"), name);
498833965Sjdp
498933965Sjdp  /* Check the global and local match names, and make sure there
499033965Sjdp     aren't any duplicates.  */
499133965Sjdp
499233965Sjdp  for (e1 = version->globals; e1 != NULL; e1 = e1->next)
499333965Sjdp    {
499433965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
499533965Sjdp	{
499633965Sjdp	  struct bfd_elf_version_expr *e2;
499733965Sjdp
499833965Sjdp	  for (e2 = t->locals; e2 != NULL; e2 = e2->next)
499960484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
500060484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
500160484Sobrien		     e1->pattern);
500233965Sjdp	}
500333965Sjdp    }
500433965Sjdp
500533965Sjdp  for (e1 = version->locals; e1 != NULL; e1 = e1->next)
500633965Sjdp    {
500733965Sjdp      for (t = lang_elf_version_info; t != NULL; t = t->next)
500833965Sjdp	{
500933965Sjdp	  struct bfd_elf_version_expr *e2;
501033965Sjdp
501133965Sjdp	  for (e2 = t->globals; e2 != NULL; e2 = e2->next)
501260484Sobrien	    if (strcmp (e1->pattern, e2->pattern) == 0)
501360484Sobrien	      einfo (_("%X%P: duplicate expression `%s' in version information\n"),
501460484Sobrien		     e1->pattern);
501533965Sjdp	}
501633965Sjdp    }
501733965Sjdp
501833965Sjdp  version->deps = deps;
501933965Sjdp  version->name = name;
502033965Sjdp  ++version_index;
502133965Sjdp  version->vernum = version_index;
502233965Sjdp
502333965Sjdp  for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
502433965Sjdp    ;
502533965Sjdp  *pp = version;
502633965Sjdp}
502733965Sjdp
502833965Sjdp/* This is called when we see a version dependency.  */
502933965Sjdp
503033965Sjdpstruct bfd_elf_version_deps *
503133965Sjdplang_add_vers_depend (list, name)
503233965Sjdp     struct bfd_elf_version_deps *list;
503333965Sjdp     const char *name;
503433965Sjdp{
503533965Sjdp  struct bfd_elf_version_deps *ret;
503633965Sjdp  struct bfd_elf_version_tree *t;
503733965Sjdp
503833965Sjdp  ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
503933965Sjdp  ret->next = list;
504033965Sjdp
504133965Sjdp  for (t = lang_elf_version_info; t != NULL; t = t->next)
504233965Sjdp    {
504333965Sjdp      if (strcmp (t->name, name) == 0)
504433965Sjdp	{
504533965Sjdp	  ret->version_needed = t;
504633965Sjdp	  return ret;
504733965Sjdp	}
504833965Sjdp    }
504933965Sjdp
505060484Sobrien  einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
505133965Sjdp
505233965Sjdp  return ret;
505333965Sjdp}
505460484Sobrien
505560484Sobrienstatic void
505660484Sobrienlang_do_version_exports_section ()
505760484Sobrien{
505860484Sobrien  struct bfd_elf_version_expr *greg = NULL, *lreg;
505960484Sobrien
506060484Sobrien  LANG_FOR_EACH_INPUT_STATEMENT (is)
506160484Sobrien    {
506260484Sobrien      asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
506360484Sobrien      char *contents, *p;
506460484Sobrien      bfd_size_type len;
506560484Sobrien
506660484Sobrien      if (sec == NULL)
506760484Sobrien        continue;
506860484Sobrien
506960484Sobrien      len = bfd_section_size (is->the_bfd, sec);
507060484Sobrien      contents = xmalloc (len);
507160484Sobrien      if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
507260484Sobrien	einfo (_("%X%P: unable to read .exports section contents"), sec);
507360484Sobrien
507460484Sobrien      p = contents;
507577298Sobrien      while (p < contents + len)
507660484Sobrien	{
507760484Sobrien	  greg = lang_new_vers_regex (greg, p, NULL);
507860484Sobrien	  p = strchr (p, '\0') + 1;
507960484Sobrien	}
508060484Sobrien
508160484Sobrien      /* Do not free the contents, as we used them creating the regex.  */
508260484Sobrien
508360484Sobrien      /* Do not include this section in the link.  */
508460484Sobrien      bfd_set_section_flags (is->the_bfd, sec,
508560484Sobrien	bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
508660484Sobrien    }
508760484Sobrien
508860484Sobrien  lreg = lang_new_vers_regex (NULL, "*", NULL);
508960484Sobrien  lang_register_vers_node (command_line.version_exports_section,
509060484Sobrien			   lang_new_vers_node (greg, lreg), NULL);
509160484Sobrien}
509277298Sobrien
509377298Sobrienvoid
509477298Sobrienlang_add_unique (name)
509577298Sobrien     const char *name;
509677298Sobrien{
509777298Sobrien  struct unique_sections *ent;
509877298Sobrien
509977298Sobrien  for (ent = unique_section_list; ent; ent = ent->next)
510077298Sobrien    if (strcmp (ent->name, name) == 0)
510177298Sobrien      return;
510277298Sobrien
510377298Sobrien  ent = (struct unique_sections *) xmalloc (sizeof *ent);
510477298Sobrien  ent->name = xstrdup (name);
510577298Sobrien  ent->next = unique_section_list;
510677298Sobrien  unique_section_list = ent;
510777298Sobrien}
5108