Say I compile a simple application called myproggy that depends on the libfoo dynamic library. Assuming I have another dynamic library libbaz that implements absolutely the same interface (in other words it has definitions of the same functions as libfoo), is it possible to modify the myproggy to depend upon libbaz without the need for rebuild?

I can easily do this in the case when I can dynamically load the library, but what if I can't, and I also can't re-compile the library?

I am also aware of the security issues related to this.

  • if myproggy is statically linked to libfoo, then the code of libfoo has become an integral part of myproggy and can't be replaced in any way without rebuilding myproggy. It can't be replaced with libbaz, but also not with a newer version of libfoo. – Bart van Ingen Schenau Sep 1 '14 at 10:50
  • 1
    No, I never mentioned statically linked libfoo. - It would not make sense to ask this question if I do static linking... :) – DejanLekic Sep 1 '14 at 10:57
  • @BartvanIngenSchenau quite possible using the right tools to modify the binaries directly. Lot of work, obviously, changing the call to the static library to another to a dynamic library is probably easier. – jwenting Sep 1 '14 at 10:58
  • @DejanLekic: Maybe I misread your second paragraph. It wasn't meant as an answer anyway. – Bart van Ingen Schenau Sep 1 '14 at 10:59

If it really does implement exactly the same interface,m then a simple rename should be sufficient.

Otherwise, yes you can - using a binary editor, you can find the location of the system calls to the dll and modify them to call the new library. Hackers do this (or similar techniques) all the time to remove copy-protection mechanisms.

In addition, Microsoft has a tool (Fakes) that is used for unit testing by rewriting the binary to call different methods at runtime, so you can slip in a modified stub for a real library call for testing purposes. This uses their tool Detours that:

contains utilities to attach arbitrary DLLs and data segments (called payloads) to any Win32 binary

I used to use a tool called Purify that instrumented (ie edited the binary) of running applications to inject performance and quality-checking code.

  • That is what I was hoping for. Yes, I am limited with the same amount of characters, but it should not be difficult to write a tool that finds a name which avoids collision with existing libraries... – DejanLekic Sep 1 '14 at 10:58
  • 1
    You can dump the exports from the dll (use depends.exe or dumpbin) and then re-create a dll with the same library names. You can put whatever code you like in those functions, even if it simply calls the desired function in the new dll. – gbjbaanb Sep 1 '14 at 11:01
  • Note that DLLs on Windows do not only have exports by name, but also by ordinals (16-bit? number identifying an export). A drop-in replacement library should also have the same ordinals for its exports in case the application imports by ordinal. – Lars Viklund Sep 1 '14 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.