authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-07-06 17:20:05+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-07-08 18:08:25+02:00
logf74b21f0bd6919d4388b65e9cb88009da0a67859
tree29d6115351fe0f1eab22e905da6486f51a99f189
parent134c217a5bea8a7ac524d689e6d0ed34d12b4cef
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Elf2: fix targetLoad()/targetStore() to work with underaligned pointers


1 files changed, 10 insertions(+), 6 deletions(-)

src/link/Elf2.zig+10-6
...@@ -4108,27 +4108,31 @@ fn targetEndian(elf: *const Elf) std.lang.Endian {...@@ -4108,27 +4108,31 @@ fn targetEndian(elf: *const Elf) std.lang.Endian {
4108 };4108 };
4109}4109}
4110fn targetLoad(elf: *const Elf, ptr: anytype) @typeInfo(@TypeOf(ptr)).pointer.child {4110fn targetLoad(elf: *const Elf, ptr: anytype) @typeInfo(@TypeOf(ptr)).pointer.child {
4111 const Child = @typeInfo(@TypeOf(ptr)).pointer.child;4111 const pointer_ty = @typeInfo(@TypeOf(ptr)).pointer;
4112 const Child = pointer_ty.child;
4113 const alignment = pointer_ty.attrs.@"align" orelse @alignOf(Child);
4112 return switch (@typeInfo(Child)) {4114 return switch (@typeInfo(Child)) {
4113 else => @compileError(@typeName(Child)),4115 else => @compileError(@typeName(Child)),
4114 .int => std.mem.toNative(Child, ptr.*, elf.targetEndian()),4116 .int => std.mem.toNative(Child, ptr.*, elf.targetEndian()),
4115 .@"enum" => |@"enum"| @enumFromInt(elf.targetLoad(@as(*@"enum".tag_type, @ptrCast(ptr)))),4117 .@"enum" => |@"enum"| @enumFromInt(elf.targetLoad(@as(*align(alignment) @"enum".tag_type, @ptrCast(ptr)))),
4116 .@"struct" => |@"struct"| @bitCast(4118 .@"struct" => |@"struct"| @bitCast(
4117 elf.targetLoad(@as(*@"struct".backing_integer.?, @ptrCast(ptr))),4119 elf.targetLoad(@as(*align(alignment) @"struct".backing_integer.?, @ptrCast(ptr))),
4118 ),4120 ),
4119 };4121 };
4120}4122}
4121fn targetStore(elf: *const Elf, ptr: anytype, val: @typeInfo(@TypeOf(ptr)).pointer.child) void {4123fn targetStore(elf: *const Elf, ptr: anytype, val: @typeInfo(@TypeOf(ptr)).pointer.child) void {
4122 const Child = @typeInfo(@TypeOf(ptr)).pointer.child;4124 const pointer_ty = @typeInfo(@TypeOf(ptr)).pointer;
4125 const Child = pointer_ty.child;
4126 const alignment = pointer_ty.attrs.@"align" orelse @alignOf(Child);
4123 return switch (@typeInfo(Child)) {4127 return switch (@typeInfo(Child)) {
4124 else => @compileError(@typeName(Child)),4128 else => @compileError(@typeName(Child)),
4125 .int => ptr.* = std.mem.nativeTo(Child, val, elf.targetEndian()),4129 .int => ptr.* = std.mem.nativeTo(Child, val, elf.targetEndian()),
4126 .@"enum" => |@"enum"| elf.targetStore(4130 .@"enum" => |@"enum"| elf.targetStore(
4127 @as(*@"enum".tag_type, @ptrCast(ptr)),4131 @as(*align(alignment) @"enum".tag_type, @ptrCast(ptr)),
4128 @intFromEnum(val),4132 @intFromEnum(val),
4129 ),4133 ),
4130 .@"struct" => |@"struct"| elf.targetStore(4134 .@"struct" => |@"struct"| elf.targetStore(
4131 @as(*@"struct".backing_integer.?, @ptrCast(ptr)),4135 @as(*align(alignment) @"struct".backing_integer.?, @ptrCast(ptr)),
4132 @bitCast(val),4136 @bitCast(val),
4133 ),4137 ),
4134 };4138 };