authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2025-08-07 21:27:11-07:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-08-11 03:02:54+02:00
log20486c4a8155499db8a26651bb2b1d2886003c33
tree783bdb09513eb5e1fc570a59334fb8f2ed25f57e
parent826b33863fe9ee46a30f80a10793bdbde80a96ca

elf: fix potential overflow in got emission


1 files changed, 11 insertions(+), 16 deletions(-)

src/link/Elf/synthetic_sections.zig+11-16
...@@ -384,7 +384,7 @@ pub const GotSection = struct {...@@ -384,7 +384,7 @@ pub const GotSection = struct {
384 try writeInt(value, elf_file, writer);384 try writeInt(value, elf_file, writer);
385 },385 },
386 .tlsld => {386 .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);
388 try writeInt(0, elf_file, writer);388 try writeInt(0, elf_file, writer);
389 },389 },
390 .tlsgd => {390 .tlsgd => {
...@@ -392,7 +392,7 @@ pub const GotSection = struct {...@@ -392,7 +392,7 @@ pub const GotSection = struct {
392 try writeInt(0, elf_file, writer);392 try writeInt(0, elf_file, writer);
393 try writeInt(0, elf_file, writer);393 try writeInt(0, elf_file, writer);
394 } else {394 } 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);
396 const offset = symbol.?.address(.{}, elf_file) - elf_file.dtpAddress();396 const offset = symbol.?.address(.{}, elf_file) - elf_file.dtpAddress();
397 try writeInt(offset, elf_file, writer);397 try writeInt(offset, elf_file, writer);
398 }398 }
...@@ -412,17 +412,12 @@ pub const GotSection = struct {...@@ -412,17 +412,12 @@ pub const GotSection = struct {
412 }412 }
413 },413 },
414 .tlsdesc => {414 .tlsdesc => {
415 if (symbol.?.flags.import) {415 try writeInt(0, elf_file, writer);
416 try writeInt(0, elf_file, writer);416 const offset: i64 = if (apply_relocs and !symbol.?.flags.import)
417 try writeInt(0, elf_file, writer);417 symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
418 } else {418 else
419 try writeInt(0, elf_file, writer);419 0;
420 const offset = if (apply_relocs)420 try writeInt(offset, elf_file, writer);
421 symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
422 else
423 0;
424 try writeInt(offset, elf_file, writer);
425 }
426 },421 },
427 }422 }
428 }423 }
...@@ -1505,9 +1500,9 @@ fn writeInt(value: anytype, elf_file: *Elf, writer: anytype) !void {...@@ -1505,9 +1500,9 @@ fn writeInt(value: anytype, elf_file: *Elf, writer: anytype) !void {
1505 const target = elf_file.getTarget();1500 const target = elf_file.getTarget();
1506 const endian = target.cpu.arch.endian();1501 const endian = target.cpu.arch.endian();
1507 switch (entry_size) {1502 switch (entry_size) {
1508 2 => try writer.writeInt(u16, @intCast(value), endian),1503 2 => try writer.writeInt(i16, @intCast(value), endian),
1509 4 => try writer.writeInt(u32, @intCast(value), endian),1504 4 => try writer.writeInt(i32, @intCast(value), endian),
1510 8 => try writer.writeInt(u64, @intCast(value), endian),1505 8 => try writer.writeInt(i64, value, endian),
1511 else => unreachable,1506 else => unreachable,
1512 }1507 }
1513}1508}