Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert dynamically linked application to statically one?

I have an application, say gedit, which is dynamically linked and I don't have the source code. So I can not compile it as I like. what I want to do is to make it statically linked and move it to the system which doesn't have the necessary libraries to run that application. So is it possible to do it and how?

like image 817
cosimo Avatar asked Jun 30 '13 13:06

cosimo


People also ask

Is dynamic linking better than static?

They might perform better than statically linked programs if several programs use the same shared routines at the same time. By using dynamic linking, you can upgrade the routines in the shared libraries without relinking. This form of linking is the default and no additional options are needed.

What is a dynamically statically linked file?

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk. This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.

What are the benefits of dynamic link over static linking?

Dynamic linking has the following advantages over static linking: Multiple processes that load the same DLL at the same base address share a single copy of the DLL in physical memory. Doing this saves system memory and reduces swapping.

Can you statically link a shared library?

You can't statically link a shared library (or dynamically link a static one). The flag -static will force the linker to use static libraries (. a) instead of shared (. so) ones.


1 Answers

It is theoretically possible. You basically have to do the same job that the dynamic linker does, with some modifications, i.e.

  • dump all sections from the original file
  • resolve symbols
  • locate libraries
  • instead of loading them into memory, assemble them into a "virtual image"
  • resolve internal links
  • dump the whole thing in a independent file.

So objdump, readelf, and objcopy will be some of your friends.

The task is not easy and the result will be neither automatic, nor (probably) stable.

You may want to check out this code by someone else that tried the same, by actually intercepting the dynamic linker (i.e. all steps above, except the last) and dumping the results to disk.

It is based on this tool, so it's anyone's bet whether it works on the newest kernels.

(It probably doesn't - and you need at least to patch it to reflect the new structures. This is my attempt at doing so. Caveat emptor).

like image 149
LSerni Avatar answered Oct 03 '22 08:10

LSerni