blob: f6c91f85c8418cd01dc20e8743adfbf1e15ac435 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- TestingSupport.cpp - Convert objects files into test files --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Object/ObjectFile.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000011#include "llvm/Support/CommandLine.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/Support/LEB128.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000013#include "llvm/Support/ManagedStatic.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000014#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/Support/Signals.h"
16#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000017#include <functional>
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include <system_error>
Alex Lorenze82d89c2014-08-22 22:56:03 +000019
20using namespace llvm;
21using namespace object;
22
Justin Bognerd249a3b2014-10-30 20:57:49 +000023int convertForTestingMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000024 sys::PrintStackTraceOnErrorSignal();
25 PrettyStackTraceProgram X(argc, argv);
26 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
27
28 cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
29 cl::desc("<Source file>"));
30
31 cl::opt<std::string> OutputFilename(
32 "o", cl::Required,
33 cl::desc(
34 "File with the profile data obtained after an instrumented run"));
35
36 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
37
38 auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000039 if (!ObjErr) {
40 std::string Buf;
41 raw_string_ostream OS(Buf);
42 logAllUnhandledErrors(ObjErr.takeError(), OS, "");
43 OS.flush();
44 errs() << "error: " << Buf;
Alex Lorenze82d89c2014-08-22 22:56:03 +000045 return 1;
46 }
Lang Hamesf04de6e2014-10-31 21:37:49 +000047 ObjectFile *OF = ObjErr.get().getBinary();
Alex Lorenze82d89c2014-08-22 22:56:03 +000048 auto BytesInAddress = OF->getBytesInAddress();
49 if (BytesInAddress != 8) {
50 errs() << "error: 64 bit binary expected\n";
51 return 1;
52 }
53
54 // Look for the sections that we are interested in.
55 int FoundSectionCount = 0;
56 SectionRef ProfileNames, CoverageMapping;
57 for (const auto &Section : OF->sections()) {
58 StringRef Name;
59 if (Section.getName(Name))
60 return 1;
61 if (Name == "__llvm_prf_names") {
62 ProfileNames = Section;
63 } else if (Name == "__llvm_covmap") {
64 CoverageMapping = Section;
65 } else
66 continue;
67 ++FoundSectionCount;
68 }
69 if (FoundSectionCount != 2)
70 return 1;
71
72 // Get the contents of the given sections.
Rafael Espindola80291272014-10-08 15:28:58 +000073 uint64_t ProfileNamesAddress = ProfileNames.getAddress();
Alex Lorenze82d89c2014-08-22 22:56:03 +000074 StringRef CoverageMappingData;
Alex Lorenze82d89c2014-08-22 22:56:03 +000075 StringRef ProfileNamesData;
76 if (CoverageMapping.getContents(CoverageMappingData) ||
Alex Lorenze82d89c2014-08-22 22:56:03 +000077 ProfileNames.getContents(ProfileNamesData))
78 return 1;
79
80 int FD;
81 if (auto Err =
82 sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) {
83 errs() << "error: " << Err.message() << "\n";
84 return 1;
85 }
86
87 raw_fd_ostream OS(FD, true);
88 OS << "llvmcovmtestdata";
89 encodeULEB128(ProfileNamesData.size(), OS);
90 encodeULEB128(ProfileNamesAddress, OS);
91 OS << ProfileNamesData << CoverageMappingData;
92
93 return 0;
94}