Skip to content

Commit bb451bd

Browse files
committed
elf: fix potential overflow in got emission
1 parent 62d1724 commit bb451bd

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/link/Elf/synthetic_sections.zig

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,15 @@ pub const GotSection = struct {
384384
try writeInt(value, elf_file, writer);
385385
},
386386
.tlsld => {
387-
try writeInt(if (is_dyn_lib) @as(u64, 0) else 1, elf_file, writer);
387+
try writeInt(if (is_dyn_lib) @as(i64, 0) else 1, elf_file, writer);
388388
try writeInt(0, elf_file, writer);
389389
},
390390
.tlsgd => {
391391
if (symbol.?.flags.import) {
392392
try writeInt(0, elf_file, writer);
393393
try writeInt(0, elf_file, writer);
394394
} else {
395-
try writeInt(if (is_dyn_lib) @as(u64, 0) else 1, elf_file, writer);
395+
try writeInt(if (is_dyn_lib) @as(i64, 0) else 1, elf_file, writer);
396396
const offset = symbol.?.address(.{}, elf_file) - elf_file.dtpAddress();
397397
try writeInt(offset, elf_file, writer);
398398
}
@@ -412,17 +412,12 @@ pub const GotSection = struct {
412412
}
413413
},
414414
.tlsdesc => {
415-
if (symbol.?.flags.import) {
416-
try writeInt(0, elf_file, writer);
417-
try writeInt(0, elf_file, writer);
418-
} else {
419-
try writeInt(0, elf_file, writer);
420-
const offset = if (apply_relocs)
421-
symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
422-
else
423-
0;
424-
try writeInt(offset, elf_file, writer);
425-
}
415+
try writeInt(0, elf_file, writer);
416+
const offset: i64 = if (apply_relocs and !symbol.?.flags.import)
417+
symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
418+
else
419+
0;
420+
try writeInt(offset, elf_file, writer);
426421
},
427422
}
428423
}
@@ -1505,9 +1500,9 @@ fn writeInt(value: anytype, elf_file: *Elf, writer: anytype) !void {
15051500
const target = elf_file.getTarget();
15061501
const endian = target.cpu.arch.endian();
15071502
switch (entry_size) {
1508-
2 => try writer.writeInt(u16, @intCast(value), endian),
1509-
4 => try writer.writeInt(u32, @intCast(value), endian),
1510-
8 => try writer.writeInt(u64, @intCast(value), endian),
1503+
2 => try writer.writeInt(i16, @intCast(value), endian),
1504+
4 => try writer.writeInt(i32, @intCast(value), endian),
1505+
8 => try writer.writeInt(i64, value, endian),
15111506
else => unreachable,
15121507
}
15131508
}

0 commit comments

Comments
 (0)