[llvm] ccf7dd5 - [llvm-objdump] Ensure offloading sections have proper alignment

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 1 20:27:06 PDT 2022


Author: Joseph Huber
Date: 2022-07-01T23:26:44-04:00
New Revision: ccf7dd5e813dd066d720cbd7f70c9052a3a17e96

URL: https://github.com/llvm/llvm-project/commit/ccf7dd5e813dd066d720cbd7f70c9052a3a17e96
DIFF: https://github.com/llvm/llvm-project/commit/ccf7dd5e813dd066d720cbd7f70c9052a3a17e96.diff

LOG: [llvm-objdump] Ensure offloading sections have proper alignment

Summary:
A previous patch added support for dumping offloading sections. The
tests for this feature added dummy input to the required section using
`llvm-objcopy`. This binary format has a required alignment of `8` which
was not being respected by the file copied with llvm-objcopy and would
cause failures on architectures sensitive to alignment problems or with
sanitizers. This patch adds the proper alignemnt and adds an error check
at least for the binary format so it's not completely opaque. This
should be improvbed so users actually get a helpful message.

Added: 
    

Modified: 
    llvm/lib/Object/OffloadBinary.cpp
    llvm/test/tools/llvm-objdump/Offloading/binary.test
    llvm/test/tools/llvm-objdump/Offloading/warning.test

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index abdc704c847e6..21946ec2d6fb0 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -12,6 +12,7 @@
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/MC/StringTableBuilder.h"
 #include "llvm/Object/Error.h"
+#include "llvm/Support/Alignment.h"
 #include "llvm/Support/FileOutputBuffer.h"
 
 using namespace llvm;
@@ -26,6 +27,10 @@ OffloadBinary::create(MemoryBufferRef Buf) {
   if (identify_magic(Buf.getBuffer()) != file_magic::offload_binary)
     return errorCodeToError(object_error::parse_failed);
 
+  // Make sure that the data has sufficient alignment.
+  if (!isAddrAligned(Align(getAlignment()), Buf.getBufferStart()))
+    return errorCodeToError(object_error::parse_failed);
+
   const char *Start = Buf.getBufferStart();
   const Header *TheHeader = reinterpret_cast<const Header *>(Start);
   if (TheHeader->Version != OffloadBinary::Version)

diff  --git a/llvm/test/tools/llvm-objdump/Offloading/binary.test b/llvm/test/tools/llvm-objdump/Offloading/binary.test
index 3e2b711057b8c..fab988233c8d9 100644
--- a/llvm/test/tools/llvm-objdump/Offloading/binary.test
+++ b/llvm/test/tools/llvm-objdump/Offloading/binary.test
@@ -5,6 +5,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-objdump --offloading %t.elf | FileCheck %s --check-prefixes=CHECK,ELF --match-full-lines --strict-whitespace --implicit-check-not={{.}}
 
 !ELF

diff  --git a/llvm/test/tools/llvm-objdump/Offloading/warning.test b/llvm/test/tools/llvm-objdump/Offloading/warning.test
index a7ce7db3d7f96..aaf796b5b9e18 100644
--- a/llvm/test/tools/llvm-objdump/Offloading/warning.test
+++ b/llvm/test/tools/llvm-objdump/Offloading/warning.test
@@ -4,6 +4,7 @@
 # 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-objdump --offloading %t.elf 2>&1 | FileCheck %s -DFILENAME=%t.elf
 
 !ELF


        


More information about the llvm-commits mailing list