| 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(Name: "llvm.embedded.module" , /*AllowInternal=*/true)) |
| 27 | reportFatalUsageError(reason: "Can only embed the module once" ); |
| 28 | |
| 29 | Triple T(M.getTargetTriple()); |
| 30 | if (T.getObjectFormat() != Triple::ELF) |
| 31 | reportFatalUsageError( |
| 32 | reason: "EmbedBitcode pass currently only supports ELF object format" ); |
| 33 | |
| 34 | std::string Data; |
| 35 | raw_string_ostream OS(Data); |
| 36 | if (IsThinLTO) |
| 37 | ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM); |
| 38 | else |
| 39 | BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary) |
| 40 | .run(M, AM); |
| 41 | |
| 42 | embedBufferInModule(M, Buf: MemoryBufferRef(Data, "ModuleData" ), SectionName: ".llvm.lto" ); |
| 43 | |
| 44 | return PreservedAnalyses::none(); |
| 45 | } |
| 46 | |