Bug 97166 - libffi build issue when compiling with -mcpu=power10
Summary: libffi build issue when compiling with -mcpu=power10
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libffi (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: 10.3
Assignee: Alan Modra
URL:
Keywords: build
Depends on:
Blocks:
 
Reported: 2020-09-22 17:53 UTC by Peter Bergner
Modified: 2020-09-24 14:21 UTC (History)
4 users (show)

See Also:
Host:
Target: powerpc*-*-*
Build:
Known to work:
Known to fail: 10.0
Last reconfirmed: 2020-09-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Bergner 2020-09-22 17:53:09 UTC
When building libffi with -mcpu=power10, we're seeing the following linker error.

libtool: link: gcc -shared  -fPIC -DPIC  src/.libs/prep_cif.o src/.libs/types.o src/.libs/raw_api.o src/.libs/java_raw_api.o src/.libs/closures.o src/powerpc/.libs/ffi.o src/powerpc/.libs/ffi_sysv.o src/powerpc/.libs/ffi_linux64.o src/powerpc/.libs/sysv.o src/powerpc/.libs/ppc_closure.o src/powerpc/.libs/linux64.o src/powerpc/.libs/linux64_closure.o    -O3 -mcpu=power10 -Wl,--version-script -Wl,libffi.map   -Wl,-soname -Wl,libffi.so.8 -o .libs/libffi.so.8.1.0
src/powerpc/.libs/linux64.o: in function `ffi_call_LINUX64':
(.text+0x40): call to `ffi_prep_args64' lacks nop, can't restore toc; (toc save/adjust stub)
/home/bergner/gcc/install/gcc-fsf-mainline-power10/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
Makefile:1028: recipe for target 'libffi.la' failed

This ends up being a bug in some POWER specific libffi asm code that doesn't know about our new to POWER10 PCREL support.  This bugzilla is to track syncing the fix to libffi into GCC version of libffi, which is being tracked upstream here:

  https://github.com/libffi/libffi/pull/584
Comment 1 Peter Bergner 2020-09-22 18:44:11 UTC
The same issue affects the libffi in GCC 10 sources, since GCC 10 has power10 support, so that needs fixing too.
Comment 2 Alan Modra 2020-09-24 08:02:10 UTC
Oops didn't put the PR number in the commit.  Fixed on master with commit 08cd8d5929 and followup commit fff56af642.  These two will give a working power10 libffi even without commit 677b9150f5, the patch adding a built-in __PCREL__ define but you'll get some linker stubs to call between pc-rel and toc code.
Comment 3 GCC Commits 2020-09-24 14:01:37 UTC
The releases/gcc-10 branch has been updated by Alan Modra <amodra@gcc.gnu.org>:

https://gcc.gnu.org/g:8f4b43c00feed11a6cedd4c40baa3cdcf687b3a1

commit r10-8794-g8f4b43c00feed11a6cedd4c40baa3cdcf687b3a1
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Sep 23 20:15:39 2020 +0930

    [RS6000] Built-in __PCREL__ define
    
    Useful in assembly to know details of power10 function calls.
    
            PR target/97166
            * config/rs6000/rs6000-c.c (rs6000_target_modify_macros):
            Conditionally define __PCREL__.
    
    (cherry picked from commit 677b9150f54a0483d3de1182ac40717b7c4431a5)
Comment 4 GCC Commits 2020-09-24 14:01:43 UTC
The releases/gcc-10 branch has been updated by Alan Modra <amodra@gcc.gnu.org>:

https://gcc.gnu.org/g:71c83e108de7b54f604eeebefbc9e97672310ca7

commit r10-8795-g71c83e108de7b54f604eeebefbc9e97672310ca7
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Sep 18 23:21:05 2020 +0930

    [RS6000] Power10 libffi fixes
    
    Power10 pc-relative code doesn't use or preserve r2 as a TOC pointer.
    That means calling between pc-relative and TOC using code can't be
    done without intervening linker stubs, and a call from TOC code to
    pc-relative code must have a nop after the bl in order to restore r2.
    
    Now the PowerPC libffi assembly code doesn't use r2 except for the
    implicit use when making calls back to C, ffi_closure_helper_LINUX64
    and ffi_prep_args64.  So changing the assembly to interoperate with
    pc-relative code without stubs is easily done.
    
            PR target/97166
            * src/powerpc/linux64.S (ffi_call_LINUX64): Don't emit global
            entry when __PCREL__.  Call using @notoc.  Add nops.
            * src/powerpc/linux64_closure.S (ffi_closure_LINUX64): Likewise.
            (ffi_go_closure_linux64): Likewise.
    
    (cherry picked from commit 08cd8d5929eac84b27788d8483fd75ab7ad13129)
    (cherry picked from commit fff56af6421a1a3e357bcaad99f2ea084d72a7a8)
Comment 5 Alan Modra 2020-09-24 14:21:42 UTC
Fixed everywhere we support power10