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 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
16 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
17 Free Software Foundation, Inc.
18 Written by Steve Chamberlain <sac@cygnus.com>
19 ELF support by Ian Lance Taylor <ian@cygnus.com>
21 This file is part of the GNU Binutils.
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 3 of the License, or
26 (at your option) any later version.
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
36 MA 02110-1301, USA. */
38 #define TARGET_IS_${EMULATION_NAME}
42 #include "libiberty.h"
43 #include "filenames.h"
44 #include "safe-ctype.h"
60 #include "elf/common.h"
62 #include "filenames.h"
64 /* Declare functions used by various EXTRA_EM_FILEs. */
65 static void gld${EMULATION_NAME}_before_parse (void);
66 static void gld${EMULATION_NAME}_after_open (void);
67 static void gld${EMULATION_NAME}_before_allocation (void);
68 static void gld${EMULATION_NAME}_after_allocation (void);
69 static lang_output_section_statement_type *gld${EMULATION_NAME}_place_orphan
70 (asection *, const char *, int);
73 if [ "x${USE_LIBPATH}" = xyes ] ; then
75 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
85 # Import any needed special functions and/or overrides.
87 source_em ${srcdir}/emultempl/elf-generic.em
88 if test -n "$EXTRA_EM_FILE" ; then
89 source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
92 # Functions in this file can be overridden by setting the LDEMUL_* shell
93 # variables. If the name of the overriding function is the same as is
94 # defined in this file, then don't output this file's version.
95 # If a different overriding name is given then output the standard function
96 # as presumably it is called from the overriding function.
98 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
102 gld${EMULATION_NAME}_before_parse (void)
104 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
105 input_flags.dynamic = ${DYNAMIC_LINK-TRUE};
106 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
107 config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
111 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu* | *-*-nacl*)
113 link_info.new_dtags = TRUE;
123 if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
125 /* Handle the generation of DT_NEEDED tags. */
128 gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
132 /* Tell the ELF linker that we don't want the output file to have a
133 DT_NEEDED entry for this file, unless it is used to resolve
134 references in a regular object. */
135 if (entry->flags.add_DT_NEEDED_for_regular)
136 link_class = DYN_AS_NEEDED;
138 /* Tell the ELF linker that we don't want the output file to have a
139 DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
141 if (!entry->flags.add_DT_NEEDED_for_dynamic)
142 link_class |= DYN_NO_ADD_NEEDED;
144 if (entry->flags.just_syms
145 && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
146 einfo (_("%P%F: --just-symbols may not be used on DSO: %B\n"),
150 || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
153 bfd_elf_set_dyn_lib_class (entry->the_bfd,
154 (enum dynamic_lib_link_class) link_class);
156 /* Continue on with normal load_symbols processing. */
164 /* These variables are required to pass information back and forth
165 between after_open and check_needed and stat_needed and vercheck. */
167 static struct bfd_link_needed_list *global_needed;
168 static struct stat global_stat;
169 static lang_input_statement_type *global_found;
170 static struct bfd_link_needed_list *global_vercheck_needed;
171 static bfd_boolean global_vercheck_failed;
173 /* These variables are used to implement target options */
175 static char *audit; /* colon (typically) separated list of libs */
176 static char *depaudit; /* colon (typically) separated list of libs */
178 /* On Linux, it's possible to have different versions of the same
179 shared library linked against different versions of libc. The
180 dynamic linker somehow tags which libc version to use in
181 /etc/ld.so.cache, and, based on the libc that it sees in the
182 executable, chooses which version of the shared library to use.
184 We try to do a similar check here by checking whether this shared
185 library needs any other shared libraries which may conflict with
186 libraries we have already included in the link. If it does, we
187 skip it, and try to find another shared library farther on down the
190 This is called via lang_for_each_input_file.
191 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
192 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
193 a conflicting version. */
196 gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
199 struct bfd_link_needed_list *l;
201 if (global_vercheck_failed)
203 if (s->the_bfd == NULL
204 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
207 soname = bfd_elf_get_dt_soname (s->the_bfd);
209 soname = lbasename (bfd_get_filename (s->the_bfd));
211 for (l = global_vercheck_needed; l != NULL; l = l->next)
215 if (filename_cmp (soname, l->name) == 0)
217 /* Probably can't happen, but it's an easy check. */
221 if (strchr (l->name, '/') != NULL)
224 suffix = strstr (l->name, ".so.");
228 suffix += sizeof ".so." - 1;
230 if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
232 /* Here we know that S is a dynamic object FOO.SO.VER1, and
233 the object we are considering needs a dynamic object
234 FOO.SO.VER2, and VER1 and VER2 are different. This
235 appears to be a version mismatch, so we tell the caller
236 to try a different version of this library. */
237 global_vercheck_failed = TRUE;
244 /* See if an input file matches a DT_NEEDED entry by running stat on
248 gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
254 if (global_found != NULL)
256 if (s->the_bfd == NULL)
259 /* If this input file was an as-needed entry, and wasn't found to be
260 needed at the stage it was linked, then don't say we have loaded it. */
261 if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
264 if (bfd_stat (s->the_bfd, &st) != 0)
266 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
270 /* Some operating systems, e.g. Windows, do not provide a meaningful
271 st_ino; they always set it to zero. (Windows does provide a
272 meaningful st_dev.) Do not indicate a duplicate library in that
273 case. While there is no guarantee that a system that provides
274 meaningful inode numbers will never set st_ino to zero, this is
275 merely an optimization, so we do not need to worry about false
277 if (st.st_dev == global_stat.st_dev
278 && st.st_ino == global_stat.st_ino
285 /* We issue a warning if it looks like we are including two
286 different versions of the same shared library. For example,
287 there may be a problem if -lc picks up libc.so.6 but some other
288 shared library has a DT_NEEDED entry of libc.so.5. This is a
289 heuristic test, and it will only work if the name looks like
290 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
291 If we really want to issue warnings about mixing version numbers
292 of shared libraries, we need to find a better way. */
294 if (strchr (global_needed->name, '/') != NULL)
296 suffix = strstr (global_needed->name, ".so.");
299 suffix += sizeof ".so." - 1;
301 soname = bfd_elf_get_dt_soname (s->the_bfd);
303 soname = lbasename (s->filename);
305 if (filename_ncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
306 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
307 global_needed->name, global_needed->by, soname);
316 /* This function is called for each possible name for a dynamic object
317 named by a DT_NEEDED entry. The FORCE parameter indicates whether
318 to skip the check for a conflicting version. */
321 gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
325 const char *name = needed->name;
329 abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
333 /* Linker needs to decompress sections. */
334 abfd->flags |= BFD_DECOMPRESS;
336 if (! bfd_check_format (abfd, bfd_object))
341 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
347 /* For DT_NEEDED, they have to match. */
348 if (abfd->xvec != link_info.output_bfd->xvec)
354 /* Check whether this object would include any conflicting library
355 versions. If FORCE is set, then we skip this check; we use this
356 the second time around, if we couldn't find any compatible
357 instance of the shared library. */
361 struct bfd_link_needed_list *needs;
363 if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
364 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
368 global_vercheck_needed = needs;
369 global_vercheck_failed = FALSE;
370 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
371 if (global_vercheck_failed)
374 /* Return FALSE to force the caller to move on to try
375 another file on the search path. */
379 /* But wait! It gets much worse. On Linux, if a shared
380 library does not use libc at all, we are supposed to skip
381 it the first time around in case we encounter a shared
382 library later on with the same name which does use the
383 version of libc that we want. This is much too horrible
384 to use on any system other than Linux. */
388 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
391 struct bfd_link_needed_list *l;
393 for (l = needs; l != NULL; l = l->next)
394 if (CONST_STRNEQ (l->name, "libc.so"))
410 /* We've found a dynamic object matching the DT_NEEDED entry. */
412 /* We have already checked that there is no other input file of the
413 same name. We must now check again that we are not including the
414 same file twice. We need to do this because on many systems
415 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
416 reference libc.so.1. If we have already included libc.so, we
417 don't want to include libc.so.1 if they are the same file, and we
418 can only check that using stat. */
420 if (bfd_stat (abfd, &global_stat) != 0)
421 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
423 /* First strip off everything before the last '/'. */
424 soname = lbasename (abfd->filename);
427 info_msg (_("found %s at %s\n"), soname, name);
430 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
431 if (global_found != NULL)
433 /* Return TRUE to indicate that we found the file, even though
434 we aren't going to do anything with it. */
438 /* Specify the soname to use. */
439 bfd_elf_set_dt_needed_name (abfd, soname);
441 /* Tell the ELF linker that we don't want the output file to have a
442 DT_NEEDED entry for this file, unless it is used to resolve
443 references in a regular object. */
444 link_class = DYN_DT_NEEDED;
446 /* Tell the ELF linker that we don't want the output file to have a
447 DT_NEEDED entry for this file at all if the entry is from a file
448 with DYN_NO_ADD_NEEDED. */
449 if (needed->by != NULL
450 && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
451 link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
453 bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
455 /* Add this file into the symbol table. */
456 if (! bfd_link_add_symbols (abfd, &link_info))
457 einfo ("%F%B: could not read symbols: %E\n", abfd);
463 /* Search for a needed file in a path. */
466 gld${EMULATION_NAME}_search_needed (const char *path,
467 struct dt_needed *n, int force)
470 const char *name = n->name;
472 struct dt_needed needed;
475 return gld${EMULATION_NAME}_try_needed (n, force);
477 if (path == NULL || *path == '\0')
481 needed.name = n->name;
486 char *filename, *sset;
488 s = strchr (path, config.rpath_separator);
490 s = path + strlen (path);
492 #if HAVE_DOS_BASED_FILE_SYSTEM
493 /* Assume a match on the second char is part of drive specifier. */
494 else if (config.rpath_separator == ':'
498 s = strchr (s + 1, config.rpath_separator);
500 s = path + strlen (path);
503 filename = (char *) xmalloc (s - path + len + 2);
508 memcpy (filename, path, s - path);
509 filename[s - path] = '/';
510 sset = filename + (s - path) + 1;
514 needed.name = filename;
515 if (gld${EMULATION_NAME}_try_needed (&needed, force))
529 if [ "x${USE_LIBPATH}" = xyes ] ; then
532 /* Add the sysroot to every entry in a path separated by
533 config.rpath_separator. */
536 gld${EMULATION_NAME}_add_sysroot (const char *path)
545 if (path[i++] == config.rpath_separator)
551 len = len + (colons + 1) * strlen (ld_sysroot);
552 ret = xmalloc (len + 1);
553 strcpy (ret, ld_sysroot);
554 p = ret + strlen (ret);
557 if (path[i] == config.rpath_separator)
560 strcpy (p, ld_sysroot);
572 *-*-freebsd* | *-*-dragonfly*)
574 /* Read the system search path the FreeBSD way rather than the Linux way. */
575 #ifdef HAVE_ELF_HINTS_H
576 #include <elf-hints.h>
578 #include "elf-hints-local.h"
582 gld${EMULATION_NAME}_check_ld_elf_hints (const struct bfd_link_needed_list *l,
585 static bfd_boolean initialized;
586 static char *ld_elf_hints;
587 struct dt_needed needed;
594 tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
595 f = fopen (tmppath, FOPEN_RB);
599 struct elfhints_hdr hdr;
601 if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
602 && hdr.magic == ELFHINTS_MAGIC
605 if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
609 b = xmalloc (hdr.dirlistlen + 1);
610 if (fread (b, 1, hdr.dirlistlen + 1, f) ==
612 ld_elf_hints = gld${EMULATION_NAME}_add_sysroot (b);
623 if (ld_elf_hints == NULL)
627 needed.name = l->name;
628 return gld${EMULATION_NAME}_search_needed (ld_elf_hints, &needed, force);
634 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
636 /* For a native linker, check the file /etc/ld.so.conf for directories
637 in which we may find shared libraries. /etc/ld.so.conf is really
638 only meaningful on Linux. */
640 struct gld${EMULATION_NAME}_ld_so_conf
647 gld${EMULATION_NAME}_parse_ld_so_conf
648 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
651 gld${EMULATION_NAME}_parse_ld_so_conf_include
652 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename,
660 if (pattern[0] != '/')
662 char *p = strrchr (filename, '/');
663 size_t patlen = strlen (pattern) + 1;
665 newp = xmalloc (p - filename + 1 + patlen);
666 memcpy (newp, filename, p - filename + 1);
667 memcpy (newp + (p - filename + 1), pattern, patlen);
672 if (glob (pattern, 0, NULL, &gl) == 0)
676 for (i = 0; i < gl.gl_pathc; ++i)
677 gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]);
681 /* If we do not have glob, treat the pattern as a literal filename. */
682 gld${EMULATION_NAME}_parse_ld_so_conf (info, pattern);
690 gld${EMULATION_NAME}_parse_ld_so_conf
691 (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename)
693 FILE *f = fopen (filename, FOPEN_RT);
701 line = xmalloc (linelen);
706 /* Normally this would use getline(3), but we need to be portable. */
707 while ((q = fgets (p, linelen - (p - line), f)) != NULL
708 && strlen (q) == linelen - (p - line) - 1
709 && line[linelen - 2] != '\n')
711 line = xrealloc (line, 2 * linelen);
712 p = line + linelen - 1;
716 if (q == NULL && p == line)
719 p = strchr (line, '\n');
723 /* Because the file format does not know any form of quoting we
724 can search forward for the next '#' character and if found
725 make it terminating the line. */
726 p = strchr (line, '#');
730 /* Remove leading whitespace. NUL is no whitespace character. */
732 while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
735 /* If the line is blank it is ignored. */
739 if (CONST_STRNEQ (p, "include") && (p[7] == ' ' || p[7] == '\t'))
745 while (*p == ' ' || *p == '\t')
753 while (*p != ' ' && *p != '\t' && *p)
759 gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename,
767 while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
768 && *p != '\r' && *p != '\v')
771 while (p != dir && p[-1] == '/')
773 if (info->path == NULL)
775 info->alloc = p - dir + 1 + 256;
776 info->path = xmalloc (info->alloc);
781 if (info->len + 1 + (p - dir) >= info->alloc)
783 info->alloc += p - dir + 256;
784 info->path = xrealloc (info->path, info->alloc);
786 info->path[info->len++] = config.rpath_separator;
788 memcpy (info->path + info->len, dir, p - dir);
789 info->len += p - dir;
790 info->path[info->len] = '\0';
800 gld${EMULATION_NAME}_check_ld_so_conf (const struct bfd_link_needed_list *l,
803 static bfd_boolean initialized;
804 static char *ld_so_conf;
805 struct dt_needed needed;
810 struct gld${EMULATION_NAME}_ld_so_conf info;
813 info.len = info.alloc = 0;
814 tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf",
815 (const char *) NULL);
816 if (!gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath))
819 tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
820 (const char *) NULL);
821 gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
827 char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
834 if (ld_so_conf == NULL)
839 needed.name = l->name;
840 return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
850 /* See if an input file matches a DT_NEEDED entry by name. */
853 gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
857 /* Stop looking if we've found a loaded lib. */
858 if (global_found != NULL
859 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
860 & DYN_AS_NEEDED) == 0)
863 if (s->filename == NULL || s->the_bfd == NULL)
866 /* Don't look for a second non-loaded as-needed lib. */
867 if (global_found != NULL
868 && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
871 if (filename_cmp (s->filename, global_needed->name) == 0)
877 if (s->flags.search_dirs)
879 const char *f = strrchr (s->filename, '/');
881 && filename_cmp (f + 1, global_needed->name) == 0)
888 soname = bfd_elf_get_dt_soname (s->the_bfd);
890 && filename_cmp (soname, global_needed->name) == 0)
899 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
903 gld${EMULATION_NAME}_id_note_section_size (bfd *abfd,
904 struct bfd_link_info *linfo)
906 const char *style = linfo->emit_note_gnu_build_id;
911 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
912 size = (size + 3) & -(bfd_size_type) 4;
914 if (!strcmp (style, "md5") || !strcmp (style, "uuid"))
916 else if (!strcmp (style, "sha1"))
918 else if (!strncmp (style, "0x", 2))
920 /* ID is in string form (hex). Convert to bits. */
921 const char *id = style + 2;
924 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
929 else if (*id == '-' || *id == ':')
936 } while (*id != '\0');
945 read_hex (const char xdigit)
947 if (ISDIGIT (xdigit))
949 if (ISUPPER (xdigit))
950 return xdigit - 'A' + 0xa;
951 if (ISLOWER (xdigit))
952 return xdigit - 'a' + 0xa;
964 gld${EMULATION_NAME}_write_build_id_section (bfd *abfd)
966 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
967 struct build_id_info *info = (struct build_id_info *)
968 elf_tdata (abfd)->after_write_object_contents_info;
970 Elf_Internal_Shdr *i_shdr;
971 unsigned char *contents, *id_bits;
973 Elf_External_Note *e_note;
976 if (bfd_is_abs_section (asec->output_section))
978 einfo (_("%P: warning: .note.gnu.build-id section discarded,"
979 " --build-id ignored.\n"));
982 i_shdr = &elf_section_data (asec->output_section)->this_hdr;
984 if (i_shdr->contents == NULL)
986 if (asec->contents == NULL)
987 asec->contents = (unsigned char *) xmalloc (asec->size);
988 contents = asec->contents;
991 contents = i_shdr->contents + asec->output_offset;
993 e_note = (Elf_External_Note *) contents;
994 size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
995 size = (size + 3) & -(bfd_size_type) 4;
996 id_bits = contents + size;
997 size = asec->size - size;
999 bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
1000 bfd_h_put_32 (abfd, size, &e_note->descsz);
1001 bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
1002 memcpy (e_note->name, "GNU", sizeof "GNU");
1004 if (!strcmp (info->style, "md5"))
1007 md5_init_ctx (&ctx);
1008 if (bed->s->checksum_contents (abfd,
1009 (void (*) (const void *, size_t, void *))
1012 md5_finish_ctx (&ctx, id_bits);
1016 else if (!strcmp (info->style, "sha1"))
1018 struct sha1_ctx ctx;
1019 sha1_init_ctx (&ctx);
1020 if (bed->s->checksum_contents (abfd,
1021 (void (*) (const void *, size_t, void *))
1022 &sha1_process_bytes,
1024 sha1_finish_ctx (&ctx, id_bits);
1028 else if (!strcmp (info->style, "uuid"))
1031 int fd = open ("/dev/urandom", O_RDONLY);
1034 n = read (fd, id_bits, size);
1039 else if (!strncmp (info->style, "0x", 2))
1041 /* ID is in string form (hex). Convert to bits. */
1042 const char *id = info->style + 2;
1046 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
1048 id_bits[n] = read_hex (*id++) << 4;
1049 id_bits[n++] |= read_hex (*id++);
1051 else if (*id == '-' || *id == ':')
1054 abort (); /* Should have been validated earlier. */
1055 } while (*id != '\0');
1058 abort (); /* Should have been validated earlier. */
1061 return (bfd_seek (abfd,
1062 i_shdr->sh_offset + asec->output_offset, SEEK_SET) == 0
1063 && bfd_bwrite (contents, size, abfd) == size);
1067 /* This is called after all the input files have been opened. */
1070 gld${EMULATION_NAME}_after_open (void)
1072 struct bfd_link_needed_list *needed, *l;
1073 struct elf_link_hash_table *htab;
1075 after_open_default ();
1077 htab = elf_hash_table (&link_info);
1078 if (!is_elf_hash_table (htab))
1081 if (link_info.emit_note_gnu_build_id)
1087 /* Find an ELF input. */
1088 for (abfd = link_info.input_bfds;
1089 abfd != (bfd *) NULL; abfd = abfd->link_next)
1090 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1095 /* PR 10555: If there are no input files do not
1096 try to create a .note.gnu-build-id section. */
1097 free (link_info.emit_note_gnu_build_id);
1098 link_info.emit_note_gnu_build_id = NULL;
1102 size = gld${EMULATION_NAME}_id_note_section_size (abfd, &link_info);
1105 einfo ("%P: warning: unrecognized --build-id style ignored.\n");
1106 free (link_info.emit_note_gnu_build_id);
1107 link_info.emit_note_gnu_build_id = NULL;
1111 s = bfd_make_section_with_flags (abfd, ".note.gnu.build-id",
1112 SEC_ALLOC | SEC_LOAD
1113 | SEC_IN_MEMORY | SEC_LINKER_CREATED
1114 | SEC_READONLY | SEC_DATA);
1115 if (s != NULL && bfd_set_section_alignment (abfd, s, 2))
1117 struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
1118 struct build_id_info *b =
1119 (struct build_id_info *) xmalloc (sizeof *b);
1121 b->style = link_info.emit_note_gnu_build_id;
1123 elf_section_type (s) = SHT_NOTE;
1125 t->after_write_object_contents
1126 = &gld${EMULATION_NAME}_write_build_id_section;
1127 t->after_write_object_contents_info = b;
1131 einfo ("%P: warning: Cannot create .note.gnu.build-id section,"
1132 " --build-id ignored.\n");
1133 free (link_info.emit_note_gnu_build_id);
1134 link_info.emit_note_gnu_build_id = NULL;
1140 if (link_info.relocatable)
1143 if (link_info.eh_frame_hdr
1144 && !link_info.traditional_format)
1146 bfd *abfd, *elfbfd = NULL;
1147 bfd_boolean warn_eh_frame = FALSE;
1150 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link_next)
1152 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1156 s = bfd_get_section_by_name (abfd, ".eh_frame");
1159 || bfd_is_abs_section (s->output_section)))
1160 s = bfd_get_next_section_by_name (s);
1161 warn_eh_frame = s != NULL;
1163 if (elfbfd && warn_eh_frame)
1168 const struct elf_backend_data *bed;
1170 bed = get_elf_backend_data (elfbfd);
1171 s = bfd_make_section_with_flags (elfbfd, ".eh_frame_hdr",
1172 bed->dynamic_sec_flags
1175 && bfd_set_section_alignment (elfbfd, s, 2))
1177 htab->eh_info.hdr_sec = s;
1178 warn_eh_frame = FALSE;
1182 einfo ("%P: warning: Cannot create .eh_frame_hdr section,"
1183 " --eh-frame-hdr ignored.\n");
1186 /* Get the list of files which appear in DT_NEEDED entries in
1187 dynamic objects included in the link (often there will be none).
1188 For each such file, we want to track down the corresponding
1189 library, and include the symbol table in the link. This is what
1190 the runtime dynamic linker will do. Tracking the files down here
1191 permits one dynamic object to include another without requiring
1192 special action by the person doing the link. Note that the
1193 needed list can actually grow while we are stepping through this
1195 needed = bfd_elf_get_needed_list (link_info.output_bfd, &link_info);
1196 for (l = needed; l != NULL; l = l->next)
1198 struct bfd_link_needed_list *ll;
1199 struct dt_needed n, nn;
1202 /* If the lib that needs this one was --as-needed and wasn't
1203 found to be needed, then this lib isn't needed either. Skip
1204 the lib when creating a shared object unless we are copying
1205 DT_NEEDED entres. */
1207 && ((bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0
1208 || (!link_info.executable
1209 && bfd_elf_get_dyn_lib_class (l->by) & DYN_NO_ADD_NEEDED) != 0))
1212 /* If we've already seen this file, skip it. */
1213 for (ll = needed; ll != l; ll = ll->next)
1215 || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
1216 && strcmp (ll->name, l->name) == 0)
1221 /* See if this file was included in the link explicitly. */
1223 global_found = NULL;
1224 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
1225 if (global_found != NULL
1226 && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1227 & DYN_AS_NEEDED) == 0)
1234 info_msg (_("%s needed by %B\n"), l->name, l->by);
1236 /* As-needed libs specified on the command line (or linker script)
1237 take priority over libs found in search dirs. */
1238 if (global_found != NULL)
1240 nn.name = global_found->filename;
1241 if (gld${EMULATION_NAME}_try_needed (&nn, TRUE))
1245 /* We need to find this file and include the symbol table. We
1246 want to search for the file in the same way that the dynamic
1247 linker will search. That means that we want to use
1248 rpath_link, rpath, then the environment variable
1249 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1250 entries (native only), then the linker script LIB_SEARCH_DIRS.
1251 We do not search using the -L arguments.
1253 We search twice. The first time, we skip objects which may
1254 introduce version mismatches. The second time, we force
1255 their use. See gld${EMULATION_NAME}_vercheck comment. */
1256 for (force = 0; force < 2; force++)
1259 search_dirs_type *search;
1261 if [ "x${NATIVE}" = xyes ] ; then
1263 const char *lib_path;
1266 if [ "x${USE_LIBPATH}" = xyes ] ; then
1268 struct bfd_link_needed_list *rp;
1274 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
1278 if [ "x${USE_LIBPATH}" = xyes ] ; then
1280 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
1285 if [ "x${NATIVE}" = xyes ] ; then
1287 if (command_line.rpath_link == NULL
1288 && command_line.rpath == NULL)
1290 lib_path = (const char *) getenv ("LD_RUN_PATH");
1291 if (gld${EMULATION_NAME}_search_needed (lib_path, &n,
1295 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
1296 if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1300 if [ "x${USE_LIBPATH}" = xyes ] ; then
1303 rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
1304 for (; !found && rp != NULL; rp = rp->next)
1306 char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
1307 found = (rp->by == l->by
1308 && gld${EMULATION_NAME}_search_needed (tmpname,
1318 if [ "x${USE_LIBPATH}" = xyes ] ; then
1320 *-*-freebsd* | *-*-dragonfly*)
1322 if (gld${EMULATION_NAME}_check_ld_elf_hints (l, force))
1328 *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
1331 if (gld${EMULATION_NAME}_check_ld_so_conf (l, force))
1339 len = strlen (l->name);
1340 for (search = search_head; search != NULL; search = search->next)
1344 if (search->cmdline)
1346 filename = (char *) xmalloc (strlen (search->name) + len + 2);
1347 sprintf (filename, "%s/%s", search->name, l->name);
1349 if (gld${EMULATION_NAME}_try_needed (&nn, force))
1362 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
1372 /* Look through an expression for an assignment statement. */
1375 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1377 bfd_boolean provide = FALSE;
1379 switch (exp->type.node_class)
1382 case etree_provided:
1386 /* We call record_link_assignment even if the symbol is defined.
1387 This is because if it is defined by a dynamic object, we
1388 actually want to use the value defined by the linker script,
1389 not the value from the dynamic object (because we are setting
1390 symbols like etext). If the symbol is defined by a regular
1391 object, then, as it happens, calling record_link_assignment
1393 if (strcmp (exp->assign.dst, ".") != 0)
1395 if (!bfd_elf_record_link_assignment (link_info.output_bfd,
1397 exp->assign.dst, provide,
1398 exp->assign.hidden))
1399 einfo ("%P%F: failed to record assignment to %s: %E\n",
1402 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1406 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1407 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1411 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1412 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1413 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1417 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1426 /* This is called by the before_allocation routine via
1427 lang_for_each_statement. It locates any assignment statements, and
1428 tells the ELF backend about them, in case they are assignments to
1429 symbols which are referred to by dynamic objects. */
1432 gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
1434 if (s->header.type == lang_assignment_statement_enum)
1435 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1440 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
1441 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
1442 ELF_INTERPRETER_SET_DEFAULT="
1443 if (sinterp != NULL)
1445 sinterp->contents = (unsigned char *) ${ELF_INTERPRETER_NAME};
1446 sinterp->size = strlen ((char *) sinterp->contents) + 1;
1451 ELF_INTERPRETER_SET_DEFAULT=
1455 /* used by before_allocation and handle_option. */
1457 gld${EMULATION_NAME}_append_to_separated_string (char **to, char *op_arg)
1460 *to = xstrdup (op_arg);
1463 size_t to_len = strlen (*to);
1464 size_t op_arg_len = strlen (op_arg);
1468 /* First see whether OPTARG is already in the path. */
1471 if (strncmp (op_arg, cp, op_arg_len) == 0
1472 && (cp[op_arg_len] == 0
1473 || cp[op_arg_len] == config.rpath_separator))
1477 /* Not yet found. */
1478 cp = strchr (cp, config.rpath_separator);
1486 buf = xmalloc (to_len + op_arg_len + 2);
1487 sprintf (buf, "%s%c%s", *to,
1488 config.rpath_separator, op_arg);
1495 /* This is called after the sections have been attached to output
1496 sections, but before any sizes or addresses have been set. */
1499 gld${EMULATION_NAME}_before_allocation (void)
1505 if (link_info.hash->type == bfd_link_elf_hash_table)
1506 _bfd_elf_tls_setup (link_info.output_bfd, &link_info);
1508 /* If we are going to make any variable assignments, we need to let
1509 the ELF backend know about them in case the variables are
1510 referred to by dynamic objects. */
1511 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1513 /* Let the ELF backend work out the sizes of any sections required
1514 by dynamic linking. */
1515 rpath = command_line.rpath;
1517 rpath = (const char *) getenv ("LD_RUN_PATH");
1519 for (abfd = link_info.input_bfds; abfd; abfd = abfd->link_next)
1520 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1522 const char *audit_libs = elf_dt_audit (abfd);
1524 /* If the input bfd contains an audit entry, we need to add it as
1525 a dep audit entry. */
1526 if (audit_libs && *audit_libs != '\0')
1528 char *cp = xstrdup (audit_libs);
1532 char *cp2 = strchr (cp, config.rpath_separator);
1540 if (cp != NULL && *cp != '\0')
1541 gld${EMULATION_NAME}_append_to_separated_string (&depaudit, cp);
1543 cp = more ? ++cp2 : NULL;
1549 if (! (bfd_elf_size_dynamic_sections
1550 (link_info.output_bfd, command_line.soname, rpath,
1551 command_line.filter_shlib, audit, depaudit,
1552 (const char * const *) command_line.auxiliary_filters,
1553 &link_info, &sinterp)))
1554 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1556 ${ELF_INTERPRETER_SET_DEFAULT}
1557 /* Let the user override the dynamic linker we are using. */
1558 if (command_line.interpreter != NULL
1561 sinterp->contents = (bfd_byte *) command_line.interpreter;
1562 sinterp->size = strlen (command_line.interpreter) + 1;
1565 /* Look for any sections named .gnu.warning. As a GNU extensions,
1566 we treat such sections as containing warning messages. We print
1567 out the warning message, and then zero out the section size so
1568 that it does not get copied into the output file. */
1571 LANG_FOR_EACH_INPUT_STATEMENT (is)
1578 if (is->flags.just_syms)
1581 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1586 msg = (char *) xmalloc ((size_t) (sz + 1));
1587 if (! bfd_get_section_contents (is->the_bfd, s, msg,
1589 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1592 ret = link_info.callbacks->warning (&link_info, msg,
1593 (const char *) NULL,
1594 is->the_bfd, (asection *) NULL,
1599 /* Clobber the section size, so that we don't waste space
1600 copying the warning into the output file. If we've already
1601 sized the output section, adjust its size. The adjustment
1602 is on rawsize because targets that size sections early will
1603 have called lang_reset_memory_regions after sizing. */
1604 if (s->output_section != NULL
1605 && s->output_section->rawsize >= s->size)
1606 s->output_section->rawsize -= s->size;
1610 /* Also set SEC_EXCLUDE, so that local symbols defined in the
1611 warning section don't get copied to the output. */
1612 s->flags |= SEC_EXCLUDE | SEC_KEEP;
1616 before_allocation_default ();
1618 if (!bfd_elf_size_dynsym_hash_dynstr (link_info.output_bfd, &link_info))
1619 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1625 if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1628 /* Try to open a dynamic archive. This is where we know that ELF
1629 dynamic libraries have an extension of .so (or .sl on oddball systems
1633 gld${EMULATION_NAME}_open_dynamic_archive
1634 (const char *arch, search_dirs_type *search, lang_input_statement_type *entry)
1636 const char *filename;
1639 if (! entry->flags.maybe_archive)
1642 filename = entry->filename;
1644 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
1645 is defined, but it does not seem worth the headache to optimize
1646 away those two bytes of space. */
1647 string = (char *) xmalloc (strlen (search->name)
1650 #ifdef EXTRA_SHLIB_EXTENSION
1651 + strlen (EXTRA_SHLIB_EXTENSION)
1653 + sizeof "/lib.so");
1655 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1657 #ifdef EXTRA_SHLIB_EXTENSION
1658 /* Try the .so extension first. If that fails build a new filename
1659 using EXTRA_SHLIB_EXTENSION. */
1660 if (! ldfile_try_open_bfd (string, entry))
1662 sprintf (string, "%s/lib%s%s%s", search->name,
1663 filename, arch, EXTRA_SHLIB_EXTENSION);
1666 if (! ldfile_try_open_bfd (string, entry))
1671 #ifdef EXTRA_SHLIB_EXTENSION
1675 entry->filename = string;
1677 /* We have found a dynamic object to include in the link. The ELF
1678 backend linker will create a DT_NEEDED entry in the .dynamic
1679 section naming this file. If this file includes a DT_SONAME
1680 entry, it will be used. Otherwise, the ELF linker will just use
1681 the name of the file. For an archive found by searching, like
1682 this one, the DT_NEEDED entry should consist of just the name of
1683 the file, without the path information used to find it. Note
1684 that we only need to do this if we have a dynamic object; an
1685 archive will never be referenced by a DT_NEEDED entry.
1687 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1688 very pretty. I haven't been able to think of anything that is
1690 if (bfd_check_format (entry->the_bfd, bfd_object)
1691 && (entry->the_bfd->flags & DYNAMIC) != 0)
1693 ASSERT (entry->flags.maybe_archive && entry->flags.search_dirs);
1695 /* Rather than duplicating the logic above. Just use the
1696 filename we recorded earlier. */
1698 filename = lbasename (entry->filename);
1699 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1708 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1711 /* A variant of lang_output_section_find used by place_orphan. */
1713 static lang_output_section_statement_type *
1714 output_rel_find (asection *sec, int isdyn)
1716 lang_output_section_statement_type *lookup;
1717 lang_output_section_statement_type *last = NULL;
1718 lang_output_section_statement_type *last_alloc = NULL;
1719 lang_output_section_statement_type *last_ro_alloc = NULL;
1720 lang_output_section_statement_type *last_rel = NULL;
1721 lang_output_section_statement_type *last_rel_alloc = NULL;
1722 int rela = sec->name[4] == 'a';
1724 for (lookup = &lang_output_section_statement.head->output_section_statement;
1726 lookup = lookup->next)
1728 if (lookup->constraint >= 0
1729 && CONST_STRNEQ (lookup->name, ".rel"))
1731 int lookrela = lookup->name[4] == 'a';
1733 /* .rel.dyn must come before all other reloc sections, to suit
1738 /* Don't place after .rel.plt as doing so results in wrong
1740 if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
1743 if (rela == lookrela || last_rel == NULL)
1745 if ((rela == lookrela || last_rel_alloc == NULL)
1746 && lookup->bfd_section != NULL
1747 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1748 last_rel_alloc = lookup;
1752 if (lookup->bfd_section != NULL
1753 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1755 last_alloc = lookup;
1756 if ((lookup->bfd_section->flags & SEC_READONLY) != 0)
1757 last_ro_alloc = lookup;
1762 return last_rel_alloc;
1768 return last_ro_alloc;
1776 /* Place an orphan section. We use this to put random SHF_ALLOC
1777 sections in the right segment. */
1779 static lang_output_section_statement_type *
1780 gld${EMULATION_NAME}_place_orphan (asection *s,
1781 const char *secname,
1784 static struct orphan_save hold[] =
1787 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1790 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1793 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1799 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1802 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1805 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
1811 enum orphan_save_index
1822 static int orphan_init_done = 0;
1823 struct orphan_save *place;
1824 lang_output_section_statement_type *after;
1825 lang_output_section_statement_type *os;
1826 lang_output_section_statement_type *match_by_name = NULL;
1828 int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
1829 unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
1831 if (! link_info.relocatable
1832 && link_info.combreloc
1833 && (s->flags & SEC_ALLOC))
1839 secname = ".rela.dyn";
1843 secname = ".rel.dyn";
1849 else if (CONST_STRNEQ (secname, ".rel"))
1851 secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1856 /* Look through the script to see where to place this section. */
1857 if (constraint == 0)
1858 for (os = lang_output_section_find (secname);
1860 os = next_matching_output_section_statement (os, 0))
1862 /* If we don't match an existing output section, tell
1863 lang_insert_orphan to create a new output section. */
1864 constraint = SPECIAL;
1866 if (os->bfd_section != NULL
1867 && (os->bfd_section->flags == 0
1868 || (_bfd_elf_match_sections_by_type (link_info.output_bfd,
1871 && ((s->flags ^ os->bfd_section->flags)
1872 & (SEC_LOAD | SEC_ALLOC)) == 0)))
1874 /* We already have an output section statement with this
1875 name, and its bfd section has compatible flags.
1876 If the section already exists but does not have any flags
1877 set, then it has been created by the linker, probably as a
1878 result of a --section-start command line switch. */
1879 lang_add_section (&os->children, s, NULL, os);
1883 /* Save unused output sections in case we can match them
1884 against orphans later. */
1885 if (os->bfd_section == NULL)
1889 /* If we didn't match an active output section, see if we matched an
1890 unused one and use that. */
1893 lang_add_section (&match_by_name->children, s, NULL, match_by_name);
1894 return match_by_name;
1897 if (!orphan_init_done)
1899 struct orphan_save *ho;
1901 for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1902 if (ho->name != NULL)
1904 ho->os = lang_output_section_find (ho->name);
1905 if (ho->os != NULL && ho->os->flags == 0)
1906 ho->os->flags = ho->flags;
1908 orphan_init_done = 1;
1911 /* If this is a final link, then always put .gnu.warning.SYMBOL
1912 sections into the .text section to get them out of the way. */
1913 if (link_info.executable
1914 && ! link_info.relocatable
1915 && CONST_STRNEQ (s->name, ".gnu.warning.")
1916 && hold[orphan_text].os != NULL)
1918 os = hold[orphan_text].os;
1919 lang_add_section (&os->children, s, NULL, os);
1923 /* Decide which segment the section should go in based on the
1924 section name and section flags. We put loadable .note sections
1925 right after the .interp section, so that the PT_NOTE segment is
1926 stored right after the program headers where the OS can read it
1927 in the first page. */
1930 if ((s->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
1931 place = &hold[orphan_nonalloc];
1932 else if ((s->flags & SEC_ALLOC) == 0)
1934 else if ((s->flags & SEC_LOAD) != 0
1935 && ((iself && sh_type == SHT_NOTE)
1936 || (!iself && CONST_STRNEQ (secname, ".note"))))
1937 place = &hold[orphan_interp];
1938 else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
1939 place = &hold[orphan_bss];
1940 else if ((s->flags & SEC_SMALL_DATA) != 0)
1941 place = &hold[orphan_sdata];
1942 else if ((s->flags & SEC_READONLY) == 0)
1943 place = &hold[orphan_data];
1944 else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
1945 || (!iself && CONST_STRNEQ (secname, ".rel")))
1946 && (s->flags & SEC_LOAD) != 0)
1947 place = &hold[orphan_rel];
1948 else if ((s->flags & SEC_CODE) == 0)
1949 place = &hold[orphan_rodata];
1951 place = &hold[orphan_text];
1956 if (place->os == NULL)
1958 if (place->name != NULL)
1959 place->os = lang_output_section_find (place->name);
1961 place->os = output_rel_find (s, isdyn);
1965 after = lang_output_section_find_by_flags
1966 (s, &place->os, _bfd_elf_match_sections_by_type);
1968 /* *ABS* is always the first output section statement. */
1969 after = &lang_output_section_statement.head->output_section_statement;
1972 return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL);
1977 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
1981 gld${EMULATION_NAME}_after_allocation (void)
1983 bfd_boolean need_layout = bfd_elf_discard_info (link_info.output_bfd,
1985 gld${EMULATION_NAME}_map_segments (need_layout);
1990 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1994 gld${EMULATION_NAME}_get_script (int *isfile)
1997 if test -n "$COMPILE_IN"
1999 # Scripts compiled in.
2001 # sed commands to quote an ld script as a C string.
2002 sc="-f stringify.sed"
2008 if (link_info.relocatable && config.build_constructors)
2011 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
2012 echo ' ; else if (link_info.relocatable) return' >> e${EMULATION_NAME}.c
2013 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
2014 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
2015 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
2016 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
2017 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
2018 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
2020 if test -n "$GENERATE_PIE_SCRIPT" ; then
2021 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2022 echo ' ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c
2023 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2024 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2025 sed $sc ldscripts/${EMULATION_NAME}.xdw >> e${EMULATION_NAME}.c
2026 echo ' ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2027 sed $sc ldscripts/${EMULATION_NAME}.xdc >> e${EMULATION_NAME}.c
2029 echo ' ; else if (link_info.pie) return' >> e${EMULATION_NAME}.c
2030 sed $sc ldscripts/${EMULATION_NAME}.xd >> e${EMULATION_NAME}.c
2032 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2033 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2034 echo ' ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c
2035 echo ' && link_info.relro' >> e${EMULATION_NAME}.c
2036 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2037 sed $sc ldscripts/${EMULATION_NAME}.xsw >> e${EMULATION_NAME}.c
2038 echo ' ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c
2039 sed $sc ldscripts/${EMULATION_NAME}.xsc >> e${EMULATION_NAME}.c
2041 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
2042 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
2044 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2045 echo ' ; else if (link_info.combreloc && link_info.relro' >> e${EMULATION_NAME}.c
2046 echo ' && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
2047 sed $sc ldscripts/${EMULATION_NAME}.xw >> e${EMULATION_NAME}.c
2048 echo ' ; else if (link_info.combreloc) return' >> e${EMULATION_NAME}.c
2049 sed $sc ldscripts/${EMULATION_NAME}.xc >> e${EMULATION_NAME}.c
2051 echo ' ; else return' >> e${EMULATION_NAME}.c
2052 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
2053 echo '; }' >> e${EMULATION_NAME}.c
2056 # Scripts read from the filesystem.
2062 if (link_info.relocatable && config.build_constructors)
2063 return "ldscripts/${EMULATION_NAME}.xu";
2064 else if (link_info.relocatable)
2065 return "ldscripts/${EMULATION_NAME}.xr";
2066 else if (!config.text_read_only)
2067 return "ldscripts/${EMULATION_NAME}.xbn";
2069 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
2072 else if (!config.magic_demand_paged)
2073 return "ldscripts/${EMULATION_NAME}.xn";
2076 if test -n "$GENERATE_PIE_SCRIPT" ; then
2077 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2079 else if (link_info.pie && link_info.combreloc
2080 && link_info.relro && (link_info.flags & DF_BIND_NOW))
2081 return "ldscripts/${EMULATION_NAME}.xdw";
2082 else if (link_info.pie && link_info.combreloc)
2083 return "ldscripts/${EMULATION_NAME}.xdc";
2087 else if (link_info.pie)
2088 return "ldscripts/${EMULATION_NAME}.xd";
2091 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2092 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2094 else if (link_info.shared && link_info.combreloc
2095 && link_info.relro && (link_info.flags & DF_BIND_NOW))
2096 return "ldscripts/${EMULATION_NAME}.xsw";
2097 else if (link_info.shared && link_info.combreloc)
2098 return "ldscripts/${EMULATION_NAME}.xsc";
2102 else if (link_info.shared)
2103 return "ldscripts/${EMULATION_NAME}.xs";
2106 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2108 else if (link_info.combreloc && link_info.relro
2109 && (link_info.flags & DF_BIND_NOW))
2110 return "ldscripts/${EMULATION_NAME}.xw";
2111 else if (link_info.combreloc)
2112 return "ldscripts/${EMULATION_NAME}.xc";
2117 return "ldscripts/${EMULATION_NAME}.x";
2124 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
2126 $PARSE_AND_LIST_PROLOGUE
2132 #define OPTION_DISABLE_NEW_DTAGS (400)
2133 #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
2134 #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
2135 #define OPTION_EH_FRAME_HDR (OPTION_GROUP + 1)
2136 #define OPTION_EXCLUDE_LIBS (OPTION_EH_FRAME_HDR + 1)
2137 #define OPTION_HASH_STYLE (OPTION_EXCLUDE_LIBS + 1)
2138 #define OPTION_BUILD_ID (OPTION_HASH_STYLE + 1)
2139 #define OPTION_AUDIT (OPTION_BUILD_ID + 1)
2142 gld${EMULATION_NAME}_add_options
2143 (int ns, char **shortopts, int nl, struct option **longopts,
2144 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
2147 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2149 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:P:";
2153 static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
2157 static const struct option xtra_long[] = {
2159 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2161 {"audit", required_argument, NULL, OPTION_AUDIT},
2162 {"Bgroup", no_argument, NULL, OPTION_GROUP},
2166 {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
2168 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2170 {"depaudit", required_argument, NULL, 'P'},
2171 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
2172 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
2173 {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
2174 {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
2175 {"hash-style", required_argument, NULL, OPTION_HASH_STYLE},
2178 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
2180 $PARSE_AND_LIST_LONGOPTS
2184 {NULL, no_argument, NULL, 0}
2187 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
2188 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
2189 *longopts = (struct option *)
2190 xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
2191 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
2194 #define DEFAULT_BUILD_ID_STYLE "sha1"
2197 gld${EMULATION_NAME}_handle_option (int optc)
2204 case OPTION_BUILD_ID:
2205 if (link_info.emit_note_gnu_build_id != NULL)
2207 free (link_info.emit_note_gnu_build_id);
2208 link_info.emit_note_gnu_build_id = NULL;
2211 optarg = DEFAULT_BUILD_ID_STYLE;
2212 if (strcmp (optarg, "none"))
2213 link_info.emit_note_gnu_build_id = xstrdup (optarg);
2218 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2221 gld${EMULATION_NAME}_append_to_separated_string (&audit, optarg);
2225 gld${EMULATION_NAME}_append_to_separated_string (&depaudit, optarg);
2228 case OPTION_DISABLE_NEW_DTAGS:
2229 link_info.new_dtags = FALSE;
2232 case OPTION_ENABLE_NEW_DTAGS:
2233 link_info.new_dtags = TRUE;
2236 case OPTION_EH_FRAME_HDR:
2237 link_info.eh_frame_hdr = TRUE;
2241 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
2242 /* Groups must be self-contained. */
2243 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2244 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
2247 case OPTION_EXCLUDE_LIBS:
2248 add_excluded_libs (optarg);
2251 case OPTION_HASH_STYLE:
2252 link_info.emit_hash = FALSE;
2253 link_info.emit_gnu_hash = FALSE;
2254 if (strcmp (optarg, "sysv") == 0)
2255 link_info.emit_hash = TRUE;
2256 else if (strcmp (optarg, "gnu") == 0)
2257 link_info.emit_gnu_hash = TRUE;
2258 else if (strcmp (optarg, "both") == 0)
2260 link_info.emit_hash = TRUE;
2261 link_info.emit_gnu_hash = TRUE;
2264 einfo (_("%P%F: invalid hash style \`%s'\n"), optarg);
2271 if (strcmp (optarg, "defs") == 0)
2272 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2273 else if (strcmp (optarg, "muldefs") == 0)
2274 link_info.allow_multiple_definition = TRUE;
2275 else if (CONST_STRNEQ (optarg, "max-page-size="))
2279 config.maxpagesize = strtoul (optarg + 14, &end, 0);
2280 if (*end || (config.maxpagesize & (config.maxpagesize - 1)) != 0)
2281 einfo (_("%P%F: invalid maxium page size \`%s'\n"),
2284 else if (CONST_STRNEQ (optarg, "common-page-size="))
2287 config.commonpagesize = strtoul (optarg + 17, &end, 0);
2289 || (config.commonpagesize & (config.commonpagesize - 1)) != 0)
2290 einfo (_("%P%F: invalid common page size \`%s'\n"),
2293 else if (CONST_STRNEQ (optarg, "stack-size="))
2296 link_info.stacksize = strtoul (optarg + 11, &end, 0);
2297 if (*end || link_info.stacksize < 0)
2298 einfo (_("%P%F: invalid stack size \`%s'\n"), optarg + 11);
2299 if (!link_info.stacksize)
2300 /* Use -1 for explicit no-stack, because zero means
2302 link_info.stacksize = -1;
2304 else if (strcmp (optarg, "execstack") == 0)
2306 link_info.execstack = TRUE;
2307 link_info.noexecstack = FALSE;
2309 else if (strcmp (optarg, "noexecstack") == 0)
2311 link_info.noexecstack = TRUE;
2312 link_info.execstack = FALSE;
2315 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2317 else if (strcmp (optarg, "global") == 0)
2318 link_info.flags_1 |= (bfd_vma) DF_1_GLOBAL;
2319 else if (strcmp (optarg, "initfirst") == 0)
2320 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
2321 else if (strcmp (optarg, "interpose") == 0)
2322 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
2323 else if (strcmp (optarg, "loadfltr") == 0)
2324 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
2325 else if (strcmp (optarg, "nodefaultlib") == 0)
2326 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
2327 else if (strcmp (optarg, "nodelete") == 0)
2328 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
2329 else if (strcmp (optarg, "nodlopen") == 0)
2330 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
2331 else if (strcmp (optarg, "nodump") == 0)
2332 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
2333 else if (strcmp (optarg, "now") == 0)
2335 link_info.flags |= (bfd_vma) DF_BIND_NOW;
2336 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
2338 else if (strcmp (optarg, "lazy") == 0)
2340 link_info.flags &= ~(bfd_vma) DF_BIND_NOW;
2341 link_info.flags_1 &= ~(bfd_vma) DF_1_NOW;
2343 else if (strcmp (optarg, "origin") == 0)
2345 link_info.flags |= (bfd_vma) DF_ORIGIN;
2346 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
2348 else if (strcmp (optarg, "combreloc") == 0)
2349 link_info.combreloc = TRUE;
2350 else if (strcmp (optarg, "nocombreloc") == 0)
2351 link_info.combreloc = FALSE;
2352 else if (strcmp (optarg, "nocopyreloc") == 0)
2353 link_info.nocopyreloc = TRUE;
2354 else if (strcmp (optarg, "relro") == 0)
2355 link_info.relro = TRUE;
2356 else if (strcmp (optarg, "norelro") == 0)
2357 link_info.relro = FALSE;
2358 else if (strcmp (optarg, "text") == 0)
2359 link_info.error_textrel = TRUE;
2360 else if (strcmp (optarg, "notext") == 0)
2361 link_info.error_textrel = FALSE;
2362 else if (strcmp (optarg, "textoff") == 0)
2363 link_info.error_textrel = FALSE;
2369 einfo (_("%P: warning: -z %s ignored.\n"), optarg);
2373 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
2375 $PARSE_AND_LIST_ARGS_CASES
2387 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
2391 gld${EMULATION_NAME}_list_options (FILE * file)
2394 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2397 --audit=AUDITLIB Specify a library to use for auditing\n"));
2399 -Bgroup Selects group name lookup rules for DSO\n"));
2404 --build-id[=STYLE] Generate build ID note\n"));
2406 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2409 -P AUDITLIB, --depaudit=AUDITLIB\n" "\
2410 Specify a library to use for auditing dependencies\n"));
2412 --disable-new-dtags Disable new dynamic tags\n"));
2414 --enable-new-dtags Enable new dynamic tags\n"));
2416 --eh-frame-hdr Create .eh_frame_hdr section\n"));
2418 --exclude-libs=LIBS Make all symbols in LIBS hidden\n"));
2420 --hash-style=STYLE Set hash style to sysv, gnu or both\n"));
2422 -z combreloc Merge dynamic relocs into one section and sort\n"));
2428 -z common-page-size=SIZE Set common page size to SIZE\n"));
2430 -z defs Report unresolved symbols in object files.\n"));
2432 -z execstack Mark executable as requiring executable stack\n"));
2435 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2438 -z global Make symbols in DSO available for subsequently\n\
2439 loaded objects\n"));
2441 -z initfirst Mark DSO to be initialized first at runtime\n"));
2443 -z interpose Mark object to interpose all DSOs but executable\n"));
2445 -z lazy Mark object lazy runtime binding (default)\n"));
2447 -z loadfltr Mark object requiring immediate process\n"));
2453 -z max-page-size=SIZE Set maximum page size to SIZE\n"));
2455 -z muldefs Allow multiple definitions\n"));
2458 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2461 -z nocombreloc Don't merge dynamic relocs into one section\n"));
2463 -z nocopyreloc Don't create copy relocs\n"));
2465 -z nodefaultlib Mark object not to use default search paths\n"));
2467 -z nodelete Mark DSO non-deletable at runtime\n"));
2469 -z nodlopen Mark DSO not available to dlopen\n"));
2471 -z nodump Mark DSO not available to dldump\n"));
2476 -z noexecstack Mark executable as not requiring executable stack\n"));
2478 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2481 -z norelro Don't create RELRO program header\n"));
2483 -z now Mark object non-lazy runtime binding\n"));
2485 -z origin Mark object requiring immediate \$ORIGIN\n\
2486 processing at runtime\n"));
2488 -z relro Create RELRO program header\n"));
2490 -z stacksize=SIZE Set size of stack segment\n"));
2494 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
2496 $PARSE_AND_LIST_OPTIONS
2504 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
2506 $PARSE_AND_LIST_EPILOGUE
2513 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2515 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
2516 ${LDEMUL_SYSLIB-syslib_default},
2517 ${LDEMUL_HLL-hll_default},
2518 ${LDEMUL_AFTER_PARSE-after_parse_default},
2519 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
2520 ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation},
2521 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
2522 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
2523 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
2524 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
2525 "${EMULATION_NAME}",
2527 ${LDEMUL_FINISH-finish_default},
2528 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
2529 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
2530 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
2531 ${LDEMUL_SET_SYMBOLS-NULL},
2532 ${LDEMUL_PARSE_ARGS-NULL},
2533 gld${EMULATION_NAME}_add_options,
2534 gld${EMULATION_NAME}_handle_option,
2535 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
2536 ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
2537 ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
2538 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
2539 ${LDEMUL_NEW_VERS_PATTERN-NULL}