Merge "Use mmap to read an initial ELF header of library"

This commit is contained in:
Elliott Hughes
2012-05-25 15:11:35 -07:00
committed by Android (Google) Code Review

View File

@@ -640,10 +640,6 @@ static int open_library(const char *name)
return -1; return -1;
} }
/* temporary space for holding the first page of the shared lib
* which contains the elf header (with the pht). */
static unsigned char __header[PAGE_SIZE];
typedef struct { typedef struct {
long mmap_addr; long mmap_addr;
char tag[4]; /* 'P', 'R', 'E', ' ' */ char tag[4]; /* 'P', 'R', 'E', ' ' */
@@ -1086,29 +1082,34 @@ load_library(const char *name)
unsigned ext_sz; unsigned ext_sz;
unsigned req_base; unsigned req_base;
const char *bname; const char *bname;
struct stat sb;
soinfo *si = NULL; soinfo *si = NULL;
Elf32_Ehdr *hdr; Elf32_Ehdr *hdr = MAP_FAILED;
if(fd == -1) { if (fd == -1) {
DL_ERR("Library '%s' not found", name); DL_ERR("Library '%s' not found", name);
return NULL; return NULL;
} }
/* We have to read the ELF header to figure out what to do with this image /* We have to read the ELF header to figure out what to do with this image.
* Map entire file for this. There won't be much difference in physical
* memory usage or performance.
*/ */
if (lseek(fd, 0, SEEK_SET) < 0) { if (fstat(fd, &sb) < 0) {
DL_ERR("lseek() failed!"); DL_ERR("%5d fstat() failed! (%s)", pid, strerror(errno));
goto fail; goto fail;
} }
if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) { hdr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
DL_ERR("read() failed!"); if (hdr == MAP_FAILED) {
DL_ERR("%5d failed to mmap() header of '%s' (%s)",
pid, name, strerror(errno));
goto fail; goto fail;
} }
/* Parse the ELF header and get the size of the memory footprint for /* Parse the ELF header and get the size of the memory footprint for
* the library */ * the library */
req_base = get_lib_extents(fd, name, &__header[0], &ext_sz); req_base = get_lib_extents(fd, name, hdr, &ext_sz);
if (req_base == (unsigned)-1) if (req_base == (unsigned)-1)
goto fail; goto fail;
TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name, TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
@@ -1138,22 +1139,20 @@ load_library(const char *name)
pid, name, (void *)si->base, (unsigned) ext_sz); pid, name, (void *)si->base, (unsigned) ext_sz);
/* Now actually load the library's segments into right places in memory */ /* Now actually load the library's segments into right places in memory */
if (load_segments(fd, &__header[0], si) < 0) { if (load_segments(fd, hdr, si) < 0) {
goto fail; goto fail;
} }
/* this might not be right. Technically, we don't even need this info
* once we go through 'load_segments'. */
hdr = (Elf32_Ehdr *)si->base;
si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff); si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
si->phnum = hdr->e_phnum; si->phnum = hdr->e_phnum;
/**/
munmap(hdr, sb.st_size);
close(fd); close(fd);
return si; return si;
fail: fail:
if (si) free_info(si); if (si) free_info(si);
if (hdr != MAP_FAILED) munmap(hdr, sb.st_size);
close(fd); close(fd);
return NULL; return NULL;
} }