MODULE_LICENSE is defined but undetected in linux kernel 5.X - c

I have a simple kernel module with multiple objects. When I compile it, it prompts me that MODULE_LICENSE is missing. It's defined in the main.o but somehow undetected by the compiler:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
make[1]: Entering directory '/usr/src/kernels/5.14.0-105.el9.x86_64'
CC [M] /tmp/linux5-multi-files/util.o
LD [M] /tmp/linux5-multi-files/main.o
MODPOST /tmp/linux5-multi-files/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /tmp/linux5-multi-files/main.o
make[2]: *** [scripts/Makefile.modpost:150: /tmp/linux5-multi-files/Module.symvers] Error 1
make[2]: *** Deleting file '/tmp/linux5-multi-files/Module.symvers'
make[1]: *** [Makefile:1792: modules] Error 2
make[1]: Leaving directory '/usr/src/kernels/5.14.0-105.el9.x86_64'
make: *** [Makefile:5: build] Error 2
On older Linux (centos 6-7) it works fine, but on CentOS 8 and CentOS 9, it no longer works. What was wrong? I've attached all the files.
-- attached all files --
Makefile
main-objs := util.o
obj-m += main.o
build:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
clean:
make -C /lib/modules/`uname -r`/build M=`pwd` clean
main.c
#include "util.h"
static void __exit cleanup(void)
{
}
static int __init startup(void)
{
test();
return 0;
}
module_init(startup);
module_exit(cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux");
util.h
#ifndef UTIL_H
#define UTIL_H
#include <linux/module.h>
#include <linux/kernel.h>
void test(void);
#endif
util.c
#include "util.h"
void test()
{
printk(KERN_INFO "whatever\n");
}

To further debug this issue, I've added -v to the ccflags, and main.c is not even compiled on CentOS 8!
So far it looks like the module name can't be the same as the object name, so I have to modify the makefile to something like this:
xxx-objs := main.o util.o
obj-m += xxx.o
And the problem is gone.

Related

KBuild fails while compiling without debugging flag

I am trying to compile a very simple module, but it fails to create *.ko when i am not adding -g flag. Here is the source code:
#include <linux/init.h>
#include <linux/module.h>
static int simple_test_init(void){
printk(KERN_INFO "Hello Kernel!");
return 0;
}
static void simple_test_exit(void){
printk(KERN_INFO "Goodbye Kernel!");
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple test module");
module_init(simple_test_init);
module_exit(simple_test_exit);
And here is GCC/Makefile.
KBUILD_CFLAGS := -Werror -fcf-protection=branch
obj-m += test.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
And finally the output of GCC.
make -C /lib/modules/5.19.3-arch1-1/build M=/test modules
make[1]: Entering directory '/usr/lib/modules/5.19.3-arch1-1/build'
CC [M] /test/test.o
/test/test.o: warning: objtool: folio_flags+0x29: 'naked' return found in RETHUNK build
/test/test.o: warning: objtool: simple_test_init+0x22: 'naked' return found in RETHUNK build
/test/test.o: warning: objtool: simple_test_exit+0x1e: 'naked' return found in RETHUNK build
/test/test.o: warning: objtool: folio_flags+0x29: missing int3 after ret
/test/test.o: warning: objtool: simple_test_init+0x22: missing int3 after ret
/test/test.o: warning: objtool: simple_test_exit+0x1e: missing int3 after ret
MODPOST /test/Module.symvers
CC [M] /test/test.mod.o
LD [M] /test/test.ko
BTF [M] /test/test.ko
pahole: /test/test.ko: No such file or directory
make[3]: *** [scripts/Makefile.modfinal:60: /test/test.ko] Error 1
make[3]: *** Deleting file '/test/test.ko'
make[2]: *** [scripts/Makefile.modpost:134: __modpost] Error 2
make[1]: *** [Makefile:1763: modules] Error 2
make[1]: Leaving directory '/usr/lib/modules/5.19.3-arch1-1/build'
make: *** [Makefile:6: all] Error 2
Thank you.

C Programming with NetBeans on Mac OSX

I am pretty new to programming and I am trying to learn C. I installed net beans 8.0.2 on a mac with OS 10.10.3. And I tried to run my first code:
/* HelloWorld.c */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello PROGC"); /* prints > Hello PROGC */
return EXIT_SUCCESS;
}
Now I am getting this Error Message:
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/cppapplication_1
mkdir -p build/Debug/GNU-MacOSX
rm -f "build/Debug/GNU-MacOSX/Hello World.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/Hello World.o.d" -o build/Debug/GNU-MacOSX/Hello\ World.o Hello\ World.c
mkdir -p dist/Debug/GNU-MacOSX
gcc -o dist/Debug/GNU-MacOSX/cppapplication_1 build/Debug/GNU-MacOSX/Hello\ World.o build/Debug/GNU-MacOSX/main.o
duplicate symbol _main in:
build/Debug/GNU-MacOSX/Hello World.o
build/Debug/GNU-MacOSX/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [dist/Debug/GNU-MacOSX/cppapplication_1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 687ms)
Could you please let me know what I have to change? Many thanks in advance!

How to write and compile kernel module from multiple sources? (WARNING: "do_function" [... main.ko] undefined)

I want to separate my kernel module to sources. But I can't compile it properly.
print_hello.h
#ifndef PRINT_HELLO_H_
#define PRINT_HELLO_H_
void print_hello(void);
#endif /* PRINT_HELLO_H_ */
print_hello.c:
#include <linux/kernel.h>
#include "print_hello.h"
void print_hello(void) {
printk("Hello, World!\n");
}
main.c:
#include<linux/module.h>
#include<linux/version.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include "print_hello.h"
int hello_init(void) {
print_hello();
return 0;
}
module_init(hello_init);;
MODULE_LICENSE("GPL");
Makefile:
obj-m += main.o
main-objs := print_hello.o main.o
all:
make -C /lib/modules/3.2.51/build M=$(PWD) modules
clean:
make -C /lib/modules/3.2.51/build M=$(PWD) clean
but when I compile this program this error occur:
root#linux:r# make
make -C /lib/modules/3.2.51/build M=/home/root/hello modules
make[1]: Entering directory `/usr/src/build'
CC [M] /home/root/hello/main.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "print_hello" [/home/root/hello/main.ko] undefined!
LD [M] /home/root/hello/foo.ko
make[1]: Leaving directory `/usr/src/build'
Any solution to solve this problem?

using header in netbeans C

I tried to compile a simple C program with a header, a main, and another source file in Netbeans but it didn't work. I always get a huge error message.
I have absolutely no idea what I can do to make it work. Hope you guy can help me.
console:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/***/***/***/***/***/headertest'
nbproject/Makefile-Debug.mk:73: warning: overriding commands for target `build/Debug/GNU-Linux-x86/header.o'
nbproject/Makefile-Debug.mk:68: warning: ignoring old commands for target `build/Debug/GNU-Linux-x86/header.o'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/headertest
make[2]: Entering directory `//home/***/***/***/***/***/headertest'
nbproject/Makefile-Debug.mk:73: warning: overriding commands for target `build/Debug/GNU-Linux-x86/header.o'
nbproject/Makefile-Debug.mk:68: warning: ignoring old commands for target `build/Debug/GNU-Linux-x86/header.o'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/header.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/header.o.d" -o build/Debug/GNU-Linux-x86/header.o header.h
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/main.o.d"
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.c
mkdir -p dist/Debug/GNU-Linux-x86
gcc -o dist/Debug/GNU-Linux-x86/headertest build/Debug/GNU-Linux-x86/header.o build/Debug/GNU-Linux-x86/header.o build/Debug/GNU-Linux-x86/main.o
/usr/bin/ld:build/Debug/GNU-Linux-x86/header.o: file format not recognized; treating as linker script
/usr/bin/ld:build/Debug/GNU-Linux-x86/header.o:1: syntax error
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/headertest] Error 1
make[2]: Leaving directory `/home/***/***/***/***/***/headertest'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/***/***/***/***/***/headertest'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 283ms)
Here is the rest of my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char** argv) {
lol();
return (EXIT_SUCCESS);
}
header.c
#include "header.h"
void lol(){
printf("lol");
}
header.h
#ifndef HEADER_H
#define HEADER_H
#ifdef __cplusplus
extern "C" {
#endif
void lol();
#ifdef __cplusplus
}
#endif
#endif /* HEADER_H */
You have not included stdio.h. Your lol() function doesn't know printf().
Put a #include <stdio.h> either into header.c or header.h.

What is wrong With make?

C
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
/* makefile */
/* uname -r = 2.6.32-5-686 */
obj-m += hello.o
all :
make -C /lib/modules/2.6.32-5-686/build /home/mgh/Documents modules
clean :
make -C /lib/modules/2.6.32-5-686/build /home/mgh/Documents clean
root#localhost:/home/mgh/Documents# make
make -C /lib/modules/2.6.32-5-686/build /home/mgh/Documents modules
make[1]: Entering directory /usr/src/linux-headers-2.6.32-5-686'
/usr/src/linux-headers-2.6.32-5-common/scripts/Makefile.build:44: /usr/src/linux-headers-``2.6.32-5-common/scripts/basic/Makefile: No such file or directory
make[5]: *** No rule to make target `/usr/src/linux-headers-2.6.32-5-common/scripts/basic``/Makefile'. Stop.
make[4]: *** [scripts_basic] Error 2
make[3]: Nothing to be done for `/home/mgh/Documents'.
make[3]: *** No rule to make target `include/config/auto.conf', needed by `include/config``/kernel.release'. Stop.
make[2]: *** [sub-make] Error 2
make[1]: *** [all] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-5-686'
make: *** [all] Error 2
Guesswork: You did not set the M environment variable in your custom Makefile. See http://tldp.org/LDP/lkmpg/2.6/html/x181.html
And the next time, please do indeed ask a more specific question than 'What is wrong?'
I dispute that anything is wrong with make. I am very certain, though, that something is wrong with your Makefile. According to some random internet searching, try something like this:
make -C /lib/modules/2.6.32-5-686/build SUBDIRS=/home/mgh/Documents modules
This is assuming that your Makefile resides in /home/mgh/Documents.

Resources