2be09a
commit 387bff63dc2dccd62b09aa26dccf8cdc5f3c985c
2be09a
Author: Matheus Castanho <msc@linux.ibm.com>
2be09a
Date:   Tue Oct 26 10:44:59 2021 -0300
2be09a
2be09a
    powerpc64[le]: Fix CFI and LR save address for asm syscalls [BZ #28532]
2be09a
    
2be09a
    Syscalls based on the assembly templates are missing CFI for r31, which gets
2be09a
    clobbered when scv is used, and info for LR is inaccurate, placed in the wrong
2be09a
    LOC and not using the proper offset. LR was also being saved to the callee's
2be09a
    frame, while the ABI mandates it to be saved to the caller's frame. These are
2be09a
    fixed by this commit.
2be09a
    
2be09a
    After this change:
2be09a
    
2be09a
    $ readelf -wF libc.so.6 | grep 0004b9d4.. -A 7 && objdump --disassemble=kill libc.so.6
2be09a
    00004a48 0000000000000020 00004a4c FDE cie=00000000 pc=000000000004b9d4..000000000004ba3c
2be09a
       LOC           CFA      r31   ra
2be09a
    000000000004b9d4 r1+0     u     u
2be09a
    000000000004b9e4 r1+48    u     u
2be09a
    000000000004b9e8 r1+48    c-16  u
2be09a
    000000000004b9fc r1+48    c-16  c+16
2be09a
    000000000004ba08 r1+48    c-16
2be09a
    000000000004ba18 r1+48    u
2be09a
    000000000004ba1c r1+0     u
2be09a
    
2be09a
    libc.so.6:     file format elf64-powerpcle
2be09a
    
2be09a
    Disassembly of section .text:
2be09a
    
2be09a
    000000000004b9d4 <kill>:
2be09a
       4b9d4:       1f 00 4c 3c     addis   r2,r12,31
2be09a
       4b9d8:       2c c3 42 38     addi    r2,r2,-15572
2be09a
       4b9dc:       25 00 00 38     li      r0,37
2be09a
       4b9e0:       d1 ff 21 f8     stdu    r1,-48(r1)
2be09a
       4b9e4:       20 00 e1 fb     std     r31,32(r1)
2be09a
       4b9e8:       98 8f ed eb     ld      r31,-28776(r13)
2be09a
       4b9ec:       10 00 ff 77     andis.  r31,r31,16
2be09a
       4b9f0:       1c 00 82 41     beq     4ba0c <kill+0x38>
2be09a
       4b9f4:       a6 02 28 7d     mflr    r9
2be09a
       4b9f8:       40 00 21 f9     std     r9,64(r1)
2be09a
       4b9fc:       01 00 00 44     scv     0
2be09a
       4ba00:       40 00 21 e9     ld      r9,64(r1)
2be09a
       4ba04:       a6 03 28 7d     mtlr    r9
2be09a
       4ba08:       08 00 00 48     b       4ba10 <kill+0x3c>
2be09a
       4ba0c:       02 00 00 44     sc
2be09a
       4ba10:       00 00 bf 2e     cmpdi   cr5,r31,0
2be09a
       4ba14:       20 00 e1 eb     ld      r31,32(r1)
2be09a
       4ba18:       30 00 21 38     addi    r1,r1,48
2be09a
       4ba1c:       18 00 96 41     beq     cr5,4ba34 <kill+0x60>
2be09a
       4ba20:       01 f0 20 39     li      r9,-4095
2be09a
       4ba24:       40 48 23 7c     cmpld   r3,r9
2be09a
       4ba28:       20 00 e0 4d     bltlr+
2be09a
       4ba2c:       d0 00 63 7c     neg     r3,r3
2be09a
       4ba30:       08 00 00 48     b       4ba38 <kill+0x64>
2be09a
       4ba34:       20 00 e3 4c     bnslr+
2be09a
       4ba38:       c8 32 fe 4b     b       2ed00 <__syscall_error>
2be09a
            ...
2be09a
       4ba44:       40 20 0c 00     .long 0xc2040
2be09a
       4ba48:       68 00 00 00     .long 0x68
2be09a
       4ba4c:       06 00 5f 5f     rlwnm   r31,r26,r0,0,3
2be09a
       4ba50:       6b 69 6c 6c     xoris   r12,r3,26987
2be09a
    
2be09a
    (cherry picked from commit d120fb9941be1fb1934f0b50c6ad64e4c5e404fb)
2be09a
2be09a
diff --git a/sysdeps/powerpc/powerpc64/sysdep.h b/sysdeps/powerpc/powerpc64/sysdep.h
2be09a
index 589f7c8d18814ee9..cfcfa69f91f1773d 100644
2be09a
--- a/sysdeps/powerpc/powerpc64/sysdep.h
2be09a
+++ b/sysdeps/powerpc/powerpc64/sysdep.h
2be09a
@@ -275,12 +275,14 @@ LT_LABELSUFFIX(name,_name_end): ; \
2be09a
 /* Allocate frame and save register */
2be09a
 #define NVOLREG_SAVE \
2be09a
     stdu r1,-SCV_FRAME_SIZE(r1); \
2be09a
+    cfi_adjust_cfa_offset(SCV_FRAME_SIZE); \
2be09a
     std r31,SCV_FRAME_NVOLREG_SAVE(r1); \
2be09a
-    cfi_adjust_cfa_offset(SCV_FRAME_SIZE);
2be09a
+    cfi_rel_offset(r31,SCV_FRAME_NVOLREG_SAVE);
2be09a
 
2be09a
 /* Restore register and destroy frame */
2be09a
 #define NVOLREG_RESTORE	\
2be09a
     ld r31,SCV_FRAME_NVOLREG_SAVE(r1); \
2be09a
+    cfi_restore(r31); \
2be09a
     addi r1,r1,SCV_FRAME_SIZE; \
2be09a
     cfi_adjust_cfa_offset(-SCV_FRAME_SIZE);
2be09a
 
2be09a
@@ -331,13 +333,13 @@ LT_LABELSUFFIX(name,_name_end): ; \
2be09a
 
2be09a
 #define DO_CALL_SCV \
2be09a
     mflr r9; \
2be09a
-    std r9,FRAME_LR_SAVE(r1); \
2be09a
-    cfi_offset(lr,FRAME_LR_SAVE); \
2be09a
+    std r9,SCV_FRAME_SIZE+FRAME_LR_SAVE(r1);   \
2be09a
+    cfi_rel_offset(lr,SCV_FRAME_SIZE+FRAME_LR_SAVE); \
2be09a
     .machine "push"; \
2be09a
     .machine "power9"; \
2be09a
     scv 0; \
2be09a
     .machine "pop"; \
2be09a
-    ld r9,FRAME_LR_SAVE(r1); \
2be09a
+    ld r9,SCV_FRAME_SIZE+FRAME_LR_SAVE(r1);      \
2be09a
     mtlr r9; \
2be09a
     cfi_restore(lr);
2be09a