Compilation: enable the x86_64 backend by default for debug builds

Closes #22257
This commit is contained in:
Jacob Young 2025-06-04 01:42:40 -04:00 committed by Andrew Kelley
parent e96d86064e
commit 5986bdf868
15 changed files with 216 additions and 104 deletions

View File

@ -437,8 +437,8 @@ pub fn build(b: *std.Build) !void {
.skip_non_native = skip_non_native, .skip_non_native = skip_non_native,
.skip_libc = skip_libc, .skip_libc = skip_libc,
.use_llvm = use_llvm, .use_llvm = use_llvm,
// 2520100864 was observed on an x86_64-linux-gnu host. // 2923515904 was observed on an x86_64-linux-gnu host.
.max_rss = 2772110950, .max_rss = 3100000000,
})); }));
test_modules_step.dependOn(tests.addModuleTests(b, .{ test_modules_step.dependOn(tests.addModuleTests(b, .{

View File

@ -7225,7 +7225,7 @@ fn buildOutputFromZig(
assert(out.* == null); assert(out.* == null);
out.* = crt_file; out.* = crt_file;
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
} }
pub const CrtFileOptions = struct { pub const CrtFileOptions = struct {
@ -7349,7 +7349,7 @@ pub fn build_crt_file(
try comp.updateSubCompilation(sub_compilation, misc_task_tag, prog_node); try comp.updateSubCompilation(sub_compilation, misc_task_tag, prog_node);
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
{ {
comp.mutex.lock(); comp.mutex.lock();
@ -7359,11 +7359,14 @@ pub fn build_crt_file(
} }
} }
pub fn queueLinkTaskMode(comp: *Compilation, path: Cache.Path, output_mode: std.builtin.OutputMode) void { pub fn queueLinkTaskMode(comp: *Compilation, path: Cache.Path, config: *const Compilation.Config) void {
comp.queueLinkTasks(switch (output_mode) { comp.queueLinkTasks(switch (config.output_mode) {
.Exe => unreachable, .Exe => unreachable,
.Obj => &.{.{ .load_object = path }}, .Obj => &.{.{ .load_object = path }},
.Lib => &.{.{ .load_archive = path }}, .Lib => &.{switch (config.link_mode) {
.static => .{ .load_archive = path },
.dynamic => .{ .load_dso = path },
}},
}); });
} }

View File

@ -195,10 +195,7 @@ pub fn resolve(options: Options) ResolveError!Config {
// Note that using the LLVM backend does not necessarily mean using LLVM libraries. // Note that using the LLVM backend does not necessarily mean using LLVM libraries.
// For example, Zig can emit .bc and .ll files directly, and this is still considered // For example, Zig can emit .bc and .ll files directly, and this is still considered
// using "the LLVM backend". // using "the LLVM backend".
const use_llvm = b: { const can_use_llvm = b: {
// If we have no zig code to compile, no need for LLVM.
if (!options.have_zcu) break :b false;
// If emitting to LLVM bitcode object format, must use LLVM backend. // If emitting to LLVM bitcode object format, must use LLVM backend.
if (options.emit_llvm_ir or options.emit_llvm_bc) { if (options.emit_llvm_ir or options.emit_llvm_bc) {
if (options.use_llvm == false) if (options.use_llvm == false)
@ -237,6 +234,13 @@ pub fn resolve(options: Options) ResolveError!Config {
break :b !target_util.selfHostedBackendIsAsRobustAsLlvm(target); break :b !target_util.selfHostedBackendIsAsRobustAsLlvm(target);
}; };
const use_llvm = b: {
// If we have no zig code to compile, no need for LLVM.
if (!options.have_zcu) break :b false;
break :b can_use_llvm;
};
if (options.emit_bin and options.have_zcu) { if (options.emit_bin and options.have_zcu) {
if (!use_lib_llvm and use_llvm) { if (!use_lib_llvm and use_llvm) {
// Explicit request to use LLVM to produce an object file, but without // Explicit request to use LLVM to produce an object file, but without
@ -273,7 +277,7 @@ pub fn resolve(options: Options) ResolveError!Config {
} }
if (options.use_lld) |x| break :b x; if (options.use_lld) |x| break :b x;
break :b true; break :b can_use_llvm;
}; };
// Make a decision on whether to use Clang or Aro for translate-c and compiling C files. // Make a decision on whether to use Clang or Aro for translate-c and compiling C files.

View File

@ -378,9 +378,9 @@ pub fn emitMir(emit: *Emit) Error!void {
}; };
break :stack_value &loc_buf[0]; break :stack_value &loc_buf[0];
} } }, } } },
.pseudo_dbg_local_as => .{ mir_inst.data.as.air_inst, .{ .addr = .{ .pseudo_dbg_local_as => .{ mir_inst.data.as.air_inst, .{
.sym = mir_inst.data.as.sym_index, .addr_reloc = mir_inst.data.as.sym_index,
} } }, } },
.pseudo_dbg_local_aso => loc: { .pseudo_dbg_local_aso => loc: {
const sym_off = emit.lower.mir.extraData( const sym_off = emit.lower.mir.extraData(
bits.SymbolOffset, bits.SymbolOffset,
@ -388,7 +388,7 @@ pub fn emitMir(emit: *Emit) Error!void {
).data; ).data;
break :loc .{ mir_inst.data.ax.air_inst, .{ .plus = .{ break :loc .{ mir_inst.data.ax.air_inst, .{ .plus = .{
sym: { sym: {
loc_buf[0] = .{ .addr = .{ .sym = sym_off.sym_index } }; loc_buf[0] = .{ .addr_reloc = sym_off.sym_index };
break :sym &loc_buf[0]; break :sym &loc_buf[0];
}, },
off: { off: {
@ -437,7 +437,7 @@ pub fn emitMir(emit: *Emit) Error!void {
.none => .{ .constu = 0 }, .none => .{ .constu = 0 },
.reg => |reg| .{ .breg = reg.dwarfNum() }, .reg => |reg| .{ .breg = reg.dwarfNum() },
.frame, .table, .rip_inst => unreachable, .frame, .table, .rip_inst => unreachable,
.reloc => |sym_index| .{ .addr = .{ .sym = sym_index } }, .reloc => |sym_index| .{ .addr_reloc = sym_index },
}; };
break :base &loc_buf[0]; break :base &loc_buf[0];
}, },

View File

@ -308,7 +308,7 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
assert(comp.libcxx_static_lib == null); assert(comp.libcxx_static_lib == null);
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.libcxx_static_lib = crt_file; comp.libcxx_static_lib = crt_file;
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
} }
pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildError!void { pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildError!void {
@ -504,7 +504,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
assert(comp.libcxxabi_static_lib == null); assert(comp.libcxxabi_static_lib == null);
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.libcxxabi_static_lib = crt_file; comp.libcxxabi_static_lib = crt_file;
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
} }
pub fn addCxxArgs( pub fn addCxxArgs(

View File

@ -325,7 +325,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
}; };
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
assert(comp.tsan_lib == null); assert(comp.tsan_lib == null);
comp.tsan_lib = crt_file; comp.tsan_lib = crt_file;
} }

View File

@ -195,7 +195,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
}; };
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
assert(comp.libunwind_static_lib == null); assert(comp.libunwind_static_lib == null);
comp.libunwind_static_lib = crt_file; comp.libunwind_static_lib = crt_file;
} }

View File

@ -278,7 +278,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
errdefer comp.gpa.free(basename); errdefer comp.gpa.free(basename);
const crt_file = try sub_compilation.toCrtFile(); const crt_file = try sub_compilation.toCrtFile();
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode); comp.queueLinkTaskMode(crt_file.full_object_path, &config);
{ {
comp.mutex.lock(); comp.mutex.lock();
defer comp.mutex.unlock(); defer comp.mutex.unlock();

View File

@ -785,6 +785,7 @@ const Entry = struct {
} }
const Index = enum(u32) { const Index = enum(u32) {
//got_proc,
_, _,
const Optional = enum(u32) { const Optional = enum(u32) {
@ -1040,7 +1041,7 @@ const Entry = struct {
const symbol = zo.symbol(reloc.target_sym); const symbol = zo.symbol(reloc.target_sym);
try dwarf.resolveReloc( try dwarf.resolveReloc(
entry_off + reloc.source_off, entry_off + reloc.source_off,
@bitCast(symbol.address(.{}, elf_file) + @as(i64, @intCast(reloc.target_off)) - @bitCast(symbol.address(.{}, elf_file) + @as(i64, @intCast(@intFromEnum(reloc.target_off))) -
if (symbol.flags.is_tls) elf_file.dtpAddress() else 0), if (symbol.flags.is_tls) elf_file.dtpAddress() else 0),
@intFromEnum(dwarf.address_size), @intFromEnum(dwarf.address_size),
); );
@ -1051,7 +1052,7 @@ const Entry = struct {
const ref = zo.getSymbolRef(reloc.target_sym, macho_file); const ref = zo.getSymbolRef(reloc.target_sym, macho_file);
try dwarf.resolveReloc( try dwarf.resolveReloc(
entry_off + reloc.source_off, entry_off + reloc.source_off,
ref.getSymbol(macho_file).?.getAddress(.{}, macho_file), ref.getSymbol(macho_file).?.getAddress(.{}, macho_file) + @as(i64, @intCast(@intFromEnum(reloc.target_off))),
@intFromEnum(dwarf.address_size), @intFromEnum(dwarf.address_size),
); );
} }
@ -1080,18 +1081,39 @@ const CrossSectionReloc = struct {
const ExternalReloc = struct { const ExternalReloc = struct {
source_off: u32 = 0, source_off: u32 = 0,
target_sym: u32, target_sym: u32,
target_off: u64 = 0, target_off: enum(u64) {
none = 0,
got = std.math.maxInt(i64) + 1,
_,
pub fn rel(off: u64) @This() {
const res: @This() = @enumFromInt(off);
switch (res) {
.none => {},
_ => {},
.got => unreachable, // assertion failure
}
return res;
}
} = .none,
}; };
pub const Loc = union(enum) { pub const Loc = union(enum) {
empty, empty,
addr: union(enum) { sym: u32 }, addr_reloc: u32,
got_reloc: u32,
deref: *const Loc,
constu: u64, constu: u64,
consts: i64, consts: i64,
plus: Bin, plus: Bin,
reg: u32, reg: u32,
breg: u32, breg: u32,
push_object_address, push_object_address,
call: struct {
args: []const Loc = &.{},
unit: Unit.Index,
entry: Entry.Index,
},
form_tls_address: *const Loc, form_tls_address: *const Loc,
implicit_value: []const u8, implicit_value: []const u8,
stack_value: *const Loc, stack_value: *const Loc,
@ -1136,11 +1158,17 @@ pub const Loc = union(enum) {
const writer = adapter.writer(); const writer = adapter.writer();
switch (loc) { switch (loc) {
.empty => {}, .empty => {},
.addr => |addr| { .addr_reloc => |sym_index| {
try writer.writeByte(DW.OP.addr); try writer.writeByte(DW.OP.addr);
switch (addr) { try adapter.addrSym(sym_index);
.sym => |sym_index| try adapter.addrSym(sym_index), },
} .deref => |addr| {
try addr.write(adapter);
try writer.writeByte(DW.OP.deref);
},
.got_reloc => |sym_index| {
try writer.writeByte(DW.OP.const4s);
try adapter.gotSym(sym_index);
}, },
.constu => |constu| if (std.math.cast(u5, constu)) |lit| { .constu => |constu| if (std.math.cast(u5, constu)) |lit| {
try writer.writeByte(@as(u8, DW.OP.lit0) + lit); try writer.writeByte(@as(u8, DW.OP.lit0) + lit);
@ -1225,6 +1253,11 @@ pub const Loc = union(enum) {
try sleb128(writer, 0); try sleb128(writer, 0);
}, },
.push_object_address => try writer.writeByte(DW.OP.push_object_address), .push_object_address => try writer.writeByte(DW.OP.push_object_address),
.call => |call| {
for (call.args) |arg| try arg.write(adapter);
try writer.writeByte(DW.OP.call_ref);
try adapter.infoEntry(call.unit, call.entry);
},
.form_tls_address => |addr| { .form_tls_address => |addr| {
try addr.write(adapter); try addr.write(adapter);
try writer.writeByte(DW.OP.form_tls_address); try writer.writeByte(DW.OP.form_tls_address);
@ -1385,12 +1418,12 @@ pub const Cfa = union(enum) {
}, },
.def_cfa_expression => |expr| { .def_cfa_expression => |expr| {
try writer.writeByte(DW.CFA.def_cfa_expression); try writer.writeByte(DW.CFA.def_cfa_expression);
try wip_nav.frameExprloc(expr); try wip_nav.frameExprLoc(expr);
}, },
.expression => |reg_expr| { .expression => |reg_expr| {
try writer.writeByte(DW.CFA.expression); try writer.writeByte(DW.CFA.expression);
try uleb128(writer, reg_expr.reg); try uleb128(writer, reg_expr.reg);
try wip_nav.frameExprloc(reg_expr.expr); try wip_nav.frameExprLoc(reg_expr.expr);
}, },
.val_offset => |reg_off| { .val_offset => |reg_off| {
const factored_off = @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor); const factored_off = @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
@ -1407,7 +1440,7 @@ pub const Cfa = union(enum) {
.val_expression => |reg_expr| { .val_expression => |reg_expr| {
try writer.writeByte(DW.CFA.val_expression); try writer.writeByte(DW.CFA.val_expression);
try uleb128(writer, reg_expr.reg); try uleb128(writer, reg_expr.reg);
try wip_nav.frameExprloc(reg_expr.expr); try wip_nav.frameExprLoc(reg_expr.expr);
}, },
.escape => |bytes| try writer.writeAll(bytes), .escape => |bytes| try writer.writeAll(bytes),
} }
@ -1471,7 +1504,7 @@ pub const WipNav = struct {
}); });
try wip_nav.strp(name); try wip_nav.strp(name);
try wip_nav.refType(ty); try wip_nav.refType(ty);
try wip_nav.infoExprloc(loc); try wip_nav.infoExprLoc(loc);
wip_nav.any_children = true; wip_nav.any_children = true;
} }
@ -1733,6 +1766,9 @@ pub const WipNav = struct {
fn endian(_: ExprLocCounter) std.builtin.Endian { fn endian(_: ExprLocCounter) std.builtin.Endian {
return @import("builtin").cpu.arch.endian(); return @import("builtin").cpu.arch.endian();
} }
fn gotSym(counter: *ExprLocCounter, _: u32) error{}!void {
counter.stream.bytes_written += 4;
}
fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void { fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {
counter.stream.bytes_written += @intFromEnum(counter.address_size); counter.stream.bytes_written += @intFromEnum(counter.address_size);
} }
@ -1741,7 +1777,7 @@ pub const WipNav = struct {
} }
}; };
fn infoExprloc(wip_nav: *WipNav, loc: Loc) UpdateError!void { fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
var counter: ExprLocCounter = .init(wip_nav.dwarf); var counter: ExprLocCounter = .init(wip_nav.dwarf);
try loc.write(&counter); try loc.write(&counter);
@ -1753,6 +1789,14 @@ pub const WipNav = struct {
fn endian(ctx: @This()) std.builtin.Endian { fn endian(ctx: @This()) std.builtin.Endian {
return ctx.wip_nav.dwarf.endian; return ctx.wip_nav.dwarf.endian;
} }
fn gotSym(ctx: @This(), sym_index: u32) UpdateError!void {
try ctx.wip_nav.infoExternalReloc(.{
.source_off = @intCast(ctx.wip_nav.debug_info.items.len),
.target_sym = sym_index,
.target_off = .got,
});
try ctx.wip_nav.debug_info.appendNTimes(ctx.wip_nav.dwarf.gpa, 0, 4);
}
fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void { fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
try ctx.wip_nav.infoAddrSym(sym_index, 0); try ctx.wip_nav.infoAddrSym(sym_index, 0);
} }
@ -1768,12 +1812,12 @@ pub const WipNav = struct {
try wip_nav.infoExternalReloc(.{ try wip_nav.infoExternalReloc(.{
.source_off = @intCast(wip_nav.debug_info.items.len), .source_off = @intCast(wip_nav.debug_info.items.len),
.target_sym = sym_index, .target_sym = sym_index,
.target_off = sym_off, .target_off = .rel(sym_off),
}); });
try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size)); try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
} }
fn frameExprloc(wip_nav: *WipNav, loc: Loc) UpdateError!void { fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
var counter: ExprLocCounter = .init(wip_nav.dwarf); var counter: ExprLocCounter = .init(wip_nav.dwarf);
try loc.write(&counter); try loc.write(&counter);
@ -1785,6 +1829,14 @@ pub const WipNav = struct {
fn endian(ctx: @This()) std.builtin.Endian { fn endian(ctx: @This()) std.builtin.Endian {
return ctx.wip_nav.dwarf.endian; return ctx.wip_nav.dwarf.endian;
} }
fn gotSym(ctx: @This(), sym_index: u32) UpdateError!void {
try ctx.wip_nav.frameExternalReloc(.{
.source_off = @intCast(ctx.wip_nav.debug_frame.items.len),
.target_sym = sym_index,
.target_off = .got,
});
try ctx.wip_nav.debug_frame.appendNTimes(ctx.wip_nav.dwarf.gpa, 0, 4);
}
fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void { fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
try ctx.wip_nav.frameAddrSym(sym_index, 0); try ctx.wip_nav.frameAddrSym(sym_index, 0);
} }
@ -1800,7 +1852,7 @@ pub const WipNav = struct {
try wip_nav.frameExternalReloc(.{ try wip_nav.frameExternalReloc(.{
.source_off = @intCast(wip_nav.debug_frame.items.len), .source_off = @intCast(wip_nav.debug_frame.items.len),
.target_sym = sym_index, .target_sym = sym_index,
.target_off = sym_off, .target_off = .rel(sym_off),
}); });
try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size)); try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
} }
@ -2286,6 +2338,7 @@ fn getUnit(dwarf: *Dwarf, mod: *Module) !Unit.Index {
const mod_gop = try dwarf.mods.getOrPut(dwarf.gpa, mod); const mod_gop = try dwarf.mods.getOrPut(dwarf.gpa, mod);
const unit: Unit.Index = @enumFromInt(mod_gop.index); const unit: Unit.Index = @enumFromInt(mod_gop.index);
if (!mod_gop.found_existing) { if (!mod_gop.found_existing) {
{
errdefer _ = dwarf.mods.pop(); errdefer _ = dwarf.mods.pop();
mod_gop.value_ptr.* = .{ mod_gop.value_ptr.* = .{
.root_dir_path = undefined, .root_dir_path = undefined,
@ -2331,6 +2384,36 @@ fn getUnit(dwarf: *Dwarf, mod: *Module) !Unit.Index {
) == unit); ) == unit);
errdefer dwarf.debug_rnglists.section.popUnit(dwarf.gpa); errdefer dwarf.debug_rnglists.section.popUnit(dwarf.gpa);
} }
//if (dwarf.bin_file.cast(.elf)) |elf_file| {
// if (unit == .main) assert(try dwarf.addCommonEntry(unit) == .got_proc);
// if (mod.pic and dwarf.debug_info.section.getUnit(.main).getEntry(.got_proc).len == 0) {
// var wip_nav: WipNav = .{
// .dwarf = dwarf,
// .pt = undefined,
// .unit = .main,
// .entry = .got_proc,
// .any_children = false,
// .func = .none,
// .func_sym_index = undefined,
// .func_high_pc = undefined,
// .blocks = undefined,
// .cfi = undefined,
// .debug_frame = .empty,
// .debug_info = .empty,
// .debug_line = .empty,
// .debug_loclists = .empty,
// .pending_lazy = .empty,
// };
// defer wip_nav.deinit();
// try wip_nav.abbrevCode(.proc);
// try wip_nav.infoExprLoc(.{ .deref = &.{ .plus = .{
// &.empty,
// &.{ .addr_reloc = try elf_file.zigObjectPtr().?.getGlobalSymbol(elf_file, "_GLOBAL_OFFSET_TABLE_", null) },
// } } });
// try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
// }
//}
}
return unit; return unit;
} }
@ -2384,7 +2467,8 @@ fn initWipNavInner(
else => {}, else => {},
} }
const unit = try dwarf.getUnit(file.mod.?); const mod = file.mod.?;
const unit = try dwarf.getUnit(mod);
const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index); const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index);
errdefer _ = if (!nav_gop.found_existing) dwarf.navs.pop(); errdefer _ = if (!nav_gop.found_existing) dwarf.navs.pop();
if (nav_gop.found_existing) { if (nav_gop.found_existing) {
@ -2425,13 +2509,22 @@ fn initWipNavInner(
}, &nav, inst_info.file, &decl); }, &nav, inst_info.file, &decl);
try wip_nav.strp(nav.fqn.toSlice(ip)); try wip_nav.strp(nav.fqn.toSlice(ip));
const ty: Type = nav_val.typeOf(zcu); const ty: Type = nav_val.typeOf(zcu);
const addr: Loc = .{ .addr = .{ .sym = sym_index } }; const addr: Loc = .{ .addr_reloc = sym_index };
const loc: Loc = if (decl.is_threadlocal) .{ .form_tls_address = &addr } else addr; const loc: Loc = loc: {
if (dwarf.bin_file.cast(.elf)) |elf_file| if (decl.linkage == .@"extern" and mod.pic)
// TODO: lldb doesn't support call :(
//.{ .call = .{ .args = &.{.{ .got_reloc = sym_index }}, .unit = .main, .entry = .got_proc } }
break :loc .{ .deref = &.{ .plus = .{
&.{ .addr_reloc = try elf_file.zigObjectPtr().?.getGlobalSymbol(elf_file, "_GLOBAL_OFFSET_TABLE_", null) },
&.{ .got_reloc = sym_index },
} } };
break :loc if (decl.is_threadlocal) .{ .form_tls_address = &addr } else addr;
};
switch (decl.kind) { switch (decl.kind) {
.unnamed_test, .@"test", .decltest, .@"comptime", .@"usingnamespace" => unreachable, .unnamed_test, .@"test", .decltest, .@"comptime", .@"usingnamespace" => unreachable,
.@"const" => { .@"const" => {
const const_ty_reloc_index = try wip_nav.refForward(); const const_ty_reloc_index = try wip_nav.refForward();
try wip_nav.infoExprloc(loc); try wip_nav.infoExprLoc(loc);
try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
ty.abiAlignment(zcu).toByteUnits().?); ty.abiAlignment(zcu).toByteUnits().?);
try diw.writeByte(@intFromBool(decl.linkage != .normal)); try diw.writeByte(@intFromBool(decl.linkage != .normal));
@ -2441,7 +2534,7 @@ fn initWipNavInner(
}, },
.@"var" => { .@"var" => {
try wip_nav.refType(ty); try wip_nav.refType(ty);
try wip_nav.infoExprloc(loc); try wip_nav.infoExprLoc(loc);
try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
ty.abiAlignment(zcu).toByteUnits().?); ty.abiAlignment(zcu).toByteUnits().?);
try diw.writeByte(@intFromBool(decl.linkage != .normal)); try diw.writeByte(@intFromBool(decl.linkage != .normal));
@ -2649,7 +2742,7 @@ pub fn finishWipNavFunc(
.{ .{
.source_off = 1 + @intFromEnum(dwarf.address_size), .source_off = 1 + @intFromEnum(dwarf.address_size),
.target_sym = wip_nav.func_sym_index, .target_sym = wip_nav.func_sym_index,
.target_off = code_size, .target_off = .rel(code_size),
}, },
}); });
try dwarf.debug_rnglists.section.replaceEntry( try dwarf.debug_rnglists.section.replaceEntry(
@ -3838,7 +3931,7 @@ fn updateLazyValue(
byte_offset += base_ptr.byte_offset; byte_offset += base_ptr.byte_offset;
}; };
try wip_nav.abbrevCode(.location_comptime_value); try wip_nav.abbrevCode(.location_comptime_value);
try wip_nav.infoExprloc(.{ .implicit_pointer = .{ try wip_nav.infoExprLoc(.{ .implicit_pointer = .{
.unit = base_unit, .unit = base_unit,
.entry = base_entry, .entry = base_entry,
.offset = byte_offset, .offset = byte_offset,
@ -4360,7 +4453,7 @@ fn refAbbrevCode(dwarf: *Dwarf, abbrev_code: AbbrevCode) UpdateError!@typeInfo(A
assert(abbrev_code != .null); assert(abbrev_code != .null);
const entry: Entry.Index = @enumFromInt(@intFromEnum(abbrev_code)); const entry: Entry.Index = @enumFromInt(@intFromEnum(abbrev_code));
if (dwarf.debug_abbrev.section.getUnit(DebugAbbrev.unit).getEntry(entry).len > 0) return @intFromEnum(abbrev_code); if (dwarf.debug_abbrev.section.getUnit(DebugAbbrev.unit).getEntry(entry).len > 0) return @intFromEnum(abbrev_code);
var debug_abbrev = std.ArrayList(u8).init(dwarf.gpa); var debug_abbrev: std.ArrayList(u8) = .init(dwarf.gpa);
defer debug_abbrev.deinit(); defer debug_abbrev.deinit();
const daw = debug_abbrev.writer(); const daw = debug_abbrev.writer();
const abbrev = AbbrevCode.abbrevs.get(abbrev_code); const abbrev = AbbrevCode.abbrevs.get(abbrev_code);
@ -4422,7 +4515,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
mod_info.root_dir_path = try dwarf.debug_line_str.addString(dwarf, root_dir_path); mod_info.root_dir_path = try dwarf.debug_line_str.addString(dwarf, root_dir_path);
} }
var header = std.ArrayList(u8).init(dwarf.gpa); var header: std.ArrayList(u8) = .init(dwarf.gpa);
defer header.deinit(); defer header.deinit();
if (dwarf.debug_aranges.section.dirty) { if (dwarf.debug_aranges.section.dirty) {
for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| { for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| {
@ -4888,6 +4981,7 @@ const AbbrevCode = enum {
comptime_value_field_comptime_state, comptime_value_field_comptime_state,
comptime_value_elem_runtime_bits, comptime_value_elem_runtime_bits,
comptime_value_elem_comptime_state, comptime_value_elem_comptime_state,
//proc,
const decl_bytes = uleb128Bytes(@intFromEnum(AbbrevCode.decl_instance_func_generic)); const decl_bytes = uleb128Bytes(@intFromEnum(AbbrevCode.decl_instance_func_generic));
comptime { comptime {
@ -5757,6 +5851,12 @@ const AbbrevCode = enum {
.{ .ZIG_comptime_value, .ref_addr }, .{ .ZIG_comptime_value, .ref_addr },
}, },
}, },
//.proc = .{
// .tag = .dwarf_procedure,
// .attrs = &.{
// .{ .location, .exprloc },
// },
//},
.null = undefined, .null = undefined,
}); });
}; };

View File

@ -463,8 +463,11 @@ pub fn flush(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void {
for (entry.external_relocs.items) |reloc| { for (entry.external_relocs.items) |reloc| {
const target_sym = self.symbol(reloc.target_sym); const target_sym = self.symbol(reloc.target_sym);
const r_offset = entry_off + reloc.source_off; const r_offset = entry_off + reloc.source_off;
const r_addend: i64 = @intCast(reloc.target_off); const r_addend: i64 = switch (reloc.target_off) {
const r_type = relocation.dwarf.externalRelocType(target_sym.*, sect_index, dwarf.address_size, cpu_arch); .none, .got => 0,
else => |off| @intCast(@intFromEnum(off)),
};
const r_type = relocation.dwarf.externalRelocType(target_sym.*, reloc.target_off == .got, sect_index, dwarf.address_size, cpu_arch);
atom_ptr.addRelocAssumeCapacity(.{ atom_ptr.addRelocAssumeCapacity(.{
.r_offset = r_offset, .r_offset = r_offset,
.r_addend = r_addend, .r_addend = r_addend,
@ -947,7 +950,7 @@ pub fn getNavVAddr(
.dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{ .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
.source_off = @intCast(reloc_info.offset), .source_off = @intCast(reloc_info.offset),
.target_sym = this_sym_index, .target_sym = this_sym_index,
.target_off = reloc_info.addend, .target_off = .rel(reloc_info.addend),
}), }),
.plan9 => unreachable, .plan9 => unreachable,
.none => unreachable, .none => unreachable,
@ -980,7 +983,7 @@ pub fn getUavVAddr(
.dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{ .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
.source_off = @intCast(reloc_info.offset), .source_off = @intCast(reloc_info.offset),
.target_sym = sym_index, .target_sym = sym_index,
.target_off = reloc_info.addend, .target_off = .rel(reloc_info.addend),
}), }),
.plan9 => unreachable, .plan9 => unreachable,
.none => unreachable, .none => unreachable,

View File

@ -108,12 +108,13 @@ pub const dwarf = struct {
pub fn externalRelocType( pub fn externalRelocType(
target: Symbol, target: Symbol,
is_got: bool,
source_section: Dwarf.Section.Index, source_section: Dwarf.Section.Index,
address_size: Dwarf.AddressSize, address_size: Dwarf.AddressSize,
cpu_arch: std.Target.Cpu.Arch, cpu_arch: std.Target.Cpu.Arch,
) u32 { ) u32 {
return switch (cpu_arch) { return switch (cpu_arch) {
.x86_64 => @intFromEnum(@as(elf.R_X86_64, switch (source_section) { .x86_64 => @intFromEnum(@as(elf.R_X86_64, if (is_got) .GOT32 else switch (source_section) {
else => switch (address_size) { else => switch (address_size) {
.@"32" => if (target.flags.is_tls) .DTPOFF32 else .@"32", .@"32" => if (target.flags.is_tls) .DTPOFF32 else .@"32",
.@"64" => if (target.flags.is_tls) .DTPOFF64 else .@"64", .@"64" => if (target.flags.is_tls) .DTPOFF64 else .@"64",

View File

@ -648,7 +648,7 @@ pub fn getNavVAddr(
.dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{ .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
.source_off = @intCast(reloc_info.offset), .source_off = @intCast(reloc_info.offset),
.target_sym = sym_index, .target_sym = sym_index,
.target_off = reloc_info.addend, .target_off = .rel(reloc_info.addend),
}), }),
.plan9 => unreachable, .plan9 => unreachable,
.none => unreachable, .none => unreachable,
@ -688,7 +688,7 @@ pub fn getUavVAddr(
.dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{ .dwarf => |wip_nav| try wip_nav.infoExternalReloc(.{
.source_off = @intCast(reloc_info.offset), .source_off = @intCast(reloc_info.offset),
.target_sym = sym_index, .target_sym = sym_index,
.target_off = reloc_info.addend, .target_off = .rel(reloc_info.addend),
}), }),
.plan9 => unreachable, .plan9 => unreachable,
.none => unreachable, .none => unreachable,

View File

@ -223,6 +223,10 @@ pub fn hasLldSupport(ofmt: std.Target.ObjectFormat) bool {
/// than or equal to the number of behavior tests as the respective LLVM backend. /// than or equal to the number of behavior tests as the respective LLVM backend.
pub fn selfHostedBackendIsAsRobustAsLlvm(target: std.Target) bool { pub fn selfHostedBackendIsAsRobustAsLlvm(target: std.Target) bool {
if (target.cpu.arch.isSpirV()) return true; if (target.cpu.arch.isSpirV()) return true;
if (target.cpu.arch == .x86_64 and target.ptrBitWidth() == 64) return switch (target.ofmt) {
.elf, .macho => true,
else => false,
};
return false; return false;
} }

View File

@ -1722,6 +1722,7 @@ test "signed zeros are represented properly" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
const S = struct { const S = struct {
fn doTheTest() !void { fn doTheTest() !void {

View File

@ -1113,8 +1113,6 @@ const test_targets = blk: {
.os_tag = .linux, .os_tag = .linux,
.abi = .none, .abi = .none,
}, },
.use_llvm = false,
.use_lld = false,
}, },
.{ .{
.target = .{ .target = .{
@ -1123,8 +1121,6 @@ const test_targets = blk: {
.os_tag = .linux, .os_tag = .linux,
.abi = .none, .abi = .none,
}, },
.use_llvm = false,
.use_lld = false,
.pic = true, .pic = true,
}, },
.{ .{
@ -1134,8 +1130,6 @@ const test_targets = blk: {
.os_tag = .linux, .os_tag = .linux,
.abi = .none, .abi = .none,
}, },
.use_llvm = false,
.use_lld = false,
.strip = true, .strip = true,
}, },
.{ .{
@ -1144,6 +1138,8 @@ const test_targets = blk: {
.os_tag = .linux, .os_tag = .linux,
.abi = .none, .abi = .none,
}, },
.use_llvm = true,
.use_lld = true,
}, },
.{ .{
.target = .{ .target = .{