-h显示文件头应该是最简单的一个参数,我想没有之一了,只有下面这一点点的输出:
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x580
Start of program headers: 64 (bytes into file)
Start of section headers: 6488 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 29
Section header string table index: 28
程序的执行过程是这样的:
main
process_file
process_object
get_file_header——拷贝内容
process_file_header——显示输出
get_file_header是最重要的一个函数,因为里面是先拷贝Ehdr的内容,然后再通过get_XXbit_section_headers接口拷贝所有section的内容,如下:
拷贝Ehdr:
filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
拷贝sections:(-h选项并没有显示这部分内容)
for (i = 0, internal = filedata->section_headers;
i < num;
i++, internal++)
{
internal->sh_name = BYTE_GET (shdrs[i].sh_name);
internal->sh_type = BYTE_GET (shdrs[i].sh_type);
internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
internal->sh_size = BYTE_GET (shdrs[i].sh_size);
internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
internal->sh_link = BYTE_GET (shdrs[i].sh_link);
internal->sh_info = BYTE_GET (shdrs[i].sh_info);
internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
if (!probe && internal->sh_link > num)
warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
}
貌似没有什么可说的,虽然是简单的拷贝,但是比我写的6的太多。里面fread和fseek用的很好,可以看一下。附近的几个接口中都有,还有一个get_data接口,真的是整个文件内容烂熟于心,全部都是指针偏移,这才是我们C语言应该有的样子!就像武侠中蒙眼下棋的高手。process_file_header中的printf也很6,不过不是重点,没有细致研究,估计忘不了,后面有兴趣了会看一下为什么那么一种输出流程。这种级别的代码中真的是隐藏了很多奇特的写法。哈哈哈,就这样吧。