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"
13d9903888SChandler Carruth #include "llvm/Support/LEB128.h"
14d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h"
15e82d89ccSAlex Lorenz #include <functional>
16d9903888SChandler Carruth #include <system_error>
17e82d89ccSAlex Lorenz 
18e82d89ccSAlex Lorenz using namespace llvm;
19e82d89ccSAlex Lorenz using namespace object;
20e82d89ccSAlex Lorenz 
21d249a3b3SJustin Bogner int convertForTestingMain(int argc, const char *argv[]) {
22e82d89ccSAlex Lorenz   cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
23e82d89ccSAlex Lorenz                                        cl::desc("<Source file>"));
24e82d89ccSAlex Lorenz 
25e82d89ccSAlex Lorenz   cl::opt<std::string> OutputFilename(
26e82d89ccSAlex Lorenz       "o", cl::Required,
27e82d89ccSAlex Lorenz       cl::desc(
28e82d89ccSAlex Lorenz           "File with the profile data obtained after an instrumented run"));
29e82d89ccSAlex Lorenz 
30e82d89ccSAlex Lorenz   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
31e82d89ccSAlex Lorenz 
32e82d89ccSAlex Lorenz   auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
333fcdf6aeSKevin Enderby   if (!ObjErr) {
343fcdf6aeSKevin Enderby     std::string Buf;
353fcdf6aeSKevin Enderby     raw_string_ostream OS(Buf);
3645eb84f3SJonas Devlieghere     logAllUnhandledErrors(ObjErr.takeError(), OS);
373fcdf6aeSKevin Enderby     OS.flush();
383fcdf6aeSKevin Enderby     errs() << "error: " << Buf;
39e82d89ccSAlex Lorenz     return 1;
40e82d89ccSAlex Lorenz   }
41f04de6ecSLang Hames   ObjectFile *OF = ObjErr.get().getBinary();
42e82d89ccSAlex Lorenz   auto BytesInAddress = OF->getBytesInAddress();
43e82d89ccSAlex Lorenz   if (BytesInAddress != 8) {
44e82d89ccSAlex Lorenz     errs() << "error: 64 bit binary expected\n";
45e82d89ccSAlex Lorenz     return 1;
46e82d89ccSAlex Lorenz   }
47e82d89ccSAlex Lorenz 
48e82d89ccSAlex Lorenz   // Look for the sections that we are interested in.
49e82d89ccSAlex Lorenz   int FoundSectionCount = 0;
50e82d89ccSAlex Lorenz   SectionRef ProfileNames, CoverageMapping;
511a6a2b64SVedant Kumar   auto ObjFormat = OF->getTripleObjectFormat();
52e82d89ccSAlex Lorenz   for (const auto &Section : OF->sections()) {
53e82d89ccSAlex Lorenz     StringRef Name;
54bcc00e1aSGeorge Rimar     if (Expected<StringRef> NameOrErr = Section.getName()) {
55bcc00e1aSGeorge Rimar       Name = *NameOrErr;
56bcc00e1aSGeorge Rimar     } else {
57bcc00e1aSGeorge Rimar       consumeError(NameOrErr.takeError());
58e82d89ccSAlex Lorenz       return 1;
59bcc00e1aSGeorge Rimar     }
60bcc00e1aSGeorge Rimar 
611a6a2b64SVedant Kumar     if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
621a6a2b64SVedant Kumar                                               /*AddSegmentInfo=*/false)) {
63e82d89ccSAlex Lorenz       ProfileNames = Section;
641a6a2b64SVedant Kumar     } else if (Name == llvm::getInstrProfSectionName(
651a6a2b64SVedant Kumar                            IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
66e82d89ccSAlex Lorenz       CoverageMapping = Section;
67e82d89ccSAlex Lorenz     } else
68e82d89ccSAlex Lorenz       continue;
69e82d89ccSAlex Lorenz     ++FoundSectionCount;
70e82d89ccSAlex Lorenz   }
71e82d89ccSAlex Lorenz   if (FoundSectionCount != 2)
72e82d89ccSAlex Lorenz     return 1;
73e82d89ccSAlex Lorenz 
74e82d89ccSAlex Lorenz   // Get the contents of the given sections.
7580291274SRafael Espindola   uint64_t ProfileNamesAddress = ProfileNames.getAddress();
76e82d89ccSAlex Lorenz   StringRef CoverageMappingData;
77e82d89ccSAlex Lorenz   StringRef ProfileNamesData;
78e183340cSFangrui Song   if (Expected<StringRef> E = CoverageMapping.getContents())
79e183340cSFangrui Song     CoverageMappingData = *E;
80e183340cSFangrui Song   else {
81e183340cSFangrui Song     consumeError(E.takeError());
82e82d89ccSAlex Lorenz     return 1;
83e183340cSFangrui Song   }
84e183340cSFangrui Song   if (Expected<StringRef> E = ProfileNames.getContents())
85e183340cSFangrui Song     ProfileNamesData = *E;
86e183340cSFangrui Song   else {
87e183340cSFangrui Song     consumeError(E.takeError());
88e183340cSFangrui Song     return 1;
89e183340cSFangrui Song   }
90e82d89ccSAlex Lorenz 
91e82d89ccSAlex Lorenz   int FD;
921f67a3cbSZachary Turner   if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
93e82d89ccSAlex Lorenz     errs() << "error: " << Err.message() << "\n";
94e82d89ccSAlex Lorenz     return 1;
95e82d89ccSAlex Lorenz   }
96e82d89ccSAlex Lorenz 
97e82d89ccSAlex Lorenz   raw_fd_ostream OS(FD, true);
98e82d89ccSAlex Lorenz   OS << "llvmcovmtestdata";
99e82d89ccSAlex Lorenz   encodeULEB128(ProfileNamesData.size(), OS);
100e82d89ccSAlex Lorenz   encodeULEB128(ProfileNamesAddress, OS);
101eb103073SIgor Kudrin   OS << ProfileNamesData;
102eb103073SIgor Kudrin   // Coverage mapping data is expected to have an alignment of 8.
103*18f805a7SGuillaume Chatelet   for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
104eb103073SIgor Kudrin     OS.write(uint8_t(0));
105eb103073SIgor Kudrin   OS << CoverageMappingData;
106e82d89ccSAlex Lorenz 
107e82d89ccSAlex Lorenz   return 0;
108e82d89ccSAlex Lorenz }
109