5

I have just started writing kernel modules. I am referring to this guide.

I wrote a simple Hello world program exactly as specified in the guide.

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");

    /* 
     * A non 0 return means init_module failed; module can't be loaded. 
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

I tried compiling it using the following makefile:

obj-m += hello-1.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

I get the error:

error: code model kernel does not support PIC mode

I looked up on this forum and others and found the following

  1. Somebody who was trying to build the Linux kernel and couldn't. Here
  2. That lead me to this patch. But I don't know how to use it.
  3. Then I tried with gcc-5 (just ot see what happens. It still doesn't work.
  4. I basically went through all steps as this guy. I tried adding -fno-pie, then the other steps that guy follows.

$ uname -a
Linux cristopher 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

According to the bug link, this has been fixed in gcc. But i think it is not yet in the stable release that I am using.

I can think of multiple solutions to this problem. 1. Installing the patch from (2) above. 2. Using an old version of gcc where this is not an issue. 3. Usinng some more extra flags in the kernel makefile. (Not sure of this though.)


I don't know how to do any of the above things. Any help would be appreciated.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
Codename:   bionic

PS: I recently upgraded to 18.04 from 16.04. It was working fine previously.


Including the complete output of make all.

$ make all
make -C /lib/modules/4.15.0-43-generic/build M=/media/parth/F/Parth/programs/dev modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-43-generic'
  CC [M]  /media/parth/F/Parth/programs/dev/hello-1.o
/media/parth/F/Parth/programs/dev/hello-1.c:1:0: error: code model kernel does not support PIC mode
 #include <linux/module.h> /* Needed by all modules */
 ^
scripts/Makefile.build:339: recipe for target '/media/parth/F/Parth/programs/dev/hello-1.o' failed
make[2]: *** [/media/parth/F/Parth/programs/dev/hello-1.o] Error 1
Makefile:1551: recipe for target '_module_/media/parth/F/Parth/programs/dev' failed
make[1]: *** [_module_/media/parth/F/Parth/programs/dev] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-43-generic'
Makefile:7: recipe for target 'all' failed
make: *** [all] Error 2

Okay, I am not really sure how, or why, but the original error is not coming anymore. But there is a new error:

In file included from ./include/linux/list.h:5:0,
                 from ./include/linux/module.h:9,
                 from /media/parth/F/Parth/programs/dev/hello-1.c:1:
./include/linux/types.h:17:9: error: unknown type name ‘__kernel_ino_t’
 typedef __kernel_ino_t  ino_t;
         ^~~~~~~~~~~~~~
./include/linux/types.h:18:9: error: unknown type name ‘__kernel_mode_t’
 typedef __kernel_mode_t  mode_t;
         ^~~~~~~~~~~~~~~
./include/linux/types.h:21:9: error: unknown type name ‘__kernel_off_t’
 typedef __kernel_off_t  off_t;
         ^~~~~~~~~~~~~~

etc, etc, etc....

It all seems to stem from this:

In file included from ./include/linux/list.h:9:0,
                 from ./include/linux/module.h:9,
                 from /media/parth/F/Parth/programs/dev/hello-1.c:1:
./include/linux/kernel.h:6:10: fatal error: stdarg.h: No such file or directory
 #include <stdarg.h>
          ^~~~~~~~~~

I tried removing the -fno-pie from the kernel build makefile in the following lines(around line 650) in my system. and got the original error message back.

KBUILD_CFLAGS   += $(call cc-option,-fno-PIE)
KBUILD_AFLAGS   += $(call cc-option,-fno-PIE)

Okay, I updated my system, after the release files at this, this, and this links became valid. Seems that they have fixed the error. Or, is there a clause that you cannot upgrade to the latest packages once you perform a clean install of UBUNTU?

Either way, would still like to know more...

  • 1
    Hmm... that's odd - it appears to work for me, with the exact same kernel and GCC 7.3.0-27ubuntu1~18.04. Can you provide a more complete transcription of the make command output? – steeldriver Jan 20 at 17:16
  • 3
    The only way I can reproduce this error is to do something like make EXTRA_CFLAGS=-fpic - do you perhaps have such a variable defined somewhere in your environment (perhaps look at env | grep FLAGS)? – steeldriver Jan 20 at 17:44
  • Perhaps this could be useful: askubuntu.com/q/1083091/295286 In my specific case, makefile had to use shell pwd instead of PWD. – Sergiy Kolodyazhnyy Jan 21 at 0:34
  • By the way tldp guide you're referring to seems to be for 2.6 kernel. Whole lot of things have changed since then, so these guides often may not work with modern kernels – Sergiy Kolodyazhnyy Jan 21 at 0:36
  • @SergiyKolodyazhnyy Do you have some other guide that I could use? – Parth K Jan 21 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.