[llvm] r312192 - [yaml2obj][ELF] Make symbols optional for relocations

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 30 16:13:31 PDT 2017


Author: phosek
Date: Wed Aug 30 16:13:31 2017
New Revision: 312192

URL: http://llvm.org/viewvc/llvm-project?rev=312192&view=rev
Log:
[yaml2obj][ELF] Make symbols optional for relocations

Some kinds of relocations do not have symbols, like R_X86_64_RELATIVE
for instance. I would like to test this case in D36554 but currently
can't because symbols are required by yaml2obj. The other option is
using the empty symbol but that doesn't seem quite right to me.

This change makes the Symbol field of Relocation optional and in the
case where the user does not specify a symbol name the Symbol index is 0.

Patch by Jake Ehrlich

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

Added:
    llvm/trunk/test/tools/yaml2obj/invalid-symboless-relocation.yaml
    llvm/trunk/test/tools/yaml2obj/symboless-relocation.yaml
Modified:
    llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h
    llvm/trunk/lib/ObjectYAML/ELFYAML.cpp
    llvm/trunk/tools/yaml2obj/yaml2elf.cpp

Modified: llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h?rev=312192&r1=312191&r2=312192&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h (original)
+++ llvm/trunk/include/llvm/ObjectYAML/ELFYAML.h Wed Aug 30 16:13:31 2017
@@ -161,7 +161,7 @@ struct Relocation {
   llvm::yaml::Hex64 Offset;
   int64_t Addend;
   ELF_REL Type;
-  StringRef Symbol;
+  Optional<StringRef> Symbol;
 };
 
 struct RelocationSection : Section {

Modified: llvm/trunk/lib/ObjectYAML/ELFYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ObjectYAML/ELFYAML.cpp?rev=312192&r1=312191&r2=312192&view=diff
==============================================================================
--- llvm/trunk/lib/ObjectYAML/ELFYAML.cpp (original)
+++ llvm/trunk/lib/ObjectYAML/ELFYAML.cpp Wed Aug 30 16:13:31 2017
@@ -853,7 +853,7 @@ void MappingTraits<ELFYAML::Relocation>:
   assert(Object && "The IO context is not initialized");
 
   IO.mapRequired("Offset", Rel.Offset);
-  IO.mapRequired("Symbol", Rel.Symbol);
+  IO.mapOptional("Symbol", Rel.Symbol);
 
   if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
       Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {

Added: llvm/trunk/test/tools/yaml2obj/invalid-symboless-relocation.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/yaml2obj/invalid-symboless-relocation.yaml?rev=312192&view=auto
==============================================================================
--- llvm/trunk/test/tools/yaml2obj/invalid-symboless-relocation.yaml (added)
+++ llvm/trunk/test/tools/yaml2obj/invalid-symboless-relocation.yaml Wed Aug 30 16:13:31 2017
@@ -0,0 +1,29 @@
+# This test succeeds but produces an invalid relocation. This test
+# documents this behavoir.
+# RUN: yaml2obj %s > %t
+# RUN: llvm-readobj -relocations %t | FileCheck %s
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Content:         "00000000"
+  - Name:            .rel.text
+    Type:            SHT_REL
+    Link:            .symtab
+    Info:            .text
+    Relocations:
+      - Offset: 0x1000
+        Type:   R_X86_64_PC32
+
+#CHECK:     Relocations [
+#CHECK-NEXT:  Section (2) .rel.text {
+#CHECK-NEXT:    0x1000 R_X86_64_PC32 - 0x0
+#CHECK-NEXT:  }
+#CHECK-NEXT:]

Added: llvm/trunk/test/tools/yaml2obj/symboless-relocation.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/yaml2obj/symboless-relocation.yaml?rev=312192&view=auto
==============================================================================
--- llvm/trunk/test/tools/yaml2obj/symboless-relocation.yaml (added)
+++ llvm/trunk/test/tools/yaml2obj/symboless-relocation.yaml Wed Aug 30 16:13:31 2017
@@ -0,0 +1,21 @@
+# Just make sure this isn't an error even though it has no Symbol
+# RUN: yaml2obj %s
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Content:         "00000000"
+  - Name:            .rel.text
+    Type:            SHT_REL
+    Link:            .symtab
+    Info:            .text
+    Relocations:
+      - Offset: 0x1000
+        Type:   R_X86_64_RELATIVE

Modified: llvm/trunk/tools/yaml2obj/yaml2elf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2elf.cpp?rev=312192&r1=312191&r2=312192&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2elf.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp Wed Aug 30 16:13:31 2017
@@ -459,7 +459,8 @@ ELFState<ELFT>::writeSectionContent(Elf_
     // Some special relocation, R_ARM_v4BX for instance, does not have
     // an external reference.  So it ignores the return value of lookup()
     // here.
-    SymN2I.lookup(Rel.Symbol, SymIdx);
+    if (Rel.Symbol)
+      SymN2I.lookup(*Rel.Symbol, SymIdx);
 
     if (IsRela) {
       Elf_Rela REntry;




More information about the llvm-commits mailing list