| ... | @@ -52,13 +52,22 @@ source_version_cmd_index: ?u16 = null, | ... | @@ -52,13 +52,22 @@ source_version_cmd_index: ?u16 = null, |
| 52 | uuid_cmd_index: ?u16 = null, | 52 | uuid_cmd_index: ?u16 = null, |
| 53 | code_signature_cmd_index: ?u16 = null, | 53 | code_signature_cmd_index: ?u16 = null, |
| 54 | | 54 | |
| | 55 | // __TEXT segment sections |
| 55 | text_section_index: ?u16 = null, | 56 | text_section_index: ?u16 = null, |
| 56 | stubs_section_index: ?u16 = null, | 57 | stubs_section_index: ?u16 = null, |
| 57 | stub_helper_section_index: ?u16 = null, | 58 | stub_helper_section_index: ?u16 = null, |
| | 59 | text_const_section_index: ?u16 = null, |
| | 60 | cstring_section_index: ?u16 = null, |
| | 61 | |
| | 62 | // __DATA segment sections |
| 58 | got_section_index: ?u16 = null, | 63 | got_section_index: ?u16 = null, |
| 59 | tlv_section_index: ?u16 = null, | 64 | tlv_section_index: ?u16 = null, |
| | 65 | tlv_data_section_index: ?u16 = null, |
| | 66 | tlv_bss_section_index: ?u16 = null, |
| 60 | la_symbol_ptr_section_index: ?u16 = null, | 67 | la_symbol_ptr_section_index: ?u16 = null, |
| | 68 | data_const_section_index: ?u16 = null, |
| 61 | data_section_index: ?u16 = null, | 69 | data_section_index: ?u16 = null, |
| | 70 | bss_section_index: ?u16 = null, |
| 62 | | 71 | |
| 63 | locals: std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(Symbol)) = .{}, | 72 | locals: std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(Symbol)) = .{}, |
| 64 | exports: std.StringArrayHashMapUnmanaged(macho.nlist_64) = .{}, | 73 | exports: std.StringArrayHashMapUnmanaged(macho.nlist_64) = .{}, |
| ... | @@ -71,13 +80,25 @@ strtab: std.ArrayListUnmanaged(u8) = .{}, | ... | @@ -71,13 +80,25 @@ strtab: std.ArrayListUnmanaged(u8) = .{}, |
| 71 | | 80 | |
| 72 | stub_helper_stubs_start_off: ?u64 = null, | 81 | stub_helper_stubs_start_off: ?u64 = null, |
| 73 | | 82 | |
| 74 | segments_directory: std.AutoHashMapUnmanaged([16]u8, u16) = .{}, | 83 | mappings: std.AutoHashMapUnmanaged(MappingKey, SectionMapping) = .{}, |
| 75 | directory: std.AutoHashMapUnmanaged(DirectoryKey, DirectoryEntry) = .{}, | 84 | unhandled_sections: std.AutoHashMapUnmanaged(MappingKey, u0) = .{}, |
| | 85 | |
| | 86 | const MappingKey = struct { |
| | 87 | object_id: u16, |
| | 88 | source_sect_id: u16, |
| | 89 | }; |
| | 90 | |
| | 91 | const SectionMapping = struct { |
| | 92 | source_sect_id: u16, |
| | 93 | target_seg_id: u16, |
| | 94 | target_sect_id: u16, |
| | 95 | offset: u32, |
| | 96 | }; |
| 76 | | 97 | |
| 77 | const Symbol = struct { | 98 | const Symbol = struct { |
| 78 | inner: macho.nlist_64, | 99 | inner: macho.nlist_64, |
| 79 | tt: Type, | 100 | tt: Type, |
| 80 | object: *Object, | 101 | object_id: u16, |
| 81 | | 102 | |
| 82 | const Type = enum { | 103 | const Type = enum { |
| 83 | Local, | 104 | Local, |
| ... | @@ -86,16 +107,6 @@ const Symbol = struct { | ... | @@ -86,16 +107,6 @@ const Symbol = struct { |
| 86 | }; | 107 | }; |
| 87 | }; | 108 | }; |
| 88 | | 109 | |
| 89 | const DirectoryKey = struct { | | |
| 90 | segname: [16]u8, | | |
| 91 | sectname: [16]u8, | | |
| 92 | }; | | |
| 93 | | | |
| 94 | const DirectoryEntry = struct { | | |
| 95 | seg_index: u16, | | |
| 96 | sect_index: u16, | | |
| 97 | }; | | |
| 98 | | | |
| 99 | const DebugInfo = struct { | 110 | const DebugInfo = struct { |
| 100 | inner: dwarf.DwarfInfo, | 111 | inner: dwarf.DwarfInfo, |
| 101 | debug_info: []u8, | 112 | debug_info: []u8, |
| ... | @@ -221,8 +232,8 @@ pub fn deinit(self: *Zld) void { | ... | @@ -221,8 +232,8 @@ pub fn deinit(self: *Zld) void { |
| 221 | lc.deinit(self.allocator); | 232 | lc.deinit(self.allocator); |
| 222 | } | 233 | } |
| 223 | self.load_commands.deinit(self.allocator); | 234 | self.load_commands.deinit(self.allocator); |
| 224 | self.segments_directory.deinit(self.allocator); | 235 | self.mappings.deinit(self.allocator); |
| 225 | self.directory.deinit(self.allocator); | 236 | self.unhandled_sections.deinit(self.allocator); |
| 226 | if (self.file) |*f| f.close(); | 237 | if (self.file) |*f| f.close(); |
| 227 | } | 238 | } |
| 228 | | 239 | |
| ... | @@ -263,6 +274,7 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8) !void { | ... | @@ -263,6 +274,7 @@ pub fn link(self: *Zld, files: []const []const u8, out_path: []const u8) !void { |
| 263 | | 274 | |
| 264 | try self.populateMetadata(); | 275 | try self.populateMetadata(); |
| 265 | try self.parseInputFiles(files); | 276 | try self.parseInputFiles(files); |
| | 277 | try self.sortSections(); |
| 266 | try self.resolveImports(); | 278 | try self.resolveImports(); |
| 267 | try self.allocateTextSegment(); | 279 | try self.allocateTextSegment(); |
| 268 | try self.allocateDataSegment(); | 280 | try self.allocateDataSegment(); |
| ... | @@ -282,10 +294,9 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { | ... | @@ -282,10 +294,9 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { |
| 282 | error.NotObject => break :try_object, | 294 | error.NotObject => break :try_object, |
| 283 | else => |e| return e, | 295 | else => |e| return e, |
| 284 | }; | 296 | }; |
| 285 | const index = self.objects.items.len; | 297 | const index = @intCast(u16, self.objects.items.len); |
| 286 | try self.objects.append(self.allocator, object); | 298 | try self.objects.append(self.allocator, object); |
| 287 | const p_object = &self.objects.items[index]; | 299 | try self.updateMetadata(index); |
| 288 | try self.parseObjectFile(p_object); | | |
| 289 | continue; | 300 | continue; |
| 290 | } | 301 | } |
| 291 | | 302 | |
| ... | @@ -296,10 +307,9 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { | ... | @@ -296,10 +307,9 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { |
| 296 | }; | 307 | }; |
| 297 | defer archive.deinit(); | 308 | defer archive.deinit(); |
| 298 | while (archive.objects.popOrNull()) |object| { | 309 | while (archive.objects.popOrNull()) |object| { |
| 299 | const index = self.objects.items.len; | 310 | const index = @intCast(u16, self.objects.items.len); |
| 300 | try self.objects.append(self.allocator, object); | 311 | try self.objects.append(self.allocator, object); |
| 301 | const p_object = &self.objects.items[index]; | 312 | try self.updateMetadata(index); |
| 302 | try self.parseObjectFile(p_object); | | |
| 303 | } | 313 | } |
| 304 | continue; | 314 | continue; |
| 305 | } | 315 | } |
| ... | @@ -309,49 +319,425 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { | ... | @@ -309,49 +319,425 @@ fn parseInputFiles(self: *Zld, files: []const []const u8) !void { |
| 309 | } | 319 | } |
| 310 | } | 320 | } |
| 311 | | 321 | |
| 312 | fn parseObjectFile(self: *Zld, object: *const Object) !void { | 322 | fn mapAndUpdateSections( |
| 313 | const seg_cmd = object.load_commands.items[object.segment_cmd_index.?].Segment; | 323 | self: *Zld, |
| 314 | for (seg_cmd.sections.items) |sect| { | 324 | object_id: u16, |
| 315 | const segname = parseName(&sect.segname); | 325 | source_sect_id: u16, |
| 316 | const sectname = parseName(&sect.sectname); | 326 | target_seg_id: u16, |
| | 327 | target_sect_id: u16, |
| | 328 | ) !void { |
| | 329 | const object = self.objects.items[object_id]; |
| | 330 | const source_seg = object.load_commands.items[object.segment_cmd_index.?].Segment; |
| | 331 | const source_sect = source_seg.sections.items[source_sect_id]; |
| | 332 | const target_seg = &self.load_commands.items[target_seg_id].Segment; |
| | 333 | const target_sect = &target_seg.sections.items[target_sect_id]; |
| | 334 | log.warn("{}", .{target_sect}); |
| | 335 | |
| | 336 | const alignment = try math.powi(u32, 2, source_sect.@"align"); |
| | 337 | const offset = mem.alignForwardGeneric(u64, target_sect.size, alignment); |
| | 338 | const size = mem.alignForwardGeneric(u64, source_sect.size, alignment); |
| | 339 | const key = MappingKey{ |
| | 340 | .object_id = object_id, |
| | 341 | .source_sect_id = source_sect_id, |
| | 342 | }; |
| | 343 | try self.mappings.putNoClobber(self.allocator, key, .{ |
| | 344 | .source_sect_id = source_sect_id, |
| | 345 | .target_seg_id = target_seg_id, |
| | 346 | .target_sect_id = target_sect_id, |
| | 347 | .offset = @intCast(u32, offset), |
| | 348 | }); |
| | 349 | log.warn("{s}: {s},{s} mapped to {s},{s} from 0x{x} to 0x{x}", .{ |
| | 350 | object.name, |
| | 351 | parseName(&source_sect.segname), |
| | 352 | parseName(&source_sect.sectname), |
| | 353 | parseName(&target_sect.segname), |
| | 354 | parseName(&target_sect.sectname), |
| | 355 | offset, |
| | 356 | offset + size, |
| | 357 | }); |
| 317 | | 358 | |
| 318 | const seg_index = self.segments_directory.get(sect.segname) orelse { | 359 | target_sect.@"align" = math.max(target_sect.@"align", source_sect.@"align"); |
| 319 | log.info("segname {s} not found in the output artifact", .{sect.segname}); | 360 | target_sect.size = offset + size; |
| 320 | continue; | 361 | } |
| 321 | }; | 362 | |
| 322 | const seg = &self.load_commands.items[seg_index].Segment; | 363 | fn updateMetadata(self: *Zld, object_id: u16) !void { |
| 323 | const res = try self.directory.getOrPut(self.allocator, .{ | 364 | const object = self.objects.items[object_id]; |
| 324 | .segname = sect.segname, | 365 | const object_seg = object.load_commands.items[object.segment_cmd_index.?].Segment; |
| 325 | .sectname = sect.sectname, | 366 | const text_seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 326 | }); | 367 | const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 327 | if (!res.found_existing) { | 368 | |
| 328 | const sect_index = @intCast(u16, seg.sections.items.len); | 369 | // Create missing metadata |
| 329 | if (mem.eql(u8, sectname, "__thread_vars")) { | 370 | for (object_seg.sections.items) |source_sect, id| { |
| 330 | self.tlv_section_index = sect_index; | 371 | if (id == object.text_section_index.?) continue; |
| 331 | } | 372 | const segname = parseName(&source_sect.segname); |
| 332 | try seg.append(self.allocator, .{ | 373 | const sectname = parseName(&source_sect.sectname); |
| 333 | .sectname = makeStaticString(&sect.sectname), | 374 | const flags = source_sect.flags; |
| 334 | .segname = makeStaticString(&sect.segname), | 375 | |
| 335 | .addr = 0, | 376 | switch (flags) { |
| 336 | .size = 0, | 377 | macho.S_REGULAR, macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => { |
| 337 | .offset = 0, | 378 | if (mem.eql(u8, segname, "__TEXT")) { |
| 338 | .@"align" = sect.@"align", | 379 | if (self.text_const_section_index != null) continue; |
| 339 | .reloff = 0, | 380 | |
| 340 | .nreloc = 0, | 381 | self.text_const_section_index = @intCast(u16, text_seg.sections.items.len); |
| 341 | .flags = sect.flags, | 382 | try text_seg.append(self.allocator, .{ |
| 342 | .reserved1 = 0, | 383 | .sectname = makeStaticString("__const"), |
| 343 | .reserved2 = 0, | 384 | .segname = makeStaticString("__TEXT"), |
| 344 | .reserved3 = 0, | 385 | .addr = 0, |
| 345 | }); | 386 | .size = 0, |
| 346 | res.entry.value = .{ | 387 | .offset = 0, |
| 347 | .seg_index = seg_index, | 388 | .@"align" = 0, |
| 348 | .sect_index = sect_index, | 389 | .reloff = 0, |
| 349 | }; | 390 | .nreloc = 0, |
| | 391 | .flags = macho.S_REGULAR, |
| | 392 | .reserved1 = 0, |
| | 393 | .reserved2 = 0, |
| | 394 | .reserved3 = 0, |
| | 395 | }); |
| | 396 | } else if (mem.eql(u8, segname, "__DATA")) { |
| | 397 | if (!mem.eql(u8, sectname, "__const")) continue; |
| | 398 | if (self.data_const_section_index != null) continue; |
| | 399 | |
| | 400 | self.data_const_section_index = @intCast(u16, data_seg.sections.items.len); |
| | 401 | try data_seg.append(self.allocator, .{ |
| | 402 | .sectname = makeStaticString("__const"), |
| | 403 | .segname = makeStaticString("__DATA"), |
| | 404 | .addr = 0, |
| | 405 | .size = 0, |
| | 406 | .offset = 0, |
| | 407 | .@"align" = 0, |
| | 408 | .reloff = 0, |
| | 409 | .nreloc = 0, |
| | 410 | .flags = macho.S_REGULAR, |
| | 411 | .reserved1 = 0, |
| | 412 | .reserved2 = 0, |
| | 413 | .reserved3 = 0, |
| | 414 | }); |
| | 415 | } |
| | 416 | }, |
| | 417 | macho.S_CSTRING_LITERALS => { |
| | 418 | if (!mem.eql(u8, segname, "__TEXT")) continue; |
| | 419 | if (self.cstring_section_index != null) continue; |
| | 420 | |
| | 421 | self.cstring_section_index = @intCast(u16, text_seg.sections.items.len); |
| | 422 | try text_seg.append(self.allocator, .{ |
| | 423 | .sectname = makeStaticString("__cstring"), |
| | 424 | .segname = makeStaticString("__TEXT"), |
| | 425 | .addr = 0, |
| | 426 | .size = 0, |
| | 427 | .offset = 0, |
| | 428 | .@"align" = 0, |
| | 429 | .reloff = 0, |
| | 430 | .nreloc = 0, |
| | 431 | .flags = macho.S_CSTRING_LITERALS, |
| | 432 | .reserved1 = 0, |
| | 433 | .reserved2 = 0, |
| | 434 | .reserved3 = 0, |
| | 435 | }); |
| | 436 | }, |
| | 437 | macho.S_ZEROFILL => { |
| | 438 | if (!mem.eql(u8, segname, "__DATA")) continue; |
| | 439 | if (self.bss_section_index != null) continue; |
| | 440 | |
| | 441 | self.bss_section_index = @intCast(u16, data_seg.sections.items.len); |
| | 442 | try data_seg.append(self.allocator, .{ |
| | 443 | .sectname = makeStaticString("__bss"), |
| | 444 | .segname = makeStaticString("__DATA"), |
| | 445 | .addr = 0, |
| | 446 | .size = 0, |
| | 447 | .offset = 0, |
| | 448 | .@"align" = 0, |
| | 449 | .reloff = 0, |
| | 450 | .nreloc = 0, |
| | 451 | .flags = macho.S_ZEROFILL, |
| | 452 | .reserved1 = 0, |
| | 453 | .reserved2 = 0, |
| | 454 | .reserved3 = 0, |
| | 455 | }); |
| | 456 | }, |
| | 457 | macho.S_THREAD_LOCAL_VARIABLES => { |
| | 458 | if (!mem.eql(u8, segname, "__DATA")) continue; |
| | 459 | if (self.tlv_section_index != null) continue; |
| | 460 | |
| | 461 | self.tlv_section_index = @intCast(u16, data_seg.sections.items.len); |
| | 462 | try data_seg.append(self.allocator, .{ |
| | 463 | .sectname = makeStaticString("__thread_vars"), |
| | 464 | .segname = makeStaticString("__DATA"), |
| | 465 | .addr = 0, |
| | 466 | .size = 0, |
| | 467 | .offset = 0, |
| | 468 | .@"align" = 0, |
| | 469 | .reloff = 0, |
| | 470 | .nreloc = 0, |
| | 471 | .flags = macho.S_THREAD_LOCAL_VARIABLES, |
| | 472 | .reserved1 = 0, |
| | 473 | .reserved2 = 0, |
| | 474 | .reserved3 = 0, |
| | 475 | }); |
| | 476 | }, |
| | 477 | macho.S_THREAD_LOCAL_REGULAR => { |
| | 478 | if (!mem.eql(u8, segname, "__DATA")) continue; |
| | 479 | if (self.tlv_data_section_index != null) continue; |
| | 480 | |
| | 481 | self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len); |
| | 482 | try data_seg.append(self.allocator, .{ |
| | 483 | .sectname = makeStaticString("__thread_data"), |
| | 484 | .segname = makeStaticString("__DATA"), |
| | 485 | .addr = 0, |
| | 486 | .size = 0, |
| | 487 | .offset = 0, |
| | 488 | .@"align" = 0, |
| | 489 | .reloff = 0, |
| | 490 | .nreloc = 0, |
| | 491 | .flags = macho.S_THREAD_LOCAL_REGULAR, |
| | 492 | .reserved1 = 0, |
| | 493 | .reserved2 = 0, |
| | 494 | .reserved3 = 0, |
| | 495 | }); |
| | 496 | }, |
| | 497 | macho.S_THREAD_LOCAL_ZEROFILL => { |
| | 498 | if (!mem.eql(u8, segname, "__DATA")) continue; |
| | 499 | if (self.tlv_bss_section_index != null) continue; |
| | 500 | |
| | 501 | self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len); |
| | 502 | try data_seg.append(self.allocator, .{ |
| | 503 | .sectname = makeStaticString("__thread_bss"), |
| | 504 | .segname = makeStaticString("__DATA"), |
| | 505 | .addr = 0, |
| | 506 | .size = 0, |
| | 507 | .offset = 0, |
| | 508 | .@"align" = 0, |
| | 509 | .reloff = 0, |
| | 510 | .nreloc = 0, |
| | 511 | .flags = macho.S_THREAD_LOCAL_ZEROFILL, |
| | 512 | .reserved1 = 0, |
| | 513 | .reserved2 = 0, |
| | 514 | .reserved3 = 0, |
| | 515 | }); |
| | 516 | }, |
| | 517 | else => { |
| | 518 | log.warn("unhandled section type 0x{x} for '{s}/{s}'", .{ flags, segname, sectname }); |
| | 519 | }, |
| 350 | } | 520 | } |
| 351 | const dest_sect = &seg.sections.items[res.entry.value.sect_index]; | 521 | } |
| 352 | dest_sect.@"align" = math.max(dest_sect.@"align", sect.@"align"); | 522 | |
| 353 | dest_sect.size += sect.size; | 523 | // Update section mappings |
| 354 | seg.inner.filesize += sect.size; | 524 | // __TEXT,__text has to be always defined! |
| | 525 | try self.mapAndUpdateSections( |
| | 526 | object_id, |
| | 527 | object.text_section_index.?, |
| | 528 | self.text_segment_cmd_index.?, |
| | 529 | self.text_section_index.?, |
| | 530 | ); |
| | 531 | |
| | 532 | for (object_seg.sections.items) |source_sect, id| { |
| | 533 | const source_sect_id = @intCast(u16, id); |
| | 534 | if (id == object.text_section_index.?) continue; |
| | 535 | |
| | 536 | const segname = parseName(&source_sect.segname); |
| | 537 | const sectname = parseName(&source_sect.sectname); |
| | 538 | const flags = source_sect.flags; |
| | 539 | |
| | 540 | switch (flags) { |
| | 541 | macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => { |
| | 542 | try self.mapAndUpdateSections( |
| | 543 | object_id, |
| | 544 | source_sect_id, |
| | 545 | self.text_segment_cmd_index.?, |
| | 546 | self.text_const_section_index.?, |
| | 547 | ); |
| | 548 | }, |
| | 549 | macho.S_CSTRING_LITERALS => { |
| | 550 | try self.mapAndUpdateSections( |
| | 551 | object_id, |
| | 552 | source_sect_id, |
| | 553 | self.text_segment_cmd_index.?, |
| | 554 | self.cstring_section_index.?, |
| | 555 | ); |
| | 556 | }, |
| | 557 | macho.S_ZEROFILL => { |
| | 558 | try self.mapAndUpdateSections( |
| | 559 | object_id, |
| | 560 | source_sect_id, |
| | 561 | self.data_segment_cmd_index.?, |
| | 562 | self.bss_section_index.?, |
| | 563 | ); |
| | 564 | }, |
| | 565 | macho.S_THREAD_LOCAL_VARIABLES => { |
| | 566 | try self.mapAndUpdateSections( |
| | 567 | object_id, |
| | 568 | source_sect_id, |
| | 569 | self.data_segment_cmd_index.?, |
| | 570 | self.tlv_section_index.?, |
| | 571 | ); |
| | 572 | }, |
| | 573 | macho.S_THREAD_LOCAL_REGULAR => { |
| | 574 | try self.mapAndUpdateSections( |
| | 575 | object_id, |
| | 576 | source_sect_id, |
| | 577 | self.data_segment_cmd_index.?, |
| | 578 | self.tlv_data_section_index.?, |
| | 579 | ); |
| | 580 | }, |
| | 581 | macho.S_THREAD_LOCAL_ZEROFILL => { |
| | 582 | try self.mapAndUpdateSections( |
| | 583 | object_id, |
| | 584 | source_sect_id, |
| | 585 | self.data_segment_cmd_index.?, |
| | 586 | self.tlv_bss_section_index.?, |
| | 587 | ); |
| | 588 | }, |
| | 589 | macho.S_REGULAR => { |
| | 590 | if (mem.eql(u8, segname, "__TEXT")) { |
| | 591 | try self.mapAndUpdateSections( |
| | 592 | object_id, |
| | 593 | source_sect_id, |
| | 594 | self.text_segment_cmd_index.?, |
| | 595 | self.text_const_section_index.?, |
| | 596 | ); |
| | 597 | continue; |
| | 598 | } else if (mem.eql(u8, segname, "__DATA")) { |
| | 599 | if (mem.eql(u8, sectname, "__data")) { |
| | 600 | try self.mapAndUpdateSections( |
| | 601 | object_id, |
| | 602 | source_sect_id, |
| | 603 | self.data_segment_cmd_index.?, |
| | 604 | self.data_section_index.?, |
| | 605 | ); |
| | 606 | continue; |
| | 607 | } else if (mem.eql(u8, sectname, "__const")) { |
| | 608 | try self.mapAndUpdateSections( |
| | 609 | object_id, |
| | 610 | source_sect_id, |
| | 611 | self.data_segment_cmd_index.?, |
| | 612 | self.data_const_section_index.?, |
| | 613 | ); |
| | 614 | continue; |
| | 615 | } |
| | 616 | } |
| | 617 | log.warn("section '{s}/{s}' will be unmapped", .{ segname, sectname }); |
| | 618 | try self.unhandled_sections.putNoClobber(self.allocator, .{ |
| | 619 | .object_id = object_id, |
| | 620 | .source_sect_id = source_sect_id, |
| | 621 | }, 0); |
| | 622 | }, |
| | 623 | else => { |
| | 624 | log.warn("section '{s}/{s}' will be unmapped", .{ segname, sectname }); |
| | 625 | try self.unhandled_sections.putNoClobber(self.allocator, .{ |
| | 626 | .object_id = object_id, |
| | 627 | .source_sect_id = source_sect_id, |
| | 628 | }, 0); |
| | 629 | }, |
| | 630 | } |
| | 631 | } |
| | 632 | } |
| | 633 | |
| | 634 | fn sortSections(self: *Zld) !void { |
| | 635 | const text_seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| | 636 | var text_sections = text_seg.sections.toOwnedSlice(self.allocator); |
| | 637 | defer self.allocator.free(text_sections); |
| | 638 | try text_seg.sections.ensureCapacity(self.allocator, text_sections.len); |
| | 639 | |
| | 640 | const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| | 641 | var data_sections = data_seg.sections.toOwnedSlice(self.allocator); |
| | 642 | defer self.allocator.free(data_sections); |
| | 643 | try data_seg.sections.ensureCapacity(self.allocator, data_sections.len); |
| | 644 | |
| | 645 | var text_index_mapping = std.AutoHashMap(u16, u16).init(self.allocator); |
| | 646 | defer text_index_mapping.deinit(); |
| | 647 | |
| | 648 | var data_index_mapping = std.AutoHashMap(u16, u16).init(self.allocator); |
| | 649 | defer data_index_mapping.deinit(); |
| | 650 | |
| | 651 | if (self.text_section_index) |index| { |
| | 652 | const new_index = @intCast(u16, text_seg.sections.items.len); |
| | 653 | self.text_section_index = new_index; |
| | 654 | text_seg.sections.appendAssumeCapacity(text_sections[index]); |
| | 655 | try text_index_mapping.putNoClobber(index, new_index); |
| | 656 | } |
| | 657 | if (self.stubs_section_index) |index| { |
| | 658 | const new_index = @intCast(u16, text_seg.sections.items.len); |
| | 659 | self.stubs_section_index = new_index; |
| | 660 | text_seg.sections.appendAssumeCapacity(text_sections[index]); |
| | 661 | try text_index_mapping.putNoClobber(index, new_index); |
| | 662 | } |
| | 663 | if (self.stub_helper_section_index) |index| { |
| | 664 | const new_index = @intCast(u16, text_seg.sections.items.len); |
| | 665 | self.stub_helper_section_index = new_index; |
| | 666 | text_seg.sections.appendAssumeCapacity(text_sections[index]); |
| | 667 | try text_index_mapping.putNoClobber(index, new_index); |
| | 668 | } |
| | 669 | if (self.text_const_section_index) |index| { |
| | 670 | const new_index = @intCast(u16, text_seg.sections.items.len); |
| | 671 | self.text_const_section_index = new_index; |
| | 672 | text_seg.sections.appendAssumeCapacity(text_sections[index]); |
| | 673 | try text_index_mapping.putNoClobber(index, new_index); |
| | 674 | } |
| | 675 | if (self.cstring_section_index) |index| { |
| | 676 | const new_index = @intCast(u16, text_seg.sections.items.len); |
| | 677 | self.cstring_section_index = new_index; |
| | 678 | text_seg.sections.appendAssumeCapacity(text_sections[index]); |
| | 679 | try text_index_mapping.putNoClobber(index, new_index); |
| | 680 | } |
| | 681 | |
| | 682 | if (self.got_section_index) |index| { |
| | 683 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 684 | self.got_section_index = new_index; |
| | 685 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 686 | try data_index_mapping.putNoClobber(index, new_index); |
| | 687 | } |
| | 688 | if (self.data_const_section_index) |index| { |
| | 689 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 690 | self.data_const_section_index = new_index; |
| | 691 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 692 | try data_index_mapping.putNoClobber(index, new_index); |
| | 693 | } |
| | 694 | if (self.la_symbol_ptr_section_index) |index| { |
| | 695 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 696 | self.la_symbol_ptr_section_index = new_index; |
| | 697 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 698 | try data_index_mapping.putNoClobber(index, new_index); |
| | 699 | } |
| | 700 | if (self.tlv_section_index) |index| { |
| | 701 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 702 | self.tlv_section_index = new_index; |
| | 703 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 704 | try data_index_mapping.putNoClobber(index, new_index); |
| | 705 | } |
| | 706 | if (self.data_section_index) |index| { |
| | 707 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 708 | self.data_section_index = new_index; |
| | 709 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 710 | try data_index_mapping.putNoClobber(index, new_index); |
| | 711 | } |
| | 712 | if (self.tlv_data_section_index) |index| { |
| | 713 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 714 | self.tlv_data_section_index = new_index; |
| | 715 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 716 | try data_index_mapping.putNoClobber(index, new_index); |
| | 717 | } |
| | 718 | if (self.tlv_bss_section_index) |index| { |
| | 719 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 720 | self.tlv_bss_section_index = new_index; |
| | 721 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 722 | try data_index_mapping.putNoClobber(index, new_index); |
| | 723 | } |
| | 724 | if (self.bss_section_index) |index| { |
| | 725 | const new_index = @intCast(u16, data_seg.sections.items.len); |
| | 726 | self.bss_section_index = new_index; |
| | 727 | data_seg.sections.appendAssumeCapacity(data_sections[index]); |
| | 728 | try data_index_mapping.putNoClobber(index, new_index); |
| | 729 | } |
| | 730 | |
| | 731 | var it = self.mappings.iterator(); |
| | 732 | while (it.next()) |entry| { |
| | 733 | const mapping = &entry.value; |
| | 734 | if (self.text_segment_cmd_index.? == mapping.target_seg_id) { |
| | 735 | const new_index = text_index_mapping.get(mapping.target_sect_id) orelse unreachable; |
| | 736 | mapping.target_sect_id = new_index; |
| | 737 | } else if (self.data_segment_cmd_index.? == mapping.target_seg_id) { |
| | 738 | const new_index = data_index_mapping.get(mapping.target_sect_id) orelse unreachable; |
| | 739 | mapping.target_sect_id = new_index; |
| | 740 | } else unreachable; |
| 355 | } | 741 | } |
| 356 | } | 742 | } |
| 357 | | 743 | |
| ... | @@ -790,35 +1176,9 @@ fn writeStubInStubHelper(self: *Zld, index: u32) !void { | ... | @@ -790,35 +1176,9 @@ fn writeStubInStubHelper(self: *Zld, index: u32) !void { |
| 790 | } | 1176 | } |
| 791 | | 1177 | |
| 792 | fn resolveSymbols(self: *Zld) !void { | 1178 | fn resolveSymbols(self: *Zld) !void { |
| 793 | const Address = struct { | 1179 | for (self.objects.items) |object, object_id| { |
| 794 | addr: u64, | | |
| 795 | size: u64, | | |
| 796 | }; | | |
| 797 | var next_address = std.AutoHashMap(DirectoryKey, Address).init(self.allocator); | | |
| 798 | defer next_address.deinit(); | | |
| 799 | | | |
| 800 | for (self.objects.items) |*object| { | | |
| 801 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; | 1180 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; |
| 802 | | 1181 | |
| 803 | for (seg.sections.items) |sect| { | | |
| 804 | const key: DirectoryKey = .{ | | |
| 805 | .segname = sect.segname, | | |
| 806 | .sectname = sect.sectname, | | |
| 807 | }; | | |
| 808 | const indices = self.directory.get(key) orelse continue; | | |
| 809 | const out_seg = self.load_commands.items[indices.seg_index].Segment; | | |
| 810 | const out_sect = out_seg.sections.items[indices.sect_index]; | | |
| 811 | | | |
| 812 | const res = try next_address.getOrPut(key); | | |
| 813 | const next = &res.entry.value; | | |
| 814 | if (res.found_existing) { | | |
| 815 | next.addr += next.size; | | |
| 816 | } else { | | |
| 817 | next.addr = out_sect.addr; | | |
| 818 | } | | |
| 819 | next.size = sect.size; | | |
| 820 | } | | |
| 821 | | | |
| 822 | for (object.symtab.items) |sym| { | 1182 | for (object.symtab.items) |sym| { |
| 823 | if (isImport(&sym)) continue; | 1183 | if (isImport(&sym)) continue; |
| 824 | | 1184 | |
| ... | @@ -851,24 +1211,36 @@ fn resolveSymbols(self: *Zld) !void { | ... | @@ -851,24 +1211,36 @@ fn resolveSymbols(self: *Zld) !void { |
| 851 | } | 1211 | } |
| 852 | } | 1212 | } |
| 853 | | 1213 | |
| 854 | const sect = seg.sections.items[sym.n_sect - 1]; | 1214 | const source_sect_id = sym.n_sect - 1; |
| 855 | const key: DirectoryKey = .{ | 1215 | const target_mapping = self.mappings.get(.{ |
| 856 | .segname = sect.segname, | 1216 | .object_id = @intCast(u16, object_id), |
| 857 | .sectname = sect.sectname, | 1217 | .source_sect_id = source_sect_id, |
| | 1218 | }) orelse { |
| | 1219 | if (self.unhandled_sections.get(.{ |
| | 1220 | .object_id = @intCast(u16, object_id), |
| | 1221 | .source_sect_id = source_sect_id, |
| | 1222 | }) != null) continue; |
| | 1223 | |
| | 1224 | log.err("section not mapped for symbol '{s}': {}", .{ sym_name, sym }); |
| | 1225 | return error.SectionNotMappedForSymbol; |
| 858 | }; | 1226 | }; |
| 859 | const res = self.directory.get(key) orelse continue; | 1227 | const source_sect = seg.sections.items[source_sect_id]; |
| 860 | | 1228 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; |
| 861 | const n_value = sym.n_value - sect.addr + next_address.get(key).?.addr; | 1229 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; |
| | 1230 | const target_addr = target_sect.addr + target_mapping.offset; |
| | 1231 | const n_value = sym.n_value - source_sect.addr + target_addr; |
| 862 | | 1232 | |
| 863 | log.warn("resolving '{s}':{} as {s} symbol at 0x{x}", .{ sym_name, sym, tt, n_value }); | 1233 | log.warn("resolving '{s}':{} as {s} symbol at 0x{x}", .{ sym_name, sym, tt, n_value }); |
| 864 | | 1234 | |
| 865 | var n_sect = res.sect_index + 1; | 1235 | // TODO this assumes only two symbol-filled segments. Also, there might be a more |
| 866 | for (self.load_commands.items) |sseg, i| { | 1236 | // generic way of doing this. |
| 867 | if (i == res.seg_index) { | 1237 | const n_sect = blk: { |
| 868 | break; | 1238 | if (self.text_segment_cmd_index.? == target_mapping.target_seg_id) { |
| | 1239 | break :blk target_mapping.target_sect_id + 1; |
| 869 | } | 1240 | } |
| 870 | n_sect += @intCast(u16, sseg.Segment.sections.items.len); | 1241 | const prev_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 871 | } | 1242 | break :blk @intCast(u16, prev_seg.sections.items.len + target_mapping.target_sect_id + 1); |
| | 1243 | }; |
| 872 | | 1244 | |
| 873 | const n_strx = try self.makeString(sym_name); | 1245 | const n_strx = try self.makeString(sym_name); |
| 874 | try locs.entry.value.append(self.allocator, .{ | 1246 | try locs.entry.value.append(self.allocator, .{ |
| ... | @@ -880,64 +1252,26 @@ fn resolveSymbols(self: *Zld) !void { | ... | @@ -880,64 +1252,26 @@ fn resolveSymbols(self: *Zld) !void { |
| 880 | .n_sect = @intCast(u8, n_sect), | 1252 | .n_sect = @intCast(u8, n_sect), |
| 881 | }, | 1253 | }, |
| 882 | .tt = tt, | 1254 | .tt = tt, |
| 883 | .object = object, | 1255 | .object_id = @intCast(u16, object_id), |
| 884 | }); | 1256 | }); |
| 885 | } | 1257 | } |
| 886 | } | 1258 | } |
| 887 | } | 1259 | } |
| 888 | | 1260 | |
| 889 | fn doRelocs(self: *Zld) !void { | 1261 | fn doRelocs(self: *Zld) !void { |
| 890 | const Space = struct { | 1262 | for (self.objects.items) |object, object_id| { |
| 891 | address: u64, | | |
| 892 | offset: u64, | | |
| 893 | size: u64, | | |
| 894 | }; | | |
| 895 | var next_space = std.AutoHashMap(DirectoryKey, Space).init(self.allocator); | | |
| 896 | defer next_space.deinit(); | | |
| 897 | | | |
| 898 | for (self.objects.items) |object| { | | |
| 899 | log.warn("\n\n", .{}); | 1263 | log.warn("\n\n", .{}); |
| 900 | log.warn("relocating object {s}", .{object.name}); | 1264 | log.warn("relocating object {s}", .{object.name}); |
| 901 | | 1265 | |
| 902 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; | 1266 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; |
| 903 | | 1267 | |
| 904 | for (seg.sections.items) |sect| { | 1268 | for (seg.sections.items) |sect, source_sect_id| { |
| 905 | const key: DirectoryKey = .{ | | |
| 906 | .segname = sect.segname, | | |
| 907 | .sectname = sect.sectname, | | |
| 908 | }; | | |
| 909 | const indices = self.directory.get(key) orelse continue; | | |
| 910 | const out_seg = self.load_commands.items[indices.seg_index].Segment; | | |
| 911 | const out_sect = out_seg.sections.items[indices.sect_index]; | | |
| 912 | | | |
| 913 | const res = try next_space.getOrPut(key); | | |
| 914 | const next = &res.entry.value; | | |
| 915 | if (res.found_existing) { | | |
| 916 | next.offset += next.size; | | |
| 917 | next.address += next.size; | | |
| 918 | } else { | | |
| 919 | next.offset = out_sect.offset; | | |
| 920 | next.address = out_sect.addr; | | |
| 921 | } | | |
| 922 | next.size = sect.size; | | |
| 923 | } | | |
| 924 | | | |
| 925 | for (seg.sections.items) |sect| { | | |
| 926 | const segname = parseName(&sect.segname); | 1269 | const segname = parseName(&sect.segname); |
| 927 | const sectname = parseName(&sect.sectname); | 1270 | const sectname = parseName(&sect.sectname); |
| 928 | | 1271 | |
| 929 | const key: DirectoryKey = .{ | 1272 | var code = try self.allocator.alloc(u8, sect.size); |
| 930 | .segname = sect.segname, | 1273 | _ = try object.file.preadAll(code, sect.offset); |
| 931 | .sectname = sect.sectname, | 1274 | defer self.allocator.free(code); |
| 932 | }; | | |
| 933 | const next = next_space.get(key) orelse continue; | | |
| 934 | | | |
| 935 | var code = blk: { | | |
| 936 | var buf = try self.allocator.alloc(u8, sect.size); | | |
| 937 | _ = try object.file.preadAll(buf, sect.offset); | | |
| 938 | break :blk std.ArrayList(u8).fromOwnedSlice(self.allocator, buf); | | |
| 939 | }; | | |
| 940 | defer code.deinit(); | | |
| 941 | | 1275 | |
| 942 | // Parse relocs (if any) | 1276 | // Parse relocs (if any) |
| 943 | var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc); | 1277 | var raw_relocs = try self.allocator.alloc(u8, @sizeOf(macho.relocation_info) * sect.nreloc); |
| ... | @@ -945,12 +1279,25 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -945,12 +1279,25 @@ fn doRelocs(self: *Zld) !void { |
| 945 | _ = try object.file.preadAll(raw_relocs, sect.reloff); | 1279 | _ = try object.file.preadAll(raw_relocs, sect.reloff); |
| 946 | const relocs = mem.bytesAsSlice(macho.relocation_info, raw_relocs); | 1280 | const relocs = mem.bytesAsSlice(macho.relocation_info, raw_relocs); |
| 947 | | 1281 | |
| | 1282 | // Get mapping |
| | 1283 | const target_mapping = self.mappings.get(.{ |
| | 1284 | .object_id = @intCast(u16, object_id), |
| | 1285 | .source_sect_id = @intCast(u16, source_sect_id), |
| | 1286 | }) orelse { |
| | 1287 | log.warn("no mapping for {s},{s}; skipping", .{ segname, sectname }); |
| | 1288 | continue; |
| | 1289 | }; |
| | 1290 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; |
| | 1291 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; |
| | 1292 | const target_sect_addr = target_sect.addr + target_mapping.offset; |
| | 1293 | const target_sect_off = target_sect.offset + target_mapping.offset; |
| | 1294 | |
| 948 | var addend: ?u64 = null; | 1295 | var addend: ?u64 = null; |
| 949 | var sub: ?i64 = null; | 1296 | var sub: ?i64 = null; |
| 950 | | 1297 | |
| 951 | for (relocs) |rel| { | 1298 | for (relocs) |rel| { |
| 952 | const off = @intCast(u32, rel.r_address); | 1299 | const off = @intCast(u32, rel.r_address); |
| 953 | const this_addr = next.address + off; | 1300 | const this_addr = target_sect_addr + off; |
| 954 | | 1301 | |
| 955 | switch (self.arch.?) { | 1302 | switch (self.arch.?) { |
| 956 | .aarch64 => { | 1303 | .aarch64 => { |
| ... | @@ -975,7 +1322,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -975,7 +1322,7 @@ fn doRelocs(self: *Zld) !void { |
| 975 | else => {}, | 1322 | else => {}, |
| 976 | } | 1323 | } |
| 977 | | 1324 | |
| 978 | const target_addr = try self.relocTargetAddr(object, rel, next_space); | 1325 | const target_addr = try self.relocTargetAddr(@intCast(u16, object_id), rel); |
| 979 | log.warn(" | target address 0x{x}", .{target_addr}); | 1326 | log.warn(" | target address 0x{x}", .{target_addr}); |
| 980 | if (rel.r_extern == 1) { | 1327 | if (rel.r_extern == 1) { |
| 981 | const target_symname = object.getString(object.symtab.items[rel.r_symbolnum].n_strx); | 1328 | const target_symname = object.getString(object.symtab.items[rel.r_symbolnum].n_strx); |
| ... | @@ -995,16 +1342,16 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -995,16 +1342,16 @@ fn doRelocs(self: *Zld) !void { |
| 995 | .X86_64_RELOC_GOT, | 1342 | .X86_64_RELOC_GOT, |
| 996 | => { | 1343 | => { |
| 997 | assert(rel.r_length == 2); | 1344 | assert(rel.r_length == 2); |
| 998 | const inst = code.items[off..][0..4]; | 1345 | const inst = code[off..][0..4]; |
| 999 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); | 1346 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); |
| 1000 | mem.writeIntLittle(u32, inst, displacement); | 1347 | mem.writeIntLittle(u32, inst, displacement); |
| 1001 | }, | 1348 | }, |
| 1002 | .X86_64_RELOC_TLV => { | 1349 | .X86_64_RELOC_TLV => { |
| 1003 | assert(rel.r_length == 2); | 1350 | assert(rel.r_length == 2); |
| 1004 | // We need to rewrite the opcode from movq to leaq. | 1351 | // We need to rewrite the opcode from movq to leaq. |
| 1005 | code.items[off - 2] = 0x8d; | 1352 | code[off - 2] = 0x8d; |
| 1006 | // Add displacement. | 1353 | // Add displacement. |
| 1007 | const inst = code.items[off..][0..4]; | 1354 | const inst = code[off..][0..4]; |
| 1008 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); | 1355 | const displacement = @bitCast(u32, @intCast(i32, @intCast(i64, target_addr) - @intCast(i64, this_addr) - 4)); |
| 1009 | mem.writeIntLittle(u32, inst, displacement); | 1356 | mem.writeIntLittle(u32, inst, displacement); |
| 1010 | }, | 1357 | }, |
| ... | @@ -1014,7 +1361,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1014,7 +1361,7 @@ fn doRelocs(self: *Zld) !void { |
| 1014 | .X86_64_RELOC_SIGNED_4, | 1361 | .X86_64_RELOC_SIGNED_4, |
| 1015 | => { | 1362 | => { |
| 1016 | assert(rel.r_length == 2); | 1363 | assert(rel.r_length == 2); |
| 1017 | const inst = code.items[off..][0..4]; | 1364 | const inst = code[off..][0..4]; |
| 1018 | const offset: i32 = blk: { | 1365 | const offset: i32 = blk: { |
| 1019 | if (rel.r_extern == 1) { | 1366 | if (rel.r_extern == 1) { |
| 1020 | break :blk mem.readIntLittle(i32, inst); | 1367 | break :blk mem.readIntLittle(i32, inst); |
| ... | @@ -1043,7 +1390,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1043,7 +1390,7 @@ fn doRelocs(self: *Zld) !void { |
| 1043 | .X86_64_RELOC_UNSIGNED => { | 1390 | .X86_64_RELOC_UNSIGNED => { |
| 1044 | switch (rel.r_length) { | 1391 | switch (rel.r_length) { |
| 1045 | 3 => { | 1392 | 3 => { |
| 1046 | const inst = code.items[off..][0..8]; | 1393 | const inst = code[off..][0..8]; |
| 1047 | const offset = mem.readIntLittle(i64, inst); | 1394 | const offset = mem.readIntLittle(i64, inst); |
| 1048 | log.warn(" | calculated addend 0x{x}", .{offset}); | 1395 | log.warn(" | calculated addend 0x{x}", .{offset}); |
| 1049 | const result = if (sub) |s| | 1396 | const result = if (sub) |s| |
| ... | @@ -1054,12 +1401,19 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1054,12 +1401,19 @@ fn doRelocs(self: *Zld) !void { |
| 1054 | sub = null; | 1401 | sub = null; |
| 1055 | | 1402 | |
| 1056 | // TODO should handle this better. | 1403 | // TODO should handle this better. |
| 1057 | if (mem.eql(u8, segname, "__DATA")) outer: { | 1404 | outer: { |
| 1058 | if (!mem.eql(u8, sectname, "__data") and | 1405 | var hit: bool = false; |
| 1059 | !mem.eql(u8, sectname, "__const") and | 1406 | if (self.data_section_index) |index| inner: { |
| 1060 | !mem.eql(u8, sectname, "__mod_init_func")) break :outer; | 1407 | if (index != target_mapping.target_sect_id) break :inner; |
| | 1408 | hit = true; |
| | 1409 | } |
| | 1410 | if (self.data_const_section_index) |index| inner: { |
| | 1411 | if (index != target_mapping.target_sect_id) break :inner; |
| | 1412 | hit = true; |
| | 1413 | } |
| | 1414 | if (!hit) break :outer; |
| 1061 | const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; | 1415 | const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1062 | const this_offset = next.address + off - this_seg.inner.vmaddr; | 1416 | const this_offset = target_sect_addr + off - this_seg.inner.vmaddr; |
| 1063 | try self.local_rebases.append(self.allocator, .{ | 1417 | try self.local_rebases.append(self.allocator, .{ |
| 1064 | .offset = this_offset, | 1418 | .offset = this_offset, |
| 1065 | .segment_id = @intCast(u16, self.data_segment_cmd_index.?), | 1419 | .segment_id = @intCast(u16, self.data_segment_cmd_index.?), |
| ... | @@ -1067,7 +1421,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1067,7 +1421,7 @@ fn doRelocs(self: *Zld) !void { |
| 1067 | } | 1421 | } |
| 1068 | }, | 1422 | }, |
| 1069 | 2 => { | 1423 | 2 => { |
| 1070 | const inst = code.items[off..][0..4]; | 1424 | const inst = code[off..][0..4]; |
| 1071 | const offset = mem.readIntLittle(i32, inst); | 1425 | const offset = mem.readIntLittle(i32, inst); |
| 1072 | log.warn(" | calculated addend 0x{x}", .{offset}); | 1426 | log.warn(" | calculated addend 0x{x}", .{offset}); |
| 1073 | const result = if (sub) |s| | 1427 | const result = if (sub) |s| |
| ... | @@ -1091,7 +1445,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1091,7 +1445,7 @@ fn doRelocs(self: *Zld) !void { |
| 1091 | switch (rel_type) { | 1445 | switch (rel_type) { |
| 1092 | .ARM64_RELOC_BRANCH26 => { | 1446 | .ARM64_RELOC_BRANCH26 => { |
| 1093 | assert(rel.r_length == 2); | 1447 | assert(rel.r_length == 2); |
| 1094 | const inst = code.items[off..][0..4]; | 1448 | const inst = code[off..][0..4]; |
| 1095 | const displacement = @intCast(i28, @intCast(i64, target_addr) - @intCast(i64, this_addr)); | 1449 | const displacement = @intCast(i28, @intCast(i64, target_addr) - @intCast(i64, this_addr)); |
| 1096 | var parsed = mem.bytesAsValue(meta.TagPayload(Arm64, Arm64.Branch), inst); | 1450 | var parsed = mem.bytesAsValue(meta.TagPayload(Arm64, Arm64.Branch), inst); |
| 1097 | parsed.disp = @truncate(u26, @bitCast(u28, displacement) >> 2); | 1451 | parsed.disp = @truncate(u26, @bitCast(u28, displacement) >> 2); |
| ... | @@ -1101,7 +1455,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1101,7 +1455,7 @@ fn doRelocs(self: *Zld) !void { |
| 1101 | .ARM64_RELOC_TLVP_LOAD_PAGE21, | 1455 | .ARM64_RELOC_TLVP_LOAD_PAGE21, |
| 1102 | => { | 1456 | => { |
| 1103 | assert(rel.r_length == 2); | 1457 | assert(rel.r_length == 2); |
| 1104 | const inst = code.items[off..][0..4]; | 1458 | const inst = code[off..][0..4]; |
| 1105 | const ta = if (addend) |a| target_addr + a else target_addr; | 1459 | const ta = if (addend) |a| target_addr + a else target_addr; |
| 1106 | const this_page = @intCast(i32, this_addr >> 12); | 1460 | const this_page = @intCast(i32, this_addr >> 12); |
| 1107 | const target_page = @intCast(i32, ta >> 12); | 1461 | const target_page = @intCast(i32, ta >> 12); |
| ... | @@ -1115,7 +1469,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1115,7 +1469,7 @@ fn doRelocs(self: *Zld) !void { |
| 1115 | .ARM64_RELOC_PAGEOFF12, | 1469 | .ARM64_RELOC_PAGEOFF12, |
| 1116 | .ARM64_RELOC_GOT_LOAD_PAGEOFF12, | 1470 | .ARM64_RELOC_GOT_LOAD_PAGEOFF12, |
| 1117 | => { | 1471 | => { |
| 1118 | const inst = code.items[off..][0..4]; | 1472 | const inst = code[off..][0..4]; |
| 1119 | if (Arm64.isArithmetic(inst)) { | 1473 | if (Arm64.isArithmetic(inst)) { |
| 1120 | log.warn(" | detected ADD opcode", .{}); | 1474 | log.warn(" | detected ADD opcode", .{}); |
| 1121 | // add | 1475 | // add |
| ... | @@ -1153,7 +1507,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1153,7 +1507,7 @@ fn doRelocs(self: *Zld) !void { |
| 1153 | rn: u5, | 1507 | rn: u5, |
| 1154 | size: u1, | 1508 | size: u1, |
| 1155 | }; | 1509 | }; |
| 1156 | const inst = code.items[off..][0..4]; | 1510 | const inst = code[off..][0..4]; |
| 1157 | const parsed: RegInfo = blk: { | 1511 | const parsed: RegInfo = blk: { |
| 1158 | if (Arm64.isArithmetic(inst)) { | 1512 | if (Arm64.isArithmetic(inst)) { |
| 1159 | const curr = mem.bytesAsValue(meta.TagPayload(Arm64, Arm64.Add), inst); | 1513 | const curr = mem.bytesAsValue(meta.TagPayload(Arm64, Arm64.Add), inst); |
| ... | @@ -1175,7 +1529,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1175,7 +1529,7 @@ fn doRelocs(self: *Zld) !void { |
| 1175 | .ARM64_RELOC_UNSIGNED => { | 1529 | .ARM64_RELOC_UNSIGNED => { |
| 1176 | switch (rel.r_length) { | 1530 | switch (rel.r_length) { |
| 1177 | 3 => { | 1531 | 3 => { |
| 1178 | const inst = code.items[off..][0..8]; | 1532 | const inst = code[off..][0..8]; |
| 1179 | const offset = mem.readIntLittle(i64, inst); | 1533 | const offset = mem.readIntLittle(i64, inst); |
| 1180 | log.warn(" | calculated addend 0x{x}", .{offset}); | 1534 | log.warn(" | calculated addend 0x{x}", .{offset}); |
| 1181 | const result = if (sub) |s| | 1535 | const result = if (sub) |s| |
| ... | @@ -1186,12 +1540,19 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1186,12 +1540,19 @@ fn doRelocs(self: *Zld) !void { |
| 1186 | sub = null; | 1540 | sub = null; |
| 1187 | | 1541 | |
| 1188 | // TODO should handle this better. | 1542 | // TODO should handle this better. |
| 1189 | if (mem.eql(u8, segname, "__DATA")) outer: { | 1543 | outer: { |
| 1190 | if (!mem.eql(u8, sectname, "__data") and | 1544 | var hit: bool = false; |
| 1191 | !mem.eql(u8, sectname, "__const") and | 1545 | if (self.data_section_index) |index| inner: { |
| 1192 | !mem.eql(u8, sectname, "__mod_init_func")) break :outer; | 1546 | if (index != target_mapping.target_sect_id) break :inner; |
| | 1547 | hit = true; |
| | 1548 | } |
| | 1549 | if (self.data_const_section_index) |index| inner: { |
| | 1550 | if (index != target_mapping.target_sect_id) break :inner; |
| | 1551 | hit = true; |
| | 1552 | } |
| | 1553 | if (!hit) break :outer; |
| 1193 | const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; | 1554 | const this_seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1194 | const this_offset = next.address + off - this_seg.inner.vmaddr; | 1555 | const this_offset = target_sect_addr + off - this_seg.inner.vmaddr; |
| 1195 | try self.local_rebases.append(self.allocator, .{ | 1556 | try self.local_rebases.append(self.allocator, .{ |
| 1196 | .offset = this_offset, | 1557 | .offset = this_offset, |
| 1197 | .segment_id = @intCast(u16, self.data_segment_cmd_index.?), | 1558 | .segment_id = @intCast(u16, self.data_segment_cmd_index.?), |
| ... | @@ -1199,7 +1560,7 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1199,7 +1560,7 @@ fn doRelocs(self: *Zld) !void { |
| 1199 | } | 1560 | } |
| 1200 | }, | 1561 | }, |
| 1201 | 2 => { | 1562 | 2 => { |
| 1202 | const inst = code.items[off..][0..4]; | 1563 | const inst = code[off..][0..4]; |
| 1203 | const offset = mem.readIntLittle(i32, inst); | 1564 | const offset = mem.readIntLittle(i32, inst); |
| 1204 | log.warn(" | calculated addend 0x{x}", .{offset}); | 1565 | log.warn(" | calculated addend 0x{x}", .{offset}); |
| 1205 | const result = if (sub) |s| | 1566 | const result = if (sub) |s| |
| ... | @@ -1227,40 +1588,50 @@ fn doRelocs(self: *Zld) !void { | ... | @@ -1227,40 +1588,50 @@ fn doRelocs(self: *Zld) !void { |
| 1227 | segname, | 1588 | segname, |
| 1228 | sectname, | 1589 | sectname, |
| 1229 | object.name, | 1590 | object.name, |
| 1230 | next.offset, | 1591 | target_sect_off, |
| 1231 | next.offset + next.size, | 1592 | target_sect_off + code.len, |
| 1232 | }); | 1593 | }); |
| 1233 | | 1594 | |
| 1234 | if (mem.eql(u8, sectname, "__bss") or | 1595 | if (target_sect.flags == macho.S_ZEROFILL or |
| 1235 | mem.eql(u8, sectname, "__thread_bss") or | 1596 | target_sect.flags == macho.S_THREAD_LOCAL_ZEROFILL or |
| 1236 | mem.eql(u8, sectname, "__thread_vars")) | 1597 | target_sect.flags == macho.S_THREAD_LOCAL_VARIABLES) |
| 1237 | { | 1598 | { |
| | 1599 | log.warn("zeroing out '{s},{s}' from 0x{x} to 0x{x}", .{ |
| | 1600 | parseName(&target_sect.segname), |
| | 1601 | parseName(&target_sect.sectname), |
| | 1602 | target_sect_off, |
| | 1603 | target_sect_off + code.len, |
| | 1604 | }); |
| 1238 | // Zero-out the space | 1605 | // Zero-out the space |
| 1239 | var zeroes = try self.allocator.alloc(u8, next.size); | 1606 | var zeroes = try self.allocator.alloc(u8, code.len); |
| 1240 | defer self.allocator.free(zeroes); | 1607 | defer self.allocator.free(zeroes); |
| 1241 | mem.set(u8, zeroes, 0); | 1608 | mem.set(u8, zeroes, 0); |
| 1242 | try self.file.?.pwriteAll(zeroes, next.offset); | 1609 | try self.file.?.pwriteAll(zeroes, target_sect_off); |
| 1243 | } else { | 1610 | } else { |
| 1244 | try self.file.?.pwriteAll(code.items, next.offset); | 1611 | try self.file.?.pwriteAll(code, target_sect_off); |
| 1245 | } | 1612 | } |
| 1246 | } | 1613 | } |
| 1247 | } | 1614 | } |
| 1248 | } | 1615 | } |
| 1249 | | 1616 | |
| 1250 | fn relocTargetAddr(self: *Zld, object: Object, rel: macho.relocation_info, next_space: anytype) !u64 { | 1617 | fn relocTargetAddr(self: *Zld, object_id: u16, rel: macho.relocation_info) !u64 { |
| | 1618 | const object = self.objects.items[object_id]; |
| 1251 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; | 1619 | const seg = object.load_commands.items[object.segment_cmd_index.?].Segment; |
| 1252 | const target_addr = blk: { | 1620 | const target_addr = blk: { |
| 1253 | if (rel.r_extern == 1) { | 1621 | if (rel.r_extern == 1) { |
| 1254 | const sym = object.symtab.items[rel.r_symbolnum]; | 1622 | const sym = object.symtab.items[rel.r_symbolnum]; |
| 1255 | if (isLocal(&sym) or isExport(&sym)) { | 1623 | if (isLocal(&sym) or isExport(&sym)) { |
| 1256 | // Relocate using section offsets only. | 1624 | // Relocate using section offsets only. |
| 1257 | const source_sect = seg.sections.items[sym.n_sect - 1]; | 1625 | const target_mapping = self.mappings.get(.{ |
| 1258 | const target_space = next_space.get(.{ | 1626 | .object_id = object_id, |
| 1259 | .segname = source_sect.segname, | 1627 | .source_sect_id = sym.n_sect - 1, |
| 1260 | .sectname = source_sect.sectname, | 1628 | }) orelse unreachable; |
| 1261 | }).?; | 1629 | const source_sect = seg.sections.items[target_mapping.source_sect_id]; |
| | 1630 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; |
| | 1631 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; |
| | 1632 | const target_sect_addr = target_sect.addr + target_mapping.offset; |
| 1262 | log.warn(" | symbol local to object", .{}); | 1633 | log.warn(" | symbol local to object", .{}); |
| 1263 | break :blk target_space.address + sym.n_value - source_sect.addr; | 1634 | break :blk target_sect_addr + sym.n_value - source_sect.addr; |
| 1264 | } else if (isImport(&sym)) { | 1635 | } else if (isImport(&sym)) { |
| 1265 | // Relocate to either the artifact's local symbol, or an import from | 1636 | // Relocate to either the artifact's local symbol, or an import from |
| 1266 | // shared library. | 1637 | // shared library. |
| ... | @@ -1309,12 +1680,13 @@ fn relocTargetAddr(self: *Zld, object: Object, rel: macho.relocation_info, next_ | ... | @@ -1309,12 +1680,13 @@ fn relocTargetAddr(self: *Zld, object: Object, rel: macho.relocation_info, next_ |
| 1309 | // here to get the actual section plus offset into that section of the relocated | 1680 | // here to get the actual section plus offset into that section of the relocated |
| 1310 | // symbol. Unless the fine-grained location is encoded within the cell in the code | 1681 | // symbol. Unless the fine-grained location is encoded within the cell in the code |
| 1311 | // buffer? | 1682 | // buffer? |
| 1312 | const source_sectname = seg.sections.items[rel.r_symbolnum - 1]; | 1683 | const target_mapping = self.mappings.get(.{ |
| 1313 | const target_space = next_space.get(.{ | 1684 | .object_id = object_id, |
| 1314 | .segname = source_sectname.segname, | 1685 | .source_sect_id = @intCast(u16, rel.r_symbolnum - 1), |
| 1315 | .sectname = source_sectname.sectname, | 1686 | }) orelse unreachable; |
| 1316 | }).?; | 1687 | const target_seg = self.load_commands.items[target_mapping.target_seg_id].Segment; |
| 1317 | break :blk target_space.address; | 1688 | const target_sect = target_seg.sections.items[target_mapping.target_sect_id]; |
| | 1689 | break :blk target_sect.addr + target_mapping.offset; |
| 1318 | } | 1690 | } |
| 1319 | }; | 1691 | }; |
| 1320 | return target_addr; | 1692 | return target_addr; |
| ... | @@ -1338,7 +1710,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1338,7 +1710,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1338 | .flags = 0, | 1710 | .flags = 0, |
| 1339 | }), | 1711 | }), |
| 1340 | }); | 1712 | }); |
| 1341 | try self.addSegmentToDir(0); | | |
| 1342 | } | 1713 | } |
| 1343 | | 1714 | |
| 1344 | if (self.text_segment_cmd_index == null) { | 1715 | if (self.text_segment_cmd_index == null) { |
| ... | @@ -1358,7 +1729,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1358,7 +1729,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1358 | .flags = 0, | 1729 | .flags = 0, |
| 1359 | }), | 1730 | }), |
| 1360 | }); | 1731 | }); |
| 1361 | try self.addSegmentToDir(self.text_segment_cmd_index.?); | | |
| 1362 | } | 1732 | } |
| 1363 | | 1733 | |
| 1364 | if (self.text_section_index == null) { | 1734 | if (self.text_section_index == null) { |
| ... | @@ -1383,10 +1753,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1383,10 +1753,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1383 | .reserved2 = 0, | 1753 | .reserved2 = 0, |
| 1384 | .reserved3 = 0, | 1754 | .reserved3 = 0, |
| 1385 | }); | 1755 | }); |
| 1386 | try self.addSectionToDir(.{ | | |
| 1387 | .seg_index = self.text_segment_cmd_index.?, | | |
| 1388 | .sect_index = self.text_section_index.?, | | |
| 1389 | }); | | |
| 1390 | } | 1756 | } |
| 1391 | | 1757 | |
| 1392 | if (self.stubs_section_index == null) { | 1758 | if (self.stubs_section_index == null) { |
| ... | @@ -1416,10 +1782,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1416,10 +1782,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1416 | .reserved2 = stub_size, | 1782 | .reserved2 = stub_size, |
| 1417 | .reserved3 = 0, | 1783 | .reserved3 = 0, |
| 1418 | }); | 1784 | }); |
| 1419 | try self.addSectionToDir(.{ | | |
| 1420 | .seg_index = self.text_segment_cmd_index.?, | | |
| 1421 | .sect_index = self.stubs_section_index.?, | | |
| 1422 | }); | | |
| 1423 | } | 1785 | } |
| 1424 | | 1786 | |
| 1425 | if (self.stub_helper_section_index == null) { | 1787 | if (self.stub_helper_section_index == null) { |
| ... | @@ -1449,10 +1811,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1449,10 +1811,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1449 | .reserved2 = 0, | 1811 | .reserved2 = 0, |
| 1450 | .reserved3 = 0, | 1812 | .reserved3 = 0, |
| 1451 | }); | 1813 | }); |
| 1452 | try self.addSectionToDir(.{ | | |
| 1453 | .seg_index = self.text_segment_cmd_index.?, | | |
| 1454 | .sect_index = self.stub_helper_section_index.?, | | |
| 1455 | }); | | |
| 1456 | } | 1814 | } |
| 1457 | | 1815 | |
| 1458 | if (self.data_segment_cmd_index == null) { | 1816 | if (self.data_segment_cmd_index == null) { |
| ... | @@ -1472,7 +1830,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1472,7 +1830,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1472 | .flags = 0, | 1830 | .flags = 0, |
| 1473 | }), | 1831 | }), |
| 1474 | }); | 1832 | }); |
| 1475 | try self.addSegmentToDir(self.data_segment_cmd_index.?); | | |
| 1476 | } | 1833 | } |
| 1477 | | 1834 | |
| 1478 | if (self.got_section_index == null) { | 1835 | if (self.got_section_index == null) { |
| ... | @@ -1492,10 +1849,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1492,10 +1849,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1492 | .reserved2 = 0, | 1849 | .reserved2 = 0, |
| 1493 | .reserved3 = 0, | 1850 | .reserved3 = 0, |
| 1494 | }); | 1851 | }); |
| 1495 | try self.addSectionToDir(.{ | | |
| 1496 | .seg_index = self.data_segment_cmd_index.?, | | |
| 1497 | .sect_index = self.got_section_index.?, | | |
| 1498 | }); | | |
| 1499 | } | 1852 | } |
| 1500 | | 1853 | |
| 1501 | if (self.la_symbol_ptr_section_index == null) { | 1854 | if (self.la_symbol_ptr_section_index == null) { |
| ... | @@ -1515,10 +1868,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1515,10 +1868,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1515 | .reserved2 = 0, | 1868 | .reserved2 = 0, |
| 1516 | .reserved3 = 0, | 1869 | .reserved3 = 0, |
| 1517 | }); | 1870 | }); |
| 1518 | try self.addSectionToDir(.{ | | |
| 1519 | .seg_index = self.data_segment_cmd_index.?, | | |
| 1520 | .sect_index = self.la_symbol_ptr_section_index.?, | | |
| 1521 | }); | | |
| 1522 | } | 1871 | } |
| 1523 | | 1872 | |
| 1524 | if (self.data_section_index == null) { | 1873 | if (self.data_section_index == null) { |
| ... | @@ -1538,10 +1887,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1538,10 +1887,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1538 | .reserved2 = 0, | 1887 | .reserved2 = 0, |
| 1539 | .reserved3 = 0, | 1888 | .reserved3 = 0, |
| 1540 | }); | 1889 | }); |
| 1541 | try self.addSectionToDir(.{ | | |
| 1542 | .seg_index = self.data_segment_cmd_index.?, | | |
| 1543 | .sect_index = self.data_section_index.?, | | |
| 1544 | }); | | |
| 1545 | } | 1890 | } |
| 1546 | | 1891 | |
| 1547 | if (self.linkedit_segment_cmd_index == null) { | 1892 | if (self.linkedit_segment_cmd_index == null) { |
| ... | @@ -1561,7 +1906,6 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1561,7 +1906,6 @@ fn populateMetadata(self: *Zld) !void { |
| 1561 | .flags = 0, | 1906 | .flags = 0, |
| 1562 | }), | 1907 | }), |
| 1563 | }); | 1908 | }); |
| 1564 | try self.addSegmentToDir(self.linkedit_segment_cmd_index.?); | | |
| 1565 | } | 1909 | } |
| 1566 | | 1910 | |
| 1567 | if (self.dyld_info_cmd_index == null) { | 1911 | if (self.dyld_info_cmd_index == null) { |
| ... | @@ -1719,22 +2063,15 @@ fn populateMetadata(self: *Zld) !void { | ... | @@ -1719,22 +2063,15 @@ fn populateMetadata(self: *Zld) !void { |
| 1719 | } | 2063 | } |
| 1720 | | 2064 | |
| 1721 | fn flush(self: *Zld) !void { | 2065 | fn flush(self: *Zld) !void { |
| 1722 | { | 2066 | if (self.bss_section_index) |index| { |
| 1723 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; | 2067 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1724 | for (seg.sections.items) |*sect| { | 2068 | const sect = &seg.sections.items[index]; |
| 1725 | const sectname = parseName(&sect.sectname); | 2069 | sect.offset = 0; |
| 1726 | if (mem.eql(u8, sectname, "__bss") or mem.eql(u8, sectname, "__thread_bss")) { | | |
| 1727 | sect.offset = 0; | | |
| 1728 | } | | |
| 1729 | } | | |
| 1730 | } | 2070 | } |
| 1731 | { | 2071 | if (self.tlv_bss_section_index) |index| { |
| 1732 | const seg = &self.load_commands.items[self.text_segment_cmd_index.?].Segment; | 2072 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 1733 | for (seg.sections.items) |*sect| { | 2073 | const sect = &seg.sections.items[index]; |
| 1734 | if (mem.eql(u8, parseName(&sect.sectname), "__eh_frame")) { | 2074 | sect.offset = 0; |
| 1735 | sect.flags = 0; | | |
| 1736 | } | | |
| 1737 | } | | |
| 1738 | } | 2075 | } |
| 1739 | try self.setEntryPoint(); | 2076 | try self.setEntryPoint(); |
| 1740 | try self.writeRebaseInfoTable(); | 2077 | try self.writeRebaseInfoTable(); |
| ... | @@ -2056,9 +2393,9 @@ fn writeDebugInfo(self: *Zld) !void { | ... | @@ -2056,9 +2393,9 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2056 | var stabs = std.ArrayList(macho.nlist_64).init(self.allocator); | 2393 | var stabs = std.ArrayList(macho.nlist_64).init(self.allocator); |
| 2057 | defer stabs.deinit(); | 2394 | defer stabs.deinit(); |
| 2058 | | 2395 | |
| 2059 | for (self.objects.items) |*object| { | 2396 | for (self.objects.items) |object, object_id| { |
| 2060 | var debug_info = blk: { | 2397 | var debug_info = blk: { |
| 2061 | var di = try DebugInfo.parseFromObject(self.allocator, object.*); | 2398 | var di = try DebugInfo.parseFromObject(self.allocator, object); |
| 2062 | break :blk di orelse continue; | 2399 | break :blk di orelse continue; |
| 2063 | }; | 2400 | }; |
| 2064 | defer debug_info.deinit(self.allocator); | 2401 | defer debug_info.deinit(self.allocator); |
| ... | @@ -2108,7 +2445,7 @@ fn writeDebugInfo(self: *Zld) !void { | ... | @@ -2108,7 +2445,7 @@ fn writeDebugInfo(self: *Zld) !void { |
| 2108 | const target_syms = self.locals.get(symname) orelse continue; | 2445 | const target_syms = self.locals.get(symname) orelse continue; |
| 2109 | const target_sym: Symbol = blk: { | 2446 | const target_sym: Symbol = blk: { |
| 2110 | for (target_syms.items) |ts| { | 2447 | for (target_syms.items) |ts| { |
| 2111 | if (ts.object == object) break :blk ts; | 2448 | if (ts.object_id == @intCast(u16, object_id)) break :blk ts; |
| 2112 | } else continue; | 2449 | } else continue; |
| 2113 | }; | 2450 | }; |
| 2114 | | 2451 | |
| ... | @@ -2204,7 +2541,7 @@ fn writeSymbolTable(self: *Zld) !void { | ... | @@ -2204,7 +2541,7 @@ fn writeSymbolTable(self: *Zld) !void { |
| 2204 | for (entries.value.items) |entry| { | 2541 | for (entries.value.items) |entry| { |
| 2205 | log.warn(" | {}", .{entry.inner}); | 2542 | log.warn(" | {}", .{entry.inner}); |
| 2206 | log.warn(" | {}", .{entry.tt}); | 2543 | log.warn(" | {}", .{entry.tt}); |
| 2207 | log.warn(" | {s}", .{entry.object.name}); | 2544 | log.warn(" | {s}", .{self.objects.items[entry.object_id].name}); |
| 2208 | // switch (entry.tt) { | 2545 | // switch (entry.tt) { |
| 2209 | // .Global => { | 2546 | // .Global => { |
| 2210 | // symbol = entry.inner; | 2547 | // symbol = entry.inner; |
| ... | @@ -2468,20 +2805,6 @@ pub fn parseName(name: *const [16]u8) []const u8 { | ... | @@ -2468,20 +2805,6 @@ pub fn parseName(name: *const [16]u8) []const u8 { |
| 2468 | return name[0..len]; | 2805 | return name[0..len]; |
| 2469 | } | 2806 | } |
| 2470 | | 2807 | |
| 2471 | fn addSegmentToDir(self: *Zld, idx: u16) !void { | | |
| 2472 | const segment_cmd = self.load_commands.items[idx].Segment; | | |
| 2473 | return self.segments_directory.putNoClobber(self.allocator, segment_cmd.inner.segname, idx); | | |
| 2474 | } | | |
| 2475 | | | |
| 2476 | fn addSectionToDir(self: *Zld, value: DirectoryEntry) !void { | | |
| 2477 | const seg = self.load_commands.items[value.seg_index].Segment; | | |
| 2478 | const sect = seg.sections.items[value.sect_index]; | | |
| 2479 | return self.directory.putNoClobber(self.allocator, .{ | | |
| 2480 | .segname = sect.segname, | | |
| 2481 | .sectname = sect.sectname, | | |
| 2482 | }, value); | | |
| 2483 | } | | |
| 2484 | | | |
| 2485 | fn isLocal(sym: *const macho.nlist_64) callconv(.Inline) bool { | 2808 | fn isLocal(sym: *const macho.nlist_64) callconv(.Inline) bool { |
| 2486 | if (isExtern(sym)) return false; | 2809 | if (isExtern(sym)) return false; |
| 2487 | const tt = macho.N_TYPE & sym.n_type; | 2810 | const tt = macho.N_TYPE & sym.n_type; |