|
14 | 14 | #include "OffloadDump.h" |
15 | 15 | #include "llvm-objdump.h" |
16 | 16 | #include "llvm/Object/ELFObjectFile.h" |
| 17 | +#include "llvm/Object/OffloadBinary.h" |
17 | 18 | #include "llvm/Support/Alignment.h" |
18 | 19 |
|
19 | 20 | using namespace llvm; |
20 | 21 | using namespace llvm::object; |
21 | 22 | using namespace llvm::objdump; |
22 | 23 |
|
| 24 | +void disassembleObject(llvm::object::ObjectFile *, bool InlineRelocs); |
| 25 | + |
23 | 26 | /// Get the printable name of the image kind. |
24 | 27 | static StringRef getImageName(const OffloadBinary &OB) { |
25 | 28 | switch (OB.getImageKind()) { |
@@ -66,6 +69,55 @@ void llvm::dumpOffloadBinary(const ObjectFile &O) { |
66 | 69 | printBinary(*Binaries[I].getBinary(), I); |
67 | 70 | } |
68 | 71 |
|
| 72 | +// Given an Object file, collect all Bundles of FatBin Binaries |
| 73 | +// and dump them into Code Object files |
| 74 | +// if -d is specified, disassemble the Code Object Files |
| 75 | +// if -arch=-name is specified, only dump the Entries that match the target arch |
| 76 | +void llvm::dumpOffloadBundleFatBinary(const ObjectFile &O, std::string ArchName, |
| 77 | + bool Disassemble) { |
| 78 | + assert((O.isELF() || O.isCOFF()) && "Invalid file type"); |
| 79 | + // Collect all Bundles and their Entries .... |
| 80 | + SmallVector<llvm::object::OffloadBundleFatBin> FoundBundles; |
| 81 | + SmallVector<OffloadBundleEntry> FoundEntries; |
| 82 | + |
| 83 | + if (Error Err = llvm::object::extractOffloadBundleFatBinary(O, FoundBundles)) |
| 84 | + reportError(O.getFileName(), "while extracting offload FatBin bundles: " + |
| 85 | + toString(std::move(Err))); |
| 86 | + |
| 87 | + // Now filter based on if arch-name is specified |
| 88 | + SmallVectorImpl<llvm::object::OffloadBundleFatBin>::iterator BundleIter = |
| 89 | + FoundBundles.begin(); |
| 90 | + for (uint64_t bundle_num = 0; bundle_num < FoundBundles.size(); |
| 91 | + bundle_num++) { |
| 92 | + if (!ArchName.empty()) |
| 93 | + FoundEntries = BundleIter->EntryIDContains(StringRef(ArchName)); |
| 94 | + else |
| 95 | + FoundEntries = BundleIter->getEntries(); |
| 96 | + |
| 97 | + // now we have a list of Found Entries .... dump them |
| 98 | + SmallVectorImpl<OffloadBundleEntry>::iterator FoundIter = |
| 99 | + FoundEntries.begin(); |
| 100 | + for (int64_t entry_num = 0; entry_num < FoundEntries.size(); entry_num++) { |
| 101 | + // create file name for this object file: <source-filename>:<Bundle |
| 102 | + // Number>.<EntryID> |
| 103 | + std::string str = BundleIter->getFileName().str() + ":" + |
| 104 | + itostr(bundle_num) + "." + FoundIter->ID.str(); |
| 105 | + StringRef OutputFilename = StringRef(str); |
| 106 | + if (Error Err = object::extractCodeObject( |
| 107 | + O, FoundIter->Offset, FoundIter->Size, OutputFilename)) |
| 108 | + reportError(O.getFileName(), |
| 109 | + "while extracting offload Bundle Entries: " + |
| 110 | + toString(std::move(Err))); |
| 111 | + |
| 112 | + // TODO: If -d was specified, disasseble the Code Object too |
| 113 | + |
| 114 | + ++FoundIter; |
| 115 | + } // end of for found_entries loop |
| 116 | + |
| 117 | + ++BundleIter; |
| 118 | + } // end of for Bundles loop |
| 119 | +} |
| 120 | + |
69 | 121 | /// Print the contents of an offload binary file \p OB. This may contain |
70 | 122 | /// multiple binaries stored in the same buffer. |
71 | 123 | void llvm::dumpOffloadSections(const OffloadBinary &OB) { |
|
0 commit comments