From 47d202178056fd4ba73e5a35496f94de562bc986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 25 Aug 2021 15:14:41 -0400 Subject: [PATCH] Tools: scripts: Add PIE support in firmware_version_decoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calculate pointer offset for PIE (Position Independent Executables) binaries. Signed-off-by: Patrick José Pereira --- Tools/scripts/firmware_version_decoder.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Tools/scripts/firmware_version_decoder.py b/Tools/scripts/firmware_version_decoder.py index 1ab722e296..5afa2b340e 100755 --- a/Tools/scripts/firmware_version_decoder.py +++ b/Tools/scripts/firmware_version_decoder.py @@ -6,6 +6,7 @@ import sys import struct from argparse import ArgumentParser from dataclasses import dataclass +from elftools.elf.elffile import ELFFile from typing import Any @@ -137,6 +138,7 @@ class Decoder: self.fwversion = FWVersion() self.byteorder = "" self.pointer_size = 0 + self.elffile = None def unpack(self, struct_format: str) -> Any: struct_format = f"{self.byteorder}{struct_format}" @@ -151,6 +153,9 @@ class Decoder: if address == 0: return "" + # Calculate address offset for PIE (Position Independent Executables) binaries + address = next(self.elffile.address_offsets(address)) + current_address = self.bytesio.seek(0, io.SEEK_CUR) self.bytesio.seek(address) string = [] @@ -193,8 +198,10 @@ class Decoder: self.fwversion.os_hash_string = self.unpack_string_from_pointer() def process(self, filename) -> None: - with open(filename, "rb") as file: - data = file.read() + # We need the file open for ELFFile + file = open(filename, "rb") + data = file.read() + self.elffile = ELFFile(file) if not data: raise RuntimeError("Failed to find FWVersion.")