| ... | ... | @@ -276,11 +276,11 @@ const NlistWithIndex = struct { |
| 276 | 276 | nlist: macho.nlist_64, |
| 277 | 277 | index: u32, |
| 278 | 278 | |
| 279 | | pub fn cmp(_: void, lhs: @This(), rhs: @This()) bool { |
| 279 | fn lessThan(_: void, lhs: @This(), rhs: @This()) bool { |
| 280 | 280 | return lhs.nlist.n_value < rhs.nlist.n_value; |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | | fn filterNlistsInSection(symbols: []@This(), sect_id: u8) []@This() { |
| 283 | fn filterInSection(symbols: []@This(), sect_id: u8) []@This() { |
| 284 | 284 | var start: usize = 0; |
| 285 | 285 | var end: usize = symbols.len; |
| 286 | 286 | |
| ... | ... | @@ -327,19 +327,111 @@ fn filterRelocs(relocs: []macho.relocation_info, start: u64, end: u64) []macho.r |
| 327 | 327 | return relocs[start_id..end_id]; |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | | const SeniorityContext = struct { |
| 330 | const TextBlockParser = struct { |
| 331 | allocator: *Allocator, |
| 332 | section: macho.section_64, |
| 333 | code: []u8, |
| 334 | object: *Object, |
| 331 | 335 | zld: *Zld, |
| 332 | | }; |
| 333 | | fn cmpSymBySeniority(context: SeniorityContext, lhs: u32, rhs: u32) bool { |
| 334 | | const lreg = context.zld.locals.items[lhs].payload.regular; |
| 335 | | const rreg = context.zld.locals.items[rhs].payload.regular; |
| 336 | | |
| 337 | | return switch (rreg.linkage) { |
| 338 | | .global => true, |
| 339 | | .linkage_unit => lreg.linkage == .translation_unit, |
| 340 | | else => false, |
| 336 | nlists: []NlistWithIndex, |
| 337 | index: u32 = 0, |
| 338 | |
| 339 | fn peek(self: *TextBlockParser) ?NlistWithIndex { |
| 340 | return if (self.index + 1 < self.nlists.len) self.nlists[self.index + 1] else null; |
| 341 | } |
| 342 | |
| 343 | const SeniorityContext = struct { |
| 344 | zld: *Zld, |
| 341 | 345 | }; |
| 342 | | } |
| 346 | |
| 347 | fn lessThanBySeniority(context: SeniorityContext, lhs: NlistWithIndex, rhs: NlistWithIndex) bool { |
| 348 | const lreg = context.zld.locals.items[lhs.index].payload.regular; |
| 349 | const rreg = context.zld.locals.items[rhs.index].payload.regular; |
| 350 | |
| 351 | return switch (rreg.linkage) { |
| 352 | .global => true, |
| 353 | .linkage_unit => lreg.linkage == .translation_unit, |
| 354 | else => false, |
| 355 | }; |
| 356 | } |
| 357 | |
| 358 | pub fn next(self: *TextBlockParser) !?*TextBlock { |
| 359 | if (self.index == self.nlists.len) return null; |
| 360 | |
| 361 | var aliases = std.ArrayList(NlistWithIndex).init(self.allocator); |
| 362 | defer aliases.deinit(); |
| 363 | |
| 364 | const next_nlist: ?NlistWithIndex = blk: while (true) { |
| 365 | const curr_nlist = self.nlists[self.index]; |
| 366 | try aliases.append(curr_nlist); |
| 367 | |
| 368 | if (self.peek()) |next_nlist| { |
| 369 | if (curr_nlist.nlist.n_value == next_nlist.nlist.n_value) { |
| 370 | self.index += 1; |
| 371 | continue; |
| 372 | } |
| 373 | break :blk next_nlist; |
| 374 | } |
| 375 | break :blk null; |
| 376 | } else null; |
| 377 | |
| 378 | for (aliases.items) |*nlist_with_index| { |
| 379 | const sym = self.object.symbols.items[nlist_with_index.index]; |
| 380 | if (sym.payload != .regular) { |
| 381 | log.err("expected a regular symbol, found {s}", .{sym.payload}); |
| 382 | log.err(" when remapping {s}", .{sym.name}); |
| 383 | return error.SymbolIsNotRegular; |
| 384 | } |
| 385 | assert(sym.payload.regular.local_sym_index != 0); // This means the symbol has not been properly resolved. |
| 386 | nlist_with_index.index = sym.payload.regular.local_sym_index; |
| 387 | } |
| 388 | |
| 389 | if (aliases.items.len > 1) { |
| 390 | // Bubble-up senior symbol as the main link to the text block. |
| 391 | std.sort.sort( |
| 392 | NlistWithIndex, |
| 393 | aliases.items, |
| 394 | SeniorityContext{ .zld = self.zld }, |
| 395 | @This().lessThanBySeniority, |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | const senior_nlist = aliases.pop(); |
| 400 | const senior_sym = self.zld.locals.items[senior_nlist.index]; |
| 401 | assert(senior_sym.payload == .regular); |
| 402 | |
| 403 | const start_addr = senior_nlist.nlist.n_value - self.section.addr; |
| 404 | const end_addr = if (next_nlist) |n| n.nlist.n_value - self.section.addr else self.section.size; |
| 405 | |
| 406 | const code = self.code[start_addr..end_addr]; |
| 407 | const size = code.len; |
| 408 | |
| 409 | const alias_only_indices = if (aliases.items.len > 0) blk: { |
| 410 | var out = std.ArrayList(u32).init(self.allocator); |
| 411 | try out.ensureTotalCapacity(aliases.items.len); |
| 412 | for (aliases.items) |alias| { |
| 413 | out.appendAssumeCapacity(alias.index); |
| 414 | } |
| 415 | break :blk out.toOwnedSlice(); |
| 416 | } else null; |
| 417 | |
| 418 | const block = try self.allocator.create(TextBlock); |
| 419 | errdefer self.allocator.destroy(block); |
| 420 | |
| 421 | block.* = .{ |
| 422 | .local_sym_index = senior_nlist.index, |
| 423 | .aliases = alias_only_indices, |
| 424 | .code = code, |
| 425 | .size = size, |
| 426 | .alignment = self.section.@"align", |
| 427 | }; |
| 428 | |
| 429 | self.index += 1; |
| 430 | block.print_this(self.zld); |
| 431 | |
| 432 | return block; |
| 433 | } |
| 434 | }; |
| 343 | 435 | |
| 344 | 436 | pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 345 | 437 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; |
| ... | ... | @@ -361,7 +453,7 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 361 | 453 | }); |
| 362 | 454 | } |
| 363 | 455 | |
| 364 | | std.sort.sort(NlistWithIndex, sorted_nlists.items, {}, NlistWithIndex.cmp); |
| 456 | std.sort.sort(NlistWithIndex, sorted_nlists.items, {}, NlistWithIndex.lessThan); |
| 365 | 457 | |
| 366 | 458 | var last_block: ?*TextBlock = null; |
| 367 | 459 | |
| ... | ... | @@ -385,53 +477,26 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 385 | 477 | // Is there any padding between symbols within the section? |
| 386 | 478 | const is_padded = self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0; |
| 387 | 479 | |
| 388 | | // Section alignment will be the assumed alignment per symbol. |
| 389 | | const alignment = sect.@"align"; |
| 390 | | |
| 391 | 480 | next: { |
| 392 | 481 | if (is_padded) blocks: { |
| 393 | | const filtered_nlists = NlistWithIndex.filterNlistsInSection( |
| 482 | const filtered_nlists = NlistWithIndex.filterInSection( |
| 394 | 483 | sorted_nlists.items, |
| 395 | 484 | @intCast(u8, sect_id + 1), |
| 396 | 485 | ); |
| 397 | 486 | |
| 398 | 487 | if (filtered_nlists.len == 0) break :blocks; |
| 399 | 488 | |
| 400 | | var nlist_indices = std.ArrayList(u32).init(self.allocator); |
| 401 | | defer nlist_indices.deinit(); |
| 402 | | |
| 403 | | var i: u32 = 0; |
| 404 | | while (i < filtered_nlists.len) : (i += 1) { |
| 405 | | const curr = filtered_nlists[i]; |
| 406 | | try nlist_indices.append(curr.index); |
| 407 | | |
| 408 | | const next: ?NlistWithIndex = if (i + 1 < filtered_nlists.len) |
| 409 | | filtered_nlists[i + 1] |
| 410 | | else |
| 411 | | null; |
| 412 | | |
| 413 | | if (next) |n| { |
| 414 | | if (curr.nlist.n_value == n.nlist.n_value) { |
| 415 | | continue; |
| 416 | | } |
| 417 | | } |
| 418 | | |
| 419 | | // Bubble-up senior symbol as the main link to the text block. |
| 420 | | for (nlist_indices.items) |*index| { |
| 421 | | const sym = self.symbols.items[index.*]; |
| 422 | | if (sym.payload != .regular) { |
| 423 | | log.err("expected a regular symbol, found {s}", .{sym.payload}); |
| 424 | | log.err(" when remapping {s}", .{sym.name}); |
| 425 | | return error.SymbolIsNotRegular; |
| 426 | | } |
| 427 | | assert(sym.payload.regular.local_sym_index != 0); // This means the symbol has not been properly resolved. |
| 428 | | index.* = sym.payload.regular.local_sym_index; |
| 429 | | } |
| 430 | | |
| 431 | | std.sort.sort(u32, nlist_indices.items, SeniorityContext{ .zld = zld }, cmpSymBySeniority); |
| 432 | | |
| 433 | | const local_sym_index = nlist_indices.pop(); |
| 434 | | const sym = zld.locals.items[local_sym_index]; |
| 489 | var parser = TextBlockParser{ |
| 490 | .allocator = self.allocator, |
| 491 | .section = sect, |
| 492 | .code = code, |
| 493 | .object = self, |
| 494 | .zld = zld, |
| 495 | .nlists = filtered_nlists, |
| 496 | }; |
| 497 | |
| 498 | while (try parser.next()) |block| { |
| 499 | const sym = zld.locals.items[block.local_sym_index]; |
| 435 | 500 | if (sym.payload.regular.file) |file| { |
| 436 | 501 | if (file != self) { |
| 437 | 502 | log.warn("deduping definition of {s} in {s}", .{ sym.name, self.name.? }); |
| ... | ... | @@ -439,27 +504,8 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 439 | 504 | } |
| 440 | 505 | } |
| 441 | 506 | |
| 442 | | const start_addr = curr.nlist.n_value - sect.addr; |
| 443 | | const end_addr = if (next) |n| n.nlist.n_value - sect.addr else sect.size; |
| 444 | | |
| 445 | | const tb_code = code[start_addr..end_addr]; |
| 446 | | const size = tb_code.len; |
| 447 | | |
| 448 | | const block = try self.allocator.create(TextBlock); |
| 449 | | errdefer self.allocator.destroy(block); |
| 450 | | |
| 451 | | block.* = .{ |
| 452 | | .local_sym_index = local_sym_index, |
| 453 | | .aliases = std.ArrayList(u32).init(self.allocator), |
| 454 | | .references = std.ArrayList(u32).init(self.allocator), |
| 455 | | .code = tb_code, |
| 456 | | .relocs = std.ArrayList(*Relocation).init(self.allocator), |
| 457 | | .size = size, |
| 458 | | .alignment = alignment, |
| 459 | | .segment_id = match.seg, |
| 460 | | .section_id = match.sect, |
| 461 | | }; |
| 462 | | try block.aliases.appendSlice(nlist_indices.items); |
| 507 | block.segment_id = match.seg; |
| 508 | block.section_id = match.sect; |
| 463 | 509 | |
| 464 | 510 | // TODO parse relocs |
| 465 | 511 | |
| ... | ... | @@ -468,8 +514,6 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 468 | 514 | block.prev = last; |
| 469 | 515 | } |
| 470 | 516 | last_block = block; |
| 471 | | |
| 472 | | nlist_indices.clearRetainingCapacity(); |
| 473 | 517 | } |
| 474 | 518 | |
| 475 | 519 | break :next; |
| ... | ... | @@ -498,12 +542,9 @@ pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 498 | 542 | |
| 499 | 543 | block.* = .{ |
| 500 | 544 | .local_sym_index = local_sym_index, |
| 501 | | .aliases = std.ArrayList(u32).init(self.allocator), |
| 502 | | .references = std.ArrayList(u32).init(self.allocator), |
| 503 | 545 | .code = code, |
| 504 | | .relocs = std.ArrayList(*Relocation).init(self.allocator), |
| 505 | 546 | .size = sect.size, |
| 506 | | .alignment = alignment, |
| 547 | .alignment = sect.@"align", |
| 507 | 548 | .segment_id = match.seg, |
| 508 | 549 | .section_id = match.sect, |
| 509 | 550 | }; |