| ... | ... | @@ -188,9 +188,21 @@ text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{}, |
| 188 | 188 | /// Pointer to the last allocated text block |
| 189 | 189 | last_text_block: ?*TextBlock = null, |
| 190 | 190 | |
| 191 | | managed_blocks: std.ArrayListUnmanaged(TextBlock) = .{}, |
| 191 | /// List of TextBlocks that are owned directly by the linker. |
| 192 | /// Currently these are only TextBlocks that are the result of linking |
| 193 | /// object files. TextBlock which take part in incremental linking are |
| 194 | /// at present owned by Module.Decl. |
| 195 | /// TODO consolidate this. |
| 196 | managed_blocks: std.ArrayListUnmanaged(*TextBlock) = .{}, |
| 197 | |
| 192 | 198 | blocks: std.AutoHashMapUnmanaged(MatchingSection, *TextBlock) = .{}, |
| 193 | 199 | |
| 200 | /// List of Decls that are currently alive. |
| 201 | /// We store them here so that we can properly dispose of any allocated |
| 202 | /// memory within the TextBlock in the incremental linker. |
| 203 | /// TODO consolidate this. |
| 204 | decls: std.ArrayListUnmanaged(*Module.Decl) = .{}, |
| 205 | |
| 194 | 206 | /// A list of all PIE fixups required for this run of the linker. |
| 195 | 207 | /// Warning, this is currently NOT thread-safe. See the TODO below. |
| 196 | 208 | /// TODO Move this list inside `updateDecl` where it should be allocated |
| ... | ... | @@ -203,7 +215,7 @@ pie_fixups: std.ArrayListUnmanaged(PIEFixup) = .{}, |
| 203 | 215 | const StringIndexContext = struct { |
| 204 | 216 | strtab: *std.ArrayListUnmanaged(u8), |
| 205 | 217 | |
| 206 | | pub fn eql(self: StringIndexContext, a: u32, b: u32) bool { |
| 218 | pub fn eql(_: StringIndexContext, a: u32, b: u32) bool { |
| 207 | 219 | return a == b; |
| 208 | 220 | } |
| 209 | 221 | |
| ... | ... | @@ -2224,7 +2236,6 @@ fn resolveSymbols(self: *MachO) !void { |
| 2224 | 2236 | for (self.tentatives.items) |sym| { |
| 2225 | 2237 | if (symbolIsNull(sym)) continue; |
| 2226 | 2238 | |
| 2227 | | const sym_name = self.getString(sym.n_strx); |
| 2228 | 2239 | const match: MatchingSection = blk: { |
| 2229 | 2240 | if (self.common_section_index == null) { |
| 2230 | 2241 | const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| ... | ... | @@ -2263,12 +2274,13 @@ fn resolveSymbols(self: *MachO) !void { |
| 2263 | 2274 | .local_sym_index = local_sym_index, |
| 2264 | 2275 | }; |
| 2265 | 2276 | |
| 2266 | | const block = try self.managed_blocks.addOne(self.base.allocator); |
| 2277 | const block = try self.base.allocator.create(TextBlock); |
| 2267 | 2278 | block.* = TextBlock.empty; |
| 2268 | 2279 | block.local_sym_index = local_sym_index; |
| 2269 | 2280 | block.code = code; |
| 2270 | 2281 | block.size = size; |
| 2271 | 2282 | block.alignment = alignment; |
| 2283 | try self.managed_blocks.append(self.base.allocator, block); |
| 2272 | 2284 | |
| 2273 | 2285 | // Update target section's metadata |
| 2274 | 2286 | // TODO should we update segment's size here too? |
| ... | ... | @@ -2403,12 +2415,13 @@ fn resolveSymbols(self: *MachO) !void { |
| 2403 | 2415 | // We create an empty atom for this symbol. |
| 2404 | 2416 | // TODO perhaps we should special-case special symbols? Create a separate |
| 2405 | 2417 | // linked list of atoms? |
| 2406 | | const block = try self.managed_blocks.addOne(self.base.allocator); |
| 2418 | const block = try self.base.allocator.create(TextBlock); |
| 2407 | 2419 | block.* = TextBlock.empty; |
| 2408 | 2420 | block.local_sym_index = local_sym_index; |
| 2409 | 2421 | block.code = try self.base.allocator.alloc(u8, 0); |
| 2410 | 2422 | block.size = 0; |
| 2411 | 2423 | block.alignment = 0; |
| 2424 | try self.managed_blocks.append(self.base.allocator, block); |
| 2412 | 2425 | |
| 2413 | 2426 | if (self.blocks.getPtr(match)) |last| { |
| 2414 | 2427 | last.*.next = block; |
| ... | ... | @@ -3306,12 +3319,18 @@ pub fn deinit(self: *MachO) void { |
| 3306 | 3319 | } |
| 3307 | 3320 | self.load_commands.deinit(self.base.allocator); |
| 3308 | 3321 | |
| 3309 | | for (self.managed_blocks.items) |*block| { |
| 3322 | for (self.managed_blocks.items) |block| { |
| 3310 | 3323 | block.deinit(self.base.allocator); |
| 3324 | self.base.allocator.destroy(block); |
| 3311 | 3325 | } |
| 3312 | 3326 | self.managed_blocks.deinit(self.base.allocator); |
| 3313 | 3327 | self.blocks.deinit(self.base.allocator); |
| 3314 | 3328 | self.text_block_free_list.deinit(self.base.allocator); |
| 3329 | |
| 3330 | for (self.decls.items) |decl| { |
| 3331 | decl.link.macho.deinit(self.base.allocator); |
| 3332 | } |
| 3333 | self.decls.deinit(self.base.allocator); |
| 3315 | 3334 | } |
| 3316 | 3335 | |
| 3317 | 3336 | pub fn closeFiles(self: MachO) void { |
| ... | ... | @@ -3325,7 +3344,7 @@ pub fn closeFiles(self: MachO) void { |
| 3325 | 3344 | |
| 3326 | 3345 | fn freeTextBlock(self: *MachO, text_block: *TextBlock) void { |
| 3327 | 3346 | log.debug("freeTextBlock {*}", .{text_block}); |
| 3328 | | // text_block.deinit(self.base.allocator); |
| 3347 | text_block.deinit(self.base.allocator); |
| 3329 | 3348 | |
| 3330 | 3349 | var already_have_free_list_node = false; |
| 3331 | 3350 | { |
| ... | ... | @@ -3412,6 +3431,9 @@ pub fn allocateDeclIndexes(self: *MachO, decl: *Module.Decl) !void { |
| 3412 | 3431 | |
| 3413 | 3432 | try self.locals.ensureUnusedCapacity(self.base.allocator, 1); |
| 3414 | 3433 | try self.got_entries.ensureUnusedCapacity(self.base.allocator, 1); |
| 3434 | try self.decls.ensureUnusedCapacity(self.base.allocator, 1); |
| 3435 | |
| 3436 | self.decls.appendAssumeCapacity(decl); |
| 3415 | 3437 | |
| 3416 | 3438 | if (self.locals_free_list.popOrNull()) |i| { |
| 3417 | 3439 | log.debug("reusing symbol index {d} for {s}", .{ i, decl.name }); |
| ... | ... | @@ -3497,7 +3519,6 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void { |
| 3497 | 3519 | .externally_managed => |x| break :blk x, |
| 3498 | 3520 | .appended => { |
| 3499 | 3521 | decl.link.macho.code = code_buffer.toOwnedSlice(); |
| 3500 | | log.warn("WAT", .{}); |
| 3501 | 3522 | break :blk decl.link.macho.code; |
| 3502 | 3523 | }, |
| 3503 | 3524 | .fail => |em| { |