[llvm] de87238 - [JITLink] Add anonymous symbols in LinkGraph for unnamed temporary symbols

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 22 00:51:03 PST 2022


Author: luxufan
Date: 2022-01-22T16:47:39+08:00
New Revision: de872382951572b70dfaefe8d77eb98d15586115

URL: https://github.com/llvm/llvm-project/commit/de872382951572b70dfaefe8d77eb98d15586115
DIFF: https://github.com/llvm/llvm-project/commit/de872382951572b70dfaefe8d77eb98d15586115.diff

LOG: [JITLink] Add anonymous symbols in LinkGraph for unnamed temporary symbols

In RISCV, temporary symbols will be used to generate dwarf, eh_frame sections..., and will be placed in object code's symbol table. However, LLVM does not use names on these temporary symbols. This patch add anonymous symbols in LinkGraph for these temporary symbols.

Reviewed By: lhames

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

Added: 
    llvm/test/ExecutionEngine/JITLink/RISCV/anonymous_symbol.s

Modified: 
    llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
    llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
    llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
    llvm/lib/ExecutionEngine/JITLink/riscv.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h b/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
index 3c5cdfcfdba40..4d045a23c012b 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/riscv.h
@@ -79,6 +79,12 @@ enum EdgeKind_riscv : Edge::Kind {
   ///   Fixup <- (Target - Fixup + Addend)
   R_RISCV_CALL,
 
+  /// 32 bits PC relative relocation
+  ///
+  /// Fixup expression:
+  ///   Fixup <- (Target - Fixup + Addend)
+  R_RISCV_32_PCREL,
+
   /// PC relative GOT offset
   ///
   /// Fixup expression:
@@ -137,7 +143,7 @@ enum EdgeKind_riscv : Edge::Kind {
   ///
   /// Fixup expression
   ///   Fixup <- (Target - *{1}Fixup - Addend)
-  R_RISCV_SUB8
+  R_RISCV_SUB8,
 };
 
 /// Returns a string name for the given riscv edge. For debugging purposes

diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
index 931a60224ee2f..2ab7ed61f71b4 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
+++ b/llvm/lib/ExecutionEngine/JITLink/ELFLinkGraphBuilder.h
@@ -441,11 +441,15 @@ template <typename ELFT> Error ELFLinkGraphBuilder<ELFT>::graphifySymbols() {
                  << "\"\n";
         });
 
-        // Model the section symbols as anonymous symbol.
+        // In RISCV, temporary symbols (Used to generate dwarf, eh_frame
+        // sections...) will appear in object code's symbol table, and LLVM does
+        // not use names on these temporary symbols (RISCV gnu toolchain uses
+        // names on these temporary symbols). If the symbol is unnamed, add an
+        // anonymous symbol.
         auto &GSym =
-            Sym.getType() == ELF::STT_SECTION
-                ? G->addAnonymousSymbol(*B, Sym.getValue(), Sym.st_size, false,
-                                        false)
+            Name->empty()
+                ? G->addAnonymousSymbol(*B, Sym.getValue(), Sym.st_size,
+                                        false, false)
                 : G->addDefinedSymbol(*B, Sym.getValue(), *Name, Sym.st_size, L,
                                       S, Sym.getType() == ELF::STT_FUNC, false);
         setGraphSymbol(SymIndex, GSym);

diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
index 4483147c1b1d7..b3bc5cdb13f1d 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
@@ -359,6 +359,12 @@ class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
       *FixupPtr = static_cast<uint8_t>(Value);
       break;
     }
+    case R_RISCV_32_PCREL: {
+      // FIXME: It seems that R_RISCV_32_PCREL relocation will only appear in debug sections
+      // like eh_frame section. Currently, because of eh_frame will not be processed in JITLink's RISCV
+      // backend, test this relocation is 
diff icult, so here report error if needs to fixup this relocation
+      return make_error<JITLinkError>("Fixup of relocation type R_RISCV_32_PCREL is not supportted");
+    }
     }
     return Error::success();
   }
@@ -409,6 +415,8 @@ class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
       return EdgeKind_riscv::R_RISCV_SUB16;
     case ELF::R_RISCV_SUB8:
       return EdgeKind_riscv::R_RISCV_SUB8;
+    case ELF::R_RISCV_32_PCREL:
+      return EdgeKind_riscv::R_RISCV_32_PCREL;
     }
 
     return make_error<JITLinkError>("Unsupported riscv relocation:" +

diff  --git a/llvm/lib/ExecutionEngine/JITLink/riscv.cpp b/llvm/lib/ExecutionEngine/JITLink/riscv.cpp
index 4d1ace73a04e3..1dcc3ebb8e97b 100644
--- a/llvm/lib/ExecutionEngine/JITLink/riscv.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/riscv.cpp
@@ -38,6 +38,8 @@ const char *getEdgeKindName(Edge::Kind K) {
     return "R_RISCV_PCREL_LO12_S";
   case R_RISCV_CALL:
     return "R_RISCV_CALL";
+  case R_RISCV_32_PCREL:
+    return "R_RISCV_32_PCREL";
   case R_RISCV_ADD64:
     return "R_RISCV_ADD64";
   case R_RISCV_ADD32:

diff  --git a/llvm/test/ExecutionEngine/JITLink/RISCV/anonymous_symbol.s b/llvm/test/ExecutionEngine/JITLink/RISCV/anonymous_symbol.s
new file mode 100644
index 0000000000000..cb4c59e196dc9
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/RISCV/anonymous_symbol.s
@@ -0,0 +1,20 @@
+# RUN: llvm-mc -triple=riscv64 -filetype=obj -o %t %s
+# RUN: llvm-jitlink -debug-only=jitlink -noexec %t 2>&1 | FileCheck %s
+#
+# Because of the exist of cfi directive, sections like eh_frame section will be emitted
+# in llvm's object code emission phase. Anonymous symbols will also be emitted to indicate
+# the section start and section end. So that by relocating these symbol, the section length
+# can be calculated.
+#
+# CHECK: Creating defined graph symbol for ELF symbol ""
+# CHECK: Creating defined graph symbol for ELF symbol ""
+        .text
+        .globl main
+        .p2align 2
+        .type main, at function
+main:
+        .cfi_startproc
+        ret
+        .Lfunc_end0:
+        .size main, .Lfunc_end0-main
+        .cfi_endproc


        


More information about the llvm-commits mailing list