public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Inserting a module (2.6 kernel)
@ 2004-06-09  0:33 Steve Hemond
  2004-06-09  0:40 ` Arthur Othieno
  0 siblings, 1 reply; 4+ messages in thread
From: Steve Hemond @ 2004-06-09  0:33 UTC (permalink / raw)
  To: linux-kernel

Hi people,

I am new to kernel module writing and I base myself on the Linux Device Drivers book from O'reilly. I have written this simple module :

#include <linux/module.h>

int init_module(void)
{
  printk("<1>Module inserted\n");
  return 0;
}

void cleanup_module(void)
{
  printk("<1>Module removed\n");
}

--------------------------------------------
And this is the Makefile :

KERNELDIR = /usr/src/linux

include $(KERNELDIR)/.config

CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include \
        -O -Wall

ifdef CONFIG_SMP
        CFLAGS += -D__SMP__ -DSMP
endif

all : moduletest.o

clean :
        rm -f *.o *~ core

---------------------------------------------

And look at this :

bash-2.05b# make
gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/include -O -Wall   -c -o moduletest.o moduletest.c
bash-2.05b# insmod ./moduletest.o
insmod: error inserting './moduletest.o': -1 Invalid module format

Anyone know what needs to be added or changed for kernel 2.6, or maybe its simply my own mistake?

(By the way, if you know of a kernel-beginner mailing list that would be better suited about this, tell me)

Thanks a lot in advance,

Best regards,

Steve

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Inserting a module (2.6 kernel)
  2004-06-09  0:33 Inserting a module (2.6 kernel) Steve Hemond
@ 2004-06-09  0:40 ` Arthur Othieno
  0 siblings, 0 replies; 4+ messages in thread
From: Arthur Othieno @ 2004-06-09  0:40 UTC (permalink / raw)
  To: Steve Hemond; +Cc: linux-kernel

On Tue, Jun 08, 2004 at 08:33:42PM -0400, Steve Hemond wrote:
> Hi people,
> 
> I am new to kernel module writing and I base myself on the Linux Device Drivers book from O'reilly. I have written this simple module :
> 
> #include <linux/module.h>
> 
> int init_module(void)
> {
>   printk("<1>Module inserted\n");
>   return 0;
> }
> 
> void cleanup_module(void)
> {
>   printk("<1>Module removed\n");
> }

http://lwn.net/Articles/21817/

> And this is the Makefile :
> 
> KERNELDIR = /usr/src/linux
> 
> include $(KERNELDIR)/.config
> 
> CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include \
>         -O -Wall
> 
> ifdef CONFIG_SMP
>         CFLAGS += -D__SMP__ -DSMP
> endif
> 
> all : moduletest.o
> 
> clean :
>         rm -f *.o *~ core
> 
 
http://lwn.net/Articles/21823/

> And look at this :
> 
> bash-2.05b# make
> gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/include -O -Wall   -c -o moduletest.o moduletest.c
> bash-2.05b# insmod ./moduletest.o
> insmod: error inserting './moduletest.o': -1 Invalid module format
 
http://kernel.org/pub/linux/utils/kernel/module-init-tools/
http://lwn.net/Articles/22197/   "Kernel version checking"

> Anyone know what needs to be added or changed for kernel 2.6, or maybe its simply my own mistake?
> 
> (By the way, if you know of a kernel-beginner mailing list that would be better suited about this, tell me)

http://kernelnewbies.org/mailinglist.php3

  $ echo subscribe kernelnewbies | mail listar@nl.linux.org

> Thanks a lot in advance,
> 
> Best regards,
> 
> Steve

	Arthur

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Inserting a module (2.6 kernel)
       [not found] <24Zio-6xX-3@gated-at.bofh.it>
@ 2004-06-09  9:47 ` Andi Kleen
  2004-06-12 21:08   ` Sam Ravnborg
  0 siblings, 1 reply; 4+ messages in thread
From: Andi Kleen @ 2004-06-09  9:47 UTC (permalink / raw)
  To: Steve Hemond; +Cc: linux-kernel

Steve Hemond <steve.hemond@sympatico.ca> writes:

> Hi people,
>
> I am new to kernel module writing and I base myself on the Linux Device Drivers book from O'reilly. I have written this simple module :
>
> #include <linux/module.h>
>
> int init_module(void)
> {
>   printk("<1>Module inserted\n");
>   return 0;
> }
>
> void cleanup_module(void)
> {
>   printk("<1>Module removed\n");
> }
>

For some reason that's probably far too complicated for my little
brain it's getting more and more complicated to write custom
modules for 2.6.

Compile all with:

gcc -O2 -c hello.c -I /path/to/kernel/include

or 

gcc -O2 -mcmodel=kernel -mno-red-zone -c hello.c -I /path/to/kernel/include
if you're using x86-64.

In 2.4 what worked was:

#define MODULE 1
#define __KERNEL__ 1 
#include <linux/module.h>

int init_module(void)
{
        printk("Hello world\n");
        return 0;
}

Then in 2.6 it needed

#define MODULE 1
#define __KERNEL__ 1 
#define KBUILD_MODNAME "hello"
#include <linux/module.h>

int init_module(void)
{
        printk("Hello world\n");
        return 0;
}

Now since 2.6.5 or so it needs: 

/* MODULE is not needed anymore */
#define __KERNEL__1 
#include <linux/module.h>

int init_module(void)
{
        printk("Hello world\n");
        return 0;
}

struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
        .name = "hello",
        .init = init_module,
};

I'm sure there will be more surprises in the future. Keep tuned.

-Andi




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Inserting a module (2.6 kernel)
  2004-06-09  9:47 ` Andi Kleen
@ 2004-06-12 21:08   ` Sam Ravnborg
  0 siblings, 0 replies; 4+ messages in thread
From: Sam Ravnborg @ 2004-06-12 21:08 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Steve Hemond, linux-kernel

On Wed, Jun 09, 2004 at 11:47:58AM +0200, Andi Kleen wrote:
> 
> Now since 2.6.5 or so it needs: 
> 
> /* MODULE is not needed anymore */
> #define __KERNEL__1 
> #include <linux/module.h>
> 
> int init_module(void)
> {
>         printk("Hello world\n");
>         return 0;
> }
> 
> struct module __this_module
> __attribute__((section(".gnu.linkonce.this_module"))) = {
>         .name = "hello",
>         .init = init_module,
> };

Most of the glue above can be deleted if you just accept to use kbuild when
building modules.
So to compile your module use a simple Makefile:
obj-m := mymodule.o

Then to compile the module use:
make -C path/to/kernel/src M=`pwd`

And to install it use:
make -C path/to/kernel/src M=`pwd` modules_install

And to clean up in the directory where the module is being compiled:
make -C path/to/kernel/src M=`pwd` clean

	Sam

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-06-12 21:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-09  0:33 Inserting a module (2.6 kernel) Steve Hemond
2004-06-09  0:40 ` Arthur Othieno
     [not found] <24Zio-6xX-3@gated-at.bofh.it>
2004-06-09  9:47 ` Andi Kleen
2004-06-12 21:08   ` Sam Ravnborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox