llvm-modextract.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===-- llvm-modextract.cpp - LLVM module extractor utility ---------------===//
  2. //
  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
  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. #include "llvm/Bitcode/BitcodeReader.h"
  15. #include "llvm/Bitcode/BitcodeWriter.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/ToolOutputFile.h"
  20. #include "llvm/Support/WithColor.h"
  21. using namespace llvm;
  22. static cl::OptionCategory ModextractCategory("Modextract Options");
  23. static cl::opt<bool>
  24. BinaryExtract("b", cl::desc("Whether to perform binary extraction"),
  25. cl::cat(ModextractCategory));
  26. static cl::opt<std::string> OutputFilename("o", cl::Required,
  27. cl::desc("Output filename"),
  28. cl::value_desc("filename"),
  29. cl::cat(ModextractCategory));
  30. static cl::opt<std::string> InputFilename(cl::Positional,
  31. cl::desc("<input bitcode>"),
  32. cl::init("-"),
  33. cl::cat(ModextractCategory));
  34. static cl::opt<unsigned> ModuleIndex("n", cl::Required,
  35. cl::desc("Index of module to extract"),
  36. cl::value_desc("index"),
  37. cl::cat(ModextractCategory));
  38. int main(int argc, char **argv) {
  39. cl::HideUnrelatedOptions({&ModextractCategory, &getColorCategory()});
  40. cl::ParseCommandLineOptions(argc, argv, "Module extractor");
  41. ExitOnError ExitOnErr("llvm-modextract: error: ");
  42. std::unique_ptr<MemoryBuffer> MB =
  43. ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
  44. std::vector<BitcodeModule> Ms = ExitOnErr(getBitcodeModuleList(*MB));
  45. LLVMContext Context;
  46. if (ModuleIndex >= Ms.size()) {
  47. errs() << "llvm-modextract: error: module index out of range; bitcode file "
  48. "contains "
  49. << Ms.size() << " module(s)\n";
  50. return 1;
  51. }
  52. std::error_code EC;
  53. std::unique_ptr<ToolOutputFile> Out(
  54. new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
  55. ExitOnErr(errorCodeToError(EC));
  56. if (BinaryExtract) {
  57. SmallVector<char, 0> Result;
  58. BitcodeWriter Writer(Result);
  59. Result.append(Ms[ModuleIndex].getBuffer().begin(),
  60. Ms[ModuleIndex].getBuffer().end());
  61. Writer.copyStrtab(Ms[ModuleIndex].getStrtab());
  62. Out->os() << Result;
  63. Out->keep();
  64. return 0;
  65. }
  66. std::unique_ptr<Module> M = ExitOnErr(Ms[ModuleIndex].parseModule(Context));
  67. WriteBitcodeToFile(*M, Out->os());
  68. Out->keep();
  69. return 0;
  70. }