Skip to content

Commit f2bf6c1

Browse files
authored
Merge pull request #20776 from alexrp/start-pie-more-arches
`std.os.linux.start_pie`: Add arc, csky, and hexagon support (and arm variants)
2 parents 2458e53 + 38c492b commit f2bf6c1

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

lib/std/os/linux/start_pie.zig

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ const assert = std.debug.assert;
55

66
const R_AMD64_RELATIVE = 8;
77
const R_386_RELATIVE = 8;
8+
const R_ARC_RELATIVE = 56;
89
const R_ARM_RELATIVE = 23;
910
const R_AARCH64_RELATIVE = 1027;
11+
const R_CSKY_RELATIVE = 9;
12+
const R_HEXAGON_RELATIVE = 35;
1013
const R_LARCH_RELATIVE = 3;
1114
const R_68K_RELATIVE = 22;
1215
const R_RISCV_RELATIVE = 3;
@@ -16,8 +19,11 @@ const R_SPARC_RELATIVE = 22;
1619
const R_RELATIVE = switch (builtin.cpu.arch) {
1720
.x86 => R_386_RELATIVE,
1821
.x86_64 => R_AMD64_RELATIVE,
19-
.arm => R_ARM_RELATIVE,
20-
.aarch64 => R_AARCH64_RELATIVE,
22+
.arc => R_ARC_RELATIVE,
23+
.arm, .armeb, .thumb, .thumbeb => R_ARM_RELATIVE,
24+
.aarch64, .aarch64_be => R_AARCH64_RELATIVE,
25+
.csky => R_CSKY_RELATIVE,
26+
.hexagon => R_HEXAGON_RELATIVE,
2127
.loongarch32, .loongarch64 => R_LARCH_RELATIVE,
2228
.m68k => R_68K_RELATIVE,
2329
.riscv64 => R_RISCV_RELATIVE,
@@ -44,8 +50,14 @@ fn getDynamicSymbol() [*]elf.Dyn {
4450
\\ lea _DYNAMIC(%%rip), %[ret]
4551
: [ret] "=r" (-> [*]elf.Dyn),
4652
),
53+
.arc => asm volatile (
54+
\\ .weak _DYNAMIC
55+
\\ .hidden _DYNAMIC
56+
\\ add %[ret], pcl, _DYNAMIC@pcl
57+
: [ret] "=r" (-> [*]elf.Dyn),
58+
),
4759
// Work around the limited offset range of `ldr`
48-
.arm => asm volatile (
60+
.arm, .armeb, .thumb, .thumbeb => asm volatile (
4961
\\ .weak _DYNAMIC
5062
\\ .hidden _DYNAMIC
5163
\\ ldr %[ret], 1f
@@ -56,13 +68,34 @@ fn getDynamicSymbol() [*]elf.Dyn {
5668
: [ret] "=r" (-> [*]elf.Dyn),
5769
),
5870
// A simple `adr` is not enough as it has a limited offset range
59-
.aarch64 => asm volatile (
71+
.aarch64, .aarch64_be => asm volatile (
6072
\\ .weak _DYNAMIC
6173
\\ .hidden _DYNAMIC
6274
\\ adrp %[ret], _DYNAMIC
6375
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
6476
: [ret] "=r" (-> [*]elf.Dyn),
6577
),
78+
// The CSKY ABI requires the gb register to point to the GOT. Additionally, the first
79+
// entry in the GOT is defined to hold the address of _DYNAMIC.
80+
.csky => asm volatile (
81+
\\ mov %[ret], gb
82+
\\ ldw %[ret], %[ret]
83+
: [ret] "=r" (-> [*]elf.Dyn),
84+
),
85+
.hexagon => asm volatile (
86+
\\ .weak _DYNAMIC
87+
\\ .hidden _DYNAMIC
88+
\\ jump 1f
89+
\\ .word _DYNAMIC - .
90+
\\ 1:
91+
\\ r1 = pc
92+
\\ r1 = add(r1, #-4)
93+
\\ %[ret] = memw(r1)
94+
\\ %[ret] = add(r1, %[ret])
95+
: [ret] "=r" (-> [*]elf.Dyn),
96+
:
97+
: "r1"
98+
),
6699
.loongarch32, .loongarch64 => asm volatile (
67100
\\ .weak _DYNAMIC
68101
\\ .hidden _DYNAMIC

0 commit comments

Comments
 (0)