authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-01 18:28:54-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-03 12:59:38-04:00
log3939c4f5b56a6f3440d98e1c5f6dbf4a1e6ccdce
tree657e1e3ed76624bec63a11b14d617f3fbe7b0309
parent00116e2c7d68d35284c8e671d7d00d59f76149bd

Elf: factor out common code in section decompression


1 files changed, 15 insertions(+), 22 deletions(-)

src/link/Elf/Object.zig+15-22
...@@ -1236,29 +1236,22 @@ pub fn codeDecompressAlloc(self: *Object, elf_file: *Elf, atom_index: Atom.Index...@@ -1236,29 +1236,22 @@ pub fn codeDecompressAlloc(self: *Object, elf_file: *Elf, atom_index: Atom.Index
12361236
1237 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {1237 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {
1238 const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*;1238 const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*;
1239 var compressed_reader: Io.Reader = .fixed(data[@sizeOf(elf.Elf64_Chdr)..]);
1240 const size = std.math.cast(usize, chdr.ch_size) orelse return error.Overflow;
1241 var aw: Io.Writer.Allocating = try .initCapacity(gpa, size);
1242 defer aw.deinit();
1239 switch (chdr.ch_type) {1243 switch (chdr.ch_type) {
1240 .ZLIB => {1244 .ZLIB => {
1241 var stream: std.Io.Reader = .fixed(data[@sizeOf(elf.Elf64_Chdr)..]);1245 var decompress: std.compress.flate.Decompress = .init(&compressed_reader, .zlib, &.{});
1242 var zlib_stream: std.compress.flate.Decompress = .init(&stream, .zlib, &.{});1246 _ = try decompress.reader.streamRemaining(&aw.writer);
1243 const size = std.math.cast(usize, chdr.ch_size) orelse return error.Overflow;
1244 var aw: std.Io.Writer.Allocating = .init(gpa);
1245 try aw.ensureUnusedCapacity(size);
1246 defer aw.deinit();
1247 _ = try zlib_stream.reader.streamRemaining(&aw.writer);
1248 return aw.toOwnedSlice();
1249 },1247 },
1250 .ZSTD => {1248 .ZSTD => {
1251 var input: std.Io.Reader = .fixed(data[@sizeOf(elf.Elf64_Chdr)..]);1249 var decompress: std.compress.zstd.Decompress = .init(&compressed_reader, &.{}, .{});
1252 var stream: std.compress.zstd.Decompress = .init(&input, &.{}, .{});1250 _ = try decompress.reader.streamRemaining(&aw.writer);
1253 const size = std.math.cast(usize, chdr.ch_size) orelse return error.Overflow;
1254 var aw: std.Io.Writer.Allocating = try .initCapacity(gpa, size);
1255 defer aw.deinit();
1256 _ = try stream.reader.streamRemaining(&aw.writer);
1257
1258 return aw.toOwnedSlice();
1259 },1251 },
1260 else => @panic("TODO unhandled compression scheme"),1252 else => @panic("TODO unhandled compression scheme"),
1261 }1253 }
1254 return aw.toOwnedSlice();
1262 }1255 }
12631256
1264 return data;1257 return data;
...@@ -1492,7 +1485,7 @@ const Format = struct {...@@ -1492,7 +1485,7 @@ const Format = struct {
1492 object: *Object,1485 object: *Object,
1493 elf_file: *Elf,1486 elf_file: *Elf,
14941487
1495 fn symtab(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {1488 fn symtab(f: Format, writer: *Io.Writer) Io.Writer.Error!void {
1496 const object = f.object;1489 const object = f.object;
1497 const elf_file = f.elf_file;1490 const elf_file = f.elf_file;
1498 try writer.writeAll(" locals\n");1491 try writer.writeAll(" locals\n");
...@@ -1511,7 +1504,7 @@ const Format = struct {...@@ -1511,7 +1504,7 @@ const Format = struct {
1511 }1504 }
1512 }1505 }
15131506
1514 fn atoms(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {1507 fn atoms(f: Format, writer: *Io.Writer) Io.Writer.Error!void {
1515 const object = f.object;1508 const object = f.object;
1516 try writer.writeAll(" atoms\n");1509 try writer.writeAll(" atoms\n");
1517 for (object.atoms_indexes.items) |atom_index| {1510 for (object.atoms_indexes.items) |atom_index| {
...@@ -1520,7 +1513,7 @@ const Format = struct {...@@ -1520,7 +1513,7 @@ const Format = struct {
1520 }1513 }
1521 }1514 }
15221515
1523 fn cies(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {1516 fn cies(f: Format, writer: *Io.Writer) Io.Writer.Error!void {
1524 const object = f.object;1517 const object = f.object;
1525 try writer.writeAll(" cies\n");1518 try writer.writeAll(" cies\n");
1526 for (object.cies.items, 0..) |cie, i| {1519 for (object.cies.items, 0..) |cie, i| {
...@@ -1528,7 +1521,7 @@ const Format = struct {...@@ -1528,7 +1521,7 @@ const Format = struct {
1528 }1521 }
1529 }1522 }
15301523
1531 fn fdes(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {1524 fn fdes(f: Format, writer: *Io.Writer) Io.Writer.Error!void {
1532 const object = f.object;1525 const object = f.object;
1533 try writer.writeAll(" fdes\n");1526 try writer.writeAll(" fdes\n");
1534 for (object.fdes.items, 0..) |fde, i| {1527 for (object.fdes.items, 0..) |fde, i| {
...@@ -1536,7 +1529,7 @@ const Format = struct {...@@ -1536,7 +1529,7 @@ const Format = struct {
1536 }1529 }
1537 }1530 }
15381531
1539 fn groups(f: Format, writer: *std.Io.Writer) std.Io.Writer.Error!void {1532 fn groups(f: Format, writer: *Io.Writer) Io.Writer.Error!void {
1540 const object = f.object;1533 const object = f.object;
1541 const elf_file = f.elf_file;1534 const elf_file = f.elf_file;
1542 try writer.writeAll(" groups\n");1535 try writer.writeAll(" groups\n");
...@@ -1586,7 +1579,7 @@ pub fn fmtPath(self: Object) std.fmt.Alt(Object, formatPath) {...@@ -1586,7 +1579,7 @@ pub fn fmtPath(self: Object) std.fmt.Alt(Object, formatPath) {
1586 return .{ .data = self };1579 return .{ .data = self };
1587}1580}
15881581
1589fn formatPath(object: Object, writer: *std.Io.Writer) std.Io.Writer.Error!void {1582fn formatPath(object: Object, writer: *Io.Writer) Io.Writer.Error!void {
1590 if (object.archive) |ar| {1583 if (object.archive) |ar| {
1591 try writer.print("{f}({f})", .{ ar.path, object.path });1584 try writer.print("{f}({f})", .{ ar.path, object.path });
1592 } else {1585 } else {