Merge "Convert bionic benchmarks and tests to Android.bp"
diff --git a/libc/Android.bp b/libc/Android.bp
index ba62592..7309bd8 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -2006,3 +2006,5 @@
 
     defaults: ["crt_defaults"],
 }
+
+subdirs = ["malloc_debug"]
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 0c59c92..5188b6d 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -350,7 +350,7 @@
 int     __set_thread_area:set_thread_area(void*) x86
 
 # vdso stuff.
-int clock_gettime(clockid_t, timespec*)                 arm,mips,mips64
-int __clock_gettime:clock_gettime(clockid_t, timespec*) arm64,x86,x86_64
-int gettimeofday(timeval*, timezone*)                   arm,mips,mips64
-int __gettimeofday:gettimeofday(timeval*, timezone*)    arm64,x86,x86_64
+int clock_gettime(clockid_t, timespec*)                 mips,mips64
+int __clock_gettime:clock_gettime(clockid_t, timespec*) arm,arm64,x86,x86_64
+int gettimeofday(timeval*, timezone*)                   mips,mips64
+int __gettimeofday:gettimeofday(timeval*, timezone*)    arm,arm64,x86,x86_64
diff --git a/libc/arch-arm/syscalls/clock_gettime.S b/libc/arch-arm/syscalls/__clock_gettime.S
similarity index 87%
rename from libc/arch-arm/syscalls/clock_gettime.S
rename to libc/arch-arm/syscalls/__clock_gettime.S
index 61a95dd..30eff03 100644
--- a/libc/arch-arm/syscalls/clock_gettime.S
+++ b/libc/arch-arm/syscalls/__clock_gettime.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(clock_gettime)
+ENTRY(__clock_gettime)
     mov     ip, r7
     .cfi_register r7, ip
     ldr     r7, =__NR_clock_gettime
@@ -13,4 +13,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno_internal
-END(clock_gettime)
+END(__clock_gettime)
diff --git a/libc/arch-arm/syscalls/gettimeofday.S b/libc/arch-arm/syscalls/__gettimeofday.S
similarity index 88%
rename from libc/arch-arm/syscalls/gettimeofday.S
rename to libc/arch-arm/syscalls/__gettimeofday.S
index f5ed674..de0eca5 100644
--- a/libc/arch-arm/syscalls/gettimeofday.S
+++ b/libc/arch-arm/syscalls/__gettimeofday.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(gettimeofday)
+ENTRY(__gettimeofday)
     mov     ip, r7
     .cfi_register r7, ip
     ldr     r7, =__NR_gettimeofday
@@ -13,4 +13,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno_internal
-END(gettimeofday)
+END(__gettimeofday)
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
index 029c148..f3d95ca 100644
--- a/libc/bionic/vdso.cpp
+++ b/libc/bionic/vdso.cpp
@@ -17,7 +17,7 @@
 #include "private/bionic_globals.h"
 #include "private/bionic_vdso.h"
 
-#if defined(__aarch64__) || defined(__x86_64__) || defined (__i386__)
+#if defined(__aarch64__) || defined(__arm__) || defined(__i386__) || defined(__x86_64__)
 
 #include <limits.h>
 #include <link.h>
@@ -28,6 +28,8 @@
 #include <unistd.h>
 #include "private/KernelArgumentBlock.h"
 
+#define AT_SYSINFO_EHDR 33 /* until we have new enough uapi headers... */
+
 int clock_gettime(int clock_id, timespec* tp) {
   auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
     __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
diff --git a/libc/malloc_debug/Android.bp b/libc/malloc_debug/Android.bp
new file mode 100644
index 0000000..8ce3ff3
--- /dev/null
+++ b/libc/malloc_debug/Android.bp
@@ -0,0 +1,132 @@
+// ==============================================================
+// libc_malloc_debug_backtrace.a
+// ==============================================================
+// Used by libmemunreachable
+cc_library_static {
+
+    name: "libc_malloc_debug_backtrace",
+
+    srcs: [
+        "backtrace.cpp",
+        "MapData.cpp",
+    ],
+
+    stl: "libc++_static",
+
+    static_libs: ["libc_logging"],
+
+    include_dirs: ["bionic/libc"],
+    export_include_dirs: ["."],
+
+    sanitize: {
+        never: true,
+    },
+    native_coverage: false,
+
+    // -Wno-error=format-zero-length needed for gcc to compile.
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-error=format-zero-length",
+    ],
+
+}
+
+// ==============================================================
+// libc_malloc_debug.so
+// ==============================================================
+cc_library {
+    name: "libc_malloc_debug",
+
+    srcs: [
+        "BacktraceData.cpp",
+        "Config.cpp",
+        "DebugData.cpp",
+        "debug_disable.cpp",
+        "FreeTrackData.cpp",
+        "GuardData.cpp",
+        "malloc_debug.cpp",
+        "RecordData.cpp",
+        "TrackData.cpp",
+    ],
+
+    stl: "libc++_static",
+
+    // Only need this for arm since libc++ uses its own unwind code that
+    // doesn't mix with the other default unwind code.
+    arch: {
+        arm: {
+            static_libs: ["libunwind_llvm"],
+        },
+    },
+
+    static_libs: [
+        "libbase",
+        "libc_malloc_debug_backtrace",
+        "libc_logging",
+    ],
+
+    multilib: {
+        lib32: {
+            version_script: "exported32.map",
+        },
+        lib64: {
+            version_script: "exported64.map",
+        },
+    },
+    allow_undefined_symbols: true,
+    include_dirs: ["bionic/libc"],
+
+    sanitize: {
+        never: true,
+    },
+    native_coverage: false,
+
+    // -Wno-error=format-zero-length needed for gcc to compile.
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-fno-stack-protector",
+        "-Wno-error=format-zero-length",
+    ],
+
+}
+
+// ==============================================================
+// Unit Tests
+// ==============================================================
+cc_test {
+
+    name: "malloc_debug_unit_tests",
+    multilib: {
+        lib32: {
+            suffix: "32",
+        },
+        lib64: {
+            suffix: "64",
+        },
+    },
+
+    srcs: [
+        "tests/backtrace_fake.cpp",
+        "tests/log_fake.cpp",
+        "tests/libc_fake.cpp",
+        "tests/property_fake.cpp",
+        "tests/malloc_debug_config_tests.cpp",
+        "tests/malloc_debug_unit_tests.cpp",
+    ],
+
+    whole_static_libs: ["libc_malloc_debug"],
+
+    local_include_dirs: ["tests"],
+    include_dirs: ["bionic/libc"],
+
+    shared_libs: ["libbase"],
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-error=format-zero-length",
+    ],
+
+}
diff --git a/libc/malloc_debug/Android.mk b/libc/malloc_debug/Android.mk
deleted file mode 100644
index 00f5f89..0000000
--- a/libc/malloc_debug/Android.mk
+++ /dev/null
@@ -1,111 +0,0 @@
-LOCAL_PATH := $(call my-dir)
-
-libc_malloc_debug_src_files := \
-    BacktraceData.cpp \
-    Config.cpp \
-    DebugData.cpp \
-    debug_disable.cpp \
-    FreeTrackData.cpp \
-    GuardData.cpp \
-    malloc_debug.cpp \
-    RecordData.cpp \
-    TrackData.cpp \
-
-# ==============================================================
-# libc_malloc_debug_backtrace.a
-# ==============================================================
-# Used by libmemunreachable
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libc_malloc_debug_backtrace
-
-LOCAL_SRC_FILES := \
-    backtrace.cpp \
-    MapData.cpp \
-
-LOCAL_CXX_STL := libc++_static
-
-LOCAL_STATIC_LIBRARIES += \
-    libc_logging \
-
-LOCAL_C_INCLUDES += bionic/libc
-LOCAL_EXPORT_C_INCLUDE_DIRS += $(LOCAL_PATH)
-
-LOCAL_SANITIZE := never
-LOCAL_NATIVE_COVERAGE := false
-
-# -Wno-error=format-zero-length needed for gcc to compile.
-LOCAL_CFLAGS := \
-    -Wall \
-    -Werror \
-    -Wno-error=format-zero-length \
-
-include $(BUILD_STATIC_LIBRARY)
-
-# ==============================================================
-# libc_malloc_debug.so
-# ==============================================================
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libc_malloc_debug
-
-LOCAL_SRC_FILES := \
-    $(libc_malloc_debug_src_files) \
-
-LOCAL_CXX_STL := libc++_static
-
-# Only need this for arm since libc++ uses its own unwind code that
-# doesn't mix with the other default unwind code.
-LOCAL_STATIC_LIBRARIES_arm := libunwind_llvm
-
-LOCAL_STATIC_LIBRARIES += \
-    libbase \
-    libc_malloc_debug_backtrace \
-    libc_logging \
-
-LOCAL_LDFLAGS_32 := -Wl,--version-script,$(LOCAL_PATH)/exported32.map
-LOCAL_LDFLAGS_64 := -Wl,--version-script,$(LOCAL_PATH)/exported64.map
-LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
-LOCAL_C_INCLUDES += bionic/libc
-
-LOCAL_SANITIZE := never
-LOCAL_NATIVE_COVERAGE := false
-
-# -Wno-error=format-zero-length needed for gcc to compile.
-LOCAL_CFLAGS := \
-    -Wall \
-    -Werror \
-    -fno-stack-protector \
-    -Wno-error=format-zero-length \
-
-include $(BUILD_SHARED_LIBRARY)
-
-# ==============================================================
-# Unit Tests
-# ==============================================================
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := malloc_debug_unit_tests
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
-
-LOCAL_SRC_FILES := \
-    tests/backtrace_fake.cpp \
-    tests/log_fake.cpp \
-    tests/libc_fake.cpp \
-    tests/property_fake.cpp \
-    tests/malloc_debug_config_tests.cpp \
-    tests/malloc_debug_unit_tests.cpp \
-    $(libc_malloc_debug_src_files) \
-
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/tests
-LOCAL_C_INCLUDES += bionic/libc
-
-LOCAL_SHARED_LIBRARIES := libbase
-
-LOCAL_CFLAGS := \
-    -Wall \
-    -Werror \
-    -Wno-error=format-zero-length \
-
-include $(BUILD_NATIVE_TEST)
diff --git a/libc/private/bionic_vdso.h b/libc/private/bionic_vdso.h
index 5400de5..8fd0743 100644
--- a/libc/private/bionic_vdso.h
+++ b/libc/private/bionic_vdso.h
@@ -25,6 +25,7 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _PRIVATE_BIONIC_VDSO_H
 #define _PRIVATE_BIONIC_VDSO_H
 
@@ -33,7 +34,7 @@
 #if defined(__aarch64__)
 #define VDSO_CLOCK_GETTIME_SYMBOL "__kernel_clock_gettime"
 #define VDSO_GETTIMEOFDAY_SYMBOL  "__kernel_gettimeofday"
-#elif defined(__x86_64__) || defined(__i386__)
+#elif defined(__arm__) || defined(__i386__) || defined(__x86_64__)
 #define VDSO_CLOCK_GETTIME_SYMBOL "__vdso_clock_gettime"
 #define VDSO_GETTIMEOFDAY_SYMBOL  "__vdso_gettimeofday"
 #endif
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index 136e432..a2ae2a2 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -252,7 +252,12 @@
   off64_t range_start;
   off64_t range_end;
 
-  return safe_add(&range_start, file_offset_, offset) &&
+  // Only header can be located at the 0 offset... This function called to
+  // check DYNSYM and DYNAMIC sections and phdr/shdr - none of them can be
+  // at offset 0.
+
+  return offset > 0 &&
+         safe_add(&range_start, file_offset_, offset) &&
          safe_add(&range_end, range_start, size) &&
          range_start < file_size_ &&
          range_end <= file_size_;
@@ -324,6 +329,35 @@
     return false;
   }
 
+  // Make sure dynamic_shdr offset and size matches PT_DYNAMIC phdr
+  size_t pt_dynamic_offset = 0;
+  size_t pt_dynamic_filesz = 0;
+  for (size_t i = 0; i < phdr_num_; ++i) {
+    const ElfW(Phdr)* phdr = &phdr_table_[i];
+    if (phdr->p_type == PT_DYNAMIC) {
+      pt_dynamic_offset = phdr->p_offset;
+      pt_dynamic_filesz = phdr->p_filesz;
+    }
+  }
+
+  if (pt_dynamic_offset != dynamic_shdr->sh_offset) {
+    DL_ERR("\"%s\" .dynamic section has invalid offset: 0x%zx, "
+           "expected to match PT_DYNAMIC offset: 0x%zx",
+           name_.c_str(),
+           static_cast<size_t>(dynamic_shdr->sh_offset),
+           pt_dynamic_offset);
+    return false;
+  }
+
+  if (pt_dynamic_filesz != dynamic_shdr->sh_size) {
+    DL_ERR("\"%s\" .dynamic section has invalid size: 0x%zx, "
+           "expected to match PT_DYNAMIC filesz: 0x%zx",
+           name_.c_str(),
+           static_cast<size_t>(dynamic_shdr->sh_size),
+           pt_dynamic_filesz);
+    return false;
+  }
+
   if (dynamic_shdr->sh_link >= shdr_num_) {
     DL_ERR("\"%s\" .dynamic section has invalid sh_link: %d", name_.c_str(), dynamic_shdr->sh_link);
     return false;
diff --git a/tools/Android.bp b/tools/Android.bp
new file mode 100644
index 0000000..b44c296
--- /dev/null
+++ b/tools/Android.bp
@@ -0,0 +1 @@
+subdirs = ["*"]
diff --git a/tools/relocation_packer/Android.bp b/tools/relocation_packer/Android.bp
new file mode 100644
index 0000000..2907b31
--- /dev/null
+++ b/tools/relocation_packer/Android.bp
@@ -0,0 +1,84 @@
+//
+// Copyright (C) 2015 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.
+//
+
+cc_defaults {
+    name: "relocation_packer_flags",
+    cppflags: [
+        "-Wall",
+        "-Wextra",
+        "-Wunused",
+        "-Werror",
+        "-Wold-style-cast",
+    ],
+
+    target: {
+        darwin: {
+            enabled: false,
+        },
+    },
+}
+
+cc_library_host_static {
+    name: "lib_relocation_packer",
+    defaults: ["relocation_packer_flags"],
+    srcs: [
+        "src/debug.cc",
+        "src/delta_encoder.cc",
+        "src/elf_file.cc",
+        "src/packer.cc",
+        "src/sleb128.cc",
+    ],
+
+    static_libs: [
+        "libelf",
+        "libz",
+    ],
+}
+
+cc_binary_host {
+    name: "relocation_packer",
+    defaults: ["relocation_packer_flags"],
+
+    srcs: ["src/main.cc"],
+    static_libs: [
+        "lib_relocation_packer",
+        "libelf",
+        "libz",
+        "libbase",
+    ],
+
+    // Statically linking libc++ to make it work from prebuilts
+    stl: "libc++_static",
+}
+
+cc_test_host {
+    name: "relocation_packer_unit_tests",
+    defaults: ["relocation_packer_flags"],
+
+    srcs: [
+        "src/debug_unittest.cc",
+        "src/delta_encoder_unittest.cc",
+        "src/elf_file_unittest.cc",
+        "src/sleb128_unittest.cc",
+        "src/packer_unittest.cc",
+    ],
+
+    static_libs: [
+        "lib_relocation_packer",
+        "libelf",
+        "libz",
+    ],
+}
diff --git a/tools/relocation_packer/Android.mk b/tools/relocation_packer/Android.mk
index bdb54b7..80208ec 100644
--- a/tools/relocation_packer/Android.mk
+++ b/tools/relocation_packer/Android.mk
@@ -15,63 +15,6 @@
 #
 
 ifeq ($(HOST_OS),linux)
-common_cppflags := -Wall -Wextra -Wunused -Werror -Wold-style-cast
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_CPP_EXTENSION := .cc
-
-LOCAL_SRC_FILES := \
-  src/debug.cc \
-  src/delta_encoder.cc \
-  src/elf_file.cc \
-  src/packer.cc \
-  src/sleb128.cc \
-
-LOCAL_STATIC_LIBRARIES := libelf libz
-LOCAL_MODULE := lib_relocation_packer
-
-LOCAL_CPPFLAGS := $(common_cppflags)
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-
-LOCAL_CPP_EXTENSION := .cc
-
-LOCAL_SRC_FILES := src/main.cc
-LOCAL_STATIC_LIBRARIES := lib_relocation_packer libelf libz libbase
-
-# Statically linking libc++ to make it work from prebuilts
-LOCAL_CXX_STL := libc++_static
-
-LOCAL_MODULE := relocation_packer
-
-LOCAL_CPPFLAGS := $(common_cppflags)
-
-include $(BUILD_HOST_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_CPP_EXTENSION := .cc
-
-LOCAL_SRC_FILES := \
-  src/debug_unittest.cc \
-  src/delta_encoder_unittest.cc \
-  src/elf_file_unittest.cc \
-  src/sleb128_unittest.cc \
-  src/packer_unittest.cc \
-
-LOCAL_STATIC_LIBRARIES := lib_relocation_packer libelf libz
-
-LOCAL_CPPFLAGS := $(common_cppflags)
-
-LOCAL_MODULE := relocation_packer_unit_tests
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-
-include $(BUILD_HOST_NATIVE_TEST)
 
 # $(1) library name
 define copy-test-library