am c41dcad0: Merge "More linker cleanup."

* commit 'c41dcad040ede2975ea63e383a8a3d36e3642d56':
  More linker cleanup.
This commit is contained in:
Elliott Hughes 2013-03-05 22:42:33 -08:00 committed by Android Git Automerger
commit 9a0b658c9c
6 changed files with 326 additions and 466 deletions

View file

@ -64,7 +64,7 @@ void* dlopen(const char* filename, int flags) {
ScopedPthreadMutexLocker locker(&gDlMutex); ScopedPthreadMutexLocker locker(&gDlMutex);
soinfo* result = do_dlopen(filename, flags); soinfo* result = do_dlopen(filename, flags);
if (result == NULL) { if (result == NULL) {
__bionic_format_dlerror("dlopen failed", linker_get_error()); __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
return NULL; return NULL;
} }
return result; return result;

View file

@ -41,7 +41,6 @@
// Private C library headers. // Private C library headers.
#include <private/bionic_tls.h> #include <private/bionic_tls.h>
#include <private/debug_format.h>
#include <private/KernelArgumentBlock.h> #include <private/KernelArgumentBlock.h>
#include <private/logd.h> #include <private/logd.h>
#include <private/ScopedPthreadMutexLocker.h> #include <private/ScopedPthreadMutexLocker.h>
@ -105,7 +104,7 @@ static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
static soinfo* gLdPreloads[LDPRELOAD_MAX + 1]; static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
static int debug_verbosity; __LIBC_HIDDEN__ int gLdDebugVerbosity;
enum RelocationKind { enum RelocationKind {
kRelocAbsolute = 0, kRelocAbsolute = 0,
@ -158,17 +157,15 @@ DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
static char tmp_err_buf[768]; static char tmp_err_buf[768];
static char __linker_dl_err_buf[768]; static char __linker_dl_err_buf[768];
#define DL_ERR(fmt, x...) \
do { \
__libc_format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
/* If LD_DEBUG is set high enough, send every dlerror(3) message to the log. */ \
DEBUG(fmt "\n", ##x); \
} while(0)
const char* linker_get_error() { char* linker_get_error_buffer() {
return &__linker_dl_err_buf[0]; return &__linker_dl_err_buf[0];
} }
size_t linker_get_error_buffer_size() {
return sizeof(__linker_dl_err_buf);
}
/* /*
* This function is an empty stub where GDB locates a breakpoint to get notified * This function is an empty stub where GDB locates a breakpoint to get notified
* about linker activity. * about linker activity.
@ -706,197 +703,34 @@ static int open_library(const char* name) {
return fd; return fd;
} }
// Returns 'true' if the library is prelinked or on failure so we error out
// either way. We no longer support prelinking.
static bool is_prelinked(int fd, const char* name)
{
struct prelink_info_t {
long mmap_addr;
char tag[4]; // "PRE ".
};
off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
if (sz < 0) {
DL_ERR("lseek failed: %s", strerror(errno));
return true;
}
prelink_info_t info;
int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
if (rc != sizeof(info)) {
DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
return true;
}
if (memcmp(info.tag, "PRE ", 4) == 0) {
DL_ERR("prelinked libraries no longer supported: %s", name);
return true;
}
return false;
}
/* verify_elf_header
* Verifies the content of an ELF header.
*
* Args:
*
* Returns:
* 0 on success
* -1 if no valid ELF object is found @ base.
*/
static int
verify_elf_header(const Elf32_Ehdr* hdr)
{
if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
if (hdr->e_type != ET_DYN) return -1;
/* TODO: Should we verify anything else in the header? */
#ifdef ANDROID_ARM_LINKER
if (hdr->e_machine != EM_ARM) return -1;
#elif defined(ANDROID_X86_LINKER)
if (hdr->e_machine != EM_386) return -1;
#elif defined(ANDROID_MIPS_LINKER)
if (hdr->e_machine != EM_MIPS) return -1;
#endif
return 0;
}
struct scoped_fd {
~scoped_fd() {
if (fd != -1) {
close(fd);
}
}
int fd;
};
struct soinfo_ptr {
soinfo_ptr(const char* name) {
const char* bname = strrchr(name, '/');
ptr = soinfo_alloc(bname ? bname + 1 : name);
}
~soinfo_ptr() {
soinfo_free(ptr);
}
soinfo* release() {
soinfo* result = ptr;
ptr = NULL;
return result;
}
soinfo* ptr;
};
// TODO: rewrite linker_phdr.h to use a class, then lose this.
struct phdr_ptr {
phdr_ptr() : phdr_mmap(NULL) {}
~phdr_ptr() {
if (phdr_mmap != NULL) {
phdr_table_unload(phdr_mmap, phdr_size);
}
}
void* phdr_mmap;
Elf32_Addr phdr_size;
};
static soinfo* load_library(const char* name) { static soinfo* load_library(const char* name) {
// Open the file. // Open the file.
scoped_fd fd; int fd = open_library(name);
fd.fd = open_library(name); if (fd == -1) {
if (fd.fd == -1) {
DL_ERR("library \"%s\" not found", name); DL_ERR("library \"%s\" not found", name);
return NULL; return NULL;
} }
// Read the ELF header. // Read the ELF header and load the segments.
Elf32_Ehdr header[1]; ElfReader elf_reader(name, fd);
int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header))); if (!elf_reader.Load()) {
if (ret < 0) {
DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
return NULL;
}
if (ret != (int)sizeof(header)) {
DL_ERR("too small to be an ELF executable: %s", name);
return NULL;
}
if (verify_elf_header(header) < 0) {
DL_ERR("not a valid ELF executable: %s", name);
return NULL; return NULL;
} }
// Read the program header table. const char* bname = strrchr(name, '/');
const Elf32_Phdr* phdr_table; soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
phdr_ptr phdr_holder; if (si == NULL) {
ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
&phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
if (ret < 0) {
DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
return NULL; return NULL;
} }
size_t phdr_count = header->e_phnum; si->base = elf_reader.load_start();
si->size = elf_reader.load_size();
// Get the load extents. si->load_bias = elf_reader.load_bias();
Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count); si->flags = 0;
TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz); si->entry = 0;
if (ext_sz == 0) { si->dynamic = NULL;
DL_ERR("no loadable segments in file: %s", name); si->phnum = elf_reader.phdr_count();
return NULL; si->phdr = elf_reader.loaded_phdr();
} return si;
// We no longer support pre-linked libraries.
if (is_prelinked(fd.fd, name)) {
return NULL;
}
// Reserve address space for all loadable segments.
void* load_start = NULL;
Elf32_Addr load_size = 0;
Elf32_Addr load_bias = 0;
ret = phdr_table_reserve_memory(phdr_table,
phdr_count,
&load_start,
&load_size,
&load_bias);
if (ret < 0) {
DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
ext_sz, name, strerror(errno));
return NULL;
}
TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
/* Map all the segments in our address space with default protections */
ret = phdr_table_load_segments(phdr_table,
phdr_count,
load_bias,
fd.fd);
if (ret < 0) {
DL_ERR("can't map loadable segments for \"%s\": %s",
name, strerror(errno));
return NULL;
}
soinfo_ptr si(name);
if (si.ptr == NULL) {
return NULL;
}
si.ptr->base = (Elf32_Addr) load_start;
si.ptr->size = load_size;
si.ptr->load_bias = load_bias;
si.ptr->flags = 0;
si.ptr->entry = 0;
si.ptr->dynamic = NULL;
si.ptr->phnum = phdr_count;
si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
if (si.ptr->phdr == NULL) {
DL_ERR("can't find loaded PHDR for \"%s\"", name);
return NULL;
}
return si.release();
} }
static soinfo *find_loaded_library(const char *name) static soinfo *find_loaded_library(const char *name)
@ -1686,7 +1520,7 @@ static bool soinfo_link_image(soinfo* si) {
for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) { for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
soinfo* lsi = find_library(gLdPreloadNames[i]); soinfo* lsi = find_library(gLdPreloadNames[i]);
if (lsi == NULL) { if (lsi == NULL) {
strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); 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", DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
gLdPreloadNames[i], si->name, tmp_err_buf); gLdPreloadNames[i], si->name, tmp_err_buf);
return false; return false;
@ -1704,7 +1538,7 @@ static bool soinfo_link_image(soinfo* si) {
DEBUG("%s needs %s\n", si->name, library_name); DEBUG("%s needs %s\n", si->name, library_name);
soinfo* lsi = find_library(library_name); soinfo* lsi = find_library(library_name);
if (lsi == NULL) { if (lsi == NULL) {
strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); 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", DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
library_name, si->name, tmp_err_buf); library_name, si->name, tmp_err_buf);
return false; return false;
@ -1804,7 +1638,7 @@ static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigne
// Get a few environment variables. // Get a few environment variables.
const char* LD_DEBUG = linker_env_get("LD_DEBUG"); const char* LD_DEBUG = linker_env_get("LD_DEBUG");
if (LD_DEBUG != NULL) { if (LD_DEBUG != NULL) {
debug_verbosity = atoi(LD_DEBUG); gLdDebugVerbosity = atoi(LD_DEBUG);
} }
// Normally, these are cleaned by linker_env_init, but the test // Normally, these are cleaned by linker_env_init, but the test
@ -1890,9 +1724,7 @@ static unsigned __linker_init_post_relocation(KernelArgumentBlock& args, unsigne
somain = si; somain = si;
if (!soinfo_link_image(si)) { if (!soinfo_link_image(si)) {
const char* msg = "CANNOT LINK EXECUTABLE\n"; __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
write(2, msg, strlen(msg));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -36,6 +36,15 @@
#include <link.h> #include <link.h>
#include <private/debug_format.h>
#define DL_ERR(fmt, x...) \
do { \
__libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
/* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
DEBUG("%s\n", linker_get_error_buffer()); \
} while(0)
// Returns the address of the page containing address 'x'. // Returns the address of the page containing address 'x'.
#define PAGE_START(x) ((x) & PAGE_MASK) #define PAGE_START(x) ((x) & PAGE_MASK)
@ -169,7 +178,6 @@ int do_dlclose(soinfo* si);
Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start); Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
soinfo* find_containing_library(const void* addr); soinfo* find_containing_library(const void* addr);
const char* linker_get_error();
Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr); Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr);
Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name); Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
@ -177,4 +185,7 @@ Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
void debuggerd_init(); void debuggerd_init();
extern "C" void notify_gdb_of_libraries(); extern "C" void notify_gdb_of_libraries();
char* linker_get_error_buffer();
size_t linker_get_error_buffer_size();
#endif #endif

View file

@ -54,15 +54,17 @@
#include <private/debug_format.h> #include <private/debug_format.h>
__LIBC_HIDDEN__ extern int gLdDebugVerbosity;
#if LINKER_DEBUG_TO_LOG #if LINKER_DEBUG_TO_LOG
#define _PRINTVF(v,x...) \ #define _PRINTVF(v,x...) \
do { \ do { \
if (debug_verbosity > (v)) __libc_format_log(5-(v),"linker",x); \ if (gLdDebugVerbosity > (v)) __libc_format_log(5-(v),"linker",x); \
} while (0) } while (0)
#else /* !LINKER_DEBUG_TO_LOG */ #else /* !LINKER_DEBUG_TO_LOG */
#define _PRINTVF(v,x...) \ #define _PRINTVF(v,x...) \
do { \ do { \
if (debug_verbosity > (v)) __libc_format_fd(1, x); \ if (gLdDebugVerbosity > (v)) __libc_format_fd(1, x); \
} while (0) } while (0)
#endif /* !LINKER_DEBUG_TO_LOG */ #endif /* !LINKER_DEBUG_TO_LOG */

View file

@ -26,10 +26,13 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include "linker_phdr.h"
#include <errno.h> #include <errno.h>
#include <sys/mman.h> #include <sys/mman.h>
#include "linker_phdr.h" #include "linker.h"
#include "linker_debug.h"
/** /**
TECHNICAL NOTE ON ELF LOADING. TECHNICAL NOTE ON ELF LOADING.
@ -112,67 +115,117 @@
MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \ MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE)) MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
/* Load the program header table from an ELF file into a read-only private ElfReader::ElfReader(const char* name, int fd)
* anonymous mmap-ed block. : name_(name), fd_(fd),
* phdr_num_(0), phdr_mmap_(NULL), phdr_table_(NULL), phdr_size_(0),
* Input: load_start_(NULL), load_size_(0), load_bias_(0),
* fd -> file descriptor loaded_phdr_(NULL) {
* phdr_offset -> file offset of phdr table }
* phdr_num -> number of entries in the table.
*
* Output:
* phdr_mmap -> address of mmap block in memory.
* phdr_memsize -> size of mmap block in memory.
* phdr_table -> address of first entry in memory.
*
* Return:
* -1 on error, or 0 on success.
*/
int phdr_table_load(int fd,
Elf32_Addr phdr_offset,
Elf32_Half phdr_num,
void** phdr_mmap,
Elf32_Addr* phdr_size,
const Elf32_Phdr** phdr_table)
{
Elf32_Addr page_min, page_max, page_offset;
void* mmap_result;
/* Just like the kernel, we only accept program header tables that ElfReader::~ElfReader() {
* are smaller than 64KB. */ if (fd_ != -1) {
if (phdr_num < 1 || phdr_num > 65536/sizeof(Elf32_Phdr)) { close(fd_);
errno = EINVAL; }
return -1; if (phdr_mmap_ != NULL) {
munmap(phdr_mmap_, phdr_size_);
}
}
bool ElfReader::Load() {
return ReadElfHeader() &&
VerifyElfHeader() &&
ReadProgramHeader() &&
ReserveAddressSpace() &&
LoadSegments() &&
FindPhdr();
}
bool ElfReader::ReadElfHeader() {
ssize_t rc = TEMP_FAILURE_RETRY(read(fd_, &header_, sizeof(header_)));
if (rc < 0) {
DL_ERR("can't read file \"%s\": %s", name_, strerror(errno));
return false;
}
if (rc != sizeof(header_)) {
DL_ERR("\"%s\" is too small to be an ELF executable", name_);
return false;
}
return true;
}
bool ElfReader::VerifyElfHeader() {
if (header_.e_ident[EI_MAG0] != ELFMAG0 ||
header_.e_ident[EI_MAG1] != ELFMAG1 ||
header_.e_ident[EI_MAG2] != ELFMAG2 ||
header_.e_ident[EI_MAG3] != ELFMAG3) {
DL_ERR("\"%s\" has bad ELF magic", name_);
return false;
} }
page_min = PAGE_START(phdr_offset); if (header_.e_ident[EI_CLASS] != ELFCLASS32) {
page_max = PAGE_END(phdr_offset + phdr_num*sizeof(Elf32_Phdr)); DL_ERR("\"%s\" not 32-bit: %d", name_, header_.e_ident[EI_CLASS]);
page_offset = PAGE_OFFSET(phdr_offset); return false;
}
if (header_.e_ident[EI_DATA] != ELFDATA2LSB) {
DL_ERR("\"%s\" not little-endian: %d", name_, header_.e_ident[EI_DATA]);
return false;
}
mmap_result = mmap(NULL, if (header_.e_type != ET_DYN) {
page_max - page_min, DL_ERR("\"%s\" has unexpected e_type: %d", name_, header_.e_type);
PROT_READ, return false;
MAP_PRIVATE, }
fd,
page_min);
if (header_.e_version != EV_CURRENT) {
DL_ERR("\"%s\" has unexpected e_version: %d", name_, header_.e_version);
return false;
}
if (header_.e_machine !=
#ifdef ANDROID_ARM_LINKER
EM_ARM
#elif defined(ANDROID_MIPS_LINKER)
EM_MIPS
#elif defined(ANDROID_X86_LINKER)
EM_386
#endif
) {
DL_ERR("\"%s\" has unexpected e_machine: %d", name_, header_.e_machine);
return false;
}
return true;
}
// Loads the program header table from an ELF file into a read-only private
// anonymous mmap-ed block.
bool ElfReader::ReadProgramHeader() {
phdr_num_ = header_.e_phnum;
// Like the kernel, we only accept program header tables that
// are smaller than 64KiB.
if (phdr_num_ < 1 || phdr_num_ > 65536/sizeof(Elf32_Phdr)) {
DL_ERR("\"%s\" has invalid e_phnum: %d", name_, phdr_num_);
return false;
}
Elf32_Addr page_min = PAGE_START(header_.e_phoff);
Elf32_Addr page_max = PAGE_END(header_.e_phoff + (phdr_num_ * sizeof(Elf32_Phdr)));
Elf32_Addr page_offset = PAGE_OFFSET(header_.e_phoff);
phdr_size_ = page_max - page_min;
void* mmap_result = mmap(NULL, phdr_size_, PROT_READ, MAP_PRIVATE, fd_, page_min);
if (mmap_result == MAP_FAILED) { if (mmap_result == MAP_FAILED) {
return -1; DL_ERR("\"%s\" phdr mmap failed: %s", name_, strerror(errno));
return false;
} }
*phdr_mmap = mmap_result; phdr_mmap_ = mmap_result;
*phdr_size = page_max - page_min; phdr_table_ = reinterpret_cast<Elf32_Phdr*>(reinterpret_cast<char*>(mmap_result) + page_offset);
*phdr_table = (Elf32_Phdr*)((char*)mmap_result + page_offset); return true;
return 0;
} }
void phdr_table_unload(void* phdr_mmap, Elf32_Addr phdr_memsize)
{
munmap(phdr_mmap, phdr_memsize);
}
/* Compute the extent of all loadable segments in an ELF program header /* Compute the extent of all loadable segments in an ELF program header
* table. This corresponds to the page-aligned size in bytes that needs to be * table. This corresponds to the page-aligned size in bytes that needs to be
* reserved in the process' address space * reserved in the process' address space
@ -211,84 +264,50 @@ Elf32_Addr phdr_table_get_load_size(const Elf32_Phdr* phdr_table,
return max_vaddr - min_vaddr; return max_vaddr - min_vaddr;
} }
/* Reserve a virtual address range big enough to hold all loadable // Reserve a virtual address range big enough to hold all loadable
* segments of a program header table. This is done by creating a // segments of a program header table. This is done by creating a
* private anonymous mmap() with PROT_NONE. // private anonymous mmap() with PROT_NONE.
* bool ElfReader::ReserveAddressSpace() {
* Input: load_size_ = phdr_table_get_load_size(phdr_table_, phdr_num_);
* phdr_table -> program header table if (load_size_ == 0) {
* phdr_count -> number of entries in the tables DL_ERR("\"%s\" has no loadable segments", name_);
* Output: return false;
* load_start -> first page of reserved address space range
* load_size -> size in bytes of reserved address space range
* load_bias -> load bias, as described in technical note above.
*
* Return:
* 0 on success, -1 otherwise. Error code in errno.
*/
int
phdr_table_reserve_memory(const Elf32_Phdr* phdr_table,
size_t phdr_count,
void** load_start,
Elf32_Addr* load_size,
Elf32_Addr* load_bias)
{
Elf32_Addr size = phdr_table_get_load_size(phdr_table, phdr_count);
if (size == 0) {
errno = EINVAL;
return -1;
} }
int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
void* start = mmap(NULL, size, PROT_NONE, mmap_flags, -1, 0); void* start = mmap(NULL, load_size_, PROT_NONE, mmap_flags, -1, 0);
if (start == MAP_FAILED) { if (start == MAP_FAILED) {
return -1; DL_ERR("couldn't reserve %d bytes of address space for \"%s\"", load_size_, name_);
return false;
} }
*load_start = start; load_start_ = start;
*load_size = size; load_bias_ = 0;
*load_bias = 0;
for (size_t i = 0; i < phdr_count; ++i) { for (size_t i = 0; i < phdr_num_; ++i) {
const Elf32_Phdr* phdr = &phdr_table[i]; const Elf32_Phdr* phdr = &phdr_table_[i];
if (phdr->p_type == PT_LOAD) { if (phdr->p_type == PT_LOAD) {
*load_bias = (Elf32_Addr)start - PAGE_START(phdr->p_vaddr); load_bias_ = reinterpret_cast<Elf32_Addr>(start) - PAGE_START(phdr->p_vaddr);
break; break;
} }
} }
return 0; return true;
} }
/* Map all loadable segments in process' address space. // Map all loadable segments in process' address space.
* This assumes you already called phdr_table_reserve_memory to // This assumes you already called phdr_table_reserve_memory to
* reserve the address space range for the library. // reserve the address space range for the library.
* // TODO: assert assumption.
* Input: bool ElfReader::LoadSegments() {
* phdr_table -> program header table for (size_t i = 0; i < phdr_num_; ++i) {
* phdr_count -> number of entries in the table const Elf32_Phdr* phdr = &phdr_table_[i];
* load_bias -> load offset.
* fd -> input file descriptor.
*
* Return:
* 0 on success, -1 otherwise. Error code in errno.
*/
int
phdr_table_load_segments(const Elf32_Phdr* phdr_table,
int phdr_count,
Elf32_Addr load_bias,
int fd)
{
int nn;
for (nn = 0; nn < phdr_count; nn++) { if (phdr->p_type != PT_LOAD) {
const Elf32_Phdr* phdr = &phdr_table[nn];
void* seg_addr;
if (phdr->p_type != PT_LOAD)
continue; continue;
}
/* Segment addresses in memory */ // Segment addresses in memory.
Elf32_Addr seg_start = phdr->p_vaddr + load_bias; Elf32_Addr seg_start = phdr->p_vaddr + load_bias_;
Elf32_Addr seg_end = seg_start + phdr->p_memsz; Elf32_Addr seg_end = seg_start + phdr->p_memsz;
Elf32_Addr seg_page_start = PAGE_START(seg_start); Elf32_Addr seg_page_start = PAGE_START(seg_start);
@ -296,36 +315,35 @@ phdr_table_load_segments(const Elf32_Phdr* phdr_table,
Elf32_Addr seg_file_end = seg_start + phdr->p_filesz; Elf32_Addr seg_file_end = seg_start + phdr->p_filesz;
/* File offsets */ // File offsets.
Elf32_Addr file_start = phdr->p_offset; Elf32_Addr file_start = phdr->p_offset;
Elf32_Addr file_end = file_start + phdr->p_filesz; Elf32_Addr file_end = file_start + phdr->p_filesz;
Elf32_Addr file_page_start = PAGE_START(file_start); Elf32_Addr file_page_start = PAGE_START(file_start);
seg_addr = mmap((void*)seg_page_start, void* seg_addr = mmap((void*)seg_page_start,
file_end - file_page_start, file_end - file_page_start,
PFLAGS_TO_PROT(phdr->p_flags), PFLAGS_TO_PROT(phdr->p_flags),
MAP_FIXED|MAP_PRIVATE, MAP_FIXED|MAP_PRIVATE,
fd, fd_,
file_page_start); file_page_start);
if (seg_addr == MAP_FAILED) { if (seg_addr == MAP_FAILED) {
return -1; DL_ERR("couldn't map \"%s\" segment %d: %s", name_, i, strerror(errno));
return false;
} }
/* if the segment is writable, and does not end on a page boundary, // if the segment is writable, and does not end on a page boundary,
* zero-fill it until the page limit. */ // zero-fill it until the page limit.
if ((phdr->p_flags & PF_W) != 0 && PAGE_OFFSET(seg_file_end) > 0) { if ((phdr->p_flags & PF_W) != 0 && PAGE_OFFSET(seg_file_end) > 0) {
memset((void*)seg_file_end, 0, PAGE_SIZE - PAGE_OFFSET(seg_file_end)); memset((void*)seg_file_end, 0, PAGE_SIZE - PAGE_OFFSET(seg_file_end));
} }
seg_file_end = PAGE_END(seg_file_end); seg_file_end = PAGE_END(seg_file_end);
/* seg_file_end is now the first page address after the file // seg_file_end is now the first page address after the file
* content. If seg_end is larger, we need to zero anything // content. If seg_end is larger, we need to zero anything
* between them. This is done by using a private anonymous // between them. This is done by using a private anonymous
* map for all extra pages. // map for all extra pages.
*/
if (seg_page_end > seg_file_end) { if (seg_page_end > seg_file_end) {
void* zeromap = mmap((void*)seg_file_end, void* zeromap = mmap((void*)seg_file_end,
seg_page_end - seg_file_end, seg_page_end - seg_file_end,
@ -334,11 +352,12 @@ phdr_table_load_segments(const Elf32_Phdr* phdr_table,
-1, -1,
0); 0);
if (zeromap == MAP_FAILED) { if (zeromap == MAP_FAILED) {
return -1; DL_ERR("couldn't zero fill \"%s\" gap: %s", name_, strerror(errno));
return false;
} }
} }
} }
return 0; return true;
} }
/* Used internally. Used to set the protection bits of all loaded segments /* Used internally. Used to set the protection bits of all loaded segments
@ -577,72 +596,55 @@ phdr_table_get_dynamic_section(const Elf32_Phdr* phdr_table,
} }
} }
/* Return the address of the program header table as it appears in the loaded // Returns the address of the program header table as it appears in the loaded
* segments in memory. This is in contrast with the input 'phdr_table' which // segments in memory. This is in contrast with 'phdr_table_' which
* is temporary and will be released before the library is relocated. // is temporary and will be released before the library is relocated.
* bool ElfReader::FindPhdr() {
* Input: const Elf32_Phdr* phdr_limit = phdr_table_ + phdr_num_;
* phdr_table -> program header table
* phdr_count -> number of entries in tables
* load_bias -> load bias
* Return:
* Address of loaded program header table on success (it has
* 'phdr_count' entries), or NULL on failure (no error code).
*/
const Elf32_Phdr*
phdr_table_get_loaded_phdr(const Elf32_Phdr* phdr_table,
int phdr_count,
Elf32_Addr load_bias)
{
const Elf32_Phdr* phdr = phdr_table;
const Elf32_Phdr* phdr_limit = phdr + phdr_count;
Elf32_Addr loaded = 0;
Elf32_Addr loaded_end;
/* If there is a PT_PHDR, use it directly */ // If there is a PT_PHDR, use it directly.
for (phdr = phdr_table; phdr < phdr_limit; phdr++) { for (const Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
if (phdr->p_type == PT_PHDR) { if (phdr->p_type == PT_PHDR) {
loaded = load_bias + phdr->p_vaddr; return CheckPhdr(load_bias_ + phdr->p_vaddr);
goto CHECK;
} }
} }
/* Otherwise, check the first loadable segment. If its file offset // Otherwise, check the first loadable segment. If its file offset
* is 0, it starts with the ELF header, and we can trivially find the // is 0, it starts with the ELF header, and we can trivially find the
* loaded program header from it. */ // loaded program header from it.
for (phdr = phdr_table; phdr < phdr_limit; phdr++) { for (const Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
if (phdr->p_type == PT_LOAD) { if (phdr->p_type == PT_LOAD) {
if (phdr->p_offset == 0) { if (phdr->p_offset == 0) {
Elf32_Addr elf_addr = load_bias + phdr->p_vaddr; Elf32_Addr elf_addr = load_bias_ + phdr->p_vaddr;
const Elf32_Ehdr* ehdr = (const Elf32_Ehdr*)(void*)elf_addr; const Elf32_Ehdr* ehdr = (const Elf32_Ehdr*)(void*)elf_addr;
Elf32_Addr offset = ehdr->e_phoff; Elf32_Addr offset = ehdr->e_phoff;
loaded = (Elf32_Addr)ehdr + offset; return CheckPhdr((Elf32_Addr)ehdr + offset);
goto CHECK;
} }
break; break;
} }
} }
/* We didn't find it, let the client know. He may be able to DL_ERR("can't find loaded phdr for \"%s\"", name_);
* keep a copy of the input phdr_table instead. */ return false;
return NULL; }
CHECK: // Ensures that our program header is actually within a loadable
/* Ensure that our program header is actually within a loadable // segment. This should help catch badly-formed ELF files that
* segment. This should help catch badly-formed ELF files that // would cause the linker to crash later when trying to access it.
* would cause the linker to crash later when trying to access it. bool ElfReader::CheckPhdr(Elf32_Addr loaded) {
*/ const Elf32_Phdr* phdr_limit = phdr_table_ + phdr_num_;
loaded_end = loaded + phdr_count*sizeof(Elf32_Phdr); Elf32_Addr loaded_end = loaded + (phdr_num_ * sizeof(Elf32_Phdr));
for (Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
for (phdr = phdr_table; phdr < phdr_limit; phdr++) { if (phdr->p_type != PT_LOAD) {
if (phdr->p_type != PT_LOAD) continue;
continue; }
Elf32_Addr seg_start = phdr->p_vaddr + load_bias; Elf32_Addr seg_start = phdr->p_vaddr + load_bias_;
Elf32_Addr seg_end = phdr->p_filesz + seg_start; Elf32_Addr seg_end = phdr->p_filesz + seg_start;
if (seg_start <= loaded && loaded_end <= seg_end) {
if (seg_start <= loaded && loaded_end <= seg_end) { loaded_phdr_ = reinterpret_cast<const Elf32_Phdr*>(loaded);
return (const Elf32_Phdr*)loaded; return true;
} }
} }
return NULL; DL_ERR("\"%s\" loaded phdr %x not in loadable segment", name_, loaded);
return false;
} }

View file

@ -37,33 +37,50 @@
#include "linker.h" #include "linker.h"
int class ElfReader {
phdr_table_load(int fd, public:
Elf32_Addr phdr_offset, ElfReader(const char* name, int fd);
Elf32_Half phdr_num, ~ElfReader();
void** phdr_mmap,
Elf32_Addr* phdr_size,
const Elf32_Phdr** phdr_table);
void bool Load();
phdr_table_unload(void* phdr_mmap, Elf32_Addr phdr_memsize);
Elf32_Addr size_t phdr_count() { return phdr_num_; }
phdr_table_get_load_size(const Elf32_Phdr* phdr_table, Elf32_Addr load_start() { return reinterpret_cast<Elf32_Addr>(load_start_); }
size_t phdr_count); Elf32_Addr load_size() { return load_size_; }
Elf32_Addr load_bias() { return load_bias_; }
const Elf32_Phdr* loaded_phdr() { return loaded_phdr_; }
int private:
phdr_table_reserve_memory(const Elf32_Phdr* phdr_table, bool ReadElfHeader();
size_t phdr_count, bool VerifyElfHeader();
void** load_start, bool ReadProgramHeader();
Elf32_Addr* load_size, bool ReserveAddressSpace();
Elf32_Addr* load_bias); bool LoadSegments();
bool FindPhdr();
bool CheckPhdr(Elf32_Addr);
int const char* name_;
phdr_table_load_segments(const Elf32_Phdr* phdr_table, int fd_;
int phdr_count,
Elf32_Addr load_bias, Elf32_Ehdr header_;
int fd); size_t phdr_num_;
void* phdr_mmap_;
Elf32_Phdr* phdr_table_;
Elf32_Addr phdr_size_;
// First page of reserved address space.
void* load_start_;
// Size in bytes of reserved address space.
Elf32_Addr load_size_;
// Load bias.
Elf32_Addr load_bias_;
// Loaded phdr.
const Elf32_Phdr* loaded_phdr_;
};
Elf32_Addr phdr_table_get_load_size(const Elf32_Phdr* phdr, size_t phnum);
int int
phdr_table_protect_segments(const Elf32_Phdr* phdr_table, phdr_table_protect_segments(const Elf32_Phdr* phdr_table,
@ -80,10 +97,6 @@ phdr_table_protect_gnu_relro(const Elf32_Phdr* phdr_table,
int phdr_count, int phdr_count,
Elf32_Addr load_bias); Elf32_Addr load_bias);
const Elf32_Phdr*
phdr_table_get_loaded_phdr(const Elf32_Phdr* phdr_table,
int phdr_count,
Elf32_Addr load_bias);
#ifdef ANDROID_ARM_LINKER #ifdef ANDROID_ARM_LINKER
int int