2002-09-24 13:22:51 +04:00
|
|
|
/* $NetBSD: map_object.c,v 1.17 2002/09/24 09:22:51 junyoung Exp $ */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1996 John D. Polstra.
|
|
|
|
* Copyright 1996 Matt Thomas <matt@3am-software.com>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "rtld.h"
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
static int protflags __P((int)); /* Elf flags -> mmap protection */
|
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 *
|
1999-11-07 03:21:12 +03:00
|
|
|
_rtld_map_object(path, fd, sb)
|
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
|
|
|
char *path;
|
1999-03-01 19:40:07 +03:00
|
|
|
int fd;
|
1999-11-07 03:21:12 +03:00
|
|
|
const struct stat *sb;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Obj_Entry *obj;
|
2002-09-24 13:22:51 +04:00
|
|
|
Elf_Ehdr *ehdr;
|
|
|
|
void *u;
|
1999-03-01 19:40:07 +03:00
|
|
|
Elf_Phdr *phdr;
|
|
|
|
Elf_Phdr *phlimit;
|
|
|
|
Elf_Phdr *segs[2];
|
|
|
|
int nsegs;
|
|
|
|
Elf_Phdr *phdyn;
|
|
|
|
Elf_Phdr *phphdr;
|
1999-11-07 03:21:12 +03:00
|
|
|
Elf_Phdr *phinterp;
|
1999-03-01 19:40:07 +03:00
|
|
|
caddr_t mapbase;
|
|
|
|
size_t mapsize;
|
|
|
|
Elf_Off base_offset;
|
|
|
|
Elf_Addr base_vaddr;
|
|
|
|
Elf_Addr base_vlimit;
|
1999-08-07 02:33:49 +04:00
|
|
|
Elf_Addr text_vlimit;
|
1999-03-01 19:40:07 +03:00
|
|
|
caddr_t base_addr;
|
|
|
|
Elf_Off data_offset;
|
|
|
|
Elf_Addr data_vaddr;
|
|
|
|
Elf_Addr data_vlimit;
|
|
|
|
caddr_t data_addr;
|
1999-08-07 02:33:49 +04:00
|
|
|
caddr_t gap_addr;
|
|
|
|
size_t gap_size;
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
Elf_Addr clear_vaddr;
|
|
|
|
caddr_t clear_addr;
|
|
|
|
size_t nclear;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
u = mmap(NULL, PAGESIZE, PROT_READ, MAP_FILE | MAP_SHARED, fd,
|
|
|
|
(off_t)0);
|
|
|
|
if (u == MAP_FAILED) {
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_error("%s: read error: %s", path, xstrerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-09-24 13:22:51 +04:00
|
|
|
ehdr = (Elf_Ehdr *)u;
|
1999-03-01 19:40:07 +03:00
|
|
|
/* Make sure the file is valid */
|
2002-09-24 13:22:51 +04:00
|
|
|
if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
|
|
|
|
ehdr->e_ident[EI_CLASS] != ELFCLASS) {
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_error("%s: unrecognized file format", 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;
|
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));
|
|
|
|
assert(ehdr->e_phoff + ehdr->e_phnum * sizeof(Elf_Phdr) <= PAGESIZE);
|
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);
|
|
|
|
phlimit = phdr + ehdr->e_phnum;
|
1999-03-01 19:40:07 +03:00
|
|
|
nsegs = 0;
|
1999-11-07 03:21:12 +03:00
|
|
|
phdyn = phphdr = phinterp = NULL;
|
1999-03-01 19:40:07 +03:00
|
|
|
while (phdr < phlimit) {
|
|
|
|
switch (phdr->p_type) {
|
1999-11-07 03:21:12 +03:00
|
|
|
case PT_INTERP:
|
|
|
|
phinterp = phdr;
|
|
|
|
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;
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_PHDR:
|
1999-03-01 19:40:07 +03:00
|
|
|
phphdr = phdr;
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_DYNAMIC:
|
1999-03-01 19:40:07 +03:00
|
|
|
phdyn = phdr;
|
|
|
|
break;
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
++phdr;
|
|
|
|
}
|
|
|
|
if (phdyn == NULL) {
|
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
|
|
|
|
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);
|
1999-03-01 19:40:07 +03:00
|
|
|
mapsize = base_vlimit - base_vaddr;
|
2000-02-13 07:28:09 +03:00
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
2002-09-24 13:22:51 +04:00
|
|
|
base_addr = ehdr->e_type == ET_EXEC ? (caddr_t) base_vaddr : NULL;
|
1996-12-16 23:37:55 +03:00
|
|
|
#else
|
1999-03-01 19:40:07 +03:00
|
|
|
base_addr = NULL;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
|
|
|
|
2000-02-13 07:28:09 +03:00
|
|
|
mapbase = mmap(base_addr, mapsize, protflags(segs[0]->p_flags),
|
|
|
|
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
|
|
|
}
|
1999-08-07 02:33:49 +04:00
|
|
|
|
2000-02-13 07:28:09 +03:00
|
|
|
base_addr = mapbase;
|
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_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_addr = mapbase + (data_vaddr - base_vaddr);
|
|
|
|
if (mmap(data_addr, data_vlimit - data_vaddr,
|
2000-02-13 07:28:09 +03:00
|
|
|
protflags(segs[1]->p_flags),
|
|
|
|
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));
|
2000-02-13 07:28:09 +03:00
|
|
|
munmap(mapbase, mapsize);
|
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-02-13 07:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Overlay the bss segment onto the proper region. */
|
|
|
|
if (mmap(mapbase + data_vlimit - base_vaddr, base_vlimit - data_vlimit,
|
|
|
|
protflags(segs[1]->p_flags),
|
|
|
|
MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0)
|
|
|
|
== MAP_FAILED) {
|
|
|
|
_rtld_error("mmap of bss failed: %s", xstrerror(errno));
|
|
|
|
munmap(mapbase, mapsize);
|
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
|
|
|
}
|
1999-08-07 02:33:49 +04:00
|
|
|
|
|
|
|
/* Unmap the gap between the text and data. */
|
|
|
|
gap_addr = base_addr + round_up(text_vlimit - base_vaddr);
|
|
|
|
gap_size = data_addr - gap_addr;
|
|
|
|
if (gap_size != 0 && munmap(gap_addr, gap_size) == -1) {
|
|
|
|
_rtld_error("munmap of text -> data gap failed: %s",
|
|
|
|
xstrerror(errno));
|
2000-02-13 07:28:09 +03:00
|
|
|
munmap(mapbase, mapsize);
|
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-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_vaddr = segs[1]->p_vaddr + segs[1]->p_filesz;
|
|
|
|
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
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
obj = _rtld_obj_new();
|
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
|
|
|
obj->path = path;
|
1999-11-07 03:21:12 +03:00
|
|
|
if (sb != NULL) {
|
|
|
|
obj->dev = sb->st_dev;
|
|
|
|
obj->ino = sb->st_ino;
|
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->mapbase = mapbase;
|
|
|
|
obj->mapsize = mapsize;
|
|
|
|
obj->textsize = round_up(segs[0]->p_vaddr + segs[0]->p_memsz) -
|
|
|
|
base_vaddr;
|
|
|
|
obj->vaddrbase = base_vaddr;
|
|
|
|
obj->relocbase = mapbase - base_vaddr;
|
|
|
|
obj->dynamic = (Elf_Dyn *)(obj->relocbase + phdyn->p_vaddr);
|
2002-09-24 13:22:51 +04:00
|
|
|
if (ehdr->e_entry != 0)
|
|
|
|
obj->entry = (caddr_t)(obj->relocbase + ehdr->e_entry);
|
1999-03-01 19:40:07 +03:00
|
|
|
if (phphdr != NULL) {
|
|
|
|
obj->phdr = (const Elf_Phdr *)
|
|
|
|
(obj->relocbase + phphdr->p_vaddr);
|
|
|
|
obj->phsize = phphdr->p_memsz;
|
|
|
|
}
|
1999-11-07 03:21:12 +03:00
|
|
|
if (phinterp != NULL)
|
|
|
|
obj->interp = (const char *) (obj->relocbase + phinterp->p_vaddr);
|
2002-09-24 13:22:51 +04:00
|
|
|
obj->isdynamic = ehdr->e_type == ET_DYN;
|
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:
|
|
|
|
munmap(u, PAGESIZE);
|
|
|
|
return NULL;
|
1999-11-07 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_rtld_obj_free(obj)
|
|
|
|
Obj_Entry *obj;
|
|
|
|
{
|
|
|
|
Objlist_Entry *elm;
|
|
|
|
|
|
|
|
free(obj->path);
|
|
|
|
while (obj->needed != NULL) {
|
|
|
|
Needed_Entry *needed = obj->needed;
|
|
|
|
obj->needed = needed->next;
|
|
|
|
free(needed);
|
|
|
|
}
|
2002-06-02 03:50:52 +04:00
|
|
|
while ((elm = SIMPLEQ_FIRST(&obj->dldags)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&obj->dldags, link);
|
1999-11-07 03:21:12 +03:00
|
|
|
free(elm);
|
|
|
|
}
|
2002-06-02 03:50:52 +04:00
|
|
|
while ((elm = SIMPLEQ_FIRST(&obj->dagmembers)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE_HEAD(&obj->dagmembers, link);
|
1999-11-07 03:21:12 +03:00
|
|
|
free(elm);
|
|
|
|
}
|
|
|
|
free(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
Obj_Entry *
|
|
|
|
_rtld_obj_new(void)
|
|
|
|
{
|
|
|
|
Obj_Entry *obj;
|
|
|
|
|
|
|
|
obj = CNEW(Obj_Entry);
|
|
|
|
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
|
1999-03-01 19:40:07 +03:00
|
|
|
protflags(elfflags)
|
|
|
|
int elfflags;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
int prot = 0;
|
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
|
|
|
}
|