Commit cf2e462d91

Jakub Konka <kubkon@jakubkonka.com>
2024-10-02 13:14:22
elf: add some extra logging for created dynamic relocs
1 parent 133aa70
Changed files (3)
src/link/Elf/Atom.zig
@@ -723,6 +723,7 @@ fn resolveDynAbsReloc(
                     .sym = target.extra(elf_file).dynamic,
                     .type = relocation.encode(.abs, cpu_arch),
                     .addend = A,
+                    .target = target,
                 });
                 try applyDynamicReloc(A, elf_file, writer);
             } else {
@@ -737,6 +738,7 @@ fn resolveDynAbsReloc(
                     .sym = target.extra(elf_file).dynamic,
                     .type = relocation.encode(.abs, cpu_arch),
                     .addend = A,
+                    .target = target,
                 });
                 try applyDynamicReloc(A, elf_file, writer);
             } else {
@@ -750,6 +752,7 @@ fn resolveDynAbsReloc(
                 .sym = target.extra(elf_file).dynamic,
                 .type = relocation.encode(.abs, cpu_arch),
                 .addend = A,
+                .target = target,
             });
             try applyDynamicReloc(A, elf_file, writer);
         },
@@ -759,6 +762,7 @@ fn resolveDynAbsReloc(
                 .offset = P,
                 .type = relocation.encode(.rel, cpu_arch),
                 .addend = S + A,
+                .target = target,
             });
             try applyDynamicReloc(S + A, elf_file, writer);
         },
@@ -769,6 +773,7 @@ fn resolveDynAbsReloc(
                 .offset = P,
                 .type = relocation.encode(.irel, cpu_arch),
                 .addend = S_ + A,
+                .target = target,
             });
             try applyDynamicReloc(S_ + A, elf_file, writer);
         },
src/link/Elf/synthetic_sections.zig
@@ -435,6 +435,8 @@ pub const GotSection = struct {
         const cpu_arch = elf_file.getTarget().cpu.arch;
         try elf_file.rela_dyn.ensureUnusedCapacity(gpa, got.numRela(elf_file));
 
+        relocs_log.debug(".got", .{});
+
         for (got.entries.items) |entry| {
             const symbol = elf_file.symbol(entry.ref);
             const extra = if (symbol) |s| s.extra(elf_file) else null;
@@ -447,6 +449,7 @@ pub const GotSection = struct {
                             .offset = offset,
                             .sym = extra.?.dynamic,
                             .type = relocation.encode(.glob_dat, cpu_arch),
+                            .target = symbol,
                         });
                         continue;
                     }
@@ -455,6 +458,7 @@ pub const GotSection = struct {
                             .offset = offset,
                             .type = relocation.encode(.irel, cpu_arch),
                             .addend = symbol.?.address(.{ .plt = false }, elf_file),
+                            .target = symbol,
                         });
                         continue;
                     }
@@ -465,6 +469,7 @@ pub const GotSection = struct {
                             .offset = offset,
                             .type = relocation.encode(.rel, cpu_arch),
                             .addend = symbol.?.address(.{ .plt = false }, elf_file),
+                            .target = symbol,
                         });
                     }
                 },
@@ -486,17 +491,20 @@ pub const GotSection = struct {
                             .offset = offset,
                             .sym = extra.?.dynamic,
                             .type = relocation.encode(.dtpmod, cpu_arch),
+                            .target = symbol,
                         });
                         elf_file.addRelaDynAssumeCapacity(.{
                             .offset = offset + 8,
                             .sym = extra.?.dynamic,
                             .type = relocation.encode(.dtpoff, cpu_arch),
+                            .target = symbol,
                         });
                     } else if (is_dyn_lib) {
                         elf_file.addRelaDynAssumeCapacity(.{
                             .offset = offset,
                             .sym = extra.?.dynamic,
                             .type = relocation.encode(.dtpmod, cpu_arch),
+                            .target = symbol,
                         });
                     }
                 },
@@ -508,12 +516,14 @@ pub const GotSection = struct {
                             .offset = offset,
                             .sym = extra.?.dynamic,
                             .type = relocation.encode(.tpoff, cpu_arch),
+                            .target = symbol,
                         });
                     } else if (is_dyn_lib) {
                         elf_file.addRelaDynAssumeCapacity(.{
                             .offset = offset,
                             .type = relocation.encode(.tpoff, cpu_arch),
                             .addend = symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
+                            .target = symbol,
                         });
                     }
                 },
@@ -525,6 +535,7 @@ pub const GotSection = struct {
                         .sym = if (symbol.?.flags.import) extra.?.dynamic else 0,
                         .type = relocation.encode(.tlsdesc, cpu_arch),
                         .addend = if (symbol.?.flags.import) 0 else symbol.?.address(.{}, elf_file) - elf_file.tlsAddress(),
+                        .target = symbol,
                     });
                 },
             }
@@ -681,6 +692,9 @@ pub const PltSection = struct {
         const gpa = comp.gpa;
         const cpu_arch = elf_file.getTarget().cpu.arch;
         try elf_file.rela_plt.ensureUnusedCapacity(gpa, plt.numRela());
+
+        relocs_log.debug(".plt", .{});
+
         for (plt.symbols.items) |ref| {
             const sym = elf_file.symbol(ref).?;
             assert(sym.flags.import);
@@ -688,6 +702,14 @@ pub const PltSection = struct {
             const r_offset: u64 = @intCast(sym.gotPltAddress(elf_file));
             const r_sym: u64 = extra.dynamic;
             const r_type = relocation.encode(.jump_slot, cpu_arch);
+
+            relocs_log.debug("  {s}: [{x} => {d}({s})] + 0", .{
+                relocation.fmtRelocType(r_type, cpu_arch),
+                r_offset,
+                r_sym,
+                sym.name(elf_file),
+            });
+
             elf_file.rela_plt.appendAssumeCapacity(.{
                 .r_offset = r_offset,
                 .r_info = (r_sym << 32) | r_type,
@@ -1053,6 +1075,9 @@ pub const CopyRelSection = struct {
         const gpa = comp.gpa;
         const cpu_arch = elf_file.getTarget().cpu.arch;
         try elf_file.rela_dyn.ensureUnusedCapacity(gpa, copy_rel.numRela());
+
+        relocs_log.debug(".copy.rel", .{});
+
         for (copy_rel.symbols.items) |ref| {
             const sym = elf_file.symbol(ref).?;
             assert(sym.flags.import and sym.flags.has_copy_rel);
@@ -1525,6 +1550,7 @@ const elf = std.elf;
 const math = std.math;
 const mem = std.mem;
 const log = std.log.scoped(.link);
+const relocs_log = std.log.scoped(.link_relocs);
 const relocation = @import("relocation.zig");
 const std = @import("std");
 
src/link/Elf.zig
@@ -4834,6 +4834,7 @@ const RelaDyn = struct {
     sym: u64 = 0,
     type: u32,
     addend: i64 = 0,
+    target: ?*const Symbol = null,
 };
 
 pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
@@ -4842,6 +4843,13 @@ pub fn addRelaDyn(self: *Elf, opts: RelaDyn) !void {
 }
 
 pub fn addRelaDynAssumeCapacity(self: *Elf, opts: RelaDyn) void {
+    relocs_log.debug("  {s}: [{x} => {d}({s})] + {x}", .{
+        relocation.fmtRelocType(opts.type, self.getTarget().cpu.arch),
+        opts.offset,
+        opts.sym,
+        if (opts.target) |sym| sym.name(self) else "",
+        opts.addend,
+    });
     self.rela_dyn.appendAssumeCapacity(.{
         .r_offset = opts.offset,
         .r_info = (opts.sym << 32) | opts.type,
@@ -5772,6 +5780,7 @@ const assert = std.debug.assert;
 const elf = std.elf;
 const fs = std.fs;
 const log = std.log.scoped(.link);
+const relocs_log = std.log.scoped(.link_relocs);
 const state_log = std.log.scoped(.link_state);
 const math = std.math;
 const mem = std.mem;