From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 5AE45AFE35A; Thu, 11 Apr 2024 18:26:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5AE45AFE35A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1712849217; bh=6lcB64t6dv9iqnfpMVNJ5yLJD3Pr1ETo/yebNNtebc0=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=klNFq6gKUpl63+rfaCjWzDvXVjhwuMmH2eP4CPsLHAj8nKgwhbdVCww4YWAlqrf2Y IzsJ/llQchWo1mVd0kX877LwIQvVa4HvRyDWIfCFlGJkwElo1hKVCFkXISGpr1Qqgu m4SS0K9TzEDDxs9NhhfuOGftZSpQUw0AhrkUZoGI= Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C34ACAFE34D for ; Thu, 11 Apr 2024 18:26:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C34ACAFE34D Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-516d6898bebso7212503e87.3 for ; Thu, 11 Apr 2024 08:26:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712849177; x=1713453977; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pcRhkCM2V1SKYwSTUWC3iJn7aD2Omp5A+fqqHsqpi9A=; b=tKQNiB8vrUa5sFmnP5yaC5wonCWpCvscZW+J9gvlGF83g/DJQzW7S6ko6xKayOE+kx hOW4kijbe+bquddW3HHU1f6T3DNontDMeoNelNr1+2MflIwSpfVHzFmr9g3vEFtDMfRj p6HEeMQU+Kg54vvhFlUGzEjbMqGVkOOzA5SAJcoRBWCq4cF22ugYWdAkz8F7icWK2blL aJ/ijoh6okmFaG9xuyjeHMD4ev9R2IZhq5tB7aQpkxb4JgCisc137RLdquI3nHfEsHlE EFfTBm65eiP+ZRjFX0yTzgZmWH9fviuwRcbVWjZyTN8XKb5P2cEmYl+Ujxd91+VXQ3JI lwHw== X-Gm-Message-State: AOJu0Yy9IVYYctn9U0PbdcocOXdEGzuIjcikxz1TOvM43rQkRB0pVMlI s8lbGIQbHCpmsJC93sU3bGE8q6Ak+W0sXHLqjOYGQgDQO184kQ2sNJwIOg== X-Google-Smtp-Source: AGHT+IG968IvN76gnkxklxK4R3dagwpQLpW4SObevEX8YV/8xDK8W/jvje2a5P6fGk9cZYkZyChlXw== X-Received: by 2002:a05:6512:609:b0:515:c07e:bfd0 with SMTP id b9-20020a056512060900b00515c07ebfd0mr40526lfe.28.1712849176763; Thu, 11 Apr 2024 08:26:16 -0700 (PDT) Received: from pony.. ([5.181.62.126]) by smtp.gmail.com with ESMTPSA id b5-20020ac25e85000000b00516d6419a70sm235623lfq.85.2024.04.11.08.26.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 08:26:16 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , Maxim Kokryashkin Date: Thu, 11 Apr 2024 16:22:08 +0300 Message-Id: <0968f450b5ddf3a14ef9ef865bafad99a5e626b4.1712841312.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 4/4][v2] OSX/iOS/ARM64: Fix bytecode embedding in Mach-O object file. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall 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