Untitled diff

Created Diff never expires
/*
/*
* Copyright (C) 2008, 2009 The Android Open Source Project
* Copyright (C) 2008, 2009 The Android Open Source Project
* All rights reserved.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* modification, are permitted provided that the following conditions
* are met:
* are met:
* * Redistributions of source code must retain the above copyright
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* the documentation and/or other materials provided with the
* distribution.
* distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* SUCH DAMAGE.
*/
*/

#include <linux/auxvec.h>
#include <linux/auxvec.h>

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <fcntl.h>
#include <fcntl.h>
#include <errno.h>
#include <errno.h>
#include <dlfcn.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <sys/stat.h>

#include <pthread.h>
#include <pthread.h>

#include <sys/mman.h>
#include <sys/mman.h>
#include <sys/atomics.h>
/* special private C library header - see Android.mk */
/* special private C library header - see Android.mk */
#include <bionic_tls.h>
//#include "bionic_tls.h"

#include "linker.h"
#include "linker.h"
#include "linker_debug.h"
#include "linker_debug.h"
#include "linker_environ.h"
#include "linker_environ.h"
#include "linker_format.h"
#include "linker_format.h"

#define ALLOW_SYMBOLS_FROM_MAIN 1
#define ALLOW_SYMBOLS_FROM_MAIN 1
#define SO_MAX 128
#define SO_MAX 128

/* Assume average path length of 64 and max 8 paths */
/* Assume average path length of 64 and max 8 paths */
#define LDPATH_BUFSIZE 512
#define LDPATH_BUFSIZE 512
#define LDPATH_MAX 8
#define LDPATH_MAX 8

#define LDPRELOAD_BUFSIZE 512
#define LDPRELOAD_BUFSIZE 512
#define LDPRELOAD_MAX 8
#define LDPRELOAD_MAX 8

/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
*
*
* Do NOT use malloc() and friends or pthread_*() code here.
* Do NOT use malloc() and friends or pthread_*() code here.
* Don't use printf() either; it's caused mysterious memory
* Don't use printf() either; it's caused mysterious memory
* corruption in the past.
* corruption in the past.
* The linker runs before we bring up libc and it's easiest
* The linker runs before we bring up libc and it's easiest
* to make sure it does not depend on any complex libc features
* to make sure it does not depend on any complex libc features
*
*
* open issues / todo:
* open issues / todo:
*
*
* - are we doing everything we should for ARM_COPY relocations?
* - are we doing everything we should for ARM_COPY relocations?
* - cleaner error reporting
* - cleaner error reporting
* - after linking, set as much stuff as possible to READONLY
* - after linking, set as much stuff as possible to READONLY
* and NOEXEC
* and NOEXEC
* - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
* - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
* headers provide versions that are negative...
* headers provide versions that are negative...
* - allocate space for soinfo structs dynamically instead of
* - allocate space for soinfo structs dynamically instead of
* having a hard limit (64)
* having a hard limit (64)
*/
*/


static int link_image(soinfo *si, unsigned wr_offset);
static int link_image(soinfo *si, unsigned wr_offset);

static int socount = 0;
static int socount = 0;
static soinfo sopool[SO_MAX];
static soinfo sopool[SO_MAX];
static soinfo *freelist = NULL;
static soinfo *freelist = NULL;
static soinfo *solist = &libdl_info;
static soinfo *solist = &libdl_info;
static soinfo *sonext = &libdl_info;
static soinfo *sonext = &libdl_info;
#if ALLOW_SYMBOLS_FROM_MAIN
#if ALLOW_SYMBOLS_FROM_MAIN
static soinfo *somain; /* main process, always the one after libdl_info */
static soinfo *somain; /* main process, always the one after libdl_info */
#endif
#endif


static inline int validate_soinfo(soinfo *si)
static inline int validate_soinfo(soinfo *si)
{
{
return (si >= sopool && si < sopool + SO_MAX) ||
return (si >= sopool && si < sopool + SO_MAX) ||
si == &libdl_info;
si == &libdl_info;
}
}

static char ldpaths_buf[LDPATH_BUFSIZE];
static char ldpaths_buf[LDPATH_BUFSIZE];
static const char *ldpaths[LDPATH_MAX + 1];
static const char *ldpaths[LDPATH_MAX + 1];

static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
static char ldpreloads_buf[LDPRELOAD_BUFSIZE];
static const char *ldpreload_names[LDPRELOAD_MAX + 1];
static const char *ldpreload_names[LDPRELOAD_MAX + 1];

static soinfo *preloads[LDPRELOAD_MAX + 1];
static soinfo *preloads[LDPRELOAD_MAX + 1];

#if LINKER_DEBUG
#if LINKER_DEBUG
int debug_verbosity;
int debug_verbosity = 0;
int debug_stdout = 0;
#endif
#endif

static int pid;
static int pid;

/* This boolean is set if the program being loaded is setuid */
/* This boolean is set if the program being loaded is setuid */
static int program_is_setuid;
static int program_is_setuid;

#if STATS
#if STATS
struct _link_stats linker_stats;
struct _link_stats linker_stats;
#endif
#endif

#if COUNT_PAGES
#if COUNT_PAGES
unsigned bitmask[4096];
unsigned bitmask[4096];
#endif
#endif

#ifndef PT_ARM_EXIDX
#ifndef PT_ARM_EXIDX
#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
#endif
#endif

#if 0
// disable abort() since this is not a linker anymore
#define HOODLUM(name, ret, ...) \
#define HOODLUM(name, ret, ...) \
ret name __VA_ARGS__ \
ret name __VA_ARGS__ \
{ \
{ \
char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
write(2, errstr, sizeof(errstr)); \
write(2, errstr, sizeof(errstr)); \
abort(); \
abort(); \
}
}
HOODLUM(malloc, void *, (size_t size));
HOODLUM(malloc, void *, (size_t size));
HOODLUM(free, void, (void *ptr));
HOODLUM(free, void, (void *ptr));
HOODLUM(realloc, void *, (void *ptr, size_t size));
HOODLUM(realloc, void *, (void *ptr, size_t size));
HOODLUM(calloc, void *, (size_t cnt, size_t size));
HOODLUM(calloc, void *, (size_t cnt, size_t size));
#endif

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...) \
#define DL_ERR(fmt, x...) \
do { \
do { \
format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
"%s[%d]: " fmt, __func__, __LINE__, ##x); \
"%s[%d]: " fmt, __func__, __LINE__, ##x); \
ERROR(fmt "\n", ##x); \
ERROR(fmt "\n", ##x); \
} while(0)
} while(0)

const char *linker_get_error(void)
const char *linker_get_error(void)
{
{
return (const char *)&__linker_dl_err_buf[0];
return (const char *)&__linker_dl_err_buf[0];
}
}

/*
/*
* 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.
*/
*/
extern void __attribute__((noinline)) rtld_db_dlactivity(void);
extern void __attribute__((noinline)) rtld_db_dlactivity(void);

static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
RT_CONSISTENT, 0};
RT_CONSISTENT, 0};
static struct link_map *r_debug_tail = 0;
static struct link_map *r_debug_tail = 0;

static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;

static void insert_soinfo_into_debug_map(soinfo * info)
static void insert_soinfo_into_debug_map(soinfo * info)
{
{
struct link_map * map;
struct link_map * map;

/* Copy the necessary fields into the debug structure.
/* Copy the necessary fields into the debug structure.
*/
*/
map = &(info->linkmap);
map = &(info->linkmap);
map->l_addr = info->base;
map->l_addr = info->base;
map->l_name = (char*) info->name;
map->l_name = (char*) info->name;
map->l_ld = (uintptr_t)info->dynamic;
map->l_ld = (uintptr_t)info->dynamic;

/* Stick the new library at the end of the list.
/* Stick the new library at the end of the list.
* gdb tends to care more about libc than it does
* gdb tends to care more about libc than it does
* about leaf libraries, and ordering it this way
* about leaf libraries, and ordering it this way
* reduces the back-and-forth over the wire.
* reduces the back-and-forth over the wire.
*/
*/
if (r_debug_tail) {
if (r_debug_tail) {
r_debug_tail->l_next = map;
r_debug_tail->l_next = map;
map->l_prev = r_debug_tail;
map->l_prev = r_debug_tail;
map->l_next = 0;
map->l_next = 0;
} else {
} else {
_r_debug.r_map = map;
_r_debug.r_map = map;
map->l_prev = 0;
map->l_prev = 0;
map->l_next = 0;
map->l_next = 0;
}
}
r_debug_tail = map;
r_debug_tail = map;
}
}

static void remove_soinfo_from_debug_map(soinfo * info)
static void remove_soinfo_from_debug_map(soinfo * info)
{
{
struct link_map * map = &(info->linkmap);
struct link_map * map = &(info->linkmap);

if (r_debug_tail == map)
if (r_debug_tail == map)
r_debug_tail = map->l_prev;
r_debug_tail = map->l_prev;

if (map->l_prev) map->l_prev->l_next = map->l_next;
if (map->l_prev) map->l_prev->l_next = map->l_next;
if (map->l_next) map->l_next->l_prev = map->l_prev;
if (map->l_next) map->l_next->l_prev = map->l_prev;
}
}

void notify_gdb_of_load(soinfo * info)
void notify_gdb_of_load(soinfo * info)
{
{
if (info->flags & FLAG_EXE) {
if (info->flags & FLAG_EXE) {
// GDB already knows about the main executable
// GDB already knows about the main executable
return;
return;
}
}

pthread_mutex_lock(&_r_debug_lock);
pthread_mutex_lock(&_r_debug_lock);

_r_debug.r_state = RT_ADD;
_r_debug.r_state = RT_ADD;
rtld_db_dlactivity();
rtld_db_dlactivity();

insert_soinfo_into_debug_map(info);
insert_soinfo_into_debug_map(info);

_r_debug.r_state = RT_CONSISTENT;
_r_debug.r_state = RT_CONSISTENT;
rtld_db_dlactivity();
rtld_db_dlactivity();

pthread_mutex_unlock(&_r_debug_lock);
pthread_mutex_unlock(&_r_debug_lock);
}
}

void notify_gdb_of_unload(soinfo * info)
void notify_gdb_of_unload(soinfo * info)
{
{
if (info->flags & FLAG_EXE) {
if (info->flags & FLAG_EXE) {
// GDB already knows about the main executable
// GDB already knows about the main executable
return;
return;
}
}

pthread_mutex_lock(&_r_debug_lock);
pthread_mutex_lock(&_r_debug_lock);

_r_debug.r_state = RT_DELETE;
_r_debug.r_state = RT_DELETE;
rtld_db_dlactivity();
rtld_db_dlactivity();

remove_soinfo_from_debug_map(info);
remove_soinfo_from_debug_map(info);

_r_debug.r_state = RT_CONSISTENT;
_r_debug.r_state = RT_CONSISTENT;
rtld_db_dlactivity();
rtld_db_dlactivity();

pthread_mutex_unlock(&_r_debug_lock);
pthread_mutex_unlock(&_r_debug_lock);
}
}

void notify_gdb_of_libraries()
void notify_gdb_of_libraries()
{
{
pthread_mutex_lock(&_r_debug_lock);
_r_debug.r_state = RT_ADD;
_r_debug.r_state = RT_ADD;
rtld_db_dlactivity();
rtld_db_dlactivity();
_r_debug.r_state = RT_CONSISTENT;
_r_debug.r_state = RT_CONSISTENT;
rtld_db_dlactivity();
rtld_db_dlactivity();
pthread_mutex_unlock(&_r_debug_lock);
}
}

static soinfo *alloc_info(const char *name)
static soinfo *alloc_info(const char *name)
{
{
soinfo *si;
soinfo *si;

if(strlen(name) >= SOINFO_NAME_LEN) {
if(strlen(name) >= SOINFO_NAME_LEN) {
DL_ERR("%5d library name %s too long", pid, name);
DL_ERR("%5d library name %s too long", pid, name);
return NULL;
return NULL;
}
}

/* The freelist is populated when we call free_info(), which in turn is
/* The freelist is populated when we call free_info(), which in turn is
done only by dlclose(), which is not likely to be used.
done only by dlclose(), which is not likely to be used.
*/
*/
if (!freelist) {
if (!freelist) {
if(socount == SO_MAX) {
if(socount == SO_MAX) {
DL_ERR("%5d too many libraries when loading %s", pid, name);
DL_ERR("%5d too many libraries when loading %s", pid, name);
return NULL;
return NULL;
}
}
freelist = sopool + socount++;
freelist = sopool + socount++;
freelist->next = NULL;
freelist->next = NULL;
}
}

si = freelist;
si = freelist;
freelist = freelist->next;
freelist = freelist->next;

/* Make sure we get a clean block of soinfo */
/* Make sure we get a clean block of soinfo */
memset(si, 0, sizeof(soinfo));
memset(si, 0, sizeof(soinfo));
strlcpy((char*) si->name, name, sizeof(si->name));
strlcpy((char*) si->name, name, sizeof(si->name));
sonext->next = si;
sonext->next = si;
si->next = NULL;
si->next = NULL;
si->refcount = 0;
si->refcount = 0;
sonext = si;
sonext = si;

TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
return si;
return si;
}
}

static void free_info(soinfo *si)
static void free_info(soinfo *si)
{
{
soinfo *prev = NULL, *trav;
soinfo *prev = NULL, *trav;

TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);

for(trav = solist; trav != NULL; trav = trav->next){
for(trav = solist; trav != NULL; trav = trav->next){
if (trav == si)
if (trav == si)
break;
break;
prev = trav;
prev = trav;
}
}
if (trav == NULL) {
if (trav == NULL) {
/* si was not ni solist */
/* si was not ni solist */
DL_ERR("%5d name %s is not in solist!", pid, si->name);
DL_ERR("%5d name %s is not in solist!", pid, si->name);
return;
return;
}
}

/* prev will never be NULL, because the first entry in solist is
/* prev will never be NULL, because the first entry in solist is
always the static libdl_info.
always the static libdl_info.
*/
*/
prev->next = si->next;
prev->next = si->next;
if (si == sonext) sonext = prev;
if (si == sonext) sonext = prev;
si->next = freelist;
si->next = freelist;
freelist = si;
freelist = si;
}
}

const char *addr_to_name(unsigned addr)
const char *addr_to_name(unsigned addr)
{
{
soinfo *si;
soinfo *si;

for(si = solist; si != 0; si = si->next){
for(si = solist; si != 0; si = si->next){
if((addr >= si->base) && (addr < (si->base + si->size))) {
if((addr >= si->base) && (addr < (si->base + si->size))) {
return si->name;
return si->name;
}
}
}
}

return "";
return "";
}
}

/* For a given PC, find the .so that it belongs to.
/* For a given PC, find the .so that it belongs to.
* Returns the base address of the .ARM.exidx section
* Returns the base address of the .ARM.exidx section
* for that .so, and the number of 8-byte entries
* for that .so, and the number of 8-byte entries
* in that section (via *pcount).
* in that section (via *pcount).
*
*
* Intended to be called by libc's __gnu_Unwind_Find_exidx().
* Intended to be called by libc's __gnu_Unwind_Find_exidx().
*
*
* This function is exposed via dlfcn.c and libdl.so.
* This function is exposed via dlfcn.c and libdl.so.
*/
*/
#ifdef ANDROID_ARM_LINKER
#ifdef ANDROID_ARM_LINKER
_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
_Unwind_Ptr android_dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
{
{
soinfo *si;
soinfo *si;
unsigned addr = (unsigned)pc;
unsigned addr = (unsigned)pc;

for (si = solist; si != 0; si = si->next){
for (si = solist; si != 0; si = si->next){
if ((addr >= si->base) && (addr < (si->base + si->size))) {
if ((addr >= si->base) && (addr < (si->base + si->size))) {
*pcount = si->ARM_exidx_count;
*pcount = si->ARM_exidx_count;
return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx);
}
}
}
}
*pcount = 0;
*pcount = 0;
return NULL;
return NULL;
}
}
#elif defined(ANDROID_X86_LINKER)
#endif
/* Here, we only have to provide a callback to iterate across all the
/* Here, we only have to provide a callback to iterate across all the
* loaded libraries. gcc_eh does the rest. */
* loaded libraries. gcc_eh does the rest. */
int
int
dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
android_dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
void *data)
void *data)
{
{
soinfo *si;
soinfo *si;
struct dl_phdr_info dl_info;
struct dl_phdr_info dl_info;
int rv = 0;
int rv = 0;

for (si = solist; si != NULL; si = si->next) {
for (si = solist; si != NULL; si = si->next) {
dl_info.dlpi_addr = si->linkmap.l_addr;
dl_info.dlpi_addr = si->linkmap.l_addr;
dl_info.dlpi_name = si->linkmap.l_name;
dl_info.dlpi_name = si->linkmap.l_name;
dl_info.dlpi_phdr = si->phdr;
dl_info.dlpi_phdr = si->phdr;
dl_info.dlpi_phnum = si->phnum;
dl_info.dlpi_phnum = si->phnum;
rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
if (rv != 0)
if (rv != 0)
break;
break;
}
}
return rv;
return rv;
}
}
#endif

static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
static Elf_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name)
{
{
Elf32_Sym *s;
Elf_Sym *s;
Elf32_Sym *symtab = si->symtab;
Elf_Sym *symtab = si->symtab;
const char *strtab = si->strtab;
const char *strtab = si->strtab;
unsigned n;
unsigned n;

TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
name, si->name, si->base, hash, hash % si->nbucket);
name, si->name, si->base, hash, hash % si->nbucket);
n = hash % si->nbucket;
n = hash % si->nbucket;

for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
s = symtab + n;
s = symtab + n;
if(strcmp(strtab + s->st_name, name)) continue;
if(strcmp(strtab + s->st_name, name)) continue;

/* only concern ourselves with global and weak symbol definitions */
/* only concern ourselves with global and weak symbol definitions */
switch(ELF32_ST_BIND(s->st_info)){
switch(ELF32_ST_BIND(s->st_info)){
case STB_GLOBAL:
case STB_GLOBAL:
case STB_WEAK:
case STB_WEAK:
/* no section == undefined */
/* no section == undefined */
if(s->st_shndx == 0) continue;
if(s->st_shndx == 0) continue;

TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
name, si->name, s->st_value, s->st_size);
name, si->name, s->st_value, s->st_size);
return s;
return s;
}
}
}
}

return NULL;
return NULL;
}
}

static unsigned elfhash(const char *_name)
static unsigned elfhash(const char *_name)
{
{
const unsigned char *name = (const unsigned char *) _name;
const unsigned char *name = (const unsigned char *) _name;
unsigned h = 0, g;
unsigned h = 0, g;

while(*name) {
while(*name) {
h = (h << 4) + *name++;
h = (h << 4) + *name++;
g = h & 0xf0000000;
g = h & 0xf0000000;
h ^= g;
h ^= g;
h ^= g >> 24;
h ^= g >> 24;
}
}
return h;
return h;
}
}
static Elf32_Sym *

static Elf_Sym *
_do_lookup(soinfo *si, const char *name, unsigned *base)
_do_lookup(soinfo *si, const char *name, unsigned *base)
{
{
unsigned elf_hash = elfhash(name);
unsigned elf_hash = elfhash(name);
Elf32_Sym *s;
Elf_Sym *s;
unsigned *d;
unsigned *d;
soinfo *lsi = si;
soinfo *lsi = si;
int i;
int i;

/* Look for symbols in the local scope (the object who is
/* Look for symbols in the local scope (the object who is
* searching). This happens with C++ templates on i386 for some
* searching). This happens with C++ templates on i386 for some
* reason.
* reason.
*
*
* Notes on weak symbols:
* Notes on weak symbols:
* The ELF specs are ambigious about treatment of weak definitions in
* The ELF specs are ambigious about treatment of weak definitions in
* dynamic linking. Some systems return the first definition found
* dynamic linking. Some systems return the first definition found
* and some the first non-weak definition. This is system dependent.
* and some the first non-weak definition. This is system dependent.
* Here we return the first definition found for simplicity. */
* Here we return the first definition found for simplicity. */

s = _elf_lookup(si, elf_hash, name);
s = _elf_lookup(si, elf_hash, name);
if(s != NULL)
if(s != NULL)
goto done;
goto done;

/* Next, look for it in the preloads list */
/* Next, look for it in the preloads list */
for(i = 0; preloads[i] != NULL; i++) {
for(i = 0; preloads[i] != NULL; i++) {
lsi = preloads[i];
lsi = preloads[i];
s = _elf_lookup(lsi, elf_hash, name);
s = _elf_lookup(lsi, elf_hash, name);
if(s != NULL)
if(s != NULL)
goto done;
goto done;
}
}

for(d = si->dynamic; *d; d += 2) {
for(d = si->dynamic; *d; d += 2) {
if(d[0] == DT_NEEDED){
if(d[0] == DT_NEEDED){
lsi = (soinfo *)d[1];
lsi = (soinfo *)d[1];
if (!validate_soinfo(lsi)) {
if (!validate_soinfo(lsi)) {
DL_ERR("%5d bad DT_NEEDED pointer in %s",
DL_ERR("%5d bad DT_NEEDED pointer in %s",
pid, si->name);
pid, si->name);
return NULL;
return NULL;
}
}

DEBUG("%5d %s: looking up %s in %s\n",
DEBUG("%5d %s: looking up %s in %s\n",
pid, si->name, name, lsi->name);
pid, si->name, name, lsi->name);
s = _elf_lookup(lsi, elf_hash, name);
s = _elf_lookup(lsi, elf_hash, name);
if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
goto done;
goto done;
}
}
}
}

#if ALLOW_SYMBOLS_FROM_MAIN
#if ALLOW_SYMBOLS_FROM_MAIN
/* If we are resolving relocations while dlopen()ing a library, it's OK for
/* If we are resolving relocations while dlopen()ing a library, it's OK for
* the library to resolve a symbol that's defined in the executable itself,
* the library to resolve a symbol that's defined in the executable itself,
* although this is rare and is generally a bad idea.
* although this is rare and is generally a bad idea.
*/
*/
if (somain) {
if (somain) {
lsi = somain;
lsi = somain;
DEBUG("%5d %s: looking up %s in executable %s\n",
DEBUG("%5d %s: looking up %s in executable %s\n",
pid, si->name, name, lsi->name);
pid, si->name, name, lsi->name);
s = _elf_lookup(lsi, elf_hash, name);
s = _elf_lookup(lsi, elf_hash, name);
}
}
#endif
#endif

done:
done:
if(s != NULL) {
if(s != NULL) {
TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
"found in %s, base = 0x%08x\n",
"found in %s, base = 0x%08x\n",
pid, si->name, name, s->st_value, lsi->name, lsi->base);
pid, si->name, name, s->st_value, lsi->name, lsi->base);
*base = lsi->base;
*base = lsi->base;
return s;
return s;
}
}

return NULL;
return NULL;
}
}

/* This is used by dl_sym(). It performs symbol lookup only within the
/* This is used by dl_sym(). It performs symbol lookup only within the
specified soinfo object and not in any of its dependencies.
specified soinfo object and not in any of its dependencies.
*/
*/
Elf32_Sym *lookup_in_library(soinfo *si, const char *name)
Elf_Sym *lookup_in_library(soinfo *si, const char *name)
{
{
return _elf_lookup(si, elfhash(name), name);
return _elf_lookup(si, elfhash(name), name);
}
}

/* This is used by dl_sym(). It performs a global symbol lookup.
/* This is used by dl_sym(). It performs a global symbol lookup.
*/
*/
Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
Elf_Sym *lookup(const char *name, soinfo **found, soinfo *start)
{
{
unsigned elf_hash = elfhash(name);
unsigned elf_hash = elfhash(name);
Elf32_Sym *s = NULL;
Elf_Sym *s = NULL;
soinfo *si;
soinfo *si;

if(start == NULL) {
if(start == NULL) {
start = solist;
start = solist;
}
}

for(si = start; (s == NULL) && (si != NULL); si = si->next)
for(si = start; (s == NULL) && (si != NULL); si = si->next)
{
{
if(si->flags & FLAG_ERROR)
if(si->flags & FLAG_ERROR)
continue;
continue;
s = _elf_lookup(si, elf_hash, name);
s = _elf_lookup(si, elf_hash, name);
if (s != NULL) {
if (s != NULL) {
*found = si;
*found = si;
break;
break;
}
}
}
}

if(s != NULL) {
if(s != NULL) {
TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
"si->base = 0x%08x\n", pid, name, s->st_value, si->base);
"si->base = 0x%08x\n", pid, name, s->st_value, si->base);
return s;
return s;
}
}

return NULL;
return NULL;
}
}

soinfo *find_containing_library(const void *addr)
soinfo *find_containing_library(const void *addr)
{
{
soinfo *si;
soinfo *si;

for(si = solist; si != NULL; si = si->next)
for(si = solist; si != NULL; si = si->next)
{
{
if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
return si;
return si;
}
}
}
}

return NULL;
return NULL;
}
}
Elf32_Sym *find_containing_symbol(const void *addr, soinfo *si)

Elf_Sym *find_containing_symbol(const void *addr, soinfo *si)
{
{
unsigned int i;
unsigned int i;
unsigned soaddr = (unsigned)addr - si->base;
unsigned soaddr = (unsigned)addr - si->base;

/* Search the library's symbol table for any defined symbol which
/* Search the library's symbol table for any defined symbol which
* contains this address */
* contains this address */
for(i=0; i<si->nchain; i++) {
for(i=0; i<si->nchain; i++) {
Elf32_Sym *sym = &si->symtab[i];
Elf_Sym *sym = &si->symtab[i];

if(sym->st_shndx != SHN_UNDEF &&
if(sym->st_shndx != SHN_UNDEF &&
soaddr >= sym->st_value &&
soaddr >= sym->st_value &&
soaddr < sym->st_value + sym->st_size) {
soaddr < sym->st_value + sym->st_size) {
return sym;
return sym;
}
}
}
}

return NULL;
return NULL;
}
}

#if 0
#if 0
static void dump(soinfo *si)
static void dump(soinfo *si)
{
{
Elf32_Sym *s = si->symtab;
Elf_Sym *s = si->symtab;
unsigned n;
unsigned n;

for(n = 0; n < si->nchain; n++) {
for(n = 0; n < si->nchain; n++) {
TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
s->st_info, s->st_shndx, s->st_value, s->st_size,
s->st_info, s->st_shndx, s->st_value, s->st_size,
si->strtab + s->st_name);
si->strtab + s->st_name);
s++;
s++;
}
}
}
}
#endif
#endif

static const char *sopaths[] = {
static const char *sopaths[] = {
"/vendor/lib",
"/vendor/lib",
"/system/lib",
"/system/lib",
0
0
};
};

static int _open_lib(const char *name)
static int _open_lib(const char *name)
{
{
int fd;
int fd;
struct stat filestat;
struct stat filestat;

if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
if ((fd = open(name, O_RDONLY)) >= 0)
if ((fd = open(name, O_RDONLY)) >= 0)
return fd;
return fd;
}
}

return -1;
return -1;
}
}

static void parse_library_path(const char *path, char *delim);

static int open_library(const char *name)
static int open_library(const char *name)
{
{
int fd;
int fd;
char buf[512];
char buf[512];
const char **path;
const char **path;
int n;
int n;

TRACE("[ %5d opening %s ]\n", pid, name);
TRACE("[ %5d opening %s ]\n", pid, name);

if(name == 0) return -1;
if(name == 0) return -1;
if(strlen(name) > 256) return -1;
if(strlen(name) > 256) return -1;

if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
return fd;
return fd;

#ifdef DEFAULT_HYBRIS_LD_LIBRARY_PATH
if (getenv("HYBRIS_LD_LIBRARY_PATH") == NULL && *ldpaths == 0)
{
parse_library_path(DEFAULT_HYBRIS_LD_LIBRARY_PATH, ":");
}
#endif
if (getenv("HYBRIS_LD_LIBRARY_PATH") != NULL && *ldpaths == 0)
{
parse_library_path(getenv("HYBRIS_LD_LIBRARY_PATH"), ":");
}

for (path = ldpaths; *path; path++) {
for (path = ldpaths; *path; path++) {
n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
if (n < 0 || n >= (int)sizeof(buf)) {
if (n < 0 || n >= (int)sizeof(buf)) {
WARN("Ignoring very long library path: %s/%s\n", *path, name);
WARN("Ignoring very long library path: %s/%s\n", *path, name);
continue;
continue;
}
}
if ((fd = _open_lib(buf)) >= 0)
if ((fd = _open_lib(buf)) >= 0)
return fd;
return fd;
}
}
for (path = sopaths; *path; path++) {
for (path = sopaths; *path; path++) {
n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
if (n < 0 || n >= (int)sizeof(buf)) {
if (n < 0 || n >= (int)sizeof(buf)) {
WARN("Ignoring very long library path: %s/%s\n", *path, name);
WARN("Ignoring very long library path: %s/%s\n", *path, name);
continue;
continue;
}
}
if ((fd = _open_lib(buf)) >= 0)
if ((fd = _open_lib(buf)) >= 0)
return fd;
return fd;
}
}

return -1;
return -1;
}
}

/* temporary space for holding the first page of the shared lib
/* temporary space for holding the first page of the shared lib
* which contains the elf header (with the pht). */
* which contains the elf header (with the pht). */
static unsigned char __header[PAGE_SIZE];
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', ' ' */
} prelink_info_t;
} prelink_info_t;

/* Returns the requested base address if the library is prelinked,
/* Returns the requested base address if the library is prelinked,
* and 0 otherwise. */
* and 0 otherwise. */
static unsigned long
static unsigned long
is_prelinked(int fd, const char *name)
is_prelinked(int fd, const char *name)
{
{
off_t sz;
off_t sz;
prelink_info_t info;
prelink_info_t info;

sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
if (sz < 0) {
if (sz < 0) {
DL_ERR("lseek() failed!");
DL_ERR("lseek() failed!");
return 0;
return 0;
}
}

if (read(fd, &info, sizeof(info)) != sizeof(info)) {
if (read(fd, &info, sizeof(info)) != sizeof(info)) {
WARN("Could not read prelink_info_t structure for `%s`\n", name);
INFO("Could not read prelink_info_t structure for `%s`\n", name);
return 0;
return 0;
}
}

if (strncmp(info.tag, "PRE ", 4)) {
if (strncmp(info.tag, "PRE ", 4)) {
WARN("`%s` is not a prelinked library\n", name);
INFO("`%s` is not a prelinked library\n", name);
return 0;
return 0;
}
}

return (unsigned long)info.mmap_addr;
return (unsigned long)info.mmap_addr;
}
}

/* verify_elf_object
/* verify_elf_object
* Verifies if the object @ base is a valid ELF object
* Verifies if the object @ base is a valid ELF object
*
*
* Args:
* Args:
*
*
* Returns:
* Returns:
* 0 on success
* 0 on success
* -1 if no valid ELF object is found @ base.
* -1 if no valid ELF object is found @ base.
*/
*/
static int
static int
verify_elf_object(void *base, const char *name)
verify_elf_object(void *base, const char *name)
{
{
Elf32_Ehdr *hdr = (Elf32_Ehdr *) base;
Elf_Ehdr *hdr = (Elf_Ehdr *) base;

if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
if (hdr->e_ident[EI_MAG1] != ELFMAG1) 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_MAG2] != ELFMAG2) return -1;
if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;

/* TODO: Should we verify anything else in the header? */
/* TODO: Should we verify anything else in the header? */
#ifdef ANDROID_ARM_LINKER
#ifdef ANDROID_ARM_LINKER
if (hdr->e_machine != EM_ARM) return -1;
if (hdr->e_machine != EM_ARM) return -1;
#elif defined(ANDROID_X86_LINKER)
#elif defined(ANDROID_X86_LINKER)
if (hdr->e_machine != EM_386) return -1;
if (hdr->e_machine != EM_386) return -1;
#endif
#endif
return 0;
return 0;
}
}


/* get_lib_extents
/* get_lib_extents
* Retrieves the base (*base) address where the ELF object should be
* Retrieves the base (*base) address where the ELF object should be
* mapped and its overall memory size (*total_sz).
* mapped and its overall memory size (*total_sz).
*
*
* Args:
* Args:
* fd: Opened file descriptor for the library
* fd: Opened file descriptor for the library
* name: The name of the library
* name: The name of the library
* _hdr: Pointer to the header page of the library
* _hdr: Pointer to the header page of the library
* total_sz: Total size of the memory that should be allocated for
* total_sz: Total size of the memory that should be allocated for
* this library
* this library
*
*
* Returns:
* Returns:
* -1 if there was an error while trying to get the lib extents.
* -1 if there was an error while trying to get the lib extents.
* The possible reasons are:
* The possible reasons are:
* - Could not determine if the library was prelinked.
* - Could not determine if the library was prelinked.
* - The library provided is not a valid ELF object
* - The library provided is not a valid ELF object
* 0 if the library did not request a specific base offset (normal
* 0 if the library did not request a specific base offset (normal
* for non-prelinked libs)
* for non-prelinked libs)
* > 0 if the library requests a specific address to be mapped to.
* > 0 if the library requests a specific address to be mapped to.
* This indicates a pre-linked library.
* This indicates a pre-linked library.
*/
*/
static unsigned
static unsigned
get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz)
{
{
unsigned req_base;
unsigned req_base;
unsigned min_vaddr = 0xffffffff;
unsigned min_vaddr = 0xffffffff;
unsigned max_vaddr = 0;
unsigned max_vaddr = 0;
unsigned char *_hdr = (unsigned char *)__hdr;
unsigned char *_hdr = (unsigned char *)__hdr;
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr;
Elf_Ehdr *ehdr = (Elf_Ehdr *)_hdr;
Elf32_Phdr *phdr;
Elf_Phdr *phdr;
int cnt;
int cnt;

TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
if (verify_elf_object(_hdr, name) < 0) {
if (verify_elf_object(_hdr, name) < 0) {
DL_ERR("%5d - %s is not a valid ELF object", pid, name);
DL_ERR("%5d - %s is not a valid ELF object", pid, name);
return (unsigned)-1;
return (unsigned)-1;
}
}

req_base = (unsigned) is_prelinked(fd, name);
req_base = (unsigned) is_prelinked(fd, name);
if (req_base == (unsigned)-1)
if (req_base == (unsigned)-1)
return -1;
return -1;
else if (req_base != 0) {
else if (req_base != 0) {
TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n",
pid, name, req_base);
pid, name, req_base);
} else {
} else {
TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name);
}
}
phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff);

phdr = (Elf_Phdr *)(_hdr + ehdr->e_phoff);

/* find the min/max p_vaddrs from all the PT_LOAD segments so we can
/* find the min/max p_vaddrs from all the PT_LOAD segments so we can
* get the range. */
* get the range. */
for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) {
if (phdr->p_type == PT_LOAD) {
if (phdr->p_type == PT_LOAD) {
if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr)
max_vaddr = phdr->p_vaddr + phdr->p_memsz;
max_vaddr = phdr->p_vaddr + phdr->p_memsz;
if (phdr->p_vaddr < min_vaddr)
if (phdr->p_vaddr < min_vaddr)
min_vaddr = phdr->p_vaddr;
min_vaddr = phdr->p_vaddr;
}
}
}
}

if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
DL_ERR("%5d - No loadable segments found in %s.", pid, name);
DL_ERR("%5d - No loadable segments found in %s.", pid, name);
return (unsigned)-1;
return (unsigned)-1;
}
}

/* truncate min_vaddr down to page boundary */
/* truncate min_vaddr down to page boundary */
min_vaddr &= ~PAGE_MASK;
min_vaddr &= ~PAGE_MASK;

/* round max_vaddr up to the next page */
/* round max_vaddr up to the next page */
max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;
max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK;

*total_sz = (max_vaddr - min_vaddr);
*total_sz = (max_vaddr - min_vaddr);
return (unsigned)req_base;
return (unsigned)req_base;
}
}

/* reserve_mem_region
/* reserve_mem_region
*
*
* This function reserves a chunk of memory to be used for mapping in
* This function reserves a chunk of memory to be used for mapping in
* a prelinked shared library. We reserve the entire memory region here, and
* a prelinked shared library. We reserve the entire memory region here, and
* then the rest of the linker will relocate the individual loadable
* then the rest of the linker will relocate the individual loadable
* segments into the correct locations within this memory range.
* segments into the correct locations within this memory range.
*
*
* Args:
* Args:
* si->base: The requested base of the allocation.
* si->base: The requested base of the allocation.
* si->size: The size of the allocation.
* si->size: The size of the allocation.
*
*
* Returns:
* Returns:
* -1 on failure, and 0 on success. On success, si->base will contain
* -1 on failure, and 0 on success. On success, si->base will contain
* the virtual address at which the library will be mapped.
* the virtual address at which the library will be mapped.
*/
*/

static int reserve_mem_region(soinfo *si)
static int reserve_mem_region(soinfo *si)
{
{
void *base = mmap((void *)si->base, si->size, PROT_NONE,
void *base = mmap((void *)si->base, si->size, PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (base == MAP_FAILED) {
if (base == MAP_FAILED) {
DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
"as requested, will try general pool: %d (%s)",
"as requested, will try general pool: %d (%s)",
pid, (si->base ? "" : "non-"), si->name, si->base,
pid, (si->base ? "" : "non-"), si->name, si->base,
errno, strerror(errno));
errno, strerror(errno));
return -1;
return -1;
} else if (base != (void *)si->base) {
} else if (base != (void *)si->base) {
DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
"not at 0x%08x", pid, (si->base ? "" : "non-"),
"not at 0x%08x", pid, (si->base ? "" : "non-"),
si->name, (unsigned)base, si->base);
si->name, (unsigned)base, si->base);
munmap(base, si->size);
munmap(base, si->size);
return -1;
return -1;
}
}
return 0;
return 0;
}
}

static int alloc_mem_region(soinfo *si)
static int alloc_mem_region(soinfo *si)
{
{
if (si->base) {
if (si->base) {
/* Attempt to mmap a prelinked library. */
/* Attempt to mmap a prelinked library. */
return reserve_mem_region(si);
return reserve_mem_region(si);
}
}

/* This is not a prelinked library, so we use the kernel's default
/* This is not a prelinked library, so we use the kernel's default
allocator.
allocator.
*/
*/

void *base = mmap(NULL, si->size, PROT_NONE,
void *base = mmap(NULL, si->size, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (base == MAP_FAILED) {
if (base == MAP_FAILED) {
DL_ERR("%5d mmap of library '%s' failed: %d (%s)\n",
DL_ERR("%5d mmap of library '%s' failed: %d (%s)\n",
pid, si->name,
pid, si->name,
errno, strerror(errno));
errno, strerror(errno));
goto err;
goto err;
}
}
si->base = (unsigned) base;
si->base = (unsigned) base;
PRINT("%5d mapped library '%s' to %08x via kernel allocator.\n",
INFO("%5d mapped library '%s' to %08x via kernel allocator.\n",
pid, si->name, si->base);
pid, si->name, si->base);
return 0;
return 0;

err:
err:
DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.",
pid, si->name);
pid, si->name);
return -1;
return -1;
}
}

#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
#define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0)
#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
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_segments
/* load_segments
*
*
* This function loads all the loadable (PT_LOAD) segments into memory
* This function loads all the loadable (PT_LOAD) segments into memory
* at their appropriate memory offsets off the base address.
* at their appropriate
*
* Args:
* fd: Open file descriptor to the library to load.
* header: Pointer to a header page that contains the ELF header.
* This is needed since we haven't mapped in the real file yet.
* si: ptr to soinfo struct describing the shared object.
*
* Returns:
* 0 on success, -1 on failure.
*/
static int
load_segments(int fd, void *header, soinfo *si)
{
Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff);
Elf32_Addr base = (Elf32_Addr) si->base;
int cnt;
unsigned len;