Linking kernel modules with gcc/ld

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
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Linking kernel modules with gcc/ld

Post by mallard »

Hi,

I'm probably making some sort of basic error here, but how do I get gcc/ld to link a binary suitable for use as a kernel module?

I'm using ELF format and as far as I can tell, the binary needs to have both program headers in order for me to load it and relocation tables to allow me to load it at an arbitrary(ish) place in the kernel's address space. However, by default it seems that the linker outputs files without relocation tables (i.e. non-relocatable executables), but if I pass the "-r" (--relocatable) flag to the linker, the files are "partially linked" and have no program headers.

I've looked through the gcc and ld manual pages, but can't see anything obvious...
Image
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linking kernel modules with gcc/ld

Post by Combuster »

There's two relocation flags right above each other - you're probably looking for --emit-relocs (-q) which actually performs linking unlike what -r does.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: Linking kernel modules with gcc/ld

Post by mallard »

Thanks, that looks like it's produced what I want (according to objdump at least). I'm not sure how I didn't spot that option before... :oops:
Image
Post Reply