aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/Dwarf.cpp
blob: 551ed93773fc6b260eeecc0167b701a54806f605 (plain) (blame)
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
#if defined(__ELF__) && !defined(OS_FREEBSD)

/*
 * Copyright 2012-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** This file was edited for ClickHouse.
  */

#include <cstring>

#include <Common/Elf.h>
#include <Common/Dwarf.h>
#include <Common/Exception.h>

#define DW_CHILDREN_no 0

#define DW_FORM_addr 1
#define DW_FORM_block1 0x0a
#define DW_FORM_block2 3
#define DW_FORM_block4 4
#define DW_FORM_block 9
#define DW_FORM_exprloc 0x18
#define DW_FORM_data1 0x0b
#define DW_FORM_ref1 0x11
#define DW_FORM_data2 0x05
#define DW_FORM_ref2 0x12
#define DW_FORM_data4 0x06
#define DW_FORM_ref4 0x13
#define DW_FORM_data8 0x07
#define DW_FORM_ref8 0x14
#define DW_FORM_ref_sig8 0x20
#define DW_FORM_sdata 0x0d
#define DW_FORM_udata 0x0f
#define DW_FORM_ref_udata 0x15
#define DW_FORM_flag 0x0c
#define DW_FORM_flag_present 0x19
#define DW_FORM_sec_offset 0x17
#define DW_FORM_ref_addr 0x10
#define DW_FORM_string 0x08
#define DW_FORM_strp 0x0e
#define DW_FORM_indirect 0x16
#define DW_FORM_strx 0x1a
#define DW_FORM_addrx 0x1b
#define DW_FORM_ref_sup4 0x1c
#define DW_FORM_strp_sup 0x1d
#define DW_FORM_data16 0x1e
#define DW_FORM_line_strp 0x1f
#define DW_FORM_implicit_const 0x21
#define DW_FORM_rnglistx 0x23
#define DW_FORM_loclistx 0x22
#define DW_FORM_ref_sup8 0x24
#define DW_FORM_strx1 0x25
#define DW_FORM_strx2 0x26
#define DW_FORM_strx3 0x27
#define DW_FORM_strx4 0x28
#define DW_FORM_addrx1 0x29
#define DW_FORM_addrx2 0x2a
#define DW_FORM_addrx3 0x2b
#define DW_FORM_addrx4 0x2c

#define DW_TAG_compile_unit 0x11
#define DW_TAG_subprogram 0x2e
#define DW_TAG_try_block 0x32
#define DW_TAG_catch_block 0x25
#define DW_TAG_entry_point 0x03
#define DW_TAG_common_block 0x1a
#define DW_TAG_lexical_block 0x0b

#define DW_AT_stmt_list 0x10
#define DW_AT_comp_dir 0x1b
#define DW_AT_name 0x03
#define DW_AT_high_pc 0x12
#define DW_AT_low_pc 0x11
#define DW_AT_entry_pc 0x52
#define DW_AT_ranges 0x55
#define DW_AT_abstract_origin 0x31
#define DW_AT_call_line 0x59
#define DW_AT_call_file 0x58
#define DW_AT_linkage_name 0x6e
#define DW_AT_specification 0x47
#define DW_AT_str_offsets_base 0x72
#define DW_AT_addr_base 0x73
#define DW_AT_rnglists_base 0x74
#define DW_AT_loclists_base 0x8c
#define DW_AT_GNU_ranges_base 0x2132
#define DW_AT_GNU_addr_base 0x2133

#define DW_LNE_define_file 0x03
#define DW_LNS_copy 0x01
#define DW_LNS_advance_pc 0x02
#define DW_LNS_advance_line 0x03
#define DW_LNS_set_file 0x04
#define DW_LNS_set_column 0x05
#define DW_LNS_negate_stmt 0x06
#define DW_LNS_set_basic_block 0x07
#define DW_LNS_const_add_pc 0x08
#define DW_LNS_fixed_advance_pc 0x09
#define DW_LNS_set_prologue_end 0x0a
#define DW_LNS_set_epilogue_begin 0x0b
#define DW_LNS_set_isa 0x0c
#define DW_LNE_end_sequence 0x01
#define DW_LNE_set_address 0x02
#define DW_LNE_set_discriminator 0x04

#define DW_LNCT_path 0x1
#define DW_LNCT_directory_index 0x2
#define DW_LNCT_timestamp 0x3
#define DW_LNCT_size 0x4
#define DW_LNCT_MD5 0x5

#define DW_RLE_end_of_list 0x0
#define DW_RLE_base_addressx 0x1
#define DW_RLE_startx_endx 0x2
#define DW_RLE_startx_length 0x3
#define DW_RLE_offset_pair 0x4
#define DW_RLE_base_address 0x5
#define DW_RLE_start_end 0x6
#define DW_RLE_start_length 0x7


namespace DB
{

namespace ErrorCodes
{
    extern const int CANNOT_PARSE_DWARF;
}


Dwarf::Dwarf(const std::shared_ptr<Elf> & elf)
    : elf_(elf)
    , abbrev_(getSection(".debug_abbrev"))
    , addr_(getSection(".debug_addr"))
    , aranges_(getSection(".debug_aranges"))
    , info_(getSection(".debug_info"))
    , line_(getSection(".debug_line"))
    , line_str_(getSection(".debug_line_str"))
    , loclists_(getSection(".debug_loclists"))
    , ranges_(getSection(".debug_ranges"))
    , rnglists_(getSection(".debug_rnglists"))
    , str_(getSection(".debug_str"))
    , str_offsets_(getSection(".debug_str_offsets"))
{
    // Optional sections:
    //  - debugAranges_: for fast address range lookup.
    //     If missing .debug_info can be used - but it's much slower (linear
    //     scan).
    //  - debugRanges_ (DWARF 4) / debugRnglists_ (DWARF 5): non-contiguous
    //    address ranges of debugging information entries.
    //    Used for inline function address lookup.
    if (info_.empty() || abbrev_.empty() || line_.empty() || str_.empty())
    {
        elf_ = nullptr;
    }
}

Dwarf::Section::Section(std::string_view d) : is64_bit(false), data(d)
{
}


#define SAFE_CHECK(cond, ...) do { if (!(cond)) throw Exception(ErrorCodes::CANNOT_PARSE_DWARF, __VA_ARGS__); } while (false)


namespace
{
// Maximum number of DIEAbbreviation to cache in a compilation unit. Used to
// speed up inline function lookup.
const uint32_t kMaxAbbreviationEntries = 1000;

// All following read* functions read from a std::string_view, advancing the
// std::string_view, and aborting if there's not enough room.

// Read (bitwise) one object of type T
template <typename T>
requires std::is_trivial_v<T> && std::is_standard_layout_v<T>
T read(std::string_view & sp)
{
    SAFE_CHECK(sp.size() >= sizeof(T), "underflow: expected bytes {}, got bytes {}", sizeof(T), sp.size());
    T x;
    memcpy(&x, sp.data(), sizeof(T));
    sp.remove_prefix(sizeof(T));
    return x;
}

// Read (bitwise) an unsigned number of N bytes (N in 1, 2, 3, 4).
template <size_t N>
uint64_t readU64(std::string_view & sp)
{
    SAFE_CHECK(sp.size() >= N, "underflow");
    uint64_t x = 0;
    memcpy(&x, sp.data(), N);
    sp.remove_prefix(N);
    return x;
}

// Read ULEB (unsigned) varint value; algorithm from the DWARF spec
uint64_t readULEB(std::string_view & sp, uint8_t & shift, uint8_t & val)
{
    uint64_t r = 0;
    shift = 0;
    do
    {
        val = read<uint8_t>(sp);
        r |= (uint64_t(val & 0x7f) << shift);
        shift += 7;
    } while (val & 0x80);
    return r;
}

uint64_t readULEB(std::string_view & sp)
{
    uint8_t shift;
    uint8_t val;
    return readULEB(sp, shift, val);
}

// Read SLEB (signed) varint value; algorithm from the DWARF spec
int64_t readSLEB(std::string_view & sp)
{
    uint8_t shift;
    uint8_t val;
    uint64_t r = readULEB(sp, shift, val);

    if (shift < 64 && (val & 0x40))
    {
        r |= -(1ULL << shift); // sign extend
    }

    return r;
}

// Read a value of "section offset" type, which may be 4 or 8 bytes
uint64_t readOffset(std::string_view & sp, bool is64_bit)
{
    return is64_bit ? read<uint64_t>(sp) : read<uint32_t>(sp);
}

// Read "len" bytes
std::string_view readBytes(std::string_view & sp, uint64_t len)
{
    SAFE_CHECK(len <= sp.size(), "invalid string length: {} vs. {}", len, sp.size());
    std::string_view ret(sp.data(), len);
    sp.remove_prefix(len);
    return ret;
}

// Read a null-terminated string
std::string_view readNullTerminated(std::string_view & sp)
{
    const char * p = static_cast<const char *>(memchr(sp.data(), 0, sp.size()));
    SAFE_CHECK(p, "invalid null-terminated string");
    std::string_view ret(sp.data(), p - sp.data());
    sp = std::string_view(p + 1, sp.size());
    return ret;
}

// Get a string from the section
std::string_view getStringFromStringSection(std::string_view section, uint64_t offset)
{
    SAFE_CHECK(offset < section.size(), "invalid section offset");
    std::string_view sp(section);
    sp.remove_prefix(offset);
    return readNullTerminated(sp);
}

// Skip over padding until sp.data() - start is a multiple of alignment
void skipPadding(std::string_view & sp, const char * start, size_t alignment)
{
    size_t remainder = (sp.data() - start) % alignment;
    if (remainder)
    {
        SAFE_CHECK(alignment - remainder <= sp.size(), "invalid padding");
        sp.remove_prefix(alignment - remainder);
    }
}

}


Dwarf::Path::Path(std::string_view baseDir, std::string_view subDir, std::string_view file)
    : baseDir_(baseDir), subDir_(subDir), file_(file)
{
    using std::swap;

    // Normalize
    if (file_.empty())
    {
        baseDir_ = {};
        subDir_ = {};
        return;
    }

    if (file_[0] == '/')
    {
        // file_ is absolute
        baseDir_ = {};
        subDir_ = {};
    }

    if (!subDir_.empty() && subDir_[0] == '/')
    {
        baseDir_ = {}; // subDir_ is absolute
    }

    // Make sure it's never the case that baseDir_ is empty, but subDir_ isn't.
    if (baseDir_.empty())
    {
        swap(baseDir_, subDir_);
    }
}

size_t Dwarf::Path::size() const
{
    size_t size = 0;
    bool needs_slash = false;

    if (!baseDir_.empty())
    {
        size += baseDir_.size();
        needs_slash = baseDir_.back() != '/';
    }

    if (!subDir_.empty())
    {
        size += needs_slash;
        size += subDir_.size();
        needs_slash = subDir_.back() != '/';
    }

    if (!file_.empty())
    {
        size += needs_slash;
        size += file_.size();
    }

    return size;
}

size_t Dwarf::Path::toBuffer(char * buf, size_t bufSize) const
{
    size_t total_size = 0;
    bool needs_slash = false;

    auto append = [&](std::string_view sp)
    {
        if (bufSize >= 2)
        {
            size_t to_copy = std::min(sp.size(), bufSize - 1);
            memcpy(buf, sp.data(), to_copy);
            buf += to_copy;
            bufSize -= to_copy;
        }
        total_size += sp.size();
    };

    if (!baseDir_.empty())
    {
        append(baseDir_);
        needs_slash = baseDir_.back() != '/';
    }
    if (!subDir_.empty())
    {
        if (needs_slash)
        {
            append("/");
        }
        append(subDir_);
        needs_slash = subDir_.back() != '/';
    }
    if (!file_.empty())
    {
        if (needs_slash)
        {
            append("/");
        }
        append(file_);
    }
    if (bufSize)
    {
        *buf = '\0';
    }

    SAFE_CHECK(total_size == size(), "Size mismatch");
    return total_size;
}

void Dwarf::Path::toString(std::string & dest) const
{
    size_t initial_size = dest.size();
    dest.reserve(initial_size + size());
    if (!baseDir_.empty())
    {
        dest.append(baseDir_.begin(), baseDir_.end());
    }
    if (!subDir_.empty())
    {
        if (!dest.empty() && dest.back() != '/')
        {
            dest.push_back('/');
        }
        dest.append(subDir_.begin(), subDir_.end());
    }
    if (!file_.empty())
    {
        if (!dest.empty() && dest.back() != '/')
        {
            dest.push_back('/');
        }
        dest.append(file_.begin(), file_.end());
    }
    SAFE_CHECK(dest.size() == initial_size + size(), "Size mismatch");
}

// Next chunk in section
bool Dwarf::Section::next(std::string_view & chunk)
{
    chunk = data;
    if (chunk.empty())
        return false;

    // Initial length is a uint32_t value for a 32-bit section, and
    // a 96-bit value (0xffffffff followed by the 64-bit length) for a 64-bit
    // section.
    auto initial_length = read<uint32_t>(chunk);
    is64_bit = (initial_length == uint32_t(-1));
    auto length = is64_bit ? read<uint64_t>(chunk) : initial_length;
    SAFE_CHECK(length <= chunk.size(), "invalid DWARF section");
    chunk = std::string_view(chunk.data(), length);
    data = std::string_view(chunk.end(), data.end() - chunk.end());
    return true;
}

std::string_view Dwarf::getSection(const char * name) const
{
    std::optional<Elf::Section> elf_section = elf_->findSectionByName(name);
    if (!elf_section)
        return {};

#ifdef SHF_COMPRESSED
    if (elf_section->header.sh_flags & SHF_COMPRESSED)
        return {};
#endif

    return { elf_section->begin(), elf_section->size()};
}

// static
bool Dwarf::readAbbreviation(std::string_view & section, DIEAbbreviation & abbr)
{
    // abbreviation code
    abbr.code = readULEB(section);
    if (abbr.code == 0)
        return false;

    // abbreviation tag
    abbr.tag = readULEB(section);

    // does this entry have children?
    abbr.has_children = (read<uint8_t>(section) != DW_CHILDREN_no);

    // attributes
    const char * attribute_begin = section.data();
    for (;;)
    {
        SAFE_CHECK(!section.empty(), "invalid attribute section");
        auto attr = readAttributeSpec(section);
        if (attr.name == 0 && attr.form == 0)
            break;
    }

    abbr.attributes = std::string_view(attribute_begin, section.data() - attribute_begin);
    return true;
}

// static
void Dwarf::readCompilationUnitAbbrs(std::string_view abbrev, CompilationUnit & cu)
{
    abbrev.remove_prefix(cu.abbrev_offset);

    DIEAbbreviation abbr;
    while (readAbbreviation(abbrev, abbr))
    {
        // Abbreviation code 0 is reserved for null debugging information entries.
        if (abbr.code != 0 && abbr.code <= kMaxAbbreviationEntries)
        {
            cu.abbr_cache[abbr.code - 1] = abbr;
        }
    }
}

size_t Dwarf::forEachChild(const CompilationUnit & cu, const Die & die, std::function<bool(const Die & die)> f) const
{
    size_t next_die_offset = forEachAttribute(cu, die, [&](const Attribute &) { return true; });
    if (!die.abbr.has_children)
    {
        return next_die_offset;
    }

    auto child_die = getDieAtOffset(cu, next_die_offset);
    while (child_die.code != 0)
    {
        if (!f(child_die))
        {
            return child_die.offset;
        }

        // NOTE: Don't run `f` over grandchildren, just skip over them.
        size_t sibling_offset = forEachChild(cu, child_die, [](const Die &) { return true; });
        child_die = getDieAtOffset(cu, sibling_offset);
    }

    // childDie is now a dummy die whose offset is to the code 0 marking the
    // end of the children. Need to add one to get the offset of the next die.
    return child_die.offset + 1;
}

/*
 * Iterate over all attributes of the given DIE, calling the given callable
 * for each. Iteration is stopped early if any of the calls return false.
 */
size_t Dwarf::forEachAttribute(const CompilationUnit & cu, const Die & die, std::function<bool(const Attribute & die)> f) const
{
    auto attrs = die.abbr.attributes;
    auto values = std::string_view{info_.data() + die.offset + die.attr_offset, cu.offset + cu.size - die.offset - die.attr_offset};
    while (auto spec = readAttributeSpec(attrs))
    {
        auto attr = readAttribute(cu, die, spec, values);
        if (!f(attr))
        {
            return static_cast<size_t>(-1);
        }
    }
    return values.data() - info_.data();
}

Dwarf::Attribute Dwarf::readAttribute(const CompilationUnit & cu,
    const Die & die,
    AttributeSpec spec,
    std::string_view & info) const
{
    // DWARF 5 introduces new FORMs whose values are relative to some base attrs:
    // DW_AT_str_offsets_base, DW_AT_rnglists_base, DW_AT_addr_base.
    // Debug Fission DWARF 4 uses GNU DW_AT_GNU_ranges_base & DW_AT_GNU_addr_base.
    //
    // The order in which attributes appear in a CU is not defined.
    // The DW_AT_*_base attrs may appear after attributes that need them.
    // The DW_AT_*_base attrs are CU specific; so we read them just after
    // reading the CU header. During this first pass return empty values
    // when encountering a FORM that depends on DW_AT_*_base.
    auto get_string_using_offset_table = [&](uint64_t index)
    {
        if (!cu.str_offsets_base.has_value())
        {
            return std::string_view();
        }
        // DWARF 5: 7.26 String Offsets Table
        // The DW_AT_str_offsets_base attribute points to the first entry following
        // the header. The entries are indexed sequentially from this base entry,
        // starting from 0.
        auto sp = str_offsets_.substr(*cu.str_offsets_base + index * (cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t)));
        uint64_t str_offset = readOffset(sp, cu.is64Bit);
        return getStringFromStringSection(str_, str_offset);
    };

    auto read_debug_addr = [&](uint64_t index)
    {
        if (!cu.addr_base.has_value())
        {
            return uint64_t(0);
        }
        // DWARF 5: 7.27 Address Table
        // The DW_AT_addr_base attribute points to the first entry following the
        // header. The entries are indexed sequentially from this base entry,
        // starting from 0.
        auto sp = addr_.substr(*cu.addr_base + index * sizeof(uint64_t));
        return read<uint64_t>(sp);
    };

    switch (spec.form)
    {
        case DW_FORM_addr:
            return {spec, die, read<uintptr_t>(info)};
        case DW_FORM_block1:
            return {spec, die, readBytes(info, read<uint8_t>(info))};
        case DW_FORM_block2:
            return {spec, die, readBytes(info, read<uint16_t>(info))};
        case DW_FORM_block4:
            return {spec, die, readBytes(info, read<uint32_t>(info))};
        case DW_FORM_block:
            [[fallthrough]];
        case DW_FORM_exprloc:
            return {spec, die, readBytes(info, readULEB(info))};
        case DW_FORM_data1:
            [[fallthrough]];
        case DW_FORM_ref1:
            return {spec, die, read<uint8_t>(info)};
        case DW_FORM_data2:
            [[fallthrough]];
        case DW_FORM_ref2:
            return {spec, die, read<uint16_t>(info)};
        case DW_FORM_data4:
            [[fallthrough]];
        case DW_FORM_ref4:
            return {spec, die, read<uint32_t>(info)};
        case DW_FORM_data8:
            [[fallthrough]];
        case DW_FORM_ref8:
            [[fallthrough]];
        case DW_FORM_ref_sig8:
            return {spec, die, read<uint64_t>(info)};
        case DW_FORM_sdata:
            return {spec, die, static_cast<uint64_t>(readSLEB(info))};
        case DW_FORM_udata:
            [[fallthrough]];
        case DW_FORM_ref_udata:
            return {spec, die, readULEB(info)};
        case DW_FORM_flag:
            return {spec, die, read<uint8_t>(info)};
        case DW_FORM_flag_present:
            return {spec, die, 1ULL};
        case DW_FORM_sec_offset:
            [[fallthrough]];
        case DW_FORM_ref_addr:
            return {spec, die, readOffset(info, die.is64Bit)};
        case DW_FORM_string:
            return {spec, die, readNullTerminated(info)};
        case DW_FORM_strp:
            return {spec, die, getStringFromStringSection(str_, readOffset(info, die.is64Bit))};
        case DW_FORM_indirect: // form is explicitly specified
            // Update spec with the actual FORM.
            spec.form = readULEB(info);
            return readAttribute(cu, die, spec, info);

        // DWARF 5:
        case DW_FORM_implicit_const: // form is explicitly specified
            // For attributes with this form, the attribute specification contains a
            // third part, which is a signed LEB128 number. The value of this number
            // is used as the value of the attribute, and no value is stored in the
            // .debug_info section.
            return {spec, die, static_cast<uint64_t>(spec.implicitConst)};

        case DW_FORM_addrx:
            return {spec, die, read_debug_addr(readULEB(info))};
        case DW_FORM_addrx1:
            return {spec, die, read_debug_addr(readU64<1>(info))};
        case DW_FORM_addrx2:
            return {spec, die, read_debug_addr(readU64<2>(info))};
        case DW_FORM_addrx3:
            return {spec, die, read_debug_addr(readU64<3>(info))};
        case DW_FORM_addrx4:
            return {spec, die, read_debug_addr(readU64<4>(info))};

        case DW_FORM_line_strp:
            return {spec, die, getStringFromStringSection(line_str_, readOffset(info, die.is64Bit))};

        case DW_FORM_strx:
            return {spec, die, get_string_using_offset_table(readULEB(info))};
        case DW_FORM_strx1:
            return {spec, die, get_string_using_offset_table(readU64<1>(info))};
        case DW_FORM_strx2:
            return {spec, die, get_string_using_offset_table(readU64<2>(info))};
        case DW_FORM_strx3:
            return {spec, die, get_string_using_offset_table(readU64<3>(info))};
        case DW_FORM_strx4:
            return {spec, die, get_string_using_offset_table(readU64<4>(info))};

        case DW_FORM_rnglistx: {
            auto index = readULEB(info);
            if (!cu.rnglists_base.has_value())
            {
                return {spec, die, 0ULL};
            }
            const uint64_t offset_size = cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t);
            auto sp = rnglists_.substr(*cu.rnglists_base + index * offset_size);
            auto offset = readOffset(sp, cu.is64Bit);
            return {spec, die, *cu.rnglists_base + offset};
        }

        case DW_FORM_loclistx: {
            auto index = readULEB(info);
            if (!cu.loclists_base.has_value())
            {
                return {spec, die, 0ULL};
            }
            const uint64_t offset_size = cu.is64Bit ? sizeof(uint64_t) : sizeof(uint32_t);
            auto sp = loclists_.substr(*cu.loclists_base + index * offset_size);
            auto offset = readOffset(sp, cu.is64Bit);
            return {spec, die, *cu.loclists_base + offset};
        }

        case DW_FORM_data16:
            return {spec, die, readBytes(info, 16)};

        case DW_FORM_ref_sup4:
        case DW_FORM_ref_sup8:
        case DW_FORM_strp_sup:
            SAFE_CHECK(false, "Unexpected DWARF5 supplimentary object files");

        default:
            SAFE_CHECK(false, "invalid attribute form");
    }
    return {spec, die, 0ULL};
}

// static
Dwarf::AttributeSpec Dwarf::readAttributeSpec(std::string_view & sp)
{
    Dwarf::AttributeSpec spec;
    spec.name = readULEB(sp);
    spec.form = readULEB(sp);
    if (spec.form == DW_FORM_implicit_const)
    {
        spec.implicitConst = readSLEB(sp);
    }
    return spec;
}

Dwarf::CompilationUnit Dwarf::getCompilationUnit(uint64_t offset) const
{
    // SAFE_CHECK(offset < info_.size(), "unexpected offset");
    CompilationUnit cu;
    std::string_view chunk(info_);
    cu.offset = offset;
    chunk.remove_prefix(offset);

    // 1) unit_length
    auto initial_length = read<uint32_t>(chunk);
    cu.is64Bit = (initial_length == uint32_t(-1));
    cu.size = cu.is64Bit ? read<uint64_t>(chunk) : initial_length;
    SAFE_CHECK(cu.size <= chunk.size(), "invalid chunk size");
    cu.size += cu.is64Bit ? 12 : 4;

    // 2) version
    cu.version = read<uint16_t>(chunk);
    SAFE_CHECK(cu.version >= 2 && cu.version <= 5, "invalid info version");

    if (cu.version == 5)
    {
        // DWARF5: 7.5.1.1 Full and Partial Compilation Unit Headers
        // 3) unit_type (new DWARF 5)
        cu.unit_type = read<uint8_t>(chunk);
        if (cu.unit_type != DW_UT_compile && cu.unit_type != DW_UT_skeleton)
        {
            return cu;
        }
        // 4) address_size
        cu.addr_size = read<uint8_t>(chunk);
        SAFE_CHECK(cu.addr_size == sizeof(uintptr_t), "invalid address size");

        // 5) debug_abbrev_offset
        cu.abbrev_offset = readOffset(chunk, cu.is64Bit);

        if (cu.unit_type == DW_UT_skeleton)
        {
            // 6) dwo_id
            read<uint64_t>(chunk);
        }
    }
    else
    {
        // DWARF4 has a single type of unit in .debug_info
        cu.unit_type = DW_UT_compile;
        // 3) debug_abbrev_offset
        cu.abbrev_offset = readOffset(chunk, cu.is64Bit);
        // 4) address_size
        cu.addr_size = read<uint8_t>(chunk);
        SAFE_CHECK(cu.addr_size == sizeof(uintptr_t), "invalid address size");
    }
    cu.first_die = chunk.data() - info_.data();
    if (cu.version < 5)
    {
        return cu;
    }

    Die die = getDieAtOffset(cu, cu.first_die);
    if (die.abbr.tag != DW_TAG_compile_unit)
    {
        return cu;
    }

    // Read the DW_AT_*_base attributes.
    // Attributes which use FORMs relative to these base attrs
    // will not have valid values during this first pass!
    forEachAttribute(
        cu,
        die,
        [&](const Attribute & attr)
        {
            switch (attr.spec.name)
            {
                case DW_AT_addr_base:
                case DW_AT_GNU_addr_base:
                    cu.addr_base = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_loclists_base:
                    cu.loclists_base = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_rnglists_base:
                case DW_AT_GNU_ranges_base:
                    cu.rnglists_base = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_str_offsets_base:
                    cu.str_offsets_base = std::get<uint64_t>(attr.attr_value);
                    break;
            }
            return true; // continue forEachAttribute
        });
    return cu;
}

// Finds the Compilation Unit starting at offset.
Dwarf::CompilationUnit Dwarf::findCompilationUnit(uint64_t targetOffset) const
{
    // SAFE_CHECK(targetOffset < info_.size(), "unexpected target address");
    uint64_t offset = 0;
    while (offset < info_.size())
    {
        std::string_view chunk(info_);
        chunk.remove_prefix(offset);

        auto initial_length = read<uint32_t>(chunk);
        auto is64_bit = (initial_length == static_cast<uint32_t>(-1));
        auto size = is64_bit ? read<uint64_t>(chunk) : initial_length;
        SAFE_CHECK(size <= chunk.size(), "invalid chunk size");
        size += is64_bit ? 12 : 4;

        if (offset + size > targetOffset)
        {
            break;
        }
        offset += size;
    }
    return getCompilationUnit(offset);
}


Dwarf::DIEAbbreviation Dwarf::getAbbreviation(uint64_t code, uint64_t offset) const
{
    // Linear search in the .debug_abbrev section, starting at offset
    std::string_view section = abbrev_;
    section.remove_prefix(offset);

    Dwarf::DIEAbbreviation abbr;
    while (readAbbreviation(section, abbr))
        if (abbr.code == code)
            return abbr;

    SAFE_CHECK(false, "could not find abbreviation code");
}

Dwarf::AttributeValue Dwarf::readAttributeValue(std::string_view & sp, uint64_t form, bool is64_bit) const
{
    switch (form)
    {
        case DW_FORM_addr:
            return uint64_t(read<uintptr_t>(sp));
        case DW_FORM_block1:
            return readBytes(sp, read<uint8_t>(sp));
        case DW_FORM_block2:
            return readBytes(sp, read<uint16_t>(sp));
        case DW_FORM_block4:
            return readBytes(sp, read<uint32_t>(sp));
        case DW_FORM_block: [[fallthrough]];
        case DW_FORM_exprloc:
            return readBytes(sp, readULEB(sp));
        case DW_FORM_data1: [[fallthrough]];
        case DW_FORM_ref1:
            return uint64_t(read<uint8_t>(sp));
        case DW_FORM_data2: [[fallthrough]];
        case DW_FORM_ref2:
            return uint64_t(read<uint16_t>(sp));
        case DW_FORM_data4: [[fallthrough]];
        case DW_FORM_ref4:
            return uint64_t(read<uint32_t>(sp));
        case DW_FORM_data8: [[fallthrough]];
        case DW_FORM_ref8:
            return read<uint64_t>(sp);
        case DW_FORM_sdata:
            return uint64_t(readSLEB(sp));
        case DW_FORM_udata: [[fallthrough]];
        case DW_FORM_ref_udata:
            return readULEB(sp);
        case DW_FORM_flag:
            return uint64_t(read<uint8_t>(sp));
        case DW_FORM_flag_present:
            return uint64_t(1);
        case DW_FORM_sec_offset: [[fallthrough]];
        case DW_FORM_ref_addr:
            return readOffset(sp, is64_bit);
        case DW_FORM_string:
            return readNullTerminated(sp);
        case DW_FORM_strp:
            return getStringFromStringSection(str_, readOffset(sp, is64_bit));
        case DW_FORM_indirect: // form is explicitly specified
            return readAttributeValue(sp, readULEB(sp), is64_bit);
        default:
            SAFE_CHECK(false, "invalid attribute form");
    }
}

/**
 * Find @address in .debug_aranges and return the offset in
 * .debug_info for compilation unit to which this address belongs.
 */
bool Dwarf::findDebugInfoOffset(uintptr_t address, std::string_view aranges, uint64_t & offset)
{
    Section aranges_section(aranges);
    std::string_view chunk;
    while (aranges_section.next(chunk))
    {
        auto version = read<uint16_t>(chunk);
        SAFE_CHECK(version == 2, "invalid aranges version");

        offset = readOffset(chunk, aranges_section.is64Bit());
        auto address_size = read<uint8_t>(chunk);
        SAFE_CHECK(address_size == sizeof(uintptr_t), "invalid address size");
        auto segment_size = read<uint8_t>(chunk);
        SAFE_CHECK(segment_size == 0, "segmented architecture not supported");

        // Padded to a multiple of 2 addresses.
        // Strangely enough, this is the only place in the DWARF spec that requires
        // padding.
        skipPadding(chunk, aranges.data(), 2 * sizeof(uintptr_t));
        for (;;)
        {
            auto start = read<uintptr_t>(chunk);
            auto length = read<uintptr_t>(chunk);

            if (start == 0 && length == 0)
                break;

            // Is our address in this range?
            if (address >= start && address < start + length)
                return true;
        }
    }
    return false;
}

Dwarf::Die Dwarf::getDieAtOffset(const CompilationUnit & cu, uint64_t offset) const
{
    SAFE_CHECK(offset < info_.size(), "unexpected offset {}, info size {}", offset, info_.size());
    Die die;
    std::string_view sp{info_.data() + offset, cu.offset + cu.size - offset};
    die.offset = offset;
    die.is64Bit = cu.is64Bit;
    auto code = readULEB(sp);
    die.code = code;
    if (code == 0)
    {
        return die;
    }
    die.attr_offset = sp.data() - info_.data() - offset;
    die.abbr = !cu.abbr_cache.empty() && die.code < kMaxAbbreviationEntries ? cu.abbr_cache[die.code - 1]
                                                                            : getAbbreviation(die.code, cu.abbrev_offset);

    return die;
}

/**
 * Find the @locationInfo for @address in the compilation unit represented
 * by the @sp .debug_info entry.
 * Returns whether the address was found.
 * Advances @sp to the next entry in .debug_info.
 */
bool Dwarf::findLocation(
    uintptr_t address,
    const LocationInfoMode mode,
    CompilationUnit & cu,
    LocationInfo & info,
    std::vector<SymbolizedFrame> & inline_frames) const
{
    Die die = getDieAtOffset(cu, cu.first_die);
    // Partial compilation unit (DW_TAG_partial_unit) is not supported.
    SAFE_CHECK(die.abbr.tag == DW_TAG_compile_unit, "expecting compile unit entry");

    // Offset in .debug_line for the line number VM program for this CU
    std::optional<uint64_t> line_offset = 0;
    std::string_view compilation_directory;
    std::optional<std::string_view> main_file_name;
    std::optional<uint64_t> base_addr_cu;

    forEachAttribute(cu, die, [&](const Attribute & attr)
    {
        switch (attr.spec.name)
        {
            case DW_AT_stmt_list:
                // Offset in .debug_line for the line number VM program for this
                // compilation unit
                line_offset = std::get<uint64_t>(attr.attr_value);
                break;
            case DW_AT_comp_dir:
                // Compilation directory
                compilation_directory = std::get<std::string_view>(attr.attr_value);
                break;
            case DW_AT_name:
                // File name of main file being compiled
                main_file_name = std::get<std::string_view>(attr.attr_value);
                break;
            case DW_AT_low_pc:
            case DW_AT_entry_pc:
                // 2.17.1: historically DW_AT_low_pc was used. DW_AT_entry_pc was
                // introduced in DWARF3. Support either to determine the base address of
                // the CU.
                base_addr_cu = std::get<uint64_t>(attr.attr_value);
                break;
        }
        // Iterate through all attributes until find all above.
        return true;
    });

    if (main_file_name)
    {
        info.has_main_file = true;
        info.main_file = Path(compilation_directory, "", *main_file_name);
    }

    if (!line_offset)
    {
        return false;
    }

    std::string_view line_section(line_);
    line_section.remove_prefix(*line_offset);
    LineNumberVM line_vm(line_section, compilation_directory, str_, line_str_);

    // Execute line number VM program to find file and line
    info.has_file_and_line = line_vm.findAddress(address, info.file, info.line);

    bool check_inline = (mode == LocationInfoMode::FULL_WITH_INLINE);

    if (info.has_file_and_line && check_inline)
    {
        // Re-get the compilation unit with abbreviation cached.
        cu.abbr_cache.clear();
        cu.abbr_cache.resize(kMaxAbbreviationEntries);
        readCompilationUnitAbbrs(abbrev_, cu);

        // Find the subprogram that matches the given address.
        Die subprogram;
        findSubProgramDieForAddress(cu, die, address, base_addr_cu, subprogram);

        // Subprogram is the DIE of caller function.
        if (/*check_inline &&*/ subprogram.abbr.has_children)
        {
            // Use an extra location and get its call file and call line, so that
            // they can be used for the second last location when we don't have
            // enough inline frames for all inline functions call stack.
            const size_t max_size = Dwarf::kMaxInlineLocationInfoPerFrame + 1;
            std::vector<CallLocation> call_locations;
            call_locations.reserve(Dwarf::kMaxInlineLocationInfoPerFrame + 1);

            findInlinedSubroutineDieForAddress(cu, subprogram, line_vm, address, base_addr_cu, call_locations, max_size);
            size_t num_found = call_locations.size();

            if (num_found > 0)
            {
                const auto inner_most_file = info.file;
                const auto inner_most_line = info.line;

                // Earlier we filled in locationInfo:
                // - mainFile: the path to the CU -- the file where the non-inlined
                //   call is made from.
                // - file + line: the location of the inner-most inlined call.
                // Here we already find inlined info so mainFile would be redundant.
                info.has_main_file = false;
                info.main_file = Path{};
                // @findInlinedSubroutineDieForAddress fills inlineLocations[0] with the
                // file+line of the non-inlined outer function making the call.
                // locationInfo.name is already set by the caller by looking up the
                // non-inlined function @address belongs to.
                info.has_file_and_line = true;
                info.file = call_locations[0].file;
                info.line = call_locations[0].line;

                // The next inlined subroutine's call file and call line is the current
                // caller's location.
                for (size_t i = 0; i < num_found - 1; ++i)
                {
                    call_locations[i].file = call_locations[i + 1].file;
                    call_locations[i].line = call_locations[i + 1].line;
                }
                // CallLocation for the inner-most inlined function:
                // - will be computed if enough space was available in the passed
                //   buffer.
                // - will have a .name, but no !.file && !.line
                // - its corresponding file+line is the one returned by LineVM based
                //   on @address.
                // Use the inner-most inlined file+line info we got from the LineVM.
                call_locations[num_found - 1].file = inner_most_file;
                call_locations[num_found - 1].line = inner_most_line;

                // Fill in inline frames in reverse order (as expected by the caller).
                std::reverse(call_locations.begin(), call_locations.end());
                for (const auto & call_location : call_locations)
                {
                    SymbolizedFrame inline_frame;
                    inline_frame.found = true;
                    inline_frame.addr = address;
                    if (!call_location.name.empty())
                        inline_frame.name = call_location.name.data();
                    else
                        inline_frame.name = nullptr;
                    inline_frame.location.has_file_and_line = true;
                    inline_frame.location.file = call_location.file;
                    inline_frame.location.line = call_location.line;
                    inline_frames.push_back(inline_frame);
                }
            }
        }
    }

    return info.has_file_and_line;
}

void Dwarf::findSubProgramDieForAddress(const CompilationUnit & cu,
    const Die & die,
    uint64_t address,
    std::optional<uint64_t> base_addr_cu,
    Die & subprogram) const
{
    forEachChild(cu, die, [&](const Die & child_die)
    {
        if (child_die.abbr.tag == DW_TAG_subprogram)
        {
            std::optional<uint64_t> low_pc;
            std::optional<uint64_t> high_pc;
            std::optional<bool> is_high_pc_addr;
            std::optional<uint64_t> range_offset;
            forEachAttribute(cu, child_die, [&](const Attribute & attr)
            {
                switch (attr.spec.name)
                {
                    case DW_AT_ranges:
                        range_offset = std::get<uint64_t>(attr.attr_value);
                        break;
                    case DW_AT_low_pc:
                        low_pc = std::get<uint64_t>(attr.attr_value);
                        break;
                    case DW_AT_high_pc:
                        // The value of the DW_AT_high_pc attribute can be
                        // an address (DW_FORM_addr*) or an offset (DW_FORM_data*).
                        is_high_pc_addr = attr.spec.form == DW_FORM_addr || //
                            attr.spec.form == DW_FORM_addrx || //
                            attr.spec.form == DW_FORM_addrx1 || //
                            attr.spec.form == DW_FORM_addrx2 || //
                            attr.spec.form == DW_FORM_addrx3 || //
                            attr.spec.form == DW_FORM_addrx4;
                        high_pc = std::get<uint64_t>(attr.attr_value);
                        break;
                }
                // Iterate through all attributes until find all above.
                return true;
            });
            bool pc_match = low_pc && high_pc && is_high_pc_addr && address >= *low_pc
                && (address < (*is_high_pc_addr ? *high_pc : *low_pc + *high_pc));
            bool range_match = range_offset && isAddrInRangeList(cu, address, base_addr_cu, range_offset.value(), cu.addr_size);
            if (pc_match || range_match)
            {
                subprogram = child_die;
                return false;
            }
        }

        findSubProgramDieForAddress(cu, child_die, address, base_addr_cu, subprogram);

        // Iterates through children until find the inline subprogram.
        return true;
    });
}

/**
 * Find DW_TAG_inlined_subroutine child DIEs that contain @address and
 * then extract:
 * - Where was it called from (DW_AT_call_file & DW_AT_call_line):
 *   the statement or expression that caused the inline expansion.
 * - The inlined function's name. As a function may be inlined multiple
 *   times, common attributes like DW_AT_linkage_name or DW_AT_name
 *   are only stored in its "concrete out-of-line instance" (a
 *   DW_TAG_subprogram) which we find using DW_AT_abstract_origin.
 */
void Dwarf::findInlinedSubroutineDieForAddress(
    const CompilationUnit & cu,
    const Die & die,
    const LineNumberVM & line_vm,
    uint64_t address,
    std::optional<uint64_t> base_addr_cu,
    std::vector<CallLocation> & locations,
    const size_t max_size) const
{
    if (locations.size() >= max_size)
    {
        return;
    }

    forEachChild(cu, die, [&](const Die & child_die)
    {
        // Between a DW_TAG_subprogram and and DW_TAG_inlined_subroutine we might
        // have arbitrary intermediary "nodes", including DW_TAG_common_block,
        // DW_TAG_lexical_block, DW_TAG_try_block, DW_TAG_catch_block and
        // DW_TAG_with_stmt, etc.
        // We can't filter with locationhere since its range may be not specified.
        // See section 2.6.2: A location list containing only an end of list entry
        // describes an object that exists in the source code but not in the
        // executable program.
        if (child_die.abbr.tag == DW_TAG_try_block || child_die.abbr.tag == DW_TAG_catch_block || child_die.abbr.tag == DW_TAG_entry_point
            || child_die.abbr.tag == DW_TAG_common_block || child_die.abbr.tag == DW_TAG_lexical_block)
        {
            findInlinedSubroutineDieForAddress(cu, child_die, line_vm, address, base_addr_cu, locations, max_size);
            return true;
        }

        std::optional<uint64_t> low_pc;
        std::optional<uint64_t> high_pc;
        std::optional<bool> is_high_pc_addr;
        std::optional<uint64_t> abstract_origin;
        std::optional<uint64_t> abstract_origin_ref_type;
        std::optional<uint64_t> call_file;
        std::optional<uint64_t> call_line;
        std::optional<uint64_t> range_offset;
        forEachAttribute(cu, child_die, [&](const Attribute & attr)
        {
            switch (attr.spec.name)
            {
                case DW_AT_ranges:
                    range_offset = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_low_pc:
                    low_pc = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_high_pc:
                    // The value of the DW_AT_high_pc attribute can be
                    // an address (DW_FORM_addr*) or an offset (DW_FORM_data*).
                    is_high_pc_addr = attr.spec.form == DW_FORM_addr || //
                        attr.spec.form == DW_FORM_addrx || //
                        attr.spec.form == DW_FORM_addrx1 || //
                        attr.spec.form == DW_FORM_addrx2 || //
                        attr.spec.form == DW_FORM_addrx3 || //
                        attr.spec.form == DW_FORM_addrx4;
                    high_pc = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_abstract_origin:
                    abstract_origin_ref_type = attr.spec.form;
                    abstract_origin = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_call_line:
                    call_line = std::get<uint64_t>(attr.attr_value);
                    break;
                case DW_AT_call_file:
                    call_file = std::get<uint64_t>(attr.attr_value);
                    break;
            }
            // Iterate through all until find all above attributes.
            return true;
        });

        // 2.17 Code Addresses and Ranges
        // Any debugging information entry describing an entity that has a
        // machine code address or range of machine code addresses,
        // which includes compilation units, module initialization, subroutines,
        // ordinary blocks, try/catch blocks, labels and the like, may have
        //  - A DW_AT_low_pc attribute for a single address,
        //  - A DW_AT_low_pc and DW_AT_high_pc pair of attributes for a
        //    single contiguous range of addresses, or
        //  - A DW_AT_ranges attribute for a non-contiguous range of addresses.
        // TODO: Support DW_TAG_entry_point and DW_TAG_common_block that don't
        // have DW_AT_low_pc/DW_AT_high_pc pairs and DW_AT_ranges.
        // TODO: Support relocated address which requires lookup in relocation map.
        bool pc_match
            = low_pc && high_pc && is_high_pc_addr && address >= *low_pc && (address < (*is_high_pc_addr ? *high_pc : *low_pc + *high_pc));
        bool range_match = range_offset && isAddrInRangeList(cu, address, base_addr_cu, range_offset.value(), cu.addr_size);
        if (!pc_match && !range_match)
        {
            // Address doesn't match. Keep searching other children.
            return true;
        }

        if (!abstract_origin || !abstract_origin_ref_type || !call_line || !call_file)
        {
            // We expect a single sibling DIE to match on addr, but it's missing
            // required fields. Stop searching for other DIEs.
            return false;
        }

        CallLocation location;
        location.file = line_vm.getFullFileName(*call_file);
        location.line = *call_line;

        /// Something wrong with receiving debug info about inline.
        /// If set to true we stop parsing DWARF.
        bool die_for_inline_broken = false;

        auto get_function_name = [&](const CompilationUnit & srcu, uint64_t die_offset)
        {
            Die decl_die = getDieAtOffset(srcu, die_offset);
            auto & die_to_look_for_name = decl_die;

            Die def_die;
            // Jump to the actual function definition instead of declaration for name
            // and line info.
            // DW_AT_specification: Incomplete, non-defining, or separate declaration
            // corresponding to a declaration
            auto offset = getAttribute<uint64_t>(srcu, decl_die, DW_AT_specification);
            if (offset)
            {
                /// FIXME: actually it's a bug in our DWARF parser.
                ///
                /// Most of the times compilation unit offset (srcu.offset) is some big number inside .debug_info (like 434782255).
                /// Offset of DIE definition is some small relative number to srcu.offset (like 3518).
                /// However in some unknown cases offset looks like global, non relative number (like 434672579) and in this
                /// case we obviously doing something wrong parsing DWARF.
                ///
                /// What is important -- this bug? reproduces only with -flto=thin in release mode.
                /// Also llvm-dwarfdump --verify ./clickhouse says that our DWARF is ok, so it's another prove
                /// that we just doing something wrong.
                ///
                /// FIXME: Currently we just give up parsing DWARF for inlines when we got into this situation.
                if (srcu.offset + offset.value() >= info_.size())
                {
                    die_for_inline_broken = true;
                }
                else
                {
                    def_die = getDieAtOffset(srcu, srcu.offset + offset.value());
                    die_to_look_for_name = def_die;
                }
            }

            std::string_view name;

            if (die_for_inline_broken)
                return name;

            // The file and line will be set in the next inline subroutine based on
            // its DW_AT_call_file and DW_AT_call_line.
            forEachAttribute(srcu, die_to_look_for_name, [&](const Attribute & attr)
            {
                switch (attr.spec.name)
                {
                    case DW_AT_linkage_name:
                        name = std::get<std::string_view>(attr.attr_value);
                        break;
                    case DW_AT_name:
                        // NOTE: when DW_AT_linkage_name and DW_AT_name match, dwarf
                        // emitters omit DW_AT_linkage_name (to save space). If present
                        // DW_AT_linkage_name should always be preferred (mangled C++ name
                        // vs just the function name).
                        if (name.empty())
                        {
                            name = std::get<std::string_view>(attr.attr_value);
                        }
                        break;
                }
                return true;
            });
            return name;
        };

        // DW_AT_abstract_origin is a reference. There a 3 types of references:
        // - the reference can identify any debugging information entry within the
        //   compilation unit (DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4,
        //   DW_FORM_ref8, DW_FORM_ref_udata). This type of reference is an offset
        //   from the first byte of the compilation header for the compilation unit
        //   containing the reference.
        // - the reference can identify any debugging information entry within a
        //   .debug_info section; in particular, it may refer to an entry in a
        //   different compilation unit (DW_FORM_ref_addr)
        // - the reference can identify any debugging information type entry that
        //   has been placed in its own type unit.
        //   Not applicable for DW_AT_abstract_origin.
        location.name = (*abstract_origin_ref_type != DW_FORM_ref_addr)
            ? get_function_name(cu, cu.offset + *abstract_origin)
            : get_function_name(findCompilationUnit(*abstract_origin), *abstract_origin);

        /// FIXME: see comment above
        if (die_for_inline_broken)
            return false;

        locations.push_back(location);

        findInlinedSubroutineDieForAddress(cu, child_die, line_vm, address, base_addr_cu, locations, max_size);

        return false;
    });
}

bool Dwarf::findAddress(
    uintptr_t address, LocationInfo & locationInfo, LocationInfoMode mode, std::vector<SymbolizedFrame> & inline_frames) const
{
    locationInfo = LocationInfo();

    if (mode == LocationInfoMode::DISABLED)
    {
        return false;
    }

    if (!elf_)
    { // No file.
        return false;
    }

    if (!aranges_.empty())
    {
        // Fast path: find the right .debug_info entry by looking up the
        // address in .debug_aranges.
        uint64_t offset = 0;
        if (findDebugInfoOffset(address, aranges_, offset))
        {
            // Read compilation unit header from .debug_info
            auto unit = getCompilationUnit(offset);
            if (unit.unit_type != DW_UT_compile && unit.unit_type != DW_UT_skeleton)
            {
                return false;
            }
            findLocation(address, mode, unit, locationInfo, inline_frames);
            return locationInfo.has_file_and_line;
        }
        else if (mode == LocationInfoMode::FAST)
        {
            // NOTE: Clang (when using -gdwarf-aranges) doesn't generate entries
            // in .debug_aranges for some functions, but always generates
            // .debug_info entries.  Scanning .debug_info is slow, so fall back to
            // it only if such behavior is requested via LocationInfoMode.
            return false;
        }
        else
        {
            SAFE_CHECK(mode == LocationInfoMode::FULL || mode == LocationInfoMode::FULL_WITH_INLINE, "unexpected mode");
            // Fall back to the linear scan.
        }
    }

    // Slow path (linear scan): Iterate over all .debug_info entries
    // and look for the address in each compilation unit.
    uint64_t offset = 0;
    while (offset < info_.size() && !locationInfo.has_file_and_line)
    {
        auto unit = getCompilationUnit(offset);
        offset += unit.size;
        if (unit.unit_type != DW_UT_compile && unit.unit_type != DW_UT_skeleton)
        {
            continue;
        }
        findLocation(address, mode, unit, locationInfo, inline_frames);
    }

    return locationInfo.has_file_and_line;
}

bool Dwarf::isAddrInRangeList(const CompilationUnit & cu,
    uint64_t address,
    std::optional<uint64_t> base_addr,
    size_t offset,
    uint8_t addr_size) const
{
    SAFE_CHECK(addr_size == 4 || addr_size == 8, "wrong address size");
    if (cu.version <= 4 && !ranges_.empty())
    {
        const bool is64_bit_addr = addr_size == 8;
        std::string_view sp = ranges_;
        sp.remove_prefix(offset);
        const uint64_t max_addr = is64_bit_addr ? std::numeric_limits<uint64_t>::max() : std::numeric_limits<uint32_t>::max();
        while (!sp.empty())
        {
            uint64_t begin = readOffset(sp, is64_bit_addr);
            uint64_t end = readOffset(sp, is64_bit_addr);
            // The range list entry is a base address selection entry.
            if (begin == max_addr)
            {
                base_addr = end;
                continue;
            }
            // The range list entry is an end of list entry.
            if (begin == 0 && end == 0)
            {
                break;
            }

            // Check if the given address falls in the range list entry.
            // 2.17.3 Non-Contiguous Address Ranges
            // The applicable base address of a range list entry is determined by the
            // closest preceding base address selection entry (see below) in the same
            // range list. If there is no such selection entry, then the applicable
            // base address defaults to the base address of the compilation unit.
            if (base_addr && address >= begin + *base_addr && address < end + *base_addr)
            {
                return true;
            }
        }
    }

    if (cu.version == 5 && !rnglists_.empty() && cu.addr_base.has_value())
    {
        auto rnglists = rnglists_;
        rnglists.remove_prefix(offset);

        while (!rnglists.empty())
        {
            auto kind = read<uint8_t>(rnglists);
            switch (kind)
            {
                case DW_RLE_end_of_list:
                    return false;
                case DW_RLE_base_addressx: {
                    auto index = readULEB(rnglists);
                    auto sp = addr_.substr(*cu.addr_base + index * sizeof(uint64_t));
                    base_addr = read<uint64_t>(sp);
                }
                break;

                case DW_RLE_startx_endx: {
                    auto index_start = readULEB(rnglists);
                    auto index_end = readULEB(rnglists);
                    auto sp_start = addr_.substr(*cu.addr_base + index_start * sizeof(uint64_t));
                    auto start = read<uint64_t>(sp_start);

                    auto sp_end = addr_.substr(*cu.addr_base + index_end * sizeof(uint64_t));
                    auto end = read<uint64_t>(sp_end);
                    if (address >= start && address < end)
                    {
                        return true;
                    }
                }
                break;

                case DW_RLE_startx_length: {
                    auto index_start = readULEB(rnglists);
                    auto length = readULEB(rnglists);
                    auto sp_start = addr_.substr(*cu.addr_base + index_start * sizeof(uint64_t));
                    auto start = read<uint64_t>(sp_start);

                    auto sp_end = addr_.substr(*cu.addr_base + index_start * sizeof(uint64_t) + length);
                    auto end = read<uint64_t>(sp_end);
                    if (start != end && address >= start && address < end)
                    {
                        return true;
                    }
                }
                break;

                case DW_RLE_offset_pair: {
                    auto offset_start = readULEB(rnglists);
                    auto offset_end = readULEB(rnglists);
                    if (base_addr && address >= (*base_addr + offset_start) && address < (*base_addr + offset_end))
                    {
                        return true;
                    }
                }
                break;

                case DW_RLE_base_address:
                    base_addr = read<uint64_t>(rnglists);
                    break;

                case DW_RLE_start_end: {
                    uint64_t start = read<uint64_t>(rnglists);
                    uint64_t end = read<uint64_t>(rnglists);
                    if (address >= start && address < end)
                    {
                        return true;
                    }
                }
                break;

                case DW_RLE_start_length: {
                    uint64_t start = read<uint64_t>(rnglists);
                    uint64_t end = start + readULEB(rnglists);
                    if (address >= start && address < end)
                    {
                        return true;
                    }
                }
                break;

                default:
                    SAFE_CHECK(false, "Unexpected debug_rnglists entry kind");
            }
        }
    }
    return false;
}


Dwarf::LineNumberVM::LineNumberVM(
    std::string_view data,
    std::string_view compilationDirectory,
    std::string_view debugStr,
    std::string_view debugLineStr)
    : compilationDirectory_(compilationDirectory)
    , debugStr_(debugStr)
    , debugLineStr_(debugLineStr)
{
    Section section(data);
    SAFE_CHECK(section.next(data_), "invalid line number VM");
    is64Bit_ = section.is64Bit();
    init();
    reset();
}

void Dwarf::LineNumberVM::reset()
{
    address_ = 0;
    file_ = 1;
    line_ = 1;
    column_ = 0;
    isStmt_ = defaultIsStmt_;
    basicBlock_ = false;
    endSequence_ = false;
    prologueEnd_ = false;
    epilogueBegin_ = false;
    isa_ = 0;
    discriminator_ = 0;
}

struct LineNumberAttribute
{
    uint64_t content_type_code;
    uint64_t form_code;
    std::variant<uint64_t, std::string_view> attr_value;
};

LineNumberAttribute readLineNumberAttribute(
    bool is64_bit, std::string_view & format, std::string_view & entries, std::string_view debugStr, std::string_view debugLineStr)
{
    uint64_t content_type_code = readULEB(format);
    uint64_t form_code = readULEB(format);
    std::variant<uint64_t, std::string_view> attr_value;

    switch (content_type_code)
    {
        case DW_LNCT_path: {
            switch (form_code)
            {
                case DW_FORM_string:
                    attr_value = readNullTerminated(entries);
                    break;
                case DW_FORM_line_strp: {
                    auto off = readOffset(entries, is64_bit);
                    attr_value = getStringFromStringSection(debugLineStr, off);
                }
                break;
                case DW_FORM_strp:
                    attr_value = getStringFromStringSection(debugStr, readOffset(entries, is64_bit));
                    break;
                case DW_FORM_strp_sup:
                    SAFE_CHECK(false, "Unexpected DW_FORM_strp_sup");
                    break;
                default:
                    SAFE_CHECK(false, "Unexpected form for DW_LNCT_path");
                    break;
            }
        }
        break;

        case DW_LNCT_directory_index: {
            switch (form_code)
            {
                case DW_FORM_data1:
                    attr_value = read<uint8_t>(entries);
                    break;
                case DW_FORM_data2:
                    attr_value = read<uint16_t>(entries);
                    break;
                case DW_FORM_udata:
                    attr_value = readULEB(entries);
                    break;
                default:
                    SAFE_CHECK(false, "Unexpected form for DW_LNCT_directory_index");
                    break;
            }
        }
        break;

        case DW_LNCT_timestamp: {
            switch (form_code)
            {
                case DW_FORM_udata:
                    attr_value = readULEB(entries);
                    break;
                case DW_FORM_data4:
                    attr_value = read<uint32_t>(entries);
                    break;
                case DW_FORM_data8:
                    attr_value = read<uint64_t>(entries);
                    break;
                case DW_FORM_block:
                    attr_value = readBytes(entries, readULEB(entries));
                    break;
                default:
                    SAFE_CHECK(false, "Unexpected form for DW_LNCT_timestamp");
            }
        }
        break;

        case DW_LNCT_size: {
            switch (form_code)
            {
                case DW_FORM_udata:
                    attr_value = readULEB(entries);
                    break;
                case DW_FORM_data1:
                    attr_value = read<uint8_t>(entries);
                    break;
                case DW_FORM_data2:
                    attr_value = read<uint16_t>(entries);
                    break;
                case DW_FORM_data4:
                    attr_value = read<uint32_t>(entries);
                    break;
                case DW_FORM_data8:
                    attr_value = read<uint64_t>(entries);
                    break;
                default:
                    SAFE_CHECK(false, "Unexpected form for DW_LNCT_size");
                    break;
            }
        }
        break;

        case DW_LNCT_MD5: {
            switch (form_code)
            {
                case DW_FORM_data16:
                    attr_value = readBytes(entries, 16);
                    break;
                default:
                    SAFE_CHECK(false, "Unexpected form for DW_LNCT_MD5");
                    break;
            }
        }
        break;

        default:
            // TODO: skip over vendor data as specified by the form instead.
            SAFE_CHECK(false, "Unexpected vendor content type code");
            break;
    }
    return {
        .content_type_code = content_type_code,
        .form_code = form_code,
        .attr_value = attr_value,
    };
}

void Dwarf::LineNumberVM::init()
{
    version_ = read<uint16_t>(data_);
    SAFE_CHECK(version_ >= 2 && version_ <= 5, "invalid version in line number VM: {}", version_);
    if (version_ == 5)
    {
        auto address_size = read<uint8_t>(data_);
        SAFE_CHECK(address_size == sizeof(uintptr_t), "Unexpected Line Number Table address_size");
        auto segment_selector_size = read<uint8_t>(data_);
        SAFE_CHECK(segment_selector_size == 0, "Segments not supported");
    }
    uint64_t header_length = readOffset(data_, is64Bit_);
    SAFE_CHECK(header_length <= data_.size(), "invalid line number VM header length");
    std::string_view header(data_.data(), header_length);
    data_ = std::string_view(header.end(), data_.end() - header.end());

    minLength_ = read<uint8_t>(header);
    if (version_ >= 4)
    { // Version 2 and 3 records don't have this
        uint8_t max_ops_per_instruction = read<uint8_t>(header);
        SAFE_CHECK(max_ops_per_instruction == 1, "VLIW not supported");
    }
    defaultIsStmt_ = read<uint8_t>(header);
    lineBase_ = read<int8_t>(header); // yes, signed
    lineRange_ = read<uint8_t>(header);
    opcodeBase_ = read<uint8_t>(header);
    SAFE_CHECK(opcodeBase_ != 0, "invalid opcode base");
    standardOpcodeLengths_ = reinterpret_cast<const uint8_t *>(header.data());
    header.remove_prefix(opcodeBase_ - 1);

    if (version_ <= 4)
    {
        // We don't want to use heap, so we don't keep an unbounded amount of state.
        // We'll just skip over include directories and file names here, and
        // we'll loop again when we actually need to retrieve one.
        std::string_view sp;
        const char * tmp = header.data();
        v4_.includeDirectoryCount = 0;
        while (!(sp = readNullTerminated(header)).empty())
        {
            ++v4_.includeDirectoryCount;
        }
        v4_.includeDirectories = {tmp, header.data()};

        tmp = header.data();
        FileName fn;
        v4_.fileNameCount = 0;
        while (readFileName(header, fn))
        {
            ++v4_.fileNameCount;
        }
        v4_.fileNames = {tmp, header.data()};
    }
    else if (version_ == 5)
    {
        v5_.directoryEntryFormatCount = read<uint8_t>(header);
        const char * tmp = header.data();
        for (uint8_t i = 0; i < v5_.directoryEntryFormatCount; i++)
        {
            // A sequence of directory entry format descriptions. Each description
            // consists of a pair of ULEB128 values:
            readULEB(header); // A content type code
            readULEB(header); // A form code using the attribute form codes
        }
        v5_.directoryEntryFormat = {tmp, header.data()};
        v5_.directoriesCount = readULEB(header);
        tmp = header.data();
        for (uint64_t i = 0; i < v5_.directoriesCount; i++)
        {
            std::string_view format = v5_.directoryEntryFormat;
            for (uint8_t f = 0; f < v5_.directoryEntryFormatCount; f++)
            {
                readLineNumberAttribute(is64Bit_, format, header, debugStr_, debugLineStr_);
            }
        }
        v5_.directories = {tmp, header.data()};

        v5_.fileNameEntryFormatCount = read<uint8_t>(header);
        tmp = header.data();
        for (uint8_t i = 0; i < v5_.fileNameEntryFormatCount; i++)
        {
            // A sequence of file entry format descriptions. Each description
            // consists of a pair of ULEB128 values:
            readULEB(header); // A content type code
            readULEB(header); // A form code using the attribute form codes
        }
        v5_.fileNameEntryFormat = {tmp, header.data()};
        v5_.fileNamesCount = readULEB(header);
        tmp = header.data();
        for (uint64_t i = 0; i < v5_.fileNamesCount; i++)
        {
            std::string_view format = v5_.fileNameEntryFormat;
            for (uint8_t f = 0; f < v5_.fileNameEntryFormatCount; f++)
            {
                readLineNumberAttribute(is64Bit_, format, header, debugStr_, debugLineStr_);
            }
        }
        v5_.fileNames = {tmp, header.data()};
    }
}

bool Dwarf::LineNumberVM::next(std::string_view & program)
{
    Dwarf::LineNumberVM::StepResult ret;
    do
    {
        ret = step(program);
    } while (ret == CONTINUE);

    return (ret == COMMIT);
}

Dwarf::LineNumberVM::FileName Dwarf::LineNumberVM::getFileName(uint64_t index) const
{
    if (version_ <= 4)
    {
        SAFE_CHECK(index != 0, "invalid file index 0");
        FileName fn;
        if (index <= v4_.fileNameCount)
        {
            std::string_view file_names = v4_.fileNames;
            for (; index; --index)
            {
                if (!readFileName(file_names, fn))
                {
                    abort();
                }
            }
            return fn;
        }

        index -= v4_.fileNameCount;

        std::string_view program = data_;
        for (; index; --index)
        {
            SAFE_CHECK(nextDefineFile(program, fn), "invalid file index");
        }

        return fn;
    }
    else
    {
        FileName fn;
        SAFE_CHECK(index < v5_.fileNamesCount, "invalid file index");
        std::string_view file_names = v5_.fileNames;
        for (uint64_t i = 0; i < v5_.fileNamesCount; i++)
        {
            std::string_view format = v5_.fileNameEntryFormat;
            for (uint8_t f = 0; f < v5_.fileNameEntryFormatCount; f++)
            {
                auto attr = readLineNumberAttribute(is64Bit_, format, file_names, debugStr_, debugLineStr_);
                if (i == index)
                {
                    switch (attr.content_type_code)
                    {
                        case DW_LNCT_path:
                            fn.relativeName = std::get<std::string_view>(attr.attr_value);
                            break;
                        case DW_LNCT_directory_index:
                            fn.directoryIndex = std::get<uint64_t>(attr.attr_value);
                            break;
                    }
                }
            }
        }
        return fn;
    }
}

std::string_view Dwarf::LineNumberVM::getIncludeDirectory(uint64_t index) const
{
    if (version_ <= 4)
    {
        if (index == 0)
        {
            // In DWARF <= 4 the current directory is not represented in the
            // directories field and a directory index of 0 implicitly referred to
            // that directory as found in the DW_AT_comp_dir attribute of the
            // compilation unit debugging information entry.
            return {};
        }

        SAFE_CHECK(index <= v4_.includeDirectoryCount, "invalid include directory");

        std::string_view include_directories = v4_.includeDirectories;
        std::string_view dir;
        for (; index; --index)
        {
            dir = readNullTerminated(include_directories);
            if (dir.empty())
            {
                abort(); // BUG
            }
        }

        return dir;
    }
    else
    {
        SAFE_CHECK(index < v5_.directoriesCount, "invalid file index");
        std::string_view directories = v5_.directories;
        for (uint64_t i = 0; i < v5_.directoriesCount; i++)
        {
            std::string_view format = v5_.directoryEntryFormat;
            for (uint8_t f = 0; f < v5_.directoryEntryFormatCount; f++)
            {
                auto attr = readLineNumberAttribute(is64Bit_, format, directories, debugStr_, debugLineStr_);
                if (i == index && attr.content_type_code == DW_LNCT_path)
                {
                    return std::get<std::string_view>(attr.attr_value);
                }
            }
        }
        // This could only happen if DWARF5's directory_entry_format doesn't contain
        // a DW_LNCT_path. Highly unlikely, but we shouldn't crash.
        return std::string_view("<directory not found>");
    }
}

bool Dwarf::LineNumberVM::readFileName(std::string_view & program, FileName & fn)
{
    fn.relativeName = readNullTerminated(program);
    if (fn.relativeName.empty())
    {
        return false;
    }
    fn.directoryIndex = readULEB(program);
    // Skip over file size and last modified time
    readULEB(program);
    readULEB(program);
    return true;
}

bool Dwarf::LineNumberVM::nextDefineFile(std::string_view & program, FileName & fn) const
{
    while (!program.empty())
    {
        auto opcode = read<uint8_t>(program);

        if (opcode >= opcodeBase_)
        { // special opcode
            continue;
        }

        if (opcode != 0)
        { // standard opcode
            // Skip, slurp the appropriate number of LEB arguments
            uint8_t arg_count = standardOpcodeLengths_[opcode - 1];
            while (arg_count--)
            {
                readULEB(program);
            }
            continue;
        }

        // Extended opcode
        auto length = readULEB(program);
        // the opcode itself should be included in the length, so length >= 1
        SAFE_CHECK(length != 0, "invalid extended opcode length");
        read<uint8_t>(program); // extended opcode
        --length;

        if (opcode == DW_LNE_define_file)
        {
            SAFE_CHECK(version_ < 5, "DW_LNE_define_file deprecated in DWARF5");
            SAFE_CHECK(readFileName(program, fn), "invalid empty file in DW_LNE_define_file");
            return true;
        }

        program.remove_prefix(length);
    }

    return false;
}

Dwarf::LineNumberVM::StepResult Dwarf::LineNumberVM::step(std::string_view & program)
{
    auto opcode = read<uint8_t>(program);

    if (opcode >= opcodeBase_)
    { // special opcode
        uint8_t adjusted_opcode = opcode - opcodeBase_;
        uint8_t op_advance = adjusted_opcode / lineRange_;

        address_ += minLength_ * op_advance;
        line_ += lineBase_ + adjusted_opcode % lineRange_;

        basicBlock_ = false;
        prologueEnd_ = false;
        epilogueBegin_ = false;
        discriminator_ = 0;
        return COMMIT;
    }

    if (opcode != 0)
    { // standard opcode
        // Only interpret opcodes that are recognized by the version we're parsing;
        // the others are vendor extensions and we should ignore them.
        switch (opcode)
        {
            case DW_LNS_copy:
                basicBlock_ = false;
                prologueEnd_ = false;
                epilogueBegin_ = false;
                discriminator_ = 0;
                return COMMIT;
            case DW_LNS_advance_pc:
                address_ += minLength_ * readULEB(program);
                return CONTINUE;
            case DW_LNS_advance_line:
                line_ += readSLEB(program);
                return CONTINUE;
            case DW_LNS_set_file:
                file_ = readULEB(program);
                return CONTINUE;
            case DW_LNS_set_column:
                column_ = readULEB(program);
                return CONTINUE;
            case DW_LNS_negate_stmt:
                isStmt_ = !isStmt_;
                return CONTINUE;
            case DW_LNS_set_basic_block:
                basicBlock_ = true;
                return CONTINUE;
            case DW_LNS_const_add_pc:
                address_ += minLength_ * ((255 - opcodeBase_) / lineRange_);
                return CONTINUE;
            case DW_LNS_fixed_advance_pc:
                address_ += read<uint16_t>(program);
                return CONTINUE;
            case DW_LNS_set_prologue_end:
                if (version_ == 2)
                {
                    break; // not supported in version 2
                }
                prologueEnd_ = true;
                return CONTINUE;
            case DW_LNS_set_epilogue_begin:
                if (version_ == 2)
                {
                    break; // not supported in version 2
                }
                epilogueBegin_ = true;
                return CONTINUE;
            case DW_LNS_set_isa:
                if (version_ == 2)
                {
                    break; // not supported in version 2
                }
                isa_ = readULEB(program);
                return CONTINUE;
        }

        // Unrecognized standard opcode, slurp the appropriate number of LEB
        // arguments.
        uint8_t arg_count = standardOpcodeLengths_[opcode - 1];
        while (arg_count--)
        {
            readULEB(program);
        }
        return CONTINUE;
    }

    // Extended opcode
    auto length = readULEB(program);
    // the opcode itself should be included in the length, so length >= 1
    SAFE_CHECK(length != 0, "invalid extended opcode length");
    auto extended_opcode = read<uint8_t>(program);
    --length;

    switch (extended_opcode)
    {
        case DW_LNE_end_sequence:
            return END;
        case DW_LNE_set_address:
            address_ = read<uintptr_t>(program);
            return CONTINUE;
        case DW_LNE_define_file:
            SAFE_CHECK(version_ < 5, "DW_LNE_define_file deprecated in DWARF5");
            // We can't process DW_LNE_define_file here, as it would require us to
            // use unbounded amounts of state (ie. use the heap).  We'll do a second
            // pass (using nextDefineFile()) if necessary.
            break;
        case DW_LNE_set_discriminator:
            discriminator_ = readULEB(program);
            return CONTINUE;
    }

    // Unrecognized extended opcode
    program.remove_prefix(length);
    return CONTINUE;
}

Dwarf::Path Dwarf::LineNumberVM::getFullFileName(uint64_t index) const
{
    auto fn = getFileName(index);
    // DWARF <= 4: the current dir is not represented in the CU's Line Number
    // Program Header and relies on the CU's DW_AT_comp_dir.
    // DWARF 5: the current directory is explicitly present.
    const std::string_view base_dir = version_ == 5 ? "" : compilationDirectory_;
    return Path(base_dir, getIncludeDirectory(fn.directoryIndex), fn.relativeName);
}

bool Dwarf::LineNumberVM::findAddress(uintptr_t target, Path & file, uint64_t & line)
{
    std::string_view program = data_;

    // Within each sequence of instructions, the address may only increase.
    // Unfortunately, within the same compilation unit, sequences may appear
    // in any order.  So any sequence is a candidate if it starts at an address
    // <= the target address, and we know we've found the target address if
    // a candidate crosses the target address.
    enum State
    {
        START,
        LOW_SEQ, // candidate
        HIGH_SEQ
    };
    State state = START;
    reset();

    uint64_t prev_file = 0;
    uint64_t prev_line = 0;
    while (!program.empty())
    {
        bool seq_end = !next(program);

        if (state == START)
        {
            if (!seq_end)
            {
                state = address_ <= target ? LOW_SEQ : HIGH_SEQ;
            }
        }

        if (state == LOW_SEQ)
        {
            if (address_ > target)
            {
                // Found it!  Note that ">" is indeed correct (not ">="), as each
                // sequence is guaranteed to have one entry past-the-end (emitted by
                // DW_LNE_end_sequence)
                //
                // NOTE: In DWARF <= 4 the file register is non-zero.
                //   See DWARF 4: 6.2.4 The Line Number Program Header
                //   "The line number program assigns numbers to each of the file
                //   entries in order, beginning with 1, and uses those numbers instead
                //   of file names in the file register."
                // DWARF 5 has a different include directory/file header and 0 is valid.
                if (version_ <= 4 && prev_file == 0)
                {
                    return false;
                }
                file = getFullFileName(prev_file);
                line = prev_line;
                return true;
            }
            prev_file = file_;
            prev_line = line_;
        }

        if (seq_end)
        {
            state = START;
            reset();
        }
    }

    return false;
}

}

#endif