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/Cloning.h"
20 #include "llvm/Transforms/Utils/ModuleUtils.h"
21 
22 #include <memory>
23 #include <string>
24 
25 using namespace llvm;
26 
27 PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {
28   if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true))
29     report_fatal_error("Can only embed the module once",
30                        /*gen_crash_diag=*/false);
31 
32   Triple T(M.getTargetTriple());
33   if (T.getObjectFormat() != Triple::ELF)
34     report_fatal_error(
35         "EmbedBitcode pass currently only supports ELF object format",
36         /*gen_crash_diag=*/false);
37 
38   std::unique_ptr<Module> NewModule = CloneModule(M);
39   MPM.run(*NewModule, AM);
40 
41   std::string Data;
42   raw_string_ostream OS(Data);
43   if (IsThinLTO)
44     ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(*NewModule, AM);
45   else
46     BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary)
47         .run(*NewModule, AM);
48 
49   embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto");
50 
51   return PreservedAnalyses::all();
52 }
53