readelf.py: adapt section mapping output for .tbss sections (#289)
authorAndreas Ziegler <andreas.ziegler@fau.de>
Sat, 7 Mar 2020 14:05:42 +0000 (15:05 +0100)
committerGitHub <noreply@github.com>
Sat, 7 Mar 2020 14:05:42 +0000 (06:05 -0800)
* readelf.py: adapt section mapping output for .tbss sections

GNU readelf does not show the .tbss section as part of the
loaded data segment when listing the section to segment
mappings, using the ELF_TBSS_SPECIAL macro in
include/elf/internal.h to skip printing the section name.

Implement the same logic in readelf.py.

* test: add test file for .tbss output in readelf.py

This test file includes a .tbss section which is not output
by GNU readelf as part of the loaded data segment when
listing the section to segment mappings.

The source code for tls.elf is simply:

  __thread int i;

  int main(){}

The file was compiled using the following command line:

$ gcc -o tls.elf -m32 tls.c

scripts/readelf.py
test/testfiles_for_readelf/tls.elf [new file with mode: 0755]

index a7b5ab998c773b55b53e853344087a87879ff4d9..6db36dfd0da7520fc47c69c01b1e03c3dbe3cc21 100755 (executable)
@@ -49,6 +49,7 @@ from elftools.elf.descriptions import (
     )
 from elftools.elf.constants import E_FLAGS
 from elftools.elf.constants import E_FLAGS_MASKS
+from elftools.elf.constants import SH_FLAGS
 from elftools.dwarf.dwarfinfo import DWARFInfo
 from elftools.dwarf.descriptions import (
     describe_reg_name, describe_attr_value, set_global_machine_arch,
@@ -277,6 +278,9 @@ class ReadElf(object):
 
             for section in self.elffile.iter_sections():
                 if (    not section.is_null() and
+                        not ((section['sh_flags'] & SH_FLAGS.SHF_TLS) != 0 and
+                             section['sh_type'] == 'SHT_NOBITS' and
+                             segment['p_type'] != 'PT_TLS') and
                         segment.section_in_segment(section)):
                     self._emit('%s ' % section.name)
 
diff --git a/test/testfiles_for_readelf/tls.elf b/test/testfiles_for_readelf/tls.elf
new file mode 100755 (executable)
index 0000000..8c9ce8f
Binary files /dev/null and b/test/testfiles_for_readelf/tls.elf differ