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