OSDev.org
https://forum.osdev.org/

Is there any example at implementing dlsym(3)?
https://forum.osdev.org/viewtopic.php?f=1&t=33370
Page 1 of 1

Author:  technix [ Wed Dec 12, 2018 10:28 pm ]
Post subject:  Is there any example at implementing dlsym(3)?

This is part of porting libobjc2 to a kernel, which can also be used to implement a modular kernel along with dlopen(3) even without libobjc2. With a multiboot kernel, how to implement dlsym(3) to allow dynamic lookup of symbols?

Author:  egranata [ Thu Dec 13, 2018 12:07 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

This is by no means a complete implementation, but it works for very basic use cases and might help you get started: https://github.com/egranata/puppy/blob/master/newlib/syscalls/dlfcn.cpp

Main advantage is that it is entirely in userspace, all it asks of the kernel is the ability to map a region of memory (it's 32-bit Intel, so if you can read it, you can execute it)

Author:  Korona [ Thu Dec 13, 2018 12:12 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

mlibc (which is the C library of my OS) has a dynamic linker that shows how it is done in general.

For the special case of a kernel module, I have a complete gist for ELF loading and dynamic linking to the kernel itself. As it cannot handle arbitrary dependencies and the full ELF feature set, this code is considerably shorter and easier to understand than mlibc. The symbol resolution is done in the function lookup(). It uses eligible() to check if a given symbol is exported and elf64Hash() to compute the ELF hash function. The symbol lookup is only about 35 lines of code.

Author:  klange [ Thu Dec 13, 2018 12:20 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

I'll throw my chips in here...

As an alternative to dynamic linking, I do runtime static linking of kernel modules, similar to Linux.

I also have a userspace dynamic linker that has rudimentary implementations of (most of) the dl* functions (good enough for Python).

Author:  pvc [ Thu Dec 13, 2018 4:40 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

dietlibc has nice and easy to understand implementation. Check /ldso.c file.

Author:  technix [ Thu Dec 13, 2018 8:50 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

I am using dlsym(3) in kernel here as part of dynamic binding process of Objective-C language and its runtime library libobjc2. Keep in mind that Objective-C uses exclusively dynamic binding for any and all method calls. The user mode has its own separate libc and dlsym(3) implementation as part of ld.so.

Author:  Korona [ Thu Dec 13, 2018 11:40 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

We understand that. That's why I posted a more-or-less freestanding gist and klange posted his module implementation. Why is that not enough? What exactly do you ask for?

Author:  technix [ Fri Dec 14, 2018 11:52 pm ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

Korona wrote:
We understand that. That's why I posted a more-or-less freestanding gist and klange posted his module implementation. Why is that not enough? What exactly do you ask for?

I need a dlsym(3) that is capable of resolving symbols of the kernel itself, even without or before loading any modules. libobjc2 maps this:
Code:
GKProcess *process;
[process killWithSignal:SIGTERM];

into equivalent of this:
Code:
__objc_GKProcess_killWithSignal_(process, NSSelectorFromString("killWithSignal:"), SIGTERM);

through dlsym(3) of the symbol listed above.

Author:  klange [ Sat Dec 15, 2018 12:51 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

technix wrote:
I need a dlsym(3) that is capable of resolving symbols of the kernel itself, even without or before loading any modules. libobjc2 maps this:


You talked about building modular kernels, so we provided more complete solutions for loading modules.

Implementing dlsym on a single object is as simple as writing a function that looks at a table mapping strings of symbol names to symbol values, and storing your symbols in that table. You can do that in a few different ways. I do a two-pass kernel build where I generate an object file with all the symbols. You can also make sure the actual ELF symbol table for your binary is loaded into memory, but that's a bit tricky.

Author:  technix [ Sat Dec 15, 2018 1:43 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

klange wrote:
technix wrote:
I need a dlsym(3) that is capable of resolving symbols of the kernel itself, even without or before loading any modules. libobjc2 maps this:


You talked about building modular kernels, so we provided more complete solutions for loading modules.

Implementing dlsym on a single object is as simple as writing a function that looks at a table mapping strings of symbol names to symbol values, and storing your symbols in that table. You can do that in a few different ways. I do a two-pass kernel build where I generate an object file with all the symbols. You can also make sure the actual ELF symbol table for your binary is loaded into memory, but that's a bit tricky.

For my system the symbol table need to be pre-populated with in-kernel symbols before any modules are loaded, and then new symbols from modules need to be be added upon loading.

Author:  klange [ Sat Dec 15, 2018 1:47 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

technix wrote:
For my system the symbol table need to be pre-populated with in-kernel symbols before any modules are loaded, and then new symbols from modules need to be be added upon loading.

Which is what my kernel - which I have already linked for you - does.

You will not find a tutorial on how to implement this functionality. However, we have provided several example implementations for you to peruse.

Author:  technix [ Sat Dec 15, 2018 1:59 am ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

klange wrote:
technix wrote:
For my system the symbol table need to be pre-populated with in-kernel symbols before any modules are loaded, and then new symbols from modules need to be be added upon loading.

Which is what my kernel - which I have already linked for you - does.

You will not find a tutorial on how to implement this functionality. However, we have provided several example implementations for you to peruse.

Is there any way to make sure linker and Multiboot keep ELF symbol table intact? This way I can use the same ELF symbol table parser for both in-kernel symbols and loaded modules.

Author:  pvc [ Sat Dec 15, 2018 4:45 pm ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

I've had problems with GRUB !NOT! loading symbol tables as well. I've had to load all of the headers on top of what GRUB does.

Author:  Korona [ Sun Dec 16, 2018 1:37 pm ]
Post subject:  Re: Is there any example at implementing dlsym(3)?

The proper way to handle this is to make sure that the symbols appear in the dynamic symbol type (referenced by the DYNAMIC PHDR). That way, you do not have to rely on GRUB (or any other loader) to get load an external table and your kernel will work even after being strip'ed.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/