TestingSupport.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===- TestingSupport.cpp - Convert objects files into test files --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Object/ObjectFile.h"
  10. #include "llvm/Support/CommandLine.h"
  11. #include "llvm/Support/LEB128.h"
  12. #include "llvm/Support/ManagedStatic.h"
  13. #include "llvm/Support/PrettyStackTrace.h"
  14. #include "llvm/Support/Signals.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <functional>
  17. #include <system_error>
  18. using namespace llvm;
  19. using namespace object;
  20. int convertForTestingMain(int argc, const char *argv[]) {
  21. sys::PrintStackTraceOnErrorSignal();
  22. PrettyStackTraceProgram X(argc, argv);
  23. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  24. cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
  25. cl::desc("<Source file>"));
  26. cl::opt<std::string> OutputFilename(
  27. "o", cl::Required,
  28. cl::desc(
  29. "File with the profile data obtained after an instrumented run"));
  30. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  31. auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
  32. if (auto Err = ObjErr.getError()) {
  33. errs() << "error: " << Err.message() << "\n";
  34. return 1;
  35. }
  36. ObjectFile *OF = ObjErr.get().getBinary();
  37. auto BytesInAddress = OF->getBytesInAddress();
  38. if (BytesInAddress != 8) {
  39. errs() << "error: 64 bit binary expected\n";
  40. return 1;
  41. }
  42. // Look for the sections that we are interested in.
  43. int FoundSectionCount = 0;
  44. SectionRef ProfileNames, CoverageMapping;
  45. for (const auto &Section : OF->sections()) {
  46. StringRef Name;
  47. if (Section.getName(Name))
  48. return 1;
  49. if (Name == "__llvm_prf_names") {
  50. ProfileNames = Section;
  51. } else if (Name == "__llvm_covmap") {
  52. CoverageMapping = Section;
  53. } else
  54. continue;
  55. ++FoundSectionCount;
  56. }
  57. if (FoundSectionCount != 2)
  58. return 1;
  59. // Get the contents of the given sections.
  60. uint64_t ProfileNamesAddress = ProfileNames.getAddress();
  61. StringRef CoverageMappingData;
  62. StringRef ProfileNamesData;
  63. if (CoverageMapping.getContents(CoverageMappingData) ||
  64. ProfileNames.getContents(ProfileNamesData))
  65. return 1;
  66. int FD;
  67. if (auto Err =
  68. sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) {
  69. errs() << "error: " << Err.message() << "\n";
  70. return 1;
  71. }
  72. raw_fd_ostream OS(FD, true);
  73. OS << "llvmcovmtestdata";
  74. encodeULEB128(ProfileNamesData.size(), OS);
  75. encodeULEB128(ProfileNamesAddress, OS);
  76. OS << ProfileNamesData << CoverageMappingData;
  77. return 0;
  78. }