C/C++知识点之编译 linux 内核及添加系统调用
小标 2019-03-14 来源 : 阅读 993 评论 0

摘要:本文主要向大家介绍了C/C++知识点之编译 linux 内核及添加系统调用,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之编译 linux 内核及添加系统调用,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

C/C++知识点之编译 linux 内核及添加系统调用

后面编译的是 4.2.2 与 4.14.14
在 gcc4.8 上编译4.14.14 时报不支持堆栈保护 反正都差不多就先编译了 4.2.2 再编译4.14.14
1.下载 linux 内核源码
2.解压放到 /usr/src
3.创建软连接  (不一定非得把文件放到 /usr/src  看个人我直接放桌面)


root@ubuntu:~# cd /usr/src
root@ubuntu:/usr/src# ls
linux-4.14.14           linux-headers-4.2.0-27-generic
linux-headers-4.2.0-27
root@ubuntu:/usr/src# ln -s linux-4.14.14 linux
root@ubuntu:/usr/src#


4.安装工具(这些可以不要安装)


root@ubuntu:/usr/src# sudo apt-get install build-essential kernel-package libncurses5-dev fakeroot
sudo aptitude install libqt3-headers libqt3-mt-dev libqt3-compat-headers libqt3-mt


(如果找不到命令,请先sudo apt-get install aptitude)
5.内核修改


# cd /usr/include/ 
# rm -r asm linux scsi
# ln -s /usr/src/linux/include/asm-generic asm
# ln -s /usr/src/linux/include/linux linux
# ln -s /usr/src/linux/include/scsi scsi



root@ubuntu:/usr/src# cd /usr/include/
root@ubuntu:/usr/include# rm -r asm linux scsi
root@ubuntu:/usr/include# ln -s /usr/src/linux/include/asm-generic asm
root@ubuntu:/usr/include# ln -s /usr/src/linux/include/linux linux
root@ubuntu:/usr/include#  ln -s /usr/src/linux/include/scsi scsi
root@ubuntu:/usr/include#


6.添加新的系统调用

找到系统调用号的表(下面的linux 是一个刚才设置的软连接 )
注意版本不同文件地方不同 
linux-2.6.32.65  在文件:/usr/src/linux/arch/x86/kernel/syscall_table_32.S
linux-3.5.4 在文件:/usr/src/linux/arch/x86/syscalls/syscall_64.tbl
linux-4.14   在文件:/usr/src/linux/arch/x86/entry/syscalls/syscall_64.tbl
这里是 4.14.14

添加一个系统调用号 (548)


548     x32      hello                   sys_hello


声明系统调用服务例程
打开:include/linux/syscalls.h 也要注意版本 不同版本不同

添加


asmlinkage long sys_hello(const char __user *name);


定义系统调用服务例程


asmlinkage long sys_hello(const char __user *name)
{
    char *name_kd;
    long ret;
    name_kd = strndup_user(name, PAGE_SIZE);
    if (IS_ERR(name_kd))
    {
        ret = PTR_ERR(name);
        goto error;

    }
    printk("Hello, %s!\n", name_kd);
    ret = 0;
    error:
    return ret;

}


kernel/sys.c 只要不插函数 头包含中间 其它随便

开始编译内核:
cd /usr/src/linux


root@ubuntu:/usr/include# cd /usr/src/linux
root@ubuntu:/usr/src/linux# ls
arch     Documentation  ipc          Makefile  security
block    drivers        Kbuild       mm        sound
certs    firmware       Kconfig      net       tools
COPYING  fs             kernel       README    usr
CREDITS  include        lib          samples   virt
crypto   init           MAINTAINERS  scripts
root@ubuntu:/usr/src/linux#


输入 make mrproper
输入 make menuconfig (根据需要选择相关配置)可能报错 找不到linux/limits.h
直接安装apt-get install linux-libc-dev:amd64

这里由于直接在 4.2.0 上编译 4.14.14 会报 gcc 不支持 所以先编译的 4.2.2 后面在编译的 4.14.14 调用号在 4.14.14 与 4.2.2上 都可以测试的
make 编译
开始编译

编译完后


root@ubuntu:~/Desktop/linux-4.2.2# ls
arch     crypto         fs       Kbuild   MAINTAINERS      modules.order   REPORTING-BUGS  signing_key.priv  tools    vmlinux-gdb.py
block    Documentation  include  Kconfig  Makefile         Module.symvers  samples         signing_key.x509  usr      vmlinux.o
COPYING  drivers        init     kernel   mm               net             scripts         sound             virt     x509.genkey
CREDITS  firmware       ipc      lib      modules.builtin  README          security        System.map        vmlinux
root@ubuntu:~/Desktop/linux-4.2.2#


安装:
make modules_install install

可能报错:要安装
sudo apt install libssl-dev


scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^
compilation terminated.
scripts/extract-cert.c:21:25: fatal error: openssl/bio.h: No such file or directory
 #include <openssl/bio.h>
                         ^
compilation terminated.
make[1]: *** [scripts/extract-cert] Error 1
make[1]: *** Waiting for unfinished jobs....
  CC      arch/x86/kernel/asm-offsets.s
make[1]: *** [scripts/sign-file] Error 1
  HOSTLD  scripts/mod/modpost
make: *** [scripts] Error 2
make: *** Waiting for unfinished jobs....
  CHK     include/generated/asm-offsets.h
  UPD     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
root@ubuntu:~/Desktop/linux-4.14.14# sudo apt install libssl-dev


修改 grub 文件
gedit /etc/default/grub
注释掉:
#GRUB_HIDDEN_TIMEOUT=0

更新
sudo update-grub2


root@ubuntu:~/Desktop/linux-4.2.2# gedit /etc/default/grub
root@ubuntu:~/Desktop/linux-4.2.2# sudo update-grub2
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.2.2
Found initrd image: /boot/initrd.img-4.2.2
Found linux image: /boot/vmlinuz-4.2.0-27-generic
Found initrd image: /boot/initrd.img-4.2.0-27-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done
root@ubuntu:~/Desktop/linux-4.2.2#


更换内核:

以下为 4.14.14

调用:


#include <sys/syscall.h>
 #include <stdio.h>
 #include <unistd.h>
int main()
{
long ret = syscall(548, "lw");
printf("ret: %d\n", (int)ret);
return 0;
 }


root@ubuntu:~/Desktop# g++ allen.c -o test
root@ubuntu:~/Desktop# ./test lw
ret: 0
root@ubuntu:~/Desktop# dmesg


ation="profile_load" profile="unconfined" name="/usr/lib/telepathy/mission-control-5" pid=987 comm="apparmor_parser"
[   20.827182] audit: type=1400 audit(1536418840.947:23): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*" pid=987 comm="apparmor_parser"
[   20.829930] audit: type=1400 audit(1536418840.951:24): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*//pxgsettings" pid=987 comm="apparmor_parser"
[   20.832445] audit: type=1400 audit(1536418840.955:25): apparmor="STATUS" operation="profile_load" profile="unconfined" name="/usr/lib/telepathy/telepathy-*//sanitized_helper" pid=987 comm="apparmor_parser"
[   23.628220] init: plymouth-upstart-bridge main process ended, respawning
[   23.656900] init: plymouth-upstart-bridge main process (1266) terminated with status 1
[   23.656918] init: plymouth-upstart-bridge main process ended, respawning
[  421.177187] Hello, lw!
root@ubuntu:~/Desktop#

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程