Merge "Move test_v_helper library to top level."
diff --git a/springbok/crt0.S b/springbok/crt0.S
index f51ac30..7e3ba79 100644
--- a/springbok/crt0.S
+++ b/springbok/crt0.S
@@ -101,13 +101,13 @@
         li   a1, 0
         call __call_exitprocs
 
-        # Restore main's return value
-        mv   a0, s0
-
         # Don't clear the stack if the program returned from main
         j    1f
 
 _exit:
+        # Save _exit's return value
+        mv   s0, a0
+
         # Clear the stack
         la   sp, _stack_ptr
 1:
@@ -116,8 +116,11 @@
         ##########################################################################
         # Check the stack, and fix it if it's broken
         jal  ra, _check_stack
+
         # Save check_stack's return value
         mv   s1, a0
+
+        # Restore the application's return value
         mv   a0, s0
 
         # Print main's return value
@@ -134,6 +137,7 @@
         sw   t3, 12(sp)
         li   t4, 2 # INFO logging level
         .word 0x00A10EFB # simprint t4, sp, a0 (encoded as custom3<func3=0>)
+        addi sp, sp, 16
 
         # Was the stack corrupted?
         beq  s1, zero, _finish
@@ -143,6 +147,7 @@
         li   t1, 0x6f63206b # "k co"
         li   t2, 0x70757272 # "rrup"
         li   t3, 0x00646574 # "ted\0"
+        addi sp, sp, -16
         sw   t0, 0(sp)
         sw   t1, 4(sp)
         sw   t2, 8(sp)
@@ -150,10 +155,18 @@
         mv   a0, s1
         li   t4, 0 # ERROR logging level
         .word 0x00A10EFB # simprint t4, sp, a0 (encoded as custom3<func3=0>)
+        addi sp, sp, 16
+        mv   a0, s0
 
 _finish:
+        # Store the application's return value onto the stack
+        addi sp, sp, -8
+        sw   a0, 0(sp)
+        csrr t0, mepc
+        sw   t0, 4(sp)
+1:
         .word 0x0000307B # finish (encoded as custom3<func3=3>)
-        j _finish
+        j    1b
 
 _setup_stack_sentinels:
         #######################################