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