[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.
This commit is contained in:
Joseph Huber 2022-07-01 23:24:22 -04:00
parent 5744b9cb79
commit ccf7dd5e81
3 changed files with 7 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include "llvm/BinaryFormat/Magic.h" #include "llvm/BinaryFormat/Magic.h"
#include "llvm/MC/StringTableBuilder.h" #include "llvm/MC/StringTableBuilder.h"
#include "llvm/Object/Error.h" #include "llvm/Object/Error.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/FileOutputBuffer.h"
using namespace llvm; using namespace llvm;
@ -26,6 +27,10 @@ OffloadBinary::create(MemoryBufferRef Buf) {
if (identify_magic(Buf.getBuffer()) != file_magic::offload_binary) if (identify_magic(Buf.getBuffer()) != file_magic::offload_binary)
return errorCodeToError(object_error::parse_failed); 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 char *Start = Buf.getBufferStart();
const Header *TheHeader = reinterpret_cast<const Header *>(Start); const Header *TheHeader = reinterpret_cast<const Header *>(Start);
if (TheHeader->Version != OffloadBinary::Version) if (TheHeader->Version != OffloadBinary::Version)

View File

@ -5,6 +5,7 @@
## Check that we can dump an offloading binary inside of an ELF section. ## Check that we can dump an offloading binary inside of an ELF section.
# RUN: yaml2obj %s -o %t.elf # RUN: yaml2obj %s -o %t.elf
# RUN: llvm-objcopy --add-section .llvm.offloading=%t.bin %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={{.}} # RUN: llvm-objdump --offloading %t.elf | FileCheck %s --check-prefixes=CHECK,ELF --match-full-lines --strict-whitespace --implicit-check-not={{.}}
!ELF !ELF

View File

@ -4,6 +4,7 @@
# RUN: cat %t-bad.bin >> %t-good.bin # RUN: cat %t-bad.bin >> %t-good.bin
# RUN: yaml2obj %s -o %t.elf # RUN: yaml2obj %s -o %t.elf
# RUN: llvm-objcopy --add-section .llvm.offloading=%t-good.bin %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 # RUN: llvm-objdump --offloading %t.elf 2>&1 | FileCheck %s -DFILENAME=%t.elf
!ELF !ELF