authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-25 19:06:38+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-29 10:05:25+02:00
loge33af8e9021199696e413057b57fb86e750a9de0
tree264476aa162eabd04ba072586c5fee40c88b19c3
parent77fc3b88fbdf805e0c2c1fc2647513cee56813a4
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

start: Perform the posixCallMainAndExit() call with jalr on mips.

It's actually important for the ABI that r25 (t9) contains the address of the called function, so that this standard prologue sequence works: lui $2, %hi(_gp_disp) addiu $2, $2, %lo(_gp_disp) addu $gp, $2, $t9 (This is a bit similar to the ToC situation on powerpc that was fixed in 7bc78967b400322a0fc5651f37a1b0428c37fb9d.)

1 files changed, 16 insertions(+), 4 deletions(-)

lib/std/start.zig+16-4
......@@ -329,31 +329,43 @@ fn _start() callconv(.Naked) noreturn {
329329 \\ jsr (%%pc, %%a0)
330330 ,
331331 .mips, .mipsel =>
332 \\ move $fp, $0
332333 \\ bal 1f
333334 \\ .gpword .
335 \\ .gpword %[posixCallMainAndExit]
334336 \\ 1:
335337 \\ lw $gp, 0($ra)
336338 \\ subu $gp, $ra, $gp
337 \\ move $fp, $0
339 \\ lw $25, 4($ra)
340 \\ addu $25, $25, $gp
338341 \\ move $ra, $0
339342 \\ move $a0, $sp
340343 \\ and $sp, -8
341 \\ j %[posixCallMainAndExit]
344 \\ subu $sp, $sp, 16
345 \\ jalr $25
342346 ,
343347 .mips64, .mips64el =>
348 \\ move $fp, $0
349 // This is needed because early MIPS versions don't support misaligned loads. Without
350 // this directive, the hidden `nop` inserted to fill the delay slot after `bal` would
351 // cause the two doublewords to be aligned to 4 bytes instead of 8.
352 \\ .balign 8
344353 \\ bal 1f
345354 \\ .gpdword .
355 \\ .gpdword %[posixCallMainAndExit]
346356 \\ 1:
347357 // The `gp` register on MIPS serves a similar purpose to `r2` (ToC pointer) on PPC64.
348358 // We need to set it up in order for dynamically-linked / position-independent code to
349359 // work.
350360 \\ ld $gp, 0($ra)
351361 \\ dsubu $gp, $ra, $gp
352 \\ move $fp, $0
362 \\ ld $25, 8($ra)
363 \\ daddu $25, $25, $gp
353364 \\ move $ra, $0
354365 \\ move $a0, $sp
355366 \\ and $sp, -16
356 \\ j %[posixCallMainAndExit]
367 \\ dsubu $sp, $sp, 16
368 \\ jalr $25
357369 ,
358370 .powerpc, .powerpcle =>
359371 // Set up the initial stack frame, and clear the back chain pointer.