Merge "More linker cleanup."
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 0603d40..0b38ec4 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -64,7 +64,7 @@
   ScopedPthreadMutexLocker locker(&gDlMutex);
   soinfo* result = do_dlopen(filename, flags);
   if (result == NULL) {
-    __bionic_format_dlerror("dlopen failed", linker_get_error());
+    __bionic_format_dlerror("dlopen failed", linker_get_error_buffer());
     return NULL;
   }
   return result;
diff --git a/linker/linker.cpp b/linker/linker.cpp
index e08eaba..f18443c 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -41,7 +41,6 @@
 
 // Private C library headers.
 #include <private/bionic_tls.h>
-#include <private/debug_format.h>
 #include <private/KernelArgumentBlock.h>
 #include <private/logd.h>
 #include <private/ScopedPthreadMutexLocker.h>
@@ -105,7 +104,7 @@
 
 static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
 
-static int debug_verbosity;
+__LIBC_HIDDEN__ int gLdDebugVerbosity;
 
 enum RelocationKind {
     kRelocAbsolute = 0,
@@ -158,17 +157,15 @@
 
 static char tmp_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];
 }
 
+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
  * about linker activity.
@@ -706,197 +703,34 @@
   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) {
     // Open the file.
-    scoped_fd fd;
-    fd.fd = open_library(name);
-    if (fd.fd == -1) {
+    int fd = open_library(name);
+    if (fd == -1) {
         DL_ERR("library \"%s\" not found", name);
         return NULL;
     }
 
-    // Read the ELF header.
-    Elf32_Ehdr header[1];
-    int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
-    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);
+    // Read the ELF header and load the segments.
+    ElfReader elf_reader(name, fd);
+    if (!elf_reader.Load()) {
         return NULL;
     }
 
-    // Read the program header table.
-    const Elf32_Phdr* phdr_table;
-    phdr_ptr phdr_holder;
-    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));
+    const char* bname = strrchr(name, '/');
+    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
+    if (si == NULL) {
         return NULL;
     }
-    size_t phdr_count = header->e_phnum;
-
-    // Get the load extents.
-    Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
-    TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
-    if (ext_sz == 0) {
-        DL_ERR("no loadable segments in file: %s", name);
-        return NULL;
-    }
-
-    // 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();
+    si->base = elf_reader.load_start();
+    si->size = elf_reader.load_size();
+    si->load_bias = elf_reader.load_bias();
+    si->flags = 0;
+    si->entry = 0;
+    si->dynamic = NULL;
+    si->phnum = elf_reader.phdr_count();
+    si->phdr = elf_reader.loaded_phdr();
+    return si;
 }
 
 static soinfo *find_loaded_library(const char *name)
@@ -1686,7 +1520,7 @@
         for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
             soinfo* lsi = find_library(gLdPreloadNames[i]);
             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",
                        gLdPreloadNames[i], si->name, tmp_err_buf);
                 return false;
@@ -1704,7 +1538,7 @@
             DEBUG("%s needs %s\n", si->name, library_name);
             soinfo* lsi = find_library(library_name);
             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",
                        library_name, si->name, tmp_err_buf);
                 return false;
@@ -1804,7 +1638,7 @@
     // Get a few environment variables.
     const char* LD_DEBUG = linker_env_get("LD_DEBUG");
     if (LD_DEBUG != NULL) {
-      debug_verbosity = atoi(LD_DEBUG);
+      gLdDebugVerbosity = atoi(LD_DEBUG);
     }
 
     // Normally, these are cleaned by linker_env_init, but the test
@@ -1890,9 +1724,7 @@
     somain = si;
 
     if (!soinfo_link_image(si)) {
-        const char* msg = "CANNOT LINK EXECUTABLE\n";
-        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
-        write(2, msg, strlen(msg));
+        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
         exit(EXIT_FAILURE);
     }
 
diff --git a/linker/linker.h b/linker/linker.h
index dac555a..cef6905 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -36,6 +36,15 @@
 
 #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'.
 #define PAGE_START(x)  ((x) & PAGE_MASK)
 
@@ -169,7 +178,6 @@
 
 Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
 soinfo* find_containing_library(const void* addr);
-const char* linker_get_error();
 
 Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr);
 Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name);
@@ -177,4 +185,7 @@
 void debuggerd_init();
 extern "C" void notify_gdb_of_libraries();
 
+char* linker_get_error_buffer();
+size_t linker_get_error_buffer_size();
+
 #endif
diff --git a/linker/linker_debug.h b/linker/linker_debug.h
index b326d00..bf9ef92 100644
--- a/linker/linker_debug.h
+++ b/linker/linker_debug.h
@@ -54,15 +54,17 @@
 
 #include <private/debug_format.h>
 
+__LIBC_HIDDEN__ extern int gLdDebugVerbosity;
+
 #if LINKER_DEBUG_TO_LOG
 #define _PRINTVF(v,x...)                                        \
     do {                                                          \
-        if (debug_verbosity > (v)) __libc_format_log(5-(v),"linker",x);  \
+        if (gLdDebugVerbosity > (v)) __libc_format_log(5-(v),"linker",x);  \
     } while (0)
 #else /* !LINKER_DEBUG_TO_LOG */
 #define _PRINTVF(v,x...)                           \
     do {                                             \
-        if (debug_verbosity > (v)) __libc_format_fd(1, x);  \
+        if (gLdDebugVerbosity > (v)) __libc_format_fd(1, x);  \
     } while (0)
 #endif /* !LINKER_DEBUG_TO_LOG */
 
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index acd18cd..64dbb70 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -26,10 +26,13 @@
  * SUCH DAMAGE.
  */
 
+#include "linker_phdr.h"
+
 #include <errno.h>
 #include <sys/mman.h>
 
-#include "linker_phdr.h"
+#include "linker.h"
+#include "linker_debug.h"
 
 /**
   TECHNICAL NOTE ON ELF LOADING.
@@ -112,66 +115,116 @@
                                       MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
                                       MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
 
-/* Load the program header table from an ELF file into a read-only private
- * anonymous mmap-ed block.
- *
- * Input:
- *   fd           -> file descriptor
- *   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
-     * are smaller than 64KB. */
-    if (phdr_num < 1 || phdr_num > 65536/sizeof(Elf32_Phdr)) {
-        errno = EINVAL;
-        return -1;
-    }
-
-    page_min = PAGE_START(phdr_offset);
-    page_max = PAGE_END(phdr_offset + phdr_num*sizeof(Elf32_Phdr));
-    page_offset = PAGE_OFFSET(phdr_offset);
-
-    mmap_result = mmap(NULL,
-                       page_max - page_min,
-                       PROT_READ,
-                       MAP_PRIVATE,
-                       fd,
-                       page_min);
-
-    if (mmap_result == MAP_FAILED) {
-        return -1;
-    }
-
-    *phdr_mmap = mmap_result;
-    *phdr_size = page_max - page_min;
-    *phdr_table = (Elf32_Phdr*)((char*)mmap_result + page_offset);
-
-    return 0;
+ElfReader::ElfReader(const char* name, int fd)
+    : name_(name), fd_(fd),
+      phdr_num_(0), phdr_mmap_(NULL), phdr_table_(NULL), phdr_size_(0),
+      load_start_(NULL), load_size_(0), load_bias_(0),
+      loaded_phdr_(NULL) {
 }
 
-void phdr_table_unload(void* phdr_mmap, Elf32_Addr phdr_memsize)
-{
-    munmap(phdr_mmap, phdr_memsize);
+ElfReader::~ElfReader() {
+  if (fd_ != -1) {
+    close(fd_);
+  }
+  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;
+  }
+
+  if (header_.e_ident[EI_CLASS] != ELFCLASS32) {
+    DL_ERR("\"%s\" not 32-bit: %d", name_, header_.e_ident[EI_CLASS]);
+    return false;
+  }
+  if (header_.e_ident[EI_DATA] != ELFDATA2LSB) {
+    DL_ERR("\"%s\" not little-endian: %d", name_, header_.e_ident[EI_DATA]);
+    return false;
+  }
+
+  if (header_.e_type != ET_DYN) {
+    DL_ERR("\"%s\" has unexpected e_type: %d", name_, header_.e_type);
+    return false;
+  }
+
+  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) {
+    DL_ERR("\"%s\" phdr mmap failed: %s", name_, strerror(errno));
+    return false;
+  }
+
+  phdr_mmap_ = mmap_result;
+  phdr_table_ = reinterpret_cast<Elf32_Phdr*>(reinterpret_cast<char*>(mmap_result) + page_offset);
+  return true;
+}
 
 /* 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
@@ -211,134 +264,100 @@
     return max_vaddr - min_vaddr;
 }
 
-/* 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.
- *
- * Input:
- *   phdr_table    -> program header table
- *   phdr_count    -> number of entries in the tables
- * Output:
- *   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;
-    }
+// 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() {
+  load_size_ = phdr_table_get_load_size(phdr_table_, phdr_num_);
+  if (load_size_ == 0) {
+    DL_ERR("\"%s\" has no loadable segments", name_);
+    return false;
+  }
 
-    int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
-    void* start = mmap(NULL, size, PROT_NONE, mmap_flags, -1, 0);
-    if (start == MAP_FAILED) {
-        return -1;
-    }
+  int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
+  void* start = mmap(NULL, load_size_, PROT_NONE, mmap_flags, -1, 0);
+  if (start == MAP_FAILED) {
+    DL_ERR("couldn't reserve %d bytes of address space for \"%s\"", load_size_, name_);
+    return false;
+  }
 
-    *load_start = start;
-    *load_size  = size;
-    *load_bias  = 0;
+  load_start_ = start;
+  load_bias_ = 0;
 
-    for (size_t i = 0; i < phdr_count; ++i) {
-        const Elf32_Phdr* phdr = &phdr_table[i];
-        if (phdr->p_type == PT_LOAD) {
-            *load_bias = (Elf32_Addr)start - PAGE_START(phdr->p_vaddr);
-            break;
-        }
+  for (size_t i = 0; i < phdr_num_; ++i) {
+    const Elf32_Phdr* phdr = &phdr_table_[i];
+    if (phdr->p_type == PT_LOAD) {
+      load_bias_ = reinterpret_cast<Elf32_Addr>(start) - PAGE_START(phdr->p_vaddr);
+      break;
     }
-    return 0;
+  }
+  return true;
 }
 
-/* Map all loadable segments in process' address space.
- * This assumes you already called phdr_table_reserve_memory to
- * reserve the address space range for the library.
- *
- * Input:
- *   phdr_table    -> program header table
- *   phdr_count    -> number of entries in the table
- *   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;
+// Map all loadable segments in process' address space.
+// This assumes you already called phdr_table_reserve_memory to
+// reserve the address space range for the library.
+// TODO: assert assumption.
+bool ElfReader::LoadSegments() {
+  for (size_t i = 0; i < phdr_num_; ++i) {
+    const Elf32_Phdr* phdr = &phdr_table_[i];
 
-    for (nn = 0; nn < phdr_count; nn++) {
-        const Elf32_Phdr* phdr = &phdr_table[nn];
-        void* seg_addr;
-
-        if (phdr->p_type != PT_LOAD)
-            continue;
-
-        /* Segment addresses in memory */
-        Elf32_Addr seg_start = phdr->p_vaddr + load_bias;
-        Elf32_Addr seg_end   = seg_start + phdr->p_memsz;
-
-        Elf32_Addr seg_page_start = PAGE_START(seg_start);
-        Elf32_Addr seg_page_end   = PAGE_END(seg_end);
-
-        Elf32_Addr seg_file_end   = seg_start + phdr->p_filesz;
-
-        /* File offsets */
-        Elf32_Addr file_start = phdr->p_offset;
-        Elf32_Addr file_end   = file_start + phdr->p_filesz;
-
-        Elf32_Addr file_page_start = PAGE_START(file_start);
-
-        seg_addr = mmap((void*)seg_page_start,
-                        file_end - file_page_start,
-                        PFLAGS_TO_PROT(phdr->p_flags),
-                        MAP_FIXED|MAP_PRIVATE,
-                        fd,
-                        file_page_start);
-
-        if (seg_addr == MAP_FAILED) {
-            return -1;
-        }
-
-        /* if the segment is writable, and does not end on a page boundary,
-         * zero-fill it until the page limit. */
-        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));
-        }
-
-        seg_file_end = PAGE_END(seg_file_end);
-
-        /* seg_file_end is now the first page address after the file
-         * content. If seg_end is larger, we need to zero anything
-         * between them. This is done by using a private anonymous
-         * map for all extra pages.
-         */
-        if (seg_page_end > seg_file_end) {
-            void* zeromap = mmap((void*)seg_file_end,
-                                    seg_page_end - seg_file_end,
-                                    PFLAGS_TO_PROT(phdr->p_flags),
-                                    MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
-                                    -1,
-                                    0);
-            if (zeromap == MAP_FAILED) {
-                return -1;
-            }
-        }
+    if (phdr->p_type != PT_LOAD) {
+      continue;
     }
-    return 0;
+
+    // Segment addresses in memory.
+    Elf32_Addr seg_start = phdr->p_vaddr + load_bias_;
+    Elf32_Addr seg_end   = seg_start + phdr->p_memsz;
+
+    Elf32_Addr seg_page_start = PAGE_START(seg_start);
+    Elf32_Addr seg_page_end   = PAGE_END(seg_end);
+
+    Elf32_Addr seg_file_end   = seg_start + phdr->p_filesz;
+
+    // File offsets.
+    Elf32_Addr file_start = phdr->p_offset;
+    Elf32_Addr file_end   = file_start + phdr->p_filesz;
+
+    Elf32_Addr file_page_start = PAGE_START(file_start);
+
+    void* seg_addr = mmap((void*)seg_page_start,
+                          file_end - file_page_start,
+                          PFLAGS_TO_PROT(phdr->p_flags),
+                          MAP_FIXED|MAP_PRIVATE,
+                          fd_,
+                          file_page_start);
+    if (seg_addr == MAP_FAILED) {
+      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,
+    // zero-fill it until the page limit.
+    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));
+    }
+
+    seg_file_end = PAGE_END(seg_file_end);
+
+    // seg_file_end is now the first page address after the file
+    // content. If seg_end is larger, we need to zero anything
+    // between them. This is done by using a private anonymous
+    // map for all extra pages.
+    if (seg_page_end > seg_file_end) {
+      void* zeromap = mmap((void*)seg_file_end,
+                           seg_page_end - seg_file_end,
+                           PFLAGS_TO_PROT(phdr->p_flags),
+                           MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
+                           -1,
+                           0);
+      if (zeromap == MAP_FAILED) {
+        DL_ERR("couldn't zero fill \"%s\" gap: %s", name_, strerror(errno));
+        return false;
+      }
+    }
+  }
+  return true;
 }
 
 /* Used internally. Used to set the protection bits of all loaded segments
@@ -577,72 +596,55 @@
     }
 }
 
-/* Return 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
- * is temporary and will be released before the library is relocated.
- *
- * Input:
- *   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;
+// Returns the address of the program header table as it appears in the loaded
+// segments in memory. This is in contrast with 'phdr_table_' which
+// is temporary and will be released before the library is relocated.
+bool ElfReader::FindPhdr() {
+  const Elf32_Phdr* phdr_limit = phdr_table_ + phdr_num_;
 
-    /* If there is a PT_PHDR, use it directly */
-    for (phdr = phdr_table; phdr < phdr_limit; phdr++) {
-        if (phdr->p_type == PT_PHDR) {
-            loaded = load_bias + phdr->p_vaddr;
-            goto CHECK;
-        }
+  // If there is a PT_PHDR, use it directly.
+  for (const Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
+    if (phdr->p_type == PT_PHDR) {
+      return CheckPhdr(load_bias_ + phdr->p_vaddr);
     }
+  }
 
-    /* Otherwise, check the first loadable segment. If its file offset
-     * is 0, it starts with the ELF header, and we can trivially find the
-     * loaded program header from it. */
-    for (phdr = phdr_table; phdr < phdr_limit; phdr++) {
-        if (phdr->p_type == PT_LOAD) {
-            if (phdr->p_offset == 0) {
-                Elf32_Addr  elf_addr = load_bias + phdr->p_vaddr;
-                const Elf32_Ehdr* ehdr = (const Elf32_Ehdr*)(void*)elf_addr;
-                Elf32_Addr  offset = ehdr->e_phoff;
-                loaded = (Elf32_Addr)ehdr + offset;
-                goto CHECK;
-            }
-            break;
-        }
+  // Otherwise, check the first loadable segment. If its file offset
+  // is 0, it starts with the ELF header, and we can trivially find the
+  // loaded program header from it.
+  for (const Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
+    if (phdr->p_type == PT_LOAD) {
+      if (phdr->p_offset == 0) {
+        Elf32_Addr  elf_addr = load_bias_ + phdr->p_vaddr;
+        const Elf32_Ehdr* ehdr = (const Elf32_Ehdr*)(void*)elf_addr;
+        Elf32_Addr  offset = ehdr->e_phoff;
+        return CheckPhdr((Elf32_Addr)ehdr + offset);
+      }
+      break;
     }
+  }
 
-    /* We didn't find it, let the client know. He may be able to
-     * keep a copy of the input phdr_table instead. */
-    return NULL;
+  DL_ERR("can't find loaded phdr for \"%s\"", name_);
+  return false;
+}
 
-CHECK:
-    /* Ensure that our program header is actually within a loadable
-     * segment. This should help catch badly-formed ELF files that
-     * would cause the linker to crash later when trying to access it.
-     */
-    loaded_end = loaded + phdr_count*sizeof(Elf32_Phdr);
-
-    for (phdr = phdr_table; phdr < phdr_limit; phdr++) {
-        if (phdr->p_type != PT_LOAD)
-            continue;
-        Elf32_Addr seg_start = phdr->p_vaddr + load_bias;
-        Elf32_Addr seg_end   = phdr->p_filesz + seg_start;
-
-        if (seg_start <= loaded && loaded_end <= seg_end) {
-            return (const Elf32_Phdr*)loaded;
-        }
+// Ensures that our program header is actually within a loadable
+// segment. This should help catch badly-formed ELF files that
+// 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_;
+  Elf32_Addr loaded_end = loaded + (phdr_num_ * sizeof(Elf32_Phdr));
+  for (Elf32_Phdr* phdr = phdr_table_; phdr < phdr_limit; ++phdr) {
+    if (phdr->p_type != PT_LOAD) {
+      continue;
     }
-    return NULL;
+    Elf32_Addr seg_start = phdr->p_vaddr + load_bias_;
+    Elf32_Addr seg_end = phdr->p_filesz + seg_start;
+    if (seg_start <= loaded && loaded_end <= seg_end) {
+      loaded_phdr_ = reinterpret_cast<const Elf32_Phdr*>(loaded);
+      return true;
+    }
+  }
+  DL_ERR("\"%s\" loaded phdr %x not in loadable segment", name_, loaded);
+  return false;
 }
diff --git a/linker/linker_phdr.h b/linker/linker_phdr.h
index ec55159..a31d1d9 100644
--- a/linker/linker_phdr.h
+++ b/linker/linker_phdr.h
@@ -37,33 +37,50 @@
 
 #include "linker.h"
 
-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);
+class ElfReader {
+ public:
+  ElfReader(const char* name, int fd);
+  ~ElfReader();
 
-void
-phdr_table_unload(void* phdr_mmap, Elf32_Addr phdr_memsize);
+  bool Load();
 
-Elf32_Addr
-phdr_table_get_load_size(const Elf32_Phdr* phdr_table,
-                         size_t phdr_count);
+  size_t phdr_count() { return phdr_num_; }
+  Elf32_Addr load_start() { return reinterpret_cast<Elf32_Addr>(load_start_); }
+  Elf32_Addr load_size() { return load_size_; }
+  Elf32_Addr load_bias() { return load_bias_; }
+  const Elf32_Phdr* loaded_phdr() { return loaded_phdr_; }
 
-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);
+ private:
+  bool ReadElfHeader();
+  bool VerifyElfHeader();
+  bool ReadProgramHeader();
+  bool ReserveAddressSpace();
+  bool LoadSegments();
+  bool FindPhdr();
+  bool CheckPhdr(Elf32_Addr);
 
-int
-phdr_table_load_segments(const Elf32_Phdr* phdr_table,
-                         int               phdr_count,
-                         Elf32_Addr        load_bias,
-                         int               fd);
+  const char* name_;
+  int fd_;
+
+  Elf32_Ehdr header_;
+  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
 phdr_table_protect_segments(const Elf32_Phdr* phdr_table,
@@ -80,10 +97,6 @@
                              int               phdr_count,
                              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
 int