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