2002-10-04 22:50:43 +04:00
|
|
|
/* $NetBSD: headers.c,v 1.16 2002/10/04 18:50:43 mycroft 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dynamic linker for ELF.
|
|
|
|
*
|
|
|
|
* John Polstra <jdp@polstra.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rtld.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a shared object's DYNAMIC section, and save the important
|
|
|
|
* information in its Obj_Entry structure.
|
|
|
|
*/
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_digest_dynamic(obj)
|
|
|
|
Obj_Entry *obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
Elf_Dyn *dynp;
|
|
|
|
Needed_Entry **needed_tail = &obj->needed;
|
|
|
|
const Elf_Dyn *dyn_rpath = NULL;
|
2002-09-27 00:27:50 +04:00
|
|
|
Elf_Sword plttype = DT_NULL;
|
2000-07-26 06:07:34 +04:00
|
|
|
Elf_Addr relsz = 0, relasz = 0;
|
2002-09-13 17:28:43 +04:00
|
|
|
Elf_Addr pltrel = 0, pltrelsz = 0;
|
2002-07-10 19:12:33 +04:00
|
|
|
Elf_Addr init = 0, fini = 0;
|
1999-03-01 19:40:07 +03:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) {
|
1999-03-01 19:40:07 +03:00
|
|
|
switch (dynp->d_tag) {
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_REL:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->rel = (const Elf_Rel *)
|
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RELSZ:
|
1999-03-01 19:40:07 +03:00
|
|
|
relsz = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RELENT:
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(dynp->d_un.d_val == sizeof(Elf_Rel));
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_JMPREL:
|
2002-09-13 17:28:43 +04:00
|
|
|
pltrel = dynp->d_un.d_ptr;
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_PLTRELSZ:
|
2000-07-26 06:07:34 +04:00
|
|
|
pltrelsz = dynp->d_un.d_val;
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RELA:
|
2001-04-25 16:24:50 +04:00
|
|
|
obj->rela = (const Elf_Rela *)
|
1999-03-01 19:40:07 +03:00
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RELASZ:
|
1999-03-01 19:40:07 +03:00
|
|
|
relasz = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RELAENT:
|
2001-04-25 16:24:50 +04:00
|
|
|
assert(dynp->d_un.d_val == sizeof(Elf_Rela));
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_PLTREL:
|
1999-03-01 19:40:07 +03:00
|
|
|
plttype = dynp->d_un.d_val;
|
2002-09-13 17:28:43 +04:00
|
|
|
assert(plttype == DT_REL || plttype == DT_RELA);
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_SYMTAB:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->symtab = (const Elf_Sym *)
|
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_SYMENT:
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(dynp->d_un.d_val == sizeof(Elf_Sym));
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_STRTAB:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->strtab = (const char *)
|
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_STRSZ:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->strsize = dynp->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_HASH:
|
1999-03-01 19:40:07 +03:00
|
|
|
{
|
|
|
|
const Elf_Word *hashtab = (const Elf_Word *)
|
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
|
|
|
|
obj->nbuckets = hashtab[0];
|
|
|
|
obj->nchains = hashtab[1];
|
|
|
|
obj->buckets = hashtab + 2;
|
|
|
|
obj->chains = obj->buckets + obj->nbuckets;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_NEEDED:
|
1999-03-01 19:40:07 +03:00
|
|
|
{
|
|
|
|
Needed_Entry *nep = NEW(Needed_Entry);
|
|
|
|
|
|
|
|
nep->name = dynp->d_un.d_val;
|
|
|
|
nep->obj = NULL;
|
|
|
|
nep->next = NULL;
|
|
|
|
|
|
|
|
*needed_tail = nep;
|
|
|
|
needed_tail = &nep->next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_PLTGOT:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->pltgot = (Elf_Addr *)
|
|
|
|
(obj->relocbase + dynp->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_TEXTREL:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->textrel = true;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_SYMBOLIC:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->symbolic = true;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_RPATH:
|
1999-03-01 19:40:07 +03:00
|
|
|
/*
|
|
|
|
* We have to wait until later to process this, because
|
|
|
|
* we might not have gotten the address of the string
|
|
|
|
* table yet.
|
|
|
|
*/
|
|
|
|
dyn_rpath = dynp;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_SONAME:
|
1999-03-01 19:40:07 +03:00
|
|
|
/* Not used by the dynamic linker. */
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_INIT:
|
2002-07-10 19:12:33 +04:00
|
|
|
init = dynp->d_un.d_ptr;
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_FINI:
|
2002-07-10 19:12:33 +04:00
|
|
|
fini = dynp->d_un.d_ptr;
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case DT_DEBUG:
|
1996-12-16 23:37:55 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug;
|
1996-12-16 23:37:55 +03:00
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
1998-03-25 07:12:32 +03:00
|
|
|
|
2002-09-25 10:43:46 +04:00
|
|
|
#ifdef __mips__
|
1999-03-01 19:40:07 +03:00
|
|
|
case DT_MIPS_LOCAL_GOTNO:
|
|
|
|
obj->local_gotno = dynp->d_un.d_val;
|
|
|
|
break;
|
1998-03-25 07:12:32 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
case DT_MIPS_SYMTABNO:
|
|
|
|
obj->symtabno = dynp->d_un.d_val;
|
|
|
|
break;
|
1998-03-25 07:12:32 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
case DT_MIPS_GOTSYM:
|
|
|
|
obj->gotsym = dynp->d_un.d_val;
|
|
|
|
break;
|
1998-03-25 07:12:32 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
case DT_MIPS_RLD_MAP:
|
1998-03-25 07:12:32 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-03-01 19:40:07 +03:00
|
|
|
*((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr)
|
|
|
|
&_rtld_debug;
|
1998-03-25 07:12:32 +03:00
|
|
|
#endif
|
1999-03-01 19:40:07 +03:00
|
|
|
break;
|
1998-03-25 07:12:32 +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
|
|
|
obj->rellim = (const Elf_Rel *)((caddr_t)obj->rel + relsz);
|
2001-04-25 16:24:50 +04:00
|
|
|
obj->relalim = (const Elf_Rela *)((caddr_t)obj->rela + relasz);
|
2000-07-26 06:07:34 +04:00
|
|
|
if (plttype == DT_REL) {
|
2002-09-13 17:28:43 +04:00
|
|
|
obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel);
|
|
|
|
obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz);
|
2000-07-26 06:07:34 +04:00
|
|
|
obj->pltrelalim = 0;
|
2002-09-13 17:28:43 +04:00
|
|
|
/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
|
|
|
|
Trim rel(a)lim to save time later. */
|
|
|
|
if (obj->rellim && obj->pltrel &&
|
|
|
|
obj->rellim > obj->pltrel &&
|
|
|
|
obj->rellim <= obj->pltrellim)
|
|
|
|
obj->rellim = obj->pltrel;
|
2002-09-27 00:27:50 +04:00
|
|
|
} else if (plttype == DT_RELA) {
|
2002-09-13 17:28:43 +04:00
|
|
|
obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel);
|
2000-07-26 06:07:34 +04:00
|
|
|
obj->pltrellim = 0;
|
2002-09-13 17:28:43 +04:00
|
|
|
obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz);
|
|
|
|
/* On PPC and SPARC, at least, REL(A)SZ may include JMPREL.
|
|
|
|
Trim rel(a)lim to save time later. */
|
|
|
|
if (obj->relalim && obj->pltrela &&
|
|
|
|
obj->relalim > obj->pltrela &&
|
|
|
|
obj->relalim <= obj->pltrelalim)
|
|
|
|
obj->relalim = obj->pltrela;
|
2000-07-26 06:07:34 +04:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
|
2002-07-10 19:12:33 +04:00
|
|
|
#if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS)
|
|
|
|
if (init != 0)
|
|
|
|
obj->init = (void (*) __P((void)))
|
|
|
|
_rtld_function_descriptor_alloc(obj, NULL, init);
|
|
|
|
if (fini != 0)
|
|
|
|
obj->fini = (void (*) __P((void)))
|
|
|
|
_rtld_function_descriptor_alloc(obj, NULL, fini);
|
|
|
|
#else
|
|
|
|
if (init != 0)
|
|
|
|
obj->init = (void (*) __P((void)))
|
|
|
|
(obj->relocbase + init);
|
|
|
|
if (fini != 0)
|
|
|
|
obj->fini = (void (*) __P((void)))
|
|
|
|
(obj->relocbase + fini);
|
|
|
|
#endif
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (dyn_rpath != NULL) {
|
|
|
|
_rtld_add_paths(&obj->rpaths, obj->strtab +
|
2002-09-13 02:56:28 +04:00
|
|
|
dyn_rpath->d_un.d_val);
|
1999-03-01 19:40:07 +03:00
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a shared object's program header. This is used only for the
|
|
|
|
* main program, when the kernel has already loaded the main program
|
|
|
|
* into memory before calling the dynamic linker. It creates and
|
|
|
|
* returns an Obj_Entry structure.
|
|
|
|
*/
|
|
|
|
Obj_Entry *
|
1999-03-01 19:40:07 +03:00
|
|
|
_rtld_digest_phdr(phdr, phnum, entry)
|
|
|
|
const Elf_Phdr *phdr;
|
|
|
|
int phnum;
|
|
|
|
caddr_t entry;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-11-07 03:21:12 +03:00
|
|
|
Obj_Entry *obj;
|
1999-03-01 19:40:07 +03:00
|
|
|
const Elf_Phdr *phlimit = phdr + phnum;
|
|
|
|
const Elf_Phdr *ph;
|
|
|
|
int nsegs = 0;
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
obj = _rtld_obj_new();
|
1999-03-01 19:40:07 +03:00
|
|
|
for (ph = phdr; ph < phlimit; ++ph) {
|
|
|
|
switch (ph->p_type) {
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_PHDR:
|
1999-03-01 19:40:07 +03:00
|
|
|
assert((const Elf_Phdr *) ph->p_vaddr == phdr);
|
|
|
|
break;
|
|
|
|
|
1999-11-07 03:21:12 +03:00
|
|
|
case PT_INTERP:
|
|
|
|
obj->interp = (const char *) ph->p_vaddr;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_LOAD:
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(nsegs < 2);
|
|
|
|
if (nsegs == 0) { /* First load segment */
|
|
|
|
obj->vaddrbase = round_down(ph->p_vaddr);
|
|
|
|
obj->mapbase = (caddr_t) obj->vaddrbase;
|
|
|
|
obj->relocbase = obj->mapbase - obj->vaddrbase;
|
|
|
|
obj->textsize = round_up(ph->p_vaddr +
|
|
|
|
ph->p_memsz) - obj->vaddrbase;
|
|
|
|
} else { /* Last load segment */
|
|
|
|
obj->mapsize = round_up(ph->p_vaddr +
|
|
|
|
ph->p_memsz) - obj->vaddrbase;
|
|
|
|
}
|
|
|
|
++nsegs;
|
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_DYNAMIC:
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->dynamic = (Elf_Dyn *) ph->p_vaddr;
|
|
|
|
break;
|
|
|
|
}
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
assert(nsegs == 2);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
obj->entry = entry;
|
|
|
|
return obj;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|