authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-09 22:32:03+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log81b68c7465580af94ae9a7c2969eb5a7667392db
treeda1d72d70c40d4a654142721267b414ffee3182f
parent0b37a9c78d5b554fa137cfd5c66c756cf4a64893

elf: port more linker tests


1 files changed, 221 insertions(+), 2 deletions(-)

test/link/elf.zig+221-2
......@@ -39,6 +39,7 @@ pub fn build(b: *Build) void {
3939 // https://github.com/ziglang/zig/issues/17451
4040 // elf_step.dependOn(testNoEhFrameHdr(b, .{ .target = musl_target }));
4141 elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target }));
42 elf_step.dependOn(testStrip(b, .{ .target = musl_target }));
4243
4344 // glibc tests
4445 elf_step.dependOn(testAsNeeded(b, .{ .target = glibc_target }));
......@@ -78,6 +79,9 @@ pub fn build(b: *Build) void {
7879 elf_step.dependOn(testPltGot(b, .{ .target = glibc_target }));
7980 elf_step.dependOn(testPreinitArray(b, .{ .target = glibc_target }));
8081 elf_step.dependOn(testSharedAbsSymbol(b, .{ .target = glibc_target }));
82 elf_step.dependOn(testTlsDfStaticTls(b, .{ .target = glibc_target }));
83 elf_step.dependOn(testTlsDso(b, .{ .target = glibc_target }));
84 elf_step.dependOn(testTlsGd(b, .{ .target = glibc_target }));
8185}
8286
8387fn testAbsSymbols(b: *Build, opts: Options) *Step {
......@@ -1417,7 +1421,6 @@ fn testLinkOrder(b: *Build, opts: Options) *Step {
14171421 // exe.addLibraryPath(lib.getEmittedBinDirectory());
14181422 // exe.addRPath(lib.getEmittedBinDirectory());
14191423 // exe.linkLibC();
1420 // exe.verbose_link = true;
14211424
14221425 // const check = exe.checkObject();
14231426 // check.checkInDynamicSection();
......@@ -1435,7 +1438,6 @@ fn testLinkOrder(b: *Build, opts: Options) *Step {
14351438 exe.addLibraryPath(dso.getEmittedBinDirectory());
14361439 exe.addRPath(dso.getEmittedBinDirectory());
14371440 exe.linkLibC();
1438 exe.verbose_link = true;
14391441
14401442 const check = exe.checkObject();
14411443 check.checkInDynamicSection();
......@@ -1705,6 +1707,223 @@ fn testSharedAbsSymbol(b: *Build, opts: Options) *Step {
17051707 return test_step;
17061708}
17071709
1710fn testStrip(b: *Build, opts: Options) *Step {
1711 const test_step = addTestStep(b, "strip", opts);
1712
1713 const obj = addObject(b, "obj", opts);
1714 addCSourceBytes(obj,
1715 \\#include <stdio.h>
1716 \\int main() {
1717 \\ printf("Hello!\n");
1718 \\ return 0;
1719 \\}
1720 , &.{});
1721 obj.linkLibC();
1722
1723 {
1724 const exe = addExecutable(b, "main1", opts);
1725 exe.addObject(obj);
1726 exe.strip = false;
1727 exe.linkLibC();
1728
1729 const check = exe.checkObject();
1730 check.checkStart();
1731 check.checkExact("section headers");
1732 check.checkExact("name .debug_info");
1733 test_step.dependOn(&check.step);
1734 }
1735
1736 {
1737 const exe = addExecutable(b, "main2", opts);
1738 exe.addObject(obj);
1739 exe.strip = true;
1740 exe.linkLibC();
1741
1742 const check = exe.checkObject();
1743 check.checkStart();
1744 check.checkExact("section headers");
1745 check.checkNotPresent("name .debug_info");
1746 test_step.dependOn(&check.step);
1747 }
1748
1749 return test_step;
1750}
1751
1752fn testTlsDfStaticTls(b: *Build, opts: Options) *Step {
1753 const test_step = addTestStep(b, "tls-df-static-tls", opts);
1754
1755 const obj = addObject(b, "obj", opts);
1756 addCSourceBytes(obj,
1757 \\static _Thread_local int foo = 5;
1758 \\void mutate() { ++foo; }
1759 \\int bar() { return foo; }
1760 , &.{"-ftls-model=initial-exec"});
1761 obj.force_pic = true;
1762
1763 {
1764 const dso = addSharedLibrary(b, "a", opts);
1765 dso.addObject(obj);
1766 // dso.link_relax = true;
1767
1768 const check = dso.checkObject();
1769 check.checkInDynamicSection();
1770 check.checkContains("STATIC_TLS");
1771 test_step.dependOn(&check.step);
1772 }
1773
1774 // TODO add -Wl,--no-relax
1775 // {
1776 // const dso = addSharedLibrary(b, "a", opts);
1777 // dso.addObject(obj);
1778 // dso.link_relax = false;
1779
1780 // const check = dso.checkObject();
1781 // check.checkInDynamicSection();
1782 // check.checkContains("STATIC_TLS");
1783 // test_step.dependOn(&check.step);
1784 // }
1785
1786 return test_step;
1787}
1788
1789fn testTlsDso(b: *Build, opts: Options) *Step {
1790 const test_step = addTestStep(b, "tls-dso", opts);
1791
1792 const dso = addSharedLibrary(b, "a", opts);
1793 addCSourceBytes(dso,
1794 \\extern _Thread_local int foo;
1795 \\_Thread_local int bar;
1796 \\int get_foo1() { return foo; }
1797 \\int get_bar1() { return bar; }
1798 , &.{});
1799
1800 const exe = addExecutable(b, "main", opts);
1801 addCSourceBytes(exe,
1802 \\#include <stdio.h>
1803 \\_Thread_local int foo;
1804 \\extern _Thread_local int bar;
1805 \\int get_foo1();
1806 \\int get_bar1();
1807 \\int get_foo2() { return foo; }
1808 \\int get_bar2() { return bar; }
1809 \\int main() {
1810 \\ foo = 5;
1811 \\ bar = 3;
1812 \\ printf("%d %d %d %d %d %d\n",
1813 \\ foo, bar,
1814 \\ get_foo1(), get_bar1(),
1815 \\ get_foo2(), get_bar2());
1816 \\ return 0;
1817 \\}
1818 , &.{});
1819 exe.linkLibrary(dso);
1820 exe.linkLibC();
1821
1822 const run = addRunArtifact(exe);
1823 run.expectStdOutEqual("5 3 5 3 5 3\n");
1824 test_step.dependOn(&run.step);
1825
1826 return test_step;
1827}
1828
1829fn testTlsGd(b: *Build, opts: Options) *Step {
1830 const test_step = addTestStep(b, "tls-gd", opts);
1831
1832 const main_o = addObject(b, "main", opts);
1833 addCSourceBytes(main_o,
1834 \\#include <stdio.h>
1835 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x1 = 1;
1836 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x2;
1837 \\__attribute__((tls_model("global-dynamic"))) extern _Thread_local int x3;
1838 \\__attribute__((tls_model("global-dynamic"))) extern _Thread_local int x4;
1839 \\int get_x5();
1840 \\int get_x6();
1841 \\int main() {
1842 \\ x2 = 2;
1843 \\ printf("%d %d %d %d %d %d\n", x1, x2, x3, x4, get_x5(), get_x6());
1844 \\ return 0;
1845 \\}
1846 , &.{});
1847 main_o.linkLibC();
1848 main_o.force_pic = true;
1849
1850 const a_o = addObject(b, "a", opts);
1851 addCSourceBytes(a_o,
1852 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x3 = 3;
1853 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x5 = 5;
1854 \\int get_x5() { return x5; }
1855 , &.{});
1856 a_o.force_pic = true;
1857
1858 const b_o = addObject(b, "b", opts);
1859 addCSourceBytes(b_o,
1860 \\__attribute__((tls_model("global-dynamic"))) _Thread_local int x4 = 4;
1861 \\__attribute__((tls_model("global-dynamic"))) static _Thread_local int x6 = 6;
1862 \\int get_x6() { return x6; }
1863 , &.{});
1864 b_o.force_pic = true;
1865
1866 const exp_stdout = "1 2 3 4 5 6\n";
1867
1868 const dso1 = addSharedLibrary(b, "a", opts);
1869 dso1.addObject(a_o);
1870
1871 const dso2 = addSharedLibrary(b, "b", opts);
1872 dso2.addObject(b_o);
1873 // dso2.link_relax = false; // TODO
1874
1875 {
1876 const exe = addExecutable(b, "main1", opts);
1877 exe.addObject(main_o);
1878 exe.linkLibrary(dso1);
1879 exe.linkLibrary(dso2);
1880
1881 const run = addRunArtifact(exe);
1882 run.expectStdOutEqual(exp_stdout);
1883 test_step.dependOn(&run.step);
1884 }
1885
1886 {
1887 const exe = addExecutable(b, "main2", opts);
1888 exe.addObject(main_o);
1889 // exe.link_relax = false; // TODO
1890 exe.linkLibrary(dso1);
1891 exe.linkLibrary(dso2);
1892
1893 const run = addRunArtifact(exe);
1894 run.expectStdOutEqual(exp_stdout);
1895 test_step.dependOn(&run.step);
1896 }
1897
1898 // https://github.com/ziglang/zig/issues/17430 ??
1899 // {
1900 // const exe = addExecutable(b, "main3", opts);
1901 // exe.addObject(main_o);
1902 // exe.linkLibrary(dso1);
1903 // exe.linkLibrary(dso2);
1904 // exe.linkage = .static;
1905
1906 // const run = addRunArtifact(exe);
1907 // run.expectStdOutEqual(exp_stdout);
1908 // test_step.dependOn(&run.step);
1909 // }
1910
1911 // {
1912 // const exe = addExecutable(b, "main4", opts);
1913 // exe.addObject(main_o);
1914 // // exe.link_relax = false; // TODO
1915 // exe.linkLibrary(dso1);
1916 // exe.linkLibrary(dso2);
1917 // exe.linkage = .static;
1918
1919 // const run = addRunArtifact(exe);
1920 // run.expectStdOutEqual(exp_stdout);
1921 // test_step.dependOn(&run.step);
1922 // }
1923
1924 return test_step;
1925}
1926
17081927fn testTlsStatic(b: *Build, opts: Options) *Step {
17091928 const test_step = addTestStep(b, "tls-static", opts);
17101929