Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void SectionHeaderTableBuilder::WriteTable(
for (const char i : kShStrTabBlob)
buf.push_back(static_cast<std::uint8_t>(i));

Io::AlignBuffer(buf, std::size_t{8});
const auto shOff = static_cast<std::uint64_t>(buf.size());
buf.resize(buf.size() + kShdrEntrySize * kSectionHeaderCount, 0);

Expand Down
28 changes: 27 additions & 1 deletion core/relinker/elfpatcher/tests/SectionRangeTests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <elfpatcher/general/SectionHeaderTableBuilder.hpp>
#include <elfpatcher/general/ElfConstants.hpp>
#include <io/ByteWriter.hpp>
#include <io/BufferUtils.hpp>
#include <algorithm>
#include <iostream>
#include <limits>
#include <stdexcept>
Expand Down Expand Up @@ -29,6 +31,30 @@ int main() {
auto empty = valid; empty.StubOffset = 384; empty.StubSize = 0;
bytes = Fixture(); builder.WriteTable(bytes, empty);
Require(Io::ReadU64(bytes, Io::ReadU64(bytes, 40) + 5 * 64 + 16) == 0x1100);
for (std::size_t tail = 0; tail < 8; ++tail) {
bytes = Fixture();
bytes.resize(512 + tail, 0xa5);
const auto original = bytes;
const auto originalSize = bytes.size();
builder.WriteTable(bytes, valid);
const auto alignedTable = Io::ReadU64(bytes, 40);
if (alignedTable % 8 != 0)
throw std::runtime_error("ELF64 section header table is not 8-byte aligned");
const auto stringOffset = Io::ReadU64(bytes, alignedTable + 64 + 24);
const auto stringSize = Io::ReadU64(bytes, alignedTable + 64 + 32);
const auto stringEnd = stringOffset + stringSize;
Require(stringOffset == originalSize && stringSize == sizeof(Elfpatcher::kShStrTabBlob));
Require(stringEnd <= alignedTable && alignedTable - stringEnd < 8);
Require(bytes[stringOffset] == 0 && bytes[stringEnd - 1] == 0);
Require(std::equal(original.begin() + 64, original.end(), bytes.begin() + 64));
Require(std::all_of(bytes.begin() + stringEnd, bytes.begin() + alignedTable,
[](const auto byte) { return byte == 0; }));
Require(bytes.size() == alignedTable + Io::ReadU16(bytes, 60) * 64);
Require(Io::ReadU64(bytes, alignedTable + 2 * 64 + 24) == valid.DynStrOffset);
Require(Io::ReadU64(bytes, alignedTable + 3 * 64 + 24) == valid.DynSymOffset);
Require(Io::ReadU64(bytes, alignedTable + 4 * 64 + 24) == valid.DynamicSegmentOffset);
Require(Io::ReadU64(bytes, alignedTable + 5 * 64 + 24) == valid.StubOffset);
}
for (int kind = 0; kind < 11; ++kind) {
bytes = Fixture(); auto request = valid;
switch (kind) {
Expand All @@ -49,6 +75,6 @@ int main() {
try { builder.WriteTable(bytes, request); } catch (const std::runtime_error&) { rejected = true; }
Require(rejected && bytes == before);
}
std::cout << "13 section range cases passed\n";
std::cout << "21 section range and alignment cases passed\n";
} catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }
}
Loading