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