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
5 test -z "${ELFSIZE}" && ELFSIZE=32
6 if [ -z "$MACHINE" ]; then
9 OUTPUT_ARCH=${ARCH}:${MACHINE}
12 /* This file is is generated by a shell script. DO NOT EDIT! */
14 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
15 Copyright (C) 1991-2015 Free Software Foundation, Inc.
16 Written by Steve Chamberlain <sac@cygnus.com>
17 ELF support by Ian Lance Taylor <ian@cygnus.com>
19 This file is part of the GNU Binutils.
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.
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.
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. */
36 #define TARGET_IS_${EMULATION_NAME}
40 #include "libiberty.h"
41 #include "safe-ctype.h"
42 #include "filenames.h"
55 #include "ldbuildid.h"
57 #include "elf/common.h"
59 #include "filenames.h"
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);
71 if [ "x${USE_LIBPATH}" = xyes ] ; then
73 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
83 # Import any needed special functions and/or overrides.
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
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.
96 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
100 gld${EMULATION_NAME}_before_parse (void)
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`;
111 if test x"$LDEMUL_AFTER_PARSE" != xgld"$EMULATION_NAME"_after_parse; then
115 gld${EMULATION_NAME}_after_parse (void)
117 if (bfd_link_pie (&link_info))
118 link_info.flags_1 |= (bfd_vma) DF_1_PIE;
120 after_parse_default ();
126 if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
128 /* Handle the generation of DT_NEEDED tags. */
131 gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
135 /* Tell the ELF linker that we don't want the output file to have a
136 DT_NEEDED entry for this file, unless it is used to resolve
137 references in a regular object. */
138 if (entry->flags.add_DT_NEEDED_for_regular)
139 link_class = DYN_AS_NEEDED;
141 /* Tell the ELF linker that we don't want the output file to have a
142 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
144 if (!entry->flags.add_DT_NEEDED_for_dynamic)
145 link_class |= DYN_NO_ADD_NEEDED;
147 if (entry->flags.just_syms
148 && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
149 einfo (_("%P%F: --just-symbols may not be used on DSO: %B\n"),
153 || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
156 bfd_elf_set_dyn_lib_class (entry->the_bfd,
157 (enum dynamic_lib_link_class) link_class);
159 /* Continue on with normal load_symbols processing. */
167 /* These variables are required to pass information back and forth
168 between after_open and check_needed and stat_needed and vercheck. */
170 static struct bfd_link_needed_list *global_needed;
171 static struct stat global_stat;
172 static lang_input_statement_type *global_found;
173 static struct bfd_link_needed_list *global_vercheck_needed;
174 static bfd_boolean global_vercheck_failed;
176 /* These variables are used to implement target options */
178 static char *audit; /* colon (typically) separated list of libs */
179 static char *depaudit; /* colon (typically) separated list of libs */
181 /* Style of .note.gnu.build-id section. */
182 static const char *emit_note_gnu_build_id;
184 /* On Linux, it's possible to have different versions of the same
185 shared library linked against different versions of libc. The
186 dynamic linker somehow tags which libc version to use in
187 /etc/ld.so.cache, and, based on the libc that it sees in the
188 executable, chooses which version of the shared library to use.
190 We try to do a similar check here by checking whether this shared
191 library needs any other shared libraries which may conflict with
192 libraries we have already included in the link. If it does, we
193 skip it, and try to find another shared library farther on down the
196 This is called via lang_for_each_input_file.
197 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
198 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
199 a conflicting version. */
202 gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
205 struct bfd_link_needed_list *l;
207 if (global_vercheck_failed)
209 if (s->the_bfd == NULL
210 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
213 soname = bfd_elf_get_dt_soname (s->the_bfd);
215 soname = lbasename (bfd_get_filename (s->the_bfd));
217 for (l = global_vercheck_needed; l != NULL; l = l->next)
221 if (filename_cmp (soname, l->name) == 0)
223 /* Probably can't happen, but it's an easy check. */
227 if (strchr (l->name, '/') != NULL)
230 suffix = strstr (l->name, ".so.");
234 suffix += sizeof ".so." - 1;
236 if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
238 /* Here we know that S is a dynamic object FOO.SO.VER1, and
239 the object we are considering needs a dynamic object
240 FOO.SO.VER2, and VER1 and VER2 are different. This
241 appears to be a version mismatch, so we tell the caller
242 to try a different version of this library. */
243 global_vercheck_failed = TRUE;
250 /* See if an input file matches a DT_NEEDED entry by running stat on
254 gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
260 if (global_found != NULL)
262 if (s->the_bfd == NULL)
265 /* If this input file was an as-needed entry, and wasn't found to be
266 needed at the stage it was linked, then don't say we have loaded it. */
267 if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
270 if (bfd_stat (s->the_bfd, &st) != 0)
272 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
276 /* Some operating systems, e.g. Windows, do not provide a meaningful
277 st_ino; they always set it to zero. (Windows does provide a
278 meaningful st_dev.) Do not indicate a duplicate library in that
279 case. While there is no guarantee that a system that provides
280 meaningful inode numbers will never set st_ino to zero, this is
281 merely an optimization, so we do not need to worry about false
283 if (st.st_dev == global_stat.st_dev
284 && st.st_ino == global_stat.st_ino
291 /* We issue a warning if it looks like we are including two
292 different versions of the same shared library. For example,
293 there may be a problem if -lc picks up libc.so.6 but some other
294 shared library has a DT_NEEDED entry of libc.so.5. This is a
295 heuristic test, and it will only work if the name looks like
296 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
297 If we really want to issue warnings about mixing version numbers
298 of shared libraries, we need to find a better way. */
300 if (strchr (global_needed->name, '/') != NULL)
302 suffix = strstr (global_needed->name, ".so.");
305 suffix += sizeof ".so." - 1;
307 soname = bfd_elf_get_dt_soname (s->the_bfd);
309 soname = lbasename (s->filename);
311 if (filename_ncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
312 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
313 global_needed->name, global_needed->by, soname);
322 /* This function is called for each possible name for a dynamic object
323 named by a DT_NEEDED entry. The FORCE parameter indicates whether
324 to skip the check for a conflicting version. */
327 gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
331 const char *name = needed->name;
335 abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
339 /* Linker needs to decompress sections. */
340 abfd->flags |= BFD_DECOMPRESS;
342 if (! bfd_check_format (abfd, bfd_object))
347 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
353 /* For DT_NEEDED, they have to match. */
354 if (abfd->xvec != link_info.output_bfd->xvec)
360 /* Check whether this object would include any conflicting library
361 versions. If FORCE is set, then we skip this check; we use this
362 the second time around, if we couldn't find any compatible
363 instance of the shared library. */
367 struct bfd_link_needed_list *needs;
369 if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
370 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
374 global_vercheck_needed = needs;
375 global_vercheck_failed = FALSE;
376 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
377 if (global_vercheck_failed)
380 /* Return FALSE to force the caller to move on to try
381 another file on the search path. */
385 /* But wait! It gets much worse. On Linux, if a shared
386 library does not use libc at all, we are supposed to skip
387 it the first time around in case we encounter a shared
388 library later on with the same name which does use the
389 version of libc that we want. This is much too horrible
390 to use on any system other than Linux. */
394 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
397 struct bfd_link_needed_list *l;
399 for (l = needs; l != NULL; l = l->next)
400 if (CONST_STRNEQ (l->name, "libc.so"))
416 /* We've found a dynamic object matching the DT_NEEDED entry. */
418 /* We have already checked that there is no other input file of the
419 same name. We must now check again that we are not including the
420 same file twice. We need to do this because on many systems
421 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
422 reference libc.so.1. If we have already included libc.so, we
423 don't want to include libc.so.1 if they are the same file, and we
424 can only check that using stat. */
426 if (bfd_stat (abfd, &global_stat) != 0)
427 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
429 /* First strip off everything before the last '/'. */
430 soname = lbasename (abfd->filename);
433 info_msg (_("found %s at %s\n"), soname, name);
436 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
437 if (global_found != NULL)
439 /* Return TRUE to indicate that we found the file, even though
440 we aren't going to do anything with it. */
444 /* Specify the soname to use. */
445 bfd_elf_set_dt_needed_name (abfd, soname);
447 /* Tell the ELF linker that we don't want the output file to have a
448 DT_NEEDED entry for this file, unless it is used to resolve
449 references in a regular object. */
450 link_class = DYN_DT_NEEDED;
452 /* Tell the ELF linker that we don't want the output file to have a
453 DT_NEEDED entry for this file at all if the entry is from a file
454 with DYN_NO_ADD_NEEDED. */
455 if (needed->by != NULL
456 && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
457 link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
459 bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
461 /* Add this file into the symbol table. */
462 if (! bfd_link_add_symbols (abfd, &link_info))
463 einfo ("%F%B: error adding symbols: %E\n", abfd);
469 /* Search for a needed file in a path. */
472 gld${EMULATION_NAME}_search_needed (const char *path,
473 struct dt_needed *n, int force)
476 const char *name = n->name;
478 struct dt_needed needed;
481 return gld${EMULATION_NAME}_try_needed (n, force);
483 if (path == NULL || *path == '\0')
487 needed.name = n->name;
492 char *filename, *sset;
494 s = strchr (path, config.rpath_separator);
496 s = path + strlen (path);
498 #if HAVE_DOS_BASED_FILE_SYSTEM
499 /* Assume a match on the second char is part of drive specifier. */
500 else if (config.rpath_separator == ':'
504 s = strchr (s + 1, config.rpath_separator);
506 s = path + strlen (path);
509 filename = (char *) xmalloc (s - path + len + 2);
514 memcpy (filename, path, s - path);
515 filename[s - path] = '/';
516 sset = filename + (s - path) + 1;
520 needed.name = filename;
521 if (gld${EMULATION_NAME}_try_needed (&needed, force))
535 if [ "x${USE_LIBPATH}" = xyes ] ; then
538 /* Add the sysroot to every entry in a path separated by
539 config.rpath_separator. */
542 gld${EMULATION_NAME}_add_sysroot (const char *path)
551 if (path[i++] == config.rpath_separator)
557 len = len + (colons + 1) * strlen (ld_sysroot);
558 ret = xmalloc (len + 1);
559 strcpy (ret, ld_sysroot);
560 p = ret + strlen (ret);
563 if (path[i] == config.rpath_separator)
566 strcpy (p, ld_sysroot);
578 *-*-freebsd* | *-*-dragonfly*)
580 /* Read the system search path the FreeBSD way rather than the Linux way. */
581 #ifdef HAVE_ELF_HINTS_H
582 #include <elf-hints.h>
584 #include "elf-hints-local.h"
588 gld${EMULATION_NAME}_check_ld_elf_hints (const struct bfd_link_needed_list *l,
591 static bfd_boolean initialized;
592 static char *ld_elf_hints;
593 struct dt_needed needed;
600 tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
601 f = fopen (tmppath, FOPEN_RB);
605 struct elfhints_hdr hdr;
607 if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
608 && hdr.magic == ELFHINTS_MAGIC
611 if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
615 b = xmalloc (hdr.dirlistlen + 1);
616 if (fread (b, 1, hdr.dirlistlen + 1, f) ==
618 ld_elf_hints = gld${EMULATION_NAME}_add_sysroot (b);
629 if (ld_elf_hints == NULL)
633 needed.name = l->name;
634 return gld${EMULATION_NAME}_search_needed (ld_elf_hints, &needed, force);
640 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
642 /* For a native linker, check the file /etc/ld.so.conf for directories
643 in which we may find shared libraries. /etc/ld.so.conf is really
644 only meaningful on Linux. */
646 struct gld${EMULATION_NAME}_ld_so_conf
653 gld${EMULATION_NAME}_parse_ld_so_conf
654 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
657 gld${EMULATION_NAME}_parse_ld_so_conf_include
658 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename,
666 if (pattern[0] != '/')
668 char *p = strrchr (filename, '/');
669 size_t patlen = strlen (pattern) + 1;
671 newp = xmalloc (p - filename + 1 + patlen);
672 memcpy (newp, filename, p - filename + 1);
673 memcpy (newp + (p - filename + 1), pattern, patlen);
678 if (glob (pattern, 0, NULL, &gl) == 0)
682 for (i = 0; i < gl.gl_pathc; ++i)
683 gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]);
687 /* If we do not have glob, treat the pattern as a literal filename. */
688 gld${EMULATION_NAME}_parse_ld_so_conf (info, pattern);
696 gld${EMULATION_NAME}_parse_ld_so_conf
697 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename)
699 FILE *f = fopen (filename, FOPEN_RT);
707 line = xmalloc (linelen);
712 /* Normally this would use getline(3), but we need to be portable. */
713 while ((q = fgets (p, linelen - (p - line), f)) != NULL
714 && strlen (q) == linelen - (p - line) - 1
715 && line[linelen - 2] != '\n')
717 line = xrealloc (line, 2 * linelen);
718 p = line + linelen - 1;
722 if (q == NULL && p == line)
725 p = strchr (line, '\n');
729 /* Because the file format does not know any form of quoting we
730 can search forward for the next '#' character and if found
731 make it terminating the line. */
732 p = strchr (line, '#');
736 /* Remove leading whitespace. NUL is no whitespace character. */
738 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
741 /* If the line is blank it is ignored. */
745 if (CONST_STRNEQ (p, "include") && (p[7] == ' ' || p[7] == '\t'))
751 while (*p == ' ' || *p == '\t')
759 while (*p != ' ' && *p != '\t' && *p)
765 gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename,
773 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
774 && *p != '\r' && *p != '\v')
777 while (p != dir && p[-1] == '/')
779 if (info->path == NULL)
781 info->alloc = p - dir + 1 + 256;
782 info->path = xmalloc (info->alloc);
787 if (info->len + 1 + (p - dir) >= info->alloc)
789 info->alloc += p - dir + 256;
790 info->path = xrealloc (info->path, info->alloc);
792 info->path[info->len++] = config.rpath_separator;
794 memcpy (info->path + info->len, dir, p - dir);
795 info->len += p - dir;
796 info->path[info->len] = '\0';
806 gld${EMULATION_NAME}_check_ld_so_conf (const struct bfd_link_needed_list *l,
809 static bfd_boolean initialized;
810 static char *ld_so_conf;
811 struct dt_needed needed;
816 struct gld${EMULATION_NAME}_ld_so_conf info;
819 info.len = info.alloc = 0;
820 tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf",
821 (const char *) NULL);
822 if (!gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath))
825 tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
826 (const char *) NULL);
827 gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
833 char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
840 if (ld_so_conf == NULL)
845 needed.name = l->name;
846 return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
856 /* See if an input file matches a DT_NEEDED entry by name. */
859 gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
863 /* Stop looking if we've found a loaded lib. */
864 if (global_found != NULL
865 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
866 & DYN_AS_NEEDED) == 0)
869 if (s->filename == NULL || s->the_bfd == NULL)
872 /* Don't look for a second non-loaded as-needed lib. */
873 if (global_found != NULL
874 && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
877 if (filename_cmp (s->filename, global_needed->name) == 0)
883 if (s->flags.search_dirs)
885 const char *f = strrchr (s->filename, '/');
887 && filename_cmp (f + 1, global_needed->name) == 0)
894 soname = bfd_elf_get_dt_soname (s->the_bfd);
896 && filename_cmp (soname, global_needed->name) == 0)
905 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
909 id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED)
911 const char *style = emit_note_gnu_build_id;
913 bfd_size_type build_id_size;
915 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
916 size = (size + 3) & -(bfd_size_type) 4;
918 build_id_size = compute_build_id_size (style);
920 size += build_id_size;
928 write_build_id (bfd *abfd)
930 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
931 struct elf_obj_tdata *t = elf_tdata (abfd);
934 Elf_Internal_Shdr *i_shdr;
935 unsigned char *contents, *id_bits;
938 Elf_External_Note *e_note;
940 style = t->o->build_id.style;
941 asec = t->o->build_id.sec;
942 if (bfd_is_abs_section (asec->output_section))
944 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
945 " --build-id ignored.\n"));
948 i_shdr = &elf_section_data (asec->output_section)->this_hdr;
950 if (i_shdr->contents == NULL)
952 if (asec->contents == NULL)
953 asec->contents = (unsigned char *) xmalloc (asec->size);
954 contents = asec->contents;
957 contents = i_shdr->contents + asec->output_offset;
959 e_note = (Elf_External_Note *) contents;
960 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
961 size = (size + 3) & -(bfd_size_type) 4;
962 id_bits = contents + size;
963 size = asec->size - size;
965 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
966 bfd_h_put_32 (abfd, size, &e_note->descsz);
967 bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
968 memcpy (e_note->name, "GNU", sizeof "GNU");
970 generate_build_id (abfd, style, bed->s->checksum_contents, id_bits, size);
972 position = i_shdr->sh_offset + asec->output_offset;
974 return (bfd_seek (abfd, position, SEEK_SET) == 0
975 && bfd_bwrite (contents, size, abfd) == size);
978 /* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
981 setup_build_id (bfd *ibfd)
987 size = id_note_section_size (ibfd);
990 einfo ("%P: warning: unrecognized --build-id style ignored.\n");
994 flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
995 | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
996 s = bfd_make_section_with_flags (ibfd, ".note.gnu.build-id", flags);
997 if (s != NULL && bfd_set_section_alignment (ibfd, s, 2))
999 struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
1000 t->o->build_id.after_write_object_contents = &write_build_id;
1001 t->o->build_id.style = emit_note_gnu_build_id;
1002 t->o->build_id.sec = s;
1003 elf_section_type (s) = SHT_NOTE;
1008 einfo ("%P: warning: Cannot create .note.gnu.build-id section,"
1009 " --build-id ignored.\n");
1013 /* This is called after all the input files have been opened. */
1016 gld${EMULATION_NAME}_after_open (void)
1018 struct bfd_link_needed_list *needed, *l;
1019 struct elf_link_hash_table *htab;
1021 after_open_default ();
1023 htab = elf_hash_table (&link_info);
1024 if (!is_elf_hash_table (htab))
1027 if (emit_note_gnu_build_id != NULL)
1031 /* Find an ELF input. */
1032 for (abfd = link_info.input_bfds;
1033 abfd != (bfd *) NULL; abfd = abfd->link.next)
1034 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1035 && bfd_count_sections (abfd) != 0)
1038 /* PR 10555: If there are no ELF input files do not try to
1039 create a .note.gnu-build-id section. */
1041 || !setup_build_id (abfd))
1043 free ((char *) emit_note_gnu_build_id);
1044 emit_note_gnu_build_id = NULL;
1048 if (bfd_link_relocatable (&link_info))
1050 if (link_info.execstack == ! link_info.noexecstack)
1051 /* PR ld/16744: If "-z [no]execstack" has been specified on the
1052 command line and we are perfoming a relocatable link then no
1053 PT_GNU_STACK segment will be created and so the
1054 linkinfo.[no]execstack values set in _handle_option() will have no
1055 effect. Instead we create a .note.GNU-stack section in much the
1056 same way as the assembler does with its --[no]execstack option. */
1057 (void) bfd_make_section_with_flags (link_info.input_bfds,
1059 SEC_READONLY | (link_info.execstack ? SEC_CODE : 0));
1064 if (!link_info.traditional_format)
1066 bfd *abfd, *elfbfd = NULL;
1067 bfd_boolean warn_eh_frame = FALSE;
1071 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1074 for (s = abfd->sections; s && type < COMPACT_EH_HDR; s = s->next)
1076 const char *name = bfd_get_section_name (abfd, s);
1078 if (bfd_is_abs_section (s->output_section))
1080 if (CONST_STRNEQ (name, ".eh_frame_entry"))
1081 type = COMPACT_EH_HDR;
1082 else if (strcmp (name, ".eh_frame") == 0 && s->size > 8)
1083 type = DWARF2_EH_HDR;
1092 else if (seen_type != type)
1094 einfo (_("%P%F: compact frame descriptions incompatible with"
1095 " DWARF2 .eh_frame from %B\n"),
1096 type == DWARF2_EH_HDR ? abfd : elfbfd);
1101 && (type == COMPACT_EH_HDR || link_info.eh_frame_hdr_type != 0))
1103 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1106 warn_eh_frame = TRUE;
1110 if (seen_type == COMPACT_EH_HDR)
1111 link_info.eh_frame_hdr_type = COMPACT_EH_HDR;
1113 if (bfd_count_sections (abfd) == 0)
1118 const struct elf_backend_data *bed;
1120 bed = get_elf_backend_data (elfbfd);
1121 s = bfd_make_section_with_flags (elfbfd, ".eh_frame_hdr",
1122 bed->dynamic_sec_flags
1125 && bfd_set_section_alignment (elfbfd, s, 2))
1127 htab->eh_info.hdr_sec = s;
1128 warn_eh_frame = FALSE;
1132 einfo ("%P: warning: Cannot create .eh_frame_hdr section,"
1133 " --eh-frame-hdr ignored.\n");
1136 /* Get the list of files which appear in DT_NEEDED entries in
1137 dynamic objects included in the link (often there will be none).
1138 For each such file, we want to track down the corresponding
1139 library, and include the symbol table in the link. This is what
1140 the runtime dynamic linker will do. Tracking the files down here
1141 permits one dynamic object to include another without requiring
1142 special action by the person doing the link. Note that the
1143 needed list can actually grow while we are stepping through this
1145 needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info);
1146 for (l = needed; l != NULL; l = l->next)
1148 struct bfd_link_needed_list *ll;
1149 struct dt_needed n, nn;
1152 /* If the lib that needs this one was --as-needed and wasn't
1153 found to be needed, then this lib isn't needed either. */
1155 && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0)
1158 /* Skip the lib if --no-copy-dt-needed-entries and
1159 --allow-shlib-undefined is in effect. */
1161 && link_info.unresolved_syms_in_shared_libs == RM_IGNORE
1162 && (bfd_elf_get_dyn_lib_class (l->by) & DYN_NO_ADD_NEEDED) != 0)
1165 /* If we've already seen this file, skip it. */
1166 for (ll = needed; ll != l; ll = ll->next)
1168 || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
1169 && strcmp (ll->name, l->name) == 0)
1174 /* See if this file was included in the link explicitly. */
1176 global_found = NULL;
1177 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
1178 if (global_found != NULL
1179 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1180 & DYN_AS_NEEDED) == 0)
1187 info_msg (_("%s needed by %B\n"), l->name, l->by);
1189 /* As-needed libs specified on the command line (or linker script)
1190 take priority over libs found in search dirs. */
1191 if (global_found != NULL)
1193 nn.name = global_found->filename;
1194 if (gld${EMULATION_NAME}_try_needed (&nn, TRUE))
1198 /* We need to find this file and include the symbol table. We
1199 want to search for the file in the same way that the dynamic
1200 linker will search. That means that we want to use
1201 rpath_link, rpath, then the environment variable
1202 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1203 entries (native only), then the linker script LIB_SEARCH_DIRS.
1204 We do not search using the -L arguments.
1206 We search twice. The first time, we skip objects which may
1207 introduce version mismatches. The second time, we force
1208 their use. See gld${EMULATION_NAME}_vercheck comment. */
1209 for (force = 0; force < 2; force++)
1212 search_dirs_type *search;
1214 if [ "x${NATIVE}" = xyes ] ; then
1216 const char *lib_path;
1219 if [ "x${USE_LIBPATH}" = xyes ] ; then
1221 struct bfd_link_needed_list *rp;
1227 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
1231 if [ "x${USE_LIBPATH}" = xyes ] ; then
1233 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
1238 if [ "x${NATIVE}" = xyes ] ; then
1240 if (command_line.rpath_link == NULL
1241 && command_line.rpath == NULL)
1243 lib_path = (const char *) getenv ("LD_RUN_PATH");
1244 if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
1248 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
1249 if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1253 if [ "x${USE_LIBPATH}" = xyes ] ; then
1256 rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
1257 for (; !found && rp != NULL; rp = rp->next)
1259 char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
1260 found = (rp->by == l->by
1261 && gld${EMULATION_NAME}_search_needed (tmpname,
1271 if [ "x${USE_LIBPATH}" = xyes ] ; then
1273 *-*-freebsd* | *-*-dragonfly*)
1275 if (gld${EMULATION_NAME}_check_ld_elf_hints (l, force))
1281 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
1283 if (gld${EMULATION_NAME}_check_ld_so_conf (l, force))
1292 len = strlen (l->name);
1293 for (search = search_head; search != NULL; search = search->next)
1297 if (search->cmdline)
1299 filename = (char *) xmalloc (strlen (search->name) + len + 2);
1300 sprintf (filename, "%s/%s", search->name, l->name);
1302 if (gld${EMULATION_NAME}_try_needed (&nn, force))
1315 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
1319 if (link_info.eh_frame_hdr_type == COMPACT_EH_HDR)
1320 if (bfd_elf_parse_eh_frame_entries (NULL, &link_info) == FALSE)
1321 einfo (_("%P%F: Failed to parse EH frame entries.\n"));
1329 /* Look through an expression for an assignment statement. */
1332 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1334 bfd_boolean provide = FALSE;
1336 switch (exp->type.node_class)
1339 case etree_provided:
1343 /* We call record_link_assignment even if the symbol is defined.
1344 This is because if it is defined by a dynamic object, we
1345 actually want to use the value defined by the linker script,
1346 not the value from the dynamic object (because we are setting
1347 symbols like etext). If the symbol is defined by a regular
1348 object, then, as it happens, calling record_link_assignment
1350 if (strcmp (exp->assign.dst, ".") != 0)
1352 if (!bfd_elf_record_link_assignment (link_info.output_bfd,
1354 exp->assign.dst, provide,
1355 exp->assign.hidden))
1356 einfo ("%P%F: failed to record assignment to %s: %E\n",
1359 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1363 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1364 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1368 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1369 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1370 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1374 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1383 /* This is called by the before_allocation routine via
1384 lang_for_each_statement. It locates any assignment statements, and
1385 tells the ELF backend about them, in case they are assignments to
1386 symbols which are referred to by dynamic objects. */
1389 gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
1391 if (s->header.type == lang_assignment_statement_enum)
1392 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1397 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
1398 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
1399 ELF_INTERPRETER_SET_DEFAULT="
1400 if (sinterp != NULL)
1402 sinterp->contents = (unsigned char *) ${ELF_INTERPRETER_NAME};
1403 sinterp->size = strlen ((char *) sinterp->contents) + 1;
1408 ELF_INTERPRETER_SET_DEFAULT=
1412 /* used by before_allocation and handle_option. */
1414 gld${EMULATION_NAME}_append_to_separated_string (char **to, char *op_arg)
1417 *to = xstrdup (op_arg);
1420 size_t to_len = strlen (*to);
1421 size_t op_arg_len = strlen (op_arg);
1425 /* First see whether OPTARG is already in the path. */
1428 if (strncmp (op_arg, cp, op_arg_len) == 0
1429 && (cp[op_arg_len] == 0
1430 || cp[op_arg_len] == config.rpath_separator))
1434 /* Not yet found. */
1435 cp = strchr (cp, config.rpath_separator);
1443 buf = xmalloc (to_len + op_arg_len + 2);
1444 sprintf (buf, "%s%c%s", *to,
1445 config.rpath_separator, op_arg);
1452 /* This is called after the sections have been attached to output
1453 sections, but before any sizes or addresses have been set. */
1456 gld${EMULATION_NAME}_before_allocation (void)
1461 struct elf_link_hash_entry *ehdr_start = NULL;
1462 #if defined(__GNUC__) && GCC_VERSION < 4006
1463 /* Work around a GCC uninitialized warning bug fixed in GCC 4.6. */
1464 struct bfd_link_hash_entry ehdr_start_save = ehdr_start_save;
1466 struct bfd_link_hash_entry ehdr_start_save;
1469 if (is_elf_hash_table (link_info.hash))
1471 _bfd_elf_tls_setup (link_info.output_bfd, &link_info);
1473 /* Make __ehdr_start hidden if it has been referenced, to
1474 prevent the symbol from being dynamic. */
1475 if (!bfd_link_relocatable (&link_info))
1477 struct elf_link_hash_entry *h
1478 = elf_link_hash_lookup (elf_hash_table (&link_info), "__ehdr_start",
1479 FALSE, FALSE, TRUE);
1481 /* Only adjust the export class if the symbol was referenced
1482 and not defined, otherwise leave it alone. */
1484 && (h->root.type == bfd_link_hash_new
1485 || h->root.type == bfd_link_hash_undefined
1486 || h->root.type == bfd_link_hash_undefweak
1487 || h->root.type == bfd_link_hash_common))
1489 _bfd_elf_link_hash_hide_symbol (&link_info, h, TRUE);
1490 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
1491 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
1492 /* Don't leave the symbol undefined. Undefined hidden
1493 symbols typically won't have dynamic relocations, but
1494 we most likely will need dynamic relocations for
1495 __ehdr_start if we are building a PIE or shared
1498 ehdr_start_save = h->root;
1499 h->root.type = bfd_link_hash_defined;
1500 h->root.u.def.section = bfd_abs_section_ptr;
1501 h->root.u.def.value = 0;
1505 /* If we are going to make any variable assignments, we need to
1506 let the ELF backend know about them in case the variables are
1507 referred to by dynamic objects. */
1508 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1511 /* Let the ELF backend work out the sizes of any sections required
1512 by dynamic linking. */
1513 rpath = command_line.rpath;
1515 rpath = (const char *) getenv ("LD_RUN_PATH");
1517 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link.next)
1518 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1520 const char *audit_libs = elf_dt_audit (abfd);
1522 /* If the input bfd contains an audit entry, we need to add it as
1523 a dep audit entry. */
1524 if (audit_libs && *audit_libs != '\0')
1526 char *cp = xstrdup (audit_libs);
1530 char *cp2 = strchr (cp, config.rpath_separator);
1538 if (cp != NULL && *cp != '\0')
1539 gld${EMULATION_NAME}_append_to_separated_string (&depaudit, cp);
1541 cp = more ? ++cp2 : NULL;
1547 if (! (bfd_elf_size_dynamic_sections
1548 (link_info.output_bfd, command_line.soname, rpath,
1549 command_line.filter_shlib, audit, depaudit,
1550 (const char * const *) command_line.auxiliary_filters,
1551 &link_info, &sinterp)))
1552 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1554 ${ELF_INTERPRETER_SET_DEFAULT}
1555 /* Let the user override the dynamic linker we are using. */
1556 if (command_line.interpreter != NULL
1559 sinterp->contents = (bfd_byte *) command_line.interpreter;
1560 sinterp->size = strlen (command_line.interpreter) + 1;
1563 /* Look for any sections named .gnu.warning. As a GNU extensions,
1564 we treat such sections as containing warning messages. We print
1565 out the warning message, and then zero out the section size so
1566 that it does not get copied into the output file. */
1569 LANG_FOR_EACH_INPUT_STATEMENT (is)
1576 if (is->flags.just_syms)
1579 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1584 msg = (char *) xmalloc ((size_t) (sz + 1));
1585 if (! bfd_get_section_contents (is->the_bfd, s, msg,
1587 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1590 ret = link_info.callbacks->warning (&link_info, msg,
1591 (const char *) NULL,
1592 is->the_bfd, (asection *) NULL,
1597 /* Clobber the section size, so that we don't waste space
1598 copying the warning into the output file. If we've already
1599 sized the output section, adjust its size. The adjustment
1600 is on rawsize because targets that size sections early will
1601 have called lang_reset_memory_regions after sizing. */
1602 if (s->output_section != NULL
1603 && s->output_section->rawsize >= s->size)
1604 s->output_section->rawsize -= s->size;
1608 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1609 warning section don't get copied to the output. */
1610 s->flags |= SEC_EXCLUDE | SEC_KEEP;
1614 before_allocation_default ();
1616 if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info))
1617 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1619 if (ehdr_start != NULL)
1621 /* If we twiddled __ehdr_start to defined earlier, put it back
1623 ehdr_start->root.type = ehdr_start_save.type;
1624 ehdr_start->root.u = ehdr_start_save.u;
1631 if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1634 /* Try to open a dynamic archive. This is where we know that ELF
1635 dynamic libraries have an extension of .so (or .sl on oddball systems
1639 gld${EMULATION_NAME}_open_dynamic_archive
1640 (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
1642 const char *filename;
1645 bfd_boolean opened = FALSE;
1647 if (! entry->flags.maybe_archive)
1650 filename = entry->filename;
1651 len = strlen (search->name) + strlen (filename);
1652 if (entry->flags.full_name_provided)
1655 string = (char *) xmalloc (len);
1656 sprintf (string, "%s/%s", search->name, filename);
1662 len += strlen (arch) + sizeof "/lib.so";
1663 #ifdef EXTRA_SHLIB_EXTENSION
1664 xlen = (strlen (EXTRA_SHLIB_EXTENSION) > 3
1665 ? strlen (EXTRA_SHLIB_EXTENSION) - 3
1668 string = (char *) xmalloc (len + xlen);
1669 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1670 #ifdef EXTRA_SHLIB_EXTENSION
1671 /* Try the .so extension first. If that fails build a new filename
1672 using EXTRA_SHLIB_EXTENSION. */
1673 opened = ldfile_try_open_bfd (string, entry);
1675 strcpy (string + len - 4, EXTRA_SHLIB_EXTENSION);
1679 if (!opened && !ldfile_try_open_bfd (string, entry))
1685 entry->filename = string;
1687 /* We have found a dynamic object to include in the link. The ELF
1688 backend linker will create a DT_NEEDED entry in the .dynamic
1689 section naming this file. If this file includes a DT_SONAME
1690 entry, it will be used. Otherwise, the ELF linker will just use
1691 the name of the file. For an archive found by searching, like
1692 this one, the DT_NEEDED entry should consist of just the name of
1693 the file, without the path information used to find it. Note
1694 that we only need to do this if we have a dynamic object; an
1695 archive will never be referenced by a DT_NEEDED entry.
1697 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1698 very pretty. I haven't been able to think of anything that is
1700 if (bfd_check_format (entry->the_bfd, bfd_object)
1701 && (entry->the_bfd->flags & DYNAMIC) != 0)
1703 ASSERT (entry->flags.maybe_archive && entry->flags.search_dirs);
1705 /* Rather than duplicating the logic above. Just use the
1706 filename we recorded earlier. */
1708 if (!entry->flags.full_name_provided)
1709 filename = lbasename (entry->filename);
1710 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1719 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1722 /* A variant of lang_output_section_find used by place_orphan. */
1724 static lang_output_section_statement_type *
1725 output_rel_find (asection *sec, int isdyn)
1727 lang_output_section_statement_type *lookup;
1728 lang_output_section_statement_type *last = NULL;
1729 lang_output_section_statement_type *last_alloc = NULL;
1730 lang_output_section_statement_type *last_ro_alloc = NULL;
1731 lang_output_section_statement_type *last_rel = NULL;
1732 lang_output_section_statement_type *last_rel_alloc = NULL;
1733 int rela = sec->name[4] == 'a';
1735 for (lookup = &lang_output_section_statement.head->output_section_statement;
1737 lookup = lookup->next)
1739 if (lookup->constraint >= 0
1740 && CONST_STRNEQ (lookup->name, ".rel"))
1742 int lookrela = lookup->name[4] == 'a';
1744 /* .rel.dyn must come before all other reloc sections, to suit
1749 /* Don't place after .rel.plt as doing so results in wrong
1751 if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
1754 if (rela == lookrela || last_rel == NULL)
1756 if ((rela == lookrela || last_rel_alloc == NULL)
1757 && lookup->bfd_section != NULL
1758 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1759 last_rel_alloc = lookup;
1763 if (lookup->bfd_section != NULL
1764 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1766 last_alloc = lookup;
1767 if ((lookup->bfd_section->flags & SEC_READONLY) != 0)
1768 last_ro_alloc = lookup;
1773 return last_rel_alloc;
1779 return last_ro_alloc;
1787 /* Place an orphan section. We use this to put random SHF_ALLOC
1788 sections in the right segment. */
1790 static lang_output_section_statement_type *
1791 gld${EMULATION_NAME}_place_orphan (asection *s,
1792 const char *secname,
1795 static struct orphan_save hold[] =
1798 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1801 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1804 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_THREAD_LOCAL,
1807 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1813 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1816 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1819 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
1825 enum orphan_save_index
1837 static int orphan_init_done = 0;
1838 struct orphan_save *place;
1839 lang_output_section_statement_type *after;
1840 lang_output_section_statement_type *os;
1841 lang_output_section_statement_type *match_by_name = NULL;
1843 int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
1844 unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
1846 if (!bfd_link_relocatable (&link_info)
1847 && link_info.combreloc
1848 && (s->flags & SEC_ALLOC))
1854 secname = ".rela.dyn";
1858 secname = ".rel.dyn";
1864 else if (CONST_STRNEQ (secname, ".rel"))
1866 secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1871 /* Look through the script to see where to place this section. */
1872 if (constraint == 0)
1873 for (os = lang_output_section_find (secname);
1875 os = next_matching_output_section_statement (os, 0))
1877 /* If we don't match an existing output section, tell
1878 lang_insert_orphan to create a new output section. */
1879 constraint = SPECIAL;
1881 if (os->bfd_section != NULL
1882 && (os->bfd_section->flags == 0
1883 || (_bfd_elf_match_sections_by_type (link_info.output_bfd,
1886 && ((s->flags ^ os->bfd_section->flags)
1887 & (SEC_LOAD | SEC_ALLOC)) == 0)))
1889 /* We already have an output section statement with this
1890 name, and its bfd section has compatible flags.
1891 If the section already exists but does not have any flags
1892 set, then it has been created by the linker, probably as a
1893 result of a --section-start command line switch. */
1894 lang_add_section (&os->children, s, NULL, os);
1898 /* Save unused output sections in case we can match them
1899 against orphans later. */
1900 if (os->bfd_section == NULL)
1904 /* If we didn't match an active output section, see if we matched an
1905 unused one and use that. */
1908 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1909 return match_by_name;
1912 if (!orphan_init_done)
1914 struct orphan_save *ho;
1916 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1917 if (ho->name != NULL)
1919 ho->os = lang_output_section_find (ho->name);
1920 if (ho->os != NULL && ho->os->flags == 0)
1921 ho->os->flags = ho->flags;
1923 orphan_init_done = 1;
1926 /* If this is a final link, then always put .gnu.warning.SYMBOL
1927 sections into the .text section to get them out of the way. */
1928 if (bfd_link_executable (&link_info)
1929 && CONST_STRNEQ (s->name, ".gnu.warning.")
1930 && hold[orphan_text].os != NULL)
1932 os = hold[orphan_text].os;
1933 lang_add_section (&os->children, s, NULL, os);
1937 /* Decide which segment the section should go in based on the
1938 section name and section flags. We put loadable .note sections
1939 right after the .interp section, so that the PT_NOTE segment is
1940 stored right after the program headers where the OS can read it
1941 in the first page. */
1944 if ((s->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
1945 place = &hold[orphan_nonalloc];
1946 else if ((s->flags & SEC_ALLOC) == 0)
1948 else if ((s->flags & SEC_LOAD) != 0
1949 && ((iself && sh_type == SHT_NOTE)
1950 || (!iself && CONST_STRNEQ (secname, ".note"))))
1951 place = &hold[orphan_interp];
1952 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
1953 place = &hold[orphan_bss];
1954 else if ((s->flags & SEC_SMALL_DATA) != 0)
1955 place = &hold[orphan_sdata];
1956 else if ((s->flags & SEC_THREAD_LOCAL) != 0)
1957 place = &hold[orphan_tdata];
1958 else if ((s->flags & SEC_READONLY) == 0)
1959 place = &hold[orphan_data];
1960 else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
1961 || (!iself && CONST_STRNEQ (secname, ".rel")))
1962 && (s->flags & SEC_LOAD) != 0)
1963 place = &hold[orphan_rel];
1964 else if ((s->flags & SEC_CODE) == 0)
1965 place = &hold[orphan_rodata];
1967 place = &hold[orphan_text];
1972 if (place->os == NULL)
1974 if (place->name != NULL)
1975 place->os = lang_output_section_find (place->name);
1977 place->os = output_rel_find (s, isdyn);
1981 after = lang_output_section_find_by_flags
1982 (s, &place->os, _bfd_elf_match_sections_by_type);
1984 /* *ABS* is always the first output section statement. */
1985 after = &lang_output_section_statement.head->output_section_statement;
1988 return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
1993 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
1997 gld${EMULATION_NAME}_after_allocation (void)
1999 int need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
2001 if (need_layout < 0)
2002 einfo ("%X%P: .eh_frame/.stab edit: %E\n");
2004 gld${EMULATION_NAME}_map_segments (need_layout);
2009 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
2013 gld${EMULATION_NAME}_get_script (int *isfile)
2016 if test x"$COMPILE_IN" = xyes
2018 # Scripts compiled in.
2020 # sed commands to quote an ld script as a C string.
2021 sc="-f stringify.sed"
2027 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2030 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2031 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
2032 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2033 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2034 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2035 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
2036 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2037 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2039 if test -n "$GENERATE_PIE_SCRIPT" ; then
2040 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2041 echo ' ; else if (bfd_link_pie (&link_info)' >> e${EMULATION_NAME}.c
2042 echo ' && link_info.combreloc' >> e${EMULATION_NAME}.c
2043 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2044 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2045 sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c
2046 echo ' ; else if (bfd_link_pie (&link_info)' >> e${EMULATION_NAME}.c
2047 echo ' && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2048 sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c
2050 echo ' ; else if (bfd_link_pie (&link_info)) return' >> e${EMULATION_NAME}.c
2051 sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c
2053 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2054 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2055 echo ' ; else if (bfd_link_dll (&link_info) && link_info.combreloc' >> e${EMULATION_NAME}.c
2056 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2057 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2058 sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c
2059 echo ' ; else if (bfd_link_dll (&link_info) && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2060 sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c
2062 echo ' ; else if (bfd_link_dll (&link_info)) return' >> e${EMULATION_NAME}.c
2063 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
2065 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2066 echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c
2067 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2068 sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c
2069 echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c
2070 sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c
2072 echo ' ; else return' >> e${EMULATION_NAME}.c
2073 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2074 echo '; }' >> e${EMULATION_NAME}.c
2077 # Scripts read from the filesystem.
2083 if (bfd_link_relocatable (&link_info) && config.build_constructors)
2084 return "ldscripts/${EMULATION_NAME}.xu";
2085 else if (bfd_link_relocatable (&link_info))
2086 return "ldscripts/${EMULATION_NAME}.xr";
2087 else if (!config.text_read_only)
2088 return "ldscripts/${EMULATION_NAME}.xbn";
2090 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
2093 else if (!config.magic_demand_paged)
2094 return "ldscripts/${EMULATION_NAME}.xn";
2097 if test -n "$GENERATE_PIE_SCRIPT" ; then
2098 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2100 else if (bfd_link_pie (&link_info)
2101 && link_info.combreloc
2103 && (link_info.flags & DF_BIND_NOW))
2104 return "ldscripts/${EMULATION_NAME}.xdw";
2105 else if (bfd_link_pie (&link_info)
2106 && link_info.combreloc)
2107 return "ldscripts/${EMULATION_NAME}.xdc";
2111 else if (bfd_link_pie (&link_info))
2112 return "ldscripts/${EMULATION_NAME}.xd";
2115 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2116 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2118 else if (bfd_link_dll (&link_info) && link_info.combreloc
2119 && link_info.relro && (link_info.flags & DF_BIND_NOW))
2120 return "ldscripts/${EMULATION_NAME}.xsw";
2121 else if (bfd_link_dll (&link_info) && link_info.combreloc)
2122 return "ldscripts/${EMULATION_NAME}.xsc";
2126 else if (bfd_link_dll (&link_info))
2127 return "ldscripts/${EMULATION_NAME}.xs";
2130 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2132 else if (link_info.combreloc && link_info.relro
2133 && (link_info.flags & DF_BIND_NOW))
2134 return "ldscripts/${EMULATION_NAME}.xw";
2135 else if (link_info.combreloc)
2136 return "ldscripts/${EMULATION_NAME}.xc";
2141 return "ldscripts/${EMULATION_NAME}.x";
2148 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
2150 $PARSE_AND_LIST_PROLOGUE
2156 #define OPTION_DISABLE_NEW_DTAGS (400)
2157 #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
2158 #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
2159 #define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1)
2160 #define OPTION_EXCLUDE_LIBS (OPTION_EH_FRAME_HDR + 1)
2161 #define OPTION_HASH_STYLE (OPTION_EXCLUDE_LIBS + 1)
2162 #define OPTION_BUILD_ID (OPTION_HASH_STYLE + 1)
2163 #define OPTION_AUDIT (OPTION_BUILD_ID + 1)
2164 #define OPTION_COMPRESS_DEBUG (OPTION_AUDIT + 1)
2167 gld${EMULATION_NAME}_add_options
2168 (int ns, char **shortopts, int nl, struct option **longopts,
2169 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
2172 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2174 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:P:";
2178 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
2182 static const struct option xtra_long[] = {
2184 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2186 {"audit", required_argument, NULL, OPTION_AUDIT},
2187 {"Bgroup", no_argument, NULL, OPTION_GROUP},
2191 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
2192 {"compress-debug-sections", required_argument, NULL, OPTION_COMPRESS_DEBUG},
2194 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2196 {"depaudit", required_argument, NULL, 'P'},
2197 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
2198 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
2199 {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
2200 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
2201 {"hash-style", required_argument, NULL, OPTION_HASH_STYLE},
2204 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
2206 $PARSE_AND_LIST_LONGOPTS
2210 {NULL, no_argument, NULL, 0}
2213 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
2214 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
2215 *longopts = (struct option *)
2216 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
2217 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
2220 #define DEFAULT_BUILD_ID_STYLE "sha1"
2223 gld${EMULATION_NAME}_handle_option (int optc)
2230 case OPTION_BUILD_ID:
2231 if (emit_note_gnu_build_id != NULL)
2233 free ((char *) emit_note_gnu_build_id);
2234 emit_note_gnu_build_id = NULL;
2237 optarg = DEFAULT_BUILD_ID_STYLE;
2238 if (strcmp (optarg, "none"))
2239 emit_note_gnu_build_id = xstrdup (optarg);
2242 case OPTION_COMPRESS_DEBUG:
2243 if (strcasecmp (optarg, "none") == 0)
2244 link_info.compress_debug = COMPRESS_DEBUG_NONE;
2245 else if (strcasecmp (optarg, "zlib") == 0)
2246 link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
2247 else if (strcasecmp (optarg, "zlib-gnu") == 0)
2248 link_info.compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
2249 else if (strcasecmp (optarg, "zlib-gabi") == 0)
2250 link_info.compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
2252 einfo (_("%P%F: invalid --compress-debug-sections option: \`%s'\n"),
2257 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2260 gld${EMULATION_NAME}_append_to_separated_string (&audit, optarg);
2264 gld${EMULATION_NAME}_append_to_separated_string (&depaudit, optarg);
2267 case OPTION_DISABLE_NEW_DTAGS:
2268 link_info.new_dtags = FALSE;
2271 case OPTION_ENABLE_NEW_DTAGS:
2272 link_info.new_dtags = TRUE;
2275 case OPTION_EH_FRAME_HDR:
2276 link_info.eh_frame_hdr_type = DWARF2_EH_HDR;
2280 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
2281 /* Groups must be self-contained. */
2282 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2283 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
2286 case OPTION_EXCLUDE_LIBS:
2287 add_excluded_libs (optarg);
2290 case OPTION_HASH_STYLE:
2291 link_info.emit_hash = FALSE;
2292 link_info.emit_gnu_hash = FALSE;
2293 if (strcmp (optarg, "sysv") == 0)
2294 link_info.emit_hash = TRUE;
2295 else if (strcmp (optarg, "gnu") == 0)
2296 link_info.emit_gnu_hash = TRUE;
2297 else if (strcmp (optarg, "both") == 0)
2299 link_info.emit_hash = TRUE;
2300 link_info.emit_gnu_hash = TRUE;
2303 einfo (_("%P%F: invalid hash style \`%s'\n"), optarg);
2310 if (strcmp (optarg, "defs") == 0)
2311 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2312 else if (strcmp (optarg, "muldefs") == 0)
2313 link_info.allow_multiple_definition = TRUE;
2314 else if (CONST_STRNEQ (optarg, "max-page-size="))
2318 config.maxpagesize = strtoul (optarg + 14, &end, 0);
2319 if (*end || (config.maxpagesize & (config.maxpagesize - 1)) != 0)
2320 einfo (_("%P%F: invalid maxium page size \`%s'\n"),
2323 else if (CONST_STRNEQ (optarg, "common-page-size="))
2326 config.commonpagesize = strtoul (optarg + 17, &end, 0);
2328 || (config.commonpagesize & (config.commonpagesize - 1)) != 0)
2329 einfo (_("%P%F: invalid common page size \`%s'\n"),
2332 else if (CONST_STRNEQ (optarg, "stack-size="))
2335 link_info.stacksize = strtoul (optarg + 11, &end, 0);
2336 if (*end || link_info.stacksize < 0)
2337 einfo (_("%P%F: invalid stack size \`%s'\n"), optarg + 11);
2338 if (!link_info.stacksize)
2339 /* Use -1 for explicit no-stack, because zero means
2341 link_info.stacksize = -1;
2343 else if (strcmp (optarg, "execstack") == 0)
2345 link_info.execstack = TRUE;
2346 link_info.noexecstack = FALSE;
2348 else if (strcmp (optarg, "noexecstack") == 0)
2350 link_info.noexecstack = TRUE;
2351 link_info.execstack = FALSE;
2355 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2357 else if (strcmp (optarg, "global") == 0)
2358 link_info.flags_1 |= (bfd_vma) DF_1_GLOBAL;
2359 else if (strcmp (optarg, "initfirst") == 0)
2360 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
2361 else if (strcmp (optarg, "interpose") == 0)
2362 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
2363 else if (strcmp (optarg, "loadfltr") == 0)
2364 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
2365 else if (strcmp (optarg, "nodefaultlib") == 0)
2366 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
2367 else if (strcmp (optarg, "nodelete") == 0)
2368 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
2369 else if (strcmp (optarg, "nodlopen") == 0)
2370 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
2371 else if (strcmp (optarg, "nodump") == 0)
2372 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
2373 else if (strcmp (optarg, "now") == 0)
2375 link_info.flags |= (bfd_vma) DF_BIND_NOW;
2376 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
2378 else if (strcmp (optarg, "lazy") == 0)
2380 link_info.flags &= ~(bfd_vma) DF_BIND_NOW;
2381 link_info.flags_1 &= ~(bfd_vma) DF_1_NOW;
2383 else if (strcmp (optarg, "origin") == 0)
2385 link_info.flags |= (bfd_vma) DF_ORIGIN;
2386 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
2388 else if (strcmp (optarg, "combreloc") == 0)
2389 link_info.combreloc = TRUE;
2390 else if (strcmp (optarg, "nocombreloc") == 0)
2391 link_info.combreloc = FALSE;
2392 else if (strcmp (optarg, "nocopyreloc") == 0)
2393 link_info.nocopyreloc = TRUE;
2394 else if (strcmp (optarg, "relro") == 0)
2395 link_info.relro = TRUE;
2396 else if (strcmp (optarg, "norelro") == 0)
2397 link_info.relro = FALSE;
2398 else if (strcmp (optarg, "text") == 0)
2399 link_info.error_textrel = TRUE;
2400 else if (strcmp (optarg, "notext") == 0)
2401 link_info.error_textrel = FALSE;
2402 else if (strcmp (optarg, "textoff") == 0)
2403 link_info.error_textrel = FALSE;
2407 if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
2409 $PARSE_AND_LIST_ARGS_CASE_Z
2415 einfo (_("%P: warning: -z %s ignored.\n"), optarg);
2419 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
2421 $PARSE_AND_LIST_ARGS_CASES
2433 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
2434 gld_list_options="gld${EMULATION_NAME}_list_options"
2435 if test -n "$PARSE_AND_LIST_OPTIONS"; then
2439 gld${EMULATION_NAME}_list_options (FILE * file)
2443 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
2445 $PARSE_AND_LIST_OPTIONS
2453 gld_list_options="NULL"
2456 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
2458 $PARSE_AND_LIST_EPILOGUE
2465 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2467 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
2468 ${LDEMUL_SYSLIB-syslib_default},
2469 ${LDEMUL_HLL-hll_default},
2470 ${LDEMUL_AFTER_PARSE-gld${EMULATION_NAME}_after_parse},
2471 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
2472 ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation},
2473 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
2474 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
2475 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
2476 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
2477 "${EMULATION_NAME}",
2479 ${LDEMUL_FINISH-finish_default},
2480 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
2481 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
2482 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
2483 ${LDEMUL_SET_SYMBOLS-NULL},
2484 ${LDEMUL_PARSE_ARGS-NULL},
2485 gld${EMULATION_NAME}_add_options,
2486 gld${EMULATION_NAME}_handle_option,
2487 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
2488 ${LDEMUL_LIST_OPTIONS-${gld_list_options}},
2489 ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
2490 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
2491 ${LDEMUL_NEW_VERS_PATTERN-NULL},
2492 ${LDEMUL_EXTRA_MAP_FILE_TEXT-NULL}