diff --git a/ChangeLog b/ChangeLog index 84070a4c9..547a82242 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ This file details the changelog of Qiling Framework. +------------------------------------ +[Unreleased] +- Report supported RV64 ISA extensions in Linux AT_HWCAP. + ------------------------------------ [Version 1.4.6]: August 4th, 2023 diff --git a/qiling/loader/elf.py b/qiling/loader/elf.py index a5a8ac6fe..e10746d0e 100644 --- a/qiling/loader/elf.py +++ b/qiling/loader/elf.py @@ -335,6 +335,12 @@ def __push_str(top: int, s: str) -> int: # determine hwcap value by arch properties; if not found default to 0 hwcap = hwcap_values.get((self.ql.arch.type, self.ql.arch.endian, self.ql.arch.bits), 0) + if self.ql.os.type == QL_OS.LINUX and self.ql.arch.type == QL_ARCH.RISCV64: + # Linux RISC-V HWCAP bits for I/M/A/F/D/C have the mask 0x112d. + hwcap = self.ql.arch.regs.misa & 0x112d + # Linux does not advertise F on systems that lack D. + if not hwcap & 0x8: + hwcap &= ~0x20 # setup aux vector auxv_entries = [ diff --git a/tests/test_elf_hwcap_projection.py b/tests/test_elf_hwcap_projection.py new file mode 100644 index 000000000..cc8657042 --- /dev/null +++ b/tests/test_elf_hwcap_projection.py @@ -0,0 +1,129 @@ +import struct +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +from qiling.const import QL_ARCH, QL_ENDIAN, QL_OS +from qiling.loader.elf import AUXV, QlLoaderELF + + +class _Segment: + def __getitem__(self, name): + return { + 'p_vaddr': 0x1000, + 'p_memsz': 0x100, + 'p_filesz': 4, + 'p_flags': 5, + }[name] + + @staticmethod + def data(): + return b'\x13\x00\x00\x00' + + +class _ELF: + def __getitem__(self, name): + return { + 'e_entry': 0x1000, + 'e_phoff': 0x40, + 'e_phentsize': 56, + 'e_phnum': 1, + }[name] + + def iter_segments(self, type=None): + return iter([_Segment()] if type == 'PT_LOAD' else []) + + +class _Memory: + pagesize = 4096 + + def __init__(self): + self.writes = [] + self.bytes = {} + + @staticmethod + def align(value, alignment=4096): + return value & ~(alignment - 1) + + @staticmethod + def align_up(value, alignment=4096): + return (value + alignment - 1) & ~(alignment - 1) + + def map(self, *_args): + pass + + def write(self, address, data): + data = bytes(data) + self.writes.append((address, data)) + for offset, byte in enumerate(data): + self.bytes[address + offset] = byte + + def read(self, address, count): + return bytes(self.bytes.get(address + offset, 0) for offset in range(count)) + + def cstring(self, address): + result = bytearray() + while self.bytes.get(address, 0): + result.append(self.bytes[address]) + address += 1 + return bytes(result) + + +class ElfHwcapProjectionTests(unittest.TestCase): + @staticmethod + def load_auxv(arch_type, misa, os_type=QL_OS.LINUX): + memory = _Memory() + ql = SimpleNamespace( + arch=SimpleNamespace( + type=arch_type, + bits=64, + endian=QL_ENDIAN.EL, + pointersize=8, + regs=SimpleNamespace(arch_sp=0, misa=misa), + ), + mem=memory, + log=SimpleNamespace(debug=lambda *_args: None, exception=lambda *_args: None), + os=SimpleNamespace( + type=os_type, + uid=1000, + euid=1000, + gid=1000, + egid=1000, + entry_point=0, + function_hook=None, + ), + pack=lambda value: int(value).to_bytes(8, 'little'), + env={}, + argv=['guest'], + ) + loader = QlLoaderELF(ql) + loader.path = '/tmp/guest.elf' + loader.images = [] + loader.profile = SimpleNamespace(getint=lambda _name: 0x3000000) + with patch('qiling.loader.elf.FunctionHook', return_value=object()): + loader.load_with_ld(_ELF(), 0x8000, 0x10000, ['guest'], {}) + + stack = next(data for address, data in memory.writes if address == loader.stack_address) + offset = loader.auxv - loader.stack_address + entries = {} + while True: + tag, value = struct.unpack_from('