TestingSupport.cpp 3.3 KB

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