[Tarantool-patches] [PATCH luajit 4/4][v2] OSX/iOS/ARM64: Fix bytecode embedding in Mach-O object file.
Sergey Bronnikov
estetus at gmail.com
Thu Apr 11 16:22:08 MSK 2024
From: Mike Pall <mike>
Thanks to Carlo Cabrera.
(cherry picked from commit b98b37231bd2dcb79e10b0f974cefd91eb0d7b3a)
Mach-O FAT object files generated by LuaJIT for ARM64 Had
an incorrect format due to the usage of the 32-bit version of
FFI structure. This patch adds the 64-bit structure definition
and uses it for ARM64.
Sergey Bronnikov:
* added the description and the test for the problem
Part of tarantool/tarantool#9595
---
src/jit/bcsave.lua | 14 ++++-
...-865-cross-generation-mach-o-file.test.lua | 55 +++++++++++++++++--
2 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua
index 7aec1555..069cf1a3 100644
--- a/src/jit/bcsave.lua
+++ b/src/jit/bcsave.lua
@@ -491,6 +491,18 @@ typedef struct {
mach_nlist sym_entry;
uint8_t space[4096];
} mach_fat_obj;
+typedef struct {
+ mach_fat_header fat;
+ mach_fat_arch fat_arch[2];
+ struct {
+ mach_header_64 hdr;
+ mach_segment_command_64 seg;
+ mach_section_64 sec;
+ mach_symtab_command sym;
+ } arch[2];
+ mach_nlist_64 sym_entry;
+ uint8_t space[4096];
+} mach_fat_obj_64;
]]
local symname = '_'..LJBC_PREFIX..ctx.modname
local isfat, is64, align, mobj = false, false, 4, "mach_obj"
@@ -499,7 +511,7 @@ typedef struct {
elseif ctx.arch == "arm" then
isfat, mobj = true, "mach_fat_obj"
elseif ctx.arch == "arm64" then
- is64, align, isfat, mobj = true, 8, true, "mach_fat_obj"
+ is64, align, isfat, mobj = true, 8, true, "mach_fat_obj_64"
else
check(ctx.arch == "x86", "unsupported architecture for OSX")
end
diff --git a/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua b/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua
index 04fb5495..07988948 100644
--- a/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua
+++ b/test/tarantool-tests/lj-865-cross-generation-mach-o-file.test.lua
@@ -2,7 +2,7 @@ local tap = require('tap')
local test = tap.test('lj-865-cross-generation-mach-o-file')
local utils = require('utils')
-test:plan(1)
+test:plan(2)
-- The test creates an object file in Mach-O format with LuaJIT
-- bytecode and checks the validity of the object file fields.
@@ -114,6 +114,11 @@ typedef struct
uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags;
} mach_header;
+typedef struct
+{
+ mach_header; uint32_t reserved;
+} mach_header_64;
+
typedef struct {
uint32_t cmd, cmdsize;
char segname[16];
@@ -121,6 +126,13 @@ typedef struct {
uint32_t maxprot, initprot, nsects, flags;
} mach_segment_command;
+typedef struct {
+ uint32_t cmd, cmdsize;
+ char segname[16];
+ uint64_t vmaddr, vmsize, fileoff, filesize;
+ uint32_t maxprot, initprot, nsects, flags;
+} mach_segment_command_64;
+
typedef struct {
char sectname[16], segname[16];
uint32_t addr, size;
@@ -128,6 +140,13 @@ typedef struct {
uint32_t reserved1, reserved2;
} mach_section;
+typedef struct {
+ char sectname[16], segname[16];
+ uint64_t addr, size;
+ uint32_t offset, align, reloff, nreloc, flags;
+ uint32_t reserved1, reserved2, reserved3;
+} mach_section_64;
+
typedef struct {
uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize;
} mach_symtab_command;
@@ -139,6 +158,13 @@ typedef struct {
uint32_t value;
} mach_nlist;
+typedef struct {
+ int32_t strx;
+ uint8_t type, sect;
+ uint16_t desc;
+ uint64_t value;
+} mach_nlist_64;
+
typedef struct
{
int32_t magic, nfat_arch;
@@ -161,6 +187,19 @@ typedef struct {
mach_nlist sym_entry;
uint8_t space[4096];
} mach_fat_obj;
+
+typedef struct {
+ mach_fat_header fat;
+ mach_fat_arch fat_arch[2];
+ struct {
+ mach_header_64 hdr;
+ mach_segment_command_64 seg;
+ mach_section_64 sec;
+ mach_symtab_command sym;
+ } arch[2];
+ mach_nlist_64 sym_entry;
+ uint8_t space[4096];
+} mach_fat_obj_64;
]]
local function create_obj_file(name, arch)
@@ -176,7 +215,7 @@ end
-- Parses a buffer in the Mach-O format and returns
-- the FAT magic number and `nfat_arch`.
-local function read_mach_o(buf)
+local function read_mach_o(buf, hw_arch)
local res = {
header = {
magic = 0,
@@ -185,8 +224,11 @@ local function read_mach_o(buf)
fat_arch = {},
}
+ local is64 = hw_arch == 'arm64'
+
-- Mach-O FAT object.
- local mach_fat_obj_type = ffi.typeof('mach_fat_obj *')
+ local mach_fat_obj_type = ffi.typeof(is64 and
+ 'mach_fat_obj_64 *' or 'mach_fat_obj *')
local obj = ffi.cast(mach_fat_obj_type, buf)
-- Mach-O FAT object header.
@@ -221,9 +263,11 @@ end
--
local SUM_CPUTYPE = {
arm = 7 + 12,
+ arm64 = 0x01000007 + 0x0100000c,
}
local SUM_CPUSUBTYPE = {
arm = 3 + 9,
+ arm64 = 3 + 0,
}
-- The function builds Mach-O FAT object file and retrieves
@@ -248,7 +292,7 @@ local SUM_CPUSUBTYPE = {
-- $ lipo -archs empty.o
-- x86_64 arm64
local function build_and_check_mach_o(subtest, hw_arch)
- assert(hw_arch == 'arm')
+ assert(hw_arch == 'arm' or hw_arch == 'arm64')
subtest:plan(4)
-- FAT_MAGIC is an integer containing the value 0xCAFEBABE in
@@ -272,7 +316,7 @@ local function build_and_check_mach_o(subtest, hw_arch)
local mach_o_buf = utils.tools.read_file(mach_o_obj_path)
assert(mach_o_buf ~= nil and #mach_o_buf ~= 0, 'cannot read an object file')
- local mach_o = read_mach_o(mach_o_buf)
+ local mach_o = read_mach_o(mach_o_buf, hw_arch)
-- Teardown.
assert(os.remove(mach_o_obj_path), 'remove an object file')
@@ -296,5 +340,6 @@ local function build_and_check_mach_o(subtest, hw_arch)
end
test:test('arm', build_and_check_mach_o, 'arm')
+test:test('arm64', build_and_check_mach_o, 'arm64')
test:done(true)
--
2.34.1
More information about the Tarantool-patches
mailing list