Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 1 | //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 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 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | static cl::opt<bool> |
| 25 | BinaryExtract("b", cl::desc("Whether to perform binary extraction")); |
| 26 | |
| 27 | static cl::opt<std::string> OutputFilename("o", cl::Required, |
| 28 | cl::desc("Output filename"), |
| 29 | cl::value_desc("filename")); |
| 30 | |
| 31 | static cl::opt<std::string> |
| 32 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
| 33 | |
| 34 | static cl::opt<unsigned> ModuleIndex("n", cl::Required, |
| 35 | cl::desc("Index of module to extract"), |
| 36 | cl::value_desc("index")); |
| 37 | |
| 38 | int main(int argc, char **argv) { |
| 39 | cl::ParseCommandLineOptions(argc, argv, "Module extractor"); |
| 40 | |
| 41 | ExitOnError ExitOnErr("llvm-modextract: error: "); |
| 42 | |
| 43 | std::unique_ptr<MemoryBuffer> MB = |
| 44 | ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); |
| 45 | std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB)); |
| 46 | |
| 47 | LLVMContext Context; |
| 48 | if (ModuleIndex >= Ms.size()) { |
| 49 | errs() << "llvm-modextract: error: module index out of range; bitcode file " |
| 50 | "contains " |
| 51 | << Ms.size() << " module(s)\n"; |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | std::error_code EC; |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 56 | std::unique_ptr<ToolOutputFile> Out( |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 57 | new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None)); |
Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 58 | ExitOnErr(errorCodeToError(EC)); |
| 59 | |
| 60 | if (BinaryExtract) { |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 61 | SmallVector<char, 0> Result; |
| 62 | BitcodeWriter Writer(Result); |
| 63 | Result.append(Ms[ModuleIndex].getBuffer().begin(), |
| 64 | Ms[ModuleIndex].getBuffer().end()); |
| 65 | Writer.copyStrtab(Ms[ModuleIndex].getStrtab()); |
| 66 | Out->os() << Result; |
Peter Collingbourne | 85c2184a | 2016-12-01 23:13:11 +0000 | [diff] [blame] | 67 | Out->keep(); |
Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context)); |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 72 | WriteBitcodeToFile(*M, Out->os()); |
Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 73 | |
Peter Collingbourne | 85c2184a | 2016-12-01 23:13:11 +0000 | [diff] [blame] | 74 | Out->keep(); |
Peter Collingbourne | cb6b920 | 2016-11-29 21:54:33 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |