1 //===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===//
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
7 //===----------------------------------------------------------------------===//
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"
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);
32 Triple
T(M
.getTargetTriple());
33 if (T
.getObjectFormat() != Triple::ELF
)
35 "EmbedBitcode pass currently only supports ELF object format",
36 /*gen_crash_diag=*/false);
38 std::unique_ptr
<Module
> NewModule
= CloneModule(M
);
39 MPM
.run(*NewModule
, AM
);
42 raw_string_ostream
OS(Data
);
44 ThinLTOBitcodeWriterPass(OS
, /*ThinLinkOS=*/nullptr).run(*NewModule
, AM
);
46 BitcodeWriterPass(OS
, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary
)
49 embedBufferInModule(M
, MemoryBufferRef(Data
, "ModuleData"), ".llvm.lto");
51 return PreservedAnalyses::all();