aboutsummaryrefslogtreecommitdiff
path: root/src/link/Elf/Atom.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2023-10-31 04:26:57 -0400
committerJacob Young <jacobly0@users.noreply.github.com>2023-10-31 21:37:35 -0400
commitd890e817610dd75feef55c1f7983190852c622a5 (patch)
tree679d13309da35aa957a9d20d28671d9b8673b5a1 /src/link/Elf/Atom.zig
parent50cdb65784937965b5871037ff40bc34d8eb14af (diff)
downloadzig-d890e817610dd75feef55c1f7983190852c622a5.tar.gz
zig-d890e817610dd75feef55c1f7983190852c622a5.zip
mem: fix ub in writeInt
Use inline to vastly simplify the exposed API. This allows a comptime-known endian parameter to be propogated, making extra functions for a specific endianness completely unnecessary.
Diffstat (limited to 'src/link/Elf/Atom.zig')
-rw-r--r--src/link/Elf/Atom.zig84
1 files changed, 42 insertions, 42 deletions
diff --git a/src/link/Elf/Atom.zig b/src/link/Elf/Atom.zig
index bb988a678f..f3b41f2e10 100644
--- a/src/link/Elf/Atom.zig
+++ b/src/link/Elf/Atom.zig
@@ -786,43 +786,43 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
elf.R_X86_64_PLT32,
elf.R_X86_64_PC32,
- => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))),
+ => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .Little),
- elf.R_X86_64_GOTPCREL => try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P))),
- elf.R_X86_64_GOTPC32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(GOT + A - P))),
- elf.R_X86_64_GOTPC64 => try cwriter.writeIntLittle(i64, GOT + A - P),
+ elf.R_X86_64_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .Little),
+ elf.R_X86_64_GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .Little),
+ elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .Little),
elf.R_X86_64_GOTPCRELX => {
if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk;
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .Little);
continue;
}
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .Little);
},
elf.R_X86_64_REX_GOTPCRELX => {
if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk;
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .Little);
continue;
}
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .Little);
},
- elf.R_X86_64_32 => try cwriter.writeIntLittle(u32, @as(u32, @truncate(@as(u64, @intCast(S + A))))),
- elf.R_X86_64_32S => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A))),
+ elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .Little),
+ elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .Little),
- elf.R_X86_64_TPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A - TP))),
- elf.R_X86_64_TPOFF64 => try cwriter.writeIntLittle(i64, S + A - TP),
+ elf.R_X86_64_TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .Little),
+ elf.R_X86_64_TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .Little),
- elf.R_X86_64_DTPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A - DTP))),
- elf.R_X86_64_DTPOFF64 => try cwriter.writeIntLittle(i64, S + A - DTP),
+ elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .Little),
+ elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .Little),
elf.R_X86_64_TLSGD => {
if (target.flags.has_tlsgd) {
const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file)));
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .Little);
} else if (target.flags.has_gottp) {
const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
try x86_64.relaxTlsGdToIe(self, rels[i .. i + 2], @intCast(S_ - P), elf_file, &stream);
@@ -843,7 +843,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
if (elf_file.got.tlsld_index) |entry_index| {
const tlsld_entry = elf_file.got.entries.items[entry_index];
const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file)));
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .Little);
} else {
try x86_64.relaxTlsLdToLe(
self,
@@ -859,10 +859,10 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
elf.R_X86_64_GOTPC32_TLSDESC => {
if (target.flags.has_tlsdesc) {
const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .Little);
} else {
try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]);
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .Little);
}
},
@@ -874,18 +874,18 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
elf.R_X86_64_GOTTPOFF => {
if (target.flags.has_gottp) {
const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .Little);
} else {
x86_64.relaxGotTpOff(code[r_offset - 3 ..]) catch unreachable;
- try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP)));
+ try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .Little);
}
},
- elf.R_X86_64_GOT32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A))),
+ elf.R_X86_64_GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .Little),
// Zig custom relocations
- Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeIntLittle(u32, @as(u32, @intCast(ZIG_GOT + A))),
- Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeIntLittle(i32, @as(i32, @intCast(ZIG_GOT + A - P))),
+ Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .Little),
+ Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .Little),
else => {},
}
@@ -920,7 +920,7 @@ fn resolveDynAbsReloc(
.copyrel,
.cplt,
.none,
- => try writer.writeIntLittle(i32, @as(i32, @truncate(S + A))),
+ => try writer.writeInt(i32, @as(i32, @truncate(S + A)), .Little),
.dyn_copyrel => {
if (is_writeable or elf_file.base.options.z_nocopyreloc) {
@@ -932,7 +932,7 @@ fn resolveDynAbsReloc(
});
try applyDynamicReloc(A, elf_file, writer);
} else {
- try writer.writeIntLittle(i32, @as(i32, @truncate(S + A)));
+ try writer.writeInt(i32, @as(i32, @truncate(S + A)), .Little);
}
},
@@ -946,7 +946,7 @@ fn resolveDynAbsReloc(
});
try applyDynamicReloc(A, elf_file, writer);
} else {
- try writer.writeIntLittle(i32, @as(i32, @truncate(S + A)));
+ try writer.writeInt(i32, @as(i32, @truncate(S + A)), .Little);
}
},
@@ -984,7 +984,7 @@ fn resolveDynAbsReloc(
fn applyDynamicReloc(value: i64, elf_file: *Elf, writer: anytype) !void {
_ = elf_file;
// if (elf_file.options.apply_dynamic_relocs) {
- try writer.writeIntLittle(i64, value);
+ try writer.writeInt(i64, value, .Little);
// }
}
@@ -1058,22 +1058,22 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
switch (r_type) {
elf.R_X86_64_NONE => unreachable,
- elf.R_X86_64_8 => try cwriter.writeIntLittle(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A))))),
- elf.R_X86_64_16 => try cwriter.writeIntLittle(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A))))),
- elf.R_X86_64_32 => try cwriter.writeIntLittle(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A))))),
- elf.R_X86_64_32S => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A))),
- elf.R_X86_64_64 => try cwriter.writeIntLittle(i64, S + A),
- elf.R_X86_64_DTPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - DTP))),
- elf.R_X86_64_DTPOFF64 => try cwriter.writeIntLittle(i64, S + A - DTP),
- elf.R_X86_64_GOTOFF64 => try cwriter.writeIntLittle(i64, S + A - GOT),
- elf.R_X86_64_GOTPC64 => try cwriter.writeIntLittle(i64, GOT + A),
+ elf.R_X86_64_8 => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .Little),
+ elf.R_X86_64_16 => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .Little),
+ elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .Little),
+ elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .Little),
+ elf.R_X86_64_64 => try cwriter.writeInt(i64, S + A, .Little),
+ elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .Little),
+ elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .Little),
+ elf.R_X86_64_GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .Little),
+ elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A, .Little),
elf.R_X86_64_SIZE32 => {
const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
- try cwriter.writeIntLittle(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))));
+ try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .Little);
},
elf.R_X86_64_SIZE64 => {
const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
- try cwriter.writeIntLittle(i64, @as(i64, @intCast(size + A)));
+ try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .Little);
},
else => try self.reportUnhandledRelocError(rel, elf_file),
}
@@ -1254,7 +1254,7 @@ const x86_64 = struct {
0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
0x48, 0x03, 0x05, 0, 0, 0, 0, // add foo@gottpoff(%rip), %rax
};
- std.mem.writeIntLittle(i32, insts[12..][0..4], value - 12);
+ std.mem.writeInt(i32, insts[12..][0..4], value - 12, .Little);
try stream.seekBy(-4);
try writer.writeAll(&insts);
},
@@ -1292,7 +1292,7 @@ const x86_64 = struct {
0x64, 0x48, 0x8b, 0, // mov %fs:(%rax), %rax
0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax
};
- std.mem.writeIntLittle(i32, insts[8..][0..4], value);
+ std.mem.writeInt(i32, insts[8..][0..4], value, .Little);
try stream.seekBy(-3);
try writer.writeAll(&insts);
},
@@ -1306,7 +1306,7 @@ const x86_64 = struct {
0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax
0x90, // nop
};
- std.mem.writeIntLittle(i32, insts[8..][0..4], value);
+ std.mem.writeInt(i32, insts[8..][0..4], value, .Little);
try stream.seekBy(-3);
try writer.writeAll(&insts);
},
@@ -1392,7 +1392,7 @@ const x86_64 = struct {
0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
0x48, 0x81, 0xc0, 0, 0, 0, 0, // add $tp_offset, %rax
};
- std.mem.writeIntLittle(i32, insts[12..][0..4], value);
+ std.mem.writeInt(i32, insts[12..][0..4], value, .Little);
try stream.seekBy(-4);
try writer.writeAll(&insts);
relocs_log.debug(" relaxing {} and {}", .{