diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 1b3e069fe..04c6bd510 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -204,6 +204,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_tls_diagnostics.py $) add_test(NAME linux_load_alignment COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_load_alignment.py $) + add_test(NAME linux_dynamic_strings COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_dynamic_strings.py $) add_test(NAME executable_segments COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_executable_segments.py $) add_test(NAME linux_entry_argv COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp b/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp index 90ce2bc28..3e52300dc 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp @@ -101,6 +101,7 @@ std::vector LinuxElfPatcher::Patch( for (char c : runPath) buf.push_back(static_cast(c)); buf.push_back(0); + const std::uint64_t dynStrSize = static_cast(buf.size()) - dynStrOff; alignBuf(buf, kDynStrAlignment); const auto dynSymOff = static_cast(buf.size()); @@ -131,7 +132,7 @@ std::vector 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 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; diff --git a/core/relinker/relinker/tests/test_linux_dynamic_strings.py b/core/relinker/relinker/tests/test_linux_dynamic_strings.py new file mode 100644 index 000000000..82c72cbbe --- /dev/null +++ b/core/relinker/relinker/tests/test_linux_dynamic_strings.py @@ -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("= 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()