Elf: implement linksection

Closes #24330
This commit is contained in:
Jacob Young
2025-09-11 05:20:45 -04:00
committed by Alex Rønne Petersen
parent e14540399c
commit b8bf2780a0
2 changed files with 142 additions and 1 deletions

View File

@@ -73,6 +73,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));
elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
elf_step.dependOn(testLinksection(b, .{ .target = musl_target }));
elf_step.dependOn(testMergeStrings(b, .{ .target = musl_target }));
elf_step.dependOn(testMergeStrings2(b, .{ .target = musl_target }));
// https://github.com/ziglang/zig/issues/17451
@@ -165,6 +166,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
elf_step.dependOn(testLinkingObj(b, .{ .use_llvm = false, .target = default_target }));
elf_step.dependOn(testLinkingStaticLib(b, .{ .use_llvm = false, .target = default_target }));
elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false, .target = default_target }));
elf_step.dependOn(testLinksection(b, .{ .use_llvm = false, .target = default_target }));
elf_step.dependOn(testImportingDataDynamic(b, .{ .use_llvm = false, .target = x86_64_gnu }));
elf_step.dependOn(testImportingDataStatic(b, .{ .use_llvm = false, .target = x86_64_musl }));
@@ -2439,6 +2441,43 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
return test_step;
}
fn testLinksection(b: *Build, opts: Options) *Step {
const test_step = addTestStep(b, "linksection", opts);
const obj = addObject(b, opts, .{ .name = "main", .zig_source_bytes =
\\export var test_global: u32 linksection(".TestGlobal") = undefined;
\\export fn testFn() linksection(".TestFn") callconv(.c) void {
\\ TestGenericFn("A").f();
\\}
\\fn TestGenericFn(comptime suffix: []const u8) type {
\\ return struct {
\\ fn f() linksection(".TestGenFn" ++ suffix) void {}
\\ };
\\}
});
const check = obj.checkObject();
check.checkInSymtab();
check.checkContains("SECTION LOCAL DEFAULT .TestGlobal");
check.checkInSymtab();
check.checkContains("SECTION LOCAL DEFAULT .TestFn");
check.checkInSymtab();
check.checkContains("SECTION LOCAL DEFAULT .TestGenFnA");
check.checkInSymtab();
check.checkContains("OBJECT GLOBAL DEFAULT test_global");
check.checkInSymtab();
check.checkContains("FUNC GLOBAL DEFAULT testFn");
if (opts.optimize == .Debug) {
check.checkInSymtab();
check.checkContains("FUNC LOCAL DEFAULT main.TestGenericFn(");
}
test_step.dependOn(&check.step);
return test_step;
}
// Adapted from https://github.com/rui314/mold/blob/main/test/elf/mergeable-strings.sh
fn testMergeStrings(b: *Build, opts: Options) *Step {
const test_step = addTestStep(b, "merge-strings", opts);