Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable Deployment of Delphi-Generated Dylib on OSX

I would like to deploy a .dylib on OSX, which has been created with Delphi. This .dylib should be loadable by third-party applications.

This is going to seem like a duplicate question, but after plenty of searching, I can't find an answer for it. It's the same issue as this: https://forums.embarcadero.com/thread.jspa?messageID=592417

The problem is that the .dylib requires libcgunwind.1.0.dylib, but that it cannot find it when being run from a third-party application. As a test, I tried copying libcgunwind.1.0.dylib into usr/lib, and that worked. When OSX cannot locate the .dylib, it will always search in usr/lib. Unfortunately, I do not want to do this as a final solution, because it would require elevated permissions, and seems like a heavy handed way of dealing with a simple problem.


Inspecting a bit closer, I used otool to inspect my .dylib, and it gave me the following path: @rpath/libcgunwind.1.0.dylib.

The trouble is, there's no accounting for what paths will be listed in @rpath, when your .dylib is being run by a third-party application. For this to work, the application would always have to provide an absolute path to libcgunwind.1.0.dylib.

The obvious solution is to use install_name_tool to replace @rpath with @loader_path. If my logic is correct, this should cause my .dylib to always find libcgunwind.1.0.dylib, as long as they are in the same folder. This makes sense, as the .dylib ought to be in charge of finding its own dependencies.

So, I tried the following command line:

install_name_tool -change @rpath/libcgunwind.1.0.dylib @loader_path/libcgunwind.1.0.dylib libTest.dylib

and received this message:

install_name_tool: file not in an order that can be processed (local relocation entries out of place): libTest.dylib

I did some searching around for this error message, and I haven't been able to find any information on it. I have to assume that install_name_tool is expecting some specific conventions found in a gcc or llvm built .dylib, and that Delphi's compiler is not providing those conventions.

I did some digging around in Delphi's compiler dccosx, and checked the command-line arguments Delphi is sending to it, but I can't find any useful options. This way of using libcgunwind.1.0.dylib, seems to be implicit to the compiler, and not something I can adjust.

I'm not suggesting that this is the right way to tackle the issue, these are just the latest things that I've tried. If you can come up with a different way of working around the problem, please share your ideas!

like image 803
AudioGL Avatar asked May 09 '14 19:05

AudioGL


1 Answers

The install_name_tool seems to require that certain parts of the dylib are in a certain order like "local relocation entries" then "symbol table", "local symbols", ... The error message means that this order is not as expected.

My suggestion is that you try to patch the dylib file. I do that all the time with OSX executables because I need to tell them which dylib versions they are compatible with. This fixes a couple of bugs in a Delphi OSX application. A dylib has a very similar structure as the executable. My experience is that patching these files is not as complicated as it sounds.

You can find the description of the file structure here: https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html

The LC_LOAD_DYLIB section is what you'll need to look at. You should be able to replace the dylib names with something longer as this section usually has some extra padding.

like image 90
Sebastian Z Avatar answered Oct 15 '22 00:10

Sebastian Z