mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(relinker): include the run path in the Linux DT_STRSZ
The Linux patcher appends the DT_RUNPATH string after the copied .dynstr data but kept DT_STRSZ and the .dynstr section size at the size of the copied data, so DT_RUNPATH pointed at the first byte past the declared string table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -204,6 +204,8 @@ if(BUILD_TESTING)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_tls_diagnostics.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME linux_load_alignment COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_load_alignment.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME linux_dynamic_strings COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_dynamic_strings.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME executable_segments COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_executable_segments.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME linux_entry_argv COMMAND ${Python3_EXECUTABLE}
|
||||
|
||||
@@ -101,6 +101,7 @@ std::vector<std::uint8_t> LinuxElfPatcher::Patch(
|
||||
for (char c : runPath)
|
||||
buf.push_back(static_cast<std::uint8_t>(c));
|
||||
buf.push_back(0);
|
||||
const std::uint64_t dynStrSize = static_cast<std::uint64_t>(buf.size()) - dynStrOff;
|
||||
alignBuf(buf, kDynStrAlignment);
|
||||
|
||||
const auto dynSymOff = static_cast<std::uint64_t>(buf.size());
|
||||
@@ -131,7 +132,7 @@ std::vector<std::uint8_t> LinuxElfPatcher::Patch(
|
||||
for (std::uint8_t b : dynSection.DynamicSegmentData)
|
||||
dynSegBuf.push_back(b);
|
||||
_appendDynEntry(dynSegBuf, DT_STRTAB, vaddrOfExtraBlockOffset(dynStrOff));
|
||||
_appendDynEntry(dynSegBuf, DT_STRSZ, dynSection.DynStrData.size());
|
||||
_appendDynEntry(dynSegBuf, DT_STRSZ, dynStrSize);
|
||||
_appendDynEntry(dynSegBuf, DT_SYMTAB, vaddrOfExtraBlockOffset(dynSymOff));
|
||||
_appendDynEntry(dynSegBuf, DT_SYMENT, kSymEntrySize);
|
||||
if (!dynSection.RelaData.empty()) {
|
||||
@@ -191,7 +192,7 @@ std::vector<std::uint8_t> LinuxElfPatcher::Patch(
|
||||
|
||||
SectionHeaderTableRequest sectionRequest{};
|
||||
sectionRequest.DynStrOffset = dynStrOff;
|
||||
sectionRequest.DynStrSize = dynSection.DynStrData.size();
|
||||
sectionRequest.DynStrSize = dynStrSize;
|
||||
sectionRequest.DynSymOffset = dynSymOff;
|
||||
sectionRequest.DynSymSize = dynSection.DynSymData.size();
|
||||
sectionRequest.DynamicSegmentOffset = dynSegOff;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Check that DT_STRSZ and the .dynstr section of a Linux relink cover the DT_RUNPATH string."""
|
||||
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from test_linux_load_alignment import fixture
|
||||
|
||||
PT_LOAD = 1
|
||||
PT_DYNAMIC = 2
|
||||
DT_NULL = 0
|
||||
DT_STRTAB = 5
|
||||
DT_STRSZ = 10
|
||||
DT_RUNPATH = 29
|
||||
|
||||
|
||||
def file_offset(elf, segments, vaddr):
|
||||
for segment in segments:
|
||||
if segment[0] == PT_LOAD and segment[3] <= vaddr < segment[3] + segment[5]:
|
||||
return segment[2] + vaddr - segment[3]
|
||||
raise AssertionError(("address is not file-backed", hex(vaddr)))
|
||||
|
||||
|
||||
def dynamic_strings(elf):
|
||||
phoff, = struct.unpack_from("<Q", elf, 0x20)
|
||||
phentsize, phnum = struct.unpack_from("<HH", elf, 0x36)
|
||||
segments = [struct.unpack_from("<IIQQQQQQ", elf, phoff + index * phentsize) for index in range(phnum)]
|
||||
dynamic = next(segment for segment in segments if segment[0] == PT_DYNAMIC)
|
||||
tags = {}
|
||||
for offset in range(dynamic[2], dynamic[2] + dynamic[5], 16):
|
||||
tag, value = struct.unpack_from("<qQ", elf, offset)
|
||||
if tag == DT_NULL:
|
||||
break
|
||||
tags[tag] = value
|
||||
table = file_offset(elf, segments, tags[DT_STRTAB])
|
||||
return tags, elf[table:table + tags[DT_STRSZ]]
|
||||
|
||||
|
||||
def dynstr_section_size(elf):
|
||||
shoff, = struct.unpack_from("<Q", elf, 0x28)
|
||||
shentsize, shnum, shstrndx = struct.unpack_from("<HHH", elf, 0x3A)
|
||||
sections = [struct.unpack_from("<IIQQQQIIQQ", elf, shoff + index * shentsize) for index in range(shnum)]
|
||||
names = sections[shstrndx][4]
|
||||
for section in sections:
|
||||
name = elf[names + section[0]:elf.index(b"\0", names + section[0])]
|
||||
if name == b".dynstr":
|
||||
return section[5]
|
||||
raise AssertionError("missing .dynstr section")
|
||||
|
||||
|
||||
def main():
|
||||
relinker = Path(sys.argv[1]).resolve()
|
||||
with tempfile.TemporaryDirectory(prefix="anyps5-dynstr-") as directory:
|
||||
source = Path(directory) / "input.elf"
|
||||
source.write_bytes(fixture())
|
||||
for name, options, expected in (("default", [], b"$ORIGIN/libs"),
|
||||
("custom", ["--rpath", "/opt/anyps5/lib"], b"/opt/anyps5/lib")):
|
||||
output = Path(directory) / (name + ".elf")
|
||||
result = subprocess.run([str(relinker), "--skip-sce-module", *options, str(source), str(output)],
|
||||
capture_output=True, text=True, timeout=20)
|
||||
if result.returncode != 0:
|
||||
raise AssertionError((name, result.returncode, result.stdout, result.stderr))
|
||||
elf = output.read_bytes()
|
||||
tags, strings = dynamic_strings(elf)
|
||||
run_path = tags[DT_RUNPATH]
|
||||
end = strings.find(b"\0", run_path)
|
||||
if run_path >= len(strings) or end < 0 or strings[run_path:end] != expected:
|
||||
raise AssertionError((name, "DT_RUNPATH is outside DT_STRSZ", run_path, tags[DT_STRSZ]))
|
||||
if dynstr_section_size(elf) != tags[DT_STRSZ]:
|
||||
raise AssertionError((name, ".dynstr size differs from DT_STRSZ", dynstr_section_size(elf), tags[DT_STRSZ]))
|
||||
print("Linux dynamic string tests passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user