elf32.em
来自「基于4个mips核的noc设计」· EM 代码 · 共 1,533 行 · 第 1/3 页
EM
1,533 行
# This shell script emits a C file. -*- C -*-# It does some substitutions.# This file is now misnamed, because it supports both 32 bit and 64 bit# ELF emulations.test -z "${ELFSIZE}" && ELFSIZE=32cat >e${EMULATION_NAME}.c <<EOF/* This file is is generated by a shell script. DO NOT EDIT! *//* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME} Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. Written by Steve Chamberlain <sac@cygnus.com> ELF support by Ian Lance Taylor <ian@cygnus.com>This file is part of GLD, the Gnu Linker.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#define TARGET_IS_${EMULATION_NAME}#include "bfd.h"#include "sysdep.h"#include "libiberty.h"#include <ctype.h>#include "bfdlink.h"#include "ld.h"#include "ldmain.h"#include "ldmisc.h"#include "ldexp.h"#include "ldlang.h"#include "ldfile.h"#include "ldemul.h"#include "ldgram.h"#include "elf/common.h"static void gld${EMULATION_NAME}_before_parse PARAMS ((void));static void gld${EMULATION_NAME}_vercheck PARAMS ((lang_input_statement_type *));static void gld${EMULATION_NAME}_stat_needed PARAMS ((lang_input_statement_type *));static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));static boolean gld${EMULATION_NAME}_search_needed PARAMS ((const char *, const char *, int));static void gld${EMULATION_NAME}_check_needed PARAMS ((lang_input_statement_type *));static void gld${EMULATION_NAME}_after_open PARAMS ((void));static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));static void gld${EMULATION_NAME}_find_statement_assignment PARAMS ((lang_statement_union_type *));static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));static boolean gld${EMULATION_NAME}_open_dynamic_archive PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));static lang_output_section_statement_type *output_rel_find PARAMS ((void));static boolean gld${EMULATION_NAME}_place_orphan PARAMS ((lang_input_statement_type *, asection *));static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));EOF# Import any needed special functions and/or overrides.#if test -n "$EXTRA_EM_FILE" ; then. ${srcdir}/emultempl/${EXTRA_EM_FILE}.emfi# Functions in this file can be overridden by setting the LDEMUL_* shell# variables. If the name of the overriding function is the same as is# defined in this file, then don't output this file's version.# If a different overriding name is given then output the standard function# as presumably it is called from the overriding function.#if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; thencat >>e${EMULATION_NAME}.c <<EOFstatic voidgld${EMULATION_NAME}_before_parse (){ ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`; config.dynamic_link = ${DYNAMIC_LINK-true}; config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;}EOFficat >>e${EMULATION_NAME}.c <<EOF/* These variables are required to pass information back and forth between after_open and check_needed and stat_needed and vercheck. */static struct bfd_link_needed_list *global_needed;static struct stat global_stat;static boolean global_found;static struct bfd_link_needed_list *global_vercheck_needed;static boolean global_vercheck_failed;/* On Linux, it's possible to have different versions of the same shared library linked against different versions of libc. The dynamic linker somehow tags which libc version to use in /etc/ld.so.cache, and, based on the libc that it sees in the executable, chooses which version of the shared library to use. We try to do a similar check here by checking whether this shared library needs any other shared libraries which may conflict with libraries we have already included in the link. If it does, we skip it, and try to find another shared library farther on down the link path. This is called via lang_for_each_input_file. GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find a conflicting version. */static voidgld${EMULATION_NAME}_vercheck (s) lang_input_statement_type *s;{ const char *soname; struct bfd_link_needed_list *l; if (global_vercheck_failed) return; if (s->the_bfd == NULL || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0) return; soname = bfd_elf_get_dt_soname (s->the_bfd); if (soname == NULL) soname = basename (bfd_get_filename (s->the_bfd)); for (l = global_vercheck_needed; l != NULL; l = l->next) { const char *suffix; if (strcmp (soname, l->name) == 0) { /* Probably can't happen, but it's an easy check. */ continue; } if (strchr (l->name, '/') != NULL) continue; suffix = strstr (l->name, ".so."); if (suffix == NULL) continue; suffix += sizeof ".so." - 1; if (strncmp (soname, l->name, suffix - l->name) == 0) { /* Here we know that S is a dynamic object FOO.SO.VER1, and the object we are considering needs a dynamic object FOO.SO.VER2, and VER1 and VER2 are different. This appears to be a version mismatch, so we tell the caller to try a different version of this library. */ global_vercheck_failed = true; return; } }}/* See if an input file matches a DT_NEEDED entry by running stat on the file. */static voidgld${EMULATION_NAME}_stat_needed (s) lang_input_statement_type *s;{ struct stat st; const char *suffix; const char *soname; if (global_found) return; if (s->the_bfd == NULL) return; if (bfd_stat (s->the_bfd, &st) != 0) { einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd); return; } if (st.st_dev == global_stat.st_dev && st.st_ino == global_stat.st_ino) { global_found = true; return; } /* We issue a warning if it looks like we are including two different versions of the same shared library. For example, there may be a problem if -lc picks up libc.so.6 but some other shared library has a DT_NEEDED entry of libc.so.5. This is a heuristic test, and it will only work if the name looks like NAME.so.VERSION. FIXME: Depending on file names is error-prone. If we really want to issue warnings about mixing version numbers of shared libraries, we need to find a better way. */ if (strchr (global_needed->name, '/') != NULL) return; suffix = strstr (global_needed->name, ".so."); if (suffix == NULL) return; suffix += sizeof ".so." - 1; soname = bfd_elf_get_dt_soname (s->the_bfd); if (soname == NULL) soname = basename (s->filename); if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0) einfo ("%P: warning: %s, needed by %B, may conflict with %s\n", global_needed->name, global_needed->by, soname);}/* This function is called for each possible name for a dynamic object named by a DT_NEEDED entry. The FORCE parameter indicates whether to skip the check for a conflicting version. */static booleangld${EMULATION_NAME}_try_needed (name, force) const char *name; int force;{ bfd *abfd; const char *soname; abfd = bfd_openr (name, bfd_get_target (output_bfd)); if (abfd == NULL) return false; if (! bfd_check_format (abfd, bfd_object)) { (void) bfd_close (abfd); return false; } if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0) { (void) bfd_close (abfd); return false; } /* Check whether this object would include any conflicting library versions. If FORCE is set, then we skip this check; we use this the second time around, if we couldn't find any compatible instance of the shared library. */ if (! force) { struct bfd_link_needed_list *needed; if (! bfd_elf_get_bfd_needed_list (abfd, &needed)) einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd); if (needed != NULL) { global_vercheck_needed = needed; global_vercheck_failed = false; lang_for_each_input_file (gld${EMULATION_NAME}_vercheck); if (global_vercheck_failed) { (void) bfd_close (abfd); /* Return false to force the caller to move on to try another file on the search path. */ return false; } /* But wait! It gets much worse. On Linux, if a shared library does not use libc at all, we are supposed to skip it the first time around in case we encounter a shared library later on with the same name which does use the version of libc that we want. This is much too horrible to use on any system other than Linux. */EOFcase ${target} in *-*-linux-gnu*) cat >>e${EMULATION_NAME}.c <<EOF { struct bfd_link_needed_list *l; for (l = needed; l != NULL; l = l->next) if (strncmp (l->name, "libc.so", 7) == 0) break; if (l == NULL) { (void) bfd_close (abfd); return false; } }EOF ;;esaccat >>e${EMULATION_NAME}.c <<EOF } } /* We've found a dynamic object matching the DT_NEEDED entry. */ /* We have already checked that there is no other input file of the same name. We must now check again that we are not including the same file twice. We need to do this because on many systems libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will reference libc.so.1. If we have already included libc.so, we don't want to include libc.so.1 if they are the same file, and we can only check that using stat. */ if (bfd_stat (abfd, &global_stat) != 0) einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd); /* First strip off everything before the last '/'. */ soname = basename (abfd->filename); if (trace_file_tries) info_msg (_("found %s at %s\n"), soname, name); global_found = false; lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed); if (global_found) { /* Return true to indicate that we found the file, even though we aren't going to do anything with it. */ return true; } /* Tell the ELF backend that we don't want the output file to have a DT_NEEDED entry for this file. */ bfd_elf_set_dt_needed_name (abfd, ""); /* Tell the ELF backend that the output file needs a DT_NEEDED entry for this file if it is used to resolve the reference in a regular object. */ bfd_elf_set_dt_needed_soname (abfd, soname); /* Add this file into the symbol table. */ if (! bfd_link_add_symbols (abfd, &link_info)) einfo ("%F%B: could not read symbols: %E\n", abfd); return true;}/* Search for a needed file in a path. */static booleangld${EMULATION_NAME}_search_needed (path, name, force) const char *path; const char *name; int force;{ const char *s; size_t len; if (name[0] == '/') return gld${EMULATION_NAME}_try_needed (name, force); if (path == NULL || *path == '\0') return false; len = strlen (name); while (1) { char *filename, *sset; s = strchr (path, ':'); if (s == NULL) s = path + strlen (path); filename = (char *) xmalloc (s - path + len + 2); if (s == path) sset = filename; else { memcpy (filename, path, s - path); filename[s - path] = '/'; sset = filename + (s - path) + 1; } strcpy (sset, name); if (gld${EMULATION_NAME}_try_needed (filename, force)) return true; free (filename); if (*s == '\0') break; path = s + 1; } return false;}EOFif [ "x${host}" = "x${target}" ] ; then case " ${EMULATION_LIBPATH} " in *" ${EMULATION_NAME} "*) case ${target} in *-*-linux-gnu*) cat >>e${EMULATION_NAME}.c <<EOF/* For a native linker, check the file /etc/ld.so.conf for directories in which we may find shared libraries. /etc/ld.so.conf is really only meaningful on Linux. */static boolean gld${EMULATION_NAME}_check_ld_so_conf PARAMS ((const char *, int));static booleangld${EMULATION_NAME}_check_ld_so_conf (name, force) const char *name; int force;{ static boolean initialized; static char *ld_so_conf; if (! initialized) { FILE *f; f = fopen ("/etc/ld.so.conf", FOPEN_RT); if (f != NULL) { char *b; size_t len, alloc; int c; len = 0; alloc = 100; b = (char *) xmalloc (alloc); while ((c = getc (f)) != EOF) { if (len + 1 >= alloc) { alloc *= 2; b = (char *) xrealloc (b, alloc); } if (c != ':' && c != ' ' && c != '\t' && c != '\n' && c != ',') { b[len] = c; ++len; } else { if (len > 0 && b[len - 1] != ':') { b[len] = ':'; ++len; } } } if (len > 0 && b[len - 1] == ':') --len; if (len > 0) b[len] = '\0'; else { free (b); b = NULL; } fclose (f); ld_so_conf = b; } initialized = true; } if (ld_so_conf == NULL) return false; return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);}EOF # Linux ;; esac esacficat >>e${EMULATION_NAME}.c <<EOF/* See if an input file matches a DT_NEEDED entry by name. */static void
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?