| ... | ... | @@ -31,6 +31,7 @@ pub fn build(b: *Build) void { |
| 31 | 31 | // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false })); |
| 32 | 32 | |
| 33 | 33 | // Exercise linker with LLVM backend |
| 34 | elf_step.dependOn(testAbsSymbols(b, .{ .target = musl_target })); |
| 34 | 35 | elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target })); |
| 35 | 36 | elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target })); |
| 36 | 37 | elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); |
| ... | ... | @@ -41,6 +42,7 @@ pub fn build(b: *Build) void { |
| 41 | 42 | elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target })); |
| 42 | 43 | |
| 43 | 44 | elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker })); |
| 45 | elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker })); |
| 44 | 46 | elf_step.dependOn(testDsoPlt(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker })); |
| 45 | 47 | elf_step.dependOn(testDsoUndef(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker })); |
| 46 | 48 | elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target, .dynamic_linker = dynamic_linker })); |
| ... | ... | @@ -50,6 +52,46 @@ pub fn build(b: *Build) void { |
| 50 | 52 | } |
| 51 | 53 | } |
| 52 | 54 | |
| 55 | fn testAbsSymbols(b: *Build, opts: Options) *Step { |
| 56 | const test_step = addTestStep(b, "abs-symbols", opts); |
| 57 | |
| 58 | const obj = addObject(b, "obj", opts); |
| 59 | addAsmSourceBytes(obj, |
| 60 | \\.globl foo |
| 61 | \\foo = 0x800008 |
| 62 | ); |
| 63 | |
| 64 | const exe = addExecutable(b, "test", opts); |
| 65 | addCSourceBytes(exe, |
| 66 | \\#include <signal.h> |
| 67 | \\#include <stdio.h> |
| 68 | \\#include <stdlib.h> |
| 69 | \\#include <ucontext.h> |
| 70 | \\#include <assert.h> |
| 71 | \\void handler(int signum, siginfo_t *info, void *ptr) { |
| 72 | \\ assert((size_t)info->si_addr == 0x800008); |
| 73 | \\ exit(0); |
| 74 | \\} |
| 75 | \\extern int foo; |
| 76 | \\int main() { |
| 77 | \\ struct sigaction act; |
| 78 | \\ act.sa_flags = SA_SIGINFO | SA_RESETHAND; |
| 79 | \\ act.sa_sigaction = handler; |
| 80 | \\ sigemptyset(&act.sa_mask); |
| 81 | \\ sigaction(SIGSEGV, &act, 0); |
| 82 | \\ foo = 5; |
| 83 | \\ return 0; |
| 84 | \\} |
| 85 | , &.{}); |
| 86 | exe.addObject(obj); |
| 87 | exe.linkLibC(); |
| 88 | |
| 89 | const run = addRunArtifact(exe); |
| 90 | test_step.dependOn(&run.step); |
| 91 | |
| 92 | return test_step; |
| 93 | } |
| 94 | |
| 53 | 95 | fn testAsNeeded(b: *Build, opts: Options) *Step { |
| 54 | 96 | const test_step = addTestStep(b, "as-needed", opts); |
| 55 | 97 | |
| ... | ... | @@ -62,7 +104,7 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { |
| 62 | 104 | \\ return 0; |
| 63 | 105 | \\} |
| 64 | 106 | , &.{}); |
| 65 | | main_o.is_linking_libc = true; |
| 107 | main_o.linkLibC(); |
| 66 | 108 | |
| 67 | 109 | const libfoo = addSharedLibrary(b, "foo", opts); |
| 68 | 110 | addCSourceBytes(libfoo, "int foo() { return 42; }", &.{"-fPIC"}); |
| ... | ... | @@ -88,7 +130,7 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { |
| 88 | 130 | exe.linkSystemLibrary2("baz", .{ .needed = true }); |
| 89 | 131 | exe.addLibraryPath(libbaz.getEmittedBinDirectory()); |
| 90 | 132 | exe.addRPath(libbaz.getEmittedBinDirectory()); |
| 91 | | exe.is_linking_libc = true; |
| 133 | exe.linkLibC(); |
| 92 | 134 | |
| 93 | 135 | const run = addRunArtifact(exe); |
| 94 | 136 | run.expectStdOutEqual("42\n"); |
| ... | ... | @@ -114,7 +156,7 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { |
| 114 | 156 | exe.linkSystemLibrary2("baz", .{ .needed = false }); |
| 115 | 157 | exe.addLibraryPath(libbaz.getEmittedBinDirectory()); |
| 116 | 158 | exe.addRPath(libbaz.getEmittedBinDirectory()); |
| 117 | | exe.is_linking_libc = true; |
| 159 | exe.linkLibC(); |
| 118 | 160 | |
| 119 | 161 | const run = addRunArtifact(exe); |
| 120 | 162 | run.expectStdOutEqual("42\n"); |
| ... | ... | @@ -132,6 +174,54 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { |
| 132 | 174 | return test_step; |
| 133 | 175 | } |
| 134 | 176 | |
| 177 | fn testCanonicalPlt(b: *Build, opts: Options) *Step { |
| 178 | const test_step = addTestStep(b, "canonical-plt", opts); |
| 179 | |
| 180 | const dso = addSharedLibrary(b, "a", opts); |
| 181 | addCSourceBytes(dso, |
| 182 | \\void *foo() { |
| 183 | \\ return foo; |
| 184 | \\} |
| 185 | \\void *bar() { |
| 186 | \\ return bar; |
| 187 | \\} |
| 188 | , &.{"-fPIC"}); |
| 189 | |
| 190 | const b_o = addObject(b, "obj", opts); |
| 191 | addCSourceBytes(b_o, |
| 192 | \\void *bar(); |
| 193 | \\void *baz() { |
| 194 | \\ return bar; |
| 195 | \\} |
| 196 | , &.{"-fPIC"}); |
| 197 | |
| 198 | const main_o = addObject(b, "main", opts); |
| 199 | addCSourceBytes(main_o, |
| 200 | \\#include <assert.h> |
| 201 | \\void *foo(); |
| 202 | \\void *bar(); |
| 203 | \\void *baz(); |
| 204 | \\int main() { |
| 205 | \\ assert(foo == foo()); |
| 206 | \\ assert(bar == bar()); |
| 207 | \\ assert(bar == baz()); |
| 208 | \\} |
| 209 | , &.{"-fno-PIC"}); |
| 210 | main_o.linkLibC(); |
| 211 | |
| 212 | const exe = addExecutable(b, "main", opts); |
| 213 | exe.addObject(main_o); |
| 214 | exe.addObject(b_o); |
| 215 | exe.linkLibrary(dso); |
| 216 | exe.linkLibC(); |
| 217 | exe.pie = false; |
| 218 | |
| 219 | const run = addRunArtifact(exe); |
| 220 | test_step.dependOn(&run.step); |
| 221 | |
| 222 | return test_step; |
| 223 | } |
| 224 | |
| 135 | 225 | fn testCommonSymbols(b: *Build, opts: Options) *Step { |
| 136 | 226 | const test_step = addTestStep(b, "common-symbols", opts); |
| 137 | 227 | |
| ... | ... | @@ -150,7 +240,7 @@ fn testCommonSymbols(b: *Build, opts: Options) *Step { |
| 150 | 240 | \\ printf("%d %d %d\n", foo, bar, baz); |
| 151 | 241 | \\} |
| 152 | 242 | , &.{"-fcommon"}); |
| 153 | | exe.is_linking_libc = true; |
| 243 | exe.linkLibC(); |
| 154 | 244 | |
| 155 | 245 | const run = addRunArtifact(exe); |
| 156 | 246 | run.expectStdOutEqual("0 5 42\n"); |
| ... | ... | @@ -173,7 +263,7 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step { |
| 173 | 263 | \\ printf("%d %d %d %d\n", foo, bar, baz, two ? two() : -1); |
| 174 | 264 | \\} |
| 175 | 265 | , &.{"-fcommon"}); |
| 176 | | a_o.is_linking_libc = true; |
| 266 | a_o.linkLibC(); |
| 177 | 267 | |
| 178 | 268 | const b_o = addObject(b, "b", opts); |
| 179 | 269 | addCSourceBytes(b_o, "int foo = 5;", &.{"-fcommon"}); |
| ... | ... | @@ -196,7 +286,7 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step { |
| 196 | 286 | const exe = addExecutable(b, "test", opts); |
| 197 | 287 | exe.addObject(a_o); |
| 198 | 288 | exe.linkLibrary(lib); |
| 199 | | exe.is_linking_libc = true; |
| 289 | exe.linkLibC(); |
| 200 | 290 | |
| 201 | 291 | const run = addRunArtifact(exe); |
| 202 | 292 | run.expectStdOutEqual("5 0 0 -1\n"); |
| ... | ... | @@ -218,7 +308,7 @@ fn testCommonSymbolsInArchive(b: *Build, opts: Options) *Step { |
| 218 | 308 | const exe = addExecutable(b, "test", opts); |
| 219 | 309 | exe.addObject(a_o); |
| 220 | 310 | exe.linkLibrary(lib); |
| 221 | | exe.is_linking_libc = true; |
| 311 | exe.linkLibC(); |
| 222 | 312 | |
| 223 | 313 | const run = addRunArtifact(exe); |
| 224 | 314 | run.expectStdOutEqual("5 0 7 2\n"); |
| ... | ... | @@ -245,7 +335,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step { |
| 245 | 335 | \\ real_hello(); |
| 246 | 336 | \\} |
| 247 | 337 | , &.{"-fPIC"}); |
| 248 | | dso.is_linking_libc = true; |
| 338 | dso.linkLibC(); |
| 249 | 339 | |
| 250 | 340 | const exe = addExecutable(b, "test", opts); |
| 251 | 341 | addCSourceBytes(exe, |
| ... | ... | @@ -259,7 +349,7 @@ fn testDsoPlt(b: *Build, opts: Options) *Step { |
| 259 | 349 | \\} |
| 260 | 350 | , &.{}); |
| 261 | 351 | exe.linkLibrary(dso); |
| 262 | | exe.is_linking_libc = true; |
| 352 | exe.linkLibC(); |
| 263 | 353 | |
| 264 | 354 | const run = addRunArtifact(exe); |
| 265 | 355 | run.expectStdOutEqual("Hello WORLD\n"); |
| ... | ... | @@ -277,7 +367,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step { |
| 277 | 367 | \\int bar = 5; |
| 278 | 368 | \\int baz() { return foo; } |
| 279 | 369 | , &.{"-fPIC"}); |
| 280 | | dso.is_linking_libc = true; |
| 370 | dso.linkLibC(); |
| 281 | 371 | |
| 282 | 372 | const obj = addObject(b, "obj", opts); |
| 283 | 373 | addCSourceBytes(obj, "int foo = 3;", &.{}); |
| ... | ... | @@ -294,7 +384,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step { |
| 294 | 384 | \\ return bar - 5; |
| 295 | 385 | \\} |
| 296 | 386 | , &.{}); |
| 297 | | exe.is_linking_libc = true; |
| 387 | exe.linkLibC(); |
| 298 | 388 | |
| 299 | 389 | const run = addRunArtifact(exe); |
| 300 | 390 | run.expectExitCode(0); |
| ... | ... | @@ -314,7 +404,7 @@ fn testEmptyObject(b: *Build, opts: Options) *Step { |
| 314 | 404 | const exe = addExecutable(b, "test", opts); |
| 315 | 405 | addCSourceBytes(exe, "int main() { return 0; }", &.{}); |
| 316 | 406 | addCSourceBytes(exe, "", &.{}); |
| 317 | | exe.is_linking_libc = true; |
| 407 | exe.linkLibC(); |
| 318 | 408 | |
| 319 | 409 | const run = addRunArtifact(exe); |
| 320 | 410 | run.expectExitCode(0); |
| ... | ... | @@ -345,15 +435,15 @@ fn testGcSections(b: *Build, opts: Options) *Step { |
| 345 | 435 | , &.{}); |
| 346 | 436 | obj.link_function_sections = true; |
| 347 | 437 | obj.link_data_sections = true; |
| 348 | | obj.is_linking_libc = true; |
| 349 | | obj.is_linking_libcpp = true; |
| 438 | obj.linkLibC(); |
| 439 | obj.linkLibCpp(); |
| 350 | 440 | |
| 351 | 441 | { |
| 352 | 442 | const exe = addExecutable(b, "test", opts); |
| 353 | 443 | exe.addObject(obj); |
| 354 | 444 | exe.link_gc_sections = false; |
| 355 | | exe.is_linking_libc = true; |
| 356 | | exe.is_linking_libcpp = true; |
| 445 | exe.linkLibC(); |
| 446 | exe.linkLibCpp(); |
| 357 | 447 | |
| 358 | 448 | const run = addRunArtifact(exe); |
| 359 | 449 | run.expectStdOutEqual("1 2\n"); |
| ... | ... | @@ -383,8 +473,8 @@ fn testGcSections(b: *Build, opts: Options) *Step { |
| 383 | 473 | const exe = addExecutable(b, "test", opts); |
| 384 | 474 | exe.addObject(obj); |
| 385 | 475 | exe.link_gc_sections = true; |
| 386 | | exe.is_linking_libc = true; |
| 387 | | exe.is_linking_libcpp = true; |
| 476 | exe.linkLibC(); |
| 477 | exe.linkLibCpp(); |
| 388 | 478 | |
| 389 | 479 | const run = addRunArtifact(exe); |
| 390 | 480 | run.expectStdOutEqual("1 2\n"); |
| ... | ... | @@ -434,7 +524,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step { |
| 434 | 524 | \\} |
| 435 | 525 | , &.{"-fPIC"}); |
| 436 | 526 | dso.link_function_sections = true; |
| 437 | | dso.is_linking_libc = true; |
| 527 | dso.linkLibC(); |
| 438 | 528 | |
| 439 | 529 | const check = dso.checkObject(); |
| 440 | 530 | check.checkInSymtab(); |
| ... | ... | @@ -451,7 +541,7 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step { |
| 451 | 541 | \\int main() { greet(); } |
| 452 | 542 | , &.{}); |
| 453 | 543 | exe.linkLibrary(dso); |
| 454 | | exe.is_linking_libc = true; |
| 544 | exe.linkLibC(); |
| 455 | 545 | |
| 456 | 546 | const run = addRunArtifact(exe); |
| 457 | 547 | run.expectStdOutEqual("Hello world"); |
| ... | ... | @@ -485,7 +575,7 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step { |
| 485 | 575 | \\} |
| 486 | 576 | , &.{}); |
| 487 | 577 | exe.link_function_sections = true; |
| 488 | | exe.is_linking_libc = true; |
| 578 | exe.linkLibC(); |
| 489 | 579 | |
| 490 | 580 | const run = addRunArtifact(exe); |
| 491 | 581 | run.expectStdOutEqual("Hello world"); |
| ... | ... | @@ -514,7 +604,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step { |
| 514 | 604 | \\ return 0; |
| 515 | 605 | \\} |
| 516 | 606 | , &.{}); |
| 517 | | exe.is_linking_libc = true; |
| 607 | exe.linkLibC(); |
| 518 | 608 | |
| 519 | 609 | const run = addRunArtifact(exe); |
| 520 | 610 | run.expectStdOutEqual("Hello World!\n"); |
| ... | ... | @@ -543,8 +633,8 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step { |
| 543 | 633 | \\ return 0; |
| 544 | 634 | \\} |
| 545 | 635 | , &.{}); |
| 546 | | exe.is_linking_libc = true; |
| 547 | | exe.is_linking_libcpp = true; |
| 636 | exe.linkLibC(); |
| 637 | exe.linkLibCpp(); |
| 548 | 638 | |
| 549 | 639 | const run = addRunArtifact(exe); |
| 550 | 640 | run.expectStdOutEqual("Hello World!\n"); |
| ... | ... | @@ -606,7 +696,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step { |
| 606 | 696 | \\ return 0; |
| 607 | 697 | \\} |
| 608 | 698 | , &.{}); |
| 609 | | exe.is_linking_libc = true; |
| 699 | exe.linkLibC(); |
| 610 | 700 | |
| 611 | 701 | const run = addRunArtifact(exe); |
| 612 | 702 | run.expectStdOutEqual( |