Don't merge 2 sections with different SHF_EXCLUDE
[deliverable/binutils-gdb.git] / ld / emultempl / elf32.em
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.
5 test -z "${ELFSIZE}" && ELFSIZE=32
6 if [ -z "$MACHINE" ]; then
7 OUTPUT_ARCH=${ARCH}
8 else
9 OUTPUT_ARCH=${ARCH}:${MACHINE}
10 fi
11 fragment <<EOF
12 /* This file is is generated by a shell script. DO NOT EDIT! */
13
14 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
15 Copyright (C) 1991-2016 Free Software Foundation, Inc.
16 Written by Steve Chamberlain <sac@cygnus.com>
17 ELF support by Ian Lance Taylor <ian@cygnus.com>
18
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. */
35
36 #define TARGET_IS_${EMULATION_NAME}
37
38 #include "sysdep.h"
39 #include "bfd.h"
40 #include "libiberty.h"
41 #include "safe-ctype.h"
42 #include "filenames.h"
43 #include "getopt.h"
44 #include <fcntl.h>
45
46 #include "bfdlink.h"
47
48 #include "ld.h"
49 #include "ldmain.h"
50 #include "ldmisc.h"
51 #include "ldexp.h"
52 #include "ldlang.h"
53 #include "ldfile.h"
54 #include "ldemul.h"
55 #include "ldbuildid.h"
56 #include <ldgram.h>
57 #include "elf/common.h"
58 #include "elf-bfd.h"
59 #include "filenames.h"
60
61 /* Declare functions used by various EXTRA_EM_FILEs. */
62 static void gld${EMULATION_NAME}_before_parse (void);
63 static void gld${EMULATION_NAME}_after_parse (void);
64 static void gld${EMULATION_NAME}_after_open (void);
65 static void gld${EMULATION_NAME}_before_allocation (void);
66 static void gld${EMULATION_NAME}_after_allocation (void);
67 static lang_output_section_statement_type *gld${EMULATION_NAME}_place_orphan
68 (asection *, const char *, int);
69 EOF
70
71 if [ "x${USE_LIBPATH}" = xyes ] ; then
72 case ${target} in
73 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
74 fragment <<EOF
75 #ifdef HAVE_GLOB
76 #include <glob.h>
77 #endif
78 EOF
79 ;;
80 esac
81 fi
82
83 # Import any needed special functions and/or overrides.
84 #
85 source_em ${srcdir}/emultempl/elf-generic.em
86 if test -n "$EXTRA_EM_FILE" ; then
87 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
88 fi
89
90 # Functions in this file can be overridden by setting the LDEMUL_* shell
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 #
96 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
97 fragment <<EOF
98
99 static void
100 gld${EMULATION_NAME}_before_parse (void)
101 {
102 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
103 input_flags.dynamic = ${DYNAMIC_LINK-TRUE};
104 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
105 config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
106 `if test -n "$CALL_NOP_BYTE" ; then echo link_info.call_nop_byte = $CALL_NOP_BYTE; fi`;
107 link_info.check_relocs_after_open_input = `if test "x${CHECK_RELOCS_AFTER_OPEN_INPUT}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
108 link_info.relro = DEFAULT_LD_Z_RELRO;
109 }
110
111 EOF
112 fi
113
114 if test x"$LDEMUL_AFTER_PARSE" != xgld"$EMULATION_NAME"_after_parse; then
115 fragment <<EOF
116
117 static void
118 gld${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
126 EOF
127 fi
128
129 if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
130 fragment <<EOF
131 /* Handle the generation of DT_NEEDED tags. */
132
133 static bfd_boolean
134 gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
135 {
136 int link_class = 0;
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. */
141 if (entry->flags.add_DT_NEEDED_for_regular)
142 link_class = DYN_AS_NEEDED;
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. */
147 if (!entry->flags.add_DT_NEEDED_for_dynamic)
148 link_class |= DYN_NO_ADD_NEEDED;
149
150 if (entry->flags.just_syms
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
155 if (link_class == 0
156 || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
157 return FALSE;
158
159 bfd_elf_set_dyn_lib_class (entry->the_bfd,
160 (enum dynamic_lib_link_class) link_class);
161
162 /* Continue on with normal load_symbols processing. */
163 return FALSE;
164 }
165 EOF
166 fi
167
168 fragment <<EOF
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
173 static struct bfd_link_needed_list *global_needed;
174 static struct stat global_stat;
175 static lang_input_statement_type *global_found;
176 static struct bfd_link_needed_list *global_vercheck_needed;
177 static bfd_boolean global_vercheck_failed;
178
179 /* These variables are used to implement target options */
180
181 static char *audit; /* colon (typically) separated list of libs */
182 static char *depaudit; /* colon (typically) separated list of libs */
183
184 /* Style of .note.gnu.build-id section. */
185 static const char *emit_note_gnu_build_id;
186
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.
192
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.
198
199 This is called via lang_for_each_input_file.
200 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
201 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
202 a conflicting version. */
203
204 static void
205 gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
206 {
207 const char *soname;
208 struct bfd_link_needed_list *l;
209
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;
215
216 soname = bfd_elf_get_dt_soname (s->the_bfd);
217 if (soname == NULL)
218 soname = lbasename (bfd_get_filename (s->the_bfd));
219
220 for (l = global_vercheck_needed; l != NULL; l = l->next)
221 {
222 const char *suffix;
223
224 if (filename_cmp (soname, l->name) == 0)
225 {
226 /* Probably can't happen, but it's an easy check. */
227 continue;
228 }
229
230 if (strchr (l->name, '/') != NULL)
231 continue;
232
233 suffix = strstr (l->name, ".so.");
234 if (suffix == NULL)
235 continue;
236
237 suffix += sizeof ".so." - 1;
238
239 if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
240 {
241 /* Here we know that S is a dynamic object FOO.SO.VER1, and
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. */
246 global_vercheck_failed = TRUE;
247 return;
248 }
249 }
250 }
251
252
253 /* See if an input file matches a DT_NEEDED entry by running stat on
254 the file. */
255
256 static void
257 gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
258 {
259 struct stat st;
260 const char *suffix;
261 const char *soname;
262
263 if (global_found != NULL)
264 return;
265 if (s->the_bfd == NULL)
266 return;
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)
271 return;
272
273 if (bfd_stat (s->the_bfd, &st) != 0)
274 {
275 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
276 return;
277 }
278
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. */
286 if (st.st_dev == global_stat.st_dev
287 && st.st_ino == global_stat.st_ino
288 && st.st_ino != 0)
289 {
290 global_found = s;
291 return;
292 }
293
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
298 heuristic test, and it will only work if the name looks like
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. */
302
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;
309
310 soname = bfd_elf_get_dt_soname (s->the_bfd);
311 if (soname == NULL)
312 soname = lbasename (s->filename);
313
314 if (filename_ncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
315 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
316 global_needed->name, global_needed->by, soname);
317 }
318
319 struct dt_needed
320 {
321 bfd *by;
322 const char *name;
323 };
324
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
329 static bfd_boolean
330 gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
331 int force)
332 {
333 bfd *abfd;
334 const char *name = needed->name;
335 const char *soname;
336 int link_class;
337
338 abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
339 if (abfd == NULL)
340 {
341 if (verbose)
342 info_msg (_("attempt to open %s failed\n"), name);
343 return FALSE;
344 }
345
346 /* Linker needs to decompress sections. */
347 abfd->flags |= BFD_DECOMPRESS;
348
349 if (! bfd_check_format (abfd, bfd_object))
350 {
351 bfd_close (abfd);
352 return FALSE;
353 }
354 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
355 {
356 bfd_close (abfd);
357 return FALSE;
358 }
359
360 /* For DT_NEEDED, they have to match. */
361 if (abfd->xvec != link_info.output_bfd->xvec)
362 {
363 bfd_close (abfd);
364 return FALSE;
365 }
366
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 {
374 struct bfd_link_needed_list *needs;
375
376 if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
377 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
378
379 if (needs != NULL)
380 {
381 global_vercheck_needed = needs;
382 global_vercheck_failed = FALSE;
383 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
384 if (global_vercheck_failed)
385 {
386 bfd_close (abfd);
387 /* Return FALSE to force the caller to move on to try
388 another file on the search path. */
389 return FALSE;
390 }
391
392 /* But wait! It gets much worse. On Linux, if a shared
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. */
398
399 EOF
400 case ${target} in
401 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
402 fragment <<EOF
403 {
404 struct bfd_link_needed_list *l;
405
406 for (l = needs; l != NULL; l = l->next)
407 if (CONST_STRNEQ (l->name, "libc.so"))
408 break;
409 if (l == NULL)
410 {
411 bfd_close (abfd);
412 return FALSE;
413 }
414 }
415
416 EOF
417 ;;
418 esac
419 fragment <<EOF
420 }
421 }
422
423 /* We've found a dynamic object matching the DT_NEEDED entry. */
424
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);
435
436 /* First strip off everything before the last '/'. */
437 soname = lbasename (abfd->filename);
438
439 if (verbose)
440 info_msg (_("found %s at %s\n"), soname, name);
441
442 global_found = NULL;
443 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
444 if (global_found != NULL)
445 {
446 /* Return TRUE to indicate that we found the file, even though
447 we aren't going to do anything with it. */
448 return TRUE;
449 }
450
451 /* Specify the soname to use. */
452 bfd_elf_set_dt_needed_name (abfd, soname);
453
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. */
457 link_class = DYN_DT_NEEDED;
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. */
462 if (needed->by != NULL
463 && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
464 link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
465
466 bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
467
468 /* Add this file into the symbol table. */
469 if (! bfd_link_add_symbols (abfd, &link_info))
470 einfo ("%F%B: error adding symbols: %E\n", abfd);
471
472 return TRUE;
473 }
474
475
476 /* Search for a needed file in a path. */
477
478 static bfd_boolean
479 gld${EMULATION_NAME}_search_needed (const char *path,
480 struct dt_needed *n, int force)
481 {
482 const char *s;
483 const char *name = n->name;
484 size_t len;
485 struct dt_needed needed;
486
487 if (name[0] == '/')
488 return gld${EMULATION_NAME}_try_needed (n, force);
489
490 if (path == NULL || *path == '\0')
491 return FALSE;
492
493 needed.by = n->by;
494 needed.name = n->name;
495
496 len = strlen (name);
497 while (1)
498 {
499 char *filename, *sset;
500
501 s = strchr (path, config.rpath_separator);
502 if (s == NULL)
503 s = path + strlen (path);
504
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
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
527 needed.name = filename;
528 if (gld${EMULATION_NAME}_try_needed (&needed, force))
529 return TRUE;
530
531 free (filename);
532
533 if (*s == '\0')
534 break;
535 path = s + 1;
536 }
537
538 return FALSE;
539 }
540
541 EOF
542 if [ "x${USE_LIBPATH}" = xyes ] ; then
543 fragment <<EOF
544
545 /* Add the sysroot to every entry in a path separated by
546 config.rpath_separator. */
547
548 static char *
549 gld${EMULATION_NAME}_add_sysroot (const char *path)
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])
558 if (path[i++] == config.rpath_separator)
559 colons++;
560
561 if (path[i])
562 colons++;
563
564 len = len + (colons + 1) * strlen (ld_sysroot);
565 ret = xmalloc (len + 1);
566 strcpy (ret, ld_sysroot);
567 p = ret + strlen (ret);
568 i = 0;
569 while (path[i])
570 if (path[i] == config.rpath_separator)
571 {
572 *p++ = path[i++];
573 strcpy (p, ld_sysroot);
574 p = p + strlen (p);
575 }
576 else
577 *p++ = path[i++];
578
579 *p = 0;
580 return ret;
581 }
582
583 EOF
584 case ${target} in
585 *-*-freebsd* | *-*-dragonfly*)
586 fragment <<EOF
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
594 static bfd_boolean
595 gld${EMULATION_NAME}_check_ld_elf_hints (const struct bfd_link_needed_list *l,
596 int force)
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
607 tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
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);
626
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
639 needed.by = l->by;
640 needed.name = l->name;
641 return gld${EMULATION_NAME}_search_needed (ld_elf_hints, &needed, force);
642 }
643 EOF
644 # FreeBSD
645 ;;
646
647 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
648 fragment <<EOF
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
651 only meaningful on Linux. */
652
653 struct gld${EMULATION_NAME}_ld_so_conf
654 {
655 char *path;
656 size_t len, alloc;
657 };
658
659 static bfd_boolean
660 gld${EMULATION_NAME}_parse_ld_so_conf
661 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
662
663 static void
664 gld${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;
669 #ifdef HAVE_GLOB
670 glob_t gl;
671 #endif
672
673 if (pattern[0] != '/')
674 {
675 char *p = strrchr (filename, '/');
676 size_t patlen = strlen (pattern) + 1;
677
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 }
683
684 #ifdef HAVE_GLOB
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 }
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
697
698 if (newp)
699 free (newp);
700 }
701
702 static bfd_boolean
703 gld${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);
707 char *line;
708 size_t linelen;
709
710 if (f == NULL)
711 return FALSE;
712
713 linelen = 256;
714 line = xmalloc (linelen);
715 do
716 {
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;
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;
751
752 if (CONST_STRNEQ (p, "include") && (p[7] == ' ' || p[7] == '\t'))
753 {
754 char *dir, c;
755 p += 8;
756 do
757 {
758 while (*p == ' ' || *p == '\t')
759 ++p;
760
761 if (*p == '\0')
762 break;
763
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 }
792 else
793 {
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 }
799 info->path[info->len++] = config.rpath_separator;
800 }
801 memcpy (info->path + info->len, dir, p - dir);
802 info->len += p - dir;
803 info->path[info->len] = '\0';
804 }
805 }
806 while (! feof (f));
807 free (line);
808 fclose (f);
809 return TRUE;
810 }
811
812 static bfd_boolean
813 gld${EMULATION_NAME}_check_ld_so_conf (const struct bfd_link_needed_list *l,
814 int force)
815 {
816 static bfd_boolean initialized;
817 static char *ld_so_conf;
818 struct dt_needed needed;
819
820 if (! initialized)
821 {
822 char *tmppath;
823 struct gld${EMULATION_NAME}_ld_so_conf info;
824
825 info.path = NULL;
826 info.len = info.alloc = 0;
827 tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf",
828 (const char *) NULL);
829 if (!gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath))
830 {
831 free (tmppath);
832 tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
833 (const char *) NULL);
834 gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
835 }
836 free (tmppath);
837
838 if (info.path)
839 {
840 char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
841 free (info.path);
842 ld_so_conf = d;
843 }
844 initialized = TRUE;
845 }
846
847 if (ld_so_conf == NULL)
848 return FALSE;
849
850
851 needed.by = l->by;
852 needed.name = l->name;
853 return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
854 }
855
856 EOF
857 # Linux
858 ;;
859 esac
860 fi
861 fragment <<EOF
862
863 /* See if an input file matches a DT_NEEDED entry by name. */
864
865 static void
866 gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
867 {
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)
874 return;
875
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)
882 return;
883
884 if (filename_cmp (s->filename, global_needed->name) == 0)
885 {
886 global_found = s;
887 return;
888 }
889
890 if (s->flags.search_dirs)
891 {
892 const char *f = strrchr (s->filename, '/');
893 if (f != NULL
894 && filename_cmp (f + 1, global_needed->name) == 0)
895 {
896 global_found = s;
897 return;
898 }
899 }
900
901 soname = bfd_elf_get_dt_soname (s->the_bfd);
902 if (soname != NULL
903 && filename_cmp (soname, global_needed->name) == 0)
904 {
905 global_found = s;
906 return;
907 }
908 }
909
910 EOF
911
912 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
913 fragment <<EOF
914
915 static bfd_size_type
916 id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED)
917 {
918 const char *style = emit_note_gnu_build_id;
919 bfd_size_type size;
920 bfd_size_type build_id_size;
921
922 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
923 size = (size + 3) & -(bfd_size_type) 4;
924
925 build_id_size = compute_build_id_size (style);
926 if (build_id_size)
927 size += build_id_size;
928 else
929 size = 0;
930
931 return size;
932 }
933
934 static bfd_boolean
935 write_build_id (bfd *abfd)
936 {
937 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
938 struct elf_obj_tdata *t = elf_tdata (abfd);
939 const char *style;
940 asection *asec;
941 Elf_Internal_Shdr *i_shdr;
942 unsigned char *contents, *id_bits;
943 bfd_size_type size;
944 file_ptr position;
945 Elf_External_Note *e_note;
946
947 style = t->o->build_id.style;
948 asec = t->o->build_id.sec;
949 if (bfd_is_abs_section (asec->output_section))
950 {
951 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
952 " --build-id ignored.\n"));
953 return TRUE;
954 }
955 i_shdr = &elf_section_data (asec->output_section)->this_hdr;
956
957 if (i_shdr->contents == NULL)
958 {
959 if (asec->contents == NULL)
960 asec->contents = (unsigned char *) xmalloc (asec->size);
961 contents = asec->contents;
962 }
963 else
964 contents = i_shdr->contents + asec->output_offset;
965
966 e_note = (Elf_External_Note *) contents;
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
977 generate_build_id (abfd, style, bed->s->checksum_contents, id_bits, size);
978
979 position = i_shdr->sh_offset + asec->output_offset;
980 size = asec->size;
981 return (bfd_seek (abfd, position, SEEK_SET) == 0
982 && bfd_bwrite (contents, size, abfd) == size);
983 }
984
985 /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
986
987 static bfd_boolean
988 setup_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);
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;
1013 }
1014
1015 einfo ("%P: warning: Cannot create .note.gnu.build-id section,"
1016 " --build-id ignored.\n");
1017 return FALSE;
1018 }
1019
1020 /* This is called after all the input files have been opened. */
1021
1022 static void
1023 gld${EMULATION_NAME}_after_open (void)
1024 {
1025 struct bfd_link_needed_list *needed, *l;
1026 struct elf_link_hash_table *htab;
1027
1028 after_open_default ();
1029
1030 htab = elf_hash_table (&link_info);
1031 if (!is_elf_hash_table (htab))
1032 return;
1033
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
1048 if (emit_note_gnu_build_id != NULL)
1049 {
1050 bfd *abfd;
1051
1052 /* Find an ELF input. */
1053 for (abfd = link_info.input_bfds;
1054 abfd != (bfd *) NULL; abfd = abfd->link.next)
1055 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1056 && bfd_count_sections (abfd) != 0)
1057 break;
1058
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))
1063 {
1064 free ((char *) emit_note_gnu_build_id);
1065 emit_note_gnu_build_id = NULL;
1066 }
1067 }
1068
1069 if (bfd_link_relocatable (&link_info))
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 }
1084
1085 if (!link_info.traditional_format)
1086 {
1087 bfd *abfd, *elfbfd = NULL;
1088 bfd_boolean warn_eh_frame = FALSE;
1089 asection *s;
1090 int seen_type = 0;
1091
1092 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1093 {
1094 int type = 0;
1095 for (s = abfd->sections; s && type < COMPACT_EH_HDR; s = s->next)
1096 {
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;
1105 }
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;
1136 }
1137 if (elfbfd)
1138 {
1139 const struct elf_backend_data *bed;
1140
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);
1145 if (s != NULL
1146 && bfd_set_section_alignment (elfbfd, s, 2))
1147 {
1148 htab->eh_info.hdr_sec = s;
1149 warn_eh_frame = FALSE;
1150 }
1151 }
1152 if (warn_eh_frame)
1153 einfo ("%P: warning: Cannot create .eh_frame_hdr section,"
1154 " --eh-frame-hdr ignored.\n");
1155 }
1156
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. */
1166 needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info);
1167 for (l = needed; l != NULL; l = l->next)
1168 {
1169 struct bfd_link_needed_list *ll;
1170 struct dt_needed n, nn;
1171 int force;
1172
1173 /* If the lib that needs this one was --as-needed and wasn't
1174 found to be needed, then this lib isn't needed either. */
1175 if (l->by != NULL
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)
1184 continue;
1185
1186 /* If we've already seen this file, skip it. */
1187 for (ll = needed; ll != l; ll = ll->next)
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)
1191 break;
1192 if (ll != l)
1193 continue;
1194
1195 /* See if this file was included in the link explicitly. */
1196 global_needed = l;
1197 global_found = NULL;
1198 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
1199 if (global_found != NULL
1200 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1201 & DYN_AS_NEEDED) == 0)
1202 continue;
1203
1204 n.by = l->by;
1205 n.name = l->name;
1206 nn.by = l->by;
1207 if (verbose)
1208 info_msg (_("%s needed by %B\n"), l->name, l->by);
1209
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
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
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.
1226
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 {
1232 size_t len;
1233 search_dirs_type *search;
1234 EOF
1235 if [ "x${NATIVE}" = xyes ] ; then
1236 fragment <<EOF
1237 const char *lib_path;
1238 EOF
1239 fi
1240 if [ "x${USE_LIBPATH}" = xyes ] ; then
1241 fragment <<EOF
1242 struct bfd_link_needed_list *rp;
1243 int found;
1244 EOF
1245 fi
1246 fragment <<EOF
1247
1248 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
1249 &n, force))
1250 break;
1251 EOF
1252 if [ "x${USE_LIBPATH}" = xyes ] ; then
1253 fragment <<EOF
1254 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
1255 &n, force))
1256 break;
1257 EOF
1258 fi
1259 if [ "x${NATIVE}" = xyes ] ; then
1260 fragment <<EOF
1261 if (command_line.rpath_link == NULL
1262 && command_line.rpath == NULL)
1263 {
1264 lib_path = (const char *) getenv ("LD_RUN_PATH");
1265 if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
1266 force))
1267 break;
1268 }
1269 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
1270 if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1271 break;
1272 EOF
1273 fi
1274 if [ "x${USE_LIBPATH}" = xyes ] ; then
1275 fragment <<EOF
1276 found = 0;
1277 rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
1278 for (; !found && rp != NULL; rp = rp->next)
1279 {
1280 const char *tmpname = rp->name;
1281
1282 if (IS_ABSOLUTE_PATH (tmpname))
1283 tmpname = gld${EMULATION_NAME}_add_sysroot (tmpname);
1284 found = (rp->by == l->by
1285 && gld${EMULATION_NAME}_search_needed (tmpname,
1286 &n,
1287 force));
1288 if (tmpname != rp->name)
1289 free ((char *) tmpname);
1290 }
1291 if (found)
1292 break;
1293
1294 EOF
1295 fi
1296 if [ "x${USE_LIBPATH}" = xyes ] ; then
1297 case ${target} in
1298 *-*-freebsd* | *-*-dragonfly*)
1299 fragment <<EOF
1300 if (gld${EMULATION_NAME}_check_ld_elf_hints (l, force))
1301 break;
1302 EOF
1303 # FreeBSD
1304 ;;
1305
1306 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
1307 fragment <<EOF
1308 if (gld${EMULATION_NAME}_check_ld_so_conf (l, force))
1309 break;
1310
1311 EOF
1312 # Linux
1313 ;;
1314 esac
1315 fi
1316 fragment <<EOF
1317 len = strlen (l->name);
1318 for (search = search_head; search != NULL; search = search->next)
1319 {
1320 char *filename;
1321
1322 if (search->cmdline)
1323 continue;
1324 filename = (char *) xmalloc (strlen (search->name) + len + 2);
1325 sprintf (filename, "%s/%s", search->name, l->name);
1326 nn.name = filename;
1327 if (gld${EMULATION_NAME}_try_needed (&nn, force))
1328 break;
1329 free (filename);
1330 }
1331 if (search != NULL)
1332 break;
1333 EOF
1334 fragment <<EOF
1335 }
1336
1337 if (force < 2)
1338 continue;
1339
1340 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
1341 l->name, l->by);
1342 }
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"));
1347 }
1348
1349 EOF
1350 fi
1351
1352 fragment <<EOF
1353
1354 /* Look through an expression for an assignment statement. */
1355
1356 static void
1357 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1358 {
1359 bfd_boolean provide = FALSE;
1360
1361 switch (exp->type.node_class)
1362 {
1363 case etree_provide:
1364 case etree_provided:
1365 provide = TRUE;
1366 /* Fall thru */
1367 case etree_assign:
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. */
1375 if (strcmp (exp->assign.dst, ".") != 0)
1376 {
1377 if (!bfd_elf_record_link_assignment (link_info.output_bfd,
1378 &link_info,
1379 exp->assign.dst, provide,
1380 exp->assign.hidden))
1381 einfo ("%P%F: failed to record assignment to %s: %E\n",
1382 exp->assign.dst);
1383 }
1384 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1385 break;
1386
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;
1391
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;
1397
1398 case etree_unary:
1399 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1400 break;
1401
1402 default:
1403 break;
1404 }
1405 }
1406
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
1413 static void
1414 gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
1415 {
1416 if (s->header.type == lang_assignment_statement_enum)
1417 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1418 }
1419
1420 EOF
1421
1422 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
1423 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
1424 ELF_INTERPRETER_SET_DEFAULT="
1425 if (sinterp != NULL)
1426 {
1427 sinterp->contents = (unsigned char *) ${ELF_INTERPRETER_NAME};
1428 sinterp->size = strlen ((char *) sinterp->contents) + 1;
1429 }
1430
1431 "
1432 else
1433 ELF_INTERPRETER_SET_DEFAULT=
1434 fi
1435 fragment <<EOF
1436
1437 /* used by before_allocation and handle_option. */
1438 static void
1439 gld${EMULATION_NAME}_append_to_separated_string (char **to, char *op_arg)
1440 {
1441 if (*to == NULL)
1442 *to = xstrdup (op_arg);
1443 else
1444 {
1445 size_t to_len = strlen (*to);
1446 size_t op_arg_len = strlen (op_arg);
1447 char *buf;
1448 char *cp = *to;
1449
1450 /* First see whether OPTARG is already in the path. */
1451 do
1452 {
1453 if (strncmp (op_arg, cp, op_arg_len) == 0
1454 && (cp[op_arg_len] == 0
1455 || cp[op_arg_len] == config.rpath_separator))
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 {
1468 buf = xmalloc (to_len + op_arg_len + 2);
1469 sprintf (buf, "%s%c%s", *to,
1470 config.rpath_separator, op_arg);
1471 free (*to);
1472 *to = buf;
1473 }
1474 }
1475 }
1476
1477 #if defined(__GNUC__) && GCC_VERSION < 4006
1478 /* Work around a GCC uninitialized warning bug fixed in GCC 4.6. */
1479 static struct bfd_link_hash_entry ehdr_start_empty;
1480 #endif
1481
1482 /* This is called after the sections have been attached to output
1483 sections, but before any sizes or addresses have been set. */
1484
1485 static void
1486 gld${EMULATION_NAME}_before_allocation (void)
1487 {
1488 const char *rpath;
1489 asection *sinterp;
1490 bfd *abfd;
1491 struct elf_link_hash_entry *ehdr_start = NULL;
1492 #if defined(__GNUC__) && GCC_VERSION < 4006
1493 /* Work around a GCC uninitialized warning bug fixed in GCC 4.6. */
1494 struct bfd_link_hash_entry ehdr_start_save = ehdr_start_empty;
1495 #else
1496 struct bfd_link_hash_entry ehdr_start_save;
1497 #endif
1498
1499 if (is_elf_hash_table (link_info.hash))
1500 {
1501 _bfd_elf_tls_setup (link_info.output_bfd, &link_info);
1502
1503 /* Make __ehdr_start hidden if it has been referenced, to
1504 prevent the symbol from being dynamic. */
1505 if (!bfd_link_relocatable (&link_info))
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;
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;
1532 }
1533 }
1534
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 }
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");
1546
1547 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1548 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1549 {
1550 const char *audit_libs = elf_dt_audit (abfd);
1551
1552 /* If the input bfd contains an audit entry, we need to add it as
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);
1561
1562 if (cp2)
1563 {
1564 *cp2 = '\0';
1565 more = 1;
1566 }
1567
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 }
1576
1577 if (! (bfd_elf_size_dynamic_sections
1578 (link_info.output_bfd, command_line.soname, rpath,
1579 command_line.filter_shlib, audit, depaudit,
1580 (const char * const *) command_line.auxiliary_filters,
1581 &link_info, &sinterp)))
1582 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1583
1584 ${ELF_INTERPRETER_SET_DEFAULT}
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;
1590 sinterp->size = strlen (command_line.interpreter) + 1;
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;
1604
1605 if (is->flags.just_syms)
1606 continue;
1607
1608 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1609 if (s == NULL)
1610 continue;
1611
1612 sz = s->size;
1613 msg = (char *) xmalloc ((size_t) (sz + 1));
1614 if (! bfd_get_section_contents (is->the_bfd, s, msg,
1615 (file_ptr) 0, sz))
1616 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1617 is->the_bfd);
1618 msg[sz] = '\0';
1619 (*link_info.callbacks->warning) (&link_info, msg,
1620 (const char *) NULL, is->the_bfd,
1621 (asection *) NULL, (bfd_vma) 0);
1622 free (msg);
1623
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
1633 s->size = 0;
1634
1635 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1636 warning section don't get copied to the output. */
1637 s->flags |= SEC_EXCLUDE | SEC_KEEP;
1638 }
1639 }
1640
1641 before_allocation_default ();
1642
1643 if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info))
1644 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
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 }
1653 }
1654
1655 EOF
1656 fi
1657
1658 if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1659 fragment <<EOF
1660
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
1665 static bfd_boolean
1666 gld${EMULATION_NAME}_open_dynamic_archive
1667 (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
1668 {
1669 const char *filename;
1670 char *string;
1671 size_t len;
1672 bfd_boolean opened = FALSE;
1673
1674 if (! entry->flags.maybe_archive)
1675 return FALSE;
1676
1677 filename = entry->filename;
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;
1688
1689 len += strlen (arch) + sizeof "/lib.so";
1690 #ifdef EXTRA_SHLIB_EXTENSION
1691 xlen = (strlen (EXTRA_SHLIB_EXTENSION) > 3
1692 ? strlen (EXTRA_SHLIB_EXTENSION) - 3
1693 : 0);
1694 #endif
1695 string = (char *) xmalloc (len + xlen);
1696 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1697 #ifdef EXTRA_SHLIB_EXTENSION
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);
1703 #endif
1704 }
1705
1706 if (!opened && !ldfile_try_open_bfd (string, entry))
1707 {
1708 free (string);
1709 return FALSE;
1710 }
1711
1712 entry->filename = string;
1713
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.
1723
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 {
1730 ASSERT (entry->flags.maybe_archive && entry->flags.search_dirs);
1731
1732 /* Rather than duplicating the logic above. Just use the
1733 filename we recorded earlier. */
1734
1735 if (!entry->flags.full_name_provided)
1736 filename = lbasename (entry->filename);
1737 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1738 }
1739
1740 return TRUE;
1741 }
1742
1743 EOF
1744 fi
1745
1746 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1747 fragment <<EOF
1748
1749 /* A variant of lang_output_section_find used by place_orphan. */
1750
1751 static lang_output_section_statement_type *
1752 output_rel_find (asection *sec, int isdyn)
1753 {
1754 lang_output_section_statement_type *lookup;
1755 lang_output_section_statement_type *last = NULL;
1756 lang_output_section_statement_type *last_alloc = NULL;
1757 lang_output_section_statement_type *last_ro_alloc = NULL;
1758 lang_output_section_statement_type *last_rel = NULL;
1759 lang_output_section_statement_type *last_rel_alloc = NULL;
1760 int rela = sec->name[4] == 'a';
1761
1762 for (lookup = &lang_output_section_statement.head->output_section_statement;
1763 lookup != NULL;
1764 lookup = lookup->next)
1765 {
1766 if (lookup->constraint >= 0
1767 && CONST_STRNEQ (lookup->name, ".rel"))
1768 {
1769 int lookrela = lookup->name[4] == 'a';
1770
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)
1779 break;
1780
1781 if (rela == lookrela || last_rel == NULL)
1782 last_rel = lookup;
1783 if ((rela == lookrela || last_rel_alloc == NULL)
1784 && lookup->bfd_section != NULL
1785 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1786 last_rel_alloc = lookup;
1787 }
1788
1789 last = lookup;
1790 if (lookup->bfd_section != NULL
1791 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1792 {
1793 last_alloc = lookup;
1794 if ((lookup->bfd_section->flags & SEC_READONLY) != 0)
1795 last_ro_alloc = lookup;
1796 }
1797 }
1798
1799 if (last_rel_alloc)
1800 return last_rel_alloc;
1801
1802 if (last_rel)
1803 return last_rel;
1804
1805 if (last_ro_alloc)
1806 return last_ro_alloc;
1807
1808 if (last_alloc)
1809 return last_alloc;
1810
1811 return last;
1812 }
1813
1814 /* Place an orphan section. We use this to put random SHF_ALLOC
1815 sections in the right segment. */
1816
1817 static lang_output_section_statement_type *
1818 gld${EMULATION_NAME}_place_orphan (asection *s,
1819 const char *secname,
1820 int constraint)
1821 {
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 },
1830 { ".tdata",
1831 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_THREAD_LOCAL,
1832 0, 0, 0, 0 },
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,
1847 0, 0, 0, 0 },
1848 { ".comment",
1849 SEC_HAS_CONTENTS,
1850 0, 0, 0, 0 },
1851 };
1852 enum orphan_save_index
1853 {
1854 orphan_text = 0,
1855 orphan_rodata,
1856 orphan_tdata,
1857 orphan_data,
1858 orphan_bss,
1859 orphan_rel,
1860 orphan_interp,
1861 orphan_sdata,
1862 orphan_nonalloc
1863 };
1864 static int orphan_init_done = 0;
1865 struct orphan_save *place;
1866 lang_output_section_statement_type *after;
1867 lang_output_section_statement_type *os;
1868 lang_output_section_statement_type *match_by_name = NULL;
1869 int isdyn = 0;
1870 int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
1871 unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
1872 flagword flags;
1873 asection *nexts;
1874
1875 if (!bfd_link_relocatable (&link_info)
1876 && link_info.combreloc
1877 && (s->flags & SEC_ALLOC))
1878 {
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 }
1893 else if (CONST_STRNEQ (secname, ".rel"))
1894 {
1895 secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1896 isdyn = 1;
1897 }
1898 }
1899
1900 /* Look through the script to see where to place this section. */
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
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. */
1913 if (os->bfd_section != NULL
1914 && (os->bfd_section->flags == 0
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)
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))))
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. */
1930 lang_add_section (&os->children, s, NULL, os);
1931 return os;
1932 }
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;
1938 }
1939
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 {
1944 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1945 return match_by_name;
1946 }
1947
1948 if (!orphan_init_done)
1949 {
1950 struct orphan_save *ho;
1951
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 }
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. */
1964 if (bfd_link_executable (&link_info)
1965 && CONST_STRNEQ (s->name, ".gnu.warning.")
1966 && hold[orphan_text].os != NULL)
1967 {
1968 os = hold[orphan_text].os;
1969 lang_add_section (&os->children, s, NULL, os);
1970 return os;
1971 }
1972
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
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. */
1998
1999 place = NULL;
2000 if ((flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
2001 place = &hold[orphan_nonalloc];
2002 else if ((flags & SEC_ALLOC) == 0)
2003 ;
2004 else if ((flags & SEC_LOAD) != 0
2005 && ((iself && sh_type == SHT_NOTE)
2006 || (!iself && CONST_STRNEQ (secname, ".note"))))
2007 place = &hold[orphan_interp];
2008 else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
2009 place = &hold[orphan_bss];
2010 else if ((flags & SEC_SMALL_DATA) != 0)
2011 place = &hold[orphan_sdata];
2012 else if ((flags & SEC_THREAD_LOCAL) != 0)
2013 place = &hold[orphan_tdata];
2014 else if ((flags & SEC_READONLY) == 0)
2015 place = &hold[orphan_data];
2016 else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
2017 || (!iself && CONST_STRNEQ (secname, ".rel")))
2018 && (flags & SEC_LOAD) != 0)
2019 place = &hold[orphan_rel];
2020 else if ((flags & SEC_CODE) == 0)
2021 place = &hold[orphan_rodata];
2022 else
2023 place = &hold[orphan_text];
2024
2025 after = NULL;
2026 if (place != NULL)
2027 {
2028 if (place->os == NULL)
2029 {
2030 if (place->name != NULL)
2031 place->os = lang_output_section_find (place->name);
2032 else
2033 place->os = output_rel_find (s, isdyn);
2034 }
2035 after = place->os;
2036 if (after == NULL)
2037 after
2038 = lang_output_section_find_by_flags (s, flags, &place->os,
2039 _bfd_elf_match_sections_by_type);
2040 if (after == NULL)
2041 /* *ABS* is always the first output section statement. */
2042 after = &lang_output_section_statement.head->output_section_statement;
2043 }
2044
2045 return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
2046 }
2047 EOF
2048 fi
2049
2050 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
2051 fragment <<EOF
2052
2053 static void
2054 gld${EMULATION_NAME}_after_allocation (void)
2055 {
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);
2062 }
2063 EOF
2064 fi
2065
2066 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
2067 fragment <<EOF
2068
2069 static char *
2070 gld${EMULATION_NAME}_get_script (int *isfile)
2071 EOF
2072
2073 if test x"$COMPILE_IN" = xyes
2074 then
2075 # Scripts compiled in.
2076
2077 # sed commands to quote an ld script as a C string.
2078 sc="-f stringify.sed"
2079
2080 fragment <<EOF
2081 {
2082 *isfile = 0;
2083
2084 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2085 return
2086 EOF
2087 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2088 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2089 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2090 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2091 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2092 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
2093 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2094 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2095 fi
2096 if test -n "$GENERATE_PIE_SCRIPT" ; then
2097 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2098 echo ' ; else if (bfd_link_pie (&link_info)' >> e${EMULATION_NAME}.c
2099 echo ' && link_info.combreloc' >> e${EMULATION_NAME}.c
2100 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2101 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2102 sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c
2103 echo ' ; else if (bfd_link_pie (&link_info)' >> e${EMULATION_NAME}.c
2104 echo ' && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2105 sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c
2106 fi
2107 echo ' ; else if (bfd_link_pie (&link_info)) return' >> e${EMULATION_NAME}.c
2108 sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c
2109 fi
2110 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2111 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2112 echo ' ; else if (bfd_link_dll (&link_info) && link_info.combreloc' >> e${EMULATION_NAME}.c
2113 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2114 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2115 sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c
2116 echo ' ; else if (bfd_link_dll (&link_info) && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2117 sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c
2118 fi
2119 echo ' ; else if (bfd_link_dll (&link_info)) return' >> e${EMULATION_NAME}.c
2120 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
2121 fi
2122 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2123 echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c
2124 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2125 sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c
2126 echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c
2127 sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c
2128 fi
2129 echo ' ; else return' >> e${EMULATION_NAME}.c
2130 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2131 echo '; }' >> e${EMULATION_NAME}.c
2132
2133 else
2134 # Scripts read from the filesystem.
2135
2136 fragment <<EOF
2137 {
2138 *isfile = 1;
2139
2140 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2141 return "ldscripts/${EMULATION_NAME}.xu";
2142 else if (bfd_link_relocatable (&link_info))
2143 return "ldscripts/${EMULATION_NAME}.xr";
2144 else if (!config.text_read_only)
2145 return "ldscripts/${EMULATION_NAME}.xbn";
2146 EOF
2147 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
2148 else
2149 fragment <<EOF
2150 else if (!config.magic_demand_paged)
2151 return "ldscripts/${EMULATION_NAME}.xn";
2152 EOF
2153 fi
2154 if test -n "$GENERATE_PIE_SCRIPT" ; then
2155 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2156 fragment <<EOF
2157 else if (bfd_link_pie (&link_info)
2158 && link_info.combreloc
2159 && link_info.relro
2160 && (link_info.flags & DF_BIND_NOW))
2161 return "ldscripts/${EMULATION_NAME}.xdw";
2162 else if (bfd_link_pie (&link_info)
2163 && link_info.combreloc)
2164 return "ldscripts/${EMULATION_NAME}.xdc";
2165 EOF
2166 fi
2167 fragment <<EOF
2168 else if (bfd_link_pie (&link_info))
2169 return "ldscripts/${EMULATION_NAME}.xd";
2170 EOF
2171 fi
2172 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2173 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2174 fragment <<EOF
2175 else if (bfd_link_dll (&link_info) && link_info.combreloc
2176 && link_info.relro && (link_info.flags & DF_BIND_NOW))
2177 return "ldscripts/${EMULATION_NAME}.xsw";
2178 else if (bfd_link_dll (&link_info) && link_info.combreloc)
2179 return "ldscripts/${EMULATION_NAME}.xsc";
2180 EOF
2181 fi
2182 fragment <<EOF
2183 else if (bfd_link_dll (&link_info))
2184 return "ldscripts/${EMULATION_NAME}.xs";
2185 EOF
2186 fi
2187 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2188 fragment <<EOF
2189 else if (link_info.combreloc && link_info.relro
2190 && (link_info.flags & DF_BIND_NOW))
2191 return "ldscripts/${EMULATION_NAME}.xw";
2192 else if (link_info.combreloc)
2193 return "ldscripts/${EMULATION_NAME}.xc";
2194 EOF
2195 fi
2196 fragment <<EOF
2197 else
2198 return "ldscripts/${EMULATION_NAME}.x";
2199 }
2200
2201 EOF
2202 fi
2203 fi
2204
2205 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
2206 fragment <<EOF
2207 $PARSE_AND_LIST_PROLOGUE
2208 EOF
2209 fi
2210
2211 fragment <<EOF
2212
2213 enum 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 };
2226
2227 static void
2228 gld${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)
2231 {
2232 EOF
2233 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2234 fragment <<EOF
2235 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:P:";
2236 EOF
2237 else
2238 fragment <<EOF
2239 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
2240 EOF
2241 fi
2242 fragment <<EOF
2243 static const struct option xtra_long[] = {
2244 EOF
2245 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2246 fragment <<EOF
2247 {"audit", required_argument, NULL, OPTION_AUDIT},
2248 {"Bgroup", no_argument, NULL, OPTION_GROUP},
2249 EOF
2250 fi
2251 fragment <<EOF
2252 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
2253 {"compress-debug-sections", required_argument, NULL, OPTION_COMPRESS_DEBUG},
2254 EOF
2255 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2256 fragment <<EOF
2257 {"depaudit", required_argument, NULL, 'P'},
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},
2261 {"no-eh-frame-hdr", no_argument, NULL, OPTION_NO_EH_FRAME_HDR},
2262 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
2263 {"hash-style", required_argument, NULL, OPTION_HASH_STYLE},
2264 EOF
2265 fi
2266 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
2267 fragment <<EOF
2268 $PARSE_AND_LIST_LONGOPTS
2269 EOF
2270 fi
2271 fragment <<EOF
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 }
2281
2282 #define DEFAULT_BUILD_ID_STYLE "sha1"
2283
2284 static bfd_boolean
2285 gld${EMULATION_NAME}_handle_option (int optc)
2286 {
2287 switch (optc)
2288 {
2289 default:
2290 return FALSE;
2291
2292 case OPTION_BUILD_ID:
2293 if (emit_note_gnu_build_id != NULL)
2294 {
2295 free ((char *) emit_note_gnu_build_id);
2296 emit_note_gnu_build_id = NULL;
2297 }
2298 if (optarg == NULL)
2299 optarg = DEFAULT_BUILD_ID_STYLE;
2300 if (strcmp (optarg, "none"))
2301 emit_note_gnu_build_id = xstrdup (optarg);
2302 break;
2303
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)
2308 link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
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;
2317 EOF
2318
2319 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2320 fragment <<EOF
2321 case OPTION_AUDIT:
2322 gld${EMULATION_NAME}_append_to_separated_string (&audit, optarg);
2323 break;
2324
2325 case 'P':
2326 gld${EMULATION_NAME}_append_to_separated_string (&depaudit, optarg);
2327 break;
2328
2329 case OPTION_DISABLE_NEW_DTAGS:
2330 link_info.new_dtags = FALSE;
2331 break;
2332
2333 case OPTION_ENABLE_NEW_DTAGS:
2334 link_info.new_dtags = TRUE;
2335 break;
2336
2337 case OPTION_EH_FRAME_HDR:
2338 link_info.eh_frame_hdr_type = DWARF2_EH_HDR;
2339 break;
2340
2341 case OPTION_NO_EH_FRAME_HDR:
2342 link_info.eh_frame_hdr_type = 0;
2343 break;
2344
2345 case OPTION_GROUP:
2346 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
2347 /* Groups must be self-contained. */
2348 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2349 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
2350 break;
2351
2352 case OPTION_EXCLUDE_LIBS:
2353 add_excluded_libs (optarg);
2354 break;
2355
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
2372 EOF
2373 fi
2374 fragment <<EOF
2375 case 'z':
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 }
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 }
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 }
2419 EOF
2420
2421 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2422 fragment <<EOF
2423 else if (strcmp (optarg, "global") == 0)
2424 link_info.flags_1 |= (bfd_vma) DF_1_GLOBAL;
2425 else if (strcmp (optarg, "initfirst") == 0)
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 }
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 }
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 }
2454 else if (strcmp (optarg, "combreloc") == 0)
2455 link_info.combreloc = TRUE;
2456 else if (strcmp (optarg, "nocombreloc") == 0)
2457 link_info.combreloc = FALSE;
2458 else if (strcmp (optarg, "nocopyreloc") == 0)
2459 link_info.nocopyreloc = TRUE;
2460 else if (strcmp (optarg, "relro") == 0)
2461 link_info.relro = TRUE;
2462 else if (strcmp (optarg, "norelro") == 0)
2463 link_info.relro = FALSE;
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;
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;
2474 EOF
2475 fi
2476
2477 if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
2478 fragment <<EOF
2479 $PARSE_AND_LIST_ARGS_CASE_Z
2480 EOF
2481 fi
2482
2483 fragment <<EOF
2484 else
2485 einfo (_("%P: warning: -z %s ignored.\n"), optarg);
2486 break;
2487 EOF
2488
2489 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
2490 fragment <<EOF
2491 $PARSE_AND_LIST_ARGS_CASES
2492 EOF
2493 fi
2494
2495 fragment <<EOF
2496 }
2497
2498 return TRUE;
2499 }
2500
2501 EOF
2502
2503 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
2504 gld_list_options="gld${EMULATION_NAME}_list_options"
2505 if test -n "$PARSE_AND_LIST_OPTIONS"; then
2506 fragment <<EOF
2507
2508 static void
2509 gld${EMULATION_NAME}_list_options (FILE * file)
2510 {
2511 EOF
2512
2513 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
2514 fragment <<EOF
2515 $PARSE_AND_LIST_OPTIONS
2516 EOF
2517 fi
2518
2519 fragment <<EOF
2520 }
2521 EOF
2522 else
2523 gld_list_options="NULL"
2524 fi
2525
2526 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
2527 fragment <<EOF
2528 $PARSE_AND_LIST_EPILOGUE
2529 EOF
2530 fi
2531 fi
2532
2533 fragment <<EOF
2534
2535 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2536 {
2537 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
2538 ${LDEMUL_SYSLIB-syslib_default},
2539 ${LDEMUL_HLL-hll_default},
2540 ${LDEMUL_AFTER_PARSE-gld${EMULATION_NAME}_after_parse},
2541 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
2542 ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation},
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},
2547 "${EMULATION_NAME}",
2548 "${OUTPUT_FORMAT}",
2549 ${LDEMUL_FINISH-finish_default},
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},
2554 ${LDEMUL_PARSE_ARGS-NULL},
2555 gld${EMULATION_NAME}_add_options,
2556 gld${EMULATION_NAME}_handle_option,
2557 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
2558 ${LDEMUL_LIST_OPTIONS-${gld_list_options}},
2559 ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
2560 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
2561 ${LDEMUL_NEW_VERS_PATTERN-NULL},
2562 ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}
2563 };
2564 EOF
This page took 0.079635 seconds and 5 git commands to generate.