xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/IPO/EmbedBitcodePass.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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/IR/PassManager.h"
11 #include "llvm/Pass.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/MemoryBufferRef.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/TargetParser/Triple.h"
16 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
17 #include "llvm/Transforms/Utils/ModuleUtils.h"
18 
19 #include <string>
20 
21 using namespace llvm;
22 
23 PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {
24   if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true))
25     report_fatal_error("Can only embed the module once",
26                        /*gen_crash_diag=*/false);
27 
28   Triple T(M.getTargetTriple());
29   if (T.getObjectFormat() != Triple::ELF)
30     report_fatal_error(
31         "EmbedBitcode pass currently only supports ELF object format",
32         /*gen_crash_diag=*/false);
33   std::string Data;
34   raw_string_ostream OS(Data);
35   ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM);
36   embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto");
37   return PreservedAnalyses::all();
38 }
39