Skip to content

Commit 66253e5

Browse files
committed
link/elf: populate current Zig version in .comment; test
1 parent bc0d84b commit 66253e5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/link/Elf.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -3329,7 +3329,7 @@ fn checkDuplicates(self: *Elf) !void {
33293329
pub fn addCommentString(self: *Elf) !void {
33303330
const msec_index = try self.getOrCreateMergeSection(".comment", elf.SHF_MERGE | elf.SHF_STRINGS, elf.SHT_PROGBITS);
33313331
const msec = self.mergeSection(msec_index);
3332-
const res = try msec.insertZ(self.base.comp.gpa, "zig version x.x.x"); // TODO get actual version
3332+
const res = try msec.insertZ(self.base.comp.gpa, "zig " ++ builtin.zig_version_string);
33333333
if (res.found_existing) return;
33343334
const msub_index = try self.addMergeSubsection();
33353335
const msub = self.mergeSubsection(msub_index);

test/link/elf.zig

+34
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
6161
elf_step.dependOn(testAbsSymbols(b, .{ .target = musl_target }));
6262
elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));
6363
elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target }));
64+
elf_step.dependOn(testCommentString(b, .{ .target = musl_target }));
6465
elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target }));
6566
elf_step.dependOn(testEntryPoint(b, .{ .target = musl_target }));
6667
elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
@@ -83,6 +84,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
8384
elf_step.dependOn(testAsNeeded(b, .{ .target = gnu_target }));
8485
// https://github.com/ziglang/zig/issues/17430
8586
// elf_step.dependOn(testCanonicalPlt(b, .{ .target = gnu_target }));
87+
elf_step.dependOn(testCommentString(b, .{ .target = gnu_target }));
8688
elf_step.dependOn(testCopyrel(b, .{ .target = gnu_target }));
8789
// https://github.com/ziglang/zig/issues/17430
8890
// elf_step.dependOn(testCopyrelAlias(b, .{ .target = gnu_target }));
@@ -154,6 +156,8 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
154156
elf_step.dependOn(testThunks(b, .{ .target = aarch64_musl }));
155157

156158
// x86_64 self-hosted backend
159+
elf_step.dependOn(testCommentString(b, .{ .use_llvm = false, .target = default_target }));
160+
elf_step.dependOn(testCommentStringStaticLib(b, .{ .use_llvm = false, .target = default_target }));
157161
elf_step.dependOn(testEmitRelocatable(b, .{ .use_llvm = false, .target = x86_64_musl }));
158162
elf_step.dependOn(testEmitStaticLibZig(b, .{ .use_llvm = false, .target = x86_64_musl }));
159163
elf_step.dependOn(testGcSectionsZig(b, .{ .use_llvm = false, .target = default_target }));
@@ -364,6 +368,36 @@ fn testCanonicalPlt(b: *Build, opts: Options) *Step {
364368
return test_step;
365369
}
366370

371+
fn testCommentString(b: *Build, opts: Options) *Step {
372+
const test_step = addTestStep(b, "comment-string", opts);
373+
374+
const exe = addExecutable(b, opts, .{ .name = "main", .zig_source_bytes =
375+
\\pub fn main() void {}
376+
});
377+
378+
const check = exe.checkObject();
379+
check.dumpSection(".comment");
380+
check.checkContains("zig");
381+
test_step.dependOn(&check.step);
382+
383+
return test_step;
384+
}
385+
386+
fn testCommentStringStaticLib(b: *Build, opts: Options) *Step {
387+
const test_step = addTestStep(b, "comment-string-static-lib", opts);
388+
389+
const lib = addStaticLibrary(b, opts, .{ .name = "lib", .zig_source_bytes =
390+
\\export fn foo() void {}
391+
});
392+
393+
const check = lib.checkObject();
394+
check.dumpSection(".comment");
395+
check.checkContains("zig");
396+
test_step.dependOn(&check.step);
397+
398+
return test_step;
399+
}
400+
367401
fn testCommonSymbols(b: *Build, opts: Options) *Step {
368402
const test_step = addTestStep(b, "common-symbols", opts);
369403

0 commit comments

Comments
 (0)