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 19 using namespace llvm; 20 using namespace llvm::object; 21 using namespace llvm::objdump; 22 23 /// Get the printable name of the image kind. 24 static 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 41 static void printBinary(const OffloadBinary &OB, uint64_t Index) { 42 outs() << "\nOFFLOADING IMAGE [" << Index << "]:\n"; 43 outs() << left_justify("kind", 16) << getImageName(OB) << "\n"; 44 outs() << left_justify("arch", 16) << OB.getArch() << "\n"; 45 outs() << left_justify("triple", 16) << OB.getTriple() << "\n"; 46 outs() << left_justify("producer", 16) 47 << getOffloadKindName(OB.getOffloadKind()) << "\n"; 48 } 49 50 /// Print the embedded offloading contents of an ObjectFile \p O. 51 void llvm::dumpOffloadBinary(const ObjectFile &O) { 52 if (!O.isELF()) { 53 reportWarning("--offloading is currently only supported for ELF targets", 54 O.getFileName()); 55 return; 56 } 57 58 SmallVector<OffloadFile> Binaries; 59 if (Error Err = extractOffloadBinaries(O.getMemoryBufferRef(), Binaries)) 60 reportError(O.getFileName(), "while extracting offloading files: " + 61 toString(std::move(Err))); 62 63 // Print out all the binaries that are contained in this buffer. 64 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I) 65 printBinary(*Binaries[I].getBinary(), I); 66 } 67 68 /// Print the contents of an offload binary file \p OB. This may contain 69 /// multiple binaries stored in the same buffer. 70 void llvm::dumpOffloadSections(const OffloadBinary &OB) { 71 SmallVector<OffloadFile> Binaries; 72 if (Error Err = extractOffloadBinaries(OB.getMemoryBufferRef(), Binaries)) 73 reportError(OB.getFileName(), "while extracting offloading files: " + 74 toString(std::move(Err))); 75 76 // Print out all the binaries that are contained in this buffer. 77 for (uint64_t I = 0, E = Binaries.size(); I != E; ++I) 78 printBinary(*Binaries[I].getBinary(), I); 79 } 80