Add support for ELF shared libraries.
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script. DO NOT EDIT! */
5
6 /* 32 bit ELF emulation code for ${EMULATION_NAME}
7 Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
8 Written by Steve Chamberlain <sac@cygnus.com>
9 ELF support by Ian Lance Taylor <ian@cygnus.com>
10
11 This file is part of GLD, the Gnu Linker.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "config.h"
35 #include "ldmain.h"
36 #include "ldemul.h"
37 #include "ldfile.h"
38 #include "ldmisc.h"
39 #include "ldexp.h"
40 #include "ldlang.h"
41
42 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
43 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
44 static void gld${EMULATION_NAME}_find_statement_assignment
45 PARAMS ((lang_statement_union_type *));
46 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
47 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
48
49 static void
50 gld${EMULATION_NAME}_before_parse()
51 {
52 ldfile_output_architecture = bfd_arch_${ARCH};
53 config.dynamic_link = true;
54 }
55
56 /* This is called after the sections have been attached to output
57 sections, but before any sizes or addresses have been set. */
58
59 static void
60 gld${EMULATION_NAME}_before_allocation ()
61 {
62 /* If we are going to make any variable assignments, we need to let
63 the ELF backend know about them in case the variables are
64 referred to by dynamic objects. */
65 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
66
67 /* Let the ELF backend work out the sizes of any sections required
68 by dynamic linking. */
69 if (! bfd_elf32_size_dynamic_sections (output_bfd, &link_info))
70 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
71 }
72
73 /* This is called by the before_allocation routine via
74 lang_for_each_statement. It locates any assignment statements, and
75 tells the ELF backend about them, in case they are assignments to
76 symbols which are referred to by dynamic objects. */
77
78 static void
79 gld${EMULATION_NAME}_find_statement_assignment (s)
80 lang_statement_union_type *s;
81 {
82 if (s->header.type == lang_assignment_statement_enum)
83 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
84 }
85
86 /* Look through an expression for an assignment statement. */
87
88 static void
89 gld${EMULATION_NAME}_find_exp_assignment (exp)
90 etree_type *exp;
91 {
92 switch (exp->type.node_class)
93 {
94 case etree_assign:
95 if (strcmp (exp->assign.dst, ".") != 0)
96 {
97 if (! bfd_elf32_record_link_assignment (output_bfd, &link_info,
98 exp->assign.dst))
99 einfo ("%P%F: failed to record assignment to %s: %E\n",
100 exp->assign.dst);
101 }
102 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
103 break;
104
105 case etree_binary:
106 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
107 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
108 break;
109
110 case etree_trinary:
111 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
112 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
113 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
114 break;
115
116 case etree_unary:
117 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
118 break;
119
120 default:
121 break;
122 }
123 }
124
125 static char *
126 gld${EMULATION_NAME}_get_script(isfile)
127 int *isfile;
128 EOF
129
130 if test -n "$COMPILE_IN"
131 then
132 # Scripts compiled in.
133
134 # sed commands to quote an ld script as a C string.
135 sc='s/["\\]/\\&/g
136 s/$/\\n\\/
137 1s/^/"/
138 $s/$/n"/
139 '
140
141 cat >>e${EMULATION_NAME}.c <<EOF
142 {
143 *isfile = 0;
144
145 if (link_info.relocateable == true && config.build_constructors == true)
146 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
147 else if (link_info.relocateable == true)
148 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
149 else if (!config.text_read_only)
150 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
151 else if (!config.magic_demand_paged)
152 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
153 else
154 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
155 }
156 EOF
157
158 else
159 # Scripts read from the filesystem.
160
161 cat >>e${EMULATION_NAME}.c <<EOF
162 {
163 *isfile = 1;
164
165 if (link_info.relocateable == true && config.build_constructors == true)
166 return "ldscripts/${EMULATION_NAME}.xu";
167 else if (link_info.relocateable == true)
168 return "ldscripts/${EMULATION_NAME}.xr";
169 else if (!config.text_read_only)
170 return "ldscripts/${EMULATION_NAME}.xbn";
171 else if (!config.magic_demand_paged)
172 return "ldscripts/${EMULATION_NAME}.xn";
173 else
174 return "ldscripts/${EMULATION_NAME}.x";
175 }
176 EOF
177
178 fi
179
180 cat >>e${EMULATION_NAME}.c <<EOF
181
182 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
183 {
184 gld${EMULATION_NAME}_before_parse,
185 syslib_default,
186 hll_default,
187 after_parse_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 };
196 EOF
This page took 0.033425 seconds and 4 git commands to generate.