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 | |
92b93329 | 11 | fragment <<EOF |
252b5132 RH |
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, |
aa820537 AM |
16 | 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
17 | Free Software Foundation, Inc. | |
252b5132 RH |
18 | Written by Steve Chamberlain <sac@cygnus.com> |
19 | ELF support by Ian Lance Taylor <ian@cygnus.com> | |
20 | ||
f96b4a7b NC |
21 | This file is part of the GNU Binutils. |
22 | ||
23 | This program is free software; you can redistribute it and/or modify | |
24 | it under the terms of the GNU General Public License as published by | |
25 | the Free Software Foundation; either version 3 of the License, or | |
26 | (at your option) any later version. | |
27 | ||
28 | This program is distributed in the hope that it will be useful, | |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
31 | GNU General Public License for more details. | |
32 | ||
33 | You should have received a copy of the GNU General Public License | |
34 | along with this program; if not, write to the Free Software | |
35 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
36 | MA 02110-1301, USA. */ | |
252b5132 RH |
37 | |
38 | #define TARGET_IS_${EMULATION_NAME} | |
39 | ||
252b5132 | 40 | #include "sysdep.h" |
3db64b00 | 41 | #include "bfd.h" |
1c9acd94 | 42 | #include "libiberty.h" |
3882b010 | 43 | #include "safe-ctype.h" |
fcf65871 | 44 | #include "getopt.h" |
24382dca RM |
45 | #include "md5.h" |
46 | #include "sha1.h" | |
47 | #include <fcntl.h> | |
252b5132 RH |
48 | |
49 | #include "bfdlink.h" | |
50 | ||
51 | #include "ld.h" | |
52 | #include "ldmain.h" | |
252b5132 RH |
53 | #include "ldmisc.h" |
54 | #include "ldexp.h" | |
55 | #include "ldlang.h" | |
b71e2778 AM |
56 | #include "ldfile.h" |
57 | #include "ldemul.h" | |
df2a7313 | 58 | #include <ldgram.h> |
e0ee487b | 59 | #include "elf/common.h" |
ecca9871 | 60 | #include "elf-bfd.h" |
252b5132 | 61 | |
0c7a8e5a AM |
62 | /* Declare functions used by various EXTRA_EM_FILEs. */ |
63 | static void gld${EMULATION_NAME}_before_parse (void); | |
64 | static void gld${EMULATION_NAME}_after_open (void); | |
65 | static void gld${EMULATION_NAME}_before_allocation (void); | |
eaeb0a9d | 66 | static void gld${EMULATION_NAME}_after_allocation (void); |
c2edb4b8 | 67 | static lang_output_section_statement_type *gld${EMULATION_NAME}_place_orphan |
8a99a385 | 68 | (asection *, const char *, int); |
41392f03 AM |
69 | EOF |
70 | ||
d3989512 JJ |
71 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
72 | case ${target} in | |
711c0baa | 73 | *-*-linux-* | *-*-k*bsd*-*) |
92b93329 | 74 | fragment <<EOF |
0b48acfe | 75 | #ifdef HAVE_GLOB |
d3989512 | 76 | #include <glob.h> |
0b48acfe | 77 | #endif |
d3989512 JJ |
78 | EOF |
79 | ;; | |
80 | esac | |
81 | fi | |
82 | ||
41392f03 AM |
83 | # Import any needed special functions and/or overrides. |
84 | # | |
92b93329 | 85 | source_em ${srcdir}/emultempl/elf-generic.em |
41392f03 | 86 | if test -n "$EXTRA_EM_FILE" ; then |
92b93329 | 87 | source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em |
41392f03 AM |
88 | fi |
89 | ||
f813923c | 90 | # Functions in this file can be overridden by setting the LDEMUL_* shell |
41392f03 AM |
91 | # variables. If the name of the overriding function is the same as is |
92 | # defined in this file, then don't output this file's version. | |
93 | # If a different overriding name is given then output the standard function | |
94 | # as presumably it is called from the overriding function. | |
95 | # | |
96 | if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then | |
92b93329 | 97 | fragment <<EOF |
41392f03 | 98 | |
252b5132 | 99 | static void |
0c7a8e5a | 100 | gld${EMULATION_NAME}_before_parse (void) |
252b5132 | 101 | { |
5e2f1575 | 102 | ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`); |
b34976b6 AM |
103 | config.dynamic_link = ${DYNAMIC_LINK-TRUE}; |
104 | config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`; | |
252b5132 RH |
105 | } |
106 | ||
41392f03 AM |
107 | EOF |
108 | fi | |
109 | ||
4a43e768 | 110 | if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then |
92b93329 | 111 | fragment <<EOF |
4a43e768 AM |
112 | /* Handle as_needed DT_NEEDED. */ |
113 | ||
114 | static bfd_boolean | |
115 | gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry) | |
116 | { | |
96d56e9f | 117 | int link_class = 0; |
4a43e768 AM |
118 | |
119 | /* Tell the ELF linker that we don't want the output file to have a | |
120 | DT_NEEDED entry for this file, unless it is used to resolve | |
121 | references in a regular object. */ | |
e56f61be | 122 | if (entry->as_needed) |
96d56e9f | 123 | link_class = DYN_AS_NEEDED; |
e56f61be L |
124 | |
125 | /* Tell the ELF linker that we don't want the output file to have a | |
126 | DT_NEEDED entry for any dynamic library in DT_NEEDED tags from | |
127 | this file at all. */ | |
128 | if (!entry->add_needed) | |
96d56e9f | 129 | link_class |= DYN_NO_ADD_NEEDED; |
e56f61be | 130 | |
12b9ff6c L |
131 | if (entry->just_syms_flag |
132 | && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0) | |
133 | einfo (_("%P%F: --just-symbols may not be used on DSO: %B\n"), | |
134 | entry->the_bfd); | |
135 | ||
96d56e9f | 136 | if (!link_class |
e56f61be L |
137 | || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0) |
138 | return FALSE; | |
139 | ||
1e9cc1c2 NC |
140 | bfd_elf_set_dyn_lib_class (entry->the_bfd, |
141 | (enum dynamic_lib_link_class) link_class); | |
4a43e768 AM |
142 | |
143 | /* Continue on with normal load_symbols processing. */ | |
144 | return FALSE; | |
145 | } | |
146 | EOF | |
147 | fi | |
148 | ||
92b93329 | 149 | fragment <<EOF |
252b5132 RH |
150 | |
151 | /* These variables are required to pass information back and forth | |
152 | between after_open and check_needed and stat_needed and vercheck. */ | |
153 | ||
154 | static struct bfd_link_needed_list *global_needed; | |
155 | static struct stat global_stat; | |
e13629bc | 156 | static lang_input_statement_type *global_found; |
252b5132 | 157 | static struct bfd_link_needed_list *global_vercheck_needed; |
b34976b6 | 158 | static bfd_boolean global_vercheck_failed; |
252b5132 | 159 | |
252b5132 | 160 | |
04925e1e AM |
161 | /* On Linux, it's possible to have different versions of the same |
162 | shared library linked against different versions of libc. The | |
163 | dynamic linker somehow tags which libc version to use in | |
164 | /etc/ld.so.cache, and, based on the libc that it sees in the | |
165 | executable, chooses which version of the shared library to use. | |
252b5132 | 166 | |
04925e1e AM |
167 | We try to do a similar check here by checking whether this shared |
168 | library needs any other shared libraries which may conflict with | |
169 | libraries we have already included in the link. If it does, we | |
170 | skip it, and try to find another shared library farther on down the | |
171 | link path. | |
252b5132 | 172 | |
04925e1e AM |
173 | This is called via lang_for_each_input_file. |
174 | GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object | |
f813923c | 175 | which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find |
04925e1e | 176 | a conflicting version. */ |
252b5132 | 177 | |
04925e1e | 178 | static void |
0c7a8e5a | 179 | gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s) |
04925e1e | 180 | { |
1c9acd94 | 181 | const char *soname; |
04925e1e | 182 | struct bfd_link_needed_list *l; |
252b5132 | 183 | |
04925e1e AM |
184 | if (global_vercheck_failed) |
185 | return; | |
186 | if (s->the_bfd == NULL | |
187 | || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0) | |
188 | return; | |
252b5132 | 189 | |
04925e1e AM |
190 | soname = bfd_elf_get_dt_soname (s->the_bfd); |
191 | if (soname == NULL) | |
fed2999d | 192 | soname = lbasename (bfd_get_filename (s->the_bfd)); |
04925e1e AM |
193 | |
194 | for (l = global_vercheck_needed; l != NULL; l = l->next) | |
195 | { | |
196 | const char *suffix; | |
197 | ||
1c9acd94 | 198 | if (strcmp (soname, l->name) == 0) |
04925e1e AM |
199 | { |
200 | /* Probably can't happen, but it's an easy check. */ | |
201 | continue; | |
252b5132 RH |
202 | } |
203 | ||
04925e1e | 204 | if (strchr (l->name, '/') != NULL) |
252b5132 RH |
205 | continue; |
206 | ||
04925e1e AM |
207 | suffix = strstr (l->name, ".so."); |
208 | if (suffix == NULL) | |
209 | continue; | |
210 | ||
211 | suffix += sizeof ".so." - 1; | |
212 | ||
1c9acd94 | 213 | if (strncmp (soname, l->name, suffix - l->name) == 0) |
04925e1e AM |
214 | { |
215 | /* Here we know that S is a dynamic object FOO.SO.VER1, and | |
0c7a8e5a AM |
216 | the object we are considering needs a dynamic object |
217 | FOO.SO.VER2, and VER1 and VER2 are different. This | |
218 | appears to be a version mismatch, so we tell the caller | |
219 | to try a different version of this library. */ | |
b34976b6 | 220 | global_vercheck_failed = TRUE; |
04925e1e AM |
221 | return; |
222 | } | |
252b5132 RH |
223 | } |
224 | } | |
225 | ||
252b5132 | 226 | |
04925e1e AM |
227 | /* See if an input file matches a DT_NEEDED entry by running stat on |
228 | the file. */ | |
229 | ||
230 | static void | |
0c7a8e5a | 231 | gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s) |
252b5132 | 232 | { |
04925e1e AM |
233 | struct stat st; |
234 | const char *suffix; | |
235 | const char *soname; | |
252b5132 | 236 | |
e13629bc | 237 | if (global_found != NULL) |
04925e1e AM |
238 | return; |
239 | if (s->the_bfd == NULL) | |
240 | return; | |
e13629bc AM |
241 | |
242 | /* If this input file was an as-needed entry, and wasn't found to be | |
243 | needed at the stage it was linked, then don't say we have loaded it. */ | |
244 | if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0) | |
7cedef86 | 245 | return; |
04925e1e AM |
246 | |
247 | if (bfd_stat (s->the_bfd, &st) != 0) | |
252b5132 | 248 | { |
04925e1e AM |
249 | einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd); |
250 | return; | |
251 | } | |
252b5132 | 252 | |
9eabd475 ZW |
253 | /* Some operating systems, e.g. Windows, do not provide a meaningful |
254 | st_ino; they always set it to zero. (Windows does provide a | |
255 | meaningful st_dev.) Do not indicate a duplicate library in that | |
256 | case. While there is no guarantee that a system that provides | |
257 | meaningful inode numbers will never set st_ino to zero, this is | |
258 | merely an optimization, so we do not need to worry about false | |
259 | negatives. */ | |
04925e1e | 260 | if (st.st_dev == global_stat.st_dev |
9eabd475 ZW |
261 | && st.st_ino == global_stat.st_ino |
262 | && st.st_ino != 0) | |
04925e1e | 263 | { |
e13629bc | 264 | global_found = s; |
04925e1e AM |
265 | return; |
266 | } | |
252b5132 | 267 | |
04925e1e AM |
268 | /* We issue a warning if it looks like we are including two |
269 | different versions of the same shared library. For example, | |
270 | there may be a problem if -lc picks up libc.so.6 but some other | |
271 | shared library has a DT_NEEDED entry of libc.so.5. This is a | |
f813923c | 272 | heuristic test, and it will only work if the name looks like |
04925e1e AM |
273 | NAME.so.VERSION. FIXME: Depending on file names is error-prone. |
274 | If we really want to issue warnings about mixing version numbers | |
275 | of shared libraries, we need to find a better way. */ | |
252b5132 | 276 | |
04925e1e AM |
277 | if (strchr (global_needed->name, '/') != NULL) |
278 | return; | |
279 | suffix = strstr (global_needed->name, ".so."); | |
280 | if (suffix == NULL) | |
281 | return; | |
282 | suffix += sizeof ".so." - 1; | |
252b5132 | 283 | |
04925e1e AM |
284 | soname = bfd_elf_get_dt_soname (s->the_bfd); |
285 | if (soname == NULL) | |
fed2999d | 286 | soname = lbasename (s->filename); |
252b5132 | 287 | |
cde43e70 | 288 | if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0) |
04925e1e | 289 | einfo ("%P: warning: %s, needed by %B, may conflict with %s\n", |
1c9acd94 | 290 | global_needed->name, global_needed->by, soname); |
252b5132 RH |
291 | } |
292 | ||
e56f61be L |
293 | struct dt_needed |
294 | { | |
295 | bfd *by; | |
296 | const char *name; | |
297 | }; | |
04925e1e | 298 | |
252b5132 RH |
299 | /* This function is called for each possible name for a dynamic object |
300 | named by a DT_NEEDED entry. The FORCE parameter indicates whether | |
301 | to skip the check for a conflicting version. */ | |
302 | ||
b34976b6 | 303 | static bfd_boolean |
e56f61be L |
304 | gld${EMULATION_NAME}_try_needed (struct dt_needed *needed, |
305 | int force) | |
252b5132 RH |
306 | { |
307 | bfd *abfd; | |
e56f61be | 308 | const char *name = needed->name; |
ec4eb78a | 309 | const char *soname; |
96d56e9f | 310 | int link_class; |
252b5132 | 311 | |
f13a99db | 312 | abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd)); |
252b5132 | 313 | if (abfd == NULL) |
b34976b6 | 314 | return FALSE; |
252b5132 RH |
315 | if (! bfd_check_format (abfd, bfd_object)) |
316 | { | |
cde43e70 | 317 | bfd_close (abfd); |
b34976b6 | 318 | return FALSE; |
252b5132 RH |
319 | } |
320 | if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) | |
321 | { | |
cde43e70 | 322 | bfd_close (abfd); |
b34976b6 | 323 | return FALSE; |
252b5132 RH |
324 | } |
325 | ||
c4bb8067 | 326 | /* For DT_NEEDED, they have to match. */ |
f13a99db | 327 | if (abfd->xvec != link_info.output_bfd->xvec) |
c4bb8067 L |
328 | { |
329 | bfd_close (abfd); | |
b34976b6 | 330 | return FALSE; |
c4bb8067 L |
331 | } |
332 | ||
252b5132 RH |
333 | /* Check whether this object would include any conflicting library |
334 | versions. If FORCE is set, then we skip this check; we use this | |
335 | the second time around, if we couldn't find any compatible | |
336 | instance of the shared library. */ | |
337 | ||
338 | if (! force) | |
339 | { | |
340 | struct bfd_link_needed_list *needed; | |
341 | ||
342 | if (! bfd_elf_get_bfd_needed_list (abfd, &needed)) | |
343 | einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd); | |
344 | ||
345 | if (needed != NULL) | |
346 | { | |
347 | global_vercheck_needed = needed; | |
b34976b6 | 348 | global_vercheck_failed = FALSE; |
252b5132 RH |
349 | lang_for_each_input_file (gld${EMULATION_NAME}_vercheck); |
350 | if (global_vercheck_failed) | |
351 | { | |
cde43e70 | 352 | bfd_close (abfd); |
b34976b6 | 353 | /* Return FALSE to force the caller to move on to try |
0c7a8e5a | 354 | another file on the search path. */ |
b34976b6 | 355 | return FALSE; |
252b5132 RH |
356 | } |
357 | ||
358 | /* But wait! It gets much worse. On Linux, if a shared | |
0c7a8e5a AM |
359 | library does not use libc at all, we are supposed to skip |
360 | it the first time around in case we encounter a shared | |
361 | library later on with the same name which does use the | |
362 | version of libc that we want. This is much too horrible | |
363 | to use on any system other than Linux. */ | |
252b5132 RH |
364 | |
365 | EOF | |
366 | case ${target} in | |
711c0baa | 367 | *-*-linux-* | *-*-k*bsd*-*) |
92b93329 | 368 | fragment <<EOF |
252b5132 RH |
369 | { |
370 | struct bfd_link_needed_list *l; | |
371 | ||
372 | for (l = needed; l != NULL; l = l->next) | |
0112cd26 | 373 | if (CONST_STRNEQ (l->name, "libc.so")) |
252b5132 RH |
374 | break; |
375 | if (l == NULL) | |
376 | { | |
cde43e70 | 377 | bfd_close (abfd); |
b34976b6 | 378 | return FALSE; |
252b5132 RH |
379 | } |
380 | } | |
381 | ||
382 | EOF | |
383 | ;; | |
384 | esac | |
92b93329 | 385 | fragment <<EOF |
252b5132 RH |
386 | } |
387 | } | |
388 | ||
389 | /* We've found a dynamic object matching the DT_NEEDED entry. */ | |
390 | ||
04925e1e AM |
391 | /* We have already checked that there is no other input file of the |
392 | same name. We must now check again that we are not including the | |
393 | same file twice. We need to do this because on many systems | |
394 | libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will | |
395 | reference libc.so.1. If we have already included libc.so, we | |
396 | don't want to include libc.so.1 if they are the same file, and we | |
397 | can only check that using stat. */ | |
398 | ||
399 | if (bfd_stat (abfd, &global_stat) != 0) | |
400 | einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd); | |
ec4eb78a L |
401 | |
402 | /* First strip off everything before the last '/'. */ | |
fed2999d | 403 | soname = lbasename (abfd->filename); |
ec4eb78a L |
404 | |
405 | if (trace_file_tries) | |
406 | info_msg (_("found %s at %s\n"), soname, name); | |
407 | ||
e13629bc | 408 | global_found = NULL; |
04925e1e | 409 | lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed); |
e13629bc | 410 | if (global_found != NULL) |
04925e1e | 411 | { |
b34976b6 | 412 | /* Return TRUE to indicate that we found the file, even though |
0c7a8e5a | 413 | we aren't going to do anything with it. */ |
b34976b6 | 414 | return TRUE; |
04925e1e AM |
415 | } |
416 | ||
4a43e768 AM |
417 | /* Specify the soname to use. */ |
418 | bfd_elf_set_dt_needed_name (abfd, soname); | |
04925e1e | 419 | |
4a43e768 AM |
420 | /* Tell the ELF linker that we don't want the output file to have a |
421 | DT_NEEDED entry for this file, unless it is used to resolve | |
422 | references in a regular object. */ | |
96d56e9f | 423 | link_class = DYN_DT_NEEDED; |
e56f61be L |
424 | |
425 | /* Tell the ELF linker that we don't want the output file to have a | |
426 | DT_NEEDED entry for this file at all if the entry is from a file | |
427 | with DYN_NO_ADD_NEEDED. */ | |
7e9f0867 AM |
428 | if (needed->by != NULL |
429 | && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0) | |
96d56e9f | 430 | link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED; |
e56f61be | 431 | |
1e9cc1c2 | 432 | bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class); |
04925e1e AM |
433 | |
434 | /* Add this file into the symbol table. */ | |
435 | if (! bfd_link_add_symbols (abfd, &link_info)) | |
436 | einfo ("%F%B: could not read symbols: %E\n", abfd); | |
437 | ||
b34976b6 | 438 | return TRUE; |
04925e1e AM |
439 | } |
440 | ||
441 | ||
442 | /* Search for a needed file in a path. */ | |
443 | ||
b34976b6 | 444 | static bfd_boolean |
e56f61be L |
445 | gld${EMULATION_NAME}_search_needed (const char *path, |
446 | struct dt_needed *n, int force) | |
04925e1e AM |
447 | { |
448 | const char *s; | |
e56f61be | 449 | const char *name = n->name; |
04925e1e | 450 | size_t len; |
e56f61be | 451 | struct dt_needed needed; |
04925e1e | 452 | |
cc5ce052 | 453 | if (name[0] == '/') |
e56f61be | 454 | return gld${EMULATION_NAME}_try_needed (n, force); |
cc5ce052 | 455 | |
04925e1e | 456 | if (path == NULL || *path == '\0') |
b34976b6 | 457 | return FALSE; |
e56f61be L |
458 | |
459 | needed.by = n->by; | |
460 | needed.name = n->name; | |
461 | ||
04925e1e AM |
462 | len = strlen (name); |
463 | while (1) | |
464 | { | |
465 | char *filename, *sset; | |
466 | ||
c76308d2 | 467 | s = strchr (path, config.rpath_separator); |
04925e1e AM |
468 | if (s == NULL) |
469 | s = path + strlen (path); | |
470 | ||
471 | filename = (char *) xmalloc (s - path + len + 2); | |
472 | if (s == path) | |
473 | sset = filename; | |
474 | else | |
475 | { | |
476 | memcpy (filename, path, s - path); | |
477 | filename[s - path] = '/'; | |
478 | sset = filename + (s - path) + 1; | |
479 | } | |
480 | strcpy (sset, name); | |
481 | ||
e56f61be L |
482 | needed.name = filename; |
483 | if (gld${EMULATION_NAME}_try_needed (&needed, force)) | |
b34976b6 | 484 | return TRUE; |
04925e1e AM |
485 | |
486 | free (filename); | |
487 | ||
488 | if (*s == '\0') | |
489 | break; | |
490 | path = s + 1; | |
491 | } | |
492 | ||
b34976b6 | 493 | return FALSE; |
04925e1e AM |
494 | } |
495 | ||
496 | EOF | |
9c8ebd6a | 497 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
92b93329 | 498 | fragment <<EOF |
9c8ebd6a | 499 | |
c76308d2 RS |
500 | /* Add the sysroot to every entry in a path separated by |
501 | config.rpath_separator. */ | |
9c8ebd6a DJ |
502 | |
503 | static char * | |
0c7a8e5a | 504 | gld${EMULATION_NAME}_add_sysroot (const char *path) |
9c8ebd6a DJ |
505 | { |
506 | int len, colons, i; | |
507 | char *ret, *p; | |
508 | ||
509 | len = strlen (path); | |
510 | colons = 0; | |
511 | i = 0; | |
512 | while (path[i]) | |
c76308d2 | 513 | if (path[i++] == config.rpath_separator) |
9c8ebd6a DJ |
514 | colons++; |
515 | ||
516 | if (path[i]) | |
517 | colons++; | |
518 | ||
42644a89 | 519 | len = len + (colons + 1) * strlen (ld_sysroot); |
9c8ebd6a DJ |
520 | ret = xmalloc (len + 1); |
521 | strcpy (ret, ld_sysroot); | |
522 | p = ret + strlen (ret); | |
523 | i = 0; | |
524 | while (path[i]) | |
c76308d2 | 525 | if (path[i] == config.rpath_separator) |
9c8ebd6a | 526 | { |
0c7a8e5a | 527 | *p++ = path[i++]; |
9c8ebd6a DJ |
528 | strcpy (p, ld_sysroot); |
529 | p = p + strlen (p); | |
9c8ebd6a DJ |
530 | } |
531 | else | |
532 | *p++ = path[i++]; | |
533 | ||
534 | *p = 0; | |
535 | return ret; | |
536 | } | |
04925e1e | 537 | |
3dc16cab DJ |
538 | EOF |
539 | case ${target} in | |
ebe1fac1 | 540 | *-*-freebsd* | *-*-dragonfly*) |
92b93329 | 541 | fragment <<EOF |
ebe1fac1 NC |
542 | /* Read the system search path the FreeBSD way rather than the Linux way. */ |
543 | #ifdef HAVE_ELF_HINTS_H | |
544 | #include <elf-hints.h> | |
545 | #else | |
546 | #include "elf-hints-local.h" | |
547 | #endif | |
548 | ||
549 | static bfd_boolean | |
550 | gld${EMULATION_NAME}_check_ld_elf_hints (const char *name, int force) | |
551 | { | |
552 | static bfd_boolean initialized; | |
553 | static char *ld_elf_hints; | |
554 | struct dt_needed needed; | |
555 | ||
556 | if (!initialized) | |
557 | { | |
558 | FILE *f; | |
559 | char *tmppath; | |
560 | ||
ff7a0acf | 561 | tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL); |
ebe1fac1 NC |
562 | f = fopen (tmppath, FOPEN_RB); |
563 | free (tmppath); | |
564 | if (f != NULL) | |
565 | { | |
566 | struct elfhints_hdr hdr; | |
567 | ||
568 | if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr) | |
569 | && hdr.magic == ELFHINTS_MAGIC | |
570 | && hdr.version == 1) | |
571 | { | |
572 | if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1) | |
573 | { | |
574 | char *b; | |
575 | ||
576 | b = xmalloc (hdr.dirlistlen + 1); | |
577 | if (fread (b, 1, hdr.dirlistlen + 1, f) == | |
578 | hdr.dirlistlen + 1) | |
579 | ld_elf_hints = gld${EMULATION_NAME}_add_sysroot (b); | |
e13629bc | 580 | |
ebe1fac1 NC |
581 | free (b); |
582 | } | |
583 | } | |
584 | fclose (f); | |
585 | } | |
586 | ||
587 | initialized = TRUE; | |
588 | } | |
589 | ||
590 | if (ld_elf_hints == NULL) | |
591 | return FALSE; | |
592 | ||
593 | needed.by = NULL; | |
594 | needed.name = name; | |
595 | return gld${EMULATION_NAME}_search_needed (ld_elf_hints, & needed, | |
e13629bc | 596 | force); |
ebe1fac1 NC |
597 | } |
598 | EOF | |
599 | # FreeBSD | |
600 | ;; | |
601 | ||
711c0baa | 602 | *-*-linux-* | *-*-k*bsd*-*) |
92b93329 | 603 | fragment <<EOF |
04925e1e AM |
604 | /* For a native linker, check the file /etc/ld.so.conf for directories |
605 | in which we may find shared libraries. /etc/ld.so.conf is really | |
482e8b32 | 606 | only meaningful on Linux. */ |
04925e1e | 607 | |
d3989512 | 608 | struct gld${EMULATION_NAME}_ld_so_conf |
04925e1e | 609 | { |
d3989512 JJ |
610 | char *path; |
611 | size_t len, alloc; | |
612 | }; | |
04925e1e | 613 | |
dfcffada | 614 | static bfd_boolean |
d3989512 JJ |
615 | gld${EMULATION_NAME}_parse_ld_so_conf |
616 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename); | |
617 | ||
618 | static void | |
619 | gld${EMULATION_NAME}_parse_ld_so_conf_include | |
620 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename, | |
621 | const char *pattern) | |
622 | { | |
623 | char *newp = NULL; | |
0b48acfe | 624 | #ifdef HAVE_GLOB |
d3989512 | 625 | glob_t gl; |
0b48acfe | 626 | #endif |
d3989512 JJ |
627 | |
628 | if (pattern[0] != '/') | |
04925e1e | 629 | { |
d3989512 JJ |
630 | char *p = strrchr (filename, '/'); |
631 | size_t patlen = strlen (pattern) + 1; | |
04925e1e | 632 | |
d3989512 JJ |
633 | newp = xmalloc (p - filename + 1 + patlen); |
634 | memcpy (newp, filename, p - filename + 1); | |
635 | memcpy (newp + (p - filename + 1), pattern, patlen); | |
636 | pattern = newp; | |
637 | } | |
04925e1e | 638 | |
0b48acfe | 639 | #ifdef HAVE_GLOB |
d3989512 JJ |
640 | if (glob (pattern, 0, NULL, &gl) == 0) |
641 | { | |
642 | size_t i; | |
643 | ||
644 | for (i = 0; i < gl.gl_pathc; ++i) | |
645 | gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]); | |
646 | globfree (&gl); | |
647 | } | |
0b48acfe MM |
648 | #else |
649 | /* If we do not have glob, treat the pattern as a literal filename. */ | |
650 | gld${EMULATION_NAME}_parse_ld_so_conf (info, pattern); | |
651 | #endif | |
d3989512 JJ |
652 | |
653 | if (newp) | |
654 | free (newp); | |
655 | } | |
656 | ||
dfcffada | 657 | static bfd_boolean |
d3989512 JJ |
658 | gld${EMULATION_NAME}_parse_ld_so_conf |
659 | (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename) | |
660 | { | |
661 | FILE *f = fopen (filename, FOPEN_RT); | |
563f4125 JJ |
662 | char *line; |
663 | size_t linelen; | |
d3989512 JJ |
664 | |
665 | if (f == NULL) | |
dfcffada | 666 | return FALSE; |
d3989512 | 667 | |
563f4125 JJ |
668 | linelen = 256; |
669 | line = xmalloc (linelen); | |
670 | do | |
d3989512 | 671 | { |
563f4125 JJ |
672 | char *p = line, *q; |
673 | ||
674 | /* Normally this would use getline(3), but we need to be portable. */ | |
675 | while ((q = fgets (p, linelen - (p - line), f)) != NULL | |
676 | && strlen (q) == linelen - (p - line) - 1 | |
677 | && line[linelen - 2] != '\n') | |
678 | { | |
679 | line = xrealloc (line, 2 * linelen); | |
680 | p = line + linelen - 1; | |
681 | linelen += linelen; | |
682 | } | |
683 | ||
684 | if (q == NULL && p == line) | |
685 | break; | |
d3989512 JJ |
686 | |
687 | p = strchr (line, '\n'); | |
688 | if (p) | |
689 | *p = '\0'; | |
690 | ||
691 | /* Because the file format does not know any form of quoting we | |
692 | can search forward for the next '#' character and if found | |
693 | make it terminating the line. */ | |
694 | p = strchr (line, '#'); | |
695 | if (p) | |
696 | *p = '\0'; | |
697 | ||
698 | /* Remove leading whitespace. NUL is no whitespace character. */ | |
699 | p = line; | |
700 | while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v') | |
701 | ++p; | |
702 | ||
703 | /* If the line is blank it is ignored. */ | |
704 | if (p[0] == '\0') | |
705 | continue; | |
04925e1e | 706 | |
0112cd26 | 707 | if (CONST_STRNEQ (p, "include") && (p[7] == ' ' || p[7] == '\t')) |
d3989512 JJ |
708 | { |
709 | char *dir, c; | |
710 | p += 8; | |
711 | do | |
04925e1e | 712 | { |
d3989512 JJ |
713 | while (*p == ' ' || *p == '\t') |
714 | ++p; | |
04925e1e | 715 | |
d3989512 JJ |
716 | if (*p == '\0') |
717 | break; | |
252b5132 | 718 | |
d3989512 JJ |
719 | dir = p; |
720 | ||
721 | while (*p != ' ' && *p != '\t' && *p) | |
722 | ++p; | |
723 | ||
724 | c = *p; | |
725 | *p++ = '\0'; | |
726 | if (dir[0] != '\0') | |
727 | gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename, | |
728 | dir); | |
729 | } | |
730 | while (c != '\0'); | |
731 | } | |
732 | else | |
733 | { | |
734 | char *dir = p; | |
735 | while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f' | |
736 | && *p != '\r' && *p != '\v') | |
737 | ++p; | |
738 | ||
739 | while (p != dir && p[-1] == '/') | |
740 | --p; | |
741 | if (info->path == NULL) | |
742 | { | |
743 | info->alloc = p - dir + 1 + 256; | |
744 | info->path = xmalloc (info->alloc); | |
745 | info->len = 0; | |
746 | } | |
04925e1e AM |
747 | else |
748 | { | |
d3989512 JJ |
749 | if (info->len + 1 + (p - dir) >= info->alloc) |
750 | { | |
751 | info->alloc += p - dir + 256; | |
752 | info->path = xrealloc (info->path, info->alloc); | |
753 | } | |
c76308d2 | 754 | info->path[info->len++] = config.rpath_separator; |
04925e1e | 755 | } |
d3989512 JJ |
756 | memcpy (info->path + info->len, dir, p - dir); |
757 | info->len += p - dir; | |
758 | info->path[info->len] = '\0'; | |
759 | } | |
760 | } | |
563f4125 | 761 | while (! feof (f)); |
d3989512 JJ |
762 | free (line); |
763 | fclose (f); | |
dfcffada | 764 | return TRUE; |
d3989512 | 765 | } |
252b5132 | 766 | |
d3989512 JJ |
767 | static bfd_boolean |
768 | gld${EMULATION_NAME}_check_ld_so_conf (const char *name, int force) | |
769 | { | |
770 | static bfd_boolean initialized; | |
771 | static char *ld_so_conf; | |
772 | struct dt_needed needed; | |
252b5132 | 773 | |
d3989512 JJ |
774 | if (! initialized) |
775 | { | |
776 | char *tmppath; | |
777 | struct gld${EMULATION_NAME}_ld_so_conf info; | |
9c8ebd6a | 778 | |
d3989512 JJ |
779 | info.path = NULL; |
780 | info.len = info.alloc = 0; | |
ff7a0acf AM |
781 | tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf", |
782 | (const char *) NULL); | |
dfcffada AM |
783 | if (!gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath)) |
784 | { | |
785 | free (tmppath); | |
ff7a0acf AM |
786 | tmppath = concat (ld_sysroot, "/etc/ld.so.conf", |
787 | (const char *) NULL); | |
dfcffada AM |
788 | gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath); |
789 | } | |
d3989512 | 790 | free (tmppath); |
dfcffada | 791 | |
d3989512 JJ |
792 | if (info.path) |
793 | { | |
794 | char *d = gld${EMULATION_NAME}_add_sysroot (info.path); | |
795 | free (info.path); | |
796 | ld_so_conf = d; | |
04925e1e | 797 | } |
b34976b6 | 798 | initialized = TRUE; |
04925e1e | 799 | } |
49e56c49 | 800 | |
04925e1e | 801 | if (ld_so_conf == NULL) |
b34976b6 | 802 | return FALSE; |
252b5132 | 803 | |
e56f61be L |
804 | |
805 | needed.by = NULL; | |
806 | needed.name = name; | |
807 | return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force); | |
252b5132 RH |
808 | } |
809 | ||
04925e1e | 810 | EOF |
9c8ebd6a DJ |
811 | # Linux |
812 | ;; | |
04925e1e AM |
813 | esac |
814 | fi | |
92b93329 | 815 | fragment <<EOF |
04925e1e | 816 | |
252b5132 RH |
817 | /* See if an input file matches a DT_NEEDED entry by name. */ |
818 | ||
819 | static void | |
0c7a8e5a | 820 | gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s) |
252b5132 | 821 | { |
e13629bc AM |
822 | const char *soname; |
823 | ||
824 | /* Stop looking if we've found a loaded lib. */ | |
825 | if (global_found != NULL | |
826 | && (bfd_elf_get_dyn_lib_class (global_found->the_bfd) | |
827 | & DYN_AS_NEEDED) == 0) | |
252b5132 RH |
828 | return; |
829 | ||
e13629bc AM |
830 | if (s->filename == NULL || s->the_bfd == NULL) |
831 | return; | |
832 | ||
833 | /* Don't look for a second non-loaded as-needed lib. */ | |
834 | if (global_found != NULL | |
835 | && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0) | |
7cedef86 AM |
836 | return; |
837 | ||
e13629bc | 838 | if (strcmp (s->filename, global_needed->name) == 0) |
252b5132 | 839 | { |
e13629bc AM |
840 | global_found = s; |
841 | return; | |
842 | } | |
577a0623 | 843 | |
e13629bc AM |
844 | if (s->search_dirs_flag) |
845 | { | |
846 | const char *f = strrchr (s->filename, '/'); | |
847 | if (f != NULL | |
848 | && strcmp (f + 1, global_needed->name) == 0) | |
577a0623 | 849 | { |
e13629bc | 850 | global_found = s; |
577a0623 AM |
851 | return; |
852 | } | |
252b5132 RH |
853 | } |
854 | ||
e13629bc AM |
855 | soname = bfd_elf_get_dt_soname (s->the_bfd); |
856 | if (soname != NULL | |
857 | && strcmp (soname, global_needed->name) == 0) | |
252b5132 | 858 | { |
e13629bc AM |
859 | global_found = s; |
860 | return; | |
252b5132 | 861 | } |
252b5132 RH |
862 | } |
863 | ||
41392f03 AM |
864 | EOF |
865 | ||
866 | if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then | |
92b93329 | 867 | fragment <<EOF |
04925e1e | 868 | |
24382dca RM |
869 | static bfd_size_type |
870 | gld${EMULATION_NAME}_id_note_section_size (bfd *abfd, | |
871 | struct bfd_link_info *link_info) | |
872 | { | |
873 | const char *style = link_info->emit_note_gnu_build_id; | |
874 | bfd_size_type size; | |
875 | ||
876 | abfd = abfd; | |
877 | ||
878 | size = offsetof (Elf_External_Note, name[sizeof "GNU"]); | |
879 | size = (size + 3) & -(bfd_size_type) 4; | |
880 | ||
881 | if (!strcmp (style, "md5") || !strcmp (style, "uuid")) | |
882 | size += 128 / 8; | |
883 | else if (!strcmp (style, "sha1")) | |
884 | size += 160 / 8; | |
885 | else if (!strncmp (style, "0x", 2)) | |
886 | { | |
887 | /* ID is in string form (hex). Convert to bits. */ | |
888 | const char *id = style + 2; | |
889 | do | |
890 | { | |
891 | if (ISXDIGIT (id[0]) && ISXDIGIT (id[1])) | |
892 | { | |
893 | ++size; | |
894 | id += 2; | |
895 | } | |
896 | else if (*id == '-' || *id == ':') | |
897 | ++id; | |
898 | else | |
899 | { | |
900 | size = 0; | |
901 | break; | |
902 | } | |
903 | } while (*id != '\0'); | |
904 | } | |
905 | else | |
906 | size = 0; | |
907 | ||
908 | return size; | |
909 | } | |
910 | ||
911 | static unsigned char | |
912 | read_hex (const char xdigit) | |
913 | { | |
914 | if (ISDIGIT (xdigit)) | |
915 | return xdigit - '0'; | |
916 | if (ISUPPER (xdigit)) | |
917 | return xdigit - 'A' + 0xa; | |
918 | if (ISLOWER (xdigit)) | |
919 | return xdigit - 'a' + 0xa; | |
920 | abort (); | |
921 | return 0; | |
922 | } | |
923 | ||
924 | struct build_id_info | |
925 | { | |
926 | const char *style; | |
927 | asection *sec; | |
928 | }; | |
929 | ||
930 | static bfd_boolean | |
931 | gld${EMULATION_NAME}_write_build_id_section (bfd *abfd) | |
932 | { | |
933 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1e9cc1c2 | 934 | struct build_id_info *info = (struct build_id_info *) |
24382dca RM |
935 | elf_tdata (abfd)->after_write_object_contents_info; |
936 | asection *asec; | |
937 | Elf_Internal_Shdr *i_shdr; | |
938 | unsigned char *contents, *id_bits; | |
939 | bfd_size_type size; | |
940 | Elf_External_Note *e_note; | |
941 | ||
942 | asec = info->sec; | |
83e4970b | 943 | if (bfd_is_abs_section (asec->output_section)) |
24382dca | 944 | { |
83e4970b AM |
945 | einfo (_("%P: warning: .note.gnu.build-id section discarded," |
946 | " --build-id ignored.\n")); | |
947 | return TRUE; | |
24382dca RM |
948 | } |
949 | i_shdr = &elf_section_data (asec->output_section)->this_hdr; | |
950 | ||
951 | if (i_shdr->contents == NULL) | |
952 | { | |
b2ff8454 | 953 | if (asec->contents == NULL) |
1e9cc1c2 | 954 | asec->contents = (unsigned char *) xmalloc (asec->size); |
b2ff8454 | 955 | contents = asec->contents; |
24382dca | 956 | } |
b2ff8454 RM |
957 | else |
958 | contents = i_shdr->contents + asec->output_offset; | |
24382dca | 959 | |
1e9cc1c2 | 960 | e_note = (Elf_External_Note *) contents; |
24382dca RM |
961 | size = offsetof (Elf_External_Note, name[sizeof "GNU"]); |
962 | size = (size + 3) & -(bfd_size_type) 4; | |
963 | id_bits = contents + size; | |
964 | size = asec->size - size; | |
965 | ||
966 | bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz); | |
967 | bfd_h_put_32 (abfd, size, &e_note->descsz); | |
968 | bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type); | |
969 | memcpy (e_note->name, "GNU", sizeof "GNU"); | |
970 | ||
971 | if (!strcmp (info->style, "md5")) | |
972 | { | |
973 | struct md5_ctx ctx; | |
974 | md5_init_ctx (&ctx); | |
975 | if (bed->s->checksum_contents (abfd, | |
976 | (void (*) (const void *, size_t, void *)) | |
977 | &md5_process_bytes, | |
978 | &ctx)) | |
979 | md5_finish_ctx (&ctx, id_bits); | |
980 | else | |
981 | return FALSE; | |
982 | } | |
983 | else if (!strcmp (info->style, "sha1")) | |
984 | { | |
985 | struct sha1_ctx ctx; | |
986 | sha1_init_ctx (&ctx); | |
987 | if (bed->s->checksum_contents (abfd, | |
988 | (void (*) (const void *, size_t, void *)) | |
989 | &sha1_process_bytes, | |
990 | &ctx)) | |
991 | sha1_finish_ctx (&ctx, id_bits); | |
992 | else | |
993 | return FALSE; | |
994 | } | |
995 | else if (!strcmp (info->style, "uuid")) | |
996 | { | |
997 | int n; | |
998 | int fd = open ("/dev/urandom", O_RDONLY); | |
999 | if (fd < 0) | |
1000 | return FALSE; | |
1001 | n = read (fd, id_bits, size); | |
1002 | close (fd); | |
1003 | if (n < (int) size) | |
1004 | return FALSE; | |
1005 | } | |
1006 | else if (!strncmp (info->style, "0x", 2)) | |
1007 | { | |
1008 | /* ID is in string form (hex). Convert to bits. */ | |
1009 | const char *id = info->style + 2; | |
1010 | size_t n = 0; | |
1011 | do | |
1012 | { | |
1013 | if (ISXDIGIT (id[0]) && ISXDIGIT (id[1])) | |
1014 | { | |
1015 | id_bits[n] = read_hex (*id++) << 4; | |
1016 | id_bits[n++] |= read_hex (*id++); | |
1017 | } | |
1018 | else if (*id == '-' || *id == ':') | |
1019 | ++id; | |
1020 | else | |
1021 | abort (); /* Should have been validated earlier. */ | |
1022 | } while (*id != '\0'); | |
1023 | } | |
1024 | else | |
1025 | abort (); /* Should have been validated earlier. */ | |
1026 | ||
b2ff8454 RM |
1027 | size = asec->size; |
1028 | return (bfd_seek (abfd, | |
1029 | i_shdr->sh_offset + asec->output_offset, SEEK_SET) == 0 | |
1030 | && bfd_bwrite (contents, size, abfd) == size); | |
24382dca RM |
1031 | } |
1032 | ||
1033 | ||
04925e1e | 1034 | /* This is called after all the input files have been opened. */ |
252b5132 RH |
1035 | |
1036 | static void | |
0c7a8e5a | 1037 | gld${EMULATION_NAME}_after_open (void) |
252b5132 | 1038 | { |
04925e1e | 1039 | struct bfd_link_needed_list *needed, *l; |
d10e7fcc AM |
1040 | struct elf_link_hash_table *htab; |
1041 | ||
1042 | htab = elf_hash_table (&link_info); | |
1043 | if (!is_elf_hash_table (htab)) | |
1044 | return; | |
252b5132 | 1045 | |
c0065db7 RM |
1046 | if (link_info.emit_note_gnu_build_id) |
1047 | { | |
1048 | bfd *abfd; | |
1049 | asection *s; | |
1050 | bfd_size_type size; | |
1051 | ||
1052 | abfd = link_info.input_bfds; | |
1053 | ||
e9e0a58e | 1054 | if (abfd == NULL) |
c0065db7 | 1055 | { |
e9e0a58e NC |
1056 | /* PR 10555: If there are no input files do not |
1057 | try to create a .note.gnu-build-id section. */ | |
c0065db7 RM |
1058 | free (link_info.emit_note_gnu_build_id); |
1059 | link_info.emit_note_gnu_build_id = NULL; | |
1060 | } | |
1061 | else | |
1062 | { | |
e9e0a58e NC |
1063 | size = gld${EMULATION_NAME}_id_note_section_size (abfd, &link_info); |
1064 | if (size == 0) | |
c0065db7 | 1065 | { |
e9e0a58e NC |
1066 | einfo ("%P: warning: unrecognized --build-id style ignored.\n"); |
1067 | free (link_info.emit_note_gnu_build_id); | |
1068 | link_info.emit_note_gnu_build_id = NULL; | |
c0065db7 RM |
1069 | } |
1070 | else | |
1071 | { | |
e9e0a58e NC |
1072 | s = bfd_make_section_with_flags (abfd, ".note.gnu.build-id", |
1073 | SEC_ALLOC | SEC_LOAD | |
1074 | | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
1075 | | SEC_READONLY | SEC_DATA); | |
1076 | if (s != NULL && bfd_set_section_alignment (abfd, s, 2)) | |
1077 | { | |
1078 | struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd); | |
1e9cc1c2 NC |
1079 | struct build_id_info *b = |
1080 | (struct build_id_info *) xmalloc (sizeof *b); | |
e9e0a58e NC |
1081 | |
1082 | b->style = link_info.emit_note_gnu_build_id; | |
1083 | b->sec = s; | |
1084 | elf_section_type (s) = SHT_NOTE; | |
1085 | s->size = size; | |
1086 | t->after_write_object_contents | |
1087 | = &gld${EMULATION_NAME}_write_build_id_section; | |
1088 | t->after_write_object_contents_info = b; | |
1089 | } | |
1090 | else | |
1091 | { | |
1092 | einfo ("%P: warning: Cannot create .note.gnu.build-id section," | |
1093 | " --build-id ignored.\n"); | |
1094 | free (link_info.emit_note_gnu_build_id); | |
1095 | link_info.emit_note_gnu_build_id = NULL; | |
1096 | } | |
c0065db7 RM |
1097 | } |
1098 | } | |
1099 | } | |
1100 | ||
d10e7fcc AM |
1101 | if (link_info.relocatable) |
1102 | return; | |
1103 | ||
eb3d5f3b | 1104 | if (link_info.eh_frame_hdr |
d10e7fcc | 1105 | && !link_info.traditional_format) |
eb3d5f3b | 1106 | { |
d10e7fcc AM |
1107 | bfd *abfd; |
1108 | asection *s; | |
eb3d5f3b | 1109 | |
d10e7fcc | 1110 | for (abfd = link_info.input_bfds; abfd; abfd = abfd->link_next) |
eb3d5f3b | 1111 | { |
d10e7fcc AM |
1112 | s = bfd_get_section_by_name (abfd, ".eh_frame"); |
1113 | if (s && s->size > 8 && !bfd_is_abs_section (s->output_section)) | |
1114 | break; | |
1115 | } | |
1116 | if (abfd) | |
1117 | { | |
1118 | const struct elf_backend_data *bed; | |
1119 | ||
1120 | bed = get_elf_backend_data (abfd); | |
1121 | s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", | |
1122 | bed->dynamic_sec_flags | |
1123 | | SEC_READONLY); | |
1124 | if (s != NULL | |
1125 | && bfd_set_section_alignment (abfd, s, 2)) | |
1126 | htab->eh_info.hdr_sec = s; | |
1127 | else | |
1128 | einfo ("%P: warning: Cannot create .eh_frame_hdr section," | |
1129 | " --eh-frame-hdr ignored.\n"); | |
eb3d5f3b JB |
1130 | } |
1131 | } | |
1132 | ||
04925e1e AM |
1133 | /* Get the list of files which appear in DT_NEEDED entries in |
1134 | dynamic objects included in the link (often there will be none). | |
1135 | For each such file, we want to track down the corresponding | |
1136 | library, and include the symbol table in the link. This is what | |
1137 | the runtime dynamic linker will do. Tracking the files down here | |
1138 | permits one dynamic object to include another without requiring | |
1139 | special action by the person doing the link. Note that the | |
1140 | needed list can actually grow while we are stepping through this | |
1141 | loop. */ | |
d10e7fcc AM |
1142 | if (!link_info.executable) |
1143 | return; | |
f13a99db | 1144 | needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info); |
04925e1e | 1145 | for (l = needed; l != NULL; l = l->next) |
252b5132 | 1146 | { |
04925e1e | 1147 | struct bfd_link_needed_list *ll; |
e56f61be | 1148 | struct dt_needed n, nn; |
04925e1e | 1149 | int force; |
252b5132 | 1150 | |
7e9f0867 AM |
1151 | /* If the lib that needs this one was --as-needed and wasn't |
1152 | found to be needed, then this lib isn't needed either. */ | |
1153 | if (l->by != NULL | |
1154 | && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0) | |
1155 | continue; | |
1156 | ||
04925e1e AM |
1157 | /* If we've already seen this file, skip it. */ |
1158 | for (ll = needed; ll != l; ll = ll->next) | |
7e9f0867 AM |
1159 | if ((ll->by == NULL |
1160 | || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0) | |
1161 | && strcmp (ll->name, l->name) == 0) | |
04925e1e AM |
1162 | break; |
1163 | if (ll != l) | |
1164 | continue; | |
252b5132 | 1165 | |
04925e1e AM |
1166 | /* See if this file was included in the link explicitly. */ |
1167 | global_needed = l; | |
e13629bc | 1168 | global_found = NULL; |
04925e1e | 1169 | lang_for_each_input_file (gld${EMULATION_NAME}_check_needed); |
e13629bc AM |
1170 | if (global_found != NULL |
1171 | && (bfd_elf_get_dyn_lib_class (global_found->the_bfd) | |
1172 | & DYN_AS_NEEDED) == 0) | |
04925e1e | 1173 | continue; |
252b5132 | 1174 | |
e56f61be L |
1175 | n.by = l->by; |
1176 | n.name = l->name; | |
1177 | nn.by = l->by; | |
ec4eb78a L |
1178 | if (trace_file_tries) |
1179 | info_msg (_("%s needed by %B\n"), l->name, l->by); | |
1180 | ||
e13629bc AM |
1181 | /* As-needed libs specified on the command line (or linker script) |
1182 | take priority over libs found in search dirs. */ | |
1183 | if (global_found != NULL) | |
1184 | { | |
1185 | nn.name = global_found->filename; | |
1186 | if (gld${EMULATION_NAME}_try_needed (&nn, TRUE)) | |
1187 | continue; | |
1188 | } | |
1189 | ||
04925e1e AM |
1190 | /* We need to find this file and include the symbol table. We |
1191 | want to search for the file in the same way that the dynamic | |
1192 | linker will search. That means that we want to use | |
1193 | rpath_link, rpath, then the environment variable | |
ec4eb78a L |
1194 | LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH |
1195 | entries (native only), then the linker script LIB_SEARCH_DIRS. | |
1196 | We do not search using the -L arguments. | |
252b5132 | 1197 | |
04925e1e AM |
1198 | We search twice. The first time, we skip objects which may |
1199 | introduce version mismatches. The second time, we force | |
1200 | their use. See gld${EMULATION_NAME}_vercheck comment. */ | |
1201 | for (force = 0; force < 2; force++) | |
1202 | { | |
04925e1e AM |
1203 | size_t len; |
1204 | search_dirs_type *search; | |
6dd8c765 | 1205 | EOF |
ff925e69 | 1206 | if [ "x${NATIVE}" = xyes ] ; then |
92b93329 | 1207 | fragment <<EOF |
dcb0bd0e | 1208 | const char *lib_path; |
ff925e69 KK |
1209 | EOF |
1210 | fi | |
1211 | if [ "x${USE_LIBPATH}" = xyes ] ; then | |
92b93329 | 1212 | fragment <<EOF |
6dd8c765 L |
1213 | struct bfd_link_needed_list *rp; |
1214 | int found; | |
1215 | EOF | |
6dd8c765 | 1216 | fi |
92b93329 | 1217 | fragment <<EOF |
252b5132 | 1218 | |
04925e1e | 1219 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link, |
e56f61be | 1220 | &n, force)) |
04925e1e | 1221 | break; |
dcb0bd0e | 1222 | EOF |
9c8ebd6a | 1223 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
92b93329 | 1224 | fragment <<EOF |
04925e1e | 1225 | if (gld${EMULATION_NAME}_search_needed (command_line.rpath, |
e56f61be | 1226 | &n, force)) |
04925e1e | 1227 | break; |
9c8ebd6a DJ |
1228 | EOF |
1229 | fi | |
1230 | if [ "x${NATIVE}" = xyes ] ; then | |
92b93329 | 1231 | fragment <<EOF |
04925e1e AM |
1232 | if (command_line.rpath_link == NULL |
1233 | && command_line.rpath == NULL) | |
1234 | { | |
1235 | lib_path = (const char *) getenv ("LD_RUN_PATH"); | |
e56f61be | 1236 | if (gld${EMULATION_NAME}_search_needed (lib_path, &n, |
04925e1e AM |
1237 | force)) |
1238 | break; | |
1239 | } | |
04925e1e | 1240 | lib_path = (const char *) getenv ("LD_LIBRARY_PATH"); |
e56f61be | 1241 | if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force)) |
04925e1e | 1242 | break; |
9c8ebd6a DJ |
1243 | EOF |
1244 | fi | |
1245 | if [ "x${USE_LIBPATH}" = xyes ] ; then | |
92b93329 | 1246 | fragment <<EOF |
ec4eb78a | 1247 | found = 0; |
f13a99db | 1248 | rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info); |
6dd8c765 | 1249 | for (; !found && rp != NULL; rp = rp->next) |
ec4eb78a | 1250 | { |
9c8ebd6a | 1251 | char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name); |
ec4eb78a | 1252 | found = (rp->by == l->by |
9c8ebd6a | 1253 | && gld${EMULATION_NAME}_search_needed (tmpname, |
e56f61be | 1254 | &n, |
ec4eb78a | 1255 | force)); |
9c8ebd6a | 1256 | free (tmpname); |
ec4eb78a L |
1257 | } |
1258 | if (found) | |
1259 | break; | |
1260 | ||
04925e1e | 1261 | EOF |
04925e1e | 1262 | fi |
c1446dba L |
1263 | if [ "x${USE_LIBPATH}" = xyes ] ; then |
1264 | case ${target} in | |
ebe1fac1 | 1265 | *-*-freebsd* | *-*-dragonfly*) |
92b93329 | 1266 | fragment <<EOF |
ebe1fac1 NC |
1267 | if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force)) |
1268 | break; | |
1269 | EOF | |
1270 | # FreeBSD | |
1271 | ;; | |
1272 | ||
c1446dba L |
1273 | *-*-linux-* | *-*-k*bsd*-*) |
1274 | # Linux | |
92b93329 | 1275 | fragment <<EOF |
c1446dba L |
1276 | if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force)) |
1277 | break; | |
1278 | ||
1279 | EOF | |
1280 | ;; | |
1281 | esac | |
1282 | fi | |
92b93329 | 1283 | fragment <<EOF |
04925e1e AM |
1284 | len = strlen (l->name); |
1285 | for (search = search_head; search != NULL; search = search->next) | |
1286 | { | |
1287 | char *filename; | |
252b5132 | 1288 | |
04925e1e AM |
1289 | if (search->cmdline) |
1290 | continue; | |
1291 | filename = (char *) xmalloc (strlen (search->name) + len + 2); | |
1292 | sprintf (filename, "%s/%s", search->name, l->name); | |
e56f61be L |
1293 | nn.name = filename; |
1294 | if (gld${EMULATION_NAME}_try_needed (&nn, force)) | |
04925e1e AM |
1295 | break; |
1296 | free (filename); | |
1297 | } | |
1298 | if (search != NULL) | |
1299 | break; | |
1300 | EOF | |
92b93329 | 1301 | fragment <<EOF |
04925e1e | 1302 | } |
252b5132 | 1303 | |
04925e1e AM |
1304 | if (force < 2) |
1305 | continue; | |
252b5132 | 1306 | |
e374f1d9 | 1307 | einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n", |
04925e1e AM |
1308 | l->name, l->by); |
1309 | } | |
1310 | } | |
252b5132 | 1311 | |
41392f03 AM |
1312 | EOF |
1313 | fi | |
1314 | ||
92b93329 | 1315 | fragment <<EOF |
252b5132 | 1316 | |
04925e1e | 1317 | /* Look through an expression for an assignment statement. */ |
252b5132 | 1318 | |
04925e1e | 1319 | static void |
0c7a8e5a | 1320 | gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp) |
04925e1e | 1321 | { |
4ea42fb7 | 1322 | bfd_boolean provide = FALSE; |
04925e1e AM |
1323 | |
1324 | switch (exp->type.node_class) | |
252b5132 | 1325 | { |
04925e1e | 1326 | case etree_provide: |
4ea42fb7 AM |
1327 | provide = TRUE; |
1328 | /* Fall thru */ | |
1329 | case etree_assign: | |
04925e1e AM |
1330 | /* We call record_link_assignment even if the symbol is defined. |
1331 | This is because if it is defined by a dynamic object, we | |
1332 | actually want to use the value defined by the linker script, | |
1333 | not the value from the dynamic object (because we are setting | |
1334 | symbols like etext). If the symbol is defined by a regular | |
1335 | object, then, as it happens, calling record_link_assignment | |
1336 | will do no harm. */ | |
04925e1e | 1337 | if (strcmp (exp->assign.dst, ".") != 0) |
252b5132 | 1338 | { |
f13a99db AM |
1339 | if (!bfd_elf_record_link_assignment (link_info.output_bfd, |
1340 | &link_info, | |
fe21a8fc L |
1341 | exp->assign.dst, provide, |
1342 | exp->assign.hidden)) | |
04925e1e AM |
1343 | einfo ("%P%F: failed to record assignment to %s: %E\n", |
1344 | exp->assign.dst); | |
252b5132 | 1345 | } |
04925e1e AM |
1346 | gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src); |
1347 | break; | |
252b5132 | 1348 | |
04925e1e AM |
1349 | case etree_binary: |
1350 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs); | |
1351 | gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs); | |
1352 | break; | |
252b5132 | 1353 | |
04925e1e AM |
1354 | case etree_trinary: |
1355 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond); | |
1356 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs); | |
1357 | gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs); | |
1358 | break; | |
252b5132 | 1359 | |
04925e1e AM |
1360 | case etree_unary: |
1361 | gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child); | |
1362 | break; | |
252b5132 | 1363 | |
04925e1e AM |
1364 | default: |
1365 | break; | |
252b5132 RH |
1366 | } |
1367 | } | |
1368 | ||
04925e1e AM |
1369 | |
1370 | /* This is called by the before_allocation routine via | |
1371 | lang_for_each_statement. It locates any assignment statements, and | |
1372 | tells the ELF backend about them, in case they are assignments to | |
1373 | symbols which are referred to by dynamic objects. */ | |
1374 | ||
1375 | static void | |
0c7a8e5a | 1376 | gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s) |
04925e1e AM |
1377 | { |
1378 | if (s->header.type == lang_assignment_statement_enum) | |
1379 | gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp); | |
1380 | } | |
1381 | ||
41392f03 AM |
1382 | EOF |
1383 | ||
1384 | if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then | |
57e6abd2 AO |
1385 | if test x"${ELF_INTERPRETER_NAME+set}" = xset; then |
1386 | ELF_INTERPRETER_SET_DEFAULT=" | |
1387 | if (sinterp != NULL) | |
1388 | { | |
5718918d AM |
1389 | sinterp->contents = (unsigned char *) ${ELF_INTERPRETER_NAME}; |
1390 | sinterp->size = strlen ((char *) sinterp->contents) + 1; | |
57e6abd2 AO |
1391 | } |
1392 | ||
1393 | " | |
1394 | else | |
1395 | ELF_INTERPRETER_SET_DEFAULT= | |
1396 | fi | |
92b93329 | 1397 | fragment <<EOF |
04925e1e | 1398 | |
252b5132 RH |
1399 | /* This is called after the sections have been attached to output |
1400 | sections, but before any sizes or addresses have been set. */ | |
1401 | ||
1402 | static void | |
0c7a8e5a | 1403 | gld${EMULATION_NAME}_before_allocation (void) |
252b5132 RH |
1404 | { |
1405 | const char *rpath; | |
1406 | asection *sinterp; | |
1407 | ||
fd574c46 | 1408 | if (link_info.hash->type == bfd_link_elf_hash_table) |
f13a99db | 1409 | _bfd_elf_tls_setup (link_info.output_bfd, &link_info); |
e1918d23 | 1410 | |
252b5132 RH |
1411 | /* If we are going to make any variable assignments, we need to let |
1412 | the ELF backend know about them in case the variables are | |
1413 | referred to by dynamic objects. */ | |
1414 | lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment); | |
1415 | ||
1416 | /* Let the ELF backend work out the sizes of any sections required | |
1417 | by dynamic linking. */ | |
1418 | rpath = command_line.rpath; | |
1419 | if (rpath == NULL) | |
1420 | rpath = (const char *) getenv ("LD_RUN_PATH"); | |
5a580b3a | 1421 | if (! (bfd_elf_size_dynamic_sections |
f13a99db | 1422 | (link_info.output_bfd, command_line.soname, rpath, |
605d9b3a | 1423 | command_line.filter_shlib, |
252b5132 RH |
1424 | (const char * const *) command_line.auxiliary_filters, |
1425 | &link_info, &sinterp, lang_elf_version_info))) | |
1426 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); | |
8423293d | 1427 | |
57e6abd2 | 1428 | ${ELF_INTERPRETER_SET_DEFAULT} |
252b5132 RH |
1429 | /* Let the user override the dynamic linker we are using. */ |
1430 | if (command_line.interpreter != NULL | |
1431 | && sinterp != NULL) | |
1432 | { | |
1433 | sinterp->contents = (bfd_byte *) command_line.interpreter; | |
eea6121a | 1434 | sinterp->size = strlen (command_line.interpreter) + 1; |
252b5132 RH |
1435 | } |
1436 | ||
1437 | /* Look for any sections named .gnu.warning. As a GNU extensions, | |
1438 | we treat such sections as containing warning messages. We print | |
1439 | out the warning message, and then zero out the section size so | |
1440 | that it does not get copied into the output file. */ | |
1441 | ||
1442 | { | |
1443 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
1444 | { | |
1445 | asection *s; | |
1446 | bfd_size_type sz; | |
1447 | char *msg; | |
b34976b6 | 1448 | bfd_boolean ret; |
252b5132 RH |
1449 | |
1450 | if (is->just_syms_flag) | |
1451 | continue; | |
1452 | ||
1453 | s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning"); | |
1454 | if (s == NULL) | |
1455 | continue; | |
1456 | ||
eea6121a | 1457 | sz = s->size; |
1e9cc1c2 | 1458 | msg = (char *) xmalloc ((size_t) (sz + 1)); |
bc251d50 | 1459 | if (! bfd_get_section_contents (is->the_bfd, s, msg, |
8c675694 | 1460 | (file_ptr) 0, sz)) |
252b5132 RH |
1461 | einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n", |
1462 | is->the_bfd); | |
bc251d50 | 1463 | msg[sz] = '\0'; |
252b5132 RH |
1464 | ret = link_info.callbacks->warning (&link_info, msg, |
1465 | (const char *) NULL, | |
1466 | is->the_bfd, (asection *) NULL, | |
1467 | (bfd_vma) 0); | |
1468 | ASSERT (ret); | |
1469 | free (msg); | |
1470 | ||
9e3be61d AM |
1471 | /* Clobber the section size, so that we don't waste space |
1472 | copying the warning into the output file. If we've already | |
1473 | sized the output section, adjust its size. The adjustment | |
1474 | is on rawsize because targets that size sections early will | |
1475 | have called lang_reset_memory_regions after sizing. */ | |
1476 | if (s->output_section != NULL | |
1477 | && s->output_section->rawsize >= s->size) | |
1478 | s->output_section->rawsize -= s->size; | |
1479 | ||
eea6121a | 1480 | s->size = 0; |
11d2f718 | 1481 | |
9e3be61d AM |
1482 | /* Also set SEC_EXCLUDE, so that local symbols defined in the |
1483 | warning section don't get copied to the output. */ | |
a14a5de3 | 1484 | s->flags |= SEC_EXCLUDE | SEC_KEEP; |
252b5132 RH |
1485 | } |
1486 | } | |
8423293d | 1487 | |
1e035701 | 1488 | before_allocation_default (); |
8423293d | 1489 | |
f13a99db | 1490 | if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info)) |
8423293d | 1491 | einfo ("%P%F: failed to set dynamic section sizes: %E\n"); |
252b5132 RH |
1492 | } |
1493 | ||
41392f03 AM |
1494 | EOF |
1495 | fi | |
1496 | ||
1497 | if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then | |
92b93329 | 1498 | fragment <<EOF |
252b5132 | 1499 | |
04925e1e AM |
1500 | /* Try to open a dynamic archive. This is where we know that ELF |
1501 | dynamic libraries have an extension of .so (or .sl on oddball systems | |
1502 | like hpux). */ | |
1503 | ||
b34976b6 | 1504 | static bfd_boolean |
0c7a8e5a AM |
1505 | gld${EMULATION_NAME}_open_dynamic_archive |
1506 | (const char *arch, search_dirs_type *search, lang_input_statement_type *entry) | |
252b5132 | 1507 | { |
04925e1e AM |
1508 | const char *filename; |
1509 | char *string; | |
252b5132 | 1510 | |
04925e1e | 1511 | if (! entry->is_archive) |
b34976b6 | 1512 | return FALSE; |
252b5132 | 1513 | |
04925e1e | 1514 | filename = entry->filename; |
252b5132 | 1515 | |
04925e1e AM |
1516 | /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION |
1517 | is defined, but it does not seem worth the headache to optimize | |
1518 | away those two bytes of space. */ | |
1519 | string = (char *) xmalloc (strlen (search->name) | |
1520 | + strlen (filename) | |
1521 | + strlen (arch) | |
1522 | #ifdef EXTRA_SHLIB_EXTENSION | |
1523 | + strlen (EXTRA_SHLIB_EXTENSION) | |
1524 | #endif | |
1525 | + sizeof "/lib.so"); | |
1526 | ||
1527 | sprintf (string, "%s/lib%s%s.so", search->name, filename, arch); | |
1528 | ||
1529 | #ifdef EXTRA_SHLIB_EXTENSION | |
1530 | /* Try the .so extension first. If that fails build a new filename | |
1531 | using EXTRA_SHLIB_EXTENSION. */ | |
1532 | if (! ldfile_try_open_bfd (string, entry)) | |
a26cc967 AM |
1533 | { |
1534 | sprintf (string, "%s/lib%s%s%s", search->name, | |
1535 | filename, arch, EXTRA_SHLIB_EXTENSION); | |
04925e1e AM |
1536 | #endif |
1537 | ||
1538 | if (! ldfile_try_open_bfd (string, entry)) | |
252b5132 | 1539 | { |
04925e1e | 1540 | free (string); |
b34976b6 | 1541 | return FALSE; |
04925e1e | 1542 | } |
a26cc967 AM |
1543 | #ifdef EXTRA_SHLIB_EXTENSION |
1544 | } | |
1545 | #endif | |
252b5132 | 1546 | |
04925e1e | 1547 | entry->filename = string; |
252b5132 | 1548 | |
04925e1e AM |
1549 | /* We have found a dynamic object to include in the link. The ELF |
1550 | backend linker will create a DT_NEEDED entry in the .dynamic | |
1551 | section naming this file. If this file includes a DT_SONAME | |
1552 | entry, it will be used. Otherwise, the ELF linker will just use | |
1553 | the name of the file. For an archive found by searching, like | |
1554 | this one, the DT_NEEDED entry should consist of just the name of | |
1555 | the file, without the path information used to find it. Note | |
1556 | that we only need to do this if we have a dynamic object; an | |
1557 | archive will never be referenced by a DT_NEEDED entry. | |
252b5132 | 1558 | |
04925e1e AM |
1559 | FIXME: This approach--using bfd_elf_set_dt_needed_name--is not |
1560 | very pretty. I haven't been able to think of anything that is | |
1561 | pretty, though. */ | |
1562 | if (bfd_check_format (entry->the_bfd, bfd_object) | |
1563 | && (entry->the_bfd->flags & DYNAMIC) != 0) | |
1564 | { | |
04925e1e | 1565 | ASSERT (entry->is_archive && entry->search_dirs_flag); |
252b5132 | 1566 | |
04925e1e | 1567 | /* Rather than duplicating the logic above. Just use the |
1c9acd94 | 1568 | filename we recorded earlier. */ |
04925e1e | 1569 | |
fed2999d | 1570 | filename = lbasename (entry->filename); |
1c9acd94 | 1571 | bfd_elf_set_dt_needed_name (entry->the_bfd, filename); |
04925e1e AM |
1572 | } |
1573 | ||
b34976b6 | 1574 | return TRUE; |
04925e1e AM |
1575 | } |
1576 | ||
41392f03 AM |
1577 | EOF |
1578 | fi | |
cde43e70 AM |
1579 | |
1580 | if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then | |
92b93329 | 1581 | fragment <<EOF |
41392f03 | 1582 | |
afd7a018 | 1583 | /* A variant of lang_output_section_find used by place_orphan. */ |
04925e1e | 1584 | |
04925e1e | 1585 | static lang_output_section_statement_type * |
2a36a117 | 1586 | output_rel_find (asection *sec, int isdyn) |
04925e1e | 1587 | { |
04925e1e | 1588 | lang_output_section_statement_type *lookup; |
ba493122 | 1589 | lang_output_section_statement_type *last = NULL; |
2a36a117 | 1590 | lang_output_section_statement_type *last_alloc = NULL; |
48f2ff54 | 1591 | lang_output_section_statement_type *last_ro_alloc = NULL; |
ba493122 AM |
1592 | lang_output_section_statement_type *last_rel = NULL; |
1593 | lang_output_section_statement_type *last_rel_alloc = NULL; | |
24cdb50a | 1594 | int rela = sec->name[4] == 'a'; |
04925e1e | 1595 | |
afd7a018 AM |
1596 | for (lookup = &lang_output_section_statement.head->output_section_statement; |
1597 | lookup != NULL; | |
1598 | lookup = lookup->next) | |
04925e1e | 1599 | { |
66c103b7 | 1600 | if (lookup->constraint >= 0 |
0112cd26 | 1601 | && CONST_STRNEQ (lookup->name, ".rel")) |
04925e1e | 1602 | { |
24cdb50a | 1603 | int lookrela = lookup->name[4] == 'a'; |
ba493122 | 1604 | |
2a36a117 AM |
1605 | /* .rel.dyn must come before all other reloc sections, to suit |
1606 | GNU ld.so. */ | |
1607 | if (isdyn) | |
1608 | break; | |
1609 | ||
1610 | /* Don't place after .rel.plt as doing so results in wrong | |
1611 | dynamic tags. */ | |
1612 | if (strcmp (".plt", lookup->name + 4 + lookrela) == 0) | |
ba493122 | 1613 | break; |
2a36a117 AM |
1614 | |
1615 | if (rela == lookrela || last_rel == NULL) | |
24cdb50a | 1616 | last_rel = lookup; |
2a36a117 AM |
1617 | if ((rela == lookrela || last_rel_alloc == NULL) |
1618 | && lookup->bfd_section != NULL | |
ba493122 AM |
1619 | && (lookup->bfd_section->flags & SEC_ALLOC) != 0) |
1620 | last_rel_alloc = lookup; | |
04925e1e | 1621 | } |
2a36a117 AM |
1622 | |
1623 | last = lookup; | |
1624 | if (lookup->bfd_section != NULL | |
1625 | && (lookup->bfd_section->flags & SEC_ALLOC) != 0) | |
48f2ff54 AM |
1626 | { |
1627 | last_alloc = lookup; | |
1628 | if ((lookup->bfd_section->flags & SEC_READONLY) != 0) | |
1629 | last_ro_alloc = lookup; | |
1630 | } | |
252b5132 | 1631 | } |
ba493122 AM |
1632 | |
1633 | if (last_rel_alloc) | |
1634 | return last_rel_alloc; | |
1635 | ||
1636 | if (last_rel) | |
1637 | return last_rel; | |
1638 | ||
48f2ff54 AM |
1639 | if (last_ro_alloc) |
1640 | return last_ro_alloc; | |
1641 | ||
2a36a117 AM |
1642 | if (last_alloc) |
1643 | return last_alloc; | |
1644 | ||
ba493122 | 1645 | return last; |
252b5132 RH |
1646 | } |
1647 | ||
1648 | /* Place an orphan section. We use this to put random SHF_ALLOC | |
1649 | sections in the right segment. */ | |
1650 | ||
c2edb4b8 | 1651 | static lang_output_section_statement_type * |
8a99a385 AM |
1652 | gld${EMULATION_NAME}_place_orphan (asection *s, |
1653 | const char *secname, | |
1654 | int constraint) | |
252b5132 | 1655 | { |
afd7a018 AM |
1656 | static struct orphan_save hold[] = |
1657 | { | |
1658 | { ".text", | |
1659 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE, | |
1660 | 0, 0, 0, 0 }, | |
1661 | { ".rodata", | |
1662 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1663 | 0, 0, 0, 0 }, | |
1664 | { ".data", | |
1665 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA, | |
1666 | 0, 0, 0, 0 }, | |
1667 | { ".bss", | |
1668 | SEC_ALLOC, | |
1669 | 0, 0, 0, 0 }, | |
1670 | { 0, | |
1671 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1672 | 0, 0, 0, 0 }, | |
1673 | { ".interp", | |
1674 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1675 | 0, 0, 0, 0 }, | |
1676 | { ".sdata", | |
1677 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA, | |
07890c07 AM |
1678 | 0, 0, 0, 0 }, |
1679 | { 0, | |
1680 | SEC_HAS_CONTENTS, | |
1681 | 0, 0, 0, 0 }, | |
afd7a018 AM |
1682 | }; |
1683 | enum orphan_save_index | |
1684 | { | |
1685 | orphan_text = 0, | |
1686 | orphan_rodata, | |
1687 | orphan_data, | |
1688 | orphan_bss, | |
1689 | orphan_rel, | |
1690 | orphan_interp, | |
07890c07 AM |
1691 | orphan_sdata, |
1692 | orphan_nonalloc | |
afd7a018 AM |
1693 | }; |
1694 | static int orphan_init_done = 0; | |
6a345e87 | 1695 | struct orphan_save *place; |
afd7a018 | 1696 | lang_output_section_statement_type *after; |
252b5132 | 1697 | lang_output_section_statement_type *os; |
24cdb50a | 1698 | int isdyn = 0; |
ecca9871 L |
1699 | int iself = s->owner->xvec->flavour == bfd_target_elf_flavour; |
1700 | unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL; | |
252b5132 | 1701 | |
1049f94e | 1702 | if (! link_info.relocatable |
24cdb50a | 1703 | && link_info.combreloc |
ecca9871 | 1704 | && (s->flags & SEC_ALLOC)) |
24cdb50a | 1705 | { |
ecca9871 L |
1706 | if (iself) |
1707 | switch (sh_type) | |
1708 | { | |
1709 | case SHT_RELA: | |
1710 | secname = ".rela.dyn"; | |
1711 | isdyn = 1; | |
1712 | break; | |
1713 | case SHT_REL: | |
1714 | secname = ".rel.dyn"; | |
1715 | isdyn = 1; | |
1716 | break; | |
1717 | default: | |
1718 | break; | |
1719 | } | |
0112cd26 | 1720 | else if (CONST_STRNEQ (secname, ".rel")) |
ecca9871 L |
1721 | { |
1722 | secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn"; | |
1723 | isdyn = 1; | |
1724 | } | |
24cdb50a | 1725 | } |
aea4bd9d | 1726 | |
bcacc0f5 | 1727 | /* Look through the script to see where to place this section. */ |
d127ecce AM |
1728 | if (constraint == 0) |
1729 | for (os = lang_output_section_find (secname); | |
1730 | os != NULL; | |
1731 | os = next_matching_output_section_statement (os, 0)) | |
1732 | { | |
1733 | /* If we don't match an existing output section, tell | |
1734 | lang_insert_orphan to create a new output section. */ | |
1735 | constraint = SPECIAL; | |
1736 | ||
1737 | if (os->bfd_section != NULL | |
1738 | && (os->bfd_section->flags == 0 | |
1739 | || (_bfd_elf_match_sections_by_type (link_info.output_bfd, | |
1740 | os->bfd_section, | |
1741 | s->owner, s) | |
1742 | && ((s->flags ^ os->bfd_section->flags) | |
1743 | & (SEC_LOAD | SEC_ALLOC)) == 0))) | |
1744 | { | |
1745 | /* We already have an output section statement with this | |
1746 | name, and its bfd section has compatible flags. | |
1747 | If the section already exists but does not have any flags | |
1748 | set, then it has been created by the linker, probably as a | |
1749 | result of a --section-start command line switch. */ | |
1750 | lang_add_section (&os->children, s, os); | |
1751 | return os; | |
1752 | } | |
1753 | } | |
252b5132 | 1754 | |
afd7a018 AM |
1755 | if (!orphan_init_done) |
1756 | { | |
07890c07 | 1757 | lang_output_section_statement_type *lookup; |
afd7a018 | 1758 | struct orphan_save *ho; |
07890c07 | 1759 | |
afd7a018 AM |
1760 | for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho) |
1761 | if (ho->name != NULL) | |
1762 | { | |
1763 | ho->os = lang_output_section_find (ho->name); | |
1764 | if (ho->os != NULL && ho->os->flags == 0) | |
1765 | ho->os->flags = ho->flags; | |
1766 | } | |
07890c07 AM |
1767 | lookup = hold[orphan_bss].os; |
1768 | if (lookup == NULL) | |
1769 | lookup = &lang_output_section_statement.head->output_section_statement; | |
1770 | for (; lookup != NULL; lookup = lookup->next) | |
1771 | if ((lookup->bfd_section != NULL | |
1772 | && (lookup->bfd_section->flags & SEC_DEBUGGING) != 0) | |
1773 | || strcmp (lookup->name, ".comment") == 0) | |
1774 | break; | |
1775 | hold[orphan_nonalloc].os = lookup ? lookup->prev : NULL; | |
1776 | hold[orphan_nonalloc].name = ".comment"; | |
afd7a018 AM |
1777 | orphan_init_done = 1; |
1778 | } | |
252b5132 RH |
1779 | |
1780 | /* If this is a final link, then always put .gnu.warning.SYMBOL | |
1781 | sections into the .text section to get them out of the way. */ | |
36af4a4e | 1782 | if (link_info.executable |
1049f94e | 1783 | && ! link_info.relocatable |
bcacc0f5 | 1784 | && CONST_STRNEQ (s->name, ".gnu.warning.") |
afd7a018 | 1785 | && hold[orphan_text].os != NULL) |
252b5132 | 1786 | { |
c2edb4b8 AM |
1787 | os = hold[orphan_text].os; |
1788 | lang_add_section (&os->children, s, os); | |
1789 | return os; | |
252b5132 RH |
1790 | } |
1791 | ||
1792 | /* Decide which segment the section should go in based on the | |
1793 | section name and section flags. We put loadable .note sections | |
1794 | right after the .interp section, so that the PT_NOTE segment is | |
1795 | stored right after the program headers where the OS can read it | |
1796 | in the first page. */ | |
aea4bd9d | 1797 | |
71bfc0ae | 1798 | place = NULL; |
07890c07 AM |
1799 | if ((s->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0) |
1800 | place = &hold[orphan_nonalloc]; | |
1801 | else if ((s->flags & SEC_ALLOC) == 0) | |
71bfc0ae | 1802 | ; |
252b5132 | 1803 | else if ((s->flags & SEC_LOAD) != 0 |
ecca9871 | 1804 | && ((iself && sh_type == SHT_NOTE) |
0112cd26 | 1805 | || (!iself && CONST_STRNEQ (secname, ".note")))) |
afd7a018 AM |
1806 | place = &hold[orphan_interp]; |
1807 | else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
1808 | place = &hold[orphan_bss]; | |
1809 | else if ((s->flags & SEC_SMALL_DATA) != 0) | |
1810 | place = &hold[orphan_sdata]; | |
1811 | else if ((s->flags & SEC_READONLY) == 0) | |
1812 | place = &hold[orphan_data]; | |
ecca9871 | 1813 | else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL)) |
0112cd26 | 1814 | || (!iself && CONST_STRNEQ (secname, ".rel"))) |
afd7a018 AM |
1815 | && (s->flags & SEC_LOAD) != 0) |
1816 | place = &hold[orphan_rel]; | |
1817 | else if ((s->flags & SEC_CODE) == 0) | |
1818 | place = &hold[orphan_rodata]; | |
1819 | else | |
1820 | place = &hold[orphan_text]; | |
71bfc0ae | 1821 | |
afd7a018 | 1822 | after = NULL; |
5ba47421 | 1823 | if (place != NULL) |
252b5132 | 1824 | { |
afd7a018 | 1825 | if (place->os == NULL) |
5ba47421 | 1826 | { |
afd7a018 AM |
1827 | if (place->name != NULL) |
1828 | place->os = lang_output_section_find (place->name); | |
1829 | else | |
1830 | place->os = output_rel_find (s, isdyn); | |
5ba47421 | 1831 | } |
afd7a018 AM |
1832 | after = place->os; |
1833 | if (after == NULL) | |
390fbbf1 AM |
1834 | after = lang_output_section_find_by_flags |
1835 | (s, &place->os, _bfd_elf_match_sections_by_type); | |
afd7a018 AM |
1836 | if (after == NULL) |
1837 | /* *ABS* is always the first output section statement. */ | |
1838 | after = &lang_output_section_statement.head->output_section_statement; | |
252b5132 RH |
1839 | } |
1840 | ||
c2edb4b8 | 1841 | return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL); |
252b5132 | 1842 | } |
c56feb2b AM |
1843 | EOF |
1844 | fi | |
1845 | ||
eaeb0a9d | 1846 | if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then |
92b93329 | 1847 | fragment <<EOF |
252b5132 | 1848 | |
deb04cdb | 1849 | static void |
eaeb0a9d | 1850 | gld${EMULATION_NAME}_after_allocation (void) |
deb04cdb | 1851 | { |
f13a99db AM |
1852 | bfd_boolean need_layout = bfd_elf_discard_info (link_info.output_bfd, |
1853 | &link_info); | |
8ded5a0f | 1854 | gld${EMULATION_NAME}_map_segments (need_layout); |
73d074b4 | 1855 | } |
41392f03 AM |
1856 | EOF |
1857 | fi | |
1858 | ||
1859 | if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then | |
92b93329 | 1860 | fragment <<EOF |
252b5132 RH |
1861 | |
1862 | static char * | |
0c7a8e5a | 1863 | gld${EMULATION_NAME}_get_script (int *isfile) |
252b5132 RH |
1864 | EOF |
1865 | ||
1866 | if test -n "$COMPILE_IN" | |
1867 | then | |
1868 | # Scripts compiled in. | |
1869 | ||
1870 | # sed commands to quote an ld script as a C string. | |
597e2591 | 1871 | sc="-f stringify.sed" |
252b5132 | 1872 | |
92b93329 | 1873 | fragment <<EOF |
60bcf0fa | 1874 | { |
252b5132 RH |
1875 | *isfile = 0; |
1876 | ||
1049f94e | 1877 | if (link_info.relocatable && config.build_constructors) |
597e2591 | 1878 | return |
252b5132 | 1879 | EOF |
afd7a018 AM |
1880 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c |
1881 | echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c | |
1882 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c | |
1883 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
1884 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
61585df2 | 1885 | if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else |
afd7a018 AM |
1886 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c |
1887 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
db6751f2 | 1888 | fi |
36af4a4e JJ |
1889 | if test -n "$GENERATE_PIE_SCRIPT" ; then |
1890 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
8c37241b JJ |
1891 | echo ' ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c |
1892 | echo ' && link_info.relro' >> e${EMULATION_NAME}.c | |
fbfca19e | 1893 | echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1894 | sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c |
36af4a4e | 1895 | echo ' ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1896 | sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c |
36af4a4e | 1897 | fi |
afd7a018 AM |
1898 | echo ' ; else if (link_info.pie) return' >> e${EMULATION_NAME}.c |
1899 | sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c | |
36af4a4e | 1900 | fi |
252b5132 | 1901 | if test -n "$GENERATE_SHLIB_SCRIPT" ; then |
82434356 | 1902 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then |
8c37241b JJ |
1903 | echo ' ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c |
1904 | echo ' && link_info.relro' >> e${EMULATION_NAME}.c | |
fbfca19e | 1905 | echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1906 | sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c |
db6751f2 | 1907 | echo ' ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c |
afd7a018 | 1908 | sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c |
82434356 | 1909 | fi |
afd7a018 AM |
1910 | echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c |
1911 | sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c | |
252b5132 | 1912 | fi |
82434356 | 1913 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then |
8c37241b | 1914 | echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c |
fbfca19e | 1915 | echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c |
afd7a018 AM |
1916 | sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c |
1917 | echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c | |
1918 | sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c | |
82434356 | 1919 | fi |
afd7a018 AM |
1920 | echo ' ; else return' >> e${EMULATION_NAME}.c |
1921 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
1922 | echo '; }' >> e${EMULATION_NAME}.c | |
252b5132 RH |
1923 | |
1924 | else | |
1925 | # Scripts read from the filesystem. | |
1926 | ||
92b93329 | 1927 | fragment <<EOF |
60bcf0fa | 1928 | { |
252b5132 RH |
1929 | *isfile = 1; |
1930 | ||
1049f94e | 1931 | if (link_info.relocatable && config.build_constructors) |
252b5132 | 1932 | return "ldscripts/${EMULATION_NAME}.xu"; |
1049f94e | 1933 | else if (link_info.relocatable) |
252b5132 RH |
1934 | return "ldscripts/${EMULATION_NAME}.xr"; |
1935 | else if (!config.text_read_only) | |
1936 | return "ldscripts/${EMULATION_NAME}.xbn"; | |
a060b769 AM |
1937 | EOF |
1938 | if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : | |
1939 | else | |
92b93329 | 1940 | fragment <<EOF |
252b5132 RH |
1941 | else if (!config.magic_demand_paged) |
1942 | return "ldscripts/${EMULATION_NAME}.xn"; | |
a060b769 AM |
1943 | EOF |
1944 | fi | |
36af4a4e JJ |
1945 | if test -n "$GENERATE_PIE_SCRIPT" ; then |
1946 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
92b93329 | 1947 | fragment <<EOF |
8c37241b | 1948 | else if (link_info.pie && link_info.combreloc |
fbfca19e | 1949 | && link_info.relro && (link_info.flags & DF_BIND_NOW)) |
8c37241b | 1950 | return "ldscripts/${EMULATION_NAME}.xdw"; |
36af4a4e JJ |
1951 | else if (link_info.pie && link_info.combreloc) |
1952 | return "ldscripts/${EMULATION_NAME}.xdc"; | |
1953 | EOF | |
1954 | fi | |
92b93329 | 1955 | fragment <<EOF |
36af4a4e JJ |
1956 | else if (link_info.pie) |
1957 | return "ldscripts/${EMULATION_NAME}.xd"; | |
1958 | EOF | |
1959 | fi | |
a060b769 AM |
1960 | if test -n "$GENERATE_SHLIB_SCRIPT" ; then |
1961 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
92b93329 | 1962 | fragment <<EOF |
8c37241b | 1963 | else if (link_info.shared && link_info.combreloc |
fbfca19e | 1964 | && link_info.relro && (link_info.flags & DF_BIND_NOW)) |
8c37241b | 1965 | return "ldscripts/${EMULATION_NAME}.xsw"; |
a060b769 AM |
1966 | else if (link_info.shared && link_info.combreloc) |
1967 | return "ldscripts/${EMULATION_NAME}.xsc"; | |
1968 | EOF | |
1969 | fi | |
92b93329 | 1970 | fragment <<EOF |
252b5132 RH |
1971 | else if (link_info.shared) |
1972 | return "ldscripts/${EMULATION_NAME}.xs"; | |
a060b769 AM |
1973 | EOF |
1974 | fi | |
1975 | if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then | |
92b93329 | 1976 | fragment <<EOF |
8c37241b | 1977 | else if (link_info.combreloc && link_info.relro |
fbfca19e | 1978 | && (link_info.flags & DF_BIND_NOW)) |
8c37241b | 1979 | return "ldscripts/${EMULATION_NAME}.xw"; |
a060b769 AM |
1980 | else if (link_info.combreloc) |
1981 | return "ldscripts/${EMULATION_NAME}.xc"; | |
1982 | EOF | |
1983 | fi | |
92b93329 | 1984 | fragment <<EOF |
252b5132 RH |
1985 | else |
1986 | return "ldscripts/${EMULATION_NAME}.x"; | |
1987 | } | |
252b5132 | 1988 | |
3b108066 | 1989 | EOF |
41392f03 AM |
1990 | fi |
1991 | fi | |
3b108066 | 1992 | |
41392f03 | 1993 | if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then |
3b108066 | 1994 | |
e0ee487b | 1995 | if test -n "$PARSE_AND_LIST_PROLOGUE" ; then |
92b93329 | 1996 | fragment <<EOF |
e0ee487b L |
1997 | $PARSE_AND_LIST_PROLOGUE |
1998 | EOF | |
1999 | fi | |
2000 | ||
92b93329 | 2001 | fragment <<EOF |
e0ee487b | 2002 | |
6c1439be L |
2003 | #define OPTION_DISABLE_NEW_DTAGS (400) |
2004 | #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1) | |
a1ab1d2a | 2005 | #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1) |
65765700 | 2006 | #define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1) |
b58f81ae | 2007 | #define OPTION_EXCLUDE_LIBS (OPTION_EH_FRAME_HDR + 1) |
fdc90cb4 | 2008 | #define OPTION_HASH_STYLE (OPTION_EXCLUDE_LIBS + 1) |
c0065db7 | 2009 | #define OPTION_BUILD_ID (OPTION_HASH_STYLE + 1) |
e13629bc | 2010 | |
3bcf5557 | 2011 | static void |
0c7a8e5a AM |
2012 | gld${EMULATION_NAME}_add_options |
2013 | (int ns, char **shortopts, int nl, struct option **longopts, | |
2014 | int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED) | |
e0ee487b | 2015 | { |
3bcf5557 AM |
2016 | static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:"; |
2017 | static const struct option xtra_long[] = { | |
c0065db7 | 2018 | {"build-id", optional_argument, NULL, OPTION_BUILD_ID}, |
e0ee487b | 2019 | EOF |
e0ee487b L |
2020 | |
2021 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
92b93329 | 2022 | fragment <<EOF |
3bcf5557 AM |
2023 | {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS}, |
2024 | {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS}, | |
2025 | {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR}, | |
b58f81ae | 2026 | {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS}, |
fdc90cb4 | 2027 | {"hash-style", required_argument, NULL, OPTION_HASH_STYLE}, |
3bcf5557 | 2028 | {"Bgroup", no_argument, NULL, OPTION_GROUP}, |
e0ee487b L |
2029 | EOF |
2030 | fi | |
2031 | ||
2032 | if test -n "$PARSE_AND_LIST_LONGOPTS" ; then | |
92b93329 | 2033 | fragment <<EOF |
3bcf5557 | 2034 | $PARSE_AND_LIST_LONGOPTS |
e0ee487b L |
2035 | EOF |
2036 | fi | |
2037 | ||
92b93329 | 2038 | fragment <<EOF |
3bcf5557 AM |
2039 | {NULL, no_argument, NULL, 0} |
2040 | }; | |
2041 | ||
2042 | *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short)); | |
2043 | memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short)); | |
2044 | *longopts = (struct option *) | |
2045 | xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long)); | |
2046 | memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long)); | |
2047 | } | |
04925e1e | 2048 | |
24382dca | 2049 | #define DEFAULT_BUILD_ID_STYLE "sha1" |
c0065db7 | 2050 | |
3bcf5557 | 2051 | static bfd_boolean |
0c7a8e5a | 2052 | gld${EMULATION_NAME}_handle_option (int optc) |
e0ee487b | 2053 | { |
e0ee487b L |
2054 | switch (optc) |
2055 | { | |
2056 | default: | |
3bcf5557 | 2057 | return FALSE; |
6c1439be | 2058 | |
c0065db7 RM |
2059 | case OPTION_BUILD_ID: |
2060 | if (link_info.emit_note_gnu_build_id != NULL) | |
2061 | { | |
2062 | free (link_info.emit_note_gnu_build_id); | |
2063 | link_info.emit_note_gnu_build_id = NULL; | |
2064 | } | |
2065 | if (optarg == NULL) | |
2066 | optarg = DEFAULT_BUILD_ID_STYLE; | |
2067 | if (strcmp (optarg, "none")) | |
2068 | link_info.emit_note_gnu_build_id = xstrdup (optarg); | |
2069 | break; | |
2070 | ||
e0ee487b | 2071 | EOF |
e0ee487b L |
2072 | |
2073 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
92b93329 | 2074 | fragment <<EOF |
6c1439be | 2075 | case OPTION_DISABLE_NEW_DTAGS: |
b34976b6 | 2076 | link_info.new_dtags = FALSE; |
6c1439be L |
2077 | break; |
2078 | ||
2079 | case OPTION_ENABLE_NEW_DTAGS: | |
b34976b6 | 2080 | link_info.new_dtags = TRUE; |
6c1439be L |
2081 | break; |
2082 | ||
65765700 | 2083 | case OPTION_EH_FRAME_HDR: |
b34976b6 | 2084 | link_info.eh_frame_hdr = TRUE; |
65765700 JJ |
2085 | break; |
2086 | ||
a1ab1d2a UD |
2087 | case OPTION_GROUP: |
2088 | link_info.flags_1 |= (bfd_vma) DF_1_GROUP; | |
2089 | /* Groups must be self-contained. */ | |
560e09e9 NC |
2090 | link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR; |
2091 | link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR; | |
a1ab1d2a UD |
2092 | break; |
2093 | ||
b58f81ae DJ |
2094 | case OPTION_EXCLUDE_LIBS: |
2095 | add_excluded_libs (optarg); | |
2096 | break; | |
2097 | ||
fdc90cb4 JJ |
2098 | case OPTION_HASH_STYLE: |
2099 | link_info.emit_hash = FALSE; | |
2100 | link_info.emit_gnu_hash = FALSE; | |
2101 | if (strcmp (optarg, "sysv") == 0) | |
2102 | link_info.emit_hash = TRUE; | |
2103 | else if (strcmp (optarg, "gnu") == 0) | |
2104 | link_info.emit_gnu_hash = TRUE; | |
2105 | else if (strcmp (optarg, "both") == 0) | |
2106 | { | |
2107 | link_info.emit_hash = TRUE; | |
2108 | link_info.emit_gnu_hash = TRUE; | |
2109 | } | |
2110 | else | |
2111 | einfo (_("%P%F: invalid hash style \`%s'\n"), optarg); | |
2112 | break; | |
2113 | ||
e0ee487b L |
2114 | case 'z': |
2115 | if (strcmp (optarg, "initfirst") == 0) | |
2116 | link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST; | |
2117 | else if (strcmp (optarg, "interpose") == 0) | |
2118 | link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE; | |
2119 | else if (strcmp (optarg, "loadfltr") == 0) | |
2120 | link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR; | |
2121 | else if (strcmp (optarg, "nodefaultlib") == 0) | |
2122 | link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB; | |
2123 | else if (strcmp (optarg, "nodelete") == 0) | |
2124 | link_info.flags_1 |= (bfd_vma) DF_1_NODELETE; | |
2125 | else if (strcmp (optarg, "nodlopen") == 0) | |
2126 | link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN; | |
2127 | else if (strcmp (optarg, "nodump") == 0) | |
2128 | link_info.flags_1 |= (bfd_vma) DF_1_NODUMP; | |
2129 | else if (strcmp (optarg, "now") == 0) | |
2130 | { | |
2131 | link_info.flags |= (bfd_vma) DF_BIND_NOW; | |
2132 | link_info.flags_1 |= (bfd_vma) DF_1_NOW; | |
2133 | } | |
5fa222e4 AM |
2134 | else if (strcmp (optarg, "lazy") == 0) |
2135 | { | |
2136 | link_info.flags &= ~(bfd_vma) DF_BIND_NOW; | |
2137 | link_info.flags_1 &= ~(bfd_vma) DF_1_NOW; | |
2138 | } | |
e0ee487b L |
2139 | else if (strcmp (optarg, "origin") == 0) |
2140 | { | |
2141 | link_info.flags |= (bfd_vma) DF_ORIGIN; | |
2142 | link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN; | |
2143 | } | |
a1ab1d2a | 2144 | else if (strcmp (optarg, "defs") == 0) |
560e09e9 | 2145 | link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR; |
aa713662 | 2146 | else if (strcmp (optarg, "muldefs") == 0) |
b34976b6 | 2147 | link_info.allow_multiple_definition = TRUE; |
db6751f2 | 2148 | else if (strcmp (optarg, "combreloc") == 0) |
b34976b6 | 2149 | link_info.combreloc = TRUE; |
db6751f2 | 2150 | else if (strcmp (optarg, "nocombreloc") == 0) |
b34976b6 | 2151 | link_info.combreloc = FALSE; |
8bd621d8 | 2152 | else if (strcmp (optarg, "nocopyreloc") == 0) |
0c7a8e5a | 2153 | link_info.nocopyreloc = TRUE; |
9ee5e499 JJ |
2154 | else if (strcmp (optarg, "execstack") == 0) |
2155 | { | |
2156 | link_info.execstack = TRUE; | |
2157 | link_info.noexecstack = FALSE; | |
2158 | } | |
2159 | else if (strcmp (optarg, "noexecstack") == 0) | |
2160 | { | |
2161 | link_info.noexecstack = TRUE; | |
2162 | link_info.execstack = FALSE; | |
2163 | } | |
88b882e9 L |
2164 | EOF |
2165 | ||
2166 | if test -n "$COMMONPAGESIZE"; then | |
92b93329 | 2167 | fragment <<EOF |
8c37241b JJ |
2168 | else if (strcmp (optarg, "relro") == 0) |
2169 | link_info.relro = TRUE; | |
2170 | else if (strcmp (optarg, "norelro") == 0) | |
2171 | link_info.relro = FALSE; | |
88b882e9 L |
2172 | EOF |
2173 | fi | |
2174 | ||
92b93329 | 2175 | fragment <<EOF |
0112cd26 | 2176 | else if (CONST_STRNEQ (optarg, "max-page-size=")) |
24718e3b L |
2177 | { |
2178 | char *end; | |
0112cd26 | 2179 | |
24718e3b | 2180 | config.maxpagesize = strtoul (optarg + 14, &end, 0); |
b8142865 | 2181 | if (*end || (config.maxpagesize & (config.maxpagesize - 1)) != 0) |
24718e3b L |
2182 | einfo (_("%P%F: invalid maxium page size \`%s'\n"), |
2183 | optarg + 14); | |
2184 | } | |
0112cd26 | 2185 | else if (CONST_STRNEQ (optarg, "common-page-size=")) |
24718e3b L |
2186 | { |
2187 | char *end; | |
2188 | config.commonpagesize = strtoul (optarg + 17, &end, 0); | |
b8142865 L |
2189 | if (*end |
2190 | || (config.commonpagesize & (config.commonpagesize - 1)) != 0) | |
24718e3b L |
2191 | einfo (_("%P%F: invalid common page size \`%s'\n"), |
2192 | optarg + 17); | |
2193 | } | |
e0ee487b | 2194 | /* What about the other Solaris -z options? FIXME. */ |
6c1439be | 2195 | break; |
e0ee487b L |
2196 | EOF |
2197 | fi | |
2198 | ||
2199 | if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then | |
92b93329 | 2200 | fragment <<EOF |
e0ee487b L |
2201 | $PARSE_AND_LIST_ARGS_CASES |
2202 | EOF | |
2203 | fi | |
2204 | ||
92b93329 | 2205 | fragment <<EOF |
e0ee487b L |
2206 | } |
2207 | ||
3bcf5557 | 2208 | return TRUE; |
e0ee487b L |
2209 | } |
2210 | ||
41392f03 | 2211 | EOF |
41392f03 | 2212 | |
4b209b22 | 2213 | if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then |
92b93329 | 2214 | fragment <<EOF |
41392f03 | 2215 | |
e0ee487b | 2216 | static void |
0c7a8e5a | 2217 | gld${EMULATION_NAME}_list_options (FILE * file) |
e0ee487b | 2218 | { |
442996ee AM |
2219 | fprintf (file, _("\ |
2220 | --build-id[=STYLE] Generate build ID note\n")); | |
e0ee487b | 2221 | EOF |
e0ee487b L |
2222 | |
2223 | if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then | |
92b93329 | 2224 | fragment <<EOF |
442996ee AM |
2225 | fprintf (file, _("\ |
2226 | -Bgroup Selects group name lookup rules for DSO\n")); | |
2227 | fprintf (file, _("\ | |
2228 | --disable-new-dtags Disable new dynamic tags\n")); | |
2229 | fprintf (file, _("\ | |
2230 | --enable-new-dtags Enable new dynamic tags\n")); | |
2231 | fprintf (file, _("\ | |
2232 | --eh-frame-hdr Create .eh_frame_hdr section\n")); | |
2233 | fprintf (file, _("\ | |
2234 | --hash-style=STYLE Set hash style to sysv, gnu or both\n")); | |
2235 | fprintf (file, _("\ | |
2236 | -z combreloc Merge dynamic relocs into one section and sort\n")); | |
2237 | fprintf (file, _("\ | |
2238 | -z defs Report unresolved symbols in object files.\n")); | |
2239 | fprintf (file, _("\ | |
2240 | -z execstack Mark executable as requiring executable stack\n")); | |
2241 | fprintf (file, _("\ | |
2242 | -z initfirst Mark DSO to be initialized first at runtime\n")); | |
2243 | fprintf (file, _("\ | |
2244 | -z interpose Mark object to interpose all DSOs but executable\n")); | |
2245 | fprintf (file, _("\ | |
2246 | -z lazy Mark object lazy runtime binding (default)\n")); | |
2247 | fprintf (file, _("\ | |
2248 | -z loadfltr Mark object requiring immediate process\n")); | |
2249 | fprintf (file, _("\ | |
2250 | -z muldefs Allow multiple definitions\n")); | |
2251 | fprintf (file, _("\ | |
2252 | -z nocombreloc Don't merge dynamic relocs into one section\n")); | |
2253 | fprintf (file, _("\ | |
2254 | -z nocopyreloc Don't create copy relocs\n")); | |
2255 | fprintf (file, _("\ | |
2256 | -z nodefaultlib Mark object not to use default search paths\n")); | |
2257 | fprintf (file, _("\ | |
2258 | -z nodelete Mark DSO non-deletable at runtime\n")); | |
2259 | fprintf (file, _("\ | |
2260 | -z nodlopen Mark DSO not available to dlopen\n")); | |
2261 | fprintf (file, _("\ | |
2262 | -z nodump Mark DSO not available to dldump\n")); | |
2263 | fprintf (file, _("\ | |
2264 | -z noexecstack Mark executable as not requiring executable stack\n")); | |
88b882e9 L |
2265 | EOF |
2266 | ||
2267 | if test -n "$COMMONPAGESIZE"; then | |
92b93329 | 2268 | fragment <<EOF |
442996ee AM |
2269 | fprintf (file, _("\ |
2270 | -z norelro Don't create RELRO program header\n")); | |
88b882e9 L |
2271 | EOF |
2272 | fi | |
2273 | ||
92b93329 | 2274 | fragment <<EOF |
442996ee AM |
2275 | fprintf (file, _("\ |
2276 | -z now Mark object non-lazy runtime binding\n")); | |
2277 | fprintf (file, _("\ | |
2278 | -z origin Mark object requiring immediate \$ORIGIN\n\ | |
2279 | processing at runtime\n")); | |
88b882e9 L |
2280 | EOF |
2281 | ||
2282 | if test -n "$COMMONPAGESIZE"; then | |
92b93329 | 2283 | fragment <<EOF |
442996ee AM |
2284 | fprintf (file, _("\ |
2285 | -z relro Create RELRO program header\n")); | |
88b882e9 L |
2286 | EOF |
2287 | fi | |
2288 | ||
92b93329 | 2289 | fragment <<EOF |
442996ee AM |
2290 | fprintf (file, _("\ |
2291 | -z max-page-size=SIZE Set maximum page size to SIZE\n")); | |
2292 | fprintf (file, _("\ | |
2293 | -z common-page-size=SIZE Set common page size to SIZE\n")); | |
2294 | fprintf (file, _("\ | |
2295 | -z KEYWORD Ignored for Solaris compatibility\n")); | |
e0ee487b L |
2296 | EOF |
2297 | fi | |
2298 | ||
2299 | if test -n "$PARSE_AND_LIST_OPTIONS" ; then | |
92b93329 | 2300 | fragment <<EOF |
e0ee487b L |
2301 | $PARSE_AND_LIST_OPTIONS |
2302 | EOF | |
2303 | fi | |
2304 | ||
92b93329 | 2305 | fragment <<EOF |
e0ee487b L |
2306 | } |
2307 | EOF | |
e0ee487b L |
2308 | |
2309 | if test -n "$PARSE_AND_LIST_EPILOGUE" ; then | |
92b93329 | 2310 | fragment <<EOF |
e0ee487b L |
2311 | $PARSE_AND_LIST_EPILOGUE |
2312 | EOF | |
2313 | fi | |
41392f03 AM |
2314 | fi |
2315 | else | |
92b93329 | 2316 | fragment <<EOF |
3bcf5557 AM |
2317 | #define gld${EMULATION_NAME}_add_options NULL |
2318 | #define gld${EMULATION_NAME}_handle_option NULL | |
41392f03 | 2319 | EOF |
4b209b22 | 2320 | if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then |
92b93329 | 2321 | fragment <<EOF |
4b209b22 | 2322 | #define gld${EMULATION_NAME}_list_options NULL |
41392f03 AM |
2323 | EOF |
2324 | fi | |
2325 | fi | |
e0ee487b | 2326 | |
92b93329 | 2327 | fragment <<EOF |
252b5132 | 2328 | |
60bcf0fa | 2329 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = |
252b5132 | 2330 | { |
41392f03 AM |
2331 | ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse}, |
2332 | ${LDEMUL_SYSLIB-syslib_default}, | |
2333 | ${LDEMUL_HLL-hll_default}, | |
2334 | ${LDEMUL_AFTER_PARSE-after_parse_default}, | |
2335 | ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open}, | |
eaeb0a9d | 2336 | ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation}, |
41392f03 AM |
2337 | ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default}, |
2338 | ${LDEMUL_CHOOSE_TARGET-ldemul_default_target}, | |
2339 | ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation}, | |
2340 | ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script}, | |
252b5132 RH |
2341 | "${EMULATION_NAME}", |
2342 | "${OUTPUT_FORMAT}", | |
eaeb0a9d | 2343 | ${LDEMUL_FINISH-finish_default}, |
41392f03 AM |
2344 | ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL}, |
2345 | ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive}, | |
2346 | ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan}, | |
2347 | ${LDEMUL_SET_SYMBOLS-NULL}, | |
3bcf5557 AM |
2348 | ${LDEMUL_PARSE_ARGS-NULL}, |
2349 | gld${EMULATION_NAME}_add_options, | |
2350 | gld${EMULATION_NAME}_handle_option, | |
41392f03 | 2351 | ${LDEMUL_UNRECOGNIZED_FILE-NULL}, |
4b209b22 | 2352 | ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options}, |
4a43e768 | 2353 | ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols}, |
41392f03 | 2354 | ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL}, |
fac1652d | 2355 | ${LDEMUL_NEW_VERS_PATTERN-NULL} |
252b5132 RH |
2356 | }; |
2357 | EOF |