Skip to content

std.os.linux.pie: Remove .weak/.hidden usage in inline assembly. #21260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/std/dynamic_library.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
28 changes: 0 additions & 28 deletions lib/std/os/linux/start_pie.zig → lib/std/os/linux/pie.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -129,8 +111,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn {
: "lr"
),
.mips64, .mips64el => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ .balign 8
\\ bal 1f
\\ .gpdword _DYNAMIC
Expand All @@ -142,8 +122,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn {
: "lr"
),
.powerpc, .powerpcle => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ bl 1f
\\ .long _DYNAMIC - .
\\ 1:
Expand All @@ -155,8 +133,6 @@ inline fn getDynamicSymbol() [*]elf.Dyn {
: "lr", "r4"
),
.powerpc64, .powerpc64le => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ bl 1f
\\ .quad _DYNAMIC - .
\\ 1:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading