3

Why isn't it possible* to "re-link" a native shared library (DLL) into an executable file, as if they had been statically linked? Is the DLL missing any required information?

*Note: Or is it actually posible? If it is, please let me know, but through searches I've come to the conclustion that it's not possible.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Check DLL to lib, in an answer for this thread: http://stackoverflow.com/questions/424032/how-to-link-a-dll-statically – Pablo Jun 06 '11 at 07:01
  • 1
    It's not a generally solvable problem, because the EXE may rely on dynamic loading, obtaining function addresses via the DLL's HMODULE and a function name, etc. – Damien_The_Unbeliever Jun 06 '11 at 07:05
  • @Pablo: Interesting, I'll look at it, thanks. @Damien_The_Unbeliever: For the sake of this question, please assume the exe doesn't explicitly depend on this fact to be true. :) – user541686 Jun 06 '11 at 07:09

1 Answers1

1

It isn't directly possible.

When an EXE loads a DLL (via LoadLibrary) there's a lot of work done by the DLL loader to patch adresses. You can't just combine a DLL as is with an exe, because its adresses are wrong if it's not dynamically loaded.

On the other hand, a LIB is statically linked: no loading involved, no adress fixing, everything works without further job when you launch the program.

What is possible to do is to convert a DLL and EXE back to OBJ and link them together statically.

user703016
  • 37,307
  • 8
  • 87
  • 112
  • @Heandel: Did you by any chance happen to read my reply comment to @Damien? – user541686 Jun 06 '11 at 07:27
  • @Heandel: Yeah, I know what you mean by everything being dynamic, but isn't it a little different if you're not explicitly calling `LoadLibrary`? In that case, the executable should be (relatively) easily modifiable to allow the import thunks to directly call an embedded library, no? – user541686 Jun 06 '11 at 07:32
  • @Heandel: Also, how do you convert a DLL or EXE back to OBJ? Isn't it missing some information? – user541686 Jun 06 '11 at 07:35
  • @Heandel: Hm... when I try doing that with NDISASM/NASM, I get: `error: Win32 COFF does not correctly support relative references to absolute addresses`... are you sure that's possible? – user541686 Jun 06 '11 at 13:09