diff options
author | Shilpa Gopal <c_shutta@quicinc.com> | 2009-08-01 16:05:30 -0700 |
---|---|---|
committer | Shilpa Gopal <c_shutta@quicinc.com> | 2009-08-03 20:13:31 -0700 |
commit | 172f4ca290926afd242eef207a7f8205c897afe0 (patch) | |
tree | de624f8e840e8782deb60b574fb3368f7b94f0a1 | |
parent | 47f1e2857eed0b542492dfa35580823e0a45f46c (diff) | |
parent | 6ee8f1b0444c0db94931d2cd64427ded8fba38b0 (diff) | |
download | bionic-172f4ca290926afd242eef207a7f8205c897afe0.zip bionic-172f4ca290926afd242eef207a7f8205c897afe0.tar.gz bionic-172f4ca290926afd242eef207a7f8205c897afe0.tar.bz2 |
Merge commit 'quic/korg/donut' into donut
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | libc/Android.mk | 218 | ||||
-rw-r--r-- | libc/bionic/eabi.c | 8 | ||||
-rw-r--r-- | libc/docs/ISSUES.TXT | 20 | ||||
-rw-r--r-- | libc/include/sys/stat.h | 7 | ||||
-rw-r--r-- | libc/kernel/common/linux/if_pppolac.h | 29 | ||||
-rw-r--r-- | libc/kernel/common/linux/if_pppopns.h | 28 | ||||
-rw-r--r-- | libc/kernel/common/linux/ipsec.h | 56 | ||||
-rwxr-xr-x | libc/kernel/tools/update_all.py | 4 | ||||
-rw-r--r-- | libc/netbsd/net/getservent.c | 9 | ||||
-rw-r--r-- | libc/stdlib/atexit.c | 9 | ||||
-rw-r--r-- | libstdc++/include/cmath | 71 | ||||
-rw-r--r-- | linker/Android.mk | 2 | ||||
-rw-r--r-- | linker/README.TXT | 114 | ||||
-rw-r--r-- | linker/dlfcn.c | 43 | ||||
-rw-r--r-- | linker/linker.c | 230 | ||||
-rw-r--r-- | linker/linker.h | 1 | ||||
-rw-r--r-- | linker/linker_debug.h | 14 |
18 files changed, 655 insertions, 210 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0db5b03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +libc/kernel/original diff --git a/libc/Android.mk b/libc/Android.mk index c0773cc..457e7dd 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -1,6 +1,9 @@ LOCAL_PATH:= $(call my-dir) include $(LOCAL_PATH)/arch-$(TARGET_ARCH)/syscalls.mk + +# Define the common source files for all the libc instances +# ========================================================= libc_common_src_files := \ $(syscall_src) \ unistd/abort.c \ @@ -272,6 +275,8 @@ libc_common_src_files := \ netbsd/nameser/ns_print.c \ netbsd/nameser/ns_samedomain.c +# Architecture specific source files go here +# ========================================================= ifeq ($(TARGET_ARCH),arm) libc_common_src_files += \ bionic/eabi.c \ @@ -302,6 +307,14 @@ libc_common_src_files += \ bionic/pthread.c.arm \ bionic/pthread-timers.c.arm \ bionic/ptrace.c.arm + +# these are used by the static and dynamic versions of the libc +# respectively +libc_arch_static_src_files := \ + arch-arm/bionic/exidx_static.c + +libc_arch_dynamic_src_files := \ + arch-arm/bionic/exidx_dynamic.c else # !arm ifeq ($(TARGET_ARCH),x86) @@ -323,10 +336,17 @@ libc_common_src_files += \ bionic/pthread.c \ bionic/pthread-timers.c \ bionic/ptrace.c -endif # x86 +# this is needed for static versions of libc +libc_arch_static_src_files := \ + arch-x86/bionic/dl_iterate_phdr_static.c + +libc_arch_dynamic_src_files := +endif # x86 endif # !arm +# Define some common cflags +# ======================================================== libc_common_cflags := \ -DWITH_ERRLIST \ -DANDROID_CHANGES \ @@ -338,6 +358,7 @@ libc_common_cflags := \ -DNEED_PSELECT=1 \ -DINET6 \ -I$(LOCAL_PATH)/private \ + -DUSE_DL_PREFIX ifeq ($(strip $(DEBUG_BIONIC_LIBC)),true) libc_common_cflags += -DDEBUG @@ -345,159 +366,154 @@ endif ifeq ($(TARGET_ARCH),arm) libc_common_cflags += -fstrict-aliasing -endif + libc_crt_target_cflags := -mthumb-interwork +else # !arm + ifeq ($(TARGET_ARCH),x86) + libc_crt_target_cflags := -m32 + endif # x86 +endif # !arm ifneq (, $(filter msm7630_surf msm7630_ffa qsd8250_surf qsd8250_ffa, $(TARGET_PRODUCT))) libc_common_cflags += -DCOPROC_TLS=1 endif +# Define some common includes +# ======================================================== libc_common_c_includes := \ $(LOCAL_PATH)/stdlib \ $(LOCAL_PATH)/string \ $(LOCAL_PATH)/stdio -# libc_common.a -# ======================================================== - -include $(CLEAR_VARS) -LOCAL_SRC_FILES := $(libc_common_src_files) -LOCAL_CFLAGS := $(libc_common_cflags) -DUSE_DL_PREFIX -LOCAL_C_INCLUDES := $(libc_common_c_includes) +# Define the libc run-time (crt) support object files that must be built, +# which are needed to build all other objects (shared/static libs and +# executables) +# ========================================================================== -ifneq ($(TARGET_SIMULATOR),true) - ifeq ($(TARGET_ARCH),arm) - crtend_target_cflags := -mthumb-interwork - else - ifeq ($(TARGET_ARCH),x86) - crtend_target_cflags := -m32 - endif - endif -# We rename crtend.o to crtend_android.o to avoid a -# name clash between gcc and bionic. -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_android.o -$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend.S +ifeq ($(TARGET_ARCH),x86) +# we only need begin_so/end_so for x86, since it needs an appropriate .init +# section in the shared library with a function to call all the entries in +# .ctors section. ARM uses init_array, and does not need the function. +GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_so.o +$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_so.S @mkdir -p $(dir $@) - $(TARGET_CC) $(crtend_target_cflags) -o $@ -c $< + $(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $< ALL_GENERATED_SOURCES += $(GEN) -endif +GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_so.o +$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend_so.S + @mkdir -p $(dir $@) + $(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $< +ALL_GENERATED_SOURCES += $(GEN) +endif # TARGET_ARCH == x86 -# crtbegin_so.o/crtend_so.o -# These are needed for building the shared libs. -ifneq ($(TARGET_SIMULATOR),true) - -ifeq ($(TARGET_ARCH),x86) -crt_begin_end_so_target_cflags := -m32 +GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_static.o +$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_static.S + @mkdir -p $(dir $@) + $(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $< +ALL_GENERATED_SOURCES += $(GEN) -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_so.o -$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_so.S +GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_dynamic.o +$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_dynamic.S @mkdir -p $(dir $@) - $(TARGET_CC) $(crt_begin_end_so_target_cflags) -o $@ -c $< + $(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $< ALL_GENERATED_SOURCES += $(GEN) -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_so.o -$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend_so.S + +# We rename crtend.o to crtend_android.o to avoid a +# name clash between gcc and bionic. +GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_android.o +$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend.S @mkdir -p $(dir $@) - $(TARGET_CC) $(crt_begin_end_so_target_cflags) -o $@ -c $< + $(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $< ALL_GENERATED_SOURCES += $(GEN) -endif # TARGET_ARCH == x86 -endif # !TARGET_SIMULATOR +# To enable malloc leak check for statically linked programs, add +# "WITH_MALLOC_CHECK_LIBC_A := true" to buildspec.mk +WITH_MALLOC_CHECK_LIBC_A := $(strip $(WITH_MALLOC_CHECK_LIBC_A)) +# ======================================================== +# libc_common.a +# ======================================================== +include $(CLEAR_VARS) +LOCAL_SRC_FILES := $(libc_common_src_files) +LOCAL_CFLAGS := $(libc_common_cflags) +LOCAL_C_INCLUDES := $(libc_common_c_includes) LOCAL_MODULE := libc_common LOCAL_SYSTEM_SHARED_LIBRARIES := include $(BUILD_STATIC_LIBRARY) -# libc.a # ======================================================== +# libc_nomalloc.a +# ======================================================== +# +# This is a version of the static C library that does not +# include malloc. It's useful in situations when calling +# the user wants to provide their own malloc implementation, +# or wants to explicitly disallow the use of the use of malloc, +# like the dynamic loader. include $(CLEAR_VARS) -include $(LOCAL_PATH)/arch-$(TARGET_ARCH)/syscalls.mk - -# To enable malloc leak check for statically linked programs, add -# "WITH_MALLOC_CHECK_LIBC_A := true" to device/buildspec.mk -WITH_MALLOC_CHECK_LIBC_A := $(strip $(WITH_MALLOC_CHECK_LIBC_A)) - LOCAL_SRC_FILES := \ - $(libc_common_src_files) \ - bionic/dlmalloc.c \ + $(libc_arch_static_src_files) \ bionic/libc_init_static.c -ifeq ($(WITH_MALLOC_CHECK_LIBC_A),true) - LOCAL_SRC_FILES += bionic/malloc_leak.c.arm -endif +LOCAL_C_INCLUDES := $(libc_common_c_includes) +LOCAL_CFLAGS := $(libc_common_cflags) -ifeq ($(TARGET_ARCH),arm) -LOCAL_SRC_FILES += \ - arch-arm/bionic/exidx_static.c +LOCAL_MODULE := libc_nomalloc +LOCAL_WHOLE_STATIC_LIBRARIES := libc_common +LOCAL_SYSTEM_SHARED_LIBRARIES := -else # TARGET_ARCH != arm +include $(BUILD_STATIC_LIBRARY) -ifeq ($(TARGET_ARCH),x86) -LOCAL_SRC_FILES += \ - arch-x86/bionic/dl_iterate_phdr_static.c -endif -endif +# ======================================================== +# libc.a +# ======================================================== +include $(CLEAR_VARS) -ifneq ($(TARGET_SIMULATOR),true) - ifeq ($(TARGET_ARCH),arm) - crtbegin_static_target_cflags := -mthumb-interwork - else - ifeq ($(TARGET_ARCH),x86) - crtbegin_static_target_cflags := -m32 - endif - endif -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_static.o -$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_static.S - @mkdir -p $(dir $@) - $(TARGET_CC) $(crtbegin_static_target_cflags) -o $@ -c $< -ALL_GENERATED_SOURCES += $(GEN) -endif +LOCAL_SRC_FILES := \ + $(libc_arch_static_src_files) \ + bionic/dlmalloc.c \ + bionic/libc_init_static.c LOCAL_CFLAGS := $(libc_common_cflags) -LOCAL_C_INCLUDES := $(libc_common_c_includes) - ifeq ($(WITH_MALLOC_CHECK_LIBC_A),true) - LOCAL_CFLAGS += -DUSE_DL_PREFIX -DMALLOC_LEAK_CHECK + LOCAL_CFLAGS += -DMALLOC_LEAK_CHECK + LOCAL_SRC_FILES += bionic/malloc_leak.c.arm endif +LOCAL_C_INCLUDES := $(libc_common_c_includes) + +LOCAL_MODULE := libc LOCAL_WHOLE_STATIC_LIBRARIES := libc_common -LOCAL_MODULE:= libc LOCAL_SYSTEM_SHARED_LIBRARIES := include $(BUILD_STATIC_LIBRARY) +# ======================================================== # libc.so # ======================================================== - include $(CLEAR_VARS) LOCAL_CFLAGS := $(libc_common_cflags) - -LOCAL_CFLAGS += -DUSE_DL_PREFIX - LOCAL_C_INCLUDES := $(libc_common_c_includes) LOCAL_SRC_FILES := \ + $(libc_arch_dynamic_src_files) \ bionic/dlmalloc.c \ bionic/malloc_leak.c.arm \ bionic/libc_init_dynamic.c -ifeq ($(TARGET_ARCH),arm) -LOCAL_SRC_FILES += \ - arch-arm/bionic/exidx_dynamic.c -endif - LOCAL_MODULE:= libc # WARNING: The only library libc.so should depend on is libdl.so! If you add other libraries, @@ -512,45 +528,26 @@ LOCAL_SHARED_LIBRARIES := libdl LOCAL_WHOLE_STATIC_LIBRARIES := libc_common LOCAL_SYSTEM_SHARED_LIBRARIES := -ifneq ($(TARGET_SIMULATOR),true) - ifeq ($(TARGET_ARCH),arm) - crtbegin_dynamic_target_cflags := -mthumb-interwork - else - ifeq ($(TARGET_ARCH),x86) - crtbegin_dynamic_target_cflags := -m32 - endif - endif -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_dynamic.o -$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_dynamic.S - @mkdir -p $(dir $@) - $(TARGET_CC) $(crtbegin_dynamic_target_cflags) -o $@ -c $< -ALL_GENERATED_SOURCES += $(GEN) -endif - include $(BUILD_SHARED_LIBRARY) +# ======================================================== # libc_debug.so # ======================================================== - include $(CLEAR_VARS) -LOCAL_CFLAGS := $(libc_common_cflags) - -LOCAL_CFLAGS += -DUSE_DL_PREFIX -DMALLOC_LEAK_CHECK +LOCAL_CFLAGS := \ + $(libc_common_cflags) \ + -DMALLOC_LEAK_CHECK LOCAL_C_INCLUDES := $(libc_common_c_includes) LOCAL_SRC_FILES := \ + $(libc_arch_dynamic_src_files) \ bionic/dlmalloc.c \ bionic/malloc_leak.c.arm \ bionic/libc_init_dynamic.c -ifeq ($(TARGET_ARCH),arm) -LOCAL_SRC_FILES += \ - arch-arm/bionic/exidx_dynamic.c -endif - LOCAL_MODULE:= libc_debug # WARNING: The only library libc.so should depend on is libdl.so! If you add other libraries, @@ -569,9 +566,6 @@ LOCAL_PRELINK_MODULE := false # Don't install on release build LOCAL_MODULE_TAGS := eng -GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_dynamic.o -ALL_GENERATED_SOURCES += $(GEN) - include $(BUILD_SHARED_LIBRARY) # ======================================================== diff --git a/libc/bionic/eabi.c b/libc/bionic/eabi.c index 21d8537..a5f6627 100644 --- a/libc/bionic/eabi.c +++ b/libc/bionic/eabi.c @@ -32,6 +32,14 @@ extern int __cxa_atexit(void (*)(void*), void*, void* ); void* __dso_handle = 0; +/* The "C++ ABI for ARM" document states that static C++ constructors, + * which are called from the .init_array, should manually call + * __aeabi_atexit() to register static destructors explicitely. + * + * Note that 'dso_handle' is the address of a magic linker-generate + * variable from the shared object that contains the constructor/destructor + */ + /* Make this a weak symbol to avoid a multiple definition error when linking * with libstdc++-v3. */ int __attribute__((weak)) diff --git a/libc/docs/ISSUES.TXT b/libc/docs/ISSUES.TXT new file mode 100644 index 0000000..b53eb16 --- /dev/null +++ b/libc/docs/ISSUES.TXT @@ -0,0 +1,20 @@ +Bionic C Library Issues: +======================== + +This document lists important known issues of various releases +of the Bionic C library. Note that these differ from specific +implementation choices that are documented in the OVERVIEW.TXT +document. + +Currently known issues: +----------------------- + +- The C library initialization will improperly run static C++ + constructors located in shared libraries twice. + +Android-1.5 issues: +------------------- + +- getservbyname() returned the port number in the s_port field of + the 'struct servent' structure in host-byte-order, instead of + network-byte-order. diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h index 23ab5ae..091ee6d 100644 --- a/libc/include/sys/stat.h +++ b/libc/include/sys/stat.h @@ -71,6 +71,13 @@ struct stat { unsigned long long st_ino; }; +/* For compatibility with GLibc, we provide macro aliases + * for the non-Posix nano-seconds accessors. + */ +#define st_atimensec st_atime_nsec +#define st_mtimensec st_mtime_nsec +#define st_ctimensec st_ctime_nsec + extern int chmod(const char *, mode_t); extern int fchmod(int, mode_t); extern int mkdir(const char *, mode_t); diff --git a/libc/kernel/common/linux/if_pppolac.h b/libc/kernel/common/linux/if_pppolac.h new file mode 100644 index 0000000..bf6eba0 --- /dev/null +++ b/libc/kernel/common/linux/if_pppolac.h @@ -0,0 +1,29 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef __LINUX_IF_PPPOLAC_H +#define __LINUX_IF_PPPOLAC_H + +#include <linux/socket.h> +#include <linux/types.h> + +#define PX_PROTO_OLAC 2 + +struct sockaddr_pppolac { + sa_family_t sa_family; + unsigned int sa_protocol; + int udp_socket; + struct __attribute__((packed)) { + __u16 tunnel, session; + } local, remote; +} __attribute__((packed)); + +#endif diff --git a/libc/kernel/common/linux/if_pppopns.h b/libc/kernel/common/linux/if_pppopns.h new file mode 100644 index 0000000..ac75210 --- /dev/null +++ b/libc/kernel/common/linux/if_pppopns.h @@ -0,0 +1,28 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef __LINUX_IF_PPPOPNS_H +#define __LINUX_IF_PPPOPNS_H + +#include <linux/socket.h> +#include <linux/types.h> + +#define PX_PROTO_OPNS 3 + +struct sockaddr_pppopns { + sa_family_t sa_family; + unsigned int sa_protocol; + int tcp_socket; + __u16 local; + __u16 remote; +} __attribute__((packed)); + +#endif diff --git a/libc/kernel/common/linux/ipsec.h b/libc/kernel/common/linux/ipsec.h new file mode 100644 index 0000000..56acafa --- /dev/null +++ b/libc/kernel/common/linux/ipsec.h @@ -0,0 +1,56 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + **************************************************************************** + ****************************************************************************/ +#ifndef _LINUX_IPSEC_H +#define _LINUX_IPSEC_H + +#include <linux/pfkeyv2.h> + +#define IPSEC_PORT_ANY 0 +#define IPSEC_ULPROTO_ANY 255 +#define IPSEC_PROTO_ANY 255 + +enum { + IPSEC_MODE_ANY = 0, + IPSEC_MODE_TRANSPORT = 1, + IPSEC_MODE_TUNNEL = 2, + IPSEC_MODE_BEET = 3 +}; + +enum { + IPSEC_DIR_ANY = 0, + IPSEC_DIR_INBOUND = 1, + IPSEC_DIR_OUTBOUND = 2, + IPSEC_DIR_FWD = 3, + IPSEC_DIR_MAX = 4, + IPSEC_DIR_INVALID = 5 +}; + +enum { + IPSEC_POLICY_DISCARD = 0, + IPSEC_POLICY_NONE = 1, + IPSEC_POLICY_IPSEC = 2, + IPSEC_POLICY_ENTRUST = 3, + IPSEC_POLICY_BYPASS = 4 +}; + +enum { + IPSEC_LEVEL_DEFAULT = 0, + IPSEC_LEVEL_USE = 1, + IPSEC_LEVEL_REQUIRE = 2, + IPSEC_LEVEL_UNIQUE = 3 +}; + +#define IPSEC_MANUAL_REQID_MAX 0x3fff + +#define IPSEC_REPLAYWSIZE 32 + +#endif diff --git a/libc/kernel/tools/update_all.py b/libc/kernel/tools/update_all.py index 6272fcf..d25dc0e 100755 --- a/libc/kernel/tools/update_all.py +++ b/libc/kernel/tools/update_all.py @@ -73,7 +73,9 @@ for path in sources: print "cleaning: %-*s -> %-*s (%s)" % ( 35, path, 35, dst_path, r ) -usePerforce = os.environ.has_key("ANDROID_PRODUCT_OUT") +# We don't use Perforce anymore, but just in case, define ANDROID_USE_P4 +# in your environment if you think you need it. +usePerforce = os.environ.has_key("ANDROID_USE_P4") if usePerforce: b.updateP4Files() diff --git a/libc/netbsd/net/getservent.c b/libc/netbsd/net/getservent.c index 45207df..65fbd7e 100644 --- a/libc/netbsd/net/getservent.c +++ b/libc/netbsd/net/getservent.c @@ -27,6 +27,7 @@ */ #include <sys/cdefs.h> #include <sys/types.h> +#include <sys/endian.h> #include <netdb.h> #include "servent.h" #include "services.h" @@ -54,6 +55,7 @@ getservent_r( res_static rs ) int namelen; int nn,count; int total = 0; + int port; char* p2; p = rs->servent_ptr; @@ -92,9 +94,12 @@ getservent_r( res_static rs ) memcpy( rs->servent.s_name, p+1, namelen ); rs->servent.s_name[namelen] = 0; p += 1 + namelen; - rs->servent.s_port = ((((unsigned char*)p)[0] << 8) | - ((unsigned char*)p)[1]); + /* s_port must be in network byte order */ + port = ((((unsigned char*)p)[0] << 8) | + ((unsigned char*)p)[1]); + + rs->servent.s_port = htons(port); rs->servent.s_proto = p[2] == 't' ? "tcp" : "udp"; p += 4; /* skip port(2) + proto(1) + aliascount(1) */ diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c index 5bf82ba..4ba2177 100644 --- a/libc/stdlib/atexit.c +++ b/libc/stdlib/atexit.c @@ -147,10 +147,19 @@ __cxa_finalize(void *dso) p->fns[n].fn_ptr.cxa_func = NULL; mprotect(p, pgsize, PROT_READ); } +#if ANDROID + /* it looks like we should always call the function + * with an argument, even if dso is not NULL. Otherwise + * static destructors will not be called properly on + * the ARM. + */ + (*fn.fn_ptr.cxa_func)(fn.fn_arg); +#else /* !ANDROID */ if (dso != NULL) (*fn.fn_ptr.cxa_func)(fn.fn_arg); else (*fn.fn_ptr.std_func)(); +#endif /* !ANDROID */ } } diff --git a/libstdc++/include/cmath b/libstdc++/include/cmath new file mode 100644 index 0000000..bf697ff --- /dev/null +++ b/libstdc++/include/cmath @@ -0,0 +1,71 @@ +/* -*- c++ -*- */ +/* + * Copyright (C) 2009 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef BIONIC_LIBSTDCPP_INCLUDE_CMATH__ +#define BIONIC_LIBSTDCPP_INCLUDE_CMATH__ + +/* + * Standard C++ Library wrapper around the C time.h header file. + */ + +#include <cstddef> +#include <math.h> + +namespace std +{ +// Functions. +using ::cos; +using ::sin; +using ::tan; +using ::acos; +using ::asin; +using ::atan; +using ::atan2; + +using ::cosh; +using ::sinh; +using ::tanh; + +using ::exp; +using ::frexp; +using ::ldexp; +using ::log; +using ::log10; +using ::modf; + +using ::pow; +using ::sqrt; + +using ::ceil; +using ::fabs; +using ::floor; +using ::fmod; +} // namespace std + +#endif // BIONIC_LIBSTDCPP_INCLUDE_CMATH__ diff --git a/linker/Android.mk b/linker/Android.mk index cc97690..00b5b0a 100644 --- a/linker/Android.mk +++ b/linker/Android.mk @@ -37,7 +37,7 @@ endif LOCAL_MODULE:= linker -LOCAL_STATIC_LIBRARIES := libcutils libc +LOCAL_STATIC_LIBRARIES := libcutils libc_nomalloc #LOCAL_FORCE_STATIC_EXECUTABLE := true # not necessary when not including BUILD_EXECUTABLE diff --git a/linker/README.TXT b/linker/README.TXT new file mode 100644 index 0000000..4fff14e --- /dev/null +++ b/linker/README.TXT @@ -0,0 +1,114 @@ +Android Dynamic Linker Design Notes +=================================== + +Introduction: +------------- + +This document provides several notes related to the design of the Android +dynamic linker. + + +Prelinking: +----------- + +System libraries in Android are internally prelinked, which means that +any internal relocations within them are stripped from the corresponding +shared object, in order to reduce size and speed up loading. + +Such libraries can only be loaded at the very specific virtual memory address +they have been prelinked to (during the build process). The list of prelinked +system libraries and their corresponding virtual memory address is found in +the file: + + build/core/prelink-linux-<arch>.map + +It should be updated each time a new system library is added to the +system. + +The prelink step happens at build time, and uses the 'soslim' and 'apriori' +tools: + + - 'apriori' is the real prelink tool which removes relocations from the + shared object, however, it must be given a list of symbols to remove + from the file. + + - 'soslim' is used to find symbols in an executable ELF file + and generate a list that can be passed to 'apriori'. + +By default, these tools are only used to remove internal symbols from +libraries, though they have been designed to allow more aggressive +optimizations (e.g. 'global' prelinking and symbol stripping, which +prevent replacing individual system libraries though). + +You can disable prelinking at build time by modifying your Android.mk with +a line like: + + LOCAL_PRELINK_MODULE := false + + +Initialization and Termination functions: +----------------------------------------- + +The Unix Sys V Binary Interface standard states that an +executable can have the following entries in its .dynamic +section: + + DT_INIT + Points to the address of an initialization function + that must be called when the file is loaded. + + DT_INIT_ARRAY + Points to an array of function addresses that must be + called, in-order, to perform initialization. Some of + the entries in the array can be 0 or -1, and should + be ignored. + + Note: this is generally stored in a .init_array section + + DT_INIT_ARRAYSZ + The size of the DT_INITARRAY, if any + + DT_FINI + Points to the address of a finalization function which + must be called when the file is unloaded or the process + terminated. + + DT_FINI_ARRAY + Same as DT_INITARRAY but for finalizers. Note that the + functions must be called in reverse-order though + + Note: this is generally stroed in a .fini_array section + + DT_FINI_ARRAYSZ + Size of FT_FINIARRAY + + DT_PREINIT_ARRAY + An array similar to DT_INIT_ARRAY which must *only* be + present in executables, not shared libraries, which contains + a list of functions that need to be called before any other + initialization function (i.e. DT_INIT and/or DT_INIT_ARRAY) + + Note: this is generally stroed in a .preinit_array section + + DT_PREINIT_ARRAYSZ + The size of DT_PREINIT_ARRAY + +If both a DT_INIT and DT_INITARRAY entry are present, the DT_INIT +function must be called before the DT_INITARRAY functions. + +Consequently, the DT_FINIARRAY must be parsed in reverse order before +the DT_FINI function, if both are available. + +Note that the implementation of static C++ constructors is very +much processor dependent, and may use different ELF sections. + +On the ARM (see "C++ ABI for ARM" document), the static constructors +must be called explicitely from the DT_INIT_ARRAY, and each one of them +shall register a destructor by calling the special __eabi_atexit() +function (provided by the C library). The DT_FINI_ARRAY is not used +by static C++ destructors. + +On x86, the lists of constructors and destructors are placed in special +sections named ".ctors" and ".dtors", and the DT_INIT / DT_FINI functions +are in charge of calling them explicitely. + diff --git a/linker/dlfcn.c b/linker/dlfcn.c index cd73d11..b54674f 100644 --- a/linker/dlfcn.c +++ b/linker/dlfcn.c @@ -15,41 +15,49 @@ */ #include <dlfcn.h> #include <pthread.h> +#include <stdio.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_CANNOT_LOAD_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 char dl_err_buf[1024]; +static const char *dl_err_str; + static const char *dl_errors[] = { - [DL_SUCCESS] = NULL, - [DL_ERR_CANNOT_FIND_LIBRARY] = "Cannot find library", + [DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load 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) +static void set_dlerror(int err) +{ + snprintf(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err], + linker_get_error()); + dl_err_str = (const char *)&dl_err_buf[0]; +}; + +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; + set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY); } else { ret->refcount++; } @@ -59,9 +67,9 @@ void *dlopen(const char *filename, int flag) const char *dlerror(void) { - const char *err = dl_errors[dl_last_err]; - dl_last_err = DL_SUCCESS; - return err; + const char *tmp = dl_err_str; + dl_err_str = NULL; + return (const char *)tmp; } void *dlsym(void *handle, const char *symbol) @@ -71,16 +79,16 @@ void *dlsym(void *handle, const char *symbol) unsigned bind; pthread_mutex_lock(&dl_lock); - + if(unlikely(handle == 0)) { - dl_last_err = DL_ERR_INVALID_LIBRARY_HANDLE; + set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE); goto err; } if(unlikely(symbol == 0)) { - dl_last_err = DL_ERR_BAD_SYMBOL_NAME; + set_dlerror(DL_ERR_BAD_SYMBOL_NAME); goto err; } - + if(handle == RTLD_DEFAULT) { sym = lookup(symbol, &base); } else if(handle == RTLD_NEXT) { @@ -92,16 +100,17 @@ void *dlsym(void *handle, const char *symbol) 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; + set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL); } - else dl_last_err = DL_ERR_SYMBOL_NOT_FOUND; + else + set_dlerror(DL_ERR_SYMBOL_NOT_FOUND); err: pthread_mutex_unlock(&dl_lock); diff --git a/linker/linker.c b/linker/linker.c index bb3b8e7..77f995e 100644 --- a/linker/linker.c +++ b/linker/linker.c @@ -53,6 +53,10 @@ #define SO_MAX 96 +/* Assume average path length of 64 and max 8 paths */ +#define LDPATH_BUFSIZE 512 +#define LDPATH_MAX 8 + /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< * * Do NOT use malloc() and friends or pthread_*() code here. @@ -66,7 +70,6 @@ * - 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 @@ -89,6 +92,9 @@ static soinfo *freelist = NULL; static soinfo *solist = &libdl_info; static soinfo *sonext = &libdl_info; +static char ldpaths_buf[LDPATH_BUFSIZE]; +static const char *ldpaths[LDPATH_MAX + 1]; + int debug_verbosity; static int pid; @@ -104,6 +110,32 @@ unsigned bitmask[4096]; #define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */ #endif +#define HOODLUM(name, ret, ...) \ + ret name __VA_ARGS__ \ + { \ + char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \ + write(2, errstr, sizeof(errstr)); \ + abort(); \ + } +HOODLUM(malloc, void *, (size_t size)); +HOODLUM(free, void, (void *ptr)); +HOODLUM(realloc, void *, (void *ptr, size_t size)); +HOODLUM(calloc, void *, (size_t cnt, size_t size)); + +static char tmp_err_buf[768]; +static char __linker_dl_err_buf[768]; +#define DL_ERR(fmt, x...) \ + do { \ + snprintf(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \ + "%s[%d]: " fmt, __func__, __LINE__, ##x); \ + ERROR(fmt, ##x); \ + } while(0) + +const char *linker_get_error(void) +{ + return (const char *)&__linker_dl_err_buf[0]; +} + /* * This function is an empty stub where GDB locates a breakpoint to get notified * about linker activity. @@ -207,7 +239,7 @@ 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); + DL_ERR("%5d library name %s too long\n", pid, name); return 0; } @@ -216,7 +248,7 @@ static soinfo *alloc_info(const char *name) */ if (!freelist) { if(socount == SO_MAX) { - ERROR("%5d too many libraries when loading %s\n", pid, name); + DL_ERR("%5d too many libraries when loading %s\n", pid, name); return NULL; } freelist = sopool + socount++; @@ -252,7 +284,7 @@ static void free_info(soinfo *si) } if (trav == NULL) { /* si was not ni solist */ - ERROR("%5d name %s is not in solist!\n", pid, si->name); + DL_ERR("%5d name %s is not in solist!\n", pid, si->name); return; } @@ -477,13 +509,12 @@ static int _open_lib(const char *name) return -1; } -/* TODO: Need to add support for initializing the so search path with - * LD_LIBRARY_PATH env variable for non-setuid programs. */ static int open_library(const char *name) { int fd; char buf[512]; const char **path; + int n; TRACE("[ %5d opening %s ]\n", pid, name); @@ -493,8 +524,21 @@ static int open_library(const char *name) if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0)) return fd; + for (path = ldpaths; *path; path++) { + n = snprintf(buf, sizeof(buf), "%s/%s", *path, name); + if (n < 0 || n >= (int)sizeof(buf)) { + WARN("Ignoring very long library path: %s/%s\n", *path, name); + continue; + } + if ((fd = _open_lib(buf)) >= 0) + return fd; + } for (path = sopaths; *path; path++) { - snprintf(buf, sizeof(buf), "%s/%s", *path, name); + n = snprintf(buf, sizeof(buf), "%s/%s", *path, name); + if (n < 0 || n >= (int)sizeof(buf)) { + WARN("Ignoring very long library path: %s/%s\n", *path, name); + continue; + } if ((fd = _open_lib(buf)) >= 0) return fd; } @@ -521,7 +565,7 @@ is_prelinked(int fd, const char *name) sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END); if (sz < 0) { - ERROR("lseek() failed!\n"); + DL_ERR("lseek() failed!\n"); return 0; } @@ -597,7 +641,7 @@ get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz) 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); + DL_ERR("%5d - %s is not a valid ELF object\n", pid, name); return (unsigned)-1; } @@ -625,7 +669,7 @@ get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz) } if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) { - ERROR("%5d - No loadable segments found in %s.\n", pid, name); + DL_ERR("%5d - No loadable segments found in %s.\n", pid, name); return (unsigned)-1; } @@ -661,13 +705,13 @@ static int reserve_mem_region(soinfo *si) void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (base == MAP_FAILED) { - ERROR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x " + DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x " "as requested, will try general pool: %d (%s)\n", pid, (si->base ? "" : "non-"), si->name, si->base, errno, strerror(errno)); return -1; } else if (base != (void *)si->base) { - ERROR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, " + DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, " "not at 0x%08x\n", pid, (si->base ? "" : "non-"), si->name, (unsigned)base, si->base); munmap(base, si->size); @@ -705,7 +749,7 @@ alloc_mem_region(soinfo *si) } err: - ERROR("OOPS: %5d cannot map library '%s'. no vspace available.\n", + DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.\n", pid, si->name); return -1; } @@ -764,7 +808,7 @@ load_segments(int fd, void *header, soinfo *si) 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). " + DL_ERR("%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; @@ -820,8 +864,8 @@ load_segments(int fd, void *header, soinfo *si) 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, + DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x" + " (0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len); goto fail; } @@ -873,7 +917,7 @@ load_segments(int fd, void *header, soinfo *si) /* Sanity check */ if (total_sz > si->size) { - ERROR("%5d - Total length (0x%08x) of mapped segments from '%s' is " + DL_ERR("%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; @@ -940,18 +984,20 @@ load_library(const char *name) soinfo *si = NULL; Elf32_Ehdr *hdr; - if(fd == -1) + if(fd == -1) { + DL_ERR("Library '%s' not found\n", name); 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"); + DL_ERR("lseek() failed!\n"); goto fail; } if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) { - ERROR("read() failed!\n"); + DL_ERR("read() failed!\n"); goto fail; } @@ -1042,8 +1088,8 @@ soinfo *find_library(const char *name) 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; + DL_ERR("OOPS: %5d recursive link to '%s'\n", pid, si->name); + return NULL; } } @@ -1074,7 +1120,7 @@ unsigned unload_library(soinfo *si) if(lsi) unload_library(lsi); else - ERROR("%5d could not unload '%s'\n", + DL_ERR("%5d could not unload '%s'\n", pid, si->strtab + d[1]); } } @@ -1124,19 +1170,19 @@ static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) sym_name = (char *)(strtab + symtab[sym].st_name); s = _do_lookup(si, sym_name, &base); if(s == 0) { - ERROR("%5d cannot locate '%s'...\n", pid, sym_name); + DL_ERR("%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", + DL_ERR("%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 " + DL_ERR("%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; @@ -1198,7 +1244,7 @@ static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) COUNT_RELOC(RELOC_RELATIVE); MARK(rel->r_offset); if(sym){ - ERROR("%5d odd RELATIVE form...\n", pid); + DL_ERR("%5d odd RELATIVE form...\n", pid); return -1; } TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid, @@ -1239,7 +1285,7 @@ static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) #endif /* ANDROID_ARM_LINKER */ default: - ERROR("%5d unknown reloc type %d @ %p (%d)\n", + DL_ERR("%5d unknown reloc type %d @ %p (%d)\n", pid, type, rel, (int) (rel - start)); return -1; } @@ -1248,13 +1294,38 @@ static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) return 0; } -static void call_array(unsigned *ctor, int count) + +/* Please read the "Initialization and Termination functions" functions. + * of the linker design note in bionic/linker/README.TXT to understand + * what the following code is doing. + * + * The important things to remember are: + * + * DT_PREINIT_ARRAY must be called first for executables, and should + * not appear in shared libraries. + * + * DT_INIT should be called before DT_INIT_ARRAY if both are present + * + * DT_FINI should be called after DT_FINI_ARRAY if both are present + * + * DT_FINI_ARRAY must be parsed in reverse order. + */ + +static void call_array(unsigned *ctor, int count, int reverse) { - int n; - for(n = count; n > 0; n--){ - TRACE("[ %5d Looking at ctor *0x%08x == 0x%08x ]\n", pid, + int n, inc = 1; + + if (reverse) { + ctor += (count-1); + inc = -1; + } + + for(n = count; n > 0; n--) { + TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid, + reverse ? "dtor" : "ctor", (unsigned)ctor, (unsigned)*ctor); - void (*func)() = (void (*)()) *ctor++; + void (*func)() = (void (*)()) *ctor; + ctor += inc; if(((int) func == 0) || ((int) func == -1)) continue; TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func); func(); @@ -1263,31 +1334,20 @@ static void call_array(unsigned *ctor, int count) 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); + call_array(si->preinit_array, si->preinit_array_count, 0); 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); + DL_ERR("%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); @@ -1298,25 +1358,21 @@ static void call_constructors(soinfo *si) 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); + call_array(si->init_array, si->init_array_count, 0); 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); + call_array(si->fini_array, si->fini_array_count, 1); 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); @@ -1334,7 +1390,7 @@ static int nullify_closed_stdio (void) dev_null = open("/dev/null", O_RDWR); if (dev_null < 0) { - ERROR("Cannot open /dev/null.\n"); + DL_ERR("Cannot open /dev/null.\n"); return -1; } TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null); @@ -1360,7 +1416,7 @@ static int nullify_closed_stdio (void) /* 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)); + DL_ERR("nullify_stdio: unhandled error %s\n", strerror(errno)); return_value = -1; continue; } @@ -1371,9 +1427,9 @@ static int nullify_closed_stdio (void) do { status = dup2(dev_null, i); } while (status < 0 && errno == EINTR); - + if (status < 0) { - ERROR("nullify_stdio: dup2 error %s\n", strerror(errno)); + DL_ERR("nullify_stdio: dup2 error %s\n", strerror(errno)); return_value = -1; continue; } @@ -1382,12 +1438,12 @@ static int nullify_closed_stdio (void) /* 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 { + do { status = close(dev_null); } while (status < 0 && errno == EINTR); if (status < 0) { - ERROR("nullify_stdio: close error %s\n", strerror(errno)); + DL_ERR("nullify_stdio: close error %s\n", strerror(errno)); return_value = -1; } } @@ -1455,7 +1511,7 @@ static int link_image(soinfo *si, unsigned wr_offset) } } else if (phdr->p_type == PT_DYNAMIC) { if (si->dynamic != (unsigned *)-1) { - ERROR("%5d multiple PT_DYNAMIC segments found in '%s'. " + DL_ERR("%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); @@ -1468,7 +1524,7 @@ static int link_image(soinfo *si, unsigned wr_offset) } if (si->dynamic == (unsigned *)-1) { - ERROR("%5d missing PT_DYNAMIC?!\n", pid); + DL_ERR("%5d missing PT_DYNAMIC?!\n", pid); goto fail; } @@ -1492,7 +1548,7 @@ static int link_image(soinfo *si, unsigned wr_offset) break; case DT_PLTREL: if(*d != DT_REL) { - ERROR("DT_RELA not supported\n"); + DL_ERR("DT_RELA not supported\n"); goto fail; } break; @@ -1517,7 +1573,7 @@ static int link_image(soinfo *si, unsigned wr_offset) *d = (int) &_r_debug; break; case DT_RELA: - ERROR("%5d DT_RELA not supported\n", pid); + DL_ERR("%5d DT_RELA not supported\n", pid); goto fail; case DT_INIT: si->init_func = (void (*)(void))(si->base + *d); @@ -1569,16 +1625,18 @@ static int link_image(soinfo *si, unsigned wr_offset) pid, si->base, si->strtab, si->symtab); if((si->strtab == 0) || (si->symtab == 0)) { - ERROR("%5d missing essential tables\n", pid); + DL_ERR("%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]); + soinfo *lsi = find_library(si->strtab + d[1]); if(lsi == 0) { - ERROR("%5d could not load '%s'\n", pid, si->strtab + d[1]); + strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); + DL_ERR("%5d could not load needed library '%s' for '%s' (%s)\n", + pid, si->strtab + d[1], si->name, tmp_err_buf); goto fail; } lsi->refcount++; @@ -1643,6 +1701,29 @@ fail: return -1; } +static void parse_library_path(char *path, char *delim) +{ + size_t len; + char *ldpaths_bufp = ldpaths_buf; + int i = 0; + + len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf)); + + while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) { + if (*ldpaths[i] != '\0') + ++i; + } + + /* Forget the last path if we had to truncate; this occurs if the 2nd to + * last char isn't '\0' (i.e. not originally a delim). */ + if (i > 0 && len >= sizeof(ldpaths_buf) && + ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') { + ldpaths[i - 1] = NULL; + } else { + ldpaths[i] = NULL; + } +} + int main(int argc, char **argv) { return 0; @@ -1661,6 +1742,7 @@ unsigned __linker_init(unsigned **elfdata) unsigned *vecs = (unsigned*) (argv + argc + 1); soinfo *si; struct link_map * map; + char *ldpath_env = NULL; pid = getpid(); @@ -1678,6 +1760,8 @@ unsigned __linker_init(unsigned **elfdata) while(vecs[0] != 0) { if(!strncmp((char*) vecs[0], "DEBUG=", 6)) { debug_verbosity = atoi(((char*) vecs[0]) + 6); + } else if(!strncmp((char*) vecs[0], "LD_LIBRARY_PATH=", 16)) { + ldpath_env = (char*) vecs[0] + 16; } vecs++; } @@ -1737,8 +1821,14 @@ unsigned __linker_init(unsigned **elfdata) si->wrprotect_start = 0xffffffff; si->wrprotect_end = 0; - if(link_image(si, 0)){ - ERROR("CANNOT LINK EXECUTABLE '%s'\n", argv[0]); + /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */ + if (ldpath_env && getuid() == geteuid() && getgid() == getegid()) + parse_library_path(ldpath_env, ":"); + + if(link_image(si, 0)) { + char errmsg[] = "CANNOT LINK EXECUTABLE\n"; + write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); + write(2, errmsg, sizeof(errmsg)); exit(-1); } diff --git a/linker/linker.h b/linker/linker.h index bb41075..ec7e139 100644 --- a/linker/linker.h +++ b/linker/linker.h @@ -204,6 +204,7 @@ 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); +const char *linker_get_error(void); #ifdef ANDROID_ARM_LINKER typedef long unsigned int *_Unwind_Ptr; diff --git a/linker/linker_debug.h b/linker/linker_debug.h index 3cc1343..3f4fc4c 100644 --- a/linker/linker_debug.h +++ b/linker/linker_debug.h @@ -55,17 +55,16 @@ #define TRUE 1 #define FALSE 0 - -#define __PRINTVF(v,f,x...) do { \ - (debug_verbosity > (v)) && (printf(x), ((f) && fflush(stdout))); \ - } while (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...) __PRINTVF(v,f,x) +#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 */ @@ -75,8 +74,9 @@ extern int debug_verbosity; #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) +#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) |