llvm.org GIT mirror llvm / c67ca17
Add llvm-modextract tool. This program is for testing features that rely on multi-module bitcode files. It takes a multi-module bitcode file, extracts one of the modules and writes it to the output file. Differential Revision: https://reviews.llvm.org/D26778 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288201 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
8 changed file(s) with 122 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
6565 bool ShouldLazyLoadMetadata);
6666
6767 public:
68 ArrayRef getBuffer() const { return Buffer; }
68 StringRef getBuffer() const {
69 return StringRef((const char *)Buffer.begin(), Buffer.size());
70 }
6971
7072 /// Read the bitcode module and prepare for lazy deserialization of function
7173 /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well.
4747 llvm-lto2
4848 llvm-mc
4949 llvm-mcmarkup
50 llvm-modextract
5051 llvm-nm
5152 llvm-objdump
5253 llvm-opt-report
297297 r"\bllvm-lto2\b",
298298 r"\bllvm-mc\b",
299299 r"\bllvm-mcmarkup\b",
300 r"\bllvm-modextract\b",
300301 r"\bllvm-nm\b",
301302 r"\bllvm-objdump\b",
302303 r"\bllvm-pdbdump\b",
0 ; RUN: llvm-as -o %t %s
1 ; RUN: llvm-modextract -n 0 -o - %t | llvm-dis | FileCheck %s
2 ; RUN: not llvm-modextract -n 1 -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
3 ; RUN: llvm-modextract -b -n 0 -o - %t | llvm-dis | FileCheck %s
4 ; RUN: not llvm-modextract -b -n 1 -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
5
6 ; CHECK: define void @f()
7 ; ERROR: llvm-modextract: error: module index out of range; bitcode file contains 1 module(s)
8
9 define void @f() {
10 ret void
11 }
3535 llvm-lto
3636 llvm-mc
3737 llvm-mcmarkup
38 llvm-modextract
3839 llvm-nm
3940 llvm-objdump
4041 llvm-pdbdump
0 set(LLVM_LINK_COMPONENTS
1 IRReader
2 BitWriter
3 Core
4 Support
5 )
6
7 add_llvm_tool(llvm-modextract
8 llvm-modextract.cpp
9 )
0 ;===- ./tools/llvm-modextract/LLVMBuild.txt --------------------*- Conf -*--===;
1 ;
2 ; The LLVM Compiler Infrastructure
3 ;
4 ; This file is distributed under the University of Illinois Open Source
5 ; License. See LICENSE.TXT for details.
6 ;
7 ;===------------------------------------------------------------------------===;
8 ;
9 ; This is an LLVMBuild description file for the components in this subdirectory.
10 ;
11 ; For more information on the LLVMBuild system, please see:
12 ;
13 ; http://llvm.org/docs/LLVMBuild.html
14 ;
15 ;===------------------------------------------------------------------------===;
16
17 [component_0]
18 type = Tool
19 name = llvm-modextract
20 parent = Tools
21 required_libraries = BitReader BitWriter
0 //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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
25 BinaryExtract("b", cl::desc("Whether to perform binary extraction"));
26
27 static cl::opt OutputFilename("o", cl::Required,
28 cl::desc("Output filename"),
29 cl::value_desc("filename"));
30
31 static cl::opt
32 InputFilename(cl::Positional, cl::desc(""), cl::init("-"));
33
34 static cl::opt 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 MB =
44 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
45 std::vector 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;
56 std::unique_ptr Out(
57 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
58 ExitOnErr(errorCodeToError(EC));
59
60 if (BinaryExtract) {
61 SmallVector Header;
62 BitcodeWriter Writer(Header);
63 Out->os() << Header << Ms[ModuleIndex].getBuffer();
64 return 0;
65 }
66
67 std::unique_ptr M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
68 WriteBitcodeToFile(M.get(), Out->os());
69
70 return 0;
71 }