TestingSupport.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===- TestingSupport.cpp - Convert objects files into test files --------===//
  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. #include "llvm/Object/ObjectFile.h"
  9. #include "llvm/ProfileData/InstrProf.h"
  10. #include "llvm/Support/CommandLine.h"
  11. #include "llvm/Support/LEB128.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <functional>
  14. #include <system_error>
  15. using namespace llvm;
  16. using namespace object;
  17. int convertForTestingMain(int argc, const char *argv[]) {
  18. cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
  19. cl::desc("<Source file>"));
  20. cl::opt<std::string> OutputFilename(
  21. "o", cl::Required,
  22. cl::desc(
  23. "File with the profile data obtained after an instrumented run"));
  24. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  25. auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
  26. if (!ObjErr) {
  27. std::string Buf;
  28. raw_string_ostream OS(Buf);
  29. logAllUnhandledErrors(ObjErr.takeError(), OS);
  30. OS.flush();
  31. errs() << "error: " << Buf;
  32. return 1;
  33. }
  34. ObjectFile *OF = ObjErr.get().getBinary();
  35. auto BytesInAddress = OF->getBytesInAddress();
  36. if (BytesInAddress != 8) {
  37. errs() << "error: 64 bit binary expected\n";
  38. return 1;
  39. }
  40. // Look for the sections that we are interested in.
  41. int FoundSectionCount = 0;
  42. SectionRef ProfileNames, CoverageMapping;
  43. auto ObjFormat = OF->getTripleObjectFormat();
  44. for (const auto &Section : OF->sections()) {
  45. StringRef Name;
  46. if (Section.getName(Name))
  47. return 1;
  48. if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
  49. /*AddSegmentInfo=*/false)) {
  50. ProfileNames = Section;
  51. } else if (Name == llvm::getInstrProfSectionName(
  52. IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
  53. CoverageMapping = Section;
  54. } else
  55. continue;
  56. ++FoundSectionCount;
  57. }
  58. if (FoundSectionCount != 2)
  59. return 1;
  60. // Get the contents of the given sections.
  61. uint64_t ProfileNamesAddress = ProfileNames.getAddress();
  62. StringRef CoverageMappingData;
  63. StringRef ProfileNamesData;
  64. if (Expected<StringRef> E = CoverageMapping.getContents())
  65. CoverageMappingData = *E;
  66. else {
  67. consumeError(E.takeError());
  68. return 1;
  69. }
  70. if (Expected<StringRef> E = ProfileNames.getContents())
  71. ProfileNamesData = *E;
  72. else {
  73. consumeError(E.takeError());
  74. return 1;
  75. }
  76. int FD;
  77. if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
  78. errs() << "error: " << Err.message() << "\n";
  79. return 1;
  80. }
  81. raw_fd_ostream OS(FD, true);
  82. OS << "llvmcovmtestdata";
  83. encodeULEB128(ProfileNamesData.size(), OS);
  84. encodeULEB128(ProfileNamesAddress, OS);
  85. OS << ProfileNamesData;
  86. // Coverage mapping data is expected to have an alignment of 8.
  87. for (unsigned Pad = OffsetToAlignment(OS.tell(), 8); Pad; --Pad)
  88. OS.write(uint8_t(0));
  89. OS << CoverageMappingData;
  90. return 0;
  91. }