1//===-- OffloadDump.cpp - Offloading dumper ---------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the offloading-specific dumper for llvm-objdump.
11///
12//===----------------------------------------------------------------------===//
13
14#include "OffloadDump.h"
15#include "llvm-objdump.h"
16#include "llvm/Object/ELFObjectFile.h"
17#include "llvm/Object/OffloadBinary.h"
18#include "llvm/Object/OffloadBundle.h"
19
20using namespace llvm;
21using namespace llvm::object;
22using namespace llvm::objdump;
23
24void disassembleObject(llvm::object::ObjectFile *, bool InlineRelocs);
25
26/// Get the printable name of the image kind.
27static StringRef getImageName(const OffloadBinary &OB) {
28 switch (OB.getImageKind()) {
29 case IMG_Object:
30 return "elf";
31 case IMG_Bitcode:
32 return "llvm ir";
33 case IMG_Cubin:
34 return "cubin";
35 case IMG_Fatbinary:
36 return "fatbinary";
37 case IMG_PTX:
38 return "ptx";
39 default:
40 return "<none>";
41 }
42}
43
44static void printBinary(const OffloadBinary &OB, uint64_t Index) {
45 outs() << "\nOFFLOADING IMAGE [" << Index << "]:\n";
46 outs() << left_justify(Str: "kind", Width: 16) << getImageName(OB) << "\n";
47 outs() << left_justify(Str: "arch", Width: 16) << OB.getArch() << "\n";
48 outs() << left_justify(Str: "triple", Width: 16) << OB.getTriple() << "\n";
49 outs() << left_justify(Str: "producer", Width: 16)
50 << getOffloadKindName(Name: OB.getOffloadKind()) << "\n";
51}
52
53/// Print the embedded offloading contents of an ObjectFile \p O.
54void llvm::dumpOffloadBinary(const ObjectFile &O, StringRef ArchName) {
55 if (!O.isELF() && !O.isCOFF()) {
56 reportWarning(
57 Message: "--offloading is currently only supported for COFF and ELF targets",
58 File: O.getFileName());
59 return;
60 }
61
62 SmallVector<OffloadFile> Binaries;
63 if (Error Err = extractOffloadBinaries(Buffer: O.getMemoryBufferRef(), Binaries))
64 reportError(File: O.getFileName(), Message: "while extracting offloading files: " +
65 toString(E: std::move(Err)));
66
67 // Print out all the binaries that are contained in this buffer.
68 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
69 printBinary(OB: *Binaries[I].getBinary(), Index: I);
70
71 dumpOffloadBundleFatBinary(O, ArchName);
72}
73
74// Given an Object file, collect all Bundles of FatBin Binaries
75// and dump them into Code Object files
76// if -arch=-name is specified, only dump the Entries that match the target arch
77void llvm::dumpOffloadBundleFatBinary(const ObjectFile &O, StringRef ArchName) {
78 if (!O.isELF() && !O.isCOFF()) {
79 reportWarning(
80 Message: "--offloading is currently only supported for COFF and ELF targets",
81 File: O.getFileName());
82 return;
83 }
84
85 SmallVector<llvm::object::OffloadBundleFatBin> FoundBundles;
86
87 if (Error Err = llvm::object::extractOffloadBundleFatBinary(Obj: O, Bundles&: FoundBundles))
88 reportError(File: O.getFileName(), Message: "while extracting offload FatBin bundles: " +
89 toString(E: std::move(Err)));
90 for (const auto &[BundleNum, Bundle] : llvm::enumerate(First&: FoundBundles)) {
91 for (OffloadBundleEntry &Entry : Bundle.getEntries()) {
92 if (!ArchName.empty() && Entry.ID.find(svt: ArchName) != std::string::npos)
93 continue;
94
95 // create file name for this object file: <source-filename>.<Bundle
96 // Number>.<EntryID>
97 std::string str =
98 Bundle.getFileName().str() + "." + itostr(X: BundleNum) + "." + Entry.ID;
99
100 if (Bundle.isDecompressed()) {
101 if (Error Err = object::extractCodeObject(
102 Buffer: Bundle.DecompressedBuffer->getMemBufferRef(), Offset: Entry.Offset,
103 Size: Entry.Size, OutputFileName: StringRef(str)))
104 reportError(File: O.getFileName(),
105 Message: "while extracting offload Bundle Entries: " +
106 toString(E: std::move(Err)));
107 } else {
108 if (Error Err = object::extractCodeObject(Source: O, Offset: Entry.Offset, Size: Entry.Size,
109 OutputFileName: StringRef(str)))
110 reportError(File: O.getFileName(),
111 Message: "while extracting offload Bundle Entries: " +
112 toString(E: std::move(Err)));
113 }
114 outs() << "Extracting offload bundle: " << str << "\n";
115 }
116 }
117}
118
119/// Print the contents of an offload binary file \p OB. This may contain
120/// multiple binaries stored in the same buffer.
121void llvm::dumpOffloadSections(const OffloadBinary &OB) {
122 SmallVector<OffloadFile> Binaries;
123 if (Error Err = extractOffloadBinaries(Buffer: OB.getMemoryBufferRef(), Binaries))
124 reportError(File: OB.getFileName(), Message: "while extracting offloading files: " +
125 toString(E: std::move(Err)));
126
127 // Print out all the binaries that are contained in this buffer.
128 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
129 printBinary(OB: *Binaries[I].getBinary(), Index: I);
130}
131