【问题标题】:How to print entire ELF file header with all its information? [duplicate]如何打印整个 ELF 文件头及其所有信息? [复制]
【发布时间】:2021-03-12 21:38:27
【问题描述】:

我必须打印 ELF 文件头。它包含的所有数据,就像运行 readelf -h hello.bin 时一样。该程序是用 C 语言编写的。这是我到目前为止所拥有的:

  typedef struct elf64_hdr {
  unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
  Elf64_Half e_type;
  Elf64_Half e_machine;
  Elf64_Word e_version;
  Elf64_Addr e_entry;       /* Entry point virtual address */
  Elf64_Off e_phoff;        /* Program header table file offset */
  Elf64_Off e_shoff;        /* Section header table file offset */
  Elf64_Word e_flags;
  Elf64_Half e_ehsize;
  Elf64_Half e_phentsize;
  Elf64_Half e_phnum;
  Elf64_Half e_shentsize;
  Elf64_Half e_shnum;
  Elf64_Half e_shstrndx;
} Elf64_Ehdr;

typedef struct elf64_shdr {
  Elf64_Word sh_name;       /* Section name, index in string tbl */
  Elf64_Word sh_type;       /* Type of section */
  Elf64_Xword sh_flags;     /* Miscellaneous section attributes */
  Elf64_Addr sh_addr;       /* Section virtual addr at execution */
  Elf64_Off sh_offset;      /* Section file offset */
  Elf64_Xword sh_size;      /* Size of section in bytes */
  Elf64_Word sh_link;       /* Index of another section */
  Elf64_Word sh_info;       /* Additional section information */
  Elf64_Xword sh_addralign; /* Section alignment */
  Elf64_Xword sh_entsize;   /* Entry size if section holds table */
} Elf64_Shdr;

这些是结构。 以下是 main 中声明的变量:

  FILE* ElfFile = NULL;
  char* SectNames = NULL;
  Elf64_Ehdr elfHdr;
  Elf64_Shdr sectHdr;
  uint32_t idx;

这是打印代码的相关部分,需要您的帮助:

// read ELF header, first thing in the file
  fread(&elfHdr, 1, sizeof(Elf64_Ehdr), ElfFile); 
  SectNames = malloc(sectHdr.sh_size); //variable for section names (like "Magic", "Data" etc.)
  fseek(ElfFile, sectHdr.sh_offset, SEEK_SET); //going to the offset of the section
  fread(SectNames, 1, sectHdr.sh_size, ElfFile); //reading the size of section
  for(int i=0; i<sectHdr.sh_size; i++)
  {
    char *name1 = "";
    fseek(ElfFile, elfHdr.e_shoff + i*sizeof(sectHdr), SEEK_SET);
    fread(&sectHdr, 1, sizeof(sectHdr), ElfFile);
    name1 = SectNames + sectHdr.sh_name;
    printf("%s \n", name1);
  }

代码编译但不打印任何内容。我希望打印诸如“Magic”、“Data”、“Class”等字符串......

【问题讨论】:

  • SectNames = malloc(sectHdr.sh_size); 读取sectHdr 的代码此时尚未执行。如果这不是问题,请提供完整的代码minimal verifiable example
  • 虽然您当然推出了自己的版本,但有一个 ELF 库可以完成您想做的大部分工作:libelf。您可以从您的发行版安装开发包。 (例如)在 Fedora 中,它是 elfutils-libelf-devel

标签: c linux binaryfiles elf


【解决方案1】:

您不能使用+ 运算符在 C 中添加字符串。

这一行:

char *name1 = "";

表示name1 指向一个空的常量字符串。
它不会为您可以添加字符串的某些字符串对象分配内存。

所以这段代码:

name1 = SectNames + sectHdr.sh_name;

只需在name1 中放入一个垃圾值(某个数字),这将导致未定义的行为(您的程序可能会崩溃,或不打印,或打印随机字符)。

要打印部分名称,您需要如下代码:

printf("Section name: %u\n", sectHdr.sh_name);

这会将sectHdr.sh_name 中的值格式化为文本并将其输出到屏幕。

您将不得不对所有其他字段做类似的事情,一个接一个,但您必须注意字段类型并使用正确的格式说明符。

%u 表示“无符号整数”,它与 Elf64_Word 最接近,printf 具有。
仔细检查其他类型和suitable specifiers

【讨论】:

  • 我修改了这个但仍然没有警告也没有打印..
  • @Anew希望您不要检查fread 操作的结果或您在sectHdr.sh_size 中获得的值。您的代码可能在其他地方失败并且永远不会进入循环。您应该首先打印sectHdr.sh_size 进行调试,并检查每个fread 的返回值,看看它是否真的读取了您预期的字节数!
  • @AnewHope 如果您想看到正确的警告,请确保使用 -Wall -pedantic 和 -std=gnu99 进行编译(我认为这是使用 Linux 所需的标准)。
猜你喜欢
  • 2015-10-25
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 2014-12-21
  • 2023-02-09
相关资源
最近更新 更多