Skip to content

Commit abb48f3

Browse files
committed
stage2: add support for -fbuild-id,-fno-build-id
closes #3047
1 parent d7f632f commit abb48f3

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

build.zig

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub fn build(b: *Builder) !void {
145145

146146
const exe = b.addExecutable("zig", main_file);
147147
exe.strip = strip;
148+
exe.build_id = !strip;
148149
exe.install();
149150
exe.setBuildMode(mode);
150151
exe.setTarget(target);

lib/std/build.zig

+14
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,12 @@ pub const LibExeObjStep = struct {
15491549

15501550
valgrind_support: ?bool = null,
15511551
each_lib_rpath: ?bool = null,
1552+
/// On ELF targets, this will emit a link section called ".note.gnu.build-id"
1553+
/// which can be used to coordinate a stripped binary with its debug symbols.
1554+
/// As an example, the bloaty project refuses to work unless its inputs have
1555+
/// build ids, in order to prevent accidental mismatches.
1556+
/// The default is to not include this section because it slows down linking.
1557+
build_id: ?bool = null,
15521558

15531559
/// Create a .eh_frame_hdr section and a PT_GNU_EH_FRAME segment in the ELF
15541560
/// file.
@@ -2953,6 +2959,14 @@ pub const LibExeObjStep = struct {
29532959
}
29542960
}
29552961

2962+
if (self.build_id) |build_id| {
2963+
if (build_id) {
2964+
try zig_args.append("-fbuild-id");
2965+
} else {
2966+
try zig_args.append("-fno-build-id");
2967+
}
2968+
}
2969+
29562970
if (self.override_lib_dir) |dir| {
29572971
try zig_args.append("--zig-lib-dir");
29582972
try zig_args.append(builder.pathFromRoot(dir));

src/Compilation.zig

+3
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ pub const InitOptions = struct {
757757
linker_global_base: ?u64 = null,
758758
linker_export_symbol_names: []const []const u8 = &.{},
759759
each_lib_rpath: ?bool = null,
760+
build_id: ?bool = null,
760761
disable_c_depfile: bool = false,
761762
linker_z_nodelete: bool = false,
762763
linker_z_notext: bool = false,
@@ -1640,6 +1641,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16401641
.skip_linker_dependencies = options.skip_linker_dependencies,
16411642
.parent_compilation_link_libc = options.parent_compilation_link_libc,
16421643
.each_lib_rpath = options.each_lib_rpath orelse options.is_native_os,
1644+
.build_id = options.build_id orelse false,
16431645
.cache_mode = cache_mode,
16441646
.disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,
16451647
.subsystem = options.subsystem,
@@ -2340,6 +2342,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
23402342
man.hash.addListOfBytes(comp.bin_file.options.lib_dirs);
23412343
man.hash.addListOfBytes(comp.bin_file.options.rpath_list);
23422344
man.hash.add(comp.bin_file.options.each_lib_rpath);
2345+
man.hash.add(comp.bin_file.options.build_id);
23432346
man.hash.add(comp.bin_file.options.skip_linker_dependencies);
23442347
man.hash.add(comp.bin_file.options.z_nodelete);
23452348
man.hash.add(comp.bin_file.options.z_notext);

src/link.zig

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub const Options = struct {
146146
skip_linker_dependencies: bool,
147147
parent_compilation_link_libc: bool,
148148
each_lib_rpath: bool,
149+
build_id: bool,
149150
disable_lld_caching: bool,
150151
is_test: bool,
151152
use_stage1: bool,

src/link/Elf.zig

+8-1
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
13111311
// We can skip hashing libc and libc++ components that we are in charge of building from Zig
13121312
// installation sources because they are always a product of the compiler version + target information.
13131313
man.hash.addOptionalBytes(self.base.options.entry);
1314-
man.hash.add(stack_size);
13151314
man.hash.addOptional(self.base.options.image_base_override);
13161315
man.hash.add(gc_sections);
13171316
man.hash.add(self.base.options.eh_frame_hdr);
@@ -1320,6 +1319,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
13201319
man.hash.addListOfBytes(self.base.options.lib_dirs);
13211320
man.hash.addListOfBytes(self.base.options.rpath_list);
13221321
man.hash.add(self.base.options.each_lib_rpath);
1322+
if (self.base.options.output_mode == .Exe) {
1323+
man.hash.add(stack_size);
1324+
man.hash.add(self.base.options.build_id);
1325+
}
13231326
man.hash.add(self.base.options.skip_linker_dependencies);
13241327
man.hash.add(self.base.options.z_nodelete);
13251328
man.hash.add(self.base.options.z_notext);
@@ -1450,6 +1453,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
14501453
if (self.base.options.output_mode == .Exe) {
14511454
try argv.append("-z");
14521455
try argv.append(try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}));
1456+
1457+
if (self.base.options.build_id) {
1458+
try argv.append("--build-id");
1459+
}
14531460
}
14541461

14551462
if (self.base.options.image_base_override) |image_base| {

src/main.zig

+21-3
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ const usage_build_generic =
423423
\\ -fno-each-lib-rpath Prevent adding rpath for each used dynamic library
424424
\\ -fallow-shlib-undefined Allows undefined symbols in shared libraries
425425
\\ -fno-allow-shlib-undefined Disallows undefined symbols in shared libraries
426+
\\ -fbuild-id Helps coordinate stripped binaries with debug symbols
427+
\\ -fno-build-id (default) Saves a bit of time linking
426428
\\ --eh-frame-hdr Enable C++ exception handling by passing --eh-frame-hdr to linker
427429
\\ --emit-relocs Enable output of relocation sections for post build tools
428430
\\ -z [arg] Set linker extension flags
@@ -671,6 +673,7 @@ fn buildOutputType(
671673
var link_eh_frame_hdr = false;
672674
var link_emit_relocs = false;
673675
var each_lib_rpath: ?bool = null;
676+
var build_id: ?bool = null;
674677
var sysroot: ?[]const u8 = null;
675678
var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
676679
var machine_code_model: std.builtin.CodeModel = .default;
@@ -1030,6 +1033,10 @@ fn buildOutputType(
10301033
each_lib_rpath = true;
10311034
} else if (mem.eql(u8, arg, "-fno-each-lib-rpath")) {
10321035
each_lib_rpath = false;
1036+
} else if (mem.eql(u8, arg, "-fbuild-id")) {
1037+
build_id = true;
1038+
} else if (mem.eql(u8, arg, "-fno-build-id")) {
1039+
build_id = false;
10331040
} else if (mem.eql(u8, arg, "--enable-cache")) {
10341041
enable_cache = true;
10351042
} else if (mem.eql(u8, arg, "--test-cmd-bin")) {
@@ -1415,10 +1422,20 @@ fn buildOutputType(
14151422
while (split_it.next()) |linker_arg| {
14161423
// Handle nested-joined args like `-Wl,-rpath=foo`.
14171424
// Must be prefixed with 1 or 2 dashes.
1418-
if (linker_arg.len >= 3 and linker_arg[0] == '-' and linker_arg[2] != '-') {
1425+
if (linker_arg.len >= 3 and
1426+
linker_arg[0] == '-' and
1427+
linker_arg[2] != '-')
1428+
{
14191429
if (mem.indexOfScalar(u8, linker_arg, '=')) |equals_pos| {
1420-
try linker_args.append(linker_arg[0..equals_pos]);
1421-
try linker_args.append(linker_arg[equals_pos + 1 ..]);
1430+
const key = linker_arg[0..equals_pos];
1431+
const value = linker_arg[equals_pos + 1 ..];
1432+
if (mem.eql(u8, key, "build-id")) {
1433+
build_id = true;
1434+
warn("ignoring build-id style argument: '{s}'", .{value});
1435+
continue;
1436+
}
1437+
try linker_args.append(key);
1438+
try linker_args.append(value);
14221439
continue;
14231440
}
14241441
}
@@ -2727,6 +2744,7 @@ fn buildOutputType(
27272744
.stack_report = stack_report,
27282745
.is_test = arg_mode == .zig_test,
27292746
.each_lib_rpath = each_lib_rpath,
2747+
.build_id = build_id,
27302748
.test_evented_io = test_evented_io,
27312749
.test_filter = test_filter,
27322750
.test_name_prefix = test_name_prefix,

0 commit comments

Comments
 (0)