haxpor |
05-01-2007 08:31 AM |
Problem when Compile User Kernel Module.
I use a piece of code and try to compile it into kernel module.The source code is below.
Source code
Quote:
#include <linux/kernel.h>
#include <linux/module.h>
int init_module(){
printk("<1>Hello world\n");
return 0;
}
void cleanup_module(){
printk("<1>Goodbye\n");
}
MODULE_LICENSE("GPL");
|
First I use simple way to compile, 'gcc -c main.c'. I have main.o then i used 'insmod ./main.o' but the error appear and show below.
Quote:
insmod: error inserting './main.o': -1 Invalid module format
|
Due to the first way failed I move to second.
Second I use 'gcc -c -D__KERNEL__ -DMODULE main.c' and the error is show below.
Quote:
In file included from /usr/include/linux/module.h:10,
from main.c:2:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
|
Next I search through google and find another way to compile.
I used Makefile to compile the source as follow.
Content of Makefile.
Quote:
KERNEL_SOURCE = /usr/src/linux-`uname -r`/
obj-m := main.o
default:
make -C ${KERNEL_SOURCE} SUBDIRS=`pwd` modules
|
Then I use 'make default' to compile and the errors are show below.
Some of the errors.
Quote:
...
...
include/linux/slab.h:93: error: `flags' undeclared (first use in this function)
include/linux/slab.h:94: error: invalid use of undefined type `struct cache_sizes'
include/linux/slab.h:95: error: invalid use of undefined type `struct cache_sizes'
include/linux/slab.h: At top level:
include/linux/slab.h:100: error: syntax error before "int"
include/linux/slab.h:100: warning: function declaration isn't a prototype
include/linux/slab.h:119: error: syntax error before "slab_reclaim_pages"
include/linux/slab.h:119: warning: type defaults to `int' in declaration of `slab_reclaim_pages'
include/linux/slab.h:119: warning: data definition has no type or storage class
In file included from include/linux/sched.h:31,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/percpu.h:7:24: asm/percpu.h: No such file or directory
In file included from include/linux/sched.h:31,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/percpu.h:43: error: syntax error before "size"
include/linux/percpu.h:44: warning: function declaration isn't a prototype
include/linux/percpu.h: In function `__alloc_percpu':
include/linux/percpu.h:45: error: `size' undeclared (first use in this function)
In file included from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/sched.h: At top level:
include/linux/sched.h:95: error: syntax error before "process_counts"
include/linux/sched.h:95: warning: type defaults to `int' in declaration of `DECLARE_PER_CPU'
include/linux/sched.h:95: warning: function declaration isn't a prototype
include/linux/sched.h:95: warning: data definition has no type or storage class
In file included from include/linux/sched.h:103,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/resource.h:22: error: field `ru_utime' has incomplete type
include/linux/resource.h:23: error: field `ru_stime' has incomplete type
In file included from include/linux/sched.h:103,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/resource.h:71:26: asm/resource.h: No such file or directory
make[2]: *** [/root/Programming/C/Kernel/Helloworld/main.o] Interrupt
make[1]: *** [_module_/root/Programming/C/Kernel/Helloworld] Interrupt
...
...
include/linux/sched.h:227: error: storage size of `mmap_sem' isn't known
include/linux/sched.h:261: error: storage size of `default_kioctx' isn't known
include/linux/sched.h:268: error: storage size of `action' isn't known
include/linux/sched.h:286: error: storage size of `shared_pending' isn't known
include/linux/sched.h:390: error: storage size of `wall_to_prev' isn't known
include/linux/sched.h:521: error: storage size of `start_time' isn't known
include/linux/sched.h:532: error: storage size of `rlim' isn't known
include/linux/sched.h:540: error: storage size of `thread' isn't known
include/linux/sched.h:552: error: storage size of `pending' isn't known
include/linux/stat.h:68: error: storage size of `atime' isn't known
include/linux/stat.h:69: error: storage size of `mtime' isn't known
include/linux/stat.h:70: error: storage size of `ctime' isn't known
{standard input}: Assembler messages:
{standard input}:378: Error: symbol `seq' is already defined
{standard input}:426: Error: symbol `sem_nsems' is already defined
{standard input}:449: Error: symbol `lock' is already defined
{standard input}:1584: Error: symbol `files' is already defined
{standard input}:1770: Error: symbol `blocks' is already defined
make[2]: *** [/root/Programming/C/Kernel/Helloworld/main.o] Error 1
make[1]: *** [_module_/root/Programming/C/Kernel/Helloworld] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.9-8.tlc'
make: *** [default] Error 2
|
It seem like it cannot find the header or somethings.
In this case I test it with Linux TLE 7.0 (2.6.9), gcc 3.4.2 and the kernel with Loadable Module Enabled.
What is the problem i'm facing and How to solve it?
Thanks in advance for any help.
|