Support loading libraries to a reserved address.
Add flags and parameters to android_dlopen_ext() to allow loading a
library at an already-reserved fixed address. If the library to be
loaded will not fit within the space reserved, then the linker will
either fail, or allocate its own address space as usual, according to
which flag has been specified. This behaviour only applies to the
specific library requested; any other libraries loaded as dependencies
will be loaded in the normal fashion.
There is a new gtest included to cover the functionality added.
Bug: 13005501
Change-Id: I5d1810375b20fc51ba6a9b3191a25f9792c687f1
diff --git a/linker/linker.cpp b/linker/linker.cpp
index ba50d5c..85ef63b 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -690,7 +690,7 @@
return fd;
}
-static soinfo* load_library(const char* name) {
+static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) {
// Open the file.
int fd = open_library(name);
if (fd == -1) {
@@ -700,7 +700,7 @@
// Read the ELF header and load the segments.
ElfReader elf_reader(name, fd);
- if (!elf_reader.Load()) {
+ if (!elf_reader.Load(extinfo)) {
return NULL;
}
@@ -735,7 +735,7 @@
return NULL;
}
-static soinfo* find_library_internal(const char* name) {
+static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) {
if (name == NULL) {
return somain;
}
@@ -750,7 +750,7 @@
}
TRACE("[ '%s' has not been loaded yet. Locating...]", name);
- si = load_library(name);
+ si = load_library(name, extinfo);
if (si == NULL) {
return NULL;
}
@@ -769,8 +769,8 @@
return si;
}
-static soinfo* find_library(const char* name) {
- soinfo* si = find_library_internal(name);
+static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
+ soinfo* si = find_library_internal(name, extinfo);
if (si != NULL) {
si->ref_count++;
}
@@ -821,7 +821,7 @@
return NULL;
}
set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
- soinfo* si = find_library(name);
+ soinfo* si = find_library(name, extinfo);
if (si != NULL) {
si->CallConstructors();
}
@@ -1803,7 +1803,7 @@
memset(gLdPreloads, 0, sizeof(gLdPreloads));
size_t preload_count = 0;
for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
- soinfo* lsi = find_library(gLdPreloadNames[i]);
+ soinfo* lsi = find_library(gLdPreloadNames[i], NULL);
if (lsi != NULL) {
gLdPreloads[preload_count++] = lsi;
} else {
@@ -1821,7 +1821,7 @@
if (d->d_tag == DT_NEEDED) {
const char* library_name = si->strtab + d->d_un.d_val;
DEBUG("%s needs %s", si->name, library_name);
- soinfo* lsi = find_library(library_name);
+ soinfo* lsi = find_library(library_name, NULL);
if (lsi == NULL) {
strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index 7c5d3f6..7e97aa9 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -132,11 +132,11 @@
}
}
-bool ElfReader::Load() {
+bool ElfReader::Load(const android_dlextinfo* extinfo) {
return ReadElfHeader() &&
VerifyElfHeader() &&
ReadProgramHeader() &&
- ReserveAddressSpace() &&
+ ReserveAddressSpace(extinfo) &&
LoadSegments() &&
FindPhdr();
}
@@ -291,7 +291,7 @@
// Reserve a virtual address range big enough to hold all loadable
// segments of a program header table. This is done by creating a
// private anonymous mmap() with PROT_NONE.
-bool ElfReader::ReserveAddressSpace() {
+bool ElfReader::ReserveAddressSpace(const android_dlextinfo* extinfo) {
ElfW(Addr) min_vaddr;
load_size_ = phdr_table_get_load_size(phdr_table_, phdr_num_, &min_vaddr);
if (load_size_ == 0) {
@@ -300,11 +300,33 @@
}
uint8_t* addr = reinterpret_cast<uint8_t*>(min_vaddr);
- int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
- void* start = mmap(addr, load_size_, PROT_NONE, mmap_flags, -1, 0);
- if (start == MAP_FAILED) {
- DL_ERR("couldn't reserve %zd bytes of address space for \"%s\"", load_size_, name_);
- return false;
+ void* start;
+ size_t reserved_size = 0;
+ bool reserved_hint = true;
+
+ if (extinfo != NULL) {
+ if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS) {
+ reserved_size = extinfo->reserved_size;
+ reserved_hint = false;
+ } else if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_HINT) {
+ reserved_size = extinfo->reserved_size;
+ }
+ }
+
+ if (load_size_ > reserved_size) {
+ if (!reserved_hint) {
+ DL_ERR("reserved address space %zd smaller than %zd bytes needed for \"%s\"",
+ reserved_size - load_size_, load_size_, name_);
+ return false;
+ }
+ int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
+ start = mmap(addr, load_size_, PROT_NONE, mmap_flags, -1, 0);
+ if (start == MAP_FAILED) {
+ DL_ERR("couldn't reserve %zd bytes of address space for \"%s\"", load_size_, name_);
+ return false;
+ }
+ } else {
+ start = extinfo->reserved_addr;
}
load_start_ = start;
diff --git a/linker/linker_phdr.h b/linker/linker_phdr.h
index 6b72caf..430c6ec 100644
--- a/linker/linker_phdr.h
+++ b/linker/linker_phdr.h
@@ -42,7 +42,7 @@
ElfReader(const char* name, int fd);
~ElfReader();
- bool Load();
+ bool Load(const android_dlextinfo* extinfo);
size_t phdr_count() { return phdr_num_; }
ElfW(Addr) load_start() { return reinterpret_cast<ElfW(Addr)>(load_start_); }
@@ -54,7 +54,7 @@
bool ReadElfHeader();
bool VerifyElfHeader();
bool ReadProgramHeader();
- bool ReserveAddressSpace();
+ bool ReserveAddressSpace(const android_dlextinfo* extinfo);
bool LoadSegments();
bool FindPhdr();
bool CheckPhdr(ElfW(Addr));