Error: Unknown symbol in module? - c

I wrote a simple Linux kernel module:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/reboot.h>
static int my_init(void)
{
machine_power_off();
return 0;
}
static void my_exit(void)
{
return;
}
module_init(my_init);
module_exit(my_exit);
Source compiled successfully but after installing the module (insmod) the following error occurred:
Error: could not insert module my_module.ko: Unknown symbol in module
System log error:
Jun 25 21:50:00 my-virtual-machine kernel: [31625.207827] my_module: Unknown symbol machine_power_off (err 0)
How do I solve this error?
machine_power_off:
http://lxr.free-electrons.com/ident?i=machine_power_off

#Amir, traversed through the files in the linux kernel, w.r.t. all the architecture files where machine_power_off() is used, it is not exported so cannot be used in your module.

Related

Kernel module is removed successfully although warning message 'not found"

Yesterday I installed a very simple kernel module in my embedded Linux system. "hello.ko".
When I install this module to our system, it's normally:
#insmod hello.ko
Initing kernel module
#
#lsmod
hello 638 0 - Live 0xf628f000
But when I remove this kernel module, It can be removed normally but rmmod notice 'hello.ko' not found
# rmmod hello
Cleaning kernel module
rmmod: module 'hello' not found
#lsmod <------------------- already be removed
#
# uname -a
# Linux SWITCH 2.6.32 #8 Thu Jun 23 20:34:48 KST 2016 ppc GNU/Linux
It's very strange. Could anyone explain for me why 'hello.ko' is removed but there's log 'rmmod module not found'?
The following are source code of hello.ko
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/config.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/string.h>
int init_module(void)
{
printk (KERN_EMERG "Initing kernel module\n");
return 0;
}
void cleanup_module(void)
{
printk (KERN_EMERG "Cleaning kernel module\n");
}
MODULE_DESCRIPTION ("Kernel function replacement module");
MODULE_AUTHOR ("DASAN Networks Inc.");
MODULE_LICENSE ("GPL");
You need to add few things in my opinion __init and __exit macros and module_init and module_exit macros, try with:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/string.h>
static int __init init_example(void)
{
printk (KERN_EMERG "Initing kernel module\n");
return 0;
}
static void __exit cleanup_example(void)
{
printk (KERN_EMERG "Cleaning kernel module\n");
}
module_init(init_example);
module_exit(cleanup_example);
MODULE_DESCRIPTION ("Kernel function replacement module");
MODULE_AUTHOR ("DASAN Networks Inc.");
MODULE_LICENSE ("GPL");

Kernel Module: No printk messages showing. Is init function being called?

I have a simple module, written as follows:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init hellomod_init(void)
{
printk(KERN_DEBUG, "Hello, world!\n");
return 0;
}
static void __exit hellomod_exit(void)
{
printk(KERN_DEBUG, "Goodbye, world!");
}
module_init(hellomod_init);
module_exit(hellomod_exit);
and
$ cat /proc/sys/kernel/printk
7 7 7 7
so that any level messages should output.
The module loads and this can be verified with lsmod, however no output is produced when loading or unloading the module and checking dmesg.
I have tried replacing KERN_DEBUG with lower levels and still no output, so I don't think the log level is the issue.
How else can I verify the hellomod_init() function is called? If it is not being called, what is my error?
I am running and compiling against kernel version 4.6.1-2 on Arch Linux.
There shouldn't be a comma after KERN_DEBUG. So it should look something like this:
static int __init hellomod_init(void)
{
printk(KERN_DEBUG "Hello, world!\n");
return 0;
}

Why static library not working?

I tried making this simple static library in Code::Blocks with MinGW:
#include <time.h>
#include <sys/time.h>
#include "header.h"
extern int go(int x)
{
struct timeval t;
gettimeofday(&t, NULL);
return 1;
}
This same snippet works if compiled as a console app (with printf), but when compiled as a static library, the calling code keeps getting this error:
unresolved external symbol "_gettimeofday"

C / CUDA implementation with external variables produces linker error (unresolved external symbol)

I have a C-Project, which I would like to boost using a CUDA-module. But somehow, the externally defined variables can not be resolved. I am using Microsoft Visual C++ 2010 Express and CUDA Toolkit 5.0.
The following shows my minimal (not) working example:
main.c:
#include "main.h"
#include <stdio.h>
#include "cuda_test.cu"
int main( int argc, const char* argv[] )
{
testfunc();
return 1;
}
main.h:
#ifndef main_h
#define main_h
extern float PI;
#endif
testfile.c:
#include "main.h"
float PI = 3;
cuda_test.cu:
#include "main.h"
#include <stdio.h>
void testfunc()
{
printf("Hello from cudafile: %E", PI);
}
This yields the following error:
1>------ Build started: Project: cuda_min, Configuration: Debug Win32 ------
1>cuda_test.cu.obj : error LNK2001: unresolved external symbol "float PI" (?PI##3MA)
1>D:\Backup\diplomarbeit\cuda_xanthos\cuda_min\Debug\cuda_min.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
When passing the variable PI to the function testfunc, I get the desired behavior. This is what I am doing in my project (which actually uses the CUDA-device), but I really do not want to pass about 20 variables to my functions.
I suppose I am missing some setting for nvcc...
Any help would be much appreciated.
.cu is compiled and linked as .cpp, not .c. So, you can either rename your .c files to .cpp, or use extern "C" in your .cu file.

Apple Mach-O Link Error with curses.h

I've been getting this error numerous times in my program. I've simplified things down a bit to illustrate the basics and am still getting errors. I was told that I needed to add this library file to my project for it to work (libncurses.dylib) and it did solve some problems, but not this one.
Here is my code:
// screen.h
#ifndef screen_h
#define screen_h
#define MAC 1
#define WIN 2
#define LNX 3
#ifdef PLATFORM
#undef PLATFORM
#endif
#define PLATFORM MAC
void screen_erase();
#endif
// screen.c
#include <string.h>
#include <stdlib.h>
#include "screen.h"
#if PLATFORM == MAC
#include <curses.h>
void screen_erase(){
erase();
}
#endif
// main.cpp
#include <iostream>
#include <curses.h>
#include "screen.h"
using namespace std;
int main(){
screen_erase();
}
And here's the error I am getting:
Undefined symbols for architecture x86_64:
"screen_erase()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation
What's going on here?
It's because you mix two different languages: C and C++.
In the screen.h header file, change the declaration to this:
#ifdef __cplusplus
extern "C" {
#endif
void screen_erase();
#ifdef __cplusplus
}
#endif
That tells the C++ compiler to not do name mangling on the screen_erase function name.

Resources