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 {
48344834 sym: u64 = 0,
48354835 type: u32,
48364836 addend: i64 = 0,
4837 target: ?*const Symbol = null,
48374838};
48384839
48394840pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
......@@ -4842,6 +4843,13 @@ pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
48424843}
48434844
48444845pub 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 });
48454853 self.rela_dyn.appendAssumeCapacity(.{
48464854 .r_offset = opts.offset,
48474855 .r_info = (opts.sym << 32) | opts.type,
......@@ -5772,6 +5780,7 @@ const assert = std.debug.assert;
57725780const elf = std.elf;
57735781const fs = std.fs;
57745782const log = std.log.scoped(.link);
5783const relocs_log = std.log.scoped(.link_relocs);
57755784const state_log = std.log.scoped(.link_state);
57765785const math = std.math;
57775786const mem = std.mem;
src/link/Elf/Atom.zig+5
......@@ -723,6 +723,7 @@ fn resolveDynAbsReloc(
723723 .sym = target.extra(elf_file).dynamic,
724724 .type = relocation.encode(.abs, cpu_arch),
725725 .addend = A,
726 .target = target,
726727 });
727728 try applyDynamicReloc(A, elf_file, writer);
728729 } else {
......@@ -737,6 +738,7 @@ fn resolveDynAbsReloc(
737738 .sym = target.extra(elf_file).dynamic,
738739 .type = relocation.encode(.abs, cpu_arch),
739740 .addend = A,
741 .target = target,
740742 });
741743 try applyDynamicReloc(A, elf_file, writer);
742744 } else {
......@@ -750,6 +752,7 @@ fn resolveDynAbsReloc(
750752 .sym = target.extra(elf_file).dynamic,
751753 .type = relocation.encode(.abs, cpu_arch),
752754 .addend = A,
755 .target = target,
753756 });
754757 try applyDynamicReloc(A, elf_file, writer);
755758 },
......@@ -759,6 +762,7 @@ fn resolveDynAbsReloc(
759762 .offset = P,
760763 .type = relocation.encode(.rel, cpu_arch),
761764 .addend = S + A,
765 .target = target,
762766 });
763767 try applyDynamicReloc(S + A, elf_file, writer);
764768 },
......@@ -769,6 +773,7 @@ fn resolveDynAbsReloc(
769773 .offset = P,
770774 .type = relocation.encode(.irel, cpu_arch),
771775 .addend = S_ + A,
776 .target = target,
772777 });
773778 try applyDynamicReloc(S_ + A, elf_file, writer);
774779 },
src/link/Elf/synthetic_sections.zig+26
......@@ -435,6 +435,8 @@ pub const GotSection = struct {
435435 const cpu_arch = elf_file.getTarget().cpu.arch;
436436 try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file));
437437
438 relocs_log.debug(".got", .{});
439
438440 for (got.entries.items) |entry| {
439441 const symbol = elf_file.symbol(entry.ref);
440442 const extra = if (symbol) |s| s.extra(elf_file) else null;
......@@ -447,6 +449,7 @@ pub const GotSection = struct {
447449 .offset = offset,
448450 .sym = extra.?.dynamic,
449451 .type = relocation.encode(.glob_dat, cpu_arch),
452 .target = symbol,
450453 });
451454 continue;
452455 }
......@@ -455,6 +458,7 @@ pub const GotSection = struct {
455458 .offset = offset,
456459 .type = relocation.encode(.irel, cpu_arch),
457460 .addend = symbol.?.address(.{ .plt = false }, elf_file),
461 .target = symbol,
458462 });
459463 continue;
460464 }
......@@ -465,6 +469,7 @@ pub const GotSection = struct {
465469 .offset = offset,
466470 .type = relocation.encode(.rel, cpu_arch),
467471 .addend = symbol.?.address(.{ .plt = false }, elf_file),
472 .target = symbol,
468473 });
469474 }
470475 },
......@@ -486,17 +491,20 @@ pub const GotSection = struct {
486491 .offset = offset,
487492 .sym = extra.?.dynamic,
488493 .type = relocation.encode(.dtpmod, cpu_arch),
494 .target = symbol,
489495 });
490496 elf_file.addRelaDynAssumeCapacity(.{
491497 .offset = offset + 8,
492498 .sym = extra.?.dynamic,
493499 .type = relocation.encode(.dtpoff, cpu_arch),
500 .target = symbol,
494501 });
495502 } else if (is_dyn_lib) {
496503 elf_file.addRelaDynAssumeCapacity(.{
497504 .offset = offset,
498505 .sym = extra.?.dynamic,
499506 .type = relocation.encode(.dtpmod, cpu_arch),
507 .target = symbol,
500508 });
501509 }
502510 },
......@@ -508,12 +516,14 @@ pub const GotSection = struct {
508516 .offset = offset,
509517 .sym = extra.?.dynamic,
510518 .type = relocation.encode(.tpoff, cpu_arch),
519 .target = symbol,
511520 });
512521 } else if (is_dyn_lib) {
513522 elf_file.addRelaDynAssumeCapacity(.{
514523 .offset = offset,
515524 .type = relocation.encode(.tpoff, cpu_arch),
516525 .addend = symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
526 .target = symbol,
517527 });
518528 }
519529 },
......@@ -525,6 +535,7 @@ pub const GotSection = struct {
525535 .sym = if (symbol.?.flags.import) extra.?.dynamic else 0,
526536 .type = relocation.encode(.tlsdesc, cpu_arch),
527537 .addend = if (symbol.?.flags.import) 0 else symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
538 .target = symbol,
528539 });
529540 },
530541 }
......@@ -681,6 +692,9 @@ pub const PltSection = struct {
681692 const gpa = comp.gpa;
682693 const cpu_arch = elf_file.getTarget().cpu.arch;
683694 try elf_file.rela_plt.ensureUnusedCapacity(gpa, plt.numRela());
695
696 relocs_log.debug(".plt", .{});
697
684698 for (plt.symbols.items) |ref| {
685699 const sym = elf_file.symbol(ref).?;
686700 assert(sym.flags.import);
......@@ -688,6 +702,14 @@ pub const PltSection = struct {
688702 const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file));
689703 const r_sym: u64 = extra.dynamic;
690704 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
691713 elf_file.rela_plt.appendAssumeCapacity(.{
692714 .r_offset = r_offset,
693715 .r_info = (r_sym << 32) | r_type,
......@@ -1053,6 +1075,9 @@ pub const CopyRelSection = struct {
10531075 const gpa = comp.gpa;
10541076 const cpu_arch = elf_file.getTarget().cpu.arch;
10551077 try elf_file.rela_dyn.ensureUnusedCapacity(gpa, copy_rel.numRela());
1078
1079 relocs_log.debug(".copy.rel", .{});
1080
10561081 for (copy_rel.symbols.items) |ref| {
10571082 const sym = elf_file.symbol(ref).?;
10581083 assert(sym.flags.import and sym.flags.has_copy_rel);
......@@ -1525,6 +1550,7 @@ const elf = std.elf;
15251550const math = std.math;
15261551const mem = std.mem;
15271552const log = std.log.scoped(.link);
1553const relocs_log = std.log.scoped(.link_relocs);
15281554const relocation = @import("relocation.zig");
15291555const std = @import("std");
15301556