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" 21*04eeddc0SDimitry Andric #include "llvm/Support/WithColor.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric 25fe6060f1SDimitry Andric static cl::OptionCategory ModextractCategory("Modextract Options"); 26fe6060f1SDimitry Andric 270b57cec5SDimitry Andric static cl::opt<bool> 28fe6060f1SDimitry Andric BinaryExtract("b", cl::desc("Whether to perform binary extraction"), 29fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric static cl::opt<std::string> OutputFilename("o", cl::Required, 320b57cec5SDimitry Andric cl::desc("Output filename"), 33fe6060f1SDimitry Andric cl::value_desc("filename"), 34fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 350b57cec5SDimitry Andric 36fe6060f1SDimitry Andric static cl::opt<std::string> InputFilename(cl::Positional, 37fe6060f1SDimitry Andric cl::desc("<input bitcode>"), 38fe6060f1SDimitry Andric cl::init("-"), 39fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric static cl::opt<unsigned> ModuleIndex("n", cl::Required, 420b57cec5SDimitry Andric cl::desc("Index of module to extract"), 43fe6060f1SDimitry Andric cl::value_desc("index"), 44fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric int main(int argc, char **argv) { 47fe6060f1SDimitry Andric cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()}); 480b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv, "Module extractor"); 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric ExitOnError ExitOnErr("llvm-modextract: error: "); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> MB = 530b57cec5SDimitry Andric ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); 540b57cec5SDimitry Andric std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB)); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric LLVMContext Context; 570b57cec5SDimitry Andric if (ModuleIndex >= Ms.size()) { 580b57cec5SDimitry Andric errs() << "llvm-modextract: error: module index out of range; bitcode file " 590b57cec5SDimitry Andric "contains " 600b57cec5SDimitry Andric << Ms.size() << " module(s)\n"; 610b57cec5SDimitry Andric return 1; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric std::error_code EC; 650b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> Out( 668bcb0991SDimitry Andric new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None)); 670b57cec5SDimitry Andric ExitOnErr(errorCodeToError(EC)); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric if (BinaryExtract) { 700b57cec5SDimitry Andric SmallVector<char, 0> Result; 710b57cec5SDimitry Andric BitcodeWriter Writer(Result); 720b57cec5SDimitry Andric Result.append(Ms[ModuleIndex].getBuffer().begin(), 730b57cec5SDimitry Andric Ms[ModuleIndex].getBuffer().end()); 740b57cec5SDimitry Andric Writer.copyStrtab(Ms[ModuleIndex].getStrtab()); 750b57cec5SDimitry Andric Out->os() << Result; 760b57cec5SDimitry Andric Out->keep(); 770b57cec5SDimitry Andric return 0; 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context)); 810b57cec5SDimitry Andric WriteBitcodeToFile(*M, Out->os()); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric Out->keep(); 840b57cec5SDimitry Andric return 0; 850b57cec5SDimitry Andric } 86