我正在尝试在内核 3.19 中导入新的系统调用。我已经按照给定的教程 here 进行操作!
这是我通过系统调用实现阶乘计算的简单代码。
#include <linux/kernel.h>
asmlinkage long sys_fact(int a)
{
int n;
int c;
for(n = 1;n <= a;n++)
c = c * n;
printk(KERN_INFO "Factorial calculated!\n");
return((long) c);
}
当我尝试编译 C 代码时,出现 Undefined reference to sys_fact 错误。 我在其中使用该系统调用的程序如下。
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
int n;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
printf("Factorial of %d = %d\n", n, sys_fact(9));
return 0;
}
我的系统是 64 位 ubuntu 14.04,我已经根据我的系统遵循了上述教程。
此外,我在安装内核时结合了以下命令,我认为这就是它在安装内核时没有报错的原因。
make && make modules_install && make install
内核安装花了 2-3 个小时,我现在很沮丧。 请帮忙!!
我对 syscall_64.tbl 所做的编辑(最后四个条目)。
320 common kexec_file_load sys_kexec_file_load
321 common bpf sys_bpf
322 64 execveat stub_execveat
323 common fact sys_fact
最佳答案
原因是虽然您可能正在使用新内核运行,但#include header 和 C 库仍然很旧,因此它们对您新添加的系统调用一无所知。所以你不能指望定义 sys_fact。
根据@SantoshA 的建议,网站 http://linuxseekernel.blogspot.in/2014/07/adding-system-call-in-x86-qemu.html建议使用带有系统调用号的 syscall() 函数。
关于c - 使用自定义系统调用时出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137244/