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
9#include "llvm/Object/COFF.h"
10#include "llvm/Object/ObjectFile.h"
11#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
12#include "llvm/ProfileData/InstrProf.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/raw_ostream.h"
16#include <system_error>
17
18using namespace llvm;
19using namespace object;
20
21int convertForTestingMain(int argc, const char *argv[]) {
22 cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
23 cl::desc("<Source file>"));
24
25 cl::opt<std::string> OutputFilename(
26 "o", cl::Required,
27 cl::desc(
28 "File with the profile data obtained after an instrumented run"));
29
30 cl::ParseCommandLineOptions(argc, argv, Overview: "LLVM code coverage tool\n");
31
32 auto ObjErr = llvm::object::ObjectFile::createObjectFile(ObjectPath: InputSourceFile);
33 if (!ObjErr) {
34 std::string Buf;
35 raw_string_ostream OS(Buf);
36 logAllUnhandledErrors(E: ObjErr.takeError(), OS);
37 errs() << "error: " << Buf;
38 return 1;
39 }
40 ObjectFile *OF = ObjErr.get().getBinary();
41 auto BytesInAddress = OF->getBytesInAddress();
42 if (BytesInAddress != 8) {
43 errs() << "error: 64 bit binary expected\n";
44 return 1;
45 }
46
47 // Look for the sections that we are interested in.
48 int FoundSectionCount = 0;
49 SectionRef ProfileNames, CoverageMapping, CoverageRecords;
50 auto ObjFormat = OF->getTripleObjectFormat();
51
52 auto ProfileNamesSection = getInstrProfSectionName(IPSK: IPSK_name, OF: ObjFormat,
53 /*AddSegmentInfo=*/false);
54 auto CoverageMappingSection =
55 getInstrProfSectionName(IPSK: IPSK_covmap, OF: ObjFormat, /*AddSegmentInfo=*/false);
56 auto CoverageRecordsSection =
57 getInstrProfSectionName(IPSK: IPSK_covfun, OF: ObjFormat, /*AddSegmentInfo=*/false);
58 if (isa<object::COFFObjectFile>(Val: OF)) {
59 // On COFF, the object file section name may end in "$M". This tells the
60 // linker to sort these sections between "$A" and "$Z". The linker removes
61 // the dollar and everything after it in the final binary. Do the same to
62 // match.
63 auto Strip = [](std::string &Str) {
64 auto Pos = Str.find(c: '$');
65 if (Pos != std::string::npos)
66 Str.resize(n: Pos);
67 };
68 Strip(ProfileNamesSection);
69 Strip(CoverageMappingSection);
70 Strip(CoverageRecordsSection);
71 }
72
73 for (const auto &Section : OF->sections()) {
74 StringRef Name;
75 if (Expected<StringRef> NameOrErr = Section.getName()) {
76 Name = *NameOrErr;
77 } else {
78 consumeError(Err: NameOrErr.takeError());
79 return 1;
80 }
81
82 if (Name == ProfileNamesSection)
83 ProfileNames = Section;
84 else if (Name == CoverageMappingSection)
85 CoverageMapping = Section;
86 else if (Name == CoverageRecordsSection)
87 CoverageRecords = Section;
88 else
89 continue;
90 ++FoundSectionCount;
91 }
92 if (FoundSectionCount != 3)
93 return 1;
94
95 // Get the contents of the given sections.
96 uint64_t ProfileNamesAddress = ProfileNames.getAddress();
97 StringRef CoverageMappingData;
98 StringRef CoverageRecordsData;
99 StringRef ProfileNamesData;
100 if (Expected<StringRef> E = CoverageMapping.getContents())
101 CoverageMappingData = *E;
102 else {
103 consumeError(Err: E.takeError());
104 return 1;
105 }
106 if (Expected<StringRef> E = CoverageRecords.getContents())
107 CoverageRecordsData = *E;
108 else {
109 consumeError(Err: E.takeError());
110 return 1;
111 }
112 if (Expected<StringRef> E = ProfileNames.getContents())
113 ProfileNamesData = *E;
114 else {
115 consumeError(Err: E.takeError());
116 return 1;
117 }
118
119 // If this is a linked PE/COFF file, then we have to skip over the null byte
120 // that is allocated in the .lprfn$A section in the LLVM profiling runtime.
121 if (isa<COFFObjectFile>(Val: OF) && !OF->isRelocatableObject())
122 ProfileNamesData = ProfileNamesData.drop_front(N: 1);
123
124 int FD;
125 if (auto Err = sys::fs::openFileForWrite(Name: OutputFilename, ResultFD&: FD)) {
126 errs() << "error: " << Err.message() << "\n";
127 return 1;
128 }
129
130 coverage::TestingFormatWriter Writer(ProfileNamesAddress, ProfileNamesData,
131 CoverageMappingData,
132 CoverageRecordsData);
133 raw_fd_ostream OS(FD, true);
134 Writer.write(OS);
135
136 return 0;
137}
138