Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org,
	Sergey Kaplun <skaplun@tarantool.org>,
	Maxim Kokryashkin <m.kokryashkin@tarantool.org>
Subject: [Tarantool-patches] [PATCH luajit 4/4][v2] OSX/iOS/ARM64: Fix bytecode embedding in Mach-O object file.
Date: Thu, 11 Apr 2024 16:22:08 +0300	[thread overview]
Message-ID: <0968f450b5ddf3a14ef9ef865bafad99a5e626b4.1712841312.git.sergeyb@tarantool.org> (raw)
In-Reply-To: <cover.1712841312.git.sergeyb@tarantool.org>

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


  parent reply	other threads:[~2024-04-11 15:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11 13:22 [Tarantool-patches] [PATCH luajit 0/4][v2] Mach-O generation fixes Sergey Bronnikov via Tarantool-patches
2024-04-11 13:22 ` [Tarantool-patches] [PATCH luajit 1/4][v2] ci: add a workflow for testing with AVX512 Sergey Bronnikov via Tarantool-patches
2024-04-12 10:27   ` Sergey Kaplun via Tarantool-patches
2024-04-16 11:53     ` Sergey Bronnikov via Tarantool-patches
2024-04-18  8:24       ` Sergey Kaplun via Tarantool-patches
2024-05-05 12:29         ` Maxim Kokryashkin via Tarantool-patches
2024-04-11 13:22 ` [Tarantool-patches] [PATCH luajit 2/4][v2] test: introduce a helper read_file Sergey Bronnikov via Tarantool-patches
2024-04-12 10:47   ` Sergey Kaplun via Tarantool-patches
2024-04-16 12:02     ` Sergey Bronnikov via Tarantool-patches
2024-04-11 13:22 ` [Tarantool-patches] [PATCH luajit 3/4][v2] OSX/iOS/ARM64: Fix generation of Mach-O object files Sergey Bronnikov via Tarantool-patches
2024-04-12 11:19   ` Sergey Kaplun via Tarantool-patches
2024-04-16 15:29     ` Sergey Bronnikov via Tarantool-patches
2024-04-11 13:22 ` Sergey Bronnikov via Tarantool-patches [this message]
2024-04-12 11:27   ` [Tarantool-patches] [PATCH luajit 4/4][v2] OSX/iOS/ARM64: Fix bytecode embedding in Mach-O object file Sergey Kaplun via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0968f450b5ddf3a14ef9ef865bafad99a5e626b4.1712841312.git.sergeyb@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=estetus@gmail.com \
    --cc=m.kokryashkin@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 4/4][v2] OSX/iOS/ARM64: Fix bytecode embedding in Mach-O object file.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox