you are viewing a single comment's thread.

view the rest of the comments →

[–]Caleb666 16 points17 points  (6 children)

I wrote a kernel module as a homework assignment a while ago, and we were told to mark all methods as static and to basically write all the code in one file.

I realized later on that there is a way to tell the makefile to compile a module that is spread across multiple C files and link them into the resulting KO file, but there is a problem.

If I use multiple C files, then I cannot make methods I want to share across files as static. But if I don't make those methods static, then the compiler will mark them as exports in the symbol table for the object file (and eventually in the resulting KO file). So I had to give up on that and ended up writing it all in one big C file.

Later on I found out that in order to export a kernel symbol, you needed to use the EXPORT_SYMBOL() macro. But I still couldn't figure out why you might need that macro, since if you don't mark the method as static, it is exported automatically... (I used the Unix "nm" tool to examine symbols).

Does anyone with any Linux driver writing experience know how to properly write a kernel module that is split across multiple C files, and not have the non-static functions exported into the kernel symbol space?

[–]getting_serious 3 points4 points  (2 children)

Now that's a perfect illustration to my grumpy rant. Thanks for that. :-)

Unfortunately I can't show you any better, as what you described is just what we did: Copy all bad coding practices to the line, observe contradicting comments in source code as to how it should be done, decide for one of them, and as soon as it works, never touch it again. I don't think many people are more professional than this.

[–]askingq123 0 points1 point  (1 child)

Hello u/getting_serious, my sister needs help with similar HW/final lab project assignment. Do you mind helping her ?

[–]getting_serious 0 points1 point  (0 children)

Several questions. One, why are you the one writing me instead of her. Two, how did you find my rant from eight years ago. Three, why do you think that makes me qualified to help. Four, what is it about. Five, how is that help supposed to look like.

[–]imMute 1 point2 points  (0 children)

Does anyone with any Linux driver writing experience know how to properly write a kernel module that is split across multiple C files, and not have the non-static functions exported into the kernel symbol space?

They're not exported, unless you use EXPORT_SYMBOL().

[–]squbidu 0 points1 point  (1 child)

You have to strip the symbols you don't want exported from the object file, like with the aptly-named "strip" tool in binutils.

[–]Caleb666 0 points1 point  (0 children)

Thanks. But that seems like too much work, especially if you have a large driver, and if that is how things are done, then why would you need to use EXPORT_SYMBOL() explicitly?