authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-06 00:56:56+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
loge53fa93170e09fa8792e773fe328b4ed87fda9b4
tree7b64743b177c63f295b9a5544b605277acc7e972
parentac03a35e8201f0b1cc5f71699c29820317534914

elf: test basic DSO generation and linking


1 files changed, 59 insertions(+), 0 deletions(-)

test/link/elf.zig+59
......@@ -11,6 +11,11 @@ pub fn build(b: *Build) void {
1111 .os_tag = .linux,
1212 .abi = .musl,
1313 };
14 const glibc_target = CrossTarget{
15 .cpu_arch = .x86_64,
16 .os_tag = .linux,
17 .abi = .gnu,
18 };
1419
1520 // Exercise linker with self-hosted backend (no LLVM)
1621 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
......@@ -24,6 +29,10 @@ pub fn build(b: *Build) void {
2429 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));
2530 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
2631 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
32
33 for (&[_]CrossTarget{ glibc_target, musl_target }) |target| {
34 elf_step.dependOn(testDsoPlt(b, .{ .target = target }));
35 }
2736}
2837
2938fn testCommonSymbols(b: *Build, opts: Options) *Step {
......@@ -122,6 +131,46 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step {
122131 return test_step;
123132}
124133
134fn testDsoPlt(b: *Build, opts: Options) *Step {
135 const test_step = addTestStep(b, "dso-plt", opts);
136
137 const dso = addSharedLibrary(b, opts);
138 addCSourceBytes(dso,
139 \\#include<stdio.h>
140 \\void world() {
141 \\ printf("world\n");
142 \\}
143 \\void real_hello() {
144 \\ printf("Hello ");
145 \\ world();
146 \\}
147 \\void hello() {
148 \\ real_hello();
149 \\}
150 , &.{"-fPIC"});
151 dso.is_linking_libc = true;
152
153 const exe = addExecutable(b, opts);
154 addCSourceBytes(exe,
155 \\#include<stdio.h>
156 \\void world() {
157 \\ printf("WORLD\n");
158 \\}
159 \\void hello();
160 \\int main() {
161 \\ hello();
162 \\}
163 , &.{});
164 exe.linkLibrary(dso);
165 exe.is_linking_libc = true;
166
167 const run = addRunArtifact(exe);
168 run.expectStdOutEqual("Hello WORLD\n");
169 test_step.dependOn(&run.step);
170
171 return test_step;
172}
173
125174fn testEmptyObject(b: *Build, opts: Options) *Step {
126175 const test_step = addTestStep(b, "empty-object", opts);
127176
......@@ -391,6 +440,16 @@ fn addStaticLibrary(b: *Build, opts: Options) *Compile {
391440 });
392441}
393442
443fn addSharedLibrary(b: *Build, opts: Options) *Compile {
444 return b.addSharedLibrary(.{
445 .name = "a.so",
446 .target = opts.target,
447 .optimize = opts.optimize,
448 .use_llvm = opts.use_llvm,
449 .use_lld = false,
450 });
451}
452
394453fn addRunArtifact(comp: *Compile) *Run {
395454 const b = comp.step.owner;
396455 const run = b.addRunArtifact(comp);