Skip to content

Commit

Permalink
[Binary] Align the image offset in OffloadBinary
Browse files Browse the repository at this point in the history
Summary:
The OffloadBinary wraps around an embedded device image, commonly an ELF
or LLVM BC file. These file formats have alignment requirements for
parsing, so if the image is stored at an un-aligned offset from the
beginning of the file we will be unable to parse the embeded image
without copying the image buffer. This patch adds alignment padding
before the binary image is appended to ensure we can parse the symbolic
file it contains in-place without copying memory.
  • Loading branch information
jhuber6 committed Jun 7, 2022
1 parent d46e7ae commit 9db2f32
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions llvm/lib/Object/OffloadBinary.cpp
Expand Up @@ -48,14 +48,17 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
uint64_t StringEntrySize =
sizeof(StringEntry) * OffloadingData.StringData.size();

// Make sure the image we're wrapping around is aligned as well.
uint64_t BinaryDataSize = alignTo(sizeof(Header) + sizeof(Entry) +
StringEntrySize + StrTab.getSize(),
getAlignment());

// Create the header and fill in the offsets. The entry will be directly
// placed after the header in memory. Align the size to the alignment of the
// header so this can be placed contiguously in a single section.
Header TheHeader;
TheHeader.Size =
alignTo(sizeof(Header) + sizeof(Entry) + StringEntrySize +
OffloadingData.Image.getBufferSize() + StrTab.getSize(),
getAlignment());
TheHeader.Size = alignTo(
BinaryDataSize + OffloadingData.Image.getBufferSize(), getAlignment());
TheHeader.EntryOffset = sizeof(Header);
TheHeader.EntrySize = sizeof(Entry);

Expand All @@ -68,8 +71,7 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
TheEntry.StringOffset = sizeof(Header) + sizeof(Entry);
TheEntry.NumStrings = OffloadingData.StringData.size();

TheEntry.ImageOffset =
sizeof(Header) + sizeof(Entry) + StringEntrySize + StrTab.getSize();
TheEntry.ImageOffset = BinaryDataSize;
TheEntry.ImageSize = OffloadingData.Image.getBufferSize();

SmallVector<char, 1024> Data;
Expand All @@ -83,6 +85,8 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
OS << StringRef(reinterpret_cast<char *>(&Map), sizeof(StringEntry));
}
StrTab.write(OS);
// Add padding to required image alignment.
OS.write_zeros(TheEntry.ImageOffset - OS.tell());
OS << OffloadingData.Image.getBuffer();

// Add final padding to required alignment.
Expand Down

0 comments on commit 9db2f32

Please sign in to comment.