authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-09 15:40:38+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log1fe0fd69e059e19586290a450c2a48f9777cd4d4
treecdc1d8c9c4fc54956ff192a742156520b5914455
parent9487007ffe332affe3960a5cca6c87759ffdfd22

elf: port more linker tests


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

test/link/elf.zig+173
...@@ -21,6 +21,7 @@ pub fn build(b: *Build) void {...@@ -21,6 +21,7 @@ pub fn build(b: *Build) void {
21 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));21 // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false }));
2222
23 // Exercise linker with LLVM backend23 // Exercise linker with LLVM backend
24 // musl tests
24 elf_step.dependOn(testAbsSymbols(b, .{ .target = musl_target }));25 elf_step.dependOn(testAbsSymbols(b, .{ .target = musl_target }));
25 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));26 elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target }));
26 elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target }));27 elf_step.dependOn(testCommonSymbolsInArchive(b, .{ .target = musl_target }));
...@@ -29,11 +30,17 @@ pub fn build(b: *Build) void {...@@ -29,11 +30,17 @@ pub fn build(b: *Build) void {
29 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));30 elf_step.dependOn(testGcSections(b, .{ .target = musl_target }));
30 elf_step.dependOn(testImageBase(b, .{ .target = musl_target }));31 elf_step.dependOn(testImageBase(b, .{ .target = musl_target }));
31 elf_step.dependOn(testInitArrayOrder(b, .{ .target = musl_target }));32 elf_step.dependOn(testInitArrayOrder(b, .{ .target = musl_target }));
33 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = musl_target }));
34 // https://github.com/ziglang/zig/issues/17449
35 // elf_step.dependOn(testLargeBss(b, .{ .target = musl_target }));
32 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));36 elf_step.dependOn(testLinkingC(b, .{ .target = musl_target }));
33 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));37 elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target }));
34 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));38 elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target }));
39 // https://github.com/ziglang/zig/issues/17451
40 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = musl_target }));
35 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));41 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
3642
43 // glibc tests
37 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target }));44 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target }));
38 // https://github.com/ziglang/zig/issues/1743045 // https://github.com/ziglang/zig/issues/17430
39 // elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target }));46 // elf_step.dependOn(testCanonicalPlt(b, .{ .target = glibc_target }));
...@@ -63,7 +70,13 @@ pub fn build(b: *Build) void {...@@ -63,7 +70,13 @@ pub fn build(b: *Build) void {
63 elf_step.dependOn(testInitArrayOrder(b, .{ .target = glibc_target }));70 elf_step.dependOn(testInitArrayOrder(b, .{ .target = glibc_target }));
64 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target }));71 elf_step.dependOn(testLargeAlignmentDso(b, .{ .target = glibc_target }));
65 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = glibc_target }));72 elf_step.dependOn(testLargeAlignmentExe(b, .{ .target = glibc_target }));
73 elf_step.dependOn(testLargeBss(b, .{ .target = glibc_target }));
74 elf_step.dependOn(testLinkOrder(b, .{ .target = glibc_target }));
75 // https://github.com/ziglang/zig/issues/17451
76 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = glibc_target }));
66 elf_step.dependOn(testPie(b, .{ .target = glibc_target }));77 elf_step.dependOn(testPie(b, .{ .target = glibc_target }));
78 elf_step.dependOn(testPltGot(b, .{ .target = glibc_target }));
79 elf_step.dependOn(testPreinitArray(b, .{ .target = glibc_target }));
67}80}
6881
69fn testAbsSymbols(b: *Build, opts: Options) *Step {82fn testAbsSymbols(b: *Build, opts: Options) *Step {
...@@ -1353,6 +1366,85 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {...@@ -1353,6 +1366,85 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
1353 return test_step;1366 return test_step;
1354}1367}
13551368
1369fn testLargeBss(b: *Build, opts: Options) *Step {
1370 const test_step = addTestStep(b, "large-bss", opts);
1371
1372 const exe = addExecutable(b, "main", opts);
1373 addCSourceBytes(exe,
1374 \\char arr[0x100000000];
1375 \\int main() {
1376 \\ return arr[2000];
1377 \\}
1378 , &.{});
1379 exe.linkLibC();
1380
1381 const run = addRunArtifact(exe);
1382 test_step.dependOn(&run.step);
1383
1384 return test_step;
1385}
1386
1387fn testLinkOrder(b: *Build, opts: Options) *Step {
1388 const test_step = addTestStep(b, "link-order", opts);
1389
1390 const obj = addObject(b, "obj", opts);
1391 addCSourceBytes(obj, "void foo() {}", &.{});
1392 obj.force_pic = true;
1393
1394 const dso = addSharedLibrary(b, "a", opts);
1395 dso.addObject(obj);
1396
1397 const lib = addStaticLibrary(b, "b", opts);
1398 lib.addObject(obj);
1399
1400 const main_o = addObject(b, "main", opts);
1401 addCSourceBytes(main_o,
1402 \\void foo();
1403 \\int main() {
1404 \\ foo();
1405 \\}
1406 , &.{});
1407
1408 // https://github.com/ziglang/zig/issues/17450
1409 // {
1410 // const exe = addExecutable(b, "main1", opts);
1411 // exe.addObject(main_o);
1412 // exe.linkSystemLibrary2("a", .{});
1413 // exe.addLibraryPath(dso.getEmittedBinDirectory());
1414 // exe.addRPath(dso.getEmittedBinDirectory());
1415 // exe.linkSystemLibrary2("b", .{});
1416 // exe.addLibraryPath(lib.getEmittedBinDirectory());
1417 // exe.addRPath(lib.getEmittedBinDirectory());
1418 // exe.linkLibC();
1419 // exe.verbose_link = true;
1420
1421 // const check = exe.checkObject();
1422 // check.checkInDynamicSection();
1423 // check.checkContains("libb.so");
1424 // test_step.dependOn(&check.step);
1425 // }
1426
1427 {
1428 const exe = addExecutable(b, "main2", opts);
1429 exe.addObject(main_o);
1430 exe.linkSystemLibrary2("b", .{});
1431 exe.addLibraryPath(lib.getEmittedBinDirectory());
1432 exe.addRPath(lib.getEmittedBinDirectory());
1433 exe.linkSystemLibrary2("a", .{});
1434 exe.addLibraryPath(dso.getEmittedBinDirectory());
1435 exe.addRPath(dso.getEmittedBinDirectory());
1436 exe.linkLibC();
1437 exe.verbose_link = true;
1438
1439 const check = exe.checkObject();
1440 check.checkInDynamicSection();
1441 check.checkNotPresent("libb.so");
1442 test_step.dependOn(&check.step);
1443 }
1444
1445 return test_step;
1446}
1447
1356fn testLinkingC(b: *Build, opts: Options) *Step {1448fn testLinkingC(b: *Build, opts: Options) *Step {
1357 const test_step = addTestStep(b, "linking-c", opts);1449 const test_step = addTestStep(b, "linking-c", opts);
13581450
...@@ -1438,6 +1530,23 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {...@@ -1438,6 +1530,23 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
1438 return test_step;1530 return test_step;
1439}1531}
14401532
1533fn testNoEhFrameHdr(b: *Build, opts: Options) *Step {
1534 const test_step = addTestStep(b, "no-eh-frame-hdr", opts);
1535
1536 const exe = addExecutable(b, "main", opts);
1537 addCSourceBytes(exe, "int main() { return 0; }", &.{});
1538 exe.link_eh_frame_hdr = false;
1539 exe.linkLibC();
1540
1541 const check = exe.checkObject();
1542 check.checkStart();
1543 check.checkExact("section headers");
1544 check.checkNotPresent("name .eh_frame_hdr");
1545 test_step.dependOn(&check.step);
1546
1547 return test_step;
1548}
1549
1441fn testPie(b: *Build, opts: Options) *Step {1550fn testPie(b: *Build, opts: Options) *Step {
1442 const test_step = addTestStep(b, "hello-pie", opts);1551 const test_step = addTestStep(b, "hello-pie", opts);
14431552
...@@ -1469,6 +1578,70 @@ fn testPie(b: *Build, opts: Options) *Step {...@@ -1469,6 +1578,70 @@ fn testPie(b: *Build, opts: Options) *Step {
1469 return test_step;1578 return test_step;
1470}1579}
14711580
1581fn testPltGot(b: *Build, opts: Options) *Step {
1582 const test_step = addTestStep(b, "plt-got", opts);
1583
1584 const dso = addSharedLibrary(b, "a", opts);
1585 addCSourceBytes(dso,
1586 \\#include <stdio.h>
1587 \\void ignore(void *foo) {}
1588 \\void hello() {
1589 \\ printf("Hello world\n");
1590 \\}
1591 , &.{});
1592 dso.linkLibC();
1593
1594 const exe = addExecutable(b, "main", opts);
1595 addCSourceBytes(exe,
1596 \\void ignore(void *);
1597 \\int hello();
1598 \\void foo() { ignore(hello); }
1599 \\int main() { hello(); }
1600 , &.{});
1601 exe.linkLibrary(dso);
1602 exe.force_pic = true;
1603 exe.linkLibC();
1604
1605 const run = addRunArtifact(exe);
1606 run.expectStdOutEqual("Hello world\n");
1607 test_step.dependOn(&run.step);
1608
1609 return test_step;
1610}
1611
1612fn testPreinitArray(b: *Build, opts: Options) *Step {
1613 const test_step = addTestStep(b, "preinit-array", opts);
1614
1615 {
1616 const obj = addObject(b, "obj", opts);
1617 addCSourceBytes(obj, "void _start() {}", &.{});
1618
1619 const exe = addExecutable(b, "main1", opts);
1620 exe.addObject(obj);
1621
1622 const check = exe.checkObject();
1623 check.checkInDynamicSection();
1624 check.checkNotPresent("PREINIT_ARRAY");
1625 }
1626
1627 {
1628 const exe = addExecutable(b, "main2", opts);
1629 addCSourceBytes(exe,
1630 \\void preinit_fn() {}
1631 \\int main() {}
1632 \\__attribute__((section(".preinit_array")))
1633 \\void *preinit[] = { preinit_fn };
1634 , &.{});
1635 exe.linkLibC();
1636
1637 const check = exe.checkObject();
1638 check.checkInDynamicSection();
1639 check.checkContains("PREINIT_ARRAY");
1640 }
1641
1642 return test_step;
1643}
1644
1472fn testTlsStatic(b: *Build, opts: Options) *Step {1645fn testTlsStatic(b: *Build, opts: Options) *Step {
1473 const test_step = addTestStep(b, "tls-static", opts);1646 const test_step = addTestStep(b, "tls-static", opts);
14741647