108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
import struct
|
|
import unittest
|
|
|
|
from patch_orbis_relocations import (
|
|
ELF_MACHINE_X86_64,
|
|
ORBIS_GOTPCREL,
|
|
STANDARD_GOTPCREL,
|
|
ElfFormatError,
|
|
patch_elf,
|
|
)
|
|
|
|
|
|
def make_elf(relocations: tuple[tuple[int, int, int, int], ...]) -> bytes:
|
|
header_size = 64
|
|
section_size = 64
|
|
rela_offset = header_size + section_size * 2
|
|
rela_data = b"".join(
|
|
struct.pack("<QQq", offset, (symbol << 32) | kind, addend)
|
|
for offset, symbol, kind, addend in relocations
|
|
)
|
|
header = bytearray(header_size)
|
|
header[:16] = b"\x7fELF\x02\x01\x01\x09" + bytes(8)
|
|
struct.pack_into("<HHI", header, 16, 1, ELF_MACHINE_X86_64, 1)
|
|
struct.pack_into("<Q", header, 40, header_size)
|
|
struct.pack_into("<HH", header, 58, section_size, 2)
|
|
null_section = bytes(section_size)
|
|
rela_section = bytearray(section_size)
|
|
struct.pack_into("<I", rela_section, 4, 4)
|
|
struct.pack_into("<QQ", rela_section, 24, rela_offset, len(rela_data))
|
|
struct.pack_into("<Q", rela_section, 56, 24)
|
|
return bytes(header) + null_section + bytes(rela_section) + rela_data
|
|
|
|
|
|
class PatchElfTests(unittest.TestCase):
|
|
def test_rewrites_only_orbis_gotpcrel_types(self) -> None:
|
|
source = make_elf(((0x10, 7, ORBIS_GOTPCREL, -4), (0x28, 11, 2, 8)))
|
|
|
|
patched, count = patch_elf(source)
|
|
|
|
self.assertEqual(count, 1)
|
|
expected = source[:200] + struct.pack("<Q", (7 << 32) | STANDARD_GOTPCREL) + source[208:]
|
|
self.assertEqual(patched, expected)
|
|
first_offset, first_info, first_addend = struct.unpack_from("<QQq", patched, 192)
|
|
second = struct.unpack_from("<QQq", patched, 216)
|
|
self.assertEqual((first_offset, first_info >> 32, first_info & 0xFFFFFFFF, first_addend),
|
|
(0x10, 7, STANDARD_GOTPCREL, -4))
|
|
self.assertEqual(second, (0x28, (11 << 32) | 2, 8))
|
|
self.assertEqual(source, make_elf(((0x10, 7, ORBIS_GOTPCREL, -4),
|
|
(0x28, 11, 2, 8))))
|
|
|
|
def test_is_idempotent(self) -> None:
|
|
source = make_elf(((0x10, 7, ORBIS_GOTPCREL, -4),))
|
|
patched, first_count = patch_elf(source)
|
|
patched_again, second_count = patch_elf(patched)
|
|
self.assertEqual(first_count, 1)
|
|
self.assertEqual(second_count, 0)
|
|
self.assertEqual(patched_again, patched)
|
|
|
|
def test_ignores_type_pattern_outside_relocation_sections(self) -> None:
|
|
source = bytearray(make_elf(tuple()))
|
|
struct.pack_into("<Q", source, 72, (7 << 32) | ORBIS_GOTPCREL)
|
|
patched, count = patch_elf(bytes(source))
|
|
self.assertEqual(count, 0)
|
|
self.assertEqual(patched, bytes(source))
|
|
|
|
def test_rejects_non_elf_input(self) -> None:
|
|
with self.assertRaisesRegex(ElfFormatError, "ELF64"):
|
|
patch_elf(b"not an elf")
|
|
|
|
def test_rejects_big_endian_input(self) -> None:
|
|
source = bytearray(make_elf(tuple()))
|
|
source[5] = 2
|
|
with self.assertRaisesRegex(ElfFormatError, "little-endian"):
|
|
patch_elf(bytes(source))
|
|
|
|
def test_rejects_non_x86_64_input(self) -> None:
|
|
source = bytearray(make_elf(tuple()))
|
|
struct.pack_into("<H", source, 18, 40)
|
|
with self.assertRaisesRegex(ElfFormatError, "x86-64"):
|
|
patch_elf(bytes(source))
|
|
|
|
def test_rejects_non_relocatable_elf(self) -> None:
|
|
source = bytearray(make_elf(tuple()))
|
|
struct.pack_into("<H", source, 16, 3)
|
|
with self.assertRaisesRegex(ElfFormatError, "relocatable"):
|
|
patch_elf(bytes(source))
|
|
|
|
def test_rejects_unsupported_extended_section_count(self) -> None:
|
|
source = bytearray(make_elf(tuple()))
|
|
struct.pack_into("<H", source, 60, 0)
|
|
with self.assertRaisesRegex(ElfFormatError, "extended section count"):
|
|
patch_elf(bytes(source))
|
|
|
|
def test_rejects_invalid_relocation_entry_size(self) -> None:
|
|
source = bytearray(make_elf(((0, 1, ORBIS_GOTPCREL, 0),)))
|
|
struct.pack_into("<Q", source, 64 + 64 + 56, 0)
|
|
with self.assertRaisesRegex(ElfFormatError, "RELA section shape"):
|
|
patch_elf(bytes(source))
|
|
|
|
def test_rejects_truncated_relocation_section(self) -> None:
|
|
source = make_elf(((0, 1, ORBIS_GOTPCREL, 0),))[:-1]
|
|
with self.assertRaisesRegex(ElfFormatError, "outside the file"):
|
|
patch_elf(source)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|