1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3cat >e${EMULATION_NAME}.c <<EOF
4/* This file is is generated by a shell script.  DO NOT EDIT! */
5
6/* Handle embedded relocs for m68k.
7   Copyright 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
8   Written by Michael Sokolov <msokolov@ivan.Harhan.ORG>, based on generic.em
9   by Steve Chamberlain <steve@cygnus.com>, embedded relocs code based on
10   mipsecoff.em by Ian Lance Taylor <ian@cygnus.com>.
11
12This file is part of GLD, the Gnu Linker.
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
27
28#define TARGET_IS_${EMULATION_NAME}
29
30#include "bfd.h"
31#include "sysdep.h"
32#include "bfdlink.h"
33
34#include "ld.h"
35#include "ldmain.h"
36#include "ldfile.h"
37#include "ldemul.h"
38#include "ldmisc.h"
39
40static void check_sections (bfd *, asection *, void *);
41
42static void
43gld${EMULATION_NAME}_before_parse (void)
44{
45#ifndef TARGET_			/* I.e., if not generic.  */
46  ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
47#endif /* not TARGET_ */
48}
49
50/* This function is run after all the input files have been opened.
51   We create a .emreloc section for each input file with a non zero
52   .data section.  The BFD backend will fill in these sections with
53   magic numbers which can be used to relocate the data section at run
54   time.  */
55
56static void
57gld${EMULATION_NAME}_after_open (void)
58{
59  bfd *abfd;
60
61  if (! command_line.embedded_relocs
62      || link_info.relocatable)
63    return;
64
65  for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
66    {
67      asection *datasec;
68
69      /* As first-order business, make sure that each input BFD is COFF. It
70         better be, as we are directly calling a COFF backend function.  */
71      if (bfd_get_flavour (abfd) != bfd_target_coff_flavour)
72	einfo ("%F%B: all input objects must be COFF for --embedded-relocs\n");
73
74      datasec = bfd_get_section_by_name (abfd, ".data");
75
76      /* Note that we assume that the reloc_count field has already
77         been set up.  We could call bfd_get_reloc_upper_bound, but
78         that returns the size of a memory buffer rather than a reloc
79         count.  We do not want to call bfd_canonicalize_reloc,
80         because although it would always work it would force us to
81         read in the relocs into BFD canonical form, which would waste
82         a significant amount of time and memory.  */
83      if (datasec != NULL && datasec->reloc_count > 0)
84	{
85	  asection *relsec;
86
87	  relsec = bfd_make_section (abfd, ".emreloc");
88	  if (relsec == NULL
89	      || ! bfd_set_section_flags (abfd, relsec,
90					  (SEC_ALLOC
91					   | SEC_LOAD
92					   | SEC_HAS_CONTENTS
93					   | SEC_IN_MEMORY))
94	      || ! bfd_set_section_alignment (abfd, relsec, 2)
95	      || ! bfd_set_section_size (abfd, relsec,
96					 datasec->reloc_count * 12))
97	    einfo ("%F%B: can not create .emreloc section: %E\n");
98	}
99
100      /* Double check that all other data sections are empty, as is
101         required for embedded PIC code.  */
102      bfd_map_over_sections (abfd, check_sections, datasec);
103    }
104}
105
106/* Check that of the data sections, only the .data section has
107   relocs.  This is called via bfd_map_over_sections.  */
108
109static void
110check_sections (bfd *abfd, asection *sec, void *datasec)
111{
112  if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
113      && sec != datasec
114      && sec->reloc_count != 0)
115    einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
116	   abfd, bfd_get_section_name (abfd, sec));
117}
118
119/* This function is called after the section sizes and offsets have
120   been set.  If we are generating embedded relocs, it calls a special
121   BFD backend routine to do the work.  */
122
123static void
124gld${EMULATION_NAME}_after_allocation (void)
125{
126  bfd *abfd;
127
128  if (! command_line.embedded_relocs
129      || link_info.relocatable)
130    return;
131
132  for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
133    {
134      asection *datasec, *relsec;
135      char *errmsg;
136
137      datasec = bfd_get_section_by_name (abfd, ".data");
138
139      if (datasec == NULL || datasec->reloc_count == 0)
140	continue;
141
142      relsec = bfd_get_section_by_name (abfd, ".emreloc");
143      ASSERT (relsec != NULL);
144
145      if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info,
146						   datasec, relsec,
147						   &errmsg))
148	{
149	  if (errmsg == NULL)
150	    einfo ("%B%X: can not create runtime reloc information: %E\n",
151		   abfd);
152	  else
153	    einfo ("%X%B: can not create runtime reloc information: %s\n",
154		   abfd, errmsg);
155	}
156    }
157}
158
159static char *
160gld${EMULATION_NAME}_get_script (int *isfile)
161EOF
162
163if test -n "$COMPILE_IN"
164then
165# Scripts compiled in.
166
167# sed commands to quote an ld script as a C string.
168sc="-f stringify.sed"
169
170cat >>e${EMULATION_NAME}.c <<EOF
171{
172  *isfile = 0;
173
174  if (link_info.relocatable && config.build_constructors)
175    return
176EOF
177sed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
178echo '  ; else if (link_info.relocatable) return'     >> e${EMULATION_NAME}.c
179sed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
180echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
181sed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
182echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
183sed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
184echo '  ; else return'                                 >> e${EMULATION_NAME}.c
185sed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
186echo '; }'                                             >> e${EMULATION_NAME}.c
187
188else
189# Scripts read from the filesystem.
190
191cat >>e${EMULATION_NAME}.c <<EOF
192{
193  *isfile = 1;
194
195  if (link_info.relocatable && config.build_constructors)
196    return "ldscripts/${EMULATION_NAME}.xu";
197  else if (link_info.relocatable)
198    return "ldscripts/${EMULATION_NAME}.xr";
199  else if (!config.text_read_only)
200    return "ldscripts/${EMULATION_NAME}.xbn";
201  else if (!config.magic_demand_paged)
202    return "ldscripts/${EMULATION_NAME}.xn";
203  else
204    return "ldscripts/${EMULATION_NAME}.x";
205}
206EOF
207
208fi
209
210cat >>e${EMULATION_NAME}.c <<EOF
211
212struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
213{
214  gld${EMULATION_NAME}_before_parse,
215  syslib_default,
216  hll_default,
217  after_parse_default,
218  gld${EMULATION_NAME}_after_open,
219  gld${EMULATION_NAME}_after_allocation,
220  set_output_arch_default,
221  ldemul_default_target,
222  before_allocation_default,
223  gld${EMULATION_NAME}_get_script,
224  "${EMULATION_NAME}",
225  "${OUTPUT_FORMAT}",
226  NULL,	/* finish */
227  NULL,	/* create output section statements */
228  NULL,	/* open dynamic archive */
229  NULL,	/* place orphan */
230  NULL,	/* set symbols */
231  NULL,	/* parse args */
232  NULL,	/* add_options */
233  NULL,	/* handle_option */
234  NULL,	/* unrecognized file */
235  NULL,	/* list options */
236  NULL,	/* recognized file */
237  NULL,	/* find_potential_libraries */
238  NULL	/* new_vers_pattern */
239};
240EOF
241