Skip to content

Instantly share code, notes, and snippets.

@dreness
Last active December 28, 2021 05:40
Show Gist options
  • Save dreness/5f5b80d04c02361ca7b5d7044b486f69 to your computer and use it in GitHub Desktop.
Save dreness/5f5b80d04c02361ca7b5d7044b486f69 to your computer and use it in GitHub Desktop.
The Powder Toy build produces malformed executable?

Working out of this repo: [email protected]:The-Powder-Toy/The-Powder-Toy.git

(base) andre@boom The-Powder-Toy % git remote -v
origin	[email protected]:The-Powder-Toy/The-Powder-Toy.git (fetch)
origin	[email protected]:The-Powder-Toy/The-Powder-Toy.git (push)

I had to change the SDL includes from two-level style to one-level style.

(base) andre@boom The-Powder-Toy % git diff
diff --git a/src/SDLCompat.h b/src/SDLCompat.h
index 599b265f..ef37e688 100644
--- a/src/SDLCompat.h
+++ b/src/SDLCompat.h
@@ -1,5 +1,5 @@
 #include "Config.h"
-#include "SDL2/SDL.h"
+#include "SDL.h"
 #ifdef INCLUDE_SYSWM
 # if defined(WIN)
 #  include <SDL2/SDL_syswm.h>
diff --git a/src/gui/interface/Mouse.h b/src/gui/interface/Mouse.h
index ecf57257..39a195b8 100644
--- a/src/gui/interface/Mouse.h
+++ b/src/gui/interface/Mouse.h
@@ -1 +1 @@
-#include "SDL2/SDL_mouse.h"
+#include "SDL_mouse.h"

Make a small test program to smoke-test SDL.

(base) andre@boom The-Powder-Toy % cat sdl-test.cpp 
#include <iostream>
#include <SDL.h>

int main(int argc, char **argv){
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }
    SDL_Quit();

    return 0;
}

build the SDL test

(base) andre@boom The-Powder-Toy % g++ sdl-test.cpp -o main $(sdl2-config --cflags --libs)

Look at the resulting binary

(base) andre@boom The-Powder-Toy % file main
main: Mach-O 64-bit executable arm64

Switch monacles and look again

(base) andre@boom The-Powder-Toy % otool -L main
main:
	/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib (compatibility version 19.0.0, current version 19.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1200.3.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)

Run the test program, check exit status

(base) andre@boom The-Powder-Toy % ./main ; echo $?
0

Ok, satisfied with SDL. Next is bootstrapping with meson as per the build instructions.

(base) andre@boom The-Powder-Toy % meson debug-build
The Meson build system
Version: 0.60.2
Source dir: /Users/andre/work/The-Powder-Toy
Build dir: /Users/andre/work/The-Powder-Toy/debug-build
Build type: native build
Project name: the-powder-toy
Project version: the.cake.is.a.lie
C compiler for the host machine: cc (clang 13.0.0 "Apple clang version 13.0.0 (clang-1300.0.29.30)")
C linker for the host machine: cc ld64 711
C++ compiler for the host machine: c++ (clang 13.0.0 "Apple clang version 13.0.0 (clang-1300.0.29.30)")
C++ linker for the host machine: c++ ld64 711
Host machine cpu family: aarch64
Host machine cpu: arm64
meson.build:35: WARNING: unsupported architecture: aarch64; you are on your own
Found pkg-config: /opt/homebrew/bin/pkg-config (0.29.2)
Run-time dependency luajit found: YES 2.1.0-beta3
Run-time dependency libcurl found: YES 7.64.1
Run-time dependency fftw3f found: YES 3.3.10
Run-time dependency threads found: YES
Run-time dependency zlib found: YES 1.2.11
Run-time dependency sdl2 found: YES 2.0.18
Message: SSE level explicitly configured but unavailable on macosx
Configuring Config.h using configuration
Configuring ElementNumbers.h using configuration
Configuring ToolNumbers.h using configuration
Build targets in project: 2

Found ninja-1.10.2.git.kitware.jobserver-1 at /Users/andre/miniforge3/bin/ninja

Build with ninja

(base) andre@boom debug-build % ninja
[361/361] Linking target powder
ld: warning: Linking with PIE, -image_base will be ignored

Technically not a failed build, although I'm not yet sure about that PIE warning.

Let's check the build output. It seems to report as the expected arch:

(base) andre@boom debug-build % file powder
powder: Mach-O 64-bit executable arm64

library linkage looks right

(base) andre@boom debug-build % otool -L powder
powder:
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
	/opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib (compatibility version 19.0.0, current version 19.0.0)
	/opt/homebrew/opt/luajit/lib/libluajit-5.1.2.dylib (compatibility version 2.1.0, current version 2.1.0)
	/usr/lib/libcurl.4.dylib (compatibility version 7.0.0, current version 9.0.0)
	/opt/homebrew/opt/fftw/lib/libfftw3f.3.dylib (compatibility version 10.0.0, current version 10.10.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1200.3.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)

objdump is sad :/

(base) andre@boom debug-build % objdump --all-headers powder

powder:	file format mach-o arm64

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: error: 'powder': Invalid/Unsupported object file format

lldb is sad

(base) andre@boom debug-build % lldb ./powder
(lldb) target create "./powder"
Current executable set to '/Users/andre/work/The-Powder-Toy/debug-build/powder' (arm64).
(lldb) r
error: Malformed Mach-o file
(lldb) q

... not surprisingly, this doesn't run for long:

(base) andre@boom debug-build % ./powder -h
zsh: killed     ./powder -h

... ok, what about dyld_info:

(base) andre@boom debug-build % dyld_info powder                                       
powder [arm64]:
    -platform:
        platform     minOS      sdk
           macOS     12.0      12.1   
    -segments:
        load-offset   segment section        sect-size  seg-size perm
        0x00000000    __TEXT                              6816KB r.x
        0x00004624             __text           6024320
        0x005C32A4             __stubs           22872
        0x005C8BFC             __init_offsets       36
        0x005C8C20             __const          477664
        0x0063D600             __gcc_except_tab 134268
        0x0065E27C             __cstring         66950
        0x0066E804             __unwind_info    219872
        0x006A42E8             __eh_frame        15632
        0x006A8000    __DATA_CONST                          80KB rw.
        0x006A8000             __got             16528
        0x006AC090             __const           53056
        0x006BC000    __DATA                                48KB rw.
        0x006BC000             __data             6712
        0x006BDA38             __thread_vars        96
        0x006BDA98             __thread_bss        585
        0x006BDCE8             __common          31760
        0x006C58F8             __bss               292
    -dependents:
        attributes     load path
                       /usr/lib/libz.1.dylib
                       /opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib
                       /opt/homebrew/opt/luajit/lib/libluajit-5.1.2.dylib
                       /usr/lib/libcurl.4.dylib
                       /opt/homebrew/opt/fftw/lib/libfftw3f.3.dylib
                       /usr/lib/libc++.1.dylib
                       /usr/lib/libSystem.B.dylib

... and the SDL test app for good measure:

(base) andre@boom debug-build % dyld_info main                                         
main [arm64]:
    -platform:
        platform     minOS      sdk
           macOS     12.0      12.1   
    -segments:
        load-offset   segment section        sect-size  seg-size perm
        0x00000000    __TEXT                                16KB r.x
        0x00003004             __text             3344
        0x00003D14             __stubs             300
        0x00003E40             __gcc_except_tab    172
        0x00003EEC             __cstring            17
        0x00003F00             __unwind_info       256
        0x00004000    __DATA_CONST                          16KB rw.
        0x00004000             __got               232
    -dependents:
        attributes     load path
                       /opt/homebrew/opt/sdl2/lib/libSDL2-2.0.0.dylib
                       /usr/lib/libc++.1.dylib
                       /usr/lib/libSystem.B.dylib

mach header via otool:

(base) andre@boom debug-build % otool -vvvvvv -h powder
powder:
Mach header
      magic  cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64    ARM64        ALL  0x00     EXECUTE    27       2456   NOUNDEFS DYLDLINK TWOLEVEL WEAK_DEFINES BINDS_TO_WEAK PIE MH_HAS_TLV_DESCRIPTORS

compare hexdumps

(base) andre@boom debug-build % xxd -l 256 main > test-hex              
(base) andre@boom debug-build % xxd -l 256 powder > powder-hex
(base) andre@boom debug-build % cdiff powder-hex test-hex 
2c2
< 00000010: 1b00 0000 9809 0000 8580 a100 0000 0000  ................
---
> 00000010: 1300 0000 e804 0000 8580 2100 0000 0000  ..........!.....
5c5
< 00000040: 0000 0100 0000 0000 0000 0000 0000 0000  ................
---
> 00000040: 0000 0000 0100 0000 0000 0000 0000 0000  ................
7c7
< 00000060: 0000 0000 0000 0000 1900 0000 c802 0000  ................
---
> 00000060: 0000 0000 0000 0000 1900 0000 d801 0000  ................
9,11c9,11
< 00000080: 0000 0100 0000 0000 0080 6a00 0000 0000  ..........j.....
< 00000090: 0000 0000 0000 0000 0080 6a00 0000 0000  ..........j.....
< 000000a0: 0500 0000 0500 0000 0800 0000 0000 0000  ................
---
> 00000080: 0000 0000 0100 0000 0040 0000 0000 0000  .........@......
> 00000090: 0000 0000 0000 0000 0040 0000 0000 0000  .........@......
> 000000a0: 0500 0000 0500 0000 0500 0000 0000 0000  ................
14,15c14,15
< 000000d0: 2446 0100 0000 0000 80ec 5b00 0000 0000  $F........[.....
< 000000e0: 2446 0000 0200 0000 0000 0000 0000 0000  $F..............
---
> 000000d0: 0430 0000 0100 0000 100d 0000 0000 0000  .0..............
> 000000e0: 0430 0000 0200 0000 0000 0000 0000 0000  .0..............

It is code signed...

andre@boom build-release % codesign -d -vvvvv powder         
Executable=/Users/andre/work/The-Powder-Toy/build-release/powder
Identifier=com.dreness.powder
Format=Mach-O thin (arm64)
CodeDirectory v=20400 size=31798 flags=0x0(none) hashes=988+2 location=embedded
VersionPlatform=1
VersionMin=786432
VersionSDK=786688
Hash type=sha256 size=32
CandidateCDHash sha256=723c019241b624296fe51892688e16c7da409c4c
CandidateCDHashFull sha256=723c019241b624296fe51892688e16c7da409c4c6336d9895ef9be388d828857
Hash choices=sha256
CMSDigest=723c019241b624296fe51892688e16c7da409c4c6336d9895ef9be388d828857
CMSDigestType=2
Executable Segment base=0
Executable Segment limit=3162112
Executable Segment flags=0x1
Page size=4096
    -2=34cc4a4a61b9fe9f1655f0a6861ca020930eb29a9af1e8e77bdefd55d1da4231
CDHash=723c019241b624296fe51892688e16c7da409c4c
Signature size=8980
Authority=Developer ID Application: Andre LaBranche (UNKTN53737)
Authority=Developer ID Certification Authority
Authority=Apple Root CA
Timestamp=Dec 27, 2021 at 7:40:35 PM
Info.plist=not bound
TeamIdentifier=UNKTN53737
Sealed Resources=none
Internal requirements count=1 size=180
@dreness
Copy link
Author

dreness commented Dec 27, 2021

synalize can't see it..
synalize-it-broken-powder
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment