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 24*fe6060f1SDimitry Andric static cl::OptionCategory ModextractCategory("Modextract Options"); 25*fe6060f1SDimitry Andric 260b57cec5SDimitry Andric static cl::opt<bool> 27*fe6060f1SDimitry Andric BinaryExtract("b", cl::desc("Whether to perform binary extraction"), 28*fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric static cl::opt<std::string> OutputFilename("o", cl::Required, 310b57cec5SDimitry Andric cl::desc("Output filename"), 32*fe6060f1SDimitry Andric cl::value_desc("filename"), 33*fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 340b57cec5SDimitry Andric 35*fe6060f1SDimitry Andric static cl::opt<std::string> InputFilename(cl::Positional, 36*fe6060f1SDimitry Andric cl::desc("<input bitcode>"), 37*fe6060f1SDimitry Andric cl::init("-"), 38*fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric static cl::opt<unsigned> ModuleIndex("n", cl::Required, 410b57cec5SDimitry Andric cl::desc("Index of module to extract"), 42*fe6060f1SDimitry Andric cl::value_desc("index"), 43*fe6060f1SDimitry Andric cl::cat(ModextractCategory)); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric int main(int argc, char **argv) { 46*fe6060f1SDimitry Andric cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()}); 470b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv, "Module extractor"); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric ExitOnError ExitOnErr("llvm-modextract: error: "); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> MB = 520b57cec5SDimitry Andric ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); 530b57cec5SDimitry Andric std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB)); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric LLVMContext Context; 560b57cec5SDimitry Andric if (ModuleIndex >= Ms.size()) { 570b57cec5SDimitry Andric errs() << "llvm-modextract: error: module index out of range; bitcode file " 580b57cec5SDimitry Andric "contains " 590b57cec5SDimitry Andric << Ms.size() << " module(s)\n"; 600b57cec5SDimitry Andric return 1; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric std::error_code EC; 640b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> Out( 658bcb0991SDimitry Andric new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None)); 660b57cec5SDimitry Andric ExitOnErr(errorCodeToError(EC)); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric if (BinaryExtract) { 690b57cec5SDimitry Andric SmallVector<char, 0> Result; 700b57cec5SDimitry Andric BitcodeWriter Writer(Result); 710b57cec5SDimitry Andric Result.append(Ms[ModuleIndex].getBuffer().begin(), 720b57cec5SDimitry Andric Ms[ModuleIndex].getBuffer().end()); 730b57cec5SDimitry Andric Writer.copyStrtab(Ms[ModuleIndex].getStrtab()); 740b57cec5SDimitry Andric Out->os() << Result; 750b57cec5SDimitry Andric Out->keep(); 760b57cec5SDimitry Andric return 0; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context)); 800b57cec5SDimitry Andric WriteBitcodeToFile(*M, Out->os()); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric Out->keep(); 830b57cec5SDimitry Andric return 0; 840b57cec5SDimitry Andric } 85