Dynamic Linking

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Dynamic Linking

Post by Jeko »

I want to implement SO libraries in my os. But I don't know how to do this!
I implemented elf execution.
Otter
Member
Member
Posts: 75
Joined: Sun Dec 31, 2006 11:56 am
Location: Germany

Post by Otter »

You have to support a binary format which contains relocation data, for example elf shared. Then, you need a dynamic linker, which performs the relocations when you load an elf executable.

Relocation means that you correct the offsets encoded in the binary data of your executable image. Relocation data contains position of these offsets and some hints how to relocate.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

Otter wrote:You have to support a binary format which contains relocation data, for example elf shared. Then, you need a dynamic linker, which performs the relocations when you load an elf executable.

Relocation means that you correct the offsets encoded in the binary data of your executable image. Relocation data contains position of these offsets and some hints how to relocate.
how can a program use a function in an elf shared.

For example there is an elf shared called lib.so.
If a program called prog.elf wants to use the function int hello(char s) from lib.so, how can it do?

Sorry for my bad english!
Otter
Member
Member
Posts: 75
Joined: Sun Dec 31, 2006 11:56 am
Location: Germany

Post by Otter »

Lets say your program will be loaded to adress 0x00100000. The loader sees in the headers and symbol tables ( dynamic section ) that the executable uses a dynamic function, "hello". Then, the loader ( or the dynamic linker ) checks whether the shared lib "lib.so" is already loaded. If not, it loads it to a shared space which is visible to all user space processes.

Then, back to the executable, it looks for relocation data of "hello". These should be in the executable file. Using these data the dynamic linker finds all offsets and correct them this way that they point to the "hello" of "lib.so".

You should read something about relocation, there are lots of different types. For IA-32 the most important ones are absolute 32 bit relocation and relative 32 bit relocation. There are much informations available. If you have a good explanation of the elf binary format, you should find all you need in them.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

Otter wrote:Lets say your program will be loaded to adress 0x00100000. The loader sees in the headers and symbol tables ( dynamic section ) that the executable uses a dynamic function, "hello". Then, the loader ( or the dynamic linker ) checks whether the shared lib "lib.so" is already loaded. If not, it loads it to a shared space which is visible to all user space processes.

Then, back to the executable, it looks for relocation data of "hello". These should be in the executable file. Using these data the dynamic linker finds all offsets and correct them this way that they point to the "hello" of "lib.so".

You should read something about relocation, there are lots of different types. For IA-32 the most important ones are absolute 32 bit relocation and relative 32 bit relocation. There are much informations available. If you have a good explanation of the elf binary format, you should find all you need in them.
how can the dynamic linker know that 'hello' is a function of the library lib.so?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

MarkOS wrote:how can the dynamic linker know that 'hello' is a function of the library lib.so?
You might want to read this.. http://en.wikipedia.org/wiki/Library_%2 ... science%29

Probably around this section.. http://en.wikipedia.org/wiki/Library_%2 ... ic_linking

You might also notice that Dynamic Linking and Dynamic Loading are two different things.

Why not just look at the source code to an existing project that supports Dynamic linking or Dynamic Loading.. Like BSD or Linux.

Useful Links:
http://en.wikipedia.org/wiki/Loader_%28computing%29
http://en.wikipedia.org/wiki/LD_PRELOAD

Good luck :?
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

I want to implement routines in dlfcn.h:

Code: Select all

#include <dlfcn.h>

#include <stdlib.h>
#include <stdio.h>

typedef void (*simple_demo_function)(void);

int main(void) {
 const char *error;
 void *module;
 simple_demo_function demo_function;

 /* Load dynamically loaded library */
 module = dlopen("libhello.so", RTLD_LAZY);
 if (!module) {
   fprintf(stderr, "Couldn't open libhello.so: %s\n",
           dlerror());
   exit(1);
 }

 /* Get symbol */
 dlerror();
 demo_function = dlsym(module, "hello");
 if ((error = dlerror())) {
   fprintf(stderr, "Couldn't find hello: %s\n", error);
   exit(1);
 }

 /* Now call the function in the DL library */
 (*demo_function)();

 /* All done, close things cleanly */
 dlclose(module);
 return 0;
}
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Here is a example of the way DexOS does it:
http://jas2o.forthworks.com/dexforum/in ... 55#msg1855
Post Reply