mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(relinker): reject incomplete RELA entries
This commit is contained in:
@@ -194,6 +194,8 @@ if(BUILD_TESTING)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_offsets.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME dynamic_segment COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_dynamic_segment.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME rela_size COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_rela_size.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME optional_plt COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE}
|
||||
|
||||
@@ -145,6 +145,8 @@ RelinkResult RelinkerPipeline::Relink(const std::vector<std::uint8_t>& sourceElf
|
||||
constexpr std::size_t relaEntSize = 24;
|
||||
if (readAsSize(DT_OS_RELAENT, DT_RELAENT, "DT_RELAENT") != relaEntSize)
|
||||
throw RelinkerException("Unsupported DT_RELAENT value");
|
||||
if (dynRelaSize % relaEntSize != 0)
|
||||
throw RelinkerException("Invalid DT_RELASZ value");
|
||||
|
||||
std::vector<std::pair<std::uint64_t, std::string>> neededLibraryNamesByStrOffset;
|
||||
for (const auto& tag : dynTags)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from test_dynamic_segment import fixture
|
||||
|
||||
|
||||
def main():
|
||||
relinker = Path(sys.argv[1]).resolve()
|
||||
failures = []
|
||||
with tempfile.TemporaryDirectory(prefix="anyps5-rela-size-") as directory:
|
||||
work = Path(directory)
|
||||
for tag in (8, 0x61000031):
|
||||
for size in (0, 1, 23, 24, 25, 47, 48, 0xffffffffffffffff):
|
||||
image = fixture()
|
||||
struct.pack_into("<qQ", image, 0x450, tag, size)
|
||||
struct.pack_into("<QQq", image, 0x718, 0x308, 8, 0x210)
|
||||
source = work / f"{tag:x}-{size}.elf"
|
||||
source.write_bytes(image)
|
||||
for mode in ([], ["--windows"]):
|
||||
output = source.with_suffix(".exe" if mode else ".out")
|
||||
result = subprocess.run([str(relinker), "--skip-sce-module", *mode, str(source), str(output)],
|
||||
capture_output=True, text=True, timeout=20)
|
||||
if size % 24 != 0:
|
||||
valid = result.returncode == 2 and "Invalid DT_RELASZ value" in result.stderr and not output.exists()
|
||||
else:
|
||||
magic = b"MZ" if mode else b"\x7fELF"
|
||||
valid = result.returncode == 0 and output.exists() and output.read_bytes().startswith(magic)
|
||||
if valid and not mode:
|
||||
data = output.read_bytes()
|
||||
header_offset = struct.unpack_from("<Q", data, 32)[0]
|
||||
header_size, header_count = struct.unpack_from("<HH", data, 54)
|
||||
dynamic = next(struct.unpack_from("<IIQQQQQQ", data, header_offset + index * header_size)
|
||||
for index in range(header_count)
|
||||
if struct.unpack_from("<I", data, header_offset + index * header_size)[0] == 2)
|
||||
tags = dict(struct.unpack_from("<QQ", data, offset)
|
||||
for offset in range(dynamic[2], dynamic[2] + dynamic[5], 16))
|
||||
valid = tags.get(8, 0) == size
|
||||
if not valid:
|
||||
failures.append((tag, size, mode, result.returncode, result.stdout, result.stderr))
|
||||
if failures:
|
||||
raise AssertionError(failures)
|
||||
print("RELA size tests passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user