From b1779fe77e90b5b5fb89c1959918b013eef06b33 Mon Sep 17 00:00:00 2001
From: Roberto Oliveira <robertoguimaraes8@gmail.com>
Date: Thu, 24 Aug 2017 20:49:50 +0000
Subject: [PATCH] main/musl: backport ppc64 fix for setjmp/longjmp handling of
 TOC pointer

---
 ...tjmp-longjmp-handling-of-TOC-pointer.patch | 178 ++++++++++++++++++
 main/musl/APKBUILD                            |   4 +-
 2 files changed, 181 insertions(+), 1 deletion(-)
 create mode 100644 main/musl/0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch

diff --git a/main/musl/0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch b/main/musl/0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch
new file mode 100644
index 000000000000..37d188b77cda
--- /dev/null
+++ b/main/musl/0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch
@@ -0,0 +1,178 @@
+From e31c8c2d796e8a9596503f079dc567c45f93c2ae Mon Sep 17 00:00:00 2001
+From: Bobby Bingham <koorogi@koorogi.info>
+Date: Fri, 4 Aug 2017 00:12:32 -0500
+Subject: [PATCH] ppc64: fix setjmp/longjmp handling of TOC pointer
+
+The TOC pointer is constant within a single dso, but needs to be saved
+and restored around cross-dso calls.  The PLT stub saves it to the
+caller's stack frame, and the linker adds code to the caller to restore
+it.
+
+With a local call, as within a single dso or with static linking, this
+doesn't happen and the TOC pointer is always in r2.  Therefore,
+setjmp/longjmp need to save/restore the TOC pointer from/to different
+locations depending on whether the call to setjmp was a local or non-local
+call.
+
+It is always safe for longjmp to restore to both r2 and the caller's stack.
+If the call to setjmp was local, and only r2 matters and the stack location
+will be ignored, but is required by the ABI to be reserved for the TOC
+pointer.  If the call was non-local, then only the stack location matters,
+and whatever is restored into r2 will be clobbered anyway when the caller
+reloads r2 from the stack.
+
+A little extra care is required for sigsetjmp, because it uses setjmp
+internally.  After the second return from this setjmp call, r2 will contain
+the caller's TOC pointer instead of libc's TOC pointer.  We need to save
+and restore the correct libc pointer before we can tail call to
+__sigsetjmp_tail.
+---
+ src/setjmp/powerpc64/longjmp.s   | 14 +++++++++-----
+ src/setjmp/powerpc64/setjmp.s    | 21 ++++++++++++++++-----
+ src/signal/powerpc64/sigsetjmp.s | 21 ++++++++++++++-------
+ 3 files changed, 39 insertions(+), 17 deletions(-)
+
+diff --git a/src/setjmp/powerpc64/longjmp.s b/src/setjmp/powerpc64/longjmp.s
+index 7f241c2d..81d45ff6 100644
+--- a/src/setjmp/powerpc64/longjmp.s
++++ b/src/setjmp/powerpc64/longjmp.s
+@@ -10,10 +10,14 @@ longjmp:
+ 	# 1) restore cr
+ 	ld   0,  1*8(3)
+ 	mtcr 0
+-	# 2) restore r1-r2 (SP and TOC)
++	# 2) restore SP
+ 	ld   1,  2*8(3)
++	# 3) restore TOC into both r2 and the caller's stack.
++	#    Which location is required depends on whether setjmp was called
++	#    locally or non-locally, but it's always safe to restore to both.
+ 	ld   2,  3*8(3)
+-	# 3) restore r14-r31
++	std  2,   24(1)
++	# 4) restore r14-r31
+ 	ld  14,  4*8(3)
+ 	ld  15,  5*8(3)
+ 	ld  16,  6*8(3)
+@@ -32,7 +36,7 @@ longjmp:
+ 	ld  29, 19*8(3)
+ 	ld  30, 20*8(3)
+ 	ld  31, 21*8(3)
+-	# 4) restore floating point registers f14-f31
++	# 5) restore floating point registers f14-f31
+ 	lfd 14, 22*8(3)
+ 	lfd 15, 23*8(3)
+ 	lfd 16, 24*8(3)
+@@ -52,7 +56,7 @@ longjmp:
+ 	lfd 30, 38*8(3)
+ 	lfd 31, 39*8(3)
+ 
+-	# 5) restore vector registers v20-v31
++	# 6) restore vector registers v20-v31
+ 	addi 3, 3, 40*8
+ 	lvx 20, 0, 3 ; addi 3, 3, 16
+ 	lvx 21, 0, 3 ; addi 3, 3, 16
+@@ -67,7 +71,7 @@ longjmp:
+ 	lvx 30, 0, 3 ; addi 3, 3, 16
+ 	lvx 31, 0, 3
+ 
+-	# 6) return r4 ? r4 : 1
++	# 7) return r4 ? r4 : 1
+ 	mr    3,   4
+ 	cmpwi cr7, 4, 0
+ 	bne   cr7, 1f
+diff --git a/src/setjmp/powerpc64/setjmp.s b/src/setjmp/powerpc64/setjmp.s
+index d16d4bae..37683fda 100644
+--- a/src/setjmp/powerpc64/setjmp.s
++++ b/src/setjmp/powerpc64/setjmp.s
+@@ -1,24 +1,35 @@
+-	.global ___setjmp
+-	.hidden ___setjmp
+ 	.global __setjmp
+ 	.global _setjmp
+ 	.global setjmp
+ 	.type   __setjmp,@function
+ 	.type   _setjmp,@function
+ 	.type   setjmp,@function
+-___setjmp:
+ __setjmp:
+ _setjmp:
+ setjmp:
++	ld 5, 24(1)   # load from the TOC slot in the caller's stack frame
++	b __setjmp_toc
++
++	.localentry __setjmp,.-__setjmp
++	.localentry _setjmp,.-_setjmp
++	.localentry setjmp,.-setjmp
++	mr 5, 2
++
++	.global __setjmp_toc
++	.hidden __setjmp_toc
++	# same as normal setjmp, except TOC pointer to save is provided in r5.
++	# r4 would normally be the 2nd parameter, but we're using r5 to simplify calling from sigsetjmp.
++	# solves the problem of knowing whether to save the TOC pointer from r2 or the caller's stack frame.
++__setjmp_toc:
+ 	# 0) store IP into 0, then into the jmpbuf pointed to by r3 (first arg)
+ 	mflr  0
+ 	std   0,  0*8(3)
+ 	# 1) store cr
+ 	mfcr  0
+ 	std   0,  1*8(3)
+-	# 2) store r1-r2 (SP and TOC)
++	# 2) store SP and TOC
+ 	std   1,  2*8(3)
+-	std   2,  3*8(3)
++	std   5,  3*8(3)
+ 	# 3) store r14-31
+ 	std  14,  4*8(3)
+ 	std  15,  5*8(3)
+diff --git a/src/signal/powerpc64/sigsetjmp.s b/src/signal/powerpc64/sigsetjmp.s
+index 52ac1d03..410c2831 100644
+--- a/src/signal/powerpc64/sigsetjmp.s
++++ b/src/signal/powerpc64/sigsetjmp.s
+@@ -2,29 +2,36 @@
+ 	.global __sigsetjmp
+ 	.type sigsetjmp,%function
+ 	.type __sigsetjmp,%function
+-	.hidden ___setjmp
++	.hidden __setjmp_toc
+ sigsetjmp:
+ __sigsetjmp:
+ 	addis 2, 12, .TOC.-__sigsetjmp@ha
+ 	addi  2,  2, .TOC.-__sigsetjmp@l
++	ld    5, 24(1)   # load from the TOC slot in the caller's stack frame
++	b     1f
++
+ 	.localentry sigsetjmp,.-sigsetjmp
+ 	.localentry __sigsetjmp,.-__sigsetjmp
++	mr    5,  2
+ 
++1:
+ 	cmpwi cr7, 4, 0
+-	beq-  cr7, ___setjmp
++	beq-  cr7, __setjmp_toc
+ 
+-	mflr  5
+-	std   5, 512(3)
+-	std  16, 512+8+8(3)
++	mflr  6
++	std   6, 512(3)
++	std   2, 512+16(3)
++	std  16, 512+24(3)
+ 	mr   16, 3
+ 
+-	bl ___setjmp
++	bl __setjmp_toc
+ 
+ 	mr   4,  3
+ 	mr   3, 16
+ 	ld   5, 512(3)
+ 	mtlr 5
+-	ld  16, 512+8+8(3)
++	ld   2, 512+16(3)
++	ld  16, 512+24(3)
+ 
+ .hidden __sigsetjmp_tail
+ 	b __sigsetjmp_tail
+-- 
+2.13.1
+
diff --git a/main/musl/APKBUILD b/main/musl/APKBUILD
index 5891546e85db..e96032f2072e 100644
--- a/main/musl/APKBUILD
+++ b/main/musl/APKBUILD
@@ -2,7 +2,7 @@
 # Maintainer: Timo Teräs <timo.teras@iki.fi>
 pkgname=musl
 pkgver=1.1.16
-pkgrel=17
+pkgrel=18
 pkgdesc="the musl c library (libc) implementation"
 url="http://www.musl-libc.org/"
 arch="all"
@@ -74,6 +74,7 @@ source="http://www.musl-libc.org/releases/musl-$pkgver.tar.gz
 	0055-reapply-va_arg-hacks-removal-to-wprintf.patch
 	0056-fix-undefined-behavior-in-free.patch
 	0057-fix-missing-volatile-qualifier-on-lock-in-__get_loca.patch
+	0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch
 	1000-implement-strftime-GNU-extension-padding-specifiers-.patch
 	2000-pthread-internals-increase-DEFAULT_GUARD_SIZE-to-2-p.patch
 	2001-thread-do-not-attempt-to-join-detached-threads-in-pt.patch
@@ -257,6 +258,7 @@ a42d23a218683eaf5b2bf8d7badbc8e0d146b4a4ac06c9f71cd516071b22e3b0055239912ed02bc1
 ea68e0c88430b65b5a61e4cbc6e6f477b383d34de89f21d59da50a05912f11a07b55de48b75cf4de1b278b8b25afacbc105ab4748525f2c91b6219364f453f09  0055-reapply-va_arg-hacks-removal-to-wprintf.patch
 dde4bb6c877d4fdf976e3ffea5d0a4a48f365708c488ceeaa4dcc29296820517aebbfa3b0527d74ddb64bf6cdbac04624ba9043b884ac4cd770a848f4d0e1f88  0056-fix-undefined-behavior-in-free.patch
 6e0a65d4023b4d2b0a971f1dbb5017fe7aedf7c663c0f9971841a4739758826c323cd0856a1591cfd874df35e8b96f1248eda029a9cd56987c36178a32b1f0ee  0057-fix-missing-volatile-qualifier-on-lock-in-__get_loca.patch
+3fd640b606279eec9ee7551ca39903d3a9a91f30e5a78dbcc0e0a59fd7edec25dcafd24f50dc0f1065209b402c3f12720ed0180b49ff641dbd54bd83989f1dc9  0058-ppc64-fix-setjmp-longjmp-handling-of-TOC-pointer.patch
 7e4c703e57a3564cd3ee1d5334b806cbe654355179ba55d4d25361dfc555eb4a7d081d80d64fdaff8476949afd04558d278b124d1fb108080beaa5ba2f8ce2b9  1000-implement-strftime-GNU-extension-padding-specifiers-.patch
 2c8e1dde1834238097b2ee8a7bfb53471a0d9cff4a5e38b55f048b567deff1cdd47c170d0578a67b1a039f95a6c5fbb8cff369c75b6a3e4d7ed171e8e86ebb8c  2000-pthread-internals-increase-DEFAULT_GUARD_SIZE-to-2-p.patch
 76330dfff60b2a8703ddc38f378995334ab0fa56e31e499937a4b4dfd8ff4a0bf1f8108174e8f863810de5cc28ff4c50656b886ee468605072bc55310a077624  2001-thread-do-not-attempt-to-join-detached-threads-in-pt.patch
-- 
GitLab