summaryrefslogtreecommitdiff
path: root/elf
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2006-01-30 09:30:09 +0000
committerJakub Jelinek <jakub@redhat.com>2006-01-30 09:30:09 +0000
commit3e543bc56346540cbf73fd48d0172fc6a588efd5 (patch)
treeb2c3502b6596d238ac861cc82cafdc6f8b84e143 /elf
parent06f313e361a523605ba6d4c9cdc67a7353cd367c (diff)
Updated to fedora-glibc-20060130T0922
Diffstat (limited to 'elf')
-rw-r--r--elf/Makefile30
-rw-r--r--elf/check-localplt.c299
-rw-r--r--elf/check-textrel.c20
-rw-r--r--elf/dl-deps.c2
4 files changed, 343 insertions, 8 deletions
diff --git a/elf/Makefile b/elf/Makefile
index 91fb218ccd..5cd78c2f83 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1995-2004, 2005 Free Software Foundation, Inc.
+# Copyright (C) 1995-2004, 2005, 2006 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
@@ -647,7 +647,7 @@ $(objpfx)dblunload: $(libdl)
$(objpfx)dblunload.out: $(objpfx)dblloadmod1.so $(objpfx)dblloadmod2.so
$(objpfx)reldep5: $(libdl)
-$(objpfx)reldep5.out: $(objpfx)reldepmod5.so $(objpfx)reldepmod5.so
+$(objpfx)reldep5.out: $(objpfx)reldepmod5.so $(objpfx)reldepmod6.so
$(objpfx)reldep6: $(libdl)
$(objpfx)reldep6.out: $(objpfx)reldep6mod3.so $(objpfx)reldep6mod4.so
@@ -806,6 +806,10 @@ check-textrel-CFLAGS = -O -Wall -D_XOPEN_SOURCE=600 -D_BSD_SOURCE
$(objpfx)check-textrel: check-textrel.c
$(native-compile)
+check-localplt-CFLAGS = -O -Wall -D_GNU_SOURCE -std=gnu99
+$(objpfx)check-localplt: check-localplt.c
+ $(native-compile)
+
ifeq (yes,$(build-shared))
tests: $(objpfx)check-textrel.out
@@ -818,6 +822,28 @@ generated += check-textrel check-textrel.out
$(objpfx)tst-dlmodcount: $(libdl)
$(objpfx)tst-dlmodcount.out: $(test-modules)
+check-data := $(firstword $(wildcard \
+ $(foreach M,$(config-machine) $(base-machine),\
+ ../scripts/data/localplt-$M-$(config-os).data)))
+ifneq (,$(check-data))
+tests: $(objpfx)check-localplt.out
+
+ifeq ($(have-thread-library),yes)
+thread-dso := $(filter-out %_nonshared.a, $(shared-thread-library))
+endif
+
+$(objpfx)check-localplt.out: $(objpfx)check-localplt $(common-objpfx)libc.so \
+ $(common-objpfx)math/libm.so $(thread-dso) \
+ $(common-objpfx)rt/librt.so \
+ $(common-objpfx)dlfcn/libdl.so \
+ $(check-data)
+ $(objpfx)check-localplt $(common-objpfx)libc.so \
+ $(common-objpfx)math/libm.so $(thread-dso) \
+ $(common-objpfx)rt/librt.so \
+ $(common-objpfx)dlfcn/libdl.so | \
+ LC_ALL=C sort | \
+ diff -u $(check-data) - > $@
+endif
endif
$(objpfx)tst-dlopenrpathmod.so: $(libdl)
diff --git a/elf/check-localplt.c b/elf/check-localplt.c
new file mode 100644
index 0000000000..b4358a8a3f
--- /dev/null
+++ b/elf/check-localplt.c
@@ -0,0 +1,299 @@
+/* Show local PLT use in DSOs.
+ Copyright (C) 2006 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contribute by Ulrich Drepper <drepper@redhat.com>. 2006.
+
+ 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. */
+
+#include <byteswap.h>
+#include <elf.h>
+#include <endian.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+#ifdef BITS
+
+# define AB(name) _AB (name, BITS)
+# define _AB(name, bits) __AB (name, bits)
+# define __AB(name, bits) name##bits
+# define E(name) _E (name, BITS)
+# define _E(name, bits) __E (name, bits)
+# define __E(name, bits) Elf##bits##_##name
+# define EE(name) _EE (name, BITS)
+# define _EE(name, bits) __EE (name, bits)
+# define __EE(name, bits) ELF##bits##_##name
+# define SWAP(val) \
+ ({ __typeof (val) __res; \
+ if (((ehdr.e_ident[EI_DATA] == ELFDATA2MSB \
+ && BYTE_ORDER == LITTLE_ENDIAN) \
+ || (ehdr.e_ident[EI_DATA] == ELFDATA2LSB \
+ && BYTE_ORDER == BIG_ENDIAN)) \
+ && sizeof (val) != 1) \
+ { \
+ if (sizeof (val) == 2) \
+ __res = bswap_16 (val); \
+ else if (sizeof (val) == 4) \
+ __res = bswap_32 (val); \
+ else \
+ __res = bswap_64 (val); \
+ } \
+ else \
+ __res = (val); \
+ __res; })
+
+
+static int
+AB(handle_file) (const char *fname, int fd)
+{
+ E(Ehdr) ehdr;
+
+ if (pread (fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr))
+ {
+ read_error:
+ printf ("%s: read error: %m\n", fname);
+ return 1;
+ }
+
+ const size_t phnum = SWAP (ehdr.e_phnum);
+ const size_t phentsize = SWAP (ehdr.e_phentsize);
+
+ /* Read the program header. */
+ E(Phdr) *phdr = alloca (phentsize * phnum);
+ if (pread (fd, phdr, phentsize * phnum, SWAP (ehdr.e_phoff))
+ != phentsize * phnum)
+ goto read_error;
+
+ /* Search for the PT_DYNAMIC entry. */
+ size_t cnt;
+ E(Phdr) *dynphdr = NULL;
+ for (cnt = 0; cnt < phnum; ++cnt)
+ if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC)
+ {
+ dynphdr = &phdr[cnt];
+ break;
+ }
+
+ if (dynphdr == NULL)
+ {
+ printf ("%s: no DYNAMIC segment found\n", fname);
+ return 1;
+ }
+
+ /* Read the dynamic segment. */
+ size_t pmemsz = SWAP(dynphdr->p_memsz);
+ E(Dyn) *dyn = alloca (pmemsz);
+ if (pread64 (fd, dyn, pmemsz, SWAP(dynphdr->p_offset)) != pmemsz)
+ goto read_error;
+
+ /* Search for an DT_PLTREL, DT_JMPREL, DT_PLTRELSZ, DT_STRTAB,
+ DT_STRSZ, and DT_SYMTAB entries. */
+ size_t pltrel_idx = SIZE_MAX;
+ size_t jmprel_idx = SIZE_MAX;
+ size_t pltrelsz_idx = SIZE_MAX;
+ size_t strtab_idx = SIZE_MAX;
+ size_t strsz_idx = SIZE_MAX;
+ size_t symtab_idx = SIZE_MAX;
+ for (cnt = 0; (cnt + 1) * sizeof (E(Dyn)) - 1 < pmemsz; ++cnt)
+ {
+ unsigned int tag = SWAP (dyn[cnt].d_tag);
+
+ if (tag == DT_NULL)
+ /* We reached the end. */
+ break;
+
+ if (tag == DT_PLTREL)
+ pltrel_idx = cnt;
+ else if (tag == DT_JMPREL)
+ jmprel_idx = cnt;
+ else if (tag == DT_PLTRELSZ)
+ pltrelsz_idx = cnt;
+ else if (tag == DT_STRTAB)
+ strtab_idx = cnt;
+ else if (tag == DT_STRSZ)
+ strsz_idx = cnt;
+ else if (tag == DT_SYMTAB)
+ symtab_idx = cnt;
+ }
+
+ if (pltrel_idx == SIZE_MAX || jmprel_idx == SIZE_MAX
+ || pltrelsz_idx == SIZE_MAX || strtab_idx == SIZE_MAX
+ || strsz_idx == SIZE_MAX || symtab_idx == SIZE_MAX)
+ {
+ puts ("not all PLT information found");
+ return 1;
+ }
+
+ E(Xword) relsz = SWAP (dyn[pltrelsz_idx].d_un.d_val);
+
+ void *relmem = NULL;
+ char *strtab = NULL;
+ E(Xword) symtab_offset = 0;
+
+ /* Find the offset of DT_JMPREL and load the data. */
+ for (cnt = 0; cnt < phnum; ++cnt)
+ if (SWAP (phdr[cnt].p_type) == PT_LOAD)
+ {
+ E(Addr) vaddr = SWAP (phdr[cnt].p_vaddr);
+ E(Xword) memsz = SWAP (phdr[cnt].p_memsz);
+
+ if (vaddr <= SWAP (dyn[jmprel_idx].d_un.d_val)
+ && vaddr + memsz >= SWAP (dyn[jmprel_idx].d_un.d_val) + relsz)
+ {
+ relmem = alloca (SWAP (dyn[pltrelsz_idx].d_un.d_val));
+ if (pread64 (fd, relmem, relsz,
+ SWAP (phdr[cnt].p_offset)
+ + SWAP (dyn[jmprel_idx].d_un.d_val) - vaddr)
+ != relsz)
+ {
+ puts ("cannot read JMPREL");
+ return 1;
+ }
+ }
+
+ if (vaddr <= SWAP (dyn[symtab_idx].d_un.d_val)
+ && vaddr + memsz > SWAP (dyn[symtab_idx].d_un.d_val))
+ symtab_offset = (SWAP (phdr[cnt].p_offset)
+ + SWAP (dyn[symtab_idx].d_un.d_val) - vaddr);
+
+ if (vaddr <= SWAP (dyn[strtab_idx].d_un.d_val)
+ && vaddr + memsz >= (SWAP (dyn[strtab_idx].d_un.d_val)
+ + SWAP(dyn[strsz_idx].d_un.d_val)))
+ {
+ strtab = alloca (SWAP(dyn[strsz_idx].d_un.d_val));
+ if (pread64 (fd, strtab, SWAP(dyn[strsz_idx].d_un.d_val),
+ SWAP (phdr[cnt].p_offset)
+ + SWAP (dyn[strtab_idx].d_un.d_val) - vaddr)
+ != SWAP(dyn[strsz_idx].d_un.d_val))
+ {
+ puts ("cannot read STRTAB");
+ return 1;
+ }
+ }
+ }
+
+ if (relmem == NULL || strtab == NULL || symtab_offset == 0)
+ {
+ puts ("couldn't load PLT data");
+ return 1;
+ }
+
+ if (SWAP (dyn[pltrel_idx].d_un.d_val) == DT_RELA)
+ for (E(Rela) *rela = relmem; (char *) rela - (char *) relmem < relsz;
+ ++rela)
+ {
+ E(Sym) sym;
+
+ if (pread64 (fd, &sym, sizeof (sym),
+ symtab_offset
+ + EE(R_SYM) (SWAP (rela->r_info)) * sizeof (sym))
+ != sizeof (sym))
+ {
+ puts ("cannot read symbol");
+ return 1;
+ }
+
+ if (sym.st_value != 0)
+ /* This symbol is locally defined. */
+ printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
+ }
+ else
+ for (E(Rel) *rel = relmem; (char *) rel - (char *) relmem < relsz; ++rel)
+ {
+ E(Sym) sym;
+
+ if (pread64 (fd, &sym, sizeof (sym),
+ symtab_offset
+ + EE(R_SYM) (SWAP (rel->r_info)) * sizeof (sym))
+ != sizeof (sym))
+ {
+ puts ("cannot read symbol");
+ return 1;
+ }
+
+ if (sym.st_value != 0)
+ /* This symbol is locally defined. */
+ printf ("%s: %s\n", basename (fname), strtab + SWAP (sym.st_name));
+ }
+
+ return 0;
+}
+
+# undef BITS
+#else
+
+# define BITS 32
+# include "check-localplt.c"
+
+# define BITS 64
+# include "check-localplt.c"
+
+
+static int
+handle_file (const char *fname)
+{
+ int fd = open (fname, O_RDONLY);
+ if (fd == -1)
+ {
+ printf ("cannot open %s: %m\n", fname);
+ return 1;
+ }
+
+ /* Read was is supposed to be the ELF header. Read the initial
+ bytes to determine whether this is a 32 or 64 bit file. */
+ char ident[EI_NIDENT];
+ if (read (fd, ident, EI_NIDENT) != EI_NIDENT)
+ {
+ printf ("%s: read error: %m\n", fname);
+ close (fd);
+ return 1;
+ }
+
+ if (memcmp (&ident[EI_MAG0], ELFMAG, SELFMAG) != 0)
+ {
+ printf ("%s: not an ELF file\n", fname);
+ close (fd);
+ return 1;
+ }
+
+ int result;
+ if (ident[EI_CLASS] == ELFCLASS64)
+ result = handle_file64 (fname, fd);
+ else
+ result = handle_file32 (fname, fd);
+
+ close (fd);
+
+ return result;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ int cnt;
+ int result = 0;
+
+ for (cnt = 1; cnt < argc; ++cnt)
+ result |= handle_file (argv[cnt]);
+
+ return result;
+}
+#endif
diff --git a/elf/check-textrel.c b/elf/check-textrel.c
index ec97e4b0c9..2b9639ff19 100644
--- a/elf/check-textrel.c
+++ b/elf/check-textrel.c
@@ -1,5 +1,5 @@
/* Check for text relocations in DSOs.
- Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contribute by Ulrich Drepper <drepper@redhat.com>. 2002.
@@ -79,20 +79,28 @@ AB(handle_file) (const char *fname, int fd)
/* Search for the PT_DYNAMIC entry. */
size_t cnt;
+ E(Phdr) *dynphdr = NULL;
for (cnt = 0; cnt < phnum; ++cnt)
if (SWAP (phdr[cnt].p_type) == PT_DYNAMIC)
- break;
-
- if (cnt == phnum)
+ dynphdr = &phdr[cnt];
+ else if (SWAP (phdr[cnt].p_type) == PT_LOAD
+ && (SWAP (phdr[cnt].p_flags) & (PF_X | PF_W)) == (PF_X | PF_W))
+ {
+ printf ("%s: segment %zu is executable and writable\n",
+ fname, cnt);
+ return 1;
+ }
+
+ if (dynphdr == NULL)
{
printf ("%s: no DYNAMIC segment found\n", fname);
return 1;
}
/* Read the dynamic segment. */
- size_t pmemsz = SWAP(phdr[cnt].p_memsz);
+ size_t pmemsz = SWAP(dynphdr->p_memsz);
E(Dyn) *dyn = alloca (pmemsz);
- if (pread (fd, dyn, pmemsz, SWAP(phdr[cnt].p_offset)) != pmemsz)
+ if (pread (fd, dyn, pmemsz, SWAP(dynphdr->p_offset)) != pmemsz)
goto read_error;
/* Search for an DT_TEXTREL entry of DT_FLAGS with the DF_TEXTREL
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
index a73e21db7b..fd3b5243fd 100644
--- a/elf/dl-deps.c
+++ b/elf/dl-deps.c
@@ -247,6 +247,7 @@ _dl_map_object_deps (struct link_map *map,
if (__builtin_expect (errstring != NULL, 0))
{
char *new_errstring = strdupa (errstring);
+ objname = strdupa (objname);
if (malloced)
free ((char *) errstring);
errstring = new_errstring;
@@ -337,6 +338,7 @@ _dl_map_object_deps (struct link_map *map,
if (__builtin_expect (errstring != NULL, 0))
{
char *new_errstring = strdupa (errstring);
+ objname = strdupa (objname);
if (malloced)
free ((char *) errstring);
errstring = new_errstring;