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