Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1 | //===- 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 Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 11 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 12 | #include "llvm/Support/LEB128.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ManagedStatic.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 14 | #include "llvm/Support/PrettyStackTrace.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Signals.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 17 | #include <functional> |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include <system_error> |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace object; |
| 22 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 23 | int convertForTestingMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 24 | 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); |
| 39 | if (auto Err = ObjErr.getError()) { |
| 40 | errs() << "error: " << Err.message() << "\n"; |
| 41 | return 1; |
| 42 | } |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 43 | ObjectFile *OF = ObjErr.get().getBinary(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 44 | auto BytesInAddress = OF->getBytesInAddress(); |
| 45 | if (BytesInAddress != 8) { |
| 46 | errs() << "error: 64 bit binary expected\n"; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 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()) { |
| 54 | StringRef Name; |
| 55 | if (Section.getName(Name)) |
| 56 | return 1; |
| 57 | if (Name == "__llvm_prf_names") { |
| 58 | ProfileNames = Section; |
| 59 | } else if (Name == "__llvm_covmap") { |
| 60 | CoverageMapping = Section; |
| 61 | } else |
| 62 | continue; |
| 63 | ++FoundSectionCount; |
| 64 | } |
| 65 | if (FoundSectionCount != 2) |
| 66 | return 1; |
| 67 | |
| 68 | // Get the contents of the given sections. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 69 | uint64_t ProfileNamesAddress = ProfileNames.getAddress(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 70 | StringRef CoverageMappingData; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 71 | StringRef ProfileNamesData; |
| 72 | if (CoverageMapping.getContents(CoverageMappingData) || |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 73 | ProfileNames.getContents(ProfileNamesData)) |
| 74 | return 1; |
| 75 | |
| 76 | int FD; |
| 77 | if (auto Err = |
| 78 | sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) { |
| 79 | errs() << "error: " << Err.message() << "\n"; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | raw_fd_ostream OS(FD, true); |
| 84 | OS << "llvmcovmtestdata"; |
| 85 | encodeULEB128(ProfileNamesData.size(), OS); |
| 86 | encodeULEB128(ProfileNamesAddress, OS); |
| 87 | OS << ProfileNamesData << CoverageMappingData; |
| 88 | |
| 89 | return 0; |
| 90 | } |