Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct x64 machine code generation - considerations for implementation for macOS and Windows #5374

Closed
ylluminate opened this issue Jun 13, 2020 · 7 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@ylluminate
Copy link
Contributor

As we all know, x64 code generation is currently only available for Linux. The purpose of this issue is to track, discuss and collaborate on efforts to get this also implemented for macOS and Windows (or to branch out into other issues to get more specific for each).

@ylluminate ylluminate added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Jun 13, 2020
@sheerun
Copy link
Contributor

sheerun commented Jun 14, 2020

Here are some resources for MacOS Mach-O format and assembler:

Something about statically compiled binaries:

https://developer.apple.com/library/archive/qa/qa1118/_index.html

Apple does not support statically linked binaries on Mac OS X. A statically linked binary assumes binary compatibility at the kernel system call interface, and we do not make any guarantees on that front. Rather, we strive to ensure binary compatibility in each dynamically linked system library and framework.

If your project absolutely must create a statically linked binary, you can get the Csu (C startup) module from Darwin and try building crt0.o for yourself. Obviously, we won't support such an endeavor.

Probably it also means that fully statically compiled binary won't be accepted to App Store (you cannot use direct syscalls), and is not backward or forward-compatible, so these are huge downsides. Also statically compiled crt0.o is not enough, you also need statically compiled libSystem which simply isn't currently available. So probably it means we need to live with it and figure out how to link system libraries (at least System) with V compiler.

So the proper way to compile assembly that uses libc calls (libSystem) to binary on MacOS is:

as main.s -o main.o && ld main.o -lc -o main

There are some interesting facts when it comes to linking when you read source of go-lang linker: https://golang.org/src/cmd/link/internal/ld/macho.go For example some offsets need to be maintained for code signing to be working

@ylluminate
Copy link
Contributor Author

ylluminate commented Jun 14, 2020

Due to Apple's stance on static bins, could parts of Darling (GitHub & other associated repos) be used to help accomplish this?

@bjorn3
Copy link

bjorn3 commented Jun 14, 2020

Linux is one of the few systems that has a stable syscall interface. On all other systems you must use the system libraries to interface with the kernel. For macOS cross compilation there seems to be https://github.com/tpoechtrager/osxcross. For Windows cross compilation you can use mingw.

@medvednikov
Copy link
Member

Exactly. V is going to be able to generate dynamically linked binaries.

For now it's Linux/static only.

@ylluminate
Copy link
Contributor Author

For Windows @nord501 noted that it should be pe.v and asks whether or not we should use kernel32 or ntdll.lib...

@ylluminate
Copy link
Contributor Author

@nord501 is putting forth a valiant and successful effort with Windows implementation and noted this last night that he now has a working hello world and is digging into V's x64 gen for integration. Source:
https://github.com/nord501/pe.v/blob/master/pe.v

@ylluminate
Copy link
Contributor Author

macOS 11 (Big Sur) updates: https://gist.github.com/FiloSottile/7125822#gistcomment-3578065

@vlang vlang locked and limited conversation to collaborators Sep 22, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants