diff options
| author | matt <matt@NetBSD.org> | 2014-09-19 17:36:24 +0000 |
|---|---|---|
| committer | matt <matt@NetBSD.org> | 2014-09-19 17:36:24 +0000 |
| commit | 25195b1e580593d7db7375fb8a883f3cb9df3cce (patch) | |
| tree | ebf58bf7ac2dda20ebb4d45153ec831dc590c76d /libexec | |
| parent | 2a9c01cfd73c186ddd2af4732708c88047ffdc1a (diff) | |
New files for Userland support of UCB RISC-V (both 32-bit and 64-bit)
Diffstat (limited to 'libexec')
| -rw-r--r-- | libexec/ld.elf_so/arch/riscv/Makefile.inc | 15 | ||||
| -rw-r--r-- | libexec/ld.elf_so/arch/riscv/mdreloc.c | 370 | ||||
| -rw-r--r-- | libexec/ld.elf_so/arch/riscv/rtld_start.S | 114 |
3 files changed, 499 insertions, 0 deletions
diff --git a/libexec/ld.elf_so/arch/riscv/Makefile.inc b/libexec/ld.elf_so/arch/riscv/Makefile.inc new file mode 100644 index 00000000000..74064faecd1 --- /dev/null +++ b/libexec/ld.elf_so/arch/riscv/Makefile.inc @@ -0,0 +1,15 @@ +# $NetBSD: Makefile.inc,v 1.1 2014/09/19 17:36:25 matt Exp $ + +SRCS+= rtld_start.S mdreloc.c + +# XXX Should not be in CPPFLAGS! +CPPFLAGS+= -fpic + +.if ${LDELFSO_MACHINE_ARCH} == "riscv64" +CPPFLAGS+= -DELFSIZE=64 +.else +CPPFLAGS+= -DELFSIZE=32 +.endif +CPPFLAGS+= -DHAVE_INITFINI_ARRAY + +LDFLAGS+= -Wl,-e,_rtld_start diff --git a/libexec/ld.elf_so/arch/riscv/mdreloc.c b/libexec/ld.elf_so/arch/riscv/mdreloc.c new file mode 100644 index 00000000000..559905aa0e0 --- /dev/null +++ b/libexec/ld.elf_so/arch/riscv/mdreloc.c @@ -0,0 +1,370 @@ +/* $NetBSD: mdreloc.c,v 1.1 2014/09/19 17:36:25 matt Exp $ */ + +/*- + * Copyright (c) 2014 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas of 3am Software Foundry. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``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 FOUNDATION OR CONTRIBUTORS + * 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 <sys/cdefs.h> +#ifndef lint +__RCSID("$NetBSD: mdreloc.c,v 1.1 2014/09/19 17:36:25 matt Exp $"); +#endif /* not lint */ + +#include <sys/types.h> +#include <sys/endian.h> +#include <sys/tls.h> + +#include <stdlib.h> +#include <string.h> + +#include "debug.h" +#include "rtld.h" + +void _rtld_bind_start(void); +void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); +void *_rtld_bind(const Obj_Entry *, Elf_Word); + +#if ELFSIZE == 64 +#define Elf_Sxword Elf64_Sxword +#else +#define Elf_Sxword Elf32_Sword +#endif + +void +_rtld_setup_pltgot(const Obj_Entry *obj) +{ + obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start; + obj->pltgot[1] = (Elf_Addr) obj; +} + +void +_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) +{ + const Elf_Rel *rel = 0, *rellim; + Elf_Addr relsz = 0; + Elf_Sxword *where; + const Elf_Sym *symtab = NULL, *sym; + Elf_Addr *got = NULL; + Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0; + size_t i; + + for (; dynp->d_tag != DT_NULL; dynp++) { + switch (dynp->d_tag) { + case DT_REL: + rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr); + break; + case DT_RELSZ: + relsz = dynp->d_un.d_val; + break; + case DT_SYMTAB: + symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr); + break; + case DT_PLTGOT: + got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr); + break; + case DT_RISCV_LOCAL_GOTNO: + local_gotno = dynp->d_un.d_val; + break; + case DT_RISCV_SYMTABNO: + symtabno = dynp->d_un.d_val; + break; + case DT_RISCV_GOTSYM: + gotsym = dynp->d_un.d_val; + break; + } + } + + i = (got[1] & 0x80000000) ? 2 : 1; + /* Relocate the local GOT entries */ + got += i; + for (; i < local_gotno; i++) + *got++ += relocbase; + sym = symtab + gotsym; + /* Now do the global GOT entries */ + for (i = gotsym; i < symtabno; i++) { + *got = sym->st_value + relocbase; + ++sym; + ++got; + } + + rellim = (const Elf_Rel *)((uintptr_t)rel + relsz); + for (; rel < rellim; rel++) { + Elf_Word r_symndx, r_type; + + where = (Elf_Sxword *)(relocbase + rel->r_offset); + + r_symndx = ELF_R_SYM(rel->r_info); + r_type = ELF_R_TYPE(rel->r_info); + + switch (r_type & 0xff) { + case R_TYPE(REL32): { + Elf_Sxword old = *where; + Elf_Sxword val = old; +#if ELFSIZE == 64 + assert(r_type == R_TYPE(REL32) + || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8))); +#endif + assert(r_symndx < gotsym); + sym = symtab + r_symndx; + assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL); + val += relocbase; + *(Elf_Sword *)where = val; + rdbg(("REL32/L(%p) %p -> %p in <self>", + where, (void *)old, (void *)val)); + break; + } + + case R_TYPE(NONE): + break; + + default: + abort(); + } + } +} + +int +_rtld_relocate_nonplt_objects(Obj_Entry *obj) +{ + const Elf_Rel *rel; + Elf_Addr *got = obj->pltgot; + const Elf_Sym *sym, *def; + const Obj_Entry *defobj; + Elf_Word i; + + i = 2; + /* Relocate the local GOT entries */ + got += i; + for (; i < obj->local_gotno; i++) + *got++ += (Elf_Addr)obj->relocbase; + + sym = obj->symtab + obj->gotsym; + /* Now do the global GOT entries */ + for (i = obj->gotsym; i < obj->symtabno; i++) { + rdbg((" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym, + sym->st_name + obj->strtab, (u_long) *got)); + + if (ELF_ST_TYPE(sym->st_info) == STT_FUNC && + sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) { + /* + * If there are non-PLT references to the function, + * st_value should be 0, forcing us to resolve the + * address immediately. + * + * XXX DANGER WILL ROBINSON! + * The linker is not outputting PLT slots for calls to + * functions that are defined in the same shared + * library. This is a bug, because it can screw up + * link ordering rules if the symbol is defined in + * more than one module. For now, if there is a + * definition, we fail the test above and force a full + * symbol lookup. This means that all intra-module + * calls are bound immediately. - mycroft, 2003/09/24 + */ + *got = sym->st_value + (Elf_Addr)obj->relocbase; + } else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) { + /* Symbols with index SHN_ABS are not relocated. */ + if (sym->st_shndx != SHN_ABS) + *got = sym->st_value + + (Elf_Addr)obj->relocbase; + } else { + def = _rtld_find_symdef(i, obj, &defobj, false); + if (def == NULL) + return -1; + *got = def->st_value + (Elf_Addr)defobj->relocbase; + } + + rdbg((" --> now %lx", (u_long) *got)); + ++sym; + ++got; + } + + got = obj->pltgot; + for (rel = obj->rel; rel < obj->rellim; rel++) { + Elf_Addr * const where = + (Elf_Addr *)(obj->relocbase + rel->r_offset); + const Elf_Word r_symndx = ELF_R_SYM(rel->r_info); + const Elf_Word r_type = ELF_R_TYPE(rel->r_info); + + switch (r_type) { + case R_TYPE(NONE): + break; + + case R_TYPE(REL32): { + /* 32-bit PC-relative reference */ + Elf_Sxword old = *where; + Elf_Sxword val = old; + + def = obj->symtab + r_symndx; + + if (r_symndx < obj->gotsym) { + val += (Elf_Addr)obj->relocbase; + + rdbg(("REL32/L(%p) %p -> %p (%s) in %s", + where, (void *)old, (void *)val, + obj->strtab + def->st_name, obj->path)); + } else { + val += got[obj->local_gotno + r_symndx - obj->gotsym]; + rdbg(("REL32/G(%p) %p --> %p (%s) in %s", + where, (void *)old, (void *)val, + obj->strtab + def->st_name, + obj->path)); + } + *where = val; + break; + } + +#if ELFSIZE == 64 + case R_TYPE(TLS_DTPMOD64): +#else + case R_TYPE(TLS_DTPMOD32): +#endif + { + Elf_Addr old = *where; + Elf_Addr val = old; + + def = _rtld_find_symdef(r_symndx, obj, &defobj, false); + if (def == NULL) + return -1; + + val += (Elf_Addr)defobj->tlsindex; + + *where = val; + rdbg(("DTPMOD %s in %s --> %p in %s", + obj->strtab + obj->symtab[r_symndx].st_name, + obj->path, (void *)old, defobj->path)); + break; + } + +#if ELFSIZE == 64 + case R_TYPE(TLS_DTPREL64): +#else + case R_TYPE(TLS_DTPREL32): +#endif + { + Elf_Addr old = *where; + Elf_Addr val = old; + + def = _rtld_find_symdef(r_symndx, obj, &defobj, false); + if (def == NULL) + return -1; + + if (!defobj->tls_done && _rtld_tls_offset_allocate(obj)) + return -1; + + val += (Elf_Addr)def->st_value - TLS_DTV_OFFSET; + *(Elf_Word *) where = val; + + rdbg(("DTPREL %s in %s --> %p in %s", + obj->strtab + obj->symtab[r_symndx].st_name, + obj->path, (void *)old, defobj->path)); + break; + } + + default: + rdbg(("sym = %lu, type = %lu, offset = %p, " + "contents = %p, symbol = %s", + (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info), + (void *)rel->r_offset, + (void *)load_ptr(where, sizeof(Elf_Sword)), + obj->strtab + obj->symtab[r_symndx].st_name)); + _rtld_error("%s: Unsupported relocation type %ld " + "in non-PLT relocations", + obj->path, (u_long) ELF_R_TYPE(rel->r_info)); + return -1; + } + } + + return 0; +} + +int +_rtld_relocate_plt_lazy(const Obj_Entry *obj) +{ + /* PLT fixups were done above in the GOT relocation. */ + return 0; +} + +static int +_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel, + Elf_Addr *tp) +{ + const Obj_Entry *defobj; + Elf_Addr new_value; + + assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT)); + + const Elf_Sym *def = _rtld_find_plt_symdef(ELF_R_SYM(rel->r_info), + obj, &defobj, tp != NULL); + if (__predict_false(def == NULL)) + return -1; + if (__predict_false(def == &_rtld_sym_zero)) + return -1; + + if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) { + if (tp == NULL) + return 0; + new_value = _rtld_resolve_ifunc(defobj, def); + } else { + new_value = (Elf_Addr)(defobj->relocbase + def->st_value); + } + rdbg(("bind now/fixup in %s --> new=%p", + defobj->strtab + def->st_name, (void *)new_value)); + *(Elf_Addr *)(obj->relocbase + rel->r_offset) = new_value; + + if (tp) + *tp = new_value; + return 0; +} + +void * +_rtld_bind(const Obj_Entry *obj, Elf_Word reloff) +{ + const Elf_Rel *pltrel = (const Elf_Rel *)(obj->pltrel + reloff); + Elf_Addr new_value; + int err; + + _rtld_shared_enter(); + err = _rtld_relocate_plt_object(obj, pltrel, &new_value); + if (err) + _rtld_die(); + _rtld_shared_exit(); + + return (caddr_t)new_value; +} + +int +_rtld_relocate_plt_objects(const Obj_Entry *obj) +{ + + for (const Elf_Rel *rel = obj->pltrel; rel < obj->pltrellim; rel++) { + if (_rtld_relocate_plt_object(obj, rel, NULL) < 0) + return -1; + } + + return 0; +} diff --git a/libexec/ld.elf_so/arch/riscv/rtld_start.S b/libexec/ld.elf_so/arch/riscv/rtld_start.S new file mode 100644 index 00000000000..9760cdb459d --- /dev/null +++ b/libexec/ld.elf_so/arch/riscv/rtld_start.S @@ -0,0 +1,114 @@ +/* $NetBSD: rtld_start.S,v 1.1 2014/09/19 17:36:25 matt Exp $ */ + +/*- + * Copyright (c) 2014 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Matt Thomas of 3am Software Foundry. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``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 FOUNDATION OR CONTRIBUTORS + * 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> + + .globl _C_LABEL(_rtld_relocate_nonplt_self) + .globl _C_LABEL(_rtld) + +/* + * void + * ___start(void (*cleanup)(void), + * const Obj_Entry *obj, + * struct ps_strings *ps_strings) + */ +ENTRY(_rtld_start) + move s0, sp # save stack pointer + addi sp, sp, -4*__SIZEOF_POINTER__ + # adjust stack pointer + # -> 2*PTR_SIZE(sp) for atexit + # -> 3*PTR_SIZE(sp) for obj_main + move s1, a2 # save ps_strings pointer + + auipc gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_) + PTR_L t0, %pcrel_lo(_GLOBAL_OFFSET_TABLE_)(gp) # &_DYNAMIC + auipc a0, %pcrel_hi(_DYNAMIC) + addi a0, a0, %pcrel_lo(_DYNAMIC) + sub s2, a0, t0 # save for _rtld + move a1, s2 + call _C_LABEL(_rtld_relocate_nonplt_self) + + move a1, s2 # relocbase + addi a0, sp, 2*__SIZEOF_POINTER__ # sp + call _C_LABEL(_rtld) # v0 = _rtld(sp, relocbase) + + PTR_L a0, 2*__SIZEOF_POINTER__(sp) # cleanup function + PTR_L a1, 3*__SIZEOF_POINTER__(sp) # obj_main entry + move a2, s1 # restore ps_strings + move sp, s0 # readjust stack + move s0, zero # break stack chain + jr v0 # _start(cleanup, obj_main, ps_strings); +END(_rtld_start) + +#define XCALLFRAME_SIZ (12*SZREG) +#define XCALLFRAME_RA (8*SZREG) +#define XCALLFRAME_A7 (7*SZREG) +#define XCALLFRAME_A6 (6*SZREG) +#define XCALLFRAME_A5 (5*SZREG) +#define XCALLFRAME_A4 (4*SZREG) +#define XCALLFRAME_A3 (3*SZREG) +#define XCALLFRAME_A2 (2*SZREG) +#define XCALLFRAME_A1 (1*SZREG) +#define XCALLFRAME_A0 (0*SZREG) + +/* + * t0 = obj pointer + * t1 = reloc offset + */ +ENTRY_NP(_rtld_bind_start) + addi sp, sp, -XCALLFRAME_SIZ // save arguments on stack + REG_S a0, XCALLFRAME_A0(sp) + REG_S a1, XCALLFRAME_A1(sp) + REG_S a2, XCALLFRAME_A2(sp) + REG_S a3, XCALLFRAME_A3(sp) + REG_S a4, XCALLFRAME_A4(sp) + REG_S a5, XCALLFRAME_A5(sp) + REG_S a6, XCALLFRAME_A6(sp) + REG_S a7, XCALLFRAME_A7(sp) + REG_S ra, XCALLFRAME_RA(sp) + + move a0, t0 /* object from got.plt[1] */ + move a1, t1 /* reloc offset */ + + call _C_LABEL(_rtld_bind) + + REG_L a0, XCALLFRAME_A0(sp) + REG_L a1, XCALLFRAME_A1(sp) + REG_L a2, XCALLFRAME_A2(sp) + REG_L a3, XCALLFRAME_A3(sp) + REG_L a4, XCALLFRAME_A4(sp) + REG_L a5, XCALLFRAME_A5(sp) + REG_L a6, XCALLFRAME_A6(sp) + REG_L a7, XCALLFRAME_A7(sp) + REG_L ra, XCALLFRAME_RA(sp) + addi sp, sp, XCALLFRAME_SIZ + jr v0 +END(_rtld_bind_start) |
