OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:22 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Is there any example at implementing dlsym(3)?
PostPosted: Wed Dec 12, 2018 10:28 pm 
Offline
Member
Member

Joined: Sun Jun 16, 2013 10:13 am
Posts: 28
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?


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 12:07 am 
Offline

Joined: Sat Jun 09, 2018 11:51 am
Posts: 6
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)


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 12:12 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 12:20 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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).

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 4:40 am 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
dietlibc has nice and easy to understand implementation. Check /ldso.c file.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 8:50 am 
Offline
Member
Member

Joined: Sun Jun 16, 2013 10:13 am
Posts: 28
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.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Thu Dec 13, 2018 11:40 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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?

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Fri Dec 14, 2018 11:52 pm 
Offline
Member
Member

Joined: Sun Jun 16, 2013 10:13 am
Posts: 28
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.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sat Dec 15, 2018 12:51 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sat Dec 15, 2018 1:43 am 
Offline
Member
Member

Joined: Sun Jun 16, 2013 10:13 am
Posts: 28
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.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sat Dec 15, 2018 1:47 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sat Dec 15, 2018 1:59 am 
Offline
Member
Member

Joined: Sun Jun 16, 2013 10:13 am
Posts: 28
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.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sat Dec 15, 2018 4:45 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
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.


Top
 Profile  
 
 Post subject: Re: Is there any example at implementing dlsym(3)?
PostPosted: Sun Dec 16, 2018 1:37 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
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.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 58 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group