power8 assembly code with shared build issue with save and restore of TOC

173 views Asked by At

I have the following assembly code

        .machine power8
        .abiversion 2
        .section        ".toc","aw"
        .section        .text
        GLOBAL(myfunc)
myfunc:
        stdu 1,-240(1)
        mflr 0
        std  0, 0*8(1)
        mfcr 8
        std  8, 1*8(1)
        std  2, 2*8(1)
        # Save all non-volatile registers R14-R31
        std  14, 4*8(1)
        ...
        # Save all the non-volatile FPRs
        ...
        stwu 1, -48(1)
        bl function_call
        nop
        addi 1, 1, 48
        ld   0, 0*8(1)
        mtlr 0
        ld  8, 1*8(1)
        ld  2, 2*8(1)
        ...
        # epilogue, restore stack frame

This works fine with static build but shared build gives segmentation fault in 00000157.plt_call.__tls_get_addr_opt@@GLIBC_2.22, should the shared build be handled differently in power8 w.r.t TOC?

1

There are 1 answers

8
Jeremy Kerr On

The calling convention is the same between POWER 8 and previous processors. However, there has been changes with regards to the TOC pointer (r2) handling between ABIv1 and ABIv2.

In ABIv2, the caller does not establish the TOC pointer in r2; the called function should do this for global entry points (ie, where the TOC pointer may not be the same as that used in the callee). To do this, ABIv2 functions will have a prologue that sets r2:

0000000000000000 <foo>:
   0:   00 00 4c 3c     addis   r2,r12,0
   4:   00 00 42 38     addi    r2,r2,0

- this depends on r12 containing the address of the function's global entry point (those 0 values will be replaced with actual offsets at final link time).

I don't see any code setting r12 appropriately in your example. Are you sure you're complying with the v2 ABI there?

The ABIv2 spec is available here: https://members.openpowerfoundation.org/document/dl/576 Section 2.3.2 will be the most relevant for this issue.