OSX/iOS: Always generate 64 bit non-FAT Mach-O object files.

Reported by Sergey Bronnikov. #1181
This commit is contained in:
Mike Pall 2024-04-19 00:31:06 +02:00
parent d2fe2a6d46
commit 7110b93567

View File

@ -439,24 +439,12 @@ typedef struct
{ {
mach_header; uint32_t reserved; mach_header; uint32_t reserved;
} mach_header_64; } mach_header_64;
typedef struct {
uint32_t cmd, cmdsize;
char segname[16];
uint32_t vmaddr, vmsize, fileoff, filesize;
uint32_t maxprot, initprot, nsects, flags;
} mach_segment_command;
typedef struct { typedef struct {
uint32_t cmd, cmdsize; uint32_t cmd, cmdsize;
char segname[16]; char segname[16];
uint64_t vmaddr, vmsize, fileoff, filesize; uint64_t vmaddr, vmsize, fileoff, filesize;
uint32_t maxprot, initprot, nsects, flags; uint32_t maxprot, initprot, nsects, flags;
} mach_segment_command_64; } mach_segment_command_64;
typedef struct {
char sectname[16], segname[16];
uint32_t addr, size;
uint32_t offset, align, reloff, nreloc, flags;
uint32_t reserved1, reserved2;
} mach_section;
typedef struct { typedef struct {
char sectname[16], segname[16]; char sectname[16], segname[16];
uint64_t addr, size; uint64_t addr, size;
@ -466,133 +454,58 @@ typedef struct {
typedef struct { typedef struct {
uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize;
} mach_symtab_command; } mach_symtab_command;
typedef struct {
int32_t strx;
uint8_t type, sect;
int16_t desc;
uint32_t value;
} mach_nlist;
typedef struct { typedef struct {
int32_t strx; int32_t strx;
uint8_t type, sect; uint8_t type, sect;
uint16_t desc; uint16_t desc;
uint64_t value; uint64_t value;
} mach_nlist_64; } mach_nlist_64;
typedef struct
{
int32_t magic, nfat_arch;
} mach_fat_header;
typedef struct
{
int32_t cputype, cpusubtype, offset, size, align;
} mach_fat_arch;
typedef struct { typedef struct {
struct {
mach_header hdr;
mach_segment_command seg;
mach_section sec;
mach_symtab_command sym;
} arch[1];
mach_nlist sym_entry;
uint8_t space[4096];
} mach_obj;
typedef struct {
struct {
mach_header_64 hdr; mach_header_64 hdr;
mach_segment_command_64 seg; mach_segment_command_64 seg;
mach_section_64 sec; mach_section_64 sec;
mach_symtab_command sym; mach_symtab_command sym;
} arch[1];
mach_nlist_64 sym_entry; mach_nlist_64 sym_entry;
uint8_t space[4096]; uint8_t space[4096];
} mach_obj_64; } mach_obj_64;
typedef struct {
mach_fat_header fat;
mach_fat_arch fat_arch[2];
struct {
mach_header hdr;
mach_segment_command seg;
mach_section sec;
mach_symtab_command sym;
} arch[2];
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 symname = '_'..LJBC_PREFIX..ctx.modname
local isfat, is64, align, mobj = false, false, 4, "mach_obj" local cputype, cpusubtype = 0x01000007, 3
if ctx.arch == "x64" then if ctx.arch ~= "x64" then
is64, align, mobj = true, 8, "mach_obj_64" check(ctx.arch == "arm64", "unsupported architecture for OSX")
elseif ctx.arch == "arm" then cputype, cpusubtype = 0x0100000c, 0
isfat, mobj = true, "mach_fat_obj"
elseif ctx.arch == "arm64" then
is64, align, isfat, mobj = true, 8, true, "mach_fat_obj_64"
else
check(ctx.arch == "x86", "unsupported architecture for OSX")
end end
local function aligned(v, a) return bit.band(v+a-1, -a) end local function aligned(v, a) return bit.band(v+a-1, -a) end
local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE.
-- Create Mach-O object and fill in header. -- Create Mach-O object and fill in header.
local o = ffi.new(mobj) local o = ffi.new("mach_obj_64")
local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align) local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, 8)
local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12}, arm64={0x01000007,0x0100000c} })[ctx.arch]
local cpusubtype = ({ x86={3}, x64={3}, arm={3,9}, arm64={3,0} })[ctx.arch]
if isfat then
o.fat.magic = be32(0xcafebabe)
o.fat.nfat_arch = be32(#cpusubtype)
end
-- Fill in sections and symbols. -- Fill in sections and symbols.
for i=0,#cpusubtype-1 do o.hdr.magic = 0xfeedfacf
local ofs = 0 o.hdr.cputype = cputype
if isfat then o.hdr.cpusubtype = cpusubtype
local a = o.fat_arch[i] o.hdr.filetype = 1
a.cputype = be32(cputype[i+1]) o.hdr.ncmds = 2
a.cpusubtype = be32(cpusubtype[i+1]) o.hdr.sizeofcmds = ffi.sizeof(o.seg)+ffi.sizeof(o.sec)+ffi.sizeof(o.sym)
-- Subsequent slices overlap each other to share data. o.seg.cmd = 0x19
ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0]) o.seg.cmdsize = ffi.sizeof(o.seg)+ffi.sizeof(o.sec)
a.offset = be32(ofs) o.seg.vmsize = #s
a.size = be32(mach_size-ofs+#s) o.seg.fileoff = mach_size
end o.seg.filesize = #s
local a = o.arch[i] o.seg.maxprot = 1
a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface o.seg.initprot = 1
a.hdr.cputype = cputype[i+1] o.seg.nsects = 1
a.hdr.cpusubtype = cpusubtype[i+1] ffi.copy(o.sec.sectname, "__data")
a.hdr.filetype = 1 ffi.copy(o.sec.segname, "__DATA")
a.hdr.ncmds = 2 o.sec.size = #s
a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym) o.sec.offset = mach_size
a.seg.cmd = is64 and 0x19 or 0x1 o.sym.cmd = 2
a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec) o.sym.cmdsize = ffi.sizeof(o.sym)
a.seg.vmsize = #s o.sym.symoff = ffi.offsetof(o, "sym_entry")
a.seg.fileoff = mach_size-ofs o.sym.nsyms = 1
a.seg.filesize = #s o.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)
a.seg.maxprot = 1 o.sym.strsize = aligned(#symname+2, 8)
a.seg.initprot = 1
a.seg.nsects = 1
ffi.copy(a.sec.sectname, "__data")
ffi.copy(a.sec.segname, "__DATA")
a.sec.size = #s
a.sec.offset = mach_size-ofs
a.sym.cmd = 2
a.sym.cmdsize = ffi.sizeof(a.sym)
a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs
a.sym.nsyms = 1
a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs
a.sym.strsize = aligned(#symname+2, align)
end
o.sym_entry.type = 0xf o.sym_entry.type = 0xf
o.sym_entry.sect = 1 o.sym_entry.sect = 1
o.sym_entry.strx = 1 o.sym_entry.strx = 1