aboutsummaryrefslogtreecommitdiff
path: root/src/link/MachO/Atom.zig
diff options
context:
space:
mode:
authorAli Chraghi <chraghiali1@gmail.com>2022-05-22 19:36:59 +0430
committerAndrew Kelley <andrew@ziglang.org>2022-05-27 16:43:33 -0400
commit0e6285c8fc31ff866df96847fe34e660da38b4a9 (patch)
tree5d5830d5b3ce6c13041aacb7e073763551cb4852 /src/link/MachO/Atom.zig
parentddd5b57045d38b7d1f7d5a4120302797433233cd (diff)
downloadzig-0e6285c8fc31ff866df96847fe34e660da38b4a9.tar.gz
zig-0e6285c8fc31ff866df96847fe34e660da38b4a9.zip
math: make `cast` return optional instead of an error
Diffstat (limited to 'src/link/MachO/Atom.zig')
-rw-r--r--src/link/MachO/Atom.zig38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/link/MachO/Atom.zig b/src/link/MachO/Atom.zig
index dd2dc3c1f1..ff78b26989 100644
--- a/src/link/MachO/Atom.zig
+++ b/src/link/MachO/Atom.zig
@@ -763,17 +763,15 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
const displacement = math.cast(
i28,
@intCast(i64, target_addr) - @intCast(i64, source_addr),
- ) catch |err| switch (err) {
- error.Overflow => {
- log.err("jump too big to encode as i28 displacement value", .{});
- log.err(" (target - source) = displacement => 0x{x} - 0x{x} = 0x{x}", .{
- target_addr,
- source_addr,
- @intCast(i64, target_addr) - @intCast(i64, source_addr),
- });
- log.err(" TODO implement branch islands to extend jump distance for arm64", .{});
- return error.TODOImplementBranchIslands;
- },
+ ) orelse {
+ log.err("jump too big to encode as i28 displacement value", .{});
+ log.err(" (target - source) = displacement => 0x{x} - 0x{x} = 0x{x}", .{
+ target_addr,
+ source_addr,
+ @intCast(i64, target_addr) - @intCast(i64, source_addr),
+ });
+ log.err(" TODO implement branch islands to extend jump distance for arm64", .{});
+ return error.TODOImplementBranchIslands;
};
const code = self.code.items[rel.offset..][0..4];
var inst = aarch64.Instruction{
@@ -915,7 +913,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
mem.writeIntLittle(u32, code, inst.toU32());
},
.ARM64_RELOC_POINTER_TO_GOT => {
- const result = try math.cast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr));
+ const result = math.cast(i32, @intCast(i64, target_addr) - @intCast(i64, source_addr)) orelse return error.Overflow;
mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, result));
},
.ARM64_RELOC_UNSIGNED => {
@@ -945,17 +943,17 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
.x86_64 => {
switch (@intToEnum(macho.reloc_type_x86_64, rel.@"type")) {
.X86_64_RELOC_BRANCH => {
- const displacement = try math.cast(
+ const displacement = math.cast(
i32,
@intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
- );
+ ) orelse return error.Overflow;
mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
},
.X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => {
- const displacement = try math.cast(
+ const displacement = math.cast(
i32,
@intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
- );
+ ) orelse return error.Overflow;
mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
},
.X86_64_RELOC_TLV => {
@@ -963,10 +961,10 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
// We need to rewrite the opcode from movq to leaq.
self.code.items[rel.offset - 2] = 0x8d;
}
- const displacement = try math.cast(
+ const displacement = math.cast(
i32,
@intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
- );
+ ) orelse return error.Overflow;
mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
},
.X86_64_RELOC_SIGNED,
@@ -982,10 +980,10 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
else => unreachable,
};
const actual_target_addr = @intCast(i64, target_addr) + rel.addend;
- const displacement = try math.cast(
+ const displacement = math.cast(
i32,
actual_target_addr - @intCast(i64, source_addr + correction + 4),
- );
+ ) orelse return error.Overflow;
mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
},
.X86_64_RELOC_UNSIGNED => {