Simple Makefile for multi-files - makefile

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
=======================

Related

Cross-compiling to arm64 against kernel source, does not find header files

I wrote a basic module that implements IOCTL handling. I'm trying to cross compile this module to arm64, against 4.19.114 kernel source. Here is my Makefile:
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
obj-m := chardev.o
KDIR := linux-4.19.114
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
Here is my module source:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#define NAME "chardev_roi"
#define MAGIC 'a'
#define READPARAM 'b'
#define WRITEPARAM 'c'
#define IOCTL_READ_ROI _IOR(MAGIC, READPARAM, int64_t*)
#define IOCTL_WRITE_ROI _IOW(MAGIC, WRITEPARAM, int64_t*)
static ssize_t ext_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
printk("Reading from driver\n");
return 0;
}
static ssize_t ext_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
printk("Writing to driver...\n");
return 0;
}
static long ext_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
char* _from_user_buffer = NULL;
switch (cmd)
{
case IOCTL_WRITE_ROI:
printk("IOCTL_WRITE_ROI just has benn called\n");
_from_user_buffer = kmalloc(32, GFP_KERNEL);
copy_from_user(_from_user_buffer, (char*) arg, sizeof(_from_user_buffer));
break;
case IOCTL_READ_ROI:
printk("IOCTL_READ_ROI just has been called\n");
copy_to_user((char*) arg, "Hello, World!\n", 14);
break;
}
}
static int init(void)
{
int major_number;
static struct file_operations _fops =
{
.owner = THIS_MODULE,
.read = ext_read,
.write = ext_write,
.unlocked_ioctl = ext_ioctl
};
major_number = register_chrdev(0, NAME, &_fops);
return 0;
}
Running this command: sudo ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -d -C linux-4.19.114 M=$(pwd) modules
Throws this error:
./include/uapi/linux/types.h:5:10: fatal error: asm/types.h: No such file or directory
The kernel configuration is defconfig without any changes. Tried to run make clean && make mrproper but it didn't help.
The problem was that I didn't generate the modules.
Run:
make <args> modules
Will probably solve the problem.

Cannot Load C dynamic library with C Program compile with liblua.a (lua5.3)

I first download lua-5.3.5 , and put the source in my working directory and compile it with
make linux
so I got the liblua.a and lua binary file in ./lua-5.3.5/src.
And then I write a C Dynamic Library like this :
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int l_sin(lua_State *L)
{
double d = luaL_checknumber(L, 1);
lua_pushnumber(L, sin(d)); /* push result */
return 1; /* number of results */
}
static const struct luaL_Reg mylib[] = {
{"mysin", l_sin},
{NULL, NULL}
};
extern int luaopen_mylib(lua_State* L)
{
luaL_newlib(L, mylib);
return 1;
}
I compile with command :
gcc mylib.c -I ./lua-5.3.5/src -fPIC -shared -o mylib.so -Wall
And if I use the original lua binary, it can be loaded
user00:lua/ $ ./lua-5.3.5/src/lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require 'mylib'
table: 0xd13170
>
But If I write A C program linked with liblua.a, it cannot load the dynamic library.
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(void){
char buff[256];
int error;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
while(fgets(buff, sizeof(buff), stdin) != NULL)
{
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0 , 0);
if(error)
{
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_close(L);
return 0;
}
Compile :
gcc test01.c -L ./lua-5.3.5/src/ -llua -lstdc++ -o test01 -lm -ldl -I ./lua-5.3.5/src
Run:
user00:lua/ $ ./test01
require 'mylib'
error loading module 'mylib' from file './mylib.so':
./mylib.so: undefined symbol: luaL_setfuncs
You need to export the Lua API functions from your executable. For this, link it with -Wl,-E as the Makefile from the Lua distribution does.

Can't cross-compile simple kernel module

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.

"Unknown symbol" shown when 'insmod' module1 calls function declared in module2

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.

How to print linux kernel version number in kernel module

I'm a noob to linux kernel programming and thought I'd be able to find the answer for this (since it seems really simple) but haven't had any luck yet. I need to make a linux kernel module that prints the version number of the kernel. The assignment requires that implement a module which displays this kind of message when loaded:
"Hello Master. You are currently using Linux (version)", where (version) is the kernel version no.
How can I do this? I tried using uname (http://man7.org/linux/man-pages/man2/uname.2.html) but when I include sys/utsname.h, I get a fatal error upon compiling with my makefile
"Cannot open include file: 'sys/utsname.h': No such file or directory".
Here is my module
#undef __KERNEL__
#define __KERNEL__
#undef __MODULE__
#define __MODULE__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <sys/utsname.h>
struct utsname unameData;
static int __init config_init(void)
{
uname(&unameData);
printk(KERN_INFO "Version number is %s\n", unameData.version);
return 0;
}
static void __exit config_exit(void)
{
printk(KERN_INFO "config_exit executed with success\n");
return;
}
module_init(config_init);
module_exit(config_exit);
MODULE_LICENSE("GPL");
Makefile
obj-m := config.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
Firstly, you need to use the correct headers and also the correct function utsname(). Following code work well for me.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/utsname.h>
static int __init my_init(void)
{
printk(KERN_INFO "Kernel version %s\n", utsname()->version);
printk(KERN_INFO "Kernel release %s\n", utsname()->release);
return 0;
}
static void __exit my_exit(void)
{
printk(KERN_INFO "exit module");
return;
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
dmesg output should looks something like this:
[ 1117.358451] Kernel version #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014
[ 1117.358457] Kernel release 3.13.0-37-generic

Resources