annotate tools/llvm-modextract/llvm-modextract.cpp @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 3a76565eade5
children
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 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 //
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 // This program is for testing features that rely on multi-module bitcode files.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // It takes a multi-module bitcode file, extracts one of the modules and writes
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // it to the output file.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //
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 #include "llvm/Bitcode/BitcodeReader.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "llvm/Bitcode/BitcodeWriter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/Support/CommandLine.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/Support/FileSystem.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/Support/ToolOutputFile.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 static cl::opt<bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 BinaryExtract("b", cl::desc("Whether to perform binary extraction"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 static cl::opt<std::string> OutputFilename("o", cl::Required,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 cl::desc("Output filename"),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 cl::value_desc("filename"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 static cl::opt<std::string>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 static cl::opt<unsigned> ModuleIndex("n", cl::Required,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 cl::desc("Index of module to extract"),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 cl::value_desc("index"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 int main(int argc, char **argv) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 cl::ParseCommandLineOptions(argc, argv, "Module extractor");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 ExitOnError ExitOnErr("llvm-modextract: error: ");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 std::unique_ptr<MemoryBuffer> MB =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 LLVMContext Context;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 if (ModuleIndex >= Ms.size()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 errs() << "llvm-modextract: error: module index out of range; bitcode file "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 "contains "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 << Ms.size() << " module(s)\n";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 return 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 std::error_code EC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 std::unique_ptr<ToolOutputFile> Out(
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
57 new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 ExitOnErr(errorCodeToError(EC));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 if (BinaryExtract) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 SmallVector<char, 0> Result;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 BitcodeWriter Writer(Result);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 Result.append(Ms[ModuleIndex].getBuffer().begin(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 Ms[ModuleIndex].getBuffer().end());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 Out->os() << Result;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 Out->keep();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
72 WriteBitcodeToFile(*M, Out->os());
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 Out->keep();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 }