diff options
-rw-r--r-- | ChangeLog.cross | 18 | ||||
-rw-r--r-- | src/Makefile.am | 11 | ||||
-rw-r--r-- | src/dso-readonly.c | 1800 | ||||
-rw-r--r-- | src/gather.c | 69 | ||||
-rw-r--r-- | src/get.c | 70 | ||||
-rw-r--r-- | src/ld-do-lookup.h | 225 | ||||
-rw-r--r-- | src/ld-libs.c | 1345 | ||||
-rw-r--r-- | src/ld-libs.h | 113 | ||||
-rw-r--r-- | src/ld-lookup.c | 214 | ||||
-rw-r--r-- | src/ld-lookup64.c | 24 | ||||
-rw-r--r-- | src/main.c | 19 | ||||
-rw-r--r-- | src/prelink.h | 2 |
12 files changed, 3872 insertions, 38 deletions
diff --git a/ChangeLog.cross b/ChangeLog.cross index bacc9d7..06bfc89 100644 --- a/ChangeLog.cross +++ b/ChangeLog.cross @@ -1,3 +1,21 @@ +2006-08-09 Mark Hatle <mark.hatle@windriver.com> + + * cross-prelink code merged and upreved + original code from: + MontaVista Software, Inc. + CodeSourcery, LLC. + * Makefile.am: add prelink-rtld, libiberty + * dso-readonly.c: new file based on dso.c + * gather.c: use cross-rtld + * get.c: use cross-rtld + * ld-do-lookup.h: Add ld-do-lookup.h from glibc + * ld-libs.c: add cross-rtld functionality + * ld-libs.h: ditto + * ld-lookup.c: ditto + * ld-lookup64.c: ditto + * main.c: Add rtld option + * prelink.h: add extern rtld variable + 2006-08-09 CodeSourcery, LLC. * main.c: if we modified /sbin/init, re-run init diff --git a/src/Makefile.am b/src/Makefile.am index e5f3b8f..d5675ab 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,10 +4,10 @@ AUTOMAKE_OPTIONS = 1.4 gnu DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -Wall AM_CFLAGS = -Wall -AM_CPPFLAGS = -DSBINDIR='"@sbindir@"' +AM_CPPFLAGS = -DSBINDIR='"@sbindir@"' -DBINDIR='"@bindir@"' INCLUDES = @GELFINCLUDE@ -sbin_PROGRAMS = prelink +sbin_PROGRAMS = prelink prelink-rtld bin_PROGRAMS = execstack arch_SOURCES = arch-i386.c arch-alpha.c arch-ppc.c arch-ppc64.c \ @@ -21,9 +21,14 @@ prelink_SOURCES = cache.c conflict.c cxx.c doit.c exec.c execle_open.c get.c \ prelinktab.h reloc.c reloc.h space.c undo.c undoall.c \ verify.c md5.c md5.h sha.c sha.h \ $(common_SOURCES) $(arch_SOURCES) -prelink_LDADD = @LIBGELF@ +prelink_LDADD = @LIBGELF@ -liberty prelink_LDFLAGS = -all-static execstack_SOURCES = execstack.c $(common_SOURCES) $(arch_SOURCES) +prelink_rtld_LDADD = @LIBGELF@ -liberty +prelink_rtld_SOURCES = data.c dso-readonly.c ld-libs.c ld-lookup.c \ + canonicalize.c wrap-file.c ld-lookup64.c +prelink_rtld_LDFLAGS = -all-static + extra_DIST = makecrc.c diff --git a/src/dso-readonly.c b/src/dso-readonly.c new file mode 100644 index 0000000..4d10f18 --- /dev/null +++ b/src/dso-readonly.c @@ -0,0 +1,1800 @@ +/* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Red Hat, Inc. + Written by Jakub Jelinek <jakub@redhat.com>, 2001. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, 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 of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include <config.h> +#include <assert.h> +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <time.h> +#include <unistd.h> +#include <utime.h> +#include "prelink.h" + +#if defined HAVE_LIBSELINUX && defined HAVE_SELINUX_SELINUX_H +#include <selinux/selinux.h> +#define USE_SELINUX +#endif + +#define RELOCATE_SCN(shf) \ + ((shf) & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR)) + +#ifndef ELF_F_PERMISSIVE +# define ELF_F_PERMISSIVE 0 +#endif + +void +read_dynamic (DSO *dso) +{ + int i; + + memset (dso->info, 0, sizeof(dso->info)); + dso->info_set_mask = 0; + for (i = 0; i < dso->ehdr.e_shnum; i++) + if (dso->shdr[i].sh_type == SHT_DYNAMIC) + { + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[i]; + GElf_Dyn dyn; + + dso->dynamic = i; + while ((data = elf_getdata (scn, data)) != NULL) + { + int ndx, maxndx; + + maxndx = data->d_size / dso->shdr[i].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getdyn (dso->elf, data, ndx, &dyn); + if (dyn.d_tag == DT_NULL) + break; + else if ((GElf_Xword) dyn.d_tag < DT_NUM) + { + dso->info[dyn.d_tag] = dyn.d_un.d_val; + if (dyn.d_tag < 50) + dso->info_set_mask |= (1ULL << dyn.d_tag); + } + else if (dyn.d_tag == DT_CHECKSUM) + { + dso->info_DT_CHECKSUM = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_CHECKSUM_BIT); + } + else if (dyn.d_tag == DT_GNU_PRELINKED) + { + dso->info_DT_GNU_PRELINKED = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_GNU_PRELINKED_BIT); + } + else if (dyn.d_tag == DT_VERDEF) + { + dso->info_DT_VERDEF = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_VERDEF_BIT); + } + else if (dyn.d_tag == DT_VERNEED) + { + dso->info_DT_VERNEED = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_VERNEED_BIT); + } + else if (dyn.d_tag == DT_VERSYM) + { + dso->info_DT_VERSYM = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_VERSYM_BIT); + } + else if (dyn.d_tag == DT_FILTER) + dso->info_set_mask |= (1ULL << DT_FILTER_BIT); + else if (dyn.d_tag == DT_AUXILIARY) + dso->info_set_mask |= (1ULL << DT_AUXILIARY_BIT); + else if (dyn.d_tag == DT_LOPROC) + dso->info_set_mask |= (1ULL << DT_LOPROC_BIT); + else if (dyn.d_tag == DT_GNU_HASH) + { + dso->info_DT_GNU_HASH = dyn.d_un.d_val; + dso->info_set_mask |= (1ULL << DT_GNU_HASH_BIT); + } + } + if (ndx < maxndx) + break; + } + } +} + +int +set_dynamic (DSO *dso, GElf_Word tag, GElf_Addr value, int fatal) +{ + Elf_Data *data; + Elf_Scn *scn; + GElf_Dyn dyn; + int ndx, maxndx; + uint64_t mask = dso->info_set_mask; + + assert (dso->shdr[dso->dynamic].sh_type == SHT_DYNAMIC); + + scn = dso->scn[dso->dynamic]; + + data = elf_getdata (scn, NULL); + assert (elf_getdata (scn, data) == NULL); + + switch (tag) + { + case DT_CHECKSUM: + mask |= (1ULL << DT_CHECKSUM_BIT); break; + case DT_GNU_PRELINKED: + mask |= (1ULL << DT_GNU_PRELINKED_BIT); break; + case DT_VERDEF: + mask |= (1ULL << DT_VERDEF_BIT); break; + case DT_VERNEED: + mask |= (1ULL << DT_VERNEED_BIT); break; + case DT_VERSYM: + mask |= (1ULL << DT_VERSYM_BIT); break; + default: + if (tag < DT_NUM && tag < 50) + mask |= (1ULL << tag); + break; + } + + maxndx = data->d_size / dso->shdr[dso->dynamic].sh_entsize; + for (ndx = 0; ndx < maxndx; ndx++) + { + gelfx_getdyn (dso->elf, data, ndx, &dyn); + if (dyn.d_tag == DT_NULL) + break; + else if (dyn.d_tag == tag) + { + if (dyn.d_un.d_ptr != value) + { + dyn.d_un.d_ptr = value; + gelfx_update_dyn (dso->elf, data, ndx, &dyn); + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + } + + return 0; + } + } + assert (ndx < maxndx); + + if (ndx + 1 < maxndx) + { + /* DT_NULL is not the last dynamic entry. */ + gelfx_update_dyn (dso->elf, data, ndx + 1, &dyn); + dyn.d_tag = tag; + dyn.d_un.d_ptr = value; + gelfx_update_dyn (dso->elf, data, ndx, &dyn); + dso->info_set_mask = mask; + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + return 0; + } + + if (fatal) + error (0, 0, "%s: Not enough room to add .dynamic entry", + dso->filename); + return 1; +} + +int +check_dso (DSO *dso) +{ + int i, last = 1; + + /* FIXME: Several routines in prelink and in libelf-0.7.0 too + rely on sh_offset's monotonically increasing. */ + for (i = 2; i < dso->ehdr.e_shnum; ++i) + { + if (dso->shdr[last].sh_offset + + (dso->shdr[last].sh_type == SHT_NOBITS + ? 0 : dso->shdr[last].sh_size) > dso->shdr[i].sh_offset) + { + if (!dso->permissive + || RELOCATE_SCN (dso->shdr[last].sh_flags) + || RELOCATE_SCN (dso->shdr[i].sh_flags)) + { + error (0, 0, "%s: section file offsets not monotonically increasing", + dso->filename); + return 1; + } + } + if (!dso->permissive + || (dso->shdr[i].sh_type != SHT_NOBITS && dso->shdr[i].sh_size != 0)) + last = i; + } + return 0; +} + +DSO * +open_dso (const char *name) +{ + int fd; + + fd = wrap_open (name, O_RDONLY); + if (fd == -1) + { + error (0, errno, "cannot open \"%s\"", name); + return NULL; + } + return fdopen_dso (fd, name); +} + +/* WARNING: If prelink is ever multi-threaded, this will not work + Other alternatives are: + 1) make section_cmp nested function - trampolines + vs. non-exec stack needs to be resolved for it though + 2) make the variable __thread + 3) use locking around the qsort + */ +static DSO *section_cmp_dso; + +int +section_cmp (const void *A, const void *B) +{ + int *a = (int *) A; + int *b = (int *) B; + DSO *dso = section_cmp_dso; + + if (dso->shdr[*a].sh_offset < dso->shdr[*b].sh_offset) + return -1; + if (dso->shdr[*a].sh_offset > dso->shdr[*b].sh_offset) + return 1; + if (*a < *b) + return -1; + return *a > *b; +} + +DSO * +fdopen_dso (int fd, const char *name) +{ + Elf *elf = NULL; + GElf_Ehdr ehdr; + GElf_Addr last_off; + int i, j, k, last, *sections, *invsections; + DSO *dso = NULL; +#if 0 + struct PLArch *plarch; + extern struct PLArch __start_pl_arch[], __stop_pl_arch[]; +#endif + + elf = elf_begin (fd, ELF_C_READ, NULL); + if (elf == NULL) + { + error (0, 0, "cannot open ELF file: %s (dso_readonly)", elf_errmsg (-1)); + goto error_out; + } + + if (elf_kind (elf) != ELF_K_ELF) + { + error (0, 0, "\"%s\" is not an ELF file", name); + goto error_out; + } + + if (gelf_getehdr (elf, &ehdr) == NULL) + { + error (0, 0, "cannot get the ELF header: %s", + elf_errmsg (-1)); + goto error_out; + } + + if (ehdr.e_type != ET_DYN && ehdr.e_type != ET_EXEC) + { + error (0, 0, "\"%s\" is not a shared library", name); + goto error_out; + } + + if (ehdr.e_shnum == 0) + { + GElf_Phdr phdr; + + /* Check for UPX compressed executables. */ + if (ehdr.e_type == ET_EXEC + && ehdr.e_phnum > 0 + && (gelf_getphdr (elf, 0, &phdr), phdr.p_type == PT_LOAD) + && phdr.p_filesz >= 256 + && phdr.p_filesz <= 4096 + && phdr.p_offset == 0 + && ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize < phdr.p_filesz) + { + char *buf = alloca (phdr.p_filesz); + size_t start = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize; + + if (pread (fd, buf, phdr.p_filesz, 0) == phdr.p_filesz + && memmem (buf + start, phdr.p_filesz - start, + "UPX!", 4) != NULL) + { + error (0, 0, "\"%s\" is UPX compressed executable", name); + goto error_out; + } + } + error (0, 0, "\"%s\" has no section headers", name); + goto error_out; + } + + /* Allocate DSO structure. Leave place for additional 20 new section + headers. */ + dso = (DSO *) + malloc (sizeof(DSO) + (ehdr.e_shnum + 20) * sizeof(GElf_Shdr) + + (ehdr.e_phnum + 1) * sizeof(GElf_Phdr) + + (ehdr.e_shnum + 20) * sizeof(Elf_Scn *)); + if (!dso) + { + error (0, ENOMEM, "Could not open DSO"); + goto error_out; + } + + elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT | ELF_F_PERMISSIVE); + + memset (dso, 0, sizeof(DSO)); + dso->elf = elf; + dso->ehdr = ehdr; + dso->phdr = (GElf_Phdr *) &dso->shdr[ehdr.e_shnum + 20]; + dso->scn = (Elf_Scn **) &dso->phdr[ehdr.e_phnum + 1]; + switch (ehdr.e_ident[EI_CLASS]) + { + case ELFCLASS32: + dso->mask = 0xffffffff; break; + case ELFCLASS64: + dso->mask = 0xffffffffffffffffULL; break; + } + for (i = 0; i < ehdr.e_phnum; ++i) + gelf_getphdr (elf, i, dso->phdr + i); + dso->fd = fd; + + for (i = 0, j = 0; i < ehdr.e_shnum; ++i) + { + dso->scn[i] = elf_getscn (elf, i); + gelfx_getshdr (elf, dso->scn[i], dso->shdr + i); + if ((dso->shdr[i].sh_flags & SHF_ALLOC) && dso->shdr[i].sh_type != SHT_NOBITS) + j = 1; + } + if (j == 0) + { + /* If all ALLOC sections are SHT_NOBITS, then this is a + stripped-to-file debuginfo. Skip it silently. */ + goto error_out; + } + + sections = (int *) alloca (dso->ehdr.e_shnum * sizeof (int) * 2); + sections[0] = 0; + for (i = 1, j = 1, k = dso->ehdr.e_shnum, last = -1; + i < dso->ehdr.e_shnum; ++i) + if (RELOCATE_SCN (dso->shdr[i].sh_flags)) + { + last = i; + sections[j++] = i; + } + else + sections[--k] = i; + assert (j == k); + + section_cmp_dso = dso; + qsort (sections + k, dso->ehdr.e_shnum - k, sizeof (*sections), section_cmp); + invsections = sections + dso->ehdr.e_shnum; + invsections[0] = 0; + for (i = 1, j = 0; i < ehdr.e_shnum; ++i) + { + if (i != sections[i]) + { + j = 1; + dso->scn[i] = elf_getscn (elf, sections[i]); + gelfx_getshdr (elf, dso->scn[i], dso->shdr + i); + } + invsections[sections[i]] = i; + } + +#if 0 + if (j) + { + dso->move = init_section_move (dso); + if (dso->move == NULL) + goto error_out; + memcpy (dso->move->old_to_new, invsections, dso->ehdr.e_shnum * sizeof (int)); + memcpy (dso->move->new_to_old, sections, dso->ehdr.e_shnum * sizeof (int)); + } +#endif + + last_off = 0; + for (i = 1; i < ehdr.e_shnum; ++i) + { + if (dso->shdr[i].sh_link >= ehdr.e_shnum) + { + error (0, 0, "%s: bogus sh_link value %d", name, + dso->shdr[i].sh_link); + goto error_out; + } + dso->shdr[i].sh_link = invsections[dso->shdr[i].sh_link]; + if (dso->shdr[i].sh_type == SHT_REL + || dso->shdr[i].sh_type == SHT_RELA + || (dso->shdr[i].sh_flags & SHF_INFO_LINK)) + { + if (dso->shdr[i].sh_info >= ehdr.e_shnum) + { + error (0, 0, "%s: bogus sh_info value %d", name, + dso->shdr[i].sh_info); + goto error_out; + } + dso->shdr[i].sh_info = invsections[dso->shdr[i].sh_info]; + } + + /* Some linkers mess up sh_offset fields for empty or nobits + sections. */ + if (RELOCATE_SCN (dso->shdr[i].sh_flags) + && (dso->shdr[i].sh_size == 0 + || dso->shdr[i].sh_type == SHT_NOBITS)) + { + for (j = i + 1; j < ehdr.e_shnum; ++j) + if (! RELOCATE_SCN (dso->shdr[j].sh_flags)) + break; + else if (dso->shdr[j].sh_size != 0 + && dso->shdr[j].sh_type != SHT_NOBITS) + break; + dso->shdr[i].sh_offset = (last_off + dso->shdr[i].sh_addralign - 1) + & ~(dso->shdr[i].sh_addralign - 1); + if (j < ehdr.e_shnum + && dso->shdr[i].sh_offset > dso->shdr[j].sh_offset) + { + GElf_Addr k; + + for (k = dso->shdr[i].sh_addralign - 1; k; ) + { + k >>= 1; + dso->shdr[i].sh_offset = (last_off + k) & ~k; + if (dso->shdr[i].sh_offset <= dso->shdr[j].sh_offset) + break; + } + } + last_off = dso->shdr[i].sh_offset; + } + else + last_off = dso->shdr[i].sh_offset + dso->shdr[i].sh_size; + } + dso->ehdr.e_shstrndx = invsections[dso->ehdr.e_shstrndx]; + +#if 0 + for (plarch = __start_pl_arch; plarch < __stop_pl_arch; plarch++) + if (plarch->class == ehdr.e_ident[EI_CLASS] + && (plarch->machine == ehdr.e_machine + || plarch->alternate_machine[0] == ehdr.e_machine + || plarch->alternate_machine[1] == ehdr.e_machine + || plarch->alternate_machine[2] == ehdr.e_machine)) + break; + + if (plarch == __stop_pl_arch || ehdr.e_machine == EM_NONE) + { + error (0, 0, "\"%s\"'s architecture is not supported", name); + goto error_out; + } + + dso->arch = plarch; +#else + dso->arch = NULL; +#endif + + dso->base = ~(GElf_Addr) 0; + dso->align = 0; + dso->end = 0; + for (i = 0; i < dso->ehdr.e_phnum; i++) + if (dso->phdr[i].p_type == PT_LOAD) + { + GElf_Addr base, end; + + if (dso->phdr[i].p_align > dso->align) + dso->align = dso->phdr[i].p_align; + base = dso->phdr[i].p_vaddr & ~(dso->phdr[i].p_align - 1); + end = dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz; + if (base < dso->base) + dso->base = base; + if (end > dso->end) + dso->end = end; + } + + if (dso->base == ~(GElf_Addr) 0) + { + error (0, 0, "%s: cannot find loadable segment", name); + goto error_out; + } + + read_dynamic (dso); + + dso->filename = (const char *) strdup (name); + dso->soname = dso->filename; + if (dso->info[DT_STRTAB] && dso->info[DT_SONAME]) + { + const char *soname; + + soname = get_data (dso, dso->info[DT_STRTAB] + dso->info[DT_SONAME], + NULL); + if (soname && soname[0] != '\0') + dso->soname = (const char *) strdup (soname); + } + +#if 0 + if (dso->arch->machine == EM_ALPHA + || dso->arch->machine == EM_MIPS) + for (i = 1; i < ehdr.e_shnum; ++i) + { + if ((dso->shdr[i].sh_type == SHT_ALPHA_DEBUG + && dso->arch->machine == EM_ALPHA) + || (dso->shdr[i].sh_type == SHT_MIPS_DEBUG + && dso->arch->machine == EM_MIPS)) + { + const char *name + = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); + if (! strcmp (name, ".mdebug")) + dso->mdebug_orig_offset = dso->shdr[i].sh_offset; + break; + } + } +#endif + + return dso; + +error_out: + if (dso) + { + free (dso->move); + if (dso->soname != dso->filename) + free ((char *) dso->soname); + free ((char *) dso->filename); + free (dso); + } + if (elf) + elf_end (elf); + if (fd != -1) + close (fd); + return NULL; +} + +#if 0 + +static int +adjust_symtab_section_indices (DSO *dso, int n, int old_shnum, int *old_to_new) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[n]; + GElf_Sym sym; + int changed = 0, ndx, maxndx; + + while ((data = elf_getdata (scn, data)) != NULL) + { + maxndx = data->d_size / dso->shdr[n].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getsym (dso->elf, data, ndx, &sym); + if (sym.st_shndx > SHN_UNDEF && sym.st_shndx < SHN_LORESERVE) + { + if (sym.st_shndx >= old_shnum + || old_to_new[sym.st_shndx] == -1) + { + if (! sym.st_size && + sym.st_info == ELF32_ST_INFO (STB_LOCAL, STT_SECTION)) + { + sym.st_value = 0; + sym.st_shndx = SHN_UNDEF; + gelfx_update_sym (dso->elf, data, ndx, &sym); + changed = 1; + continue; + } + else + { + if (sym.st_shndx >= old_shnum) + { + error (0, 0, "%s: Symbol section index outside of section numbers", + dso->filename); + return 1; + } + error (0, 0, "%s: Section symbol points into has been removed", + dso->filename); + return 1; + } + } + if (old_to_new[sym.st_shndx] != sym.st_shndx) + { + changed = 1; + sym.st_shndx = old_to_new[sym.st_shndx]; + gelfx_update_sym (dso->elf, data, ndx, &sym); + } + } + } + } + + if (changed) + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + + return 0; +} + +static int +set_stt_section_values (DSO *dso, int n) +{ + Elf_Data *data; + Elf_Scn *scn = dso->scn[n]; + GElf_Sym sym; + int ndx, maxndx, sec; + char seen[dso->ehdr.e_shnum]; + + memset (seen, 0, dso->ehdr.e_shnum); + data = elf_getdata (scn, NULL); + assert (data != NULL); + assert (elf_getdata (scn, data) == NULL); + assert (data->d_off == 0); + + maxndx = data->d_size / dso->shdr[n].sh_entsize; + gelfx_getsym (dso->elf, data, 0, &sym); + if (sym.st_info != ELF32_ST_INFO (STB_LOCAL, STT_NOTYPE) + || sym.st_size != 0 || sym.st_other != 0 + || sym.st_value != 0 || sym.st_shndx != SHN_UNDEF + || sym.st_name != 0) + return 0; + + for (ndx = 1; ndx < maxndx; ++ndx) + { + gelfx_getsym (dso->elf, data, ndx, &sym); + if (sym.st_info == ELF32_ST_INFO (STB_LOCAL, STT_SECTION) + && sym.st_size == 0 && sym.st_other == 0 + && sym.st_name == 0) + { + if (sym.st_shndx > SHN_UNDEF && sym.st_shndx < SHN_LORESERVE) + { + seen[sym.st_shndx] = 1; + sym.st_value = dso->shdr[sym.st_shndx].sh_addr; + gelfx_update_sym (dso->elf, data, ndx, &sym); + } + } + else + break; + } + + for (ndx = 1, sec = 1; ndx < maxndx; ++ndx) + { + gelfx_getsym (dso->elf, data, ndx, &sym); + if (sym.st_info == ELF32_ST_INFO (STB_LOCAL, STT_SECTION) + && sym.st_size == 0 && sym.st_other == 0 + && sym.st_name == 0) + { + if (sym.st_shndx == SHN_UNDEF) + { + while (sec < dso->ehdr.e_shnum && seen[sec]) + ++sec; + + if (sec >= dso->ehdr.e_shnum) + sym.st_value = 0; + else + sym.st_value = dso->shdr[sec].sh_addr; + sym.st_shndx = sec++; + gelfx_update_sym (dso->elf, data, ndx, &sym); + } + } + else + break; + } + + return 0; +} + +struct section_move * +init_section_move (DSO *dso) +{ + struct section_move *move; + int i; + + move = malloc (sizeof (struct section_move) + + (dso->ehdr.e_shnum * 2 + 20) * sizeof (int)); + if (move == NULL) + { + error (0, ENOMEM, "%s: Could not move sections", dso->filename); + return move; + } + move->old_shnum = dso->ehdr.e_shnum; + move->new_shnum = dso->ehdr.e_shnum; + move->old_to_new = (int *)(move + 1); + move->new_to_old = move->old_to_new + move->new_shnum; + for (i = 0; i < move->new_shnum; i++) + { + move->old_to_new[i] = i; + move->new_to_old[i] = i; + } + return move; +} + +void +add_section (struct section_move *move, int sec) +{ + int i; + + assert (move->new_shnum < move->old_shnum + 20); + assert (sec <= move->new_shnum); + + memmove (move->new_to_old + sec + 1, move->new_to_old + sec, + (move->new_shnum - sec) * sizeof (int)); + ++move->new_shnum; + move->new_to_old[sec] = -1; + for (i = 1; i < move->old_shnum; i++) + if (move->old_to_new[i] >= sec) + ++move->old_to_new[i]; +} + +void +remove_section (struct section_move *move, int sec) +{ + int i; + + assert (sec < move->new_shnum); + + memmove (move->new_to_old + sec, move->new_to_old + sec + 1, + (move->new_shnum - sec - 1) * sizeof (int)); + --move->new_shnum; + for (i = 1; i < move->old_shnum; i++) + if (move->old_to_new[i] == sec) + move->old_to_new[i] = -1; + else if (move->old_to_new[i] > sec) + --move->old_to_new[i]; +} + +int +reopen_dso (DSO *dso, struct section_move *move, const char *temp_base) +{ + char filename[strlen (temp_base ? temp_base : dso->filename) + + sizeof ("/dev/shm/.#prelink#.XXXXXX")]; + int adddel = 0; + int free_move = 0; + Elf *elf = NULL; + GElf_Ehdr ehdr; + char *e_ident; + int fd, i, j; + + if (move == NULL) + { + move = init_section_move (dso); + if (move == NULL) + return 1; + free_move = 1; + } + else + assert (dso->ehdr.e_shnum == move->old_shnum); + + if (temp_base == NULL) + temp_base = dso->filename; + sprintf (filename, "%s.#prelink#.XXXXXX", temp_base); + + fd = wrap_mkstemp (filename); + if (fd == -1) + { + strcpy (filename, "/tmp/#prelink#.XXXXXX"); + fd = wrap_mkstemp (filename); + if (fd == -1) + { + strcpy (filename, "/dev/shm/#prelink#.XXXXXX"); + fd = wrap_mkstemp (filename); + } + if (fd == -1) + { + error (0, errno, "Could not create temporary file %s", filename); + goto error_out; + } + } + + elf = elf_begin (fd, ELF_C_WRITE, NULL); + if (elf == NULL) + { + error (0, 0, "cannot open ELF file: %s (elf_begin failed, dso_readonly)", elf_errmsg (-1)); + goto error_out; + + } + + /* Some gelf_newehdr implementations don't return the resulting + ElfNN_Ehdr, so we have to do it the hard way instead of: + e_ident = (char *) gelf_newehdr (elf, gelf_getclass (dso->elf)); */ + switch (gelf_getclass (dso->elf)) + { + case ELFCLASS32: + e_ident = (char *) elf32_newehdr (elf); + break; + case ELFCLASS64: + e_ident = (char *) elf64_newehdr (elf); + break; + default: + e_ident = NULL; + break; + } + + if (e_ident == NULL + /* This is here just for the gelfx wrapper, so that gelf_update_ehdr + already has the correct ELF class. */ + || memcpy (e_ident, dso->ehdr.e_ident, EI_NIDENT) == NULL + || gelf_update_ehdr (elf, &dso->ehdr) == 0 + || gelf_newphdr (elf, dso->ehdr.e_phnum) == 0) + { + error (0, 0, "Could not create new ELF headers"); + goto error_out; + } + ehdr = dso->ehdr; + elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT | ELF_F_PERMISSIVE); + for (i = 0; i < ehdr.e_phnum; ++i) + gelf_update_phdr (elf, i, dso->phdr + i); + + for (i = 1; i < move->new_shnum; ++i) + { + Elf_Scn *scn; + Elf_Data data, *data1, *data2; + + if (move->new_to_old[i] == -1) + { + scn = elf_newscn (elf); + elf_newdata (scn); + } + else + { + j = move->new_to_old[i]; + scn = elf_newscn (elf); + gelfx_update_shdr (elf, scn, &dso->shdr[j]); + if (dso->shdr[j].sh_type == SHT_NOBITS) + { + data1 = elf_getdata (dso->scn[j], NULL); + data2 = elf_newdata (scn); + memcpy (data2, data1, sizeof (*data1)); + } + else + { + data.d_type = ELF_T_NUM; + data1 = NULL; + while ((data1 = elf_getdata (dso->scn[j], data1)) + != NULL) + { + if (data.d_type == ELF_T_NUM) + data = *data1; + else if (data.d_type != data1->d_type + || data.d_version != data1->d_version) + abort (); + else + { + if (data1->d_off < data.d_off) + { + data.d_size += data.d_off - data1->d_off; + data.d_off = data1->d_off; + } + if (data1->d_off + data1->d_size + > data.d_off + data.d_size) + data.d_size = data1->d_off + data1->d_size + - data.d_off; + if (data1->d_align > data.d_align) + data.d_align = data1->d_align; + } + } + if (data.d_type == ELF_T_NUM) + { + assert (dso->shdr[j].sh_size == 0); + continue; + } + if (data.d_size != 0) + { + data.d_buf = calloc (1, data.d_size); + if (data.d_buf == NULL) + { + error (0, ENOMEM, "%s: Could not copy section", + dso->filename); + goto error_out; + } + } + else + data.d_buf = NULL; + data1 = NULL; + while ((data1 = elf_getdata (dso->scn[j], data1)) + != NULL) + memcpy (data.d_buf + data1->d_off - data.d_off, data1->d_buf, + data1->d_size); + data2 = elf_newdata (scn); + memcpy (data2, &data, sizeof (data)); + } + } + } + + ehdr.e_shnum = move->new_shnum; + dso->temp_filename = strdup (filename); + if (dso->temp_filename == NULL) + { + error (0, ENOMEM, "%s: Could not save temporary filename", dso->filename); + goto error_out; + } + dso->elfro = dso->elf; + dso->elf = elf; + dso->fdro = dso->fd; + dso->fd = fd; + dso->ehdr = ehdr; + dso->lastscn = 0; + elf = NULL; + fd = -1; + for (i = 1; i < move->old_shnum; i++) + if (move->old_to_new[i] != i) + { + adddel = 1; + break; + } + if (! adddel) + for (i = 1; i < move->new_shnum; i++) + if (move->new_to_old[i] != i) + { + adddel = 1; + break; + } + + for (i = 1; i < move->new_shnum; i++) + { + dso->scn[i] = elf_getscn (dso->elf, i); + gelfx_getshdr (dso->elf, dso->scn[i], dso->shdr + i); + if (move->new_to_old[i] == -1) + continue; + if (dso->move + && (dso->shdr[i].sh_type == SHT_SYMTAB + || dso->shdr[i].sh_type == SHT_DYNSYM)) + { + if (adjust_symtab_section_indices (dso, i, dso->move->old_shnum, + dso->move->old_to_new)) + goto error_out; + } + if (adddel) + { + if (dso->shdr[i].sh_link) + { + if (dso->shdr[i].sh_link >= move->old_shnum) + { + error (0, 0, "%s: bogus sh_link value %d", dso->filename, + dso->shdr[i].sh_link); + goto error_out; + } + if (move->old_to_new[dso->shdr[i].sh_link] == -1) + { + error (0, 0, "Section sh_link points to has been removed"); + goto error_out; + } + dso->shdr[i].sh_link = move->old_to_new[dso->shdr[i].sh_link]; + } + /* Only some section types use sh_info for section index. */ + if (dso->shdr[i].sh_info + && (dso->shdr[i].sh_type == SHT_REL + || dso->shdr[i].sh_type == SHT_RELA + || (dso->shdr[i].sh_flags & SHF_INFO_LINK))) + { + if (dso->shdr[i].sh_info >= move->old_shnum) + { + error (0, 0, "%s: bogus sh_info value %d", dso->filename, + dso->shdr[i].sh_info); + goto error_out; + } + if (move->old_to_new[dso->shdr[i].sh_info] == -1) + { + error (0, 0, "Section sh_info points to has been removed"); + goto error_out; + } + dso->shdr[i].sh_info = move->old_to_new[dso->shdr[i].sh_info]; + } + if (dso->shdr[i].sh_type == SHT_SYMTAB + || dso->shdr[i].sh_type == SHT_DYNSYM) + { + if (adjust_symtab_section_indices (dso, i, move->old_shnum, + move->old_to_new)) + goto error_out; + } + } + } + + free (dso->move); + dso->move = NULL; + + dso->ehdr.e_shstrndx = move->old_to_new[dso->ehdr.e_shstrndx]; + gelf_update_ehdr (dso->elf, &dso->ehdr); + + read_dynamic (dso); + + /* If shoff does not point after last section, we need to adjust the sections + after it if we added or removed some sections. */ + if (move->old_shnum != move->new_shnum + && adjust_dso_nonalloc (dso, 0, dso->ehdr.e_shoff + 1, + ((long) move->new_shnum - (long) move->old_shnum) + * gelf_fsize (dso->elf, ELF_T_SHDR, 1, + EV_CURRENT))) + goto error_out; + + if (free_move) + free (move); + return 0; + +error_out: + if (free_move) + free (move); + if (elf) + elf_end (elf); + if (fd != -1) + { + wrap_unlink (filename); + close (fd); + } + return 1; +} + +static int +adjust_symtab (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[n]; + GElf_Sym sym; + int ndx, maxndx; + + while ((data = elf_getdata (scn, data)) != NULL) + { + maxndx = data->d_size / dso->shdr[n].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getsym (dso->elf, data, ndx, &sym); + if (sym.st_shndx == SHN_ABS && sym.st_value != 0 + && (GELF_ST_TYPE (sym.st_info) <= STT_FUNC + || (dso->ehdr.e_machine == EM_ARM + && GELF_ST_TYPE (sym.st_info) == STT_ARM_TFUNC))) + { + /* This is problematic. How do we find out if + we should relocate this? Assume we should. */ + if (sym.st_value >= start) + { + sym.st_value += adjust; + sym.st_value &= dso->mask; + gelfx_update_sym (dso->elf, data, ndx, &sym); + } + continue; + } + + if (sym.st_shndx <= SHN_UNDEF + || sym.st_shndx >= dso->ehdr.e_shnum + || ELF32_ST_TYPE (sym.st_info) == STT_TLS) + continue; + + if (! RELOCATE_SCN (dso->shdr[sym.st_shndx].sh_flags)) + continue; + + if (sym.st_value >= start) + { + sym.st_value += adjust; + gelfx_update_sym (dso->elf, data, ndx, &sym); + } + } + } + + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + return 0; +} + +#endif + +int +dso_is_rdwr (DSO *dso) +{ + return dso->elfro != NULL; +} + +#if 0 + +GElf_Addr +adjust_old_to_new (DSO *dso, GElf_Addr addr) +{ + int i; + + if (dso->adjust == NULL) + return addr; /* Fast path. */ + + for (i = 0; i < dso->nadjust; i++) + if (addr >= dso->adjust[i].start) + { + addr += dso->adjust[i].adjust; + assert (dso->ehdr.e_ident[EI_CLASS] != ELFCLASS32 + || addr == (Elf32_Addr) addr); + return addr; + } + + return addr; +} + +GElf_Addr +adjust_new_to_old (DSO *dso, GElf_Addr addr) +{ + int i; + + if (dso->adjust == NULL) + return addr; /* Fast path. */ + + for (i = 0; i < dso->nadjust; i++) + if (addr >= dso->adjust[i].start + dso->adjust[i].adjust) + { + addr -= dso->adjust[i].adjust; + assert (dso->ehdr.e_ident[EI_CLASS] != ELFCLASS32 + || addr == (Elf32_Addr) addr); + return addr; + } + + return addr; +} + +static int +adjust_dynamic (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[n]; + GElf_Dyn dyn; + int ndx, maxndx; + + while ((data = elf_getdata (scn, data)) != NULL) + { + maxndx = data->d_size / dso->shdr[n].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getdyn (dso->elf, data, ndx, &dyn); + if (dso->arch->adjust_dyn (dso, n, &dyn, start, adjust) == 0) + switch (dyn.d_tag) + { + case DT_REL: + case DT_RELA: + /* On some arches DT_REL* may be 0 indicating no relocations + (if DT_REL*SZ is also 0). Don't adjust it in that case. */ + if (dyn.d_un.d_ptr && dyn.d_un.d_ptr >= start) + { + dyn.d_un.d_ptr += adjust; + gelfx_update_dyn (dso->elf, data, ndx, &dyn); + } + break; + default: + if (dyn.d_tag < DT_ADDRRNGLO || dyn.d_tag > DT_ADDRRNGHI) + break; + /* FALLTHROUGH */ + case DT_INIT: + case DT_FINI: + case DT_HASH: + case DT_STRTAB: + case DT_SYMTAB: + case DT_JMPREL: + case DT_INIT_ARRAY: + case DT_FINI_ARRAY: + case DT_PREINIT_ARRAY: + case DT_VERDEF: + case DT_VERNEED: + case DT_VERSYM: + case DT_PLTGOT: + if (dyn.d_un.d_ptr >= start) + { + dyn.d_un.d_ptr += adjust; + gelfx_update_dyn (dso->elf, data, ndx, &dyn); + } + break; + } + else + gelfx_update_dyn (dso->elf, data, ndx, &dyn); + } + } + + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + + /* Update the cached dynamic info as well. */ + read_dynamic (dso); + return 0; +} + +#endif + +int +addr_to_sec (DSO *dso, GElf_Addr addr) +{ + GElf_Shdr *shdr; + int i; + + shdr = &dso->shdr[dso->lastscn]; + for (i = -1; i < dso->ehdr.e_shnum; shdr = &dso->shdr[++i]) + if (RELOCATE_SCN (shdr->sh_flags) + && shdr->sh_addr <= addr && shdr->sh_addr + shdr->sh_size > addr + && (shdr->sh_type != SHT_NOBITS || (shdr->sh_flags & SHF_TLS) == 0)) + { + if (i != -1) + dso->lastscn = i; + return dso->lastscn; + } + + return -1; +} + +#if 0 + +static int +adjust_rel (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[n]; + GElf_Rel rel; + int sec, ndx, maxndx; + + while ((data = elf_getdata (scn, data)) != NULL) + { + maxndx = data->d_size / dso->shdr[n].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getrel (dso->elf, data, ndx, &rel); + sec = addr_to_sec (dso, rel.r_offset); + if (sec == -1) + continue; + + dso->arch->adjust_rel (dso, &rel, start, adjust); + addr_adjust (rel.r_offset, start, adjust); + gelfx_update_rel (dso->elf, data, ndx, &rel); + } + } + + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + return 0; +} + +static int +adjust_rela (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = dso->scn[n]; + GElf_Rela rela; + int sec, ndx, maxndx; + + while ((data = elf_getdata (scn, data)) != NULL) + { + maxndx = data->d_size / dso->shdr[n].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getrela (dso->elf, data, ndx, &rela); + sec = addr_to_sec (dso, rela.r_offset); + if (sec == -1) + continue; + + dso->arch->adjust_rela (dso, &rela, start, adjust); + addr_adjust (rela.r_offset, start, adjust); + gelfx_update_rela (dso->elf, data, ndx, &rela); + } + } + + elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY); + return 0; +} + +int +adjust_nonalloc (DSO *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int first, + GElf_Addr start, GElf_Addr adjust) +{ + int i; + + for (i = 1; i < ehdr->e_shnum; i++) + { + if (RELOCATE_SCN (shdr[i].sh_flags) || shdr[i].sh_type == SHT_NULL) + continue; + + if ((shdr[i].sh_offset > start + || (shdr[i].sh_offset == start && i >= first)) + && (adjust & (shdr[i].sh_addralign - 1))) + adjust = (adjust + shdr[i].sh_addralign - 1) + & ~(shdr[i].sh_addralign - 1); + } + + if (ehdr->e_shoff >= start) + { + GElf_Addr shdralign = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT); + + if (adjust & (shdralign - 1)) + adjust = (adjust + shdralign - 1) & ~(shdralign - 1); + ehdr->e_shoff += adjust; + } + + for (i = 1; i < ehdr->e_shnum; i++) + { + if (RELOCATE_SCN (shdr[i].sh_flags) || shdr[i].sh_type == SHT_NULL) + continue; + + if (shdr[i].sh_offset > start + || (shdr[i].sh_offset == start && i >= first)) + shdr[i].sh_offset += adjust; + } + return 0; +} + +int +adjust_dso_nonalloc (DSO *dso, int first, GElf_Addr start, GElf_Addr adjust) +{ + return adjust_nonalloc (dso, &dso->ehdr, dso->shdr, first, start, adjust); +} + +/* Add ADJUST to all addresses above START. */ +int +adjust_dso (DSO *dso, GElf_Addr start, GElf_Addr adjust) +{ + int i; + + if (dso->ehdr.e_entry >= start) + { + dso->ehdr.e_entry += adjust; + gelf_update_ehdr (dso->elf, &dso->ehdr); + elf_flagehdr (dso->elf, ELF_C_SET, ELF_F_DIRTY); + } + + for (i = 0; i < dso->ehdr.e_phnum; i++) + { + /* Leave STACK segment alone, it has + p_vaddr == p_paddr == p_offset == p_filesz == p_memsz == 0. */ + if (dso->phdr[i].p_type == PT_GNU_STACK) + continue; + if (! start) + { + dso->phdr[i].p_vaddr += adjust; + dso->phdr[i].p_paddr += adjust; + } + else if (start <= dso->phdr[i].p_vaddr) + { + dso->phdr[i].p_vaddr += adjust; + dso->phdr[i].p_paddr += adjust; + dso->phdr[i].p_offset += adjust; + } + else if (start < dso->phdr[i].p_vaddr + dso->phdr[i].p_filesz) + { + dso->phdr[i].p_filesz += adjust; + dso->phdr[i].p_memsz += adjust; + } + else if (start < dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz) + dso->phdr[i].p_memsz += adjust; + else + continue; + if (dso->phdr[i].p_type == PT_LOAD + && (dso->phdr[i].p_vaddr - dso->phdr[i].p_offset) + % dso->phdr[i].p_align) + { + error (0, 0, "%s: PT_LOAD %08llx %08llx 0x%x would be not properly aligned", + dso->filename, (long long) dso->phdr[i].p_offset, + (long long) dso->phdr[i].p_vaddr, (int) dso->phdr[i].p_align); + return 1; + } + gelf_update_phdr (dso->elf, i, dso->phdr + i); + } + elf_flagphdr (dso->elf, ELF_C_SET, ELF_F_DIRTY); + + for (i = 1; i < dso->ehdr.e_shnum; i++) + { + const char *name; + + if (dso->arch->adjust_section) + { + int ret = dso->arch->adjust_section (dso, i, start, adjust); + + if (ret == 1) + return 1; + else if (ret) + continue; + } + switch (dso->shdr[i].sh_type) + { + case SHT_PROGBITS: + name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); + if (strcmp (name, ".stab") == 0 + && adjust_stabs (dso, i, start, adjust)) + return 1; + if (strcmp (name, ".debug_info") == 0 + && adjust_dwarf2 (dso, i, start, adjust)) + return 1; + break; + case SHT_HASH: + case SHT_GNU_HASH: + case SHT_NOBITS: + case SHT_STRTAB: + break; + case SHT_SYMTAB: + case SHT_DYNSYM: + if (adjust_symtab (dso, i, start, adjust)) + return 1; + break; + case SHT_DYNAMIC: + if (adjust_dynamic (dso, i, start, adjust)) + return 1; + break; + case SHT_REL: + /* Don't adjust reloc sections for debug info. */ + if (dso->shdr[i].sh_flags & SHF_ALLOC) + if (adjust_rel (dso, i, start, adjust)) + return 1; + break; + case SHT_RELA: + if (dso->shdr[i].sh_flags & SHF_ALLOC) + if (adjust_rela (dso, i, start, adjust)) + return 1; + break; + } + if ((dso->arch->machine == EM_ALPHA + && dso->shdr[i].sh_type == SHT_ALPHA_DEBUG) + || (dso->arch->machine == EM_MIPS + && dso->shdr[i].sh_type == SHT_MIPS_DEBUG)) + if (adjust_mdebug (dso, i, start, adjust)) + return 1; + } + + for (i = 0; i < dso->ehdr.e_shnum; i++) + { + if (RELOCATE_SCN (dso->shdr[i].sh_flags)) + { + if (dso->shdr[i].sh_addr >= start) + { + Elf_Scn *scn = dso->scn[i]; + + dso->shdr[i].sh_addr += adjust; + if (start) + dso->shdr[i].sh_offset += adjust; + gelfx_update_shdr (dso->elf, scn, dso->shdr + i); + elf_flagshdr (scn, ELF_C_SET, ELF_F_DIRTY); + } + } + } + + addr_adjust (dso->base, start, adjust); + addr_adjust (dso->end, start, adjust); + + if (start) + { + start = adjust_new_to_old (dso, start); + for (i = 0; i < dso->nadjust; i++) + if (start < dso->adjust[i].start) + dso->adjust[i].adjust += adjust; + else + break; + if (i < dso->nadjust && start == dso->adjust[i].start) + dso->adjust[i].adjust += adjust; + else + { + dso->adjust = + realloc (dso->adjust, (dso->nadjust + 1) * sizeof (*dso->adjust)); + if (dso->adjust == NULL) + { + error (0, ENOMEM, "Cannot record the list of adjustements being made"); + return 1; + } + memmove (dso->adjust + i + 1, dso->adjust + i, dso->nadjust - i); + dso->adjust[i].start = start; + dso->adjust[i].adjust = adjust; + ++dso->nadjust; + } + } + + return start ? adjust_dso_nonalloc (dso, 0, 0, adjust) : 0; +} + +int +recompute_nonalloc_offsets (DSO *dso) +{ + int i, first_nonalloc, sec_before_shoff = 0; + GElf_Addr last_offset = 0; + GElf_Addr shdralign = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT); + GElf_Addr shdrsize = gelf_fsize (dso->elf, ELF_T_SHDR, 1, EV_CURRENT) + * dso->ehdr.e_shnum; + + for (i = 1; i < dso->ehdr.e_shnum; ++i) + if (RELOCATE_SCN (dso->shdr[i].sh_flags)) + { + if (dso->shdr[i].sh_type == SHT_NOBITS) + last_offset = dso->shdr[i].sh_offset; + else + last_offset = dso->shdr[i].sh_offset + dso->shdr[i].sh_size; + } + else + break; + + first_nonalloc = i; + if (dso->ehdr.e_shoff < dso->shdr[i].sh_offset) + { + dso->ehdr.e_shoff = (last_offset + shdralign - 1) & ~(shdralign - 1); + last_offset = dso->ehdr.e_shoff + shdrsize; + } + else + for (; i < dso->ehdr.e_shnum; ++i) + if (dso->shdr[i].sh_offset < dso->ehdr.e_shoff + && (i == dso->ehdr.e_shnum - 1 + || dso->shdr[i + 1].sh_offset > dso->ehdr.e_shoff)) + { + sec_before_shoff = i; + break; + } + + for (i = first_nonalloc; i < dso->ehdr.e_shnum; ++i) + { + assert (!RELOCATE_SCN (dso->shdr[i].sh_flags)); + assert (dso->shdr[i].sh_type != SHT_NOBITS); + dso->shdr[i].sh_offset = (last_offset + dso->shdr[i].sh_addralign - 1) + & ~(dso->shdr[i].sh_addralign - 1); + last_offset = dso->shdr[i].sh_offset + dso->shdr[i].sh_size; + if (i == sec_before_shoff) + { + dso->ehdr.e_shoff = (last_offset + shdralign - 1) & ~(shdralign - 1); + last_offset = dso->ehdr.e_shoff + shdrsize; + } + } + + return 0; +} + +int +strtabfind (DSO *dso, int strndx, const char *name) +{ + Elf_Scn *scn; + Elf_Data *data; + const char *p, *q, *r; + size_t len = strlen (name); + + if (dso->shdr[strndx].sh_type != SHT_STRTAB) + return 0; + + scn = dso->scn[strndx]; + data = elf_getdata (scn, NULL); + assert (elf_getdata (scn, data) == NULL); + assert (data->d_off == 0); + assert (data->d_size == dso->shdr[strndx].sh_size); + q = data->d_buf + data->d_size; + for (p = data->d_buf; p < q; p = r + 1) + { + r = strchr (p, '\0'); + if (r - p >= len && memcmp (r - len, name, len) == 0) + return (r - (const char *) data->d_buf) - len; + } + + return 0; +} + +int +shstrtabadd (DSO *dso, const char *name) +{ + Elf_Scn *scn; + Elf_Data *data; + GElf_Addr adjust; + const char *p, *q, *r; + size_t len = strlen (name), align; + int ret; + + scn = dso->scn[dso->ehdr.e_shstrndx]; + data = elf_getdata (scn, NULL); + assert (elf_getdata (scn, data) == NULL); + assert (data->d_off == 0); + assert (data->d_size == dso->shdr[dso->ehdr.e_shstrndx].sh_size); + q = data->d_buf + data->d_size; + for (p = data->d_buf; p < q; p = r + 1) + { + r = strchr (p, '\0'); + if (r - p >= len && memcmp (r - len, name, len) == 0) + return (r - (const char *) data->d_buf) - len; + } + + data->d_buf = realloc (data->d_buf, data->d_size + len + 1); + if (data->d_buf == NULL) + { + error (0, ENOMEM, "Cannot add new section name %s", name); + return 0; + } + + memcpy (data->d_buf + data->d_size, name, len + 1); + ret = data->d_size; + data->d_size += len + 1; + align = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT); + adjust = (len + 1 + align - 1) & ~(align - 1); + if (adjust_dso_nonalloc (dso, 0, + dso->shdr[dso->ehdr.e_shstrndx].sh_offset + + dso->shdr[dso->ehdr.e_shstrndx].sh_size, + adjust)) + return 0; + dso->shdr[dso->ehdr.e_shstrndx].sh_size += len + 1; + return ret; +} + +int +relocate_dso (DSO *dso, GElf_Addr base) +{ + /* Check if it is already relocated. */ + if (dso->base == base) + return 0; + + if (! dso_is_rdwr (dso)) + { + if (reopen_dso (dso, NULL, NULL)) + return 1; + } + + return adjust_dso (dso, 0, base - dso->base); +} + +#endif + +static int +close_dso_1 (DSO *dso) +{ + if (dso_is_rdwr (dso)) + { + int i; + + for (i = 1; i < dso->ehdr.e_shnum; ++i) + { + Elf_Scn *scn = dso->scn[i]; + Elf_Data *data = NULL; + + while ((data = elf_getdata (scn, data)) != NULL) + { + free (data->d_buf); + data->d_buf = NULL; + } + } + } + + elf_end (dso->elf); + close (dso->fd); + if (dso->elfro) + { + elf_end (dso->elfro); + close (dso->fdro); + } + if (dso->filename != dso->soname) + free ((char *) dso->soname); + free ((char *) dso->filename); + free ((char *) dso->temp_filename); + free (dso->move); + free (dso->adjust); + free (dso->undo.d_buf); + free (dso); + return 0; +} + +int +close_dso (DSO *dso) +{ + int rdwr = dso_is_rdwr (dso); + + if (rdwr && dso->temp_filename != NULL) + wrap_unlink (dso->temp_filename); + close_dso_1 (dso); + return 0; +} + +#if 0 + +int +prepare_write_dso (DSO *dso) +{ + int i; + + if (check_dso (dso) + || (dso->mdebug_orig_offset && finalize_mdebug (dso))) + return 1; + + gelf_update_ehdr (dso->elf, &dso->ehdr); + for (i = 0; i < dso->ehdr.e_phnum; ++i) + gelf_update_phdr (dso->elf, i, dso->phdr + i); + for (i = 0; i < dso->ehdr.e_shnum; ++i) + { + gelfx_update_shdr (dso->elf, dso->scn[i], dso->shdr + i); + if (dso->shdr[i].sh_type == SHT_SYMTAB + || dso->shdr[i].sh_type == SHT_DYNSYM) + set_stt_section_values (dso, i); + } + return 0; +} + +int +write_dso (DSO *dso) +{ + if (prepare_write_dso (dso)) + return 1; + + if (! dso->permissive && ELF_F_PERMISSIVE) + elf_flagelf (dso->elf, ELF_C_CLR, ELF_F_PERMISSIVE); + + if (elf_update (dso->elf, ELF_C_WRITE) == -1) + return 2; + return 0; +} + +int +set_security_context (DSO *dso, const char *temp_name, const char *name) +{ +#ifdef USE_SELINUX + static int selinux_enabled = -1; + if (selinux_enabled == -1) + selinux_enabled = is_selinux_enabled (); + if (selinux_enabled > 0) + { + security_context_t scontext; + if (getfilecon (name, &scontext) < 0) + { + /* If the filesystem doesn't support extended attributes, + the original had no special security context and the + target cannot have one either. */ + if (errno == EOPNOTSUPP) + return 0; + + error (0, errno, "Could not get security context for %s", + name); + return 1; + } + if (setfilecon (temp_name, scontext) < 0) + { + error (0, errno, "Could not set security context for %s", + name); + freecon (scontext); + return 1; + } + freecon (scontext); + } +#endif + return 0; +} + +int +update_dso (DSO *dso, const char *orig_name) +{ + int rdwr = dso_is_rdwr (dso); + + if (rdwr) + { + char *name1, *name2; + struct utimbuf u; + struct stat64 st; + + switch (write_dso (dso)) + { + case 2: + error (0, 0, "Could not write %s: %s", dso->filename, + elf_errmsg (-1)); + /* FALLTHROUGH */ + case 1: + close_dso (dso); + return 1; + case 0: + break; + } + + name1 = strdupa (dso->filename); + name2 = strdupa (dso->temp_filename); + if (fstat64 (dso->fdro, &st) < 0) + { + error (0, errno, "Could not stat %s", dso->filename); + close_dso (dso); + return 1; + } + if (fchown (dso->fd, st.st_uid, st.st_gid) < 0 + || fchmod (dso->fd, st.st_mode & 07777) < 0) + { + error (0, errno, "Could not set %s owner or mode", dso->filename); + close_dso (dso); + return 1; + } + close_dso_1 (dso); + u.actime = time (NULL); + u.modtime = st.st_mtime; + wrap_utime (name2, &u); + + if (set_security_context (dso, name2, orig_name ? orig_name : name1)) + { + wrap_unlink (name2); + return 1; + } + + if (wrap_rename (name2, name1)) + { + wrap_unlink (name2); + error (0, errno, "Could not rename temporary to %s", name1); + return 1; + } + } + else + close_dso_1 (dso); + + return 0; +} + +#endif diff --git a/src/gather.c b/src/gather.c index 4bed182..9219fdf 100644 --- a/src/gather.c +++ b/src/gather.c @@ -61,8 +61,8 @@ gather_deps (DSO *dso, struct prelink_entry *ent) { int i, j, seen = 0; FILE *f = NULL; - const char *argv[5]; - const char *envp[4]; + const char *argv[6]; + const char *envp[5]; char *line = NULL, *p, *q = NULL; const char **depends = NULL; size_t ndepends = 0, ndepends_alloced = 0; @@ -143,11 +143,6 @@ gather_deps (DSO *dso, struct prelink_entry *ent) i = 0; argv[i++] = dl; - if (ld_library_path) - { - argv[i++] = "--library-path"; - argv[i++] = ld_library_path; - } if (strchr (ent->filename, '/') != NULL) ent_filename = ent->filename; else @@ -158,13 +153,55 @@ gather_deps (DSO *dso, struct prelink_entry *ent) memcpy (tp + 2, ent->filename, flen + 1); ent_filename = tp; } - argv[i++] = ent_filename; - argv[i] = NULL; - envp[0] = "LD_TRACE_LOADED_OBJECTS=1"; - envp[1] = "LD_TRACE_PRELINKING=1"; - envp[2] = "LD_WARN="; - envp[3] = NULL; - f = execve_open (dl, (char * const *)argv, (char * const *)envp); + + if (prelink_rtld == NULL) + { + i = 0; + argv[i++] = dl; + if (ld_library_path) + { + argv[i++] = "--library-path"; + argv[i++] = ld_library_path; + } + argv[i++] = ent_filename; + argv[i] = NULL; + envp[0] = "LD_TRACE_LOADED_OBJECTS=1"; + envp[1] = "LD_TRACE_PRELINKING=1"; + envp[2] = "LD_WARN="; + envp[3] = NULL; + f = execve_open (dl, (char * const *)argv, (char * const *)envp); + } + else + { + char *path; + i = 0; + argv[i++] = prelink_rtld; + if (ld_library_path) + { + argv[i++] = "--library-path"; + argv[i++] = ld_library_path; + } + argv[i++] = "--target-paths"; + argv[i++] = ent_filename; + argv[i] = NULL; + envp[0] = "RTLD_TRACE_PRELINKING=1"; + envp[1] = "LD_WARN="; + path = alloca (sizeof "PATH=" + strlen (getenv ("PATH"))); + sprintf (path, "PATH=%s", getenv ("PATH")); + envp[2] = path; + + if (sysroot) + { + envp[3] = alloca (sizeof "PRELINK_SYSROOT=" + strlen (sysroot)); + sprintf ((char *) envp[3], "PRELINK_SYSROOT=%s", sysroot); + envp[4] = NULL; + } + else + envp[3] = NULL; + + f = execve_open (prelink_rtld, (char * const *)argv, (char * const *)envp); + } + if (f == NULL) goto error_out; @@ -183,8 +220,8 @@ gather_deps (DSO *dso, struct prelink_entry *ent) q = strstr (p, " ("); if (q == NULL && strcmp (p, " => not found") == 0) { - error (0, 0, "%s: Could not find one of the dependencies", - ent->filename); + error (0, 0, "%s: Could not find one of the dependencies: %s", + ent->filename, line); goto error_out; } } @@ -562,7 +562,7 @@ prelink_get_relocations (struct prelink_info *info) { FILE *f; DSO *dso = info->dso; - const char *argv[5]; + const char *argv[6]; const char *envp[4]; int i, ret, status; char *p; @@ -591,13 +591,6 @@ prelink_get_relocations (struct prelink_info *info) / info->symtab_entsize; info->symbols = calloc (sizeof (struct prelink_symbol), info->symbol_count); - i = 0; - argv[i++] = dl; - if (ld_library_path) - { - argv[i++] = "--library-path"; - argv[i++] = ld_library_path; - } if (strchr (info->ent->filename, '/') != NULL) ent_filename = info->ent->filename; else @@ -608,17 +601,56 @@ prelink_get_relocations (struct prelink_info *info) memcpy (p + 2, info->ent->filename, flen + 1); ent_filename = p; } - argv[i++] = ent_filename; - argv[i] = NULL; - envp[0] = "LD_TRACE_LOADED_OBJECTS=1"; - envp[1] = "LD_BIND_NOW=1"; - p = alloca (sizeof "LD_TRACE_PRELINKING=" + strlen (info->ent->filename)); - strcpy (stpcpy (p, "LD_TRACE_PRELINKING="), info->ent->filename); - envp[2] = p; - envp[3] = NULL; - - ret = 2; - f = execve_open (dl, (char * const *)argv, (char * const *)envp); + if (prelink_rtld == NULL) + { + i = 0; + argv[i++] = dl; + if (ld_library_path) + { + argv[i++] = "--library-path"; + argv[i++] = ld_library_path; + } + argv[i++] = ent_filename; + argv[i] = NULL; + envp[0] = "LD_TRACE_LOADED_OBJECTS=1"; + envp[1] = "LD_BIND_NOW=1"; + p = alloca (sizeof "LD_TRACE_PRELINKING=" + strlen (info->ent->filename)); + strcpy (stpcpy (p, "LD_TRACE_PRELINKING="), info->ent->filename); + envp[2] = p; + envp[3] = NULL; + ret = 2; + f = execve_open (dl, (char * const *)argv, (char * const *)envp); + } + else + { + i = 0; + argv[i++] = prelink_rtld; + if (ld_library_path) + { + argv[i++] = "--library-path"; + argv[i++] = ld_library_path; + } + argv[i++] = "--target-paths"; + argv[i++] = ent_filename; + argv[i] = NULL; + p = alloca (sizeof "RTLD_TRACE_PRELINKING=" + strlen (info->ent->filename)); + strcpy (stpcpy (p, "RTLD_TRACE_PRELINKING="), info->ent->filename); + envp[0] = p; + p = alloca (sizeof "PATH=" + strlen (getenv ("PATH"))); + sprintf (p, "PATH=%s", getenv ("PATH")); + envp[1] = p; + envp[2] = NULL; + if (sysroot) + { + p = alloca (sizeof "PRELINK_SYSROOT=" + strlen (sysroot)); + sprintf (p, "PRELINK_SYSROOT=%s", sysroot); + envp[2] = p; + envp[3] = NULL; + } + ret = 2; + f = execve_open (prelink_rtld, (char * const *)argv, (char * const *)envp); + } + if (f == NULL) { error (0, errno, "%s: Could not trace symbol resolving", diff --git a/src/ld-do-lookup.h b/src/ld-do-lookup.h new file mode 100644 index 0000000..62a1280 --- /dev/null +++ b/src/ld-do-lookup.h @@ -0,0 +1,225 @@ +/* Look up a symbol in the loaded objects. + Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#if VERSIONED +# define FCT do_lookup_versioned +# define ARG const struct r_found_version *const version +#else +# define FCT do_lookup +# define ARG int flags +#endif + +/* Inner part of the lookup functions. We return a value > 0 if we + found the symbol, the value 0 if nothing is found and < 0 if + something bad happened. */ +static inline int +FCT (const char *undef_name, unsigned long int hash, const ElfW(Sym) *ref, + struct sym_val *result, struct r_scope_elem *scope, size_t i, ARG, + struct ldlibs_link_map *skip, int type_class, int machine) +{ + struct ldlibs_link_map **list = scope->r_list; + size_t n = scope->r_nlist; + struct ldlibs_link_map *map; + + do + { + const ElfW(Sym) *symtab; + const char *strtab; + const ElfW(Half) *verstab; + Elf_Symndx symidx; + const ElfW(Sym) *sym; +#if ! VERSIONED + int num_versions = 0; + const ElfW(Sym) *versioned_sym = NULL; +#endif + + map = list[i]; + + /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */ + if (skip != NULL && map == skip) + continue; + + /* Don't search the executable when resolving a copy reloc. */ + if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable) + continue; + + /* Print some debugging info if wanted. */ + if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0)) + INTUSE(_dl_debug_printf) ("symbol=%s; lookup in file=%s\n", + undef_name, (map->l_name[0] + ? map->l_name : rtld_progname)); + + symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]); + strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); + verstab = map->l_versyms; + + /* Search the appropriate hash bucket in this object's symbol table + for a definition for the same symbol name. */ + for (symidx = map->l_buckets[hash % map->l_nbuckets]; + symidx != STN_UNDEF; + symidx = map->l_chain[symidx]) + { + sym = &symtab[symidx]; + + assert (ELF_RTYPE_CLASS_PLT == 1); + if ((sym->st_value == 0 /* No value. */ +#ifdef USE_TLS + && ELFW(ST_TYPE) (sym->st_info) != STT_TLS +#endif + ) + || (type_class & (sym->st_shndx == SHN_UNDEF))) + continue; + + if (ELFW(ST_TYPE) (sym->st_info) > STT_FUNC + && (machine != EM_ARM + || ELFW(ST_TYPE) (sym->st_info) != STT_ARM_TFUNC) +#ifdef USE_TLS + && ELFW(ST_TYPE) (sym->st_info) != STT_TLS +#endif + ) + /* Ignore all but STT_NOTYPE, STT_OBJECT and STT_FUNC + entries (and STT_TLS if TLS is supported) since these + are no code/data definitions. */ + continue; + + if (sym != ref && strcmp (strtab + sym->st_name, undef_name)) + /* Not the symbol we are looking for. */ + continue; + +#if VERSIONED + if (__builtin_expect (verstab == NULL, 0)) + { + /* We need a versioned symbol but haven't found any. If + this is the object which is referenced in the verneed + entry it is a bug in the library since a symbol must + not simply disappear. + + It would also be a bug in the object since it means that + the list of required versions is incomplete and so the + tests in dl-version.c haven't found a problem.*/ + assert (version->filename == NULL + || ! _dl_soname_match_p (version->filename, map)); + + /* Otherwise we accept the symbol. */ + } + else + { + /* We can match the version information or use the + default one if it is not hidden. */ + ElfW(Half) ndx = verstab[symidx] & 0x7fff; + if ((map->l_versions[ndx].hash != version->hash + || strcmp (map->l_versions[ndx].name, version->name)) + && (version->hidden || map->l_versions[ndx].hash + || (verstab[symidx] & 0x8000))) + /* It's not the version we want. */ + continue; + } +#else + /* No specific version is selected. There are two ways we + can got here: + + - a binary which does not include versioning information + is loaded + + - dlsym() instead of dlvsym() is used to get a symbol which + might exist in more than one form + + If the library does not provide symbol version + information there is no problem at at: we simply use the + symbol if it is defined. + + These two lookups need to be handled differently if the + library defines versions. In the case of the old + unversioned application the oldest (default) version + should be used. In case of a dlsym() call the latest and + public interface should be returned. */ + if (verstab != NULL) + { + if ((verstab[symidx] & 0x7fff) + >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3)) + { + /* Don't accept hidden symbols. */ + if ((verstab[symidx] & 0x8000) == 0 && num_versions++ == 0) + /* No version so far. */ + versioned_sym = sym; + + continue; + } + } +#endif + + /* There cannot be another entry for this symbol so stop here. */ + goto found_it; + } + + /* If we have seen exactly one versioned symbol while we are + looking for an unversioned symbol and the version is not the + default version we still accept this symbol since there are + no possible ambiguities. */ +#if VERSIONED + sym = NULL; +#else + sym = num_versions == 1 ? versioned_sym : NULL; +#endif + + if (sym != NULL) + { + found_it: + switch (ELFW(ST_BIND) (sym->st_info)) + { + case STB_WEAK: + /* Weak definition. Use this value if we don't find another. */ + if (__builtin_expect (GL(dl_dynamic_weak), 0)) + { + if (! result->s) + { + result->s = sym; + result->m = map; + } + break; + } + /* FALLTHROUGH */ + case STB_GLOBAL: + /* Global definition. Just what we need. */ + result->s = sym; + result->m = map; + return 1; + default: + /* Local symbols are ignored. */ + break; + } + } + +#if VERSIONED + /* If this current map is the one mentioned in the verneed entry + and we have not found a weak entry, it is a bug. */ + if (symidx == STN_UNDEF && version->filename != NULL + && __builtin_expect (_dl_soname_match_p (version->filename, map), 0)) + return -1; +#endif + } + while (++i < n); + + /* We have not found anything until now. */ + return 0; +} + +#undef FCT +#undef ARG +#undef VERSIONED diff --git a/src/ld-libs.c b/src/ld-libs.c new file mode 100644 index 0000000..fa4a3d1 --- /dev/null +++ b/src/ld-libs.c @@ -0,0 +1,1345 @@ +/* Copyright (C) 2003 MontaVista Software, Inc. + Written by Daniel Jacobowitz <drow@mvista.com>, 2003 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, 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 of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include <config.h> +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "prelinktab.h" +#include "reloc.h" + +#include "ld-libs.h" + +#ifndef PT_TLS +#define PT_TLS 7 /* Thread-local storage segment */ +#endif + +#ifndef R_ARM_TLS_DTPMOD32 +#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ +#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ +#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ +#endif + +/* This function returns the same constants expected by glibc's + symbol lookup routines. This is slightly different from the + equivalent routines in prelink. It should return PLT for any + relocation where an undefined symbol in the application should + be ignored: typically, this means any jump slot or TLS relocations, + but not copy relocations. Don't return the prelinker's + RTYPE_CLASS_TLS. */ +int +reloc_type_class (int type, int machine) +{ + switch (machine) + { + case EM_386: + switch (type) + { + case R_386_COPY: return ELF_RTYPE_CLASS_COPY; + case R_386_JMP_SLOT: + case R_386_TLS_DTPMOD32: + case R_386_TLS_DTPOFF32: + case R_386_TLS_TPOFF32: + case R_386_TLS_TPOFF: + return ELF_RTYPE_CLASS_PLT; + default: return 0; + } + + case EM_X86_64: + switch (type) + { + case R_X86_64_COPY: return ELF_RTYPE_CLASS_COPY; + case R_X86_64_JUMP_SLOT: + case R_X86_64_DTPMOD64: + case R_X86_64_DTPOFF64: + case R_X86_64_TPOFF64: + case R_X86_64_DTPOFF32: + case R_X86_64_TPOFF32: + return ELF_RTYPE_CLASS_PLT; + default: return 0; + } + + case EM_ARM: + switch (type) + { + case R_ARM_COPY: return ELF_RTYPE_CLASS_COPY; + case R_ARM_JUMP_SLOT: + case R_ARM_TLS_DTPMOD32: + case R_ARM_TLS_DTPOFF32: + case R_ARM_TLS_TPOFF32: + return ELF_RTYPE_CLASS_PLT; + default: return 0; + } + + case EM_SH: + switch (type) + { + case R_SH_COPY: return ELF_RTYPE_CLASS_COPY; + case R_SH_JMP_SLOT: return ELF_RTYPE_CLASS_PLT; + default: return 0; + } + + case EM_PPC: + switch (type) + { + case R_PPC_COPY: return ELF_RTYPE_CLASS_COPY; + case R_PPC_JMP_SLOT: return ELF_RTYPE_CLASS_PLT; + default: + if (type >= R_PPC_DTPMOD32 && type <= R_PPC_DTPREL32) + return ELF_RTYPE_CLASS_PLT; + return 0; + } + + case EM_PPC64: + switch (type) + { + case R_PPC64_COPY: return ELF_RTYPE_CLASS_COPY; + case R_PPC64_ADDR24: return ELF_RTYPE_CLASS_PLT; + default: + if (type >= R_PPC64_DTPMOD64 && type <= R_PPC64_TPREL16_HIGHESTA) + return ELF_RTYPE_CLASS_PLT; + return 0; + } + + default: + printf ("Unknown architecture!\n"); + exit (1); + return 0; + } +} + +int +is_ldso_soname (const char *soname) +{ + if (! strcmp (soname, "ld-linux.so.2") + || ! strcmp (soname, "ld-linux.so.3") + || ! strcmp (soname, "ld.so.1") + || ! strcmp (soname, "ld-linux-ia64.so.2") + || ! strcmp (soname, "ld-linux-x86-64.so.2") + || ! strcmp (soname, "ld64.so.1")) + return 1; + return 0; +} + + +struct needed_list +{ + struct dso_list *ent; + struct needed_list *next; +}; + +struct dso_list +{ + DSO *dso; + struct ldlibs_link_map *map; + struct dso_list *next, *prev; + struct needed_list *needed, *needed_tail; + const char *name; + struct dso_list *loader; + const char *canon_filename; +}; + +static int dso_open_error = 0; + +static void +free_needed (struct needed_list *p) +{ + struct needed_list *old_p = p; + while (old_p) + { + old_p = p->next; + free (p); + p = old_p; + } +} + +static struct dso_list * +in_dso_list (struct dso_list *dso_list, const char *soname, const char *filename) +{ + while (dso_list != NULL) + { + if (dso_list->dso != NULL) + { + if (strcmp (dso_list->dso->soname, soname) == 0) + return dso_list; + } + + if (strcmp (dso_list->name, soname) == 0) + return dso_list; + + if (filename && dso_list->canon_filename + && strcmp (dso_list->canon_filename, filename) == 0) + return dso_list; + + dso_list = dso_list->next; + } + return NULL; +} + +static int +in_needed_list (struct needed_list *needed_list, const char *soname) +{ + while (needed_list != NULL) + { + if (needed_list->ent->dso != NULL + && strcmp (needed_list->ent->dso->soname, soname) == 0) + return 1; + needed_list = needed_list->next; + } + return 0; +} + + +/****/ + +struct search_path +{ + int maxlen, count, allocated; + char **dirs; +}; + +struct search_path ld_dirs, ld_library_search_path; + +void +add_dir (struct search_path *path, const char *dir, int dirlen) +{ + if (path->allocated == 0) + { + path->allocated = 5; + path->dirs = malloc (sizeof (char *) * 5); + } + else if (path->count == path->allocated) + { + path->allocated *= 2; + path->dirs = realloc (path->dirs, sizeof (char *) * path->allocated); + } + path->dirs[path->count] = malloc (dirlen + 1); + memcpy (path->dirs[path->count], dir, dirlen); + path->dirs[path->count++][dirlen] = 0; + + if (path->maxlen < dirlen) + path->maxlen = dirlen; +} + +void +free_path (struct search_path *path) +{ + if (path->allocated) + { + int i; + for (i = 0; i < path->count; i++) + free (path->dirs[i]); + free (path->dirs); + } +} + +void +load_ld_so_conf (int use_64bit) +{ + int fd; + FILE *conf; + char buf[1024]; + + memset (&ld_dirs, 0, sizeof (ld_dirs)); + + /* Only use the correct machine, to prevent mismatches if we + have both /lib/ld.so and /lib64/ld.so on x86-64. */ + if (use_64bit) + { + add_dir (&ld_dirs, "/lib64/tls", strlen ("/lib64/tls")); + add_dir (&ld_dirs, "/lib64", strlen ("/lib64")); + add_dir (&ld_dirs, "/usr/lib64/tls", strlen ("/usr/lib64/tls")); + add_dir (&ld_dirs, "/usr/lib64", strlen ("/usr/lib64")); + } + else + { + add_dir (&ld_dirs, "/lib/tls", strlen ("/lib/tls")); + add_dir (&ld_dirs, "/lib", strlen ("/lib")); + add_dir (&ld_dirs, "/usr/lib/tls", strlen ("/usr/lib/tls")); + add_dir (&ld_dirs, "/usr/lib", strlen ("/usr/lib")); + } + + fd = wrap_open ("/etc/ld.so.conf", O_RDONLY); + if (fd == -1) + return; + conf = fdopen (fd, "r"); + while (fgets (buf, 1024, conf) != NULL) + { + int len; + char *p; + + p = strchr (buf, '#'); + if (p) + *p = 0; + len = strlen (buf); + while (isspace (buf[len - 1])) + buf[--len] = 0; + + add_dir (&ld_dirs, buf, len); + } + fclose (conf); +} + +void +string_to_path (struct search_path *path, const char *string) +{ + const char *start, *end, *end_tmp; + + start = string; + while (1) { + end = start; + while (*end && *end != ':' && *end != ';') + end ++; + + /* Eliminate any trailing '/' characters, but be sure to leave a + leading slash if someeone wants / in their RPATH. */ + end_tmp = end; + while (end_tmp > start + 1 && end_tmp[-1] == '/') + end_tmp --; + + add_dir (path, start, end_tmp - start); + + if (*end == 0) + break; + + /* Skip the separator. */ + start = end + 1; + } +} + +char * +find_lib_in_path (struct search_path *path, const char *soname, + int elfclass) +{ + char *ret; + int i; + + ret = malloc (strlen (soname) + 2 + path->maxlen); + + for (i = 0; i < path->count; i++) + { + sprintf (ret, "%s/%s", path->dirs[i], soname); + if (wrap_access (ret, F_OK) == 0) + { + /* Skip 32-bit libraries when looking for 64-bit. */ + DSO *dso = open_dso (ret); + + if (dso == NULL) + continue; + + if (gelf_getclass (dso->elf) != elfclass) + { + close_dso (dso); + continue; + } + + close_dso (dso); + return ret; + } + } + + free (ret); + return NULL; +} + +char * +find_lib_by_soname (const char *soname, struct dso_list *loader, + int elfclass) +{ + char *ret; + + if (strchr (soname, '/')) + return strdup (soname); + + if (loader->dso->info[DT_RUNPATH] == 0) + { + /* Search DT_RPATH all the way up. */ + struct dso_list *loader_p = loader; + while (loader_p) + { + if (loader_p->dso->info[DT_RPATH]) + { + struct search_path r_path; + const char *rpath = get_data (loader_p->dso, + loader_p->dso->info[DT_STRTAB] + + loader_p->dso->info[DT_RPATH], + NULL); + memset (&r_path, 0, sizeof (r_path)); + string_to_path (&r_path, rpath); + ret = find_lib_in_path (&r_path, soname, elfclass); + free_path (&r_path); + if (ret) + return ret; + } + loader_p = loader_p->loader; + } + } + + ret = find_lib_in_path (&ld_library_search_path, soname, elfclass); + if (ret) + return ret; + + if (loader->dso->info[DT_RUNPATH]) + { + struct search_path r_path; + const char *rpath = get_data (loader->dso, + loader->dso->info[DT_STRTAB] + + loader->dso->info[DT_RUNPATH], + NULL); + memset (&r_path, 0, sizeof (r_path)); + string_to_path (&r_path, rpath); + ret = find_lib_in_path (&r_path, soname, elfclass); + free_path (&r_path); + if (ret) + return ret; + } + + ret = find_lib_in_path (&ld_dirs, soname, elfclass); + if (ret) + return ret; + + return NULL; +} + +static struct dso_list * +load_dsos (DSO *dso) +{ + struct dso_list *dso_list, *dso_list_tail, *cur_dso_ent, *new_dso_ent; + + dso_list = malloc (sizeof (struct dso_list)); + dso_list->dso = dso; + dso_list->next = NULL; + dso_list->prev = NULL; + dso_list->needed = NULL; + dso_list->name = dso->filename; + dso_list->loader = NULL; + dso_list->canon_filename = wrap_prelink_canonicalize (dso->filename, NULL); + + cur_dso_ent = dso_list_tail = dso_list; + + while (cur_dso_ent != NULL) + { + DSO *cur_dso, *new_dso; + Elf_Scn *scn; + Elf_Data *data; + GElf_Dyn dyn; + + cur_dso = cur_dso_ent->dso; + if (cur_dso == NULL) + { + cur_dso_ent = cur_dso_ent->next; + continue; + } + + scn = cur_dso->scn[cur_dso->dynamic]; + data = NULL; + while ((data = elf_getdata (scn, data)) != NULL) + { + int ndx, maxndx; + maxndx = data->d_size / cur_dso->shdr[cur_dso->dynamic].sh_entsize; + for (ndx = 0; ndx < maxndx; ++ndx) + { + gelfx_getdyn (cur_dso->elf, data, ndx, &dyn); + if (dyn.d_tag == DT_NULL) + break; + if (dyn.d_tag == DT_NEEDED) + { + char *new_name=NULL, *new_canon_name=NULL; + const char *soname = get_data (cur_dso, + cur_dso->info[DT_STRTAB] + + dyn.d_un.d_val, + NULL); + new_dso_ent = in_dso_list (dso_list, soname, NULL); + if (new_dso_ent == NULL) + { + new_name = find_lib_by_soname (soname, cur_dso_ent, + gelf_getclass (dso->elf)); + if (new_name == 0 || wrap_access (new_name, R_OK) < 0) + { + dso_open_error ++; + + new_dso_ent = malloc (sizeof (struct dso_list)); + dso_list_tail->next = new_dso_ent; + dso_list_tail->next->prev = dso_list_tail; + dso_list_tail = dso_list_tail->next; + dso_list_tail->next = NULL; + dso_list_tail->dso = NULL; + dso_list_tail->needed = NULL; + dso_list_tail->name = soname; + dso_list_tail->loader = NULL; + dso_list_tail->canon_filename = soname; + + continue; + } + + /* See if the filename we found has already been + opened (possibly under a different SONAME via + some symlink). */ + new_canon_name = wrap_prelink_canonicalize (new_name, NULL); + if (new_canon_name == NULL) + new_canon_name = strdup (new_name); + new_dso_ent = in_dso_list (dso_list, soname, new_canon_name); + } + else if (new_dso_ent->dso == NULL) + continue; + + if (new_dso_ent == NULL) + { + new_dso = open_dso (new_name); + free (new_name); + new_dso_ent = malloc (sizeof (struct dso_list)); + dso_list_tail->next = new_dso_ent; + dso_list_tail->next->prev = dso_list_tail; + dso_list_tail = dso_list_tail->next; + dso_list_tail->next = NULL; + dso_list_tail->dso = new_dso; + dso_list_tail->needed = NULL; + dso_list_tail->loader = cur_dso_ent; + dso_list_tail->canon_filename = new_canon_name; + + if (is_ldso_soname (new_dso->soname)) + dso_list_tail->name = new_dso->filename; + else if (strcmp (new_dso->soname, new_dso->filename) == 0) + /* new_dso->soname might be a full path if the library + had no SONAME. Use the original SONAME instead. */ + dso_list_tail->name = soname; + else + /* Use the new SONAME if possible, in case some library + links to this one using an incorrect SONAME. */ + dso_list_tail->name = new_dso->soname; + } + + if (!cur_dso_ent->needed) + { + cur_dso_ent->needed = malloc (sizeof (struct needed_list)); + cur_dso_ent->needed_tail = cur_dso_ent->needed; + cur_dso_ent->needed_tail->ent = new_dso_ent; + cur_dso_ent->needed_tail->next = NULL; + } + else if (!in_needed_list (cur_dso_ent->needed, soname)) + { + cur_dso_ent->needed_tail->next = malloc (sizeof (struct needed_list)); + cur_dso_ent->needed_tail = cur_dso_ent->needed_tail->next; + cur_dso_ent->needed_tail->ent = new_dso_ent; + cur_dso_ent->needed_tail->next = NULL; + } + + continue; + } + if (dyn.d_tag == DT_FILTER || dyn.d_tag == DT_AUXILIARY) + { + // big fat warning; + } + } + } + cur_dso_ent = cur_dso_ent->next; + } + return dso_list; +} + +static void +get_version_info (DSO *dso, struct ldlibs_link_map *map) +{ + int i; + Elf_Data *data; + int ndx_high; + const char *strtab = map->l_info[DT_STRTAB]; + + /* Fortunately, 32-bit and 64-bit ELF use the same Verneed and Verdef + structures, so this function will work for either. */ + + Elf64_Verneed *verneed; + Elf64_Verdef *verdef; + + map->l_versyms = NULL; + + if (dso->info_set_mask & (1ULL << DT_VERNEED_BIT)) + { + i = addr_to_sec (dso, dso->info_DT_VERNEED); + data = elf_getdata (dso->scn[i], NULL); + verneed = data->d_buf; + } + else + verneed = NULL; + + if (dso->info_set_mask & (1ULL << DT_VERDEF_BIT)) + { + i = addr_to_sec (dso, dso->info_DT_VERDEF); + data = elf_getdata (dso->scn[i], NULL); + verdef = data->d_buf; + } + else + verdef = NULL; + + ndx_high = 0; + if (verneed) + { + Elf64_Verneed *ent = verneed; + Elf64_Vernaux *aux; + while (1) + { + aux = (Elf64_Vernaux *) ((char *) ent + ent->vn_aux); + while (1) + { + if ((unsigned int) (aux->vna_other & 0x7fff) > ndx_high) + ndx_high = aux->vna_other & 0x7fff; + + if (aux->vna_next == 0) + break; + aux = (Elf64_Vernaux *) ((char *) aux + aux->vna_next); + } + + if (ent->vn_next == 0) + break; + ent = (Elf64_Verneed *) ((char *) ent + ent->vn_next); + } + } + + if (verdef) + { + Elf64_Verdef *ent = verdef; + while (1) + { + if ((unsigned int) (ent->vd_ndx & 0x7fff) > ndx_high) + ndx_high = ent->vd_ndx & 0x7fff; + + if (ent->vd_next == 0) + break; + ent = (Elf64_Verdef *) ((char *) ent + ent->vd_next); + } + } + + if (ndx_high) + { + map->l_versions = (struct r_found_version *) + calloc (ndx_high + 1, sizeof (struct r_found_version)); + map->l_nversions = ndx_high + 1; + + i = addr_to_sec (dso, dso->info_DT_VERSYM); + data = elf_getdata (dso->scn[i], NULL); + map->l_versyms = data->d_buf; + + if (verneed) + { + Elf64_Verneed *ent = verneed; + + while (1) + { + Elf64_Vernaux *aux; + aux = (Elf64_Vernaux *) ((char *) ent + ent->vn_aux); + while (1) + { + Elf64_Half ndx = aux->vna_other & 0x7fff; + map->l_versions[ndx].hash = aux->vna_hash; + map->l_versions[ndx].hidden = aux->vna_other & 0x8000; + map->l_versions[ndx].name = &strtab[aux->vna_name]; + map->l_versions[ndx].filename = &strtab[ent->vn_file]; + + if (aux->vna_next == 0) + break; + aux = (Elf64_Vernaux *) ((char *) aux + aux->vna_next); + } + + if (ent->vn_next == 0) + break; + ent = (Elf64_Verneed *) ((char *) ent + ent->vn_next); + } + } + + if (verdef) + { + Elf64_Verdef *ent = verdef; + Elf64_Verdaux *aux; + while (1) + { + aux = (Elf64_Verdaux *) ((char *) ent + ent->vd_aux); + + if ((ent->vd_flags & VER_FLG_BASE) == 0) + { + /* The name of the base version should not be + available for matching a versioned symbol. */ + Elf64_Half ndx = ent->vd_ndx & 0x7fff; + map->l_versions[ndx].hash = ent->vd_hash; + map->l_versions[ndx].name = &strtab[aux->vda_name]; + map->l_versions[ndx].filename = NULL; + } + + if (ent->vd_next == 0) + break; + ent = (Elf64_Verdef *) ((char *) ent + ent->vd_next); + } + } + } +} + +const char *rtld_progname; + +static Elf64_Addr load_addr = 0xdead0000; + +static void +create_ldlibs_link_map (struct dso_list *cur_dso_ent) +{ + struct ldlibs_link_map *map = malloc (sizeof (struct ldlibs_link_map)); + DSO *dso = cur_dso_ent->dso; + int i; + Elf_Data *data; + Elf_Symndx *hash; + + memset (map, 0, sizeof (*map)); + cur_dso_ent->map = map; + + if (is_ldso_soname (cur_dso_ent->dso->soname)) + { + map->l_name = dso->filename; + rtld_progname = dso->filename; + } + else + map->l_name = dso->soname; + map->l_soname = dso->soname; + map->filename = dso->filename; + + if (dso->ehdr.e_type == ET_EXEC) + map->l_type = lt_executable; + else + map->l_type = lt_library; + + /* FIXME: gelfify, endianness issues */ + /* and leaks? */ + i = addr_to_sec (dso, dso->info[DT_SYMTAB]); + data = elf_getdata (dso->scn[i], NULL); + map->l_info[DT_SYMTAB] = data->d_buf; + + i = addr_to_sec (dso, dso->info[DT_STRTAB]); + data = elf_getdata (dso->scn[i], NULL); + map->l_info[DT_STRTAB] = data->d_buf; + + i = addr_to_sec (dso, dso->info[DT_HASH]); + data = elf_getdata (dso->scn[i], NULL); + hash = data->d_buf; + map->l_nbuckets = *hash; + map->l_buckets = hash + 2; + map->l_chain = hash + 2 + map->l_nbuckets; + + get_version_info (dso, map); + + map->l_map_start = load_addr; + load_addr += 0x1000; + + map->sym_base = dso->info[DT_SYMTAB] - dso->base; + + for (i = 0; i < dso->ehdr.e_phnum; ++i) + if (dso->phdr[i].p_type == PT_TLS) + { + map->l_tls_blocksize = dso->phdr[i].p_memsz; + map->l_tls_align = dso->phdr[i].p_align; + if (map->l_tls_align == 0) + map->l_tls_firstbyte_offset = 0; + else + map->l_tls_firstbyte_offset = dso->phdr[i].p_vaddr & (map->l_tls_align - 1); + break; + } +} + +struct +{ + void *symptr; + int rtypeclass; +} cache; + +void +do_rel_section (DSO *dso, struct ldlibs_link_map *map, + struct r_scope_elem *scope, + int tag, int section) +{ + Elf_Data *data; + int ndx, maxndx, sym, type; + struct r_found_version *ver; + int rtypeclass; + void *symptr; + const char *name; + Elf64_Word st_name; + + data = elf_getdata (dso->scn[section], NULL); + maxndx = data->d_size / dso->shdr[section].sh_entsize; + for (ndx = 0; ndx < maxndx; ndx++) + { + if (tag == DT_REL) + { + GElf_Rel rel; + gelfx_getrel (dso->elf, data, ndx, &rel); + sym = GELF_R_SYM (rel.r_info); + type = GELF_R_TYPE (rel.r_info); + } + else + { + GElf_Rela rela; + gelfx_getrela (dso->elf, data, ndx, &rela); + sym = GELF_R_SYM (rela.r_info); + type = GELF_R_TYPE (rela.r_info); + } + if (sym == 0) + continue; + if (map->l_versyms) + { + int vernum = map->l_versyms[sym] & 0x7fff; + ver = &map->l_versions[vernum]; + } + else + ver = NULL; + + rtypeclass = reloc_type_class (type, dso->ehdr.e_machine); + + if (gelf_getclass (dso->elf) == ELFCLASS32) + { + Elf32_Sym *sym32 = &((Elf32_Sym *)map->l_info[DT_SYMTAB])[sym]; + + if (ELF32_ST_BIND (sym32->st_info) == STB_LOCAL) + continue; + symptr = sym32; + st_name = sym32->st_name; + } + else + { + Elf64_Sym *sym64 = &((Elf64_Sym *)map->l_info[DT_SYMTAB])[sym]; + + if (ELF64_ST_BIND (sym64->st_info) == STB_LOCAL) + continue; + symptr = sym64; + st_name = sym64->st_name; + } + + if (cache.symptr == symptr && cache.rtypeclass == rtypeclass) + continue; + cache.symptr = symptr; + cache.rtypeclass = rtypeclass; + + name = ((const char *)map->l_info[DT_STRTAB]) + st_name; + + if (gelf_getclass (dso->elf) == ELFCLASS32) + { + if (ver && ver->hash) + rtld_lookup_symbol_versioned (name, symptr, scope, ver, rtypeclass, map, + dso->ehdr.e_machine); + else + rtld_lookup_symbol (name, symptr, scope, rtypeclass, map, dso->ehdr.e_machine); + } + else + { + if (ver && ver->hash) + rtld_lookup_symbol_versioned64 (name, symptr, scope, ver, rtypeclass, map, + dso->ehdr.e_machine); + else + rtld_lookup_symbol64 (name, symptr, scope, rtypeclass, map, dso->ehdr.e_machine); + } + } +} + +void +do_relocs (DSO *dso, struct ldlibs_link_map *map, struct r_scope_elem *scope, int tag) +{ + GElf_Addr rel_start, rel_end; + GElf_Addr pltrel_start, pltrel_end; + int first, last; + + /* Load the DT_REL or DT_RELA section. */ + if (dso->info[tag] != 0) + { + rel_start = dso->info[tag]; + rel_end = rel_start + dso->info[tag == DT_REL ? DT_RELSZ : DT_RELASZ]; + first = addr_to_sec (dso, rel_start); + last = addr_to_sec (dso, rel_end - 1); + while (first <= last) + do_rel_section (dso, map, scope, tag, first++); + + /* If the DT_JMPREL relocs are of the same type and not included, + load them too. Assume they overlap completely or not at all, + and are in at most a single section. They also need to be adjacent. */ + if (dso->info[DT_PLTREL] == tag) + { + pltrel_start = dso->info[DT_JMPREL]; + pltrel_end = pltrel_start + dso->info[DT_PLTRELSZ]; + if (pltrel_start < rel_start || pltrel_start >= rel_end) + do_rel_section (dso, map, scope, tag, addr_to_sec (dso, pltrel_start)); + } + } + else if (dso->info[DT_PLTREL] == tag) + do_rel_section (dso, map, scope, tag, addr_to_sec (dso, dso->info[DT_JMPREL])); +} + +void +handle_relocs (DSO *dso, struct dso_list *dso_list) +{ + struct dso_list *ldso, *tail; + + /* do them all last to first. + skip the dynamic linker; then do it last + in glibc this is conditional on the opencount; but every binary + should be linked to libc and thereby have an opencount for ld.so... + besides, that's the only way it would get on our dso list. */ + + tail = dso_list; + while (tail->next) + tail = tail->next; + + ldso = NULL; + while (tail) + { + if (is_ldso_soname (tail->dso->soname)) + ldso = tail; + else + { + /* Load the symbols and relocations. */ + do_relocs (tail->dso, tail->map, dso_list->map->l_local_scope, DT_REL); + do_relocs (tail->dso, tail->map, dso_list->map->l_local_scope, DT_RELA); + } + tail = tail->prev; + } + + if (ldso) + { + do_relocs (ldso->dso, ldso->map, dso_list->map->l_local_scope, DT_REL); + do_relocs (ldso->dso, ldso->map, dso_list->map->l_local_scope, DT_RELA); + } +} + +void +add_to_scope (struct r_scope_elem *scope, struct dso_list *ent) +{ + struct needed_list *n; + int i; + + for (i = 0; i < scope->r_nlist; i++) + if (scope->r_list[i] == ent->map) + return; + + scope->r_list[scope->r_nlist++] = ent->map; + n = ent->needed; + while (n) + { + add_to_scope (scope, n->ent); + n = n->next; + } +} + +void +build_local_scope (struct dso_list *ent, int max) +{ + ent->map->l_local_scope = malloc (sizeof (struct r_scope_elem)); + ent->map->l_local_scope->r_list = malloc (sizeof (struct ldlibs_link_map *) * max); + ent->map->l_local_scope->r_nlist = 0; + add_to_scope (ent->map->l_local_scope, ent); +} + +/* Assign TLS offsets for every loaded library. This code is taken + almost directly from glibc! */ + +#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) + +static void +determine_tlsoffsets (int e_machine, struct r_scope_elem *search_list) +{ + uint64_t freetop = 0; + uint64_t freebottom = 0; + uint64_t offset; + uint64_t modid = 1; + int i; + + /* This comes from each architecture's ABI. If TLS_TCB_AT_TP, then + set offset to -1; if TLS_DTV_AT_TP, then set offset to + TLS_TCB_SIZE. */ + switch (e_machine) + { + case EM_X86_64: + offset = -1; + break; + + case EM_386: + offset = -1; + break; + + case EM_SH: + offset = 8; + break; + + case EM_PPC: + offset = 0; + break; + + case EM_PPC64: + offset = 0; + break; + + case EM_ARM: + offset = 8; + break; + + default: + /* Hope there's no TLS! */ + for (i = 0; i < search_list->r_nlist; i++) + { + struct ldlibs_link_map *map = search_list->r_list[i]; + + if (map->l_tls_blocksize > 0) + error (1, 0, "TLS encountered on an unsupported architecture"); + } + + return; + } + + /* Loop over the loaded DSOs. We use the symbol search order; this + should be the same as glibc's ordering, which traverses l_next. + It's somewhat important that we use both the same ordering to + assign module IDs and the same algorithm to assign offsets, + because the prelinker will resolve all relocations using these + offsets... and then glibc will recalculate them. Future dynamic + relocations in any loaded modules will use glibc's values. Also + if we take too much space here, glibc won't allocate enough + static TLS area to hold it. */ + + if (offset == (uint64_t) -1) + { + /* We simply start with zero. */ + offset = 0; + + for (i = 0; i < search_list->r_nlist; i++) + { + struct ldlibs_link_map *map = search_list->r_list[i]; + uint64_t firstbyte = (-map->l_tls_firstbyte_offset + & (map->l_tls_align - 1)); + uint64_t off; + + if (map->l_tls_blocksize == 0) + continue; + map->l_tls_modid = modid++; + + if (freebottom - freetop >= map->l_tls_blocksize) + { + off = roundup (freetop + map->l_tls_blocksize + - firstbyte, map->l_tls_align) + + firstbyte; + if (off <= freebottom) + { + freetop = off; + + map->l_tls_offset = off; + continue; + } + } + + off = roundup (offset + map->l_tls_blocksize - firstbyte, + map->l_tls_align) + firstbyte; + if (off > offset + map->l_tls_blocksize + + (freebottom - freetop)) + { + freetop = offset; + freebottom = off - map->l_tls_blocksize; + } + offset = off; + + map->l_tls_offset = off; + } + } + else + { + for (i = 0; i < search_list->r_nlist; i++) + { + struct ldlibs_link_map *map = search_list->r_list[i]; + uint64_t firstbyte = (-map->l_tls_firstbyte_offset + & (map->l_tls_align - 1)); + uint64_t off; + + if (map->l_tls_blocksize == 0) + continue; + map->l_tls_modid = modid++; + + if (map->l_tls_blocksize <= freetop - freebottom) + { + off = roundup (freebottom, map->l_tls_align); + if (off - freebottom < firstbyte) + off += map->l_tls_align; + if (off + map->l_tls_blocksize - firstbyte <= freetop) + { + map->l_tls_offset = off - firstbyte; + freebottom = (off + map->l_tls_blocksize + - firstbyte); + continue; + } + } + + off = roundup (offset, map->l_tls_align); + if (off - offset < firstbyte) + off += map->l_tls_align; + + map->l_tls_offset = off - firstbyte; + if (off - firstbyte - offset > freetop - freebottom) + { + freebottom = offset; + freetop = off - firstbyte; + } + + offset = off + map->l_tls_blocksize - firstbyte; + } + } +} + + +struct ldlibs_link_map *requested_map; + +static void process_one_dso (DSO *dso, int host_paths); + +int +main(int argc, char **argv) +{ + int host_paths = 1; + int multiple = 0; + + sysroot = getenv ("PRELINK_SYSROOT"); +#ifdef DEFAULT_SYSROOT + if (sysroot == NULL) + { + extern char *make_relative_prefix (const char *, const char *, const char *); + sysroot = make_relative_prefix (argv[0], BINDIR, DEFAULT_SYSROOT); + } +#endif + + if (sysroot) + sysroot = prelink_canonicalize (sysroot, NULL); + + elf_version (EV_CURRENT); + + while (1) + { + if (argc > 2 && strcmp (argv[1], "--library-path") == 0) + { + string_to_path (&ld_library_search_path, argv[2]); + argc -= 2; + argv += 2; + } + else if (argc > 1 && strcmp (argv[1], "--target-paths") == 0) + { + host_paths = 0; + argc -= 1; + argv += 1; + } + else + break; + } + + if (argc < 2) + error (1, 0, "No filename given."); + + if (argc > 2) + multiple = 1; + + while (argc > 1) + { + DSO *dso = NULL; + int i, fd; + + if (host_paths) + fd = open (argv[1], O_RDONLY); + else + fd = wrap_open (argv[1], O_RDONLY); + + if (fd >= 0) + dso = fdopen_dso (fd, argv[1]); + + if (dso == NULL) + error (1, errno, "Could not open %s", argv[1]); + + load_ld_so_conf (gelf_getclass (dso->elf) == ELFCLASS64); + + if (multiple) + printf ("%s:\n", argv[1]); + + for (i = 0; i < dso->ehdr.e_phnum; ++i) + if (dso->phdr[i].p_type == PT_INTERP) + break; + + /* If there are no PT_INTERP segments, it is statically linked. */ + if (dso->ehdr.e_type == ET_EXEC && i == dso->ehdr.e_phnum) + printf ("\tnot a dynamic executable\n"); + else + process_one_dso (dso, host_paths); + + argc -= 1; + argv += 1; + } + + return 0; +} + +static void +process_one_dso (DSO *dso, int host_paths) +{ + struct dso_list *dso_list, *cur_dso_ent, *old_dso_ent; + const char *req = getenv ("RTLD_TRACE_PRELINKING"); + int i, flag; + int process_relocs = 0; + + /* Close enough. Really it's if LD_WARN is "" and RTLD_TRACE_PRELINKING. */ + if (getenv ("LD_WARN") == 0 && req != NULL) + process_relocs = 1; + + dso_list = load_dsos (dso); + + cur_dso_ent = dso_list; + i = 0; + while (cur_dso_ent) + { + if (cur_dso_ent->dso) + { + create_ldlibs_link_map (cur_dso_ent); + if (req && strcmp (req, cur_dso_ent->dso->filename) == 0) + requested_map = cur_dso_ent->map; + i++; + } + cur_dso_ent = cur_dso_ent->next; + } + dso_list->map->l_local_scope = malloc (sizeof (struct r_scope_elem)); + dso_list->map->l_local_scope->r_list = malloc (sizeof (struct ldlibs_link_map *) * i); + dso_list->map->l_local_scope->r_nlist = i; + cur_dso_ent = dso_list; + i = 0; + while (cur_dso_ent) + { + if (cur_dso_ent->dso) + { + dso_list->map->l_local_scope->r_list[i] = cur_dso_ent->map; + if (cur_dso_ent != dso_list) + build_local_scope (cur_dso_ent, dso_list->map->l_local_scope->r_nlist); + + i++; + } + cur_dso_ent = cur_dso_ent->next; + } + + determine_tlsoffsets (dso->ehdr.e_machine, dso_list->map->l_local_scope); + + cur_dso_ent = dso_list; + flag = 0; + /* In ldd mode, do not show the application. Note that we do show it + in list-loaded-objects RTLD_TRACE_PRELINK mode. */ + if (req == NULL && cur_dso_ent) + cur_dso_ent = cur_dso_ent->next; + while (cur_dso_ent) + { + char *filename; + + if (host_paths && sysroot && cur_dso_ent->dso) + { + const char *rooted_filename; + + if (cur_dso_ent->dso->filename[0] == '/') + rooted_filename = cur_dso_ent->dso->filename; + else + rooted_filename = wrap_prelink_canonicalize (cur_dso_ent->dso->filename, NULL); + + /* This covers the odd case where we have a sysroot set, + * but the item isn't in the sysroot! + */ + if (rooted_filename == NULL) + filename = strdup (cur_dso_ent->dso->filename); + else + { + filename = malloc (strlen (rooted_filename) + strlen (sysroot) + 1); + strcpy (filename, sysroot); + strcat (filename, rooted_filename); + } + } + else if (cur_dso_ent->dso) + filename = strdup (cur_dso_ent->dso->filename); + else + filename = NULL; + + /* The difference between the two numbers must be dso->base, + and the first number must be unique. */ + if (cur_dso_ent->dso == NULL) + printf ("\t%s => not found\n", cur_dso_ent->name); + else if (gelf_getclass (cur_dso_ent->dso->elf) == ELFCLASS32) + { + if (process_relocs) + { + printf ("\t%s => %s (0x%08x, 0x%08x)", + cur_dso_ent->name, filename, + (uint32_t) cur_dso_ent->map->l_map_start, + (uint32_t) (cur_dso_ent->map->l_map_start - cur_dso_ent->dso->base)); + if (cur_dso_ent->map->l_tls_modid) + printf (" TLS(0x%x, 0x%08x)", + (uint32_t) cur_dso_ent->map->l_tls_modid, + (uint32_t) cur_dso_ent->map->l_tls_offset); + printf ("\n"); + } + else + printf ("\t%s => %s (0x%08x)\n", + cur_dso_ent->name, filename, + (uint32_t) cur_dso_ent->map->l_map_start); + } + else + { + if (process_relocs) + { + printf ("\t%s => %s (0x%016" HOST_LONG_LONG_FORMAT + "x, 0x%016" HOST_LONG_LONG_FORMAT "x)", + cur_dso_ent->name, filename, + (unsigned long long) cur_dso_ent->map->l_map_start, + (unsigned long long) (cur_dso_ent->map->l_map_start - cur_dso_ent->dso->base)); + if (cur_dso_ent->map->l_tls_modid) + printf (" TLS(0x%x, 0x%016" HOST_LONG_LONG_FORMAT "x)", + (uint32_t) cur_dso_ent->map->l_tls_modid, + (unsigned long long) cur_dso_ent->map->l_tls_offset); + printf ("\n"); + } + else + printf ("\t%s => %s (0x%08x)\n", + cur_dso_ent->name, filename, + (uint32_t) cur_dso_ent->map->l_map_start); + } + + if (filename) + free (filename); + + cur_dso_ent = cur_dso_ent->next; + flag = 1; + } + + if (dso_open_error) + exit (1); + + if (process_relocs) + handle_relocs (dso_list->dso, dso_list); + + cur_dso_ent = dso_list; + while (cur_dso_ent) + { + if (cur_dso_ent->dso) + close_dso (cur_dso_ent->dso); + old_dso_ent = cur_dso_ent; + cur_dso_ent = cur_dso_ent->next; + if (old_dso_ent->needed) + free_needed (old_dso_ent->needed); + free (old_dso_ent); + } +} diff --git a/src/ld-libs.h b/src/ld-libs.h new file mode 100644 index 0000000..48dd555 --- /dev/null +++ b/src/ld-libs.h @@ -0,0 +1,113 @@ +#ifndef _LD_LIBS_H +#define _LD_LIBS_H + +#if !defined (__linux__) +#define DT_VERSIONTAGNUM 16 +#endif + +struct ldlibs_link_map; + +struct r_scope_elem +{ + struct ldlibs_link_map **r_list; + unsigned int r_nlist; +}; + +struct r_found_version + { + const char *name; + Elf64_Word hash; + + int hidden; + const char *filename; + }; + +/* The size of entries in .hash. Only Alpha and 64-bit S/390 use 64-bit + entries; those are not currently supported. */ +typedef uint32_t Elf_Symndx; + +struct ldlibs_link_map + { + const char *l_name; + struct r_scope_elem *l_local_scope; + enum { lt_executable, lt_library, lt_loaded } l_type; + void *l_info[DT_NUM + DT_VERSIONTAGNUM]; + + /* Symbol hash table. */ + Elf_Symndx l_nbuckets; + const Elf_Symndx *l_buckets, *l_chain; + + unsigned int l_nversions; + struct r_found_version *l_versions; + + /* Pointer to the version information if available. Fortunately, 32-bit + and 64-bit ELF use the same Versym type. */ + Elf64_Versym *l_versyms; + + /* for _dl_soname_match_p */ + const char *l_soname; + + Elf64_Addr l_map_start; + + Elf64_Addr sym_base; + const char *filename; + + /* For TLS. From the object file. */ + uint64_t l_tls_blocksize; + uint64_t l_tls_align; + uint64_t l_tls_firstbyte_offset; + + /* For TLS. Computed. */ + uint64_t l_tls_modid; + uint64_t l_tls_offset; + }; + +#define ELF_RTYPE_CLASS_COPY 2 +#define ELF_RTYPE_CLASS_PLT 1 + +#define GL(X) _ ## X +#define INTUSE(X) X + +#define D_PTR(MAP,MEM) MAP->MEM +#define VERSYMIDX(tag) DT_NUM + DT_VERSIONTAGIDX (tag) + +extern int _dl_debug_mask; +#define DL_DEBUG_SYMBOLS 0 +#define DL_LOOKUP_RETURN_NEWEST 0 +#define _dl_dynamic_weak 0 +extern const char *rtld_progname; +#define _dl_debug_printf printf + + +#define USE_TLS + +#ifndef rtld_lookup_symbol +void rtld_lookup_symbol (const char *name, const Elf32_Sym *sym, + struct r_scope_elem *scope, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine); +void rtld_lookup_symbol_versioned (const char *name, const Elf32_Sym *sym, + struct r_scope_elem *scope, + struct r_found_version *version, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine); +#endif + +void rtld_lookup_symbol64 (const char *name, const Elf64_Sym *sym, + struct r_scope_elem *scope, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine); +void rtld_lookup_symbol_versioned64 (const char *name, const Elf64_Sym *sym, + struct r_scope_elem *scope, + struct r_found_version *version, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine); + +extern struct ldlibs_link_map *requested_map; + +#define __builtin_expect(a,b) (a) + +#if defined(__MINGW32__) +# define HOST_LONG_LONG_FORMAT "I64" +#else +# define HOST_LONG_LONG_FORMAT "ll" +#endif + +#endif + diff --git a/src/ld-lookup.c b/src/ld-lookup.c new file mode 100644 index 0000000..69b6bd1 --- /dev/null +++ b/src/ld-lookup.c @@ -0,0 +1,214 @@ +/* Copyright (C) 2003 MontaVista Software, Inc. + Written by Daniel Jacobowitz <drow@mvista.com>, 2003 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, 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 of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include <config.h> +#include <assert.h> +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "prelinktab.h" +#include "reloc.h" + +#include "ld-libs.h" + +#ifndef ElfW +/* Default to 32-bit. */ +#define ElfW(x) Elf32_##x +#define ELFW(x) ELF32_##x +#endif + +static int _dl_soname_match_p (const char *name, struct ldlibs_link_map *map); + +struct sym_val +{ + const ElfW(Sym) *s; + struct ldlibs_link_map *m; +}; + +#include "ld-do-lookup.h" +#define VERSIONED 1 +#include "ld-do-lookup.h" +#undef VERSIONED + +static unsigned long +rtld_elf_hash (const char *name) +{ + const unsigned char *str = (const unsigned char *) name; + unsigned long int hash, hi; + hash = *str++; + while (*str != '\0') + { + hash = (hash << 4) + *str++; + hi = hash & 0xf0000000; + hash ^= hi; + hash ^= hi >> 24; + } + return hash & 0xffffffff; +} + +static int +_dl_soname_match_p (const char *name, struct ldlibs_link_map *map) +{ + if (strcmp (name, map->l_name) == 0) + return 1; + if (strcmp (name, map->l_soname) == 0) + return 1; + return 0; +} + +#if 0 +void +rtld_lookup_symbol (const char *name, const ElfW(Sym) *sym, + struct r_scope_elem *scope, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine) +{ + int ret; + struct sym_val result; + + result.s = NULL; + ret = do_lookup (name, rtld_elf_hash (name), sym, + &result, scope, 0, 0, NULL, rtypeclass); + if (ret > 0) + printf ("name %s /%d\n", name, rtypeclass); +#if 0 + printf ("name %s ret %d", name, ret); + if (result.s) + printf (" result sym 0x%08x (in %s)", result.s->st_value, result.m->l_name); + printf ("\n"); +#endif +} +#endif + +void +rtld_lookup_symbol (const char *name, const ElfW(Sym) *sym, + struct r_scope_elem *scope, + int rtypeclass, + struct ldlibs_link_map *undef_map, int machine) +{ + rtld_lookup_symbol_versioned (name, sym, scope, NULL, rtypeclass, undef_map, machine); +} + +void +rtld_lookup_symbol_versioned (const char *name, const ElfW(Sym) *sym, + struct r_scope_elem *scope, + struct r_found_version *version, int rtypeclass, + struct ldlibs_link_map *undef_map, int machine) +{ + int ret; + int conflict = 0; + int sym_offset; + struct sym_val result, result2; + unsigned int value1, value2; + + result.s = NULL; + result.m = NULL; + result2.s = NULL; + result2.m = NULL; + if (version) + ret = do_lookup_versioned (name, rtld_elf_hash (name), sym, + &result, scope, 0, version, NULL, rtypeclass, + machine); + else + ret = do_lookup (name, rtld_elf_hash (name), sym, + &result, scope, 0, 0, NULL, rtypeclass, + machine); + + if (result.s == NULL && ELFW(ST_BIND) (sym->st_info) != STB_WEAK) + printf ("undefined symbol: %s\t(%s)\n", name, undef_map->filename); + + if (ret <= 0) + return; + + /* Don't do conflict checking for references in the executable. */ + if (undef_map->l_local_scope != scope) + { + result2.s = NULL; + result2.m = NULL; + if (version) + ret = do_lookup_versioned (name, rtld_elf_hash (name), sym, + &result2, undef_map->l_local_scope, 0, version, + NULL, rtypeclass, machine); + else + ret = do_lookup (name, rtld_elf_hash (name), sym, + &result2, undef_map->l_local_scope, 0, 0, + NULL, rtypeclass, machine); + + if (result2.s != result.s + || result2.m != result.m) + conflict = 1; + } + + if (result.s && ELFW(ST_TYPE) (result.s->st_info) == STT_TLS) + rtypeclass = 4; + + /* Print out information for the requested object, all conflicts, and all TLS. */ + if (!conflict + && rtypeclass != 4 + && requested_map + && requested_map != undef_map) + return; + + /* FIXME: Careful with this if we change the size of symbols when reading in! */ + sym_offset = ((char *)sym) - ((char *)undef_map->l_info[DT_SYMTAB]); + sym_offset += undef_map->sym_base; + + value1 = 0; + if (machine == EM_ARM && result.s + && ELFW(ST_TYPE) (result.s->st_info) == STT_ARM_TFUNC) + value1 = 1; + + value2 = 0; + if (machine == EM_ARM && conflict && result2.s + && ELFW(ST_TYPE) (result2.s->st_info) == STT_ARM_TFUNC) + value2 = 1; + +#if defined(rtld_lookup_symbol) /* 64-bit */ + printf ("%s 0x%016" HOST_LONG_LONG_FORMAT "x " + "0x%016" HOST_LONG_LONG_FORMAT "x " + "-> 0x%016" HOST_LONG_LONG_FORMAT "x " + "0x%016" HOST_LONG_LONG_FORMAT "x ", + conflict ? "conflict" : "lookup", + (unsigned long long) undef_map->l_map_start, + (unsigned long long) sym_offset, + (unsigned long long) (result.s ? result.m->l_map_start : 0), + (unsigned long long) (result.s ? result.s->st_value | value1 : 0)); + if (conflict) + printf ("x 0x%016" HOST_LONG_LONG_FORMAT "x " + "0x%016" HOST_LONG_LONG_FORMAT "x ", + (unsigned long long) (result2.s ? result2.m->l_map_start : 0), + (unsigned long long) (result2.s ? result2.s->st_value | value2 : 0)); +#else + printf ("%s 0x%08x 0x%08x -> 0x%08x 0x%08x ", + conflict ? "conflict" : "lookup", + (uint32_t) undef_map->l_map_start, + (uint32_t) sym_offset, + (uint32_t) (result.s ? result.m->l_map_start : 0), + (uint32_t) (result.s ? result.s->st_value | value1 : 0)); + if (conflict) + printf ("x 0x%08x 0x%08x ", + (uint32_t) (result2.s ? result2.m->l_map_start : 0), + (uint32_t) (result2.s ? result2.s->st_value | value2 : 0)); +#endif + printf ("/%x %s\n", rtypeclass, name); +} + diff --git a/src/ld-lookup64.c b/src/ld-lookup64.c new file mode 100644 index 0000000..7e35334 --- /dev/null +++ b/src/ld-lookup64.c @@ -0,0 +1,24 @@ +/* Copyright (C) 2003 MontaVista Software, Inc. + Written by Daniel Jacobowitz <drow@mvista.com>, 2003 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, 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 of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#define ElfW(x) Elf64_ ## x +#define ELFW(x) ELF64_ ## x + +#define rtld_lookup_symbol rtld_lookup_symbol64 +#define rtld_lookup_symbol_versioned rtld_lookup_symbol_versioned64 + +#include "ld-lookup.c" @@ -80,6 +80,7 @@ static char argp_doc[] = "prelink -- program to relocate and prelink ELF shared #define OPT_SHA 0x8a #define OPT_COMPUTE_CHECKSUM 0x8b #define OPT_SYSROOT 0x8c +#define OPT_RTLD 0x8d static struct argp_option options[] = { {"all", 'a', 0, 0, "Prelink all binaries" }, @@ -115,6 +116,7 @@ static struct argp_option options[] = { {"mmap-region-end", OPT_MMAP_REG_END, "BASE_ADDRESS", OPTION_HIDDEN, "" }, {"seed", OPT_SEED, "SEED", OPTION_HIDDEN, "" }, {"compute-checksum", OPT_COMPUTE_CHECKSUM, 0, OPTION_HIDDEN, "" }, + {"rtld", OPT_RTLD, "RTLD", OPTION_HIDDEN, "" }, {"init", 'i', 0, 0, "Do not re-execute init" }, { 0 } }; @@ -230,6 +232,9 @@ parse_opt (int key, char *arg, struct argp_state *state) case OPT_SYSROOT: sysroot = arg; break; + case OPT_RTLD: + prelink_rtld = arg; + break; case 'i': noreexecinit=1; break; @@ -255,6 +260,8 @@ void checkinit() { static struct argp argp = { options, parse_opt, "[FILES]", argp_doc }; +const char *prelink_rtld = NULL; + int main (int argc, char *argv[]) { @@ -314,6 +321,18 @@ main (int argc, char *argv[]) asprintf ((char **) &prelink_conf, "%s%s", sysroot, prelink_conf); } + if (prelink_rtld == NULL) + { + extern char *make_relative_prefix (const char *, const char *, const char *); + const char *path = make_relative_prefix (argv[0], BINDIR, BINDIR); + if (strchr (argv[0], '/')) + asprintf ((char **) &prelink_rtld, "%s-rtld", argv[0]); + else + asprintf ((char **) &prelink_rtld, "%s/%s-rtld", path, argv[0]); + } + else if (prelink_rtld[0] == 0) + prelink_rtld = NULL; + if (print_cache) { prelink_load_cache (); diff --git a/src/prelink.h b/src/prelink.h index 64cd348..ae2e24e 100644 --- a/src/prelink.h +++ b/src/prelink.h @@ -455,4 +455,6 @@ int wrap_utime (const char *file, struct utimbuf *file_times); int wrap_mkstemp (char *filename); int wrap_unlink (const char *filename); +extern const char *prelink_rtld; + #endif /* PRELINK_H */ |