From: Andreas Ziegler Date: Sat, 7 Mar 2020 14:32:40 +0000 (+0100) Subject: readelf: print addend for RELA relocations without symbol (#292) X-Git-Tag: v0.27~54 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6fff9b53f24478610f4bc2e6a6e5b4d75f686f8a;p=pyelftools.git readelf: print addend for RELA relocations without symbol (#292) * readelf: print addend for RELA relocations without symbol When processing relocations from a SHT_RELA type section, GNU readelf displays the value of the 'r_addend' field if no symbol index is given (that is, 'r_info_sym' is 0). By also implementing this we can better test the output for 64-bit binaries which commonly use SHT_RELA relocations. The included test file is the same as tls.elf but compiled for x86_64. Its code is the following: __thread int i; int main(){} and it is compiled using the following command line: $ gcc -m64 -o tls64.elf tls.c * test: add source file for tls{,64}.elf The comments at the top describe how to compile the source file into tls.elf and tls64.elf. --- diff --git a/scripts/readelf.py b/scripts/readelf.py index 6db36df..5c5111c 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -506,6 +506,10 @@ class ReadElf(object): rel['r_info_type'], self.elffile))) if rel['r_info_sym'] == 0: + if section.is_RELA(): + fieldsize = 8 if self.elffile.elfclass == 32 else 16 + addend = self._format_hex(rel['r_addend'], lead0x=False) + self._emit(' %s %s' % (' ' * fieldsize, addend)) self._emitline() continue diff --git a/test/testfiles_for_readelf/tls.c b/test/testfiles_for_readelf/tls.c new file mode 100644 index 0000000..781bf99 --- /dev/null +++ b/test/testfiles_for_readelf/tls.c @@ -0,0 +1,8 @@ +// Compile into tls.elf using: +// $ gcc -m32 -o tls.elf tls.c +// For tls64.elf, use: +// $ gcc -m64 -o tls64.elf tls.c + +__thread int i; + +int main(){} diff --git a/test/testfiles_for_readelf/tls64.elf b/test/testfiles_for_readelf/tls64.elf new file mode 100755 index 0000000..ef77538 Binary files /dev/null and b/test/testfiles_for_readelf/tls64.elf differ