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/Support/Alignment.h"
18
19using namespace llvm;
20using namespace llvm::object;
21using namespace llvm::objdump;
22
23/// Get the printable name of the image kind.
24static StringRef getImageName(const OffloadBinary &OB) {
25 switch (OB.getImageKind()) {
26 case IMG_Object:
27 return "elf";
28 case IMG_Bitcode:
29 return "llvm ir";
30 case IMG_Cubin:
31 return "cubin";
32 case IMG_Fatbinary:
33 return "fatbinary";
34 case IMG_PTX:
35 return "ptx";
36 default:
37 return "<none>";
38 }
39}
40
41static void printBinary(const OffloadBinary &OB, uint64_t Index) {
42 outs() << "\nOFFLOADING IMAGE [" << Index << "]:\n";
43 outs() << left_justify(Str: "kind", Width: 16) << getImageName(OB) << "\n";
44 outs() << left_justify(Str: "arch", Width: 16) << OB.getArch() << "\n";
45 outs() << left_justify(Str: "triple", Width: 16) << OB.getTriple() << "\n";
46 outs() << left_justify(Str: "producer", Width: 16)
47 << getOffloadKindName(Name: OB.getOffloadKind()) << "\n";
48}
49
50/// Print the embedded offloading contents of an ObjectFile \p O.
51void llvm::dumpOffloadBinary(const ObjectFile &O) {
52 if (!O.isELF() && !O.isCOFF()) {
53 reportWarning(
54 Message: "--offloading is currently only supported for COFF and ELF targets",
55 File: O.getFileName());
56 return;
57 }
58
59 SmallVector<OffloadFile> Binaries;
60 if (Error Err = extractOffloadBinaries(Buffer: O.getMemoryBufferRef(), Binaries))
61 reportError(File: O.getFileName(), Message: "while extracting offloading files: " +
62 toString(E: std::move(Err)));
63
64 // Print out all the binaries that are contained in this buffer.
65 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
66 printBinary(OB: *Binaries[I].getBinary(), Index: I);
67}
68
69/// Print the contents of an offload binary file \p OB. This may contain
70/// multiple binaries stored in the same buffer.
71void llvm::dumpOffloadSections(const OffloadBinary &OB) {
72 SmallVector<OffloadFile> Binaries;
73 if (Error Err = extractOffloadBinaries(Buffer: OB.getMemoryBufferRef(), Binaries))
74 reportError(File: OB.getFileName(), Message: "while extracting offloading files: " +
75 toString(E: std::move(Err)));
76
77 // Print out all the binaries that are contained in this buffer.
78 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I)
79 printBinary(OB: *Binaries[I].getBinary(), Index: I);
80}
81