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