| ... | @@ -845,12 +845,21 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void { | ... | @@ -845,12 +845,21 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void { |
| 845 | try self.local_symbols.ensureCapacity(self.base.allocator, self.local_symbols.items.len + 1); | 845 | try self.local_symbols.ensureCapacity(self.base.allocator, self.local_symbols.items.len + 1); |
| 846 | try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1); | 846 | try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1); |
| 847 | | 847 | |
| 848 | log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name }); | 848 | if (self.local_symbol_free_list.popOrNull()) |i| { |
| 849 | decl.link.macho.local_sym_index = @intCast(u32, self.local_symbols.items.len); | 849 | log.debug("reusing symbol index {} for {}\n", .{i, decl.name}); |
| 850 | _ = self.local_symbols.addOneAssumeCapacity(); | 850 | decl.link.macho.local_sym_index = i; |
| | 851 | } else { |
| | 852 | log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name }); |
| | 853 | decl.link.macho.local_sym_index = @intCast(u32, self.local_symbols.items.len); |
| | 854 | _ = self.local_symbols.addOneAssumeCapacity(); |
| | 855 | } |
| 851 | | 856 | |
| 852 | decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len); | 857 | if (self.offset_table_free_list.popOrNull()) |i| { |
| 853 | _ = self.offset_table.addOneAssumeCapacity(); | 858 | decl.link.macho.offset_table_index = i; |
| | 859 | } else { |
| | 860 | decl.link.macho.offset_table_index = @intCast(u32, self.offset_table.items.len); |
| | 861 | _ = self.offset_table.addOneAssumeCapacity(); |
| | 862 | } |
| 854 | | 863 | |
| 855 | self.local_symbols.items[decl.link.macho.local_sym_index] = .{ | 864 | self.local_symbols.items[decl.link.macho.local_sym_index] = .{ |
| 856 | .n_strx = 0, | 865 | .n_strx = 0, |
| ... | @@ -1401,12 +1410,11 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, | ... | @@ -1401,12 +1410,11 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, |
| 1401 | } | 1410 | } |
| 1402 | else if (self.last_text_block) |last| { | 1411 | else if (self.last_text_block) |last| { |
| 1403 | const last_symbol = self.local_symbols.items[last.local_sym_index]; | 1412 | const last_symbol = self.local_symbols.items[last.local_sym_index]; |
| 1404 | // TODO pad out with NOPs and reenable | 1413 | // TODO We should pad out the excess capacity with NOPs. For executables, |
| 1405 | // const ideal_capacity = last.size * alloc_num / alloc_den; | 1414 | // no padding seems to be OK, but it will probably not be for objects. |
| 1406 | // const ideal_capacity_end_addr = last_symbol.n_value + ideal_capacity; | 1415 | const ideal_capacity = last.size * alloc_num / alloc_den; |
| 1407 | // const new_start_addr = mem.alignForwardGeneric(u64, ideal_capacity_end_addr, alignment); | 1416 | const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity; |
| 1408 | const end_vaddr = last_symbol.n_value + last.size; | 1417 | const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment); |
| 1409 | const new_start_vaddr = mem.alignForwardGeneric(u64, end_vaddr, alignment); | | |
| 1410 | block_placement = last; | 1418 | block_placement = last; |
| 1411 | break :blk new_start_vaddr; | 1419 | break :blk new_start_vaddr; |
| 1412 | } else { | 1420 | } else { |
| ... | @@ -1421,7 +1429,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, | ... | @@ -1421,7 +1429,7 @@ fn allocateTextBlock(self: *MachO, text_block: *TextBlock, new_block_size: u64, |
| 1421 | assert(needed_size <= text_capacity); // TODO must move the entire text section. | 1429 | assert(needed_size <= text_capacity); // TODO must move the entire text section. |
| 1422 | | 1430 | |
| 1423 | self.last_text_block = text_block; | 1431 | self.last_text_block = text_block; |
| 1424 | text_section.size = needed_size; // TODO temp until we pad out with NOPs | 1432 | text_section.size = needed_size; |
| 1425 | | 1433 | |
| 1426 | self.cmd_table_dirty = true; | 1434 | self.cmd_table_dirty = true; |
| 1427 | } | 1435 | } |