Skip to content

Commit 8fc15f1

Browse files
authored
Merge pull request #21145 from ziglang/elf-dwarf-relocs
elf+zigobject: emit relocs for debug sections
2 parents 61919fe + e79ac14 commit 8fc15f1

File tree

10 files changed

+470
-238
lines changed

10 files changed

+470
-238
lines changed

src/arch/riscv64/Emit.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ pub fn emitMir(emit: *Emit) Error!void {
5656
const hi_r_type: u32 = @intFromEnum(std.elf.R_RISCV.HI20);
5757
const lo_r_type: u32 = @intFromEnum(std.elf.R_RISCV.LO12_I);
5858

59-
try atom_ptr.addReloc(elf_file, .{
59+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
6060
.r_offset = start_offset,
6161
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | hi_r_type,
6262
.r_addend = 0,
63-
});
63+
}, zo);
6464

65-
try atom_ptr.addReloc(elf_file, .{
65+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
6666
.r_offset = start_offset + 4,
6767
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | lo_r_type,
6868
.r_addend = 0,
69-
});
69+
}, zo);
7070
},
7171
.load_tlv_reloc => |symbol| {
7272
const elf_file = emit.bin_file.cast(.elf).?;
@@ -76,23 +76,23 @@ pub fn emitMir(emit: *Emit) Error!void {
7676

7777
const R_RISCV = std.elf.R_RISCV;
7878

79-
try atom_ptr.addReloc(elf_file, .{
79+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
8080
.r_offset = start_offset,
8181
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | @intFromEnum(R_RISCV.TPREL_HI20),
8282
.r_addend = 0,
83-
});
83+
}, zo);
8484

85-
try atom_ptr.addReloc(elf_file, .{
85+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
8686
.r_offset = start_offset + 4,
8787
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | @intFromEnum(R_RISCV.TPREL_ADD),
8888
.r_addend = 0,
89-
});
89+
}, zo);
9090

91-
try atom_ptr.addReloc(elf_file, .{
91+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
9292
.r_offset = start_offset + 8,
9393
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | @intFromEnum(R_RISCV.TPREL_LO12_I),
9494
.r_addend = 0,
95-
});
95+
}, zo);
9696
},
9797
.call_extern_fn_reloc => |symbol| {
9898
const elf_file = emit.bin_file.cast(.elf).?;
@@ -101,11 +101,11 @@ pub fn emitMir(emit: *Emit) Error!void {
101101

102102
const r_type: u32 = @intFromEnum(std.elf.R_RISCV.CALL_PLT);
103103

104-
try atom_ptr.addReloc(elf_file, .{
104+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
105105
.r_offset = start_offset,
106106
.r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | r_type,
107107
.r_addend = 0,
108-
});
108+
}, zo);
109109
},
110110
};
111111
}

src/arch/x86_64/Emit.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ pub fn emitMir(emit: *Emit) Error!void {
4848
const zo = elf_file.zigObjectPtr().?;
4949
const atom_ptr = zo.symbol(emit.atom_index).atom(elf_file).?;
5050
const r_type = @intFromEnum(std.elf.R_X86_64.PLT32);
51-
try atom_ptr.addReloc(elf_file, .{
51+
try atom_ptr.addReloc(elf_file.base.comp.gpa, .{
5252
.r_offset = end_offset - 4,
5353
.r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
5454
.r_addend = lowered_relocs[0].off - 4,
55-
});
55+
}, zo);
5656
} else if (emit.lower.bin_file.cast(.macho)) |macho_file| {
5757
// Add relocation to the decl.
5858
const zo = macho_file.getZigObject().?;
@@ -95,22 +95,22 @@ pub fn emitMir(emit: *Emit) Error!void {
9595
const zo = elf_file.zigObjectPtr().?;
9696
const atom = zo.symbol(emit.atom_index).atom(elf_file).?;
9797
const r_type = @intFromEnum(std.elf.R_X86_64.TLSLD);
98-
try atom.addReloc(elf_file, .{
98+
try atom.addReloc(elf_file.base.comp.gpa, .{
9999
.r_offset = end_offset - 4,
100100
.r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
101101
.r_addend = lowered_relocs[0].off - 4,
102-
});
102+
}, zo);
103103
},
104104
.linker_dtpoff => |sym_index| {
105105
const elf_file = emit.lower.bin_file.cast(.elf).?;
106106
const zo = elf_file.zigObjectPtr().?;
107107
const atom = zo.symbol(emit.atom_index).atom(elf_file).?;
108108
const r_type = @intFromEnum(std.elf.R_X86_64.DTPOFF32);
109-
try atom.addReloc(elf_file, .{
109+
try atom.addReloc(elf_file.base.comp.gpa, .{
110110
.r_offset = end_offset - 4,
111111
.r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
112112
.r_addend = lowered_relocs[0].off,
113-
});
113+
}, zo);
114114
},
115115
.linker_reloc => |sym_index| if (emit.lower.bin_file.cast(.elf)) |elf_file| {
116116
const zo = elf_file.zigObjectPtr().?;
@@ -121,21 +121,21 @@ pub fn emitMir(emit: *Emit) Error!void {
121121
@intFromEnum(std.elf.R_X86_64.GOTPCREL)
122122
else
123123
@intFromEnum(std.elf.R_X86_64.PC32);
124-
try atom.addReloc(elf_file, .{
124+
try atom.addReloc(elf_file.base.comp.gpa, .{
125125
.r_offset = end_offset - 4,
126126
.r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
127127
.r_addend = lowered_relocs[0].off - 4,
128-
});
128+
}, zo);
129129
} else {
130130
const r_type: u32 = if (sym.flags.is_tls)
131131
@intFromEnum(std.elf.R_X86_64.TPOFF32)
132132
else
133133
@intFromEnum(std.elf.R_X86_64.@"32");
134-
try atom.addReloc(elf_file, .{
134+
try atom.addReloc(elf_file.base.comp.gpa, .{
135135
.r_offset = end_offset - 4,
136136
.r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
137137
.r_addend = lowered_relocs[0].off,
138-
});
138+
}, zo);
139139
}
140140
} else if (emit.lower.bin_file.cast(.macho)) |macho_file| {
141141
const zo = macho_file.getZigObject().?;

src/link/Dwarf.zig

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const StringSection = struct {
201201
};
202202

203203
/// A linker section containing a sequence of `Unit`s.
204-
const Section = struct {
204+
pub const Section = struct {
205205
dirty: bool,
206206
pad_to_ideal: bool,
207207
alignment: InternPool.Alignment,
@@ -287,7 +287,7 @@ const Section = struct {
287287
return sec.getUnit(unit).addEntry(sec, dwarf);
288288
}
289289

290-
fn getUnit(sec: *Section, unit: Unit.Index) *Unit {
290+
pub fn getUnit(sec: *Section, unit: Unit.Index) *Unit {
291291
return &sec.units.items[@intFromEnum(unit)];
292292
}
293293

@@ -368,7 +368,7 @@ const Unit = struct {
368368
none = std.math.maxInt(u32),
369369
_,
370370

371-
fn unwrap(uio: Optional) ?Index {
371+
pub fn unwrap(uio: Optional) ?Index {
372372
return if (uio != .none) @enumFromInt(@intFromEnum(uio)) else null;
373373
}
374374
};
@@ -415,7 +415,7 @@ const Unit = struct {
415415
return entry;
416416
}
417417

418-
fn getEntry(unit: *Unit, entry: Entry.Index) *Entry {
418+
pub fn getEntry(unit: *Unit, entry: Entry.Index) *Entry {
419419
return &unit.entries.items[@intFromEnum(entry)];
420420
}
421421

@@ -614,7 +614,7 @@ const Entry = struct {
614614
none = std.math.maxInt(u32),
615615
_,
616616

617-
fn unwrap(eio: Optional) ?Index {
617+
pub fn unwrap(eio: Optional) ?Index {
618618
return if (eio != .none) @enumFromInt(@intFromEnum(eio)) else null;
619619
}
620620
};
@@ -736,7 +736,7 @@ const Entry = struct {
736736
}
737737
}
738738

739-
fn assertNonEmpty(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) *Entry {
739+
pub fn assertNonEmpty(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) *Entry {
740740
if (entry.len > 0) return entry;
741741
if (std.debug.runtime_safety) {
742742
log.err("missing {} from {s}", .{
@@ -1958,11 +1958,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
19581958
const loc = tree.tokenLocation(0, tree.nodes.items(.main_token)[decl_inst.data.declaration.src_node]);
19591959
assert(loc.line == zcu.navSrcLine(nav_index));
19601960

1961-
const unit = try dwarf.getUnit(file.mod);
19621961
var wip_nav: WipNav = .{
19631962
.dwarf = dwarf,
19641963
.pt = pt,
1965-
.unit = unit,
1964+
.unit = try dwarf.getUnit(file.mod),
19661965
.entry = undefined,
19671966
.any_children = false,
19681967
.func = .none,
@@ -1981,7 +1980,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
19811980
switch (ip.indexToKey(nav_val.toIntern())) {
19821981
.func => |func| {
19831982
if (nav_gop.found_existing) {
1984-
const unit_ptr = dwarf.debug_info.section.getUnit(unit);
1983+
const unit_ptr = dwarf.debug_info.section.getUnit(wip_nav.unit);
19851984
const entry_ptr = unit_ptr.getEntry(nav_gop.value_ptr.*);
19861985
if (entry_ptr.len >= AbbrevCode.decl_bytes) {
19871986
var abbrev_code_buf: [AbbrevCode.decl_bytes]u8 = undefined;
@@ -2000,7 +1999,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
20001999
}
20012000
}
20022001
entry_ptr.clear();
2003-
} else nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2002+
} else nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
20042003
wip_nav.entry = nav_gop.value_ptr.*;
20052004

20062005
const parent_type, const accessibility: u8 = if (nav.analysis_owner.unwrap()) |cau| parent: {
@@ -2074,8 +2073,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
20742073
if (type_inst_info.inst != value_inst) break :decl_struct;
20752074

20762075
const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
2077-
if (type_gop.found_existing) nav_gop.value_ptr.* = type_gop.value_ptr.* else {
2078-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2076+
if (type_gop.found_existing) {
2077+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(type_gop.value_ptr.*).clear();
2078+
nav_gop.value_ptr.* = type_gop.value_ptr.*;
2079+
} else {
2080+
if (nav_gop.found_existing)
2081+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2082+
else
2083+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
20792084
type_gop.value_ptr.* = nav_gop.value_ptr.*;
20802085
}
20812086
wip_nav.entry = nav_gop.value_ptr.*;
@@ -2139,7 +2144,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
21392144
break :done;
21402145
}
21412146

2142-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2147+
if (nav_gop.found_existing)
2148+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2149+
else
2150+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
21432151
wip_nav.entry = nav_gop.value_ptr.*;
21442152
const diw = wip_nav.debug_info.writer(dwarf.gpa);
21452153
try uleb128(diw, @intFromEnum(AbbrevCode.decl_alias));
@@ -2190,8 +2198,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
21902198
if (type_inst_info.inst != value_inst) break :decl_enum;
21912199

21922200
const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
2193-
if (type_gop.found_existing) nav_gop.value_ptr.* = type_gop.value_ptr.* else {
2194-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2201+
if (type_gop.found_existing) {
2202+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(type_gop.value_ptr.*).clear();
2203+
nav_gop.value_ptr.* = type_gop.value_ptr.*;
2204+
} else {
2205+
if (nav_gop.found_existing)
2206+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2207+
else
2208+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
21952209
type_gop.value_ptr.* = nav_gop.value_ptr.*;
21962210
}
21972211
wip_nav.entry = nav_gop.value_ptr.*;
@@ -2215,7 +2229,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
22152229
break :done;
22162230
}
22172231

2218-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2232+
if (nav_gop.found_existing)
2233+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2234+
else
2235+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
22192236
wip_nav.entry = nav_gop.value_ptr.*;
22202237
const diw = wip_nav.debug_info.writer(dwarf.gpa);
22212238
try uleb128(diw, @intFromEnum(AbbrevCode.decl_alias));
@@ -2264,8 +2281,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
22642281
if (type_inst_info.inst != value_inst) break :decl_union;
22652282

22662283
const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
2267-
if (type_gop.found_existing) nav_gop.value_ptr.* = type_gop.value_ptr.* else {
2268-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2284+
if (type_gop.found_existing) {
2285+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(type_gop.value_ptr.*).clear();
2286+
nav_gop.value_ptr.* = type_gop.value_ptr.*;
2287+
} else {
2288+
if (nav_gop.found_existing)
2289+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2290+
else
2291+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
22692292
type_gop.value_ptr.* = nav_gop.value_ptr.*;
22702293
}
22712294
wip_nav.entry = nav_gop.value_ptr.*;
@@ -2328,7 +2351,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
23282351
break :done;
23292352
}
23302353

2331-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2354+
if (nav_gop.found_existing)
2355+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2356+
else
2357+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
23322358
wip_nav.entry = nav_gop.value_ptr.*;
23332359
const diw = wip_nav.debug_info.writer(dwarf.gpa);
23342360
try uleb128(diw, @intFromEnum(AbbrevCode.decl_alias));
@@ -2377,8 +2403,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
23772403
if (type_inst_info.inst != value_inst) break :decl_opaque;
23782404

23792405
const type_gop = try dwarf.types.getOrPut(dwarf.gpa, nav_val.toIntern());
2380-
if (type_gop.found_existing) nav_gop.value_ptr.* = type_gop.value_ptr.* else {
2381-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2406+
if (type_gop.found_existing) {
2407+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(type_gop.value_ptr.*).clear();
2408+
nav_gop.value_ptr.* = type_gop.value_ptr.*;
2409+
} else {
2410+
if (nav_gop.found_existing)
2411+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2412+
else
2413+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
23822414
type_gop.value_ptr.* = nav_gop.value_ptr.*;
23832415
}
23842416
wip_nav.entry = nav_gop.value_ptr.*;
@@ -2394,7 +2426,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
23942426
break :done;
23952427
}
23962428

2397-
if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit);
2429+
if (nav_gop.found_existing)
2430+
dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(nav_gop.value_ptr.*).clear()
2431+
else
2432+
nav_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
23982433
wip_nav.entry = nav_gop.value_ptr.*;
23992434
const diw = wip_nav.debug_info.writer(dwarf.gpa);
24002435
try uleb128(diw, @intFromEnum(AbbrevCode.decl_alias));
@@ -2412,7 +2447,6 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
24122447
},
24132448
}
24142449
try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
2415-
try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
24162450
try wip_nav.flush();
24172451
}
24182452

0 commit comments

Comments
 (0)