| ... | @@ -168,8 +168,7 @@ strtab_needs_relocation: bool = false, | ... | @@ -168,8 +168,7 @@ strtab_needs_relocation: bool = false, |
| 168 | has_dices: bool = false, | 168 | has_dices: bool = false, |
| 169 | has_stabs: bool = false, | 169 | has_stabs: bool = false, |
| 170 | | 170 | |
| 171 | section_ordinals: std.ArrayListUnmanaged(MatchingSection) = .{}, | 171 | section_ordinals: std.AutoArrayHashMapUnmanaged(MatchingSection, void) = .{}, |
| 172 | section_to_ordinal: std.AutoHashMapUnmanaged(MatchingSection, u8) = .{}, | | |
| 173 | | 172 | |
| 174 | pending_updates: std.ArrayListUnmanaged(struct { | 173 | pending_updates: std.ArrayListUnmanaged(struct { |
| 175 | kind: enum { | 174 | kind: enum { |
| ... | @@ -940,13 +939,6 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { | ... | @@ -940,13 +939,6 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { |
| 940 | }); | 939 | }); |
| 941 | try self.strtab.append(self.base.allocator, 0); | 940 | try self.strtab.append(self.base.allocator, 0); |
| 942 | | 941 | |
| 943 | // Initialize section ordinals with null ordinal pointing at | | |
| 944 | // PAGEZERO segment. | | |
| 945 | try self.section_ordinals.append(self.base.allocator, .{ | | |
| 946 | .seg = 0, | | |
| 947 | .sect = 0, | | |
| 948 | }); | | |
| 949 | | | |
| 950 | try self.populateMetadata(); | 942 | try self.populateMetadata(); |
| 951 | try self.parseInputFiles(positionals.items, self.base.options.sysroot); | 943 | try self.parseInputFiles(positionals.items, self.base.options.sysroot); |
| 952 | try self.parseLibs(libs.items, self.base.options.sysroot); | 944 | try self.parseLibs(libs.items, self.base.options.sysroot); |
| ... | @@ -1454,7 +1446,7 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio | ... | @@ -1454,7 +1446,7 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio |
| 1454 | }; | 1446 | }; |
| 1455 | | 1447 | |
| 1456 | if (res) |match| { | 1448 | if (res) |match| { |
| 1457 | try self.createSectionOrdinal(match); | 1449 | _ = try self.section_ordinals.getOrPut(self.base.allocator, match); |
| 1458 | } | 1450 | } |
| 1459 | | 1451 | |
| 1460 | return res; | 1452 | return res; |
| ... | @@ -1586,32 +1578,29 @@ fn sortSections(self: *MachO) !void { | ... | @@ -1586,32 +1578,29 @@ fn sortSections(self: *MachO) !void { |
| 1586 | { | 1578 | { |
| 1587 | // Create new section ordinals. | 1579 | // Create new section ordinals. |
| 1588 | self.section_ordinals.clearRetainingCapacity(); | 1580 | self.section_ordinals.clearRetainingCapacity(); |
| 1589 | self.section_to_ordinal.clearRetainingCapacity(); | | |
| 1590 | // First ordinal is always null | | |
| 1591 | self.section_ordinals.appendAssumeCapacity(.{ | | |
| 1592 | .seg = 0, | | |
| 1593 | .sect = 0, | | |
| 1594 | }); | | |
| 1595 | const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; | 1581 | const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 1596 | for (text_seg.sections.items) |_, sect_id| { | 1582 | for (text_seg.sections.items) |_, sect_id| { |
| 1597 | try self.createSectionOrdinal(.{ | 1583 | const res = self.section_ordinals.getOrPutAssumeCapacity(.{ |
| 1598 | .seg = self.text_segment_cmd_index.?, | 1584 | .seg = self.text_segment_cmd_index.?, |
| 1599 | .sect = @intCast(u16, sect_id), | 1585 | .sect = @intCast(u16, sect_id), |
| 1600 | }); | 1586 | }); |
| | 1587 | assert(!res.found_existing); |
| 1601 | } | 1588 | } |
| 1602 | const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; | 1589 | const data_const_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; |
| 1603 | for (data_const_seg.sections.items) |_, sect_id| { | 1590 | for (data_const_seg.sections.items) |_, sect_id| { |
| 1604 | try self.createSectionOrdinal(.{ | 1591 | const res = self.section_ordinals.getOrPutAssumeCapacity(.{ |
| 1605 | .seg = self.data_const_segment_cmd_index.?, | 1592 | .seg = self.data_const_segment_cmd_index.?, |
| 1606 | .sect = @intCast(u16, sect_id), | 1593 | .sect = @intCast(u16, sect_id), |
| 1607 | }); | 1594 | }); |
| | 1595 | assert(!res.found_existing); |
| 1608 | } | 1596 | } |
| 1609 | const data_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; | 1597 | const data_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1610 | for (data_seg.sections.items) |_, sect_id| { | 1598 | for (data_seg.sections.items) |_, sect_id| { |
| 1611 | try self.createSectionOrdinal(.{ | 1599 | const res = self.section_ordinals.getOrPutAssumeCapacity(.{ |
| 1612 | .seg = self.data_segment_cmd_index.?, | 1600 | .seg = self.data_segment_cmd_index.?, |
| 1613 | .sect = @intCast(u16, sect_id), | 1601 | .sect = @intCast(u16, sect_id), |
| 1614 | }); | 1602 | }); |
| | 1603 | assert(!res.found_existing); |
| 1615 | } | 1604 | } |
| 1616 | } | 1605 | } |
| 1617 | } | 1606 | } |
| ... | @@ -1740,7 +1729,7 @@ fn allocateTextBlocks(self: *MachO) !void { | ... | @@ -1740,7 +1729,7 @@ fn allocateTextBlocks(self: *MachO) !void { |
| 1740 | const sect = seg.sections.items[match.sect]; | 1729 | const sect = seg.sections.items[match.sect]; |
| 1741 | | 1730 | |
| 1742 | var base_addr: u64 = sect.addr; | 1731 | var base_addr: u64 = sect.addr; |
| 1743 | const n_sect = self.section_to_ordinal.get(match) orelse unreachable; | 1732 | const n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1); |
| 1744 | | 1733 | |
| 1745 | log.debug(" within section {s},{s}", .{ commands.segmentName(sect), commands.sectionName(sect) }); | 1734 | log.debug(" within section {s},{s}", .{ commands.segmentName(sect), commands.sectionName(sect) }); |
| 1746 | log.debug(" {}", .{sect}); | 1735 | log.debug(" {}", .{sect}); |
| ... | @@ -2262,7 +2251,7 @@ fn resolveSymbols(self: *MachO) !void { | ... | @@ -2262,7 +2251,7 @@ fn resolveSymbols(self: *MachO) !void { |
| 2262 | .sect = self.common_section_index.?, | 2251 | .sect = self.common_section_index.?, |
| 2263 | }; | 2252 | }; |
| 2264 | }; | 2253 | }; |
| 2265 | try self.createSectionOrdinal(match); | 2254 | _ = try self.section_ordinals.getOrPut(self.base.allocator, match); |
| 2266 | | 2255 | |
| 2267 | const size = sym.n_value; | 2256 | const size = sym.n_value; |
| 2268 | const code = try self.base.allocator.alloc(u8, size); | 2257 | const code = try self.base.allocator.alloc(u8, size); |
| ... | @@ -2275,7 +2264,7 @@ fn resolveSymbols(self: *MachO) !void { | ... | @@ -2275,7 +2264,7 @@ fn resolveSymbols(self: *MachO) !void { |
| 2275 | var nlist = macho.nlist_64{ | 2264 | var nlist = macho.nlist_64{ |
| 2276 | .n_strx = sym.n_strx, | 2265 | .n_strx = sym.n_strx, |
| 2277 | .n_type = macho.N_SECT, | 2266 | .n_type = macho.N_SECT, |
| 2278 | .n_sect = self.section_to_ordinal.get(match) orelse unreachable, | 2267 | .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1), |
| 2279 | .n_desc = 0, | 2268 | .n_desc = 0, |
| 2280 | .n_value = 0, | 2269 | .n_value = 0, |
| 2281 | }; | 2270 | }; |
| ... | @@ -2391,7 +2380,7 @@ fn resolveSymbols(self: *MachO) !void { | ... | @@ -2391,7 +2380,7 @@ fn resolveSymbols(self: *MachO) !void { |
| 2391 | var nlist = macho.nlist_64{ | 2380 | var nlist = macho.nlist_64{ |
| 2392 | .n_strx = undef.n_strx, | 2381 | .n_strx = undef.n_strx, |
| 2393 | .n_type = macho.N_SECT, | 2382 | .n_type = macho.N_SECT, |
| 2394 | .n_sect = self.section_to_ordinal.get(match) orelse unreachable, | 2383 | .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1), |
| 2395 | .n_desc = 0, | 2384 | .n_desc = 0, |
| 2396 | .n_value = 0, | 2385 | .n_value = 0, |
| 2397 | }; | 2386 | }; |
| ... | @@ -2487,7 +2476,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2487,7 +2476,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2487 | .@"align" = alignment, | 2476 | .@"align" = alignment, |
| 2488 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | 2477 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| 2489 | }); | 2478 | }); |
| 2490 | try self.createSectionOrdinal(.{ | 2479 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2491 | .seg = self.text_segment_cmd_index.?, | 2480 | .seg = self.text_segment_cmd_index.?, |
| 2492 | .sect = self.text_section_index.?, | 2481 | .sect = self.text_section_index.?, |
| 2493 | }); | 2482 | }); |
| ... | @@ -2511,7 +2500,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2511,7 +2500,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2511 | .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | 2500 | .flags = macho.S_SYMBOL_STUBS | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| 2512 | .reserved2 = stub_size, | 2501 | .reserved2 = stub_size, |
| 2513 | }); | 2502 | }); |
| 2514 | try self.createSectionOrdinal(.{ | 2503 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2515 | .seg = self.text_segment_cmd_index.?, | 2504 | .seg = self.text_segment_cmd_index.?, |
| 2516 | .sect = self.stubs_section_index.?, | 2505 | .sect = self.stubs_section_index.?, |
| 2517 | }); | 2506 | }); |
| ... | @@ -2535,7 +2524,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2535,7 +2524,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2535 | .@"align" = alignment, | 2524 | .@"align" = alignment, |
| 2536 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | 2525 | .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, |
| 2537 | }); | 2526 | }); |
| 2538 | try self.createSectionOrdinal(.{ | 2527 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2539 | .seg = self.text_segment_cmd_index.?, | 2528 | .seg = self.text_segment_cmd_index.?, |
| 2540 | .sect = self.stub_helper_section_index.?, | 2529 | .sect = self.stub_helper_section_index.?, |
| 2541 | }); | 2530 | }); |
| ... | @@ -2558,7 +2547,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2558,7 +2547,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2558 | .@"align" = 3, // 2^3 = @sizeOf(u64) | 2547 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 2559 | .flags = macho.S_NON_LAZY_SYMBOL_POINTERS, | 2548 | .flags = macho.S_NON_LAZY_SYMBOL_POINTERS, |
| 2560 | }); | 2549 | }); |
| 2561 | try self.createSectionOrdinal(.{ | 2550 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2562 | .seg = self.data_const_segment_cmd_index.?, | 2551 | .seg = self.data_const_segment_cmd_index.?, |
| 2563 | .sect = self.got_section_index.?, | 2552 | .sect = self.got_section_index.?, |
| 2564 | }); | 2553 | }); |
| ... | @@ -2581,7 +2570,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2581,7 +2570,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2581 | .@"align" = 3, // 2^3 = @sizeOf(u64) | 2570 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 2582 | .flags = macho.S_LAZY_SYMBOL_POINTERS, | 2571 | .flags = macho.S_LAZY_SYMBOL_POINTERS, |
| 2583 | }); | 2572 | }); |
| 2584 | try self.createSectionOrdinal(.{ | 2573 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2585 | .seg = self.data_segment_cmd_index.?, | 2574 | .seg = self.data_segment_cmd_index.?, |
| 2586 | .sect = self.la_symbol_ptr_section_index.?, | 2575 | .sect = self.la_symbol_ptr_section_index.?, |
| 2587 | }); | 2576 | }); |
| ... | @@ -2593,7 +2582,7 @@ fn populateMetadata(self: *MachO) !void { | ... | @@ -2593,7 +2582,7 @@ fn populateMetadata(self: *MachO) !void { |
| 2593 | try data_seg.addSection(self.base.allocator, "__data", .{ | 2582 | try data_seg.addSection(self.base.allocator, "__data", .{ |
| 2594 | .@"align" = 3, // 2^3 = @sizeOf(u64) | 2583 | .@"align" = 3, // 2^3 = @sizeOf(u64) |
| 2595 | }); | 2584 | }); |
| 2596 | try self.createSectionOrdinal(.{ | 2585 | _ = try self.section_ordinals.getOrPut(self.base.allocator, .{ |
| 2597 | .seg = self.data_segment_cmd_index.?, | 2586 | .seg = self.data_segment_cmd_index.?, |
| 2598 | .sect = self.data_section_index.?, | 2587 | .sect = self.data_section_index.?, |
| 2599 | }); | 2588 | }); |
| ... | @@ -3324,7 +3313,6 @@ pub fn deinit(self: *MachO) void { | ... | @@ -3324,7 +3313,6 @@ pub fn deinit(self: *MachO) void { |
| 3324 | } | 3313 | } |
| 3325 | | 3314 | |
| 3326 | self.section_ordinals.deinit(self.base.allocator); | 3315 | self.section_ordinals.deinit(self.base.allocator); |
| 3327 | self.section_to_ordinal.deinit(self.base.allocator); | | |
| 3328 | self.pending_updates.deinit(self.base.allocator); | 3316 | self.pending_updates.deinit(self.base.allocator); |
| 3329 | self.got_entries.deinit(self.base.allocator); | 3317 | self.got_entries.deinit(self.base.allocator); |
| 3330 | self.got_entries_map.deinit(self.base.allocator); | 3318 | self.got_entries_map.deinit(self.base.allocator); |
| ... | @@ -5882,13 +5870,6 @@ pub fn findFirst(comptime T: type, haystack: []T, start: usize, predicate: anyty | ... | @@ -5882,13 +5870,6 @@ pub fn findFirst(comptime T: type, haystack: []T, start: usize, predicate: anyty |
| 5882 | return i; | 5870 | return i; |
| 5883 | } | 5871 | } |
| 5884 | | 5872 | |
| 5885 | fn createSectionOrdinal(self: *MachO, match: MatchingSection) !void { | | |
| 5886 | if (self.section_to_ordinal.contains(match)) return; | | |
| 5887 | const ordinal = @intCast(u8, self.section_ordinals.items.len); | | |
| 5888 | try self.section_ordinals.append(self.base.allocator, match); | | |
| 5889 | try self.section_to_ordinal.putNoClobber(self.base.allocator, match, ordinal); | | |
| 5890 | } | | |
| 5891 | | | |
| 5892 | fn printSymtabAndTextBlock(self: *MachO) void { | 5873 | fn printSymtabAndTextBlock(self: *MachO) void { |
| 5893 | log.debug("locals", .{}); | 5874 | log.debug("locals", .{}); |
| 5894 | for (self.locals.items) |sym, id| { | 5875 | for (self.locals.items) |sym, id| { |