Skip to content

Commit 74a3ae4

Browse files
committed
start: Don't artificially limit some posixCallMainAndExit() logic to Linux.
This code applies to ~any POSIX OS where we don't link libc. For example, it'll be useful for FreeBSD and NetBSD. As part of this, move std.os.linux.pie to std.pie since there's really nothing Linux-specific about what that file is doing.
1 parent 8e72a25 commit 74a3ae4

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

lib/std/os/linux.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ pub const user_desc = arch_bits.user_desc;
115115
pub const getcontext = arch_bits.getcontext;
116116

117117
pub const tls = @import("linux/tls.zig");
118-
pub const pie = @import("linux/pie.zig");
119118
pub const BPF = @import("linux/bpf.zig");
120119
pub const IOCTL = @import("linux/ioctl.zig");
121120
pub const SECCOMP = @import("linux/seccomp.zig");
File renamed without changes.

lib/std/start.zig

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn _start() callconv(.naked) noreturn {
325325
,
326326
.csky =>
327327
// The CSKY ABI assumes that `gb` is set to the address of the GOT in order for
328-
// position-independent code to work. We depend on this in `std.os.linux.pie` to locate
328+
// position-independent code to work. We depend on this in `std.pie` to locate
329329
// `_DYNAMIC` as well.
330330
// r8 = FP
331331
\\ grs t0, 1f
@@ -514,33 +514,33 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
514514
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
515515
const envp = @as([*][*:0]u8, @ptrCast(envp_optional))[0..envp_count];
516516

517-
if (native_os == .linux) {
518-
// Find the beginning of the auxiliary vector
519-
const auxv: [*]elf.Auxv = @ptrCast(@alignCast(envp.ptr + envp_count + 1));
520-
521-
var at_hwcap: usize = 0;
522-
const phdrs = init: {
523-
var i: usize = 0;
524-
var at_phdr: usize = 0;
525-
var at_phnum: usize = 0;
526-
while (auxv[i].a_type != elf.AT_NULL) : (i += 1) {
527-
switch (auxv[i].a_type) {
528-
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
529-
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
530-
elf.AT_HWCAP => at_hwcap = auxv[i].a_un.a_val,
531-
else => continue,
532-
}
517+
// Find the beginning of the auxiliary vector
518+
const auxv: [*]elf.Auxv = @ptrCast(@alignCast(envp.ptr + envp_count + 1));
519+
520+
var at_hwcap: usize = 0;
521+
const phdrs = init: {
522+
var i: usize = 0;
523+
var at_phdr: usize = 0;
524+
var at_phnum: usize = 0;
525+
while (auxv[i].a_type != elf.AT_NULL) : (i += 1) {
526+
switch (auxv[i].a_type) {
527+
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
528+
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
529+
elf.AT_HWCAP => at_hwcap = auxv[i].a_un.a_val,
530+
else => continue,
533531
}
534-
break :init @as([*]elf.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];
535-
};
536-
537-
// Apply the initial relocations as early as possible in the startup process. We cannot
538-
// make calls yet on some architectures (e.g. MIPS) *because* they haven't been applied yet,
539-
// so this must be fully inlined.
540-
if (builtin.position_independent_executable) {
541-
@call(.always_inline, std.os.linux.pie.relocate, .{phdrs});
542532
}
533+
break :init @as([*]elf.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];
534+
};
535+
536+
// Apply the initial relocations as early as possible in the startup process. We cannot
537+
// make calls yet on some architectures (e.g. MIPS) *because* they haven't been applied yet,
538+
// so this must be fully inlined.
539+
if (builtin.position_independent_executable) {
540+
@call(.always_inline, std.pie.relocate, .{phdrs});
541+
}
543542

543+
if (native_os == .linux) {
544544
// This must be done after PIE relocations have been applied or we may crash
545545
// while trying to access the global variable (happens on MIPS at least).
546546
std.os.linux.elf_aux_maybe = auxv;
@@ -567,20 +567,20 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
567567
// Here we look for the stack size in our program headers and use setrlimit
568568
// to ask for more stack space.
569569
expandStackSize(phdrs);
570+
}
570571

571-
const opt_init_array_start = @extern([*]*const fn () callconv(.c) void, .{
572-
.name = "__init_array_start",
573-
.linkage = .weak,
574-
});
575-
const opt_init_array_end = @extern([*]*const fn () callconv(.c) void, .{
576-
.name = "__init_array_end",
577-
.linkage = .weak,
578-
});
579-
if (opt_init_array_start) |init_array_start| {
580-
const init_array_end = opt_init_array_end.?;
581-
const slice = init_array_start[0 .. init_array_end - init_array_start];
582-
for (slice) |func| func();
583-
}
572+
const opt_init_array_start = @extern([*]*const fn () callconv(.c) void, .{
573+
.name = "__init_array_start",
574+
.linkage = .weak,
575+
});
576+
const opt_init_array_end = @extern([*]*const fn () callconv(.c) void, .{
577+
.name = "__init_array_end",
578+
.linkage = .weak,
579+
});
580+
if (opt_init_array_start) |init_array_start| {
581+
const init_array_end = opt_init_array_end.?;
582+
const slice = init_array_start[0 .. init_array_end - init_array_start];
583+
for (slice) |func| func();
584584
}
585585

586586
std.posix.exit(callMainWithArgs(argc, argv, envp));

lib/std/std.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub const net = @import("net.zig");
7979
pub const os = @import("os.zig");
8080
pub const once = @import("once.zig").once;
8181
pub const pdb = @import("pdb.zig");
82+
pub const pie = @import("pie.zig");
8283
pub const posix = @import("posix.zig");
8384
pub const process = @import("process.zig");
8485
pub const sort = @import("sort.zig");

0 commit comments

Comments
 (0)