linux-user/mips64: fix elf_core_copy_regs register layout in core files
PATCH 3/3 linux-user/mips: use tswap32 in elf_core_copy_regs
In reply to Matt Turner--- linux-user/mips/elfload.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
linux-user/mips/elfload.c +7 -7
@@ -135,16 +135,16 @@ const char *get_elf_base_platform(CPUState *cs)void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) {for (int i =1 ; i <ARRAY_SIZE (env->active_tc.gpr); i++) { - r->pt.regs[i] =tswapl (env->active_tc.gpr[i]); + r->pt.regs[i] =tswap32 (env->active_tc.gpr[i]); } r->pt.regs[26 ] =0 ; r->pt.regs[27 ] =0 ; - r->pt.lo =tswapl (env->active_tc.LO[0 ]); - r->pt.hi =tswapl (env->active_tc.HI[0 ]); - r->pt.cp0_epc =tswapl (env->active_tc.PC); - r->pt.cp0_badvaddr =tswapl (env->CP0_BadVAddr); - r->pt.cp0_status =tswapl (env->CP0_Status); - r->pt.cp0_cause =tswapl (env->CP0_Cause); + r->pt.lo =tswap32 (env->active_tc.LO[0 ]); + r->pt.hi =tswap32 (env->active_tc.HI[0 ]); + r->pt.cp0_epc =tswap32 (env->active_tc.PC); + r->pt.cp0_badvaddr =tswap32 (env->CP0_BadVAddr); + r->pt.cp0_status =tswap32 (env->CP0_Status); + r->pt.cp0_cause =tswap32 (env->CP0_Cause); }#endif
Signature · 1 line
2.53.0
PATCH 2/3 linux-user/mips64: fix mipsn32 elf_core_copy_regs entry width
In reply to Matt TurnerFor mipsn32 (TARGET_ABI32=y, TARGET_LONG_BITS=64):
abi_ulong = uint32_t (4 bytes) — for pointers and ABI-sized fields target_ulong = uint64_t (8 bytes) — for general-purpose registers
linux-user/elfload.c allocates target_elf_prstatus using the mips64/target_elf.h definition where target_elf_gregset_t has target_ulong reserved[45] (8 bytes each, 360 bytes total).
However, in linux-user/mips64/elfload.c, #include "target_elf.h" inside the included mips/elfload.c resolves to mips/target_elf.h (compiler searches the file's own directory first), where the union uses abi_ulong reserved[45]. For mipsn32 this gives 4-byte entries (180 bytes), not the 8-byte entries (360 bytes) that elfload.c actually allocated.
Writing via r->reserved[34] therefore lands at byte offset 34*4=136 instead of the correct 34*8=272, silently zeroing the EPC in the core file.
Fix by casting the pointer to target_ulong * so writes always use 8-byte slots and land at the offsets matching the allocated layout.
This does not change behavior for mips64 (N64) where abi_ulong already equals target_ulong (both 8 bytes).
---
v2: Use tswap32/tswap64 instead of tswapl
linux-user/mips64/elfload.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-)
linux-user/mips64/elfload.c +25 -10
@@ -15,16 +15,31 @@*/ void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) { -/* R0 is always 0; r->reserved is zero-initialised by the caller */ +/* +* linux-user/elfload.c allocates target_elf_prstatus using the +* definition from mips64/target_elf.h, where target_elf_gregset_t +* has target_ulong reserved[45] (8 bytes each = 360 bytes total). +* +* But in this compilation unit, "#include target_elf.h" resolved to +* mips/target_elf.h (wrong directory), so our local target_elf_gregset_t +* has abi_ulong reserved[45] which is only 4 bytes each for mipsn32. +* Using r->reserved[i] would write to the wrong offsets for mipsn32. +* +* Cast to target_ulong * to always write 8-byte entries at the correct +* positions, matching the layout that elfload.c allocated. +*/ +target_ulong *regs = (target_ulong *)r; + +/* R0 is always 0; buffer is zero-initialised by the caller */ for (int i =1 ; i <32 ; i++) { - r->reserved[i] =tswap64 (env->active_tc.gpr[i]); + regs[i] =tswap64 (env->active_tc.gpr[i]); } - r->reserved[26 ] =0 ;/* k0 */ - r->reserved[27 ] =0 ;/* k1 */ - r->reserved[32 ] =tswap64 (env->active_tc.LO[0 ]); - r->reserved[33 ] =tswap64 (env->active_tc.HI[0 ]); - r->reserved[34 ] =tswap64 (env->active_tc.PC); - r->reserved[35 ] =tswap64 (env->CP0_BadVAddr); - r->reserved[36 ] =tswap64 (env->CP0_Status); - r->reserved[37 ] =tswap64 (env->CP0_Cause); + regs[26 ] =0 ;/* k0 */ + regs[27 ] =0 ;/* k1 */ + regs[32 ] =tswap64 (env->active_tc.LO[0 ]); + regs[33 ] =tswap64 (env->active_tc.HI[0 ]); + regs[34 ] =tswap64 (env->active_tc.PC); + regs[35 ] =tswap64 (env->CP0_BadVAddr); + regs[36 ] =tswap64 (env->CP0_Status); + regs[37 ] =tswap64 (env->CP0_Cause); }
Signature · 1 line
2.53.0
PATCH 1/3 linux-user/mips64: fix elf_core_copy_regs register layout in core files
mips64/elfload.c uses #include "../mips/elfload.c" to share code. When the compiler processes mips/elfload.c the quoted #include "target_elf.h" resolves relative to the including file's directory, so it picks up mips/target_elf.h instead of mips64/target_elf.h. mips/target_elf.h pulls in mips/target_ptrace.h, whose target_pt_regs has a pad0[6] field before regs[]. As a result elf_core_copy_regs writes:
r->pt.regs[i] -> reserved[6+i] (shifted by 6 from the correct index) r->pt.cp0_epc -> reserved[40] (correct mips64 N64 index is 34)
The Linux kernel and glibc both use the mips64 N64 layout (no pad0): EPC at reserved[34]. Debuggers and libunwind reading the core with N64 constants therefore see a completely wrong register set — EPC points to GP, RA holds the branch target instead of the link address, etc.
Fix by:
- Guarding the mips32 elf_core_copy_regs in mips/elfload.c with #ifndef TARGET_MIPS64 so it is not compiled for mips64/mipsn32 targets.
- Providing a mips64-specific elf_core_copy_regs in mips64/elfload.c that writes directly to r->reserved[i] with the correct N64 indices, bypassing the struct field names that are tainted by the wrong header include.
The mipsn32 (TARGET_ABI_MIPSN32) and mips64el targets are covered by the same mips64/elfload.c and benefit from the same fix.
---
v2: Use tswap32/tswap64 instead of tswapl
linux-user/mips/elfload.c | 2 ++ linux-user/mips64/elfload.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
linux-user/mips/elfload.c +2 -0
@@ -131,6 +131,7 @@ const char *get_elf_base_platform(CPUState *cs)#undef MATCH_PLATFORM_INSN/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ +#ifndef TARGET_MIPS64 void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) {for (int i =1 ; i <ARRAY_SIZE (env->active_tc.gpr); i++) {
@@ -146,3 +147,4 @@ void elf_core_copy_regs(target_elf_gregset_t *r, const CPUMIPSState *env) r->pt.cp0_status =tswapl (env->CP0_Status); r->pt.cp0_cause = tswapl(env->CP0_Cause); } +#endif
linux-user/mips64/elfload.c +29 -0
@@ -1 +1,30 @@#include "../mips/elfload.c" + +/* +* mips/elfload.c defines elf_core_copy_regs guarded by #ifndef TARGET_MIPS64. +* +* We must provide the mips64 version here. We cannot use r->pt.regs[] because +* when mips/elfload.c is #include'd above its "#include "target_elf.h"" resolves +* to mips/target_elf.h (compiler searches the including file's directory first), +* which pulls in mips/target_ptrace.h. That struct has pad0[6] before regs[], +* so r->pt.regs[i] writes to reserved[6+i] — offset by 6 from what the kernel +* and glibc expect for the N64 ABI (EPC at reserved[34], not reserved[40]). +* +* Write directly to reserved[] using the mips64 N64 index layout: +* R0-R31 at reserved[0..31], LO at [32], HI at [33], EPC at [34]. +*/ +void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) +{ +/* R0 is always 0; r->reserved is zero-initialised by the caller */ +for (int i =1 ; i <32 ; i++) { + r->reserved[i] =tswap64 (env->active_tc.gpr[i]); + } + r->reserved[26 ] =0 ;/* k0 */ + r->reserved[27 ] =0 ;/* k1 */ + r->reserved[32 ] =tswap64 (env->active_tc.LO[0 ]); + r->reserved[33 ] =tswap64 (env->active_tc.HI[0 ]); + r->reserved[34 ] =tswap64 (env->active_tc.PC); + r->reserved[35 ] =tswap64 (env->CP0_BadVAddr); + r->reserved[36 ] =tswap64 (env->CP0_Status); + r->reserved[37 ] =tswap64 (env->CP0_Cause); +}
Signature · 1 line
2.53.0
Quoted 23 lines · Matt Turner
mips64/elfload.cuses #include "../mips/elfload.c" to share code. When the compiler processesmips/elfload.cthe quoted #include "target_elf.h" resolves relative to the including file's directory, so it picks upmips/target_elf.hinstead ofmips64/target_elf.h.mips/target_elf.hpulls inmips/target_ptrace.h, whosetarget_pt_regshas a pad0[6] field before regs[]. As a resultelf_core_copy_regswrites:r->pt.regs[i] -> reserved[6+i] (shifted by 6 from the correct index) r->pt.cp0_epc -> reserved[40] (correct mips64 N64 index is 34)The Linux kernel and glibc both use the mips64 N64 layout (no pad0): EPC at reserved[34]. Debuggers and libunwind reading the core with N64 constants therefore see a completely wrong register set — EPC points to GP, RA holds the branch target instead of the link address, etc.
Fix by:
- Guarding the mips32elf_core_copy_regsinmips/elfload.cwith #ifndefTARGET_MIPS64so it is not compiled for mips64/mipsn32 targets.
- Providing a mips64-specificelf_core_copy_regsinmips64/elfload.cthat writes directly to r->reserved[i] with the correct N64 indices, bypassing the struct field names that are tainted by the wrong header include.
Quoted 7 lines
The mipsn32 (
TARGET_ABI_MIPSN32) and mips64el targets are covered by the samemips64/elfload.cand benefit from the same fix.Signed-off-by Matt Turner <[email protected]>---
v2: Use tswap32/tswap64 instead of tswapl
Series added to linux-user-next git tree.
Thanks!
Helge
Quoted 23 lines · Matt Turner
mips64/elfload.cuses #include "../mips/elfload.c" to share code. When the compiler processesmips/elfload.cthe quoted #include "target_elf.h" resolves relative to the including file's directory, so it picks upmips/target_elf.hinstead ofmips64/target_elf.h.mips/target_elf.hpulls inmips/target_ptrace.h, whosetarget_pt_regshas a pad0[6] field before regs[]. As a resultelf_core_copy_regswrites:r->pt.regs[i] -> reserved[6+i] (shifted by 6 from the correct index) r->pt.cp0_epc -> reserved[40] (correct mips64 N64 index is 34)The Linux kernel and glibc both use the mips64 N64 layout (no pad0): EPC at reserved[34]. Debuggers and libunwind reading the core with N64 constants therefore see a completely wrong register set — EPC points to GP, RA holds the branch target instead of the link address, etc.
Fix by:
- Guarding the mips32elf_core_copy_regsinmips/elfload.cwith #ifndefTARGET_MIPS64so it is not compiled for mips64/mipsn32 targets.
- Providing a mips64-specificelf_core_copy_regsinmips64/elfload.cthat writes directly to r->reserved[i] with the correct N64 indices, bypassing the struct field names that are tainted by the wrong header include.
Quoted 2 lines
The mipsn32 (
TARGET_ABI_MIPSN32) and mips64el targets are covered by the samemips64/elfload.cand benefit from the same fix.
This code was different before the linux/elfload.c split into targets - before the split, all this was in single linux/elfload.c file. While this patch should not be needed before the split (eg. for stable-10.0 branch), the following patch is apparently needed still.
Can you verify please, and maybe provide a similar fix for 10.0.x too - provided it's actually needed there?
Thanks,
/mjt
Quoted 61 lines
Signed-off-by Matt Turner <[email protected]>---
v2: Use tswap32/tswap64 instead of tswapllinux-user/mips/elfload.c | 2 ++ linux-user/mips64/elfload.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)linux-user/mips/elfload.c +2
-0@@ -131,6 +131,7 @@ const char *get_elf_base_platform(CPUState *cs)#undef MATCH_PLATFORM_INSN/* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */ +#ifndef TARGET_MIPS64 void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) {for (int i =1 ; i <ARRAY_SIZE (env->active_tc.gpr); i++) {@@ -146,3 +147,4 @@ void elf_core_copy_regs(target_elf_gregset_t *r, const CPUMIPSState *env) r->pt.cp0_status =tswapl (env->CP0_Status); r->pt.cp0_cause = tswapl(env->CP0_Cause); } +#endif linux-user/mips64/elfload.c +29
-0@@ -1 +1,30 @@#include "../mips/elfload.c" + +/* +* mips/elfload.c defines elf_core_copy_regs guarded by #ifndef TARGET_MIPS64. +* +* We must provide the mips64 version here. We cannot use r->pt.regs[] because +* when mips/elfload.c is #include'd above its "#include "target_elf.h"" resolves +* to mips/target_elf.h (compiler searches the including file's directory first), +* which pulls in mips/target_ptrace.h. That struct has pad0[6] before regs[], +* so r->pt.regs[i] writes to reserved[6+i] — offset by 6 from what the kernel +* and glibc expect for the N64 ABI (EPC at reserved[34], not reserved[40]). +* +* Write directly to reserved[] using the mips64 N64 index layout: +* R0-R31 at reserved[0..31], LO at [32], HI at [33], EPC at [34]. +*/ +void elf_core_copy_regs (target_elf_gregset_t *r,const CPUMIPSState *env) +{ +/* R0 is always 0; r->reserved is zero-initialised by the caller */ +for (int i =1 ; i <32 ; i++) { + r->reserved[i] =tswap64 (env->active_tc.gpr[i]); + } + r->reserved[26 ] =0 ;/* k0 */ + r->reserved[27 ] =0 ;/* k1 */ + r->reserved[32 ] =tswap64 (env->active_tc.LO[0 ]); + r->reserved[33 ] =tswap64 (env->active_tc.HI[0 ]); + r->reserved[34 ] =tswap64 (env->active_tc.PC); + r->reserved[35 ] =tswap64 (env->CP0_BadVAddr); + r->reserved[36 ] =tswap64 (env->CP0_Status); + r->reserved[37 ] =tswap64 (env->CP0_Cause); +}
Quoted 28 lines · Michael Tokarev
Quoted 23 lines · Matt Turner
mips64/elfload.cuses #include "../mips/elfload.c" to share code. When the compiler processesmips/elfload.cthe quoted #include "target_elf.h" resolves relative to the including file's directory, so it picks upmips/target_elf.hinstead ofmips64/target_elf.h.mips/target_elf.hpulls inmips/target_ptrace.h, whosetarget_pt_regshas a pad0[6] field before regs[]. As a resultelf_core_copy_regswrites:r->pt.regs[i] -> reserved[6+i] (shifted by 6 from the correct index) r->pt.cp0_epc -> reserved[40] (correct mips64 N64 index is 34)The Linux kernel and glibc both use the mips64 N64 layout (no pad0): EPC at reserved[34]. Debuggers and libunwind reading the core with N64 constants therefore see a completely wrong register set — EPC points to GP, RA holds the branch target instead of the link address, etc.
Fix by:
- Guarding the mips32elf_core_copy_regsinmips/elfload.cwith #ifndefTARGET_MIPS64so it is not compiled for mips64/mipsn32 targets.
- Providing a mips64-specificelf_core_copy_regsinmips64/elfload.cthat writes directly to r->reserved[i] with the correct N64 indices, bypassing the struct field names that are tainted by the wrong header include.Quoted 2 lines
The mipsn32 (
TARGET_ABI_MIPSN32) and mips64el targets are covered by the samemips64/elfload.cand benefit from the same fix.
Quoted 7 lines
This code was different before the
linux/elfload.csplit into targets - before the split, all this was in singlelinux/elfload.cfile. While this patch should not be needed before the split (eg. for stable-10.0 branch), the following patch is apparently needed still.Can you verify please, and maybe provide a similar fix for 10.0.x too - provided it's actually needed there?
I just took a look at this.
10.2 & 11.0: clean cherry-picks.
10.0 & 10.1: not affected by any of the issues fixed in this series. Looks like all the issues were introduced by commit a8081da18d ("linux-user: Move elf_core_copy_regs to mips/elfload.c"). If I'd realized that beforehand I would have inserted a Fixes: tag and saved you the email :)
Thanks,
Matt
Quoted 1 line · Matt Turner
On Wed, May 27, 2026 at 7:06 AM Michael Tokarev <[email protected]> wrote:
...
Quoted 8 lines
Quoted 7 lines
This code was different before the
linux/elfload.csplit into targets - before the split, all this was in singlelinux/elfload.cfile. While this patch should not be needed before the split (eg. for stable-10.0 branch), the following patch is apparently needed still.Can you verify please, and maybe provide a similar fix for 10.0.x too - provided it's actually needed there?
Quoted 7 lines
I just took a look at this.
10.2 & 11.0: clean cherry-picks.
10.0 & 10.1: not affected by any of the issues fixed in this series. Looks like all the issues were introduced by commit
a8081da18d("linux-user: Moveelf_core_copy_regstomips/elfload.c").
Yes, that was the split I mentioned above. I guessed it was the change which introduced the whole issue here, but wanted to be sure I actually understand what's going on correctly, especially the second change.
Quoted 2 lines
If I'd> realized that beforehand I would have inserted a Fixes: tag and saved
you the email :)
Hehe. Yes, I'm a bit annoying at times ;))
(imagine a very good smile here)
Thank you for the clarification!
Best,
/mjt