linux.em revision 60484
1178476Sjb# This shell script emits a C file. -*- C -*-
2178476Sjb# It does some substitutions.
3178476Sjbcat >e${EMULATION_NAME}.c <<EOF
4178476Sjb/* This file is is generated by a shell script.  DO NOT EDIT! */
5178476Sjb
6178476Sjb/* Linux a.out emulation code for ${EMULATION_NAME}
7178476Sjb   Copyright (C) 1991, 93, 94, 95, 96, 98, 1999 Free Software Foundation, Inc.
8178476Sjb   Written by Steve Chamberlain <sac@cygnus.com>
9178476Sjb   Linux support by Eric Youngdale <ericy@cais.cais.com>
10178476Sjb
11178476SjbThis file is part of GLD, the Gnu Linker.
12178476Sjb
13178476SjbThis program is free software; you can redistribute it and/or modify
14178476Sjbit under the terms of the GNU General Public License as published by
15178476Sjbthe Free Software Foundation; either version 2 of the License, or
16178476Sjb(at your option) any later version.
17178476Sjb
18178476SjbThis program is distributed in the hope that it will be useful,
19178476Sjbbut WITHOUT ANY WARRANTY; without even the implied warranty of
20178476SjbMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21178476SjbGNU General Public License for more details.
22178476Sjb
23178476SjbYou should have received a copy of the GNU General Public License
24178476Sjbalong with this program; if not, write to the Free Software
25178476SjbFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
26178476Sjb
27178476Sjb#define TARGET_IS_${EMULATION_NAME}
28178476Sjb
29178476Sjb#include "bfd.h"
30178476Sjb#include "sysdep.h"
31178476Sjb#include "bfdlink.h"
32178476Sjb
33178476Sjb#include "ld.h"
34178476Sjb#include "ldmain.h"
35178476Sjb#include "ldemul.h"
36178476Sjb#include "ldfile.h"
37178476Sjb#include "ldmisc.h"
38178476Sjb#include "ldexp.h"
39178476Sjb#include "ldlang.h"
40178476Sjb
41178476Sjbstatic void gld${EMULATION_NAME}_before_parse PARAMS ((void));
42178476Sjbstatic boolean gld${EMULATION_NAME}_open_dynamic_archive
43178476Sjb  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
44178476Sjbstatic void gld${EMULATION_NAME}_find_address_statement
45178476Sjb  PARAMS ((lang_statement_union_type *));
46178476Sjbstatic void gld${EMULATION_NAME}_create_output_section_statements
47178476Sjb  PARAMS ((void));
48178476Sjbstatic void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
49178476Sjbstatic char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
50
51static void
52gld${EMULATION_NAME}_before_parse()
53{
54  ldfile_output_architecture = bfd_arch_${ARCH};
55  config.dynamic_link = true;
56  config.has_shared = true;
57}
58
59/* Try to open a dynamic archive.  This is where we know that Linux
60   dynamic libraries have an extension of .sa.  */
61
62static boolean
63gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
64     const char *arch;
65     search_dirs_type *search;
66     lang_input_statement_type *entry;
67{
68  char *string;
69
70  if (! entry->is_archive)
71    return false;
72
73  string = (char *) xmalloc (strlen (search->name)
74			     + strlen (entry->filename)
75			     + strlen (arch)
76			     + sizeof "/lib.sa");
77
78  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
79
80  if (! ldfile_try_open_bfd (string, entry))
81    {
82      free (string);
83      return false;
84    }
85
86  entry->filename = string;
87
88  return true;
89}
90
91/* This is called by the create_output_section_statements routine via
92   lang_for_each_statement.  It locates any address assignment to
93   .text, and modifies it to include the size of the headers.  This
94   causes -Ttext to mean the starting address of the header, rather
95   than the starting address of .text, which is compatible with other
96   Linux tools.  */
97
98static void
99gld${EMULATION_NAME}_find_address_statement (s)
100     lang_statement_union_type *s;
101{
102  if (s->header.type == lang_address_statement_enum
103      && strcmp (s->address_statement.section_name, ".text") == 0)
104    {
105      ASSERT (s->address_statement.address->type.node_class == etree_value);
106      s->address_statement.address->value.value += 0x20;
107    }
108}
109
110/* This is called before opening the input BFD's.  */
111
112static void
113gld${EMULATION_NAME}_create_output_section_statements ()
114{
115  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
116}
117
118/* This is called after the sections have been attached to output
119   sections, but before any sizes or addresses have been set.  */
120
121static void
122gld${EMULATION_NAME}_before_allocation ()
123{
124  if (link_info.relocateable)
125    return;
126
127  /* Let the backend work out the sizes of any sections required by
128     dynamic linking.  */
129  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
130    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
131}
132
133static char *
134gld${EMULATION_NAME}_get_script(isfile)
135     int *isfile;
136EOF
137
138if test -n "$COMPILE_IN"
139then
140# Scripts compiled in.
141
142# sed commands to quote an ld script as a C string.
143sc="-f stringify.sed"
144
145cat >>e${EMULATION_NAME}.c <<EOF
146{			     
147  *isfile = 0;
148
149  if (link_info.relocateable == true && config.build_constructors == true)
150    return
151EOF
152sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
153echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
154sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
155echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
156sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
157echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
158sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
159echo '  ; else return'                                     >> e${EMULATION_NAME}.c
160sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
161echo '; }'                                                 >> e${EMULATION_NAME}.c
162
163else
164# Scripts read from the filesystem.
165
166cat >>e${EMULATION_NAME}.c <<EOF
167{			     
168  *isfile = 1;
169
170  if (link_info.relocateable == true && config.build_constructors == true)
171    return "ldscripts/${EMULATION_NAME}.xu";
172  else if (link_info.relocateable == true)
173    return "ldscripts/${EMULATION_NAME}.xr";
174  else if (!config.text_read_only)
175    return "ldscripts/${EMULATION_NAME}.xbn";
176  else if (!config.magic_demand_paged)
177    return "ldscripts/${EMULATION_NAME}.xn";
178  else
179    return "ldscripts/${EMULATION_NAME}.x";
180}
181EOF
182
183fi
184
185cat >>e${EMULATION_NAME}.c <<EOF
186
187struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
188{
189  gld${EMULATION_NAME}_before_parse,
190  syslib_default,
191  hll_default,
192  after_parse_default,
193  after_open_default,
194  after_allocation_default,
195  set_output_arch_default,
196  ldemul_default_target,
197  gld${EMULATION_NAME}_before_allocation,
198  gld${EMULATION_NAME}_get_script,
199  "${EMULATION_NAME}",
200  "${OUTPUT_FORMAT}",
201  NULL,	/* finish */
202  gld${EMULATION_NAME}_create_output_section_statements,
203  gld${EMULATION_NAME}_open_dynamic_archive,
204  NULL,	/* place orphan */
205  NULL,	/* set symbols */
206  NULL,	/* parse args */
207  NULL,	/* unrecognized file */
208  NULL,	/* list options */
209  NULL,	/* recognized file */
210  NULL 	/* find_potential_libraries */
211};
212EOF
213