From 2dabb7ec778dcf7eb59162ced4d3ceacecba187a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 24 Jul 2024 09:13:25 +0200 Subject: [PATCH 1/4] std.os.linux.start_pie: Handle armeb, thumb, thumbeb, and aarch64_be too. --- lib/std/os/linux/start_pie.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/start_pie.zig index 9ab04e911b..6e7f00c006 100644 --- a/lib/std/os/linux/start_pie.zig +++ b/lib/std/os/linux/start_pie.zig @@ -16,8 +16,8 @@ const R_SPARC_RELATIVE = 22; const R_RELATIVE = switch (builtin.cpu.arch) { .x86 => R_386_RELATIVE, .x86_64 => R_AMD64_RELATIVE, - .arm => R_ARM_RELATIVE, - .aarch64 => R_AARCH64_RELATIVE, + .arm, .armeb, .thumb, .thumbeb => R_ARM_RELATIVE, + .aarch64, .aarch64_be => R_AARCH64_RELATIVE, .loongarch32, .loongarch64 => R_LARCH_RELATIVE, .m68k => R_68K_RELATIVE, .riscv64 => R_RISCV_RELATIVE, @@ -45,7 +45,7 @@ fn getDynamicSymbol() [*]elf.Dyn { : [ret] "=r" (-> [*]elf.Dyn), ), // Work around the limited offset range of `ldr` - .arm => asm volatile ( + .arm, .armeb, .thumb, .thumbeb => asm volatile ( \\ .weak _DYNAMIC \\ .hidden _DYNAMIC \\ ldr %[ret], 1f @@ -56,7 +56,7 @@ fn getDynamicSymbol() [*]elf.Dyn { : [ret] "=r" (-> [*]elf.Dyn), ), // A simple `adr` is not enough as it has a limited offset range - .aarch64 => asm volatile ( + .aarch64, .aarch64_be => asm volatile ( \\ .weak _DYNAMIC \\ .hidden _DYNAMIC \\ adrp %[ret], _DYNAMIC From bc054a713e3316843dcf3089c6a4b09e52c19b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 24 Jul 2024 09:24:33 +0200 Subject: [PATCH 2/4] std.os.linux.start_pie: Add csky support. --- lib/std/os/linux/start_pie.zig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/start_pie.zig index 6e7f00c006..7e6ecc763c 100644 --- a/lib/std/os/linux/start_pie.zig +++ b/lib/std/os/linux/start_pie.zig @@ -7,6 +7,7 @@ const R_AMD64_RELATIVE = 8; const R_386_RELATIVE = 8; const R_ARM_RELATIVE = 23; const R_AARCH64_RELATIVE = 1027; +const R_CSKY_RELATIVE = 9; const R_LARCH_RELATIVE = 3; const R_68K_RELATIVE = 22; const R_RISCV_RELATIVE = 3; @@ -18,6 +19,7 @@ const R_RELATIVE = switch (builtin.cpu.arch) { .x86_64 => R_AMD64_RELATIVE, .arm, .armeb, .thumb, .thumbeb => R_ARM_RELATIVE, .aarch64, .aarch64_be => R_AARCH64_RELATIVE, + .csky => R_CSKY_RELATIVE, .loongarch32, .loongarch64 => R_LARCH_RELATIVE, .m68k => R_68K_RELATIVE, .riscv64 => R_RISCV_RELATIVE, @@ -63,6 +65,13 @@ fn getDynamicSymbol() [*]elf.Dyn { \\ add %[ret], %[ret], #:lo12:_DYNAMIC : [ret] "=r" (-> [*]elf.Dyn), ), + // The CSKY ABI requires the gb register to point to the GOT. Additionally, the first + // entry in the GOT is defined to hold the address of _DYNAMIC. + .csky => asm volatile ( + \\ mov %[ret], gb + \\ ldw %[ret], %[ret] + : [ret] "=r" (-> [*]elf.Dyn), + ), .loongarch32, .loongarch64 => asm volatile ( \\ .weak _DYNAMIC \\ .hidden _DYNAMIC From a0b2b987c8a4252a1ae11937fc2affe6f48ee8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 24 Jul 2024 09:40:27 +0200 Subject: [PATCH 3/4] std.os.linux.start_pie: Add arc support. --- lib/std/os/linux/start_pie.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/start_pie.zig index 7e6ecc763c..2f46ebb791 100644 --- a/lib/std/os/linux/start_pie.zig +++ b/lib/std/os/linux/start_pie.zig @@ -5,6 +5,7 @@ const assert = std.debug.assert; const R_AMD64_RELATIVE = 8; const R_386_RELATIVE = 8; +const R_ARC_RELATIVE = 56; const R_ARM_RELATIVE = 23; const R_AARCH64_RELATIVE = 1027; const R_CSKY_RELATIVE = 9; @@ -17,6 +18,7 @@ const R_SPARC_RELATIVE = 22; const R_RELATIVE = switch (builtin.cpu.arch) { .x86 => R_386_RELATIVE, .x86_64 => R_AMD64_RELATIVE, + .arc => R_ARC_RELATIVE, .arm, .armeb, .thumb, .thumbeb => R_ARM_RELATIVE, .aarch64, .aarch64_be => R_AARCH64_RELATIVE, .csky => R_CSKY_RELATIVE, @@ -46,6 +48,12 @@ fn getDynamicSymbol() [*]elf.Dyn { \\ 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 From 38c492bb531f57fa70f68276399e302f90f66176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 24 Jul 2024 10:00:58 +0200 Subject: [PATCH 4/4] std.os.linux.start_pie: Add hexagon support. --- lib/std/os/linux/start_pie.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/start_pie.zig index 2f46ebb791..19b6c332f7 100644 --- a/lib/std/os/linux/start_pie.zig +++ b/lib/std/os/linux/start_pie.zig @@ -9,6 +9,7 @@ const R_ARC_RELATIVE = 56; const R_ARM_RELATIVE = 23; const R_AARCH64_RELATIVE = 1027; const R_CSKY_RELATIVE = 9; +const R_HEXAGON_RELATIVE = 35; const R_LARCH_RELATIVE = 3; const R_68K_RELATIVE = 22; const R_RISCV_RELATIVE = 3; @@ -22,6 +23,7 @@ const R_RELATIVE = switch (builtin.cpu.arch) { .arm, .armeb, .thumb, .thumbeb => R_ARM_RELATIVE, .aarch64, .aarch64_be => R_AARCH64_RELATIVE, .csky => R_CSKY_RELATIVE, + .hexagon => R_HEXAGON_RELATIVE, .loongarch32, .loongarch64 => R_LARCH_RELATIVE, .m68k => R_68K_RELATIVE, .riscv64 => R_RISCV_RELATIVE, @@ -80,6 +82,20 @@ fn getDynamicSymbol() [*]elf.Dyn { \\ ldw %[ret], %[ret] : [ret] "=r" (-> [*]elf.Dyn), ), + .hexagon => asm volatile ( + \\ .weak _DYNAMIC + \\ .hidden _DYNAMIC + \\ jump 1f + \\ .word _DYNAMIC - . + \\ 1: + \\ r1 = pc + \\ r1 = add(r1, #-4) + \\ %[ret] = memw(r1) + \\ %[ret] = add(r1, %[ret]) + : [ret] "=r" (-> [*]elf.Dyn), + : + : "r1" + ), .loongarch32, .loongarch64 => asm volatile ( \\ .weak _DYNAMIC \\ .hidden _DYNAMIC