1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3fragment <<EOF
4/* This file is is generated by a shell script.  DO NOT EDIT! */
5
6/* emulate the original gld for the given ${EMULATION_NAME}
7   Copyright (C) 1991-2016 Free Software Foundation, Inc.
8   Written by Steve Chamberlain steve@cygnus.com
9
10   This file is part of the GNU Binutils.
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 3 of the License, or
15   (at your option) any later version.
16
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, write to the Free Software
24   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
25   MA 02110-1301, USA.  */
26
27#define TARGET_IS_${EMULATION_NAME}
28
29#include "sysdep.h"
30#include "bfd.h"
31#include "bfdlink.h"
32#include "getopt.h"
33
34#include "ld.h"
35#include "ldmain.h"
36#include "ldmisc.h"
37
38#include "ldexp.h"
39#include "ldlang.h"
40#include "ldfile.h"
41#include "ldemul.h"
42
43/* If TRUE, then interworking stubs which support calls to old,
44   non-interworking aware ARM code should be generated.  */
45
46static int support_old_code = 0;
47static char * thumb_entry_symbol = NULL;
48
49#define OPTION_SUPPORT_OLD_CODE		300
50#define OPTION_THUMB_ENTRY		301
51
52static void
53gld${EMULATION_NAME}_add_options
54  (int ns ATTRIBUTE_UNUSED, char **shortopts ATTRIBUTE_UNUSED, int nl,
55   struct option **longopts, int nrl ATTRIBUTE_UNUSED,
56   struct option **really_longopts ATTRIBUTE_UNUSED)
57{
58  static const struct option xtra_long[] = {
59    {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE},
60    {"thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
61    {NULL, no_argument, NULL, 0}
62  };
63
64  *longopts = xrealloc (*longopts,
65			nl * sizeof (struct option) + sizeof (xtra_long));
66  memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
67}
68
69static void
70gld${EMULATION_NAME}_list_options (FILE *file)
71{
72  fprintf (file, _("  --support-old-code          Support interworking with old code\n"));
73  fprintf (file, _("  --thumb-entry=<sym>         Set the entry point to be Thumb symbol <sym>\n"));
74}
75
76static bfd_boolean
77gld${EMULATION_NAME}_handle_option (int optc)
78{
79  switch (optc)
80    {
81    default:
82      return FALSE;
83
84    case OPTION_SUPPORT_OLD_CODE:
85      support_old_code = 1;
86      break;
87
88    case OPTION_THUMB_ENTRY:
89      thumb_entry_symbol = optarg;
90      break;
91    }
92
93  return TRUE;
94}
95
96static void
97gld${EMULATION_NAME}_before_parse (void)
98{
99#ifndef TARGET_			/* I.e., if not generic.  */
100  ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
101#endif /* not TARGET_ */
102}
103
104/* This is called after the sections have been attached to output
105   sections, but before any sizes or addresses have been set.  */
106
107static void
108gld${EMULATION_NAME}_before_allocation (void)
109{
110  /* we should be able to set the size of the interworking stub section */
111
112  /* Here we rummage through the found bfds to collect glue information */
113  /* FIXME: should this be based on a command line option? krk@cygnus.com */
114  {
115    LANG_FOR_EACH_INPUT_STATEMENT (is)
116      {
117	if (! bfd_arm_process_before_allocation
118	    (is->the_bfd, & link_info, support_old_code))
119	  {
120	    /* xgettext:c-format */
121	    einfo (_("Errors encountered processing file %s"), is->filename);
122	  }
123      }
124  }
125
126  /* We have seen it all. Allocate it, and carry on */
127  bfd_arm_allocate_interworking_sections (& link_info);
128
129  before_allocation_default ();
130}
131
132static void
133gld${EMULATION_NAME}_after_open (void)
134{
135  after_open_default ();
136
137  if (strstr (bfd_get_target (link_info.output_bfd), "arm") == NULL)
138    {
139      /* The arm backend needs special fields in the output hash structure.
140	 These will only be created if the output format is an arm format,
141	 hence we do not support linking and changing output formats at the
142	 same time.  Use a link followed by objcopy to change output formats.  */
143      einfo ("%F%X%P: error: cannot change output format whilst linking ARM binaries\n");
144      return;
145    }
146
147  {
148    LANG_FOR_EACH_INPUT_STATEMENT (is)
149      {
150	if (bfd_arm_get_bfd_for_interworking (is->the_bfd, & link_info))
151	  break;
152      }
153  }
154}
155
156static void
157gld${EMULATION_NAME}_finish (void)
158{
159  if (thumb_entry_symbol != NULL)
160    {
161      struct bfd_link_hash_entry * h;
162
163      h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol,
164				FALSE, FALSE, TRUE);
165
166      if (h != (struct bfd_link_hash_entry *) NULL
167	  && (h->type == bfd_link_hash_defined
168	      || h->type == bfd_link_hash_defweak)
169	  && h->u.def.section->output_section != NULL)
170	{
171	  static char buffer[32];
172	  bfd_vma val;
173
174	  /* Special procesing is required for a Thumb entry symbol.  The
175	     bottom bit of its address must be set.  */
176	  val = (h->u.def.value
177		 + bfd_get_section_vma (link_info.output_bfd,
178					h->u.def.section->output_section)
179		 + h->u.def.section->output_offset);
180
181	  val |= 1;
182
183	  /* Now convert this value into a string and store it in entry_symbol
184	     where the lang_finish() function will pick it up.  */
185	  buffer[0] = '0';
186	  buffer[1] = 'x';
187
188	  sprintf_vma (buffer + 2, val);
189
190	  if (entry_symbol.name != NULL && entry_from_cmdline)
191	    einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
192		   thumb_entry_symbol, entry_symbol.name);
193	  entry_symbol.name = buffer;
194	}
195      else
196	einfo (_("%P: warning: cannot find thumb start symbol %s\n"),
197	       thumb_entry_symbol);
198    }
199
200  finish_default ();
201}
202
203static char *
204gld${EMULATION_NAME}_get_script (int *isfile)
205EOF
206
207if test x"$COMPILE_IN" = xyes
208then
209# Scripts compiled in.
210
211# sed commands to quote an ld script as a C string.
212sc="-f stringify.sed"
213
214fragment <<EOF
215{
216  *isfile = 0;
217
218  if (bfd_link_relocatable (&link_info) && config.build_constructors)
219    return
220EOF
221sed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
222echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
223sed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
224echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
225sed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
226echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
227sed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
228echo '  ; else return'                                 >> e${EMULATION_NAME}.c
229sed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
230echo '; }'                                             >> e${EMULATION_NAME}.c
231
232else
233# Scripts read from the filesystem.
234
235fragment <<EOF
236{
237  *isfile = 1;
238
239  if (bfd_link_relocatable (&link_info) && config.build_constructors)
240    return "ldscripts/${EMULATION_NAME}.xu";
241  else if (bfd_link_relocatable (&link_info))
242    return "ldscripts/${EMULATION_NAME}.xr";
243  else if (!config.text_read_only)
244    return "ldscripts/${EMULATION_NAME}.xbn";
245  else if (!config.magic_demand_paged)
246    return "ldscripts/${EMULATION_NAME}.xn";
247  else
248    return "ldscripts/${EMULATION_NAME}.x";
249}
250EOF
251
252fi
253
254fragment <<EOF
255
256struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
257{
258  gld${EMULATION_NAME}_before_parse,
259  syslib_default,
260  hll_default,
261  after_parse_default,
262  gld${EMULATION_NAME}_after_open,
263  after_allocation_default,
264  set_output_arch_default,
265  ldemul_default_target,
266  gld${EMULATION_NAME}_before_allocation,
267  gld${EMULATION_NAME}_get_script,
268  "${EMULATION_NAME}",
269  "${OUTPUT_FORMAT}",
270  gld${EMULATION_NAME}_finish,
271  NULL,	/* create output section statements */
272  NULL,	/* open dynamic archive */
273  NULL,	/* place orphan */
274  NULL,	/* set symbols */
275  NULL, /* parse_args */
276  gld${EMULATION_NAME}_add_options,
277  gld${EMULATION_NAME}_handle_option,
278  NULL,	/* unrecognised file */
279  gld${EMULATION_NAME}_list_options,
280  NULL,	/* recognized file */
281  NULL,	/* find_potential_libraries */
282  NULL,	/* new_vers_pattern */
283  NULL	/* extra_map_file_text */
284};
285EOF
286