2019-2020-1 20209319《Linux内核原理与分析》第八周作业

《Linux内核原理与分析》第八周作业

作业信息

这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)>
这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第八周作业>
这个作业的目标
作业正文 ... 本博客链接

实验

首先对环境进行配置:

cd LinuxKernel
rm menu -rf
git clone https://github.com/mengning/menu.git
mv test_exec.c test.c
make rootfs

如图:

可以看到,在qemu中输入exec可以看到如下显示:

以调试模式启动:

qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -S -s

打开gdb,对gdb进行如下配置:

file linux-3.18.6/vmlinux
target remote:1234
b sys_execve
b load_elf_binary

一直按c继续,可以看到:

elf文件格式[这里是参考了百度]链接https://baike.baidu.com/item/ELF/7120560

格式代码如下:

#define EI_NIDENT 16
  typedef struct{
  unsigned char e_ident[EI_NIDENT];
  Elf32_Half e_type;
  Elf32_Half e_machine;
  Elf32_Word e_version;
  Elf32_Addr e_entry;
  Elf32_Off e_phoff;
  Elf32_Off e_shoff;
  Elf32_Word e_flags;
  Elf32_Half e_ehsize;
  Elf32_Half e_phentsize;
  Elf32_Half e_phnum;
  Elf32_Half e_shentsize;
  Elf32_Half e_shnum;
  Elf32_Half e_shstrndx;
  }Elf32_Ehdr;

数据类型说明:
名称 大小 对齐 用途
Elf32_Addr 4 4 无符号程序地址
Elf32_Half 2 2 无符号中等大小整数
Elf32_Off 4 4 无符号文件偏移
Elf32_Sword 4 4 有符号大整数
Elf32_Word 4 4 无符号大整数
unsigned char 1 1 无符号小整数
最开头是16个字节的e_ident, 其中包含用以表示ELF文件的字符,以及其他一些与机器无关的信息。开头的4个字节值固定不变,为0x7f和ELF三个字符。

e_type 它标识的是该文件的类型。
e_machine 表明运行该程序需要的体系结构。
e_version 表示文件的版本。
e_entry 程序的入口地址。
e_phoff 表示Program header table 在文件中的偏移量(以字节计数)。
e_shoff 表示Section header table 在文件中的偏移量(以字节计数)。
e_flags 对IA32而言,此项为0。
e_ehsize 表示ELF header大小(以字节计数)。
e_phentsize 表示Program header table中每一个条目的大小。
e_phnum 表示Program header table中有多少个条目。
e_shentsize 表示Section header table中的每一个条目的大小。 
e_shnum 表示Section header table中有多少个条目。 
e_shstrndx 包含节名称的字符串是第几个节(从零开始计数)

原文地址:https://www.cnblogs.com/ring3toring0/p/14053873.html