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