From 5839054e8591f60cbfdd3693398ecd5844530fe9 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 20 Jul 2023 08:39:49 +0200 Subject: check-object: dump contents of .dynamic section --- lib/std/Build/Step/CheckObject.zig | 152 ++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 2 deletions(-) (limited to 'lib/std/Build/Step/CheckObject.zig') diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 0c74a4e40e..5708328523 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -712,8 +712,7 @@ const ElfDumper = struct { fn getName(st: Symtab, index: usize) ?[]const u8 { const sym = st.get(index) orelse return null; - assert(sym.st_name < st.strings.len); - return mem.sliceTo(@ptrCast(st.strings.ptr + sym.st_name), 0); + return getString(st.strings, sym.st_name); } }; @@ -786,6 +785,7 @@ const ElfDumper = struct { try dumpHeader(ctx, writer); try dumpShdrs(ctx, writer); try dumpPhdrs(ctx, writer); + try dumpDynamic(ctx, writer); return output.toOwnedSlice(); } @@ -803,6 +803,17 @@ const ElfDumper = struct { return ctx.data[shdr.sh_offset..][0..shdr.sh_size]; } + fn getSectionByName(ctx: Context, name: []const u8) ?usize { + for (0..ctx.shdrs.len) |shndx| { + if (mem.eql(u8, getSectionName(ctx, shndx), name)) return shndx; + } else return null; + } + + fn getString(strtab: []const u8, off: u32) []const u8 { + assert(off < strtab.len); + return mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + off)), 0); + } + fn dumpHeader(ctx: Context, writer: anytype) !void { try writer.writeAll("header\n"); try writer.print("type {s}\n", .{@tagName(ctx.hdr.e_type)}); @@ -824,6 +835,143 @@ const ElfDumper = struct { } } + fn dumpDynamic(ctx: Context, writer: anytype) !void { + const shndx = getSectionByName(ctx, ".dynamic") orelse return; + const shdr = ctx.shdrs[shndx]; + const strtab = getSectionContents(ctx, shdr.sh_link); + const data = getSectionContents(ctx, shndx); + const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn)); + const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries]; + + for (entries) |entry| { + const key = @as(u64, @bitCast(entry.d_tag)); + const value = entry.d_val; + + const key_str = switch (key) { + elf.DT_NEEDED => "NEEDED", + elf.DT_SONAME => "SONAME", + elf.DT_INIT_ARRAY => "INIT_ARRAY", + elf.DT_INIT_ARRAYSZ => "INIT_ARRAYSZ", + elf.DT_FINI_ARRAY => "FINI_ARRAY", + elf.DT_FINI_ARRAYSZ => "FINI_ARRAYSZ", + elf.DT_HASH => "HASH", + elf.DT_GNU_HASH => "GNU_HASH", + elf.DT_STRTAB => "STRTAB", + elf.DT_SYMTAB => "SYMTAB", + elf.DT_STRSZ => "STRSZ", + elf.DT_SYMENT => "SYMENT", + elf.DT_PLTGOT => "PLTGOT", + elf.DT_PLTRELSZ => "PLTRELSZ", + elf.DT_PLTREL => "PLTREL", + elf.DT_JMPREL => "JMPREL", + elf.DT_RELA => "RELA", + elf.DT_RELASZ => "RELASZ", + elf.DT_RELAENT => "RELAENT", + elf.DT_VERDEF => "VERDEF", + elf.DT_VERDEFNUM => "VERDEFNUM", + elf.DT_FLAGS => "FLAGS", + elf.DT_FLAGS_1 => "FLAGS_1", + elf.DT_VERNEED => "VERNEED", + elf.DT_VERNEEDNUM => "VERNEEDNUM", + elf.DT_VERSYM => "VERSYM", + elf.DT_RELACOUNT => "RELACOUNT", + elf.DT_RPATH => "RPATH", + elf.DT_RUNPATH => "RUNPATH", + elf.DT_INIT => "INIT", + elf.DT_FINI => "FINI", + elf.DT_NULL => "NULL", + else => "UNKNOWN", + }; + try writer.print("{s}", .{key_str}); + + switch (key) { + elf.DT_NEEDED, + elf.DT_SONAME, + elf.DT_RPATH, + elf.DT_RUNPATH, + => { + const name = getString(strtab, @intCast(value)); + try writer.print(" {s}", .{name}); + }, + + elf.DT_INIT_ARRAY, + elf.DT_FINI_ARRAY, + elf.DT_HASH, + elf.DT_GNU_HASH, + elf.DT_STRTAB, + elf.DT_SYMTAB, + elf.DT_PLTGOT, + elf.DT_JMPREL, + elf.DT_RELA, + elf.DT_VERDEF, + elf.DT_VERNEED, + elf.DT_VERSYM, + elf.DT_INIT, + elf.DT_FINI, + elf.DT_NULL, + => try writer.print(" {x}", .{value}), + + elf.DT_INIT_ARRAYSZ, + elf.DT_FINI_ARRAYSZ, + elf.DT_STRSZ, + elf.DT_SYMENT, + elf.DT_PLTRELSZ, + elf.DT_RELASZ, + elf.DT_RELAENT, + elf.DT_RELACOUNT, + => try writer.print(" {d}", .{value}), + + elf.DT_PLTREL => try writer.writeAll(switch (value) { + elf.DT_REL => " REL", + elf.DT_RELA => " RELA", + else => " UNKNOWN", + }), + + elf.DT_FLAGS => if (value > 0) { + if (value & elf.DF_ORIGIN != 0) try writer.writeAll(" ORIGIN"); + if (value & elf.DF_SYMBOLIC != 0) try writer.writeAll(" SYMBOLIC"); + if (value & elf.DF_TEXTREL != 0) try writer.writeAll(" TEXTREL"); + if (value & elf.DF_BIND_NOW != 0) try writer.writeAll(" BIND_NOW"); + if (value & elf.DF_STATIC_TLS != 0) try writer.writeAll(" STATIC_TLS"); + }, + + elf.DT_FLAGS_1 => if (value > 0) { + if (value & elf.DF_1_NOW != 0) try writer.writeAll(" NOW"); + if (value & elf.DF_1_GLOBAL != 0) try writer.writeAll(" GLOBAL"); + if (value & elf.DF_1_GROUP != 0) try writer.writeAll(" GROUP"); + if (value & elf.DF_1_NODELETE != 0) try writer.writeAll(" NODELETE"); + if (value & elf.DF_1_LOADFLTR != 0) try writer.writeAll(" LOADFLTR"); + if (value & elf.DF_1_INITFIRST != 0) try writer.writeAll(" INITFIRST"); + if (value & elf.DF_1_NOOPEN != 0) try writer.writeAll(" NOOPEN"); + if (value & elf.DF_1_ORIGIN != 0) try writer.writeAll(" ORIGIN"); + if (value & elf.DF_1_DIRECT != 0) try writer.writeAll(" DIRECT"); + if (value & elf.DF_1_TRANS != 0) try writer.writeAll(" TRANS"); + if (value & elf.DF_1_INTERPOSE != 0) try writer.writeAll(" INTERPOSE"); + if (value & elf.DF_1_NODEFLIB != 0) try writer.writeAll(" NODEFLIB"); + if (value & elf.DF_1_NODUMP != 0) try writer.writeAll(" NODUMP"); + if (value & elf.DF_1_CONFALT != 0) try writer.writeAll(" CONFALT"); + if (value & elf.DF_1_ENDFILTEE != 0) try writer.writeAll(" ENDFILTEE"); + if (value & elf.DF_1_DISPRELDNE != 0) try writer.writeAll(" DISPRELDNE"); + if (value & elf.DF_1_DISPRELPND != 0) try writer.writeAll(" DISPRELPND"); + if (value & elf.DF_1_NODIRECT != 0) try writer.writeAll(" NODIRECT"); + if (value & elf.DF_1_IGNMULDEF != 0) try writer.writeAll(" IGNMULDEF"); + if (value & elf.DF_1_NOKSYMS != 0) try writer.writeAll(" NOKSYMS"); + if (value & elf.DF_1_NOHDR != 0) try writer.writeAll(" NOHDR"); + if (value & elf.DF_1_EDITED != 0) try writer.writeAll(" EDITED"); + if (value & elf.DF_1_NORELOC != 0) try writer.writeAll(" NORELOC"); + if (value & elf.DF_1_SYMINTPOSE != 0) try writer.writeAll(" SYMINTPOSE"); + if (value & elf.DF_1_GLOBAUDIT != 0) try writer.writeAll(" GLOBAUDIT"); + if (value & elf.DF_1_SINGLETON != 0) try writer.writeAll(" SINGLETON"); + if (value & elf.DF_1_STUB != 0) try writer.writeAll(" STUB"); + if (value & elf.DF_1_PIE != 0) try writer.writeAll(" PIE"); + }, + + else => try writer.print(" {x}", .{value}), + } + try writer.writeByte('\n'); + } + } + fn fmtShType(sh_type: u32) std.fmt.Formatter(formatShType) { return .{ .data = sh_type }; } -- cgit v1.2.3 From e8b613783f383b39018cb83b2b141f991c2bc7ae Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 20 Jul 2023 14:05:16 +0200 Subject: check-object: remove wildcard matchers as they are too clunky Instead, we now have a looser helper called `checkContains(...)` that will match on any occurrence similarly to `std.mem.indexOf()`. While at it, I have cleaned up other combinators to make the entire API more consistent, and so: * `checkStart(phrase)` is now `checkStart()` followed by `checkExact(phrase)` * `checkNext(phrase)` if matching exactly is now `checkExact(phrase)` * `checkNext(phrase)` if matching loosely is now `checkContains(phrase)` * `checkNext(phrase)` if matching exactly with var extractors is now `checkExtract(phrase)` Finally, `ElfDumper` is now dumping contents of `.symtab` and `.dynsym` symbol tables. I have also removed dumping of symtabs as optional - they are now always dumped which cleaned up the implementation even more. --- lib/std/Build/Step/CheckObject.zig | 584 +++++++++++++++++++--------- test/link/macho/dead_strip/build.zig | 4 +- test/link/macho/dead_strip_dylibs/build.zig | 10 +- test/link/macho/dylib/build.zig | 29 +- test/link/macho/entry/build.zig | 12 +- test/link/macho/entry_in_dylib/build.zig | 15 +- test/link/macho/headerpad/build.zig | 20 +- test/link/macho/linksection/build.zig | 6 +- test/link/macho/needed_framework/build.zig | 5 +- test/link/macho/needed_library/build.zig | 5 +- test/link/macho/pagezero/build.zig | 21 +- test/link/macho/search_strategy/build.zig | 5 +- test/link/macho/stack_size/build.zig | 5 +- test/link/macho/strict_validation/build.zig | 77 ++-- test/link/macho/unwind_info/build.zig | 11 +- test/link/macho/weak_framework/build.zig | 5 +- test/link/macho/weak_library/build.zig | 9 +- test/link/wasm/archive/build.zig | 5 +- test/link/wasm/basic-features/build.zig | 7 +- test/link/wasm/bss/build.zig | 44 ++- test/link/wasm/export-data/build.zig | 36 +- test/link/wasm/export/build.zig | 27 +- test/link/wasm/extern-mangle/build.zig | 13 +- test/link/wasm/function-table/build.zig | 47 ++- test/link/wasm/infer-features/build.zig | 19 +- test/link/wasm/producers/build.zig | 21 +- test/link/wasm/segments/build.zig | 24 +- test/link/wasm/stack_pointer/build.zig | 27 +- test/link/wasm/type/build.zig | 17 +- 29 files changed, 689 insertions(+), 421 deletions(-) (limited to 'lib/std/Build/Step/CheckObject.zig') diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 5708328523..6cf007c5b8 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -18,7 +18,6 @@ step: Step, source: std.Build.FileSource, max_bytes: usize = 20 * 1024 * 1024, checks: std.ArrayList(Check), -dump_symtab: bool = false, obj_format: std.Target.ObjectFormat, pub fn create( @@ -53,62 +52,41 @@ const SearchPhrase = struct { } }; -/// There two types of actions currently supported: -/// * `.match` - is the main building block of standard matchers with optional eat-all token `{*}` -/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature -/// i.e., it won't really handle edge cases/nontrivial examples. But given that we do want to use -/// it mainly to test the output of our object format parser-dumpers when testing the linkers, etc. -/// it should be plenty useful in its current form. -/// * `.compute_cmp` - can be used to perform an operation on the extracted global variables +/// There five types of actions currently supported: +/// .exact - will do an exact match against the haystack +/// .contains - will check for existence within the haystack +/// .not_present - will check for non-existence within the haystack +/// .extract - will do an exact match and extract into a variable enclosed within `{name}` braces +/// .compute_cmp - will perform an operation on the extracted global variables /// using the MatchAction. It currently only supports an addition. The operation is required /// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well, /// to avoid any parsing really). /// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively /// they could then be added with this simple program `vmaddr entryoff +`. const Action = struct { - tag: enum { match, not_present, compute_cmp }, + tag: enum { exact, contains, not_present, extract, compute_cmp }, phrase: SearchPhrase, expected: ?ComputeCompareExpected = null, - /// Will return true if the `phrase` was found in the `haystack`. - /// Some examples include: - /// - /// LC 0 => will match in its entirety - /// vmaddr {vmaddr} => will match `vmaddr` and then extract the following value as u64 - /// and save under `vmaddr` global name (see `global_vars` param) - /// name {*}libobjc{*}.dylib => will match `name` followed by a token which contains `libobjc` and `.dylib` - /// in that order with other letters in between - fn match( + /// Returns true if the `phrase` is an exact match with the haystack and variable was successfully extracted. + fn extract( act: Action, b: *std.Build, step: *Step, haystack: []const u8, global_vars: anytype, ) !bool { - assert(act.tag == .match or act.tag == .not_present); - const phrase = act.phrase.resolve(b, step); + assert(act.tag == .extract); + const hay = mem.trim(u8, haystack, " "); + const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); + var candidate_var: ?struct { name: []const u8, value: u64 } = null; - var hay_it = mem.tokenizeScalar(u8, mem.trim(u8, haystack, " "), ' '); - var needle_it = mem.tokenizeScalar(u8, mem.trim(u8, phrase, " "), ' '); + var hay_it = mem.tokenizeScalar(u8, hay, ' '); + var needle_it = mem.tokenizeScalar(u8, phrase, ' '); while (needle_it.next()) |needle_tok| { - const hay_tok = hay_it.next() orelse return false; - - if (mem.indexOf(u8, needle_tok, "{*}")) |index| { - // We have fuzzy matchers within the search pattern, so we match substrings. - var start = index; - var n_tok = needle_tok; - var h_tok = hay_tok; - while (true) { - n_tok = n_tok[start + 3 ..]; - const inner = if (mem.indexOf(u8, n_tok, "{*}")) |sub_end| - n_tok[0..sub_end] - else - n_tok; - if (mem.indexOf(u8, h_tok, inner) == null) return false; - start = mem.indexOf(u8, n_tok, "{*}") orelse break; - } - } else if (mem.startsWith(u8, needle_tok, "{")) { + const hay_tok = hay_it.next() orelse break; + if (mem.startsWith(u8, needle_tok, "{")) { const closing_brace = mem.indexOf(u8, needle_tok, "}") orelse return error.MissingClosingBrace; if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast; @@ -124,11 +102,49 @@ const Action = struct { } } - if (candidate_var) |v| { - try global_vars.putNoClobber(v.name, v.value); - } + if (candidate_var) |v| try global_vars.putNoClobber(v.name, v.value); + return candidate_var != null; + } + + /// Returns true if the `phrase` is an exact match with the haystack. + fn exact( + act: Action, + b: *std.Build, + step: *Step, + haystack: []const u8, + ) bool { + assert(act.tag == .exact); + const hay = mem.trim(u8, haystack, " "); + const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); + return mem.eql(u8, hay, phrase); + } + + /// Returns true if the `phrase` exists within the haystack. + fn contains( + act: Action, + b: *std.Build, + step: *Step, + haystack: []const u8, + ) bool { + assert(act.tag == .contains); + const hay = mem.trim(u8, haystack, " "); + const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); + return mem.indexOf(u8, hay, phrase) != null; + } - return true; + /// Returns true if the `phrase` does not exist within the haystack. + fn notPresent( + act: Action, + b: *std.Build, + step: *Step, + haystack: []const u8, + ) bool { + assert(act.tag == .not_present); + return !contains(.{ + .tag = .contains, + .phrase = act.phrase, + .expected = act.expected, + }, b, step, haystack); } /// Will return true if the `phrase` is correctly parsed into an RPN program and @@ -235,9 +251,23 @@ const Check = struct { }; } - fn match(self: *Check, phrase: SearchPhrase) void { + fn extract(self: *Check, phrase: SearchPhrase) void { + self.actions.append(.{ + .tag = .extract, + .phrase = phrase, + }) catch @panic("OOM"); + } + + fn exact(self: *Check, phrase: SearchPhrase) void { + self.actions.append(.{ + .tag = .exact, + .phrase = phrase, + }) catch @panic("OOM"); + } + + fn contains(self: *Check, phrase: SearchPhrase) void { self.actions.append(.{ - .tag = .match, + .tag = .contains, .phrase = phrase, }) catch @panic("OOM"); } @@ -258,52 +288,118 @@ const Check = struct { } }; -/// Creates a new sequence of actions with `phrase` as the first anchor searched phrase. -pub fn checkStart(self: *CheckObject, phrase: []const u8) void { +/// Creates a new empty sequence of actions. +pub fn checkStart(self: *CheckObject) void { var new_check = Check.create(self.step.owner.allocator); - new_check.match(.{ .string = self.step.owner.dupe(phrase) }); self.checks.append(new_check) catch @panic("OOM"); } -/// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)`. -/// Asserts at least one check already exists. -pub fn checkNext(self: *CheckObject, phrase: []const u8) void { +/// Adds an exact match phrase to the latest created Check with `CheckObject.checkStart()`. +pub fn checkExact(self: *CheckObject, phrase: []const u8) void { + self.checkExactInner(phrase, null); +} + +/// Like `checkExact()` but takes an additional argument `FileSource` which will be +/// resolved to a full search query in `make()`. +pub fn checkExactFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { + self.checkExactInner(phrase, file_source); +} + +fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; - last.match(.{ .string = self.step.owner.dupe(phrase) }); + last.exact(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); } -/// Like `checkNext()` but takes an additional argument `FileSource` which will be +/// Adds a fuzzy match phrase to the latest created Check with `CheckObject.checkStart()`. +pub fn checkContains(self: *CheckObject, phrase: []const u8) void { + self.checkContainsInner(phrase, null); +} + +/// Like `checkContains()` but takes an additional argument `FileSource` which will be /// resolved to a full search query in `make()`. -pub fn checkNextFileSource( - self: *CheckObject, - phrase: []const u8, - file_source: std.Build.FileSource, -) void { +pub fn checkContainsFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { + self.checkContainsInner(phrase, file_source); +} + +fn checkContainsInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { + assert(self.checks.items.len > 0); + const last = &self.checks.items[self.checks.items.len - 1]; + last.contains(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); +} + +/// Adds an exact match phrase with variable extractor to the latest created Check +/// with `CheckObject.checkStart()`. +pub fn checkExtract(self: *CheckObject, phrase: []const u8) void { + self.checkExtractInner(phrase, null); +} + +/// Like `checkExtract()` but takes an additional argument `FileSource` which will be +/// resolved to a full search query in `make()`. +pub fn checkExtractFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { + self.checkExtractInner(phrase, file_source); +} + +fn checkExtractInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; - last.match(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); + last.extract(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); } /// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)` /// however ensures there is no matching phrase in the output. -/// Asserts at least one check already exists. pub fn checkNotPresent(self: *CheckObject, phrase: []const u8) void { + self.checkNotPresentInner(phrase, null); +} + +/// Like `checkExtract()` but takes an additional argument `FileSource` which will be +/// resolved to a full search query in `make()`. +pub fn checkNotPresentFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { + self.checkNotPresentInner(phrase, file_source); +} + +fn checkNotPresentInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; - last.notPresent(.{ .string = self.step.owner.dupe(phrase) }); + last.notPresent(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); } /// Creates a new check checking specifically symbol table parsed and dumped from the object /// file. -/// Issuing this check will force parsing and dumping of the symbol table. pub fn checkInSymtab(self: *CheckObject) void { - self.dump_symtab = true; - const symtab_label = switch (self.obj_format) { + const label = switch (self.obj_format) { .macho => MachODumper.symtab_label, - else => @panic("TODO other parsers"), + .elf => ElfDumper.symtab_label, + .wasm => WasmDumper.symtab_label, + .coff => @panic("TODO symtab for coff"), + else => @panic("TODO other file formats"), }; - self.checkStart(symtab_label); + self.checkStart(); + self.checkExact(label); +} + +/// Creates a new check checking specifically dynamic symbol table parsed and dumped from the object +/// file. +/// This check is target-dependent and applicable to ELF only. +pub fn checkInDynamicSymtab(self: *CheckObject) void { + const label = switch (self.obj_format) { + .elf => ElfDumper.dynamic_symtab_label, + else => @panic("Unsupported target platform"), + }; + self.checkStart(); + self.checkExact(label); +} + +/// Creates a new check checking specifically dynamic section parsed and dumped from the object +/// file. +/// This check is target-dependent and applicable to ELF only. +pub fn checkInDynamicSection(self: *CheckObject) void { + const label = switch (self.obj_format) { + .elf => ElfDumper.dynamic_section_label, + else => @panic("Unsupported target platform"), + }; + self.checkStart(); + self.checkExact(label); } /// Creates a new standalone, singular check which allows running simple binary operations @@ -336,16 +432,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { ) catch |err| return step.fail("unable to read '{s}': {s}", .{ src_path, @errorName(err) }); const output = switch (self.obj_format) { - .macho => try MachODumper.parseAndDump(step, contents, .{ - .dump_symtab = self.dump_symtab, - }), - .elf => try ElfDumper.parseAndDump(step, contents, .{ - .dump_symtab = self.dump_symtab, - }), + .macho => try MachODumper.parseAndDump(step, contents), + .elf => try ElfDumper.parseAndDump(step, contents), .coff => @panic("TODO coff parser"), - .wasm => try WasmDumper.parseAndDump(step, contents, .{ - .dump_symtab = self.dump_symtab, - }), + .wasm => try WasmDumper.parseAndDump(step, contents), else => unreachable, }; @@ -355,9 +445,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { var it = mem.tokenizeAny(u8, output, "\r\n"); for (chk.actions.items) |act| { switch (act.tag) { - .match => { + .exact => { while (it.next()) |line| { - if (try act.match(b, step, line, &vars)) break; + if (act.exact(b, step, line)) break; } else { return step.fail( \\ @@ -369,18 +459,46 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { , .{ act.phrase.resolve(b, step), output }); } }, + .contains => { + while (it.next()) |line| { + if (act.contains(b, step, line)) break; + } else { + return step.fail( + \\ + \\========= expected to find: ========================== + \\*{s}* + \\========= but parsed file does not contain it: ======= + \\{s} + \\====================================================== + , .{ act.phrase.resolve(b, step), output }); + } + }, .not_present => { while (it.next()) |line| { - if (try act.match(b, step, line, &vars)) { - return step.fail( - \\ - \\========= expected not to find: =================== - \\{s} - \\========= but parsed file does contain it: ======== - \\{s} - \\=================================================== - , .{ act.phrase.resolve(b, step), output }); - } + if (act.notPresent(b, step, line)) break; + } else { + return step.fail( + \\ + \\========= expected not to find: =================== + \\{s} + \\========= but parsed file does contain it: ======== + \\{s} + \\=================================================== + , .{ act.phrase.resolve(b, step), output }); + } + }, + .extract => { + while (it.next()) |line| { + if (try act.extract(b, step, line, &vars)) break; + } else { + return step.fail( + \\ + \\========= expected to find and extract: ============== + \\{s} + \\========= but parsed file does not contain it: ======= + \\{s} + \\====================================================== + , .{ act.phrase.resolve(b, step), output }); } }, .compute_cmp => { @@ -410,15 +528,16 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } -const Opts = struct { - dump_symtab: bool = false, -}; - const MachODumper = struct { const LoadCommandIterator = macho.LoadCommandIterator; - const symtab_label = "symtab"; + const symtab_label = "symbol table"; + + const Symtab = struct { + symbols: []align(1) const macho.nlist_64, + strings: []const u8, + }; - fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8, opts: Opts) ![]const u8 { + fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8) ![]const u8 { const gpa = step.owner.allocator; var stream = std.io.fixedBufferStream(bytes); const reader = stream.reader(); @@ -431,8 +550,7 @@ const MachODumper = struct { var output = std.ArrayList(u8).init(gpa); const writer = output.writer(); - var symtab: []const macho.nlist_64 = undefined; - var strtab: []const u8 = undefined; + var symtab: ?Symtab = null; var sections = std.ArrayList(macho.section_64).init(gpa); var imports = std.ArrayList([]const u8).init(gpa); @@ -450,13 +568,11 @@ const MachODumper = struct { sections.appendAssumeCapacity(sect); } }, - .SYMTAB => if (opts.dump_symtab) { + .SYMTAB => { const lc = cmd.cast(macho.symtab_command).?; - symtab = @as( - [*]const macho.nlist_64, - @ptrCast(@alignCast(&bytes[lc.symoff])), - )[0..lc.nsyms]; - strtab = bytes[lc.stroff..][0..lc.strsize]; + const symbols = @as([*]align(1) const macho.nlist_64, @ptrCast(bytes.ptr + lc.symoff))[0..lc.nsyms]; + const strings = bytes[lc.stroff..][0..lc.strsize]; + symtab = .{ .symbols = symbols, .strings = strings }; }, .LOAD_DYLIB, .LOAD_WEAK_DYLIB, @@ -473,53 +589,8 @@ const MachODumper = struct { i += 1; } - if (opts.dump_symtab) { - try writer.print("{s}\n", .{symtab_label}); - for (symtab) |sym| { - if (sym.stab()) continue; - const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + sym.n_strx)), 0); - if (sym.sect()) { - const sect = sections.items[sym.n_sect - 1]; - try writer.print("{x} ({s},{s})", .{ - sym.n_value, - sect.segName(), - sect.sectName(), - }); - if (sym.ext()) { - try writer.writeAll(" external"); - } - try writer.print(" {s}\n", .{sym_name}); - } else if (sym.undf()) { - const ordinal = @divTrunc(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); - const import_name = blk: { - if (ordinal <= 0) { - if (ordinal == macho.BIND_SPECIAL_DYLIB_SELF) - break :blk "self import"; - if (ordinal == macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) - break :blk "main executable"; - if (ordinal == macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP) - break :blk "flat lookup"; - unreachable; - } - const full_path = imports.items[@as(u16, @bitCast(ordinal)) - 1]; - const basename = fs.path.basename(full_path); - assert(basename.len > 0); - const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len; - break :blk basename[0..ext]; - }; - try writer.writeAll("(undefined)"); - if (sym.weakRef()) { - try writer.writeAll(" weak"); - } - if (sym.ext()) { - try writer.writeAll(" external"); - } - try writer.print(" {s} (from {s})\n", .{ - sym_name, - import_name, - }); - } else unreachable; - } + if (symtab) |stab| { + try dumpSymtab(sections.items, imports.items, stab, writer); } return output.toOwnedSlice(); @@ -696,10 +767,67 @@ const MachODumper = struct { else => {}, } } + + fn dumpSymtab( + sections: []const macho.section_64, + imports: []const []const u8, + symtab: Symtab, + writer: anytype, + ) !void { + try writer.writeAll(symtab_label ++ "\n"); + + for (symtab.symbols) |sym| { + if (sym.stab()) continue; + const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(symtab.strings.ptr + sym.n_strx)), 0); + if (sym.sect()) { + const sect = sections[sym.n_sect - 1]; + try writer.print("{x} ({s},{s})", .{ + sym.n_value, + sect.segName(), + sect.sectName(), + }); + if (sym.ext()) { + try writer.writeAll(" external"); + } + try writer.print(" {s}\n", .{sym_name}); + } else if (sym.undf()) { + const ordinal = @divTrunc(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); + const import_name = blk: { + if (ordinal <= 0) { + if (ordinal == macho.BIND_SPECIAL_DYLIB_SELF) + break :blk "self import"; + if (ordinal == macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) + break :blk "main executable"; + if (ordinal == macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP) + break :blk "flat lookup"; + unreachable; + } + const full_path = imports[@as(u16, @bitCast(ordinal)) - 1]; + const basename = fs.path.basename(full_path); + assert(basename.len > 0); + const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len; + break :blk basename[0..ext]; + }; + try writer.writeAll("(undefined)"); + if (sym.weakRef()) { + try writer.writeAll(" weak"); + } + if (sym.ext()) { + try writer.writeAll(" external"); + } + try writer.print(" {s} (from {s})\n", .{ + sym_name, + import_name, + }); + } else unreachable; + } + } }; const ElfDumper = struct { - const symtab_label = "symtab"; + const symtab_label = "symbol table"; + const dynamic_symtab_label = "dynamic symbol table"; + const dynamic_section_label = "dynamic section"; const Symtab = struct { symbols: []align(1) const elf.Elf64_Sym, @@ -727,7 +855,7 @@ const ElfDumper = struct { dysymtab: ?Symtab = null, }; - fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 { + fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 { const gpa = step.owner.allocator; var stream = std.io.fixedBufferStream(bytes); const reader = stream.reader(); @@ -750,34 +878,32 @@ const ElfDumper = struct { }; ctx.shstrtab = getSectionContents(ctx, ctx.hdr.e_shstrndx); - if (opts.dump_symtab) { - for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) { - elf.SHT_SYMTAB, elf.SHT_DYNSYM => { - const raw = getSectionContents(ctx, i); - const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym)); - const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms]; - const strings = getSectionContents(ctx, shdr.sh_link); - - switch (shdr.sh_type) { - elf.SHT_SYMTAB => { - ctx.symtab = .{ - .symbols = symbols, - .strings = strings, - }; - }, - elf.SHT_DYNSYM => { - ctx.dysymtab = .{ - .symbols = symbols, - .strings = strings, - }; - }, - else => unreachable, - } - }, + for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) { + elf.SHT_SYMTAB, elf.SHT_DYNSYM => { + const raw = getSectionContents(ctx, i); + const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym)); + const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms]; + const strings = getSectionContents(ctx, shdr.sh_link); + + switch (shdr.sh_type) { + elf.SHT_SYMTAB => { + ctx.symtab = .{ + .symbols = symbols, + .strings = strings, + }; + }, + elf.SHT_DYNSYM => { + ctx.dysymtab = .{ + .symbols = symbols, + .strings = strings, + }; + }, + else => unreachable, + } + }, - else => {}, - }; - } + else => {}, + }; var output = std.ArrayList(u8).init(gpa); const writer = output.writer(); @@ -785,15 +911,16 @@ const ElfDumper = struct { try dumpHeader(ctx, writer); try dumpShdrs(ctx, writer); try dumpPhdrs(ctx, writer); - try dumpDynamic(ctx, writer); + try dumpDynamicSection(ctx, writer); + try dumpSymtab(ctx, .symtab, writer); + try dumpSymtab(ctx, .dysymtab, writer); return output.toOwnedSlice(); } - fn getSectionName(ctx: Context, shndx: usize) []const u8 { + inline fn getSectionName(ctx: Context, shndx: usize) []const u8 { const shdr = ctx.shdrs[shndx]; - assert(shdr.sh_name < ctx.shstrtab.len); - return mem.sliceTo(@as([*:0]const u8, @ptrCast(ctx.shstrtab.ptr + shdr.sh_name)), 0); + return getString(ctx.shstrtab, shdr.sh_name); } fn getSectionContents(ctx: Context, shndx: usize) []const u8 { @@ -835,7 +962,7 @@ const ElfDumper = struct { } } - fn dumpDynamic(ctx: Context, writer: anytype) !void { + fn dumpDynamicSection(ctx: Context, writer: anytype) !void { const shndx = getSectionByName(ctx, ".dynamic") orelse return; const shdr = ctx.shdrs[shndx]; const strtab = getSectionContents(ctx, shdr.sh_link); @@ -843,6 +970,8 @@ const ElfDumper = struct { const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn)); const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries]; + try writer.writeAll(ElfDumper.dynamic_section_label ++ "\n"); + for (entries) |entry| { const key = @as(u64, @bitCast(entry.d_tag)); const value = entry.d_val; @@ -1072,17 +1201,98 @@ const ElfDumper = struct { try writer.writeAll(p_type); } } + + fn dumpSymtab(ctx: Context, comptime @"type": enum { symtab, dysymtab }, writer: anytype) !void { + const symtab = switch (@"type") { + .symtab => ctx.symtab, + .dysymtab => ctx.dysymtab, + } orelse return; + + try writer.writeAll(switch (@"type") { + .symtab => symtab_label, + .dysymtab => dynamic_symtab_label, + } ++ "\n"); + + for (symtab.symbols, 0..) |sym, index| { + try writer.print("{x} {x}", .{ sym.st_value, sym.st_size }); + + { + const tt = sym.st_type(); + if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { + try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); + } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { + try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); + } else { + const sym_type = switch (tt) { + elf.STT_NOTYPE => "NOTYPE", + elf.STT_OBJECT => "OBJECT", + elf.STT_FUNC => "FUNC", + elf.STT_SECTION => "SECTION", + elf.STT_FILE => "FILE", + elf.STT_COMMON => "COMMON", + elf.STT_TLS => "TLS", + elf.STT_NUM => "NUM", + else => "UNK", + }; + try writer.print(" {s}", .{sym_type}); + } + } + + { + const bind = sym.st_bind(); + if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { + try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); + } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { + try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); + } else { + const sym_bind = switch (bind) { + elf.STB_LOCAL => "LOCAL", + elf.STB_GLOBAL => "GLOBAL", + elf.STB_WEAK => "WEAK", + elf.STB_NUM => "NUM", + else => "UNKNOWN", + }; + try writer.print(" {s}", .{sym_bind}); + } + } + + const sym_vis = @as(elf.STV, @enumFromInt(sym.st_other)); + try writer.print(" {s}", .{@tagName(sym_vis)}); + + { + if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) { + if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) { + try writer.print(" LO+{d}", .{sym.st_shndx - elf.SHN_LOPROC}); + } else { + const sym_ndx = &switch (sym.st_shndx) { + elf.SHN_ABS => "ABS", + elf.SHN_COMMON => "COM", + elf.SHN_LIVEPATCH => "LIV", + else => "UNK", + }; + try writer.print(" {s}", .{sym_ndx}); + } + } else if (sym.st_shndx == elf.SHN_UNDEF) { + try writer.writeAll(" UND"); + } else { + try writer.print(" {d}", .{sym.st_shndx}); + } + } + + const sym_name = switch (sym.st_type()) { + elf.STT_SECTION => getSectionName(ctx, sym.st_shndx), + else => symtab.getName(index).?, + }; + try writer.print(" {s}\n", .{sym_name}); + } + } }; const WasmDumper = struct { const symtab_label = "symbols"; - fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 { + fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 { const gpa = step.owner.allocator; - if (opts.dump_symtab) { - @panic("TODO: Implement symbol table parsing and dumping"); - } - var fbs = std.io.fixedBufferStream(bytes); const reader = fbs.reader(); diff --git a/test/link/macho/dead_strip/build.zig b/test/link/macho/dead_strip/build.zig index 4489fdf3ad..8dd75b7019 100644 --- a/test/link/macho/dead_strip/build.zig +++ b/test/link/macho/dead_strip/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void { const check = exe.checkObject(); check.checkInSymtab(); - check.checkNext("{*} (__TEXT,__text) external _iAmUnused"); + check.checkContains("(__TEXT,__text) external _iAmUnused"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void { const check = exe.checkObject(); check.checkInSymtab(); - check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused"); + check.checkNotPresent("(__TEXT,__text) external _iAmUnused"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/dead_strip_dylibs/build.zig b/test/link/macho/dead_strip_dylibs/build.zig index c226e03196..4767a7cf34 100644 --- a/test/link/macho/dead_strip_dylibs/build.zig +++ b/test/link/macho/dead_strip_dylibs/build.zig @@ -19,11 +19,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const exe = createScenario(b, optimize, "no-dead-strip"); const check = exe.checkObject(); - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name {*}Cocoa"); + check.checkStart(); + check.checkExact("cmd LOAD_DYLIB"); + check.checkContains("Cocoa"); - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name {*}libobjc{*}.dylib"); + check.checkStart(); + check.checkExact("cmd LOAD_DYLIB"); + check.checkContains("libobjc"); test_step.dependOn(&check.step); diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index 499a0089e1..de693625d0 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -25,11 +25,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize dylib.linkLibC(); const check_dylib = dylib.checkObject(); - check_dylib.checkStart("cmd ID_DYLIB"); - check_dylib.checkNext("name @rpath/liba.dylib"); - check_dylib.checkNext("timestamp 2"); - check_dylib.checkNext("current version 10000"); - check_dylib.checkNext("compatibility version 10000"); + check_dylib.checkStart(); + check_dylib.checkExact("cmd ID_DYLIB"); + check_dylib.checkExact("name @rpath/liba.dylib"); + check_dylib.checkExact("timestamp 2"); + check_dylib.checkExact("current version 10000"); + check_dylib.checkExact("compatibility version 10000"); test_step.dependOn(&check_dylib.step); @@ -45,14 +46,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.linkLibC(); const check_exe = exe.checkObject(); - check_exe.checkStart("cmd LOAD_DYLIB"); - check_exe.checkNext("name @rpath/liba.dylib"); - check_exe.checkNext("timestamp 2"); - check_exe.checkNext("current version 10000"); - check_exe.checkNext("compatibility version 10000"); - - check_exe.checkStart("cmd RPATH"); - check_exe.checkNextFileSource("path", dylib.getOutputDirectorySource()); + check_exe.checkStart(); + check_exe.checkExact("cmd LOAD_DYLIB"); + check_exe.checkExact("name @rpath/liba.dylib"); + check_exe.checkExact("timestamp 2"); + check_exe.checkExact("current version 10000"); + check_exe.checkExact("compatibility version 10000"); + + check_exe.checkStart(); + check_exe.checkExact("cmd RPATH"); + check_exe.checkExactFileSource("path", dylib.getOutputDirectorySource()); test_step.dependOn(&check_exe.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index 1e0c146e11..a5269ff6ab 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -24,14 +24,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const check_exe = exe.checkObject(); - check_exe.checkStart("segname __TEXT"); - check_exe.checkNext("vmaddr {vmaddr}"); + check_exe.checkStart(); + check_exe.checkExact("segname __TEXT"); + check_exe.checkExtract("vmaddr {vmaddr}"); - check_exe.checkStart("cmd MAIN"); - check_exe.checkNext("entryoff {entryoff}"); + check_exe.checkStart(); + check_exe.checkExact("cmd MAIN"); + check_exe.checkExtract("entryoff {entryoff}"); check_exe.checkInSymtab(); - check_exe.checkNext("{n_value} (__TEXT,__text) external _non_main"); + check_exe.checkExtract("{n_value} (__TEXT,__text) external _non_main"); check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } }); test_step.dependOn(&check_exe.step); diff --git a/test/link/macho/entry_in_dylib/build.zig b/test/link/macho/entry_in_dylib/build.zig index cf1d028706..a5deaa73fe 100644 --- a/test/link/macho/entry_in_dylib/build.zig +++ b/test/link/macho/entry_in_dylib/build.zig @@ -34,14 +34,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.forceUndefinedSymbol("_my_main"); const check_exe = exe.checkObject(); - check_exe.checkStart("segname __TEXT"); - check_exe.checkNext("vmaddr {text_vmaddr}"); + check_exe.checkStart(); + check_exe.checkExact("segname __TEXT"); + check_exe.checkExtract("vmaddr {text_vmaddr}"); - check_exe.checkStart("sectname __stubs"); - check_exe.checkNext("addr {stubs_vmaddr}"); + check_exe.checkStart(); + check_exe.checkExact("sectname __stubs"); + check_exe.checkExtract("addr {stubs_vmaddr}"); - check_exe.checkStart("cmd MAIN"); - check_exe.checkNext("entryoff {entryoff}"); + check_exe.checkStart(); + check_exe.checkExact("cmd MAIN"); + check_exe.checkExtract("entryoff {entryoff}"); check_exe.checkComputeCompare("text_vmaddr entryoff +", .{ .op = .eq, diff --git a/test/link/macho/headerpad/build.zig b/test/link/macho/headerpad/build.zig index 99edfe72fa..2c1146f249 100644 --- a/test/link/macho/headerpad/build.zig +++ b/test/link/macho/headerpad/build.zig @@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.headerpad_max_install_names = true; const check = exe.checkObject(); - check.checkStart("sectname __text"); - check.checkNext("offset {offset}"); + check.checkStart(); + check.checkExact("sectname __text"); + check.checkExtract("offset {offset}"); switch (builtin.cpu.arch) { .aarch64 => { @@ -46,8 +47,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.headerpad_size = 0x10000; const check = exe.checkObject(); - check.checkStart("sectname __text"); - check.checkNext("offset {offset}"); + check.checkStart(); + check.checkExact("sectname __text"); + check.checkExtract("offset {offset}"); check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } }); test_step.dependOn(&check.step); @@ -63,8 +65,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.headerpad_size = 0x10000; const check = exe.checkObject(); - check.checkStart("sectname __text"); - check.checkNext("offset {offset}"); + check.checkStart(); + check.checkExact("sectname __text"); + check.checkExtract("offset {offset}"); check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } }); test_step.dependOn(&check.step); @@ -80,8 +83,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.headerpad_max_install_names = true; const check = exe.checkObject(); - check.checkStart("sectname __text"); - check.checkNext("offset {offset}"); + check.checkStart(); + check.checkExact("sectname __text"); + check.checkExtract("offset {offset}"); switch (builtin.cpu.arch) { .aarch64 => { diff --git a/test/link/macho/linksection/build.zig b/test/link/macho/linksection/build.zig index b8b3a59f35..113034f96f 100644 --- a/test/link/macho/linksection/build.zig +++ b/test/link/macho/linksection/build.zig @@ -25,14 +25,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const check = obj.checkObject(); check.checkInSymtab(); - check.checkNext("{*} (__DATA,__TestGlobal) external _test_global"); + check.checkContains("(__DATA,__TestGlobal) external _test_global"); check.checkInSymtab(); - check.checkNext("{*} (__TEXT,__TestFn) external _testFn"); + check.checkContains("(__TEXT,__TestFn) external _testFn"); if (optimize == .Debug) { check.checkInSymtab(); - check.checkNext("{*} (__TEXT,__TestGenFnA) _main.testGenericFn__anon_{*}"); + check.checkContains("(__TEXT,__TestGenFnA) _main.testGenericFn__anon_"); } test_step.dependOn(&check.step); diff --git a/test/link/macho/needed_framework/build.zig b/test/link/macho/needed_framework/build.zig index fc80a44f73..2323f277e6 100644 --- a/test/link/macho/needed_framework/build.zig +++ b/test/link/macho/needed_framework/build.zig @@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.dead_strip_dylibs = true; const check = exe.checkObject(); - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name {*}Cocoa"); + check.checkStart(); + check.checkExact("cmd LOAD_DYLIB"); + check.checkContains("Cocoa"); test_step.dependOn(&check.step); const run_cmd = b.addRunArtifact(exe); diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index 4b7dbfd1e0..26c69ea815 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -39,8 +39,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.dead_strip_dylibs = true; const check = exe.checkObject(); - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name @rpath/liba.dylib"); + check.checkStart(); + check.checkExact("cmd LOAD_DYLIB"); + check.checkExact("name @rpath/liba.dylib"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index b467df2b20..f7b13e171e 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -20,13 +20,15 @@ pub fn build(b: *std.Build) void { exe.pagezero_size = 0x4000; const check = exe.checkObject(); - check.checkStart("LC 0"); - check.checkNext("segname __PAGEZERO"); - check.checkNext("vmaddr 0"); - check.checkNext("vmsize 4000"); + check.checkStart(); + check.checkExact("LC 0"); + check.checkExact("segname __PAGEZERO"); + check.checkExact("vmaddr 0"); + check.checkExact("vmsize 4000"); - check.checkStart("segname __TEXT"); - check.checkNext("vmaddr 4000"); + check.checkStart(); + check.checkExact("segname __TEXT"); + check.checkExact("vmaddr 4000"); test_step.dependOn(&check.step); } @@ -42,9 +44,10 @@ pub fn build(b: *std.Build) void { exe.pagezero_size = 0; const check = exe.checkObject(); - check.checkStart("LC 0"); - check.checkNext("segname __TEXT"); - check.checkNext("vmaddr 0"); + check.checkStart(); + check.checkExact("LC 0"); + check.checkExact("segname __TEXT"); + check.checkExact("vmaddr 0"); test_step.dependOn(&check.step); } diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index b30540b818..df0cab9d5d 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.search_strategy = .dylibs_first; const check = exe.checkObject(); - check.checkStart("cmd LOAD_DYLIB"); - check.checkNext("name @rpath/libsearch_dylibs_first.dylib"); + check.checkStart(); + check.checkExact("cmd LOAD_DYLIB"); + check.checkExact("name @rpath/libsearch_dylibs_first.dylib"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index 81ca052545..d7ea44b626 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -25,8 +25,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.stack_size = 0x100000000; const check_exe = exe.checkObject(); - check_exe.checkStart("cmd MAIN"); - check_exe.checkNext("stacksize 100000000"); + check_exe.checkStart(); + check_exe.checkExact("cmd MAIN"); + check_exe.checkExact("stacksize 100000000"); test_step.dependOn(&check_exe.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/strict_validation/build.zig b/test/link/macho/strict_validation/build.zig index fd62a4712e..cc2825a458 100644 --- a/test/link/macho/strict_validation/build.zig +++ b/test/link/macho/strict_validation/build.zig @@ -26,44 +26,51 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const check_exe = exe.checkObject(); - check_exe.checkStart("cmd SEGMENT_64"); - check_exe.checkNext("segname __LINKEDIT"); - check_exe.checkNext("fileoff {fileoff}"); - check_exe.checkNext("filesz {filesz}"); - - check_exe.checkStart("cmd DYLD_INFO_ONLY"); - check_exe.checkNext("rebaseoff {rebaseoff}"); - check_exe.checkNext("rebasesize {rebasesize}"); - check_exe.checkNext("bindoff {bindoff}"); - check_exe.checkNext("bindsize {bindsize}"); - check_exe.checkNext("lazybindoff {lazybindoff}"); - check_exe.checkNext("lazybindsize {lazybindsize}"); - check_exe.checkNext("exportoff {exportoff}"); - check_exe.checkNext("exportsize {exportsize}"); - - check_exe.checkStart("cmd FUNCTION_STARTS"); - check_exe.checkNext("dataoff {fstartoff}"); - check_exe.checkNext("datasize {fstartsize}"); - - check_exe.checkStart("cmd DATA_IN_CODE"); - check_exe.checkNext("dataoff {diceoff}"); - check_exe.checkNext("datasize {dicesize}"); - - check_exe.checkStart("cmd SYMTAB"); - check_exe.checkNext("symoff {symoff}"); - check_exe.checkNext("nsyms {symnsyms}"); - check_exe.checkNext("stroff {stroff}"); - check_exe.checkNext("strsize {strsize}"); - - check_exe.checkStart("cmd DYSYMTAB"); - check_exe.checkNext("indirectsymoff {dysymoff}"); - check_exe.checkNext("nindirectsyms {dysymnsyms}"); + check_exe.checkStart(); + check_exe.checkExact("cmd SEGMENT_64"); + check_exe.checkExact("segname __LINKEDIT"); + check_exe.checkExtract("fileoff {fileoff}"); + check_exe.checkExtract("filesz {filesz}"); + + check_exe.checkStart(); + check_exe.checkExact("cmd DYLD_INFO_ONLY"); + check_exe.checkExtract("rebaseoff {rebaseoff}"); + check_exe.checkExtract("rebasesize {rebasesize}"); + check_exe.checkExtract("bindoff {bindoff}"); + check_exe.checkExtract("bindsize {bindsize}"); + check_exe.checkExtract("lazybindoff {lazybindoff}"); + check_exe.checkExtract("lazybindsize {lazybindsize}"); + check_exe.checkExtract("exportoff {exportoff}"); + check_exe.checkExtract("exportsize {exportsize}"); + + check_exe.checkStart(); + check_exe.checkExact("cmd FUNCTION_STARTS"); + check_exe.checkExtract("dataoff {fstartoff}"); + check_exe.checkExtract("datasize {fstartsize}"); + + check_exe.checkStart(); + check_exe.checkExact("cmd DATA_IN_CODE"); + check_exe.checkExtract("dataoff {diceoff}"); + check_exe.checkExtract("datasize {dicesize}"); + + check_exe.checkStart(); + check_exe.checkExact("cmd SYMTAB"); + check_exe.checkExtract("symoff {symoff}"); + check_exe.checkExtract("nsyms {symnsyms}"); + check_exe.checkExtract("stroff {stroff}"); + check_exe.checkExtract("strsize {strsize}"); + + check_exe.checkStart(); + check_exe.checkExact("cmd DYSYMTAB"); + check_exe.checkExtract("indirectsymoff {dysymoff}"); + check_exe.checkExtract("nindirectsyms {dysymnsyms}"); switch (builtin.cpu.arch) { .aarch64 => { - check_exe.checkStart("cmd CODE_SIGNATURE"); - check_exe.checkNext("dataoff {codesigoff}"); - check_exe.checkNext("datasize {codesigsize}"); + check_exe.checkStart(); + check_exe.checkExact("cmd CODE_SIGNATURE"); + check_exe.checkExtract("dataoff {codesigoff}"); + check_exe.checkExtract("datasize {codesigsize}"); }, .x86_64 => {}, else => unreachable, diff --git a/test/link/macho/unwind_info/build.zig b/test/link/macho/unwind_info/build.zig index bc0373e693..c3b9d1ea4f 100644 --- a/test/link/macho/unwind_info/build.zig +++ b/test/link/macho/unwind_info/build.zig @@ -32,20 +32,21 @@ fn testUnwindInfo( exe.link_gc_sections = dead_strip; const check = exe.checkObject(); - check.checkStart("segname __TEXT"); - check.checkNext("sectname __gcc_except_tab"); - check.checkNext("sectname __unwind_info"); + check.checkStart(); + check.checkExact("segname __TEXT"); + check.checkExact("sectname __gcc_except_tab"); + check.checkExact("sectname __unwind_info"); switch (builtin.cpu.arch) { .aarch64 => { - check.checkNext("sectname __eh_frame"); + check.checkExact("sectname __eh_frame"); }, .x86_64 => {}, // We do not expect `__eh_frame` section on x86_64 in this case else => unreachable, } check.checkInSymtab(); - check.checkNext("{*} (__TEXT,__text) external ___gxx_personality_v0"); + check.checkContains("(__TEXT,__text) external ___gxx_personality_v0"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); diff --git a/test/link/macho/weak_framework/build.zig b/test/link/macho/weak_framework/build.zig index 5dfd421243..8069d62bd3 100644 --- a/test/link/macho/weak_framework/build.zig +++ b/test/link/macho/weak_framework/build.zig @@ -23,8 +23,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.linkFrameworkWeak("Cocoa"); const check = exe.checkObject(); - check.checkStart("cmd LOAD_WEAK_DYLIB"); - check.checkNext("name {*}Cocoa"); + check.checkStart(); + check.checkExact("cmd LOAD_WEAK_DYLIB"); + check.checkContains("Cocoa"); test_step.dependOn(&check.step); const run_cmd = b.addRunArtifact(exe); diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig index 79b7c748b8..831a96e880 100644 --- a/test/link/macho/weak_library/build.zig +++ b/test/link/macho/weak_library/build.zig @@ -37,14 +37,15 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); const check = exe.checkObject(); - check.checkStart("cmd LOAD_WEAK_DYLIB"); - check.checkNext("name @rpath/liba.dylib"); + check.checkStart(); + check.checkExact("cmd LOAD_WEAK_DYLIB"); + check.checkExact("name @rpath/liba.dylib"); check.checkInSymtab(); - check.checkNext("(undefined) weak external _a (from liba)"); + check.checkExact("(undefined) weak external _a (from liba)"); check.checkInSymtab(); - check.checkNext("(undefined) weak external _asStr (from liba)"); + check.checkExact("(undefined) weak external _asStr (from liba)"); test_step.dependOn(&check.step); const run = b.addRunArtifact(exe); diff --git a/test/link/wasm/archive/build.zig b/test/link/wasm/archive/build.zig index 120134af19..d87b8e973e 100644 --- a/test/link/wasm/archive/build.zig +++ b/test/link/wasm/archive/build.zig @@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize lib.strip = false; const check = lib.checkObject(); - check.checkStart("Section custom"); - check.checkNext("name __trunch"); // Ensure it was imported and resolved + check.checkStart(); + check.checkExact("Section custom"); + check.checkExact("name __trunch"); // Ensure it was imported and resolved test_step.dependOn(&check.step); } diff --git a/test/link/wasm/basic-features/build.zig b/test/link/wasm/basic-features/build.zig index be709a698f..703bd13feb 100644 --- a/test/link/wasm/basic-features/build.zig +++ b/test/link/wasm/basic-features/build.zig @@ -20,9 +20,10 @@ pub fn build(b: *std.Build) void { // Verify the result contains the features explicitly set on the target for the library. const check = lib.checkObject(); - check.checkStart("name target_features"); - check.checkNext("features 1"); - check.checkNext("+ atomics"); + check.checkStart(); + check.checkExact("name target_features"); + check.checkExact("features 1"); + check.checkExact("+ atomics"); const test_step = b.step("test", "Run linker test"); test_step.dependOn(&check.step); diff --git a/test/link/wasm/bss/build.zig b/test/link/wasm/bss/build.zig index 4a26e78a12..e6cb9d4f3d 100644 --- a/test/link/wasm/bss/build.zig +++ b/test/link/wasm/bss/build.zig @@ -29,28 +29,31 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt const check_lib = lib.checkObject(); // since we import memory, make sure it exists with the correct naming - check_lib.checkStart("Section import"); - check_lib.checkNext("entries 1"); - check_lib.checkNext("module env"); // default module name is "env" - check_lib.checkNext("name memory"); // as per linker specification + check_lib.checkStart(); + check_lib.checkExact("Section import"); + check_lib.checkExact("entries 1"); + check_lib.checkExact("module env"); // default module name is "env" + check_lib.checkExact("name memory"); // as per linker specification // since we are importing memory, ensure it's not exported + check_lib.checkStart(); check_lib.checkNotPresent("Section export"); // validate the name of the stack pointer - check_lib.checkStart("Section custom"); - check_lib.checkNext("type data_segment"); - check_lib.checkNext("names 2"); - check_lib.checkNext("index 0"); - check_lib.checkNext("name .rodata"); + check_lib.checkStart(); + check_lib.checkExact("Section custom"); + check_lib.checkExact("type data_segment"); + check_lib.checkExact("names 2"); + check_lib.checkExact("index 0"); + check_lib.checkExact("name .rodata"); // for safe optimization modes `undefined` is stored in data instead of bss. if (is_safe) { - check_lib.checkNext("index 1"); - check_lib.checkNext("name .data"); + check_lib.checkExact("index 1"); + check_lib.checkExact("name .data"); check_lib.checkNotPresent("name .bss"); } else { - check_lib.checkNext("index 1"); // bss section always last - check_lib.checkNext("name .bss"); + check_lib.checkExact("index 1"); // bss section always last + check_lib.checkExact("name .bss"); } test_step.dependOn(&check_lib.step); } @@ -70,13 +73,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt lib.import_memory = true; const check_lib = lib.checkObject(); - check_lib.checkStart("Section custom"); - check_lib.checkNext("type data_segment"); - check_lib.checkNext("names 2"); - check_lib.checkNext("index 0"); - check_lib.checkNext("name .rodata"); - check_lib.checkNext("index 1"); - check_lib.checkNext("name .bss"); + check_lib.checkStart(); + check_lib.checkExact("Section custom"); + check_lib.checkExact("type data_segment"); + check_lib.checkExact("names 2"); + check_lib.checkExact("index 0"); + check_lib.checkExact("name .rodata"); + check_lib.checkExact("index 1"); + check_lib.checkExact("name .bss"); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/export-data/build.zig b/test/link/wasm/export-data/build.zig index 38b8c3e19e..7e3128aa76 100644 --- a/test/link/wasm/export-data/build.zig +++ b/test/link/wasm/export-data/build.zig @@ -21,26 +21,28 @@ pub fn build(b: *std.Build) void { const check_lib = lib.checkObject(); - check_lib.checkStart("Section global"); - check_lib.checkNext("entries 3"); - check_lib.checkNext("type i32"); // stack pointer so skip other fields - check_lib.checkNext("type i32"); - check_lib.checkNext("mutable false"); - check_lib.checkNext("i32.const {foo_address}"); - check_lib.checkNext("type i32"); - check_lib.checkNext("mutable false"); - check_lib.checkNext("i32.const {bar_address}"); + check_lib.checkStart(); + check_lib.checkExact("Section global"); + check_lib.checkExact("entries 3"); + check_lib.checkExact("type i32"); // stack pointer so skip other fields + check_lib.checkExact("type i32"); + check_lib.checkExact("mutable false"); + check_lib.checkExtract("i32.const {foo_address}"); + check_lib.checkExact("type i32"); + check_lib.checkExact("mutable false"); + check_lib.checkExtract("i32.const {bar_address}"); check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 4 } }); check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 0 } }); - check_lib.checkStart("Section export"); - check_lib.checkNext("entries 3"); - check_lib.checkNext("name foo"); - check_lib.checkNext("kind global"); - check_lib.checkNext("index 1"); - check_lib.checkNext("name bar"); - check_lib.checkNext("kind global"); - check_lib.checkNext("index 2"); + check_lib.checkStart(); + check_lib.checkExact("Section export"); + check_lib.checkExact("entries 3"); + check_lib.checkExact("name foo"); + check_lib.checkExact("kind global"); + check_lib.checkExact("index 1"); + check_lib.checkExact("name bar"); + check_lib.checkExact("kind global"); + check_lib.checkExact("index 2"); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/export/build.zig b/test/link/wasm/export/build.zig index 794201dbf6..5afe2df768 100644 --- a/test/link/wasm/export/build.zig +++ b/test/link/wasm/export/build.zig @@ -43,22 +43,25 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize force_export.use_lld = false; const check_no_export = no_export.checkObject(); - check_no_export.checkStart("Section export"); - check_no_export.checkNext("entries 1"); - check_no_export.checkNext("name memory"); - check_no_export.checkNext("kind memory"); + check_no_export.checkStart(); + check_no_export.checkExact("Section export"); + check_no_export.checkExact("entries 1"); + check_no_export.checkExact("name memory"); + check_no_export.checkExact("kind memory"); const check_dynamic_export = dynamic_export.checkObject(); - check_dynamic_export.checkStart("Section export"); - check_dynamic_export.checkNext("entries 2"); - check_dynamic_export.checkNext("name foo"); - check_dynamic_export.checkNext("kind function"); + check_dynamic_export.checkStart(); + check_dynamic_export.checkExact("Section export"); + check_dynamic_export.checkExact("entries 2"); + check_dynamic_export.checkExact("name foo"); + check_dynamic_export.checkExact("kind function"); const check_force_export = force_export.checkObject(); - check_force_export.checkStart("Section export"); - check_force_export.checkNext("entries 2"); - check_force_export.checkNext("name foo"); - check_force_export.checkNext("kind function"); + check_force_export.checkStart(); + check_force_export.checkExact("Section export"); + check_force_export.checkExact("entries 2"); + check_force_export.checkExact("name foo"); + check_force_export.checkExact("kind function"); test_step.dependOn(&check_no_export.step); test_step.dependOn(&check_dynamic_export.step); diff --git a/test/link/wasm/extern-mangle/build.zig b/test/link/wasm/extern-mangle/build.zig index 6c292acbab..841d118efd 100644 --- a/test/link/wasm/extern-mangle/build.zig +++ b/test/link/wasm/extern-mangle/build.zig @@ -21,12 +21,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize lib.rdynamic = true; // export `foo` const check_lib = lib.checkObject(); - check_lib.checkStart("Section import"); - check_lib.checkNext("entries 2"); // a.hello & b.hello - check_lib.checkNext("module a"); - check_lib.checkNext("name hello"); - check_lib.checkNext("module b"); - check_lib.checkNext("name hello"); + check_lib.checkStart(); + check_lib.checkExact("Section import"); + check_lib.checkExact("entries 2"); // a.hello & b.hello + check_lib.checkExact("module a"); + check_lib.checkExact("name hello"); + check_lib.checkExact("module b"); + check_lib.checkExact("name hello"); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/function-table/build.zig b/test/link/wasm/function-table/build.zig index 4ce6294727..796ba670ad 100644 --- a/test/link/wasm/function-table/build.zig +++ b/test/link/wasm/function-table/build.zig @@ -46,31 +46,36 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const check_export = export_table.checkObject(); const check_regular = regular_table.checkObject(); - check_import.checkStart("Section import"); - check_import.checkNext("entries 1"); - check_import.checkNext("module env"); - check_import.checkNext("name __indirect_function_table"); - check_import.checkNext("kind table"); - check_import.checkNext("type funcref"); - check_import.checkNext("min 1"); // 1 function pointer + check_import.checkStart(); + check_import.checkExact("Section import"); + check_import.checkExact("entries 1"); + check_import.checkExact("module env"); + check_import.checkExact("name __indirect_function_table"); + check_import.checkExact("kind table"); + check_import.checkExact("type funcref"); + check_import.checkExact("min 1"); // 1 function pointer check_import.checkNotPresent("max"); // when importing, we do not provide a max check_import.checkNotPresent("Section table"); // we're importing it - check_export.checkStart("Section export"); - check_export.checkNext("entries 2"); - check_export.checkNext("name __indirect_function_table"); // as per linker specification - check_export.checkNext("kind table"); + check_export.checkStart(); + check_export.checkExact("Section export"); + check_export.checkExact("entries 2"); + check_export.checkExact("name __indirect_function_table"); // as per linker specification + check_export.checkExact("kind table"); - check_regular.checkStart("Section table"); - check_regular.checkNext("entries 1"); - check_regular.checkNext("type funcref"); - check_regular.checkNext("min 2"); // index starts at 1 & 1 function pointer = 2. - check_regular.checkNext("max 2"); - check_regular.checkStart("Section element"); - check_regular.checkNext("entries 1"); - check_regular.checkNext("table index 0"); - check_regular.checkNext("i32.const 1"); // we want to start function indexes at 1 - check_regular.checkNext("indexes 1"); // 1 function pointer + check_regular.checkStart(); + check_regular.checkExact("Section table"); + check_regular.checkExact("entries 1"); + check_regular.checkExact("type funcref"); + check_regular.checkExact("min 2"); // index starts at 1 & 1 function pointer = 2. + check_regular.checkExact("max 2"); + + check_regular.checkStart(); + check_regular.checkExact("Section element"); + check_regular.checkExact("entries 1"); + check_regular.checkExact("table index 0"); + check_regular.checkExact("i32.const 1"); // we want to start function indexes at 1 + check_regular.checkExact("indexes 1"); // 1 function pointer test_step.dependOn(&check_import.step); test_step.dependOn(&check_export.step); diff --git a/test/link/wasm/infer-features/build.zig b/test/link/wasm/infer-features/build.zig index 00fb48651b..35ab68abf6 100644 --- a/test/link/wasm/infer-features/build.zig +++ b/test/link/wasm/infer-features/build.zig @@ -33,15 +33,16 @@ pub fn build(b: *std.Build) void { // Verify the result contains the features from the C Object file. const check = lib.checkObject(); - check.checkStart("name target_features"); - check.checkNext("features 7"); - check.checkNext("+ atomics"); - check.checkNext("+ bulk-memory"); - check.checkNext("+ mutable-globals"); - check.checkNext("+ nontrapping-fptoint"); - check.checkNext("+ sign-ext"); - check.checkNext("+ simd128"); - check.checkNext("+ tail-call"); + check.checkStart(); + check.checkExact("name target_features"); + check.checkExact("features 7"); + check.checkExact("+ atomics"); + check.checkExact("+ bulk-memory"); + check.checkExact("+ mutable-globals"); + check.checkExact("+ nontrapping-fptoint"); + check.checkExact("+ sign-ext"); + check.checkExact("+ simd128"); + check.checkExact("+ tail-call"); const test_step = b.step("test", "Run linker test"); test_step.dependOn(&check.step); diff --git a/test/link/wasm/producers/build.zig b/test/link/wasm/producers/build.zig index 439a66cfa5..f541a1c8ec 100644 --- a/test/link/wasm/producers/build.zig +++ b/test/link/wasm/producers/build.zig @@ -28,16 +28,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const version_fmt = "version " ++ builtin.zig_version_string; const check_lib = lib.checkObject(); - check_lib.checkStart("name producers"); - check_lib.checkNext("fields 2"); - check_lib.checkNext("field_name language"); - check_lib.checkNext("values 1"); - check_lib.checkNext("value_name Zig"); - check_lib.checkNext(version_fmt); - check_lib.checkNext("field_name processed-by"); - check_lib.checkNext("values 1"); - check_lib.checkNext("value_name Zig"); - check_lib.checkNext(version_fmt); + check_lib.checkStart(); + check_lib.checkExact("name producers"); + check_lib.checkExact("fields 2"); + check_lib.checkExact("field_name language"); + check_lib.checkExact("values 1"); + check_lib.checkExact("value_name Zig"); + check_lib.checkExact(version_fmt); + check_lib.checkExact("field_name processed-by"); + check_lib.checkExact("values 1"); + check_lib.checkExact("value_name Zig"); + check_lib.checkExact(version_fmt); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/segments/build.zig b/test/link/wasm/segments/build.zig index 59fab9bb84..d01c34f90d 100644 --- a/test/link/wasm/segments/build.zig +++ b/test/link/wasm/segments/build.zig @@ -25,16 +25,20 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize b.installArtifact(lib); const check_lib = lib.checkObject(); - check_lib.checkStart("Section data"); - check_lib.checkNext("entries 2"); // rodata & data, no bss because we're exporting memory + check_lib.checkStart(); + check_lib.checkExact("Section data"); + check_lib.checkExact("entries 2"); // rodata & data, no bss because we're exporting memory - check_lib.checkStart("Section custom"); - check_lib.checkStart("name name"); // names custom section - check_lib.checkStart("type data_segment"); - check_lib.checkNext("names 2"); - check_lib.checkNext("index 0"); - check_lib.checkNext("name .rodata"); - check_lib.checkNext("index 1"); - check_lib.checkNext("name .data"); + check_lib.checkStart(); + check_lib.checkExact("Section custom"); + check_lib.checkStart(); + check_lib.checkExact("name name"); // names custom section + check_lib.checkStart(); + check_lib.checkExact("type data_segment"); + check_lib.checkExact("names 2"); + check_lib.checkExact("index 0"); + check_lib.checkExact("name .rodata"); + check_lib.checkExact("index 1"); + check_lib.checkExact("name .data"); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/stack_pointer/build.zig b/test/link/wasm/stack_pointer/build.zig index bf643c1e3a..ce724e5736 100644 --- a/test/link/wasm/stack_pointer/build.zig +++ b/test/link/wasm/stack_pointer/build.zig @@ -28,23 +28,26 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const check_lib = lib.checkObject(); // ensure global exists and its initial value is equal to explitic stack size - check_lib.checkStart("Section global"); - check_lib.checkNext("entries 1"); - check_lib.checkNext("type i32"); // on wasm32 the stack pointer must be i32 - check_lib.checkNext("mutable true"); // must be able to mutate the stack pointer - check_lib.checkNext("i32.const {stack_pointer}"); + check_lib.checkStart(); + check_lib.checkExact("Section global"); + check_lib.checkExact("entries 1"); + check_lib.checkExact("type i32"); // on wasm32 the stack pointer must be i32 + check_lib.checkExact("mutable true"); // must be able to mutate the stack pointer + check_lib.checkExtract("i32.const {stack_pointer}"); check_lib.checkComputeCompare("stack_pointer", .{ .op = .eq, .value = .{ .literal = lib.stack_size.? } }); // validate memory section starts after virtual stack - check_lib.checkNext("Section data"); - check_lib.checkNext("i32.const {data_start}"); + check_lib.checkStart(); + check_lib.checkExact("Section data"); + check_lib.checkExtract("i32.const {data_start}"); check_lib.checkComputeCompare("data_start", .{ .op = .eq, .value = .{ .variable = "stack_pointer" } }); // validate the name of the stack pointer - check_lib.checkStart("Section custom"); - check_lib.checkNext("type global"); - check_lib.checkNext("names 1"); - check_lib.checkNext("index 0"); - check_lib.checkNext("name __stack_pointer"); + check_lib.checkStart(); + check_lib.checkExact("Section custom"); + check_lib.checkExact("type global"); + check_lib.checkExact("names 1"); + check_lib.checkExact("index 0"); + check_lib.checkExact("name __stack_pointer"); test_step.dependOn(&check_lib.step); } diff --git a/test/link/wasm/type/build.zig b/test/link/wasm/type/build.zig index 72a9261750..7110f465f4 100644 --- a/test/link/wasm/type/build.zig +++ b/test/link/wasm/type/build.zig @@ -25,17 +25,18 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize b.installArtifact(lib); const check_lib = lib.checkObject(); - check_lib.checkStart("Section type"); + check_lib.checkStart(); + check_lib.checkExact("Section type"); // only 2 entries, although we have more functions. // This is to test functions with the same function signature // have their types deduplicated. - check_lib.checkNext("entries 2"); - check_lib.checkNext("params 1"); - check_lib.checkNext("type i32"); - check_lib.checkNext("returns 1"); - check_lib.checkNext("type i64"); - check_lib.checkNext("params 0"); - check_lib.checkNext("returns 0"); + check_lib.checkExact("entries 2"); + check_lib.checkExact("params 1"); + check_lib.checkExact("type i32"); + check_lib.checkExact("returns 1"); + check_lib.checkExact("type i64"); + check_lib.checkExact("params 0"); + check_lib.checkExact("returns 0"); test_step.dependOn(&check_lib.step); } -- cgit v1.2.3 From 3b6200db41f201839a7238c95ddc2e1323d8acbd Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 20 Jul 2023 15:36:58 +0200 Subject: check-object: dump PT flags when dumping program headers --- lib/std/Build/Step/CheckObject.zig | 137 +++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 58 deletions(-) (limited to 'lib/std/Build/Step/CheckObject.zig') diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 6cf007c5b8..6466708080 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -950,6 +950,8 @@ const ElfDumper = struct { fn dumpShdrs(ctx: Context, writer: anytype) !void { if (ctx.shdrs.len == 0) return; + try writer.writeAll("section headers\n"); + for (ctx.shdrs, 0..) |shdr, shndx| { try writer.print("shdr {d}\n", .{shndx}); try writer.print("name {s}\n", .{getSectionName(ctx, shndx)}); @@ -1113,46 +1115,46 @@ const ElfDumper = struct { ) !void { _ = unused_fmt_string; _ = options; - if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) { - try writer.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS}); - } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) { - try writer.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC}); - } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) { - try writer.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER}); - } else { - const name = switch (sh_type) { - elf.SHT_NULL => "NULL", - elf.SHT_PROGBITS => "PROGBITS", - elf.SHT_SYMTAB => "SYMTAB", - elf.SHT_STRTAB => "STRTAB", - elf.SHT_RELA => "RELA", - elf.SHT_HASH => "HASH", - elf.SHT_DYNAMIC => "DYNAMIC", - elf.SHT_NOTE => "NOTE", - elf.SHT_NOBITS => "NOBITS", - elf.SHT_REL => "REL", - elf.SHT_SHLIB => "SHLIB", - elf.SHT_DYNSYM => "DYNSYM", - elf.SHT_INIT_ARRAY => "INIT_ARRAY", - elf.SHT_FINI_ARRAY => "FINI_ARRAY", - elf.SHT_PREINIT_ARRAY => "PREINIT_ARRAY", - elf.SHT_GROUP => "GROUP", - elf.SHT_SYMTAB_SHNDX => "SYMTAB_SHNDX", - elf.SHT_X86_64_UNWIND => "X86_64_UNWIND", - elf.SHT_LLVM_ADDRSIG => "LLVM_ADDRSIG", - elf.SHT_GNU_HASH => "GNU_HASH", - elf.SHT_GNU_VERDEF => "VERDEF", - elf.SHT_GNU_VERNEED => "VERNEED", - elf.SHT_GNU_VERSYM => "VERSYM", - else => "UNKNOWN", - }; - try writer.writeAll(name); - } + const name = switch (sh_type) { + elf.SHT_NULL => "NULL", + elf.SHT_PROGBITS => "PROGBITS", + elf.SHT_SYMTAB => "SYMTAB", + elf.SHT_STRTAB => "STRTAB", + elf.SHT_RELA => "RELA", + elf.SHT_HASH => "HASH", + elf.SHT_DYNAMIC => "DYNAMIC", + elf.SHT_NOTE => "NOTE", + elf.SHT_NOBITS => "NOBITS", + elf.SHT_REL => "REL", + elf.SHT_SHLIB => "SHLIB", + elf.SHT_DYNSYM => "DYNSYM", + elf.SHT_INIT_ARRAY => "INIT_ARRAY", + elf.SHT_FINI_ARRAY => "FINI_ARRAY", + elf.SHT_PREINIT_ARRAY => "PREINIT_ARRAY", + elf.SHT_GROUP => "GROUP", + elf.SHT_SYMTAB_SHNDX => "SYMTAB_SHNDX", + elf.SHT_X86_64_UNWIND => "X86_64_UNWIND", + elf.SHT_LLVM_ADDRSIG => "LLVM_ADDRSIG", + elf.SHT_GNU_HASH => "GNU_HASH", + elf.SHT_GNU_VERDEF => "VERDEF", + elf.SHT_GNU_VERNEED => "VERNEED", + elf.SHT_GNU_VERSYM => "VERSYM", + else => if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) { + return try writer.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS}); + } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) { + return try writer.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC}); + } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) { + return try writer.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER}); + } else "UNKNOWN", + }; + try writer.writeAll(name); } fn dumpPhdrs(ctx: Context, writer: anytype) !void { if (ctx.phdrs.len == 0) return; + try writer.writeAll("program headers\n"); + for (ctx.phdrs, 0..) |phdr, phndx| { try writer.print("phdr {d}\n", .{phndx}); try writer.print("type {s}\n", .{fmtPhType(phdr.p_type)}); @@ -1162,7 +1164,28 @@ const ElfDumper = struct { try writer.print("memsz {x}\n", .{phdr.p_memsz}); try writer.print("filesz {x}\n", .{phdr.p_filesz}); try writer.print("align {x}\n", .{phdr.p_align}); - // TODO dump formatted p_flags + + { + const flags = phdr.p_flags; + try writer.writeAll("flags"); + if (flags > 0) try writer.writeByte(' '); + if (flags & elf.PF_R != 0) { + try writer.writeByte('R'); + } + if (flags & elf.PF_W != 0) { + try writer.writeByte('W'); + } + if (flags & elf.PF_X != 0) { + try writer.writeByte('E'); + } + if (flags & elf.PF_MASKOS != 0) { + try writer.writeAll("OS"); + } + if (flags & elf.PF_MASKPROC != 0) { + try writer.writeAll("PROC"); + } + try writer.writeByte('\n'); + } } } @@ -1178,28 +1201,26 @@ const ElfDumper = struct { ) !void { _ = unused_fmt_string; _ = options; - if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) { - try writer.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS}); - } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) { - try writer.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC}); - } else { - const p_type = switch (ph_type) { - elf.PT_NULL => "NULL", - elf.PT_LOAD => "LOAD", - elf.PT_DYNAMIC => "DYNAMIC", - elf.PT_INTERP => "INTERP", - elf.PT_NOTE => "NOTE", - elf.PT_SHLIB => "SHLIB", - elf.PT_PHDR => "PHDR", - elf.PT_TLS => "TLS", - elf.PT_NUM => "NUM", - elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME", - elf.PT_GNU_STACK => "GNU_STACK", - elf.PT_GNU_RELRO => "GNU_RELRO", - else => "UNKNOWN", - }; - try writer.writeAll(p_type); - } + const p_type = switch (ph_type) { + elf.PT_NULL => "NULL", + elf.PT_LOAD => "LOAD", + elf.PT_DYNAMIC => "DYNAMIC", + elf.PT_INTERP => "INTERP", + elf.PT_NOTE => "NOTE", + elf.PT_SHLIB => "SHLIB", + elf.PT_PHDR => "PHDR", + elf.PT_TLS => "TLS", + elf.PT_NUM => "NUM", + elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME", + elf.PT_GNU_STACK => "GNU_STACK", + elf.PT_GNU_RELRO => "GNU_RELRO", + else => if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) { + return try writer.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS}); + } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) { + return try writer.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC}); + } else "UNKNOWN", + }; + try writer.writeAll(p_type); } fn dumpSymtab(ctx: Context, comptime @"type": enum { symtab, dysymtab }, writer: anytype) !void { -- cgit v1.2.3 From 245f6553e6840b2221141f3e7724ae6a73294387 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 20 Jul 2023 20:00:44 +0200 Subject: check-object: format known OS-specific types before doing generic format --- lib/std/Build/Step/CheckObject.zig | 83 ++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 43 deletions(-) (limited to 'lib/std/Build/Step/CheckObject.zig') diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 6466708080..3fe84658a2 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -1237,49 +1237,6 @@ const ElfDumper = struct { for (symtab.symbols, 0..) |sym, index| { try writer.print("{x} {x}", .{ sym.st_value, sym.st_size }); - { - const tt = sym.st_type(); - if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { - try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); - } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { - try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); - } else { - const sym_type = switch (tt) { - elf.STT_NOTYPE => "NOTYPE", - elf.STT_OBJECT => "OBJECT", - elf.STT_FUNC => "FUNC", - elf.STT_SECTION => "SECTION", - elf.STT_FILE => "FILE", - elf.STT_COMMON => "COMMON", - elf.STT_TLS => "TLS", - elf.STT_NUM => "NUM", - else => "UNK", - }; - try writer.print(" {s}", .{sym_type}); - } - } - - { - const bind = sym.st_bind(); - if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { - try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); - } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { - try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); - } else { - const sym_bind = switch (bind) { - elf.STB_LOCAL => "LOCAL", - elf.STB_GLOBAL => "GLOBAL", - elf.STB_WEAK => "WEAK", - elf.STB_NUM => "NUM", - else => "UNKNOWN", - }; - try writer.print(" {s}", .{sym_bind}); - } - } - - const sym_vis = @as(elf.STV, @enumFromInt(sym.st_other)); - try writer.print(" {s}", .{@tagName(sym_vis)}); - { if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) { if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) { @@ -1300,6 +1257,46 @@ const ElfDumper = struct { } } + blk: { + const tt = sym.st_type(); + const sym_type = switch (tt) { + elf.STT_NOTYPE => "NOTYPE", + elf.STT_OBJECT => "OBJECT", + elf.STT_FUNC => "FUNC", + elf.STT_SECTION => "SECTION", + elf.STT_FILE => "FILE", + elf.STT_COMMON => "COMMON", + elf.STT_TLS => "TLS", + elf.STT_NUM => "NUM", + elf.STT_GNU_IFUNC => "IFUNC", + else => if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { + break :blk try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); + } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { + break :blk try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); + } else "UNK", + }; + try writer.print(" {s}", .{sym_type}); + } + + blk: { + const bind = sym.st_bind(); + const sym_bind = switch (bind) { + elf.STB_LOCAL => "LOCAL", + elf.STB_GLOBAL => "GLOBAL", + elf.STB_WEAK => "WEAK", + elf.STB_NUM => "NUM", + else => if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { + break :blk try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); + } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { + break :blk try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); + } else "UNKNOWN", + }; + try writer.print(" {s}", .{sym_bind}); + } + + const sym_vis = @as(elf.STV, @enumFromInt(sym.st_other)); + try writer.print(" {s}", .{@tagName(sym_vis)}); + const sym_name = switch (sym.st_type()) { elf.STT_SECTION => getSectionName(ctx, sym.st_shndx), else => symtab.getName(index).?, -- cgit v1.2.3 From c0260d39d555b9cd0c0abc1f9f61ece55b992b1e Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 20 Jul 2023 22:12:06 +0200 Subject: check-object: allow for multiple extractions within one check --- lib/std/Build/Step/CheckObject.zig | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'lib/std/Build/Step/CheckObject.zig') diff --git a/lib/std/Build/Step/CheckObject.zig b/lib/std/Build/Step/CheckObject.zig index 3fe84658a2..5a816b4103 100644 --- a/lib/std/Build/Step/CheckObject.zig +++ b/lib/std/Build/Step/CheckObject.zig @@ -80,7 +80,7 @@ const Action = struct { const hay = mem.trim(u8, haystack, " "); const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); - var candidate_var: ?struct { name: []const u8, value: u64 } = null; + var candidate_vars = std.ArrayList(struct { name: []const u8, value: u64 }).init(b.allocator); var hay_it = mem.tokenizeScalar(u8, hay, ' '); var needle_it = mem.tokenizeScalar(u8, phrase, ' '); @@ -92,18 +92,21 @@ const Action = struct { const name = needle_tok[1..closing_brace]; if (name.len == 0) return error.MissingBraceValue; - const value = try std.fmt.parseInt(u64, hay_tok, 16); - candidate_var = .{ + const value = std.fmt.parseInt(u64, hay_tok, 16) catch return false; + try candidate_vars.append(.{ .name = name, .value = value, - }; + }); } else { if (!mem.eql(u8, hay_tok, needle_tok)) return false; } } - if (candidate_var) |v| try global_vars.putNoClobber(v.name, v.value); - return candidate_var != null; + if (candidate_vars.items.len == 0) return false; + + for (candidate_vars.items) |cv| try global_vars.putNoClobber(cv.name, cv.value); + + return true; } /// Returns true if the `phrase` is an exact match with the haystack. @@ -1253,7 +1256,7 @@ const ElfDumper = struct { } else if (sym.st_shndx == elf.SHN_UNDEF) { try writer.writeAll(" UND"); } else { - try writer.print(" {d}", .{sym.st_shndx}); + try writer.print(" {x}", .{sym.st_shndx}); } } -- cgit v1.2.3