1

I am trying to cross compile a driver for an arm based board. In the make file the search path for include files is that of the host machine's kernel i.e it points to the linux headers that come with ubuntu. I also have the kernel source tree of target board present on the host system(i7/ubuntu). My Question is that which include path is needed for cross compilation (native system's linux headers path or the path to the kernel source tree of board ? Does same thing applies to all modules including drivers?

user1877992
  • 67
  • 1
  • 5
  • 1
    it should point to the kernel source tree of the board. This is because the include files that the driver you are compiling may need platform based header files. For eg. if you are cross compiling for ARM the include files should be pointing to arch/arm/include rather then the generic linux include folders. – spitfire88 Dec 06 '12 at 13:22
  • When i use the include files of board, i get many file missing errors. and when i use include files of host machine(running ubuntu 12.04) there are no errors but the driver(module) gets compiled in an invalid format that is not compatible with the board. in both cases i am using the arm-linux-gcc compiler in the tool chain supplied by the vendor. – user1877992 Dec 07 '12 at 05:21
  • When you say "the driver gets compiled in an invalid format" what is the error exactly? Could you elaborate on that? If it is compiling fine that is the error shown during module insertion? – spitfire88 Dec 07 '12 at 06:17
  • yes The compilation goes fine. but when i try to insmod the .ko file of driver i get an invalid format error, pasting the error below insmod: error inserting e1000e.ko: -1 Invalid module format – user1877992 Dec 07 '12 at 07:21
  • Possible duplicate of [Cross compiling a kernel module](http://stackoverflow.com/questions/3467850/cross-compiling-a-kernel-module) – Ciro Santilli OurBigBook.com May 21 '17 at 12:29

3 Answers3

2

Here is a Makefile for an out of tree driver. The architecture, toolchain and kernel dir specified :

ifneq ($(KERNELRELEASE),)
# We were called by kbuild

obj-m += fpgacam.o

else  # We were called from command line

KDIR := path/to/your/target/kernel
PWD  := $(shell pwd)
CROSS=arm-none-linux-gnueabi-

default:
    @echo '    Building Cam drivers for 2.6 kernel.'
    @echo '    PLEASE IGNORE THE "Overriding SUBDIRS" WARNING'
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD)  ARCH=arm CROSS_COMPILE=$(CROSS) modules

install:
    ./do_install.sh *.ko

endif  # End kbuild check
######################### Version independent targets ##########################

clean:
    rm -f -r *.o *.ko .*cmd .tmp* core *.i

When make is called from the module directory, the command line path is taken, and make is redirected to the kernel directory build system using make -C. The kernel build system then the different variable passed to it to go back into the module directory with everything setup (include path, toolchain etc ..) to compile a module. This second time through the Makefile, the kbuild path is taken, and the module is built as if it was in-tree.

shodanex
  • 14,975
  • 11
  • 57
  • 91
0

Compiling with your build-system's headers is spectacularly bad news, and may subtle binary incompatibilities that manifest themselves as seemingly bizarre crashes on the target.

As you've already discovered, the kernel is already hardened against this and will refuse to load modules built against the wrong headers. You'll need to build using the same source tree as the existing kernel - including any patches. You might as well at this point rebuild the entire kernel.

The kernel tree is self-contained, so simply cross-compiling it in place will work. If you're adding a driver it's probably easiest to compile it in-tree.

If you want to build any user-space components, you have two solutions:

  1. Pass the --sysroot=<dir> option to gcc, where <dir> is the system root of your target system
  2. Build gcc and binutils to use as their default sysroot

The latter approach is the one that Angstrom uses, and it save a lot of butt-hurt.

marko
  • 9,029
  • 4
  • 30
  • 46
-3

You might want to try using crosstool-ng

It takes care of the majority of the work. You only need to bother about the configuration settings you want to enable for kernel compilation.

Here another reference: link

Community
  • 1
  • 1
spitfire88
  • 1,596
  • 3
  • 19
  • 31