summaryrefslogtreecommitdiffstats
path: root/trunk/src
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/src')
-rw-r--r--trunk/src/Makefile.am34
-rw-r--r--trunk/src/Makefile.in500
-rw-r--r--trunk/src/arch-alpha.c497
-rw-r--r--trunk/src/arch-arm.c856
-rw-r--r--trunk/src/arch-cris.c404
-rw-r--r--trunk/src/arch-i386.c1087
-rw-r--r--trunk/src/arch-ia64.c525
-rw-r--r--trunk/src/arch-mips.c1031
-rw-r--r--trunk/src/arch-ppc.c1154
-rw-r--r--trunk/src/arch-ppc64.c856
-rw-r--r--trunk/src/arch-s390.c513
-rw-r--r--trunk/src/arch-s390x.c629
-rw-r--r--trunk/src/arch-sh.c445
-rw-r--r--trunk/src/arch-sparc.c637
-rw-r--r--trunk/src/arch-sparc64.c725
-rw-r--r--trunk/src/arch-x86_64.c536
-rw-r--r--trunk/src/cache.c856
-rw-r--r--trunk/src/canonicalize.c319
-rw-r--r--trunk/src/checksum.c89
-rw-r--r--trunk/src/conflict.c804
-rw-r--r--trunk/src/crc32.c87
-rw-r--r--trunk/src/cxx.c377
-rw-r--r--trunk/src/data.c339
-rw-r--r--trunk/src/doit.c270
-rw-r--r--trunk/src/dso-readonly.c1823
-rw-r--r--trunk/src/dso.c1797
-rw-r--r--trunk/src/dwarf2.c1148
-rw-r--r--trunk/src/dwarf2.h479
-rw-r--r--trunk/src/elf.h2558
-rw-r--r--trunk/src/exec.c1068
-rw-r--r--trunk/src/execle_open.c80
-rw-r--r--trunk/src/execstack.c459
-rw-r--r--trunk/src/fptr.c458
-rw-r--r--trunk/src/fptr.h67
-rw-r--r--trunk/src/gather.c1412
-rw-r--r--trunk/src/get.c673
-rw-r--r--trunk/src/hashtab.c609
-rw-r--r--trunk/src/hashtab.h155
-rw-r--r--trunk/src/layout.c647
-rw-r--r--trunk/src/layout.h35
-rw-r--r--trunk/src/ld-do-lookup.h225
-rw-r--r--trunk/src/ld-libs.c1371
-rw-r--r--trunk/src/ld-libs.h113
-rw-r--r--trunk/src/ld-lookup.c214
-rw-r--r--trunk/src/ld-lookup64.c24
-rw-r--r--trunk/src/main.c486
-rw-r--r--trunk/src/makecrc.c63
-rw-r--r--trunk/src/md5.c362
-rw-r--r--trunk/src/md5.h105
-rw-r--r--trunk/src/mdebug.c692
-rw-r--r--trunk/src/prelink.c972
-rw-r--r--trunk/src/prelink.h527
-rw-r--r--trunk/src/prelinktab.h27
-rw-r--r--trunk/src/reloc.c422
-rw-r--r--trunk/src/reloc.h44
-rw-r--r--trunk/src/sha.c331
-rw-r--r--trunk/src/sha.h69
-rw-r--r--trunk/src/space.c692
-rw-r--r--trunk/src/space.h39
-rw-r--r--trunk/src/stabs.c188
-rw-r--r--trunk/src/undo.c711
-rw-r--r--trunk/src/undoall.c173
-rw-r--r--trunk/src/verify.c442
-rw-r--r--trunk/src/wrap-file.c536
64 files changed, 35866 insertions, 0 deletions
diff --git a/trunk/src/Makefile.am b/trunk/src/Makefile.am
new file mode 100644
index 0000000..6981801
--- /dev/null
+++ b/trunk/src/Makefile.am
@@ -0,0 +1,34 @@
+## Process this file with automake to create Makefile.in
+
+AUTOMAKE_OPTIONS = 1.4 gnu
+
+DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -Wall
+AM_CFLAGS = -Wall
+AM_CPPFLAGS = -DSBINDIR='"@sbindir@"' -DBINDIR='"@bindir@"'
+INCLUDES = @GELFINCLUDE@
+
+sbin_PROGRAMS = prelink prelink-rtld
+bin_PROGRAMS = execstack
+
+arch_SOURCES = arch-i386.c arch-alpha.c arch-ppc.c arch-ppc64.c \
+ arch-sparc.c arch-sparc64.c arch-x86_64.c arch-mips.c \
+ arch-s390.c arch-s390x.c arch-arm.c arch-sh.c arch-ia64.c
+common_SOURCES = checksum.c data.c dso.c dwarf2.c dwarf2.h fptr.c fptr.h \
+ hashtab.c hashtab.h mdebug.c prelink.h stabs.c crc32.c \
+ wrap-file.c canonicalize.c
+prelink_SOURCES = cache.c conflict.c cxx.c doit.c exec.c execle_open.c get.c \
+ gather.c layout.c main.c prelink.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@ -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/trunk/src/Makefile.in b/trunk/src/Makefile.in
new file mode 100644
index 0000000..7dbe941
--- /dev/null
+++ b/trunk/src/Makefile.in
@@ -0,0 +1,500 @@
+# Makefile.in generated by automake 1.6.3 from Makefile.am.
+# @configure_input@
+
+# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
+# Free Software Foundation, Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+SHELL = @SHELL@
+
+srcdir = @srcdir@
+top_srcdir = @top_srcdir@
+VPATH = @srcdir@
+prefix = @prefix@
+exec_prefix = @exec_prefix@
+
+bindir = @bindir@
+sbindir = @sbindir@
+libexecdir = @libexecdir@
+datadir = @datadir@
+sysconfdir = @sysconfdir@
+sharedstatedir = @sharedstatedir@
+localstatedir = @localstatedir@
+libdir = @libdir@
+infodir = @infodir@
+mandir = @mandir@
+includedir = @includedir@
+oldincludedir = /usr/include
+pkgdatadir = $(datadir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+top_builddir = ..
+
+ACLOCAL = @ACLOCAL@
+AUTOCONF = @AUTOCONF@
+AUTOMAKE = @AUTOMAKE@
+AUTOHEADER = @AUTOHEADER@
+
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+INSTALL = @INSTALL@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_DATA = @INSTALL_DATA@
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = @program_transform_name@
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+host_alias = @host_alias@
+host_triplet = @host@
+
+EXEEXT = @EXEEXT@
+OBJEXT = @OBJEXT@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+AMTAR = @AMTAR@
+AR = @AR@
+AS = @AS@
+AWK = @AWK@
+CC = @CC@
+CPP = @CPP@
+CXX = @CXX@
+CXXCPP = @CXXCPP@
+DEPDIR = @DEPDIR@
+DLLTOOL = @DLLTOOL@
+ECHO = @ECHO@
+EGREP = @EGREP@
+F77 = @F77@
+GCJ = @GCJ@
+GCJFLAGS = @GCJFLAGS@
+GELFINCLUDE = @GELFINCLUDE@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+LIBGELF = @LIBGELF@
+LIBTOOL = @LIBTOOL@
+LN_S = @LN_S@
+OBJDUMP = @OBJDUMP@
+PACKAGE = @PACKAGE@
+RANLIB = @RANLIB@
+RC = @RC@
+STRIP = @STRIP@
+VERSION = @VERSION@
+am__include = @am__include@
+am__quote = @am__quote@
+install_sh = @install_sh@
+
+AUTOMAKE_OPTIONS = 1.4 gnu
+
+DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H -Wall
+AM_CFLAGS = -Wall
+AM_CPPFLAGS = -DSBINDIR='"@sbindir@"'
+INCLUDES = @GELFINCLUDE@
+
+sbin_PROGRAMS = prelink
+bin_PROGRAMS = execstack
+
+arch_SOURCES = arch-i386.c arch-alpha.c arch-ppc.c arch-ppc64.c \
+ arch-sparc.c arch-sparc64.c arch-x86_64.c arch-mips.c \
+ arch-s390.c arch-s390x.c arch-arm.c arch-sh.c arch-ia64.c
+
+common_SOURCES = checksum.c data.c dso.c dwarf2.c dwarf2.h fptr.c fptr.h \
+ hashtab.c hashtab.h mdebug.c prelink.h stabs.c crc32.c
+
+prelink_SOURCES = cache.c conflict.c cxx.c doit.c exec.c execle_open.c get.c \
+ gather.c layout.c main.c prelink.c \
+ prelinktab.h reloc.c reloc.h space.c undo.c undoall.c \
+ verify.c canonicalize.c md5.c md5.h sha.c sha.h \
+ $(common_SOURCES) $(arch_SOURCES)
+
+prelink_LDADD = @LIBGELF@
+prelink_LDFLAGS = -all-static
+
+execstack_SOURCES = execstack.c $(common_SOURCES) $(arch_SOURCES)
+
+extra_DIST = makecrc.c
+subdir = src
+mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
+CONFIG_HEADER = $(top_builddir)/config.h
+CONFIG_CLEAN_FILES =
+bin_PROGRAMS = execstack$(EXEEXT)
+sbin_PROGRAMS = prelink$(EXEEXT)
+PROGRAMS = $(bin_PROGRAMS) $(sbin_PROGRAMS)
+
+am__objects_1 = checksum.$(OBJEXT) data.$(OBJEXT) dso.$(OBJEXT) \
+ dwarf2.$(OBJEXT) fptr.$(OBJEXT) hashtab.$(OBJEXT) \
+ mdebug.$(OBJEXT) stabs.$(OBJEXT) crc32.$(OBJEXT)
+am__objects_2 = arch-i386.$(OBJEXT) arch-alpha.$(OBJEXT) \
+ arch-ppc.$(OBJEXT) arch-ppc64.$(OBJEXT) arch-sparc.$(OBJEXT) \
+ arch-sparc64.$(OBJEXT) arch-x86_64.$(OBJEXT) \
+ arch-mips.$(OBJEXT) arch-s390.$(OBJEXT) arch-s390x.$(OBJEXT) \
+ arch-arm.$(OBJEXT) arch-sh.$(OBJEXT) arch-ia64.$(OBJEXT)
+am_execstack_OBJECTS = execstack.$(OBJEXT) $(am__objects_1) \
+ $(am__objects_2)
+execstack_OBJECTS = $(am_execstack_OBJECTS)
+execstack_LDADD = $(LDADD)
+execstack_DEPENDENCIES =
+execstack_LDFLAGS =
+am_prelink_OBJECTS = cache.$(OBJEXT) conflict.$(OBJEXT) cxx.$(OBJEXT) \
+ doit.$(OBJEXT) exec.$(OBJEXT) execle_open.$(OBJEXT) \
+ get.$(OBJEXT) gather.$(OBJEXT) layout.$(OBJEXT) main.$(OBJEXT) \
+ prelink.$(OBJEXT) reloc.$(OBJEXT) space.$(OBJEXT) \
+ undo.$(OBJEXT) undoall.$(OBJEXT) verify.$(OBJEXT) \
+ canonicalize.$(OBJEXT) md5.$(OBJEXT) sha.$(OBJEXT) \
+ $(am__objects_1) $(am__objects_2)
+prelink_OBJECTS = $(am_prelink_OBJECTS)
+prelink_DEPENDENCIES =
+DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)
+CPPFLAGS = @CPPFLAGS@
+LDFLAGS = @LDFLAGS@
+LIBS = @LIBS@
+depcomp = $(SHELL) $(top_srcdir)/depcomp
+am__depfiles_maybe = depfiles
+@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/arch-alpha.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-arm.Po ./$(DEPDIR)/arch-i386.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-ia64.Po ./$(DEPDIR)/arch-mips.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-ppc.Po ./$(DEPDIR)/arch-ppc64.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-s390.Po ./$(DEPDIR)/arch-s390x.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-sh.Po ./$(DEPDIR)/arch-sparc.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-sparc64.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/arch-x86_64.Po ./$(DEPDIR)/cache.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/canonicalize.Po ./$(DEPDIR)/checksum.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/conflict.Po ./$(DEPDIR)/crc32.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/cxx.Po ./$(DEPDIR)/data.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/doit.Po ./$(DEPDIR)/dso.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/dwarf2.Po ./$(DEPDIR)/exec.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/execle_open.Po ./$(DEPDIR)/execstack.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/fptr.Po ./$(DEPDIR)/gather.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/get.Po ./$(DEPDIR)/hashtab.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/layout.Po ./$(DEPDIR)/main.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/md5.Po ./$(DEPDIR)/mdebug.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/prelink.Po ./$(DEPDIR)/reloc.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/sha.Po ./$(DEPDIR)/space.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/stabs.Po ./$(DEPDIR)/undo.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/undoall.Po ./$(DEPDIR)/verify.Po
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \
+ $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+CCLD = $(CC)
+LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+ $(AM_LDFLAGS) $(LDFLAGS) -o $@
+CFLAGS = @CFLAGS@
+DIST_SOURCES = $(execstack_SOURCES) $(prelink_SOURCES)
+DIST_COMMON = Makefile.am Makefile.in
+SOURCES = $(execstack_SOURCES) $(prelink_SOURCES)
+
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .lo .o .obj
+$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4)
+ cd $(top_srcdir) && \
+ $(AUTOMAKE) --gnu src/Makefile
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)
+binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
+install-binPROGRAMS: $(bin_PROGRAMS)
+ @$(NORMAL_INSTALL)
+ $(mkinstalldirs) $(DESTDIR)$(bindir)
+ @list='$(bin_PROGRAMS)'; for p in $$list; do \
+ p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
+ if test -f $$p \
+ || test -f $$p1 \
+ ; then \
+ f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \
+ echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f"; \
+ $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f; \
+ else :; fi; \
+ done
+
+uninstall-binPROGRAMS:
+ @$(NORMAL_UNINSTALL)
+ @list='$(bin_PROGRAMS)'; for p in $$list; do \
+ f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \
+ echo " rm -f $(DESTDIR)$(bindir)/$$f"; \
+ rm -f $(DESTDIR)$(bindir)/$$f; \
+ done
+
+clean-binPROGRAMS:
+ @list='$(bin_PROGRAMS)'; for p in $$list; do \
+ f=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
+ echo " rm -f $$p $$f"; \
+ rm -f $$p $$f ; \
+ done
+sbinPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
+install-sbinPROGRAMS: $(sbin_PROGRAMS)
+ @$(NORMAL_INSTALL)
+ $(mkinstalldirs) $(DESTDIR)$(sbindir)
+ @list='$(sbin_PROGRAMS)'; for p in $$list; do \
+ p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
+ if test -f $$p \
+ || test -f $$p1 \
+ ; then \
+ f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \
+ echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(sbinPROGRAMS_INSTALL) $$p $(DESTDIR)$(sbindir)/$$f"; \
+ $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(sbinPROGRAMS_INSTALL) $$p $(DESTDIR)$(sbindir)/$$f; \
+ else :; fi; \
+ done
+
+uninstall-sbinPROGRAMS:
+ @$(NORMAL_UNINSTALL)
+ @list='$(sbin_PROGRAMS)'; for p in $$list; do \
+ f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \
+ echo " rm -f $(DESTDIR)$(sbindir)/$$f"; \
+ rm -f $(DESTDIR)$(sbindir)/$$f; \
+ done
+
+clean-sbinPROGRAMS:
+ @list='$(sbin_PROGRAMS)'; for p in $$list; do \
+ f=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
+ echo " rm -f $$p $$f"; \
+ rm -f $$p $$f ; \
+ done
+execstack$(EXEEXT): $(execstack_OBJECTS) $(execstack_DEPENDENCIES)
+ @rm -f execstack$(EXEEXT)
+ $(LINK) $(execstack_LDFLAGS) $(execstack_OBJECTS) $(execstack_LDADD) $(LIBS)
+prelink$(EXEEXT): $(prelink_OBJECTS) $(prelink_DEPENDENCIES)
+ @rm -f prelink$(EXEEXT)
+ $(LINK) $(prelink_LDFLAGS) $(prelink_OBJECTS) $(prelink_LDADD) $(LIBS)
+
+mostlyclean-compile:
+ -rm -f *.$(OBJEXT) core *.core
+
+distclean-compile:
+ -rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-alpha.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-arm.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-i386.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-ia64.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-mips.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-ppc.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-ppc64.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-s390.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-s390x.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-sh.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-sparc.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-sparc64.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arch-x86_64.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cache.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/canonicalize.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/checksum.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/conflict.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/crc32.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cxx.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/data.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/doit.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dso.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwarf2.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/exec.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/execle_open.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/execstack.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fptr.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gather.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hashtab.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/layout.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/md5.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mdebug.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/prelink.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reloc.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sha.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/space.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stabs.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/undo.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/undoall.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/verify.Po@am__quote@
+
+distclean-depend:
+ -rm -rf ./$(DEPDIR)
+
+.c.o:
+@AMDEP_TRUE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(COMPILE) -c `test -f '$<' || echo '$(srcdir)/'`$<
+
+.c.obj:
+@AMDEP_TRUE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(COMPILE) -c `cygpath -w $<`
+
+.c.lo:
+@AMDEP_TRUE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/$*.Plo' tmpdepfile='$(DEPDIR)/$*.TPlo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(LTCOMPILE) -c -o $@ `test -f '$<' || echo '$(srcdir)/'`$<
+CCDEPMODE = @CCDEPMODE@
+
+mostlyclean-libtool:
+ -rm -f *.lo
+
+clean-libtool:
+ -rm -rf .libs _libs
+
+distclean-libtool:
+ -rm -f libtool
+uninstall-info-am:
+
+ETAGS = etags
+ETAGSFLAGS =
+
+tags: TAGS
+
+ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
+ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | \
+ $(AWK) ' { files[$$0] = 1; } \
+ END { for (i in files) print i; }'`; \
+ mkid -fID $$unique
+
+TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
+ $(TAGS_FILES) $(LISP)
+ tags=; \
+ here=`pwd`; \
+ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | \
+ $(AWK) ' { files[$$0] = 1; } \
+ END { for (i in files) print i; }'`; \
+ test -z "$(ETAGS_ARGS)$$tags$$unique" \
+ || $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ $$tags $$unique
+
+GTAGS:
+ here=`$(am__cd) $(top_builddir) && pwd` \
+ && cd $(top_srcdir) \
+ && gtags -i $(GTAGS_ARGS) $$here
+
+distclean-tags:
+ -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+
+top_distdir = ..
+distdir = $(top_distdir)/$(PACKAGE)-$(VERSION)
+
+distdir: $(DISTFILES)
+ @list='$(DISTFILES)'; for file in $$list; do \
+ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+ dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \
+ if test "$$dir" != "$$file" && test "$$dir" != "."; then \
+ dir="/$$dir"; \
+ $(mkinstalldirs) "$(distdir)$$dir"; \
+ else \
+ dir=''; \
+ fi; \
+ if test -d $$d/$$file; then \
+ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+ cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
+ fi; \
+ cp -pR $$d/$$file $(distdir)$$dir || exit 1; \
+ else \
+ test -f $(distdir)/$$file \
+ || cp -p $$d/$$file $(distdir)/$$file \
+ || exit 1; \
+ fi; \
+ done
+check-am: all-am
+check: check-am
+all-am: Makefile $(PROGRAMS)
+
+installdirs:
+ $(mkinstalldirs) $(DESTDIR)$(bindir) $(DESTDIR)$(sbindir)
+
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ INSTALL_STRIP_FLAG=-s \
+ `test -z '$(STRIP)' || \
+ echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
+mostlyclean-generic:
+
+clean-generic:
+
+distclean-generic:
+ -rm -f Makefile $(CONFIG_CLEAN_FILES)
+
+maintainer-clean-generic:
+ @echo "This command is intended for maintainers to use"
+ @echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-binPROGRAMS clean-generic clean-libtool \
+ clean-sbinPROGRAMS mostlyclean-am
+
+distclean: distclean-am
+
+distclean-am: clean-am distclean-compile distclean-depend \
+ distclean-generic distclean-libtool distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+info: info-am
+
+info-am:
+
+install-data-am:
+
+install-exec-am: install-binPROGRAMS install-sbinPROGRAMS
+
+install-info: install-info-am
+
+install-man:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic \
+ mostlyclean-libtool
+
+uninstall-am: uninstall-binPROGRAMS uninstall-info-am \
+ uninstall-sbinPROGRAMS
+
+.PHONY: GTAGS all all-am check check-am clean clean-binPROGRAMS \
+ clean-generic clean-libtool clean-sbinPROGRAMS distclean \
+ distclean-compile distclean-depend distclean-generic \
+ distclean-libtool distclean-tags distdir dvi dvi-am info \
+ info-am install install-am install-binPROGRAMS install-data \
+ install-data-am install-exec install-exec-am install-info \
+ install-info-am install-man install-sbinPROGRAMS install-strip \
+ installcheck installcheck-am installdirs maintainer-clean \
+ maintainer-clean-generic mostlyclean mostlyclean-compile \
+ mostlyclean-generic mostlyclean-libtool tags uninstall \
+ uninstall-am uninstall-binPROGRAMS uninstall-info-am \
+ uninstall-sbinPROGRAMS
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/trunk/src/arch-alpha.c b/trunk/src/arch-alpha.c
new file mode 100644
index 0000000..51182ed
--- /dev/null
+++ b/trunk/src/arch-alpha.c
@@ -0,0 +1,497 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+alpha_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ return 0;
+}
+
+static int
+alpha_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: Alpha doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+alpha_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (GELF_R_TYPE (rela->r_info) == R_ALPHA_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_ALPHA_JMP_SLOT)
+ {
+ GElf_Addr val = read_ule64 (dso, rela->r_offset);
+
+ if (val >= start)
+ {
+ write_le64 (dso, rela->r_offset, val + adjust);
+ if (val == rela->r_addend)
+ rela->r_addend += adjust;
+ }
+ }
+ else if (GELF_R_TYPE (rela->r_info) == R_ALPHA_GLOB_DAT)
+ {
+ GElf_Addr val = read_ule64 (dso, rela->r_offset) - rela->r_addend;
+
+ if (val && val >= start)
+ write_le64 (dso, rela->r_offset, val + adjust + rela->r_addend);
+ }
+ return 0;
+}
+
+static int
+alpha_prelink_rel (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Alpha doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static void
+alpha_fixup_plt (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr,
+ GElf_Addr value)
+{
+ Elf64_Sxword disp;
+ Elf64_Addr plt;
+
+ relaaddr -= dso->info[DT_JMPREL];
+ relaaddr /= sizeof (Elf64_Rela);
+ relaaddr *= 12;
+ plt = dso->info[DT_PLTGOT] + 32 + relaaddr;
+ disp = ((Elf64_Sxword) (value - plt - 12)) / 4;
+ if (disp >= -0x100000 && disp < 0x100000)
+ {
+ int32_t hi, lo;
+
+ hi = value - plt;
+ lo = (int16_t) hi;
+ hi = (hi - lo) >> 16;
+
+ /* ldah $27,hi($27)
+ lda $27,lo($27)
+ br $31,value */
+ write_le32 (dso, plt, 0x277b0000 | (hi & 0xffff));
+ write_le32 (dso, plt + 4, 0x237b0000 | (lo & 0xffff));
+ write_le32 (dso, plt + 8, 0xc3e00000 | (disp & 0x1fffff));
+ }
+ else
+ {
+ int32_t hi, lo;
+
+ hi = rela->r_offset - plt;
+ lo = (int16_t) hi;
+ hi = (hi - lo) >> 16;
+
+ /* ldah $27,hi($27)
+ ldq $27,lo($27)
+ jmp $31,($27) */
+ write_le32 (dso, plt, 0x277b0000 | (hi & 0xffff));
+ write_le32 (dso, plt + 4, 0xa77b0000 | (lo & 0xffff));
+ write_le32 (dso, plt + 8, 0x6bfb0000);
+ }
+}
+
+static int
+alpha_is_indirect_plt (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ Elf64_Addr pltaddr;
+ uint32_t plt[3];
+ int32_t hi, lo;
+
+ relaaddr -= dso->info[DT_JMPREL];
+ relaaddr /= sizeof (Elf64_Rela);
+ relaaddr *= 12;
+ pltaddr = dso->info[DT_PLTGOT] + 32 + relaaddr;
+ hi = rela->r_offset - pltaddr;
+ lo = (int16_t) hi;
+ hi = (hi - lo) >> 16;
+ plt[0] = read_ule32 (dso, pltaddr);
+ plt[1] = read_ule32 (dso, pltaddr + 4);
+ plt[2] = read_ule32 (dso, pltaddr + 8);
+ if (plt[0] == (0x277b0000 | (hi & 0xffff))
+ && plt[1] == (0xa77b0000 | (lo & 0xffff))
+ && plt[2] == 0x6bfb0000)
+ return 1;
+ return 0;
+}
+
+static int
+alpha_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_ALPHA_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_ALPHA_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ALPHA_GLOB_DAT:
+ case R_ALPHA_REFQUAD:
+ case R_ALPHA_DTPREL64:
+ write_le64 (dso, rela->r_offset, value);
+ break;
+ case R_ALPHA_JMP_SLOT:
+ write_le64 (dso, rela->r_offset, value);
+ alpha_fixup_plt (dso, rela, relaaddr, value);
+ break;
+ /* DTPMOD64 and TPREL64 is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_ALPHA_DTPMOD64:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_ALPHA_DTPMOD64 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_ALPHA_TPREL64:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_le64 (dso, rela->r_offset, value + info->resolvetls->offset);
+ break;
+ default:
+ error (0, 0, "%s: Unknown alpha relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+alpha_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info) & 0xff)
+ {
+ case R_ALPHA_GLOB_DAT:
+ case R_ALPHA_REFQUAD:
+ case R_ALPHA_JMP_SLOT:
+ buf_write_le64 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+alpha_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: Alpha doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+alpha_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ALPHA_NONE:
+ break;
+ case R_ALPHA_GLOB_DAT:
+ case R_ALPHA_REFQUAD:
+ case R_ALPHA_JMP_SLOT:
+ buf_write_le64 (buf, value + rela->r_addend);
+ break;
+ case R_ALPHA_RELATIVE:
+ error (0, 0, "%s: R_ALPHA_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+alpha_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Alpha doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+alpha_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_ALPHA_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_ALPHA_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD64 and TPREL64 relocs need conflicts. */
+ case R_ALPHA_DTPMOD64:
+ case R_ALPHA_TPREL64:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPREL64 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_ALPHA_DTPREL64
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ALPHA_GLOB_DAT:
+ case R_ALPHA_REFQUAD:
+ ret->r_addend = value + rela->r_addend;
+ break;
+ case R_ALPHA_JMP_SLOT:
+ ret->r_addend = value + rela->r_addend;
+ if (alpha_is_indirect_plt (dso, rela, relaaddr))
+ ret->r_info = GELF_R_INFO (0, R_ALPHA_GLOB_DAT);
+ else
+ {
+ relaaddr -= dso->info[DT_JMPREL];
+ relaaddr /= sizeof (Elf64_Rela);
+ if (relaaddr > 0xffffff)
+ {
+ error (0, 0, "%s: Cannot create R_ALPHA_JMP_SLOT conflict against .rel.plt with more than 16M entries",
+ dso->filename);
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, (relaaddr << 8) | R_ALPHA_JMP_SLOT);
+ }
+ break;
+ case R_ALPHA_DTPMOD64:
+ case R_ALPHA_DTPREL64:
+ case R_ALPHA_TPREL64:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_ALPHA_GLOB_DAT);
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ALPHA_DTPMOD64:
+ ret->r_addend = tls->modid;
+ break;
+ case R_ALPHA_DTPREL64:
+ ret->r_addend = value + rela->r_addend;
+ break;
+ case R_ALPHA_TPREL64:
+ ret->r_addend = value + rela->r_addend + tls->offset;
+ break;
+ }
+ break;
+ default:
+ error (0, 0, "%s: Unknown Alpha relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+alpha_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: Alpha doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+alpha_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+alpha_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+
+ /* Correct sh_entsize on .plt sections. */
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT] + 16);
+ assert (sec != -1);
+ if (dso->shdr[sec].sh_type == SHT_PROGBITS
+ && dso->shdr[sec].sh_entsize == 32)
+ dso->shdr[sec].sh_entsize = 0;
+ }
+ return 0;
+}
+
+static int
+alpha_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+ Elf_Scn *scn;
+ Elf_Data *data;
+ GElf_Sym sym;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ALPHA_NONE:
+ case R_ALPHA_RELATIVE:
+ break;
+ case R_ALPHA_JMP_SLOT:
+ relaaddr -= dso->info[DT_JMPREL];
+ relaaddr /= sizeof (Elf64_Rela);
+ relaaddr *= 12;
+ relaaddr += dso->info[DT_PLTGOT] + 32;
+ /* br at,.plt */
+ write_le32 (dso, relaaddr,
+ 0xc39fffff - (relaaddr - dso->info[DT_PLTGOT]) / 4);
+ write_le64 (dso, relaaddr + 4, 0);
+ write_le64 (dso, rela->r_offset, relaaddr);
+ break;
+ case R_ALPHA_GLOB_DAT:
+ /* This is ugly. Linker doesn't clear memory at r_offset of GLOB_DAT
+ reloc, but instead puts in sym.st_value + addend. */
+ sec = addr_to_sec (dso, relaaddr);
+ assert (sec != -1);
+ sec = dso->shdr[sec].sh_link;
+ assert (sec > 0 && sec < dso->ehdr.e_shnum);
+ scn = dso->scn[sec];
+ data = elf_getdata (scn, NULL);
+ assert (data != NULL && elf_getdata (scn, data) == NULL);
+ assert (GELF_R_SYM (rela->r_info)
+ <= dso->shdr[sec].sh_size / sizeof (Elf64_Sym));
+ gelfx_getsym (dso->elf, data, GELF_R_SYM (rela->r_info), &sym);
+ write_le64 (dso, rela->r_offset, sym.st_value + rela->r_addend);
+ break;
+ case R_ALPHA_REFQUAD:
+ case R_ALPHA_DTPMOD64:
+ case R_ALPHA_DTPREL64:
+ case R_ALPHA_TPREL64:
+ write_le64 (dso, rela->r_offset, 0);
+ break;
+ default:
+ error (0, 0, "%s: Unknown alpha relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+alpha_reloc_size (int reloc_type)
+{
+ return 8;
+}
+
+static int
+alpha_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_ALPHA_JMP_SLOT:
+ return RTYPE_CLASS_PLT;
+ case R_ALPHA_DTPMOD64:
+ case R_ALPHA_DTPREL64:
+ case R_ALPHA_TPREL64:
+ return RTYPE_CLASS_TLS;
+ default:
+ return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "Alpha",
+ .class = ELFCLASS64,
+ .machine = EM_ALPHA,
+ .alternate_machine = { EM_FAKE_ALPHA },
+ .R_JMP_SLOT = R_ALPHA_JMP_SLOT,
+ .R_COPY = -1,
+ .R_RELATIVE = R_ALPHA_RELATIVE,
+ .dynamic_linker = "/lib/ld-linux.so.2",
+ .adjust_dyn = alpha_adjust_dyn,
+ .adjust_rel = alpha_adjust_rel,
+ .adjust_rela = alpha_adjust_rela,
+ .prelink_rel = alpha_prelink_rel,
+ .prelink_rela = alpha_prelink_rela,
+ .prelink_conflict_rel = alpha_prelink_conflict_rel,
+ .prelink_conflict_rela = alpha_prelink_conflict_rela,
+ .apply_conflict_rela = alpha_apply_conflict_rela,
+ .apply_rel = alpha_apply_rel,
+ .apply_rela = alpha_apply_rela,
+ .rel_to_rela = alpha_rel_to_rela,
+ .need_rel_to_rela = alpha_need_rel_to_rela,
+ .reloc_size = alpha_reloc_size,
+ .reloc_class = alpha_reloc_class,
+ .max_reloc_size = 8,
+ .arch_prelink = alpha_arch_prelink,
+ .undo_prelink_rela = alpha_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x0000020000000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x0000020001000000LL,
+ .mmap_end = 0x0000020100000000LL,
+ .max_page_size = 0x10000,
+ .page_size = 0x02000
+};
diff --git a/trunk/src/arch-arm.c b/trunk/src/arch-arm.c
new file mode 100644
index 0000000..12b8f3d
--- /dev/null
+++ b/trunk/src/arch-arm.c
@@ -0,0 +1,856 @@
+/* Copyright (C) 2001, 2002, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+#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
+
+static int
+arm_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_une32 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_ne32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_une32 (dso, dyn->d_un.d_ptr + 4);
+ /* If .got.plt[1] points to .plt, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_ne32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+arm_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_RELATIVE:
+ case R_ARM_JUMP_SLOT:
+ data = read_une32 (dso, rel->r_offset);
+ if (data >= start)
+ write_ne32 (dso, rel->r_offset, data + adjust);
+ break;
+ }
+ return 0;
+}
+
+static int
+arm_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_RELATIVE:
+ if ((Elf32_Addr) rela->r_addend >= start)
+ {
+ rela->r_addend += (Elf32_Sword) adjust;
+ /* Write it to the memory location as well.
+ Not necessary, but we can do it. */
+ write_ne32 (dso, rela->r_offset, rela->r_addend);
+ }
+ break;
+ case R_ARM_JUMP_SLOT:
+ data = read_une32 (dso, rela->r_offset);
+ if (data >= start)
+ write_ne32 (dso, rela->r_offset, data + adjust);
+ break;
+ break;
+ }
+ return 0;
+}
+
+static int
+arm_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rel->r_info) == R_ARM_RELATIVE
+ || GELF_R_TYPE (rel->r_info) == R_ARM_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ write_ne32 (dso, rel->r_offset, value);
+ break;
+ case R_ARM_ABS32:
+ {
+ if (read_une32 (dso, rel->r_offset))
+ {
+ error (0, 0, "%s: R_ARM_ABS32 relocs with non-zero addend should not be present in prelinked REL sections",
+ dso->filename);
+ return 1;
+ }
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_ARM_GLOB_DAT);
+ write_ne32 (dso, rel->r_offset, value);
+ /* Tell prelink_rel routine *rel has changed. */
+ return 2;
+ }
+ case R_ARM_PC24:
+ error (0, 0, "%s: R_ARM_PC24 relocs with non-zero addend should not be present in prelinked REL sections",
+ dso->filename);
+ return 1;
+ case R_ARM_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
+ return 1;
+ case R_ARM_TLS_DTPOFF32:
+ write_ne32 (dso, rel->r_offset, value);
+ break;
+ /* DTPMOD32 and TPOFF32 is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_ARM_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_ARM_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_ARM_TLS_TPOFF32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ error (0, 0, "%s: R_ARM_TLS_TPOFF32 relocs should not be present in "
+ "prelinked ET_EXEC REL sections",
+ dso->filename);
+ break;
+ default:
+ error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+ Elf32_Sword val;
+
+ if (GELF_R_TYPE (rela->r_info) == R_ARM_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_ARM_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ write_ne32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_ARM_ABS32:
+ write_ne32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_ARM_PC24:
+ val = value + rela->r_addend - rela->r_offset;
+ val >>= 2;
+ if ((Elf32_Word) val + 0x800000 >= 0x1000000)
+ {
+ error (0, 0, "%s: R_ARM_PC24 overflow", dso->filename);
+ return 1;
+ }
+ val &= 0xffffff;
+ write_ne32 (dso, rela->r_offset,
+ (read_une32 (dso, rela->r_offset) & 0xff000000) | val);
+ break;
+ case R_ARM_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
+ return 1;
+ case R_ARM_TLS_DTPOFF32:
+ write_ne32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ /* DTPMOD32 and TPOFF32 is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_ARM_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_ARM_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_ARM_TLS_TPOFF32:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_ne32 (dso, rela->r_offset,
+ value + rela->r_addend + info->resolvetls->offset);
+ break;
+ default:
+ error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ case R_ARM_ABS32:
+ buf_write_ne32 (info->dso, buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+arm_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ GElf_Addr value;
+ Elf32_Sword val;
+
+ value = info->resolve (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_NONE:
+ break;
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ buf_write_ne32 (info->dso, buf, value);
+ break;
+ case R_ARM_ABS32:
+ buf_write_ne32 (info->dso, buf, value + read_une32 (info->dso, rel->r_offset));
+ break;
+ case R_ARM_PC24:
+ val = value + rel->r_offset;
+ value = read_une32 (info->dso, rel->r_offset) << 8;
+ value = ((Elf32_Sword) value) >> 6;
+ val += value;
+ val >>= 2;
+ if ((Elf32_Word) val + 0x800000 >= 0x1000000)
+ {
+ error (0, 0, "%s: R_ARM_PC24 overflow", info->dso->filename);
+ return 1;
+ }
+ val &= 0xffffff;
+ buf_write_ne32 (info->dso, buf, (buf_read_une32 (info->dso, buf) & 0xff000000) | val);
+ break;
+ case R_ARM_COPY:
+ abort ();
+ case R_ARM_RELATIVE:
+ error (0, 0, "%s: R_ARM_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+ Elf32_Sword val;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_NONE:
+ break;
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ case R_ARM_ABS32:
+ buf_write_ne32 (info->dso, buf, value + rela->r_addend);
+ break;
+ case R_ARM_PC24:
+ val = value + rela->r_addend - rela->r_offset;
+ val >>= 2;
+ if ((Elf32_Word) val + 0x800000 >= 0x1000000)
+ {
+ error (0, 0, "%s: R_ARM_PC24 overflow", info->dso->filename);
+ return 1;
+ }
+ val &= 0xffffff;
+ buf_write_ne32 (info->dso, buf, (buf_read_une32 (info->dso, buf) & 0xff000000) | val);
+ break;
+ case R_ARM_COPY:
+ abort ();
+ case R_ARM_RELATIVE:
+ error (0, 0, "%s: R_ARM_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rel->r_info) == R_ARM_RELATIVE
+ || GELF_R_TYPE (rel->r_info) == R_ARM_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_TPOFF32:
+ break;
+
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF32 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rel->r_info) == R_ARM_TLS_DTPOFF32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rel->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ ret->r_addend = (Elf32_Sword) value;
+ break;
+ case R_ARM_ABS32:
+ case R_ARM_PC24:
+ error (0, 0, "%s: R_ARM_%s relocs should not be present in prelinked REL sections",
+ dso->filename, GELF_R_TYPE (rel->r_info) == R_ARM_ABS32 ? "ABS32" : "PC24");
+ return 1;
+ case R_ARM_COPY:
+ error (0, 0, "R_ARM_COPY should not be present in shared libraries");
+ return 1;
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ case R_ARM_TLS_TPOFF32:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_ARM_ABS32);
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_TLS_DTPMOD32:
+ ret->r_addend = tls->modid;
+ break;
+ case R_ARM_TLS_DTPOFF32:
+ ret->r_addend = value;
+ break;
+ case R_ARM_TLS_TPOFF32:
+ ret->r_addend = (value + read_une32 (dso, rel->r_offset)
+ + tls->offset);
+ break;
+ }
+ break;
+ default:
+ error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+ Elf32_Sword val;
+
+ if (GELF_R_TYPE (rela->r_info) == R_ARM_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_ARM_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_TPOFF32:
+ break;
+
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF32 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_ARM_TLS_DTPOFF32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_GLOB_DAT:
+ case R_ARM_JUMP_SLOT:
+ case R_ARM_ABS32:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
+ break;
+ case R_ARM_PC24:
+ val = value + rela->r_addend - rela->r_offset;
+ val >>= 2;
+ if ((Elf32_Word) val + 0x800000 >= 0x1000000)
+ {
+ error (0, 0, "%s: R_ARM_PC24 overflow", dso->filename);
+ return 1;
+ }
+ value = read_une32 (dso, rela->r_offset) & 0xff000000;
+ ret->r_addend = (Elf32_Sword) (value | (val & 0xffffff));
+ ret->r_info = GELF_R_INFO (0, R_ARM_ABS32);
+ break;
+ case R_ARM_COPY:
+ error (0, 0, "R_ARM_COPY should not be present in shared libraries");
+ return 1;
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ case R_ARM_TLS_TPOFF32:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_ARM_ABS32);
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_ARM_TLS_DTPMOD32:
+ ret->r_addend = tls->modid;
+ break;
+ case R_ARM_TLS_DTPOFF32:
+ ret->r_addend = value;
+ break;
+ case R_ARM_TLS_TPOFF32:
+ ret->r_addend = value + rela->r_offset + tls->offset;
+ break;
+ }
+ break;
+ default:
+ error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ rela->r_offset = rel->r_offset;
+ rela->r_info = rel->r_info;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_JUMP_SLOT:
+ /* We should be never converting .rel.plt into .rela.plt. */
+ abort ();
+ case R_ARM_RELATIVE:
+ case R_ARM_ABS32:
+ case R_ARM_TLS_TPOFF32:
+ rela->r_addend = (Elf32_Sword) read_une32 (dso, rel->r_offset);
+ break;
+ case R_ARM_PC24:
+ rela->r_addend = read_une32 (dso, rel->r_offset) << 8;
+ rela->r_addend = ((Elf32_Sword) rela->r_addend) >> 6;
+ break;
+ case R_ARM_COPY:
+ case R_ARM_GLOB_DAT:
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ rela->r_addend = 0;
+ break;
+ }
+ return 0;
+}
+
+static int
+arm_rela_to_rel (DSO *dso, GElf_Rela *rela, GElf_Rel *rel)
+{
+ rel->r_offset = rela->r_offset;
+ rel->r_info = rela->r_info;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_JUMP_SLOT:
+ /* We should be never converting .rel.plt into .rela.plt
+ and thus never .rela.plt back to .rel.plt. */
+ abort ();
+ case R_ARM_RELATIVE:
+ case R_ARM_ABS32:
+ case R_ARM_TLS_TPOFF32:
+ write_ne32 (dso, rela->r_offset, rela->r_addend);
+ break;
+ case R_ARM_PC24:
+ write_ne32 (dso, rela->r_offset,
+ (read_une32 (dso, rela->r_offset) & 0xff000000)
+ | ((rela->r_addend >> 2) & 0xffffff));
+ break;
+ case R_ARM_GLOB_DAT:
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ write_ne32 (dso, rela->r_offset, 0);
+ break;
+ }
+ return 0;
+}
+
+static int
+arm_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ Elf_Data *data;
+ Elf_Scn *scn;
+ Elf32_Rel *rel, *relend;
+ unsigned int val;
+
+ while (first <= last)
+ {
+ data = NULL;
+ scn = dso->scn[first++];
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ rel = (Elf32_Rel *) data->d_buf;
+ relend = rel + data->d_size / sizeof (Elf32_Rel);
+ for (; rel < relend; rel++)
+ switch (ELF32_R_TYPE (rel->r_info))
+ {
+ case R_ARM_ABS32:
+ val = read_une32 (dso, rel->r_offset);
+ /* R_ARM_ABS32 with addend 0 can be converted
+ to R_ARM_GLOB_DAT and we don't have to convert
+ to RELA because of that. */
+ if (val == 0)
+ break;
+ /* FALLTHROUGH */
+ case R_ARM_PC24:
+ return 1;
+ case R_ARM_TLS_TPOFF32:
+ /* In shared libraries TPOFF is changed always into
+ conflicts, for executables we need to preserve
+ original addend. */
+ if (dso->ehdr.e_type == ET_EXEC)
+ return 1;
+
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+arm_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt into got[1].
+ .plt is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = dso->shdr[i].sh_addr;
+ write_ne32 (dso, dso->info[DT_PLTGOT] + 4, data);
+ }
+
+ return 0;
+}
+
+static int
+arm_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_une32 (dso, dso->info[DT_PLTGOT] + 4);
+ if (data == dso->shdr[i].sh_addr)
+ write_ne32 (dso, dso->info[DT_PLTGOT] + 4, 0);
+ }
+
+ return 0;
+}
+
+static int
+arm_undo_prelink_rel (DSO *dso, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_ARM_RELATIVE:
+ case R_ARM_NONE:
+ break;
+ case R_ARM_JUMP_SLOT:
+ sec = addr_to_sec (dso, rel->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_ARM_JMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf32_Addr data = read_une32 (dso, dso->shdr[sec].sh_addr + 4);
+
+ assert (rel->r_offset >= dso->shdr[sec].sh_addr + 12);
+ assert (((rel->r_offset - dso->shdr[sec].sh_addr) & 3) == 0);
+ write_ne32 (dso, rel->r_offset, data);
+ }
+ break;
+ case R_ARM_GLOB_DAT:
+ sec = addr_to_sec (dso, rel->r_offset);
+
+ write_ne32 (dso, rel->r_offset, 0);
+ if (sec != -1)
+ {
+ if (strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[sec].sh_name),
+ ".got"))
+ {
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_ARM_ABS32);
+ return 2;
+ }
+ }
+ break;
+ case R_ARM_ABS32:
+ case R_ARM_PC24:
+ error (0, 0, "%s: R_ARM_%s relocs should not be present in prelinked REL sections",
+ GELF_R_TYPE (rel->r_info) == R_ARM_ABS32 ? "ABS32" : "PC24",
+ dso->filename);
+ return 1;
+ case R_ARM_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_ARM_COPY reloc in shared library?", dso->filename);
+ return 1;
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ write_ne32 (dso, rel->r_offset, 0);
+ break;
+ case R_ARM_TLS_TPOFF32:
+ break;
+ default:
+ error (0, 0, "%s: Unknown arm relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+arm_reloc_size (int reloc_type)
+{
+ assert (reloc_type != R_ARM_COPY);
+ return 4;
+}
+
+static int
+arm_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_ARM_COPY: return RTYPE_CLASS_COPY;
+ case R_ARM_JUMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_ARM_TLS_DTPMOD32:
+ case R_ARM_TLS_DTPOFF32:
+ case R_ARM_TLS_TPOFF32:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "ARM",
+ .class = ELFCLASS32,
+ .machine = EM_ARM,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_ARM_JUMP_SLOT,
+ .R_COPY = R_ARM_COPY,
+ .R_RELATIVE = R_ARM_RELATIVE,
+ .dynamic_linker = "/lib/ld-linux.so.2",
+ .dynamic_linker_alt = "/lib/ld-linux.so.3",
+ .adjust_dyn = arm_adjust_dyn,
+ .adjust_rel = arm_adjust_rel,
+ .adjust_rela = arm_adjust_rela,
+ .prelink_rel = arm_prelink_rel,
+ .prelink_rela = arm_prelink_rela,
+ .prelink_conflict_rel = arm_prelink_conflict_rel,
+ .prelink_conflict_rela = arm_prelink_conflict_rela,
+ .apply_conflict_rela = arm_apply_conflict_rela,
+ .apply_rel = arm_apply_rel,
+ .apply_rela = arm_apply_rela,
+ .rel_to_rela = arm_rel_to_rela,
+ .rela_to_rel = arm_rela_to_rel,
+ .need_rel_to_rela = arm_need_rel_to_rela,
+ .reloc_size = arm_reloc_size,
+ .reloc_class = arm_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = arm_arch_prelink,
+ .arch_undo_prelink = arm_arch_undo_prelink,
+ .undo_prelink_rel = arm_undo_prelink_rel,
+ /* Although TASK_UNMAPPED_BASE is 0x40000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x41000000,
+ .mmap_end = 0x50000000,
+ .max_page_size = 0x8000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-cris.c b/trunk/src/arch-cris.c
new file mode 100644
index 0000000..6b56016
--- /dev/null
+++ b/trunk/src/arch-cris.c
@@ -0,0 +1,404 @@
+/* Copyright (C) 2001, 2002, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+cris_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_ule32 (dso, dyn->d_un.d_ptr);
+ /* If .got[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_le32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ule32 (dso, dyn->d_un.d_ptr + 4);
+ /* If .got[1] points to .plt + 28, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 28
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_le32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+cris_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: CRIS doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+cris_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_CRIS_RELATIVE:
+ if ((Elf32_Addr) rela->r_addend >= start)
+ rela->r_addend += (Elf32_Sword) adjust;
+ break;
+ case R_CRIS_JUMP_SLOT:
+ data = read_ule32 (dso, rela->r_offset);
+ if (data >= start)
+ write_le32 (dso, rela->r_offset, data + adjust);
+ break;
+ break;
+ }
+ return 0;
+}
+
+static int
+cris_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: CRIS doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+cris_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ dso = info->dso;
+ if (GELF_R_TYPE (rela->r_info) == R_CRIS_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_CRIS_RELATIVE)
+ {
+ write_le32 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_CRIS_GLOB_DAT:
+ case R_CRIS_JUMP_SLOT:
+ case R_CRIS_32:
+ write_le32 (dso, rela->r_offset, value);
+ break;
+ case R_CRIS_16:
+ write_le16 (dso, rela->r_offset, value);
+ break;
+ case R_CRIS_8:
+ write_8 (dso, rela->r_offset, value);
+ break;
+ case R_CRIS_32_PCREL:
+ write_le32 (dso, rela->r_offset, value - rela->r_offset - 4);
+ break;
+ case R_CRIS_16_PCREL:
+ write_le16 (dso, rela->r_offset, value - rela->r_offset - 2);
+ break;
+ case R_CRIS_8_PCREL:
+ write_8 (dso, rela->r_offset, value - rela->r_offset - 1);
+ break;
+ case R_CRIS_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_CRIS_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown cris relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+cris_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_CRIS_GLOB_DAT:
+ case R_CRIS_JUMP_SLOT:
+ case R_CRIS_32:
+ buf_write_le32 (buf, rela->r_addend);
+ break;
+ case R_CRIS_16:
+ buf_write_le16 (buf, rela->r_addend);
+ break;
+ case R_CRIS_8:
+ buf_write_8 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+cris_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: CRIS doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+cris_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_CRIS_NONE:
+ break;
+ case R_CRIS_GLOB_DAT:
+ case R_CRIS_JUMP_SLOT:
+ case R_CRIS_32:
+ buf_write_le32 (buf, value);
+ break;
+ case R_CRIS_16:
+ buf_write_le16 (buf, value);
+ break;
+ case R_CRIS_8:
+ buf_write_8 (buf, value);
+ break;
+ case R_CRIS_32_PCREL:
+ buf_write_le32 (buf, value - rela->r_offset - 4);
+ break;
+ case R_CRIS_16_PCREL:
+ buf_write_le16 (buf, value - rela->r_offset - 2);
+ break;
+ case R_CRIS_8:
+ buf_write_8 (buf, value - rela->r_offset - 1);
+ break;
+ case R_CRIS_COPY:
+ abort ();
+ case R_CRIS_RELATIVE:
+ error (0, 0, "%s: R_CRIS_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+cris_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: CRIS doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+cris_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_CRIS_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_CRIS_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ return 0;
+ value = conflict_lookup_value (conflict);
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_CRIS_GLOB_DAT:
+ case R_CRIS_JUMP_SLOT:
+ case R_CRIS_32:
+ case R_CRIS_16:
+ case R_CRIS_8:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
+ break;
+ case R_CRIS_32_PCREL:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend
+ - rela->r_offset - 4);
+ ret->r_info = GELF_R_INFO (0, R_CRIS_32);
+ break;
+ case R_CRIS_16_PCREL:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend
+ - rela->r_offset - 2);
+ ret->r_info = GELF_R_INFO (0, R_CRIS_16);
+ break;
+ case R_CRIS_8_PCREL:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend
+ - rela->r_offset - 1);
+ ret->r_info = GELF_R_INFO (0, R_CRIS_8);
+ break;
+ case R_CRIS_COPY:
+ error (0, 0, "R_CRIS_COPY should not be present in shared libraries");
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown cris relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+cris_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ return 0;
+}
+
+static int
+cris_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+cris_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 28 into got[1].
+ .plt + 28 is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ assert (i < dso->ehdr.e_shnum);
+ data = dso->shdr[i].sh_addr + 28;
+ write_le32 (dso, dso->info[DT_PLTGOT] + 4, data);
+ }
+
+ return 0;
+}
+
+static int
+cris_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_CRIS_16:
+ case R_CRIS_16_PCREL:
+ return 2;
+ case R_CRIS_8:
+ case R_CRIS_8_PCREL:
+ return 1;
+ default:
+ return 4;
+ }
+}
+
+static int
+cris_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_CRIS_COPY: return RTYPE_CLASS_COPY;
+ case R_CRIS_JUMP_SLOT: return RTYPE_CLASS_PLT;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "CRIS",
+ .class = ELFCLASS32,
+ .machine = EM_CRIS,
+ .alternate_machine = { EM_NONE },
+ .R_JUMP_SLOT = R_CRIS_JUMP_SLOT,
+ .R_COPY = R_CRIS_COPY,
+ .R_RELATIVE = R_CRIS_RELATIVE,
+ .dynamic_linker = "/lib/ld.so.1",
+ .adjust_dyn = cris_adjust_dyn,
+ .adjust_rel = cris_adjust_rel,
+ .adjust_rela = cris_adjust_rela,
+ .prelink_rel = cris_prelink_rel,
+ .prelink_rela = cris_prelink_rela,
+ .prelink_conflict_rel = cris_prelink_conflict_rel,
+ .prelink_conflict_rela = cris_prelink_conflict_rela,
+ .apply_conflict_rela = cris_apply_conflict_rela,
+ .apply_rel = cris_apply_rel,
+ .apply_rela = cris_apply_rela,
+ .rel_to_rela = cris_rel_to_rela,
+ .need_rel_to_rela = cris_need_rel_to_rela,
+ .reloc_size = cris_reloc_size,
+ .reloc_class = cris_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = cris_arch_prelink,
+ /* Although TASK_UNMAPPED_BASE is 0x3aaaa000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x3c000000,
+ .mmap_end = 0x48000000,
+ .max_page_size = 0x2000,
+ .page_size = 0x2000
+};
diff --git a/trunk/src/arch-i386.c b/trunk/src/arch-i386.c
new file mode 100644
index 0000000..d5211b2
--- /dev/null
+++ b/trunk/src/arch-i386.c
@@ -0,0 +1,1087 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+#include "layout.h"
+
+static int
+i386_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_ule32 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_le32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ule32 (dso, dyn->d_un.d_ptr + 4);
+ /* If .got.plt[1] points to .plt + 0x16, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 0x16
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_le32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+i386_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_RELATIVE:
+ case R_386_JMP_SLOT:
+ data = read_ule32 (dso, rel->r_offset);
+ if (data >= start)
+ write_le32 (dso, rel->r_offset, data + adjust);
+ break;
+ }
+ return 0;
+}
+
+static int
+i386_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_RELATIVE:
+ if ((Elf32_Addr) rela->r_addend >= start)
+ {
+ rela->r_addend += (Elf32_Sword) adjust;
+ /* Write it to the memory location as well.
+ Not necessary, but we can do it. */
+ write_le32 (dso, rela->r_offset, rela->r_addend);
+ }
+ break;
+ case R_386_JMP_SLOT:
+ data = read_ule32 (dso, rela->r_offset);
+ if (data >= start)
+ write_le32 (dso, rela->r_offset, data + adjust);
+ break;
+ break;
+ }
+ return 0;
+}
+
+static int
+i386_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rel->r_info) == R_386_RELATIVE
+ || GELF_R_TYPE (rel->r_info) == R_386_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ write_le32 (dso, rel->r_offset, value);
+ break;
+ case R_386_32:
+ {
+ if (read_ule32 (dso, rel->r_offset))
+ {
+ error (0, 0, "%s: R_386_32 relocs with non-zero addend should not be present in prelinked REL sections",
+ dso->filename);
+ return 1;
+ }
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_386_GLOB_DAT);
+ write_le32 (dso, rel->r_offset, value);
+ /* Tell prelink_rel routine *rel has changed. */
+ return 2;
+ }
+ case R_386_PC32:
+ error (0, 0, "%s: R_386_PC32 relocs should not be present in prelinked REL sections",
+ dso->filename);
+ return 1;
+ case R_386_TLS_DTPOFF32:
+ write_le32 (dso, rel->r_offset, value);
+ break;
+ /* DTPMOD32 and TPOFF{32,} is impossible to predict unless prelink
+ sets the rules. Also for TPOFF{32,} there is REL->RELA problem. */
+ case R_386_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_386_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ if (dso->ehdr.e_type == ET_EXEC)
+ error (0, 0, "%s: R_386_TLS_TPOFF relocs should not be present in prelinked ET_EXEC REL sections",
+ dso->filename);
+ break;
+ case R_386_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_386_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown i386 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_386_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_386_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_386_32:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_386_PC32:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_386_TLS_DTPOFF32:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ /* DTPMOD32 and TPOFF{32,} is impossible to predict unless prelink
+ sets the rules. */
+ case R_386_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_386_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_386_TLS_TPOFF32:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_le32 (dso, rela->r_offset,
+ -(value + rela->r_addend - info->resolvetls->offset));
+ break;
+ case R_386_TLS_TPOFF:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_le32 (dso, rela->r_offset,
+ value + rela->r_addend - info->resolvetls->offset);
+ break;
+ case R_386_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_386_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown i386 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ case R_386_32:
+ buf_write_le32 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+i386_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_NONE:
+ break;
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ buf_write_le32 (buf, value);
+ break;
+ case R_386_32:
+ buf_write_le32 (buf, value + read_ule32 (info->dso, rel->r_offset));
+ break;
+ case R_386_PC32:
+ buf_write_le32 (buf, value + read_ule32 (info->dso, rel->r_offset)
+ - rel->r_offset);
+ break;
+ case R_386_COPY:
+ abort ();
+ case R_386_RELATIVE:
+ error (0, 0, "%s: R_386_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_NONE:
+ break;
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ case R_386_32:
+ buf_write_le32 (buf, value + rela->r_addend);
+ break;
+ case R_386_PC32:
+ buf_write_le32 (buf, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_386_COPY:
+ abort ();
+ case R_386_RELATIVE:
+ error (0, 0, "%s: R_386_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rel->r_info) == R_386_RELATIVE
+ || GELF_R_TYPE (rel->r_info) == R_386_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF32 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rel->r_info) == R_386_TLS_DTPOFF32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rel->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rel->r_info));
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_GLOB_DAT:
+ ret->r_info = GELF_R_INFO (0, R_386_32);
+ /* FALLTHROUGH */
+ case R_386_JMP_SLOT:
+ ret->r_addend = (Elf32_Sword) value;
+ break;
+ case R_386_32:
+ case R_386_PC32:
+ error (0, 0, "%s: R_386_%s32 relocs should not be present in prelinked REL sections",
+ dso->filename, GELF_R_TYPE (rel->r_info) == R_386_32 ? "" : "PC");
+ return 1;
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_DTPOFF32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: R_386_TLS not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_386_32);
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_TLS_DTPMOD32:
+ ret->r_addend = tls->modid;
+ break;
+ case R_386_TLS_DTPOFF32:
+ ret->r_addend = value;
+ break;
+ case R_386_TLS_TPOFF32:
+ ret->r_addend = -(value + read_ule32 (dso, rel->r_offset)
+ - tls->offset);
+ break;
+ case R_386_TLS_TPOFF:
+ ret->r_addend = value + read_ule32 (dso, rel->r_offset)
+ - tls->offset;
+ }
+ break;
+ case R_386_COPY:
+ error (0, 0, "R_386_COPY should not be present in shared libraries");
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown i386 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_386_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_386_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF32 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_386_TLS_DTPOFF32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_GLOB_DAT:
+ ret->r_info = GELF_R_INFO (0, R_386_32);
+ /* FALLTHROUGH */
+ case R_386_JMP_SLOT:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
+ break;
+ case R_386_32:
+ value += rela->r_addend;
+ ret->r_addend = (Elf32_Sword) value;
+ break;
+ case R_386_PC32:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend - rela->r_offset);
+ ret->r_info = GELF_R_INFO (0, R_386_32);
+ break;
+ case R_386_COPY:
+ error (0, 0, "R_386_COPY should not be present in shared libraries");
+ return 1;
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_DTPOFF32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: R_386_TLS not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_386_32);
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_386_TLS_DTPMOD32:
+ ret->r_addend = tls->modid;
+ break;
+ case R_386_TLS_DTPOFF32:
+ ret->r_addend += value;
+ break;
+ case R_386_TLS_TPOFF32:
+ ret->r_addend = -(value + rela->r_addend - tls->offset);
+ break;
+ case R_386_TLS_TPOFF:
+ ret->r_addend = value + rela->r_addend - tls->offset;
+ break;
+ }
+ break;
+ default:
+ error (0, 0, "%s: Unknown i386 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ rela->r_offset = rel->r_offset;
+ rela->r_info = rel->r_info;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_JMP_SLOT:
+ /* We should be never converting .rel.plt into .rela.plt. */
+ abort ();
+ case R_386_RELATIVE:
+ case R_386_32:
+ case R_386_PC32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ rela->r_addend = (Elf32_Sword) read_ule32 (dso, rel->r_offset);
+ break;
+ case R_386_COPY:
+ case R_386_GLOB_DAT:
+ case R_386_TLS_DTPOFF32:
+ case R_386_TLS_DTPMOD32:
+ rela->r_addend = 0;
+ break;
+ }
+ return 0;
+}
+
+static int
+i386_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ Elf_Data *data;
+ Elf_Scn *scn;
+ Elf32_Rel *rel, *relend;
+ unsigned int val;
+
+ while (first <= last)
+ {
+ data = NULL;
+ scn = dso->scn[first++];
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ rel = (Elf32_Rel *) data->d_buf;
+ relend = rel + data->d_size / sizeof (Elf32_Rel);
+ for (; rel < relend; rel++)
+ switch (ELF32_R_TYPE (rel->r_info))
+ {
+ case R_386_32:
+ val = read_ule32 (dso, rel->r_offset);
+ /* R_386_32 with addend 0 can be converted
+ to R_386_GLOB_DAT and we don't have to convert
+ to RELA because of that. */
+ if (val == 0)
+ break;
+ /* FALLTHROUGH */
+ case R_386_PC32:
+ return 1;
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ /* In shared libraries TPOFF is changed always into
+ conflicts, for executables we need to preserve
+ original addend. */
+ if (dso->ehdr.e_type == ET_EXEC)
+ return 1;
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+i386_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 0x16 into got[1].
+ .plt + 0x16 is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = dso->shdr[i].sh_addr + 0x16;
+ write_le32 (dso, dso->info[DT_PLTGOT] + 4, data);
+ }
+
+ return 0;
+}
+
+static int
+i386_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt + 0x16. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_ule32 (dso, dso->info[DT_PLTGOT] + 4);
+ if (data == dso->shdr[i].sh_addr + 0x16)
+ write_le32 (dso, dso->info[DT_PLTGOT] + 4, 0);
+ }
+
+ return 0;
+}
+
+static int
+i386_undo_prelink_rel (DSO *dso, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_NONE:
+ case R_386_RELATIVE:
+ break;
+ case R_386_JMP_SLOT:
+ sec = addr_to_sec (dso, rel->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_386_JMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf32_Addr data = read_ule32 (dso, dso->shdr[sec].sh_addr + 4);
+
+ assert (rel->r_offset >= dso->shdr[sec].sh_addr + 12);
+ assert (((rel->r_offset - dso->shdr[sec].sh_addr) & 3) == 0);
+ write_le32 (dso, rel->r_offset,
+ 4 * (rel->r_offset - dso->shdr[sec].sh_addr - 12)
+ + data);
+ }
+ break;
+ case R_386_GLOB_DAT:
+ sec = addr_to_sec (dso, rel->r_offset);
+
+ write_le32 (dso, rel->r_offset, 0);
+ if (sec != -1)
+ {
+ if (strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[sec].sh_name),
+ ".got"))
+ {
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_386_32);
+ return 2;
+ }
+ }
+ break;
+ case R_386_32:
+ case R_386_PC32:
+ error (0, 0, "%s: R_386_%s32 relocs should not be present in prelinked REL sections",
+ GELF_R_TYPE (rel->r_info) == R_386_32 ? "" : "PC", dso->filename);
+ return 1;
+ case R_386_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_386_COPY reloc in shared library?", dso->filename);
+ return 1;
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_DTPOFF32:
+ write_le32 (dso, rel->r_offset, 0);
+ break;
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ break;
+ default:
+ error (0, 0, "%s: Unknown i386 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+i386_rela_to_rel (DSO *dso, GElf_Rela *rela, GElf_Rel *rel)
+{
+ rel->r_offset = rela->r_offset;
+ rel->r_info = rela->r_info;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_386_JMP_SLOT:
+ /* We should be never converting .rel.plt into .rela.plt
+ and thus never .rela.plt back to .rel.plt. */
+ abort ();
+ case R_386_RELATIVE:
+ case R_386_32:
+ case R_386_PC32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ write_le32 (dso, rela->r_offset, rela->r_addend);
+ break;
+ case R_386_COPY:
+ case R_386_GLOB_DAT:
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_DTPOFF32:
+ write_le32 (dso, rela->r_offset, 0);
+ break;
+ }
+ return 0;
+}
+
+static int
+i386_reloc_size (int reloc_type)
+{
+ assert (reloc_type != R_386_COPY);
+ return 4;
+}
+
+static int
+i386_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_386_COPY: return RTYPE_CLASS_COPY;
+ case R_386_JMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_386_TLS_DTPMOD32:
+ case R_386_TLS_DTPOFF32:
+ case R_386_TLS_TPOFF32:
+ case R_386_TLS_TPOFF:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+/* Library memory regions if --exec-shield in order of precedence:
+ 0x00101000 + (rand % 0x00cff000) .. 0x00e00000 bottom to top
+ 0x00101000 .. 0x00101000 + (rand % 0x00cff000) bottom to top
+ 0x02000000 + (rand % 0x06000000) .. 0x08000000 bottom to top
+ 0x02000000 .. 0x02000000 + (rand % 0x06000000) bottom to top
+ 0x41000000 + (rand % 0x0f000000) .. 0x50000000 bottom to top
+ 0x41000000 .. 0x41000000 + (rand % 0x0f000000) bottom to top */
+
+#define REG0S 0x00101000
+#define REG0E 0x00e00000
+#define REG1S 0x02000000
+#define REG1E 0x08000000
+#define REG2S 0x41000000
+#define REG2E 0x50000000
+
+struct i386_layout_data
+{
+ struct prelink_entry e[6];
+ Elf32_Addr addrs[12];
+};
+
+static inline void
+list_append (struct prelink_entry *x, struct prelink_entry *e)
+{
+ x->prev->next = e;
+ e->prev = x->prev;
+ e->next = NULL;
+ x->prev = e;
+}
+
+static inline void
+list_merge (struct prelink_entry *x, struct prelink_entry *e)
+{
+ struct prelink_entry *end = e->prev;
+ x->prev->next = e;
+ e->prev = x->prev;
+ x->prev = end;
+}
+
+static int
+i386_layout_libs_init (struct layout_libs *l)
+{
+ if (exec_shield)
+ {
+ int i;
+ struct prelink_entry *e;
+
+ l->mmap_base = REG0S;
+ l->mmap_end = REG2E;
+ /* Don't allow this to be overridden. */
+ mmap_reg_start = ~(GElf_Addr) 0;
+ mmap_reg_end = ~(GElf_Addr) 0;
+ for (i = 0; i < l->nlibs; ++i)
+ {
+ e = l->libs[i];
+ if (e->done == 0)
+ continue;
+ if (e->base < REG0S
+ || (e->base < REG1S && e->layend > REG0E)
+ || (e->base < REG2S && e->layend > REG1E)
+ || e->layend > REG2E)
+ e->done = 0;
+ }
+ }
+ else
+ {
+ l->mmap_base = REG2S;
+ l->mmap_end = REG2E;
+ }
+ return 0;
+}
+
+static void
+i386_find_free_addr (struct layout_libs *l, Elf32_Addr *ret,
+ Elf32_Addr beg, Elf32_Addr end, Elf32_Addr start)
+{
+ struct prelink_entry *e;
+ Elf32_Addr low, hi;
+
+ ret[0] = beg;
+ ret[3] = end;
+ for (e = l->list; e != NULL; e = e->next)
+ if (e->base >= start)
+ break;
+ if (e == l->list)
+ {
+ ret[1] = ret[2] = start;
+ return;
+ }
+
+ if (e == NULL)
+ e = l->list;
+ low = start;
+ for (e = e->prev; ; e = e->prev)
+ {
+ if (e->base < beg)
+ break;
+ if (e->layend > low)
+ low = e->base;
+ if (e == l->list)
+ break;
+ }
+
+ if (low == start)
+ {
+ ret[1] = ret[2] = start;
+ return;
+ }
+
+ hi = start;
+ for (; e; e = e->next)
+ {
+ if (e->base >= end)
+ break;
+ if (e->base >= hi)
+ break;
+ if (e->layend > hi)
+ hi = e->layend;
+ }
+
+ assert (low >= beg && hi <= end);
+
+ if (hi - start > start - low)
+ start = low;
+ else
+ start = hi;
+
+ ret[1] = ret[2] = start;
+}
+
+static int
+i386_layout_libs_pre (struct layout_libs *l)
+{
+ Elf32_Addr mmap_start, virt;
+ struct prelink_entry *e, *next;
+ struct i386_layout_data *pld;
+ int i;
+
+ if (!exec_shield)
+ {
+ l->mmap_fin = l->mmap_end;
+ l->fake = NULL;
+ l->fakecnt = 0;
+ return 0;
+ }
+
+ pld = calloc (sizeof (*pld), 1);
+ if (pld == NULL)
+ error (EXIT_FAILURE, ENOMEM, "Cannot lay libraries out");
+
+ l->arch_data = pld;
+
+ mmap_start = l->mmap_start - REG0S;
+ /* Unless not randomizing, try not to make the first region
+ too small, because otherwise it is likely libc.so as first
+ big library would often end up at REG0S. */
+ virt = mmap_start % (REG0E - REG0S - 0x200000);
+ i386_find_free_addr (l, pld->addrs + 0, REG0S, REG0E, REG0S + virt);
+ virt = mmap_start % (REG1E - REG1S - 0x200000);
+ i386_find_free_addr (l, pld->addrs + 4, REG1S, REG1E, REG1S + virt);
+ virt = mmap_start % (REG0E - REG0S - 0x200000);
+ i386_find_free_addr (l, pld->addrs + 8, REG2S, REG2E, REG2S + virt);
+ i = 0;
+ virt = pld->addrs[3] - pld->addrs[2];
+ pld->e[0].u.tmp = -1;
+ pld->e[0].base = virt;
+ pld->e[0].end = pld->e[0].base;
+ pld->e[0].layend = pld->e[0].end;
+ pld->e[0].prev = &pld->e[0];
+ next = NULL;
+ for (e = l->list; e != NULL; e = next)
+ {
+ next = e->next;
+ while (i < 5
+ && (e->base >= pld->addrs[2 * i + 1]
+ || pld->addrs[2 * i] == pld->addrs[2 * i + 1]))
+ {
+ ++i;
+ pld->e[i].u.tmp = -1;
+ if (i & 1)
+ virt -= pld->addrs[2 * i + 1] - pld->addrs[2 * i];
+ else
+ {
+ virt += pld->addrs[2 * i - 1] - pld->addrs[2 * i - 4];
+ virt += pld->addrs[2 * i + 3] - pld->addrs[2 * i + 2];
+ }
+ pld->e[i].base = virt;
+ pld->e[i].end = pld->e[i].base;
+ pld->e[i].layend = pld->e[i].end;
+ pld->e[i].prev = &pld->e[i];
+ }
+ e->base += (Elf32_Sword) (virt - pld->addrs[2 * i]);
+ e->end += (Elf32_Sword) (virt - pld->addrs[2 * i]);
+ e->layend += (Elf32_Sword) (virt - pld->addrs[2 * i]);
+ list_append (&pld->e[i], e);
+ }
+ while (i < 5)
+ {
+ ++i;
+ pld->e[i].u.tmp = -1;
+ if (i & 1)
+ virt -= pld->addrs[2 * i + 1] - pld->addrs[2 * i];
+ else
+ {
+ virt += pld->addrs[2 * i - 1] - pld->addrs[2 * i - 4];
+ virt += pld->addrs[2 * i + 3] - pld->addrs[2 * i + 2];
+ }
+ pld->e[i].base = virt;
+ pld->e[i].end = pld->e[i].base;
+ pld->e[i].layend = pld->e[i].end;
+ pld->e[i].prev = &pld->e[i];
+ }
+ l->list = &pld->e[1];
+ list_merge (&pld->e[1], &pld->e[0]);
+ list_merge (&pld->e[1], &pld->e[3]);
+ list_merge (&pld->e[1], &pld->e[2]);
+ list_merge (&pld->e[1], &pld->e[5]);
+ list_merge (&pld->e[1], &pld->e[4]);
+
+ l->mmap_start = 0;
+ l->mmap_base = 0;
+ l->mmap_fin = virt + pld->addrs[2 * i + 1] - pld->addrs[2 * i];
+ l->mmap_end = l->mmap_fin;
+ l->fakecnt = 6;
+ l->fake = pld->e;
+
+ return 0;
+}
+
+static int
+i386_layout_libs_post (struct layout_libs *l)
+{
+ struct prelink_entry *e;
+ struct i386_layout_data *pld = (struct i386_layout_data *) l->arch_data;
+ Elf32_Sword adj = 0;
+ int i;
+
+ if (!exec_shield)
+ return 0;
+
+ for (i = 0, e = l->list; e != NULL; e = e->next)
+ {
+ if (e == &pld->e[i ^ 1])
+ {
+ adj = pld->addrs[2 * (i ^ 1)] - e->base;
+ ++i;
+ }
+ else
+ {
+ e->base += adj;
+ e->end += adj;
+ e->layend += adj;
+ }
+ }
+
+ free (l->arch_data);
+ return 0;
+}
+
+PL_ARCH = {
+ .name = "i386",
+ .class = ELFCLASS32,
+ .machine = EM_386,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_386_JMP_SLOT,
+ .R_COPY = R_386_COPY,
+ .R_RELATIVE = R_386_RELATIVE,
+ .dynamic_linker = "/lib/ld-linux.so.2",
+ .adjust_dyn = i386_adjust_dyn,
+ .adjust_rel = i386_adjust_rel,
+ .adjust_rela = i386_adjust_rela,
+ .prelink_rel = i386_prelink_rel,
+ .prelink_rela = i386_prelink_rela,
+ .prelink_conflict_rel = i386_prelink_conflict_rel,
+ .prelink_conflict_rela = i386_prelink_conflict_rela,
+ .apply_conflict_rela = i386_apply_conflict_rela,
+ .apply_rel = i386_apply_rel,
+ .apply_rela = i386_apply_rela,
+ .rel_to_rela = i386_rel_to_rela,
+ .rela_to_rel = i386_rela_to_rel,
+ .need_rel_to_rela = i386_need_rel_to_rela,
+ .reloc_size = i386_reloc_size,
+ .reloc_class = i386_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = i386_arch_prelink,
+ .arch_undo_prelink = i386_arch_undo_prelink,
+ .undo_prelink_rel = i386_undo_prelink_rel,
+ .layout_libs_init = i386_layout_libs_init,
+ .layout_libs_pre = i386_layout_libs_pre,
+ .layout_libs_post = i386_layout_libs_post,
+ /* Although TASK_UNMAPPED_BASE is 0x40000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = REG2S,
+ .mmap_end = REG2E,
+ .max_page_size = 0x1000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-ia64.c b/trunk/src/arch-ia64.c
new file mode 100644
index 0000000..0be672c
--- /dev/null
+++ b/trunk/src/arch-ia64.c
@@ -0,0 +1,525 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+#include "fptr.h"
+
+static int
+ia64_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_IA_64_PLT_RESERVE)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf64_Addr data;
+
+ if (sec != -1)
+ {
+ data = read_ule64 (dso, dyn->d_un.d_ptr + 8);
+
+ /* If .got[1] points to .plt + 0x30, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 0x30
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_le64 (dso, dyn->d_un.d_ptr + 8, data + adjust);
+ break;
+ }
+ }
+ }
+
+ if (dyn->d_un.d_ptr >= start)
+ dyn->d_un.d_ptr += adjust;
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+ia64_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: IA-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ia64_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_REL32MSB
+ && rela->r_addend >= start)
+ {
+ rela->r_addend += adjust;
+ switch (GELF_R_TYPE (rela->r_info) & 3)
+ {
+ case 0: write_be32 (dso, rela->r_offset, rela->r_addend); break;
+ case 1: write_le32 (dso, rela->r_offset, rela->r_addend); break;
+ case 2: write_be64 (dso, rela->r_offset, rela->r_addend); break;
+ case 3: write_le64 (dso, rela->r_offset, rela->r_addend); break;
+ }
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ {
+ GElf_Addr val, gp;
+
+ if (GELF_R_TYPE (rela->r_info) & 1)
+ {
+ val = read_ule64 (dso, rela->r_offset);
+ gp = read_ule64 (dso, rela->r_offset + 8);
+ }
+ else
+ {
+ val = read_ube64 (dso, rela->r_offset);
+ gp = read_ube64 (dso, rela->r_offset + 8);
+ }
+ if (gp == dso->info[DT_PLTGOT])
+ {
+ if (val >= start)
+ val += adjust;
+ if (gp >= start)
+ gp += adjust;
+ }
+ if (GELF_R_TYPE (rela->r_info) & 1)
+ {
+ write_le64 (dso, rela->r_offset, val);
+ write_le64 (dso, rela->r_offset + 8, gp);
+ }
+ else
+ {
+ write_le64 (dso, rela->r_offset, val);
+ write_le64 (dso, rela->r_offset + 8, gp);
+ }
+ }
+ return 0;
+}
+
+static int
+ia64_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: IA-64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+ia64_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_REL32MSB
+ || GELF_R_TYPE (rela->r_info) == R_IA64_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ dso = info->dso;
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_DIR32MSB)
+ {
+ /* Nothing to do. */
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_PCREL32MSB)
+ {
+ value -= rela->r_offset & -16;
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_FPTR32MSB)
+ {
+ /* FIXME */
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ {
+ GElf_Addr gp = info->resolveent->pltgot;
+
+ if (GELF_R_TYPE (rela->r_info) & 1)
+ {
+ write_le64 (dso, rela->r_offset, value);
+ write_le64 (dso, rela->r_offset + 8, gp);
+ }
+ else
+ {
+ write_be64 (dso, rela->r_offset, value);
+ write_be64 (dso, rela->r_offset + 8, gp);
+ }
+
+ return 0;
+ }
+ else
+ {
+ error (0, 0, "%s: Unknown ia64 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+
+ switch (GELF_R_TYPE (rela->r_info) & 3)
+ {
+ case 0: write_be32 (dso, rela->r_offset, value); break;
+ case 1: write_le32 (dso, rela->r_offset, value); break;
+ case 2: write_be64 (dso, rela->r_offset, value); break;
+ case 3: write_le64 (dso, rela->r_offset, value); break;
+ }
+ return 0;
+}
+
+static int
+ia64_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ if ((GELF_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ {
+ GElf_Addr gp = 0;
+ int i;
+
+ for (i = 0; i < info->ent->ndepends; ++i)
+ if (info->ent->depends[i]->base <= rela->r_addend
+ && info->ent->depends[i]->end > rela->r_addend)
+ {
+ gp = info->ent->depends[i]->pltgot;
+ break;
+ }
+
+ if (i == info->ent->ndepends)
+ abort ();
+
+ if (GELF_R_TYPE (rela->r_info) & 1)
+ {
+ buf_write_le64 (buf, rela->r_addend);
+ buf_write_le64 (buf + 8, gp);
+ }
+ else
+ {
+ buf_write_be64 (buf, rela->r_addend);
+ buf_write_be64 (buf + 8, gp);
+ }
+ return 0;
+ }
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_IA64_DIR32MSB: buf_write_be32 (buf, rela->r_addend); break;
+ case R_IA64_DIR32LSB: buf_write_le32 (buf, rela->r_addend); break;
+ case R_IA64_DIR64MSB: buf_write_be64 (buf, rela->r_addend); break;
+ case R_IA64_DIR64LSB: buf_write_le64 (buf, rela->r_addend); break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+ia64_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: IA-64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+ia64_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_DIR32MSB)
+ {
+ /* Nothing to do. */
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_PCREL32MSB)
+ {
+ value -= rela->r_offset & -16;
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_FPTR32MSB)
+ {
+ /* FIXME */
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ {
+ GElf_Addr gp = info->resolveent->pltgot;
+
+ if (GELF_R_TYPE (rela->r_info) & 1)
+ {
+ buf_write_le64 (buf, value);
+ buf_write_le64 (buf + 8, gp);
+ }
+ else
+ {
+ buf_write_be64 (buf, value);
+ buf_write_be64 (buf + 8, gp);
+ }
+ return 0;
+ }
+ else
+ return 1;
+
+ switch (GELF_R_TYPE (rela->r_info) & 3)
+ {
+ case 0: buf_write_be32 (buf, value); break;
+ case 1: buf_write_le32 (buf, value); break;
+ case 2: buf_write_be64 (buf, value); break;
+ case 3: buf_write_le64 (buf, value); break;
+ }
+ return 0;
+}
+
+static int
+ia64_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: IA-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ia64_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ GElf_Rela *ret;
+
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_REL32MSB
+ || GELF_R_TYPE (rela->r_info) == R_IA64_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ return 0;
+ value = conflict_lookup_value (conflict);
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_DIR32MSB
+ || (GELF_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ {
+ ret->r_addend = value + rela->r_addend;
+ return 0;
+ }
+ else if ((GELF_R_TYPE (rela->r_info) & ~3) == R_IA64_PCREL32MSB)
+ {
+ ret->r_addend = value + rela->r_addend - (rela->r_offset & -16);
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info)
+ + R_IA64_DIR32MSB - R_IA64_PCREL32MSB);
+ return 0;
+ }
+ else if (GELF_R_TYPE (rela->r_info) == R_IA64_COPY)
+ {
+ error (0, 0, "R_IA64_COPY should not be present in shared libraries");
+ return 1;
+ }
+ error (0, 0, "%s: Unknown ia64 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+}
+
+static int
+ia64_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: IA-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ia64_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static GElf_Addr
+ia64_create_opd (struct prelink_info *info, int first, int last, int plt)
+{
+ Elf_Data *d;
+ Elf_Scn *scn;
+ Elf64_Rela *rela, *relaend;
+ DSO *dso = info->dso;
+ int sec;
+
+ if (opd_init (info))
+ return -1;
+
+ if (plt)
+ info->ent->opd->plt_start = dso->shdr[dso->shdr[plt].sh_info].sh_addr;
+ else
+ info->ent->opd->plt_start = dso->shdr[dso->dynamic].sh_addr;
+ sec = first;
+ while (sec <= last)
+ {
+ d = NULL;
+ scn = dso->scn[sec++];
+ while ((d = elf_getdata (scn, d)) != NULL)
+ {
+ rela = (Elf64_Rela *) d->d_buf;
+ relaend = rela + d->d_size / sizeof (Elf64_Rela);
+ for (; rela < relaend; rela++)
+ if ((ELF64_R_TYPE (rela->r_info) & ~3) == R_IA64_FPTR32MSB
+ && opd_add (info, ELF64_R_SYM (rela->r_info),
+ R_IA64_FPTR64LSB))
+ return -1;
+ }
+ }
+
+ sec = first;
+ while (sec)
+ {
+ d = NULL;
+ if (sec == plt)
+ break;
+ scn = dso->scn[sec++];
+ if (sec == last + 1)
+ sec = plt;
+ while ((d = elf_getdata (scn, d)) != NULL)
+ {
+ rela = (Elf64_Rela *) d->d_buf;
+ relaend = rela + d->d_size / sizeof (Elf64_Rela);
+ for (; rela < relaend; rela++)
+ if ((ELF64_R_TYPE (rela->r_info) & ~1) == R_IA64_IPLTMSB)
+ opd_note_plt (info, ELF64_R_SYM (rela->r_info), R_IA64_IPLTLSB,
+ rela->r_offset);
+ }
+ }
+
+ return opd_size (info, 16);
+}
+
+static int
+ia64_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int plt = -1, got = -1, i;
+ const char *name;
+
+ /* Write address of .plt + 0x30 into .got[1].
+ .plt + 0x30 is what .IA_64.pltoff[0] contains unless prelinking. */
+
+ dso = info->dso;
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS)
+ {
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name);
+ if (! strcmp (name, ".got"))
+ {
+ if (got != -1)
+ {
+ error (0, 0, "%s: More than one .got section", dso->filename);
+ return 1;
+ }
+ got = i;
+ }
+ else if (! strcmp (name, ".plt"))
+ {
+ if (plt != -1)
+ {
+ error (0, 0, "%s: More than one .plt section", dso->filename);
+ return 1;
+ }
+ plt = i;
+ }
+ }
+
+ if (plt == -1)
+ return 0;
+
+ if (got == -1)
+ {
+ error (0, 0, "%s: Has .plt section but not .got section", dso->filename);
+ return 1;
+ }
+
+ write_le64 (dso, dso->shdr[got].sh_addr + 8, dso->shdr[plt].sh_addr + 0x30);
+ return 0;
+}
+
+static int
+ia64_reloc_size (int reloc_type)
+{
+ if ((reloc_type & ~1) == R_IA64_IPLTMSB)
+ return 16;
+ return (reloc_type & 2) ? 8 : 4;
+}
+
+static int
+ia64_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_IA64_IPLTLSB:
+ case R_IA64_IPLTMSB:
+ return RTYPE_CLASS_PLT;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "IA-64",
+ .class = ELFCLASS64,
+ .machine = EM_IA_64,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_IA64_IPLTLSB,
+ .R_COPY = -1,
+ .R_RELATIVE = R_IA64_REL64LSB,
+ .dynamic_linker = "/lib/ld-linux-ia64.so.2",
+ .adjust_dyn = ia64_adjust_dyn,
+ .adjust_rel = ia64_adjust_rel,
+ .adjust_rela = ia64_adjust_rela,
+ .prelink_rel = ia64_prelink_rel,
+ .prelink_rela = ia64_prelink_rela,
+ .prelink_conflict_rel = ia64_prelink_conflict_rel,
+ .prelink_conflict_rela = ia64_prelink_conflict_rela,
+ .apply_conflict_rela = ia64_apply_conflict_rela,
+ .apply_rel = ia64_apply_rel,
+ .apply_rela = ia64_apply_rela,
+ .rel_to_rela = ia64_rel_to_rela,
+ .need_rel_to_rela = ia64_need_rel_to_rela,
+ .create_opd = ia64_create_opd,
+ .reloc_size = ia64_reloc_size,
+ .reloc_class = ia64_reloc_class,
+ .max_reloc_size = 16,
+ .arch_prelink = ia64_arch_prelink,
+ /* Although TASK_UNMAPPED_BASE is 0x2000000000000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x2000000010000000LL,
+ .mmap_end = 0x4000000000000000LL,
+ .max_page_size = 0x10000,
+ /* The kernel can be configured for 4K, 8K, 16K and 64K,
+ but most kernels have at least 8K. */
+ .page_size = 0x02000
+};
diff --git a/trunk/src/arch-mips.c b/trunk/src/arch-mips.c
new file mode 100644
index 0000000..f5a2361
--- /dev/null
+++ b/trunk/src/arch-mips.c
@@ -0,0 +1,1031 @@
+/* Copyright (C) 2006 CodeSourcery
+ Written by Richard Sandiford <richard@codesourcery.com>, 2006
+
+ 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. */
+
+/* GENERAL NOTES
+
+ The psABI defines R_MIPS_REL32 as A - EA + S, where the value of EA
+ depends on the symbol index. If the index is less than DT_MIPS_GOTSYM,
+ EA is the symbol's st_value "plus displacement". If the index is greater
+ than or equal to DT_MIPS_GOTSYM, EA is the original value of the
+ associated GOT entry.
+
+ However, glibc's dynamic linker implements a different definition.
+ If the index is less than DT_MIPS_GOTSYM, the dynamic linker adds the
+ symbol's st_value and the base address to the addend. If the index
+ is greater than or equal to DT_MIPS_GOTSYM, the dynamic linker adds
+ the final symbol value to the addend.
+
+ MIPS GOTs are divided into three parts:
+
+ - Reserved entries (of which GNU objects have 2)
+ - Local entries
+ - Global entries
+
+ DT_MIPS_LOCAL_GOTNO gives the total number of reserved and local
+ entries. The local entries all hold virtual addresses and the
+ dynamic linker will add the base address to each one.
+
+ Unlike most other architectures, the MIPS ABI does not use
+ relocations to initialize the global GOT entries. Instead, global
+ GOT entry X is mapped to dynamic symbol DT_MIPS_GOTSYM + X, and there
+ are a total of DT_MIPS_SYMTABNO - DT_MIPS_GOTSYM global GOT entries.
+
+ The interpretation of a global GOT entry depends on the symbol entry
+ and the initial GOT contents. The psABI lists the following cases:
+
+ st_shndx st_type st_value initial GOT value
+ -------- ------- -------- -----------------
+ A: SHN_UNDEF STT_FUNC 0 st_value (== 0) / QS
+ B: SHN_UNDEF STT_FUNC stub address st_value / QS
+ C: SHN_UNDEF all others 0 st_value (== 0) / QS
+ D: SHN_COMMON any alignment 0 / QS
+ E: all others STT_FUNC value st_value / stub address
+ F: all others all others value st_value
+
+ (wording slightly modified from the psABI table). Here, QS denotes
+ Quickstart values.
+
+ The dynamic linker treats each case as follows:
+
+ - [A, B when not binding lazily, C, D, E when not binding lazily, F]
+ Resolve the symbol and store its value in the GOT.
+
+ - [B when binding lazily] Set the GOT entry to the st_value plus
+ the base address.
+
+ - [E when binding lazily] If the GOT entry is different from the st_value,
+ add the base addreess to the GOT entry. Otherwise resolve the symbol
+ and store its value in the GOT (as for A, C, etc).
+
+ As the table shows, we can install Quickstart values for types A-D.
+ Installing Quickstart values for type F should be a no-op, because the
+ GOT should already hold the desired value. Installing Quickstart values
+ for type E would either be a no-op (if the GOT entry already contains
+ st_value) or would lose the address of the lazy binding stub. */
+
+#include <config.h>
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+#include "layout.h"
+
+/* The thread pointer points 0x7000 past the first static TLS block. */
+#define TLS_TP_OFFSET 0x7000
+
+/* Dynamic thread vector pointers point 0x8000 past the start of each
+ TLS block. */
+#define TLS_DTV_OFFSET 0x8000
+
+/* The number of reserved entries at the beginning of the GOT.
+ The dynamic linker points entry 0 to the resolver function
+ and entry 1 to the link_map. */
+#define RESERVED_GOTNO 2
+
+/* A structure for iterating over local GOT entries. */
+struct mips_local_got_iterator {
+ /* The DSO containing the GOT. */
+ DSO *dso;
+
+ /* The size of a GOT entry. */
+ GElf_Word entry_size;
+
+ /* The index of the current GOT entry. */
+ GElf_Word got_index;
+
+ /* A pointer to the current GOT entry. */
+ unsigned char *got_entry;
+
+ /* True if we failed to read an entry correctly. */
+ int failed;
+
+ /* Used internally to obtain GOT_ENTRY. */
+ struct data_iterator got_iterator;
+};
+
+/* Set up LGI to iterate over DSO's local GOT. The caller should use
+ mips_get_local_got_entry to read the first entry. */
+
+static inline void
+mips_init_local_got_iterator (struct mips_local_got_iterator *lgi, DSO *dso)
+{
+ lgi->dso = dso;
+ lgi->entry_size = gelf_fsize (dso->elf, ELF_T_WORD, 1, EV_CURRENT);
+ lgi->got_index = RESERVED_GOTNO - 1;
+ lgi->failed = 0;
+ init_data_iterator (&lgi->got_iterator, dso,
+ dso->info[DT_PLTGOT]
+ + (lgi->got_index + 1) * lgi->entry_size);
+}
+
+/* Return true if LGI has not reached the end of the GOT and if the next
+ entry can be accessed. When returning true, use LGI's fields to
+ describe the next entry. */
+
+static inline int
+mips_get_local_got_entry (struct mips_local_got_iterator *lgi)
+{
+ lgi->got_index++;
+ if (lgi->got_index >= lgi->dso->info_DT_MIPS_LOCAL_GOTNO)
+ return 0;
+
+ lgi->got_entry = get_data_from_iterator (&lgi->got_iterator,
+ lgi->entry_size);
+ if (lgi->got_entry == NULL)
+ {
+ error (0, 0, "%s: Malformed local GOT\n", lgi->dso->filename);
+ lgi->failed = 1;
+ return 0;
+ }
+
+ return 1;
+}
+
+/* A structure for iterating over global GOT entries. */
+struct mips_global_got_iterator {
+ /* The DSO containing the GOT. */
+ DSO *dso;
+
+ /* The size of a GOT entry. */
+ GElf_Word entry_size;
+
+ /* The virtual address of the current GOT entry. */
+ GElf_Addr got_addr;
+
+ /* The index of the associated entry in the dynamic symbol table. */
+ GElf_Word sym_index;
+
+ /* A pointer to the current GOT entry. */
+ unsigned char *got_entry;
+
+ /* The symbol associated with the current GOT entry. */
+ GElf_Sym sym;
+
+ /* True if we failed to read an entry correctly. */
+ int failed;
+
+ /* Used internally to obtain GOT_ENTRY and SYM. */
+ struct data_iterator got_iterator;
+ struct data_iterator sym_iterator;
+};
+
+/* Set up GGI to iterate over DSO's global GOT. The caller should use
+ mips_get_global_got_entry to read the first entry. */
+
+static inline void
+mips_init_global_got_iterator (struct mips_global_got_iterator *ggi, DSO *dso)
+{
+ GElf_Word sym_size;
+
+ ggi->dso = dso;
+ ggi->entry_size = gelf_fsize (dso->elf, ELF_T_WORD, 1, EV_CURRENT);
+ ggi->got_addr = (dso->info[DT_PLTGOT]
+ + (dso->info_DT_MIPS_LOCAL_GOTNO - 1) * ggi->entry_size);
+ ggi->sym_index = dso->info_DT_MIPS_GOTSYM - 1;
+ ggi->failed = 0;
+
+ sym_size = gelf_fsize (dso->elf, ELF_T_SYM, 1, EV_CURRENT);
+ init_data_iterator (&ggi->got_iterator, dso,
+ ggi->got_addr + ggi->entry_size);
+ init_data_iterator (&ggi->sym_iterator, dso,
+ dso->info[DT_SYMTAB] + (ggi->sym_index + 1) * sym_size);
+}
+
+/* Return true if GGI has not reached the end of the GOT and if the next
+ entry can be accessed. When returning true, use GGI's fields to
+ describe the next entry. */
+
+static inline int
+mips_get_global_got_entry (struct mips_global_got_iterator *ggi)
+{
+ ggi->sym_index++;
+ ggi->got_addr += ggi->entry_size;
+ if (ggi->sym_index >= ggi->dso->info_DT_MIPS_SYMTABNO)
+ return 0;
+
+ ggi->got_entry = get_data_from_iterator (&ggi->got_iterator,
+ ggi->entry_size);
+ if (ggi->got_entry == NULL
+ || !get_sym_from_iterator (&ggi->sym_iterator, &ggi->sym))
+ {
+ error (0, 0, "%s: Malformed global GOT\n", ggi->dso->filename);
+ ggi->failed = 1;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+mips_arch_adjust (DSO *dso, GElf_Addr start, GElf_Addr adjust)
+{
+ struct mips_local_got_iterator lgi;
+ struct mips_global_got_iterator ggi;
+ GElf_Addr value;
+
+ if (dso->info[DT_PLTGOT] == 0)
+ return 0;
+
+ /* Adjust every local GOT entry by ADJUST. Every adjustment moves
+ the code and data, so we do not need to check START here. */
+ mips_init_local_got_iterator (&lgi, dso);
+ while (mips_get_local_got_entry (&lgi))
+ {
+ value = buf_read_une32 (dso, lgi.got_entry);
+ buf_write_ne32 (dso, lgi.got_entry, value + adjust);
+ }
+
+ /* Adjust every global GOT entry. Referring to the table above:
+
+ For [A, B, C]: Adjust the GOT entry if it contains st_value
+ and if the symbol's value will be adjusted.
+
+ For [D]: Do nothing. SHN_COMMON entries never need adjusting.
+
+ For [E, F]: Adjust the GOT entry if it does not contain st_value
+ -- in other words, if it is a type E entry that points to a lazy
+ binding stub -- or if the symbol's value will also be adjusted. */
+ mips_init_global_got_iterator (&ggi, dso);
+ while (mips_get_global_got_entry (&ggi))
+ {
+ value = buf_read_une32 (dso, ggi.got_entry);
+ if (ggi.sym.st_shndx != SHN_COMMON
+ && value >= start
+ && (value == ggi.sym.st_value
+ ? adjust_symbol_p (dso, &ggi.sym)
+ : ggi.sym.st_shndx != SHN_UNDEF))
+ buf_write_ne32 (dso, ggi.got_entry, value + adjust);
+ }
+
+ return lgi.failed || ggi.failed;
+}
+
+static int
+mips_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ switch (dyn->d_tag)
+ {
+ case DT_MIPS_TIME_STAMP:
+ case DT_MIPS_ICHECKSUM:
+ case DT_MIPS_IVERSION:
+ case DT_MIPS_CONFLICT:
+ case DT_MIPS_CONFLICTNO:
+ case DT_MIPS_LIBLIST:
+ case DT_MIPS_LIBLISTNO:
+ error (0, 0, "%s: File contains QuickStart information", dso->filename);
+ return 1;
+
+ case DT_MIPS_BASE_ADDRESS:
+ case DT_MIPS_RLD_MAP:
+ case DT_MIPS_OPTIONS:
+ if (dyn->d_un.d_ptr >= start)
+ dyn->d_un.d_ptr += adjust;
+ return 1;
+
+ case DT_MIPS_LOCAL_GOTNO:
+ case DT_MIPS_UNREFEXTNO:
+ case DT_MIPS_SYMTABNO:
+ case DT_MIPS_HIPAGENO:
+ case DT_MIPS_GOTSYM:
+ /* We don't change the layout of the GOT or symbol table. */
+ return 1;
+
+ case DT_MIPS_RLD_VERSION:
+ case DT_MIPS_FLAGS:
+ /* We don't change these properties. */
+ return 1;
+ }
+ return 0;
+}
+
+/* Read the addend for a relocation in DSO. If RELA is nonnull,
+ use its r_addend, otherwise read a 32-bit in-place addend from
+ address R_OFFSET. */
+
+static inline uint32_t
+mips_read_addend (DSO *dso, GElf_Addr r_offset, GElf_Rela *rela)
+{
+ return rela ? rela->r_addend : read_une32 (dso, r_offset);
+}
+
+/* Like mips_read_addend, but change the addend to VALUE. */
+
+static inline void
+mips_write_addend (DSO *dso, GElf_Addr r_offset, GElf_Rela *rela,
+ uint32_t value)
+{
+ if (rela)
+ rela->r_addend = (int32_t) value;
+ else
+ write_ne32 (dso, r_offset, value);
+}
+
+/* There is a relocation of type R_INFO against address R_OFFSET in DSO.
+ Adjust it so that virtual addresses >= START are increased by ADJUST
+ If the relocation is in a RELA section, RELA points to the relocation,
+ otherwise it is null. */
+
+static int
+mips_adjust_reloc (DSO *dso, GElf_Addr r_offset, GElf_Xword r_info,
+ GElf_Addr start, GElf_Addr adjust, GElf_Rela *rela)
+{
+ GElf_Addr value;
+ GElf_Word r_sym;
+
+ if (GELF_R_TYPE (r_info) == R_MIPS_REL32)
+ {
+ r_sym = GELF_R_SYM (r_info);
+ if (r_sym < dso->info_DT_MIPS_GOTSYM)
+ {
+ /* glibc's dynamic linker adds the symbol's st_value and the
+ base address to the addend. It therefore treats all symbols
+ as being relative, even if they would normally be considered
+ absolute. For example, the special null symbol should always
+ have the value zero, even when the base address is nonzero,
+ but R_MIPS_REL32 relocations against the null symbol must
+ nevertheles be adjusted as if that symbol were relative.
+ The same would apply to SHN_ABS symbols too.
+
+ Thus the result of the relocation calculation must always
+ be adjusted by ADJUST. (We do not need to check START because
+ every adjustment requested by the caller will affect all
+ legitimate local relocation values.) This means that we
+ should add ADJUST to the addend if and only if the symbol's
+ value is not being adjusted.
+
+ In general, we can only check whether a symbol's value is
+ being adjusted by reading its entry in the dynamic symbol
+ table and then querying adjust_symbol_p. However, this
+ generality is fortunately not needed. Modern versions
+ of binutils will never generate R_MIPS_REL32 relocations
+ against symbols in the range [1, DT_MIPS_GOTSYM), so we
+ only need to handle relocations against the null symbol. */
+ if (r_sym != 0)
+ {
+ error (0, 0, "%s: The prelinker does not support R_MIPS_REL32"
+ " relocs against local symbols", dso->filename);
+ return 1;
+ }
+ value = mips_read_addend (dso, r_offset, rela);
+ mips_write_addend (dso, r_offset, rela, value + adjust);
+ }
+ }
+ return 0;
+}
+
+static int
+mips_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start, GElf_Addr adjust)
+{
+ return mips_adjust_reloc (dso, rel->r_offset, rel->r_info,
+ start, adjust, NULL);
+}
+
+static int
+mips_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start, GElf_Addr adjust)
+{
+ return mips_adjust_reloc (dso, rela->r_offset, rela->r_info,
+ start, adjust, rela);
+}
+
+/* Calculate relocation RELA as A + VALUE and store the result in DSO. */
+
+static void
+mips_prelink_32bit_reloc (DSO *dso, GElf_Rela *rela, GElf_Addr value)
+{
+ assert (rela != NULL);
+ write_ne32 (dso, rela->r_offset, value + rela->r_addend);
+}
+
+/* There is a relocation of type R_INFO against address R_OFFSET in DSO.
+ Prelink the relocation field, using INFO to look up symbol values.
+ If the relocation is in a RELA section, RELA points to the relocation,
+ otherwise it is null. */
+
+static int
+mips_prelink_reloc (struct prelink_info *info, GElf_Addr r_offset,
+ GElf_Xword r_info, GElf_Rela *rela)
+{
+ DSO *dso;
+ GElf_Addr value;
+ GElf_Word r_sym;
+ int r_type;
+
+ dso = info->dso;
+ r_sym = GELF_R_SYM (r_info);
+ r_type = GELF_R_TYPE (r_info);
+ switch (r_type)
+ {
+ case R_MIPS_NONE:
+ break;
+
+ case R_MIPS_REL32:
+ /* An in-place R_MIPS_REL32 relocation against symbol 0 needs no
+ adjustment. */
+ if (rela != NULL || GELF_R_SYM (r_info) != 0)
+ {
+ value = info->resolve (info, r_sym, r_type);
+ mips_prelink_32bit_reloc (dso, rela, value);
+ }
+ break;
+
+ case R_MIPS_GLOB_DAT:
+ write_ne32 (dso, r_offset, info->resolve (info, r_sym, r_type));
+ break;
+
+ case R_MIPS_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_MIPS_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ /* These relocations will be resolved using a conflict. We need
+ not change the field value here. */
+ break;
+
+ case R_MIPS_TLS_DTPREL32:
+ value = info->resolve (info, r_sym, r_type);
+ mips_prelink_32bit_reloc (dso, rela, value - TLS_DTV_OFFSET);
+ break;
+
+ case R_MIPS_TLS_TPREL32:
+ /* Relocations in a shared library will be resolved using a conflict.
+ We need not change the relocation field here. */
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ value = info->resolve (info, r_sym, r_type);
+ value += info->resolvetls->offset - TLS_TP_OFFSET;
+ mips_prelink_32bit_reloc (dso, rela, value);
+ }
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown MIPS relocation type %d",
+ dso->filename, (int) GELF_R_TYPE (r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mips_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ DSO *dso;
+
+ /* Convert R_MIPS_REL32 relocations against global symbols into
+ R_MIPS_GLOB_DAT if the addend is zero. */
+ dso = info->dso;
+ if (GELF_R_TYPE (rel->r_info) == R_MIPS_REL32
+ && GELF_R_SYM (rel->r_info) >= dso->info_DT_MIPS_GOTSYM
+ && read_une32 (dso, rel->r_offset) == 0)
+ {
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_MIPS_GLOB_DAT);
+ write_ne32 (dso, rel->r_offset,
+ info->resolve (info, GELF_R_SYM (rel->r_info),
+ GELF_R_TYPE (rel->r_info)));
+ return 2;
+ }
+ return mips_prelink_reloc (info, rel->r_offset, rel->r_info, NULL);
+}
+
+static int
+mips_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ return mips_prelink_reloc (info, rela->r_offset, rela->r_info, rela);
+}
+
+/* CONFLICT is a conflict returned by prelink_conflict for a symbol
+ belonging to DSO. Set *TLS_OUT to the associated TLS information.
+ Return 1 on failure. */
+
+static int
+mips_get_tls (DSO *dso, struct prelink_conflict *conflict,
+ struct prelink_tls **tls_out)
+{
+ if (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL)
+ {
+ error (0, 0, "%s: R_MIPS_TLS not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+
+ *tls_out = conflict->lookup.tls;
+ return 0;
+}
+
+/* There is a relocation of type R_INFO against address R_OFFSET in DSO.
+ See if the relocation field must be adjusted by a conflict when DSO
+ is used in the context described by INFO. Add a conflict entry if so.
+ If the relocation is in a RELA section, RELA points to the relocation,
+ otherwise it is null. */
+
+static int
+mips_prelink_conflict_reloc (DSO *dso, struct prelink_info *info,
+ GElf_Addr r_offset, GElf_Xword r_info,
+ GElf_Rela *rela)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *entry;
+
+ conflict = prelink_conflict (info, GELF_R_SYM (r_info),
+ GELF_R_TYPE (r_info));
+ if (conflict == NULL)
+ {
+ switch (GELF_R_TYPE (r_info))
+ {
+ case R_MIPS_TLS_DTPMOD32:
+ case R_MIPS_TLS_TPREL32:
+ tls = info->curtls;
+ if (tls == NULL)
+ return 0;
+ /* A relocation against symbol 0. A shared library cannot
+ know what the final module IDs or TP-relative offsets are,
+ so the executable must always have a conflict for them. */
+ value = 0;
+ break;
+ default:
+ return 0;
+ }
+ }
+ else
+ {
+ /* DTPREL32 relocations just involve the symbol value; no other
+ TLS information is needed. Ignore conflicts created from a
+ lookup of type RTYPE_CLASS_TLS if no real conflict exists. */
+ if (GELF_R_TYPE (r_info) == R_MIPS_TLS_DTPREL32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ /* VALUE now contains the final symbol value. Change it to the
+ value we want to store at R_OFFSET. */
+ switch (GELF_R_TYPE (r_info))
+ {
+ case R_MIPS_REL32:
+ value += mips_read_addend (dso, r_offset, rela);
+ break;
+
+ case R_MIPS_GLOB_DAT:
+ break;
+
+ case R_MIPS_TLS_DTPMOD32:
+ if (conflict != NULL && mips_get_tls (dso, conflict, &tls) == 1)
+ return 1;
+ value = tls->modid;
+ break;
+
+ case R_MIPS_TLS_DTPREL32:
+ value += mips_read_addend (dso, r_offset, rela) - TLS_DTV_OFFSET;
+ break;
+
+ case R_MIPS_TLS_TPREL32:
+ if (conflict != NULL && mips_get_tls (dso, conflict, &tls) == 1)
+ return 1;
+ value += (mips_read_addend (dso, r_offset, rela)
+ + tls->offset - TLS_TP_OFFSET);
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (r_info));
+ return 1;
+ }
+ /* Create and initialize a conflict entry. */
+ entry = prelink_conflict_add_rela (info);
+ if (entry == NULL)
+ return 1;
+ entry->r_addend = (int32_t) value;
+ entry->r_offset = r_offset;
+ entry->r_info = GELF_R_INFO (0, R_MIPS_REL32);
+ return 0;
+}
+
+static int
+mips_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ return mips_prelink_conflict_reloc (dso, info, rel->r_offset,
+ rel->r_info, NULL);
+}
+
+static int
+mips_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ return mips_prelink_conflict_reloc (dso, info, rela->r_offset,
+ rela->r_info, rela);
+}
+
+static int
+mips_arch_prelink_conflict (DSO *dso, struct prelink_info *info)
+{
+ struct mips_global_got_iterator ggi;
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ GElf_Rela *entry;
+
+ if (dso->info[DT_PLTGOT] == 0)
+ return 0;
+
+ /* Add a conflict for every global GOT entry that does not hold the
+ right value, either because of a conflict, or because the DSO has
+ a lazy binding stub for a symbol that it also defines. */
+ mips_init_global_got_iterator (&ggi, dso);
+ while (mips_get_global_got_entry (&ggi))
+ {
+ conflict = prelink_conflict (info, ggi.sym_index, R_MIPS_REL32);
+ if (conflict != NULL)
+ value = conflict_lookup_value (conflict);
+ else if (ggi.sym.st_shndx != SHN_UNDEF
+ && ggi.sym.st_shndx != SHN_COMMON)
+ value = ggi.sym.st_value;
+ else
+ continue;
+ if (buf_read_une32 (dso, ggi.got_entry) != value)
+ {
+ entry = prelink_conflict_add_rela (info);
+ if (entry == NULL)
+ return 1;
+ entry->r_addend = (int32_t) value;
+ entry->r_offset = ggi.got_addr;
+ entry->r_info = GELF_R_INFO (0, R_MIPS_REL32);
+ }
+ }
+
+ return ggi.failed;
+}
+
+static int
+mips_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_MIPS_REL32:
+ buf_write_ne32 (info->dso, buf, rela->r_addend);
+ break;
+
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+/* BUF points to a 32-bit field in DSO that is subject to relocation.
+ If the relocation is in a RELA section, RELA points to the relocation,
+ otherwise it is null. Add the addend to ADJUSTMENT and install the
+ result. */
+
+static inline void
+mips_apply_adjustment (DSO *dso, GElf_Rela *rela, char *buf,
+ GElf_Addr adjustment)
+{
+ if (rela)
+ adjustment += rela->r_addend;
+ else
+ adjustment += buf_read_une32 (dso, buf);
+ buf_write_ne32 (dso, buf, adjustment);
+}
+
+static int
+mips_apply_reloc (struct prelink_info *info, GElf_Xword r_info,
+ GElf_Rela *rela, char *buf)
+{
+ DSO *dso;
+
+ dso = info->dso;
+ switch (GELF_R_TYPE (r_info))
+ {
+ case R_MIPS_NONE:
+ break;
+
+ case R_MIPS_REL32:
+ mips_apply_adjustment (dso, rela, buf,
+ info->resolve (info, GELF_R_SYM (r_info),
+ GELF_R_TYPE (r_info)));
+ break;
+
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mips_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ return mips_apply_reloc (info, rel->r_info, NULL, buf);
+}
+
+static int
+mips_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ return mips_apply_reloc (info, rela->r_info, rela, buf);
+}
+
+static int
+mips_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ rela->r_offset = rel->r_offset;
+ rela->r_info = rel->r_info;
+ switch (GELF_R_TYPE (rel->r_info))
+ {
+ case R_MIPS_REL32:
+ case R_MIPS_TLS_DTPREL32:
+ case R_MIPS_TLS_TPREL32:
+ /* These relocations have an in-place addend. */
+ rela->r_addend = (int32_t) read_une32 (dso, rel->r_offset);
+ break;
+
+ case R_MIPS_NONE:
+ case R_MIPS_GLOB_DAT:
+ case R_MIPS_TLS_DTPMOD32:
+ /* These relocations have no addend. */
+ rela->r_addend = 0;
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mips_rela_to_rel (DSO *dso, GElf_Rela *rela, GElf_Rel *rel)
+{
+ rel->r_offset = rela->r_offset;
+ rel->r_info = rela->r_info;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_MIPS_NONE:
+ break;
+
+ case R_MIPS_REL32:
+ case R_MIPS_TLS_DTPREL32:
+ case R_MIPS_TLS_TPREL32:
+ /* These relocations have an in-place addend. */
+ write_ne32 (dso, rela->r_offset, rela->r_addend);
+ break;
+
+ case R_MIPS_GLOB_DAT:
+ case R_MIPS_TLS_DTPMOD32:
+ /* These relocations have no addend. */
+ write_ne32 (dso, rela->r_offset, 0);
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+mips_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ Elf_Data *data;
+ Elf_Scn *scn;
+ Elf32_Rel *rel, *relend;
+ int n;
+
+ for (n = first; n <= last; n++)
+ {
+ data = NULL;
+ scn = dso->scn[n];
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ rel = (Elf32_Rel *) data->d_buf;
+ relend = rel + data->d_size / sizeof (Elf32_Rel);
+ for (; rel < relend; rel++)
+ switch (ELF32_R_TYPE (rel->r_info))
+ {
+ case R_MIPS_NONE:
+ break;
+
+ case R_MIPS_REL32:
+ /* The SVR4 definition was designed to allow exactly the
+ sort of prelinking we want to do here, in combination
+ with Quickstart. Unfortunately, glibc's definition
+ makes it impossible for relocations against anything
+ other than the null symbol. We get around this for
+ zero addends by using a R_MIPS_GLOB_DAT relocation
+ instead, where R_MIPS_GLOB_DAT is a GNU extension
+ added specifically for this purpose. */
+ if (ELF32_R_SYM (rel->r_info) != 0
+ && (ELF32_R_SYM (rel->r_info) < dso->info_DT_MIPS_GOTSYM
+ || read_une32 (dso, rel->r_offset) != 0))
+ return 1;
+ break;
+
+ case R_MIPS_GLOB_DAT:
+ /* This relocation has no addend. */
+ break;
+
+ case R_MIPS_TLS_DTPMOD32:
+ /* The relocation will be resolved using a conflict. */
+ break;
+
+ case R_MIPS_TLS_DTPREL32:
+ /* We can prelink these fields, and the addend is relative
+ to the symbol value. A RELA entry is needed. */
+ return 1;
+
+ case R_MIPS_TLS_TPREL32:
+ /* Relocations in shared libraries will be resolved by a
+ conflict. Relocations in executables will not, and the
+ addend is relative to the symbol value. */
+ if (dso->ehdr.e_type == ET_EXEC)
+ return 1;
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown MIPS relocation type %d",
+ dso->filename, (int) GELF_R_TYPE (rel->r_info));
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+mips_reloc_size (int reloc_type)
+{
+ return 4;
+}
+
+static int
+mips_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_MIPS_TLS_DTPMOD32:
+ case R_MIPS_TLS_DTPREL32:
+ case R_MIPS_TLS_TPREL32:
+ return RTYPE_CLASS_TLS;
+ default:
+ /* MIPS lazy resolution stubs are local to the containing object,
+ so SHN_UNDEF symbols never participate in symbol lookup. */
+ return RTYPE_CLASS_PLT;
+ }
+}
+
+static int
+mips_arch_prelink (struct prelink_info *info)
+{
+ struct mips_global_got_iterator ggi;
+ DSO *dso;
+ GElf_Addr value;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT] == 0)
+ return 0;
+
+ /* Install Quickstart values for all global GOT entries of type A-D
+ in the table above. */
+ mips_init_global_got_iterator (&ggi, dso);
+ while (mips_get_global_got_entry (&ggi))
+ {
+ value = info->resolve (info, ggi.sym_index, R_MIPS_REL32);
+ if (ggi.sym.st_shndx == SHN_UNDEF
+ || ggi.sym.st_shndx == SHN_COMMON)
+ buf_write_ne32 (dso, ggi.got_entry, value);
+ else
+ {
+ /* Type E and F in the table above. We cannot install Quickstart
+ values for type E, but we should never need to in executables,
+ because an executable should not use lazy binding stubs for
+ symbols it defines itself. Although we could in theory just
+ discard any such stub address, it goes against the principle
+ that prelinking should be reversible.
+
+ When type E entries occur in shared libraries, we can fix
+ them up using conflicts.
+
+ Type F entries should never need a Quickstart value -- the
+ current value should already be correct. However, the conflict
+ code will cope correctly with malformed type F entries in
+ shared libraries, so we only complain about executables here. */
+ if (dso->ehdr.e_type == ET_EXEC
+ && value != buf_read_une32 (dso, ggi.got_entry))
+ {
+ error (0, 0, "%s: The global GOT entries for defined symbols"
+ " do not match their st_values\n", dso->filename);
+ return 1;
+ }
+ }
+ }
+ return ggi.failed;
+}
+
+static int
+mips_arch_undo_prelink (DSO *dso)
+{
+ struct mips_global_got_iterator ggi;
+
+ if (dso->info[DT_PLTGOT] == 0)
+ return 0;
+
+ mips_init_global_got_iterator (&ggi, dso);
+ while (mips_get_global_got_entry (&ggi))
+ if (ggi.sym.st_shndx == SHN_UNDEF)
+ /* Types A-C in the table above. */
+ buf_write_ne32 (dso, ggi.got_entry, ggi.sym.st_value);
+ else if (ggi.sym.st_shndx == SHN_COMMON)
+ /* Type D in the table above. */
+ buf_write_ne32 (dso, ggi.got_entry, 0);
+ return ggi.failed;
+}
+
+static int
+mips_undo_prelink_rel (DSO *dso, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ /* Convert R_MIPS_GLOB_DAT relocations back into R_MIPS_REL32
+ relocations. Ideally we'd have some mechanism for recording
+ these changes in the undo section, but in the absence of that,
+ it's better to assume that the original relocation was
+ R_MIPS_REL32; R_MIPS_GLOB_DAT was added specifically for the
+ prelinker and shouldn't be used in non-prelinked binaries. */
+ if (GELF_R_TYPE (rel->r_info) == R_MIPS_GLOB_DAT)
+ {
+ write_ne32 (dso, rel->r_offset, 0);
+ rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_MIPS_REL32);
+ return 2;
+ }
+ return 0;
+}
+
+PL_ARCH = {
+ .name = "MIPS",
+ .class = ELFCLASS32,
+ .machine = EM_MIPS,
+ .max_reloc_size = 4,
+ .dynamic_linker = "/lib/ld.so.1",
+ /* MIPS does not use COPY relocs or jump slots. Pick a value outside
+ the ELF32_R_TYPE range. */
+ .R_COPY = ~0U,
+ .R_JMP_SLOT = ~0U,
+ /* R_MIPS_REL32 relocations against symbol 0 do act as relative relocs,
+ but those against other symbols don't. */
+ .R_RELATIVE = ~0U,
+ .arch_adjust = mips_arch_adjust,
+ .adjust_dyn = mips_adjust_dyn,
+ .adjust_rel = mips_adjust_rel,
+ .adjust_rela = mips_adjust_rela,
+ .prelink_rel = mips_prelink_rel,
+ .prelink_rela = mips_prelink_rela,
+ .prelink_conflict_rel = mips_prelink_conflict_rel,
+ .prelink_conflict_rela = mips_prelink_conflict_rela,
+ .arch_prelink_conflict = mips_arch_prelink_conflict,
+ .apply_conflict_rela = mips_apply_conflict_rela,
+ .apply_rel = mips_apply_rel,
+ .apply_rela = mips_apply_rela,
+ .rel_to_rela = mips_rel_to_rela,
+ .rela_to_rel = mips_rela_to_rel,
+ .need_rel_to_rela = mips_need_rel_to_rela,
+ .reloc_size = mips_reloc_size,
+ .reloc_class = mips_reloc_class,
+ .arch_prelink = mips_arch_prelink,
+ .arch_undo_prelink = mips_arch_undo_prelink,
+ .undo_prelink_rel = mips_undo_prelink_rel,
+ /* Although TASK_UNMAPPED_BASE is 0x2aaa8000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x2c000000,
+ .mmap_end = 0x3c000000,
+ .max_page_size = 0x10000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-ppc.c b/trunk/src/arch-ppc.c
new file mode 100644
index 0000000..5608750
--- /dev/null
+++ b/trunk/src/arch-ppc.c
@@ -0,0 +1,1154 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 2005 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+#include "layout.h"
+
+#ifndef DT_PPC_GOT
+# define DT_PPC_GOT (DT_LOPROC + 0)
+#endif
+
+#define DT_PPC_GOT_BIT DT_LOPROC_BIT
+
+static int
+ppc_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PPC_GOT)
+ {
+ Elf32_Addr data;
+
+ data = read_ube32 (dso, dyn->d_un.d_ptr);
+ /* DT_PPC_GOT[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_be32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ube32 (dso, dyn->d_un.d_ptr + 4);
+ /* DT_PPC_GOT[1] points to .glink in prelinked libs. */
+ if (data && data >= start)
+ write_be32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+
+ if (dyn->d_un.d_ptr >= start)
+ {
+ dyn->d_un.d_ptr += adjust;
+ return 1;
+ }
+ }
+ else if (dyn->d_tag == DT_PLTGOT
+ && !dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".got")
+ && dso->shdr[i].sh_size >= 16)
+ {
+ Elf32_Addr data, addr;
+ int step;
+
+ /* If .got[1] points to _DYNAMIC, it needs to be adjusted.
+ Other possible locations of the .got header are at the
+ end of .got or around offset 32768 in it. */
+ for (addr = dso->shdr[i].sh_addr, step = 0; step < 18; step++)
+ {
+ if (read_ube32 (dso, addr) == 0x4e800021
+ && (data = read_ube32 (dso, addr + 4))
+ == dso->shdr[n].sh_addr
+ && data >= start
+ && read_ube32 (dso, addr + 8) == 0
+ && read_ube32 (dso, addr + 12) == 0)
+ {
+ /* Probably should use here a check that neither of
+ the 4 addresses contains a dynamic relocation against
+ it. */
+ write_be32 (dso, addr + 4, data + adjust);
+ break;
+ }
+ if (step == 0)
+ addr = dso->shdr[i].sh_addr + dso->shdr[i].sh_size - 16;
+ else if (step == 1)
+ {
+ if (dso->shdr[i].sh_size >= 32768 - 32)
+ addr = dso->shdr[i].sh_addr + 32768 - 32 - 16;
+ else
+ break;
+ }
+ else
+ {
+ addr += 4;
+ if (addr + 16
+ > dso->shdr[i].sh_addr + dso->shdr[i].sh_size)
+ break;
+ }
+ }
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int
+ppc_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: PowerPC doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (GELF_R_TYPE (rela->r_info) == R_PPC_RELATIVE)
+ {
+ if ((Elf32_Word) rela->r_addend >= start)
+ rela->r_addend += (Elf32_Sword) adjust;
+ }
+ if (GELF_R_TYPE (rela->r_info) == R_PPC_JMP_SLOT
+ && dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ {
+ Elf32_Addr data = read_ube32 (dso, rela->r_offset);
+ if (data >= start)
+ write_be32 (dso, rela->r_offset, data + adjust);
+ }
+ return 0;
+}
+
+static int
+ppc_prelink_rel (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: PowerPC doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static void
+ppc_fixup_plt (DSO *dso, GElf_Rela *rela, GElf_Addr value)
+{
+ Elf32_Sword disp = value - rela->r_offset;
+
+ if (disp >= -0x2000000 && disp < 0x2000000)
+ {
+ /* b value */
+ write_be32 (dso, rela->r_offset, 0x48000000 | (disp & 0x3fffffc));
+ }
+ else if ((Elf32_Addr) value >= -0x2000000 || value < 0x2000000)
+ {
+ /* ba value */
+ write_be32 (dso, rela->r_offset, 0x48000002 | (value & 0x3fffffc));
+ }
+ else
+ {
+ Elf32_Addr plt = dso->info[DT_PLTGOT];
+
+ if (rela->r_offset - plt < (8192 * 2 + 18) * 4)
+ {
+ Elf32_Word index = (rela->r_offset - plt - 18 * 4) / (4 * 2);
+ Elf32_Word count = dso->info[DT_PLTRELSZ] / sizeof (Elf32_Rela);
+ Elf32_Addr data;
+
+ data = plt + (18 + 2 * count
+ + (count > 8192 ? (count - 8192) * 2 : 0)) * 4;
+ write_be32 (dso, data + 4 * index, value);
+ /* li %r11, 4*index
+ b .plt+0 */
+ write_be32 (dso, rela->r_offset,
+ 0x39600000 | ((index * 4) & 0xffff));
+ write_be32 (dso, rela->r_offset + 4,
+ 0x48000000 | ((plt - rela->r_offset - 4) & 0x3fffffc));
+ }
+ else
+ {
+ /* lis %r12, %hi(finaladdr)
+ addi %r12, %r12, %lo(finaladdr)
+ mtctr %r12
+ bctr */
+ write_be32 (dso, rela->r_offset,
+ 0x39800000 | (((value + 0x8000) >> 16) & 0xffff));
+ write_be32 (dso, rela->r_offset + 4, 0x398c0000 | (value & 0xffff));
+ write_be32 (dso, rela->r_offset + 8, 0x7d8903a6);
+ write_be32 (dso, rela->r_offset + 12, 0x4e800420);
+ }
+ }
+}
+
+static int
+ppc_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_PPC_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_PPC_RELATIVE)
+ {
+ write_be32 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_GLOB_DAT:
+ case R_PPC_ADDR32:
+ case R_PPC_UADDR32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_PPC_DTPREL32:
+ write_be32 (dso, rela->r_offset, value - 0x8000);
+ break;
+ case R_PPC_JMP_SLOT:
+ if (dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ write_be32 (dso, rela->r_offset, value);
+ else
+ ppc_fixup_plt (dso, rela, value);
+ break;
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ case R_PPC_ADDR16_LO:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_PPC_DTPREL16:
+ case R_PPC_DTPREL16_LO:
+ write_be16 (dso, rela->r_offset, value - 0x8000);
+ break;
+ case R_PPC_ADDR16_HI:
+ case R_PPC_DTPREL16_HA:
+ write_be16 (dso, rela->r_offset, value >> 16);
+ break;
+ case R_PPC_DTPREL16_HI:
+ write_be16 (dso, rela->r_offset, (value - 0x8000) >> 16);
+ break;
+ case R_PPC_ADDR16_HA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 16);
+ break;
+ case R_PPC_ADDR24:
+ write_be32 (dso, rela->r_offset,
+ (value & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003));
+ break;
+ case R_PPC_ADDR14:
+ write_be32 (dso, rela->r_offset,
+ (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffff0003));
+ break;
+ case R_PPC_ADDR14_BRTAKEN:
+ case R_PPC_ADDR14_BRNTAKEN:
+ write_be32 (dso, rela->r_offset,
+ (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffdf0003)
+ | ((((GELF_R_TYPE (rela->r_info) == R_PPC_ADDR14_BRTAKEN)
+ << 21)
+ ^ (value >> 10)) & 0x00200000));
+ break;
+ case R_PPC_REL24:
+ write_be32 (dso, rela->r_offset,
+ ((value - rela->r_offset) & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003));
+ break;
+ case R_PPC_REL32:
+ write_be32 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ /* DTPMOD32 and TPREL* is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_PPC_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_PPC_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_PPC_TPREL32:
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ case R_PPC_TPREL16_HI:
+ case R_PPC_TPREL16_HA:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ {
+ value += info->resolvetls->offset - 0x7000;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_TPREL32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_PPC_TPREL16_HI:
+ write_be16 (dso, rela->r_offset, value >> 16);
+ break;
+ case R_PPC_TPREL16_HA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 16);
+ break;
+ }
+ }
+ break;
+ case R_PPC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_PPC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown ppc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_ADDR32:
+ case R_PPC_UADDR32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ buf_write_be16 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+ppc_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: PowerPC doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+ppc_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_NONE:
+ break;
+ case R_PPC_GLOB_DAT:
+ case R_PPC_ADDR32:
+ case R_PPC_UADDR32:
+ buf_write_be32 (buf, value);
+ break;
+ case R_PPC_ADDR16_HA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC_ADDR16_HI:
+ value = value >> 16;
+ /* FALLTHROUGH */
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ case R_PPC_ADDR16_LO:
+ buf_write_be16 (buf, value);
+ break;
+ case R_PPC_ADDR24:
+ buf_write_be32 (buf, (value & 0x03fffffc)
+ | (buf_read_ube32 (buf) & 0xfc000003));
+ break;
+ case R_PPC_ADDR14:
+ buf_write_be32 (buf, (value & 0xfffc)
+ | (buf_read_ube32 (buf) & 0xffff0003));
+ break;
+ case R_PPC_ADDR14_BRTAKEN:
+ case R_PPC_ADDR14_BRNTAKEN:
+ buf_write_be32 (buf, (value & 0xfffc)
+ | (buf_read_ube32 (buf) & 0xffdf0003)
+ | ((((GELF_R_TYPE (rela->r_info)
+ == R_PPC_ADDR14_BRTAKEN) << 21)
+ ^ (value >> 10)) & 0x00200000));
+ break;
+ case R_PPC_REL24:
+ buf_write_be32 (buf, ((value - rela->r_offset) & 0x03fffffc)
+ | (buf_read_ube32 (buf) & 0xfc000003));
+ break;
+ case R_PPC_REL32:
+ buf_write_be32 (buf, value - rela->r_offset);
+ break;
+ case R_PPC_RELATIVE:
+ error (0, 0, "%s: R_PPC_RELATIVE in ET_EXEC object?",
+ info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: PowerPC doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+ int r_type;
+
+ if (GELF_R_TYPE (rela->r_info) == R_PPC_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_PPC_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPREL relocs need conflicts. */
+ case R_PPC_DTPMOD32:
+ case R_PPC_TPREL32:
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ case R_PPC_TPREL16_HI:
+ case R_PPC_TPREL16_HA:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPREL wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_DTPREL32:
+ case R_PPC_DTPREL16:
+ case R_PPC_DTPREL16_LO:
+ case R_PPC_DTPREL16_HI:
+ case R_PPC_DTPREL16_HA:
+ return 0;
+ }
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ value += rela->r_addend;
+ r_type = GELF_R_TYPE (rela->r_info);
+ switch (r_type)
+ {
+ case R_PPC_GLOB_DAT:
+ r_type = R_PPC_ADDR32;
+ break;
+ case R_PPC_ADDR32:
+ case R_PPC_UADDR32:
+ break;
+ case R_PPC_JMP_SLOT:
+ if (dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ r_type = R_PPC_ADDR32;
+ break;
+ case R_PPC_ADDR16_HA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC_ADDR16_HI:
+ value = value >> 16;
+ /* FALLTHROUGH */
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ case R_PPC_ADDR16_LO:
+ if (r_type != R_PPC_UADDR16)
+ r_type = R_PPC_ADDR16;
+ value = ((value & 0xffff) ^ 0x8000) - 0x8000;
+ break;
+ case R_PPC_ADDR24:
+ r_type = R_PPC_ADDR32;
+ value = (value & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ break;
+ case R_PPC_ADDR14:
+ r_type = R_PPC_ADDR32;
+ value = (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffff0003);
+ break;
+ case R_PPC_ADDR14_BRTAKEN:
+ case R_PPC_ADDR14_BRNTAKEN:
+ r_type = R_PPC_ADDR32;
+ value = (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffdf0003)
+ | ((((r_type == R_PPC_ADDR14_BRTAKEN) << 21)
+ ^ (value >> 10)) & 0x00200000);
+ break;
+ case R_PPC_REL24:
+ r_type = R_PPC_ADDR32;
+ value = ((value - rela->r_offset) & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ break;
+ case R_PPC_REL32:
+ r_type = R_PPC_ADDR32;
+ value -= rela->r_offset;
+ break;
+ case R_PPC_DTPMOD32:
+ case R_PPC_DTPREL32:
+ case R_PPC_DTPREL16:
+ case R_PPC_DTPREL16_LO:
+ case R_PPC_DTPREL16_HI:
+ case R_PPC_DTPREL16_HA:
+ case R_PPC_TPREL32:
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ case R_PPC_TPREL16_HI:
+ case R_PPC_TPREL16_HA:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ r_type = R_PPC_ADDR16;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_DTPMOD32:
+ r_type = R_PPC_ADDR32;
+ value = tls->modid;
+ break;
+ case R_PPC_DTPREL32:
+ r_type = R_PPC_ADDR32;
+ value -= 0x8000;
+ break;
+ case R_PPC_DTPREL16:
+ case R_PPC_DTPREL16_LO:
+ value -= 0x8000;
+ break;
+ case R_PPC_DTPREL16_HI:
+ value = (value - 0x8000) >> 16;
+ break;
+ case R_PPC_DTPREL16_HA:
+ value >>= 16;
+ break;
+ case R_PPC_TPREL32:
+ r_type = R_PPC_ADDR32;
+ value += tls->offset - 0x7000;
+ break;
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ value += tls->offset - 0x7000;
+ break;
+ case R_PPC_TPREL16_HI:
+ value = (value + tls->offset - 0x7000) >> 16;
+ break;
+ case R_PPC_TPREL16_HA:
+ value = (value + tls->offset - 0x7000 + 0x8000) >> 16;
+ break;
+ }
+ if (r_type == R_PPC_ADDR16)
+ value = ((value & 0xffff) ^ 0x8000) - 0x8000;
+ break;
+ default:
+ error (0, 0, "%s: Unknown PowerPC relocation type %d", dso->filename,
+ r_type);
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, r_type);
+ ret->r_addend = (Elf32_Sword) value;
+ return 0;
+}
+
+static int
+ppc_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: PowerPC doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+ppc_arch_pre_prelink (DSO *dso)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn;
+ GElf_Dyn dyn;
+ Elf32_Addr val;
+ int i;
+
+ if (!dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ return 0;
+
+ assert (dso->shdr[dso->dynamic].sh_type == SHT_DYNAMIC);
+
+ scn = dso->scn[dso->dynamic];
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+
+ maxndx = data->d_size / dso->shdr[dso->dynamic].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ {
+ gelfx_getdyn (dso->elf, data, ndx, &dyn);
+ assert (dyn.d_tag != DT_NULL);
+ if (dyn.d_tag == DT_PPC_GOT)
+ break;
+ }
+ if (ndx < maxndx)
+ break;
+ }
+
+ /* DT_PPC_GOT[1] should point to .glink in prelinked libs. */
+ val = read_ube32 (dso, dyn.d_un.d_ptr + 4);
+ if (val)
+ return 0;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+
+ val = read_ube32 (dso, dso->shdr[i].sh_addr);
+ write_be32 (dso, dyn.d_un.d_ptr + 4, val);
+
+ return 0;
+}
+
+static int
+ppc_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso = info->dso;
+ Elf32_Addr plt = dso->info[DT_PLTGOT];
+
+ if (plt && !dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ {
+ Elf32_Word count = dso->info[DT_PLTRELSZ] / sizeof (Elf32_Rela);
+ Elf32_Addr data;
+
+ data = plt + (18 + 2 * count
+ + (count > 8192 ? (count - 8192) * 2 : 0)) * 4;
+
+ /* addis %r11, %r11, %hi(data)
+ lwz %r11, %r11, %lo(data)
+ mtctr %r11
+ bctr */
+ write_be32 (dso, plt, 0x3d6b0000 | (((data + 0x8000) >> 16) & 0xffff));
+ write_be32 (dso, plt + 4, 0x816b0000 | (data & 0xffff));
+ write_be32 (dso, plt + 8, 0x7d6903a6);
+ write_be32 (dso, plt + 12, 0x4e800420);
+ }
+ return 0;
+}
+
+static int
+ppc_arch_undo_prelink (DSO *dso)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn;
+ GElf_Dyn dyn;
+ Elf32_Addr val, addr, endaddr;
+ int i;
+
+ if (!dynamic_info_is_set (dso, DT_PPC_GOT_BIT))
+ return 0;
+
+ assert (dso->shdr[dso->dynamic].sh_type == SHT_DYNAMIC);
+
+ scn = dso->scn[dso->dynamic];
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+
+ maxndx = data->d_size / dso->shdr[dso->dynamic].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ {
+ gelfx_getdyn (dso->elf, data, ndx, &dyn);
+ assert (dyn.d_tag != DT_NULL);
+ if (dyn.d_tag == DT_PPC_GOT)
+ break;
+ }
+ if (ndx < maxndx)
+ break;
+ }
+
+ /* DT_PPC_GOT[1] should point to .glink in prelinked libs. */
+ val = read_ube32 (dso, dyn.d_un.d_ptr + 4);
+ if (!val)
+ return 0;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum || (dso->shdr[i].sh_size & 3))
+ return 0;
+
+ addr = dso->shdr[i].sh_addr;
+ endaddr = addr + dso->shdr[i].sh_size;
+ for (; addr < endaddr; addr += 4, val += 4)
+ write_be32 (dso, addr, val);
+
+ write_be32 (dso, dyn.d_un.d_ptr + 4, 0);
+
+ return 0;
+}
+
+
+static int
+ppc_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC_NONE:
+ return 0;
+ case R_PPC_RELATIVE:
+ case R_PPC_GLOB_DAT:
+ case R_PPC_ADDR32:
+ case R_PPC_UADDR32:
+ case R_PPC_REL32:
+ case R_PPC_DTPMOD32:
+ case R_PPC_DTPREL32:
+ case R_PPC_TPREL32:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_PPC_JMP_SLOT:
+ /* .plt section will become SHT_NOBITS if DT_PPC_GOT is not present,
+ otherwise .plt section will be unprelinked in
+ ppc_arch_undo_prelink. */
+ return 0;
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ case R_PPC_ADDR16_LO:
+ case R_PPC_ADDR16_HI:
+ case R_PPC_ADDR16_HA:
+ case R_PPC_DTPREL16:
+ case R_PPC_TPREL16:
+ case R_PPC_DTPREL16_LO:
+ case R_PPC_TPREL16_LO:
+ case R_PPC_DTPREL16_HI:
+ case R_PPC_TPREL16_HI:
+ case R_PPC_DTPREL16_HA:
+ case R_PPC_TPREL16_HA:
+ write_be16 (dso, rela->r_offset, 0);
+ break;
+ case R_PPC_ADDR24:
+ case R_PPC_REL24:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ break;
+ case R_PPC_ADDR14:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffff0003);
+ break;
+ case R_PPC_ADDR14_BRTAKEN:
+ case R_PPC_ADDR14_BRNTAKEN:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffdf0003);
+ break;
+ case R_PPC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_PPC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown ppc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
+ case R_PPC_ADDR16_LO:
+ case R_PPC_ADDR16_HI:
+ case R_PPC_ADDR16_HA:
+ case R_PPC_DTPREL16:
+ case R_PPC_DTPREL16_LO:
+ case R_PPC_DTPREL16_HI:
+ case R_PPC_DTPREL16_HA:
+ case R_PPC_TPREL16:
+ case R_PPC_TPREL16_LO:
+ case R_PPC_TPREL16_HI:
+ case R_PPC_TPREL16_HA:
+ return 2;
+ default:
+ break;
+ }
+ return 4;
+}
+
+static int
+ppc_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_PPC_COPY: return RTYPE_CLASS_COPY;
+ case R_PPC_JMP_SLOT: return RTYPE_CLASS_PLT;
+ default:
+ if (reloc_type >= R_PPC_DTPMOD32 && reloc_type <= R_PPC_DTPREL32)
+ return RTYPE_CLASS_TLS;
+ return RTYPE_CLASS_VALID;
+ }
+}
+
+/* Library memory regions in order of precedence:
+ 0xe800000 .. 0x10000000 top to bottom
+ 0x40000 .. 0xe800000 bottom to top
+ 0x18000000 .. 0x30000000 bottom to top */
+
+#define REG0S 0x0e800000
+#define REG0E 0x10000000
+#define REG1S 0x00040000
+#define REG1E REG0S
+#define REG2S 0x18000000
+#define REG2E 0x30000000
+
+struct ppc_layout_data
+{
+ int cnt;
+ struct prelink_entry e[3];
+ Elf32_Addr mmap_start, first_start, last_start;
+ struct
+ {
+ struct prelink_entry *e;
+ Elf32_Addr base, end, layend;
+ } ents[0];
+};
+
+static inline void
+list_append (struct prelink_entry *x, struct prelink_entry *e)
+{
+ x->prev->next = e;
+ e->prev = x->prev;
+ e->next = NULL;
+ x->prev = e;
+}
+
+static int
+addr_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+
+ if (a->base < b->base)
+ return -1;
+ else if (a->base > b->base)
+ return 1;
+ if (a->layend < b->layend)
+ return -1;
+ else if (a->layend > b->layend)
+ return 1;
+ return 0;
+}
+
+static void
+list_sort (struct prelink_entry *x)
+{
+ int cnt, i;
+ struct prelink_entry *e;
+ struct prelink_entry **a;
+
+ if (x->next == NULL)
+ return;
+ for (cnt = 0, e = x->next; e != NULL; e = e->next)
+ ++cnt;
+ a = alloca (cnt * sizeof (*a));
+ for (i = 0, e = x->next; e != NULL; e = e->next)
+ a[i++] = e;
+ qsort (a, cnt, sizeof (*a), addr_cmp);
+ x->next = NULL;
+ x->prev = x;
+ for (i = 0; i < cnt; ++i)
+ list_append (x, a[i]);
+}
+
+static int
+ppc_layout_libs_pre (struct layout_libs *l)
+{
+ Elf32_Addr mmap_start = l->mmap_start - REG1S;
+ Elf32_Addr first_start = REG0S, last_start = REG2S;
+ struct prelink_entry *e, e0, *next = NULL;
+ struct ppc_layout_data *pld;
+ int cnt;
+
+ mmap_start = REG0E - (mmap_start & 0xff0000);
+ for (cnt = 0, e = l->list; e != NULL; e = e->next, ++cnt)
+ {
+ if (e->base < mmap_start && e->layend > mmap_start)
+ mmap_start = (e->layend + 0xffff) & ~0xffff;
+ if (e->base < REG0S && e->layend > REG0S && first_start > e->base)
+ first_start = e->base;
+ if (e->base < REG0E && e->layend > REG2S && last_start < e->layend)
+ last_start = e->layend;
+ }
+ if (mmap_start > REG0E)
+ mmap_start = REG0E;
+
+ pld = calloc (sizeof (*pld) + cnt * sizeof (pld->ents[0]), 1);
+ if (pld == NULL)
+ error (EXIT_FAILURE, ENOMEM, "Cannot lay libraries out");
+
+ l->arch_data = pld;
+ memset (&e0, 0, sizeof (e0));
+ e0.prev = &e0;
+ pld->cnt = cnt;
+ pld->e[0].u.tmp = -1;
+ pld->e[0].base = REG1S + REG0E - mmap_start;
+ pld->e[0].end = pld->e[0].base;
+ pld->e[0].layend = pld->e[0].end;
+ pld->e[0].prev = &pld->e[0];
+ pld->e[1].u.tmp = -1;
+ pld->e[1].base = pld->e[0].end + mmap_start - REG0S;
+ pld->e[1].end = pld->e[1].base;
+ pld->e[1].layend = pld->e[1].end;
+ pld->e[1].prev = &pld->e[1];
+ pld->e[2].u.tmp = -1;
+ pld->e[2].base = pld->e[1].end + first_start - REG1S;
+ pld->e[2].end = pld->e[1].base;
+ pld->e[2].layend = pld->e[2].end;
+ pld->e[2].prev = &pld->e[2];
+ for (cnt = 0, e = l->list; e != NULL; e = next, ++cnt)
+ {
+ next = e->next;
+ pld->ents[cnt].e = e;
+ pld->ents[cnt].base = e->base;
+ pld->ents[cnt].end = e->end;
+ pld->ents[cnt].layend = e->layend;
+ if (e->layend <= REG0S)
+ {
+ if (e->base < REG1S)
+ e->base = REG1S;
+ else if (e->base > first_start)
+ e->base = first_start;
+ if (e->layend < REG1S)
+ e->layend = REG1S;
+ else if (e->layend > first_start)
+ e->layend = first_start;
+ e->base += pld->e[1].end - REG1S;
+ e->layend += pld->e[1].end - REG1S;
+ list_append (&pld->e[1], e);
+ }
+ else if (e->base < mmap_start)
+ {
+ if (e->base < REG0S)
+ e->base = REG0S;
+ if (e->layend > mmap_start)
+ e->layend = mmap_start;
+ e->base = pld->e[0].end + mmap_start - e->layend;
+ e->layend = pld->e[0].layend + mmap_start - pld->ents[cnt].base;
+ list_append (&pld->e[0], e);
+ }
+ else if (e->base < REG0E)
+ {
+ if (e->layend > REG0E)
+ e->layend = REG0E;
+ e->base = REG1S + REG0E - e->layend;
+ e->layend = REG1S + REG0E - pld->ents[cnt].base;
+ list_append (&e0, e);
+ }
+ else if (e->layend >= last_start)
+ {
+ if (e->base < last_start)
+ e->base = last_start;
+ e->base += pld->e[2].end - last_start;
+ e->layend += pld->e[2].end - last_start;
+ list_append (&pld->e[2], e);
+ }
+ e->end = e->layend;
+ }
+
+ list_sort (&pld->e[0]);
+ if (e0.next == NULL)
+ l->list = &pld->e[0];
+ else
+ {
+ list_sort (&e0);
+ l->list = e0.next;
+ l->list->prev = pld->e[0].prev;
+ e0.prev->next = &pld->e[0];
+ pld->e[0].prev = e0.prev;
+ }
+
+ e0.prev = l->list->prev;
+ l->list->prev = pld->e[1].prev;
+ e0.prev->next = &pld->e[1];
+ pld->e[1].prev = e0.prev;
+
+ e0.prev = l->list->prev;
+ l->list->prev = pld->e[2].prev;
+ e0.prev->next = &pld->e[2];
+ pld->e[2].prev = e0.prev;
+
+ pld->mmap_start = mmap_start;
+ pld->first_start = first_start;
+ pld->last_start = last_start;
+
+ l->mmap_start = REG1S;
+ l->mmap_fin = pld->e[2].end + REG2E - last_start;
+ l->mmap_end = l->mmap_fin;
+ l->fakecnt = 3;
+ l->fake = pld->e;
+
+ return 0;
+}
+
+static int
+ppc_layout_libs_post (struct layout_libs *l)
+{
+ struct prelink_entry *e;
+ struct ppc_layout_data *pld = (struct ppc_layout_data *) l->arch_data;
+ Elf32_Addr base, end;
+ int i;
+
+ /* First fix up base and end fields we saved. */
+ for (i = 0; i < pld->cnt; ++i)
+ {
+ pld->ents[i].e->base = pld->ents[i].base;
+ pld->ents[i].e->layend = pld->ents[i].layend;
+ pld->ents[i].e->end = pld->ents[i].end;
+ pld->ents[i].e->done |= 0x40;
+ }
+ pld->e[0].done |= 0x40;
+ pld->e[1].done |= 0x40;
+ pld->e[2].done |= 0x40;
+
+ /* Now fix up the newly created items. */
+ for (e = l->list; e != NULL; e = e->next)
+ if (e->done & 0x40)
+ e->done &= ~0x40;
+ else
+ {
+ base = e->base;
+ end = e->layend;
+ if (e->base < pld->e[0].base)
+ {
+ e->base = REG1S + REG0E - end;
+ e->end += e->base - base;
+ e->layend = REG1S + REG0E - base;
+ }
+ else if (e->base < pld->e[1].base)
+ {
+ e->base = pld->e[0].end + pld->mmap_start - end;
+ e->end += e->base - base;
+ e->layend = pld->e[0].end + pld->mmap_start - base;
+ }
+ else if (e->base < pld->e[2].base)
+ {
+ e->base -= pld->e[1].end - REG1S;
+ e->end -= pld->e[1].end - REG1S;
+ e->layend -= pld->e[1].end - REG1S;
+ }
+ else
+ {
+ e->base -= pld->e[2].end - pld->last_start;
+ e->end -= pld->e[2].end - pld->last_start;
+ e->layend -= pld->e[2].end - pld->last_start;
+ }
+ }
+
+ for (i = 0; i < pld->cnt; ++i)
+ pld->ents[i].e->done &= ~0x40;
+
+ free (l->arch_data);
+ return 0;
+}
+
+PL_ARCH = {
+ .name = "PowerPC",
+ .class = ELFCLASS32,
+ .machine = EM_PPC,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_PPC_JMP_SLOT,
+ .R_COPY = R_PPC_COPY,
+ .R_RELATIVE = R_PPC_RELATIVE,
+ .dynamic_linker = "/lib/ld.so.1",
+ .adjust_dyn = ppc_adjust_dyn,
+ .adjust_rel = ppc_adjust_rel,
+ .adjust_rela = ppc_adjust_rela,
+ .prelink_rel = ppc_prelink_rel,
+ .prelink_rela = ppc_prelink_rela,
+ .prelink_conflict_rel = ppc_prelink_conflict_rel,
+ .prelink_conflict_rela = ppc_prelink_conflict_rela,
+ .apply_conflict_rela = ppc_apply_conflict_rela,
+ .apply_rel = ppc_apply_rel,
+ .apply_rela = ppc_apply_rela,
+ .rel_to_rela = ppc_rel_to_rela,
+ .need_rel_to_rela = ppc_need_rel_to_rela,
+ .reloc_size = ppc_reloc_size,
+ .reloc_class = ppc_reloc_class,
+ .max_reloc_size = 4,
+ .arch_pre_prelink = ppc_arch_pre_prelink,
+ .arch_prelink = ppc_arch_prelink,
+ .arch_undo_prelink = ppc_arch_undo_prelink,
+ .undo_prelink_rela = ppc_undo_prelink_rela,
+ .layout_libs_pre = ppc_layout_libs_pre,
+ .layout_libs_post = ppc_layout_libs_post,
+ /* This will need some changes in layout.c.
+ PowerPC prefers addresses right below REG0E
+ and can use the region above REG2S if libs don't fit. */
+ .mmap_base = REG1S,
+ .mmap_end = REG2E,
+ .max_page_size = 0x10000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-ppc64.c b/trunk/src/arch-ppc64.c
new file mode 100644
index 0000000..3b12dbe
--- /dev/null
+++ b/trunk/src/arch-ppc64.c
@@ -0,0 +1,856 @@
+/* Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
+ Written by Jakub Jelinek <jakub@redhat.com>, 2002.
+
+ 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+#include "layout.h"
+
+struct opd_rec
+{
+ GElf_Addr fn, toc, chain;
+};
+
+struct opd_lib
+{
+ GElf_Addr start, size;
+ GElf_Addr table[1];
+};
+
+static int
+ppc64_adjust_section (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust)
+{
+ if (dso->shdr[n].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[n].sh_name), ".got"))
+ {
+ Elf64_Addr data;
+
+ /* .got[0]-0x8000 points to .got, it needs to be adjusted. */
+ data = read_ube64 (dso, dso->shdr[n].sh_addr);
+ if (addr_to_sec (dso, data - 0x8000) == n
+ && data - 0x8000 == dso->shdr[n].sh_addr)
+ write_be64 (dso, dso->shdr[n].sh_addr, data + adjust);
+ }
+ return 0;
+}
+
+static int
+ppc64_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PPC64_GLINK && dyn->d_un.d_ptr >= start)
+ {
+ dyn->d_un.d_ptr += adjust;
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+ppc64_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: PowerPC64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc64_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (GELF_R_TYPE (rela->r_info) == R_PPC64_RELATIVE)
+ {
+ GElf_Addr val = read_ube64 (dso, rela->r_offset);
+
+ if (val == rela->r_addend && val >= start)
+ write_be64 (dso, rela->r_offset, val + adjust);
+ if (rela->r_addend >= start)
+ rela->r_addend += adjust;
+ }
+ return 0;
+}
+
+static int
+ppc64_prelink_rel (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: PowerPC64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+ppc64_fixup_plt (struct prelink_info *info, GElf_Rela *rela, GElf_Addr value)
+{
+ DSO *dso = info->dso;
+ int sec, i;
+ size_t n;
+ struct opd_rec rec;
+
+ if (value == 0)
+ {
+ rec.fn = 0;
+ rec.toc = 0;
+ rec.chain = 0;
+ }
+ else if ((sec = addr_to_sec (dso, value)) != -1)
+ {
+ rec.fn = read_ube64 (dso, value);
+ rec.toc = read_ube64 (dso, value + 8);
+ rec.chain = read_ube64 (dso, value + 16);
+ }
+ else
+ {
+ for (i = 0; i < info->ent->ndepends; ++i)
+ if (info->ent->depends[i]->opd
+ && info->ent->depends[i]->opd->start <= value
+ && (info->ent->depends[i]->opd->start
+ + info->ent->depends[i]->opd->size) > value)
+ break;
+
+ if (i == info->ent->ndepends)
+ {
+ error (0, 0, "%s: R_PPC64_JMP_SLOT doesn't resolve to an .opd address",
+ dso->filename);
+ return 1;
+ }
+ if ((value - info->ent->depends[i]->opd->start) % 8)
+ {
+ error (0, 0, "%s: R_PPC64_JMP_SLOT doesn't resolve to valid .opd section location",
+ dso->filename);
+ return 1;
+ }
+ n = (value - info->ent->depends[i]->opd->start) / 8;
+ rec.fn = info->ent->depends[i]->opd->table[n];
+ rec.toc = info->ent->depends[i]->opd->table[n + 1];
+ rec.chain = info->ent->depends[i]->opd->table[n + 2];
+ }
+ write_be64 (dso, rela->r_offset, rec.fn);
+ write_be64 (dso, rela->r_offset + 8, rec.toc);
+ write_be64 (dso, rela->r_offset + 16, rec.chain);
+ return 0;
+}
+
+static int
+ppc64_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_PPC64_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_PPC64_RELATIVE)
+ {
+ write_be64 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_GLOB_DAT:
+ case R_PPC64_ADDR64:
+ case R_PPC64_UADDR64:
+ write_be64 (dso, rela->r_offset, value);
+ break;
+ case R_PPC64_DTPREL64:
+ write_be64 (dso, rela->r_offset, value - 0x8000);
+ break;
+ case R_PPC64_ADDR32:
+ case R_PPC64_UADDR32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_PPC64_JMP_SLOT:
+ return ppc64_fixup_plt (info, rela, value);
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ case R_PPC64_ADDR16_LO:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_PPC64_DTPREL16:
+ case R_PPC64_DTPREL16_LO:
+ write_be16 (dso, rela->r_offset, value - 0x8000);
+ break;
+ case R_PPC64_ADDR16_HI:
+ case R_PPC64_DTPREL16_HA:
+ write_be16 (dso, rela->r_offset, value >> 16);
+ break;
+ case R_PPC64_DTPREL16_HI:
+ write_be16 (dso, rela->r_offset, (value - 0x8000) >> 16);
+ break;
+ case R_PPC64_ADDR16_HA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 16);
+ break;
+ case R_PPC64_ADDR16_HIGHER:
+ write_be16 (dso, rela->r_offset, value >> 32);
+ break;
+ case R_PPC64_ADDR16_HIGHERA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 32);
+ break;
+ case R_PPC64_ADDR16_HIGHEST:
+ write_be16 (dso, rela->r_offset, value >> 48);
+ break;
+ case R_PPC64_ADDR16_HIGHESTA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 48);
+ break;
+ case R_PPC64_ADDR16_LO_DS:
+ case R_PPC64_ADDR16_DS:
+ write_be16 (dso, rela->r_offset,
+ (value & 0xfffc) | read_ube16 (dso, rela->r_offset & 3));
+ break;
+ case R_PPC64_ADDR24:
+ write_be32 (dso, rela->r_offset,
+ (value & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003));
+ break;
+ case R_PPC64_ADDR14:
+ write_be32 (dso, rela->r_offset,
+ (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffff0003));
+ break;
+ case R_PPC64_ADDR14_BRTAKEN:
+ case R_PPC64_ADDR14_BRNTAKEN:
+ write_be32 (dso, rela->r_offset,
+ (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffdf0003)
+ | ((((GELF_R_TYPE (rela->r_info) == R_PPC64_ADDR14_BRTAKEN)
+ << 21)
+ ^ (value >> 42)) & 0x00200000));
+ break;
+ case R_PPC64_REL24:
+ write_be32 (dso, rela->r_offset,
+ ((value - rela->r_offset) & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003));
+ break;
+ case R_PPC64_REL32:
+ write_be32 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_PPC64_REL64:
+ write_be64 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ /* DTPMOD64 and TPREL* is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_PPC64_DTPMOD64:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_PPC64_DTPMOD64 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_PPC64_TPREL64:
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ case R_PPC64_TPREL16_HI:
+ case R_PPC64_TPREL16_HA:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ {
+ value += info->resolvetls->offset - 0x7000;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_TPREL64:
+ write_be64 (dso, rela->r_offset, value);
+ break;
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_PPC64_TPREL16_HI:
+ write_be16 (dso, rela->r_offset, value >> 16);
+ break;
+ case R_PPC64_TPREL16_HA:
+ write_be16 (dso, rela->r_offset, (value + 0x8000) >> 16);
+ break;
+ }
+ }
+ break;
+ case R_PPC64_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_PPC64_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown ppc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc64_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_ADDR64:
+ case R_PPC64_UADDR64:
+ buf_write_be64 (buf, rela->r_addend);
+ break;
+ case R_PPC64_ADDR32:
+ case R_PPC64_UADDR32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ buf_write_be16 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+ppc64_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: PowerPC64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+ppc64_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_NONE:
+ break;
+ case R_PPC64_GLOB_DAT:
+ case R_PPC64_ADDR64:
+ case R_PPC64_UADDR64:
+ buf_write_be64 (buf, value);
+ break;
+ case R_PPC64_ADDR32:
+ case R_PPC64_UADDR32:
+ buf_write_be32 (buf, value);
+ break;
+ case R_PPC64_ADDR16_HA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HI:
+ value = value >> 16;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ case R_PPC64_ADDR16_LO:
+ buf_write_be16 (buf, value);
+ break;
+ case R_PPC64_ADDR16_HIGHERA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HIGHER:
+ buf_write_be16 (buf, value >> 32);
+ break;
+ case R_PPC64_ADDR16_HIGHESTA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HIGHEST:
+ buf_write_be16 (buf, value >> 48);
+ break;
+ case R_PPC64_ADDR16_LO_DS:
+ case R_PPC64_ADDR16_DS:
+ buf_write_be16 (buf, (value & 0xfffc)
+ | (buf_read_ube16 (buf) & 3));
+ break;
+ case R_PPC64_ADDR24:
+ buf_write_be32 (buf, (value & 0x03fffffc)
+ | (buf_read_ube32 (buf) & 0xfc000003));
+ break;
+ case R_PPC64_ADDR14:
+ buf_write_be32 (buf, (value & 0xfffc)
+ | (buf_read_ube32 (buf) & 0xffff0003));
+ break;
+ case R_PPC64_ADDR14_BRTAKEN:
+ case R_PPC64_ADDR14_BRNTAKEN:
+ buf_write_be32 (buf, (value & 0xfffc)
+ | (buf_read_ube32 (buf) & 0xffdf0003)
+ | ((((GELF_R_TYPE (rela->r_info)
+ == R_PPC64_ADDR14_BRTAKEN) << 21)
+ ^ (value >> 42)) & 0x00200000));
+ break;
+ case R_PPC64_REL24:
+ buf_write_be32 (buf, ((value - rela->r_offset) & 0x03fffffc)
+ | (buf_read_ube32 (buf) & 0xfc000003));
+ break;
+ case R_PPC64_REL32:
+ buf_write_be32 (buf, value - rela->r_offset);
+ break;
+ case R_PPC64_REL64:
+ buf_write_be64 (buf, value - rela->r_offset);
+ break;
+ case R_PPC64_RELATIVE:
+ error (0, 0, "%s: R_PPC64_RELATIVE in ET_EXEC object?",
+ info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc64_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: PowerPC64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc64_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+ int r_type;
+
+ if (GELF_R_TYPE (rela->r_info) == R_PPC64_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_PPC64_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPREL relocs need conflicts. */
+ case R_PPC64_DTPMOD64:
+ case R_PPC64_TPREL64:
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ case R_PPC64_TPREL16_HI:
+ case R_PPC64_TPREL16_HA:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPREL wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_DTPREL64:
+ case R_PPC64_DTPREL16:
+ case R_PPC64_DTPREL16_LO:
+ case R_PPC64_DTPREL16_HI:
+ case R_PPC64_DTPREL16_HA:
+ return 0;
+ }
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ value += rela->r_addend;
+ r_type = GELF_R_TYPE (rela->r_info);
+ switch (r_type)
+ {
+ case R_PPC64_GLOB_DAT:
+ r_type = R_PPC64_ADDR64;
+ break;
+ case R_PPC64_ADDR64:
+ case R_PPC64_UADDR64:
+ case R_PPC64_JMP_SLOT:
+ break;
+ case R_PPC64_ADDR32:
+ case R_PPC64_UADDR32:
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_ADDR16_HA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HI:
+ value = value >> 16;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ case R_PPC64_ADDR16_LO:
+ if (r_type != R_PPC64_UADDR16)
+ r_type = R_PPC64_ADDR16;
+ value = ((value & 0xffff) ^ 0x8000) - 0x8000;
+ break;
+ case R_PPC64_ADDR16_HIGHERA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HIGHER:
+ r_type = R_PPC64_ADDR16;
+ value = (((value >> 32) & 0xffff) ^ 0x8000) - 0x8000;
+ break;
+ case R_PPC64_ADDR16_HIGHESTA:
+ value += 0x8000;
+ /* FALLTHROUGH */
+ case R_PPC64_ADDR16_HIGHEST:
+ r_type = R_PPC64_ADDR16;
+ value = ((Elf64_Sxword) value) >> 48;
+ break;
+ case R_PPC64_ADDR16_LO_DS:
+ case R_PPC64_ADDR16_DS:
+ r_type = R_PPC64_ADDR16;
+ value = ((value & 0xffff) ^ 0x8000) - 0x8000;
+ value |= read_ube16 (dso, rela->r_offset) & 3;
+ break;
+ case R_PPC64_ADDR24:
+ r_type = R_PPC64_ADDR32;
+ value = (value & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_ADDR14:
+ r_type = R_PPC64_ADDR32;
+ value = (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffff0003);
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_ADDR14_BRTAKEN:
+ case R_PPC64_ADDR14_BRNTAKEN:
+ r_type = R_PPC64_ADDR32;
+ value = (value & 0xfffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xffdf0003)
+ | ((((r_type == R_PPC64_ADDR14_BRTAKEN) << 21)
+ ^ (value >> 42)) & 0x00200000);
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_REL24:
+ r_type = R_PPC64_ADDR32;
+ value = ((value - rela->r_offset) & 0x03fffffc)
+ | (read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_REL32:
+ r_type = R_PPC64_ADDR32;
+ value -= rela->r_offset;
+ value = (Elf32_Sword) value;
+ break;
+ case R_PPC64_REL64:
+ r_type = R_PPC64_ADDR64;
+ value -= rela->r_offset;
+ break;
+ case R_PPC64_DTPMOD64:
+ case R_PPC64_DTPREL64:
+ case R_PPC64_DTPREL16:
+ case R_PPC64_DTPREL16_LO:
+ case R_PPC64_DTPREL16_HI:
+ case R_PPC64_DTPREL16_HA:
+ case R_PPC64_TPREL64:
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ case R_PPC64_TPREL16_HI:
+ case R_PPC64_TPREL16_HA:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ r_type = R_PPC64_ADDR16;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_DTPMOD64:
+ r_type = R_PPC64_ADDR64;
+ value = tls->modid;
+ break;
+ case R_PPC64_DTPREL64:
+ r_type = R_PPC64_ADDR64;
+ value -= 0x8000;
+ break;
+ case R_PPC64_DTPREL16:
+ case R_PPC64_DTPREL16_LO:
+ value -= 0x8000;
+ break;
+ case R_PPC64_DTPREL16_HI:
+ value = (value - 0x8000) >> 16;
+ break;
+ case R_PPC64_DTPREL16_HA:
+ value >>= 16;
+ break;
+ case R_PPC64_TPREL64:
+ r_type = R_PPC64_ADDR64;
+ value += tls->offset - 0x7000;
+ break;
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ value += tls->offset - 0x7000;
+ break;
+ case R_PPC64_TPREL16_HI:
+ value = (value + tls->offset - 0x7000) >> 16;
+ break;
+ case R_PPC64_TPREL16_HA:
+ value = (value + tls->offset - 0x7000 + 0x8000) >> 16;
+ break;
+ }
+ if (r_type == R_PPC64_ADDR16)
+ value = ((value & 0xffff) ^ 0x8000) - 0x8000;
+ break;
+ default:
+ error (0, 0, "%s: Unknown PowerPC64 relocation type %d", dso->filename,
+ r_type);
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, r_type);
+ ret->r_addend = value;
+ return 0;
+}
+
+static int
+ppc64_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: PowerPC64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+ppc64_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+ppc64_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_PPC64_NONE:
+ return 0;
+ case R_PPC64_JMP_SLOT:
+ /* .plt section will become SHT_NOBITS. */
+ return 0;
+ case R_PPC64_RELATIVE:
+ case R_PPC64_ADDR64:
+ write_be64 (dso, rela->r_offset, rela->r_addend);
+ break;
+ case R_PPC64_GLOB_DAT:
+ case R_PPC64_UADDR64:
+ case R_PPC64_DTPREL64:
+ case R_PPC64_TPREL64:
+ case R_PPC64_DTPMOD64:
+ case R_PPC64_REL64:
+ write_be64 (dso, rela->r_offset, 0);
+ break;
+ case R_PPC64_ADDR32:
+ case R_PPC64_UADDR32:
+ case R_PPC64_REL32:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_PPC64_ADDR16_HA:
+ case R_PPC64_DTPREL16_HA:
+ case R_PPC64_TPREL16_HA:
+ case R_PPC64_ADDR16_HI:
+ case R_PPC64_DTPREL16_HI:
+ case R_PPC64_TPREL16_HI:
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ case R_PPC64_ADDR16_LO:
+ case R_PPC64_DTPREL16:
+ case R_PPC64_TPREL16:
+ case R_PPC64_DTPREL16_LO:
+ case R_PPC64_TPREL16_LO:
+ case R_PPC64_ADDR16_HIGHERA:
+ case R_PPC64_ADDR16_HIGHER:
+ case R_PPC64_ADDR16_HIGHESTA:
+ case R_PPC64_ADDR16_HIGHEST:
+ case R_PPC64_ADDR16_LO_DS:
+ case R_PPC64_ADDR16_DS:
+ write_be16 (dso, rela->r_offset, 0);
+ break;
+ case R_PPC64_ADDR24:
+ case R_PPC64_REL24:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xfc000003);
+ break;
+ case R_PPC64_ADDR14:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffff0003);
+ break;
+ case R_PPC64_ADDR14_BRTAKEN:
+ case R_PPC64_ADDR14_BRNTAKEN:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffdf0003);
+ break;
+ case R_PPC64_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_PPC64_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown ppc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+ppc64_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_PPC64_ADDR16:
+ case R_PPC64_UADDR16:
+ case R_PPC64_ADDR16_LO:
+ case R_PPC64_ADDR16_HA:
+ case R_PPC64_ADDR16_HI:
+ case R_PPC64_ADDR16_LO_DS:
+ case R_PPC64_ADDR16_DS:
+ case R_PPC64_ADDR16_HIGHER:
+ case R_PPC64_ADDR16_HIGHERA:
+ case R_PPC64_ADDR16_HIGHEST:
+ case R_PPC64_ADDR16_HIGHESTA:
+ case R_PPC64_DTPREL16:
+ case R_PPC64_DTPREL16_LO:
+ case R_PPC64_DTPREL16_HI:
+ case R_PPC64_DTPREL16_HA:
+ case R_PPC64_TPREL16:
+ case R_PPC64_TPREL16_LO:
+ case R_PPC64_TPREL16_HI:
+ case R_PPC64_TPREL16_HA:
+ return 2;
+ case R_PPC64_GLOB_DAT:
+ case R_PPC64_ADDR64:
+ case R_PPC64_UADDR64:
+ case R_PPC64_REL64:
+ case R_PPC64_DTPMOD64:
+ case R_PPC64_DTPREL64:
+ case R_PPC64_TPREL64:
+ return 8;
+ default:
+ break;
+ }
+ return 4;
+}
+
+static int
+ppc64_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_PPC64_COPY: return RTYPE_CLASS_COPY;
+ case R_PPC64_ADDR24: return RTYPE_CLASS_PLT;
+ default:
+ if (reloc_type >= R_PPC64_DTPMOD64
+ && reloc_type <= R_PPC64_TPREL16_HIGHESTA)
+ return RTYPE_CLASS_TLS;
+ return RTYPE_CLASS_VALID;
+ }
+}
+
+static int
+ppc64_read_opd (DSO *dso, struct prelink_entry *ent)
+{
+ int opd;
+ GElf_Addr n, s;
+
+ free (ent->opd);
+ ent->opd = NULL;
+ for (opd = 1; opd < dso->ehdr.e_shnum; ++opd)
+ if (dso->shdr[opd].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[opd].sh_name), ".opd"))
+ break;
+ if (opd == dso->ehdr.e_shnum)
+ return 0;
+ ent->opd = malloc (sizeof (struct opd_lib) + dso->shdr[opd].sh_size);
+ /* The error will happen only when we'll need the opd. */
+ if (ent->opd == NULL)
+ return 0;
+ s = dso->shdr[opd].sh_addr;
+ for (n = 0; n < dso->shdr[opd].sh_size / 8; ++n, s += 8)
+ ent->opd->table[n] = read_ube64 (dso, s);
+ ent->opd->start = dso->shdr[opd].sh_addr;
+ ent->opd->size = dso->shdr[opd].sh_size;
+ return 0;
+}
+
+static int
+ppc64_free_opd (struct prelink_entry *ent)
+{
+ free (ent->opd);
+ ent->opd = NULL;
+ return 0;
+}
+
+PL_ARCH = {
+ .name = "PowerPC",
+ .class = ELFCLASS64,
+ .machine = EM_PPC64,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_PPC64_JMP_SLOT,
+ .R_COPY = R_PPC64_COPY,
+ .R_RELATIVE = R_PPC64_RELATIVE,
+ .dynamic_linker = "/lib64/ld64.so.1",
+ .adjust_section = ppc64_adjust_section,
+ .adjust_dyn = ppc64_adjust_dyn,
+ .adjust_rel = ppc64_adjust_rel,
+ .adjust_rela = ppc64_adjust_rela,
+ .prelink_rel = ppc64_prelink_rel,
+ .prelink_rela = ppc64_prelink_rela,
+ .prelink_conflict_rel = ppc64_prelink_conflict_rel,
+ .prelink_conflict_rela = ppc64_prelink_conflict_rela,
+ .apply_conflict_rela = ppc64_apply_conflict_rela,
+ .apply_rel = ppc64_apply_rel,
+ .apply_rela = ppc64_apply_rela,
+ .rel_to_rela = ppc64_rel_to_rela,
+ .need_rel_to_rela = ppc64_need_rel_to_rela,
+ .reloc_size = ppc64_reloc_size,
+ .reloc_class = ppc64_reloc_class,
+ .read_opd = ppc64_read_opd,
+ .free_opd = ppc64_free_opd,
+ .max_reloc_size = 8,
+ .undo_prelink_rela = ppc64_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x8000000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x8001000000LL,
+ .mmap_end = 0x8100000000LL,
+ .max_page_size = 0x10000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-s390.c b/trunk/src/arch-s390.c
new file mode 100644
index 0000000..4be0dcb
--- /dev/null
+++ b/trunk/src/arch-s390.c
@@ -0,0 +1,513 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+s390_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_ube32 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_be32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ube32 (dso, dyn->d_un.d_ptr + 4);
+ /* If .got.plt[1] points to .plt + 0x2c, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 0x2c
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_be32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+s390_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr addr;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_RELATIVE:
+ if ((Elf32_Addr) rela->r_addend >= start)
+ {
+ addr = read_ube32 (dso, rela->r_offset);
+ if (addr == rela->r_addend)
+ write_be32 (dso, rela->r_offset, addr + adjust);
+ rela->r_addend += (Elf32_Sword) adjust;
+ }
+ break;
+ case R_390_JMP_SLOT:
+ addr = read_ube32 (dso, rela->r_offset);
+ if (addr >= start)
+ write_be32 (dso, rela->r_offset, addr + adjust);
+ break;
+ }
+ return 0;
+}
+
+static int
+s390_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+s390_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_390_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_390_RELATIVE)
+ {
+ write_be32 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_390_32:
+ write_be32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_390_PC32:
+ write_be32 (dso, rela->r_offset, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_390_TLS_DTPOFF:
+ write_be32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ /* DTPMOD and TPOFF is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_390_TLS_DTPMOD:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_390_TLS_DTPMOD reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_390_TLS_TPOFF:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_be32 (dso, rela->r_offset,
+ value + rela->r_addend - info->resolvetls->offset);
+ break;
+ case R_390_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_390_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown S390 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+s390_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+s390_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_NONE:
+ break;
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ buf_write_be32 (buf, value);
+ break;
+ case R_390_32:
+ buf_write_be32 (buf, value + rela->r_addend);
+ break;
+ case R_390_PC32:
+ buf_write_be32 (buf, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_390_COPY:
+ abort ();
+ case R_390_RELATIVE:
+ error (0, 0, "%s: R_390_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_390_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_390_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_TPOFF:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_390_TLS_DTPOFF
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, R_390_32);
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ ret->r_addend = (Elf32_Sword) value;
+ break;
+ case R_390_32:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
+ break;
+ case R_390_PC32:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend - rela->r_offset);
+ break;
+ case R_390_COPY:
+ error (0, 0, "R_390_COPY should not be present in shared libraries");
+ return 1;
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_TLS_DTPMOD:
+ ret->r_addend = tls->modid;
+ break;
+ case R_390_TLS_DTPOFF:
+ ret->r_addend = value + rela->r_addend;
+ break;
+ case R_390_TLS_TPOFF:
+ ret->r_addend = value + rela->r_addend - tls->offset;
+ break;
+ }
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown S390 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+s390_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 0x2c into got[1].
+ .plt + 0x2c is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = dso->shdr[i].sh_addr + 0x2c;
+ write_be32 (dso, dso->info[DT_PLTGOT] + 4, data);
+ }
+
+ return 0;
+}
+
+static int
+s390_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt + 0x2c. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_ube32 (dso, dso->info[DT_PLTGOT] + 4);
+ if (data == dso->shdr[i].sh_addr + 0x2c)
+ write_be32 (dso, dso->info[DT_PLTGOT] + 4, 0);
+ }
+
+ return 0;
+}
+
+static int
+s390_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_NONE:
+ case R_390_RELATIVE:
+ break;
+ case R_390_JMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_390_JMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf32_Addr data = read_ube32 (dso, dso->shdr[sec].sh_addr + 4);
+
+ assert (rela->r_offset >= dso->shdr[sec].sh_addr + 12);
+ assert (((rela->r_offset - dso->shdr[sec].sh_addr) & 3) == 0);
+ write_be32 (dso, rela->r_offset,
+ 8 * (rela->r_offset - dso->shdr[sec].sh_addr - 12)
+ + data);
+ }
+ break;
+ case R_390_GLOB_DAT:
+ case R_390_32:
+ case R_390_PC32:
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_390_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_390_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown s390 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390_reloc_size (int reloc_type)
+{
+ return 4;
+}
+
+static int
+s390_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_390_COPY: return RTYPE_CLASS_COPY;
+ case R_390_JMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "S390",
+ .class = ELFCLASS32,
+ .machine = EM_S390,
+ .alternate_machine = { 0xA390 },
+ .R_JMP_SLOT = R_390_JMP_SLOT,
+ .R_COPY = R_390_COPY,
+ .R_RELATIVE = R_390_RELATIVE,
+ .dynamic_linker = "/lib/ld.so.1",
+ .adjust_dyn = s390_adjust_dyn,
+ .adjust_rel = s390_adjust_rel,
+ .adjust_rela = s390_adjust_rela,
+ .prelink_rel = s390_prelink_rel,
+ .prelink_rela = s390_prelink_rela,
+ .prelink_conflict_rel = s390_prelink_conflict_rel,
+ .prelink_conflict_rela = s390_prelink_conflict_rela,
+ .apply_conflict_rela = s390_apply_conflict_rela,
+ .apply_rel = s390_apply_rel,
+ .apply_rela = s390_apply_rela,
+ .rel_to_rela = s390_rel_to_rela,
+ .need_rel_to_rela = s390_need_rel_to_rela,
+ .reloc_size = s390_reloc_size,
+ .reloc_class = s390_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = s390_arch_prelink,
+ .arch_undo_prelink = s390_arch_undo_prelink,
+ .undo_prelink_rela = s390_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x40000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x41000000,
+ .mmap_end = 0x50000000,
+ .max_page_size = 0x1000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-s390x.c b/trunk/src/arch-s390x.c
new file mode 100644
index 0000000..1e381c7
--- /dev/null
+++ b/trunk/src/arch-s390x.c
@@ -0,0 +1,629 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+s390x_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_ube64 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_be64 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ube64 (dso, dyn->d_un.d_ptr + 8);
+ /* If .got.plt[1] points to .plt + 0x2e, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 0x2e
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_be64 (dso, dyn->d_un.d_ptr + 8, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+s390x_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390x_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf64_Addr addr;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_RELATIVE:
+ if (rela->r_addend >= start)
+ {
+ addr = read_ube64 (dso, rela->r_offset);
+ if (addr == rela->r_addend)
+ write_be64 (dso, rela->r_offset, addr + adjust);
+ rela->r_addend += adjust;
+ }
+ break;
+ case R_390_JMP_SLOT:
+ addr = read_ube64 (dso, rela->r_offset);
+ if (addr >= start)
+ write_be64 (dso, rela->r_offset, addr + adjust);
+ break;
+ }
+ return 0;
+}
+
+static int
+s390x_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+s390x_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_390_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_390_RELATIVE)
+ {
+ write_be64 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ case R_390_64:
+ write_be64 (dso, rela->r_offset, value);
+ break;
+ case R_390_PC64:
+ write_be64 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_390_32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_390_PC32:
+ write_be32 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_390_PC32DBL:
+ case R_390_PLT32DBL:
+ write_be32 (dso, rela->r_offset,
+ ((Elf32_Sword) (value - rela->r_offset)) >> 1);
+ break;
+ case R_390_16:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_390_PC16:
+ write_be16 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_390_PC16DBL:
+ case R_390_PLT16DBL:
+ write_be16 (dso, rela->r_offset,
+ ((int16_t) (value - rela->r_offset)) >> 1);
+ break;
+ case R_390_8:
+ write_8 (dso, rela->r_offset, value);
+ break;
+ case R_390_TLS_DTPOFF:
+ write_be64 (dso, rela->r_offset, value);
+ break;
+ /* DTPMOD and TPOFF is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_390_TLS_DTPMOD:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_390_TLS_DTPMOD reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_390_TLS_TPOFF:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_be64 (dso, rela->r_offset, value - info->resolvetls->offset);
+ break;
+ case R_390_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_390_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown S390 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390x_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_64:
+ buf_write_be64 (buf, rela->r_addend);
+ break;
+ case R_390_32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ case R_390_16:
+ buf_write_be16 (buf, rela->r_addend);
+ break;
+ case R_390_8:
+ buf_write_8 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+s390x_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+s390x_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_NONE:
+ break;
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ case R_390_64:
+ buf_write_be64 (buf, value);
+ break;
+ case R_390_PC64:
+ buf_write_be64 (buf, value - rela->r_offset);
+ break;
+ case R_390_32:
+ buf_write_be32 (buf, value);
+ break;
+ case R_390_PC32:
+ buf_write_be32 (buf, value - rela->r_offset);
+ break;
+ case R_390_PC32DBL:
+ case R_390_PLT32DBL:
+ buf_write_be32 (buf, ((Elf32_Sword) (value - rela->r_offset)) >> 1);
+ break;
+ case R_390_16:
+ buf_write_be16 (buf, value);
+ break;
+ case R_390_PC16:
+ buf_write_be16 (buf, value - rela->r_offset);
+ break;
+ case R_390_PC16DBL:
+ case R_390_PLT16DBL:
+ buf_write_be16 (buf, ((int16_t) (value - rela->r_offset)) >> 1);
+ break;
+ case R_390_8:
+ buf_write_8 (buf, value);
+ break;
+ case R_390_COPY:
+ abort ();
+ case R_390_RELATIVE:
+ error (0, 0, "%s: R_390_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390x_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390x_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+ int r_type;
+
+ if (GELF_R_TYPE (rela->r_info) == R_390_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_390_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_TPOFF:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_390_TLS_DTPOFF
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ r_type = GELF_R_TYPE (rela->r_info);
+ value += rela->r_addend;
+ switch (r_type)
+ {
+ case R_390_PC64:
+ value -= rela->r_offset;
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ case R_390_64:
+ ret->r_addend = value;
+ r_type = R_390_64;
+ break;
+ case R_390_PC32:
+ value -= rela->r_offset;
+ case R_390_32:
+ ret->r_addend = (Elf32_Addr) value;
+ r_type = R_390_32;
+ break;
+ case R_390_PC32DBL:
+ case R_390_PLT32DBL:
+ ret->r_addend
+ = (Elf32_Addr) (((Elf32_Sword) (value - rela->r_offset)) >> 1);
+ r_type = R_390_32;
+ break;
+ case R_390_PC16:
+ value -= rela->r_offset;
+ case R_390_16:
+ ret->r_addend = (Elf32_Half) value;
+ r_type = R_390_16;
+ break;
+ case R_390_PC16DBL:
+ case R_390_PLT16DBL:
+ ret->r_addend = (Elf32_Half) (((int16_t) (value - rela->r_offset)) >> 1);
+ r_type = R_390_16;
+ break;
+ case R_390_8:
+ ret->r_addend = value & 0xff;
+ break;
+ case R_390_COPY:
+ error (0, 0, "R_390_COPY should not be present in shared libraries");
+ return 1;
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ switch (r_type)
+ {
+ case R_390_TLS_DTPMOD:
+ ret->r_addend = tls->modid;
+ break;
+ case R_390_TLS_DTPOFF:
+ ret->r_addend = value;
+ break;
+ case R_390_TLS_TPOFF:
+ ret->r_addend = value - tls->offset;
+ break;
+ }
+ r_type = R_390_64;
+ break;
+ default:
+ error (0, 0, "%s: Unknown S390 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, r_type);
+ return 0;
+}
+
+static int
+s390x_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: S390 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+s390x_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+s390x_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 0x2e into got[1].
+ .plt + 0x2e is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = dso->shdr[i].sh_addr + 0x2e;
+ write_be64 (dso, dso->info[DT_PLTGOT] + 8, data);
+ }
+
+ return 0;
+}
+
+static int
+s390x_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt + 0x2e. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_ube64 (dso, dso->info[DT_PLTGOT] + 8);
+ if (data == dso->shdr[i].sh_addr + 0x2e)
+ write_be64 (dso, dso->info[DT_PLTGOT] + 8, 0);
+ }
+
+ return 0;
+}
+
+static int
+s390x_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_390_NONE:
+ case R_390_RELATIVE:
+ break;
+ case R_390_JMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_390_JMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf64_Addr data = read_ube64 (dso, dso->shdr[sec].sh_addr + 8);
+
+ assert (rela->r_offset >= dso->shdr[sec].sh_addr + 24);
+ assert (((rela->r_offset - dso->shdr[sec].sh_addr) & 7) == 0);
+ write_be64 (dso, rela->r_offset,
+ 4 * (rela->r_offset - dso->shdr[sec].sh_addr - 24)
+ + data);
+ }
+ break;
+ case R_390_GLOB_DAT:
+ case R_390_64:
+ case R_390_PC64:
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ write_be64 (dso, rela->r_offset, 0);
+ break;
+ case R_390_32:
+ case R_390_PC32:
+ case R_390_PC32DBL:
+ case R_390_PLT32DBL:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_390_16:
+ case R_390_PC16:
+ case R_390_PC16DBL:
+ case R_390_PLT16DBL:
+ write_be16 (dso, rela->r_offset, 0);
+ break;
+ case R_390_8:
+ write_8 (dso, rela->r_offset, 0);
+ break;
+ case R_390_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_390_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown s390x relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+s390x_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ case R_390_64:
+ case R_390_PC64:
+ return 8;
+ case R_390_32:
+ case R_390_PC32:
+ case R_390_PC32DBL:
+ case R_390_PLT32DBL:
+ default:
+ return 4;
+ case R_390_16:
+ case R_390_PC16:
+ case R_390_PC16DBL:
+ case R_390_PLT16DBL:
+ return 2;
+ case R_390_8:
+ return 1;
+ }
+}
+
+static int
+s390x_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_390_COPY: return RTYPE_CLASS_COPY;
+ case R_390_JMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_390_TLS_DTPMOD:
+ case R_390_TLS_DTPOFF:
+ case R_390_TLS_TPOFF:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "S390",
+ .class = ELFCLASS64,
+ .machine = EM_S390,
+ .alternate_machine = { 0xA390 },
+ .R_JMP_SLOT = R_390_JMP_SLOT,
+ .R_COPY = R_390_COPY,
+ .R_RELATIVE = R_390_RELATIVE,
+ .dynamic_linker = "/lib/ld64.so.1",
+ .adjust_dyn = s390x_adjust_dyn,
+ .adjust_rel = s390x_adjust_rel,
+ .adjust_rela = s390x_adjust_rela,
+ .prelink_rel = s390x_prelink_rel,
+ .prelink_rela = s390x_prelink_rela,
+ .prelink_conflict_rel = s390x_prelink_conflict_rel,
+ .prelink_conflict_rela = s390x_prelink_conflict_rela,
+ .apply_conflict_rela = s390x_apply_conflict_rela,
+ .apply_rel = s390x_apply_rel,
+ .apply_rela = s390x_apply_rela,
+ .rel_to_rela = s390x_rel_to_rela,
+ .need_rel_to_rela = s390x_need_rel_to_rela,
+ .reloc_size = s390x_reloc_size,
+ .reloc_class = s390x_reloc_class,
+ .max_reloc_size = 8,
+ .arch_prelink = s390x_arch_prelink,
+ .arch_undo_prelink = s390x_arch_undo_prelink,
+ .undo_prelink_rela = s390x_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x4000000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x4010000000LL,
+ .mmap_end = 0x5000000000LL,
+ .max_page_size = 0x1000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-sh.c b/trunk/src/arch-sh.c
new file mode 100644
index 0000000..6ddf5f6
--- /dev/null
+++ b/trunk/src/arch-sh.c
@@ -0,0 +1,445 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+sh_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_une32 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_ne32 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_une32 (dso, dyn->d_un.d_ptr + 4);
+ /* If .got.plt[1] points to .plt + 36, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 36
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_ne32 (dso, dyn->d_un.d_ptr + 4, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+sh_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: SH doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sh_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf32_Addr data;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_RELATIVE:
+ if (rela->r_addend && (Elf32_Addr) rela->r_addend >= start)
+ {
+ rela->r_addend += (Elf32_Sword) adjust;
+ break;
+ }
+ /* FALLTHROUGH */
+ case R_SH_JMP_SLOT:
+ data = read_une32 (dso, rela->r_offset);
+ if (data >= start)
+ write_ne32 (dso, rela->r_offset, data + adjust);
+ break;
+ break;
+ }
+ return 0;
+}
+
+static int
+sh_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: SH doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+sh_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ dso = info->dso;
+ if (GELF_R_TYPE (rela->r_info) == R_SH_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_SH_RELATIVE)
+ {
+ if (rela->r_addend)
+ write_ne32 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_GLOB_DAT:
+ case R_SH_JMP_SLOT:
+ case R_SH_DIR32:
+ write_ne32 (dso, rela->r_offset, value);
+ break;
+ case R_SH_REL32:
+ write_ne32 (dso, rela->r_offset, value - rela->r_addend);
+ break;
+ case R_SH_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SH_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sh_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_GLOB_DAT:
+ case R_SH_JMP_SLOT:
+ case R_SH_DIR32:
+ buf_write_ne32 (info->dso, buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+sh_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: SH doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+sh_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_NONE:
+ break;
+ case R_SH_GLOB_DAT:
+ case R_SH_JMP_SLOT:
+ case R_SH_DIR32:
+ buf_write_ne32 (info->dso, buf, value);
+ break;
+ case R_SH_REL32:
+ buf_write_ne32 (info->dso, buf, value - rela->r_offset);
+ break;
+ case R_SH_COPY:
+ abort ();
+ case R_SH_RELATIVE:
+ error (0, 0, "%s: R_SH_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sh_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: SH doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sh_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_SH_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_SH_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ return 0;
+ value = conflict_lookup_value (conflict);
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_REL32:
+ value -= rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, R_SH_DIR32);
+ /* FALLTHROUGH */
+ case R_SH_DIR32:
+ if ((rela->r_offset & 3) == 0)
+ ret->r_info = GELF_R_INFO (0, R_SH_GLOB_DAT);
+ /* FALLTHROUGH */
+ case R_SH_GLOB_DAT:
+ case R_SH_JMP_SLOT:
+ ret->r_addend = (Elf32_Sword) (value + rela->r_addend);
+ break;
+ case R_SH_COPY:
+ error (0, 0, "R_SH_COPY should not be present in shared libraries");
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sh_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ return 0;
+}
+
+static int
+sh_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+sh_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 36 into got[1].
+ .plt + 36 is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = dso->shdr[i].sh_addr + 36;
+ write_ne32 (dso, dso->info[DT_PLTGOT] + 4, data);
+ }
+
+ return 0;
+}
+
+static int
+sh_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt + 36. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf32_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_une32 (dso, dso->info[DT_PLTGOT] + 4);
+ if (data == dso->shdr[i].sh_addr + 36)
+ write_ne32 (dso, dso->info[DT_PLTGOT] + 4, 0);
+ }
+
+ return 0;
+}
+
+static int
+sh_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SH_NONE:
+ break;
+ case R_SH_RELATIVE:
+ if (rela->r_addend)
+ write_ne32 (dso, rela->r_offset, 0);
+ break;
+ case R_SH_JMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_SH_JMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf32_Addr data = read_une32 (dso, dso->shdr[sec].sh_addr + 4);
+
+ assert (rela->r_offset >= dso->shdr[sec].sh_addr + 12);
+ assert (((rela->r_offset - dso->shdr[sec].sh_addr) & 3) == 0);
+ write_ne32 (dso, rela->r_offset,
+ 7 * (rela->r_offset - dso->shdr[sec].sh_addr - 12)
+ + data);
+ }
+ break;
+ case R_SH_GLOB_DAT:
+ case R_SH_DIR32:
+ case R_SH_REL32:
+ write_ne32 (dso, rela->r_offset, 0);
+ break;
+ case R_SH_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SH_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sh relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sh_reloc_size (int reloc_type)
+{
+ return 4;
+}
+
+static int
+sh_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_SH_COPY: return RTYPE_CLASS_COPY;
+ case R_SH_JMP_SLOT: return RTYPE_CLASS_PLT;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "SuperH",
+ .class = ELFCLASS32,
+ .machine = EM_SH,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_SH_JMP_SLOT,
+ .R_COPY = R_SH_COPY,
+ .R_RELATIVE = R_SH_RELATIVE,
+ .dynamic_linker = "/lib/ld-linux.so.2",
+ .adjust_dyn = sh_adjust_dyn,
+ .adjust_rel = sh_adjust_rel,
+ .adjust_rela = sh_adjust_rela,
+ .prelink_rel = sh_prelink_rel,
+ .prelink_rela = sh_prelink_rela,
+ .prelink_conflict_rel = sh_prelink_conflict_rel,
+ .prelink_conflict_rela = sh_prelink_conflict_rela,
+ .apply_conflict_rela = sh_apply_conflict_rela,
+ .apply_rel = sh_apply_rel,
+ .apply_rela = sh_apply_rela,
+ .rel_to_rela = sh_rel_to_rela,
+ .need_rel_to_rela = sh_need_rel_to_rela,
+ .reloc_size = sh_reloc_size,
+ .reloc_class = sh_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = sh_arch_prelink,
+ .arch_undo_prelink = sh_arch_undo_prelink,
+ .undo_prelink_rela = sh_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x29555000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x30000000,
+ .mmap_end = 0x40000000,
+ .max_page_size = 0x10000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-sparc.c b/trunk/src/arch-sparc.c
new file mode 100644
index 0000000..98bacac
--- /dev/null
+++ b/trunk/src/arch-sparc.c
@@ -0,0 +1,637 @@
+/* Copyright (C) 2001, 2002, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+sparc_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".got"))
+ {
+ Elf32_Addr data;
+
+ data = read_ube32 (dso, dso->shdr[i].sh_addr);
+ /* .got[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_be32 (dso, dso->shdr[i].sh_addr, data + adjust);
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int
+sparc_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (GELF_R_TYPE (rela->r_info) == R_SPARC_RELATIVE)
+ {
+ if (rela->r_addend)
+ {
+ if ((Elf32_Addr) rela->r_addend >= start)
+ rela->r_addend += (Elf32_Sword) adjust;
+ }
+ else
+ {
+ GElf_Addr val = read_ube32 (dso, rela->r_offset);
+
+ if (val >= start)
+ write_be32 (dso, rela->r_offset, val + adjust);
+ }
+ }
+ return 0;
+}
+
+static int
+sparc_prelink_rel (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static void
+sparc_fixup_plt (DSO *dso, GElf_Rela *rela, GElf_Addr value)
+{
+ Elf32_Sword disp = value - rela->r_offset;
+
+ if (disp >= -0x800000 && disp < 0x800000)
+ {
+ /* b,a value
+ nop
+ nop */
+ write_be32 (dso, rela->r_offset, 0x30800000 | ((disp >> 2) & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4, 0x01000000);
+ write_be32 (dso, rela->r_offset + 8, 0x01000000);
+ }
+ else
+ {
+ /* sethi %hi(value), %g1
+ jmpl %g1 + %lo(value), %g0
+ nop */
+ write_be32 (dso, rela->r_offset, 0x03000000 | ((value >> 10) & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4, 0x81c06000 | (value & 0x3ff));
+ write_be32 (dso, rela->r_offset + 8, 0x01000000);
+ }
+}
+
+static int
+sparc_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (GELF_R_TYPE (rela->r_info) == R_SPARC_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_SPARC_RELATIVE)
+ {
+ /* 32-bit SPARC handles RELATIVE relocs as
+ *(int *)rela->r_offset += l_addr + rela->r_addend.
+ RELATIVE relocs against .got traditionally used to have the
+ addend in memory pointed by r_offset and 0 r_addend,
+ other RELATIVE relocs and more recent .got RELATIVE relocs
+ too have 0 in memory and non-zero r_addend. For prelinking,
+ we need the value in memory to be already relocated for
+ l_addr == 0 case, so we have to make sure r_addend will be 0. */
+ if (rela->r_addend == 0)
+ return 0;
+ value = read_ube32 (dso, rela->r_offset);
+ value += rela->r_addend;
+ rela->r_addend = 0;
+ write_be32 (dso, rela->r_offset, value);
+ /* Tell prelink_rela routine it should update the relocation. */
+ return 2;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_JMP_SLOT:
+ sparc_fixup_plt (dso, rela, value);
+ break;
+ case R_SPARC_8:
+ write_8 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_LO10:
+ write_be32 (dso, rela->r_offset,
+ (value & 0x3ff) | (read_ube32 (dso, rela->r_offset) & ~0x3ff));
+ break;
+ case R_SPARC_HI22:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 10) & 0x3fffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xffc00000));
+ break;
+ case R_SPARC_DISP8:
+ write_8 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_DISP16:
+ write_be16 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_DISP32:
+ write_be32 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_WDISP30:
+ write_be32 (dso, rela->r_offset,
+ (((value - rela->r_offset) >> 2) & 0x3fffffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xc0000000));
+ break;
+ case R_SPARC_TLS_DTPOFF32:
+ write_be32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ /* DTPMOD32 and TPOFF32 is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_SPARC_TLS_DTPMOD32:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_SPARC_TLS_DTPMOD32 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_SPARC_TLS_TPOFF32:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_be32 (dso, rela->r_offset,
+ value + rela->r_addend - info->resolvetls->offset);
+ break;
+ case R_SPARC_TLS_LE_HIX22:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_be32 (dso, rela->r_offset,
+ (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | (((~(value + rela->r_addend - info->resolvetls->offset))
+ >> 10) & 0x3fffff));
+ break;
+ case R_SPARC_TLS_LE_LOX10:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_be32 (dso, rela->r_offset,
+ (read_ube32 (dso, rela->r_offset) & 0xffffe000) | 0x1c00
+ | ((value + rela->r_addend - info->resolvetls->offset)
+ & 0x3ff));
+ break;
+ case R_SPARC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SPARC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sparc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ buf_write_be16 (buf, rela->r_addend);
+ break;
+ case R_SPARC_8:
+ buf_write_8 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+sparc_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+sparc_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_NONE:
+ break;
+ case R_SPARC_DISP32:
+ value -= rela->r_offset;
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ buf_write_be32 (buf, value);
+ break;
+ case R_SPARC_DISP16:
+ value -= rela->r_offset;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ buf_write_be16 (buf, value);
+ break;
+ case R_SPARC_DISP8:
+ value -= rela->r_offset;
+ case R_SPARC_8:
+ buf_write_8 (buf, value);
+ break;
+ case R_SPARC_LO10:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0x3ff) | (value & 0x3ff));
+ break;
+ case R_SPARC_HI22:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xffc00000)
+ | ((value >> 10) & 0x3fffff));
+ break;
+ case R_SPARC_WDISP30:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xc0000000)
+ | (((value - rela->r_offset) >> 2) & 0x3fffffff));
+ break;
+ case R_SPARC_RELATIVE:
+ error (0, 0, "%s: R_SPARC_RELATIVE in ET_EXEC object?",
+ info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+ int r_type;
+
+ if (GELF_R_TYPE (rela->r_info) == R_SPARC_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_SPARC_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD32 and TPOFF32 relocs need conflicts. */
+ case R_SPARC_TLS_DTPMOD32:
+ case R_SPARC_TLS_TPOFF32:
+ case R_SPARC_TLS_LE_HIX22:
+ case R_SPARC_TLS_LE_LOX10:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF32 wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_SPARC_TLS_DTPOFF32
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ value += rela->r_addend;
+ r_type = GELF_R_TYPE (rela->r_info);
+ switch (r_type)
+ {
+ case R_SPARC_DISP32:
+ value -= rela->r_offset;
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_32:
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_DISP16:
+ value -= rela->r_offset;
+ case R_SPARC_16:
+ r_type = R_SPARC_16;
+ break;
+ case R_SPARC_DISP8:
+ value -= rela->r_offset;
+ case R_SPARC_8:
+ r_type = R_SPARC_8;
+ break;
+ /* Attempt to transform all reloc which read-modify-write into
+ simple writes. */
+ case R_SPARC_LO10:
+ value = (read_ube32 (dso, rela->r_offset) & ~0x3ff) | (value & 0x3ff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_HI22:
+ value = (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | ((value >> 10) & 0x3fffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_WDISP30:
+ value = (read_ube32 (dso, rela->r_offset) & 0xc0000000)
+ | (((value - rela->r_offset) >> 2) & 0x3fffffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_UA16:
+ case R_SPARC_UA32:
+ case R_SPARC_JMP_SLOT:
+ break;
+ case R_SPARC_TLS_DTPMOD32:
+ case R_SPARC_TLS_DTPOFF32:
+ case R_SPARC_TLS_TPOFF32:
+ case R_SPARC_TLS_LE_HIX22:
+ case R_SPARC_TLS_LE_LOX10:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ r_type = R_SPARC_32;
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_TLS_DTPMOD32:
+ value = tls->modid;
+ break;
+ case R_SPARC_TLS_DTPOFF32:
+ break;
+ case R_SPARC_TLS_TPOFF32:
+ value -= tls->offset;
+ break;
+ case R_SPARC_TLS_LE_HIX22:
+ value -= tls->offset;
+ value = (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | (((~value) >> 10) & 0x3fffff);
+ break;
+ case R_SPARC_TLS_LE_LOX10:
+ value -= tls->offset;
+ value = (read_ube32 (dso, rela->r_offset) & 0xffffe000) | 0x1c00
+ | (value & 0x3ff);
+ break;
+ }
+ break;
+ default:
+ error (0, 0, "%s: Unknown Sparc relocation type %d", dso->filename,
+ r_type);
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, r_type);
+ ret->r_addend = (Elf32_Sword) value;
+ return 0;
+}
+
+static int
+sparc_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+sparc_arch_prelink (struct prelink_info *info)
+{
+ return 0;
+}
+
+static int
+sparc_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_NONE:
+ return 0;
+ case R_SPARC_RELATIVE:
+ /* 32-bit SPARC handles RELATIVE relocs as
+ *(int *)rela->r_offset += l_addr + rela->r_addend.
+ RELATIVE relocs against .got traditionally used to have the
+ addend in memory pointed by r_offset and 0 r_addend,
+ other RELATIVE relocs and more recent RELATIVE relocs have 0
+ in memory and non-zero r_addend.
+ Always store 0 to memory when doing undo. */
+ assert (rela->r_addend == 0);
+ rela->r_addend = (Elf32_Sword) read_ube32 (dso, rela->r_offset);
+ write_be32 (dso, rela->r_offset, 0);
+ /* Tell undo_prelink_rela routine it should update the
+ relocation. */
+ return 2;
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ case R_SPARC_DISP32:
+ case R_SPARC_TLS_DTPMOD32:
+ case R_SPARC_TLS_DTPOFF32:
+ case R_SPARC_TLS_TPOFF32:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_JMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ if (sec != -1)
+ {
+ /* sethi .-.plt, %g1
+ b,a .plt+0 */
+ write_be32 (dso, rela->r_offset,
+ 0x03000000
+ | ((rela->r_offset - dso->shdr[sec].sh_addr)
+ & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4,
+ 0x30800000
+ | (((dso->shdr[sec].sh_addr - rela->r_offset - 4) >> 2)
+ & 0x3fffff));
+ }
+ break;
+ case R_SPARC_8:
+ case R_SPARC_DISP8:
+ write_8 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ case R_SPARC_DISP16:
+ write_be16 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_LO10:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0x3ff);
+ break;
+ case R_SPARC_TLS_LE_LOX10:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffffe000);
+ break;
+ case R_SPARC_HI22:
+ case R_SPARC_TLS_LE_HIX22:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffc00000);
+ break;
+ case R_SPARC_WDISP30:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xc0000000);
+ break;
+ case R_SPARC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SPARC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sparc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_SPARC_8:
+ case R_SPARC_DISP8:
+ return 1;
+ case R_SPARC_16:
+ case R_SPARC_DISP16:
+ case R_SPARC_UA16:
+ return 2;
+ default:
+ break;
+ }
+ return 4;
+}
+
+static int
+sparc_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_SPARC_COPY: return RTYPE_CLASS_COPY;
+ case R_SPARC_JMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_SPARC_TLS_DTPMOD32:
+ case R_SPARC_TLS_DTPOFF32:
+ case R_SPARC_TLS_TPOFF32:
+ case R_SPARC_TLS_LE_HIX22:
+ case R_SPARC_TLS_LE_LOX10:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "SPARC",
+ .class = ELFCLASS32,
+ .machine = EM_SPARC,
+ .alternate_machine = { EM_SPARC32PLUS },
+ .R_JMP_SLOT = R_SPARC_JMP_SLOT,
+ .R_COPY = R_SPARC_COPY,
+ .R_RELATIVE = R_SPARC_RELATIVE,
+ .dynamic_linker = "/lib/ld-linux.so.2",
+ .adjust_dyn = sparc_adjust_dyn,
+ .adjust_rel = sparc_adjust_rel,
+ .adjust_rela = sparc_adjust_rela,
+ .prelink_rel = sparc_prelink_rel,
+ .prelink_rela = sparc_prelink_rela,
+ .prelink_conflict_rel = sparc_prelink_conflict_rel,
+ .prelink_conflict_rela = sparc_prelink_conflict_rela,
+ .apply_conflict_rela = sparc_apply_conflict_rela,
+ .apply_rel = sparc_apply_rel,
+ .apply_rela = sparc_apply_rela,
+ .rel_to_rela = sparc_rel_to_rela,
+ .need_rel_to_rela = sparc_need_rel_to_rela,
+ .reloc_size = sparc_reloc_size,
+ .reloc_class = sparc_reloc_class,
+ .max_reloc_size = 4,
+ .arch_prelink = sparc_arch_prelink,
+ .undo_prelink_rela = sparc_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x70000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x71000000LL,
+ .mmap_end = 0x80000000LL,
+ .max_page_size = 0x10000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/arch-sparc64.c b/trunk/src/arch-sparc64.c
new file mode 100644
index 0000000..e673e99
--- /dev/null
+++ b/trunk/src/arch-sparc64.c
@@ -0,0 +1,725 @@
+/* Copyright (C) 2001, 2002, 2004 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+#define SPARC64_R_TYPE(info) (GELF_R_TYPE (info) & 0xff)
+
+static int
+sparc64_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".got"))
+ {
+ Elf64_Addr data;
+
+ data = read_ube64 (dso, dso->shdr[i].sh_addr);
+ /* .got[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_be64 (dso, dso->shdr[i].sh_addr, data + adjust);
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int
+sparc64_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc64_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (SPARC64_R_TYPE (rela->r_info) == R_SPARC_RELATIVE)
+ {
+ if (rela->r_addend >= start)
+ rela->r_addend += adjust;
+ }
+ else if (SPARC64_R_TYPE (rela->r_info) == R_SPARC_JMP_SLOT
+ && rela->r_addend)
+ {
+ /* .plt[32768+] r_addends are -some_address_in_plt_section. */
+ if ((- rela->r_addend) >= start)
+ rela->r_addend -= adjust;
+ }
+ return 0;
+}
+
+static int
+sparc64_prelink_rel (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static void
+sparc64_fixup_plt (DSO *dso, GElf_Rela *rela, GElf_Addr value)
+{
+ Elf64_Sxword disp = value - rela->r_offset;
+
+ if (rela->r_addend)
+ {
+ /* .plt[32768+] */
+ write_be64 (dso, rela->r_offset, value);
+ }
+ else if (disp >= -0x800000 && disp < 0x800000)
+ {
+ /* b,a value
+ nop
+ nop */
+ write_be32 (dso, rela->r_offset, 0x30800000 | ((disp >> 2) & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4, 0x01000000);
+ write_be32 (dso, rela->r_offset + 8, 0x01000000);
+ }
+ else if (! (value >> 32))
+ {
+ /* sethi %hi(value), %g1
+ jmpl %g1 + %lo(value), %g0
+ nop */
+ write_be32 (dso, rela->r_offset, 0x03000000 | ((value >> 10) & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4, 0x81c06000 | (value & 0x3ff));
+ write_be32 (dso, rela->r_offset + 8, 0x01000000);
+ }
+ else if ((rela->r_offset + 4 > value
+ && ((rela->r_offset - value) >> 31) == 0)
+ || (value > rela->r_offset + 4
+ && ((value - rela->r_offset - 4) >> 31) == 0))
+ {
+ /* mov %o7, %g1
+ call value
+ mov %g1, %o7 */
+ write_be32 (dso, rela->r_offset, 0x8210000f);
+ write_be32 (dso, rela->r_offset + 4, 0x40000000
+ | (((value - rela->r_offset - 4) >> 2) & 0x3fffffff));
+ write_be32 (dso, rela->r_offset + 8, 0x9e100001);
+ }
+ else
+ {
+ unsigned int csts[4];
+ int i = 0;
+
+ /* sethi %hh(value), %g1
+ sethi %lm(value), %g5
+ or %g1, %hm(value), %g1
+ or %g5, %lo(value), %g5
+ sllx %g1, 32, %g1
+ jmpl %g1 + %g5, %g0
+ nop */
+
+ csts[0] = value >> 42;
+ csts[1] = (value >> 32) & 0x3ff;
+ csts[2] = (value >> 10) & 0x3fffff;
+ csts[3] = value & 0x3ff;
+ write_be32 (dso, rela->r_offset, 0x03000000 | csts[0]);
+ write_be32 (dso, rela->r_offset + 4, 0x0b000000 | csts[2]);
+ /* Sparc64 shared libs are often 0xfffff800XXXXXXXX, so optimize
+ for this common case. */
+ if (csts[1] == 0)
+ write_be32 (dso, rela->r_offset + 8, 0x83287020);
+ else
+ write_be32 (dso, rela->r_offset + 8, 0x82106000 | csts[1]);
+ write_be32 (dso, rela->r_offset + 12, 0x8a116000 | csts[3]);
+ if (csts[1] != 0)
+ write_be32 (dso, rela->r_offset + 16, 0x83287020), i = 4;
+ write_be32 (dso, rela->r_offset + 16 + i, 0x81c04005);
+ write_be32 (dso, rela->r_offset + 20 + i, 0x01000000);
+ }
+}
+
+static int
+sparc64_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso = info->dso;
+ GElf_Addr value;
+
+ if (SPARC64_R_TYPE (rela->r_info) == R_SPARC_NONE)
+ return 0;
+ else if (SPARC64_R_TYPE (rela->r_info) == R_SPARC_RELATIVE)
+ {
+ /* 64-bit SPARC handles RELATIVE relocs as
+ *(long *)rela->r_offset = l_addr + rela->r_addend,
+ so we must update the memory. */
+ write_be64 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ SPARC64_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (SPARC64_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_64:
+ case R_SPARC_UA64:
+ write_be64 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ write_be32 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_JMP_SLOT:
+ sparc64_fixup_plt (dso, rela, value);
+ break;
+ case R_SPARC_8:
+ write_8 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ write_be16 (dso, rela->r_offset, value);
+ break;
+ case R_SPARC_LO10:
+ write_be32 (dso, rela->r_offset,
+ (value & 0x3ff) | (read_ube32 (dso, rela->r_offset) & ~0x3ff));
+ break;
+ case R_SPARC_LM22:
+ case R_SPARC_HI22:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 10) & 0x3fffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xffc00000));
+ break;
+ case R_SPARC_DISP8:
+ write_8 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_DISP16:
+ write_be16 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_DISP32:
+ write_be32 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_DISP64:
+ write_be64 (dso, rela->r_offset, value - rela->r_offset);
+ break;
+ case R_SPARC_WDISP30:
+ write_be32 (dso, rela->r_offset,
+ (((value - rela->r_offset) >> 2) & 0x3fffffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xc0000000));
+ break;
+ case R_SPARC_H44:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 22) & 0x3fffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xffc00000));
+ break;
+ case R_SPARC_M44:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 12) & 0x3ff)
+ | (read_ube32 (dso, rela->r_offset) & ~0x3ff));
+ break;
+ case R_SPARC_L44:
+ write_be32 (dso, rela->r_offset,
+ (value & 0xfff) | (read_ube32 (dso, rela->r_offset) & ~0xfff));
+ break;
+ case R_SPARC_HH22:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 42) & 0x3fffff)
+ | (read_ube32 (dso, rela->r_offset) & 0xffc00000));
+ break;
+ case R_SPARC_HM10:
+ write_be32 (dso, rela->r_offset,
+ ((value >> 32) & 0x3ff)
+ | (read_ube32 (dso, rela->r_offset) & ~0x3ff));
+ break;
+ case R_SPARC_OLO10:
+ write_be32 (dso, rela->r_offset,
+ (((value & 0x3ff) + (GELF_R_TYPE (rela->r_info) >> 8)) & 0x1fff)
+ | (read_ube32 (dso, rela->r_offset) & ~0x1fff));
+ break;
+ case R_SPARC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SPARC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sparc relocation type %d", dso->filename,
+ (int) SPARC64_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc64_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (SPARC64_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_64:
+ case R_SPARC_UA64:
+ buf_write_be64 (buf, rela->r_addend);
+ break;
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ buf_write_be32 (buf, rela->r_addend);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ buf_write_be16 (buf, rela->r_addend);
+ break;
+ case R_SPARC_8:
+ buf_write_8 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+sparc64_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+sparc64_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ SPARC64_R_TYPE (rela->r_info));
+ value += rela->r_addend;
+ switch (SPARC64_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_NONE:
+ break;
+ case R_SPARC_DISP64:
+ value -= rela->r_offset;
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_64:
+ case R_SPARC_UA64:
+ buf_write_be64 (buf, value);
+ break;
+ case R_SPARC_DISP32:
+ value -= rela->r_offset;
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ buf_write_be32 (buf, value);
+ break;
+ case R_SPARC_DISP16:
+ value -= rela->r_offset;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ buf_write_be16 (buf, value);
+ break;
+ case R_SPARC_DISP8:
+ value -= rela->r_offset;
+ case R_SPARC_8:
+ buf_write_8 (buf, value);
+ break;
+ case R_SPARC_LO10:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0x3ff) | (value & 0x3ff));
+ break;
+ case R_SPARC_LM22:
+ case R_SPARC_HI22:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xffc00000)
+ | ((value >> 10) & 0x3fffff));
+ break;
+ case R_SPARC_WDISP30:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xc0000000)
+ | (((value - rela->r_offset) >> 2) & 0x3fffffff));
+ break;
+ case R_SPARC_H44:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xffc00000)
+ | ((value >> 22) & 0x3fffff));
+ break;
+ case R_SPARC_M44:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0x3ff)
+ | ((value >> 12) & 0x3ff));
+ break;
+ case R_SPARC_L44:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0xfff) | (value & 0xfff));
+ break;
+ case R_SPARC_HH22:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & 0xffc00000)
+ | ((value >> 42) & 0x3fffff));
+ break;
+ case R_SPARC_HM10:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0x3ff)
+ | ((value >> 32) & 0x3ff));
+ break;
+ case R_SPARC_OLO10:
+ buf_write_be32 (buf, (buf_read_ube32 (buf) & ~0x1fff)
+ | (((value & 0x3ff)
+ + (GELF_R_TYPE (rela->r_info) >> 8)) & 0x1fff));
+ break;
+ case R_SPARC_RELATIVE:
+ error (0, 0, "%s: R_SPARC_RELATIVE in ET_EXEC object?",
+ info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc64_prelink_conflict_rel (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc64_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ GElf_Rela *ret;
+ int r_type;
+
+ if (SPARC64_R_TYPE (rela->r_info) == R_SPARC_RELATIVE
+ || SPARC64_R_TYPE (rela->r_info) == R_SPARC_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ SPARC64_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ return 0;
+ value = conflict_lookup_value (conflict);
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ value += rela->r_addend;
+ r_type = SPARC64_R_TYPE (rela->r_info);
+ switch (r_type)
+ {
+ case R_SPARC_DISP64:
+ value -= rela->r_offset;
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_64:
+ r_type = R_SPARC_64;
+ break;
+ case R_SPARC_DISP32:
+ value -= rela->r_offset;
+ case R_SPARC_32:
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_DISP16:
+ value -= rela->r_offset;
+ case R_SPARC_16:
+ r_type = R_SPARC_16;
+ break;
+ case R_SPARC_DISP8:
+ value -= rela->r_offset;
+ case R_SPARC_8:
+ r_type = R_SPARC_8;
+ break;
+ /* Attempt to transform all reloc which read-modify-write into
+ simple writes. */
+ case R_SPARC_LO10:
+ value = (read_ube32 (dso, rela->r_offset) & ~0x3ff) | (value & 0x3ff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_LM22:
+ case R_SPARC_HI22:
+ value = (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | ((value >> 10) & 0x3fffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_WDISP30:
+ value = (read_ube32 (dso, rela->r_offset) & 0xc0000000)
+ | (((value - rela->r_offset) >> 2) & 0x3fffffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_H44:
+ value = (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | ((value >> 22) & 0x3fffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_M44:
+ value = (read_ube32 (dso, rela->r_offset) & ~0x3ff)
+ | ((value >> 12) & 0x3ff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_L44:
+ value = (read_ube32 (dso, rela->r_offset) & ~0xfff) | (value & 0xfff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_HH22:
+ value = (read_ube32 (dso, rela->r_offset) & 0xffc00000)
+ | ((value >> 42) & 0x3fffff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_HM10:
+ value = (read_ube32 (dso, rela->r_offset) & ~0x3ff)
+ | ((value >> 32) & 0x3ff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_OLO10:
+ value = (read_ube32 (dso, rela->r_offset) & ~0x1fff)
+ | (((value & 0x3ff) + (GELF_R_TYPE (rela->r_info) >> 8)) & 0x1fff);
+ r_type = R_SPARC_32;
+ break;
+ case R_SPARC_JMP_SLOT:
+ if (rela->r_addend)
+ r_type = R_SPARC_64;
+ break;
+ case R_SPARC_UA16:
+ case R_SPARC_UA32:
+ case R_SPARC_UA64:
+ break;
+ default:
+ error (0, 0, "%s: Unknown Sparc relocation type %d", dso->filename,
+ r_type);
+ return 1;
+ }
+ ret->r_info = GELF_R_INFO (0, r_type);
+ ret->r_addend = value;
+ return 0;
+}
+
+static int
+sparc64_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: Sparc doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+sparc64_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+sparc64_arch_prelink (struct prelink_info *info)
+{
+ return 0;
+}
+
+static int
+sparc64_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_SPARC_NONE:
+ break;
+ case R_SPARC_JMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ if (sec != -1)
+ {
+ if (rela->r_addend == 0)
+ {
+ /* sethi .-.plt, %g1
+ b,a %xcc, .plt+0x20 */
+ write_be32 (dso, rela->r_offset,
+ 0x03000000
+ | ((rela->r_offset - dso->shdr[sec].sh_addr)
+ & 0x3fffff));
+ write_be32 (dso, rela->r_offset + 4,
+ 0x30680000
+ | (((dso->shdr[sec].sh_addr + 32
+ - rela->r_offset - 4) >> 2)
+ & 0x7ffff));
+ write_be32 (dso, rela->r_offset + 8, 0x01000000);
+ write_be32 (dso, rela->r_offset + 12, 0x01000000);
+ write_be32 (dso, rela->r_offset + 16, 0x01000000);
+ write_be32 (dso, rela->r_offset + 20, 0x01000000);
+ write_be32 (dso, rela->r_offset + 24, 0x01000000);
+ write_be32 (dso, rela->r_offset + 28, 0x01000000);
+ }
+ else
+ {
+ GElf_Addr slot = ((rela->r_offset + 0x400
+ - dso->shdr[sec].sh_addr)
+ / 0x1400) * 0x1400
+ + dso->shdr[sec].sh_addr - 0x400;
+ /* slot+12 contains: ldx [%o7 + X], %g1 */
+ GElf_Addr ptr = slot + (read_ube32 (dso, slot + 12) & 0xfff) + 4;
+
+ write_be64 (dso, rela->r_offset,
+ dso->shdr[sec].sh_addr
+ - (slot + ((rela->r_offset - ptr) / 8) * 24 + 4));
+ }
+ }
+ break;
+ case R_SPARC_RELATIVE:
+ case R_SPARC_GLOB_DAT:
+ case R_SPARC_64:
+ case R_SPARC_UA64:
+ case R_SPARC_DISP64:
+ write_be64 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_32:
+ case R_SPARC_UA32:
+ case R_SPARC_DISP32:
+ write_be32 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_8:
+ case R_SPARC_DISP8:
+ write_8 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_16:
+ case R_SPARC_UA16:
+ case R_SPARC_DISP16:
+ write_be16 (dso, rela->r_offset, 0);
+ break;
+ case R_SPARC_LO10:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0x3ff);
+ break;
+ case R_SPARC_LM22:
+ case R_SPARC_HI22:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffc00000);
+ break;
+ case R_SPARC_WDISP30:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xc0000000);
+ break;
+ case R_SPARC_H44:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffc00000);
+ break;
+ case R_SPARC_M44:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0x3ff);
+ break;
+ case R_SPARC_L44:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0xfff);
+ break;
+ case R_SPARC_HH22:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & 0xffc00000);
+ break;
+ case R_SPARC_HM10:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0x3ff);
+ break;
+ case R_SPARC_OLO10:
+ write_be32 (dso, rela->r_offset,
+ read_ube32 (dso, rela->r_offset) & ~0x1fff);
+ break;
+ case R_SPARC_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_SPARC_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown sparc relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+sparc64_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_SPARC_8:
+ case R_SPARC_DISP8:
+ return 1;
+ case R_SPARC_16:
+ case R_SPARC_DISP16:
+ case R_SPARC_UA16:
+ return 2;
+ case R_SPARC_RELATIVE:
+ case R_SPARC_64:
+ case R_SPARC_UA64:
+ case R_SPARC_GLOB_DAT:
+ return 8;
+ default:
+ break;
+ }
+ return 4;
+}
+
+static int
+sparc64_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_SPARC_COPY: return RTYPE_CLASS_COPY;
+ case R_SPARC_JMP_SLOT: return RTYPE_CLASS_PLT;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "SPARC",
+ .class = ELFCLASS64,
+ .machine = EM_SPARCV9,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_SPARC_JMP_SLOT,
+ .R_COPY = R_SPARC_COPY,
+ .R_RELATIVE = R_SPARC_RELATIVE,
+ .dynamic_linker = "/lib64/ld-linux.so.2",
+ .adjust_dyn = sparc64_adjust_dyn,
+ .adjust_rel = sparc64_adjust_rel,
+ .adjust_rela = sparc64_adjust_rela,
+ .prelink_rel = sparc64_prelink_rel,
+ .prelink_rela = sparc64_prelink_rela,
+ .prelink_conflict_rel = sparc64_prelink_conflict_rel,
+ .prelink_conflict_rela = sparc64_prelink_conflict_rela,
+ .apply_conflict_rela = sparc64_apply_conflict_rela,
+ .apply_rel = sparc64_apply_rel,
+ .apply_rela = sparc64_apply_rela,
+ .rel_to_rela = sparc64_rel_to_rela,
+ .need_rel_to_rela = sparc64_need_rel_to_rela,
+ .reloc_size = sparc64_reloc_size,
+ .reloc_class = sparc64_reloc_class,
+ .max_reloc_size = 8,
+ .arch_prelink = sparc64_arch_prelink,
+ .undo_prelink_rela = sparc64_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0xfffff80000000000, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0xfffff80001000000LL,
+ /* If we need yet more space for shared libraries, we can of course
+ expand, but limiting all DSOs into 4 GB means stack overflows
+ jumping to shared library functions is much harder (there is
+ '\0' byte in the address before the bytes that matter). */
+ .mmap_end = 0xfffff80100000000LL,
+ .max_page_size = 0x100000,
+ .page_size = 0x2000
+};
diff --git a/trunk/src/arch-x86_64.c b/trunk/src/arch-x86_64.c
new file mode 100644
index 0000000..8079f1a
--- /dev/null
+++ b/trunk/src/arch-x86_64.c
@@ -0,0 +1,536 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 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 <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+
+#include "prelink.h"
+
+static int
+x86_64_adjust_dyn (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ if (dyn->d_tag == DT_PLTGOT)
+ {
+ int sec = addr_to_sec (dso, dyn->d_un.d_ptr);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 0;
+
+ data = read_ule64 (dso, dyn->d_un.d_ptr);
+ /* If .got.plt[0] points to _DYNAMIC, it needs to be adjusted. */
+ if (data == dso->shdr[n].sh_addr && data >= start)
+ write_le64 (dso, dyn->d_un.d_ptr, data + adjust);
+
+ data = read_ule64 (dso, dyn->d_un.d_ptr + 8);
+ /* If .got.plt[1] points to .plt + 0x16, it needs to be adjusted. */
+ if (data && data >= start)
+ {
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (data == dso->shdr[i].sh_addr + 0x16
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name), ".plt") == 0)
+ {
+ write_le64 (dso, dyn->d_un.d_ptr + 8, data + adjust);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+x86_64_adjust_rel (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ error (0, 0, "%s: X86-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+x86_64_adjust_rela (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust)
+{
+ Elf64_Addr addr;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_RELATIVE:
+ if (rela->r_addend >= start)
+ {
+ if (read_ule64 (dso, rela->r_offset) == rela->r_addend)
+ write_le64 (dso, rela->r_offset, rela->r_addend + adjust);
+ rela->r_addend += adjust;
+ }
+ break;
+ case R_X86_64_JUMP_SLOT:
+ addr = read_ule64 (dso, rela->r_offset);
+ if (addr >= start)
+ write_le64 (dso, rela->r_offset, addr + adjust);
+ break;
+ }
+ return 0;
+}
+
+static int
+x86_64_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr)
+{
+ error (0, 0, "%s: X86-64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+x86_64_prelink_rela (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr)
+{
+ DSO *dso;
+ GElf_Addr value;
+
+ dso = info->dso;
+ if (GELF_R_TYPE (rela->r_info) == R_X86_64_NONE)
+ return 0;
+ else if (GELF_R_TYPE (rela->r_info) == R_X86_64_RELATIVE)
+ {
+ write_le64 (dso, rela->r_offset, rela->r_addend);
+ return 0;
+ }
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_JUMP_SLOT:
+ case R_X86_64_64:
+ write_le64 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_X86_64_32:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ case R_X86_64_PC32:
+ write_le32 (dso, rela->r_offset, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_X86_64_DTPOFF64:
+ write_le64 (dso, rela->r_offset, value + rela->r_addend);
+ break;
+ /* DTPMOD64 and TPOFF64 is impossible to predict in shared libraries
+ unless prelink sets the rules. */
+ case R_X86_64_DTPMOD64:
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ error (0, 0, "%s: R_X86_64_DTPMOD64 reloc in executable?",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case R_X86_64_TPOFF64:
+ if (dso->ehdr.e_type == ET_EXEC && info->resolvetls)
+ write_le64 (dso, rela->r_offset,
+ value + rela->r_addend - info->resolvetls->offset);
+ break;
+ case R_X86_64_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_X86_64_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown X86-64 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+x86_64_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela,
+ char *buf)
+{
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_JUMP_SLOT:
+ case R_X86_64_64:
+ buf_write_le64 (buf, rela->r_addend);
+ break;
+ case R_X86_64_32:
+ buf_write_le32 (buf, rela->r_addend);
+ break;
+ default:
+ abort ();
+ }
+ return 0;
+}
+
+static int
+x86_64_apply_rel (struct prelink_info *info, GElf_Rel *rel, char *buf)
+{
+ error (0, 0, "%s: X86-64 doesn't support REL relocs", info->dso->filename);
+ return 1;
+}
+
+static int
+x86_64_apply_rela (struct prelink_info *info, GElf_Rela *rela, char *buf)
+{
+ GElf_Addr value;
+
+ value = info->resolve (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_NONE:
+ break;
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_JUMP_SLOT:
+ case R_X86_64_64:
+ buf_write_le64 (buf, value + rela->r_addend);
+ break;
+ case R_X86_64_32:
+ buf_write_le32 (buf, value + rela->r_addend);
+ break;
+ case R_X86_64_PC32:
+ buf_write_le32 (buf, value + rela->r_addend - rela->r_offset);
+ break;
+ case R_X86_64_COPY:
+ abort ();
+ case R_X86_64_RELATIVE:
+ error (0, 0, "%s: R_X86_64_RELATIVE in ET_EXEC object?", info->dso->filename);
+ return 1;
+ default:
+ return 1;
+ }
+ return 0;
+}
+
+static int
+x86_64_prelink_conflict_rel (DSO *dso, struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr)
+{
+ error (0, 0, "%s: X86-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+x86_64_prelink_conflict_rela (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ GElf_Addr value;
+ struct prelink_conflict *conflict;
+ struct prelink_tls *tls;
+ GElf_Rela *ret;
+
+ if (GELF_R_TYPE (rela->r_info) == R_X86_64_RELATIVE
+ || GELF_R_TYPE (rela->r_info) == R_X86_64_NONE)
+ /* Fast path: nothing to do. */
+ return 0;
+ conflict = prelink_conflict (info, GELF_R_SYM (rela->r_info),
+ GELF_R_TYPE (rela->r_info));
+ if (conflict == NULL)
+ {
+ if (info->curtls == NULL)
+ return 0;
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ /* Even local DTPMOD and TPOFF relocs need conflicts. */
+ case R_X86_64_DTPMOD64:
+ case R_X86_64_TPOFF64:
+ break;
+ default:
+ return 0;
+ }
+ value = 0;
+ }
+ else
+ {
+ /* DTPOFF wants to see only real conflicts, not lookups
+ with reloc_class RTYPE_CLASS_TLS. */
+ if (GELF_R_TYPE (rela->r_info) == R_X86_64_DTPOFF64
+ && conflict->lookup.tls == conflict->conflict.tls
+ && conflict->lookupval == conflict->conflictval)
+ return 0;
+
+ value = conflict_lookup_value (conflict);
+ }
+ ret = prelink_conflict_add_rela (info);
+ if (ret == NULL)
+ return 1;
+ ret->r_offset = rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, GELF_R_TYPE (rela->r_info));
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_GLOB_DAT:
+ ret->r_info = GELF_R_INFO (0, R_X86_64_64);
+ /* FALLTHROUGH */
+ case R_X86_64_JUMP_SLOT:
+ case R_X86_64_64:
+ ret->r_addend = value + rela->r_addend;
+ break;
+ case R_X86_64_32:
+ value += rela->r_addend;
+ ret->r_addend = value;
+ break;
+ case R_X86_64_PC32:
+ ret->r_addend = value + rela->r_addend - rela->r_offset;
+ ret->r_info = GELF_R_INFO (0, R_X86_64_32);
+ break;
+ case R_X86_64_COPY:
+ error (0, 0, "R_X86_64_COPY should not be present in shared libraries");
+ return 1;
+ case R_X86_64_DTPMOD64:
+ case R_X86_64_DTPOFF64:
+ case R_X86_64_TPOFF64:
+ if (conflict != NULL
+ && (conflict->reloc_class != RTYPE_CLASS_TLS
+ || conflict->lookup.tls == NULL))
+ {
+ error (0, 0, "%s: TLS reloc not resolving to STT_TLS symbol",
+ dso->filename);
+ return 1;
+ }
+ tls = conflict ? conflict->lookup.tls : info->curtls;
+ ret->r_info = GELF_R_INFO (0, R_X86_64_64);
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_DTPMOD64:
+ ret->r_addend = tls->modid;
+ break;
+ case R_X86_64_DTPOFF64:
+ ret->r_addend = value + rela->r_addend;
+ break;
+ case R_X86_64_TPOFF64:
+ ret->r_addend = value + rela->r_addend - tls->offset;
+ break;
+ }
+ break;
+
+ default:
+ error (0, 0, "%s: Unknown X86-64 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+x86_64_rel_to_rela (DSO *dso, GElf_Rel *rel, GElf_Rela *rela)
+{
+ error (0, 0, "%s: X86-64 doesn't support REL relocs", dso->filename);
+ return 1;
+}
+
+static int
+x86_64_need_rel_to_rela (DSO *dso, int first, int last)
+{
+ return 0;
+}
+
+static int
+x86_64_arch_prelink (struct prelink_info *info)
+{
+ DSO *dso;
+ int i;
+
+ dso = info->dso;
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Write address of .plt + 0x16 into got[1].
+ .plt + 0x16 is what got[3] contains unless prelinking. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ assert (i < dso->ehdr.e_shnum);
+ data = dso->shdr[i].sh_addr + 0x16;
+ write_le64 (dso, dso->info[DT_PLTGOT] + 8, data);
+ }
+
+ return 0;
+}
+
+static int
+x86_64_arch_undo_prelink (DSO *dso)
+{
+ int i;
+
+ if (dso->info[DT_PLTGOT])
+ {
+ /* Clear got[1] if it contains address of .plt + 0x16. */
+ int sec = addr_to_sec (dso, dso->info[DT_PLTGOT]);
+ Elf64_Addr data;
+
+ if (sec == -1)
+ return 1;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && ! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".plt"))
+ break;
+
+ if (i == dso->ehdr.e_shnum)
+ return 0;
+ data = read_ule64 (dso, dso->info[DT_PLTGOT] + 8);
+ if (data == dso->shdr[i].sh_addr + 0x16)
+ write_le64 (dso, dso->info[DT_PLTGOT] + 8, 0);
+ }
+
+ return 0;
+}
+
+static int
+x86_64_undo_prelink_rela (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr)
+{
+ int sec;
+ const char *name;
+
+ switch (GELF_R_TYPE (rela->r_info))
+ {
+ case R_X86_64_NONE:
+ case R_X86_64_RELATIVE:
+ break;
+ case R_X86_64_JUMP_SLOT:
+ sec = addr_to_sec (dso, rela->r_offset);
+ name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[sec].sh_name);
+ if (sec == -1 || (strcmp (name, ".got") && strcmp (name, ".got.plt")))
+ {
+ error (0, 0, "%s: R_X86_64_JUMP_SLOT not pointing into .got section",
+ dso->filename);
+ return 1;
+ }
+ else
+ {
+ Elf64_Addr data = read_ule64 (dso, dso->shdr[sec].sh_addr + 8);
+
+ assert (rela->r_offset >= dso->shdr[sec].sh_addr + 24);
+ assert (((rela->r_offset - dso->shdr[sec].sh_addr) & 7) == 0);
+ write_le64 (dso, rela->r_offset,
+ 2 * (rela->r_offset - dso->shdr[sec].sh_addr - 24)
+ + data);
+ }
+ break;
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_64:
+ case R_X86_64_DTPMOD64:
+ case R_X86_64_DTPOFF64:
+ case R_X86_64_TPOFF64:
+ write_le64 (dso, rela->r_offset, 0);
+ break;
+ case R_X86_64_32:
+ case R_X86_64_PC32:
+ write_le32 (dso, rela->r_offset, 0);
+ break;
+ case R_X86_64_COPY:
+ if (dso->ehdr.e_type == ET_EXEC)
+ /* COPY relocs are handled specially in generic code. */
+ return 0;
+ error (0, 0, "%s: R_X86_64_COPY reloc in shared library?", dso->filename);
+ return 1;
+ default:
+ error (0, 0, "%s: Unknown X86-64 relocation type %d", dso->filename,
+ (int) GELF_R_TYPE (rela->r_info));
+ return 1;
+ }
+ return 0;
+}
+
+static int
+x86_64_reloc_size (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_JUMP_SLOT:
+ case R_X86_64_64:
+ return 8;
+ default:
+ return 4;
+ }
+}
+
+static int
+x86_64_reloc_class (int reloc_type)
+{
+ switch (reloc_type)
+ {
+ case R_X86_64_COPY: return RTYPE_CLASS_COPY;
+ case R_X86_64_JUMP_SLOT: return RTYPE_CLASS_PLT;
+ case R_X86_64_DTPMOD64:
+ case R_X86_64_DTPOFF64:
+ case R_X86_64_TPOFF64:
+ return RTYPE_CLASS_TLS;
+ default: return RTYPE_CLASS_VALID;
+ }
+}
+
+PL_ARCH = {
+ .name = "x86-64",
+ .class = ELFCLASS64,
+ .machine = EM_X86_64,
+ .alternate_machine = { EM_NONE },
+ .R_JMP_SLOT = R_X86_64_JUMP_SLOT,
+ .R_COPY = R_X86_64_COPY,
+ .R_RELATIVE = R_X86_64_RELATIVE,
+ .dynamic_linker = "/lib64/ld-linux-x86-64.so.2",
+ .adjust_dyn = x86_64_adjust_dyn,
+ .adjust_rel = x86_64_adjust_rel,
+ .adjust_rela = x86_64_adjust_rela,
+ .prelink_rel = x86_64_prelink_rel,
+ .prelink_rela = x86_64_prelink_rela,
+ .prelink_conflict_rel = x86_64_prelink_conflict_rel,
+ .prelink_conflict_rela = x86_64_prelink_conflict_rela,
+ .apply_conflict_rela = x86_64_apply_conflict_rela,
+ .apply_rel = x86_64_apply_rel,
+ .apply_rela = x86_64_apply_rela,
+ .rel_to_rela = x86_64_rel_to_rela,
+ .need_rel_to_rela = x86_64_need_rel_to_rela,
+ .reloc_size = x86_64_reloc_size,
+ .reloc_class = x86_64_reloc_class,
+ .max_reloc_size = 8,
+ .arch_prelink = x86_64_arch_prelink,
+ .arch_undo_prelink = x86_64_arch_undo_prelink,
+ .undo_prelink_rela = x86_64_undo_prelink_rela,
+ /* Although TASK_UNMAPPED_BASE is 0x2a95555555, we leave some
+ area so that mmap of /etc/ld.so.cache and ld.so's malloc
+ does not take some library's VA slot.
+ Also, if this guard area isn't too small, typically
+ even dlopened libraries will get the slots they desire. */
+ .mmap_base = 0x3000000000LL,
+ .mmap_end = 0x4000000000LL,
+ .max_page_size = 0x200000,
+ .page_size = 0x1000
+};
diff --git a/trunk/src/cache.c b/trunk/src/cache.c
new file mode 100644
index 0000000..2a72267
--- /dev/null
+++ b/trunk/src/cache.c
@@ -0,0 +1,856 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/mman.h>
+#include "prelinktab.h"
+
+htab_t prelink_devino_htab, prelink_filename_htab;
+
+int prelink_entry_count;
+
+static hashval_t
+devino_hash (const void *p)
+{
+ struct prelink_entry *e = (struct prelink_entry *)p;
+
+ return (e->dev << 2) ^ (e->ino) ^ (e->ino >> 20);
+}
+
+static int
+devino_eq (const void *p, const void *q)
+{
+ struct prelink_entry *e = (struct prelink_entry *)p;
+ struct prelink_entry *f = (struct prelink_entry *)q;
+
+ return e->ino == f->ino && e->dev == f->dev;
+}
+
+static hashval_t
+filename_hash (const void *p)
+{
+ struct prelink_entry *e = (struct prelink_entry *)p;
+ const unsigned char *s = (const unsigned char *)e->filename;
+ hashval_t h = 0;
+ unsigned char c;
+ size_t len = 0;
+
+ while ((c = *s++) != '\0')
+ {
+ h += c + (c << 17);
+ h ^= h >> 2;
+ ++len;
+ }
+ return h + len + (len << 17);
+}
+
+static int
+filename_eq (const void *p, const void *q)
+{
+ struct prelink_entry *e = (struct prelink_entry *)p;
+ struct prelink_entry *f = (struct prelink_entry *)q;
+
+ return strcmp (e->filename, f->filename) == 0;
+}
+
+int
+prelink_init_cache (void)
+{
+ prelink_devino_htab = htab_try_create (100, devino_hash, devino_eq, NULL);
+ prelink_filename_htab = htab_try_create (100, filename_hash, filename_eq,
+ NULL);
+ if (prelink_devino_htab == NULL || prelink_filename_htab == NULL)
+ error (EXIT_FAILURE, ENOMEM, "Could not create hash table");
+ return 0;
+}
+
+struct prelink_entry *
+prelink_find_entry (const char *filename, const struct stat64 *stp,
+ int insert)
+{
+ struct prelink_entry e, *ent = NULL;
+ void **filename_slot, *dummy = NULL;
+ void **devino_slot = NULL;
+ struct stat64 st;
+ char *canon_filename = NULL;
+
+ e.filename = filename;
+ filename_slot = htab_find_slot (prelink_filename_htab, &e,
+ insert ? INSERT : NO_INSERT);
+ if (filename_slot == NULL)
+ {
+ if (insert)
+ goto error_out;
+ filename_slot = &dummy;
+ }
+
+ if (*filename_slot != NULL)
+ return (struct prelink_entry *) *filename_slot;
+
+ if (! stp)
+ {
+ canon_filename = wrap_prelink_canonicalize (filename, &st);
+ if (canon_filename == NULL && wrap_stat64 (filename, &st) < 0)
+ {
+ error (0, errno, "Could not stat %s", filename);
+ if (insert)
+ {
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+ }
+ return NULL;
+ }
+ stp = &st;
+ }
+
+ e.dev = stp->st_dev;
+ e.ino = stp->st_ino;
+ devino_slot = htab_find_slot (prelink_devino_htab, &e,
+ insert ? INSERT : NO_INSERT);
+ if (devino_slot == NULL)
+ {
+ if (insert)
+ goto error_out;
+ free (canon_filename);
+ return NULL;
+ }
+
+ if (*devino_slot != NULL)
+ {
+ ent = (struct prelink_entry *) *devino_slot;
+ if (canon_filename == NULL)
+ canon_filename = wrap_prelink_canonicalize (filename, NULL);
+ if (canon_filename == NULL)
+ {
+ error (0, 0, "Could not canonicalize filename %s", filename);
+ goto error_out2;
+ }
+
+ if (strcmp (canon_filename, ent->canon_filename) != 0)
+ {
+ struct prelink_link *hardlink;
+
+ hardlink = (struct prelink_link *)
+ malloc (sizeof (struct prelink_link));
+ if (hardlink == NULL)
+ {
+ error (0, ENOMEM, "Could not record hardlink %s to %s",
+ canon_filename, ent->canon_filename);
+ goto error_out2;
+ }
+
+ hardlink->canon_filename = canon_filename;
+ hardlink->next = ent->hardlink;
+ ent->hardlink = hardlink;
+ }
+ else
+ free (canon_filename);
+ return ent;
+ }
+
+ if (! insert)
+ {
+ if (canon_filename != NULL)
+ free (canon_filename);
+ return NULL;
+ }
+
+ ent = (struct prelink_entry *) calloc (sizeof (struct prelink_entry), 1);
+ if (ent == NULL)
+ goto error_out;
+
+ ent->filename = strdup (filename);
+ if (ent->filename == NULL)
+ goto error_out;
+
+ if (canon_filename != NULL)
+ ent->canon_filename = canon_filename;
+ else
+ ent->canon_filename = wrap_prelink_canonicalize (filename, NULL);
+ if (ent->canon_filename == NULL)
+ {
+ error (0, 0, "Could not canonicalize filename %s", filename);
+ free ((char *) ent->filename);
+ free (ent);
+ goto error_out2;
+ }
+
+ ent->dev = stp->st_dev;
+ ent->ino = stp->st_ino;
+ ent->ctime = stp->st_ctime;
+ ent->mtime = stp->st_mtime;
+
+ *filename_slot = ent;
+ *devino_slot = ent;
+ ++prelink_entry_count;
+ return ent;
+
+error_out:
+ free (ent);
+ error (0, ENOMEM, "Could not insert %s into hash table", filename);
+error_out2:
+ if (insert)
+ {
+ if (filename_slot != NULL)
+ {
+ assert (*filename_slot == NULL);
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+ }
+ if (devino_slot != NULL && *devino_slot == NULL)
+ {
+ *devino_slot = &dummy;
+ htab_clear_slot (prelink_devino_htab, devino_slot);
+ }
+ }
+ free (canon_filename);
+ return NULL;
+}
+
+static struct prelink_entry *
+prelink_load_entry (const char *filename)
+{
+ struct prelink_entry e, *ent = NULL;
+ void **filename_slot, *dummy = NULL;
+ void **devino_slot = &dummy;
+ struct stat64 st;
+ uint32_t ctime = 0, mtime = 0;
+ char *canon_filename = NULL;
+
+ e.filename = filename;
+ filename_slot = htab_find_slot (prelink_filename_htab, &e, INSERT);
+ if (filename_slot == NULL)
+ goto error_out;
+
+ if (*filename_slot != NULL)
+ return (struct prelink_entry *) *filename_slot;
+
+ canon_filename = wrap_prelink_canonicalize (filename, &st);
+ if (canon_filename == NULL)
+ goto error_out2;
+ if (strcmp (canon_filename, filename) != 0)
+ {
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+
+ e.filename = canon_filename;
+ filename_slot = htab_find_slot (prelink_filename_htab, &e, INSERT);
+ if (filename_slot == NULL)
+ goto error_out;
+
+ if (*filename_slot != NULL)
+ {
+ free (canon_filename);
+ return (struct prelink_entry *) *filename_slot;
+ }
+ }
+
+ if (! S_ISREG (st.st_mode))
+ {
+ free (canon_filename);
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+ return NULL;
+ }
+ else
+ {
+ e.dev = st.st_dev;
+ e.ino = st.st_ino;
+ ctime = (uint32_t) st.st_ctime;
+ mtime = (uint32_t) st.st_mtime;
+ devino_slot = htab_find_slot (prelink_devino_htab, &e, INSERT);
+ if (devino_slot == NULL)
+ goto error_out;
+ }
+
+ if (*devino_slot != NULL)
+ {
+ free (canon_filename);
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+ return (struct prelink_entry *) *devino_slot;
+ }
+
+ ent = (struct prelink_entry *) calloc (sizeof (struct prelink_entry), 1);
+ if (ent == NULL)
+ goto error_out;
+
+ ent->filename = strdup (filename);
+ if (ent->filename == NULL)
+ goto error_out;
+
+ ent->canon_filename = canon_filename;
+ ent->dev = e.dev;
+ ent->ino = e.ino;
+ ent->ctime = ctime;
+ ent->mtime = mtime;
+ *filename_slot = ent;
+ *devino_slot = ent;
+ ++prelink_entry_count;
+ return ent;
+
+error_out:
+ free (ent);
+ error (0, ENOMEM, "Could not insert %s into hash table", filename);
+error_out2:
+ if (filename_slot != NULL)
+ {
+ *filename_slot = &dummy;
+ htab_clear_slot (prelink_filename_htab, filename_slot);
+ }
+ if (devino_slot != NULL && devino_slot != &dummy)
+ {
+ *devino_slot = &dummy;
+ htab_clear_slot (prelink_devino_htab, devino_slot);
+ }
+ free (canon_filename);
+ return NULL;
+}
+
+static int
+deps_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+
+ if (a == NULL)
+ return (b != NULL);
+ if (a != NULL && b == NULL)
+ return -1;
+
+ if (a->type == ET_NONE && b->type != ET_NONE)
+ return 1;
+ if (a->type != ET_NONE && b->type == ET_NONE)
+ return -1;
+
+ /* Libraries with fewest dependencies first. */
+ if (a->ndepends < b->ndepends)
+ return -1;
+ if (a->ndepends > b->ndepends)
+ return 1;
+ return 0;
+}
+
+int
+prelink_load_cache (void)
+{
+ int fd, i, j;
+ struct stat64 st;
+ struct prelink_cache *cache;
+ struct prelink_entry **ents;
+ size_t cache_size;
+ uint32_t string_start, *dep;
+
+ fd = wrap_open (prelink_cache, O_RDONLY);
+ if (fd < 0)
+ return 0; /* The cache does not exist yet. */
+
+ if (fstat64 (fd, &st) < 0
+ || st.st_size == 0)
+ {
+ close (fd);
+ return 0;
+ }
+
+ cache = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (cache == MAP_FAILED)
+ error (EXIT_FAILURE, errno, "mmap of prelink cache file failed.");
+ cache_size = st.st_size;
+ if (memcmp (cache->magic, PRELINK_CACHE_MAGIC,
+ sizeof (PRELINK_CACHE_MAGIC) - 1))
+ {
+ if (memcmp (cache->magic, PRELINK_CACHE_NAME,
+ sizeof (PRELINK_CACHE_NAME) - 1))
+ error (EXIT_FAILURE, 0, "%s: is not prelink cache file",
+ prelink_cache);
+ munmap (cache, cache_size);
+ return 0;
+ }
+ dep = (uint32_t *) & cache->entry[cache->nlibs];
+ string_start = ((long) dep) - ((long) cache)
+ + cache->ndeps * sizeof (uint32_t);
+ ents = (struct prelink_entry **)
+ alloca (cache->nlibs * sizeof (struct prelink_entry *));
+ memset (ents, 0, cache->nlibs * sizeof (struct prelink_entry *));
+ for (i = 0; i < cache->nlibs; i++)
+ {
+ /* Sanity checks. */
+ if (cache->entry[i].filename < string_start
+ || cache->entry[i].filename >= string_start + cache->len_strings
+ || cache->entry[i].depends >= cache->ndeps)
+ error (EXIT_FAILURE, 0, "%s: bogus prelink cache file",
+ prelink_cache);
+
+ ents[i] = prelink_load_entry (((char *) cache)
+ + cache->entry[i].filename);
+ }
+
+ for (i = 0; i < cache->nlibs; i++)
+ {
+ if (ents[i] == NULL)
+ continue;
+
+ if (ents[i]->type != ET_NONE)
+ continue;
+
+ ents[i]->checksum = cache->entry[i].checksum;
+ ents[i]->base = cache->entry[i].base;
+ ents[i]->end = cache->entry[i].end;
+ ents[i]->type = (ents[i]->base == 0 && ents[i]->end == 0)
+ ? ET_CACHE_EXEC : ET_CACHE_DYN;
+ ents[i]->flags = cache->entry[i].flags;
+
+ if (ents[i]->flags == PCF_UNPRELINKABLE)
+ ents[i]->type = (quick || print_cache) ? ET_UNPRELINKABLE : ET_NONE;
+
+ /* If mtime is equal to ctime, assume the filesystem does not store
+ ctime. */
+ if (quick
+ && ((ents[i]->ctime == ents[i]->mtime
+ && ents[i]->type != ET_UNPRELINKABLE)
+ || ents[i]->ctime != cache->entry[i].ctime
+ || ents[i]->mtime != cache->entry[i].mtime))
+ ents[i]->type = ET_NONE;
+
+ for (j = cache->entry[i].depends; dep[j] != i; ++j)
+ if (dep[j] >= cache->nlibs)
+ error (EXIT_FAILURE, 0, "%s: bogus prelink cache file",
+ prelink_cache);
+ else if (ents[dep[j]] == NULL)
+ ents[i]->type = ET_NONE;
+
+ if (ents[i]->type == ET_NONE)
+ continue;
+
+ ents[i]->ndepends = j - cache->entry[i].depends;
+ if (ents[i]->ndepends)
+ {
+ ents[i]->depends =
+ (struct prelink_entry **)
+ malloc (ents[i]->ndepends * sizeof (struct prelink_entry *));
+ if (ents[i]->depends == NULL)
+ error (EXIT_FAILURE, ENOMEM, "Cannot read cache file %s",
+ prelink_cache);
+
+ for (j = 0; j < ents[i]->ndepends; ++j)
+ ents[i]->depends[j] = ents[dep[cache->entry[i].depends + j]];
+ }
+ }
+
+ if (quick)
+ {
+ qsort (ents, cache->nlibs, sizeof (struct prelink_entry *), deps_cmp);
+ for (i = 0; i < cache->nlibs; ++i)
+ {
+ if (ents[i] == NULL || ents[i]->type == ET_NONE)
+ continue;
+
+ for (j = 0; j < ents[i]->ndepends; ++j)
+ if (ents[i]->depends[j]->type == ET_NONE)
+ {
+ ents[i]->type = ET_NONE;
+ free (ents[i]->depends);
+ ents[i]->depends = NULL;
+ ents[i]->ndepends = 0;
+ break;
+ }
+ }
+ }
+
+ munmap (cache, cache_size);
+ close (fd);
+ return 0;
+}
+
+static int
+prelink_print_cache_size (void **p, void *info)
+{
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+ int *psize = (int *) info;
+
+ if ((e->base & 0xffffffff) != e->base
+ || (e->end & 0xffffffff) != e->end)
+ {
+ *psize = 16;
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+prelink_print_cache_object (void **p, void *info)
+{
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+ int *psize = (int *) info, i;
+
+ if (e->type == ET_UNPRELINKABLE)
+ {
+ printf ("%s (not prelinkable)%s\n", e->filename, e->ndepends ? ":" : "");
+ for (i = 0; i < e->ndepends; i++)
+ if (e->depends[i]->type == ET_UNPRELINKABLE)
+ printf (" %s (not prelinkable)\n", e->depends[i]->filename);
+ else
+ printf (" %s [0x%08x]\n", e->depends[i]->filename,
+ e->depends[i]->checksum);
+ return 1;
+ }
+
+ if (e->type == ET_CACHE_DYN)
+ printf ("%s [0x%08x] 0x%0*llx-0x%0*llx%s\n", e->filename, e->checksum,
+ *psize, (long long) e->base, *psize, (long long) e->end,
+ e->ndepends ? ":" : "");
+ else
+ printf ("%s%s\n", e->filename, e->ndepends ? ":" : "");
+ for (i = 0; i < e->ndepends; i++)
+ printf (" %s [0x%08x]\n", e->depends[i]->filename,
+ e->depends[i]->checksum);
+ return 1;
+}
+
+int
+prelink_print_cache (void)
+{
+ int size = 8;
+
+ printf ("%d objects found in prelink cache `%s'\n", prelink_entry_count,
+ prelink_cache);
+
+ htab_traverse (prelink_filename_htab, prelink_print_cache_size, &size);
+ htab_traverse (prelink_filename_htab, prelink_print_cache_object, &size);
+ return 0;
+}
+
+struct collect_ents
+{
+ struct prelink_entry **ents;
+ size_t len_strings;
+ int nents;
+ int ndeps;
+};
+
+static int
+prelink_save_cache_check (struct prelink_entry *ent)
+{
+ int i;
+
+ for (i = 0; i < ent->ndepends; ++i)
+ switch (ent->depends[i]->type)
+ {
+ case ET_DYN:
+ if (ent->depends[i]->done < 2
+ || (quick && (ent->depends[i]->flags & PCF_PRELINKED)))
+ return 1;
+ break;
+ case ET_CACHE_DYN:
+ break;
+ case ET_UNPRELINKABLE:
+ if (ent->type != ET_UNPRELINKABLE)
+ return 1;
+ break;
+ default:
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+find_ents (void **p, void *info)
+{
+ struct collect_ents *l = (struct collect_ents *) info;
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+
+ if (((e->type == ET_DYN || e->type == ET_EXEC) && e->done == 2)
+ || ((e->type == ET_CACHE_DYN || e->type == ET_CACHE_EXEC
+ || e->type == ET_UNPRELINKABLE)
+ && ! prelink_save_cache_check (e)))
+ {
+ l->ents[l->nents++] = e;
+ l->ndeps += e->ndepends + 1;
+ l->len_strings += strlen (e->canon_filename) + 1;
+ }
+ return 1;
+}
+
+int
+prelink_save_cache (int do_warn)
+{
+ struct prelink_cache cache;
+ struct collect_ents l;
+ struct prelink_cache_entry *data;
+ uint32_t *deps, ndeps = 0, i, j, k;
+ char *strings;
+ int fd, len;
+ struct prelink_entry *ents_array[prelink_entry_count];
+
+ memset (&cache, 0, sizeof (cache));
+ memcpy ((char *) & cache, PRELINK_CACHE_MAGIC,
+ sizeof (PRELINK_CACHE_MAGIC) - 1);
+ l.ents = ents_array;
+ l.nents = 0;
+ l.ndeps = 0;
+ l.len_strings = 0;
+ htab_traverse (prelink_filename_htab, find_ents, &l);
+ cache.nlibs = l.nents;
+ cache.ndeps = l.ndeps;
+ cache.len_strings = l.len_strings;
+
+ len = cache.nlibs * sizeof (struct prelink_cache_entry)
+ + cache.ndeps * sizeof (uint32_t) + cache.len_strings;
+ char data_buf[len];
+ data = (struct prelink_cache_entry *) data_buf;
+ deps = (uint32_t *) & data[cache.nlibs];
+ strings = (char *) & deps[cache.ndeps];
+
+ for (i = 0; i < l.nents; ++i)
+ {
+ data[i].filename = (strings - (char *) data) + sizeof (cache);
+ strings = stpcpy (strings, l.ents[i]->canon_filename) + 1;
+ data[i].checksum = l.ents[i]->checksum;
+ data[i].flags = l.ents[i]->flags & ~PCF_PRELINKED;
+ data[i].ctime = l.ents[i]->ctime;
+ data[i].mtime = l.ents[i]->mtime;
+ if (l.ents[i]->type == ET_EXEC || l.ents[i]->type == ET_CACHE_EXEC)
+ {
+ data[i].base = 0;
+ data[i].end = 0;
+ }
+ else if (l.ents[i]->type == ET_UNPRELINKABLE)
+ {
+ data[i].base = 0;
+ data[i].end = 0;
+ data[i].checksum = 0;
+ data[i].flags = PCF_UNPRELINKABLE;
+ }
+ else
+ {
+ data[i].base = l.ents[i]->base;
+ data[i].end = l.ents[i]->end;
+ }
+ }
+
+ for (i = 0; i < cache.nlibs; i++)
+ {
+ data[i].depends = ndeps;
+ for (j = 0; j < l.ents[i]->ndepends; j++)
+ {
+ for (k = 0; k < cache.nlibs; k++)
+ if (l.ents[k] == l.ents[i]->depends[j])
+ break;
+ if (k == cache.nlibs)
+ abort ();
+ deps[ndeps++] = k;
+ }
+ deps[ndeps++] = i;
+ }
+
+ size_t prelink_cache_len = strlen (prelink_cache);
+ char prelink_cache_tmp [prelink_cache_len + sizeof (".XXXXXX")];
+ memcpy (mempcpy (prelink_cache_tmp, prelink_cache, prelink_cache_len),
+ ".XXXXXX", sizeof (".XXXXXX"));
+ fd = wrap_mkstemp (prelink_cache_tmp);
+ if (fd < 0)
+ {
+ error (0, errno, "Could not write prelink cache");
+ return 1;
+ }
+
+ if (write (fd, &cache, sizeof (cache)) != sizeof (cache)
+ || write (fd, data, len) != len
+ || fchmod (fd, 0644)
+ || close (fd)
+ || wrap_rename (prelink_cache_tmp, prelink_cache))
+ {
+ error (0, errno, "Could not write prelink cache");
+ wrap_unlink (prelink_cache_tmp);
+ return 1;
+ }
+ return 0;
+}
+
+#ifndef NDEBUG
+static void
+prelink_entry_dumpfn (FILE *f, const void *ptr)
+{
+ struct prelink_entry *e = (struct prelink_entry *) ptr;
+ struct prelink_link *l;
+ int i;
+
+ fprintf (f, "%s|%s|%s|%x|%x|%llx|%llx|%llx|%llx|%llx|%d|%d|%d|%d|%d|%d|%d|",
+ e->filename,
+ strcmp (e->canon_filename, e->filename) ? e->canon_filename : "",
+ e->soname && strcmp (e->soname, e->filename) ? e->soname : "",
+ e->timestamp, e->checksum,
+ (long long) e->base, (long long) e->end, (long long) e->pltgot,
+ (long long) e->dev, (long long) e->ino,
+ e->type, e->done, e->ndepends, e->refs, e->flags,
+ e->prev ? e->prev->u.tmp : -1, e->next ? e->next->u.tmp : -1);
+ for (i = 0; i < e->ndepends; ++i)
+ fprintf (f, "%d-", e->depends [i]->u.tmp);
+ fputc ('|', f);
+ for (l = e->hardlink; l; l = l->next)
+ fprintf (f, "%s|", l->canon_filename);
+ fputs ("\n", f);
+}
+
+void
+prelink_entry_dump (htab_t htab, const char *filename)
+{
+ size_t i;
+
+ for (i = 0; i < htab->size; ++i)
+ if (htab->entries [i] && htab->entries [i] != (void *) 1)
+ ((struct prelink_entry *) htab->entries [i])->u.tmp = i;
+ htab_dump (htab, filename, prelink_entry_dumpfn);
+}
+
+static char *restore_line;
+static size_t restore_size;
+
+static void *
+prelink_entry_restorefn (FILE *f)
+{
+ struct prelink_entry *e;
+ struct prelink_link **plink;
+ char *p, *q, *s;
+ long long ll[5];
+ int ii[5];
+ int i;
+
+ if (getline (&restore_line, &restore_size, f) < 0)
+ abort ();
+ e = (struct prelink_entry *) calloc (1, sizeof (struct prelink_entry));
+ if (e == NULL)
+ abort ();
+ p = restore_line;
+ q = strchr (p, '|');
+ s = malloc (q - p + 1);
+ memcpy (s, p, q - p);
+ s [q - p] = '\0';
+ e->filename = s;
+ ++q;
+ p = q;
+ if (*p == '|')
+ e->canon_filename = strdup (e->filename);
+ else
+ {
+ q = strchr (p, '|');
+ s = malloc (q - p + 1);
+ memcpy (s, p, q - p);
+ s [q - p] = '\0';
+ e->canon_filename = s;
+ }
+ ++q;
+ p = q;
+ if (*p == '|')
+ e->soname = strdup (e->filename);
+ else
+ {
+ q = strchr (p, '|');
+ s = malloc (q - p + 1);
+ memcpy (s, p, q - p);
+ s [q - p] = '\0';
+ e->soname = s;
+ }
+ p = q + 1;
+ if (sscanf (p, "%x|%x|%llx|%llx|%llx|%llx|%llx|%d|%d|%d|%d|%d|%d|%d|%n",
+ ii, ii + 1, ll, ll + 1, ll + 2, ll + 3, ll + 4,
+ &e->type, &e->done, &e->ndepends, &e->refs, &e->flags,
+ ii + 2, ii + 3, ii + 4) < 14)
+ abort ();
+ e->timestamp = ii[0];
+ e->checksum = ii[1];
+ e->base = ll[0];
+ e->end = ll[1];
+ e->pltgot = ll[2];
+ e->dev = ll[3];
+ e->ino = ll[4];
+ e->prev = (void *) (long) ii[2];
+ e->next = (void *) (long) ii[3];
+ e->depends = (struct prelink_entry **)
+ malloc (e->ndepends * sizeof (struct prelink_entry *));
+ p += ii[4];
+ for (i = 0; i < e->ndepends; ++i)
+ {
+ e->depends [i] = (void *) strtol (p, &q, 0);
+ if (p == q || *q != '-')
+ abort ();
+ p = q + 1;
+ }
+ if (*p++ != '|')
+ abort ();
+ plink = &e->hardlink;
+ while (*p != '\n')
+ {
+ struct prelink_link *link = (struct prelink_link *)
+ malloc (sizeof (struct prelink_link));
+ q = strchr (p, '|');
+ *plink = link;
+ plink = &link->next;
+ s = malloc (q - p + 1);
+ memcpy (s, p, q - p);
+ s [q - p] = '\0';
+ e->soname = s;
+ link->canon_filename = s;
+ p = q + 1;
+ }
+ *plink = NULL;
+ ++prelink_entry_count;
+ return e;
+}
+
+void
+prelink_entry_restore (htab_t htab, const char *filename)
+{
+ size_t i, j;
+ struct prelink_entry *e;
+
+ prelink_entry_count = 0;
+ htab_restore (htab, filename, prelink_entry_restorefn);
+ free (restore_line);
+ for (i = 0; i < htab->size; ++i)
+ if (htab->entries [i] && htab->entries [i] != (void *) 1)
+ {
+ e = (struct prelink_entry *) htab->entries [i];
+ if (e->prev == (void *) -1)
+ e->prev = NULL;
+ else
+ e->prev = (struct prelink_entry *)
+ htab->entries [(long) e->prev];
+ if (e->next == (void *) -1)
+ e->next = NULL;
+ else
+ e->next = (struct prelink_entry *)
+ htab->entries [(long) e->next];
+ for (j = 0; j < e->ndepends; ++j)
+ {
+ e->depends [j] = (struct prelink_entry *)
+ htab->entries [(long) e->depends [j]];
+ }
+ }
+}
+#endif
diff --git a/trunk/src/canonicalize.c b/trunk/src/canonicalize.c
new file mode 100644
index 0000000..79df44e
--- /dev/null
+++ b/trunk/src/canonicalize.c
@@ -0,0 +1,319 @@
+/* Return the canonical absolute name of a given file.
+ Copyright (C) 1996-2002, 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
+ 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 <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <stddef.h>
+
+#include "hashtab.h"
+
+htab_t prelink_dirname_htab;
+
+struct dirname_entry
+{
+ const char *dirname;
+ size_t dirname_len;
+ const char *canon_dirname;
+ size_t canon_dirname_len;
+};
+
+static hashval_t
+dirname_hash (const void *p)
+{
+ struct dirname_entry *e = (struct dirname_entry *)p;
+ const unsigned char *s = (const unsigned char *)e->dirname;
+ hashval_t h = 0;
+ unsigned char c;
+ size_t len = e->dirname_len;
+
+ while (len--)
+ {
+ c = *s++;
+ h += c + (c << 17);
+ h ^= h >> 2;
+ }
+ return h + e->dirname_len + (e->dirname_len << 17);
+}
+
+static int
+dirname_eq (const void *p, const void *q)
+{
+ struct dirname_entry *e = (struct dirname_entry *)p;
+ struct dirname_entry *f = (struct dirname_entry *)q;
+
+ return (e->dirname_len == f->dirname_len
+ && memcmp (e->dirname, f->dirname, e->dirname_len) == 0);
+}
+
+/* Return the canonical absolute name of file NAME. A canonical name
+ does not contain any `.', `..' components nor any repeated path
+ separators ('/') or symlinks. All path components must exist.
+ The result is malloc'd. */
+
+static char *
+canon_filename (const char *name, int nested, struct stat64 *stp)
+{
+ char *rpath, *dest, *extra_buf = NULL;
+ const char *start, *end, *rpath_limit;
+ long int path_max;
+ int num_links = 0;
+ int stp_initialized = 0;
+
+ if (name == NULL)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (name[0] == '\0')
+ {
+ errno = ENOENT;
+ return NULL;
+ }
+
+#ifdef PATH_MAX
+ path_max = PATH_MAX;
+#else
+ path_max = pathconf (name, _PC_PATH_MAX);
+ if (path_max <= 0)
+ path_max = 1024;
+#endif
+
+ rpath = malloc (path_max);
+ if (rpath == NULL)
+ return NULL;
+ rpath_limit = rpath + path_max;
+
+ if (name[0] != '/')
+ {
+ if (!getcwd (rpath, path_max))
+ {
+ rpath[0] = '\0';
+ goto error;
+ }
+ dest = strchr (rpath, '\0');
+ }
+ else
+ {
+ rpath[0] = '/';
+ dest = rpath + 1;
+
+ if (!nested)
+ {
+ if (prelink_dirname_htab == NULL)
+ prelink_dirname_htab = htab_try_create (100, dirname_hash,
+ dirname_eq, NULL);
+ if (prelink_dirname_htab == NULL)
+ nested = 1;
+ }
+ if (!nested)
+ {
+ struct dirname_entry e;
+ void **dirname_slot;
+
+ end = strrchr (name, '/');
+
+ e.dirname = name;
+ e.dirname_len = end - name;
+ dirname_slot = htab_find_slot (prelink_dirname_htab, &e, INSERT);
+ if (*dirname_slot == NULL)
+ {
+ struct dirname_entry *ep = malloc (sizeof (struct dirname_entry)
+ + e.dirname_len + 1);
+ if (ep != NULL)
+ {
+ char *dirname = (char *) (ep + 1);
+ struct stat64 st;
+
+ ep->dirname = (const char *) dirname;
+ ep->dirname_len = e.dirname_len;
+ memcpy (dirname, name, ep->dirname_len);
+ dirname[ep->dirname_len] = '\0';
+ ep->canon_dirname = canon_filename (ep->dirname, 1, &st);
+ if (ep->canon_dirname == NULL || !S_ISDIR (st.st_mode))
+ free (ep);
+ else
+ {
+ ep->canon_dirname_len = strlen (ep->canon_dirname);
+ *dirname_slot = ep;
+ }
+ }
+ }
+
+ if (*dirname_slot != NULL)
+ {
+ struct dirname_entry *ep = *dirname_slot;
+
+ if (rpath + ep->canon_dirname_len + 1 >= rpath_limit)
+ {
+ size_t new_size;
+ char *new_rpath;
+
+ new_size = rpath_limit - rpath;
+ if (ep->canon_dirname_len + 1 > path_max)
+ new_size += ep->canon_dirname_len + 1;
+ else
+ new_size += path_max;
+ new_rpath = (char *) realloc (rpath, new_size);
+ if (new_rpath == NULL)
+ goto error;
+ rpath = new_rpath;
+ rpath_limit = rpath + new_size;
+ }
+ dest = mempcpy (rpath, ep->canon_dirname, ep->canon_dirname_len);
+ *dest = '\0';
+ name = end + 1;
+ }
+ }
+ }
+
+ for (start = end = name; *start; start = end)
+ {
+ int n;
+
+ /* Skip sequence of multiple path-separators. */
+ while (*start == '/')
+ ++start;
+
+ /* Find end of path component. */
+ for (end = start; *end && *end != '/'; ++end)
+ /* Nothing. */;
+
+ if (end - start == 0)
+ break;
+ else if (end - start == 1 && start[0] == '.')
+ /* nothing */;
+ else if (end - start == 2 && start[0] == '.' && start[1] == '.')
+ {
+ /* Back up to previous component, ignore if at root already. */
+ if (dest > rpath + 1)
+ while ((--dest)[-1] != '/');
+ stp_initialized = 0;
+ }
+ else
+ {
+ size_t new_size;
+
+ if (dest[-1] != '/')
+ *dest++ = '/';
+
+ if (dest + (end - start) >= rpath_limit)
+ {
+ ptrdiff_t dest_offset = dest - rpath;
+ char *new_rpath;
+
+ new_size = rpath_limit - rpath;
+ if (end - start + 1 > path_max)
+ new_size += end - start + 1;
+ else
+ new_size += path_max;
+ new_rpath = (char *) realloc (rpath, new_size);
+ if (new_rpath == NULL)
+ goto error;
+ rpath = new_rpath;
+ rpath_limit = rpath + new_size;
+
+ dest = rpath + dest_offset;
+ }
+
+ dest = mempcpy (dest, start, end - start);
+ *dest = '\0';
+
+ if (lstat64 (rpath, stp) < 0)
+ goto error;
+
+ stp_initialized = 1;
+
+ if (S_ISLNK (stp->st_mode))
+ {
+ char *buf = alloca (path_max);
+ size_t len;
+
+ if (++num_links > MAXSYMLINKS)
+ {
+ errno = ELOOP;
+ goto error;
+ }
+
+ n = readlink (rpath, buf, path_max);
+ if (n < 0)
+ goto error;
+ buf[n] = '\0';
+
+ if (!extra_buf)
+ extra_buf = alloca (path_max);
+
+ len = strlen (end);
+ if ((long int) (n + len) >= path_max)
+ {
+ errno = ENAMETOOLONG;
+ goto error;
+ }
+
+ /* Careful here, end may be a pointer into extra_buf... */
+ memmove (&extra_buf[n], end, len + 1);
+ name = end = memcpy (extra_buf, buf, n);
+
+ if (buf[0] == '/')
+ dest = rpath + 1; /* It's an absolute symlink */
+ else
+ /* Back up to previous component, ignore if at root already: */
+ if (dest > rpath + 1)
+ while ((--dest)[-1] != '/');
+ }
+ else if (!S_ISDIR (stp->st_mode) && *end != '\0')
+ {
+ errno = ENOTDIR;
+ goto error;
+ }
+ }
+ }
+ if (dest > rpath + 1 && dest[-1] == '/')
+ --dest;
+ *dest = '\0';
+
+ if (!stp_initialized && lstat64 (rpath, stp) < 0)
+ goto error;
+
+ if (dest + 1 - rpath <= (rpath_limit - rpath) / 2)
+ {
+ char *new_rpath = realloc (rpath, dest + 1 - rpath);
+
+ if (new_rpath != NULL)
+ return new_rpath;
+ }
+ return rpath;
+
+error:
+ free (rpath);
+ return NULL;
+}
+
+char *
+prelink_canonicalize (const char *name, struct stat64 *stp)
+{
+ struct stat64 st;
+ return canon_filename (name, 0, stp ? stp : &st);
+}
diff --git a/trunk/src/checksum.c b/trunk/src/checksum.c
new file mode 100644
index 0000000..20a23a0
--- /dev/null
+++ b/trunk/src/checksum.c
@@ -0,0 +1,89 @@
+/* Copyright (C) 2001, 2002, 2003 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 <endian.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelink.h"
+
+int
+prelink_set_checksum (DSO *dso)
+{
+ extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
+ uint32_t crc;
+ int i, cvt;
+
+ if (set_dynamic (dso, DT_CHECKSUM, 0, 1))
+ return 1;
+
+ if (dso->info_DT_GNU_PRELINKED
+ && set_dynamic (dso, DT_GNU_PRELINKED, 0, 1))
+ return 1;
+
+ /* Ensure any pending .mdebug/.dynsym/.dynstr etc. modifications
+ write_dso would do happen before checksumming. */
+ if (prepare_write_dso (dso))
+ return 1;
+
+ cvt = ! ((__BYTE_ORDER == __LITTLE_ENDIAN
+ && dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+ || (__BYTE_ORDER == __BIG_ENDIAN
+ && dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB));
+ crc = 0;
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ if (! (dso->shdr[i].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR)))
+ continue;
+ if (dso->shdr[i].sh_type != SHT_NOBITS && dso->shdr[i].sh_size)
+ {
+ Elf_Scn *scn = dso->scn[i];
+ Elf_Data *d = NULL;
+
+ /* Cannot use elf_rawdata here, since the image is not written
+ yet. */
+ while ((d = elf_getdata (scn, d)) != NULL)
+ {
+ if (cvt && d->d_type != ELF_T_BYTE)
+ {
+ gelf_xlatetof (dso->elf, d, d,
+ dso->ehdr.e_ident[EI_DATA]);
+ crc = crc32 (crc, d->d_buf, d->d_size);
+ gelf_xlatetom (dso->elf, d, d,
+ dso->ehdr.e_ident[EI_DATA]);
+ }
+ else
+ crc = crc32 (crc, d->d_buf, d->d_size);
+ }
+ }
+ }
+
+ if (set_dynamic (dso, DT_CHECKSUM, crc, 1))
+ abort ();
+ if (dso->info_DT_GNU_PRELINKED
+ && set_dynamic (dso, DT_GNU_PRELINKED, dso->info_DT_GNU_PRELINKED, 1))
+ abort ();
+ dso->info_DT_CHECKSUM = crc;
+
+ return 0;
+}
diff --git a/trunk/src/conflict.c b/trunk/src/conflict.c
new file mode 100644
index 0000000..db36054
--- /dev/null
+++ b/trunk/src/conflict.c
@@ -0,0 +1,804 @@
+/* Copyright (C) 2001, 2002, 2003, 2004 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+
+struct prelink_conflict *
+prelink_conflict (struct prelink_info *info, GElf_Word r_sym,
+ int reloc_type)
+{
+ GElf_Word symoff = info->symtab_start + r_sym * info->symtab_entsize;
+ struct prelink_conflict *conflict;
+ int reloc_class = info->dso->arch->reloc_class (reloc_type);
+
+ for (conflict = info->curconflicts; conflict; conflict = conflict->next)
+ if (conflict->symoff == symoff && conflict->reloc_class == reloc_class)
+ {
+ conflict->used = 1;
+ return conflict;
+ }
+
+ return NULL;
+}
+
+GElf_Rela *
+prelink_conflict_add_rela (struct prelink_info *info)
+{
+ GElf_Rela *ret;
+
+ if (info->conflict_rela_alloced == info->conflict_rela_size)
+ {
+ info->conflict_rela_alloced += 10;
+ info->conflict_rela = realloc (info->conflict_rela,
+ info->conflict_rela_alloced
+ * sizeof (GElf_Rela));
+ if (info->conflict_rela == NULL)
+ {
+ error (0, ENOMEM, "Could not build .gnu.conflict section memory image");
+ return NULL;
+ }
+ }
+ ret = info->conflict_rela + info->conflict_rela_size++;
+ ret->r_offset = 0;
+ ret->r_info = 0;
+ ret->r_addend = 0;
+ return ret;
+}
+
+static int
+prelink_conflict_rel (DSO *dso, int n, struct prelink_info *info)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rel rel;
+ int sec, ndx, maxndx;
+
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrel (dso->elf, data, ndx, &rel);
+ sec = addr_to_sec (dso, rel.r_offset);
+ if (sec == -1)
+ continue;
+
+ if (dso->arch->prelink_conflict_rel (dso, info, &rel, addr))
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int
+prelink_conflict_rela (DSO *dso, int n, struct prelink_info *info)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rela rela;
+ int sec, ndx, maxndx;
+
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrela (dso->elf, data, ndx, &rela);
+ sec = addr_to_sec (dso, rela.r_offset);
+ if (sec == -1)
+ continue;
+
+ if (dso->arch->prelink_conflict_rela (dso, info, &rela, addr))
+ return 1;
+ }
+ }
+ return 0;
+}
+
+struct copy_relocs
+{
+ GElf_Rela *rela;
+ int alloced;
+ int count;
+};
+
+static int
+prelink_add_copy_rel (DSO *dso, int n, GElf_Rel *rel, struct copy_relocs *cr)
+{
+ Elf_Data *data = NULL;
+ int symsec = dso->shdr[n].sh_link;
+ Elf_Scn *scn = dso->scn[symsec];
+ GElf_Sym sym;
+ size_t entsize = dso->shdr[symsec].sh_entsize;
+ off_t off = GELF_R_SYM (rel->r_info) * entsize;
+
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ if (data->d_off <= off &&
+ data->d_off + data->d_size >= off + entsize)
+ {
+ gelfx_getsym (dso->elf, data, (off - data->d_off) / entsize, &sym);
+ if (sym.st_size == 0)
+ {
+ error (0, 0, "%s: Copy reloc against symbol with zero size",
+ dso->filename);
+ return 1;
+ }
+
+ if (cr->alloced == cr->count)
+ {
+ cr->alloced += 10;
+ cr->rela = realloc (cr->rela, cr->alloced * sizeof (GElf_Rela));
+ if (cr->rela == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not build list of COPY relocs",
+ dso->filename);
+ return 1;
+ }
+ }
+ cr->rela[cr->count].r_offset = rel->r_offset;
+ cr->rela[cr->count].r_info = rel->r_info;
+ cr->rela[cr->count].r_addend = sym.st_size;
+ ++cr->count;
+ return 0;
+ }
+ }
+
+ error (0, 0, "%s: Copy reloc against unknown symbol", dso->filename);
+ return 1;
+}
+
+static int
+prelink_find_copy_rel (DSO *dso, int n, struct copy_relocs *cr)
+{
+ 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;
+
+ if (GELF_R_TYPE (rel.r_info) == dso->arch->R_COPY
+ && prelink_add_copy_rel (dso, n, &rel, cr))
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int
+prelink_find_copy_rela (DSO *dso, int n, struct copy_relocs *cr)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ union {
+ GElf_Rel rel;
+ GElf_Rela rela;
+ } u;
+ 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, &u.rela);
+ sec = addr_to_sec (dso, u.rela.r_offset);
+ if (sec == -1)
+ continue;
+
+ if (GELF_R_TYPE (u.rela.r_info) == dso->arch->R_COPY)
+ {
+ if (u.rela.r_addend != 0)
+ {
+ error (0, 0, "%s: COPY reloc with non-zero addend?",
+ dso->filename);
+ return 1;
+ }
+ if (prelink_add_copy_rel (dso, n, &u.rel, cr))
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+rela_cmp (const void *A, const void *B)
+{
+ GElf_Rela *a = (GElf_Rela *)A;
+ GElf_Rela *b = (GElf_Rela *)B;
+
+ if (a->r_offset < b->r_offset)
+ return -1;
+ if (a->r_offset > b->r_offset)
+ return 1;
+ return 0;
+}
+
+static int
+conflict_rela_cmp (const void *A, const void *B)
+{
+ GElf_Rela *a = (GElf_Rela *)A;
+ GElf_Rela *b = (GElf_Rela *)B;
+
+ if (GELF_R_SYM (a->r_info) < GELF_R_SYM (b->r_info))
+ return -1;
+ if (GELF_R_SYM (a->r_info) > GELF_R_SYM (b->r_info))
+ return 1;
+ if (a->r_offset < b->r_offset)
+ return -1;
+ if (a->r_offset > b->r_offset)
+ return 1;
+ return 0;
+}
+
+int
+get_relocated_mem (struct prelink_info *info, DSO *dso, GElf_Addr addr,
+ char *buf, GElf_Word size)
+{
+ int sec = addr_to_sec (dso, addr), j;
+ Elf_Scn *scn;
+ Elf_Data *data;
+ off_t off;
+
+ if (sec == -1)
+ return 1;
+
+ memset (buf, 0, size);
+ if (dso->shdr[sec].sh_type != SHT_NOBITS)
+ {
+ scn = dso->scn[sec];
+ data = NULL;
+ off = addr - dso->shdr[sec].sh_addr;
+ while ((data = elf_rawdata (scn, data)) != NULL)
+ {
+ if (data->d_off < off + size
+ && data->d_off + data->d_size > off)
+ {
+ off_t off2 = off - data->d_off;
+ size_t len = size;
+
+ if (off2 < 0)
+ {
+ len += off2;
+ off2 = 0;
+ }
+ if (off2 + len > data->d_size)
+ len = data->d_size - off2;
+ assert (off2 + len <= data->d_size);
+ assert (len <= size);
+ memcpy (buf + off2 - off, data->d_buf + off2, len);
+ }
+ }
+ }
+
+ if (info->dso != dso)
+ {
+ /* This is tricky. We need to apply any conflicts
+ against memory area which we've copied to the COPY
+ reloc offset. */
+ for (j = 0; j < info->conflict_rela_size; ++j)
+ {
+ int reloc_type, reloc_size;
+ off_t off;
+
+ if (info->conflict_rela[j].r_offset >= addr + size)
+ continue;
+ if (info->conflict_rela[j].r_offset + dso->arch->max_reloc_size
+ <= addr)
+ continue;
+
+ reloc_type = GELF_R_TYPE (info->conflict_rela[j].r_info);
+ reloc_size = dso->arch->reloc_size (reloc_type);
+ if (info->conflict_rela[j].r_offset + reloc_size <= addr)
+ continue;
+
+ off = info->conflict_rela[j].r_offset - addr;
+
+ /* Check if whole relocation fits into the area.
+ Punt if not. */
+ if (off < 0 || size - off < reloc_size)
+ return 2;
+ /* Note that apply_conflict_rela shouldn't rely on R_SYM
+ field of conflict to be 0. */
+ dso->arch->apply_conflict_rela (info, info->conflict_rela + j,
+ buf + off);
+ }
+ }
+ else
+ {
+ int i, ndx, maxndx;
+ int reloc_type, reloc_size;
+ union { GElf_Rel rel; GElf_Rela rela; } u;
+ off_t off;
+
+ if (addr + size > info->dynbss_base
+ && addr < info->dynbss_base + info->dynbss_size)
+ {
+ if (addr < info->dynbss_base
+ || addr + size > info->dynbss_base + info->dynbss_size)
+ return 4;
+
+ memcpy (buf, info->dynbss + (addr - info->dynbss_base), size);
+ return 0;
+ }
+
+ if (addr + size > info->sdynbss_base
+ && addr < info->sdynbss_base + info->sdynbss_size)
+ {
+ if (addr < info->sdynbss_base
+ || addr + size > info->sdynbss_base + info->sdynbss_size)
+ return 4;
+
+ memcpy (buf, info->sdynbss + (addr - info->sdynbss_base), size);
+ return 0;
+ }
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+
+ if (! (dso->shdr[i].sh_flags & SHF_ALLOC))
+ continue;
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".gnu.conflict"))
+ continue;
+ switch (dso->shdr[i].sh_type)
+ {
+ case SHT_REL:
+ case SHT_RELA:
+ break;
+ default:
+ continue;
+ }
+ scn = dso->scn[i];
+ data = NULL;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ maxndx = data->d_size / dso->shdr[i].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ {
+ if (dso->shdr[i].sh_type == SHT_REL)
+ gelfx_getrel (dso->elf, data, ndx, &u.rel);
+ else
+ gelfx_getrela (dso->elf, data, ndx, &u.rela);
+
+ if (u.rel.r_offset >= addr + size)
+ continue;
+ if (u.rel.r_offset + dso->arch->max_reloc_size <= addr)
+ continue;
+
+ reloc_type = GELF_R_TYPE (u.rel.r_info);
+ reloc_size = dso->arch->reloc_size (reloc_type);
+ if (u.rel.r_offset + reloc_size <= addr)
+ continue;
+
+ if (reloc_type == dso->arch->R_COPY)
+ return 3;
+
+ off = u.rel.r_offset - addr;
+
+ /* Check if whole relocation fits into the area.
+ Punt if not. */
+ if (off < 0 || size - off < reloc_size)
+ return 2;
+
+ if (dso->shdr[i].sh_type == SHT_REL)
+ dso->arch->apply_rel (info, &u.rel, buf + off);
+ else
+ dso->arch->apply_rela (info, &u.rela, buf + off);
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+int
+prelink_build_conflicts (struct prelink_info *info)
+{
+ int i, ndeps = info->ent->ndepends + 1;
+ struct prelink_entry *ent;
+ int ret = 0;
+ DSO *dso;
+ struct copy_relocs cr;
+
+ info->dsos = alloca (sizeof (struct DSO *) * ndeps);
+ memset (info->dsos, 0, sizeof (struct DSO *) * ndeps);
+ memset (&cr, 0, sizeof (cr));
+ info->dsos[0] = info->dso;
+ for (i = 1; i < ndeps; ++i)
+ {
+ ent = info->ent->depends[i - 1];
+ if ((dso = open_dso (ent->filename)) == NULL)
+ goto error_out;
+ info->dsos[i] = dso;
+ /* Now check that the DSO matches what we recorded about it. */
+ if (ent->timestamp != dso->info_DT_GNU_PRELINKED
+ || ent->checksum != dso->info_DT_CHECKSUM
+ || ent->base != dso->base)
+ {
+ error (0, 0, "%s: Library %s has changed since it has been prelinked",
+ info->dso->filename, ent->filename);
+ goto error_out;
+ }
+ }
+
+ for (i = 1; i < ndeps; ++i)
+ {
+ dso = info->dsos[i];
+ ent = info->ent->depends[i - 1];
+
+ /* Verify .gnu.liblist sections of all dependent libraries. */
+ if (ent->ndepends > 0)
+ {
+ int j;
+ const char *name;
+ int nliblist;
+ Elf32_Lib *liblist;
+ Elf_Scn *scn;
+ Elf_Data *data;
+
+ for (j = 1; j < dso->ehdr.e_shnum; ++j)
+ if (dso->shdr[j].sh_type == SHT_GNU_LIBLIST
+ && (name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[j].sh_name))
+ && ! strcmp (name, ".gnu.liblist")
+ && (dso->shdr[j].sh_size % sizeof (Elf32_Lib)) == 0)
+ break;
+
+ if (j == dso->ehdr.e_shnum)
+ {
+ error (0, 0, "%s: Library %s has dependencies, but doesn't contain .gnu.liblist section",
+ info->dso->filename, ent->filename);
+ goto error_out;
+ }
+
+ nliblist = dso->shdr[j].sh_size / sizeof (Elf32_Lib);
+ scn = dso->scn[j];
+ data = elf_getdata (scn, NULL);
+ if (data == NULL || elf_getdata (scn, data)
+ || data->d_buf == NULL || data->d_off
+ || data->d_size != dso->shdr[j].sh_size)
+ {
+ error (0, 0, "%s: Could not read .gnu.liblist section from %s",
+ info->dso->filename, ent->filename);
+ goto error_out;
+ }
+
+ if (nliblist != ent->ndepends)
+ {
+ error (0, 0, "%s: Library %s has different number of libs in .gnu.liblist than expected",
+ info->dso->filename, ent->filename);
+ goto error_out;
+ }
+ liblist = (Elf32_Lib *) data->d_buf;
+ for (j = 0; j < nliblist; ++j)
+ if (liblist[j].l_time_stamp != ent->depends[j]->timestamp
+ || liblist[j].l_checksum != ent->depends[j]->checksum)
+ {
+ error (0, 0, "%s: .gnu.liblist in library %s is inconsistent with recorded dependencies",
+ info->dso->filename, ent->filename);
+ goto error_out;
+ }
+
+ /* Extra check, maybe not needed. */
+ for (j = 0; j < nliblist; ++j)
+ {
+ int k;
+ for (k = 0; k < info->ent->ndepends; ++k)
+ if (liblist[j].l_time_stamp == info->ent->depends[k]->timestamp
+ && liblist[j].l_checksum == info->ent->depends[k]->checksum)
+ break;
+
+ if (k == info->ent->ndepends)
+ abort ();
+ }
+ }
+
+ if (info->conflicts[i] || info->tls[i].modid)
+ {
+ int j, sec, first_conflict;
+ struct prelink_conflict *conflict;
+
+ info->curconflicts = info->conflicts[i];
+ info->curtls = info->tls[i].modid ? info->tls + i : NULL;
+ first_conflict = info->conflict_rela_size;
+ sec = addr_to_sec (dso, dso->info[DT_SYMTAB]);
+ /* DT_SYMTAB should be found and should point to
+ start of .dynsym section. */
+ if (sec == -1
+ || dso->info[DT_SYMTAB] != dso->shdr[sec].sh_addr)
+ {
+ error (0, 0, "Bad symtab");
+ goto error_out;
+ }
+ info->symtab_start = dso->shdr[sec].sh_addr - dso->base;
+ info->symtab_end = info->symtab_start + dso->shdr[sec].sh_size;
+ for (j = 0; j < dso->ehdr.e_shnum; ++j)
+ {
+ if (! (dso->shdr[j].sh_flags & SHF_ALLOC))
+ continue;
+ switch (dso->shdr[j].sh_type)
+ {
+ case SHT_REL:
+ if (prelink_conflict_rel (dso, j, info))
+ goto error_out;
+ break;
+ case SHT_RELA:
+ if (prelink_conflict_rela (dso, j, info))
+ goto error_out;
+ break;
+ }
+ }
+
+ if (dso->arch->arch_prelink_conflict
+ && dso->arch->arch_prelink_conflict (dso, info))
+ goto error_out;
+
+ for (conflict = info->curconflicts; conflict;
+ conflict = conflict->next)
+ if (! conflict->used)
+ {
+ error (0, 0, "%s: Conflict %08llx not found in any relocation",
+ dso->filename, (unsigned long long) conflict->symoff);
+ ret = 1;
+ }
+
+ /* Record library's position in search scope into R_SYM field. */
+ for (j = first_conflict; j < info->conflict_rela_size; ++j)
+ info->conflict_rela[j].r_info
+ = GELF_R_INFO (i, GELF_R_TYPE (info->conflict_rela[j].r_info));
+
+ if (dynamic_info_is_set (dso, DT_TEXTREL)
+ && info->conflict_rela_size > first_conflict)
+ {
+ /* We allow prelinking against non-PIC libraries, as long as
+ no conflict is against read-only segment. */
+ int k;
+
+ for (j = first_conflict; j < info->conflict_rela_size; ++j)
+ for (k = 0; k < dso->ehdr.e_phnum; ++k)
+ if (dso->phdr[k].p_type == PT_LOAD
+ && (dso->phdr[k].p_flags & PF_W) == 0
+ && dso->phdr[k].p_vaddr
+ <= info->conflict_rela[j].r_offset
+ && dso->phdr[k].p_vaddr + dso->phdr[k].p_memsz
+ > info->conflict_rela[j].r_offset)
+ {
+ error (0, 0, "%s: Cannot prelink against non-PIC shared library %s",
+ info->dso->filename, dso->filename);
+ goto error_out;
+ }
+ }
+ }
+ }
+
+ dso = info->dso;
+ for (i = 0; i < dso->ehdr.e_shnum; ++i)
+ {
+ if (! (dso->shdr[i].sh_flags & SHF_ALLOC))
+ continue;
+ switch (dso->shdr[i].sh_type)
+ {
+ case SHT_REL:
+ if (prelink_find_copy_rel (dso, i, &cr))
+ goto error_out;
+ break;
+ case SHT_RELA:
+ if (prelink_find_copy_rela (dso, i, &cr))
+ goto error_out;
+ break;
+ }
+ }
+
+ if (cr.count)
+ {
+ int bss1, bss2, firstbss2 = 0;
+ const char *name;
+
+ qsort (cr.rela, cr.count, sizeof (GElf_Rela), rela_cmp);
+ bss1 = addr_to_sec (dso, cr.rela[0].r_offset);
+ bss2 = addr_to_sec (dso, cr.rela[cr.count - 1].r_offset);
+ if (bss1 != bss2)
+ {
+ for (i = 1; i < cr.count; ++i)
+ if (cr.rela[i].r_offset
+ > dso->shdr[bss1].sh_addr + dso->shdr[bss1].sh_size)
+ break;
+ if (cr.rela[i].r_offset < dso->shdr[bss2].sh_addr)
+ {
+ error (0, 0, "%s: Copy relocs against 3 or more sections",
+ dso->filename);
+ goto error_out;
+ }
+ firstbss2 = i;
+ info->sdynbss_size = cr.rela[i - 1].r_offset - cr.rela[0].r_offset;
+ info->sdynbss_size += cr.rela[i - 1].r_addend;
+ info->sdynbss = calloc (info->sdynbss_size, 1);
+ info->sdynbss_base = cr.rela[0].r_offset;
+ if (info->sdynbss == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot build .sdynbss", dso->filename);
+ goto error_out;
+ }
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->shdr[bss1].sh_addr >= dso->phdr[i].p_vaddr
+ && dso->shdr[bss1].sh_addr
+ < dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz)
+ break;
+ if (i == dso->ehdr.e_phnum
+ || dso->shdr[bss2].sh_addr + dso->shdr[bss2].sh_size
+ > dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz)
+ {
+ error (0, 0, "%s: Copy relocs against more than one segment",
+ dso->filename);
+ goto error_out;
+ }
+ }
+
+ info->dynbss_size = cr.rela[cr.count - 1].r_offset
+ - cr.rela[firstbss2].r_offset;
+ info->dynbss_size += cr.rela[cr.count - 1].r_addend;
+ info->dynbss = calloc (info->dynbss_size, 1);
+ info->dynbss_base = cr.rela[firstbss2].r_offset;
+ if (info->dynbss == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot build .dynbss", dso->filename);
+ goto error_out;
+ }
+
+ /* emacs apparently has .rel.bss relocations against .data section,
+ crap. */
+ if (dso->shdr[bss1].sh_type != SHT_NOBITS
+ && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[bss1].sh_name),
+ ".dynbss") != 0
+ && strcmp (name, ".sdynbss") != 0)
+ {
+ error (0, 0, "%s: COPY relocations don't point into .bss or .sbss section",
+ dso->filename);
+ goto error_out;
+ }
+ if (bss1 != bss2
+ && dso->shdr[bss2].sh_type != SHT_NOBITS
+ && strcmp (name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[bss2].sh_name),
+ ".dynbss") != 0
+ && strcmp (name, ".sdynbss") != 0)
+ {
+ error (0, 0, "%s: COPY relocations don't point into .bss or .sbss section",
+ dso->filename);
+ goto error_out;
+ }
+
+ for (i = 0; i < cr.count; ++i)
+ {
+ struct prelink_symbol *s;
+ DSO *ndso = NULL;
+ int j, reloc_class;
+
+ reloc_class
+ = dso->arch->reloc_class (GELF_R_TYPE (cr.rela[i].r_info));
+
+ assert (reloc_class != RTYPE_CLASS_TLS);
+
+ for (s = & info->symbols[GELF_R_SYM (cr.rela[i].r_info)]; s;
+ s = s->next)
+ if (s->reloc_class == reloc_class)
+ break;
+
+ if (s == NULL || s->u.ent == NULL)
+ {
+ error (0, 0, "%s: Could not find symbol copy reloc is against",
+ dso->filename);
+ goto error_out;
+ }
+
+ for (j = 1; j < ndeps; ++j)
+ if (info->ent->depends[j - 1] == s->u.ent)
+ {
+ ndso = info->dsos[j];
+ break;
+ }
+
+ assert (j < ndeps);
+ if (i < firstbss2)
+ j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
+ info->sdynbss + cr.rela[i].r_offset
+ - info->sdynbss_base, cr.rela[i].r_addend);
+ else
+ j = get_relocated_mem (info, ndso, s->u.ent->base + s->value,
+ info->dynbss + cr.rela[i].r_offset
+ - info->dynbss_base, cr.rela[i].r_addend);
+
+ switch (j)
+ {
+ case 1:
+ error (0, 0, "%s: Could not find variable copy reloc is against",
+ dso->filename);
+ goto error_out;
+ case 2:
+ error (0, 0, "%s: Conflict partly overlaps with %08llx-%08llx area",
+ dso->filename,
+ (long long) cr.rela[i].r_offset,
+ (long long) (cr.rela[i].r_offset + cr.rela[i].r_addend));
+ goto error_out;
+ }
+ }
+ }
+
+ if (info->conflict_rela_size)
+ {
+ qsort (info->conflict_rela, info->conflict_rela_size, sizeof (GElf_Rela),
+ conflict_rela_cmp);
+
+ /* Now make sure all conflict RELA's are against absolute 0 symbol. */
+ for (i = 0; i < info->conflict_rela_size; ++i)
+ info->conflict_rela[i].r_info
+ = GELF_R_INFO (0, GELF_R_TYPE (info->conflict_rela[i].r_info));
+
+ if (enable_cxx_optimizations && remove_redundant_cxx_conflicts (info))
+ goto error_out;
+ }
+
+ for (i = 1; i < ndeps; ++i)
+ if (info->dsos[i])
+ close_dso (info->dsos[i]);
+
+ info->dsos = NULL;
+ free (cr.rela);
+ return ret;
+
+error_out:
+ free (cr.rela);
+ free (info->dynbss);
+ free (info->sdynbss);
+ info->dynbss = NULL;
+ info->sdynbss = NULL;
+ for (i = 1; i < ndeps; ++i)
+ if (info->dsos[i])
+ close_dso (info->dsos[i]);
+ return 1;
+}
diff --git a/trunk/src/crc32.c b/trunk/src/crc32.c
new file mode 100644
index 0000000..f36abb5
--- /dev/null
+++ b/trunk/src/crc32.c
@@ -0,0 +1,87 @@
+/* Copyright (C) 2001 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 <stdint.h>
+#include <sys/types.h>
+
+/* Table computed with Mark Adler's makecrc.c utility. */
+static const uint32_t crc32_table[256] =
+{
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
+ 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
+ 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
+ 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
+ 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
+ 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+ 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
+ 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
+ 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
+ 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
+ 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
+ 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
+ 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
+ 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
+ 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
+ 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
+ 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
+ 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
+ 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
+ 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
+ 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
+ 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
+ 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
+ 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
+ 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
+ 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
+ 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
+ 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
+ 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
+ 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
+ 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
+ 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
+ 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
+ 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
+ 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
+ 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
+ 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
+ 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
+ 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
+ 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
+ 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
+ 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
+ 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
+ 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
+ 0x2d02ef8d
+};
+
+uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len)
+{
+ unsigned char *end;
+
+ crc = ~crc;
+ for (end = buf + len; buf < end; ++buf)
+ crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
+ return ~crc;
+}
diff --git a/trunk/src/cxx.c b/trunk/src/cxx.c
new file mode 100644
index 0000000..b3530f0
--- /dev/null
+++ b/trunk/src/cxx.c
@@ -0,0 +1,377 @@
+/* Copyright (C) 2001, 2002, 2003 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include "prelink.h"
+
+struct find_cxx_sym
+{
+ DSO *dso;
+ int n;
+ struct prelink_entry *ent;
+ Elf_Data *symtab, *strtab;
+ int symsec, strsec;
+ GElf_Sym sym;
+};
+
+static int
+find_cxx_sym (struct prelink_info *info, GElf_Addr addr,
+ struct find_cxx_sym *fcs, int reloc_size)
+{
+ int n, ndeps = info->ent->ndepends + 1;
+ int ndx, maxndx;
+ DSO *dso = NULL;
+ Elf_Scn *scn;
+
+ if (fcs->dso == NULL
+ || addr < fcs->dso->base
+ || addr >= fcs->dso->end)
+ {
+ for (n = 1; n < ndeps; ++n)
+ {
+ dso = info->dsos[n];
+ if (addr >= dso->base
+ && addr < dso->end)
+ break;
+ }
+
+ if (n == ndeps
+ && addr >= info->dso->base
+ && addr < info->dso->end)
+ {
+ n = 0;
+ dso = info->dso;
+ }
+
+ assert (n < ndeps);
+ fcs->n = n;
+ fcs->ent = n ? info->ent->depends[n - 1] : info->ent;
+ fcs->dso = dso;
+ fcs->symsec = addr_to_sec (dso, dso->info[DT_SYMTAB]);
+ if (fcs->symsec == -1)
+ {
+ fcs->ent = NULL;
+ return -1;
+ }
+ scn = dso->scn[fcs->symsec];
+ fcs->symtab = elf_getdata (scn, NULL);
+ assert (elf_getdata (scn, fcs->symtab) == NULL);
+ fcs->strsec = addr_to_sec (dso, dso->info[DT_STRTAB]);
+ if (fcs->strsec == -1)
+ {
+ fcs->ent = NULL;
+ return -1;
+ }
+ scn = dso->scn[fcs->strsec];
+ fcs->strtab = elf_getdata (scn, NULL);
+ assert (elf_getdata (scn, fcs->strtab) == NULL);
+ }
+ else
+ dso = fcs->dso;
+
+ maxndx = fcs->symtab->d_size / dso->shdr[fcs->symsec].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ {
+ gelfx_getsym (dso->elf, fcs->symtab, ndx, &fcs->sym);
+ if (fcs->sym.st_value <= addr
+ && fcs->sym.st_value + fcs->sym.st_size >= addr + reloc_size)
+ break;
+ }
+
+ if (ndx == maxndx)
+ return -1;
+
+ return ndx;
+}
+
+/* The idea here is that C++ virtual tables are always emitted
+ in .gnu.linkonce.d.* sections as WEAK symbols and they
+ need to be the same.
+ We check if they are and if yes, remove conflicts against
+ virtual tables which will not be used. */
+
+int
+remove_redundant_cxx_conflicts (struct prelink_info *info)
+{
+ int i, j, k, n, o, state, removed = 0;
+ int ndx, maxndx, sec;
+ int reloc_type, reloc_size;
+ struct find_cxx_sym fcs1, fcs2;
+ char *mem1, *mem2;
+ const char *name = NULL, *secname = NULL;
+ GElf_Addr symtab_start;
+ GElf_Word symoff;
+ Elf_Data *binsymtab = NULL;
+ int binsymtabsec;
+ struct prelink_conflict *conflict;
+ static struct
+ {
+ unsigned char *prefix;
+ unsigned char prefix_len, st_info, check_pltref;
+ unsigned char *section;
+ }
+ specials[] =
+ {
+ /* G++ 3.0 ABI. */
+ /* Virtual table. */
+ { "_ZTV", 4, GELF_ST_INFO (STB_WEAK, STT_OBJECT), 1, ".data" },
+ /* Typeinfo. */
+ { "_ZTI", 4, GELF_ST_INFO (STB_WEAK, STT_OBJECT), 0, ".data" },
+ /* G++ 2.96-RH ABI. */
+ /* Virtual table. */
+ { "__vt_", 5, GELF_ST_INFO (STB_WEAK, STT_OBJECT), 0, ".data" },
+ { NULL, 0, 0, 0, NULL }
+ };
+
+ /* Don't bother doing this for non-C++ programs. */
+ for (i = 0; i < info->ent->ndepends; ++i)
+ if (strstr (info->ent->depends[i]->canon_filename, "libstdc++"))
+ break;
+ if (i == info->ent->ndepends)
+ return 0;
+
+ binsymtabsec = addr_to_sec (info->dso, info->dso->info[DT_SYMTAB]);
+ if (binsymtabsec != -1)
+ {
+ Elf_Scn *scn = info->dso->scn[binsymtabsec];
+
+ binsymtab = elf_getdata (scn, NULL);
+ assert (elf_getdata (scn, binsymtab) == NULL);
+ }
+
+ state = 0;
+ memset (&fcs1, 0, sizeof (fcs1));
+ memset (&fcs2, 0, sizeof (fcs2));
+ for (i = 0; i < info->conflict_rela_size; ++i)
+ {
+ reloc_type = GELF_R_TYPE (info->conflict_rela[i].r_info);
+ reloc_size = info->dso->arch->reloc_size (reloc_type);
+
+ if (GELF_R_SYM (info->conflict_rela[i].r_info) != 0)
+ continue;
+
+ if (state
+ && fcs1.sym.st_value <= info->conflict_rela[i].r_offset
+ && fcs1.sym.st_value + fcs1.sym.st_size
+ >= info->conflict_rela[i].r_offset + reloc_size)
+ {
+ if (state == 3)
+ goto remove_noref;
+ if (state == 2)
+ goto check_pltref;
+ continue;
+ }
+
+ n = find_cxx_sym (info, info->conflict_rela[i].r_offset,
+ &fcs1, reloc_size);
+
+ name = (const char *) fcs1.strtab->d_buf + fcs1.sym.st_name;
+ state = 0;
+ if (n == -1)
+ continue;
+ state = 1;
+ sec = addr_to_sec (fcs1.dso, fcs1.sym.st_value);
+ if (sec == -1)
+ continue;
+ secname = strptr (fcs1.dso, fcs1.dso->ehdr.e_shstrndx,
+ fcs1.dso->shdr[sec].sh_name);
+ if (secname == NULL)
+ continue;
+
+ for (k = 0; specials[k].prefix; ++k)
+ if (ELF32_ST_VISIBILITY (fcs1.sym.st_other) == STV_DEFAULT
+ && fcs1.sym.st_info == specials[k].st_info
+ && strncmp (name, specials[k].prefix, specials[k].prefix_len) == 0
+ && strcmp (secname, specials[k].section) == 0)
+ break;
+
+ if (specials[k].prefix == NULL)
+ continue;
+
+ /* Now check there are no other symbols pointing to it. */
+ maxndx = fcs1.symtab->d_size / fcs1.dso->shdr[fcs1.symsec].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ if (ndx != n)
+ {
+ GElf_Sym sym;
+
+ gelfx_getsym (fcs1.dso->elf, fcs1.symtab, ndx, &sym);
+ if ((sym.st_value + sym.st_size > fcs1.sym.st_value
+ && sym.st_value < fcs1.sym.st_value + fcs1.sym.st_size)
+ || sym.st_value == fcs1.sym.st_value)
+ break;
+ }
+
+ if (ndx < maxndx)
+ continue;
+
+ if (specials[k].check_pltref)
+ state = 2;
+
+ symtab_start = fcs1.dso->shdr[fcs1.symsec].sh_addr - fcs1.dso->base;
+ symoff = symtab_start + n * fcs1.dso->shdr[fcs1.symsec].sh_entsize;
+
+ for (conflict = info->conflicts[fcs1.n]; conflict;
+ conflict = conflict->next)
+ if (conflict->symoff == symoff
+ && conflict->reloc_class == RTYPE_CLASS_VALID)
+ break;
+
+ if (conflict == NULL)
+ goto check_pltref;
+
+ if (conflict->conflict.ent != fcs1.ent
+ || fcs1.dso->base + conflict->conflictval != fcs1.sym.st_value)
+ goto check_pltref;
+
+ if (verbose > 4)
+ error (0, 0, "Possible C++ conflict removal from unreferenced table at %s:%s+%d",
+ fcs1.dso->filename, name,
+ (int) (info->conflict_rela[i].r_offset - fcs1.sym.st_value));
+
+ /* Limit size slightly. */
+ if (fcs1.sym.st_size > 16384)
+ goto check_pltref;
+
+ o = find_cxx_sym (info, conflict->lookup.ent->base + conflict->lookupval,
+ &fcs2, fcs1.sym.st_size);
+
+ if (o == -1
+ || fcs1.sym.st_size != fcs2.sym.st_size
+ || fcs1.sym.st_info != fcs2.sym.st_info
+ || ELF32_ST_VISIBILITY (fcs2.sym.st_other) != STV_DEFAULT
+ || strcmp (name, (char *) fcs2.strtab->d_buf + fcs2.sym.st_name) != 0)
+ goto check_pltref;
+
+ mem1 = malloc (fcs1.sym.st_size * 2);
+ if (mem1 == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not compare %s arrays",
+ info->dso->filename, name);
+ return 1;
+ }
+
+ mem2 = mem1 + fcs1.sym.st_size;
+
+ if (get_relocated_mem (info, fcs1.dso, fcs1.sym.st_value, mem1,
+ fcs1.sym.st_size)
+ || get_relocated_mem (info, fcs2.dso, fcs2.sym.st_value, mem2,
+ fcs1.sym.st_size)
+ || memcmp (mem1, mem2, fcs1.sym.st_size) != 0)
+ {
+ free (mem1);
+ goto check_pltref;
+ }
+
+ free (mem1);
+
+ state = 3;
+
+remove_noref:
+ if (verbose > 3)
+ error (0, 0, "Removing C++ conflict from unreferenced table at %s:%s+%d",
+ fcs1.dso->filename, name,
+ (int) (info->conflict_rela[i].r_offset - fcs1.sym.st_value));
+
+ info->conflict_rela[i].r_info =
+ GELF_R_INFO (1, GELF_R_TYPE (info->conflict_rela[i].r_info));
+ ++removed;
+ continue;
+
+check_pltref:
+ /* If the binary calls directly (or takes its address) one of the
+ methods in a virtual table, but doesn't define it, there is no
+ need to leave conflicts in the virtual table which will only
+ slow down the code (as it has to hop through binary's .plt
+ back to the method). */
+ if (state != 2
+ || info->conflict_rela[i].r_addend < info->dso->base
+ || info->conflict_rela[i].r_addend >= info->dso->end
+ || binsymtab == NULL)
+ continue;
+
+ maxndx = binsymtab->d_size / info->dso->shdr[binsymtabsec].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ {
+ GElf_Sym sym;
+
+ gelfx_getsym (info->dso->elf, binsymtab, ndx, &sym);
+ if (sym.st_value == info->conflict_rela[i].r_addend)
+ {
+ if (sym.st_shndx == SHN_UNDEF && sym.st_value)
+ {
+ struct prelink_symbol *s;
+
+ if (verbose > 4)
+ error (0, 0, "Possible C++ conflict removal due to reference to binary's .plt at %s:%s+%d",
+ fcs1.dso->filename, name,
+ (int) (info->conflict_rela[i].r_offset
+ - fcs1.sym.st_value));
+
+ for (s = &info->symbols[ndx]; s; s = s->next)
+ if (s->reloc_class == RTYPE_CLASS_PLT)
+ {
+ for (conflict = info->conflicts[fcs1.n]; conflict;
+ conflict = conflict->next)
+ if (conflict->lookup.ent->base + conflict->lookupval
+ == info->conflict_rela[i].r_addend
+ && conflict->conflict.ent
+ && (conflict->conflict.ent->base
+ + conflict->conflictval
+ == s->u.ent->base + s->value)
+ && conflict->reloc_class == RTYPE_CLASS_VALID)
+ {
+ if (verbose > 3)
+ error (0, 0, "Removing C++ conflict due to reference to binary's .plt at %s:%s+%d",
+ fcs1.dso->filename, name,
+ (int) (info->conflict_rela[i].r_offset
+ - fcs1.sym.st_value));
+
+ info->conflict_rela[i].r_info =
+ GELF_R_INFO (1, GELF_R_TYPE (info->conflict_rela[i].r_info));
+ ++removed;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ if (removed)
+ {
+ for (i = 0, j = 0; i < info->conflict_rela_size; ++i)
+ if (GELF_R_SYM (info->conflict_rela[i].r_info) == 0)
+ {
+ if (i != j)
+ info->conflict_rela[j] = info->conflict_rela[i];
+ ++j;
+ }
+ info->conflict_rela_size = j;
+ }
+
+ return 0;
+}
diff --git a/trunk/src/data.c b/trunk/src/data.c
new file mode 100644
index 0000000..13380da
--- /dev/null
+++ b/trunk/src/data.c
@@ -0,0 +1,339 @@
+/* Copyright (C) 2001, 2002 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 "prelink.h"
+
+#define UREAD(le,nn) \
+uint##nn##_t \
+read_u##le##nn (DSO *dso, GElf_Addr addr) \
+{ \
+ Elf_Type type; \
+ unsigned char *data = get_data (dso, addr, NULL, &type); \
+ \
+ if (data == NULL) \
+ return 0; \
+ \
+ if (type == ELF_T_BYTE) \
+ return buf_read_u##le##nn (data); \
+ else \
+ return *(uint##nn##_t *)data; \
+}
+
+#define WRITE(le,nn) \
+int \
+write_##le##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val) \
+{ \
+ int sec; \
+ Elf_Type type; \
+ unsigned char *data = get_data (dso, addr, &sec, &type); \
+ \
+ if (data == NULL) \
+ return -1; \
+ \
+ if (type == ELF_T_BYTE) \
+ buf_write_##le##nn (data, val); \
+ else \
+ *(uint##nn##_t *)data = val; \
+ elf_flagscn (dso->scn[sec], ELF_C_SET, ELF_F_DIRTY); \
+ return 0; \
+}
+
+#define BUFREADUNE(nn) \
+uint##nn##_t \
+buf_read_une##nn (DSO *dso, unsigned char *buf) \
+{ \
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB) \
+ return buf_read_ule32 (buf); \
+ else \
+ return buf_read_ube32 (buf); \
+}
+
+#define READUNE(nn) \
+uint##nn##_t \
+read_une##nn (DSO *dso, GElf_Addr addr) \
+{ \
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB) \
+ return read_ule##nn (dso, addr); \
+ else \
+ return read_ube##nn (dso, addr); \
+}
+
+#define WRITENE(nn) \
+void \
+write_ne##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val) \
+{ \
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB) \
+ write_le##nn (dso, addr, val); \
+ else \
+ write_be##nn (dso, addr, val); \
+}
+
+#define BUFWRITENE(nn) \
+void \
+buf_write_ne##nn (DSO *dso, unsigned char *buf, \
+ uint##nn##_t val) \
+{ \
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB) \
+ buf_write_le##nn (buf, val); \
+ else \
+ buf_write_be##nn (buf, val); \
+}
+
+#define READWRITE(le,nn) UREAD(le,nn) WRITE(le,nn)
+#define READWRITESIZE(nn) \
+ READWRITE(le,nn) READWRITE(be,nn) \
+ BUFREADUNE(nn) READUNE(nn) \
+ WRITENE(nn) BUFWRITENE(nn)
+
+unsigned char *
+get_data (DSO *dso, GElf_Addr addr, int *secp, Elf_Type *typep)
+{
+ int sec = addr_to_sec (dso, addr);
+ Elf_Data *data = NULL;
+
+ if (sec == -1)
+ return NULL;
+
+ if (secp)
+ *secp = sec;
+
+ addr -= dso->shdr[sec].sh_addr;
+ while ((data = elf_getdata (dso->scn[sec], data)) != NULL)
+ if (data->d_off <= addr && data->d_off + data->d_size > addr)
+ {
+ if (typep) *typep = data->d_type;
+ return (unsigned char *) data->d_buf + (addr - data->d_off);
+ }
+ return NULL;
+}
+
+/* Initialize IT so that the first byte it provides is address ADDR
+ of DSO. */
+
+void
+init_data_iterator (struct data_iterator *it, DSO *dso, GElf_Addr addr)
+{
+ it->dso = dso;
+ it->data = NULL;
+ it->addr = addr;
+}
+
+/* Return a pointer to the next SIZE bytes pointed to by IT, and move
+ IT to the end of the returned block. Return null if the data could
+ not be read for some reason. */
+
+unsigned char *
+get_data_from_iterator (struct data_iterator *it, GElf_Addr size)
+{
+ unsigned char *ptr;
+
+ /* If we're at the end of a data block, move onto the next. */
+ if (it->data && it->data->d_off + it->data->d_size == it->sec_offset)
+ it->data = elf_getdata (it->dso->scn[it->sec], it->data);
+
+ if (it->data == NULL)
+ {
+ /* Find out which section contains the next byte. */
+ it->sec = addr_to_sec (it->dso, it->addr);
+ if (it->sec < 0)
+ return NULL;
+
+ /* Fast-forward to the block that contains ADDR, if any. */
+ it->sec_offset = it->addr - it->dso->shdr[it->sec].sh_addr;
+ do
+ it->data = elf_getdata (it->dso->scn[it->sec], it->data);
+ while (it->data && it->data->d_off + it->data->d_size <= it->sec_offset);
+ }
+
+ /* Make sure that all the data we want is included in this block. */
+ if (it->data == NULL
+ || it->data->d_off > it->sec_offset
+ || it->data->d_off + it->data->d_size < it->sec_offset + size)
+ return NULL;
+
+ ptr = (unsigned char *) it->data->d_buf + (it->sec_offset - it->data->d_off);
+ it->sec_offset += size;
+ it->addr += size;
+ return ptr;
+}
+
+/* Read the symbol pointed to by IT into SYM and move IT onto the
+ next symbol. Return true on success. */
+
+int
+get_sym_from_iterator (struct data_iterator *it, GElf_Sym *sym)
+{
+ GElf_Addr offset, size;
+ unsigned char *ptr;
+
+ size = gelf_fsize (it->dso->elf, ELF_T_SYM, 1, EV_CURRENT);
+ ptr = get_data_from_iterator (it, size);
+ if (ptr != NULL)
+ {
+ offset = ptr - (unsigned char *) it->data->d_buf;
+ if (offset % size == 0)
+ {
+ gelfx_getsym (it->dso->elf, it->data, offset / size, sym);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+inline uint8_t
+buf_read_u8 (unsigned char *data)
+{
+ return *data;
+}
+
+inline uint16_t
+buf_read_ule16 (unsigned char *data)
+{
+ return data[0] | (data[1] << 8);
+}
+
+inline uint16_t
+buf_read_ube16 (unsigned char *data)
+{
+ return data[1] | (data[0] << 8);
+}
+
+inline uint32_t
+buf_read_ule32 (unsigned char *data)
+{
+ return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
+}
+
+inline uint32_t
+buf_read_ube32 (unsigned char *data)
+{
+ return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
+}
+
+inline uint64_t
+buf_read_ule64 (unsigned char *data)
+{
+ return (data[0] | (data[1] << 8) | (data[2] << 16))
+ | (((uint64_t)data[3]) << 24)
+ | (((uint64_t)data[4]) << 32)
+ | (((uint64_t)data[5]) << 40)
+ | (((uint64_t)data[6]) << 48)
+ | (((uint64_t)data[7]) << 56);
+}
+
+inline uint64_t
+buf_read_ube64 (unsigned char *data)
+{
+ return (data[7] | (data[6] << 8) | (data[5] << 16))
+ | (((uint64_t)data[4]) << 24)
+ | (((uint64_t)data[3]) << 32)
+ | (((uint64_t)data[2]) << 40)
+ | (((uint64_t)data[1]) << 48)
+ | (((uint64_t)data[0]) << 56);
+}
+
+inline void
+buf_write_8 (unsigned char *data, uint8_t val)
+{
+ *data = val;
+}
+
+inline void
+buf_write_le16 (unsigned char *data, uint16_t val)
+{
+ data[0] = val;
+ data[1] = val >> 8;
+}
+
+inline void
+buf_write_be16 (unsigned char *data, uint16_t val)
+{
+ data[1] = val;
+ data[0] = val >> 8;
+}
+
+inline void
+buf_write_le32 (unsigned char *data, uint32_t val)
+{
+ data[0] = val;
+ data[1] = val >> 8;
+ data[2] = val >> 16;
+ data[3] = val >> 24;
+}
+
+inline void
+buf_write_be32 (unsigned char *data, uint32_t val)
+{
+ data[3] = val;
+ data[2] = val >> 8;
+ data[1] = val >> 16;
+ data[0] = val >> 24;
+}
+
+inline void
+buf_write_le64 (unsigned char *data, uint64_t val)
+{
+ data[0] = val;
+ data[1] = val >> 8;
+ data[2] = val >> 16;
+ data[3] = val >> 24;
+ data[4] = val >> 32;
+ data[5] = val >> 40;
+ data[6] = val >> 48;
+ data[7] = val >> 56;
+}
+
+inline void
+buf_write_be64 (unsigned char *data, uint64_t val)
+{
+ data[7] = val;
+ data[6] = val >> 8;
+ data[5] = val >> 16;
+ data[4] = val >> 24;
+ data[3] = val >> 32;
+ data[2] = val >> 40;
+ data[1] = val >> 48;
+ data[0] = val >> 56;
+}
+
+READWRITE(,8)
+READWRITESIZE(16)
+READWRITESIZE(32)
+READWRITESIZE(64)
+
+const char *
+strptr (DSO *dso, int sec, off_t offset)
+{
+ Elf_Scn *scn;
+ Elf_Data *data;
+
+ scn = dso->scn[sec];
+ if (offset >= 0 && offset < dso->shdr[sec].sh_size)
+ {
+ data = NULL;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ if (data->d_buf
+ && offset >= data->d_off
+ && offset < data->d_off + data->d_size)
+ return (const char *) data->d_buf + (offset - data->d_off);
+ }
+ }
+
+ return NULL;
+}
diff --git a/trunk/src/doit.c b/trunk/src/doit.c
new file mode 100644
index 0000000..2208ece
--- /dev/null
+++ b/trunk/src/doit.c
@@ -0,0 +1,270 @@
+/* Copyright (C) 2001, 2003, 2004, 2005 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 <alloca.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelinktab.h"
+
+struct collect_ents
+ {
+ struct prelink_entry **ents;
+ int nents;
+ };
+
+static int
+find_ents (void **p, void *info)
+{
+ struct collect_ents *l = (struct collect_ents *) info;
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+
+ if ((e->type == ET_DYN && e->done == 1)
+ || (e->type == ET_EXEC && e->done == 0 && ! libs_only))
+ l->ents[l->nents++] = e;
+
+ return 1;
+}
+
+static void
+clear_ent_marks (struct prelink_entry *ent)
+{
+ int i;
+
+ ent->u.tmp = 0;
+ for (i = 0; i < ent->ndepends; ++i)
+ clear_ent_marks (ent->depends[i]);
+}
+
+static struct prelink_entry *
+find_unlisted_dependency (struct prelink_entry *ent)
+{
+ int i;
+ struct prelink_entry *ret;
+
+ if (ent->u.tmp == 0)
+ return ent;
+ for (i = 0; i < ent->ndepends; ++i)
+ if ((ret = find_unlisted_dependency (ent->depends[i])) != NULL)
+ return ret;
+ return NULL;
+}
+
+static void
+prelink_ent (struct prelink_entry *ent)
+{
+ int i;
+ DSO *dso;
+ struct stat64 st;
+ struct prelink_link *hardlink;
+ char *move = NULL;
+ size_t movelen = 0;
+ struct prelink_entry *dep;
+
+ for (i = 0; i < ent->ndepends; ++i)
+ if (ent->depends[i]->done == 1)
+ prelink_ent (ent->depends[i]);
+
+ for (i = 0; i < ent->ndepends; ++i)
+ if (ent->depends[i]->done != 2)
+ {
+ ent->done = 0;
+ if (! undo)
+ ent->type = ET_UNPRELINKABLE;
+ if (verbose)
+ error (0, 0, "Could not prelink %s because its dependency %s could not be prelinked",
+ ent->filename, ent->depends[i]->filename);
+ return;
+ }
+ else
+ clear_ent_marks (ent->depends[i]);
+
+ ent->u.tmp = 1;
+ for (i = 0; i < ent->ndepends; ++i)
+ ent->depends[i]->u.tmp = 1;
+
+ if ((dep = find_unlisted_dependency (ent)) != NULL)
+ {
+ ent->done = 0;
+ if (! undo)
+ ent->type = ET_UNPRELINKABLE;
+ if (verbose)
+ error (0, 0, "Could not prelink %s because it doesn't use %s, but one of its dependencies has been prelinked against it",
+ ent->filename, dep->filename);
+ return;
+ }
+
+ if (verbose)
+ {
+ if (dry_run)
+ printf ("Would prelink %s\n", ent->canon_filename);
+ else
+ printf ("Prelinking %s\n", ent->canon_filename);
+ }
+
+ dso = open_dso (ent->canon_filename);
+ if (dso == NULL)
+ goto error_out;
+
+ if (fstat64 (dso->fd, &st) < 0)
+ {
+ error (0, errno, "%s changed during prelinking", ent->filename);
+ goto error_out;
+ }
+
+ if (st.st_dev != ent->dev || st.st_ino != ent->ino)
+ {
+ error (0, 0, "%s changed during prelinking", ent->filename);
+ goto error_out;
+ }
+
+ if (dry_run)
+ close_dso (dso);
+ else
+ {
+ if (prelink_prepare (dso))
+ goto make_unprelinkable;
+ if (ent->type == ET_DYN && relocate_dso (dso, ent->base))
+ goto make_unprelinkable;
+ if (prelink (dso, ent))
+ goto make_unprelinkable;
+ if (update_dso (dso, NULL))
+ {
+ dso = NULL;
+ goto error_out;
+ }
+ }
+ ent->done = 2;
+ ent->flags |= PCF_PRELINKED;
+
+ /* Redo hardlinks. */
+ for (hardlink = ent->hardlink; hardlink; hardlink = hardlink->next)
+ {
+ size_t len;
+
+ if (wrap_lstat64 (hardlink->canon_filename, &st) < 0)
+ {
+ error (0, 0, "Could not stat %s (former hardlink to %s)",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (st.st_dev != ent->dev || st.st_ino != ent->ino)
+ {
+ error (0, 0, "%s is no longer hardlink to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (verbose)
+ {
+ if (dry_run)
+ printf ("Would link %s to %s\n", hardlink->canon_filename,
+ ent->canon_filename);
+ else
+ printf ("Linking %s to %s\n", hardlink->canon_filename,
+ ent->canon_filename);
+ }
+
+ if (dry_run)
+ continue;
+
+ len = strlen (hardlink->canon_filename);
+ if (len + sizeof (".#prelink#") > movelen)
+ {
+ movelen = len + sizeof (".#prelink#");
+ move = realloc (move, movelen);
+ if (move == NULL)
+ {
+ error (0, ENOMEM, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ movelen = 0;
+ continue;
+ }
+ }
+
+ memcpy (mempcpy (move, hardlink->canon_filename, len), ".#prelink#",
+ sizeof (".#prelink#"));
+ if (wrap_rename (hardlink->canon_filename, move) < 0)
+ {
+ error (0, errno, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (wrap_link (ent->canon_filename, hardlink->canon_filename) < 0)
+ {
+ error (0, errno, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+
+ if (wrap_rename (move, hardlink->canon_filename) < 0)
+ {
+ error (0, errno, "Could not rename %s back to %s",
+ move, hardlink->canon_filename);
+ }
+ continue;
+ }
+
+ if (wrap_unlink (move) < 0)
+ {
+ error (0, errno, "Could not unlink %s", move);
+ continue;
+ }
+ }
+ free (move);
+
+ if (! dry_run && wrap_stat64 (ent->canon_filename, &st) >= 0)
+ {
+ ent->dev = st.st_dev;
+ ent->ino = st.st_ino;
+ ent->ctime = st.st_ctime;
+ ent->mtime = st.st_mtime;
+ }
+ return;
+
+make_unprelinkable:
+ if (! undo)
+ ent->type = ET_UNPRELINKABLE;
+error_out:
+ ent->done = 0;
+ if (dso)
+ close_dso (dso);
+ return;
+}
+
+void
+prelink_all (void)
+{
+ struct collect_ents l;
+ int i;
+
+ l.ents =
+ (struct prelink_entry **) alloca (prelink_entry_count
+ * sizeof (struct prelink_entry *));
+ l.nents = 0;
+ htab_traverse (prelink_filename_htab, find_ents, &l);
+
+ for (i = 0; i < l.nents; ++i)
+ if (l.ents[i]->done == 1
+ || (l.ents[i]->done == 0 && l.ents[i]->type == ET_EXEC))
+ prelink_ent (l.ents[i]);
+}
diff --git a/trunk/src/dso-readonly.c b/trunk/src/dso-readonly.c
new file mode 100644
index 0000000..8ad5865
--- /dev/null
+++ b/trunk/src/dso-readonly.c
@@ -0,0 +1,1823 @@
+/* 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 (dso->ehdr.e_machine == EM_MIPS)
+ {
+ if (dyn.d_tag == DT_MIPS_LOCAL_GOTNO)
+ dso->info_DT_MIPS_LOCAL_GOTNO = dyn.d_un.d_val;
+ else if (dyn.d_tag == DT_MIPS_GOTSYM)
+ dso->info_DT_MIPS_GOTSYM = dyn.d_un.d_val;
+ else if (dyn.d_tag == DT_MIPS_SYMTABNO)
+ dso->info_DT_MIPS_SYMTABNO = dyn.d_un.d_val;
+ }
+ }
+ 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;
+
+static 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, 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;
+}
+
+/* Return true if the value of symbol SYM, which belongs to DSO,
+ should be treated as an address within the DSO, and should
+ therefore track DSO's relocations. */
+
+int
+adjust_symbol_p (DSO *dso, GElf_Sym *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. */
+ return 1;
+
+ /* If a MIPS object does not define a symbol, but has a lazy binding
+ stub for it, st_value will point to that stub. Note that unlike
+ other targets, these stub addresses never participate in symbol
+ lookup; the stubs can only be called by the object that defines them.
+ st_values are only used in this way so that the associated GOT entry
+ can store a Quickstart value without losing the original stub
+ address. */
+ if (dso->ehdr.e_machine == EM_MIPS
+ && sym->st_shndx == SHN_UNDEF
+ && sym->st_value != 0)
+ return 1;
+
+ return (sym->st_shndx > SHN_UNDEF
+ && sym->st_shndx < dso->ehdr.e_shnum
+ && ELF32_ST_TYPE (sym->st_info) != STT_TLS
+ && RELOCATE_SCN (dso->shdr[sym->st_shndx].sh_flags));
+}
+
+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 (adjust_symbol_p (dso, &sym) && 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->arch->arch_adjust
+ && dso->arch->arch_adjust (dso, start, adjust))
+ return 1;
+
+ 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/trunk/src/dso.c b/trunk/src/dso.c
new file mode 100644
index 0000000..5080ffc
--- /dev/null
+++ b/trunk/src/dso.c
@@ -0,0 +1,1797 @@
+/* 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 (dso->ehdr.e_machine == EM_MIPS)
+ {
+ if (dyn.d_tag == DT_MIPS_LOCAL_GOTNO)
+ dso->info_DT_MIPS_LOCAL_GOTNO = dyn.d_un.d_val;
+ else if (dyn.d_tag == DT_MIPS_GOTSYM)
+ dso->info_DT_MIPS_GOTSYM = dyn.d_un.d_val;
+ else if (dyn.d_tag == DT_MIPS_SYMTABNO)
+ dso->info_DT_MIPS_SYMTABNO = dyn.d_un.d_val;
+ }
+ }
+ 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;
+
+static 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;
+ struct PLArch *plarch;
+ extern struct PLArch __start_pl_arch[], __stop_pl_arch[];
+
+ elf = elf_begin (fd, ELF_C_READ, NULL);
+ if (elf == NULL)
+ {
+ error (0, 0, "cannot open ELF file: %s", 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 (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));
+ }
+
+ 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];
+
+ 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;
+
+ 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, NULL);
+ if (soname && soname[0] != '\0')
+ dso->soname = (const char *) strdup (soname);
+ }
+
+ 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;
+ }
+ }
+
+ 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;
+}
+
+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_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;
+}
+
+/* Return true if the value of symbol SYM, which belongs to DSO,
+ should be treated as an address within the DSO, and should
+ therefore track DSO's relocations. */
+
+int
+adjust_symbol_p (DSO *dso, GElf_Sym *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. */
+ return 1;
+
+ /* If a MIPS object does not define a symbol, but has a lazy binding
+ stub for it, st_value will point to that stub. Note that unlike
+ other targets, these stub addresses never participate in symbol
+ lookup; the stubs can only be called by the object that defines them.
+ st_values are only used in this way so that the associated GOT entry
+ can store a Quickstart value without losing the original stub
+ address. */
+ if (dso->ehdr.e_machine == EM_MIPS
+ && sym->st_shndx == SHN_UNDEF
+ && sym->st_value != 0)
+ return 1;
+
+ return (sym->st_shndx > SHN_UNDEF
+ && sym->st_shndx < dso->ehdr.e_shnum
+ && ELF32_ST_TYPE (sym->st_info) != STT_TLS
+ && RELOCATE_SCN (dso->shdr[sym->st_shndx].sh_flags));
+}
+
+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 (adjust_symbol_p (dso, &sym) && 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;
+}
+
+int
+dso_is_rdwr (DSO *dso)
+{
+ return dso->elfro != NULL;
+}
+
+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;
+}
+
+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;
+}
+
+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->arch->arch_adjust
+ && dso->arch->arch_adjust (dso, start, adjust))
+ return 1;
+
+ 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);
+}
+
+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;
+}
+
+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;
+}
diff --git a/trunk/src/dwarf2.c b/trunk/src/dwarf2.c
new file mode 100644
index 0000000..4c27836
--- /dev/null
+++ b/trunk/src/dwarf2.c
@@ -0,0 +1,1148 @@
+/* Copyright (C) 2001, 2002, 2003, 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 <byteswap.h>
+#include <endian.h>
+#include <errno.h>
+#include <error.h>
+#include <limits.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "dwarf2.h"
+#include "hashtab.h"
+#include "prelink.h"
+
+#define read_uleb128(ptr) ({ \
+ unsigned int ret = 0; \
+ unsigned int c; \
+ int shift = 0; \
+ do \
+ { \
+ c = *ptr++; \
+ ret |= (c & 0x7f) << shift; \
+ shift += 7; \
+ } while (c & 0x80); \
+ \
+ if (shift >= 35) \
+ ret = UINT_MAX; \
+ ret; \
+})
+
+static uint16_t (*do_read_16) (unsigned char *ptr);
+static uint32_t (*do_read_32) (unsigned char *ptr);
+static uint64_t (*do_read_32_64) (unsigned char *ptr);
+static uint64_t (*do_read_64) (unsigned char *ptr);
+static uint64_t (*do_read_ptr) (unsigned char *ptr);
+static void (*write_32) (unsigned char *ptr, GElf_Addr val);
+static void (*write_64) (unsigned char *ptr, GElf_Addr val);
+static void (*write_ptr) (unsigned char *ptr, GElf_Addr val);
+
+static int ptr_size;
+
+#define read_1(ptr) *ptr++
+
+#define read_16(ptr) ({ \
+ uint16_t ret = do_read_16 (ptr); \
+ ptr += 2; \
+ ret; \
+})
+
+#define read_32(ptr) ({ \
+ uint32_t ret = do_read_32 (ptr); \
+ ptr += 4; \
+ ret; \
+})
+
+#define read_64(ptr) ({ \
+ uint64_t ret = do_read_64 (ptr); \
+ ptr += 8; \
+ ret; \
+})
+
+#define read_ptr(ptr) ({ \
+ uint64_t ret = do_read_ptr (ptr); \
+ ptr += ptr_size; \
+ ret; \
+})
+
+static uint64_t
+buf_read_ule32_64 (unsigned char *p)
+{
+ return buf_read_ule32 (p);
+}
+
+static uint64_t
+buf_read_ube32_64 (unsigned char *p)
+{
+ return buf_read_ube32 (p);
+}
+
+static void
+dwarf2_write_le32 (unsigned char *p, GElf_Addr val)
+{
+ uint32_t v = (uint32_t) val;
+
+ p[0] = v;
+ p[1] = v >> 8;
+ p[2] = v >> 16;
+ p[3] = v >> 24;
+}
+
+static void
+dwarf2_write_le64 (unsigned char *p, GElf_Addr val)
+{
+ p[0] = val;
+ p[1] = val >> 8;
+ p[2] = val >> 16;
+ p[3] = val >> 24;
+ p[4] = val >> 32;
+ p[5] = val >> 40;
+ p[6] = val >> 48;
+ p[7] = val >> 58;
+}
+
+static void
+dwarf2_write_be32 (unsigned char *p, GElf_Addr val)
+{
+ uint32_t v = (uint32_t) val;
+
+ p[3] = v;
+ p[2] = v >> 8;
+ p[1] = v >> 16;
+ p[0] = v >> 24;
+}
+
+static void
+dwarf2_write_be64 (unsigned char *p, GElf_Addr val)
+{
+ p[7] = val;
+ p[6] = val >> 8;
+ p[5] = val >> 16;
+ p[4] = val >> 24;
+ p[3] = val >> 32;
+ p[2] = val >> 40;
+ p[1] = val >> 48;
+ p[0] = val >> 58;
+}
+
+static struct
+ {
+ const char *name;
+ unsigned char *data;
+ size_t size;
+ int sec;
+ } debug_sections[] =
+ {
+#define DEBUG_INFO 0
+#define DEBUG_ABBREV 1
+#define DEBUG_LINE 2
+#define DEBUG_ARANGES 3
+#define DEBUG_PUBNAMES 4
+#define DEBUG_MACINFO 5
+#define DEBUG_LOC 6
+#define DEBUG_STR 7
+#define DEBUG_FRAME 8
+#define DEBUG_RANGES 9
+ { ".debug_info", NULL, 0, 0 },
+ { ".debug_abbrev", NULL, 0, 0 },
+ { ".debug_line", NULL, 0, 0 },
+ { ".debug_aranges", NULL, 0, 0 },
+ { ".debug_pubnames", NULL, 0, 0 },
+ { ".debug_macinfo", NULL, 0, 0 },
+ { ".debug_loc", NULL, 0, 0 },
+ { ".debug_str", NULL, 0, 0 },
+ { ".debug_frame", NULL, 0, 0 },
+ { ".debug_ranges", NULL, 0, 0 },
+ { NULL, NULL, 0 }
+ };
+
+struct abbrev_attr
+ {
+ unsigned int attr;
+ unsigned int form;
+ };
+
+struct abbrev_tag
+ {
+ unsigned int entry;
+ unsigned int tag;
+ int nattr;
+ struct abbrev_attr attr[0];
+ };
+
+struct cu_data
+ {
+ GElf_Addr cu_entry_pc;
+ GElf_Addr cu_low_pc;
+ };
+
+static hashval_t
+abbrev_hash (const void *p)
+{
+ struct abbrev_tag *t = (struct abbrev_tag *)p;
+
+ return t->entry;
+}
+
+static int
+abbrev_eq (const void *p, const void *q)
+{
+ struct abbrev_tag *t1 = (struct abbrev_tag *)p;
+ struct abbrev_tag *t2 = (struct abbrev_tag *)q;
+
+ return t1->entry == t2->entry;
+}
+
+static void
+abbrev_del (void *p)
+{
+ free (p);
+}
+
+static htab_t
+read_abbrev (DSO *dso, unsigned char *ptr)
+{
+ htab_t h = htab_try_create (50, abbrev_hash, abbrev_eq, abbrev_del);
+ unsigned int attr, form;
+ struct abbrev_tag *t;
+ int size;
+ void **slot;
+
+ if (h == NULL)
+ {
+no_memory:
+ error (0, ENOMEM, "%s: Could not read .debug_abbrev", dso->filename);
+ if (h)
+ htab_delete (h);
+ return NULL;
+ }
+
+ while ((attr = read_uleb128 (ptr)) != 0)
+ {
+ size = 10;
+ t = malloc (sizeof (*t) + size * sizeof (struct abbrev_attr));
+ if (t == NULL)
+ goto no_memory;
+ t->entry = attr;
+ t->nattr = 0;
+ slot = htab_find_slot (h, t, INSERT);
+ if (slot == NULL)
+ {
+ free (t);
+ goto no_memory;
+ }
+ if (*slot != NULL)
+ {
+ error (0, 0, "%s: Duplicate DWARF-2 abbreviation %d", dso->filename,
+ t->entry);
+ free (t);
+ htab_delete (h);
+ return NULL;
+ }
+ t->tag = read_uleb128 (ptr);
+ ++ptr; /* skip children flag. */
+ while ((attr = read_uleb128 (ptr)) != 0)
+ {
+ if (t->nattr == size)
+ {
+ size += 10;
+ t = realloc (t, sizeof (*t) + size * sizeof (struct abbrev_attr));
+ if (t == NULL)
+ goto no_memory;
+ }
+ form = read_uleb128 (ptr);
+ if (form == 2 || form > DW_FORM_indirect)
+ {
+ error (0, 0, "%s: Unknown DWARF-2 DW_FORM_%d", dso->filename, form);
+ htab_delete (h);
+ return NULL;
+ }
+
+ t->attr[t->nattr].attr = attr;
+ t->attr[t->nattr++].form = form;
+ }
+ if (read_uleb128 (ptr) != 0)
+ {
+ error (0, 0, "%s: DWARF-2 abbreviation does not end with 2 zeros",
+ dso->filename);
+ htab_delete (h);
+ return NULL;
+ }
+ *slot = t;
+ }
+
+ return h;
+}
+
+static int
+adjust_location_list (DSO *dso, unsigned char *ptr, size_t len,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *end = ptr + len;
+ unsigned char op;
+ GElf_Addr addr;
+
+ while (ptr < end)
+ {
+ op = *ptr++;
+ switch (op)
+ {
+ case DW_OP_addr:
+ addr = read_ptr (ptr);
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - ptr_size, addr + adjust);
+ break;
+ case DW_OP_deref:
+ case DW_OP_dup:
+ case DW_OP_drop:
+ case DW_OP_over:
+ case DW_OP_swap:
+ case DW_OP_rot:
+ case DW_OP_xderef:
+ case DW_OP_abs:
+ case DW_OP_and:
+ case DW_OP_div:
+ case DW_OP_minus:
+ case DW_OP_mod:
+ case DW_OP_mul:
+ case DW_OP_neg:
+ case DW_OP_not:
+ case DW_OP_or:
+ case DW_OP_plus:
+ case DW_OP_shl:
+ case DW_OP_shr:
+ case DW_OP_shra:
+ case DW_OP_xor:
+ case DW_OP_eq:
+ case DW_OP_ge:
+ case DW_OP_gt:
+ case DW_OP_le:
+ case DW_OP_lt:
+ case DW_OP_ne:
+ case DW_OP_lit0 ... DW_OP_lit31:
+ case DW_OP_reg0 ... DW_OP_reg31:
+ case DW_OP_nop:
+ case DW_OP_push_object_address:
+ case DW_OP_call_ref:
+ case DW_OP_GNU_push_tls_address:
+ break;
+ case DW_OP_const1u:
+ case DW_OP_pick:
+ case DW_OP_deref_size:
+ case DW_OP_xderef_size:
+ case DW_OP_const1s:
+ ++ptr;
+ break;
+ case DW_OP_const2u:
+ case DW_OP_const2s:
+ case DW_OP_skip:
+ case DW_OP_bra:
+ case DW_OP_call2:
+ ptr += 2;
+ break;
+ case DW_OP_const4u:
+ case DW_OP_const4s:
+ case DW_OP_call4:
+ ptr += 4;
+ break;
+ case DW_OP_const8u:
+ case DW_OP_const8s:
+ ptr += 8;
+ break;
+ case DW_OP_constu:
+ case DW_OP_plus_uconst:
+ case DW_OP_regx:
+ case DW_OP_piece:
+ case DW_OP_consts:
+ case DW_OP_breg0 ... DW_OP_breg31:
+ case DW_OP_fbreg:
+ read_uleb128 (ptr);
+ break;
+ case DW_OP_bregx:
+ read_uleb128 (ptr);
+ read_uleb128 (ptr);
+ break;
+ default:
+ error (0, 0, "%s: Unknown DWARF-2 DW_OP_%d", dso->filename, op);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int
+adjust_dwarf2_ranges (DSO *dso, GElf_Addr offset, GElf_Addr base,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *ptr, *endsec;
+ GElf_Addr low, high;
+ int adjusted_base;
+
+ ptr = debug_sections[DEBUG_RANGES].data;
+ if (ptr == NULL)
+ {
+ error (0, 0, "%s: DW_AT_ranges attribute, yet no .debug_ranges section",
+ dso->filename);
+ return 1;
+ }
+ if (offset >= debug_sections[DEBUG_RANGES].size)
+ {
+ error (0, 0,
+ "%s: DW_AT_ranges offset %Ld outside of .debug_ranges section",
+ dso->filename, (long long) offset);
+ return 1;
+ }
+ endsec = ptr + debug_sections[DEBUG_RANGES].size;
+ ptr += offset;
+ adjusted_base = (base && base >= start && addr_to_sec (dso, base) != -1);
+ while (ptr < endsec)
+ {
+ low = read_ptr (ptr);
+ high = read_ptr (ptr);
+ if (low == 0 && high == 0)
+ break;
+
+ if (low == ~ (GElf_Addr) 0 || (ptr_size == 4 && low == 0xffffffff))
+ {
+ base = high;
+ adjusted_base = (base && base >= start
+ && addr_to_sec (dso, base) != -1);
+ if (adjusted_base)
+ write_ptr (ptr - ptr_size, base + adjust);
+ }
+ else if (! adjusted_base)
+ {
+ if (base + low >= start && addr_to_sec (dso, base + low) != -1)
+ {
+ write_ptr (ptr - 2 * ptr_size, low + adjust);
+ if (high == low)
+ write_ptr (ptr - ptr_size, high + adjust);
+ }
+ if (low != high && base + high >= start
+ && addr_to_sec (dso, base + high - 1) != -1)
+ write_ptr (ptr - ptr_size, high + adjust);
+ }
+ }
+
+ elf_flagscn (dso->scn[debug_sections[DEBUG_RANGES].sec], ELF_C_SET,
+ ELF_F_DIRTY);
+ return 0;
+}
+
+static int
+adjust_dwarf2_loc (DSO *dso, GElf_Addr offset, GElf_Addr base,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *ptr, *endsec;
+ GElf_Addr low, high;
+ int adjusted_base;
+ size_t len;
+
+ ptr = debug_sections[DEBUG_LOC].data;
+ if (ptr == NULL)
+ {
+ error (0, 0, "%s: loclistptr attribute, yet no .debug_loc section",
+ dso->filename);
+ return 1;
+ }
+ if (offset >= debug_sections[DEBUG_LOC].size)
+ {
+ error (0, 0,
+ "%s: loclistptr offset %Ld outside of .debug_loc section",
+ dso->filename, (long long) offset);
+ return 1;
+ }
+ endsec = ptr + debug_sections[DEBUG_LOC].size;
+ ptr += offset;
+ adjusted_base = (base && base >= start && addr_to_sec (dso, base) != -1);
+ while (ptr < endsec)
+ {
+ low = read_ptr (ptr);
+ high = read_ptr (ptr);
+ if (low == 0 && high == 0)
+ break;
+
+ if (low == ~ (GElf_Addr) 0 || (ptr_size == 4 && low == 0xffffffff))
+ {
+ base = high;
+ adjusted_base = (base && base >= start
+ && addr_to_sec (dso, base) != -1);
+ if (adjusted_base)
+ write_ptr (ptr - ptr_size, base + adjust);
+ continue;
+ }
+ len = read_16 (ptr);
+ assert (ptr + len <= endsec);
+
+ if (adjust_location_list (dso, ptr, len, start, adjust))
+ return 1;
+
+ ptr += len;
+ }
+
+ elf_flagscn (dso->scn[debug_sections[DEBUG_LOC].sec], ELF_C_SET,
+ ELF_F_DIRTY);
+ return 0;
+}
+
+static unsigned char *
+adjust_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t,
+ struct cu_data *cu,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ int i;
+ GElf_Addr addr;
+
+ for (i = 0; i < t->nattr; ++i)
+ {
+ uint32_t form = t->attr[i].form;
+ uint32_t len = 0;
+
+ while (1)
+ {
+ switch (t->attr[i].attr)
+ {
+ case DW_AT_location:
+ case DW_AT_string_length:
+ case DW_AT_return_addr:
+ case DW_AT_data_member_location:
+ case DW_AT_frame_base:
+ case DW_AT_static_link:
+ case DW_AT_use_location:
+ case DW_AT_vtable_elem_location:
+ case DW_AT_ranges:
+ if (form == DW_FORM_data4)
+ addr = read_32 (ptr), ptr -= 4;
+ else if (form == DW_FORM_data8)
+ addr = read_64 (ptr), ptr -= 8;
+ else
+ break;
+ {
+ GElf_Addr base;
+
+ if (cu->cu_entry_pc != ~ (GElf_Addr) 0)
+ base = cu->cu_entry_pc;
+ else if (cu->cu_low_pc != ~ (GElf_Addr) 0)
+ base = cu->cu_low_pc;
+ else
+ base = 0;
+ if (t->attr[i].attr == DW_AT_ranges)
+ {
+ if (adjust_dwarf2_ranges (dso, addr, base, start, adjust))
+ return NULL;
+ }
+ else
+ {
+ if (adjust_dwarf2_loc (dso, addr, base, start, adjust))
+ return NULL;
+ }
+ }
+ break;
+ }
+ switch (form)
+ {
+ case DW_FORM_addr:
+ addr = read_ptr (ptr);
+ if (t->tag == DW_TAG_compile_unit
+ || t->tag == DW_TAG_partial_unit)
+ {
+ if (t->attr[i].attr == DW_AT_entry_pc)
+ cu->cu_entry_pc = addr;
+ else if (t->attr[i].attr == DW_AT_low_pc)
+ cu->cu_low_pc = addr;
+ if (addr == 0)
+ break;
+ }
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - ptr_size, addr + adjust);
+ break;
+ case DW_FORM_ref1:
+ case DW_FORM_flag:
+ case DW_FORM_data1:
+ ++ptr;
+ break;
+ case DW_FORM_ref2:
+ case DW_FORM_data2:
+ ptr += 2;
+ break;
+ case DW_FORM_ref4:
+ case DW_FORM_data4:
+ ptr += 4;
+ break;
+ case DW_FORM_ref8:
+ case DW_FORM_data8:
+ ptr += 8;
+ break;
+ case DW_FORM_sdata:
+ case DW_FORM_ref_udata:
+ case DW_FORM_udata:
+ read_uleb128 (ptr);
+ break;
+ case DW_FORM_ref_addr:
+ case DW_FORM_strp:
+ ptr += 4;
+ break;
+ case DW_FORM_string:
+ ptr = strchr (ptr, '\0') + 1;
+ break;
+ case DW_FORM_indirect:
+ form = read_uleb128 (ptr);
+ continue;
+ case DW_FORM_block1:
+ len = *ptr++;
+ break;
+ case DW_FORM_block2:
+ len = read_16 (ptr);
+ form = DW_FORM_block1;
+ break;
+ case DW_FORM_block4:
+ len = read_32 (ptr);
+ form = DW_FORM_block1;
+ break;
+ case DW_FORM_block:
+ len = read_uleb128 (ptr);
+ form = DW_FORM_block1;
+ assert (len < UINT_MAX);
+ break;
+ default:
+ error (0, 0, "%s: Unknown DWARF-2 DW_FORM_%d", dso->filename,
+ form);
+ return NULL;
+ }
+
+ if (form == DW_FORM_block1)
+ {
+ switch (t->attr[i].attr)
+ {
+ case DW_AT_frame_base:
+ case DW_AT_location:
+ case DW_AT_data_member_location:
+ case DW_AT_vtable_elem_location:
+ if (adjust_location_list (dso, ptr, len, start, adjust))
+ return NULL;
+ break;
+ default:
+ if (t->attr[i].attr <= DW_AT_call_line
+ || (t->attr[i].attr >= DW_AT_MIPS_fde
+ && t->attr[i].attr <= DW_AT_MIPS_has_inlines)
+ || (t->attr[i].attr >= DW_AT_sf_names
+ && t->attr[i].attr <= DW_AT_body_end))
+ break;
+ error (0, 0, "%s: Unknown DWARF-2 DW_AT_%d with block DW_FORM",
+ dso->filename, t->attr[i].attr);
+ return NULL;
+ }
+ ptr += len;
+ }
+
+ break;
+ }
+ }
+
+ return ptr;
+}
+
+static int
+adjust_dwarf2_line (DSO *dso, GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *ptr = debug_sections[DEBUG_LINE].data;
+ unsigned char *endsec = ptr + debug_sections[DEBUG_LINE].size;
+ unsigned char *endcu, *endprol;
+ unsigned char opcode_base, *opcode_lengths, op;
+ uint32_t value;
+ GElf_Addr addr;
+ int i;
+
+ while (ptr < endsec)
+ {
+ endcu = ptr + 4;
+ endcu += read_32 (ptr);
+ if (endcu == ptr + 0xffffffff)
+ {
+ error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
+ return 1;
+ }
+
+ if (endcu > endsec)
+ {
+ error (0, 0, "%s: .debug_line CU does not fit into section",
+ dso->filename);
+ return 1;
+ }
+
+ value = read_16 (ptr);
+ if (value != 2)
+ {
+ error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
+ value);
+ return 1;
+ }
+
+ endprol = ptr + 4;
+ endprol += read_32 (ptr);
+ if (endprol > endcu)
+ {
+ error (0, 0, "%s: .debug_line CU prologue does not fit into CU",
+ dso->filename);
+ return 1;
+ }
+
+ opcode_base = ptr[4];
+ opcode_lengths = ptr + 4;
+
+ ptr = endprol;
+ while (ptr < endcu)
+ {
+ op = *ptr++;
+ if (op >= opcode_base)
+ continue;
+ if (op == 0)
+ {
+ unsigned int len = read_uleb128 (ptr);
+
+ assert (len < UINT_MAX);
+ op = *ptr++;
+ switch (op)
+ {
+ case DW_LNE_set_address:
+ addr = read_ptr (ptr);
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - ptr_size, addr + adjust);
+ break;
+ case DW_LNE_end_sequence:
+ case DW_LNE_define_file:
+ default:
+ ptr += len - 1;
+ break;
+ }
+ }
+ else if (op == DW_LNS_fixed_advance_pc)
+ ptr += 2;
+ else
+ for (i = 0; i < opcode_lengths[op]; ++i)
+ read_uleb128 (ptr);
+ }
+ }
+
+ elf_flagscn (dso->scn[debug_sections[DEBUG_LINE].sec], ELF_C_SET,
+ ELF_F_DIRTY);
+ return 0;
+}
+
+static int
+adjust_dwarf2_aranges (DSO *dso, GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *ptr = debug_sections[DEBUG_ARANGES].data;
+ unsigned char *endsec = ptr + debug_sections[DEBUG_ARANGES].size;
+ unsigned char *endcu;
+ GElf_Addr addr, len;
+ uint32_t value;
+
+ while (ptr < endsec)
+ {
+ endcu = ptr + 4;
+ endcu += read_32 (ptr);
+ if (endcu == ptr + 0xffffffff)
+ {
+ error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
+ return 1;
+ }
+
+ if (endcu > endsec)
+ {
+ error (0, 0, "%s: .debug_line CU does not fit into section",
+ dso->filename);
+ return 1;
+ }
+
+ value = read_16 (ptr);
+ if (value != 2)
+ {
+ error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
+ value);
+ return 1;
+ }
+
+ ptr += 4;
+ if (ptr[0] != ptr_size || ptr[1])
+ {
+ error (0, 0, "%s: Unsupported .debug_aranges address size %d or segment size %d",
+ dso->filename, ptr[0], ptr[1]);
+ return 1;
+ }
+
+ ptr += 6;
+ while (ptr < endcu)
+ {
+ addr = read_ptr (ptr);
+ len = read_ptr (ptr);
+ if (addr == 0 && len == 0)
+ break;
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - 2 * ptr_size, addr + adjust);
+ }
+ assert (ptr == endcu);
+ }
+
+ elf_flagscn (dso->scn[debug_sections[DEBUG_LINE].sec], ELF_C_SET,
+ ELF_F_DIRTY);
+ return 0;
+}
+
+static int
+adjust_dwarf2_frame (DSO *dso, GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned char *ptr = debug_sections[DEBUG_FRAME].data;
+ unsigned char *endsec = ptr + debug_sections[DEBUG_FRAME].size;
+ unsigned char *endie;
+ GElf_Addr addr, len;
+ uint32_t value;
+
+ while (ptr < endsec)
+ {
+ endie = ptr + 4;
+ endie += read_32 (ptr);
+ if (endie == ptr + 0xffffffff)
+ {
+ error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
+ return 1;
+ }
+
+ if (endie > endsec)
+ {
+ error (0, 0, "%s: .debug_frame CIE/FDE does not fit into section",
+ dso->filename);
+ return 1;
+ }
+
+ value = read_32 (ptr);
+ if (value == 0xffffffff)
+ {
+ /* CIE. */
+ ptr++; /* Skip version. */
+ if (*ptr != '\0')
+ {
+ error (0, 0, "%s: .debug_frame unhandled augmentation \"%s\"",
+ dso->filename, ptr);
+ return 1;
+ }
+ ptr++; /* Skip augmentation. */
+ read_uleb128 (ptr); /* Skip code_alignment factor. */
+ read_uleb128 (ptr); /* Skip data_alignment factor. */
+ read_uleb128 (ptr); /* Skip return_address_register. */
+ }
+ else
+ {
+ addr = read_ptr (ptr);
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - ptr_size, addr + adjust);
+ read_ptr (ptr); /* Skip address range. */
+ }
+
+ while (ptr < endie)
+ {
+ unsigned char insn = *ptr++;
+
+ if ((insn & 0xc0) == DW_CFA_advance_loc
+ || (insn & 0xc0) == DW_CFA_restore)
+ continue;
+ else if ((insn & 0xc0) == DW_CFA_offset)
+ {
+ read_uleb128 (ptr);
+ continue;
+ }
+ switch (insn)
+ {
+ case DW_CFA_nop:
+ case DW_CFA_remember_state:
+ case DW_CFA_restore_state:
+ case DW_CFA_GNU_window_save:
+ break;
+ case DW_CFA_offset_extended:
+ case DW_CFA_register:
+ case DW_CFA_def_cfa:
+ case DW_CFA_offset_extended_sf:
+ case DW_CFA_def_cfa_sf:
+ case DW_CFA_GNU_negative_offset_extended:
+ read_uleb128 (ptr);
+ /* FALLTHROUGH */
+ case DW_CFA_restore_extended:
+ case DW_CFA_undefined:
+ case DW_CFA_same_value:
+ case DW_CFA_def_cfa_register:
+ case DW_CFA_def_cfa_offset:
+ case DW_CFA_def_cfa_offset_sf:
+ case DW_CFA_GNU_args_size:
+ read_uleb128 (ptr);
+ break;
+ case DW_CFA_set_loc:
+ addr = read_ptr (ptr);
+ if (addr >= start && addr_to_sec (dso, addr) != -1)
+ write_ptr (ptr - ptr_size, addr + adjust);
+ break;
+ case DW_CFA_advance_loc1:
+ ptr++;
+ break;
+ case DW_CFA_advance_loc2:
+ ptr += 2;
+ break;
+ case DW_CFA_advance_loc4:
+ ptr += 4;
+ break;
+ case DW_CFA_expression:
+ read_uleb128 (ptr);
+ /* FALLTHROUGH */
+ case DW_CFA_def_cfa_expression:
+ len = read_uleb128 (ptr);
+ if (adjust_location_list (dso, ptr, len, start, adjust))
+ return 1;
+ ptr += len;
+ break;
+ default:
+ error (0, 0, "%s: Unhandled DW_CFA_%02x operation",
+ dso->filename, insn);
+ return 1;
+ }
+ }
+ }
+
+ elf_flagscn (dso->scn[debug_sections[DEBUG_FRAME].sec], ELF_C_SET,
+ ELF_F_DIRTY);
+ return 0;
+}
+
+int
+adjust_dwarf2 (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust)
+{
+ Elf_Data *data;
+ Elf_Scn *scn;
+ int i, j;
+ struct cu_data cu;
+
+ for (i = 0; debug_sections[i].name; ++i)
+ {
+ debug_sections[i].data = NULL;
+ debug_sections[i].size = 0;
+ debug_sections[i].sec = 0;
+ }
+ ptr_size = 0;
+
+ memset (&cu, 0, sizeof(cu));
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (! (dso->shdr[i].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR))
+ && dso->shdr[i].sh_size)
+ {
+ const char *name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name);
+
+ if (strncmp (name, ".debug_", sizeof (".debug_") - 1) == 0)
+ {
+ for (j = 0; debug_sections[j].name; ++j)
+ if (strcmp (name, debug_sections[j].name) == 0)
+ {
+ if (debug_sections[j].data)
+ {
+ error (0, 0, "%s: Found two copies of %s section",
+ dso->filename, name);
+ return 1;
+ }
+
+ scn = dso->scn[i];
+ data = elf_getdata (scn, NULL);
+ assert (data != NULL && data->d_buf != NULL);
+ assert (elf_getdata (scn, data) == NULL);
+ assert (data->d_off == 0);
+ assert (data->d_size == dso->shdr[i].sh_size);
+ debug_sections[j].data = data->d_buf;
+ debug_sections[j].size = data->d_size;
+ debug_sections[j].sec = i;
+ break;
+ }
+
+ if (debug_sections[j].name == NULL)
+ {
+ error (0, 0, "%s: Unknown debugging section %s",
+ dso->filename, name);
+ }
+ }
+ }
+
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+ {
+ do_read_16 = buf_read_ule16;
+ do_read_32 = buf_read_ule32;
+ do_read_32_64 = buf_read_ule32_64;
+ do_read_64 = buf_read_ule64;
+ write_32 = dwarf2_write_le32;
+ write_64 = dwarf2_write_le64;
+ }
+ else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
+ {
+ do_read_16 = buf_read_ube16;
+ do_read_32 = buf_read_ube32;
+ do_read_32_64 = buf_read_ube32_64;
+ do_read_64 = buf_read_ube64;
+ write_32 = dwarf2_write_be32;
+ write_64 = dwarf2_write_be64;
+ }
+ else
+ {
+ error (0, 0, "%s: Wrong ELF data enconding", dso->filename);
+ return 1;
+ }
+
+ if (debug_sections[DEBUG_INFO].data != NULL)
+ {
+ unsigned char *ptr, *endcu, *endsec;
+ uint32_t value;
+ htab_t abbrev;
+ struct abbrev_tag tag, *t;
+
+ ptr = debug_sections[DEBUG_INFO].data;
+ endsec = ptr + debug_sections[DEBUG_INFO].size;
+ while (ptr < endsec)
+ {
+ if (ptr + 11 > endsec)
+ {
+ error (0, 0, "%s: .debug_info CU header too small",
+ dso->filename);
+ return 1;
+ }
+
+ endcu = ptr + 4;
+ endcu += read_32 (ptr);
+ if (endcu == ptr + 0xffffffff)
+ {
+ error (0, 0, "%s: 64-bit DWARF not supported", dso->filename);
+ return 1;
+ }
+
+ if (endcu > endsec)
+ {
+ error (0, 0, "%s: .debug_info too small", dso->filename);
+ return 1;
+ }
+
+ value = read_16 (ptr);
+ if (value != 2)
+ {
+ error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
+ value);
+ return 1;
+ }
+
+ value = read_32 (ptr);
+ if (value >= debug_sections[DEBUG_ABBREV].size)
+ {
+ if (debug_sections[DEBUG_ABBREV].data == NULL)
+ error (0, 0, "%s: .debug_abbrev not present", dso->filename);
+ else
+ error (0, 0, "%s: DWARF-2 CU abbrev offset too large",
+ dso->filename);
+ return 1;
+ }
+
+ if (ptr_size == 0)
+ {
+ ptr_size = read_1 (ptr);
+ if (ptr_size == 4)
+ {
+ do_read_ptr = do_read_32_64;
+ write_ptr = write_32;
+ }
+ else if (ptr_size == 8)
+ {
+ do_read_ptr = do_read_64;
+ write_ptr = write_64;
+ }
+ else
+ {
+ error (0, 0, "%s: Invalid DWARF-2 pointer size %d",
+ dso->filename, ptr_size);
+ return 1;
+ }
+ }
+ else if (read_1 (ptr) != ptr_size)
+ {
+ error (0, 0, "%s: DWARF-2 pointer size differs between CUs",
+ dso->filename);
+ return 1;
+ }
+
+ abbrev = read_abbrev (dso,
+ debug_sections[DEBUG_ABBREV].data + value);
+ if (abbrev == NULL)
+ return 1;
+
+ cu.cu_entry_pc = ~ (GElf_Addr) 0;
+ cu.cu_low_pc = ~ (GElf_Addr) 0;
+
+ while (ptr < endcu)
+ {
+ tag.entry = read_uleb128 (ptr);
+ if (tag.entry == 0)
+ continue;
+ t = htab_find_with_hash (abbrev, &tag, tag.entry);
+ if (t == NULL)
+ {
+ error (0, 0, "%s: Could not find DWARF-2 abbreviation %d",
+ dso->filename, tag.entry);
+ htab_delete (abbrev);
+ return 1;
+ }
+
+ ptr = adjust_attributes (dso, ptr, t, &cu, start, adjust);
+ if (ptr == NULL)
+ {
+ htab_delete (abbrev);
+ return 1;
+ }
+ }
+
+ htab_delete (abbrev);
+ }
+ }
+
+ if (ptr_size == 0)
+ /* Should not happen. */
+ ptr_size = dso->ehdr.e_ident[EI_CLASS] == ELFCLASS64 ? 8 : 4;
+
+ if (debug_sections[DEBUG_LINE].data != NULL
+ && adjust_dwarf2_line (dso, start, adjust))
+ return 1;
+
+ if (debug_sections[DEBUG_ARANGES].data != NULL
+ && adjust_dwarf2_aranges (dso, start, adjust))
+ return 1;
+
+ if (debug_sections[DEBUG_FRAME].data != NULL
+ && adjust_dwarf2_frame (dso, start, adjust))
+ return 1;
+
+ /* .debug_abbrev requires no adjustement. */
+ /* .debug_pubnames requires no adjustement. */
+ /* .debug_macinfo requires no adjustement. */
+ /* .debug_str requires no adjustement. */
+ /* .debug_ranges adjusted for each DW_AT_ranges pointing into it. */
+ /* .debug_loc adjusted for each loclistptr pointing into it. */
+
+ elf_flagscn (dso->scn[n], ELF_C_SET, ELF_F_DIRTY);
+ return 0;
+}
diff --git a/trunk/src/dwarf2.h b/trunk/src/dwarf2.h
new file mode 100644
index 0000000..1f938f5
--- /dev/null
+++ b/trunk/src/dwarf2.h
@@ -0,0 +1,479 @@
+/* Copyright (C) 2001, 2002 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. */
+
+#define DW_TAG_padding 0x00
+#define DW_TAG_array_type 0x01
+#define DW_TAG_class_type 0x02
+#define DW_TAG_entry_point 0x03
+#define DW_TAG_enumeration_type 0x04
+#define DW_TAG_formal_parameter 0x05
+#define DW_TAG_imported_declaration 0x08
+#define DW_TAG_label 0x0a
+#define DW_TAG_lexical_block 0x0b
+#define DW_TAG_member 0x0d
+#define DW_TAG_pointer_type 0x0f
+#define DW_TAG_reference_type 0x10
+#define DW_TAG_compile_unit 0x11
+#define DW_TAG_string_type 0x12
+#define DW_TAG_structure_type 0x13
+#define DW_TAG_subroutine_type 0x15
+#define DW_TAG_typedef 0x16
+#define DW_TAG_union_type 0x17
+#define DW_TAG_unspecified_parameters 0x18
+#define DW_TAG_variant 0x19
+#define DW_TAG_common_block 0x1a
+#define DW_TAG_common_inclusion 0x1b
+#define DW_TAG_inheritance 0x1c
+#define DW_TAG_inlined_subroutine 0x1d
+#define DW_TAG_module 0x1e
+#define DW_TAG_ptr_to_member_type 0x1f
+#define DW_TAG_set_type 0x20
+#define DW_TAG_subrange_type 0x21
+#define DW_TAG_with_stmt 0x22
+#define DW_TAG_access_declaration 0x23
+#define DW_TAG_base_type 0x24
+#define DW_TAG_catch_block 0x25
+#define DW_TAG_const_type 0x26
+#define DW_TAG_constant 0x27
+#define DW_TAG_enumerator 0x28
+#define DW_TAG_file_type 0x29
+#define DW_TAG_friend 0x2a
+#define DW_TAG_namelist 0x2b
+#define DW_TAG_namelist_item 0x2c
+#define DW_TAG_packed_type 0x2d
+#define DW_TAG_subprogram 0x2e
+#define DW_TAG_template_type_param 0x2f
+#define DW_TAG_template_value_param 0x30
+#define DW_TAG_thrown_type 0x31
+#define DW_TAG_try_block 0x32
+#define DW_TAG_variant_part 0x33
+#define DW_TAG_variable 0x34
+#define DW_TAG_volatile_type 0x35
+#define DW_TAG_dwarf_procedure 0x36
+#define DW_TAG_restrict_type 0x37
+#define DW_TAG_interface_type 0x38
+#define DW_TAG_namespace 0x39
+#define DW_TAG_imported_module 0x3a
+#define DW_TAG_unspecified_type 0x3b
+#define DW_TAG_partial_unit 0x3c
+#define DW_TAG_imported_unit 0x3d
+#define DW_TAG_MIPS_loop 0x4081
+#define DW_TAG_format_label 0x4101
+#define DW_TAG_function_template 0x4102
+#define DW_TAG_class_template 0x4103
+#define DW_TAG_GNU_BINCL 0x4104
+#define DW_TAG_GNU_EINCL 0x4105
+#define DW_TAG_lo_user 0x4080
+#define DW_TAG_hi_user 0xffff
+
+#define DW_children_no 0x0
+#define DW_children_yes 0x1
+
+#define DW_FORM_addr 0x01
+#define DW_FORM_block2 0x03
+#define DW_FORM_block4 0x04
+#define DW_FORM_data2 0x05
+#define DW_FORM_data4 0x06
+#define DW_FORM_data8 0x07
+#define DW_FORM_string 0x08
+#define DW_FORM_block 0x09
+#define DW_FORM_block1 0x0a
+#define DW_FORM_data1 0x0b
+#define DW_FORM_flag 0x0c
+#define DW_FORM_sdata 0x0d
+#define DW_FORM_strp 0x0e
+#define DW_FORM_udata 0x0f
+#define DW_FORM_ref_addr 0x10
+#define DW_FORM_ref1 0x11
+#define DW_FORM_ref2 0x12
+#define DW_FORM_ref4 0x13
+#define DW_FORM_ref8 0x14
+#define DW_FORM_ref_udata 0x15
+#define DW_FORM_indirect 0x16
+
+#define DW_AT_sibling 0x01
+#define DW_AT_location 0x02
+#define DW_AT_name 0x03
+#define DW_AT_ordering 0x09
+#define DW_AT_subscr_data 0x0a
+#define DW_AT_byte_size 0x0b
+#define DW_AT_bit_offset 0x0c
+#define DW_AT_bit_size 0x0d
+#define DW_AT_element_list 0x0f
+#define DW_AT_stmt_list 0x10
+#define DW_AT_low_pc 0x11
+#define DW_AT_high_pc 0x12
+#define DW_AT_language 0x13
+#define DW_AT_member 0x14
+#define DW_AT_discr 0x15
+#define DW_AT_discr_value 0x16
+#define DW_AT_visibility 0x17
+#define DW_AT_import 0x18
+#define DW_AT_string_length 0x19
+#define DW_AT_common_reference 0x1a
+#define DW_AT_comp_dir 0x1b
+#define DW_AT_const_value 0x1c
+#define DW_AT_containing_type 0x1d
+#define DW_AT_default_value 0x1e
+#define DW_AT_inline 0x20
+#define DW_AT_is_optional 0x21
+#define DW_AT_lower_bound 0x22
+#define DW_AT_producer 0x25
+#define DW_AT_prototyped 0x27
+#define DW_AT_return_addr 0x2a
+#define DW_AT_start_scope 0x2c
+#define DW_AT_stride_size 0x2e
+#define DW_AT_upper_bound 0x2f
+#define DW_AT_abstract_origin 0x31
+#define DW_AT_accessibility 0x32
+#define DW_AT_address_class 0x33
+#define DW_AT_artificial 0x34
+#define DW_AT_base_types 0x35
+#define DW_AT_calling_convention 0x36
+#define DW_AT_count 0x37
+#define DW_AT_data_member_location 0x38
+#define DW_AT_decl_column 0x39
+#define DW_AT_decl_file 0x3a
+#define DW_AT_decl_line 0x3b
+#define DW_AT_declaration 0x3c
+#define DW_AT_discr_list 0x3d
+#define DW_AT_encoding 0x3e
+#define DW_AT_external 0x3f
+#define DW_AT_frame_base 0x40
+#define DW_AT_friend 0x41
+#define DW_AT_identifier_case 0x42
+#define DW_AT_macro_info 0x43
+#define DW_AT_namelist_items 0x44
+#define DW_AT_priority 0x45
+#define DW_AT_segment 0x46
+#define DW_AT_specification 0x47
+#define DW_AT_static_link 0x48
+#define DW_AT_type 0x49
+#define DW_AT_use_location 0x4a
+#define DW_AT_variable_parameter 0x4b
+#define DW_AT_virtuality 0x4c
+#define DW_AT_vtable_elem_location 0x4d
+#define DW_AT_allocated 0x4e
+#define DW_AT_associated 0x4f
+#define DW_AT_data_location 0x50
+#define DW_AT_stride 0x51
+#define DW_AT_entry_pc 0x52
+#define DW_AT_use_UTF8 0x53
+#define DW_AT_extension 0x54
+#define DW_AT_ranges 0x55
+#define DW_AT_trampoline 0x56
+#define DW_AT_call_column 0x57
+#define DW_AT_call_file 0x58
+#define DW_AT_call_line 0x59
+#define DW_AT_MIPS_fde 0x2001
+#define DW_AT_MIPS_loop_begin 0x2002
+#define DW_AT_MIPS_tail_loop_begin 0x2003
+#define DW_AT_MIPS_epilog_begin 0x2004
+#define DW_AT_MIPS_loop_unroll_factor 0x2005
+#define DW_AT_MIPS_software_pipeline_depth 0x2006
+#define DW_AT_MIPS_linkage_name 0x2007
+#define DW_AT_MIPS_stride 0x2008
+#define DW_AT_MIPS_abstract_name 0x2009
+#define DW_AT_MIPS_clone_origin 0x200a
+#define DW_AT_MIPS_has_inlines 0x200b
+#define DW_AT_sf_names 0x2101
+#define DW_AT_src_info 0x2102
+#define DW_AT_mac_info 0x2103
+#define DW_AT_src_coords 0x2104
+#define DW_AT_body_begin 0x2105
+#define DW_AT_body_end 0x2106
+#define DW_AT_lo_user 0x2000
+#define DW_AT_hi_user 0x3ff0
+
+#define DW_OP_addr 0x03
+#define DW_OP_deref 0x06
+#define DW_OP_const1u 0x08
+#define DW_OP_const1s 0x09
+#define DW_OP_const2u 0x0a
+#define DW_OP_const2s 0x0b
+#define DW_OP_const4u 0x0c
+#define DW_OP_const4s 0x0d
+#define DW_OP_const8u 0x0e
+#define DW_OP_const8s 0x0f
+#define DW_OP_constu 0x10
+#define DW_OP_consts 0x11
+#define DW_OP_dup 0x12
+#define DW_OP_drop 0x13
+#define DW_OP_over 0x14
+#define DW_OP_pick 0x15
+#define DW_OP_swap 0x16
+#define DW_OP_rot 0x17
+#define DW_OP_xderef 0x18
+#define DW_OP_abs 0x19
+#define DW_OP_and 0x1a
+#define DW_OP_div 0x1b
+#define DW_OP_minus 0x1c
+#define DW_OP_mod 0x1d
+#define DW_OP_mul 0x1e
+#define DW_OP_neg 0x1f
+#define DW_OP_not 0x20
+#define DW_OP_or 0x21
+#define DW_OP_plus 0x22
+#define DW_OP_plus_uconst 0x23
+#define DW_OP_shl 0x24
+#define DW_OP_shr 0x25
+#define DW_OP_shra 0x26
+#define DW_OP_xor 0x27
+#define DW_OP_bra 0x28
+#define DW_OP_eq 0x29
+#define DW_OP_ge 0x2a
+#define DW_OP_gt 0x2b
+#define DW_OP_le 0x2c
+#define DW_OP_lt 0x2d
+#define DW_OP_ne 0x2e
+#define DW_OP_skip 0x2f
+#define DW_OP_lit0 0x30
+#define DW_OP_lit1 0x31
+#define DW_OP_lit2 0x32
+#define DW_OP_lit3 0x33
+#define DW_OP_lit4 0x34
+#define DW_OP_lit5 0x35
+#define DW_OP_lit6 0x36
+#define DW_OP_lit7 0x37
+#define DW_OP_lit8 0x38
+#define DW_OP_lit9 0x39
+#define DW_OP_lit10 0x3a
+#define DW_OP_lit11 0x3b
+#define DW_OP_lit12 0x3c
+#define DW_OP_lit13 0x3d
+#define DW_OP_lit14 0x3e
+#define DW_OP_lit15 0x3f
+#define DW_OP_lit16 0x40
+#define DW_OP_lit17 0x41
+#define DW_OP_lit18 0x42
+#define DW_OP_lit19 0x43
+#define DW_OP_lit20 0x44
+#define DW_OP_lit21 0x45
+#define DW_OP_lit22 0x46
+#define DW_OP_lit23 0x47
+#define DW_OP_lit24 0x48
+#define DW_OP_lit25 0x49
+#define DW_OP_lit26 0x4a
+#define DW_OP_lit27 0x4b
+#define DW_OP_lit28 0x4c
+#define DW_OP_lit29 0x4d
+#define DW_OP_lit30 0x4e
+#define DW_OP_lit31 0x4f
+#define DW_OP_reg0 0x50
+#define DW_OP_reg1 0x51
+#define DW_OP_reg2 0x52
+#define DW_OP_reg3 0x53
+#define DW_OP_reg4 0x54
+#define DW_OP_reg5 0x55
+#define DW_OP_reg6 0x56
+#define DW_OP_reg7 0x57
+#define DW_OP_reg8 0x58
+#define DW_OP_reg9 0x59
+#define DW_OP_reg10 0x5a
+#define DW_OP_reg11 0x5b
+#define DW_OP_reg12 0x5c
+#define DW_OP_reg13 0x5d
+#define DW_OP_reg14 0x5e
+#define DW_OP_reg15 0x5f
+#define DW_OP_reg16 0x60
+#define DW_OP_reg17 0x61
+#define DW_OP_reg18 0x62
+#define DW_OP_reg19 0x63
+#define DW_OP_reg20 0x64
+#define DW_OP_reg21 0x65
+#define DW_OP_reg22 0x66
+#define DW_OP_reg23 0x67
+#define DW_OP_reg24 0x68
+#define DW_OP_reg25 0x69
+#define DW_OP_reg26 0x6a
+#define DW_OP_reg27 0x6b
+#define DW_OP_reg28 0x6c
+#define DW_OP_reg29 0x6d
+#define DW_OP_reg30 0x6e
+#define DW_OP_reg31 0x6f
+#define DW_OP_breg0 0x70
+#define DW_OP_breg1 0x71
+#define DW_OP_breg2 0x72
+#define DW_OP_breg3 0x73
+#define DW_OP_breg4 0x74
+#define DW_OP_breg5 0x75
+#define DW_OP_breg6 0x76
+#define DW_OP_breg7 0x77
+#define DW_OP_breg8 0x78
+#define DW_OP_breg9 0x79
+#define DW_OP_breg10 0x7a
+#define DW_OP_breg11 0x7b
+#define DW_OP_breg12 0x7c
+#define DW_OP_breg13 0x7d
+#define DW_OP_breg14 0x7e
+#define DW_OP_breg15 0x7f
+#define DW_OP_breg16 0x80
+#define DW_OP_breg17 0x81
+#define DW_OP_breg18 0x82
+#define DW_OP_breg19 0x83
+#define DW_OP_breg20 0x84
+#define DW_OP_breg21 0x85
+#define DW_OP_breg22 0x86
+#define DW_OP_breg23 0x87
+#define DW_OP_breg24 0x88
+#define DW_OP_breg25 0x89
+#define DW_OP_breg26 0x8a
+#define DW_OP_breg27 0x8b
+#define DW_OP_breg28 0x8c
+#define DW_OP_breg29 0x8d
+#define DW_OP_breg30 0x8e
+#define DW_OP_breg31 0x8f
+#define DW_OP_regx 0x90
+#define DW_OP_fbreg 0x91
+#define DW_OP_bregx 0x92
+#define DW_OP_piece 0x93
+#define DW_OP_deref_size 0x94
+#define DW_OP_xderef_size 0x95
+#define DW_OP_nop 0x96
+#define DW_OP_push_object_address 0x97
+#define DW_OP_call2 0x98
+#define DW_OP_call4 0x99
+#define DW_OP_call_ref 0x9a
+#define DW_OP_GNU_push_tls_address 0xe0
+#define DW_OP_lo_user 0xe0
+#define DW_OP_hi_user 0xff
+
+#define DW_ATE_void 0x0
+#define DW_ATE_address 0x1
+#define DW_ATE_boolean 0x2
+#define DW_ATE_complex_float 0x3
+#define DW_ATE_float 0x4
+#define DW_ATE_signed 0x5
+#define DW_ATE_signed_char 0x6
+#define DW_ATE_unsigned 0x7
+#define DW_ATE_unsigned_char 0x8
+#define DW_ATE_imaginary_float 0x9
+#define DW_ATE_lo_user 0x80
+#define DW_ATE_hi_user 0xff
+
+#define DW_ORD_row_major 0x0
+#define DW_ORD_col_major 0x1
+
+#define DW_ACCESS_public 0x1
+#define DW_ACCESS_protected 0x2
+#define DW_ACCESS_private 0x3
+
+#define DW_VIS_local 0x1
+#define DW_VIS_exported 0x2
+#define DW_VIS_qualified 0x3
+
+#define DW_VIRTUALITY_none 0x0
+#define DW_VIRTUALITY_virtual 0x1
+#define DW_VIRTUALITY_pure_virtual 0x2
+
+#define DW_ID_case_sensitive 0x0
+#define DW_ID_up_case 0x1
+#define DW_ID_down_case 0x2
+#define DW_ID_case_insensitive 0x3
+
+#define DW_CC_normal 0x1
+#define DW_CC_program 0x2
+#define DW_CC_nocall 0x3
+#define DW_CC_lo_user 0x40
+#define DW_CC_hi_user 0xff
+
+#define DW_INL_not_inlined 0x0
+#define DW_INL_inlined 0x1
+#define DW_INL_declared_not_inlined 0x2
+#define DW_INL_declared_inlined 0x3
+
+#define DW_DSC_label 0x0
+#define DW_DSC_range 0x1
+
+#define DW_LNS_extended_op 0x0
+#define DW_LNS_copy 0x1
+#define DW_LNS_advance_pc 0x2
+#define DW_LNS_advance_line 0x3
+#define DW_LNS_set_file 0x4
+#define DW_LNS_set_column 0x5
+#define DW_LNS_negate_stmt 0x6
+#define DW_LNS_set_basic_block 0x7
+#define DW_LNS_const_add_pc 0x8
+#define DW_LNS_fixed_advance_pc 0x9
+
+#define DW_LNE_end_sequence 0x1
+#define DW_LNE_set_address 0x2
+#define DW_LNE_define_file 0x3
+
+#define DW_CFA_advance_loc 0x40
+#define DW_CFA_offset 0x80
+#define DW_CFA_restore 0xc0
+#define DW_CFA_nop 0x00
+#define DW_CFA_set_loc 0x01
+#define DW_CFA_advance_loc1 0x02
+#define DW_CFA_advance_loc2 0x03
+#define DW_CFA_advance_loc4 0x04
+#define DW_CFA_offset_extended 0x05
+#define DW_CFA_restore_extended 0x06
+#define DW_CFA_undefined 0x07
+#define DW_CFA_same_value 0x08
+#define DW_CFA_register 0x09
+#define DW_CFA_remember_state 0x0a
+#define DW_CFA_restore_state 0x0b
+#define DW_CFA_def_cfa 0x0c
+#define DW_CFA_def_cfa_register 0x0d
+#define DW_CFA_def_cfa_offset 0x0e
+#define DW_CFA_def_cfa_expression 0x0f
+#define DW_CFA_expression 0x10
+#define DW_CFA_offset_extended_sf 0x11
+#define DW_CFA_def_cfa_sf 0x12
+#define DW_CFA_def_cfa_offset_sf 0x13
+#define DW_CFA_MIPS_advance_loc8 0x1d
+#define DW_CFA_GNU_window_save 0x2d
+#define DW_CFA_GNU_args_size 0x2e
+#define DW_CFA_GNU_negative_offset_extended 0x2f
+
+#define DW_CIE_ID 0xffffffff
+#define DW_CIE_VERSION 1
+
+#define DW_CFA_extended 0
+#define DW_CFA_low_user 0x1c
+#define DW_CFA_high_user 0x3f
+
+#define DW_CHILDREN_no 0x00
+#define DW_CHILDREN_yes 0x01
+
+#define DW_ADDR_none 0
+
+#define DW_LANG_C89 0x0001
+#define DW_LANG_C 0x0002
+#define DW_LANG_Ada83 0x0003
+#define DW_LANG_C_plus_plus 0x0004
+#define DW_LANG_Cobol74 0x0005
+#define DW_LANG_Cobol85 0x0006
+#define DW_LANG_Fortran77 0x0007
+#define DW_LANG_Fortran90 0x0008
+#define DW_LANG_Pascal83 0x0009
+#define DW_LANG_Modula2 0x000a
+#define DW_LANG_Java 0x000b
+#define DW_LANG_C99 0x000c
+#define DW_LANG_Ada95 0x000d
+#define DW_LANG_Fortran95 0x000e
+#define DW_LANG_Mips_Assembler 0x8001
+#define DW_LANG_lo_user 0x8000
+#define DW_LANG_hi_user 0xffff
+
+#define DW_MACINFO_define 1
+#define DW_MACINFO_undef 2
+#define DW_MACINFO_start_file 3
+#define DW_MACINFO_end_file 4
+#define DW_MACINFO_vendor_ext 255
diff --git a/trunk/src/elf.h b/trunk/src/elf.h
new file mode 100644
index 0000000..0daff76
--- /dev/null
+++ b/trunk/src/elf.h
@@ -0,0 +1,2558 @@
+/* This file defines standard ELF types, structures, and macros.
+ Copyright (C) 1995-2003, 2004 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. */
+
+#ifndef _ELF_H
+#define _ELF_H 1
+
+#include <features.h>
+
+__BEGIN_DECLS
+
+/* Standard ELF types. */
+
+#include <stdint.h>
+
+/* Type for a 16-bit quantity. */
+typedef uint16_t Elf32_Half;
+typedef uint16_t Elf64_Half;
+
+/* Types for signed and unsigned 32-bit quantities. */
+typedef uint32_t Elf32_Word;
+typedef int32_t Elf32_Sword;
+typedef uint32_t Elf64_Word;
+typedef int32_t Elf64_Sword;
+
+/* Types for signed and unsigned 64-bit quantities. */
+typedef uint64_t Elf32_Xword;
+typedef int64_t Elf32_Sxword;
+typedef uint64_t Elf64_Xword;
+typedef int64_t Elf64_Sxword;
+
+/* Type of addresses. */
+typedef uint32_t Elf32_Addr;
+typedef uint64_t Elf64_Addr;
+
+/* Type of file offsets. */
+typedef uint32_t Elf32_Off;
+typedef uint64_t Elf64_Off;
+
+/* Type for section indices, which are 16-bit quantities. */
+typedef uint16_t Elf32_Section;
+typedef uint16_t Elf64_Section;
+
+/* Type for version symbol information. */
+typedef Elf32_Half Elf32_Versym;
+typedef Elf64_Half Elf64_Versym;
+
+
+/* The ELF file header. This appears at the start of every ELF file. */
+
+#define EI_NIDENT (16)
+
+typedef struct
+{
+ unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
+ Elf32_Half e_type; /* Object file type */
+ Elf32_Half e_machine; /* Architecture */
+ Elf32_Word e_version; /* Object file version */
+ Elf32_Addr e_entry; /* Entry point virtual address */
+ Elf32_Off e_phoff; /* Program header table file offset */
+ Elf32_Off e_shoff; /* Section header table file offset */
+ Elf32_Word e_flags; /* Processor-specific flags */
+ Elf32_Half e_ehsize; /* ELF header size in bytes */
+ Elf32_Half e_phentsize; /* Program header table entry size */
+ Elf32_Half e_phnum; /* Program header table entry count */
+ Elf32_Half e_shentsize; /* Section header table entry size */
+ Elf32_Half e_shnum; /* Section header table entry count */
+ Elf32_Half e_shstrndx; /* Section header string table index */
+} Elf32_Ehdr;
+
+typedef struct
+{
+ unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
+ Elf64_Half e_type; /* Object file type */
+ Elf64_Half e_machine; /* Architecture */
+ Elf64_Word e_version; /* Object file version */
+ Elf64_Addr e_entry; /* Entry point virtual address */
+ Elf64_Off e_phoff; /* Program header table file offset */
+ Elf64_Off e_shoff; /* Section header table file offset */
+ Elf64_Word e_flags; /* Processor-specific flags */
+ Elf64_Half e_ehsize; /* ELF header size in bytes */
+ Elf64_Half e_phentsize; /* Program header table entry size */
+ Elf64_Half e_phnum; /* Program header table entry count */
+ Elf64_Half e_shentsize; /* Section header table entry size */
+ Elf64_Half e_shnum; /* Section header table entry count */
+ Elf64_Half e_shstrndx; /* Section header string table index */
+} Elf64_Ehdr;
+
+/* Fields in the e_ident array. The EI_* macros are indices into the
+ array. The macros under each EI_* macro are the values the byte
+ may have. */
+
+#define EI_MAG0 0 /* File identification byte 0 index */
+#define ELFMAG0 0x7f /* Magic number byte 0 */
+
+#define EI_MAG1 1 /* File identification byte 1 index */
+#define ELFMAG1 'E' /* Magic number byte 1 */
+
+#define EI_MAG2 2 /* File identification byte 2 index */
+#define ELFMAG2 'L' /* Magic number byte 2 */
+
+#define EI_MAG3 3 /* File identification byte 3 index */
+#define ELFMAG3 'F' /* Magic number byte 3 */
+
+/* Conglomeration of the identification bytes, for easy testing as a word. */
+#define ELFMAG "\177ELF"
+#define SELFMAG 4
+
+#define EI_CLASS 4 /* File class byte index */
+#define ELFCLASSNONE 0 /* Invalid class */
+#define ELFCLASS32 1 /* 32-bit objects */
+#define ELFCLASS64 2 /* 64-bit objects */
+#define ELFCLASSNUM 3
+
+#define EI_DATA 5 /* Data encoding byte index */
+#define ELFDATANONE 0 /* Invalid data encoding */
+#define ELFDATA2LSB 1 /* 2's complement, little endian */
+#define ELFDATA2MSB 2 /* 2's complement, big endian */
+#define ELFDATANUM 3
+
+#define EI_VERSION 6 /* File version byte index */
+ /* Value must be EV_CURRENT */
+
+#define EI_OSABI 7 /* OS ABI identification */
+#define ELFOSABI_NONE 0 /* UNIX System V ABI */
+#define ELFOSABI_SYSV 0 /* Alias. */
+#define ELFOSABI_HPUX 1 /* HP-UX */
+#define ELFOSABI_NETBSD 2 /* NetBSD. */
+#define ELFOSABI_LINUX 3 /* Linux. */
+#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */
+#define ELFOSABI_AIX 7 /* IBM AIX. */
+#define ELFOSABI_IRIX 8 /* SGI Irix. */
+#define ELFOSABI_FREEBSD 9 /* FreeBSD. */
+#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */
+#define ELFOSABI_MODESTO 11 /* Novell Modesto. */
+#define ELFOSABI_OPENBSD 12 /* OpenBSD. */
+#define ELFOSABI_ARM 97 /* ARM */
+#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
+
+#define EI_ABIVERSION 8 /* ABI version */
+
+#define EI_PAD 9 /* Byte index of padding bytes */
+
+/* Legal values for e_type (object file type). */
+
+#define ET_NONE 0 /* No file type */
+#define ET_REL 1 /* Relocatable file */
+#define ET_EXEC 2 /* Executable file */
+#define ET_DYN 3 /* Shared object file */
+#define ET_CORE 4 /* Core file */
+#define ET_NUM 5 /* Number of defined types */
+#define ET_LOOS 0xfe00 /* OS-specific range start */
+#define ET_HIOS 0xfeff /* OS-specific range end */
+#define ET_LOPROC 0xff00 /* Processor-specific range start */
+#define ET_HIPROC 0xffff /* Processor-specific range end */
+
+/* Legal values for e_machine (architecture). */
+
+#define EM_NONE 0 /* No machine */
+#define EM_M32 1 /* AT&T WE 32100 */
+#define EM_SPARC 2 /* SUN SPARC */
+#define EM_386 3 /* Intel 80386 */
+#define EM_68K 4 /* Motorola m68k family */
+#define EM_88K 5 /* Motorola m88k family */
+#define EM_860 7 /* Intel 80860 */
+#define EM_MIPS 8 /* MIPS R3000 big-endian */
+#define EM_S370 9 /* IBM System/370 */
+#define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */
+
+#define EM_PARISC 15 /* HPPA */
+#define EM_VPP500 17 /* Fujitsu VPP500 */
+#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
+#define EM_960 19 /* Intel 80960 */
+#define EM_PPC 20 /* PowerPC */
+#define EM_PPC64 21 /* PowerPC 64-bit */
+#define EM_S390 22 /* IBM S390 */
+
+#define EM_V800 36 /* NEC V800 series */
+#define EM_FR20 37 /* Fujitsu FR20 */
+#define EM_RH32 38 /* TRW RH-32 */
+#define EM_RCE 39 /* Motorola RCE */
+#define EM_ARM 40 /* ARM */
+#define EM_FAKE_ALPHA 41 /* Digital Alpha */
+#define EM_SH 42 /* Hitachi SH */
+#define EM_SPARCV9 43 /* SPARC v9 64-bit */
+#define EM_TRICORE 44 /* Siemens Tricore */
+#define EM_ARC 45 /* Argonaut RISC Core */
+#define EM_H8_300 46 /* Hitachi H8/300 */
+#define EM_H8_300H 47 /* Hitachi H8/300H */
+#define EM_H8S 48 /* Hitachi H8S */
+#define EM_H8_500 49 /* Hitachi H8/500 */
+#define EM_IA_64 50 /* Intel Merced */
+#define EM_MIPS_X 51 /* Stanford MIPS-X */
+#define EM_COLDFIRE 52 /* Motorola Coldfire */
+#define EM_68HC12 53 /* Motorola M68HC12 */
+#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/
+#define EM_PCP 55 /* Siemens PCP */
+#define EM_NCPU 56 /* Sony nCPU embeeded RISC */
+#define EM_NDR1 57 /* Denso NDR1 microprocessor */
+#define EM_STARCORE 58 /* Motorola Start*Core processor */
+#define EM_ME16 59 /* Toyota ME16 processor */
+#define EM_ST100 60 /* STMicroelectronic ST100 processor */
+#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/
+#define EM_X86_64 62 /* AMD x86-64 architecture */
+#define EM_PDSP 63 /* Sony DSP Processor */
+
+#define EM_FX66 66 /* Siemens FX66 microcontroller */
+#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */
+#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */
+#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */
+#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */
+#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */
+#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */
+#define EM_SVX 73 /* Silicon Graphics SVx */
+#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */
+#define EM_VAX 75 /* Digital VAX */
+#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */
+#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */
+#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */
+#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */
+#define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */
+#define EM_HUANY 81 /* Harvard University machine-independent object files */
+#define EM_PRISM 82 /* SiTera Prism */
+#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */
+#define EM_FR30 84 /* Fujitsu FR30 */
+#define EM_D10V 85 /* Mitsubishi D10V */
+#define EM_D30V 86 /* Mitsubishi D30V */
+#define EM_V850 87 /* NEC v850 */
+#define EM_M32R 88 /* Mitsubishi M32R */
+#define EM_MN10300 89 /* Matsushita MN10300 */
+#define EM_MN10200 90 /* Matsushita MN10200 */
+#define EM_PJ 91 /* picoJava */
+#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
+#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */
+#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */
+#define EM_NUM 95
+
+/* If it is necessary to assign new unofficial EM_* values, please
+ pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
+ chances of collision with official or non-GNU unofficial values. */
+
+#define EM_ALPHA 0x9026
+
+/* Legal values for e_version (version). */
+
+#define EV_NONE 0 /* Invalid ELF version */
+#define EV_CURRENT 1 /* Current version */
+#define EV_NUM 2
+
+/* Section header. */
+
+typedef struct
+{
+ Elf32_Word sh_name; /* Section name (string tbl index) */
+ Elf32_Word sh_type; /* Section type */
+ Elf32_Word sh_flags; /* Section flags */
+ Elf32_Addr sh_addr; /* Section virtual addr at execution */
+ Elf32_Off sh_offset; /* Section file offset */
+ Elf32_Word sh_size; /* Section size in bytes */
+ Elf32_Word sh_link; /* Link to another section */
+ Elf32_Word sh_info; /* Additional section information */
+ Elf32_Word sh_addralign; /* Section alignment */
+ Elf32_Word sh_entsize; /* Entry size if section holds table */
+} Elf32_Shdr;
+
+typedef struct
+{
+ Elf64_Word sh_name; /* Section name (string tbl index) */
+ Elf64_Word sh_type; /* Section type */
+ Elf64_Xword sh_flags; /* Section flags */
+ Elf64_Addr sh_addr; /* Section virtual addr at execution */
+ Elf64_Off sh_offset; /* Section file offset */
+ Elf64_Xword sh_size; /* Section size in bytes */
+ Elf64_Word sh_link; /* Link to another section */
+ Elf64_Word sh_info; /* Additional section information */
+ Elf64_Xword sh_addralign; /* Section alignment */
+ Elf64_Xword sh_entsize; /* Entry size if section holds table */
+} Elf64_Shdr;
+
+/* Special section indices. */
+
+#define SHN_UNDEF 0 /* Undefined section */
+#define SHN_LORESERVE 0xff00 /* Start of reserved indices */
+#define SHN_LOPROC 0xff00 /* Start of processor-specific */
+#define SHN_BEFORE 0xff00 /* Order section before all others
+ (Solaris). */
+#define SHN_AFTER 0xff01 /* Order section after all others
+ (Solaris). */
+#define SHN_HIPROC 0xff1f /* End of processor-specific */
+#define SHN_LOOS 0xff20 /* Start of OS-specific */
+#define SHN_HIOS 0xff3f /* End of OS-specific */
+#define SHN_ABS 0xfff1 /* Associated symbol is absolute */
+#define SHN_COMMON 0xfff2 /* Associated symbol is common */
+#define SHN_XINDEX 0xffff /* Index is in extra table. */
+#define SHN_HIRESERVE 0xffff /* End of reserved indices */
+
+/* Legal values for sh_type (section type). */
+
+#define SHT_NULL 0 /* Section header table entry unused */
+#define SHT_PROGBITS 1 /* Program data */
+#define SHT_SYMTAB 2 /* Symbol table */
+#define SHT_STRTAB 3 /* String table */
+#define SHT_RELA 4 /* Relocation entries with addends */
+#define SHT_HASH 5 /* Symbol hash table */
+#define SHT_DYNAMIC 6 /* Dynamic linking information */
+#define SHT_NOTE 7 /* Notes */
+#define SHT_NOBITS 8 /* Program space with no data (bss) */
+#define SHT_REL 9 /* Relocation entries, no addends */
+#define SHT_SHLIB 10 /* Reserved */
+#define SHT_DYNSYM 11 /* Dynamic linker symbol table */
+#define SHT_INIT_ARRAY 14 /* Array of constructors */
+#define SHT_FINI_ARRAY 15 /* Array of destructors */
+#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */
+#define SHT_GROUP 17 /* Section group */
+#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
+#define SHT_NUM 19 /* Number of defined types. */
+#define SHT_LOOS 0x60000000 /* Start OS-specific */
+#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */
+#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */
+#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */
+#define SHT_SUNW_move 0x6ffffffa
+#define SHT_SUNW_COMDAT 0x6ffffffb
+#define SHT_SUNW_syminfo 0x6ffffffc
+#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */
+#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */
+#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */
+#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */
+#define SHT_HIOS 0x6fffffff /* End OS-specific type */
+#define SHT_LOPROC 0x70000000 /* Start of processor-specific */
+#define SHT_HIPROC 0x7fffffff /* End of processor-specific */
+#define SHT_LOUSER 0x80000000 /* Start of application-specific */
+#define SHT_HIUSER 0x8fffffff /* End of application-specific */
+
+/* Legal values for sh_flags (section flags). */
+
+#define SHF_WRITE (1 << 0) /* Writable */
+#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
+#define SHF_EXECINSTR (1 << 2) /* Executable */
+#define SHF_MERGE (1 << 4) /* Might be merged */
+#define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */
+#define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */
+#define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */
+#define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling
+ required */
+#define SHF_GROUP (1 << 9) /* Section is member of a group. */
+#define SHF_TLS (1 << 10) /* Section hold thread-local data. */
+#define SHF_MASKOS 0x0ff00000 /* OS-specific. */
+#define SHF_MASKPROC 0xf0000000 /* Processor-specific */
+#define SHF_ORDERED (1 << 30) /* Special ordering requirement
+ (Solaris). */
+#define SHF_EXCLUDE (1 << 31) /* Section is excluded unless
+ referenced or allocated (Solaris).*/
+
+/* Section group handling. */
+#define GRP_COMDAT 0x1 /* Mark group as COMDAT. */
+
+/* Symbol table entry. */
+
+typedef struct
+{
+ Elf32_Word st_name; /* Symbol name (string tbl index) */
+ Elf32_Addr st_value; /* Symbol value */
+ Elf32_Word st_size; /* Symbol size */
+ unsigned char st_info; /* Symbol type and binding */
+ unsigned char st_other; /* Symbol visibility */
+ Elf32_Section st_shndx; /* Section index */
+} Elf32_Sym;
+
+typedef struct
+{
+ Elf64_Word st_name; /* Symbol name (string tbl index) */
+ unsigned char st_info; /* Symbol type and binding */
+ unsigned char st_other; /* Symbol visibility */
+ Elf64_Section st_shndx; /* Section index */
+ Elf64_Addr st_value; /* Symbol value */
+ Elf64_Xword st_size; /* Symbol size */
+} Elf64_Sym;
+
+/* The syminfo section if available contains additional information about
+ every dynamic symbol. */
+
+typedef struct
+{
+ Elf32_Half si_boundto; /* Direct bindings, symbol bound to */
+ Elf32_Half si_flags; /* Per symbol flags */
+} Elf32_Syminfo;
+
+typedef struct
+{
+ Elf64_Half si_boundto; /* Direct bindings, symbol bound to */
+ Elf64_Half si_flags; /* Per symbol flags */
+} Elf64_Syminfo;
+
+/* Possible values for si_boundto. */
+#define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */
+#define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */
+#define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */
+
+/* Possible bitmasks for si_flags. */
+#define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */
+#define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-thru symbol for translator */
+#define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */
+#define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy
+ loaded */
+/* Syminfo version values. */
+#define SYMINFO_NONE 0
+#define SYMINFO_CURRENT 1
+#define SYMINFO_NUM 2
+
+
+/* How to extract and insert information held in the st_info field. */
+
+#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4)
+#define ELF32_ST_TYPE(val) ((val) & 0xf)
+#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
+
+/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */
+#define ELF64_ST_BIND(val) ELF32_ST_BIND (val)
+#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val)
+#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type))
+
+/* Legal values for ST_BIND subfield of st_info (symbol binding). */
+
+#define STB_LOCAL 0 /* Local symbol */
+#define STB_GLOBAL 1 /* Global symbol */
+#define STB_WEAK 2 /* Weak symbol */
+#define STB_NUM 3 /* Number of defined types. */
+#define STB_LOOS 10 /* Start of OS-specific */
+#define STB_HIOS 12 /* End of OS-specific */
+#define STB_LOPROC 13 /* Start of processor-specific */
+#define STB_HIPROC 15 /* End of processor-specific */
+
+/* Legal values for ST_TYPE subfield of st_info (symbol type). */
+
+#define STT_NOTYPE 0 /* Symbol type is unspecified */
+#define STT_OBJECT 1 /* Symbol is a data object */
+#define STT_FUNC 2 /* Symbol is a code object */
+#define STT_SECTION 3 /* Symbol associated with a section */
+#define STT_FILE 4 /* Symbol's name is file name */
+#define STT_COMMON 5 /* Symbol is a common data object */
+#define STT_TLS 6 /* Symbol is thread-local data object*/
+#define STT_NUM 7 /* Number of defined types. */
+#define STT_LOOS 10 /* Start of OS-specific */
+#define STT_HIOS 12 /* End of OS-specific */
+#define STT_LOPROC 13 /* Start of processor-specific */
+#define STT_HIPROC 15 /* End of processor-specific */
+
+
+/* Symbol table indices are found in the hash buckets and chain table
+ of a symbol hash table section. This special index value indicates
+ the end of a chain, meaning no further symbols are found in that bucket. */
+
+#define STN_UNDEF 0 /* End of a chain. */
+
+
+/* How to extract and insert information held in the st_other field. */
+
+#define ELF32_ST_VISIBILITY(o) ((o) & 0x03)
+
+/* For ELF64 the definitions are the same. */
+#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o)
+
+/* Symbol visibility specification encoded in the st_other field. */
+#define STV_DEFAULT 0 /* Default symbol visibility rules */
+#define STV_INTERNAL 1 /* Processor specific hidden class */
+#define STV_HIDDEN 2 /* Sym unavailable in other modules */
+#define STV_PROTECTED 3 /* Not preemptible, not exported */
+
+
+/* Relocation table entry without addend (in section of type SHT_REL). */
+
+typedef struct
+{
+ Elf32_Addr r_offset; /* Address */
+ Elf32_Word r_info; /* Relocation type and symbol index */
+} Elf32_Rel;
+
+/* I have seen two different definitions of the Elf64_Rel and
+ Elf64_Rela structures, so we'll leave them out until Novell (or
+ whoever) gets their act together. */
+/* The following, at least, is used on Sparc v9, MIPS, and Alpha. */
+
+typedef struct
+{
+ Elf64_Addr r_offset; /* Address */
+ Elf64_Xword r_info; /* Relocation type and symbol index */
+} Elf64_Rel;
+
+/* Relocation table entry with addend (in section of type SHT_RELA). */
+
+typedef struct
+{
+ Elf32_Addr r_offset; /* Address */
+ Elf32_Word r_info; /* Relocation type and symbol index */
+ Elf32_Sword r_addend; /* Addend */
+} Elf32_Rela;
+
+typedef struct
+{
+ Elf64_Addr r_offset; /* Address */
+ Elf64_Xword r_info; /* Relocation type and symbol index */
+ Elf64_Sxword r_addend; /* Addend */
+} Elf64_Rela;
+
+/* How to extract and insert information held in the r_info field. */
+
+#define ELF32_R_SYM(val) ((val) >> 8)
+#define ELF32_R_TYPE(val) ((val) & 0xff)
+#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff))
+
+#define ELF64_R_SYM(i) ((i) >> 32)
+#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
+#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
+
+/* Program segment header. */
+
+typedef struct
+{
+ Elf32_Word p_type; /* Segment type */
+ Elf32_Off p_offset; /* Segment file offset */
+ Elf32_Addr p_vaddr; /* Segment virtual address */
+ Elf32_Addr p_paddr; /* Segment physical address */
+ Elf32_Word p_filesz; /* Segment size in file */
+ Elf32_Word p_memsz; /* Segment size in memory */
+ Elf32_Word p_flags; /* Segment flags */
+ Elf32_Word p_align; /* Segment alignment */
+} Elf32_Phdr;
+
+typedef struct
+{
+ Elf64_Word p_type; /* Segment type */
+ Elf64_Word p_flags; /* Segment flags */
+ Elf64_Off p_offset; /* Segment file offset */
+ Elf64_Addr p_vaddr; /* Segment virtual address */
+ Elf64_Addr p_paddr; /* Segment physical address */
+ Elf64_Xword p_filesz; /* Segment size in file */
+ Elf64_Xword p_memsz; /* Segment size in memory */
+ Elf64_Xword p_align; /* Segment alignment */
+} Elf64_Phdr;
+
+/* Legal values for p_type (segment type). */
+
+#define PT_NULL 0 /* Program header table entry unused */
+#define PT_LOAD 1 /* Loadable program segment */
+#define PT_DYNAMIC 2 /* Dynamic linking information */
+#define PT_INTERP 3 /* Program interpreter */
+#define PT_NOTE 4 /* Auxiliary information */
+#define PT_SHLIB 5 /* Reserved */
+#define PT_PHDR 6 /* Entry for header table itself */
+#define PT_TLS 7 /* Thread-local storage segment */
+#define PT_NUM 8 /* Number of defined types */
+#define PT_LOOS 0x60000000 /* Start of OS-specific */
+#define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
+#define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */
+#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
+#define PT_LOSUNW 0x6ffffffa
+#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */
+#define PT_SUNWSTACK 0x6ffffffb /* Stack segment */
+#define PT_HISUNW 0x6fffffff
+#define PT_HIOS 0x6fffffff /* End of OS-specific */
+#define PT_LOPROC 0x70000000 /* Start of processor-specific */
+#define PT_HIPROC 0x7fffffff /* End of processor-specific */
+
+/* Legal values for p_flags (segment flags). */
+
+#define PF_X (1 << 0) /* Segment is executable */
+#define PF_W (1 << 1) /* Segment is writable */
+#define PF_R (1 << 2) /* Segment is readable */
+#define PF_MASKOS 0x0ff00000 /* OS-specific */
+#define PF_MASKPROC 0xf0000000 /* Processor-specific */
+
+/* Legal values for note segment descriptor types for core files. */
+
+#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */
+#define NT_FPREGSET 2 /* Contains copy of fpregset struct */
+#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */
+#define NT_PRXREG 4 /* Contains copy of prxregset struct */
+#define NT_TASKSTRUCT 4 /* Contains copy of task structure */
+#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */
+#define NT_AUXV 6 /* Contains copy of auxv array */
+#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */
+#define NT_ASRS 8 /* Contains copy of asrset struct */
+#define NT_PSTATUS 10 /* Contains copy of pstatus struct */
+#define NT_PSINFO 13 /* Contains copy of psinfo struct */
+#define NT_PRCRED 14 /* Contains copy of prcred struct */
+#define NT_UTSNAME 15 /* Contains copy of utsname struct */
+#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */
+#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */
+#define NT_PRFPXREG 20 /* Contains copy of fprxregset struct*/
+
+/* Legal values for the note segment descriptor types for object files. */
+
+#define NT_VERSION 1 /* Contains a version string. */
+
+
+/* Dynamic section entry. */
+
+typedef struct
+{
+ Elf32_Sword d_tag; /* Dynamic entry type */
+ union
+ {
+ Elf32_Word d_val; /* Integer value */
+ Elf32_Addr d_ptr; /* Address value */
+ } d_un;
+} Elf32_Dyn;
+
+typedef struct
+{
+ Elf64_Sxword d_tag; /* Dynamic entry type */
+ union
+ {
+ Elf64_Xword d_val; /* Integer value */
+ Elf64_Addr d_ptr; /* Address value */
+ } d_un;
+} Elf64_Dyn;
+
+/* Legal values for d_tag (dynamic entry type). */
+
+#define DT_NULL 0 /* Marks end of dynamic section */
+#define DT_NEEDED 1 /* Name of needed library */
+#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */
+#define DT_PLTGOT 3 /* Processor defined value */
+#define DT_HASH 4 /* Address of symbol hash table */
+#define DT_STRTAB 5 /* Address of string table */
+#define DT_SYMTAB 6 /* Address of symbol table */
+#define DT_RELA 7 /* Address of Rela relocs */
+#define DT_RELASZ 8 /* Total size of Rela relocs */
+#define DT_RELAENT 9 /* Size of one Rela reloc */
+#define DT_STRSZ 10 /* Size of string table */
+#define DT_SYMENT 11 /* Size of one symbol table entry */
+#define DT_INIT 12 /* Address of init function */
+#define DT_FINI 13 /* Address of termination function */
+#define DT_SONAME 14 /* Name of shared object */
+#define DT_RPATH 15 /* Library search path (deprecated) */
+#define DT_SYMBOLIC 16 /* Start symbol search here */
+#define DT_REL 17 /* Address of Rel relocs */
+#define DT_RELSZ 18 /* Total size of Rel relocs */
+#define DT_RELENT 19 /* Size of one Rel reloc */
+#define DT_PLTREL 20 /* Type of reloc in PLT */
+#define DT_DEBUG 21 /* For debugging; unspecified */
+#define DT_TEXTREL 22 /* Reloc might modify .text */
+#define DT_JMPREL 23 /* Address of PLT relocs */
+#define DT_BIND_NOW 24 /* Process relocations of object */
+#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */
+#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */
+#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */
+#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */
+#define DT_RUNPATH 29 /* Library search path */
+#define DT_FLAGS 30 /* Flags for the object being loaded */
+#define DT_ENCODING 32 /* Start of encoded range */
+#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/
+#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */
+#define DT_NUM 34 /* Number used */
+#define DT_LOOS 0x6000000d /* Start of OS-specific */
+#define DT_HIOS 0x6ffff000 /* End of OS-specific */
+#define DT_LOPROC 0x70000000 /* Start of processor-specific */
+#define DT_HIPROC 0x7fffffff /* End of processor-specific */
+#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */
+
+/* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the
+ Dyn.d_un.d_val field of the Elf*_Dyn structure. This follows Sun's
+ approach. */
+#define DT_VALRNGLO 0x6ffffd00
+#define DT_GNU_PRELINKED 0x6ffffdf5 /* Prelinking timestamp */
+#define DT_GNU_CONFLICTSZ 0x6ffffdf6 /* Size of conflict section */
+#define DT_GNU_LIBLISTSZ 0x6ffffdf7 /* Size of library list */
+#define DT_CHECKSUM 0x6ffffdf8
+#define DT_PLTPADSZ 0x6ffffdf9
+#define DT_MOVEENT 0x6ffffdfa
+#define DT_MOVESZ 0x6ffffdfb
+#define DT_FEATURE_1 0x6ffffdfc /* Feature selection (DTF_*). */
+#define DT_POSFLAG_1 0x6ffffdfd /* Flags for DT_* entries, effecting
+ the following DT_* entry. */
+#define DT_SYMINSZ 0x6ffffdfe /* Size of syminfo table (in bytes) */
+#define DT_SYMINENT 0x6ffffdff /* Entry size of syminfo */
+#define DT_VALRNGHI 0x6ffffdff
+#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) /* Reverse order! */
+#define DT_VALNUM 12
+
+/* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the
+ Dyn.d_un.d_ptr field of the Elf*_Dyn structure.
+
+ If any adjustment is made to the ELF object after it has been
+ built these entries will need to be adjusted. */
+#define DT_ADDRRNGLO 0x6ffffe00
+#define DT_GNU_CONFLICT 0x6ffffef8 /* Start of conflict section */
+#define DT_GNU_LIBLIST 0x6ffffef9 /* Library list */
+#define DT_CONFIG 0x6ffffefa /* Configuration information. */
+#define DT_DEPAUDIT 0x6ffffefb /* Dependency auditing. */
+#define DT_AUDIT 0x6ffffefc /* Object auditing. */
+#define DT_PLTPAD 0x6ffffefd /* PLT padding. */
+#define DT_MOVETAB 0x6ffffefe /* Move table. */
+#define DT_SYMINFO 0x6ffffeff /* Syminfo table. */
+#define DT_ADDRRNGHI 0x6ffffeff
+#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) /* Reverse order! */
+#define DT_ADDRNUM 10
+
+/* The versioning entry types. The next are defined as part of the
+ GNU extension. */
+#define DT_VERSYM 0x6ffffff0
+
+#define DT_RELACOUNT 0x6ffffff9
+#define DT_RELCOUNT 0x6ffffffa
+
+/* These were chosen by Sun. */
+#define DT_FLAGS_1 0x6ffffffb /* State flags, see DF_1_* below. */
+#define DT_VERDEF 0x6ffffffc /* Address of version definition
+ table */
+#define DT_VERDEFNUM 0x6ffffffd /* Number of version definitions */
+#define DT_VERNEED 0x6ffffffe /* Address of table with needed
+ versions */
+#define DT_VERNEEDNUM 0x6fffffff /* Number of needed versions */
+#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
+#define DT_VERSIONTAGNUM 16
+
+/* Sun added these machine-independent extensions in the "processor-specific"
+ range. Be compatible. */
+#define DT_AUXILIARY 0x7ffffffd /* Shared object to load before self */
+#define DT_FILTER 0x7fffffff /* Shared object to get values from */
+#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1)
+#define DT_EXTRANUM 3
+
+/* Values of `d_un.d_val' in the DT_FLAGS entry. */
+#define DF_ORIGIN 0x00000001 /* Object may use DF_ORIGIN */
+#define DF_SYMBOLIC 0x00000002 /* Symbol resolutions starts here */
+#define DF_TEXTREL 0x00000004 /* Object contains text relocations */
+#define DF_BIND_NOW 0x00000008 /* No lazy binding for this object */
+#define DF_STATIC_TLS 0x00000010 /* Module uses the static TLS model */
+
+/* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
+ entry in the dynamic section. */
+#define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */
+#define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */
+#define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */
+#define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/
+#define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/
+#define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/
+#define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */
+#define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */
+#define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */
+#define DF_1_TRANS 0x00000200
+#define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */
+#define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */
+#define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */
+#define DF_1_CONFALT 0x00002000 /* Configuration alternative created.*/
+#define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */
+#define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */
+#define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */
+
+/* Flags for the feature selection in DT_FEATURE_1. */
+#define DTF_1_PARINIT 0x00000001
+#define DTF_1_CONFEXP 0x00000002
+
+/* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */
+#define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */
+#define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not
+ generally available. */
+
+/* Version definition sections. */
+
+typedef struct
+{
+ Elf32_Half vd_version; /* Version revision */
+ Elf32_Half vd_flags; /* Version information */
+ Elf32_Half vd_ndx; /* Version Index */
+ Elf32_Half vd_cnt; /* Number of associated aux entries */
+ Elf32_Word vd_hash; /* Version name hash value */
+ Elf32_Word vd_aux; /* Offset in bytes to verdaux array */
+ Elf32_Word vd_next; /* Offset in bytes to next verdef
+ entry */
+} Elf32_Verdef;
+
+typedef struct
+{
+ Elf64_Half vd_version; /* Version revision */
+ Elf64_Half vd_flags; /* Version information */
+ Elf64_Half vd_ndx; /* Version Index */
+ Elf64_Half vd_cnt; /* Number of associated aux entries */
+ Elf64_Word vd_hash; /* Version name hash value */
+ Elf64_Word vd_aux; /* Offset in bytes to verdaux array */
+ Elf64_Word vd_next; /* Offset in bytes to next verdef
+ entry */
+} Elf64_Verdef;
+
+
+/* Legal values for vd_version (version revision). */
+#define VER_DEF_NONE 0 /* No version */
+#define VER_DEF_CURRENT 1 /* Current version */
+#define VER_DEF_NUM 2 /* Given version number */
+
+/* Legal values for vd_flags (version information flags). */
+#define VER_FLG_BASE 0x1 /* Version definition of file itself */
+#define VER_FLG_WEAK 0x2 /* Weak version identifier */
+
+/* Versym symbol index values. */
+#define VER_NDX_LOCAL 0 /* Symbol is local. */
+#define VER_NDX_GLOBAL 1 /* Symbol is global. */
+#define VER_NDX_LORESERVE 0xff00 /* Beginning of reserved entries. */
+#define VER_NDX_ELIMINATE 0xff01 /* Symbol is to be eliminated. */
+
+/* Auxialiary version information. */
+
+typedef struct
+{
+ Elf32_Word vda_name; /* Version or dependency names */
+ Elf32_Word vda_next; /* Offset in bytes to next verdaux
+ entry */
+} Elf32_Verdaux;
+
+typedef struct
+{
+ Elf64_Word vda_name; /* Version or dependency names */
+ Elf64_Word vda_next; /* Offset in bytes to next verdaux
+ entry */
+} Elf64_Verdaux;
+
+
+/* Version dependency section. */
+
+typedef struct
+{
+ Elf32_Half vn_version; /* Version of structure */
+ Elf32_Half vn_cnt; /* Number of associated aux entries */
+ Elf32_Word vn_file; /* Offset of filename for this
+ dependency */
+ Elf32_Word vn_aux; /* Offset in bytes to vernaux array */
+ Elf32_Word vn_next; /* Offset in bytes to next verneed
+ entry */
+} Elf32_Verneed;
+
+typedef struct
+{
+ Elf64_Half vn_version; /* Version of structure */
+ Elf64_Half vn_cnt; /* Number of associated aux entries */
+ Elf64_Word vn_file; /* Offset of filename for this
+ dependency */
+ Elf64_Word vn_aux; /* Offset in bytes to vernaux array */
+ Elf64_Word vn_next; /* Offset in bytes to next verneed
+ entry */
+} Elf64_Verneed;
+
+
+/* Legal values for vn_version (version revision). */
+#define VER_NEED_NONE 0 /* No version */
+#define VER_NEED_CURRENT 1 /* Current version */
+#define VER_NEED_NUM 2 /* Given version number */
+
+/* Auxiliary needed version information. */
+
+typedef struct
+{
+ Elf32_Word vna_hash; /* Hash value of dependency name */
+ Elf32_Half vna_flags; /* Dependency specific information */
+ Elf32_Half vna_other; /* Unused */
+ Elf32_Word vna_name; /* Dependency name string offset */
+ Elf32_Word vna_next; /* Offset in bytes to next vernaux
+ entry */
+} Elf32_Vernaux;
+
+typedef struct
+{
+ Elf64_Word vna_hash; /* Hash value of dependency name */
+ Elf64_Half vna_flags; /* Dependency specific information */
+ Elf64_Half vna_other; /* Unused */
+ Elf64_Word vna_name; /* Dependency name string offset */
+ Elf64_Word vna_next; /* Offset in bytes to next vernaux
+ entry */
+} Elf64_Vernaux;
+
+
+/* Legal values for vna_flags. */
+#define VER_FLG_WEAK 0x2 /* Weak version identifier */
+
+
+/* Auxiliary vector. */
+
+/* This vector is normally only used by the program interpreter. The
+ usual definition in an ABI supplement uses the name auxv_t. The
+ vector is not usually defined in a standard <elf.h> file, but it
+ can't hurt. We rename it to avoid conflicts. The sizes of these
+ types are an arrangement between the exec server and the program
+ interpreter, so we don't fully specify them here. */
+
+typedef struct
+{
+ int a_type; /* Entry type */
+ union
+ {
+ long int a_val; /* Integer value */
+ void *a_ptr; /* Pointer value */
+ void (*a_fcn) (void); /* Function pointer value */
+ } a_un;
+} Elf32_auxv_t;
+
+typedef struct
+{
+ long int a_type; /* Entry type */
+ union
+ {
+ long int a_val; /* Integer value */
+ void *a_ptr; /* Pointer value */
+ void (*a_fcn) (void); /* Function pointer value */
+ } a_un;
+} Elf64_auxv_t;
+
+/* Legal values for a_type (entry type). */
+
+#define AT_NULL 0 /* End of vector */
+#define AT_IGNORE 1 /* Entry should be ignored */
+#define AT_EXECFD 2 /* File descriptor of program */
+#define AT_PHDR 3 /* Program headers for program */
+#define AT_PHENT 4 /* Size of program header entry */
+#define AT_PHNUM 5 /* Number of program headers */
+#define AT_PAGESZ 6 /* System page size */
+#define AT_BASE 7 /* Base address of interpreter */
+#define AT_FLAGS 8 /* Flags */
+#define AT_ENTRY 9 /* Entry point of program */
+#define AT_NOTELF 10 /* Program is not ELF */
+#define AT_UID 11 /* Real uid */
+#define AT_EUID 12 /* Effective uid */
+#define AT_GID 13 /* Real gid */
+#define AT_EGID 14 /* Effective gid */
+#define AT_CLKTCK 17 /* Frequency of times() */
+
+/* Some more special a_type values describing the hardware. */
+#define AT_PLATFORM 15 /* String identifying platform. */
+#define AT_HWCAP 16 /* Machine dependent hints about
+ processor capabilities. */
+
+/* This entry gives some information about the FPU initialization
+ performed by the kernel. */
+#define AT_FPUCW 18 /* Used FPU control word. */
+
+/* Cache block sizes. */
+#define AT_DCACHEBSIZE 19 /* Data cache block size. */
+#define AT_ICACHEBSIZE 20 /* Instruction cache block size. */
+#define AT_UCACHEBSIZE 21 /* Unified cache block size. */
+
+/* A special ignored value for PPC, used by the kernel to control the
+ interpretation of the AUXV. Must be > 16. */
+#define AT_IGNOREPPC 22 /* Entry should be ignored. */
+
+#define AT_SECURE 23 /* Boolean, was exec setuid-like? */
+
+/* Pointer to the global system page used for system calls and other
+ nice things. */
+#define AT_SYSINFO 32
+#define AT_SYSINFO_EHDR 33
+
+/* Shapes of the caches. Bits 0-3 contains associativity; bits 4-7 contains
+ log2 of line size; mask those to get cache size. */
+#define AT_L1I_CACHESHAPE 34
+#define AT_L1D_CACHESHAPE 35
+#define AT_L2_CACHESHAPE 36
+#define AT_L3_CACHESHAPE 37
+
+/* Note section contents. Each entry in the note section begins with
+ a header of a fixed form. */
+
+typedef struct
+{
+ Elf32_Word n_namesz; /* Length of the note's name. */
+ Elf32_Word n_descsz; /* Length of the note's descriptor. */
+ Elf32_Word n_type; /* Type of the note. */
+} Elf32_Nhdr;
+
+typedef struct
+{
+ Elf64_Word n_namesz; /* Length of the note's name. */
+ Elf64_Word n_descsz; /* Length of the note's descriptor. */
+ Elf64_Word n_type; /* Type of the note. */
+} Elf64_Nhdr;
+
+/* Known names of notes. */
+
+/* Solaris entries in the note section have this name. */
+#define ELF_NOTE_SOLARIS "SUNW Solaris"
+
+/* Note entries for GNU systems have this name. */
+#define ELF_NOTE_GNU "GNU"
+
+
+/* Defined types of notes for Solaris. */
+
+/* Value of descriptor (one word) is desired pagesize for the binary. */
+#define ELF_NOTE_PAGESIZE_HINT 1
+
+
+/* Defined note types for GNU systems. */
+
+/* ABI information. The descriptor consists of words:
+ word 0: OS descriptor
+ word 1: major version of the ABI
+ word 2: minor version of the ABI
+ word 3: subminor version of the ABI
+*/
+#define ELF_NOTE_ABI 1
+
+/* Known OSes. These value can appear in word 0 of an ELF_NOTE_ABI
+ note section entry. */
+#define ELF_NOTE_OS_LINUX 0
+#define ELF_NOTE_OS_GNU 1
+#define ELF_NOTE_OS_SOLARIS2 2
+#define ELF_NOTE_OS_FREEBSD 3
+
+
+/* Move records. */
+typedef struct
+{
+ Elf32_Xword m_value; /* Symbol value. */
+ Elf32_Word m_info; /* Size and index. */
+ Elf32_Word m_poffset; /* Symbol offset. */
+ Elf32_Half m_repeat; /* Repeat count. */
+ Elf32_Half m_stride; /* Stride info. */
+} Elf32_Move;
+
+typedef struct
+{
+ Elf64_Xword m_value; /* Symbol value. */
+ Elf64_Xword m_info; /* Size and index. */
+ Elf64_Xword m_poffset; /* Symbol offset. */
+ Elf64_Half m_repeat; /* Repeat count. */
+ Elf64_Half m_stride; /* Stride info. */
+} Elf64_Move;
+
+/* Macro to construct move records. */
+#define ELF32_M_SYM(info) ((info) >> 8)
+#define ELF32_M_SIZE(info) ((unsigned char) (info))
+#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size))
+
+#define ELF64_M_SYM(info) ELF32_M_SYM (info)
+#define ELF64_M_SIZE(info) ELF32_M_SIZE (info)
+#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size)
+
+
+/* Motorola 68k specific definitions. */
+
+/* Values for Elf32_Ehdr.e_flags. */
+#define EF_CPU32 0x00810000
+
+/* m68k relocs. */
+
+#define R_68K_NONE 0 /* No reloc */
+#define R_68K_32 1 /* Direct 32 bit */
+#define R_68K_16 2 /* Direct 16 bit */
+#define R_68K_8 3 /* Direct 8 bit */
+#define R_68K_PC32 4 /* PC relative 32 bit */
+#define R_68K_PC16 5 /* PC relative 16 bit */
+#define R_68K_PC8 6 /* PC relative 8 bit */
+#define R_68K_GOT32 7 /* 32 bit PC relative GOT entry */
+#define R_68K_GOT16 8 /* 16 bit PC relative GOT entry */
+#define R_68K_GOT8 9 /* 8 bit PC relative GOT entry */
+#define R_68K_GOT32O 10 /* 32 bit GOT offset */
+#define R_68K_GOT16O 11 /* 16 bit GOT offset */
+#define R_68K_GOT8O 12 /* 8 bit GOT offset */
+#define R_68K_PLT32 13 /* 32 bit PC relative PLT address */
+#define R_68K_PLT16 14 /* 16 bit PC relative PLT address */
+#define R_68K_PLT8 15 /* 8 bit PC relative PLT address */
+#define R_68K_PLT32O 16 /* 32 bit PLT offset */
+#define R_68K_PLT16O 17 /* 16 bit PLT offset */
+#define R_68K_PLT8O 18 /* 8 bit PLT offset */
+#define R_68K_COPY 19 /* Copy symbol at runtime */
+#define R_68K_GLOB_DAT 20 /* Create GOT entry */
+#define R_68K_JMP_SLOT 21 /* Create PLT entry */
+#define R_68K_RELATIVE 22 /* Adjust by program base */
+/* Keep this the last entry. */
+#define R_68K_NUM 23
+
+/* Intel 80386 specific definitions. */
+
+/* i386 relocs. */
+
+#define R_386_NONE 0 /* No reloc */
+#define R_386_32 1 /* Direct 32 bit */
+#define R_386_PC32 2 /* PC relative 32 bit */
+#define R_386_GOT32 3 /* 32 bit GOT entry */
+#define R_386_PLT32 4 /* 32 bit PLT address */
+#define R_386_COPY 5 /* Copy symbol at runtime */
+#define R_386_GLOB_DAT 6 /* Create GOT entry */
+#define R_386_JMP_SLOT 7 /* Create PLT entry */
+#define R_386_RELATIVE 8 /* Adjust by program base */
+#define R_386_GOTOFF 9 /* 32 bit offset to GOT */
+#define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */
+#define R_386_32PLT 11
+#define R_386_TLS_TPOFF 14 /* Offset in static TLS block */
+#define R_386_TLS_IE 15 /* Address of GOT entry for static TLS
+ block offset */
+#define R_386_TLS_GOTIE 16 /* GOT entry for static TLS block
+ offset */
+#define R_386_TLS_LE 17 /* Offset relative to static TLS
+ block */
+#define R_386_TLS_GD 18 /* Direct 32 bit for GNU version of
+ general dynamic thread local data */
+#define R_386_TLS_LDM 19 /* Direct 32 bit for GNU version of
+ local dynamic thread local data
+ in LE code */
+#define R_386_16 20
+#define R_386_PC16 21
+#define R_386_8 22
+#define R_386_PC8 23
+#define R_386_TLS_GD_32 24 /* Direct 32 bit for general dynamic
+ thread local data */
+#define R_386_TLS_GD_PUSH 25 /* Tag for pushl in GD TLS code */
+#define R_386_TLS_GD_CALL 26 /* Relocation for call to
+ __tls_get_addr() */
+#define R_386_TLS_GD_POP 27 /* Tag for popl in GD TLS code */
+#define R_386_TLS_LDM_32 28 /* Direct 32 bit for local dynamic
+ thread local data in LE code */
+#define R_386_TLS_LDM_PUSH 29 /* Tag for pushl in LDM TLS code */
+#define R_386_TLS_LDM_CALL 30 /* Relocation for call to
+ __tls_get_addr() in LDM code */
+#define R_386_TLS_LDM_POP 31 /* Tag for popl in LDM TLS code */
+#define R_386_TLS_LDO_32 32 /* Offset relative to TLS block */
+#define R_386_TLS_IE_32 33 /* GOT entry for negated static TLS
+ block offset */
+#define R_386_TLS_LE_32 34 /* Negated offset relative to static
+ TLS block */
+#define R_386_TLS_DTPMOD32 35 /* ID of module containing symbol */
+#define R_386_TLS_DTPOFF32 36 /* Offset in TLS block */
+#define R_386_TLS_TPOFF32 37 /* Negated offset in static TLS block */
+/* Keep this the last entry. */
+#define R_386_NUM 38
+
+/* SUN SPARC specific definitions. */
+
+/* Legal values for ST_TYPE subfield of st_info (symbol type). */
+
+#define STT_SPARC_REGISTER 13 /* Global register reserved to app. */
+
+/* Values for Elf64_Ehdr.e_flags. */
+
+#define EF_SPARCV9_MM 3
+#define EF_SPARCV9_TSO 0
+#define EF_SPARCV9_PSO 1
+#define EF_SPARCV9_RMO 2
+#define EF_SPARC_LEDATA 0x800000 /* little endian data */
+#define EF_SPARC_EXT_MASK 0xFFFF00
+#define EF_SPARC_32PLUS 0x000100 /* generic V8+ features */
+#define EF_SPARC_SUN_US1 0x000200 /* Sun UltraSPARC1 extensions */
+#define EF_SPARC_HAL_R1 0x000400 /* HAL R1 extensions */
+#define EF_SPARC_SUN_US3 0x000800 /* Sun UltraSPARCIII extensions */
+
+/* SPARC relocs. */
+
+#define R_SPARC_NONE 0 /* No reloc */
+#define R_SPARC_8 1 /* Direct 8 bit */
+#define R_SPARC_16 2 /* Direct 16 bit */
+#define R_SPARC_32 3 /* Direct 32 bit */
+#define R_SPARC_DISP8 4 /* PC relative 8 bit */
+#define R_SPARC_DISP16 5 /* PC relative 16 bit */
+#define R_SPARC_DISP32 6 /* PC relative 32 bit */
+#define R_SPARC_WDISP30 7 /* PC relative 30 bit shifted */
+#define R_SPARC_WDISP22 8 /* PC relative 22 bit shifted */
+#define R_SPARC_HI22 9 /* High 22 bit */
+#define R_SPARC_22 10 /* Direct 22 bit */
+#define R_SPARC_13 11 /* Direct 13 bit */
+#define R_SPARC_LO10 12 /* Truncated 10 bit */
+#define R_SPARC_GOT10 13 /* Truncated 10 bit GOT entry */
+#define R_SPARC_GOT13 14 /* 13 bit GOT entry */
+#define R_SPARC_GOT22 15 /* 22 bit GOT entry shifted */
+#define R_SPARC_PC10 16 /* PC relative 10 bit truncated */
+#define R_SPARC_PC22 17 /* PC relative 22 bit shifted */
+#define R_SPARC_WPLT30 18 /* 30 bit PC relative PLT address */
+#define R_SPARC_COPY 19 /* Copy symbol at runtime */
+#define R_SPARC_GLOB_DAT 20 /* Create GOT entry */
+#define R_SPARC_JMP_SLOT 21 /* Create PLT entry */
+#define R_SPARC_RELATIVE 22 /* Adjust by program base */
+#define R_SPARC_UA32 23 /* Direct 32 bit unaligned */
+
+/* Additional Sparc64 relocs. */
+
+#define R_SPARC_PLT32 24 /* Direct 32 bit ref to PLT entry */
+#define R_SPARC_HIPLT22 25 /* High 22 bit PLT entry */
+#define R_SPARC_LOPLT10 26 /* Truncated 10 bit PLT entry */
+#define R_SPARC_PCPLT32 27 /* PC rel 32 bit ref to PLT entry */
+#define R_SPARC_PCPLT22 28 /* PC rel high 22 bit PLT entry */
+#define R_SPARC_PCPLT10 29 /* PC rel trunc 10 bit PLT entry */
+#define R_SPARC_10 30 /* Direct 10 bit */
+#define R_SPARC_11 31 /* Direct 11 bit */
+#define R_SPARC_64 32 /* Direct 64 bit */
+#define R_SPARC_OLO10 33 /* 10bit with secondary 13bit addend */
+#define R_SPARC_HH22 34 /* Top 22 bits of direct 64 bit */
+#define R_SPARC_HM10 35 /* High middle 10 bits of ... */
+#define R_SPARC_LM22 36 /* Low middle 22 bits of ... */
+#define R_SPARC_PC_HH22 37 /* Top 22 bits of pc rel 64 bit */
+#define R_SPARC_PC_HM10 38 /* High middle 10 bit of ... */
+#define R_SPARC_PC_LM22 39 /* Low miggle 22 bits of ... */
+#define R_SPARC_WDISP16 40 /* PC relative 16 bit shifted */
+#define R_SPARC_WDISP19 41 /* PC relative 19 bit shifted */
+#define R_SPARC_7 43 /* Direct 7 bit */
+#define R_SPARC_5 44 /* Direct 5 bit */
+#define R_SPARC_6 45 /* Direct 6 bit */
+#define R_SPARC_DISP64 46 /* PC relative 64 bit */
+#define R_SPARC_PLT64 47 /* Direct 64 bit ref to PLT entry */
+#define R_SPARC_HIX22 48 /* High 22 bit complemented */
+#define R_SPARC_LOX10 49 /* Truncated 11 bit complemented */
+#define R_SPARC_H44 50 /* Direct high 12 of 44 bit */
+#define R_SPARC_M44 51 /* Direct mid 22 of 44 bit */
+#define R_SPARC_L44 52 /* Direct low 10 of 44 bit */
+#define R_SPARC_REGISTER 53 /* Global register usage */
+#define R_SPARC_UA64 54 /* Direct 64 bit unaligned */
+#define R_SPARC_UA16 55 /* Direct 16 bit unaligned */
+#define R_SPARC_TLS_GD_HI22 56
+#define R_SPARC_TLS_GD_LO10 57
+#define R_SPARC_TLS_GD_ADD 58
+#define R_SPARC_TLS_GD_CALL 59
+#define R_SPARC_TLS_LDM_HI22 60
+#define R_SPARC_TLS_LDM_LO10 61
+#define R_SPARC_TLS_LDM_ADD 62
+#define R_SPARC_TLS_LDM_CALL 63
+#define R_SPARC_TLS_LDO_HIX22 64
+#define R_SPARC_TLS_LDO_LOX10 65
+#define R_SPARC_TLS_LDO_ADD 66
+#define R_SPARC_TLS_IE_HI22 67
+#define R_SPARC_TLS_IE_LO10 68
+#define R_SPARC_TLS_IE_LD 69
+#define R_SPARC_TLS_IE_LDX 70
+#define R_SPARC_TLS_IE_ADD 71
+#define R_SPARC_TLS_LE_HIX22 72
+#define R_SPARC_TLS_LE_LOX10 73
+#define R_SPARC_TLS_DTPMOD32 74
+#define R_SPARC_TLS_DTPMOD64 75
+#define R_SPARC_TLS_DTPOFF32 76
+#define R_SPARC_TLS_DTPOFF64 77
+#define R_SPARC_TLS_TPOFF32 78
+#define R_SPARC_TLS_TPOFF64 79
+/* Keep this the last entry. */
+#define R_SPARC_NUM 80
+
+/* For Sparc64, legal values for d_tag of Elf64_Dyn. */
+
+#define DT_SPARC_REGISTER 0x70000001
+#define DT_SPARC_NUM 2
+
+/* Bits present in AT_HWCAP, primarily for Sparc32. */
+
+#define HWCAP_SPARC_FLUSH 1 /* The cpu supports flush insn. */
+#define HWCAP_SPARC_STBAR 2
+#define HWCAP_SPARC_SWAP 4
+#define HWCAP_SPARC_MULDIV 8
+#define HWCAP_SPARC_V9 16 /* The cpu is v9, so v8plus is ok. */
+#define HWCAP_SPARC_ULTRA3 32
+
+/* MIPS R3000 specific definitions. */
+
+/* Legal values for e_flags field of Elf32_Ehdr. */
+
+#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used */
+#define EF_MIPS_PIC 2 /* Contains PIC code */
+#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence */
+#define EF_MIPS_XGOT 8
+#define EF_MIPS_64BIT_WHIRL 16
+#define EF_MIPS_ABI2 32
+#define EF_MIPS_ABI_ON32 64
+#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level */
+
+/* Legal values for MIPS architecture level. */
+
+#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */
+#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */
+#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */
+#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */
+#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */
+#define EF_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */
+#define EF_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */
+
+/* The following are non-official names and should not be used. */
+
+#define E_MIPS_ARCH_1 0x00000000 /* -mips1 code. */
+#define E_MIPS_ARCH_2 0x10000000 /* -mips2 code. */
+#define E_MIPS_ARCH_3 0x20000000 /* -mips3 code. */
+#define E_MIPS_ARCH_4 0x30000000 /* -mips4 code. */
+#define E_MIPS_ARCH_5 0x40000000 /* -mips5 code. */
+#define E_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */
+#define E_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */
+
+/* Special section indices. */
+
+#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols */
+#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */
+#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */
+#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols */
+#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols */
+
+/* Legal values for sh_type field of Elf32_Shdr. */
+
+#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link */
+#define SHT_MIPS_MSYM 0x70000001
+#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols */
+#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes */
+#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */
+#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging information*/
+#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information */
+#define SHT_MIPS_PACKAGE 0x70000007
+#define SHT_MIPS_PACKSYM 0x70000008
+#define SHT_MIPS_RELD 0x70000009
+#define SHT_MIPS_IFACE 0x7000000b
+#define SHT_MIPS_CONTENT 0x7000000c
+#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */
+#define SHT_MIPS_SHDR 0x70000010
+#define SHT_MIPS_FDESC 0x70000011
+#define SHT_MIPS_EXTSYM 0x70000012
+#define SHT_MIPS_DENSE 0x70000013
+#define SHT_MIPS_PDESC 0x70000014
+#define SHT_MIPS_LOCSYM 0x70000015
+#define SHT_MIPS_AUXSYM 0x70000016
+#define SHT_MIPS_OPTSYM 0x70000017
+#define SHT_MIPS_LOCSTR 0x70000018
+#define SHT_MIPS_LINE 0x70000019
+#define SHT_MIPS_RFDESC 0x7000001a
+#define SHT_MIPS_DELTASYM 0x7000001b
+#define SHT_MIPS_DELTAINST 0x7000001c
+#define SHT_MIPS_DELTACLASS 0x7000001d
+#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */
+#define SHT_MIPS_DELTADECL 0x7000001f
+#define SHT_MIPS_SYMBOL_LIB 0x70000020
+#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */
+#define SHT_MIPS_TRANSLATE 0x70000022
+#define SHT_MIPS_PIXIE 0x70000023
+#define SHT_MIPS_XLATE 0x70000024
+#define SHT_MIPS_XLATE_DEBUG 0x70000025
+#define SHT_MIPS_WHIRL 0x70000026
+#define SHT_MIPS_EH_REGION 0x70000027
+#define SHT_MIPS_XLATE_OLD 0x70000028
+#define SHT_MIPS_PDR_EXCEPTION 0x70000029
+
+/* Legal values for sh_flags field of Elf32_Shdr. */
+
+#define SHF_MIPS_GPREL 0x10000000 /* Must be part of global data area */
+#define SHF_MIPS_MERGE 0x20000000
+#define SHF_MIPS_ADDR 0x40000000
+#define SHF_MIPS_STRINGS 0x80000000
+#define SHF_MIPS_NOSTRIP 0x08000000
+#define SHF_MIPS_LOCAL 0x04000000
+#define SHF_MIPS_NAMES 0x02000000
+#define SHF_MIPS_NODUPE 0x01000000
+
+
+/* Symbol tables. */
+
+/* MIPS specific values for `st_other'. */
+#define STO_MIPS_DEFAULT 0x0
+#define STO_MIPS_INTERNAL 0x1
+#define STO_MIPS_HIDDEN 0x2
+#define STO_MIPS_PROTECTED 0x3
+#define STO_MIPS_SC_ALIGN_UNUSED 0xff
+
+/* MIPS specific values for `st_info'. */
+#define STB_MIPS_SPLIT_COMMON 13
+
+/* Entries found in sections of type SHT_MIPS_GPTAB. */
+
+typedef union
+{
+ struct
+ {
+ Elf32_Word gt_current_g_value; /* -G value used for compilation */
+ Elf32_Word gt_unused; /* Not used */
+ } gt_header; /* First entry in section */
+ struct
+ {
+ Elf32_Word gt_g_value; /* If this value were used for -G */
+ Elf32_Word gt_bytes; /* This many bytes would be used */
+ } gt_entry; /* Subsequent entries in section */
+} Elf32_gptab;
+
+/* Entry found in sections of type SHT_MIPS_REGINFO. */
+
+typedef struct
+{
+ Elf32_Word ri_gprmask; /* General registers used */
+ Elf32_Word ri_cprmask[4]; /* Coprocessor registers used */
+ Elf32_Sword ri_gp_value; /* $gp register value */
+} Elf32_RegInfo;
+
+/* Entries found in sections of type SHT_MIPS_OPTIONS. */
+
+typedef struct
+{
+ unsigned char kind; /* Determines interpretation of the
+ variable part of descriptor. */
+ unsigned char size; /* Size of descriptor, including header. */
+ Elf32_Section section; /* Section header index of section affected,
+ 0 for global options. */
+ Elf32_Word info; /* Kind-specific information. */
+} Elf_Options;
+
+/* Values for `kind' field in Elf_Options. */
+
+#define ODK_NULL 0 /* Undefined. */
+#define ODK_REGINFO 1 /* Register usage information. */
+#define ODK_EXCEPTIONS 2 /* Exception processing options. */
+#define ODK_PAD 3 /* Section padding options. */
+#define ODK_HWPATCH 4 /* Hardware workarounds performed */
+#define ODK_FILL 5 /* record the fill value used by the linker. */
+#define ODK_TAGS 6 /* reserve space for desktop tools to write. */
+#define ODK_HWAND 7 /* HW workarounds. 'AND' bits when merging. */
+#define ODK_HWOR 8 /* HW workarounds. 'OR' bits when merging. */
+
+/* Values for `info' in Elf_Options for ODK_EXCEPTIONS entries. */
+
+#define OEX_FPU_MIN 0x1f /* FPE's which MUST be enabled. */
+#define OEX_FPU_MAX 0x1f00 /* FPE's which MAY be enabled. */
+#define OEX_PAGE0 0x10000 /* page zero must be mapped. */
+#define OEX_SMM 0x20000 /* Force sequential memory mode? */
+#define OEX_FPDBUG 0x40000 /* Force floating point debug mode? */
+#define OEX_PRECISEFP OEX_FPDBUG
+#define OEX_DISMISS 0x80000 /* Dismiss invalid address faults? */
+
+#define OEX_FPU_INVAL 0x10
+#define OEX_FPU_DIV0 0x08
+#define OEX_FPU_OFLO 0x04
+#define OEX_FPU_UFLO 0x02
+#define OEX_FPU_INEX 0x01
+
+/* Masks for `info' in Elf_Options for an ODK_HWPATCH entry. */
+
+#define OHW_R4KEOP 0x1 /* R4000 end-of-page patch. */
+#define OHW_R8KPFETCH 0x2 /* may need R8000 prefetch patch. */
+#define OHW_R5KEOP 0x4 /* R5000 end-of-page patch. */
+#define OHW_R5KCVTL 0x8 /* R5000 cvt.[ds].l bug. clean=1. */
+
+#define OPAD_PREFIX 0x1
+#define OPAD_POSTFIX 0x2
+#define OPAD_SYMBOL 0x4
+
+/* Entry found in `.options' section. */
+
+typedef struct
+{
+ Elf32_Word hwp_flags1; /* Extra flags. */
+ Elf32_Word hwp_flags2; /* Extra flags. */
+} Elf_Options_Hw;
+
+/* Masks for `info' in ElfOptions for ODK_HWAND and ODK_HWOR entries. */
+
+#define OHWA0_R4KEOP_CHECKED 0x00000001
+#define OHWA1_R4KEOP_CLEAN 0x00000002
+
+/* MIPS relocs. */
+
+#define R_MIPS_NONE 0 /* No reloc */
+#define R_MIPS_16 1 /* Direct 16 bit */
+#define R_MIPS_32 2 /* Direct 32 bit */
+#define R_MIPS_REL32 3 /* PC relative 32 bit */
+#define R_MIPS_26 4 /* Direct 26 bit shifted */
+#define R_MIPS_HI16 5 /* High 16 bit */
+#define R_MIPS_LO16 6 /* Low 16 bit */
+#define R_MIPS_GPREL16 7 /* GP relative 16 bit */
+#define R_MIPS_LITERAL 8 /* 16 bit literal entry */
+#define R_MIPS_GOT16 9 /* 16 bit GOT entry */
+#define R_MIPS_PC16 10 /* PC relative 16 bit */
+#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */
+#define R_MIPS_GPREL32 12 /* GP relative 32 bit */
+
+#define R_MIPS_SHIFT5 16
+#define R_MIPS_SHIFT6 17
+#define R_MIPS_64 18
+#define R_MIPS_GOT_DISP 19
+#define R_MIPS_GOT_PAGE 20
+#define R_MIPS_GOT_OFST 21
+#define R_MIPS_GOT_HI16 22
+#define R_MIPS_GOT_LO16 23
+#define R_MIPS_SUB 24
+#define R_MIPS_INSERT_A 25
+#define R_MIPS_INSERT_B 26
+#define R_MIPS_DELETE 27
+#define R_MIPS_HIGHER 28
+#define R_MIPS_HIGHEST 29
+#define R_MIPS_CALL_HI16 30
+#define R_MIPS_CALL_LO16 31
+#define R_MIPS_SCN_DISP 32
+#define R_MIPS_REL16 33
+#define R_MIPS_ADD_IMMEDIATE 34
+#define R_MIPS_PJUMP 35
+#define R_MIPS_RELGOT 36
+#define R_MIPS_JALR 37
+/* Keep this the last entry. */
+#define R_MIPS_NUM 38
+
+/* Legal values for p_type field of Elf32_Phdr. */
+
+#define PT_MIPS_REGINFO 0x70000000 /* Register usage information */
+#define PT_MIPS_RTPROC 0x70000001 /* Runtime procedure table. */
+#define PT_MIPS_OPTIONS 0x70000002
+
+/* Special program header types. */
+
+#define PF_MIPS_LOCAL 0x10000000
+
+/* Legal values for d_tag field of Elf32_Dyn. */
+
+#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime linker interface version */
+#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */
+#define DT_MIPS_ICHECKSUM 0x70000003 /* Checksum */
+#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */
+#define DT_MIPS_FLAGS 0x70000005 /* Flags */
+#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Base address */
+#define DT_MIPS_MSYM 0x70000007
+#define DT_MIPS_CONFLICT 0x70000008 /* Address of CONFLICT section */
+#define DT_MIPS_LIBLIST 0x70000009 /* Address of LIBLIST section */
+#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local GOT entries */
+#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of CONFLICT entries */
+#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of LIBLIST entries */
+#define DT_MIPS_SYMTABNO 0x70000011 /* Number of DYNSYM entries */
+#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */
+#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in DYNSYM */
+#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */
+#define DT_MIPS_RLD_MAP 0x70000016 /* Address of run time loader map. */
+#define DT_MIPS_DELTA_CLASS 0x70000017 /* Delta C++ class definition. */
+#define DT_MIPS_DELTA_CLASS_NO 0x70000018 /* Number of entries in
+ DT_MIPS_DELTA_CLASS. */
+#define DT_MIPS_DELTA_INSTANCE 0x70000019 /* Delta C++ class instances. */
+#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a /* Number of entries in
+ DT_MIPS_DELTA_INSTANCE. */
+#define DT_MIPS_DELTA_RELOC 0x7000001b /* Delta relocations. */
+#define DT_MIPS_DELTA_RELOC_NO 0x7000001c /* Number of entries in
+ DT_MIPS_DELTA_RELOC. */
+#define DT_MIPS_DELTA_SYM 0x7000001d /* Delta symbols that Delta
+ relocations refer to. */
+#define DT_MIPS_DELTA_SYM_NO 0x7000001e /* Number of entries in
+ DT_MIPS_DELTA_SYM. */
+#define DT_MIPS_DELTA_CLASSSYM 0x70000020 /* Delta symbols that hold the
+ class declaration. */
+#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 /* Number of entries in
+ DT_MIPS_DELTA_CLASSSYM. */
+#define DT_MIPS_CXX_FLAGS 0x70000022 /* Flags indicating for C++ flavor. */
+#define DT_MIPS_PIXIE_INIT 0x70000023
+#define DT_MIPS_SYMBOL_LIB 0x70000024
+#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025
+#define DT_MIPS_LOCAL_GOTIDX 0x70000026
+#define DT_MIPS_HIDDEN_GOTIDX 0x70000027
+#define DT_MIPS_PROTECTED_GOTIDX 0x70000028
+#define DT_MIPS_OPTIONS 0x70000029 /* Address of .options. */
+#define DT_MIPS_INTERFACE 0x7000002a /* Address of .interface. */
+#define DT_MIPS_DYNSTR_ALIGN 0x7000002b
+#define DT_MIPS_INTERFACE_SIZE 0x7000002c /* Size of the .interface section. */
+#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d /* Address of rld_text_rsolve
+ function stored in GOT. */
+#define DT_MIPS_PERF_SUFFIX 0x7000002e /* Default suffix of dso to be added
+ by rld on dlopen() calls. */
+#define DT_MIPS_COMPACT_SIZE 0x7000002f /* (O32)Size of compact rel section. */
+#define DT_MIPS_GP_VALUE 0x70000030 /* GP value for aux GOTs. */
+#define DT_MIPS_AUX_DYNAMIC 0x70000031 /* Address of aux .dynamic. */
+#define DT_MIPS_NUM 0x32
+
+/* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry. */
+
+#define RHF_NONE 0 /* No flags */
+#define RHF_QUICKSTART (1 << 0) /* Use quickstart */
+#define RHF_NOTPOT (1 << 1) /* Hash size not power of 2 */
+#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) /* Ignore LD_LIBRARY_PATH */
+#define RHF_NO_MOVE (1 << 3)
+#define RHF_SGI_ONLY (1 << 4)
+#define RHF_GUARANTEE_INIT (1 << 5)
+#define RHF_DELTA_C_PLUS_PLUS (1 << 6)
+#define RHF_GUARANTEE_START_INIT (1 << 7)
+#define RHF_PIXIE (1 << 8)
+#define RHF_DEFAULT_DELAY_LOAD (1 << 9)
+#define RHF_REQUICKSTART (1 << 10)
+#define RHF_REQUICKSTARTED (1 << 11)
+#define RHF_CORD (1 << 12)
+#define RHF_NO_UNRES_UNDEF (1 << 13)
+#define RHF_RLD_ORDER_SAFE (1 << 14)
+
+/* Entries found in sections of type SHT_MIPS_LIBLIST. */
+
+typedef struct
+{
+ Elf32_Word l_name; /* Name (string table index) */
+ Elf32_Word l_time_stamp; /* Timestamp */
+ Elf32_Word l_checksum; /* Checksum */
+ Elf32_Word l_version; /* Interface version */
+ Elf32_Word l_flags; /* Flags */
+} Elf32_Lib;
+
+typedef struct
+{
+ Elf64_Word l_name; /* Name (string table index) */
+ Elf64_Word l_time_stamp; /* Timestamp */
+ Elf64_Word l_checksum; /* Checksum */
+ Elf64_Word l_version; /* Interface version */
+ Elf64_Word l_flags; /* Flags */
+} Elf64_Lib;
+
+
+/* Legal values for l_flags. */
+
+#define LL_NONE 0
+#define LL_EXACT_MATCH (1 << 0) /* Require exact match */
+#define LL_IGNORE_INT_VER (1 << 1) /* Ignore interface version */
+#define LL_REQUIRE_MINOR (1 << 2)
+#define LL_EXPORTS (1 << 3)
+#define LL_DELAY_LOAD (1 << 4)
+#define LL_DELTA (1 << 5)
+
+/* Entries found in sections of type SHT_MIPS_CONFLICT. */
+
+typedef Elf32_Addr Elf32_Conflict;
+
+
+/* HPPA specific definitions. */
+
+/* Legal values for e_flags field of Elf32_Ehdr. */
+
+#define EF_PARISC_TRAPNIL 0x00010000 /* Trap nil pointer dereference. */
+#define EF_PARISC_EXT 0x00020000 /* Program uses arch. extensions. */
+#define EF_PARISC_LSB 0x00040000 /* Program expects little endian. */
+#define EF_PARISC_WIDE 0x00080000 /* Program expects wide mode. */
+#define EF_PARISC_NO_KABP 0x00100000 /* No kernel assisted branch
+ prediction. */
+#define EF_PARISC_LAZYSWAP 0x00400000 /* Allow lazy swapping. */
+#define EF_PARISC_ARCH 0x0000ffff /* Architecture version. */
+
+/* Defined values for `e_flags & EF_PARISC_ARCH' are: */
+
+#define EFA_PARISC_1_0 0x020b /* PA-RISC 1.0 big-endian. */
+#define EFA_PARISC_1_1 0x0210 /* PA-RISC 1.1 big-endian. */
+#define EFA_PARISC_2_0 0x0214 /* PA-RISC 2.0 big-endian. */
+
+/* Additional section indeces. */
+
+#define SHN_PARISC_ANSI_COMMON 0xff00 /* Section for tenatively declared
+ symbols in ANSI C. */
+#define SHN_PARISC_HUGE_COMMON 0xff01 /* Common blocks in huge model. */
+
+/* Legal values for sh_type field of Elf32_Shdr. */
+
+#define SHT_PARISC_EXT 0x70000000 /* Contains product specific ext. */
+#define SHT_PARISC_UNWIND 0x70000001 /* Unwind information. */
+#define SHT_PARISC_DOC 0x70000002 /* Debug info for optimized code. */
+
+/* Legal values for sh_flags field of Elf32_Shdr. */
+
+#define SHF_PARISC_SHORT 0x20000000 /* Section with short addressing. */
+#define SHF_PARISC_HUGE 0x40000000 /* Section far from gp. */
+#define SHF_PARISC_SBP 0x80000000 /* Static branch prediction code. */
+
+/* Legal values for ST_TYPE subfield of st_info (symbol type). */
+
+#define STT_PARISC_MILLICODE 13 /* Millicode function entry point. */
+
+#define STT_HP_OPAQUE (STT_LOOS + 0x1)
+#define STT_HP_STUB (STT_LOOS + 0x2)
+
+/* HPPA relocs. */
+
+#define R_PARISC_NONE 0 /* No reloc. */
+#define R_PARISC_DIR32 1 /* Direct 32-bit reference. */
+#define R_PARISC_DIR21L 2 /* Left 21 bits of eff. address. */
+#define R_PARISC_DIR17R 3 /* Right 17 bits of eff. address. */
+#define R_PARISC_DIR17F 4 /* 17 bits of eff. address. */
+#define R_PARISC_DIR14R 6 /* Right 14 bits of eff. address. */
+#define R_PARISC_PCREL32 9 /* 32-bit rel. address. */
+#define R_PARISC_PCREL21L 10 /* Left 21 bits of rel. address. */
+#define R_PARISC_PCREL17R 11 /* Right 17 bits of rel. address. */
+#define R_PARISC_PCREL17F 12 /* 17 bits of rel. address. */
+#define R_PARISC_PCREL14R 14 /* Right 14 bits of rel. address. */
+#define R_PARISC_DPREL21L 18 /* Left 21 bits of rel. address. */
+#define R_PARISC_DPREL14R 22 /* Right 14 bits of rel. address. */
+#define R_PARISC_GPREL21L 26 /* GP-relative, left 21 bits. */
+#define R_PARISC_GPREL14R 30 /* GP-relative, right 14 bits. */
+#define R_PARISC_LTOFF21L 34 /* LT-relative, left 21 bits. */
+#define R_PARISC_LTOFF14R 38 /* LT-relative, right 14 bits. */
+#define R_PARISC_SECREL32 41 /* 32 bits section rel. address. */
+#define R_PARISC_SEGBASE 48 /* No relocation, set segment base. */
+#define R_PARISC_SEGREL32 49 /* 32 bits segment rel. address. */
+#define R_PARISC_PLTOFF21L 50 /* PLT rel. address, left 21 bits. */
+#define R_PARISC_PLTOFF14R 54 /* PLT rel. address, right 14 bits. */
+#define R_PARISC_LTOFF_FPTR32 57 /* 32 bits LT-rel. function pointer. */
+#define R_PARISC_LTOFF_FPTR21L 58 /* LT-rel. fct ptr, left 21 bits. */
+#define R_PARISC_LTOFF_FPTR14R 62 /* LT-rel. fct ptr, right 14 bits. */
+#define R_PARISC_FPTR64 64 /* 64 bits function address. */
+#define R_PARISC_PLABEL32 65 /* 32 bits function address. */
+#define R_PARISC_PCREL64 72 /* 64 bits PC-rel. address. */
+#define R_PARISC_PCREL22F 74 /* 22 bits PC-rel. address. */
+#define R_PARISC_PCREL14WR 75 /* PC-rel. address, right 14 bits. */
+#define R_PARISC_PCREL14DR 76 /* PC rel. address, right 14 bits. */
+#define R_PARISC_PCREL16F 77 /* 16 bits PC-rel. address. */
+#define R_PARISC_PCREL16WF 78 /* 16 bits PC-rel. address. */
+#define R_PARISC_PCREL16DF 79 /* 16 bits PC-rel. address. */
+#define R_PARISC_DIR64 80 /* 64 bits of eff. address. */
+#define R_PARISC_DIR14WR 83 /* 14 bits of eff. address. */
+#define R_PARISC_DIR14DR 84 /* 14 bits of eff. address. */
+#define R_PARISC_DIR16F 85 /* 16 bits of eff. address. */
+#define R_PARISC_DIR16WF 86 /* 16 bits of eff. address. */
+#define R_PARISC_DIR16DF 87 /* 16 bits of eff. address. */
+#define R_PARISC_GPREL64 88 /* 64 bits of GP-rel. address. */
+#define R_PARISC_GPREL14WR 91 /* GP-rel. address, right 14 bits. */
+#define R_PARISC_GPREL14DR 92 /* GP-rel. address, right 14 bits. */
+#define R_PARISC_GPREL16F 93 /* 16 bits GP-rel. address. */
+#define R_PARISC_GPREL16WF 94 /* 16 bits GP-rel. address. */
+#define R_PARISC_GPREL16DF 95 /* 16 bits GP-rel. address. */
+#define R_PARISC_LTOFF64 96 /* 64 bits LT-rel. address. */
+#define R_PARISC_LTOFF14WR 99 /* LT-rel. address, right 14 bits. */
+#define R_PARISC_LTOFF14DR 100 /* LT-rel. address, right 14 bits. */
+#define R_PARISC_LTOFF16F 101 /* 16 bits LT-rel. address. */
+#define R_PARISC_LTOFF16WF 102 /* 16 bits LT-rel. address. */
+#define R_PARISC_LTOFF16DF 103 /* 16 bits LT-rel. address. */
+#define R_PARISC_SECREL64 104 /* 64 bits section rel. address. */
+#define R_PARISC_SEGREL64 112 /* 64 bits segment rel. address. */
+#define R_PARISC_PLTOFF14WR 115 /* PLT-rel. address, right 14 bits. */
+#define R_PARISC_PLTOFF14DR 116 /* PLT-rel. address, right 14 bits. */
+#define R_PARISC_PLTOFF16F 117 /* 16 bits LT-rel. address. */
+#define R_PARISC_PLTOFF16WF 118 /* 16 bits PLT-rel. address. */
+#define R_PARISC_PLTOFF16DF 119 /* 16 bits PLT-rel. address. */
+#define R_PARISC_LTOFF_FPTR64 120 /* 64 bits LT-rel. function ptr. */
+#define R_PARISC_LTOFF_FPTR14WR 123 /* LT-rel. fct. ptr., right 14 bits. */
+#define R_PARISC_LTOFF_FPTR14DR 124 /* LT-rel. fct. ptr., right 14 bits. */
+#define R_PARISC_LTOFF_FPTR16F 125 /* 16 bits LT-rel. function ptr. */
+#define R_PARISC_LTOFF_FPTR16WF 126 /* 16 bits LT-rel. function ptr. */
+#define R_PARISC_LTOFF_FPTR16DF 127 /* 16 bits LT-rel. function ptr. */
+#define R_PARISC_LORESERVE 128
+#define R_PARISC_COPY 128 /* Copy relocation. */
+#define R_PARISC_IPLT 129 /* Dynamic reloc, imported PLT */
+#define R_PARISC_EPLT 130 /* Dynamic reloc, exported PLT */
+#define R_PARISC_TPREL32 153 /* 32 bits TP-rel. address. */
+#define R_PARISC_TPREL21L 154 /* TP-rel. address, left 21 bits. */
+#define R_PARISC_TPREL14R 158 /* TP-rel. address, right 14 bits. */
+#define R_PARISC_LTOFF_TP21L 162 /* LT-TP-rel. address, left 21 bits. */
+#define R_PARISC_LTOFF_TP14R 166 /* LT-TP-rel. address, right 14 bits.*/
+#define R_PARISC_LTOFF_TP14F 167 /* 14 bits LT-TP-rel. address. */
+#define R_PARISC_TPREL64 216 /* 64 bits TP-rel. address. */
+#define R_PARISC_TPREL14WR 219 /* TP-rel. address, right 14 bits. */
+#define R_PARISC_TPREL14DR 220 /* TP-rel. address, right 14 bits. */
+#define R_PARISC_TPREL16F 221 /* 16 bits TP-rel. address. */
+#define R_PARISC_TPREL16WF 222 /* 16 bits TP-rel. address. */
+#define R_PARISC_TPREL16DF 223 /* 16 bits TP-rel. address. */
+#define R_PARISC_LTOFF_TP64 224 /* 64 bits LT-TP-rel. address. */
+#define R_PARISC_LTOFF_TP14WR 227 /* LT-TP-rel. address, right 14 bits.*/
+#define R_PARISC_LTOFF_TP14DR 228 /* LT-TP-rel. address, right 14 bits.*/
+#define R_PARISC_LTOFF_TP16F 229 /* 16 bits LT-TP-rel. address. */
+#define R_PARISC_LTOFF_TP16WF 230 /* 16 bits LT-TP-rel. address. */
+#define R_PARISC_LTOFF_TP16DF 231 /* 16 bits LT-TP-rel. address. */
+#define R_PARISC_HIRESERVE 255
+
+/* Legal values for p_type field of Elf32_Phdr/Elf64_Phdr. */
+
+#define PT_HP_TLS (PT_LOOS + 0x0)
+#define PT_HP_CORE_NONE (PT_LOOS + 0x1)
+#define PT_HP_CORE_VERSION (PT_LOOS + 0x2)
+#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3)
+#define PT_HP_CORE_COMM (PT_LOOS + 0x4)
+#define PT_HP_CORE_PROC (PT_LOOS + 0x5)
+#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6)
+#define PT_HP_CORE_STACK (PT_LOOS + 0x7)
+#define PT_HP_CORE_SHM (PT_LOOS + 0x8)
+#define PT_HP_CORE_MMF (PT_LOOS + 0x9)
+#define PT_HP_PARALLEL (PT_LOOS + 0x10)
+#define PT_HP_FASTBIND (PT_LOOS + 0x11)
+#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12)
+#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13)
+#define PT_HP_STACK (PT_LOOS + 0x14)
+
+#define PT_PARISC_ARCHEXT 0x70000000
+#define PT_PARISC_UNWIND 0x70000001
+
+/* Legal values for p_flags field of Elf32_Phdr/Elf64_Phdr. */
+
+#define PF_PARISC_SBP 0x08000000
+
+#define PF_HP_PAGE_SIZE 0x00100000
+#define PF_HP_FAR_SHARED 0x00200000
+#define PF_HP_NEAR_SHARED 0x00400000
+#define PF_HP_CODE 0x01000000
+#define PF_HP_MODIFY 0x02000000
+#define PF_HP_LAZYSWAP 0x04000000
+#define PF_HP_SBP 0x08000000
+
+
+/* Alpha specific definitions. */
+
+/* Legal values for e_flags field of Elf64_Ehdr. */
+
+#define EF_ALPHA_32BIT 1 /* All addresses must be < 2GB. */
+#define EF_ALPHA_CANRELAX 2 /* Relocations for relaxing exist. */
+
+/* Legal values for sh_type field of Elf64_Shdr. */
+
+/* These two are primerily concerned with ECOFF debugging info. */
+#define SHT_ALPHA_DEBUG 0x70000001
+#define SHT_ALPHA_REGINFO 0x70000002
+
+/* Legal values for sh_flags field of Elf64_Shdr. */
+
+#define SHF_ALPHA_GPREL 0x10000000
+
+/* Legal values for st_other field of Elf64_Sym. */
+#define STO_ALPHA_NOPV 0x80 /* No PV required. */
+#define STO_ALPHA_STD_GPLOAD 0x88 /* PV only used for initial ldgp. */
+
+/* Alpha relocs. */
+
+#define R_ALPHA_NONE 0 /* No reloc */
+#define R_ALPHA_REFLONG 1 /* Direct 32 bit */
+#define R_ALPHA_REFQUAD 2 /* Direct 64 bit */
+#define R_ALPHA_GPREL32 3 /* GP relative 32 bit */
+#define R_ALPHA_LITERAL 4 /* GP relative 16 bit w/optimization */
+#define R_ALPHA_LITUSE 5 /* Optimization hint for LITERAL */
+#define R_ALPHA_GPDISP 6 /* Add displacement to GP */
+#define R_ALPHA_BRADDR 7 /* PC+4 relative 23 bit shifted */
+#define R_ALPHA_HINT 8 /* PC+4 relative 16 bit shifted */
+#define R_ALPHA_SREL16 9 /* PC relative 16 bit */
+#define R_ALPHA_SREL32 10 /* PC relative 32 bit */
+#define R_ALPHA_SREL64 11 /* PC relative 64 bit */
+#define R_ALPHA_GPRELHIGH 17 /* GP relative 32 bit, high 16 bits */
+#define R_ALPHA_GPRELLOW 18 /* GP relative 32 bit, low 16 bits */
+#define R_ALPHA_GPREL16 19 /* GP relative 16 bit */
+#define R_ALPHA_COPY 24 /* Copy symbol at runtime */
+#define R_ALPHA_GLOB_DAT 25 /* Create GOT entry */
+#define R_ALPHA_JMP_SLOT 26 /* Create PLT entry */
+#define R_ALPHA_RELATIVE 27 /* Adjust by program base */
+#define R_ALPHA_TLS_GD_HI 28
+#define R_ALPHA_TLSGD 29
+#define R_ALPHA_TLS_LDM 30
+#define R_ALPHA_DTPMOD64 31
+#define R_ALPHA_GOTDTPREL 32
+#define R_ALPHA_DTPREL64 33
+#define R_ALPHA_DTPRELHI 34
+#define R_ALPHA_DTPRELLO 35
+#define R_ALPHA_DTPREL16 36
+#define R_ALPHA_GOTTPREL 37
+#define R_ALPHA_TPREL64 38
+#define R_ALPHA_TPRELHI 39
+#define R_ALPHA_TPRELLO 40
+#define R_ALPHA_TPREL16 41
+/* Keep this the last entry. */
+#define R_ALPHA_NUM 46
+
+/* Magic values of the LITUSE relocation addend. */
+#define LITUSE_ALPHA_ADDR 0
+#define LITUSE_ALPHA_BASE 1
+#define LITUSE_ALPHA_BYTOFF 2
+#define LITUSE_ALPHA_JSR 3
+#define LITUSE_ALPHA_TLS_GD 4
+#define LITUSE_ALPHA_TLS_LDM 5
+
+
+/* PowerPC specific declarations */
+
+/* Values for Elf32/64_Ehdr.e_flags. */
+#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */
+
+/* Cygnus local bits below */
+#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/
+#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib
+ flag */
+
+/* PowerPC relocations defined by the ABIs */
+#define R_PPC_NONE 0
+#define R_PPC_ADDR32 1 /* 32bit absolute address */
+#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */
+#define R_PPC_ADDR16 3 /* 16bit absolute address */
+#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */
+#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */
+#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */
+#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */
+#define R_PPC_ADDR14_BRTAKEN 8
+#define R_PPC_ADDR14_BRNTAKEN 9
+#define R_PPC_REL24 10 /* PC relative 26 bit */
+#define R_PPC_REL14 11 /* PC relative 16 bit */
+#define R_PPC_REL14_BRTAKEN 12
+#define R_PPC_REL14_BRNTAKEN 13
+#define R_PPC_GOT16 14
+#define R_PPC_GOT16_LO 15
+#define R_PPC_GOT16_HI 16
+#define R_PPC_GOT16_HA 17
+#define R_PPC_PLTREL24 18
+#define R_PPC_COPY 19
+#define R_PPC_GLOB_DAT 20
+#define R_PPC_JMP_SLOT 21
+#define R_PPC_RELATIVE 22
+#define R_PPC_LOCAL24PC 23
+#define R_PPC_UADDR32 24
+#define R_PPC_UADDR16 25
+#define R_PPC_REL32 26
+#define R_PPC_PLT32 27
+#define R_PPC_PLTREL32 28
+#define R_PPC_PLT16_LO 29
+#define R_PPC_PLT16_HI 30
+#define R_PPC_PLT16_HA 31
+#define R_PPC_SDAREL16 32
+#define R_PPC_SECTOFF 33
+#define R_PPC_SECTOFF_LO 34
+#define R_PPC_SECTOFF_HI 35
+#define R_PPC_SECTOFF_HA 36
+
+/* PowerPC relocations defined for the TLS access ABI. */
+#define R_PPC_TLS 67 /* none (sym+add)@tls */
+#define R_PPC_DTPMOD32 68 /* word32 (sym+add)@dtpmod */
+#define R_PPC_TPREL16 69 /* half16* (sym+add)@tprel */
+#define R_PPC_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */
+#define R_PPC_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */
+#define R_PPC_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */
+#define R_PPC_TPREL32 73 /* word32 (sym+add)@tprel */
+#define R_PPC_DTPREL16 74 /* half16* (sym+add)@dtprel */
+#define R_PPC_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */
+#define R_PPC_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */
+#define R_PPC_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */
+#define R_PPC_DTPREL32 78 /* word32 (sym+add)@dtprel */
+#define R_PPC_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */
+#define R_PPC_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */
+#define R_PPC_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */
+#define R_PPC_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */
+#define R_PPC_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */
+#define R_PPC_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */
+#define R_PPC_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */
+#define R_PPC_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */
+#define R_PPC_GOT_TPREL16 87 /* half16* (sym+add)@got@tprel */
+#define R_PPC_GOT_TPREL16_LO 88 /* half16 (sym+add)@got@tprel@l */
+#define R_PPC_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */
+#define R_PPC_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */
+#define R_PPC_GOT_DTPREL16 91 /* half16* (sym+add)@got@dtprel */
+#define R_PPC_GOT_DTPREL16_LO 92 /* half16* (sym+add)@got@dtprel@l */
+#define R_PPC_GOT_DTPREL16_HI 93 /* half16* (sym+add)@got@dtprel@h */
+#define R_PPC_GOT_DTPREL16_HA 94 /* half16* (sym+add)@got@dtprel@ha */
+
+/* Keep this the last entry. */
+#define R_PPC_NUM 95
+
+/* The remaining relocs are from the Embedded ELF ABI, and are not
+ in the SVR4 ELF ABI. */
+#define R_PPC_EMB_NADDR32 101
+#define R_PPC_EMB_NADDR16 102
+#define R_PPC_EMB_NADDR16_LO 103
+#define R_PPC_EMB_NADDR16_HI 104
+#define R_PPC_EMB_NADDR16_HA 105
+#define R_PPC_EMB_SDAI16 106
+#define R_PPC_EMB_SDA2I16 107
+#define R_PPC_EMB_SDA2REL 108
+#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */
+#define R_PPC_EMB_MRKREF 110
+#define R_PPC_EMB_RELSEC16 111
+#define R_PPC_EMB_RELST_LO 112
+#define R_PPC_EMB_RELST_HI 113
+#define R_PPC_EMB_RELST_HA 114
+#define R_PPC_EMB_BIT_FLD 115
+#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */
+
+/* Diab tool relocations. */
+#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */
+#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */
+#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */
+#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */
+#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */
+#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */
+
+/* This is a phony reloc to handle any old fashioned TOC16 references
+ that may still be in object files. */
+#define R_PPC_TOC16 255
+
+
+/* PowerPC64 relocations defined by the ABIs */
+#define R_PPC64_NONE R_PPC_NONE
+#define R_PPC64_ADDR32 R_PPC_ADDR32 /* 32bit absolute address */
+#define R_PPC64_ADDR24 R_PPC_ADDR24 /* 26bit address, word aligned */
+#define R_PPC64_ADDR16 R_PPC_ADDR16 /* 16bit absolute address */
+#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO /* lower 16bits of address */
+#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI /* high 16bits of address. */
+#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA /* adjusted high 16bits. */
+#define R_PPC64_ADDR14 R_PPC_ADDR14 /* 16bit address, word aligned */
+#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN
+#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN
+#define R_PPC64_REL24 R_PPC_REL24 /* PC-rel. 26 bit, word aligned */
+#define R_PPC64_REL14 R_PPC_REL14 /* PC relative 16 bit */
+#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN
+#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN
+#define R_PPC64_GOT16 R_PPC_GOT16
+#define R_PPC64_GOT16_LO R_PPC_GOT16_LO
+#define R_PPC64_GOT16_HI R_PPC_GOT16_HI
+#define R_PPC64_GOT16_HA R_PPC_GOT16_HA
+
+#define R_PPC64_COPY R_PPC_COPY
+#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT
+#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT
+#define R_PPC64_RELATIVE R_PPC_RELATIVE
+
+#define R_PPC64_UADDR32 R_PPC_UADDR32
+#define R_PPC64_UADDR16 R_PPC_UADDR16
+#define R_PPC64_REL32 R_PPC_REL32
+#define R_PPC64_PLT32 R_PPC_PLT32
+#define R_PPC64_PLTREL32 R_PPC_PLTREL32
+#define R_PPC64_PLT16_LO R_PPC_PLT16_LO
+#define R_PPC64_PLT16_HI R_PPC_PLT16_HI
+#define R_PPC64_PLT16_HA R_PPC_PLT16_HA
+
+#define R_PPC64_SECTOFF R_PPC_SECTOFF
+#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO
+#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI
+#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA
+#define R_PPC64_ADDR30 37 /* word30 (S + A - P) >> 2 */
+#define R_PPC64_ADDR64 38 /* doubleword64 S + A */
+#define R_PPC64_ADDR16_HIGHER 39 /* half16 #higher(S + A) */
+#define R_PPC64_ADDR16_HIGHERA 40 /* half16 #highera(S + A) */
+#define R_PPC64_ADDR16_HIGHEST 41 /* half16 #highest(S + A) */
+#define R_PPC64_ADDR16_HIGHESTA 42 /* half16 #highesta(S + A) */
+#define R_PPC64_UADDR64 43 /* doubleword64 S + A */
+#define R_PPC64_REL64 44 /* doubleword64 S + A - P */
+#define R_PPC64_PLT64 45 /* doubleword64 L + A */
+#define R_PPC64_PLTREL64 46 /* doubleword64 L + A - P */
+#define R_PPC64_TOC16 47 /* half16* S + A - .TOC */
+#define R_PPC64_TOC16_LO 48 /* half16 #lo(S + A - .TOC.) */
+#define R_PPC64_TOC16_HI 49 /* half16 #hi(S + A - .TOC.) */
+#define R_PPC64_TOC16_HA 50 /* half16 #ha(S + A - .TOC.) */
+#define R_PPC64_TOC 51 /* doubleword64 .TOC */
+#define R_PPC64_PLTGOT16 52 /* half16* M + A */
+#define R_PPC64_PLTGOT16_LO 53 /* half16 #lo(M + A) */
+#define R_PPC64_PLTGOT16_HI 54 /* half16 #hi(M + A) */
+#define R_PPC64_PLTGOT16_HA 55 /* half16 #ha(M + A) */
+
+#define R_PPC64_ADDR16_DS 56 /* half16ds* (S + A) >> 2 */
+#define R_PPC64_ADDR16_LO_DS 57 /* half16ds #lo(S + A) >> 2 */
+#define R_PPC64_GOT16_DS 58 /* half16ds* (G + A) >> 2 */
+#define R_PPC64_GOT16_LO_DS 59 /* half16ds #lo(G + A) >> 2 */
+#define R_PPC64_PLT16_LO_DS 60 /* half16ds #lo(L + A) >> 2 */
+#define R_PPC64_SECTOFF_DS 61 /* half16ds* (R + A) >> 2 */
+#define R_PPC64_SECTOFF_LO_DS 62 /* half16ds #lo(R + A) >> 2 */
+#define R_PPC64_TOC16_DS 63 /* half16ds* (S + A - .TOC.) >> 2 */
+#define R_PPC64_TOC16_LO_DS 64 /* half16ds #lo(S + A - .TOC.) >> 2 */
+#define R_PPC64_PLTGOT16_DS 65 /* half16ds* (M + A) >> 2 */
+#define R_PPC64_PLTGOT16_LO_DS 66 /* half16ds #lo(M + A) >> 2 */
+
+/* PowerPC64 relocations defined for the TLS access ABI. */
+#define R_PPC64_TLS 67 /* none (sym+add)@tls */
+#define R_PPC64_DTPMOD64 68 /* doubleword64 (sym+add)@dtpmod */
+#define R_PPC64_TPREL16 69 /* half16* (sym+add)@tprel */
+#define R_PPC64_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */
+#define R_PPC64_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */
+#define R_PPC64_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */
+#define R_PPC64_TPREL64 73 /* doubleword64 (sym+add)@tprel */
+#define R_PPC64_DTPREL16 74 /* half16* (sym+add)@dtprel */
+#define R_PPC64_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */
+#define R_PPC64_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */
+#define R_PPC64_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */
+#define R_PPC64_DTPREL64 78 /* doubleword64 (sym+add)@dtprel */
+#define R_PPC64_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */
+#define R_PPC64_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */
+#define R_PPC64_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */
+#define R_PPC64_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */
+#define R_PPC64_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */
+#define R_PPC64_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */
+#define R_PPC64_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */
+#define R_PPC64_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */
+#define R_PPC64_GOT_TPREL16_DS 87 /* half16ds* (sym+add)@got@tprel */
+#define R_PPC64_GOT_TPREL16_LO_DS 88 /* half16ds (sym+add)@got@tprel@l */
+#define R_PPC64_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */
+#define R_PPC64_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */
+#define R_PPC64_GOT_DTPREL16_DS 91 /* half16ds* (sym+add)@got@dtprel */
+#define R_PPC64_GOT_DTPREL16_LO_DS 92 /* half16ds (sym+add)@got@dtprel@l */
+#define R_PPC64_GOT_DTPREL16_HI 93 /* half16 (sym+add)@got@dtprel@h */
+#define R_PPC64_GOT_DTPREL16_HA 94 /* half16 (sym+add)@got@dtprel@ha */
+#define R_PPC64_TPREL16_DS 95 /* half16ds* (sym+add)@tprel */
+#define R_PPC64_TPREL16_LO_DS 96 /* half16ds (sym+add)@tprel@l */
+#define R_PPC64_TPREL16_HIGHER 97 /* half16 (sym+add)@tprel@higher */
+#define R_PPC64_TPREL16_HIGHERA 98 /* half16 (sym+add)@tprel@highera */
+#define R_PPC64_TPREL16_HIGHEST 99 /* half16 (sym+add)@tprel@highest */
+#define R_PPC64_TPREL16_HIGHESTA 100 /* half16 (sym+add)@tprel@highesta */
+#define R_PPC64_DTPREL16_DS 101 /* half16ds* (sym+add)@dtprel */
+#define R_PPC64_DTPREL16_LO_DS 102 /* half16ds (sym+add)@dtprel@l */
+#define R_PPC64_DTPREL16_HIGHER 103 /* half16 (sym+add)@dtprel@higher */
+#define R_PPC64_DTPREL16_HIGHERA 104 /* half16 (sym+add)@dtprel@highera */
+#define R_PPC64_DTPREL16_HIGHEST 105 /* half16 (sym+add)@dtprel@highest */
+#define R_PPC64_DTPREL16_HIGHESTA 106 /* half16 (sym+add)@dtprel@highesta */
+
+/* Keep this the last entry. */
+#define R_PPC64_NUM 107
+
+/* PowerPC64 specific values for the Dyn d_tag field. */
+#define DT_PPC64_GLINK (DT_LOPROC + 0)
+#define DT_PPC64_OPD (DT_LOPROC + 1)
+#define DT_PPC64_OPDSZ (DT_LOPROC + 2)
+#define DT_PPC64_NUM 3
+
+
+/* ARM specific declarations */
+
+/* Processor specific flags for the ELF header e_flags field. */
+#define EF_ARM_RELEXEC 0x01
+#define EF_ARM_HASENTRY 0x02
+#define EF_ARM_INTERWORK 0x04
+#define EF_ARM_APCS_26 0x08
+#define EF_ARM_APCS_FLOAT 0x10
+#define EF_ARM_PIC 0x20
+#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */
+#define EF_ARM_NEW_ABI 0x80
+#define EF_ARM_OLD_ABI 0x100
+
+/* Other constants defined in the ARM ELF spec. version B-01. */
+/* NB. These conflict with values defined above. */
+#define EF_ARM_SYMSARESORTED 0x04
+#define EF_ARM_DYNSYMSUSESEGIDX 0x08
+#define EF_ARM_MAPSYMSFIRST 0x10
+#define EF_ARM_EABIMASK 0XFF000000
+
+#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)
+#define EF_ARM_EABI_UNKNOWN 0x00000000
+#define EF_ARM_EABI_VER1 0x01000000
+#define EF_ARM_EABI_VER2 0x02000000
+
+/* Additional symbol types for Thumb */
+#define STT_ARM_TFUNC 0xd
+
+/* ARM-specific values for sh_flags */
+#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */
+#define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined
+ in the input to a link step */
+
+/* ARM-specific program header flags */
+#define PF_ARM_SB 0x10000000 /* Segment contains the location
+ addressed by the static base */
+
+/* ARM relocs. */
+#define R_ARM_NONE 0 /* No reloc */
+#define R_ARM_PC24 1 /* PC relative 26 bit branch */
+#define R_ARM_ABS32 2 /* Direct 32 bit */
+#define R_ARM_REL32 3 /* PC relative 32 bit */
+#define R_ARM_PC13 4
+#define R_ARM_ABS16 5 /* Direct 16 bit */
+#define R_ARM_ABS12 6 /* Direct 12 bit */
+#define R_ARM_THM_ABS5 7
+#define R_ARM_ABS8 8 /* Direct 8 bit */
+#define R_ARM_SBREL32 9
+#define R_ARM_THM_PC22 10
+#define R_ARM_THM_PC8 11
+#define R_ARM_AMP_VCALL9 12
+#define R_ARM_SWI24 13
+#define R_ARM_THM_SWI8 14
+#define R_ARM_XPC25 15
+#define R_ARM_THM_XPC22 16
+#define R_ARM_COPY 20 /* Copy symbol at runtime */
+#define R_ARM_GLOB_DAT 21 /* Create GOT entry */
+#define R_ARM_JUMP_SLOT 22 /* Create PLT entry */
+#define R_ARM_RELATIVE 23 /* Adjust by program base */
+#define R_ARM_GOTOFF 24 /* 32 bit offset to GOT */
+#define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */
+#define R_ARM_GOT32 26 /* 32 bit GOT entry */
+#define R_ARM_PLT32 27 /* 32 bit PLT address */
+#define R_ARM_ALU_PCREL_7_0 32
+#define R_ARM_ALU_PCREL_15_8 33
+#define R_ARM_ALU_PCREL_23_15 34
+#define R_ARM_LDR_SBREL_11_0 35
+#define R_ARM_ALU_SBREL_19_12 36
+#define R_ARM_ALU_SBREL_27_20 37
+#define R_ARM_GNU_VTENTRY 100
+#define R_ARM_GNU_VTINHERIT 101
+#define R_ARM_THM_PC11 102 /* thumb unconditional branch */
+#define R_ARM_THM_PC9 103 /* thumb conditional branch */
+#define R_ARM_RXPC25 249
+#define R_ARM_RSBREL32 250
+#define R_ARM_THM_RPC22 251
+#define R_ARM_RREL32 252
+#define R_ARM_RABS22 253
+#define R_ARM_RPC24 254
+#define R_ARM_RBASE 255
+/* Keep this the last entry. */
+#define R_ARM_NUM 256
+
+/* IA-64 specific declarations. */
+
+/* Processor specific flags for the Ehdr e_flags field. */
+#define EF_IA_64_MASKOS 0x0000000f /* os-specific flags */
+#define EF_IA_64_ABI64 0x00000010 /* 64-bit ABI */
+#define EF_IA_64_ARCH 0xff000000 /* arch. version mask */
+
+/* Processor specific values for the Phdr p_type field. */
+#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) /* arch extension bits */
+#define PT_IA_64_UNWIND (PT_LOPROC + 1) /* ia64 unwind bits */
+#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12)
+#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13)
+#define PT_IA_64_HP_STACK (PT_LOOS + 0x14)
+
+/* Processor specific flags for the Phdr p_flags field. */
+#define PF_IA_64_NORECOV 0x80000000 /* spec insns w/o recovery */
+
+/* Processor specific values for the Shdr sh_type field. */
+#define SHT_IA_64_EXT (SHT_LOPROC + 0) /* extension bits */
+#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) /* unwind bits */
+
+/* Processor specific flags for the Shdr sh_flags field. */
+#define SHF_IA_64_SHORT 0x10000000 /* section near gp */
+#define SHF_IA_64_NORECOV 0x20000000 /* spec insns w/o recovery */
+
+/* Processor specific values for the Dyn d_tag field. */
+#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0)
+#define DT_IA_64_NUM 1
+
+/* IA-64 relocations. */
+#define R_IA64_NONE 0x00 /* none */
+#define R_IA64_IMM14 0x21 /* symbol + addend, add imm14 */
+#define R_IA64_IMM22 0x22 /* symbol + addend, add imm22 */
+#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */
+#define R_IA64_DIR32MSB 0x24 /* symbol + addend, data4 MSB */
+#define R_IA64_DIR32LSB 0x25 /* symbol + addend, data4 LSB */
+#define R_IA64_DIR64MSB 0x26 /* symbol + addend, data8 MSB */
+#define R_IA64_DIR64LSB 0x27 /* symbol + addend, data8 LSB */
+#define R_IA64_GPREL22 0x2a /* @gprel(sym + add), add imm22 */
+#define R_IA64_GPREL64I 0x2b /* @gprel(sym + add), mov imm64 */
+#define R_IA64_GPREL32MSB 0x2c /* @gprel(sym + add), data4 MSB */
+#define R_IA64_GPREL32LSB 0x2d /* @gprel(sym + add), data4 LSB */
+#define R_IA64_GPREL64MSB 0x2e /* @gprel(sym + add), data8 MSB */
+#define R_IA64_GPREL64LSB 0x2f /* @gprel(sym + add), data8 LSB */
+#define R_IA64_LTOFF22 0x32 /* @ltoff(sym + add), add imm22 */
+#define R_IA64_LTOFF64I 0x33 /* @ltoff(sym + add), mov imm64 */
+#define R_IA64_PLTOFF22 0x3a /* @pltoff(sym + add), add imm22 */
+#define R_IA64_PLTOFF64I 0x3b /* @pltoff(sym + add), mov imm64 */
+#define R_IA64_PLTOFF64MSB 0x3e /* @pltoff(sym + add), data8 MSB */
+#define R_IA64_PLTOFF64LSB 0x3f /* @pltoff(sym + add), data8 LSB */
+#define R_IA64_FPTR64I 0x43 /* @fptr(sym + add), mov imm64 */
+#define R_IA64_FPTR32MSB 0x44 /* @fptr(sym + add), data4 MSB */
+#define R_IA64_FPTR32LSB 0x45 /* @fptr(sym + add), data4 LSB */
+#define R_IA64_FPTR64MSB 0x46 /* @fptr(sym + add), data8 MSB */
+#define R_IA64_FPTR64LSB 0x47 /* @fptr(sym + add), data8 LSB */
+#define R_IA64_PCREL60B 0x48 /* @pcrel(sym + add), brl */
+#define R_IA64_PCREL21B 0x49 /* @pcrel(sym + add), ptb, call */
+#define R_IA64_PCREL21M 0x4a /* @pcrel(sym + add), chk.s */
+#define R_IA64_PCREL21F 0x4b /* @pcrel(sym + add), fchkf */
+#define R_IA64_PCREL32MSB 0x4c /* @pcrel(sym + add), data4 MSB */
+#define R_IA64_PCREL32LSB 0x4d /* @pcrel(sym + add), data4 LSB */
+#define R_IA64_PCREL64MSB 0x4e /* @pcrel(sym + add), data8 MSB */
+#define R_IA64_PCREL64LSB 0x4f /* @pcrel(sym + add), data8 LSB */
+#define R_IA64_LTOFF_FPTR22 0x52 /* @ltoff(@fptr(s+a)), imm22 */
+#define R_IA64_LTOFF_FPTR64I 0x53 /* @ltoff(@fptr(s+a)), imm64 */
+#define R_IA64_LTOFF_FPTR32MSB 0x54 /* @ltoff(@fptr(s+a)), data4 MSB */
+#define R_IA64_LTOFF_FPTR32LSB 0x55 /* @ltoff(@fptr(s+a)), data4 LSB */
+#define R_IA64_LTOFF_FPTR64MSB 0x56 /* @ltoff(@fptr(s+a)), data8 MSB */
+#define R_IA64_LTOFF_FPTR64LSB 0x57 /* @ltoff(@fptr(s+a)), data8 LSB */
+#define R_IA64_SEGREL32MSB 0x5c /* @segrel(sym + add), data4 MSB */
+#define R_IA64_SEGREL32LSB 0x5d /* @segrel(sym + add), data4 LSB */
+#define R_IA64_SEGREL64MSB 0x5e /* @segrel(sym + add), data8 MSB */
+#define R_IA64_SEGREL64LSB 0x5f /* @segrel(sym + add), data8 LSB */
+#define R_IA64_SECREL32MSB 0x64 /* @secrel(sym + add), data4 MSB */
+#define R_IA64_SECREL32LSB 0x65 /* @secrel(sym + add), data4 LSB */
+#define R_IA64_SECREL64MSB 0x66 /* @secrel(sym + add), data8 MSB */
+#define R_IA64_SECREL64LSB 0x67 /* @secrel(sym + add), data8 LSB */
+#define R_IA64_REL32MSB 0x6c /* data 4 + REL */
+#define R_IA64_REL32LSB 0x6d /* data 4 + REL */
+#define R_IA64_REL64MSB 0x6e /* data 8 + REL */
+#define R_IA64_REL64LSB 0x6f /* data 8 + REL */
+#define R_IA64_LTV32MSB 0x74 /* symbol + addend, data4 MSB */
+#define R_IA64_LTV32LSB 0x75 /* symbol + addend, data4 LSB */
+#define R_IA64_LTV64MSB 0x76 /* symbol + addend, data8 MSB */
+#define R_IA64_LTV64LSB 0x77 /* symbol + addend, data8 LSB */
+#define R_IA64_PCREL21BI 0x79 /* @pcrel(sym + add), 21bit inst */
+#define R_IA64_PCREL22 0x7a /* @pcrel(sym + add), 22bit inst */
+#define R_IA64_PCREL64I 0x7b /* @pcrel(sym + add), 64bit inst */
+#define R_IA64_IPLTMSB 0x80 /* dynamic reloc, imported PLT, MSB */
+#define R_IA64_IPLTLSB 0x81 /* dynamic reloc, imported PLT, LSB */
+#define R_IA64_COPY 0x84 /* copy relocation */
+#define R_IA64_SUB 0x85 /* Addend and symbol difference */
+#define R_IA64_LTOFF22X 0x86 /* LTOFF22, relaxable. */
+#define R_IA64_LDXMOV 0x87 /* Use of LTOFF22X. */
+#define R_IA64_TPREL14 0x91 /* @tprel(sym + add), imm14 */
+#define R_IA64_TPREL22 0x92 /* @tprel(sym + add), imm22 */
+#define R_IA64_TPREL64I 0x93 /* @tprel(sym + add), imm64 */
+#define R_IA64_TPREL64MSB 0x96 /* @tprel(sym + add), data8 MSB */
+#define R_IA64_TPREL64LSB 0x97 /* @tprel(sym + add), data8 LSB */
+#define R_IA64_LTOFF_TPREL22 0x9a /* @ltoff(@tprel(s+a)), imm2 */
+#define R_IA64_DTPMOD64MSB 0xa6 /* @dtpmod(sym + add), data8 MSB */
+#define R_IA64_DTPMOD64LSB 0xa7 /* @dtpmod(sym + add), data8 LSB */
+#define R_IA64_LTOFF_DTPMOD22 0xaa /* @ltoff(@dtpmod(sym + add)), imm22 */
+#define R_IA64_DTPREL14 0xb1 /* @dtprel(sym + add), imm14 */
+#define R_IA64_DTPREL22 0xb2 /* @dtprel(sym + add), imm22 */
+#define R_IA64_DTPREL64I 0xb3 /* @dtprel(sym + add), imm64 */
+#define R_IA64_DTPREL32MSB 0xb4 /* @dtprel(sym + add), data4 MSB */
+#define R_IA64_DTPREL32LSB 0xb5 /* @dtprel(sym + add), data4 LSB */
+#define R_IA64_DTPREL64MSB 0xb6 /* @dtprel(sym + add), data8 MSB */
+#define R_IA64_DTPREL64LSB 0xb7 /* @dtprel(sym + add), data8 LSB */
+#define R_IA64_LTOFF_DTPREL22 0xba /* @ltoff(@dtprel(s+a)), imm22 */
+
+/* SH specific declarations */
+
+/* SH relocs. */
+#define R_SH_NONE 0
+#define R_SH_DIR32 1
+#define R_SH_REL32 2
+#define R_SH_DIR8WPN 3
+#define R_SH_IND12W 4
+#define R_SH_DIR8WPL 5
+#define R_SH_DIR8WPZ 6
+#define R_SH_DIR8BP 7
+#define R_SH_DIR8W 8
+#define R_SH_DIR8L 9
+#define R_SH_SWITCH16 25
+#define R_SH_SWITCH32 26
+#define R_SH_USES 27
+#define R_SH_COUNT 28
+#define R_SH_ALIGN 29
+#define R_SH_CODE 30
+#define R_SH_DATA 31
+#define R_SH_LABEL 32
+#define R_SH_SWITCH8 33
+#define R_SH_GNU_VTINHERIT 34
+#define R_SH_GNU_VTENTRY 35
+#define R_SH_TLS_GD_32 144
+#define R_SH_TLS_LD_32 145
+#define R_SH_TLS_LDO_32 146
+#define R_SH_TLS_IE_32 147
+#define R_SH_TLS_LE_32 148
+#define R_SH_TLS_DTPMOD32 149
+#define R_SH_TLS_DTPOFF32 150
+#define R_SH_TLS_TPOFF32 151
+#define R_SH_GOT32 160
+#define R_SH_PLT32 161
+#define R_SH_COPY 162
+#define R_SH_GLOB_DAT 163
+#define R_SH_JMP_SLOT 164
+#define R_SH_RELATIVE 165
+#define R_SH_GOTOFF 166
+#define R_SH_GOTPC 167
+/* Keep this the last entry. */
+#define R_SH_NUM 256
+
+/* Additional s390 relocs */
+
+#define R_390_NONE 0 /* No reloc. */
+#define R_390_8 1 /* Direct 8 bit. */
+#define R_390_12 2 /* Direct 12 bit. */
+#define R_390_16 3 /* Direct 16 bit. */
+#define R_390_32 4 /* Direct 32 bit. */
+#define R_390_PC32 5 /* PC relative 32 bit. */
+#define R_390_GOT12 6 /* 12 bit GOT offset. */
+#define R_390_GOT32 7 /* 32 bit GOT offset. */
+#define R_390_PLT32 8 /* 32 bit PC relative PLT address. */
+#define R_390_COPY 9 /* Copy symbol at runtime. */
+#define R_390_GLOB_DAT 10 /* Create GOT entry. */
+#define R_390_JMP_SLOT 11 /* Create PLT entry. */
+#define R_390_RELATIVE 12 /* Adjust by program base. */
+#define R_390_GOTOFF32 13 /* 32 bit offset to GOT. */
+#define R_390_GOTPC 14 /* 32 bit PC relative offset to GOT. */
+#define R_390_GOT16 15 /* 16 bit GOT offset. */
+#define R_390_PC16 16 /* PC relative 16 bit. */
+#define R_390_PC16DBL 17 /* PC relative 16 bit shifted by 1. */
+#define R_390_PLT16DBL 18 /* 16 bit PC rel. PLT shifted by 1. */
+#define R_390_PC32DBL 19 /* PC relative 32 bit shifted by 1. */
+#define R_390_PLT32DBL 20 /* 32 bit PC rel. PLT shifted by 1. */
+#define R_390_GOTPCDBL 21 /* 32 bit PC rel. GOT shifted by 1. */
+#define R_390_64 22 /* Direct 64 bit. */
+#define R_390_PC64 23 /* PC relative 64 bit. */
+#define R_390_GOT64 24 /* 64 bit GOT offset. */
+#define R_390_PLT64 25 /* 64 bit PC relative PLT address. */
+#define R_390_GOTENT 26 /* 32 bit PC rel. to GOT entry >> 1. */
+#define R_390_GOTOFF16 27 /* 16 bit offset to GOT. */
+#define R_390_GOTOFF64 28 /* 64 bit offset to GOT. */
+#define R_390_GOTPLT12 29 /* 12 bit offset to jump slot. */
+#define R_390_GOTPLT16 30 /* 16 bit offset to jump slot. */
+#define R_390_GOTPLT32 31 /* 32 bit offset to jump slot. */
+#define R_390_GOTPLT64 32 /* 64 bit offset to jump slot. */
+#define R_390_GOTPLTENT 33 /* 32 bit rel. offset to jump slot. */
+#define R_390_PLTOFF16 34 /* 16 bit offset from GOT to PLT. */
+#define R_390_PLTOFF32 35 /* 32 bit offset from GOT to PLT. */
+#define R_390_PLTOFF64 36 /* 16 bit offset from GOT to PLT. */
+#define R_390_TLS_LOAD 37 /* Tag for load insn in TLS code. */
+#define R_390_TLS_GDCALL 38 /* Tag for function call in general
+ dynamic TLS code. */
+#define R_390_TLS_LDCALL 39 /* Tag for function call in local
+ dynamic TLS code. */
+#define R_390_TLS_GD32 40 /* Direct 32 bit for general dynamic
+ thread local data. */
+#define R_390_TLS_GD64 41 /* Direct 64 bit for general dynamic
+ thread local data. */
+#define R_390_TLS_GOTIE12 42 /* 12 bit GOT offset for static TLS
+ block offset. */
+#define R_390_TLS_GOTIE32 43 /* 32 bit GOT offset for static TLS
+ block offset. */
+#define R_390_TLS_GOTIE64 44 /* 64 bit GOT offset for static TLS
+ block offset. */
+#define R_390_TLS_LDM32 45 /* Direct 32 bit for local dynamic
+ thread local data in LE code. */
+#define R_390_TLS_LDM64 46 /* Direct 64 bit for local dynamic
+ thread local data in LE code. */
+#define R_390_TLS_IE32 47 /* 32 bit address of GOT entry for
+ negated static TLS block offset. */
+#define R_390_TLS_IE64 48 /* 64 bit address of GOT entry for
+ negated static TLS block offset. */
+#define R_390_TLS_IEENT 49 /* 32 bit rel. offset to GOT entry for
+ negated static TLS block offset. */
+#define R_390_TLS_LE32 50 /* 32 bit negated offset relative to
+ static TLS block. */
+#define R_390_TLS_LE64 51 /* 64 bit negated offset relative to
+ static TLS block. */
+#define R_390_TLS_LDO32 52 /* 32 bit offset relative to TLS
+ block. */
+#define R_390_TLS_LDO64 53 /* 64 bit offset relative to TLS
+ block. */
+#define R_390_TLS_DTPMOD 54 /* ID of module containing symbol. */
+#define R_390_TLS_DTPOFF 55 /* Offset in TLS block. */
+#define R_390_TLS_TPOFF 56 /* Negated offset in static TLS
+ block. */
+#define R_390_20 57 /* Direct 20 bit. */
+#define R_390_GOT20 58 /* 20 bit GOT offset. */
+#define R_390_GOTPLT20 59 /* 20 bit offset to jump slot. */
+#define R_390_TLS_GOTIE20 60 /* 20 bit GOT offset for static TLS
+ block offset. */
+/* Keep this the last entry. */
+#define R_390_NUM 61
+
+
+/* CRIS relocations. */
+#define R_CRIS_NONE 0
+#define R_CRIS_8 1
+#define R_CRIS_16 2
+#define R_CRIS_32 3
+#define R_CRIS_8_PCREL 4
+#define R_CRIS_16_PCREL 5
+#define R_CRIS_32_PCREL 6
+#define R_CRIS_GNU_VTINHERIT 7
+#define R_CRIS_GNU_VTENTRY 8
+#define R_CRIS_COPY 9
+#define R_CRIS_GLOB_DAT 10
+#define R_CRIS_JUMP_SLOT 11
+#define R_CRIS_RELATIVE 12
+#define R_CRIS_16_GOT 13
+#define R_CRIS_32_GOT 14
+#define R_CRIS_16_GOTPLT 15
+#define R_CRIS_32_GOTPLT 16
+#define R_CRIS_32_GOTREL 17
+#define R_CRIS_32_PLT_GOTREL 18
+#define R_CRIS_32_PLT_PCREL 19
+
+#define R_CRIS_NUM 20
+
+
+/* AMD x86-64 relocations. */
+#define R_X86_64_NONE 0 /* No reloc */
+#define R_X86_64_64 1 /* Direct 64 bit */
+#define R_X86_64_PC32 2 /* PC relative 32 bit signed */
+#define R_X86_64_GOT32 3 /* 32 bit GOT entry */
+#define R_X86_64_PLT32 4 /* 32 bit PLT address */
+#define R_X86_64_COPY 5 /* Copy symbol at runtime */
+#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
+#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
+#define R_X86_64_RELATIVE 8 /* Adjust by program base */
+#define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative
+ offset to GOT */
+#define R_X86_64_32 10 /* Direct 32 bit zero extended */
+#define R_X86_64_32S 11 /* Direct 32 bit sign extended */
+#define R_X86_64_16 12 /* Direct 16 bit zero extended */
+#define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
+#define R_X86_64_8 14 /* Direct 8 bit sign extended */
+#define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
+#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
+#define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */
+#define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */
+#define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset
+ to two GOT entries for GD symbol */
+#define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset
+ to two GOT entries for LD symbol */
+#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
+#define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset
+ to GOT entry for IE symbol */
+#define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */
+
+#define R_X86_64_NUM 24
+
+
+/* AM33 relocations. */
+#define R_MN10300_NONE 0 /* No reloc. */
+#define R_MN10300_32 1 /* Direct 32 bit. */
+#define R_MN10300_16 2 /* Direct 16 bit. */
+#define R_MN10300_8 3 /* Direct 8 bit. */
+#define R_MN10300_PCREL32 4 /* PC-relative 32-bit. */
+#define R_MN10300_PCREL16 5 /* PC-relative 16-bit signed. */
+#define R_MN10300_PCREL8 6 /* PC-relative 8-bit signed. */
+#define R_MN10300_GNU_VTINHERIT 7 /* Ancient C++ vtable garbage... */
+#define R_MN10300_GNU_VTENTRY 8 /* ... collection annotation. */
+#define R_MN10300_24 9 /* Direct 24 bit. */
+#define R_MN10300_GOTPC32 10 /* 32-bit PCrel offset to GOT. */
+#define R_MN10300_GOTPC16 11 /* 16-bit PCrel offset to GOT. */
+#define R_MN10300_GOTOFF32 12 /* 32-bit offset from GOT. */
+#define R_MN10300_GOTOFF24 13 /* 24-bit offset from GOT. */
+#define R_MN10300_GOTOFF16 14 /* 16-bit offset from GOT. */
+#define R_MN10300_PLT32 15 /* 32-bit PCrel to PLT entry. */
+#define R_MN10300_PLT16 16 /* 16-bit PCrel to PLT entry. */
+#define R_MN10300_GOT32 17 /* 32-bit offset to GOT entry. */
+#define R_MN10300_GOT24 18 /* 24-bit offset to GOT entry. */
+#define R_MN10300_GOT16 19 /* 16-bit offset to GOT entry. */
+#define R_MN10300_COPY 20 /* Copy symbol at runtime. */
+#define R_MN10300_GLOB_DAT 21 /* Create GOT entry. */
+#define R_MN10300_JMP_SLOT 22 /* Create PLT entry. */
+#define R_MN10300_RELATIVE 23 /* Adjust by program base. */
+
+#define R_MN10300_NUM 24
+
+
+/* M32R relocs. */
+#define R_M32R_NONE 0 /* No reloc. */
+#define R_M32R_16 1 /* Direct 16 bit. */
+#define R_M32R_32 2 /* Direct 32 bit. */
+#define R_M32R_24 3 /* Direct 24 bit. */
+#define R_M32R_10_PCREL 4 /* PC relative 10 bit shifted. */
+#define R_M32R_18_PCREL 5 /* PC relative 18 bit shifted. */
+#define R_M32R_26_PCREL 6 /* PC relative 26 bit shifted. */
+#define R_M32R_HI16_ULO 7 /* High 16 bit with unsigned low. */
+#define R_M32R_HI16_SLO 8 /* High 16 bit with signed low. */
+#define R_M32R_LO16 9 /* Low 16 bit. */
+#define R_M32R_SDA16 10 /* 16 bit offset in SDA. */
+#define R_M32R_GNU_VTINHERIT 11
+#define R_M32R_GNU_VTENTRY 12
+/* M32R relocs use SHT_RELA. */
+#define R_M32R_16_RELA 33 /* Direct 16 bit. */
+#define R_M32R_32_RELA 34 /* Direct 32 bit. */
+#define R_M32R_24_RELA 35 /* Direct 24 bit. */
+#define R_M32R_10_PCREL_RELA 36 /* PC relative 10 bit shifted. */
+#define R_M32R_18_PCREL_RELA 37 /* PC relative 18 bit shifted. */
+#define R_M32R_26_PCREL_RELA 38 /* PC relative 26 bit shifted. */
+#define R_M32R_HI16_ULO_RELA 39 /* High 16 bit with unsigned low */
+#define R_M32R_HI16_SLO_RELA 40 /* High 16 bit with signed low */
+#define R_M32R_LO16_RELA 41 /* Low 16 bit */
+#define R_M32R_SDA16_RELA 42 /* 16 bit offset in SDA */
+#define R_M32R_RELA_GNU_VTINHERIT 43
+#define R_M32R_RELA_GNU_VTENTRY 44
+
+#define R_M32R_GOT24 48 /* 24 bit GOT entry */
+#define R_M32R_26_PLTREL 49 /* 26 bit PC relative to PLT shifted */
+#define R_M32R_COPY 50 /* Copy symbol at runtime */
+#define R_M32R_GLOB_DAT 51 /* Create GOT entry */
+#define R_M32R_JMP_SLOT 52 /* Create PLT entry */
+#define R_M32R_RELATIVE 53 /* Adjust by program base */
+#define R_M32R_GOTOFF 54 /* 24 bit offset to GOT */
+#define R_M32R_GOTPC24 55 /* 24 bit PC relative offset to GOT */
+#define R_M32R_GOT16_HI_ULO 56 /* High 16 bit GOT entry with unsigned
+ low */
+#define R_M32R_GOT16_HI_SLO 57 /* High 16 bit GOT entry with signed
+ low */
+#define R_M32R_GOT16_LO 58 /* Low 16 bit GOT entry */
+#define R_M32R_GOTPC_HI_ULO 59 /* High 16 bit PC relative offset to
+ GOT with unsigned low */
+#define R_M32R_GOTPC_HI_SLO 60 /* High 16 bit PC relative offset to
+ GOT with signed low */
+#define R_M32R_GOTPC_LO 61 /* Low 16 bit PC relative offset to
+ GOT */
+#define R_M32R_GOTOFF_HI_ULO 62 /* High 16 bit offset to GOT
+ with unsigned low */
+#define R_M32R_GOTOFF_HI_SLO 63 /* High 16 bit offset to GOT
+ with signed low */
+#define R_M32R_GOTOFF_LO 64 /* Low 16 bit offset to GOT */
+#define R_M32R_NUM 256 /* Keep this the last entry. */
+
+
+__END_DECLS
+
+#endif /* elf.h */
diff --git a/trunk/src/exec.c b/trunk/src/exec.c
new file mode 100644
index 0000000..7c8f38f
--- /dev/null
+++ b/trunk/src/exec.c
@@ -0,0 +1,1068 @@
+/* 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+#include "space.h"
+
+int
+update_dynamic_tags (DSO *dso, GElf_Shdr *shdr, GElf_Shdr *old_shdr,
+ struct section_move *move)
+{
+ int i, j;
+
+ for (i = 1; i < move->new_shnum; ++i)
+ {
+ j = move->new_to_old[i];
+ if (j == -1)
+ continue;
+ if ((dynamic_info_is_set (dso, DT_HASH)
+ && dso->info[DT_HASH] == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_HASH
+ && set_dynamic (dso, DT_HASH, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_SYMTAB)
+ && dso->info[DT_SYMTAB] == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_DYNSYM
+ && set_dynamic (dso, DT_SYMTAB, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_STRTAB)
+ && dso->info[DT_STRTAB] == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_STRTAB
+ && set_dynamic (dso, DT_STRTAB, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_VERDEF_BIT)
+ && dso->info_DT_VERDEF == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_GNU_verdef
+ && set_dynamic (dso, DT_VERDEF, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_VERNEED_BIT)
+ && dso->info_DT_VERNEED == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_GNU_verneed
+ && set_dynamic (dso, DT_VERNEED, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_VERSYM_BIT)
+ && dso->info_DT_VERSYM == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_GNU_versym
+ && set_dynamic (dso, DT_VERSYM, shdr[i].sh_addr, 1))
+ || (dynamic_info_is_set (dso, DT_GNU_HASH_BIT)
+ && dso->info_DT_GNU_HASH == old_shdr[j].sh_addr
+ && old_shdr[j].sh_type == SHT_GNU_HASH
+ && set_dynamic (dso, DT_GNU_HASH, shdr[i].sh_addr, 1)))
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+prelink_exec (struct prelink_info *info)
+{
+ int i, j, ndeps = info->ent->ndepends + 1;
+ int dynstrndx, dynstrndxnew, growdynstr = 0, shstrndxnew;
+ int old_conflict = 0, old_liblist = 0;
+ int new_conflict = -1, new_liblist = -1;
+ int new_reloc = -1, new_plt = -1, new_dynstr = -1;
+ int old_dynbss = -1, old_bss = -1, new_dynbss = -1;
+ int old_sdynbss = -1, old_sbss = -1, new_sdynbss = -1;
+ int addcnt, undo, shnum_after_undo;
+ struct reloc_info rinfo, rinfonew;
+ DSO *dso = info->dso;
+ GElf_Ehdr ehdr;
+ GElf_Phdr phdr[dso->ehdr.e_phnum + 1];
+ GElf_Shdr old_shdr[dso->ehdr.e_shnum], new_shdr[dso->ehdr.e_shnum + 20];
+ GElf_Shdr shdr_after_undo[dso->ehdr.e_shnum + 20];
+ GElf_Shdr *shdr;
+ Elf32_Lib *liblist = NULL;
+ struct readonly_adjust adjust;
+ struct section_move *move = NULL;
+
+ if (prelink_build_conflicts (info))
+ return 1;
+
+ if (find_reloc_sections (dso, &rinfo))
+ return 1;
+
+ move = init_section_move (dso);
+ if (move == NULL)
+ return 1;
+
+ ehdr = dso->ehdr;
+ memcpy (phdr, dso->phdr, dso->ehdr.e_phnum * sizeof (GElf_Phdr));
+ memcpy (old_shdr, dso->shdr, dso->ehdr.e_shnum * sizeof (GElf_Shdr));
+ shdr = new_shdr;
+ memcpy (shdr, dso->shdr, dso->ehdr.e_shnum * sizeof (GElf_Shdr));
+
+ for (undo = 1; undo < dso->ehdr.e_shnum; ++undo)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[undo].sh_name),
+ ".gnu.prelink_undo"))
+ break;
+
+ if (undo < dso->ehdr.e_shnum)
+ {
+ Elf_Data *data;
+
+ if (undo_sections (dso, undo, move, &rinfo, &ehdr, phdr, shdr))
+ {
+error_out:
+ free (liblist);
+ free (move);
+ return 1;
+ }
+
+ data = elf_getdata (dso->scn[undo], NULL);
+ assert (data->d_buf != NULL);
+ assert (data->d_off == 0);
+ assert (data->d_size == dso->shdr[undo].sh_size);
+ dso->undo = *data;
+ dso->undo.d_buf = malloc (dso->undo.d_size);
+ if (dso->undo.d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not create .gnu.prelink_undo section",
+ dso->filename);
+ goto error_out;
+ }
+ memcpy (dso->undo.d_buf, data->d_buf, data->d_size);
+ ehdr.e_shstrndx = dso->ehdr.e_shstrndx;
+ }
+ undo = 0;
+
+ memcpy (shdr_after_undo, shdr, ehdr.e_shnum * sizeof (GElf_Shdr));
+
+ for (dynstrndx = 1; dynstrndx < dso->ehdr.e_shnum; ++dynstrndx)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[dynstrndx].sh_name),
+ ".dynstr"))
+ break;
+
+ if (dynstrndx == dso->ehdr.e_shnum)
+ {
+ error (0, 0, "%s: Could not find .dynstr section", dso->filename);
+ goto error_out;
+ }
+
+ dynstrndxnew = move->old_to_new[dynstrndx];
+ shstrndxnew = move->old_to_new[dso->ehdr.e_shstrndx];
+ shnum_after_undo = move->new_shnum;
+
+ if (ndeps > 1)
+ {
+ liblist = calloc (ndeps - 1, sizeof (Elf32_Lib));
+ if (liblist == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot build .gnu.liblist section",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ else
+ liblist = NULL;
+
+ for (i = 0; i < ndeps - 1; ++i)
+ {
+ struct prelink_entry *ent = info->ent->depends[i];
+
+ liblist[i].l_name = strtabfind (dso, dynstrndx, info->sonames[i + 1]);
+ if (liblist[i].l_name >= shdr[dynstrndxnew].sh_size)
+ liblist[i].l_name = 0;
+ if (liblist[i].l_name == 0)
+ growdynstr += strlen (info->sonames[i + 1]) + 1;
+ liblist[i].l_time_stamp = ent->timestamp;
+ liblist[i].l_checksum = ent->checksum;
+ }
+
+ if (info->dynbss)
+ {
+ old_bss = addr_to_sec (dso, info->dynbss_base);
+ assert (old_bss != -1);
+ if (move->old_to_new[old_bss] == -1)
+ ++old_bss;
+ assert (move->old_to_new[old_bss] != -1);
+ assert (shdr[move->old_to_new[old_bss]].sh_addr <= info->dynbss_base);
+ assert (shdr[move->old_to_new[old_bss]].sh_addr
+ + shdr[move->old_to_new[old_bss]].sh_size > info->dynbss_base);
+ }
+ if (info->sdynbss)
+ {
+ old_sbss = addr_to_sec (dso, info->sdynbss_base);
+ assert (old_sbss != -1);
+ if (move->old_to_new[old_sbss] == -1)
+ ++old_sbss;
+ assert (move->old_to_new[old_sbss] != -1);
+ assert (shdr[move->old_to_new[old_sbss]].sh_addr <= info->sdynbss_base);
+ assert (shdr[move->old_to_new[old_sbss]].sh_addr
+ + shdr[move->old_to_new[old_sbss]].sh_size > info->sdynbss_base);
+ }
+
+ rinfonew = rinfo;
+ if (rinfo.first != -1)
+ {
+ rinfonew.first = move->old_to_new[rinfo.first];
+ rinfonew.last = move->old_to_new[rinfo.last];
+ if (shdr[rinfonew.first].sh_type == SHT_REL
+ && dso->shdr[rinfo.first].sh_type == SHT_RELA)
+ {
+ rinfonew.rel_to_rela = 1;
+ rinfonew.reldyn_rela = 0;
+ }
+ }
+ if (rinfo.plt != -1)
+ {
+ rinfonew.plt = move->old_to_new[rinfo.plt];
+ if (shdr[rinfonew.plt].sh_type == SHT_REL
+ && dso->shdr[rinfo.plt].sh_type == SHT_RELA)
+ {
+ rinfonew.rel_to_rela_plt = 1;
+ rinfonew.plt_rela = 0;
+ }
+ }
+
+ for (i = 1, j = 1; i < ehdr.e_shnum; ++i)
+ {
+ const char *name;
+ name = strptr (dso, dso->ehdr.e_shstrndx, shdr[i].sh_name);
+ if (! strcmp (name, ".dynbss"))
+ old_dynbss = move->new_to_old[j];
+ else if (! strcmp (name, ".sdynbss"))
+ old_sdynbss = move->new_to_old[j];
+ else if (! strcmp (name, ".gnu.prelink_undo"))
+ undo = -1;
+ if (! strcmp (name, ".gnu.conflict"))
+ {
+ old_conflict = move->new_to_old[j];
+ remove_section (move, j);
+ }
+ else if (! strcmp (name, ".gnu.liblist"))
+ {
+ old_liblist = move->new_to_old[j];
+ remove_section (move, j);
+ }
+ else if (rinfonew.rel_to_rela
+ && i >= rinfonew.first && i <= rinfonew.last)
+ remove_section (move, j);
+ else if (i == rinfonew.plt
+ && (rinfonew.rel_to_rela || rinfonew.rel_to_rela_plt))
+ remove_section (move, j);
+ else if (i == dynstrndxnew && growdynstr)
+ remove_section (move, j);
+ else
+ shdr[j++] = shdr[i];
+ }
+ assert (j == move->new_shnum);
+ ehdr.e_shnum = j;
+
+ if (old_sdynbss != -1 && old_dynbss == -1)
+ {
+ old_dynbss = old_sdynbss;
+ old_sdynbss = -1;
+ }
+
+ GElf_Shdr add[rinfo.last - rinfo.first + 5];
+ int old[rinfo.last - rinfo.first + 5];
+ int new[rinfo.last - rinfo.first + 5];
+ memset (add, 0, sizeof (add));
+ memset (old, 0, sizeof (old));
+ memset (new, 0, sizeof (new));
+
+ i = 0;
+ if (rinfonew.rel_to_rela)
+ {
+ add[i] = shdr_after_undo[rinfonew.first];
+ add[i].sh_size = shdr_after_undo[rinfonew.last].sh_addr
+ + shdr_after_undo[rinfonew.last].sh_size
+ - add[i].sh_addr;
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ add[i].sh_size = add[i].sh_size / 2 * 3;
+ old[i] = rinfo.first;
+ new_reloc = i++;
+ for (j = rinfo.first + 1; j <= rinfo.last; ++j)
+ {
+ add[i] = shdr_after_undo[rinfonew.first - rinfo.first + j];
+ add[i].sh_size = add[i].sh_size / 2 * 3;
+ old[i++] = j;
+ }
+ if (rinfonew.plt)
+ {
+ add[i] = shdr_after_undo[rinfonew.plt];
+ if (rinfonew.rel_to_rela_plt)
+ add[i].sh_size = add[i].sh_size / 2 * 3;
+ /* Temporarily merge them, so that they are allocated adjacently. */
+ add[new_reloc].sh_size += add[i].sh_size;
+ old[i] = rinfo.plt;
+ new_plt = i++;
+ }
+ }
+ else if (rinfonew.rel_to_rela_plt)
+ {
+ add[i] = shdr_after_undo[rinfonew.plt];
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ add[i].sh_size = add[i].sh_size / 2 * 3;
+ old[i] = rinfo.plt;
+ new_plt = i++;
+ }
+ if (growdynstr)
+ {
+ add[i] = shdr_after_undo[dynstrndxnew];
+ add[i].sh_size += growdynstr;
+ old[i] = dynstrndx;
+ new_dynstr = i++;
+ }
+ add[i].sh_flags = SHF_ALLOC;
+ add[i].sh_type = SHT_GNU_LIBLIST;
+ add[i].sh_size = (ndeps - 1) * sizeof (Elf32_Lib);
+ add[i].sh_addralign = sizeof (GElf_Word);
+ add[i].sh_entsize = sizeof (Elf32_Lib);
+ old[i] = old_liblist;
+ new_liblist = i++;
+ if (info->conflict_rela_size)
+ {
+ add[i].sh_flags = SHF_ALLOC;
+ add[i].sh_type = SHT_RELA;
+ add[i].sh_entsize = gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ add[i].sh_size = info->conflict_rela_size * add[i].sh_entsize;
+ add[i].sh_addralign = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT);
+ old[i] = old_conflict;
+ new_conflict = i++;
+ }
+ addcnt = i;
+ memset (&adjust, 0, sizeof (adjust));
+ adjust.new = new;
+ adjust.move = move;
+
+ for (i = 0; i < addcnt; ++i)
+ {
+ new[i] = find_readonly_space (dso, add + i, &ehdr, phdr, shdr, &adjust);
+ if (new[i] == 0)
+ goto error_out;
+ add_section (move, new[i]);
+ ++adjust.newcount;
+ if (old[i])
+ {
+ move->old_to_new[old[i]] = new[i];
+ move->new_to_old[new[i]] = old[i];
+ }
+ if (i == new_reloc)
+ {
+ int k, l = new[new_reloc];
+
+ j = rinfo.last - rinfo.first + (new_plt != -1);
+ shdr[l].sh_size = shdr_after_undo[rinfonew.first].sh_size / 2 * 3;
+ for (k = 1; k <= j; ++k)
+ {
+ insert_readonly_section (&ehdr, shdr, l + k, &adjust);
+ shdr[l + k] = add[new_reloc + k];
+ shdr[l + k].sh_addr = shdr[l + k - 1].sh_addr
+ + shdr[l + k - 1].sh_size;
+ shdr[l + k].sh_offset = shdr[l + k - 1].sh_offset
+ + shdr[l + k - 1].sh_size;
+ new[++i] = l + k;
+ add_section (move, l + k);
+ move->old_to_new[rinfo.first + k] = l + k;
+ move->new_to_old[l + k] = rinfo.first + k;
+ ++adjust.newcount;
+ }
+ }
+ }
+
+ if (info->sdynbss)
+ {
+ if (old_sdynbss == -1)
+ {
+ new_sdynbss = move->old_to_new[old_sbss];
+ memmove (&shdr[new_sdynbss + 1], &shdr[new_sdynbss],
+ (ehdr.e_shnum - new_sdynbss) * sizeof (GElf_Shdr));
+ shdr[new_sdynbss].sh_size = 0;
+ ++ehdr.e_shnum;
+ add_section (move, new_sdynbss);
+ for (i = 0; i < addcnt; ++i)
+ if (new[i] >= new_sdynbss)
+ ++new[i];
+ }
+ else
+ new_sdynbss = move->old_to_new[old_sdynbss];
+ }
+
+ if (info->dynbss)
+ {
+ if (old_dynbss == -1)
+ {
+ new_dynbss = move->old_to_new[old_bss];
+ memmove (&shdr[new_dynbss + 1], &shdr[new_dynbss],
+ (ehdr.e_shnum - new_dynbss) * sizeof (GElf_Shdr));
+ shdr[new_dynbss].sh_size = 0;
+ ++ehdr.e_shnum;
+ add_section (move, new_dynbss);
+ for (i = 0; i < addcnt; ++i)
+ if (new[i] >= new_dynbss)
+ ++new[i];
+ }
+ else
+ new_dynbss = move->old_to_new[old_dynbss];
+ }
+
+ if (undo != -1)
+ {
+ undo = move->old_to_new[dso->ehdr.e_shstrndx];
+ memmove (&shdr[undo + 1], &shdr[undo],
+ (ehdr.e_shnum - undo) * sizeof (GElf_Shdr));
+ memset (&shdr[undo], 0, sizeof (shdr[undo]));
+ shdr[undo].sh_type = SHT_PROGBITS;
+ shdr[undo].sh_addralign = dso->undo.d_align;
+ ++ehdr.e_shnum;
+ for (i = 0; i < addcnt; ++i)
+ if (new[i] >= undo)
+ ++new[i];
+ add_section (move, undo);
+ }
+
+ i = ehdr.e_shnum;
+ ehdr.e_shnum = dso->ehdr.e_shnum;
+ dso->ehdr = ehdr;
+ memcpy (dso->phdr, phdr, ehdr.e_phnum * sizeof (GElf_Phdr));
+ if (reopen_dso (dso, move, NULL))
+ goto error_out;
+
+ assert (i == dso->ehdr.e_shnum);
+
+ if (shnum_after_undo != move->new_shnum)
+ adjust_nonalloc (dso, &dso->ehdr, shdr, 0,
+ dso->ehdr.e_shoff + 1,
+ ((long) move->new_shnum - (long) shnum_after_undo)
+ * gelf_fsize (dso->elf, ELF_T_SHDR, 1, EV_CURRENT));
+
+ if (shdr_after_undo[shstrndxnew].sh_size
+ < dso->shdr[dso->ehdr.e_shstrndx].sh_size)
+ {
+ Elf_Data *data = elf_getdata (dso->scn[dso->ehdr.e_shstrndx], NULL);
+
+ assert (elf_getdata (dso->scn[dso->ehdr.e_shstrndx], data) == NULL);
+ assert (data->d_off == 0);
+ assert (shdr_after_undo[shstrndxnew].sh_size
+ == shdr[dso->ehdr.e_shstrndx].sh_size);
+ assert (data->d_size == dso->shdr[dso->ehdr.e_shstrndx].sh_size);
+ data->d_size = shdr_after_undo[shstrndxnew].sh_size;
+ }
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (move->new_to_old[i] == -1)
+ dso->shdr[i] = shdr[i];
+ else
+ {
+ if (shdr[i].sh_type == SHT_PROGBITS
+ && dso->shdr[i].sh_type == SHT_NOBITS)
+ {
+ Elf_Data *data = elf_getdata (dso->scn[i], NULL);
+
+ assert (data->d_buf == NULL);
+ data->d_size = shdr[i].sh_size;
+ if (data->d_size)
+ {
+ data->d_buf = calloc (shdr[i].sh_size, 1);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not convert NOBITS section into PROGBITS",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ data->d_type = ELF_T_BYTE;
+ }
+ dso->shdr[i].sh_type = shdr[i].sh_type;
+ dso->shdr[i].sh_addr = shdr[i].sh_addr;
+ dso->shdr[i].sh_size = shdr[i].sh_size;
+ dso->shdr[i].sh_offset = shdr[i].sh_offset;
+ }
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD)
+ {
+ GElf_Addr last_offset = dso->phdr[i].p_offset;
+ GElf_Addr adj = 0;
+ int sfirst = 0, slast = 0, last = 0;
+
+ for (j = 1; j < dso->ehdr.e_shnum; ++j)
+ if (dso->shdr[j].sh_addr >= dso->phdr[i].p_vaddr
+ && dso->shdr[j].sh_addr + dso->shdr[j].sh_size
+ <= dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz)
+ {
+ if (dso->shdr[j].sh_type != SHT_NOBITS
+ || (dso->shdr[j].sh_flags & SHF_TLS))
+ {
+ if (sfirst)
+ {
+ error (0, 0, "%s: NOBITS section followed by non-NOBITS section in the same segment",
+ dso->filename);
+ goto error_out;
+ }
+ continue;
+ }
+
+ if (!sfirst)
+ sfirst = j;
+ if (strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[j].sh_name), ".plt") == 0)
+ slast = j + 1;
+ else if (j == new_dynbss || j == new_sdynbss)
+ slast = j;
+ }
+
+ if (sfirst && slast)
+ {
+ for (j = sfirst; j < slast; ++j)
+ {
+ Elf_Data *data = elf_getdata (dso->scn[j], NULL);
+
+ assert (data->d_size == dso->shdr[j].sh_size
+ || j == new_dynbss + 1
+ || j == new_sdynbss + 1);
+ if (data->d_size)
+ {
+ data->d_buf = realloc (data->d_buf, data->d_size);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not convert NOBITS section into PROGBITS",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ memset (data->d_buf, 0, data->d_size);
+ data->d_type = ELF_T_BYTE;
+ dso->shdr[j].sh_type = SHT_PROGBITS;
+ }
+
+ adj = dso->shdr[slast - 1].sh_addr + dso->shdr[slast - 1].sh_size
+ - dso->phdr[i].p_vaddr;
+
+ if (adj > dso->phdr[i].p_filesz)
+ {
+ adj -= dso->phdr[i].p_filesz;
+ for (j = slast;
+ j < dso->ehdr.e_shnum
+ && (dso->shdr[j].sh_flags
+ & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR));
+ ++j)
+ if (dso->shdr[j].sh_addr >= dso->phdr[i].p_vaddr
+ + dso->phdr[i].p_memsz)
+ adj = (adj + dso->shdr[j].sh_addralign - 1)
+ & ~(dso->shdr[j].sh_addralign - 1);
+
+ dso->phdr[i].p_filesz += adj;
+ }
+ else
+ adj = 0;
+ }
+
+ for (j = 1; j < dso->ehdr.e_shnum; ++j)
+ if (dso->shdr[j].sh_addr >= dso->phdr[i].p_vaddr
+ && dso->shdr[j].sh_addr + dso->shdr[j].sh_size
+ <= dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz)
+ {
+ last = j;
+ if (dso->shdr[j].sh_type == SHT_NOBITS)
+ {
+ last_offset += dso->shdr[j].sh_addralign - 1;
+ last_offset &= ~(dso->shdr[j].sh_addralign - 1);
+ if (last_offset > dso->phdr[i].p_offset
+ + dso->phdr[i].p_filesz)
+ last_offset = dso->phdr[i].p_offset
+ + dso->phdr[i].p_filesz;
+ dso->shdr[j].sh_offset = last_offset;
+ }
+ else if (dso->shdr[j].sh_addr + dso->shdr[j].sh_size
+ > dso->phdr[i].p_vaddr + dso->phdr[i].p_filesz)
+ {
+ error (0, 0, "%s: section spans beyond end of segment",
+ dso->filename);
+ goto error_out;
+ }
+ else
+ {
+ dso->shdr[j].sh_offset
+ = dso->phdr[i].p_offset + dso->shdr[j].sh_addr
+ - dso->phdr[i].p_vaddr;
+ last_offset = dso->shdr[j].sh_offset + dso->shdr[j].sh_size;
+ }
+ }
+
+ if (adj)
+ {
+ for (j = i + 1; j < dso->ehdr.e_phnum; ++j)
+ if (dso->phdr[j].p_type == PT_LOAD
+ && dso->phdr[j].p_vaddr >= dso->shdr[slast - 1].sh_addr)
+ {
+ dso->phdr[j].p_vaddr += adj;
+ dso->phdr[j].p_paddr += adj;
+ dso->phdr[j].p_offset += adj;
+ }
+
+ j = last + 1;
+ while (j < dso->ehdr.e_shnum
+ && (dso->shdr[j].sh_flags
+ & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR)))
+ {
+ dso->shdr[j].sh_offset += adj;
+ dso->shdr[j++].sh_addr += adj;
+ }
+
+ if (adjust_dso_nonalloc (dso, last + 1,
+ dso->shdr[sfirst].sh_offset,
+ adj))
+ goto error_out;
+ }
+ }
+
+ /* Adjust .rel*.dyn (or .rel*.*) if necessary. */
+ assert (new_reloc == -1
+ || (rinfo.last - rinfo.first
+ == (move->old_to_new[rinfo.last]
+ - move->old_to_new[rinfo.first])));
+ rinfo.first = move->old_to_new[rinfo.first];
+ rinfo.last = move->old_to_new[rinfo.last];
+ assert (new_reloc == -1 || rinfo.first == new[new_reloc]);
+
+ if (rinfo.rel_to_rela)
+ {
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ assert (new_reloc != -1);
+ for (j = rinfo.first; j <= rinfo.last; ++j)
+ {
+ dso->shdr[j].sh_size
+ = dso->shdr[j].sh_size / 3 * 2;
+ if (convert_rel_to_rela (dso, j))
+ goto error_out;
+ dso->shdr[j].sh_size = shdr[j].sh_size;
+ }
+ }
+ else if (rinfonew.rel_to_rela)
+ {
+ assert (new_reloc != -1);
+ for (j = rinfo.first; j <= rinfo.last; ++j)
+ {
+ dso->shdr[j].sh_entsize
+ = gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ dso->shdr[j].sh_type = SHT_RELA;
+ }
+ }
+
+ /* Adjust .rel*.plt if necessary. */
+ rinfo.plt = move->old_to_new[rinfo.plt];
+ if (new_plt != -1)
+ {
+ assert (rinfo.plt == new[new_plt]);
+ if (rinfo.rel_to_rela_plt)
+ {
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ dso->shdr[rinfo.first].sh_size
+ = dso->shdr[rinfo.first].sh_size / 3 * 2;
+ if (convert_rel_to_rela (dso, rinfo.plt))
+ goto error_out;
+ dso->shdr[rinfo.plt].sh_size = shdr[rinfo.plt].sh_size;
+ }
+ else if (rinfonew.rel_to_rela_plt)
+ {
+ dso->shdr[rinfo.plt].sh_entsize
+ = gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ dso->shdr[rinfo.plt].sh_type = SHT_RELA;
+ }
+ }
+
+ /* Add new strings into .dynstr if necessary. */
+ if (new_dynstr != -1)
+ {
+ Elf_Data *data;
+ char *ptr;
+
+ i = new[new_dynstr];
+ data = elf_getdata (dso->scn[i], NULL);
+ assert (data->d_off == 0);
+ data->d_buf = realloc (data->d_buf, dso->shdr[i].sh_size);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not append names needed for .gnu.liblist to .dynstr",
+ dso->filename);
+ goto error_out;
+ }
+ ptr = data->d_buf + shdr_after_undo[dynstrndxnew].sh_size;
+ data->d_size = dso->shdr[i].sh_size;
+ for (j = 0; j < ndeps - 1; ++j)
+ if (liblist[j].l_name == 0)
+ {
+ liblist[j].l_name = ptr - (char *) data->d_buf;
+ ptr = stpcpy (ptr, info->sonames[j + 1]) + 1;
+ }
+ assert (ptr == (char *) data->d_buf + data->d_size);
+ }
+
+ /* Create or update .sdynbss if necessary. */
+ if (new_sdynbss != -1)
+ {
+ Elf_Data *data;
+
+ if (old_sdynbss == -1)
+ {
+ dso->shdr[new_sdynbss] = dso->shdr[new_sdynbss + 1];
+
+ dso->shdr[new_sdynbss].sh_name = shstrtabadd (dso, ".sdynbss");
+ if (dso->shdr[new_sdynbss].sh_name == 0)
+ goto error_out;
+
+ dso->shdr[new_sdynbss].sh_size =
+ info->sdynbss_base + info->sdynbss_size
+ - dso->shdr[new_sdynbss].sh_addr;
+
+ dso->shdr[new_sdynbss + 1].sh_size
+ -= dso->shdr[new_sdynbss].sh_size;
+ dso->shdr[new_sdynbss + 1].sh_addr
+ += dso->shdr[new_sdynbss].sh_size;
+ dso->shdr[new_sdynbss + 1].sh_offset
+ += dso->shdr[new_sdynbss].sh_size;
+ dso->shdr[new_sdynbss].sh_type = SHT_PROGBITS;
+ }
+ else
+ {
+ if (dso->shdr[new_sdynbss].sh_type != SHT_PROGBITS
+ || dso->shdr[new_sdynbss].sh_addr > info->sdynbss_base
+ || dso->shdr[new_sdynbss].sh_addr
+ + dso->shdr[new_sdynbss].sh_size
+ < info->sdynbss_base + info->sdynbss_size)
+ {
+ error (0, 0, "%s: Copy relocs don't point into .sdynbss section",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ data = elf_getdata (dso->scn[new_sdynbss], NULL);
+ free (data->d_buf);
+ data->d_buf = info->sdynbss;
+ info->sdynbss = NULL;
+ data->d_off = info->sdynbss_base - dso->shdr[new_sdynbss].sh_addr;
+ data->d_size = info->sdynbss_size;
+ data->d_type = ELF_T_BYTE;
+ if (old_sdynbss == -1)
+ {
+ data = elf_getdata (dso->scn[new_sdynbss + 1], NULL);
+ assert (dso->shdr[new_sdynbss + 1].sh_type != SHT_NOBITS
+ || data->d_buf == NULL);
+ if (data->d_size != dso->shdr[new_sdynbss + 1].sh_size)
+ {
+ assert (data->d_size == dso->shdr[new_sdynbss].sh_size
+ + dso->shdr[new_sdynbss + 1].sh_size);
+ data->d_size -= dso->shdr[new_sdynbss].sh_size;
+ }
+ }
+ }
+
+ /* Create or update .dynbss if necessary. */
+ if (new_dynbss != -1)
+ {
+ Elf_Data *data;
+
+ if (old_dynbss == -1)
+ {
+ GElf_Addr adj;
+
+ dso->shdr[new_dynbss] = dso->shdr[new_dynbss + 1];
+
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[new_dynbss + 1].sh_name),
+ ".sbss")
+ && new_sdynbss == -1)
+ dso->shdr[new_dynbss].sh_name = shstrtabadd (dso, ".sdynbss");
+ else
+ dso->shdr[new_dynbss].sh_name = shstrtabadd (dso, ".dynbss");
+ if (dso->shdr[new_dynbss].sh_name == 0)
+ goto error_out;
+
+ dso->shdr[new_dynbss].sh_size =
+ info->dynbss_base + info->dynbss_size
+ - dso->shdr[new_dynbss].sh_addr;
+
+ dso->shdr[new_dynbss + 1].sh_size
+ -= dso->shdr[new_dynbss].sh_size;
+ dso->shdr[new_dynbss + 1].sh_addr
+ += dso->shdr[new_dynbss].sh_size;
+ dso->shdr[new_dynbss + 1].sh_offset
+ += dso->shdr[new_dynbss].sh_size;
+ dso->shdr[new_dynbss].sh_type = SHT_PROGBITS;
+
+ if (dso->shdr[new_dynbss + 1].sh_type == SHT_NOBITS)
+ {
+ GElf_Addr last_offset;
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->phdr[i].p_vaddr <= dso->shdr[new_dynbss].sh_addr
+ && dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz
+ >= info->dynbss_base + info->dynbss_size)
+ break;
+ assert (i < dso->ehdr.e_phnum);
+
+ for (j = new_dynbss - 1; j; --j)
+ {
+ if (dso->shdr[j].sh_addr < dso->phdr[i].p_vaddr)
+ break;
+ if (dso->shdr[j].sh_type == SHT_NOBITS
+ && (dso->shdr[j].sh_flags & SHF_TLS) == 0)
+ {
+ error (0, 0, "%s: COPY relocs not present at start of first SHT_NOBITS section",
+ dso->filename);
+ goto error_out;
+ }
+ }
+
+ if (dso->phdr[i].p_filesz
+ < info->dynbss_base + info->dynbss_size
+ - dso->phdr[i].p_vaddr)
+ {
+ dso->phdr[i].p_filesz =
+ info->dynbss_base + info->dynbss_size
+ - dso->phdr[i].p_vaddr;
+ assert (dso->phdr[i].p_filesz <= dso->phdr[i].p_memsz);
+ }
+
+ adj = dso->phdr[i].p_offset + dso->shdr[new_dynbss].sh_addr
+ - dso->phdr[i].p_vaddr - dso->shdr[new_dynbss].sh_offset;
+
+ dso->shdr[new_dynbss].sh_offset += adj;
+ dso->shdr[new_dynbss + 1].sh_offset += adj;
+
+ adj += dso->shdr[new_dynbss].sh_size;
+
+ for (j = new_dynbss + 2;
+ j < dso->ehdr.e_shnum
+ && (dso->shdr[j].sh_flags
+ & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR));
+ ++j)
+ if (dso->shdr[j].sh_addr >= dso->phdr[i].p_vaddr
+ + dso->phdr[i].p_memsz)
+ adj = (adj + dso->shdr[j].sh_addralign - 1)
+ & ~(dso->shdr[j].sh_addralign - 1);
+
+ for (j = i + 1; j < dso->ehdr.e_phnum; ++j)
+ if (dso->phdr[j].p_type == PT_LOAD
+ && dso->phdr[j].p_vaddr >= dso->shdr[new_dynbss].sh_addr)
+ {
+ dso->phdr[j].p_vaddr += adj;
+ dso->phdr[j].p_paddr += adj;
+ dso->phdr[j].p_offset += adj;
+ }
+
+ last_offset = dso->shdr[new_dynbss + 1].sh_offset;
+ for (j = new_dynbss + 2; j < dso->ehdr.e_shnum; ++j)
+ if (dso->shdr[j].sh_type != SHT_NOBITS
+ || dso->shdr[j].sh_addr < dso->phdr[i].p_vaddr
+ || dso->shdr[j].sh_addr + dso->shdr[j].sh_size
+ > dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz)
+ break;
+ else
+ {
+ last_offset += dso->shdr[j].sh_addralign - 1;
+ last_offset &= ~(dso->shdr[j].sh_addralign - 1);
+ if (last_offset > dso->phdr[i].p_offset
+ + dso->phdr[i].p_filesz)
+ last_offset = dso->phdr[i].p_offset
+ + dso->phdr[i].p_filesz;
+ dso->shdr[j].sh_offset = last_offset;
+ }
+
+ while (j < dso->ehdr.e_shnum
+ && (dso->shdr[j].sh_flags
+ & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR)))
+ {
+ dso->shdr[j].sh_offset += adj;
+ dso->shdr[j++].sh_addr += adj;
+ }
+
+ if (adjust_dso_nonalloc (dso, new_dynbss + 2,
+ dso->shdr[new_dynbss].sh_offset,
+ adj))
+ goto error_out;
+ }
+ }
+ else
+ {
+ if (dso->shdr[new_dynbss].sh_type != SHT_PROGBITS
+ || dso->shdr[new_dynbss].sh_addr > info->dynbss_base
+ || dso->shdr[new_dynbss].sh_addr
+ + dso->shdr[new_dynbss].sh_size
+ < info->dynbss_base + info->dynbss_size)
+ {
+ error (0, 0, "%s: Copy relocs don't point into .dynbss section",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ data = elf_getdata (dso->scn[new_dynbss], NULL);
+ free (data->d_buf);
+ data->d_buf = info->dynbss;
+ info->dynbss = NULL;
+ data->d_off = info->dynbss_base - dso->shdr[new_dynbss].sh_addr;
+ data->d_size = info->dynbss_size;
+ data->d_type = ELF_T_BYTE;
+ if (old_dynbss == -1)
+ {
+ data = elf_getdata (dso->scn[new_dynbss + 1], NULL);
+ if (dso->shdr[new_dynbss + 1].sh_type == SHT_NOBITS
+ && data->d_buf != NULL)
+ {
+#ifndef NDEBUG
+ char *buf_start = data->d_buf;
+ char *buf_end = buf_start + data->d_size;
+
+ while (buf_start < buf_end)
+ if (*buf_start++)
+ break;
+ assert (buf_start == buf_end);
+#endif
+ free (data->d_buf);
+ data->d_buf = NULL;
+ }
+ if (data->d_size != dso->shdr[new_dynbss + 1].sh_size)
+ {
+ assert (data->d_size == dso->shdr[new_dynbss].sh_size
+ + dso->shdr[new_dynbss + 1].sh_size);
+ data->d_size -= dso->shdr[new_dynbss].sh_size;
+ }
+ }
+ }
+
+ /* Create the liblist. */
+ i = new[new_liblist];
+ dso->shdr[i].sh_flags = shdr[i].sh_flags;
+ dso->shdr[i].sh_addralign = shdr[i].sh_addralign;
+ dso->shdr[i].sh_entsize = shdr[i].sh_entsize;
+ dso->shdr[i].sh_name = shstrtabadd (dso, ".gnu.liblist");
+ if (dso->shdr[i].sh_name == 0)
+ goto error_out;
+ else
+ {
+ Elf_Data *data;
+
+ dso->shdr[i].sh_link
+ = new_dynstr != -1 ? new[new_dynstr] : move->old_to_new[dynstrndx];
+ data = elf_getdata (dso->scn[i], NULL);
+ data->d_type = ELF_T_WORD;
+ data->d_size = (ndeps - 1) * sizeof (Elf32_Lib);
+ free (data->d_buf);
+ data->d_buf = liblist;
+ liblist = NULL;
+ data->d_off = 0;
+ data->d_align = sizeof (GElf_Word);
+ data->d_version = EV_CURRENT;
+ if (set_dynamic (dso, DT_GNU_LIBLIST, dso->shdr[i].sh_addr, 1))
+ goto error_out;
+ if (set_dynamic (dso, DT_GNU_LIBLISTSZ, dso->shdr[i].sh_size, 1))
+ goto error_out;
+ }
+
+ /* Create the conflict list if necessary. */
+ if (new_conflict != -1)
+ {
+ Elf_Data *data;
+
+ i = new[new_conflict];
+ data = elf_getdata (dso->scn[i], NULL);
+ data->d_type = ELF_T_RELA;
+ data->d_size = info->conflict_rela_size
+ * gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ data->d_off = 0;
+ data->d_align = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT);
+ data->d_version = EV_CURRENT;
+ if (data->d_size)
+ {
+ data->d_buf = realloc (data->d_buf, data->d_size);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not build .gnu.conflict section",
+ dso->filename);
+ goto error_out;
+ }
+ }
+ else
+ {
+ free (data->d_buf);
+ data->d_buf = NULL;
+ }
+ for (j = 0; j < info->conflict_rela_size; ++j)
+ gelfx_update_rela (dso->elf, data, j, info->conflict_rela + j);
+ free (info->conflict_rela);
+ info->conflict_rela = NULL;
+
+ dso->shdr[i].sh_flags = shdr[i].sh_flags;
+ dso->shdr[i].sh_addralign = shdr[i].sh_addralign;
+ dso->shdr[i].sh_entsize = shdr[i].sh_entsize;
+ for (j = 1; j < dso->ehdr.e_shnum; ++j)
+ if (dso->shdr[j].sh_type == SHT_DYNSYM)
+ break;
+ assert (j < dso->ehdr.e_shnum);
+ dso->shdr[i].sh_link = j;
+ dso->shdr[i].sh_name = shstrtabadd (dso, ".gnu.conflict");
+ if (dso->shdr[i].sh_name == 0)
+ goto error_out;
+ if (set_dynamic (dso, DT_GNU_CONFLICT, dso->shdr[i].sh_addr, 1))
+ goto error_out;
+ if (set_dynamic (dso, DT_GNU_CONFLICTSZ, dso->shdr[i].sh_size, 1))
+ goto error_out;
+ }
+
+ if (undo != -1)
+ {
+ Elf_Scn *scn;
+ Elf_Data *data;
+ GElf_Addr newoffset;
+
+ dso->shdr[undo].sh_name = shstrtabadd (dso, ".gnu.prelink_undo");
+ if (dso->shdr[undo].sh_name == 0)
+ return 1;
+ dso->shdr[undo].sh_offset = dso->shdr[undo - 1].sh_offset;
+ if (dso->shdr[undo - 1].sh_type != SHT_NOBITS)
+ dso->shdr[undo].sh_offset += dso->shdr[undo - 1].sh_size;
+ dso->shdr[undo].sh_entsize = 1;
+ dso->shdr[undo].sh_size = dso->undo.d_size;
+ newoffset = dso->shdr[undo].sh_offset + dso->undo.d_align - 1;
+ newoffset &= ~(dso->shdr[undo].sh_addralign - 1);
+ if (adjust_dso_nonalloc (dso, undo + 1, dso->shdr[undo].sh_offset,
+ dso->undo.d_size + newoffset
+ - dso->shdr[undo].sh_offset))
+ return 1;
+ dso->shdr[undo].sh_offset = newoffset;
+ scn = dso->scn[undo];
+ data = elf_getdata (scn, NULL);
+ assert (data != NULL && elf_getdata (scn, data) == NULL);
+ free (data->d_buf);
+ *data = dso->undo;
+ dso->undo.d_buf = NULL;
+ }
+
+ recompute_nonalloc_offsets (dso);
+
+ if (update_dynamic_tags (dso, dso->shdr, old_shdr, move))
+ goto error_out;
+
+ if (update_dynamic_rel (dso, &rinfo))
+ goto error_out;
+
+ free (move);
+ return 0;
+}
diff --git a/trunk/src/execle_open.c b/trunk/src/execle_open.c
new file mode 100644
index 0000000..52872aa
--- /dev/null
+++ b/trunk/src/execle_open.c
@@ -0,0 +1,80 @@
+/* Copyright (C) 2001 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 <errno.h>
+#include <error.h>
+#include <stdio.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static pid_t pid;
+
+int
+execve_close (FILE *f)
+{
+ pid_t p;
+ int status;
+
+ if (f != NULL)
+ fclose (f);
+ while ((p = waitpid (pid, &status, 0)) == -1 && errno == EINTR);
+ if (p == -1 || ! WIFEXITED (status))
+ return -1;
+ return WEXITSTATUS (status);
+}
+
+FILE *
+execve_open (const char *path, char *const argv[], char *const envp[])
+{
+ int p[2];
+ FILE *f;
+
+ if (pipe (p) < 0)
+ {
+ error (0, errno, "Could not run %s", path);
+ return NULL;
+ }
+
+ switch (vfork ())
+ {
+ case -1:
+ error (0, errno, "Could not run %s", path);
+ return NULL;
+ case 0:
+ close (p[0]);
+ if (p[1] != 1)
+ {
+ dup2 (p[1], 1);
+ close (p[1]);
+ }
+ dup2 (1, 2);
+ execve (path, argv, envp);
+ _exit (127);
+ }
+
+ close (p[1]);
+
+ f = fdopen (p[0], "r");
+ if (f == NULL)
+ {
+ close (p[0]);
+ execve_close (NULL);
+ }
+
+ return f;
+}
diff --git a/trunk/src/execstack.c b/trunk/src/execstack.c
new file mode 100644
index 0000000..f38fead
--- /dev/null
+++ b/trunk/src/execstack.c
@@ -0,0 +1,459 @@
+/* Copyright (C) 2003, 2005 Red Hat, Inc.
+ Written by Jakub Jelinek <jakub@redhat.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 <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#include "prelink.h"
+
+int set;
+int execflag;
+
+const char *argp_program_version = "execstack 1.0 (20061201) Wind River";
+
+const char *argp_program_bug_address = "<support@windriver.com>";
+
+static char argp_doc[] = "execstack -- program to query or set executable stack flag";
+
+static struct argp_option options[] = {
+ {"set-execstack", 's', 0, 0, "Set executable stack flag bit" },
+ {"execstack", 's', 0, OPTION_HIDDEN, "" },
+ {"clear-execstack", 'c', 0, 0, "Clear executable stack flag bit" },
+ {"noexecstack", 'c', 0, OPTION_HIDDEN, "" },
+ {"query", 'q', 0, 0, "Query executable stack flag bit" },
+ { 0 }
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case 's':
+ set = 1;
+ execflag = 1;
+ break;
+ case 'c':
+ set = 1;
+ execflag = 0;
+ break;
+ case 'q':
+ set = 0;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+static struct argp argp = { options, parse_opt, 0, argp_doc };
+
+static int execstack_set (DSO *dso, int flag);
+
+static void
+execstack_fill_phdr (DSO *dso, int i, int flag)
+{
+ memset (&dso->phdr[i], 0, sizeof (dso->phdr[i]));
+ dso->phdr[i].p_type = PT_GNU_STACK;
+ dso->phdr[i].p_flags = PF_W | PF_R | (flag ? PF_X : 0);
+ dso->phdr[i].p_align = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT);
+}
+
+static int
+execstack_make_rdwr (DSO *dso, int flag)
+{
+ int i, fd = -1, status;
+ pid_t pid;
+ DSO *ndso = NULL;
+ char *p = NULL;
+ char filename[strlen (dso->filename) + sizeof ".#execstack#.XXXXXX"];
+
+ for (i = 0; i < dso->ehdr.e_shnum; ++i)
+ {
+ const char *name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name);
+ if (strcmp (name, ".gnu.prelink_undo") == 0)
+ break;
+ }
+
+ if (i == dso->ehdr.e_shnum)
+ return reopen_dso (dso, NULL, NULL) ? 1 : -1;
+
+ /* We need to unprelink the file first, so that prelink --undo
+ or reprelinking it doesn't destroy the PT_GNU_STACK segment
+ header we've created. */
+ sprintf (filename, "%s.#execstack#.XXXXXX", dso->filename);
+
+ fd = wrap_mkstemp (filename);
+ if (fd == -1)
+ {
+ error (0, 0, "%s: Cannot create temporary file",
+ dso->filename);
+ goto error_out;
+ }
+
+ p = strdup (dso->filename);
+ if (p == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot create temporary file",
+ dso->filename);
+ goto error_out;
+ }
+
+ pid = vfork ();
+ if (pid == 0)
+ {
+ close (fd);
+ execlp ("prelink", "prelink", "-u", "-o", filename,
+ dso->filename, NULL);
+ execl (SBINDIR "/prelink", "prelink", "-u", "-o", filename,
+ dso->filename, NULL);
+ _exit (-1);
+ }
+
+ if (pid < 0)
+ {
+ error (0, errno, "%s: Cannot run prelink --undo",
+ dso->filename);
+ goto error_out;
+ }
+
+ if (waitpid (pid, &status, 0) < 0
+ || !WIFEXITED (status)
+ || WEXITSTATUS (status))
+ {
+ error (0, 0, "%s: prelink --undo failed", dso->filename);
+ goto error_out;
+ }
+
+ ndso = open_dso (filename);
+ if (ndso == NULL)
+ {
+ error (0, 0, "%s: Couldn't open prelink --undo output",
+ dso->filename);
+ goto error_out;
+ }
+
+ for (i = 0; i < ndso->ehdr.e_shnum; ++i)
+ {
+ const char *name = strptr (ndso, ndso->ehdr.e_shstrndx,
+ ndso->shdr[i].sh_name);
+ if (strcmp (name, ".gnu.prelink_undo") == 0)
+ break;
+ }
+
+ if (i != ndso->ehdr.e_shnum)
+ {
+ error (0, 0, "%s: prelink --undo output contains .gnu.prelink_undo section",
+ dso->filename);
+ goto error_out;
+ }
+
+ if (ndso->ehdr.e_type != dso->ehdr.e_type)
+ {
+ error (0, 0, "%s: Object type changed during prelink --undo operation",
+ dso->filename);
+ }
+
+ if (ndso->filename != ndso->soname)
+ free ((char *) ndso->filename);
+ ndso->filename = p;
+ p = NULL;
+
+ wrap_unlink (filename);
+ close (fd);
+ fd = -1;
+ close_dso (dso);
+ return execstack_set (ndso, flag);
+
+error_out:
+ free (p);
+ if (ndso != NULL)
+ close_dso (ndso);
+ if (fd != -1)
+ {
+ wrap_unlink (filename);
+ close (fd);
+ }
+ close_dso (dso);
+ return 1;
+}
+
+static int
+execstack_set (DSO *dso, int flag)
+{
+ int i, null = -1, last, ret;
+ GElf_Addr lowoff = ~(GElf_Addr) 0, start = 0, align = 0;
+ GElf_Addr adjust;
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_GNU_STACK)
+ {
+ /* Found PT_GNU_STACK. Check if we need any change or not. */
+ if (flag ^ ((dso->phdr[i].p_flags & PF_X) != 0))
+ {
+ ret = execstack_make_rdwr (dso, flag);
+ if (ret != -1)
+ return ret;
+ dso->phdr[i].p_flags ^= PF_X;
+ goto out_write;
+ }
+ else
+ goto out_close;
+ }
+ else if (dso->phdr[i].p_type == PT_NULL)
+ null = i;
+
+ if (null != -1)
+ {
+ /* Overwrite PT_NULL segment with PT_GNU_STACK. */
+ ret = execstack_make_rdwr (dso, flag);
+ if (ret != -1)
+ return ret;
+ execstack_fill_phdr (dso, i, flag);
+ goto out_write;
+ }
+
+ if (dso->ehdr.e_shnum == 0)
+ {
+ error (0, 0, "%s: Section header table missing", dso->filename);
+ goto error_out;
+ }
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ if (lowoff > dso->shdr[i].sh_offset)
+ {
+ if (dso->shdr[i].sh_flags & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR))
+ {
+ lowoff = dso->shdr[i].sh_offset;
+ start = dso->shdr[i].sh_addr;
+ }
+ else
+ {
+ error (0, 0, "%s: Non-alloced sections before alloced ones",
+ dso->filename);
+ goto error_out;
+ }
+ }
+
+ if (dso->shdr[i].sh_addralign > align)
+ align = dso->shdr[i].sh_addralign;
+ }
+
+ if (dso->ehdr.e_phoff >= lowoff)
+ {
+ error (0, 0, "%s: Program header table not before all sections",
+ dso->filename);
+ goto error_out;
+ }
+
+ if (dso->ehdr.e_shoff <= lowoff)
+ {
+ error (0, 0, "%s: Section header table before first section",
+ dso->filename);
+ goto error_out;
+ }
+
+ if (dso->ehdr.e_phoff + (dso->ehdr.e_phnum + 1) * dso->ehdr.e_phentsize
+ <= lowoff)
+ {
+ /* There is enough space for the headers even without reshuffling
+ anything. */
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_PHDR)
+ {
+ if (dso->phdr[i].p_filesz
+ == dso->ehdr.e_phnum * dso->ehdr.e_phentsize)
+ dso->phdr[i].p_filesz += dso->ehdr.e_phentsize;
+ if (dso->phdr[i].p_memsz
+ == dso->ehdr.e_phnum * dso->ehdr.e_phentsize)
+ dso->phdr[i].p_memsz += dso->ehdr.e_phentsize;
+ }
+ i = dso->ehdr.e_phnum++;
+ ret = execstack_make_rdwr (dso, flag);
+ if (ret != -1)
+ return ret;
+ execstack_fill_phdr (dso, i, flag);
+ goto out_write;
+ }
+
+ if (dso->ehdr.e_type != ET_DYN)
+ {
+ error (0, 0, "%s: Reshuffling of objects to make room for\n"
+ "program header entry only supported for shared libraries",
+ dso->filename);
+ goto error_out;
+ }
+
+ adjust = dso->ehdr.e_phoff + (dso->ehdr.e_phnum + 1) * dso->ehdr.e_phentsize
+ - lowoff;
+ if (align)
+ adjust = (adjust + align - 1) & ~(align - 1);
+
+ /* Need to make sure adjust doesn't cause different Phdr segments
+ to overlap on the same page. */
+ last = -1;
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz >= start)
+ {
+ if (last != -1
+ && (((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz - 1)
+ ^ dso->phdr[i].p_vaddr)
+ & ~(dso->arch->max_page_size - 1))
+ && !(((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz
+ + adjust - 1)
+ ^ (dso->phdr[i].p_vaddr + adjust))
+ & ~(dso->arch->max_page_size - 1)))
+ {
+ if (align >= dso->arch->max_page_size)
+ {
+ error (0, 0, "%s: Cannot grow reloc sections", dso->filename);
+ goto error_out;
+ }
+ adjust = (adjust + dso->arch->max_page_size - 1)
+ & ~(dso->arch->max_page_size - 1);
+ }
+ last = i;
+ }
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_PHDR)
+ {
+ if (dso->phdr[i].p_filesz == dso->ehdr.e_phnum * dso->ehdr.e_phentsize)
+ dso->phdr[i].p_filesz += dso->ehdr.e_phentsize;
+ if (dso->phdr[i].p_memsz == dso->ehdr.e_phnum * dso->ehdr.e_phentsize)
+ dso->phdr[i].p_memsz += dso->ehdr.e_phentsize;
+ }
+
+ i = dso->ehdr.e_phnum++;
+ ret = execstack_make_rdwr (dso, flag);
+ if (ret != -1)
+ return ret;
+
+ if (adjust_dso (dso, start, adjust))
+ goto error_out;
+
+ execstack_fill_phdr (dso, i, flag);
+
+out_write:
+ if (dynamic_info_is_set (dso, DT_CHECKSUM_BIT)
+ && dso_is_rdwr (dso)
+ && prelink_set_checksum (dso))
+ goto error_out;
+
+ dso->permissive = 1;
+
+ return update_dso (dso, NULL);
+
+out_close:
+ close_dso (dso);
+ return 0;
+
+error_out:
+ close_dso (dso);
+ return 1;
+}
+
+static int
+execstack_query (DSO *dso)
+{
+ int stack = '?', i;
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_GNU_STACK)
+ {
+ stack = (dso->phdr[i].p_flags & PF_X) ? 'X' : '-';
+ break;
+ }
+ printf ("%c %s\n", stack, dso->filename);
+ close_dso (dso);
+ return 0;
+}
+
+int
+main (int argc, char *argv[])
+{
+ int remaining, failures = 0;
+
+ setlocale (LC_ALL, "");
+
+ argp_parse (&argp, argc, argv, 0, &remaining, 0);
+
+ elf_version (EV_CURRENT);
+
+ if (remaining == argc)
+ error (EXIT_FAILURE, 0, "no files given");
+
+ while (remaining < argc)
+ {
+ DSO *dso = open_dso (argv[remaining++]);
+ int ret;
+
+ if (dso == NULL)
+ {
+ ++failures;
+ continue;
+ }
+
+ if (dso->ehdr.e_type != ET_DYN
+ && dso->ehdr.e_type != ET_EXEC)
+ {
+ ++failures;
+ error (0, 0, "%s is not a shared library nor executable", dso->filename);
+ continue;
+ }
+
+ if (set)
+ ret = execstack_set (dso, execflag);
+ else
+ ret = execstack_query (dso);
+
+ if (ret)
+ ++failures;
+ }
+
+ return failures;
+}
+
+/* FIXME: Dummy. When arch dependent files are split into adjust and prelink
+ parts, this can go away. */
+struct prelink_conflict *
+prelink_conflict (struct prelink_info *info, GElf_Word r_sym, int reloc_type)
+{
+ abort ();
+}
+
+GElf_Rela *
+prelink_conflict_add_rela (struct prelink_info *info)
+{
+ abort ();
+}
+
+GElf_Addr mmap_reg_start;
+GElf_Addr mmap_reg_end;
+int exec_shield;
diff --git a/trunk/src/fptr.c b/trunk/src/fptr.c
new file mode 100644
index 0000000..7602d5e
--- /dev/null
+++ b/trunk/src/fptr.c
@@ -0,0 +1,458 @@
+/* Copyright (C) 2001, 2002, 2003 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 "fptr.h"
+
+struct opd_refent;
+
+struct opd_tabent
+{
+ struct opd_ent *ent;
+ struct opd_refent *ref;
+};
+
+struct opd_refent
+{
+ GElf_Addr val;
+ GElf_Addr gp;
+ struct opd_refent *first;
+ struct opd_tabent *tabent;
+ struct opd_refent *next, *nextref;
+ GElf_Word refcnt;
+};
+
+struct opd_fptr
+{
+ /* The first 2 fields have to match opd_refent. */
+ GElf_Addr val;
+ GElf_Addr gp;
+ struct opd_ent *ent;
+};
+
+static void
+opd_del (void *p)
+{
+ free (p);
+}
+
+static hashval_t
+opd_tabent_hash (const void *p)
+{
+ struct opd_tabent *e = (struct opd_tabent *)p;
+
+ return e->ent->opd;
+}
+
+static int
+opd_tabent_eq (const void *p, const void *q)
+{
+ struct opd_tabent *e = (struct opd_tabent *)p;
+ struct opd_tabent *f = (struct opd_tabent *)q;
+
+ return e->ent == f->ent;
+}
+
+static hashval_t
+opd_refent_hash (const void *p)
+{
+ struct opd_refent *e = (struct opd_refent *)p;
+
+ return e->val ^ (e->val >> 31);
+}
+
+static int
+opd_refent_eq (const void *p, const void *q)
+{
+ struct opd_refent *e = (struct opd_refent *)p;
+ struct opd_refent *f = (struct opd_refent *)q;
+
+ return e->val == f->val && e->gp == f->gp;
+}
+
+static int
+opd_gather_refent (void **p, void *info)
+{
+ struct opd_refent ***ptr = (struct opd_refent ***) info;
+ struct opd_refent *r = *(struct opd_refent **) p, *t;
+
+ for (t = r; t; t = t->next)
+ {
+ *(*ptr)++ = t;
+ t->first = r;
+ }
+ return 1;
+}
+
+static int
+opd_refent_cmp (const void *A, const void *B)
+{
+ struct opd_refent *a = * (struct opd_refent **) A;
+ struct opd_refent *b = * (struct opd_refent **) B;
+
+ if (a->refcnt > b->refcnt)
+ return -1;
+ if (a->refcnt < b->refcnt)
+ return 1;
+ return 0;
+}
+
+int
+opd_init (struct prelink_info *info)
+{
+ int i, j, nrefent = 0;
+ struct opd_lib *l;
+ struct opd_refent refent, *r, **refarr, **a;
+ struct opd_tabent tabent, *t;
+ void **tabslot;
+ htab_t tabent_htab = NULL, refent_htab = NULL;
+
+ l = calloc (sizeof (struct opd_lib), 1);
+ if (l == NULL)
+ goto error_mem;
+ l->nrefs = (info->symtab_end - info->symtab_start) / info->symtab_entsize;
+ if (l->nrefs)
+ {
+ l->u.refp = calloc (l->nrefs, sizeof (struct opd_ref *));
+ if (l->u.refp == NULL)
+ goto error_mem;
+ }
+ else
+ l->u.refp = NULL;
+ tabent_htab = htab_try_create (100, opd_tabent_hash, opd_tabent_eq, opd_del);
+ refent_htab = htab_try_create (100, opd_refent_hash, opd_refent_eq, opd_del);
+ l->htab = htab_try_create (100, opd_refent_hash, opd_refent_eq, opd_del);
+ if (tabent_htab == NULL || refent_htab == NULL || l->htab == NULL)
+ goto error_mem;
+
+ for (i = 0; i < info->ent->ndepends; ++i)
+ {
+ struct prelink_entry *ent;
+ struct prelink_conflict *conflict;
+ struct opd_lib *ol;
+
+ ent = info->ent->depends[i];
+ ol = ent->opd;
+ for (j = 0; j < ol->nrefs; ++j)
+ {
+ refent.val = ol->u.refs[j].ent->val;
+ refent.gp = ol->u.refs[j].ent->gp;
+ for (conflict = info->conflicts[i + 1]; conflict;
+ conflict = conflict->next)
+ {
+ if (conflict->symoff == ol->u.refs[j].symoff
+ && conflict->reloc_class != RTYPE_CLASS_COPY
+ && conflict->reloc_class != RTYPE_CLASS_TLS)
+ break;
+ }
+
+ if (conflict)
+ {
+ if (refent.val
+ != conflict->conflict.ent->base + conflict->conflictval
+ || refent.gp != conflict->conflict.ent->pltgot)
+ {
+ error (0, 0, "%s: OPD value changed during prelinking",
+ info->ent->filename);
+ goto error_out;
+ }
+
+ refent.val = conflict->lookup.ent->base + conflict->lookupval;
+ refent.gp = conflict->lookup.ent->pltgot;
+ }
+
+ if (ol->u.refs[j].ent->opd & OPD_ENT_PLT)
+ {
+ struct opd_ent_plt *entp
+ = (struct opd_ent_plt *) ol->u.refs[j].ent;
+ int k;
+
+ for (k = 0; k < info->ent->ndepends; ++k)
+ if (info->ent->depends[k] == entp->lib)
+ break;
+
+ assert (k < info->ent->ndepends);
+
+ for (conflict = info->conflicts[k + 1]; conflict;
+ conflict = conflict->next)
+ {
+ if (conflict->symoff == entp->symoff
+ && conflict->reloc_class == RTYPE_CLASS_PLT)
+ break;
+ }
+
+ if (conflict)
+ {
+ if (ol->u.refs[j].ent->val
+ != conflict->conflict.ent->base + conflict->conflictval
+ || ol->u.refs[j].ent->gp
+ != conflict->conflict.ent->pltgot)
+ {
+ error (0, 0, "%s: OPD value changed during prelinking",
+ info->ent->filename);
+ goto error_out;
+ }
+
+ /* FPTR originally pointed into .plt, but since they
+ now resolve to different values, this cannot be used. */
+ if (refent.val
+ != conflict->lookup.ent->base + conflict->lookupval
+ || refent.gp != conflict->lookup.ent->pltgot)
+ continue;
+ }
+ else if (refent.val != ol->u.refs[j].ent->val
+ || refent.gp != ol->u.refs[j].ent->gp)
+ continue;
+ }
+
+ tabslot = htab_find_slot (refent_htab, &refent, INSERT);
+ if (tabslot == NULL)
+ goto error_mem;
+
+ if (*tabslot != NULL)
+ {
+ for (r = (struct opd_refent *) *tabslot; r; r = r->next)
+ if (r->tabent->ent == ol->u.refs[j].ent)
+ {
+ r->refcnt += ol->u.refs[j].refcnt;
+ break;
+ }
+
+ if (r)
+ continue;
+ }
+
+ r = (struct opd_refent *) calloc (sizeof (struct opd_refent), 1);
+ if (r == NULL)
+ goto error_mem;
+
+ ++nrefent;
+ r->next = (struct opd_refent *) *tabslot;
+ *tabslot = r;
+ r->val = refent.val;
+ r->gp = refent.gp;
+ r->refcnt = ol->u.refs[j].refcnt;
+
+ tabent.ent = ol->u.refs[j].ent;
+
+ tabslot = htab_find_slot (tabent_htab, &tabent, INSERT);
+ if (tabslot == NULL)
+ goto error_mem;
+
+ if (*tabslot != NULL)
+ {
+ t = (struct opd_tabent *) *tabslot;
+ t->ref->nextref = r;
+ r->nextref = t->ref;
+ }
+ else
+ {
+ t = (struct opd_tabent *) calloc (sizeof (struct opd_tabent), 1);
+ if (t == NULL)
+ goto error_mem;
+ t->ent = ol->u.refs[j].ent;
+ *tabslot = t;
+ r->nextref = r;
+ t->ref = r;
+ }
+
+ r->tabent = t;
+ }
+ }
+
+ refarr = alloca (nrefent * sizeof (struct opd_refent *));
+ a = refarr;
+ htab_traverse (refent_htab, opd_gather_refent, &a);
+ assert (a == refarr + nrefent);
+ qsort (refarr, nrefent, sizeof (struct opd_refent *), opd_refent_cmp);
+ for (i = 0; i < nrefent; ++i)
+ {
+ struct opd_fptr *f;
+
+ if (refarr[i]->tabent == NULL)
+ continue;
+
+ f = (struct opd_fptr *) calloc (sizeof (struct opd_fptr), 1);
+ if (f == NULL)
+ goto error_mem;
+
+ f->val = refarr[i]->val;
+ f->gp = refarr[i]->gp;
+ f->ent = refarr[i]->tabent->ent;
+ tabslot = htab_find_slot (l->htab, f, INSERT);
+ if (tabslot == NULL)
+ goto error_mem;
+
+ *tabslot = f;
+ r = refarr[i]->tabent->ref;
+ do
+ {
+ if (r != refarr[i])
+ r->tabent = NULL;
+ r = r->nextref;
+ }
+ while (r != refarr[i]->tabent->ref);
+
+ for (r = refarr[i]->first; r; r = r->next)
+ r->tabent = NULL;
+ }
+
+ htab_delete (tabent_htab);
+ htab_delete (refent_htab);
+ info->ent->opd = l;
+ return 0;
+
+error_mem:
+ error (0, ENOMEM, "%s: Could not create OPD table",
+ info->ent->filename);
+error_out:
+ if (tabent_htab)
+ htab_delete (tabent_htab);
+ if (refent_htab)
+ htab_delete (refent_htab);
+ if (l && l->htab)
+ htab_delete (l->htab);
+ free (l);
+ return 1;
+}
+
+int
+opd_add (struct prelink_info *info, GElf_Word r_sym, int reloc_type)
+{
+ struct opd_fptr *f, fp;
+ void **tabslot;
+ struct opd_lib *l = info->ent->opd;
+
+ if (l->u.refp[r_sym] != NULL)
+ {
+ ++l->u.refp[r_sym]->refcnt;
+ return 0;
+ }
+
+ if (ELF64_ST_BIND (info->symtab [r_sym].st_info)
+ == STB_LOCAL)
+ {
+ fp.val = info->symtab [r_sym].st_value;
+ fp.gp = info->ent->pltgot;
+ }
+ else
+ {
+ fp.val = info->resolve (info, r_sym, reloc_type);
+ if (info->resolveent == NULL)
+ return 0;
+ fp.gp = info->resolveent->pltgot;
+ }
+
+ l->u.refp[r_sym] = malloc (sizeof (struct opd_ref));
+ if (l->u.refp[r_sym] == NULL)
+ goto error_mem;
+ l->u.refp[r_sym]->symoff = r_sym;
+ l->u.refp[r_sym]->refcnt = 1;
+ l->u.refp[r_sym]->ent = NULL;
+
+ tabslot = htab_find_slot (l->htab, &fp, INSERT);
+ if (tabslot == NULL)
+ goto error_mem;
+
+ if (*tabslot == NULL)
+ {
+ f = calloc (sizeof (struct opd_fptr), 1);
+ if (f == NULL)
+ goto error_mem;
+ f->val = fp.val;
+ f->gp = fp.gp;
+ *tabslot = f;
+ }
+
+ l->u.refp[r_sym]->ent = *tabslot;
+ return 0;
+
+error_mem:
+ error (0, ENOMEM, "%s: Could not create OPD table",
+ info->ent->filename);
+ return 1;
+}
+
+void
+opd_note_plt (struct prelink_info *info, GElf_Word r_sym, int reloc_type,
+ GElf_Addr r_offset)
+{
+ struct opd_fptr *f, fp;
+ struct opd_lib *l = info->ent->opd;
+ struct opd_ent_plt *entp;
+
+ if (ELF64_ST_BIND (info->symtab [r_sym].st_info)
+ == STB_LOCAL)
+ {
+ fp.val = info->symtab [r_sym].st_value;
+ fp.gp = info->ent->pltgot;
+ }
+ else
+ {
+ fp.val = info->resolve (info, r_sym, reloc_type);
+ if (info->resolveent == NULL)
+ return;
+ fp.gp = info->resolveent->pltgot;
+ }
+
+ f = (struct opd_fptr *) htab_find (l->htab, &fp);
+ if (f == NULL || f->ent != NULL)
+ return;
+
+ entp = calloc (sizeof (struct opd_ent_plt), 1);
+ if (entp == NULL)
+ return;
+
+ entp->v.val = fp.val;
+ entp->v.gp = fp.gp;
+ entp->v.opd = (r_offset - l->plt_start) | (OPD_ENT_PLT | OPD_ENT_NEW);
+ entp->lib = info->ent;
+ entp->symoff = r_sym;
+ f->ent = &entp->v;
+}
+
+GElf_Addr
+opd_size (struct prelink_info *info, GElf_Word entsize)
+{
+ struct opd_lib *l = info->ent->opd;
+ int i;
+ GElf_Addr ret = 0;
+ struct opd_ent *e;
+ struct opd_fptr *f;
+
+ for (i = 0; i < l->nrefs; ++i)
+ if ((f = (struct opd_fptr *) l->u.refp[i]->ent)->ent == NULL)
+ {
+ e = calloc (sizeof (struct opd_ent), 1);
+ if (e == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not create OPD table",
+ info->ent->filename);
+ return -1;
+ }
+
+ e->val = f->val;
+ e->gp = f->gp;
+ e->opd = ret | OPD_ENT_NEW;
+ ret += entsize;
+ }
+
+ return ret;
+}
diff --git a/trunk/src/fptr.h b/trunk/src/fptr.h
new file mode 100644
index 0000000..36ef7c6
--- /dev/null
+++ b/trunk/src/fptr.h
@@ -0,0 +1,67 @@
+/* Copyright (C) 2001 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. */
+
+#ifndef FPTR_H
+#define FPTR_H
+
+#include "prelink.h"
+#include "hashtab.h"
+
+struct opd_ent
+{
+ GElf_Addr val;
+ GElf_Addr gp;
+ GElf_Addr opd;
+#define OPD_ENT_PLT 1
+#define OPD_ENT_NEW 2
+};
+
+struct opd_ent_plt
+{
+ struct opd_ent v;
+ struct prelink_entry *lib;
+ GElf_Word symoff;
+};
+
+struct opd_ref
+{
+ GElf_Word symoff;
+ GElf_Word refcnt;
+ struct opd_ent *ent;
+};
+
+struct opd_lib
+{
+ GElf_Addr symtab_start;
+ GElf_Addr opd_start;
+ GElf_Addr plt_start;
+ union
+ {
+ struct opd_ref *refs;
+ struct opd_ref **refp;
+ } u;
+ htab_t htab;
+ int nrefs;
+};
+
+int opd_init (struct prelink_info *info);
+int opd_add (struct prelink_info *info, GElf_Word r_sym, int reloc_type);
+void opd_note_plt (struct prelink_info *info, GElf_Word r_sym, int reloc_type,
+ GElf_Addr r_offset);
+GElf_Addr opd_size (struct prelink_info *info, GElf_Word entsize);
+
+#endif /* FPTR_H */
diff --git a/trunk/src/gather.c b/trunk/src/gather.c
new file mode 100644
index 0000000..9219fdf
--- /dev/null
+++ b/trunk/src/gather.c
@@ -0,0 +1,1412 @@
+/* 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 <fnmatch.h>
+#include <ftw.h>
+#include <glob.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "prelinktab.h"
+#include "reloc.h"
+
+#ifndef HAVE_FTW_ACTIONRETVAL
+# define FTW_ACTIONRETVAL 0
+# define FTW_CONTINUE 0
+# define FTW_STOP 1
+#endif
+
+static int gather_lib (struct prelink_entry *ent);
+static int implicit;
+
+static struct prelink_dir *dirs;
+static struct prelink_dir *blacklist;
+#ifndef HAVE_FTW_ACTIONRETVAL
+static char *blacklist_dir;
+static size_t blacklist_dir_len;
+#endif
+static struct extension
+{
+ const char *ext;
+ size_t len;
+ int is_glob;
+} *blacklist_ext;
+static int blacklist_next;
+
+static int
+gather_deps (DSO *dso, struct prelink_entry *ent)
+{
+ int i, j, seen = 0;
+ FILE *f = NULL;
+ 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;
+ size_t len = 0;
+ ssize_t n;
+ Elf_Scn *scn;
+ Elf_Data *data;
+ Elf32_Lib *liblist = NULL;
+ int nliblist = 0;
+ const char *dl;
+ const char *ent_filename;
+
+ if (check_dso (dso))
+ {
+ if (! undo)
+ ent->type = ET_UNPRELINKABLE;
+ goto error_out;
+ }
+
+ ent->pltgot = dso->info[DT_PLTGOT];
+ ent->soname = strdup (dso->soname);
+ ent->flags = (dso->arch->class == ELFCLASS64 ? PCF_ELF64 : 0)
+ | (dso->arch->machine & PCF_MACHINE);
+ if (ent->soname == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not record SONAME", ent->filename);
+ goto error_out;
+ }
+
+ dl = dynamic_linker ?: dso->arch->dynamic_linker;
+ if (strcmp (dso->filename, dl) == 0
+ || is_ldso_soname (dso->soname))
+ {
+ if (dynamic_info_is_set (dso, DT_GNU_PRELINKED_BIT)
+ && dynamic_info_is_set (dso, DT_CHECKSUM_BIT))
+ {
+ if (! undo && dso->arch->read_opd)
+ dso->arch->read_opd (dso, ent);
+ ent->done = 2;
+ }
+ close_dso (dso);
+ return 0;
+ }
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ const char *name;
+ if (dso->shdr[i].sh_type == SHT_GNU_LIBLIST
+ && (name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name))
+ && ! strcmp (name, ".gnu.liblist")
+ && (dso->shdr[i].sh_size % sizeof (Elf32_Lib)) == 0)
+ {
+ nliblist = dso->shdr[i].sh_size / sizeof (Elf32_Lib);
+ liblist = (Elf32_Lib *) alloca (dso->shdr[i].sh_size);
+ scn = dso->scn[i];
+ data = elf_getdata (scn, NULL);
+ if (data == NULL || elf_getdata (scn, data)
+ || data->d_buf == NULL || data->d_off
+ || data->d_size != dso->shdr[i].sh_size)
+ liblist = NULL;
+ else
+ memcpy (liblist, data->d_buf, dso->shdr[i].sh_size);
+ if (! undo)
+ break;
+ }
+ else if (undo
+ && dso->shdr[i].sh_type == SHT_PROGBITS
+ && (name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name))
+ && ! strcmp (name, ".gnu.prelink_undo"))
+ ent->done = 2;
+ }
+
+ if (! undo && dso->arch->read_opd)
+ dso->arch->read_opd (dso, ent);
+ close_dso (dso);
+ dso = NULL;
+
+ i = 0;
+ argv[i++] = dl;
+ if (strchr (ent->filename, '/') != NULL)
+ ent_filename = ent->filename;
+ else
+ {
+ size_t flen = strlen (ent->filename);
+ char *tp = alloca (2 + flen + 1);
+ memcpy (tp, "./", 2);
+ memcpy (tp + 2, ent->filename, flen + 1);
+ ent_filename = tp;
+ }
+
+ 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;
+
+ do
+ {
+ n = getline (&line, &len, f);
+ if (n < 0)
+ break;
+
+ if (line[n - 1] == '\n')
+ line[n - 1] = '\0';
+
+ p = strstr (line, " => ");
+ if (p)
+ {
+ q = strstr (p, " (");
+ if (q == NULL && strcmp (p, " => not found") == 0)
+ {
+ error (0, 0, "%s: Could not find one of the dependencies: %s",
+ ent->filename, line);
+ goto error_out;
+ }
+ }
+ if (p == NULL || q == NULL)
+ {
+ if (strstr (line, "statically linked") != NULL)
+ error (0, 0, "%s: Library without dependencies", ent->filename);
+ else
+ {
+ p = strstr (line, "error while loading shared libraries: ");
+ if (p != NULL)
+ {
+ p += sizeof "error while loading shared libraries: " - 1;
+ q = strstr (line, "cannot open shared object file: "
+ "No such file or directory");
+ if (q != NULL)
+ {
+ error (0, 0,
+ "%s: Could not find one of the dependencies",
+ ent->filename);
+ goto error_out;
+ }
+ }
+ error (0, 0, "%s: Could not parse `%s'", ent->filename, line);
+ }
+ goto error_out;
+ }
+
+ *p = '\0';
+ p += sizeof " => " - 1;
+ *q = '\0';
+ if (! strcmp (p, ent_filename))
+ {
+ ++seen;
+ continue;
+ }
+ if (ndepends == ndepends_alloced)
+ {
+ ndepends_alloced += 10;
+ depends =
+ (const char **) realloc (depends,
+ ndepends_alloced * sizeof (char *));
+ if (depends == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not record dependencies",
+ ent->filename);
+ goto error_out;
+ }
+ }
+
+ depends[ndepends] = strdupa (p);
+ ++ndepends;
+ } while (!feof (f));
+
+ if (execve_close (f))
+ {
+ f = NULL;
+ error (0, 0, "%s: Dependency tracing failed", ent->filename);
+ goto error_out;
+ }
+
+ f = NULL;
+ if (seen != 1)
+ {
+ error (0, 0, "%s seen %d times in LD_TRACE_PRELINKING output, expected once",
+ ent->filename, seen);
+ goto error_out;
+ }
+
+ free (line);
+ line = NULL;
+
+ if (ndepends == 0)
+ ent->depends = NULL;
+ else
+ {
+ ent->depends =
+ (struct prelink_entry **)
+ malloc (ndepends * sizeof (struct prelink_entry *));
+ if (ent->depends == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not record dependencies", ent->filename);
+ goto error_out;
+ }
+ }
+
+ ent->ndepends = ndepends;
+ char *cache_dyn_depends = NULL;
+ if (ndepends)
+ {
+ cache_dyn_depends = alloca (ndepends);
+ memset (cache_dyn_depends, '\0', ndepends);
+ }
+ for (i = 0; i < ndepends; ++i)
+ {
+ ent->depends[i] = prelink_find_entry (depends [i], NULL, 1);
+ if (ent->depends[i] == NULL)
+ goto error_out_free_depends;
+
+ if (ent->depends[i]->type == ET_CACHE_DYN)
+ {
+ ent->depends[i]->type = ET_NONE;
+ free (ent->depends[i]->depends);
+ ent->depends[i]->depends = NULL;
+ ent->depends[i]->ndepends = 0;
+ cache_dyn_depends[i] = 1;
+ }
+
+ if (ent->depends[i]->type != ET_NONE
+ && ent->depends[i]->type != ET_BAD
+ && ent->depends[i]->type != ET_DYN
+ && ent->depends[i]->type != ET_UNPRELINKABLE)
+ {
+ error (0, 0, "%s is not a shared library", depends [i]);
+error_out_regather_libs:
+ for (i = 0; i < ndepends; ++i)
+ {
+ if (cache_dyn_depends[i] && ent->depends[i]->type == ET_NONE)
+ gather_lib (ent->depends[i]);
+ }
+ goto error_out_free_depends;
+ }
+ }
+
+ free (depends);
+ depends = NULL;
+
+ for (i = 0; i < ndepends; ++i)
+ if (ent->depends[i]->type == ET_NONE
+ && gather_lib (ent->depends[i]))
+ {
+ cache_dyn_depends[i] = 0;
+ goto error_out_regather_libs;
+ }
+
+ for (i = 0; i < ndepends; ++i)
+ for (j = 0; j < ent->depends[i]->ndepends; ++j)
+ if (ent->depends[i]->depends[j] == ent)
+ {
+ error (0, 0, "%s has a dependency cycle", ent->canon_filename);
+ goto error_out_free_depends;
+ }
+
+ for (i = 0; i < ndepends; ++i)
+ if (ent->depends[i]->type == ET_UNPRELINKABLE)
+ {
+ error (0, 0, "Could not prelink %s because its dependency %s could not be prelinked",
+ ent->filename, ent->depends[i]->filename);
+ ent->type = ET_UNPRELINKABLE;
+ goto error_out;
+ }
+
+ if (! undo && (!nliblist || liblist) && nliblist == ndepends)
+ {
+ for (i = 0; i < ndepends; ++i)
+ if (liblist[i].l_time_stamp != ent->depends[i]->timestamp
+ || liblist[i].l_checksum != ent->depends[i]->checksum
+ || ! ent->depends[i]->done)
+ break;
+
+ if (i == ndepends)
+ ent->done = 2;
+ }
+
+ return 0;
+
+error_out_free_depends:
+ free (ent->depends);
+ ent->depends = NULL;
+ ent->ndepends = 0;
+error_out:
+ if (f)
+ execve_close (f);
+ free (line);
+ free (depends);
+ if (dso)
+ close_dso (dso);
+ return 1;
+}
+
+static int
+gather_dso (DSO *dso, struct prelink_entry *ent)
+{
+ int prelinked;
+
+ if (verbose > 5)
+ printf ("Checking shared library %s\n", ent->canon_filename);
+
+ if (dso->ehdr.e_type != ET_DYN)
+ {
+ error (0, 0, "%s is not a shared library", ent->filename);
+ close_dso (dso);
+ return 1;
+ }
+
+ prelinked = (dynamic_info_is_set (dso, DT_GNU_PRELINKED_BIT)
+ && dynamic_info_is_set (dso, DT_CHECKSUM_BIT));
+ ent->timestamp = dso->info_DT_GNU_PRELINKED;
+ ent->checksum = dso->info_DT_CHECKSUM;
+ ent->base = dso->base;
+ ent->end = dso->end;
+ if (dso->arch->need_rel_to_rela != NULL && ! prelinked)
+ {
+ /* If the library has not been prelinked yet and we need
+ to convert REL to RELA, then make room for it. */
+ struct reloc_info rinfo;
+ GElf_Addr adjust = 0;
+ int sec = dso->ehdr.e_shnum;
+
+ if (find_reloc_sections (dso, &rinfo))
+ {
+ close_dso (dso);
+ return 1;
+ }
+
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ if (rinfo.rel_to_rela)
+ {
+ sec = rinfo.first;
+ adjust = (dso->shdr[rinfo.last].sh_addr
+ + dso->shdr[rinfo.last].sh_size
+ - dso->shdr[rinfo.first].sh_addr) / 2;
+ }
+ if (rinfo.rel_to_rela_plt)
+ {
+ if (rinfo.plt < sec)
+ sec = rinfo.plt;
+ adjust += dso->shdr[rinfo.plt].sh_size / 2;
+ }
+ if (adjust)
+ {
+ int align = 0, i, last;
+ GElf_Addr start;
+
+ for (i = rinfo.plt ? rinfo.plt : rinfo.first;
+ i < dso->ehdr.e_shnum; i++)
+ {
+ if (dso->shdr[i].sh_addralign > align)
+ align = dso->shdr[i].sh_addralign;
+ }
+
+ if (rinfo.plt)
+ start = dso->shdr[rinfo.plt].sh_addr
+ + dso->shdr[rinfo.plt].sh_size;
+ else
+ start = dso->shdr[rinfo.first].sh_addr
+ + dso->shdr[rinfo.first].sh_size;
+
+ /* Need to make sure that all the remaining sections are properly
+ aligned. */
+ if (align)
+ adjust = (adjust + align - 1) & ~(align - 1);
+
+ /* Need to make sure adjust doesn't cause different Phdr segments
+ to overlap on the same page. */
+ last = -1;
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz >= start)
+ {
+ if (last != -1
+ && (((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz
+ - 1) ^ dso->phdr[i].p_vaddr)
+ & ~(dso->arch->max_page_size - 1))
+ && !(((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz
+ + adjust - 1)
+ ^ (dso->phdr[i].p_vaddr + adjust))
+ & ~(dso->arch->max_page_size - 1)))
+ {
+ if (align >= dso->arch->max_page_size)
+ {
+ error (0, 0, "%s: Cannot grow reloc sections",
+ ent->filename);
+ close_dso (dso);
+ return 1;
+ }
+ adjust = (adjust + dso->arch->max_page_size - 1)
+ & ~(dso->arch->max_page_size - 1);
+ }
+ last = i;
+ }
+
+ ent->end += adjust;
+ }
+ }
+
+ if (gather_deps (dso, ent))
+ return 1;
+
+ if (ent->done && ! prelinked && ! undo)
+ ent->done = 0;
+ ent->type = ET_DYN;
+ return 0;
+}
+
+static int
+gather_lib (struct prelink_entry *ent)
+{
+ DSO *dso;
+
+ ent->type = ET_BAD;
+ dso = open_dso (ent->filename);
+ if (dso == NULL)
+ return 1;
+
+ return gather_dso (dso, ent);
+}
+
+static int
+gather_exec (DSO *dso, const struct stat64 *st)
+{
+ int i, j;
+ Elf_Data *data;
+ const char *dl;
+ struct prelink_entry *ent;
+
+ if (verbose > 5)
+ printf ("Checking executable %s\n", dso->filename);
+
+ 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 (i == dso->ehdr.e_phnum)
+ {
+make_unprelinkable:
+ if (undo)
+ goto error_out;
+
+ ent = prelink_find_entry (dso->filename, st, 1);
+ if (ent == NULL)
+ goto error_out;
+
+ assert (ent->type == ET_NONE);
+ ent->type = ET_UNPRELINKABLE;
+ if (dso)
+ close_dso (dso);
+ return 0;
+ }
+
+ j = addr_to_sec (dso, dso->phdr[i].p_vaddr);
+ if (j == -1 || dso->shdr[j].sh_addr != dso->phdr[i].p_vaddr
+ || dso->shdr[j].sh_type != SHT_PROGBITS)
+ {
+ error (0, 0, "%s: PT_INTERP segment not corresponding to .interp section",
+ dso->filename);
+ goto make_unprelinkable;
+ }
+
+ data = elf_getdata (dso->scn[j], NULL);
+ if (data == NULL)
+ {
+ error (0, 0, "%s: Could not read .interp section", dso->filename);
+ goto error_out;
+ }
+
+ i = strnlen (data->d_buf, data->d_size);
+ if (i == data->d_size)
+ {
+ error (0, 0, "%s: .interp section not zero terminated", dso->filename);
+ goto error_out;
+ }
+
+ dl = dynamic_linker ?: dso->arch->dynamic_linker;
+ if (strcmp (dl, data->d_buf) != 0
+ && (dynamic_linker != NULL || dso->arch->dynamic_linker_alt == NULL
+ || strcmp (dso->arch->dynamic_linker_alt, data->d_buf) != 0))
+ {
+ error (0, 0, "%s: Using %s, not %s as dynamic linker", dso->filename,
+ (char *) data->d_buf, dl);
+ goto error_out;
+ }
+
+ ent = prelink_find_entry (dso->filename, st, 1);
+ if (ent == NULL)
+ goto error_out;
+
+ assert (ent->type == ET_NONE);
+ ent->u.explicit = 1;
+
+ if (gather_deps (dso, ent))
+ return 0;
+
+ for (i = 0; i < ent->ndepends; ++i)
+ ++ent->depends[i]->refs;
+
+ ent->type = ET_EXEC;
+ return 0;
+
+error_out:
+ if (dso)
+ close_dso (dso);
+ return 0;
+}
+
+static int
+add_dir_to_dirlist (const char *name, dev_t dev, int flags)
+{
+ const char *canon_name;
+ struct prelink_dir *dir;
+ size_t len;
+
+ canon_name = wrap_prelink_canonicalize (name, NULL);
+ if (canon_name == NULL)
+ {
+ if (! all && implicit)
+ return 0;
+ error (0, errno, "Could not record directory %s", name);
+ return 1;
+ }
+
+ len = strlen (canon_name);
+
+ for (dir = blacklist; dir; dir = dir->next)
+ if (((dir->flags != FTW_CHDIR && len >= dir->len)
+ || (dir->flags == FTW_CHDIR && len == dir->len))
+ && strncmp (dir->dir, canon_name, dir->len) == 0)
+ {
+ if (dir->flags == FTW_CHDIR)
+ break;
+ if ((dir->flags & FTW_MOUNT) && dir->dev != dev)
+ continue;
+ break;
+ }
+
+ if (dir != NULL)
+ {
+ free ((char *) canon_name);
+ return 2;
+ }
+
+ dir = malloc (sizeof (struct prelink_dir) + len + 1);
+ if (dir == NULL)
+ {
+ error (0, ENOMEM, "Could not record directory %s", name);
+ free ((char *) canon_name);
+ return 1;
+ }
+
+ dir->next = dirs;
+ dir->flags = flags;
+ dir->dev = dev;
+ dir->len = len;
+ strcpy (dir->dir, canon_name);
+ free ((char *) canon_name);
+ dirs = dir;
+ return 0;
+}
+
+static int
+gather_func (const char *name, const struct stat64 *st, int type,
+ struct FTW *ftwp)
+{
+ unsigned char e_ident [sizeof (Elf64_Ehdr) + sizeof (Elf64_Phdr)];
+
+#ifndef HAVE_FTW_ACTIONRETVAL
+ if (blacklist_dir)
+ {
+ if (strncmp (name, blacklist_dir, blacklist_dir_len) == 0)
+ return FTW_CONTINUE;
+ free (blacklist_dir);
+ blacklist_dir = NULL;
+ }
+#endif
+ if (type == FTW_F && S_ISREG (st->st_mode) && (st->st_mode & 0111))
+ {
+ int fd, i;
+ DSO *dso;
+ struct prelink_entry *ent;
+ size_t len = strlen (name);
+ const char *base = NULL;
+
+ for (i = 0; i < blacklist_next; ++i)
+ if (blacklist_ext[i].is_glob)
+ {
+ if (base == NULL)
+ {
+ base = strrchr (name, '/');
+ if (base == NULL)
+ base = name;
+ else
+ ++base;
+ }
+ if (fnmatch (blacklist_ext[i].ext, base, FNM_PERIOD) == 0)
+ return FTW_CONTINUE;
+ }
+ else if (blacklist_ext[i].len <= len
+ && memcmp (name + len - blacklist_ext[i].len,
+ blacklist_ext[i].ext, blacklist_ext[i].len) == 0)
+ return FTW_CONTINUE;
+
+ ent = prelink_find_entry (name, st, 0);
+ if (ent != NULL && ent->type != ET_NONE)
+ {
+ if (verbose > 5)
+ {
+ if (ent->type == ET_CACHE_EXEC || ent->type == ET_CACHE_DYN)
+ printf ("Assuming prelinked %s\n", name);
+ if (ent->type == ET_UNPRELINKABLE)
+ printf ("Assuming non-prelinkable %s\n", name);
+ }
+ ent->u.explicit = 1;
+ return FTW_CONTINUE;
+ }
+
+ if (st->st_size < sizeof (e_ident))
+ return FTW_CONTINUE;
+
+ fd = wrap_open (name, O_RDONLY);
+ if (fd == -1)
+ return FTW_CONTINUE;
+
+ if (read (fd, e_ident, sizeof (e_ident)) != sizeof (e_ident))
+ {
+close_it:
+ close (fd);
+ return FTW_CONTINUE;
+ }
+
+ /* Quickly find ET_EXEC ELF binaries and most of PIE binaries. */
+
+ if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
+ {
+make_unprelinkable:
+ if (! undo)
+ {
+ ent = prelink_find_entry (name, st, 1);
+ if (ent != NULL)
+ {
+ assert (ent->type == ET_NONE);
+ ent->type = ET_UNPRELINKABLE;
+ }
+ }
+ close (fd);
+ return FTW_CONTINUE;
+ }
+
+ switch (e_ident [EI_DATA])
+ {
+ case ELFDATA2LSB:
+ if (e_ident [EI_NIDENT + 1] != 0)
+ goto make_unprelinkable;
+ if (e_ident [EI_NIDENT] != ET_EXEC)
+ {
+ if (e_ident [EI_NIDENT] != ET_DYN)
+ goto make_unprelinkable;
+ else if (e_ident [EI_CLASS] == ELFCLASS32)
+ {
+ if (e_ident [offsetof (Elf32_Ehdr, e_phoff)]
+ == sizeof (Elf32_Ehdr)
+ && memcmp (e_ident + offsetof (Elf32_Ehdr, e_phoff) + 1,
+ "\0\0\0", 3) == 0
+ && (e_ident [offsetof (Elf32_Ehdr, e_phnum)]
+ || e_ident [offsetof (Elf32_Ehdr, e_phnum) + 1])
+ && e_ident [sizeof (Elf32_Ehdr)
+ + offsetof (Elf32_Phdr, p_type)] == PT_PHDR
+ && memcmp (e_ident + sizeof (Elf32_Ehdr)
+ + offsetof (Elf32_Phdr, p_type) + 1,
+ "\0\0\0", 3) == 0)
+ {
+maybe_pie:
+ dso = fdopen_dso (fd, name);
+ if (dso == NULL)
+ goto close_it;
+ if (dynamic_info_is_set (dso, DT_DEBUG))
+ {
+ close_dso (dso);
+ goto make_unprelinkable;
+ }
+ close_dso (dso);
+ }
+ goto close_it;
+ }
+ else if (e_ident [EI_CLASS] == ELFCLASS64)
+ {
+ if (e_ident [offsetof (Elf64_Ehdr, e_phoff)]
+ == sizeof (Elf64_Ehdr)
+ && memcmp (e_ident + offsetof (Elf64_Ehdr, e_phoff) + 1,
+ "\0\0\0\0\0\0\0", 7) == 0
+ && (e_ident [offsetof (Elf64_Ehdr, e_phnum)]
+ || e_ident [offsetof (Elf64_Ehdr, e_phnum) + 1])
+ && e_ident [sizeof (Elf64_Ehdr)
+ + offsetof (Elf64_Phdr, p_type)] == PT_PHDR
+ && memcmp (e_ident + sizeof (Elf64_Ehdr)
+ + offsetof (Elf64_Phdr, p_type) + 1,
+ "\0\0\0", 3) == 0)
+ goto maybe_pie;
+ goto close_it;
+ }
+ else
+ goto make_unprelinkable;
+ }
+ break;
+ case ELFDATA2MSB:
+ if (e_ident [EI_NIDENT] != 0)
+ goto make_unprelinkable;
+ if (e_ident [EI_NIDENT + 1] != ET_EXEC)
+ {
+ if (e_ident [EI_NIDENT + 1] != ET_DYN)
+ goto make_unprelinkable;
+ else if (e_ident [EI_CLASS] == ELFCLASS32)
+ {
+ if (e_ident [offsetof (Elf32_Ehdr, e_phoff) + 3]
+ == sizeof (Elf32_Ehdr)
+ && memcmp (e_ident + offsetof (Elf32_Ehdr, e_phoff),
+ "\0\0\0", 3) == 0
+ && (e_ident [offsetof (Elf32_Ehdr, e_phnum)]
+ || e_ident [offsetof (Elf32_Ehdr, e_phnum) + 1])
+ && e_ident [sizeof (Elf32_Ehdr)
+ + offsetof (Elf32_Phdr, p_type) + 3]
+ == PT_PHDR
+ && memcmp (e_ident + sizeof (Elf32_Ehdr)
+ + offsetof (Elf32_Phdr, p_type),
+ "\0\0\0", 3) == 0)
+ goto maybe_pie;
+ goto close_it;
+ }
+ else if (e_ident [EI_CLASS] == ELFCLASS64)
+ {
+ if (e_ident [offsetof (Elf64_Ehdr, e_phoff) + 7]
+ == sizeof (Elf64_Ehdr)
+ && memcmp (e_ident + offsetof (Elf64_Ehdr, e_phoff),
+ "\0\0\0\0\0\0\0", 7) == 0
+ && (e_ident [offsetof (Elf64_Ehdr, e_phnum)]
+ || e_ident [offsetof (Elf64_Ehdr, e_phnum) + 1])
+ && e_ident [sizeof (Elf64_Ehdr)
+ + offsetof (Elf64_Phdr, p_type) + 3]
+ == PT_PHDR
+ && memcmp (e_ident + sizeof (Elf64_Ehdr)
+ + offsetof (Elf64_Phdr, p_type),
+ "\0\0\0", 3) == 0)
+ goto maybe_pie;
+ goto close_it;
+ }
+ else
+ goto make_unprelinkable;
+ }
+ break;
+ default:
+ goto make_unprelinkable;
+ }
+
+ dso = fdopen_dso (fd, name);
+ if (dso == NULL)
+ return FTW_CONTINUE;
+
+ gather_exec (dso, st);
+ }
+ else if (type == FTW_D)
+ switch (add_dir_to_dirlist (name, st->st_dev, FTW_CHDIR))
+ {
+ case 0: return FTW_CONTINUE;
+ default: return FTW_STOP;
+ case 2:
+#ifdef HAVE_FTW_ACTIONRETVAL
+ return FTW_SKIP_SUBTREE;
+#else
+ {
+ blacklist_dir_len = strlen (name) + 1;
+ if (blacklist_dir_len > 1 && name[blacklist_dir_len - 2] == '/')
+ blacklist_dir_len--;
+ blacklist_dir = malloc (blacklist_dir_len + 1);
+ if (blacklist_dir == NULL)
+ {
+ error (0, ENOMEM, "Cannot store blacklisted dir name");
+ return FTW_STOP;
+ }
+ memcpy (blacklist_dir, name, blacklist_dir_len - 1);
+ blacklist_dir[blacklist_dir_len - 1] = '/';
+ blacklist_dir[blacklist_dir_len] = '\0';
+ return FTW_CONTINUE;
+ }
+#endif
+ }
+
+ return FTW_CONTINUE;
+}
+
+static int
+gather_binlib (const char *name, const struct stat64 *st)
+{
+ unsigned char e_ident [EI_NIDENT + 2];
+ int fd, type;
+ DSO *dso;
+ struct prelink_entry *ent;
+
+ if (! S_ISREG (st->st_mode))
+ {
+ error (0, 0, "%s is not a regular file", name);
+ return 1;
+ }
+
+ ent = prelink_find_entry (name, st, 0);
+ if (ent != NULL && ent->type == ET_UNPRELINKABLE)
+ {
+ free (ent->depends);
+ ent->depends = NULL;
+ ent->ndepends = 0;
+ ent->type = ET_NONE;
+ }
+ if (ent != NULL && ent->type != ET_NONE)
+ {
+ ent->u.explicit = 1;
+ return 0;
+ }
+
+ fd = wrap_open (name, O_RDONLY);
+ if (fd == -1)
+ {
+ error (0, errno, "Could not open %s", name);
+ return 1;
+ }
+
+ if (read (fd, e_ident, sizeof (e_ident)) != sizeof (e_ident))
+ {
+ error (0, errno, "Could not read ELF header from %s", name);
+ close (fd);
+ return 1;
+ }
+
+ /* Quickly find ET_EXEC/ET_DYN ELF binaries/libraries only. */
+
+ if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
+ {
+ error (0, 0, "%s is not an ELF object", name);
+ close (fd);
+ return 1;
+ }
+
+ switch (e_ident [EI_DATA])
+ {
+ case ELFDATA2LSB:
+ if (e_ident [EI_NIDENT + 1] != 0)
+ goto unsupported_type;
+ type = e_ident [EI_NIDENT];
+ break;
+ case ELFDATA2MSB:
+ if (e_ident [EI_NIDENT] != 0)
+ goto unsupported_type;
+ type = e_ident [EI_NIDENT + 1];
+ break;
+ default:
+ goto unsupported_type;
+ }
+
+ if (type != ET_EXEC && type != ET_DYN)
+ {
+unsupported_type:
+ error (0, 0, "%s is neither ELF executable nor ELF shared library", name);
+ close (fd);
+ return 1;
+ }
+
+ dso = fdopen_dso (fd, name);
+ if (dso == NULL)
+ return 0;
+
+ if (type == ET_EXEC)
+ {
+ int i;
+
+ 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 (i == dso->ehdr.e_phnum)
+ {
+ error (0, 0, "%s is statically linked", name);
+ close_dso (dso);
+ return 1;
+ }
+
+ return gather_exec (dso, st);
+ }
+
+ ent = prelink_find_entry (name, st, 1);
+ if (ent == NULL)
+ {
+ close_dso (dso);
+ return 1;
+ }
+
+ assert (ent->type == ET_NONE);
+ ent->type = ET_BAD;
+ ent->u.explicit = 1;
+ return gather_dso (dso, ent);
+}
+
+int
+gather_object (const char *name, int deref, int onefs)
+{
+ struct stat64 st;
+
+ if (wrap_stat64 (name, &st) < 0)
+ {
+ if (implicit)
+ return 0;
+ error (0, errno, "Could not stat %s", name);
+ return 1;
+ }
+
+ if (S_ISDIR (st.st_mode))
+ {
+ int flags = 0, ret;
+ if (! deref) flags |= FTW_PHYS;
+ if (onefs) flags |= FTW_MOUNT;
+
+ if (implicit && ! deref)
+ {
+ ret = add_dir_to_dirlist (name, st.st_dev, flags);
+ if (ret)
+ return ret == 2 ? 0 : 1;
+ }
+ if (!all && implicit && ! deref)
+ return 0;
+ ++implicit;
+ ret = wrap_nftw64 (name, gather_func, 20, flags | FTW_ACTIONRETVAL);
+ --implicit;
+#ifndef HAVE_FTW_ACTIONRETVAL
+ free (blacklist_dir);
+ blacklist_dir = NULL;
+#endif
+ return ret;
+ }
+ else
+ return gather_binlib (name, &st);
+}
+
+int
+gather_config (const char *config)
+{
+ FILE *file = fopen (config, "r");
+ char *line = NULL;
+ size_t len;
+ int ret = 0;
+
+ if (file == NULL)
+ {
+ error (0, errno, "Can't open configuration file %s", config);
+ return 1;
+ }
+
+ implicit = 1;
+ do
+ {
+ ssize_t i = getline (&line, &len, file);
+ int deref = 0;
+ int onefs = 0;
+ char *p;
+
+ if (i < 0)
+ break;
+
+ if (line[i - 1] == '\n')
+ line[i - 1] = '\0';
+
+ p = strchr (line, '#');
+ if (p != NULL)
+ *p = '\0';
+
+ p = line + strspn (line, " \t");
+
+ while (*p == '-')
+ {
+ switch (p[1])
+ {
+ case 'h': deref = 1; break;
+ case 'l': onefs = 1; break;
+ case 'b': *p = '\0'; continue;
+ default:
+ error (0, 0, "Unknown directory option `%s'\n", p);
+ break;
+ }
+ p = p + 2 + strspn (p + 2, " \t");
+ }
+
+ if (*p == '\0')
+ continue;
+
+ if (strpbrk (p, "*?[{") == NULL)
+ {
+ ret = gather_object (p, deref, onefs);
+ if (ret)
+ {
+ ret = 1;
+ break;
+ }
+ }
+ else
+ {
+ glob_t g;
+
+ if (!glob (p, GLOB_BRACE, NULL, &g))
+ {
+ size_t n;
+
+ for (n = 0; n < g.gl_pathc; ++n)
+ {
+ ret = gather_object (g.gl_pathv[n], deref, onefs);
+ if (ret)
+ {
+ ret = 1;
+ break;
+ }
+ }
+
+ globfree (&g);
+ if (ret)
+ break;
+ }
+ }
+ } while (!feof (file));
+
+ free (line);
+ fclose (file);
+ implicit = 0;
+ return ret;
+}
+
+static int
+gather_check_lib (void **p, void *info)
+{
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+
+ if (e->type != ET_DYN)
+ return 1;
+
+ if (! e->u.explicit)
+ {
+ struct prelink_dir *dir;
+ const char *name;
+ size_t len;
+
+ name = strrchr (e->canon_filename, '/');
+ if (!name)
+ name = e->canon_filename;
+ len = name - e->canon_filename;
+
+ for (dir = blacklist; dir; dir = dir->next)
+ if (((dir->flags != FTW_CHDIR && len >= dir->len)
+ || (dir->flags == FTW_CHDIR && len == dir->len))
+ && strncmp (dir->dir, e->canon_filename, dir->len) == 0)
+ {
+ if (dir->flags == FTW_CHDIR)
+ break;
+ if ((dir->flags & FTW_MOUNT) && dir->dev != e->dev)
+ continue;
+ break;
+ }
+
+ if (dir != NULL)
+ {
+ error (0, 0, "%s is present in a blacklisted directory %s",
+ e->canon_filename, dir->dir);
+ e->type = ET_BAD;
+ return 1;
+ }
+
+ for (dir = dirs; dir; dir = dir->next)
+ if (((dir->flags != FTW_CHDIR && len >= dir->len)
+ || (dir->flags == FTW_CHDIR && len == dir->len))
+ && strncmp (dir->dir, e->canon_filename, dir->len) == 0)
+ {
+ if (dir->flags == FTW_CHDIR)
+ break;
+ if ((dir->flags & FTW_MOUNT) && dir->dev != e->dev)
+ continue;
+ break;
+ }
+
+ if (dir == NULL)
+ {
+ error (0, 0, "%s is not present in any config file directories, nor was specified on command line",
+ e->canon_filename);
+ e->type = ET_BAD;
+ return 1;
+ }
+ }
+
+ return 1;
+}
+
+int
+gather_check_libs (void)
+{
+ struct prelink_dir *dir;
+ void *f;
+
+ htab_traverse (prelink_filename_htab, gather_check_lib, NULL);
+
+ dir = dirs;
+ while (dir != NULL)
+ {
+ f = dir;
+ dir = dir->next;
+ free (f);
+ }
+
+ dir = blacklist;
+ while (dir != NULL)
+ {
+ f = dir;
+ dir = dir->next;
+ free (f);
+ }
+
+ dirs = NULL;
+ blacklist = NULL;
+ return 0;
+}
+
+int
+add_to_blacklist (const char *name, int deref, int onefs)
+{
+ const char *canon_name;
+ struct prelink_dir *path;
+ size_t len;
+ struct stat64 st;
+
+ if (stat64 (name, &st) < 0)
+ {
+ if (implicit)
+ return 0;
+ error (0, errno, "Could not stat %s", name);
+ return 1;
+ }
+
+ if (!S_ISDIR (st.st_mode))
+ {
+ struct prelink_entry *ent;
+
+ ent = prelink_find_entry (name, &st, 1);
+ if (ent == NULL)
+ return 1;
+
+ ent->type = ET_BAD;
+ ent->u.explicit = 1;
+ return 0;
+ }
+
+ canon_name = wrap_prelink_canonicalize (name, NULL);
+ if (canon_name == NULL)
+ {
+ if (implicit)
+ return 0;
+ error (0, errno, "Could not canonicalize %s", name);
+ return 1;
+ }
+
+ len = strlen (canon_name);
+ path = malloc (sizeof (struct prelink_dir) + len + 1);
+ if (path == NULL)
+ {
+ error (0, ENOMEM, "Could not record path %s", name);
+ free ((char *) canon_name);
+ return 1;
+ }
+
+ path->next = blacklist;
+ path->flags = 0;
+ if (! deref) path->flags |= FTW_PHYS;
+ if (onefs) path->flags |= FTW_MOUNT;
+ path->dev = 0;
+ path->len = len;
+ strcpy (path->dir, canon_name);
+ free ((char *) canon_name);
+ blacklist = path;
+ return 0;
+}
+
+void
+add_blacklist_ext (const char *ext)
+{
+ blacklist_ext = realloc (blacklist_ext,
+ (blacklist_next + 1) * sizeof (*blacklist_ext));
+ if (blacklist_ext == NULL)
+ error (EXIT_FAILURE, errno, "can't create blacklist extension list");
+ if (*ext == '*' && strpbrk (ext + 1, "*?[{") == NULL)
+ {
+ blacklist_ext[blacklist_next].is_glob = 0;
+ ext++;
+ }
+ else
+ blacklist_ext[blacklist_next].is_glob = 1;
+ blacklist_ext[blacklist_next].ext = strdup (ext);
+ if (blacklist_ext[blacklist_next].ext == NULL)
+ error (EXIT_FAILURE, errno, "can't create blacklist extension list");
+ blacklist_ext[blacklist_next].len = strlen (ext);
+ blacklist_next++;
+}
+
+int
+blacklist_from_config (const char *config)
+{
+ FILE *file = fopen (config, "r");
+ char *line = NULL;
+ size_t len;
+ int ret = 0;
+
+ if (file == NULL)
+ {
+ error (0, errno, "Can't open configuration file %s", config);
+ return 1;
+ }
+
+ implicit = 1;
+ do
+ {
+ ssize_t i = getline (&line, &len, file);
+ int deref = 0;
+ int onefs = 0;
+ int blacklist = 0;
+ char *p;
+
+ if (i < 0)
+ break;
+
+ if (line[i - 1] == '\n')
+ line[i - 1] = '\0';
+
+ p = strchr (line, '#');
+ if (p != NULL)
+ *p = '\0';
+
+ p = line + strspn (line, " \t");
+
+ while (*p == '-')
+ {
+ switch (p[1])
+ {
+ case 'h': deref = 1; break;
+ case 'l': onefs = 1; break;
+ case 'b': blacklist = 1; break;
+ }
+ p = p + 2 + strspn (p + 2, " \t");
+ }
+
+ if (*p == '\0' || !blacklist)
+ continue;
+
+ if (strchr (p, '/') == NULL)
+ {
+ add_blacklist_ext (p);
+ continue;
+ }
+
+ if (strpbrk (p, "*?[{") == NULL)
+ {
+ ret = add_to_blacklist (p, deref, onefs);
+ if (ret)
+ {
+ ret = 1;
+ break;
+ }
+ }
+ else
+ {
+ glob_t g;
+
+ if (!glob (p, GLOB_BRACE | GLOB_PERIOD, NULL, &g))
+ {
+ size_t n;
+
+ for (n = 0; n < g.gl_pathc; ++n)
+ {
+ ret = add_to_blacklist (g.gl_pathv[n], deref, onefs);
+ if (ret)
+ {
+ ret = 1;
+ break;
+ }
+ }
+
+ globfree (&g);
+ if (ret)
+ break;
+ }
+ }
+ } while (!feof (file));
+
+ free (line);
+ fclose (file);
+ implicit = 0;
+ return ret;
+}
diff --git a/trunk/src/get.c b/trunk/src/get.c
new file mode 100644
index 0000000..0ae418b
--- /dev/null
+++ b/trunk/src/get.c
@@ -0,0 +1,673 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 2005 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include "prelink.h"
+
+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;
+}
+
+static int
+prelink_record_relocations (struct prelink_info *info, FILE *f,
+ const char *ent_filename)
+{
+ char buffer[8192];
+ DSO *dso = info->dso;
+ struct prelink_entry *ent, *ent2;
+ struct prelink_tls *tls;
+ struct deps
+ {
+ struct prelink_entry *ent;
+ char *soname;
+ GElf_Addr start;
+ GElf_Addr l_addr;
+ GElf_Addr tls_modid;
+ GElf_Addr tls_offset;
+ } deps[info->ent->ndepends + 1];
+ char *r;
+ int i, ndeps = 0, undef = 0, seen = 0, tdeps = 0;
+ int mask_32bit = (info->dso->ehdr.e_ident[EI_CLASS] == ELFCLASS32);
+
+ /* Record the dependencies. */
+ while ((r = fgets (buffer, 8192, f)) != NULL)
+ {
+ char *soname, *filename, *p, *q;
+ GElf_Addr start = 0, l_addr = 0, tls_modid = 0, tls_offset = 0;
+ unsigned long long l;
+
+ if (buffer[0] != '\t' || (filename = strstr (buffer, " => ")) == NULL)
+ break;
+ soname = buffer + 1;
+ p = strstr (filename + sizeof (" => "), " (0x");
+ if (p != NULL)
+ {
+ l = strtoull (p + sizeof (" (0x") - 1, &q, 16);
+ start = (GElf_Addr) l;
+ if (start != l || strncmp (q, ", 0x", sizeof (", 0x") - 1))
+ p = NULL;
+ else
+ {
+ l = strtoull (q + sizeof (", 0x") - 1, &q, 16);
+ l_addr = (GElf_Addr) l;
+ if (l_addr != l || q[-1] == 'x')
+ p = NULL;
+ else if (strncmp (q, ") TLS(0x", sizeof (") TLS(0x") - 1) == 0)
+ {
+ l = strtoull (q + sizeof (") TLS(0x") - 1, &q, 16);
+ tls_modid = (GElf_Addr) l;
+ if (tls_modid != l || q[-1] == 'x'
+ || strncmp (q, ", 0x", sizeof (", 0x") - 1))
+ p = NULL;
+ else
+ {
+ l = strtoull (q + sizeof (", 0x") - 1, &q, 16);
+ tls_offset = (GElf_Addr) l;
+ if (tls_offset != l || q[-1] == 'x')
+ p = NULL;
+ }
+ }
+ if (p && strcmp (q, ")\n"))
+ p = NULL;
+ }
+ }
+ if (p == NULL)
+ {
+ p = strchr (buffer, '\n');
+ if (p != NULL)
+ *p = '\0';
+ error (0, 0, "Could not parse line `%s'", buffer);
+ goto error_out;
+ }
+ *filename = '\0';
+ filename += sizeof (" => ") - 1;
+ *p = '\0';
+
+ if (ndeps > info->ent->ndepends)
+ {
+ error (0, 0, "%s: Recorded %d dependencies, now seeing %d\n",
+ info->ent->filename, info->ent->ndepends, ndeps - 1);
+ goto error_out;
+ }
+
+ tdeps = ndeps - seen + 1;
+ if (! seen
+ && (strcmp (info->ent->filename, filename) == 0
+ || (info->ent->filename != ent_filename
+ && strcmp (ent_filename, filename) == 0)
+ || strcmp (info->ent->canon_filename, filename) == 0))
+ {
+ seen = 1;
+ tdeps = 0;
+ }
+ else if (ent2 = info->ent->depends [tdeps - 1],
+ strcmp (ent2->filename, filename) != 0
+ && strcmp (ent2->canon_filename, filename) != 0)
+ {
+ struct prelink_link *hardlink;
+
+ for (hardlink = ent2->hardlink; hardlink; hardlink = hardlink->next)
+ if (strcmp (hardlink->canon_filename, filename) == 0)
+ break;
+
+ if (hardlink == NULL)
+ {
+ struct stat64 st;
+
+ if (wrap_stat64 (filename, &st) < 0)
+ {
+ error (0, errno, "%s: Could not stat %s",
+ info->ent->filename, filename);
+ goto error_out;
+ }
+
+ if (st.st_dev != ent2->dev || st.st_ino != ent2->ino)
+ {
+ error (0, 0, "%s: %s => %s does not match recorded dependency",
+ info->ent->filename, soname, filename);
+ goto error_out;
+ }
+ }
+ }
+
+ if (! tdeps)
+ deps[0].ent = info->ent;
+ else
+ deps[tdeps].ent = info->ent->depends[tdeps - 1];
+ deps[tdeps].soname = strdup (soname);
+ if (deps[tdeps].soname == NULL)
+ {
+ error (0, ENOMEM, "Could not record `%s' SONAME", soname);
+ goto error_out;
+ }
+ deps[tdeps].start = start;
+ deps[tdeps].l_addr = l_addr;
+ deps[tdeps].tls_modid = tls_modid;
+ deps[tdeps].tls_offset = tls_offset;
+ ++ndeps;
+ }
+
+ if (ndeps != info->ent->ndepends + 1)
+ {
+ error (0, 0, "%s: Recorded %d dependencies, now seeing %d\n",
+ info->ent->filename, info->ent->ndepends, ndeps - 1);
+ goto error_out;
+ }
+
+ if (r == NULL && !ndeps)
+ {
+ error (0, 0, "%s: %s did not print any lookup lines", info->ent->filename,
+ dynamic_linker ?: dso->arch->dynamic_linker);
+ goto error_out;
+ }
+
+ info->tls = malloc (ndeps * sizeof (struct prelink_tls));
+ if (info->tls == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not record dependency TLS information",
+ dso->filename);
+ goto error_out;
+ }
+
+ for (i = 0; i < ndeps; i++)
+ {
+ info->tls[i].modid = deps[i].tls_modid;
+ info->tls[i].offset = deps[i].tls_offset;
+ }
+
+ if (dso->ehdr.e_type == ET_EXEC || dso->arch->create_opd)
+ {
+ info->conflicts = (struct prelink_conflict **)
+ calloc (sizeof (struct prelink_conflict *), ndeps);
+ if (info->conflicts == NULL)
+ {
+ error (0, ENOMEM, "%s: Can't build list of conflicts", info->ent->filename);
+ goto error_out;
+ }
+ }
+ do
+ {
+ unsigned long long symstart, symoff, valstart[3], value[3];
+ int reloc_class, len, type = 1;
+ char *symname;
+
+ r = strchr (buffer, '\n');
+ if (r)
+ *r = '\0';
+ if (strncmp (buffer, "lookup ", sizeof ("lookup ") - 1) == 0)
+ {
+ struct prelink_symbol *s;
+
+ if (sscanf (buffer, "lookup 0x%llx 0x%llx -> 0x%llx 0x%llx %n",
+ &symstart, &symoff, &valstart[0], &value[0], &len) != 4)
+ {
+ error (0, 0, "%s: Could not parse `%s'", info->ent->filename, buffer);
+ goto error_out;
+ }
+
+ if (buffer[len] == '/')
+ {
+ ++len;
+ type = 0;
+ }
+
+ reloc_class = strtoul (buffer + len, &symname, 16);
+ if (buffer + len == symname || (reloc_class == 0 && type)
+ || (*symname != ' ' && *symname != '\t'))
+ {
+ error (0, 0, "%s: Could not parse `%s'", info->ent->filename, buffer);
+ goto error_out;
+ }
+
+ if (type)
+ reloc_class = dso->arch->reloc_class (reloc_class);
+ else
+ reloc_class |= RTYPE_CLASS_VALID;
+
+ while (*symname == ' ' || *symname == '\t') ++symname;
+
+ ent = NULL;
+ tls = NULL;
+ if (symstart == deps[0].start
+ || (reloc_class == RTYPE_CLASS_TLS && info->conflicts))
+ {
+ for (i = 0; i < ndeps; i++)
+ if (deps[i].start == valstart[0])
+ {
+ if (reloc_class == RTYPE_CLASS_TLS)
+ tls = info->tls + i;
+ else
+ {
+ ent = deps[i].ent;
+ /* If the library the symbol is bound to is already
+ prelinked, adjust the value so that it is relative
+ to library base. */
+ if (mask_32bit)
+ value[0] -= (Elf32_Addr) (deps[i].start - deps[i].l_addr);
+ else
+ value[0] -= deps[i].start - deps[i].l_addr;
+ }
+ break;
+ }
+
+ if (ent == NULL && tls == NULL && valstart[0])
+ {
+ error (0, 0, "Could not find base 0x%08llx in the list of bases `%s'",
+ valstart[0], buffer);
+ goto error_out;
+ }
+ }
+
+ if (symstart == deps[0].start)
+ {
+ /* Only interested in relocations from the current object. */
+ if (symoff < info->symtab_start || symoff >= info->symtab_end)
+ {
+ error (0, 0, "%s: Symbol `%s' offset 0x%08llx does not point into .dynsym section",
+ info->ent->filename, symname, symoff);
+ goto error_out;
+ }
+
+ if (ent == info->ent && reloc_class != RTYPE_CLASS_TLS)
+ value[0] = adjust_old_to_new (info->dso, value[0]);
+
+ s = &info->symbols[(symoff - info->symtab_start)
+ / info->symtab_entsize];
+ if (s->reloc_class)
+ {
+ while (s->reloc_class != reloc_class && s->next != NULL)
+ s = s->next;
+ if (s->reloc_class == reloc_class)
+ {
+ if ((reloc_class != RTYPE_CLASS_TLS && s->u.ent != ent)
+ || (reloc_class == RTYPE_CLASS_TLS
+ && s->u.tls != tls)
+ || s->value != value[0])
+ {
+ error (0, 0, "%s: Symbol `%s' with the same reloc type resolves to different values each time",
+ info->ent->filename, symname);
+ goto error_out;
+ }
+ s = NULL;
+ }
+ else
+ {
+ s->next = (struct prelink_symbol *)
+ malloc (sizeof (struct prelink_symbol));
+ if (s->next == NULL)
+ {
+ error (0, ENOMEM, "Cannot build symbol lookup map");
+ goto error_out;
+ }
+ s = s->next;
+ }
+ }
+ if (s)
+ {
+ if (reloc_class == RTYPE_CLASS_TLS)
+ s->u.tls = tls;
+ else
+ s->u.ent = ent;
+ s->value = value[0];
+ s->reloc_class = reloc_class;
+ s->next = NULL;
+ }
+ }
+ else if (reloc_class == RTYPE_CLASS_TLS && info->conflicts)
+ {
+ struct prelink_conflict *conflict;
+ int symowner;
+
+ for (symowner = 1; symowner < ndeps; symowner++)
+ if (deps[symowner].start == symstart)
+ break;
+ if (symowner == ndeps)
+ {
+ error (0, 0, "Could not find base 0x%08llx in the list of bases `%s'",
+ symstart, buffer);
+ goto error_out;
+ }
+
+ for (conflict = info->conflicts[symowner]; conflict;
+ conflict = conflict->next)
+ if (conflict->symoff == symoff
+ && conflict->reloc_class == reloc_class)
+ {
+ if (conflict->lookup.tls != tls
+ || conflict->conflict.tls != tls
+ || conflict->lookupval != value[0]
+ || conflict->conflictval != value[0])
+ {
+ error (0, 0, "%s: Symbol `%s' with the same reloc type resolves to different values each time",
+ info->ent->filename, symname);
+ goto error_out;
+ }
+ break;
+ }
+ if (conflict == NULL)
+ {
+ conflict = malloc (sizeof (struct prelink_conflict));
+ if (conflict == NULL)
+ {
+ error (0, ENOMEM, "Cannot build list of conflicts");
+ goto error_out;
+ }
+
+ conflict->next = info->conflicts[symowner];
+ info->conflicts[symowner] = conflict;
+ conflict->lookup.tls = tls;
+ conflict->conflict.tls = tls;
+ conflict->lookupval = value[0];
+ conflict->conflictval = value[0];
+ conflict->symoff = symoff;
+ conflict->reloc_class = reloc_class;
+ conflict->used = 0;
+ }
+ }
+ }
+ else if (strncmp (buffer, "conflict ", sizeof ("conflict ") - 1) == 0)
+ {
+ if (sscanf (buffer, "conflict 0x%llx 0x%llx -> 0x%llx 0x%llx x 0x%llx 0x%llx %n",
+ &symstart, &symoff, &valstart[0], &value[0],
+ &valstart[1], &value[1], &len) != 6)
+ {
+ error (0, 0, "%s: Could not parse `%s'", info->ent->filename, buffer);
+ goto error_out;
+ }
+
+ if (buffer[len] == '/')
+ {
+ ++len;
+ type = 0;
+ }
+
+ reloc_class = strtoul (buffer + len, &symname, 16);
+ if (buffer + len == symname || (reloc_class == 0 && type)
+ || (*symname != ' ' && *symname != '\t'))
+ {
+ error (0, 0, "%s: Could not parse `%s'", info->ent->filename, buffer);
+ goto error_out;
+ }
+
+ if (type)
+ reloc_class = dso->arch->reloc_class (reloc_class);
+ else
+ reloc_class |= RTYPE_CLASS_VALID;
+
+ while (*symname == ' ' || *symname == '\t') ++symname;
+
+ if (symstart == deps[0].start)
+ {
+ error (0, 0, "Conflict in _dl_loaded `%s'", buffer);
+ goto error_out;
+ }
+
+ if (info->conflicts)
+ {
+ struct prelink_entry *ents[2];
+ struct prelink_tls *tlss[2];
+ struct prelink_conflict *conflict;
+ int symowner, j;
+
+ for (symowner = 1; symowner < ndeps; symowner++)
+ if (deps[symowner].start == symstart)
+ break;
+ if (symowner == ndeps)
+ {
+ error (0, 0, "Could not find base 0x%08llx in the list of bases `%s'",
+ symstart, buffer);
+ goto error_out;
+ }
+
+ for (j = 0; j < 2; j++)
+ {
+ ents[j] = NULL;
+ tlss[j] = NULL;
+ for (i = 0; i < ndeps; i++)
+ if (deps[i].start == valstart[j])
+ {
+ if (reloc_class == RTYPE_CLASS_TLS)
+ tlss[j] = info->tls + i;
+ else
+ {
+ ents[j] = deps[i].ent;
+ /* If the library the symbol is bound to is already
+ prelinked, adjust the value so that it is relative
+ to library base. */
+ if (mask_32bit)
+ value[j] -= (Elf32_Addr) (deps[i].start - deps[i].l_addr);
+ else
+ value[j] -= deps[i].start - deps[i].l_addr;
+ }
+ break;
+ }
+ if (ents[j] == NULL && tlss[j] == NULL && valstart[j])
+ {
+ error (0, 0, "Could not find base 0x%08llx in the list of bases `%s'",
+ valstart[j], buffer);
+ goto error_out;
+ }
+ }
+
+ for (conflict = info->conflicts[symowner]; conflict;
+ conflict = conflict->next)
+ if (conflict->symoff == symoff
+ && conflict->reloc_class == reloc_class)
+ {
+ if ((reloc_class != RTYPE_CLASS_TLS
+ && (conflict->lookup.ent != ents[0]
+ || conflict->conflict.ent != ents[1]))
+ || (reloc_class == RTYPE_CLASS_TLS
+ && (conflict->lookup.tls != tlss[0]
+ || conflict->conflict.tls != tlss[1]))
+ || conflict->lookupval != value[0]
+ || conflict->conflictval != value[1])
+ {
+ error (0, 0, "%s: Symbol `%s' with the same reloc type resolves to different values each time",
+ info->ent->filename, symname);
+ goto error_out;
+ }
+ break;
+ }
+ if (conflict == NULL)
+ {
+ conflict = malloc (sizeof (struct prelink_conflict));
+ if (conflict == NULL)
+ {
+ error (0, ENOMEM, "Cannot build list of conflicts");
+ goto error_out;
+ }
+
+ conflict->next = info->conflicts[symowner];
+ info->conflicts[symowner] = conflict;
+ if (reloc_class != RTYPE_CLASS_TLS)
+ {
+ conflict->lookup.ent = ents[0];
+ conflict->conflict.ent = ents[1];
+ }
+ else
+ {
+ conflict->lookup.tls = tlss[0];
+ conflict->conflict.tls = tlss[1];
+ }
+ conflict->lookupval = value[0];
+ conflict->conflictval = value[1];
+ conflict->symoff = symoff;
+ conflict->reloc_class = reloc_class;
+ conflict->used = 0;
+ }
+ }
+ }
+ else if (strncmp (buffer, "undefined symbol: ",
+ sizeof ("undefined symbol: ") - 1) == 0 && ! undef)
+ {
+ undef = 1;
+ if (verbose)
+ error (0, 0, "Warning: %s has undefined non-weak symbols",
+ info->ent->filename);
+ }
+ } while (fgets (buffer, 8192, f) != NULL);
+
+ info->sonames = malloc (ndeps * sizeof (const char *));
+ if (info->sonames == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not record dependency SONAMEs", dso->filename);
+ goto error_out;
+ }
+
+ for (i = 0; i < ndeps; i++)
+ info->sonames[i] = deps[i].soname;
+
+ return 0;
+
+error_out:
+ for (i = 0; i < ndeps; i++)
+ free (deps[i].soname);
+ return 1;
+}
+
+int
+prelink_get_relocations (struct prelink_info *info)
+{
+ FILE *f;
+ DSO *dso = info->dso;
+ const char *argv[6];
+ const char *envp[4];
+ int i, ret, status;
+ char *p;
+ const char *dl = dynamic_linker ?: dso->arch->dynamic_linker;
+ const char *ent_filename;
+
+ if (info->ent->type == ET_DYN)
+ {
+ assert (info->ent->base == dso->base);
+ if (info->ent->end < dso->end)
+ {
+ error (0, 0, "%s: grew since it has been recorded", info->ent->filename);
+ return 0;
+ }
+ }
+ else
+ {
+ info->ent->base = dso->base;
+ info->ent->end = dso->end;
+ }
+
+ if (is_ldso_soname (info->dso->soname))
+ return 1;
+
+ info->symbol_count = (info->symtab_end - info->symtab_start)
+ / info->symtab_entsize;
+ info->symbols = calloc (sizeof (struct prelink_symbol), info->symbol_count);
+
+ if (strchr (info->ent->filename, '/') != NULL)
+ ent_filename = info->ent->filename;
+ else
+ {
+ size_t flen = strlen (info->ent->filename);
+ char *p = alloca (2 + flen + 1);
+ memcpy (p, "./", 2);
+ memcpy (p + 2, info->ent->filename, flen + 1);
+ ent_filename = p;
+ }
+ 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",
+ info->ent->filename);
+ return 0;
+ }
+
+ if (prelink_record_relocations (info, f, ent_filename))
+ ret = 0;
+
+ if ((status = execve_close (f)))
+ {
+ if (ret)
+ error (0, status == -1 ? errno : 0,
+ "%s Could not trace symbol resolving", info->ent->filename);
+ return 0;
+ }
+
+ return ret;
+}
diff --git a/trunk/src/hashtab.c b/trunk/src/hashtab.c
new file mode 100644
index 0000000..fa27446
--- /dev/null
+++ b/trunk/src/hashtab.c
@@ -0,0 +1,609 @@
+/* An expandable hash tables datatype.
+ Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
+ Contributed by Vladimir Makarov (vmakarov@cygnus.com).
+
+This file is part of the libiberty library.
+Libiberty is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public
+License as published by the Free Software Foundation; either
+version 2 of the License, or (at your option) any later version.
+
+Libiberty 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
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with libiberty; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
+
+/* This package implements basic hash table functionality. It is possible
+ to search for an entry, create an entry and destroy an entry.
+
+ Elements in the table are generic pointers.
+
+ The size of the table is not fixed; if the occupancy of the table
+ grows too high the hash table will be expanded.
+
+ The abstract data implementation is based on generalized Algorithm D
+ from Knuth's book "The art of computer programming". Hash table is
+ expanded by creation of new hash table and transferring elements from
+ the old table to the new table. */
+
+#include <config.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "hashtab.h"
+
+/* This macro defines reserved value for empty table entry. */
+
+#define EMPTY_ENTRY ((void *) 0)
+
+/* This macro defines reserved value for table entry which contained
+ a deleted element. */
+
+#define DELETED_ENTRY ((void *) 1)
+
+static unsigned long higher_prime_number (unsigned long);
+static hashval_t hash_pointer (const void *);
+static int eq_pointer (const void *, const void *);
+static int htab_expand (htab_t);
+static void **find_empty_slot_for_expand (htab_t, hashval_t);
+
+/* At some point, we could make these be NULL, and modify the
+ hash-table routines to handle NULL specially; that would avoid
+ function-call overhead for the common case of hashing pointers. */
+htab_hash htab_hash_pointer = hash_pointer;
+htab_eq htab_eq_pointer = eq_pointer;
+
+/* The following function returns a nearest prime number which is
+ greater than N, and near a power of two. */
+
+static unsigned long
+higher_prime_number (n)
+ unsigned long n;
+{
+ /* These are primes that are near, but slightly smaller than, a
+ power of two. */
+ static unsigned long primes[] = {
+ (unsigned long) 2,
+ (unsigned long) 7,
+ (unsigned long) 13,
+ (unsigned long) 31,
+ (unsigned long) 61,
+ (unsigned long) 127,
+ (unsigned long) 251,
+ (unsigned long) 509,
+ (unsigned long) 1021,
+ (unsigned long) 2039,
+ (unsigned long) 4093,
+ (unsigned long) 8191,
+ (unsigned long) 16381,
+ (unsigned long) 32749,
+ (unsigned long) 65521,
+ (unsigned long) 131071,
+ (unsigned long) 262139,
+ (unsigned long) 524287,
+ (unsigned long) 1048573,
+ (unsigned long) 2097143,
+ (unsigned long) 4194301,
+ (unsigned long) 8388593,
+ (unsigned long) 16777213,
+ (unsigned long) 33554393,
+ (unsigned long) 67108859,
+ (unsigned long) 134217689,
+ (unsigned long) 268435399,
+ (unsigned long) 536870909,
+ (unsigned long) 1073741789,
+ (unsigned long) 2147483647,
+ /* 4294967291L */
+ ((unsigned long) 2147483647) + ((unsigned long) 2147483644),
+ };
+
+ unsigned long* low = &primes[0];
+ unsigned long* high = &primes[sizeof(primes) / sizeof(primes[0])];
+
+ while (low != high)
+ {
+ unsigned long* mid = low + (high - low) / 2;
+ if (n > *mid)
+ low = mid + 1;
+ else
+ high = mid;
+ }
+
+ /* If we've run out of primes, abort. */
+ if (n > *low)
+ {
+ fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
+ abort ();
+ }
+
+ return *low;
+}
+
+/* Returns a hash code for P. */
+
+static hashval_t
+hash_pointer (p)
+ const void * p;
+{
+ return (hashval_t) ((long)p >> 3);
+}
+
+/* Returns non-zero if P1 and P2 are equal. */
+
+static int
+eq_pointer (p1, p2)
+ const void * p1;
+ const void * p2;
+{
+ return p1 == p2;
+}
+
+/* This function creates table with length slightly longer than given
+ source length. The created hash table is initiated as empty (all the
+ hash table entries are EMPTY_ENTRY). The function returns the created
+ hash table. Memory allocation may fail; it may return NULL. */
+
+htab_t
+htab_try_create (size, hash_f, eq_f, del_f)
+ size_t size;
+ htab_hash hash_f;
+ htab_eq eq_f;
+ htab_del del_f;
+{
+ htab_t result;
+
+ size = higher_prime_number (size);
+ result = (htab_t) calloc (1, sizeof (struct htab));
+ if (result == NULL)
+ return NULL;
+
+ result->entries = (void **) calloc (size, sizeof (void *));
+ if (result->entries == NULL)
+ {
+ free (result);
+ return NULL;
+ }
+
+ result->size = size;
+ result->hash_f = hash_f;
+ result->eq_f = eq_f;
+ result->del_f = del_f;
+ result->return_allocation_failure = 1;
+ return result;
+}
+
+/* This function frees all memory allocated for given hash table.
+ Naturally the hash table must already exist. */
+
+void
+htab_delete (htab)
+ htab_t htab;
+{
+ int i;
+
+ if (htab->del_f)
+ for (i = htab->size - 1; i >= 0; i--)
+ if (htab->entries[i] != EMPTY_ENTRY
+ && htab->entries[i] != DELETED_ENTRY)
+ (*htab->del_f) (htab->entries[i]);
+
+ free (htab->entries);
+ free (htab);
+}
+
+/* This function clears all entries in the given hash table. */
+
+void
+htab_empty (htab)
+ htab_t htab;
+{
+ int i;
+
+ if (htab->del_f)
+ for (i = htab->size - 1; i >= 0; i--)
+ if (htab->entries[i] != EMPTY_ENTRY
+ && htab->entries[i] != DELETED_ENTRY)
+ (*htab->del_f) (htab->entries[i]);
+
+ memset (htab->entries, 0, htab->size * sizeof (void *));
+}
+
+/* Similar to htab_find_slot, but without several unwanted side effects:
+ - Does not call htab->eq_f when it finds an existing entry.
+ - Does not change the count of elements/searches/collisions in the
+ hash table.
+ This function also assumes there are no deleted entries in the table.
+ HASH is the hash value for the element to be inserted. */
+
+static void **
+find_empty_slot_for_expand (htab, hash)
+ htab_t htab;
+ hashval_t hash;
+{
+ size_t size = htab->size;
+ hashval_t hash2 = 1 + hash % (size - 2);
+ unsigned int index = hash % size;
+
+ for (;;)
+ {
+ void **slot = htab->entries + index;
+
+ if (*slot == EMPTY_ENTRY)
+ return slot;
+ else if (*slot == DELETED_ENTRY)
+ abort ();
+
+ index += hash2;
+ if (index >= size)
+ index -= size;
+ }
+}
+
+/* The following function changes size of memory allocated for the
+ entries and repeatedly inserts the table elements. The occupancy
+ of the table after the call will be about 50%. Naturally the hash
+ table must already exist. Remember also that the place of the
+ table entries is changed. If memory allocation failures are allowed,
+ this function will return zero, indicating that the table could not be
+ expanded. If all goes well, it will return a non-zero value. */
+
+static int
+htab_expand (htab)
+ htab_t htab;
+{
+ void **oentries;
+ void **olimit;
+ void **p;
+
+ oentries = htab->entries;
+ olimit = oentries + htab->size;
+
+ htab->size = higher_prime_number (htab->size * 2);
+
+ if (htab->return_allocation_failure)
+ {
+ void **nentries = (void **) calloc (htab->size, sizeof (void **));
+ if (nentries == NULL)
+ return 0;
+ htab->entries = nentries;
+ }
+
+ htab->n_elements -= htab->n_deleted;
+ htab->n_deleted = 0;
+
+ p = oentries;
+ do
+ {
+ void * x = *p;
+
+ if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
+ {
+ void **q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
+
+ *q = x;
+ }
+
+ p++;
+ }
+ while (p < olimit);
+
+ free (oentries);
+ return 1;
+}
+
+/* This function searches for a hash table entry equal to the given
+ element. It cannot be used to insert or delete an element. */
+
+void *
+htab_find_with_hash (htab, element, hash)
+ htab_t htab;
+ const void * element;
+ hashval_t hash;
+{
+ unsigned int index;
+ hashval_t hash2;
+ size_t size;
+ void * entry;
+
+ htab->searches++;
+ size = htab->size;
+ index = hash % size;
+
+ entry = htab->entries[index];
+ if (entry == EMPTY_ENTRY
+ || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
+ return entry;
+
+ hash2 = 1 + hash % (size - 2);
+
+ for (;;)
+ {
+ htab->collisions++;
+ index += hash2;
+ if (index >= size)
+ index -= size;
+
+ entry = htab->entries[index];
+ if (entry == EMPTY_ENTRY
+ || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
+ return entry;
+ }
+}
+
+/* Like htab_find_slot_with_hash, but compute the hash value from the
+ element. */
+
+void *
+htab_find (htab, element)
+ htab_t htab;
+ const void * element;
+{
+ return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
+}
+
+/* This function searches for a hash table slot containing an entry
+ equal to the given element. To delete an entry, call this with
+ INSERT = 0, then call htab_clear_slot on the slot returned (possibly
+ after doing some checks). To insert an entry, call this with
+ INSERT = 1, then write the value you want into the returned slot.
+ When inserting an entry, NULL may be returned if memory allocation
+ fails. */
+
+void **
+htab_find_slot_with_hash (htab, element, hash, insert)
+ htab_t htab;
+ const void * element;
+ hashval_t hash;
+ enum insert_option insert;
+{
+ void **first_deleted_slot;
+ unsigned int index;
+ hashval_t hash2;
+ size_t size;
+
+ if (insert == INSERT && htab->size * 3 <= htab->n_elements * 4
+ && htab_expand (htab) == 0)
+ return NULL;
+
+ size = htab->size;
+ hash2 = 1 + hash % (size - 2);
+ index = hash % size;
+
+ htab->searches++;
+ first_deleted_slot = NULL;
+
+ for (;;)
+ {
+ void * entry = htab->entries[index];
+ if (entry == EMPTY_ENTRY)
+ {
+ if (insert == NO_INSERT)
+ return NULL;
+
+ htab->n_elements++;
+
+ if (first_deleted_slot)
+ {
+ *first_deleted_slot = EMPTY_ENTRY;
+ return first_deleted_slot;
+ }
+
+ return &htab->entries[index];
+ }
+
+ if (entry == DELETED_ENTRY)
+ {
+ if (!first_deleted_slot)
+ first_deleted_slot = &htab->entries[index];
+ }
+ else if ((*htab->eq_f) (entry, element))
+ return &htab->entries[index];
+
+ htab->collisions++;
+ index += hash2;
+ if (index >= size)
+ index -= size;
+ }
+}
+
+/* Like htab_find_slot_with_hash, but compute the hash value from the
+ element. */
+
+void **
+htab_find_slot (htab, element, insert)
+ htab_t htab;
+ const void * element;
+ enum insert_option insert;
+{
+ return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
+ insert);
+}
+
+/* This function deletes an element with the given value from hash
+ table. If there is no matching element in the hash table, this
+ function does nothing. */
+
+void
+htab_remove_elt (htab, element)
+ htab_t htab;
+ void * element;
+{
+ void **slot;
+
+ slot = htab_find_slot (htab, element, NO_INSERT);
+ if (*slot == EMPTY_ENTRY)
+ return;
+
+ if (htab->del_f)
+ (*htab->del_f) (*slot);
+
+ *slot = DELETED_ENTRY;
+ htab->n_deleted++;
+}
+
+/* This function clears a specified slot in a hash table. It is
+ useful when you've already done the lookup and don't want to do it
+ again. */
+
+void
+htab_clear_slot (htab, slot)
+ htab_t htab;
+ void **slot;
+{
+ if (slot < htab->entries || slot >= htab->entries + htab->size
+ || *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY)
+ abort ();
+
+ if (htab->del_f)
+ (*htab->del_f) (*slot);
+
+ *slot = DELETED_ENTRY;
+ htab->n_deleted++;
+}
+
+/* This function scans over the entire hash table calling
+ CALLBACK for each live entry. If CALLBACK returns false,
+ the iteration stops. INFO is passed as CALLBACK's second
+ argument. */
+
+void
+htab_traverse (htab, callback, info)
+ htab_t htab;
+ htab_trav callback;
+ void * info;
+{
+ void **slot = htab->entries;
+ void **limit = slot + htab->size;
+
+ do
+ {
+ void * x = *slot;
+
+ if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
+ if (!(*callback) (slot, info))
+ break;
+ }
+ while (++slot < limit);
+}
+
+/* Return the current size of given hash table. */
+
+size_t
+htab_size (htab)
+ htab_t htab;
+{
+ return htab->size;
+}
+
+/* Return the current number of elements in given hash table. */
+
+size_t
+htab_elements (htab)
+ htab_t htab;
+{
+ return htab->n_elements - htab->n_deleted;
+}
+
+/* Return the fraction of fixed collisions during all work with given
+ hash table. */
+
+double
+htab_collisions (htab)
+ htab_t htab;
+{
+ if (htab->searches == 0)
+ return 0.0;
+
+ return (double) htab->collisions / (double) htab->searches;
+}
+
+#ifndef NDEBUG
+void
+htab_dump (htab, name, dumpfn)
+ htab_t htab;
+ const char *name;
+ htab_dumpfn dumpfn;
+{
+ FILE *f = fopen (name, "w");
+ size_t i, j;
+
+ if (f == NULL)
+ abort ();
+ fprintf (f, "size %zd n_elements %zd n_deleted %zd\n",
+ htab->size, htab->n_elements, htab->n_deleted);
+ for (i = 0; i < htab->size; ++i)
+ {
+ if (htab->entries [i] == EMPTY_ENTRY
+ || htab->entries [i] == DELETED_ENTRY)
+ {
+ for (j = i + 1; j < htab->size; ++j)
+ if (htab->entries [j] != htab->entries [i])
+ break;
+ fprintf (f, "%c%zd\n",
+ htab->entries [i] == EMPTY_ENTRY ? 'E' : 'D',
+ j - i);
+ i = j - 1;
+ }
+ else
+ {
+ fputc ('V', f);
+ (*dumpfn) (f, htab->entries [i]);
+ }
+ }
+ fclose (f);
+}
+
+void
+htab_restore (htab, name, restorefn)
+ htab_t htab;
+ const char *name;
+ htab_restorefn restorefn;
+{
+ FILE *f = fopen (name, "r");
+ size_t size, n_elements, n_deleted, i, j, k;
+ int c;
+
+ if (f == NULL)
+ abort ();
+ if (fscanf (f, "size %zd n_elements %zd n_deleted %zd\n",
+ &size, &n_elements, &n_deleted) != 3)
+ abort ();
+ htab_empty (htab);
+ free (htab->entries);
+ htab->entries = (void **) calloc (size, sizeof (void *));
+ if (htab->entries == NULL)
+ abort ();
+ htab->size = size;
+ htab->n_elements = n_elements;
+ htab->n_deleted = n_deleted;
+ for (i = 0; i < htab->size; ++i)
+ {
+ switch ((c = fgetc (f)))
+ {
+ case 'E':
+ case 'D':
+ if (fscanf (f, "%zd\n", &j) != 1)
+ abort ();
+ if (i + j > htab->size)
+ abort ();
+ if (c == 'D')
+ for (k = i; k < i + j; ++k)
+ htab->entries [k] = DELETED_ENTRY;
+ i += j - 1;
+ break;
+ case 'V':
+ htab->entries [i] = (*restorefn) (f);
+ break;
+ default:
+ abort ();
+ }
+ }
+ fclose (f);
+}
+#endif
diff --git a/trunk/src/hashtab.h b/trunk/src/hashtab.h
new file mode 100644
index 0000000..31e63e0
--- /dev/null
+++ b/trunk/src/hashtab.h
@@ -0,0 +1,155 @@
+/* An expandable hash tables datatype.
+ Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+ Contributed by Vladimir Makarov (vmakarov@cygnus.com).
+
+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 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty 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. */
+
+/* This package implements basic hash table functionality. It is possible
+ to search for an entry, create an entry and destroy an entry.
+
+ Elements in the table are generic pointers.
+
+ The size of the table is not fixed; if the occupancy of the table
+ grows too high the hash table will be expanded.
+
+ The abstract data implementation is based on generalized Algorithm D
+ from Knuth's book "The art of computer programming". Hash table is
+ expanded by creation of new hash table and transferring elements from
+ the old table to the new table. */
+
+#ifndef __HASHTAB_H__
+#define __HASHTAB_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* The type for a hash code. */
+typedef unsigned int hashval_t;
+
+/* Callback function pointer types. */
+
+/* Calculate hash of a table entry. */
+typedef hashval_t (*htab_hash) (const void *);
+
+/* Compare a table entry with a possible entry. The entry already in
+ the table always comes first, so the second element can be of a
+ different type (but in this case htab_find and htab_find_slot
+ cannot be used; instead the variants that accept a hash value
+ must be used). */
+typedef int (*htab_eq) (const void *, const void *);
+
+/* Cleanup function called whenever a live element is removed from
+ the hash table. */
+typedef void (*htab_del) (void *);
+
+/* Function called by htab_traverse for each live element. The first
+ arg is the slot of the element (which can be passed to htab_clear_slot
+ if desired), the second arg is the auxiliary pointer handed to
+ htab_traverse. Return 1 to continue scan, 0 to stop. */
+typedef int (*htab_trav) (void **, void *);
+
+/* Hash tables are of the following type. The structure
+ (implementation) of this type is not needed for using the hash
+ tables. All work with hash table should be executed only through
+ functions mentioned below. */
+
+struct htab
+{
+ /* Pointer to hash function. */
+ htab_hash hash_f;
+
+ /* Pointer to comparison function. */
+ htab_eq eq_f;
+
+ /* Pointer to cleanup function. */
+ htab_del del_f;
+
+ /* Table itself. */
+ void **entries;
+
+ /* Current size (in entries) of the hash table */
+ size_t size;
+
+ /* Current number of elements including also deleted elements */
+ size_t n_elements;
+
+ /* Current number of deleted elements in the table */
+ size_t n_deleted;
+
+ /* The following member is used for debugging. Its value is number
+ of all calls of `htab_find_slot' for the hash table. */
+ unsigned int searches;
+
+ /* The following member is used for debugging. Its value is number
+ of collisions fixed for time of work with the hash table. */
+ unsigned int collisions;
+
+ /* This is non-zero if we are allowed to return NULL for function calls
+ that allocate memory. */
+ int return_allocation_failure;
+};
+
+typedef struct htab *htab_t;
+
+/* An enum saying whether we insert into the hash table or not. */
+enum insert_option {NO_INSERT, INSERT};
+
+/* The prototypes of the package functions. */
+
+/* This function is like htab_create, but may return NULL if memory
+ allocation fails, and also signals that htab_find_slot_with_hash and
+ htab_find_slot are allowed to return NULL when inserting. */
+extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del);
+extern void htab_delete (htab_t);
+extern void htab_empty (htab_t);
+
+extern void *htab_find (htab_t, const void *);
+extern void **htab_find_slot (htab_t, const void *, enum insert_option);
+extern void *htab_find_with_hash (htab_t, const void *, hashval_t);
+extern void **htab_find_slot_with_hash (htab_t, const void *, hashval_t,
+ enum insert_option);
+extern void htab_clear_slot (htab_t, void **);
+extern void htab_remove_elt (htab_t, void *);
+
+extern void htab_traverse (htab_t, htab_trav, void *);
+
+extern size_t htab_size (htab_t);
+extern size_t htab_elements (htab_t);
+extern double htab_collisions (htab_t);
+
+/* A hash function for pointers. */
+extern htab_hash htab_hash_pointer;
+
+/* An equality function for pointers. */
+extern htab_eq htab_eq_pointer;
+
+#ifndef NDEBUG
+
+#include <stdio.h>
+
+typedef void (*htab_dumpfn) (FILE *, const void *);
+typedef void *(*htab_restorefn) (FILE *);
+
+extern void htab_dump (htab_t, const char *, htab_dumpfn);
+extern void htab_restore (htab_t, const char *, htab_restorefn);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __HASHTAB_H */
diff --git a/trunk/src/layout.c b/trunk/src/layout.c
new file mode 100644
index 0000000..9292425
--- /dev/null
+++ b/trunk/src/layout.c
@@ -0,0 +1,647 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 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 <alloca.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelinktab.h"
+#include "layout.h"
+
+#ifndef NDEBUG
+# define DEBUG_LAYOUT
+#endif
+
+#ifdef DEBUG_LAYOUT
+void
+print_ent (struct prelink_entry *e)
+{
+ printf ("%s: %08x %08x/%08x\n",
+ e->filename, (int)e->base, (int)e->end, (int)e->layend);
+}
+
+void
+print_list (struct prelink_entry *e)
+{
+ for (; e; e = e->next)
+ print_ent (e);
+ printf ("\n");
+}
+#endif
+
+static int
+find_arches (void **p, void *info)
+{
+ struct layout_libs *l = (struct layout_libs *) info;
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+ int i;
+
+ if (e->type == ET_DYN || e->type == ET_EXEC
+ || e->type == ET_CACHE_DYN || e->type == ET_CACHE_EXEC)
+ {
+ for (i = 0; i < l->nbinlibs; ++i)
+ if ((l->binlibs[i]->flags & (PCF_ELF64 | PCF_MACHINE))
+ == (e->flags & (PCF_ELF64 | PCF_MACHINE)))
+ return 1;
+
+ l->binlibs[l->nbinlibs++] = e;
+ }
+
+ return 1;
+}
+
+static int
+find_libs (void **p, void *info)
+{
+ struct layout_libs *l = (struct layout_libs *) info;
+ struct prelink_entry *e = * (struct prelink_entry **) p;
+
+ if ((e->flags & (PCF_ELF64 | PCF_MACHINE)) != l->flags)
+ return 1;
+
+ if (e->type == ET_DYN || e->type == ET_EXEC
+ || e->type == ET_CACHE_DYN || e->type == ET_CACHE_EXEC)
+ l->binlibs[l->nbinlibs++] = e;
+ if (e->type == ET_DYN || e->type == ET_CACHE_DYN)
+ l->libs[l->nlibs++] = e;
+ if (force)
+ e->done = 0;
+ if (e->type == ET_CACHE_DYN || e->type == ET_CACHE_EXEC)
+ e->done = 2;
+ if (e->base & (l->max_page_size - 1))
+ {
+ e->done = 0;
+ e->end -= e->base;
+ e->base = 0;
+ }
+
+ return 1;
+}
+
+static int
+refs_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+ int i;
+
+ /* Dynamic linkers first. */
+ if (! a->ndepends && b->ndepends)
+ return -1;
+ if (a->ndepends && ! b->ndepends)
+ return 1;
+ /* Most widely used libraries first. */
+ if (a->refs > b->refs)
+ return -1;
+ if (a->refs < b->refs)
+ return 1;
+ /* Largest libraries first. */
+ if (a->layend - a->base > b->layend - b->base)
+ return -1;
+ if (a->layend - a->base < b->layend - b->base)
+ return 1;
+ if (a->refs)
+ {
+ i = strcmp (a->soname, b->soname);
+ if (i)
+ return i;
+ }
+ return strcmp (a->filename, b->filename);
+}
+
+static int
+refs_rnd_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+ int i, refs;
+
+ /* Dynamic linkers first. */
+ if (! a->ndepends && b->ndepends)
+ return -1;
+ if (a->ndepends && ! b->ndepends)
+ return 1;
+ /* Most widely used libraries first with some randomization. */
+ refs = a->refs < b->refs ? a->refs : b->refs;
+ if (refs < 8)
+ i = 1;
+ else if (refs < 32)
+ i = 2;
+ else if (refs < 128)
+ i = 4;
+ else
+ i = 8;
+ if (a->refs > b->refs && a->refs - b->refs >= i)
+ return -1;
+ if (a->refs < b->refs && b->refs - a->refs >= i)
+ return 1;
+ if (a->u.tmp < b->u.tmp)
+ return -1;
+ if (a->u.tmp > b->u.tmp)
+ return 1;
+ /* Largest libraries first. */
+ if (a->layend - a->base > b->layend - b->base)
+ return -1;
+ if (a->layend - a->base < b->layend - b->base)
+ return 1;
+ if (a->refs && b->refs)
+ {
+ i = strcmp (a->soname, b->soname);
+ if (i)
+ return i;
+ }
+ return strcmp (a->filename, b->filename);
+}
+
+static int
+addr_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+
+ if (! a->done)
+ return b->done ? 1 : 0;
+ else if (! b->done)
+ return -1;
+ if (a->base < b->base)
+ return -1;
+ else if (a->base > b->base)
+ return 1;
+ if (a->layend < b->layend)
+ return -1;
+ else if (a->layend > b->layend)
+ return 1;
+ return 0;
+}
+
+int deps_cmp (const void *A, const void *B)
+{
+ struct prelink_entry *a = * (struct prelink_entry **) A;
+ struct prelink_entry *b = * (struct prelink_entry **) B;
+
+ if (a->base < b->base)
+ return -1;
+ if (a->base > b->base)
+ return 1;
+ return 0;
+}
+
+int
+layout_libs (void)
+{
+ struct layout_libs l;
+ int arch, *arches, narches;
+ struct prelink_entry **plibs, **pbinlibs;
+
+ memset (&l, 0, sizeof (l));
+ l.libs = plibs =
+ (struct prelink_entry **) alloca (prelink_entry_count
+ * sizeof (struct prelink_entry *));
+ l.binlibs = pbinlibs =
+ (struct prelink_entry **) alloca (prelink_entry_count
+ * sizeof (struct prelink_entry *));
+ htab_traverse (prelink_filename_htab, find_arches, &l);
+ narches = l.nbinlibs;
+ arches = (int *) alloca (narches * sizeof (int));
+ for (arch = 0; arch < narches; ++arch)
+ arches[arch] = l.binlibs[arch]->flags & (PCF_ELF64 | PCF_MACHINE);
+
+ for (arch = 0; arch < narches; ++arch)
+ {
+ struct PLArch *plarch;
+ extern struct PLArch __start_pl_arch[], __stop_pl_arch[];
+ int i, j, k, m, done, class;
+ GElf_Addr mmap_start, mmap_base, mmap_end, mmap_fin, max_page_size;
+ GElf_Addr base, size;
+ struct prelink_entry *list, *e, *fake, **deps;
+ struct prelink_entry fakeent;
+ int fakecnt;
+ int (*layout_libs_pre) (struct layout_libs *l);
+ int (*layout_libs_post) (struct layout_libs *l);
+
+ for (plarch = __start_pl_arch; plarch < __stop_pl_arch; plarch++)
+ if (plarch->class == (arches[arch] & PCF_ELF64 ? ELFCLASS64 : ELFCLASS32)
+ && plarch->machine == (arches[arch] & PCF_MACHINE))
+ break;
+
+ if (plarch == __stop_pl_arch)
+ error (EXIT_FAILURE, 0, "%d-bit ELF e_machine %04x not supported",
+ (arches[arch] & PCF_ELF64) ? 64 : 32, arches[arch] & PCF_MACHINE);
+
+ list = NULL;
+ fake = NULL;
+ fakecnt = 0;
+ memset (&l, 0, sizeof (l));
+ l.flags = arches[arch];
+ l.libs = plibs;
+ l.binlibs = pbinlibs;
+ l.max_page_size = plarch->max_page_size;
+ htab_traverse (prelink_filename_htab, find_libs, &l);
+ max_page_size = plarch->max_page_size;
+
+ /* Make sure there is some room between libraries. */
+ for (i = 0; i < l.nlibs; ++i)
+ l.libs[i]->layend = (l.libs[i]->end + 8192 + max_page_size - 1)
+ & ~(max_page_size - 1);
+
+ if (plarch->layout_libs_init)
+ {
+ plarch->layout_libs_init (&l);
+ mmap_base = l.mmap_base;
+ mmap_end = l.mmap_end;
+ }
+ else
+ {
+ mmap_base = plarch->mmap_base;
+ mmap_end = plarch->mmap_end;
+ }
+ if (mmap_reg_start != ~(GElf_Addr) 0)
+ mmap_base = mmap_reg_start;
+ if (mmap_reg_end != ~(GElf_Addr) 0)
+ mmap_end = mmap_reg_end;
+ if (mmap_base >= mmap_end)
+ error (EXIT_FAILURE, 0,
+ "--mmap-region-start cannot be bigger than --mmap-region-end");
+ class = plarch->class;
+ /* The code below relies on having a VA slot as big as <mmap_base,mmap_end)
+ above mmap_end for -R. */
+ if (mmap_end + (mmap_end - mmap_base) <= mmap_end)
+ random_base = 0;
+ layout_libs_pre = plarch->layout_libs_pre;
+ layout_libs_post = plarch->layout_libs_post;
+
+ deps = (struct prelink_entry **)
+ alloca (l.nlibs * sizeof (struct prelink_entry *));
+
+ /* Now see which already prelinked libraries have to be
+ re-prelinked to avoid overlaps. */
+ for (i = 0; i < l.nbinlibs; ++i)
+ {
+ for (j = 0, k = 0; j < l.binlibs[i]->ndepends; ++j)
+ if (l.binlibs[i]->depends[j]->type == ET_DYN
+ && l.binlibs[i]->depends[j]->done)
+ deps[k++] = l.binlibs[i]->depends[j];
+ if (k)
+ {
+ qsort (deps, k, sizeof (struct prelink_entry *), deps_cmp);
+ for (j = 1; j < k; ++j)
+ if (deps[j]->base < deps[j - 1]->end
+ && (deps[j]->type == ET_DYN
+ || deps[j - 1]->type == ET_DYN))
+ {
+ if (deps[j - 1]->refs < deps[j]->refs)
+ --j;
+ deps[j]->done = 0;
+ --k;
+ memmove (deps + j, deps + j + 1, (k - j) * sizeof (*deps));
+ if (j > 0)
+ --j;
+ }
+ }
+ }
+
+ /* If layout_libs_init or the for cycle above cleared
+ done flags for some libraries, make sure all libraries
+ that depend on them are re-prelinked as well. */
+ for (i = 0; i < l.nlibs; ++i)
+ if (l.libs[i]->done)
+ for (j = 0; j < l.libs[i]->ndepends; ++j)
+ if (l.libs[i]->depends[j]->done == 0)
+ {
+ l.libs[i]->done = 0;
+ break;
+ }
+
+ /* Put the already prelinked libs into double linked list. */
+ qsort (l.libs, l.nlibs, sizeof (struct prelink_entry *), addr_cmp);
+ for (i = 0; i < l.nlibs; ++i)
+ if (! l.libs[i]->done || l.libs[i]->layend >= mmap_base)
+ break;
+ j = 0;
+ if (i < l.nlibs && l.libs[i]->done)
+ {
+ if (l.libs[i]->base < mmap_base)
+ random_base = 0;
+ for (j = i + 1; j < l.nlibs; ++j)
+ {
+ if (! l.libs[j]->done || l.libs[j]->base >= mmap_end)
+ break;
+
+ if (l.libs[j]->base < mmap_base || l.libs[j]->layend > mmap_end)
+ random_base = 0;
+ l.libs[j]->prev = l.libs[j - 1];
+ l.libs[j - 1]->next = l.libs[j];
+ }
+ list = l.libs[i];
+ list->prev = l.libs[j - 1];
+ while (j < l.nlibs && l.libs[j]->done) ++j;
+ }
+
+ mmap_start = mmap_base;
+ mmap_fin = mmap_end;
+ done = 1;
+ if (random_base & 2)
+ {
+ mmap_start = seed;
+ if (mmap_start < mmap_base || mmap_start >= mmap_end)
+ mmap_start = mmap_base;
+
+ mmap_start = (mmap_start + max_page_size - 1) & ~(max_page_size - 1);
+ }
+ else if (random_base)
+ {
+ int fd = open ("/dev/urandom", O_RDONLY);
+
+ mmap_start = 0;
+ if (fd != -1)
+ {
+ GElf_Addr x;
+
+ if (read (fd, &x, sizeof (x)) == sizeof (x))
+ {
+ mmap_start = x % (mmap_end - mmap_base);
+ mmap_start += mmap_base;
+ }
+
+ close (fd);
+ }
+
+ if (! mmap_start)
+ {
+ mmap_start = ((mmap_end - mmap_base) >> 16)
+ * (time (NULL) & 0xffff);
+ mmap_start += mmap_base;
+ }
+
+ seed = mmap_start;
+ mmap_start = (mmap_start + max_page_size - 1) & ~(max_page_size - 1);
+ }
+ if (random_base)
+ {
+ srandom (mmap_start >> 12);
+ for (i = 0; i < l.nlibs; ++i)
+ l.libs[i]->u.tmp = random ();
+ qsort (l.libs, l.nlibs, sizeof (struct prelink_entry *), refs_rnd_cmp);
+ }
+ else
+ qsort (l.libs, l.nlibs, sizeof (struct prelink_entry *), refs_cmp);
+
+ if (verbose && l.nlibs > j)
+ {
+ printf ("Laying out %d libraries in virtual address space %0*llx-%0*llx\n",
+ l.nlibs - j, class == ELFCLASS32 ? 8 : 16, (long long) mmap_base,
+ class == ELFCLASS32 ? 8 : 16, (long long) mmap_end);
+ if (mmap_start != mmap_base)
+ printf ("Random base 0x%0*llx\n", class == ELFCLASS32 ? 8 : 16,
+ (long long) mmap_start);
+ }
+
+ if (layout_libs_pre)
+ {
+ l.list = list;
+ l.mmap_base = mmap_base;
+ l.mmap_start = mmap_start;
+ l.mmap_end = mmap_end;
+ layout_libs_pre (&l);
+ list = l.list;
+ mmap_base = l.mmap_base;
+ mmap_start = l.mmap_start;
+ mmap_fin = l.mmap_fin;
+ mmap_end = l.mmap_end;
+ fake = l.fake;
+ fakecnt = l.fakecnt;
+ }
+
+ if (mmap_start != mmap_base && list)
+ {
+ for (e = list; e != NULL; e = e->next)
+ {
+ if (e->base >= mmap_start)
+ break;
+ if (e->layend > mmap_start)
+ mmap_start = (e->layend + max_page_size - 1)
+ & ~(max_page_size - 1);
+ e->base += mmap_end - mmap_base;
+ e->end += mmap_end - mmap_base;
+ e->layend += mmap_end - mmap_base;
+ e->done |= 0x80;
+ }
+
+ if (mmap_start < mmap_end)
+ {
+ if (e && e != list)
+ {
+ memset (&fakeent, 0, sizeof (fakeent));
+ fakeent.u.tmp = -1;
+ fakeent.base = mmap_end;
+ fakeent.end = mmap_end;
+ fakeent.layend = mmap_end;
+ fake = &fakeent;
+ fakecnt = 1;
+ fakeent.prev = list->prev;
+ fakeent.next = list;
+ list->prev = fake;
+ fakeent.prev->next = fake;
+ list = e;
+ e->prev->next = NULL;
+ }
+ }
+ else
+ {
+ mmap_start = mmap_base;
+ for (e = list; e != NULL; e = e->next)
+ if (e->done & 0x80)
+ {
+ e->done &= ~0x80;
+ e->base -= mmap_end - mmap_base;
+ e->end -= mmap_end - mmap_base;
+ e->layend -= mmap_end - mmap_base;
+ }
+ }
+ }
+
+ if (mmap_start != mmap_base)
+ {
+ done |= 0x80;
+ mmap_fin = mmap_end + (mmap_start - mmap_base);
+ }
+
+ for (i = 0; i < l.nlibs; ++i)
+ l.libs[i]->u.tmp = -1;
+ m = -1;
+
+ for (i = 0; i < l.nlibs; ++i)
+ if (! l.libs[i]->done)
+ {
+ if (conserve_memory)
+ {
+ /* If conserving virtual address space, only consider libraries
+ which ever appear together with this one. Otherwise consider
+ all libraries. */
+ m = i;
+ for (j = 0; j < l.nbinlibs; ++j)
+ {
+ for (k = 0; k < l.binlibs[j]->ndepends; ++k)
+ if (l.binlibs[j]->depends[k] == l.libs[i])
+ {
+ for (k = 0; k < l.binlibs[j]->ndepends; ++k)
+ l.binlibs[j]->depends[k]->u.tmp = m;
+ break;
+ }
+ }
+ for (j = 0; j < fakecnt; ++j)
+ fake[j].u.tmp = m;
+ }
+
+ size = l.libs[i]->layend - l.libs[i]->base;
+ base = mmap_start;
+ for (e = list; e; e = e->next)
+ if (e->u.tmp == m)
+ {
+ if (base + size <= e->base)
+ goto found;
+
+ if (base < e->layend)
+ base = e->layend;
+ }
+
+ if (base + size > mmap_fin)
+ goto not_found;
+found:
+ l.libs[i]->end += base - l.libs[i]->base;
+ l.libs[i]->base = base;
+ l.libs[i]->layend = base + size;
+ if (base >= mmap_end)
+ l.libs[i]->done = done;
+ else
+ l.libs[i]->done = 1;
+ if (list == NULL)
+ {
+ list = l.libs[i];
+ list->prev = list;
+ }
+ else
+ {
+ if (e == NULL)
+ e = list->prev;
+ else
+ e = e->prev;
+ while (e != list && e->base > base)
+ e = e->prev;
+ if (e->base > base)
+ {
+ l.libs[i]->next = list;
+ l.libs[i]->prev = list->prev;
+ list->prev = l.libs[i];
+ list = l.libs[i];
+ }
+ else
+ {
+ l.libs[i]->next = e->next;
+ l.libs[i]->prev = e;
+ if (e->next)
+ e->next->prev = l.libs[i];
+ else
+ list->prev = l.libs[i];
+ e->next = l.libs[i];
+ }
+ }
+#ifdef DEBUG_LAYOUT
+ {
+ struct prelink_entry *last = list;
+ base = 0;
+ for (e = list; e; last = e, e = e->next)
+ {
+ if (e->base < base)
+ abort ();
+ base = e->base;
+ if ((e == list && e->prev->next != NULL)
+ || (e != list && e->prev->next != e))
+ abort ();
+ }
+ if (list->prev != last)
+ abort ();
+ }
+#endif
+ continue;
+
+not_found:
+ error (EXIT_FAILURE, 0, "Could not find virtual address slot for %s",
+ l.libs[i]->filename);
+ }
+
+ if (layout_libs_post)
+ {
+ l.list = list;
+ layout_libs_post (&l);
+ }
+
+ if (done & 0x80)
+ for (e = list; e != NULL; e = e->next)
+ if (e->done & 0x80)
+ {
+ e->done &= ~0x80;
+ e->base -= mmap_end - mmap_base;
+ e->end -= mmap_end - mmap_base;
+ e->layend -= mmap_base - mmap_base;
+ }
+
+ if (verbose)
+ {
+ if (narches == 1)
+ printf ("Assigned virtual address space slots for libraries:\n");
+ else
+ printf ("Assigned virtual address space slots for %d-bit %s ELF libraries:\n",
+ class == ELFCLASS32 ? 32 : 64, plarch->name);
+
+ for (i = 0; i < l.nlibs; ++i)
+ if (l.libs[i]->done >= 1)
+ printf ("%-60s %0*llx-%0*llx\n", l.libs[i]->filename,
+ class == ELFCLASS32 ? 8 : 16, (long long) l.libs[i]->base,
+ class == ELFCLASS32 ? 8 : 16, (long long) l.libs[i]->end);
+ }
+
+#ifdef DEBUG_LAYOUT
+ for (i = 0; i < l.nbinlibs; ++i)
+ {
+ for (j = 0; j < l.binlibs[i]->ndepends; ++j)
+ if ((l.binlibs[i]->depends[j]->type != ET_DYN
+ && l.binlibs[i]->depends[j]->type != ET_CACHE_DYN)
+ || l.binlibs[i]->depends[j]->done == 0)
+ break;
+ if (j < l.binlibs[i]->ndepends)
+ continue;
+ memcpy (deps, l.binlibs[i]->depends,
+ l.binlibs[i]->ndepends * sizeof (struct prelink_entry *));
+ qsort (deps, l.binlibs[i]->ndepends, sizeof (struct prelink_entry *),
+ deps_cmp);
+ for (j = 1; j < l.binlibs[i]->ndepends; ++j)
+ if (deps[j]->base
+ < ((deps[j - 1]->end + max_page_size - 1)
+ & ~(max_page_size - 1))
+ && (deps[j]->type == ET_DYN || deps[j - 1]->type == ET_DYN))
+ abort ();
+ }
+#endif
+ }
+
+ return 0;
+}
diff --git a/trunk/src/layout.h b/trunk/src/layout.h
new file mode 100644
index 0000000..f481d22
--- /dev/null
+++ b/trunk/src/layout.h
@@ -0,0 +1,35 @@
+/* Copyright (C) 2001, 2004, 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. */
+
+#ifndef LAYOUT_H
+#define LAYOUT_H
+
+struct layout_libs
+ {
+ struct prelink_entry **libs;
+ struct prelink_entry **binlibs;
+ struct prelink_entry *list;
+ struct prelink_entry *fake;
+ GElf_Addr mmap_base, mmap_start, mmap_fin, mmap_end, max_page_size;
+ void *arch_data;
+ int flags;
+ int nlibs;
+ int nbinlibs;
+ int fakecnt;
+ };
+
+#endif /* LAYOUT_H */
diff --git a/trunk/src/ld-do-lookup.h b/trunk/src/ld-do-lookup.h
new file mode 100644
index 0000000..62a1280
--- /dev/null
+++ b/trunk/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/trunk/src/ld-libs.c b/trunk/src/ld-libs.c
new file mode 100644
index 0000000..b169cde
--- /dev/null
+++ b/trunk/src/ld-libs.c
@@ -0,0 +1,1371 @@
+/* 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 <argp.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"
+
+struct search_path
+{
+ int maxlen, count, allocated;
+ char **dirs;
+};
+
+struct search_path ld_dirs, ld_library_search_path;
+int host_paths;
+
+void string_to_path (struct search_path *path, const char *string);
+
+const char *argp_program_version = "prelink-rtld 1.0 (20061201) Wind River Linux";
+
+const char *argp_program_bug_address = "<support@windriver.com>";
+
+static char argp_doc[] = "prelink-rtld -- program to simulate the runtime linker";
+
+#define OPT_SYSROOT 0x8c
+#define OPT_LIBRARY_PATH 0x8e
+#define OPT_TARGET_PATHS 0x8f
+
+static struct argp_option options[] = {
+ {"library-path", OPT_LIBRARY_PATH, "LIBRARY_PATH", 0, "Set library search path to LIBRARY_PATH" },
+ {"root", OPT_SYSROOT, "ROOT_PATH", 0, "Prefix all paths with ROOT_PATH" },
+ {"target-paths", OPT_TARGET_PATHS, 0, 0, "Specified paths are based on ROOT_PATH" },
+ { 0 }
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case OPT_SYSROOT:
+ sysroot = arg;
+ break;
+ case OPT_LIBRARY_PATH:
+ string_to_path(&ld_library_search_path, arg);
+ break;
+ case OPT_TARGET_PATHS:
+ host_paths = 0;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+#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;
+}
+
+
+/****/
+
+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, 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, 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, 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;
+ }
+ }
+}
+
+static struct argp argp = { options, parse_opt, "[FILES]", argp_doc };
+
+struct ldlibs_link_map *requested_map;
+
+static void process_one_dso (DSO *dso, int host_paths);
+
+int
+main(int argc, char **argv)
+{
+ int remaining;
+ int multiple = 0;
+ host_paths = 1;
+
+ 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
+
+ elf_version (EV_CURRENT);
+
+ argp_parse (&argp, argc, argv, 0, &remaining, 0);
+
+ if (sysroot)
+ sysroot = prelink_canonicalize (sysroot, NULL);
+
+ if (remaining == argc)
+ error (1, 0, "missing file arguments\nTry `%s: --help' for more information.", argv[0]);
+
+ if ((argc-remaining) >= 2)
+ multiple = 1;
+
+ while (remaining < argc)
+ {
+ DSO *dso = NULL;
+ int i, fd;
+
+ if (host_paths)
+ fd = open (argv[remaining], O_RDONLY);
+ else
+ fd = wrap_open (argv[remaining], O_RDONLY);
+
+ if (fd >= 0)
+ dso = fdopen_dso (fd, argv[remaining]);
+
+ if (dso == NULL)
+ error (1, errno, "Could not open %s", argv[remaining]);
+
+ load_ld_so_conf (gelf_getclass (dso->elf) == ELFCLASS64);
+
+ if (multiple)
+ printf ("%s:\n", argv[remaining]);
+
+ 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);
+
+ remaining++;
+ }
+
+ 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/trunk/src/ld-libs.h b/trunk/src/ld-libs.h
new file mode 100644
index 0000000..48dd555
--- /dev/null
+++ b/trunk/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/trunk/src/ld-lookup.c b/trunk/src/ld-lookup.c
new file mode 100644
index 0000000..69b6bd1
--- /dev/null
+++ b/trunk/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/trunk/src/ld-lookup64.c b/trunk/src/ld-lookup64.c
new file mode 100644
index 0000000..7e35334
--- /dev/null
+++ b/trunk/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"
diff --git a/trunk/src/main.c b/trunk/src/main.c
new file mode 100644
index 0000000..631da44
--- /dev/null
+++ b/trunk/src/main.c
@@ -0,0 +1,486 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 2005 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 <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <locale.h>
+#include <error.h>
+#include <argp.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "prelink.h"
+
+#define PRELINK_CONF "/etc/prelink.conf"
+#define PRELINK_CACHE "/etc/prelink.cache"
+
+int all;
+int force;
+int verbose;
+int print_cache;
+int reloc_only;
+GElf_Addr reloc_base;
+int no_update;
+int random_base;
+int conserve_memory;
+int libs_only;
+int dry_run;
+int dereference;
+int one_file_system;
+int enable_cxx_optimizations = 1;
+int exec_shield;
+int undo, verify;
+enum verify_method_t verify_method;
+int quick;
+int compute_checksum;
+long long seed;
+GElf_Addr mmap_reg_start = ~(GElf_Addr) 0;
+GElf_Addr mmap_reg_end = ~(GElf_Addr) 0;
+const char *dynamic_linker;
+const char *ld_library_path;
+const char *prelink_conf = PRELINK_CONF;
+const char *prelink_cache = PRELINK_CACHE;
+const char *undo_output;
+int noreexecinit;
+time_t initctime;
+
+const char *argp_program_version = "prelink 1.0 (20061201) Wind River Linux";
+
+const char *argp_program_bug_address = "<support@windriver.com>";
+
+static char argp_doc[] = "prelink -- program to relocate and prelink ELF shared libraries and programs";
+
+#define OPT_DYNAMIC_LINKER 0x80
+#define OPT_LD_LIBRARY_PATH 0x81
+#define OPT_LIBS_ONLY 0x82
+#define OPT_CXX_DISABLE 0x83
+#define OPT_MMAP_REG_START 0x84
+#define OPT_MMAP_REG_END 0x85
+#define OPT_EXEC_SHIELD 0x86
+#define OPT_NO_EXEC_SHIELD 0x87
+#define OPT_SEED 0x88
+#define OPT_MD5 0x89
+#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" },
+ {"black-list", 'b', "PATH", 0, "Blacklist path" },
+ {"cache-file", 'C', "CACHE", 0, "Use CACHE as cache file" },
+ {"config-file", 'c', "CONF", 0, "Use CONF as configuration file" },
+ {"force", 'f', 0, 0, "Force prelinking" },
+ {"dereference", 'h', 0, 0, "Follow symlinks when processing directory trees from command line" },
+ {"one-file-system", 'l', 0, 0, "Stay in local file system when processing directories from command line" },
+ {"conserve-memory", 'm', 0, 0, "Allow libraries to overlap as long as they never appear in the same program" },
+ {"no-update-cache", 'N', 0, 0, "Don't update prelink cache" },
+ {"dry-run", 'n', 0, 0, "Don't actually prelink anything" },
+ {"undo-output", 'o', "FILE", 0, "Undo output file" },
+ {"print-cache", 'p', 0, 0, "Print prelink cache" },
+ {"quick", 'q', 0, 0, "Quick scan" },
+ {"random", 'R', 0, 0, "Choose random base for libraries" },
+ {"reloc-only", 'r', "BASE_ADDRESS", 0, "Relocate library to given address, don't prelink" },
+ {"root", OPT_SYSROOT, "ROOT_PATH", 0, "Prefix all paths with ROOT_PATH" },
+ {"undo", 'u', 0, 0, "Undo prelink" },
+ {"verbose", 'v', 0, 0, "Produce verbose output" },
+ {"verify", 'y', 0, 0, "Verify file consistency by undoing and redoing prelink and printing original to standard output" },
+ {"md5", OPT_MD5, 0, 0, "For verify print MD5 sum of original to standard output instead of content" },
+ {"sha", OPT_SHA, 0, 0, "For verify print SHA sum of original to standard output instead of content" },
+ {"dynamic-linker", OPT_DYNAMIC_LINKER, "DYNAMIC_LINKER",
+ 0, "Special dynamic linker path" },
+ {"exec-shield", OPT_EXEC_SHIELD, 0, 0, "Lay out libraries for exec-shield on IA-32" },
+ {"no-exec-shield", OPT_NO_EXEC_SHIELD, 0, 0, "Don't lay out libraries for exec-shield on IA-32" },
+ {"ld-library-path", OPT_LD_LIBRARY_PATH, "PATHLIST",
+ 0, "What LD_LIBRARY_PATH should be used" },
+ {"libs-only", OPT_LIBS_ONLY, 0, 0, "Prelink only libraries, no binaries" },
+ {"disable-c++-optimizations", OPT_CXX_DISABLE, 0, OPTION_HIDDEN, "" },
+ {"mmap-region-start", OPT_MMAP_REG_START, "BASE_ADDRESS", OPTION_HIDDEN, "" },
+ {"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 }
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ char *endarg;
+
+ switch (key)
+ {
+ case 'a':
+ all = 1;
+ break;
+ case 'b':
+ if (add_to_blacklist (arg, dereference, one_file_system))
+ exit (EXIT_FAILURE);
+ break;
+ case 'f':
+ force = 1;
+ break;
+ case 'p':
+ print_cache = 1;
+ break;
+ case 'q':
+ quick = 1;
+ break;
+ case 'v':
+ ++verbose;
+ break;
+ case 'R':
+ random_base |= 1;
+ break;
+ case OPT_SEED:
+ random_base |= 2;
+ seed = strtoull (arg, &endarg, 0);
+ if (endarg != strchr (arg, '\0'))
+ error (EXIT_FAILURE, 0, "--seed option requires numberic argument");
+ break;
+ case 'r':
+ reloc_only = 1;
+ reloc_base = strtoull (arg, &endarg, 0);
+ if (endarg != strchr (arg, '\0'))
+ error (EXIT_FAILURE, 0, "-r option requires numberic argument");
+ break;
+ case 'h':
+ dereference = 1;
+ break;
+ case 'l':
+ one_file_system = 1;
+ break;
+ case 'm':
+ conserve_memory = 1;
+ break;
+ case 'N':
+ no_update = 1;
+ break;
+ case 'n':
+ dry_run = 1;
+ break;
+ case 'C':
+ prelink_cache = arg;
+ break;
+ case 'c':
+ prelink_conf = arg;
+ break;
+ case 'u':
+ undo = 1;
+ break;
+ case 'y':
+ verify = 1;
+ break;
+ case 'o':
+ undo_output = arg;
+ break;
+ case OPT_DYNAMIC_LINKER:
+ dynamic_linker = arg;
+ break;
+ case OPT_LD_LIBRARY_PATH:
+ ld_library_path = arg;
+ break;
+ case OPT_LIBS_ONLY:
+ libs_only = 1;
+ break;
+ case OPT_MD5:
+ verify_method = VERIFY_MD5;
+ break;
+ case OPT_SHA:
+ verify_method = VERIFY_SHA;
+ break;
+ case OPT_CXX_DISABLE:
+ enable_cxx_optimizations = 0;
+ break;
+ case OPT_MMAP_REG_START:
+ mmap_reg_start = strtoull (arg, &endarg, 0);
+ if (endarg != strchr (arg, '\0'))
+ error (EXIT_FAILURE, 0, "--mmap-region-start option requires numberic argument");
+ break;
+ case OPT_MMAP_REG_END:
+ mmap_reg_end = strtoull (arg, &endarg, 0);
+ if (endarg != strchr (arg, '\0'))
+ error (EXIT_FAILURE, 0, "--mmap-region-end option requires numberic argument");
+ break;
+ case OPT_EXEC_SHIELD:
+ exec_shield = 1;
+ break;
+ case OPT_NO_EXEC_SHIELD:
+ exec_shield = 0;
+ break;
+ case OPT_COMPUTE_CHECKSUM:
+ compute_checksum = 1;
+ break;
+ case OPT_SYSROOT:
+ sysroot = arg;
+ break;
+ case OPT_RTLD:
+ prelink_rtld = arg;
+ break;
+ case 'i':
+ noreexecinit=1;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+time_t get_ctime(const char *file) {
+ struct stat st;
+ if(stat(file,&st) == 0)
+ return st.st_ctime;
+ return 0;
+}
+
+void checkinit() {
+ if(initctime != get_ctime("/sbin/init")) {
+ printf("Executing /sbin/init U\n");
+ system("/sbin/init U");
+ }
+}
+
+static struct argp argp = { options, parse_opt, "[FILES]", argp_doc };
+
+const char *prelink_rtld = NULL;
+
+int
+main (int argc, char *argv[])
+{
+ int remaining, failures = 0;
+
+ setlocale (LC_ALL, "");
+
+ exec_shield = 2;
+
+ prelink_init_cache ();
+
+ elf_version (EV_CURRENT);
+
+ argp_parse (&argp, argc, argv, 0, &remaining, 0);
+
+ if(!noreexecinit) {
+ initctime = get_ctime("/sbin/init");
+ atexit(checkinit);
+ }
+
+ if (ld_library_path == NULL)
+ ld_library_path = getenv ("LD_LIBRARY_PATH");
+
+ if (all && reloc_only)
+ error (EXIT_FAILURE, 0, "--all and --reloc-only options are incompatible");
+ if ((undo || verify) && reloc_only)
+ error (EXIT_FAILURE, 0, "--undo and --reloc-only options are incompatible");
+ if (verify && (undo || all))
+ error (EXIT_FAILURE, 0, "--verify and either --undo or --all options are incompatible");
+ if (dry_run && verify)
+ error (EXIT_FAILURE, 0, "--dry-run and --verify options are incompatible");
+ if ((undo || verify) && quick)
+ error (EXIT_FAILURE, 0, "--undo and --quick options are incompatible");
+
+ /* Set the default for exec_shield. */
+ if (exec_shield == 2)
+ {
+ if (sysroot == NULL && ! access ("/proc/sys/kernel/exec-shield", F_OK))
+ exec_shield = 1;
+ else
+ exec_shield = 0;
+ }
+
+#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);
+ if (sysroot == NULL)
+ error (EXIT_FAILURE, 0, "Could not canonicalize --root argument");
+ 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 ();
+ prelink_print_cache ();
+ return 0;
+ }
+
+ if (remaining == argc && ! all)
+ error (EXIT_FAILURE, 0, "no files given and --all not used");
+
+ if (undo_output && (!undo || all))
+ error (EXIT_FAILURE, 0, "-o can be only specified together with -u and without -a");
+
+ if (undo_output && remaining + 1 != argc)
+ error (EXIT_FAILURE, 0, "-o can only be used when undoing a single object");
+
+ if (compute_checksum)
+ {
+ while (remaining < argc)
+ {
+ DSO *dso = open_dso (argv[remaining++]);
+
+ if (dso == NULL || reopen_dso (dso, NULL, NULL)
+ || prelink_set_checksum (dso))
+ error (0, 0, "could not recompute checksum of %s", dso->filename);
+ close_dso (dso);
+ error (0, 0, "%08x %s\n", (unsigned int) dso->info_DT_CHECKSUM, dso->filename);
+ }
+ exit (0);
+ }
+
+ if (verify)
+ {
+ if (remaining + 1 != argc)
+ error (EXIT_FAILURE, 0, "only one library or binary can be verified in a single command");
+ return prelink_verify (argv[remaining]);
+ }
+
+ if (reloc_only || (undo && ! all))
+ {
+ while (remaining < argc)
+ {
+ DSO *dso = open_dso (argv[remaining++]);
+ int ret;
+
+ if (dso == NULL)
+ {
+ ++failures;
+ continue;
+ }
+
+ if (dso->ehdr.e_type != ET_DYN
+ && (reloc_only || dso->ehdr.e_type != ET_EXEC))
+ {
+ ++failures;
+ error (0, 0, "%s is not a shared library", dso->filename);
+ continue;
+ }
+
+ if (undo)
+ ret = prelink_undo (dso);
+ else
+ ret = relocate_dso (dso, reloc_base);
+
+ if (ret)
+ {
+ ++failures;
+ close_dso (dso);
+ continue;
+ }
+
+ if (dynamic_info_is_set (dso, DT_CHECKSUM_BIT)
+ && dso_is_rdwr (dso)
+ && prelink_set_checksum (dso))
+ {
+ ++failures;
+ close_dso (dso);
+ continue;
+ }
+
+ if (dry_run)
+ {
+ close_dso (dso);
+ continue;
+ }
+
+ if (reloc_only)
+ dso->permissive = 1;
+ else if (undo_output)
+ {
+ const char *output = strdup (undo_output);
+ const char *orig_filename;
+ if (!output)
+ {
+ ++failures;
+ close_dso (dso);
+ continue;
+ }
+ if (dso->filename != dso->soname)
+ orig_filename = dso->filename;
+ else
+ orig_filename = strdup (dso->filename);
+ if (!orig_filename)
+ {
+ ++failures;
+ close_dso (dso);
+ continue;
+ }
+ dso->filename = output;
+ if (update_dso (dso, orig_filename))
+ ++failures;
+ free ((char *) orig_filename);
+ continue;
+ }
+
+ if (update_dso (dso, NULL))
+ ++failures;
+ }
+
+ return failures;
+ }
+
+ if (blacklist_from_config (prelink_conf))
+ return EXIT_FAILURE;
+
+ if (quick)
+ prelink_load_cache ();
+
+ if (gather_config (prelink_conf))
+ return EXIT_FAILURE;
+
+ while (remaining < argc)
+ if (gather_object (argv[remaining++], dereference, one_file_system))
+ return EXIT_FAILURE;
+
+ if (gather_check_libs ())
+ return EXIT_FAILURE;
+
+ if (undo)
+ return undo_all ();
+
+ if (! all && ! quick)
+ prelink_load_cache ();
+
+ layout_libs ();
+ prelink_all ();
+
+ if (! no_update && ! dry_run)
+ prelink_save_cache (all);
+ return 0;
+}
diff --git a/trunk/src/makecrc.c b/trunk/src/makecrc.c
new file mode 100644
index 0000000..db52013
--- /dev/null
+++ b/trunk/src/makecrc.c
@@ -0,0 +1,63 @@
+/* Not copyrighted 1990 Mark Adler */
+
+#ifndef lint
+static char rcsid[] = "$Id: makecrc.c,v 0.6 1993/05/28 07:42:59 jloup Exp $";
+#endif
+
+#include <stdio.h>
+
+main()
+/*
+ Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
+ x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
+
+ Polynomials over GF(2) are represented in binary, one bit per coefficient,
+ with the lowest powers in the most significant bit. Then adding polynomials
+ is just exclusive-or, and multiplying a polynomial by x is a right shift by
+ one. If we call the above polynomial p, and represent a byte as the
+ polynomial q, also with the lowest power in the most significant bit (so the
+ byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
+ where a mod b means the remainder after dividing a by b.
+
+ This calculation is done using the shift-register method of multiplying and
+ taking the remainder. The register is initialized to zero, and for each
+ incoming bit, x^32 is added mod p to the register if the bit is a one (where
+ x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
+ x (which is shifting right by one and adding x^32 mod p if the bit shifted
+ out is a one). We start with the highest power (least significant bit) of
+ q and repeat for all eight bits of q.
+
+ The table is simply the CRC of all possible eight bit values. This is all
+ the information needed to generate CRC's on data a byte at a time for all
+ combinations of CRC register values and incoming bytes. The table is
+ written to stdout as 256 long hexadecimal values in C language format.
+*/
+{
+ unsigned long c; /* crc shift register */
+ unsigned long e; /* polynomial exclusive-or pattern */
+ int i; /* counter for all possible eight bit values */
+ int k; /* byte being shifted into crc apparatus */
+
+ /* terms of polynomial defining this crc (except x^32): */
+ static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+ /* Make exclusive-or pattern from polynomial (0xedb88320) */
+ e = 0;
+ for (i = 0; i < sizeof(p)/sizeof(int); i++)
+ e |= 1L << (31 - p[i]);
+
+ /* Compute and print table of CRC's, five per line */
+ printf(" 0x00000000");
+ for (i = 1; i < 256; i++)
+ {
+ c = i;
+ /* The idea to initialize the register with the byte instead of
+ * zero was stolen from Haruhiko Okumura's ar002
+ */
+ for (k = 8; k; k--)
+ c = c & 1 ? (c >> 1) ^ e : c >> 1;
+ printf(i % 5 ? ", 0x%08lx" : ",\n 0x%08lx", c);
+ }
+ putchar('\n');
+ return 0;
+}
diff --git a/trunk/src/md5.c b/trunk/src/md5.c
new file mode 100644
index 0000000..0d2fdef
--- /dev/null
+++ b/trunk/src/md5.c
@@ -0,0 +1,362 @@
+/* md5.c - Functions to compute MD5 message digest of files or memory blocks
+ according to the definition of MD5 in RFC 1321 from April 1992.
+ Copyright (C) 1995, 1996, 2001, 2003 Free Software Foundation, Inc.
+ NOTE: The canonical source of this file is maintained with the GNU C
+ Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
+
+ 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. */
+
+/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <endian.h>
+#include <byteswap.h>
+#include "md5.h"
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+# define SWAP(n) bswap_32 (n)
+#else
+# define SWAP(n) (n)
+#endif
+
+/* This array contains the bytes used to pad the buffer to the next
+ 64-byte boundary. (RFC 1321, 3.1: Step 1) */
+static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
+
+
+/* Initialize structure containing state of computation.
+ (RFC 1321, 3.3: Step 3) */
+void
+md5_init_ctx (ctx)
+ struct md5_ctx *ctx;
+{
+ ctx->A = 0x67452301;
+ ctx->B = 0xefcdab89;
+ ctx->C = 0x98badcfe;
+ ctx->D = 0x10325476;
+
+ ctx->total[0] = ctx->total[1] = 0;
+ ctx->buflen = 0;
+}
+
+/* Put result from CTX in first 16 bytes following RESBUF. The result
+ must be in little endian byte order.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+void *
+md5_read_ctx (ctx, resbuf)
+ const struct md5_ctx *ctx;
+ void *resbuf;
+{
+ ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
+ ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
+ ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
+ ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
+
+ return resbuf;
+}
+
+/* Process the remaining bytes in the internal buffer and the usual
+ prolog according to the standard and write the result to RESBUF.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+void *
+md5_finish_ctx (ctx, resbuf)
+ struct md5_ctx *ctx;
+ void *resbuf;
+{
+ /* Take yet unprocessed bytes into account. */
+ md5_uint32 bytes = ctx->buflen;
+ size_t pad;
+
+ /* Now count remaining bytes. */
+ ctx->total[0] += bytes;
+ if (ctx->total[0] < bytes)
+ ++ctx->total[1];
+
+ pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
+ memcpy (&ctx->buffer[bytes], fillbuf, pad);
+
+ /* Put the 64-bit file length in *bits* at the end of the buffer. */
+ *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
+ *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
+ (ctx->total[0] >> 29));
+
+ /* Process last bytes. */
+ md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
+
+ return md5_read_ctx (ctx, resbuf);
+}
+
+/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
+ result is always in little endian byte order, so that a byte-wise
+ output yields to the wanted ASCII representation of the message
+ digest. */
+void *
+md5_buffer (buffer, len, resblock)
+ const char *buffer;
+ size_t len;
+ void *resblock;
+{
+ struct md5_ctx ctx;
+
+ /* Initialize the computation context. */
+ md5_init_ctx (&ctx);
+
+ /* Process whole buffer but last len % 64 bytes. */
+ md5_process_bytes (buffer, len, &ctx);
+
+ /* Put result in desired memory area. */
+ return md5_finish_ctx (&ctx, resblock);
+}
+
+
+void
+md5_process_bytes (buffer, len, ctx)
+ const void *buffer;
+ size_t len;
+ struct md5_ctx *ctx;
+{
+ /* When we already have some bits in our internal buffer concatenate
+ both inputs first. */
+ if (ctx->buflen != 0)
+ {
+ size_t left_over = ctx->buflen;
+ size_t add = 128 - left_over > len ? len : 128 - left_over;
+
+ memcpy (&ctx->buffer[left_over], buffer, add);
+ ctx->buflen += add;
+
+ if (ctx->buflen > 64)
+ {
+ md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
+
+ ctx->buflen &= 63;
+ /* The regions in the following copy operation cannot overlap. */
+ memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
+ ctx->buflen);
+ }
+
+ buffer = (const char *) buffer + add;
+ len -= add;
+ }
+
+ /* Process available complete blocks. */
+ if (len >= 64)
+ {
+#define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
+ if (UNALIGNED_P (buffer))
+ while (len > 64)
+ {
+ md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
+ buffer = (const char *) buffer + 64;
+ len -= 64;
+ }
+ else
+ {
+ md5_process_block (buffer, len & ~63, ctx);
+ buffer = (const char *) buffer + (len & ~63);
+ len &= 63;
+ }
+ }
+
+ /* Move remaining bytes in internal buffer. */
+ if (len > 0)
+ {
+ size_t left_over = ctx->buflen;
+
+ memcpy (&ctx->buffer[left_over], buffer, len);
+ left_over += len;
+ if (left_over >= 64)
+ {
+ md5_process_block (ctx->buffer, 64, ctx);
+ left_over -= 64;
+ memcpy (ctx->buffer, &ctx->buffer[64], left_over);
+ }
+ ctx->buflen = left_over;
+ }
+}
+
+
+/* These are the four functions used in the four steps of the MD5 algorithm
+ and defined in the RFC 1321. The first function is a little bit optimized
+ (as found in Colin Plumbs public domain implementation). */
+/* #define FF(b, c, d) ((b & c) | (~b & d)) */
+#define FF(b, c, d) (d ^ (b & (c ^ d)))
+#define FG(b, c, d) FF (d, b, c)
+#define FH(b, c, d) (b ^ c ^ d)
+#define FI(b, c, d) (c ^ (b | ~d))
+
+/* Process LEN bytes of BUFFER, accumulating context into CTX.
+ It is assumed that LEN % 64 == 0. */
+
+void
+md5_process_block (buffer, len, ctx)
+ const void *buffer;
+ size_t len;
+ struct md5_ctx *ctx;
+{
+ md5_uint32 correct_words[16];
+ const md5_uint32 *words = buffer;
+ size_t nwords = len / sizeof (md5_uint32);
+ const md5_uint32 *endp = words + nwords;
+ md5_uint32 A = ctx->A;
+ md5_uint32 B = ctx->B;
+ md5_uint32 C = ctx->C;
+ md5_uint32 D = ctx->D;
+
+ /* First increment the byte count. RFC 1321 specifies the possible
+ length of the file up to 2^64 bits. Here we only compute the
+ number of bytes. Do a double word increment. */
+ ctx->total[0] += len;
+ if (ctx->total[0] < len)
+ ++ctx->total[1];
+
+ /* Process all bytes in the buffer with 64 bytes in each round of
+ the loop. */
+ while (words < endp)
+ {
+ md5_uint32 *cwp = correct_words;
+ md5_uint32 A_save = A;
+ md5_uint32 B_save = B;
+ md5_uint32 C_save = C;
+ md5_uint32 D_save = D;
+
+ /* First round: using the given function, the context and a constant
+ the next context is computed. Because the algorithms processing
+ unit is a 32-bit word and it is determined to work on words in
+ little endian byte order we perhaps have to change the byte order
+ before the computation. To reduce the work for the next steps
+ we store the swapped words in the array CORRECT_WORDS. */
+
+#define OP(a, b, c, d, s, T) \
+ do \
+ { \
+ a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
+ ++words; \
+ a = rol (a, s); \
+ a += b; \
+ } \
+ while (0)
+
+ /* Before we start, one word to the strange constants.
+ They are defined in RFC 1321 as
+
+ T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or
+ perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
+ */
+
+ /* Round 1. */
+ OP (A, B, C, D, 7, 0xd76aa478);
+ OP (D, A, B, C, 12, 0xe8c7b756);
+ OP (C, D, A, B, 17, 0x242070db);
+ OP (B, C, D, A, 22, 0xc1bdceee);
+ OP (A, B, C, D, 7, 0xf57c0faf);
+ OP (D, A, B, C, 12, 0x4787c62a);
+ OP (C, D, A, B, 17, 0xa8304613);
+ OP (B, C, D, A, 22, 0xfd469501);
+ OP (A, B, C, D, 7, 0x698098d8);
+ OP (D, A, B, C, 12, 0x8b44f7af);
+ OP (C, D, A, B, 17, 0xffff5bb1);
+ OP (B, C, D, A, 22, 0x895cd7be);
+ OP (A, B, C, D, 7, 0x6b901122);
+ OP (D, A, B, C, 12, 0xfd987193);
+ OP (C, D, A, B, 17, 0xa679438e);
+ OP (B, C, D, A, 22, 0x49b40821);
+
+ /* For the second to fourth round we have the possibly swapped words
+ in CORRECT_WORDS. Redefine the macro to take an additional first
+ argument specifying the function to use. */
+#undef OP
+#define OP(f, a, b, c, d, k, s, T) \
+ do \
+ { \
+ a += f (b, c, d) + correct_words[k] + T; \
+ a = rol (a, s); \
+ a += b; \
+ } \
+ while (0)
+
+ /* Round 2. */
+ OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
+ OP (FG, D, A, B, C, 6, 9, 0xc040b340);
+ OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
+ OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
+ OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
+ OP (FG, D, A, B, C, 10, 9, 0x02441453);
+ OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
+ OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
+ OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
+ OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
+ OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
+ OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
+ OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
+ OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
+ OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
+ OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
+
+ /* Round 3. */
+ OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
+ OP (FH, D, A, B, C, 8, 11, 0x8771f681);
+ OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
+ OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
+ OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
+ OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
+ OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
+ OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
+ OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
+ OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
+ OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
+ OP (FH, B, C, D, A, 6, 23, 0x04881d05);
+ OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
+ OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
+ OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
+ OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
+
+ /* Round 4. */
+ OP (FI, A, B, C, D, 0, 6, 0xf4292244);
+ OP (FI, D, A, B, C, 7, 10, 0x432aff97);
+ OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
+ OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
+ OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
+ OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
+ OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
+ OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
+ OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
+ OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
+ OP (FI, C, D, A, B, 6, 15, 0xa3014314);
+ OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
+ OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
+ OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
+ OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
+ OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
+
+ /* Add the starting values of the context. */
+ A += A_save;
+ B += B_save;
+ C += C_save;
+ D += D_save;
+ }
+
+ /* Put checksum in context given as argument. */
+ ctx->A = A;
+ ctx->B = B;
+ ctx->C = C;
+ ctx->D = D;
+}
diff --git a/trunk/src/md5.h b/trunk/src/md5.h
new file mode 100644
index 0000000..55f6195
--- /dev/null
+++ b/trunk/src/md5.h
@@ -0,0 +1,105 @@
+/* md5.h - Declaration of functions and data types used for MD5 sum
+ computing library functions.
+ Copyright (C) 1995, 1996, 1999 Free Software Foundation, Inc.
+ NOTE: The canonical source of this file is maintained with the GNU C
+ Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
+
+ 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. */
+
+#ifndef _MD5_H
+#define _MD5_H 1
+
+#include <limits.h>
+#include <stdint.h>
+typedef uint32_t md5_uint32;
+typedef uintptr_t md5_uintptr;
+
+/* Structure to save state of computation between the single steps. */
+struct md5_ctx
+{
+ md5_uint32 A;
+ md5_uint32 B;
+ md5_uint32 C;
+ md5_uint32 D;
+
+ md5_uint32 total[2];
+ md5_uint32 buflen;
+ char buffer[128];
+};
+
+/*
+ * The following three functions are build up the low level used in
+ * the function `md5_buffer'.
+ */
+
+/* Initialize structure containing state of computation.
+ (RFC 1321, 3.3: Step 3) */
+extern void md5_init_ctx (struct md5_ctx *ctx);
+
+/* Starting with the result of former calls of this function (or the
+ initialization function update the context for the next LEN bytes
+ starting at BUFFER.
+ It is necessary that LEN is a multiple of 64!!! */
+extern void md5_process_block __P ((const void *buffer, size_t len,
+ struct md5_ctx *ctx));
+
+/* Starting with the result of former calls of this function (or the
+ initialization function update the context for the next LEN bytes
+ starting at BUFFER.
+ It is NOT required that LEN is a multiple of 64. */
+extern void md5_process_bytes __P ((const void *buffer, size_t len,
+ struct md5_ctx *ctx));
+
+/* Process the remaining bytes in the buffer and put result from CTX
+ in first 16 bytes following RESBUF. The result is always in little
+ endian byte order, so that a byte-wise output yields to the wanted
+ ASCII representation of the message digest.
+
+ IMPORTANT: On some systems it is required that RESBUF be correctly
+ aligned for a 32 bits value. */
+extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
+
+
+/* Put result from CTX in first 16 bytes following RESBUF. The result is
+ always in little endian byte order, so that a byte-wise output yields
+ to the wanted ASCII representation of the message digest.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
+
+
+/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
+ result is always in little endian byte order, so that a byte-wise
+ output yields to the wanted ASCII representation of the message
+ digest. */
+extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
+
+/* The following is from gnupg-1.0.2's cipher/bithelp.h. */
+/* Rotate a 32 bit integer by n bytes */
+#if defined __GNUC__ && defined __i386__
+static inline md5_uint32
+rol(md5_uint32 x, int n)
+{
+ __asm__("roll %%cl,%0"
+ :"=r" (x)
+ :"0" (x),"c" (n));
+ return x;
+}
+#else
+# define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
+#endif
+
+#endif
diff --git a/trunk/src/mdebug.c b/trunk/src/mdebug.c
new file mode 100644
index 0000000..4c22d2f
--- /dev/null
+++ b/trunk/src/mdebug.c
@@ -0,0 +1,692 @@
+/* Copyright (C) 2001 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 <byteswap.h>
+#include <endian.h>
+#include <error.h>
+#include <stddef.h>
+
+#include "prelink.h"
+
+#define F8(x) unsigned char x[1];
+#define F16(x) unsigned char x[2];
+#define F24(x) unsigned char x[3];
+#define F32(x) unsigned char x[4];
+#define F64(x) unsigned char x[8];
+
+typedef struct
+{
+ F16(magic)
+ F16(vstamp)
+ F32(ilineMax)
+ F32(cbLine)
+ F32(cbLineOffset)
+ F32(idnMax)
+ F32(cbDnOffset)
+ F32(ipdMax)
+ F32(cbPdOffset)
+ F32(isymMax)
+ F32(cbSymOffset)
+ F32(ioptMax)
+ F32(cbOptOffset)
+ F32(iauxMax)
+ F32(cbAuxOffset)
+ F32(issMax)
+ F32(cbSsOffset)
+ F32(issExtMax)
+ F32(cbSsExtOffset)
+ F32(ifdMax)
+ F32(cbFdOffset)
+ F32(crfd)
+ F32(cbRfdOffset)
+ F32(iextMax)
+ F32(cbExtOffset)
+} mdebug_hdr_32;
+
+typedef struct
+{
+ F16(magic)
+ F16(vstamp)
+ F32(ilineMax)
+ F32(idnMax)
+ F32(ipdMax)
+ F32(isymMax)
+ F32(ioptMax)
+ F32(iauxMax)
+ F32(issMax)
+ F32(issExtMax)
+ F32(ifdMax)
+ F32(crfd)
+ F32(iextMax)
+ F64(cbLine)
+ F64(cbLineOffset)
+ F64(cbDnOffset)
+ F64(cbPdOffset)
+ F64(cbSymOffset)
+ F64(cbOptOffset)
+ F64(cbAuxOffset)
+ F64(cbSsOffset)
+ F64(cbSsExtOffset)
+ F64(cbFdOffset)
+ F64(cbRfdOffset)
+ F64(cbExtOffset)
+} mdebug_hdr_64;
+
+typedef struct
+{
+ F32(adr)
+ F32(rss)
+ F32(issBase)
+ F32(cbSs)
+ F32(isymBase)
+ F32(csym)
+ F32(ilineBase)
+ F32(cline)
+ F32(ioptBase)
+ F32(copt)
+ F16(ipdFirst)
+ F16(cpd)
+ F32(iauxBase)
+ F32(caux)
+ F32(rfdBase)
+ F32(crfd)
+ F8(bits1)
+ F24(bits2)
+ F32(cbLineOffset)
+ F32(cbLine)
+} mdebug_fdr_32;
+
+typedef struct
+{
+ F64(adr)
+ F64(cbLineOffset)
+ F64(cbLine)
+ F64(cbSs)
+ F32(rss)
+ F32(issBase)
+ F32(isymBase)
+ F32(csym)
+ F32(ilineBase)
+ F32(cline)
+ F32(ioptBase)
+ F32(copt)
+ F32(ipdFirst)
+ F32(cpd)
+ F32(iauxBase)
+ F32(caux)
+ F32(rfdBase)
+ F32(crfd)
+ F8(bits1)
+ F24(bits2)
+ F32(padding)
+} mdebug_fdr_64;
+
+typedef struct
+{
+ F32(iss)
+ F32(value)
+ F8(bits1)
+ F8(bits2)
+ F8(bits3)
+ F8(bits4)
+} mdebug_sym_32;
+
+typedef struct
+{
+ F64(value)
+ F32(iss)
+ F8(bits1)
+ F8(bits2)
+ F8(bits3)
+ F8(bits4)
+} mdebug_sym_64;
+
+typedef struct
+{
+ F8(bits1)
+ F8(bits2)
+ F16(fd)
+ mdebug_sym_32 asym;
+} mdebug_ext_32;
+
+typedef struct
+{
+ mdebug_sym_64 asym;
+ F8(bits1)
+ F24(bits2)
+ F32(fd)
+} mdebug_ext_64;
+
+typedef struct
+{
+ F32(adr)
+ F32(isym)
+ F32(iline)
+ F32(regmask)
+ F32(regoffset)
+ F32(iopt)
+ F32(fregmask)
+ F32(fregoffset)
+ F32(frameoffset)
+ F16(framereg)
+ F16(pcreg)
+ F32(lnLow)
+ F32(lnHigh)
+ F32(cbLineOffset)
+} mdebug_pdr_32;
+
+typedef struct
+{
+ F64(adr)
+ F64(cbLineOffset)
+ F32(isym)
+ F32(iline)
+ F32(regmask)
+ F32(regoffset)
+ F32(iopt)
+ F32(fregmask)
+ F32(fregoffset)
+ F32(frameoffset)
+ F32(lnLow)
+ F32(lnHigh)
+ F8(gp_prologue)
+ F8(bits1)
+ F8(bits2)
+ F8(localoff)
+ F16(framereg)
+ F16(pcreg)
+} mdebug_pdr_64;
+
+typedef struct
+{
+ F32(bits);
+} mdebug_rndx;
+
+typedef struct
+{
+ F8(bits1)
+ F8(bits2)
+ F8(bits3)
+ F8(bits4)
+ mdebug_rndx rndx;
+ F32(offset)
+} mdebug_opt;
+
+typedef struct
+{
+ F32(rfd)
+ F32(index)
+} mdebug_dnr;
+
+typedef struct
+{
+ F32(rfd)
+} mdebug_rfd;
+
+#define scNil 0
+#define scText 1
+#define scData 2
+#define scBss 3
+#define scRegister 4
+#define scAbs 5
+#define scUndefined 6
+#define scCdbLocal 7
+#define scBits 8
+#define scCdbSystem 9
+#define scDbx 9
+#define scRegImage 10
+#define scInfo 11
+#define scUserStruct 12
+#define scSData 13
+#define scSBss 14
+#define scRData 15
+#define scVar 16
+#define scCommon 17
+#define scSCommon 18
+#define scVarRegister 19
+#define scVariant 20
+#define scSUndefined 21
+#define scInit 22
+#define scBasedVar 23
+#define scXData 24
+#define scPData 25
+#define scFini 26
+#define scRConst 27
+#define scMax 32
+
+#define stNil 0
+#define stGlobal 1
+#define stStatic 2
+#define stParam 3
+#define stLocal 4
+#define stLabel 5
+#define stProc 6
+#define stBlock 7
+#define stEnd 8
+#define stMember 9
+#define stTypedef 10
+#define stFile 11
+#define stRegReloc 12
+#define stForward 13
+#define stStaticProc 14
+#define stConstant 15
+#define stStaParam 16
+#define stStruct 26
+#define stUnion 27
+#define stEnum 28
+#define stIndirect 34
+#define stMax 64
+
+struct mdebug
+{
+ uint32_t (*read_32) (char *);
+ GElf_Addr (*read_ptr) (char *);
+ void (*write_ptr) (char *, GElf_Addr);
+ void (*adjust_sym) (struct mdebug *, unsigned char *, GElf_Addr, GElf_Addr);
+ unsigned char *buf;
+ DSO *dso;
+};
+
+static uint32_t
+read_native_32 (char *p)
+{
+ return *(uint32_t *)p;
+}
+
+static uint32_t
+read_swap_32 (char *p)
+{
+ return bswap_32 (*(uint32_t *)p);
+}
+
+static GElf_Addr
+read_native_ptr32 (char *p)
+{
+ return *(uint32_t *)p;
+}
+
+static GElf_Addr
+read_swap_ptr32 (char *p)
+{
+ return bswap_32 (*(uint32_t *)p);
+}
+
+static void
+write_native_ptr32 (char *p, GElf_Addr v)
+{
+ *(uint32_t *)p = v;
+}
+
+static void
+write_swap_ptr32 (char *p, GElf_Addr v)
+{
+ *(uint32_t *)p = bswap_32 (v);
+}
+
+static GElf_Addr
+read_native_ptr64 (char *p)
+{
+ return *(uint64_t *)p;
+}
+
+static GElf_Addr
+read_swap_ptr64 (char *p)
+{
+ return bswap_64 (*(uint64_t *)p);
+}
+
+static void
+write_native_ptr64 (char *p, GElf_Addr v)
+{
+ *(uint64_t *)p = v;
+}
+
+static void
+write_swap_ptr64 (char *p, GElf_Addr v)
+{
+ *(uint64_t *)p = bswap_64 (v);
+}
+
+static inline int
+mdebug_sym_relocate (unsigned int st, unsigned int sc)
+{
+ switch (sc)
+ {
+ case scData:
+ case scBss:
+ case scAbs:
+ case scSData:
+ case scSBss:
+ case scRData:
+ case scXData:
+ case scPData:
+ return 1;
+ case scText:
+ case scInit:
+ case scFini:
+ case scRConst:
+ if (st != stBlock && st != stEnd && st != stFile)
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+static void
+adjust_mdebug_sym_le32 (struct mdebug *mdebug, mdebug_sym_32 *symptr,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned int st, sc;
+ GElf_Addr addr;
+
+ st = symptr->bits1[0] & 0x3f;
+ sc = (symptr->bits1[0] >> 6) | ((symptr->bits2[0] & 7) << 2);
+ if (mdebug_sym_relocate (st, sc))
+ {
+ addr = mdebug->read_ptr (symptr->value);
+ if (addr >= start && (addr || sc != scAbs))
+ mdebug->write_ptr (symptr->value, addr + adjust);
+ }
+}
+
+static void
+adjust_mdebug_sym_be32 (struct mdebug *mdebug, mdebug_sym_32 *symptr,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned int st, sc;
+ GElf_Addr addr;
+
+ st = symptr->bits1[0] >> 2;
+ sc = ((symptr->bits1[0] & 3) << 3) | (symptr->bits2[0] >> 5);
+ if (mdebug_sym_relocate (st, sc))
+ {
+ addr = mdebug->read_ptr (symptr->value);
+ if (addr >= start && (addr || sc != scAbs))
+ mdebug->write_ptr (symptr->value, addr + adjust);
+ }
+}
+
+static void
+adjust_mdebug_sym_le64 (struct mdebug *mdebug, mdebug_sym_64 *symptr,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned int st, sc;
+ GElf_Addr addr;
+
+ st = symptr->bits1[0] & 0x3f;
+ sc = (symptr->bits1[0] >> 6) | ((symptr->bits2[0] & 7) << 2);
+ if (mdebug_sym_relocate (st, sc))
+ {
+ addr = mdebug->read_ptr (symptr->value);
+ if (addr >= start && (addr || sc != scAbs))
+ mdebug->write_ptr (symptr->value, addr + adjust);
+ }
+}
+
+static void
+adjust_mdebug_sym_be64 (struct mdebug *mdebug, mdebug_sym_64 *symptr,
+ GElf_Addr start, GElf_Addr adjust)
+{
+ unsigned int st, sc;
+ GElf_Addr addr;
+
+ st = symptr->bits1[0] >> 2;
+ sc = ((symptr->bits1[0] & 3) << 3) | (symptr->bits2[0] >> 5);
+ if (mdebug_sym_relocate (st, sc))
+ {
+ addr = mdebug->read_ptr (symptr->value);
+ if (addr >= start && (addr || sc != scAbs))
+ mdebug->write_ptr (symptr->value, addr + adjust);
+ }
+}
+
+#define SIZEOf(x) \
+ (dso->arch->class == ELFCLASS32 ? sizeof (x##_32) : sizeof (x##_64))
+#define SIZEOF(x) SIZEOf(x)
+#define OFFSETOf(x,y) \
+ (dso->arch->class == ELFCLASS32 ? offsetof (x##_32, y) : offsetof (x##_64, y))
+#define OFFSETOF(x,y) OFFSETOf(x,y)
+
+static int
+start_mdebug (DSO *dso, int n, struct mdebug *mdebug)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+
+ data = elf_getdata (scn, NULL);
+ mdebug->buf = data->d_buf;
+ mdebug->dso = dso;
+ assert (data != NULL && data->d_buf != NULL);
+ assert (elf_getdata (scn, data) == NULL);
+ assert (data->d_off == 0 && data->d_size == dso->shdr[n].sh_size);
+ if (dso->mdebug_orig_offset == 0)
+ dso->mdebug_orig_offset = dso->shdr[n].sh_offset;
+#if __BYTE_ORDER == __BIG_ENDIAN
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+#else
+# error Not supported host endianess
+#endif
+ {
+ mdebug->read_32 = read_native_32;
+ if (dso->arch->class == ELFCLASS32)
+ {
+ mdebug->read_ptr = read_native_ptr32;
+ mdebug->write_ptr = write_native_ptr32;
+ }
+ else
+ {
+ mdebug->read_ptr = read_native_ptr64;
+ mdebug->write_ptr = write_native_ptr64;
+ }
+ }
+#if __BYTE_ORDER == __BIG_ENDIAN
+ else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
+#endif
+ {
+ mdebug->read_32 = read_swap_32;
+ if (dso->arch->class == ELFCLASS32)
+ {
+ mdebug->read_ptr = read_swap_ptr32;
+ mdebug->write_ptr = write_swap_ptr32;
+ }
+ else
+ {
+ mdebug->read_ptr = read_swap_ptr64;
+ mdebug->write_ptr = write_swap_ptr64;
+ }
+ }
+ else
+ {
+ error (0, 0, "%s: Wrong ELF data enconding", dso->filename);
+ return 1;
+ }
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+ {
+ if (dso->arch->class == ELFCLASS32)
+ mdebug->adjust_sym = (void *) adjust_mdebug_sym_le32;
+ else
+ mdebug->adjust_sym = (void *) adjust_mdebug_sym_le64;
+ }
+ else
+ {
+ if (dso->arch->class == ELFCLASS32)
+ mdebug->adjust_sym = (void *) adjust_mdebug_sym_be32;
+ else
+ mdebug->adjust_sym = (void *) adjust_mdebug_sym_be64;
+ }
+
+ if (dso->shdr[n].sh_size < SIZEOF (mdebug_hdr))
+ {
+ error (0, 0, "%s: .mdebug section too small", dso->filename);
+ return 1;
+ }
+ return 0;
+}
+
+int
+adjust_mdebug (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust)
+{
+ struct mdebug mdebug;
+ struct { GElf_Off offset; GElf_Off size; size_t entsize; } regions [11];
+ int i = 0;
+ unsigned char *symptr, *endptr;
+
+ if (start_mdebug (dso, n, &mdebug))
+ return 1;
+
+#define READ(x, y, longsize, sz) \
+do { \
+ unsigned char *tmp; \
+ tmp = mdebug.buf + OFFSETOF (mdebug_hdr, x); \
+ regions[i].offset = mdebug.read_ptr (tmp); \
+ tmp = mdebug.buf + OFFSETOF (mdebug_hdr, y); \
+ if (longsize) \
+ regions[i].size = mdebug.read_ptr (tmp); \
+ else \
+ regions[i].size = mdebug.read_32 (tmp); \
+ regions[i].entsize = sz; \
+ ++i; \
+} while (0)
+
+ READ (cbLineOffset, cbLine, 1, sizeof (char));
+ READ (cbDnOffset, idnMax, 0, sizeof (mdebug_dnr));
+ READ (cbPdOffset, ipdMax, 0, SIZEOF (mdebug_pdr));
+ READ (cbSymOffset, isymMax, 0, SIZEOF (mdebug_sym));
+ READ (cbOptOffset, ioptMax, 0, sizeof (mdebug_opt));
+ READ (cbAuxOffset, iauxMax, 0, 4 * sizeof (char));
+ READ (cbSsOffset, issMax, 0, sizeof (char));
+ READ (cbSsExtOffset, issExtMax, 0, sizeof (char));
+ READ (cbFdOffset, ifdMax, 0, SIZEOF (mdebug_fdr));
+ READ (cbRfdOffset, crfd, 0, sizeof (mdebug_rfd));
+ READ (cbExtOffset, iextMax, 0, SIZEOF (mdebug_ext));
+
+#undef READ
+
+ for (i = 0; i < 11; ++i)
+ {
+ if (regions[i].offset)
+ regions[i].offset -= dso->mdebug_orig_offset;
+ regions[i].size *= regions[i].entsize;
+ if (regions[i].offset >= dso->shdr[n].sh_size
+ || regions[i].offset + regions[i].size > dso->shdr[n].sh_size)
+ {
+ error (0, 0, "%s: File offsets in .mdebug header point outside of .mdebug section",
+ dso->filename);
+ return 1;
+ }
+ }
+
+ /* Adjust symbols. */
+ if (regions[3].offset)
+ for (symptr = mdebug.buf + regions[3].offset,
+ endptr = symptr + regions[3].size;
+ symptr < endptr;
+ symptr += regions[3].entsize)
+ mdebug.adjust_sym (&mdebug, symptr, start, adjust);
+
+ /* Adjust file descriptor's addresses. */
+ if (regions[8].offset)
+ for (symptr = mdebug.buf + regions[8].offset,
+ endptr = symptr + regions[8].size;
+ symptr < endptr;
+ symptr += regions[8].entsize)
+ {
+ GElf_Addr addr;
+
+ assert (offsetof (mdebug_fdr_32, adr) == 0);
+ assert (offsetof (mdebug_fdr_64, adr) == 0);
+ addr = mdebug.read_ptr (symptr);
+ if (addr >= start)
+ mdebug.write_ptr (symptr, addr + adjust);
+ }
+
+ /* Adjust extended symbols. */
+ if (regions[10].offset)
+ for (symptr = mdebug.buf + regions[10].offset
+ + OFFSETOF (mdebug_ext, asym),
+ endptr = symptr + regions[10].size;
+ symptr < endptr;
+ symptr += regions[10].entsize)
+ mdebug.adjust_sym (&mdebug, symptr, start, adjust);
+
+ return 0;
+}
+
+int
+finalize_mdebug (DSO *dso)
+{
+ int i;
+ struct mdebug mdebug;
+ GElf_Addr adj;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ 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))
+ break;
+
+ assert (i < dso->ehdr.e_shnum);
+
+ /* If .mdebug's file position did not change, there is nothing to do. */
+ adj = dso->shdr[i].sh_offset - dso->mdebug_orig_offset;
+ if (! adj)
+ return 0;
+
+ if (start_mdebug (dso, i, &mdebug))
+ return 1;
+
+#define ADJUST(x) \
+do { \
+ unsigned char *tmp; \
+ GElf_Addr val; \
+ tmp = mdebug.buf + OFFSETOF (mdebug_hdr, x); \
+ val = mdebug.read_ptr (tmp); \
+ if (! val) \
+ break; \
+ val += adj; \
+ if (val < dso->shdr[i].sh_offset \
+ || val >= dso->shdr[i].sh_offset + dso->shdr[i].sh_size) \
+ { \
+ error (0, 0, "%s: File offsets in .mdebug header point outside of .mdebug section", \
+ dso->filename); \
+ return 1; \
+ } \
+ mdebug.write_ptr (tmp, val); \
+} while (0)
+
+ ADJUST (cbLineOffset);
+ ADJUST (cbDnOffset);
+ ADJUST (cbPdOffset);
+ ADJUST (cbSymOffset);
+ ADJUST (cbOptOffset);
+ ADJUST (cbAuxOffset);
+ ADJUST (cbSsOffset);
+ ADJUST (cbSsExtOffset);
+ ADJUST (cbFdOffset);
+ ADJUST (cbRfdOffset);
+ ADJUST (cbExtOffset);
+
+#undef ADJUST
+ return 0;
+}
diff --git a/trunk/src/prelink.c b/trunk/src/prelink.c
new file mode 100644
index 0000000..1a2260e
--- /dev/null
+++ b/trunk/src/prelink.c
@@ -0,0 +1,972 @@
+/* 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 <endian.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+
+static GElf_Addr
+resolve_ldso (struct prelink_info *info, GElf_Word r_sym,
+ int reloc_type __attribute__((unused)))
+{
+ /* Dynamic linker does not depend on any other library,
+ all symbols resolve to themselves with the exception
+ of SHN_UNDEF symbols which resolve to 0. */
+ if (info->symtab[r_sym].st_shndx == SHN_UNDEF)
+ {
+ info->resolveent = NULL;
+ info->resolvetls = NULL;
+ return 0;
+ }
+ else
+ {
+ /* As the dynamic linker is relocated first,
+ l_addr will be 0. */
+ info->resolveent = info->ent;
+ info->resolvetls = NULL;
+ return 0 + info->symtab[r_sym].st_value;
+ }
+}
+
+static GElf_Addr
+resolve_dso (struct prelink_info *info, GElf_Word r_sym,
+ int reloc_type)
+{
+ struct prelink_symbol *s;
+ int reloc_class = info->dso->arch->reloc_class (reloc_type);
+
+ for (s = & info->symbols[r_sym]; s; s = s->next)
+ if (s->reloc_class == reloc_class)
+ break;
+
+ info->resolveent = NULL;
+ info->resolvetls = NULL;
+
+ if (s == NULL || s->u.ent == NULL)
+ return 0;
+
+ if (reloc_class == RTYPE_CLASS_TLS)
+ {
+ info->resolvetls = s->u.tls;
+ return s->value;
+ }
+
+ info->resolveent = s->u.ent;
+ return s->u.ent->base + s->value;
+}
+
+static int
+prelink_rel (DSO *dso, int n, struct prelink_info *info)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rel rel;
+ int sec;
+
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrel (dso->elf, data, ndx, &rel);
+ sec = addr_to_sec (dso, rel.r_offset);
+ if (sec == -1)
+ continue;
+
+ switch (dso->arch->prelink_rel (info, &rel, addr))
+ {
+ case 2:
+ gelfx_update_rel (dso->elf, data, ndx, &rel);
+ break;
+ case 0:
+ break;
+ default:
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+prelink_rela (DSO *dso, int n, struct prelink_info *info)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rela rela;
+ int sec;
+
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrela (dso->elf, data, ndx, &rela);
+ sec = addr_to_sec (dso, rela.r_offset);
+ if (sec == -1)
+ continue;
+
+ switch (dso->arch->prelink_rela (info, &rela, addr))
+ {
+ case 2:
+ gelfx_update_rela (dso->elf, data, ndx, &rela);
+ break;
+ case 0:
+ break;
+ default:
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+int
+prelink_prepare (DSO *dso)
+{
+ struct reloc_info rinfo;
+ int liblist = 0, libstr = 0, newlibstr = 0, undo = 0, newundo = 0;
+ int i;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ const char *name
+ = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name);
+ if (! strcmp (name, ".gnu.liblist"))
+ liblist = i;
+ else if (! strcmp (name, ".gnu.libstr"))
+ libstr = i;
+ else if (! strcmp (name, ".gnu.prelink_undo"))
+ undo = i;
+ }
+
+ if (undo == 0)
+ {
+ Elf32_Shdr *shdr32;
+ Elf64_Shdr *shdr64;
+ Elf_Data src, dst;
+
+ dso->undo.d_size = gelf_fsize (dso->elf, ELF_T_EHDR, 1, EV_CURRENT)
+ + gelf_fsize (dso->elf, ELF_T_PHDR,
+ dso->ehdr.e_phnum, EV_CURRENT)
+ + gelf_fsize (dso->elf, ELF_T_SHDR,
+ dso->ehdr.e_shnum - 1, EV_CURRENT);
+ dso->undo.d_buf = malloc (dso->undo.d_size);
+ if (dso->undo.d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ dso->undo.d_type = ELF_T_BYTE;
+ dso->undo.d_off = 0;
+ dso->undo.d_align = gelf_fsize (dso->elf, ELF_T_ADDR, 1, EV_CURRENT);
+ dso->undo.d_version = EV_CURRENT;
+ src = dso->undo;
+ src.d_type = ELF_T_EHDR;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_EHDR, 1, EV_CURRENT);
+ dst = src;
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ src.d_buf = elf32_getehdr (dso->elf);
+ if (elf32_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case ELFCLASS64:
+ src.d_buf = elf64_getehdr (dso->elf);
+ if (elf64_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ default:
+ return 1;
+ }
+ src.d_buf = dst.d_buf + src.d_size;
+ src.d_type = ELF_T_PHDR;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_PHDR, dso->ehdr.e_phnum,
+ EV_CURRENT);
+ dst = src;
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ src.d_buf = elf32_getphdr (dso->elf);
+ if (elf32_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case ELFCLASS64:
+ src.d_buf = elf64_getphdr (dso->elf);
+ if (elf64_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ }
+ src.d_buf = dst.d_buf + src.d_size;
+ src.d_type = ELF_T_SHDR;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_SHDR,
+ dso->ehdr.e_shnum - 1, EV_CURRENT);
+ dst = src;
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ shdr32 = (Elf32_Shdr *) src.d_buf;
+ /* Note: cannot use dso->scn[i] below, since we want to save the
+ original section order before non-alloced sections were
+ sorted by sh_offset. */
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ shdr32[i - 1] = *elf32_getshdr (elf_getscn (dso->elf, i));
+ if (elf32_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ case ELFCLASS64:
+ shdr64 = (Elf64_Shdr *) src.d_buf;
+ /* Note: cannot use dso->scn[i] below, since we want to save the
+ original section order before non-alloced sections were
+ sorted by sh_offset. */
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ shdr64[i - 1] = *elf64_getshdr (elf_getscn (dso->elf, i));
+ if (elf64_xlatetof (&dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Failed to create .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ break;
+ }
+ }
+
+ if (dso->ehdr.e_type != ET_DYN)
+ return 0;
+
+ if (find_reloc_sections (dso, &rinfo))
+ return 1;
+
+ if (is_ldso_soname (dso->soname))
+ {
+ liblist = -1;
+ libstr = -1;
+ }
+
+ if (liblist && libstr && undo
+ && ! rinfo.rel_to_rela && ! rinfo.rel_to_rela_plt)
+ return 0;
+
+ if (! liblist || ! libstr || ! undo)
+ {
+ struct section_move *move;
+
+ move = init_section_move (dso);
+ if (move == NULL)
+ return 1;
+
+ if (! liblist)
+ {
+ liblist = move->old_to_new [dso->ehdr.e_shstrndx];
+ add_section (move, liblist);
+ }
+ else
+ liblist = 0;
+
+ if (! libstr)
+ {
+ add_section (move, liblist + 1);
+ libstr = liblist + 1;
+ newlibstr = 1;
+ }
+ else if (libstr != -1)
+ libstr = move->old_to_new[libstr];
+
+ if (! undo)
+ {
+ if (libstr == -1)
+ {
+ undo = move->old_to_new [dso->ehdr.e_shstrndx];
+ add_section (move, undo);
+ }
+ else
+ {
+ add_section (move, libstr + 1);
+ undo = libstr + 1;
+ }
+ newundo = 1;
+ }
+ else
+ undo = move->old_to_new[undo];
+
+ if (reopen_dso (dso, move, NULL))
+ {
+ free (move);
+ return 1;
+ }
+
+ free (move);
+ if (liblist)
+ {
+ memset (&dso->shdr[liblist], 0, sizeof (GElf_Shdr));
+ dso->shdr[liblist].sh_name = shstrtabadd (dso, ".gnu.liblist");
+ if (dso->shdr[liblist].sh_name == 0)
+ return 1;
+ dso->shdr[liblist].sh_type = SHT_GNU_LIBLIST;
+ dso->shdr[liblist].sh_offset = dso->shdr[liblist - 1].sh_offset;
+ if (dso->shdr[liblist - 1].sh_type != SHT_NOBITS)
+ dso->shdr[liblist].sh_offset += dso->shdr[liblist - 1].sh_size;
+ dso->shdr[liblist].sh_link = libstr;
+ dso->shdr[liblist].sh_addralign = sizeof (GElf_Word);
+ dso->shdr[liblist].sh_entsize = sizeof (Elf32_Lib);
+ }
+
+ if (newlibstr)
+ {
+ memset (&dso->shdr[libstr], 0, sizeof (GElf_Shdr));
+ dso->shdr[libstr].sh_name = shstrtabadd (dso, ".gnu.libstr");
+ if (dso->shdr[libstr].sh_name == 0)
+ return 1;
+ dso->shdr[libstr].sh_type = SHT_STRTAB;
+ dso->shdr[libstr].sh_offset = dso->shdr[libstr - 1].sh_offset;
+ if (dso->shdr[libstr - 1].sh_type != SHT_NOBITS)
+ dso->shdr[libstr].sh_offset += dso->shdr[libstr - 1].sh_size;
+ dso->shdr[libstr].sh_addralign = 1;
+ }
+
+ if (newundo)
+ {
+ Elf_Scn *scn;
+ Elf_Data *data;
+ GElf_Addr newoffset;
+
+ memset (&dso->shdr[undo], 0, sizeof (GElf_Shdr));
+ dso->shdr[undo].sh_name = shstrtabadd (dso, ".gnu.prelink_undo");
+ if (dso->shdr[undo].sh_name == 0)
+ return 1;
+ dso->shdr[undo].sh_type = SHT_PROGBITS;
+ dso->shdr[undo].sh_offset = dso->shdr[undo - 1].sh_offset;
+ if (dso->shdr[undo - 1].sh_type != SHT_NOBITS)
+ dso->shdr[undo].sh_offset += dso->shdr[undo - 1].sh_size;
+ dso->shdr[undo].sh_addralign = dso->undo.d_align;
+ dso->shdr[undo].sh_entsize = 1;
+ dso->shdr[undo].sh_size = dso->undo.d_size;
+ newoffset = dso->shdr[undo].sh_offset + dso->undo.d_align - 1;
+ newoffset &= ~(dso->shdr[undo].sh_addralign - 1);
+ if (adjust_dso_nonalloc (dso, undo + 1, dso->shdr[undo].sh_offset,
+ dso->undo.d_size + newoffset
+ - dso->shdr[undo].sh_offset))
+ return 1;
+ dso->shdr[undo].sh_offset = newoffset;
+ scn = dso->scn[undo];
+ data = elf_getdata (scn, NULL);
+ assert (data != NULL && elf_getdata (scn, data) == NULL);
+ free (data->d_buf);
+ *data = dso->undo;
+ dso->undo.d_buf = NULL;
+ }
+ }
+ else if (reopen_dso (dso, NULL, NULL))
+ return 1;
+
+ if (rinfo.rel_to_rela || rinfo.rel_to_rela_plt)
+ {
+ /* On REL architectures, we might need to convert some REL
+ relocations to RELA relocs. */
+
+ int safe = 1, align = 0, last;
+ GElf_Addr start, adjust, adjust1, adjust2;
+
+ for (i = 1; i < (rinfo.plt ? rinfo.plt : rinfo.first); i++)
+ switch (dso->shdr[i].sh_type)
+ {
+ case SHT_HASH:
+ case SHT_GNU_HASH:
+ case SHT_DYNSYM:
+ case SHT_REL:
+ case SHT_RELA:
+ case SHT_STRTAB:
+ case SHT_NOTE:
+ case SHT_GNU_verdef:
+ case SHT_GNU_verneed:
+ case SHT_GNU_versym:
+ /* These sections are safe, no relocations should point
+ to it, therefore enlarging a section after sections
+ from this set only (and SHT_REL) in ET_DYN just needs
+ adjusting the rest of the library. */
+ break;
+ case SHT_DYNAMIC:
+ case SHT_MIPS_REGINFO:
+ /* The same applies to these sections on MIPS. The convention
+ is to put .dynamic and .reginfo near the beginning of the
+ read-only segment, before the program text. No relocations
+ may refer to them. */
+ if (dso->ehdr.e_machine == EM_MIPS)
+ break;
+ /* FALLTHROUGH */
+ default:
+ /* The rest of sections are not safe. */
+ safe = 0;
+ break;
+ }
+
+ if (! safe)
+ {
+ error (0, 0, "%s: Cannot safely convert %s' section from REL to RELA",
+ dso->filename, strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[rinfo.rel_to_rela
+ ? rinfo.first : rinfo.plt].sh_name));
+ return 1;
+ }
+
+ for (i = rinfo.plt ? rinfo.plt : rinfo.first; i < dso->ehdr.e_shnum; i++)
+ {
+ if (dso->shdr[i].sh_addralign > align)
+ align = dso->shdr[i].sh_addralign;
+ }
+
+ if (rinfo.plt)
+ start = dso->shdr[rinfo.plt].sh_addr + dso->shdr[rinfo.plt].sh_size;
+ else
+ start = dso->shdr[rinfo.last].sh_addr + dso->shdr[rinfo.last].sh_size;
+
+ adjust1 = 0;
+ adjust2 = 0;
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ if (rinfo.rel_to_rela)
+ {
+ for (i = rinfo.first; i <= rinfo.last; ++i)
+ {
+ GElf_Addr size = dso->shdr[i].sh_size / 2 * 3;
+ adjust1 += size - dso->shdr[i].sh_size;
+ if (convert_rel_to_rela (dso, i))
+ return 1;
+ }
+ }
+ if (rinfo.rel_to_rela_plt)
+ {
+ GElf_Addr size = dso->shdr[rinfo.plt].sh_size / 2 * 3;
+ adjust2 = size - dso->shdr[rinfo.plt].sh_size;
+ if (convert_rel_to_rela (dso, rinfo.plt))
+ return 1;
+ }
+
+ adjust = adjust1 + adjust2;
+
+ /* Need to make sure that all the remaining sections are properly
+ aligned. */
+ if (align)
+ adjust = (adjust + align - 1) & ~(align - 1);
+
+ /* Need to make sure adjust doesn't cause different Phdr segments
+ to overlap on the same page. */
+ last = -1;
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz >= start)
+ {
+ if (last != -1
+ && (((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz - 1)
+ ^ dso->phdr[i].p_vaddr)
+ & ~(dso->arch->max_page_size - 1))
+ && !(((dso->phdr[last].p_vaddr + dso->phdr[last].p_memsz
+ + adjust - 1)
+ ^ (dso->phdr[i].p_vaddr + adjust))
+ & ~(dso->arch->max_page_size - 1)))
+ {
+ if (align >= dso->arch->max_page_size)
+ {
+ error (0, 0, "%s: Cannot grow reloc sections", dso->filename);
+ return 1;
+ }
+ adjust = (adjust + dso->arch->max_page_size - 1)
+ & ~(dso->arch->max_page_size - 1);
+ }
+ last = i;
+ }
+
+ /* Adjust all addresses pointing into remaining sections. */
+ if (adjust_dso (dso, start - 1, adjust))
+ return 1;
+
+ if (rinfo.rel_to_rela)
+ {
+ GElf_Addr adjust3 = 0;
+ for (i = rinfo.first; i <= rinfo.last; ++i)
+ {
+ GElf_Addr size = dso->shdr[i].sh_size / 2 * 3;
+
+ dso->shdr[i].sh_addr += adjust3;
+ dso->shdr[i].sh_offset += adjust3;
+ adjust3 += size - dso->shdr[i].sh_size;
+ dso->shdr[i].sh_size = size;
+ }
+ assert (adjust1 == adjust3);
+ if (rinfo.plt)
+ {
+ dso->shdr[rinfo.plt].sh_addr += adjust1;
+ dso->shdr[rinfo.plt].sh_offset += adjust1;
+ }
+ }
+ if (rinfo.rel_to_rela_plt)
+ dso->shdr[rinfo.plt].sh_size += adjust2;
+
+ if (update_dynamic_rel (dso, &rinfo))
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+prelink_dso (struct prelink_info *info)
+{
+ int liblist = 0, libstr = 0, nobits_plt = 0;
+ int i, ndeps = info->ent->ndepends + 1;
+ DSO *dso = info->dso;
+ Elf32_Lib *list = NULL;
+ Elf_Scn *scn;
+ Elf_Data *data;
+ GElf_Addr oldsize, oldoffset;
+ size_t strsize;
+
+ if (dso->ehdr.e_type != ET_DYN)
+ return 0;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ const char *name
+ = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name);
+ if (! strcmp (name, ".gnu.liblist"))
+ liblist = i;
+ else if (! strcmp (name, ".gnu.libstr"))
+ libstr = i;
+ else if (! strcmp (name, ".plt") && dso->shdr[i].sh_type == SHT_NOBITS)
+ nobits_plt = i;
+#if 0
+ else if (dso->arch->create_opd && ! strcmp (name, ".opd"))
+ opd = i;
+#endif
+ }
+
+ if (nobits_plt)
+ {
+ int j, first;
+ GElf_Addr adj, last_offset;
+
+ for (i = 0; i < dso->ehdr.e_phnum; ++i)
+ if (dso->phdr[i].p_type == PT_LOAD
+ && dso->phdr[i].p_vaddr <= dso->shdr[nobits_plt].sh_addr
+ && dso->phdr[i].p_vaddr + dso->phdr[i].p_memsz
+ >= dso->shdr[nobits_plt].sh_addr
+ + dso->shdr[nobits_plt].sh_size)
+ break;
+
+ if (i == dso->ehdr.e_phnum)
+ {
+ error (0, 0, "%s: .plt section not contained within a segment",
+ dso->filename);
+ return 1;
+ }
+
+ for (j = i + 1; j < dso->ehdr.e_phnum; ++j)
+ if (dso->phdr[j].p_type == PT_LOAD)
+ {
+ error (0, 0, "%s: library's NOBITS .plt section not in loadable last segment",
+ dso->filename);
+ return 1;
+ }
+
+ for (j = nobits_plt - 1; j > 0; --j)
+ if (dso->shdr[j].sh_addr < dso->phdr[i].p_vaddr
+ || dso->shdr[j].sh_type != SHT_NOBITS)
+ break;
+ first = j + 1;
+
+ for (j = first; j <= nobits_plt; ++j)
+ {
+ Elf_Data *data = elf_getdata (dso->scn[j], NULL);
+
+ assert (data->d_buf == NULL);
+ assert (data->d_size == dso->shdr[j].sh_size);
+ if (data->d_size)
+ {
+ data->d_buf = calloc (data->d_size, 1);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not convert NOBITS section into PROGBITS",
+ dso->filename);
+ return 1;
+ }
+ }
+ data->d_type = ELF_T_BYTE;
+ dso->shdr[j].sh_type = SHT_PROGBITS;
+ dso->shdr[j].sh_offset = dso->phdr[i].p_offset + dso->shdr[j].sh_addr
+ - dso->phdr[i].p_vaddr;
+ }
+
+ adj = dso->shdr[nobits_plt].sh_offset + dso->shdr[nobits_plt].sh_size
+ - dso->phdr[i].p_offset;
+ assert (adj <= dso->phdr[i].p_memsz);
+ if (adj > dso->phdr[i].p_filesz)
+ {
+ adj -= dso->phdr[i].p_filesz;
+ dso->phdr[i].p_filesz += adj;
+ if (adjust_dso_nonalloc (dso, nobits_plt + 1,
+ dso->shdr[first].sh_offset, adj))
+ return 1;
+ }
+
+ last_offset = dso->shdr[nobits_plt].sh_offset
+ + dso->shdr[nobits_plt].sh_size;
+ for (j = nobits_plt + 1; j < dso->ehdr.e_shnum; ++j)
+ if (!(dso->shdr[j].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR)))
+ break;
+ else
+ {
+ last_offset += dso->shdr[j].sh_addralign - 1;
+ last_offset &= ~(dso->shdr[j].sh_addralign - 1);
+ if (last_offset > dso->phdr[i].p_offset + dso->phdr[i].p_filesz)
+ last_offset = dso->phdr[i].p_offset + dso->phdr[i].p_filesz;
+ dso->shdr[j].sh_offset = last_offset;
+ }
+ }
+
+ if (ndeps <= 1)
+ return 0;
+
+ assert (liblist != 0);
+ assert (libstr != 0);
+
+ list = calloc (ndeps - 1, sizeof (Elf32_Lib));
+ if (list == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot build .gnu.liblist section",
+ dso->filename);
+ goto error_out;
+ }
+
+ strsize = 1;
+ for (i = 0; i < ndeps - 1; ++i)
+ {
+ struct prelink_entry *ent = info->ent->depends[i];
+
+ strsize += strlen (info->sonames[i + 1]) + 1;
+ list[i].l_time_stamp = ent->timestamp;
+ list[i].l_checksum = ent->checksum;
+ }
+
+ scn = dso->scn[libstr];
+ data = elf_getdata (scn, NULL);
+ if (data == NULL)
+ data = elf_newdata (scn);
+ assert (elf_getdata (scn, data) == NULL);
+
+ data->d_type = ELF_T_BYTE;
+ data->d_size = 1;
+ data->d_off = 0;
+ data->d_align = 1;
+ data->d_version = EV_CURRENT;
+ data->d_buf = realloc (data->d_buf, strsize);
+ if (data->d_buf == NULL)
+ {
+ error (0, ENOMEM, "%s: Could not build .gnu.libstr section",
+ dso->filename);
+ goto error_out;
+ }
+
+ oldsize = dso->shdr[libstr].sh_size;
+ dso->shdr[libstr].sh_size = 1;
+ *(char *)data->d_buf = '\0';
+ for (i = 0; i < ndeps - 1; ++i)
+ {
+ const char *name = info->sonames[i + 1];
+
+ list[i].l_name = strtabfind (dso, liblist, name);
+ if (list[i].l_name == 0)
+ {
+ size_t len = strlen (name) + 1;
+
+ memcpy (data->d_buf + data->d_size, name, len);
+ list[i].l_name = data->d_size;
+ data->d_size += len;
+ dso->shdr[libstr].sh_size += len;
+ }
+ }
+ if (oldsize != dso->shdr[libstr].sh_size)
+ {
+ GElf_Addr adjust = dso->shdr[libstr].sh_size - oldsize;
+
+ oldoffset = dso->shdr[libstr].sh_offset;
+ if (adjust_dso_nonalloc (dso, libstr + 1, oldoffset, adjust))
+ goto error_out;
+ }
+
+ scn = dso->scn[liblist];
+ data = elf_getdata (scn, NULL);
+ if (data == NULL)
+ data = elf_newdata (scn);
+ assert (elf_getdata (scn, data) == NULL);
+
+ data->d_type = ELF_T_WORD;
+ data->d_size = (ndeps - 1) * sizeof (Elf32_Lib);
+ data->d_off = 0;
+ data->d_align = sizeof (GElf_Word);
+ data->d_version = EV_CURRENT;
+ free (data->d_buf);
+ data->d_buf = list;
+ list = NULL;
+
+ if (data->d_size != dso->shdr[liblist].sh_size)
+ {
+ GElf_Addr adjust = data->d_size - dso->shdr[liblist].sh_size;
+ GElf_Addr newoffset;
+
+ oldoffset = dso->shdr[liblist].sh_offset;
+ newoffset = oldoffset;
+ if (newoffset & (data->d_align - 1))
+ {
+ newoffset = (newoffset + data->d_align - 1) & ~(data->d_align - 1);
+ adjust += newoffset - dso->shdr[liblist].sh_offset;
+ }
+ if (adjust_dso_nonalloc (dso, liblist + 1, oldoffset, adjust))
+ goto error_out;
+ dso->shdr[liblist].sh_offset = newoffset;
+ dso->shdr[liblist].sh_size = data->d_size;
+ }
+
+ recompute_nonalloc_offsets (dso);
+ return 0;
+
+error_out:
+ free (list);
+ return 1;
+}
+
+static int
+prelink_set_timestamp (struct prelink_info *info)
+{
+ DSO *dso = info->dso;
+
+ if (! verify)
+ info->ent->timestamp = getenv ("PRELINK_TIMESTAMP") ?
+ atoi (getenv ("PRELINK_TIMESTAMP"))
+ : (GElf_Word) time (NULL);
+ dso->info_DT_GNU_PRELINKED = info->ent->timestamp;
+ if (prelink_set_checksum (dso))
+ return 1;
+ info->ent->checksum = dso->info_DT_CHECKSUM;
+ return 0;
+}
+
+static void
+free_info (struct prelink_info *info)
+{
+ int i;
+
+ free (info->symtab);
+ free (info->dynbss);
+ free (info->sdynbss);
+ free (info->conflict_rela);
+ if (info->conflicts)
+ {
+ for (i = 0; i < info->ent->ndepends + 1; ++i)
+ {
+ struct prelink_conflict *c = info->conflicts[i];
+ void *f;
+
+ while (c != NULL)
+ {
+ f = c;
+ c = c->next;
+ free (f);
+ }
+ }
+ free (info->conflicts);
+ }
+ if (info->sonames)
+ {
+ for (i = 0; i < info->ent->ndepends + 1; ++i)
+ free ((char *) info->sonames[i]);
+ free (info->sonames);
+ }
+ free (info->tls);
+ if (info->symbols)
+ {
+ for (i = 0; i < info->symbol_count; ++i)
+ {
+ struct prelink_symbol *s = info->symbols[i].next;
+ void *f;
+
+ while (s != NULL)
+ {
+ f = s;
+ s = s->next;
+ free (f);
+ }
+ }
+ free (info->symbols);
+ }
+}
+
+int
+prelink (DSO *dso, struct prelink_entry *ent)
+{
+ int i;
+ Elf_Scn *scn;
+ Elf_Data *data;
+ struct prelink_info info;
+
+ ent->pltgot = dso->info[DT_PLTGOT];
+
+ if (! dso->info[DT_SYMTAB])
+ return 0;
+
+ if (! dso_is_rdwr (dso) && dso->ehdr.e_type == ET_DYN)
+ {
+ if (reopen_dso (dso, NULL, NULL))
+ return 1;
+ }
+
+ i = addr_to_sec (dso, dso->info[DT_SYMTAB]);
+ /* DT_SYMTAB should be found and should point to
+ start of .dynsym section. */
+ if (i == -1
+ || dso->info[DT_SYMTAB] != dso->shdr[i].sh_addr)
+ {
+ error (0, 0, "%s: Bad symtab", dso->filename);
+ return 1;
+ }
+
+ memset (&info, 0, sizeof (info));
+ info.ent = ent;
+ info.symtab_entsize = dso->shdr[i].sh_entsize;
+ info.symtab = calloc (dso->shdr[i].sh_size / dso->shdr[i].sh_entsize,
+ sizeof (GElf_Sym));
+ if (info.symtab == NULL)
+ {
+ error (0, ENOMEM, "%s: Cannot convert .dynsym section", dso->filename);
+ return 1;
+ }
+
+ scn = dso->scn[i];
+ data = NULL;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx, loc;
+
+ loc = data->d_off / info.symtab_entsize;
+ maxndx = data->d_size / info.symtab_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx)
+ gelfx_getsym (dso->elf, data, ndx, info.symtab + loc + ndx);
+ }
+ info.symtab_start =
+ adjust_new_to_old (dso, dso->shdr[i].sh_addr - dso->base);
+ info.symtab_end = info.symtab_start + dso->shdr[i].sh_size;
+ info.dso = dso;
+ switch (prelink_get_relocations (&info))
+ {
+ case 0:
+ goto error_out;
+ case 1:
+ info.resolve = resolve_ldso;
+ break;
+ case 2:
+ info.resolve = resolve_dso;
+ break;
+ }
+
+ if (dso->arch->arch_pre_prelink && dso->arch->arch_pre_prelink (dso))
+ goto error_out;
+
+ if (dso->ehdr.e_type == ET_EXEC)
+ {
+ if (prelink_exec (&info))
+ goto error_out;
+ }
+ else if (prelink_dso (&info))
+ goto error_out;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ {
+ if (! (dso->shdr[i].sh_flags & SHF_ALLOC))
+ continue;
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".gnu.conflict"))
+ continue;
+ switch (dso->shdr[i].sh_type)
+ {
+ case SHT_REL:
+ if (prelink_rel (dso, i, &info))
+ goto error_out;
+ break;
+ case SHT_RELA:
+ if (prelink_rela (dso, i, &info))
+ goto error_out;
+ break;
+ }
+ }
+
+ if (dso->arch->arch_prelink && dso->arch->arch_prelink (&info))
+ goto error_out;
+
+ if (dso->arch->read_opd && dso->arch->read_opd (dso, ent))
+ goto error_out;
+
+ /* Must be last. */
+ if (dso->ehdr.e_type == ET_DYN
+ && prelink_set_timestamp (&info))
+ goto error_out;
+
+ free_info (&info);
+ return 0;
+
+error_out:
+ free_info (&info);
+ return 1;
+}
diff --git a/trunk/src/prelink.h b/trunk/src/prelink.h
new file mode 100644
index 0000000..0118602
--- /dev/null
+++ b/trunk/src/prelink.h
@@ -0,0 +1,527 @@
+/* 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. */
+
+#ifndef PRELINK_H
+#define PRELINK_H
+
+#include <elf.h>
+#include <libelf.h>
+#include <gelfx.h>
+#include <ftw.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <utime.h>
+
+#ifndef DT_GNU_LIBLIST
+#define DT_GNU_LIBLIST 0x6ffffef9
+#define DT_GNU_LIBLISTSZ 0x6ffffdf7
+#define DT_GNU_CONFLICT 0x6ffffef8
+#define DT_GNU_CONFLICTSZ 0x6ffffdf6
+#define DT_GNU_PRELINKED 0x6ffffdf5
+#define SHT_GNU_LIBLIST 0x6ffffff7
+#endif
+
+#if DT_GNU_LIBLIST == 0x6ffffef7
+#undef DT_GNU_LIBLIST
+#undef DT_GNU_CONFLICT
+#undef SHT_GNU_LIBLIST
+#define DT_GNU_LIBLIST 0x6ffffef9
+#define DT_GNU_CONFLICT 0x6ffffef8
+#define SHT_GNU_LIBLIST 0x6ffffff7
+#endif
+
+#ifndef DT_GNU_HASH
+#define DT_GNU_HASH 0x6ffffef5
+#define SHT_GNU_HASH 0x6ffffff6
+#endif
+
+#ifndef DT_MIPS_RLD_VERSION
+#define DT_MIPS_RLD_VERSION 0x70000001
+#define DT_MIPS_TIME_STAMP 0x70000002
+#define DT_MIPS_ICHECKSUM 0x70000003
+#define DT_MIPS_IVERSION 0x70000004
+#define DT_MIPS_FLAGS 0x70000005
+#define DT_MIPS_BASE_ADDRESS 0x70000006
+#define DT_MIPS_CONFLICT 0x70000008
+#define DT_MIPS_LIBLIST 0x70000009
+#define DT_MIPS_LOCAL_GOTNO 0x7000000a
+#define DT_MIPS_CONFLICTNO 0x7000000b
+#define DT_MIPS_LIBLISTNO 0x70000010
+#define DT_MIPS_SYMTABNO 0x70000011
+#define DT_MIPS_UNREFEXTNO 0x70000012
+#define DT_MIPS_GOTSYM 0x70000013
+#define DT_MIPS_HIPAGENO 0x70000014
+#define DT_MIPS_RLD_MAP 0x70000016
+#endif
+
+#ifndef R_MIPS_TLS_DTPMOD32
+#define R_MIPS_TLS_DTPMOD32 38
+#define R_MIPS_TLS_DTPREL32 39
+#define R_MIPS_TLS_TPREL32 47
+#endif
+
+#ifndef R_MIPS_GLOB_DAT
+#define R_MIPS_GLOB_DAT 51
+#endif
+
+struct prelink_entry;
+struct prelink_info;
+struct PLArch;
+struct opd_lib;
+
+struct PLAdjust
+{
+ GElf_Addr start;
+ GElf_Addr adjust;
+};
+
+struct section_move
+{
+ int old_shnum;
+ int new_shnum;
+ int *old_to_new;
+ int *new_to_old;
+};
+
+typedef struct
+{
+ Elf *elf, *elfro;
+ GElf_Ehdr ehdr;
+ GElf_Phdr *phdr;
+ Elf_Scn **scn;
+ GElf_Addr base, end, align;
+ GElf_Addr mask;
+ GElf_Addr info[DT_NUM];
+ GElf_Addr info_DT_GNU_PRELINKED;
+ GElf_Addr info_DT_CHECKSUM;
+ GElf_Addr info_DT_VERNEED, info_DT_VERDEF, info_DT_VERSYM;
+ GElf_Addr info_DT_GNU_HASH;
+ GElf_Addr info_DT_MIPS_LOCAL_GOTNO;
+ GElf_Addr info_DT_MIPS_GOTSYM;
+ GElf_Addr info_DT_MIPS_SYMTABNO;
+#define DT_GNU_PRELINKED_BIT 50
+#define DT_CHECKSUM_BIT 51
+#define DT_VERNEED_BIT 52
+#define DT_VERDEF_BIT 53
+#define DT_VERSYM_BIT 54
+#define DT_FILTER_BIT 55
+#define DT_AUXILIARY_BIT 56
+#define DT_LOPROC_BIT 57
+#define DT_GNU_HASH_BIT 58
+ uint64_t info_set_mask;
+ int fd, fdro;
+ int lastscn, dynamic;
+ const char *soname;
+ const char *filename, *temp_filename;
+ struct PLArch *arch;
+ struct PLAdjust *adjust;
+ /* .mdebug has absolute file offsets in it. */
+ GElf_Off mdebug_orig_offset;
+ Elf_Data undo;
+ int nadjust;
+ int permissive;
+ struct section_move *move;
+ GElf_Shdr shdr[0];
+} DSO;
+
+#define dynamic_info_is_set(dso,bit) ((dso)->info_set_mask & (1ULL << (bit)))
+
+struct layout_libs;
+
+struct PLArch
+{
+ const char *name;
+ int class;
+ int machine;
+ int alternate_machine[3];
+ int max_reloc_size;
+ const char *dynamic_linker;
+ const char *dynamic_linker_alt;
+ int R_COPY;
+ int R_JMP_SLOT;
+ int R_RELATIVE;
+ int (*arch_adjust) (DSO *dso, GElf_Addr start, GElf_Addr adjust);
+ int (*adjust_section) (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust);
+ int (*adjust_dyn) (DSO *dso, int n, GElf_Dyn *dyn, GElf_Addr start,
+ GElf_Addr adjust);
+ int (*adjust_rel) (DSO *dso, GElf_Rel *rel, GElf_Addr start,
+ GElf_Addr adjust);
+ int (*adjust_rela) (DSO *dso, GElf_Rela *rela, GElf_Addr start,
+ GElf_Addr adjust);
+ int (*prelink_rel) (struct prelink_info *info, GElf_Rel *rel,
+ GElf_Addr reladdr);
+ int (*prelink_rela) (struct prelink_info *info, GElf_Rela *rela,
+ GElf_Addr relaaddr);
+ int (*prelink_conflict_rel) (DSO *dso, struct prelink_info *info,
+ GElf_Rel *rel, GElf_Addr reladdr);
+ int (*prelink_conflict_rela) (DSO *dso, struct prelink_info *info,
+ GElf_Rela *rela, GElf_Addr relaaddr);
+ int (*arch_prelink_conflict) (DSO *dso, struct prelink_info *info);
+ int (*apply_conflict_rela) (struct prelink_info *info, GElf_Rela *rela,
+ char *buf);
+ int (*apply_rel) (struct prelink_info *info, GElf_Rel *rel, char *buf);
+ int (*apply_rela) (struct prelink_info *info, GElf_Rela *rela, char *buf);
+ int (*rel_to_rela) (DSO *dso, GElf_Rel *rel, GElf_Rela *rela);
+ int (*rela_to_rel) (DSO *dso, GElf_Rela *rela, GElf_Rel *rel);
+ int (*need_rel_to_rela) (DSO *dso, int first, int last);
+ GElf_Addr (*create_opd) (struct prelink_info *info, int first, int last,
+ int plt);
+ int (*read_opd) (DSO *dso, struct prelink_entry *ent);
+ int (*free_opd) (struct prelink_entry *ent);
+ /* Return reloc size in bytes for given non-COPY reloc type. */
+ int (*reloc_size) (int);
+#define RTYPE_CLASS_VALID 8
+#define RTYPE_CLASS_PLT (8|1)
+#define RTYPE_CLASS_COPY (8|2)
+#define RTYPE_CLASS_TLS (8|4)
+ int (*reloc_class) (int);
+ int (*arch_pre_prelink) (DSO *dso);
+ int (*arch_prelink) (struct prelink_info *info);
+ int (*arch_undo_prelink) (DSO *dso);
+ int (*undo_prelink_rel) (DSO *dso, GElf_Rel *rel, GElf_Addr reladdr);
+ int (*undo_prelink_rela) (DSO *dso, GElf_Rela *rela, GElf_Addr relaaddr);
+ int (*layout_libs_init) (struct layout_libs *l);
+ int (*layout_libs_pre) (struct layout_libs *l);
+ int (*layout_libs_post) (struct layout_libs *l);
+ GElf_Addr mmap_base, mmap_end;
+ /* max_page_size is the ELF page size (ELF_MAXPAGESIZE in bfd),
+ page_size is PAGE_SIZE the architecture typically has,
+ or if there are more typical sizes, the smallest one.
+ It doesn't need to be the absolutely smallest supported one,
+ prelink only optimizes for such page_size. */
+ GElf_Addr max_page_size, page_size;
+} __attribute__((aligned(64)));
+
+DSO * open_dso (const char *name);
+DSO * fdopen_dso (int fd, const char *name);
+struct section_move *init_section_move (DSO *dso);
+void add_section (struct section_move *move, int sec);
+void remove_section (struct section_move *move, int sec);
+int reopen_dso (DSO *dso, struct section_move *move, const char *);
+int adjust_symbol_p (DSO *dso, GElf_Sym *sym);
+int check_dso (DSO *dso);
+int dso_is_rdwr (DSO *dso);
+void read_dynamic (DSO *dso);
+int set_dynamic (DSO *dso, GElf_Word tag, GElf_Addr value, int fatal);
+int addr_to_sec (DSO *dso, GElf_Addr addr);
+int adjust_dso (DSO *dso, GElf_Addr start, GElf_Addr adjust);
+int adjust_nonalloc (DSO *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int first,
+ GElf_Addr start, GElf_Addr adjust);
+int adjust_dso_nonalloc (DSO *dso, int first, GElf_Addr start,
+ GElf_Addr adjust);
+int recompute_nonalloc_offsets (DSO *dso);
+int adjust_stabs (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust);
+int adjust_dwarf2 (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust);
+int adjust_mdebug (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust);
+int finalize_mdebug (DSO *dso);
+int relocate_dso (DSO *dso, GElf_Addr base);
+int update_dso (DSO *dso, const char *);
+int prepare_write_dso (DSO *dso);
+int write_dso (DSO *dso);
+int close_dso (DSO *dso);
+GElf_Addr adjust_old_to_new (DSO *dso, GElf_Addr addr);
+GElf_Addr adjust_new_to_old (DSO *dso, GElf_Addr addr);
+int strtabfind (DSO *dso, int strndx, const char *name);
+int shstrtabadd (DSO *dso, const char *name);
+
+/* data.c */
+
+/* Used for reading consecutive blocks of data from a DSO. */
+struct data_iterator {
+ /* The DSO that is being read. */
+ DSO *dso;
+
+ /* The data block that contained the last byte to be read.
+ NULL if no data has been read yet or if the end of the
+ DSO has been reached. */
+ Elf_Data *data;
+
+ /* The section that contains DATA, when DATA is nonnull. */
+ int sec;
+
+ /* The address of the next byte. */
+ GElf_Addr addr;
+
+ /* The offset of the next byte from the start of SEC, when DATA
+ is nonnull. */
+ GElf_Addr sec_offset;
+};
+
+unsigned char * get_data (DSO *dso, GElf_Addr addr, int *scnp, Elf_Type *typep);
+#define READWRITEPROTO(le,nn) \
+uint##nn##_t buf_read_u##le##nn (unsigned char *data); \
+uint##nn##_t read_u##le##nn (DSO *dso, GElf_Addr addr); \
+void buf_write_##le##nn (unsigned char *data, uint##nn##_t val);\
+int write_##le##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val);
+#define READWRITEPROTOSIZE(nn) \
+READWRITEPROTO(le,nn) \
+READWRITEPROTO(be,nn) \
+uint##nn##_t buf_read_une##nn (DSO *dso, unsigned char *data); \
+uint##nn##_t read_une##nn (DSO *dso, GElf_Addr addr); \
+void buf_write_ne##nn (DSO *dso, unsigned char *data, \
+ uint##nn##_t val); \
+void write_ne##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val);
+READWRITEPROTO(,8)
+READWRITEPROTOSIZE(16)
+READWRITEPROTOSIZE(32)
+READWRITEPROTOSIZE(64)
+#undef READWRITEPROTO
+#undef READWRITEPROTOSIZE
+const char * strptr (DSO *dso, int sec, off_t offset);
+void init_data_iterator (struct data_iterator *it, DSO *dso, GElf_Addr addr);
+unsigned char *get_data_from_iterator (struct data_iterator *it,
+ GElf_Addr size);
+int get_sym_from_iterator (struct data_iterator *it, GElf_Sym *sym);
+
+#define PL_ARCH \
+static struct PLArch plarch __attribute__((section("pl_arch"),used))
+
+#define addr_adjust(addr, start, adjust) \
+ do { \
+ if (addr >= start) \
+ addr += adjust; \
+ } while (0)
+
+struct prelink_cache_entry
+{
+ uint32_t filename;
+ uint32_t depends;
+ uint32_t checksum;
+#define PCF_UNPRELINKABLE 0x40000
+#define PCF_PRELINKED 0x20000
+#define PCF_ELF64 0x10000
+#define PCF_MACHINE 0x0ffff
+ uint32_t flags;
+ uint32_t ctime;
+ uint32_t mtime;
+ uint64_t base;
+ uint64_t end;
+};
+
+struct prelink_cache
+{
+#define PRELINK_CACHE_NAME "prelink-ELF"
+#define PRELINK_CACHE_VER "0.3.2"
+#define PRELINK_CACHE_MAGIC PRELINK_CACHE_NAME PRELINK_CACHE_VER
+ const char magic [sizeof (PRELINK_CACHE_MAGIC) - 1];
+ uint32_t nlibs;
+ uint32_t ndeps;
+ uint32_t len_strings;
+ uint32_t unused[9];
+ struct prelink_cache_entry entry[0];
+ /* uint32_t depends [ndeps]; */
+ /* const char strings [len_strings]; */
+};
+
+struct prelink_link
+{
+ struct prelink_link *next;
+ const char *canon_filename;
+};
+
+struct prelink_entry
+{
+ const char *filename;
+ const char *canon_filename;
+ const char *soname;
+ struct prelink_link *hardlink;
+ GElf_Word timestamp;
+ GElf_Word checksum;
+ GElf_Addr base, end, layend, pltgot;
+ dev_t dev;
+ ino64_t ino;
+#define ET_BAD (ET_NUM)
+#define ET_CACHE_EXEC (ET_NUM + 1)
+#define ET_CACHE_DYN (ET_NUM + 2)
+#define ET_UNPRELINKABLE (ET_NUM + 3)
+ int type, done, ndepends, refs, flags;
+ union
+ {
+ int explicit;
+ int tmp;
+ } u;
+ uint32_t ctime, mtime;
+ struct prelink_entry **depends;
+ struct prelink_entry *prev, *next;
+ struct opd_lib *opd;
+};
+
+struct prelink_dir
+{
+ dev_t dev;
+ struct prelink_dir *next;
+ size_t len;
+ int flags;
+ char dir[0];
+};
+
+struct prelink_tls
+{
+ GElf_Addr modid;
+ GElf_Addr offset;
+};
+
+struct prelink_symbol
+{
+ union
+ {
+ struct prelink_entry *ent;
+ struct prelink_tls *tls;
+ } u;
+ struct prelink_symbol *next;
+ GElf_Addr value;
+ int reloc_class;
+};
+
+struct prelink_conflict
+{
+ struct prelink_conflict *next;
+ /* Object which it was relocated to. */
+ union
+ {
+ struct prelink_entry *ent;
+ struct prelink_tls *tls;
+ } lookup,
+ /* Object which the relocation was prelinked to. */
+ conflict;
+ /* Offset from start of owner to owner's symbol. */
+ GElf_Addr symoff;
+ /* Value it has in lookup.ent. */
+ GElf_Addr lookupval;
+ /* Value it has in conflict.ent. */
+ GElf_Addr conflictval;
+ int reloc_class;
+ int used;
+};
+
+#define conflict_lookup_value(cfl) \
+ (((cfl)->reloc_class != RTYPE_CLASS_TLS ? (cfl)->lookup.ent->base : 0) \
+ + (cfl)->lookupval)
+
+struct prelink_info
+{
+ DSO *dso;
+ DSO **dsos;
+ struct prelink_entry *ent;
+ struct prelink_symbol *symbols;
+ struct prelink_conflict **conflicts;
+ struct prelink_conflict *curconflicts;
+ struct prelink_tls *tls, *curtls;
+ const char **sonames;
+ char *dynbss, *sdynbss;
+ GElf_Addr dynbss_base, sdynbss_base;
+ size_t dynbss_size, sdynbss_size, symtab_entsize;
+ int symbol_count;
+ GElf_Sym *symtab;
+ GElf_Rela *conflict_rela;
+ size_t conflict_rela_alloced, conflict_rela_size;
+ GElf_Addr symtab_start, symtab_end;
+ GElf_Addr (*resolve) (struct prelink_info *info, GElf_Word r_sym,
+ int reloc_type);
+ struct prelink_entry *resolveent;
+ struct prelink_tls *resolvetls;
+};
+
+int prelink_prepare (DSO *dso);
+int prelink (DSO *dso, struct prelink_entry *ent);
+int prelink_init_cache (void);
+int prelink_load_cache (void);
+int prelink_print_cache (void);
+int prelink_save_cache (int do_warn);
+struct prelink_entry *
+ prelink_find_entry (const char *filename, const struct stat64 *stp,
+ int insert);
+struct prelink_conflict *
+ prelink_conflict (struct prelink_info *info, GElf_Word r_sym,
+ int reloc_type);
+GElf_Rela *prelink_conflict_add_rela (struct prelink_info *info);
+int prelink_get_relocations (struct prelink_info *info);
+int prelink_build_conflicts (struct prelink_info *info);
+int update_dynamic_tags (DSO *dso, GElf_Shdr *shdr, GElf_Shdr *old_shdr,
+ struct section_move *move);
+int prelink_exec (struct prelink_info *info);
+int prelink_set_checksum (DSO *dso);
+int is_ldso_soname (const char *soname);
+
+int prelink_undo (DSO *dso);
+
+int prelink_verify (const char *filename);
+
+int gather_object (const char *dir, int deref, int onefs);
+int gather_config (const char *config);
+int gather_check_libs (void);
+int add_to_blacklist (const char *name, int deref, int onefs);
+int blacklist_from_config (const char *config);
+
+FILE *execve_open (const char *path, char *const argv[], char *const envp[]);
+int execve_close (FILE *f);
+
+int remove_redundant_cxx_conflicts (struct prelink_info *info);
+int get_relocated_mem (struct prelink_info *info, DSO *dso, GElf_Addr addr,
+ char *buf, GElf_Word size);
+
+int layout_libs (void);
+
+void prelink_all (void);
+
+int undo_all (void);
+
+char *prelink_canonicalize (const char *name, struct stat64 *stp);
+
+extern const char *dynamic_linker;
+extern const char *ld_library_path;
+extern const char *prelink_cache;
+extern const char *prelink_conf;
+extern const char *undo_output;
+extern int all;
+extern int force;
+extern int random_base;
+extern int conserve_memory;
+extern int verbose;
+extern int dry_run;
+extern int libs_only;
+extern int enable_cxx_optimizations;
+extern int exec_shield;
+extern int undo;
+extern int verify;
+extern int print_cache;
+enum verify_method_t { VERIFY_CONTENT, VERIFY_MD5, VERIFY_SHA };
+extern enum verify_method_t verify_method;
+extern int quick;
+extern long long seed;
+extern GElf_Addr mmap_reg_start, mmap_reg_end;
+
+extern const char *sysroot;
+
+char *wrap_prelink_canonicalize (const char *name, struct stat64 *stp);
+int wrap_lstat64 (const char *file, struct stat64 *buf);
+int wrap_stat64 (const char *file, struct stat64 *buf);
+int wrap_open (const char *file, int mode, ...);
+int wrap_access (const char *file, int mode);
+int wrap_rename (const char *old, const char *new);
+int wrap_link (const char *old, const char *new);
+int wrap_nftw64 (const char *dir, __nftw64_func_t func,
+ int descriptors, int flag);
+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 */
diff --git a/trunk/src/prelinktab.h b/trunk/src/prelinktab.h
new file mode 100644
index 0000000..f41c79c
--- /dev/null
+++ b/trunk/src/prelinktab.h
@@ -0,0 +1,27 @@
+/* Copyright (C) 2001 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. */
+
+#ifndef PRELINKTAB_H
+#define PRELINKTAB_H
+
+#include "hashtab.h"
+#include "prelink.h"
+
+extern htab_t prelink_devino_htab, prelink_filename_htab;
+extern int prelink_entry_count;
+
+#endif /* PRELINKTAB_H */
diff --git a/trunk/src/reloc.c b/trunk/src/reloc.c
new file mode 100644
index 0000000..70b4dbc
--- /dev/null
+++ b/trunk/src/reloc.c
@@ -0,0 +1,422 @@
+/* Copyright (C) 2001, 2002, 2003, 2005 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+
+int
+find_reloc_sections (DSO *dso, struct reloc_info *rinfo)
+{
+ int first, last, rela, i;
+ GElf_Addr start, end, pltstart, pltend;
+
+ memset (rinfo, 0, sizeof (*rinfo));
+
+ if (dynamic_info_is_set (dso, DT_REL)
+ && dynamic_info_is_set (dso, DT_RELA))
+ {
+ error (0, 0, "%s: Cannot prelink object with both DT_REL and DT_RELA tags",
+ dso->filename);
+ return 1;
+ }
+
+ rela = dynamic_info_is_set (dso, DT_RELA);
+
+ if (rela)
+ {
+ start = dso->info[DT_RELA];
+ end = dso->info[DT_RELA] + dso->info[DT_RELASZ];
+ }
+ else
+ {
+ start = dso->info[DT_REL];
+ end = dso->info[DT_REL] + dso->info[DT_RELSZ];
+ }
+ rinfo->reldyn_rela = rela;
+
+ if (dso->info[DT_JMPREL])
+ {
+ pltstart = dso->info[DT_JMPREL];
+ pltend = dso->info[DT_JMPREL] + dso->info[DT_PLTRELSZ];
+ first = addr_to_sec (dso, pltstart);
+ last = addr_to_sec (dso, pltend - 1);
+ if (first == -1
+ || last == -1
+ || first != last
+ || dso->shdr[first].sh_addr != pltstart
+ || dso->shdr[first].sh_addr + dso->shdr[first].sh_size != pltend
+ || (dso->info[DT_PLTREL] != DT_REL
+ && dso->info[DT_PLTREL] != DT_RELA)
+ || dso->shdr[first].sh_type
+ != (dso->info[DT_PLTREL] == DT_RELA ? SHT_RELA : SHT_REL)
+ || strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[first].sh_name),
+ dso->info[DT_PLTREL] == DT_RELA
+ ? ".rela.plt" : ".rel.plt"))
+ {
+ error (0, 0, "%s: DT_JMPREL tags don't surround .rel%s.plt section",
+ dso->filename, dso->info[DT_PLTREL] == DT_RELA ? "a" : "");
+ return 1;
+ }
+ rinfo->plt = first;
+ rinfo->plt_rela = (dso->shdr[first].sh_type == SHT_RELA);
+ if (dso->shdr[first].sh_type == SHT_REL
+ && dso->arch->need_rel_to_rela != NULL
+ && dso->arch->need_rel_to_rela (dso, first, first))
+ rinfo->rel_to_rela_plt = 1;
+ }
+ else
+ {
+ pltstart = end;
+ pltend = end;
+ }
+
+ if (start == 0 && end == 0)
+ {
+ /* No non-PLT relocations. */
+ return 0;
+ }
+
+ if (start == end)
+ {
+ first = 0;
+ last = 0;
+ }
+ else
+ {
+ first = addr_to_sec (dso, start);
+ last = addr_to_sec (dso, end - 1);
+
+ if (first == -1
+ || last == -1
+ || dso->shdr[first].sh_addr != start
+ || dso->shdr[last].sh_addr + dso->shdr[last].sh_size != end)
+ {
+ error (0, 0, "%s: DT_REL%s tags don't surround whole relocation sections",
+ dso->filename, rela ? "A" : "");
+ return 1;
+ }
+
+ for (i = first; i <= last; i++)
+ if (dso->shdr[i].sh_type != (rela ? SHT_RELA : SHT_REL))
+ {
+ error (0, 0, "%s: DT_REL%s tags don't surround relocation sections of expected type",
+ dso->filename, rela ? "A" : "");
+ return 1;
+ }
+ }
+
+ if (pltstart != end && pltend != end)
+ {
+ error (0, 0, "%s: DT_JMPREL tag not adjacent to DT_REL%s relocations",
+ dso->filename, rela ? "A" : "");
+ return 1;
+ }
+
+ if (pltstart == start && pltend == end)
+ {
+ /* No non-PLT relocations. */
+ rinfo->overlap = 1;
+ return 0;
+ }
+
+ if (pltstart != end && pltend == end)
+ {
+ rinfo->overlap = 1;
+ --last;
+ }
+
+ rinfo->first = first;
+ rinfo->last = last;
+ if (! rela
+ && first
+ && dso->arch->need_rel_to_rela != NULL
+ && dso->arch->need_rel_to_rela (dso, first, last))
+ rinfo->rel_to_rela = 1;
+ return 0;
+}
+
+int
+convert_rel_to_rela (DSO *dso, int i)
+{
+ Elf_Data d1, d2, *d;
+ Elf_Scn *scn;
+ GElf_Rel rel;
+ GElf_Rela rela;
+ int ndx, maxndx;
+
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (elf_getdata (scn, d) == NULL);
+ assert (d->d_off == 0);
+ assert (d->d_size == dso->shdr[i].sh_size);
+ d1 = *d;
+ d2 = *d;
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ d1.d_size = d->d_size / 2 * 3;
+ d1.d_buf = malloc (d1.d_size);
+ d1.d_type = ELF_T_RELA;
+ if (d1.d_buf == NULL)
+ {
+ error (0, ENOMEM, "Cannot convert REL section to RELA");
+ return 1;
+ }
+
+ maxndx = d->d_size / dso->shdr[i].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ndx++)
+ {
+ if (gelfx_getrel (dso->elf, d, ndx, &rel) == 0
+ || dso->arch->rel_to_rela (dso, &rel, &rela))
+ {
+ free (d1.d_buf);
+ return 1;
+ }
+ /* gelf_update_rel etc. should have Elf * argument, so that
+ we don't have to do this crap. */
+ *d = d1;
+ if (gelfx_update_rela (dso->elf, d, ndx, &rela) == 0)
+ {
+ *d = d2;
+ free (d1.d_buf);
+ return 1;
+ }
+ *d = d2;
+ }
+
+ free (d2.d_buf);
+ *d = d1;
+ dso->shdr[i].sh_entsize
+ = gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ dso->shdr[i].sh_type = SHT_RELA;
+ return 0;
+}
+
+int
+convert_rela_to_rel (DSO *dso, int i)
+{
+ Elf_Data d1, d2, *d;
+ Elf_Scn *scn;
+ GElf_Rel rel;
+ GElf_Rela rela;
+ int ndx, maxndx;
+
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (elf_getdata (scn, d) == NULL);
+ assert (d->d_off == 0);
+ assert (d->d_size == dso->shdr[i].sh_size);
+ d1 = *d;
+ d2 = *d;
+ assert (sizeof (Elf32_Rel) * 3 == sizeof (Elf32_Rela) * 2);
+ assert (sizeof (Elf64_Rel) * 3 == sizeof (Elf64_Rela) * 2);
+ d1.d_size = d->d_size / 3 * 2;
+ d1.d_buf = malloc (d1.d_size);
+ d1.d_type = ELF_T_REL;
+ if (d1.d_buf == NULL)
+ {
+ error (0, ENOMEM, "Cannot convert RELA section to REL");
+ return 1;
+ }
+
+ maxndx = d->d_size / dso->shdr[i].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ndx++)
+ {
+ if (gelfx_getrela (dso->elf, d, ndx, &rela) == 0
+ || dso->arch->rela_to_rel (dso, &rela, &rel))
+ {
+ free (d1.d_buf);
+ return 1;
+ }
+ /* gelf_update_rela etc. should have Elf * argument, so that
+ we don't have to do this crap. */
+ *d = d1;
+ if (gelfx_update_rel (dso->elf, d, ndx, &rel) == 0)
+ {
+ *d = d2;
+ free (d1.d_buf);
+ return 1;
+ }
+ *d = d2;
+ }
+
+ free (d2.d_buf);
+ *d = d1;
+ dso->shdr[i].sh_entsize
+ = gelf_fsize (dso->elf, ELF_T_REL, 1, EV_CURRENT);
+ dso->shdr[i].sh_type = SHT_REL;
+ return 0;
+}
+
+int
+update_dynamic_rel (DSO *dso, struct reloc_info *rinfo)
+{
+ GElf_Dyn *info[DT_NUM], *info_DT_RELCOUNT, *info_DT_RELACOUNT;
+ GElf_Dyn *dynamic = NULL;
+ int rel = rinfo->first, plt = rinfo->plt, overlap = rinfo->overlap;
+ int dynsec, count = 0, loc;
+ Elf_Data *data;
+ Elf_Scn *scn = NULL;
+
+ memset (&info, 0, sizeof (info));
+ info_DT_RELCOUNT = NULL;
+ info_DT_RELACOUNT = NULL;
+ for (dynsec = 0; dynsec < dso->ehdr.e_shnum; dynsec++)
+ if (dso->shdr[dynsec].sh_type == SHT_DYNAMIC)
+ {
+ scn = dso->scn[dynsec];
+ dynamic = alloca (dso->shdr[dynsec].sh_size
+ / dso->shdr[dynsec].sh_entsize * sizeof (GElf_Dyn));
+ loc = 0;
+ data = NULL;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+
+ maxndx = data->d_size / dso->shdr[dynsec].sh_entsize;
+ for (ndx = 0; ndx < maxndx; ++ndx, ++loc)
+ {
+ gelfx_getdyn (dso->elf, data, ndx, dynamic + loc);
+ if (dynamic[loc].d_tag == DT_NULL)
+ break;
+ else if ((GElf_Xword) dynamic[loc].d_tag < DT_NUM)
+ info[dynamic[loc].d_tag] = dynamic + loc;
+ else if (dynamic[loc].d_tag == DT_RELCOUNT)
+ info_DT_RELCOUNT = dynamic + loc;
+ else if (dynamic[loc].d_tag == DT_RELACOUNT)
+ info_DT_RELACOUNT = dynamic + loc;
+ }
+ if (ndx < maxndx)
+ break;
+ }
+ count = loc;
+ break;
+ }
+
+ if (rel && plt && overlap)
+ {
+ if (dso->shdr[rel].sh_type != dso->shdr[plt].sh_type)
+ overlap = 0;
+ }
+
+ if (rel || (plt && overlap))
+ {
+ int dt_RELENT, dt_REL, dt_RELSZ;
+
+ if (rinfo->reldyn_rela)
+ {
+ dt_RELENT = DT_RELAENT;
+ dt_REL = DT_RELA;
+ dt_RELSZ = DT_RELASZ;
+ }
+ else
+ {
+ dt_RELENT = DT_RELENT;
+ dt_REL = DT_REL;
+ dt_RELSZ = DT_RELSZ;
+ }
+
+ assert (dso->info[dt_RELENT]
+ == gelf_fsize (dso->elf, rinfo->reldyn_rela
+ ? ELF_T_RELA : ELF_T_REL, 1, EV_CURRENT));
+ assert (dso->info[dt_REL] != 0);
+ assert (dso->info[dt_RELSZ] != 0);
+
+ info[dt_REL]->d_un.d_ptr = dso->shdr[rel ?: plt].sh_addr;
+ if (plt && overlap)
+ info[dt_RELSZ]->d_un.d_val =
+ dso->shdr[plt].sh_addr + dso->shdr[plt].sh_size;
+ else
+ info[dt_RELSZ]->d_un.d_val =
+ dso->shdr[rinfo->last].sh_addr + dso->shdr[rinfo->last].sh_size;
+ info[dt_RELSZ]->d_un.d_val -= info[dt_REL]->d_un.d_ptr;
+
+ if (!rinfo->reldyn_rela && dso->shdr[rel ?: plt].sh_type == SHT_RELA)
+ {
+ info[DT_RELENT]->d_un.d_val =
+ gelf_fsize (dso->elf, ELF_T_RELA, 1, EV_CURRENT);
+ info[DT_REL]->d_tag = DT_RELA;
+ info[DT_RELSZ]->d_tag = DT_RELASZ;
+ info[DT_RELENT]->d_tag = DT_RELAENT;
+ if (info_DT_RELCOUNT)
+ info_DT_RELCOUNT->d_tag = DT_RELACOUNT;
+ }
+ else if (rinfo->reldyn_rela && dso->shdr[rel ?: plt].sh_type == SHT_REL)
+ {
+ info[DT_RELAENT]->d_un.d_val =
+ gelf_fsize (dso->elf, ELF_T_REL, 1, EV_CURRENT);
+ info[DT_RELA]->d_tag = DT_REL;
+ info[DT_RELASZ]->d_tag = DT_RELSZ;
+ info[DT_RELAENT]->d_tag = DT_RELENT;
+ if (info_DT_RELACOUNT)
+ info_DT_RELACOUNT->d_tag = DT_RELCOUNT;
+ }
+ }
+
+ if (plt)
+ {
+ assert (dso->info[DT_JMPREL] != 0);
+ assert (dso->info[DT_PLTREL] == rinfo->plt_rela ? DT_RELA : DT_REL);
+
+ info[DT_JMPREL]->d_un.d_ptr = dso->shdr[plt].sh_addr;
+ if (!rinfo->plt_rela && dso->shdr[plt].sh_type == SHT_RELA)
+ {
+ info[DT_PLTREL]->d_un.d_val = DT_RELA;
+ info[DT_PLTRELSZ]->d_un.d_val = dso->shdr[plt].sh_size;
+ }
+ else if (rinfo->plt_rela && dso->shdr[plt].sh_type == SHT_REL)
+ {
+ info[DT_PLTREL]->d_un.d_val = DT_REL;
+ info[DT_PLTRELSZ]->d_un.d_val = dso->shdr[plt].sh_size;
+ }
+
+ if (!rel && !overlap)
+ {
+ int dt_REL = rinfo->reldyn_rela ? DT_RELA : DT_REL;
+
+ if (info[dt_REL] && info[dt_REL]->d_un.d_ptr)
+ info[dt_REL]->d_un.d_ptr = info[DT_JMPREL]->d_un.d_ptr;
+ }
+ }
+
+ loc = 0;
+ data = NULL;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+
+ maxndx = data->d_size / dso->shdr[dynsec].sh_entsize;
+ for (ndx = 0; ndx < maxndx && loc < count; ++ndx, ++loc)
+ if ((GElf_Xword) dynamic[loc].d_tag < DT_NUM
+ || dynamic[loc].d_tag == DT_RELCOUNT
+ || dynamic[loc].d_tag == DT_RELACOUNT)
+ gelfx_update_dyn (dso->elf, data, ndx, dynamic + loc);
+ if (ndx < maxndx)
+ break;
+ }
+
+ read_dynamic (dso);
+ return 0;
+}
diff --git a/trunk/src/reloc.h b/trunk/src/reloc.h
new file mode 100644
index 0000000..a2ceff9
--- /dev/null
+++ b/trunk/src/reloc.h
@@ -0,0 +1,44 @@
+/* Copyright (C) 2001, 2002 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. */
+
+#ifndef RELOC_H
+#define RELOC_H
+
+#include "prelink.h"
+
+struct reloc_info
+{
+ int first; /* First dynamic SHT_REL* section. */
+ int last; /* Last dynamic SHT_REL* section not counting .rel*.plt. */
+ int plt; /* .rel*.plt section. */
+ int overlap; /* 1 if DT_REL{,A}SZ range includes DT_PLTRELSZ range. */
+ int reldyn_rela; /* first..last sections were originally RELA. */
+ int plt_rela; /* plt section was originally RELA. */
+ int rel_to_rela; /* first..last sections have to be converted REL->RELA. */
+ int rel_to_rela_plt; /* plt section has to be converted REL->RELA. */
+ int relcount; /* DT_RELCOUNT resp. DT_RELACOUNT. */
+};
+
+int find_reloc_sections (DSO *dso, struct reloc_info *rinfo);
+int convert_rel_to_rela (DSO *dso, int i);
+int convert_rela_to_rel (DSO *dso, int i);
+int update_dynamic_rel (DSO *dso, struct reloc_info *rinfo);
+int undo_sections (DSO *dso, int undo, struct section_move *move,
+ struct reloc_info *rinfo, GElf_Ehdr *ehdr,
+ GElf_Phdr *phdr, GElf_Shdr *shdr);
+
+#endif /* RELOC_H */
diff --git a/trunk/src/sha.c b/trunk/src/sha.c
new file mode 100644
index 0000000..cfbc956
--- /dev/null
+++ b/trunk/src/sha.c
@@ -0,0 +1,331 @@
+/* sha.c - Functions to compute the SHA1 hash (message-digest) of files
+ or blocks of memory. Complies to the NIST specification FIPS-180-1.
+
+ Copyright (C) 2000, 2001, 2003 Scott G. Miller
+
+ Credits:
+ Robert Klep <robert@ilse.nl> -- Expansion function fix
+ NOTE: The canonical source of this file is maintained in GNU coreutils.
+*/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <byteswap.h>
+#include "md5.h"
+#include "sha.h"
+
+/*
+ Not-swap is a macro that does an endian swap on architectures that are
+ big-endian, as SHA needs some data in a little-endian format
+*/
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+# define SWAP(n) bswap_32 (n)
+# define NOTSWAP(n) (n)
+#else
+# define SWAP(n) (n)
+# define NOTSWAP(n) bswap_32 (n)
+#endif
+
+/* This array contains the bytes used to pad the buffer to the next
+ 64-byte boundary. (RFC 1321, 3.1: Step 1) */
+static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
+
+
+/*
+ Takes a pointer to a 160 bit block of data (five 32 bit ints) and
+ intializes it to the start constants of the SHA1 algorithm. This
+ must be called before using hash in the call to sha_hash
+*/
+void
+sha_init_ctx (struct sha_ctx *ctx)
+{
+ ctx->A = 0x67452301;
+ ctx->B = 0xefcdab89;
+ ctx->C = 0x98badcfe;
+ ctx->D = 0x10325476;
+ ctx->E = 0xc3d2e1f0;
+
+ ctx->total[0] = ctx->total[1] = 0;
+ ctx->buflen = 0;
+}
+
+/* Put result from CTX in first 20 bytes following RESBUF. The result
+ must be in little endian byte order.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+void *
+sha_read_ctx (const struct sha_ctx *ctx, void *resbuf)
+{
+ ((md5_uint32 *) resbuf)[0] = NOTSWAP (ctx->A);
+ ((md5_uint32 *) resbuf)[1] = NOTSWAP (ctx->B);
+ ((md5_uint32 *) resbuf)[2] = NOTSWAP (ctx->C);
+ ((md5_uint32 *) resbuf)[3] = NOTSWAP (ctx->D);
+ ((md5_uint32 *) resbuf)[4] = NOTSWAP (ctx->E);
+
+ return resbuf;
+}
+
+/* Process the remaining bytes in the internal buffer and the usual
+ prolog according to the standard and write the result to RESBUF.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+void *
+sha_finish_ctx (struct sha_ctx *ctx, void *resbuf)
+{
+ /* Take yet unprocessed bytes into account. */
+ md5_uint32 bytes = ctx->buflen;
+ size_t pad;
+
+ /* Now count remaining bytes. */
+ ctx->total[0] += bytes;
+ if (ctx->total[0] < bytes)
+ ++ctx->total[1];
+
+ pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
+ memcpy (&ctx->buffer[bytes], fillbuf, pad);
+
+ /* Put the 64-bit file length in *bits* at the end of the buffer. */
+ *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = NOTSWAP (ctx->total[0] << 3);
+ *(md5_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) |
+ (ctx->total[0] >> 29));
+
+ /* Process last bytes. */
+ sha_process_block (ctx->buffer, bytes + pad + 8, ctx);
+
+ return sha_read_ctx (ctx, resbuf);
+}
+
+/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
+ result is always in little endian byte order, so that a byte-wise
+ output yields to the wanted ASCII representation of the message
+ digest. */
+void *
+sha_buffer (const char *buffer, size_t len, void *resblock)
+{
+ struct sha_ctx ctx;
+
+ /* Initialize the computation context. */
+ sha_init_ctx (&ctx);
+
+ /* Process whole buffer but last len % 64 bytes. */
+ sha_process_bytes (buffer, len, &ctx);
+
+ /* Put result in desired memory area. */
+ return sha_finish_ctx (&ctx, resblock);
+}
+
+void
+sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx)
+{
+ /* When we already have some bits in our internal buffer concatenate
+ both inputs first. */
+ if (ctx->buflen != 0)
+ {
+ size_t left_over = ctx->buflen;
+ size_t add = 128 - left_over > len ? len : 128 - left_over;
+
+ memcpy (&ctx->buffer[left_over], buffer, add);
+ ctx->buflen += add;
+
+ if (ctx->buflen > 64)
+ {
+ sha_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
+
+ ctx->buflen &= 63;
+ /* The regions in the following copy operation cannot overlap. */
+ memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
+ ctx->buflen);
+ }
+
+ buffer = (const char *) buffer + add;
+ len -= add;
+ }
+
+ /* Process available complete blocks. */
+ if (len >= 64)
+ {
+#define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
+ if (UNALIGNED_P (buffer))
+ while (len > 64)
+ {
+ sha_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
+ buffer = (const char *) buffer + 64;
+ len -= 64;
+ }
+ else
+ {
+ sha_process_block (buffer, len & ~63, ctx);
+ buffer = (const char *) buffer + (len & ~63);
+ len &= 63;
+ }
+ }
+
+ /* Move remaining bytes in internal buffer. */
+ if (len > 0)
+ {
+ size_t left_over = ctx->buflen;
+
+ memcpy (&ctx->buffer[left_over], buffer, len);
+ left_over += len;
+ if (left_over >= 64)
+ {
+ sha_process_block (ctx->buffer, 64, ctx);
+ left_over -= 64;
+ memcpy (ctx->buffer, &ctx->buffer[64], left_over);
+ }
+ ctx->buflen = left_over;
+ }
+}
+
+/* --- Code below is the primary difference between md5.c and sha.c --- */
+
+/* SHA1 round constants */
+#define K1 0x5a827999L
+#define K2 0x6ed9eba1L
+#define K3 0x8f1bbcdcL
+#define K4 0xca62c1d6L
+
+/* Round functions. Note that F2 is the same as F4. */
+#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
+#define F2(B,C,D) (B ^ C ^ D)
+#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
+#define F4(B,C,D) (B ^ C ^ D)
+
+/* Process LEN bytes of BUFFER, accumulating context into CTX.
+ It is assumed that LEN % 64 == 0.
+ Most of this code comes from GnuPG's cipher/sha1.c. */
+
+void
+sha_process_block (const void *buffer, size_t len, struct sha_ctx *ctx)
+{
+ const md5_uint32 *words = buffer;
+ size_t nwords = len / sizeof (md5_uint32);
+ const md5_uint32 *endp = words + nwords;
+ md5_uint32 x[16];
+ md5_uint32 a = ctx->A;
+ md5_uint32 b = ctx->B;
+ md5_uint32 c = ctx->C;
+ md5_uint32 d = ctx->D;
+ md5_uint32 e = ctx->E;
+
+ /* First increment the byte count. RFC 1321 specifies the possible
+ length of the file up to 2^64 bits. Here we only compute the
+ number of bytes. Do a double word increment. */
+ ctx->total[0] += len;
+ if (ctx->total[0] < len)
+ ++ctx->total[1];
+
+#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
+ ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
+ , (x[I&0x0f] = rol(tm, 1)) )
+
+#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
+ + F( B, C, D ) \
+ + K \
+ + M; \
+ B = rol( B, 30 ); \
+ } while(0)
+
+ while (words < endp)
+ {
+ md5_uint32 tm;
+ int t;
+ /* FIXME: see sha1.c for a better implementation. */
+ for (t = 0; t < 16; t++)
+ {
+ x[t] = NOTSWAP (*words);
+ words++;
+ }
+
+ R( a, b, c, d, e, F1, K1, x[ 0] );
+ R( e, a, b, c, d, F1, K1, x[ 1] );
+ R( d, e, a, b, c, F1, K1, x[ 2] );
+ R( c, d, e, a, b, F1, K1, x[ 3] );
+ R( b, c, d, e, a, F1, K1, x[ 4] );
+ R( a, b, c, d, e, F1, K1, x[ 5] );
+ R( e, a, b, c, d, F1, K1, x[ 6] );
+ R( d, e, a, b, c, F1, K1, x[ 7] );
+ R( c, d, e, a, b, F1, K1, x[ 8] );
+ R( b, c, d, e, a, F1, K1, x[ 9] );
+ R( a, b, c, d, e, F1, K1, x[10] );
+ R( e, a, b, c, d, F1, K1, x[11] );
+ R( d, e, a, b, c, F1, K1, x[12] );
+ R( c, d, e, a, b, F1, K1, x[13] );
+ R( b, c, d, e, a, F1, K1, x[14] );
+ R( a, b, c, d, e, F1, K1, x[15] );
+ R( e, a, b, c, d, F1, K1, M(16) );
+ R( d, e, a, b, c, F1, K1, M(17) );
+ R( c, d, e, a, b, F1, K1, M(18) );
+ R( b, c, d, e, a, F1, K1, M(19) );
+ R( a, b, c, d, e, F2, K2, M(20) );
+ R( e, a, b, c, d, F2, K2, M(21) );
+ R( d, e, a, b, c, F2, K2, M(22) );
+ R( c, d, e, a, b, F2, K2, M(23) );
+ R( b, c, d, e, a, F2, K2, M(24) );
+ R( a, b, c, d, e, F2, K2, M(25) );
+ R( e, a, b, c, d, F2, K2, M(26) );
+ R( d, e, a, b, c, F2, K2, M(27) );
+ R( c, d, e, a, b, F2, K2, M(28) );
+ R( b, c, d, e, a, F2, K2, M(29) );
+ R( a, b, c, d, e, F2, K2, M(30) );
+ R( e, a, b, c, d, F2, K2, M(31) );
+ R( d, e, a, b, c, F2, K2, M(32) );
+ R( c, d, e, a, b, F2, K2, M(33) );
+ R( b, c, d, e, a, F2, K2, M(34) );
+ R( a, b, c, d, e, F2, K2, M(35) );
+ R( e, a, b, c, d, F2, K2, M(36) );
+ R( d, e, a, b, c, F2, K2, M(37) );
+ R( c, d, e, a, b, F2, K2, M(38) );
+ R( b, c, d, e, a, F2, K2, M(39) );
+ R( a, b, c, d, e, F3, K3, M(40) );
+ R( e, a, b, c, d, F3, K3, M(41) );
+ R( d, e, a, b, c, F3, K3, M(42) );
+ R( c, d, e, a, b, F3, K3, M(43) );
+ R( b, c, d, e, a, F3, K3, M(44) );
+ R( a, b, c, d, e, F3, K3, M(45) );
+ R( e, a, b, c, d, F3, K3, M(46) );
+ R( d, e, a, b, c, F3, K3, M(47) );
+ R( c, d, e, a, b, F3, K3, M(48) );
+ R( b, c, d, e, a, F3, K3, M(49) );
+ R( a, b, c, d, e, F3, K3, M(50) );
+ R( e, a, b, c, d, F3, K3, M(51) );
+ R( d, e, a, b, c, F3, K3, M(52) );
+ R( c, d, e, a, b, F3, K3, M(53) );
+ R( b, c, d, e, a, F3, K3, M(54) );
+ R( a, b, c, d, e, F3, K3, M(55) );
+ R( e, a, b, c, d, F3, K3, M(56) );
+ R( d, e, a, b, c, F3, K3, M(57) );
+ R( c, d, e, a, b, F3, K3, M(58) );
+ R( b, c, d, e, a, F3, K3, M(59) );
+ R( a, b, c, d, e, F4, K4, M(60) );
+ R( e, a, b, c, d, F4, K4, M(61) );
+ R( d, e, a, b, c, F4, K4, M(62) );
+ R( c, d, e, a, b, F4, K4, M(63) );
+ R( b, c, d, e, a, F4, K4, M(64) );
+ R( a, b, c, d, e, F4, K4, M(65) );
+ R( e, a, b, c, d, F4, K4, M(66) );
+ R( d, e, a, b, c, F4, K4, M(67) );
+ R( c, d, e, a, b, F4, K4, M(68) );
+ R( b, c, d, e, a, F4, K4, M(69) );
+ R( a, b, c, d, e, F4, K4, M(70) );
+ R( e, a, b, c, d, F4, K4, M(71) );
+ R( d, e, a, b, c, F4, K4, M(72) );
+ R( c, d, e, a, b, F4, K4, M(73) );
+ R( b, c, d, e, a, F4, K4, M(74) );
+ R( a, b, c, d, e, F4, K4, M(75) );
+ R( e, a, b, c, d, F4, K4, M(76) );
+ R( d, e, a, b, c, F4, K4, M(77) );
+ R( c, d, e, a, b, F4, K4, M(78) );
+ R( b, c, d, e, a, F4, K4, M(79) );
+
+ a = ctx->A += a;
+ b = ctx->B += b;
+ c = ctx->C += c;
+ d = ctx->D += d;
+ e = ctx->E += e;
+ }
+}
diff --git a/trunk/src/sha.h b/trunk/src/sha.h
new file mode 100644
index 0000000..13583e3
--- /dev/null
+++ b/trunk/src/sha.h
@@ -0,0 +1,69 @@
+/* sha.h - Declaration of functions and datatypes for SHA1 sum computing
+ library functions.
+
+ Copyright (C) 1999, Scott G. Miller
+*/
+
+#ifndef _SHA_H
+# define _SHA_H 1
+
+# include "md5.h"
+
+/* Structure to save state of computation between the single steps. */
+struct sha_ctx
+{
+ md5_uint32 A;
+ md5_uint32 B;
+ md5_uint32 C;
+ md5_uint32 D;
+ md5_uint32 E;
+
+ md5_uint32 total[2];
+ md5_uint32 buflen;
+ char buffer[128];
+};
+
+
+/* Starting with the result of former calls of this function (or the
+ initialization function update the context for the next LEN bytes
+ starting at BUFFER.
+ It is necessary that LEN is a multiple of 64!!! */
+extern void sha_process_block (const void *buffer, size_t len,
+ struct sha_ctx *ctx);
+
+/* Starting with the result of former calls of this function (or the
+ initialization function update the context for the next LEN bytes
+ starting at BUFFER.
+ It is NOT required that LEN is a multiple of 64. */
+extern void sha_process_bytes (const void *buffer, size_t len,
+ struct sha_ctx *ctx);
+
+/* Initialize structure containing state of computation. */
+extern void sha_init_ctx (struct sha_ctx *ctx);
+
+/* Process the remaining bytes in the buffer and put result from CTX
+ in first 16 bytes following RESBUF. The result is always in little
+ endian byte order, so that a byte-wise output yields to the wanted
+ ASCII representation of the message digest.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+extern void *sha_finish_ctx (struct sha_ctx *ctx, void *resbuf);
+
+
+/* Put result from CTX in first 16 bytes following RESBUF. The result is
+ always in little endian byte order, so that a byte-wise output yields
+ to the wanted ASCII representation of the message digest.
+
+ IMPORTANT: On some systems it is required that RESBUF is correctly
+ aligned for a 32 bits value. */
+extern void *sha_read_ctx (const struct sha_ctx *ctx, void *resbuf);
+
+
+/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
+ result is always in little endian byte order, so that a byte-wise
+ output yields to the wanted ASCII representation of the message
+ digest. */
+extern void *sha_buffer (const char *buffer, size_t len, void *resblock);
+
+#endif
diff --git a/trunk/src/space.c b/trunk/src/space.c
new file mode 100644
index 0000000..4fea7c7
--- /dev/null
+++ b/trunk/src/space.c
@@ -0,0 +1,692 @@
+/* Copyright (C) 2001, 2002, 2003, 2004, 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+#include "space.h"
+
+#define DEBUG_SECTIONS
+
+#ifdef DEBUG_SECTIONS
+void
+print_sections (DSO *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr)
+{
+ int elf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
+ int i, j, shf, flag;
+ char buf[32], *q;
+ const char *p;
+ static struct { int sh_type; const char *type_name; } types[] =
+ {
+ { SHT_NULL, "NULL" },
+ { SHT_PROGBITS, "PROGBITS" },
+ { SHT_SYMTAB, "SYMTAB" },
+ { SHT_STRTAB, "STRTAB" },
+ { SHT_RELA, "RELA" },
+ { SHT_HASH, "HASH" },
+ { SHT_DYNAMIC, "DYNAMIC" },
+ { SHT_NOTE, "NOTE" },
+ { SHT_NOBITS, "NOBITS" },
+ { SHT_REL, "REL" },
+ { SHT_SHLIB, "SHLIB" },
+ { SHT_DYNSYM, "DYNSYM" },
+ { SHT_INIT_ARRAY, "INIT_ARRAY" },
+ { SHT_FINI_ARRAY, "FINI_ARRAY" },
+ { SHT_PREINIT_ARRAY, "PREINIT_ARRAY" },
+ { SHT_GROUP, "GROUP" },
+ { SHT_SYMTAB_SHNDX, "SYMTAB SECTION INDICIES" },
+ { SHT_GNU_verdef, "VERDEF" },
+ { SHT_GNU_verneed, "VERNEED" },
+ { SHT_GNU_versym, "VERSYM" },
+ { SHT_GNU_LIBLIST, "LIBLIST" },
+ { SHT_GNU_HASH, "GNU_HASH" },
+ { 0, NULL }
+ };
+
+ if (elf64)
+ printf (" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n");
+ else
+ printf (" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n");
+ for (i = 0; i < ehdr->e_shnum; ++i)
+ {
+ p = NULL;
+ for (j = 0; types[j].type_name; ++j)
+ if (types[j].sh_type == shdr[i].sh_type)
+ {
+ p = types[j].type_name;
+ break;
+ }
+
+ if (p == NULL)
+ {
+ if (shdr[i].sh_type >= SHT_LOPROC && shdr[i].sh_type <= SHT_HIPROC)
+ sprintf (buf, "LOPROC+%x", shdr[i].sh_type - SHT_LOPROC);
+ else if (shdr[i].sh_type >= SHT_LOOS && shdr[i].sh_type <= SHT_HIOS)
+ sprintf (buf, "LOOS+%x", shdr[i].sh_type - SHT_LOOS);
+ else if (shdr[i].sh_type >= SHT_LOUSER && shdr[i].sh_type <= SHT_HIUSER)
+ sprintf (buf, "LOUSER+%x", shdr[i].sh_type - SHT_LOUSER);
+ else
+ sprintf (buf, "Unknown: %x", shdr[i].sh_type);
+ p = buf;
+ }
+
+ printf (" [%2d] %-17.17s %-15.15s ", i,
+ strptr (dso, ehdr->e_shstrndx, shdr[i].sh_name), p);
+
+ q = buf;
+ shf = shdr[i].sh_flags;
+ while (shf)
+ {
+ flag = shf & -shf;
+ shf &= ~flag;
+ switch (flag)
+ {
+ case SHF_WRITE: *q++ = 'W'; break;
+ case SHF_ALLOC: *q++ = 'A'; break;
+ case SHF_EXECINSTR: *q++ = 'X'; break;
+ case SHF_MERGE: *q++ = 'M'; break;
+ case SHF_STRINGS: *q++ = 'S'; break;
+ case SHF_INFO_LINK: *q++ = 'I'; break;
+ case SHF_LINK_ORDER: *q++ = 'L'; break;
+ case SHF_OS_NONCONFORMING: *q++ = 'O'; break;
+ case SHF_TLS: *q++ = 'T'; break;
+ default:
+ if (flag & SHF_MASKOS)
+ *q++ = 'o', shf &= ~SHF_MASKOS;
+ else if (flag & SHF_MASKPROC)
+ *q++ = 'p', shf &= ~SHF_MASKPROC;
+ else
+ *q++ = 'x';
+ break;
+ }
+ }
+ *q = '\0';
+ if (elf64)
+ printf (" %16.16llx %6.6llx %6.6llx %2.2lx %3s %2ld %3lx %2ld\n",
+ (long long) shdr[i].sh_addr, (long long) shdr[i].sh_offset,
+ (long long) shdr[i].sh_size, (long) shdr[i].sh_entsize,
+ buf, (long) shdr[i].sh_link, (long) shdr[i].sh_info,
+ (long) shdr[i].sh_addralign);
+ else
+ printf (" %8.8lx %6.6lx %6.6lx %2.2lx %3s %2ld %3lx %2ld\n",
+ (long) shdr[i].sh_addr, (long) shdr[i].sh_offset,
+ (long) shdr[i].sh_size, (long) shdr[i].sh_entsize,
+ buf, (long) shdr[i].sh_link, (long) shdr[i].sh_info,
+ (long) shdr[i].sh_addralign);
+ }
+}
+#endif
+
+void
+insert_readonly_section (GElf_Ehdr *ehdr, GElf_Shdr *shdr, int n,
+ struct readonly_adjust *adjust)
+{
+ int i;
+
+ memmove (&shdr[n + 1], &shdr[n],
+ (ehdr->e_shnum - n) * sizeof (GElf_Shdr));
+ ++ehdr->e_shnum;
+ for (i = 0; i < adjust->newcount; ++i)
+ if (adjust->new[i] >= n)
+ ++adjust->new[i];
+}
+
+int
+remove_readonly_section (GElf_Ehdr *ehdr, GElf_Shdr *shdr, int n,
+ struct readonly_adjust *adjust)
+{
+ int i, ret = -1;
+
+ memmove (&shdr[n], &shdr[n + 1],
+ (ehdr->e_shnum - n) * sizeof (GElf_Shdr));
+ --ehdr->e_shnum;
+ for (i = 0; i < adjust->newcount; ++i)
+ if (adjust->new[i] > n)
+ --adjust->new[i];
+ else if (adjust->new[i] == n)
+ {
+ adjust->new[i] = -1;
+ ret = i;
+ }
+
+ return ret;
+}
+
+static inline int
+readonly_is_movable (DSO *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int k)
+{
+ if (! (shdr[k].sh_flags & (SHF_ALLOC | SHF_WRITE)))
+ return 0;
+
+ switch (shdr[k].sh_type)
+ {
+ case SHT_HASH:
+ case SHT_GNU_HASH:
+ case SHT_DYNSYM:
+ case SHT_REL:
+ case SHT_RELA:
+ case SHT_STRTAB:
+ case SHT_NOTE:
+ case SHT_GNU_verdef:
+ case SHT_GNU_verneed:
+ case SHT_GNU_versym:
+ case SHT_GNU_LIBLIST:
+ return 1;
+ default:
+ if (strcmp (strptr (dso, ehdr->e_shstrndx,
+ shdr[k].sh_name), ".interp") == 0)
+ return 1;
+ return 0;
+ }
+}
+
+int
+find_readonly_space (DSO *dso, GElf_Shdr *add, GElf_Ehdr *ehdr,
+ GElf_Phdr *phdr, GElf_Shdr *shdr,
+ struct readonly_adjust *adjust)
+{
+ int i, j;
+ GElf_Addr addr;
+ GElf_Off p_filesz;
+
+ if (add->sh_addr)
+ {
+ /* Prefer the current address if possible. */
+ for (i = 0; i < ehdr->e_phnum; ++i)
+ if (phdr[i].p_type == PT_LOAD
+ && (phdr[i].p_flags & (PF_R | PF_W)) == PF_R
+ && phdr[i].p_vaddr <= add->sh_addr
+ && phdr[i].p_vaddr + phdr[i].p_filesz
+ >= add->sh_addr + add->sh_size)
+ break;
+
+ if (i < ehdr->e_phnum)
+ for (j = 1; j < ehdr->e_shnum; ++j)
+ if ((shdr[j].sh_flags & SHF_ALLOC)
+ && shdr[j].sh_addr >= add->sh_addr)
+ {
+ if (shdr[j].sh_addr >= add->sh_addr + add->sh_size
+ && shdr[j - 1].sh_addr + shdr[j - 1].sh_size <= add->sh_addr)
+ {
+ insert_readonly_section (ehdr, shdr, j, adjust);
+ shdr[j] = *add;
+ shdr[j].sh_offset = (shdr[j].sh_addr - phdr[i].p_vaddr)
+ + phdr[i].p_offset;
+ return j;
+ }
+ break;
+ }
+ }
+
+ for (i = 0; i < ehdr->e_phnum; ++i)
+ if (phdr[i].p_type == PT_LOAD
+ && (phdr[i].p_flags & (PF_R | PF_W)) == PF_R)
+ {
+ GElf_Addr start = phdr[i].p_vaddr;
+ int after = -1, min;
+
+ if (phdr[i].p_offset < ehdr->e_phoff)
+ {
+ for (j = 0; j < ehdr->e_phnum; j++)
+ if (phdr[j].p_type == PT_PHDR
+ && phdr[j].p_offset == ehdr->e_phoff
+ && phdr[j].p_filesz >= ehdr->e_phnum * ehdr->e_phentsize)
+ break;
+ start += ehdr->e_phoff;
+ if (j < ehdr->e_phnum)
+ start += phdr[j].p_filesz;
+ else
+ start += ehdr->e_phnum * ehdr->e_phentsize;
+ start -= phdr[i].p_offset;
+ }
+ start = (start + add->sh_addralign - 1) & ~(add->sh_addralign - 1);
+ for (j = 1; j < ehdr->e_shnum; ++j)
+ if ((shdr[j].sh_flags & SHF_ALLOC)
+ && shdr[j].sh_addr >= phdr[i].p_vaddr
+ && shdr[j].sh_addr + shdr[j].sh_size
+ <= phdr[i].p_vaddr + phdr[i].p_filesz)
+ {
+ if (after == -1)
+ after = j - 1;
+ if (start + add->sh_size > shdr[j].sh_addr)
+ {
+ start = shdr[j].sh_addr + shdr[j].sh_size;
+ start = (start + add->sh_addralign - 1)
+ & ~(add->sh_addralign - 1);
+ after = j;
+ }
+ }
+
+ min = -1;
+ for (j = i + 1; j < ehdr->e_phnum; ++j)
+ if (phdr[j].p_offset >= phdr[i].p_offset + phdr[i].p_filesz
+ && (min == -1 || phdr[min].p_offset > phdr[j].p_offset))
+ min = j;
+
+ if (after != -1
+ && (start + add->sh_size <= phdr[i].p_vaddr + phdr[i].p_filesz
+ || (phdr[i].p_filesz == phdr[i].p_memsz
+ && (min == -1
+ || start + add->sh_size - phdr[i].p_vaddr
+ <= phdr[min].p_offset))))
+ {
+ insert_readonly_section (ehdr, shdr, after + 1, adjust);
+ shdr[after + 1] = *add;
+ shdr[after + 1].sh_addr = start;
+ shdr[after + 1].sh_offset = (start - phdr[i].p_vaddr)
+ + phdr[i].p_offset;
+ if (start + add->sh_size > phdr[i].p_vaddr + phdr[i].p_filesz)
+ {
+ adjust_nonalloc (dso, ehdr, shdr, 0, 0,
+ start + add->sh_size - phdr[i].p_vaddr
+ - phdr[i].p_filesz);
+ phdr[i].p_filesz = start + add->sh_size - phdr[i].p_vaddr;
+ phdr[i].p_memsz = phdr[i].p_filesz;
+ }
+ return after + 1;
+ }
+ }
+
+ /* If SHT_NOBITS sections are small, just extend the last PT_LOAD
+ segment. Small enough here means that the whole .bss fits into
+ the same CPU page as the alloced part of it. */
+ for (i = -1, j = 0; j < ehdr->e_phnum; ++j)
+ if (phdr[j].p_type == PT_LOAD)
+ i = j;
+ p_filesz = phdr[i].p_filesz;
+
+ /* If we'll be converting NOBITS .plt to PROGBITS, account for that in the
+ calculation. */
+ for (j = 1; j < ehdr->e_shnum; ++j)
+ {
+ if (shdr[j].sh_type == SHT_NOBITS
+ && shdr[j].sh_addr >= phdr[i].p_vaddr
+ && shdr[j].sh_addr + shdr[j].sh_size
+ <= phdr[i].p_vaddr + phdr[i].p_memsz
+ && !strcmp (strptr (dso, ehdr->e_shstrndx, shdr[j].sh_name), ".plt"))
+ {
+ if (shdr[j].sh_addr + shdr[j].sh_size - phdr[i].p_vaddr > p_filesz)
+ p_filesz = shdr[j].sh_addr + shdr[j].sh_size - phdr[i].p_vaddr;
+ break;
+ }
+ }
+
+ if (phdr[i].p_filesz
+ && p_filesz <= phdr[i].p_memsz
+ && !(((phdr[i].p_vaddr + phdr[i].p_memsz - 1)
+ ^ (phdr[i].p_vaddr + p_filesz - 1)) & ~(dso->arch->page_size - 1)))
+ {
+ for (j = 1; j < ehdr->e_shnum; ++j)
+ {
+ if (!(shdr[j].sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_ALLOC)))
+ break;
+ if (shdr[j].sh_type == SHT_NOBITS
+ && (shdr[j].sh_flags & SHF_TLS) == 0
+ && shdr[j].sh_addr >= phdr[i].p_vaddr)
+ shdr[j].sh_type = SHT_PROGBITS;
+ }
+
+ insert_readonly_section (ehdr, shdr, j, adjust);
+ shdr[j] = *add;
+ shdr[j].sh_addr = (shdr[j - 1].sh_addr + shdr[j - 1].sh_size
+ + add->sh_addralign - 1) & ~(add->sh_addralign - 1);
+ shdr[j].sh_offset = (shdr[j].sh_addr - phdr[i].p_vaddr)
+ + phdr[i].p_offset;
+ phdr[i].p_filesz = shdr[j].sh_addr + add->sh_size - phdr[i].p_vaddr;
+ phdr[i].p_memsz = phdr[i].p_filesz;
+ adjust_nonalloc (dso, ehdr, shdr, 0, 0, phdr[i].p_offset
+ + phdr[i].p_filesz - shdr[j + 1].sh_offset);
+ return j;
+ }
+
+ /* See if we can decrease binary's base VMA and thus gain space.
+ This trick is mainly useful for IA-32. */
+ for (i = 0; i < ehdr->e_phnum; ++i)
+ if (phdr[i].p_type == PT_LOAD)
+ break;
+
+ addr = (add->sh_size + add->sh_addralign - 1 + phdr[i].p_align - 1)
+ & ~(phdr[i].p_align - 1);
+ if (phdr[i].p_align <= dso->arch->page_size
+ && phdr[i].p_flags == (PF_R | PF_X)
+ && phdr[i].p_filesz == phdr[i].p_memsz
+ && phdr[i].p_vaddr - addr
+ && ! (((phdr[i].p_vaddr - addr) ^ phdr[i].p_vaddr)
+ & ~(phdr[i].p_align * 256 - 1)))
+ {
+ int moveend;
+ if (! adjust->basemove_end)
+ {
+ for (moveend = 1; moveend < ehdr->e_shnum; ++moveend)
+ if (strcmp (strptr (dso, ehdr->e_shstrndx,
+ shdr[moveend].sh_name), ".interp")
+ && shdr[moveend].sh_type != SHT_NOTE)
+ break;
+ if (moveend < ehdr->e_shnum && moveend > 1)
+ {
+ adjust->basemove_end = shdr[moveend].sh_addr;
+ adjust->moveend = moveend;
+ }
+ }
+ else
+ moveend = adjust->moveend;
+ if (moveend < ehdr->e_shnum && moveend > 1
+ && (shdr[moveend].sh_flags & (SHF_ALLOC | SHF_WRITE)))
+ {
+ int k = moveend;
+ GElf_Addr adj = addr;
+
+ if (add->sh_addr && ! adjust->move2
+ && phdr[i].p_vaddr <= add->sh_addr
+ && phdr[i].p_vaddr + phdr[i].p_filesz > add->sh_addr)
+ {
+ for (k = moveend; k < ehdr->e_shnum; ++k)
+ {
+ if (! (shdr[k].sh_flags & (SHF_ALLOC | SHF_WRITE)))
+ {
+ k = ehdr->e_shnum;
+ break;
+ }
+
+ if (shdr[k].sh_addr > add->sh_addr)
+ {
+ /* Don't allow inserting in between reloc sections
+ if they are adjacent. */
+ if (shdr[k].sh_type != SHT_REL
+ && shdr[k].sh_type != SHT_RELA)
+ break;
+ if (shdr[k - 1].sh_type != SHT_REL
+ && shdr[k - 1].sh_type != SHT_RELA)
+ break;
+ if (shdr[k - 1].sh_addr + shdr[k - 1].sh_size
+ != shdr[k].sh_addr)
+ break;
+ }
+
+ if (! readonly_is_movable (dso, ehdr, shdr, k))
+ {
+ k = ehdr->e_shnum;
+ break;
+ }
+ }
+
+ if (k < ehdr->e_shnum)
+ {
+ GElf_Addr a;
+
+ a = shdr[k].sh_addr;
+ a -= shdr[k - 1].sh_addr + shdr[k - 1].sh_size;
+ assert (add->sh_addralign <= phdr[i].p_align);
+ assert (add->sh_size > a);
+ a = (add->sh_size - a + phdr[i].p_align - 1)
+ & ~(phdr[i].p_align - 1);
+ if (a < adj)
+ {
+ adjust->move2 = 1;
+ adj = a;
+ }
+ else
+ k = moveend;
+ }
+ else
+ k = moveend;
+ }
+
+ for (j = 1; j < k; ++j)
+ shdr[j].sh_addr -= adj;
+ phdr[i].p_vaddr -= adj;
+ phdr[i].p_paddr -= adj;
+ phdr[i].p_filesz += adj;
+ phdr[i].p_memsz += adj;
+ for (j = 0; j < ehdr->e_phnum; ++j)
+ {
+ if (j == i)
+ continue;
+ /* Leave STACK segment alone, it has p_vaddr == p_paddr == 0
+ and p_offset == p_filesz == p_memsz == 0. */
+ if (phdr[j].p_type == PT_GNU_STACK)
+ continue;
+ if (phdr[j].p_vaddr
+ < adjust->basemove_end - adjust->basemove_adjust)
+ {
+ phdr[j].p_vaddr -= adj;
+ phdr[j].p_paddr -= adj;
+ }
+ else
+ phdr[j].p_offset += adj;
+ }
+ adjust->basemove_adjust += adj;
+ insert_readonly_section (ehdr, shdr, k, adjust);
+ shdr[k] = *add;
+ if (k == moveend)
+ {
+ addr = shdr[k - 1].sh_addr + shdr[k - 1].sh_size;
+ addr = (addr + add->sh_addralign - 1) & ~(add->sh_addralign - 1);
+ }
+ else
+ {
+ addr = (shdr[k + 1].sh_addr - add->sh_size)
+ & ~(add->sh_addralign - 1);
+ }
+
+ shdr[k].sh_addr = addr;
+ shdr[k].sh_offset = (addr - phdr[i].p_vaddr) + phdr[i].p_offset;
+ adjust_nonalloc (dso, ehdr, shdr, 0, 0, adj);
+ return k;
+ }
+ }
+
+ /* We have to create new PT_LOAD if at all possible. */
+ addr = ehdr->e_phoff + (ehdr->e_phnum + 1) * ehdr->e_phentsize;
+ for (j = 1; j < ehdr->e_shnum; ++j)
+ {
+ if (addr > shdr[j].sh_offset)
+ {
+ GElf_Addr start, addstart, endaddr, *old_addr;
+ GElf_Addr minsize = ~(GElf_Addr) 0;
+ int movesec = -1, last, k, e;
+
+ if (ehdr->e_phoff < phdr[i].p_offset
+ || ehdr->e_phoff + (ehdr->e_phnum + 1) * ehdr->e_phentsize
+ > phdr[i].p_offset + phdr[i].p_filesz
+ || ! readonly_is_movable (dso, ehdr, shdr, j)
+ || shdr[j].sh_addr >= phdr[i].p_vaddr + phdr[i].p_filesz)
+ {
+ error (0, 0, "%s: No space in ELF segment table to add new ELF segment",
+ dso->filename);
+ return 0;
+ }
+
+ start = phdr[i].p_vaddr - phdr[i].p_offset + ehdr->e_phoff
+ + (ehdr->e_phnum + 1) * ehdr->e_phentsize;
+ for (last = 1; last < ehdr->e_shnum; ++last)
+ if (! readonly_is_movable (dso, ehdr, shdr, last)
+ || shdr[last].sh_addr >= phdr[i].p_vaddr + phdr[i].p_filesz)
+ break;
+ for (j = 1; j < last; ++j)
+ {
+ addstart = (start + add->sh_addralign - 1)
+ & ~(add->sh_addralign - 1);
+ start = (start + shdr[j].sh_addralign - 1)
+ & ~(shdr[j].sh_addralign - 1);
+ endaddr = -1;
+ if (j + 1 < ehdr->e_shnum)
+ endaddr = shdr[j + 1].sh_addr;
+ if (phdr[i].p_vaddr + phdr[i].p_filesz < endaddr)
+ endaddr = phdr[i].p_vaddr + phdr[i].p_filesz;
+
+ switch (shdr[j].sh_type)
+ {
+ case SHT_HASH:
+ case SHT_GNU_HASH:
+ case SHT_DYNSYM:
+ case SHT_STRTAB:
+ case SHT_GNU_verdef:
+ case SHT_GNU_verneed:
+ case SHT_GNU_versym:
+ case SHT_GNU_LIBLIST:
+ if (endaddr >= start
+ && endaddr - start < minsize)
+ {
+ minsize = endaddr - start;
+ movesec = j;
+ }
+ if (endaddr > addstart
+ && endaddr - addstart > add->sh_size
+ && endaddr - addstart - add->sh_size
+ < minsize)
+ {
+ minsize = endaddr - addstart - add->sh_size;
+ movesec = j;
+ }
+ break;
+ }
+
+ if (start + shdr[j].sh_size <= endaddr)
+ {
+ movesec = j + 1;
+ break;
+ }
+ start += shdr[j].sh_size;
+ }
+
+ if (movesec == -1)
+ {
+ error (0, 0, "%s: No space in ELF segment table to add new ELF segment",
+ dso->filename);
+ return 0;
+ }
+
+ start = phdr[i].p_vaddr - phdr[i].p_offset + ehdr->e_phoff
+ + (ehdr->e_phnum + 1) * ehdr->e_phentsize;
+ old_addr = (GElf_Addr *) alloca (movesec * sizeof (GElf_Addr));
+ for (k = 1; k < movesec; ++k)
+ {
+ start = (start + shdr[k].sh_addralign - 1)
+ & ~(shdr[k].sh_addralign - 1);
+ old_addr[k] = shdr[k].sh_addr;
+ shdr[k].sh_addr = start;
+ shdr[k].sh_offset = start + phdr[i].p_offset
+ - phdr[i].p_vaddr;
+ start += shdr[k].sh_size;
+ }
+
+ for (e = 0; e < ehdr->e_phnum; ++e)
+ if (phdr[e].p_type != PT_LOAD
+ && phdr[e].p_type != PT_GNU_STACK)
+ for (k = 1; k < movesec; ++k)
+ if (old_addr[k] == phdr[e].p_vaddr)
+ {
+ if (phdr[e].p_filesz != shdr[k].sh_size
+ || phdr[e].p_memsz != shdr[k].sh_size)
+ {
+ error (0, 0, "%s: Non-PT_LOAD segment spanning more than one section",
+ dso->filename);
+ return 0;
+ }
+ phdr[e].p_vaddr += shdr[k].sh_addr - old_addr[k];
+ phdr[e].p_paddr += shdr[k].sh_addr - old_addr[k];
+ phdr[e].p_offset += shdr[k].sh_addr - old_addr[k];
+ break;
+ }
+
+ if (j < last)
+ /* Now continue as if there was place for a new PT_LOAD
+ in ElfW(Phdr) table initially. */
+ break;
+ else
+ {
+ GElf_Shdr moveshdr;
+ int newidx, ret, movedidx, oldidx;
+
+ moveshdr = shdr[movesec];
+ newidx = remove_readonly_section (ehdr, shdr, movesec, adjust);
+ oldidx = adjust->move->new_to_old[movesec];
+ remove_section (adjust->move, movesec);
+ ret = find_readonly_space (dso, add, ehdr, phdr, shdr, adjust);
+ if (ret == 0)
+ return 0;
+ movedidx = find_readonly_space (dso, &moveshdr, ehdr, phdr,
+ shdr, adjust);
+ if (movedidx == 0)
+ return 0;
+ if (newidx != -1)
+ adjust->new[newidx] = movedidx;
+ add_section (adjust->move, movedidx);
+ if (oldidx != -1)
+ {
+ adjust->move->old_to_new[oldidx] = movedidx;
+ adjust->move->new_to_old[movedidx] = oldidx;
+ }
+ if (movedidx <= ret)
+ ++ret;
+ return ret;
+ }
+ }
+ }
+
+ for (i = 0, j = 0; i < ehdr->e_phnum; ++i)
+ if (phdr[i].p_type == PT_LOAD)
+ j = i;
+ else if (phdr[i].p_type == PT_PHDR)
+ {
+ if (phdr[i].p_filesz == ehdr->e_phnum * ehdr->e_phentsize)
+ phdr[i].p_filesz += ehdr->e_phentsize;
+ if (phdr[i].p_memsz == ehdr->e_phnum * ehdr->e_phentsize)
+ phdr[i].p_memsz += ehdr->e_phentsize;
+ }
+
+ memmove (&phdr[j + 2], &phdr[j + 1],
+ (ehdr->e_phnum - j - 1) * sizeof (GElf_Phdr));
+ ++ehdr->e_phnum;
+ phdr[++j].p_type = PT_LOAD;
+ phdr[j].p_offset = phdr[j - 1].p_offset + phdr[j - 1].p_filesz;
+ phdr[j].p_offset = (phdr[j].p_offset + add->sh_addralign - 1)
+ & ~(add->sh_addralign - 1);
+ phdr[j].p_align = phdr[j - 1].p_align;
+ phdr[j].p_vaddr = phdr[j - 1].p_vaddr + phdr[j - 1].p_memsz;
+ phdr[j].p_vaddr += (phdr[j].p_align - 1);
+ phdr[j].p_vaddr &= ~(phdr[j].p_align - 1);
+ phdr[j].p_vaddr += (phdr[j].p_offset & (phdr[j].p_align - 1));
+ phdr[j].p_paddr = phdr[j].p_vaddr;
+ /* Although the content of the segment is read-only, unless it ends on
+ a page boundary, we must make it writeable. This is because the rest of
+ the last page in the segment will be used as sbrk area which is assumed
+ to be writeable. */
+ phdr[j].p_flags = (PF_R | PF_W);
+ phdr[j].p_filesz = add->sh_size;
+ phdr[j].p_memsz = add->sh_size;
+ for (i = 1; i < ehdr->e_shnum; ++i)
+ if (! (shdr[i].sh_flags & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR)))
+ break;
+ assert (i < ehdr->e_shnum);
+ insert_readonly_section (ehdr, shdr, i, adjust);
+ shdr[i] = *add;
+ shdr[i].sh_addr = phdr[j].p_vaddr;
+ shdr[i].sh_offset = phdr[j].p_offset;
+ adjust_nonalloc (dso, ehdr, shdr, 0, 0,
+ phdr[j].p_offset + phdr[j].p_filesz - phdr[j - 1].p_offset
+ - phdr[j - 1].p_filesz);
+ return i;
+}
diff --git a/trunk/src/space.h b/trunk/src/space.h
new file mode 100644
index 0000000..5c9f725
--- /dev/null
+++ b/trunk/src/space.h
@@ -0,0 +1,39 @@
+/* Copyright (C) 2001, 2004 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. */
+
+#ifndef SPACE_H
+#define SPACE_H
+
+struct readonly_adjust
+{
+ off_t basemove_adjust;
+ GElf_Addr basemove_end;
+ int moveend;
+ int move2;
+ int newcount, *new;
+ struct section_move *move;
+};
+
+void insert_readonly_section (GElf_Ehdr *ehdr, GElf_Shdr *shdr, int n,
+ struct readonly_adjust *adjust);
+int remove_readonly_section (GElf_Ehdr *ehdr, GElf_Shdr *shdr, int n,
+ struct readonly_adjust *adjust);
+int find_readonly_space (DSO *dso, GElf_Shdr *add, GElf_Ehdr *ehdr,
+ GElf_Phdr *phdr, GElf_Shdr *shdr,
+ struct readonly_adjust *adjust);
+
+#endif /* SPACE_H */
diff --git a/trunk/src/stabs.c b/trunk/src/stabs.c
new file mode 100644
index 0000000..c0a5a6a
--- /dev/null
+++ b/trunk/src/stabs.c
@@ -0,0 +1,188 @@
+/* Copyright (C) 2001, 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 <byteswap.h>
+#include <endian.h>
+#include <error.h>
+
+#include "prelink.h"
+
+#define N_ZERO 0x00
+#define N_GSYM 0x20
+#define N_FNAME 0x22
+#define N_FUN 0x24
+#define N_STSYM 0x26
+#define N_LCSYM 0x28
+#define N_MAIN 0x2a
+#define N_BNSYM 0x2e
+#define N_PC 0x30
+#define N_NSYMS 0x32
+#define N_NOMAP 0x34
+#define N_OBJ 0x38
+#define N_OPT 0x3c
+#define N_RSYM 0x40
+#define N_M2C 0x42
+#define N_SLINE 0x44
+#define N_DSLINE 0x46
+#define N_BSLINE 0x48
+#define N_BROWS 0x48
+#define N_DEFD 0x4a
+#define N_ENSYM 0x4e
+#define N_EHDECL 0x50
+#define N_MOD2 0x50
+#define N_CATCH 0x54
+#define N_SSYM 0x60
+#define N_SO 0x64
+#define N_LSYM 0x80
+#define N_BINCL 0x82
+#define N_SOL 0x84
+#define N_PSYM 0xa0
+#define N_EINCL 0xa2
+#define N_ENTRY 0xa4
+#define N_LBRAC 0xc0
+#define N_EXCL 0xc2
+#define N_SCOPE 0xc4
+#define N_RBRAC 0xe0
+#define N_BCOMM 0xe2
+#define N_ECOMM 0xe4
+#define N_ECOML 0xe8
+#define N_LENG 0xfe
+
+static uint32_t
+read_native (char *p)
+{
+ return *(uint32_t *)p;
+}
+
+static uint32_t
+read_swap (char *p)
+{
+ return bswap_32 (*(uint32_t *)p);
+}
+
+static void
+write_native (char *p, uint32_t v)
+{
+ *(uint32_t *)p = v;
+}
+
+static void
+write_swap (char *p, uint32_t v)
+{
+ *(uint32_t *)p = bswap_32 (v);
+}
+
+int
+adjust_stabs (DSO *dso, int n, GElf_Addr start, GElf_Addr adjust)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ off_t off;
+ uint32_t (*read_32) (char *p);
+ void (*write_32) (char *p, uint32_t v);
+ uint32_t value;
+ int sec, type;
+
+ assert (dso->shdr[n].sh_entsize == 12);
+ data = elf_getdata (scn, NULL);
+ assert (data != NULL && data->d_buf != NULL);
+ assert (elf_getdata (scn, data) == NULL);
+ assert (data->d_off == 0 && data->d_size == dso->shdr[n].sh_size);
+#if __BYTE_ORDER == __BIG_ENDIAN
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+#else
+# error Not supported host endianess
+#endif
+ {
+ read_32 = read_native;
+ write_32 = write_native;
+ }
+#if __BYTE_ORDER == __BIG_ENDIAN
+ else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ else if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
+#endif
+ {
+ read_32 = read_swap;
+ write_32 = write_swap;
+ }
+ else
+ {
+ error (0, 0, "%s: Wrong ELF data enconding", dso->filename);
+ return 1;
+ }
+
+ for (off = 0; off < data->d_size; off += 12)
+ {
+ switch ((type = *(uint8_t *)(data->d_buf + off + 4)))
+ {
+ case N_FUN:
+ /* If string is "", N_FUN is function length, otherwise
+ it is function start address. */
+ if (read_32 (data->d_buf + off) == 0)
+ break;
+ /* FALLTHROUGH */
+ case N_STSYM:
+ case N_LCSYM:
+ case N_CATCH:
+ case N_SO:
+ case N_SOL:
+ case N_BNSYM:
+ case N_ENSYM:
+ value = read_32 (data->d_buf + off + 8);
+ sec = addr_to_sec (dso, value);
+ if (sec != -1)
+ {
+ addr_adjust (value, start, adjust);
+ write_32 (data->d_buf + off + 8, value);
+ }
+ break;
+ /* These should be always 0. */
+ case N_GSYM:
+ case N_BINCL:
+ case N_EINCL:
+ case N_EXCL:
+ case N_BCOMM:
+ case N_ECOMM:
+ /* These contain other values. */
+ case N_ZERO:
+ case N_NSYMS:
+ case N_NOMAP:
+ case N_RSYM:
+ case N_LSYM:
+ case N_PSYM:
+ case N_OPT:
+ /* These are relative. */
+ case N_LBRAC:
+ case N_RBRAC:
+ case N_SLINE:
+ case N_BSLINE:
+ case N_DSLINE:
+ break;
+ default:
+ error (0, 0, "%s: Unknown stabs code 0x%02x\n", dso->filename, type);
+ return 1;
+ }
+ }
+
+ elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
+ return 0;
+}
diff --git a/trunk/src/undo.c b/trunk/src/undo.c
new file mode 100644
index 0000000..f9b0bd3
--- /dev/null
+++ b/trunk/src/undo.c
@@ -0,0 +1,711 @@
+/* Copyright (C) 2001, 2002, 2003, 2005 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 <endian.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelink.h"
+#include "reloc.h"
+
+static int
+undo_prelink_rel (DSO *dso, int n)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rel rel;
+ int sec;
+
+ if (dso->arch->undo_prelink_rel == NULL)
+ return 0;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrel (dso->elf, data, ndx, &rel);
+ sec = addr_to_sec (dso, rel.r_offset);
+ if (sec == -1)
+ continue;
+
+ switch (dso->arch->undo_prelink_rel (dso, &rel, addr))
+ {
+ case 2:
+ gelfx_update_rel (dso->elf, data, ndx, &rel);
+ break;
+ case 0:
+ break;
+ default:
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+undo_prelink_rela (DSO *dso, int n)
+{
+ Elf_Data *data = NULL;
+ Elf_Scn *scn = dso->scn[n];
+ GElf_Rela rela;
+ int sec;
+
+ if (dso->arch->undo_prelink_rela == NULL)
+ return 0;
+ while ((data = elf_getdata (scn, data)) != NULL)
+ {
+ int ndx, maxndx;
+ GElf_Addr addr = dso->shdr[n].sh_addr + data->d_off;
+
+ maxndx = data->d_size / dso->shdr[n].sh_entsize;
+ for (ndx = 0; ndx < maxndx;
+ ++ndx, addr += dso->shdr[n].sh_entsize)
+ {
+ gelfx_getrela (dso->elf, data, ndx, &rela);
+ sec = addr_to_sec (dso, rela.r_offset);
+ if (sec == -1)
+ continue;
+
+ switch (dso->arch->undo_prelink_rela (dso, &rela, addr))
+ {
+ case 2:
+ gelfx_update_rela (dso->elf, data, ndx, &rela);
+ break;
+ case 0:
+ break;
+ default:
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+static int
+remove_dynamic_prelink_tags (DSO *dso)
+{
+ Elf_Data *data;
+ Elf_Scn *scn;
+ GElf_Dyn dyn;
+ int ndx;
+
+ 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);
+ ndx = data->d_size / dso->shdr[dso->dynamic].sh_entsize;
+ while (--ndx >= 0)
+ {
+ gelfx_getdyn (dso->elf, data, ndx, &dyn);
+ switch (dyn.d_tag)
+ {
+ case DT_NULL:
+ continue;
+ case DT_CHECKSUM:
+ case DT_GNU_PRELINKED:
+ case DT_GNU_LIBLIST:
+ case DT_GNU_LIBLISTSZ:
+ case DT_GNU_CONFLICT:
+ case DT_GNU_CONFLICTSZ:
+ dyn.d_tag = DT_NULL;
+ dyn.d_un.d_val = 0;
+ gelfx_update_dyn (dso->elf, data, ndx, &dyn);
+ elf_flagscn (scn, ELF_C_SET, ELF_F_DIRTY);
+ break;
+ default:
+ ndx = 0;
+ break;
+ }
+ }
+ return 0;
+}
+
+int
+undo_sections (DSO *dso, int undo, struct section_move *move,
+ struct reloc_info *rinfo, GElf_Ehdr *ehdr,
+ GElf_Phdr *phdr, GElf_Shdr *shdr)
+{
+ Elf_Data src, dst, *d;
+ Elf_Scn *scn;
+ int i, j;
+
+ scn = dso->scn[undo];
+ d = elf_getdata (scn, NULL);
+ assert (d != NULL && elf_getdata (scn, d) == NULL);
+
+ src = *d;
+ src.d_type = ELF_T_EHDR;
+ src.d_align = dso->shdr[undo].sh_addralign;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_EHDR, 1, EV_CURRENT);
+ dst = src;
+ if (src.d_size > d->d_size)
+ {
+ error (0, 0, "%s: .gnu.prelink_undo section too small",
+ dso->filename);
+ return 1;
+ }
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ dst.d_buf = alloca (dst.d_size);
+ break;
+ case ELFCLASS64:
+ dst.d_buf = ehdr;
+ break;
+ default:
+ return 1;
+ }
+ if (gelf_xlatetom (dso->elf, &dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Could not read .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+ if (gelf_getclass (dso->elf) == ELFCLASS32)
+ {
+ Elf32_Ehdr *ehdr32 = (Elf32_Ehdr *) dst.d_buf;
+
+ memcpy (ehdr->e_ident, ehdr32->e_ident, sizeof (ehdr->e_ident));
+#define COPY(name) ehdr->name = ehdr32->name
+ COPY (e_type);
+ COPY (e_machine);
+ COPY (e_version);
+ COPY (e_entry);
+ COPY (e_phoff);
+ COPY (e_shoff);
+ COPY (e_flags);
+ COPY (e_ehsize);
+ COPY (e_phentsize);
+ COPY (e_phnum);
+ COPY (e_shentsize);
+ COPY (e_shnum);
+ COPY (e_shstrndx);
+#undef COPY
+ }
+
+ if (memcmp (ehdr->e_ident, dso->ehdr.e_ident, sizeof (ehdr->e_ident))
+ || ehdr->e_type != dso->ehdr.e_type
+ || ehdr->e_machine != dso->ehdr.e_machine
+ || ehdr->e_version != dso->ehdr.e_version
+ || ehdr->e_flags != dso->ehdr.e_flags
+ || ehdr->e_ehsize != dso->ehdr.e_ehsize
+ || ehdr->e_phentsize != dso->ehdr.e_phentsize
+ || ehdr->e_shentsize != dso->ehdr.e_shentsize)
+ {
+ error (0, 0, "%s: ELF headers changed since prelinking",
+ dso->filename);
+ return 1;
+ }
+
+ if (ehdr->e_phnum > dso->ehdr.e_phnum)
+ {
+ error (0, 0, "%s: Number of program headers is less than before prelinking",
+ dso->filename);
+ return 1;
+ }
+
+ if (d->d_size != (src.d_size
+ + gelf_fsize (dso->elf, ELF_T_PHDR, ehdr->e_phnum,
+ EV_CURRENT)
+ + gelf_fsize (dso->elf, ELF_T_SHDR, ehdr->e_shnum - 1,
+ EV_CURRENT)))
+ {
+ error (0, 0, "%s: Incorrect size of .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+
+ src.d_type = ELF_T_PHDR;
+ src.d_buf += src.d_size;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_PHDR, ehdr->e_phnum, EV_CURRENT);
+ dst = src;
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ dst.d_buf = alloca (dst.d_size);
+ break;
+ case ELFCLASS64:
+ dst.d_buf = phdr;
+ break;
+ }
+ if (gelf_xlatetom (dso->elf, &dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Could not read .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+
+ if (gelf_getclass (dso->elf) == ELFCLASS32)
+ {
+ Elf32_Phdr *phdr32 = (Elf32_Phdr *) dst.d_buf;
+
+ for (i = 0; i < ehdr->e_phnum; ++i)
+ {
+#define COPY(name) phdr[i].name = phdr32[i].name
+ COPY(p_type);
+ COPY(p_flags);
+ COPY(p_offset);
+ COPY(p_vaddr);
+ COPY(p_paddr);
+ COPY(p_filesz);
+ COPY(p_memsz);
+ COPY(p_align);
+#undef COPY
+ }
+ }
+
+ memset (shdr, 0, sizeof (GElf_Shdr));
+ src.d_type = ELF_T_SHDR;
+ src.d_buf += src.d_size;
+ src.d_size = gelf_fsize (dso->elf, ELF_T_SHDR, ehdr->e_shnum - 1, EV_CURRENT);
+ dst = src;
+ switch (gelf_getclass (dso->elf))
+ {
+ case ELFCLASS32:
+ dst.d_buf = alloca (dst.d_size);
+ break;
+ case ELFCLASS64:
+ dst.d_buf = shdr + 1;
+ break;
+ default:
+ return 1;
+ }
+ if (gelf_xlatetom (dso->elf, &dst, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
+ {
+ error (0, 0, "%s: Could not read .gnu.prelink_undo section",
+ dso->filename);
+ return 1;
+ }
+
+ if (gelf_getclass (dso->elf) == ELFCLASS32)
+ {
+ Elf32_Shdr *shdr32 = (Elf32_Shdr *) dst.d_buf;
+
+ for (i = 1; i < ehdr->e_shnum; ++i)
+ {
+#define COPY(name) shdr[i].name = shdr32[i - 1].name
+ COPY (sh_name);
+ COPY (sh_type);
+ COPY (sh_flags);
+ COPY (sh_addr);
+ COPY (sh_offset);
+ COPY (sh_size);
+ COPY (sh_link);
+ COPY (sh_info);
+ COPY (sh_addralign);
+ COPY (sh_entsize);
+#undef COPY
+ }
+ }
+
+ move->new_shnum = ehdr->e_shnum;
+ for (i = 1; i < move->old_shnum; ++i)
+ move->old_to_new[i] = -1;
+ for (i = 1; i < move->new_shnum; ++i)
+ move->new_to_old[i] = -1;
+
+ for (i = 1; i < move->old_shnum; ++i)
+ {
+ for (j = 1; j < move->new_shnum; ++j)
+ if (dso->shdr[i].sh_name == shdr[j].sh_name
+ && dso->shdr[i].sh_type == shdr[j].sh_type
+ && dso->shdr[i].sh_flags == shdr[j].sh_flags
+ && dso->shdr[i].sh_addralign == shdr[j].sh_addralign
+ && dso->shdr[i].sh_entsize == shdr[j].sh_entsize
+ && dso->shdr[i].sh_size == shdr[j].sh_size
+ && move->new_to_old[j] == -1)
+ break;
+
+ if (j == move->new_shnum)
+ continue;
+
+ move->old_to_new[i] = j;
+ move->new_to_old[j] = i;
+ }
+
+ for (i = 1; i < move->old_shnum; ++i)
+ if (move->old_to_new[i] == -1)
+ {
+ const char *name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name);
+
+ if (! strcmp (name, ".gnu.prelink_undo")
+ || ! strcmp (name, ".gnu.conflict")
+ || ! strcmp (name, ".gnu.liblist")
+ || ! strcmp (name, ".gnu.libstr")
+ || ((! strcmp (name, ".dynbss") || ! strcmp (name, ".sdynbss"))
+ && dso->ehdr.e_type == ET_EXEC))
+ continue;
+
+ if ((! strcmp (name, ".dynstr") && dso->ehdr.e_type == ET_EXEC)
+ || i == dso->ehdr.e_shstrndx)
+ {
+ for (j = 1; j < move->new_shnum; ++j)
+ if (dso->shdr[i].sh_name == shdr[j].sh_name
+ && dso->shdr[i].sh_type == shdr[j].sh_type
+ && dso->shdr[i].sh_flags == shdr[j].sh_flags
+ && dso->shdr[i].sh_addralign == shdr[j].sh_addralign
+ && dso->shdr[i].sh_entsize == shdr[j].sh_entsize
+ && dso->shdr[i].sh_size > shdr[j].sh_size
+ && move->new_to_old[j] == -1)
+ break;
+
+ if (j < move->new_shnum)
+ {
+ move->old_to_new[i] = j;
+ move->new_to_old[j] = i;
+ continue;
+ }
+ }
+
+ if (((i >= rinfo->first && i <= rinfo->last) || i == rinfo->plt)
+ && dso->shdr[i].sh_type == SHT_RELA)
+ {
+ for (j = 1; j < move->new_shnum; ++j)
+ if (dso->shdr[i].sh_name == shdr[j].sh_name
+ && shdr[j].sh_type == SHT_REL
+ && dso->shdr[i].sh_flags == shdr[j].sh_flags
+ && dso->shdr[i].sh_addralign == shdr[j].sh_addralign
+ && 2 * dso->shdr[i].sh_entsize == 3 * shdr[j].sh_entsize
+ && 2 * dso->shdr[i].sh_size == 3 * shdr[j].sh_size
+ && move->new_to_old[j] == -1)
+ break;
+
+ if (j < move->new_shnum)
+ {
+ move->old_to_new[i] = j;
+ move->new_to_old[j] = i;
+ continue;
+ }
+ }
+
+ if (! strcmp (name, ".bss")
+ || ! strcmp (name, ".sbss")
+ || (! strcmp (name, ".plt")
+ && dso->shdr[i].sh_type == SHT_PROGBITS))
+ {
+ int is_plt = ! strcmp (name, ".plt");
+
+ for (j = 1; j < move->new_shnum; ++j)
+ if (dso->shdr[i].sh_name == shdr[j].sh_name
+ && dso->shdr[i].sh_flags == shdr[j].sh_flags
+ && dso->shdr[i].sh_addralign == shdr[j].sh_addralign
+ && (is_plt || dso->shdr[i].sh_entsize == shdr[j].sh_entsize)
+ && move->new_to_old[j] == -1)
+ {
+ if (is_plt)
+ {
+ if (dso->shdr[i].sh_size != shdr[j].sh_size)
+ continue;
+ if (shdr[j].sh_type == SHT_NOBITS
+ && dso->shdr[i].sh_entsize == shdr[j].sh_entsize)
+ break;
+ /* On Alpha prelink fixes bogus sh_entsize of .plt
+ sections. */
+ if (shdr[j].sh_type == SHT_PROGBITS)
+ break;
+ }
+ else
+ {
+ const char *pname;
+
+ if (dso->shdr[i].sh_type != shdr[j].sh_type
+ && (dso->shdr[i].sh_type != SHT_PROGBITS
+ || shdr[j].sh_type != SHT_NOBITS))
+ continue;
+
+ if (dso->shdr[i].sh_size == shdr[j].sh_size)
+ break;
+
+ pname = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i - 1].sh_name);
+ if (strcmp (pname, ".dynbss")
+ && strcmp (pname, ".sdynbss"))
+ continue;
+
+ if (dso->shdr[i].sh_size + dso->shdr[i - 1].sh_size
+ == shdr[j].sh_size)
+ break;
+ }
+ }
+
+ if (j < move->new_shnum)
+ {
+ move->old_to_new[i] = j;
+ move->new_to_old[j] = i;
+ continue;
+ }
+ }
+
+ error (0, 0, "%s: Section %s created after prelinking",
+ dso->filename, name);
+ return 1;
+ }
+
+ for (i = 1; i < move->new_shnum; ++i)
+ if (move->new_to_old[i] == -1)
+ {
+ const char *name = strptr (dso, dso->ehdr.e_shstrndx, shdr[i].sh_name);
+
+ error (0, 0, "%s: Section %s removed after prelinking", dso->filename,
+ name);
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+prelink_undo (DSO *dso)
+{
+ GElf_Ehdr ehdr;
+ GElf_Shdr shdr[dso->ehdr.e_shnum + 20], old_shdr[dso->ehdr.e_shnum];
+ GElf_Phdr phdr[dso->ehdr.e_phnum];
+ Elf_Scn *scn;
+ Elf_Data *d;
+ int undo, i;
+ struct section_move *move;
+ struct reloc_info rinfo;
+
+ for (undo = 1; undo < dso->ehdr.e_shnum; ++undo)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[undo].sh_name),
+ ".gnu.prelink_undo"))
+ break;
+
+ if (undo == dso->ehdr.e_shnum)
+ {
+ if (undo_output)
+ return reopen_dso (dso, NULL, undo_output);
+ error (0, 0, "%s does not have .gnu.prelink_undo section", dso->filename);
+ return 1;
+ }
+
+ memcpy (old_shdr, dso->shdr, sizeof (GElf_Shdr) * dso->ehdr.e_shnum);
+ move = init_section_move (dso);
+ if (move == NULL)
+ return 1;
+
+ if (find_reloc_sections (dso, &rinfo))
+ goto error_out;
+
+ if (undo_sections (dso, undo, move, &rinfo, &ehdr, phdr, shdr))
+ goto error_out;
+
+ if (reopen_dso (dso, move, undo_output))
+ goto error_out;
+
+ if (find_reloc_sections (dso, &rinfo))
+ goto error_out;
+
+ for (i = 1; i < dso->ehdr.e_shnum; i++)
+ {
+ if (! (dso->shdr[i].sh_flags & SHF_ALLOC))
+ continue;
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name),
+ ".gnu.conflict"))
+ continue;
+ switch (dso->shdr[i].sh_type)
+ {
+ case SHT_REL:
+ if (undo_prelink_rel (dso, i))
+ goto error_out;
+ break;
+ case SHT_RELA:
+ if (undo_prelink_rela (dso, i))
+ goto error_out;
+ break;
+ }
+ }
+
+ if (dso->arch->arch_undo_prelink && dso->arch->arch_undo_prelink (dso))
+ goto error_out;
+
+ if (dso->ehdr.e_type == ET_DYN)
+ {
+ GElf_Addr adjust = 0, diff;
+
+ for (i = dso->ehdr.e_shnum - 1; i > 0; --i)
+ if (shdr[i].sh_flags & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR))
+ {
+ adjust = shdr[i].sh_addr - dso->shdr[i].sh_addr;
+ break;
+ }
+ while (i > 0)
+ {
+ int nsec = 1, j;
+ /* Change here PROGBITS .plt into NOBITS if needed. */
+
+ /* Convert RELA to REL if needed. */
+ if (dso->shdr[i].sh_type == SHT_RELA && shdr[i].sh_type == SHT_REL)
+ {
+ assert (dso->arch->rela_to_rel != NULL);
+ if (i == rinfo.plt)
+ {
+ if (convert_rela_to_rel (dso, i))
+ goto error_out;
+ dso->shdr[i].sh_size = shdr[i].sh_size;
+ }
+ else if (i == rinfo.last)
+ {
+ GElf_Addr start = dso->shdr[rinfo.first].sh_addr;
+
+ for (j = rinfo.first; j <= rinfo.last; ++j)
+ {
+ if (convert_rela_to_rel (dso, j))
+ goto error_out;
+ dso->shdr[j].sh_addr = start;
+ dso->shdr[j].sh_size = shdr[j].sh_size;
+ start += dso->shdr[j].sh_size;
+ }
+ nsec = rinfo.last - rinfo.first + 1;
+ i = rinfo.first;
+ }
+ else
+ {
+ error (0, 0, "%s: Cannot convert RELA to REL", dso->filename);
+ goto error_out;
+ }
+ }
+ diff = shdr[i].sh_addr - dso->shdr[i].sh_addr;
+ if (diff != adjust)
+ {
+ assert (diff >= adjust);
+ if (adjust_dso (dso, dso->shdr[i + nsec].sh_addr, adjust - diff))
+ goto error_out;
+ adjust = diff;
+ }
+ --i;
+ }
+ if (adjust && adjust_dso (dso, 0, adjust))
+ goto error_out;
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (shdr[i].sh_flags & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR))
+ assert (shdr[i].sh_addr == dso->shdr[i].sh_addr);
+ }
+ else
+ {
+ /* Executable. */
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ {
+ const char *name = strptr (dso, dso->ehdr.e_shstrndx,
+ dso->shdr[i].sh_name);
+
+ if (dso->shdr[i].sh_type == SHT_PROGBITS
+ && shdr[i].sh_type == SHT_NOBITS)
+ {
+ assert (strcmp (name, ".bss") == 0
+ || strcmp (name, ".sbss") == 0
+ || strcmp (name, ".plt") == 0);
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (d != NULL && elf_getdata (scn, d) == NULL);
+ assert (d->d_size == 0 || d->d_buf != NULL);
+ assert (d->d_size == dso->shdr[i].sh_size);
+ free (d->d_buf);
+ d->d_buf = NULL;
+ dso->shdr[i].sh_type = SHT_NOBITS;
+ }
+ else if (dso->shdr[i].sh_type == SHT_RELA
+ && shdr[i].sh_type == SHT_REL)
+ {
+ if (convert_rela_to_rel (dso, i))
+ goto error_out;
+ dso->shdr[i].sh_size = shdr[i].sh_size;
+ }
+ else
+ assert (dso->shdr[i].sh_type == shdr[i].sh_type);
+ if (dso->shdr[i].sh_size != shdr[i].sh_size)
+ {
+ /* This is handled in code below for both ET_DYN and ET_EXEC. */
+ if (i == dso->ehdr.e_shstrndx)
+ continue;
+ assert (shdr[i].sh_type == SHT_NOBITS
+ || shdr[i].sh_size < dso->shdr[i].sh_size);
+ assert (strcmp (name, ".dynstr") == 0
+ || strcmp (name, ".bss") == 0
+ || strcmp (name, ".sbss") == 0);
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (d != NULL && elf_getdata (scn, d) == NULL);
+ d->d_size = shdr[i].sh_size;
+ }
+ }
+
+ if (update_dynamic_tags (dso, shdr, old_shdr, move))
+ goto error_out;
+
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ if (shdr[i].sh_flags & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR))
+ dso->shdr[i].sh_addr = shdr[i].sh_addr;
+ }
+
+ /* Clear .dynamic entries added by prelink, update others. */
+ if (remove_dynamic_prelink_tags (dso)
+ || update_dynamic_rel (dso, &rinfo))
+ goto error_out;
+
+ /* Shrink .shstrtab. */
+ i = dso->ehdr.e_shstrndx;
+ if (shdr[i].sh_size < dso->shdr[i].sh_size)
+ {
+ scn = dso->scn[i];
+ d = elf_getdata (scn, NULL);
+ assert (d != NULL && elf_getdata (scn, d) == NULL);
+ assert (d->d_size == dso->shdr[i].sh_size);
+ d->d_size = shdr[i].sh_size;
+ }
+
+ /* Now restore the rest. */
+ for (i = 1; i < dso->ehdr.e_shnum; ++i)
+ dso->shdr[i] = shdr[i];
+ if (dso->ehdr.e_phnum != ehdr.e_phnum)
+ {
+ assert (ehdr.e_phnum < dso->ehdr.e_phnum);
+ if (gelf_newphdr (dso->elf, ehdr.e_phnum) == 0)
+ {
+ error (0, 0, "Could not create new ELF headers");
+ goto error_out;
+ }
+ }
+ for (i = 0; i < ehdr.e_phnum; ++i)
+ dso->phdr[i] = phdr[i];
+ dso->permissive = 1;
+ assert (dso->ehdr.e_entry == ehdr.e_entry);
+ assert (dso->ehdr.e_shnum == ehdr.e_shnum);
+ assert (dso->ehdr.e_shstrndx == ehdr.e_shstrndx);
+ dso->ehdr.e_phoff = ehdr.e_phoff;
+ dso->ehdr.e_shoff = ehdr.e_shoff;
+ dso->ehdr.e_phnum = ehdr.e_phnum;
+ free (move);
+ return 0;
+
+error_out:
+ free (move);
+ return 1;
+}
diff --git a/trunk/src/undoall.c b/trunk/src/undoall.c
new file mode 100644
index 0000000..90e9240
--- /dev/null
+++ b/trunk/src/undoall.c
@@ -0,0 +1,173 @@
+/* Copyright (C) 2002, 2005 Red Hat, Inc.
+ Written by Jakub Jelinek <jakub@redhat.com>, 2002.
+
+ 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 <alloca.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include "prelinktab.h"
+
+static int
+undo_one (void **p, void *info)
+{
+ struct prelink_entry *ent = * (struct prelink_entry **) p;
+ DSO *dso;
+ struct stat64 st;
+ struct prelink_link *hardlink;
+ char *move = NULL;
+ size_t movelen = 0;
+
+ if (ent->done != 2)
+ return 1;
+
+ if (ent->type != ET_DYN
+ && (ent->type != ET_EXEC || libs_only))
+ return 1;
+
+ dso = open_dso (ent->canon_filename);
+ if (dso == NULL)
+ goto error_out;
+
+ if (fstat64 (dso->fd, &st) < 0)
+ {
+ error (0, errno, "%s changed during prelinking", ent->filename);
+ goto error_out;
+ }
+
+ if (st.st_dev != ent->dev || st.st_ino != ent->ino)
+ {
+ error (0, 0, "%s changed during prelinking", ent->filename);
+ goto error_out;
+ }
+
+ if (verbose)
+ {
+ if (dry_run)
+ printf ("Would undo %s\n", ent->canon_filename);
+ else
+ printf ("Undoing %s\n", ent->canon_filename);
+ }
+
+ if (prelink_undo (dso))
+ goto error_out;
+
+ if (dry_run)
+ close_dso (dso);
+ else
+ {
+ if (update_dso (dso, NULL))
+ {
+ dso = NULL;
+ goto error_out;
+ }
+ }
+
+ dso = NULL;
+
+ /* Redo hardlinks. */
+ for (hardlink = ent->hardlink; hardlink; hardlink = hardlink->next)
+ {
+ size_t len;
+
+ if (wrap_lstat64 (hardlink->canon_filename, &st) < 0)
+ {
+ error (0, 0, "Could not stat %s (former hardlink to %s)",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (st.st_dev != ent->dev || st.st_ino != ent->ino)
+ {
+ error (0, 0, "%s is no longer hardlink to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (verbose)
+ {
+ if (dry_run)
+ printf ("Would link %s to %s\n", hardlink->canon_filename,
+ ent->canon_filename);
+ else
+ printf ("Linking %s to %s\n", hardlink->canon_filename,
+ ent->canon_filename);
+ }
+
+ len = strlen (hardlink->canon_filename);
+ if (len + sizeof (".#prelink#") > movelen)
+ {
+ movelen = len + sizeof (".#prelink#");
+ move = realloc (move, movelen);
+ if (move == NULL)
+ {
+ error (0, ENOMEM, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ movelen = 0;
+ continue;
+ }
+ }
+
+ memcpy (mempcpy (move, hardlink->canon_filename, len), ".#prelink#",
+ sizeof (".#prelink#"));
+ if (wrap_rename (hardlink->canon_filename, move) < 0)
+ {
+ error (0, errno, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+ continue;
+ }
+
+ if (wrap_link (ent->canon_filename, hardlink->canon_filename) < 0)
+ {
+ error (0, errno, "Could not hardlink %s to %s",
+ hardlink->canon_filename, ent->canon_filename);
+
+ if (wrap_rename (move, hardlink->canon_filename) < 0)
+ {
+ error (0, errno, "Could not rename %s back to %s",
+ move, hardlink->canon_filename);
+ }
+ continue;
+ }
+
+ if (wrap_unlink (move) < 0)
+ {
+ error (0, errno, "Could not unlink %s", move);
+ continue;
+ }
+ }
+ free (move);
+ return 1;
+
+error_out:
+ if (dso)
+ close_dso (dso);
+ (*(int *)info)++;
+ return 1;
+}
+
+int
+undo_all (void)
+{
+ int failures = 0;
+ htab_traverse (prelink_filename_htab, undo_one, &failures);
+ return failures != 0;
+}
diff --git a/trunk/src/verify.c b/trunk/src/verify.c
new file mode 100644
index 0000000..8acecfb
--- /dev/null
+++ b/trunk/src/verify.c
@@ -0,0 +1,442 @@
+/* Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
+ Written by Jakub Jelinek <jakub@redhat.com>, 2002.
+
+ 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 <endian.h>
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include "prelink.h"
+#include "md5.h"
+#include "sha.h"
+
+static ssize_t
+send_file (int outfd, int infd, off_t *poff, size_t count)
+{
+ char buf[65536], *b, *p, *q;
+ size_t todo = count, len;
+ ssize_t n;
+
+ b = mmap (NULL, count, PROT_READ, MAP_PRIVATE, infd, *poff);
+ if (b != MAP_FAILED)
+ {
+ p = b;
+ q = p + count;
+ while (p != q)
+ {
+ n = TEMP_FAILURE_RETRY (write (outfd, p, q - p));
+ if (n < 0)
+ {
+ munmap (b, count);
+ return -1;
+ }
+ p += n;
+ }
+ munmap (b, count);
+ return count;
+ }
+
+ if (lseek (infd, *poff, SEEK_SET) != *poff)
+ return -1;
+ while (todo > 0)
+ {
+ len = todo > sizeof (buf) ? sizeof (buf) : todo;
+ p = buf;
+ q = buf + len;
+ while (p != q)
+ {
+ n = TEMP_FAILURE_RETRY (read (infd, p, q - p));
+ if (n < 0)
+ return -1;
+ p += n;
+ }
+ p = buf;
+ while (p != q)
+ {
+ n = TEMP_FAILURE_RETRY (write (outfd, p, q - p));
+ if (n < 0)
+ return -1;
+ p += n;
+ }
+ todo -= len;
+ }
+ return count;
+}
+
+static int
+checksum_file (int fd, size_t count,
+ void (*sum) (const void *, size_t, void *), void *arg)
+{
+ char buf[65536+64], *b, *p, *q;
+ size_t todo = count, len;
+ ssize_t n;
+
+ b = mmap (NULL, count, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (b != MAP_FAILED)
+ {
+ sum (b, count, arg);
+ munmap (b, count);
+ return 0;
+ }
+
+ b = (char *) (((uintptr_t) buf + 63) & ~(uintptr_t) 63);
+ while (todo > 0)
+ {
+ len = todo > 65536 ? 65536 : todo;
+ p = b;
+ q = b + len;
+ while (p != q)
+ {
+ n = TEMP_FAILURE_RETRY (read (fd, p, q - p));
+ if (n < 0)
+ return 1;
+ p += n;
+ }
+ sum (b, len, arg);
+ todo -= len;
+ }
+ return 0;
+}
+
+static int
+handle_verify (int fd, const char *filename)
+{
+ off_t off;
+ size_t cnt;
+ struct stat64 st;
+
+ if (fstat64 (fd, &st) < 0)
+ {
+ error (0, errno, "%s: couldn't fstat temporary file", filename);
+ return 1;
+ }
+
+ if (verify_method == VERIFY_CONTENT)
+ {
+ off = 0;
+ if (send_file (1, fd, &off, st.st_size) != st.st_size)
+ {
+ error (0, errno, "Couldn't write file to standard output");
+ return 1;
+ }
+ }
+ else if (verify_method == VERIFY_MD5)
+ {
+ struct md5_ctx ctx;
+ unsigned char bin_buffer[16];
+
+ md5_init_ctx (&ctx);
+ if (checksum_file (fd, st.st_size,
+ (void (*) (const void *, size_t, void *))
+ md5_process_bytes, &ctx))
+ {
+ error (0, errno, "%s: Couldn't read temporary file", filename);
+ return 1;
+ }
+
+ md5_finish_ctx (&ctx, bin_buffer);
+ for (cnt = 0; cnt < 16; ++cnt)
+ printf ("%02x", bin_buffer[cnt]);
+ printf (" %s\n", filename);
+ }
+ else if (verify_method == VERIFY_SHA)
+ {
+ struct sha_ctx ctx;
+ unsigned char bin_buffer[20];
+
+ sha_init_ctx (&ctx);
+ if (checksum_file (fd, st.st_size,
+ (void (*) (const void *, size_t, void *))
+ sha_process_bytes, &ctx))
+ {
+ error (0, errno, "%s: Couldn't read temporary file", filename);
+ return 1;
+ }
+
+ sha_finish_ctx (&ctx, bin_buffer);
+ for (cnt = 0; cnt < 20; ++cnt)
+ printf ("%02x", bin_buffer[cnt]);
+ printf (" %s\n", filename);
+ }
+ return 0;
+}
+
+int
+prelink_verify (const char *filename)
+{
+ DSO *dso = NULL, *dso2 = NULL;
+ int fd = -1, fdorig = -1, fdundone = -1, undo, ret;
+ struct stat64 st, st2;
+ struct prelink_entry *ent;
+ GElf_Addr base;
+ char buffer[32768], buffer2[32768];
+ size_t count;
+ char *p, *q;
+
+ if (wrap_stat64 (filename, &st) < 0)
+ error (EXIT_FAILURE, errno, "Couldn't stat %s", filename);
+
+ dso = open_dso (filename);
+ if (dso == NULL)
+ goto not_prelinked;
+
+ if (dso->ehdr.e_type != ET_DYN && dso->ehdr.e_type != ET_EXEC)
+ {
+ error (0, 0, "%s is not an ELF shared library nor binary", filename);
+ goto not_prelinked;
+ }
+
+ for (undo = 1; undo < dso->ehdr.e_shnum; ++undo)
+ if (! strcmp (strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[undo].sh_name),
+ ".gnu.prelink_undo"))
+ break;
+
+ if (undo == dso->ehdr.e_shnum)
+ goto not_prelinked;
+
+ if (fstat64 (dso->fd, &st2) < 0)
+ {
+ error (0, errno, "Couldn't fstat %s", filename);
+ goto failure;
+ }
+
+ if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino
+ || st.st_size != st2.st_size)
+ {
+ error (0, 0, "%s: changed during --verify", filename);
+ goto failure;
+ }
+
+ if (gather_config (prelink_conf))
+ goto failure;
+
+ if (gather_object (filename, 0, 0))
+ goto failure;
+
+ ent = prelink_find_entry (filename, &st, 0);
+ if (ent == NULL)
+ {
+ error (0, 0, "%s disappeared while running --verify", filename);
+ goto failure;
+ }
+
+ if (ent->done != 2)
+ {
+ error (0, 0, "%s: at least one of file's dependencies has changed since prelinking",
+ filename);
+ goto failure;
+ }
+
+ base = dso->base;
+ ent->base = base;
+
+ ret = prelink_undo (dso);
+ if (ret)
+ goto failure;
+
+ switch (write_dso (dso))
+ {
+ case 2:
+ error (0, 0, "Could not write temporary for %s: %s", filename,
+ elf_errmsg (-1));
+ goto failure;
+ case 1:
+ goto failure;
+ case 0:
+ break;
+ }
+
+ fd = wrap_open (dso->temp_filename, O_RDONLY);
+ if (fd < 0)
+ {
+ error (0, errno, "Could not verify %s", filename);
+ goto failure;
+ }
+
+ fdorig = dup (dso->fdro);
+ if (fdorig < 0)
+ {
+ error (0, errno, "Could not verify %s", filename);
+ goto failure;
+ }
+
+ ent->filename = dso->temp_filename;
+ dso->temp_filename = NULL;
+ close_dso (dso);
+ dso = NULL;
+
+ fchmod (fd, 0700);
+
+ dso2 = fdopen_dso (fd, filename);
+ if (dso2 == NULL)
+ goto failure_unlink;
+ fd = -1;
+
+ if (prelink_prepare (dso2))
+ goto failure_unlink;
+
+ if (ent->type == ET_DYN && relocate_dso (dso2, base))
+ goto failure_unlink;
+
+ if (prelink (dso2, ent))
+ goto failure_unlink;
+
+ unlink (ent->filename);
+
+ if (write_dso (dso2))
+ goto failure;
+
+ fd = dup (dso2->fd);
+ if (fd < 0)
+ {
+ error (0, errno, "Could not verify %s", filename);
+ goto failure;
+ }
+
+ fdundone = dup (dso2->fdro);
+ if (fdundone < 0)
+ {
+ error (0, errno, "Could not verify %s", filename);
+ goto failure;
+ }
+
+ close_dso (dso2);
+ dso2 = NULL;
+
+ if (fstat64 (fdorig, &st2) < 0)
+ {
+ error (0, errno, "Couldn't fstat %s", filename);
+ goto failure;
+ }
+
+ if (st.st_dev != st2.st_dev || st.st_ino != st2.st_ino
+ || st.st_size != st2.st_size)
+ {
+ error (0, 0, "%s: changed during --verify", filename);
+ goto failure;
+ }
+
+ if (fstat64 (fd, &st2) < 0)
+ {
+ error (0, errno, "Couldn't fstat temporary file");
+ goto failure;
+ }
+
+ if (st.st_size != st2.st_size)
+ {
+ error (0, 0, "%s: prelinked file size differs", filename);
+ goto failure;
+ }
+
+ q = MAP_FAILED;
+ p = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdorig, 0);
+ if (p != MAP_FAILED)
+ {
+ q = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (q == MAP_FAILED)
+ {
+ munmap (p, st.st_size);
+ p = MAP_FAILED;
+ }
+ }
+ if (p != MAP_FAILED)
+ {
+ int ret = memcmp (p, q, st.st_size);
+
+ munmap (p, st.st_size);
+ munmap (q, st.st_size);
+ if (ret != 0)
+ {
+ error (0, 0, "%s: prelinked file was modified", filename);
+ goto failure;
+ }
+ }
+ else
+ {
+ if (lseek (fdorig, 0, SEEK_SET) != 0
+ || lseek (fd, 0, SEEK_SET) != 0)
+ {
+ error (0, errno, "%s: couldn't seek to start of files", filename);
+ goto failure;
+ }
+
+ count = st.st_size;
+ while (count > 0)
+ {
+ size_t len = sizeof (buffer);
+
+ if (len > count)
+ len = count;
+ if (read (fdorig, buffer, len) != len)
+ {
+ error (0, errno, "%s: couldn't read file", filename);
+ goto failure;
+ }
+ if (read (fd, buffer2, len) != len)
+ {
+ error (0, errno, "%s: couldn't read temporary file", filename);
+ goto failure;
+ }
+ if (memcmp (buffer, buffer2, len) != 0)
+ {
+ error (0, 0, "%s: prelinked file was modified", filename);
+ goto failure;
+ }
+ count -= len;
+ }
+ }
+
+ if (handle_verify (fdundone, filename))
+ goto failure;
+
+ close (fd);
+ close (fdorig);
+ close (fdundone);
+ return 0;
+
+failure_unlink:
+ unlink (ent->filename);
+failure:
+ if (fd != -1)
+ close (fd);
+ if (fdorig != -1)
+ close (fdorig);
+ if (fdundone != -1)
+ close (fdundone);
+ if (dso)
+ close_dso (dso);
+ if (dso2)
+ close_dso (dso2);
+ return EXIT_FAILURE;
+
+not_prelinked:
+ if (dso)
+ close_dso (dso);
+ fd = wrap_open (filename, O_RDONLY);
+ if (fd < 0)
+ error (EXIT_FAILURE, errno, "Couldn't open %s", filename);
+ if (handle_verify (fd, filename))
+ return EXIT_FAILURE;
+ close (fd);
+ return 0;
+}
diff --git a/trunk/src/wrap-file.c b/trunk/src/wrap-file.c
new file mode 100644
index 0000000..c631458
--- /dev/null
+++ b/trunk/src/wrap-file.c
@@ -0,0 +1,536 @@
+/* Copyright (C) 2003 MontaVista Software, Inc.
+ Written by Daniel Jacobowitz <drow@mvista.com>, 2003.
+
+ The chroot_canon function is copied from the GNU C Library,
+ elf/chroot-canon.c, also licensed under the GPL:
+ Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
+ [and then further modified.]
+
+ 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 <ftw.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+#include <utime.h>
+#include "prelink.h"
+
+#ifndef PATH_MAX
+#define PATH_MAX 1024
+#endif
+
+#ifndef MAXSYMLINKS
+#define MAXSYMLINKS 20
+#endif
+
+/* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
+ done first. A canonical name does not contain any `.', `..' components
+ nor any repeated path separators ('/') or symlinks. All path components
+ must exist and NAME must be absolute filename. The result is malloc'd.
+ The returned name includes the CHROOT prefix.
+
+ If ALLOW_LAST_LINK, then symlinks in the last component won't be
+ resolved. */
+
+static char *
+chroot_canon_filename (const char * chroot, const char *name, int allow_last_link, struct stat64 *stp)
+{
+ char *rpath, *dest, *extra_buf = NULL;
+ char *rpath_root;
+ const char *start, *end, *rpath_limit;
+ long int path_max;
+ int num_links = 0;
+ int stp_initialized = 0;
+ size_t chroot_len = 0;
+
+ if (name == NULL)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (name[0] == '\0')
+ {
+ errno = ENOENT;
+ return NULL;
+ }
+
+ if (chroot == NULL)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ chroot_len = strlen (chroot);
+
+#ifdef PATH_MAX
+ path_max = PATH_MAX;
+#else
+ path_max = pathconf (name, _PC_PATH_MAX);
+ if (path_max <= 0)
+ path_max = 1024;
+#endif
+
+ rpath = malloc (chroot_len + path_max);
+ if (rpath == NULL)
+ return NULL;
+ rpath_limit = rpath + chroot_len + path_max;
+
+ rpath_root = (char *) mempcpy (rpath, chroot, chroot_len) - 1;
+ if (*rpath_root != '/')
+ *++rpath_root = '/';
+ dest = rpath_root + 1;
+
+ for (start = end = name; *start; start = end)
+ {
+ int n;
+
+ /* Skip sequence of multiple path-separators. */
+ while (*start == '/')
+ ++start;
+
+ /* Find end of path component. */
+ for (end = start; *end && *end != '/'; ++end)
+ /* Nothing. */;
+
+ if (end - start == 0)
+ break;
+ else if (end - start == 1 && start[0] == '.')
+ /* nothing */;
+ else if (end - start == 2 && start[0] == '.' && start[1] == '.')
+ {
+ /* Back up to previous component, ignore if at root already. */
+ if (dest > rpath_root + 1)
+ while ((--dest)[-1] != '/');
+ stp_initialized = 0;
+ }
+ else
+ {
+ size_t new_size;
+
+ if (dest[-1] != '/')
+ *dest++ = '/';
+
+ if (dest + (end - start) >= rpath_limit)
+ {
+ ptrdiff_t dest_offset = dest - rpath;
+ char *new_rpath;
+
+ new_size = rpath_limit - rpath;
+ if (end - start + 1 > path_max)
+ new_size += end - start + 1;
+ else
+ new_size += path_max;
+ new_rpath = (char *) realloc (rpath, new_size);
+ if (new_rpath == NULL)
+ goto error;
+ rpath = new_rpath;
+ rpath_limit = rpath + new_size;
+
+ dest = rpath + dest_offset;
+ }
+
+ dest = mempcpy (dest, start, end - start);
+ *dest = '\0';
+
+ if (lstat64 (rpath, stp) < 0)
+ goto error;
+
+ stp_initialized = 1;
+
+ if (allow_last_link && *end == '\0')
+ goto done;
+
+ if (S_ISLNK (stp->st_mode))
+ {
+ char *buf = alloca (path_max);
+ size_t len;
+
+ if (++num_links > MAXSYMLINKS)
+ {
+ errno = ELOOP;
+ goto error;
+ }
+
+ n = readlink (rpath, buf, path_max);
+ if (n < 0)
+ goto error;
+ buf[n] = '\0';
+
+ if (!extra_buf)
+ extra_buf = alloca (path_max);
+
+ len = strlen (end);
+ if ((long int) (n + len) >= path_max)
+ {
+ errno = ENAMETOOLONG;
+ goto error;
+ }
+
+ /* Careful here, end may be a pointer into extra_buf... */
+ memmove (&extra_buf[n], end, len + 1);
+ name = end = memcpy (extra_buf, buf, n);
+
+ if (buf[0] == '/')
+ dest = rpath_root + 1; /* It's an absolute symlink */
+ else
+ /* Back up to previous component, ignore if at root already: */
+ if (dest > rpath_root + 1)
+ while ((--dest)[-1] != '/');
+ }
+ else if (!S_ISDIR (stp->st_mode) && *end != '\0')
+ {
+ errno = ENOTDIR;
+ goto error;
+ }
+ }
+ }
+done:
+ if (dest > rpath_root + 1 && dest[-1] == '/')
+ --dest;
+ *dest = '\0';
+
+ if (!stp_initialized && lstat64 (rpath, stp) < 0)
+ goto error;
+
+ if (dest + 1 - rpath <= (rpath_limit - rpath) / 2)
+ {
+ char *new_rpath = realloc (rpath, dest + 1 - rpath);
+
+ if (new_rpath != NULL)
+ return new_rpath;
+ }
+ return rpath;
+
+error:
+ free (rpath);
+ return NULL;
+}
+
+
+const char *sysroot;
+
+static char *
+sysroot_file_name (const char *name, int allow_last_link, struct stat64 *stp)
+{
+ char *ret;
+ struct stat64 st;
+
+ if (sysroot == NULL || name == NULL)
+ return (char *) name;
+
+ if (name[0] != '/')
+ {
+ char *tmpname = malloc (strlen (name) + 2);
+ strcpy (tmpname, "/");
+ strcat (tmpname, name);
+ ret = chroot_canon_filename (sysroot, tmpname, allow_last_link, stp ? stp : &st);
+ free (tmpname);
+ }
+ else
+ ret = chroot_canon_filename (sysroot, name, allow_last_link, stp ? stp : &st);
+
+ if (ret == NULL)
+ {
+ char *ret_root;
+
+ ret = malloc(strlen(sysroot) + strlen(name) + 1);
+ ret_root = mempcpy(ret, sysroot, strlen(sysroot));
+ ret_root = mempcpy(ret_root, name, strlen(name));
+ *ret_root='\0';
+ }
+ return ret;
+}
+
+static char *
+unsysroot_file_name (const char *name)
+{
+ if (name == NULL)
+ return (char *)name;
+
+ if (sysroot)
+ {
+ int sysroot_len = strlen (sysroot);
+ if (strncmp (name, sysroot, sysroot_len) == 0)
+ {
+ if (name[sysroot_len] == '/')
+ return strdup (name + sysroot_len);
+ else if (name[sysroot_len] == 0)
+ return strdup ("/");
+ }
+ }
+ return (char *)name;
+}
+
+char *
+wrap_prelink_canonicalize (const char *name, struct stat64 *stp)
+{
+ if (sysroot)
+ {
+ struct stat64 st;
+ char *tmpname;
+ char *ret;
+
+ /* Use chroot_canon_filename because we want a NULL return if it doesn't exist! */
+ tmpname = chroot_canon_filename (sysroot, name, 0, stp ? stp : &st);
+
+ if (tmpname == NULL)
+ return NULL;
+
+ ret = unsysroot_file_name (tmpname);
+
+ if (ret == tmpname)
+ ret = strdup (ret);
+
+ if (tmpname != name)
+ free (tmpname);
+
+ return ret;
+ }
+ else
+ return prelink_canonicalize(name, stp);
+}
+
+int
+wrap_lstat64 (const char *file, struct stat64 *buf)
+{
+ char *tmpname = sysroot_file_name (file, 1, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ ret = lstat64 (tmpname, buf);
+
+ if (tmpname != file)
+ free (tmpname);
+ return ret;
+}
+
+int
+wrap_stat64 (const char *file, struct stat64 *buf)
+{
+ char* file_copy;
+ char *tmpname;
+ int ret;
+ int len;
+
+ tmpname = sysroot_file_name (file, 0, NULL);
+
+ if (tmpname == NULL)
+ return -1;
+
+ file_copy = strdup (tmpname);
+
+ if (tmpname != file)
+ free (tmpname);
+
+ if (file_copy == NULL)
+ return -1;
+
+ len = strlen (file_copy);
+ if (file_copy[len - 1] == '/')
+ file_copy[len - 1] = '\0';
+
+ ret = stat64 (file_copy, buf);
+
+ free (file_copy);
+
+ return ret;
+}
+
+int
+wrap_rename (const char *old, const char *new)
+{
+ char *tmpold = sysroot_file_name (old, 1, NULL);
+ char *tmpnew;
+ int ret;
+
+ if (tmpold == NULL)
+ return -1;
+
+ tmpnew = sysroot_file_name (new, 1, NULL);
+ if (tmpnew == NULL)
+ return -1;
+
+ ret = rename (tmpold, tmpnew);
+
+ if (tmpold != old)
+ free (tmpold);
+ if (tmpnew != new)
+ free (tmpnew);
+ return ret;
+}
+
+int
+wrap_open (const char *name, int mode, ...)
+{
+ char *tmpname = sysroot_file_name (name, 0, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ if (mode & O_CREAT)
+ {
+ va_list va;
+ int flags;
+ va_start (va, mode);
+ flags = va_arg (va, int);
+ va_end (va);
+ ret = open (tmpname, mode, flags);
+ }
+ else
+ ret = open (tmpname, mode);
+
+ if (tmpname != name)
+ free (tmpname);
+ return ret;
+}
+
+int
+wrap_access (const char *name, int mode)
+{
+ char *tmpname = sysroot_file_name (name, 0, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ ret = access (tmpname, mode);
+
+ if (tmpname != name)
+ free (tmpname);
+ return ret;
+}
+
+int
+wrap_link (const char *old, const char *new)
+{
+ char *tmpold = sysroot_file_name (old, 1, NULL);
+ char *tmpnew;
+ int ret;
+
+ if (tmpold == NULL)
+ return -1;
+
+ tmpnew = sysroot_file_name (new, 1, NULL);
+ if (tmpnew == NULL)
+ return -1;
+
+ ret = link (tmpold, tmpnew);
+
+ if (tmpold != old)
+ free (tmpold);
+ if (tmpnew != new)
+ free (tmpnew);
+ return ret;
+}
+
+/* Note that this isn't recursive safe, since nftw64 doesn't
+ pass an opaque object around to use. But that fits our needs
+ for now. */
+
+static __nftw64_func_t nftw64_cur_func;
+
+static int
+wrap_nftw64_func (const char *filename, const struct stat64 *status,
+ int flag, struct FTW *info)
+{
+ char *tmpname = unsysroot_file_name (filename);
+ int ret = nftw64_cur_func (tmpname, status, flag, info);
+
+ if (tmpname != filename)
+ free (tmpname);
+ return ret;
+}
+
+int
+wrap_nftw64 (const char *dir, __nftw64_func_t func,
+ int descriptors, int flag)
+{
+ char *tmpdir = sysroot_file_name (dir, 1, NULL);
+ int ret;
+
+ if (tmpdir == NULL)
+ return -1;
+
+ nftw64_cur_func = func;
+ ret = nftw64 (tmpdir, wrap_nftw64_func, descriptors, flag);
+
+ if (tmpdir != dir)
+ free (tmpdir);
+ return ret;
+}
+
+int
+wrap_utime (const char *file, struct utimbuf *file_times)
+{
+ char *tmpname = sysroot_file_name (file, 0, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ ret = utime (tmpname, file_times);
+
+ if (tmpname != file)
+ free (tmpname);
+ return ret;
+}
+
+int
+wrap_mkstemp (char *filename)
+{
+ char *tmpname = sysroot_file_name (filename, 1, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ ret = mkstemp (tmpname);
+
+ if (tmpname != filename)
+ {
+ strcpy (filename, tmpname + strlen (sysroot));
+ free (tmpname);
+ }
+ return ret;
+}
+
+int
+wrap_unlink (const char *filename)
+{
+ char *tmpname = sysroot_file_name (filename, 1, NULL);
+ int ret;
+
+ if (tmpname == NULL)
+ return -1;
+
+ ret = unlink (tmpname);
+
+ if (tmpname != filename)
+ free (tmpname);
+ return ret;
+}