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

Unable to Load Shared Library 'capstone' or One of its Dependencies on MacOS #30

Closed
ventouris opened this issue Sep 19, 2021 · 10 comments
Closed
Assignees
Labels

Comments

@ventouris
Copy link

I am working on a MacBook and since the NuGet package does not include the Mac binaries, I try to build and add reference to it. However, I am lost in the process.

I cloned the repository, opened the Capstone.NET.sln file and ran Release. Then, in my project, I added a reference to

/Capstone.NET-master/Gee.External.Capstone/bin/Release/X/Gee.External.Capstone.dll

I tried with all net40, net45 and netstandard2.0. In all of them, I get the following error

Unhandled exception. System.TypeInitializationException: The type initializer for 'TestProject.Program' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'capstone' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libcapstone, 1): image not found
   at Gee.External.Capstone.NativeCapstoneImport.CreateDisassembler(DisassembleArchitecture disassembleArchitecture, NativeDisassembleMode disassembleMode, IntPtr& pDisassembler)
   at Gee.External.Capstone.NativeCapstone.CreateDisassembler(DisassembleArchitecture disassembleArchitecture, NativeDisassembleMode disassembleMode) in /tools/Capstone.NET-master/Gee.External.Capstone/NativeCapstone.cs:line 86
   at Gee.External.Capstone.CapstoneDisassembler`8..ctor(DisassembleArchitecture disassembleArchitecture, TDisassembleMode disassembleMode) in /tools/Capstone.NET-master/Gee.External.Capstone/CapstoneDisassembler.cs:line 735
   at Gee.External.Capstone.Arm64.CapstoneArm64Disassembler..ctor(Arm64DisassembleMode disassembleMode) in /tools/Capstone.NET-master/Gee.External.Capstone/Arm64/CapstoneArm64Disassembler.cs:line 19
   at Gee.External.Capstone.CapstoneDisassembler.CreateArm64Disassembler(Arm64DisassembleMode disassembleMode) in /tools/Capstone.NET-master/Gee.External.Capstone/CapstoneDisassembler.cs:line 299
   at TestProject.Program..cctor() in /[MacVersion] Il2CppInspector/Il2CppInspector/Program.cs:line 26
   --- End of inner exception stack trace ---
@9ee1
Copy link
Owner

9ee1 commented Sep 23, 2021

Hello. I don't have a Mac to test this one but I am pretty sure the dylib file needs to be named capstone.dylib and it has to be placed either in a system wide directory (I don't know what it is on Mac but it would be equivalent to C:/Windows/System32 on Windows) or in the same directory as your application's executable.

Can you try that?

@9ee1 9ee1 self-assigned this Sep 23, 2021
@9ee1 9ee1 added the question label Sep 23, 2021
@9ee1
Copy link
Owner

9ee1 commented Sep 23, 2021

I am wrong about the naming convention. I found an MSDN document that suggests the dylib file can be named either capstone.dylib or libcapstone.dylib. Here is a link for your reference: https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform

Also make sure you're compiling the .NET Solution to x64 not x86.

@ventouris
Copy link
Author

I found the issue. Managed to include the dll but then I got an error for missing kernel32.dll. It looks it's not supported for MacOS :(

@9ee1
Copy link
Owner

9ee1 commented Sep 23, 2021

That's interesting. The only code path where it would need to load kernel32.dll is if you're using the DLL compiled for .NET Framework. If you're using the DLL compiled for .NET Core/Standard, it should not even attempt to load kernel32.dll.

Can you make sure you're linking your application against the DLL compiled for .NET Core/Standard? To be honest, I am not even sure how you are able to run the .NET Framework DLLs on Mac.

@ventouris
Copy link
Author

Thank you again. I appreciate your help. Here are the steps I do.

  1. Open the Capstone Solution with Visual Studio. Don't change anything and choose Build for "Release | X64"
  2. Open my solution, double check my framework with right click on project and then Build -> General. In the Target Framework, it says ".Net Core 3.1"
  3. Then right click on my project, then Add -> Add reference and select the bin/x64/Release/net40/Gee.External.Capstone.dll
  4. Then right click on my project, then Add -> Add existing file and select the bin/x64/Release/net40/capstone.dll

Then when I build it, I get the missing kernel32.dll error. In a Windows laptop, when I install it through NuGet, I am able to run it without any error for the same project.

@9ee1
Copy link
Owner

9ee1 commented Sep 24, 2021

It's no problem. I am happy to help out.

Your issue is you're linking against the .NET Framework DLLs. In step 3, you're adding a reference to bin/x64/Release/net40/Gee.External.Capstone.dll: the "net40" indicates you're linking against the .NET Framework 4.0 DLL, which is only supported on Windows and will attempt to load kernel32.dll.

Instead, add a reference to bin/x64/Release/netstandard2.0/Gee.External.Capstone.dll: the netstandard2.0 indicates you want to link against the .NET Standard DLL which is more cross-platform compatible and will NOT attempt to load kernel32.dll.

As a side note, on a Mac you don't need to do step 4 because capstone.dll is a Windows only library. In addition, if you reference the NuGet package from a .NET Core project, it should automatically reference the .NET Standard DLL I mentioned above. All you need to do is simply copy the capstone.dylib (or libcapstone.dylib) library you manually compiled for Mac and place it in your working directory (typically that will be your project's bin directory).

@ventouris
Copy link
Author

Thanks. I am trying again with the netstandard2.0 without step 4, but I get the original error. Cannot find capstone. When I build the capstone solution in Visual Studio on my Mac, I don't get any .dylib file. I get only the .dll files. So not sure where to find that capstone.dylib to make it work.

@9ee1
Copy link
Owner

9ee1 commented Sep 24, 2021

capstone.dylib is the equivalent of capstone.dll on Windows but for Mac. It is dynamic link library that is the result of compiling native Capstone on Mac (not Capstone.NET). You have to compile native Capstone on Mac in order to create it.

capstone.dylib is not packaged in either the NuGet package or this repository. I don't have access to a Mac so I could not do it myself.

If you want to use a precompiled version of capstone.dylib, you'll need to follow the official Capstone Installation Instructions (look towards the bottom of the page for the section titled "MacOS - Binaries". Alternatively, you can compile your own (look for the section titled "Mac OSX - core engine").

Make sure you read the full instructions, including the part about where the library is installed and setting the DYLD_LIBRARY_PATH environment variable so applications can find it, including your application.

@ventouris
Copy link
Author

Wow. I thought that everything I need is in this repository. Didn't think about downloading manually the capstone. Doing what you said, fixed my issue. Brew install capstone and then export to the path. Thank you

@9ee1
Copy link
Owner

9ee1 commented Sep 25, 2021

Glad you got it working. Good luck with your project.

@9ee1 9ee1 closed this as completed Sep 25, 2021
@9ee1 9ee1 changed the title Unable to load shared library 'capstone' or one of its dependencies. Unable to Load Shared Library 'capstone' or One of its Dependencies on MacOS Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants