LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 05-18-2011, 06:51 AM   #1
LQ Newbie
Registered: May 2011
Posts: 1

Rep: Reputation: Disabled
Smile Linking Static library to Kernel Module

Hello everyone,
I am new to this forum. Recently I am learning how to link static libraries to kernel module.
I have found simliar posts in :

I have a test.c file with the following contents :

int TEST ( int p1 , int p2 )
return p1 + p2 ;

I compiled it with :

gcc -o test.o -c test.c

I made the static library with :

ar -rcs test.a test.o

I tried to use the TEST function in a kernel module, however, I found out that the passed in values of the parameters are totally wrong, and made the function give wrong result. Does any one know if there is special ways or flags should be added when making the static library for kernel module? Thank you in advanced for your advise !

John Sung

The kernel module file looks like below :
// module.c
#include <linux/module.h>
int TEST ( int , int ) ;
static int __init Module_Init ( void )
printk ( "TEST result %d\n" , TEST( 1 , 2 ) ) ;
return 0 ;
static void __exit Module_Exit ( void )
return ;
module_init ( Module_Init ) ;
module_exit ( Module_Exit ) ;
MODULE_AUTHOR ( "test <>" ) ;
MODULE_DESCRIPTION ( "This is a Test Module" ) ;

// Makefile

KERNELDIR := /lib/modules/$(shell uname -r)/build

obj-m := TestMod.o
TestMod-objs := module.o test.a


$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
/sbin/depmod -ae

Last edited by john.sung; 05-18-2011 at 06:52 AM.
Old 05-19-2011, 02:08 PM   #2
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,277

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
Usually, you should be building the kernel and your module in the same source tree at the same time with the same version of gcc. In any case, to build a kernel module you need to give the same options to gcc as were used when the kernel was built. For example, if your kernel is 64-bit, you need -m64. See the file Documentation/kbuild/modules.txt in the kernel source tree.
1 members found this post helpful.
Old 11-17-2012, 08:47 PM   #3
LQ Newbie
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
not solved

I met the same issue.

printk ( "TEST result %d\n" , TEST( 1 , 2 ) ) ;


[ 985.040060] overflow in relocation type 10 val ffffffffa0499024
[ 985.040063] `drv' likely not compiled with -mcmodel=kernel

so ,add -mcmodel=kernel when compiling, the issue has been solved.


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
How to link a static library to a kernel module pallava Linux - Newbie 1 09-26-2007 09:25 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-05-2007 12:11 AM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 02:24 AM
static linking to c library lantern Programming 1 04-19-2004 09:58 AM

All times are GMT -5. The time now is 12:56 AM.

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