authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-12-13 11:38:57+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-12-13 11:38:57+01:00
log92cca7fbf155602584dee294d04ee0b33135a25e
tree7025cc0d0469a09454909d229415559c857eaf1e
parent2492488501ee04e132cb552fd11a3025990ea047

lib/std/Build/CheckObject: implement for ELF


1 files changed, 43 insertions(+), 24 deletions(-)

lib/std/Build/Step/CheckObject.zig+43-24
......@@ -1554,15 +1554,14 @@ const ElfDumper = struct {
15541554 const archive_symtab_label = "archive symbol table";
15551555
15561556 fn parseAndDump(step: *Step, kind: Check.Kind, bytes: []const u8) ![]const u8 {
1557 _ = kind;
1558 const gpa = step.owner.allocator;
1559 return parseAndDumpArchive(gpa, bytes) catch |err| switch (err) {
1560 error.InvalidArchiveMagicNumber => try parseAndDumpObject(gpa, bytes),
1557 return parseAndDumpArchive(step, kind, bytes) catch |err| switch (err) {
1558 error.InvalidArchiveMagicNumber => try parseAndDumpObject(step, kind, bytes),
15611559 else => |e| return e,
15621560 };
15631561 }
15641562
1565 fn parseAndDumpArchive(gpa: Allocator, bytes: []const u8) ![]const u8 {
1563 fn parseAndDumpArchive(step: *Step, kind: Check.Kind, bytes: []const u8) ![]const u8 {
1564 const gpa = step.owner.allocator;
15661565 var stream = std.io.fixedBufferStream(bytes);
15671566 const reader = stream.reader();
15681567
......@@ -1623,8 +1622,15 @@ const ElfDumper = struct {
16231622 var output = std.ArrayList(u8).init(gpa);
16241623 const writer = output.writer();
16251624
1626 try ctx.dumpSymtab(writer);
1627 try ctx.dumpObjects(writer);
1625 switch (kind) {
1626 .archive_symtab => if (ctx.symtab.items.len > 0) {
1627 try ctx.dumpSymtab(writer);
1628 } else return step.fail("no archive symbol table found", .{}),
1629
1630 else => if (ctx.objects.items.len > 0) {
1631 try ctx.dumpObjects(step, kind, writer);
1632 } else return step.fail("empty archive", .{}),
1633 }
16281634
16291635 return output.toOwnedSlice();
16301636 }
......@@ -1666,8 +1672,6 @@ const ElfDumper = struct {
16661672 }
16671673
16681674 fn dumpSymtab(ctx: ArchiveContext, writer: anytype) !void {
1669 if (ctx.symtab.items.len == 0) return;
1670
16711675 var files = std.AutoHashMap(usize, []const u8).init(ctx.gpa);
16721676 defer files.deinit();
16731677 try files.ensureUnusedCapacity(@intCast(ctx.objects.items.len));
......@@ -1701,10 +1705,10 @@ const ElfDumper = struct {
17011705 }
17021706 }
17031707
1704 fn dumpObjects(ctx: ArchiveContext, writer: anytype) !void {
1708 fn dumpObjects(ctx: ArchiveContext, step: *Step, kind: Check.Kind, writer: anytype) !void {
17051709 for (ctx.objects.items) |object| {
17061710 try writer.print("object {s}\n", .{object.name});
1707 const output = try parseAndDumpObject(ctx.gpa, ctx.data[object.off..][0..object.len]);
1711 const output = try parseAndDumpObject(step, kind, ctx.data[object.off..][0..object.len]);
17081712 defer ctx.gpa.free(output);
17091713 try writer.print("{s}\n", .{output});
17101714 }
......@@ -1722,7 +1726,8 @@ const ElfDumper = struct {
17221726 };
17231727 };
17241728
1725 fn parseAndDumpObject(gpa: Allocator, bytes: []const u8) ![]const u8 {
1729 fn parseAndDumpObject(step: *Step, kind: Check.Kind, bytes: []const u8) ![]const u8 {
1730 const gpa = step.owner.allocator;
17261731 var stream = std.io.fixedBufferStream(bytes);
17271732 const reader = stream.reader();
17281733
......@@ -1774,12 +1779,27 @@ const ElfDumper = struct {
17741779 var output = std.ArrayList(u8).init(gpa);
17751780 const writer = output.writer();
17761781
1777 try ctx.dumpHeader(writer);
1778 try ctx.dumpShdrs(writer);
1779 try ctx.dumpPhdrs(writer);
1780 try ctx.dumpDynamicSection(writer);
1781 try ctx.dumpSymtab(.symtab, writer);
1782 try ctx.dumpSymtab(.dysymtab, writer);
1782 switch (kind) {
1783 .headers => {
1784 try ctx.dumpHeader(writer);
1785 try ctx.dumpShdrs(writer);
1786 try ctx.dumpPhdrs(writer);
1787 },
1788
1789 .symtab => if (ctx.symtab.symbols.len > 0) {
1790 try ctx.dumpSymtab(.symtab, writer);
1791 } else return step.fail("no symbol table found", .{}),
1792
1793 .dynamic_symtab => if (ctx.dysymtab.symbols.len > 0) {
1794 try ctx.dumpSymtab(.dysymtab, writer);
1795 } else return step.fail("no dynamic symbol table found", .{}),
1796
1797 .dynamic_section => if (ctx.getSectionByName(".dynamic")) |shndx| {
1798 try ctx.dumpDynamicSection(shndx, writer);
1799 } else return step.fail("no .dynamic section found", .{}),
1800
1801 else => return step.fail("invalid check kind for ELF file format: {s}", .{@tagName(kind)}),
1802 }
17831803
17841804 return output.toOwnedSlice();
17851805 }
......@@ -1791,8 +1811,8 @@ const ElfDumper = struct {
17911811 shdrs: []align(1) const elf.Elf64_Shdr,
17921812 phdrs: []align(1) const elf.Elf64_Phdr,
17931813 shstrtab: []const u8,
1794 symtab: ?Symtab = null,
1795 dysymtab: ?Symtab = null,
1814 symtab: Symtab = .{},
1815 dysymtab: Symtab = .{},
17961816
17971817 fn dumpHeader(ctx: ObjectContext, writer: anytype) !void {
17981818 try writer.writeAll("header\n");
......@@ -1856,8 +1876,7 @@ const ElfDumper = struct {
18561876 }
18571877 }
18581878
1859 fn dumpDynamicSection(ctx: ObjectContext, writer: anytype) !void {
1860 const shndx = ctx.getSectionByName(".dynamic") orelse return;
1879 fn dumpDynamicSection(ctx: ObjectContext, shndx: usize, writer: anytype) !void {
18611880 const shdr = ctx.shdrs[shndx];
18621881 const strtab = ctx.getSectionContents(shdr.sh_link);
18631882 const data = ctx.getSectionContents(shndx);
......@@ -2097,8 +2116,8 @@ const ElfDumper = struct {
20972116 };
20982117
20992118 const Symtab = struct {
2100 symbols: []align(1) const elf.Elf64_Sym,
2101 strings: []const u8,
2119 symbols: []align(1) const elf.Elf64_Sym = &[0]elf.Elf64_Sym{},
2120 strings: []const u8 = &[0]u8{},
21022121
21032122 fn get(st: Symtab, index: usize) ?elf.Elf64_Sym {
21042123 if (index >= st.symbols.len) return null;