Page 1 of 1

Compiling kernel modules (crosscompiler problem?)

Posted: Sun Feb 01, 2009 12:52 pm
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

Re: Compiling kernel modules

Posted: Sun Feb 01, 2009 1:08 pm
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

Re: Compiling kernel modules

Posted: Sun Feb 01, 2009 1:12 pm
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.

Re: Compiling kernel modules (crosscompiler problem?)

Posted: Tue Feb 03, 2009 2:00 pm
by giszo
So... no ideas? :(