linux.em revision 33965
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/* Linux a.out emulation code for ${EMULATION_NAME}
7   Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
8   Written by Steve Chamberlain <sac@cygnus.com>
9   Linux support by Eric Youngdale <ericy@cais.cais.com>
10
11This file is part of GLD, the Gnu Linker.
12
13This program is free software; you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation; either version 2 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
26
27#define TARGET_IS_${EMULATION_NAME}
28
29#include "bfd.h"
30#include "sysdep.h"
31#include "bfdlink.h"
32
33#include "ld.h"
34#include "ldmain.h"
35#include "ldemul.h"
36#include "ldfile.h"
37#include "ldmisc.h"
38#include "ldexp.h"
39#include "ldlang.h"
40
41static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
42static boolean gld${EMULATION_NAME}_open_dynamic_archive
43  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
44static void gld${EMULATION_NAME}_find_address_statement
45  PARAMS ((lang_statement_union_type *));
46static void gld${EMULATION_NAME}_create_output_section_statements
47  PARAMS ((void));
48static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
49static 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}
57
58/* Try to open a dynamic archive.  This is where we know that Linux
59   dynamic libraries have an extension of .sa.  */
60
61static boolean
62gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
63     const char *arch;
64     search_dirs_type *search;
65     lang_input_statement_type *entry;
66{
67  char *string;
68
69  if (! entry->is_archive)
70    return false;
71
72  string = (char *) xmalloc (strlen (search->name)
73			     + strlen (entry->filename)
74			     + strlen (arch)
75			     + sizeof "/lib.sa");
76
77  sprintf (string, "%s/lib%s%s.sa", search->name, entry->filename, arch);
78
79  if (! ldfile_try_open_bfd (string, entry))
80    {
81      free (string);
82      return false;
83    }
84
85  entry->filename = string;
86
87  return true;
88}
89
90/* This is called by the create_output_section_statements routine via
91   lang_for_each_statement.  It locates any address assignment to
92   .text, and modifies it to include the size of the headers.  This
93   causes -Ttext to mean the starting address of the header, rather
94   than the starting address of .text, which is compatible with other
95   Linux tools.  */
96
97static void
98gld${EMULATION_NAME}_find_address_statement (s)
99     lang_statement_union_type *s;
100{
101  if (s->header.type == lang_address_statement_enum
102      && strcmp (s->address_statement.section_name, ".text") == 0)
103    {
104      ASSERT (s->address_statement.address->type.node_class == etree_value);
105      s->address_statement.address->value.value += 0x20;
106    }
107}
108
109/* This is called before opening the input BFD's.  */
110
111static void
112gld${EMULATION_NAME}_create_output_section_statements ()
113{
114  lang_for_each_statement (gld${EMULATION_NAME}_find_address_statement);
115}
116
117/* This is called after the sections have been attached to output
118   sections, but before any sizes or addresses have been set.  */
119
120static void
121gld${EMULATION_NAME}_before_allocation ()
122{
123  if (link_info.relocateable)
124    return;
125
126  /* Let the backend work out the sizes of any sections required by
127     dynamic linking.  */
128  if (! bfd_${EMULATION_NAME}_size_dynamic_sections (output_bfd, &link_info))
129    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
130}
131
132static char *
133gld${EMULATION_NAME}_get_script(isfile)
134     int *isfile;
135EOF
136
137if test -n "$COMPILE_IN"
138then
139# Scripts compiled in.
140
141# sed commands to quote an ld script as a C string.
142sc='s/["\\]/\\&/g
143s/$/\\n\\/
1441s/^/"/
145$s/$/n"/
146'
147
148cat >>e${EMULATION_NAME}.c <<EOF
149{			     
150  *isfile = 0;
151
152  if (link_info.relocateable == true && config.build_constructors == true)
153    return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
154  else if (link_info.relocateable == true)
155    return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
156  else if (!config.text_read_only)
157    return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
158  else if (!config.magic_demand_paged)
159    return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
160  else
161    return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
162}
163EOF
164
165else
166# Scripts read from the filesystem.
167
168cat >>e${EMULATION_NAME}.c <<EOF
169{			     
170  *isfile = 1;
171
172  if (link_info.relocateable == true && config.build_constructors == true)
173    return "ldscripts/${EMULATION_NAME}.xu";
174  else if (link_info.relocateable == true)
175    return "ldscripts/${EMULATION_NAME}.xr";
176  else if (!config.text_read_only)
177    return "ldscripts/${EMULATION_NAME}.xbn";
178  else if (!config.magic_demand_paged)
179    return "ldscripts/${EMULATION_NAME}.xn";
180  else
181    return "ldscripts/${EMULATION_NAME}.x";
182}
183EOF
184
185fi
186
187cat >>e${EMULATION_NAME}.c <<EOF
188
189struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
190{
191  gld${EMULATION_NAME}_before_parse,
192  syslib_default,
193  hll_default,
194  after_parse_default,
195  after_open_default,
196  after_allocation_default,
197  set_output_arch_default,
198  ldemul_default_target,
199  gld${EMULATION_NAME}_before_allocation,
200  gld${EMULATION_NAME}_get_script,
201  "${EMULATION_NAME}",
202  "${OUTPUT_FORMAT}",
203  NULL,
204  gld${EMULATION_NAME}_create_output_section_statements,
205  gld${EMULATION_NAME}_open_dynamic_archive
206};
207EOF
208