TestingSupport.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Alignment.h"
  11. #include "llvm/Support/CommandLine.h"
  12. #include "llvm/Support/FileSystem.h"
  13. #include "llvm/Support/LEB128.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <functional>
  16. #include <system_error>
  17. using namespace llvm;
  18. using namespace object;
  19. int convertForTestingMain(int argc, const char *argv[]) {
  20. cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
  21. cl::desc("<Source file>"));
  22. cl::opt<std::string> OutputFilename(
  23. "o", cl::Required,
  24. cl::desc(
  25. "File with the profile data obtained after an instrumented run"));
  26. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  27. auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
  28. if (!ObjErr) {
  29. std::string Buf;
  30. raw_string_ostream OS(Buf);
  31. logAllUnhandledErrors(ObjErr.takeError(), OS);
  32. OS.flush();
  33. errs() << "error: " << Buf;
  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, CoverageRecords;
  45. auto ObjFormat = OF->getTripleObjectFormat();
  46. for (const auto &Section : OF->sections()) {
  47. StringRef Name;
  48. if (Expected<StringRef> NameOrErr = Section.getName()) {
  49. Name = *NameOrErr;
  50. } else {
  51. consumeError(NameOrErr.takeError());
  52. return 1;
  53. }
  54. if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
  55. /*AddSegmentInfo=*/false)) {
  56. ProfileNames = Section;
  57. } else if (Name == llvm::getInstrProfSectionName(
  58. IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
  59. CoverageMapping = Section;
  60. } else if (Name == llvm::getInstrProfSectionName(
  61. IPSK_covfun, ObjFormat, /*AddSegmentInfo=*/false)) {
  62. CoverageRecords = Section;
  63. } else
  64. continue;
  65. ++FoundSectionCount;
  66. }
  67. if (FoundSectionCount != 3)
  68. return 1;
  69. // Get the contents of the given sections.
  70. uint64_t ProfileNamesAddress = ProfileNames.getAddress();
  71. StringRef CoverageMappingData;
  72. StringRef CoverageRecordsData;
  73. StringRef ProfileNamesData;
  74. if (Expected<StringRef> E = CoverageMapping.getContents())
  75. CoverageMappingData = *E;
  76. else {
  77. consumeError(E.takeError());
  78. return 1;
  79. }
  80. if (Expected<StringRef> E = CoverageRecords.getContents())
  81. CoverageRecordsData = *E;
  82. else {
  83. consumeError(E.takeError());
  84. return 1;
  85. }
  86. if (Expected<StringRef> E = ProfileNames.getContents())
  87. ProfileNamesData = *E;
  88. else {
  89. consumeError(E.takeError());
  90. return 1;
  91. }
  92. int FD;
  93. if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
  94. errs() << "error: " << Err.message() << "\n";
  95. return 1;
  96. }
  97. raw_fd_ostream OS(FD, true);
  98. OS << "llvmcovmtestdata";
  99. encodeULEB128(ProfileNamesData.size(), OS);
  100. encodeULEB128(ProfileNamesAddress, OS);
  101. OS << ProfileNamesData;
  102. // Coverage mapping data is expected to have an alignment of 8.
  103. for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
  104. OS.write(uint8_t(0));
  105. OS << CoverageMappingData;
  106. // Coverage records data is expected to have an alignment of 8.
  107. for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
  108. OS.write(uint8_t(0));
  109. OS << CoverageRecordsData;
  110. return 0;
  111. }