2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2021-10-05 07:47:27 +01:00
|
|
|
const builtin = @import("builtin");
|
2018-06-16 22:01:23 +01:00
|
|
|
const mem = std.mem;
|
2018-09-27 22:51:22 +01:00
|
|
|
const os = std.os;
|
|
|
|
const assert = std.debug.assert;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2018-09-27 22:51:22 +01:00
|
|
|
const elf = std.elf;
|
2019-05-27 04:35:26 +01:00
|
|
|
const windows = std.os.windows;
|
2019-05-31 02:03:56 +01:00
|
|
|
const system = std.os.system;
|
2018-10-26 19:59:58 +01:00
|
|
|
const maxInt = std.math.maxInt;
|
2020-02-05 20:31:09 +00:00
|
|
|
const max = std.math.max;
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2020-02-25 06:52:27 +00:00
|
|
|
pub const DynLib = switch (builtin.os.tag) {
|
2020-02-05 20:31:09 +00:00
|
|
|
.linux => if (builtin.link_libc) DlDynlib else ElfDynLib,
|
2019-05-25 03:52:07 +01:00
|
|
|
.windows => WindowsDynLib,
|
2021-08-31 13:21:23 +01:00
|
|
|
.macos, .tvos, .watchos, .ios, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris => DlDynlib,
|
2018-09-27 06:18:47 +01:00
|
|
|
else => void,
|
|
|
|
};
|
|
|
|
|
2019-05-05 11:14:53 +01:00
|
|
|
// The link_map structure is not completely specified beside the fields
|
|
|
|
// reported below, any libc is free to store additional data in the remaining
|
|
|
|
// space.
|
|
|
|
// An iterator is provided in order to traverse the linked list in a idiomatic
|
|
|
|
// fashion.
|
2019-04-24 19:54:17 +01:00
|
|
|
const LinkMap = extern struct {
|
|
|
|
l_addr: usize,
|
2020-01-14 22:11:10 +00:00
|
|
|
l_name: [*:0]const u8,
|
2019-05-05 11:14:53 +01:00
|
|
|
l_ld: ?*elf.Dyn,
|
|
|
|
l_next: ?*LinkMap,
|
|
|
|
l_prev: ?*LinkMap,
|
2019-04-24 19:54:17 +01:00
|
|
|
|
|
|
|
pub const Iterator = struct {
|
2019-05-05 11:14:53 +01:00
|
|
|
current: ?*LinkMap,
|
2019-04-24 19:54:17 +01:00
|
|
|
|
2020-04-04 03:04:40 +01:00
|
|
|
pub fn end(self: *Iterator) bool {
|
2019-05-05 11:14:53 +01:00
|
|
|
return self.current == null;
|
2019-04-24 19:54:17 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 03:04:40 +01:00
|
|
|
pub fn next(self: *Iterator) ?*LinkMap {
|
2019-05-05 11:14:53 +01:00
|
|
|
if (self.current) |it| {
|
|
|
|
self.current = it.l_next;
|
|
|
|
return it;
|
2019-04-24 19:54:17 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const RDebug = extern struct {
|
|
|
|
r_version: i32,
|
2019-05-05 11:14:53 +01:00
|
|
|
r_map: ?*LinkMap,
|
2019-04-24 19:54:17 +01:00
|
|
|
r_brk: usize,
|
|
|
|
r_ldbase: usize,
|
|
|
|
};
|
|
|
|
|
2020-11-25 02:51:24 +00:00
|
|
|
/// TODO make it possible to reference this same external symbol 2x so we don't need this
|
|
|
|
/// helper function.
|
|
|
|
pub fn get_DYNAMIC() ?[*]elf.Dyn {
|
|
|
|
return @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .Weak });
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:54:17 +01:00
|
|
|
pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
|
2021-06-20 02:10:22 +01:00
|
|
|
_ = phdrs;
|
2020-11-25 02:51:24 +00:00
|
|
|
const _DYNAMIC = get_DYNAMIC() orelse {
|
2019-04-24 19:54:17 +01:00
|
|
|
// No PT_DYNAMIC means this is either a statically-linked program or a
|
2020-11-23 11:00:12 +00:00
|
|
|
// badly corrupted dynamically-linked one.
|
2019-05-12 17:56:01 +01:00
|
|
|
return LinkMap.Iterator{ .current = null };
|
2020-11-23 11:00:12 +00:00
|
|
|
};
|
2019-04-24 19:54:17 +01:00
|
|
|
|
|
|
|
const link_map_ptr = init: {
|
2019-12-17 14:07:19 +00:00
|
|
|
var i: usize = 0;
|
|
|
|
while (_DYNAMIC[i].d_tag != elf.DT_NULL) : (i += 1) {
|
|
|
|
switch (_DYNAMIC[i].d_tag) {
|
2019-04-24 19:54:17 +01:00
|
|
|
elf.DT_DEBUG => {
|
2020-11-22 19:39:44 +00:00
|
|
|
const ptr = @intToPtr(?*RDebug, _DYNAMIC[i].d_val);
|
2019-12-17 14:07:19 +00:00
|
|
|
if (ptr) |r_debug| {
|
|
|
|
if (r_debug.r_version != 1) return error.InvalidExe;
|
|
|
|
break :init r_debug.r_map;
|
|
|
|
}
|
2019-04-24 19:54:17 +01:00
|
|
|
},
|
|
|
|
elf.DT_PLTGOT => {
|
2020-11-22 19:39:44 +00:00
|
|
|
const ptr = @intToPtr(?[*]usize, _DYNAMIC[i].d_val);
|
2019-12-17 14:07:19 +00:00
|
|
|
if (ptr) |got_table| {
|
|
|
|
// The address to the link_map structure is stored in
|
|
|
|
// the second slot
|
|
|
|
break :init @intToPtr(?*LinkMap, got_table[1]);
|
|
|
|
}
|
2019-04-24 19:54:17 +01:00
|
|
|
},
|
2019-05-12 17:56:01 +01:00
|
|
|
else => {},
|
2019-04-24 19:54:17 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 14:07:19 +00:00
|
|
|
return LinkMap.Iterator{ .current = null };
|
2019-04-24 19:54:17 +01:00
|
|
|
};
|
|
|
|
|
2019-05-12 17:56:01 +01:00
|
|
|
return LinkMap.Iterator{ .current = link_map_ptr };
|
2019-04-24 19:54:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
pub const ElfDynLib = struct {
|
|
|
|
strings: [*:0]u8,
|
|
|
|
syms: [*]elf.Sym,
|
|
|
|
hashtab: [*]os.Elf_Symndx,
|
|
|
|
versym: ?[*]u16,
|
|
|
|
verdef: ?*elf.Verdef,
|
2019-05-27 04:35:26 +01:00
|
|
|
memory: []align(mem.page_size) u8,
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2019-05-31 02:03:56 +01:00
|
|
|
pub const Error = error{
|
2022-05-22 16:06:59 +01:00
|
|
|
FileTooBig,
|
2019-05-31 02:03:56 +01:00
|
|
|
NotElfFile,
|
|
|
|
NotDynamicLibrary,
|
|
|
|
MissingDynamicLinkingInformation,
|
|
|
|
ElfStringSectionNotFound,
|
|
|
|
ElfSymSectionNotFound,
|
|
|
|
ElfHashTableNotFound,
|
|
|
|
};
|
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
/// Trusts the file. Malicious file will be able to execute arbitrary code.
|
|
|
|
pub fn open(path: []const u8) !ElfDynLib {
|
2021-08-25 08:00:39 +01:00
|
|
|
const fd = try os.open(path, 0, os.O.RDONLY | os.O.CLOEXEC);
|
2020-02-05 20:31:09 +00:00
|
|
|
defer os.close(fd);
|
|
|
|
|
|
|
|
const stat = try os.fstat(fd);
|
2022-05-22 16:06:59 +01:00
|
|
|
const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig;
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
// This one is to read the ELF info. We do more mmapping later
|
|
|
|
// corresponding to the actual LOAD sections.
|
|
|
|
const file_bytes = try os.mmap(
|
|
|
|
null,
|
|
|
|
mem.alignForward(size, mem.page_size),
|
2021-08-25 08:00:39 +01:00
|
|
|
os.PROT.READ,
|
|
|
|
os.MAP.PRIVATE,
|
2020-02-05 20:31:09 +00:00
|
|
|
fd,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
defer os.munmap(file_bytes);
|
|
|
|
|
|
|
|
const eh = @ptrCast(*elf.Ehdr, file_bytes.ptr);
|
2022-04-04 13:33:24 +01:00
|
|
|
if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
|
2019-11-26 18:12:57 +00:00
|
|
|
if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
const elf_addr = @ptrToInt(file_bytes.ptr);
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
// Iterate over the program header entries to find out the
|
|
|
|
// dynamic vector as well as the total size of the virtual memory.
|
2018-06-16 22:01:23 +01:00
|
|
|
var maybe_dynv: ?[*]usize = null;
|
2020-02-05 20:31:09 +00:00
|
|
|
var virt_addr_end: usize = 0;
|
2018-06-16 22:01:23 +01:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
2020-02-05 20:31:09 +00:00
|
|
|
var ph_addr: usize = elf_addr + eh.e_phoff;
|
2018-06-16 22:01:23 +01:00
|
|
|
while (i < eh.e_phnum) : ({
|
|
|
|
i += 1;
|
|
|
|
ph_addr += eh.e_phentsize;
|
|
|
|
}) {
|
|
|
|
const ph = @intToPtr(*elf.Phdr, ph_addr);
|
|
|
|
switch (ph.p_type) {
|
2020-02-05 20:31:09 +00:00
|
|
|
elf.PT_LOAD => virt_addr_end = max(virt_addr_end, ph.p_vaddr + ph.p_memsz),
|
2018-06-16 22:01:23 +01:00
|
|
|
elf.PT_DYNAMIC => maybe_dynv = @intToPtr([*]usize, elf_addr + ph.p_offset),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const dynv = maybe_dynv orelse return error.MissingDynamicLinkingInformation;
|
2020-02-05 20:31:09 +00:00
|
|
|
|
2021-08-25 08:00:39 +01:00
|
|
|
// Reserve the entire range (with no permissions) so that we can do MAP.FIXED below.
|
2020-02-05 20:31:09 +00:00
|
|
|
const all_loaded_mem = try os.mmap(
|
|
|
|
null,
|
|
|
|
virt_addr_end,
|
2021-08-25 08:00:39 +01:00
|
|
|
os.PROT.NONE,
|
|
|
|
os.MAP.PRIVATE | os.MAP.ANONYMOUS,
|
2020-02-05 20:31:09 +00:00
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
errdefer os.munmap(all_loaded_mem);
|
|
|
|
|
|
|
|
const base = @ptrToInt(all_loaded_mem.ptr);
|
|
|
|
|
|
|
|
// Now iterate again and actually load all the program sections.
|
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
var ph_addr: usize = elf_addr + eh.e_phoff;
|
|
|
|
while (i < eh.e_phnum) : ({
|
|
|
|
i += 1;
|
|
|
|
ph_addr += eh.e_phentsize;
|
|
|
|
}) {
|
|
|
|
const ph = @intToPtr(*elf.Phdr, ph_addr);
|
|
|
|
switch (ph.p_type) {
|
|
|
|
elf.PT_LOAD => {
|
|
|
|
// The VirtAddr may not be page-aligned; in such case there will be
|
|
|
|
// extra nonsense mapped before/after the VirtAddr,MemSiz
|
|
|
|
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, mem.page_size) - 1);
|
|
|
|
const extra_bytes = (base + ph.p_vaddr) - aligned_addr;
|
|
|
|
const extended_memsz = mem.alignForward(ph.p_memsz + extra_bytes, mem.page_size);
|
|
|
|
const ptr = @intToPtr([*]align(mem.page_size) u8, aligned_addr);
|
|
|
|
const prot = elfToMmapProt(ph.p_flags);
|
|
|
|
if ((ph.p_flags & elf.PF_W) == 0) {
|
|
|
|
// If it does not need write access, it can be mapped from the fd.
|
|
|
|
_ = try os.mmap(
|
|
|
|
ptr,
|
|
|
|
extended_memsz,
|
|
|
|
prot,
|
2021-08-25 08:00:39 +01:00
|
|
|
os.MAP.PRIVATE | os.MAP.FIXED,
|
2020-02-05 20:31:09 +00:00
|
|
|
fd,
|
|
|
|
ph.p_offset - extra_bytes,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const sect_mem = try os.mmap(
|
|
|
|
ptr,
|
|
|
|
extended_memsz,
|
|
|
|
prot,
|
2021-08-25 08:00:39 +01:00
|
|
|
os.MAP.PRIVATE | os.MAP.FIXED | os.MAP.ANONYMOUS,
|
2020-02-05 20:31:09 +00:00
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
mem.copy(u8, sect_mem, file_bytes[0..ph.p_filesz]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-16 22:01:23 +01:00
|
|
|
|
2019-11-25 05:30:28 +00:00
|
|
|
var maybe_strings: ?[*:0]u8 = null;
|
2018-06-16 22:01:23 +01:00
|
|
|
var maybe_syms: ?[*]elf.Sym = null;
|
2019-05-25 03:52:07 +01:00
|
|
|
var maybe_hashtab: ?[*]os.Elf_Symndx = null;
|
2018-06-16 22:01:23 +01:00
|
|
|
var maybe_versym: ?[*]u16 = null;
|
|
|
|
var maybe_verdef: ?*elf.Verdef = null;
|
|
|
|
|
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (dynv[i] != 0) : (i += 2) {
|
|
|
|
const p = base + dynv[i + 1];
|
|
|
|
switch (dynv[i]) {
|
2019-11-25 05:30:28 +00:00
|
|
|
elf.DT_STRTAB => maybe_strings = @intToPtr([*:0]u8, p),
|
2018-06-16 22:01:23 +01:00
|
|
|
elf.DT_SYMTAB => maybe_syms = @intToPtr([*]elf.Sym, p),
|
2019-05-25 03:52:07 +01:00
|
|
|
elf.DT_HASH => maybe_hashtab = @intToPtr([*]os.Elf_Symndx, p),
|
2018-06-16 22:01:23 +01:00
|
|
|
elf.DT_VERSYM => maybe_versym = @intToPtr([*]u16, p),
|
|
|
|
elf.DT_VERDEF => maybe_verdef = @intToPtr(*elf.Verdef, p),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
return ElfDynLib{
|
|
|
|
.memory = all_loaded_mem,
|
2018-06-16 22:01:23 +01:00
|
|
|
.strings = maybe_strings orelse return error.ElfStringSectionNotFound,
|
|
|
|
.syms = maybe_syms orelse return error.ElfSymSectionNotFound,
|
|
|
|
.hashtab = maybe_hashtab orelse return error.ElfHashTableNotFound,
|
|
|
|
.versym = maybe_versym,
|
|
|
|
.verdef = maybe_verdef,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-05 20:31:09 +00:00
|
|
|
/// Trusts the file. Malicious file will be able to execute arbitrary code.
|
2020-03-30 19:23:22 +01:00
|
|
|
pub fn openZ(path_c: [*:0]const u8) !ElfDynLib {
|
2021-11-30 07:13:07 +00:00
|
|
|
return open(mem.sliceTo(path_c, 0));
|
2020-02-05 20:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trusts the file
|
|
|
|
pub fn close(self: *ElfDynLib) void {
|
|
|
|
os.munmap(self.memory);
|
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lookup(self: *ElfDynLib, comptime T: type, name: [:0]const u8) ?T {
|
|
|
|
if (self.lookupAddress("", name)) |symbol| {
|
|
|
|
return @intToPtr(T, symbol);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 22:01:23 +01:00
|
|
|
/// Returns the address of the symbol
|
2020-02-05 20:31:09 +00:00
|
|
|
pub fn lookupAddress(self: *const ElfDynLib, vername: []const u8, name: []const u8) ?usize {
|
2018-06-16 22:01:23 +01:00
|
|
|
const maybe_versym = if (self.verdef == null) null else self.versym;
|
|
|
|
|
|
|
|
const OK_TYPES = (1 << elf.STT_NOTYPE | 1 << elf.STT_OBJECT | 1 << elf.STT_FUNC | 1 << elf.STT_COMMON);
|
|
|
|
const OK_BINDS = (1 << elf.STB_GLOBAL | 1 << elf.STB_WEAK | 1 << elf.STB_GNU_UNIQUE);
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < self.hashtab[1]) : (i += 1) {
|
2019-11-07 04:25:57 +00:00
|
|
|
if (0 == (@as(u32, 1) << @intCast(u5, self.syms[i].st_info & 0xf) & OK_TYPES)) continue;
|
|
|
|
if (0 == (@as(u32, 1) << @intCast(u5, self.syms[i].st_info >> 4) & OK_BINDS)) continue;
|
2018-06-16 22:01:23 +01:00
|
|
|
if (0 == self.syms[i].st_shndx) continue;
|
2021-11-30 07:13:07 +00:00
|
|
|
if (!mem.eql(u8, name, mem.sliceTo(self.strings + self.syms[i].st_name, 0))) continue;
|
2018-06-16 22:01:23 +01:00
|
|
|
if (maybe_versym) |versym| {
|
|
|
|
if (!checkver(self.verdef.?, versym[i], vername, self.strings))
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-05 20:31:09 +00:00
|
|
|
return @ptrToInt(self.memory.ptr) + self.syms[i].st_value;
|
2018-06-16 22:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-05 20:31:09 +00:00
|
|
|
|
|
|
|
fn elfToMmapProt(elf_prot: u64) u32 {
|
2021-08-25 08:00:39 +01:00
|
|
|
var result: u32 = os.PROT.NONE;
|
|
|
|
if ((elf_prot & elf.PF_R) != 0) result |= os.PROT.READ;
|
|
|
|
if ((elf_prot & elf.PF_W) != 0) result |= os.PROT.WRITE;
|
|
|
|
if ((elf_prot & elf.PF_X) != 0) result |= os.PROT.EXEC;
|
2020-02-05 20:31:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-06-16 22:01:23 +01:00
|
|
|
};
|
|
|
|
|
2019-11-25 05:30:28 +00:00
|
|
|
fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [*:0]u8) bool {
|
2018-06-16 22:01:23 +01:00
|
|
|
var def = def_arg;
|
|
|
|
const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
|
|
|
|
while (true) {
|
|
|
|
if (0 == (def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
|
|
|
|
break;
|
|
|
|
if (def.vd_next == 0)
|
|
|
|
return false;
|
|
|
|
def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next);
|
|
|
|
}
|
|
|
|
const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux);
|
2021-11-30 07:13:07 +00:00
|
|
|
return mem.eql(u8, vername, mem.sliceTo(strings + aux.vda_name, 0));
|
2018-06-16 22:01:23 +01:00
|
|
|
}
|
2018-09-27 06:18:47 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const WindowsDynLib = struct {
|
2019-05-31 02:03:56 +01:00
|
|
|
pub const Error = error{FileNotFound};
|
|
|
|
|
2018-09-27 06:18:47 +01:00
|
|
|
dll: windows.HMODULE,
|
|
|
|
|
2019-05-27 16:12:07 +01:00
|
|
|
pub fn open(path: []const u8) !WindowsDynLib {
|
2019-12-10 17:28:28 +00:00
|
|
|
const path_w = try windows.sliceToPrefixedFileW(path);
|
2020-05-02 06:25:22 +01:00
|
|
|
return openW(path_w.span().ptr);
|
2019-12-10 17:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
pub fn openZ(path_c: [*:0]const u8) !WindowsDynLib {
|
2020-01-18 22:15:42 +00:00
|
|
|
const path_w = try windows.cStrToPrefixedFileW(path_c);
|
2020-05-02 06:25:22 +01:00
|
|
|
return openW(path_w.span().ptr);
|
2019-12-10 17:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openW(path_w: [*:0]const u16) !WindowsDynLib {
|
2020-11-27 18:21:29 +00:00
|
|
|
var offset: usize = 0;
|
|
|
|
if (path_w[0] == '\\' and path_w[1] == '?' and path_w[2] == '?' and path_w[3] == '\\') {
|
2019-12-10 18:21:37 +00:00
|
|
|
// + 4 to skip over the \??\
|
2020-11-27 18:21:29 +00:00
|
|
|
offset = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WindowsDynLib{
|
|
|
|
.dll = try windows.LoadLibraryW(path_w + offset),
|
2018-09-27 06:18:47 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self: *WindowsDynLib) void {
|
2019-05-27 04:35:26 +01:00
|
|
|
windows.FreeLibrary(self.dll);
|
2018-09-27 06:18:47 +01:00
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:56:05 +00:00
|
|
|
pub fn lookup(self: *WindowsDynLib, comptime T: type, name: [:0]const u8) ?T {
|
2019-12-10 17:28:28 +00:00
|
|
|
if (windows.kernel32.GetProcAddress(self.dll, name.ptr)) |addr| {
|
2019-05-31 02:03:56 +01:00
|
|
|
return @ptrCast(T, addr);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const DlDynlib = struct {
|
|
|
|
pub const Error = error{FileNotFound};
|
|
|
|
|
2021-12-19 05:24:45 +00:00
|
|
|
handle: *anyopaque,
|
2019-05-31 02:03:56 +01:00
|
|
|
|
|
|
|
pub fn open(path: []const u8) !DlDynlib {
|
2019-12-10 17:28:28 +00:00
|
|
|
const path_c = try os.toPosixPath(path);
|
2020-03-30 19:23:22 +01:00
|
|
|
return openZ(&path_c);
|
2019-12-10 17:28:28 +00:00
|
|
|
}
|
2019-05-31 02:03:56 +01:00
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
pub fn openZ(path_c: [*:0]const u8) !DlDynlib {
|
2019-05-31 02:03:56 +01:00
|
|
|
return DlDynlib{
|
2021-08-25 08:00:39 +01:00
|
|
|
.handle = system.dlopen(path_c, system.RTLD.LAZY) orelse {
|
2019-05-31 02:03:56 +01:00
|
|
|
return error.FileNotFound;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self: *DlDynlib) void {
|
|
|
|
_ = system.dlclose(self.handle);
|
|
|
|
self.* = undefined;
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:56:05 +00:00
|
|
|
pub fn lookup(self: *DlDynlib, comptime T: type, name: [:0]const u8) ?T {
|
2019-12-10 17:28:28 +00:00
|
|
|
// dlsym (and other dl-functions) secretly take shadow parameter - return address on stack
|
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826
|
|
|
|
if (@call(.{ .modifier = .never_tail }, system.dlsym, .{ self.handle, name.ptr })) |symbol| {
|
2019-05-31 02:03:56 +01:00
|
|
|
return @ptrCast(T, symbol);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 06:18:47 +01:00
|
|
|
};
|
2018-09-27 22:51:22 +01:00
|
|
|
|
|
|
|
test "dynamic_library" {
|
2020-02-25 06:52:27 +00:00
|
|
|
const libname = switch (builtin.os.tag) {
|
2020-10-11 09:23:36 +01:00
|
|
|
.linux, .freebsd, .openbsd => "invalid_so.so",
|
2019-05-25 03:52:07 +01:00
|
|
|
.windows => "invalid_dll.dll",
|
2020-10-12 09:59:43 +01:00
|
|
|
.macos, .tvos, .watchos, .ios => "invalid_dylib.dylib",
|
2019-12-10 17:28:28 +00:00
|
|
|
else => return error.SkipZigTest,
|
2018-09-27 22:51:22 +01:00
|
|
|
};
|
|
|
|
|
2021-06-10 02:35:42 +01:00
|
|
|
_ = DynLib.open(libname) catch |err| {
|
2021-05-04 18:47:26 +01:00
|
|
|
try testing.expect(err == error.FileNotFound);
|
2018-09-27 22:51:22 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
}
|