authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-29 12:02:59-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-07-29 12:02:59-07:00
log7342017404db17c3caec82f18ae83292c62c0651
tree5e59b1536c98e93ed875a2e67118097912a2ee8f
parent19e4de135f1947e91800cadf2a6c2d29799ecb9a
parentd633b35f35ddb9dd4db8529346bbb414cf8fbfbd
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #20822 from alexrp/start-mips-fixes

`start`: A handful of MIPS fixes

1 files changed, 35 insertions(+), 6 deletions(-)

lib/std/start.zig+35-6
...@@ -289,17 +289,42 @@ fn _start() callconv(.Naked) noreturn {...@@ -289,17 +289,42 @@ fn _start() callconv(.Naked) noreturn {
289 ,289 ,
290 .mips, .mipsel =>290 .mips, .mipsel =>
291 \\ move $fp, $0291 \\ move $fp, $0
292 \\ bal 1f
293 \\ .gpword .
294 \\ .gpword %[posixCallMainAndExit]
295 \\ 1:
296 \\ lw $gp, 0($ra)
297 \\ subu $gp, $ra, $gp
298 \\ lw $25, 4($ra)
299 \\ addu $25, $25, $gp
292 \\ move $ra, $0300 \\ move $ra, $0
293 \\ move $a0, $sp301 \\ move $a0, $sp
294 \\ and $sp, -8302 \\ and $sp, -8
295 \\ j %[posixCallMainAndExit]303 \\ subu $sp, $sp, 16
304 \\ jalr $25
296 ,305 ,
297 .mips64, .mips64el =>306 .mips64, .mips64el =>
298 \\ move $fp, $0307 \\ move $fp, $0
308 // This is needed because early MIPS versions don't support misaligned loads. Without
309 // this directive, the hidden `nop` inserted to fill the delay slot after `bal` would
310 // cause the two doublewords to be aligned to 4 bytes instead of 8.
311 \\ .balign 8
312 \\ bal 1f
313 \\ .gpdword .
314 \\ .gpdword %[posixCallMainAndExit]
315 \\ 1:
316 // The `gp` register on MIPS serves a similar purpose to `r2` (ToC pointer) on PPC64.
317 // We need to set it up in order for dynamically-linked / position-independent code to
318 // work.
319 \\ ld $gp, 0($ra)
320 \\ dsubu $gp, $ra, $gp
321 \\ ld $25, 8($ra)
322 \\ daddu $25, $25, $gp
299 \\ move $ra, $0323 \\ move $ra, $0
300 \\ move $a0, $sp324 \\ move $a0, $sp
301 \\ and $sp, -16325 \\ and $sp, -16
302 \\ j %[posixCallMainAndExit]326 \\ dsubu $sp, $sp, 16
327 \\ jalr $25
303 ,328 ,
304 .powerpc, .powerpcle =>329 .powerpc, .powerpcle =>
305 // Set up the initial stack frame, and clear the back chain pointer.330 // Set up the initial stack frame, and clear the back chain pointer.
...@@ -389,7 +414,6 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.C) noreturn {...@@ -389,7 +414,6 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.C) noreturn {
389 if (native_os == .linux) {414 if (native_os == .linux) {
390 // Find the beginning of the auxiliary vector415 // Find the beginning of the auxiliary vector
391 const auxv: [*]elf.Auxv = @ptrCast(@alignCast(envp.ptr + envp_count + 1));416 const auxv: [*]elf.Auxv = @ptrCast(@alignCast(envp.ptr + envp_count + 1));
392 std.os.linux.elf_aux_maybe = auxv;
393417
394 var at_hwcap: usize = 0;418 var at_hwcap: usize = 0;
395 const phdrs = init: {419 const phdrs = init: {
...@@ -407,12 +431,17 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.C) noreturn {...@@ -407,12 +431,17 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.C) noreturn {
407 break :init @as([*]elf.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];431 break :init @as([*]elf.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];
408 };432 };
409433
410 // Apply the initial relocations as early as possible in the startup434 // Apply the initial relocations as early as possible in the startup process. We cannot
411 // process.435 // make calls yet on some architectures (e.g. MIPS) *because* they haven't been applied yet,
436 // so this must be fully inlined.
412 if (builtin.position_independent_executable) {437 if (builtin.position_independent_executable) {
413 std.os.linux.pie.relocate(phdrs);438 @call(.always_inline, std.os.linux.pie.relocate, .{phdrs});
414 }439 }
415440
441 // This must be done after PIE relocations have been applied or we may crash
442 // while trying to access the global variable (happens on MIPS at least).
443 std.os.linux.elf_aux_maybe = auxv;
444
416 if (!builtin.single_threaded) {445 if (!builtin.single_threaded) {
417 // ARMv6 targets (and earlier) have no support for TLS in hardware.446 // ARMv6 targets (and earlier) have no support for TLS in hardware.
418 // FIXME: Elide the check for targets >= ARMv7 when the target feature API447 // FIXME: Elide the check for targets >= ARMv7 when the target feature API