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