authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-07-21 07:25:44+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-21 07:25:44+02:00
logc43ee5bb22298eefc3fae919807f5da8f7be70f1
tree0a9fc22f34fbb1d4ed764a4df63b830c1d99441e
parent8a18abfd60392a3adcfc4e6cfa712f63ecf2bf67
parentc0260d39d555b9cd0c0abc1f9f61ece55b992b1e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16456 from ziglang/check-object-more-elf

std: dump .dynamic, .symtab, .dysym for ELF in `CheckObject`; remove wildcard matchers in favour of `checkContains` helper

29 files changed, 917 insertions(+), 480 deletions(-)

lib/std/Build/Step/CheckObject.zig+625-246
...@@ -18,7 +18,6 @@ step: Step,...@@ -18,7 +18,6 @@ step: Step,
18source: std.Build.FileSource,18source: std.Build.FileSource,
19max_bytes: usize = 20 * 1024 * 1024,19max_bytes: usize = 20 * 1024 * 1024,
20checks: std.ArrayList(Check),20checks: std.ArrayList(Check),
21dump_symtab: bool = false,
22obj_format: std.Target.ObjectFormat,21obj_format: std.Target.ObjectFormat,
2322
24pub fn create(23pub fn create(
...@@ -53,84 +52,104 @@ const SearchPhrase = struct {...@@ -53,84 +52,104 @@ const SearchPhrase = struct {
53 }52 }
54};53};
5554
56/// There two types of actions currently supported:55/// There five types of actions currently supported:
57/// * `.match` - is the main building block of standard matchers with optional eat-all token `{*}`56/// .exact - will do an exact match against the haystack
58/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature57/// .contains - will check for existence within the haystack
59/// i.e., it won't really handle edge cases/nontrivial examples. But given that we do want to use58/// .not_present - will check for non-existence within the haystack
60/// it mainly to test the output of our object format parser-dumpers when testing the linkers, etc.59/// .extract - will do an exact match and extract into a variable enclosed within `{name}` braces
61/// it should be plenty useful in its current form.60/// .compute_cmp - will perform an operation on the extracted global variables
62/// * `.compute_cmp` - can be used to perform an operation on the extracted global variables
63/// using the MatchAction. It currently only supports an addition. The operation is required61/// using the MatchAction. It currently only supports an addition. The operation is required
64/// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well,62/// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well,
65/// to avoid any parsing really).63/// to avoid any parsing really).
66/// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively64/// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively
67/// they could then be added with this simple program `vmaddr entryoff +`.65/// they could then be added with this simple program `vmaddr entryoff +`.
68const Action = struct {66const Action = struct {
69 tag: enum { match, not_present, compute_cmp },67 tag: enum { exact, contains, not_present, extract, compute_cmp },
70 phrase: SearchPhrase,68 phrase: SearchPhrase,
71 expected: ?ComputeCompareExpected = null,69 expected: ?ComputeCompareExpected = null,
7270
73 /// Will return true if the `phrase` was found in the `haystack`.71 /// Returns true if the `phrase` is an exact match with the haystack and variable was successfully extracted.
74 /// Some examples include:72 fn extract(
75 ///
76 /// LC 0 => will match in its entirety
77 /// vmaddr {vmaddr} => will match `vmaddr` and then extract the following value as u64
78 /// and save under `vmaddr` global name (see `global_vars` param)
79 /// name {*}libobjc{*}.dylib => will match `name` followed by a token which contains `libobjc` and `.dylib`
80 /// in that order with other letters in between
81 fn match(
82 act: Action,73 act: Action,
83 b: *std.Build,74 b: *std.Build,
84 step: *Step,75 step: *Step,
85 haystack: []const u8,76 haystack: []const u8,
86 global_vars: anytype,77 global_vars: anytype,
87 ) !bool {78 ) !bool {
88 assert(act.tag == .match or act.tag == .not_present);79 assert(act.tag == .extract);
89 const phrase = act.phrase.resolve(b, step);80 const hay = mem.trim(u8, haystack, " ");
90 var candidate_var: ?struct { name: []const u8, value: u64 } = null;81 const phrase = mem.trim(u8, act.phrase.resolve(b, step), " ");
91 var hay_it = mem.tokenizeScalar(u8, mem.trim(u8, haystack, " "), ' ');82
92 var needle_it = mem.tokenizeScalar(u8, mem.trim(u8, phrase, " "), ' ');83 var candidate_vars = std.ArrayList(struct { name: []const u8, value: u64 }).init(b.allocator);
84 var hay_it = mem.tokenizeScalar(u8, hay, ' ');
85 var needle_it = mem.tokenizeScalar(u8, phrase, ' ');
9386
94 while (needle_it.next()) |needle_tok| {87 while (needle_it.next()) |needle_tok| {
95 const hay_tok = hay_it.next() orelse return false;88 const hay_tok = hay_it.next() orelse break;
9689 if (mem.startsWith(u8, needle_tok, "{")) {
97 if (mem.indexOf(u8, needle_tok, "{*}")) |index| {
98 // We have fuzzy matchers within the search pattern, so we match substrings.
99 var start = index;
100 var n_tok = needle_tok;
101 var h_tok = hay_tok;
102 while (true) {
103 n_tok = n_tok[start + 3 ..];
104 const inner = if (mem.indexOf(u8, n_tok, "{*}")) |sub_end|
105 n_tok[0..sub_end]
106 else
107 n_tok;
108 if (mem.indexOf(u8, h_tok, inner) == null) return false;
109 start = mem.indexOf(u8, n_tok, "{*}") orelse break;
110 }
111 } else if (mem.startsWith(u8, needle_tok, "{")) {
112 const closing_brace = mem.indexOf(u8, needle_tok, "}") orelse return error.MissingClosingBrace;90 const closing_brace = mem.indexOf(u8, needle_tok, "}") orelse return error.MissingClosingBrace;
113 if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast;91 if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast;
11492
115 const name = needle_tok[1..closing_brace];93 const name = needle_tok[1..closing_brace];
116 if (name.len == 0) return error.MissingBraceValue;94 if (name.len == 0) return error.MissingBraceValue;
117 const value = try std.fmt.parseInt(u64, hay_tok, 16);95 const value = std.fmt.parseInt(u64, hay_tok, 16) catch return false;
118 candidate_var = .{96 try candidate_vars.append(.{
119 .name = name,97 .name = name,
120 .value = value,98 .value = value,
121 };99 });
122 } else {100 } else {
123 if (!mem.eql(u8, hay_tok, needle_tok)) return false;101 if (!mem.eql(u8, hay_tok, needle_tok)) return false;
124 }102 }
125 }103 }
126104
127 if (candidate_var) |v| {105 if (candidate_vars.items.len == 0) return false;
128 try global_vars.putNoClobber(v.name, v.value);106
129 }107 for (candidate_vars.items) |cv| try global_vars.putNoClobber(cv.name, cv.value);
130108
131 return true;109 return true;
132 }110 }
133111
112 /// Returns true if the `phrase` is an exact match with the haystack.
113 fn exact(
114 act: Action,
115 b: *std.Build,
116 step: *Step,
117 haystack: []const u8,
118 ) bool {
119 assert(act.tag == .exact);
120 const hay = mem.trim(u8, haystack, " ");
121 const phrase = mem.trim(u8, act.phrase.resolve(b, step), " ");
122 return mem.eql(u8, hay, phrase);
123 }
124
125 /// Returns true if the `phrase` exists within the haystack.
126 fn contains(
127 act: Action,
128 b: *std.Build,
129 step: *Step,
130 haystack: []const u8,
131 ) bool {
132 assert(act.tag == .contains);
133 const hay = mem.trim(u8, haystack, " ");
134 const phrase = mem.trim(u8, act.phrase.resolve(b, step), " ");
135 return mem.indexOf(u8, hay, phrase) != null;
136 }
137
138 /// Returns true if the `phrase` does not exist within the haystack.
139 fn notPresent(
140 act: Action,
141 b: *std.Build,
142 step: *Step,
143 haystack: []const u8,
144 ) bool {
145 assert(act.tag == .not_present);
146 return !contains(.{
147 .tag = .contains,
148 .phrase = act.phrase,
149 .expected = act.expected,
150 }, b, step, haystack);
151 }
152
134 /// Will return true if the `phrase` is correctly parsed into an RPN program and153 /// Will return true if the `phrase` is correctly parsed into an RPN program and
135 /// its reduced, computed value compares using `op` with the expected value, either154 /// its reduced, computed value compares using `op` with the expected value, either
136 /// a literal or another extracted variable.155 /// a literal or another extracted variable.
...@@ -235,9 +254,23 @@ const Check = struct {...@@ -235,9 +254,23 @@ const Check = struct {
235 };254 };
236 }255 }
237256
238 fn match(self: *Check, phrase: SearchPhrase) void {257 fn extract(self: *Check, phrase: SearchPhrase) void {
258 self.actions.append(.{
259 .tag = .extract,
260 .phrase = phrase,
261 }) catch @panic("OOM");
262 }
263
264 fn exact(self: *Check, phrase: SearchPhrase) void {
239 self.actions.append(.{265 self.actions.append(.{
240 .tag = .match,266 .tag = .exact,
267 .phrase = phrase,
268 }) catch @panic("OOM");
269 }
270
271 fn contains(self: *Check, phrase: SearchPhrase) void {
272 self.actions.append(.{
273 .tag = .contains,
241 .phrase = phrase,274 .phrase = phrase,
242 }) catch @panic("OOM");275 }) catch @panic("OOM");
243 }276 }
...@@ -258,52 +291,118 @@ const Check = struct {...@@ -258,52 +291,118 @@ const Check = struct {
258 }291 }
259};292};
260293
261/// Creates a new sequence of actions with `phrase` as the first anchor searched phrase.294/// Creates a new empty sequence of actions.
262pub fn checkStart(self: *CheckObject, phrase: []const u8) void {295pub fn checkStart(self: *CheckObject) void {
263 var new_check = Check.create(self.step.owner.allocator);296 var new_check = Check.create(self.step.owner.allocator);
264 new_check.match(.{ .string = self.step.owner.dupe(phrase) });
265 self.checks.append(new_check) catch @panic("OOM");297 self.checks.append(new_check) catch @panic("OOM");
266}298}
267299
268/// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)`.300/// Adds an exact match phrase to the latest created Check with `CheckObject.checkStart()`.
269/// Asserts at least one check already exists.301pub fn checkExact(self: *CheckObject, phrase: []const u8) void {
270pub fn checkNext(self: *CheckObject, phrase: []const u8) void {302 self.checkExactInner(phrase, null);
303}
304
305/// Like `checkExact()` but takes an additional argument `FileSource` which will be
306/// resolved to a full search query in `make()`.
307pub fn checkExactFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
308 self.checkExactInner(phrase, file_source);
309}
310
311fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
271 assert(self.checks.items.len > 0);312 assert(self.checks.items.len > 0);
272 const last = &self.checks.items[self.checks.items.len - 1];313 const last = &self.checks.items[self.checks.items.len - 1];
273 last.match(.{ .string = self.step.owner.dupe(phrase) });314 last.exact(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
315}
316
317/// Adds a fuzzy match phrase to the latest created Check with `CheckObject.checkStart()`.
318pub fn checkContains(self: *CheckObject, phrase: []const u8) void {
319 self.checkContainsInner(phrase, null);
274}320}
275321
276/// Like `checkNext()` but takes an additional argument `FileSource` which will be322/// Like `checkContains()` but takes an additional argument `FileSource` which will be
277/// resolved to a full search query in `make()`.323/// resolved to a full search query in `make()`.
278pub fn checkNextFileSource(324pub fn checkContainsFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
279 self: *CheckObject,325 self.checkContainsInner(phrase, file_source);
280 phrase: []const u8,326}
281 file_source: std.Build.FileSource,327
282) void {328fn checkContainsInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
329 assert(self.checks.items.len > 0);
330 const last = &self.checks.items[self.checks.items.len - 1];
331 last.contains(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
332}
333
334/// Adds an exact match phrase with variable extractor to the latest created Check
335/// with `CheckObject.checkStart()`.
336pub fn checkExtract(self: *CheckObject, phrase: []const u8) void {
337 self.checkExtractInner(phrase, null);
338}
339
340/// Like `checkExtract()` but takes an additional argument `FileSource` which will be
341/// resolved to a full search query in `make()`.
342pub fn checkExtractFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
343 self.checkExtractInner(phrase, file_source);
344}
345
346fn checkExtractInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
283 assert(self.checks.items.len > 0);347 assert(self.checks.items.len > 0);
284 const last = &self.checks.items[self.checks.items.len - 1];348 const last = &self.checks.items[self.checks.items.len - 1];
285 last.match(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });349 last.extract(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
286}350}
287351
288/// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)`352/// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)`
289/// however ensures there is no matching phrase in the output.353/// however ensures there is no matching phrase in the output.
290/// Asserts at least one check already exists.
291pub fn checkNotPresent(self: *CheckObject, phrase: []const u8) void {354pub fn checkNotPresent(self: *CheckObject, phrase: []const u8) void {
355 self.checkNotPresentInner(phrase, null);
356}
357
358/// Like `checkExtract()` but takes an additional argument `FileSource` which will be
359/// resolved to a full search query in `make()`.
360pub fn checkNotPresentFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void {
361 self.checkNotPresentInner(phrase, file_source);
362}
363
364fn checkNotPresentInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void {
292 assert(self.checks.items.len > 0);365 assert(self.checks.items.len > 0);
293 const last = &self.checks.items[self.checks.items.len - 1];366 const last = &self.checks.items[self.checks.items.len - 1];
294 last.notPresent(.{ .string = self.step.owner.dupe(phrase) });367 last.notPresent(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source });
295}368}
296369
297/// Creates a new check checking specifically symbol table parsed and dumped from the object370/// Creates a new check checking specifically symbol table parsed and dumped from the object
298/// file.371/// file.
299/// Issuing this check will force parsing and dumping of the symbol table.
300pub fn checkInSymtab(self: *CheckObject) void {372pub fn checkInSymtab(self: *CheckObject) void {
301 self.dump_symtab = true;373 const label = switch (self.obj_format) {
302 const symtab_label = switch (self.obj_format) {
303 .macho => MachODumper.symtab_label,374 .macho => MachODumper.symtab_label,
304 else => @panic("TODO other parsers"),375 .elf => ElfDumper.symtab_label,
376 .wasm => WasmDumper.symtab_label,
377 .coff => @panic("TODO symtab for coff"),
378 else => @panic("TODO other file formats"),
305 };379 };
306 self.checkStart(symtab_label);380 self.checkStart();
381 self.checkExact(label);
382}
383
384/// Creates a new check checking specifically dynamic symbol table parsed and dumped from the object
385/// file.
386/// This check is target-dependent and applicable to ELF only.
387pub fn checkInDynamicSymtab(self: *CheckObject) void {
388 const label = switch (self.obj_format) {
389 .elf => ElfDumper.dynamic_symtab_label,
390 else => @panic("Unsupported target platform"),
391 };
392 self.checkStart();
393 self.checkExact(label);
394}
395
396/// Creates a new check checking specifically dynamic section parsed and dumped from the object
397/// file.
398/// This check is target-dependent and applicable to ELF only.
399pub fn checkInDynamicSection(self: *CheckObject) void {
400 const label = switch (self.obj_format) {
401 .elf => ElfDumper.dynamic_section_label,
402 else => @panic("Unsupported target platform"),
403 };
404 self.checkStart();
405 self.checkExact(label);
307}406}
308407
309/// Creates a new standalone, singular check which allows running simple binary operations408/// Creates a new standalone, singular check which allows running simple binary operations
...@@ -336,16 +435,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -336,16 +435,10 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
336 ) catch |err| return step.fail("unable to read '{s}': {s}", .{ src_path, @errorName(err) });435 ) catch |err| return step.fail("unable to read '{s}': {s}", .{ src_path, @errorName(err) });
337436
338 const output = switch (self.obj_format) {437 const output = switch (self.obj_format) {
339 .macho => try MachODumper.parseAndDump(step, contents, .{438 .macho => try MachODumper.parseAndDump(step, contents),
340 .dump_symtab = self.dump_symtab,439 .elf => try ElfDumper.parseAndDump(step, contents),
341 }),
342 .elf => try ElfDumper.parseAndDump(step, contents, .{
343 .dump_symtab = self.dump_symtab,
344 }),
345 .coff => @panic("TODO coff parser"),440 .coff => @panic("TODO coff parser"),
346 .wasm => try WasmDumper.parseAndDump(step, contents, .{441 .wasm => try WasmDumper.parseAndDump(step, contents),
347 .dump_symtab = self.dump_symtab,
348 }),
349 else => unreachable,442 else => unreachable,
350 };443 };
351444
...@@ -355,9 +448,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -355,9 +448,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
355 var it = mem.tokenizeAny(u8, output, "\r\n");448 var it = mem.tokenizeAny(u8, output, "\r\n");
356 for (chk.actions.items) |act| {449 for (chk.actions.items) |act| {
357 switch (act.tag) {450 switch (act.tag) {
358 .match => {451 .exact => {
359 while (it.next()) |line| {452 while (it.next()) |line| {
360 if (try act.match(b, step, line, &vars)) break;453 if (act.exact(b, step, line)) break;
361 } else {454 } else {
362 return step.fail(455 return step.fail(
363 \\456 \\
...@@ -369,18 +462,46 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -369,18 +462,46 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
369 , .{ act.phrase.resolve(b, step), output });462 , .{ act.phrase.resolve(b, step), output });
370 }463 }
371 },464 },
465 .contains => {
466 while (it.next()) |line| {
467 if (act.contains(b, step, line)) break;
468 } else {
469 return step.fail(
470 \\
471 \\========= expected to find: ==========================
472 \\*{s}*
473 \\========= but parsed file does not contain it: =======
474 \\{s}
475 \\======================================================
476 , .{ act.phrase.resolve(b, step), output });
477 }
478 },
372 .not_present => {479 .not_present => {
373 while (it.next()) |line| {480 while (it.next()) |line| {
374 if (try act.match(b, step, line, &vars)) {481 if (act.notPresent(b, step, line)) break;
375 return step.fail(482 } else {
376 \\483 return step.fail(
377 \\========= expected not to find: ===================484 \\
378 \\{s}485 \\========= expected not to find: ===================
379 \\========= but parsed file does contain it: ========486 \\{s}
380 \\{s}487 \\========= but parsed file does contain it: ========
381 \\===================================================488 \\{s}
382 , .{ act.phrase.resolve(b, step), output });489 \\===================================================
383 }490 , .{ act.phrase.resolve(b, step), output });
491 }
492 },
493 .extract => {
494 while (it.next()) |line| {
495 if (try act.extract(b, step, line, &vars)) break;
496 } else {
497 return step.fail(
498 \\
499 \\========= expected to find and extract: ==============
500 \\{s}
501 \\========= but parsed file does not contain it: =======
502 \\{s}
503 \\======================================================
504 , .{ act.phrase.resolve(b, step), output });
384 }505 }
385 },506 },
386 .compute_cmp => {507 .compute_cmp => {
...@@ -410,15 +531,16 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -410,15 +531,16 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
410 }531 }
411}532}
412533
413const Opts = struct {
414 dump_symtab: bool = false,
415};
416
417const MachODumper = struct {534const MachODumper = struct {
418 const LoadCommandIterator = macho.LoadCommandIterator;535 const LoadCommandIterator = macho.LoadCommandIterator;
419 const symtab_label = "symtab";536 const symtab_label = "symbol table";
537
538 const Symtab = struct {
539 symbols: []align(1) const macho.nlist_64,
540 strings: []const u8,
541 };
420542
421 fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8, opts: Opts) ![]const u8 {543 fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8) ![]const u8 {
422 const gpa = step.owner.allocator;544 const gpa = step.owner.allocator;
423 var stream = std.io.fixedBufferStream(bytes);545 var stream = std.io.fixedBufferStream(bytes);
424 const reader = stream.reader();546 const reader = stream.reader();
...@@ -431,8 +553,7 @@ const MachODumper = struct {...@@ -431,8 +553,7 @@ const MachODumper = struct {
431 var output = std.ArrayList(u8).init(gpa);553 var output = std.ArrayList(u8).init(gpa);
432 const writer = output.writer();554 const writer = output.writer();
433555
434 var symtab: []const macho.nlist_64 = undefined;556 var symtab: ?Symtab = null;
435 var strtab: []const u8 = undefined;
436 var sections = std.ArrayList(macho.section_64).init(gpa);557 var sections = std.ArrayList(macho.section_64).init(gpa);
437 var imports = std.ArrayList([]const u8).init(gpa);558 var imports = std.ArrayList([]const u8).init(gpa);
438559
...@@ -450,13 +571,11 @@ const MachODumper = struct {...@@ -450,13 +571,11 @@ const MachODumper = struct {
450 sections.appendAssumeCapacity(sect);571 sections.appendAssumeCapacity(sect);
451 }572 }
452 },573 },
453 .SYMTAB => if (opts.dump_symtab) {574 .SYMTAB => {
454 const lc = cmd.cast(macho.symtab_command).?;575 const lc = cmd.cast(macho.symtab_command).?;
455 symtab = @as(576 const symbols = @as([*]align(1) const macho.nlist_64, @ptrCast(bytes.ptr + lc.symoff))[0..lc.nsyms];
456 [*]const macho.nlist_64,577 const strings = bytes[lc.stroff..][0..lc.strsize];
457 @ptrCast(@alignCast(&bytes[lc.symoff])),578 symtab = .{ .symbols = symbols, .strings = strings };
458 )[0..lc.nsyms];
459 strtab = bytes[lc.stroff..][0..lc.strsize];
460 },579 },
461 .LOAD_DYLIB,580 .LOAD_DYLIB,
462 .LOAD_WEAK_DYLIB,581 .LOAD_WEAK_DYLIB,
...@@ -473,53 +592,8 @@ const MachODumper = struct {...@@ -473,53 +592,8 @@ const MachODumper = struct {
473 i += 1;592 i += 1;
474 }593 }
475594
476 if (opts.dump_symtab) {595 if (symtab) |stab| {
477 try writer.print("{s}\n", .{symtab_label});596 try dumpSymtab(sections.items, imports.items, stab, writer);
478 for (symtab) |sym| {
479 if (sym.stab()) continue;
480 const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + sym.n_strx)), 0);
481 if (sym.sect()) {
482 const sect = sections.items[sym.n_sect - 1];
483 try writer.print("{x} ({s},{s})", .{
484 sym.n_value,
485 sect.segName(),
486 sect.sectName(),
487 });
488 if (sym.ext()) {
489 try writer.writeAll(" external");
490 }
491 try writer.print(" {s}\n", .{sym_name});
492 } else if (sym.undf()) {
493 const ordinal = @divTrunc(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER);
494 const import_name = blk: {
495 if (ordinal <= 0) {
496 if (ordinal == macho.BIND_SPECIAL_DYLIB_SELF)
497 break :blk "self import";
498 if (ordinal == macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
499 break :blk "main executable";
500 if (ordinal == macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
501 break :blk "flat lookup";
502 unreachable;
503 }
504 const full_path = imports.items[@as(u16, @bitCast(ordinal)) - 1];
505 const basename = fs.path.basename(full_path);
506 assert(basename.len > 0);
507 const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len;
508 break :blk basename[0..ext];
509 };
510 try writer.writeAll("(undefined)");
511 if (sym.weakRef()) {
512 try writer.writeAll(" weak");
513 }
514 if (sym.ext()) {
515 try writer.writeAll(" external");
516 }
517 try writer.print(" {s} (from {s})\n", .{
518 sym_name,
519 import_name,
520 });
521 } else unreachable;
522 }
523 }597 }
524598
525 return output.toOwnedSlice();599 return output.toOwnedSlice();
...@@ -696,10 +770,67 @@ const MachODumper = struct {...@@ -696,10 +770,67 @@ const MachODumper = struct {
696 else => {},770 else => {},
697 }771 }
698 }772 }
773
774 fn dumpSymtab(
775 sections: []const macho.section_64,
776 imports: []const []const u8,
777 symtab: Symtab,
778 writer: anytype,
779 ) !void {
780 try writer.writeAll(symtab_label ++ "\n");
781
782 for (symtab.symbols) |sym| {
783 if (sym.stab()) continue;
784 const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(symtab.strings.ptr + sym.n_strx)), 0);
785 if (sym.sect()) {
786 const sect = sections[sym.n_sect - 1];
787 try writer.print("{x} ({s},{s})", .{
788 sym.n_value,
789 sect.segName(),
790 sect.sectName(),
791 });
792 if (sym.ext()) {
793 try writer.writeAll(" external");
794 }
795 try writer.print(" {s}\n", .{sym_name});
796 } else if (sym.undf()) {
797 const ordinal = @divTrunc(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER);
798 const import_name = blk: {
799 if (ordinal <= 0) {
800 if (ordinal == macho.BIND_SPECIAL_DYLIB_SELF)
801 break :blk "self import";
802 if (ordinal == macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
803 break :blk "main executable";
804 if (ordinal == macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
805 break :blk "flat lookup";
806 unreachable;
807 }
808 const full_path = imports[@as(u16, @bitCast(ordinal)) - 1];
809 const basename = fs.path.basename(full_path);
810 assert(basename.len > 0);
811 const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len;
812 break :blk basename[0..ext];
813 };
814 try writer.writeAll("(undefined)");
815 if (sym.weakRef()) {
816 try writer.writeAll(" weak");
817 }
818 if (sym.ext()) {
819 try writer.writeAll(" external");
820 }
821 try writer.print(" {s} (from {s})\n", .{
822 sym_name,
823 import_name,
824 });
825 } else unreachable;
826 }
827 }
699};828};
700829
701const ElfDumper = struct {830const ElfDumper = struct {
702 const symtab_label = "symtab";831 const symtab_label = "symbol table";
832 const dynamic_symtab_label = "dynamic symbol table";
833 const dynamic_section_label = "dynamic section";
703834
704 const Symtab = struct {835 const Symtab = struct {
705 symbols: []align(1) const elf.Elf64_Sym,836 symbols: []align(1) const elf.Elf64_Sym,
...@@ -712,8 +843,7 @@ const ElfDumper = struct {...@@ -712,8 +843,7 @@ const ElfDumper = struct {
712843
713 fn getName(st: Symtab, index: usize) ?[]const u8 {844 fn getName(st: Symtab, index: usize) ?[]const u8 {
714 const sym = st.get(index) orelse return null;845 const sym = st.get(index) orelse return null;
715 assert(sym.st_name < st.strings.len);846 return getString(st.strings, sym.st_name);
716 return mem.sliceTo(@ptrCast(st.strings.ptr + sym.st_name), 0);
717 }847 }
718 };848 };
719849
...@@ -728,7 +858,7 @@ const ElfDumper = struct {...@@ -728,7 +858,7 @@ const ElfDumper = struct {
728 dysymtab: ?Symtab = null,858 dysymtab: ?Symtab = null,
729 };859 };
730860
731 fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 {861 fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 {
732 const gpa = step.owner.allocator;862 const gpa = step.owner.allocator;
733 var stream = std.io.fixedBufferStream(bytes);863 var stream = std.io.fixedBufferStream(bytes);
734 const reader = stream.reader();864 const reader = stream.reader();
...@@ -751,34 +881,32 @@ const ElfDumper = struct {...@@ -751,34 +881,32 @@ const ElfDumper = struct {
751 };881 };
752 ctx.shstrtab = getSectionContents(ctx, ctx.hdr.e_shstrndx);882 ctx.shstrtab = getSectionContents(ctx, ctx.hdr.e_shstrndx);
753883
754 if (opts.dump_symtab) {884 for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) {
755 for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) {885 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {
756 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {886 const raw = getSectionContents(ctx, i);
757 const raw = getSectionContents(ctx, i);887 const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym));
758 const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym));888 const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms];
759 const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms];889 const strings = getSectionContents(ctx, shdr.sh_link);
760 const strings = getSectionContents(ctx, shdr.sh_link);890
761891 switch (shdr.sh_type) {
762 switch (shdr.sh_type) {892 elf.SHT_SYMTAB => {
763 elf.SHT_SYMTAB => {893 ctx.symtab = .{
764 ctx.symtab = .{894 .symbols = symbols,
765 .symbols = symbols,895 .strings = strings,
766 .strings = strings,896 };
767 };897 },
768 },898 elf.SHT_DYNSYM => {
769 elf.SHT_DYNSYM => {899 ctx.dysymtab = .{
770 ctx.dysymtab = .{900 .symbols = symbols,
771 .symbols = symbols,901 .strings = strings,
772 .strings = strings,902 };
773 };903 },
774 },904 else => unreachable,
775 else => unreachable,905 }
776 }906 },
777 },
778907
779 else => {},908 else => {},
780 };909 };
781 }
782910
783 var output = std.ArrayList(u8).init(gpa);911 var output = std.ArrayList(u8).init(gpa);
784 const writer = output.writer();912 const writer = output.writer();
...@@ -786,14 +914,16 @@ const ElfDumper = struct {...@@ -786,14 +914,16 @@ const ElfDumper = struct {
786 try dumpHeader(ctx, writer);914 try dumpHeader(ctx, writer);
787 try dumpShdrs(ctx, writer);915 try dumpShdrs(ctx, writer);
788 try dumpPhdrs(ctx, writer);916 try dumpPhdrs(ctx, writer);
917 try dumpDynamicSection(ctx, writer);
918 try dumpSymtab(ctx, .symtab, writer);
919 try dumpSymtab(ctx, .dysymtab, writer);
789920
790 return output.toOwnedSlice();921 return output.toOwnedSlice();
791 }922 }
792923
793 fn getSectionName(ctx: Context, shndx: usize) []const u8 {924 inline fn getSectionName(ctx: Context, shndx: usize) []const u8 {
794 const shdr = ctx.shdrs[shndx];925 const shdr = ctx.shdrs[shndx];
795 assert(shdr.sh_name < ctx.shstrtab.len);926 return getString(ctx.shstrtab, shdr.sh_name);
796 return mem.sliceTo(@as([*:0]const u8, @ptrCast(ctx.shstrtab.ptr + shdr.sh_name)), 0);
797 }927 }
798928
799 fn getSectionContents(ctx: Context, shndx: usize) []const u8 {929 fn getSectionContents(ctx: Context, shndx: usize) []const u8 {
...@@ -803,6 +933,17 @@ const ElfDumper = struct {...@@ -803,6 +933,17 @@ const ElfDumper = struct {
803 return ctx.data[shdr.sh_offset..][0..shdr.sh_size];933 return ctx.data[shdr.sh_offset..][0..shdr.sh_size];
804 }934 }
805935
936 fn getSectionByName(ctx: Context, name: []const u8) ?usize {
937 for (0..ctx.shdrs.len) |shndx| {
938 if (mem.eql(u8, getSectionName(ctx, shndx), name)) return shndx;
939 } else return null;
940 }
941
942 fn getString(strtab: []const u8, off: u32) []const u8 {
943 assert(off < strtab.len);
944 return mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + off)), 0);
945 }
946
806 fn dumpHeader(ctx: Context, writer: anytype) !void {947 fn dumpHeader(ctx: Context, writer: anytype) !void {
807 try writer.writeAll("header\n");948 try writer.writeAll("header\n");
808 try writer.print("type {s}\n", .{@tagName(ctx.hdr.e_type)});949 try writer.print("type {s}\n", .{@tagName(ctx.hdr.e_type)});
...@@ -812,6 +953,8 @@ const ElfDumper = struct {...@@ -812,6 +953,8 @@ const ElfDumper = struct {
812 fn dumpShdrs(ctx: Context, writer: anytype) !void {953 fn dumpShdrs(ctx: Context, writer: anytype) !void {
813 if (ctx.shdrs.len == 0) return;954 if (ctx.shdrs.len == 0) return;
814955
956 try writer.writeAll("section headers\n");
957
815 for (ctx.shdrs, 0..) |shdr, shndx| {958 for (ctx.shdrs, 0..) |shdr, shndx| {
816 try writer.print("shdr {d}\n", .{shndx});959 try writer.print("shdr {d}\n", .{shndx});
817 try writer.print("name {s}\n", .{getSectionName(ctx, shndx)});960 try writer.print("name {s}\n", .{getSectionName(ctx, shndx)});
...@@ -824,6 +967,145 @@ const ElfDumper = struct {...@@ -824,6 +967,145 @@ const ElfDumper = struct {
824 }967 }
825 }968 }
826969
970 fn dumpDynamicSection(ctx: Context, writer: anytype) !void {
971 const shndx = getSectionByName(ctx, ".dynamic") orelse return;
972 const shdr = ctx.shdrs[shndx];
973 const strtab = getSectionContents(ctx, shdr.sh_link);
974 const data = getSectionContents(ctx, shndx);
975 const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn));
976 const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries];
977
978 try writer.writeAll(ElfDumper.dynamic_section_label ++ "\n");
979
980 for (entries) |entry| {
981 const key = @as(u64, @bitCast(entry.d_tag));
982 const value = entry.d_val;
983
984 const key_str = switch (key) {
985 elf.DT_NEEDED => "NEEDED",
986 elf.DT_SONAME => "SONAME",
987 elf.DT_INIT_ARRAY => "INIT_ARRAY",
988 elf.DT_INIT_ARRAYSZ => "INIT_ARRAYSZ",
989 elf.DT_FINI_ARRAY => "FINI_ARRAY",
990 elf.DT_FINI_ARRAYSZ => "FINI_ARRAYSZ",
991 elf.DT_HASH => "HASH",
992 elf.DT_GNU_HASH => "GNU_HASH",
993 elf.DT_STRTAB => "STRTAB",
994 elf.DT_SYMTAB => "SYMTAB",
995 elf.DT_STRSZ => "STRSZ",
996 elf.DT_SYMENT => "SYMENT",
997 elf.DT_PLTGOT => "PLTGOT",
998 elf.DT_PLTRELSZ => "PLTRELSZ",
999 elf.DT_PLTREL => "PLTREL",
1000 elf.DT_JMPREL => "JMPREL",
1001 elf.DT_RELA => "RELA",
1002 elf.DT_RELASZ => "RELASZ",
1003 elf.DT_RELAENT => "RELAENT",
1004 elf.DT_VERDEF => "VERDEF",
1005 elf.DT_VERDEFNUM => "VERDEFNUM",
1006 elf.DT_FLAGS => "FLAGS",
1007 elf.DT_FLAGS_1 => "FLAGS_1",
1008 elf.DT_VERNEED => "VERNEED",
1009 elf.DT_VERNEEDNUM => "VERNEEDNUM",
1010 elf.DT_VERSYM => "VERSYM",
1011 elf.DT_RELACOUNT => "RELACOUNT",
1012 elf.DT_RPATH => "RPATH",
1013 elf.DT_RUNPATH => "RUNPATH",
1014 elf.DT_INIT => "INIT",
1015 elf.DT_FINI => "FINI",
1016 elf.DT_NULL => "NULL",
1017 else => "UNKNOWN",
1018 };
1019 try writer.print("{s}", .{key_str});
1020
1021 switch (key) {
1022 elf.DT_NEEDED,
1023 elf.DT_SONAME,
1024 elf.DT_RPATH,
1025 elf.DT_RUNPATH,
1026 => {
1027 const name = getString(strtab, @intCast(value));
1028 try writer.print(" {s}", .{name});
1029 },
1030
1031 elf.DT_INIT_ARRAY,
1032 elf.DT_FINI_ARRAY,
1033 elf.DT_HASH,
1034 elf.DT_GNU_HASH,
1035 elf.DT_STRTAB,
1036 elf.DT_SYMTAB,
1037 elf.DT_PLTGOT,
1038 elf.DT_JMPREL,
1039 elf.DT_RELA,
1040 elf.DT_VERDEF,
1041 elf.DT_VERNEED,
1042 elf.DT_VERSYM,
1043 elf.DT_INIT,
1044 elf.DT_FINI,
1045 elf.DT_NULL,
1046 => try writer.print(" {x}", .{value}),
1047
1048 elf.DT_INIT_ARRAYSZ,
1049 elf.DT_FINI_ARRAYSZ,
1050 elf.DT_STRSZ,
1051 elf.DT_SYMENT,
1052 elf.DT_PLTRELSZ,
1053 elf.DT_RELASZ,
1054 elf.DT_RELAENT,
1055 elf.DT_RELACOUNT,
1056 => try writer.print(" {d}", .{value}),
1057
1058 elf.DT_PLTREL => try writer.writeAll(switch (value) {
1059 elf.DT_REL => " REL",
1060 elf.DT_RELA => " RELA",
1061 else => " UNKNOWN",
1062 }),
1063
1064 elf.DT_FLAGS => if (value > 0) {
1065 if (value & elf.DF_ORIGIN != 0) try writer.writeAll(" ORIGIN");
1066 if (value & elf.DF_SYMBOLIC != 0) try writer.writeAll(" SYMBOLIC");
1067 if (value & elf.DF_TEXTREL != 0) try writer.writeAll(" TEXTREL");
1068 if (value & elf.DF_BIND_NOW != 0) try writer.writeAll(" BIND_NOW");
1069 if (value & elf.DF_STATIC_TLS != 0) try writer.writeAll(" STATIC_TLS");
1070 },
1071
1072 elf.DT_FLAGS_1 => if (value > 0) {
1073 if (value & elf.DF_1_NOW != 0) try writer.writeAll(" NOW");
1074 if (value & elf.DF_1_GLOBAL != 0) try writer.writeAll(" GLOBAL");
1075 if (value & elf.DF_1_GROUP != 0) try writer.writeAll(" GROUP");
1076 if (value & elf.DF_1_NODELETE != 0) try writer.writeAll(" NODELETE");
1077 if (value & elf.DF_1_LOADFLTR != 0) try writer.writeAll(" LOADFLTR");
1078 if (value & elf.DF_1_INITFIRST != 0) try writer.writeAll(" INITFIRST");
1079 if (value & elf.DF_1_NOOPEN != 0) try writer.writeAll(" NOOPEN");
1080 if (value & elf.DF_1_ORIGIN != 0) try writer.writeAll(" ORIGIN");
1081 if (value & elf.DF_1_DIRECT != 0) try writer.writeAll(" DIRECT");
1082 if (value & elf.DF_1_TRANS != 0) try writer.writeAll(" TRANS");
1083 if (value & elf.DF_1_INTERPOSE != 0) try writer.writeAll(" INTERPOSE");
1084 if (value & elf.DF_1_NODEFLIB != 0) try writer.writeAll(" NODEFLIB");
1085 if (value & elf.DF_1_NODUMP != 0) try writer.writeAll(" NODUMP");
1086 if (value & elf.DF_1_CONFALT != 0) try writer.writeAll(" CONFALT");
1087 if (value & elf.DF_1_ENDFILTEE != 0) try writer.writeAll(" ENDFILTEE");
1088 if (value & elf.DF_1_DISPRELDNE != 0) try writer.writeAll(" DISPRELDNE");
1089 if (value & elf.DF_1_DISPRELPND != 0) try writer.writeAll(" DISPRELPND");
1090 if (value & elf.DF_1_NODIRECT != 0) try writer.writeAll(" NODIRECT");
1091 if (value & elf.DF_1_IGNMULDEF != 0) try writer.writeAll(" IGNMULDEF");
1092 if (value & elf.DF_1_NOKSYMS != 0) try writer.writeAll(" NOKSYMS");
1093 if (value & elf.DF_1_NOHDR != 0) try writer.writeAll(" NOHDR");
1094 if (value & elf.DF_1_EDITED != 0) try writer.writeAll(" EDITED");
1095 if (value & elf.DF_1_NORELOC != 0) try writer.writeAll(" NORELOC");
1096 if (value & elf.DF_1_SYMINTPOSE != 0) try writer.writeAll(" SYMINTPOSE");
1097 if (value & elf.DF_1_GLOBAUDIT != 0) try writer.writeAll(" GLOBAUDIT");
1098 if (value & elf.DF_1_SINGLETON != 0) try writer.writeAll(" SINGLETON");
1099 if (value & elf.DF_1_STUB != 0) try writer.writeAll(" STUB");
1100 if (value & elf.DF_1_PIE != 0) try writer.writeAll(" PIE");
1101 },
1102
1103 else => try writer.print(" {x}", .{value}),
1104 }
1105 try writer.writeByte('\n');
1106 }
1107 }
1108
827 fn fmtShType(sh_type: u32) std.fmt.Formatter(formatShType) {1109 fn fmtShType(sh_type: u32) std.fmt.Formatter(formatShType) {
828 return .{ .data = sh_type };1110 return .{ .data = sh_type };
829 }1111 }
...@@ -836,46 +1118,46 @@ const ElfDumper = struct {...@@ -836,46 +1118,46 @@ const ElfDumper = struct {
836 ) !void {1118 ) !void {
837 _ = unused_fmt_string;1119 _ = unused_fmt_string;
838 _ = options;1120 _ = options;
839 if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) {1121 const name = switch (sh_type) {
840 try writer.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS});1122 elf.SHT_NULL => "NULL",
841 } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) {1123 elf.SHT_PROGBITS => "PROGBITS",
842 try writer.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC});1124 elf.SHT_SYMTAB => "SYMTAB",
843 } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) {1125 elf.SHT_STRTAB => "STRTAB",
844 try writer.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER});1126 elf.SHT_RELA => "RELA",
845 } else {1127 elf.SHT_HASH => "HASH",
846 const name = switch (sh_type) {1128 elf.SHT_DYNAMIC => "DYNAMIC",
847 elf.SHT_NULL => "NULL",1129 elf.SHT_NOTE => "NOTE",
848 elf.SHT_PROGBITS => "PROGBITS",1130 elf.SHT_NOBITS => "NOBITS",
849 elf.SHT_SYMTAB => "SYMTAB",1131 elf.SHT_REL => "REL",
850 elf.SHT_STRTAB => "STRTAB",1132 elf.SHT_SHLIB => "SHLIB",
851 elf.SHT_RELA => "RELA",1133 elf.SHT_DYNSYM => "DYNSYM",
852 elf.SHT_HASH => "HASH",1134 elf.SHT_INIT_ARRAY => "INIT_ARRAY",
853 elf.SHT_DYNAMIC => "DYNAMIC",1135 elf.SHT_FINI_ARRAY => "FINI_ARRAY",
854 elf.SHT_NOTE => "NOTE",1136 elf.SHT_PREINIT_ARRAY => "PREINIT_ARRAY",
855 elf.SHT_NOBITS => "NOBITS",1137 elf.SHT_GROUP => "GROUP",
856 elf.SHT_REL => "REL",1138 elf.SHT_SYMTAB_SHNDX => "SYMTAB_SHNDX",
857 elf.SHT_SHLIB => "SHLIB",1139 elf.SHT_X86_64_UNWIND => "X86_64_UNWIND",
858 elf.SHT_DYNSYM => "DYNSYM",1140 elf.SHT_LLVM_ADDRSIG => "LLVM_ADDRSIG",
859 elf.SHT_INIT_ARRAY => "INIT_ARRAY",1141 elf.SHT_GNU_HASH => "GNU_HASH",
860 elf.SHT_FINI_ARRAY => "FINI_ARRAY",1142 elf.SHT_GNU_VERDEF => "VERDEF",
861 elf.SHT_PREINIT_ARRAY => "PREINIT_ARRAY",1143 elf.SHT_GNU_VERNEED => "VERNEED",
862 elf.SHT_GROUP => "GROUP",1144 elf.SHT_GNU_VERSYM => "VERSYM",
863 elf.SHT_SYMTAB_SHNDX => "SYMTAB_SHNDX",1145 else => if (elf.SHT_LOOS <= sh_type and sh_type < elf.SHT_HIOS) {
864 elf.SHT_X86_64_UNWIND => "X86_64_UNWIND",1146 return try writer.print("LOOS+0x{x}", .{sh_type - elf.SHT_LOOS});
865 elf.SHT_LLVM_ADDRSIG => "LLVM_ADDRSIG",1147 } else if (elf.SHT_LOPROC <= sh_type and sh_type < elf.SHT_HIPROC) {
866 elf.SHT_GNU_HASH => "GNU_HASH",1148 return try writer.print("LOPROC+0x{x}", .{sh_type - elf.SHT_LOPROC});
867 elf.SHT_GNU_VERDEF => "VERDEF",1149 } else if (elf.SHT_LOUSER <= sh_type and sh_type < elf.SHT_HIUSER) {
868 elf.SHT_GNU_VERNEED => "VERNEED",1150 return try writer.print("LOUSER+0x{x}", .{sh_type - elf.SHT_LOUSER});
869 elf.SHT_GNU_VERSYM => "VERSYM",1151 } else "UNKNOWN",
870 else => "UNKNOWN",1152 };
871 };1153 try writer.writeAll(name);
872 try writer.writeAll(name);
873 }
874 }1154 }
8751155
876 fn dumpPhdrs(ctx: Context, writer: anytype) !void {1156 fn dumpPhdrs(ctx: Context, writer: anytype) !void {
877 if (ctx.phdrs.len == 0) return;1157 if (ctx.phdrs.len == 0) return;
8781158
1159 try writer.writeAll("program headers\n");
1160
879 for (ctx.phdrs, 0..) |phdr, phndx| {1161 for (ctx.phdrs, 0..) |phdr, phndx| {
880 try writer.print("phdr {d}\n", .{phndx});1162 try writer.print("phdr {d}\n", .{phndx});
881 try writer.print("type {s}\n", .{fmtPhType(phdr.p_type)});1163 try writer.print("type {s}\n", .{fmtPhType(phdr.p_type)});
...@@ -885,7 +1167,28 @@ const ElfDumper = struct {...@@ -885,7 +1167,28 @@ const ElfDumper = struct {
885 try writer.print("memsz {x}\n", .{phdr.p_memsz});1167 try writer.print("memsz {x}\n", .{phdr.p_memsz});
886 try writer.print("filesz {x}\n", .{phdr.p_filesz});1168 try writer.print("filesz {x}\n", .{phdr.p_filesz});
887 try writer.print("align {x}\n", .{phdr.p_align});1169 try writer.print("align {x}\n", .{phdr.p_align});
888 // TODO dump formatted p_flags1170
1171 {
1172 const flags = phdr.p_flags;
1173 try writer.writeAll("flags");
1174 if (flags > 0) try writer.writeByte(' ');
1175 if (flags & elf.PF_R != 0) {
1176 try writer.writeByte('R');
1177 }
1178 if (flags & elf.PF_W != 0) {
1179 try writer.writeByte('W');
1180 }
1181 if (flags & elf.PF_X != 0) {
1182 try writer.writeByte('E');
1183 }
1184 if (flags & elf.PF_MASKOS != 0) {
1185 try writer.writeAll("OS");
1186 }
1187 if (flags & elf.PF_MASKPROC != 0) {
1188 try writer.writeAll("PROC");
1189 }
1190 try writer.writeByte('\n');
1191 }
889 }1192 }
890 }1193 }
8911194
...@@ -901,27 +1204,107 @@ const ElfDumper = struct {...@@ -901,27 +1204,107 @@ const ElfDumper = struct {
901 ) !void {1204 ) !void {
902 _ = unused_fmt_string;1205 _ = unused_fmt_string;
903 _ = options;1206 _ = options;
904 if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) {1207 const p_type = switch (ph_type) {
905 try writer.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS});1208 elf.PT_NULL => "NULL",
906 } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) {1209 elf.PT_LOAD => "LOAD",
907 try writer.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC});1210 elf.PT_DYNAMIC => "DYNAMIC",
908 } else {1211 elf.PT_INTERP => "INTERP",
909 const p_type = switch (ph_type) {1212 elf.PT_NOTE => "NOTE",
910 elf.PT_NULL => "NULL",1213 elf.PT_SHLIB => "SHLIB",
911 elf.PT_LOAD => "LOAD",1214 elf.PT_PHDR => "PHDR",
912 elf.PT_DYNAMIC => "DYNAMIC",1215 elf.PT_TLS => "TLS",
913 elf.PT_INTERP => "INTERP",1216 elf.PT_NUM => "NUM",
914 elf.PT_NOTE => "NOTE",1217 elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME",
915 elf.PT_SHLIB => "SHLIB",1218 elf.PT_GNU_STACK => "GNU_STACK",
916 elf.PT_PHDR => "PHDR",1219 elf.PT_GNU_RELRO => "GNU_RELRO",
917 elf.PT_TLS => "TLS",1220 else => if (elf.PT_LOOS <= ph_type and ph_type < elf.PT_HIOS) {
918 elf.PT_NUM => "NUM",1221 return try writer.print("LOOS+0x{x}", .{ph_type - elf.PT_LOOS});
919 elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME",1222 } else if (elf.PT_LOPROC <= ph_type and ph_type < elf.PT_HIPROC) {
920 elf.PT_GNU_STACK => "GNU_STACK",1223 return try writer.print("LOPROC+0x{x}", .{ph_type - elf.PT_LOPROC});
921 elf.PT_GNU_RELRO => "GNU_RELRO",1224 } else "UNKNOWN",
922 else => "UNKNOWN",1225 };
1226 try writer.writeAll(p_type);
1227 }
1228
1229 fn dumpSymtab(ctx: Context, comptime @"type": enum { symtab, dysymtab }, writer: anytype) !void {
1230 const symtab = switch (@"type") {
1231 .symtab => ctx.symtab,
1232 .dysymtab => ctx.dysymtab,
1233 } orelse return;
1234
1235 try writer.writeAll(switch (@"type") {
1236 .symtab => symtab_label,
1237 .dysymtab => dynamic_symtab_label,
1238 } ++ "\n");
1239
1240 for (symtab.symbols, 0..) |sym, index| {
1241 try writer.print("{x} {x}", .{ sym.st_value, sym.st_size });
1242
1243 {
1244 if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) {
1245 if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) {
1246 try writer.print(" LO+{d}", .{sym.st_shndx - elf.SHN_LOPROC});
1247 } else {
1248 const sym_ndx = &switch (sym.st_shndx) {
1249 elf.SHN_ABS => "ABS",
1250 elf.SHN_COMMON => "COM",
1251 elf.SHN_LIVEPATCH => "LIV",
1252 else => "UNK",
1253 };
1254 try writer.print(" {s}", .{sym_ndx});
1255 }
1256 } else if (sym.st_shndx == elf.SHN_UNDEF) {
1257 try writer.writeAll(" UND");
1258 } else {
1259 try writer.print(" {x}", .{sym.st_shndx});
1260 }
1261 }
1262
1263 blk: {
1264 const tt = sym.st_type();
1265 const sym_type = switch (tt) {
1266 elf.STT_NOTYPE => "NOTYPE",
1267 elf.STT_OBJECT => "OBJECT",
1268 elf.STT_FUNC => "FUNC",
1269 elf.STT_SECTION => "SECTION",
1270 elf.STT_FILE => "FILE",
1271 elf.STT_COMMON => "COMMON",
1272 elf.STT_TLS => "TLS",
1273 elf.STT_NUM => "NUM",
1274 elf.STT_GNU_IFUNC => "IFUNC",
1275 else => if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) {
1276 break :blk try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC});
1277 } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) {
1278 break :blk try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS});
1279 } else "UNK",
1280 };
1281 try writer.print(" {s}", .{sym_type});
1282 }
1283
1284 blk: {
1285 const bind = sym.st_bind();
1286 const sym_bind = switch (bind) {
1287 elf.STB_LOCAL => "LOCAL",
1288 elf.STB_GLOBAL => "GLOBAL",
1289 elf.STB_WEAK => "WEAK",
1290 elf.STB_NUM => "NUM",
1291 else => if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) {
1292 break :blk try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC});
1293 } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) {
1294 break :blk try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS});
1295 } else "UNKNOWN",
1296 };
1297 try writer.print(" {s}", .{sym_bind});
1298 }
1299
1300 const sym_vis = @as(elf.STV, @enumFromInt(sym.st_other));
1301 try writer.print(" {s}", .{@tagName(sym_vis)});
1302
1303 const sym_name = switch (sym.st_type()) {
1304 elf.STT_SECTION => getSectionName(ctx, sym.st_shndx),
1305 else => symtab.getName(index).?,
923 };1306 };
924 try writer.writeAll(p_type);1307 try writer.print(" {s}\n", .{sym_name});
925 }1308 }
926 }1309 }
927};1310};
...@@ -929,12 +1312,8 @@ const ElfDumper = struct {...@@ -929,12 +1312,8 @@ const ElfDumper = struct {
929const WasmDumper = struct {1312const WasmDumper = struct {
930 const symtab_label = "symbols";1313 const symtab_label = "symbols";
9311314
932 fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 {1315 fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 {
933 const gpa = step.owner.allocator;1316 const gpa = step.owner.allocator;
934 if (opts.dump_symtab) {
935 @panic("TODO: Implement symbol table parsing and dumping");
936 }
937
938 var fbs = std.io.fixedBufferStream(bytes);1317 var fbs = std.io.fixedBufferStream(bytes);
939 const reader = fbs.reader();1318 const reader = fbs.reader();
9401319
test/link/macho/dead_strip/build.zig+2-2
...@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {...@@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void {
1515
16 const check = exe.checkObject();16 const check = exe.checkObject();
17 check.checkInSymtab();17 check.checkInSymtab();
18 check.checkNext("{*} (__TEXT,__text) external _iAmUnused");18 check.checkContains("(__TEXT,__text) external _iAmUnused");
19 test_step.dependOn(&check.step);19 test_step.dependOn(&check.step);
2020
21 const run = b.addRunArtifact(exe);21 const run = b.addRunArtifact(exe);
...@@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {...@@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {
3131
32 const check = exe.checkObject();32 const check = exe.checkObject();
33 check.checkInSymtab();33 check.checkInSymtab();
34 check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused");34 check.checkNotPresent("(__TEXT,__text) external _iAmUnused");
35 test_step.dependOn(&check.step);35 test_step.dependOn(&check.step);
3636
37 const run = b.addRunArtifact(exe);37 const run = b.addRunArtifact(exe);
test/link/macho/dead_strip_dylibs/build.zig+6-4
...@@ -19,11 +19,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -19,11 +19,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
19 const exe = createScenario(b, optimize, "no-dead-strip");19 const exe = createScenario(b, optimize, "no-dead-strip");
2020
21 const check = exe.checkObject();21 const check = exe.checkObject();
22 check.checkStart("cmd LOAD_DYLIB");22 check.checkStart();
23 check.checkNext("name {*}Cocoa");23 check.checkExact("cmd LOAD_DYLIB");
24 check.checkContains("Cocoa");
2425
25 check.checkStart("cmd LOAD_DYLIB");26 check.checkStart();
26 check.checkNext("name {*}libobjc{*}.dylib");27 check.checkExact("cmd LOAD_DYLIB");
28 check.checkContains("libobjc");
2729
28 test_step.dependOn(&check.step);30 test_step.dependOn(&check.step);
2931
test/link/macho/dylib/build.zig+16-13
...@@ -25,11 +25,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -25,11 +25,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
25 dylib.linkLibC();25 dylib.linkLibC();
2626
27 const check_dylib = dylib.checkObject();27 const check_dylib = dylib.checkObject();
28 check_dylib.checkStart("cmd ID_DYLIB");28 check_dylib.checkStart();
29 check_dylib.checkNext("name @rpath/liba.dylib");29 check_dylib.checkExact("cmd ID_DYLIB");
30 check_dylib.checkNext("timestamp 2");30 check_dylib.checkExact("name @rpath/liba.dylib");
31 check_dylib.checkNext("current version 10000");31 check_dylib.checkExact("timestamp 2");
32 check_dylib.checkNext("compatibility version 10000");32 check_dylib.checkExact("current version 10000");
33 check_dylib.checkExact("compatibility version 10000");
3334
34 test_step.dependOn(&check_dylib.step);35 test_step.dependOn(&check_dylib.step);
3536
...@@ -45,14 +46,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -45,14 +46,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
45 exe.linkLibC();46 exe.linkLibC();
4647
47 const check_exe = exe.checkObject();48 const check_exe = exe.checkObject();
48 check_exe.checkStart("cmd LOAD_DYLIB");49 check_exe.checkStart();
49 check_exe.checkNext("name @rpath/liba.dylib");50 check_exe.checkExact("cmd LOAD_DYLIB");
50 check_exe.checkNext("timestamp 2");51 check_exe.checkExact("name @rpath/liba.dylib");
51 check_exe.checkNext("current version 10000");52 check_exe.checkExact("timestamp 2");
52 check_exe.checkNext("compatibility version 10000");53 check_exe.checkExact("current version 10000");
5354 check_exe.checkExact("compatibility version 10000");
54 check_exe.checkStart("cmd RPATH");55
55 check_exe.checkNextFileSource("path", dylib.getOutputDirectorySource());56 check_exe.checkStart();
57 check_exe.checkExact("cmd RPATH");
58 check_exe.checkExactFileSource("path", dylib.getOutputDirectorySource());
56 test_step.dependOn(&check_exe.step);59 test_step.dependOn(&check_exe.step);
5760
58 const run = b.addRunArtifact(exe);61 const run = b.addRunArtifact(exe);
test/link/macho/entry/build.zig+7-5
...@@ -24,14 +24,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -24,14 +24,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2424
25 const check_exe = exe.checkObject();25 const check_exe = exe.checkObject();
2626
27 check_exe.checkStart("segname __TEXT");27 check_exe.checkStart();
28 check_exe.checkNext("vmaddr {vmaddr}");28 check_exe.checkExact("segname __TEXT");
29 check_exe.checkExtract("vmaddr {vmaddr}");
2930
30 check_exe.checkStart("cmd MAIN");31 check_exe.checkStart();
31 check_exe.checkNext("entryoff {entryoff}");32 check_exe.checkExact("cmd MAIN");
33 check_exe.checkExtract("entryoff {entryoff}");
3234
33 check_exe.checkInSymtab();35 check_exe.checkInSymtab();
34 check_exe.checkNext("{n_value} (__TEXT,__text) external _non_main");36 check_exe.checkExtract("{n_value} (__TEXT,__text) external _non_main");
3537
36 check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } });38 check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } });
37 test_step.dependOn(&check_exe.step);39 test_step.dependOn(&check_exe.step);
test/link/macho/entry_in_dylib/build.zig+9-6
...@@ -34,14 +34,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -34,14 +34,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
34 exe.forceUndefinedSymbol("_my_main");34 exe.forceUndefinedSymbol("_my_main");
3535
36 const check_exe = exe.checkObject();36 const check_exe = exe.checkObject();
37 check_exe.checkStart("segname __TEXT");37 check_exe.checkStart();
38 check_exe.checkNext("vmaddr {text_vmaddr}");38 check_exe.checkExact("segname __TEXT");
39 check_exe.checkExtract("vmaddr {text_vmaddr}");
3940
40 check_exe.checkStart("sectname __stubs");41 check_exe.checkStart();
41 check_exe.checkNext("addr {stubs_vmaddr}");42 check_exe.checkExact("sectname __stubs");
43 check_exe.checkExtract("addr {stubs_vmaddr}");
4244
43 check_exe.checkStart("cmd MAIN");45 check_exe.checkStart();
44 check_exe.checkNext("entryoff {entryoff}");46 check_exe.checkExact("cmd MAIN");
47 check_exe.checkExtract("entryoff {entryoff}");
4548
46 check_exe.checkComputeCompare("text_vmaddr entryoff +", .{49 check_exe.checkComputeCompare("text_vmaddr entryoff +", .{
47 .op = .eq,50 .op = .eq,
test/link/macho/headerpad/build.zig+12-8
...@@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
21 exe.headerpad_max_install_names = true;21 exe.headerpad_max_install_names = true;
2222
23 const check = exe.checkObject();23 const check = exe.checkObject();
24 check.checkStart("sectname __text");24 check.checkStart();
25 check.checkNext("offset {offset}");25 check.checkExact("sectname __text");
26 check.checkExtract("offset {offset}");
2627
27 switch (builtin.cpu.arch) {28 switch (builtin.cpu.arch) {
28 .aarch64 => {29 .aarch64 => {
...@@ -46,8 +47,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -46,8 +47,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
46 exe.headerpad_size = 0x10000;47 exe.headerpad_size = 0x10000;
4748
48 const check = exe.checkObject();49 const check = exe.checkObject();
49 check.checkStart("sectname __text");50 check.checkStart();
50 check.checkNext("offset {offset}");51 check.checkExact("sectname __text");
52 check.checkExtract("offset {offset}");
51 check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });53 check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });
5254
53 test_step.dependOn(&check.step);55 test_step.dependOn(&check.step);
...@@ -63,8 +65,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -63,8 +65,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
63 exe.headerpad_size = 0x10000;65 exe.headerpad_size = 0x10000;
6466
65 const check = exe.checkObject();67 const check = exe.checkObject();
66 check.checkStart("sectname __text");68 check.checkStart();
67 check.checkNext("offset {offset}");69 check.checkExact("sectname __text");
70 check.checkExtract("offset {offset}");
68 check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });71 check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });
6972
70 test_step.dependOn(&check.step);73 test_step.dependOn(&check.step);
...@@ -80,8 +83,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -80,8 +83,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
80 exe.headerpad_max_install_names = true;83 exe.headerpad_max_install_names = true;
8184
82 const check = exe.checkObject();85 const check = exe.checkObject();
83 check.checkStart("sectname __text");86 check.checkStart();
84 check.checkNext("offset {offset}");87 check.checkExact("sectname __text");
88 check.checkExtract("offset {offset}");
8589
86 switch (builtin.cpu.arch) {90 switch (builtin.cpu.arch) {
87 .aarch64 => {91 .aarch64 => {
test/link/macho/linksection/build.zig+3-3
...@@ -25,14 +25,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -25,14 +25,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
25 const check = obj.checkObject();25 const check = obj.checkObject();
2626
27 check.checkInSymtab();27 check.checkInSymtab();
28 check.checkNext("{*} (__DATA,__TestGlobal) external _test_global");28 check.checkContains("(__DATA,__TestGlobal) external _test_global");
2929
30 check.checkInSymtab();30 check.checkInSymtab();
31 check.checkNext("{*} (__TEXT,__TestFn) external _testFn");31 check.checkContains("(__TEXT,__TestFn) external _testFn");
3232
33 if (optimize == .Debug) {33 if (optimize == .Debug) {
34 check.checkInSymtab();34 check.checkInSymtab();
35 check.checkNext("{*} (__TEXT,__TestGenFnA) _main.testGenericFn__anon_{*}");35 check.checkContains("(__TEXT,__TestGenFnA) _main.testGenericFn__anon_");
36 }36 }
3737
38 test_step.dependOn(&check.step);38 test_step.dependOn(&check.step);
test/link/macho/needed_framework/build.zig+3-2
...@@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
26 exe.dead_strip_dylibs = true;26 exe.dead_strip_dylibs = true;
2727
28 const check = exe.checkObject();28 const check = exe.checkObject();
29 check.checkStart("cmd LOAD_DYLIB");29 check.checkStart();
30 check.checkNext("name {*}Cocoa");30 check.checkExact("cmd LOAD_DYLIB");
31 check.checkContains("Cocoa");
31 test_step.dependOn(&check.step);32 test_step.dependOn(&check.step);
3233
33 const run_cmd = b.addRunArtifact(exe);34 const run_cmd = b.addRunArtifact(exe);
test/link/macho/needed_library/build.zig+3-2
...@@ -39,8 +39,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -39,8 +39,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
39 exe.dead_strip_dylibs = true;39 exe.dead_strip_dylibs = true;
4040
41 const check = exe.checkObject();41 const check = exe.checkObject();
42 check.checkStart("cmd LOAD_DYLIB");42 check.checkStart();
43 check.checkNext("name @rpath/liba.dylib");43 check.checkExact("cmd LOAD_DYLIB");
44 check.checkExact("name @rpath/liba.dylib");
44 test_step.dependOn(&check.step);45 test_step.dependOn(&check.step);
4546
46 const run = b.addRunArtifact(exe);47 const run = b.addRunArtifact(exe);
test/link/macho/pagezero/build.zig+12-9
...@@ -20,13 +20,15 @@ pub fn build(b: *std.Build) void {...@@ -20,13 +20,15 @@ pub fn build(b: *std.Build) void {
20 exe.pagezero_size = 0x4000;20 exe.pagezero_size = 0x4000;
2121
22 const check = exe.checkObject();22 const check = exe.checkObject();
23 check.checkStart("LC 0");23 check.checkStart();
24 check.checkNext("segname __PAGEZERO");24 check.checkExact("LC 0");
25 check.checkNext("vmaddr 0");25 check.checkExact("segname __PAGEZERO");
26 check.checkNext("vmsize 4000");26 check.checkExact("vmaddr 0");
27 check.checkExact("vmsize 4000");
2728
28 check.checkStart("segname __TEXT");29 check.checkStart();
29 check.checkNext("vmaddr 4000");30 check.checkExact("segname __TEXT");
31 check.checkExact("vmaddr 4000");
3032
31 test_step.dependOn(&check.step);33 test_step.dependOn(&check.step);
32 }34 }
...@@ -42,9 +44,10 @@ pub fn build(b: *std.Build) void {...@@ -42,9 +44,10 @@ pub fn build(b: *std.Build) void {
42 exe.pagezero_size = 0;44 exe.pagezero_size = 0;
4345
44 const check = exe.checkObject();46 const check = exe.checkObject();
45 check.checkStart("LC 0");47 check.checkStart();
46 check.checkNext("segname __TEXT");48 check.checkExact("LC 0");
47 check.checkNext("vmaddr 0");49 check.checkExact("segname __TEXT");
50 check.checkExact("vmaddr 0");
4851
49 test_step.dependOn(&check.step);52 test_step.dependOn(&check.step);
50 }53 }
test/link/macho/search_strategy/build.zig+3-2
...@@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -21,8 +21,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
21 exe.search_strategy = .dylibs_first;21 exe.search_strategy = .dylibs_first;
2222
23 const check = exe.checkObject();23 const check = exe.checkObject();
24 check.checkStart("cmd LOAD_DYLIB");24 check.checkStart();
25 check.checkNext("name @rpath/libsearch_dylibs_first.dylib");25 check.checkExact("cmd LOAD_DYLIB");
26 check.checkExact("name @rpath/libsearch_dylibs_first.dylib");
26 test_step.dependOn(&check.step);27 test_step.dependOn(&check.step);
2728
28 const run = b.addRunArtifact(exe);29 const run = b.addRunArtifact(exe);
test/link/macho/stack_size/build.zig+3-2
...@@ -25,8 +25,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -25,8 +25,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
25 exe.stack_size = 0x100000000;25 exe.stack_size = 0x100000000;
2626
27 const check_exe = exe.checkObject();27 const check_exe = exe.checkObject();
28 check_exe.checkStart("cmd MAIN");28 check_exe.checkStart();
29 check_exe.checkNext("stacksize 100000000");29 check_exe.checkExact("cmd MAIN");
30 check_exe.checkExact("stacksize 100000000");
30 test_step.dependOn(&check_exe.step);31 test_step.dependOn(&check_exe.step);
3132
32 const run = b.addRunArtifact(exe);33 const run = b.addRunArtifact(exe);
test/link/macho/strict_validation/build.zig+42-35
...@@ -26,44 +26,51 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -26,44 +26,51 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2626
27 const check_exe = exe.checkObject();27 const check_exe = exe.checkObject();
2828
29 check_exe.checkStart("cmd SEGMENT_64");29 check_exe.checkStart();
30 check_exe.checkNext("segname __LINKEDIT");30 check_exe.checkExact("cmd SEGMENT_64");
31 check_exe.checkNext("fileoff {fileoff}");31 check_exe.checkExact("segname __LINKEDIT");
32 check_exe.checkNext("filesz {filesz}");32 check_exe.checkExtract("fileoff {fileoff}");
3333 check_exe.checkExtract("filesz {filesz}");
34 check_exe.checkStart("cmd DYLD_INFO_ONLY");34
35 check_exe.checkNext("rebaseoff {rebaseoff}");35 check_exe.checkStart();
36 check_exe.checkNext("rebasesize {rebasesize}");36 check_exe.checkExact("cmd DYLD_INFO_ONLY");
37 check_exe.checkNext("bindoff {bindoff}");37 check_exe.checkExtract("rebaseoff {rebaseoff}");
38 check_exe.checkNext("bindsize {bindsize}");38 check_exe.checkExtract("rebasesize {rebasesize}");
39 check_exe.checkNext("lazybindoff {lazybindoff}");39 check_exe.checkExtract("bindoff {bindoff}");
40 check_exe.checkNext("lazybindsize {lazybindsize}");40 check_exe.checkExtract("bindsize {bindsize}");
41 check_exe.checkNext("exportoff {exportoff}");41 check_exe.checkExtract("lazybindoff {lazybindoff}");
42 check_exe.checkNext("exportsize {exportsize}");42 check_exe.checkExtract("lazybindsize {lazybindsize}");
4343 check_exe.checkExtract("exportoff {exportoff}");
44 check_exe.checkStart("cmd FUNCTION_STARTS");44 check_exe.checkExtract("exportsize {exportsize}");
45 check_exe.checkNext("dataoff {fstartoff}");45
46 check_exe.checkNext("datasize {fstartsize}");46 check_exe.checkStart();
4747 check_exe.checkExact("cmd FUNCTION_STARTS");
48 check_exe.checkStart("cmd DATA_IN_CODE");48 check_exe.checkExtract("dataoff {fstartoff}");
49 check_exe.checkNext("dataoff {diceoff}");49 check_exe.checkExtract("datasize {fstartsize}");
50 check_exe.checkNext("datasize {dicesize}");50
5151 check_exe.checkStart();
52 check_exe.checkStart("cmd SYMTAB");52 check_exe.checkExact("cmd DATA_IN_CODE");
53 check_exe.checkNext("symoff {symoff}");53 check_exe.checkExtract("dataoff {diceoff}");
54 check_exe.checkNext("nsyms {symnsyms}");54 check_exe.checkExtract("datasize {dicesize}");
55 check_exe.checkNext("stroff {stroff}");55
56 check_exe.checkNext("strsize {strsize}");56 check_exe.checkStart();
5757 check_exe.checkExact("cmd SYMTAB");
58 check_exe.checkStart("cmd DYSYMTAB");58 check_exe.checkExtract("symoff {symoff}");
59 check_exe.checkNext("indirectsymoff {dysymoff}");59 check_exe.checkExtract("nsyms {symnsyms}");
60 check_exe.checkNext("nindirectsyms {dysymnsyms}");60 check_exe.checkExtract("stroff {stroff}");
61 check_exe.checkExtract("strsize {strsize}");
62
63 check_exe.checkStart();
64 check_exe.checkExact("cmd DYSYMTAB");
65 check_exe.checkExtract("indirectsymoff {dysymoff}");
66 check_exe.checkExtract("nindirectsyms {dysymnsyms}");
6167
62 switch (builtin.cpu.arch) {68 switch (builtin.cpu.arch) {
63 .aarch64 => {69 .aarch64 => {
64 check_exe.checkStart("cmd CODE_SIGNATURE");70 check_exe.checkStart();
65 check_exe.checkNext("dataoff {codesigoff}");71 check_exe.checkExact("cmd CODE_SIGNATURE");
66 check_exe.checkNext("datasize {codesigsize}");72 check_exe.checkExtract("dataoff {codesigoff}");
73 check_exe.checkExtract("datasize {codesigsize}");
67 },74 },
68 .x86_64 => {},75 .x86_64 => {},
69 else => unreachable,76 else => unreachable,
test/link/macho/unwind_info/build.zig+6-5
...@@ -32,20 +32,21 @@ fn testUnwindInfo(...@@ -32,20 +32,21 @@ fn testUnwindInfo(
32 exe.link_gc_sections = dead_strip;32 exe.link_gc_sections = dead_strip;
3333
34 const check = exe.checkObject();34 const check = exe.checkObject();
35 check.checkStart("segname __TEXT");35 check.checkStart();
36 check.checkNext("sectname __gcc_except_tab");36 check.checkExact("segname __TEXT");
37 check.checkNext("sectname __unwind_info");37 check.checkExact("sectname __gcc_except_tab");
38 check.checkExact("sectname __unwind_info");
3839
39 switch (builtin.cpu.arch) {40 switch (builtin.cpu.arch) {
40 .aarch64 => {41 .aarch64 => {
41 check.checkNext("sectname __eh_frame");42 check.checkExact("sectname __eh_frame");
42 },43 },
43 .x86_64 => {}, // We do not expect `__eh_frame` section on x86_64 in this case44 .x86_64 => {}, // We do not expect `__eh_frame` section on x86_64 in this case
44 else => unreachable,45 else => unreachable,
45 }46 }
4647
47 check.checkInSymtab();48 check.checkInSymtab();
48 check.checkNext("{*} (__TEXT,__text) external ___gxx_personality_v0");49 check.checkContains("(__TEXT,__text) external ___gxx_personality_v0");
49 test_step.dependOn(&check.step);50 test_step.dependOn(&check.step);
5051
51 const run = b.addRunArtifact(exe);52 const run = b.addRunArtifact(exe);
test/link/macho/weak_framework/build.zig+3-2
...@@ -23,8 +23,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -23,8 +23,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
23 exe.linkFrameworkWeak("Cocoa");23 exe.linkFrameworkWeak("Cocoa");
2424
25 const check = exe.checkObject();25 const check = exe.checkObject();
26 check.checkStart("cmd LOAD_WEAK_DYLIB");26 check.checkStart();
27 check.checkNext("name {*}Cocoa");27 check.checkExact("cmd LOAD_WEAK_DYLIB");
28 check.checkContains("Cocoa");
28 test_step.dependOn(&check.step);29 test_step.dependOn(&check.step);
2930
30 const run_cmd = b.addRunArtifact(exe);31 const run_cmd = b.addRunArtifact(exe);
test/link/macho/weak_library/build.zig+5-4
...@@ -37,14 +37,15 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -37,14 +37,15 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
37 exe.addRPathDirectorySource(dylib.getOutputDirectorySource());37 exe.addRPathDirectorySource(dylib.getOutputDirectorySource());
3838
39 const check = exe.checkObject();39 const check = exe.checkObject();
40 check.checkStart("cmd LOAD_WEAK_DYLIB");40 check.checkStart();
41 check.checkNext("name @rpath/liba.dylib");41 check.checkExact("cmd LOAD_WEAK_DYLIB");
42 check.checkExact("name @rpath/liba.dylib");
4243
43 check.checkInSymtab();44 check.checkInSymtab();
44 check.checkNext("(undefined) weak external _a (from liba)");45 check.checkExact("(undefined) weak external _a (from liba)");
4546
46 check.checkInSymtab();47 check.checkInSymtab();
47 check.checkNext("(undefined) weak external _asStr (from liba)");48 check.checkExact("(undefined) weak external _asStr (from liba)");
48 test_step.dependOn(&check.step);49 test_step.dependOn(&check.step);
4950
50 const run = b.addRunArtifact(exe);51 const run = b.addRunArtifact(exe);
test/link/wasm/archive/build.zig+3-2
...@@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -26,8 +26,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
26 lib.strip = false;26 lib.strip = false;
2727
28 const check = lib.checkObject();28 const check = lib.checkObject();
29 check.checkStart("Section custom");29 check.checkStart();
30 check.checkNext("name __trunch"); // Ensure it was imported and resolved30 check.checkExact("Section custom");
31 check.checkExact("name __trunch"); // Ensure it was imported and resolved
3132
32 test_step.dependOn(&check.step);33 test_step.dependOn(&check.step);
33}34}
test/link/wasm/basic-features/build.zig+4-3
...@@ -20,9 +20,10 @@ pub fn build(b: *std.Build) void {...@@ -20,9 +20,10 @@ pub fn build(b: *std.Build) void {
2020
21 // Verify the result contains the features explicitly set on the target for the library.21 // Verify the result contains the features explicitly set on the target for the library.
22 const check = lib.checkObject();22 const check = lib.checkObject();
23 check.checkStart("name target_features");23 check.checkStart();
24 check.checkNext("features 1");24 check.checkExact("name target_features");
25 check.checkNext("+ atomics");25 check.checkExact("features 1");
26 check.checkExact("+ atomics");
2627
27 const test_step = b.step("test", "Run linker test");28 const test_step = b.step("test", "Run linker test");
28 test_step.dependOn(&check.step);29 test_step.dependOn(&check.step);
test/link/wasm/bss/build.zig+24-20
...@@ -29,28 +29,31 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt...@@ -29,28 +29,31 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
29 const check_lib = lib.checkObject();29 const check_lib = lib.checkObject();
3030
31 // since we import memory, make sure it exists with the correct naming31 // since we import memory, make sure it exists with the correct naming
32 check_lib.checkStart("Section import");32 check_lib.checkStart();
33 check_lib.checkNext("entries 1");33 check_lib.checkExact("Section import");
34 check_lib.checkNext("module env"); // default module name is "env"34 check_lib.checkExact("entries 1");
35 check_lib.checkNext("name memory"); // as per linker specification35 check_lib.checkExact("module env"); // default module name is "env"
36 check_lib.checkExact("name memory"); // as per linker specification
3637
37 // since we are importing memory, ensure it's not exported38 // since we are importing memory, ensure it's not exported
39 check_lib.checkStart();
38 check_lib.checkNotPresent("Section export");40 check_lib.checkNotPresent("Section export");
3941
40 // validate the name of the stack pointer42 // validate the name of the stack pointer
41 check_lib.checkStart("Section custom");43 check_lib.checkStart();
42 check_lib.checkNext("type data_segment");44 check_lib.checkExact("Section custom");
43 check_lib.checkNext("names 2");45 check_lib.checkExact("type data_segment");
44 check_lib.checkNext("index 0");46 check_lib.checkExact("names 2");
45 check_lib.checkNext("name .rodata");47 check_lib.checkExact("index 0");
48 check_lib.checkExact("name .rodata");
46 // for safe optimization modes `undefined` is stored in data instead of bss.49 // for safe optimization modes `undefined` is stored in data instead of bss.
47 if (is_safe) {50 if (is_safe) {
48 check_lib.checkNext("index 1");51 check_lib.checkExact("index 1");
49 check_lib.checkNext("name .data");52 check_lib.checkExact("name .data");
50 check_lib.checkNotPresent("name .bss");53 check_lib.checkNotPresent("name .bss");
51 } else {54 } else {
52 check_lib.checkNext("index 1"); // bss section always last55 check_lib.checkExact("index 1"); // bss section always last
53 check_lib.checkNext("name .bss");56 check_lib.checkExact("name .bss");
54 }57 }
55 test_step.dependOn(&check_lib.step);58 test_step.dependOn(&check_lib.step);
56 }59 }
...@@ -70,13 +73,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt...@@ -70,13 +73,14 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt
70 lib.import_memory = true;73 lib.import_memory = true;
7174
72 const check_lib = lib.checkObject();75 const check_lib = lib.checkObject();
73 check_lib.checkStart("Section custom");76 check_lib.checkStart();
74 check_lib.checkNext("type data_segment");77 check_lib.checkExact("Section custom");
75 check_lib.checkNext("names 2");78 check_lib.checkExact("type data_segment");
76 check_lib.checkNext("index 0");79 check_lib.checkExact("names 2");
77 check_lib.checkNext("name .rodata");80 check_lib.checkExact("index 0");
78 check_lib.checkNext("index 1");81 check_lib.checkExact("name .rodata");
79 check_lib.checkNext("name .bss");82 check_lib.checkExact("index 1");
83 check_lib.checkExact("name .bss");
8084
81 test_step.dependOn(&check_lib.step);85 test_step.dependOn(&check_lib.step);
82 }86 }
test/link/wasm/export-data/build.zig+19-17
...@@ -21,26 +21,28 @@ pub fn build(b: *std.Build) void {...@@ -21,26 +21,28 @@ pub fn build(b: *std.Build) void {
2121
22 const check_lib = lib.checkObject();22 const check_lib = lib.checkObject();
2323
24 check_lib.checkStart("Section global");24 check_lib.checkStart();
25 check_lib.checkNext("entries 3");25 check_lib.checkExact("Section global");
26 check_lib.checkNext("type i32"); // stack pointer so skip other fields26 check_lib.checkExact("entries 3");
27 check_lib.checkNext("type i32");27 check_lib.checkExact("type i32"); // stack pointer so skip other fields
28 check_lib.checkNext("mutable false");28 check_lib.checkExact("type i32");
29 check_lib.checkNext("i32.const {foo_address}");29 check_lib.checkExact("mutable false");
30 check_lib.checkNext("type i32");30 check_lib.checkExtract("i32.const {foo_address}");
31 check_lib.checkNext("mutable false");31 check_lib.checkExact("type i32");
32 check_lib.checkNext("i32.const {bar_address}");32 check_lib.checkExact("mutable false");
33 check_lib.checkExtract("i32.const {bar_address}");
33 check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 4 } });34 check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 4 } });
34 check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 0 } });35 check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 0 } });
3536
36 check_lib.checkStart("Section export");37 check_lib.checkStart();
37 check_lib.checkNext("entries 3");38 check_lib.checkExact("Section export");
38 check_lib.checkNext("name foo");39 check_lib.checkExact("entries 3");
39 check_lib.checkNext("kind global");40 check_lib.checkExact("name foo");
40 check_lib.checkNext("index 1");41 check_lib.checkExact("kind global");
41 check_lib.checkNext("name bar");42 check_lib.checkExact("index 1");
42 check_lib.checkNext("kind global");43 check_lib.checkExact("name bar");
43 check_lib.checkNext("index 2");44 check_lib.checkExact("kind global");
45 check_lib.checkExact("index 2");
4446
45 test_step.dependOn(&check_lib.step);47 test_step.dependOn(&check_lib.step);
46}48}
test/link/wasm/export/build.zig+15-12
...@@ -43,22 +43,25 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -43,22 +43,25 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
43 force_export.use_lld = false;43 force_export.use_lld = false;
4444
45 const check_no_export = no_export.checkObject();45 const check_no_export = no_export.checkObject();
46 check_no_export.checkStart("Section export");46 check_no_export.checkStart();
47 check_no_export.checkNext("entries 1");47 check_no_export.checkExact("Section export");
48 check_no_export.checkNext("name memory");48 check_no_export.checkExact("entries 1");
49 check_no_export.checkNext("kind memory");49 check_no_export.checkExact("name memory");
50 check_no_export.checkExact("kind memory");
5051
51 const check_dynamic_export = dynamic_export.checkObject();52 const check_dynamic_export = dynamic_export.checkObject();
52 check_dynamic_export.checkStart("Section export");53 check_dynamic_export.checkStart();
53 check_dynamic_export.checkNext("entries 2");54 check_dynamic_export.checkExact("Section export");
54 check_dynamic_export.checkNext("name foo");55 check_dynamic_export.checkExact("entries 2");
55 check_dynamic_export.checkNext("kind function");56 check_dynamic_export.checkExact("name foo");
57 check_dynamic_export.checkExact("kind function");
5658
57 const check_force_export = force_export.checkObject();59 const check_force_export = force_export.checkObject();
58 check_force_export.checkStart("Section export");60 check_force_export.checkStart();
59 check_force_export.checkNext("entries 2");61 check_force_export.checkExact("Section export");
60 check_force_export.checkNext("name foo");62 check_force_export.checkExact("entries 2");
61 check_force_export.checkNext("kind function");63 check_force_export.checkExact("name foo");
64 check_force_export.checkExact("kind function");
6265
63 test_step.dependOn(&check_no_export.step);66 test_step.dependOn(&check_no_export.step);
64 test_step.dependOn(&check_dynamic_export.step);67 test_step.dependOn(&check_dynamic_export.step);
test/link/wasm/extern-mangle/build.zig+7-6
...@@ -21,12 +21,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -21,12 +21,13 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
21 lib.rdynamic = true; // export `foo`21 lib.rdynamic = true; // export `foo`
2222
23 const check_lib = lib.checkObject();23 const check_lib = lib.checkObject();
24 check_lib.checkStart("Section import");24 check_lib.checkStart();
25 check_lib.checkNext("entries 2"); // a.hello & b.hello25 check_lib.checkExact("Section import");
26 check_lib.checkNext("module a");26 check_lib.checkExact("entries 2"); // a.hello & b.hello
27 check_lib.checkNext("name hello");27 check_lib.checkExact("module a");
28 check_lib.checkNext("module b");28 check_lib.checkExact("name hello");
29 check_lib.checkNext("name hello");29 check_lib.checkExact("module b");
30 check_lib.checkExact("name hello");
3031
31 test_step.dependOn(&check_lib.step);32 test_step.dependOn(&check_lib.step);
32}33}
test/link/wasm/function-table/build.zig+26-21
...@@ -46,31 +46,36 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -46,31 +46,36 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
46 const check_export = export_table.checkObject();46 const check_export = export_table.checkObject();
47 const check_regular = regular_table.checkObject();47 const check_regular = regular_table.checkObject();
4848
49 check_import.checkStart("Section import");49 check_import.checkStart();
50 check_import.checkNext("entries 1");50 check_import.checkExact("Section import");
51 check_import.checkNext("module env");51 check_import.checkExact("entries 1");
52 check_import.checkNext("name __indirect_function_table");52 check_import.checkExact("module env");
53 check_import.checkNext("kind table");53 check_import.checkExact("name __indirect_function_table");
54 check_import.checkNext("type funcref");54 check_import.checkExact("kind table");
55 check_import.checkNext("min 1"); // 1 function pointer55 check_import.checkExact("type funcref");
56 check_import.checkExact("min 1"); // 1 function pointer
56 check_import.checkNotPresent("max"); // when importing, we do not provide a max57 check_import.checkNotPresent("max"); // when importing, we do not provide a max
57 check_import.checkNotPresent("Section table"); // we're importing it58 check_import.checkNotPresent("Section table"); // we're importing it
5859
59 check_export.checkStart("Section export");60 check_export.checkStart();
60 check_export.checkNext("entries 2");61 check_export.checkExact("Section export");
61 check_export.checkNext("name __indirect_function_table"); // as per linker specification62 check_export.checkExact("entries 2");
62 check_export.checkNext("kind table");63 check_export.checkExact("name __indirect_function_table"); // as per linker specification
64 check_export.checkExact("kind table");
6365
64 check_regular.checkStart("Section table");66 check_regular.checkStart();
65 check_regular.checkNext("entries 1");67 check_regular.checkExact("Section table");
66 check_regular.checkNext("type funcref");68 check_regular.checkExact("entries 1");
67 check_regular.checkNext("min 2"); // index starts at 1 & 1 function pointer = 2.69 check_regular.checkExact("type funcref");
68 check_regular.checkNext("max 2");70 check_regular.checkExact("min 2"); // index starts at 1 & 1 function pointer = 2.
69 check_regular.checkStart("Section element");71 check_regular.checkExact("max 2");
70 check_regular.checkNext("entries 1");72
71 check_regular.checkNext("table index 0");73 check_regular.checkStart();
72 check_regular.checkNext("i32.const 1"); // we want to start function indexes at 174 check_regular.checkExact("Section element");
73 check_regular.checkNext("indexes 1"); // 1 function pointer75 check_regular.checkExact("entries 1");
76 check_regular.checkExact("table index 0");
77 check_regular.checkExact("i32.const 1"); // we want to start function indexes at 1
78 check_regular.checkExact("indexes 1"); // 1 function pointer
7479
75 test_step.dependOn(&check_import.step);80 test_step.dependOn(&check_import.step);
76 test_step.dependOn(&check_export.step);81 test_step.dependOn(&check_export.step);
test/link/wasm/infer-features/build.zig+10-9
...@@ -33,15 +33,16 @@ pub fn build(b: *std.Build) void {...@@ -33,15 +33,16 @@ pub fn build(b: *std.Build) void {
3333
34 // Verify the result contains the features from the C Object file.34 // Verify the result contains the features from the C Object file.
35 const check = lib.checkObject();35 const check = lib.checkObject();
36 check.checkStart("name target_features");36 check.checkStart();
37 check.checkNext("features 7");37 check.checkExact("name target_features");
38 check.checkNext("+ atomics");38 check.checkExact("features 7");
39 check.checkNext("+ bulk-memory");39 check.checkExact("+ atomics");
40 check.checkNext("+ mutable-globals");40 check.checkExact("+ bulk-memory");
41 check.checkNext("+ nontrapping-fptoint");41 check.checkExact("+ mutable-globals");
42 check.checkNext("+ sign-ext");42 check.checkExact("+ nontrapping-fptoint");
43 check.checkNext("+ simd128");43 check.checkExact("+ sign-ext");
44 check.checkNext("+ tail-call");44 check.checkExact("+ simd128");
45 check.checkExact("+ tail-call");
4546
46 const test_step = b.step("test", "Run linker test");47 const test_step = b.step("test", "Run linker test");
47 test_step.dependOn(&check.step);48 test_step.dependOn(&check.step);
test/link/wasm/producers/build.zig+11-10
...@@ -28,16 +28,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -28,16 +28,17 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
28 const version_fmt = "version " ++ builtin.zig_version_string;28 const version_fmt = "version " ++ builtin.zig_version_string;
2929
30 const check_lib = lib.checkObject();30 const check_lib = lib.checkObject();
31 check_lib.checkStart("name producers");31 check_lib.checkStart();
32 check_lib.checkNext("fields 2");32 check_lib.checkExact("name producers");
33 check_lib.checkNext("field_name language");33 check_lib.checkExact("fields 2");
34 check_lib.checkNext("values 1");34 check_lib.checkExact("field_name language");
35 check_lib.checkNext("value_name Zig");35 check_lib.checkExact("values 1");
36 check_lib.checkNext(version_fmt);36 check_lib.checkExact("value_name Zig");
37 check_lib.checkNext("field_name processed-by");37 check_lib.checkExact(version_fmt);
38 check_lib.checkNext("values 1");38 check_lib.checkExact("field_name processed-by");
39 check_lib.checkNext("value_name Zig");39 check_lib.checkExact("values 1");
40 check_lib.checkNext(version_fmt);40 check_lib.checkExact("value_name Zig");
41 check_lib.checkExact(version_fmt);
4142
42 test_step.dependOn(&check_lib.step);43 test_step.dependOn(&check_lib.step);
43}44}
test/link/wasm/segments/build.zig+14-10
...@@ -25,16 +25,20 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -25,16 +25,20 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
25 b.installArtifact(lib);25 b.installArtifact(lib);
2626
27 const check_lib = lib.checkObject();27 const check_lib = lib.checkObject();
28 check_lib.checkStart("Section data");28 check_lib.checkStart();
29 check_lib.checkNext("entries 2"); // rodata & data, no bss because we're exporting memory29 check_lib.checkExact("Section data");
30 check_lib.checkExact("entries 2"); // rodata & data, no bss because we're exporting memory
3031
31 check_lib.checkStart("Section custom");32 check_lib.checkStart();
32 check_lib.checkStart("name name"); // names custom section33 check_lib.checkExact("Section custom");
33 check_lib.checkStart("type data_segment");34 check_lib.checkStart();
34 check_lib.checkNext("names 2");35 check_lib.checkExact("name name"); // names custom section
35 check_lib.checkNext("index 0");36 check_lib.checkStart();
36 check_lib.checkNext("name .rodata");37 check_lib.checkExact("type data_segment");
37 check_lib.checkNext("index 1");38 check_lib.checkExact("names 2");
38 check_lib.checkNext("name .data");39 check_lib.checkExact("index 0");
40 check_lib.checkExact("name .rodata");
41 check_lib.checkExact("index 1");
42 check_lib.checkExact("name .data");
39 test_step.dependOn(&check_lib.step);43 test_step.dependOn(&check_lib.step);
40}44}
test/link/wasm/stack_pointer/build.zig+15-12
...@@ -28,23 +28,26 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -28,23 +28,26 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
28 const check_lib = lib.checkObject();28 const check_lib = lib.checkObject();
2929
30 // ensure global exists and its initial value is equal to explitic stack size30 // ensure global exists and its initial value is equal to explitic stack size
31 check_lib.checkStart("Section global");31 check_lib.checkStart();
32 check_lib.checkNext("entries 1");32 check_lib.checkExact("Section global");
33 check_lib.checkNext("type i32"); // on wasm32 the stack pointer must be i3233 check_lib.checkExact("entries 1");
34 check_lib.checkNext("mutable true"); // must be able to mutate the stack pointer34 check_lib.checkExact("type i32"); // on wasm32 the stack pointer must be i32
35 check_lib.checkNext("i32.const {stack_pointer}");35 check_lib.checkExact("mutable true"); // must be able to mutate the stack pointer
36 check_lib.checkExtract("i32.const {stack_pointer}");
36 check_lib.checkComputeCompare("stack_pointer", .{ .op = .eq, .value = .{ .literal = lib.stack_size.? } });37 check_lib.checkComputeCompare("stack_pointer", .{ .op = .eq, .value = .{ .literal = lib.stack_size.? } });
3738
38 // validate memory section starts after virtual stack39 // validate memory section starts after virtual stack
39 check_lib.checkNext("Section data");40 check_lib.checkStart();
40 check_lib.checkNext("i32.const {data_start}");41 check_lib.checkExact("Section data");
42 check_lib.checkExtract("i32.const {data_start}");
41 check_lib.checkComputeCompare("data_start", .{ .op = .eq, .value = .{ .variable = "stack_pointer" } });43 check_lib.checkComputeCompare("data_start", .{ .op = .eq, .value = .{ .variable = "stack_pointer" } });
4244
43 // validate the name of the stack pointer45 // validate the name of the stack pointer
44 check_lib.checkStart("Section custom");46 check_lib.checkStart();
45 check_lib.checkNext("type global");47 check_lib.checkExact("Section custom");
46 check_lib.checkNext("names 1");48 check_lib.checkExact("type global");
47 check_lib.checkNext("index 0");49 check_lib.checkExact("names 1");
48 check_lib.checkNext("name __stack_pointer");50 check_lib.checkExact("index 0");
51 check_lib.checkExact("name __stack_pointer");
49 test_step.dependOn(&check_lib.step);52 test_step.dependOn(&check_lib.step);
50}53}
test/link/wasm/type/build.zig+9-8
...@@ -25,17 +25,18 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -25,17 +25,18 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
25 b.installArtifact(lib);25 b.installArtifact(lib);
2626
27 const check_lib = lib.checkObject();27 const check_lib = lib.checkObject();
28 check_lib.checkStart("Section type");28 check_lib.checkStart();
29 check_lib.checkExact("Section type");
29 // only 2 entries, although we have more functions.30 // only 2 entries, although we have more functions.
30 // This is to test functions with the same function signature31 // This is to test functions with the same function signature
31 // have their types deduplicated.32 // have their types deduplicated.
32 check_lib.checkNext("entries 2");33 check_lib.checkExact("entries 2");
33 check_lib.checkNext("params 1");34 check_lib.checkExact("params 1");
34 check_lib.checkNext("type i32");35 check_lib.checkExact("type i32");
35 check_lib.checkNext("returns 1");36 check_lib.checkExact("returns 1");
36 check_lib.checkNext("type i64");37 check_lib.checkExact("type i64");
37 check_lib.checkNext("params 0");38 check_lib.checkExact("params 0");
38 check_lib.checkNext("returns 0");39 check_lib.checkExact("returns 0");
3940
40 test_step.dependOn(&check_lib.step);41 test_step.dependOn(&check_lib.step);
41}42}