titzer / virgil

A fast and lightweight native programming language
1.22k stars 42 forks source link

[MacOS] Rosetta 2 refuses to run Virgil x86-64-darwin binaries #60

Open titzer opened 2 years ago

titzer commented 2 years ago

I recently ported Virgil to x86-64-darwin, i.e. 64-bit MacOS on Intel. I expected that the binaries generated would work automatically under Rosetta 2, but they apparently do not.

I could some help debugging some issues.

% aeneas bootstrap
% cd apps/HelloWorld/
% v3c-x86-64-darwin -output=/tmp HelloWorld.v3 
% /tmp/HelloWorld 
rosetta error: /tmp/HelloWorld: overlapping Mach-O segments
Trace/BPT trap: 5
%

Tinkering with the segment layouts in aeneas/src/x86-64/X86_64Darwin.v3 can get past that, but other errors remain. I am not sure what the rules are for Rosetta.

bjorn3 commented 2 years ago

I believe Rosetta 2 requires executables to use PIC (position independent code). PIE and fixed addresses are not supported I think. At least when linking a non-PIC executable you get the following warning: ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in ... from ...o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie

titzer commented 2 years ago

Thanks for that info! That makes sense. It's a bit of work to figure out how to emit linker tables and such on MacOS.

titzer commented 2 years ago

Steps to reproduce:

  1. clone the repo
  2. Run bin/dev/aeneas bootstrap
  3. Run bin/dev/v3c-x86-64-darwin -output=/tmp apps/HelloWorld/*.v3
  4. Run /tmp/HelloWord

The current error I get is "overlapping segments" but even rearranging the segments in the binary, I cannot get the program to start.