Skip to content

Commit

Permalink
[llvm-objdump] Update offload dumping to use SHT_LLVM_OFFLOADING
Browse files Browse the repository at this point in the history
In order to be more in-line with ELF semantics, a previous patch added
support for a new ELF section type to indicate if a section contains
offloading data. This allows us to now check using this rather than
checking the section name directly. This patch updates the logic to
check the type now instead.

I chose to make this emit a warning if the input is not an ELF-object
file. I could have made the logic fall-back to the section name, but
this offloading in LLVM is currently not supported on any other targets
so it's probably best to emit a warning until we improve support.

Depends on D129052

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D129053
  • Loading branch information
jhuber6 committed Jul 7, 2022
1 parent 1d2ce4d commit 82a0adf
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
8 changes: 6 additions & 2 deletions llvm/test/tools/llvm-objdump/Offloading/binary.test
Expand Up @@ -4,15 +4,19 @@

## Check that we can dump an offloading binary inside of an ELF section.
# RUN: yaml2obj %s -o %t.elf
# RUN: llvm-objcopy --add-section .llvm.offloading=%t.bin %t.elf
# RUN: llvm-objcopy --set-section-alignment .llvm.offloading=8 %t.elf
# RUN: llvm-objcopy --update-section .llvm.offloading=%t.bin %t.elf
# RUN: llvm-objdump --offloading %t.elf | FileCheck %s --check-prefixes=CHECK,ELF --match-full-lines --strict-whitespace --implicit-check-not={{.}}

!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .llvm.offloading
Type: SHT_LLVM_OFFLOADING
Flags: [ SHF_EXCLUDE ]
AddressAlign: 0x0000000000000008

# ELF:{{.*}}file format elf64-unknown
# ELF-EMPTY:
Expand Down
Expand Up @@ -9,7 +9,7 @@ FileHeader:
Type: ET_EXEC
Sections:
- Name: .llvm.offloading
Type: SHT_PROGBITS
Type: SHT_LLVM_OFFLOADING
Flags: [ SHF_EXCLUDE ]
Address: 0x0
ShOffset: 0x99999
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-objdump/Offloading/failure.test
Expand Up @@ -8,7 +8,7 @@ FileHeader:
Type: ET_EXEC
Sections:
- Name: .llvm.offloading
Type: SHT_PROGBITS
Type: SHT_LLVM_OFFLOADING
Flags: [ SHF_EXCLUDE ]
Address: 0x0
AddressAlign: 0x0000000000000008
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/tools/llvm-objdump/Offloading/non-elf.test
@@ -0,0 +1,14 @@
# RUN: yaml2obj %s -o %t
# RUN: llvm-objdump --offloading %t 2>&1 | FileCheck -DFILENAME=%t %s

--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: []
sections:
- Name: .rdata
Characteristics: []
SectionData: 00
symbols:

# CHECK: warning: '[[FILENAME]]': --offloading is currently only supported for ELF targets
8 changes: 6 additions & 2 deletions llvm/test/tools/llvm-objdump/Offloading/warning.test
Expand Up @@ -3,15 +3,19 @@
# RUN: yaml2obj %S/Inputs/malformed.yaml -o %t-bad.bin
# RUN: cat %t-bad.bin >> %t-good.bin
# RUN: yaml2obj %s -o %t.elf
# RUN: llvm-objcopy --add-section .llvm.offloading=%t-good.bin %t.elf
# RUN: llvm-objcopy --set-section-alignment .llvm.offloading=8 %t.elf
# RUN: llvm-objcopy --update-section .llvm.offloading=%t-good.bin %t.elf
# RUN: llvm-objdump --offloading %t.elf 2>&1 | FileCheck %s -DFILENAME=%t.elf

!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .llvm.offloading
Type: SHT_LLVM_OFFLOADING
Flags: [ SHF_EXCLUDE ]
AddressAlign: 0x0000000000000008

# CHECK: OFFLOADING IMAGE [0]:
# CHECK: warning: '[[FILENAME]]': while parsing offloading files: The end of the file was unexpectedly encountered
14 changes: 9 additions & 5 deletions llvm/tools/llvm-objdump/OffloadDump.cpp
Expand Up @@ -12,13 +12,12 @@
//===----------------------------------------------------------------------===//
#include "OffloadDump.h"
#include "llvm-objdump.h"
#include "llvm/Object/ELFObjectFile.h"

using namespace llvm;
using namespace llvm::object;
using namespace llvm::objdump;

constexpr const char OffloadSectionString[] = ".llvm.offloading";

/// Get the printable name of the image kind.
static StringRef getImageName(const OffloadBinary &OB) {
switch (OB.getImageKind()) {
Expand Down Expand Up @@ -66,9 +65,14 @@ static Error visitAllBinaries(const OffloadBinary &OB) {

/// Print the embedded offloading contents of an ObjectFile \p O.
void llvm::dumpOffloadBinary(const ObjectFile &O) {
for (SectionRef Sec : O.sections()) {
Expected<StringRef> Name = Sec.getName();
if (!Name || !Name->startswith(OffloadSectionString))
if (!O.isELF()) {
reportWarning("--offloading is currently only supported for ELF targets",
O.getFileName());
return;
}

for (ELFSectionRef Sec : O.sections()) {
if (Sec.getType() != ELF::SHT_LLVM_OFFLOADING)
continue;

Expected<StringRef> Contents = Sec.getContents();
Expand Down

0 comments on commit 82a0adf

Please sign in to comment.