c - 使用自定义系统调用时出现 undefined reference 错误

标签 c kernel system-calls

我正在尝试在内核 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/

相关文章:

c - 如何查看 #include 包含的 header 的位置?

c - C 中错误的双重计算?

c - 遵循简单的内核教程时出现奇怪的链接器错误 gcc

c - 格式 ‘%d’ 需要类型为 ‘int’ 的参数,但参数 3 的类型为 ‘int *’

c - 如何实现 `.nf`格式化功能

c - ELF 格式操作

c - 操作系统是用什么C写的?

python - 将子进程输出从 Python 脚本附加到文件

linux - CentOS 7.2 上的 Dirty CoW 缓解措施 - 语义错误 : while resolving probe point

linux - 为什么系统调用部分没有 `execlp()`