summaryrefslogtreecommitdiffstats
path: root/linker
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
committerThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
commita27d2baa0c1a2ec70f47ea9199b1dd6762c8a349 (patch)
treedefd1cc07d16ad2f3b21154114e092d11c94c5bb /linker
downloadbionic-a27d2baa0c1a2ec70f47ea9199b1dd6762c8a349.zip
bionic-a27d2baa0c1a2ec70f47ea9199b1dd6762c8a349.tar.gz
bionic-a27d2baa0c1a2ec70f47ea9199b1dd6762c8a349.tar.bz2
Initial Contributionandroid-1.0
Diffstat (limited to 'linker')
-rw-r--r--linker/Android.mk68
-rw-r--r--linker/MODULE_LICENSE_APACHE20
-rw-r--r--linker/NOTICE190
-rw-r--r--linker/arch/arm/begin.S17
-rw-r--r--linker/arch/x86/begin.S23
-rw-r--r--linker/debugger.c52
-rw-r--r--linker/dlfcn.c212
-rw-r--r--linker/linker.c1702
-rw-r--r--linker/linker.h185
-rw-r--r--linker/linker_debug.h111
-rw-r--r--linker/rt.c8
11 files changed, 2568 insertions, 0 deletions
diff --git a/linker/Android.mk b/linker/Android.mk
new file mode 100644
index 0000000..5d11148
--- /dev/null
+++ b/linker/Android.mk
@@ -0,0 +1,68 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+ arch/$(TARGET_ARCH)/begin.S \
+ linker.c \
+ rt.c \
+ dlfcn.c \
+ debugger.c
+
+LINKER_TEXT_BASE := 0xB0000100
+
+# The maximum size set aside for the linker, from
+# LINKER_TEXT_BASE rounded down to a megabyte.
+LINKER_AREA_SIZE := 0x01000000
+
+LOCAL_LDFLAGS := -Wl,-Ttext,$(LINKER_TEXT_BASE)
+
+LOCAL_CFLAGS += -DPRELINK -DLINKER_TEXT_BASE=$(LINKER_TEXT_BASE) -DLINKER_AREA_SIZE=$(LINKER_AREA_SIZE)
+
+ifeq ($(TARGET_ARCH),arm)
+LOCAL_CFLAGS += -DANDROID_ARM_LINKER
+else
+ ifeq ($(TARGET_ARCH),x86)
+ LOCAL_CFLAGS += -DANDROID_X86_LINKER
+ else
+ $(error Unsupported TARGET_ARCH $(TARGET_ARCH))
+ endif
+endif
+
+LOCAL_MODULE:= linker
+
+LOCAL_STATIC_LIBRARIES := libcutils libc
+
+#LOCAL_FORCE_STATIC_EXECUTABLE := true # not necessary when not including BUILD_EXECUTABLE
+
+#
+# include $(BUILD_EXECUTABLE)
+#
+# Instead of including $(BUILD_EXECUTABLE), we execute the steps to create an executable by
+# hand, as we want to insert an extra step that is not supported by the build system, and
+# is probably specific the linker only, so there's no need to modify the build system for
+# the purpose.
+
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_SUFFIX := $(TARGET_EXECUTABLE_SUFFIX)
+
+# Executables are not prelinked.
+LOCAL_PRELINK_MODULE := false
+
+include $(BUILD_SYSTEM)/dynamic_binary.mk
+
+$(linked_module): $(TARGET_CRTBEGIN_STATIC_O) $(all_objects) $(all_libraries) $(TARGET_CRTEND_O)
+ $(transform-o-to-static-executable)
+ @echo "target PrefixSymbols: $(PRIVATE_MODULE) ($@)"
+ $(hide) $(TARGET_OBJCOPY) --prefix-symbols=__dl_ $@
+
+#
+# end of BUILD_EXECUTABLE hack
+#
+
+# we don't want crtbegin.o (because we have begin.o), so unset it
+# just for this module
+$(LOCAL_BUILT_MODULE): TARGET_CRTBEGIN_STATIC_O :=
+# This line is not strictly necessary because the dynamic linker is built
+# as a static executable, but it won't hurt if in the future we start
+# building the linker as a dynamic one.
+$(LOCAL_BUILT_MODULE): TARGET_CRTBEGIN_DYNAMIC_O :=
diff --git a/linker/MODULE_LICENSE_APACHE2 b/linker/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/linker/MODULE_LICENSE_APACHE2
diff --git a/linker/NOTICE b/linker/NOTICE
new file mode 100644
index 0000000..c5b1efa
--- /dev/null
+++ b/linker/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/linker/arch/arm/begin.S b/linker/arch/arm/begin.S
new file mode 100644
index 0000000..f9b3bac
--- /dev/null
+++ b/linker/arch/arm/begin.S
@@ -0,0 +1,17 @@
+ .text
+ .align 4
+ .type _start,#function
+ .globl _start
+
+_start:
+ mov r0, sp
+ mov r1, #0
+ bl __linker_init
+
+ /* linker init returns the _entry address in the main image */
+ mov pc, r0
+
+ .section .ctors, "wa"
+ .globl __CTOR_LIST__
+__CTOR_LIST__:
+ .long -1
diff --git a/linker/arch/x86/begin.S b/linker/arch/x86/begin.S
new file mode 100644
index 0000000..8f468e6
--- /dev/null
+++ b/linker/arch/x86/begin.S
@@ -0,0 +1,23 @@
+.text
+.align 4
+.type _start, @function
+.globl _start
+
+_start:
+ /* save the elfdata ptr to %eax, AND push it onto the stack */
+ mov %esp, %eax
+ pushl %esp
+
+ pushl %eax
+ call __linker_init
+
+ /* linker init returns (%eax) the _entry address in the main image */
+ /* entry point expects sp to point to elfdata */
+ popl %esp
+ jmp *%eax
+
+.section .ctors, "wa"
+.globl __CTOR_LIST__
+
+__CTOR_LIST__:
+ .long -1
diff --git a/linker/debugger.c b/linker/debugger.c
new file mode 100644
index 0000000..68997c5
--- /dev/null
+++ b/linker/debugger.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <signal.h>
+#include <sys/mman.h>
+
+#include "linker.h"
+
+#include <sys/socket.h>
+#include <cutils/sockets.h>
+
+void notify_gdb_of_libraries();
+
+void debugger_signal_handler(int n)
+{
+ unsigned tid;
+ int s;
+
+ /* avoid picking up GC interrupts */
+ signal(SIGUSR1, SIG_IGN);
+
+ tid = gettid();
+ s = socket_local_client("android:debuggerd",
+ ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+
+ if(s >= 0) {
+ /* debugger knows our pid from the credentials on the
+ ** local socket but we need to tell it our tid. It
+ ** is paranoid and will verify that we are giving a tid
+ ** that's actually in our process
+ */
+ write(s, &tid, sizeof(unsigned));
+
+ read(s, &tid, 1);
+ notify_gdb_of_libraries();
+ close(s);
+ }
+
+ /* remove our net so we fault for real when we return */
+ signal(n, SIG_IGN);
+}
+
+void debugger_init()
+{
+ signal(SIGILL, debugger_signal_handler);
+ signal(SIGABRT, debugger_signal_handler);
+ signal(SIGBUS, debugger_signal_handler);
+ signal(SIGFPE, debugger_signal_handler);
+ signal(SIGSEGV, debugger_signal_handler);
+ signal(SIGSTKFLT, debugger_signal_handler);
+}
diff --git a/linker/dlfcn.c b/linker/dlfcn.c
new file mode 100644
index 0000000..cd73d11
--- /dev/null
+++ b/linker/dlfcn.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <dlfcn.h>
+#include <pthread.h>
+#include "linker.h"
+
+/* This file hijacks the symbols stubbed out in libdl.so. */
+
+#define DL_SUCCESS 0
+#define DL_ERR_CANNOT_FIND_LIBRARY 1
+#define DL_ERR_INVALID_LIBRARY_HANDLE 2
+#define DL_ERR_BAD_SYMBOL_NAME 3
+#define DL_ERR_SYMBOL_NOT_FOUND 4
+#define DL_ERR_SYMBOL_NOT_GLOBAL 5
+
+static const char *dl_errors[] = {
+ [DL_SUCCESS] = NULL,
+ [DL_ERR_CANNOT_FIND_LIBRARY] = "Cannot find library",
+ [DL_ERR_INVALID_LIBRARY_HANDLE] = "Invalid library handle",
+ [DL_ERR_BAD_SYMBOL_NAME] = "Invalid symbol name",
+ [DL_ERR_SYMBOL_NOT_FOUND] = "Symbol not found",
+ [DL_ERR_SYMBOL_NOT_GLOBAL] = "Symbol is not global",
+};
+
+static int dl_last_err = DL_SUCCESS;
+
+#define likely(expr) __builtin_expect (expr, 1)
+#define unlikely(expr) __builtin_expect (expr, 0)
+
+static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void *dlopen(const char *filename, int flag)
+{
+ soinfo *ret;
+
+ pthread_mutex_lock(&dl_lock);
+ ret = find_library(filename);
+ if (unlikely(ret == NULL)) {
+ dl_last_err = DL_ERR_CANNOT_FIND_LIBRARY;
+ } else {
+ ret->refcount++;
+ }
+ pthread_mutex_unlock(&dl_lock);
+ return ret;
+}
+
+const char *dlerror(void)
+{
+ const char *err = dl_errors[dl_last_err];
+ dl_last_err = DL_SUCCESS;
+ return err;
+}
+
+void *dlsym(void *handle, const char *symbol)
+{
+ unsigned base;
+ Elf32_Sym *sym;
+ unsigned bind;
+
+ pthread_mutex_lock(&dl_lock);
+
+ if(unlikely(handle == 0)) {
+ dl_last_err = DL_ERR_INVALID_LIBRARY_HANDLE;
+ goto err;
+ }
+ if(unlikely(symbol == 0)) {
+ dl_last_err = DL_ERR_BAD_SYMBOL_NAME;
+ goto err;
+ }
+
+ if(handle == RTLD_DEFAULT) {
+ sym = lookup(symbol, &base);
+ } else if(handle == RTLD_NEXT) {
+ sym = lookup(symbol, &base);
+ } else {
+ sym = lookup_in_library((soinfo*) handle, symbol);
+ base = ((soinfo*) handle)->base;
+ }
+
+ if(likely(sym != 0)) {
+ bind = ELF32_ST_BIND(sym->st_info);
+
+ if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
+ unsigned ret = sym->st_value + base;
+ pthread_mutex_unlock(&dl_lock);
+ return (void*)ret;
+ }
+
+ dl_last_err = DL_ERR_SYMBOL_NOT_GLOBAL;
+ }
+ else dl_last_err = DL_ERR_SYMBOL_NOT_FOUND;
+
+err:
+ pthread_mutex_unlock(&dl_lock);
+ return 0;
+}
+
+int dlclose(void *handle)
+{
+ pthread_mutex_lock(&dl_lock);
+ (void)unload_library((soinfo*)handle);
+ pthread_mutex_unlock(&dl_lock);
+ return 0;
+}
+
+#if defined(ANDROID_ARM_LINKER)
+// 0000000 00011111 111112 22222222 233333333334444444444
+// 0123456 78901234 567890 12345678 901234567890123456789
+#define ANDROID_LIBDL_STRTAB \
+ "dlopen\0dlclose\0dlsym\0dlerror\0dl_unwind_find_exidx\0"
+
+#elif defined(ANDROID_X86_LINKER)
+// 0000000 00011111 111112 22222222 2333333333344444
+// 0123456 78901234 567890 12345678 9012345678901234
+#define ANDROID_LIBDL_STRTAB \
+ "dlopen\0dlclose\0dlsym\0dlerror\0dl_iterate_phdr\0"
+
+#else /* !defined(ANDROID_ARM_LINKER) && !defined(ANDROID_X86_LINKER) */
+#error Unsupported architecture. Only ARM and x86 are presently supported.
+#endif
+
+
+static Elf32_Sym libdl_symtab[] = {
+ // total length of libdl_info.strtab, including trailing 0
+ // This is actually the the STH_UNDEF entry. Technically, it's
+ // supposed to have st_name == 0, but instead, it points to an index
+ // in the strtab with a \0 to make iterating through the symtab easier.
+ { st_name: sizeof(ANDROID_LIBDL_STRTAB) - 1,
+ },
+ { st_name: 0, // starting index of the name in libdl_info.strtab
+ st_value: (Elf32_Addr) &dlopen,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+ { st_name: 7,
+ st_value: (Elf32_Addr) &dlclose,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+ { st_name: 15,
+ st_value: (Elf32_Addr) &dlsym,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+ { st_name: 21,
+ st_value: (Elf32_Addr) &dlerror,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+#ifdef ANDROID_ARM_LINKER
+ { st_name: 29,
+ st_value: (Elf32_Addr) &dl_unwind_find_exidx,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+#elif defined(ANDROID_X86_LINKER)
+ { st_name: 29,
+ st_value: (Elf32_Addr) &dl_iterate_phdr,
+ st_info: STB_GLOBAL << 4,
+ st_shndx: 1,
+ },
+#endif
+};
+
+/* Fake out a hash table with a single bucket.
+ * A search of the hash table will look through
+ * libdl_symtab starting with index [1], then
+ * use libdl_chains to find the next index to
+ * look at. libdl_chains should be set up to
+ * walk through every element in libdl_symtab,
+ * and then end with 0 (sentinel value).
+ *
+ * I.e., libdl_chains should look like
+ * { 0, 2, 3, ... N, 0 } where N is the number
+ * of actual symbols, or nelems(libdl_symtab)-1
+ * (since the first element of libdl_symtab is not
+ * a real symbol).
+ *
+ * (see _elf_lookup())
+ *
+ * Note that adding any new symbols here requires
+ * stubbing them out in libdl.
+ */
+static unsigned libdl_buckets[1] = { 1 };
+static unsigned libdl_chains[6] = { 0, 2, 3, 4, 5, 0 };
+
+soinfo libdl_info = {
+ name: "libdl.so",
+ flags: FLAG_LINKED,
+
+ strtab: ANDROID_LIBDL_STRTAB,
+ symtab: libdl_symtab,
+
+ nbucket: 1,
+ nchain: 6,
+ bucket: libdl_buckets,
+ chain: libdl_chains,
+};
+
diff --git a/linker/linker.c b/linker/linker.c
new file mode 100644
index 0000000..88e3ea2
--- /dev/null
+++ b/linker/linker.c
@@ -0,0 +1,1702 @@
+#include <linux/auxvec.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <dlfcn.h>
+
+//#include <pthread.h>
+
+#include <sys/mman.h>
+
+#include <sys/atomics.h>
+#include <sys/tls.h>
+
+#include "linker.h"
+#include "linker_debug.h"
+
+#define SO_MAX 64
+
+/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
+ *
+ * Do NOT use malloc() and friends or pthread_*() code here.
+ * Don't use printf() either; it's caused mysterious memory
+ * corruption in the past.
+ * The linker runs before we bring up libc and it's easiest
+ * to make sure it does not depend on any complex libc features
+ *
+ * open issues / todo:
+ *
+ * - should we do anything special for STB_WEAK symbols?
+ * - are we doing everything we should for ARM_COPY relocations?
+ * - cleaner error reporting
+ * - configuration for paths (LD_LIBRARY_PATH?)
+ * - after linking, set as much stuff as possible to READONLY
+ * and NOEXEC
+ * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
+ * headers provide versions that are negative...
+ * - allocate space for soinfo structs dynamically instead of
+ * having a hard limit (64)
+ *
+ * features to add someday:
+ *
+ * - dlopen() and friends
+ *
+*/
+
+
+static int link_image(soinfo *si, unsigned wr_offset);
+
+static int socount = 0;
+static soinfo sopool[SO_MAX];
+static soinfo *freelist = NULL;
+static soinfo *solist = &libdl_info;
+static soinfo *sonext = &libdl_info;
+
+int debug_verbosity;
+static int pid;
+
+#if STATS
+struct _link_stats linker_stats;
+#endif
+
+#if COUNT_PAGES
+unsigned bitmask[4096];
+#endif
+
+#ifndef PT_ARM_EXIDX
+#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
+#endif
+
+/*
+ * This function is an empty stub where GDB locates a breakpoint to get notified
+ * about linker activity.
+ */
+extern void __attribute__((noinline)) rtld_db_dlactivity(void);
+
+extern void sched_yield(void);
+
+static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
+static struct link_map *r_debug_tail = 0;
+
+//static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static volatile int loader_lock = 0;
+
+static void insert_soinfo_into_debug_map(soinfo * info)
+{
+ struct link_map * map;
+
+ /* Copy the necessary fields into the debug structure.
+ */
+ map = &(info->linkmap);
+ map->l_addr = info->base;
+ map->l_name = (char*) info->name;
+
+ /* Stick the new library at the end of the list.
+ * gdb tends to care more about libc than it does
+ * about leaf libraries, and ordering it this way
+ * reduces the back-and-forth over the wire.
+ */
+ if (r_debug_tail) {
+ r_debug_tail->l_next = map;
+ map->l_prev = r_debug_tail;
+ map->l_next = 0;
+ } else {
+ _r_debug.r_map = map;
+ map->l_prev = 0;
+ map->l_next = 0;
+ }
+ r_debug_tail = map;
+}
+
+void notify_gdb_of_load(soinfo * info)
+{
+ if (info->flags & FLAG_EXE) {
+ // GDB already knows about the main executable
+ return;
+ }
+
+ /* yes, this is a little gross, but it does avoid
+ ** pulling in pthread_*() and at the moment we don't
+ ** dlopen() anything anyway
+ */
+ while(__atomic_swap(1, &loader_lock) != 0) {
+ sched_yield();
+ usleep(5000);
+ }
+
+ _r_debug.r_state = RT_ADD;
+ rtld_db_dlactivity();
+
+ insert_soinfo_into_debug_map(info);
+
+ _r_debug.r_state = RT_CONSISTENT;
+ rtld_db_dlactivity();
+
+ __atomic_swap(0, &loader_lock);
+}
+
+void notify_gdb_of_libraries()
+{
+ _r_debug.r_state = RT_ADD;
+ rtld_db_dlactivity();
+ _r_debug.r_state = RT_CONSISTENT;
+ rtld_db_dlactivity();
+}
+
+static soinfo *alloc_info(const char *name)
+{
+ soinfo *si;
+
+ if(strlen(name) >= SOINFO_NAME_LEN) {
+ ERROR("%5d library name %s too long\n", pid, name);
+ return 0;
+ }
+
+ /* The freelist is populated when we call free_info(), which in turn is
+ done only by dlclose(), which is not likely to be used.
+ */
+ if (!freelist) {
+ if(socount == SO_MAX) {
+ ERROR("%5d too many libraries when loading %s\n", pid, name);
+ return NULL;
+ }
+ freelist = sopool + socount++;
+ freelist->next = NULL;
+ }
+
+ si = freelist;
+ freelist = freelist->next;
+
+ /* Make sure we get a clean block of soinfo */
+ memset(si, 0, sizeof(soinfo));
+ strcpy((char*) si->name, name);
+ sonext->next = si;
+ si->next = NULL;
+ si->refcount = 0;
+ sonext = si;
+
+ TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
+ return si;
+}
+
+static void free_info(soinfo *si)
+{
+ soinfo *prev = NULL, *trav;
+
+ TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
+
+ for(trav = solist; trav != NULL; trav = trav->next){
+ if (trav == si)
+ break;
+ prev = trav;
+ }
+ if (trav == NULL) {
+ /* si was not ni solist */
+ ERROR("%5d name %s is not in solist!\n", pid, si->name);
+ return;
+ }
+
+ /* prev will never be NULL, because the first entry in solist is
+ always the static libdl_info.
+ */
+ prev->next = si->next;
+ if (si == sonext) sonext = prev;
+ si->next = freelist;
+ freelist = si;
+}
+
+#ifndef LINKER_TEXT_BASE
+#error "linker's makefile must define LINKER_TEXT_BASE"
+#endif
+#ifndef LINKER_AREA_SIZE
+#error "linker's makefile must define LINKER_AREA_SIZE"
+#endif
+#define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000)
+#define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE))
+
+const char *addr_to_name(unsigned addr)
+{
+ soinfo *si;
+
+ for(si = solist; si != 0; si = si->next){
+ if((addr >= si->base) && (addr < (si->base + si->size))) {
+ return si->name;
+ }
+ }
+
+ if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){
+ return "linker";
+ }
+
+ return "";
+}
+
+/* For a given PC, find the .so that it belongs to.
+ * Returns the base address of the .ARM.exidx section
+ * for that .so, and the number of 8-byte entries
+ * in that section (via *pcount).
+ *
+ * Intended to be called by libc's __gnu_Unwind_Find_exidx().
+ *
+ * This function is exposed via dlfcn.c and libdl.so.
+ */
+#ifdef ANDROID_ARM_LINKER
+_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
+{
+ soinfo *si;
+ unsigned addr = (unsigned)pc;
+
+ if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) {
+ for (si = solist; si != 0; si = si->next){
+ if ((addr >= si->base) && (addr < (si->base + si->size))) {
+ *pcount = si->ARM_exidx_count;
+ return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
+ }
+ }
+ }
+ *pcount = 0;
+ return NULL;
+}
+#elif defined(ANDROID_X86_LINKER)
+/* Here, we only have to provide a callback to iterate across all the
+ * loaded libraries. gcc_eh does the rest. */
+int
+dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
+ void *data)
+{
+ soinfo *si;
+ struct dl_phdr_info dl_info;
+ int rv = 0;
+
+ for (si = solist; si != NULL; si = si->next) {
+ dl_info.dlpi_addr = si->linkmap.l_addr;
+ dl_info.dlpi_name = si->linkmap.l_name;
+ dl_info.dlpi_phdr = si->phdr;
+ dl_info.dlpi_phnum = si->phnum;
+ rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
+ if (rv != 0)
+ break;
+ }
+ return rv;
+}
+#endif
+
+static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
+{
+ Elf32_Sym *s;
+ Elf32_Sym *symtab = si->symtab;
+ const char *strtab = si->strtab;
+ unsigned n;
+
+ TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
+ name, si->name, si->base, hash, hash % si->nbucket);
+ n = hash % si->nbucket;
+
+ for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
+ s = symtab + n;
+ if(strcmp(strtab + s->st_name, name)) continue;
+
+ /* only concern ourselves with global symbols */
+ switch(ELF32_ST_BIND(s->st_info)){
+ case STB_GLOBAL:
+ /* no section == undefined */
+ if(s->st_shndx == 0) continue;
+
+ case STB_WEAK:
+ TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
+ name, si->name, s->st_value, s->st_size);
+ return s;
+ }
+ }
+
+ return 0;
+}
+
+static unsigned elfhash(const char *_name)
+{
+ const unsigned char *name = (const unsigned char *) _name;
+ unsigned h = 0, g;
+
+ while(*name) {
+ h = (h << 4) + *name++;
+ g = h & 0xf0000000;
+ h ^= g;
+ h ^= g >> 24;
+ }
+ return h;
+}
+
+static Elf32_Sym *
+_do_lookup_in_so(soinfo *si, const char *name, unsigned *elf_hash)
+{
+ if (*elf_hash == 0)
+ *elf_hash = elfhash(name);
+ return _elf_lookup (si, *elf_hash, name);
+}
+
+/* This is used by dl_sym() */
+Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
+{
+ unsigned unused = 0;
+ return _do_lookup_in_so(si, name, &unused);
+}
+
+static Elf32_Sym *
+_do_lookup(soinfo *user_si, const char *name, unsigned *base)
+{
+ unsigned elf_hash = 0;
+ Elf32_Sym *s = NULL;
+ soinfo *si;
+
+ /* Look for symbols in the local scope first (the object who is
+ * searching). This happens with C++ templates on i386 for some
+ * reason. */
+ if (user_si) {
+ s = _do_lookup_in_so(user_si, name, &elf_hash);
+ if (s != NULL)
+ *base = user_si->base;
+ }
+
+ for(si = solist; (s == NULL) && (si != NULL); si = si->next)
+ {
+ if((si->flags & FLAG_ERROR) || (si == user_si))
+ continue;
+ s = _do_lookup_in_so(si, name, &elf_hash);
+ if (s != NULL) {
+ *base = si->base;
+ break;
+ }
+ }
+
+ if (s != NULL) {
+ TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
+ "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
+ return s;
+ }
+
+ return 0;
+}
+
+/* This is used by dl_sym() */
+Elf32_Sym *lookup(const char *name, unsigned *base)
+{
+ return _do_lookup(NULL, name, base);
+}
+
+#if 0
+static void dump(soinfo *si)
+{
+ Elf32_Sym *s = si->symtab;
+ unsigned n;
+
+ for(n = 0; n < si->nchain; n++) {
+ TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
+ s->st_info, s->st_shndx, s->st_value, s->st_size,
+ si->strtab + s->st_name);
+ s++;
+ }
+}
+#endif
+
+static const char *sopaths[] = {
+ "/system/lib",
+ "/lib",
+ 0
+};
+
+static int open_library(const char *name)
+{
+ int fd;
+ char buf[512];
+ const char **path;
+
+ TRACE("[ %5d opening %s ]\n", pid, name);
+
+ if(strlen(name) > 256) return -1;
+ if(name == 0) return -1;
+
+ fd = open(name, O_RDONLY);
+ if(fd != -1) return fd;
+
+ for(path = sopaths; *path; path++){
+ sprintf(buf,"%s/%s", *path, name);
+ fd = open(buf, O_RDONLY);
+ if(fd != -1) return fd;
+ }
+
+ return -1;
+}
+
+static unsigned libbase = LIBBASE;
+
+/* temporary space for holding the first page of the shared lib
+ * which contains the elf header (with the pht). */
+static unsigned char __header[PAGE_SIZE];
+
+typedef struct {
+ long mmap_addr;
+ char tag[4]; /* 'P', 'R', 'E', ' ' */
+} prelink_info_t;
+
+/* Returns the requested base address if the library is prelinked,
+ * and 0 otherwise. */
+static unsigned long
+is_prelinked(int fd, const char *name)
+{
+ off_t sz;
+ prelink_info_t info;
+
+ sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
+ if (sz < 0) {
+ ERROR("lseek() failed!\n");
+ return 0;
+ }
+
+ if (read(fd, &info, sizeof(info)) != sizeof(info)) {
+ WARN("Could not read prelink_info_t structure for `%s`\n", name);
+ return 0;
+ }
+
+ if (strncmp(info.tag, "PRE ", 4)) {
+ WARN("`%s` is not a prelinked library\n", name);
+ return 0;
+ }
+
+ return (unsigned long)info.mmap_addr;
+}
+
+/* verify_elf_object
+ * Verifies if the object @ base is a valid ELF object
+ *
+ * Args:
+ *
+ * Returns:
+ * 0 on success
+ * -1 if no valid ELF object is found @ base.
+ */
+static int
+verify_elf_object(void *base, const char *name)
+{
+ Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
+
+ if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
+ if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
+ if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
+ if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
+
+ /* TODO: Should we verify anything else in the header? */
+
+ return 0;
+}
+
+
+/* get_lib_extents
+ * Retrieves the base (*base) address where the ELF object should be
+ * mapped and its overall memory size (*total_sz).
+ *
+ * Args:
+ * fd: Opened file descriptor for the library
+ * name: The name of the library
+ * _hdr: Pointer to the header page of the library
+ * total_sz: Total size of the memory that should be allocated for
+ * this library
+ *
+ * Returns:
+ * -1 if there was an error while trying to get the lib extents.
+ * The possible reasons are:
+ * - Could not determine if the library was prelinked.
+ * - The library provided is not a valid ELF object
+ * 0 if the library did not request a specific base offset (normal
+ * for non-prelinked libs)
+ * > 0 if the library requests a specific address to be mapped to.
+ * This indicates a pre-linked library.
+ */
+static unsigned
+get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
+{
+ unsigned req_base;
+ unsigned min_vaddr = 0xffffffff;
+ unsigned max_vaddr = 0;
+ unsigned char *_hdr = (unsigned char *)__hdr;
+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
+ Elf32_Phdr *phdr;
+ int cnt;
+
+ TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
+ if (verify_elf_object(_hdr, name) < 0) {
+ ERROR("%5d - %s is not a valid ELF object\n", pid, name);
+ return (unsigned)-1;
+ }
+
+ req_base = (unsigned) is_prelinked(fd, name);
+ if (req_base == (unsigned)-1)
+ return -1;
+ else if (req_base != 0) {
+ TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
+ pid, name, req_base);
+ } else {
+ TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
+ }
+
+ phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);
+
+ /* find the min/max p_vaddrs from all the PT_LOAD segments so we can
+ * get the range. */
+ for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
+ if (phdr->p_type == PT_LOAD) {
+ if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
+ max_vaddr = phdr->p_vaddr + phdr->p_memsz;
+ if (phdr->p_vaddr < min_vaddr)
+ min_vaddr = phdr->p_vaddr;
+ }
+ }
+
+ if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
+ ERROR("%5d - No loadable segments found in %s.\n", pid, name);
+ return (unsigned)-1;
+ }
+
+ /* truncate min_vaddr down to page boundary */
+ min_vaddr &= ~PAGE_MASK;
+
+ /* round max_vaddr up to the next page */
+ max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
+
+ *total_sz = (max_vaddr - min_vaddr);
+ return (unsigned)req_base;
+}
+
+/* alloc_mem_region
+ *
+ * This function reserves a chunk of memory to be used for mapping in
+ * the shared library. We reserve the entire memory region here, and
+ * then the rest of the linker will relocate the individual loadable
+ * segments into the correct locations within this memory range.
+ *
+ * Args:
+ * req_base: The requested base of the allocation. If 0, a sane one will be
+ * chosen in the range LIBBASE <= base < LIBLAST.
+ * sz: The size of the allocation.
+ *
+ * Returns:
+ * NULL on failure, and non-NULL pointer to memory region on success.
+ */
+static void *
+alloc_mem_region(const char *name, unsigned req_base, unsigned sz)
+{
+ void *base;
+
+ if (req_base) {
+ /* we should probably map it as PROT_NONE, but the init code needs
+ * to read the phdr, so mark everything as readable. */
+ base = mmap((void *)req_base, sz, PROT_READ | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (base == MAP_FAILED) {
+ WARN("%5d can NOT map (prelinked) library '%s' at 0x%08x "
+ "as requested, will try general pool: %d (%s)\n",
+ pid, name, req_base, errno, strerror(errno));
+ } else if (base != (void *)req_base) {
+ ERROR("OOPS: %5d prelinked library '%s' mapped at 0x%08x, "
+ "not at 0x%08x\n", pid, name, (unsigned)base, req_base);
+ munmap(base, sz);
+ return NULL;
+ }
+
+ /* Here we know that we got a valid allocation. Hooray! */
+ return base;
+ }
+
+ /* We either did not request a specific base address to map at
+ * (i.e. not-prelinked) OR we could not map at the requested address.
+ * Try to find a memory range in our "reserved" area that can be mapped.
+ */
+ while(libbase < LIBLAST) {
+ base = mmap((void*) libbase, sz, PROT_READ | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+ if(((unsigned)base) == libbase) {
+ /* success -- got the address we wanted */
+ return base;
+ }
+
+ /* If we got a different address than requested (rather than
+ * just a failure), we need to unmap the mismapped library
+ * before trying again
+ */
+ if(base != MAP_FAILED)
+ munmap(base, sz);
+
+ libbase += LIBINC;
+ }
+
+ ERROR("OOPS: %5d cannot map library '%s'. no vspace available.\n",
+ pid, name);
+ return NULL;
+}
+
+#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
+#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
+ MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
+ MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
+/* load_segments
+ *
+ * This function loads all the loadable (PT_LOAD) segments into memory
+ * at their appropriate memory offsets off the base address.
+ *
+ * Args:
+ * fd: Open file descriptor to the library to load.
+ * header: Pointer to a header page that contains the ELF header.
+ * This is needed since we haven't mapped in the real file yet.
+ * si: ptr to soinfo struct describing the shared object.
+ *
+ * Returns:
+ * 0 on success, -1 on failure.
+ */
+static int
+load_segments(int fd, void *header, soinfo *si)
+{
+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
+ Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
+ unsigned char *base = (unsigned char *)si->base;
+ int cnt;
+ unsigned len;
+ unsigned char *tmp;
+ unsigned char *pbase;
+ unsigned char *extra_base;
+ unsigned extra_len;
+ unsigned total_sz = 0;
+
+ si->wrprotect_start = 0xffffffff;
+ si->wrprotect_end = 0;
+
+ TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n",
+ pid, si->name, (unsigned)si->base);
+ /* Now go through all the PT_LOAD segments and map them into memory
+ * at the appropriate locations. */
+ for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
+ if (phdr->p_type == PT_LOAD) {
+ DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
+ /* we want to map in the segment on a page boundary */
+ tmp = base + (phdr->p_vaddr & (~PAGE_MASK));
+ /* add the # of bytes we masked off above to the total length. */
+ len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK);
+
+ TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x "
+ "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name,
+ (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
+ pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags),
+ MAP_PRIVATE | MAP_FIXED, fd,
+ phdr->p_offset & (~PAGE_MASK));
+ if (pbase == MAP_FAILED) {
+ ERROR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
+ "p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
+ (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
+ goto fail;
+ }
+
+ /* If 'len' didn't end on page boundary, and it's a writable
+ * segment, zero-fill the rest. */
+ if ((len & PAGE_MASK) && (phdr->p_flags & PF_W))
+ memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK));
+
+ /* Check to see if we need to extend the map for this segment to
+ * cover the diff between filesz and memsz (i.e. for bss).
+ *
+ * base _+---------------------+ page boundary
+ * . .
+ * | |
+ * . .
+ * pbase _+---------------------+ page boundary
+ * | |
+ * . .
+ * base + p_vaddr _| |
+ * . \ \ .
+ * . | filesz | .
+ * pbase + len _| / | |
+ * <0 pad> . . .
+ * extra_base _+------------|--------+ page boundary
+ * / . . .
+ * | . . .
+ * | +------------|--------+ page boundary
+ * extra_len-> | | | |
+ * | . | memsz .
+ * | . | .
+ * \ _| / |
+ * . .
+ * | |
+ * _+---------------------+ page boundary
+ */
+ tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) &
+ (~PAGE_MASK));
+ if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) {
+ extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp;
+ TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x "
+ "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len);
+ /* map in the extra page(s) as anonymous into the range.
+ * This is probably not necessary as we already mapped in
+ * the entire region previously, but we just want to be
+ * sure. This will also set the right flags on the region
+ * (though we can probably accomplish the same thing with
+ * mprotect).
+ */
+ extra_base = mmap((void *)tmp, extra_len,
+ PFLAGS_TO_PROT(phdr->p_flags),
+ MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
+ -1, 0);
+ if (extra_base == MAP_FAILED) {
+ ERROR("[ %5d - failed to extend segment from '%s' @ 0x%08x "
+ "(0x%08x) ]\n", pid, si->name, (unsigned)tmp,
+ extra_len);
+ goto fail;
+ }
+ /* TODO: Check if we need to memset-0 this region.
+ * Anonymous mappings are zero-filled copy-on-writes, so we
+ * shouldn't need to. */
+ TRACE("[ %5d - Segment from '%s' extended @ 0x%08x "
+ "(0x%08x)\n", pid, si->name, (unsigned)extra_base,
+ extra_len);
+ }
+ /* set the len here to show the full extent of the segment we
+ * just loaded, mostly for debugging */
+ len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz +
+ PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase;
+ TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x "
+ "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
+ (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset);
+ total_sz += len;
+ /* Make the section writable just in case we'll have to write to
+ * it during relocation (i.e. text segment). However, we will
+ * remember what range of addresses should be write protected.
+ *
+ */
+ if (!(phdr->p_flags & PF_W)) {
+ if ((unsigned)pbase < si->wrprotect_start)
+ si->wrprotect_start = (unsigned)pbase;
+ if (((unsigned)pbase + len) > si->wrprotect_end)
+ si->wrprotect_end = (unsigned)pbase + len;
+ mprotect(pbase, len,
+ PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE);
+ }
+ } else if (phdr->p_type == PT_DYNAMIC) {
+ DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
+ /* this segment contains the dynamic linking information */
+ si->dynamic = (unsigned *)(base + phdr->p_vaddr);
+ } else {
+#ifdef ANDROID_ARM_LINKER
+ if (phdr->p_type == PT_ARM_EXIDX) {
+ DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid);
+ /* exidx entries (used for stack unwinding) are 8 bytes each.
+ */
+ si->ARM_exidx = (unsigned *)phdr->p_vaddr;
+ si->ARM_exidx_count = phdr->p_memsz / 8;
+ }
+#endif
+ }
+
+ }
+
+ /* Sanity check */
+ if (total_sz > si->size) {
+ ERROR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
+ "greater than what was allocated (0x%08x). THIS IS BAD!\n",
+ pid, total_sz, si->name, si->size);
+ goto fail;
+ }
+
+ TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. "
+ "Total memory footprint: 0x%08x bytes ]\n", pid, si->name,
+ (unsigned)si->base, si->size);
+ return 0;
+
+fail:
+ /* We can just blindly unmap the entire region even though some things
+ * were mapped in originally with anonymous and others could have been
+ * been mapped in from the file before we failed. The kernel will unmap
+ * all the pages in the range, irrespective of how they got there.
+ */
+ munmap((void *)si->base, si->size);
+ si->flags |= FLAG_ERROR;
+ return -1;
+}
+
+/* TODO: Implement this to take care of the fact that Android ARM
+ * ELF objects shove everything into a single loadable segment that has the
+ * write bit set. wr_offset is then used to set non-(data|bss) pages to be
+ * non-writable.
+ */
+#if 0
+static unsigned
+get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr)
+{
+ Elf32_Shdr *shdr_start;
+ Elf32_Shdr *shdr;
+ int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr);
+ int cnt;
+ unsigned wr_offset = 0xffffffff;
+
+ shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd,
+ ehdr->e_shoff & (~PAGE_MASK));
+ if (shdr_start == MAP_FAILED) {
+ WARN("%5d - Could not read section header info from '%s'. Will not "
+ "not be able to determine write-protect offset.\n", pid, name);
+ return (unsigned)-1;
+ }
+
+ for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) {
+ if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) &&
+ (shdr->sh_addr < wr_offset)) {
+ wr_offset = shdr->sh_addr;
+ }
+ }
+
+ munmap(shdr_start, shdr_sz);
+ return wr_offset;
+}
+#endif
+
+static soinfo *
+load_library(const char *name)
+{
+ int fd = open_library(name);
+ int cnt;
+ unsigned ext_sz;
+ unsigned req_base;
+ void *base;
+ soinfo *si;
+ Elf32_Ehdr *hdr;
+
+ if(fd == -1)
+ return NULL;
+
+ /* We have to read the ELF header to figure out what to do with this image
+ */
+ if (lseek(fd, 0, SEEK_SET) < 0) {
+ ERROR("lseek() failed!\n");
+ goto fail;
+ }
+
+ if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
+ ERROR("read() failed!\n");
+ goto fail;
+ }
+
+ /* Parse the ELF header and get the size of the memory footprint for
+ * the library */
+ req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
+ if (req_base == (unsigned)-1)
+ goto fail;
+ TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
+ (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz);
+
+ /* Carve out a chunk of memory where we will map in the individual
+ * segments */
+ base = alloc_mem_region(name, req_base, ext_sz);
+ if (base == NULL)
+ goto fail;
+ TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
+ pid, name, base, (unsigned) ext_sz);
+
+ /* Now configure the soinfo struct where we'll store all of our data
+ * for the ELF object. If the loading fails, we waste the entry, but
+ * same thing would happen if we failed during linking. Configuring the
+ * soinfo struct here is a lot more convenient.
+ */
+ si = alloc_info(name);
+ if (si == NULL)
+ goto fail;
+
+ si->base = (unsigned)base;
+ si->size = ext_sz;
+ si->flags = 0;
+ si->entry = 0;
+ si->dynamic = (unsigned *)-1;
+
+ /* Now actually load the library's segments into right places in memory */
+ if (load_segments(fd, &__header[0], si) < 0)
+ goto fail;
+
+ /* this might not be right. Technically, we don't even need this info
+ * once we go through 'load_segments'. */
+ hdr = (Elf32_Ehdr *)base;
+ si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
+ si->phnum = hdr->e_phnum;
+ /**/
+
+ close(fd);
+ return si;
+
+fail:
+ close(fd);
+ return NULL;
+}
+
+static soinfo *
+init_library(soinfo *si)
+{
+ unsigned wr_offset = 0xffffffff;
+ unsigned libbase_before = 0;
+ unsigned libbase_after = 0;
+
+ /* At this point we know that whatever is loaded @ base is a valid ELF
+ * shared library whose segments are properly mapped in. */
+ TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
+ pid, si->base, si->size, si->name);
+
+ if (si->base < LIBBASE || si->base >= LIBLAST)
+ si->flags |= FLAG_PRELINKED;
+
+ /* Adjust libbase for the size of this library, rounded up to
+ ** LIBINC alignment. Make note of the previous and current
+ ** value of libbase to allow us to roll back in the event of
+ ** a link failure.
+ */
+ if (!(si->flags & FLAG_PRELINKED)) {
+ libbase_before = libbase;
+ libbase += (si->size + (LIBINC - 1)) & (~(LIBINC - 1));
+ libbase_after = libbase;
+ }
+
+ if(link_image(si, wr_offset)) {
+ /* We failed to link. However, we can only restore libbase
+ ** if no additional libraries have moved it since we updated it.
+ */
+ if(!(si->flags & FLAG_PRELINKED) && (libbase == libbase_after)) {
+ libbase = libbase_before;
+ }
+ munmap((void *)si->base, si->size);
+ return NULL;
+ }
+
+ return si;
+}
+
+soinfo *find_library(const char *name)
+{
+ soinfo *si;
+
+ for(si = solist; si != 0; si = si->next){
+ if(!strcmp(name, si->name)) {
+ if(si->flags & FLAG_ERROR) return 0;
+ if(si->flags & FLAG_LINKED) return si;
+ ERROR("OOPS: %5d recursive link to '%s'\n", pid, si->name);
+ return 0;
+ }
+ }
+
+ TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name);
+ si = load_library(name);
+ if(si == NULL)
+ return NULL;
+ return init_library(si);
+}
+
+/* TODO:
+ * notify gdb of unload
+ * for non-prelinked libraries, find a way to decrement libbase
+ */
+static void call_destructors(soinfo *si);
+unsigned unload_library(soinfo *si)
+{
+ unsigned *d;
+ if (si->refcount == 1) {
+ TRACE("%5d unloading '%s'\n", pid, si->name);
+ call_destructors(si);
+
+ for(d = si->dynamic; *d; d += 2) {
+ if(d[0] == DT_NEEDED){
+ TRACE("%5d %s needs to unload %s\n", pid,
+ si->name, si->strtab + d[1]);
+ soinfo *lsi = find_library(si->strtab + d[1]);
+ if(lsi)
+ unload_library(lsi);
+ else
+ ERROR("%5d could not unload '%s'\n",
+ pid, si->strtab + d[1]);
+ }
+ }
+
+ munmap((char *)si->base, si->size);
+ free_info(si);
+ si->refcount = 0;
+ }
+ else {
+ si->refcount--;
+ ERROR("%5d not unloading '%s', decrementing refcount to %d\n",
+ pid, si->name, si->refcount);
+ }
+ return si->refcount;
+}
+
+/* TODO: don't use unsigned for addrs below. It works, but is not
+ * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
+ * long.
+ */
+static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count)
+{
+ Elf32_Sym *symtab = si->symtab;
+ const char *strtab = si->strtab;
+ Elf32_Sym *s;
+ unsigned base;
+ Elf32_Rel *start = rel;
+ unsigned idx;
+
+ for (idx = 0; idx < count; ++idx) {
+ unsigned type = ELF32_R_TYPE(rel->r_info);
+ unsigned sym = ELF32_R_SYM(rel->r_info);
+ unsigned reloc = (unsigned)(rel->r_offset + si->base);
+ unsigned sym_addr = 0;
+ char *sym_name = NULL;
+
+ DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
+ si->name, idx);
+ if(sym != 0) {
+ s = _do_lookup(si, strtab + symtab[sym].st_name, &base);
+ if(s == 0) {
+ ERROR("%5d cannot locate '%s'...\n", pid, sym_name);
+ return -1;
+ }
+#if 0
+ if((base == 0) && (si->base != 0)){
+ /* linking from libraries to main image is bad */
+ ERROR("%5d cannot locate '%s'...\n",
+ pid, strtab + symtab[sym].st_name);
+ return -1;
+ }
+#endif
+ if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
+ ERROR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
+ "handle this yet\n", pid, si->name, s->st_shndx,
+ s->st_value);
+ return -1;
+ }
+ sym_addr = (unsigned)(s->st_value + base);
+ sym_name = (char *)(strtab + symtab[sym].st_name);
+ COUNT_RELOC(RELOC_SYMBOL);
+ } else {
+ s = 0;
+ }
+
+/* TODO: This is ugly. Split up the relocations by arch into
+ * different files.
+ */
+ switch(type){
+#if defined(ANDROID_ARM_LINKER)
+ case R_ARM_JUMP_SLOT:
+ case R_ARM_GLOB_DAT:
+ case R_ARM_ABS32:
+ COUNT_RELOC(RELOC_ABSOLUTE);
+ MARK(rel->r_offset);
+ TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
+ reloc, sym_addr, sym_name);
+ *((unsigned*)reloc) = sym_addr;
+ break;
+#elif defined(ANDROID_X86_LINKER)
+ case R_386_JUMP_SLOT:
+ COUNT_RELOC(RELOC_ABSOLUTE);
+ MARK(rel->r_offset);
+ TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
+ reloc, sym_addr, sym_name);
+ *((unsigned*)reloc) = sym_addr;
+ break;
+ case R_386_GLOB_DAT:
+ COUNT_RELOC(RELOC_ABSOLUTE);
+ MARK(rel->r_offset);
+ TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
+ reloc, sym_addr, sym_name);
+ *((unsigned*)reloc) = sym_addr;
+ break;
+#endif /* ANDROID_*_LINKER */
+
+#if defined(ANDROID_ARM_LINKER)
+ case R_ARM_RELATIVE:
+#elif defined(ANDROID_X86_LINKER)
+ case R_386_RELATIVE:
+#endif /* ANDROID_*_LINKER */
+ COUNT_RELOC(RELOC_RELATIVE);
+ MARK(rel->r_offset);
+ if(sym){
+ ERROR("%5d odd RELATIVE form...\n", pid);
+ return -1;
+ }
+ TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
+ reloc, si->base);
+ *((unsigned*)reloc) += si->base;
+ break;
+
+#if defined(ANDROID_X86_LINKER)
+ case R_386_32:
+ COUNT_RELOC(RELOC_RELATIVE);
+ MARK(rel->r_offset);
+
+ TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
+ reloc, sym_addr, sym_name);
+ *((unsigned *)reloc) += (unsigned)sym_addr;
+ break;
+
+ case R_386_PC32:
+ COUNT_RELOC(RELOC_RELATIVE);
+ MARK(rel->r_offset);
+ TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
+ "+%08x (%08x - %08x) %s\n", pid, reloc,
+ (sym_addr - reloc), sym_addr, reloc, sym_name);
+ *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
+ break;
+#endif /* ANDROID_X86_LINKER */
+
+#ifdef ANDROID_ARM_LINKER
+ case R_ARM_COPY:
+ COUNT_RELOC(RELOC_COPY);
+ MARK(rel->r_offset);
+ TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
+ reloc, s->st_size, sym_addr, sym_name);
+ memcpy((void*)reloc, (void*)sym_addr, s->st_size);
+ break;
+#endif /* ANDROID_ARM_LINKER */
+
+ default:
+ ERROR("%5d unknown reloc type %d @ %p (%d)\n",
+ pid, type, rel, (int) (rel - start));
+ return -1;
+ }
+ rel++;
+ }
+ return 0;
+}
+
+static void call_array(unsigned *ctor, int count)
+{
+ int n;
+ for(n = count; n > 0; n--){
+ TRACE("[ %5d Looking at ctor *0x%08x == 0x%08x ]\n", pid,
+ (unsigned)ctor, (unsigned)*ctor);
+ void (*func)() = (void (*)()) *ctor++;
+ if(((int) func == 0) || ((int) func == -1)) continue;
+ TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
+ func();
+ }
+}
+
+static void call_constructors(soinfo *si)
+{
+ /* TODO: THE ORIGINAL CODE SEEMED TO CALL THE INIT FUNCS IN THE WRONG ORDER.
+ * Old order: init, init_array, preinit_array..
+ * Correct order: preinit_array, init, init_array.
+ * Verify WHY.
+ */
+
+ if (si->flags & FLAG_EXE) {
+ TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
+ pid, (unsigned)si->preinit_array, si->preinit_array_count,
+ si->name);
+ call_array(si->preinit_array, si->preinit_array_count);
+ TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
+ } else {
+ if (si->preinit_array) {
+ ERROR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
+ " This is INVALID.\n", pid, si->name,
+ (unsigned)si->preinit_array);
+ }
+ }
+
+ // If we have an init section, then we should call it now, to make sure
+ // that all the funcs in the .ctors section get run.
+ // Note: For ARM, we shouldn't have a .ctor section (should be empty)
+ // when we have an (pre)init_array section, but let's be compatible with
+ // old (non-eabi) binaries and try the _init (DT_INIT) anyway.
+ if (si->init_func) {
+ TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
+ (unsigned)si->init_func, si->name);
+ si->init_func();
+ TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
+ }
+
+ if (si->init_array) {
+ TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
+ (unsigned)si->init_array, si->init_array_count, si->name);
+ call_array(si->init_array, si->init_array_count);
+ TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
+ }
+}
+
+static void call_destructors(soinfo *si)
+{
+ if (si->fini_array) {
+ TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
+ (unsigned)si->fini_array, si->fini_array_count, si->name);
+ call_array(si->fini_array, si->fini_array_count);
+ TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
+ }
+
+ // If we have an fini section, then we should call it now, to make sure
+ // that all the funcs in the .dtors section get run.
+ // Note: For ARM, we shouldn't have a .dtor section (should be empty)
+ // when we have an fini_array section, but let's be compatible with
+ // old (non-eabi) binaries and try the _fini (DT_FINI) anyway.
+ if (si->fini_func) {
+ TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
+ (unsigned)si->fini_func, si->name);
+ si->fini_func();
+ TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
+ }
+}
+
+/* Force any of the closed stdin, stdout and stderr to be associated with
+ /dev/null. */
+static int nullify_closed_stdio (void)
+{
+ int dev_null, i, status;
+ int return_value = 0;
+
+ dev_null = open("/dev/null", O_RDWR);
+ if (dev_null < 0) {
+ ERROR("Cannot open /dev/null.\n");
+ return -1;
+ }
+ TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
+
+ /* If any of the stdio file descriptors is valid and not associated
+ with /dev/null, dup /dev/null to it. */
+ for (i = 0; i < 3; i++) {
+ /* If it is /dev/null already, we are done. */
+ if (i == dev_null)
+ continue;
+
+ TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
+ /* The man page of fcntl does not say that fcntl(..,F_GETFL)
+ can be interrupted but we do this just to be safe. */
+ do {
+ status = fcntl(i, F_GETFL);
+ } while (status < 0 && errno == EINTR);
+
+ /* If file is openned, we are good. */
+ if (status >= 0)
+ continue;
+
+ /* The only error we allow is that the file descriptor does not
+ exist, in which case we dup /dev/null to it. */
+ if (errno != EBADF) {
+ ERROR("nullify_stdio: unhandled error %s\n", strerror(errno));
+ return_value = -1;
+ continue;
+ }
+
+ /* Try dupping /dev/null to this stdio file descriptor and
+ repeat if there is a signal. Note that any errors in closing
+ the stdio descriptor are lost. */
+ do {
+ status = dup2(dev_null, i);
+ } while (status < 0 && errno == EINTR);
+
+ if (status < 0) {
+ ERROR("nullify_stdio: dup2 error %s\n", strerror(errno));
+ return_value = -1;
+ continue;
+ }
+ }
+
+ /* If /dev/null is not one of the stdio file descriptors, close it. */
+ if (dev_null > 2) {
+ TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
+ do {
+ status = close(dev_null);
+ } while (status < 0 && errno == EINTR);
+
+ if (status < 0) {
+ ERROR("nullify_stdio: close error %s\n", strerror(errno));
+ return_value = -1;
+ }
+ }
+
+ return return_value;
+}
+
+static int link_image(soinfo *si, unsigned wr_offset)
+{
+ unsigned *d;
+ Elf32_Phdr *phdr = si->phdr;
+ int phnum = si->phnum;
+
+ INFO("[ %5d linking %s ]\n", pid, si->name);
+ DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
+ si->base, si->flags);
+
+ if (si->flags & FLAG_EXE) {
+ /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
+ * linkage info if this is the executable. If this was a
+ * dynamic lib, that would have been done at load time.
+ *
+ * TODO: It's unfortunate that small pieces of this are
+ * repeated from the load_library routine. Refactor this just
+ * slightly to reuse these bits.
+ */
+ si->size = 0;
+ for(; phnum > 0; --phnum, ++phdr) {
+#ifdef ANDROID_ARM_LINKER
+ if(phdr->p_type == PT_ARM_EXIDX) {
+ /* exidx entries (used for stack unwinding) are 8 bytes each.
+ */
+ si->ARM_exidx = (unsigned *)phdr->p_vaddr;
+ si->ARM_exidx_count = phdr->p_memsz / 8;
+ }
+#endif
+ if (phdr->p_type == PT_LOAD) {
+ /* For the executable, we use the si->size field only in
+ dl_unwind_find_exidx(), so the meaning of si->size
+ is not the size of the executable; it is the last
+ virtual address of the loadable part of the executable;
+ since si->base == 0 for an executable, we use the
+ range [0, si->size) to determine whether a PC value
+ falls within the executable section. Of course, if
+ a value is below phdr->p_vaddr, it's not in the
+ executable section, but a) we shouldn't be asking for
+ such a value anyway, and b) if we have to provide
+ an EXIDX for such a value, then the executable's
+ EXIDX is probably the better choice.
+ */
+ DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid);
+ if (phdr->p_vaddr + phdr->p_memsz > si->size)
+ si->size = phdr->p_vaddr + phdr->p_memsz;
+ /* try to remember what range of addresses should be write
+ * protected */
+ if (!(phdr->p_flags & PF_W)) {
+ unsigned _end;
+
+ if (phdr->p_vaddr < si->wrprotect_start)
+ si->wrprotect_start = phdr->p_vaddr;
+ _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) &
+ (~PAGE_MASK)));
+ if (_end > si->wrprotect_end)
+ si->wrprotect_end = _end;
+ }
+ } else if (phdr->p_type == PT_DYNAMIC) {
+ if (si->dynamic != (unsigned *)-1) {
+ ERROR("%5d multiple PT_DYNAMIC segments found in '%s'. "
+ "Segment at 0x%08x, previously one found at 0x%08x\n",
+ pid, si->name, si->base + phdr->p_vaddr,
+ (unsigned)si->dynamic);
+ goto fail;
+ }
+ DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid);
+ si->dynamic = (unsigned *) (si->base + phdr->p_vaddr);
+ }
+ }
+ }
+
+ if (si->dynamic == (unsigned *)-1) {
+ ERROR("%5d missing PT_DYNAMIC?!\n", pid);
+ goto fail;
+ }
+
+ DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
+
+ /* extract useful information from dynamic section */
+ for(d = si->dynamic; *d; d++){
+ DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
+ switch(*d++){
+ case DT_HASH:
+ si->nbucket = ((unsigned *) (si->base + *d))[0];
+ si->nchain = ((unsigned *) (si->base + *d))[1];
+ si->bucket = (unsigned *) (si->base + *d + 8);
+ si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4);
+ break;
+ case DT_STRTAB:
+ si->strtab = (const char *) (si->base + *d);
+ break;
+ case DT_SYMTAB:
+ si->symtab = (Elf32_Sym *) (si->base + *d);
+ break;
+ case DT_PLTREL:
+ if(*d != DT_REL) {
+ ERROR("DT_RELA not supported\n");
+ goto fail;
+ }
+ break;
+ case DT_JMPREL:
+ si->plt_rel = (Elf32_Rel*) (si->base + *d);
+ break;
+ case DT_PLTRELSZ:
+ si->plt_rel_count = *d / 8;
+ break;
+ case DT_REL:
+ si->rel = (Elf32_Rel*) (si->base + *d);
+ break;
+ case DT_RELSZ:
+ si->rel_count = *d / 8;
+ break;
+ case DT_PLTGOT:
+ /* Save this in case we decide to do lazy binding. We don't yet. */
+ si->plt_got = (unsigned *)(si->base + *d);
+ break;
+ case DT_DEBUG:
+ // Set the DT_DEBUG entry to the addres of _r_debug for GDB
+ *d = (int) &_r_debug;
+ break;
+ case DT_RELA:
+ ERROR("%5d DT_RELA not supported\n", pid);
+ goto fail;
+ case DT_INIT:
+ si->init_func = (void (*)(void))(si->base + *d);
+ DEBUG("%5d %s constructors (init func) found at %p\n",
+ pid, si->name, si->init_func);
+ break;
+ case DT_FINI:
+ si->fini_func = (void (*)(void))(si->base + *d);
+ DEBUG("%5d %s destructors (fini func) found at %p\n",
+ pid, si->name, si->fini_func);
+ break;
+ case DT_INIT_ARRAY:
+ si->init_array = (unsigned *)(si->base + *d);
+ DEBUG("%5d %s constructors (init_array) found at %p\n",
+ pid, si->name, si->init_array);
+ break;
+ case DT_INIT_ARRAYSZ:
+ si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
+ break;
+ case DT_FINI_ARRAY:
+ si->fini_array = (unsigned *)(si->base + *d);
+ DEBUG("%5d %s destructors (fini_array) found at %p\n",
+ pid, si->name, si->fini_array);
+ break;
+ case DT_FINI_ARRAYSZ:
+ si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
+ break;
+ case DT_PREINIT_ARRAY:
+ si->preinit_array = (unsigned *)(si->base + *d);
+ DEBUG("%5d %s constructors (preinit_array) found at %p\n",
+ pid, si->name, si->preinit_array);
+ break;
+ case DT_PREINIT_ARRAYSZ:
+ si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
+ break;
+ case DT_TEXTREL:
+ /* TODO: make use of this. */
+ /* this means that we might have to write into where the text
+ * segment was loaded during relocation... Do something with
+ * it.
+ */
+ DEBUG("%5d Text segment should be writable during relocation.\n",
+ pid);
+ break;
+ }
+ }
+
+ DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
+ pid, si->base, si->strtab, si->symtab);
+
+ if((si->strtab == 0) || (si->symtab == 0)) {
+ ERROR("%5d missing essential tables\n", pid);
+ goto fail;
+ }
+
+ for(d = si->dynamic; *d; d += 2) {
+ if(d[0] == DT_NEEDED){
+ DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
+ soinfo *lsi = find_library(si->strtab + d[1]);
+ if(lsi == 0) {
+ ERROR("%5d could not load '%s'\n", pid, si->strtab + d[1]);
+ goto fail;
+ }
+ lsi->refcount++;
+ }
+ }
+
+ if(si->plt_rel) {
+ DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
+ if(reloc_library(si, si->plt_rel, si->plt_rel_count))
+ goto fail;
+ }
+ if(si->rel) {
+ DEBUG("[ %5d relocating %s ]\n", pid, si->name );
+ if(reloc_library(si, si->rel, si->rel_count))
+ goto fail;
+ }
+
+ si->flags |= FLAG_LINKED;
+ DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
+
+#if 0
+ /* This is the way that the old dynamic linker did protection of
+ * non-writable areas. It would scan section headers and find where
+ * .text ended (rather where .data/.bss began) and assume that this is
+ * the upper range of the non-writable area. This is too coarse,
+ * and is kept here for reference until we fully move away from single
+ * segment elf objects. See the code in get_wr_offset (also #if'd 0)
+ * that made this possible.
+ */
+ if(wr_offset < 0xffffffff){
+ mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC);
+ }
+#else
+ /* TODO: Verify that this does the right thing in all cases, as it
+ * presently probably does not. It is possible that an ELF image will
+ * come with multiple read-only segments. What we ought to do is scan
+ * the program headers again and mprotect all the read-only segments.
+ * To prevent re-scanning the program header, we would have to build a
+ * list of loadable segments in si, and then scan that instead. */
+ if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) {
+ mprotect((void *)si->wrprotect_start,
+ si->wrprotect_end - si->wrprotect_start,
+ PROT_READ | PROT_EXEC);
+ }
+#endif
+
+ /* If this is a SETUID programme, dup /dev/null to openned stdin,
+ stdout and stderr to close a security hole described in:
+
+ ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
+
+ */
+ if (getuid() != geteuid())
+ nullify_closed_stdio ();
+ call_constructors(si);
+ notify_gdb_of_load(si);
+ return 0;
+
+fail:
+ ERROR("failed to link %s\n", si->name);
+ si->flags |= FLAG_ERROR;
+ return -1;
+}
+
+int main(int argc, char **argv)
+{
+ return 0;
+}
+
+#define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS
+
+static void * __tls_area[ANDROID_TLS_SLOTS];
+
+unsigned __linker_init(unsigned **elfdata)
+{
+ static soinfo linker_soinfo;
+
+ int argc = (int) *elfdata;
+ char **argv = (char**) (elfdata + 1);
+ unsigned *vecs = (unsigned*) (argv + argc + 1);
+ soinfo *si;
+ struct link_map * map;
+
+ pid = getpid();
+
+#if TIMING
+ struct timeval t0, t1;
+ gettimeofday(&t0, 0);
+#endif
+
+ __set_tls(__tls_area);
+ ((unsigned *)__get_tls())[TLS_SLOT_THREAD_ID] = gettid();
+
+ debugger_init();
+
+ /* skip past the environment */
+ while(vecs[0] != 0) {
+ if(!strncmp((char*) vecs[0], "DEBUG=", 6)) {
+ debug_verbosity = atoi(((char*) vecs[0]) + 6);
+ }
+ vecs++;
+ }
+ vecs++;
+
+ INFO("[ android linker & debugger ]\n");
+ DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
+
+ si = alloc_info(argv[0]);
+ if(si == 0) {
+ exit(-1);
+ }
+
+ /* bootstrap the link map, the main exe always needs to be first */
+ si->flags |= FLAG_EXE;
+ map = &(si->linkmap);
+
+ map->l_addr = 0;
+ map->l_name = argv[0];
+ map->l_prev = NULL;
+ map->l_next = NULL;
+
+ _r_debug.r_map = map;
+ r_debug_tail = map;
+
+ /* gdb expects the linker to be in the debug shared object list,
+ * and we need to make sure that the reported load address is zero.
+ * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
+ * is. Don't use alloc_info(), because the linker shouldn't
+ * be on the soinfo list.
+ */
+ strcpy((char*) linker_soinfo.name, "/system/bin/linker");
+ linker_soinfo.flags = 0;
+ linker_soinfo.base = 0; // This is the important part; must be zero.
+ insert_soinfo_into_debug_map(&linker_soinfo);
+
+ /* extract information passed from the kernel */
+ while(vecs[0] != 0){
+ switch(vecs[0]){
+ case AT_PHDR:
+ si->phdr = (Elf32_Phdr*) vecs[1];
+ break;
+ case AT_PHNUM:
+ si->phnum = (int) vecs[1];
+ break;
+ case AT_ENTRY:
+ si->entry = vecs[1];
+ break;
+ }
+ vecs += 2;
+ }
+
+ si->base = 0;
+ si->dynamic = (unsigned *)-1;
+ si->wrprotect_start = 0xffffffff;
+ si->wrprotect_end = 0;
+
+ if(link_image(si, 0)){
+ ERROR("CANNOT LINK EXECUTABLE '%s'\n", argv[0]);
+ exit(-1);
+ }
+
+#if TIMING
+ gettimeofday(&t1,NULL);
+ PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
+ (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
+ (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
+ ));
+#endif
+#if STATS
+ PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
+ linker_stats.reloc[RELOC_ABSOLUTE],
+ linker_stats.reloc[RELOC_RELATIVE],
+ linker_stats.reloc[RELOC_COPY],
+ linker_stats.reloc[RELOC_SYMBOL]);
+#endif
+#if COUNT_PAGES
+ {
+ unsigned n;
+ unsigned i;
+ unsigned count = 0;
+ for(n = 0; n < 4096; n++){
+ if(bitmask[n]){
+ unsigned x = bitmask[n];
+ for(i = 0; i < 8; i++){
+ if(x & 1) count++;
+ x >>= 1;
+ }
+ }
+ }
+ PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
+ }
+#endif
+
+#if TIMING || STATS || COUNT_PAGES
+ fflush(stdout);
+#endif
+
+ TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
+ si->entry);
+ return si->entry;
+}
diff --git a/linker/linker.h b/linker/linker.h
new file mode 100644
index 0000000..ab2c385
--- /dev/null
+++ b/linker/linker.h
@@ -0,0 +1,185 @@
+#ifndef _LINKER_H_
+#define _LINKER_H_
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <linux/elf.h>
+
+#undef PAGE_MASK
+#undef PAGE_SIZE
+#define PAGE_SIZE 4096
+#define PAGE_MASK 4095
+
+void debugger_init();
+const char *addr_to_name(unsigned addr);
+
+/* magic shared structures that GDB knows about */
+
+struct link_map
+{
+ uintptr_t l_addr;
+ char * l_name;
+ uintptr_t l_ld;
+ struct link_map * l_next;
+ struct link_map * l_prev;
+};
+
+/* needed for dl_iterate_phdr to be passed to the callbacks provided */
+struct dl_phdr_info
+{
+ Elf32_Addr dlpi_addr;
+ const char *dlpi_name;
+ const Elf32_Phdr *dlpi_phdr;
+ Elf32_Half dlpi_phnum;
+};
+
+
+// Values for r_debug->state
+enum {
+ RT_CONSISTENT,
+ RT_ADD,
+ RT_DELETE
+};
+
+struct r_debug
+{
+ int32_t r_version;
+ struct link_map * r_map;
+ void (*r_brk)(void);
+ int32_t r_state;
+ uintptr_t r_ldbase;
+};
+
+typedef struct soinfo soinfo;
+
+#define FLAG_LINKED 0x00000001
+#define FLAG_ERROR 0x00000002
+#define FLAG_EXE 0x00000004 // The main executable
+#define FLAG_PRELINKED 0x00000008 // This is a pre-linked lib
+
+#define SOINFO_NAME_LEN 128
+
+struct soinfo
+{
+ const char name[SOINFO_NAME_LEN];
+ Elf32_Phdr *phdr;
+ int phnum;
+ unsigned entry;
+ unsigned base;
+ unsigned size;
+
+ unsigned *dynamic;
+
+ unsigned wrprotect_start;
+ unsigned wrprotect_end;
+
+ soinfo *next;
+ unsigned flags;
+
+ const char *strtab;
+ Elf32_Sym *symtab;
+
+ unsigned nbucket;
+ unsigned nchain;
+ unsigned *bucket;
+ unsigned *chain;
+
+ unsigned *plt_got;
+
+ Elf32_Rel *plt_rel;
+ unsigned plt_rel_count;
+
+ Elf32_Rel *rel;
+ unsigned rel_count;
+
+ unsigned *preinit_array;
+ unsigned preinit_array_count;
+
+ unsigned *init_array;
+ unsigned init_array_count;
+ unsigned *fini_array;
+ unsigned fini_array_count;
+
+ void (*init_func)(void);
+ void (*fini_func)(void);
+
+#ifdef ANDROID_ARM_LINKER
+ /* ARM EABI section used for stack unwinding. */
+ unsigned *ARM_exidx;
+ unsigned ARM_exidx_count;
+#endif
+
+ unsigned refcount;
+ struct link_map linkmap;
+};
+
+
+extern soinfo libdl_info;
+
+/* these must all be powers of two */
+#define LIBBASE 0x80000000
+#define LIBLAST 0x90000000
+#define LIBINC 0x00100000
+
+
+#ifdef ANDROID_ARM_LINKER
+
+#define R_ARM_COPY 20
+#define R_ARM_GLOB_DAT 21
+#define R_ARM_JUMP_SLOT 22
+#define R_ARM_RELATIVE 23
+
+#elif defined(ANDROID_X86_LINKER)
+
+#define R_386_32 1
+#define R_386_PC32 2
+#define R_386_GLOB_DAT 6
+#define R_386_JUMP_SLOT 7
+#define R_386_RELATIVE 8
+
+#endif /* ANDROID_*_LINKER */
+
+
+#ifndef DT_INIT_ARRAY
+#define DT_INIT_ARRAY 25
+#endif
+
+#ifndef DT_FINI_ARRAY
+#define DT_FINI_ARRAY 26
+#endif
+
+#ifndef DT_INIT_ARRAYSZ
+#define DT_INIT_ARRAYSZ 27
+#endif
+
+#ifndef DT_FINI_ARRAYSZ
+#define DT_FINI_ARRAYSZ 28
+#endif
+
+#ifndef DT_PREINIT_ARRAY
+#define DT_PREINIT_ARRAY 32
+#endif
+
+#ifndef DT_PREINIT_ARRAYSZ
+#define DT_PREINIT_ARRAYSZ 33
+#endif
+
+/* in theory we only need the above relative relocations,
+ but in practice the following one turns up from time
+ to time. fushigi na.
+*/
+#define R_ARM_ABS32 2
+
+soinfo *find_library(const char *name);
+unsigned unload_library(soinfo *si);
+Elf32_Sym *lookup_in_library(soinfo *si, const char *name);
+Elf32_Sym *lookup(const char *name, unsigned *base);
+
+#ifdef ANDROID_ARM_LINKER
+typedef long unsigned int *_Unwind_Ptr;
+_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
+#elif defined(ANDROID_X86_LINKER)
+int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, size_t, void *), void *);
+#endif
+
+#endif
diff --git a/linker/linker_debug.h b/linker/linker_debug.h
new file mode 100644
index 0000000..10ae7b1
--- /dev/null
+++ b/linker/linker_debug.h
@@ -0,0 +1,111 @@
+#ifndef _LINKER_DEBUG_H_
+#define _LINKER_DEBUG_H_
+
+#include <stdio.h>
+
+/* WARNING: For linker debugging only.. Be careful not to leave any of
+ * this on when submitting back to repository */
+#define LINKER_DEBUG 0
+#define TRACE_DEBUG 0
+#define DO_TRACE_LOOKUP 1
+#define DO_TRACE_RELO 1
+#define TIMING 0
+#define STATS 0
+#define COUNT_PAGES 0
+
+/*********************************************************************
+ * You shouldn't need to modify anything below unless you are adding
+ * more debugging information.
+ *
+ * To enable/disable specific debug options, change the defines above
+ *********************************************************************/
+
+
+/*********************************************************************/
+#undef TRUE
+#undef FALSE
+#define TRUE 1
+#define FALSE 0
+
+/* Only use printf() during debugging. We have seen occasional memory
+ * corruption when the linker uses printf().
+ */
+#if LINKER_DEBUG
+extern int debug_verbosity;
+#warning "*** LINKER IS USING printf(); DO NOT CHECK THIS IN ***"
+#define _PRINTVF(v,f,x...) do { \
+ (debug_verbosity > (v)) && (printf(x), ((f) && fflush(stdout))); \
+ } while (0)
+#else /* !LINKER_DEBUG */
+#define _PRINTVF(v,f,x...) do {} while(0)
+#endif /* LINKER_DEBUG */
+
+#define PRINT(x...) _PRINTVF(-1, FALSE, x)
+#define INFO(x...) _PRINTVF(0, TRUE, x)
+#define TRACE(x...) _PRINTVF(1, TRUE, x)
+#define WARN(fmt,args...) \
+ _PRINTVF(-1, TRUE, "%s:%d| WARNING: " fmt, __FILE__, __LINE__, ## args)
+#define ERROR(fmt,args...) \
+ _PRINTVF(-1, TRUE, "%s:%d| ERROR: " fmt, __FILE__, __LINE__, ## args)
+
+#if TRACE_DEBUG
+#define DEBUG(x...) _PRINTVF(2, TRUE, "DEBUG: " x)
+#else /* !TRACE_DEBUG */
+#define DEBUG(x...) do {} while (0)
+#endif /* TRACE_DEBUG */
+
+#if LINKER_DEBUG
+#define TRACE_TYPE(t,x...) do { if (DO_TRACE_##t) { TRACE(x); } } while (0)
+#else /* !LINKER_DEBUG */
+#define TRACE_TYPE(t,x...) do {} while (0)
+#endif /* LINKER_DEBUG */
+
+#if STATS
+#define RELOC_ABSOLUTE 0
+#define RELOC_RELATIVE 1
+#define RELOC_COPY 2
+#define RELOC_SYMBOL 3
+#define NUM_RELOC_STATS 4
+
+struct _link_stats {
+ int reloc[NUM_RELOC_STATS];
+};
+extern struct _link_stats linker_stats;
+
+#define COUNT_RELOC(type) \
+ do { if (type >= 0 && type < NUM_RELOC_STATS) { \
+ linker_stats.reloc[type] += 1; \
+ } else { \
+ PRINT("Unknown reloc stat requested\n"); \
+ } \
+ } while(0)
+#else /* !STATS */
+#define COUNT_RELOC(type) do {} while(0)
+#endif /* STATS */
+
+#if TIMING
+#undef WARN
+#define WARN(x...) do {} while (0)
+#endif /* TIMING */
+
+#if COUNT_PAGES
+extern unsigned bitmask[];
+#define MARK(offset) do { \
+ bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
+ } while(0)
+#else
+#define MARK(x) do {} while (0)
+#endif
+
+#define DEBUG_DUMP_PHDR(phdr, name, pid) do { \
+ DEBUG("%5d %s (phdr = 0x%08x)\n", (pid), (name), (unsigned)(phdr)); \
+ DEBUG("\t\tphdr->offset = 0x%08x\n", (unsigned)((phdr)->p_offset)); \
+ DEBUG("\t\tphdr->p_vaddr = 0x%08x\n", (unsigned)((phdr)->p_vaddr)); \
+ DEBUG("\t\tphdr->p_paddr = 0x%08x\n", (unsigned)((phdr)->p_paddr)); \
+ DEBUG("\t\tphdr->p_filesz = 0x%08x\n", (unsigned)((phdr)->p_filesz)); \
+ DEBUG("\t\tphdr->p_memsz = 0x%08x\n", (unsigned)((phdr)->p_memsz)); \
+ DEBUG("\t\tphdr->p_flags = 0x%08x\n", (unsigned)((phdr)->p_flags)); \
+ DEBUG("\t\tphdr->p_align = 0x%08x\n", (unsigned)((phdr)->p_align)); \
+ } while (0)
+
+#endif /* _LINKER_DEBUG_H_ */
diff --git a/linker/rt.c b/linker/rt.c
new file mode 100644
index 0000000..d0802ce
--- /dev/null
+++ b/linker/rt.c
@@ -0,0 +1,8 @@
+/*
+ * This function is an empty stub where GDB locates a breakpoint to get notified
+ * about linker activity.
+ */
+void __attribute__((noinline)) rtld_db_dlactivity(void)
+{
+}
+