2014-08-23 00:56:03 +02:00
|
|
|
//===- TestingSupport.cpp - Convert objects files into test files --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-06-02 19:00:50 +02:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/Support/LEB128.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include <functional>
|
2015-01-14 12:23:27 +01:00
|
|
|
#include <system_error>
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-10-30 21:57:49 +01:00
|
|
|
int convertForTestingMain(int argc, const char *argv[]) {
|
2014-08-23 00:56:03 +02:00
|
|
|
cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
|
|
|
|
cl::desc("<Source file>"));
|
|
|
|
|
|
|
|
cl::opt<std::string> OutputFilename(
|
|
|
|
"o", cl::Required,
|
|
|
|
cl::desc(
|
|
|
|
"File with the profile data obtained after an instrumented run"));
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
|
|
|
|
|
|
|
|
auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
|
2016-04-07 00:14:09 +02:00
|
|
|
if (!ObjErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(ObjErr.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
errs() << "error: " << Buf;
|
2014-08-23 00:56:03 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-10-31 22:37:49 +01:00
|
|
|
ObjectFile *OF = ObjErr.get().getBinary();
|
2014-08-23 00:56:03 +02:00
|
|
|
auto BytesInAddress = OF->getBytesInAddress();
|
|
|
|
if (BytesInAddress != 8) {
|
|
|
|
errs() << "error: 64 bit binary expected\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the sections that we are interested in.
|
|
|
|
int FoundSectionCount = 0;
|
|
|
|
SectionRef ProfileNames, CoverageMapping;
|
2017-04-15 02:09:57 +02:00
|
|
|
auto ObjFormat = OF->getTripleObjectFormat();
|
2014-08-23 00:56:03 +02:00
|
|
|
for (const auto &Section : OF->sections()) {
|
|
|
|
StringRef Name;
|
|
|
|
if (Section.getName(Name))
|
|
|
|
return 1;
|
2017-04-15 02:09:57 +02:00
|
|
|
if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
|
|
|
|
/*AddSegmentInfo=*/false)) {
|
2014-08-23 00:56:03 +02:00
|
|
|
ProfileNames = Section;
|
2017-04-15 02:09:57 +02:00
|
|
|
} else if (Name == llvm::getInstrProfSectionName(
|
|
|
|
IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
|
2014-08-23 00:56:03 +02:00
|
|
|
CoverageMapping = Section;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
++FoundSectionCount;
|
|
|
|
}
|
|
|
|
if (FoundSectionCount != 2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Get the contents of the given sections.
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
|
2014-08-23 00:56:03 +02:00
|
|
|
StringRef CoverageMappingData;
|
|
|
|
StringRef ProfileNamesData;
|
|
|
|
if (CoverageMapping.getContents(CoverageMappingData) ||
|
|
|
|
ProfileNames.getContents(ProfileNamesData))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int FD;
|
|
|
|
if (auto Err =
|
|
|
|
sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) {
|
|
|
|
errs() << "error: " << Err.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_fd_ostream OS(FD, true);
|
|
|
|
OS << "llvmcovmtestdata";
|
|
|
|
encodeULEB128(ProfileNamesData.size(), OS);
|
|
|
|
encodeULEB128(ProfileNamesAddress, OS);
|
2016-05-18 09:43:27 +02:00
|
|
|
OS << ProfileNamesData;
|
|
|
|
// Coverage mapping data is expected to have an alignment of 8.
|
|
|
|
for (unsigned Pad = OffsetToAlignment(OS.tell(), 8); Pad; --Pad)
|
|
|
|
OS.write(uint8_t(0));
|
|
|
|
OS << CoverageMappingData;
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|