1 //===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===// 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 #include "llvm/Transforms/IPO/EmbedBitcodePass.h" 10 #include "llvm/Bitcode/BitcodeWriter.h" 11 #include "llvm/Bitcode/BitcodeWriterPass.h" 12 #include "llvm/IR/PassManager.h" 13 #include "llvm/Pass.h" 14 #include "llvm/Support/ErrorHandling.h" 15 #include "llvm/Support/MemoryBufferRef.h" 16 #include "llvm/Support/raw_ostream.h" 17 #include "llvm/TargetParser/Triple.h" 18 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" 19 #include "llvm/Transforms/Utils/ModuleUtils.h" 20 21 #include <string> 22 23 using namespace llvm; 24 25 PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) { 26 if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true)) 27 report_fatal_error("Can only embed the module once", 28 /*gen_crash_diag=*/false); 29 30 Triple T(M.getTargetTriple()); 31 if (T.getObjectFormat() != Triple::ELF) 32 report_fatal_error( 33 "EmbedBitcode pass currently only supports ELF object format", 34 /*gen_crash_diag=*/false); 35 36 std::string Data; 37 raw_string_ostream OS(Data); 38 if (IsThinLTO) 39 ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM); 40 else 41 BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary) 42 .run(M, AM); 43 44 embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto"); 45 46 return PreservedAnalyses::all(); 47 } 48