fix(relinker): synthesise a non-executable PT_GNU_STACK for Linux executables #7363
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7363"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-18T02:22:59Z.
What
The Linux executable output carries no
PT_GNU_STACK:SegmentFiltertreats every type in[PT_LOOS, PT_HIOS]as SCE-specific and onlyPT_GNU_EH_FRAMEis rescued, and nothing synthesised one. On x86-64 Linux a main executable withoutPT_GNU_STACKis loaded with an executable stack (EXSTACK_DEFAULT), which is weaker than what the guest asked for.After the discussion below, this PR now carries only the
PT_GNU_STACKhalf of #6394:PT_GNU_STACKthe input has is kept (its addresses are 0 andRebaseProgramHeadersalready skips it in the fixed-base path).PF_R | PF_W, zero addresses, the same shapeLinuxGuestModuleWriteralready adds to guest-module output). The synthesis is skipped when no program header slot is available, so tight-slot inputs keep converting and the existing slot-exhaustion messages are unchanged.WriteLayoutandRequiredProgramHeaderCountnow share one predicate — the count required for the layout — so they cannot disagree about the extra slot: the header is written exactly when the count fits the table, and the output table grows one slot earlier when the input has no stack header.PT_GNU_RELROhalf is left to #6406, which guards the lazy-binding GOT and themprotectpage-range cases. This branch no longer touches the RELRO handling.Guest-module output is unchanged (its writer already keeps
PT_GNU_STACK).PT_GNU_PROPERTYand the SCE segment types are still dropped.Three existing tests assumed the old shape and are updated:
test_linux_eh_frame_header.py: exact output-header counts grow by the synthesised stack (6→7, 7→8); also asserts the synthesised header inno-frame-exact-slots.test_linux_input_program_headers.py: the exact-slot and both cases now grow the table one slot earlier, phnum 8→9.Amd64OnlyConverterTests.cppexecutableFileOffset: reade_phofffrom the ELF header instead of assuming the table stayed at offset 64 (the table now relocates one slot earlier).New test
test_linux_gnu_stack.py(registered aslinux_gnu_stack): an inputPT_GNU_STACKsurvives plain and--linux-fixed-baseconversion unchanged, an input without one gets the synthesised non-executable header, and an input without any GNU header gets it while the SCE-specific segments are still dropped.Tested
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), full relinkerctestsuite green (152 tests, including the updated and new ones) on the restructured branch.python3 tools/check_conventions.py --base origin/mainclean.PT_GNU_STACKwas dropped from the output; after the change it survives and an input without one gets the synthesised header.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)PT_GNU_STACKhalf of #6394;PT_GNU_RELROis #6406)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T02:36:05Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/relinker/codegen/tests/Amd64OnlyConverterTests.cppcore/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hppcore/relinker/codegen/tests/Amd64OnlyConverterTests.cppImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T02:53:25Z.
Thanks @plokmklo. We compared this with #6406, which fixes the same
SegmentFilter::ShouldSkipdefect and is in final testing now. The two conflict inSegmentFilter.cpp, and we'd like to take the RELRO part from #6406:PT_GNU_RELROunconditionally breaks two cases that #6406 had to guard. With--lazy-binding, the lazy GOT slots sit inside RELRO, so the first lazy call faults when ld.so writes the slot. And when the kept RELRO extends past the RW LOAD's pages, glibc'smprotect(PROT_READ)fails with ENOMEM and the process exits with 127. That second case is the failure #6406 hit and fixed inc56cafae2.PT_GNU_STACKsynthesis here is new and useful. None of the eboots we checked has aPT_GNU_STACK, so keeping an existing one changes nothing for them, while synthesizing{PF_R|PF_W, align 16}gives them a non-executable stack marking. Thee_phofffix inAmd64OnlyConverterTests.cppis good too.Proposal: once #6406 merges, could you rebase this onto it and keep only the
PT_GNU_STACKsynthesis plus thee_phofftest fix? It adds a program header to every title, so we'll run a full review and the game gates on it. One thing to look at while you're there:RequiredProgramHeaderCountalways reserves one extra header when the input has noPT_GNU_STACK, butWriteLayoutonly synthesizes it whenkept + synthetic < PhNum, so it would be good to make sure the two can't disagree, including with #6406's frame budget.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T03:05:24Z.
One compatibility case to check before treating the RELRO handling as complete: a PS5 eboot I inspected has 697
R_X86_64_JUMP_SLOTtargets inside itsPT_GNU_RELROrange and noDT_BIND_NOW. This PR preserves that range unconditionally. Could you verify that the resulting Linux executable can resolve an initially-unresolved PLT entry on first call, or otherwise explain how lazy binding avoids writing into the RELRO pages? The header-shape tests here do not exercise that runtime path. I have a related open change in #6406 that omits RELRO for lazy-binding inputs based on this layout evidence; I’m mentioning it so the overlapping approaches can be compared, not to ask you to abandon your PR.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T03:05:59Z.
Thanks for the detailed coordination note. I saw it after posting my follow-up, so I repeated the lazy-binding point you had already covered. I agree the two failure cases belong in #6406; the remaining work here is the separate PT_GNU_STACK synthesis and e_phoff test correction once the rebase is ready. I will not add another review request on that same point.
Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-18T03:19:10Z.
Restructured per your proposal: the branch now carries only the
PT_GNU_STACKkeep/synthesis and thee_phofftest correction, withDepends on: #6406for the RELRO half, so theSegmentFilterRELRO handling and its lazy-binding and mprotect guards stay with #6406.For the
RequiredProgramHeaderCount/WriteLayoutdisagreement you flagged: both now derive from one predicate —RequiredProgramHeaderCount(originalHeaders) <= PhNum— so the header is written exactly when the count says it fits, and the slot-exhaustion messages for tight inputs (including the 65536-slot overflow case) are unchanged. The newtest_linux_gnu_stack.pycovers keep-under-fixed-base, synthesis, and SCE-segment dropping; the full relinker suite (152 tests) is green on the restructured branch.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T03:21:38Z.
Thanks for the update. I checked the current PR state, and it still reports head
791985268f997555b4f2be9324c972738813deed; its three changed files areGuestPthreadGuardSize.cpp,GuestSocketPortZero.cpp, andtest_linux_gnu_stack.py. The body also still describes the RELRO implementation and saysDepends on: none. I cannot see the narrowed PT_GNU_STACK/e_phoff change or the #6406 dependency in the published branch yet. Could you push the restructured branch and update the PR body when ready? I will review that exact diff then.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:11:30Z.
Thanks for pushing the restructured head. I checked the current diff and confirmed it now contains only the PT_GNU_STACK keep/synthesis work plus the e_phoff/test updates, with the RELRO handling left to #6406 as described. The shared RequiredProgramHeaderCount predicate also addresses the slot-count/write disagreement I raised. My earlier note was based on the prior published head; that mismatch is resolved. I have not rerun the 152-test suite locally, so I am relying on your reported result for that validation.
Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-18T05:45:55Z.
Thanks for the detailed comparison — agreed on all points. The branch is rebased onto #6406 (
c56cafae2) and slimmed to exactly the two pieces approved; Depends on: #6406. Head:https://github.com/plokmklo/AnyPS5/tree/fix/relinker-gnu-stack-relro.What remains here: (1) PT_GNU_STACK synthesis — when the kept headers contain no PT_GNU_STACK, the layout appends
{PF_R|PF_W, align 16, no mapping}; an existing stack is preserved verbatim. (2) thee_phofffix inexecutableFileOffset(Amd64OnlyConverterTests), which is now load-bearing: the synthesis adds a header to every title, so full input tables move and the stub lookup must follow the outpute_phoff. Everything that duplicated #6406 (unconditional RELRO, the SegmentFilter edits) is dropped, and #6406's lazy-binding and RELRO-coverage guards are untouched.On the RequiredProgramHeaderCount/WriteLayout question: yes, they could disagree. The old layout only synthesized when
kept + 5 < PhNum, so any PhNum at or below the reserved count silently skipped the header the count had reserved — concretely, oncerequired > 65535the uint16 cap skips the table move and the stack vanished without an error. Both entry points now derive from one shared header list, so the reserved count and the written count are the same value by construction: exact-fit tables move and get the stack, tables with a free slot reuse it without moving, and inputs that already have a stack neither reserve nor grow (e_phoffunchanged at 64). With #6406's frame budget, synthesis happens before the EH_FRAME slot check, so RELRO drops and the synthetic stack flow through one count, and space exhaustion still fails loudly.Tests: new converter cases for each boundary (exact fit, one spare, with-stack exact, with-stack one short), layout-builder assertions that
written == required, and end-to-end relinks including executing an output whose stack was synthesized, with RELRO kept. Full relinker ctest suite passes (188/188) on top of #6406. Needs the full review plus a real-title sanity run per the game gates.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T06:04:34Z.
@plokmklo Thanks, the Linux relinker tests all pass on the full CI run for f104d9be0 (amd64_only_converter, program_layout_ranges, linux_gnu_segments, linux_eh_frame_header, linux_input_program_headers and linux_program_header_order), and so does Windows.
The two macOS relinker jobs fail to compile
ProgramLayoutRangeTests.cpp, though (run 39102016538):ElfConstants.hppusesstd::size_tbut only includes<cstdint>. That's been latent on main because every other file includes something that pulls in<cstddef>first; this test includesElfConstants.hppon its first line, and libc++ doesn't providesize_tthrough<cstdint>. Adding#include <cstddef>toElfConstants.hppshould fix it. Could you add that here?Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-18T06:23:39Z.
Independent check of exact head
f104d9be0865230d6b3811e4123e2c0a250ada7a, including the macOS CI blocker and live Linux stack mappings.macOS 27.2 arm64, AppleClang 21.0.0, CMake 4.4.4, Release relinker-only:
cmake --build … --target program_layout_ranges_testsreproduces the reportedElfConstants.hpp:8:23: no type named 'size_t' in namespace 'std'(exit 1).#include <cstddef>before<cstdint>in that header makes the same target build.ctest -R '^program_layout_ranges$' --output-on-failurepasses 1/1. No include-order change to the test, forced include or compiler-flag change is needed. I restored the source after the experiment; this is verification of the proposed fix, not an upstream commit or full macOS-suite claim.Ubuntu 24.04 x86-64, kernel 7.0.0-38-generic, GCC 13.3:
program_layout_ranges,linux_gnu_segments,linux_eh_frame_header,linux_program_header_order,linux_input_program_headers: 5/5 pass.--linux-fixed-base 0x400000mode. All outputs have exactly one GNU_STACK, zero addresses/sizes and alignment 16. Missing input headers synthesize flags 6; supplied flags are preserved./proc/<pid>/maps. The synthesized and preserved flags-6 cases have anrw-pstack; both preserved flags-7 controls haverwxp. All generated inputs remain byte-identical.One qualification to the opening description: on this kernel, changing only the synthesized output header type to PT_NULL still gives
rw-p, in both default/fixed modes. So this host verifies explicit header synthesis/preservation and that executable-stack requests are honored; it does not demonstrate a no-header → executable-stack default. I would qualify that claim by kernel/ABI rather than present the NX improvement as universal for current x86-64 Linux.No title sanity run, Windows execution or macOS guest execution was performed. The generated looping processes were terminated after their mappings were captured.
Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-19T08:46:38Z.
Rebased onto current
main(fb0dc118f) since #6406 merged, and pushed two commits:1.
dd1e4b40d— the<cstddef>include both of you asked for.ProgramLayoutRangeTests.cppincludesElfConstants.hppfirst and AppleClang/libc++ does not providestd::size_tthrough<cstdint>, so both macOS relinker jobs should compile again.2.
df0baf9e3— a real regression your runs surfaced, thank you. With synthesis adding a header to every title, inputs whose original table has no free slot now relocate the table into the extra block.game_patchesthen failed: the verify guard rejected every patch belowe_phoff + e_phnum * e_phentsize, which for a relocated table swallows the whole firstPT_LOAD(fixture: table at0x8108, patch at0x4fff). The guard now rejects overlap with the ELF header ([0, 64)) and the table range itself, instead of everything below the table end.headerRange()'s expectations (reject0x0/0x40/0xaf, accept0xb0) are unchanged, and patches below a start-of-file table behave exactly as before.Tested: Ubuntu 24.04 x86-64, GCC 14.2, relinker-only Release build:
ctest210/210 pass (game_patches,program_layout_ranges,linux_gnu_segments,linux_eh_frame_header,linux_input_program_headers,linux_program_header_orderincluded),tools/check_conventions.py --base origin/mainclean.Head:
df0baf9e3.