Skip to content

Commit 9b04071

Browse files
Aimol-laimolclaude
authored
compat.libffi: build the aarch64 backend, so aarch64 links (#920)
The descriptor listed only src/x86/*, whose files guard themselves out on aarch64; the resulting libffi.so.8 had no ffi_prep_cif_machdep, and every libwayland consumer died at the first closure dispatch. - compile src/aarch64/ffi.c and sysv.S under target_cfg['cfg(arch = "aarch64")']. ffi.c guards its whole body, but sysv.S has an ELF GNU property note after `#endif /* __arm64__ */` that GNU as on x86 cannot drop -- it reports `invalid operands (*UND* and *UND*) for '|'` -- so both units are selected by target, not listed in the common set. No per-file flags or defines are needed. - switch c_standard to gnu11: libffi is GNU C, and clang rejects `asm` under strict -std=c11. GCC accepts it, which is why the x86 path never showed this. - install() no longer bakes x86_64 into the shared install tree: ffi.h's @target@ block becomes a per-arch dispatch (X86 on 32-bit x86, as configure writes, because src/x86/ffitarget.h reads it), and both ffitarget.h files are installed under arch names behind a generated selector. Unsupported architectures get an explicit #error instead of a silently wrong x86 header. - extend the example test with a variadic ffi_call, the path aarch64 reaches through ffi_cif.is_variadic. Verified: tests/examples/libffi green on x86_64 with gcc 16.1.0 and llvm 22.1.8, and on aarch64 with llvm 23.1.3; an aarch64 compositor built against this descriptor then serves a headless Wayland socket and its globals enumerate with wayland-info. Co-authored-by: aimol <aimol@qq.com> Co-authored-by: Claude Code <noreply@anthropic.com>
1 parent fb6bdbc commit 9b04071

2 files changed

Lines changed: 135 additions & 14 deletions

File tree

‎pkgs/c/compat.libffi.lua‎

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@
3939
--
4040
-- so all of them can be listed and the preprocessor drops the ones that do not
4141
-- apply. The `.S` files also `#define LIBFFI_ASM` themselves, so no assembler
42-
-- flag is needed either. This is x86/x86_64 only; another architecture needs
43-
-- its own `src/<arch>/` files added here.
42+
-- flag is needed either.
43+
--
44+
-- `src/aarch64/ffi.c` guards its whole body on `__aarch64__`, so it would be
45+
-- safe in the common list — but `src/aarch64/sysv.S` is not fully guarded: the
46+
-- ELF GNU property note at its end (BTI/PAC, after `#endif /* __arm64__ */`)
47+
-- references macros defined inside the guard, and GNU as on x86 reports
48+
-- `invalid operands (*UND* and *UND*) for '|'` instead of dropping it. Both
49+
-- aarch64 units therefore live in `target_cfg`, selected only for an aarch64
50+
-- target. Unlike pixman this needs no per-file flags or defines, only the
51+
-- source set. `src/aarch64/win64_armasm.S` stays out — it is MSVC armasm
52+
-- syntax for Windows.
53+
--
54+
-- This is x86/x86_64 and aarch64 only; another architecture needs its own
55+
-- `src/<arch>/` files added here.
4456
--
4557
-- ─────────────────────────────────────────────────────────────────────────
4658
-- THE THREE GENERATED HEADERS
@@ -51,17 +63,33 @@
5163
-- `fficonfig.h` cannot be derived from the tarball — it is the record of what
5264
-- configure probed — so it is inlined below, verbatim from a real
5365
-- `./configure` run on x86_64-pc-linux-gnu with the host-naming lines dropped.
66+
-- It serves both targets unchanged: the ABI facts in it (`SIZEOF_SIZE_T`,
67+
-- `SIZEOF_DOUBLE`, `SIZEOF_LONG_DOUBLE`, `FFI_EXEC_STATIC_TRAMP`, which
68+
-- configure also enables for aarch64-linux) are identical on x86_64 and
69+
-- aarch64 SysV, and the x86-only probes (`HAVE_AS_X86_PCREL`,
70+
-- `HAVE_AS_X86_64_UNWIND_SECTION_TYPE`, `EH_FRAME_FLAGS`) are read only by
71+
-- x86 assembly, which is preprocessed away on aarch64.
5472
--
5573
-- The other two ARE derivable, so install() does what configure would rather
5674
-- than hard-coding 500 lines: `ffi.h.in` has exactly four substitutions
5775
-- (@TARGET@, @HAVE_LONG_DOUBLE@, @FFI_EXEC_TRAMPOLINE_TABLE@, @VERSION@) and
5876
-- `ffitarget.h` is a copy. Deriving them keeps this descriptor honest when the
5977
-- version moves.
78+
--
79+
-- ⚠ configure bakes ONE architecture into those two headers, and install()
80+
-- cannot: the installed tree is shared by every target the store serves, and
81+
-- mcpp compiles a package's sources per consumer build. So install() emits a
82+
-- target-neutral `ffi.h` (the @TARGET@ block becomes a dispatch on the
83+
-- consumer compiler's predefined macros) and installs BOTH
84+
-- `src/x86/ffitarget.h` and `src/aarch64/ffitarget.h` under architecture
85+
-- names, with a generated `ffitarget.h` that includes exactly one of them.
86+
-- Only the selected header is seen by a TU, so their shared `LIBFFI_TARGET_H`
87+
-- guard never collides.
6088
package = {
6189
spec = "1",
6290
namespace = "compat",
6391
name = "libffi",
64-
description = "libffi — portable foreign function interface, built from the upstream release",
92+
description = "libffi — portable foreign function interface, built from the upstream release (x86/x86_64, aarch64)",
6593
licenses = {"MIT"},
6694
repo = "https://github.com/libffi/libffi",
6795
type = "package",
@@ -81,7 +109,15 @@ package = {
81109
mcpp = {
82110
language = "c++23",
83111
import_std = false,
84-
c_standard = "c11",
112+
-- Upstream's build sets no standard, so compilers use their default
113+
-- GNU dialect. That matters here: `src/aarch64/ffi.c` keeps most of
114+
-- its semantics in GCC extended inline asm (`asm ("ldp q16, …" :
115+
-- … : "memory", "v16")`), and Clang in strict C11 does not treat
116+
-- `asm` as a keyword — it parses the template as a function call and
117+
-- stops with `expected ')'`. GCC accepts it even under `-std=c11`,
118+
-- which is why the x86 backend (whose asm lives in `.S` files) never
119+
-- showed this.
120+
c_standard = "gnu11",
85121

86122
-- Upstream's AM_CPPFLAGS is `-I. -I$(top_srcdir)/include -Iinclude
87123
-- -I$(top_srcdir)/src`; `.` is where fficonfig.h lands and
@@ -116,6 +152,17 @@ package = {
116152
"src/x86/win64.S",
117153
},
118154

155+
-- The aarch64 backend. Not in the common list because sysv.S has an
156+
-- unguarded ELF note at its end; see the header comment.
157+
target_cfg = {
158+
["cfg(arch = \"aarch64\")"] = {
159+
sources = {
160+
"src/aarch64/ffi.c",
161+
"src/aarch64/sysv.S",
162+
},
163+
},
164+
},
165+
119166
targets = { ["ffi"] = { kind = "shared", soname = "libffi.so.8" } },
120167
deps = {},
121168

@@ -337,8 +384,28 @@ function install()
337384
return false
338385
end
339386

340-
text = text:gsub("@TARGET@", "X86_64")
341-
:gsub("@HAVE_LONG_DOUBLE@", "1")
387+
-- `@TARGET@` is configure's name for the architecture it ran on. It is not
388+
-- purely decorative: `src/x86/ffitarget.h` reads `X86_64` to choose the
389+
-- UNIX64 ABI, and configure writes `X86` on 32-bit x86. One installed tree
390+
-- serves every target here, so nothing may be baked in: turn the
391+
-- template's guard block into a dispatch on the consumer compiler's own
392+
-- predefined macro, spelling the names configure would have written.
393+
text = text:gsub("#ifndef @TARGET@%s*\n#define @TARGET@%s*\n#endif",
394+
"#if defined(__aarch64__) || defined(__arm64__)\n"
395+
.. "# ifndef AARCH64\n"
396+
.. "# define AARCH64\n"
397+
.. "# endif\n"
398+
.. "#elif defined(__x86_64__)\n"
399+
.. "# ifndef X86_64\n"
400+
.. "# define X86_64\n"
401+
.. "# endif\n"
402+
.. "#elif defined(__i386__)\n"
403+
.. "# ifndef X86\n"
404+
.. "# define X86\n"
405+
.. "# endif\n"
406+
.. "#endif")
407+
408+
text = text:gsub("@HAVE_LONG_DOUBLE@", "1")
342409
:gsub("@FFI_EXEC_TRAMPOLINE_TABLE@", "0")
343410
:gsub("@VERSION@", pkginfo.version())
344411

@@ -349,14 +416,39 @@ function install()
349416
end
350417
io.writefile(path.join(root, "include", "ffi.h"), text)
351418

352-
-- configure copies the target ffitarget.h into include/; ffi.h does
353-
-- `#include <ffitarget.h>` and consumers need it too.
354-
local target_header = path.join(root, "src", "x86", "ffitarget.h")
355-
if not os.isfile(target_header) then
356-
log.error("[libffi] src/x86/ffitarget.h missing")
357-
return false
419+
-- configure copies the target's ffitarget.h into include/; ffi.h does
420+
-- `#include <ffitarget.h>` and consumers need it too. Doing that copy
421+
-- unconditionally would bind the whole installed tree to one architecture,
422+
-- so install each target's header under its own name and generate a
423+
-- selector. Both files share the `LIBFFI_TARGET_H` guard, which is safe
424+
-- because a TU only ever includes the one the selector picks.
425+
local arch_headers = {
426+
{ dir = "x86", name = "ffitarget-x86.h" },
427+
{ dir = "aarch64", name = "ffitarget-aarch64.h" },
428+
}
429+
for _, arch in ipairs(arch_headers) do
430+
local from = path.join(root, "src", arch.dir, "ffitarget.h")
431+
if not os.isfile(from) then
432+
log.error("[libffi] src/%s/ffitarget.h missing", arch.dir)
433+
return false
434+
end
435+
os.cp(from, path.join(root, "include", arch.name))
358436
end
359-
os.cp(target_header, path.join(root, "include", "ffitarget.h"))
437+
438+
io.writefile(path.join(root, "include", "ffitarget.h"),
439+
"/* Generated by mcpp-index's compat.libffi descriptor, not configure.\n"
440+
.. " The installed tree is shared by every target, so the target header\n"
441+
.. " is selected by the consumer's compiler rather than copied in. */\n"
442+
.. "#ifndef LIBFFI_TARGET_SELECTOR_H\n"
443+
.. "#define LIBFFI_TARGET_SELECTOR_H\n"
444+
.. "#if defined(__aarch64__) || defined(__arm64__)\n"
445+
.. "# include <ffitarget-aarch64.h>\n"
446+
.. "#elif defined(__x86_64__) || defined(__i386__)\n"
447+
.. "# include <ffitarget-x86.h>\n"
448+
.. "#else\n"
449+
.. "# error \"compat.libffi: no ffitarget.h for this architecture (x86/x86_64 and aarch64 are packaged)\"\n"
450+
.. "#endif\n"
451+
.. "#endif\n")
360452

361453
return true
362454
end

‎tests/examples/libffi/tests/ffi.cpp‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <ffi.h>
99

1010
#include <dlfcn.h>
11+
#include <cstdarg>
1112
#include <cstdio>
1213
#include <cstring>
1314
#include <string>
@@ -26,6 +27,17 @@ int add3(int a, int b, int c) { return a + b + c; }
2627

2728
double scale(double v, float f) { return v * static_cast<double>(f); }
2829

30+
int sumv(int n, ...)
31+
{
32+
va_list ap;
33+
va_start(ap, n);
34+
int total = 0;
35+
for (int i = 0; i < n; ++i)
36+
total += va_arg(ap, int);
37+
va_end(ap);
38+
return total;
39+
}
40+
2941
} // namespace
3042

3143
int main()
@@ -59,7 +71,24 @@ int main()
5971
check(result == 6.0, "ffi_call dispatched scale(1.5, 4.0f) == 6.0");
6072
}
6173

62-
// ── 3. It is THIS build that ran, not the ecosystem payload's ────────
74+
// ── 3. Variadic call ─────────────────────────────────────────────────
75+
// aarch64 keeps an `is_variadic` flag in ffi_cif and passes the variadic
76+
// part on the stack; x86_64 saves the argument registers differently.
77+
// Both go through ffi_prep_cif_var, not ffi_prep_cif, so this is the only
78+
// test that reaches those paths.
79+
{
80+
ffi_cif cif{};
81+
ffi_type *args[3] = { &ffi_type_sint, &ffi_type_sint, &ffi_type_sint };
82+
check(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 3, &ffi_type_sint, args) == FFI_OK,
83+
"ffi_prep_cif_var on (int, ...) with two variadic ints");
84+
85+
int n = 2, a = 5, b = 7, result = 0;
86+
void *values[3] = { &n, &a, &b };
87+
ffi_call(&cif, FFI_FN(sumv), &result, values);
88+
check(result == 12, "ffi_call dispatched sumv(2, 5, 7) == 12");
89+
}
90+
91+
// ── 4. It is THIS build that ran, not the ecosystem payload's ────────
6392
// Same soname as `xim:libffi`, so one mapping wins silently. If the path
6493
// names the payload, this package compiled a library nobody loads.
6594
{

0 commit comments

Comments
 (0)