Commit Graph
463 Commits
Author SHA1 Message Date
Sergei Trofimovich 01a75ed76e powerpc: fix build failure on power7 and older (#532)
Build failure looks as:
```
libtool: compile:  powerpc-unknown-linux-gnu-gcc \
    -O2 -mcpu=powerpc -mtune=powerpc -pipe ... -c src/powerpc/ffi.c ...
In file included from src/powerpc/ffi.c:33:
src/powerpc/ffi_powerpc.h:65:9: error: '__int128' is not supported on this target
   65 | typedef __int128 float128;
      |         ^~~~~~~~
```

The fix avoids using __int128 in favour of aligned char[16].

Closes: https://github.com/libffi/libffi/issues/531
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2019-11-28 07:42:41 -05:00
Anthony Green 31543c799a Add work-around for users who manage their own closure memory
As suggested by DJ
2019-11-20 06:15:55 -05:00
Samuel Holland 73dd43afc8 IEEE754 binary128 long double support for PowerPC64 (#526)
* powerpc: Adjust flags to make room for vector types

* powerpc64 ELFv2 IEEE128 long double support
2019-11-17 08:22:25 -05:00
Andreas Schwab a8efc2f789 Fix FFI_STDCALL ABI (#514)
Even for a stdcall function, the stack alignment is still the
responsibility of the caller.  Remember the original, not stack-aligned
argument size, but align when setting up a stack frame.  In
ffi_closure_inner, return the true argument size, so that
ffi_[go_]closure_STDCALL doesn't adjust too much.
2019-10-26 08:57:05 -04:00
zhanhb c4f61240ab Add long double test (#492)
Required to fix build error on macos with gcc-9
2019-10-09 06:59:32 -04:00
pnallan 55c22092dc handle compilation warnings with ftruncate API (#508)
* fix me: avoid warning while handle ftruncate API

Signed-off-by: Prasad Nallani <prasad.nallani@intel.com>

* Update closures.c
2019-10-08 09:16:47 -04:00
John Ericson e5f0eb1552 Clean up line endings (#509)
The CLRF visual studio files can be kept that way, but recognized as
text. The assembly file can be converted to LF.
2019-10-08 06:58:52 -04:00
Samuel Holland ea9b6639c6 PowerPC bugfixes (#520)
* powerpc: Silence warnings about unused labels

* powerpc: Fix a couple of comments

* powerpc: Fix alignment after float structs

* powerpc: Don't pad rvalues copied from FP regs

* powerpc: Add missing check in struct alignment

* powerpc: Support homogeneous long double structs
2019-10-08 06:57:28 -04:00
Paul Monson c2a6859012 fix mingw build and crashing bugs for Python Windows ARM64 (#496)
* fix mingw build and crashing bugs for Python Windows ARM64

* Fix issues found in PR review
2019-08-07 14:57:45 -04:00
ossdev07 d856743e6b libffi: added ARM64 support for Windows (#486)
*  libffi: added ARM64 support for Windows

    1. ported sysv.S to win64_armasm.S for armasm64 assembler
    2. added msvc_build folder for visual studio solution
    3. updated README.md for the same
    4. MSVC solution created with the changes, and below test suites are tested
       with test script written in python.

       libffi.bhaible
       libffi.call
    5. Basic functionality of above test suites are getting passed

Signed-off-by: ossdev07 <ossdev@puresoftware.com>

* Update README.md
2019-06-25 22:01:22 -04:00
Sergei Trofimovich fadf1eb530 hppa: avoid TEXTREL in .eh_frame section (#447)
Before the change hand-crafted .eh_frame section contained
ABS relocation and caused TEXTREL tag to be emitted:

```
$ ./configure --host=hppa2.0-unknown-linux-gnu LDFLAGS=-Wl,-z,text
$ make
...
/usr/libexec/gcc/hppa2.0-unknown-linux-gnu/ld:
    read-only segment has dynamic relocations.
```

Link failure is caused by absolute address of FDEs
encoded into .eh_frame entries.

Fixed TEXTREL by using pcrel (instead of ABS) encoding
for absolute addresses (__PIC__ code) by adding augmentation
information ("zR" CIE type).

All tests still pass on hppa2.0. The specific tests that still pass
and exercise this code path:
    testsuite/libffi.call/unwindtest.cc
    testsuite/libffi.call/unwindtest_ffi_call.cc

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2019-04-27 15:53:28 -04:00
driver1998 06bf1a9dea fix x86/x64 MSVC build (#487) 2019-04-27 15:21:44 -04:00
Paul Monson db5706ff28 add support for 32-bit ARM on Windows (#477)
* add support for 32-bit ARM on Windows

* fix mismatched brace in appveyor.yml

* remove arm platform from appveyor.yml for now

* fix arm build

* fix typo

* fix assembler names

* try Visual Studio 2017

* add windows arm32 to .appveyor.yml

* update README.md
2019-04-26 07:58:58 -04:00
Dan Horák a7d6396f06 fix check for Linux/aarch64
fixes #473
2019-03-29 14:19:20 +01:00
Jeremy Huddleston Sequoia 05a1796419 Cleanup symbol exports on darwin and add architecture preprocessor checks to assist in building fat binaries (eg: i386+x86_64 on macOS or arm+aarch64 on iOS) (#450)
* x86: Ensure _efi64 suffixed symbols are not exported

* x86: Ensure we do not export ffi_prep_cif_machdep

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* x86: Ensure we don't export ffi_call_win64, ffi_closure_win64, or ffi_go_closure_win64

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* closures: Silence a semantic warning

libffi/src/closures.c:175:23: This function declaration is not a prototype

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* aarch64: Ensure we don't export ffi_prep_cif_machdep

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* arm: Ensure we don't export ffi_prep_cif_machdep

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* aarch64, arm, x86: Add architecture preprocessor checks to support easier fat builds (eg: iOS)

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* x86: Silence some static analysis warnings

libffi/src/x86/ffi64.c:286:21: The left operand of '!=' is a garbage value due to array index out of bounds
libffi/src/x86/ffi64.c:297:22: The left operand of '!=' is a garbage value due to array index out of bounds

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* aarch: Use FFI_HIDDEN rather than .hidden

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>

* ffi.h: Don't advertise ffi_java_rvalue_to_raw, ffi_prep_java_raw_closure, and ffi_prep_java_raw_closure_loc when FFI_NATIVE_RAW_API is 0

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
2019-02-19 07:11:28 -05:00
Paul Monson e1118af505 changes for win32 on windows (#468) 2019-02-19 06:58:25 -05:00
Florian Weimer 44a6c28545 aarch64: Flush code mapping in addition to data mapping (#471)
This needs a new function, ffi_data_to_code_pointer, to translate
from data pointers to code pointers.

Fixes issue #470.
2019-02-19 06:55:11 -05:00
Martin Bektchiev 4a84df4ae9 Fix Q registers parameter passing on ARM64
The second two quads are located at offset 32 not 16
2018-10-31 16:25:05 +02:00
Tom Tromey a5ea7527cd Merge pull request #443 from jeremyhu/master
Update FFI_HIDDEN() to use .private_extern on Apple platforms and use the macro where appropriate
2018-09-19 07:29:36 -06:00
Gregory Pakosz e6eac7863e Prefix ALIGN_DOWN macro with FFI_ 2018-09-18 15:19:53 +02:00
Andreas Schwab 4cb776bc80 RISC-V go closures
This implements go closures for RISC-V.  It has been tested on
riscv64-suse-linux and against the libgo testsuite.
2018-08-09 15:41:17 +02:00
Jeremy Huddleston Sequoia ba73a671cb Update FFI_HIDDEN() to use .private_extern on Apple platforms and use the macro where appropriate
Fix issue #439

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>
2018-07-21 14:36:10 -07:00
Jeremy Huddleston Sequoia 65da63abc8 Add compact unwind for darwin/i386 (#440)
* x86: Add implementation of compact unwind for ffi_call_i386, et al.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>

* x86: Use __text as the section name to avoid deprecated section name warnings.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>

* darwin: Add missing regular,debug attributes for compact unwind sections

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>
2018-06-25 07:38:58 -04:00
Shoaib Meenai 2309b58448 Mark sysv.S as SafeSEH compatible (#438)
It contains no exception handler, so we can just emit the special
@feat.00 symbol to indicate that it's trivially SafeSEH compatible.
SafeSEH only applies to x86 and not x86-64, hence its inclusion in the
x86-specific block. See [1] for details.

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx#the_.sxdata_section_
2018-06-17 20:04:24 -04:00
Jeremy Huddleston Sequoia 1d704051b2 i386: Fix missing break; in case statement leading to incorrectly returned FFI_BAD_ABI (#437)
* i386: Add missing break triggering dead store static analyzer checks.

Register calling sequence is being reported as bad ABI instead of working as intended.

Found-by: Clang Static Analysis
Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>

* Mark ffi arm sysv entry points as private_extern.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>

* x86_64: Add implementation of compact unwind for ffi_call_unix64.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
2018-06-17 20:01:50 -04:00
Anthony Green b55baf0b50 Handle FFI_GNUW64 on non-Windows systems (EFI) 2018-05-09 13:21:02 -04:00
Anthony Green b5ee395710 Revert "Remove some symbol exports and cleanup newline warnings (#433)"
This reverts commit a5a0f3cf36.
2018-05-05 07:41:53 -04:00
Jeremy Huddleston Sequoia a5a0f3cf36 Remove some symbol exports and cleanup newline warnings (#433)
* build: Ensure darwin generated sources end with a new line

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>

* build: Use .private_extern where missing to prevent exporting symbols that are not API

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@macports.org>
2018-05-05 06:44:33 -04:00
hjl-tools d3c54cf3a2 Re-enable msabi testing (#436)
* Revert "disable msabi testing for now"

This reverts commit 7b7638eb0e.

* x86: Correct testing for 64-bit x86-64

Since passing -m32 to x86-64 compiler will generate i386 code, we
need to check both __ILP32__ and __i386__ for 64-bit x86-64.

* x86: Check __i386__ instead of i?86-*-* targets

Since passing -m32 to x86-64 compiler will generate i386 code, we need
to check __i386__ instead of i?86-*-* targets for i386 targets.

* i386: Properly passing integer parameters in registers

For thiscall and fastcall, if the paramter passed as 64-bit integer or
struct, all following integer paramters will be passed on stack.

* test: Add ABI_ATTR to callback_code

Add ABI_ATTR to callback_code to properly test different ABIs.
2018-05-02 09:19:58 -04:00
Alan Modra ebf241663c PowerPC64 ELFv1 fp arg fixes
The ELFv1 ABI says: "Single precision floating point values are mapped
to the second word in a single doubleword" and also "Floating point
registers f1 through f13 are used consecutively to pass up to 13
floating point values, one member aggregates passed by value
containing a floating point value, and to pass complex floating point
values".

libffi wasn't expecting float args in the second word, and wasn't
passing one member aggregates in fp registers.  This patch fixes those
problems, making use of the existing ELFv2 homogeneous aggregate
support since a one element fp struct is a special case of an
homogeneous aggregate.

I've also set a flag when returning pointers that might be used one
day.  This is just a tidy since the ppc64 assembly support code
currently doesn't test FLAG_RETURNS_64BITS for integer types..

	* src/powerpc/ffi_linux64.c (discover_homogeneous_aggregate):
	Compile for ELFv1 too, handling single element aggregates.
	(ffi_prep_cif_linux64_core): Call discover_homogeneous_aggregate
	for ELFv1.  Set FLAG_RETURNS_64BITS for FFI_TYPE_POINTER return.
	(ffi_prep_args64): Call discover_homogeneous_aggregate for ELFv1,
	and handle single element structs containing float or double
	as if the element wasn't wrapped in a struct.  Store floats in
	second word of doubleword slot when big-endian.
	(ffi_closure_helper_LINUX64): Similarly.
2018-05-02 07:14:39 -04:00
Tom Tromey 4c2206ace0 Fix two "return" issues in x86/ffi64.c (#431)
Issue #70 pointed out that at least one compiler didn't like:

    return ffi_call_efi64(cif, fn, rvalue, avalue);

... where the return type is "void".  This patch splits the statement
into two.

I also noticed that ffi_call_go here seems to do a double call.  I
suspect a "return" is missing here, so this patch adds it as well.
2018-04-28 06:46:10 -04:00
Lucas Pluvinage 7d3cab7926 xtensa-linux: use cache flush instruction only if it is available (#426) 2018-04-20 18:24:50 -04:00
James Cowgill 159b94e5fd Various MIPS Fixes (#425)
* mips: simplify closure #defines

This commit should have no visible effect.

* mips: add special handling of variadic functions

MIPS requires special handling of variadic functions which pass floating
point arguments:
* In the o32 ABI, all float arguments are passed in integer registers.
* In the n32/n64 ABIs, float arguments after the ellipsis are passed in
  integer registers.

Implement this in libffi. To support this in n32/n64 closures, we need to add
a new mips_nfixedargs field to ffi_cif which will break the libffi ABI.

This fixes the libffi.call/cls_longdouble_va.c test which was failing on
64-bit MIPS.

* mips: align argn for all 64-bit types in o32 closure handler

Ensure that argn is pre-aligned for all 64-bit argument types (including
doubles) and not just integer types.

This fixes closures of the form "f(float, double, <some integer args>)".
Previously the first integer argument would be read from a2 which is garbage
at this point (the float arguments have already "consumed" a0-a3). After
this commit, argn is correctly padded between the "float" and "double"
arguments so that the first integer argument is read from the stack.

Fixes "double f(float,double,int)" test in #371

* mips: do not read from floating point register if returning a struct

In the o32 ABI, the pointer passed in a0 used to return structures
indirectly is treated as the first argument for argument allocation purposes.
This means that it should inhibit floating point registers the same way that
other integer arguments do.

Fixes "Double f(float,Double,double)" test in #371

* mips: fix pointer cast warnings

Fix two pointer cast warnings when compiled on 64-bit mips by casting
through uintptr_t.

Fixes mips64el part of #404
2018-04-18 20:28:23 -04:00
Anthony Green e27f70b8cf Fix case where callback arg value is split across regs and stack 2018-04-08 18:25:34 -04:00
fwg af6773d6ab Fix appveyor windows build (#420)
* Fix msvcc dll build by adding dllexport decorations to all API declarations

* Fix appveyor build for VS 2013

Use the new -DFFI_BUILDING_DLL for producing a working DLL. Update the
msvcc.sh wrapper script to successfully compile the testsuite files.

* MSVC build: suppress warnings in testsuite

* fix testsuite on appveyor
2018-04-02 06:55:31 -05:00
Anthony Green 369ef49f71 Add missing FFI_GNUW64 enum 2018-03-18 12:53:42 -04:00
Anthony Green 43980dd10e Add FFI_GNUW64 ABI for GNU 80-bit long double support 2018-03-18 12:33:38 -04:00
Ryan C. Underwood d46406088d Fully allocate file backing writable maps (#389)
When ftruncate() is used on a filesystem supporting sparse files,
space in the file is not actually allocated.  Then, when the file
is mmap'd and libffi writes to the mapping, SIGBUS is thrown to
the calling application.  Instead, always fully allocate the file
that will back writable maps.
2018-03-18 10:00:42 -04:00
Anthony Green 6a801d042a Fix closure case where 8-byte value is partially passed in register. Fixes cls_many_mixed_float_double test case. 2018-03-16 17:53:33 -04:00
Anthony Green 01db31d94e Update moxie sub opcode 2018-03-13 20:41:55 -04:00
Stef O'Rear 3840d49aaa New RISC-V port (#281)
* Add RISC-V support

This patch adds support for the RISC-V architecture (https://riscv.org).

This patch has been tested using QEMU user-mode emulation and GCC 7.2.0
in the following configurations:

* -march=rv32imac -mabi=ilp32
* -march=rv32g -mabi=ilp32d
* -march=rv64imac -mabi=lp64
* -march=rv64g -mabi=lp64d

The ABI currently can be found at
https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md .

* Add RISC-V to README

* RISC-V: fix configure.host
2018-03-11 08:55:15 -04:00
Anthony Green dca52b55bc Merge pull request #406 from trofi/master
ia64: fix variadic function closures with FP arguments
2018-03-11 08:50:01 -04:00
Anthony Green 83d9aba3a4 Merge pull request #407 from trofi/ia64-small-struct
ia64: fix small struct return
2018-03-11 08:48:42 -04:00
Andreas Schwab e66fd67820 Revert "Fix passing struct by value on aarch64"
This reverts commit 482b37f004.

That was actually a bug in python, see <https://bugs.python.org/issue30353>.
2018-02-20 10:47:09 +01:00
Sergei Trofimovich b58caef7fd ia64: fix small struct return
This change fixes libffi.call/struct10.c failure on ia64:
FAIL: libffi.call/struct10.c -W -Wall -Wno-psabi -O0 execution test

.Lst_small_struct handles returns for structs less than 32 bytes
(following ia64 return value ABI [1]). Subroutine does roughly the
following:

```
    mov [sp+0]  = r8
    mov [sp+8]  = r9
    mov [sp+16] = r10
    mov [sp+24] = r11
    memcpy(destination, source=sp, 12);
```

The problem: ia64 ABI guarantees that top 16 bytes of stack are
scratch space for callee function. Thus it can clobber it. [1]
says (7.1 Procedure Frames):
"""
* Scratch area. This 16-byte region is provided as scratch storage
  for procedures that are called by the current procedure. Leaf
  procedures do not need to allocate this region. A procedure may
  use the 16 bytes at the top of its own frame as scratch memory,
  but the contents of this area are not preserved by a procedure call.
"""

In our case 16 top bytes are clobbered by a PLT resolver when memcpy()
is called for the first time. As a result memcpy implementation reads
already clobbered data frop top of stack.

The fix is simple: allocate 16 bytes of scrats space prior to memcpy()
call.

[1]: https://www.intel.com/content/dam/www/public/us/en/documents/guides/itanium-software-runtime-architecture-guide.pdf

Bug: https://bugs.gentoo.org/634190
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2018-02-17 19:01:29 +00:00
Sergei Trofimovich 11de69ddb7 ia64: fix variadic function closures with FP arguments
libffi test framework already flagged failures as:

```
    FAIL: libffi.call/cls_double_va.c -W -Wall -Wno-psabi -O0 output pattern test, is 7.0
    res: 4
    0.0
    res: 4
    ? should match 7.0
    ?es: 4
    ?.0
    res: 4
```

Failure happens here at

```c
    // testsuite/libffi.call/cls_double_va.c
    ...
    char*  format    = "%.1f\n";
    double doubleArg = 7;
    ...
    CHECK(ffi_prep_closure_loc(pcl, &cif, cls_double_va_fn, NULL,
                               code) == FFI_OK);
    res = ((int(*)(char*, ...))(code))(format, doubleArg);
```

libffi expects 'doubleArg' to be located in 'f9' (second FP argument) but
gcc placed it to 'r33' (second GR).

ia64 software [1] manual described argument passing ABI in
"8.5.2 Register Parameters" as:

"""
    If an actual parameter is known to correspond to a floating-point
    formal parameter, the following rules apply:
    a) The actual parameter is passed in the next available floating-point
       parameter register, if one is available. Floating-point parameter
       registers are allocated as needed from the range f8-f15, starting
       with f8.
    b) If all available floating-point parameter registers have been used,
       the actual parameter is passed in the appropriate general register(s).
       (This case can occur only as a result of homogeneous floating-point
       aggregates, described below.)

    If a floating-point actual parameter is known to correspond to
    a variable-argument specification in  the formal parameter list,
    the following rule applies:
    c) The actual parameter is passed in the appropriate general
       register(s).

    If the compiler cannot determine, at the point of call,
    whether the corresponding formal parameter is a varargs parameter,
    it must generate code that satisfies both of the above conditions.
    (The compiler’s determination may be based on prototype declarations,
    language standard assumptions,  analysis, or other user options or
    information.)
"""

We have [c] case here and gcc uses only GR for parameter passing.

The change binds known variadic arguments ro GRs instead of FPs as those
are always expected to be initialized for all variadic call types.

This fixes all 10 failures on ia64-unknown-linux-gnu:

```
                 === libffi Summary ===
    -# of expected passes            1945
    -# of unexpected failures        10
    +
    +# of expected passes            1955
```

[1]: https://www.intel.com/content/dam/www/public/us/en/documents/guides/itanium-software-runtime-architecture-guide.pdf

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2018-02-11 12:17:32 +00:00
Anthony Green 9429968b82 Merge pull request #403 from frida/fix/x86-sysv-pic-closure-regression
Fix x86 SysV closure in PIC mode
2018-02-10 23:23:33 -05:00
Andreas Schwab 482b37f004 Fix passing struct by value on aarch64
This fixes the ctypes test in the python testsuite.
2018-02-07 12:50:14 +01:00
Ole André Vadla Ravnås 28d3b61bdd Fix x86 SysV closure in PIC mode
The assembly single-line comments swallowed up the remaining assembly
code of the macros due to lack of line-endings.

This is a regression introduced in b7f6d7a.
2018-01-21 01:27:22 +01:00
Anthony Green bec6135dff Merge pull request #393 from thejunkjon/master
Linker error "recompile with -fPIC" for x86_64
2018-01-10 07:20:04 -05:00