rustld 0.1.55

A modern ELF loader (static & dynamic linker + compatible glibc & musl) written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
#[cfg(debug_assertions)]
use crate::libc::fs::write;
/// Stub implementations of _dl_* symbols that glibc expects from the dynamic linker
/// These are minimal no-op implementations to allow programs to run
use crate::{
    elf::symbol::Symbol,
    elf::thread_local_storage::ThreadControlBlock,
    elf::{header::ElfHeader, program_header::ProgramHeader},
    linking,
    syscall::thread_pointer::get_thread_pointer,
    tls,
};
use core::ffi::{c_char, c_long, c_void};
use core::mem::size_of;
use core::sync::atomic::{AtomicI32, AtomicU32, AtomicUsize, Ordering};

const SHN_ABS: u16 = 0xfff1;

#[cfg(target_arch = "x86_64")]
const SIGJMP_WORDS: usize = 32;
#[cfg(target_arch = "aarch64")]
const SIGJMP_WORDS: usize = 48;
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
const SIGJMP_WORDS: usize = 48;

#[repr(C)]
pub(crate) struct TlsIndex {
    pub(crate) ti_module: usize,
    pub(crate) ti_offset: usize,
}

#[repr(C, align(16))]
struct SigJmpBuf {
    // Conservative storage for glibc sigjmp_buf by architecture.
    // x86_64: ~200 bytes; aarch64: ~312 bytes.
    storage: [usize; SIGJMP_WORDS],
}

struct CatchErrorFrame {
    prev: *mut CatchErrorFrame,
    env: SigJmpBuf,
    objname: *mut *const c_char,
    errstring: *mut *const c_char,
    mallocedp: *mut u8,
    errcode: i32,
}

#[repr(C)]
struct DlException {
    objname: *const c_char,
    errstring: *const c_char,
    message_buffer: *mut c_char,
}

#[derive(Clone, Copy)]
struct CatchErrorThreadSlot {
    tid: i32,
    top: *mut CatchErrorFrame,
}

#[repr(C)]
struct LookupLinkMap {
    l_addr: usize,
    l_name: *const c_char,
    l_ld: *const u8,
}

const MAX_CATCH_ERROR_THREADS: usize = 128;
static CATCH_ERROR_STATE_LOCK: AtomicI32 = AtomicI32::new(0);
static mut CATCH_ERROR_SLOTS: [CatchErrorThreadSlot; MAX_CATCH_ERROR_THREADS] =
    [CatchErrorThreadSlot {
        tid: 0,
        top: core::ptr::null_mut(),
    }; MAX_CATCH_ERROR_THREADS];

static mut RTLD_LOOKUP_MAP: LookupLinkMap = LookupLinkMap {
    l_addr: 0,
    l_name: core::ptr::null(),
    l_ld: core::ptr::null(),
};
static mut DLERROR_BUF: [u8; 256] = [0; 256];
static mut DLERROR_PENDING: bool = false;
const DLERROR_BUF_SIZE: usize = 256;

// Runtime linker operations can be entered concurrently from multiple threads
// (dlopen/dlsym and rtld lookup callbacks). Guard access to the mutable
// DynamicLinker state with a small re-entrant spin lock keyed by TID.
static RTLD_LOCK_OWNER_TID: AtomicI32 = AtomicI32::new(0);
static RTLD_LOCK_DEPTH: AtomicU32 = AtomicU32::new(0);

#[cfg(target_arch = "x86_64")]
const TUNABLE_FORWARD_NONE: usize = 1;
#[cfg(target_arch = "x86_64")]
static TUNABLE_GET_VAL_FORWARD_ADDR: AtomicUsize = AtomicUsize::new(0);
#[cfg(target_arch = "x86_64")]
static TUNABLE_IS_INIT_FORWARD_ADDR: AtomicUsize = AtomicUsize::new(0);

#[cfg(debug_assertions)]
static STUB_TRACE_REMAINING: AtomicU32 = AtomicU32::new(200);

#[cfg(debug_assertions)]
#[inline(always)]
fn debug_stub_trace(name: &str) {
    if STUB_TRACE_REMAINING
        .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |v| {
            if v > 0 {
                Some(v - 1)
            } else {
                None
            }
        })
        .is_ok()
    {
        eprintln!("ld_stub: {}", name);
    }
}

#[cfg(not(debug_assertions))]
#[inline(always)]
fn debug_stub_trace(_name: &str) {}

#[cfg(target_arch = "x86_64")]
#[inline(always)]
fn allow_tunable_forwarding() -> bool {
    std::env::var("RUSTLD_FORWARD_GLIBC_TUNABLES")
        .ok()
        .map(|raw| {
            matches!(
                raw.trim().to_ascii_lowercase().as_str(),
                "1" | "true" | "yes" | "on"
            )
        })
        .unwrap_or(false)
}

#[cfg(target_arch = "x86_64")]
#[inline(always)]
fn resolve_tunable_forward_addr(
    cache: &AtomicUsize,
    symbol_name: &str,
    self_addr: usize,
) -> Option<usize> {
    if !allow_tunable_forwarding() {
        return None;
    }

    let cached = cache.load(Ordering::Relaxed);
    if cached == TUNABLE_FORWARD_NONE {
        return None;
    }
    if cached > TUNABLE_FORWARD_NONE {
        return Some(cached);
    }

    let resolved = (unsafe { linking::lookup_active_symbol(symbol_name) })
        .or_else(|| resolve_host_rtld_symbol(symbol_name));

    let Some(resolved) = resolved else {
        cache.store(TUNABLE_FORWARD_NONE, Ordering::Relaxed);
        return None;
    };
    if resolved == self_addr {
        cache.store(TUNABLE_FORWARD_NONE, Ordering::Relaxed);
        return None;
    }

    cache.store(resolved, Ordering::Relaxed);
    Some(resolved)
}

#[cfg(target_arch = "x86_64")]
#[inline(always)]
fn seed_current_glibc_thread_locale() {
    type UselocaleFn = extern "C" fn(*mut c_void) -> *mut c_void;
    type CTypeInitFn = extern "C" fn();

    let uselocale_addr = unsafe {
        linking::lookup_active_symbol("__uselocale")
            .or_else(|| linking::lookup_active_symbol("uselocale"))
    };
    if let Some(addr) = uselocale_addr {
        let uselocale_fn: UselocaleFn = unsafe { core::mem::transmute(addr) };
        let _ = uselocale_fn(usize::MAX as *mut c_void);
    }

    let ctype_init_addr = unsafe { linking::lookup_active_symbol("__ctype_init") };
    if let Some(addr) = ctype_init_addr {
        let ctype_init_fn: CTypeInitFn = unsafe { core::mem::transmute(addr) };
        ctype_init_fn();
    }
}

#[cfg(not(target_arch = "x86_64"))]
#[inline(always)]
fn seed_current_glibc_thread_locale() {}

#[inline(always)]
fn trace_thread_tls() -> bool {
    static TRACE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *TRACE.get_or_init(|| std::env::var("RUSTLD_TRACE_THREAD_TLS").is_ok())
}

#[inline(always)]
fn force_fresh_thread_tls() -> bool {
    static FORCE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *FORCE.get_or_init(|| std::env::var("RUSTLD_FORCE_FRESH_THREAD_TLS").is_ok())
}

#[inline(always)]
fn trace_rtld_lookup() -> bool {
    static TRACE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *TRACE.get_or_init(|| std::env::var("RUSTLD_TRACE_RTLD_LOOKUP").is_ok())
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Clone, Copy)]
struct Elf64SectionHeader {
    sh_name: u32,
    sh_type: u32,
    sh_flags: u64,
    sh_addr: u64,
    sh_offset: u64,
    sh_size: u64,
    sh_link: u32,
    sh_info: u32,
    sh_addralign: u64,
    sh_entsize: u64,
}

#[cfg(target_arch = "x86_64")]
#[repr(C)]
#[derive(Clone, Copy)]
struct Elf64Sym {
    st_name: u32,
    st_info: u8,
    st_other: u8,
    st_shndx: u16,
    st_value: u64,
    st_size: u64,
}

#[cfg(target_arch = "x86_64")]
const SHT_DYNSYM: u32 = 11;

#[cfg(target_arch = "x86_64")]
fn parse_maps_hex(raw: &str) -> Option<usize> {
    usize::from_str_radix(raw, 16).ok()
}

#[cfg(target_arch = "x86_64")]
fn find_host_rtld_base_and_path() -> Option<(usize, String)> {
    let maps = std::fs::read_to_string("/proc/self/maps").ok()?;
    for line in maps.lines() {
        if !line.contains("ld-linux") {
            continue;
        }

        let mut fields = line.split_whitespace();
        let range = fields.next()?;
        let _perms = fields.next()?;
        let file_offset = fields.next()?;
        let _dev = fields.next()?;
        let _inode = fields.next()?;
        let path = fields.next()?;

        if !path.contains("ld-linux") {
            continue;
        }

        let start_hex = range.split('-').next()?;
        let start = parse_maps_hex(start_hex)?;
        let offset = parse_maps_hex(file_offset)?;
        let base = start.checked_sub(offset)?;
        return Some((base, path.to_string()));
    }
    None
}

#[cfg(target_arch = "x86_64")]
fn dynsym_name_matches(candidate: &[u8], wanted: &str) -> bool {
    if candidate == wanted.as_bytes() {
        return true;
    }
    candidate.len() > wanted.len()
        && candidate[wanted.len()] == b'@'
        && &candidate[..wanted.len()] == wanted.as_bytes()
}

#[cfg(target_arch = "x86_64")]
fn resolve_dynsym_value(path: &str, symbol_name: &str) -> Option<usize> {
    let bytes = std::fs::read(path).ok()?;
    if bytes.len() < size_of::<ElfHeader>() {
        return None;
    }

    let header = unsafe { core::ptr::read_unaligned(bytes.as_ptr().cast::<ElfHeader>()) };
    if header.e_ident[0..4] != [0x7f, b'E', b'L', b'F'] {
        return None;
    }

    let shoff = header.e_shoff;
    let shentsize = header.e_shentsize as usize;
    let shnum = header.e_shnum as usize;
    if shentsize < size_of::<Elf64SectionHeader>() || shnum == 0 {
        return None;
    }

    let read_shdr = |index: usize| -> Option<Elf64SectionHeader> {
        if index >= shnum {
            return None;
        }
        let off = shoff.checked_add(index.checked_mul(shentsize)?)?;
        let end = off.checked_add(size_of::<Elf64SectionHeader>())?;
        if end > bytes.len() {
            return None;
        }
        Some(unsafe {
            core::ptr::read_unaligned(bytes.as_ptr().add(off).cast::<Elf64SectionHeader>())
        })
    };

    for sec_idx in 0..shnum {
        let shdr = read_shdr(sec_idx)?;
        if shdr.sh_type != SHT_DYNSYM {
            continue;
        }

        let strtab = read_shdr(shdr.sh_link as usize)?;
        let strtab_start = strtab.sh_offset as usize;
        let strtab_len = strtab.sh_size as usize;
        if strtab_start
            .checked_add(strtab_len)
            .is_none_or(|end| end > bytes.len())
        {
            continue;
        }
        let strtab_bytes = &bytes[strtab_start..strtab_start + strtab_len];

        let sym_size = if shdr.sh_entsize == 0 {
            size_of::<Elf64Sym>()
        } else {
            shdr.sh_entsize as usize
        };
        if sym_size < size_of::<Elf64Sym>() {
            continue;
        }

        let sym_start = shdr.sh_offset as usize;
        let sym_len = shdr.sh_size as usize;
        if sym_start
            .checked_add(sym_len)
            .is_none_or(|end| end > bytes.len())
        {
            continue;
        }
        let sym_count = sym_len / sym_size;

        for sym_idx in 0..sym_count {
            let off = sym_start + sym_idx * sym_size;
            if off
                .checked_add(size_of::<Elf64Sym>())
                .is_none_or(|end| end > bytes.len())
            {
                break;
            }
            let sym = unsafe { core::ptr::read_unaligned(bytes.as_ptr().add(off).cast::<Elf64Sym>()) };
            if sym.st_name == 0 {
                continue;
            }

            let name_off = sym.st_name as usize;
            if name_off >= strtab_bytes.len() {
                continue;
            }
            let tail = &strtab_bytes[name_off..];
            let nul = tail.iter().position(|&b| b == 0)?;
            let name = &tail[..nul];
            if dynsym_name_matches(name, symbol_name) {
                return Some(sym.st_value as usize);
            }
        }
    }

    None
}

#[cfg(target_arch = "x86_64")]
fn resolve_host_rtld_symbol(symbol_name: &str) -> Option<usize> {
    let (base, path) = find_host_rtld_base_and_path()?;
    let value = resolve_dynsym_value(&path, symbol_name)?;
    Some(base.wrapping_add(value))
}

#[cfg(target_arch = "x86_64")]
pub(crate) fn host_rtld_global_ro_ptr() -> Option<*const u8> {
    resolve_host_rtld_symbol("_rtld_global_ro").map(|addr| addr as *const u8)
}

#[cfg(not(target_arch = "x86_64"))]
pub(crate) fn host_rtld_global_ro_ptr() -> Option<*const u8> {
    None
}

struct RtldOpGuard {
    tid: i32,
    locked: bool,
}

impl Drop for RtldOpGuard {
    fn drop(&mut self) {
        if !self.locked || self.tid <= 0 {
            return;
        }

        if RTLD_LOCK_OWNER_TID.load(Ordering::Acquire) != self.tid {
            return;
        }

        let depth = RTLD_LOCK_DEPTH.load(Ordering::Acquire);
        if depth <= 1 {
            RTLD_LOCK_DEPTH.store(0, Ordering::Release);
            RTLD_LOCK_OWNER_TID.store(0, Ordering::Release);
        } else {
            RTLD_LOCK_DEPTH.store(depth - 1, Ordering::Release);
        }
    }
}

#[inline(always)]
fn current_tid() -> i32 {
    unsafe { syscall(SYS_GETTID) as i32 }
}

#[inline(always)]
fn current_pid() -> i32 {
    unsafe { syscall(SYS_GETPID) as i32 }
}

#[inline(always)]
fn thread_still_alive(tid: i32) -> bool {
    if tid <= 0 {
        return false;
    }
    // tgkill(pid, tid, 0): kernel existence check for a specific thread.
    let rc = unsafe { syscall(SYS_TGKILL, current_pid() as c_long, tid as c_long, 0 as c_long) };
    // 0 => exists; -ESRCH => does not exist; other errors conservatively treated as alive.
    rc == 0 || rc != -3
}

#[inline(always)]
fn force_unlock_rtld_ops_if_owned_by_current_thread() {
    let tid = current_tid();
    if tid <= 0 {
        return;
    }
    if RTLD_LOCK_OWNER_TID.load(Ordering::Acquire) == tid {
        RTLD_LOCK_DEPTH.store(0, Ordering::Release);
        RTLD_LOCK_OWNER_TID.store(0, Ordering::Release);
    }
}

#[inline(always)]
fn lock_catch_error_state() {
    while CATCH_ERROR_STATE_LOCK
        .compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
        .is_err()
    {
        core::hint::spin_loop();
    }
}

#[inline(always)]
fn unlock_catch_error_state() {
    CATCH_ERROR_STATE_LOCK.store(0, Ordering::Release);
}

unsafe fn catch_error_get_top(tid: i32) -> *mut CatchErrorFrame {
    lock_catch_error_state();
    let mut top = core::ptr::null_mut();
    let mut idx = 0usize;
    while idx < MAX_CATCH_ERROR_THREADS {
        let slot = core::ptr::addr_of!(CATCH_ERROR_SLOTS[idx]);
        if (*slot).tid == tid {
            top = (*slot).top;
            break;
        }
        idx += 1;
    }
    unlock_catch_error_state();
    top
}

unsafe fn catch_error_set_top(tid: i32, top: *mut CatchErrorFrame) {
    lock_catch_error_state();
    let mut empty_slot: Option<usize> = None;
    let mut idx = 0usize;
    while idx < MAX_CATCH_ERROR_THREADS {
        let slot = core::ptr::addr_of_mut!(CATCH_ERROR_SLOTS[idx]);
        if (*slot).tid == tid {
            (*slot).top = top;
            if top.is_null() {
                (*slot).tid = 0;
            }
            unlock_catch_error_state();
            return;
        }
        if empty_slot.is_none() && (*slot).tid == 0 {
            empty_slot = Some(idx);
        }
        idx += 1;
    }

    if let Some(idx) = empty_slot {
        let slot = core::ptr::addr_of_mut!(CATCH_ERROR_SLOTS[idx]);
        (*slot).tid = tid;
        (*slot).top = top;
    } else {
        // Keep running even if we exceed the slot budget.
        let slot = core::ptr::addr_of_mut!(CATCH_ERROR_SLOTS[0]);
        (*slot).tid = tid;
        (*slot).top = top;
    }
    unlock_catch_error_state();
}

unsafe fn catch_error_restore_top(
    tid: i32,
    expected: *mut CatchErrorFrame,
    prev: *mut CatchErrorFrame,
) {
    lock_catch_error_state();
    let mut idx = 0usize;
    while idx < MAX_CATCH_ERROR_THREADS {
        let slot = core::ptr::addr_of_mut!(CATCH_ERROR_SLOTS[idx]);
        if (*slot).tid == tid {
            if (*slot).top == expected {
                (*slot).top = prev;
                if prev.is_null() {
                    (*slot).tid = 0;
                }
            }
            unlock_catch_error_state();
            return;
        }
        idx += 1;
    }
    unlock_catch_error_state();
}

#[inline(always)]
fn lock_rtld_ops() -> RtldOpGuard {
    let tid = current_tid();
    if tid <= 0 {
        return RtldOpGuard {
            tid: 0,
            locked: false,
        };
    }

    let mut spins = 0usize;
    loop {
        let owner = RTLD_LOCK_OWNER_TID.load(Ordering::Acquire);
        let depth = RTLD_LOCK_DEPTH.load(Ordering::Acquire);

        if owner == tid {
            RTLD_LOCK_DEPTH.store(depth.saturating_add(1), Ordering::Release);
            return RtldOpGuard { tid, locked: true };
        }

        if owner == 0 {
            if RTLD_LOCK_OWNER_TID
                .compare_exchange(0, tid, Ordering::AcqRel, Ordering::Acquire)
                .is_ok()
            {
                RTLD_LOCK_DEPTH.store(1, Ordering::Release);
                return RtldOpGuard { tid, locked: true };
            }
            core::hint::spin_loop();
            continue;
        }

        // Recover from stale ownership metadata if a thread exited while
        // holding the lock or lock metadata was left inconsistent.
        if depth == 0 || !thread_still_alive(owner) {
            if RTLD_LOCK_OWNER_TID
                .compare_exchange(owner, 0, Ordering::AcqRel, Ordering::Acquire)
                .is_ok()
            {
                RTLD_LOCK_DEPTH.store(0, Ordering::Release);
                continue;
            }
        }

        spins = spins.wrapping_add(1);
        if spins & 0x3ff == 0 {
            // Keep spinning lock simple/no-allocation: just issue a pause hint.
            core::hint::spin_loop();
        } else {
            core::hint::spin_loop();
        }
    }
}

#[inline]
fn persist_rtld_lookup_symbol(symbol: Symbol) -> *const Symbol {
    Box::into_raw(Box::new(symbol))
}

#[no_mangle]
pub static mut __rustld_last_alloc_tls_enter: *mut () = core::ptr::null_mut();
#[no_mangle]
pub static mut __rustld_last_alloc_tls_ret: *mut () = core::ptr::null_mut();
#[no_mangle]
pub static mut __rustld_last_alloc_tls_init_arg: *mut () = core::ptr::null_mut();
#[no_mangle]
pub static mut __rustld_last_alloc_tls_init_ret: *mut () = core::ptr::null_mut();

extern "C" {
    #[link_name = "__sigsetjmp"]
    fn sigsetjmp(env: *mut SigJmpBuf, savemask: i32) -> i32;
    fn siglongjmp(env: *mut SigJmpBuf, val: i32) -> !;
    fn syscall(number: c_long, ...) -> c_long;
    static __ehdr_start: ElfHeader;
}

#[cfg(target_arch = "x86_64")]
const SYS_GETPID: c_long = 39;
#[cfg(target_arch = "x86_64")]
const SYS_GETTID: c_long = 186;
#[cfg(target_arch = "x86_64")]
const SYS_TGKILL: c_long = 234;

#[cfg(target_arch = "aarch64")]
const SYS_GETPID: c_long = 172;
#[cfg(target_arch = "aarch64")]
const SYS_GETTID: c_long = 178;
#[cfg(target_arch = "aarch64")]
const SYS_TGKILL: c_long = 131;

#[repr(C)]
struct DtvEntry {
    value: usize,
    to_free: usize,
}

#[repr(C)]
pub(crate) struct DlFindObject {
    dlfo_flags: u64,
    dlfo_map_start: *mut c_void,
    dlfo_map_end: *mut c_void,
    dlfo_link_map: *mut c_void,
    dlfo_eh_frame: *mut c_void,
    dlfo_sframe: *mut c_void,
    dlfo_reserved: [u64; 6],
}

#[repr(C)]
struct RDebug {
    r_version: i32,
    r_map: *mut c_void,
    r_brk: usize,
    r_state: i32,
    r_ldbase: usize,
}

#[unsafe(no_mangle)]
static mut _r_debug: RDebug = RDebug {
    r_version: 1,
    r_map: core::ptr::null_mut(),
    r_brk: 0,
    r_state: 0,
    r_ldbase: 0,
};

#[no_mangle]
pub extern "C" fn _dl_audit_preinit() {
    // Called by __libc_start_main before initialization
    // No-op for now
}

#[no_mangle]
pub extern "C" fn __libc_freeres() {
    // Valgrind's preload library calls this at exit. Skip freeing
    // libc internals to avoid touching uninitialized rtld state.
}

#[no_mangle]
pub extern "C" fn _dl_fini() {
    // Keep rtld_fini as a no-op so process exit can complete reliably.
    // Invoking custom fini walks here can recurse into partially-torn-down
    // runtime state for some binaries.
    // Keep rtld_fini as a no-op so process exit can complete.
}

type DlsymEntry = unsafe extern "C" fn(*mut c_void, *const u8) -> *mut c_void;
type DlvsymEntry = unsafe extern "C" fn(*mut c_void, *const u8, *const u8) -> *mut c_void;
type DlopenEntry = unsafe extern "C" fn(*const u8, i32) -> *mut c_void;
type DlcloseEntry = unsafe extern "C" fn(*mut c_void) -> i32;
type DlerrorEntry = unsafe extern "C" fn() -> *const c_char;

#[unsafe(no_mangle)]
pub static mut __rustld_dlsym_entry: DlsymEntry = __rustld_dlsym_entry_impl;

#[unsafe(no_mangle)]
pub static mut __rustld_dlvsym_entry: DlvsymEntry = __rustld_dlvsym_entry_impl;

#[unsafe(no_mangle)]
pub static mut __rustld_dlopen_entry: DlopenEntry = __rustld_dlopen_entry_impl;

#[unsafe(no_mangle)]
pub static mut __rustld_dlclose_entry: DlcloseEntry = __rustld_dlclose_entry_impl;

#[unsafe(no_mangle)]
pub static mut __rustld_dlerror_entry: DlerrorEntry = __rustld_dlerror_entry_impl;

#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn __rustld_dlsym_entry_impl(
    handle: *mut c_void,
    name: *const u8,
) -> *mut c_void {
    let resolved = dlsym_impl(handle, name);
    #[cfg(debug_assertions)]
    {
        log_dl_symbol("dlsym", name, resolved as usize);
    }
    resolved
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn dlsym(handle: *mut c_void, name: *const u8) -> *mut c_void {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlsym_entry)) };
    unsafe { entry(handle, name) }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn __rustld_dlvsym_entry_impl(
    handle: *mut c_void,
    name_ptr: *const u8,
    version_ptr: *const u8,
) -> *mut c_void {
    let resolved = if version_ptr.is_null() {
        dlsym_impl(handle, name_ptr)
    } else {
        let name = c_string(name_ptr, 512);
        let version = c_string(version_ptr, 128);
        if let (Some(name), Some(ver)) = (name, version) {
            let resolved = resolve_symbol_with_version(handle, name, ver)
                .or_else(|| resolve_symbol_for_handle(handle, name))
                .unwrap_or(core::ptr::null_mut());
            log_suspicious_runtime_symbol("dlvsym", name, resolved as usize);
            resolved
        } else {
            dlsym_impl(handle, name_ptr)
        }
    };
    #[cfg(debug_assertions)]
    {
        log_dl_symbol("dlvsym", name_ptr, resolved as usize);
    }
    resolved
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn dlvsym(handle: *mut c_void, name: *const u8, version: *const u8) -> *mut c_void {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlvsym_entry)) };
    unsafe { entry(handle, name, version) }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn __rustld_dlopen_entry_impl(file: *const u8, mode: i32) -> *mut c_void {
    #[cfg(debug_assertions)]
    {
        log_dl_symbol("dlopen", file, 0);
    }
    dlopen_impl(file, mode)
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn dlopen(file: *const u8, mode: i32) -> *mut c_void {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlopen_entry)) };
    unsafe { entry(file, mode) }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn __rustld_dlclose_entry_impl(_handle: *mut c_void) -> i32 {
    clear_dlerror();
    0
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn dlclose(handle: *mut c_void) -> i32 {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlclose_entry)) };
    unsafe { entry(handle) }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub unsafe extern "C" fn __rustld_dlerror_entry_impl() -> *const c_char {
    unsafe {
        if DLERROR_PENDING {
            DLERROR_PENDING = false;
            core::ptr::addr_of!(DLERROR_BUF).cast::<c_char>()
        } else {
            core::ptr::null()
        }
    }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn dlerror() -> *const c_char {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlerror_entry)) };
    unsafe { entry() }
}

#[repr(C)]
struct DlPhdrInfo {
    dlpi_addr: usize,
    dlpi_name: *const c_char,
    dlpi_phdr: *const ProgramHeader,
    dlpi_phnum: u16,
    dlpi_adds: u64,
    dlpi_subs: u64,
    dlpi_tls_modid: usize,
    dlpi_tls_data: *mut c_void,
}

type DlIteratePhdrCallback = extern "C" fn(*mut c_void, usize, *mut c_void) -> i32;

#[unsafe(no_mangle)]
pub extern "C" fn dl_iterate_phdr(
    callback: Option<DlIteratePhdrCallback>,
    data: *mut c_void,
) -> i32 {
    const EMPTY_NAME: *const c_char = b"\0".as_ptr().cast::<c_char>();

    let Some(callback) = callback else {
        return 0;
    };

    let _guard = lock_rtld_ops();
    let linker = unsafe { linking::active_linker() };
    let Some(linker) = linker else {
        return 0;
    };

    // Use stable names so callbacks may retain dlpi_name pointers after return.
    let dlpi_adds = linker.objects.len() as u64;

    for idx in 0..linker.objects.len() {
        let object = &linker.objects[idx];
        let ehdr = object.map_start as *const ElfHeader;
        if ehdr.is_null() {
            continue;
        }

        let ident = unsafe { (*ehdr).e_ident };
        if ident[..4] != [0x7f, b'E', b'L', b'F'] {
            continue;
        }

        let phdr =
            object.map_start.wrapping_add(unsafe { (*ehdr).e_phoff }) as *const ProgramHeader;
        let phnum = unsafe { (*ehdr).e_phnum };
        if phdr.is_null() || phnum == 0 {
            continue;
        }

        let name_ptr = if idx == 0 {
            EMPTY_NAME
        } else {
            let ptr = linker.object_link_map_name_ptr(idx);
            if ptr.is_null() {
                EMPTY_NAME
            } else {
                ptr
            }
        };

        let tls_modid = object.tls.map(|tls| tls.module_id).unwrap_or(0);
        let mut info = DlPhdrInfo {
            dlpi_addr: object.base,
            dlpi_name: name_ptr,
            dlpi_phdr: phdr,
            dlpi_phnum: phnum,
            dlpi_adds,
            dlpi_subs: 0,
            dlpi_tls_modid: tls_modid,
            dlpi_tls_data: core::ptr::null_mut(),
        };

        let result = callback(
            core::ptr::addr_of_mut!(info).cast(),
            size_of::<DlPhdrInfo>(),
            data,
        );
        if result != 0 {
            return result;
        }
    }

    0
}

#[repr(C)]
pub(crate) struct DlInfo {
    dli_fname: *const c_char,
    dli_fbase: *mut c_void,
    dli_sname: *const c_char,
    dli_saddr: *mut c_void,
}

const RTLD_DL_SYMENT: i32 = 1;
const RTLD_DL_LINKMAP: i32 = 2;

unsafe fn resolve_dladdr(addr: usize, info: *mut DlInfo) -> Option<(usize, *const Symbol)> {
    if info.is_null() {
        return None;
    }

    let linker = linking::active_linker()?;
    let index = linker.object_for_address(addr)?;
    let object = linker.objects.get(index)?;

    let mut symbol_name_ptr = core::ptr::null::<c_char>();
    let mut symbol_addr = core::ptr::null_mut();
    let mut symbol_ptr = core::ptr::null();
    let mut best_match = 0usize;

    let symbol_table_ptr = object.symbol_table.as_ptr();
    if !symbol_table_ptr.is_null() && object.symbol_count != 0 {
        for sym_idx in 0..object.symbol_count {
            let sym_ptr = symbol_table_ptr.add(sym_idx);
            let sym = *sym_ptr;
            if sym.st_name == 0 || sym.st_shndx == 0 {
                continue;
            }

            let sym_base = if sym.st_shndx == SHN_ABS {
                0
            } else {
                object.base
            };
            let sym_start = sym_base.wrapping_add(sym.st_value);
            let matches = if sym.st_size == 0 {
                addr == sym_start
            } else {
                let sym_end = sym_start.wrapping_add(sym.st_size);
                addr >= sym_start && addr < sym_end
            };
            if !matches || sym_start < best_match {
                continue;
            }

            let sym_name = object.string_table.get_bytes(sym.st_name as usize);
            if sym_name.is_empty() {
                continue;
            }

            best_match = sym_start;
            symbol_name_ptr = sym_name.as_ptr().cast::<c_char>();
            symbol_addr = sym_start as *mut c_void;
            symbol_ptr = sym_ptr;
        }
    }

    let mut file_name = linker.object_link_map_name_ptr(index);
    if file_name.is_null() {
        file_name = b"\0".as_ptr().cast::<c_char>();
    }

    (*info).dli_fname = file_name;
    (*info).dli_fbase = object.base as *mut c_void;
    (*info).dli_sname = symbol_name_ptr;
    (*info).dli_saddr = symbol_addr;

    Some((index, symbol_ptr))
}

#[no_mangle]
pub extern "C" fn dladdr(addr: *const c_void, info: *mut DlInfo) -> i32 {
    if addr.is_null() || info.is_null() {
        return 0;
    }

    let _guard = lock_rtld_ops();
    unsafe {
        core::ptr::write_bytes(info.cast::<u8>(), 0, size_of::<DlInfo>());
        resolve_dladdr(addr as usize, info).is_some() as i32
    }
}

#[no_mangle]
pub extern "C" fn dladdr1(
    addr: *const c_void,
    info: *mut DlInfo,
    extra_info: *mut *mut c_void,
    flags: i32,
) -> i32 {
    if addr.is_null() || info.is_null() {
        return 0;
    }

    let _guard = lock_rtld_ops();
    unsafe {
        core::ptr::write_bytes(info.cast::<u8>(), 0, size_of::<DlInfo>());
        let Some((index, sym_ptr)) = resolve_dladdr(addr as usize, info) else {
            if !extra_info.is_null() {
                *extra_info = core::ptr::null_mut();
            }
            return 0;
        };

        if !extra_info.is_null() {
            *extra_info = match flags {
                RTLD_DL_LINKMAP => linking::active_linker()
                    .map(|linker| linker.object_link_map_ptr(index))
                    .unwrap_or(core::ptr::null_mut()),
                RTLD_DL_SYMENT => sym_ptr.cast_mut().cast::<c_void>(),
                _ => core::ptr::null_mut(),
            };
        }

        1
    }
}

fn clear_dlerror() {
    unsafe {
        DLERROR_PENDING = false;
    }
}

fn set_dlerror(msg: &str) {
    unsafe {
        let bytes = msg.as_bytes();
        let n = bytes.len().min(DLERROR_BUF_SIZE.saturating_sub(1));
        if n != 0 {
            core::ptr::copy_nonoverlapping(
                bytes.as_ptr(),
                core::ptr::addr_of_mut!(DLERROR_BUF).cast::<u8>(),
                n,
            );
        }
        core::ptr::write(core::ptr::addr_of_mut!(DLERROR_BUF).cast::<u8>().add(n), 0);
        DLERROR_PENDING = true;
    }
}

#[inline(never)]
fn c_string<'a>(ptr: *const u8, max_len: usize) -> Option<&'a str> {
    if ptr.is_null() || max_len == 0 {
        return None;
    }

    let mut idx = 0usize;
    while idx < max_len {
        let ch = unsafe { core::ptr::read_volatile(ptr.add(idx)) };
        if ch == 0 {
            break;
        }
        idx += 1;
    }

    // Reject empty strings and unterminated buffers.
    if idx == 0 || idx >= max_len {
        return None;
    }

    let bytes = unsafe { core::slice::from_raw_parts(ptr, idx) };
    core::str::from_utf8(bytes).ok()
}

#[inline(never)]
unsafe fn resolve_symbol_with_version(
    handle: *mut c_void,
    name: &str,
    version: &str,
) -> Option<*mut c_void> {
    let mut combined = [0u8; 768];
    let required = name.len().saturating_add(1).saturating_add(version.len());
    if required == 0 || required > combined.len() {
        return None;
    }

    let mut cursor = 0usize;
    combined[cursor..cursor + name.len()].copy_from_slice(name.as_bytes());
    cursor += name.len();
    combined[cursor] = b'@';
    cursor += 1;
    combined[cursor..cursor + version.len()].copy_from_slice(version.as_bytes());
    cursor += version.len();

    let Ok(combined_name) = core::str::from_utf8(&combined[..cursor]) else {
        return None;
    };
    resolve_symbol_for_handle(handle, combined_name)
}

const HANDLE_GLOBAL_SCOPE: usize = 1;
const HANDLE_OBJECT_BIAS: usize = 2;

enum DlHandle {
    GlobalScope,
    NextScope,
    Object(usize),
}

fn encode_global_handle() -> *mut c_void {
    HANDLE_GLOBAL_SCOPE as *mut c_void
}

fn encode_object_handle(idx: usize) -> *mut c_void {
    if let Some(linker) = unsafe { linking::active_linker() } {
        let map = linker.object_link_map_ptr(idx);
        if !map.is_null() {
            return map;
        }
    }
    // Backward-compatible fallback if no link_map is available.
    idx.wrapping_add(HANDLE_OBJECT_BIAS) as *mut c_void
}

fn decode_handle(handle: *mut c_void) -> Option<DlHandle> {
    let raw = handle as usize;
    if raw == 0 {
        // RTLD_DEFAULT (NULL): search global scope.
        return None;
    }
    if raw == usize::MAX {
        // RTLD_NEXT ((void*)-1): search from the next object in scope.
        return Some(DlHandle::NextScope);
    }
    if raw == HANDLE_GLOBAL_SCOPE {
        return Some(DlHandle::GlobalScope);
    }

    // Preferred encoding: real link_map pointer (glibc-compatible).
    if let Some(linker) = unsafe { linking::active_linker() } {
        if let Some(idx) = linker.object_index_for_link_map_ptr(handle.cast_const()) {
            return Some(DlHandle::Object(idx));
        }

        // Legacy synthetic integer handles from older builds.
        if let Some(idx) = raw.checked_sub(HANDLE_OBJECT_BIAS) {
            if idx < linker.objects.len() {
                return Some(DlHandle::Object(idx));
            }
        }
    }

    None
}

#[inline(never)]
unsafe fn resolve_symbol_for_handle(handle: *mut c_void, name: &str) -> Option<*mut c_void> {
    let resolve_global = |exclude: Option<usize>| {
        let linker = linking::active_linker()?;
        let resolve_name = |candidate: &str| {
            linker
                .lookup_symbol_excluding(candidate, exclude)
                .map(|(obj_idx, symbol)| {
                    let base = if symbol.st_shndx == SHN_ABS {
                        0
                    } else {
                        linker.get_base(obj_idx)
                    };
                    base.wrapping_add(symbol.st_value) as *mut c_void
                })
        };
        resolve_name(name).or_else(|| {
            name.split_once('@')
                .and_then(|(base_name, _)| resolve_name(base_name))
        })
    };

    match decode_handle(handle) {
        Some(DlHandle::GlobalScope) => {
            linking::lookup_active_symbol(name).map(|addr| addr as *mut c_void)
        }
        Some(DlHandle::NextScope) => {
            // We do not know the exact caller object here. Skipping the
            // main executable approximates RTLD_NEXT well enough for common
            // interposer paths used by large apps (e.g. Firefox launcher).
            resolve_global(Some(0))
                .or_else(|| linking::lookup_active_symbol(name).map(|addr| addr as *mut c_void))
        }
        Some(DlHandle::Object(idx)) => {
            let resolve_in_object = |candidate: &str| {
                let linker = linking::active_linker()?;
                linker
                    .lookup_symbol_in_object_scope(idx, candidate)
                    .map(|addr| addr as *mut c_void)
            };
            if let Some(addr) = resolve_in_object(name).or_else(|| {
                name.split_once('@')
                    .and_then(|(base_name, _)| resolve_in_object(base_name))
            }) {
                return Some(addr);
            }
            None
        }
        None => linking::lookup_active_symbol(name).map(|addr| addr as *mut c_void),
    }
}

#[inline(never)]
fn dlsym_impl(handle: *mut c_void, name_ptr: *const u8) -> *mut c_void {
    let _guard = lock_rtld_ops();
    clear_dlerror();
    let Some(name) = c_string(name_ptr, 512) else {
        set_dlerror("rustld: dlsym invalid symbol name");
        return core::ptr::null_mut();
    };

    unsafe {
        if let Some(addr) = resolve_symbol_for_handle(handle, &name) {
            log_suspicious_runtime_symbol("dlsym", &name, addr as usize);
            return addr;
        }
    }

    set_dlerror("rustld: dlsym symbol not found");
    core::ptr::null_mut()
}

fn log_suspicious_runtime_symbol(api: &str, name: &str, resolved: usize) {
    #[cfg(not(debug_assertions))]
    {
        let _ = (api, name, resolved);
        return;
    }

    #[cfg(debug_assertions)]
    unsafe {
        use crate::libc::fs::write;

        if let Some(linker) = linking::active_linker() {
            for (idx, object) in linker.objects.iter().enumerate() {
                let base = object.base;
                if resolved >= base && resolved.wrapping_sub(base) < 0x100 {
                    write::write_str(write::STD_ERR, "rustld: suspicious ");
                    write::write_str(write::STD_ERR, api);
                    write::write_str(write::STD_ERR, " ");
                    write::write_str(write::STD_ERR, name);
                    write::write_str(write::STD_ERR, " -> ");
                    write_hex(resolved);
                    write::write_str(write::STD_ERR, " object=");

                    let mut idx_buf = [0u8; 32];
                    let mut value = idx;
                    let mut len = 0usize;
                    if value == 0 {
                        idx_buf[0] = b'0';
                        len = 1;
                    } else {
                        while value > 0 {
                            idx_buf[len] = b'0' + (value % 10) as u8;
                            value /= 10;
                            len += 1;
                        }
                    }
                    for i in 0..len / 2 {
                        idx_buf.swap(i, len - 1 - i);
                    }
                    let idx_text = core::str::from_utf8_unchecked(&idx_buf[..len]);
                    write::write_str(write::STD_ERR, idx_text);

                    write::write_str(write::STD_ERR, " base=");
                    write_hex(base);
                    write::write_str(write::STD_ERR, "\n");
                    break;
                }
            }
        }
    }
}

#[inline(never)]
fn dlopen_impl(file_ptr: *const u8, _mode: i32) -> *mut c_void {
    let _guard = lock_rtld_ops();
    clear_dlerror();
    seed_current_glibc_thread_locale();

    if file_ptr.is_null() {
        // glibc returns a handle for the main program on dlopen(NULL, ...).
        if let Some(linker) = unsafe { linking::active_linker() } {
            let main_map = linker.object_link_map_ptr(0);
            if !main_map.is_null() {
                return main_map;
            }
        }
        return encode_global_handle();
    }

    let Some(file) = c_string(file_ptr, 4096) else {
        set_dlerror("rustld: dlopen invalid file name");
        return core::ptr::null_mut();
    };

    let linker = unsafe { linking::active_linker_mut() };
    let Some(linker) = linker else {
        set_dlerror("rustld: dlopen no active linker");
        return core::ptr::null_mut();
    };

    #[cfg(debug_assertions)]
    {
        use crate::libc::fs::write;
        unsafe {
            write::write_str(write::STD_ERR, "ld_stub: dlopen request ");
            write::write_str(write::STD_ERR, &file);
            write::write_str(write::STD_ERR, "\n");
        }
        if let Some(idx) = linker.loaded_index(&file) {
            unsafe {
                write::write_str(write::STD_ERR, "ld_stub: dlopen already loaded idx=");
            }
            let mut buf = [0u8; 32];
            let mut n = idx;
            let mut len = 0usize;
            if n == 0 {
                buf[0] = b'0';
                len = 1;
            } else {
                while n > 0 {
                    buf[len] = b'0' + (n % 10) as u8;
                    n /= 10;
                    len += 1;
                }
                buf[..len].reverse();
            }
            unsafe {
                write::write_str(
                    write::STD_ERR,
                    core::str::from_utf8(&buf[..len]).unwrap_or("?"),
                );
                write::write_str(write::STD_ERR, "\n");
            }
        } else {
            unsafe {
                write::write_str(write::STD_ERR, "ld_stub: dlopen not in map yet\n");
            }
        }
    }

    let result = unsafe { linker.dlopen_runtime(&file, _mode) };
    match result {
        Ok(idx) => encode_object_handle(idx),
        Err(msg) => {
            set_dlerror(msg);
            core::ptr::null_mut()
        }
    }
}

type RtldDlopenDispatch = extern "C" fn(*const u8, i32) -> *mut c_void;
type RtldLookupDispatch = extern "C" fn(*const u8, usize, usize, *mut *const c_void) -> *mut c_void;

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn __rustld_rtld_dlopen_dispatch_impl(file: *const u8, mode: i32) -> *mut c_void {
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlopen_entry)) };
    unsafe { entry(file, mode) }
}

#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn __rustld_rtld_lookup_dispatch_impl(
    undef_name: *const u8,
    undef_map_raw: usize,
    skip_map_raw: usize,
    reference: *mut *const c_void,
) -> *mut c_void {
    let _guard = lock_rtld_ops();
    if let Some(name) = c_string(undef_name, 512) {
        unsafe {
            if let Some(linker) = linking::active_linker() {
                let requester_idx =
                    linker.object_index_for_link_map_ptr(undef_map_raw as *const c_void);
                let skip_idx = linker.object_index_for_link_map_ptr(skip_map_raw as *const c_void);
                if trace_rtld_lookup() {
                    eprintln!(
                        "rtld_lookup: name={} undef_map={:#x} requester={:?} skip_map={:#x} skip={:?}",
                        name,
                        undef_map_raw,
                        requester_idx,
                        skip_map_raw,
                        skip_idx
                    );
                }
                let resolve = |candidate: &str| {
                    if let Some(requester) = requester_idx {
                        linker.lookup_symbol_in_object_scope_excluding_rtld_slow(
                            requester,
                            candidate,
                            skip_idx,
                        )
                    } else {
                        linker.lookup_symbol_excluding(candidate, skip_idx)
                    }
                };

                let resolved = if let Some(found) = resolve(name) {
                    Some(found)
                } else if let Some((base_name, _)) = name.split_once('@') {
                    resolve(base_name)
                } else {
                    None
                };

                if let Some((obj_idx, symbol)) = resolved {
                    let base = if symbol.st_shndx == SHN_ABS {
                        0
                    } else {
                        linker.get_base(obj_idx)
                    };
                    RTLD_LOOKUP_MAP.l_addr = base;
                    RTLD_LOOKUP_MAP.l_name = linker.object_link_map_name_ptr(obj_idx);
                    RTLD_LOOKUP_MAP.l_ld = linker.objects[obj_idx].dynamic.cast::<u8>();
                    let map_ptr = linker.object_link_map_ptr(obj_idx);
                    let sym_ptr = persist_rtld_lookup_symbol(symbol);

                    if !reference.is_null() {
                        *reference = sym_ptr.cast();
                    }
                    return map_ptr;
                }
            }
        }
    }

    if !reference.is_null() {
        unsafe { *reference = core::ptr::null() };
    }
    core::ptr::null_mut()
}

#[unsafe(no_mangle)]
pub static mut __rustld_rtld_dlopen_dispatch: RtldDlopenDispatch =
    __rustld_rtld_dlopen_dispatch_impl;

#[unsafe(no_mangle)]
pub static mut __rustld_rtld_lookup_dispatch: RtldLookupDispatch =
    __rustld_rtld_lookup_dispatch_impl;

#[cfg(debug_assertions)]
fn log_dl_symbol(prefix: &str, symbol: *const u8, resolved: usize) {
    use core::str;
    unsafe {
        write::write_str(write::STD_ERR, "ld_stub: ");
        write::write_str(write::STD_ERR, prefix);
        write::write_str(write::STD_ERR, " ");
    }
    if symbol.is_null() {
        unsafe { write::write_str(write::STD_ERR, "<null>\n") };
        return;
    }

    let mut len = 0usize;
    while len < 128 {
        let byte = unsafe { *symbol.add(len) };
        if byte == 0 {
            break;
        }
        len += 1;
    }
    if len == 0 {
        unsafe { write::write_str(write::STD_ERR, "<empty>\n") };
        return;
    }

    let bytes = unsafe { core::slice::from_raw_parts(symbol, len) };
    if let Ok(text) = str::from_utf8(bytes) {
        unsafe {
            write::write_str(write::STD_ERR, text);
            write::write_str(write::STD_ERR, " -> ");
            write_hex(resolved);
            write::write_str(write::STD_ERR, "\n");
        }
    } else {
        unsafe { write::write_str(write::STD_ERR, "<non-utf8>\n") };
    }
}

#[cfg(debug_assertions)]
unsafe fn write_hex(mut value: usize) {
    let mut buf = [0u8; 18];
    buf[0] = b'0';
    buf[1] = b'x';
    let hex = b"0123456789abcdef";
    for i in (0..16).rev() {
        buf[2 + i] = hex[value & 0xF];
        value >>= 4;
    }
    write::write_str(write::STD_ERR, core::str::from_utf8_unchecked(&buf));
}

#[no_mangle]
pub extern "C" fn freecon(_con: *mut u8) {
    // Optional SELinux path in coreutils; treat as unavailable.
}

#[no_mangle]
pub extern "C" fn is_selinux_enabled() -> i32 {
    // Report SELinux as unavailable for portability in constrained loaders.
    0
}

#[no_mangle]
pub extern "C" fn getcon(con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn getfilecon(_path: *const u8, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn lgetfilecon(_path: *const u8, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn getfilecon_raw(_path: *const u8, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn lgetfilecon_raw(_path: *const u8, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn fgetfilecon(_fd: i32, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn fgetfilecon_raw(_fd: i32, con: *mut *mut u8) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn setfilecon_raw(_path: *const u8, _con: *const u8) -> i32 {
    -1
}

#[no_mangle]
pub extern "C" fn lsetfilecon_raw(_path: *const u8, _con: *const u8) -> i32 {
    -1
}

#[no_mangle]
pub extern "C" fn fsetfilecon_raw(_fd: i32, _con: *const u8) -> i32 {
    -1
}

#[no_mangle]
pub extern "C" fn selabel_lookup(
    _handle: *mut c_void,
    con: *mut *mut u8,
    _key: *const u8,
    _ty: i32,
) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn selabel_lookup_raw(
    _handle: *mut c_void,
    con: *mut *mut u8,
    _key: *const u8,
    _ty: i32,
) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn selabel_lookup_best_match(
    _handle: *mut c_void,
    con: *mut *mut u8,
    _key: *const u8,
    _aliases: *const *const u8,
    _ty: i32,
) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn selabel_lookup_best_match_raw(
    _handle: *mut c_void,
    con: *mut *mut u8,
    _key: *const u8,
    _aliases: *const *const u8,
    _ty: i32,
) -> i32 {
    if !con.is_null() {
        unsafe { *con = core::ptr::null_mut() };
    }
    -1
}

#[no_mangle]
pub extern "C" fn _dl_find_dso_for_object(addr: *const ()) -> *const () {
    debug_stub_trace("_dl_find_dso_for_object");
    if addr.is_null() {
        return core::ptr::null();
    }

    let _guard = lock_rtld_ops();
    let linker = unsafe { linking::active_linker() };
    let Some(linker) = linker else {
        return core::ptr::null();
    };

    if let Some(index) = linker.object_for_address(addr as usize) {
        let link_map_ptr = linker.object_link_map_ptr(index);
        return link_map_ptr.cast();
    }
    core::ptr::null()
}

#[no_mangle]
pub extern "C" fn _dl_find_object(_addr: *const c_void, result: *mut DlFindObject) -> i32 {
    if _addr.is_null() || result.is_null() {
        return -1;
    }

    unsafe {
        core::ptr::write_bytes(result.cast::<u8>(), 0, size_of::<DlFindObject>());
    }

    let _guard = lock_rtld_ops();
    let linker = unsafe { linking::active_linker() };
    let Some(linker) = linker else {
        return -1;
    };

    let Some(index) = linker.object_for_address(_addr as usize) else {
        return -1;
    };

    let Some((map_start, map_end)) = linker.object_mapping_range_for_address(index, _addr as usize)
    else {
        return -1;
    };

    unsafe {
        (*result).dlfo_flags = 0;
        (*result).dlfo_map_start = map_start as *mut c_void;
        (*result).dlfo_map_end = map_end as *mut c_void;
        (*result).dlfo_link_map = linker.object_link_map_ptr(index);
        (*result).dlfo_eh_frame = linker
            .object_eh_frame_hdr(index)
            .unwrap_or(core::ptr::null()) as *mut c_void;
        (*result).dlfo_sframe = core::ptr::null_mut();
    }
    0
}

#[unsafe(no_mangle)]
pub extern "C" fn __rustld_debug_addr_object(
    addr: usize,
    out_index: *mut usize,
    out_map_start: *mut usize,
    out_map_end: *mut usize,
) -> *const c_char {
    let _guard = lock_rtld_ops();
    let Some(linker) = (unsafe { linking::active_linker() }) else {
        return core::ptr::null();
    };
    let Some(index) = linker.object_for_address(addr) else {
        return core::ptr::null();
    };

    if !out_index.is_null() {
        unsafe { *out_index = index };
    }
    if !out_map_start.is_null() || !out_map_end.is_null() {
        if let Some((start, end)) = linker.object_mapping_range_for_address(index, addr) {
            if !out_map_start.is_null() {
                unsafe { *out_map_start = start };
            }
            if !out_map_end.is_null() {
                unsafe { *out_map_end = end };
            }
        }
    }
    linker.object_link_map_name_ptr(index)
}

#[unsafe(no_mangle)]
pub extern "C" fn __rustld_debug_addr_object_index(addr: usize) -> isize {
    let _guard = lock_rtld_ops();
    let Some(linker) = (unsafe { linking::active_linker() }) else {
        return -1;
    };
    linker
        .object_for_address(addr)
        .map(|idx| idx as isize)
        .unwrap_or(-1)
}

#[unsafe(no_mangle)]
pub extern "C" fn __rustld_debug_addr_object_map_start(addr: usize) -> usize {
    let _guard = lock_rtld_ops();
    let Some(linker) = (unsafe { linking::active_linker() }) else {
        return 0;
    };
    let Some(index) = linker.object_for_address(addr) else {
        return 0;
    };
    linker
        .object_mapping_range_for_address(index, addr)
        .map(|(start, _)| start)
        .unwrap_or(0)
}

#[unsafe(no_mangle)]
pub extern "C" fn __rustld_debug_addr_object_map_end(addr: usize) -> usize {
    let _guard = lock_rtld_ops();
    let Some(linker) = (unsafe { linking::active_linker() }) else {
        return 0;
    };
    let Some(index) = linker.object_for_address(addr) else {
        return 0;
    };
    linker
        .object_mapping_range_for_address(index, addr)
        .map(|(_, end)| end)
        .unwrap_or(0)
}

#[no_mangle]
pub extern "C" fn _dl_allocate_tls(_mem: *mut ()) -> *mut () {
    debug_stub_trace("_dl_allocate_tls");
    // Allocate TLS/TCB for a new thread.
    // This is required for glibc pthread startup paths that expect a non-null DTV.
    unsafe {
        __rustld_last_alloc_tls_enter = _mem;
    }
    unsafe {
        if trace_thread_tls() {
            eprintln!("ld_stub: _dl_allocate_tls mem={:#x}", _mem as usize);
        }
        // When glibc passes a preallocated thread descriptor buffer, initialize
        // TLS in-place. pthread startup paths continue to use this pointer as TP.
        if !_mem.is_null() && !force_fresh_thread_tls() {
            if let Some(initialized) = tls::initialize_tls_for_thread_ptr(_mem) {
                let result = initialized.cast();
                if trace_thread_tls() {
                    eprintln!(
                        "ld_stub: _dl_allocate_tls in-place ret={:#x}",
                        result as usize
                    );
                }
                __rustld_last_alloc_tls_ret = result;
                return result;
            }
        }

        // Fallback for callers that do not provide a thread descriptor buffer.
        if let Some(tcb) = tls::allocate_tls_for_new_thread() {
            let result = tcb.cast();
            if trace_thread_tls() {
                eprintln!("ld_stub: _dl_allocate_tls fresh ret={:#x}", result as usize);
            }
            __rustld_last_alloc_tls_ret = result;
            return result;
        }
        __rustld_last_alloc_tls_ret = core::ptr::null_mut();
    }
    core::ptr::null_mut()
}

#[no_mangle]
pub extern "C" fn _dl_allocate_tls_init(tcb: *mut (), _main_thread: usize) -> *mut () {
    debug_stub_trace("_dl_allocate_tls_init");
    // Always (re)initialize the thread descriptor TLS view.
    unsafe {
        __rustld_last_alloc_tls_init_arg = tcb;
        let current_tp = get_thread_pointer();
        if trace_thread_tls() {
            eprintln!(
                "ld_stub: _dl_allocate_tls_init arg={:#x} current_tp={:#x}",
                tcb as usize,
                current_tp as usize
            );
        }
        // glibc may call this for the current thread descriptor during startup/
        // teardown bookkeeping. For current-thread calls, only reinitialize
        // when the descriptor's DTV/static TLS view is clearly stale.
        if !tcb.is_null() {
            let needs_reinit = if tcb == current_tp {
                tls::thread_ptr_needs_tls_init(tcb)
            } else {
                true
            };
            if needs_reinit {
                if let Some(initialized) = tls::initialize_tls_for_thread_ptr(tcb) {
                    let current_tp_tcb = current_tp as *mut ThreadControlBlock;
                    if !current_tp_tcb.is_null() && current_tp_tcb == initialized {
                        tls::stamp_thread_tid(initialized);
                    }
                    let result = initialized.cast();
                    if trace_thread_tls() {
                        eprintln!(
                            "ld_stub: _dl_allocate_tls_init ret={:#x}",
                            result as usize
                        );
                    }
                    __rustld_last_alloc_tls_init_ret = result;
                    return result;
                }
            }
        }
        __rustld_last_alloc_tls_init_ret = tcb;
    }
    tcb
}

#[no_mangle]
pub extern "C" fn _dl_deallocate_tls(_tcb: *mut (), _dealloc_tcb: usize) {
    debug_stub_trace("_dl_deallocate_tls");
    tls::unregister_thread_tcb(_tcb as *mut ThreadControlBlock);
}

#[no_mangle]
pub extern "C" fn _dl_debug_state() {}

pub unsafe fn set_r_debug_map(map: *mut c_void) {
    _r_debug.r_map = map;
}

pub unsafe fn set_r_debug_ldbase(ldbase: usize) {
    _r_debug.r_ldbase = ldbase;
}

pub unsafe fn r_debug_ptr() -> *mut c_void {
    core::ptr::addr_of_mut!(_r_debug).cast()
}

#[no_mangle]
pub extern "C" fn _dl_signal_error(
    _errcode: i32,
    _objname: *const c_char,
    _errstring: *const c_char,
) {
    debug_stub_trace("_dl_signal_error");
    let tid = current_tid();
    let frame_ptr = unsafe { catch_error_get_top(tid) };
    if frame_ptr.is_null() {
        return;
    }
    unsafe {
        let frame = &mut *frame_ptr;
        frame.errcode = _errcode;
        if !frame.objname.is_null() {
            core::ptr::write_unaligned(frame.objname, _objname);
        }
        if !frame.errstring.is_null() {
            core::ptr::write_unaligned(frame.errstring, _errstring);
        }
        if !frame.mallocedp.is_null() {
            core::ptr::write_unaligned(frame.mallocedp, 0u8);
        }
        // Avoid keeping rtld lock held across non-local jump.
        force_unlock_rtld_ops_if_owned_by_current_thread();
        siglongjmp(core::ptr::addr_of_mut!(frame.env), 1);
    }
}

#[no_mangle]
pub extern "C" fn _dl_signal_exception(_errcode: i32, _exception: *const ()) {
    debug_stub_trace("_dl_signal_exception");
    static MSG: &[u8] = b"rustld: rtld exception\0";
    _dl_signal_error(_errcode, core::ptr::null(), MSG.as_ptr().cast::<c_char>());
}

#[inline(always)]
unsafe fn populate_dl_exception(
    exception: *mut c_void,
    objname: *const c_char,
    errstring: *const c_char,
) {
    if exception.is_null() {
        return;
    }
    let exception = exception.cast::<DlException>();
    (*exception).objname = objname;
    (*exception).errstring = errstring;
    (*exception).message_buffer = core::ptr::null_mut();
}

#[no_mangle]
pub unsafe extern "C" fn _dl_exception_create(
    exception: *mut c_void,
    objname: *const c_char,
    errstring: *const c_char,
) {
    debug_stub_trace("_dl_exception_create");
    populate_dl_exception(exception, objname, errstring);
}

#[no_mangle]
pub unsafe extern "C" fn _dl_exception_create_format(
    exception: *mut c_void,
    objname: *const c_char,
    errfmt: *const c_char,
    mut _args: ...,
) {
    debug_stub_trace("_dl_exception_create_format");
    populate_dl_exception(exception, objname, errfmt);
}

#[no_mangle]
pub unsafe extern "C" fn _dl_exception_free(exception: *mut c_void) {
    debug_stub_trace("_dl_exception_free");
    if exception.is_null() {
        return;
    }
    let exception = exception.cast::<DlException>();
    (*exception).message_buffer = core::ptr::null_mut();
}

#[no_mangle]
pub extern "C" fn _dl_catch_exception(
    _exception: *mut (),
    operate: *const (),
    args: *const (),
) -> i32 {
    debug_stub_trace("_dl_catch_exception");
    let exception = _exception as *mut DlException;
    if !exception.is_null() {
        unsafe {
            core::ptr::write_bytes(exception as *mut u8, 0, core::mem::size_of::<DlException>());
        }
    }

    let mut frame = CatchErrorFrame {
        prev: core::ptr::null_mut(),
        env: SigJmpBuf {
            storage: [0; SIGJMP_WORDS],
        },
        objname: if exception.is_null() {
            core::ptr::null_mut()
        } else {
            unsafe { core::ptr::addr_of_mut!((*exception).objname) }
        },
        errstring: if exception.is_null() {
            core::ptr::null_mut()
        } else {
            unsafe { core::ptr::addr_of_mut!((*exception).errstring) }
        },
        mallocedp: core::ptr::null_mut(),
        errcode: 0,
    };
    let tid = current_tid();
    unsafe {
        frame.prev = catch_error_get_top(tid);
    }
    let frame_ptr: *mut CatchErrorFrame = core::ptr::addr_of_mut!(frame);
    unsafe {
        catch_error_set_top(tid, frame_ptr);
    }

    let jumped = unsafe { sigsetjmp(core::ptr::addr_of_mut!((*frame_ptr).env), 0) != 0 };
    if !jumped && !operate.is_null() {
        unsafe {
            let func: extern "C" fn(*mut c_void) = core::mem::transmute(operate);
            func(args as *mut c_void);
        }
    }
    unsafe {
        catch_error_restore_top(tid, frame_ptr, (*frame_ptr).prev);
    }
    if jumped {
        unsafe { (*frame_ptr).errcode }
    } else {
        0
    }
}

#[no_mangle]
pub extern "C" fn _dl_catch_error(
    objname: *mut *const c_char,
    errstring: *mut *const c_char,
    mallocedp: *mut u8,
    operate: *const (),
    args: *const (),
) -> i32 {
    debug_stub_trace("_dl_catch_error");
    __rustld_rtld_catch_error(
        objname,
        errstring,
        mallocedp,
        operate.cast(),
        args as *mut c_void,
    )
}

/// rtld_global_ro + 0x340: internal _dl_lookup_symbol_x entry point.
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn __rustld_rtld_lookup_symbol_x_stub(
    undef_name_raw: usize,
    undef_map_raw: usize,
    reference_raw: usize,
    _symbol_scope: usize,
    _version: usize,
    _type_class: i32,
    _flags: i32,
    skip_map_raw: usize,
) -> *mut c_void {
    debug_stub_trace("__rustld_rtld_lookup_symbol_x_stub");
    clear_dlerror();
    let undef_name = undef_name_raw as *const u8;
    let reference = reference_raw as *mut *const c_void;
    let dispatch =
        unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_rtld_lookup_dispatch)) };
    dispatch(undef_name, undef_map_raw, skip_map_raw, reference)
}

/// rtld_global_ro + 0x348: internal dlopen entry point used by libc wrappers.
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn __rustld_rtld_dlopen_stub(
    file_raw: usize,
    mode: i32,
    _caller: usize,
    _nsid: isize,
    _argc: i32,
    _argv: usize,
    _envp: usize,
) -> *mut c_void {
    debug_stub_trace("__rustld_rtld_dlopen_stub");
    clear_dlerror();
    let file = file_raw as *const u8;
    let dispatch =
        unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_rtld_dlopen_dispatch)) };
    dispatch(file, mode)
}

/// rtld_global_ro + 0x350: internal dlclose entry point used by libc wrappers.
pub extern "C" fn __rustld_rtld_dlclose_stub(_map: *mut c_void) -> i32 {
    debug_stub_trace("__rustld_rtld_dlclose_stub");
    let entry = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(__rustld_dlclose_entry)) };
    unsafe { entry(_map) }
}

/// rtld_global_ro + 0x358: internal catch-error helper used by dlerror_run().
pub extern "C" fn __rustld_rtld_catch_error(
    objname: *mut *const c_char,
    errstring: *mut *const c_char,
    mallocedp: *mut u8,
    operate: *const c_void,
    args: *mut c_void,
) -> i32 {
    debug_stub_trace("__rustld_rtld_catch_error");
    if !objname.is_null() {
        unsafe { core::ptr::write_unaligned(objname, core::ptr::null()) };
    }
    if !errstring.is_null() {
        unsafe { core::ptr::write_unaligned(errstring, core::ptr::null()) };
    }
    if !mallocedp.is_null() {
        unsafe { core::ptr::write_unaligned(mallocedp, 0u8) };
    }

    let mut frame = CatchErrorFrame {
        prev: core::ptr::null_mut(),
        env: SigJmpBuf {
            storage: [0; SIGJMP_WORDS],
        },
        objname,
        errstring,
        mallocedp,
        errcode: 0,
    };
    let tid = current_tid();
    frame.prev = unsafe { catch_error_get_top(tid) };
    let frame_ptr: *mut CatchErrorFrame = core::ptr::addr_of_mut!(frame);
    unsafe {
        catch_error_set_top(tid, frame_ptr);
    }

    let jumped = unsafe { sigsetjmp(core::ptr::addr_of_mut!((*frame_ptr).env), 0) != 0 };

    if !jumped && !operate.is_null() {
        unsafe {
            let op: extern "C" fn(*mut c_void) = core::mem::transmute(operate);
            op(args);
        }
    }

    unsafe {
        catch_error_restore_top(tid, frame_ptr, (*frame_ptr).prev);
    }

    if jumped {
        unsafe { (*frame_ptr).errcode }
    } else {
        0
    }
}

/// rtld_global_ro + 0x360: internal error-string free helper used by dlerror_run().
pub extern "C" fn __rustld_rtld_error_free(_errstring: *mut c_void) {}

/// Legacy x86_64 glibc installs tiny lock helpers in `_rtld_global` and
/// calls them from internal libc paths like `_dl_addr@@GLIBC_PRIVATE`.
/// Ubuntu 20.04/glibc 2.31 uses `_rtld_global + 0xf08` and `+0xf10` for
/// this pair. The real loader only bumps the recursion counter at `lock+4`.
#[cfg(target_arch = "x86_64")]
#[inline(never)]
pub extern "C" fn __rustld_rtld_legacy_lock_acquire(lock: *mut u8) {
    if lock.is_null() {
        return;
    }
    let depth = unsafe { lock.add(4) as *mut i32 };
    unsafe {
        *depth = (*depth).wrapping_add(1);
    }
}

#[cfg(target_arch = "x86_64")]
#[inline(never)]
pub extern "C" fn __rustld_rtld_legacy_lock_release(lock: *mut u8) {
    if lock.is_null() {
        return;
    }
    let depth = unsafe { lock.add(4) as *mut i32 };
    unsafe {
        *depth = (*depth).wrapping_sub(1);
    }
}

#[no_mangle]
pub extern "C" fn _dl_audit_symbind_alt(
    _sym: *const (),
    _ndx: usize,
    _refcook: *const (),
    _defcook: *const (),
    _flags: *const (),
) -> usize {
    // Audit interface for symbol binding
    // Return 0 for now
    0
}

#[no_mangle]
pub extern "C" fn _dl_rtld_di_serinfo() -> *const () {
    // Returns information about loaded objects
    // Return null for now
    core::ptr::null()
}

#[no_mangle]
pub extern "C" fn __tunable_is_initialized(_id: usize) -> i32 {
    debug_stub_trace("__tunable_is_initialized");
    #[cfg(target_arch = "x86_64")]
    {
        let self_addr = __tunable_is_initialized as *const () as usize;
        if let Some(addr) = resolve_tunable_forward_addr(
            &TUNABLE_IS_INIT_FORWARD_ADDR,
            "__tunable_is_initialized",
            self_addr,
        ) {
            unsafe {
                let func: extern "C" fn(usize) -> i32 = core::mem::transmute(addr);
                return func(_id);
            }
        }
    }

    // Keep tunables handling entirely in rustld. Forwarding to glibc's
    // internal implementation depends on glibc-private rtld state and can
    // break across distro/glibc revisions.
    0
}

#[no_mangle]
pub extern "C" fn __tunable_get_val(_id: usize, valp: *mut (), callback: *const ()) {
    debug_stub_trace("__tunable_get_val");

    #[cfg(target_arch = "x86_64")]
    {
        let self_addr = __tunable_get_val as *const () as usize;
        if let Some(addr) = resolve_tunable_forward_addr(
            &TUNABLE_GET_VAL_FORWARD_ADDR,
            "__tunable_get_val",
            self_addr,
        ) {
            unsafe {
                let func: extern "C" fn(usize, *mut (), *const ()) = core::mem::transmute(addr);
                func(_id, valp, callback);
                return;
            }
        }

        if !callback.is_null() {
            #[repr(C)]
            union TunableVal {
                numval: u64,
                raw: [usize; 2],
            }

            unsafe {
                // glibc callback expects pointer to tunable value payload.
                // Zero-initialize the whole union so callbacks that read the
                // string variant (ptr + len) never observe uninitialized bytes.
                let mut payload = TunableVal { raw: [0; 2] };
                let cb: extern "C" fn(*mut ()) = core::mem::transmute(callback);
                cb((&mut payload as *mut TunableVal).cast::<()>());
            }
        } else if !valp.is_null() {
            unsafe {
                core::ptr::write_unaligned(valp.cast::<i32>(), 0);
            }
        }
        return;
    }

    #[cfg(not(target_arch = "x86_64"))]
    {
        // Keep tunables handling entirely in rustld on non-x86_64.
        // Preserve caller defaults when no callback is provided.
        let _ = valp;
        let _ = callback;
    }
}

#[no_mangle]
pub extern "C" fn __nptl_change_stack_perm(_thread: *mut ()) -> i32 {
    // Ubuntu/Debian glibc may bind this GLIBC_PRIVATE symbol from libc to ld.so.
    // rustld does not expose full NPTL internals, so keep a conservative success stub.
    0
}

#[no_mangle]
pub extern "C" fn _dl_make_stack_executable(_stack_endp: *mut *mut c_void) -> i32 {
    debug_stub_trace("_dl_make_stack_executable");
    0
}

#[no_mangle]
pub extern "C" fn _dl_get_tls_static_info(sizep: *mut usize, alignp: *mut usize) {
    debug_stub_trace("_dl_get_tls_static_info");
    let (size, align) = unsafe {
        linking::active_linker()
            .and_then(|linker| linker.tls_static_metadata())
            .unwrap_or((0x1000, 0x10))
    };
    if !sizep.is_null() {
        unsafe { core::ptr::write_unaligned(sizep, size) };
    }
    if !alignp.is_null() {
        unsafe { core::ptr::write_unaligned(alignp, align.max(1)) };
    }
}

#[no_mangle]
pub extern "C" fn __tls_get_addr(_ti: *const ()) -> *mut () {
    debug_stub_trace("__tls_get_addr");
    if _ti.is_null() {
        return core::ptr::null_mut();
    }

    let ti = _ti as *const TlsIndex;
    let module = unsafe { (*ti).ti_module };
    let offset = unsafe { (*ti).ti_offset };
    let resolved = unsafe { tls::resolve_tls_address(module, offset).unwrap_or(0) };

    resolved as *mut ()
}