xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-modextract/llvm-modextract.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
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"
200b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric static cl::opt<bool>
250b57cec5SDimitry Andric     BinaryExtract("b", cl::desc("Whether to perform binary extraction"));
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric static cl::opt<std::string> OutputFilename("o", cl::Required,
280b57cec5SDimitry Andric                                            cl::desc("Output filename"),
290b57cec5SDimitry Andric                                            cl::value_desc("filename"));
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric static cl::opt<std::string>
320b57cec5SDimitry Andric     InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric static cl::opt<unsigned> ModuleIndex("n", cl::Required,
350b57cec5SDimitry Andric                                      cl::desc("Index of module to extract"),
360b57cec5SDimitry Andric                                      cl::value_desc("index"));
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric int main(int argc, char **argv) {
390b57cec5SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "Module extractor");
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   ExitOnError ExitOnErr("llvm-modextract: error: ");
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> MB =
440b57cec5SDimitry Andric       ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
450b57cec5SDimitry Andric   std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   LLVMContext Context;
480b57cec5SDimitry Andric   if (ModuleIndex >= Ms.size()) {
490b57cec5SDimitry Andric     errs() << "llvm-modextract: error: module index out of range; bitcode file "
500b57cec5SDimitry Andric               "contains "
510b57cec5SDimitry Andric            << Ms.size() << " module(s)\n";
520b57cec5SDimitry Andric     return 1;
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   std::error_code EC;
560b57cec5SDimitry Andric   std::unique_ptr<ToolOutputFile> Out(
57*8bcb0991SDimitry Andric       new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
580b57cec5SDimitry Andric   ExitOnErr(errorCodeToError(EC));
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   if (BinaryExtract) {
610b57cec5SDimitry Andric     SmallVector<char, 0> Result;
620b57cec5SDimitry Andric     BitcodeWriter Writer(Result);
630b57cec5SDimitry Andric     Result.append(Ms[ModuleIndex].getBuffer().begin(),
640b57cec5SDimitry Andric                   Ms[ModuleIndex].getBuffer().end());
650b57cec5SDimitry Andric     Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
660b57cec5SDimitry Andric     Out->os() << Result;
670b57cec5SDimitry Andric     Out->keep();
680b57cec5SDimitry Andric     return 0;
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
720b57cec5SDimitry Andric   WriteBitcodeToFile(*M, Out->os());
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   Out->keep();
750b57cec5SDimitry Andric   return 0;
760b57cec5SDimitry Andric }
77