OSDN Git Service

llvm-readobj: Fix addend in relocations for android packed format
authorPeter Collingbourne <peter@pcc.me.uk>
Wed, 15 Aug 2018 17:58:22 +0000 (17:58 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Wed, 15 Aug 2018 17:58:22 +0000 (17:58 +0000)
If a relocation group doesn't have the RELOCATION_GROUP_HAS_ADDEND_FLAG set, then this implies the group's addend equals zero.
In this case android packed format won't encode an explicit addend delta, instead we need to set Addend, the "previous addend" variable, to zero by ourself.

Patch by Yi-Yo Chiang!

Differential Revision: https://reviews.llvm.org/D50601

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339799 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/ELF.cpp
test/tools/llvm-readobj/elf-packed-relocs.test

index 2eefb7e..da56d97 100644 (file)
@@ -393,20 +393,17 @@ ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
     if (GroupedByAddend && GroupHasAddend)
       Addend += ReadSLEB();
 
+    if (!GroupHasAddend)
+      Addend = 0;
+
     for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
       Elf_Rela R;
       Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
       R.r_offset = Offset;
       R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
-
-      if (GroupHasAddend) {
-        if (!GroupedByAddend)
-          Addend += ReadSLEB();
-        R.r_addend = Addend;
-      } else {
-        R.r_addend = 0;
-      }
-
+      if (GroupHasAddend && !GroupedByAddend)
+        Addend += ReadSLEB();
+      R.r_addend = Addend;
       Relocs.push_back(R);
 
       if (ErrStr)
index 4a59daf..84acd06 100644 (file)
@@ -94,3 +94,45 @@ Symbols:
     - Name:            sym1
     - Name:            sym2
 ...
+
+# RUN: yaml2obj -docnum 3 %s | llvm-readobj -elf-output-style=LLVM -relocations - | FileCheck --check-prefix=LLVM3 %s
+#
+# LLVM3:      Section (1) .rela.dyn {
+# LLVM3-NEXT:   0x1100 R_X86_64_RELATIVE - 0x0
+# LLVM3-NEXT:   0x1180 R_X86_64_RELATIVE - 0x8
+# LLVM3-NEXT:   0x1200 R_X86_64_64 sym1 0x0
+# LLVM3-NEXT:   0x1208 R_X86_64_64 sym2 0x0
+# LLVM3-NEXT:   0x1210 R_X86_64_64 sym1 0x0
+# LLVM3-NEXT:   0x1218 R_X86_64_64 sym2 0x8
+# LLVM3-NEXT: }
+
+# RUN: yaml2obj -docnum 3 %s | llvm-readobj -elf-output-style=GNU -relocations - | FileCheck --check-prefix=GNU3 %s
+# GNU3:      Relocation section '.rela.dyn' at offset 0x180 contains 6 entries:
+# GNU3: 0000000000001100  0000000000000008 R_X86_64_RELATIVE                 0
+# GNU3-NEXT: 0000000000001180  0000000000000008 R_X86_64_RELATIVE                 8
+# GNU3-NEXT: 0000000000001200  0000000100000001 R_X86_64_64            0000000000000000 sym1 + 0
+# GNU3-NEXT: 0000000000001208  0000000200000001 R_X86_64_64            0000000000000000 sym2 + 0
+# GNU3-NEXT: 0000000000001210  0000000100000001 R_X86_64_64            0000000000000000 sym1 + 0
+# GNU3-NEXT: 0000000000001218  0000000200000001 R_X86_64_64            0000000000000000 sym2 + 8
+
+# elf-packed-relocs3.s
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_DYN
+  Machine:         EM_X86_64
+  Entry:           0x0000000000001000
+Sections:
+  - Name:            .rela.dyn
+    Type:            SHT_ANDROID_RELA
+    Flags:           [ SHF_ALLOC ]
+    Address:         0x00000000000001C8
+    Link:            .symtab
+    AddressAlign:    0x0000000000000001
+    Content:         415053320680200208800208008001080802008001818080801008818080802002080881808080100008818080802008
+Symbols:
+  Global:
+    - Name:            sym1
+    - Name:            sym2
+...