xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-modextract/llvm-modextract.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===//
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 // This program is for testing features that rely on multi-module bitcode files.
10 // It takes a multi-module bitcode file, extracts one of the modules and writes
11 // it to the output file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Bitcode/BitcodeReader.h"
16 #include "llvm/Bitcode/BitcodeWriter.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 #include "llvm/Support/WithColor.h"
22 
23 using namespace llvm;
24 
25 static cl::OptionCategory ModextractCategory("Modextract Options");
26 
27 static cl::opt<bool>
28     BinaryExtract("b", cl::desc("Whether to perform binary extraction"),
29                   cl::cat(ModextractCategory));
30 
31 static cl::opt<std::string> OutputFilename("o", cl::Required,
32                                            cl::desc("Output filename"),
33                                            cl::value_desc("filename"),
34                                            cl::cat(ModextractCategory));
35 
36 static cl::opt<std::string> InputFilename(cl::Positional,
37                                           cl::desc("<input bitcode>"),
38                                           cl::init("-"),
39                                           cl::cat(ModextractCategory));
40 
41 static cl::opt<unsigned> ModuleIndex("n", cl::Required,
42                                      cl::desc("Index of module to extract"),
43                                      cl::value_desc("index"),
44                                      cl::cat(ModextractCategory));
45 
46 int main(int argc, char **argv) {
47   cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()});
48   cl::ParseCommandLineOptions(argc, argv, "Module extractor");
49 
50   ExitOnError ExitOnErr("llvm-modextract: error: ");
51 
52   std::unique_ptr<MemoryBuffer> MB =
53       ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
54   std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
55 
56   LLVMContext Context;
57   if (ModuleIndex >= Ms.size()) {
58     errs() << "llvm-modextract: error: module index out of range; bitcode file "
59               "contains "
60            << Ms.size() << " module(s)\n";
61     return 1;
62   }
63 
64   std::error_code EC;
65   std::unique_ptr<ToolOutputFile> Out(
66       new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
67   ExitOnErr(errorCodeToError(EC));
68 
69   if (BinaryExtract) {
70     SmallVector<char, 0> Result;
71     BitcodeWriter Writer(Result);
72     Result.append(Ms[ModuleIndex].getBuffer().begin(),
73                   Ms[ModuleIndex].getBuffer().end());
74     Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
75     Out->os() << Result;
76     Out->keep();
77     return 0;
78   }
79 
80   std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
81   WriteBitcodeToFile(*M, Out->os());
82 
83   Out->keep();
84   return 0;
85 }
86