readelf: print addend for RELA relocations without symbol (#292)
authorAndreas Ziegler <andreas.ziegler@fau.de>
Sat, 7 Mar 2020 14:32:40 +0000 (15:32 +0100)
committerGitHub <noreply@github.com>
Sat, 7 Mar 2020 14:32:40 +0000 (06:32 -0800)
* 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.

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

index 6db36dfd0da7520fc47c69c01b1e03c3dbe3cc21..5c5111c37e81d98e21aae83d98a65610c5d4e43b 100755 (executable)
@@ -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 (file)
index 0000000..781bf99
--- /dev/null
@@ -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 (executable)
index 0000000..ef77538
Binary files /dev/null and b/test/testfiles_for_readelf/tls64.elf differ