I want to implement SO libraries in my os. But I don't know how to do this!
I implemented elf execution.
Dynamic Linking
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.
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.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.
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!
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.
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?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.
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
You might want to read this.. http://en.wikipedia.org/wiki/Library_%2 ... science%29MarkOS wrote:how can the dynamic linker know that 'hello' is a function of the library lib.so?
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
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;
}
Here is a example of the way DexOS does it:
http://jas2o.forthworks.com/dexforum/in ... 55#msg1855
http://jas2o.forthworks.com/dexforum/in ... 55#msg1855