简体   繁体   English

获取ELF文件信息

[英]getting to an ELF file information

ok... so im suppose to write a program that prints all of the sections name in an elf file using only mmap (thats not important...) 好的...所以我想编写一个仅使用mmap在elf文件中打印所有节名称的程序(那并不重要...)

so what i did so far is this - maped the file into the stat structure = map_start = mmap(0, fd_stat.st_size, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0)) <0 ) 所以我到目前为止所做的是-将文件映射到stat结构= map_start = mmap(0,fd_stat.st_size,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0))<0)

casted it into the write format from the starting point i got = header = (Elf32_Ehdr *) map_start; 从我得到的起始点将其转换为写入格式= header =(Elf32_Ehdr *)map_start;

gotten the section header offset from the file = secoff = header->e_shoff; 从文件中获取节头的偏移量= secoff = header-> e_shoff;

now - i know i need to go to the map_start+secoff location - that will give me the section table, and the sh_name will give me an index for the string table... 现在-我知道我需要去map_start + secoff位置-这将为我提供节表,而sh_name将为我提供字符串表的索引...

how to i go to the sting table? 我如何去the桌? how is it represented? 它是如何表示的? how do i use it? 我该如何使用? and is the value in sh_name the index in the string table (if it is represented as an array) , or an offset.. 并且是sh_name中的值,它是字符串表中的索引(如果它表示为数组),或者是一个偏移量。

anyway - lets say i want to print the first two section's name - how do i do it givven the code i wrote above help please? 无论如何-假设我要打印前两个部分的名称-请给我上面帮助中写的代码该怎么做?

header = (Elf32_Ehdr *) map_start;
secoff = header->e_shoff;

This is probably wrong. 这可能是错误的。 Unless the Elf32_Ehdr structure is explicitly declared __attribute__((packed)) , the compiler will eventually insert padding between the members of the structure, so sizeof(Elf32_Ehdr) != (the actual size of an ELF header section) . 除非明确声明__attribute__((packed))声明Elf32_Ehdr结构,否则编译器最终将在该结构的成员之间插入填充,因此sizeof(Elf32_Ehdr) != (the actual size of an ELF header section) Why not simply use the libelf accessor functions instead? 为什么不简单地使用libelf访问器函数呢?

Update: if you're not allowed to use accessor functions, you'll have to do something like this: 更新:如果不允许使用访问器函数,则必须执行以下操作:

Elf32_Ehdr hdr;
memcpy(&hdr.e_ident, map_start + 0, EI_NIDENT);
memcpy(&hdr.e.type, map_start + 0 + sizeof(Elf32_half), sizeof(Elf32_Half));

et cetera. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM