Compiling kernel modules (crosscompiler problem?)

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
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Compiling kernel modules (crosscompiler problem?)

Post by giszo »

Hi!

I just started to integrate a cross compiler into my project. I have binutils 2.19 and gcc 4.3.3 compiled to run on linux and compile binary to my own OS.

Unfortunately I have a little problem when I try to compile my kernel modules with the above mentioned cross compiler. Here's the command line what I've used for kernel modules to compile with my native Linux compiler before I started to use the new cross compiler:

Code: Select all

gcc -c -O2 -m32 -Wall -Wshadow -nostdinc -nostdlib -fno-builtin -fPIC pci.c -I../../../kernel/include -o objs/pci.o
... and ...

Code: Select all

gcc -m32 -shared -nostartfiles -nodefaultlibs objs/devices.o objs/pci.o -o objs/pci
This works fine until I use my native Linux compiler but when I switch to my cross compiler I got a few errors when I try to link the final kernel module:

#1: Tries to link it as a normal executable and looks for _start:

Code: Select all

/home/giszo/prog/yaosp/build/crosscompiler/lib/gcc/i686-pc-yaosp/4.3.3/../../../../i686-pc-yaosp/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000040000080
#2: All the symbols that are defined inside the kernel are undefined

Code: Select all

objs/devices.o: In function `create_device_node_for_pci_device':                                                                                             
devices.c:(.text+0x44): undefined reference to `snprintf'                                                                                                    
devices.c:(.text+0x5a): undefined reference to `create_device_node'
...
Do you have any idea what did I wrong?

Thanks,
giszo
Last edited by giszo on Mon Feb 02, 2009 7:46 am, edited 1 time in total.
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Compiling kernel modules

Post by gzaloprgm »

#1: Tries to link it as a normal executable and looks for _start:
warning: cannot find entry symbol _start; defaulting to 0000000040000080
Elf and other executable formats ussually define one entrypoint. Because you are compiling AND linking the final file, the compiler expects you to have a crt0.o, which defines things such as _start (AFAIK). If you want just to compile and remove that warning, use gcc -e moduleentrypoint.
#2: All the symbols that are defined inside the kernel are undefined
devices.c:(.text+0x44): undefined reference to `snprintf'
devices.c:(.text+0x5a): undefined reference to `create_device_node'
Of course, the linker doesn't know where is the entry point of snprintf function neither create_device_node.

Maybe you can research about elf relocation, or create a quick hack and include in a fixed file location the addressess of both functions (the kernel can set them before executing your module) and then in your module call a function pointer instead of a simple pointer.

Cheers,
Gonzalo
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Re: Compiling kernel modules

Post by giszo »

gzaloprgm wrote:
#1: Tries to link it as a normal executable and looks for _start:
warning: cannot find entry symbol _start; defaulting to 0000000040000080
Elf and other executable formats ussually define one entrypoint. Because you are compiling AND linking the final file, the compiler expects you to have a crt0.o, which defines things such as _start (AFAIK). If you want just to compile and remove that warning, use gcc -e moduleentrypoint.
You may missed the -shared and -nostartfiles parameters at my first post ;)
gzaloprgm wrote:
#2: All the symbols that are defined inside the kernel are undefined
devices.c:(.text+0x44): undefined reference to `snprintf'
devices.c:(.text+0x5a): undefined reference to `create_device_node'
Of course, you need to have in a .h file the external references such as (it's an example):

extern void snprintf(const char *frmt, ...);
extern int creat_device_node(int, int, int);
Not specifying the function prototype would only result in a compile time warning, not in a link time undefined reference. This is caused by the fact that GCC tries to link it as a normal (maybe static?) executable instead of a shared "library".

giszo

EDIT: I think the problem isn't in my sources because the whole src tree compiles without a single problem with my native Linux compiler. So the problem should be in my cross compiler or in the cmd line I use to compile/link my files.
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Re: Compiling kernel modules (crosscompiler problem?)

Post by giszo »

So... no ideas? :(
Post Reply