| ... | @@ -435,6 +435,8 @@ pub const GotSection = struct { | ... | @@ -435,6 +435,8 @@ pub const GotSection = struct { |
| 435 | const cpu_arch = elf_file.getTarget().cpu.arch; | 435 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 436 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file)); | 436 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file)); |
| 437 | | 437 | |
| | 438 | relocs_log.debug(".got", .{}); |
| | 439 | |
| 438 | for (got.entries.items) |entry| { | 440 | for (got.entries.items) |entry| { |
| 439 | const symbol = elf_file.symbol(entry.ref); | 441 | const symbol = elf_file.symbol(entry.ref); |
| 440 | const extra = if (symbol) |s| s.extra(elf_file) else null; | 442 | const extra = if (symbol) |s| s.extra(elf_file) else null; |
| ... | @@ -447,6 +449,7 @@ pub const GotSection = struct { | ... | @@ -447,6 +449,7 @@ pub const GotSection = struct { |
| 447 | .offset = offset, | 449 | .offset = offset, |
| 448 | .sym = extra.?.dynamic, | 450 | .sym = extra.?.dynamic, |
| 449 | .type = relocation.encode(.glob_dat, cpu_arch), | 451 | .type = relocation.encode(.glob_dat, cpu_arch), |
| | 452 | .target = symbol, |
| 450 | }); | 453 | }); |
| 451 | continue; | 454 | continue; |
| 452 | } | 455 | } |
| ... | @@ -455,6 +458,7 @@ pub const GotSection = struct { | ... | @@ -455,6 +458,7 @@ pub const GotSection = struct { |
| 455 | .offset = offset, | 458 | .offset = offset, |
| 456 | .type = relocation.encode(.irel, cpu_arch), | 459 | .type = relocation.encode(.irel, cpu_arch), |
| 457 | .addend = symbol.?.address(.{ .plt = false }, elf_file), | 460 | .addend = symbol.?.address(.{ .plt = false }, elf_file), |
| | 461 | .target = symbol, |
| 458 | }); | 462 | }); |
| 459 | continue; | 463 | continue; |
| 460 | } | 464 | } |
| ... | @@ -465,6 +469,7 @@ pub const GotSection = struct { | ... | @@ -465,6 +469,7 @@ pub const GotSection = struct { |
| 465 | .offset = offset, | 469 | .offset = offset, |
| 466 | .type = relocation.encode(.rel, cpu_arch), | 470 | .type = relocation.encode(.rel, cpu_arch), |
| 467 | .addend = symbol.?.address(.{ .plt = false }, elf_file), | 471 | .addend = symbol.?.address(.{ .plt = false }, elf_file), |
| | 472 | .target = symbol, |
| 468 | }); | 473 | }); |
| 469 | } | 474 | } |
| 470 | }, | 475 | }, |
| ... | @@ -486,17 +491,20 @@ pub const GotSection = struct { | ... | @@ -486,17 +491,20 @@ pub const GotSection = struct { |
| 486 | .offset = offset, | 491 | .offset = offset, |
| 487 | .sym = extra.?.dynamic, | 492 | .sym = extra.?.dynamic, |
| 488 | .type = relocation.encode(.dtpmod, cpu_arch), | 493 | .type = relocation.encode(.dtpmod, cpu_arch), |
| | 494 | .target = symbol, |
| 489 | }); | 495 | }); |
| 490 | elf_file.addRelaDynAssumeCapacity(.{ | 496 | elf_file.addRelaDynAssumeCapacity(.{ |
| 491 | .offset = offset + 8, | 497 | .offset = offset + 8, |
| 492 | .sym = extra.?.dynamic, | 498 | .sym = extra.?.dynamic, |
| 493 | .type = relocation.encode(.dtpoff, cpu_arch), | 499 | .type = relocation.encode(.dtpoff, cpu_arch), |
| | 500 | .target = symbol, |
| 494 | }); | 501 | }); |
| 495 | } else if (is_dyn_lib) { | 502 | } else if (is_dyn_lib) { |
| 496 | elf_file.addRelaDynAssumeCapacity(.{ | 503 | elf_file.addRelaDynAssumeCapacity(.{ |
| 497 | .offset = offset, | 504 | .offset = offset, |
| 498 | .sym = extra.?.dynamic, | 505 | .sym = extra.?.dynamic, |
| 499 | .type = relocation.encode(.dtpmod, cpu_arch), | 506 | .type = relocation.encode(.dtpmod, cpu_arch), |
| | 507 | .target = symbol, |
| 500 | }); | 508 | }); |
| 501 | } | 509 | } |
| 502 | }, | 510 | }, |
| ... | @@ -508,12 +516,14 @@ pub const GotSection = struct { | ... | @@ -508,12 +516,14 @@ pub const GotSection = struct { |
| 508 | .offset = offset, | 516 | .offset = offset, |
| 509 | .sym = extra.?.dynamic, | 517 | .sym = extra.?.dynamic, |
| 510 | .type = relocation.encode(.tpoff, cpu_arch), | 518 | .type = relocation.encode(.tpoff, cpu_arch), |
| | 519 | .target = symbol, |
| 511 | }); | 520 | }); |
| 512 | } else if (is_dyn_lib) { | 521 | } else if (is_dyn_lib) { |
| 513 | elf_file.addRelaDynAssumeCapacity(.{ | 522 | elf_file.addRelaDynAssumeCapacity(.{ |
| 514 | .offset = offset, | 523 | .offset = offset, |
| 515 | .type = relocation.encode(.tpoff, cpu_arch), | 524 | .type = relocation.encode(.tpoff, cpu_arch), |
| 516 | .addend = symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(), | 525 | .addend = symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(), |
| | 526 | .target = symbol, |
| 517 | }); | 527 | }); |
| 518 | } | 528 | } |
| 519 | }, | 529 | }, |
| ... | @@ -525,6 +535,7 @@ pub const GotSection = struct { | ... | @@ -525,6 +535,7 @@ pub const GotSection = struct { |
| 525 | .sym = if (symbol.?.flags.import) extra.?.dynamic else 0, | 535 | .sym = if (symbol.?.flags.import) extra.?.dynamic else 0, |
| 526 | .type = relocation.encode(.tlsdesc, cpu_arch), | 536 | .type = relocation.encode(.tlsdesc, cpu_arch), |
| 527 | .addend = if (symbol.?.flags.import) 0 else symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(), | 537 | .addend = if (symbol.?.flags.import) 0 else symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(), |
| | 538 | .target = symbol, |
| 528 | }); | 539 | }); |
| 529 | }, | 540 | }, |
| 530 | } | 541 | } |
| ... | @@ -681,6 +692,9 @@ pub const PltSection = struct { | ... | @@ -681,6 +692,9 @@ pub const PltSection = struct { |
| 681 | const gpa = comp.gpa; | 692 | const gpa = comp.gpa; |
| 682 | const cpu_arch = elf_file.getTarget().cpu.arch; | 693 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 683 | try elf_file.rela_plt.ensureUnusedCapacity(gpa, plt.numRela()); | 694 | try elf_file.rela_plt.ensureUnusedCapacity(gpa, plt.numRela()); |
| | 695 | |
| | 696 | relocs_log.debug(".plt", .{}); |
| | 697 | |
| 684 | for (plt.symbols.items) |ref| { | 698 | for (plt.symbols.items) |ref| { |
| 685 | const sym = elf_file.symbol(ref).?; | 699 | const sym = elf_file.symbol(ref).?; |
| 686 | assert(sym.flags.import); | 700 | assert(sym.flags.import); |
| ... | @@ -688,6 +702,14 @@ pub const PltSection = struct { | ... | @@ -688,6 +702,14 @@ pub const PltSection = struct { |
| 688 | const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file)); | 702 | const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file)); |
| 689 | const r_sym: u64 = extra.dynamic; | 703 | const r_sym: u64 = extra.dynamic; |
| 690 | const r_type = relocation.encode(.jump_slot, cpu_arch); | 704 | const r_type = relocation.encode(.jump_slot, cpu_arch); |
| | 705 | |
| | 706 | relocs_log.debug(" {s}: [{x} => {d}({s})] + 0", .{ |
| | 707 | relocation.fmtRelocType(r_type, cpu_arch), |
| | 708 | r_offset, |
| | 709 | r_sym, |
| | 710 | sym.name(elf_file), |
| | 711 | }); |
| | 712 | |
| 691 | elf_file.rela_plt.appendAssumeCapacity(.{ | 713 | elf_file.rela_plt.appendAssumeCapacity(.{ |
| 692 | .r_offset = r_offset, | 714 | .r_offset = r_offset, |
| 693 | .r_info = (r_sym << 32) | r_type, | 715 | .r_info = (r_sym << 32) | r_type, |
| ... | @@ -1053,6 +1075,9 @@ pub const CopyRelSection = struct { | ... | @@ -1053,6 +1075,9 @@ pub const CopyRelSection = struct { |
| 1053 | const gpa = comp.gpa; | 1075 | const gpa = comp.gpa; |
| 1054 | const cpu_arch = elf_file.getTarget().cpu.arch; | 1076 | const cpu_arch = elf_file.getTarget().cpu.arch; |
| 1055 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, copy_rel.numRela()); | 1077 | try elf_file.rela_dyn.ensureUnusedCapacity(gpa, copy_rel.numRela()); |
| | 1078 | |
| | 1079 | relocs_log.debug(".copy.rel", .{}); |
| | 1080 | |
| 1056 | for (copy_rel.symbols.items) |ref| { | 1081 | for (copy_rel.symbols.items) |ref| { |
| 1057 | const sym = elf_file.symbol(ref).?; | 1082 | const sym = elf_file.symbol(ref).?; |
| 1058 | assert(sym.flags.import and sym.flags.has_copy_rel); | 1083 | assert(sym.flags.import and sym.flags.has_copy_rel); |
| ... | @@ -1525,6 +1550,7 @@ const elf = std.elf; | ... | @@ -1525,6 +1550,7 @@ const elf = std.elf; |
| 1525 | const math = std.math; | 1550 | const math = std.math; |
| 1526 | const mem = std.mem; | 1551 | const mem = std.mem; |
| 1527 | const log = std.log.scoped(.link); | 1552 | const log = std.log.scoped(.link); |
| | 1553 | const relocs_log = std.log.scoped(.link_relocs); |
| 1528 | const relocation = @import("relocation.zig"); | 1554 | const relocation = @import("relocation.zig"); |
| 1529 | const std = @import("std"); | 1555 | const std = @import("std"); |
| 1530 | | 1556 | |