0

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
Sharon Lee
  • 23
  • 4

2 Answers2

0

In hello-2.c you have:

static void function2 (void)
{
    ...
}

Please consider making it non-static:

void function2(void)
{
    ...
}
  • Thank you for response to my post. I've tried to change 'function2' to non-static, but I still get "Unknown symbol function2" after 'sudo insmod hello1.ko'. It seems kernel can't find 'function2' even I add 'DEXPORT_SYMTAB' to compile flag. Is there anyway to export 'hello2.ko' symbol to kernel? – Sharon Lee Sep 12 '17 at 01:05
  • I see that you discovered another one reason of the failure. I upvote your post - it's very helpful. However, making the target function non-static is also required. So, actually there were *multiple* reasons why your module failed, and leaving the function *static* was one of them. –  Sep 12 '17 at 11:23
  • Thank you for pointing out the non-static part. It's great to have people like you contributing knowledge to help beginner like me! – Sharon Lee Sep 13 '17 at 16:35
0

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.

Sharon Lee
  • 23
  • 4