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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
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
54 changes: 27 additions & 27 deletions lib/std/dynamic_library.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@ pub const DynLib = switch (builtin.os) {
// fashion.
const LinkMap = extern struct {
l_addr: usize,
l_name: [*]const u8,
l_name: [*:0]const u8,
l_ld: ?*elf.Dyn,
l_next: ?*LinkMap,
l_prev: ?*LinkMap,
Expand DownExpand Up@@ -53,48 +53,48 @@ const RDebug = extern struct {
r_ldbase: usize,
};

fn elf_get_va_offset(phdrs: []elf.Phdr) !usize {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_LOAD) {
return @ptrToInt(phdr) - phdr.p_vaddr;
}
// XXX: This should be weak (#1917)
extern var _DYNAMIC: [128]elf.Dyn;

comptime {
if (builtin.os == .linux) {
asm (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
);
}
return error.InvalidExe;
}

pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
const va_offset = try elf_get_va_offset(phdrs);

const dyn_table = init: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
const ptr = @intToPtr([*]elf.Dyn, va_offset + phdr.p_vaddr);
break :init ptr[0 .. phdr.p_memsz / @sizeOf(elf.Dyn)];
}
}
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
// No PT_DYNAMIC means this is either a statically-linked program or a
// badly corrupted one
return LinkMap.Iterator{ .current = null };
};
}

const link_map_ptr = init: {
for (dyn_table) |*dyn| {
switch (dyn.d_tag) {
var i: usize = 0;
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (_DYNAMIC[i].d_tag) {
elf.DT_DEBUG => {
const r_debug = @intToPtr(*RDebug, dyn.d_un.d_ptr);
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |r_debug| {
if (r_debug.r_version != 1) return error.InvalidExe;
break :init r_debug.r_map;
}
},
elf.DT_PLTGOT => {
const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
// The address to the link_map structure is stored in the
// second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_un.d_ptr);
if (ptr) |got_table| {
// The address to the link_map structure is stored in
// the second slot
break :init @intToPtr(?*LinkMap, got_table[1]);
}
},
else => {},
}
}
return error.InvalidExe;
return LinkMap.Iterator{ .current = null };
};

return LinkMap.Iterator{ .current = link_map_ptr };
Expand Down
38 changes: 38 additions & 0 deletions lib/std/elf.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,20 +640,48 @@ pub const Elf64_Syminfo = extern struct {
pub const Elf32_Rel = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rel = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Rela = extern struct {
r_offset: Elf32_Addr,
r_info: Elf32_Word,
r_addend: Elf32_Sword,

pub inline fn r_sym(self: @This()) u24 {
return @truncate(u24, self.r_info >> 8);
}
pub inline fn r_type(self: @This()) u8 {
return @truncate(u8, self.r_info & 0xff);
}
};
pub const Elf64_Rela = extern struct {
r_offset: Elf64_Addr,
r_info: Elf64_Xword,
r_addend: Elf64_Sxword,

pub inline fn r_sym(self: @This()) u32 {
return @truncate(u32, self.r_info >> 32);
}
pub inline fn r_type(self: @This()) u32 {
return @truncate(u32, self.r_info & 0xffffffff);
}
};
pub const Elf32_Phdr = extern struct {
p_type: Elf32_Word,
Expand DownExpand Up@@ -853,6 +881,16 @@ pub const Dyn = switch (@sizeOf(usize)) {
8 => Elf64_Dyn,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rel = switch (@sizeOf(usize)) {
4 => Elf32_Rel,
8 => Elf64_Rel,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Rela = switch (@sizeOf(usize)) {
4 => Elf32_Rela,
8 => Elf64_Rela,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Shdr = switch (@sizeOf(usize)) {
4 => Elf32_Shdr,
8 => Elf64_Shdr,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/linux.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -996,7 +996,7 @@ pub const dirent64 = extern struct {

pub const dl_phdr_info = extern struct {
dlpi_addr: usize,
dlpi_name: ?[*]const u8,
dlpi_name: ?[*:0]const u8,
dlpi_phdr: [*]std.elf.Phdr,
dlpi_phnum: u16,
};
Expand Down
138 changes: 138 additions & 0 deletions lib/std/os/linux/start_pie.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
const std = @import("std");
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;

const R_AMD64_RELATIVE = 8;
const R_386_RELATIVE = 8;
const R_ARM_RELATIVE = 23;
const R_AARCH64_RELATIVE = 1027;
const R_RISCV_RELATIVE = 3;

const ARCH_RELATIVE_RELOC = switch (builtin.arch) {
.i386 => R_386_RELATIVE,
.x86_64 => R_AMD64_RELATIVE,
.arm => R_ARM_RELATIVE,
.aarch64 => R_AARCH64_RELATIVE,
.riscv64 => R_RISCV_RELATIVE,
else => @compileError("unsupported architecture"),
};

// Just a convoluted (but necessary) way to obtain the address of the _DYNAMIC[]
// vector as PC-relative so that we can use it before any relocation is applied
fn getDynamicSymbol() [*]elf.Dyn {
const addr = switch (builtin.arch) {
.i386 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ call 1f
\\ 1: pop %[ret]
\\ lea _DYNAMIC-1b(%[ret]), %[ret]
: [ret] "=r" (-> usize)
),
.x86_64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ lea _DYNAMIC(%%rip), %[ret]
: [ret] "=r" (-> usize)
),
// Work around the limited offset range of `ldr`
.arm => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ ldr %[ret], 1f
\\ add %[ret], pc
\\ b 2f
\\ 1: .word _DYNAMIC-1b
\\ 2:
: [ret] "=r" (-> usize)
),
// A simple `adr` is not enough as it has a limited offset range
.aarch64 => asm volatile (
\\ .weak _DYNAMIC
\\ .hidden _DYNAMIC
\\ adrp %[ret], _DYNAMIC
\\ add %[ret], %[ret], #:lo12:_DYNAMIC
: [ret] "=r" (-> usize)
),
.riscv64 => asm volatile (
\\ lla %[ret], _DYNAMIC
: [ret] "=r" (-> usize)
),
else => @compileError("???"),
};
if (addr == 0) unreachable;
return @intToPtr([*]elf.Dyn, addr);
}

pub fn apply_relocations() void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-styling? Or is this a traditional name from somewhere?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not used to camelCase all the identifiers, will change it later :)

@setRuntimeSafety(false);

const dynv = getDynamicSymbol();
const auxv = std.os.linux.elf_aux_maybe.?;
var at_phent: usize = undefined;
var at_phnum: usize = undefined;
var at_phdr: usize = undefined;
var at_hwcap: usize = undefined;

{
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
switch (auxv[i].a_type) {
elf.AT_PHENT => at_phent = auxv[i].a_un.a_val,
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
else => continue,
}
}
}

// Sanity check
assert(at_phent == @sizeOf(elf.Phdr));

// Search the TLS section
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];

const base_addr = blk: {
for (phdrs) |*phdr| {
if (phdr.p_type == elf.PT_DYNAMIC) {
break :blk @ptrToInt(&dynv[0]) - phdr.p_vaddr;
}
}
unreachable;
};

var rel_addr: usize = 0;
var rela_addr: usize = 0;
var rel_size: usize = 0;
var rela_size: usize = 0;

{
var i: usize = 0;
while (dynv[i].d_tag != elf.DT_NULL) : (i += 1) {
switch (dynv[i].d_tag) {
elf.DT_REL => rel_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELA => rela_addr = base_addr + dynv[i].d_un.d_ptr,
elf.DT_RELSZ => rel_size = dynv[i].d_un.d_val,
elf.DT_RELASZ => rela_size = dynv[i].d_un.d_val,
else => {},
}
}
}

// Perform the relocations
if (rel_addr != 0) {
const rel = @bytesToSlice(elf.Rel, @intToPtr([*]u8, rel_addr)[0..rel_size]);
for (rel) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr;
}
}
if (rela_addr != 0) {
const rela = @bytesToSlice(elf.Rela, @intToPtr([*]u8, rela_addr)[0..rela_size]);
for (rela) |r| {
if (r.r_type() != ARCH_RELATIVE_RELOC) continue;
@intToPtr(*usize, base_addr + r.r_offset).* += base_addr + @bitCast(usize, r.r_addend);
}
}
}
2 changes: 2 additions & 0 deletions lib/std/os/test.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,6 +205,8 @@ export fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) i32 {
test "dl_iterate_phdr" {
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
return error.SkipZigTest;
if (builtin.position_independent_executable)
return error.SkipZigTest;

var counter: usize = 0;
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
Expand Down
6 changes: 6 additions & 0 deletions lib/std/start.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,12 @@ fn posixCallMainAndExit() noreturn {
// Find the beginning of the auxiliary vector
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
std.os.linux.elf_aux_maybe = auxv;

// Do this as early as possible, the aux vector is needed
if (builtin.position_independent_executable) {
@import("os/linux/start_pie.zig").apply_relocations();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to pass in auxv instead of using it indirectly via the global?

}

// Initialize the TLS area
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");

Expand Down
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1922,6 +1922,12 @@ enum WantPIC {
WantPICEnabled,
};

enum WantPIE {
WantPIEAuto,
WantPIEDisabled,
WantPIEEnabled,
};

enum WantStackCheck {
WantStackCheckAuto,
WantStackCheckDisabled,
Expand DownExpand Up@@ -2101,6 +2107,7 @@ struct CodeGen {
Stage2ProgressNode *sub_progress_node;

WantPIC want_pic;
WantPIE want_pie;
WantStackCheck want_stack_check;
WantCSanitize want_sanitize_c;
CacheHash cache_hash;
Expand DownExpand Up@@ -2175,6 +2182,7 @@ struct CodeGen {
bool disable_gen_h;
bool bundle_compiler_rt;
bool have_pic;
bool have_pie;
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool have_sanitize_c;
Expand Down
Loading