[llvm-objdump] Update offload dumping to use SHT_LLVM_OFFLOADING
authorJoseph Huber <jhuber6@vols.utk.edu>
Sun, 3 Jul 2022 22:47:28 +0000 (18:47 -0400)
committerJoseph Huber <jhuber6@vols.utk.edu>
Thu, 7 Jul 2022 16:20:35 +0000 (12:20 -0400)
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

llvm/test/tools/llvm-objdump/Offloading/binary.test
llvm/test/tools/llvm-objdump/Offloading/content-failure.test
llvm/test/tools/llvm-objdump/Offloading/failure.test
llvm/test/tools/llvm-objdump/Offloading/non-elf.test [new file with mode: 0644]
llvm/test/tools/llvm-objdump/Offloading/warning.test
llvm/tools/llvm-objdump/OffloadDump.cpp

index fab9882..880bab2 100644 (file)
@@ -4,8 +4,7 @@
 
 ## 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
@@ -13,6 +12,11 @@ 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:
index d445226..5089eda 100644 (file)
@@ -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
index 854d8e0..5b4cbdb 100644 (file)
@@ -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
diff --git a/llvm/test/tools/llvm-objdump/Offloading/non-elf.test b/llvm/test/tools/llvm-objdump/Offloading/non-elf.test
new file mode 100644 (file)
index 0000000..955556f
--- /dev/null
@@ -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
index aaf796b..a4be54e 100644 (file)
@@ -3,8 +3,7 @@
 # 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
@@ -12,6 +11,11 @@ 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
index 7d4461f..b3302d4 100644 (file)
 //===----------------------------------------------------------------------===//
 #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()) {
@@ -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();