| author | |
| committer | |
| log | e8b613783f383b39018cb83b2b141f991c2bc7ae |
| tree | 32e270a33ac83e44ba3258975960ba5968914c42 |
| parent | 5839054e8591f60cbfdd3693398ecd5844530fe9 |
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.29 files changed, 689 insertions(+), 421 deletions(-)
lib/std/Build/Step/CheckObject.zig+397-187| ... | ... | @@ -18,7 +18,6 @@ step: Step, |
| 18 | 18 | source: std.Build.FileSource, |
| 19 | 19 | max_bytes: usize = 20 * 1024 * 1024, |
| 20 | 20 | checks: std.ArrayList(Check), |
| 21 | dump_symtab: bool = false, | |
| 22 | 21 | obj_format: std.Target.ObjectFormat, |
| 23 | 22 | |
| 24 | 23 | pub fn create( |
| ... | ... | @@ -53,62 +52,41 @@ const SearchPhrase = struct { |
| 53 | 52 | } |
| 54 | 53 | }; |
| 55 | 54 | |
| 56 | /// There two types of actions currently supported: | |
| 57 | /// * `.match` - is the main building block of standard matchers with optional eat-all token `{*}` | |
| 58 | /// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature | |
| 59 | /// i.e., it won't really handle edge cases/nontrivial examples. But given that we do want to use | |
| 60 | /// it mainly to test the output of our object format parser-dumpers when testing the linkers, etc. | |
| 61 | /// it should be plenty useful in its current form. | |
| 62 | /// * `.compute_cmp` - can be used to perform an operation on the extracted global variables | |
| 55 | /// There five types of actions currently supported: | |
| 56 | /// .exact - will do an exact match against the haystack | |
| 57 | /// .contains - will check for existence within the haystack | |
| 58 | /// .not_present - will check for non-existence within the haystack | |
| 59 | /// .extract - will do an exact match and extract into a variable enclosed within `{name}` braces | |
| 60 | /// .compute_cmp - will perform an operation on the extracted global variables | |
| 63 | 61 | /// using the MatchAction. It currently only supports an addition. The operation is required |
| 64 | 62 | /// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well, |
| 65 | 63 | /// to avoid any parsing really). |
| 66 | 64 | /// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively |
| 67 | 65 | /// they could then be added with this simple program `vmaddr entryoff +`. |
| 68 | 66 | const Action = struct { |
| 69 | tag: enum { match, not_present, compute_cmp }, | |
| 67 | tag: enum { exact, contains, not_present, extract, compute_cmp }, | |
| 70 | 68 | phrase: SearchPhrase, |
| 71 | 69 | expected: ?ComputeCompareExpected = null, |
| 72 | 70 | |
| 73 | /// Will return true if the `phrase` was found in the `haystack`. | |
| 74 | /// Some examples include: | |
| 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( | |
| 71 | /// Returns true if the `phrase` is an exact match with the haystack and variable was successfully extracted. | |
| 72 | fn extract( | |
| 82 | 73 | act: Action, |
| 83 | 74 | b: *std.Build, |
| 84 | 75 | step: *Step, |
| 85 | 76 | haystack: []const u8, |
| 86 | 77 | global_vars: anytype, |
| 87 | 78 | ) !bool { |
| 88 | assert(act.tag == .match or act.tag == .not_present); | |
| 89 | const phrase = act.phrase.resolve(b, step); | |
| 79 | assert(act.tag == .extract); | |
| 80 | const hay = mem.trim(u8, haystack, " "); | |
| 81 | const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); | |
| 82 | ||
| 90 | 83 | var candidate_var: ?struct { name: []const u8, value: u64 } = null; |
| 91 | var hay_it = mem.tokenizeScalar(u8, mem.trim(u8, haystack, " "), ' '); | |
| 92 | var needle_it = mem.tokenizeScalar(u8, mem.trim(u8, phrase, " "), ' '); | |
| 84 | var hay_it = mem.tokenizeScalar(u8, hay, ' '); | |
| 85 | var needle_it = mem.tokenizeScalar(u8, phrase, ' '); | |
| 93 | 86 | |
| 94 | 87 | while (needle_it.next()) |needle_tok| { |
| 95 | const hay_tok = hay_it.next() orelse return false; | |
| 96 | ||
| 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, "{")) { | |
| 88 | const hay_tok = hay_it.next() orelse break; | |
| 89 | if (mem.startsWith(u8, needle_tok, "{")) { | |
| 112 | 90 | const closing_brace = mem.indexOf(u8, needle_tok, "}") orelse return error.MissingClosingBrace; |
| 113 | 91 | if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast; |
| 114 | 92 | |
| ... | ... | @@ -124,11 +102,49 @@ const Action = struct { |
| 124 | 102 | } |
| 125 | 103 | } |
| 126 | 104 | |
| 127 | if (candidate_var) |v| { | |
| 128 | try global_vars.putNoClobber(v.name, v.value); | |
| 129 | } | |
| 105 | if (candidate_var) |v| try global_vars.putNoClobber(v.name, v.value); | |
| 106 | return candidate_var != null; | |
| 107 | } | |
| 108 | ||
| 109 | /// Returns true if the `phrase` is an exact match with the haystack. | |
| 110 | fn exact( | |
| 111 | act: Action, | |
| 112 | b: *std.Build, | |
| 113 | step: *Step, | |
| 114 | haystack: []const u8, | |
| 115 | ) bool { | |
| 116 | assert(act.tag == .exact); | |
| 117 | const hay = mem.trim(u8, haystack, " "); | |
| 118 | const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); | |
| 119 | return mem.eql(u8, hay, phrase); | |
| 120 | } | |
| 121 | ||
| 122 | /// Returns true if the `phrase` exists within the haystack. | |
| 123 | fn contains( | |
| 124 | act: Action, | |
| 125 | b: *std.Build, | |
| 126 | step: *Step, | |
| 127 | haystack: []const u8, | |
| 128 | ) bool { | |
| 129 | assert(act.tag == .contains); | |
| 130 | const hay = mem.trim(u8, haystack, " "); | |
| 131 | const phrase = mem.trim(u8, act.phrase.resolve(b, step), " "); | |
| 132 | return mem.indexOf(u8, hay, phrase) != null; | |
| 133 | } | |
| 130 | 134 | |
| 131 | return true; | |
| 135 | /// Returns true if the `phrase` does not exist within the haystack. | |
| 136 | fn notPresent( | |
| 137 | act: Action, | |
| 138 | b: *std.Build, | |
| 139 | step: *Step, | |
| 140 | haystack: []const u8, | |
| 141 | ) bool { | |
| 142 | assert(act.tag == .not_present); | |
| 143 | return !contains(.{ | |
| 144 | .tag = .contains, | |
| 145 | .phrase = act.phrase, | |
| 146 | .expected = act.expected, | |
| 147 | }, b, step, haystack); | |
| 132 | 148 | } |
| 133 | 149 | |
| 134 | 150 | /// Will return true if the `phrase` is correctly parsed into an RPN program and |
| ... | ... | @@ -235,9 +251,23 @@ const Check = struct { |
| 235 | 251 | }; |
| 236 | 252 | } |
| 237 | 253 | |
| 238 | fn match(self: *Check, phrase: SearchPhrase) void { | |
| 254 | fn extract(self: *Check, phrase: SearchPhrase) void { | |
| 255 | self.actions.append(.{ | |
| 256 | .tag = .extract, | |
| 257 | .phrase = phrase, | |
| 258 | }) catch @panic("OOM"); | |
| 259 | } | |
| 260 | ||
| 261 | fn exact(self: *Check, phrase: SearchPhrase) void { | |
| 262 | self.actions.append(.{ | |
| 263 | .tag = .exact, | |
| 264 | .phrase = phrase, | |
| 265 | }) catch @panic("OOM"); | |
| 266 | } | |
| 267 | ||
| 268 | fn contains(self: *Check, phrase: SearchPhrase) void { | |
| 239 | 269 | self.actions.append(.{ |
| 240 | .tag = .match, | |
| 270 | .tag = .contains, | |
| 241 | 271 | .phrase = phrase, |
| 242 | 272 | }) catch @panic("OOM"); |
| 243 | 273 | } |
| ... | ... | @@ -258,52 +288,118 @@ const Check = struct { |
| 258 | 288 | } |
| 259 | 289 | }; |
| 260 | 290 | |
| 261 | /// Creates a new sequence of actions with `phrase` as the first anchor searched phrase. | |
| 262 | pub fn checkStart(self: *CheckObject, phrase: []const u8) void { | |
| 291 | /// Creates a new empty sequence of actions. | |
| 292 | pub fn checkStart(self: *CheckObject) void { | |
| 263 | 293 | var new_check = Check.create(self.step.owner.allocator); |
| 264 | new_check.match(.{ .string = self.step.owner.dupe(phrase) }); | |
| 265 | 294 | self.checks.append(new_check) catch @panic("OOM"); |
| 266 | 295 | } |
| 267 | 296 | |
| 268 | /// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)`. | |
| 269 | /// Asserts at least one check already exists. | |
| 270 | pub fn checkNext(self: *CheckObject, phrase: []const u8) void { | |
| 297 | /// Adds an exact match phrase to the latest created Check with `CheckObject.checkStart()`. | |
| 298 | pub fn checkExact(self: *CheckObject, phrase: []const u8) void { | |
| 299 | self.checkExactInner(phrase, null); | |
| 300 | } | |
| 301 | ||
| 302 | /// Like `checkExact()` but takes an additional argument `FileSource` which will be | |
| 303 | /// resolved to a full search query in `make()`. | |
| 304 | pub fn checkExactFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { | |
| 305 | self.checkExactInner(phrase, file_source); | |
| 306 | } | |
| 307 | ||
| 308 | fn checkExactInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { | |
| 271 | 309 | assert(self.checks.items.len > 0); |
| 272 | 310 | const last = &self.checks.items[self.checks.items.len - 1]; |
| 273 | last.match(.{ .string = self.step.owner.dupe(phrase) }); | |
| 311 | last.exact(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); | |
| 274 | 312 | } |
| 275 | 313 | |
| 276 | /// Like `checkNext()` but takes an additional argument `FileSource` which will be | |
| 314 | /// Adds a fuzzy match phrase to the latest created Check with `CheckObject.checkStart()`. | |
| 315 | pub fn checkContains(self: *CheckObject, phrase: []const u8) void { | |
| 316 | self.checkContainsInner(phrase, null); | |
| 317 | } | |
| 318 | ||
| 319 | /// Like `checkContains()` but takes an additional argument `FileSource` which will be | |
| 277 | 320 | /// resolved to a full search query in `make()`. |
| 278 | pub fn checkNextFileSource( | |
| 279 | self: *CheckObject, | |
| 280 | phrase: []const u8, | |
| 281 | file_source: std.Build.FileSource, | |
| 282 | ) void { | |
| 321 | pub fn checkContainsFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { | |
| 322 | self.checkContainsInner(phrase, file_source); | |
| 323 | } | |
| 324 | ||
| 325 | fn checkContainsInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { | |
| 326 | assert(self.checks.items.len > 0); | |
| 327 | const last = &self.checks.items[self.checks.items.len - 1]; | |
| 328 | last.contains(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); | |
| 329 | } | |
| 330 | ||
| 331 | /// Adds an exact match phrase with variable extractor to the latest created Check | |
| 332 | /// with `CheckObject.checkStart()`. | |
| 333 | pub fn checkExtract(self: *CheckObject, phrase: []const u8) void { | |
| 334 | self.checkExtractInner(phrase, null); | |
| 335 | } | |
| 336 | ||
| 337 | /// Like `checkExtract()` but takes an additional argument `FileSource` which will be | |
| 338 | /// resolved to a full search query in `make()`. | |
| 339 | pub fn checkExtractFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { | |
| 340 | self.checkExtractInner(phrase, file_source); | |
| 341 | } | |
| 342 | ||
| 343 | fn checkExtractInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { | |
| 283 | 344 | assert(self.checks.items.len > 0); |
| 284 | 345 | const last = &self.checks.items[self.checks.items.len - 1]; |
| 285 | last.match(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); | |
| 346 | last.extract(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); | |
| 286 | 347 | } |
| 287 | 348 | |
| 288 | 349 | /// Adds another searched phrase to the latest created Check with `CheckObject.checkStart(...)` |
| 289 | 350 | /// however ensures there is no matching phrase in the output. |
| 290 | /// Asserts at least one check already exists. | |
| 291 | 351 | pub fn checkNotPresent(self: *CheckObject, phrase: []const u8) void { |
| 352 | self.checkNotPresentInner(phrase, null); | |
| 353 | } | |
| 354 | ||
| 355 | /// Like `checkExtract()` but takes an additional argument `FileSource` which will be | |
| 356 | /// resolved to a full search query in `make()`. | |
| 357 | pub fn checkNotPresentFileSource(self: *CheckObject, phrase: []const u8, file_source: std.Build.FileSource) void { | |
| 358 | self.checkNotPresentInner(phrase, file_source); | |
| 359 | } | |
| 360 | ||
| 361 | fn checkNotPresentInner(self: *CheckObject, phrase: []const u8, file_source: ?std.Build.FileSource) void { | |
| 292 | 362 | assert(self.checks.items.len > 0); |
| 293 | 363 | const last = &self.checks.items[self.checks.items.len - 1]; |
| 294 | last.notPresent(.{ .string = self.step.owner.dupe(phrase) }); | |
| 364 | last.notPresent(.{ .string = self.step.owner.dupe(phrase), .file_source = file_source }); | |
| 295 | 365 | } |
| 296 | 366 | |
| 297 | 367 | /// Creates a new check checking specifically symbol table parsed and dumped from the object |
| 298 | 368 | /// file. |
| 299 | /// Issuing this check will force parsing and dumping of the symbol table. | |
| 300 | 369 | pub fn checkInSymtab(self: *CheckObject) void { |
| 301 | self.dump_symtab = true; | |
| 302 | const symtab_label = switch (self.obj_format) { | |
| 370 | const label = switch (self.obj_format) { | |
| 303 | 371 | .macho => MachODumper.symtab_label, |
| 304 | else => @panic("TODO other parsers"), | |
| 372 | .elf => ElfDumper.symtab_label, | |
| 373 | .wasm => WasmDumper.symtab_label, | |
| 374 | .coff => @panic("TODO symtab for coff"), | |
| 375 | else => @panic("TODO other file formats"), | |
| 305 | 376 | }; |
| 306 | self.checkStart(symtab_label); | |
| 377 | self.checkStart(); | |
| 378 | self.checkExact(label); | |
| 379 | } | |
| 380 | ||
| 381 | /// Creates a new check checking specifically dynamic symbol table parsed and dumped from the object | |
| 382 | /// file. | |
| 383 | /// This check is target-dependent and applicable to ELF only. | |
| 384 | pub fn checkInDynamicSymtab(self: *CheckObject) void { | |
| 385 | const label = switch (self.obj_format) { | |
| 386 | .elf => ElfDumper.dynamic_symtab_label, | |
| 387 | else => @panic("Unsupported target platform"), | |
| 388 | }; | |
| 389 | self.checkStart(); | |
| 390 | self.checkExact(label); | |
| 391 | } | |
| 392 | ||
| 393 | /// Creates a new check checking specifically dynamic section parsed and dumped from the object | |
| 394 | /// file. | |
| 395 | /// This check is target-dependent and applicable to ELF only. | |
| 396 | pub fn checkInDynamicSection(self: *CheckObject) void { | |
| 397 | const label = switch (self.obj_format) { | |
| 398 | .elf => ElfDumper.dynamic_section_label, | |
| 399 | else => @panic("Unsupported target platform"), | |
| 400 | }; | |
| 401 | self.checkStart(); | |
| 402 | self.checkExact(label); | |
| 307 | 403 | } |
| 308 | 404 | |
| 309 | 405 | /// 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 { |
| 336 | 432 | ) catch |err| return step.fail("unable to read '{s}': {s}", .{ src_path, @errorName(err) }); |
| 337 | 433 | |
| 338 | 434 | const output = switch (self.obj_format) { |
| 339 | .macho => try MachODumper.parseAndDump(step, contents, .{ | |
| 340 | .dump_symtab = self.dump_symtab, | |
| 341 | }), | |
| 342 | .elf => try ElfDumper.parseAndDump(step, contents, .{ | |
| 343 | .dump_symtab = self.dump_symtab, | |
| 344 | }), | |
| 435 | .macho => try MachODumper.parseAndDump(step, contents), | |
| 436 | .elf => try ElfDumper.parseAndDump(step, contents), | |
| 345 | 437 | .coff => @panic("TODO coff parser"), |
| 346 | .wasm => try WasmDumper.parseAndDump(step, contents, .{ | |
| 347 | .dump_symtab = self.dump_symtab, | |
| 348 | }), | |
| 438 | .wasm => try WasmDumper.parseAndDump(step, contents), | |
| 349 | 439 | else => unreachable, |
| 350 | 440 | }; |
| 351 | 441 | |
| ... | ... | @@ -355,9 +445,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { |
| 355 | 445 | var it = mem.tokenizeAny(u8, output, "\r\n"); |
| 356 | 446 | for (chk.actions.items) |act| { |
| 357 | 447 | switch (act.tag) { |
| 358 | .match => { | |
| 448 | .exact => { | |
| 359 | 449 | while (it.next()) |line| { |
| 360 | if (try act.match(b, step, line, &vars)) break; | |
| 450 | if (act.exact(b, step, line)) break; | |
| 361 | 451 | } else { |
| 362 | 452 | return step.fail( |
| 363 | 453 | \\ |
| ... | ... | @@ -369,18 +459,46 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { |
| 369 | 459 | , .{ act.phrase.resolve(b, step), output }); |
| 370 | 460 | } |
| 371 | 461 | }, |
| 462 | .contains => { | |
| 463 | while (it.next()) |line| { | |
| 464 | if (act.contains(b, step, line)) break; | |
| 465 | } else { | |
| 466 | return step.fail( | |
| 467 | \\ | |
| 468 | \\========= expected to find: ========================== | |
| 469 | \\*{s}* | |
| 470 | \\========= but parsed file does not contain it: ======= | |
| 471 | \\{s} | |
| 472 | \\====================================================== | |
| 473 | , .{ act.phrase.resolve(b, step), output }); | |
| 474 | } | |
| 475 | }, | |
| 372 | 476 | .not_present => { |
| 373 | 477 | while (it.next()) |line| { |
| 374 | if (try act.match(b, step, line, &vars)) { | |
| 375 | return step.fail( | |
| 376 | \\ | |
| 377 | \\========= expected not to find: =================== | |
| 378 | \\{s} | |
| 379 | \\========= but parsed file does contain it: ======== | |
| 380 | \\{s} | |
| 381 | \\=================================================== | |
| 382 | , .{ act.phrase.resolve(b, step), output }); | |
| 383 | } | |
| 478 | if (act.notPresent(b, step, line)) break; | |
| 479 | } else { | |
| 480 | return step.fail( | |
| 481 | \\ | |
| 482 | \\========= expected not to find: =================== | |
| 483 | \\{s} | |
| 484 | \\========= but parsed file does contain it: ======== | |
| 485 | \\{s} | |
| 486 | \\=================================================== | |
| 487 | , .{ act.phrase.resolve(b, step), output }); | |
| 488 | } | |
| 489 | }, | |
| 490 | .extract => { | |
| 491 | while (it.next()) |line| { | |
| 492 | if (try act.extract(b, step, line, &vars)) break; | |
| 493 | } else { | |
| 494 | return step.fail( | |
| 495 | \\ | |
| 496 | \\========= expected to find and extract: ============== | |
| 497 | \\{s} | |
| 498 | \\========= but parsed file does not contain it: ======= | |
| 499 | \\{s} | |
| 500 | \\====================================================== | |
| 501 | , .{ act.phrase.resolve(b, step), output }); | |
| 384 | 502 | } |
| 385 | 503 | }, |
| 386 | 504 | .compute_cmp => { |
| ... | ... | @@ -410,15 +528,16 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { |
| 410 | 528 | } |
| 411 | 529 | } |
| 412 | 530 | |
| 413 | const Opts = struct { | |
| 414 | dump_symtab: bool = false, | |
| 415 | }; | |
| 416 | ||
| 417 | 531 | const MachODumper = struct { |
| 418 | 532 | const LoadCommandIterator = macho.LoadCommandIterator; |
| 419 | const symtab_label = "symtab"; | |
| 533 | const symtab_label = "symbol table"; | |
| 534 | ||
| 535 | const Symtab = struct { | |
| 536 | symbols: []align(1) const macho.nlist_64, | |
| 537 | strings: []const u8, | |
| 538 | }; | |
| 420 | 539 | |
| 421 | fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8, opts: Opts) ![]const u8 { | |
| 540 | fn parseAndDump(step: *Step, bytes: []align(@alignOf(u64)) const u8) ![]const u8 { | |
| 422 | 541 | const gpa = step.owner.allocator; |
| 423 | 542 | var stream = std.io.fixedBufferStream(bytes); |
| 424 | 543 | const reader = stream.reader(); |
| ... | ... | @@ -431,8 +550,7 @@ const MachODumper = struct { |
| 431 | 550 | var output = std.ArrayList(u8).init(gpa); |
| 432 | 551 | const writer = output.writer(); |
| 433 | 552 | |
| 434 | var symtab: []const macho.nlist_64 = undefined; | |
| 435 | var strtab: []const u8 = undefined; | |
| 553 | var symtab: ?Symtab = null; | |
| 436 | 554 | var sections = std.ArrayList(macho.section_64).init(gpa); |
| 437 | 555 | var imports = std.ArrayList([]const u8).init(gpa); |
| 438 | 556 | |
| ... | ... | @@ -450,13 +568,11 @@ const MachODumper = struct { |
| 450 | 568 | sections.appendAssumeCapacity(sect); |
| 451 | 569 | } |
| 452 | 570 | }, |
| 453 | .SYMTAB => if (opts.dump_symtab) { | |
| 571 | .SYMTAB => { | |
| 454 | 572 | const lc = cmd.cast(macho.symtab_command).?; |
| 455 | symtab = @as( | |
| 456 | [*]const macho.nlist_64, | |
| 457 | @ptrCast(@alignCast(&bytes[lc.symoff])), | |
| 458 | )[0..lc.nsyms]; | |
| 459 | strtab = bytes[lc.stroff..][0..lc.strsize]; | |
| 573 | const symbols = @as([*]align(1) const macho.nlist_64, @ptrCast(bytes.ptr + lc.symoff))[0..lc.nsyms]; | |
| 574 | const strings = bytes[lc.stroff..][0..lc.strsize]; | |
| 575 | symtab = .{ .symbols = symbols, .strings = strings }; | |
| 460 | 576 | }, |
| 461 | 577 | .LOAD_DYLIB, |
| 462 | 578 | .LOAD_WEAK_DYLIB, |
| ... | ... | @@ -473,53 +589,8 @@ const MachODumper = struct { |
| 473 | 589 | i += 1; |
| 474 | 590 | } |
| 475 | 591 | |
| 476 | if (opts.dump_symtab) { | |
| 477 | try writer.print("{s}\n", .{symtab_label}); | |
| 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 | } | |
| 592 | if (symtab) |stab| { | |
| 593 | try dumpSymtab(sections.items, imports.items, stab, writer); | |
| 523 | 594 | } |
| 524 | 595 | |
| 525 | 596 | return output.toOwnedSlice(); |
| ... | ... | @@ -696,10 +767,67 @@ const MachODumper = struct { |
| 696 | 767 | else => {}, |
| 697 | 768 | } |
| 698 | 769 | } |
| 770 | ||
| 771 | fn dumpSymtab( | |
| 772 | sections: []const macho.section_64, | |
| 773 | imports: []const []const u8, | |
| 774 | symtab: Symtab, | |
| 775 | writer: anytype, | |
| 776 | ) !void { | |
| 777 | try writer.writeAll(symtab_label ++ "\n"); | |
| 778 | ||
| 779 | for (symtab.symbols) |sym| { | |
| 780 | if (sym.stab()) continue; | |
| 781 | const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(symtab.strings.ptr + sym.n_strx)), 0); | |
| 782 | if (sym.sect()) { | |
| 783 | const sect = sections[sym.n_sect - 1]; | |
| 784 | try writer.print("{x} ({s},{s})", .{ | |
| 785 | sym.n_value, | |
| 786 | sect.segName(), | |
| 787 | sect.sectName(), | |
| 788 | }); | |
| 789 | if (sym.ext()) { | |
| 790 | try writer.writeAll(" external"); | |
| 791 | } | |
| 792 | try writer.print(" {s}\n", .{sym_name}); | |
| 793 | } else if (sym.undf()) { | |
| 794 | const ordinal = @divTrunc(@as(i16, @bitCast(sym.n_desc)), macho.N_SYMBOL_RESOLVER); | |
| 795 | const import_name = blk: { | |
| 796 | if (ordinal <= 0) { | |
| 797 | if (ordinal == macho.BIND_SPECIAL_DYLIB_SELF) | |
| 798 | break :blk "self import"; | |
| 799 | if (ordinal == macho.BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) | |
| 800 | break :blk "main executable"; | |
| 801 | if (ordinal == macho.BIND_SPECIAL_DYLIB_FLAT_LOOKUP) | |
| 802 | break :blk "flat lookup"; | |
| 803 | unreachable; | |
| 804 | } | |
| 805 | const full_path = imports[@as(u16, @bitCast(ordinal)) - 1]; | |
| 806 | const basename = fs.path.basename(full_path); | |
| 807 | assert(basename.len > 0); | |
| 808 | const ext = mem.lastIndexOfScalar(u8, basename, '.') orelse basename.len; | |
| 809 | break :blk basename[0..ext]; | |
| 810 | }; | |
| 811 | try writer.writeAll("(undefined)"); | |
| 812 | if (sym.weakRef()) { | |
| 813 | try writer.writeAll(" weak"); | |
| 814 | } | |
| 815 | if (sym.ext()) { | |
| 816 | try writer.writeAll(" external"); | |
| 817 | } | |
| 818 | try writer.print(" {s} (from {s})\n", .{ | |
| 819 | sym_name, | |
| 820 | import_name, | |
| 821 | }); | |
| 822 | } else unreachable; | |
| 823 | } | |
| 824 | } | |
| 699 | 825 | }; |
| 700 | 826 | |
| 701 | 827 | const ElfDumper = struct { |
| 702 | const symtab_label = "symtab"; | |
| 828 | const symtab_label = "symbol table"; | |
| 829 | const dynamic_symtab_label = "dynamic symbol table"; | |
| 830 | const dynamic_section_label = "dynamic section"; | |
| 703 | 831 | |
| 704 | 832 | const Symtab = struct { |
| 705 | 833 | symbols: []align(1) const elf.Elf64_Sym, |
| ... | ... | @@ -727,7 +855,7 @@ const ElfDumper = struct { |
| 727 | 855 | dysymtab: ?Symtab = null, |
| 728 | 856 | }; |
| 729 | 857 | |
| 730 | fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 { | |
| 858 | fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 { | |
| 731 | 859 | const gpa = step.owner.allocator; |
| 732 | 860 | var stream = std.io.fixedBufferStream(bytes); |
| 733 | 861 | const reader = stream.reader(); |
| ... | ... | @@ -750,34 +878,32 @@ const ElfDumper = struct { |
| 750 | 878 | }; |
| 751 | 879 | ctx.shstrtab = getSectionContents(ctx, ctx.hdr.e_shstrndx); |
| 752 | 880 | |
| 753 | if (opts.dump_symtab) { | |
| 754 | for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) { | |
| 755 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => { | |
| 756 | const raw = getSectionContents(ctx, i); | |
| 757 | const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym)); | |
| 758 | const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms]; | |
| 759 | const strings = getSectionContents(ctx, shdr.sh_link); | |
| 760 | ||
| 761 | switch (shdr.sh_type) { | |
| 762 | elf.SHT_SYMTAB => { | |
| 763 | ctx.symtab = .{ | |
| 764 | .symbols = symbols, | |
| 765 | .strings = strings, | |
| 766 | }; | |
| 767 | }, | |
| 768 | elf.SHT_DYNSYM => { | |
| 769 | ctx.dysymtab = .{ | |
| 770 | .symbols = symbols, | |
| 771 | .strings = strings, | |
| 772 | }; | |
| 773 | }, | |
| 774 | else => unreachable, | |
| 775 | } | |
| 776 | }, | |
| 881 | for (ctx.shdrs, 0..) |shdr, i| switch (shdr.sh_type) { | |
| 882 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => { | |
| 883 | const raw = getSectionContents(ctx, i); | |
| 884 | const nsyms = @divExact(raw.len, @sizeOf(elf.Elf64_Sym)); | |
| 885 | const symbols = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(raw.ptr))[0..nsyms]; | |
| 886 | const strings = getSectionContents(ctx, shdr.sh_link); | |
| 887 | ||
| 888 | switch (shdr.sh_type) { | |
| 889 | elf.SHT_SYMTAB => { | |
| 890 | ctx.symtab = .{ | |
| 891 | .symbols = symbols, | |
| 892 | .strings = strings, | |
| 893 | }; | |
| 894 | }, | |
| 895 | elf.SHT_DYNSYM => { | |
| 896 | ctx.dysymtab = .{ | |
| 897 | .symbols = symbols, | |
| 898 | .strings = strings, | |
| 899 | }; | |
| 900 | }, | |
| 901 | else => unreachable, | |
| 902 | } | |
| 903 | }, | |
| 777 | 904 | |
| 778 | else => {}, | |
| 779 | }; | |
| 780 | } | |
| 905 | else => {}, | |
| 906 | }; | |
| 781 | 907 | |
| 782 | 908 | var output = std.ArrayList(u8).init(gpa); |
| 783 | 909 | const writer = output.writer(); |
| ... | ... | @@ -785,15 +911,16 @@ const ElfDumper = struct { |
| 785 | 911 | try dumpHeader(ctx, writer); |
| 786 | 912 | try dumpShdrs(ctx, writer); |
| 787 | 913 | try dumpPhdrs(ctx, writer); |
| 788 | try dumpDynamic(ctx, writer); | |
| 914 | try dumpDynamicSection(ctx, writer); | |
| 915 | try dumpSymtab(ctx, .symtab, writer); | |
| 916 | try dumpSymtab(ctx, .dysymtab, writer); | |
| 789 | 917 | |
| 790 | 918 | return output.toOwnedSlice(); |
| 791 | 919 | } |
| 792 | 920 | |
| 793 | fn getSectionName(ctx: Context, shndx: usize) []const u8 { | |
| 921 | inline fn getSectionName(ctx: Context, shndx: usize) []const u8 { | |
| 794 | 922 | const shdr = ctx.shdrs[shndx]; |
| 795 | assert(shdr.sh_name < ctx.shstrtab.len); | |
| 796 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(ctx.shstrtab.ptr + shdr.sh_name)), 0); | |
| 923 | return getString(ctx.shstrtab, shdr.sh_name); | |
| 797 | 924 | } |
| 798 | 925 | |
| 799 | 926 | fn getSectionContents(ctx: Context, shndx: usize) []const u8 { |
| ... | ... | @@ -835,7 +962,7 @@ const ElfDumper = struct { |
| 835 | 962 | } |
| 836 | 963 | } |
| 837 | 964 | |
| 838 | fn dumpDynamic(ctx: Context, writer: anytype) !void { | |
| 965 | fn dumpDynamicSection(ctx: Context, writer: anytype) !void { | |
| 839 | 966 | const shndx = getSectionByName(ctx, ".dynamic") orelse return; |
| 840 | 967 | const shdr = ctx.shdrs[shndx]; |
| 841 | 968 | const strtab = getSectionContents(ctx, shdr.sh_link); |
| ... | ... | @@ -843,6 +970,8 @@ const ElfDumper = struct { |
| 843 | 970 | const nentries = @divExact(data.len, @sizeOf(elf.Elf64_Dyn)); |
| 844 | 971 | const entries = @as([*]align(1) const elf.Elf64_Dyn, @ptrCast(data.ptr))[0..nentries]; |
| 845 | 972 | |
| 973 | try writer.writeAll(ElfDumper.dynamic_section_label ++ "\n"); | |
| 974 | ||
| 846 | 975 | for (entries) |entry| { |
| 847 | 976 | const key = @as(u64, @bitCast(entry.d_tag)); |
| 848 | 977 | const value = entry.d_val; |
| ... | ... | @@ -1072,17 +1201,98 @@ const ElfDumper = struct { |
| 1072 | 1201 | try writer.writeAll(p_type); |
| 1073 | 1202 | } |
| 1074 | 1203 | } |
| 1204 | ||
| 1205 | fn dumpSymtab(ctx: Context, comptime @"type": enum { symtab, dysymtab }, writer: anytype) !void { | |
| 1206 | const symtab = switch (@"type") { | |
| 1207 | .symtab => ctx.symtab, | |
| 1208 | .dysymtab => ctx.dysymtab, | |
| 1209 | } orelse return; | |
| 1210 | ||
| 1211 | try writer.writeAll(switch (@"type") { | |
| 1212 | .symtab => symtab_label, | |
| 1213 | .dysymtab => dynamic_symtab_label, | |
| 1214 | } ++ "\n"); | |
| 1215 | ||
| 1216 | for (symtab.symbols, 0..) |sym, index| { | |
| 1217 | try writer.print("{x} {x}", .{ sym.st_value, sym.st_size }); | |
| 1218 | ||
| 1219 | { | |
| 1220 | const tt = sym.st_type(); | |
| 1221 | if (elf.STT_LOPROC <= tt and tt < elf.STT_HIPROC) { | |
| 1222 | try writer.print(" LOPROC+{d}", .{tt - elf.STT_LOPROC}); | |
| 1223 | } else if (elf.STT_LOOS <= tt and tt < elf.STT_HIOS) { | |
| 1224 | try writer.print(" LOOS+{d}", .{tt - elf.STT_LOOS}); | |
| 1225 | } else { | |
| 1226 | const sym_type = switch (tt) { | |
| 1227 | elf.STT_NOTYPE => "NOTYPE", | |
| 1228 | elf.STT_OBJECT => "OBJECT", | |
| 1229 | elf.STT_FUNC => "FUNC", | |
| 1230 | elf.STT_SECTION => "SECTION", | |
| 1231 | elf.STT_FILE => "FILE", | |
| 1232 | elf.STT_COMMON => "COMMON", | |
| 1233 | elf.STT_TLS => "TLS", | |
| 1234 | elf.STT_NUM => "NUM", | |
| 1235 | else => "UNK", | |
| 1236 | }; | |
| 1237 | try writer.print(" {s}", .{sym_type}); | |
| 1238 | } | |
| 1239 | } | |
| 1240 | ||
| 1241 | { | |
| 1242 | const bind = sym.st_bind(); | |
| 1243 | if (elf.STB_LOPROC <= bind and bind < elf.STB_HIPROC) { | |
| 1244 | try writer.print(" LOPROC+{d}", .{bind - elf.STB_LOPROC}); | |
| 1245 | } else if (elf.STB_LOOS <= bind and bind < elf.STB_HIOS) { | |
| 1246 | try writer.print(" LOOS+{d}", .{bind - elf.STB_LOOS}); | |
| 1247 | } else { | |
| 1248 | const sym_bind = switch (bind) { | |
| 1249 | elf.STB_LOCAL => "LOCAL", | |
| 1250 | elf.STB_GLOBAL => "GLOBAL", | |
| 1251 | elf.STB_WEAK => "WEAK", | |
| 1252 | elf.STB_NUM => "NUM", | |
| 1253 | else => "UNKNOWN", | |
| 1254 | }; | |
| 1255 | try writer.print(" {s}", .{sym_bind}); | |
| 1256 | } | |
| 1257 | } | |
| 1258 | ||
| 1259 | const sym_vis = @as(elf.STV, @enumFromInt(sym.st_other)); | |
| 1260 | try writer.print(" {s}", .{@tagName(sym_vis)}); | |
| 1261 | ||
| 1262 | { | |
| 1263 | if (elf.SHN_LORESERVE <= sym.st_shndx and sym.st_shndx < elf.SHN_HIRESERVE) { | |
| 1264 | if (elf.SHN_LOPROC <= sym.st_shndx and sym.st_shndx < elf.SHN_HIPROC) { | |
| 1265 | try writer.print(" LO+{d}", .{sym.st_shndx - elf.SHN_LOPROC}); | |
| 1266 | } else { | |
| 1267 | const sym_ndx = &switch (sym.st_shndx) { | |
| 1268 | elf.SHN_ABS => "ABS", | |
| 1269 | elf.SHN_COMMON => "COM", | |
| 1270 | elf.SHN_LIVEPATCH => "LIV", | |
| 1271 | else => "UNK", | |
| 1272 | }; | |
| 1273 | try writer.print(" {s}", .{sym_ndx}); | |
| 1274 | } | |
| 1275 | } else if (sym.st_shndx == elf.SHN_UNDEF) { | |
| 1276 | try writer.writeAll(" UND"); | |
| 1277 | } else { | |
| 1278 | try writer.print(" {d}", .{sym.st_shndx}); | |
| 1279 | } | |
| 1280 | } | |
| 1281 | ||
| 1282 | const sym_name = switch (sym.st_type()) { | |
| 1283 | elf.STT_SECTION => getSectionName(ctx, sym.st_shndx), | |
| 1284 | else => symtab.getName(index).?, | |
| 1285 | }; | |
| 1286 | try writer.print(" {s}\n", .{sym_name}); | |
| 1287 | } | |
| 1288 | } | |
| 1075 | 1289 | }; |
| 1076 | 1290 | |
| 1077 | 1291 | const WasmDumper = struct { |
| 1078 | 1292 | const symtab_label = "symbols"; |
| 1079 | 1293 | |
| 1080 | fn parseAndDump(step: *Step, bytes: []const u8, opts: Opts) ![]const u8 { | |
| 1294 | fn parseAndDump(step: *Step, bytes: []const u8) ![]const u8 { | |
| 1081 | 1295 | const gpa = step.owner.allocator; |
| 1082 | if (opts.dump_symtab) { | |
| 1083 | @panic("TODO: Implement symbol table parsing and dumping"); | |
| 1084 | } | |
| 1085 | ||
| 1086 | 1296 | var fbs = std.io.fixedBufferStream(bytes); |
| 1087 | 1297 | const reader = fbs.reader(); |
| 1088 | 1298 |
test/link/macho/dead_strip/build.zig+2-2| ... | ... | @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) void { |
| 15 | 15 | |
| 16 | 16 | const check = exe.checkObject(); |
| 17 | 17 | check.checkInSymtab(); |
| 18 | check.checkNext("{*} (__TEXT,__text) external _iAmUnused"); | |
| 18 | check.checkContains("(__TEXT,__text) external _iAmUnused"); | |
| 19 | 19 | test_step.dependOn(&check.step); |
| 20 | 20 | |
| 21 | 21 | const run = b.addRunArtifact(exe); |
| ... | ... | @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void { |
| 31 | 31 | |
| 32 | 32 | const check = exe.checkObject(); |
| 33 | 33 | check.checkInSymtab(); |
| 34 | check.checkNotPresent("{*} (__TEXT,__text) external _iAmUnused"); | |
| 34 | check.checkNotPresent("(__TEXT,__text) external _iAmUnused"); | |
| 35 | 35 | test_step.dependOn(&check.step); |
| 36 | 36 | |
| 37 | 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 | 19 | const exe = createScenario(b, optimize, "no-dead-strip"); |
| 20 | 20 | |
| 21 | 21 | const check = exe.checkObject(); |
| 22 | check.checkStart("cmd LOAD_DYLIB"); | |
| 23 | check.checkNext("name {*}Cocoa"); | |
| 22 | check.checkStart(); | |
| 23 | check.checkExact("cmd LOAD_DYLIB"); | |
| 24 | check.checkContains("Cocoa"); | |
| 24 | 25 | |
| 25 | check.checkStart("cmd LOAD_DYLIB"); | |
| 26 | check.checkNext("name {*}libobjc{*}.dylib"); | |
| 26 | check.checkStart(); | |
| 27 | check.checkExact("cmd LOAD_DYLIB"); | |
| 28 | check.checkContains("libobjc"); | |
| 27 | 29 | |
| 28 | 30 | test_step.dependOn(&check.step); |
| 29 | 31 |
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 | 25 | dylib.linkLibC(); |
| 26 | 26 | |
| 27 | 27 | const check_dylib = dylib.checkObject(); |
| 28 | check_dylib.checkStart("cmd ID_DYLIB"); | |
| 29 | check_dylib.checkNext("name @rpath/liba.dylib"); | |
| 30 | check_dylib.checkNext("timestamp 2"); | |
| 31 | check_dylib.checkNext("current version 10000"); | |
| 32 | check_dylib.checkNext("compatibility version 10000"); | |
| 28 | check_dylib.checkStart(); | |
| 29 | check_dylib.checkExact("cmd ID_DYLIB"); | |
| 30 | check_dylib.checkExact("name @rpath/liba.dylib"); | |
| 31 | check_dylib.checkExact("timestamp 2"); | |
| 32 | check_dylib.checkExact("current version 10000"); | |
| 33 | check_dylib.checkExact("compatibility version 10000"); | |
| 33 | 34 | |
| 34 | 35 | test_step.dependOn(&check_dylib.step); |
| 35 | 36 | |
| ... | ... | @@ -45,14 +46,16 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize |
| 45 | 46 | exe.linkLibC(); |
| 46 | 47 | |
| 47 | 48 | const check_exe = exe.checkObject(); |
| 48 | check_exe.checkStart("cmd LOAD_DYLIB"); | |
| 49 | check_exe.checkNext("name @rpath/liba.dylib"); | |
| 50 | check_exe.checkNext("timestamp 2"); | |
| 51 | check_exe.checkNext("current version 10000"); | |
| 52 | check_exe.checkNext("compatibility version 10000"); | |
| 53 | ||
| 54 | check_exe.checkStart("cmd RPATH"); | |
| 55 | check_exe.checkNextFileSource("path", dylib.getOutputDirectorySource()); | |
| 49 | check_exe.checkStart(); | |
| 50 | check_exe.checkExact("cmd LOAD_DYLIB"); | |
| 51 | check_exe.checkExact("name @rpath/liba.dylib"); | |
| 52 | check_exe.checkExact("timestamp 2"); | |
| 53 | check_exe.checkExact("current version 10000"); | |
| 54 | check_exe.checkExact("compatibility version 10000"); | |
| 55 | ||
| 56 | check_exe.checkStart(); | |
| 57 | check_exe.checkExact("cmd RPATH"); | |
| 58 | check_exe.checkExactFileSource("path", dylib.getOutputDirectorySource()); | |
| 56 | 59 | test_step.dependOn(&check_exe.step); |
| 57 | 60 | |
| 58 | 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 | 24 | |
| 25 | 25 | const check_exe = exe.checkObject(); |
| 26 | 26 | |
| 27 | check_exe.checkStart("segname __TEXT"); | |
| 28 | check_exe.checkNext("vmaddr {vmaddr}"); | |
| 27 | check_exe.checkStart(); | |
| 28 | check_exe.checkExact("segname __TEXT"); | |
| 29 | check_exe.checkExtract("vmaddr {vmaddr}"); | |
| 29 | 30 | |
| 30 | check_exe.checkStart("cmd MAIN"); | |
| 31 | check_exe.checkNext("entryoff {entryoff}"); | |
| 31 | check_exe.checkStart(); | |
| 32 | check_exe.checkExact("cmd MAIN"); | |
| 33 | check_exe.checkExtract("entryoff {entryoff}"); | |
| 32 | 34 | |
| 33 | 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"); | |
| 35 | 37 | |
| 36 | 38 | check_exe.checkComputeCompare("vmaddr entryoff +", .{ .op = .eq, .value = .{ .variable = "n_value" } }); |
| 37 | 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 | 34 | exe.forceUndefinedSymbol("_my_main"); |
| 35 | 35 | |
| 36 | 36 | const check_exe = exe.checkObject(); |
| 37 | check_exe.checkStart("segname __TEXT"); | |
| 38 | check_exe.checkNext("vmaddr {text_vmaddr}"); | |
| 37 | check_exe.checkStart(); | |
| 38 | check_exe.checkExact("segname __TEXT"); | |
| 39 | check_exe.checkExtract("vmaddr {text_vmaddr}"); | |
| 39 | 40 | |
| 40 | check_exe.checkStart("sectname __stubs"); | |
| 41 | check_exe.checkNext("addr {stubs_vmaddr}"); | |
| 41 | check_exe.checkStart(); | |
| 42 | check_exe.checkExact("sectname __stubs"); | |
| 43 | check_exe.checkExtract("addr {stubs_vmaddr}"); | |
| 42 | 44 | |
| 43 | check_exe.checkStart("cmd MAIN"); | |
| 44 | check_exe.checkNext("entryoff {entryoff}"); | |
| 45 | check_exe.checkStart(); | |
| 46 | check_exe.checkExact("cmd MAIN"); | |
| 47 | check_exe.checkExtract("entryoff {entryoff}"); | |
| 45 | 48 | |
| 46 | 49 | check_exe.checkComputeCompare("text_vmaddr entryoff +", .{ |
| 47 | 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 | 21 | exe.headerpad_max_install_names = true; |
| 22 | 22 | |
| 23 | 23 | const check = exe.checkObject(); |
| 24 | check.checkStart("sectname __text"); | |
| 25 | check.checkNext("offset {offset}"); | |
| 24 | check.checkStart(); | |
| 25 | check.checkExact("sectname __text"); | |
| 26 | check.checkExtract("offset {offset}"); | |
| 26 | 27 | |
| 27 | 28 | switch (builtin.cpu.arch) { |
| 28 | 29 | .aarch64 => { |
| ... | ... | @@ -46,8 +47,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize |
| 46 | 47 | exe.headerpad_size = 0x10000; |
| 47 | 48 | |
| 48 | 49 | const check = exe.checkObject(); |
| 49 | check.checkStart("sectname __text"); | |
| 50 | check.checkNext("offset {offset}"); | |
| 50 | check.checkStart(); | |
| 51 | check.checkExact("sectname __text"); | |
| 52 | check.checkExtract("offset {offset}"); | |
| 51 | 53 | check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } }); |
| 52 | 54 | |
| 53 | 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 | 65 | exe.headerpad_size = 0x10000; |
| 64 | 66 | |
| 65 | 67 | const check = exe.checkObject(); |
| 66 | check.checkStart("sectname __text"); | |
| 67 | check.checkNext("offset {offset}"); | |
| 68 | check.checkStart(); | |
| 69 | check.checkExact("sectname __text"); | |
| 70 | check.checkExtract("offset {offset}"); | |
| 68 | 71 | check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } }); |
| 69 | 72 | |
| 70 | 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 | 83 | exe.headerpad_max_install_names = true; |
| 81 | 84 | |
| 82 | 85 | const check = exe.checkObject(); |
| 83 | check.checkStart("sectname __text"); | |
| 84 | check.checkNext("offset {offset}"); | |
| 86 | check.checkStart(); | |
| 87 | check.checkExact("sectname __text"); | |
| 88 | check.checkExtract("offset {offset}"); | |
| 85 | 89 | |
| 86 | 90 | switch (builtin.cpu.arch) { |
| 87 | 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 | 25 | const check = obj.checkObject(); |
| 26 | 26 | |
| 27 | 27 | check.checkInSymtab(); |
| 28 | check.checkNext("{*} (__DATA,__TestGlobal) external _test_global"); | |
| 28 | check.checkContains("(__DATA,__TestGlobal) external _test_global"); | |
| 29 | 29 | |
| 30 | 30 | check.checkInSymtab(); |
| 31 | check.checkNext("{*} (__TEXT,__TestFn) external _testFn"); | |
| 31 | check.checkContains("(__TEXT,__TestFn) external _testFn"); | |
| 32 | 32 | |
| 33 | 33 | if (optimize == .Debug) { |
| 34 | 34 | check.checkInSymtab(); |
| 35 | check.checkNext("{*} (__TEXT,__TestGenFnA) _main.testGenericFn__anon_{*}"); | |
| 35 | check.checkContains("(__TEXT,__TestGenFnA) _main.testGenericFn__anon_"); | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 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 | 26 | exe.dead_strip_dylibs = true; |
| 27 | 27 | |
| 28 | 28 | const check = exe.checkObject(); |
| 29 | check.checkStart("cmd LOAD_DYLIB"); | |
| 30 | check.checkNext("name {*}Cocoa"); | |
| 29 | check.checkStart(); | |
| 30 | check.checkExact("cmd LOAD_DYLIB"); | |
| 31 | check.checkContains("Cocoa"); | |
| 31 | 32 | test_step.dependOn(&check.step); |
| 32 | 33 | |
| 33 | 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 | 39 | exe.dead_strip_dylibs = true; |
| 40 | 40 | |
| 41 | 41 | const check = exe.checkObject(); |
| 42 | check.checkStart("cmd LOAD_DYLIB"); | |
| 43 | check.checkNext("name @rpath/liba.dylib"); | |
| 42 | check.checkStart(); | |
| 43 | check.checkExact("cmd LOAD_DYLIB"); | |
| 44 | check.checkExact("name @rpath/liba.dylib"); | |
| 44 | 45 | test_step.dependOn(&check.step); |
| 45 | 46 | |
| 46 | 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 | 20 | exe.pagezero_size = 0x4000; |
| 21 | 21 | |
| 22 | 22 | const check = exe.checkObject(); |
| 23 | check.checkStart("LC 0"); | |
| 24 | check.checkNext("segname __PAGEZERO"); | |
| 25 | check.checkNext("vmaddr 0"); | |
| 26 | check.checkNext("vmsize 4000"); | |
| 23 | check.checkStart(); | |
| 24 | check.checkExact("LC 0"); | |
| 25 | check.checkExact("segname __PAGEZERO"); | |
| 26 | check.checkExact("vmaddr 0"); | |
| 27 | check.checkExact("vmsize 4000"); | |
| 27 | 28 | |
| 28 | check.checkStart("segname __TEXT"); | |
| 29 | check.checkNext("vmaddr 4000"); | |
| 29 | check.checkStart(); | |
| 30 | check.checkExact("segname __TEXT"); | |
| 31 | check.checkExact("vmaddr 4000"); | |
| 30 | 32 | |
| 31 | 33 | test_step.dependOn(&check.step); |
| 32 | 34 | } |
| ... | ... | @@ -42,9 +44,10 @@ pub fn build(b: *std.Build) void { |
| 42 | 44 | exe.pagezero_size = 0; |
| 43 | 45 | |
| 44 | 46 | const check = exe.checkObject(); |
| 45 | check.checkStart("LC 0"); | |
| 46 | check.checkNext("segname __TEXT"); | |
| 47 | check.checkNext("vmaddr 0"); | |
| 47 | check.checkStart(); | |
| 48 | check.checkExact("LC 0"); | |
| 49 | check.checkExact("segname __TEXT"); | |
| 50 | check.checkExact("vmaddr 0"); | |
| 48 | 51 | |
| 49 | 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 | 21 | exe.search_strategy = .dylibs_first; |
| 22 | 22 | |
| 23 | 23 | const check = exe.checkObject(); |
| 24 | check.checkStart("cmd LOAD_DYLIB"); | |
| 25 | check.checkNext("name @rpath/libsearch_dylibs_first.dylib"); | |
| 24 | check.checkStart(); | |
| 25 | check.checkExact("cmd LOAD_DYLIB"); | |
| 26 | check.checkExact("name @rpath/libsearch_dylibs_first.dylib"); | |
| 26 | 27 | test_step.dependOn(&check.step); |
| 27 | 28 | |
| 28 | 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 | 25 | exe.stack_size = 0x100000000; |
| 26 | 26 | |
| 27 | 27 | const check_exe = exe.checkObject(); |
| 28 | check_exe.checkStart("cmd MAIN"); | |
| 29 | check_exe.checkNext("stacksize 100000000"); | |
| 28 | check_exe.checkStart(); | |
| 29 | check_exe.checkExact("cmd MAIN"); | |
| 30 | check_exe.checkExact("stacksize 100000000"); | |
| 30 | 31 | test_step.dependOn(&check_exe.step); |
| 31 | 32 | |
| 32 | 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 | 26 | |
| 27 | 27 | const check_exe = exe.checkObject(); |
| 28 | 28 | |
| 29 | check_exe.checkStart("cmd SEGMENT_64"); | |
| 30 | check_exe.checkNext("segname __LINKEDIT"); | |
| 31 | check_exe.checkNext("fileoff {fileoff}"); | |
| 32 | check_exe.checkNext("filesz {filesz}"); | |
| 33 | ||
| 34 | check_exe.checkStart("cmd DYLD_INFO_ONLY"); | |
| 35 | check_exe.checkNext("rebaseoff {rebaseoff}"); | |
| 36 | check_exe.checkNext("rebasesize {rebasesize}"); | |
| 37 | check_exe.checkNext("bindoff {bindoff}"); | |
| 38 | check_exe.checkNext("bindsize {bindsize}"); | |
| 39 | check_exe.checkNext("lazybindoff {lazybindoff}"); | |
| 40 | check_exe.checkNext("lazybindsize {lazybindsize}"); | |
| 41 | check_exe.checkNext("exportoff {exportoff}"); | |
| 42 | check_exe.checkNext("exportsize {exportsize}"); | |
| 43 | ||
| 44 | check_exe.checkStart("cmd FUNCTION_STARTS"); | |
| 45 | check_exe.checkNext("dataoff {fstartoff}"); | |
| 46 | check_exe.checkNext("datasize {fstartsize}"); | |
| 47 | ||
| 48 | check_exe.checkStart("cmd DATA_IN_CODE"); | |
| 49 | check_exe.checkNext("dataoff {diceoff}"); | |
| 50 | check_exe.checkNext("datasize {dicesize}"); | |
| 51 | ||
| 52 | check_exe.checkStart("cmd SYMTAB"); | |
| 53 | check_exe.checkNext("symoff {symoff}"); | |
| 54 | check_exe.checkNext("nsyms {symnsyms}"); | |
| 55 | check_exe.checkNext("stroff {stroff}"); | |
| 56 | check_exe.checkNext("strsize {strsize}"); | |
| 57 | ||
| 58 | check_exe.checkStart("cmd DYSYMTAB"); | |
| 59 | check_exe.checkNext("indirectsymoff {dysymoff}"); | |
| 60 | check_exe.checkNext("nindirectsyms {dysymnsyms}"); | |
| 29 | check_exe.checkStart(); | |
| 30 | check_exe.checkExact("cmd SEGMENT_64"); | |
| 31 | check_exe.checkExact("segname __LINKEDIT"); | |
| 32 | check_exe.checkExtract("fileoff {fileoff}"); | |
| 33 | check_exe.checkExtract("filesz {filesz}"); | |
| 34 | ||
| 35 | check_exe.checkStart(); | |
| 36 | check_exe.checkExact("cmd DYLD_INFO_ONLY"); | |
| 37 | check_exe.checkExtract("rebaseoff {rebaseoff}"); | |
| 38 | check_exe.checkExtract("rebasesize {rebasesize}"); | |
| 39 | check_exe.checkExtract("bindoff {bindoff}"); | |
| 40 | check_exe.checkExtract("bindsize {bindsize}"); | |
| 41 | check_exe.checkExtract("lazybindoff {lazybindoff}"); | |
| 42 | check_exe.checkExtract("lazybindsize {lazybindsize}"); | |
| 43 | check_exe.checkExtract("exportoff {exportoff}"); | |
| 44 | check_exe.checkExtract("exportsize {exportsize}"); | |
| 45 | ||
| 46 | check_exe.checkStart(); | |
| 47 | check_exe.checkExact("cmd FUNCTION_STARTS"); | |
| 48 | check_exe.checkExtract("dataoff {fstartoff}"); | |
| 49 | check_exe.checkExtract("datasize {fstartsize}"); | |
| 50 | ||
| 51 | check_exe.checkStart(); | |
| 52 | check_exe.checkExact("cmd DATA_IN_CODE"); | |
| 53 | check_exe.checkExtract("dataoff {diceoff}"); | |
| 54 | check_exe.checkExtract("datasize {dicesize}"); | |
| 55 | ||
| 56 | check_exe.checkStart(); | |
| 57 | check_exe.checkExact("cmd SYMTAB"); | |
| 58 | check_exe.checkExtract("symoff {symoff}"); | |
| 59 | check_exe.checkExtract("nsyms {symnsyms}"); | |
| 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}"); | |
| 61 | 67 | |
| 62 | 68 | switch (builtin.cpu.arch) { |
| 63 | 69 | .aarch64 => { |
| 64 | check_exe.checkStart("cmd CODE_SIGNATURE"); | |
| 65 | check_exe.checkNext("dataoff {codesigoff}"); | |
| 66 | check_exe.checkNext("datasize {codesigsize}"); | |
| 70 | check_exe.checkStart(); | |
| 71 | check_exe.checkExact("cmd CODE_SIGNATURE"); | |
| 72 | check_exe.checkExtract("dataoff {codesigoff}"); | |
| 73 | check_exe.checkExtract("datasize {codesigsize}"); | |
| 67 | 74 | }, |
| 68 | 75 | .x86_64 => {}, |
| 69 | 76 | else => unreachable, |
test/link/macho/unwind_info/build.zig+6-5| ... | ... | @@ -32,20 +32,21 @@ fn testUnwindInfo( |
| 32 | 32 | exe.link_gc_sections = dead_strip; |
| 33 | 33 | |
| 34 | 34 | const check = exe.checkObject(); |
| 35 | check.checkStart("segname __TEXT"); | |
| 36 | check.checkNext("sectname __gcc_except_tab"); | |
| 37 | check.checkNext("sectname __unwind_info"); | |
| 35 | check.checkStart(); | |
| 36 | check.checkExact("segname __TEXT"); | |
| 37 | check.checkExact("sectname __gcc_except_tab"); | |
| 38 | check.checkExact("sectname __unwind_info"); | |
| 38 | 39 | |
| 39 | 40 | switch (builtin.cpu.arch) { |
| 40 | 41 | .aarch64 => { |
| 41 | check.checkNext("sectname __eh_frame"); | |
| 42 | check.checkExact("sectname __eh_frame"); | |
| 42 | 43 | }, |
| 43 | 44 | .x86_64 => {}, // We do not expect `__eh_frame` section on x86_64 in this case |
| 44 | 45 | else => unreachable, |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | check.checkInSymtab(); |
| 48 | check.checkNext("{*} (__TEXT,__text) external ___gxx_personality_v0"); | |
| 49 | check.checkContains("(__TEXT,__text) external ___gxx_personality_v0"); | |
| 49 | 50 | test_step.dependOn(&check.step); |
| 50 | 51 | |
| 51 | 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 | 23 | exe.linkFrameworkWeak("Cocoa"); |
| 24 | 24 | |
| 25 | 25 | const check = exe.checkObject(); |
| 26 | check.checkStart("cmd LOAD_WEAK_DYLIB"); | |
| 27 | check.checkNext("name {*}Cocoa"); | |
| 26 | check.checkStart(); | |
| 27 | check.checkExact("cmd LOAD_WEAK_DYLIB"); | |
| 28 | check.checkContains("Cocoa"); | |
| 28 | 29 | test_step.dependOn(&check.step); |
| 29 | 30 | |
| 30 | 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 | 37 | exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); |
| 38 | 38 | |
| 39 | 39 | const check = exe.checkObject(); |
| 40 | check.checkStart("cmd LOAD_WEAK_DYLIB"); | |
| 41 | check.checkNext("name @rpath/liba.dylib"); | |
| 40 | check.checkStart(); | |
| 41 | check.checkExact("cmd LOAD_WEAK_DYLIB"); | |
| 42 | check.checkExact("name @rpath/liba.dylib"); | |
| 42 | 43 | |
| 43 | 44 | check.checkInSymtab(); |
| 44 | check.checkNext("(undefined) weak external _a (from liba)"); | |
| 45 | check.checkExact("(undefined) weak external _a (from liba)"); | |
| 45 | 46 | |
| 46 | 47 | check.checkInSymtab(); |
| 47 | check.checkNext("(undefined) weak external _asStr (from liba)"); | |
| 48 | check.checkExact("(undefined) weak external _asStr (from liba)"); | |
| 48 | 49 | test_step.dependOn(&check.step); |
| 49 | 50 | |
| 50 | 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 | 26 | lib.strip = false; |
| 27 | 27 | |
| 28 | 28 | const check = lib.checkObject(); |
| 29 | check.checkStart("Section custom"); | |
| 30 | check.checkNext("name __trunch"); // Ensure it was imported and resolved | |
| 29 | check.checkStart(); | |
| 30 | check.checkExact("Section custom"); | |
| 31 | check.checkExact("name __trunch"); // Ensure it was imported and resolved | |
| 31 | 32 | |
| 32 | 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 | 20 | |
| 21 | 21 | // Verify the result contains the features explicitly set on the target for the library. |
| 22 | 22 | const check = lib.checkObject(); |
| 23 | check.checkStart("name target_features"); | |
| 24 | check.checkNext("features 1"); | |
| 25 | check.checkNext("+ atomics"); | |
| 23 | check.checkStart(); | |
| 24 | check.checkExact("name target_features"); | |
| 25 | check.checkExact("features 1"); | |
| 26 | check.checkExact("+ atomics"); | |
| 26 | 27 | |
| 27 | 28 | const test_step = b.step("test", "Run linker test"); |
| 28 | 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 | 29 | const check_lib = lib.checkObject(); |
| 30 | 30 | |
| 31 | 31 | // since we import memory, make sure it exists with the correct naming |
| 32 | check_lib.checkStart("Section import"); | |
| 33 | check_lib.checkNext("entries 1"); | |
| 34 | check_lib.checkNext("module env"); // default module name is "env" | |
| 35 | check_lib.checkNext("name memory"); // as per linker specification | |
| 32 | check_lib.checkStart(); | |
| 33 | check_lib.checkExact("Section import"); | |
| 34 | check_lib.checkExact("entries 1"); | |
| 35 | check_lib.checkExact("module env"); // default module name is "env" | |
| 36 | check_lib.checkExact("name memory"); // as per linker specification | |
| 36 | 37 | |
| 37 | 38 | // since we are importing memory, ensure it's not exported |
| 39 | check_lib.checkStart(); | |
| 38 | 40 | check_lib.checkNotPresent("Section export"); |
| 39 | 41 | |
| 40 | 42 | // validate the name of the stack pointer |
| 41 | check_lib.checkStart("Section custom"); | |
| 42 | check_lib.checkNext("type data_segment"); | |
| 43 | check_lib.checkNext("names 2"); | |
| 44 | check_lib.checkNext("index 0"); | |
| 45 | check_lib.checkNext("name .rodata"); | |
| 43 | check_lib.checkStart(); | |
| 44 | check_lib.checkExact("Section custom"); | |
| 45 | check_lib.checkExact("type data_segment"); | |
| 46 | check_lib.checkExact("names 2"); | |
| 47 | check_lib.checkExact("index 0"); | |
| 48 | check_lib.checkExact("name .rodata"); | |
| 46 | 49 | // for safe optimization modes `undefined` is stored in data instead of bss. |
| 47 | 50 | if (is_safe) { |
| 48 | check_lib.checkNext("index 1"); | |
| 49 | check_lib.checkNext("name .data"); | |
| 51 | check_lib.checkExact("index 1"); | |
| 52 | check_lib.checkExact("name .data"); | |
| 50 | 53 | check_lib.checkNotPresent("name .bss"); |
| 51 | 54 | } else { |
| 52 | check_lib.checkNext("index 1"); // bss section always last | |
| 53 | check_lib.checkNext("name .bss"); | |
| 55 | check_lib.checkExact("index 1"); // bss section always last | |
| 56 | check_lib.checkExact("name .bss"); | |
| 54 | 57 | } |
| 55 | 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 | 73 | lib.import_memory = true; |
| 71 | 74 | |
| 72 | 75 | const check_lib = lib.checkObject(); |
| 73 | check_lib.checkStart("Section custom"); | |
| 74 | check_lib.checkNext("type data_segment"); | |
| 75 | check_lib.checkNext("names 2"); | |
| 76 | check_lib.checkNext("index 0"); | |
| 77 | check_lib.checkNext("name .rodata"); | |
| 78 | check_lib.checkNext("index 1"); | |
| 79 | check_lib.checkNext("name .bss"); | |
| 76 | check_lib.checkStart(); | |
| 77 | check_lib.checkExact("Section custom"); | |
| 78 | check_lib.checkExact("type data_segment"); | |
| 79 | check_lib.checkExact("names 2"); | |
| 80 | check_lib.checkExact("index 0"); | |
| 81 | check_lib.checkExact("name .rodata"); | |
| 82 | check_lib.checkExact("index 1"); | |
| 83 | check_lib.checkExact("name .bss"); | |
| 80 | 84 | |
| 81 | 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 | 21 | |
| 22 | 22 | const check_lib = lib.checkObject(); |
| 23 | 23 | |
| 24 | check_lib.checkStart("Section global"); | |
| 25 | check_lib.checkNext("entries 3"); | |
| 26 | check_lib.checkNext("type i32"); // stack pointer so skip other fields | |
| 27 | check_lib.checkNext("type i32"); | |
| 28 | check_lib.checkNext("mutable false"); | |
| 29 | check_lib.checkNext("i32.const {foo_address}"); | |
| 30 | check_lib.checkNext("type i32"); | |
| 31 | check_lib.checkNext("mutable false"); | |
| 32 | check_lib.checkNext("i32.const {bar_address}"); | |
| 24 | check_lib.checkStart(); | |
| 25 | check_lib.checkExact("Section global"); | |
| 26 | check_lib.checkExact("entries 3"); | |
| 27 | check_lib.checkExact("type i32"); // stack pointer so skip other fields | |
| 28 | check_lib.checkExact("type i32"); | |
| 29 | check_lib.checkExact("mutable false"); | |
| 30 | check_lib.checkExtract("i32.const {foo_address}"); | |
| 31 | check_lib.checkExact("type i32"); | |
| 32 | check_lib.checkExact("mutable false"); | |
| 33 | check_lib.checkExtract("i32.const {bar_address}"); | |
| 33 | 34 | check_lib.checkComputeCompare("foo_address", .{ .op = .eq, .value = .{ .literal = 4 } }); |
| 34 | 35 | check_lib.checkComputeCompare("bar_address", .{ .op = .eq, .value = .{ .literal = 0 } }); |
| 35 | 36 | |
| 36 | check_lib.checkStart("Section export"); | |
| 37 | check_lib.checkNext("entries 3"); | |
| 38 | check_lib.checkNext("name foo"); | |
| 39 | check_lib.checkNext("kind global"); | |
| 40 | check_lib.checkNext("index 1"); | |
| 41 | check_lib.checkNext("name bar"); | |
| 42 | check_lib.checkNext("kind global"); | |
| 43 | check_lib.checkNext("index 2"); | |
| 37 | check_lib.checkStart(); | |
| 38 | check_lib.checkExact("Section export"); | |
| 39 | check_lib.checkExact("entries 3"); | |
| 40 | check_lib.checkExact("name foo"); | |
| 41 | check_lib.checkExact("kind global"); | |
| 42 | check_lib.checkExact("index 1"); | |
| 43 | check_lib.checkExact("name bar"); | |
| 44 | check_lib.checkExact("kind global"); | |
| 45 | check_lib.checkExact("index 2"); | |
| 44 | 46 | |
| 45 | 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 | 43 | force_export.use_lld = false; |
| 44 | 44 | |
| 45 | 45 | const check_no_export = no_export.checkObject(); |
| 46 | check_no_export.checkStart("Section export"); | |
| 47 | check_no_export.checkNext("entries 1"); | |
| 48 | check_no_export.checkNext("name memory"); | |
| 49 | check_no_export.checkNext("kind memory"); | |
| 46 | check_no_export.checkStart(); | |
| 47 | check_no_export.checkExact("Section export"); | |
| 48 | check_no_export.checkExact("entries 1"); | |
| 49 | check_no_export.checkExact("name memory"); | |
| 50 | check_no_export.checkExact("kind memory"); | |
| 50 | 51 | |
| 51 | 52 | const check_dynamic_export = dynamic_export.checkObject(); |
| 52 | check_dynamic_export.checkStart("Section export"); | |
| 53 | check_dynamic_export.checkNext("entries 2"); | |
| 54 | check_dynamic_export.checkNext("name foo"); | |
| 55 | check_dynamic_export.checkNext("kind function"); | |
| 53 | check_dynamic_export.checkStart(); | |
| 54 | check_dynamic_export.checkExact("Section export"); | |
| 55 | check_dynamic_export.checkExact("entries 2"); | |
| 56 | check_dynamic_export.checkExact("name foo"); | |
| 57 | check_dynamic_export.checkExact("kind function"); | |
| 56 | 58 | |
| 57 | 59 | const check_force_export = force_export.checkObject(); |
| 58 | check_force_export.checkStart("Section export"); | |
| 59 | check_force_export.checkNext("entries 2"); | |
| 60 | check_force_export.checkNext("name foo"); | |
| 61 | check_force_export.checkNext("kind function"); | |
| 60 | check_force_export.checkStart(); | |
| 61 | check_force_export.checkExact("Section export"); | |
| 62 | check_force_export.checkExact("entries 2"); | |
| 63 | check_force_export.checkExact("name foo"); | |
| 64 | check_force_export.checkExact("kind function"); | |
| 62 | 65 | |
| 63 | 66 | test_step.dependOn(&check_no_export.step); |
| 64 | 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 | 21 | lib.rdynamic = true; // export `foo` |
| 22 | 22 | |
| 23 | 23 | const check_lib = lib.checkObject(); |
| 24 | check_lib.checkStart("Section import"); | |
| 25 | check_lib.checkNext("entries 2"); // a.hello & b.hello | |
| 26 | check_lib.checkNext("module a"); | |
| 27 | check_lib.checkNext("name hello"); | |
| 28 | check_lib.checkNext("module b"); | |
| 29 | check_lib.checkNext("name hello"); | |
| 24 | check_lib.checkStart(); | |
| 25 | check_lib.checkExact("Section import"); | |
| 26 | check_lib.checkExact("entries 2"); // a.hello & b.hello | |
| 27 | check_lib.checkExact("module a"); | |
| 28 | check_lib.checkExact("name hello"); | |
| 29 | check_lib.checkExact("module b"); | |
| 30 | check_lib.checkExact("name hello"); | |
| 30 | 31 | |
| 31 | 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 | 46 | const check_export = export_table.checkObject(); |
| 47 | 47 | const check_regular = regular_table.checkObject(); |
| 48 | 48 | |
| 49 | check_import.checkStart("Section import"); | |
| 50 | check_import.checkNext("entries 1"); | |
| 51 | check_import.checkNext("module env"); | |
| 52 | check_import.checkNext("name __indirect_function_table"); | |
| 53 | check_import.checkNext("kind table"); | |
| 54 | check_import.checkNext("type funcref"); | |
| 55 | check_import.checkNext("min 1"); // 1 function pointer | |
| 49 | check_import.checkStart(); | |
| 50 | check_import.checkExact("Section import"); | |
| 51 | check_import.checkExact("entries 1"); | |
| 52 | check_import.checkExact("module env"); | |
| 53 | check_import.checkExact("name __indirect_function_table"); | |
| 54 | check_import.checkExact("kind table"); | |
| 55 | check_import.checkExact("type funcref"); | |
| 56 | check_import.checkExact("min 1"); // 1 function pointer | |
| 56 | 57 | check_import.checkNotPresent("max"); // when importing, we do not provide a max |
| 57 | 58 | check_import.checkNotPresent("Section table"); // we're importing it |
| 58 | 59 | |
| 59 | check_export.checkStart("Section export"); | |
| 60 | check_export.checkNext("entries 2"); | |
| 61 | check_export.checkNext("name __indirect_function_table"); // as per linker specification | |
| 62 | check_export.checkNext("kind table"); | |
| 60 | check_export.checkStart(); | |
| 61 | check_export.checkExact("Section export"); | |
| 62 | check_export.checkExact("entries 2"); | |
| 63 | check_export.checkExact("name __indirect_function_table"); // as per linker specification | |
| 64 | check_export.checkExact("kind table"); | |
| 63 | 65 | |
| 64 | check_regular.checkStart("Section table"); | |
| 65 | check_regular.checkNext("entries 1"); | |
| 66 | check_regular.checkNext("type funcref"); | |
| 67 | check_regular.checkNext("min 2"); // index starts at 1 & 1 function pointer = 2. | |
| 68 | check_regular.checkNext("max 2"); | |
| 69 | check_regular.checkStart("Section element"); | |
| 70 | check_regular.checkNext("entries 1"); | |
| 71 | check_regular.checkNext("table index 0"); | |
| 72 | check_regular.checkNext("i32.const 1"); // we want to start function indexes at 1 | |
| 73 | check_regular.checkNext("indexes 1"); // 1 function pointer | |
| 66 | check_regular.checkStart(); | |
| 67 | check_regular.checkExact("Section table"); | |
| 68 | check_regular.checkExact("entries 1"); | |
| 69 | check_regular.checkExact("type funcref"); | |
| 70 | check_regular.checkExact("min 2"); // index starts at 1 & 1 function pointer = 2. | |
| 71 | check_regular.checkExact("max 2"); | |
| 72 | ||
| 73 | check_regular.checkStart(); | |
| 74 | check_regular.checkExact("Section element"); | |
| 75 | 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 | |
| 74 | 79 | |
| 75 | 80 | test_step.dependOn(&check_import.step); |
| 76 | 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 | 33 | |
| 34 | 34 | // Verify the result contains the features from the C Object file. |
| 35 | 35 | const check = lib.checkObject(); |
| 36 | check.checkStart("name target_features"); | |
| 37 | check.checkNext("features 7"); | |
| 38 | check.checkNext("+ atomics"); | |
| 39 | check.checkNext("+ bulk-memory"); | |
| 40 | check.checkNext("+ mutable-globals"); | |
| 41 | check.checkNext("+ nontrapping-fptoint"); | |
| 42 | check.checkNext("+ sign-ext"); | |
| 43 | check.checkNext("+ simd128"); | |
| 44 | check.checkNext("+ tail-call"); | |
| 36 | check.checkStart(); | |
| 37 | check.checkExact("name target_features"); | |
| 38 | check.checkExact("features 7"); | |
| 39 | check.checkExact("+ atomics"); | |
| 40 | check.checkExact("+ bulk-memory"); | |
| 41 | check.checkExact("+ mutable-globals"); | |
| 42 | check.checkExact("+ nontrapping-fptoint"); | |
| 43 | check.checkExact("+ sign-ext"); | |
| 44 | check.checkExact("+ simd128"); | |
| 45 | check.checkExact("+ tail-call"); | |
| 45 | 46 | |
| 46 | 47 | const test_step = b.step("test", "Run linker test"); |
| 47 | 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 | 28 | const version_fmt = "version " ++ builtin.zig_version_string; |
| 29 | 29 | |
| 30 | 30 | const check_lib = lib.checkObject(); |
| 31 | check_lib.checkStart("name producers"); | |
| 32 | check_lib.checkNext("fields 2"); | |
| 33 | check_lib.checkNext("field_name language"); | |
| 34 | check_lib.checkNext("values 1"); | |
| 35 | check_lib.checkNext("value_name Zig"); | |
| 36 | check_lib.checkNext(version_fmt); | |
| 37 | check_lib.checkNext("field_name processed-by"); | |
| 38 | check_lib.checkNext("values 1"); | |
| 39 | check_lib.checkNext("value_name Zig"); | |
| 40 | check_lib.checkNext(version_fmt); | |
| 31 | check_lib.checkStart(); | |
| 32 | check_lib.checkExact("name producers"); | |
| 33 | check_lib.checkExact("fields 2"); | |
| 34 | check_lib.checkExact("field_name language"); | |
| 35 | check_lib.checkExact("values 1"); | |
| 36 | check_lib.checkExact("value_name Zig"); | |
| 37 | check_lib.checkExact(version_fmt); | |
| 38 | check_lib.checkExact("field_name processed-by"); | |
| 39 | check_lib.checkExact("values 1"); | |
| 40 | check_lib.checkExact("value_name Zig"); | |
| 41 | check_lib.checkExact(version_fmt); | |
| 41 | 42 | |
| 42 | 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 | 25 | b.installArtifact(lib); |
| 26 | 26 | |
| 27 | 27 | const check_lib = lib.checkObject(); |
| 28 | check_lib.checkStart("Section data"); | |
| 29 | check_lib.checkNext("entries 2"); // rodata & data, no bss because we're exporting memory | |
| 28 | check_lib.checkStart(); | |
| 29 | check_lib.checkExact("Section data"); | |
| 30 | check_lib.checkExact("entries 2"); // rodata & data, no bss because we're exporting memory | |
| 30 | 31 | |
| 31 | check_lib.checkStart("Section custom"); | |
| 32 | check_lib.checkStart("name name"); // names custom section | |
| 33 | check_lib.checkStart("type data_segment"); | |
| 34 | check_lib.checkNext("names 2"); | |
| 35 | check_lib.checkNext("index 0"); | |
| 36 | check_lib.checkNext("name .rodata"); | |
| 37 | check_lib.checkNext("index 1"); | |
| 38 | check_lib.checkNext("name .data"); | |
| 32 | check_lib.checkStart(); | |
| 33 | check_lib.checkExact("Section custom"); | |
| 34 | check_lib.checkStart(); | |
| 35 | check_lib.checkExact("name name"); // names custom section | |
| 36 | check_lib.checkStart(); | |
| 37 | check_lib.checkExact("type data_segment"); | |
| 38 | check_lib.checkExact("names 2"); | |
| 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 | 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 | 28 | const check_lib = lib.checkObject(); |
| 29 | 29 | |
| 30 | 30 | // ensure global exists and its initial value is equal to explitic stack size |
| 31 | check_lib.checkStart("Section global"); | |
| 32 | check_lib.checkNext("entries 1"); | |
| 33 | check_lib.checkNext("type i32"); // on wasm32 the stack pointer must be i32 | |
| 34 | check_lib.checkNext("mutable true"); // must be able to mutate the stack pointer | |
| 35 | check_lib.checkNext("i32.const {stack_pointer}"); | |
| 31 | check_lib.checkStart(); | |
| 32 | check_lib.checkExact("Section global"); | |
| 33 | check_lib.checkExact("entries 1"); | |
| 34 | check_lib.checkExact("type i32"); // on wasm32 the stack pointer must be i32 | |
| 35 | check_lib.checkExact("mutable true"); // must be able to mutate the stack pointer | |
| 36 | check_lib.checkExtract("i32.const {stack_pointer}"); | |
| 36 | 37 | check_lib.checkComputeCompare("stack_pointer", .{ .op = .eq, .value = .{ .literal = lib.stack_size.? } }); |
| 37 | 38 | |
| 38 | 39 | // validate memory section starts after virtual stack |
| 39 | check_lib.checkNext("Section data"); | |
| 40 | check_lib.checkNext("i32.const {data_start}"); | |
| 40 | check_lib.checkStart(); | |
| 41 | check_lib.checkExact("Section data"); | |
| 42 | check_lib.checkExtract("i32.const {data_start}"); | |
| 41 | 43 | check_lib.checkComputeCompare("data_start", .{ .op = .eq, .value = .{ .variable = "stack_pointer" } }); |
| 42 | 44 | |
| 43 | 45 | // validate the name of the stack pointer |
| 44 | check_lib.checkStart("Section custom"); | |
| 45 | check_lib.checkNext("type global"); | |
| 46 | check_lib.checkNext("names 1"); | |
| 47 | check_lib.checkNext("index 0"); | |
| 48 | check_lib.checkNext("name __stack_pointer"); | |
| 46 | check_lib.checkStart(); | |
| 47 | check_lib.checkExact("Section custom"); | |
| 48 | check_lib.checkExact("type global"); | |
| 49 | check_lib.checkExact("names 1"); | |
| 50 | check_lib.checkExact("index 0"); | |
| 51 | check_lib.checkExact("name __stack_pointer"); | |
| 49 | 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 | 25 | b.installArtifact(lib); |
| 26 | 26 | |
| 27 | 27 | const check_lib = lib.checkObject(); |
| 28 | check_lib.checkStart("Section type"); | |
| 28 | check_lib.checkStart(); | |
| 29 | check_lib.checkExact("Section type"); | |
| 29 | 30 | // only 2 entries, although we have more functions. |
| 30 | 31 | // This is to test functions with the same function signature |
| 31 | 32 | // have their types deduplicated. |
| 32 | check_lib.checkNext("entries 2"); | |
| 33 | check_lib.checkNext("params 1"); | |
| 34 | check_lib.checkNext("type i32"); | |
| 35 | check_lib.checkNext("returns 1"); | |
| 36 | check_lib.checkNext("type i64"); | |
| 37 | check_lib.checkNext("params 0"); | |
| 38 | check_lib.checkNext("returns 0"); | |
| 33 | check_lib.checkExact("entries 2"); | |
| 34 | check_lib.checkExact("params 1"); | |
| 35 | check_lib.checkExact("type i32"); | |
| 36 | check_lib.checkExact("returns 1"); | |
| 37 | check_lib.checkExact("type i64"); | |
| 38 | check_lib.checkExact("params 0"); | |
| 39 | check_lib.checkExact("returns 0"); | |
| 39 | 40 | |
| 40 | 41 | test_step.dependOn(&check_lib.step); |
| 41 | 42 | } |