From a330aaa4d20a2bf9ee8db7574fd0df2f1a923718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 28 Aug 2024 05:23:22 +0200 Subject: [PATCH 1/3] std.DynLib: Remove an outdated TODO. --- lib/std/dynamic_library.zig | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index 110393d91a45..49a569a8e15a 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -83,15 +83,9 @@ const RDebug = extern struct { r_ldbase: usize, }; -/// TODO make it possible to reference this same external symbol 2x so we don't need this -/// helper function. -pub fn get_DYNAMIC() ?[*]elf.Dyn { - return @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .weak }); -} - pub fn linkmap_iterator(phdrs: []elf.Phdr) error{InvalidExe}!LinkMap.Iterator { _ = phdrs; - const _DYNAMIC = get_DYNAMIC() orelse { + const _DYNAMIC = @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .weak }) orelse { // No PT_DYNAMIC means this is either a statically-linked program or a // badly corrupted dynamically-linked one. return .{ .current = null }; From a7c52c8903cb9626b8827b53d1b1c2c11e22db27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 28 Aug 2024 05:25:30 +0200 Subject: [PATCH 2/3] std.os.linux: Rename start_pie.zig to pie.zig. This just reflects the name it's actually exposed with in code. --- lib/std/os/linux.zig | 2 +- lib/std/os/linux/{start_pie.zig => pie.zig} | 0 lib/std/start.zig | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename lib/std/os/linux/{start_pie.zig => pie.zig} (100%) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index df9d491a5e04..4c7e7bc0882b 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -115,7 +115,7 @@ pub const user_desc = arch_bits.user_desc; pub const getcontext = arch_bits.getcontext; pub const tls = @import("linux/tls.zig"); -pub const pie = @import("linux/start_pie.zig"); +pub const pie = @import("linux/pie.zig"); pub const BPF = @import("linux/bpf.zig"); pub const IOCTL = @import("linux/ioctl.zig"); pub const SECCOMP = @import("linux/seccomp.zig"); diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/pie.zig similarity index 100% rename from lib/std/os/linux/start_pie.zig rename to lib/std/os/linux/pie.zig diff --git a/lib/std/start.zig b/lib/std/start.zig index ea6f347bd665..561d5b0981c0 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -288,8 +288,8 @@ fn _start() callconv(.Naked) noreturn { , .csky => // The CSKY ABI assumes that `gb` is set to the address of the GOT in order for - // position-independent code to work. We depend on this in `std.os.linux.start_pie` - // to locate `_DYNAMIC` as well. + // position-independent code to work. We depend on this in `std.os.linux.pie` to locate + // `_DYNAMIC` as well. \\ grs t0, 1f \\ 1: \\ lrw gb, 1b@GOTPC From 6c9cc09a2d5bae3983ebc8d718f159177329f951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 28 Aug 2024 05:50:06 +0200 Subject: [PATCH 3/3] std.os.linux.pie: Remove `.weak`/`.hidden` usage in inline assembly. This code is only compiled into the executable in PIE builds, so there really doesn't seem to be a compelling reason for `_DYNAMIC` to be accessed as a weak symbol. Small step towards #10761. --- lib/std/os/linux/pie.zig | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/lib/std/os/linux/pie.zig b/lib/std/os/linux/pie.zig index 5099f39563b0..3aa45efc9d2d 100644 --- a/lib/std/os/linux/pie.zig +++ b/lib/std/os/linux/pie.zig @@ -42,29 +42,21 @@ const R_RELATIVE = switch (builtin.cpu.arch) { inline fn getDynamicSymbol() [*]elf.Dyn { return switch (builtin.cpu.arch) { .x86 => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ call 1f \\ 1: pop %[ret] \\ lea _DYNAMIC-1b(%[ret]), %[ret] : [ret] "=r" (-> [*]elf.Dyn), ), .x86_64 => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ lea _DYNAMIC(%%rip), %[ret] : [ret] "=r" (-> [*]elf.Dyn), ), .arc => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ add %[ret], pcl, _DYNAMIC@pcl : [ret] "=r" (-> [*]elf.Dyn), ), // Work around the limited offset range of `ldr` .arm, .armeb, .thumb, .thumbeb => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ ldr %[ret], 1f \\ add %[ret], pc \\ b 2f @@ -74,8 +66,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn { ), // A simple `adr` is not enough as it has a limited offset range .aarch64, .aarch64_be => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ adrp %[ret], _DYNAMIC \\ add %[ret], %[ret], #:lo12:_DYNAMIC : [ret] "=r" (-> [*]elf.Dyn), @@ -88,8 +78,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : [ret] "=r" (-> [*]elf.Dyn), ), .hexagon => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ jump 1f \\ .word _DYNAMIC - . \\ 1: @@ -102,23 +90,17 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : "r1" ), .loongarch32, .loongarch64 => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ la.local %[ret], _DYNAMIC : [ret] "=r" (-> [*]elf.Dyn), ), // Note that the - 8 is needed because pc in the second lea instruction points into the // middle of that instruction. (The first lea is 6 bytes, the second is 4 bytes.) .m68k => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ lea _DYNAMIC - . - 8, %[ret] \\ lea (%[ret], %%pc), %[ret] : [ret] "=r" (-> [*]elf.Dyn), ), .mips, .mipsel => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ bal 1f \\ .gpword _DYNAMIC \\ 1: @@ -129,8 +111,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : "lr" ), .mips64, .mips64el => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ .balign 8 \\ bal 1f \\ .gpdword _DYNAMIC @@ -142,8 +122,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : "lr" ), .powerpc, .powerpcle => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ bl 1f \\ .long _DYNAMIC - . \\ 1: @@ -155,8 +133,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : "lr", "r4" ), .powerpc64, .powerpc64le => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ bl 1f \\ .quad _DYNAMIC - . \\ 1: @@ -168,14 +144,10 @@ inline fn getDynamicSymbol() [*]elf.Dyn { : "lr", "r4" ), .riscv32, .riscv64 => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ lla %[ret], _DYNAMIC : [ret] "=r" (-> [*]elf.Dyn), ), .s390x => asm volatile ( - \\ .weak _DYNAMIC - \\ .hidden _DYNAMIC \\ larl %[ret], 1f \\ ag %[ret], 0(%[ret]) \\ b 2f