Add android_dlopen_ext() interface to linker.

Add a function "android_dlopen_ext()", defined in <android/dlext.h>.
This is an extended version of dlopen() which takes a struct for passing
additional parameters for Android-specific functionality. This will be
used to support RELRO section sharing between separate processes.

Bug: 13005501
Change-Id: I9c99b2f2a02ee329dedaeba09ef3a1113b17b2d6
diff --git a/libc/include/android/dlext.h b/libc/include/android/dlext.h
new file mode 100644
index 0000000..5de39c6
--- /dev/null
+++ b/libc/include/android/dlext.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#ifndef __ANDROID_DLEXT_H__
+#define __ANDROID_DLEXT_H__
+
+#include <stddef.h>
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+/* bitfield definitions for android_dlextinfo.flags */
+enum {
+  /* Mask of valid bits */
+  ANDROID_DLEXT_VALID_FLAG_BITS = 0,
+};
+
+typedef struct {
+  int     flags;
+} android_dlextinfo;
+
+extern void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo);
+
+__END_DECLS
+
+#endif /* __ANDROID_DLEXT_H__ */
diff --git a/libdl/libdl.c b/libdl/libdl.c
index 310db54..dca51b0 100644
--- a/libdl/libdl.c
+++ b/libdl/libdl.c
@@ -17,6 +17,7 @@
 #include <dlfcn.h>
 #include <link.h>
 #include <stdlib.h>
+#include <android/dlext.h>
 
 // These are stubs for functions that are actually defined
 // in the dynamic linker and hijacked at runtime.
@@ -27,11 +28,13 @@
 int dladdr(const void* addr __unused, Dl_info* info __unused) { return 0; }
 int dlclose(void* handle __unused) { return 0; }
 
-void android_get_LD_LIBRARY_PATH(char* buffer __unused, size_t buffer_size __unused) { }
-void android_update_LD_LIBRARY_PATH(const char* ld_library_path __unused) { }
-
 #if defined(__arm__)
 _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc __unused, int* pcount __unused) { return 0; }
 #endif
 
 int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data) __unused, void* data __unused) { return 0; }
+
+void android_get_LD_LIBRARY_PATH(char* buffer __unused, size_t buffer_size __unused) { }
+void android_update_LD_LIBRARY_PATH(const char* ld_library_path __unused) { }
+
+void* android_dlopen_ext(const char* filename __unused, int flag __unused, const android_dlextinfo* extinfo __unused) { return 0; }
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 6e70808..0292bdf 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -20,6 +20,7 @@
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <android/dlext.h>
 
 #include <bionic/pthread_internal.h>
 #include "private/bionic_tls.h"
@@ -64,9 +65,10 @@
   do_android_update_LD_LIBRARY_PATH(ld_library_path);
 }
 
-void* dlopen(const char* filename, int flags) {
+void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* extinfo)
+{
   ScopedPthreadMutexLocker locker(&gDlMutex);
-  soinfo* result = do_dlopen(filename, flags);
+  soinfo* result = do_dlopen(filename, flags, extinfo);
   if (result == NULL) {
     __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
     return NULL;
@@ -74,6 +76,10 @@
   return result;
 }
 
+void* dlopen(const char* filename, int flags) {
+  return android_dlopen_ext(filename, flags, NULL);
+}
+
 void* dlsym(void* handle, const char* symbol) {
   ScopedPthreadMutexLocker locker(&gDlMutex);
 
@@ -168,15 +174,15 @@
     }
 
 #if defined(__arm__)
-  // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1
-  // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1
+  // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222 333333333344444444445
+  // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789 012345678901234567890
 #  define ANDROID_LIBDL_STRTAB \
-    "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0dl_unwind_find_exidx\0"
+    "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\0dl_unwind_find_exidx\0"
 #elif defined(__aarch64__) || defined(__i386__) || defined(__mips__) || defined(__x86_64__)
-  // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1
-  // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1
+  // 0000000 00011111 111112 22222222 2333333 3333444444444455555555556666666 6667777777777888888888899999 9999900000000001 1111111112222222222
+  // 0123456 78901234 567890 12345678 9012345 6789012345678901234567890123456 7890123456789012345678901234 5678901234567890 1234567890123456789
 #  define ANDROID_LIBDL_STRTAB \
-    "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0"
+    "dlopen\0dlclose\0dlsym\0dlerror\0dladdr\0android_update_LD_LIBRARY_PATH\0android_get_LD_LIBRARY_PATH\0dl_iterate_phdr\0android_dlopen_ext\0"
 #else
 #  error Unsupported architecture. Only arm, arm64, mips, mips64, x86 and x86_64 are presently supported.
 #endif
@@ -195,8 +201,9 @@
   ELFW(SYM_INITIALIZER)( 36, &android_update_LD_LIBRARY_PATH, 1),
   ELFW(SYM_INITIALIZER)( 67, &android_get_LD_LIBRARY_PATH, 1),
   ELFW(SYM_INITIALIZER)( 95, &dl_iterate_phdr, 1),
+  ELFW(SYM_INITIALIZER)(111, &android_dlopen_ext, 1),
 #if defined(__arm__)
-  ELFW(SYM_INITIALIZER)(111, &dl_unwind_find_exidx, 1),
+  ELFW(SYM_INITIALIZER)(130, &dl_unwind_find_exidx, 1),
 #endif
 };
 
@@ -213,9 +220,9 @@
 // Note that adding any new symbols here requires stubbing them out in libdl.
 static unsigned gLibDlBuckets[1] = { 1 };
 #if defined(__arm__)
-static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
+static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
 #else
-static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 0 };
+static unsigned gLibDlChains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 #endif
 
 // This is used by the dynamic linker. Every process gets these symbols for free.
diff --git a/linker/linker.cpp b/linker/linker.cpp
index de7b04d..ba50d5c 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -811,11 +811,15 @@
   }
 }
 
-soinfo* do_dlopen(const char* name, int flags) {
+soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
   if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
     DL_ERR("invalid flags to dlopen: %x", flags);
     return NULL;
   }
+  if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
+    DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
+    return NULL;
+  }
   set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
   soinfo* si = find_library(name);
   if (si != NULL) {
diff --git a/linker/linker.h b/linker/linker.h
index b8d0587..93ab51d 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -32,6 +32,7 @@
 #include <elf.h>
 #include <link.h>
 #include <unistd.h>
+#include <android/dlext.h>
 
 #include "private/libc_logging.h"
 
@@ -192,7 +193,7 @@
 
 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
-soinfo* do_dlopen(const char* name, int flags);
+soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
 int do_dlclose(soinfo* si);
 
 ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);