trouble compiling a kernel module in linux

10,180

Pls share the hello-world module code that you have written.

relocs.c is present under arch/x86/tools inside the kernel src.

As you have only linux-headers installed on your machine you may not see relocs.c file. You have to download the entire kernel src.

Follow these steps:

$ sudo apt-get update

$ sudo apt-get install linux-source.

As a trial (after downloading kernel src) you can change the makefile to point KDIR to the entire kernel dir and then build your module.

Share:
10,180
AvinashK
Author by

AvinashK

Updated on June 04, 2022

Comments

  • AvinashK
    AvinashK almost 2 years

    I wrote a simple hello world kernel module. A make file was written by me as below:

    obj -m += hello.o
    
    KDIR: /usr/src/linux-headers-3.2.0-21-generic-pae
    
    all:
         $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    
    clean:
         rm -rf *.o *.ko *.mod.* *.symvers *.order
    

    Then I compiled the module using make. It worked just fine.

    Then, recently I did 'make' again when the kernel was updated to 3.2.0-24(of course I changed the version to 24 in KDIR). But the following error cropped up:

    make[1]: Entering directory `/usr/src/linux-headers-3.2.0-24-generic-pae'
    make[2]: *** No rule to make target `arch/x86/tools/relocs.c', needed
    by `arch/x86/tools/relocs'.  Stop.
    

    I checked and found that an executable file of relocs was there at /arch/x86/tools/ but there was no relocs.c

    What went wrong and how should I correct it so that I successfully compile the module?

  • AvinashK
    AvinashK over 11 years
    @ye9ane...oops...I access internet through Windows OS. So, to get the exact words of error, I searched it on the internet and did a copy-paste.You are right it was 24
  • ye9ane
    ye9ane over 11 years
    another strange thing to point at, what I understand from your makefile is that you only include 'headers' folder to compile your module, are you sure the source files are available in that folder too?Unless you only use kernel statics, you may need source files to compile your module.
  • AvinashK
    AvinashK over 11 years
    @ye9ane...it worked the first time and everywhere i read the makefiles have the same format.But they do say that we are 'making' the kernel file against the kernel source tree( and I don't think source files should be in header's folder).So, if you think it should be something else, please do write the apt folder
  • ye9ane
    ye9ane over 11 years
    If it worked fine the first time , then it's not the issue. By updating kernel, do you mean that you replaced the whole source tree by the new one?...have you checked out to see if the previous kernel contained reloc.c?
  • AvinashK
    AvinashK over 11 years
    @ye9ane...the previous kernel too had the same executable file called relocs in that location