2023-2024 20233809《Linux内核原理与分析》第八周作业

20233809黄雅琦 2023-11-10 22:47:45

一、通过chatgpt学习相关知识
https://chat.openai.com/c/59e97352-da20-4ba0-8ebf-275589ecc453
二、实验楼实验七
1、理解编译链接的过程和 ELF 可执行文件格式
1)首先编写hello.c,并生成编译链接
#include<stdio.h>
int main(){
printf("Hello World!\n");
return 0;
}
编译链接命令
gcc -E hello.c -o hello.i//预处理
gcc -S hello.i -o hello.s//编译
gcc -c hello.s -o hello.o//汇编
gcc hello.o -o hello//链接
“gcc -o hello.static hello.o -static”静态编译出来的hello.static把C库里需要的东西也放到可执行文件里了。使用命令ls –l后,可以看到hello只有8511,hello.static有大概有877147。
编译过程图展示:

img


2)、输入命令readelf -h hello查看hello文件的头

img


2、编程使用 exec库函数加载一个可执行文件,动态链接分为可执行程序装载时动态链接和运行时动态链接。
1)可执行程序装载时动态链接
#ifndef SH_LIB_EXAMPLE_H
#define SH_LIB_EXAMPLE_H
#define success 0
#define failure(-1)
#ifdef __cplusplus
extern "C"{
#endif
int SharedLibApi();
#ifdef __cplusplus
}
#endif
#endif /
SH_LIB_EXAMPLE_H */
2)生成shlibexample.c文件
#include<stdio.h>
#include"shlibexample.h"

int SharedLibApi()
{
printf("This is a shared libary!\n");
return success;
}
3)输入gcc -shared shlibexample.c -o libshlibexample.so -m32”(在64位环境下执行时加上-32)生成.so文件生成文件

img


4)编写dllibexample.h文件
#ifndef DL_LIB_EXAMPLE_H
#define DL_LIB_EXAMPLE_H

#ifdef __cplusplus
extern "C" {
#endif

int DynamicalLoadingLibApi();

#ifndef __cplusplus
}
#endif
#endif /* DL_LIB_EXAMPLE_H */
5)编写dllibexample.c文件
#include<stdio.h>
#include"dllibexample.h"
#define success0
#define failure (-1)

int DynamicalLoadingLibApi()
{
printf("This is a Dynamical Loading libary!\n");
return success;
}
6)使用命令gcc -shared dllibexample.c -o libdllibexample.so生成动态库

img

7)接着建立动态链接,首先编写主函数test5.c
#include <stdio.h>
#include "shlibexample.h"
#include <dlfcn.h>

int main()
{
printf("This is a Main program!\n");
printf("Calling SharedLibApi() function of libshlibexample.so!\n");
SharedLibApi();
void * handle = dlopen("libdllibexample.so",RTLD_NOW);
if(handle == NULL)
{
printf("Open Lib libdllibexample.so Error:%s\n",dlerror());
return failure;
}
int (*func)(void);
char * error;
func = dlsym(handle,"DynamicalLoadingLibApi");
if((error = dlerror()) != NULL)
{
printf("DynamicalLoadingLibApi not found:%s\n",error);
return failure;
}
printf("Calling DynamicalLoadingLibApi() function of libdllibexample.so!\n");
func();
dlclose(handle);
return success;
}
8)
输入gcc main.c -o main -L/home/shiyanlou -lshlibexample -ldl -m32和export LD_LIBRARY_PATH=$(pwd)以及./test5.c

img


3、使用 gdb 跟踪分析一个 execve 系统调用内核处理函数 sys_execve
1)Linux提供了exec、execlp、execle、execv、execvp、execve等六个用以执行一个可执行文件的函数,统称为exec函数,差异在于对命令行参数和环境变量参数的传递方式不同。以上函数的本质都是调用系统调用sys_execve()来执行一个可执行文件。
整体的调用关系从前到后为:
sys_execve、do_execve、do_execve_common、exec_binprm、search_binary_handler、load_elf_binary、start_thread
可执行程序执行到execve的时候陷入到内核态,当系统调用从内核态返回到用户态时,eip直接跳转到ELF程序的入口地址,CPU也得到新的用户态堆栈(包含新程序的命令行参数和shell上下文环境)。这样,新程序就开始执行了。
当执行execve时,用execve的加载的可执行文件把当前进程的可执行程序给覆盖掉,execve的系统调用会返回新的可执行程序的起点——main函数。

img


2)接着使用gdb进行跟踪分析

img


img


4、实验总结
本次实验围绕着编译链接的过程和 ELF 可执行文件格式、动态链接以及系统调用,学习了相关知识以及简单的代码编译。

...全文
81 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

39

社区成员

发帖
与我相关
我的任务
社区描述
北京电子科技学院 《Linux内核原理与分析》课程
linuxarm开发系统安全 高校 北京·丰台区
社区管理员
  • rocflytosky
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧