Commit | Line | Data |
---|---|---|
252b5132 RH |
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. | |
5 | test -z "${ELFSIZE}" && ELFSIZE=32 | |
86af25fe L |
6 | if [ -z "$MACHINE" ]; then |
7 | OUTPUT_ARCH=${ARCH} | |
8 | else | |
9 | OUTPUT_ARCH=${ARCH}:${MACHINE} | |
10 | fi | |
252b5132 RH |
11 | cat >e${EMULATION_NAME}.c <<EOF |
12 | /* This file is is generated by a shell script. DO NOT EDIT! */ | |
13 | ||
14 | /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME} | |
e3dc8847 | 15 | Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
7e9f0867 | 16 | 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
252b5132 RH |
17 | Written by Steve Chamberlain <sac@cygnus.com> |
18 | ELF support by Ian Lance Taylor <ian@cygnus.com> | |
19 | ||
20 | This file is part of GLD, the Gnu Linker. | |
21 | ||
22 | This program is free software; you can redistribute it and/or modify | |
23 | it under the terms of the GNU General Public License as published by | |
24 | the Free Software Foundation; either version 2 of the License, or | |
25 | (at your option) any later version. | |
26 | ||
27 | This program is distributed in the hope that it will be useful, | |
28 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | GNU General Public License for more details. | |
31 | ||
32 | You should have received a copy of the GNU General Public License | |
33 | along with this program; if not, write to the Free Software | |
34 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
35 | ||
36 | #define TARGET_IS_${EMULATION_NAME} | |
37 | ||
38 | #include "bfd.h" | |
39 | #include "sysdep.h" | |
1c9acd94 | 40 | #include "libiberty.h" |
3882b010 | 41 | #include "safe-ctype.h" |
fcf65871 | 42 | #include "getopt.h" |
252b5132 RH |
43 | |
44 | #include "bfdlink.h" | |
45 | ||
46 | #include "ld.h" | |
47 | #include "ldmain.h" | |
252b5132 RH |
48 | #include "ldmisc.h" |
49 | #include "ldexp.h" | |
50 | #include "ldlang.h" | |
b71e2778 AM |
51 | #include "ldfile.h" |
52 | #include "ldemul.h" | |
df2a7313 | 53 | #include <ldgram.h> |
e0ee487b | 54 | #include "elf/common.h" |
252b5132 | 55 | |
0c7a8e5a AM |
56 | /* Declare functions used by various EXTRA_EM_FILEs. */ |
57 | static void gld${EMULATION_NAME}_before_parse (void); | |
58 | static void gld${EMULATION_NAME}_after_open (void); | |
59 | static void gld${EMULATION_NAME}_before_allocation (void); | |
b34976b6 | 60 | static bfd_boolean gld${EMULATION_NAME}_place_orphan |
0c7a8e5a AM |
61 | (lang_input_statement_type *file, asection *s); |
62 | static void gld${EMULATION_NAME}_finish (void); | |
252b5132 | 63 | |
41392f03 AM |
64 | EOF |
65 | ||
d3989512 JJ |
66 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
67 | case ${target} in | |
68 | *-*-linux-gnu*) | |
69 | cat >>e${EMULATION_NAME}.c <<EOF | |
70 | #include <glob.h> | |
71 | EOF | |
72 | ;; | |
73 | esac | |
74 | fi | |
75 | ||
41392f03 AM |
76 | # Import any needed special functions and/or overrides. |
77 | # | |
78 | if test -n "$EXTRA_EM_FILE" ; then | |
79 | . ${srcdir}/emultempl/${EXTRA_EM_FILE}.em | |
80 | fi | |
81 | ||
f813923c | 82 | # Functions in this file can be overridden by setting the LDEMUL_* shell |
41392f03 AM |
83 | # variables. If the name of the overriding function is the same as is |
84 | # defined in this file, then don't output this file's version. | |
85 | # If a different overriding name is given then output the standard function | |
86 | # as presumably it is called from the overriding function. | |
87 | # | |
88 | if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then | |
89 | cat >>e${EMULATION_NAME}.c <<EOF | |
90 | ||
252b5132 | 91 | static void |
0c7a8e5a | 92 | gld${EMULATION_NAME}_before_parse (void) |
252b5132 | 93 | { |
5e2f1575 | 94 | ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`); |
b34976b6 AM |
95 | config.dynamic_link = ${DYNAMIC_LINK-TRUE}; |
96 | config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`; | |
252b5132 RH |
97 | } |
98 | ||
41392f03 AM |
99 | EOF |
100 | fi | |
101 | ||
4a43e768 AM |
102 | if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then |
103 | cat >>e${EMULATION_NAME}.c <<EOF | |
104 | /* Handle as_needed DT_NEEDED. */ | |
105 | ||
106 | static bfd_boolean | |
107 | gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry) | |
108 | { | |
e56f61be | 109 | int class = 0; |
4a43e768 AM |
110 | |
111 | /* Tell the ELF linker that we don't want the output file to have a | |
112 | DT_NEEDED entry for this file, unless it is used to resolve | |
113 | references in a regular object. */ | |
e56f61be L |
114 | if (entry->as_needed) |
115 | class = DYN_AS_NEEDED; | |
116 | ||
117 | /* Tell the ELF linker that we don't want the output file to have a | |
118 | DT_NEEDED entry for any dynamic library in DT_NEEDED tags from | |
119 | this file at all. */ | |
120 | if (!entry->add_needed) | |
121 | class |= DYN_NO_ADD_NEEDED; | |
122 | ||
123 | if (!class | |
124 | || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0) | |
125 | return FALSE; | |
126 | ||
127 | bfd_elf_set_dyn_lib_class (entry->the_bfd, class); | |
4a43e768 AM |
128 | |
129 | /* Continue on with normal load_symbols processing. */ | |
130 | return FALSE; | |
131 | } | |
132 | EOF | |
133 | fi | |
134 | ||
41392f03 | 135 | cat >>e${EMULATION_NAME}.c <<EOF |
252b5132 RH |
136 | |
137 | /* These variables are required to pass information back and forth | |
138 | between after_open and check_needed and stat_needed and vercheck. */ | |
139 | ||
140 | static struct bfd_link_needed_list *global_needed; | |
141 | static struct stat global_stat; | |
b34976b6 | 142 | static bfd_boolean global_found; |
252b5132 | 143 | static struct bfd_link_needed_list *global_vercheck_needed; |
b34976b6 | 144 | static bfd_boolean global_vercheck_failed; |
252b5132 | 145 | |
252b5132 | 146 | |
04925e1e AM |
147 | /* On Linux, it's possible to have different versions of the same |
148 | shared library linked against different versions of libc. The | |
149 | dynamic linker somehow tags which libc version to use in | |
150 | /etc/ld.so.cache, and, based on the libc that it sees in the | |
151 | executable, chooses which version of the shared library to use. | |
252b5132 | 152 | |
04925e1e AM |
153 | We try to do a similar check here by checking whether this shared |
154 | library needs any other shared libraries which may conflict with | |
155 | libraries we have already included in the link. If it does, we | |
156 | skip it, and try to find another shared library farther on down the | |
157 | link path. | |
252b5132 | 158 | |
04925e1e AM |
159 | This is called via lang_for_each_input_file. |
160 | GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object | |
f813923c | 161 | which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find |
04925e1e | 162 | a conflicting version. */ |
252b5132 | 163 | |
04925e1e | 164 | static void |
0c7a8e5a | 165 | gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s) |
04925e1e | 166 | { |
1c9acd94 | 167 | const char *soname; |
04925e1e | 168 | struct bfd_link_needed_list *l; |
252b5132 | 169 | |
04925e1e AM |
170 | if (global_vercheck_failed) |
171 | return; | |
172 | if (s->the_bfd == NULL | |
173 | || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0) | |
174 | return; | |
252b5132 | 175 | |
04925e1e AM |
176 | soname = bfd_elf_get_dt_soname (s->the_bfd); |
177 | if (soname == NULL) | |
fed2999d | 178 | soname = lbasename (bfd_get_filename (s->the_bfd)); |
04925e1e AM |
179 | |
180 | for (l = global_vercheck_needed; l != NULL; l = l->next) | |
181 | { | |
182 | const char *suffix; | |
183 | ||
1c9acd94 | 184 | if (strcmp (soname, l->name) == 0) |
04925e1e AM |
185 | { |
186 | /* Probably can't happen, but it's an easy check. */ | |
187 | continue; | |
252b5132 RH |
188 | } |
189 | ||
04925e1e | 190 | if (strchr (l->name, '/') != NULL) |
252b5132 RH |
191 | continue; |
192 | ||
04925e1e AM |
193 | suffix = strstr (l->name, ".so."); |
194 | if (suffix == NULL) | |
195 | continue; | |
196 | ||
197 | suffix += sizeof ".so." - 1; | |
198 | ||
1c9acd94 | 199 | if (strncmp (soname, l->name, suffix - l->name) == 0) |
04925e1e AM |
200 | { |
201 | /* Here we know that S is a dynamic object FOO.SO.VER1, and | |
0c7a8e5a AM |
202 | the object we are considering needs a dynamic object |
203 | FOO.SO.VER2, and VER1 and VER2 are different. This | |
204 | appears to be a version mismatch, so we tell the caller | |
205 | to try a different version of this library. */ | |
b34976b6 | 206 | global_vercheck_failed = TRUE; |
04925e1e AM |
207 | return; |
208 | } | |
252b5132 RH |
209 | } |
210 | } | |
211 | ||
252b5132 | 212 | |
04925e1e AM |
213 | /* See if an input file matches a DT_NEEDED entry by running stat on |
214 | the file. */ | |
215 | ||
216 | static void | |
0c7a8e5a | 217 | gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s) |
252b5132 | 218 | { |
04925e1e AM |
219 | struct stat st; |
220 | const char *suffix; | |
221 | const char *soname; | |
252b5132 | 222 | |
04925e1e AM |
223 | if (global_found) |
224 | return; | |
225 | if (s->the_bfd == NULL) | |
226 | return; | |
227 | ||
228 | if (bfd_stat (s->the_bfd, &st) != 0) | |
252b5132 | 229 | { |
04925e1e AM |
230 | einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd); |
231 | return; | |
232 | } | |
252b5132 | 233 | |
9eabd475 ZW |
234 | /* Some operating systems, e.g. Windows, do not provide a meaningful |
235 | st_ino; they always set it to zero. (Windows does provide a | |
236 | meaningful st_dev.) Do not indicate a duplicate library in that | |
237 | case. While there is no guarantee that a system that provides | |
238 | meaningful inode numbers will never set st_ino to zero, this is | |
239 | merely an optimization, so we do not need to worry about false | |
240 | negatives. */ | |
04925e1e | 241 | if (st.st_dev == global_stat.st_dev |
9eabd475 ZW |
242 | && st.st_ino == global_stat.st_ino |
243 | && st.st_ino != 0) | |
04925e1e | 244 | { |
b34976b6 | 245 | global_found = TRUE; |
04925e1e AM |
246 | return; |
247 | } | |
252b5132 | 248 | |
04925e1e AM |
249 | /* We issue a warning if it looks like we are including two |
250 | different versions of the same shared library. For example, | |
251 | there may be a problem if -lc picks up libc.so.6 but some other | |
252 | shared library has a DT_NEEDED entry of libc.so.5. This is a | |
f813923c | 253 | heuristic test, and it will only work if the name looks like |
04925e1e AM |
254 | NAME.so.VERSION. FIXME: Depending on file names is error-prone. |
255 | If we really want to issue warnings about mixing version numbers | |
256 | of shared libraries, we need to find a better way. */ | |
252b5132 | 257 | |
04925e1e AM |
258 | if (strchr (global_needed->name, '/') != NULL) |
259 | return; | |
260 | suffix = strstr (global_needed->name, ".so."); | |
261 | if (suffix == NULL) | |
262 | return; | |
263 | suffix += sizeof ".so." - 1; | |
252b5132 | 264 | |
04925e1e AM |
265 | soname = bfd_elf_get_dt_soname (s->the_bfd); |
266 | if (soname == NULL) | |
fed2999d | 267 | soname = lbasename (s->filename); |
252b5132 | 268 | |
cde43e70 | 269 | if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0) |
04925e1e | 270 | einfo ("%P: warning: %s, needed by %B, may conflict with %s\n", |
1c9acd94 | 271 | global_needed->name, global_needed->by, soname); |
252b5132 RH |
272 | } |
273 | ||
e56f61be L |
274 | struct dt_needed |
275 | { | |
276 | bfd *by; | |
277 | const char *name; | |
278 | }; | |
04925e1e | 279 | |
252b5132 RH |
280 | /* This function is called for each possible name for a dynamic object |
281 | named by a DT_NEEDED entry. The FORCE parameter indicates whether | |
282 | to skip the check for a conflicting version. */ | |
283 | ||
b34976b6 | 284 | static bfd_boolean |
e56f61be L |
285 | gld${EMULATION_NAME}_try_needed (struct dt_needed *needed, |
286 | int force) | |
252b5132 RH |
287 | { |
288 | bfd *abfd; | |
e56f61be | 289 | const char *name = needed->name; |
ec4eb78a | 290 | const char *soname; |
e56f61be | 291 | int class; |
252b5132 RH |
292 | |
293 | abfd = bfd_openr (name, bfd_get_target (output_bfd)); | |
294 | if (abfd == NULL) | |
b34976b6 | 295 | return FALSE; |
252b5132 RH |
296 | if (! bfd_check_format (abfd, bfd_object)) |
297 | { | |
cde43e70 | 298 | bfd_close (abfd); |
b34976b6 | 299 | return FALSE; |
252b5132 RH |
300 | } |
301 | if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) | |
302 | { | |
cde43e70 | 303 | bfd_close (abfd); |
b34976b6 | 304 | return FALSE; |
252b5132 RH |
305 | } |
306 | ||
c4bb8067 L |
307 | /* For DT_NEEDED, they have to match. */ |
308 | if (abfd->xvec != output_bfd->xvec) | |
309 | { | |
310 | bfd_close (abfd); | |
b34976b6 | 311 | return FALSE; |
c4bb8067 L |
312 | } |
313 | ||
252b5132 RH |
314 | /* Check whether this object would include any conflicting library |
315 | versions. If FORCE is set, then we skip this check; we use this | |
316 | the second time around, if we couldn't find any compatible | |
317 | instance of the shared library. */ | |
318 | ||
319 | if (! force) | |
320 | { | |
321 | struct bfd_link_needed_list *needed; | |
322 | ||
323 | if (! bfd_elf_get_bfd_needed_list (abfd, &needed)) | |
324 | einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd); | |
325 | ||
326 | if (needed != NULL) | |
327 | { | |
328 | global_vercheck_needed = needed; | |
b34976b6 | 329 | global_vercheck_failed = FALSE; |
252b5132 RH |
330 | lang_for_each_input_file (gld${EMULATION_NAME}_vercheck); |
331 | if (global_vercheck_failed) | |
332 | { | |
cde43e70 | 333 | bfd_close (abfd); |
b34976b6 | 334 | /* Return FALSE to force the caller to move on to try |
0c7a8e5a | 335 | another file on the search path. */ |
b34976b6 | 336 | return FALSE; |
252b5132 RH |
337 | } |
338 | ||
339 | /* But wait! It gets much worse. On Linux, if a shared | |
0c7a8e5a AM |
340 | library does not use libc at all, we are supposed to skip |
341 | it the first time around in case we encounter a shared | |
342 | library later on with the same name which does use the | |
343 | version of libc that we want. This is much too horrible | |
344 | to use on any system other than Linux. */ | |
252b5132 RH |
345 | |
346 | EOF | |
347 | case ${target} in | |
348 | *-*-linux-gnu*) | |
349 | cat >>e${EMULATION_NAME}.c <<EOF | |
350 | { | |
351 | struct bfd_link_needed_list *l; | |
352 | ||
353 | for (l = needed; l != NULL; l = l->next) | |
354 | if (strncmp (l->name, "libc.so", 7) == 0) | |
355 | break; | |
356 | if (l == NULL) | |
357 | { | |
cde43e70 | 358 | bfd_close (abfd); |
b34976b6 | 359 | return FALSE; |
252b5132 RH |
360 | } |
361 | } | |
362 | ||
363 | EOF | |
364 | ;; | |
365 | esac | |
366 | cat >>e${EMULATION_NAME}.c <<EOF | |
367 | } | |
368 | } | |
369 | ||
370 | /* We've found a dynamic object matching the DT_NEEDED entry. */ | |
371 | ||
04925e1e AM |
372 | /* We have already checked that there is no other input file of the |
373 | same name. We must now check again that we are not including the | |
374 | same file twice. We need to do this because on many systems | |
375 | libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will | |
376 | reference libc.so.1. If we have already included libc.so, we | |
377 | don't want to include libc.so.1 if they are the same file, and we | |
378 | can only check that using stat. */ | |
379 | ||
380 | if (bfd_stat (abfd, &global_stat) != 0) | |
381 | einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd); | |
ec4eb78a L |
382 | |
383 | /* First strip off everything before the last '/'. */ | |
fed2999d | 384 | soname = lbasename (abfd->filename); |
ec4eb78a L |
385 | |
386 | if (trace_file_tries) | |
387 | info_msg (_("found %s at %s\n"), soname, name); | |
388 | ||
b34976b6 | 389 | global_found = FALSE; |
04925e1e AM |
390 | lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed); |
391 | if (global_found) | |
392 | { | |
b34976b6 | 393 | /* Return TRUE to indicate that we found the file, even though |
0c7a8e5a | 394 | we aren't going to do anything with it. */ |
b34976b6 | 395 | return TRUE; |
04925e1e AM |
396 | } |
397 | ||
4a43e768 AM |
398 | /* Specify the soname to use. */ |
399 | bfd_elf_set_dt_needed_name (abfd, soname); | |
04925e1e | 400 | |
4a43e768 AM |
401 | /* Tell the ELF linker that we don't want the output file to have a |
402 | DT_NEEDED entry for this file, unless it is used to resolve | |
403 | references in a regular object. */ | |
e56f61be L |
404 | class = DYN_DT_NEEDED; |
405 | ||
406 | /* Tell the ELF linker that we don't want the output file to have a | |
407 | DT_NEEDED entry for this file at all if the entry is from a file | |
408 | with DYN_NO_ADD_NEEDED. */ | |
7e9f0867 AM |
409 | if (needed->by != NULL |
410 | && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0) | |
e56f61be L |
411 | class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED; |
412 | ||
413 | bfd_elf_set_dyn_lib_class (abfd, class); | |
04925e1e AM |
414 | |
415 | /* Add this file into the symbol table. */ | |
416 | if (! bfd_link_add_symbols (abfd, &link_info)) | |
417 | einfo ("%F%B: could not read symbols: %E\n", abfd); | |
418 | ||
b34976b6 | 419 | return TRUE; |
04925e1e AM |
420 | } |
421 | ||
422 | ||
423 | /* Search for a needed file in a path. */ | |
424 | ||
b34976b6 | 425 | static bfd_boolean |
e56f61be L |
426 | gld${EMULATION_NAME}_search_needed (const char *path, |
427 | struct dt_needed *n, int force) | |
04925e1e AM |
428 | { |
429 | const char *s; | |
e56f61be | 430 | const char *name = n->name; |
04925e1e | 431 | size_t len; |
e56f61be | 432 | struct dt_needed needed; |
04925e1e | 433 | |
cc5ce052 | 434 | if (name[0] == '/') |
e56f61be | 435 | return gld${EMULATION_NAME}_try_needed (n, force); |
cc5ce052 | 436 | |
04925e1e | 437 | if (path == NULL || *path == '\0') |
b34976b6 | 438 | return FALSE; |
e56f61be L |
439 | |
440 | needed.by = n->by; | |
441 | needed.name = n->name; | |
442 | ||
04925e1e AM |
443 | len = strlen (name); |
444 | while (1) | |
445 | { | |
446 | char *filename, *sset; | |
447 | ||
448 | s = strchr (path, ':'); | |
449 | if (s == NULL) | |
450 | s = path + strlen (path); | |
451 | ||
452 | filename = (char *) xmalloc (s - path + len + 2); | |
453 | if (s == path) | |
454 | sset = filename; | |
455 | else | |
456 | { | |
457 | memcpy (filename, path, s - path); | |
458 | filename[s - path] = '/'; | |
459 | sset = filename + (s - path) + 1; | |
460 | } | |
461 | strcpy (sset, name); | |
462 | ||
e56f61be L |
463 | needed.name = filename; |
464 | if (gld${EMULATION_NAME}_try_needed (&needed, force)) | |
b34976b6 | 465 | return TRUE; |
04925e1e AM |
466 | |
467 | free (filename); | |
468 | ||
469 | if (*s == '\0') | |
470 | break; | |
471 | path = s + 1; | |
472 | } | |
473 | ||
b34976b6 | 474 | return FALSE; |
04925e1e AM |
475 | } |
476 | ||
477 | EOF | |
9c8ebd6a | 478 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
3dc16cab | 479 | cat >>e${EMULATION_NAME}.c <<EOF |
9c8ebd6a DJ |
480 | |
481 | /* Add the sysroot to every entry in a colon-separated path. */ | |
482 | ||
483 | static char * | |
0c7a8e5a | 484 | gld${EMULATION_NAME}_add_sysroot (const char *path) |
9c8ebd6a DJ |
485 | { |
486 | int len, colons, i; | |
487 | char *ret, *p; | |
488 | ||
489 | len = strlen (path); | |
490 | colons = 0; | |
491 | i = 0; | |
492 | while (path[i]) | |
493 | if (path[i++] == ':') | |
494 | colons++; | |
495 | ||
496 | if (path[i]) | |
497 | colons++; | |
498 | ||
42644a89 | 499 | len = len + (colons + 1) * strlen (ld_sysroot); |
9c8ebd6a DJ |
500 | ret = xmalloc (len + 1); |
501 | strcpy (ret, ld_sysroot); | |
502 | p = ret + strlen (ret); | |
503 | i = 0; | |
504 | while (path[i]) | |
505 | if (path[i] == ':') | |
506 | { | |
0c7a8e5a | 507 | *p++ = path[i++]; |
9c8ebd6a DJ |
508 | strcpy (p, ld_sysroot); |
509 | p = p + strlen (p); | |
9c8ebd6a DJ |
510 | } |
511 | else | |
512 | *p++ = path[i++]; | |
513 | ||
514 | *p = 0; | |
515 | return ret; | |
516 | } | |
04925e1e | 517 | |
3dc16cab DJ |
518 | EOF |
519 | case ${target} in | |
520 | *-*-linux-gnu*) | |
521 | cat >>e${EMULATION_NAME}.c <<EOF | |
04925e1e AM |
522 | /* For a native linker, check the file /etc/ld.so.conf for directories |
523 | in which we may find shared libraries. /etc/ld.so.conf is really | |
482e8b32 | 524 | only meaningful on Linux. */ |
04925e1e | 525 | |
d3989512 | 526 | struct gld${EMULATION_NAME}_ld_so_conf |
04925e1e | 527 | { |
d3989512 JJ |
528 | char *path; |
529 | size_t len, alloc; | |
530 | }; | |
04925e1e | 531 | |
d3989512 JJ |
532 | static void |
533 | gld${EMULATION_NAME}_parse_ld_so_conf | |
534 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename); | |
535 | ||
536 | static void | |
537 | gld${EMULATION_NAME}_parse_ld_so_conf_include | |
538 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename, | |
539 | const char *pattern) | |
540 | { | |
541 | char *newp = NULL; | |
542 | glob_t gl; | |
543 | ||
544 | if (pattern[0] != '/') | |
04925e1e | 545 | { |
d3989512 JJ |
546 | char *p = strrchr (filename, '/'); |
547 | size_t patlen = strlen (pattern) + 1; | |
04925e1e | 548 | |
d3989512 JJ |
549 | newp = xmalloc (p - filename + 1 + patlen); |
550 | memcpy (newp, filename, p - filename + 1); | |
551 | memcpy (newp + (p - filename + 1), pattern, patlen); | |
552 | pattern = newp; | |
553 | } | |
04925e1e | 554 | |
d3989512 JJ |
555 | if (glob (pattern, 0, NULL, &gl) == 0) |
556 | { | |
557 | size_t i; | |
558 | ||
559 | for (i = 0; i < gl.gl_pathc; ++i) | |
560 | gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]); | |
561 | globfree (&gl); | |
562 | } | |
563 | ||
564 | if (newp) | |
565 | free (newp); | |
566 | } | |
567 | ||
568 | static void | |
569 | gld${EMULATION_NAME}_parse_ld_so_conf | |
570 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename) | |
571 | { | |
572 | FILE *f = fopen (filename, FOPEN_RT); | |
563f4125 JJ |
573 | char *line; |
574 | size_t linelen; | |
d3989512 JJ |
575 | |
576 | if (f == NULL) | |
577 | return; | |
578 | ||
563f4125 JJ |
579 | linelen = 256; |
580 | line = xmalloc (linelen); | |
581 | do | |
d3989512 | 582 | { |
563f4125 JJ |
583 | char *p = line, *q; |
584 | ||
585 | /* Normally this would use getline(3), but we need to be portable. */ | |
586 | while ((q = fgets (p, linelen - (p - line), f)) != NULL | |
587 | && strlen (q) == linelen - (p - line) - 1 | |
588 | && line[linelen - 2] != '\n') | |
589 | { | |
590 | line = xrealloc (line, 2 * linelen); | |
591 | p = line + linelen - 1; | |
592 | linelen += linelen; | |
593 | } | |
594 | ||
595 | if (q == NULL && p == line) | |
596 | break; | |
d3989512 JJ |
597 | |
598 | p = strchr (line, '\n'); | |
599 | if (p) | |
600 | *p = '\0'; | |
601 | ||
602 | /* Because the file format does not know any form of quoting we | |
603 | can search forward for the next '#' character and if found | |
604 | make it terminating the line. */ | |
605 | p = strchr (line, '#'); | |
606 | if (p) | |
607 | *p = '\0'; | |
608 | ||
609 | /* Remove leading whitespace. NUL is no whitespace character. */ | |
610 | p = line; | |
611 | while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v') | |
612 | ++p; | |
613 | ||
614 | /* If the line is blank it is ignored. */ | |
615 | if (p[0] == '\0') | |
616 | continue; | |
04925e1e | 617 | |
d3989512 JJ |
618 | if (!strncmp (p, "include", 7) && (p[7] == ' ' || p[7] == '\t')) |
619 | { | |
620 | char *dir, c; | |
621 | p += 8; | |
622 | do | |
04925e1e | 623 | { |
d3989512 JJ |
624 | while (*p == ' ' || *p == '\t') |
625 | ++p; | |
04925e1e | 626 | |
d3989512 JJ |
627 | if (*p == '\0') |
628 | break; | |
252b5132 | 629 | |
d3989512 JJ |
630 | dir = p; |
631 | ||
632 | while (*p != ' ' && *p != '\t' && *p) | |
633 | ++p; | |
634 | ||
635 | c = *p; | |
636 | *p++ = '\0'; | |
637 | if (dir[0] != '\0') | |
638 | gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename, | |
639 | dir); | |
640 | } | |
641 | while (c != '\0'); | |
642 | } | |
643 | else | |
644 | { | |
645 | char *dir = p; | |
646 | while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f' | |
647 | && *p != '\r' && *p != '\v') | |
648 | ++p; | |
649 | ||
650 | while (p != dir && p[-1] == '/') | |
651 | --p; | |
652 | if (info->path == NULL) | |
653 | { | |
654 | info->alloc = p - dir + 1 + 256; | |
655 | info->path = xmalloc (info->alloc); | |
656 | info->len = 0; | |
657 | } | |
04925e1e AM |
658 | else |
659 | { | |
d3989512 JJ |
660 | if (info->len + 1 + (p - dir) >= info->alloc) |
661 | { | |
662 | info->alloc += p - dir + 256; | |
663 | info->path = xrealloc (info->path, info->alloc); | |
664 | } | |
665 | info->path[info->len++] = ':'; | |
04925e1e | 666 | } |
d3989512 JJ |
667 | memcpy (info->path + info->len, dir, p - dir); |
668 | info->len += p - dir; | |
669 | info->path[info->len] = '\0'; | |
670 | } | |
671 | } | |
563f4125 | 672 | while (! feof (f)); |
d3989512 JJ |
673 | free (line); |
674 | fclose (f); | |
675 | } | |
252b5132 | 676 | |
d3989512 JJ |
677 | static bfd_boolean |
678 | gld${EMULATION_NAME}_check_ld_so_conf (const char *name, int force) | |
679 | { | |
680 | static bfd_boolean initialized; | |
681 | static char *ld_so_conf; | |
682 | struct dt_needed needed; | |
252b5132 | 683 | |
d3989512 JJ |
684 | if (! initialized) |
685 | { | |
686 | char *tmppath; | |
687 | struct gld${EMULATION_NAME}_ld_so_conf info; | |
9c8ebd6a | 688 | |
d3989512 JJ |
689 | tmppath = concat (ld_sysroot, "/etc/ld.so.conf", NULL); |
690 | info.path = NULL; | |
691 | info.len = info.alloc = 0; | |
692 | gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath); | |
693 | free (tmppath); | |
694 | if (info.path) | |
695 | { | |
696 | char *d = gld${EMULATION_NAME}_add_sysroot (info.path); | |
697 | free (info.path); | |
698 | ld_so_conf = d; | |
04925e1e | 699 | } |
b34976b6 | 700 | initialized = TRUE; |
04925e1e | 701 | } |
49e56c49 | 702 | |
04925e1e | 703 | if (ld_so_conf == NULL) |
b34976b6 | 704 | return FALSE; |
252b5132 | 705 | |
e56f61be L |
706 | |
707 | needed.by = NULL; | |
708 | needed.name = name; | |
709 | return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force); | |
252b5132 RH |
710 | } |
711 | ||
04925e1e | 712 | EOF |
9c8ebd6a DJ |
713 | # Linux |
714 | ;; | |
04925e1e AM |
715 | esac |
716 | fi | |
717 | cat >>e${EMULATION_NAME}.c <<EOF | |
718 | ||
252b5132 RH |
719 | /* See if an input file matches a DT_NEEDED entry by name. */ |
720 | ||
721 | static void | |
0c7a8e5a | 722 | gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s) |
252b5132 RH |
723 | { |
724 | if (global_found) | |
725 | return; | |
726 | ||
577a0623 | 727 | if (s->filename != NULL) |
252b5132 | 728 | { |
577a0623 AM |
729 | const char *f; |
730 | ||
731 | if (strcmp (s->filename, global_needed->name) == 0) | |
732 | { | |
b34976b6 | 733 | global_found = TRUE; |
577a0623 AM |
734 | return; |
735 | } | |
736 | ||
737 | if (s->search_dirs_flag) | |
738 | { | |
739 | f = strrchr (s->filename, '/'); | |
740 | if (f != NULL | |
741 | && strcmp (f + 1, global_needed->name) == 0) | |
742 | { | |
b34976b6 | 743 | global_found = TRUE; |
577a0623 AM |
744 | return; |
745 | } | |
746 | } | |
252b5132 RH |
747 | } |
748 | ||
749 | if (s->the_bfd != NULL) | |
750 | { | |
751 | const char *soname; | |
752 | ||
753 | soname = bfd_elf_get_dt_soname (s->the_bfd); | |
754 | if (soname != NULL | |
755 | && strcmp (soname, global_needed->name) == 0) | |
756 | { | |
b34976b6 | 757 | global_found = TRUE; |
252b5132 RH |
758 | return; |
759 | } | |
760 | } | |
252b5132 RH |
761 | } |
762 | ||
41392f03 AM |
763 | EOF |
764 | ||
765 | if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then | |
766 | cat >>e${EMULATION_NAME}.c <<EOF | |
04925e1e AM |
767 | |
768 | /* This is called after all the input files have been opened. */ | |
252b5132 RH |
769 | |
770 | static void | |
0c7a8e5a | 771 | gld${EMULATION_NAME}_after_open (void) |
252b5132 | 772 | { |
04925e1e | 773 | struct bfd_link_needed_list *needed, *l; |
252b5132 | 774 | |
04925e1e | 775 | /* We only need to worry about this when doing a final link. */ |
1049f94e | 776 | if (link_info.relocatable || !link_info.executable) |
252b5132 RH |
777 | return; |
778 | ||
04925e1e AM |
779 | /* Get the list of files which appear in DT_NEEDED entries in |
780 | dynamic objects included in the link (often there will be none). | |
781 | For each such file, we want to track down the corresponding | |
782 | library, and include the symbol table in the link. This is what | |
783 | the runtime dynamic linker will do. Tracking the files down here | |
784 | permits one dynamic object to include another without requiring | |
785 | special action by the person doing the link. Note that the | |
786 | needed list can actually grow while we are stepping through this | |
787 | loop. */ | |
788 | needed = bfd_elf_get_needed_list (output_bfd, &link_info); | |
789 | for (l = needed; l != NULL; l = l->next) | |
252b5132 | 790 | { |
04925e1e | 791 | struct bfd_link_needed_list *ll; |
e56f61be | 792 | struct dt_needed n, nn; |
04925e1e | 793 | int force; |
252b5132 | 794 | |
7e9f0867 AM |
795 | /* If the lib that needs this one was --as-needed and wasn't |
796 | found to be needed, then this lib isn't needed either. */ | |
797 | if (l->by != NULL | |
798 | && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0) | |
799 | continue; | |
800 | ||
04925e1e AM |
801 | /* If we've already seen this file, skip it. */ |
802 | for (ll = needed; ll != l; ll = ll->next) | |
7e9f0867 AM |
803 | if ((ll->by == NULL |
804 | || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0) | |
805 | && strcmp (ll->name, l->name) == 0) | |
04925e1e AM |
806 | break; |
807 | if (ll != l) | |
808 | continue; | |
252b5132 | 809 | |
04925e1e AM |
810 | /* See if this file was included in the link explicitly. */ |
811 | global_needed = l; | |
b34976b6 | 812 | global_found = FALSE; |
04925e1e AM |
813 | lang_for_each_input_file (gld${EMULATION_NAME}_check_needed); |
814 | if (global_found) | |
815 | continue; | |
252b5132 | 816 | |
e56f61be L |
817 | n.by = l->by; |
818 | n.name = l->name; | |
819 | nn.by = l->by; | |
ec4eb78a L |
820 | if (trace_file_tries) |
821 | info_msg (_("%s needed by %B\n"), l->name, l->by); | |
822 | ||
04925e1e AM |
823 | /* We need to find this file and include the symbol table. We |
824 | want to search for the file in the same way that the dynamic | |
825 | linker will search. That means that we want to use | |
826 | rpath_link, rpath, then the environment variable | |
ec4eb78a L |
827 | LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH |
828 | entries (native only), then the linker script LIB_SEARCH_DIRS. | |
829 | We do not search using the -L arguments. | |
252b5132 | 830 | |
04925e1e AM |
831 | We search twice. The first time, we skip objects which may |
832 | introduce version mismatches. The second time, we force | |
833 | their use. See gld${EMULATION_NAME}_vercheck comment. */ | |
834 | for (force = 0; force < 2; force++) | |
835 | { | |
04925e1e AM |
836 | size_t len; |
837 | search_dirs_type *search; | |
6dd8c765 | 838 | EOF |
9c8ebd6a | 839 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
6dd8c765 | 840 | cat >>e${EMULATION_NAME}.c <<EOF |
dcb0bd0e | 841 | const char *lib_path; |
6dd8c765 L |
842 | struct bfd_link_needed_list *rp; |
843 | int found; | |
844 | EOF | |
6dd8c765 L |
845 | fi |
846 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 | 847 | |
04925e1e | 848 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link, |
e56f61be | 849 | &n, force)) |
04925e1e | 850 | break; |
dcb0bd0e | 851 | EOF |
9c8ebd6a | 852 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
dcb0bd0e | 853 | cat >>e${EMULATION_NAME}.c <<EOF |
04925e1e | 854 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath, |
e56f61be | 855 | &n, force)) |
04925e1e | 856 | break; |
9c8ebd6a DJ |
857 | EOF |
858 | fi | |
859 | if [ "x${NATIVE}" = xyes ] ; then | |
860 | cat >>e${EMULATION_NAME}.c <<EOF | |
04925e1e AM |
861 | if (command_line.rpath_link == NULL |
862 | && command_line.rpath == NULL) | |
863 | { | |
864 | lib_path = (const char *) getenv ("LD_RUN_PATH"); | |
e56f61be | 865 | if (gld${EMULATION_NAME}_search_needed (lib_path, &n, |
04925e1e AM |
866 | force)) |
867 | break; | |
868 | } | |
04925e1e | 869 | lib_path = (const char *) getenv ("LD_LIBRARY_PATH"); |
e56f61be | 870 | if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force)) |
04925e1e | 871 | break; |
9c8ebd6a DJ |
872 | EOF |
873 | fi | |
874 | if [ "x${USE_LIBPATH}" = xyes ] ; then | |
875 | cat >>e${EMULATION_NAME}.c <<EOF | |
ec4eb78a | 876 | found = 0; |
6dd8c765 L |
877 | rp = bfd_elf_get_runpath_list (output_bfd, &link_info); |
878 | for (; !found && rp != NULL; rp = rp->next) | |
ec4eb78a | 879 | { |
9c8ebd6a | 880 | char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name); |
ec4eb78a | 881 | found = (rp->by == l->by |
9c8ebd6a | 882 | && gld${EMULATION_NAME}_search_needed (tmpname, |
e56f61be | 883 | &n, |
ec4eb78a | 884 | force)); |
9c8ebd6a | 885 | free (tmpname); |
ec4eb78a L |
886 | } |
887 | if (found) | |
888 | break; | |
889 | ||
04925e1e | 890 | EOF |
04925e1e AM |
891 | fi |
892 | cat >>e${EMULATION_NAME}.c <<EOF | |
893 | len = strlen (l->name); | |
894 | for (search = search_head; search != NULL; search = search->next) | |
895 | { | |
896 | char *filename; | |
252b5132 | 897 | |
04925e1e AM |
898 | if (search->cmdline) |
899 | continue; | |
900 | filename = (char *) xmalloc (strlen (search->name) + len + 2); | |
901 | sprintf (filename, "%s/%s", search->name, l->name); | |
e56f61be L |
902 | nn.name = filename; |
903 | if (gld${EMULATION_NAME}_try_needed (&nn, force)) | |
04925e1e AM |
904 | break; |
905 | free (filename); | |
906 | } | |
907 | if (search != NULL) | |
908 | break; | |
909 | EOF | |
9c8ebd6a DJ |
910 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
911 | case ${target} in | |
912 | *-*-linux-gnu*) | |
913 | cat >>e${EMULATION_NAME}.c <<EOF | |
04925e1e AM |
914 | if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force)) |
915 | break; | |
916 | EOF | |
9c8ebd6a DJ |
917 | # Linux |
918 | ;; | |
04925e1e AM |
919 | esac |
920 | fi | |
921 | cat >>e${EMULATION_NAME}.c <<EOF | |
922 | } | |
252b5132 | 923 | |
04925e1e AM |
924 | if (force < 2) |
925 | continue; | |
252b5132 | 926 | |
e374f1d9 | 927 | einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n", |
04925e1e AM |
928 | l->name, l->by); |
929 | } | |
930 | } | |
252b5132 | 931 | |
41392f03 AM |
932 | EOF |
933 | fi | |
934 | ||
935 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 | 936 | |
04925e1e | 937 | /* Look through an expression for an assignment statement. */ |
252b5132 | 938 | |
04925e1e | 939 | static void |
0c7a8e5a | 940 | gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp) |
04925e1e | 941 | { |
4ea42fb7 | 942 | bfd_boolean provide = FALSE; |
04925e1e AM |
943 | |
944 | switch (exp->type.node_class) | |
252b5132 | 945 | { |
04925e1e | 946 | case etree_provide: |
4ea42fb7 AM |
947 | provide = TRUE; |
948 | /* Fall thru */ | |
949 | case etree_assign: | |
04925e1e AM |
950 | /* We call record_link_assignment even if the symbol is defined. |
951 | This is because if it is defined by a dynamic object, we | |
952 | actually want to use the value defined by the linker script, | |
953 | not the value from the dynamic object (because we are setting | |
954 | symbols like etext). If the symbol is defined by a regular | |
955 | object, then, as it happens, calling record_link_assignment | |
956 | will do no harm. */ | |
04925e1e | 957 | if (strcmp (exp->assign.dst, ".") != 0) |
252b5132 | 958 | { |
4ea42fb7 AM |
959 | if (!bfd_elf_record_link_assignment (output_bfd, &link_info, |
960 | exp->assign.dst, provide)) | |
04925e1e AM |
961 | einfo ("%P%F: failed to record assignment to %s: %E\n", |
962 | exp->assign.dst); | |
252b5132 | 963 | } |
04925e1e AM |
964 | gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); |
965 | break; | |
252b5132 | 966 | |
04925e1e AM |
967 | case etree_binary: |
968 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); | |
969 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); | |
970 | break; | |
252b5132 | 971 | |
04925e1e AM |
972 | case etree_trinary: |
973 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond); | |
974 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
975 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); | |
976 | break; | |
252b5132 | 977 | |
04925e1e AM |
978 | case etree_unary: |
979 | gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); | |
980 | break; | |
252b5132 | 981 | |
04925e1e AM |
982 | default: |
983 | break; | |
252b5132 RH |
984 | } |
985 | } | |
986 | ||
04925e1e AM |
987 | |
988 | /* This is called by the before_allocation routine via | |
989 | lang_for_each_statement. It locates any assignment statements, and | |
990 | tells the ELF backend about them, in case they are assignments to | |
991 | symbols which are referred to by dynamic objects. */ | |
992 | ||
993 | static void | |
0c7a8e5a | 994 | gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s) |
04925e1e AM |
995 | { |
996 | if (s->header.type == lang_assignment_statement_enum) | |
997 | gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); | |
998 | } | |
999 | ||
41392f03 AM |
1000 | EOF |
1001 | ||
1002 | if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then | |
57e6abd2 AO |
1003 | if test x"${ELF_INTERPRETER_NAME+set}" = xset; then |
1004 | ELF_INTERPRETER_SET_DEFAULT=" | |
1005 | if (sinterp != NULL) | |
1006 | { | |
1007 | sinterp->contents = ${ELF_INTERPRETER_NAME}; | |
eea6121a | 1008 | sinterp->size = strlen (sinterp->contents) + 1; |
57e6abd2 AO |
1009 | } |
1010 | ||
1011 | " | |
1012 | else | |
1013 | ELF_INTERPRETER_SET_DEFAULT= | |
1014 | fi | |
41392f03 | 1015 | cat >>e${EMULATION_NAME}.c <<EOF |
04925e1e | 1016 | |
252b5132 RH |
1017 | /* This is called after the sections have been attached to output |
1018 | sections, but before any sizes or addresses have been set. */ | |
1019 | ||
1020 | static void | |
0c7a8e5a | 1021 | gld${EMULATION_NAME}_before_allocation (void) |
252b5132 RH |
1022 | { |
1023 | const char *rpath; | |
1024 | asection *sinterp; | |
1025 | ||
fd574c46 | 1026 | if (link_info.hash->type == bfd_link_elf_hash_table) |
e1918d23 AM |
1027 | _bfd_elf_tls_setup (output_bfd, &link_info); |
1028 | ||
252b5132 RH |
1029 | /* If we are going to make any variable assignments, we need to let |
1030 | the ELF backend know about them in case the variables are | |
1031 | referred to by dynamic objects. */ | |
1032 | lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment); | |
1033 | ||
1034 | /* Let the ELF backend work out the sizes of any sections required | |
1035 | by dynamic linking. */ | |
1036 | rpath = command_line.rpath; | |
1037 | if (rpath == NULL) | |
1038 | rpath = (const char *) getenv ("LD_RUN_PATH"); | |
5a580b3a | 1039 | if (! (bfd_elf_size_dynamic_sections |
0c7a8e5a | 1040 | (output_bfd, command_line.soname, rpath, |
605d9b3a | 1041 | command_line.filter_shlib, |
252b5132 RH |
1042 | (const char * const *) command_line.auxiliary_filters, |
1043 | &link_info, &sinterp, lang_elf_version_info))) | |
1044 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); | |
57e6abd2 | 1045 | ${ELF_INTERPRETER_SET_DEFAULT} |
252b5132 RH |
1046 | /* Let the user override the dynamic linker we are using. */ |
1047 | if (command_line.interpreter != NULL | |
1048 | && sinterp != NULL) | |
1049 | { | |
1050 | sinterp->contents = (bfd_byte *) command_line.interpreter; | |
eea6121a | 1051 | sinterp->size = strlen (command_line.interpreter) + 1; |
252b5132 RH |
1052 | } |
1053 | ||
1054 | /* Look for any sections named .gnu.warning. As a GNU extensions, | |
1055 | we treat such sections as containing warning messages. We print | |
1056 | out the warning message, and then zero out the section size so | |
1057 | that it does not get copied into the output file. */ | |
1058 | ||
1059 | { | |
1060 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
1061 | { | |
1062 | asection *s; | |
1063 | bfd_size_type sz; | |
8c675694 | 1064 | bfd_size_type prefix_len; |
252b5132 | 1065 | char *msg; |
b34976b6 | 1066 | bfd_boolean ret; |
8c675694 | 1067 | const char * gnu_warning_prefix = _("warning: "); |
252b5132 RH |
1068 | |
1069 | if (is->just_syms_flag) | |
1070 | continue; | |
1071 | ||
1072 | s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning"); | |
1073 | if (s == NULL) | |
1074 | continue; | |
1075 | ||
eea6121a | 1076 | sz = s->size; |
8c675694 NC |
1077 | prefix_len = strlen (gnu_warning_prefix); |
1078 | msg = xmalloc ((size_t) (prefix_len + sz + 1)); | |
1079 | strcpy (msg, gnu_warning_prefix); | |
1080 | if (! bfd_get_section_contents (is->the_bfd, s, msg + prefix_len, | |
1081 | (file_ptr) 0, sz)) | |
252b5132 RH |
1082 | einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n", |
1083 | is->the_bfd); | |
8c675694 | 1084 | msg[prefix_len + sz] = '\0'; |
252b5132 RH |
1085 | ret = link_info.callbacks->warning (&link_info, msg, |
1086 | (const char *) NULL, | |
1087 | is->the_bfd, (asection *) NULL, | |
1088 | (bfd_vma) 0); | |
1089 | ASSERT (ret); | |
1090 | free (msg); | |
1091 | ||
1092 | /* Clobber the section size, so that we don't waste copying the | |
1093 | warning into the output file. */ | |
eea6121a | 1094 | s->size = 0; |
252b5132 RH |
1095 | } |
1096 | } | |
1097 | } | |
1098 | ||
41392f03 AM |
1099 | EOF |
1100 | fi | |
1101 | ||
1102 | if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then | |
1103 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 | 1104 | |
04925e1e AM |
1105 | /* Try to open a dynamic archive. This is where we know that ELF |
1106 | dynamic libraries have an extension of .so (or .sl on oddball systems | |
1107 | like hpux). */ | |
1108 | ||
b34976b6 | 1109 | static bfd_boolean |
0c7a8e5a AM |
1110 | gld${EMULATION_NAME}_open_dynamic_archive |
1111 | (const char *arch, search_dirs_type *search, lang_input_statement_type *entry) | |
252b5132 | 1112 | { |
04925e1e AM |
1113 | const char *filename; |
1114 | char *string; | |
252b5132 | 1115 | |
04925e1e | 1116 | if (! entry->is_archive) |
b34976b6 | 1117 | return FALSE; |
252b5132 | 1118 | |
04925e1e | 1119 | filename = entry->filename; |
252b5132 | 1120 | |
04925e1e AM |
1121 | /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION |
1122 | is defined, but it does not seem worth the headache to optimize | |
1123 | away those two bytes of space. */ | |
1124 | string = (char *) xmalloc (strlen (search->name) | |
1125 | + strlen (filename) | |
1126 | + strlen (arch) | |
1127 | #ifdef EXTRA_SHLIB_EXTENSION | |
1128 | + strlen (EXTRA_SHLIB_EXTENSION) | |
1129 | #endif | |
1130 | + sizeof "/lib.so"); | |
1131 | ||
1132 | sprintf (string, "%s/lib%s%s.so", search->name, filename, arch); | |
1133 | ||
1134 | #ifdef EXTRA_SHLIB_EXTENSION | |
1135 | /* Try the .so extension first. If that fails build a new filename | |
1136 | using EXTRA_SHLIB_EXTENSION. */ | |
1137 | if (! ldfile_try_open_bfd (string, entry)) | |
1138 | sprintf (string, "%s/lib%s%s%s", search->name, | |
1139 | filename, arch, EXTRA_SHLIB_EXTENSION); | |
1140 | #endif | |
1141 | ||
1142 | if (! ldfile_try_open_bfd (string, entry)) | |
252b5132 | 1143 | { |
04925e1e | 1144 | free (string); |
b34976b6 | 1145 | return FALSE; |
04925e1e | 1146 | } |
252b5132 | 1147 | |
04925e1e | 1148 | entry->filename = string; |
252b5132 | 1149 | |
04925e1e AM |
1150 | /* We have found a dynamic object to include in the link. The ELF |
1151 | backend linker will create a DT_NEEDED entry in the .dynamic | |
1152 | section naming this file. If this file includes a DT_SONAME | |
1153 | entry, it will be used. Otherwise, the ELF linker will just use | |
1154 | the name of the file. For an archive found by searching, like | |
1155 | this one, the DT_NEEDED entry should consist of just the name of | |
1156 | the file, without the path information used to find it. Note | |
1157 | that we only need to do this if we have a dynamic object; an | |
1158 | archive will never be referenced by a DT_NEEDED entry. | |
252b5132 | 1159 | |
04925e1e AM |
1160 | FIXME: This approach--using bfd_elf_set_dt_needed_name--is not |
1161 | very pretty. I haven't been able to think of anything that is | |
1162 | pretty, though. */ | |
1163 | if (bfd_check_format (entry->the_bfd, bfd_object) | |
1164 | && (entry->the_bfd->flags & DYNAMIC) != 0) | |
1165 | { | |
04925e1e | 1166 | ASSERT (entry->is_archive && entry->search_dirs_flag); |
252b5132 | 1167 | |
04925e1e | 1168 | /* Rather than duplicating the logic above. Just use the |
1c9acd94 | 1169 | filename we recorded earlier. */ |
04925e1e | 1170 | |
fed2999d | 1171 | filename = lbasename (entry->filename); |
1c9acd94 | 1172 | bfd_elf_set_dt_needed_name (entry->the_bfd, filename); |
04925e1e AM |
1173 | } |
1174 | ||
b34976b6 | 1175 | return TRUE; |
04925e1e AM |
1176 | } |
1177 | ||
41392f03 AM |
1178 | EOF |
1179 | fi | |
cde43e70 AM |
1180 | |
1181 | if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then | |
41392f03 AM |
1182 | cat >>e${EMULATION_NAME}.c <<EOF |
1183 | ||
afd7a018 | 1184 | /* A variant of lang_output_section_find used by place_orphan. */ |
04925e1e | 1185 | |
04925e1e | 1186 | static lang_output_section_statement_type * |
2a36a117 | 1187 | output_rel_find (asection *sec, int isdyn) |
04925e1e | 1188 | { |
04925e1e | 1189 | lang_output_section_statement_type *lookup; |
ba493122 | 1190 | lang_output_section_statement_type *last = NULL; |
2a36a117 | 1191 | lang_output_section_statement_type *last_alloc = NULL; |
ba493122 AM |
1192 | lang_output_section_statement_type *last_rel = NULL; |
1193 | lang_output_section_statement_type *last_rel_alloc = NULL; | |
24cdb50a | 1194 | int rela = sec->name[4] == 'a'; |
04925e1e | 1195 | |
afd7a018 AM |
1196 | for (lookup = &lang_output_section_statement.head->output_section_statement; |
1197 | lookup != NULL; | |
1198 | lookup = lookup->next) | |
04925e1e | 1199 | { |
0841712e JJ |
1200 | if (lookup->constraint != -1 |
1201 | && strncmp (".rel", lookup->name, 4) == 0) | |
04925e1e | 1202 | { |
24cdb50a | 1203 | int lookrela = lookup->name[4] == 'a'; |
ba493122 | 1204 | |
2a36a117 AM |
1205 | /* .rel.dyn must come before all other reloc sections, to suit |
1206 | GNU ld.so. */ | |
1207 | if (isdyn) | |
1208 | break; | |
1209 | ||
1210 | /* Don't place after .rel.plt as doing so results in wrong | |
1211 | dynamic tags. */ | |
1212 | if (strcmp (".plt", lookup->name + 4 + lookrela) == 0) | |
ba493122 | 1213 | break; |
2a36a117 AM |
1214 | |
1215 | if (rela == lookrela || last_rel == NULL) | |
24cdb50a | 1216 | last_rel = lookup; |
2a36a117 AM |
1217 | if ((rela == lookrela || last_rel_alloc == NULL) |
1218 | && lookup->bfd_section != NULL | |
ba493122 AM |
1219 | && (lookup->bfd_section->flags & SEC_ALLOC) != 0) |
1220 | last_rel_alloc = lookup; | |
04925e1e | 1221 | } |
2a36a117 AM |
1222 | |
1223 | last = lookup; | |
1224 | if (lookup->bfd_section != NULL | |
1225 | && (lookup->bfd_section->flags & SEC_ALLOC) != 0) | |
1226 | last_alloc = lookup; | |
252b5132 | 1227 | } |
ba493122 AM |
1228 | |
1229 | if (last_rel_alloc) | |
1230 | return last_rel_alloc; | |
1231 | ||
1232 | if (last_rel) | |
1233 | return last_rel; | |
1234 | ||
2a36a117 AM |
1235 | if (last_alloc) |
1236 | return last_alloc; | |
1237 | ||
ba493122 | 1238 | return last; |
252b5132 RH |
1239 | } |
1240 | ||
1241 | /* Place an orphan section. We use this to put random SHF_ALLOC | |
1242 | sections in the right segment. */ | |
1243 | ||
b34976b6 | 1244 | static bfd_boolean |
0c7a8e5a | 1245 | gld${EMULATION_NAME}_place_orphan (lang_input_statement_type *file, asection *s) |
252b5132 | 1246 | { |
afd7a018 AM |
1247 | static struct orphan_save hold[] = |
1248 | { | |
1249 | { ".text", | |
1250 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE, | |
1251 | 0, 0, 0, 0 }, | |
1252 | { ".rodata", | |
1253 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1254 | 0, 0, 0, 0 }, | |
1255 | { ".data", | |
1256 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA, | |
1257 | 0, 0, 0, 0 }, | |
1258 | { ".bss", | |
1259 | SEC_ALLOC, | |
1260 | 0, 0, 0, 0 }, | |
1261 | { 0, | |
1262 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1263 | 0, 0, 0, 0 }, | |
1264 | { ".interp", | |
1265 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1266 | 0, 0, 0, 0 }, | |
1267 | { ".sdata", | |
1268 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA, | |
1269 | 0, 0, 0, 0 } | |
1270 | }; | |
1271 | enum orphan_save_index | |
1272 | { | |
1273 | orphan_text = 0, | |
1274 | orphan_rodata, | |
1275 | orphan_data, | |
1276 | orphan_bss, | |
1277 | orphan_rel, | |
1278 | orphan_interp, | |
1279 | orphan_sdata | |
1280 | }; | |
1281 | static int orphan_init_done = 0; | |
6a345e87 | 1282 | struct orphan_save *place; |
a854a4a7 | 1283 | const char *secname; |
afd7a018 | 1284 | lang_output_section_statement_type *after; |
252b5132 | 1285 | lang_output_section_statement_type *os; |
24cdb50a | 1286 | int isdyn = 0; |
252b5132 | 1287 | |
aea4bd9d | 1288 | secname = bfd_get_section_name (s->owner, s); |
9f61903d | 1289 | |
1049f94e | 1290 | if (! link_info.relocatable |
24cdb50a | 1291 | && link_info.combreloc |
a8927cfd | 1292 | && (s->flags & SEC_ALLOC) |
24cdb50a AM |
1293 | && strncmp (secname, ".rel", 4) == 0) |
1294 | { | |
1295 | if (secname[4] == 'a') | |
1296 | secname = ".rela.dyn"; | |
1297 | else | |
1298 | secname = ".rel.dyn"; | |
1299 | isdyn = 1; | |
1300 | } | |
aea4bd9d | 1301 | |
d0d6a25b | 1302 | if (isdyn || (!config.unique_orphan_sections && !unique_section_p (s))) |
252b5132 | 1303 | { |
6d02a667 | 1304 | /* Look through the script to see where to place this section. */ |
a854a4a7 AM |
1305 | os = lang_output_section_find (secname); |
1306 | ||
1307 | if (os != NULL | |
0f33d40b | 1308 | && (os->bfd_section == NULL |
afd7a018 | 1309 | || os->bfd_section->flags == 0 |
0f33d40b AM |
1310 | || ((s->flags ^ os->bfd_section->flags) |
1311 | & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
a854a4a7 | 1312 | { |
0f33d40b | 1313 | /* We already have an output section statement with this |
afd7a018 AM |
1314 | name, and its bfd section, if any, has compatible flags. |
1315 | If the section already exists but does not have any flags | |
1316 | set, then it has been created by the linker, probably as a | |
1317 | result of a --section-start command line switch. */ | |
39dcfe18 | 1318 | lang_add_section (&os->children, s, os, file); |
b34976b6 | 1319 | return TRUE; |
a854a4a7 | 1320 | } |
252b5132 RH |
1321 | } |
1322 | ||
afd7a018 AM |
1323 | if (!orphan_init_done) |
1324 | { | |
1325 | struct orphan_save *ho; | |
1326 | for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho) | |
1327 | if (ho->name != NULL) | |
1328 | { | |
1329 | ho->os = lang_output_section_find (ho->name); | |
1330 | if (ho->os != NULL && ho->os->flags == 0) | |
1331 | ho->os->flags = ho->flags; | |
1332 | } | |
1333 | orphan_init_done = 1; | |
1334 | } | |
252b5132 RH |
1335 | |
1336 | /* If this is a final link, then always put .gnu.warning.SYMBOL | |
1337 | sections into the .text section to get them out of the way. */ | |
36af4a4e | 1338 | if (link_info.executable |
1049f94e | 1339 | && ! link_info.relocatable |
252b5132 | 1340 | && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0 |
afd7a018 | 1341 | && hold[orphan_text].os != NULL) |
252b5132 | 1342 | { |
afd7a018 AM |
1343 | lang_add_section (&hold[orphan_text].os->children, s, |
1344 | hold[orphan_text].os, file); | |
b34976b6 | 1345 | return TRUE; |
252b5132 RH |
1346 | } |
1347 | ||
1348 | /* Decide which segment the section should go in based on the | |
1349 | section name and section flags. We put loadable .note sections | |
1350 | right after the .interp section, so that the PT_NOTE segment is | |
1351 | stored right after the program headers where the OS can read it | |
1352 | in the first page. */ | |
aea4bd9d | 1353 | |
71bfc0ae AM |
1354 | place = NULL; |
1355 | if ((s->flags & SEC_ALLOC) == 0) | |
1356 | ; | |
252b5132 | 1357 | else if ((s->flags & SEC_LOAD) != 0 |
afd7a018 AM |
1358 | && strncmp (secname, ".note", 5) == 0) |
1359 | place = &hold[orphan_interp]; | |
1360 | else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
1361 | place = &hold[orphan_bss]; | |
1362 | else if ((s->flags & SEC_SMALL_DATA) != 0) | |
1363 | place = &hold[orphan_sdata]; | |
1364 | else if ((s->flags & SEC_READONLY) == 0) | |
1365 | place = &hold[orphan_data]; | |
252b5132 | 1366 | else if (strncmp (secname, ".rel", 4) == 0 |
afd7a018 AM |
1367 | && (s->flags & SEC_LOAD) != 0) |
1368 | place = &hold[orphan_rel]; | |
1369 | else if ((s->flags & SEC_CODE) == 0) | |
1370 | place = &hold[orphan_rodata]; | |
1371 | else | |
1372 | place = &hold[orphan_text]; | |
71bfc0ae | 1373 | |
afd7a018 | 1374 | after = NULL; |
5ba47421 | 1375 | if (place != NULL) |
252b5132 | 1376 | { |
afd7a018 | 1377 | if (place->os == NULL) |
5ba47421 | 1378 | { |
afd7a018 AM |
1379 | if (place->name != NULL) |
1380 | place->os = lang_output_section_find (place->name); | |
1381 | else | |
1382 | place->os = output_rel_find (s, isdyn); | |
5ba47421 | 1383 | } |
afd7a018 AM |
1384 | after = place->os; |
1385 | if (after == NULL) | |
1386 | after = lang_output_section_find_by_flags (s, &place->os); | |
1387 | if (after == NULL) | |
1388 | /* *ABS* is always the first output section statement. */ | |
1389 | after = &lang_output_section_statement.head->output_section_statement; | |
252b5132 RH |
1390 | } |
1391 | ||
afd7a018 AM |
1392 | /* Choose a unique name for the section. This will be needed if the |
1393 | same section name appears in the input file with different | |
1394 | loadable or allocatable characteristics. */ | |
1395 | if (bfd_get_section_by_name (output_bfd, secname) != NULL) | |
252b5132 | 1396 | { |
afd7a018 AM |
1397 | static int count = 1; |
1398 | secname = bfd_get_unique_section_name (output_bfd, secname, &count); | |
1399 | if (secname == NULL) | |
1400 | einfo ("%F%P: place_orphan failed: %E\n"); | |
71bfc0ae | 1401 | } |
5ba47421 | 1402 | |
afd7a018 | 1403 | lang_insert_orphan (file, s, secname, after, place, NULL, NULL); |
252b5132 | 1404 | |
b34976b6 | 1405 | return TRUE; |
252b5132 | 1406 | } |
c56feb2b AM |
1407 | EOF |
1408 | fi | |
1409 | ||
1410 | if test x"$LDEMUL_FINISH" != xgld"$EMULATION_NAME"_finish; then | |
1411 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 | 1412 | |
73d074b4 | 1413 | static void |
0c7a8e5a | 1414 | gld${EMULATION_NAME}_finish (void) |
73d074b4 | 1415 | { |
7a945db6 | 1416 | if (bfd_elf_discard_info (output_bfd, &link_info)) |
73d074b4 | 1417 | { |
e3dc8847 HPN |
1418 | lang_reset_memory_regions (); |
1419 | ||
73d074b4 DJ |
1420 | /* Resize the sections. */ |
1421 | lang_size_sections (stat_ptr->head, abs_output_section, | |
cf888e70 | 1422 | &stat_ptr->head, 0, (bfd_vma) 0, NULL, TRUE); |
73d074b4 DJ |
1423 | |
1424 | /* Redo special stuff. */ | |
1425 | ldemul_after_allocation (); | |
1426 | ||
1427 | /* Do the assignments again. */ | |
1428 | lang_do_assignments (stat_ptr->head, abs_output_section, | |
2c382fb6 | 1429 | (fill_type *) 0, (bfd_vma) 0); |
73d074b4 DJ |
1430 | } |
1431 | } | |
41392f03 AM |
1432 | EOF |
1433 | fi | |
1434 | ||
1435 | if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then | |
1436 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 RH |
1437 | |
1438 | static char * | |
0c7a8e5a | 1439 | gld${EMULATION_NAME}_get_script (int *isfile) |
252b5132 RH |
1440 | EOF |
1441 | ||
1442 | if test -n "$COMPILE_IN" | |
1443 | then | |
1444 | # Scripts compiled in. | |
1445 | ||
1446 | # sed commands to quote an ld script as a C string. | |
597e2591 | 1447 | sc="-f stringify.sed" |
252b5132 RH |
1448 | |
1449 | cat >>e${EMULATION_NAME}.c <<EOF | |
60bcf0fa | 1450 | { |
252b5132 RH |
1451 | *isfile = 0; |
1452 | ||
1049f94e | 1453 | if (link_info.relocatable && config.build_constructors) |
597e2591 | 1454 | return |
252b5132 | 1455 | EOF |
afd7a018 AM |
1456 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c |
1457 | echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c | |
1458 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c | |
1459 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
1460 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
61585df2 | 1461 | if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else |
afd7a018 AM |
1462 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c |
1463 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
db6751f2 | 1464 | fi |
36af4a4e JJ |
1465 | if test -n "$GENERATE_PIE_SCRIPT" ; then |
1466 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
8c37241b JJ |
1467 | echo ' ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c |
1468 | echo ' && link_info.relro' >> e${EMULATION_NAME}.c | |
1469 | echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c | |
afd7a018 | 1470 | sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c |
36af4a4e | 1471 | echo ' ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1472 | sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c |
36af4a4e | 1473 | fi |
afd7a018 AM |
1474 | echo ' ; else if (link_info.pie) return' >> e${EMULATION_NAME}.c |
1475 | sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c | |
36af4a4e | 1476 | fi |
252b5132 | 1477 | if test -n "$GENERATE_SHLIB_SCRIPT" ; then |
82434356 | 1478 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then |
8c37241b JJ |
1479 | echo ' ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c |
1480 | echo ' && link_info.relro' >> e${EMULATION_NAME}.c | |
1481 | echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c | |
afd7a018 | 1482 | sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c |
db6751f2 | 1483 | echo ' ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1484 | sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c |
82434356 | 1485 | fi |
afd7a018 AM |
1486 | echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c |
1487 | sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c | |
252b5132 | 1488 | fi |
82434356 | 1489 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then |
8c37241b JJ |
1490 | echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c |
1491 | echo ' && (link_info.flags & DT_BIND_NOW)) return' >> e${EMULATION_NAME}.c | |
afd7a018 AM |
1492 | sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c |
1493 | echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c | |
1494 | sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c | |
82434356 | 1495 | fi |
afd7a018 AM |
1496 | echo ' ; else return' >> e${EMULATION_NAME}.c |
1497 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
1498 | echo '; }' >> e${EMULATION_NAME}.c | |
252b5132 RH |
1499 | |
1500 | else | |
1501 | # Scripts read from the filesystem. | |
1502 | ||
1503 | cat >>e${EMULATION_NAME}.c <<EOF | |
60bcf0fa | 1504 | { |
252b5132 RH |
1505 | *isfile = 1; |
1506 | ||
1049f94e | 1507 | if (link_info.relocatable && config.build_constructors) |
252b5132 | 1508 | return "ldscripts/${EMULATION_NAME}.xu"; |
1049f94e | 1509 | else if (link_info.relocatable) |
252b5132 RH |
1510 | return "ldscripts/${EMULATION_NAME}.xr"; |
1511 | else if (!config.text_read_only) | |
1512 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
a060b769 AM |
1513 | EOF |
1514 | if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : | |
1515 | else | |
1516 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 RH |
1517 | else if (!config.magic_demand_paged) |
1518 | return "ldscripts/${EMULATION_NAME}.xn"; | |
a060b769 AM |
1519 | EOF |
1520 | fi | |
36af4a4e JJ |
1521 | if test -n "$GENERATE_PIE_SCRIPT" ; then |
1522 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
1523 | cat >>e${EMULATION_NAME}.c <<EOF | |
8c37241b JJ |
1524 | else if (link_info.pie && link_info.combreloc |
1525 | && link_info.relro && (link_info.flags & DT_BIND_NOW)) | |
1526 | return "ldscripts/${EMULATION_NAME}.xdw"; | |
36af4a4e JJ |
1527 | else if (link_info.pie && link_info.combreloc) |
1528 | return "ldscripts/${EMULATION_NAME}.xdc"; | |
1529 | EOF | |
1530 | fi | |
1531 | cat >>e${EMULATION_NAME}.c <<EOF | |
1532 | else if (link_info.pie) | |
1533 | return "ldscripts/${EMULATION_NAME}.xd"; | |
1534 | EOF | |
1535 | fi | |
a060b769 AM |
1536 | if test -n "$GENERATE_SHLIB_SCRIPT" ; then |
1537 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
1538 | cat >>e${EMULATION_NAME}.c <<EOF | |
8c37241b JJ |
1539 | else if (link_info.shared && link_info.combreloc |
1540 | && link_info.relro && (link_info.flags & DT_BIND_NOW)) | |
1541 | return "ldscripts/${EMULATION_NAME}.xsw"; | |
a060b769 AM |
1542 | else if (link_info.shared && link_info.combreloc) |
1543 | return "ldscripts/${EMULATION_NAME}.xsc"; | |
1544 | EOF | |
1545 | fi | |
1546 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 RH |
1547 | else if (link_info.shared) |
1548 | return "ldscripts/${EMULATION_NAME}.xs"; | |
a060b769 AM |
1549 | EOF |
1550 | fi | |
1551 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
1552 | cat >>e${EMULATION_NAME}.c <<EOF | |
8c37241b JJ |
1553 | else if (link_info.combreloc && link_info.relro |
1554 | && (link_info.flags & DT_BIND_NOW)) | |
1555 | return "ldscripts/${EMULATION_NAME}.xw"; | |
a060b769 AM |
1556 | else if (link_info.combreloc) |
1557 | return "ldscripts/${EMULATION_NAME}.xc"; | |
1558 | EOF | |
1559 | fi | |
1560 | cat >>e${EMULATION_NAME}.c <<EOF | |
252b5132 RH |
1561 | else |
1562 | return "ldscripts/${EMULATION_NAME}.x"; | |
1563 | } | |
252b5132 | 1564 | |
3b108066 | 1565 | EOF |
41392f03 AM |
1566 | fi |
1567 | fi | |
3b108066 | 1568 | |
41392f03 | 1569 | if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then |
3b108066 | 1570 | |
e0ee487b L |
1571 | if test -n "$PARSE_AND_LIST_PROLOGUE" ; then |
1572 | cat >>e${EMULATION_NAME}.c <<EOF | |
1573 | $PARSE_AND_LIST_PROLOGUE | |
1574 | EOF | |
1575 | fi | |
1576 | ||
e0ee487b L |
1577 | cat >>e${EMULATION_NAME}.c <<EOF |
1578 | ||
6c1439be L |
1579 | #define OPTION_DISABLE_NEW_DTAGS (400) |
1580 | #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1) | |
a1ab1d2a | 1581 | #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1) |
65765700 | 1582 | #define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1) |
b58f81ae DJ |
1583 | #define OPTION_EXCLUDE_LIBS (OPTION_EH_FRAME_HDR + 1) |
1584 | ||
3bcf5557 | 1585 | static void |
0c7a8e5a AM |
1586 | gld${EMULATION_NAME}_add_options |
1587 | (int ns, char **shortopts, int nl, struct option **longopts, | |
1588 | int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED) | |
e0ee487b | 1589 | { |
3bcf5557 AM |
1590 | static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:"; |
1591 | static const struct option xtra_long[] = { | |
e0ee487b | 1592 | EOF |
e0ee487b L |
1593 | |
1594 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
1595 | cat >>e${EMULATION_NAME}.c <<EOF | |
3bcf5557 AM |
1596 | {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS}, |
1597 | {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS}, | |
1598 | {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR}, | |
b58f81ae | 1599 | {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS}, |
3bcf5557 | 1600 | {"Bgroup", no_argument, NULL, OPTION_GROUP}, |
e0ee487b L |
1601 | EOF |
1602 | fi | |
1603 | ||
1604 | if test -n "$PARSE_AND_LIST_LONGOPTS" ; then | |
1605 | cat >>e${EMULATION_NAME}.c <<EOF | |
3bcf5557 | 1606 | $PARSE_AND_LIST_LONGOPTS |
e0ee487b L |
1607 | EOF |
1608 | fi | |
1609 | ||
e0ee487b | 1610 | cat >>e${EMULATION_NAME}.c <<EOF |
3bcf5557 AM |
1611 | {NULL, no_argument, NULL, 0} |
1612 | }; | |
1613 | ||
1614 | *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short)); | |
1615 | memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short)); | |
1616 | *longopts = (struct option *) | |
1617 | xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long)); | |
1618 | memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long)); | |
1619 | } | |
04925e1e | 1620 | |
3bcf5557 | 1621 | static bfd_boolean |
0c7a8e5a | 1622 | gld${EMULATION_NAME}_handle_option (int optc) |
e0ee487b | 1623 | { |
e0ee487b L |
1624 | switch (optc) |
1625 | { | |
1626 | default: | |
3bcf5557 | 1627 | return FALSE; |
6c1439be | 1628 | |
e0ee487b | 1629 | EOF |
e0ee487b L |
1630 | |
1631 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
1632 | cat >>e${EMULATION_NAME}.c <<EOF | |
6c1439be | 1633 | case OPTION_DISABLE_NEW_DTAGS: |
b34976b6 | 1634 | link_info.new_dtags = FALSE; |
6c1439be L |
1635 | break; |
1636 | ||
1637 | case OPTION_ENABLE_NEW_DTAGS: | |
b34976b6 | 1638 | link_info.new_dtags = TRUE; |
6c1439be L |
1639 | break; |
1640 | ||
65765700 | 1641 | case OPTION_EH_FRAME_HDR: |
b34976b6 | 1642 | link_info.eh_frame_hdr = TRUE; |
65765700 JJ |
1643 | break; |
1644 | ||
a1ab1d2a UD |
1645 | case OPTION_GROUP: |
1646 | link_info.flags_1 |= (bfd_vma) DF_1_GROUP; | |
1647 | /* Groups must be self-contained. */ | |
560e09e9 NC |
1648 | link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR; |
1649 | link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR; | |
a1ab1d2a UD |
1650 | break; |
1651 | ||
b58f81ae DJ |
1652 | case OPTION_EXCLUDE_LIBS: |
1653 | add_excluded_libs (optarg); | |
1654 | break; | |
1655 | ||
e0ee487b L |
1656 | case 'z': |
1657 | if (strcmp (optarg, "initfirst") == 0) | |
1658 | link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST; | |
1659 | else if (strcmp (optarg, "interpose") == 0) | |
1660 | link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE; | |
1661 | else if (strcmp (optarg, "loadfltr") == 0) | |
1662 | link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR; | |
1663 | else if (strcmp (optarg, "nodefaultlib") == 0) | |
1664 | link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB; | |
1665 | else if (strcmp (optarg, "nodelete") == 0) | |
1666 | link_info.flags_1 |= (bfd_vma) DF_1_NODELETE; | |
1667 | else if (strcmp (optarg, "nodlopen") == 0) | |
1668 | link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN; | |
1669 | else if (strcmp (optarg, "nodump") == 0) | |
1670 | link_info.flags_1 |= (bfd_vma) DF_1_NODUMP; | |
1671 | else if (strcmp (optarg, "now") == 0) | |
1672 | { | |
1673 | link_info.flags |= (bfd_vma) DF_BIND_NOW; | |
1674 | link_info.flags_1 |= (bfd_vma) DF_1_NOW; | |
1675 | } | |
1676 | else if (strcmp (optarg, "origin") == 0) | |
1677 | { | |
1678 | link_info.flags |= (bfd_vma) DF_ORIGIN; | |
1679 | link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN; | |
1680 | } | |
a1ab1d2a | 1681 | else if (strcmp (optarg, "defs") == 0) |
560e09e9 | 1682 | link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR; |
aa713662 | 1683 | else if (strcmp (optarg, "muldefs") == 0) |
b34976b6 | 1684 | link_info.allow_multiple_definition = TRUE; |
db6751f2 | 1685 | else if (strcmp (optarg, "combreloc") == 0) |
b34976b6 | 1686 | link_info.combreloc = TRUE; |
db6751f2 | 1687 | else if (strcmp (optarg, "nocombreloc") == 0) |
b34976b6 | 1688 | link_info.combreloc = FALSE; |
8bd621d8 | 1689 | else if (strcmp (optarg, "nocopyreloc") == 0) |
0c7a8e5a | 1690 | link_info.nocopyreloc = TRUE; |
9ee5e499 JJ |
1691 | else if (strcmp (optarg, "execstack") == 0) |
1692 | { | |
1693 | link_info.execstack = TRUE; | |
1694 | link_info.noexecstack = FALSE; | |
1695 | } | |
1696 | else if (strcmp (optarg, "noexecstack") == 0) | |
1697 | { | |
1698 | link_info.noexecstack = TRUE; | |
1699 | link_info.execstack = FALSE; | |
1700 | } | |
8c37241b JJ |
1701 | else if (strcmp (optarg, "relro") == 0) |
1702 | link_info.relro = TRUE; | |
1703 | else if (strcmp (optarg, "norelro") == 0) | |
1704 | link_info.relro = FALSE; | |
e0ee487b | 1705 | /* What about the other Solaris -z options? FIXME. */ |
6c1439be | 1706 | break; |
e0ee487b L |
1707 | EOF |
1708 | fi | |
1709 | ||
1710 | if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then | |
1711 | cat >>e${EMULATION_NAME}.c <<EOF | |
1712 | $PARSE_AND_LIST_ARGS_CASES | |
1713 | EOF | |
1714 | fi | |
1715 | ||
e0ee487b L |
1716 | cat >>e${EMULATION_NAME}.c <<EOF |
1717 | } | |
1718 | ||
3bcf5557 | 1719 | return TRUE; |
e0ee487b L |
1720 | } |
1721 | ||
41392f03 | 1722 | EOF |
41392f03 | 1723 | |
4b209b22 | 1724 | if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then |
41392f03 AM |
1725 | cat >>e${EMULATION_NAME}.c <<EOF |
1726 | ||
e0ee487b | 1727 | static void |
0c7a8e5a | 1728 | gld${EMULATION_NAME}_list_options (FILE * file) |
e0ee487b L |
1729 | { |
1730 | EOF | |
e0ee487b L |
1731 | |
1732 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
1733 | cat >>e${EMULATION_NAME}.c <<EOF | |
a1ab1d2a | 1734 | fprintf (file, _(" -Bgroup\t\tSelects group name lookup rules for DSO\n")); |
6c1439be L |
1735 | fprintf (file, _(" --disable-new-dtags\tDisable new dynamic tags\n")); |
1736 | fprintf (file, _(" --enable-new-dtags\tEnable new dynamic tags\n")); | |
65765700 | 1737 | fprintf (file, _(" --eh-frame-hdr\tCreate .eh_frame_hdr section\n")); |
ec38dd05 | 1738 | fprintf (file, _(" -z combreloc\t\tMerge dynamic relocs into one section and sort\n")); |
560e09e9 | 1739 | fprintf (file, _(" -z defs\t\tReport unresolved symbols in object files.\n")); |
9ee5e499 | 1740 | fprintf (file, _(" -z execstack\t\tMark executable as requiring executable stack\n")); |
f813923c HPN |
1741 | fprintf (file, _(" -z initfirst\t\tMark DSO to be initialized first at runtime\n")); |
1742 | fprintf (file, _(" -z interpose\t\tMark object to interpose all DSOs but executable\n")); | |
e0ee487b | 1743 | fprintf (file, _(" -z loadfltr\t\tMark object requiring immediate process\n")); |
aa713662 | 1744 | fprintf (file, _(" -z muldefs\t\tAllow multiple definitions\n")); |
146e7110 AM |
1745 | fprintf (file, _(" -z nocombreloc\tDon't merge dynamic relocs into one section\n")); |
1746 | fprintf (file, _(" -z nocopyreloc\tDon't create copy relocs\n")); | |
e0ee487b L |
1747 | fprintf (file, _(" -z nodefaultlib\tMark object not to use default search paths\n")); |
1748 | fprintf (file, _(" -z nodelete\t\tMark DSO non-deletable at runtime\n")); | |
f813923c HPN |
1749 | fprintf (file, _(" -z nodlopen\t\tMark DSO not available to dlopen\n")); |
1750 | fprintf (file, _(" -z nodump\t\tMark DSO not available to dldump\n")); | |
10b4329d | 1751 | fprintf (file, _(" -z noexecstack\tMark executable as not requiring executable stack\n")); |
8c37241b | 1752 | fprintf (file, _(" -z norelro\t\tDon't create RELRO program header\n")); |
e0ee487b | 1753 | fprintf (file, _(" -z now\t\tMark object non-lazy runtime binding\n")); |
73e87d70 | 1754 | fprintf (file, _(" -z origin\t\tMark object requiring immediate \$ORIGIN processing\n\t\t\t at runtime\n")); |
8c37241b | 1755 | fprintf (file, _(" -z relro\t\tCreate RELRO program header\n")); |
19e3be22 | 1756 | fprintf (file, _(" -z KEYWORD\t\tIgnored for Solaris compatibility\n")); |
e0ee487b L |
1757 | EOF |
1758 | fi | |
1759 | ||
1760 | if test -n "$PARSE_AND_LIST_OPTIONS" ; then | |
1761 | cat >>e${EMULATION_NAME}.c <<EOF | |
1762 | $PARSE_AND_LIST_OPTIONS | |
1763 | EOF | |
1764 | fi | |
1765 | ||
e0ee487b L |
1766 | cat >>e${EMULATION_NAME}.c <<EOF |
1767 | } | |
1768 | EOF | |
e0ee487b L |
1769 | |
1770 | if test -n "$PARSE_AND_LIST_EPILOGUE" ; then | |
1771 | cat >>e${EMULATION_NAME}.c <<EOF | |
1772 | $PARSE_AND_LIST_EPILOGUE | |
1773 | EOF | |
1774 | fi | |
41392f03 AM |
1775 | fi |
1776 | else | |
41392f03 | 1777 | cat >>e${EMULATION_NAME}.c <<EOF |
3bcf5557 AM |
1778 | #define gld${EMULATION_NAME}_add_options NULL |
1779 | #define gld${EMULATION_NAME}_handle_option NULL | |
41392f03 | 1780 | EOF |
4b209b22 | 1781 | if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then |
41392f03 | 1782 | cat >>e${EMULATION_NAME}.c <<EOF |
4b209b22 | 1783 | #define gld${EMULATION_NAME}_list_options NULL |
41392f03 AM |
1784 | EOF |
1785 | fi | |
1786 | fi | |
e0ee487b | 1787 | |
252b5132 RH |
1788 | cat >>e${EMULATION_NAME}.c <<EOF |
1789 | ||
60bcf0fa | 1790 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = |
252b5132 | 1791 | { |
41392f03 AM |
1792 | ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse}, |
1793 | ${LDEMUL_SYSLIB-syslib_default}, | |
1794 | ${LDEMUL_HLL-hll_default}, | |
1795 | ${LDEMUL_AFTER_PARSE-after_parse_default}, | |
1796 | ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open}, | |
1797 | ${LDEMUL_AFTER_ALLOCATION-after_allocation_default}, | |
1798 | ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default}, | |
1799 | ${LDEMUL_CHOOSE_TARGET-ldemul_default_target}, | |
1800 | ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation}, | |
1801 | ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script}, | |
252b5132 RH |
1802 | "${EMULATION_NAME}", |
1803 | "${OUTPUT_FORMAT}", | |
c56feb2b | 1804 | ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish}, |
41392f03 AM |
1805 | ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL}, |
1806 | ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive}, | |
1807 | ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}, | |
1808 | ${LDEMUL_SET_SYMBOLS-NULL}, | |
3bcf5557 AM |
1809 | ${LDEMUL_PARSE_ARGS-NULL}, |
1810 | gld${EMULATION_NAME}_add_options, | |
1811 | gld${EMULATION_NAME}_handle_option, | |
41392f03 | 1812 | ${LDEMUL_UNRECOGNIZED_FILE-NULL}, |
4b209b22 | 1813 | ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options}, |
4a43e768 | 1814 | ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols}, |
41392f03 | 1815 | ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL}, |
fac1652d | 1816 | ${LDEMUL_NEW_VERS_PATTERN-NULL} |
252b5132 RH |
1817 | }; |
1818 | EOF |