LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What's the difference between static and dynamically linked library... (https://www.linuxquestions.org/questions/linux-newbie-8/whats-the-difference-between-static-and-dynamically-linked-library-829365/)

trist007 08-30-2010 01:56 PM

What's the difference between static and dynamically linked library...
 
Libraries have always been ambiguous to me. I would love to have a clear understanding of them.

Here's what I know so far...

Ok I know that libraries are a bunch of functions and definitions in C that binaries use.

I also understand that static libraries are used by the binaries in /sbin:/usr/sbin:/usr/local/sbin

and dynamically linked libraries are used by the binaries in /bin:/usr/bin:/usr/local/bin

So why have statically linked and dynamically linked libraries?

I know that dynamically linked libraries are used to save space. Otherwise each binary would need its own private copy of a library.
So dynamically libraries are used to save space.

Is this understanding correct so far?

Now for my questions...

1. Where are the static libraries and the dynamically linked libraries located in the file structure?

2. Where does ldconfig fit into all of this?

3. Would a rootkit affect both statically and dynamically linked libraries? I would think yes unless the libraries only have read permissions.

4. If the above is true, is there any reason for your libraries to have write permission other than when you want to edit them?

Tinkster 08-30-2010 02:06 PM

Quote:

Originally Posted by trist007 (Post 4082668)
Libraries have always been ambiguous to me. I would love to have a clear understanding of them.

Here's what I know so far...

Ok I know that libraries are a bunch of functions and definitions in C that binaries use.

I also understand that static libraries are used by the binaries in /sbin:/usr/sbin:/usr/local/sbin

and dynamically linked libraries are used by the binaries in /bin:/usr/bin:/usr/local/bin

So why have statically linked and dynamically linked libraries?

Where did that come from? I've never heard this before, nor
can I (using ldd run against any program in sbin) confirm
the theory in actual installations.

Quote:

Originally Posted by trist007 (Post 4082668)
I know that dynamically linked libraries are used to save space. Otherwise each binary would need its own private copy of a library.
So dynamically libraries are used to save space.

Is this understanding correct so far?

That's correct.

Quote:

Originally Posted by trist007 (Post 4082668)
Now for my questions...

1. Where are the static libraries and the dynamically linked libraries located in the file structure?

Just go have a look.
dynamic libraries are called .so. something (commonly
with a version mumble at the end). Static ones are .la

Quote:

Originally Posted by trist007 (Post 4082668)
2. Where does ldconfig fit into all of this?

It let's apps find shared libs easily by caching info.


Quote:

Originally Posted by trist007 (Post 4082668)
4. If the above is true, is there any reason for your libraries to have write permission other than when you want to edit them?

[/quote]
They shouldn't need to be writable

weshutchins 08-30-2010 02:43 PM

Static libraries are linked at compile time so they are contained in whatever executable (.exe) that uses them. Dynamic libraries are linked at run-time and are contained in their own file (.dll).

trist007 08-30-2010 03:07 PM

So then a rootkit can affect either static/dynamic assuming an attacker gains root and adds write permissions to the dynamic link libraries (*.so) and overwrites them with the rootkit's version of those libraries. As for the static libraries, they are already embedded into the binaries correct? So take the 'w' binary. A rootkit would replace that binary and of course the embedded library in the binary(I understand that the binary has the library built into the binary, since the file itself is nothing but 0s and 1s) so it would only affect that one binary and not all the others. As for the dynamic libraries a rootkit just needs to replace the *.so files with its own libraries and essentially all binaries that use DLL's will be affected. So in a sense static libraries are more resilient. Also, I would think static library binaries run a little faster than DLL binaries. What other significant differences are there between static/dynamic?

1. If static libraries are built into the binary, say the 'hostname' binary, then why would there also be a separate .la file?

2. So ldconfig refreshes the cache so that apps can find their DLL's easily. So I would imagine ldconfig is run at boot? Also, after installing a module or program that uses DLL, it would be necessary to run ldconfig as well no?

Tinkster 08-30-2010 04:53 PM

Quote:

Originally Posted by trist007 (Post 4082733)
1. If static libraries are built into the binary, say the 'hostname' binary, then why would there also be a separate .la file?

So new statically linked programs you may want to
build can be linked against it.

Quote:

Originally Posted by trist007 (Post 4082733)
2. So ldconfig refreshes the cache so that apps can find their DLL's easily. So I would imagine ldconfig is run at boot? Also, after installing a module or program that uses DLL, it would be necessary to run ldconfig as well no?

Depends on your distro & preferences. Most distros these
days will probably kick ldconfig off on boot; I choose not
to, and run ldconfig manually each time I install something
new ... everyones mileage may vary.



Cheers,
Tink

trist007 09-19-2010 08:40 PM

OK another question. Since static binaries in /sbin have the libraries built-in how portable are they?

Can I take that binary and move it from a slackware distro to a centos distro?

Will it work as long as the kernel version is the same? What checks need to be made?

What about moving the binary from slackware to bsd?


All times are GMT -5. The time now is 02:49 PM.