diff options
Diffstat (limited to 'linker/linker.cpp')
-rw-r--r-- | linker/linker.cpp | 111 |
1 files changed, 97 insertions, 14 deletions
diff --git a/linker/linker.cpp b/linker/linker.cpp index 18f8cdcb4..15ea423f9 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -57,6 +57,7 @@ #include "linker_phdr.h" #include "linker_relocs.h" #include "linker_reloc_iterators.h" +#include "ziparchive/zip_archive.h" /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< * @@ -838,29 +839,109 @@ ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) { return nullptr; } -static int open_library_on_path(const char* name, const char* const paths[]) { +static int open_library_in_zipfile(const char* const path, + off64_t* file_offset) { + TRACE("Trying zip file open from path '%s'", path); + + // Treat an '!' character inside a path as the separator between the name + // of the zip file on disk and the subdirectory to search within it. + // For example, if path is "foo.zip!bar/bas/x.so", then we search for + // "bar/bas/x.so" within "foo.zip". + const char* separator = strchr(path, '!'); + if (separator == nullptr) { + return -1; + } + + char buf[512]; + if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { + PRINT("Warning: ignoring very long library path: %s", path); + return -1; + } + + buf[separator - path] = '\0'; + + const char* zip_path = buf; + const char* file_path = &buf[separator - path + 1]; + int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC)); + if (fd == -1) { + return -1; + } + + ZipArchiveHandle handle; + if (OpenArchiveFd(fd, "", &handle, false) != 0) { + // invalid zip-file (?) + close(fd); + return -1; + } + + auto archive_guard = make_scope_guard([&]() { + CloseArchive(handle); + }); + + ZipEntry entry; + + if (FindEntry(handle, ZipEntryName(file_path), &entry) != 0) { + // Entry was not found. + close(fd); + return -1; + } + + // Check if it is properly stored + if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) { + close(fd); + return -1; + } + + *file_offset = entry.offset; + return fd; +} + +static int open_library_on_path(const char* name, + const char* const paths[], + off64_t* file_offset) { char buf[512]; - for (size_t i = 0; paths[i] != nullptr; ++i) { - int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name); + int fd = -1; + + for (size_t i = 0; paths[i] != nullptr && fd == -1; ++i) { + const char* const path = paths[i]; + int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", path, name); if (n < 0 || n >= static_cast<int>(sizeof(buf))) { - PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name); - continue; + PRINT("Warning: ignoring very long library path: %s/%s", path, name); + return -1; } - int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); - if (fd != -1) { - return fd; + + const char* separator = strchr(path, '!'); + + if (separator != nullptr) { + fd = open_library_in_zipfile(buf, file_offset); + } + + if (fd == -1) { + fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); + if (fd != -1) { + *file_offset = 0; + } } } - return -1; + + return fd; } -static int open_library(const char* name) { +static int open_library(const char* name, off64_t* file_offset) { TRACE("[ opening %s ]", name); // If the name contains a slash, we should attempt to open it directly and not search the paths. if (strchr(name, '/') != nullptr) { + if (strchr(name, '!') != nullptr) { + int fd = open_library_in_zipfile(name, file_offset); + if (fd != -1) { + return fd; + } + } + int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); if (fd != -1) { + *file_offset = 0; return fd; } // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now. @@ -870,9 +951,9 @@ static int open_library(const char* name) { } // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. - int fd = open_library_on_path(name, g_ld_library_paths); + int fd = open_library_on_path(name, g_ld_library_paths, file_offset); if (fd == -1) { - fd = open_library_on_path(name, kDefaultLdPaths); + fd = open_library_on_path(name, kDefaultLdPaths, file_offset); } return fd; } @@ -886,7 +967,9 @@ static void for_each_dt_needed(const soinfo* si, F action) { } } -static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) { +static soinfo* load_library(LoadTaskList& load_tasks, + const char* name, int rtld_flags, + const android_dlextinfo* extinfo) { int fd = -1; off64_t file_offset = 0; ScopedFd file_guard(-1); @@ -898,7 +981,7 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld } } else { // Open the file. - fd = open_library(name); + fd = open_library(name, &file_offset); if (fd == -1) { DL_ERR("library \"%s\" not found", name); return nullptr; |