Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1 | //===- TestingSupport.cpp - Convert objects files into test files --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Object/ObjectFile.h" |
Vedant Kumar | 5c3ff13 | 2016-06-02 17:00:50 +0000 | [diff] [blame] | 10 | #include "llvm/ProfileData/InstrProf.h" |
Guillaume Chatelet | af11cc7 | 2019-09-12 15:20:36 +0000 | [diff] [blame^] | 11 | #include "llvm/Support/Alignment.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 12 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 13 | #include "llvm/Support/LEB128.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 15 | #include <functional> |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 16 | #include <system_error> |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace object; |
| 20 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 21 | int convertForTestingMain(int argc, const char *argv[]) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 22 | cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required, |
| 23 | cl::desc("<Source file>")); |
| 24 | |
| 25 | cl::opt<std::string> OutputFilename( |
| 26 | "o", cl::Required, |
| 27 | cl::desc( |
| 28 | "File with the profile data obtained after an instrumented run")); |
| 29 | |
| 30 | cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); |
| 31 | |
| 32 | auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 33 | if (!ObjErr) { |
| 34 | std::string Buf; |
| 35 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 36 | logAllUnhandledErrors(ObjErr.takeError(), OS); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 37 | OS.flush(); |
| 38 | errs() << "error: " << Buf; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 39 | return 1; |
| 40 | } |
Lang Hames | f04de6e | 2014-10-31 21:37:49 +0000 | [diff] [blame] | 41 | ObjectFile *OF = ObjErr.get().getBinary(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 42 | auto BytesInAddress = OF->getBytesInAddress(); |
| 43 | if (BytesInAddress != 8) { |
| 44 | errs() << "error: 64 bit binary expected\n"; |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | // Look for the sections that we are interested in. |
| 49 | int FoundSectionCount = 0; |
| 50 | SectionRef ProfileNames, CoverageMapping; |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 51 | auto ObjFormat = OF->getTripleObjectFormat(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 52 | for (const auto &Section : OF->sections()) { |
| 53 | StringRef Name; |
George Rimar | bcc00e1 | 2019-08-14 11:10:11 +0000 | [diff] [blame] | 54 | if (Expected<StringRef> NameOrErr = Section.getName()) { |
| 55 | Name = *NameOrErr; |
| 56 | } else { |
| 57 | consumeError(NameOrErr.takeError()); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 58 | return 1; |
George Rimar | bcc00e1 | 2019-08-14 11:10:11 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 61 | if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat, |
| 62 | /*AddSegmentInfo=*/false)) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 63 | ProfileNames = Section; |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 64 | } else if (Name == llvm::getInstrProfSectionName( |
| 65 | IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 66 | CoverageMapping = Section; |
| 67 | } else |
| 68 | continue; |
| 69 | ++FoundSectionCount; |
| 70 | } |
| 71 | if (FoundSectionCount != 2) |
| 72 | return 1; |
| 73 | |
| 74 | // Get the contents of the given sections. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 75 | uint64_t ProfileNamesAddress = ProfileNames.getAddress(); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 76 | StringRef CoverageMappingData; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 77 | StringRef ProfileNamesData; |
Fangrui Song | e183340 | 2019-05-16 13:24:04 +0000 | [diff] [blame] | 78 | if (Expected<StringRef> E = CoverageMapping.getContents()) |
| 79 | CoverageMappingData = *E; |
| 80 | else { |
| 81 | consumeError(E.takeError()); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 82 | return 1; |
Fangrui Song | e183340 | 2019-05-16 13:24:04 +0000 | [diff] [blame] | 83 | } |
| 84 | if (Expected<StringRef> E = ProfileNames.getContents()) |
| 85 | ProfileNamesData = *E; |
| 86 | else { |
| 87 | consumeError(E.takeError()); |
| 88 | return 1; |
| 89 | } |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 90 | |
| 91 | int FD; |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 92 | if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 93 | errs() << "error: " << Err.message() << "\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | raw_fd_ostream OS(FD, true); |
| 98 | OS << "llvmcovmtestdata"; |
| 99 | encodeULEB128(ProfileNamesData.size(), OS); |
| 100 | encodeULEB128(ProfileNamesAddress, OS); |
Igor Kudrin | eb10307 | 2016-05-18 07:43:27 +0000 | [diff] [blame] | 101 | OS << ProfileNamesData; |
| 102 | // Coverage mapping data is expected to have an alignment of 8. |
Guillaume Chatelet | af11cc7 | 2019-09-12 15:20:36 +0000 | [diff] [blame^] | 103 | for (unsigned Pad = offsetToAlignment(OS.tell(), llvm::Align(8)); Pad; --Pad) |
Igor Kudrin | eb10307 | 2016-05-18 07:43:27 +0000 | [diff] [blame] | 104 | OS.write(uint8_t(0)); |
| 105 | OS << CoverageMappingData; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |