xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-modextract/llvm-modextract.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This program is for testing features that rely on multi-module bitcode files.
100b57cec5SDimitry Andric // It takes a multi-module bitcode file, extracts one of the modules and writes
110b57cec5SDimitry Andric // it to the output file.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
160b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
170b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
180b57cec5SDimitry Andric #include "llvm/Support/Error.h"
190b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
20*81ad6265SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
210b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
2204eeddc0SDimitry Andric #include "llvm/Support/WithColor.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
26fe6060f1SDimitry Andric static cl::OptionCategory ModextractCategory("Modextract Options");
27fe6060f1SDimitry Andric 
280b57cec5SDimitry Andric static cl::opt<bool>
29fe6060f1SDimitry Andric     BinaryExtract("b", cl::desc("Whether to perform binary extraction"),
30fe6060f1SDimitry Andric                   cl::cat(ModextractCategory));
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric static cl::opt<std::string> OutputFilename("o", cl::Required,
330b57cec5SDimitry Andric                                            cl::desc("Output filename"),
34fe6060f1SDimitry Andric                                            cl::value_desc("filename"),
35fe6060f1SDimitry Andric                                            cl::cat(ModextractCategory));
360b57cec5SDimitry Andric 
37fe6060f1SDimitry Andric static cl::opt<std::string> InputFilename(cl::Positional,
38fe6060f1SDimitry Andric                                           cl::desc("<input bitcode>"),
39fe6060f1SDimitry Andric                                           cl::init("-"),
40fe6060f1SDimitry Andric                                           cl::cat(ModextractCategory));
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric static cl::opt<unsigned> ModuleIndex("n", cl::Required,
430b57cec5SDimitry Andric                                      cl::desc("Index of module to extract"),
44fe6060f1SDimitry Andric                                      cl::value_desc("index"),
45fe6060f1SDimitry Andric                                      cl::cat(ModextractCategory));
460b57cec5SDimitry Andric 
main(int argc,char ** argv)470b57cec5SDimitry Andric int main(int argc, char **argv) {
48fe6060f1SDimitry Andric   cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()});
490b57cec5SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "Module extractor");
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   ExitOnError ExitOnErr("llvm-modextract: error: ");
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> MB =
540b57cec5SDimitry Andric       ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
550b57cec5SDimitry Andric   std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   LLVMContext Context;
580b57cec5SDimitry Andric   if (ModuleIndex >= Ms.size()) {
590b57cec5SDimitry Andric     errs() << "llvm-modextract: error: module index out of range; bitcode file "
600b57cec5SDimitry Andric               "contains "
610b57cec5SDimitry Andric            << Ms.size() << " module(s)\n";
620b57cec5SDimitry Andric     return 1;
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   std::error_code EC;
660b57cec5SDimitry Andric   std::unique_ptr<ToolOutputFile> Out(
678bcb0991SDimitry Andric       new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
680b57cec5SDimitry Andric   ExitOnErr(errorCodeToError(EC));
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   if (BinaryExtract) {
710b57cec5SDimitry Andric     SmallVector<char, 0> Result;
720b57cec5SDimitry Andric     BitcodeWriter Writer(Result);
730b57cec5SDimitry Andric     Result.append(Ms[ModuleIndex].getBuffer().begin(),
740b57cec5SDimitry Andric                   Ms[ModuleIndex].getBuffer().end());
750b57cec5SDimitry Andric     Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
760b57cec5SDimitry Andric     Out->os() << Result;
770b57cec5SDimitry Andric     Out->keep();
780b57cec5SDimitry Andric     return 0;
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
820b57cec5SDimitry Andric   WriteBitcodeToFile(*M, Out->os());
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   Out->keep();
850b57cec5SDimitry Andric   return 0;
860b57cec5SDimitry Andric }
87