| ... | @@ -28,7 +28,6 @@ name: ?[]const u8 = null, | ... | @@ -28,7 +28,6 @@ name: ?[]const u8 = null, |
| 28 | mtime: ?u64 = null, | 28 | mtime: ?u64 = null, |
| 29 | | 29 | |
| 30 | load_commands: std.ArrayListUnmanaged(LoadCommand) = .{}, | 30 | load_commands: std.ArrayListUnmanaged(LoadCommand) = .{}, |
| 31 | sections: std.ArrayListUnmanaged(Section) = .{}, | | |
| 32 | | 31 | |
| 33 | segment_cmd_index: ?u16 = null, | 32 | segment_cmd_index: ?u16 = null, |
| 34 | symtab_cmd_index: ?u16 = null, | 33 | symtab_cmd_index: ?u16 = null, |
| ... | @@ -49,32 +48,10 @@ dwarf_debug_ranges_index: ?u16 = null, | ... | @@ -49,32 +48,10 @@ dwarf_debug_ranges_index: ?u16 = null, |
| 49 | symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{}, | 48 | symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{}, |
| 50 | strtab: std.ArrayListUnmanaged(u8) = .{}, | 49 | strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 51 | | 50 | |
| 52 | symbols: std.ArrayListUnmanaged(*Symbol) = .{}, | | |
| 53 | stabs: std.ArrayListUnmanaged(*Symbol) = .{}, | | |
| 54 | initializers: std.ArrayListUnmanaged(u32) = .{}, | 51 | initializers: std.ArrayListUnmanaged(u32) = .{}, |
| 55 | data_in_code_entries: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{}, | 52 | data_in_code_entries: std.ArrayListUnmanaged(macho.data_in_code_entry) = .{}, |
| 56 | | 53 | |
| 57 | pub const Section = struct { | 54 | symbols: std.ArrayListUnmanaged(*Symbol) = .{}, |
| 58 | inner: macho.section_64, | | |
| 59 | code: []u8, | | |
| 60 | relocs: ?[]*Relocation, | | |
| 61 | target_map: ?struct { | | |
| 62 | segment_id: u16, | | |
| 63 | section_id: u16, | | |
| 64 | offset: u32, | | |
| 65 | } = null, | | |
| 66 | | | |
| 67 | pub fn deinit(self: *Section, allocator: *Allocator) void { | | |
| 68 | allocator.free(self.code); | | |
| 69 | | | |
| 70 | if (self.relocs) |relocs| { | | |
| 71 | for (relocs) |rel| { | | |
| 72 | allocator.destroy(rel); | | |
| 73 | } | | |
| 74 | allocator.free(relocs); | | |
| 75 | } | | |
| 76 | } | | |
| 77 | }; | | |
| 78 | | 55 | |
| 79 | const DebugInfo = struct { | 56 | const DebugInfo = struct { |
| 80 | inner: dwarf.DwarfInfo, | 57 | inner: dwarf.DwarfInfo, |
| ... | @@ -177,19 +154,11 @@ pub fn deinit(self: *Object) void { | ... | @@ -177,19 +154,11 @@ pub fn deinit(self: *Object) void { |
| 177 | lc.deinit(self.allocator); | 154 | lc.deinit(self.allocator); |
| 178 | } | 155 | } |
| 179 | self.load_commands.deinit(self.allocator); | 156 | self.load_commands.deinit(self.allocator); |
| 180 | | | |
| 181 | for (self.sections.items) |*sect| { | | |
| 182 | sect.deinit(self.allocator); | | |
| 183 | } | | |
| 184 | self.sections.deinit(self.allocator); | | |
| 185 | | | |
| 186 | self.symbols.deinit(self.allocator); | | |
| 187 | self.stabs.deinit(self.allocator); | | |
| 188 | | | |
| 189 | self.data_in_code_entries.deinit(self.allocator); | 157 | self.data_in_code_entries.deinit(self.allocator); |
| 190 | self.initializers.deinit(self.allocator); | 158 | self.initializers.deinit(self.allocator); |
| 191 | self.symtab.deinit(self.allocator); | 159 | self.symtab.deinit(self.allocator); |
| 192 | self.strtab.deinit(self.allocator); | 160 | self.strtab.deinit(self.allocator); |
| | 161 | self.symbols.deinit(self.allocator); |
| 193 | | 162 | |
| 194 | if (self.name) |n| { | 163 | if (self.name) |n| { |
| 195 | self.allocator.free(n); | 164 | self.allocator.free(n); |
| ... | @@ -231,10 +200,8 @@ pub fn parse(self: *Object) !void { | ... | @@ -231,10 +200,8 @@ pub fn parse(self: *Object) !void { |
| 231 | self.header = header; | 200 | self.header = header; |
| 232 | | 201 | |
| 233 | try self.readLoadCommands(reader); | 202 | try self.readLoadCommands(reader); |
| 234 | try self.parseSections(); | | |
| 235 | try self.parseSymtab(); | 203 | try self.parseSymtab(); |
| 236 | try self.parseDataInCode(); | 204 | try self.parseDataInCode(); |
| 237 | try self.parseInitializers(); | | |
| 238 | } | 205 | } |
| 239 | | 206 | |
| 240 | pub fn readLoadCommands(self: *Object, reader: anytype) !void { | 207 | pub fn readLoadCommands(self: *Object, reader: anytype) !void { |
| ... | @@ -305,250 +272,253 @@ pub fn readLoadCommands(self: *Object, reader: anytype) !void { | ... | @@ -305,250 +272,253 @@ pub fn readLoadCommands(self: *Object, reader: anytype) !void { |
| 305 | } | 272 | } |
| 306 | } | 273 | } |
| 307 | | 274 | |
| 308 | pub fn parseSections(self: *Object) !void { | 275 | const NlistWithIndex = struct { |
| 309 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; | 276 | nlist: macho.nlist_64, |
| 310 | | 277 | index: u32, |
| 311 | log.debug("parsing sections in {s}", .{self.name.?}); | | |
| 312 | | | |
| 313 | try self.sections.ensureCapacity(self.allocator, seg.sections.items.len); | | |
| 314 | | 278 | |
| 315 | for (seg.sections.items) |sect| { | 279 | pub fn cmp(_: void, lhs: @This(), rhs: @This()) bool { |
| 316 | log.debug("parsing section '{s},{s}'", .{ segmentName(sect), sectionName(sect) }); | 280 | return lhs.nlist.n_value < rhs.nlist.n_value; |
| 317 | // Read sections' code | 281 | } |
| 318 | var code = try self.allocator.alloc(u8, @intCast(usize, sect.size)); | | |
| 319 | _ = try self.file.?.preadAll(code, sect.offset); | | |
| 320 | | | |
| 321 | var section = Section{ | | |
| 322 | .inner = sect, | | |
| 323 | .code = code, | | |
| 324 | .relocs = null, | | |
| 325 | }; | | |
| 326 | | 282 | |
| 327 | // Parse relocations | 283 | fn filterNlistsInSection(symbols: []@This(), sect_id: u8) []@This() { |
| 328 | if (sect.nreloc > 0) { | 284 | var start: usize = 0; |
| 329 | var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc); | 285 | var end: usize = symbols.len; |
| 330 | defer self.allocator.free(raw_relocs); | | |
| 331 | | 286 | |
| 332 | _ = try self.file.?.preadAll(raw_relocs, sect.reloff); | 287 | while (true) { |
| | 288 | var change = false; |
| | 289 | if (symbols[start].nlist.n_sect != sect_id) { |
| | 290 | start += 1; |
| | 291 | change = true; |
| | 292 | } |
| | 293 | if (symbols[end - 1].nlist.n_sect != sect_id) { |
| | 294 | end -= 1; |
| | 295 | change = true; |
| | 296 | } |
| 333 | | 297 | |
| 334 | section.relocs = try reloc.parse( | 298 | if (start == end) break; |
| 335 | self.allocator, | 299 | if (!change) break; |
| 336 | self.arch.?, | | |
| 337 | section.code, | | |
| 338 | mem.bytesAsSlice(macho.relocation_info, raw_relocs), | | |
| 339 | ); | | |
| 340 | } | 300 | } |
| 341 | | 301 | |
| 342 | self.sections.appendAssumeCapacity(section); | 302 | return symbols[start..end]; |
| 343 | } | 303 | } |
| 344 | } | 304 | }; |
| 345 | | | |
| 346 | pub fn parseTextBlocks(self: *Object, zld: *Zld) !*TextBlock { | | |
| 347 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; | | |
| 348 | | 305 | |
| 349 | log.warn("analysing {s}", .{self.name.?}); | 306 | fn filterRelocs(relocs: []macho.relocation_info, start: u64, end: u64) []macho.relocation_info { |
| | 307 | if (relocs.len == 0) return relocs; |
| 350 | | 308 | |
| 351 | const dysymtab = self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab; | 309 | var start_id: usize = 0; |
| | 310 | var end_id: usize = relocs.len; |
| 352 | | 311 | |
| 353 | const SymWithIndex = struct { | 312 | while (true) { |
| 354 | nlist: macho.nlist_64, | 313 | var change = false; |
| 355 | index: u32, | 314 | if (relocs[start_id].r_address > end) { |
| 356 | | 315 | start_id += 1; |
| 357 | pub fn cmp(_: void, lhs: @This(), rhs: @This()) bool { | 316 | change = true; |
| 358 | return lhs.nlist.n_value < rhs.nlist.n_value; | | |
| 359 | } | 317 | } |
| 360 | | 318 | if (relocs[end_id - 1].r_address < start) { |
| 361 | fn filterSymsInSection(symbols: []@This(), sect_id: u8) []@This() { | 319 | end_id -= 1; |
| 362 | var start: usize = 0; | 320 | change = true; |
| 363 | var end: usize = symbols.len; | | |
| 364 | | | |
| 365 | while (true) { | | |
| 366 | var change = false; | | |
| 367 | if (symbols[start].nlist.n_sect != sect_id) { | | |
| 368 | start += 1; | | |
| 369 | change = true; | | |
| 370 | } | | |
| 371 | if (symbols[end - 1].nlist.n_sect != sect_id) { | | |
| 372 | end -= 1; | | |
| 373 | change = true; | | |
| 374 | } | | |
| 375 | | | |
| 376 | if (start == end) break; | | |
| 377 | if (!change) break; | | |
| 378 | } | | |
| 379 | | | |
| 380 | return symbols[start..end]; | | |
| 381 | } | 321 | } |
| 382 | | 322 | |
| 383 | fn filterRelocs(relocs: []macho.relocation_info, start: u64, end: u64) []macho.relocation_info { | 323 | if (start_id == end_id) break; |
| 384 | if (relocs.len == 0) return relocs; | 324 | if (!change) break; |
| | 325 | } |
| 385 | | 326 | |
| 386 | var start_id: usize = 0; | 327 | return relocs[start_id..end_id]; |
| 387 | var end_id: usize = relocs.len; | 328 | } |
| 388 | | 329 | |
| 389 | while (true) { | 330 | const SeniorityContext = struct { |
| 390 | var change = false; | 331 | zld: *Zld, |
| 391 | if (relocs[start_id].r_address > end) { | 332 | }; |
| 392 | start_id += 1; | 333 | fn cmpSymBySeniority(context: SeniorityContext, lhs: u32, rhs: u32) bool { |
| 393 | change = true; | 334 | const lreg = context.zld.locals.items[lhs].payload.regular; |
| 394 | } | 335 | const rreg = context.zld.locals.items[rhs].payload.regular; |
| 395 | if (relocs[end_id - 1].r_address < start) { | 336 | |
| 396 | end_id -= 1; | 337 | return switch (rreg.linkage) { |
| 397 | change = true; | 338 | .global => true, |
| 398 | } | 339 | .linkage_unit => lreg.linkage == .translation_unit, |
| | 340 | else => false, |
| | 341 | }; |
| | 342 | } |
| 399 | | 343 | |
| 400 | if (start_id == end_id) break; | 344 | pub fn parseTextBlocks(self: *Object, zld: *Zld) !?*TextBlock { |
| 401 | if (!change) break; | 345 | const seg = self.load_commands.items[self.segment_cmd_index.?].Segment; |
| 402 | } | | |
| 403 | | 346 | |
| 404 | return relocs[start_id..end_id]; | 347 | log.warn("analysing {s}", .{self.name.?}); |
| 405 | } | | |
| 406 | }; | | |
| 407 | | 348 | |
| | 349 | const dysymtab = self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab; |
| | 350 | // We only care about defined symbols, so filter every other out. |
| 408 | const nlists = self.symtab.items[dysymtab.ilocalsym..dysymtab.iundefsym]; | 351 | const nlists = self.symtab.items[dysymtab.ilocalsym..dysymtab.iundefsym]; |
| 409 | | 352 | |
| 410 | var sorted_syms = std.ArrayList(SymWithIndex).init(self.allocator); | 353 | var sorted_nlists = std.ArrayList(NlistWithIndex).init(self.allocator); |
| 411 | defer sorted_syms.deinit(); | 354 | defer sorted_nlists.deinit(); |
| 412 | try sorted_syms.ensureTotalCapacity(nlists.len); | 355 | try sorted_nlists.ensureTotalCapacity(nlists.len); |
| 413 | | 356 | |
| 414 | for (nlists) |nlist, index| { | 357 | for (nlists) |nlist, index| { |
| 415 | sorted_syms.appendAssumeCapacity(.{ | 358 | sorted_nlists.appendAssumeCapacity(.{ |
| 416 | .nlist = nlist, | 359 | .nlist = nlist, |
| 417 | .index = @intCast(u32, index + dysymtab.ilocalsym), | 360 | .index = @intCast(u32, index + dysymtab.ilocalsym), |
| 418 | }); | 361 | }); |
| 419 | } | 362 | } |
| 420 | | 363 | |
| 421 | std.sort.sort(SymWithIndex, sorted_syms.items, {}, SymWithIndex.cmp); | 364 | std.sort.sort(NlistWithIndex, sorted_nlists.items, {}, NlistWithIndex.cmp); |
| | 365 | |
| | 366 | var last_block: ?*TextBlock = null; |
| 422 | | 367 | |
| 423 | for (seg.sections.items) |sect, sect_id| { | 368 | for (seg.sections.items) |sect, sect_id| { |
| 424 | log.warn("section {s},{s}", .{ segmentName(sect), sectionName(sect) }); | 369 | log.warn("putting section '{s},{s}' as a TextBlock", .{ |
| | 370 | segmentName(sect), |
| | 371 | sectionName(sect), |
| | 372 | }); |
| 425 | | 373 | |
| | 374 | // Get matching segment/section in the final artifact. |
| 426 | const match = (try zld.getMatchingSection(sect)) orelse { | 375 | const match = (try zld.getMatchingSection(sect)) orelse { |
| 427 | log.warn("unhandled section", .{}); | 376 | log.warn("unhandled section", .{}); |
| 428 | continue; | 377 | continue; |
| 429 | }; | 378 | }; |
| 430 | | 379 | |
| 431 | // Read code | 380 | // Read section's code |
| 432 | var code = try self.allocator.alloc(u8, @intCast(usize, sect.size)); | 381 | var code = try self.allocator.alloc(u8, @intCast(usize, sect.size)); |
| 433 | defer self.allocator.free(code); | 382 | defer self.allocator.free(code); |
| 434 | _ = try self.file.?.preadAll(code, sect.offset); | 383 | _ = try self.file.?.preadAll(code, sect.offset); |
| 435 | | 384 | |
| 436 | // Read and parse relocs | 385 | // Is there any padding between symbols within the section? |
| 437 | const raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc); | 386 | const is_padded = self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0; |
| 438 | defer self.allocator.free(raw_relocs); | | |
| 439 | _ = try self.file.?.preadAll(raw_relocs, sect.reloff); | | |
| 440 | const relocs = mem.bytesAsSlice(macho.relocation_info, raw_relocs); | | |
| 441 | | 387 | |
| | 388 | // Section alignment will be the assumed alignment per symbol. |
| 442 | const alignment = sect.@"align"; | 389 | const alignment = sect.@"align"; |
| 443 | | 390 | |
| 444 | if (self.header.?.flags & macho.MH_SUBSECTIONS_VIA_SYMBOLS != 0) { | 391 | next: { |
| 445 | const syms = SymWithIndex.filterSymsInSection(sorted_syms.items, @intCast(u8, sect_id + 1)); | 392 | if (is_padded) blocks: { |
| 446 | | 393 | const filtered_nlists = NlistWithIndex.filterNlistsInSection( |
| 447 | if (syms.len == 0) { | 394 | sorted_nlists.items, |
| 448 | // One large text block referenced by section offsets only | 395 | @intCast(u8, sect_id + 1), |
| 449 | log.warn("TextBlock", .{}); | 396 | ); |
| 450 | log.warn(" | referenced by section offsets", .{}); | 397 | |
| 451 | log.warn(" | start_addr = {}", .{sect.addr}); | 398 | if (filtered_nlists.len == 0) break :blocks; |
| 452 | log.warn(" | end_addr = {}", .{sect.size}); | | |
| 453 | log.warn(" | size = {}", .{sect.size}); | | |
| 454 | log.warn(" | alignment = 0x{x}", .{alignment}); | | |
| 455 | log.warn(" | segment_id = {}", .{match.seg}); | | |
| 456 | log.warn(" | section_id = {}", .{match.sect}); | | |
| 457 | log.warn(" | relocs: {any}", .{relocs}); | | |
| 458 | } | | |
| 459 | | 399 | |
| 460 | var indices = std.ArrayList(u32).init(self.allocator); | 400 | var nlist_indices = std.ArrayList(u32).init(self.allocator); |
| 461 | defer indices.deinit(); | 401 | defer nlist_indices.deinit(); |
| 462 | | 402 | |
| 463 | var i: u32 = 0; | 403 | var i: u32 = 0; |
| 464 | while (i < syms.len) : (i += 1) { | 404 | while (i < filtered_nlists.len) : (i += 1) { |
| 465 | const curr = syms[i]; | 405 | const curr = filtered_nlists[i]; |
| 466 | try indices.append(i); | 406 | try nlist_indices.append(curr.index); |
| 467 | | 407 | |
| 468 | const next: ?SymWithIndex = if (i + 1 < syms.len) | 408 | const next: ?NlistWithIndex = if (i + 1 < filtered_nlists.len) |
| 469 | syms[i + 1] | 409 | filtered_nlists[i + 1] |
| 470 | else | 410 | else |
| 471 | null; | 411 | null; |
| 472 | | 412 | |
| 473 | if (next) |n| { | 413 | if (next) |n| { |
| 474 | if (curr.nlist.n_value == n.nlist.n_value) { | 414 | if (curr.nlist.n_value == n.nlist.n_value) { |
| 475 | continue; | 415 | continue; |
| | 416 | } |
| 476 | } | 417 | } |
| 477 | } | | |
| 478 | | 418 | |
| 479 | const start_addr = curr.nlist.n_value - sect.addr; | 419 | // Bubble-up senior symbol as the main link to the text block. |
| 480 | const end_addr = if (next) |n| n.nlist.n_value - sect.addr else sect.size; | 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 | } |
| 481 | | 430 | |
| 482 | const tb_code = code[start_addr..end_addr]; | 431 | std.sort.sort(u32, nlist_indices.items, SeniorityContext{ .zld = zld }, cmpSymBySeniority); |
| 483 | const size = tb_code.len; | | |
| 484 | | 432 | |
| 485 | log.warn("TextBlock", .{}); | 433 | const local_sym_index = nlist_indices.pop(); |
| 486 | for (indices.items) |id| { | 434 | const sym = zld.locals.items[local_sym_index]; |
| 487 | const sym = self.symbols.items[syms[id].index]; | 435 | if (sym.payload.regular.file) |file| { |
| 488 | log.warn(" | symbol = {s}", .{sym.name}); | 436 | if (file != self) { |
| 489 | } | 437 | log.warn("deduping definition of {s} in {s}", .{ sym.name, self.name.? }); |
| 490 | log.warn(" | start_addr = {}", .{start_addr}); | 438 | continue; |
| 491 | log.warn(" | end_addr = {}", .{end_addr}); | 439 | } |
| 492 | log.warn(" | size = {}", .{size}); | 440 | } |
| 493 | log.warn(" | alignment = 0x{x}", .{alignment}); | | |
| 494 | log.warn(" | segment_id = {}", .{match.seg}); | | |
| 495 | log.warn(" | section_id = {}", .{match.sect}); | | |
| 496 | log.warn(" | relocs: {any}", .{SymWithIndex.filterRelocs(relocs, start_addr, end_addr)}); | | |
| 497 | | | |
| 498 | indices.clearRetainingCapacity(); | | |
| 499 | } | | |
| 500 | } else { | | |
| 501 | return error.TODOOneLargeTextBlock; | | |
| 502 | } | | |
| 503 | } | | |
| 504 | } | | |
| 505 | | 441 | |
| 506 | const SectionAsTextBlocksArgs = struct { | 442 | const start_addr = curr.nlist.n_value - sect.addr; |
| 507 | sect: macho.section_64, | 443 | const end_addr = if (next) |n| n.nlist.n_value - sect.addr else sect.size; |
| 508 | code: []u8, | 444 | |
| 509 | subsections_via_symbols: bool = false, | 445 | const tb_code = code[start_addr..end_addr]; |
| 510 | relocs: ?[]macho.relocation_info = null, | 446 | const size = tb_code.len; |
| 511 | segment_id: u16 = 0, | 447 | |
| 512 | section_id: u16 = 0, | 448 | const block = try self.allocator.create(TextBlock); |
| 513 | }; | 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); |
| | 463 | |
| | 464 | // TODO parse relocs |
| | 465 | |
| | 466 | if (last_block) |last| { |
| | 467 | last.next = block; |
| | 468 | block.prev = last; |
| | 469 | } |
| | 470 | last_block = block; |
| 514 | | 471 | |
| 515 | fn sectionAsTextBlocks(self: *Object, args: SectionAsTextBlocksArgs) !*TextBlock { | 472 | nlist_indices.clearRetainingCapacity(); |
| 516 | const sect = args.sect; | 473 | } |
| 517 | | 474 | |
| 518 | log.warn("putting section '{s},{s}' as a TextBlock", .{ segmentName(sect), sectionName(sect) }); | 475 | break :next; |
| | 476 | } |
| 519 | | 477 | |
| 520 | // Section alignment will be the assumed alignment per symbol. | 478 | // Since there is no symbol to refer to this block, we create |
| 521 | const alignment = sect.@"align"; | 479 | // a temp one. |
| | 480 | const name = try std.fmt.allocPrint(self.allocator, "l_{s}_{s}_{s}", .{ |
| | 481 | self.name.?, |
| | 482 | segmentName(sect), |
| | 483 | sectionName(sect), |
| | 484 | }); |
| | 485 | defer self.allocator.free(name); |
| | 486 | const symbol = try Symbol.new(self.allocator, name); |
| | 487 | symbol.payload = .{ |
| | 488 | .regular = .{ |
| | 489 | .linkage = .translation_unit, |
| | 490 | .file = self, |
| | 491 | }, |
| | 492 | }; |
| | 493 | const local_sym_index = @intCast(u32, zld.locals.items.len); |
| | 494 | try zld.locals.append(zld.allocator, symbol); |
| 522 | | 495 | |
| 523 | const first_block: *TextBlock = blk: { | | |
| 524 | if (args.subsections_via_symbols) { | | |
| 525 | return error.TODO; | | |
| 526 | } else { | | |
| 527 | const block = try self.allocator.create(TextBlock); | 496 | const block = try self.allocator.create(TextBlock); |
| 528 | errdefer self.allocator.destroy(block); | 497 | errdefer self.allocator.destroy(block); |
| 529 | | 498 | |
| 530 | block.* = .{ | 499 | block.* = .{ |
| 531 | .ref = .{ | 500 | .local_sym_index = local_sym_index, |
| 532 | .section = undefined, // Will be populated when we allocated final sections. | 501 | .aliases = std.ArrayList(u32).init(self.allocator), |
| 533 | }, | 502 | .references = std.ArrayList(u32).init(self.allocator), |
| 534 | .code = args.code, | 503 | .code = code, |
| 535 | .relocs = null, | 504 | .relocs = std.ArrayList(*Relocation).init(self.allocator), |
| 536 | .size = sect.size, | 505 | .size = sect.size, |
| 537 | .alignment = alignment, | 506 | .alignment = alignment, |
| 538 | .segment_id = args.segment_id, | 507 | .segment_id = match.seg, |
| 539 | .section_id = args.section_id, | 508 | .section_id = match.sect, |
| 540 | }; | 509 | }; |
| 541 | | 510 | |
| 542 | // TODO parse relocs | 511 | // TODO parse relocs |
| 543 | if (args.relocs) |relocs| { | | |
| 544 | block.relocs = try reloc.parse(self.allocator, self.arch.?, args.code, relocs, symbols); | | |
| 545 | } | | |
| 546 | | 512 | |
| 547 | break :blk block; | 513 | if (last_block) |last| { |
| | 514 | last.next = block; |
| | 515 | block.prev = last; |
| | 516 | } |
| | 517 | last_block = block; |
| 548 | } | 518 | } |
| 549 | }; | 519 | } |
| 550 | | 520 | |
| 551 | return first_block; | 521 | return last_block; |
| 552 | } | 522 | } |
| 553 | | 523 | |
| 554 | pub fn parseInitializers(self: *Object) !void { | 524 | pub fn parseInitializers(self: *Object) !void { |