MachOKit icon indicating copy to clipboard operation
MachOKit copied to clipboard

Initial Linux support

Open Dadoum opened this issue 1 year ago • 2 comments

I just adapted part of XNU headers in the folder. Hopefully I didn't break anything on macOS.

Addresses part of #11

Dadoum avatar Jul 07 '24 16:07 Dadoum

Thanks.

As for MachOImage, I don't think all the features are available on Linux. Therefore, I was thinking that everything could be wrapped up in #if canImport(Darwin).

Similar problems also occurred when supporting Linux with ELFKit, but it seems that for some header files it is necessary to use #if !__has_include to avoid duplicate definitions. https://github.com/p-x9/ELFKit/blob/ca073217cfc55921a33f01aa2360b56bcb78db66/Sources/ELFKitC/include/elf32.h#L37

p-x9 avatar Jul 08 '24 01:07 p-x9

As for MachOImage, I don't think all the features are available on Linux. Therefore, I was thinking that everything could be wrapped up in #if canImport(Darwin).

Since they compile, they can be useful to make a mach-o loader on Linux so I think it's okay to leave them enabled.

Similar problems also occurred when supporting Linux with ELFKit, but it seems that for some header files it is necessary to use #if !__has_include to avoid duplicate definitions. https://github.com/p-x9/ELFKit/blob/ca073217cfc55921a33f01aa2360b56bcb78db66/Sources/ELFKitC/include/elf32.h#L37

Now working on that (I also noticed the use of Apple's Crypto library bumps the requirements from macOS 10.13 to 10.15, is that okay for you?).

Dadoum avatar Aug 19 '24 22:08 Dadoum

Thanks @Dadoum .

Since they compile, they can be useful to make a mach-o loader on Linux so I think it's okay to leave them enabled.

That's true.

Now working on that (I also noticed the use of Apple's Crypto library bumps the requirements from macOS 10.13 to 10.15, is that okay for you?).

I thought the way it was written when using Apple's Crypto library was simpler and better than the current implementation, so the required OS version bump was not a problem for me.

p-x9 avatar Aug 22 '24 19:08 p-x9

Hello, @Dadoum I tried to build on macOS and I got some errors related to include in the header file. I have made some fixes, can I push some additional commits to this pull request? (I am using the virtual environment Ubuntu 22.04 for testing in a Linux environment.)

p-x9 avatar Sep 12 '24 09:09 p-x9

Yes you can. I tried to work with the updated branch but my swift compiler now crashes when trying to compile it (I need to find a fix, or to wait for a swift update for fedora)

Dadoum avatar Sep 12 '24 09:09 Dadoum

It was not possible to push additional commits to this pull request. (This may be due to the fact that it is the main branch.)

In my environment, I was able to build on both MacOS and Ubuntu with the following changes.

0001-Move-c-headers-for-linux-platforms.patch 0001-Update-supported-version-of-platforms.patch 0001-Do-not-run-MachOPrintTests-in-a-Linux-environment.patch

p-x9 avatar Sep 12 '24 13:09 p-x9

I can't test it unfortunately because I can't build PrebuiltLoader.swift unfortunately. I tried on Swift 5 and 6 on my main Fedora machine. Will commit your changes though.

For the record, here is a snippet of what outputs Swift 6 experimental dev version from Ubuntu on my computer:

2.      Compiling with effective version 5.10
3.      While walking into 'prebuilt_loader' (in module 'MachOKitC')
4.      While verifying VarDecl 'isPrebuilt' (in module 'MachOKitC')
 #0 0x000055c6ec6f3f67 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/dadoum/KDLs/swift-DEVELOPMENT-SNAPSHOT-2024-09-11-a-ubuntu20.04/usr/bin/swift-frontend+0x886ef67)
 #1 0x000055c6ec6f1c6e llvm::sys::RunSignalHandlers() (/home/dadoum/KDLs/swift-DEVELOPMENT-SNAPSHOT-2024-09-11-a-ubuntu20.04/usr/bin/swift-frontend+0x886cc6e)
 #2 0x000055c6ec6f45df SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f315f64fd00 __restore_rt (/lib64/libc.so.6+0x40d00)
 #4 0x00007f315f6a84a4 __pthread_kill_implementation (/lib64/libc.so.6+0x994a4)
 #5 0x00007f315f64fc4e gsignal (/lib64/libc.so.6+0x40c4e)
 #6 0x00007f315f637902 abort (/lib64/libc.so.6+0x28902)
 #7 0x000055c6e74802ec (anonymous namespace)::Verifier::verifyChecked(swift::AbstractStorageDecl*) ASTVerifier.cpp:0:0
 #8 0x000055c6e747fc41 (anonymous namespace)::Verifier::verifyChecked(swift::VarDecl*) ASTVerifier.cpp:0:0
 #9 0x000055c6e746ad38 (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) ASTVerifier.cpp:0:0
#10 0x000055c6e74819ba (anonymous namespace)::Traversal::doIt(swift::Decl*) ASTWalker.cpp:0:0
#11 0x000055c6e7485daf (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) ASTWalker.cpp:0:0
#12 0x000055c6e7484bd9 (anonymous namespace)::Traversal::visit(swift::Decl*) ASTWalker.cpp:0:0
#13 0x000055c6e74819aa (anonymous namespace)::Traversal::doIt(swift::Decl*) ASTWalker.cpp:0:0

Dadoum avatar Sep 12 '24 16:09 Dadoum

It's due to the bitfields used in the structure!

Dadoum avatar Sep 16 '24 19:09 Dadoum

I also used bit field in dyld_cache_format.h. I am therefore wondering why the PrebuiltLoader part is an error. (Is it a bug in swift 6.0 or a platform-specific issue?)

I am using the following environment and it works.

- Ubuntu 22.04
- swift-5.10.1-RELEASE-ubuntu22.04-aarch64

If you go to the MachOKit directory and then run the following command, the MachOKit-enabled interpreter will start up.

swift run -repl

The interpreter was then tested by typing:

import Foundation
import MachOKit

let machO = MachOFile(url: URL(fileURLWithPath: "Path To mach-o file")) 

p-x9 avatar Sep 17 '24 11:09 p-x9

Apple just released Swift 6, with a fedora build that does no longer have the issue.

Dadoum avatar Sep 17 '24 12:09 Dadoum

Hello @Dadoum I am sorry for the delay, but I have been able to confirm that it works correctly on both macOS/Linux. I will merge it if it is not a problem for you.

p-x9 avatar Oct 01 '24 11:10 p-x9

It is not a problem for me, you can merge it.

Dadoum avatar Oct 01 '24 11:10 Dadoum

OK, Thanks for your much work.

p-x9 avatar Oct 01 '24 11:10 p-x9