Linux

Thursday, May 26, 2005

Linux Dynamic Linking

Linux allows dynamic linking of libraries during the time the executable is loaded or when required (shared libraries are usually of .so.x.y.z)

The shared object can be created using the steps mentioned in [3].
Suppose the shared library is named libmylib.so
If you create an executable using the shared object while compilation of the executable
gcc source.c –lmylib –o myapp


In this case when myapp is loaded/started the shared object mylib is loaded.This is the very basic method of using a shared library.

There is a way to load/unload the dl libraries as needed using the dlopen,dlclose routines
Use –ldl while linking and include dlfcn.h in the executable

The related functions are

dlopen()

The dlopen function opens a library and prepares it for use.

void * dlopen(const char *filename, int flag);

If filename an absolute path, dlopen() will just try to use it .Otherwise, dlopen() will search for the library in the following order:

*List of directories in the user's LD_LIBRARY_PATH environment variable.

*The list of libraries specified in /etc/ld.so.cache (which is generated from /etc/ld.so.conf).
/lib, followed by /usr/lib

Note the order here; this is the reverse of the order used by the old a.out
loader. The old a.out loader, when loading a program, first searched /usr/lib, then /lib .This shouldn't normally matter, since a library should only be in one or the otherdirectory (never both)

the value of flag must be either
RTLD_LAZY, meaning ``resolve undefined symbols as code
from the dynamic library is executed'', or
RTLD_NOW, meaning ``resolve all undefined symbols before dlopen() returns and fail if this cannot be done''.
RTLD_GLOBAL may be optionally or'ed with either value inflag, meaning that the external symbols defined in the library will be made available to subsequently loaded libraries

If the libraries depend on each other (e.g., X depends on Y), then you need to load the dependees first (in this example, load Y first, and then X).

dlerror()

Errors can be reported by calling dlerror(), which returns a string describing the error from the last call todlopen(), dlsym(), or dlclose(). One oddity is that after calling dlerror(), future calls to dlerror() will return NULL until another error has been encountered.

dlsym()
dlsym() looks up the value of a symbol in a given (opened) library.

void * dlsym(void *handle, char *symbol);
the handle is the value returned from dlopen, and symbol is a NULL-terminated string.

dlclose()

The converse of dlopen() is dlclose(), which closes a DL library. The dl library maintains link counts for dynamic file handles, so a dynamic library is not actually deallocated until dlclose has been called on it as many times as dlopen has succeeded on it.

This is an example i came across from which i learnt how to use the above functions


#include "stdio.h"
#include "stdlib.h"
#include "dlfcn.h"
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
}


The program should be compiled linking dl

gcc -o foo foo.c -ldl

Library constructor and destructor functions


Libraries should export initialization and cleanup routines using the gcc

__attribute__((constructor)) and
__attribute__((destructor)) function attributes.

Constructor routines are executed before dlopen returns (or before main() is started if the library is loaded at load time).
Destructor routines are executed before dlclose returns (or after exit() or completion of main() if the library is loaded at load time).

The C example prototypes for these functions are:
void __attribute__ ((constructor)) my_init(void);
void __attribute__ ((destructor)) my_fini(void);


Shared libraries must not be compiled with the gcc arguments ``-nostartfiles'' or ``-nostdlib''. If those arguments are used, the constructor/destructor routines will not be executed

Related commands:

ldd
Displays a list of the shared libraries each program requires. For example if your executable is a.out and you need to know what all libraries this a.out requires for operation you can invoke the command ldd as ldd a.out
In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories
Or it can be added in the configuration file /etc/ld.so.conf

ldconfig

creates the necessary links and cache (for use by the run-time linker, ld.so) to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/usr/lib and /lib). ldconfig checks the header and file names of the libraries it encounters when determining which versions should have their links updated. ldconfig ignores symbolic links when scanning for libraries.ldconfig should normally be run by the super-user as it may require write permission on some root owned directories and files. It is normally run automatically at bootup, from /etc/rc, or manually whenever new DLL's are installed.

nm :
The nm command can report the list of symbols in a given library. It works on both static and shared libraries. For a given library it lists the symbol names defined, each symbol's value, and the symbol's type. It can also identify where the symbol was defined in the source code (by filename and line number), if that information is available in the library (see the -l option).
The type is displayed as a letter lowercase means that the symbol is local, while uppercase means that the symbol is global (external). Typical symbol types include T (a normal definition in the code section), D (initialized data section), B (uninitialized data section), U (undefined; the symbol is used by the library but not defined by the library), and W (weak; if another library also defines this symbol, that definition overrides this one).

Related links/references:

[1]http://www.linuxjournal.com/article/6463
[2]
http://www.linuxjournal.com/article/1059
[3]
http://www-106.ibm.com/developerworks/linux/library/l-shobj/

15 Comments:

Anonymous Anonymous said...

These might be acknowledged and the certification will greatly assist
in landing excellent jobs in interior design. In terms of furniture,
chaise-longues, bureaus and winged armchairs are
Georgian staples. This will help you to get ideas that you may not think of, which
you can incorporate into your project.

My web site - Home Interior Decoration Design Photograph

March 02, 2013 1:21 AM  
Anonymous Anonymous said...

Hi, i think that i saw you visited my website thus
i came to “return the favor”.I am trying to find things to improve my site!
I suppose its ok to use a few of your ideas!!

Stop by my web blog: is quantrim safe

March 02, 2013 6:40 AM  
Anonymous Anonymous said...

And well-built twin-gear juicers minimize oxidization much better than masticating juicers.
You also want to be sure that the juicer you are purchasing has a long warranty (of ten years or more), so that you can get parts replaced if anything
malfunctions. If you are looking for a juicer which best fits your budget, then we would like
to tell you that not all juicers that are available in the market juice everything well.


My blog - BJE200XL Juicer

March 03, 2013 1:08 AM  
Anonymous Anonymous said...

Just do more in the future than you do now, and do that routinely.
Mostly everybody knows what's healthy and what's not. Even with
the warnings in place, doctors still prescribe the injections.


Also visit my web site :: weight management programs in charleston sc

March 03, 2013 2:09 AM  
Anonymous Anonymous said...

This article provides clear idea designed for the new people of blogging,
that in fact how to do running a blog.

Also visit my web blog ukmediaroom.com
my web page :: quantrim consumer reviews

March 03, 2013 3:47 AM  
Anonymous Anonymous said...

Good day! I could have sworn I've been to this website before but after browsing through some of the post I realized it's new
to me. Anyhow, I'm definitely glad I found it and I'll be book-marking and checking back frequently!



Feel free to visit my web blog; What is quantrim

March 05, 2013 1:49 PM  
Anonymous Anonymous said...

These are often also one of the wedding favors given to the
guests as "Thank you" gifts for attending. If you want to
choose decorations that are a bit more modern, balloons can be used.
How many princesses down through history have been disappointed from kissing
frogs that didn't turn out to be Prince Charmings.

Here is my page ... cheap wedding decor ideas

March 07, 2013 1:39 AM  
Anonymous Anonymous said...

re destined for a beach wedding, you may be interested in beach wedding cakes.
Cooking magazines like Gourmet, Bon Appetit and Cooking
Light are a good place to read about current wedding
cake flavor trends. Based on each of these themes, there are collections for each category.


my webpage ... Free Valentine cake ideas

March 07, 2013 2:12 AM  
Anonymous Anonymous said...

Gnana Ramanathan is an internet entrepreneur specializing in elegant home decor items like Room Dividers.
Garden gnomes can be purchased at various garden decor stores or
on the Gnome Town USA website. New Orleans potteries are famous for its colors, designs and shapes
and this is what it makes different than other potteries.


Check out my webpage: apartment patio garden ideas

March 08, 2013 1:15 AM  
Anonymous Anonymous said...

Very descriptive blog, I liked that bit. Will there be a part 2?



Feel free to surf to my blog post - http://alplist.com/story.php?id=886514

March 11, 2013 12:43 PM  
Anonymous Anonymous said...

Everything is very open with a really clear explanation of
the challenges. It was truly informative. Your website is very helpful.
Thank you for sharing!

Here is my web-site ... any dicount for quantrim

March 12, 2013 5:19 AM  
Anonymous Anonymous said...

If using large monogram wall decals, having someone to help place the decal in the perfect area would be the best idea.
For a kid who shows an interest in sport hang a basketball hoop, or
use hockey sticks in place of curtain rods. Shop for sofa bed online as you would find a huge variety of beds on online stores together with saving
more money with the help of discounts.

Here is my page; beach bedroom decor

March 29, 2013 3:58 AM  
Anonymous Anonymous said...

When decorating kids space, you may pick the blue color as the fundamental color, in addition to other 3 principal colors as the ornament so
as to create distinct views. Moreover, the usage of decorative
panels towards the wall "possession," furthermore,
it "theme wall" as a significant decorative methods. The
versatile stems may be bent to the fall as well as the curve so desired.


Here is my web site baby nursery decorating ideas

April 08, 2013 10:14 PM  
Anonymous Anonymous said...

This design is spectacular! You certainly know how to keep a reader amused.
Between your wit and your videos, I was almost moved to start my own blog (well, almost.
..HaHa!) Fantastic job. I really enjoyed what you had to say,
and more than that, how you presented it.
Too cool!

Feel free to surf to my site qantrim

April 09, 2013 3:38 AM  
Anonymous Anonymous said...

I don't even know how I ended up here, but I thought this post was good.

I do not know who you are but certainly you're going to a famous
blogger if you aren't already ;) Cheers!

Here is my website :: medical negligence claims no win no fee

August 06, 2014 8:09 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home