Linking a userland program to the kernel

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
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Linking a userland program to the kernel

Post by JamesM »

Hi,

I'm trying to get my userland programs to link nicely with my kernel. Essentially I'm using remote procedure calls, so userland programs actually have to call kernel functions directly. However, I don't want to compile any of the kernel code statically in to these programs (for obvious reasons). So, what I want to do, is have all references to kernel symbols (functions etc) to automatically resolve to the correct address in kernel code. I don't think I explained that all too well, so here's an example:

Userland program:

Code: Select all

push eax
call 0x100010 <readStackPointer>
So, I need all kernel symbols to be defined, when linking a userland program.

So, I thought about several ways to do this. The first was to create a .so for my kernel, then bind the symbols dynamically when the program is loaded. This seems like an elegant solution to me, but I get the following error when linking with this .so (and before you ask I've tried every damn combination of ld flags both when linking the .so and linking the userland program :P):

Code: Select all

james@james-desktop:~/Code/C/Linux/jimix2/jimix2/trunk/initrd/udev$ ld -o udev -Bsymbolic ../../src/lib/crt0.o main.o ../../src/lib/libjimix.a -L ../../src/ -lkernel
ld: warning: type and size of dynamic symbol `readStackPointer' are not defined
ld: dynamic variable `readStackPointer' is zero size
ld: main.o(.text+0x2c): unresolvable R_386_PC32 relocation against symbol `readStackPointer'
ld: final link failed: Nonrepresentable section on output
I get this error when I compile the library with "-shared". If I compile with "-r" (relocatable), it links but it links statically :(

OK, so any help on that would be grand, but I tried another method - that is manually defining all kernel symbols at link-time.

I can do this with

Code: Select all

--defsym readStackPointer=0x666
and I'm sure I can hack up a script to read the output from nm and dump it into a command file, but it doesn't seem like an elegant solution, and I'm pretty damn certain it won't scale up well (for thousands of symbols).

Any help would be MUCH appreciated :)

And I have tried google. of course. But that link error pulled up 3 results, all 3 of which being the source code from ld that prints that error!

JamesM
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

OK, I know this is not very long at all after I posted the original thread but...

I managed to google successfully that error and read in some mailing list post somewhere that probably the problem is the symbol was defined in an assembly file, not a C file. I changed to testing using a different symbol, and now it works.

I feel really embarrased that I managed to solve the problem just after I posted, but I really felt I was hitting my head against a brick wall.

JamesM
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Post by kataklinger »

So your user programs run at RING0?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

No, they don't. When a procedure is called that needs to run in kernel-mode, a set of macros around sysenter/sysexit handle it. It makes syscalls slightly more transparent from the user's perspective and allows easier object-orientation.

JamesM
Post Reply