2020-03-04 04:21:17 +03:00
|
|
|
/* $NetBSD: map_object.c,v 1.61 2020/03/04 01:21:17 thorpej Exp $ */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1996 John D. Polstra.
|
|
|
|
* Copyright 1996 Matt Thomas <matt@3am-software.com>
|
2002-10-05 15:59:03 +04:00
|
|
|
* Copyright 2002 Charles M. Hannum <root@ihack.net>
|
1996-12-16 23:37:55 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by John Polstra.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-10-22 09:39:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2020-03-04 04:21:17 +03:00
|
|
|
__RCSID("$NetBSD: map_object.c,v 1.61 2020/03/04 01:21:17 thorpej Exp $");
|
2004-10-22 09:39:56 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
1999-11-07 03:21:12 +03:00
|
|
|
#include <stdlib.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1999-11-07 03:21:12 +03:00
|
|
|
#include <sys/stat.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2010-10-16 14:27:06 +04:00
|
|
|
#include "debug.h"
|
1996-12-16 23:37:55 +03:00
|
|
|
#include "rtld.h"
|
|
|
|
|
2003-07-24 14:12:25 +04:00
|
|
|
static int protflags(int); /* Elf flags -> mmap protection */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2010-10-16 14:27:06 +04:00
|
|
|
#define EA_UNDEF (~(Elf_Addr)0)
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Map a shared object into memory. The argument is a file descriptor,
|
|
|
|
* which must be open on the object and positioned at its beginning.
|
|
|
|
*
|
|
|
|
* The return value is a pointer to a newly-allocated Obj_Entry structure
|
|
|
|
* for the shared object. Returns NULL on failure.
|
|
|
|
*/
|
|
|
|
Obj_Entry *
|
2006-03-21 20:48:10 +03:00
|
|
|
_rtld_map_object(const char *path, int fd, const struct stat *sb)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
2002-09-24 13:26:43 +04:00
|
|
|
Obj_Entry *obj;
|
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
Elf_Phdr *phdr;
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
Elf_Phdr *phtls;
|
|
|
|
#endif
|
2010-10-16 14:27:06 +04:00
|
|
|
size_t phsize;
|
2002-09-24 13:26:43 +04:00
|
|
|
Elf_Phdr *phlimit;
|
|
|
|
Elf_Phdr *segs[2];
|
|
|
|
int nsegs;
|
2002-10-04 22:50:43 +04:00
|
|
|
caddr_t mapbase = MAP_FAILED;
|
2005-06-01 18:14:55 +04:00
|
|
|
size_t mapsize = 0;
|
2003-03-06 10:34:56 +03:00
|
|
|
int mapflags;
|
2002-09-24 13:26:43 +04:00
|
|
|
Elf_Off base_offset;
|
2003-03-06 10:34:56 +03:00
|
|
|
Elf_Addr base_alignment;
|
2002-09-24 13:26:43 +04:00
|
|
|
Elf_Addr base_vaddr;
|
|
|
|
Elf_Addr base_vlimit;
|
|
|
|
Elf_Addr text_vlimit;
|
2002-10-04 22:50:43 +04:00
|
|
|
int text_flags;
|
2019-01-06 22:44:54 +03:00
|
|
|
void *base_addr;
|
2002-09-24 13:26:43 +04:00
|
|
|
Elf_Off data_offset;
|
|
|
|
Elf_Addr data_vaddr;
|
|
|
|
Elf_Addr data_vlimit;
|
2002-10-04 22:50:43 +04:00
|
|
|
int data_flags;
|
2002-09-24 13:26:43 +04:00
|
|
|
caddr_t data_addr;
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
Elf_Addr tls_vaddr = 0; /* Noise GCC */
|
|
|
|
#endif
|
2010-10-16 14:27:06 +04:00
|
|
|
Elf_Addr phdr_vaddr;
|
|
|
|
size_t phdr_memsz;
|
2002-09-24 13:26:43 +04:00
|
|
|
caddr_t gap_addr;
|
|
|
|
size_t gap_size;
|
2010-10-16 14:27:06 +04:00
|
|
|
int i;
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
2002-09-24 13:26:43 +04:00
|
|
|
Elf_Addr clear_vaddr;
|
|
|
|
caddr_t clear_addr;
|
|
|
|
size_t nclear;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
2016-06-14 16:06:41 +03:00
|
|
|
#ifdef GNU_RELRO
|
|
|
|
Elf_Addr relro_page;
|
|
|
|
size_t relro_size;
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2009-05-20 00:44:52 +04:00
|
|
|
if (sb != NULL && sb->st_size < (off_t)sizeof (Elf_Ehdr)) {
|
2012-10-14 01:13:07 +04:00
|
|
|
_rtld_error("%s: not ELF file (too short)", path);
|
2003-02-06 15:38:17 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:50:43 +04:00
|
|
|
obj = _rtld_obj_new();
|
2006-03-21 20:48:10 +03:00
|
|
|
obj->path = xstrdup(path);
|
2002-12-05 07:56:56 +03:00
|
|
|
obj->pathlen = strlen(path);
|
2002-10-04 22:50:43 +04:00
|
|
|
if (sb != NULL) {
|
|
|
|
obj->dev = sb->st_dev;
|
|
|
|
obj->ino = sb->st_ino;
|
|
|
|
}
|
|
|
|
|
2002-09-24 16:44:58 +04:00
|
|
|
ehdr = mmap(NULL, _rtld_pagesz, PROT_READ, MAP_FILE | MAP_SHARED, fd,
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
(off_t)0);
|
2008-06-03 23:32:32 +04:00
|
|
|
obj->ehdr = ehdr;
|
2002-09-24 16:44:58 +04:00
|
|
|
if (ehdr == MAP_FAILED) {
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_error("%s: read error: %s", path, xstrerror(errno));
|
2002-10-04 22:50:43 +04:00
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
|
|
|
/* Make sure the file is valid */
|
2012-10-14 01:13:07 +04:00
|
|
|
if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0) {
|
|
|
|
_rtld_error("%s: not ELF file (magic number bad)", path);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
|
|
|
|
_rtld_error("%s: invalid ELF class %x; expected %x", path,
|
2010-09-11 15:11:52 +04:00
|
|
|
ehdr->e_ident[EI_CLASS], ELFCLASS);
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
|
|
|
/* Elf_e_ident includes class */
|
2002-09-24 13:22:51 +04:00
|
|
|
if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
|
|
|
|
ehdr->e_version != EV_CURRENT ||
|
|
|
|
ehdr->e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS)) {
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
_rtld_error("%s: unsupported file version", path);
|
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
2002-09-24 13:22:51 +04:00
|
|
|
if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
_rtld_error("%s: unsupported file type", path);
|
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
2002-09-24 13:22:51 +04:00
|
|
|
switch (ehdr->e_machine) {
|
1999-03-01 19:40:07 +03:00
|
|
|
ELFDEFNNAME(MACHDEP_ID_CASES)
|
|
|
|
default:
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
_rtld_error("%s: unsupported machine", path);
|
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
|
|
|
* We rely on the program header being in the first page. This is
|
|
|
|
* not strictly required by the ABI specification, but it seems to
|
|
|
|
* always true in practice. And, it simplifies things considerably.
|
|
|
|
*/
|
2002-09-24 13:22:51 +04:00
|
|
|
assert(ehdr->e_phentsize == sizeof(Elf_Phdr));
|
2002-09-24 13:35:13 +04:00
|
|
|
assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <=
|
|
|
|
_rtld_pagesz);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
|
|
|
* Scan the program header entries, and save key information.
|
|
|
|
*
|
|
|
|
* We rely on there being exactly two load segments, text and data,
|
|
|
|
* in that order.
|
|
|
|
*/
|
2002-09-24 13:22:51 +04:00
|
|
|
phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
phtls = NULL;
|
|
|
|
#endif
|
2010-10-16 14:27:06 +04:00
|
|
|
phsize = ehdr->e_phnum * sizeof(phdr[0]);
|
|
|
|
obj->phdr = NULL;
|
2016-06-14 16:06:41 +03:00
|
|
|
#ifdef GNU_RELRO
|
|
|
|
relro_page = 0;
|
|
|
|
relro_size = 0;
|
|
|
|
#endif
|
2010-10-16 14:27:06 +04:00
|
|
|
phdr_vaddr = EA_UNDEF;
|
|
|
|
phdr_memsz = 0;
|
2002-09-24 13:22:51 +04:00
|
|
|
phlimit = phdr + ehdr->e_phnum;
|
1999-03-01 19:40:07 +03:00
|
|
|
nsegs = 0;
|
|
|
|
while (phdr < phlimit) {
|
|
|
|
switch (phdr->p_type) {
|
1999-11-07 03:21:12 +03:00
|
|
|
case PT_INTERP:
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->interp = (void *)(uintptr_t)phdr->p_vaddr;
|
2010-10-16 14:27:06 +04:00
|
|
|
dbg(("%s: PT_INTERP %p", obj->path, obj->interp));
|
1999-11-07 03:21:12 +03:00
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_LOAD:
|
2000-11-26 08:22:15 +03:00
|
|
|
if (nsegs < 2)
|
|
|
|
segs[nsegs] = phdr;
|
1999-03-01 19:40:07 +03:00
|
|
|
++nsegs;
|
2012-07-26 02:51:04 +04:00
|
|
|
|
|
|
|
dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_LOAD",
|
2011-08-14 02:25:20 +04:00
|
|
|
(void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2010-10-16 14:27:06 +04:00
|
|
|
case PT_PHDR:
|
|
|
|
phdr_vaddr = phdr->p_vaddr;
|
|
|
|
phdr_memsz = phdr->p_memsz;
|
2012-07-26 02:51:04 +04:00
|
|
|
dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_PHDR",
|
2011-08-14 02:25:20 +04:00
|
|
|
(void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
|
2010-10-16 14:27:06 +04:00
|
|
|
break;
|
2013-05-06 12:02:20 +04:00
|
|
|
|
2016-06-14 16:06:41 +03:00
|
|
|
#ifdef GNU_RELRO
|
|
|
|
case PT_GNU_RELRO:
|
|
|
|
relro_page = phdr->p_vaddr;
|
|
|
|
relro_size = phdr->p_memsz;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_DYNAMIC:
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->dynamic = (void *)(uintptr_t)phdr->p_vaddr;
|
2012-07-26 02:51:04 +04:00
|
|
|
dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_DYNAMIC",
|
2011-08-14 02:25:20 +04:00
|
|
|
(void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
2011-03-10 02:10:05 +03:00
|
|
|
|
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
case PT_TLS:
|
|
|
|
phtls = phdr;
|
2012-07-26 02:51:04 +04:00
|
|
|
dbg(("%s: %s %p phsize %" PRImemsz, obj->path, "PT_TLS",
|
2011-08-14 02:25:20 +04:00
|
|
|
(void *)(uintptr_t)phdr->p_vaddr, phdr->p_memsz));
|
2011-03-10 02:10:05 +03:00
|
|
|
break;
|
2013-04-25 13:01:23 +04:00
|
|
|
#endif
|
|
|
|
#ifdef __ARM_EABI__
|
|
|
|
case PT_ARM_EXIDX:
|
|
|
|
obj->exidx_start = (void *)(uintptr_t)phdr->p_vaddr;
|
|
|
|
obj->exidx_sz = phdr->p_memsz;
|
|
|
|
break;
|
2011-03-10 02:10:05 +03:00
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
++phdr;
|
|
|
|
}
|
2010-10-16 14:27:06 +04:00
|
|
|
phdr = (Elf_Phdr *) ((caddr_t)ehdr + ehdr->e_phoff);
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->entry = (void *)(uintptr_t)ehdr->e_entry;
|
2002-10-04 22:50:43 +04:00
|
|
|
if (!obj->dynamic) {
|
2000-11-26 08:22:15 +03:00
|
|
|
_rtld_error("%s: not dynamically linked", path);
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
goto bad;
|
2000-11-26 08:22:15 +03:00
|
|
|
}
|
|
|
|
if (nsegs != 2) {
|
|
|
|
_rtld_error("%s: wrong number of segments (%d != 2)", path,
|
|
|
|
nsegs);
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
2000-02-13 07:28:09 +03:00
|
|
|
* Map the entire address space of the object as a file
|
1999-08-07 02:33:49 +04:00
|
|
|
* region to stake out our contiguous region and establish a
|
2000-02-13 07:28:09 +03:00
|
|
|
* base for relocation. We use a file mapping so that
|
|
|
|
* the kernel will give us whatever alignment is appropriate
|
|
|
|
* for the platform we're running on.
|
1999-08-07 02:33:49 +04:00
|
|
|
*
|
2000-02-13 07:28:09 +03:00
|
|
|
* We map it using the text protection, map the data segment
|
|
|
|
* into the right place, then map an anon segment for the bss
|
|
|
|
* and unmap the gaps left by padding to alignment.
|
1999-08-07 02:33:49 +04:00
|
|
|
*/
|
2000-02-13 07:28:09 +03:00
|
|
|
|
2003-03-06 10:34:56 +03:00
|
|
|
base_alignment = segs[0]->p_align;
|
1999-03-01 19:40:07 +03:00
|
|
|
base_offset = round_down(segs[0]->p_offset);
|
|
|
|
base_vaddr = round_down(segs[0]->p_vaddr);
|
|
|
|
base_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_memsz);
|
2000-02-13 07:28:09 +03:00
|
|
|
text_vlimit = round_up(segs[0]->p_vaddr + segs[0]->p_memsz);
|
2002-10-04 22:50:43 +04:00
|
|
|
text_flags = protflags(segs[0]->p_flags);
|
|
|
|
data_offset = round_down(segs[1]->p_offset);
|
|
|
|
data_vaddr = round_down(segs[1]->p_vaddr);
|
|
|
|
data_vlimit = round_up(segs[1]->p_vaddr + segs[1]->p_filesz);
|
|
|
|
data_flags = protflags(segs[1]->p_flags);
|
2002-10-05 04:13:27 +04:00
|
|
|
#ifdef RTLD_LOADER
|
2002-10-04 22:50:43 +04:00
|
|
|
clear_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
|
2002-10-05 04:13:27 +04:00
|
|
|
#endif
|
2002-10-04 22:50:43 +04:00
|
|
|
|
|
|
|
obj->textsize = text_vlimit - base_vaddr;
|
|
|
|
obj->vaddrbase = base_vaddr;
|
|
|
|
obj->isdynamic = ehdr->e_type == ET_DYN;
|
|
|
|
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
if (phtls != NULL) {
|
|
|
|
++_rtld_tls_dtv_generation;
|
|
|
|
obj->tlsindex = ++_rtld_tls_max_index;
|
|
|
|
obj->tlssize = phtls->p_memsz;
|
|
|
|
obj->tlsalign = phtls->p_align;
|
|
|
|
obj->tlsinitsize = phtls->p_filesz;
|
|
|
|
tls_vaddr = phtls->p_vaddr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-16 14:27:06 +04:00
|
|
|
obj->phdr_loaded = false;
|
|
|
|
for (i = 0; i < nsegs; i++) {
|
|
|
|
if (phdr_vaddr != EA_UNDEF &&
|
|
|
|
segs[i]->p_vaddr <= phdr_vaddr &&
|
|
|
|
segs[i]->p_memsz >= phdr_memsz) {
|
|
|
|
obj->phdr_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (segs[i]->p_offset <= ehdr->e_phoff &&
|
|
|
|
segs[i]->p_memsz >= phsize) {
|
|
|
|
phdr_vaddr = segs[i]->p_vaddr + ehdr->e_phoff;
|
|
|
|
phdr_memsz = phsize;
|
|
|
|
obj->phdr_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obj->phdr_loaded) {
|
|
|
|
obj->phdr = (void *)(uintptr_t)phdr_vaddr;
|
|
|
|
obj->phsize = phdr_memsz;
|
|
|
|
} else {
|
|
|
|
Elf_Phdr *buf;
|
|
|
|
buf = xmalloc(phsize);
|
|
|
|
if (buf == NULL) {
|
|
|
|
_rtld_error("%s: cannot allocate program header", path);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
memcpy(buf, phdr, phsize);
|
|
|
|
obj->phdr = buf;
|
|
|
|
obj->phsize = phsize;
|
|
|
|
}
|
|
|
|
dbg(("%s: phdr %p phsize %zu (%s)", obj->path, obj->phdr, obj->phsize,
|
|
|
|
obj->phdr_loaded ? "loaded" : "allocated"));
|
|
|
|
|
2008-06-03 23:32:32 +04:00
|
|
|
/* Unmap header if it overlaps the first load section. */
|
|
|
|
if (base_offset < _rtld_pagesz) {
|
|
|
|
munmap(ehdr, _rtld_pagesz);
|
|
|
|
obj->ehdr = MAP_FAILED;
|
|
|
|
}
|
2000-02-13 07:28:09 +03:00
|
|
|
|
2003-03-06 10:34:56 +03:00
|
|
|
/*
|
|
|
|
* Calculate log2 of the base section alignment.
|
|
|
|
*/
|
|
|
|
mapflags = 0;
|
|
|
|
if (base_alignment > _rtld_pagesz) {
|
|
|
|
unsigned int log2 = 0;
|
|
|
|
for (; base_alignment > 1; base_alignment >>= 1)
|
|
|
|
log2++;
|
|
|
|
mapflags = MAP_ALIGNED(log2);
|
|
|
|
}
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
base_addr = NULL;
|
2019-01-06 22:44:54 +03:00
|
|
|
#ifdef RTLD_LOADER
|
|
|
|
if (!obj->isdynamic) {
|
|
|
|
mapflags |= MAP_TRYFIXED;
|
|
|
|
base_addr = (void *)(uintptr_t)base_vaddr;
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
2002-10-04 22:50:43 +04:00
|
|
|
mapsize = base_vlimit - base_vaddr;
|
2003-03-06 10:34:56 +03:00
|
|
|
mapbase = mmap(base_addr, mapsize, text_flags,
|
|
|
|
mapflags | MAP_FILE | MAP_PRIVATE, fd, base_offset);
|
1999-08-07 02:33:49 +04:00
|
|
|
if (mapbase == MAP_FAILED) {
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_error("mmap of entire address space failed: %s",
|
|
|
|
xstrerror(errno));
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
2019-01-06 22:44:54 +03:00
|
|
|
#ifdef RTLD_LOADER
|
|
|
|
if (!obj->isdynamic && mapbase != base_addr) {
|
|
|
|
_rtld_error("mmap of executable at correct address failed");
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-07 02:33:49 +04:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
/* Overlay the data segment onto the proper region. */
|
|
|
|
data_addr = mapbase + (data_vaddr - base_vaddr);
|
2017-03-10 12:13:49 +03:00
|
|
|
if (mmap(data_addr, data_vlimit - data_vaddr, data_flags,
|
2002-10-04 22:50:43 +04:00
|
|
|
MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, data_offset) ==
|
|
|
|
MAP_FAILED) {
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_error("mmap of data failed: %s", xstrerror(errno));
|
2002-10-04 22:50:43 +04:00
|
|
|
goto bad;
|
2000-02-13 07:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Overlay the bss segment onto the proper region. */
|
2017-03-10 12:13:49 +03:00
|
|
|
if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
|
2002-10-04 22:50:43 +04:00
|
|
|
data_flags, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0) ==
|
|
|
|
MAP_FAILED) {
|
2000-02-13 07:28:09 +03:00
|
|
|
_rtld_error("mmap of bss failed: %s", xstrerror(errno));
|
2002-10-04 22:50:43 +04:00
|
|
|
goto bad;
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1999-08-07 02:33:49 +04:00
|
|
|
|
|
|
|
/* Unmap the gap between the text and data. */
|
2002-10-04 22:50:43 +04:00
|
|
|
gap_addr = mapbase + round_up(text_vlimit - base_vaddr);
|
1999-08-07 02:33:49 +04:00
|
|
|
gap_size = data_addr - gap_addr;
|
2017-03-10 12:13:49 +03:00
|
|
|
if (gap_size != 0 && mprotect(gap_addr, gap_size, PROT_NONE) == -1) {
|
|
|
|
_rtld_error("mprotect of text -> data gap failed: %s",
|
1999-08-07 02:33:49 +04:00
|
|
|
xstrerror(errno));
|
2002-10-04 22:50:43 +04:00
|
|
|
goto bad;
|
1999-08-07 02:33:49 +04:00
|
|
|
}
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
/* Clear any BSS in the last page of the data segment. */
|
|
|
|
clear_addr = mapbase + (clear_vaddr - base_vaddr);
|
|
|
|
if ((nclear = data_vlimit - clear_vaddr) > 0)
|
|
|
|
memset(clear_addr, 0, nclear);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-08-07 02:33:49 +04:00
|
|
|
/* Non-file portion of BSS mapped above. */
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
if (phtls != NULL)
|
|
|
|
obj->tlsinit = mapbase + tls_vaddr;
|
|
|
|
#endif
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->mapbase = mapbase;
|
|
|
|
obj->mapsize = mapsize;
|
|
|
|
obj->relocbase = mapbase - base_vaddr;
|
1999-11-07 03:21:12 +03:00
|
|
|
|
2016-06-16 14:34:13 +03:00
|
|
|
#ifdef GNU_RELRO
|
2020-03-04 04:21:17 +03:00
|
|
|
/* rounding happens later. */
|
|
|
|
obj->relro_page = obj->relocbase + relro_page;
|
|
|
|
obj->relro_size = relro_size;
|
2016-06-16 14:34:13 +03:00
|
|
|
#endif
|
|
|
|
|
2002-10-04 22:50:43 +04:00
|
|
|
if (obj->dynamic)
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->dynamic = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->dynamic);
|
2002-10-04 22:50:43 +04:00
|
|
|
if (obj->entry)
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->entry = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->entry);
|
2002-10-04 22:50:43 +04:00
|
|
|
if (obj->interp)
|
2009-01-06 07:01:46 +03:00
|
|
|
obj->interp = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->interp);
|
2010-10-16 14:27:06 +04:00
|
|
|
if (obj->phdr_loaded)
|
|
|
|
obj->phdr = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->phdr);
|
2013-04-25 16:28:59 +04:00
|
|
|
#ifdef __ARM_EABI__
|
|
|
|
if (obj->exidx_start)
|
|
|
|
obj->exidx_start = (void *)(obj->relocbase + (Elf_Addr)(uintptr_t)obj->exidx_start);
|
|
|
|
#endif
|
2002-10-04 22:50:43 +04:00
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
return obj;
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
|
|
|
|
bad:
|
2008-06-03 23:32:32 +04:00
|
|
|
if (obj->ehdr != MAP_FAILED)
|
|
|
|
munmap(obj->ehdr, _rtld_pagesz);
|
2002-10-04 22:50:43 +04:00
|
|
|
if (mapbase != MAP_FAILED)
|
|
|
|
munmap(mapbase, mapsize);
|
|
|
|
_rtld_obj_free(obj);
|
Several small changes that shave 7-8% off the simple-exec-loop test:
* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().
* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.
* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.
* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)
* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().
Also:
* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.
2002-09-24 03:56:46 +04:00
|
|
|
return NULL;
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-07-24 14:12:25 +04:00
|
|
|
_rtld_obj_free(Obj_Entry *obj)
|
1999-11-07 03:21:12 +03:00
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
2013-05-09 19:38:14 +04:00
|
|
|
Name_Entry *entry;
|
1999-11-07 03:21:12 +03:00
|
|
|
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
if (obj->tls_done)
|
|
|
|
_rtld_tls_offset_free(obj);
|
|
|
|
#endif
|
2007-10-06 02:21:07 +04:00
|
|
|
xfree(obj->path);
|
1999-11-07 03:21:12 +03:00
|
|
|
while (obj->needed != NULL) {
|
|
|
|
Needed_Entry *needed = obj->needed;
|
|
|
|
obj->needed = needed->next;
|
2007-10-06 02:21:07 +04:00
|
|
|
xfree(needed);
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
2013-05-09 19:38:14 +04:00
|
|
|
while ((entry = SIMPLEQ_FIRST(&obj->names)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&obj->names, link);
|
2013-05-08 19:25:01 +04:00
|
|
|
xfree(entry);
|
2013-05-06 23:59:29 +04:00
|
|
|
}
|
2002-06-02 03:50:52 +04:00
|
|
|
while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
|
2007-10-06 02:21:07 +04:00
|
|
|
xfree(elm);
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
2002-06-02 03:50:52 +04:00
|
|
|
while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
|
2007-10-06 02:21:07 +04:00
|
|
|
xfree(elm);
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
2010-10-16 14:27:06 +04:00
|
|
|
if (!obj->phdr_loaded)
|
|
|
|
xfree((void *)(uintptr_t)obj->phdr);
|
2014-10-30 10:53:41 +03:00
|
|
|
xfree(obj);
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Obj_Entry *
|
|
|
|
_rtld_obj_new(void)
|
|
|
|
{
|
|
|
|
Obj_Entry *obj;
|
|
|
|
|
|
|
|
obj = CNEW(Obj_Entry);
|
2013-05-09 19:38:14 +04:00
|
|
|
SIMPLEQ_INIT(&obj->names);
|
1999-11-07 03:21:12 +03:00
|
|
|
SIMPLEQ_INIT(&obj->dldags);
|
|
|
|
SIMPLEQ_INIT(&obj->dagmembers);
|
1999-03-01 19:40:07 +03:00
|
|
|
return obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a set of ELF protection flags, return the corresponding protection
|
|
|
|
* flags for MMAP.
|
|
|
|
*/
|
|
|
|
static int
|
2003-07-24 14:12:25 +04:00
|
|
|
protflags(int elfflags)
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
int prot = 0;
|
2003-06-05 14:41:32 +04:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
if (elfflags & PF_R)
|
1999-03-01 19:40:07 +03:00
|
|
|
prot |= PROT_READ;
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-10-25 17:55:06 +04:00
|
|
|
if (elfflags & PF_W)
|
1999-03-01 19:40:07 +03:00
|
|
|
prot |= PROT_WRITE;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
1999-10-25 17:55:06 +04:00
|
|
|
if (elfflags & PF_X)
|
1999-03-01 19:40:07 +03:00
|
|
|
prot |= PROT_EXEC;
|
|
|
|
return prot;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|