LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 08-30-2010, 01:56 PM   #1
trist007
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 972

Rep: Reputation: 56
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?
 
Old 08-30-2010, 02:06 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally Posted by trist007 View Post
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 View Post
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 View Post
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 View Post
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 View Post
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
 
Old 08-30-2010, 02:43 PM   #3
weshutchins
Member
 
Registered: Oct 2003
Location: Phoenix, AZ USA
Posts: 31

Rep: Reputation: 0
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).
 
Old 08-30-2010, 03:07 PM   #4
trist007
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 972

Original Poster
Rep: Reputation: 56
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?
 
Old 08-30-2010, 04:53 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally Posted by trist007 View Post
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 View Post
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
 
Old 09-19-2010, 08:40 PM   #6
trist007
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 972

Original Poster
Rep: Reputation: 56
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] g++ dynamically linked library not being found posop Programming 6 04-14-2010 03:20 PM
Difference in MySQL packages: glib(static) and Dynamically link Swakoo Linux - General 2 08-01-2007 10:36 PM
Dynamically linked libraries.. vishalbutte Programming 6 04-17-2006 08:54 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM


All times are GMT -5. The time now is 05:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration