i am not getting output for this code. What changes have to make in my Makefile??
code:
#define MODULE
#define LINUX
#define __KERNEL__
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>``
MODULE_LISENCE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static u8 (mybyte='A');
static int (myint='i');
MODULE_PARM(mybyte, "b");
MODULE_PARM(myint, "i");
MODULE_PARM_DESC(mybyte, "this cannot do anything\n");
static int __init hello5_init(void)
{
printk("my byte %i\n",mybyte);
printk("my integer %i \n",myint);
}
static void __exit hello5_exit()
{
printk("bye world\n");
}
module_init(hell05_init);
module_exit(helo5_exit);
Makefile:
obj-m += hello5.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
exit:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
.PHONY: all, exit
Error:
/home/kumarmagi/Desktop/amit/hello5/hello5.c:23:24: error: ‘mybyte’ undeclared (first use in this function)
/home/kumarmagi/Desktop/amit/hello5/hello5.c:23:24: note: each undeclared identifier is reported only once for each function it appears in
/home/kumarmagi/Desktop/amit/hello5/hello5.c:24:28: error: ‘myint’ undeclared (first use in this function)
/home/kumarmagi/Desktop/amit/hello5/hello5.c:25:1: warning: no return statement in function returning non-void [-Wreturn-type]
Try to change:
MODULE_PARM(mybyte, "b");
MODULE_PARM(myint, "i");
static int (myint='i');
static u8 (mybyte='A');
to:
module_param(mybyte, short, 0660);
module_param(myint, int, 0660);
static short mybyte='A';
static int myint='i';
Related
I'm trying to compile following 'Hello World'.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("BOBR");
MODULE_DESCRIPTION("HELLO MODULE");
MODULE_VERSION("0.10");
static char *name = "user";
module_param(name, charp, S_IRUGO);
MODULE_PARM_DESC(name, "The name to display");
static int __init hello_init(void) {
printk(KERN_INFO "HEllo, %s!\n", name);
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, %s !\n", name);
}
module_init(hello_init);
module_exit(hello_exit);
My Makefile is
ifneq ($(KERNELRELEASE),)
include Kbuild
#KDIR ?= ../repos/linux
else
KDIR ?= /lib/modules/4.9.0-6-amd64/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
clean:
$(MAKE) -C $(KDIR) M=$$PWD clean
endif
I start make as: make ARCH=arm CROSS_COMPILE='ccache arm-eabi-' PATH=/opt/gcc-linaro-7.2.1-2017.11-x86_64_arm-eabi/bin:$PATH
I compile on Debian 4.9.0-6-amd64. Target is arm32 4.16.18.
The problem is that I get a module for my host machine (I can insmod it), but the target kernel rejects it as invalid module.
When I try to change the linux path in the Makefile to the target kernel sources, the compilation fails with fatal error: asm/barrier.h: No such file or directory.
I am newbee to linux kernel. Currently I want to have 2 module they can interact with each other. I've tried to call function2 from module2 in module1 by EXPORT_SYMBOL_GPL. When I 'insmod' module1 it tells me function2 is "unknown symbol" to module1. I did export function2 and add -DEXPORT_SYMTAB in Makefile. What else I miss? Any advice great appreciate.
Here's my hello samples
/* *************************************************************************************
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include "hello2.h"
static int __init init_module1(void)
{
printk(KERN_INFO "Hello world 1.\n");
function2();
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
static void __exit cleanup_module1(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
module_init(init_module1);
module_exit(cleanup_module1);
MODULE_DESCRIPTION("Hello1");
MODULE_LICENSE("GPL");
/* *************************************************************************************
* hello-2.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
static void function2 (void)
{
printk(KERN_INFO "Function called in hello2.\n");
}
EXPORT_SYMBOL_GPL(function2);
static int __init init_module2(void)
{
printk(KERN_INFO "Hello world 2.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
static void __exit cleanup_module2(void)
{
printk(KERN_INFO "Goodbye world 2.\n");
}
module_init(init_module2);
module_exit(cleanup_module2);
MODULE_DESCRIPTION("Hello2");
MODULE_LICENSE("GPL");
/* *************************************************************************************
* hello-2.h - The simplest kernel module header
*/
extern void function2 (void);
################################################
# Makefile for hello2
PWD := $(shell pwd)
KVERSION := $(shell uname -r)
KDIR := /lib/modules/$(shell uname -r)/build
KERNEL_DIR := /usr/src/linux-headers-$(KVERSION)/
INSTALL_PATH := /lib/modules/$(shell uname -r)/extra
EXTRA_CFLAGS = -DEXPORT_SYMTAB
MODULE_NAME = hello2
obj-m := hello2.o
#.PHONY: all clean insert
.PHONY: all clean
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
rm -rf *.o *.ko *.mod.* .c* .t*
insert:
$(MAKE) INSTALL_MOD_DIR=$(INSTALL_PATH) -C $(KDIR) M=$(PWD) modules_install
In hello-2.c you have:
static void function2 (void)
{
...
}
Please consider making it non-static:
void function2(void)
{
...
}
I found the solution from an old post:
insmod fails with "Unknown symbol in module" for a symbol defined in another module
Turns out 2 modules need to be built at the same time. I built they separately with 2 different Makefile.
I have compiled this code:
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/genhd.h> // declaration of printk_all_partitions()
MODULE_LICENSE("GPL");
int __init start (void) {
printk ("evaluating the module ") ;
printk_all_partitions();
printk (" module inserted .. ");
return 0 ;
}
void __exit x(void) {
printk ("module unloaded ..");
}
module_init(start);
module_exit (x);
MODULE_LICENSE("GPL");
makefile is:
EXTRA_CFLAGS += -Wall
obj-m += printk_all_partitions.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
when I run make I get
WARNING: "printk_all_partitions" [/root/c++/modulez/multiple_source_files/printk_plm/printk_all_partitions.ko] undefined!
egrep -w 'printk|printk_all_partitions' /proc/kallsyms :
ffffffff8162f135 T printk
ffffffff81ac7b46 T printk_all_partitions
Now the only difference between printk and printk_all_partitions is that printk_all_partitions does not exist in the Module.symvers
and printk is:
0x27e1a049 printk vmlinux EXPORT_SYMBOL
By the time i finished the writing I found out that a function must be EXPORT_SYMBOL-ed too in order for it to work
but if (somehow) Modules.symvers was deleted , and the kernel sources were not available, both printk and printk_all_partitions are begin with a "T", what other way to know is one is exported or not?
Normally this line
obj-m += printk_all_partitions.o
is the name of the module you're trying to create. In this case you're naming your module the same as an existing symbol, is this deliberate?
I am just wondering why would go runtime fail to build. How do we pass flags (-fpermissive in this case) to the c compiler which golang compiler is using to build the runtime. I am using gcc-4.6.2 on ubuntu 12.04
../../../thirdparty/go1.4.2/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/src/runtime/cgo/gcc_linux_amd64.c: In function ‘void _cgo_sys_thread_start(ThreadStart*)’:
../../../thirdparty/go1.4.2/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/src/runtime/cgo/gcc_linux_amd64.c:45:41: error: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
A sample program written also fails to compile, it seems the nil defined in the go code is the problem, i wonder how others are working, when does the golang compiler compiles this runtime code ?
gcc t.c -lpthread -o t
t.c: In function ‘void* hello_world(void*)’:
t.c:12:41: error: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
/usr/include/x86_64-linux-gnu/bits/sigthread.h:31:12: error: initializing argument 3 of ‘int pthread_sigmask(int, const __sigset_t*, __sigset_t*)’ [-fpermissive]
rk#rk-VirtualBox:~$ gcc -fpermissive t.c -lpthread -o t
t.c: In function ‘void* hello_world(void*)’:
t.c:12:41: warning: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
rk#rk-VirtualBox:~$ cat t.c
#include<pthread.h>
#include<stdio.h>
#include<signal.h>
#define nil ((void*)0)
static void*
hello_world(void *vptr)
{
sigset_t set;
sigemptyset(&set);
pthread_sigmask(SIG_BLOCK, &set, nil);
printf("hello world");
return NULL;
}
int main(int ac, char **av)
{
pthread_t t;
pthread_create(&t, NULL, hello_world, NULL);
pthread_join(t, NULL);
return 0;
}
/usr/include/x86_64-linux-gnu/bits/sigthread.h:31:12: error: initializing argument 3 of ‘int pthread_sigmask(int, const __sigset_t*, __sigset_t*)’ [-fpermissive]
make: *** [rulemanager] Error 2
all:
I have 2 files, module1.c and module2.c which contains functions needed by the third file, big_module.c. My Makefile complained of not finding functions defined in module1.c and module2.c. These functions are needed by big_module.c Could you please help me defining my Makefile so it would work?
module1.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
int module1 (struct file *filp, struct vm_area_struct *vma)
{ return 0; }
int __init init_module1 (void)
{ return 0; }
void __exit cleanup_module1 (void)
{ }
module2.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <asm/io.h>
unsigned long virt_addr;
int module2(struct file * filp, struct vm_area_struct * vma)
{ return 0; }
int __init init_module2 (void)
{ return 0; }
void __exit cleanup_module2 (void) { }
big_module.c
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/vt_kern.h>
#include <linux/fs.h>
MODULE_DESCRIPTION("Example");
MODULE_AUTHOR("Your Name Here");
MODULE_LICENSE("GPL");
static int __init hello_init(void)
{
init_module1();
init_module2();
return 0;
}
static void __exit hello_cleanup(void)
{
cleanup_module1();
cleanup_module2();
}
module_init(hello_init);
module_exit(hello_cleanup);
Makefile:
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m += big_module.o
obj-m += module1.o
obj-m += module2.o
modules:
$(MAKE) -C $(KDIR) M=$(PWD) modules
Errors:
> uu#uu-VirtualBox:~/UCSC-Ext/LDDII/Assignment3/ASK$ sudo make
> make -C /lib/modules/3.10.0uu/build M=/home/uu/UCSC-Ext/LDDII/Assignment3/ASK modules
> make[1]: Entering directory `/usr/src/linux-3.10'
> CC [M] /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.o
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c: In function ‘hello_init’:
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c:14:2: error: implicit declaration of function ‘init_module1’
> [-Werror=implicit-function-declaration]
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c:15:2: error: implicit declaration of function ‘init_module2’
> [-Werror=implicit-function-declaration]
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c: In function ‘hello_cleanup’:
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c:21:2: error: implicit declaration of function ‘cleanup_module1’
> [-Werror=implicit-function-declaration]
> /home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.c:22:2: error: implicit declaration of function ‘cleanup_module2’
> [-Werror=implicit-function-declaration]
> cc1: some warnings being treated as errors
> make[2]: *** [/home/uu/UCSC-Ext/LDDII/Assignment3/ASK/big_module.o] Error 1
> make[1]: *** [_module_/home/uu/UCSC-Ext/LDDII/Assignment3/ASK] Error 2
> make[1]: Leaving directory `/usr/src/linux-3.10'
> make: *** [modules] Error 2
all:
I found the solution to my question. It's in the Makefile which should look
like the following:
Makefile
=======================
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m := big_module.o
big_module-objs := module1.o module2.o
modules:
$(MAKE) -C $(KDIR) M=$(PWD) modules
=======================