views:

459

answers:

3

I'm not sure if this is even possible, but given an executable file (foo.exe), with has many libraries which has been linked statically.

Is there any software that extract from this file the .lib ( or .a ) that lay inside the executable ?

Thanks.

+12  A: 

Incredibly unlikely since, typically, you don't get the entire contents of the library injected into your executable.

You only get enough to satisfy all the undefined symbols. This may actually only be a small proportion of the library. A library generally consists of a set of object files of which only those that are required are linked into your executable.

For example, if the only thing you called in the C runtime library was exit(), you would be very unlikely to have the printf() family of functions in your executable.

If you linked with the object files directly, you may have a chance, since they would be included whether used or not (unless your linker is a smart one).

But even that would be a Herculean task as there may be no information in the executable as to what code sections came from specific object files. It's potentially doable but, if there's another way, I'd be looking at that first.

Let me clarify the typical process:

  1. Four object files, a.o, b.o, c.o and d.o contain the functions a(), b(), c() and d() respectively. They are all added to the abcd.a archive.
  2. They are all standalone (no dependencies) except for the fact that b() calls c().
  3. You have a main program which calls a() and b() and you compile it then link it with the abcd.a library.
  4. The linker drags a.o and b.o out of the library and into your executable, satisfying the need for a() and b() but introducing a need for c(), because b() needs it.
  5. The linker then drags c.o out of the library and into your executable, satisfying the need for c(). Now all undefined symbols are satisfied, the executable is done and dusted, you can run it when ready.

At no stage in that process was d.o dragged into your executable so you have zero hope of getting it out.

Update: Re the "if there's another way, I'd be looking at that first" comment I made above, you have just stated in a comment to one of the other answers that you have the source code that made the libraries you want extracted. I need to ask: why can you not rebuild the libraries with that source? That seems to me a much easier solution than trying to recreate the libraries from a morass of executable code.

paxdiablo
The scenario consists on have several custom object files, link them into a single custom library file, and static link that libraries files inside the executable.If all libraries are custom ones, aren't they "full embedded" into the executable?Actually i don't want to get the source code, i only want to get the library files.
HyLian
If every object file in the library is needed to satisfy all undefined symbols then yes, they would all be there. But that's not necessarily the case.
paxdiablo
Let's step back. Why do you not have the .sl/.a files?
paxdiablo
"unless your linker is an incredibly smart one" - such as Visual C++ since 2002; compile with /Gy and link with /OPT:REF to do function-level linking.
MSalters
Okay, removing the "incredibly" bit - don't want to give those coders over at MS a big head :-)
paxdiablo
But it's a safe bet that the *relevant* parts of the statically linked library are in the executable, or the executable would not work
Andomar
Yes, @Andomar, that is correct. Getting them out is still not an easy task and may be made moot by the fact that the OP has stated they have the source that made the libraries.
paxdiablo
A: 

It seems like you're asking for a decompiler. Such tools are difficult to use (probably impossible for mildly sophisticated C++) and if there is any other way of solving your problem, including taking a couple months to rewrite the libraries, I'd recommend that course of action.

Like pax pointed out, even if you did use a decompiler, you would only get the library functions that the executable called.

Dan Hook
But i don't want to fully decompile the executable, i don't need the source code, what i need is the library files that were linked in that executable, and link another different executable with that libraries.
HyLian
Do you have the header files for the library?
Dan Hook
Yes I have, actually i have all the source code that generate those libraries.
HyLian
If you have the source code, why do you not just remake the libraries?
paxdiablo
I have the source code and the final executable. Building all the libraries take soooo much time and i want to change only a small part of the final aplication. Besides i have curiosity about how linker works :)
HyLian
+1  A: 

Imagine having 10 books in language you don't understand, without covers, title pages, page numbers and chapters. Some of the books can be incomplete. All pages are shuffled together so it is impossible to find out where is the beginning and end of each book.(each page is a function call) Now try to find page 123 of book 5 (let's say it is mentioned above function Exit()).

Well, IT IS possible...

beermann
And you have the original manuscript for book 5. Definitely easier to just reprint book 5 from the manuscript.
caspin