12 #include "clang/Basic/Version.h" 13 #include "clang/CodeGen/CodeGenAction.h" 14 #include "clang/Frontend/CompilerInvocation.h" 15 #include "clang/Frontend/CompilerInstance.h" 16 #include "clang/Frontend/TextDiagnosticPrinter.h" 17 #include "clang/Tooling/Tooling.h" 18 #include "llvm/IR/LegacyPassManager.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/Option/Option.h" 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/ManagedStatic.h" 23 #include "llvm/Support/TargetRegistry.h" 24 #include "llvm/Support/TargetSelect.h" 25 #include "llvm/Target/TargetMachine.h" 26 #include "llvm/Target/TargetOptions.h" 34 static std::unique_ptr<llvm::LLVMContext>
LLVMCtx;
36 using namespace clang;
38 static CompilerInvocation *
40 DiagnosticsEngine& Diags)
42 llvm::opt::ArgStringList CCArgs {
44 "-triple",
"bpf-pc-linux",
46 "-ferror-limit",
"19",
47 "-fmessage-length",
"127",
57 CCArgs.append(CFlags.begin(), CFlags.end());
58 CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
60 FrontendOptions& Opts = CI->getFrontendOpts();
62 Opts.Inputs.emplace_back(Path,
63 FrontendOptions::getInputKindForExtension(
"c"));
67 static std::unique_ptr<llvm::Module>
69 StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
71 CompilerInstance Clang;
72 Clang.createDiagnostics();
74 Clang.setVirtualFileSystem(&*VFS);
76 #if CLANG_VERSION_MAJOR < 4 77 IntrusiveRefCntPtr<CompilerInvocation> CI =
79 Clang.getDiagnostics());
80 Clang.setInvocation(&*CI);
82 std::shared_ptr<CompilerInvocation> CI(
84 Clang.getDiagnostics()));
85 Clang.setInvocation(CI);
88 std::unique_ptr<CodeGenAction> Act(
new EmitLLVMOnlyAction(&*LLVMCtx));
89 if (!Clang.ExecuteAction(*Act))
90 return std::unique_ptr<llvm::Module>(
nullptr);
92 return Act->takeModule();
95 std::unique_ptr<llvm::Module>
97 StringRef Name, StringRef Content)
101 llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
102 new OverlayFileSystem(getRealFileSystem()));
103 llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
104 new InMemoryFileSystem(
true));
110 OverlayFS->pushOverlay(MemFS);
111 MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
116 std::unique_ptr<llvm::Module>
119 IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
123 std::unique_ptr<llvm::SmallVectorImpl<char>>
126 using namespace llvm;
128 std::string TargetTriple(
"bpf-pc-linux");
130 const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
132 llvm::errs() << Error;
133 return std::unique_ptr<llvm::SmallVectorImpl<char>>(
nullptr);
136 llvm::TargetOptions Opt;
137 TargetMachine *TargetMachine =
138 Target->createTargetMachine(TargetTriple,
142 Module->setDataLayout(TargetMachine->createDataLayout());
143 Module->setTargetTriple(TargetTriple);
145 std::unique_ptr<SmallVectorImpl<char>> Buffer(
new SmallVector<char, 0>());
146 raw_svector_ostream ostream(*Buffer);
148 legacy::PassManager PM;
149 if (TargetMachine->addPassesToEmitFile(PM, ostream,
150 TargetMachine::CGFT_ObjectFile)) {
151 llvm::errs() <<
"TargetMachine can't emit a file of this type\n";
152 return std::unique_ptr<llvm::SmallVectorImpl<char>>(
nullptr);;
156 return std::move(Buffer);
165 LLVMInitializeBPFTargetInfo();
166 LLVMInitializeBPFTarget();
167 LLVMInitializeBPFTargetMC();
168 LLVMInitializeBPFAsmPrinter();
174 llvm::llvm_shutdown();
179 size_t *p_obj_buf_sz)
181 using namespace perf;
183 if (!p_obj_buf || !p_obj_buf_sz)
186 llvm::opt::ArgStringList CFlags;
194 size_t size = O->size_in_bytes();
202 *p_obj_buf_sz =
size;
static std::unique_ptr< llvm::LLVMContext > LLVMCtx
void perf_clang__cleanup(void)
x86 movsq based memcpy() in arch/x86/lib/memcpy_64.S") MEMCPY_FN(memcpy_erms
static std::unique_ptr< llvm::Module > getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path, IntrusiveRefCntPtr< vfs::FileSystem > VFS)
static CompilerInvocation * createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef &Path, DiagnosticsEngine &Diags)
int perf_clang__compile_bpf(const char *filename, void **p_obj_buf, size_t *p_obj_buf_sz)
std::unique_ptr< llvm::SmallVectorImpl< char > > getBPFObjectFromModule(llvm::Module *Module)
void perf_clang__init(void)