blob: aa07a79e78db369e44b3377981272f269799375b [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"
11#include "llvm/Support/raw_ostream.h"
12#include "llvm/Support/LEB128.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/ManagedStatic.h"
15#include "llvm/Support/MemoryObject.h"
16#include "llvm/Support/Signals.h"
17#include "llvm/Support/PrettyStackTrace.h"
18#include <system_error>
19#include <functional>
20
21using namespace llvm;
22using namespace object;
23
Justin Bognerd249a3b2014-10-30 20:57:49 +000024int convertForTestingMain(int argc, const char *argv[]) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000025 sys::PrintStackTraceOnErrorSignal();
26 PrettyStackTraceProgram X(argc, argv);
27 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
28
29 cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
30 cl::desc("<Source file>"));
31
32 cl::opt<std::string> OutputFilename(
33 "o", cl::Required,
34 cl::desc(
35 "File with the profile data obtained after an instrumented run"));
36
37 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
38
39 auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
40 if (auto Err = ObjErr.getError()) {
41 errs() << "error: " << Err.message() << "\n";
42 return 1;
43 }
44 ObjectFile *OF = ObjErr.get().getBinary().get();
45 auto BytesInAddress = OF->getBytesInAddress();
46 if (BytesInAddress != 8) {
47 errs() << "error: 64 bit binary expected\n";
48 return 1;
49 }
50
51 // Look for the sections that we are interested in.
52 int FoundSectionCount = 0;
53 SectionRef ProfileNames, CoverageMapping;
54 for (const auto &Section : OF->sections()) {
55 StringRef Name;
56 if (Section.getName(Name))
57 return 1;
58 if (Name == "__llvm_prf_names") {
59 ProfileNames = Section;
60 } else if (Name == "__llvm_covmap") {
61 CoverageMapping = Section;
62 } else
63 continue;
64 ++FoundSectionCount;
65 }
66 if (FoundSectionCount != 2)
67 return 1;
68
69 // Get the contents of the given sections.
Rafael Espindola80291272014-10-08 15:28:58 +000070 uint64_t ProfileNamesAddress = ProfileNames.getAddress();
Alex Lorenze82d89c2014-08-22 22:56:03 +000071 StringRef CoverageMappingData;
Alex Lorenze82d89c2014-08-22 22:56:03 +000072 StringRef ProfileNamesData;
73 if (CoverageMapping.getContents(CoverageMappingData) ||
Alex Lorenze82d89c2014-08-22 22:56:03 +000074 ProfileNames.getContents(ProfileNamesData))
75 return 1;
76
77 int FD;
78 if (auto Err =
79 sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) {
80 errs() << "error: " << Err.message() << "\n";
81 return 1;
82 }
83
84 raw_fd_ostream OS(FD, true);
85 OS << "llvmcovmtestdata";
86 encodeULEB128(ProfileNamesData.size(), OS);
87 encodeULEB128(ProfileNamesAddress, OS);
88 OS << ProfileNamesData << CoverageMappingData;
89
90 return 0;
91}