tstellar / rpms / llvm

Forked from rpms/llvm 5 years ago
Clone
1bf09a7
From 39184e407cd937f2f20d3f61eec205925bae7b13 Mon Sep 17 00:00:00 2001
1bf09a7
From: Yonghong Song <yhs@fb.com>
1bf09a7
Date: Wed, 22 Aug 2018 21:21:03 +0000
1bf09a7
Subject: [PATCH] bpf: fix an assertion in BPFAsmBackend applyFixup()
1bf09a7
1bf09a7
Fix bug https://bugs.llvm.org/show_bug.cgi?id=38643
1bf09a7
1bf09a7
In BPFAsmBackend applyFixup(), there is an assertion for FixedValue to be 0.
1bf09a7
This may not be true, esp. for optimiation level 0.
1bf09a7
For example, in the above bug, for the following two
1bf09a7
static variables:
1bf09a7
  @bpf_map_lookup_elem = internal global i8* (i8*, i8*)*
1bf09a7
      inttoptr (i64 1 to i8* (i8*, i8*)*), align 8
1bf09a7
  @bpf_map_update_elem = internal global i32 (i8*, i8*, i8*, i64)*
1bf09a7
      inttoptr (i64 2 to i32 (i8*, i8*, i8*, i64)*), align 8
1bf09a7
1bf09a7
The static variable @bpf_map_update_elem will have a symbol
1bf09a7
offset of 8 and a FK_SecRel_8 with FixupValue 8 will cause
1bf09a7
the assertion if llvm is built with -DLLVM_ENABLE_ASSERTIONS=ON.
1bf09a7
1bf09a7
The above relocations will not exist if the program is compiled
1bf09a7
with optimization level -O1 and above as the compiler optimizes
1bf09a7
those static variables away. In the below error message, -O2
1bf09a7
is suggested as this is the common practice.
1bf09a7
1bf09a7
Note that FixedValue = 0 in applyFixup() does exist and is valid,
1bf09a7
e.g., for the global variable my_map in the above bug. The bpf
1bf09a7
loader will process them properly for map_id's before loading
1bf09a7
the program into the kernel.
1bf09a7
1bf09a7
The static variables, which are not optimized away by compiler,
1bf09a7
may have FK_SecRel_8 relocation with non-zero FixedValue.
1bf09a7
1bf09a7
The patch removed the offending assertion and will issue
1bf09a7
a hard error as below if the FixedValue in applyFixup()
1bf09a7
is not 0.
1bf09a7
  $ llc -march=bpf -filetype=obj fixup.ll
1bf09a7
  LLVM ERROR: Unsupported relocation: try to compile with -O2 or above,
1bf09a7
      or check your static variable usage
1bf09a7
1bf09a7
Signed-off-by: Yonghong Song <yhs@fb.com>
1bf09a7
1bf09a7
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340455 91177308-0d34-0410-b5e6-96231b3b80d8
1bf09a7
---
1bf09a7
 lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp | 9 ++++++++-
1bf09a7
 1 file changed, 8 insertions(+), 1 deletion(-)
1bf09a7
1bf09a7
diff --git a/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp b/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
1bf09a7
index 6c255e9..1822d86 100644
1bf09a7
--- a/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
1bf09a7
+++ b/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
1bf09a7
@@ -10,6 +10,8 @@
1bf09a7
 #include "MCTargetDesc/BPFMCTargetDesc.h"
1bf09a7
 #include "llvm/ADT/StringRef.h"
1bf09a7
 #include "llvm/MC/MCAsmBackend.h"
1bf09a7
+#include "llvm/MC/MCAssembler.h"
1bf09a7
+#include "llvm/MC/MCContext.h"
1bf09a7
 #include "llvm/MC/MCFixup.h"
1bf09a7
 #include "llvm/MC/MCObjectWriter.h"
1bf09a7
 #include "llvm/Support/EndianStream.h"
1bf09a7
@@ -71,7 +73,12 @@ void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
1bf09a7
                                bool IsResolved,
1bf09a7
                                const MCSubtargetInfo *STI) const {
1bf09a7
   if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
1bf09a7
-    assert(Value == 0);
1bf09a7
+    if (Value) {
1bf09a7
+      MCContext &Ctx = Asm.getContext();
1bf09a7
+      Ctx.reportError(Fixup.getLoc(),
1bf09a7
+                      "Unsupported relocation: try to compile with -O2 or above, "
1bf09a7
+                      "or check your static variable usage");
1bf09a7
+    }
1bf09a7
   } else if (Fixup.getKind() == FK_Data_4) {
1bf09a7
     support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
1bf09a7
   } else if (Fixup.getKind() == FK_Data_8) {
1bf09a7
-- 
1bf09a7
1.8.3.1
1bf09a7