Add support for shared libraries/dynamically linked binaries on sh5.

With the latest SuperH toolchain, NetBSD/evbsh5 can now
run with a fully dynamic userland (modulo a few remaining
gremlins affecting a couple of binaries).
This commit is contained in:
scw 2003-03-25 13:11:53 +00:00
parent b95a7206b1
commit d174ad2eab
4 changed files with 331 additions and 1 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.60 2003/02/26 06:03:40 uwe Exp $
# $NetBSD: Makefile,v 1.61 2003/03/25 13:11:53 scw Exp $
.include <bsd.own.mk> # for OBJECT_FMT definition
.include <bsd.shlib.mk> # for SHLINKINSTALLDIR definition
@ -14,6 +14,7 @@ M= ${.CURDIR}/arch/${ARCHSUBDIR}
(${MACHINE_ARCH} == "mipsel") || (${MACHINE_ARCH} == "mipseb") || \
(${MACHINE_ARCH} == "powerpc") || \
(${MACHINE_ARCH} == "sh3eb") || (${MACHINE_ARCH} == "sh3el") || \
(${MACHINE_ARCH} == "sh5eb") || (${MACHINE_ARCH} == "sh5el") || \
(${MACHINE_ARCH} == "sparc") || \
(${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "x86_64") || \

View File

@ -0,0 +1,10 @@
# $NetBSD: Makefile.inc,v 1.1 2003/03/25 13:11:54 scw Exp $
SRCS+= rtld_start.S mdreloc.c
# XXX Should not be in CPPFLAGS!
CPPFLAGS+= -fpic
CPPFLAGS+= -DELFSIZE=32
LDFLAGS+= -Bshareable -Bsymbolic -e .rtld_start

View File

@ -0,0 +1,168 @@
/* $NetBSD: mdreloc.c,v 1.1 2003/03/25 13:11:54 scw Exp $ */
#include <sys/types.h>
#include <sys/stat.h>
#include "debug.h"
#include "rtld.h"
/*
* Flag added to Elf_Sym->dt_other to indicate SHmedia code
*/
#define STO_SH5_ISA32 (1 << 2)
void _rtld_bind_start(void);
void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
void
_rtld_setup_pltgot(const Obj_Entry *obj)
{
obj->pltgot[1] = (Elf_Addr) obj;
obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
}
void
_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
{
const Elf_Rela *rela = 0, *relalim;
Elf_Addr relasz = 0;
Elf_Addr *where;
for (; dynp->d_tag != DT_NULL; dynp++) {
switch (dynp->d_tag) {
case DT_RELA:
rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
break;
case DT_RELASZ:
relasz = dynp->d_un.d_val;
break;
}
}
relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
for (; rela < relalim; rela++) {
where = (Elf_Addr *)(relocbase + rela->r_offset);
*where = (Elf_Addr)(relocbase + rela->r_addend);
}
}
int
_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
{
const Elf_Rela *rela;
for (rela = obj->rela; rela < obj->relalim; rela++) {
Elf_Addr *where;
const Elf_Sym *def;
const Obj_Entry *defobj;
Elf_Addr tmp;
unsigned long symnum;
where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
symnum = ELF_R_SYM(rela->r_info);
switch (ELF_R_TYPE(rela->r_info)) {
case R_TYPE(NONE):
break;
case R_TYPE(DIR32):
case R_TYPE(GLOB_DAT):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
rela->r_addend);
if (def->st_other & STO_SH5_ISA32)
tmp |= 1;
if (*where != tmp)
*where = tmp;
rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where, defobj->path));
break;
case R_TYPE(RELATIVE):
*where += (Elf_Addr)obj->relocbase;
rdbg(("RELATIVE in %s --> %p", obj->path,
(void *)*where));
break;
case R_TYPE(COPY):
/*
* These are deferred until all other relocations have
* been done. All we do here is make sure that the
* COPY relocation is not in a shared library. They
* are allowed only in executable files.
*/
if (obj->isdynamic) {
_rtld_error(
"%s: Unexpected R_COPY relocation in shared library",
obj->path);
return -1;
}
rdbg(("COPY (avoid in main)"));
break;
default:
rdbg(("sym = %lu, type = %lu, offset = %p, "
"addend = %p, contents = %p, symbol = %s",
symnum, (u_long)ELF_R_TYPE(rela->r_info),
(void *)rela->r_offset, (void *)rela->r_addend,
(void *)*where,
obj->strtab + obj->symtab[symnum].st_name));
_rtld_error("%s: Unsupported relocation type %ld "
"in non-PLT relocations\n",
obj->path, (u_long) ELF_R_TYPE(rela->r_info));
return -1;
}
}
return 0;
}
int
_rtld_relocate_plt_lazy(const Obj_Entry *obj)
{
const Elf_Rela *rela;
if (!obj->relocbase)
return 0;
for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
/* Just relocate the GOT slots pointing into the PLT */
*where += (Elf_Addr)obj->relocbase;
rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
}
return 0;
}
caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
{
const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
Elf_Addr new_value;
const Elf_Sym *def;
const Obj_Entry *defobj;
assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
if (def == NULL)
_rtld_die();
new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
if (def->st_other & STO_SH5_ISA32)
new_value |= 1;
rdbg(("bind now/fixup in %s --> old=%p new=%p",
defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
if (*where != new_value)
*where = new_value;
return (caddr_t)new_value;
}

View File

@ -0,0 +1,151 @@
/* $NetBSD: rtld_start.S,v 1.1 2003/03/25 13:11:54 scw Exp $ */
/*
* Copyright 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Steve C. Woodford for Wasabi Systems, Inc.
*
* 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 for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
* 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 <machine/asm.h>
#ifdef _LP64
#define PTRSZ 8
#else
#define PTRSZ 4
#endif
#define RTLD_START_FR_R2 0x00
#define RTLD_START_FR_R3 0x08
#define RTLD_START_FR_R4 0x10
#define RTLD_START_FR_R7 0x18
#define RTLD_START_FR_R28 0x20
#define RTLD_START_FR_CLEANUP 0x28
#define RTLD_START_FR_OBJ (RTLD_START_FR_CLEANUP+PTRSZ)
#define RTLD_START_FR_SIZE (RTLD_START_FR_OBJ+PTRSZ)
ASENTRY_NOPROFILE(.rtld_start)
addi r15, -RTLD_START_FR_SIZE, r15
st.q r15, RTLD_START_FR_R2, r2
st.q r15, RTLD_START_FR_R3, r3
st.q r15, RTLD_START_FR_R4, r4
st.q r15, RTLD_START_FR_R7, r7
st.q r15, RTLD_START_FR_R28, r28
pt/u datalabel _GLOBAL_OFFSET_TABLE_, tr0
gettr tr0, r0
pt/u datalabel _DYNAMIC, tr1
LDPTR r0, 0, r28
gettr tr1, r2 /* &_DYNAMIC */
sub r2, r28, r28
or r28, r63, r3 /* relocbase */
pt/l _C_LABEL(_rtld_relocate_nonplt_self), tr0
blink tr0, r18
add r28, r63, r3 /* relocbase */
addi r15, RTLD_START_FR_CLEANUP, r2
pt/l _C_LABEL(_rtld), tr0
blink tr0, r18
ptabs/l r2, tr0
ld.q r15, RTLD_START_FR_R2, r2 /* argc */
ld.q r15, RTLD_START_FR_R3, r3 /* argv */
ld.q r15, RTLD_START_FR_R4, r4 /* envp */
ld.q r15, RTLD_START_FR_R7, r7 /* pstrings */
ld.q r15, RTLD_START_FR_R28, r28
LDPTR r15, RTLD_START_FR_CLEANUP, r5 /* cleanup */
LDPTR r15, RTLD_START_FR_OBJ, r6 /* obj */
addi r15, RTLD_START_FR_SIZE, r15
blink tr0, r63
.size _ASM_LABEL(.rtld_start),.-_ASM_LABEL(.rtld_start)
#define RTLD_BIND_START_FR_R2 0x00
#define RTLD_BIND_START_FR_R3 0x08
#define RTLD_BIND_START_FR_R4 0x10
#define RTLD_BIND_START_FR_R5 0x18
#define RTLD_BIND_START_FR_R6 0x20
#define RTLD_BIND_START_FR_R7 0x28
#define RTLD_BIND_START_FR_R8 0x30
#define RTLD_BIND_START_FR_R9 0x38
#define RTLD_BIND_START_FR_DR0 0x40
#define RTLD_BIND_START_FR_DR2 0x48
#define RTLD_BIND_START_FR_DR4 0x50
#define RTLD_BIND_START_FR_DR6 0x58
#define RTLD_BIND_START_FR_DR8 0x60
#define RTLD_BIND_START_FR_DR10 0x68
#define RTLD_BIND_START_FR_SIZE 0x70
/*
* On entry:
* r12 -> _GLOBAL_OFFSET_TABLE_ + GOT_BIAS (0x8000)
* r17 -> Obj_Entry *
* r21 -> reloff
*/
ASENTRY_NOPROFILE(_rtld_bind_start)
LINK_FRAME(RTLD_BIND_START_FR_SIZE)
st.q r14, RTLD_BIND_START_FR_R2, r2
st.q r14, RTLD_BIND_START_FR_R3, r3
st.q r14, RTLD_BIND_START_FR_R4, r4
st.q r14, RTLD_BIND_START_FR_R5, r5
st.q r14, RTLD_BIND_START_FR_R6, r6
st.q r14, RTLD_BIND_START_FR_R7, r7
st.q r14, RTLD_BIND_START_FR_R8, r8
st.q r14, RTLD_BIND_START_FR_R9, r9
fst.d r14, RTLD_BIND_START_FR_DR0, dr0
fst.d r14, RTLD_BIND_START_FR_DR2, dr2
fst.d r14, RTLD_BIND_START_FR_DR4, dr4
fst.d r14, RTLD_BIND_START_FR_DR6, dr6
fst.d r14, RTLD_BIND_START_FR_DR8, dr8
fst.d r14, RTLD_BIND_START_FR_DR10, dr10
pt/l _C_LABEL(_rtld_bind), tr0
add r17, r63, r2
add r21, r63, r3
blink tr0, r18
ptabs/l r2, tr0
ld.q r14, RTLD_BIND_START_FR_R2, r2
ld.q r14, RTLD_BIND_START_FR_R3, r3
ld.q r14, RTLD_BIND_START_FR_R4, r4
ld.q r14, RTLD_BIND_START_FR_R5, r5
ld.q r14, RTLD_BIND_START_FR_R6, r6
ld.q r14, RTLD_BIND_START_FR_R7, r7
ld.q r14, RTLD_BIND_START_FR_R8, r8
ld.q r14, RTLD_BIND_START_FR_R9, r9
fld.d r14, RTLD_BIND_START_FR_DR0, dr0
fld.d r14, RTLD_BIND_START_FR_DR2, dr2
fld.d r14, RTLD_BIND_START_FR_DR4, dr4
fld.d r14, RTLD_BIND_START_FR_DR6, dr6
fld.d r14, RTLD_BIND_START_FR_DR8, dr8
fld.d r14, RTLD_BIND_START_FR_DR10, dr10
UNLINK_FRAME(RTLD_BIND_START_FR_SIZE)
blink tr0, r63
.size _ASM_LABEL(_rtld_bind_start),.-_ASM_LABEL(_rtld_bind_start)