1 //===----------- BPFPreserveDIType.cpp - Preserve DebugInfo Types ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Preserve Debuginfo types encoded in __builtin_btf_type_id() metadata.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "BPF.h"
14 #include "BPFCORE.h"
15 #include "llvm/IR/DebugInfoMetadata.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/IR/Instruction.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/IR/User.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
26 
27 #define DEBUG_TYPE "bpf-preserve-di-type"
28 
29 namespace llvm {
30 constexpr StringRef BPFCoreSharedInfo::TypeIdAttr;
31 } // namespace llvm
32 
33 using namespace llvm;
34 
35 namespace {
36 
BPFPreserveDITypeImpl(Function & F)37 static bool BPFPreserveDITypeImpl(Function &F) {
38   LLVM_DEBUG(dbgs() << "********** preserve debuginfo type **********\n");
39 
40   Module *M = F.getParent();
41 
42   // Bail out if no debug info.
43   if (M->debug_compile_units().empty())
44     return false;
45 
46   std::vector<CallInst *> PreserveDITypeCalls;
47 
48   for (auto &BB : F) {
49     for (auto &I : BB) {
50       auto *Call = dyn_cast<CallInst>(&I);
51       if (!Call)
52         continue;
53 
54       const auto *GV = dyn_cast<GlobalValue>(Call->getCalledOperand());
55       if (!GV)
56         continue;
57 
58       if (GV->getName().startswith("llvm.bpf.btf.type.id")) {
59         if (!Call->getMetadata(LLVMContext::MD_preserve_access_index))
60           report_fatal_error(
61               "Missing metadata for llvm.bpf.btf.type.id intrinsic");
62         PreserveDITypeCalls.push_back(Call);
63       }
64     }
65   }
66 
67   if (PreserveDITypeCalls.empty())
68     return false;
69 
70   std::string BaseName = "llvm.btf_type_id.";
71   static int Count = 0;
72   for (auto Call : PreserveDITypeCalls) {
73     const ConstantInt *Flag = dyn_cast<ConstantInt>(Call->getArgOperand(1));
74     assert(Flag);
75     uint64_t FlagValue = Flag->getValue().getZExtValue();
76 
77     if (FlagValue >= BPFCoreSharedInfo::MAX_BTF_TYPE_ID_FLAG)
78       report_fatal_error("Incorrect flag for llvm.bpf.btf.type.id intrinsic");
79 
80     MDNode *MD = Call->getMetadata(LLVMContext::MD_preserve_access_index);
81 
82     uint32_t Reloc;
83     if (FlagValue == BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL_RELOC) {
84       Reloc = BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL;
85     } else {
86       Reloc = BPFCoreSharedInfo::BTF_TYPE_ID_REMOTE;
87       DIType *Ty = cast<DIType>(MD);
88       while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
89         unsigned Tag = DTy->getTag();
90         if (Tag != dwarf::DW_TAG_const_type &&
91             Tag != dwarf::DW_TAG_volatile_type)
92           break;
93         Ty = DTy->getBaseType();
94       }
95 
96       if (Ty->getName().empty())
97         report_fatal_error("Empty type name for BTF_TYPE_ID_REMOTE reloc");
98       MD = Ty;
99     }
100 
101     BasicBlock *BB = Call->getParent();
102     IntegerType *VarType = Type::getInt64Ty(BB->getContext());
103     std::string GVName = BaseName + std::to_string(Count) + "$" +
104         std::to_string(Reloc);
105     GlobalVariable *GV = new GlobalVariable(
106         *M, VarType, false, GlobalVariable::ExternalLinkage, NULL, GVName);
107     GV->addAttribute(BPFCoreSharedInfo::TypeIdAttr);
108     GV->setMetadata(LLVMContext::MD_preserve_access_index, MD);
109 
110     // Load the global variable which represents the type info.
111     auto *LDInst =
112         new LoadInst(Type::getInt64Ty(BB->getContext()), GV, "", Call);
113     Instruction *PassThroughInst =
114         BPFCoreSharedInfo::insertPassThrough(M, BB, LDInst, Call);
115     Call->replaceAllUsesWith(PassThroughInst);
116     Call->eraseFromParent();
117     Count++;
118   }
119 
120   return true;
121 }
122 
123 class BPFPreserveDIType final : public FunctionPass {
124   bool runOnFunction(Function &F) override;
125 
126 public:
127   static char ID;
BPFPreserveDIType()128   BPFPreserveDIType() : FunctionPass(ID) {}
129 };
130 } // End anonymous namespace
131 
132 char BPFPreserveDIType::ID = 0;
133 INITIALIZE_PASS(BPFPreserveDIType, DEBUG_TYPE, "BPF Preserve Debuginfo Type",
134                 false, false)
135 
createBPFPreserveDIType()136 FunctionPass *llvm::createBPFPreserveDIType() {
137   return new BPFPreserveDIType();
138 }
139 
runOnFunction(Function & F)140 bool BPFPreserveDIType::runOnFunction(Function &F) {
141   return BPFPreserveDITypeImpl(F);
142 }
143 
run(Function & F,FunctionAnalysisManager & AM)144 PreservedAnalyses BPFPreserveDITypePass::run(Function &F,
145                                              FunctionAnalysisManager &AM) {
146   return BPFPreserveDITypeImpl(F) ? PreservedAnalyses::none()
147                                   : PreservedAnalyses::all();
148 }
149