libdyn: Update for zig=0.11

Zig=0.11 renamed bunch of builtin functions, and while "zig fmt" tried to
update code automatically, some manual labour was necessary. Not amused.
This commit is contained in:
Dmitry Bogatov 2023-12-10 01:35:54 -05:00
parent fe6cd2668c
commit 9e639658e2

View File

@ -5,6 +5,7 @@
const std = @import("std");
const elf = std.elf;
const c = std.c;
const eq = std.math.Order.eq;
const debug = false;
@ -34,7 +35,7 @@ const MMap = struct {
if (debug) std.debug.print("offset = {d}\n", .{offset});
if (debug) std.debug.print("diff = {d}\n", .{diff});
if (debug) std.debug.print("real offset = {d}\n", .{@intCast(i64, offset - diff)});
if (debug) std.debug.print("real offset = {d}\n", .{@as(i64, @intCast(offset - diff))});
var ptr = c.mmap(
null,
@ -42,28 +43,28 @@ const MMap = struct {
prot,
mmap.MAP_PRIVATE,
fd,
@intCast(i64, offset - diff),
@as(i64, @intCast(offset - diff)),
);
if (ptr == mmap.MAP_FAILED) return null;
return MMap{
.ptr = @intToPtr(*align(8) anyopaque, @ptrToInt(ptr) + diff),
.ptr = @as(*align(8) anyopaque, @ptrFromInt(@intFromPtr(ptr) + diff)),
.size = size,
};
}
fn destroy(this: MMap) void {
// See note on [mmap offsets] in libdyn.h
var diff = @ptrToInt(this.ptr) & @as(u64, 4095);
var ptr = @intToPtr(*align(4096) anyopaque, @ptrToInt(this.ptr) - diff);
var diff = @intFromPtr(this.ptr) & @as(u64, 4095);
var ptr = @as(*align(4096) anyopaque, @ptrFromInt(@intFromPtr(this.ptr) - diff));
var size = this.size + diff;
_ = c.munmap(ptr, size);
}
fn pointer(this: MMap, type_: anytype) [*]type_ {
return @ptrCast([*]type_, this.ptr);
return @as([*]type_, @ptrCast(this.ptr));
}
fn save(this: MMap, type_: anytype, dest: *type_) void {
@ -73,7 +74,7 @@ const MMap = struct {
fn restore(type_: anytype, src: *type_) MMap {
return MMap{
.ptr = @intToPtr(*align(8) anyopaque, @ptrToInt(src.ptr)),
.ptr = @as(*align(8) anyopaque, @ptrFromInt(@intFromPtr(src.ptr))),
.size = src.size,
};
}
@ -83,7 +84,7 @@ pub export fn libdyn_open(lib: *self.libdyn_library, fd: c_int) c_int {
// TODO: add support for elf32.
var ehdr: elf.Elf64_Ehdr = undefined;
const size = @sizeOf(@TypeOf(ehdr));
var bytes = c.read(fd, @ptrCast([*]u8, &ehdr), size);
var bytes = c.read(fd, @as([*]u8, @ptrCast(&ehdr)), size);
if (bytes != size) {
if (debug) std.debug.print("Bytes {d} != size {d}", .{ bytes, size });
@ -119,11 +120,11 @@ pub export fn libdyn_open(lib: *self.libdyn_library, fd: c_int) c_int {
var dynsym: ?MMap = null;
var strtab: ?MMap = null;
for (shdrs[0..ehdr.e_shnum]) |shdr, ix| {
var name = @ptrCast([*:0]u8, shstrtab.ptr) + shdr.sh_name;
for (shdrs[0..ehdr.e_shnum], 0..) |shdr, ix| {
var name = @as([*:0]u8, @ptrCast(shstrtab.ptr)) + shdr.sh_name;
if (debug) std.debug.print("Section name : {s} (ix = {})\n", .{ name, ix });
if (std.cstr.cmp(name, ".plt") == 0 or std.cstr.cmp(name, ".rela.plt") == 0) {
if (std.mem.orderZ(u8, name, ".plt") == eq or std.mem.orderZ(u8, name, ".rela.plt") == eq) {
if (text) |x| x.destroy();
if (dynsym) |x| x.destroy();
if (strtab) |x| x.destroy();
@ -132,7 +133,7 @@ pub export fn libdyn_open(lib: *self.libdyn_library, fd: c_int) c_int {
// If there are more than one .text section, it probably won't work,
// but at least I will avoid memory leak with "text == null" check.
if (text == null and std.cstr.cmp(name, ".text") == 0) {
if (text == null and std.mem.orderZ(u8, name, ".text") == eq) {
if (debug) std.debug.print("text idx: {} offset = {}\n", .{ ix, shdr.sh_offset });
text = MMap.create(fd, shdr.sh_size, shdr.sh_offset, true);
lib.__offset = shdr.sh_offset;
@ -166,15 +167,15 @@ pub export fn libdyn_symbol(out: **anyopaque, lib: *const self.libdyn_library, s
if (debug) std.debug.print("count = {d}\n", .{count});
for (@intToPtr([*]elf.Elf64_Sym, @ptrToInt(lib.dynsym.ptr))[0..count]) |sym, ix| {
for (@as([*]elf.Elf64_Sym, @ptrFromInt(@intFromPtr(lib.dynsym.ptr)))[0..count], 0..) |sym, ix| {
if (debug) std.debug.print("st_name[{d}]: {d}\n", .{ ix, sym.st_name });
var name = @ptrCast([*:0]u8, lib.strtab.ptr) + sym.st_name;
var name = @as([*:0]u8, @ptrCast(lib.strtab.ptr)) + sym.st_name;
if (debug) std.debug.print("sym = {}\n", .{sym});
if (debug) std.debug.print("name = {s}\n", .{name});
if (std.cstr.cmp(name, symbol) != 0) continue;
if (std.mem.orderZ(u8, name, symbol) != eq) continue;
out.* = @intToPtr(*anyopaque, @ptrToInt(lib.text.ptr) + sym.st_value - lib.__offset);
out.* = @as(*anyopaque, @ptrFromInt(@intFromPtr(lib.text.ptr) + sym.st_value - lib.__offset));
return 0;
}