src/arm/sysv.S


Log

Author Commit Date CI Message
Martin Storsjö dd5bd030 2021-04-07T05:42:10 Fix building for arm windows with mingw toolchains (#631) * arm: Check _WIN32 instead of _M_ARM or _MSC_VER for detecting windows This matches what was done for ARM64 in c06468fa6674d3783a0edb1d0fae9afc8bc28513. * arm: Only use armasm source when building with MSVC When building for windows/arm with clang, the normal gas style .S source works fine (if fixed up to support thumb and other windows specifics). This matches what was done for ARM64 in c06468fa6674d3783a0edb1d0fae9afc8bc28513. * arm: Fix sysv.S to work in thumb mode Align cases in jump tables (adding nop padding to make sure each case starts where expected). Rewrite instructions that add directly to the pc register. For ffi_closure_ret, factor out a call_epilogue subroutine that restores both sp and pc from the stack; the thumb version of ldm can't load into the sp register. To avoid excessive ifdeffing, keep using call_epilogue in arm mode, but keep the shorter "ldm sp, {sp, pc}" epilogue in that case. * arm: Add win32 version of trampoline to sysv.S This matches the version of it in sysv_msvc_arm32.S. The calling C code expects a specific form of the trampoline on windows; make sure these work the same on windows regardless of the form of assembly used. * arm: Avoid optimizing out clearing the thumb bit of ffi_arm_trampoline We clear the thumb bit of ffi_arm_trampoline with a bitmask before memcpying its instructions into closure->tramp. If the bit isn't cleared, the memcpy of the trampoline function copies the wrong instructions. If the ffi_arm_trampoline symbol is declared as an array of int, the compiler can assume that it is aligned to a 4 byte boundary and the bitmask operation is a no-op, and optimize it out. See https://godbolt.org/z/dE3jE1WTz; both Clang and GCC optimize out the bitmask as it is, while MSVC doesn't. By declaring the trampoline as an array of unsigned char, the bitmask works as intended.
Jeremy Huddleston Sequoia d271dbe0 2021-03-20T06:06:28 Add some missing #if conditionals from Apple's code drop (#620) * arm/aarch64: Add FFI_CLOSURES conditionals where appropriate Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com> * aarch64: Don't emit the do_closure label when building without FFI_GO_CLOSURES Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Madhavan T. Venkataraman 9ba55921 2021-03-05T10:07:30 Static tramp v5 (#624) * Static Trampolines Closure Trampoline Security Issue ================================= Currently, the trampoline code used in libffi is not statically defined in a source file (except for MACH). The trampoline is either pre-defined machine code in a data buffer. Or, it is generated at runtime. In order to execute a trampoline, it needs to be placed in a page with executable permissions. Executable data pages are attack surfaces for attackers who may try to inject their own code into the page and contrive to have it executed. The security settings in a system may prevent various tricks used in user land to write code into a page and to have it executed somehow. On such systems, libffi trampolines would not be able to run. Static Trampoline ================= To solve this problem, the trampoline code needs to be defined statically in a source file, compiled and placed in the text segment so it can be mapped and executed naturally without any tricks. However, the trampoline needs to be able to access the closure pointer at runtime. PC-relative data referencing ============================ The solution implemented in this patch set uses PC-relative data references. The trampoline is mapped in a code page. Adjacent to the code page, a data page is mapped that contains the parameters of the trampoline: - the closure pointer - pointer to the ABI handler to jump to The trampoline code uses an offset relative to its current PC to access its data. Some architectures support PC-relative data references in the ISA itself. E.g., X64 supports RIP-relative references. For others, the PC has to somehow be loaded into a general purpose register to do PC-relative data referencing. To do this, we need to define a get_pc() kind of function and call it to load the PC in a desired register. There are two cases: 1. The call instruction pushes the return address on the stack. In this case, get_pc() will extract the return address from the stack and load it in the desired register and return. 2. The call instruction stores the return address in a designated register. In this case, get_pc() will copy the return address to the desired register and return. Either way, the PC next to the call instruction is obtained. Scratch register ================ In order to do its job, the trampoline code would need to use a scratch register. Depending on the ABI, there may not be a register available for scratch. This problem needs to be solved so that all ABIs will work. The trampoline will save two values on the stack: - the closure pointer - the original value of the scratch register This is what the stack will look like: sp before trampoline ------> -------------------- | closure pointer | -------------------- | scratch register | sp after trampoline -------> -------------------- The ABI handler can do the following as needed by the ABI: - the closure pointer can be loaded in a desired register - the scratch register can be restored to its original value - the stack pointer can be restored to its original value (the value when the trampoline was invoked) To do this, I have defined prolog code for each ABI handler. The legacy trampoline jumps to the ABI handler directly. But the static trampoline defined in this patch jumps tp the prolog code which performs the above actions before jumping to the ABI handler. Trampoline Table ================ In order to reduce the trampoline memory footprint, the trampoline code would be defined as a code array in the text segment. This array would be mapped into the address space of the caller. The mapping would, therefore, contain a trampoline table. Adjacent to the trampoline table mapping, there will be a data mapping that contains a parameter table, one parameter block for each trampoline. The parameter block will contain: - a pointer to the closure - a pointer to the ABI handler The static trampoline code would finally look like this: - Make space on the stack for the closure and the scratch register by moving the stack pointer down - Store the original value of the scratch register on the stack - Using PC-relative reference, get the closure pointer - Store the closure pointer on the stack - Using PC-relative reference, get the ABI handler pointer - Jump to the ABI handler Mapping size ============ The size of the code mapping that contains the trampoline table needs to be determined on a per architecture basis. If a particular architecture supports multiple base page sizes, then the largest supported base page size needs to be chosen. E.g., we choose 16K for ARM64. Trampoline allocation and free ============================== Static trampolines are allocated in ffi_closure_alloc() and freed in ffi_closure_free(). Normally, applications use these functions. But there are some cases out there where the user of libffi allocates and manages its own closure memory. In such cases, static trampolines cannot be used. These will fall back to using legacy trampolines. The user has to make sure that the memory is executable. ffi_closure structure ===================== I did not want to make any changes to the size of the closure structure for this feature to guarantee compatibility. But the opaque static trampoline handle needs to be stored in the closure. I have defined it as follows: - char tramp[FFI_TRAMPOLINE_SIZE]; + union { + char tramp[FFI_TRAMPOLINE_SIZE]; + void *ftramp; + }; If static trampolines are used, then tramp[] is not needed to store a dynamic trampoline. That space can be reused to store the handle. Hence, the union. Architecture Support ==================== Support has been added for x64, i386, aarch64 and arm. Support for other architectures can be added very easily in the future. OS Support ========== Support has been added for Linux. Support for other OSes can be added very easily. Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> * x86: Support for Static Trampolines - Define the arch-specific initialization function ffi_tramp_arch () that returns trampoline size information to common code. - Define the trampoline code mapping and data mapping sizes. - Define the trampoline code table statically. Define two tables, actually, one with CET and one without. - Introduce a tiny prolog for each ABI handling function. The ABI handlers addressed are: - ffi_closure_unix64 - ffi_closure_unix64_sse - ffi_closure_win64 The prolog functions are called: - ffi_closure_unix64_alt - ffi_closure_unix64_sse_alt - ffi_closure_win64_alt The legacy trampoline jumps to the ABI handler. The static trampoline jumps to the prolog function. The prolog function uses the information provided by the static trampoline, sets things up for the ABI handler and then jumps to the ABI handler. - Call ffi_tramp_set_parms () in ffi_prep_closure_loc () to initialize static trampoline parameters. Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> * i386: Support for Static Trampolines - Define the arch-specific initialization function ffi_tramp_arch () that returns trampoline size information to common code. - Define the trampoline code table statically. Define two tables, actually, one with CET and one without. - Define the trampoline code table statically. - Introduce a tiny prolog for each ABI handling function. The ABI handlers addressed are: - ffi_closure_i386 - ffi_closure_STDCALL - ffi_closure_REGISTER The prolog functions are called: - ffi_closure_i386_alt - ffi_closure_STDCALL_alt - ffi_closure_REGISTER_alt The legacy trampoline jumps to the ABI handler. The static trampoline jumps to the prolog function. The prolog function uses the information provided by the static trampoline, sets things up for the ABI handler and then jumps to the ABI handler. - Call ffi_tramp_set_parms () in ffi_prep_closure_loc () to initialize static trampoline parameters. Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> * arm64: Support for Static Trampolines - Define the arch-specific initialization function ffi_tramp_arch () that returns trampoline size information to common code. - Define the trampoline code mapping and data mapping sizes. - Define the trampoline code table statically. - Introduce a tiny prolog for each ABI handling function. The ABI handlers addressed are: - ffi_closure_SYSV - ffi_closure_SYSV_V The prolog functions are called: - ffi_closure_SYSV_alt - ffi_closure_SYSV_V_alt The legacy trampoline jumps to the ABI handler. The static trampoline jumps to the prolog function. The prolog function uses the information provided by the static trampoline, sets things up for the ABI handler and then jumps to the ABI handler. - Call ffi_tramp_set_parms () in ffi_prep_closure_loc () to initialize static trampoline parameters. Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com> * arm: Support for Static Trampolines - Define the arch-specific initialization function ffi_tramp_arch () that returns trampoline size information to common code. - Define the trampoline code mapping and data mapping sizes. - Define the trampoline code table statically. - Introduce a tiny prolog for each ABI handling function. The ABI handlers addressed are: - ffi_closure_SYSV - ffi_closure_VFP The prolog functions are called: - ffi_closure_SYSV_alt - ffi_closure_VFP_alt The legacy trampoline jumps to the ABI handler. The static trampoline jumps to the prolog function. The prolog function uses the information provided by the static trampoline, sets things up for the ABI handler and then jumps to the ABI handler. - Call ffi_tramp_set_parms () in ffi_prep_closure_loc () to initialize static trampoline parameters. Signed-off-by: Madhavan T. Venkataraman <madvenka@linux.microsoft.com>
Martin Storsjö 053b04c5 2020-04-26T05:02:14 arm: Fix the clang specific version of the assembly (#556) Also fix the same error in the comment for the non-clang case. That typo there seems to have existed since the code was written in that form, in e7f15f60e86 - and when the clang specific codepath was added in e3d2812ce43, the typo in the comment made it into the actual code.
Jeremy Huddleston Sequoia 05a17964 2019-02-19T04:11:28 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>
Jeremy Huddleston Sequoia ba73a671 2018-07-20T09:37:43 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>
Jeremy Huddleston Sequoia 1d704051 2018-06-17T17:01:50 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>
Johannes Goslar 00406945 2016-07-12T16:08:42 Update Xcodeproj Include all currently relevent files. Call autogen is build script. Fix compiler settings. Fix mach include.
Russell Keith-Magee bc4fc07a 2015-12-21T00:37:06 Fixed #181 -- Corrected problems with ARMv7 build under iOS. Based on a patch from @fealebenpae, with input from @SolaWing and @rth7680, and testing from @superdump.
Russell Keith-Magee e3d2812c 2015-04-25T19:03:03 Modified arm/sysv.S to remove directives not allowed by clang.
Richard Henderson ab83cbb9 2014-10-29T14:38:42 arm: Add support for Go closures
Richard Henderson e7f15f60 2014-10-17T01:27:16 arm: Rewrite ffi_call Use the trick to allocate the stack frame for ffi_call_SYSV within ffi_call itself.
Richard Henderson a4b785ea 2014-10-17T02:07:32 arm: Rewrite ffi_closure Move the push of the argument registers into ffi_closure_SYSV, reducing the size of the trampoline.
Anthony Green 001aaf4b 2014-02-28T00:20:17 When no VFP arguments are present the IP register is used uninitialized. Initialize it to the value of FP. This fixes a number of testsuite failures when configured for armv7l-unknown-linux-gnueabihf
Zachary Waldowski 6eff9ff9 2013-12-30T17:48:10 Darwin/iOS: Improve unified syntax use for LLVM
Anthony Green 3dc3f32c 2013-12-05T16:23:25 Undo iOS ARM64 changes.
Zachary Waldowski 953b6f14 2012-04-24T11:16:20 Darwin/iOS: More unified syntax support w/ Clang. Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
Zachary Waldowski c713a553 2012-04-24T10:25:29 Darwin/iOS: Simplify RETLDM arguments for LLVM 3.1 Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
David Schneider b4112098 2013-03-27T16:38:35 create separated versions of ffi_prep_incoming_args_* for SYSV and VFP ABIs. The different versions will be called depending on the value of cif->abi
David Schneider 0f2ff2d4 2013-03-26T19:22:02 separate ARM ffi_prepare_args in a version implementing the simple SYSV calling convention and one for the hard-float calling convention
Zachary Waldowski 39dccddb 2012-04-05T12:32:41 Fix building with Clang for Darwin (OS X 10.6+ and iOS 4.0+)
Anthony Green e1539266 2012-03-30T00:40:18 ARM VFP fix for old toolchains
Anthony Green 3d56106b 2011-11-12T07:20:24 Rebase
Anthony Green 09f8f310 2011-02-28T15:36:07 More AIX fixes. rc9.
Landon Fuller 8195e0e1 2011-02-12T11:27:00 Fix symbol prefixes on Darwin.
Landon Fuller 28a00f61 2011-02-12T11:01:48 Apple assembler support; fixed most gas/ELF-isms.
Landon Fuller 7f2ea33a 2011-02-12T10:39:18 Replace RETLDM macro. The macro is incompatible with Apple's assembler; switch to a simple inline version.
Anthony Green 0cad4386 2011-02-09T06:11:46 Add ChangeLog entry. Fix copyright headers.
Anthony Green 1106229a 2011-02-08T19:20:09 Add iOS support
Anthony Green 0e584399 2011-02-08T07:52:40 Refresh from GCC
Anthony Green 2db72615 2010-11-21T10:50:56 Rebase
Landon Fuller f38364b3 2010-09-19T10:42:36 Fix symbol prefix for ffi_closure_SYSV_inner on Darwin.
Landon Fuller 70150bdf 2010-09-18T16:38:03 Add missing UNWIND entry; disables .pad on non-EABI targets.
Landon Fuller 6b452baf 2010-09-18T16:21:32 Apple assembler support; fixed most gas/ELF-isms.
Landon Fuller 8ddac835 2010-09-18T15:38:06 Fix placement of the __APPLE__ macro.
Landon Fuller 69043d02 2010-09-18T15:32:08 Work-around libffi's FP ABI detection. On iOS, we must use the AAPCS floating point return value calling conventions. libffi's ARM implementation will only use these conventions if __SOFTFP__ is defined, which is not the case when GCC's -mfloat-abi defaults to 'softfp' instead of 'soft'. To work around this we manually define __SOFTFP__ for Apple platforms in the ARM-specific sysv.S. See also: http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iPhoneOSABIReference/Introduction/Introduction.html http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf
Anthony Green 9dc9a293 2010-04-13T10:33:52 Rebase to latest GCC sources
Anthony Green c6dddbd0 2009-10-04T08:11:33 Initial commit