Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Mach-O executable with a static lib?

Tags:

c++

ios

mach-o

Suppose you have

  1. a pre-built iOS executable app (for simulator or device).
  2. a pre-built static archive library static library which among other things contains c++ static initializers.

Now it should be possible to merge the two built products to produce the a new iOS executable which is like the old one, except that it is now also linked with the additional static library, and on execution will run the static library's static initializers.

Which tool (if any) could help solve this merge problem?

Edit: An acceptable solution is also to dynamically load the library using dlopen. The whole purpose of this is for application testing, so the re-linked app will never see app store.

like image 613
krukow Avatar asked Nov 15 '12 20:11

krukow


1 Answers

How a compiler work (in a simple explanation)

The most popular C++ compilers (like say, GCC), work by translating all the C++ (and Obj-C, C, etc...) code to ASM.

Then it calls the appropriate assembler for the target processor, and create the object binaries.

Then it calls the linker, that search on those binaries for the symbols that explain what links with what. A common optimisation that linkers can do, is also strip of the final binary anything from the statically linked libraries that was not used, other common optimisation is not attempt to link at all unused libraries.

Also finally, the linker removes the things that only it needed.

What this mean in your case

You have a library, the library has the linking symbols. You also has a executable, that one had its linking symbols stripped, in fact depending on how it was optimised the internal jumps might be only a couple of jmp instructions to arbitrary addresses on the code. No machine, can do what you want in a automatic manner, because you don't have the needed information on the executable.

How to do it anyway

You need to disassemble the executable, figure on your own where are the function calls, and then manually reassemble it with your library, changing those functions call to jump to addresses in your library instead.

This process is sometimes used by game moders to change the video drivers of old games (for example to update their OpenGL version, or to force Glide games to use some newer drivers, and so on).

So if you want to do that anyway (I warn you: it is absurdly crazy to do though...) ask those guys :) I don't remember right now anyone to point to you, but they exist.

Analogy

When you are in normal linking phase, the compiled object files are like a source code that the machine understands, full of function calls as needed.

After it is compiled, all function calls became goto.

So if you are a linker tasked in doing what you want to do, imagine that you would be reading a source code filled with goto to random places in the code (sometimes even to inside loops) and that you have to somehow figure what ones of those you want to change to jump to the new part you are trying to paste there.

like image 59
speeder Avatar answered Sep 22 '22 20:09

speeder