Is it possible to convert a shared library (someLib.so) to a static library? (someLib.a)
Asked
Active
Viewed 2.5k times
36
-
Why to do this in first place?Just to take advantage of Static library over shared Library? – Raulp Jun 11 '12 at 05:56
-
2@softly the problem with shared libraries is they are more susceptible to security issues, either due to a shared library being replaced with a malicious version which is then loaded into your application at run-time, or because two applications are using the same code, if one somehow manages to modify the code then it will affect the other. – Rob Gilliam Aug 08 '12 at 08:15
-
@RobGilliam you can also say precisely the opposite - having a shared library means it can be replaced if you find a security issue, and if it was statically linked you're stuck with that security problem. – James Moore May 16 '17 at 22:08
-
iOS doesn't allow nested dependencies, so converting a shared library to a static one (if it were possible) could be advantageous for library developers to simplify use of their library (instead of including `mylib.framework` AND `dependency.framework`, you just include `mylib.framework` and the dependency is part of it) – stevendesu Oct 08 '19 at 15:30
2 Answers
22
No. (At least for ELF shared library). A shared library is a simple object (so stands for shared object). A static library is a collection of objects. In the process of building the shared library you combine several objects and you lose some of the information which would be needed to retrieve them.

Axalo
- 2,953
- 4
- 25
- 39

AProgrammer
- 51,233
- 8
- 91
- 143
-
5Just for interest - which needed information gets lost? Those "inner objects" are still "call able" - and given the corresponding call-details, what is missing? – Bastian Ebeling Nov 29 '13 at 10:21
-
1The name of the combined objects is the most obvious candidate. It is possible that with debug information available you have enough information to solve the puzzle and reconstruct everything, but for sure it was not the purpose of the format. – AProgrammer Nov 29 '13 at 10:29
-
Okay, this seems to be interesting. But do you know, there are tools, which tell they can convert from dynamic to static? (Without source) – Bastian Ebeling Dec 03 '13 at 22:15
-
As I can not edit my last comment - what would be, if one has the .dll,.lib and .exp (for the windows case) - is it possible then? – Bastian Ebeling Dec 12 '13 at 10:37
-
@AProgrammer Would it be possible if I do not need the object names? For example, theoretically it should be possible to extract `puts` function from `libc.so.6` as `puts.o` since the exact function entry point address and code-path (i.e. sequence of instructions) are known. If not, what is the barrier? – SuibianP Aug 09 '22 at 08:49
-
@SuibianP It is possible that you can reconstruct all the information needed in some cases. For sure that information is not readily available. If you have to interpret machine code, change calls which are internal to the .so to make them in PIC fashion and so on... Yes that's possible under certain assumptions but you end up in the intelligent disassembler world, you aren't staying in the object file manipulation one. – AProgrammer Aug 14 '22 at 16:28
-
@AProgrammer Can you elaborate on the call-changing part? In my illusion it should be possible to include whatever codepath in the object and be done with it, without messing with raw assembler. Or, in other words, can I convert an entire dynamic library into a static one by including all instructions and inserting symbols at the entry points, and if not, why? – SuibianP Aug 16 '22 at 01:51
10
YES if you have the source code of the shared library.
NO if you don't have the source code of the shared library.

Benjamin W.
- 46,058
- 19
- 106
- 116

Alok Save
- 202,538
- 53
- 430
- 533
-
16
-
I have the source code for shared library. I want to use for iOS mobile application development. Could you please tell me the steps to convert shared library to static library(.a). – Pushpa Raja Aug 02 '19 at 09:40
-
4
-