TestingSupport.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/MemoryBuffer.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. cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
  22. cl::desc("<Source file>"));
  23. cl::opt<std::string> OutputFilename(
  24. "o", cl::Required,
  25. cl::desc(
  26. "File with the profile data obtained after an instrumented run"));
  27. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  28. auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
  29. if (!ObjErr) {
  30. std::string Buf;
  31. raw_string_ostream OS(Buf);
  32. logAllUnhandledErrors(ObjErr.takeError(), OS);
  33. OS.flush();
  34. errs() << "error: " << Buf;
  35. return 1;
  36. }
  37. ObjectFile *OF = ObjErr.get().getBinary();
  38. auto BytesInAddress = OF->getBytesInAddress();
  39. if (BytesInAddress != 8) {
  40. errs() << "error: 64 bit binary expected\n";
  41. return 1;
  42. }
  43. // Look for the sections that we are interested in.
  44. int FoundSectionCount = 0;
  45. SectionRef ProfileNames, CoverageMapping, CoverageRecords;
  46. auto ObjFormat = OF->getTripleObjectFormat();
  47. for (const auto &Section : OF->sections()) {
  48. StringRef Name;
  49. if (Expected<StringRef> NameOrErr = Section.getName()) {
  50. Name = *NameOrErr;
  51. } else {
  52. consumeError(NameOrErr.takeError());
  53. return 1;
  54. }
  55. if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
  56. /*AddSegmentInfo=*/false)) {
  57. ProfileNames = Section;
  58. } else if (Name == llvm::getInstrProfSectionName(
  59. IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
  60. CoverageMapping = Section;
  61. } else if (Name == llvm::getInstrProfSectionName(
  62. IPSK_covfun, ObjFormat, /*AddSegmentInfo=*/false)) {
  63. CoverageRecords = Section;
  64. } else
  65. continue;
  66. ++FoundSectionCount;
  67. }
  68. if (FoundSectionCount != 3)
  69. return 1;
  70. // Get the contents of the given sections.
  71. uint64_t ProfileNamesAddress = ProfileNames.getAddress();
  72. StringRef CoverageMappingData;
  73. StringRef CoverageRecordsData;
  74. StringRef ProfileNamesData;
  75. if (Expected<StringRef> E = CoverageMapping.getContents())
  76. CoverageMappingData = *E;
  77. else {
  78. consumeError(E.takeError());
  79. return 1;
  80. }
  81. if (Expected<StringRef> E = CoverageRecords.getContents())
  82. CoverageRecordsData = *E;
  83. else {
  84. consumeError(E.takeError());
  85. return 1;
  86. }
  87. if (Expected<StringRef> E = ProfileNames.getContents())
  88. ProfileNamesData = *E;
  89. else {
  90. consumeError(E.takeError());
  91. return 1;
  92. }
  93. int FD;
  94. if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
  95. errs() << "error: " << Err.message() << "\n";
  96. return 1;
  97. }
  98. raw_fd_ostream OS(FD, true);
  99. OS << "llvmcovmtestdata";
  100. encodeULEB128(ProfileNamesData.size(), OS);
  101. encodeULEB128(ProfileNamesAddress, OS);
  102. OS << ProfileNamesData;
  103. // Coverage mapping data is expected to have an alignment of 8.
  104. for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
  105. OS.write(uint8_t(0));
  106. OS << CoverageMappingData;
  107. // Coverage records data is expected to have an alignment of 8.
  108. for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
  109. OS.write(uint8_t(0));
  110. OS << CoverageRecordsData;
  111. return 0;
  112. }