| ... | ... | @@ -17,6 +17,7 @@ pub fn build(b: *Build) void { |
| 17 | 17 | |
| 18 | 18 | // Exercise linker with LLVM backend |
| 19 | 19 | elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); |
| 20 | elf_step.dependOn(testGcSections(b, .{ .target = musl_target })); |
| 20 | 21 | elf_step.dependOn(testLinkingC(b, .{ .target = musl_target })); |
| 21 | 22 | elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target })); |
| 22 | 23 | elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target })); |
| ... | ... | @@ -38,6 +39,93 @@ fn testEmptyObject(b: *Build, opts: Options) *Step { |
| 38 | 39 | return test_step; |
| 39 | 40 | } |
| 40 | 41 | |
| 42 | fn testGcSections(b: *Build, opts: Options) *Step { |
| 43 | const test_step = addTestStep(b, "gc-sections", opts); |
| 44 | |
| 45 | const obj = addObject(b, opts); |
| 46 | addCppSourceBytes(obj, |
| 47 | \\#include <stdio.h> |
| 48 | \\int two() { return 2; } |
| 49 | \\int live_var1 = 1; |
| 50 | \\int live_var2 = two(); |
| 51 | \\int dead_var1 = 3; |
| 52 | \\int dead_var2 = 4; |
| 53 | \\void live_fn1() {} |
| 54 | \\void live_fn2() { live_fn1(); } |
| 55 | \\void dead_fn1() {} |
| 56 | \\void dead_fn2() { dead_fn1(); } |
| 57 | \\int main() { |
| 58 | \\ printf("%d %d\n", live_var1, live_var2); |
| 59 | \\ live_fn2(); |
| 60 | \\} |
| 61 | ); |
| 62 | obj.link_function_sections = true; |
| 63 | obj.is_linking_libc = true; |
| 64 | obj.is_linking_libcpp = true; |
| 65 | |
| 66 | { |
| 67 | const exe = addExecutable(b, opts); |
| 68 | exe.addObject(obj); |
| 69 | exe.link_gc_sections = false; |
| 70 | exe.is_linking_libc = true; |
| 71 | exe.is_linking_libcpp = true; |
| 72 | |
| 73 | const run = addRunArtifact(exe); |
| 74 | run.expectStdOutEqual("1 2\n"); |
| 75 | test_step.dependOn(&run.step); |
| 76 | |
| 77 | const check = exe.checkObject(); |
| 78 | check.checkInSymtab(); |
| 79 | check.checkContains("live_var1"); |
| 80 | check.checkInSymtab(); |
| 81 | check.checkContains("live_var2"); |
| 82 | check.checkInSymtab(); |
| 83 | check.checkContains("dead_var1"); |
| 84 | check.checkInSymtab(); |
| 85 | check.checkContains("dead_var2"); |
| 86 | check.checkInSymtab(); |
| 87 | check.checkContains("live_fn1"); |
| 88 | check.checkInSymtab(); |
| 89 | check.checkContains("live_fn2"); |
| 90 | check.checkInSymtab(); |
| 91 | check.checkContains("dead_fn1"); |
| 92 | check.checkInSymtab(); |
| 93 | check.checkContains("dead_fn2"); |
| 94 | test_step.dependOn(&check.step); |
| 95 | } |
| 96 | |
| 97 | // { |
| 98 | // const exe = cc(b, opts); |
| 99 | // exe.addFileSource(obj_out.file); |
| 100 | // exe.addArg("-Wl,-gc-sections"); |
| 101 | |
| 102 | // const run = exe.run(); |
| 103 | // run.expectStdOutEqual("1 2\n"); |
| 104 | // test_step.dependOn(run.step()); |
| 105 | |
| 106 | // const check = exe.check(); |
| 107 | // check.checkInSymtab(); |
| 108 | // check.checkContains("live_var1"); |
| 109 | // check.checkInSymtab(); |
| 110 | // check.checkContains("live_var2"); |
| 111 | // check.checkInSymtab(); |
| 112 | // check.checkNotPresent("dead_var1"); |
| 113 | // check.checkInSymtab(); |
| 114 | // check.checkNotPresent("dead_var2"); |
| 115 | // check.checkInSymtab(); |
| 116 | // check.checkContains("live_fn1"); |
| 117 | // check.checkInSymtab(); |
| 118 | // check.checkContains("live_fn2"); |
| 119 | // check.checkInSymtab(); |
| 120 | // check.checkNotPresent("dead_fn1"); |
| 121 | // check.checkInSymtab(); |
| 122 | // check.checkNotPresent("dead_fn2"); |
| 123 | // test_step.dependOn(&check.step); |
| 124 | // } |
| 125 | |
| 126 | return test_step; |
| 127 | } |
| 128 | |
| 41 | 129 | fn testLinkingC(b: *Build, opts: Options) *Step { |
| 42 | 130 | const test_step = addTestStep(b, "linking-c", opts); |
| 43 | 131 | |
| ... | ... | @@ -182,6 +270,16 @@ fn addExecutable(b: *Build, opts: Options) *Compile { |
| 182 | 270 | }); |
| 183 | 271 | } |
| 184 | 272 | |
| 273 | fn addObject(b: *Build, opts: Options) *Compile { |
| 274 | return b.addObject(.{ |
| 275 | .name = "a.o", |
| 276 | .target = opts.target, |
| 277 | .optimize = opts.optimize, |
| 278 | .use_llvm = opts.use_llvm, |
| 279 | .use_lld = false, |
| 280 | }); |
| 281 | } |
| 282 | |
| 185 | 283 | fn addRunArtifact(comp: *Compile) *Run { |
| 186 | 284 | const b = comp.step.owner; |
| 187 | 285 | const run = b.addRunArtifact(comp); |