Skip to content

Commit c7170e4

Browse files
committed
Support PIE (Position Independent Executables)
Closes #4503 Revives #3960 Merges branch 'pie' into master
2 parents 98d5bfb + abc717f commit c7170e4

23 files changed

+485
-34
lines changed

lib/libc/musl/ldso/dlstart.c

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include <stddef.h>
2+
#include "dynlink.h"
3+
#include "libc.h"
4+
5+
#ifndef START
6+
#define START "_dlstart"
7+
#endif
8+
9+
#define SHARED
10+
11+
#include "crt_arch.h"
12+
13+
#ifndef GETFUNCSYM
14+
#define GETFUNCSYM(fp, sym, got) do { \
15+
hidden void sym(); \
16+
static void (*static_func_ptr)() = sym; \
17+
__asm__ __volatile__ ( "" : "+m"(static_func_ptr) : : "memory"); \
18+
*(fp) = static_func_ptr; } while(0)
19+
#endif
20+
21+
hidden void _dlstart_c(size_t *sp, size_t *dynv)
22+
{
23+
size_t i, aux[AUX_CNT], dyn[DYN_CNT];
24+
size_t *rel, rel_size, base;
25+
26+
int argc = *sp;
27+
char **argv = (void *)(sp+1);
28+
29+
for (i=argc+1; argv[i]; i++);
30+
size_t *auxv = (void *)(argv+i+1);
31+
32+
for (i=0; i<AUX_CNT; i++) aux[i] = 0;
33+
for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
34+
aux[auxv[i]] = auxv[i+1];
35+
36+
#if DL_FDPIC
37+
struct fdpic_loadseg *segs, fakeseg;
38+
size_t j;
39+
if (dynv) {
40+
/* crt_arch.h entry point asm is responsible for reserving
41+
* space and moving the extra fdpic arguments to the stack
42+
* vector where they are easily accessible from C. */
43+
segs = ((struct fdpic_loadmap *)(sp[-1] ? sp[-1] : sp[-2]))->segs;
44+
} else {
45+
/* If dynv is null, the entry point was started from loader
46+
* that is not fdpic-aware. We can assume normal fixed-
47+
* displacement ELF loading was performed, but when ldso was
48+
* run as a command, finding the Ehdr is a heursitic: we
49+
* have to assume Phdrs start in the first 4k of the file. */
50+
base = aux[AT_BASE];
51+
if (!base) base = aux[AT_PHDR] & -4096;
52+
segs = &fakeseg;
53+
segs[0].addr = base;
54+
segs[0].p_vaddr = 0;
55+
segs[0].p_memsz = -1;
56+
Ehdr *eh = (void *)base;
57+
Phdr *ph = (void *)(base + eh->e_phoff);
58+
size_t phnum = eh->e_phnum;
59+
size_t phent = eh->e_phentsize;
60+
while (phnum-- && ph->p_type != PT_DYNAMIC)
61+
ph = (void *)((size_t)ph + phent);
62+
dynv = (void *)(base + ph->p_vaddr);
63+
}
64+
#endif
65+
66+
for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
67+
for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
68+
dyn[dynv[i]] = dynv[i+1];
69+
70+
#if DL_FDPIC
71+
for (i=0; i<DYN_CNT; i++) {
72+
if (i==DT_RELASZ || i==DT_RELSZ) continue;
73+
if (!dyn[i]) continue;
74+
for (j=0; dyn[i]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
75+
dyn[i] += segs[j].addr - segs[j].p_vaddr;
76+
}
77+
base = 0;
78+
79+
const Sym *syms = (void *)dyn[DT_SYMTAB];
80+
81+
rel = (void *)dyn[DT_RELA];
82+
rel_size = dyn[DT_RELASZ];
83+
for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
84+
if (!IS_RELATIVE(rel[1], syms)) continue;
85+
for (j=0; rel[0]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
86+
size_t *rel_addr = (void *)
87+
(rel[0] + segs[j].addr - segs[j].p_vaddr);
88+
if (R_TYPE(rel[1]) == REL_FUNCDESC_VAL) {
89+
*rel_addr += segs[rel_addr[1]].addr
90+
- segs[rel_addr[1]].p_vaddr
91+
+ syms[R_SYM(rel[1])].st_value;
92+
rel_addr[1] = dyn[DT_PLTGOT];
93+
} else {
94+
size_t val = syms[R_SYM(rel[1])].st_value;
95+
for (j=0; val-segs[j].p_vaddr >= segs[j].p_memsz; j++);
96+
*rel_addr = rel[2] + segs[j].addr - segs[j].p_vaddr + val;
97+
}
98+
}
99+
#else
100+
/* If the dynamic linker is invoked as a command, its load
101+
* address is not available in the aux vector. Instead, compute
102+
* the load address as the difference between &_DYNAMIC and the
103+
* virtual address in the PT_DYNAMIC program header. */
104+
base = aux[AT_BASE];
105+
if (!base) {
106+
size_t phnum = aux[AT_PHNUM];
107+
size_t phentsize = aux[AT_PHENT];
108+
Phdr *ph = (void *)aux[AT_PHDR];
109+
for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
110+
if (ph->p_type == PT_DYNAMIC) {
111+
base = (size_t)dynv - ph->p_vaddr;
112+
break;
113+
}
114+
}
115+
}
116+
117+
/* MIPS uses an ugly packed form for GOT relocations. Since we
118+
* can't make function calls yet and the code is tiny anyway,
119+
* it's simply inlined here. */
120+
if (NEED_MIPS_GOT_RELOCS) {
121+
size_t local_cnt = 0;
122+
size_t *got = (void *)(base + dyn[DT_PLTGOT]);
123+
for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
124+
local_cnt = dynv[i+1];
125+
for (i=0; i<local_cnt; i++) got[i] += base;
126+
}
127+
128+
rel = (void *)(base+dyn[DT_REL]);
129+
rel_size = dyn[DT_RELSZ];
130+
for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
131+
if (!IS_RELATIVE(rel[1], 0)) continue;
132+
size_t *rel_addr = (void *)(base + rel[0]);
133+
*rel_addr += base;
134+
}
135+
136+
rel = (void *)(base+dyn[DT_RELA]);
137+
rel_size = dyn[DT_RELASZ];
138+
for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
139+
if (!IS_RELATIVE(rel[1], 0)) continue;
140+
size_t *rel_addr = (void *)(base + rel[0]);
141+
*rel_addr = base + rel[2];
142+
}
143+
#endif
144+
145+
stage2_func dls2;
146+
GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
147+
dls2((void *)base, sp);
148+
}

lib/std/build.zig

+11
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,9 @@ pub const LibExeObjStep = struct {
12861286
/// Position Independent Code
12871287
force_pic: ?bool = null,
12881288

1289+
/// Position Independent Executable
1290+
pie: ?bool = null,
1291+
12891292
subsystem: ?builtin.SubSystem = null,
12901293

12911294
const LinkObject = union(enum) {
@@ -2307,6 +2310,14 @@ pub const LibExeObjStep = struct {
23072310
}
23082311
}
23092312

2313+
if (self.pie) |pie| {
2314+
if (pie) {
2315+
try zig_args.append("-fPIE");
2316+
} else {
2317+
try zig_args.append("-fno-PIE");
2318+
}
2319+
}
2320+
23102321
if (self.subsystem) |subsystem| {
23112322
try zig_args.append("--subsystem");
23122323
try zig_args.append(switch (subsystem) {

lib/std/dynamic_library.zig

+26-26
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,48 @@ const RDebug = extern struct {
5959
r_ldbase: usize,
6060
};
6161

62-
fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
63-
for (phdrs) |*phdr| {
64-
if (phdr.p_type == elf.PT_LOAD) {
65-
return @ptrToInt(phdr) - phdr.p_vaddr;
66-
}
62+
// TODO: This should be weak (#1917)
63+
extern var _DYNAMIC: [128]elf.Dyn;
64+
65+
comptime {
66+
if (std.Target.current.os.tag == .linux) {
67+
asm (
68+
\\ .weak _DYNAMIC
69+
\\ .hidden _DYNAMIC
70+
);
6771
}
68-
return error.InvalidExe;
6972
}
7073

7174
pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
72-
const va_offset = try elf_get_va_offset(phdrs);
73-
74-
const dyn_table = init: {
75-
for (phdrs) |*phdr| {
76-
if (phdr.p_type == elf.PT_DYNAMIC) {
77-
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
78-
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
79-
}
80-
}
75+
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
8176
// No PT_DYNAMIC means this is either a statically-linked program or a
8277
// badly corrupted one
8378
return LinkMap.Iterator{ .current = null };
84-
};
79+
}
8580

8681
const link_map_ptr = init: {
87-
for (dyn_table) |*dyn| {
88-
switch (dyn.d_tag) {
82+
var i: usize = 0;
83+
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
84+
switch (_DYNAMIC[i].d_tag) {
8985
elf.DT_DEBUG => {
90-
const r_debug = @intToPtr(*RDebug, dyn.d_val);
91-
if (r_debug.r_version != 1) return error.InvalidExe;
92-
break :init r_debug.r_map;
86+
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_val);
87+
if (ptr) |r_debug| {
88+
if (r_debug.r_version != 1) return error.InvalidExe;
89+
break :init r_debug.r_map;
90+
}
9391
},
9492
elf.DT_PLTGOT => {
95-
const got_table = @intToPtr([*]usize, dyn.d_val);
96-
// The address to the link_map structure is stored in the
97-
// second slot
98-
break :init @intToPtr(?*LinkMap, got_table[1]);
93+
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_val);
94+
if (ptr) |got_table| {
95+
// The address to the link_map structure is stored in
96+
// the second slot
97+
break :init @intToPtr(?*LinkMap, got_table[1]);
98+
}
9999
},
100100
else => {},
101101
}
102102
}
103-
return error.InvalidExe;
103+
return LinkMap.Iterator{ .current = null };
104104
};
105105

106106
return LinkMap.Iterator{ .current = link_map_ptr };

lib/std/elf.zig

+38
Original file line numberDiff line numberDiff line change
@@ -719,20 +719,48 @@ pub const Elf64_Syminfo = extern struct {
719719
pub const Elf32_Rel = extern struct {
720720
r_offset: Elf32_Addr,
721721
r_info: Elf32_Word,
722+
723+
pub inline fn r_sym(self: @This()) u24 {
724+
return @truncate(u24, self.r_info >> 8);
725+
}
726+
pub inline fn r_type(self: @This()) u8 {
727+
return @truncate(u8, self.r_info & 0xff);
728+
}
722729
};
723730
pub const Elf64_Rel = extern struct {
724731
r_offset: Elf64_Addr,
725732
r_info: Elf64_Xword,
733+
734+
pub inline fn r_sym(self: @This()) u32 {
735+
return @truncate(u32, self.r_info >> 32);
736+
}
737+
pub inline fn r_type(self: @This()) u32 {
738+
return @truncate(u32, self.r_info & 0xffffffff);
739+
}
726740
};
727741
pub const Elf32_Rela = extern struct {
728742
r_offset: Elf32_Addr,
729743
r_info: Elf32_Word,
730744
r_addend: Elf32_Sword,
745+
746+
pub inline fn r_sym(self: @This()) u24 {
747+
return @truncate(u24, self.r_info >> 8);
748+
}
749+
pub inline fn r_type(self: @This()) u8 {
750+
return @truncate(u8, self.r_info & 0xff);
751+
}
731752
};
732753
pub const Elf64_Rela = extern struct {
733754
r_offset: Elf64_Addr,
734755
r_info: Elf64_Xword,
735756
r_addend: Elf64_Sxword,
757+
758+
pub inline fn r_sym(self: @This()) u32 {
759+
return @truncate(u32, self.r_info >> 32);
760+
}
761+
pub inline fn r_type(self: @This()) u32 {
762+
return @truncate(u32, self.r_info & 0xffffffff);
763+
}
736764
};
737765
pub const Elf32_Dyn = extern struct {
738766
d_tag: Elf32_Sword,
@@ -917,6 +945,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
917945
8 => Elf64_Dyn,
918946
else => @compileError("expected pointer size of 32 or 64"),
919947
};
948+
pub const Rel = switch (@sizeOf(usize)) {
949+
4 => Elf32_Rel,
950+
8 => Elf64_Rel,
951+
else => @compileError("expected pointer size of 32 or 64"),
952+
};
953+
pub const Rela = switch (@sizeOf(usize)) {
954+
4 => Elf32_Rela,
955+
8 => Elf64_Rela,
956+
else => @compileError("expected pointer size of 32 or 64"),
957+
};
920958
pub const Shdr = switch (@sizeOf(usize)) {
921959
4 => Elf32_Shdr,
922960
8 => Elf64_Shdr,

0 commit comments

Comments
 (0)