Skip to content

Commit

Permalink
Merge pull request #15823 from kcbanner/dwarf_unwind
Browse files Browse the repository at this point in the history
Add DWARF unwinding, and an external debug info loader for ELF
  • Loading branch information
kubkon authored Jul 21, 2023
2 parents c43ee5b + b1d86db commit 61d5b7c
Show file tree
Hide file tree
Showing 29 changed files with 5,603 additions and 515 deletions.
7 changes: 7 additions & 0 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ pub extern "c" fn timer_delete(timerid: c.timer_t) c_int;
pub extern "c" fn timer_settime(timerid: c.timer_t, flags: c_int, new_value: *const c.itimerspec, old_value: *c.itimerspec) c_int;
pub extern "c" fn timer_gettime(timerid: c.timer_t, flags: c_int, curr_value: *c.itimerspec) c_int;

pub usingnamespace if (builtin.os.tag == .linux and builtin.target.isMusl()) struct {
// musl does not implement getcontext
pub const getcontext = std.os.linux.getcontext;
} else struct {
pub extern "c" fn getcontext(ucp: *std.os.ucontext_t) c_int;
};

pub const max_align_t = if (builtin.abi == .msvc)
f64
else if (builtin.target.isDarwin())
Expand Down
6 changes: 2 additions & 4 deletions lib/std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ pub const ucontext_t = extern struct {
link: ?*ucontext_t,
mcsize: u64,
mcontext: *mcontext_t,
__mcontext_data: mcontext_t,
};

pub const mcontext_t = extern struct {
es: arch_bits.exception_state,
ss: arch_bits.thread_state,
};
pub const mcontext_t = arch_bits.mcontext_t;

extern "c" fn __error() *c_int;
pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32;
Expand Down
13 changes: 13 additions & 0 deletions lib/std/c/darwin/aarch64.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// See C headers in
// lib/libc/include/aarch64-macos.12-gnu/mach/arm/_structs.h
// lib/libc/include/aarch64-macos.13-none/arm/_mcontext.h

pub const mcontext_t = extern struct {
es: exception_state,
ss: thread_state,
ns: neon_state,
};

pub const exception_state = extern struct {
far: u64, // Virtual Fault Address
Expand All @@ -17,6 +24,12 @@ pub const thread_state = extern struct {
__pad: u32,
};

pub const neon_state = extern struct {
q: [32]u128,
fpsr: u32,
fpcr: u32,
};

pub const EXC_TYPES_COUNT = 14;
pub const EXC_MASK_MACHINE = 0;

Expand Down
29 changes: 29 additions & 0 deletions lib/std/c/darwin/x86_64.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const c = @import("../darwin.zig");

pub const mcontext_t = extern struct {
es: exception_state,
ss: thread_state,
fs: float_state,
};

pub const exception_state = extern struct {
trapno: u16,
cpu: u16,
Expand Down Expand Up @@ -31,6 +37,29 @@ pub const thread_state = extern struct {
gs: u64,
};

const stmm_reg = [16]u8;
const xmm_reg = [16]u8;
pub const float_state = extern struct {
reserved: [2]c_int,
fcw: u16,
fsw: u16,
ftw: u8,
rsrv1: u8,
fop: u16,
ip: u32,
cs: u16,
rsrv2: u16,
dp: u32,
ds: u16,
rsrv3: u16,
mxcsr: u32,
mxcsrmask: u32,
stmm: [8]stmm_reg,
xmm: [16]xmm_reg,
rsrv4: [96]u8,
reserved1: c_int,
};

pub const THREAD_STATE = 4;
pub const THREAD_STATE_COUNT: c.mach_msg_type_number_t = @sizeOf(thread_state) / @sizeOf(c_int);

Expand Down
13 changes: 8 additions & 5 deletions lib/std/coff.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,11 @@ pub const Coff = struct {
return Strtab{ .buffer = self.data[offset..][0..size] };
}

pub fn strtabRequired(self: *const Coff) bool {
for (self.getSectionHeaders()) |*sect_hdr| if (sect_hdr.getName() == null) return true;
return false;
}

pub fn getSectionHeaders(self: *const Coff) []align(1) const SectionHeader {
const coff_header = self.getCoffHeader();
const offset = self.coff_header_offset + @sizeOf(CoffHeader) + coff_header.size_of_optional_header;
Expand Down Expand Up @@ -1248,14 +1253,12 @@ pub const Coff = struct {
return null;
}

pub fn getSectionData(self: *const Coff, comptime name: []const u8) ![]const u8 {
const sec = self.getSectionByName(name) orelse return error.MissingCoffSection;
pub fn getSectionData(self: *const Coff, sec: *align(1) const SectionHeader) []const u8 {
return self.data[sec.pointer_to_raw_data..][0..sec.virtual_size];
}

// Return an owned slice full of the section data
pub fn getSectionDataAlloc(self: *const Coff, comptime name: []const u8, allocator: mem.Allocator) ![]u8 {
const section_data = try self.getSectionData(name);
pub fn getSectionDataAlloc(self: *const Coff, sec: *align(1) const SectionHeader, allocator: mem.Allocator) ![]u8 {
const section_data = self.getSectionData(sec);
return allocator.dupe(u8, section_data);
}
};
Expand Down
Loading

0 comments on commit 61d5b7c

Please sign in to comment.