authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-05 12:14:53+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-05 13:00:20+02:00
log07dfccf9674c2f7196c31b25813ea9e3e7e1dad6
treea67df2a3aaaf6ff77cf80a9e1fa96d2dbe355498
parente4825dbd771906722508933bb355e342b992869b

Review


3 files changed, 45 insertions(+), 36 deletions(-)

std/c/linux.zig+3-1
......@@ -1,3 +1,4 @@
1const linux = @import("../os/linux.zig");
12pub use @import("../os/linux/errno.zig");
23
34pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
......@@ -12,4 +13,5 @@ pub const pthread_attr_t = extern struct {
1213/// See std.elf for constants for this
1314pub extern fn getauxval(__type: c_ulong) c_ulong;
1415
15pub extern fn dl_iterate_phdr(callback: *const c_void, data: ?*c_void) i32;
16pub const dl_iterate_phdr_callback = extern fn (info: *linux.dl_phdr_info, size: usize, data: ?*c_void) c_int;
17pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
std/dynamic_library.zig+19-15
......@@ -19,25 +19,29 @@ pub const DynLib = switch (builtin.os) {
1919 else => void,
2020};
2121
22// The link_map structure is not completely specified beside the fields
23// reported below, any libc is free to store additional data in the remaining
24// space.
25// An iterator is provided in order to traverse the linked list in a idiomatic
26// fashion.
2227const LinkMap = extern struct {
2328 l_addr: usize,
24 l_name: [*]u8,
25 l_ld: [*c]elf.Dyn,
26 l_next: [*c]LinkMap,
27 l_prev: [*c]LinkMap,
29 l_name: [*]const u8,
30 l_ld: ?*elf.Dyn,
31 l_next: ?*LinkMap,
32 l_prev: ?*LinkMap,
2833
2934 pub const Iterator = struct {
30 lm_ptr: [*c]LinkMap,
35 current: ?*LinkMap,
3136
32 fn end(self: *const Iterator) bool {
33 return self.lm_ptr == 0;
37 fn end(self: *Iterator) bool {
38 return self.current == null;
3439 }
3540
3641 fn next(self: *Iterator) ?*LinkMap {
37 if (self.lm_ptr != 0) {
38 const ptr = self.lm_ptr;
39 self.lm_ptr = self.lm_ptr.*.l_next;
40 return ptr;
42 if (self.current) |it| {
43 self.current = it.l_next;
44 return it;
4145 }
4246 return null;
4347 }
......@@ -46,7 +50,7 @@ const LinkMap = extern struct {
4650
4751const RDebug = extern struct {
4852 r_version: i32,
49 r_map: [*c]LinkMap,
53 r_map: ?*LinkMap,
5054 r_brk: usize,
5155 r_ldbase: usize,
5256};
......@@ -72,7 +76,7 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
7276 }
7377 // No PT_DYNAMIC means this is either a statically-linked program or a
7478 // badly corrupted one
75 return LinkMap.Iterator{.lm_ptr = 0};
79 return LinkMap.Iterator{.current = null};
7680 };
7781
7882 const link_map_ptr = init: {
......@@ -87,7 +91,7 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
8791 const got_table = @intToPtr([*]usize, dyn.d_un.d_ptr);
8892 // The address to the link_map structure is stored in the
8993 // second slot
90 break :init @intToPtr([*c]LinkMap, got_table[1]);
94 break :init @intToPtr(?*LinkMap, got_table[1]);
9195 },
9296 else => { }
9397 }
......@@ -95,7 +99,7 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
9599 return error.InvalidExe;
96100 };
97101
98 return LinkMap.Iterator{.lm_ptr = link_map_ptr};
102 return LinkMap.Iterator{.current = link_map_ptr};
99103}
100104
101105pub const LinuxDynLib = struct {
std/os/linux.zig+23-20
......@@ -4,6 +4,7 @@ const builtin = @import("builtin");
44const maxInt = std.math.maxInt;
55const elf = std.elf;
66const vdso = @import("linux/vdso.zig");
7const dl = @import("../dynamic_library.zig");
78pub use switch (builtin.arch) {
89 builtin.Arch.x86_64 => @import("linux/x86_64.zig"),
910 builtin.Arch.i386 => @import("linux/i386.zig"),
......@@ -1537,33 +1538,32 @@ pub const dirent64 = extern struct {
15371538
15381539pub const dl_phdr_info = extern struct {
15391540 dlpi_addr: usize,
1540 dlpi_name: [*c]const u8,
1541 dlpi_phdr: [*c]*c_void,
1541 dlpi_name: ?[*]const u8,
1542 dlpi_phdr: [*]elf.Phdr,
15421543 dlpi_phnum: u16,
15431544};
15441545
1545const DynLib = @import("../dynamic_library.zig");
1546
15471546// XXX: This should be weak
15481547extern const __ehdr_start: elf.Ehdr = undefined;
15491548
1550pub fn dl_iterate_phdr(callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) i32, data: ?*c_void) isize {
1549pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
15511550 if (builtin.link_libc) {
1552 return std.c.dl_iterate_phdr(@ptrCast(*const c_void, callback), data);
1551 return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data));
15531552 }
15541553
15551554 const elf_base = @ptrToInt(&__ehdr_start);
15561555 const n_phdr = __ehdr_start.e_phnum;
15571556 const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr];
15581557
1559 var it = DynLib.linkmap_iterator(phdrs) catch return 0;
1558 var it = dl.linkmap_iterator(phdrs) catch return 0;
1559
1560 // The executable has no dynamic link segment, create a single entry for
1561 // the whole ELF image
15601562 if (it.end()) {
1561 // The executable has no dynamic link infos, create a single info
1562 // struct for the whole ELF image
15631563 var info = dl_phdr_info{
15641564 .dlpi_addr = elf_base,
15651565 .dlpi_name = c"/proc/self/exe",
1566 .dlpi_phdr = @intToPtr([*c]*c_void, elf_base + __ehdr_start.e_phoff),
1566 .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff),
15671567 .dlpi_phnum = __ehdr_start.e_phnum,
15681568 };
15691569
......@@ -1573,23 +1573,26 @@ pub fn dl_iterate_phdr(callback: extern fn (info: *dl_phdr_info, size: usize, da
15731573 // Last return value from the callback function
15741574 var last_r: isize = 0;
15751575 while (it.next()) |entry| {
1576 var info = dl_phdr_info{
1577 .dlpi_addr = entry.l_addr,
1578 .dlpi_name = entry.l_name,
1579 .dlpi_phdr = 0,
1580 .dlpi_phnum = 0,
1581 };
1576 var dlpi_phdr: usize = undefined;
1577 var dlpi_phnum: u16 = undefined;
15821578
15831579 if (entry.l_addr != 0) {
15841580 const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr);
1585 info.dlpi_phdr = @intToPtr([*c]*c_void, entry.l_addr + elf_header.e_phoff);
1586 info.dlpi_phnum = elf_header.e_phnum;
1581 dlpi_phdr = entry.l_addr + elf_header.e_phoff;
1582 dlpi_phnum = elf_header.e_phnum;
15871583 } else {
15881584 // This is the running ELF image
1589 info.dlpi_phdr = @intToPtr([*c]*c_void, elf_base + __ehdr_start.e_phoff);
1590 info.dlpi_phnum = __ehdr_start.e_phnum;
1585 dlpi_phdr = elf_base + __ehdr_start.e_phoff;
1586 dlpi_phnum = __ehdr_start.e_phnum;
15911587 }
15921588
1589 var info = dl_phdr_info{
1590 .dlpi_addr = entry.l_addr,
1591 .dlpi_name = entry.l_name,
1592 .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr),
1593 .dlpi_phnum = dlpi_phnum,
1594 };
1595
15931596 last_r = callback(&info, @sizeOf(dl_phdr_info), data);
15941597 if (last_r != 0) break;
15951598 }