* This file contains the helper routines to load an ELF shared
* library into memory and add the symbol table info to the chain.
*
- * Copyright (C) 2000-2004 by Erik Andersen <andersen@codpoet.org>
+ * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
* Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
* David Engel, Hongjiu Lu and Mitch D'Souza
*
#include "ldso.h"
-#ifdef USE_CACHE
+#ifdef __LDSO_CACHE_SUPPORT__
static caddr_t _dl_cache_addr = NULL;
static size_t _dl_cache_size = 0;
libentry_t *libent;
int i, strtabsize;
- if (_dl_cache_addr == (caddr_t) - 1)
+ if (_dl_cache_addr == MAP_FAILED)
return -1;
else if (_dl_cache_addr != NULL)
return 0;
if (_dl_stat(LDSO_CACHE, &st)
- || (fd = _dl_open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
- _dl_dprintf(2, "%s: can't open cache '%s'\n", _dl_progname, LDSO_CACHE);
- _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
+ || (fd = _dl_open(LDSO_CACHE, O_RDONLY|O_CLOEXEC, 0)) < 0) {
+ _dl_cache_addr = MAP_FAILED; /* so we won't try again */
return -1;
}
_dl_cache_size = st.st_size;
- _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
+ _dl_cache_addr = _dl_mmap(0, _dl_cache_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0);
_dl_close(fd);
if (_dl_mmap_check_error(_dl_cache_addr)) {
- _dl_dprintf(2, "%s: can't map cache '%s'\n",
- _dl_progname, LDSO_CACHE);
+ _dl_dprintf(2, "%s:%i: can't map '%s'\n",
+ _dl_progname, __LINE__, LDSO_CACHE);
return -1;
}
fail:
_dl_munmap(_dl_cache_addr, _dl_cache_size);
- _dl_cache_addr = (caddr_t) - 1;
+ _dl_cache_addr = MAP_FAILED;
return -1;
}
int _dl_unmap_cache(void)
{
- if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
+ if (_dl_cache_addr == NULL || _dl_cache_addr == MAP_FAILED)
return -1;
#if 1
}
#endif
+
+void
+_dl_protect_relro (struct elf_resolve *l)
+{
+ ElfW(Addr) base = (ElfW(Addr)) DL_RELOC_ADDR(l->loadaddr, l->relro_addr);
+ ElfW(Addr) start = (base & PAGE_ALIGN);
+ ElfW(Addr) end = ((base + l->relro_size) & PAGE_ALIGN);
+ _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
+ if (start != end &&
+ _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
+ _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
+ _dl_exit(0);
+ }
+}
+
/* This function's behavior must exactly match that
* in uClibc/ldso/util/ldd.c */
static struct elf_resolve *
search_for_named_library(const char *name, int secure, const char *path_list,
struct dyn_elf **rpnt)
{
- int i, count = 1;
- char *path, *path_n;
- char mylibname[2050];
- struct elf_resolve *tpnt1;
+ char *path, *path_n, *mylibname;
+ struct elf_resolve *tpnt;
+ int done;
if (path_list==NULL)
return NULL;
- /* We need a writable copy of this string */
- path = _dl_strdup(path_list);
- if (!path) {
- _dl_dprintf(2, "Out of memory!\n");
- _dl_exit(0);
- }
-
+ /* We need a writable copy of this string, but we don't
+ * need this allocated permanently since we don't want
+ * to leak memory, so use alloca to put path on the stack */
+ done = _dl_strlen(path_list);
+ path = alloca(done + 1);
- /* Unlike ldd.c, don't bother to eliminate double //s */
+ /* another bit of local storage */
+ mylibname = alloca(2050);
+ _dl_memcpy(path, path_list, done+1);
- /* Replace colons with zeros in path_list and count them */
- for(i=_dl_strlen(path); i > 0; i--) {
- if (path[i]==':') {
- path[i]=0;
- count++;
- }
- }
+ /* Unlike ldd.c, don't bother to eliminate double //s */
+ /* Replace colons with zeros in path_list */
+ /* : at the beginning or end of path maps to CWD */
+ /* :: anywhere maps CWD */
+ /* "" maps to CWD */
+ done = 0;
path_n = path;
- for (i = 0; i < count; i++) {
- _dl_strcpy(mylibname, path_n);
- _dl_strcat(mylibname, "/");
- _dl_strcat(mylibname, name);
- if ((tpnt1 = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
- {
- return tpnt1;
- }
- path_n += (_dl_strlen(path_n) + 1);
- }
- return NULL;
-}
-
-/* Check if the named library is already loaded... */
-struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
- int trace_loaded_objects)
-{
- const char *pnt, *pnt1;
- struct elf_resolve *tpnt1;
- const char *libname, *libname2;
- static const char libc[] = "libc.so.";
- static const char aborted_wrong_lib[] = "%s: aborted attempt to load %s!\n";
-
- pnt = libname = full_libname;
-
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug)
- _dl_dprintf(_dl_debug_file, "Checking if '%s' is already loaded\n", full_libname);
-#endif
- /* quick hack to ensure mylibname buffer doesn't overflow. don't
- allow full_libname or any directory to be longer than 1024. */
- if (_dl_strlen(full_libname) > 1024)
- return NULL;
-
- /* Skip over any initial initial './' and '/' stuff to
- * get the short form libname with no path garbage */
- pnt1 = _dl_strrchr(pnt, '/');
- if (pnt1) {
- libname = pnt1 + 1;
- }
-
- /* Make sure they are not trying to load the wrong C library!
- * This sometimes happens esp with shared libraries when the
- * library path is somehow wrong! */
-#define isdigit(c) (c >= '0' && c <= '9')
- if ((_dl_strncmp(libname, libc, 8) == 0) && _dl_strlen(libname) >=8 &&
- isdigit(libname[8]))
- {
- /* Abort attempts to load glibc, libc5, etc */
- if ( libname[8]!='0') {
- if (!trace_loaded_objects) {
- _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
- _dl_exit(1);
- }
- return NULL;
+ do {
+ if (*path == 0) {
+ *path = ':';
+ done = 1;
}
- }
-
- /* Critical step! Weed out duplicates early to avoid
- * function aliasing, which wastes memory, and causes
- * really bad things to happen with weaks and globals. */
- for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
-
- /* Skip over any initial initial './' and '/' stuff to
- * get the short form libname with no path garbage */
- libname2 = tpnt1->libname;
- pnt1 = _dl_strrchr(libname2, '/');
- if (pnt1) {
- libname2 = pnt1 + 1;
+ if (*path == ':') {
+ *path = 0;
+ if (*path_n)
+ _dl_strcpy(mylibname, path_n);
+ else
+ _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
+ _dl_strcat(mylibname, "/");
+ _dl_strcat(mylibname, name);
+ if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
+ return tpnt;
+ path_n = path+1;
}
-
- if (_dl_strcmp(libname2, libname) == 0) {
- /* Well, that was certainly easy */
- return tpnt1;
- }
- }
-
+ path++;
+ } while (!done);
return NULL;
}
-
/* Used to return error codes back to dlopen et. al. */
unsigned long _dl_error_number;
unsigned long _dl_internal_error_number;
struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
- struct elf_resolve *tpnt, char *full_libname, int trace_loaded_objects)
+ struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
{
- char *pnt, *pnt1;
+ char *pnt;
struct elf_resolve *tpnt1;
char *libname;
/* Skip over any initial initial './' and '/' stuff to
* get the short form libname with no path garbage */
- pnt1 = _dl_strrchr(libname, '/');
- if (pnt1) {
- libname = pnt1 + 1;
+ pnt = _dl_strrchr(libname, '/');
+ if (pnt) {
+ libname = pnt + 1;
}
-#if 0
- /* Critical step! Weed out duplicates early to avoid
- * function aliasing, which wastes memory, and causes
- * really bad things to happen with weaks and globals. */
- if ((tpnt1=_dl_check_if_named_library_is_loaded(libname, trace_loaded_objects))!=NULL)
- return tpnt1;
-#endif
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tfind library='%s'; searching\n", libname);
-#endif
+ _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
/* If the filename has any '/', try it straight and leave it at that.
For IBCS2 compatibility under linux, we substitute the string
/usr/i486-sysv4/lib for /usr/lib in library names. */
if (libname != full_libname) {
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\ttrying file='%s'\n", full_libname);
-#endif
+ _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
if (tpnt1) {
return tpnt1;
}
- //goto goof;
}
/*
- * The ABI specifies that RPATH is searched before LD_*_PATH or
+ * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
* the default path of /usr/lib. Check in rpath directories.
*/
- for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
- if (tpnt->libtype == elf_executable) {
- pnt = (char *) tpnt->dynamic_info[DT_RPATH];
- if (pnt) {
- pnt += (unsigned long) tpnt->loadaddr + tpnt->dynamic_info[DT_STRTAB];
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching RPATH='%s'\n", pnt);
-#endif
- if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
- {
- return tpnt1;
- }
- }
- }
+#ifdef __LDSO_RUNPATH__
+ pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
+ if (pnt) {
+ pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
+ _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
+ if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
+ return tpnt1;
}
+#endif
/* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
if (_dl_library_path) {
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
-#endif
+ _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
{
return tpnt1;
}
/*
+ * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
+ */
+#ifdef __LDSO_RUNPATH__
+ pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
+ if (pnt) {
+ pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
+ _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
+ if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
+ return tpnt1;
+ }
+#endif
+
+ /*
* Where should the cache be searched? There is no such concept in the
* ABI, so we have some flexibility here. For now, search it before
* the hard coded paths that follow (i.e before /lib and /usr/lib).
*/
-#ifdef USE_CACHE
- if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
+#ifdef __LDSO_CACHE_SUPPORT__
+ if (_dl_cache_addr != NULL && _dl_cache_addr != MAP_FAILED) {
int i;
header_t *header = (header_t *) _dl_cache_addr;
libentry_t *libent = (libentry_t *) & header[1];
char *strs = (char *) &libent[header->nlibs];
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching cache='%s'\n", LDSO_CACHE);
-#endif
+ _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
for (i = 0; i < header->nlibs; i++) {
- if ((libent[i].flags == LIB_ELF ||
- libent[i].flags == LIB_ELF_LIBC0 ||
- libent[i].flags == LIB_ELF_LIBC5) &&
- _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
- (tpnt1 = _dl_load_elf_shared_library(secure,
- rpnt, strs + libent[i].liboffset)))
+ if ((libent[i].flags == LIB_ELF
+ || libent[i].flags == LIB_ELF_LIBC0
+ || libent[i].flags == LIB_ELF_LIBC5)
+ && _dl_strcmp(libname, strs + libent[i].sooffset) == 0
+ && (tpnt1 = _dl_load_elf_shared_library(secure, rpnt, strs + libent[i].liboffset))
+ ) {
return tpnt1;
+ }
}
}
#endif
/* Look for libraries wherever the shared library loader
* was installed */
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching ldso dir='%s'\n", _dl_ldsopath);
-#endif
- if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
- {
+ _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
+ tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt);
+ if (tpnt1 != NULL)
return tpnt1;
- }
-
/* Lastly, search the standard list of paths for the library.
This list must exactly match the list in uClibc/ldso/util/ldd.c */
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching full lib path list\n");
-#endif
- if ((tpnt1 = search_for_named_library(libname, secure,
- UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:"
- UCLIBC_RUNTIME_PREFIX "usr/lib:"
+ _dl_if_debug_dprint("\tsearching full lib path list\n");
+ tpnt1 = search_for_named_library(libname, secure,
UCLIBC_RUNTIME_PREFIX "lib:"
- "/usr/X11R6/lib:"
- "/usr/lib:"
- "/lib", rpnt)
- ) != NULL)
- {
+ UCLIBC_RUNTIME_PREFIX "usr/lib"
+#ifndef __LDSO_CACHE_SUPPORT__
+ ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
+#endif
+ , rpnt);
+ if (tpnt1 != NULL)
return tpnt1;
- }
goof:
/* Well, we shot our wad on that one. All we can do now is punt */
_dl_error_number = _dl_internal_error_number;
else
_dl_error_number = LD_ERROR_NOFILE;
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
-#endif
+ _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
return NULL;
}
{
ElfW(Ehdr) *epnt;
unsigned long dynamic_addr = 0;
- unsigned long dynamic_size = 0;
- Elf32_Dyn *dpnt;
+ ElfW(Dyn) *dpnt;
struct elf_resolve *tpnt;
ElfW(Phdr) *ppnt;
+#if defined(USE_TLS) && USE_TLS
+ ElfW(Phdr) *tlsppnt = NULL;
+#endif
char *status, *header;
- unsigned long dynamic_info[24];
+ unsigned long dynamic_info[DYNAMIC_SIZE];
unsigned long *lpnt;
unsigned long libaddr;
unsigned long minvma = 0xffffffff, maxvma = 0;
+ unsigned int rtld_flags;
int i, flags, piclib, infile;
+ ElfW(Addr) relro_addr = 0;
+ size_t relro_size = 0;
+ struct stat st;
+ uint32_t *p32;
+ DL_LOADADDR_TYPE lib_loadaddr;
+ DL_INIT_LOADADDR_EXTRA_DECLS
- /* If this file is already loaded, skip this step */
- tpnt = _dl_check_hashed_files(libname);
- if (tpnt) {
- if (*rpnt) {
- (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
- _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
- (*rpnt)->next->prev = (*rpnt);
- *rpnt = (*rpnt)->next;
- (*rpnt)->dyn = tpnt;
- tpnt->symbol_scope = _dl_symbol_tables;
- }
- tpnt->usage_count++;
- tpnt->libtype = elf_lib;
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
-#endif
- return tpnt;
+ libaddr = 0;
+ infile = _dl_open(libname, O_RDONLY, 0);
+ if (infile < 0) {
+ _dl_internal_error_number = LD_ERROR_NOFILE;
+ return NULL;
}
+ if (_dl_fstat(infile, &st) < 0) {
+ _dl_internal_error_number = LD_ERROR_NOFILE;
+ _dl_close(infile);
+ return NULL;
+ }
/* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
we don't load the library if it isn't setuid. */
-
if (secure) {
- struct stat st;
-
- if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
+ if (!(st.st_mode & S_ISUID)) {
+ _dl_close(infile);
return NULL;
+ }
}
- libaddr = 0;
- infile = _dl_open(libname, O_RDONLY, 0);
- if (infile < 0) {
-#if 0
- /*
- * NO! When we open shared libraries we may search several paths.
- * it is inappropriate to generate an error here.
- */
- _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
-#endif
- _dl_internal_error_number = LD_ERROR_NOFILE;
- return NULL;
+ /* Check if file is already loaded */
+ for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
+ if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
+ /* Already loaded */
+ tpnt->usage_count++;
+ _dl_close(infile);
+ return tpnt;
+ }
}
-
header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
if (_dl_mmap_check_error(header)) {
- _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
+ _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
_dl_internal_error_number = LD_ERROR_MMAP_FAILED;
_dl_close(infile);
return NULL;
- };
+ }
_dl_read(infile, header, _dl_pagesize);
epnt = (ElfW(Ehdr) *) (intptr_t) header;
- if (epnt->e_ident[0] != 0x7f ||
- epnt->e_ident[1] != 'E' ||
- epnt->e_ident[2] != 'L' ||
- epnt->e_ident[3] != 'F')
- {
+ p32 = (uint32_t*)&epnt->e_ident;
+ if (*p32 != ELFMAG_U32) {
_dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
libname);
_dl_internal_error_number = LD_ERROR_NOTELF;
_dl_close(infile);
_dl_munmap(header, _dl_pagesize);
return NULL;
- };
+ }
if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
#ifdef MAGIC2
_dl_close(infile);
_dl_munmap(header, _dl_pagesize);
return NULL;
- };
+ }
ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
_dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
_dl_progname, libname);
dynamic_addr = ppnt->p_vaddr;
- dynamic_size = ppnt->p_filesz;
- };
+ }
if (ppnt->p_type == PT_LOAD) {
/* See if this is a PIC library. */
maxvma = ppnt->p_vaddr + ppnt->p_memsz;
}
}
+ if (ppnt->p_type == PT_TLS) {
+#if defined(USE_TLS) && USE_TLS
+ if (ppnt->p_memsz == 0)
+ /* Nothing to do for an empty segment. */
+ continue;
+ else
+ /* Save for after 'tpnt' is actually allocated. */
+ tlsppnt = ppnt;
+#else
+ /*
+ * Yup, the user was an idiot and tried to sneak in a library with
+ * TLS in it and we don't support it. Let's fall on our own sword
+ * and scream at the luser while we die.
+ */
+ _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
+ _dl_progname, libname);
+ _dl_internal_error_number = LD_ERROR_TLS_FAILED;
+ _dl_close(infile);
+ _dl_munmap(header, _dl_pagesize);
+ return NULL;
+#endif
+ }
ppnt++;
- };
+ }
- maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
+ DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
+
+ maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
minvma = minvma & ~0xffffU;
flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
if (!piclib)
flags |= MAP_FIXED;
- status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
- maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
- if (_dl_mmap_check_error(status)) {
- _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
- _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
- _dl_close(infile);
- _dl_munmap(header, _dl_pagesize);
- return NULL;
- };
- libaddr = (unsigned long) status;
- flags |= MAP_FIXED;
+ if (piclib == 0 || piclib == 1) {
+ status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
+ maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
+ if (_dl_mmap_check_error(status)) {
+ _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
+ _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
+ _dl_close(infile);
+ _dl_munmap(header, _dl_pagesize);
+ return NULL;
+ }
+ libaddr = (unsigned long) status;
+ flags |= MAP_FIXED;
+ }
/* Get the memory to store the library */
ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
+ DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
+
for (i = 0; i < epnt->e_phnum; i++) {
+ if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
+ char *addr;
+
+ addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
+ if (addr == NULL)
+ goto cant_map;
+
+ DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
+ ppnt++;
+ continue;
+ }
+ if (ppnt->p_type == PT_GNU_RELRO) {
+ relro_addr = ppnt->p_vaddr;
+ relro_size = ppnt->p_memsz;
+ }
if (ppnt->p_type == PT_LOAD) {
+ char *tryaddr;
+ ssize_t size;
/* See if this is a PIC library. */
if (i == 0 && ppnt->p_vaddr > 0x1000000) {
/* flags |= MAP_FIXED; */
}
-
-
if (ppnt->p_flags & PF_W) {
unsigned long map_size;
char *cpnt;
-
- status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
- (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
- + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
- ppnt->p_offset & OFFS_ALIGN);
-
- if (_dl_mmap_check_error(status)) {
- _dl_dprintf(2, "%s: can't map '%s'\n",
- _dl_progname, libname);
+ char *piclib2map = 0;
+
+ if (piclib == 2 &&
+ /* We might be able to avoid this
+ call if memsz doesn't require
+ an additional page, but this
+ would require mmap to always
+ return page-aligned addresses
+ and a whole number of pages
+ allocated. Unfortunately on
+ uClinux may return misaligned
+ addresses and may allocate
+ partial pages, so we may end up
+ doing unnecessary mmap calls.
+
+ This is what we could do if we
+ knew mmap would always return
+ aligned pages:
+
+ ((ppnt->p_vaddr + ppnt->p_filesz
+ + ADDR_ALIGN)
+ & PAGE_ALIGN)
+ < ppnt->p_vaddr + ppnt->p_memsz)
+
+ Instead, we have to do this: */
+ ppnt->p_filesz < ppnt->p_memsz)
+ {
+ piclib2map = (char *)
+ _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
+ + ppnt->p_memsz,
+ LXFLAGS(ppnt->p_flags),
+ flags | MAP_ANONYMOUS, -1, 0);
+ if (_dl_mmap_check_error(piclib2map))
+ goto cant_map;
+ DL_INIT_LOADADDR_HDR
+ (lib_loadaddr, piclib2map
+ + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
+ }
+
+ tryaddr = piclib == 2 ? piclib2map
+ : ((char*) (piclib ? libaddr : 0) +
+ (ppnt->p_vaddr & PAGE_ALIGN));
+
+ size = (ppnt->p_vaddr & ADDR_ALIGN)
+ + ppnt->p_filesz;
+
+ /* For !MMU, mmap to fixed address will fail.
+ So instead of desperately call mmap and fail,
+ we set status to MAP_FAILED to save a call
+ to mmap (). */
+#ifndef __ARCH_USE_MMU__
+ if (piclib2map == 0)
+#endif
+ status = (char *) _dl_mmap
+ (tryaddr, size, LXFLAGS(ppnt->p_flags),
+ flags | (piclib2map ? MAP_FIXED : 0),
+ infile, ppnt->p_offset & OFFS_ALIGN);
+#ifndef __ARCH_USE_MMU__
+ else
+ status = MAP_FAILED;
+#endif
+#ifdef _DL_PREAD
+ if (_dl_mmap_check_error(status) && piclib2map
+ && (_DL_PREAD (infile, tryaddr, size,
+ ppnt->p_offset & OFFS_ALIGN)
+ == size))
+ status = tryaddr;
+#endif
+ if (_dl_mmap_check_error(status)
+ || (tryaddr && tryaddr != status)) {
+ cant_map:
+ _dl_dprintf(2, "%s:%i: can't map '%s'\n",
+ _dl_progname, __LINE__, libname);
_dl_internal_error_number = LD_ERROR_MMAP_FAILED;
- _dl_munmap((char *) libaddr, maxvma - minvma);
+ DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
_dl_close(infile);
_dl_munmap(header, _dl_pagesize);
return NULL;
- };
-
- /* Pad the last page with zeroes. */
- cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
- ppnt->p_filesz);
- while (((unsigned long) cpnt) & ADDR_ALIGN)
- *cpnt++ = 0;
-
- /* I am not quite sure if this is completely
- * correct to do or not, but the basic way that
- * we handle bss segments is that we mmap
- * /dev/zero if there are any pages left over
- * that are not mapped as part of the file */
-
- map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
-
- if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
- status = (char *) _dl_mmap((char *) map_size +
- (piclib ? libaddr : 0),
- ppnt->p_vaddr + ppnt->p_memsz - map_size,
- LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
- } else
- status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
- + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
- ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
- infile, ppnt->p_offset & OFFS_ALIGN);
- if (_dl_mmap_check_error(status)) {
- _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
- _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
- _dl_munmap((char *) libaddr, maxvma - minvma);
- _dl_close(infile);
- _dl_munmap(header, _dl_pagesize);
- return NULL;
- };
-
- /* if(libaddr == 0 && piclib) {
+ }
+
+ if (! piclib2map) {
+ DL_INIT_LOADADDR_HDR
+ (lib_loadaddr, status
+ + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
+ }
+ /* Now we want to allocate and
+ zero-out any data from the end of
+ the region we mapped in from the
+ file (filesz) to the end of the
+ loadable segment (memsz). We may
+ need additional pages for memsz,
+ that we map in below, and we can
+ count on the kernel to zero them
+ out, but we have to zero out stuff
+ in the last page that we mapped in
+ from the file. However, we can't
+ assume to have actually obtained
+ full pages from the kernel, since
+ we didn't ask for them, and uClibc
+ may not give us full pages for
+ small allocations. So only zero
+ out up to memsz or the end of the
+ page, whichever comes first. */
+
+ /* CPNT is the beginning of the memsz
+ portion not backed by filesz. */
+ cpnt = (char *) (status + size);
+
+ /* MAP_SIZE is the address of the
+ beginning of the next page. */
+ map_size = (ppnt->p_vaddr + ppnt->p_filesz
+ + ADDR_ALIGN) & PAGE_ALIGN;
+
+#ifndef MIN
+# define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+ _dl_memset (cpnt, 0,
+ MIN (map_size
+ - (ppnt->p_vaddr
+ + ppnt->p_filesz),
+ ppnt->p_memsz
+ - ppnt->p_filesz));
+
+ if (map_size < ppnt->p_vaddr + ppnt->p_memsz
+ && !piclib2map) {
+ tryaddr = map_size + (char*)(piclib ? libaddr : 0);
+ status = (char *) _dl_mmap(tryaddr,
+ ppnt->p_vaddr + ppnt->p_memsz - map_size,
+ LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+ if (_dl_mmap_check_error(status)
+ || tryaddr != status)
+ goto cant_map;
+ }
+ } else {
+ tryaddr = (piclib == 2 ? 0
+ : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
+ + (piclib ? libaddr : 0));
+ size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
+ status = (char *) _dl_mmap
+ (tryaddr, size, LXFLAGS(ppnt->p_flags),
+ flags | (piclib == 2 ? MAP_EXECUTABLE
+ | MAP_DENYWRITE : 0),
+ infile, ppnt->p_offset & OFFS_ALIGN);
+ if (_dl_mmap_check_error(status)
+ || (tryaddr && tryaddr != status))
+ goto cant_map;
+ DL_INIT_LOADADDR_HDR
+ (lib_loadaddr, status
+ + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
+ }
+
+ /* if (libaddr == 0 && piclib) {
libaddr = (unsigned long) status;
flags |= MAP_FIXED;
- }; */
- };
+ } */
+ }
ppnt++;
- };
+ }
_dl_close(infile);
/* For a non-PIC library, the addresses are all absolute */
if (piclib) {
- dynamic_addr += (unsigned long) libaddr;
+ dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
}
/*
return NULL;
}
- dpnt = (Elf32_Dyn *) dynamic_addr;
-
- dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
+ dpnt = (ElfW(Dyn) *) dynamic_addr;
_dl_memset(dynamic_info, 0, sizeof(dynamic_info));
-
-#if defined(__mips__)
- {
-
- int indx = 1;
- Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
-
- while(dpnt->d_tag) {
- dpnt++;
- indx++;
- }
- dynamic_size = indx;
- }
-#endif
-
- {
- unsigned long indx;
-
- for (indx = 0; indx < dynamic_size; indx++)
- {
- if (dpnt->d_tag > DT_JMPREL) {
- dpnt++;
- continue;
- }
- dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
- if (dpnt->d_tag == DT_TEXTREL)
- dynamic_info[DT_TEXTREL] = 1;
- dpnt++;
- };
- }
-
+ rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
/* If the TEXTREL is set, this means that we need to make the pages
writable before we perform relocations. Do this now. They get set
back again later. */
if (dynamic_info[DT_TEXTREL]) {
-#ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
+#ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
- if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
+ if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
_dl_mprotect((void *) ((piclib ? libaddr : 0) +
(ppnt->p_vaddr & PAGE_ALIGN)),
(ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
PROT_READ | PROT_WRITE | PROT_EXEC);
+ }
}
#else
- _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
+ _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
+ " Use GCC option -fPIC for shared objects, please.\n",
+ libname);
_dl_exit(1);
#endif
}
- tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
- dynamic_addr, dynamic_size);
-
- tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
+ tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
+ dynamic_addr, 0);
+ tpnt->relro_addr = relro_addr;
+ tpnt->relro_size = relro_size;
+ tpnt->st_dev = st.st_dev;
+ tpnt->st_ino = st.st_ino;
+ tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
tpnt->n_phent = epnt->e_phnum;
+ tpnt->rtld_flags |= rtld_flags;
+
+#if defined(USE_TLS) && USE_TLS
+ if (tlsppnt) {
+ _dl_debug_early("Found TLS header for %s\n", libname);
+# if NO_TLS_OFFSET != 0
+ tpnt->l_tls_offset = NO_TLS_OFFSET;
+# endif
+ tpnt->l_tls_blocksize = tlsppnt->p_memsz;
+ tpnt->l_tls_align = tlsppnt->p_align;
+ if (tlsppnt->p_align == 0)
+ tpnt->l_tls_firstbyte_offset = 0;
+ else
+ tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
+ (tlsppnt->p_align - 1);
+ tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
+ tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
+
+ /* Assign the next available module ID. */
+ tpnt->l_tls_modid = _dl_next_tls_modid ();
+
+ /* We know the load address, so add it to the offset. */
+ if (tpnt->l_tls_initimage != NULL)
+ {
+# ifdef __SUPPORT_LD_DEBUG_EARLY__
+ unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
+ tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
+ _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
+ tmp = 0;
+# else
+ tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
+# endif
+ }
+ }
+#endif
/*
* Add this object into the symbol chain
*/
if (*rpnt) {
- (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
+ (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
_dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
(*rpnt)->next->prev = (*rpnt);
*rpnt = (*rpnt)->next;
- (*rpnt)->dyn = tpnt;
- tpnt->symbol_scope = _dl_symbol_tables;
}
+#ifndef SHARED
+ /* When statically linked, the first time we dlopen a DSO
+ * the *rpnt is NULL, so we need to allocate memory for it,
+ * and initialize the _dl_symbol_table.
+ */
+ else {
+ *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
+ _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
+ }
+#endif
+ (*rpnt)->dyn = tpnt;
+ tpnt->symbol_scope = _dl_symbol_tables;
tpnt->usage_count++;
tpnt->libtype = elf_lib;
lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
if (lpnt) {
- lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
- ((int) libaddr));
+ lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
INIT_GOT(lpnt, tpnt);
- };
+ }
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) {
- _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
- _dl_dprintf(2, "\t\tdynamic: %x base: %x size: %x\n",
- dynamic_addr, libaddr, dynamic_size);
- _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %x\n\n",
- epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
+ _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
+ _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
+ _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
+ DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
- }
-#endif
_dl_munmap(header, _dl_pagesize);
return tpnt;
}
+
/* now_flag must be RTLD_NOW or zero */
int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
{
int goof = 0;
struct elf_resolve *tpnt;
- unsigned long reloc_size;
+ ElfW(Word) reloc_size, relative_count;
+ ElfW(Addr) reloc_addr;
if (rpnt->next)
- goof += _dl_fixup(rpnt->next, now_flag);
+ goof = _dl_fixup(rpnt->next, now_flag);
+ if (goof)
+ return goof;
tpnt = rpnt->dyn;
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
-#endif
+ if (!(tpnt->init_flag & RELOCS_DONE))
+ _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) {
- _dl_dprintf(2, "%s: can't handle %s relocation records\n",
- _dl_progname, UNSUPPORTED_RELOC_STR);
- }
-#endif
+ _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
+ _dl_progname, UNSUPPORTED_RELOC_STR);
goof++;
return goof;
}
#endif
if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
!(tpnt->init_flag & RELOCS_DONE)) {
- tpnt->init_flag |= RELOCS_DONE;
+ reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
+ relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
+ if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
+ reloc_size -= relative_count * sizeof(ELF_RELOC);
+ elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
+ reloc_addr += relative_count * sizeof(ELF_RELOC);
+ }
goof += _dl_parse_relocation_information(rpnt,
- tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
- reloc_size, 0);
+ reloc_addr,
+ reloc_size);
+ tpnt->init_flag |= RELOCS_DONE;
}
+ if (tpnt->dynamic_info[DT_BIND_NOW])
+ now_flag = RTLD_NOW;
if (tpnt->dynamic_info[DT_JMPREL] &&
(!(tpnt->init_flag & JMP_RELOCS_DONE) ||
(now_flag && !(tpnt->rtld_flags & now_flag)))) {
- tpnt->rtld_flags |= now_flag;
- tpnt->init_flag |= JMP_RELOCS_DONE;
+ tpnt->rtld_flags |= now_flag;
if (!(tpnt->rtld_flags & RTLD_NOW)) {
_dl_parse_lazy_relocation_information(rpnt,
tpnt->dynamic_info[DT_JMPREL],
- tpnt->dynamic_info [DT_PLTRELSZ], 0);
+ tpnt->dynamic_info [DT_PLTRELSZ]);
} else {
goof += _dl_parse_relocation_information(rpnt,
tpnt->dynamic_info[DT_JMPREL],
- tpnt->dynamic_info[DT_PLTRELSZ], 0);
+ tpnt->dynamic_info[DT_PLTRELSZ]);
}
+ tpnt->init_flag |= JMP_RELOCS_DONE;
}
- if (tpnt->init_flag & COPY_RELOCS_DONE)
- return goof;
- tpnt->init_flag |= COPY_RELOCS_DONE;
- goof += _dl_parse_copy_information(rpnt,
- tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
- tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
-
-#if defined (__SUPPORT_LD_DEBUG__)
- if(_dl_debug) {
- _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
- _dl_dprintf(_dl_debug_file,"; finished\n\n");
- }
+#if 0
+/* _dl_add_to_slotinfo is called by init_tls() for initial DSO
+ or by dlopen() for dynamically loaded DSO. */
+#if defined(USE_TLS) && USE_TLS
+ /* Add object to slot information data if necessasy. */
+ if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
+ _dl_add_to_slotinfo ((struct link_map *) tpnt);
+#endif
#endif
-
return goof;
}
/* Minimal printf which handles only %s, %d, and %x */
void _dl_dprintf(int fd, const char *fmt, ...)
{
+#if __WORDSIZE > 32
+ long int num;
+#else
int num;
+#endif
va_list args;
char *start, *ptr, *string;
- static char *buf;
+ char *buf;
+
+ if (!fmt)
+ return;
buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
start = ptr = buf;
- if (!fmt)
- return;
-
if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
_dl_write(fd, "overflow\n", 11);
_dl_exit(20);
case 'd':
{
char tmp[22];
+#if __WORDSIZE > 32
+ num = va_arg(args, long int);
+#else
num = va_arg(args, int);
-
+#endif
string = _dl_simple_ltoa(tmp, num);
_dl_write(fd, string, _dl_strlen(string));
break;
case 'X':
{
char tmp[22];
+#if __WORDSIZE > 32
+ num = va_arg(args, long int);
+#else
num = va_arg(args, int);
-
+#endif
string = _dl_simple_ltoahex(tmp, num);
_dl_write(fd, string, _dl_strlen(string));
break;
return retval;
}
+unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
+ void *debug_addr, DL_LOADADDR_TYPE load_off)
+{
+ return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
+}