* ld.h (args_type): Add field export_dynamic.
[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
32 #include <ctype.h>
33
34 #include "bfdlink.h"
35
36 #include "ld.h"
37 #include "config.h"
38 #include "ldmain.h"
39 #include "ldemul.h"
40 #include "ldfile.h"
41 #include "ldmisc.h"
42 #include "ldexp.h"
43 #include "ldlang.h"
44 #include "ldgram.h"
45
46 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
47 static boolean gld${EMULATION_NAME}_open_dynamic_archive
48 PARAMS ((const char *, lang_input_statement_type *));
49 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
50 static void gld${EMULATION_NAME}_find_statement_assignment
51 PARAMS ((lang_statement_union_type *));
52 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
53 static boolean gld${EMULATION_NAME}_place_orphan
54 PARAMS ((lang_input_statement_type *, asection *));
55 static void gld${EMULATION_NAME}_place_section
56 PARAMS ((lang_statement_union_type *));
57 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
58
59 static void
60 gld${EMULATION_NAME}_before_parse()
61 {
62 ldfile_output_architecture = bfd_arch_${ARCH};
63 config.dynamic_link = ${DYNAMIC_LINK-true};
64 }
65
66 /* Try to open a dynamic archive. This is where we know that ELF
67 dynamic libraries have an extension of .so. */
68
69 static boolean
70 gld${EMULATION_NAME}_open_dynamic_archive (arch, entry)
71 const char *arch;
72 lang_input_statement_type *entry;
73 {
74 const char *filename;
75
76 filename = entry->filename;
77
78 if (! ldfile_open_file_search (arch, entry, "lib", ".so"))
79 return false;
80
81 /* We have found a dynamic object to include in the link. The ELF
82 backend linker will create a DT_NEEDED entry in the .dynamic
83 section naming this file. If this file includes a DT_SONAME
84 entry, it will be used. Otherwise, the ELF linker will just use
85 the name of the file. For an archive found by searching, like
86 this one, the DT_NEEDED entry should consist of just the name of
87 the file, without the path information used to find it. Note
88 that we only need to do this if we have a dynamic object; an
89 archive will never be referenced by a DT_NEEDED entry.
90
91 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
92 very pretty. I haven't been able to think of anything that is
93 pretty, though. */
94 if (bfd_check_format (entry->the_bfd, bfd_object)
95 && (entry->the_bfd->flags & DYNAMIC) != 0)
96 {
97 char *needed_name;
98
99 ASSERT (entry->is_archive && entry->search_dirs_flag);
100 needed_name = (char *) xmalloc (strlen (filename)
101 + strlen (arch)
102 + sizeof "lib.so");
103 sprintf (needed_name, "lib%s%s.so", filename, arch);
104 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
105 }
106
107 return true;
108 }
109
110 /* This is called after the sections have been attached to output
111 sections, but before any sizes or addresses have been set. */
112
113 static void
114 gld${EMULATION_NAME}_before_allocation ()
115 {
116 asection *sinterp;
117
118 /* If we are going to make any variable assignments, we need to let
119 the ELF backend know about them in case the variables are
120 referred to by dynamic objects. */
121 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
122
123 /* Let the ELF backend work out the sizes of any sections required
124 by dynamic linking. */
125 if (! bfd_elf32_size_dynamic_sections (output_bfd,
126 command_line.soname,
127 command_line.rpath,
128 command_line.export_dynamic,
129 &link_info,
130 &sinterp))
131 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
132
133 /* Let the user override the dynamic linker we are using. */
134 if (command_line.interpreter != NULL
135 && sinterp != NULL)
136 {
137 sinterp->contents = (bfd_byte *) command_line.interpreter;
138 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
139 }
140 }
141
142 /* This is called by the before_allocation routine via
143 lang_for_each_statement. It locates any assignment statements, and
144 tells the ELF backend about them, in case they are assignments to
145 symbols which are referred to by dynamic objects. */
146
147 static void
148 gld${EMULATION_NAME}_find_statement_assignment (s)
149 lang_statement_union_type *s;
150 {
151 if (s->header.type == lang_assignment_statement_enum)
152 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
153 }
154
155 /* Look through an expression for an assignment statement. */
156
157 static void
158 gld${EMULATION_NAME}_find_exp_assignment (exp)
159 etree_type *exp;
160 {
161 switch (exp->type.node_class)
162 {
163 case etree_assign:
164 if (strcmp (exp->assign.dst, ".") != 0)
165 {
166 if (! bfd_elf32_record_link_assignment (output_bfd, &link_info,
167 exp->assign.dst))
168 einfo ("%P%F: failed to record assignment to %s: %E\n",
169 exp->assign.dst);
170 }
171 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
172 break;
173
174 case etree_binary:
175 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
176 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
177 break;
178
179 case etree_trinary:
180 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
181 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
182 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
183 break;
184
185 case etree_unary:
186 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
187 break;
188
189 default:
190 break;
191 }
192 }
193
194 /* Place an orphan section. We use this to put random SHF_ALLOC
195 sections in the right segment. */
196
197 static asection *hold_section;
198 static lang_output_section_statement_type *hold_use;
199 static lang_output_section_statement_type *hold_text;
200 static lang_output_section_statement_type *hold_data;
201 static lang_output_section_statement_type *hold_bss;
202
203 /*ARGSUSED*/
204 static boolean
205 gld${EMULATION_NAME}_place_orphan (file, s)
206 lang_input_statement_type *file;
207 asection *s;
208 {
209 lang_output_section_statement_type *place;
210 asection *snew, **pps;
211 lang_statement_list_type *old;
212 lang_statement_list_type add;
213 etree_type *address;
214 const char *secname, *ps;
215 lang_output_section_statement_type *os;
216
217 if ((s->flags & SEC_ALLOC) == 0)
218 return false;
219
220 /* Look through the script to see where to place this section. */
221 hold_section = s;
222 hold_use = NULL;
223 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
224
225 if (hold_use != NULL)
226 {
227 /* We have already placed a section with this name. */
228 wild_doit (&hold_use->children, s, hold_use, file);
229 return true;
230 }
231
232 /* Decide which segment the section should go in based on the
233 section flags. */
234 place = NULL;
235 if ((s->flags & SEC_HAS_CONTENTS) == 0
236 && hold_bss != NULL)
237 place = hold_bss;
238 else if ((s->flags & SEC_READONLY) == 0
239 && hold_data != NULL)
240 place = hold_data;
241 else if ((s->flags & SEC_READONLY) != 0
242 && hold_text != NULL)
243 place = hold_text;
244 if (place == NULL)
245 return false;
246
247 secname = bfd_get_section_name (s->owner, s);
248
249 /* Create the section in the output file, and put it in the right
250 place. This shuffling to make the output file look neater, and
251 also means that the BFD backend does not have to sort the
252 sections in order by address. */
253 snew = bfd_make_section (output_bfd, secname);
254 if (snew == NULL)
255 einfo ("%P%F: output format %s cannot represent section called %s\n",
256 output_bfd->xvec->name, secname);
257 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
258 ;
259 *pps = snew->next;
260 snew->next = place->bfd_section->next;
261 place->bfd_section->next = snew;
262
263 /* Start building a list of statements for this section. */
264 old = stat_ptr;
265 stat_ptr = &add;
266 lang_list_init (stat_ptr);
267
268 /* If the name of the section is representable in C, then create
269 symbols to mark the start and the end of the section. */
270 for (ps = secname; *ps != '\0'; ps++)
271 if (! isalnum (*ps) && *ps != '_')
272 break;
273 if (*ps == '\0' && config.build_constructors)
274 {
275 char *symname;
276
277 symname = (char *) xmalloc (ps - secname + sizeof "__start_");
278 sprintf (symname, "__start_%s", secname);
279 lang_add_assignment (exp_assop ('=', symname,
280 exp_nameop (NAME, ".")));
281 }
282
283 if (! link_info.relocateable)
284 address = NULL;
285 else
286 address = exp_intop ((bfd_vma) 0);
287
288 lang_enter_output_section_statement (secname, address, 0,
289 (bfd_vma) 0,
290 (etree_type *) NULL,
291 (etree_type *) NULL,
292 (etree_type *) NULL);
293
294 os = lang_output_section_statement_lookup (secname);
295 wild_doit (&os->children, s, os, file);
296
297 lang_leave_output_section_statement ((bfd_vma) 0, "*default*");
298 stat_ptr = &add;
299
300 if (*ps == '\0' && config.build_constructors)
301 {
302 char *symname;
303
304 symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
305 sprintf (symname, "__stop_%s", secname);
306 lang_add_assignment (exp_assop ('=', symname,
307 exp_nameop (NAME, ".")));
308 }
309
310 /* Now stick the new statement list right after PLACE. */
311 *add.tail = place->header.next;
312 place->header.next = add.head;
313
314 stat_ptr = old;
315
316 return true;
317 }
318
319 static void
320 gld${EMULATION_NAME}_place_section (s)
321 lang_statement_union_type *s;
322 {
323 lang_output_section_statement_type *os;
324
325 if (s->header.type != lang_output_section_statement_enum)
326 return;
327
328 os = &s->output_section_statement;
329
330 if (strcmp (os->name, hold_section->name) == 0)
331 hold_use = os;
332
333 if (strcmp (os->name, ".text") == 0)
334 hold_text = os;
335 else if (strcmp (os->name, ".data") == 0)
336 hold_data = os;
337 else if (strcmp (os->name, ".bss") == 0)
338 hold_bss = os;
339 }
340
341 static char *
342 gld${EMULATION_NAME}_get_script(isfile)
343 int *isfile;
344 EOF
345
346 if test -n "$COMPILE_IN"
347 then
348 # Scripts compiled in.
349
350 # sed commands to quote an ld script as a C string.
351 sc='s/["\\]/\\&/g
352 s/$/\\n\\/
353 1s/^/"/
354 $s/$/n"/
355 '
356
357 cat >>e${EMULATION_NAME}.c <<EOF
358 {
359 *isfile = 0;
360
361 if (link_info.relocateable == true && config.build_constructors == true)
362 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
363 else if (link_info.relocateable == true)
364 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
365 else if (!config.text_read_only)
366 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
367 else if (!config.magic_demand_paged)
368 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
369 else if (link_info.shared)
370 return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
371 else
372 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
373 }
374 EOF
375
376 else
377 # Scripts read from the filesystem.
378
379 cat >>e${EMULATION_NAME}.c <<EOF
380 {
381 *isfile = 1;
382
383 if (link_info.relocateable == true && config.build_constructors == true)
384 return "ldscripts/${EMULATION_NAME}.xu";
385 else if (link_info.relocateable == true)
386 return "ldscripts/${EMULATION_NAME}.xr";
387 else if (!config.text_read_only)
388 return "ldscripts/${EMULATION_NAME}.xbn";
389 else if (!config.magic_demand_paged)
390 return "ldscripts/${EMULATION_NAME}.xn";
391 else if (link_info.shared)
392 return "ldscripts/${EMULATION_NAME}.xs";
393 else
394 return "ldscripts/${EMULATION_NAME}.x";
395 }
396 EOF
397
398 fi
399
400 cat >>e${EMULATION_NAME}.c <<EOF
401
402 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
403 {
404 gld${EMULATION_NAME}_before_parse,
405 syslib_default,
406 hll_default,
407 after_parse_default,
408 after_allocation_default,
409 set_output_arch_default,
410 ldemul_default_target,
411 gld${EMULATION_NAME}_before_allocation,
412 gld${EMULATION_NAME}_get_script,
413 "${EMULATION_NAME}",
414 "${OUTPUT_FORMAT}",
415 NULL,
416 NULL,
417 gld${EMULATION_NAME}_open_dynamic_archive,
418 gld${EMULATION_NAME}_place_orphan
419 };
420 EOF