authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-17 19:35:09+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-17 19:35:09+01:00
log79e99c401ca32031d29aff77e43758ca0b67d5d9
treef3edc8133467b6be6d7114a6fb0bec4105505c83
parentdd4d320eb9663c7a0ef8dbe3aca220a64795d683
parentd1429a8fa98f85c9fb8d2bd99b7c31eb9de38b38
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18973 from ziglang/elf-riscv

lib/std/elf: refactor relocation types + add RISCV relocs

6 files changed, 624 insertions(+), 679 deletions(-)

lib/std/elf.zig+422-355
......@@ -1806,363 +1806,430 @@ pub const COMPRESS = enum(u32) {
18061806};
18071807
18081808/// AMD x86-64 relocations.
1809/// No reloc
1810pub const R_X86_64_NONE = 0;
1811/// Direct 64 bit
1812pub const R_X86_64_64 = 1;
1813/// PC relative 32 bit signed
1814pub const R_X86_64_PC32 = 2;
1815/// 32 bit GOT entry
1816pub const R_X86_64_GOT32 = 3;
1817/// 32 bit PLT address
1818pub const R_X86_64_PLT32 = 4;
1819/// Copy symbol at runtime
1820pub const R_X86_64_COPY = 5;
1821/// Create GOT entry
1822pub const R_X86_64_GLOB_DAT = 6;
1823/// Create PLT entry
1824pub const R_X86_64_JUMP_SLOT = 7;
1825/// Adjust by program base
1826pub const R_X86_64_RELATIVE = 8;
1827/// 32 bit signed PC relative offset to GOT
1828pub const R_X86_64_GOTPCREL = 9;
1829/// Direct 32 bit zero extended
1830pub const R_X86_64_32 = 10;
1831/// Direct 32 bit sign extended
1832pub const R_X86_64_32S = 11;
1833/// Direct 16 bit zero extended
1834pub const R_X86_64_16 = 12;
1835/// 16 bit sign extended pc relative
1836pub const R_X86_64_PC16 = 13;
1837/// Direct 8 bit sign extended
1838pub const R_X86_64_8 = 14;
1839/// 8 bit sign extended pc relative
1840pub const R_X86_64_PC8 = 15;
1841/// ID of module containing symbol
1842pub const R_X86_64_DTPMOD64 = 16;
1843/// Offset in module's TLS block
1844pub const R_X86_64_DTPOFF64 = 17;
1845/// Offset in initial TLS block
1846pub const R_X86_64_TPOFF64 = 18;
1847/// 32 bit signed PC relative offset to two GOT entries for GD symbol
1848pub const R_X86_64_TLSGD = 19;
1849/// 32 bit signed PC relative offset to two GOT entries for LD symbol
1850pub const R_X86_64_TLSLD = 20;
1851/// Offset in TLS block
1852pub const R_X86_64_DTPOFF32 = 21;
1853/// 32 bit signed PC relative offset to GOT entry for IE symbol
1854pub const R_X86_64_GOTTPOFF = 22;
1855/// Offset in initial TLS block
1856pub const R_X86_64_TPOFF32 = 23;
1857/// PC relative 64 bit
1858pub const R_X86_64_PC64 = 24;
1859/// 64 bit offset to GOT
1860pub const R_X86_64_GOTOFF64 = 25;
1861/// 32 bit signed pc relative offset to GOT
1862pub const R_X86_64_GOTPC32 = 26;
1863/// 64 bit GOT entry offset
1864pub const R_X86_64_GOT64 = 27;
1865/// 64 bit PC relative offset to GOT entry
1866pub const R_X86_64_GOTPCREL64 = 28;
1867/// 64 bit PC relative offset to GOT
1868pub const R_X86_64_GOTPC64 = 29;
1869/// Like GOT64, says PLT entry needed
1870pub const R_X86_64_GOTPLT64 = 30;
1871/// 64-bit GOT relative offset to PLT entry
1872pub const R_X86_64_PLTOFF64 = 31;
1873/// Size of symbol plus 32-bit addend
1874pub const R_X86_64_SIZE32 = 32;
1875/// Size of symbol plus 64-bit addend
1876pub const R_X86_64_SIZE64 = 33;
1877/// GOT offset for TLS descriptor
1878pub const R_X86_64_GOTPC32_TLSDESC = 34;
1879/// Marker for call through TLS descriptor
1880pub const R_X86_64_TLSDESC_CALL = 35;
1881/// TLS descriptor
1882pub const R_X86_64_TLSDESC = 36;
1883/// Adjust indirectly by program base
1884pub const R_X86_64_IRELATIVE = 37;
1885/// 64-bit adjust by program base
1886pub const R_X86_64_RELATIVE64 = 38;
1887/// 39 Reserved was R_X86_64_PC32_BND
1888/// 40 Reserved was R_X86_64_PLT32_BND
1889/// Load from 32 bit signed pc relative offset to GOT entry without REX prefix, relaxable
1890pub const R_X86_64_GOTPCRELX = 41;
1891/// Load from 32 bit signed PC relative offset to GOT entry with REX prefix, relaxable
1892pub const R_X86_64_REX_GOTPCRELX = 42;
1893pub const R_X86_64_NUM = 43;
1809pub const R_X86_64 = enum(u32) {
1810 /// No reloc
1811 NONE = 0,
1812 /// Direct 64 bit
1813 @"64" = 1,
1814 /// PC relative 32 bit signed
1815 PC32 = 2,
1816 /// 32 bit GOT entry
1817 GOT32 = 3,
1818 /// 32 bit PLT address
1819 PLT32 = 4,
1820 /// Copy symbol at runtime
1821 COPY = 5,
1822 /// Create GOT entry
1823 GLOB_DAT = 6,
1824 /// Create PLT entry
1825 JUMP_SLOT = 7,
1826 /// Adjust by program base
1827 RELATIVE = 8,
1828 /// 32 bit signed PC relative offset to GOT
1829 GOTPCREL = 9,
1830 /// Direct 32 bit zero extended
1831 @"32" = 10,
1832 /// Direct 32 bit sign extended
1833 @"32S" = 11,
1834 /// Direct 16 bit zero extended
1835 @"16" = 12,
1836 /// 16 bit sign extended pc relative
1837 PC16 = 13,
1838 /// Direct 8 bit sign extended
1839 @"8" = 14,
1840 /// 8 bit sign extended pc relative
1841 PC8 = 15,
1842 /// ID of module containing symbol
1843 DTPMOD64 = 16,
1844 /// Offset in module's TLS block
1845 DTPOFF64 = 17,
1846 /// Offset in initial TLS block
1847 TPOFF64 = 18,
1848 /// 32 bit signed PC relative offset to two GOT entries for GD symbol
1849 TLSGD = 19,
1850 /// 32 bit signed PC relative offset to two GOT entries for LD symbol
1851 TLSLD = 20,
1852 /// Offset in TLS block
1853 DTPOFF32 = 21,
1854 /// 32 bit signed PC relative offset to GOT entry for IE symbol
1855 GOTTPOFF = 22,
1856 /// Offset in initial TLS block
1857 TPOFF32 = 23,
1858 /// PC relative 64 bit
1859 PC64 = 24,
1860 /// 64 bit offset to GOT
1861 GOTOFF64 = 25,
1862 /// 32 bit signed pc relative offset to GOT
1863 GOTPC32 = 26,
1864 /// 64 bit GOT entry offset
1865 GOT64 = 27,
1866 /// 64 bit PC relative offset to GOT entry
1867 GOTPCREL64 = 28,
1868 /// 64 bit PC relative offset to GOT
1869 GOTPC64 = 29,
1870 /// Like GOT64, says PLT entry needed
1871 GOTPLT64 = 30,
1872 /// 64-bit GOT relative offset to PLT entry
1873 PLTOFF64 = 31,
1874 /// Size of symbol plus 32-bit addend
1875 SIZE32 = 32,
1876 /// Size of symbol plus 64-bit addend
1877 SIZE64 = 33,
1878 /// GOT offset for TLS descriptor
1879 GOTPC32_TLSDESC = 34,
1880 /// Marker for call through TLS descriptor
1881 TLSDESC_CALL = 35,
1882 /// TLS descriptor
1883 TLSDESC = 36,
1884 /// Adjust indirectly by program base
1885 IRELATIVE = 37,
1886 /// 64-bit adjust by program base
1887 RELATIVE64 = 38,
1888 /// 39 Reserved was PC32_BND
1889 /// 40 Reserved was PLT32_BND
1890 /// Load from 32 bit signed pc relative offset to GOT entry without REX prefix, relaxable
1891 GOTPCRELX = 41,
1892 /// Load from 32 bit signed PC relative offset to GOT entry with REX prefix, relaxable
1893 REX_GOTPCRELX = 42,
1894 _,
1895};
18941896
18951897/// AArch64 relocs.
1896/// No relocation.
1897pub const R_AARCH64_NONE = 0;
1898
1899/// ILP32 AArch64 relocs.
1900/// Direct 32 bit.
1901pub const R_AARCH64_P32_ABS32 = 1;
1902/// Copy symbol at runtime.
1903pub const R_AARCH64_P32_COPY = 180;
1904/// Create GOT entry.
1905pub const R_AARCH64_P32_GLOB_DAT = 181;
1906/// Create PLT entry.
1907pub const R_AARCH64_P32_JUMP_SLOT = 182;
1908/// Adjust by program base.
1909pub const R_AARCH64_P32_RELATIVE = 183;
1910/// Module number, 32 bit.
1911pub const R_AARCH64_P32_TLS_DTPMOD = 184;
1912/// Module-relative offset, 32 bit.
1913pub const R_AARCH64_P32_TLS_DTPREL = 185;
1914/// TP-relative offset, 32 bit.
1915pub const R_AARCH64_P32_TLS_TPREL = 186;
1916/// TLS Descriptor.
1917pub const R_AARCH64_P32_TLSDESC = 187;
1918/// STT_GNU_IFUNC relocation.
1919pub const R_AARCH64_P32_IRELATIVE = 188;
1920
1921/// LP64 AArch64 relocs.
1922/// Direct 64 bit.
1923pub const R_AARCH64_ABS64 = 257;
1924/// Direct 32 bit.
1925pub const R_AARCH64_ABS32 = 258;
1926/// Direct 16-bit.
1927pub const R_AARCH64_ABS16 = 259;
1928/// PC-relative 64-bit.
1929pub const R_AARCH64_PREL64 = 260;
1930/// PC-relative 32-bit.
1931pub const R_AARCH64_PREL32 = 261;
1932/// PC-relative 16-bit.
1933pub const R_AARCH64_PREL16 = 262;
1934/// Dir. MOVZ imm. from bits 15:0.
1935pub const R_AARCH64_MOVW_UABS_G0 = 263;
1936/// Likewise for MOVK; no check.
1937pub const R_AARCH64_MOVW_UABS_G0_NC = 264;
1938/// Dir. MOVZ imm. from bits 31:16.
1939pub const R_AARCH64_MOVW_UABS_G1 = 265;
1940/// Likewise for MOVK; no check.
1941pub const R_AARCH64_MOVW_UABS_G1_NC = 266;
1942/// Dir. MOVZ imm. from bits 47:32.
1943pub const R_AARCH64_MOVW_UABS_G2 = 267;
1944/// Likewise for MOVK; no check.
1945pub const R_AARCH64_MOVW_UABS_G2_NC = 268;
1946/// Dir. MOV{K,Z} imm. from 63:48.
1947pub const R_AARCH64_MOVW_UABS_G3 = 269;
1948/// Dir. MOV{N,Z} imm. from 15:0.
1949pub const R_AARCH64_MOVW_SABS_G0 = 270;
1950/// Dir. MOV{N,Z} imm. from 31:16.
1951pub const R_AARCH64_MOVW_SABS_G1 = 271;
1952/// Dir. MOV{N,Z} imm. from 47:32.
1953pub const R_AARCH64_MOVW_SABS_G2 = 272;
1954/// PC-rel. LD imm. from bits 20:2.
1955pub const R_AARCH64_LD_PREL_LO19 = 273;
1956/// PC-rel. ADR imm. from bits 20:0.
1957pub const R_AARCH64_ADR_PREL_LO21 = 274;
1958/// Page-rel. ADRP imm. from 32:12.
1959pub const R_AARCH64_ADR_PREL_PG_HI21 = 275;
1960/// Likewise; no overflow check.
1961pub const R_AARCH64_ADR_PREL_PG_HI21_NC = 276;
1962/// Dir. ADD imm. from bits 11:0.
1963pub const R_AARCH64_ADD_ABS_LO12_NC = 277;
1964/// Likewise for LD/ST; no check.
1965pub const R_AARCH64_LDST8_ABS_LO12_NC = 278;
1966/// PC-rel. TBZ/TBNZ imm. from 15:2.
1967pub const R_AARCH64_TSTBR14 = 279;
1968/// PC-rel. cond. br. imm. from 20:2.
1969pub const R_AARCH64_CONDBR19 = 280;
1970/// PC-rel. B imm. from bits 27:2.
1971pub const R_AARCH64_JUMP26 = 282;
1972/// Likewise for CALL.
1973pub const R_AARCH64_CALL26 = 283;
1974/// Dir. ADD imm. from bits 11:1.
1975pub const R_AARCH64_LDST16_ABS_LO12_NC = 284;
1976/// Likewise for bits 11:2.
1977pub const R_AARCH64_LDST32_ABS_LO12_NC = 285;
1978/// Likewise for bits 11:3.
1979pub const R_AARCH64_LDST64_ABS_LO12_NC = 286;
1980/// PC-rel. MOV{N,Z} imm. from 15:0.
1981pub const R_AARCH64_MOVW_PREL_G0 = 287;
1982/// Likewise for MOVK; no check.
1983pub const R_AARCH64_MOVW_PREL_G0_NC = 288;
1984/// PC-rel. MOV{N,Z} imm. from 31:16.
1985pub const R_AARCH64_MOVW_PREL_G1 = 289;
1986/// Likewise for MOVK; no check.
1987pub const R_AARCH64_MOVW_PREL_G1_NC = 290;
1988/// PC-rel. MOV{N,Z} imm. from 47:32.
1989pub const R_AARCH64_MOVW_PREL_G2 = 291;
1990/// Likewise for MOVK; no check.
1991pub const R_AARCH64_MOVW_PREL_G2_NC = 292;
1992/// PC-rel. MOV{N,Z} imm. from 63:48.
1993pub const R_AARCH64_MOVW_PREL_G3 = 293;
1994/// Dir. ADD imm. from bits 11:4.
1995pub const R_AARCH64_LDST128_ABS_LO12_NC = 299;
1996/// GOT-rel. off. MOV{N,Z} imm. 15:0.
1997pub const R_AARCH64_MOVW_GOTOFF_G0 = 300;
1998/// Likewise for MOVK; no check.
1999pub const R_AARCH64_MOVW_GOTOFF_G0_NC = 301;
2000/// GOT-rel. o. MOV{N,Z} imm. 31:16.
2001pub const R_AARCH64_MOVW_GOTOFF_G1 = 302;
2002/// Likewise for MOVK; no check.
2003pub const R_AARCH64_MOVW_GOTOFF_G1_NC = 303;
2004/// GOT-rel. o. MOV{N,Z} imm. 47:32.
2005pub const R_AARCH64_MOVW_GOTOFF_G2 = 304;
2006/// Likewise for MOVK; no check.
2007pub const R_AARCH64_MOVW_GOTOFF_G2_NC = 305;
2008/// GOT-rel. o. MOV{N,Z} imm. 63:48.
2009pub const R_AARCH64_MOVW_GOTOFF_G3 = 306;
2010/// GOT-relative 64-bit.
2011pub const R_AARCH64_GOTREL64 = 307;
2012/// GOT-relative 32-bit.
2013pub const R_AARCH64_GOTREL32 = 308;
2014/// PC-rel. GOT off. load imm. 20:2.
2015pub const R_AARCH64_GOT_LD_PREL19 = 309;
2016/// GOT-rel. off. LD/ST imm. 14:3.
2017pub const R_AARCH64_LD64_GOTOFF_LO15 = 310;
2018/// P-page-rel. GOT off. ADRP 32:12.
2019pub const R_AARCH64_ADR_GOT_PAGE = 311;
2020/// Dir. GOT off. LD/ST imm. 11:3.
2021pub const R_AARCH64_LD64_GOT_LO12_NC = 312;
2022/// GOT-page-rel. GOT off. LD/ST 14:3
2023pub const R_AARCH64_LD64_GOTPAGE_LO15 = 313;
2024/// PC-relative ADR imm. 20:0.
2025pub const R_AARCH64_TLSGD_ADR_PREL21 = 512;
2026/// page-rel. ADRP imm. 32:12.
2027pub const R_AARCH64_TLSGD_ADR_PAGE21 = 513;
2028/// direct ADD imm. from 11:0.
2029pub const R_AARCH64_TLSGD_ADD_LO12_NC = 514;
2030/// GOT-rel. MOV{N,Z} 31:16.
2031pub const R_AARCH64_TLSGD_MOVW_G1 = 515;
2032/// GOT-rel. MOVK imm. 15:0.
2033pub const R_AARCH64_TLSGD_MOVW_G0_NC = 516;
2034/// Like 512; local dynamic model.
2035pub const R_AARCH64_TLSLD_ADR_PREL21 = 517;
2036/// Like 513; local dynamic model.
2037pub const R_AARCH64_TLSLD_ADR_PAGE21 = 518;
2038/// Like 514; local dynamic model.
2039pub const R_AARCH64_TLSLD_ADD_LO12_NC = 519;
2040/// Like 515; local dynamic model.
2041pub const R_AARCH64_TLSLD_MOVW_G1 = 520;
2042/// Like 516; local dynamic model.
2043pub const R_AARCH64_TLSLD_MOVW_G0_NC = 521;
2044/// TLS PC-rel. load imm. 20:2.
2045pub const R_AARCH64_TLSLD_LD_PREL19 = 522;
2046/// TLS DTP-rel. MOV{N,Z} 47:32.
2047pub const R_AARCH64_TLSLD_MOVW_DTPREL_G2 = 523;
2048/// TLS DTP-rel. MOV{N,Z} 31:16.
2049pub const R_AARCH64_TLSLD_MOVW_DTPREL_G1 = 524;
2050/// Likewise; MOVK; no check.
2051pub const R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC = 525;
2052/// TLS DTP-rel. MOV{N,Z} 15:0.
2053pub const R_AARCH64_TLSLD_MOVW_DTPREL_G0 = 526;
2054/// Likewise; MOVK; no check.
2055pub const R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC = 527;
2056/// DTP-rel. ADD imm. from 23:12.
2057pub const R_AARCH64_TLSLD_ADD_DTPREL_HI12 = 528;
2058/// DTP-rel. ADD imm. from 11:0.
2059pub const R_AARCH64_TLSLD_ADD_DTPREL_LO12 = 529;
2060/// Likewise; no ovfl. check.
2061pub const R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC = 530;
2062/// DTP-rel. LD/ST imm. 11:0.
2063pub const R_AARCH64_TLSLD_LDST8_DTPREL_LO12 = 531;
2064/// Likewise; no check.
2065pub const R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC = 532;
2066/// DTP-rel. LD/ST imm. 11:1.
2067pub const R_AARCH64_TLSLD_LDST16_DTPREL_LO12 = 533;
2068/// Likewise; no check.
2069pub const R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC = 534;
2070/// DTP-rel. LD/ST imm. 11:2.
2071pub const R_AARCH64_TLSLD_LDST32_DTPREL_LO12 = 535;
2072/// Likewise; no check.
2073pub const R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC = 536;
2074/// DTP-rel. LD/ST imm. 11:3.
2075pub const R_AARCH64_TLSLD_LDST64_DTPREL_LO12 = 537;
2076/// Likewise; no check.
2077pub const R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC = 538;
2078/// GOT-rel. MOV{N,Z} 31:16.
2079pub const R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 = 539;
2080/// GOT-rel. MOVK 15:0.
2081pub const R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC = 540;
2082/// Page-rel. ADRP 32:12.
2083pub const R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 = 541;
2084/// Direct LD off. 11:3.
2085pub const R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC = 542;
2086/// PC-rel. load imm. 20:2.
2087pub const R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 = 543;
2088/// TLS TP-rel. MOV{N,Z} 47:32.
2089pub const R_AARCH64_TLSLE_MOVW_TPREL_G2 = 544;
2090/// TLS TP-rel. MOV{N,Z} 31:16.
2091pub const R_AARCH64_TLSLE_MOVW_TPREL_G1 = 545;
2092/// Likewise; MOVK; no check.
2093pub const R_AARCH64_TLSLE_MOVW_TPREL_G1_NC = 546;
2094/// TLS TP-rel. MOV{N,Z} 15:0.
2095pub const R_AARCH64_TLSLE_MOVW_TPREL_G0 = 547;
2096/// Likewise; MOVK; no check.
2097pub const R_AARCH64_TLSLE_MOVW_TPREL_G0_NC = 548;
2098/// TP-rel. ADD imm. 23:12.
2099pub const R_AARCH64_TLSLE_ADD_TPREL_HI12 = 549;
2100/// TP-rel. ADD imm. 11:0.
2101pub const R_AARCH64_TLSLE_ADD_TPREL_LO12 = 550;
2102/// Likewise; no ovfl. check.
2103pub const R_AARCH64_TLSLE_ADD_TPREL_LO12_NC = 551;
2104/// TP-rel. LD/ST off. 11:0.
2105pub const R_AARCH64_TLSLE_LDST8_TPREL_LO12 = 552;
2106/// Likewise; no ovfl. check.
2107pub const R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC = 553;
2108/// TP-rel. LD/ST off. 11:1.
2109pub const R_AARCH64_TLSLE_LDST16_TPREL_LO12 = 554;
2110/// Likewise; no check.
2111pub const R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC = 555;
2112/// TP-rel. LD/ST off. 11:2.
2113pub const R_AARCH64_TLSLE_LDST32_TPREL_LO12 = 556;
2114/// Likewise; no check.
2115pub const R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC = 557;
2116/// TP-rel. LD/ST off. 11:3.
2117pub const R_AARCH64_TLSLE_LDST64_TPREL_LO12 = 558;
2118/// Likewise; no check.
2119pub const R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC = 559;
2120/// PC-rel. load immediate 20:2.
2121pub const R_AARCH64_TLSDESC_LD_PREL19 = 560;
2122/// PC-rel. ADR immediate 20:0.
2123pub const R_AARCH64_TLSDESC_ADR_PREL21 = 561;
2124/// Page-rel. ADRP imm. 32:12.
2125pub const R_AARCH64_TLSDESC_ADR_PAGE21 = 562;
2126/// Direct LD off. from 11:3.
2127pub const R_AARCH64_TLSDESC_LD64_LO12 = 563;
2128/// Direct ADD imm. from 11:0.
2129pub const R_AARCH64_TLSDESC_ADD_LO12 = 564;
2130/// GOT-rel. MOV{N,Z} imm. 31:16.
2131pub const R_AARCH64_TLSDESC_OFF_G1 = 565;
2132/// GOT-rel. MOVK imm. 15:0; no ck.
2133pub const R_AARCH64_TLSDESC_OFF_G0_NC = 566;
2134/// Relax LDR.
2135pub const R_AARCH64_TLSDESC_LDR = 567;
2136/// Relax ADD.
2137pub const R_AARCH64_TLSDESC_ADD = 568;
2138/// Relax BLR.
2139pub const R_AARCH64_TLSDESC_CALL = 569;
2140/// TP-rel. LD/ST off. 11:4.
2141pub const R_AARCH64_TLSLE_LDST128_TPREL_LO12 = 570;
2142/// Likewise; no check.
2143pub const R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC = 571;
2144/// DTP-rel. LD/ST imm. 11:4.
2145pub const R_AARCH64_TLSLD_LDST128_DTPREL_LO12 = 572;
2146/// Likewise; no check.
2147pub const R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC = 573;
2148/// Copy symbol at runtime.
2149pub const R_AARCH64_COPY = 1024;
2150/// Create GOT entry.
2151pub const R_AARCH64_GLOB_DAT = 1025;
2152/// Create PLT entry.
2153pub const R_AARCH64_JUMP_SLOT = 1026;
2154/// Adjust by program base.
2155pub const R_AARCH64_RELATIVE = 1027;
2156/// Module number, 64 bit.
2157pub const R_AARCH64_TLS_DTPMOD = 1028;
2158/// Module-relative offset, 64 bit.
2159pub const R_AARCH64_TLS_DTPREL = 1029;
2160/// TP-relative offset, 64 bit.
2161pub const R_AARCH64_TLS_TPREL = 1030;
2162/// TLS Descriptor.
2163pub const R_AARCH64_TLSDESC = 1031;
2164/// STT_GNU_IFUNC relocation.
2165pub const R_AARCH64_IRELATIVE = 1032;
1898pub const R_AARCH64 = enum(u32) {
1899 /// No relocation.
1900 NONE = 0,
1901 /// ILP32 AArch64 relocs.
1902 /// Direct 32 bit.
1903 P32_ABS32 = 1,
1904 /// Copy symbol at runtime.
1905 P32_COPY = 180,
1906 /// Create GOT entry.
1907 P32_GLOB_DAT = 181,
1908 /// Create PLT entry.
1909 P32_JUMP_SLOT = 182,
1910 /// Adjust by program base.
1911 P32_RELATIVE = 183,
1912 /// Module number, 32 bit.
1913 P32_TLS_DTPMOD = 184,
1914 /// Module-relative offset, 32 bit.
1915 P32_TLS_DTPREL = 185,
1916 /// TP-relative offset, 32 bit.
1917 P32_TLS_TPREL = 186,
1918 /// TLS Descriptor.
1919 P32_TLSDESC = 187,
1920 /// STT_GNU_IFUNC relocation.
1921 P32_IRELATIVE = 188,
1922 /// LP64 AArch64 relocs.
1923 /// Direct 64 bit.
1924 ABS64 = 257,
1925 /// Direct 32 bit.
1926 ABS32 = 258,
1927 /// Direct 16-bit.
1928 ABS16 = 259,
1929 /// PC-relative 64-bit.
1930 PREL64 = 260,
1931 /// PC-relative 32-bit.
1932 PREL32 = 261,
1933 /// PC-relative 16-bit.
1934 PREL16 = 262,
1935 /// Dir. MOVZ imm. from bits 15:0.
1936 MOVW_UABS_G0 = 263,
1937 /// Likewise for MOVK; no check.
1938 MOVW_UABS_G0_NC = 264,
1939 /// Dir. MOVZ imm. from bits 31:16.
1940 MOVW_UABS_G1 = 265,
1941 /// Likewise for MOVK; no check.
1942 MOVW_UABS_G1_NC = 266,
1943 /// Dir. MOVZ imm. from bits 47:32.
1944 MOVW_UABS_G2 = 267,
1945 /// Likewise for MOVK; no check.
1946 MOVW_UABS_G2_NC = 268,
1947 /// Dir. MOV{K,Z} imm. from 63:48.
1948 MOVW_UABS_G3 = 269,
1949 /// Dir. MOV{N,Z} imm. from 15:0.
1950 MOVW_SABS_G0 = 270,
1951 /// Dir. MOV{N,Z} imm. from 31:16.
1952 MOVW_SABS_G1 = 271,
1953 /// Dir. MOV{N,Z} imm. from 47:32.
1954 MOVW_SABS_G2 = 272,
1955 /// PC-rel. LD imm. from bits 20:2.
1956 LD_PREL_LO19 = 273,
1957 /// PC-rel. ADR imm. from bits 20:0.
1958 ADR_PREL_LO21 = 274,
1959 /// Page-rel. ADRP imm. from 32:12.
1960 ADR_PREL_PG_HI21 = 275,
1961 /// Likewise; no overflow check.
1962 ADR_PREL_PG_HI21_NC = 276,
1963 /// Dir. ADD imm. from bits 11:0.
1964 ADD_ABS_LO12_NC = 277,
1965 /// Likewise for LD/ST; no check.
1966 LDST8_ABS_LO12_NC = 278,
1967 /// PC-rel. TBZ/TBNZ imm. from 15:2.
1968 TSTBR14 = 279,
1969 /// PC-rel. cond. br. imm. from 20:2.
1970 CONDBR19 = 280,
1971 /// PC-rel. B imm. from bits 27:2.
1972 JUMP26 = 282,
1973 /// Likewise for CALL.
1974 CALL26 = 283,
1975 /// Dir. ADD imm. from bits 11:1.
1976 LDST16_ABS_LO12_NC = 284,
1977 /// Likewise for bits 11:2.
1978 LDST32_ABS_LO12_NC = 285,
1979 /// Likewise for bits 11:3.
1980 LDST64_ABS_LO12_NC = 286,
1981 /// PC-rel. MOV{N,Z} imm. from 15:0.
1982 MOVW_PREL_G0 = 287,
1983 /// Likewise for MOVK; no check.
1984 MOVW_PREL_G0_NC = 288,
1985 /// PC-rel. MOV{N,Z} imm. from 31:16.
1986 MOVW_PREL_G1 = 289,
1987 /// Likewise for MOVK; no check.
1988 MOVW_PREL_G1_NC = 290,
1989 /// PC-rel. MOV{N,Z} imm. from 47:32.
1990 MOVW_PREL_G2 = 291,
1991 /// Likewise for MOVK; no check.
1992 MOVW_PREL_G2_NC = 292,
1993 /// PC-rel. MOV{N,Z} imm. from 63:48.
1994 MOVW_PREL_G3 = 293,
1995 /// Dir. ADD imm. from bits 11:4.
1996 LDST128_ABS_LO12_NC = 299,
1997 /// GOT-rel. off. MOV{N,Z} imm. 15:0.
1998 MOVW_GOTOFF_G0 = 300,
1999 /// Likewise for MOVK; no check.
2000 MOVW_GOTOFF_G0_NC = 301,
2001 /// GOT-rel. o. MOV{N,Z} imm. 31:16.
2002 MOVW_GOTOFF_G1 = 302,
2003 /// Likewise for MOVK; no check.
2004 MOVW_GOTOFF_G1_NC = 303,
2005 /// GOT-rel. o. MOV{N,Z} imm. 47:32.
2006 MOVW_GOTOFF_G2 = 304,
2007 /// Likewise for MOVK; no check.
2008 MOVW_GOTOFF_G2_NC = 305,
2009 /// GOT-rel. o. MOV{N,Z} imm. 63:48.
2010 MOVW_GOTOFF_G3 = 306,
2011 /// GOT-relative 64-bit.
2012 GOTREL64 = 307,
2013 /// GOT-relative 32-bit.
2014 GOTREL32 = 308,
2015 /// PC-rel. GOT off. load imm. 20:2.
2016 GOT_LD_PREL19 = 309,
2017 /// GOT-rel. off. LD/ST imm. 14:3.
2018 LD64_GOTOFF_LO15 = 310,
2019 /// P-page-rel. GOT off. ADRP 32:12.
2020 ADR_GOT_PAGE = 311,
2021 /// Dir. GOT off. LD/ST imm. 11:3.
2022 LD64_GOT_LO12_NC = 312,
2023 /// GOT-page-rel. GOT off. LD/ST 14:3
2024 LD64_GOTPAGE_LO15 = 313,
2025 /// PC-relative ADR imm. 20:0.
2026 TLSGD_ADR_PREL21 = 512,
2027 /// page-rel. ADRP imm. 32:12.
2028 TLSGD_ADR_PAGE21 = 513,
2029 /// direct ADD imm. from 11:0.
2030 TLSGD_ADD_LO12_NC = 514,
2031 /// GOT-rel. MOV{N,Z} 31:16.
2032 TLSGD_MOVW_G1 = 515,
2033 /// GOT-rel. MOVK imm. 15:0.
2034 TLSGD_MOVW_G0_NC = 516,
2035 /// Like 512; local dynamic model.
2036 TLSLD_ADR_PREL21 = 517,
2037 /// Like 513; local dynamic model.
2038 TLSLD_ADR_PAGE21 = 518,
2039 /// Like 514; local dynamic model.
2040 TLSLD_ADD_LO12_NC = 519,
2041 /// Like 515; local dynamic model.
2042 TLSLD_MOVW_G1 = 520,
2043 /// Like 516; local dynamic model.
2044 TLSLD_MOVW_G0_NC = 521,
2045 /// TLS PC-rel. load imm. 20:2.
2046 TLSLD_LD_PREL19 = 522,
2047 /// TLS DTP-rel. MOV{N,Z} 47:32.
2048 TLSLD_MOVW_DTPREL_G2 = 523,
2049 /// TLS DTP-rel. MOV{N,Z} 31:16.
2050 TLSLD_MOVW_DTPREL_G1 = 524,
2051 /// Likewise; MOVK; no check.
2052 TLSLD_MOVW_DTPREL_G1_NC = 525,
2053 /// TLS DTP-rel. MOV{N,Z} 15:0.
2054 TLSLD_MOVW_DTPREL_G0 = 526,
2055 /// Likewise; MOVK; no check.
2056 TLSLD_MOVW_DTPREL_G0_NC = 527,
2057 /// DTP-rel. ADD imm. from 23:12.
2058 TLSLD_ADD_DTPREL_HI12 = 528,
2059 /// DTP-rel. ADD imm. from 11:0.
2060 TLSLD_ADD_DTPREL_LO12 = 529,
2061 /// Likewise; no ovfl. check.
2062 TLSLD_ADD_DTPREL_LO12_NC = 530,
2063 /// DTP-rel. LD/ST imm. 11:0.
2064 TLSLD_LDST8_DTPREL_LO12 = 531,
2065 /// Likewise; no check.
2066 TLSLD_LDST8_DTPREL_LO12_NC = 532,
2067 /// DTP-rel. LD/ST imm. 11:1.
2068 TLSLD_LDST16_DTPREL_LO12 = 533,
2069 /// Likewise; no check.
2070 TLSLD_LDST16_DTPREL_LO12_NC = 534,
2071 /// DTP-rel. LD/ST imm. 11:2.
2072 TLSLD_LDST32_DTPREL_LO12 = 535,
2073 /// Likewise; no check.
2074 TLSLD_LDST32_DTPREL_LO12_NC = 536,
2075 /// DTP-rel. LD/ST imm. 11:3.
2076 TLSLD_LDST64_DTPREL_LO12 = 537,
2077 /// Likewise; no check.
2078 TLSLD_LDST64_DTPREL_LO12_NC = 538,
2079 /// GOT-rel. MOV{N,Z} 31:16.
2080 TLSIE_MOVW_GOTTPREL_G1 = 539,
2081 /// GOT-rel. MOVK 15:0.
2082 TLSIE_MOVW_GOTTPREL_G0_NC = 540,
2083 /// Page-rel. ADRP 32:12.
2084 TLSIE_ADR_GOTTPREL_PAGE21 = 541,
2085 /// Direct LD off. 11:3.
2086 TLSIE_LD64_GOTTPREL_LO12_NC = 542,
2087 /// PC-rel. load imm. 20:2.
2088 TLSIE_LD_GOTTPREL_PREL19 = 543,
2089 /// TLS TP-rel. MOV{N,Z} 47:32.
2090 TLSLE_MOVW_TPREL_G2 = 544,
2091 /// TLS TP-rel. MOV{N,Z} 31:16.
2092 TLSLE_MOVW_TPREL_G1 = 545,
2093 /// Likewise; MOVK; no check.
2094 TLSLE_MOVW_TPREL_G1_NC = 546,
2095 /// TLS TP-rel. MOV{N,Z} 15:0.
2096 TLSLE_MOVW_TPREL_G0 = 547,
2097 /// Likewise; MOVK; no check.
2098 TLSLE_MOVW_TPREL_G0_NC = 548,
2099 /// TP-rel. ADD imm. 23:12.
2100 TLSLE_ADD_TPREL_HI12 = 549,
2101 /// TP-rel. ADD imm. 11:0.
2102 TLSLE_ADD_TPREL_LO12 = 550,
2103 /// Likewise; no ovfl. check.
2104 TLSLE_ADD_TPREL_LO12_NC = 551,
2105 /// TP-rel. LD/ST off. 11:0.
2106 TLSLE_LDST8_TPREL_LO12 = 552,
2107 /// Likewise; no ovfl. check.
2108 TLSLE_LDST8_TPREL_LO12_NC = 553,
2109 /// TP-rel. LD/ST off. 11:1.
2110 TLSLE_LDST16_TPREL_LO12 = 554,
2111 /// Likewise; no check.
2112 TLSLE_LDST16_TPREL_LO12_NC = 555,
2113 /// TP-rel. LD/ST off. 11:2.
2114 TLSLE_LDST32_TPREL_LO12 = 556,
2115 /// Likewise; no check.
2116 TLSLE_LDST32_TPREL_LO12_NC = 557,
2117 /// TP-rel. LD/ST off. 11:3.
2118 TLSLE_LDST64_TPREL_LO12 = 558,
2119 /// Likewise; no check.
2120 TLSLE_LDST64_TPREL_LO12_NC = 559,
2121 /// PC-rel. load immediate 20:2.
2122 TLSDESC_LD_PREL19 = 560,
2123 /// PC-rel. ADR immediate 20:0.
2124 TLSDESC_ADR_PREL21 = 561,
2125 /// Page-rel. ADRP imm. 32:12.
2126 TLSDESC_ADR_PAGE21 = 562,
2127 /// Direct LD off. from 11:3.
2128 TLSDESC_LD64_LO12 = 563,
2129 /// Direct ADD imm. from 11:0.
2130 TLSDESC_ADD_LO12 = 564,
2131 /// GOT-rel. MOV{N,Z} imm. 31:16.
2132 TLSDESC_OFF_G1 = 565,
2133 /// GOT-rel. MOVK imm. 15:0; no ck.
2134 TLSDESC_OFF_G0_NC = 566,
2135 /// Relax LDR.
2136 TLSDESC_LDR = 567,
2137 /// Relax ADD.
2138 TLSDESC_ADD = 568,
2139 /// Relax BLR.
2140 TLSDESC_CALL = 569,
2141 /// TP-rel. LD/ST off. 11:4.
2142 TLSLE_LDST128_TPREL_LO12 = 570,
2143 /// Likewise; no check.
2144 TLSLE_LDST128_TPREL_LO12_NC = 571,
2145 /// DTP-rel. LD/ST imm. 11:4.
2146 TLSLD_LDST128_DTPREL_LO12 = 572,
2147 /// Likewise; no check.
2148 TLSLD_LDST128_DTPREL_LO12_NC = 573,
2149 /// Copy symbol at runtime.
2150 COPY = 1024,
2151 /// Create GOT entry.
2152 GLOB_DAT = 1025,
2153 /// Create PLT entry.
2154 JUMP_SLOT = 1026,
2155 /// Adjust by program base.
2156 RELATIVE = 1027,
2157 /// Module number, 64 bit.
2158 TLS_DTPMOD = 1028,
2159 /// Module-relative offset, 64 bit.
2160 TLS_DTPREL = 1029,
2161 /// TP-relative offset, 64 bit.
2162 TLS_TPREL = 1030,
2163 /// TLS Descriptor.
2164 TLSDESC = 1031,
2165 /// STT_GNU_IFUNC relocation.
2166 IRELATIVE = 1032,
2167 _,
2168};
2169
2170/// RISC-V relocations.
2171pub const R_RISCV = enum(u32) {
2172 NONE = 0,
2173 @"32" = 1,
2174 @"64" = 2,
2175 RELATIVE = 3,
2176 COPY = 4,
2177 JUMP_SLOT = 5,
2178 TLS_DTPMOD32 = 6,
2179 TLS_DTPMOD64 = 7,
2180 TLS_DTPREL32 = 8,
2181 TLS_DTPREL64 = 9,
2182 TLS_TPREL32 = 10,
2183 TLS_TPREL64 = 11,
2184 TLSDESC = 12,
2185 BRANCH = 16,
2186 JAL = 17,
2187 CALL = 18,
2188 CALL_PLT = 19,
2189 GOT_HI20 = 20,
2190 TLS_GOT_HI20 = 21,
2191 TLS_GD_HI20 = 22,
2192 PCREL_HI20 = 23,
2193 PCREL_LO12_I = 24,
2194 PCREL_LO12_S = 25,
2195 HI20 = 26,
2196 LO12_I = 27,
2197 LO12_S = 28,
2198 TPREL_HI20 = 29,
2199 TPREL_LO12_I = 30,
2200 TPREL_LO12_S = 31,
2201 TPREL_ADD = 32,
2202 ADD8 = 33,
2203 ADD16 = 34,
2204 ADD32 = 35,
2205 ADD64 = 36,
2206 SUB8 = 37,
2207 SUB16 = 38,
2208 SUB32 = 39,
2209 SUB64 = 40,
2210 GNU_VTINHERIT = 41,
2211 GNU_VTENTRY = 42,
2212 ALIGN = 43,
2213 RVC_BRANCH = 44,
2214 RVC_JUMP = 45,
2215 RVC_LUI = 46,
2216 GPREL_I = 47,
2217 GPREL_S = 48,
2218 TPREL_I = 49,
2219 TPREL_S = 50,
2220 RELAX = 51,
2221 SUB6 = 52,
2222 SET6 = 53,
2223 SET8 = 54,
2224 SET16 = 55,
2225 SET32 = 56,
2226 @"32_PCREL" = 57,
2227 IRELATIVE = 58,
2228 PLT32 = 59,
2229 SET_ULEB128 = 60,
2230 SUB_ULEB128 = 61,
2231 _,
2232};
21662233
21672234pub const STV = enum(u2) {
21682235 DEFAULT = 0,
src/arch/x86_64/Emit.zig+15-11
......@@ -43,9 +43,10 @@ pub fn emitMir(emit: *Emit) Error!void {
4343 .linker_extern_fn => |symbol| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {
4444 // Add relocation to the decl.
4545 const atom_ptr = elf_file.symbol(symbol.atom_index).atom(elf_file).?;
46 const r_type = @intFromEnum(std.elf.R_X86_64.PLT32);
4647 try atom_ptr.addReloc(elf_file, .{
4748 .r_offset = end_offset - 4,
48 .r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | std.elf.R_X86_64_PLT32,
49 .r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | r_type,
4950 .r_addend = -4,
5051 });
5152 } else if (emit.lower.bin_file.cast(link.File.MachO)) |macho_file| {
......@@ -88,18 +89,20 @@ pub fn emitMir(emit: *Emit) Error!void {
8889 .linker_tlsld => |data| {
8990 const elf_file = emit.lower.bin_file.cast(link.File.Elf).?;
9091 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
92 const r_type = @intFromEnum(std.elf.R_X86_64.TLSLD);
9193 try atom.addReloc(elf_file, .{
9294 .r_offset = end_offset - 4,
93 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_TLSLD,
95 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
9496 .r_addend = -4,
9597 });
9698 },
9799 .linker_dtpoff => |data| {
98100 const elf_file = emit.lower.bin_file.cast(link.File.Elf).?;
99101 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
102 const r_type = @intFromEnum(std.elf.R_X86_64.DTPOFF32);
100103 try atom.addReloc(elf_file, .{
101104 .r_offset = end_offset - 4,
102 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_DTPOFF32,
105 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
103106 .r_addend = 0,
104107 });
105108 },
......@@ -117,11 +120,11 @@ pub fn emitMir(emit: *Emit) Error!void {
117120 }
118121 if (emit.lower.pic) {
119122 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
120 link.File.Elf.R_X86_64_ZIG_GOTPCREL
123 link.File.Elf.R_ZIG_GOTPCREL
121124 else if (sym.flags.needs_got)
122 std.elf.R_X86_64_GOTPCREL
125 @intFromEnum(std.elf.R_X86_64.GOTPCREL)
123126 else
124 std.elf.R_X86_64_PC32;
127 @intFromEnum(std.elf.R_X86_64.PC32);
125128 try atom.addReloc(elf_file, .{
126129 .r_offset = end_offset - 4,
127130 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
......@@ -129,20 +132,21 @@ pub fn emitMir(emit: *Emit) Error!void {
129132 });
130133 } else {
131134 if (lowered_inst.encoding.mnemonic == .call and sym.flags.needs_zig_got and is_obj_or_static_lib) {
135 const r_type = @intFromEnum(std.elf.R_X86_64.PC32);
132136 try atom.addReloc(elf_file, .{
133137 .r_offset = end_offset - 4,
134 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | std.elf.R_X86_64_PC32,
138 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
135139 .r_addend = -4,
136140 });
137141 } else {
138142 const r_type: u32 = if (sym.flags.needs_zig_got and !is_obj_or_static_lib)
139 link.File.Elf.R_X86_64_ZIG_GOT32
143 link.File.Elf.R_ZIG_GOT32
140144 else if (sym.flags.needs_got)
141 std.elf.R_X86_64_GOT32
145 @intFromEnum(std.elf.R_X86_64.GOT32)
142146 else if (sym.flags.is_tls)
143 std.elf.R_X86_64_TPOFF32
147 @intFromEnum(std.elf.R_X86_64.TPOFF32)
144148 else
145 std.elf.R_X86_64_32;
149 @intFromEnum(std.elf.R_X86_64.@"32");
146150 try atom.addReloc(elf_file, .{
147151 .r_offset = end_offset - 4,
148152 .r_info = (@as(u64, @intCast(data.sym_index)) << 32) | r_type,
src/link/Elf.zig+3-2
......@@ -6065,8 +6065,9 @@ const RelaSection = struct {
60656065};
60666066const RelaSectionTable = std.AutoArrayHashMapUnmanaged(u32, RelaSection);
60676067
6068pub const R_X86_64_ZIG_GOT32 = elf.R_X86_64_NUM + 1;
6069pub const R_X86_64_ZIG_GOTPCREL = elf.R_X86_64_NUM + 2;
6068// TODO: add comptime check we don't clobber any reloc for any ISA
6069pub const R_ZIG_GOT32: u32 = 0xff00;
6070pub const R_ZIG_GOTPCREL: u32 = 0xff01;
60706071
60716072fn defaultEntrySymbolName(cpu_arch: std.Target.Cpu.Arch) []const u8 {
60726073 return switch (cpu_arch) {
src/link/Elf/Atom.zig+105-94
......@@ -336,7 +336,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
336336 }
337337
338338 relocs_log.debug(" {s}: [{x} => {d}({s})] + {x}", .{
339 relocation.fmtRelocType(r_type, cpu_arch),
339 relocation.fmtRelocType(rel.r_type(), cpu_arch),
340340 r_offset,
341341 r_sym,
342342 target.name(elf_file),
......@@ -384,7 +384,10 @@ pub fn scanRelocsRequiresCode(self: Atom, elf_file: *Elf) bool {
384384 const cpu_arch = elf_file.getTarget().cpu.arch;
385385 for (self.relocs(elf_file)) |rel| {
386386 switch (cpu_arch) {
387 .x86_64 => if (rel.r_type() == elf.R_X86_64_GOTTPOFF) return true,
387 .x86_64 => {
388 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
389 if (r_type == .GOTTPOFF) return true;
390 },
388391 else => {},
389392 }
390393 }
......@@ -836,8 +839,9 @@ const x86_64 = struct {
836839 var i: usize = 0;
837840 while (i < rels.len) : (i += 1) {
838841 const rel = rels[i];
842 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
839843
840 if (rel.r_type() == elf.R_X86_64_NONE) continue;
844 if (r_type == .NONE) continue;
841845
842846 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
843847
......@@ -869,41 +873,41 @@ const x86_64 = struct {
869873
870874 // While traversing relocations, mark symbols that require special handling such as
871875 // pointer indirection via GOT, or a stub trampoline via PLT.
872 switch (rel.r_type()) {
873 elf.R_X86_64_64 => {
876 switch (r_type) {
877 .@"64" => {
874878 try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file);
875879 },
876880
877 elf.R_X86_64_32,
878 elf.R_X86_64_32S,
881 .@"32",
882 .@"32S",
879883 => {
880884 try atom.scanReloc(symbol, rel, dynAbsRelocAction(symbol, elf_file), elf_file);
881885 },
882886
883 elf.R_X86_64_GOT32,
884 elf.R_X86_64_GOTPC32,
885 elf.R_X86_64_GOTPC64,
886 elf.R_X86_64_GOTPCREL,
887 elf.R_X86_64_GOTPCREL64,
888 elf.R_X86_64_GOTPCRELX,
889 elf.R_X86_64_REX_GOTPCRELX,
887 .GOT32,
888 .GOTPC32,
889 .GOTPC64,
890 .GOTPCREL,
891 .GOTPCREL64,
892 .GOTPCRELX,
893 .REX_GOTPCRELX,
890894 => {
891895 symbol.flags.needs_got = true;
892896 },
893897
894 elf.R_X86_64_PLT32,
895 elf.R_X86_64_PLTOFF64,
898 .PLT32,
899 .PLTOFF64,
896900 => {
897901 if (symbol.flags.import) {
898902 symbol.flags.needs_plt = true;
899903 }
900904 },
901905
902 elf.R_X86_64_PC32 => {
906 .PC32 => {
903907 try atom.scanReloc(symbol, rel, pcRelocAction(symbol, elf_file), elf_file);
904908 },
905909
906 elf.R_X86_64_TLSGD => {
910 .TLSGD => {
907911 // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr
908912
909913 if (is_static or (!symbol.flags.import and !is_dyn_lib)) {
......@@ -918,7 +922,7 @@ const x86_64 = struct {
918922 }
919923 },
920924
921 elf.R_X86_64_TLSLD => {
925 .TLSLD => {
922926 // TODO verify followed by appropriate relocation such as PLT32 __tls_get_addr
923927
924928 if (is_static or !is_dyn_lib) {
......@@ -930,7 +934,7 @@ const x86_64 = struct {
930934 }
931935 },
932936
933 elf.R_X86_64_GOTTPOFF => {
937 .GOTTPOFF => {
934938 const should_relax = blk: {
935939 if (is_dyn_lib or symbol.flags.import) break :blk false;
936940 if (!x86_64.canRelaxGotTpOff(code.?[r_offset - 3 ..])) break :blk false;
......@@ -941,35 +945,37 @@ const x86_64 = struct {
941945 }
942946 },
943947
944 elf.R_X86_64_GOTPC32_TLSDESC => {
948 .GOTPC32_TLSDESC => {
945949 const should_relax = is_static or (!is_dyn_lib and !symbol.flags.import);
946950 if (!should_relax) {
947951 symbol.flags.needs_tlsdesc = true;
948952 }
949953 },
950954
951 elf.R_X86_64_TPOFF32,
952 elf.R_X86_64_TPOFF64,
955 .TPOFF32,
956 .TPOFF64,
953957 => {
954958 if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file);
955959 },
956960
957 elf.R_X86_64_GOTOFF64,
958 elf.R_X86_64_DTPOFF32,
959 elf.R_X86_64_DTPOFF64,
960 elf.R_X86_64_SIZE32,
961 elf.R_X86_64_SIZE64,
962 elf.R_X86_64_TLSDESC_CALL,
961 .GOTOFF64,
962 .DTPOFF32,
963 .DTPOFF64,
964 .SIZE32,
965 .SIZE64,
966 .TLSDESC_CALL,
963967 => {},
964968
965 // Zig custom relocations
966 Elf.R_X86_64_ZIG_GOT32,
967 Elf.R_X86_64_ZIG_GOTPCREL,
968 => {
969 assert(symbol.flags.has_zig_got);
970 },
969 else => |x| switch (@intFromEnum(x)) {
970 // Zig custom relocations
971 Elf.R_ZIG_GOT32,
972 Elf.R_ZIG_GOTPCREL,
973 => {
974 assert(symbol.flags.has_zig_got);
975 },
971976
972 else => try atom.reportUnhandledRelocError(rel, elf_file),
977 else => try atom.reportUnhandledRelocError(rel, elf_file),
978 },
973979 }
974980 }
975981 }
......@@ -983,8 +989,8 @@ const x86_64 = struct {
983989 var i: usize = 0;
984990 while (i < rels.len) : (i += 1) {
985991 const rel = rels[i];
986 const r_type = rel.r_type();
987 if (r_type == elf.R_X86_64_NONE) continue;
992 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
993 if (r_type == .NONE) continue;
988994
989995 const target = switch (file_ptr) {
990996 .zig_object => |x| elf_file.symbol(x.symbol(rel.r_sym())),
......@@ -1022,7 +1028,7 @@ const x86_64 = struct {
10221028 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
10231029
10241030 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
1025 relocation.fmtRelocType(r_type, .x86_64),
1031 relocation.fmtRelocType(rel.r_type(), .x86_64),
10261032 r_offset,
10271033 P,
10281034 S + A,
......@@ -1033,10 +1039,10 @@ const x86_64 = struct {
10331039
10341040 try stream.seekTo(r_offset);
10351041
1036 switch (rel.r_type()) {
1037 elf.R_X86_64_NONE => unreachable,
1042 switch (r_type) {
1043 .NONE => unreachable,
10381044
1039 elf.R_X86_64_64 => {
1045 .@"64" => {
10401046 try atom.resolveDynAbsReloc(
10411047 target,
10421048 rel,
......@@ -1046,15 +1052,15 @@ const x86_64 = struct {
10461052 );
10471053 },
10481054
1049 elf.R_X86_64_PLT32,
1050 elf.R_X86_64_PC32,
1055 .PLT32,
1056 .PC32,
10511057 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little),
10521058
1053 elf.R_X86_64_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little),
1054 elf.R_X86_64_GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little),
1055 elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little),
1059 .GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little),
1060 .GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little),
1061 .GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little),
10561062
1057 elf.R_X86_64_GOTPCRELX => {
1063 .GOTPCRELX => {
10581064 if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
10591065 x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk;
10601066 try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little);
......@@ -1063,7 +1069,7 @@ const x86_64 = struct {
10631069 try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little);
10641070 },
10651071
1066 elf.R_X86_64_REX_GOTPCRELX => {
1072 .REX_GOTPCRELX => {
10671073 if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: {
10681074 x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk;
10691075 try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little);
......@@ -1072,16 +1078,16 @@ const x86_64 = struct {
10721078 try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little);
10731079 },
10741080
1075 elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little),
1076 elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
1081 .@"32" => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little),
1082 .@"32S" => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
10771083
1078 elf.R_X86_64_TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little),
1079 elf.R_X86_64_TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little),
1084 .TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little),
1085 .TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little),
10801086
1081 elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little),
1082 elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1087 .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little),
1088 .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
10831089
1084 elf.R_X86_64_TLSGD => {
1090 .TLSGD => {
10851091 if (target.flags.has_tlsgd) {
10861092 const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file)));
10871093 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1101,7 +1107,7 @@ const x86_64 = struct {
11011107 }
11021108 },
11031109
1104 elf.R_X86_64_TLSLD => {
1110 .TLSLD => {
11051111 if (elf_file.got.tlsld_index) |entry_index| {
11061112 const tlsld_entry = elf_file.got.entries.items[entry_index];
11071113 const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file)));
......@@ -1118,7 +1124,7 @@ const x86_64 = struct {
11181124 }
11191125 },
11201126
1121 elf.R_X86_64_GOTPC32_TLSDESC => {
1127 .GOTPC32_TLSDESC => {
11221128 if (target.flags.has_tlsdesc) {
11231129 const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
11241130 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1128,12 +1134,12 @@ const x86_64 = struct {
11281134 }
11291135 },
11301136
1131 elf.R_X86_64_TLSDESC_CALL => if (!target.flags.has_tlsdesc) {
1137 .TLSDESC_CALL => if (!target.flags.has_tlsdesc) {
11321138 // call -> nop
11331139 try cwriter.writeAll(&.{ 0x66, 0x90 });
11341140 },
11351141
1136 elf.R_X86_64_GOTTPOFF => {
1142 .GOTTPOFF => {
11371143 if (target.flags.has_gottp) {
11381144 const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file)));
11391145 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
......@@ -1143,13 +1149,15 @@ const x86_64 = struct {
11431149 }
11441150 },
11451151
1146 elf.R_X86_64_GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little),
1152 .GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little),
11471153
1148 // Zig custom relocations
1149 Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little),
1150 Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little),
1154 else => |x| switch (@intFromEnum(x)) {
1155 // Zig custom relocations
1156 Elf.R_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little),
1157 Elf.R_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little),
11511158
1152 else => {},
1159 else => {},
1160 },
11531161 }
11541162 }
11551163 }
......@@ -1163,8 +1171,8 @@ const x86_64 = struct {
11631171 var i: usize = 0;
11641172 while (i < rels.len) : (i += 1) {
11651173 const rel = rels[i];
1166 const r_type = rel.r_type();
1167 if (r_type == elf.R_X86_64_NONE) continue;
1174 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
1175 if (r_type == .NONE) continue;
11681176
11691177 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
11701178
......@@ -1210,8 +1218,8 @@ const x86_64 = struct {
12101218 // Address of the dynamic thread pointer.
12111219 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
12121220
1213 relocs_log.debug(" {s}: {x}: [{x} => {x}] ({s})", .{
1214 relocation.fmtRelocType(r_type, .x86_64),
1221 relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{
1222 relocation.fmtRelocType(rel.r_type(), .x86_64),
12151223 rel.r_offset,
12161224 P,
12171225 S + A,
......@@ -1221,21 +1229,21 @@ const x86_64 = struct {
12211229 try stream.seekTo(r_offset);
12221230
12231231 switch (r_type) {
1224 elf.R_X86_64_NONE => unreachable,
1225 elf.R_X86_64_8 => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little),
1226 elf.R_X86_64_16 => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little),
1227 elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little),
1228 elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1229 elf.R_X86_64_64 => try cwriter.writeInt(i64, S + A, .little),
1230 elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1231 elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1232 elf.R_X86_64_GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little),
1233 elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little),
1234 elf.R_X86_64_SIZE32 => {
1232 .NONE => unreachable,
1233 .@"8" => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little),
1234 .@"16" => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little),
1235 .@"32" => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little),
1236 .@"32S" => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little),
1237 .@"64" => try cwriter.writeInt(i64, S + A, .little),
1238 .DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little),
1239 .DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little),
1240 .GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little),
1241 .GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little),
1242 .SIZE32 => {
12351243 const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
12361244 try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .little);
12371245 },
1238 elf.R_X86_64_SIZE64 => {
1246 .SIZE64 => {
12391247 const size = @as(i64, @intCast(target.elfSym(elf_file).st_size));
12401248 try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .little);
12411249 },
......@@ -1283,9 +1291,10 @@ const x86_64 = struct {
12831291 ) !void {
12841292 assert(rels.len == 2);
12851293 const writer = stream.writer();
1286 switch (rels[1].r_type()) {
1287 elf.R_X86_64_PC32,
1288 elf.R_X86_64_PLT32,
1294 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1295 switch (rel) {
1296 .PC32,
1297 .PLT32,
12891298 => {
12901299 var insts = [_]u8{
12911300 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
......@@ -1320,9 +1329,10 @@ const x86_64 = struct {
13201329 ) !void {
13211330 assert(rels.len == 2);
13221331 const writer = stream.writer();
1323 switch (rels[1].r_type()) {
1324 elf.R_X86_64_PC32,
1325 elf.R_X86_64_PLT32,
1332 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1333 switch (rel) {
1334 .PC32,
1335 .PLT32,
13261336 => {
13271337 var insts = [_]u8{
13281338 0x31, 0xc0, // xor %eax, %eax
......@@ -1334,8 +1344,8 @@ const x86_64 = struct {
13341344 try writer.writeAll(&insts);
13351345 },
13361346
1337 elf.R_X86_64_GOTPCREL,
1338 elf.R_X86_64_GOTPCRELX,
1347 .GOTPCREL,
1348 .GOTPCRELX,
13391349 => {
13401350 var insts = [_]u8{
13411351 0x31, 0xc0, // xor %eax, %eax
......@@ -1419,11 +1429,12 @@ const x86_64 = struct {
14191429 ) !void {
14201430 assert(rels.len == 2);
14211431 const writer = stream.writer();
1422 switch (rels[1].r_type()) {
1423 elf.R_X86_64_PC32,
1424 elf.R_X86_64_PLT32,
1425 elf.R_X86_64_GOTPCREL,
1426 elf.R_X86_64_GOTPCRELX,
1432 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1433 switch (rel) {
1434 .PC32,
1435 .PLT32,
1436 .GOTPCREL,
1437 .GOTPCRELX,
14271438 => {
14281439 var insts = [_]u8{
14291440 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax
src/link/Elf/eh_frame.zig+6-5
......@@ -541,11 +541,12 @@ const EH_PE = struct {
541541
542542const x86_64 = struct {
543543 fn resolveReloc(rel: elf.Elf64_Rela, source: i64, target: i64, data: []u8) void {
544 switch (rel.r_type()) {
545 elf.R_X86_64_32 => std.mem.writeInt(i32, data[0..4], @as(i32, @truncate(target)), .little),
546 elf.R_X86_64_64 => std.mem.writeInt(i64, data[0..8], target, .little),
547 elf.R_X86_64_PC32 => std.mem.writeInt(i32, data[0..4], @as(i32, @intCast(target - source)), .little),
548 elf.R_X86_64_PC64 => std.mem.writeInt(i64, data[0..8], target - source, .little),
544 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
545 switch (r_type) {
546 .@"32" => std.mem.writeInt(i32, data[0..4], @as(i32, @truncate(target)), .little),
547 .@"64" => std.mem.writeInt(i64, data[0..8], target, .little),
548 .PC32 => std.mem.writeInt(i32, data[0..4], @as(i32, @intCast(target - source)), .little),
549 .PC64 => std.mem.writeInt(i64, data[0..8], target - source, .little),
549550 else => unreachable,
550551 }
551552 }
src/link/Elf/relocation.zig+73-212
......@@ -11,54 +11,78 @@ pub const Kind = enum {
1111 tlsdesc,
1212};
1313
14const x86_64_relocs = [_]struct { Kind, u32 }{
15 .{ .abs, elf.R_X86_64_64 },
16 .{ .copy, elf.R_X86_64_COPY },
17 .{ .rel, elf.R_X86_64_RELATIVE },
18 .{ .irel, elf.R_X86_64_IRELATIVE },
19 .{ .glob_dat, elf.R_X86_64_GLOB_DAT },
20 .{ .jump_slot, elf.R_X86_64_JUMP_SLOT },
21 .{ .dtpmod, elf.R_X86_64_DTPMOD64 },
22 .{ .dtpoff, elf.R_X86_64_DTPOFF64 },
23 .{ .tpoff, elf.R_X86_64_TPOFF64 },
24 .{ .tlsdesc, elf.R_X86_64_TLSDESC },
25};
14fn Table(comptime len: comptime_int, comptime RelType: type, comptime mapping: [len]struct { Kind, RelType }) type {
15 return struct {
16 fn decode(r_type: u32) ?Kind {
17 inline for (mapping) |entry| {
18 if (@intFromEnum(entry[1]) == r_type) return entry[0];
19 }
20 return null;
21 }
2622
27const aarch64_relocs = [_]struct { Kind, u32 }{
28 .{ .abs, elf.R_AARCH64_ABS64 },
29 .{ .copy, elf.R_AARCH64_COPY },
30 .{ .rel, elf.R_AARCH64_RELATIVE },
31 .{ .irel, elf.R_AARCH64_IRELATIVE },
32 .{ .glob_dat, elf.R_AARCH64_GLOB_DAT },
33 .{ .jump_slot, elf.R_AARCH64_JUMP_SLOT },
34 .{ .dtpmod, elf.R_AARCH64_TLS_DTPMOD },
35 .{ .dtpoff, elf.R_AARCH64_TLS_DTPREL },
36 .{ .tpoff, elf.R_AARCH64_TLS_TPREL },
37 .{ .tlsdesc, elf.R_AARCH64_TLSDESC },
38};
23 fn encode(comptime kind: Kind) u32 {
24 inline for (mapping) |entry| {
25 if (entry[0] == kind) return @intFromEnum(entry[1]);
26 }
27 unreachable;
28 }
29 };
30}
31
32const x86_64_relocs = Table(10, elf.R_X86_64, .{
33 .{ .abs, .@"64" },
34 .{ .copy, .COPY },
35 .{ .rel, .RELATIVE },
36 .{ .irel, .IRELATIVE },
37 .{ .glob_dat, .GLOB_DAT },
38 .{ .jump_slot, .JUMP_SLOT },
39 .{ .dtpmod, .DTPMOD64 },
40 .{ .dtpoff, .DTPOFF64 },
41 .{ .tpoff, .TPOFF64 },
42 .{ .tlsdesc, .TLSDESC },
43});
44
45const aarch64_relocs = Table(10, elf.R_AARCH64, .{
46 .{ .abs, .ABS64 },
47 .{ .copy, .COPY },
48 .{ .rel, .RELATIVE },
49 .{ .irel, .IRELATIVE },
50 .{ .glob_dat, .GLOB_DAT },
51 .{ .jump_slot, .JUMP_SLOT },
52 .{ .dtpmod, .TLS_DTPMOD },
53 .{ .dtpoff, .TLS_DTPREL },
54 .{ .tpoff, .TLS_TPREL },
55 .{ .tlsdesc, .TLSDESC },
56});
57
58const riscv64_relocs = Table(9, elf.R_RISCV, .{
59 .{ .abs, .@"64" },
60 .{ .copy, .COPY },
61 .{ .rel, .RELATIVE },
62 .{ .irel, .IRELATIVE },
63 .{ .jump_slot, .JUMP_SLOT },
64 .{ .dtpmod, .TLS_DTPMOD64 },
65 .{ .dtpoff, .TLS_DTPREL64 },
66 .{ .tpoff, .TLS_TPREL64 },
67 .{ .tlsdesc, .TLSDESC },
68});
3969
4070pub fn decode(r_type: u32, cpu_arch: std.Target.Cpu.Arch) ?Kind {
41 const relocs = switch (cpu_arch) {
42 .x86_64 => &x86_64_relocs,
43 .aarch64 => &aarch64_relocs,
71 return switch (cpu_arch) {
72 .x86_64 => x86_64_relocs.decode(r_type),
73 .aarch64 => aarch64_relocs.decode(r_type),
74 .riscv64 => riscv64_relocs.decode(r_type),
4475 else => @panic("TODO unhandled cpu arch"),
4576 };
46 inline for (relocs) |entry| {
47 if (entry[1] == r_type) return entry[0];
48 }
49 return null;
5077}
5178
5279pub fn encode(comptime kind: Kind, cpu_arch: std.Target.Cpu.Arch) u32 {
53 const relocs = switch (cpu_arch) {
54 .x86_64 => &x86_64_relocs,
55 .aarch64 => &aarch64_relocs,
80 return switch (cpu_arch) {
81 .x86_64 => x86_64_relocs.encode(kind),
82 .aarch64 => aarch64_relocs.encode(kind),
83 .riscv64 => riscv64_relocs.encode(kind),
5684 else => @panic("TODO unhandled cpu arch"),
5785 };
58 inline for (relocs) |entry| {
59 if (entry[0] == kind) return entry[1];
60 }
61 unreachable;
6286}
6387
6488const FormatRelocTypeCtx = struct {
......@@ -82,183 +106,20 @@ fn formatRelocType(
82106 _ = unused_fmt_string;
83107 _ = options;
84108 const r_type = ctx.r_type;
85 const str = switch (ctx.cpu_arch) {
86 .x86_64 => switch (r_type) {
87 elf.R_X86_64_NONE => "R_X86_64_NONE",
88 elf.R_X86_64_64 => "R_X86_64_64",
89 elf.R_X86_64_PC32 => "R_X86_64_PC32",
90 elf.R_X86_64_GOT32 => "R_X86_64_GOT32",
91 elf.R_X86_64_PLT32 => "R_X86_64_PLT32",
92 elf.R_X86_64_COPY => "R_X86_64_COPY",
93 elf.R_X86_64_GLOB_DAT => "R_X86_64_GLOB_DAT",
94 elf.R_X86_64_JUMP_SLOT => "R_X86_64_JUMP_SLOT",
95 elf.R_X86_64_RELATIVE => "R_X86_64_RELATIVE",
96 elf.R_X86_64_GOTPCREL => "R_X86_64_GOTPCREL",
97 elf.R_X86_64_32 => "R_X86_64_32",
98 elf.R_X86_64_32S => "R_X86_64_32S",
99 elf.R_X86_64_16 => "R_X86_64_16",
100 elf.R_X86_64_PC16 => "R_X86_64_PC16",
101 elf.R_X86_64_8 => "R_X86_64_8",
102 elf.R_X86_64_PC8 => "R_X86_64_PC8",
103 elf.R_X86_64_DTPMOD64 => "R_X86_64_DTPMOD64",
104 elf.R_X86_64_DTPOFF64 => "R_X86_64_DTPOFF64",
105 elf.R_X86_64_TPOFF64 => "R_X86_64_TPOFF64",
106 elf.R_X86_64_TLSGD => "R_X86_64_TLSGD",
107 elf.R_X86_64_TLSLD => "R_X86_64_TLSLD",
108 elf.R_X86_64_DTPOFF32 => "R_X86_64_DTPOFF32",
109 elf.R_X86_64_GOTTPOFF => "R_X86_64_GOTTPOFF",
110 elf.R_X86_64_TPOFF32 => "R_X86_64_TPOFF32",
111 elf.R_X86_64_PC64 => "R_X86_64_PC64",
112 elf.R_X86_64_GOTOFF64 => "R_X86_64_GOTOFF64",
113 elf.R_X86_64_GOTPC32 => "R_X86_64_GOTPC32",
114 elf.R_X86_64_GOT64 => "R_X86_64_GOT64",
115 elf.R_X86_64_GOTPCREL64 => "R_X86_64_GOTPCREL64",
116 elf.R_X86_64_GOTPC64 => "R_X86_64_GOTPC64",
117 elf.R_X86_64_GOTPLT64 => "R_X86_64_GOTPLT64",
118 elf.R_X86_64_PLTOFF64 => "R_X86_64_PLTOFF64",
119 elf.R_X86_64_SIZE32 => "R_X86_64_SIZE32",
120 elf.R_X86_64_SIZE64 => "R_X86_64_SIZE64",
121 elf.R_X86_64_GOTPC32_TLSDESC => "R_X86_64_GOTPC32_TLSDESC",
122 elf.R_X86_64_TLSDESC_CALL => "R_X86_64_TLSDESC_CALL",
123 elf.R_X86_64_TLSDESC => "R_X86_64_TLSDESC",
124 elf.R_X86_64_IRELATIVE => "R_X86_64_IRELATIVE",
125 elf.R_X86_64_RELATIVE64 => "R_X86_64_RELATIVE64",
126 elf.R_X86_64_GOTPCRELX => "R_X86_64_GOTPCRELX",
127 elf.R_X86_64_REX_GOTPCRELX => "R_X86_64_REX_GOTPCRELX",
128 elf.R_X86_64_NUM => "R_X86_64_NUM",
129 else => "R_X86_64_UNKNOWN",
130 },
131 .aarch64 => switch (r_type) {
132 elf.R_AARCH64_NONE => "R_AARCH64_NONE",
133 elf.R_AARCH64_ABS64 => "R_AARCH64_ABS64",
134 elf.R_AARCH64_ABS32 => "R_AARCH64_ABS32",
135 elf.R_AARCH64_ABS16 => "R_AARCH64_ABS16",
136 elf.R_AARCH64_PREL64 => "R_AARCH64_PREL64",
137 elf.R_AARCH64_PREL32 => "R_AARCH64_PREL32",
138 elf.R_AARCH64_PREL16 => "R_AARCH64_PREL16",
139 elf.R_AARCH64_MOVW_UABS_G0 => "R_AARCH64_MOVW_UABS_G0",
140 elf.R_AARCH64_MOVW_UABS_G0_NC => "R_AARCH64_MOVW_UABS_G0_NC",
141 elf.R_AARCH64_MOVW_UABS_G1 => "R_AARCH64_MOVW_UABS_G1",
142 elf.R_AARCH64_MOVW_UABS_G1_NC => "R_AARCH64_MOVW_UABS_G1_NC",
143 elf.R_AARCH64_MOVW_UABS_G2 => "R_AARCH64_MOVW_UABS_G2",
144 elf.R_AARCH64_MOVW_UABS_G2_NC => "R_AARCH64_MOVW_UABS_G2_NC",
145 elf.R_AARCH64_MOVW_UABS_G3 => "R_AARCH64_MOVW_UABS_G3",
146 elf.R_AARCH64_MOVW_SABS_G0 => "R_AARCH64_MOVW_SABS_G0",
147 elf.R_AARCH64_MOVW_SABS_G1 => "R_AARCH64_MOVW_SABS_G1",
148 elf.R_AARCH64_MOVW_SABS_G2 => "R_AARCH64_MOVW_SABS_G2",
149 elf.R_AARCH64_LD_PREL_LO19 => "R_AARCH64_LD_PREL_LO19",
150 elf.R_AARCH64_ADR_PREL_LO21 => "R_AARCH64_ADR_PREL_LO21",
151 elf.R_AARCH64_ADR_PREL_PG_HI21 => "R_AARCH64_ADR_PREL_PG_HI21",
152 elf.R_AARCH64_ADR_PREL_PG_HI21_NC => "R_AARCH64_ADR_PREL_PG_HI21_NC",
153 elf.R_AARCH64_ADD_ABS_LO12_NC => "R_AARCH64_ADD_ABS_LO12_NC",
154 elf.R_AARCH64_LDST8_ABS_LO12_NC => "R_AARCH64_LDST8_ABS_LO12_NC",
155 elf.R_AARCH64_TSTBR14 => "R_AARCH64_TSTBR14",
156 elf.R_AARCH64_CONDBR19 => "R_AARCH64_CONDBR19",
157 elf.R_AARCH64_JUMP26 => "R_AARCH64_JUMP26",
158 elf.R_AARCH64_CALL26 => "R_AARCH64_CALL26",
159 elf.R_AARCH64_LDST16_ABS_LO12_NC => "R_AARCH64_LDST16_ABS_LO12_NC",
160 elf.R_AARCH64_LDST32_ABS_LO12_NC => "R_AARCH64_LDST32_ABS_LO12_NC",
161 elf.R_AARCH64_LDST64_ABS_LO12_NC => "R_AARCH64_LDST64_ABS_LO12_NC",
162 elf.R_AARCH64_MOVW_PREL_G0 => "R_AARCH64_MOVW_PREL_G0",
163 elf.R_AARCH64_MOVW_PREL_G0_NC => "R_AARCH64_MOVW_PREL_G0_NC",
164 elf.R_AARCH64_MOVW_PREL_G1 => "R_AARCH64_MOVW_PREL_G1",
165 elf.R_AARCH64_MOVW_PREL_G1_NC => "R_AARCH64_MOVW_PREL_G1_NC",
166 elf.R_AARCH64_MOVW_PREL_G2 => "R_AARCH64_MOVW_PREL_G2",
167 elf.R_AARCH64_MOVW_PREL_G2_NC => "R_AARCH64_MOVW_PREL_G2_NC",
168 elf.R_AARCH64_MOVW_PREL_G3 => "R_AARCH64_MOVW_PREL_G3",
169 elf.R_AARCH64_LDST128_ABS_LO12_NC => "R_AARCH64_LDST128_ABS_LO12_NC",
170 elf.R_AARCH64_MOVW_GOTOFF_G0 => "R_AARCH64_MOVW_GOTOFF_G0",
171 elf.R_AARCH64_MOVW_GOTOFF_G0_NC => "R_AARCH64_MOVW_GOTOFF_G0_NC",
172 elf.R_AARCH64_MOVW_GOTOFF_G1 => "R_AARCH64_MOVW_GOTOFF_G1",
173 elf.R_AARCH64_MOVW_GOTOFF_G1_NC => "R_AARCH64_MOVW_GOTOFF_G1_NC",
174 elf.R_AARCH64_MOVW_GOTOFF_G2 => "R_AARCH64_MOVW_GOTOFF_G2",
175 elf.R_AARCH64_MOVW_GOTOFF_G2_NC => "R_AARCH64_MOVW_GOTOFF_G2_NC",
176 elf.R_AARCH64_MOVW_GOTOFF_G3 => "R_AARCH64_MOVW_GOTOFF_G3",
177 elf.R_AARCH64_GOTREL64 => "R_AARCH64_GOTREL64",
178 elf.R_AARCH64_GOTREL32 => "R_AARCH64_GOTREL32",
179 elf.R_AARCH64_GOT_LD_PREL19 => "R_AARCH64_GOT_LD_PREL19",
180 elf.R_AARCH64_LD64_GOTOFF_LO15 => "R_AARCH64_LD64_GOTOFF_LO15",
181 elf.R_AARCH64_ADR_GOT_PAGE => "R_AARCH64_ADR_GOT_PAGE",
182 elf.R_AARCH64_LD64_GOT_LO12_NC => "R_AARCH64_LD64_GOT_LO12_NC",
183 elf.R_AARCH64_LD64_GOTPAGE_LO15 => "R_AARCH64_LD64_GOTPAGE_LO15",
184 elf.R_AARCH64_TLSGD_ADR_PREL21 => "R_AARCH64_TLSGD_ADR_PREL21",
185 elf.R_AARCH64_TLSGD_ADR_PAGE21 => "R_AARCH64_TLSGD_ADR_PAGE21",
186 elf.R_AARCH64_TLSGD_ADD_LO12_NC => "R_AARCH64_TLSGD_ADD_LO12_NC",
187 elf.R_AARCH64_TLSGD_MOVW_G1 => "R_AARCH64_TLSGD_MOVW_G1",
188 elf.R_AARCH64_TLSGD_MOVW_G0_NC => "R_AARCH64_TLSGD_MOVW_G0_NC",
189 elf.R_AARCH64_TLSLD_ADR_PREL21 => "R_AARCH64_TLSLD_ADR_PREL21",
190 elf.R_AARCH64_TLSLD_ADR_PAGE21 => "R_AARCH64_TLSLD_ADR_PAGE21",
191 elf.R_AARCH64_TLSLD_ADD_LO12_NC => "R_AARCH64_TLSLD_ADD_LO12_NC",
192 elf.R_AARCH64_TLSLD_MOVW_G1 => "R_AARCH64_TLSLD_MOVW_G1",
193 elf.R_AARCH64_TLSLD_MOVW_G0_NC => "R_AARCH64_TLSLD_MOVW_G0_NC",
194 elf.R_AARCH64_TLSLD_LD_PREL19 => "R_AARCH64_TLSLD_LD_PREL19",
195 elf.R_AARCH64_TLSLD_MOVW_DTPREL_G2 => "R_AARCH64_TLSLD_MOVW_DTPREL_G2",
196 elf.R_AARCH64_TLSLD_MOVW_DTPREL_G1 => "R_AARCH64_TLSLD_MOVW_DTPREL_G1",
197 elf.R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC => "R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC",
198 elf.R_AARCH64_TLSLD_MOVW_DTPREL_G0 => "R_AARCH64_TLSLD_MOVW_DTPREL_G0",
199 elf.R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC => "R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC",
200 elf.R_AARCH64_TLSLD_ADD_DTPREL_HI12 => "R_AARCH64_TLSLD_ADD_DTPREL_HI12",
201 elf.R_AARCH64_TLSLD_ADD_DTPREL_LO12 => "R_AARCH64_TLSLD_ADD_DTPREL_LO12",
202 elf.R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC => "R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC",
203 elf.R_AARCH64_TLSLD_LDST8_DTPREL_LO12 => "R_AARCH64_TLSLD_LDST8_DTPREL_LO12",
204 elf.R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC => "R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC",
205 elf.R_AARCH64_TLSLD_LDST16_DTPREL_LO12 => "R_AARCH64_TLSLD_LDST16_DTPREL_LO12",
206 elf.R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC => "R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC",
207 elf.R_AARCH64_TLSLD_LDST32_DTPREL_LO12 => "R_AARCH64_TLSLD_LDST32_DTPREL_LO12",
208 elf.R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC => "R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC",
209 elf.R_AARCH64_TLSLD_LDST64_DTPREL_LO12 => "R_AARCH64_TLSLD_LDST64_DTPREL_LO12",
210 elf.R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC => "R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC",
211 elf.R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 => "R_AARCH64_TLSIE_MOVW_GOTTPREL_G1",
212 elf.R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC => "R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC",
213 elf.R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 => "R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21",
214 elf.R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC => "R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC",
215 elf.R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 => "R_AARCH64_TLSIE_LD_GOTTPREL_PREL19",
216 elf.R_AARCH64_TLSLE_MOVW_TPREL_G2 => "R_AARCH64_TLSLE_MOVW_TPREL_G2",
217 elf.R_AARCH64_TLSLE_MOVW_TPREL_G1 => "R_AARCH64_TLSLE_MOVW_TPREL_G1",
218 elf.R_AARCH64_TLSLE_MOVW_TPREL_G1_NC => "R_AARCH64_TLSLE_MOVW_TPREL_G1_NC",
219 elf.R_AARCH64_TLSLE_MOVW_TPREL_G0 => "R_AARCH64_TLSLE_MOVW_TPREL_G0",
220 elf.R_AARCH64_TLSLE_MOVW_TPREL_G0_NC => "R_AARCH64_TLSLE_MOVW_TPREL_G0_NC",
221 elf.R_AARCH64_TLSLE_ADD_TPREL_HI12 => "R_AARCH64_TLSLE_ADD_TPREL_HI12",
222 elf.R_AARCH64_TLSLE_ADD_TPREL_LO12 => "R_AARCH64_TLSLE_ADD_TPREL_LO12",
223 elf.R_AARCH64_TLSLE_ADD_TPREL_LO12_NC => "R_AARCH64_TLSLE_ADD_TPREL_LO12_NC",
224 elf.R_AARCH64_TLSLE_LDST8_TPREL_LO12 => "R_AARCH64_TLSLE_LDST8_TPREL_LO12",
225 elf.R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC => "R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC",
226 elf.R_AARCH64_TLSLE_LDST16_TPREL_LO12 => "R_AARCH64_TLSLE_LDST16_TPREL_LO12",
227 elf.R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC => "R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC",
228 elf.R_AARCH64_TLSLE_LDST32_TPREL_LO12 => "R_AARCH64_TLSLE_LDST32_TPREL_LO12",
229 elf.R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC => "R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC",
230 elf.R_AARCH64_TLSLE_LDST64_TPREL_LO12 => "R_AARCH64_TLSLE_LDST64_TPREL_LO12",
231 elf.R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC => "R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC",
232 elf.R_AARCH64_TLSDESC_LD_PREL19 => "R_AARCH64_TLSDESC_LD_PREL19",
233 elf.R_AARCH64_TLSDESC_ADR_PREL21 => "R_AARCH64_TLSDESC_ADR_PREL21",
234 elf.R_AARCH64_TLSDESC_ADR_PAGE21 => "R_AARCH64_TLSDESC_ADR_PAGE21",
235 elf.R_AARCH64_TLSDESC_LD64_LO12 => "R_AARCH64_TLSDESC_LD64_LO12",
236 elf.R_AARCH64_TLSDESC_ADD_LO12 => "R_AARCH64_TLSDESC_ADD_LO12",
237 elf.R_AARCH64_TLSDESC_OFF_G1 => "R_AARCH64_TLSDESC_OFF_G1",
238 elf.R_AARCH64_TLSDESC_OFF_G0_NC => "R_AARCH64_TLSDESC_OFF_G0_NC",
239 elf.R_AARCH64_TLSDESC_LDR => "R_AARCH64_TLSDESC_LDR",
240 elf.R_AARCH64_TLSDESC_ADD => "R_AARCH64_TLSDESC_ADD",
241 elf.R_AARCH64_TLSDESC_CALL => "R_AARCH64_TLSDESC_CALL",
242 elf.R_AARCH64_TLSLE_LDST128_TPREL_LO12 => "R_AARCH64_TLSLE_LDST128_TPREL_LO12",
243 elf.R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC => "R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC",
244 elf.R_AARCH64_TLSLD_LDST128_DTPREL_LO12 => "R_AARCH64_TLSLD_LDST128_DTPREL_LO12",
245 elf.R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC => "R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC",
246 elf.R_AARCH64_COPY => "R_AARCH64_COPY",
247 elf.R_AARCH64_GLOB_DAT => "R_AARCH64_GLOB_DAT",
248 elf.R_AARCH64_JUMP_SLOT => "R_AARCH64_JUMP_SLOT",
249 elf.R_AARCH64_RELATIVE => "R_AARCH64_RELATIVE",
250 elf.R_AARCH64_TLS_DTPMOD => "R_AARCH64_TLS_DTPMOD",
251 elf.R_AARCH64_TLS_DTPREL => "R_AARCH64_TLS_DTPREL",
252 elf.R_AARCH64_TLS_TPREL => "R_AARCH64_TLS_TPREL",
253 elf.R_AARCH64_TLSDESC => "R_AARCH64_TLSDESC",
254 elf.R_AARCH64_IRELATIVE => "R_AARCH64_IRELATIVE",
255 else => "R_AARCH64_UNKNOWN",
109 switch (r_type) {
110 Elf.R_ZIG_GOT32 => try writer.writeAll("R_ZIG_GOT32"),
111 Elf.R_ZIG_GOTPCREL => try writer.writeAll("R_ZIG_GOTPCREL"),
112 else => switch (ctx.cpu_arch) {
113 .x86_64 => try writer.print("R_X86_64_{s}", .{@tagName(@as(elf.R_X86_64, @enumFromInt(r_type)))}),
114 .aarch64 => try writer.print("R_AARCH64_{s}", .{@tagName(@as(elf.R_AARCH64, @enumFromInt(r_type)))}),
115 .riscv64 => try writer.print("R_RISCV_{s}", .{@tagName(@as(elf.R_RISCV, @enumFromInt(r_type)))}),
116 else => unreachable,
256117 },
257 else => unreachable,
258 };
259 try writer.print("{s}", .{str});
118 }
260119}
261120
262121const assert = std.debug.assert;
263122const elf = std.elf;
264123const std = @import("std");
124
125const Elf = @import("../Elf.zig");