I have a simple question. I want to compile the following c file:
#include <linux/kernel.h>
#include <linux/module.h>
#include <sys/syscall.h>
extern void *sys_call_table[];
asmlinkage int (*original_sys_open)(int);
asmlinkage int our_fake_open_function(int error_code)
{
/*print message on console every time we
* *are called*/
printk("HEY! sys_open called with error_code=%d\n",error_code);
/*call the original sys_exit*/
return original_sys_open(error_code);
}
/*this function is called when the module is
* *loaded (initialization)*/
int init_module()
{
/*store reference to the original sys_exit*/
original_sys_open=sys_call_table[__NR_open];
/*manipulate sys_call_table to call our
* *fake exit function instead
* *of sys_exit*/
sys_call_table[__NR_open]=our_fake_open_function;
}
/*this function is called when the module is
* *unloaded*/
void cleanup_module()
{
/*make __NR_exit point to the original
* *sys_exit when our module
* *is unloaded*/
sys_call_table[__NR_open]=original_sys_open;
}
The problem is that gcc -I/usr/src/kernels/2.6.32-279.el6.x86_64/include myfile.c generates a unsolvabe include dependency which can be found in .
Please tell me how to fix this gcc compiler problem I am encountering today. Thank you very much for your insight.
How to build external modules is documented in the documentation:
$ make -C <path_to_kernel_src> M=$PWD
Related
I have a c++ webassembly module with several function that is called wasmExec.wasm. So far i have been exporting methods from c++ to JavaScript that are located in the cpp file where the main() method is located using the EMSCRIPTEN_KEEPALIVE keyword. That is working just fine. I do see those methods when i do
wasm-nm wasmExec.wasm
and i can call them from JavaScript and get the expected result. In order achieve clean methods name i have been disabling C++ name mangling.
Now i have tried to use EMSCRIPTEN_KEEPALIVE with static methods that are not located in the main.cpp file instead in a separate namespace and separate cpp and header files. In those classes i am also using EMSCRIPTEN_KEEPALIVE but unfortunately the produced wasm module is eliminating my method and thus i can not see them when executing wasm-nm wasmExec.wasm althout they are marked with EMSCRIPTEN_KEEPALIVE in the header as well as in the cpp file. i.e. here is an example
#ifndef API_REGISTRATION_REGISTRATIONMANAGER_H_
#define API_REGISTRATION_REGISTRATIONMANAGER_H_
#include <string>
#include <emscripten/emscripten.h>
using namespace std;
namespace api::registration {
#ifdef __cplusplus
extern "C" {
#endif
class RegistrationManager {
public:
explicit RegistrationManager();
virtual ~RegistrationManager();
static char* EMSCRIPTEN_KEEPALIVE registerClient(
char* userNameInputPtr, int userNameLength
);
};
#ifdef __cplusplus
}
#endif
}
#endif /* API_REGISTRATION_REGISTRATIONMANAGER_H_ */
I am using cmake to compile the project. In the target properties i have explicitly defined EXPORTED_FUNCTIONS to be equal to
set_target_properties(wasmExec PROPERTIES LINK_FLAGS "-std=c++17 -s WASM=1 -s TOTAL_MEMORY=512MB -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=1 -s VERBOSE=1 --pre-js /Projects/src/wasm/preModule.js -s DEMANGLE_SUPPORT=1 -s DISABLE_EXCEPTION_CATCHING=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s EXPORTED_FUNCTIONS='['registerClient']' -s EXTRA_EXPORTED_RUNTIME_METHODS='['cwrap', 'getValue', 'setValue', 'registerClient' ]' " )
What could be the reason to not be able to register this the method "registerClient" within the webassembly module. I have even tired to use the option -s EXPORT_ALL=1 but that did not exported all the know methods as i was expcting it to do
I am compiling everything statically here i.e. below is the example of the registration_api cmake sub-folder
MESSAGE( STATUS "ADDING API_REGISTRATION_DIR")
set( API_REGISTRATION_SRC
RegistrationManager.h RegistrationManager.cpp
)
set(LIB_TYPE STATIC)
add_library(api_registration ${LIB_TYPE} ${API_REGISTRATION_SRC} )
target_include_directories(api_registration PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/")
the compilation process is successful with emscripten, nevertheless i do get this warrning exactly about the method i am trying to export:
em++: warning: undefined exported function: "registerClient" [-Wundefined]
if i put that method in the main.cpp it is automatically exported without any problem.
My question is what could be the reason why this export is not working when the method is located in external cp/hpp file?
Addition 15 May 2020: I was able to register the method only if i remove the class definition, change the namesapce to the one that main have. Then it seems that the underscore in the EXPORTED_FUNCTIONS is important i.e. -s EXPORTED_FUNCTIONS='['_registerClient']' . That worked for me. I was under the impression that if i use static methods that should work with classes as well, but it seems that not to be the case.
#ifndef API_REGISTRATION_REGISTRATIONMANAGER_H_
#define API_REGISTRATION_REGISTRATIONMANAGER_H_
#include <string>
#include <emscripten/emscripten.h>
using namespace std;
namespace wasm {
#ifdef __cplusplus
extern "C" {
#endif
char* EMSCRIPTEN_KEEPALIVE registerClient(
char* userNameInputPtr, int userNameLength
);
#ifdef __cplusplus
}
#endif
}
#endif /* API_REGISTRATION_REGISTRATIONMANAGER_H_ */
thus my question has changed to: Can emscripten register a static method of a class with EMSCRIPTEN_KEEPALIVE or not?
I added a wait queue entry by calling DEFINE_WAIT and the code is failing to compile.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
DEFINE_WAIT(mywait);
static int __init test_hello_init(void)
{
return 0;
}
static void __exit test_hello_exit(void)
{
}
module_init(test_hello_init);
module_exit(test_hello_exit);
It fails with the below error, saying initializer element is not constant.
In file included from ./include/linux/thread_info.h:21:0,
from ./arch/x86/include/asm/preempt.h:7,
from ./include/linux/preempt.h:78,
from ./include/linux/spinlock.h:51,
from ./include/linux/seqlock.h:36,
from ./include/linux/time.h:6,
from ./include/linux/stat.h:19,
from ./include/linux/module.h:10,
from /home/linuxtrainer/Linux_Device_Drivers/day20/3_waitqueue/2/hello.c:2:
./arch/x86/include/asm/current.h:18:17: error: initializer element is not constant
#define current get_current()
Adding DEFINE_WAIT inside test_hello_init works, what is the difference?
DEFINE_WAIT() uses an initialiser that is not a compile-time constant - it needs to be executed in process context - so it can't be used at file scope.
It should be used directly in the function that is going to do the wait.
I'm trying to create a linux kernel module that will disable the data cache. I'm trying to use the v7_exit_coherency_flush(all) function in arch/arm/include/asm/cacheflush.h, and this function calls v7_flush_dcache_all, which I found is in arch/arm/mm/arch-v7.S.
My issue is that when I try to make my module, I get a warning
WARNING: "v7_flush_dcache_all [/home/pi/Documents/ARMHammer/kern/my_kernel/cache_disable.ko] undefined!
and when I try to insert the module I get an error
insmod: ERROR: could not insert module cache_disable.ko: Unknown symbol in module
So it looks like that ach-v7.S file isn't being read. I tried simply including it in my main file, but that produced a lot of errors, probably because its an assembly file.
I'm pretty much stuck at this point, is there someway I can include the assembly file in the Makefile, or maybe I'm not including all of the necessary .h files?
For what its worth, here's my main file
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h> /* For current */
#include <linux/tty.h> /* For the tty declarations */
#include <linux/version.h> /* For LINUX_VERSION_CODE */
#include <linux/mm.h>
#include <asm/cp15.h>
#include <asm/cacheflush.h>
#include <asm/glue-cache.h>
#include <asm/shmparam.h>
#include <asm/cachetype.h>
#include <asm/outercache.h>
// #include "my_cache-v7.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static void print_string(char *str)
{
struct tty_struct *my_tty;
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,5) )
my_tty = current->tty;
#else
my_tty = current->signal->tty;
#endif
if (my_tty != NULL) {
((my_tty->ops)->write) (my_tty, /* The tty itself */
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )
0, /* Don't take the string
from user space */
#endif
str, /* String */
strlen(str)); /* Length */
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )
((my_tty->ops)->write) (my_tty, 0, "\015\012", 2);
#else
((my_tty->ops)->write) (my_tty, "\015\012", 2);
#endif
}
}
static int __init print_string_init(void)
{
v7_exit_coherency_flush(all);
print_string("The module has been inserted. Hello world!");
return 0;
}
static void __exit print_string_exit(void)
{
print_string("The module has been removed. Farewell world!");
}
module_init(print_string_init);
module_exit(print_string_exit);
and my Makefile
obj-m += cache_disable.o
KDIR = /home/pi/linux/
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
Also, if anybody knows an easier way to disable the cache, I'm all ears!
v7_exit_coherency_flush() is for power management code to take a CPU out of the kernel cleanly in order to power it off - it's not callable from random modules for very good reason. If you really want to lose data and crash the machine in weird and subtle ways, you might as well just bypass kernel functions entirely and use a trivial inline asm to hit the SCTLR directly*.
I dread to imagine what you're trying to achieve, but if you really want to run Linux (painfully slowly) with the cache off, you'll need to rebuild the kernel, turning off CONFIG_SMP in order to turn on CONFIG_CPU_DCACHE_DISABLE. That's the only vaguely supported method which might work.
* I'm not even going to explain that, it's that terrible an idea.
I've searched for the error LNK2005 "already defined in .obj" but can't find content related to the specific problem I am facing. Hope someone can help me on this...
I've a header foo.h
// foo.h
#ifndef FOO_H
#define FOO_H
void foo() {
print("foo\n");
}
#endif
and main file... main.cpp
// main.cpp
#include <thread>
#include "foo.h"
int main() {
std::thread t(foo);
t.join();
return 0;
}
Now, it compile without any errors and gives the gives output to the console...
foo
But if I create another file foo.cpp and just include the header foo.h and do nothing else...
// foo.cpp
#include "foo.h"
...I get linker error LNK2005 "void __cdecl foo(void)" (?foo##YAXXZ) already defined in main.obj
Don't know what's going wrong here.?!!
You must place only the prototype of the foo() function in the header file, and the implementation once in the .cpp.
Thus, foo.h must contain:
#pragma once
void foo();
And foo.cpp:
#include "foo.h"
void foo() {
printf("Whatever");
}
I'm trying to compile a network application on Ubuntu 12.04 using GCC and glibc 2.15
Let's consider the following code as the example:
a.cpp:
#include <sys/select.h>
void func ()
{
int fd;
fd_set fds;
FD_SET(fd, &fds);
}
I can successfully compile these lines with the command "gcc -c -Wsign-conversion a.cpp", but I have the following warning after I add either -O1 or -O2 option:
gcc -c -O1 -Wsign-conversion a.cpp
a.cpp:6: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
I have the warning for both gcc 4.4, 4.5 and 4.6.
UPD: If I understand correctly, my example strictly conforms to FD_SET semantics, so I should have no warnings in this case.
What's the reason of this? How can I avoid it?
Thanks.
UPD: Looks like it's the known issue now - http://comments.gmane.org/gmane.comp.lib.glibc.alpha/22344 . But I can't understand what should I do with it on GLIBC 2.15? Just wait for the next GLIBC?
To fix this problem, I used a separate wrapper source file, and disabled warnings for just that source file.
CMacroWrapper.h:
#include <sys/select.h>
namespace CMacroWrapper
{
void FdSet(int fd, fd_set *set);
}
CMacroWrapper.cpp:
#include "CMacroWrapper.h"
#pragma GCC diagnostic ignored "-Wsign-conversion"
void CMacroWrapper::FdSet(int fd, fd_set *set)
{
FD_SET(fd, set);
}
main.cpp:
#include "CMacroWrapper.h"
void func ()
{
int fd;
fd_set fds;
FD_ZERO(&fds);
CMacroWrapper::FdSet(fd, &fds);
}
I find that CMacroWrapper namespace useful to just add any new system header macros that pop up in compiler warnings. It does add an extra function call, but it's worth it for the warning peace-of-mind. Or you can wrap all the FD_ macros for consistency, if you like.