authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-22 23:20:53-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-22 23:20:53-04:00
logc6286eee4ba8065b4a3f37fe3668041871e1b42e
tree6dda0dbc9eb1ca77546fb960a2eba24ef3f5d07f
parentee84deda988afb36f24ecd35ebea40221c1b80d8
parent0a5291435513727a9f19570008388364f391b0d2
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21170 from jacobly0/more-dwarf-cleanup

Dwarf: more cleanup

10 files changed, 212 insertions(+), 77 deletions(-)

ci/x86_64-linux-debug.sh+1-1
......@@ -64,7 +64,7 @@ stage3-debug/bin/zig build \
6464
6565stage3-debug/bin/zig build test docs \
6666 --maxrss 21000000000 \
67 -Dlldb=$HOME/deps/lldb-zig/Debug-f96d3e6fc/bin/lldb \
67 -Dlldb=$HOME/deps/lldb-zig/Debug-62538077d/bin/lldb \
6868 -fqemu \
6969 -fwasmtime \
7070 -Dstatic-llvm \
ci/x86_64-linux-release.sh+1-1
......@@ -64,7 +64,7 @@ stage3-release/bin/zig build \
6464
6565stage3-release/bin/zig build test docs \
6666 --maxrss 21000000000 \
67 -Dlldb=$HOME/deps/lldb-zig/Release-f96d3e6fc/bin/lldb \
67 -Dlldb=$HOME/deps/lldb-zig/Release-62538077d/bin/lldb \
6868 -fqemu \
6969 -fwasmtime \
7070 -Dstatic-llvm \
src/Type.zig+1-1
......@@ -202,6 +202,7 @@ pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error
202202 .C => try writer.writeAll("[*c]"),
203203 .Slice => try writer.writeAll("[]"),
204204 }
205 if (info.flags.is_allowzero and info.flags.size != .C) try writer.writeAll("allowzero ");
205206 if (info.flags.alignment != .none or
206207 info.packed_offset.host_size != 0 or
207208 info.flags.vector_index != .none)
......@@ -229,7 +230,6 @@ pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error
229230 }
230231 if (info.flags.is_const) try writer.writeAll("const ");
231232 if (info.flags.is_volatile) try writer.writeAll("volatile ");
232 if (info.flags.is_allowzero and info.flags.size != .C) try writer.writeAll("allowzero ");
233233
234234 try print(Type.fromInterned(info.child), writer, pt);
235235 return;
src/arch/x86_64/CodeGen.zig+28-9
......@@ -59,6 +59,7 @@ owner: Owner,
5959inline_func: InternPool.Index,
6060mod: *Package.Module,
6161err_msg: ?*ErrorMsg,
62arg_index: u32,
6263args: []MCValue,
6364va_info: union {
6465 sysv: struct {
......@@ -71,7 +72,6 @@ va_info: union {
7172},
7273ret_mcv: InstTracking,
7374fn_type: Type,
74arg_index: u32,
7575src_loc: Zcu.LazySrcLoc,
7676
7777eflags_inst: ?Air.Inst.Index = null,
......@@ -802,11 +802,11 @@ pub fn generate(
802802 .owner = .{ .nav_index = func.owner_nav },
803803 .inline_func = func_index,
804804 .err_msg = null,
805 .arg_index = undefined,
805806 .args = undefined, // populated after `resolveCallingConventionValues`
806807 .va_info = undefined, // populated after `resolveCallingConventionValues`
807808 .ret_mcv = undefined, // populated after `resolveCallingConventionValues`
808809 .fn_type = fn_type,
809 .arg_index = 0,
810810 .src_loc = src_loc,
811811 .end_di_line = func.rbrace_line,
812812 .end_di_column = func.rbrace_column,
......@@ -877,6 +877,7 @@ pub fn generate(
877877 }),
878878 );
879879 function.va_info = switch (cc) {
880 else => undefined,
880881 .SysV => .{ .sysv = .{
881882 .gp_count = call_info.gp_count,
882883 .fp_count = call_info.fp_count,
......@@ -884,7 +885,6 @@ pub fn generate(
884885 .reg_save_area = undefined,
885886 } },
886887 .Win64 => .{ .win64 = .{} },
887 else => undefined,
888888 };
889889
890890 function.gen() catch |err| switch (err) {
......@@ -978,11 +978,11 @@ pub fn generateLazy(
978978 .owner = .{ .lazy_sym = lazy_sym },
979979 .inline_func = undefined,
980980 .err_msg = null,
981 .arg_index = undefined,
981982 .args = undefined,
982983 .va_info = undefined,
983984 .ret_mcv = undefined,
984985 .fn_type = undefined,
985 .arg_index = undefined,
986986 .src_loc = src_loc,
987987 .end_di_line = undefined, // no debug info yet
988988 .end_di_column = undefined, // no debug info yet
......@@ -1482,6 +1482,8 @@ fn asmOpOnly(self: *Self, tag: Mir.Inst.FixedTag) !void {
14821482}
14831483
14841484fn asmPseudo(self: *Self, ops: Mir.Inst.Ops) !void {
1485 assert(std.mem.startsWith(u8, @tagName(ops), "pseudo_") and
1486 std.mem.endsWith(u8, @tagName(ops), "_none"));
14851487 _ = try self.addInst(.{
14861488 .tag = .pseudo,
14871489 .ops = ops,
......@@ -2101,6 +2103,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
21012103 const ip = &mod.intern_pool;
21022104 const air_tags = self.air.instructions.items(.tag);
21032105
2106 self.arg_index = 0;
21042107 for (body) |inst| {
21052108 wip_mir_log.debug("{}", .{self.fmtAir(inst)});
21062109 verbose_tracking_log.debug("{}", .{self.fmtTracking()});
......@@ -2114,6 +2117,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
21142117 self.checkInvariantsAfterAirInst(inst, old_air_bookkeeping);
21152118 }
21162119
2120 if (self.arg_index == 0) try self.airDbgVarArgs();
2121 self.arg_index = 0;
21172122 for (body) |inst| {
21182123 if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) continue;
21192124 wip_mir_log.debug("{}", .{self.fmtAir(inst)});
......@@ -12055,11 +12060,25 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
1205512060}
1205612061
1205712062fn airDbgArg(self: *Self, inst: Air.Inst.Index) !void {
12058 defer self.finishAirBookkeeping();
12059 if (self.debug_output == .none) return;
12060 const name = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
12061 if (name != .none) try self.genLocalDebugInfo(inst, self.getResolvedInstValue(inst).short);
12062 if (self.liveness.isUnused(inst)) try self.processDeath(inst);
12063 // skip zero-bit arguments as they don't have a corresponding arg instruction
12064 var arg_index = self.arg_index;
12065 while (self.args[arg_index] == .none) arg_index += 1;
12066 self.arg_index = arg_index + 1;
12067
12068 if (self.debug_output != .none) {
12069 const name = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.name;
12070 if (name != .none) try self.genLocalDebugInfo(inst, self.getResolvedInstValue(inst).short);
12071 if (self.liveness.isUnused(inst)) try self.processDeath(inst);
12072 }
12073 for (self.args[self.arg_index..]) |arg| {
12074 if (arg != .none) break;
12075 } else try self.airDbgVarArgs();
12076 self.finishAirBookkeeping();
12077}
12078
12079fn airDbgVarArgs(self: *Self) !void {
12080 if (self.pt.zcu.typeToFunc(self.fn_type).?.is_var_args)
12081 try self.asmPseudo(.pseudo_dbg_var_args_none);
1206312082}
1206412083
1206512084fn genLocalDebugInfo(
src/arch/x86_64/Emit.zig+7
......@@ -384,6 +384,13 @@ pub fn emitMir(emit: *Emit) Error!void {
384384 .none => {},
385385 }
386386 },
387 .pseudo_dbg_var_args_none => {
388 switch (emit.debug_output) {
389 .dwarf => |dw| try dw.genVarArgsDebugInfo(),
390 .plan9 => {},
391 .none => {},
392 }
393 },
387394 .pseudo_dead_none => {},
388395 },
389396 }
src/arch/x86_64/Lower.zig+1
......@@ -279,6 +279,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct {
279279 .pseudo_dbg_local_aro,
280280 .pseudo_dbg_local_af,
281281 .pseudo_dbg_local_am,
282 .pseudo_dbg_var_args_none,
282283 .pseudo_dead_none,
283284 => {},
284285 else => unreachable,
src/arch/x86_64/Mir.zig+2
......@@ -924,6 +924,8 @@ pub const Inst = struct {
924924 /// Local argument or variable.
925925 /// Uses `ax` payload with extra data of type `Memory`.
926926 pseudo_dbg_local_am,
927 /// Remaining arguments are varargs.
928 pseudo_dbg_var_args_none,
927929
928930 /// Tombstone
929931 /// Emitter should skip this instruction.
src/link/Dwarf.zig+92-42
......@@ -1108,6 +1108,12 @@ pub const WipNav = struct {
11081108 wip_nav.any_children = true;
11091109 }
11101110
1111 pub fn genVarArgsDebugInfo(wip_nav: *WipNav) UpdateError!void {
1112 assert(wip_nav.func != .none);
1113 try wip_nav.abbrevCode(.is_var_args);
1114 wip_nav.any_children = true;
1115 }
1116
11111117 pub fn advancePCAndLine(
11121118 wip_nav: *WipNav,
11131119 delta_line: i33,
......@@ -1380,7 +1386,11 @@ pub const WipNav = struct {
13801386 fn enumConstValue(
13811387 wip_nav: *WipNav,
13821388 loaded_enum: InternPool.LoadedEnumType,
1383 abbrev_code: std.enums.EnumFieldStruct(std.builtin.Signedness, AbbrevCode, null),
1389 abbrev_code: struct {
1390 sdata: AbbrevCode,
1391 udata: AbbrevCode,
1392 block: AbbrevCode,
1393 },
13841394 field_index: usize,
13851395 ) UpdateError!void {
13861396 const zcu = wip_nav.pt.zcu;
......@@ -1390,20 +1400,15 @@ pub const WipNav = struct {
13901400 .comptime_int_type => .signed,
13911401 else => Type.fromInterned(loaded_enum.tag_ty).intInfo(zcu).signedness,
13921402 };
1393 try wip_nav.abbrevCode(switch (signedness) {
1394 inline .signed, .unsigned => |ct_signedness| @field(abbrev_code, @tagName(ct_signedness)),
1395 });
1396 if (loaded_enum.values.len > 0) switch (ip.indexToKey(loaded_enum.values.get(ip)[field_index]).int.storage) {
1397 .u64 => |value| switch (signedness) {
1398 .signed => try sleb128(diw, value),
1399 .unsigned => try uleb128(diw, value),
1400 },
1401 .i64 => |value| switch (signedness) {
1402 .signed => try sleb128(diw, value),
1403 .unsigned => unreachable,
1404 },
1405 .big_int => |big_int| {
1406 const bits = big_int.bitCountTwosCompForSignedness(signedness);
1403 if (loaded_enum.values.len > 0) {
1404 var big_int_space: InternPool.Key.Int.Storage.BigIntSpace = undefined;
1405 const big_int = ip.indexToKey(loaded_enum.values.get(ip)[field_index]).int.storage.toBigInt(&big_int_space);
1406 const bits = @max(1, big_int.bitCountTwosCompForSignedness(signedness));
1407 if (bits <= 64) {
1408 try wip_nav.abbrevCode(switch (signedness) {
1409 .signed => abbrev_code.sdata,
1410 .unsigned => abbrev_code.udata,
1411 });
14071412 try wip_nav.debug_info.ensureUnusedCapacity(wip_nav.dwarf.gpa, std.math.divCeil(usize, bits, 7) catch unreachable);
14081413 var bit: usize = 0;
14091414 var carry: u1 = 1;
......@@ -1412,11 +1417,8 @@ pub const WipNav = struct {
14121417 const limb_index = bit / limb_bits;
14131418 const limb_shift: std.math.Log2Int(std.math.big.Limb) = @intCast(bit % limb_bits);
14141419 const low_abs_part: u7 = @truncate(big_int.limbs[limb_index] >> limb_shift);
1415 const abs_part = if (limb_shift > limb_bits - 7) abs_part: {
1416 const next_limb: std.math.big.Limb = if (limb_index + 1 < big_int.limbs.len)
1417 big_int.limbs[limb_index + 1]
1418 else if (big_int.positive) 0 else std.math.maxInt(std.math.big.Limb);
1419 const high_abs_part: u7 = @truncate(next_limb << -%limb_shift);
1420 const abs_part = if (limb_shift > limb_bits - 7 and limb_index + 1 < big_int.limbs.len) abs_part: {
1421 const high_abs_part: u7 = @truncate(big_int.limbs[limb_index + 1] << -%limb_shift);
14201422 break :abs_part high_abs_part | low_abs_part;
14211423 } else low_abs_part;
14221424 const twos_comp_part = if (big_int.positive) abs_part else twos_comp_part: {
......@@ -1425,11 +1427,21 @@ pub const WipNav = struct {
14251427 };
14261428 wip_nav.debug_info.appendAssumeCapacity(@as(u8, if (bit + 7 < bits) 0x80 else 0x00) | twos_comp_part);
14271429 }
1428 },
1429 .lazy_align, .lazy_size => unreachable,
1430 } else {
1431 try wip_nav.abbrevCode(abbrev_code.block);
1432 const bytes = Type.fromInterned(loaded_enum.tag_ty).abiSize(wip_nav.pt);
1433 try uleb128(diw, bytes);
1434 big_int.writeTwosComplement(try wip_nav.debug_info.addManyAsSlice(wip_nav.dwarf.gpa, @intCast(bytes)), wip_nav.dwarf.endian);
1435 }
14301436 } else switch (signedness) {
1431 .signed => try sleb128(diw, field_index),
1432 .unsigned => try uleb128(diw, field_index),
1437 .signed => {
1438 try wip_nav.abbrevCode(abbrev_code.sdata);
1439 try sleb128(diw, field_index);
1440 },
1441 .unsigned => {
1442 try wip_nav.abbrevCode(abbrev_code.udata);
1443 try uleb128(diw, field_index);
1444 },
14331445 }
14341446 }
14351447
......@@ -2267,8 +2279,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
22672279 try wip_nav.refType(Type.fromInterned(loaded_enum.tag_ty));
22682280 for (0..loaded_enum.names.len) |field_index| {
22692281 try wip_nav.enumConstValue(loaded_enum, .{
2270 .signed = .signed_enum_field,
2271 .unsigned = .unsigned_enum_field,
2282 .sdata = .signed_enum_field,
2283 .udata = .unsigned_enum_field,
2284 .block = .big_enum_field,
22722285 }, field_index);
22732286 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
22742287 }
......@@ -2367,8 +2380,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
23672380
23682381 for (0..loaded_union.field_types.len) |field_index| {
23692382 try wip_nav.enumConstValue(loaded_tag, .{
2370 .signed = .signed_tagged_union_field,
2371 .unsigned = .unsigned_tagged_union_field,
2383 .sdata = .signed_tagged_union_field,
2384 .udata = .unsigned_tagged_union_field,
2385 .block = .big_tagged_union_field,
23722386 }, field_index);
23732387 {
23742388 try wip_nav.abbrevCode(.struct_field);
......@@ -2831,17 +2845,18 @@ fn updateType(
28312845 },
28322846 .enum_type => {
28332847 const loaded_enum = ip.loadEnumType(type_index);
2834 try wip_nav.abbrevCode(.enum_type);
2848 try wip_nav.abbrevCode(if (loaded_enum.names.len > 0) .enum_type else .empty_enum_type);
28352849 try wip_nav.strp(name);
28362850 try wip_nav.refType(Type.fromInterned(loaded_enum.tag_ty));
28372851 for (0..loaded_enum.names.len) |field_index| {
28382852 try wip_nav.enumConstValue(loaded_enum, .{
2839 .signed = .signed_enum_field,
2840 .unsigned = .unsigned_enum_field,
2853 .sdata = .signed_enum_field,
2854 .udata = .unsigned_enum_field,
2855 .block = .big_enum_field,
28412856 }, field_index);
28422857 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
28432858 }
2844 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2859 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
28452860 },
28462861 .func_type => |func_type| {
28472862 const is_nullary = func_type.param_types.len == 0 and !func_type.is_var_args;
......@@ -3049,7 +3064,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
30493064 }
30503065 },
30513066 .@"packed" => {
3052 try wip_nav.abbrevCode(.packed_struct_type);
3067 try wip_nav.abbrevCode(if (loaded_struct.field_types.len > 0) .packed_struct_type else .empty_packed_struct_type);
30533068 try wip_nav.strp(name);
30543069 try wip_nav.refType(Type.fromInterned(loaded_struct.backingIntTypeUnordered(ip)));
30553070 var field_bit_offset: u16 = 0;
......@@ -3061,27 +3076,28 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
30613076 try uleb128(diw, field_bit_offset);
30623077 field_bit_offset += @intCast(field_type.bitSize(pt));
30633078 }
3064 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3079 if (loaded_struct.field_types.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
30653080 },
30663081 }
30673082 },
30683083 .enum_type => {
30693084 const loaded_enum = ip.loadEnumType(type_index);
3070 try wip_nav.abbrevCode(.enum_type);
3085 try wip_nav.abbrevCode(if (loaded_enum.names.len > 0) .enum_type else .empty_enum_type);
30713086 try wip_nav.strp(name);
30723087 try wip_nav.refType(Type.fromInterned(loaded_enum.tag_ty));
30733088 for (0..loaded_enum.names.len) |field_index| {
30743089 try wip_nav.enumConstValue(loaded_enum, .{
3075 .signed = .signed_enum_field,
3076 .unsigned = .unsigned_enum_field,
3090 .sdata = .signed_enum_field,
3091 .udata = .unsigned_enum_field,
3092 .block = .big_enum_field,
30773093 }, field_index);
30783094 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
30793095 }
3080 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3096 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
30813097 },
30823098 .union_type => {
30833099 const loaded_union = ip.loadUnionType(type_index);
3084 try wip_nav.abbrevCode(.union_type);
3100 try wip_nav.abbrevCode(if (loaded_union.field_types.len > 0) .union_type else .empty_union_type);
30853101 try wip_nav.strp(name);
30863102 const union_layout = pt.getUnionLayout(loaded_union);
30873103 try uleb128(diw, union_layout.abi_size);
......@@ -3103,8 +3119,9 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
31033119
31043120 for (0..loaded_union.field_types.len) |field_index| {
31053121 try wip_nav.enumConstValue(loaded_tag, .{
3106 .signed = .signed_tagged_union_field,
3107 .unsigned = .unsigned_tagged_union_field,
3122 .sdata = .signed_tagged_union_field,
3123 .udata = .unsigned_tagged_union_field,
3124 .block = .big_tagged_union_field,
31083125 }, field_index);
31093126 {
31103127 try wip_nav.abbrevCode(.struct_field);
......@@ -3130,7 +3147,7 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
31303147 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
31313148 field_type.abiAlignment(pt).toByteUnits().?);
31323149 }
3133 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3150 if (loaded_union.field_types.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
31343151 },
31353152 .opaque_type => {
31363153 try wip_nav.abbrevCode(.namespace_struct_type);
......@@ -3570,6 +3587,7 @@ const AbbrevCode = enum {
35703587 file,
35713588 signed_enum_field,
35723589 unsigned_enum_field,
3590 big_enum_field,
35733591 generated_field,
35743592 struct_field,
35753593 struct_field_comptime,
......@@ -3578,6 +3596,7 @@ const AbbrevCode = enum {
35783596 tagged_union,
35793597 signed_tagged_union_field,
35803598 unsigned_tagged_union_field,
3599 big_tagged_union_field,
35813600 tagged_union_default_field,
35823601 void_type,
35833602 numeric_type,
......@@ -3596,7 +3615,9 @@ const AbbrevCode = enum {
35963615 namespace_struct_type,
35973616 struct_type,
35983617 packed_struct_type,
3618 empty_packed_struct_type,
35993619 union_type,
3620 empty_union_type,
36003621 empty_inlined_func,
36013622 inlined_func,
36023623 local_arg,
......@@ -3778,6 +3799,13 @@ const AbbrevCode = enum {
37783799 .{ .name, .strp },
37793800 },
37803801 },
3802 .big_enum_field = .{
3803 .tag = .enumerator,
3804 .attrs = &.{
3805 .{ .const_value, .block },
3806 .{ .name, .strp },
3807 },
3808 },
37813809 .generated_field = .{
37823810 .tag = .member,
37833811 .attrs = &.{
......@@ -3841,6 +3869,13 @@ const AbbrevCode = enum {
38413869 .{ .discr_value, .udata },
38423870 },
38433871 },
3872 .big_tagged_union_field = .{
3873 .tag = .variant,
3874 .children = true,
3875 .attrs = &.{
3876 .{ .discr_value, .block },
3877 },
3878 },
38443879 .tagged_union_default_field = .{
38453880 .tag = .variant,
38463881 .children = true,
......@@ -3971,6 +4006,13 @@ const AbbrevCode = enum {
39714006 .{ .type, .ref_addr },
39724007 },
39734008 },
4009 .empty_packed_struct_type = .{
4010 .tag = .structure_type,
4011 .attrs = &.{
4012 .{ .name, .strp },
4013 .{ .type, .ref_addr },
4014 },
4015 },
39744016 .union_type = .{
39754017 .tag = .union_type,
39764018 .children = true,
......@@ -3980,6 +4022,14 @@ const AbbrevCode = enum {
39804022 .{ .alignment, .udata },
39814023 },
39824024 },
4025 .empty_union_type = .{
4026 .tag = .union_type,
4027 .attrs = &.{
4028 .{ .name, .strp },
4029 .{ .byte_size, .udata },
4030 .{ .alignment, .udata },
4031 },
4032 },
39834033 .empty_inlined_func = .{
39844034 .tag = .inlined_subroutine,
39854035 .attrs = &.{
src/link/Elf/ZigObject.zig+61-5
......@@ -169,7 +169,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
169169 if (self.dwarf) |*dwarf| {
170170 const pt: Zcu.PerThread = .{ .zcu = elf_file.base.comp.module.?, .tid = tid };
171171 try dwarf.flushModule(pt);
172 try dwarf.resolveRelocs();
173172
174173 const gpa = elf_file.base.comp.gpa;
175174 const cpu_arch = elf_file.getTarget().cpu.arch;
......@@ -209,7 +208,28 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
209208
210209 const relocs = &self.relocs.items[atom_ptr.relocsShndx().?];
211210 for (sect.units.items) |*unit| {
212 try relocs.ensureUnusedCapacity(gpa, unit.cross_section_relocs.items.len);
211 try relocs.ensureUnusedCapacity(gpa, unit.cross_unit_relocs.items.len +
212 unit.cross_section_relocs.items.len);
213 for (unit.cross_unit_relocs.items) |reloc| {
214 const target_unit = sect.getUnit(reloc.target_unit);
215 const r_offset = unit.off + reloc.source_off;
216 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
217 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sect, dwarf).off
218 else
219 0));
220 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
221 log.debug(" {s} <- r_off={x}, r_add={x}, r_type={}", .{
222 self.symbol(sym_index).name(elf_file),
223 r_offset,
224 r_addend,
225 relocation.fmtRelocType(r_type, cpu_arch),
226 });
227 atom_ptr.addRelocAssumeCapacity(.{
228 .r_offset = r_offset,
229 .r_addend = r_addend,
230 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
231 }, self);
232 }
213233 for (unit.cross_section_relocs.items) |reloc| {
214234 const target_sym_index = switch (reloc.target_sec) {
215235 .debug_abbrev => self.debug_abbrev_index.?,
......@@ -246,7 +266,45 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
246266 for (unit.entries.items) |*entry| {
247267 const entry_off = unit.off + unit.header_len + entry.off;
248268
249 try relocs.ensureUnusedCapacity(gpa, entry.cross_section_relocs.items.len);
269 try relocs.ensureUnusedCapacity(gpa, entry.cross_entry_relocs.items.len +
270 entry.cross_unit_relocs.items.len + entry.cross_section_relocs.items.len +
271 entry.external_relocs.items.len);
272 for (entry.cross_entry_relocs.items) |reloc| {
273 const r_offset = entry_off + reloc.source_off;
274 const r_addend: i64 = @intCast(unit.off + reloc.target_off + unit.header_len + unit.getEntry(reloc.target_entry).assertNonEmpty(unit, sect, dwarf).off);
275 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
276 log.debug(" {s} <- r_off={x}, r_add={x}, r_type={}", .{
277 self.symbol(sym_index).name(elf_file),
278 r_offset,
279 r_addend,
280 relocation.fmtRelocType(r_type, cpu_arch),
281 });
282 atom_ptr.addRelocAssumeCapacity(.{
283 .r_offset = r_offset,
284 .r_addend = r_addend,
285 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
286 }, self);
287 }
288 for (entry.cross_unit_relocs.items) |reloc| {
289 const target_unit = sect.getUnit(reloc.target_unit);
290 const r_offset = entry_off + reloc.source_off;
291 const r_addend: i64 = @intCast(target_unit.off + reloc.target_off + (if (reloc.target_entry.unwrap()) |target_entry|
292 target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sect, dwarf).off
293 else
294 0));
295 const r_type = relocation.dwarf.crossSectionRelocType(dwarf.format, cpu_arch);
296 log.debug(" {s} <- r_off={x}, r_add={x}, r_type={}", .{
297 self.symbol(sym_index).name(elf_file),
298 r_offset,
299 r_addend,
300 relocation.fmtRelocType(r_type, cpu_arch),
301 });
302 atom_ptr.addRelocAssumeCapacity(.{
303 .r_offset = r_offset,
304 .r_addend = r_addend,
305 .r_info = (@as(u64, @intCast(sym_index)) << 32) | r_type,
306 }, self);
307 }
250308 for (entry.cross_section_relocs.items) |reloc| {
251309 const target_sym_index = switch (reloc.target_sec) {
252310 .debug_abbrev => self.debug_abbrev_index.?,
......@@ -279,8 +337,6 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !voi
279337 .r_info = (@as(u64, @intCast(target_sym_index)) << 32) | r_type,
280338 }, self);
281339 }
282
283 try relocs.ensureUnusedCapacity(gpa, entry.external_relocs.items.len);
284340 for (entry.external_relocs.items) |reloc| {
285341 const target_sym = self.symbol(reloc.target_sym);
286342 const r_offset = entry_off + reloc.source_off;
test/src/Debugger.zig+18-18
......@@ -205,26 +205,26 @@ pub fn addTestsForTarget(db: *Debugger, target: Target) void {
205205 \\ single_volatile: *volatile u32 = @ptrFromInt(0x1018),
206206 \\ single_const_volatile: *const volatile u32 = @ptrFromInt(0x101c),
207207 \\ single_allowzero: *allowzero u32 = @ptrFromInt(0x1020),
208 \\ single_const_allowzero: *const allowzero u32 = @ptrFromInt(0x1024),
209 \\ single_volatile_allowzero: *volatile allowzero u32 = @ptrFromInt(0x1028),
210 \\ single_const_volatile_allowzero: *const volatile allowzero u32 = @ptrFromInt(0x102c),
208 \\ single_allowzero_const: *allowzero const u32 = @ptrFromInt(0x1024),
209 \\ single_allowzero_volatile: *allowzero volatile u32 = @ptrFromInt(0x1028),
210 \\ single_allowzero_const_volatile: *allowzero const volatile u32 = @ptrFromInt(0x102c),
211211 \\
212212 \\ many: [*]u32 = @ptrFromInt(0x2010),
213213 \\ many_const: [*]const u32 = @ptrFromInt(0x2014),
214214 \\ many_volatile: [*]volatile u32 = @ptrFromInt(0x2018),
215215 \\ many_const_volatile: [*]const volatile u32 = @ptrFromInt(0x201c),
216216 \\ many_allowzero: [*]allowzero u32 = @ptrFromInt(0x2020),
217 \\ many_const_allowzero: [*]const allowzero u32 = @ptrFromInt(0x2024),
218 \\ many_volatile_allowzero: [*]volatile allowzero u32 = @ptrFromInt(0x2028),
219 \\ many_const_volatile_allowzero: [*]const volatile allowzero u32 = @ptrFromInt(0x202c),
217 \\ many_allowzero_const: [*]allowzero const u32 = @ptrFromInt(0x2024),
218 \\ many_allowzero_volatile: [*]allowzero volatile u32 = @ptrFromInt(0x2028),
219 \\ many_allowzero_const_volatile: [*]allowzero const volatile u32 = @ptrFromInt(0x202c),
220220 \\ slice: []u32 = array[0..1],
221221 \\ slice_const: []const u32 = array[0..2],
222222 \\ slice_volatile: []volatile u32 = array[0..3],
223223 \\ slice_const_volatile: []const volatile u32 = array[0..4],
224224 \\ slice_allowzero: []allowzero u32 = array[4..4],
225 \\ slice_const_allowzero: []const allowzero u32 = array[4..5],
226 \\ slice_volatile_allowzero: []volatile allowzero u32 = array[4..6],
227 \\ slice_const_volatile_allowzero: []const volatile allowzero u32 = array[4..7],
225 \\ slice_allowzero_const: []allowzero const u32 = array[4..5],
226 \\ slice_allowzero_volatile: []allowzero volatile u32 = array[4..6],
227 \\ slice_allowzero_const_volatile: []allowzero const volatile u32 = array[4..7],
228228 \\
229229 \\ c: [*c]u32 = @ptrFromInt(0x4010),
230230 \\ c_const: [*c]const u32 = @ptrFromInt(0x4014),
......@@ -254,17 +254,17 @@ pub fn addTestsForTarget(db: *Debugger, target: Target) void {
254254 \\ (*volatile u32) single_volatile = 0x0000000000001018
255255 \\ (*const volatile u32) single_const_volatile = 0x000000000000101c
256256 \\ (*allowzero u32) single_allowzero = 0x0000000000001020
257 \\ (*const allowzero u32) single_const_allowzero = 0x0000000000001024
258 \\ (*volatile allowzero u32) single_volatile_allowzero = 0x0000000000001028
259 \\ (*const volatile allowzero u32) single_const_volatile_allowzero = 0x000000000000102c
257 \\ (*allowzero const u32) single_allowzero_const = 0x0000000000001024
258 \\ (*allowzero volatile u32) single_allowzero_volatile = 0x0000000000001028
259 \\ (*allowzero const volatile u32) single_allowzero_const_volatile = 0x000000000000102c
260260 \\ ([*]u32) many = 0x0000000000002010
261261 \\ ([*]const u32) many_const = 0x0000000000002014
262262 \\ ([*]volatile u32) many_volatile = 0x0000000000002018
263263 \\ ([*]const volatile u32) many_const_volatile = 0x000000000000201c
264264 \\ ([*]allowzero u32) many_allowzero = 0x0000000000002020
265 \\ ([*]const allowzero u32) many_const_allowzero = 0x0000000000002024
266 \\ ([*]volatile allowzero u32) many_volatile_allowzero = 0x0000000000002028
267 \\ ([*]const volatile allowzero u32) many_const_volatile_allowzero = 0x000000000000202c
265 \\ ([*]allowzero const u32) many_allowzero_const = 0x0000000000002024
266 \\ ([*]allowzero volatile u32) many_allowzero_volatile = 0x0000000000002028
267 \\ ([*]allowzero const volatile u32) many_allowzero_const_volatile = 0x000000000000202c
268268 \\ ([]u32) slice = len=1 {
269269 \\ (u32) [0] = 3010
270270 \\ }
......@@ -284,14 +284,14 @@ pub fn addTestsForTarget(db: *Debugger, target: Target) void {
284284 \\ (u32) [3] = 3022
285285 \\ }
286286 \\ ([]allowzero u32) slice_allowzero = len=0 {}
287 \\ ([]const allowzero u32) slice_const_allowzero = len=1 {
287 \\ ([]allowzero const u32) slice_allowzero_const = len=1 {
288288 \\ (u32) [0] = 3026
289289 \\ }
290 \\ ([]volatile allowzero u32) slice_volatile_allowzero = len=2 {
290 \\ ([]allowzero volatile u32) slice_allowzero_volatile = len=2 {
291291 \\ (u32) [0] = 3026
292292 \\ (u32) [1] = 3030
293293 \\ }
294 \\ ([]const volatile allowzero u32) slice_const_volatile_allowzero = len=3 {
294 \\ ([]allowzero const volatile u32) slice_allowzero_const_volatile = len=3 {
295295 \\ (u32) [0] = 3026
296296 \\ (u32) [1] = 3030
297297 \\ (u32) [2] = 3034