LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (http://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Kernel module - C & assembly files - makefile problems (http://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-c-and-assembly-files-makefile-problems-4175426011/)

paulcsf 09-06-2012 02:59 PM

Kernel module - C & assembly files - makefile problems
 
I am working on building a kernel module, architecture is Arm (armv7-m), tool chain is arm-uclinuxeabi-gcc and family.

I have a C file, sample.c that builds as a kernel module, loads, executes, all is fine.

I have an assembly file, mmcau.S, that builds independently.

However, when I try to include the assembly file in the kernel module build process, I can not get the make file to cooperate. I have read that there are implicit rules for .S -> .o, but I am left wondering if that is true.

To build sample.c by itself:
Code:

CFLAGS                := -Os -mcpu=cortex-m4 -mthumb
LDFLAGS                := -mcpu=cortex-m4 -mthumb
CC                = $(CROSS_COMPILE_APPS)gcc

obj-m                += sample.o

KERNELDIR        :=  $(INSTALL_ROOT)/linux

modules                :
        make -C $(KERNELDIR) M=`pwd` modules CFLAGS="" LDFLAGS=""

clean_modules:
        make -C $(KERNELDIR) M=`pwd` clean
        rm -f modules.order

To build mmcau.S by itself:
Code:

AS                := $(CROSS_COMPILE_APPS)gcc
#AS                := $(CROSS_COMPILE_APPS)as
ASFLAGS                := -x assembler-with-cpp -mcpu=cortex-m4 -mthumb

#Rule for assembling .s -> o files
$(SOBJ)                : %.o : %.S
                  @echo Assembling
                  $(AS) $(ASFLAGS) -c $< -o $@

mmcau                : mmcau.S
                  @echo Assembling mmcau.S
                  $(AS) $(ASFLAGS) -c $<
# with the -o $@ creates an application
# w/o  the -o $@ creates a .o

However, when I add mmcau.o to obj-m for the kernel module,
Code:

obj-m                += sample.o
obj-m                += mmcau.o

I get an error like:
Code:

make[2]: *** No rule to make target `..../app/mmcau.c', needed by `.../app/mmcau.o'.  Stop.
I haven't been able to get past this error. Some variations of makefile cause an output module to be created, but loading it in the kernel causes a kernel taint error regarding the licensing, and the entire system crashes when the module is accessed.

Thanks, paulcsf

paulcsf 09-06-2012 11:23 PM

I believe that the output kernel module name cannot match any of the source files.

There were two other error cases/clues that I found. Modinfo reports little information, only the filename and vermagic, no author, license, description, or parms. Another case would have the module load into the kernel, but would fail to do anything, no output, the device would exist but couldn't be opened, etc.

Here is the solution I came up with...

Code:

obj-m                += KModule.o
KModule-objs        := sample.o mmcau.o

KERNELDIR        :=  $(INSTALL_ROOT)/linux

modules                :
        make -C $(KERNELDIR) M=`pwd` modules CFLAGS="" LDFLAGS=""

clean_modules:
        make -C $(KERNELDIR) M=`pwd` clean
        rm -f modules.order

A note, I believe that adding to obj-m creates more kernel modules to build, it doesn't add source files and dependencies to the current build. So:
Code:

obj-m          += KMod1.o
obj-m          += KMod2.o

builds two kernel modules, KMod1 and KMod2. For KMod1 to have multiple sources, KMod1-objs := must be defined with multiple files, none of which are named KMod1.c or KMod1.S.

Can anybody confirm this? It does work.

Thanks, paulcsf


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