1 //===- TestingSupport.cpp - Convert objects files into test files --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Object/ObjectFile.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/LEB128.h"
13 #include "llvm/Support/ManagedStatic.h"
14 #include "llvm/Support/PrettyStackTrace.h"
15 #include "llvm/Support/Signals.h"
16 #include "llvm/Support/raw_ostream.h"
18 #include <system_error>
21 using namespace object
;
23 int convertForTestingMain(int argc
, const char *argv
[]) {
24 sys::PrintStackTraceOnErrorSignal();
25 PrettyStackTraceProgram
X(argc
, argv
);
26 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
28 cl::opt
<std::string
> InputSourceFile(cl::Positional
, cl::Required
,
29 cl::desc("<Source file>"));
31 cl::opt
<std::string
> OutputFilename(
34 "File with the profile data obtained after an instrumented run"));
36 cl::ParseCommandLineOptions(argc
, argv
, "LLVM code coverage tool\n");
38 auto ObjErr
= llvm::object::ObjectFile::createObjectFile(InputSourceFile
);
39 if (auto Err
= ObjErr
.getError()) {
40 errs() << "error: " << Err
.message() << "\n";
43 ObjectFile
*OF
= ObjErr
.get().getBinary();
44 auto BytesInAddress
= OF
->getBytesInAddress();
45 if (BytesInAddress
!= 8) {
46 errs() << "error: 64 bit binary expected\n";
50 // Look for the sections that we are interested in.
51 int FoundSectionCount
= 0;
52 SectionRef ProfileNames
, CoverageMapping
;
53 for (const auto &Section
: OF
->sections()) {
55 if (Section
.getName(Name
))
57 if (Name
== "__llvm_prf_names") {
58 ProfileNames
= Section
;
59 } else if (Name
== "__llvm_covmap") {
60 CoverageMapping
= Section
;
65 if (FoundSectionCount
!= 2)
68 // Get the contents of the given sections.
69 uint64_t ProfileNamesAddress
= ProfileNames
.getAddress();
70 StringRef CoverageMappingData
;
71 StringRef ProfileNamesData
;
72 if (CoverageMapping
.getContents(CoverageMappingData
) ||
73 ProfileNames
.getContents(ProfileNamesData
))
78 sys::fs::openFileForWrite(OutputFilename
, FD
, sys::fs::F_None
)) {
79 errs() << "error: " << Err
.message() << "\n";
83 raw_fd_ostream
OS(FD
, true);
84 OS
<< "llvmcovmtestdata";
85 encodeULEB128(ProfileNamesData
.size(), OS
);
86 encodeULEB128(ProfileNamesAddress
, OS
);
87 OS
<< ProfileNamesData
<< CoverageMappingData
;