Deleted Added
full compact
elf32.em (38890) elf32.em (42372)
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3# This file is now misnamed, because it supports both 32 bit and 64 bit
4# ELF emulations.
5test -z "${ELFSIZE}" && ELFSIZE=32
6cat >e${EMULATION_NAME}.c <<EOF
7/* This file is is generated by a shell script. DO NOT EDIT! */
8
9/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
10 Copyright (C) 1991, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
11 Written by Steve Chamberlain <sac@cygnus.com>
12 ELF support by Ian Lance Taylor <ian@cygnus.com>
13
14This file is part of GLD, the Gnu Linker.
15
16This program is free software; you can redistribute it and/or modify
17it under the terms of the GNU General Public License as published by
18the Free Software Foundation; either version 2 of the License, or
19(at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29
30#define TARGET_IS_${EMULATION_NAME}
31
32#include "bfd.h"
33#include "sysdep.h"
34
35#include <ctype.h>
36
37#include "bfdlink.h"
38
39#include "ld.h"
40#include "ldmain.h"
41#include "ldemul.h"
42#include "ldfile.h"
43#include "ldmisc.h"
44#include "ldexp.h"
45#include "ldlang.h"
46#include "ldgram.h"
47
48static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
49static boolean gld${EMULATION_NAME}_open_dynamic_archive
50 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
51static void gld${EMULATION_NAME}_after_open PARAMS ((void));
52static void gld${EMULATION_NAME}_check_needed
53 PARAMS ((lang_input_statement_type *));
54static void gld${EMULATION_NAME}_stat_needed
55 PARAMS ((lang_input_statement_type *));
56static boolean gld${EMULATION_NAME}_search_needed
57 PARAMS ((const char *, const char *, int));
58static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
59static void gld${EMULATION_NAME}_vercheck
60 PARAMS ((lang_input_statement_type *));
61static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
62static void gld${EMULATION_NAME}_find_statement_assignment
63 PARAMS ((lang_statement_union_type *));
64static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
65static boolean gld${EMULATION_NAME}_place_orphan
66 PARAMS ((lang_input_statement_type *, asection *));
67static void gld${EMULATION_NAME}_place_section
68 PARAMS ((lang_statement_union_type *));
69static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
70
71static void
72gld${EMULATION_NAME}_before_parse()
73{
74 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
75 config.dynamic_link = ${DYNAMIC_LINK-true};
76}
77
78/* Try to open a dynamic archive. This is where we know that ELF
79 dynamic libraries have an extension of .so. */
80
81static boolean
82gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
83 const char *arch;
84 search_dirs_type *search;
85 lang_input_statement_type *entry;
86{
87 const char *filename;
88 char *string;
89
90 if (! entry->is_archive)
91 return false;
92
93 filename = entry->filename;
94
95 string = (char *) xmalloc (strlen (search->name)
96 + strlen (filename)
97 + strlen (arch)
98 + sizeof "/lib.so");
99
100 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
101
102 if (! ldfile_try_open_bfd (string, entry))
103 {
104 free (string);
105 return false;
106 }
107
108 entry->filename = string;
109
110 /* We have found a dynamic object to include in the link. The ELF
111 backend linker will create a DT_NEEDED entry in the .dynamic
112 section naming this file. If this file includes a DT_SONAME
113 entry, it will be used. Otherwise, the ELF linker will just use
114 the name of the file. For an archive found by searching, like
115 this one, the DT_NEEDED entry should consist of just the name of
116 the file, without the path information used to find it. Note
117 that we only need to do this if we have a dynamic object; an
118 archive will never be referenced by a DT_NEEDED entry.
119
120 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
121 very pretty. I haven't been able to think of anything that is
122 pretty, though. */
123 if (bfd_check_format (entry->the_bfd, bfd_object)
124 && (entry->the_bfd->flags & DYNAMIC) != 0)
125 {
126 char *needed_name;
127
128 ASSERT (entry->is_archive && entry->search_dirs_flag);
129 needed_name = (char *) xmalloc (strlen (filename)
130 + strlen (arch)
131 + sizeof "lib.so");
132 sprintf (needed_name, "lib%s%s.so", filename, arch);
133 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
134 }
135
136 return true;
137}
138
139EOF
140if [ "x${host}" = "x${target}" ] ; then
141 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3# This file is now misnamed, because it supports both 32 bit and 64 bit
4# ELF emulations.
5test -z "${ELFSIZE}" && ELFSIZE=32
6cat >e${EMULATION_NAME}.c <<EOF
7/* This file is is generated by a shell script. DO NOT EDIT! */
8
9/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
10 Copyright (C) 1991, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
11 Written by Steve Chamberlain <sac@cygnus.com>
12 ELF support by Ian Lance Taylor <ian@cygnus.com>
13
14This file is part of GLD, the Gnu Linker.
15
16This program is free software; you can redistribute it and/or modify
17it under the terms of the GNU General Public License as published by
18the Free Software Foundation; either version 2 of the License, or
19(at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29
30#define TARGET_IS_${EMULATION_NAME}
31
32#include "bfd.h"
33#include "sysdep.h"
34
35#include <ctype.h>
36
37#include "bfdlink.h"
38
39#include "ld.h"
40#include "ldmain.h"
41#include "ldemul.h"
42#include "ldfile.h"
43#include "ldmisc.h"
44#include "ldexp.h"
45#include "ldlang.h"
46#include "ldgram.h"
47
48static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
49static boolean gld${EMULATION_NAME}_open_dynamic_archive
50 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
51static void gld${EMULATION_NAME}_after_open PARAMS ((void));
52static void gld${EMULATION_NAME}_check_needed
53 PARAMS ((lang_input_statement_type *));
54static void gld${EMULATION_NAME}_stat_needed
55 PARAMS ((lang_input_statement_type *));
56static boolean gld${EMULATION_NAME}_search_needed
57 PARAMS ((const char *, const char *, int));
58static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
59static void gld${EMULATION_NAME}_vercheck
60 PARAMS ((lang_input_statement_type *));
61static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
62static void gld${EMULATION_NAME}_find_statement_assignment
63 PARAMS ((lang_statement_union_type *));
64static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
65static boolean gld${EMULATION_NAME}_place_orphan
66 PARAMS ((lang_input_statement_type *, asection *));
67static void gld${EMULATION_NAME}_place_section
68 PARAMS ((lang_statement_union_type *));
69static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
70
71static void
72gld${EMULATION_NAME}_before_parse()
73{
74 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
75 config.dynamic_link = ${DYNAMIC_LINK-true};
76}
77
78/* Try to open a dynamic archive. This is where we know that ELF
79 dynamic libraries have an extension of .so. */
80
81static boolean
82gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
83 const char *arch;
84 search_dirs_type *search;
85 lang_input_statement_type *entry;
86{
87 const char *filename;
88 char *string;
89
90 if (! entry->is_archive)
91 return false;
92
93 filename = entry->filename;
94
95 string = (char *) xmalloc (strlen (search->name)
96 + strlen (filename)
97 + strlen (arch)
98 + sizeof "/lib.so");
99
100 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
101
102 if (! ldfile_try_open_bfd (string, entry))
103 {
104 free (string);
105 return false;
106 }
107
108 entry->filename = string;
109
110 /* We have found a dynamic object to include in the link. The ELF
111 backend linker will create a DT_NEEDED entry in the .dynamic
112 section naming this file. If this file includes a DT_SONAME
113 entry, it will be used. Otherwise, the ELF linker will just use
114 the name of the file. For an archive found by searching, like
115 this one, the DT_NEEDED entry should consist of just the name of
116 the file, without the path information used to find it. Note
117 that we only need to do this if we have a dynamic object; an
118 archive will never be referenced by a DT_NEEDED entry.
119
120 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
121 very pretty. I haven't been able to think of anything that is
122 pretty, though. */
123 if (bfd_check_format (entry->the_bfd, bfd_object)
124 && (entry->the_bfd->flags & DYNAMIC) != 0)
125 {
126 char *needed_name;
127
128 ASSERT (entry->is_archive && entry->search_dirs_flag);
129 needed_name = (char *) xmalloc (strlen (filename)
130 + strlen (arch)
131 + sizeof "lib.so");
132 sprintf (needed_name, "lib%s%s.so", filename, arch);
133 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
134 }
135
136 return true;
137}
138
139EOF
140if [ "x${host}" = "x${target}" ] ; then
141 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
142cat >>e${EMULATION_NAME}.c <<EOF
142 case ${target} in
143 *-*-freebsd*)
144 cat >>e${EMULATION_NAME}.c <<EOF
145/*
146 * Read the system search path the FreeBSD way rather than like Linux.
147 */
148#include <elf.h>
143
149
150static boolean gld${EMULATION_NAME}_check_ld_elf_hints
151 PARAMS ((const char *, int));
152
153static boolean
154gld${EMULATION_NAME}_check_ld_elf_hints (name, force)
155 const char *name;
156 int force;
157{
158 static boolean initialized;
159 static char *ld_elf_hints;
160
161 if (! initialized)
162 {
163 FILE *f;
164
165 f = fopen (_PATH_ELF_HINTS, FOPEN_RB);
166 if (f != NULL)
167 {
168 struct elfhints_hdr hdr;
169
170 if (fread(&hdr, 1, sizeof(hdr), f) == sizeof(hdr) &&
171 hdr.magic == ELFHINTS_MAGIC &&
172 hdr.version == 1)
173 {
174 if (fseek(f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
175 {
176 char *b;
177
178 b = (char *) xmalloc (hdr.dirlistlen + 1);
179 if (fread(b, 1, hdr.dirlistlen + 1, f) !=
180 hdr.dirlistlen + 1)
181 {
182 free(b);
183 }
184 else
185 {
186 ld_elf_hints = b;
187 }
188 }
189 }
190 fclose (f);
191 }
192
193 initialized = true;
194 }
195
196 if (ld_elf_hints == NULL)
197 return false;
198
199 return gld${EMULATION_NAME}_search_needed (ld_elf_hints, name, force);
200}
201EOF
202 ;;
203 *)
204 cat >>e${EMULATION_NAME}.c <<EOF
144/* For a native linker, check the file /etc/ld.so.conf for directories
145 in which we may find shared libraries. /etc/ld.so.conf is really
146 only meaningful on Linux, but we check it on other systems anyhow. */
147
148static boolean gld${EMULATION_NAME}_check_ld_so_conf
149 PARAMS ((const char *, int));
150
151static boolean
152gld${EMULATION_NAME}_check_ld_so_conf (name, force)
153 const char *name;
154 int force;
155{
156 static boolean initialized;
157 static char *ld_so_conf;
158
159 if (! initialized)
160 {
161 FILE *f;
162
163 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
164 if (f != NULL)
165 {
166 char *b;
167 size_t len, alloc;
168 int c;
169
170 len = 0;
171 alloc = 100;
172 b = (char *) xmalloc (alloc);
173
174 while ((c = getc (f)) != EOF)
175 {
176 if (len + 1 >= alloc)
177 {
178 alloc *= 2;
179 b = (char *) xrealloc (b, alloc);
180 }
181 if (c != ':'
182 && c != ' '
183 && c != '\t'
184 && c != '\n'
185 && c != ',')
186 {
187 b[len] = c;
188 ++len;
189 }
190 else
191 {
192 if (len > 0 && b[len - 1] != ':')
193 {
194 b[len] = ':';
195 ++len;
196 }
197 }
198 }
199
200 if (len > 0 && b[len - 1] == ':')
201 --len;
202
203 if (len > 0)
204 b[len] = '\0';
205 else
206 {
207 free (b);
208 b = NULL;
209 }
210
211 fclose (f);
212
213 ld_so_conf = b;
214 }
215
216 initialized = true;
217 }
218
219 if (ld_so_conf == NULL)
220 return false;
221
222 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
223}
205/* For a native linker, check the file /etc/ld.so.conf for directories
206 in which we may find shared libraries. /etc/ld.so.conf is really
207 only meaningful on Linux, but we check it on other systems anyhow. */
208
209static boolean gld${EMULATION_NAME}_check_ld_so_conf
210 PARAMS ((const char *, int));
211
212static boolean
213gld${EMULATION_NAME}_check_ld_so_conf (name, force)
214 const char *name;
215 int force;
216{
217 static boolean initialized;
218 static char *ld_so_conf;
219
220 if (! initialized)
221 {
222 FILE *f;
223
224 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
225 if (f != NULL)
226 {
227 char *b;
228 size_t len, alloc;
229 int c;
230
231 len = 0;
232 alloc = 100;
233 b = (char *) xmalloc (alloc);
234
235 while ((c = getc (f)) != EOF)
236 {
237 if (len + 1 >= alloc)
238 {
239 alloc *= 2;
240 b = (char *) xrealloc (b, alloc);
241 }
242 if (c != ':'
243 && c != ' '
244 && c != '\t'
245 && c != '\n'
246 && c != ',')
247 {
248 b[len] = c;
249 ++len;
250 }
251 else
252 {
253 if (len > 0 && b[len - 1] != ':')
254 {
255 b[len] = ':';
256 ++len;
257 }
258 }
259 }
260
261 if (len > 0 && b[len - 1] == ':')
262 --len;
263
264 if (len > 0)
265 b[len] = '\0';
266 else
267 {
268 free (b);
269 b = NULL;
270 }
271
272 fclose (f);
273
274 ld_so_conf = b;
275 }
276
277 initialized = true;
278 }
279
280 if (ld_so_conf == NULL)
281 return false;
282
283 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
284}
224
225EOF
285EOF
286 esac
226 fi
227fi
228cat >>e${EMULATION_NAME}.c <<EOF
229
230/* These variables are required to pass information back and forth
231 between after_open and check_needed and stat_needed and vercheck. */
232
233static struct bfd_link_needed_list *global_needed;
234static struct stat global_stat;
235static boolean global_found;
236static struct bfd_link_needed_list *global_vercheck_needed;
237static boolean global_vercheck_failed;
238
239/* This is called after all the input files have been opened. */
240
241static void
242gld${EMULATION_NAME}_after_open ()
243{
244 struct bfd_link_needed_list *needed, *l;
245
246 /* We only need to worry about this when doing a final link. */
247 if (link_info.relocateable || link_info.shared)
248 return;
249
250 /* Get the list of files which appear in DT_NEEDED entries in
251 dynamic objects included in the link (often there will be none).
252 For each such file, we want to track down the corresponding
253 library, and include the symbol table in the link. This is what
254 the runtime dynamic linker will do. Tracking the files down here
255 permits one dynamic object to include another without requiring
256 special action by the person doing the link. Note that the
257 needed list can actually grow while we are stepping through this
258 loop. */
259 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
260 for (l = needed; l != NULL; l = l->next)
261 {
262 struct bfd_link_needed_list *ll;
263 int force;
264
265 /* If we've already seen this file, skip it. */
266 for (ll = needed; ll != l; ll = ll->next)
267 if (strcmp (ll->name, l->name) == 0)
268 break;
269 if (ll != l)
270 continue;
271
272 /* See if this file was included in the link explicitly. */
273 global_needed = l;
274 global_found = false;
275 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
276 if (global_found)
277 continue;
278
279 /* We need to find this file and include the symbol table. We
280 want to search for the file in the same way that the dynamic
281 linker will search. That means that we want to use
282 rpath_link, rpath, then the environment variable
283 LD_LIBRARY_PATH (native only), then the linker script
284 LIB_SEARCH_DIRS. We do not search using the -L arguments.
285
286 We search twice. The first time, we skip objects which may
287 introduce version mismatches. The second time, we force
288 their use. See gld${EMULATION_NAME}_vercheck comment. */
289 for (force = 0; force < 2; force++)
290 {
291 const char *lib_path;
292 size_t len;
293 search_dirs_type *search;
294
295 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
296 l->name, force))
297 break;
298 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
299 l->name, force))
300 break;
301 if (command_line.rpath_link == NULL
302 && command_line.rpath == NULL)
303 {
304 lib_path = (const char *) getenv ("LD_RUN_PATH");
305 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
306 force))
307 break;
308 }
309EOF
310if [ "x${host}" = "x${target}" ] ; then
311 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
312cat >>e${EMULATION_NAME}.c <<EOF
313 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
314 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
315 break;
316EOF
317 fi
318fi
319cat >>e${EMULATION_NAME}.c <<EOF
320 len = strlen (l->name);
321 for (search = search_head; search != NULL; search = search->next)
322 {
323 char *filename;
324
325 if (search->cmdline)
326 continue;
327 filename = (char *) xmalloc (strlen (search->name) + len + 2);
328 sprintf (filename, "%s/%s", search->name, l->name);
329 if (gld${EMULATION_NAME}_try_needed (filename, force))
330 break;
331 free (filename);
332 }
333 if (search != NULL)
334 break;
335EOF
336if [ "x${host}" = "x${target}" ] ; then
337 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
287 fi
288fi
289cat >>e${EMULATION_NAME}.c <<EOF
290
291/* These variables are required to pass information back and forth
292 between after_open and check_needed and stat_needed and vercheck. */
293
294static struct bfd_link_needed_list *global_needed;
295static struct stat global_stat;
296static boolean global_found;
297static struct bfd_link_needed_list *global_vercheck_needed;
298static boolean global_vercheck_failed;
299
300/* This is called after all the input files have been opened. */
301
302static void
303gld${EMULATION_NAME}_after_open ()
304{
305 struct bfd_link_needed_list *needed, *l;
306
307 /* We only need to worry about this when doing a final link. */
308 if (link_info.relocateable || link_info.shared)
309 return;
310
311 /* Get the list of files which appear in DT_NEEDED entries in
312 dynamic objects included in the link (often there will be none).
313 For each such file, we want to track down the corresponding
314 library, and include the symbol table in the link. This is what
315 the runtime dynamic linker will do. Tracking the files down here
316 permits one dynamic object to include another without requiring
317 special action by the person doing the link. Note that the
318 needed list can actually grow while we are stepping through this
319 loop. */
320 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
321 for (l = needed; l != NULL; l = l->next)
322 {
323 struct bfd_link_needed_list *ll;
324 int force;
325
326 /* If we've already seen this file, skip it. */
327 for (ll = needed; ll != l; ll = ll->next)
328 if (strcmp (ll->name, l->name) == 0)
329 break;
330 if (ll != l)
331 continue;
332
333 /* See if this file was included in the link explicitly. */
334 global_needed = l;
335 global_found = false;
336 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
337 if (global_found)
338 continue;
339
340 /* We need to find this file and include the symbol table. We
341 want to search for the file in the same way that the dynamic
342 linker will search. That means that we want to use
343 rpath_link, rpath, then the environment variable
344 LD_LIBRARY_PATH (native only), then the linker script
345 LIB_SEARCH_DIRS. We do not search using the -L arguments.
346
347 We search twice. The first time, we skip objects which may
348 introduce version mismatches. The second time, we force
349 their use. See gld${EMULATION_NAME}_vercheck comment. */
350 for (force = 0; force < 2; force++)
351 {
352 const char *lib_path;
353 size_t len;
354 search_dirs_type *search;
355
356 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
357 l->name, force))
358 break;
359 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
360 l->name, force))
361 break;
362 if (command_line.rpath_link == NULL
363 && command_line.rpath == NULL)
364 {
365 lib_path = (const char *) getenv ("LD_RUN_PATH");
366 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
367 force))
368 break;
369 }
370EOF
371if [ "x${host}" = "x${target}" ] ; then
372 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
373cat >>e${EMULATION_NAME}.c <<EOF
374 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
375 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
376 break;
377EOF
378 fi
379fi
380cat >>e${EMULATION_NAME}.c <<EOF
381 len = strlen (l->name);
382 for (search = search_head; search != NULL; search = search->next)
383 {
384 char *filename;
385
386 if (search->cmdline)
387 continue;
388 filename = (char *) xmalloc (strlen (search->name) + len + 2);
389 sprintf (filename, "%s/%s", search->name, l->name);
390 if (gld${EMULATION_NAME}_try_needed (filename, force))
391 break;
392 free (filename);
393 }
394 if (search != NULL)
395 break;
396EOF
397if [ "x${host}" = "x${target}" ] ; then
398 if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
338cat >>e${EMULATION_NAME}.c <<EOF
399 case ${target} in
400 *-*-freebsd*)
401 cat >>e${EMULATION_NAME}.c <<EOF
402 if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force))
403 break;
404EOF
405 ;;
406 *)
407 cat >>e${EMULATION_NAME}.c <<EOF
339 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
340 break;
341EOF
408 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
409 break;
410EOF
411 ;;
412 esac
342 fi
343fi
344cat >>e${EMULATION_NAME}.c <<EOF
345 }
346
347 if (force < 2)
348 continue;
349
350 einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
351 l->name, l->by);
352 }
353}
354
355/* Search for a needed file in a path. */
356
357static boolean
358gld${EMULATION_NAME}_search_needed (path, name, force)
359 const char *path;
360 const char *name;
361 int force;
362{
363 const char *s;
364 size_t len;
365
366 if (path == NULL || *path == '\0')
367 return false;
368 len = strlen (name);
369 while (1)
370 {
371 char *filename, *sset;
372
373 s = strchr (path, ':');
374 if (s == NULL)
375 s = path + strlen (path);
376
377 filename = (char *) xmalloc (s - path + len + 2);
378 if (s == path)
379 sset = filename;
380 else
381 {
382 memcpy (filename, path, s - path);
383 filename[s - path] = '/';
384 sset = filename + (s - path) + 1;
385 }
386 strcpy (sset, name);
387
388 if (gld${EMULATION_NAME}_try_needed (filename, force))
389 return true;
390
391 free (filename);
392
393 if (*s == '\0')
394 break;
395 path = s + 1;
396 }
397
398 return false;
399}
400
401/* This function is called for each possible name for a dynamic object
402 named by a DT_NEEDED entry. The FORCE parameter indicates whether
403 to skip the check for a conflicting version. */
404
405static boolean
406gld${EMULATION_NAME}_try_needed (name, force)
407 const char *name;
408 int force;
409{
410 bfd *abfd;
411
412 abfd = bfd_openr (name, bfd_get_target (output_bfd));
413 if (abfd == NULL)
414 return false;
415 if (! bfd_check_format (abfd, bfd_object))
416 {
417 (void) bfd_close (abfd);
418 return false;
419 }
420 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
421 {
422 (void) bfd_close (abfd);
423 return false;
424 }
425
426 /* Check whether this object would include any conflicting library
427 versions. If FORCE is set, then we skip this check; we use this
428 the second time around, if we couldn't find any compatible
429 instance of the shared library. */
430
431 if (! force)
432 {
433 struct bfd_link_needed_list *needed;
434
435 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
436 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
437
438 if (needed != NULL)
439 {
440 global_vercheck_needed = needed;
441 global_vercheck_failed = false;
442 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
443 if (global_vercheck_failed)
444 {
445 (void) bfd_close (abfd);
446 /* Return false to force the caller to move on to try
447 another file on the search path. */
448 return false;
449 }
450
451 /* But wait! It gets much worse. On Linux, if a shared
452 library does not use libc at all, we are supposed to skip
453 it the first time around in case we encounter a shared
454 library later on with the same name which does use the
455 version of libc that we want. This is much too horrible
456 to use on any system other than Linux. */
457
458EOF
459case ${target} in
460 *-*-linux-gnu*)
461 cat >>e${EMULATION_NAME}.c <<EOF
462 {
463 struct bfd_link_needed_list *l;
464
465 for (l = needed; l != NULL; l = l->next)
466 if (strncmp (l->name, "libc.so", 7) == 0)
467 break;
468 if (l == NULL)
469 {
470 (void) bfd_close (abfd);
471 return false;
472 }
473 }
474
475EOF
476 ;;
477esac
478cat >>e${EMULATION_NAME}.c <<EOF
479 }
480 }
481
482 /* We've found a dynamic object matching the DT_NEEDED entry. */
483
484 /* We have already checked that there is no other input file of the
485 same name. We must now check again that we are not including the
486 same file twice. We need to do this because on many systems
487 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
488 reference libc.so.1. If we have already included libc.so, we
489 don't want to include libc.so.1 if they are the same file, and we
490 can only check that using stat. */
491
492 if (bfd_stat (abfd, &global_stat) != 0)
493 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
494 global_found = false;
495 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
496 if (global_found)
497 {
498 /* Return true to indicate that we found the file, even though
499 we aren't going to do anything with it. */
500 return true;
501 }
502
503 /* Tell the ELF backend that don't want the output file to have a
504 DT_NEEDED entry for this file. */
505 bfd_elf_set_dt_needed_name (abfd, "");
506
507 /* Add this file into the symbol table. */
508 if (! bfd_link_add_symbols (abfd, &link_info))
509 einfo ("%F%B: could not read symbols: %E\n", abfd);
510
511 return true;
512}
513
514/* See if an input file matches a DT_NEEDED entry by name. */
515
516static void
517gld${EMULATION_NAME}_check_needed (s)
518 lang_input_statement_type *s;
519{
520 if (global_found)
521 return;
522
523 if (s->filename != NULL
524 && strcmp (s->filename, global_needed->name) == 0)
525 {
526 global_found = true;
527 return;
528 }
529
530 if (s->the_bfd != NULL)
531 {
532 const char *soname;
533
534 soname = bfd_elf_get_dt_soname (s->the_bfd);
535 if (soname != NULL
536 && strcmp (soname, global_needed->name) == 0)
537 {
538 global_found = true;
539 return;
540 }
541 }
542
543 if (s->search_dirs_flag
544 && s->filename != NULL
545 && strchr (global_needed->name, '/') == NULL)
546 {
547 const char *f;
548
549 f = strrchr (s->filename, '/');
550 if (f != NULL
551 && strcmp (f + 1, global_needed->name) == 0)
552 {
553 global_found = true;
554 return;
555 }
556 }
557}
558
559/* See if an input file matches a DT_NEEDED entry by running stat on
560 the file. */
561
562static void
563gld${EMULATION_NAME}_stat_needed (s)
564 lang_input_statement_type *s;
565{
566 struct stat st;
567 const char *suffix;
568 const char *soname;
569 const char *f;
570
571 if (global_found)
572 return;
573 if (s->the_bfd == NULL)
574 return;
575
576 if (bfd_stat (s->the_bfd, &st) != 0)
577 {
578 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
579 return;
580 }
581
582 if (st.st_dev == global_stat.st_dev
583 && st.st_ino == global_stat.st_ino)
584 {
585 global_found = true;
586 return;
587 }
588
589 /* We issue a warning if it looks like we are including two
590 different versions of the same shared library. For example,
591 there may be a problem if -lc picks up libc.so.6 but some other
592 shared library has a DT_NEEDED entry of libc.so.5. This is a
593 hueristic test, and it will only work if the name looks like
594 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
595 If we really want to issue warnings about mixing version numbers
596 of shared libraries, we need to find a better way. */
597
598 if (strchr (global_needed->name, '/') != NULL)
599 return;
600 suffix = strstr (global_needed->name, ".so.");
601 if (suffix == NULL)
602 return;
603 suffix += sizeof ".so." - 1;
604
605 soname = bfd_elf_get_dt_soname (s->the_bfd);
606 if (soname == NULL)
607 soname = s->filename;
608
609 f = strrchr (soname, '/');
610 if (f != NULL)
611 ++f;
612 else
613 f = soname;
614
615 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
616 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
617 global_needed->name, global_needed->by, f);
618}
619
620/* On Linux, it's possible to have different versions of the same
621 shared library linked against different versions of libc. The
622 dynamic linker somehow tags which libc version to use in
623 /etc/ld.so.cache, and, based on the libc that it sees in the
624 executable, chooses which version of the shared library to use.
625
626 We try to do a similar check here by checking whether this shared
627 library needs any other shared libraries which may conflict with
628 libraries we have already included in the link. If it does, we
629 skip it, and try to find another shared library farther on down the
630 link path.
631
632 This is called via lang_for_each_input_file.
633 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
634 which we ar checking. This sets GLOBAL_VERCHECK_FAILED if we find
635 a conflicting version. */
636
637static void
638gld${EMULATION_NAME}_vercheck (s)
639 lang_input_statement_type *s;
640{
641 const char *soname, *f;
642 struct bfd_link_needed_list *l;
643
644 if (global_vercheck_failed)
645 return;
646 if (s->the_bfd == NULL
647 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
648 return;
649
650 soname = bfd_elf_get_dt_soname (s->the_bfd);
651 if (soname == NULL)
652 soname = bfd_get_filename (s->the_bfd);
653
654 f = strrchr (soname, '/');
655 if (f != NULL)
656 ++f;
657 else
658 f = soname;
659
660 for (l = global_vercheck_needed; l != NULL; l = l->next)
661 {
662 const char *suffix;
663
664 if (strcmp (f, l->name) == 0)
665 {
666 /* Probably can't happen, but it's an easy check. */
667 continue;
668 }
669
670 if (strchr (l->name, '/') != NULL)
671 continue;
672
673 suffix = strstr (l->name, ".so.");
674 if (suffix == NULL)
675 continue;
676
677 suffix += sizeof ".so." - 1;
678
679 if (strncmp (f, l->name, suffix - l->name) == 0)
680 {
681 /* Here we know that S is a dynamic object FOO.SO.VER1, and
682 the object we are considering needs a dynamic object
683 FOO.SO.VER2, and VER1 and VER2 are different. This
684 appears to be a version mismatch, so we tell the caller
685 to try a different version of this library. */
686 global_vercheck_failed = true;
687 return;
688 }
689 }
690}
691
692/* This is called after the sections have been attached to output
693 sections, but before any sizes or addresses have been set. */
694
695static void
696gld${EMULATION_NAME}_before_allocation ()
697{
698 const char *rpath;
699 asection *sinterp;
700
701 /* If we are going to make any variable assignments, we need to let
702 the ELF backend know about them in case the variables are
703 referred to by dynamic objects. */
704 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
705
706 /* Let the ELF backend work out the sizes of any sections required
707 by dynamic linking. */
708 rpath = command_line.rpath;
709 if (rpath == NULL)
710 rpath = (const char *) getenv ("LD_RUN_PATH");
711 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
712 (output_bfd, command_line.soname, rpath,
713 command_line.export_dynamic, command_line.filter_shlib,
714 (const char * const *) command_line.auxiliary_filters,
715 &link_info, &sinterp, lang_elf_version_info)))
716 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
717
718 /* Let the user override the dynamic linker we are using. */
719 if (command_line.interpreter != NULL
720 && sinterp != NULL)
721 {
722 sinterp->contents = (bfd_byte *) command_line.interpreter;
723 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
724 }
725
726 /* Look for any sections named .gnu.warning. As a GNU extensions,
727 we treat such sections as containing warning messages. We print
728 out the warning message, and then zero out the section size so
729 that it does not get copied into the output file. */
730
731 {
732 LANG_FOR_EACH_INPUT_STATEMENT (is)
733 {
734 asection *s;
735 bfd_size_type sz;
736 char *msg;
737 boolean ret;
738
739 if (is->just_syms_flag)
740 continue;
741
742 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
743 if (s == NULL)
744 continue;
745
746 sz = bfd_section_size (is->the_bfd, s);
747 msg = xmalloc ((size_t) sz + 1);
748 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
749 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
750 is->the_bfd);
751 msg[sz] = '\0';
752 ret = link_info.callbacks->warning (&link_info, msg,
753 (const char *) NULL,
754 is->the_bfd, (asection *) NULL,
755 (bfd_vma) 0);
756 ASSERT (ret);
757 free (msg);
758
759 /* Clobber the section size, so that we don't waste copying the
760 warning into the output file. */
761 s->_raw_size = 0;
762 }
763 }
764
765#if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
766 /* For MIPS ELF the .reginfo section requires special handling.
767 Each input section is 24 bytes, and the final output section must
768 also be 24 bytes. We handle this by clobbering all but the first
769 input section size to 0. The .reginfo section is handled
770 specially by the backend code anyhow. */
771 {
772 boolean found = false;
773 LANG_FOR_EACH_INPUT_STATEMENT (is)
774 {
775 asection *s;
776
777 if (is->just_syms_flag)
778 continue;
779
780 s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
781 if (s == NULL)
782 continue;
783
784 if (! found)
785 {
786 found = true;
787 continue;
788 }
789
790 s->_raw_size = 0;
791 s->_cooked_size = 0;
792 }
793 }
794#endif
795}
796
797/* This is called by the before_allocation routine via
798 lang_for_each_statement. It locates any assignment statements, and
799 tells the ELF backend about them, in case they are assignments to
800 symbols which are referred to by dynamic objects. */
801
802static void
803gld${EMULATION_NAME}_find_statement_assignment (s)
804 lang_statement_union_type *s;
805{
806 if (s->header.type == lang_assignment_statement_enum)
807 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
808}
809
810/* Look through an expression for an assignment statement. */
811
812static void
813gld${EMULATION_NAME}_find_exp_assignment (exp)
814 etree_type *exp;
815{
816 struct bfd_link_hash_entry *h;
817
818 switch (exp->type.node_class)
819 {
820 case etree_provide:
821 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
822 false, false, false);
823 if (h == NULL)
824 break;
825
826 /* We call record_link_assignment even if the symbol is defined.
827 This is because if it is defined by a dynamic object, we
828 actually want to use the value defined by the linker script,
829 not the value from the dynamic object (because we are setting
830 symbols like etext). If the symbol is defined by a regular
831 object, then, as it happens, calling record_link_assignment
832 will do no harm. */
833
834 /* Fall through. */
835 case etree_assign:
836 if (strcmp (exp->assign.dst, ".") != 0)
837 {
838 if (! (bfd_elf${ELFSIZE}_record_link_assignment
839 (output_bfd, &link_info, exp->assign.dst,
840 exp->type.node_class == etree_provide ? true : false)))
841 einfo ("%P%F: failed to record assignment to %s: %E\n",
842 exp->assign.dst);
843 }
844 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
845 break;
846
847 case etree_binary:
848 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
849 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
850 break;
851
852 case etree_trinary:
853 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
854 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
855 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
856 break;
857
858 case etree_unary:
859 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
860 break;
861
862 default:
863 break;
864 }
865}
866
867/* Place an orphan section. We use this to put random SHF_ALLOC
868 sections in the right segment. */
869
870static asection *hold_section;
871static lang_output_section_statement_type *hold_use;
872static lang_output_section_statement_type *hold_text;
873static lang_output_section_statement_type *hold_rodata;
874static lang_output_section_statement_type *hold_data;
875static lang_output_section_statement_type *hold_bss;
876static lang_output_section_statement_type *hold_rel;
877static lang_output_section_statement_type *hold_interp;
878
879/*ARGSUSED*/
880static boolean
881gld${EMULATION_NAME}_place_orphan (file, s)
882 lang_input_statement_type *file;
883 asection *s;
884{
885 lang_output_section_statement_type *place;
886 asection *snew, **pps;
887 lang_statement_list_type *old;
888 lang_statement_list_type add;
889 etree_type *address;
890 const char *secname, *ps;
891 const char *outsecname;
892 lang_output_section_statement_type *os;
893
894 if ((s->flags & SEC_ALLOC) == 0)
895 return false;
896
897 /* Look through the script to see where to place this section. */
898 hold_section = s;
899 hold_use = NULL;
900 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
901
902 if (hold_use != NULL)
903 {
904 /* We have already placed a section with this name. */
905 wild_doit (&hold_use->children, s, hold_use, file);
906 return true;
907 }
908
909 secname = bfd_get_section_name (s->owner, s);
910
911 /* If this is a final link, then always put .gnu.warning.SYMBOL
912 sections into the .text section to get them out of the way. */
913 if (! link_info.shared
914 && ! link_info.relocateable
915 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
916 && hold_text != NULL)
917 {
918 wild_doit (&hold_text->children, s, hold_text, file);
919 return true;
920 }
921
922 /* Decide which segment the section should go in based on the
923 section name and section flags. We put loadable .note sections
924 right after the .interp section, so that the PT_NOTE segment is
925 stored right after the program headers where the OS can read it
926 in the first page. */
927 place = NULL;
928 if ((s->flags & SEC_LOAD) != 0
929 && strncmp (secname, ".note", 4) == 0
930 && hold_interp != NULL)
931 place = hold_interp;
932 else if ((s->flags & SEC_HAS_CONTENTS) == 0
933 && hold_bss != NULL)
934 place = hold_bss;
935 else if ((s->flags & SEC_READONLY) == 0
936 && hold_data != NULL)
937 place = hold_data;
938 else if (strncmp (secname, ".rel", 4) == 0
939 && hold_rel != NULL)
940 place = hold_rel;
941 else if ((s->flags & SEC_CODE) == 0
942 && (s->flags & SEC_READONLY) != 0
943 && hold_rodata != NULL)
944 place = hold_rodata;
945 else if ((s->flags & SEC_READONLY) != 0
946 && hold_text != NULL)
947 place = hold_text;
948 if (place == NULL)
949 return false;
950
951 /* Choose a unique name for the section. This will be needed if the
952 same section name appears in the input file with different
953 loadable or allocateable characteristics. */
954 outsecname = secname;
955 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
956 {
957 unsigned int len;
958 char *newname;
959 unsigned int i;
960
961 len = strlen (outsecname);
962 newname = xmalloc (len + 5);
963 strcpy (newname, outsecname);
964 i = 0;
965 do
966 {
967 sprintf (newname + len, "%d", i);
968 ++i;
969 }
970 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
971
972 outsecname = newname;
973 }
974
975 /* Create the section in the output file, and put it in the right
976 place. This shuffling is to make the output file look neater. */
977 snew = bfd_make_section (output_bfd, outsecname);
978 if (snew == NULL)
979 einfo ("%P%F: output format %s cannot represent section called %s\n",
980 output_bfd->xvec->name, outsecname);
981 if (place->bfd_section != NULL)
982 {
983 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
984 ;
985 *pps = snew->next;
986 snew->next = place->bfd_section->next;
987 place->bfd_section->next = snew;
988 }
989
990 /* Start building a list of statements for this section. */
991 old = stat_ptr;
992 stat_ptr = &add;
993 lang_list_init (stat_ptr);
994
995 /* If the name of the section is representable in C, then create
996 symbols to mark the start and the end of the section. */
997 for (ps = outsecname; *ps != '\0'; ps++)
998 if (! isalnum ((unsigned char) *ps) && *ps != '_')
999 break;
1000 if (*ps == '\0' && config.build_constructors)
1001 {
1002 char *symname;
1003
1004 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1005 sprintf (symname, "__start_%s", outsecname);
1006 lang_add_assignment (exp_assop ('=', symname,
1007 exp_unop (ALIGN_K,
1008 exp_intop ((bfd_vma) 1
1009 << s->alignment_power))));
1010 }
1011
1012 if (! link_info.relocateable)
1013 address = NULL;
1014 else
1015 address = exp_intop ((bfd_vma) 0);
1016
1017 lang_enter_output_section_statement (outsecname, address, 0,
1018 (bfd_vma) 0,
1019 (etree_type *) NULL,
1020 (etree_type *) NULL,
1021 (etree_type *) NULL);
1022
1023 os = lang_output_section_statement_lookup (outsecname);
1024 wild_doit (&os->children, s, os, file);
1025
1026 lang_leave_output_section_statement
1027 ((bfd_vma) 0, "*default*", (struct lang_output_section_phdr_list *) NULL);
1028 stat_ptr = &add;
1029
1030 if (*ps == '\0' && config.build_constructors)
1031 {
1032 char *symname;
1033
1034 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1035 sprintf (symname, "__stop_%s", outsecname);
1036 lang_add_assignment (exp_assop ('=', symname,
1037 exp_nameop (NAME, ".")));
1038 }
1039
1040 /* Now stick the new statement list right after PLACE. */
1041 *add.tail = place->header.next;
1042 place->header.next = add.head;
1043
1044 stat_ptr = old;
1045
1046 return true;
1047}
1048
1049static void
1050gld${EMULATION_NAME}_place_section (s)
1051 lang_statement_union_type *s;
1052{
1053 lang_output_section_statement_type *os;
1054
1055 if (s->header.type != lang_output_section_statement_enum)
1056 return;
1057
1058 os = &s->output_section_statement;
1059
1060 if (strcmp (os->name, hold_section->name) == 0
1061 && os->bfd_section != NULL
1062 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1063 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1064 hold_use = os;
1065
1066 if (strcmp (os->name, ".text") == 0)
1067 hold_text = os;
1068 else if (strcmp (os->name, ".rodata") == 0)
1069 hold_rodata = os;
1070 else if (strcmp (os->name, ".data") == 0)
1071 hold_data = os;
1072 else if (strcmp (os->name, ".bss") == 0)
1073 hold_bss = os;
1074 else if (hold_rel == NULL
1075 && os->bfd_section != NULL
1076 && (os->bfd_section->flags & SEC_ALLOC) != 0
1077 && strncmp (os->name, ".rel", 4) == 0)
1078 hold_rel = os;
1079 else if (strcmp (os->name, ".interp") == 0)
1080 hold_interp = os;
1081}
1082
1083static char *
1084gld${EMULATION_NAME}_get_script(isfile)
1085 int *isfile;
1086EOF
1087
1088if test -n "$COMPILE_IN"
1089then
1090# Scripts compiled in.
1091
1092# sed commands to quote an ld script as a C string.
1093sc='s/["\\]/\\&/g
1094s/$/\\n\\/
10951s/^/"/
1096$s/$/n"/
1097'
1098
1099cat >>e${EMULATION_NAME}.c <<EOF
1100{
1101 *isfile = 0;
1102
1103 if (link_info.relocateable == true && config.build_constructors == true)
1104 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
1105 else if (link_info.relocateable == true)
1106 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
1107 else if (!config.text_read_only)
1108 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
1109 else if (!config.magic_demand_paged)
1110 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
1111 else if (link_info.shared)
1112 return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
1113 else
1114 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
1115}
1116EOF
1117
1118else
1119# Scripts read from the filesystem.
1120
1121cat >>e${EMULATION_NAME}.c <<EOF
1122{
1123 *isfile = 1;
1124
1125 if (link_info.relocateable == true && config.build_constructors == true)
1126 return "ldscripts/${EMULATION_NAME}.xu";
1127 else if (link_info.relocateable == true)
1128 return "ldscripts/${EMULATION_NAME}.xr";
1129 else if (!config.text_read_only)
1130 return "ldscripts/${EMULATION_NAME}.xbn";
1131 else if (!config.magic_demand_paged)
1132 return "ldscripts/${EMULATION_NAME}.xn";
1133 else if (link_info.shared)
1134 return "ldscripts/${EMULATION_NAME}.xs";
1135 else
1136 return "ldscripts/${EMULATION_NAME}.x";
1137}
1138EOF
1139
1140fi
1141
1142cat >>e${EMULATION_NAME}.c <<EOF
1143
1144struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1145{
1146 gld${EMULATION_NAME}_before_parse,
1147 syslib_default,
1148 hll_default,
1149 after_parse_default,
1150 gld${EMULATION_NAME}_after_open,
1151 after_allocation_default,
1152 set_output_arch_default,
1153 ldemul_default_target,
1154 gld${EMULATION_NAME}_before_allocation,
1155 gld${EMULATION_NAME}_get_script,
1156 "${EMULATION_NAME}",
1157 "${OUTPUT_FORMAT}",
1158 NULL,
1159 NULL,
1160 gld${EMULATION_NAME}_open_dynamic_archive,
1161 gld${EMULATION_NAME}_place_orphan
1162};
1163EOF
413 fi
414fi
415cat >>e${EMULATION_NAME}.c <<EOF
416 }
417
418 if (force < 2)
419 continue;
420
421 einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
422 l->name, l->by);
423 }
424}
425
426/* Search for a needed file in a path. */
427
428static boolean
429gld${EMULATION_NAME}_search_needed (path, name, force)
430 const char *path;
431 const char *name;
432 int force;
433{
434 const char *s;
435 size_t len;
436
437 if (path == NULL || *path == '\0')
438 return false;
439 len = strlen (name);
440 while (1)
441 {
442 char *filename, *sset;
443
444 s = strchr (path, ':');
445 if (s == NULL)
446 s = path + strlen (path);
447
448 filename = (char *) xmalloc (s - path + len + 2);
449 if (s == path)
450 sset = filename;
451 else
452 {
453 memcpy (filename, path, s - path);
454 filename[s - path] = '/';
455 sset = filename + (s - path) + 1;
456 }
457 strcpy (sset, name);
458
459 if (gld${EMULATION_NAME}_try_needed (filename, force))
460 return true;
461
462 free (filename);
463
464 if (*s == '\0')
465 break;
466 path = s + 1;
467 }
468
469 return false;
470}
471
472/* This function is called for each possible name for a dynamic object
473 named by a DT_NEEDED entry. The FORCE parameter indicates whether
474 to skip the check for a conflicting version. */
475
476static boolean
477gld${EMULATION_NAME}_try_needed (name, force)
478 const char *name;
479 int force;
480{
481 bfd *abfd;
482
483 abfd = bfd_openr (name, bfd_get_target (output_bfd));
484 if (abfd == NULL)
485 return false;
486 if (! bfd_check_format (abfd, bfd_object))
487 {
488 (void) bfd_close (abfd);
489 return false;
490 }
491 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
492 {
493 (void) bfd_close (abfd);
494 return false;
495 }
496
497 /* Check whether this object would include any conflicting library
498 versions. If FORCE is set, then we skip this check; we use this
499 the second time around, if we couldn't find any compatible
500 instance of the shared library. */
501
502 if (! force)
503 {
504 struct bfd_link_needed_list *needed;
505
506 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
507 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
508
509 if (needed != NULL)
510 {
511 global_vercheck_needed = needed;
512 global_vercheck_failed = false;
513 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
514 if (global_vercheck_failed)
515 {
516 (void) bfd_close (abfd);
517 /* Return false to force the caller to move on to try
518 another file on the search path. */
519 return false;
520 }
521
522 /* But wait! It gets much worse. On Linux, if a shared
523 library does not use libc at all, we are supposed to skip
524 it the first time around in case we encounter a shared
525 library later on with the same name which does use the
526 version of libc that we want. This is much too horrible
527 to use on any system other than Linux. */
528
529EOF
530case ${target} in
531 *-*-linux-gnu*)
532 cat >>e${EMULATION_NAME}.c <<EOF
533 {
534 struct bfd_link_needed_list *l;
535
536 for (l = needed; l != NULL; l = l->next)
537 if (strncmp (l->name, "libc.so", 7) == 0)
538 break;
539 if (l == NULL)
540 {
541 (void) bfd_close (abfd);
542 return false;
543 }
544 }
545
546EOF
547 ;;
548esac
549cat >>e${EMULATION_NAME}.c <<EOF
550 }
551 }
552
553 /* We've found a dynamic object matching the DT_NEEDED entry. */
554
555 /* We have already checked that there is no other input file of the
556 same name. We must now check again that we are not including the
557 same file twice. We need to do this because on many systems
558 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
559 reference libc.so.1. If we have already included libc.so, we
560 don't want to include libc.so.1 if they are the same file, and we
561 can only check that using stat. */
562
563 if (bfd_stat (abfd, &global_stat) != 0)
564 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
565 global_found = false;
566 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
567 if (global_found)
568 {
569 /* Return true to indicate that we found the file, even though
570 we aren't going to do anything with it. */
571 return true;
572 }
573
574 /* Tell the ELF backend that don't want the output file to have a
575 DT_NEEDED entry for this file. */
576 bfd_elf_set_dt_needed_name (abfd, "");
577
578 /* Add this file into the symbol table. */
579 if (! bfd_link_add_symbols (abfd, &link_info))
580 einfo ("%F%B: could not read symbols: %E\n", abfd);
581
582 return true;
583}
584
585/* See if an input file matches a DT_NEEDED entry by name. */
586
587static void
588gld${EMULATION_NAME}_check_needed (s)
589 lang_input_statement_type *s;
590{
591 if (global_found)
592 return;
593
594 if (s->filename != NULL
595 && strcmp (s->filename, global_needed->name) == 0)
596 {
597 global_found = true;
598 return;
599 }
600
601 if (s->the_bfd != NULL)
602 {
603 const char *soname;
604
605 soname = bfd_elf_get_dt_soname (s->the_bfd);
606 if (soname != NULL
607 && strcmp (soname, global_needed->name) == 0)
608 {
609 global_found = true;
610 return;
611 }
612 }
613
614 if (s->search_dirs_flag
615 && s->filename != NULL
616 && strchr (global_needed->name, '/') == NULL)
617 {
618 const char *f;
619
620 f = strrchr (s->filename, '/');
621 if (f != NULL
622 && strcmp (f + 1, global_needed->name) == 0)
623 {
624 global_found = true;
625 return;
626 }
627 }
628}
629
630/* See if an input file matches a DT_NEEDED entry by running stat on
631 the file. */
632
633static void
634gld${EMULATION_NAME}_stat_needed (s)
635 lang_input_statement_type *s;
636{
637 struct stat st;
638 const char *suffix;
639 const char *soname;
640 const char *f;
641
642 if (global_found)
643 return;
644 if (s->the_bfd == NULL)
645 return;
646
647 if (bfd_stat (s->the_bfd, &st) != 0)
648 {
649 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
650 return;
651 }
652
653 if (st.st_dev == global_stat.st_dev
654 && st.st_ino == global_stat.st_ino)
655 {
656 global_found = true;
657 return;
658 }
659
660 /* We issue a warning if it looks like we are including two
661 different versions of the same shared library. For example,
662 there may be a problem if -lc picks up libc.so.6 but some other
663 shared library has a DT_NEEDED entry of libc.so.5. This is a
664 hueristic test, and it will only work if the name looks like
665 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
666 If we really want to issue warnings about mixing version numbers
667 of shared libraries, we need to find a better way. */
668
669 if (strchr (global_needed->name, '/') != NULL)
670 return;
671 suffix = strstr (global_needed->name, ".so.");
672 if (suffix == NULL)
673 return;
674 suffix += sizeof ".so." - 1;
675
676 soname = bfd_elf_get_dt_soname (s->the_bfd);
677 if (soname == NULL)
678 soname = s->filename;
679
680 f = strrchr (soname, '/');
681 if (f != NULL)
682 ++f;
683 else
684 f = soname;
685
686 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
687 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
688 global_needed->name, global_needed->by, f);
689}
690
691/* On Linux, it's possible to have different versions of the same
692 shared library linked against different versions of libc. The
693 dynamic linker somehow tags which libc version to use in
694 /etc/ld.so.cache, and, based on the libc that it sees in the
695 executable, chooses which version of the shared library to use.
696
697 We try to do a similar check here by checking whether this shared
698 library needs any other shared libraries which may conflict with
699 libraries we have already included in the link. If it does, we
700 skip it, and try to find another shared library farther on down the
701 link path.
702
703 This is called via lang_for_each_input_file.
704 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
705 which we ar checking. This sets GLOBAL_VERCHECK_FAILED if we find
706 a conflicting version. */
707
708static void
709gld${EMULATION_NAME}_vercheck (s)
710 lang_input_statement_type *s;
711{
712 const char *soname, *f;
713 struct bfd_link_needed_list *l;
714
715 if (global_vercheck_failed)
716 return;
717 if (s->the_bfd == NULL
718 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
719 return;
720
721 soname = bfd_elf_get_dt_soname (s->the_bfd);
722 if (soname == NULL)
723 soname = bfd_get_filename (s->the_bfd);
724
725 f = strrchr (soname, '/');
726 if (f != NULL)
727 ++f;
728 else
729 f = soname;
730
731 for (l = global_vercheck_needed; l != NULL; l = l->next)
732 {
733 const char *suffix;
734
735 if (strcmp (f, l->name) == 0)
736 {
737 /* Probably can't happen, but it's an easy check. */
738 continue;
739 }
740
741 if (strchr (l->name, '/') != NULL)
742 continue;
743
744 suffix = strstr (l->name, ".so.");
745 if (suffix == NULL)
746 continue;
747
748 suffix += sizeof ".so." - 1;
749
750 if (strncmp (f, l->name, suffix - l->name) == 0)
751 {
752 /* Here we know that S is a dynamic object FOO.SO.VER1, and
753 the object we are considering needs a dynamic object
754 FOO.SO.VER2, and VER1 and VER2 are different. This
755 appears to be a version mismatch, so we tell the caller
756 to try a different version of this library. */
757 global_vercheck_failed = true;
758 return;
759 }
760 }
761}
762
763/* This is called after the sections have been attached to output
764 sections, but before any sizes or addresses have been set. */
765
766static void
767gld${EMULATION_NAME}_before_allocation ()
768{
769 const char *rpath;
770 asection *sinterp;
771
772 /* If we are going to make any variable assignments, we need to let
773 the ELF backend know about them in case the variables are
774 referred to by dynamic objects. */
775 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
776
777 /* Let the ELF backend work out the sizes of any sections required
778 by dynamic linking. */
779 rpath = command_line.rpath;
780 if (rpath == NULL)
781 rpath = (const char *) getenv ("LD_RUN_PATH");
782 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
783 (output_bfd, command_line.soname, rpath,
784 command_line.export_dynamic, command_line.filter_shlib,
785 (const char * const *) command_line.auxiliary_filters,
786 &link_info, &sinterp, lang_elf_version_info)))
787 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
788
789 /* Let the user override the dynamic linker we are using. */
790 if (command_line.interpreter != NULL
791 && sinterp != NULL)
792 {
793 sinterp->contents = (bfd_byte *) command_line.interpreter;
794 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
795 }
796
797 /* Look for any sections named .gnu.warning. As a GNU extensions,
798 we treat such sections as containing warning messages. We print
799 out the warning message, and then zero out the section size so
800 that it does not get copied into the output file. */
801
802 {
803 LANG_FOR_EACH_INPUT_STATEMENT (is)
804 {
805 asection *s;
806 bfd_size_type sz;
807 char *msg;
808 boolean ret;
809
810 if (is->just_syms_flag)
811 continue;
812
813 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
814 if (s == NULL)
815 continue;
816
817 sz = bfd_section_size (is->the_bfd, s);
818 msg = xmalloc ((size_t) sz + 1);
819 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
820 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
821 is->the_bfd);
822 msg[sz] = '\0';
823 ret = link_info.callbacks->warning (&link_info, msg,
824 (const char *) NULL,
825 is->the_bfd, (asection *) NULL,
826 (bfd_vma) 0);
827 ASSERT (ret);
828 free (msg);
829
830 /* Clobber the section size, so that we don't waste copying the
831 warning into the output file. */
832 s->_raw_size = 0;
833 }
834 }
835
836#if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
837 /* For MIPS ELF the .reginfo section requires special handling.
838 Each input section is 24 bytes, and the final output section must
839 also be 24 bytes. We handle this by clobbering all but the first
840 input section size to 0. The .reginfo section is handled
841 specially by the backend code anyhow. */
842 {
843 boolean found = false;
844 LANG_FOR_EACH_INPUT_STATEMENT (is)
845 {
846 asection *s;
847
848 if (is->just_syms_flag)
849 continue;
850
851 s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
852 if (s == NULL)
853 continue;
854
855 if (! found)
856 {
857 found = true;
858 continue;
859 }
860
861 s->_raw_size = 0;
862 s->_cooked_size = 0;
863 }
864 }
865#endif
866}
867
868/* This is called by the before_allocation routine via
869 lang_for_each_statement. It locates any assignment statements, and
870 tells the ELF backend about them, in case they are assignments to
871 symbols which are referred to by dynamic objects. */
872
873static void
874gld${EMULATION_NAME}_find_statement_assignment (s)
875 lang_statement_union_type *s;
876{
877 if (s->header.type == lang_assignment_statement_enum)
878 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
879}
880
881/* Look through an expression for an assignment statement. */
882
883static void
884gld${EMULATION_NAME}_find_exp_assignment (exp)
885 etree_type *exp;
886{
887 struct bfd_link_hash_entry *h;
888
889 switch (exp->type.node_class)
890 {
891 case etree_provide:
892 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
893 false, false, false);
894 if (h == NULL)
895 break;
896
897 /* We call record_link_assignment even if the symbol is defined.
898 This is because if it is defined by a dynamic object, we
899 actually want to use the value defined by the linker script,
900 not the value from the dynamic object (because we are setting
901 symbols like etext). If the symbol is defined by a regular
902 object, then, as it happens, calling record_link_assignment
903 will do no harm. */
904
905 /* Fall through. */
906 case etree_assign:
907 if (strcmp (exp->assign.dst, ".") != 0)
908 {
909 if (! (bfd_elf${ELFSIZE}_record_link_assignment
910 (output_bfd, &link_info, exp->assign.dst,
911 exp->type.node_class == etree_provide ? true : false)))
912 einfo ("%P%F: failed to record assignment to %s: %E\n",
913 exp->assign.dst);
914 }
915 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
916 break;
917
918 case etree_binary:
919 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
920 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
921 break;
922
923 case etree_trinary:
924 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
925 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
926 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
927 break;
928
929 case etree_unary:
930 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
931 break;
932
933 default:
934 break;
935 }
936}
937
938/* Place an orphan section. We use this to put random SHF_ALLOC
939 sections in the right segment. */
940
941static asection *hold_section;
942static lang_output_section_statement_type *hold_use;
943static lang_output_section_statement_type *hold_text;
944static lang_output_section_statement_type *hold_rodata;
945static lang_output_section_statement_type *hold_data;
946static lang_output_section_statement_type *hold_bss;
947static lang_output_section_statement_type *hold_rel;
948static lang_output_section_statement_type *hold_interp;
949
950/*ARGSUSED*/
951static boolean
952gld${EMULATION_NAME}_place_orphan (file, s)
953 lang_input_statement_type *file;
954 asection *s;
955{
956 lang_output_section_statement_type *place;
957 asection *snew, **pps;
958 lang_statement_list_type *old;
959 lang_statement_list_type add;
960 etree_type *address;
961 const char *secname, *ps;
962 const char *outsecname;
963 lang_output_section_statement_type *os;
964
965 if ((s->flags & SEC_ALLOC) == 0)
966 return false;
967
968 /* Look through the script to see where to place this section. */
969 hold_section = s;
970 hold_use = NULL;
971 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
972
973 if (hold_use != NULL)
974 {
975 /* We have already placed a section with this name. */
976 wild_doit (&hold_use->children, s, hold_use, file);
977 return true;
978 }
979
980 secname = bfd_get_section_name (s->owner, s);
981
982 /* If this is a final link, then always put .gnu.warning.SYMBOL
983 sections into the .text section to get them out of the way. */
984 if (! link_info.shared
985 && ! link_info.relocateable
986 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
987 && hold_text != NULL)
988 {
989 wild_doit (&hold_text->children, s, hold_text, file);
990 return true;
991 }
992
993 /* Decide which segment the section should go in based on the
994 section name and section flags. We put loadable .note sections
995 right after the .interp section, so that the PT_NOTE segment is
996 stored right after the program headers where the OS can read it
997 in the first page. */
998 place = NULL;
999 if ((s->flags & SEC_LOAD) != 0
1000 && strncmp (secname, ".note", 4) == 0
1001 && hold_interp != NULL)
1002 place = hold_interp;
1003 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1004 && hold_bss != NULL)
1005 place = hold_bss;
1006 else if ((s->flags & SEC_READONLY) == 0
1007 && hold_data != NULL)
1008 place = hold_data;
1009 else if (strncmp (secname, ".rel", 4) == 0
1010 && hold_rel != NULL)
1011 place = hold_rel;
1012 else if ((s->flags & SEC_CODE) == 0
1013 && (s->flags & SEC_READONLY) != 0
1014 && hold_rodata != NULL)
1015 place = hold_rodata;
1016 else if ((s->flags & SEC_READONLY) != 0
1017 && hold_text != NULL)
1018 place = hold_text;
1019 if (place == NULL)
1020 return false;
1021
1022 /* Choose a unique name for the section. This will be needed if the
1023 same section name appears in the input file with different
1024 loadable or allocateable characteristics. */
1025 outsecname = secname;
1026 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1027 {
1028 unsigned int len;
1029 char *newname;
1030 unsigned int i;
1031
1032 len = strlen (outsecname);
1033 newname = xmalloc (len + 5);
1034 strcpy (newname, outsecname);
1035 i = 0;
1036 do
1037 {
1038 sprintf (newname + len, "%d", i);
1039 ++i;
1040 }
1041 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1042
1043 outsecname = newname;
1044 }
1045
1046 /* Create the section in the output file, and put it in the right
1047 place. This shuffling is to make the output file look neater. */
1048 snew = bfd_make_section (output_bfd, outsecname);
1049 if (snew == NULL)
1050 einfo ("%P%F: output format %s cannot represent section called %s\n",
1051 output_bfd->xvec->name, outsecname);
1052 if (place->bfd_section != NULL)
1053 {
1054 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1055 ;
1056 *pps = snew->next;
1057 snew->next = place->bfd_section->next;
1058 place->bfd_section->next = snew;
1059 }
1060
1061 /* Start building a list of statements for this section. */
1062 old = stat_ptr;
1063 stat_ptr = &add;
1064 lang_list_init (stat_ptr);
1065
1066 /* If the name of the section is representable in C, then create
1067 symbols to mark the start and the end of the section. */
1068 for (ps = outsecname; *ps != '\0'; ps++)
1069 if (! isalnum ((unsigned char) *ps) && *ps != '_')
1070 break;
1071 if (*ps == '\0' && config.build_constructors)
1072 {
1073 char *symname;
1074
1075 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1076 sprintf (symname, "__start_%s", outsecname);
1077 lang_add_assignment (exp_assop ('=', symname,
1078 exp_unop (ALIGN_K,
1079 exp_intop ((bfd_vma) 1
1080 << s->alignment_power))));
1081 }
1082
1083 if (! link_info.relocateable)
1084 address = NULL;
1085 else
1086 address = exp_intop ((bfd_vma) 0);
1087
1088 lang_enter_output_section_statement (outsecname, address, 0,
1089 (bfd_vma) 0,
1090 (etree_type *) NULL,
1091 (etree_type *) NULL,
1092 (etree_type *) NULL);
1093
1094 os = lang_output_section_statement_lookup (outsecname);
1095 wild_doit (&os->children, s, os, file);
1096
1097 lang_leave_output_section_statement
1098 ((bfd_vma) 0, "*default*", (struct lang_output_section_phdr_list *) NULL);
1099 stat_ptr = &add;
1100
1101 if (*ps == '\0' && config.build_constructors)
1102 {
1103 char *symname;
1104
1105 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1106 sprintf (symname, "__stop_%s", outsecname);
1107 lang_add_assignment (exp_assop ('=', symname,
1108 exp_nameop (NAME, ".")));
1109 }
1110
1111 /* Now stick the new statement list right after PLACE. */
1112 *add.tail = place->header.next;
1113 place->header.next = add.head;
1114
1115 stat_ptr = old;
1116
1117 return true;
1118}
1119
1120static void
1121gld${EMULATION_NAME}_place_section (s)
1122 lang_statement_union_type *s;
1123{
1124 lang_output_section_statement_type *os;
1125
1126 if (s->header.type != lang_output_section_statement_enum)
1127 return;
1128
1129 os = &s->output_section_statement;
1130
1131 if (strcmp (os->name, hold_section->name) == 0
1132 && os->bfd_section != NULL
1133 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1134 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1135 hold_use = os;
1136
1137 if (strcmp (os->name, ".text") == 0)
1138 hold_text = os;
1139 else if (strcmp (os->name, ".rodata") == 0)
1140 hold_rodata = os;
1141 else if (strcmp (os->name, ".data") == 0)
1142 hold_data = os;
1143 else if (strcmp (os->name, ".bss") == 0)
1144 hold_bss = os;
1145 else if (hold_rel == NULL
1146 && os->bfd_section != NULL
1147 && (os->bfd_section->flags & SEC_ALLOC) != 0
1148 && strncmp (os->name, ".rel", 4) == 0)
1149 hold_rel = os;
1150 else if (strcmp (os->name, ".interp") == 0)
1151 hold_interp = os;
1152}
1153
1154static char *
1155gld${EMULATION_NAME}_get_script(isfile)
1156 int *isfile;
1157EOF
1158
1159if test -n "$COMPILE_IN"
1160then
1161# Scripts compiled in.
1162
1163# sed commands to quote an ld script as a C string.
1164sc='s/["\\]/\\&/g
1165s/$/\\n\\/
11661s/^/"/
1167$s/$/n"/
1168'
1169
1170cat >>e${EMULATION_NAME}.c <<EOF
1171{
1172 *isfile = 0;
1173
1174 if (link_info.relocateable == true && config.build_constructors == true)
1175 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
1176 else if (link_info.relocateable == true)
1177 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
1178 else if (!config.text_read_only)
1179 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
1180 else if (!config.magic_demand_paged)
1181 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
1182 else if (link_info.shared)
1183 return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
1184 else
1185 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
1186}
1187EOF
1188
1189else
1190# Scripts read from the filesystem.
1191
1192cat >>e${EMULATION_NAME}.c <<EOF
1193{
1194 *isfile = 1;
1195
1196 if (link_info.relocateable == true && config.build_constructors == true)
1197 return "ldscripts/${EMULATION_NAME}.xu";
1198 else if (link_info.relocateable == true)
1199 return "ldscripts/${EMULATION_NAME}.xr";
1200 else if (!config.text_read_only)
1201 return "ldscripts/${EMULATION_NAME}.xbn";
1202 else if (!config.magic_demand_paged)
1203 return "ldscripts/${EMULATION_NAME}.xn";
1204 else if (link_info.shared)
1205 return "ldscripts/${EMULATION_NAME}.xs";
1206 else
1207 return "ldscripts/${EMULATION_NAME}.x";
1208}
1209EOF
1210
1211fi
1212
1213cat >>e${EMULATION_NAME}.c <<EOF
1214
1215struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1216{
1217 gld${EMULATION_NAME}_before_parse,
1218 syslib_default,
1219 hll_default,
1220 after_parse_default,
1221 gld${EMULATION_NAME}_after_open,
1222 after_allocation_default,
1223 set_output_arch_default,
1224 ldemul_default_target,
1225 gld${EMULATION_NAME}_before_allocation,
1226 gld${EMULATION_NAME}_get_script,
1227 "${EMULATION_NAME}",
1228 "${OUTPUT_FORMAT}",
1229 NULL,
1230 NULL,
1231 gld${EMULATION_NAME}_open_dynamic_archive,
1232 gld${EMULATION_NAME}_place_orphan
1233};
1234EOF