This series follows Jakub's suggestion in the PR (for Linux in the first patch) and handles Darwin-specific cases (in the second).
Sorry this has taken a while, the Darwin permutations had some glitches which necessitated re-tests on several OS versions. Tested on x86_64 (and aarch64) Darwin, x86_64, aarch64 Linux. OK for trunk for patch 1 and the non-Darwin parts of patch2. thanks, Iain