为什么“readelf”工具输出两个ELF文件节数?

问题描述

我目前正在调查我们的工具读取和解释 ELF 文件的问题,即读取特定 elf 文件时出现问题。

带有选项“-h”的unix工具“readelf”为参数“节头数”输出2个值。在最初的数字之后还有一个

工作文件示例:

Number of section headers: 1234

不起作用的文件示例:

Number of section headers: 0 (4524)

括号里的数字是什么意思?我想这可能是我的问题的原因,因为我们的应用程序认为有问题的文件中没有任何部分。

console screen

解决方法

Here is the relevant code in the readelf source.。看起来如果 header->e_shnum 的值是 SHN_UNDEF (zero),那么括号中打印的是 filedata->section_headers[0].sh_size 的值。

这对应于 elf(5) man page 中的以下段落:

   e_shnum
          This member holds the number of entries in the section
          header table.  Thus the product of e_shentsize and e_shnum
          gives the section header table's size in bytes.  If a file
          has no section header table,e_shnum holds the value of
          zero.

          If the number of entries in the section header table is
          larger than or equal to SHN_LORESERVE (0xff00),e_shnum
          holds the value zero and the real number of entries in the
          section header table is held in the sh_size member of the
          initial entry in section header table.  Otherwise,the
          sh_size member of the initial entry in the section header
          table holds the value zero.

因此,这将是指定节头表具有 74496 个条目(确实大于 0xff00)的有效方法。您似乎需要修复您的工具来处理这种情况。