linux.em revision 104834
1233334Sgonzo# This shell script emits a C file. -*- C -*-
2233334Sgonzo# It does some substitutions.
3233334Sgonzoif [ -z "$MACHINE" ]; then 
4233334Sgonzo  OUTPUT_ARCH=${ARCH}
5233334Sgonzoelse
6233334Sgonzo  OUTPUT_ARCH=${ARCH}:${MACHINE}
7233334Sgonzofi
8233334Sgonzocat >e${EMULATION_NAME}.c <<EOF
9233334Sgonzo/* This file is is generated by a shell script.  DO NOT EDIT! */
10233334Sgonzo
11233334Sgonzo/* Linux a.out emulation code for ${EMULATION_NAME}
12233334Sgonzo   Copyright 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000
13233334Sgonzo   Free Software Foundation, Inc.
14233334Sgonzo   Written by Steve Chamberlain <sac@cygnus.com>
15233334Sgonzo   Linux support by Eric Youngdale <ericy@cais.cais.com>
16233334Sgonzo
17233334SgonzoThis file is part of GLD, the Gnu Linker.
18233334Sgonzo
19233334SgonzoThis program is free software; you can redistribute it and/or modify
20233334Sgonzoit under the terms of the GNU General Public License as published by
21233334Sgonzothe Free Software Foundation; either version 2 of the License, or
22233334Sgonzo(at your option) any later version.
23233334Sgonzo
24233334SgonzoThis program is distributed in the hope that it will be useful,
25233334Sgonzobut WITHOUT ANY WARRANTY; without even the implied warranty of
26233334SgonzoMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27233334SgonzoGNU General Public License for more details.
28233334Sgonzo
29233334SgonzoYou should have received a copy of the GNU General Public License
30233334Sgonzoalong with this program; if not, write to the Free Software
31233334SgonzoFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
32233334Sgonzo
33233334Sgonzo#define TARGET_IS_${EMULATION_NAME}
34233334Sgonzo
35233334Sgonzo#include "bfd.h"
36233334Sgonzo#include "sysdep.h"
37233334Sgonzo#include "bfdlink.h"
38233334Sgonzo
39233334Sgonzo#include "ld.h"
40233334Sgonzo#include "ldmain.h"
41233334Sgonzo#include "ldmisc.h"
42233334Sgonzo#include "ldexp.h"
43233334Sgonzo#include "ldlang.h"
44233334Sgonzo#include "ldfile.h"
45233334Sgonzo#include "ldemul.h"
46233334Sgonzo
47233334Sgonzostatic void gld${EMULATION_NAME}_before_parse PARAMS ((void));
48233334Sgonzostatic boolean gld${EMULATION_NAME}_open_dynamic_archive
49233334Sgonzo  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
50233334Sgonzostatic void gld${EMULATION_NAME}_find_address_statement
51233334Sgonzo  PARAMS ((lang_statement_union_type *));
52233334Sgonzostatic void gld${EMULATION_NAME}_create_output_section_statements
53233334Sgonzo  PARAMS ((void));
54233334Sgonzostatic void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
55233334Sgonzostatic char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
56233334Sgonzo
57233334Sgonzostatic void
58233334Sgonzogld${EMULATION_NAME}_before_parse()
59233334Sgonzo{
60233334Sgonzo  const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
61233334Sgonzo  if (arch)
62233334Sgonzo    {
63233334Sgonzo      ldfile_output_architecture = arch->arch;
64233334Sgonzo      ldfile_output_machine = arch->mach;
65233334Sgonzo      ldfile_output_machine_name = arch->printable_name;
66233334Sgonzo    }
67233334Sgonzo  else
68233334Sgonzo    ldfile_output_architecture = bfd_arch_${ARCH};
69233334Sgonzo  config.dynamic_link = true;
70233334Sgonzo  config.has_shared = true;
71233334Sgonzo}
72233334Sgonzo
73233334Sgonzo/* Try to open a dynamic archive.  This is where we know that Linux
74233334Sgonzo   dynamic libraries have an extension of .sa.  */
75233334Sgonzo
76233334Sgonzostatic boolean
77233334Sgonzogld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
78233334Sgonzo     const char *arch;
79233334Sgonzo     search_dirs_type *search;
80233334Sgonzo     lang_input_statement_type *entry;
81233334Sgonzo{
82233334Sgonzo  char *string;
83233334Sgonzo
84233334Sgonzo  if (! entry->is_archive)
85233334Sgonzo    return false;
86233334Sgonzo
87233334Sgonzo  string = (char *) xmalloc (strlen (search->name)
88233334Sgonzo			     + strlen (entry->filename)
89233334Sgonzo			     + strlen (arch)
90233334Sgonzo			     + sizeof "/lib.sa");
91233334Sgonzo
92233334Sgonzo  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
93233334Sgonzo
94233334Sgonzo  if (! ldfile_try_open_bfd (string, entry))
95233334Sgonzo    {
96233334Sgonzo      free (string);
97233334Sgonzo      return false;
98233334Sgonzo    }
99233334Sgonzo
100233334Sgonzo  entry->filename = string;
101233334Sgonzo
102233334Sgonzo  return true;
103233334Sgonzo}
104233334Sgonzo
105233334Sgonzo/* This is called by the create_output_section_statements routine via
106233334Sgonzo   lang_for_each_statement.  It locates any address assignment to
107233334Sgonzo   .text, and modifies it to include the size of the headers.  This
108233334Sgonzo   causes -Ttext to mean the starting address of the header, rather
109233334Sgonzo   than the starting address of .text, which is compatible with other
110233334Sgonzo   Linux tools.  */
111233334Sgonzo
112233334Sgonzostatic void
113233334Sgonzogld${EMULATION_NAME}_find_address_statement (s)
114233334Sgonzo     lang_statement_union_type *s;
115233334Sgonzo{
116233334Sgonzo  if (s->header.type == lang_address_statement_enum
117233334Sgonzo      && strcmp (s->address_statement.section_name, ".text") == 0)
118233334Sgonzo    {
119233334Sgonzo      ASSERT (s->address_statement.address->type.node_class == etree_value);
120233334Sgonzo      s->address_statement.address->value.value += 0x20;
121233334Sgonzo    }
122233334Sgonzo}
123233334Sgonzo
124233334Sgonzo/* This is called before opening the input BFD's.  */
125233334Sgonzo
126233334Sgonzostatic void
127233334Sgonzogld${EMULATION_NAME}_create_output_section_statements ()
128233334Sgonzo{
129233334Sgonzo  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
130233334Sgonzo}
131233334Sgonzo
132233334Sgonzo/* This is called after the sections have been attached to output
133233334Sgonzo   sections, but before any sizes or addresses have been set.  */
134233334Sgonzo
135233334Sgonzostatic void
136233334Sgonzogld${EMULATION_NAME}_before_allocation ()
137233334Sgonzo{
138233334Sgonzo  if (link_info.relocateable)
139233334Sgonzo    return;
140233334Sgonzo
141233334Sgonzo  /* Let the backend work out the sizes of any sections required by
142233334Sgonzo     dynamic linking.  */
143233334Sgonzo  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
144233334Sgonzo    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
145233334Sgonzo}
146233334Sgonzo
147233334Sgonzostatic char *
148233334Sgonzogld${EMULATION_NAME}_get_script(isfile)
149233334Sgonzo     int *isfile;
150233334SgonzoEOF
151233334Sgonzo
152233334Sgonzoif test -n "$COMPILE_IN"
153233334Sgonzothen
154233334Sgonzo# Scripts compiled in.
155233334Sgonzo
156233334Sgonzo# sed commands to quote an ld script as a C string.
157233334Sgonzosc="-f stringify.sed"
158233334Sgonzo
159233334Sgonzocat >>e${EMULATION_NAME}.c <<EOF
160233334Sgonzo{			     
161233334Sgonzo  *isfile = 0;
162233334Sgonzo
163233334Sgonzo  if (link_info.relocateable == true && config.build_constructors == true)
164233334Sgonzo    return
165233334SgonzoEOF
166233334Sgonzosed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
167233334Sgonzoecho '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
168233334Sgonzosed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
169233334Sgonzoecho '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
170233334Sgonzosed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
171233334Sgonzoecho '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
172233334Sgonzosed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
173233334Sgonzoecho '  ; else return'                                     >> e${EMULATION_NAME}.c
174233334Sgonzosed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
175233334Sgonzoecho '; }'                                                 >> e${EMULATION_NAME}.c
176233334Sgonzo
177233334Sgonzoelse
178233334Sgonzo# Scripts read from the filesystem.
179233334Sgonzo
180233334Sgonzocat >>e${EMULATION_NAME}.c <<EOF
181233334Sgonzo{			     
182233334Sgonzo  *isfile = 1;
183233334Sgonzo
184233334Sgonzo  if (link_info.relocateable == true && config.build_constructors == true)
185233334Sgonzo    return "ldscripts/${EMULATION_NAME}.xu";
186233334Sgonzo  else if (link_info.relocateable == true)
187233334Sgonzo    return "ldscripts/${EMULATION_NAME}.xr";
188233334Sgonzo  else if (!config.text_read_only)
189233334Sgonzo    return "ldscripts/${EMULATION_NAME}.xbn";
190233334Sgonzo  else if (!config.magic_demand_paged)
191233334Sgonzo    return "ldscripts/${EMULATION_NAME}.xn";
192233334Sgonzo  else
193233334Sgonzo    return "ldscripts/${EMULATION_NAME}.x";
194233334Sgonzo}
195233334SgonzoEOF
196
197fi
198
199cat >>e${EMULATION_NAME}.c <<EOF
200
201struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
202{
203  gld${EMULATION_NAME}_before_parse,
204  syslib_default,
205  hll_default,
206  after_parse_default,
207  after_open_default,
208  after_allocation_default,
209  set_output_arch_default,
210  ldemul_default_target,
211  gld${EMULATION_NAME}_before_allocation,
212  gld${EMULATION_NAME}_get_script,
213  "${EMULATION_NAME}",
214  "${OUTPUT_FORMAT}",
215  NULL,	/* finish */
216  gld${EMULATION_NAME}_create_output_section_statements,
217  gld${EMULATION_NAME}_open_dynamic_archive,
218  NULL,	/* place orphan */
219  NULL,	/* set symbols */
220  NULL,	/* parse args */
221  NULL,	/* unrecognized file */
222  NULL,	/* list options */
223  NULL,	/* recognized file */
224  NULL,	/* find_potential_libraries */
225  NULL	/* new_vers_pattern */
226};
227EOF
228