annotate tools/llvm-modextract/llvm-modextract.cpp @ 134:3a76565eade5 LLVM5.0.1

update 5.0.1
author mir3636
date Sat, 17 Feb 2018 09:57:20 +0900
parents 803732b1fca8
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // This program is for testing features that rely on multi-module bitcode files.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // It takes a multi-module bitcode file, extracts one of the modules and writes
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 // it to the output file.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "llvm/Bitcode/BitcodeReader.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/Bitcode/BitcodeWriter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/Support/CommandLine.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/Support/FileSystem.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Support/ToolOutputFile.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 static cl::opt<bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 BinaryExtract("b", cl::desc("Whether to perform binary extraction"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 static cl::opt<std::string> OutputFilename("o", cl::Required,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 cl::desc("Output filename"),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 cl::value_desc("filename"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 static cl::opt<std::string>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 static cl::opt<unsigned> ModuleIndex("n", cl::Required,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 cl::desc("Index of module to extract"),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 cl::value_desc("index"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 int main(int argc, char **argv) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 cl::ParseCommandLineOptions(argc, argv, "Module extractor");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 ExitOnError ExitOnErr("llvm-modextract: error: ");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 std::unique_ptr<MemoryBuffer> MB =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 LLVMContext Context;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 if (ModuleIndex >= Ms.size()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 errs() << "llvm-modextract: error: module index out of range; bitcode file "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 "contains "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 << Ms.size() << " module(s)\n";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 return 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 std::error_code EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 std::unique_ptr<ToolOutputFile> Out(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 ExitOnErr(errorCodeToError(EC));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 if (BinaryExtract) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 SmallVector<char, 0> Result;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 BitcodeWriter Writer(Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 Result.append(Ms[ModuleIndex].getBuffer().begin(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 Ms[ModuleIndex].getBuffer().end());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 Out->os() << Result;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 Out->keep();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
73 WriteBitcodeToFile(*M, Out->os());
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 Out->keep();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 }