shithub: scc

Download patch

ref: 26cb2d03bbed1ee32a387d34a99fbd224f1719a5
parent: e26a37a4c28c2427015a2972a34787c38103736a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Sep 3 11:06:12 EDT 2018

[lib/c] Add longjmp() and setjmp() for amd64

diff: cannot open b/lib/c/target/amd64-sysv//null: file does not exist: 'b/lib/c/target/amd64-sysv//null'
--- a/lib/c/target/amd64-sysv-linux/Makefile
+++ b/lib/c/target/amd64-sysv-linux/Makefile
@@ -6,11 +6,12 @@
 SYS        = linux
 ARCH       = amd64
 ABI        = sysv
-SYSOBJ     = raise.o signal.o
 SYSERRTBL  = ../posix/linux.e
 MORECFLAGS = -std=c99 -g -static -nostdinc
+SYSOBJ     = raise.o signal.o
 
 include syscall.mk
+include ../amd64-sysv/objlst.mk
 include ../script/objlst.mk
 include ../posix/objlst.mk
 include ../script/common.mk
--- a/lib/c/target/amd64-sysv-netbsd/Makefile
+++ b/lib/c/target/amd64-sysv-netbsd/Makefile
@@ -6,11 +6,12 @@
 SYS        = netbsd
 ARCH       = amd64
 ABI        = sysv
-SYSOBJ     = raise.o signal.o _sigaction.o _sigaction2.o _setcontext.o
 SYSERRTBL  = ../posix/netbsd.e
 MORECFLAGS = -std=c99 -g -static -nostdinc
+SYSOBJ     = raise.o signal.o _sigaction.o _sigaction2.o _setcontext.o
 
 include syscall.mk
+include ../amd64-sysv/objlst.mk
 include ../script/objlst.mk
 include ../posix/objlst.mk
 include ../script/common.mk
--- a/lib/c/target/amd64-sysv-openbsd/Makefile
+++ b/lib/c/target/amd64-sysv-openbsd/Makefile
@@ -6,11 +6,12 @@
 SYS        = openbsd
 ARCH       = amd64
 ABI        = sysv
-SYSOBJ     = raise.o signal.o _sigaction.o
 SYSERRTBL  = ../posix/netbsd.e
 MORECFLAGS = -std=c99 -g -static -nostdinc -fno-stack-protector --freestanding
+SYSOBJ     = raise.o signal.o _sigaction.o
 
 include syscall.mk
+include ../amd64-sysv/objlst.mk
 include ../script/objlst.mk
 include ../posix/objlst.mk
 include ../script/common.mk
--- /dev/null
+++ b/lib/c/target/amd64-sysv/longjmp.s
@@ -1,0 +1,20 @@
+# Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license
+	.file	"longjmp"
+	.global	longjmp
+
+longjmp:
+	mov	%rsi,%rax      # val will be longjmp return
+	test	%rax,%rax
+	jnz	1f
+	inc	%rax           # if val==0, val=1 per longjmp semantics
+1:
+	mov	(%rdi),%rbx    # rdi is the jmp_buf, restore regs from it
+	mov	8(%rdi),%rbp
+	mov	16(%rdi),%r12
+	mov	24(%rdi),%r13
+	mov	32(%rdi),%r14
+	mov	40(%rdi),%r15
+	mov	48(%rdi),%rdx  # this ends up being the stack pointer
+	mov	%rdx,%rsp
+	mov	56(%rdi),%rdx  # this is the instruction pointer
+	jmp	*%rdx          # goto saved address without altering rsp
--- /dev/null
+++ b/lib/c/target/amd64-sysv/objlst.mk
@@ -1,0 +1,7 @@
+ARCHOBJ = setjmp.o longjmp.o
+
+setjmp.o: ../amd64-sysv/setjmp.s
+	$(AS) $(ASFLAGS) -o $@ ../amd64-sysv/setjmp.s
+
+longjmp.o: ../amd64-sysv/longjmp.s
+	$(AS) $(ASFLAGS) -o $@ ../amd64-sysv/longjmp.s
--- /dev/null
+++ b/lib/c/target/amd64-sysv/setjmp.s
@@ -1,0 +1,17 @@
+/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */
+
+	.file	"setjmp.s"
+	.global	setjmp
+setjmp:
+	mov	%rbx,(%rdi)     # rdi is jmp_buf, move registers onto it
+	mov	%rbp,8(%rdi)
+	mov	%r12,16(%rdi)
+	mov	%r13,24(%rdi)
+	mov	%r14,32(%rdi)
+	mov	%r15,40(%rdi)
+	lea	8(%rsp),%rdx    # this is our rsp WITHOUT current ret addr
+	mov	%rdx,48(%rdi)
+	mov	(%rsp),%rdx     # save return addr ptr for new rip
+	mov	%rdx,56(%rdi)
+	xor	%rax,%rax       # always return 0
+	ret
--- a/lib/c/target/script/common.mk
+++ b/lib/c/target/script/common.mk
@@ -6,7 +6,7 @@
            -I$(INCDIR)/bits/$(SYS) \
            -I.
 SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h
-OBJ      = $(LIBOBJ) $(SYSOBJ) $(SYSCALL)
+OBJ      = $(LIBOBJ) $(SYSOBJ) $(SYSCALL) $(ARCHOBJ)
 
 SCC_CFLAGS = $(MORECFLAGS) $(INCLUDE)
 
--- /dev/null
+++ b/rootdir/include/scc/bits/amd64-sysv/arch/setjmp.h
@@ -1,0 +1,1 @@
+typedef unsigned long long jmp_buf[8];
--- /dev/null
+++ b/rootdir/include/scc/setjmp.h
@@ -1,0 +1,11 @@
+#ifndef _SETJMP_H
+#define _SETJMP_H
+
+#include <arch/setjmp.h>
+
+extern int setjmp(jmp_buf env);
+extern void longjmp(jmp_buf env, int val);
+
+#define setjmp setjmp
+
+#endif