Skip to content

Commit e0fb9f5

Browse files
committed
[llvm-objdump] Fix alignment issues when dumping offloading sections
Summary: The `.llvm.offloading` section should always be aligned by `8`. However, we may want to show the offloading data stored in a static library. In this case, even though the section's alignment is correct, the offset inside the archive will result in the memory buffer being misaligned. TO combat this we simply check if the buffer does not have the proper alignment and copies it to a new buffer if not. This copy should have the proper alignment.
1 parent 22c7a6d commit e0fb9f5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Ensure we can read the contents even if the alignment is bad.
2+
# RUN: yaml2obj %s -o %t.elf
3+
# RUN: yaml2obj %S/Inputs/binary.yaml -o %t.bin
4+
# RUN: llvm-objcopy --update-section .llvm.offloading=%t.bin %t.elf
5+
# RUN: llvm-objdump --offloading %t.elf | FileCheck %s -DFILENAME=%t
6+
7+
!ELF
8+
FileHeader:
9+
Class: ELFCLASS64
10+
Data: ELFDATA2LSB
11+
Type: ET_EXEC
12+
Sections:
13+
- Name: .misaligned
14+
Type: SHT_PROGBITS
15+
AddressAlign: 0x0000000000000001
16+
Content: "41424300"
17+
- Name: .llvm.offloading
18+
Type: SHT_LLVM_OFFLOADING
19+
Flags: [ SHF_EXCLUDE ]
20+
AddressAlign: 0x0000000000000001
21+
22+
# CHECK-NOT: error: '[[FILENAME]]': while extracting offloading files: Invalid data was encountered while parsing the file

llvm/tools/llvm-objdump/OffloadDump.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "OffloadDump.h"
1414
#include "llvm-objdump.h"
1515
#include "llvm/Object/ELFObjectFile.h"
16+
#include "llvm/Support/Alignment.h"
1617

1718
using namespace llvm;
1819
using namespace llvm::object;
@@ -79,8 +80,13 @@ void llvm::dumpOffloadBinary(const ObjectFile &O) {
7980
if (!Contents)
8081
reportError(Contents.takeError(), O.getFileName());
8182

82-
MemoryBufferRef Buffer = MemoryBufferRef(*Contents, O.getFileName());
83-
auto BinaryOrErr = OffloadBinary::create(Buffer);
83+
std::unique_ptr<MemoryBuffer> Buffer =
84+
MemoryBuffer::getMemBuffer(*Contents, O.getFileName(), false);
85+
if (!isAddrAligned(Align(OffloadBinary::getAlignment()),
86+
Buffer->getBufferStart()))
87+
Buffer = MemoryBuffer::getMemBufferCopy(Buffer->getBuffer(),
88+
Buffer->getBufferIdentifier());
89+
auto BinaryOrErr = OffloadBinary::create(*Buffer);
8490
if (!BinaryOrErr)
8591
reportError(O.getFileName(), "while extracting offloading files: " +
8692
toString(BinaryOrErr.takeError()));

0 commit comments

Comments
 (0)