1e82d89ccSAlex Lorenz //===- TestingSupport.cpp - Convert objects files into test files --------===//
2e82d89ccSAlex Lorenz //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e82d89ccSAlex Lorenz //
7e82d89ccSAlex Lorenz //===----------------------------------------------------------------------===//
8e82d89ccSAlex Lorenz 
9e82d89ccSAlex Lorenz #include "llvm/Object/ObjectFile.h"
105c3ff137SVedant Kumar #include "llvm/ProfileData/InstrProf.h"
11af11cc7eSGuillaume Chatelet #include "llvm/Support/Alignment.h"
12e82d89ccSAlex Lorenz #include "llvm/Support/CommandLine.h"
13ba7a92c0SNico Weber #include "llvm/Support/FileSystem.h"
14d9903888SChandler Carruth #include "llvm/Support/LEB128.h"
15*e72c195fSserge-sans-paille #include "llvm/Support/MemoryBuffer.h"
16d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h"
17e82d89ccSAlex Lorenz #include <functional>
18d9903888SChandler Carruth #include <system_error>
19e82d89ccSAlex Lorenz 
20e82d89ccSAlex Lorenz using namespace llvm;
21e82d89ccSAlex Lorenz using namespace object;
22e82d89ccSAlex Lorenz 
convertForTestingMain(int argc,const char * argv[])23d249a3b3SJustin Bogner int convertForTestingMain(int argc, const char *argv[]) {
24e82d89ccSAlex Lorenz   cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
25e82d89ccSAlex Lorenz                                        cl::desc("<Source file>"));
26e82d89ccSAlex Lorenz 
27e82d89ccSAlex Lorenz   cl::opt<std::string> OutputFilename(
28e82d89ccSAlex Lorenz       "o", cl::Required,
29e82d89ccSAlex Lorenz       cl::desc(
30e82d89ccSAlex Lorenz           "File with the profile data obtained after an instrumented run"));
31e82d89ccSAlex Lorenz 
32e82d89ccSAlex Lorenz   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
33e82d89ccSAlex Lorenz 
34e82d89ccSAlex Lorenz   auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
353fcdf6aeSKevin Enderby   if (!ObjErr) {
363fcdf6aeSKevin Enderby     std::string Buf;
373fcdf6aeSKevin Enderby     raw_string_ostream OS(Buf);
3845eb84f3SJonas Devlieghere     logAllUnhandledErrors(ObjErr.takeError(), OS);
393fcdf6aeSKevin Enderby     OS.flush();
403fcdf6aeSKevin Enderby     errs() << "error: " << Buf;
41e82d89ccSAlex Lorenz     return 1;
42e82d89ccSAlex Lorenz   }
43f04de6ecSLang Hames   ObjectFile *OF = ObjErr.get().getBinary();
44e82d89ccSAlex Lorenz   auto BytesInAddress = OF->getBytesInAddress();
45e82d89ccSAlex Lorenz   if (BytesInAddress != 8) {
46e82d89ccSAlex Lorenz     errs() << "error: 64 bit binary expected\n";
47e82d89ccSAlex Lorenz     return 1;
48e82d89ccSAlex Lorenz   }
49e82d89ccSAlex Lorenz 
50e82d89ccSAlex Lorenz   // Look for the sections that we are interested in.
51e82d89ccSAlex Lorenz   int FoundSectionCount = 0;
52489a3531SPetr Hosek   SectionRef ProfileNames, CoverageMapping, CoverageRecords;
531a6a2b64SVedant Kumar   auto ObjFormat = OF->getTripleObjectFormat();
54e82d89ccSAlex Lorenz   for (const auto &Section : OF->sections()) {
55e82d89ccSAlex Lorenz     StringRef Name;
56bcc00e1aSGeorge Rimar     if (Expected<StringRef> NameOrErr = Section.getName()) {
57bcc00e1aSGeorge Rimar       Name = *NameOrErr;
58bcc00e1aSGeorge Rimar     } else {
59bcc00e1aSGeorge Rimar       consumeError(NameOrErr.takeError());
60e82d89ccSAlex Lorenz       return 1;
61bcc00e1aSGeorge Rimar     }
62bcc00e1aSGeorge Rimar 
631a6a2b64SVedant Kumar     if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
641a6a2b64SVedant Kumar                                               /*AddSegmentInfo=*/false)) {
65e82d89ccSAlex Lorenz       ProfileNames = Section;
661a6a2b64SVedant Kumar     } else if (Name == llvm::getInstrProfSectionName(
671a6a2b64SVedant Kumar                            IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
68e82d89ccSAlex Lorenz       CoverageMapping = Section;
69489a3531SPetr Hosek     } else if (Name == llvm::getInstrProfSectionName(
70489a3531SPetr Hosek                            IPSK_covfun, ObjFormat, /*AddSegmentInfo=*/false)) {
71489a3531SPetr Hosek       CoverageRecords = Section;
72e82d89ccSAlex Lorenz     } else
73e82d89ccSAlex Lorenz       continue;
74e82d89ccSAlex Lorenz     ++FoundSectionCount;
75e82d89ccSAlex Lorenz   }
76489a3531SPetr Hosek   if (FoundSectionCount != 3)
77e82d89ccSAlex Lorenz     return 1;
78e82d89ccSAlex Lorenz 
79e82d89ccSAlex Lorenz   // Get the contents of the given sections.
8080291274SRafael Espindola   uint64_t ProfileNamesAddress = ProfileNames.getAddress();
81e82d89ccSAlex Lorenz   StringRef CoverageMappingData;
82489a3531SPetr Hosek   StringRef CoverageRecordsData;
83e82d89ccSAlex Lorenz   StringRef ProfileNamesData;
84e183340cSFangrui Song   if (Expected<StringRef> E = CoverageMapping.getContents())
85e183340cSFangrui Song     CoverageMappingData = *E;
86e183340cSFangrui Song   else {
87e183340cSFangrui Song     consumeError(E.takeError());
88e82d89ccSAlex Lorenz     return 1;
89e183340cSFangrui Song   }
90489a3531SPetr Hosek   if (Expected<StringRef> E = CoverageRecords.getContents())
91489a3531SPetr Hosek     CoverageRecordsData = *E;
92489a3531SPetr Hosek   else {
93489a3531SPetr Hosek     consumeError(E.takeError());
94489a3531SPetr Hosek     return 1;
95489a3531SPetr Hosek   }
96e183340cSFangrui Song   if (Expected<StringRef> E = ProfileNames.getContents())
97e183340cSFangrui Song     ProfileNamesData = *E;
98e183340cSFangrui Song   else {
99e183340cSFangrui Song     consumeError(E.takeError());
100e183340cSFangrui Song     return 1;
101e183340cSFangrui Song   }
102e82d89ccSAlex Lorenz 
103e82d89ccSAlex Lorenz   int FD;
1041f67a3cbSZachary Turner   if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
105e82d89ccSAlex Lorenz     errs() << "error: " << Err.message() << "\n";
106e82d89ccSAlex Lorenz     return 1;
107e82d89ccSAlex Lorenz   }
108e82d89ccSAlex Lorenz 
109e82d89ccSAlex Lorenz   raw_fd_ostream OS(FD, true);
110e82d89ccSAlex Lorenz   OS << "llvmcovmtestdata";
111e82d89ccSAlex Lorenz   encodeULEB128(ProfileNamesData.size(), OS);
112e82d89ccSAlex Lorenz   encodeULEB128(ProfileNamesAddress, OS);
113eb103073SIgor Kudrin   OS << ProfileNamesData;
114eb103073SIgor Kudrin   // Coverage mapping data is expected to have an alignment of 8.
11518f805a7SGuillaume Chatelet   for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
116eb103073SIgor Kudrin     OS.write(uint8_t(0));
117eb103073SIgor Kudrin   OS << CoverageMappingData;
118489a3531SPetr Hosek   // Coverage records data is expected to have an alignment of 8.
119489a3531SPetr Hosek   for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
120489a3531SPetr Hosek     OS.write(uint8_t(0));
121489a3531SPetr Hosek   OS << CoverageRecordsData;
122e82d89ccSAlex Lorenz 
123e82d89ccSAlex Lorenz   return 0;
124e82d89ccSAlex Lorenz }
125