Preliminary x86-64 support. Not fully functional yet (toolchain bugs).

This commit is contained in:
fvdl 2001-06-19 01:11:03 +00:00
parent a84b13f43f
commit d1d73371a6
5 changed files with 234 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.36 2001/01/14 09:39:26 christos Exp $
# $NetBSD: Makefile,v 1.37 2001/06/19 01:11:03 fvdl Exp $
.include <bsd.own.mk> # for OBJECT_FMT definition
@ -12,6 +12,7 @@ M= ${.CURDIR}/arch/${ARCHSUBDIR}
(${MACHINE_ARCH} == "powerpc") || \
(${MACHINE_ARCH} == "sparc") || \
(${MACHINE_ARCH} == "sparc64") || \
(${MACHINE_ARCH} == "x86_64") || \
(${MACHINE_ARCH} == "vax")) && \
${OBJECT_FMT} == "ELF" && ${MKPIC} != "no"

View File

@ -0,0 +1,6 @@
# $NetBSD: Makefile.inc,v 1.1 2001/06/19 01:11:44 fvdl Exp $
SRCS+= rtld_start.S mdreloc.c
CPPFLAGS+= -fpic -DELFSIZE=64 -DRTLD_RELOCATE_SELF
LDFLAGS+= -Bshareable -Bsymbolic -e .rtld_start

View File

@ -0,0 +1,173 @@
#include <sys/types.h>
#include <sys/mman.h>
#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 <elf.h>
#include "debug.h"
#include "rtld.h"
extern Elf64_Addr _GLOBAL_OFFSET_TABLE_[];
int
_rtld_relocate_nonplt_object(Obj_Entry *obj, const Elf_RelA *rela, bool dodebug)
{
Elf64_Addr *where64;
Elf32_Addr *where32;
const Elf_Sym *def;
const Obj_Entry *defobj;
Elf64_Addr tmp64;
Elf32_Addr tmp32;
where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
where32 = (Elf32_Addr *)where64;
switch (ELF_R_TYPE(rela->r_info)) {
case R_TYPE(NONE):
break;
case R_TYPE(32): /* word32 S + A, truncate */
case R_TYPE(32S): /* word32 S + A, signed truncate */
case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, false);
if (def == NULL)
return -1;
tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
rela->r_addend);
if (*where32 != tmp32)
*where32 = tmp32;
rdbg(dodebug, ("32/32S %s in %s --> %p in %s",
defobj->strtab + def->st_name, obj->path,
(void *)(unsigned long)*where32, defobj->path));
break;
case R_TYPE(64): /* word64 S + A */
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, false);
if (def == NULL)
return -1;
tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
rela->r_addend);
if (*where64 != tmp64)
*where64 = tmp64;
rdbg(dodebug, ("64 %s in %s --> %p in %s",
defobj->strtab + def->st_name, obj->path,
(void *)*where64, defobj->path));
break;
case R_TYPE(PC32): /* word32 S + A - P */
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, false);
if (def == NULL)
return -1;
tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
rela->r_addend - (Elf64_Addr)where64);
if (*where32 != tmp32)
*where32 = tmp32;
rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
defobj->strtab + def->st_name, obj->path,
(void *)(unsigned long)*where32, defobj->path));
break;
case R_TYPE(GLOB_DAT): /* word64 S */
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, false);
if (def == NULL)
return -1;
tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
if (*where64 != tmp64)
*where64 = tmp64;
rdbg(dodebug, ("64 %s in %s --> %p in %s",
defobj->strtab + def->st_name, obj->path,
(void *)*where64, defobj->path));
break;
case R_TYPE(RELATIVE): /* word64 B + A */
tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
if (*where64 != tmp64)
*where64 = tmp64;
rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
(void *)*where64));
break;
case R_TYPE(COPY):
rdbg(dodebug, ("COPY"));
break;
default:
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, true);
rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
"addend = %p, contents = %p, symbol = %s",
(u_long)ELF_R_SYM(rela->r_info),
(u_long)ELF_R_TYPE(rela->r_info),
(void *)rela->r_offset, (void *)rela->r_addend,
(void *)*where64,
def ? defobj->strtab + def->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_object(Obj_Entry *obj, const Elf_RelA *rela, caddr_t *addrp,
bool bind_now, bool dodebug)
{
Elf32_Addr *where = (Elf32_Addr *)(obj->relocbase + rela->r_offset);
Elf32_Addr new_value;
/* Fully resolve procedure addresses now */
if (bind_now || obj->pltgot == NULL) {
const Elf_Sym *def;
const Obj_Entry *defobj;
assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
&defobj, true);
if (def == NULL)
return -1;
new_value = (Elf32_Addr)(u_long)
(defobj->relocbase + def->st_value + rela->r_addend);
rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
(int)bind_now,
defobj->strtab + def->st_name,
(void *)(u_long)*where, (void *)(u_long)new_value));
} else {
if (!obj->mainprog) {
/* Just relocate the GOT slots pointing into the PLT */
new_value = *where + (Elf32_Addr)(u_long)
obj->relocbase;
rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
(void *)(unsigned long)*where));
} else {
return 0;
}
}
/*
* Since this page is probably copy-on-write, let's not write
* it unless we really really have to.
*/
if (*where != new_value)
*where = new_value;
if (addrp != NULL)
*addrp = *(caddr_t *)(obj->relocbase + rela->r_offset) -
rela->r_addend;
return 0;
}

View File

@ -0,0 +1,50 @@
/* $NetBSD: rtld_start.S,v 1.1 2001/06/19 01:11:44 fvdl Exp $ */
#include <machine/asm.h>
.text
.align 16
.globl .rtld_start
.type .rtld_start,@function
.rtld_start:
subq $16,%rsp # make room of obj_main and exit proc
movq %rsp,%rdi # stack pointer arg to _rtld
pushq %rbx # save ps_strings
call _rtld@PLT # _rtld(sp)
popq %rbx # %rbx = ps_strings
popq %rdx # %rdx = cleanup
popq %rcx # %rcx = obj_main
jmp *%rax
.align 4
.globl _rtld_bind_start
.type _rtld_bind_start,@function
_rtld_bind_start: # (obj, reloff)
pushfq # save caller-saved registers
pushq %rax
pushq %rcx
pushq %rdx
pushq %rsi
pushq %rdi
pushq %r8
pushq %r9
pushq %r10
pushq %r11
call _rtld_bind@PLT # call the binder
movq %rax,80(%rsp) # store function in return address
popq %r11
popq %r10
popq %r9
popq %r8
popq %rdi
popq %rsi
popq %rdx
popq %rcx
popq %rax
popfq
ret

View File

@ -1,4 +1,4 @@
/* $NetBSD: reloc.c,v 1.35 2001/04/25 12:24:50 kleink Exp $ */
/* $NetBSD: reloc.c,v 1.36 2001/06/19 01:11:03 fvdl Exp $ */
/*
* Copyright 1996 John D. Polstra.
@ -147,7 +147,7 @@ _rtld_do_copy_relocations(dstobj, dodebug)
}
#ifndef __sparc__
#if !defined(__sparc__) && !defined(__x86_64__)
#if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
@ -651,7 +651,7 @@ _rtld_relocate_objects(first, bind_now, dodebug)
/* Set the special PLTGOT entries. */
if (obj->pltgot != NULL) {
#if defined(__i386__) || defined(__m68k__)
#if defined(__i386__) || defined(__m68k__) || defined(__x86_64__)
obj->pltgot[1] = (Elf_Addr) obj;
obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
#endif