summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Hosek <phosek@chromium.org>2017-08-30 23:13:31 +0000
committerPetr Hosek <phosek@chromium.org>2017-08-30 23:13:31 +0000
commit8231133535b1b2101126be6ed2d1bbb65c2a81be (patch)
tree64b0e2baf984100216fd164425abf44f15f6edba
parenta678116cfb8402da77486060b47f4a0de9623852 (diff)
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312192 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/ObjectYAML/ELFYAML.h2
-rw-r--r--lib/ObjectYAML/ELFYAML.cpp2
-rw-r--r--test/tools/yaml2obj/invalid-symboless-relocation.yaml29
-rw-r--r--test/tools/yaml2obj/symboless-relocation.yaml21
-rw-r--r--tools/yaml2obj/yaml2elf.cpp3
5 files changed, 54 insertions, 3 deletions
diff --git a/include/llvm/ObjectYAML/ELFYAML.h b/include/llvm/ObjectYAML/ELFYAML.h
index c4a1fe2ce39..317e982c26f 100644
--- a/include/llvm/ObjectYAML/ELFYAML.h
+++ b/include/llvm/ObjectYAML/ELFYAML.h
@@ -161,7 +161,7 @@ struct Relocation {
llvm::yaml::Hex64 Offset;
int64_t Addend;
ELF_REL Type;
- StringRef Symbol;
+ Optional<StringRef> Symbol;
};
struct RelocationSection : Section {
diff --git a/lib/ObjectYAML/ELFYAML.cpp b/lib/ObjectYAML/ELFYAML.cpp
index fd0aa49445f..cf0a943a880 100644
--- a/lib/ObjectYAML/ELFYAML.cpp
+++ b/lib/ObjectYAML/ELFYAML.cpp
@@ -853,7 +853,7 @@ void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
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)) {
diff --git a/test/tools/yaml2obj/invalid-symboless-relocation.yaml b/test/tools/yaml2obj/invalid-symboless-relocation.yaml
new file mode 100644
index 00000000000..538f64c9ccc
--- /dev/null
+++ b/test/tools/yaml2obj/invalid-symboless-relocation.yaml
@@ -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:]
diff --git a/test/tools/yaml2obj/symboless-relocation.yaml b/test/tools/yaml2obj/symboless-relocation.yaml
new file mode 100644
index 00000000000..99f7af61095
--- /dev/null
+++ b/test/tools/yaml2obj/symboless-relocation.yaml
@@ -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
diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp
index 1d28a1abffc..e1f73f7f991 100644
--- a/tools/yaml2obj/yaml2elf.cpp
+++ b/tools/yaml2obj/yaml2elf.cpp
@@ -459,7 +459,8 @@ ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
// 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;