Skip to content
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

Initial Linux support #97

Merged
merged 4 commits into from
Oct 1, 2024
Merged

Initial Linux support #97

merged 4 commits into from
Oct 1, 2024

Conversation

Dadoum
Copy link
Contributor

@Dadoum Dadoum commented Jul 7, 2024

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

Addresses part of #11

@p-x9
Copy link
Owner

p-x9 commented Jul 8, 2024

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

@Dadoum
Copy link
Contributor Author

Dadoum commented Aug 19, 2024

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?).

@p-x9
Copy link
Owner

p-x9 commented Aug 22, 2024

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.

Comment on lines -96 to +88
guard let baseAddress = $0.baseAddress else {
return nil
}
switch hashType {
switch hashType {
case .sha1:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CC_SHA1(baseAddress, length, &digest)
return Data(digest)
return Data(Insecure.SHA1.hash(data: data))
case .sha256, .sha256_truncated:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(baseAddress, length, &digest)
return Data(digest)
return Data(SHA256.hash(data: data))
case .sha384:
var digest = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
CC_SHA384(baseAddress, length, &digest)
return Data(digest)
return Data(SHA384.hash(data: data))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@p-x9
Copy link
Owner

p-x9 commented Sep 12, 2024

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.)

@Dadoum
Copy link
Contributor Author

Dadoum commented Sep 12, 2024

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)

@p-x9
Copy link
Owner

p-x9 commented Sep 12, 2024

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

@Dadoum
Copy link
Contributor Author

Dadoum commented Sep 12, 2024

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
Copy link
Contributor Author

Dadoum commented Sep 16, 2024

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

@p-x9
Copy link
Owner

p-x9 commented Sep 17, 2024

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")) 

@Dadoum
Copy link
Contributor Author

Dadoum commented Sep 17, 2024

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

Co-authored-by: p-x9 <chi.iosdev@gmail.com>
@p-x9
Copy link
Owner

p-x9 commented Oct 1, 2024

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.

@Dadoum
Copy link
Contributor Author

Dadoum commented Oct 1, 2024

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

@p-x9
Copy link
Owner

p-x9 commented Oct 1, 2024

OK, Thanks for your much work.

@p-x9 p-x9 merged commit c3ef0ac into p-x9:main Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants