elf: support for ELF files with a large number of sections (#333)
* elf: implement support for ELF files with a large number of sections
As documented in the ELF specification [0] and reported in #330,
the number of sections (`e_shnum` member of the ELF header)
as well as the section table index of the section name string
table (`e_shstrndx` member) could exceed the SHN_LORESERVE
(0xff00) value. In this case, the members of the ELF header
are set to 0 or SHN_XINDEX (0xffff), respectively, and the
actual values are found in the inital entry of the section
header table (which is otherwise set to zeroes).
So far, the implementation of `elffile.num_sections()`
didn't handle these situations and simply reported that the
file contained 0 sections, and `scripts/readelf.py` presented
invalid values.
Fix it by following the specification more closely and
showing the corresponding correct values in `readelf.py`.
[0]: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
Closes: #330
* test: add test file with a large number of sections
This file was generated with the following commands:
$ for i in {1..65280}; do
echo "void __attribute__((section(\"s.$i\"), naked)) f$i(void) {}";
done > many_sections.c;
echo "int main(){}" >> many_sections.c
$ gcc-8 -fno-asynchronous-unwind-tables -c -o many_sections.o.elf many_sections.c
$ strip many_sections.o.elf