authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-02 13:14:22+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-09 12:38:48-07:00
logcf2e462d91cf1e07f5aedbae404cce311de6b664
tree6d7f93c71ed803dd60bf9678e4a001f68092be15
parent133aa709b07040838007ade90f9cfb1a5643fcff

elf: add some extra logging for created dynamic relocs


3 files changed, 40 insertions(+), 0 deletions(-)

src/link/Elf.zig+9
...@@ -4834,6 +4834,7 @@ const RelaDyn = struct {...@@ -4834,6 +4834,7 @@ const RelaDyn = struct {
4834 sym: u64 = 0,4834 sym: u64 = 0,
4835 type: u32,4835 type: u32,
4836 addend: i64 = 0,4836 addend: i64 = 0,
4837 target: ?*const Symbol = null,
4837};4838};
48384839
4839pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {4840pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
...@@ -4842,6 +4843,13 @@ pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {...@@ -4842,6 +4843,13 @@ pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
4842}4843}
48434844
4844pub fn addRelaDynAssumeCapacity(self: *Elf, opts: RelaDyn) void {4845pub fn addRelaDynAssumeCapacity(self: *Elf, opts: RelaDyn) void {
4846 relocs_log.debug(" {s}: [{x} => {d}({s})] + {x}", .{
4847 relocation.fmtRelocType(opts.type, self.getTarget().cpu.arch),
4848 opts.offset,
4849 opts.sym,
4850 if (opts.target) |sym| sym.name(self) else "",
4851 opts.addend,
4852 });
4845 self.rela_dyn.appendAssumeCapacity(.{4853 self.rela_dyn.appendAssumeCapacity(.{
4846 .r_offset = opts.offset,4854 .r_offset = opts.offset,
4847 .r_info = (opts.sym << 32) | opts.type,4855 .r_info = (opts.sym << 32) | opts.type,
...@@ -5772,6 +5780,7 @@ const assert = std.debug.assert;...@@ -5772,6 +5780,7 @@ const assert = std.debug.assert;
5772const elf = std.elf;5780const elf = std.elf;
5773const fs = std.fs;5781const fs = std.fs;
5774const log = std.log.scoped(.link);5782const log = std.log.scoped(.link);
5783const relocs_log = std.log.scoped(.link_relocs);
5775const state_log = std.log.scoped(.link_state);5784const state_log = std.log.scoped(.link_state);
5776const math = std.math;5785const math = std.math;
5777const mem = std.mem;5786const mem = std.mem;
src/link/Elf/Atom.zig+5
...@@ -723,6 +723,7 @@ fn resolveDynAbsReloc(...@@ -723,6 +723,7 @@ fn resolveDynAbsReloc(
723 .sym = target.extra(elf_file).dynamic,723 .sym = target.extra(elf_file).dynamic,
724 .type = relocation.encode(.abs, cpu_arch),724 .type = relocation.encode(.abs, cpu_arch),
725 .addend = A,725 .addend = A,
726 .target = target,
726 });727 });
727 try applyDynamicReloc(A, elf_file, writer);728 try applyDynamicReloc(A, elf_file, writer);
728 } else {729 } else {
...@@ -737,6 +738,7 @@ fn resolveDynAbsReloc(...@@ -737,6 +738,7 @@ fn resolveDynAbsReloc(
737 .sym = target.extra(elf_file).dynamic,738 .sym = target.extra(elf_file).dynamic,
738 .type = relocation.encode(.abs, cpu_arch),739 .type = relocation.encode(.abs, cpu_arch),
739 .addend = A,740 .addend = A,
741 .target = target,
740 });742 });
741 try applyDynamicReloc(A, elf_file, writer);743 try applyDynamicReloc(A, elf_file, writer);
742 } else {744 } else {
...@@ -750,6 +752,7 @@ fn resolveDynAbsReloc(...@@ -750,6 +752,7 @@ fn resolveDynAbsReloc(
750 .sym = target.extra(elf_file).dynamic,752 .sym = target.extra(elf_file).dynamic,
751 .type = relocation.encode(.abs, cpu_arch),753 .type = relocation.encode(.abs, cpu_arch),
752 .addend = A,754 .addend = A,
755 .target = target,
753 });756 });
754 try applyDynamicReloc(A, elf_file, writer);757 try applyDynamicReloc(A, elf_file, writer);
755 },758 },
...@@ -759,6 +762,7 @@ fn resolveDynAbsReloc(...@@ -759,6 +762,7 @@ fn resolveDynAbsReloc(
759 .offset = P,762 .offset = P,
760 .type = relocation.encode(.rel, cpu_arch),763 .type = relocation.encode(.rel, cpu_arch),
761 .addend = S + A,764 .addend = S + A,
765 .target = target,
762 });766 });
763 try applyDynamicReloc(S + A, elf_file, writer);767 try applyDynamicReloc(S + A, elf_file, writer);
764 },768 },
...@@ -769,6 +773,7 @@ fn resolveDynAbsReloc(...@@ -769,6 +773,7 @@ fn resolveDynAbsReloc(
769 .offset = P,773 .offset = P,
770 .type = relocation.encode(.irel, cpu_arch),774 .type = relocation.encode(.irel, cpu_arch),
771 .addend = S_ + A,775 .addend = S_ + A,
776 .target = target,
772 });777 });
773 try applyDynamicReloc(S_ + A, elf_file, writer);778 try applyDynamicReloc(S_ + A, elf_file, writer);
774 },779 },
src/link/Elf/synthetic_sections.zig+26
...@@ -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));
437437
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;
1525const math = std.math;1550const math = std.math;
1526const mem = std.mem;1551const mem = std.mem;
1527const log = std.log.scoped(.link);1552const log = std.log.scoped(.link);
1553const relocs_log = std.log.scoped(.link_relocs);
1528const relocation = @import("relocation.zig");1554const relocation = @import("relocation.zig");
1529const std = @import("std");1555const std = @import("std");
15301556