LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 05-18-2011, 05:51 AM   #1
john.sung
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 : http://www.linuxquestions.org/questi...module-587406/

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 <test@test.com>" ) ;
MODULE_DESCRIPTION ( "This is a Test Module" ) ;
MODULE_LICENSE ( "GPL" ) ;

//=================================================================
// Makefile
//=================================================================

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

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

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)

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

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

Last edited by john.sung; 05-18-2011 at 05:52 AM.
 
Old 05-19-2011, 01:08 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,537

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
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, 07:47 PM   #3
todaygood
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 ) ) ;

->report

[ 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.
 
  


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


All times are GMT -5. The time now is 11:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration