Lab1:操作系统的内核,启动

[toc]

ELF——操作系统内核的本质

链接器通过哪些信息来链接多个目标文件呢?

在目标文件中,记录了代码各个段的具体信息。链接器通过这些信息来将目标文件链接到一起。

ELF是一种用于可重定位文件(如目标文件),可执行文件和共享对象文件的文件格式

  • 可执行文件:包含完整程序(所有跳转地址都已确认,可被操作系统直接加载到内存运行),有明确的入口地址。具有程序头表,操作系统据此将文件映射到内存。
  • 可重定位文件:包含一些尚未确定具体地址的符号,需要在链接过程中重定位。
  • 共享对象文件:代码和数据可被多个程序同时使用,从而减少内存使用。:共享对象文件在程序运行时才会被加载到内存中,而不是在链接时静态地合并到可执行文件中,这样可以省去重复编译。

OS_L1_G1

1743045373084

elf.h是一个头文件,在类Unix系统里,它定义了ELF文件格式的各种数据结构与常量。借助elf.h我们能够编写程序来读取,解析,操作ELF文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf32_Half e_type; /* Object file type */
Elf32_Half e_machine; /* Architecture */
Elf32_Word e_version; /* Object file version */
Elf32_Addr e_entry; /* Entry point virtual address */
Elf32_Off e_phoff; /* Program header table file offset */
Elf32_Off e_shoff; /* Section header table file offset */
Elf32_Word e_flags; /* Processor-specific flags */
Elf32_Half e_ehsize; /* ELF header size in bytes */
Elf32_Half e_phentsize; /* Program header table entry size */
Elf32_Half e_phnum; /* Program header table entry count */
Elf32_Half e_shentsize; /* Section header table entry size */
Elf32_Half e_shnum; /* Section header table entry count */
Elf32_Half e_shstrndx; /* Section header string table index */
} Elf32_Ehdr;
1
2
3
4
5
6
7
8
typedef struct {
...
Elf32_Word sh_name; /* Section name */
Elf32_Addr sh_addr; /* The address of the section in memory */
Elf32_Off sh_offset; /* The offset relative to the start position of the file */
Elf32_Word sh_size; /* Section size */
...
} Elf32_Shdr;
1
2
3
4
5
6
7
8
9
typedef struct {
...
Elf32_Off p_offset; /* Segment file offset */
Elf32_Addr p_vaddr; /* The virtual address of the segment in memory */
Elf32_Addr p_paddr; /* The physical address of the segment in memory */
Elf32_Word p_filesz; /* Segment size in file */
Elf32_Word p_memsz; /* Segment size in memory */
...
} Elf32_Phdr;