linux.em revision 218822
1242123Sdteske# This shell script emits a C file. -*- C -*-
2242123Sdteske# It does some substitutions.
3242123Sdteskeif [ -z "$MACHINE" ]; then
4242123Sdteske  OUTPUT_ARCH=${ARCH}
5242123Sdteskeelse
6242123Sdteske  OUTPUT_ARCH=${ARCH}:${MACHINE}
7242123Sdteskefi
8242123Sdteskecat >e${EMULATION_NAME}.c <<EOF
9242123Sdteske/* This file is is generated by a shell script.  DO NOT EDIT! */
10242123Sdteske
11242123Sdteske/* Linux a.out emulation code for ${EMULATION_NAME}
12242123Sdteske   Copyright 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
13242123Sdteske   2003, 2004, 2005, 2007 Free Software Foundation, Inc.
14242123Sdteske   Written by Steve Chamberlain <sac@cygnus.com>
15242123Sdteske   Linux support by Eric Youngdale <ericy@cais.cais.com>
16242123Sdteske
17242123SdteskeThis file is part of GLD, the Gnu Linker.
18242123Sdteske
19242123SdteskeThis program is free software; you can redistribute it and/or modify
20242123Sdteskeit under the terms of the GNU General Public License as published by
21242123Sdteskethe Free Software Foundation; either version 2 of the License, or
22242123Sdteske(at your option) any later version.
23242123Sdteske
24242123SdteskeThis program is distributed in the hope that it will be useful,
25242123Sdteskebut WITHOUT ANY WARRANTY; without even the implied warranty of
26242123SdteskeMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27242123SdteskeGNU General Public License for more details.
28242123Sdteske
29242123SdteskeYou should have received a copy of the GNU General Public License
30242123Sdteskealong with this program; if not, write to the Free Software
31242123SdteskeFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
32242123Sdteske
33242123Sdteske#define TARGET_IS_${EMULATION_NAME}
34242123Sdteske
35242123Sdteske#include "sysdep.h"
36242123Sdteske#include "bfd.h"
37242123Sdteske#include "bfdlink.h"
38242123Sdteske
39242123Sdteske#include "ld.h"
40242123Sdteske#include "ldmain.h"
41242123Sdteske#include "ldmisc.h"
42242123Sdteske#include "ldexp.h"
43242123Sdteske#include "ldlang.h"
44242123Sdteske#include "ldfile.h"
45242123Sdteske#include "ldemul.h"
46242123Sdteske
47242123Sdteskestatic void
48242123Sdteskegld${EMULATION_NAME}_before_parse (void)
49242123Sdteske{
50242123Sdteske  ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
51242123Sdteske  config.dynamic_link = TRUE;
52242123Sdteske  config.has_shared = TRUE;
53242123Sdteske}
54242123Sdteske
55242123Sdteske/* Try to open a dynamic archive.  This is where we know that Linux
56242123Sdteske   dynamic libraries have an extension of .sa.  */
57242123Sdteske
58242123Sdteskestatic bfd_boolean
59242123Sdteskegld${EMULATION_NAME}_open_dynamic_archive
60242123Sdteske  (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
61242123Sdteske{
62242123Sdteske  char *string;
63242123Sdteske
64242123Sdteske  if (! entry->is_archive)
65242123Sdteske    return FALSE;
66242123Sdteske
67242123Sdteske  string = (char *) xmalloc (strlen (search->name)
68242123Sdteske			     + strlen (entry->filename)
69242123Sdteske			     + strlen (arch)
70242123Sdteske			     + sizeof "/lib.sa");
71242123Sdteske
72242123Sdteske  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
73242123Sdteske
74242123Sdteske  if (! ldfile_try_open_bfd (string, entry))
75242123Sdteske    {
76242123Sdteske      free (string);
77      return FALSE;
78    }
79
80  entry->filename = string;
81
82  return TRUE;
83}
84
85/* This is called by the create_output_section_statements routine via
86   lang_for_each_statement.  It locates any address assignment to
87   .text, and modifies it to include the size of the headers.  This
88   causes -Ttext to mean the starting address of the header, rather
89   than the starting address of .text, which is compatible with other
90   Linux tools.  */
91
92static void
93gld${EMULATION_NAME}_find_address_statement (lang_statement_union_type *s)
94{
95  if (s->header.type == lang_address_statement_enum
96      && strcmp (s->address_statement.section_name, ".text") == 0)
97    {
98      ASSERT (s->address_statement.address->type.node_class == etree_value);
99      s->address_statement.address->value.value += 0x20;
100    }
101}
102
103/* This is called before opening the input BFD's.  */
104
105static void
106gld${EMULATION_NAME}_create_output_section_statements (void)
107{
108  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
109}
110
111/* This is called after the sections have been attached to output
112   sections, but before any sizes or addresses have been set.  */
113
114static void
115gld${EMULATION_NAME}_before_allocation (void)
116{
117  if (link_info.relocatable)
118    return;
119
120  /* Let the backend work out the sizes of any sections required by
121     dynamic linking.  */
122  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
123    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
124
125  before_allocation_default ();
126}
127
128static char *
129gld${EMULATION_NAME}_get_script (int *isfile)
130EOF
131
132if test -n "$COMPILE_IN"
133then
134# Scripts compiled in.
135
136# sed commands to quote an ld script as a C string.
137sc="-f stringify.sed"
138
139cat >>e${EMULATION_NAME}.c <<EOF
140{
141  *isfile = 0;
142
143  if (link_info.relocatable && config.build_constructors)
144    return
145EOF
146sed $sc ldscripts/${EMULATION_NAME}.xu                 >> e${EMULATION_NAME}.c
147echo '  ; else if (link_info.relocatable) return'     >> e${EMULATION_NAME}.c
148sed $sc ldscripts/${EMULATION_NAME}.xr                 >> e${EMULATION_NAME}.c
149echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
150sed $sc ldscripts/${EMULATION_NAME}.xbn                >> e${EMULATION_NAME}.c
151echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
152sed $sc ldscripts/${EMULATION_NAME}.xn                 >> e${EMULATION_NAME}.c
153echo '  ; else return'                                 >> e${EMULATION_NAME}.c
154sed $sc ldscripts/${EMULATION_NAME}.x                  >> e${EMULATION_NAME}.c
155echo '; }'                                             >> e${EMULATION_NAME}.c
156
157else
158# Scripts read from the filesystem.
159
160cat >>e${EMULATION_NAME}.c <<EOF
161{
162  *isfile = 1;
163
164  if (link_info.relocatable && config.build_constructors)
165    return "ldscripts/${EMULATION_NAME}.xu";
166  else if (link_info.relocatable)
167    return "ldscripts/${EMULATION_NAME}.xr";
168  else if (!config.text_read_only)
169    return "ldscripts/${EMULATION_NAME}.xbn";
170  else if (!config.magic_demand_paged)
171    return "ldscripts/${EMULATION_NAME}.xn";
172  else
173    return "ldscripts/${EMULATION_NAME}.x";
174}
175EOF
176
177fi
178
179cat >>e${EMULATION_NAME}.c <<EOF
180
181struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
182{
183  gld${EMULATION_NAME}_before_parse,
184  syslib_default,
185  hll_default,
186  after_parse_default,
187  after_open_default,
188  after_allocation_default,
189  set_output_arch_default,
190  ldemul_default_target,
191  gld${EMULATION_NAME}_before_allocation,
192  gld${EMULATION_NAME}_get_script,
193  "${EMULATION_NAME}",
194  "${OUTPUT_FORMAT}",
195  finish_default,
196  gld${EMULATION_NAME}_create_output_section_statements,
197  gld${EMULATION_NAME}_open_dynamic_archive,
198  NULL,	/* place orphan */
199  NULL,	/* set symbols */
200  NULL,	/* parse args */
201  NULL,	/* add_options */
202  NULL,	/* handle_option */
203  NULL,	/* unrecognized file */
204  NULL,	/* list options */
205  NULL,	/* recognized file */
206  NULL,	/* find_potential_libraries */
207  NULL	/* new_vers_pattern */
208};
209EOF
210