"""Repack the tiny ELF64 probe without changing section data or alignment.""" import hashlib import json import pathlib import struct import sys original_path, packed_path, report_path = map(pathlib.Path, sys.argv[1:]) original = original_path.read_bytes() assert original[:6] == b"\x7fELF\x02\x01" # ELF64, little endian header_format = "<16sHHIQQQIHHHHHH" section_format = "= cut and moved[4] != 0: moved[4] += padding struct.pack_into(section_format, packed, header[6] + i * entry_size, *moved) if section[1] not in (0, 8): # SHT_NULL/SHT_NOBITS have no file payload. assert original[section[4] : section[4] + section[5]] == packed[ moved[4] : moved[4] + moved[5] ], section_name(section) observations.append( { "name": section_name(section), "size": section[5], "alignment": section[8], "originalOffset": section[4], "packedOffset": moved[4], } ) packed_path.write_bytes(packed) report = { "paddingBytes": padding, "allSectionPayloadsUnchanged": True, "symbolsAndRelocationsUnchanged": True, "sha256": { path.name: hashlib.sha256(path.read_bytes()).hexdigest() for path in (original_path, packed_path) }, "sections": observations, } report_path.write_text(json.dumps(report, indent=2) + "\n") print(json.dumps(report, indent=2))