LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2007, 08:31 AM   #1
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Rep: Reputation: 15
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.
 
Old 05-01-2007, 10:30 AM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
First off, is the kernel tree you’re using the actual build tree for your kernel? It seems to me that you are trying to compile in a clean tree (or perhaps you are pulling headers from /usr/include (e.g., files such as “/usr/include/linux/*.h” instead of “/usr/src/linux-`uname -r`/include/linux/*.h”). Here’s a somewhat saner Makefile:
Code:
KERNEL_SOURCE := /lib/modules/$(shell uname -r)/build

obj-m := main.o

all:
	$(MAKE) -C $(KERNEL_SOURCE) M=$(PWD)
For one thing, it doesn’t assume your kernel build tree resides in /usr/src/linux-`uname -r` (which is not a standard practice). It also uses the M parameter.

P.S., when posting code samples, use [code] and [/code] instead of [quote] and [/quote] (it preserves monospacing as well as tabs and spaces).
 
Old 05-01-2007, 10:43 AM   #3
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
I changed makefile to what you said, the error appear below.
Quote:
make -C /lib/modules/2.6.9-8.tlc/build M=/root/Programming/C/Kernel/Helloworld
make[1]: Entering directory `/lib/modules/2.6.9-8.tlc/build'
make[1]: *** No targets specified and no makefile found. Stop.
make[1]: Leaving directory `/lib/modules/2.6.9-8.tlc/build'
make: *** [all] Error 2
Any ideas?
Thanks again.
 
Old 05-01-2007, 10:55 AM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Can you give me the output of “ls -a /lib/modules/`uname -r`/build”? I still think your kernel tree is somehow incomplete.
 
Old 05-01-2007, 11:07 AM   #5
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
It's nothing in that directory. The first time after I install and boot the system I go to that directory and found as below.
Quote:
drwxr-xr-x 9 root root 4096 ÁÕ.¤. 17 02:54 kernel
-rw-r--r-- 1 root root 129199 ÁÕ.¤. 17 02:54 modules.alias
-rw-r--r-- 1 root root 69 ÁÕ.¤. 17 02:54 modules.ccwmap
-rw-r--r-- 1 root root 181707 ÁÕ.¤. 17 02:54 modules.dep
-rw-r--r-- 1 root root 443 ÁÕ.¤. 17 02:54 modules.ieee1394map
-rw-r--r-- 1 root root 357 ÁÕ.¤. 17 02:54 modules.inputmap
-rw-r--r-- 1 root root 15833 ÁÕ.¤. 17 02:54 modules.isapnpmap
-rw-r--r-- 1 root root 115396 ÁÕ.¤. 17 02:54 modules.pcimap
-rw-r--r-- 1 root root 83919 ÁÕ.¤. 17 02:54 modules.symbols
-rw-r--r-- 1 root root 154763 ÁÕ.¤. 17 02:54 modules.usbmap
lrwxrwxrwx 1 root root 5 ÁÕ.¤. 17 02:54 source -> build
No build directory at all. But i see symlink from source to build then I decide to create 'build' directory, perhap maybe it's require.

Also in /usr/src/linux-2.6.9-8.tlc/, i found below.
Quote:
drwxr-xr-x 19 root root 4096 ¾.¤. 1 22:50 .
drwxr-xr-x 5 root root 4096 ÁÕ.¤. 17 04:20 ..
drwxr-xr-x 24 root root 4096 ÁÕ.¤. 17 04:20 arch
-rw-r--r-- 1 root root 51359 ¾.¤. 1 18:11 .config
-rw-r--r-- 1 root root 4798 ¾.¤. 1 18:11 .config.cmd
-rw-r--r-- 1 root root 51330 ¾.¤. 1 18:08 .config.old
drwxr-xr-x 2 root root 4096 ÁÕ.¤. 17 04:20 configs
-rw-r--r-- 1 root root 18691 µ.¤. 19 2004 COPYING
-rw-r--r-- 1 root root 32823 ¾.Â. 23 2004 COPYING.modules
-rw-r--r-- 1 root root 88031 µ.¤. 19 2004 CREDITS
drwxr-xr-x 4 root root 4096 ÁÕ.¤. 17 04:20 crypto
drwxr-xr-x 45 root root 4096 ÁÕ.¤. 17 04:20 Documentation
drwxr-xr-x 46 root root 4096 ÁÕ.¤. 17 04:20 drivers
drwxr-xr-x 55 root root 4096 ÁÕ.¤. 17 04:20 fs
drwxr-xr-x 36 root root 4096 ÁÕ.¤. 17 04:20 include
drwxr-xr-x 2 root root 4096 ÁÕ.¤. 17 04:20 init
drwxr-xr-x 2 root root 4096 ÁÕ.¤. 17 04:20 ipc
drwxr-xr-x 3 root root 4096 ÁÕ.¤. 17 04:20 kernel
drwxr-xr-x 4 root root 4096 ÁÕ.¤. 17 04:20 lib
-rw-r--r-- 1 root root 54916 ¾.Â. 23 2004 MAINTAINERS
-rw-r--r-- 1 root root 42226 ¾.Â. 23 2004 Makefile
drwxr-xr-x 2 root root 4096 ÁÕ.¤. 17 04:20 mm
drwxr-xr-x 33 root root 4096 ÁÕ.¤. 17 04:20 net
-rw-r--r-- 1 root root 13970 µ.¤. 19 2004 README
-rw-r--r-- 1 root root 2815 µ.¤. 19 2004 REPORTING-BUGS
drwxr-xr-x 10 root root 4096 ÁÕ.¤. 17 04:20 scripts
drwxr-xr-x 3 root root 4096 ÁÕ.¤. 17 04:20 security
drwxr-xr-x 15 root root 4096 ÁÕ.¤. 17 04:20 sound
drwxr-xr-x 2 root root 4096 ¾.¤. 1 20:41 usr
Is it can be that my kernel source tree is in another place maybe above I show you?

What should I do next?
Thanks again osor.
 
Old 05-01-2007, 11:32 AM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by haxpor
But i see symlink from source to build then I decide to create 'build' directory, perhap maybe it's require.
That’s odd… source should be a link to your kernel sourcetree and build should be a link to your kernel build tree (usually these are the same), but you should never have to edit these by hand. My advice would be first, to back up /lib/modules, then to go to what you think is your kernel source tree (/usr/src/linux-2.6.9-8.tlc), and type (as root):
Code:
# make modules
# make modules_install
# depmod -ae

Last edited by osor; 05-01-2007 at 03:31 PM. Reason: typo and codepage issues
 
Old 05-01-2007, 12:13 PM   #7
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
I'm still working on it as per your advice. This will take time.
 
Old 05-01-2007, 01:02 PM   #8
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
Ok it work now, you are my hero, thanks again.
But for this time, when I issue 'insmod ./main.ko' the kernel module is loaded properly (checked by lsmod) but it not print anything out as per code instruction. Also when I issue 'rmmod main' nothing print again.

Any advice?
 
Old 05-01-2007, 01:19 PM   #9
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
Anyway I have searched and find some example of kernel modules,
The great doc link is http://www.cs.usfca.edu/~cruse/cs326/.

Code:
//-------------------------------------------------------------------
//	jiffies.c
//
//	This simple module create a 'pseudo' file (named 'jiffies') 
//	in the system's '/proc' directory.  Then a user can look at 
//	the current value of the kernel's global 'jiffies' variable 
//	just by typing the command: 
//	
//			$ cat /proc/jiffies
//	
//	programmer: ALLAN CRUSE
//	written on: 24 JAN 2003
//	revised on: 07 SEP 2004 -- for Linux kernel version 2.6.5
//-------------------------------------------------------------------

#include <linux/module.h>	// for init_module() 
#include <linux/proc_fs.h>	// for create_proc_read_entry() 

#define	SUCCESS	0

static char modname[] = "jiffies";


static int my_proc_read( char *buf, char **start, off_t off,
					int count, int *eof, void *data )
{
	return	sprintf( buf, "jiffies = %lu\n", jiffies );
}


int init_module( void )
{
	create_proc_read_entry( modname, 0, NULL, my_proc_read, NULL );
	return	SUCCESS;
}


void cleanup_module( void )
{
	remove_proc_entry( modname, NULL );
}

MODULE_LICENSE("GPL");
Above example code is work. But I curious about Helloworld Module why can't it print out both Helloworld when 'insmod', and Goodbye when 'rmmod'.

Thanks again osor, this help me a lot and let me go to way of practicing once again.
 
Old 05-01-2007, 03:34 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by haxpor
but it not print anything out as per code instruction. Also when I issue 'rmmod main' nothing print again.
How did you check this? Are you waiting for it to print to console, or did you check the output of “dmesg”? Kernel messages are not always printed to console (depending on what message level your system is set at), and even when they are, it must be a tty console (not an xterm). So instead of waiting for a message to console, check the output of the “dmesg” command.
 
Old 05-09-2007, 12:34 PM   #11
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks osor, I check dmesg as your advice and It's there.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can't I compile this kernel module? (kernel: 2.6.8-powerpc Debian sarge) Lannix Linux - Hardware 5 05-01-2007 12:29 PM
how to compile a 2.6 kernel module? NeoMagic Linux - Kernel 1 07-18-2006 10:07 AM
Ati Driver Installation Problem (can't compile kernel module) verdeboy2k Linux - Hardware 3 05-16-2006 02:24 AM
Compile a kernel module Ralc Linux - Newbie 5 03-11-2006 08:51 AM
can i compile just one kernel module whithout recompiling the whole kernel? edman007 Linux - Software 3 02-17-2004 03:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration