【问题标题】:print the sections of elf打印精灵的部分
【发布时间】:2012-06-18 05:29:10
【问题描述】:

我需要编写函数 write_file: 此函数 write_file(int fdOut, char *start1, char *start2) 接收输出文件的文件描述符和两个指向映射到内存的 ELF 文件结构的指针。 遍历节头表中的所有节头,将相关字段(节名、偏移量和长度)打印到标准输出(用于调试),并将相应的节内容写入输出文件。

我如何“将相应的部分内容写入输出文件”? 它总是复制 0 个字节...

这是我写的:void write_file(int fdOut, char *start_a, char *start_b) {

 char *start1=start_a;
 Elf32_Ehdr *header;
 Elf32_Shdr *curr;
 char *curr_name;
 int i;
 int c;


write(fdOut , start1 , 52);
start1=start_a;

header = (Elf32_Ehdr *)start1;

for(i=0 ; i< header->e_shnum ; i++){
  start1=start_a;
 curr= get_shdr_from_index(i, start1); /*returns a pointer to the requested section header**/
 curr_name=get_section_name(i, start1); /*returns a pointer into the section header string table.**/

 printf("[ %d ] %20s %20x %20x\n", i ,
           curr_name , curr->sh_offset , curr->sh_size);


 if (c=write(fdOut, curr, curr->sh_size)<0) {
  perror("error");
  exit(-1);
}
printf("%d" , c);

}

}

【问题讨论】:

    标签: elf


    【解决方案1】:

    您的第一个错误在这里:

    if (c=write(fdOut, curr, curr->sh_size)<0) { ...
    

    阅读运算符优先级here。你想写什么:

    if ((c = write(fdOut, curr, curr->sh_size)) < 0) { ...
    

    您的第二个错误是要求您将部分内容写入 fd。但是您正在编写 header 部分,而不是内容。内容可在原始文件中找到,偏移量为curr-&gt;sh_offset。该数据可能根本不存在于内存中,因此您可能需要将原始文件中的lseekread 那里的数据,然后才能write 将数据输出到输出。

    【讨论】:

      【解决方案2】:

      使用-O binary 输出格式:

      objcopy -O binary --only-section=.text foobar.elf foobar.text
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多