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
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
|
/* Pedantic checking of DWARF files.
Copyright (C) 2008,2009 Red Hat, Inc.
This file is part of Red Hat elfutils.
Written by Petr Machata <pmachata@redhat.com>, 2008.
Red Hat elfutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 of the License.
Red Hat elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Red Hat elfutils; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Red Hat elfutils is an included package of the Open Invention Network.
An included package of the Open Invention Network is a package for which
Open Invention Network licensees cross-license their patents. No patent
license is granted, either expressly or impliedly, by designation as an
included package. Should you wish to participate in the Open Invention
Network licensing program, please visit www.openinventionnetwork.com
<http://www.openinventionnetwork.com>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <argp.h>
#include <assert.h>
#include <error.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <libintl.h>
#include <locale.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <system.h>
#include <unistd.h>
#include "../libdw/dwarf.h"
#include "../libdw/libdwP.h"
#include "../libdw/known-dwarf.h"
#include "../libebl/libebl.h"
#include "dwarfstrings.h"
#include "dwarflint.h"
/* Bug report address. */
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
#define ARGP_strict 300
#define ARGP_gnu 301
#define ARGP_tolerant 302
#define ARGP_ref 303
#define ARGP_nohl 304
#undef FIND_SECTION_HOLES
/* Definitions of arguments for argp functions. */
static const struct argp_option options[] =
{
{ "strict", ARGP_strict, NULL, 0,
N_("Be extremely strict, flag level 2 features."), 0 },
{ "quiet", 'q', NULL, 0, N_("Do not print anything if successful"), 0 },
{ "ignore-missing", 'i', NULL, 0,
N_("Don't complain if files have no DWARF at all"), 0 },
{ "gnu", ARGP_gnu, NULL, 0,
N_("Binary has been created with GNU toolchain and is therefore known to be \
broken in certain ways"), 0 },
{ "tolerant", ARGP_tolerant, NULL, 0,
N_("Don't output certain common error messages"), 0 },
{ "ref", ARGP_ref, NULL, 0,
N_("When validating .debug_loc and .debug_ranges, display information about \
the DIE referring to the entry in consideration"), 0 },
{ "nohl", ARGP_nohl, NULL, 0,
N_("Don't run high-level tests"), 0 },
{ "verbose", 'v', NULL, 0,
N_("Be verbose"), 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
/* Short description of program. */
static const char doc[] = N_("\
Pedantic checking of DWARF stored in ELF files.");
/* Strings for arguments in help texts. */
static const char args_doc[] = N_("FILE...");
/* Prototype for option handler. */
static error_t parse_opt (int key, char *arg, struct argp_state *state);
/* Data structure to communicate with argp functions. */
static struct argp argp =
{
options, parse_opt, args_doc, doc, NULL, NULL, NULL
};
/* If true, we accept silently files without debuginfo. */
static bool tolerate_nodebug = false;
static void process_file (Dwarf *dwarf, const char *fname, bool only_one);
struct message_term
{
/* Given a term like A && !B && C && !D, we decompose it thus: */
enum message_category positive; /* non-zero bits for plain predicates */
enum message_category negative; /* non-zero bits for negated predicates */
};
struct message_criteria
{
struct message_term *terms;
size_t size;
size_t alloc;
};
static bool
message_accept (struct message_criteria *cri, enum message_category cat)
{
for (size_t i = 0; i < cri->size; ++i)
{
struct message_term *t = cri->terms + i;
if ((t->positive & cat) == t->positive
&& (t->negative & cat) == 0)
return true;
}
return false;
}
static const char *
message_term_str (struct message_term *t)
{
static char *names[] = {
#define MC(CAT, ID) [ID] = #CAT,
MESSAGE_CATEGORIES
#undef MC
};
unsigned max = 0;
#define MC(CAT, ID) max = ID;
MESSAGE_CATEGORIES
#undef MC
static char buf[512];
char *ptr = buf;
ptr = stpcpy (ptr, "(");
bool got = false;
for (unsigned i = 0; i <= max; ++i)
{
unsigned mask = 1u << i;
if ((t->positive & mask) != 0
|| (t->negative & mask) != 0)
{
if (got)
ptr = stpcpy (ptr, " & ");
if ((t->negative & (1u << i)) != 0)
ptr = stpcpy (ptr, "~");
ptr = stpcpy (ptr, names[i]);
got = true;
}
}
if (ptr == buf + 1)
ptr = stpcpy (ptr, "1");
ptr = stpcpy (ptr, ")");
return buf;
}
static const char *
message_cri_str (struct message_criteria *cri)
{
static char buf[512];
char *ptr = buf;
*ptr = 0;
for (size_t i = 0; i < cri->size; ++i)
{
struct message_term *t = cri->terms + i;
if (i > 0)
ptr = stpcpy (ptr, " | ");
ptr = stpcpy (ptr, message_term_str (t));
}
return buf;
}
static void
message_cri_and (struct message_criteria *cri, struct message_term *term)
{
assert ((term->positive & term->negative) == 0);
for (size_t i = 0; i < cri->size; )
{
struct message_term *t = cri->terms + i;
t->positive |= term->positive;
t->negative |= term->negative;
if ((t->positive & t->negative) != 0)
/* A ^ ~A -> drop the term. */
cri->terms[i] = cri->terms[--cri->size];
else
++i;
}
}
static void
message_cri_or (struct message_criteria *cri, struct message_term *term)
{
assert ((term->positive & term->negative) == 0);
REALLOC (cri, terms);
cri->terms[cri->size++] = *term;
}
/* NEG(a&b&~c) -> (~a + ~b + c) */
static struct message_criteria
message_cri_neg (struct message_term *term)
{
assert ((term->positive & term->negative) == 0);
unsigned max = 0;
#define MC(CAT, ID) max = ID;
MESSAGE_CATEGORIES
#undef MC
struct message_criteria ret;
WIPE (ret);
for (size_t i = 0; i < max; ++i)
{
unsigned mask = 1u << i;
if ((term->positive & mask) != 0)
message_cri_or (&ret, &(struct message_term){1u << i, mc_none});
else if ((term->negative & mask) != 0)
message_cri_or (&ret, &(struct message_term){mc_none, 1u << i});
}
return ret;
}
/* MUL((a&b + c&d), (e&f + g&h)) -> (a&b&e&f + a&b&g&h + c&d&e&f + c&d&g&h) */
static void
message_cri_mul (struct message_criteria *cri, struct message_criteria *rhs)
{
struct message_criteria ret;
WIPE (ret);
for (size_t i = 0; i < cri->size; ++i)
for (size_t j = 0; j < rhs->size; ++j)
{
struct message_term t1 = cri->terms[i];
struct message_term *t2 = rhs->terms + j;
t1.positive |= t2->positive;
t1.negative |= t2->negative;
if (t1.positive & t1.negative)
/* A ^ ~A -> drop the term. */
continue;
message_cri_or (&ret, &t1);
}
free (cri->terms);
*cri = ret;
}
/* Reject message if TERM passes. */
static void
message_cri_and_not (struct message_criteria *cri, struct message_term *term)
{
struct message_criteria tmp
= message_cri_neg (&(struct message_term) {term->negative, term->positive});
message_cri_mul (cri, &tmp);
free (tmp.terms);
}
/* Messages that are accepted (and made into warning). */
static struct message_criteria warning_criteria;
/* Accepted (warning) messages, that are turned into errors. */
static struct message_criteria error_criteria;
static unsigned error_count = 0;
static bool
check_category (enum message_category cat)
{
return message_accept (&warning_criteria, cat);
}
static void
wr_verror (const struct where *wh, const char *format, va_list ap)
{
printf ("error: %s", where_fmt (wh, NULL));
vprintf (format, ap);
where_fmt_chain (wh, "error");
++error_count;
}
static void
wr_vwarning (const struct where *wh, const char *format, va_list ap)
{
printf ("warning: %s", where_fmt (wh, NULL));
vprintf (format, ap);
where_fmt_chain (wh, "warning");
++error_count;
}
void
wr_error (const struct where *wh, const char *format, ...)
{
va_list ap;
va_start (ap, format);
wr_verror (wh, format, ap);
va_end (ap);
}
void
wr_warning (const struct where *wh, const char *format, ...)
{
va_list ap;
va_start (ap, format);
wr_vwarning (wh, format, ap);
va_end (ap);
}
void
wr_message (enum message_category category, const struct where *wh,
const char *format, ...)
{
va_list ap;
va_start (ap, format);
if (message_accept (&warning_criteria, category))
{
if (message_accept (&error_criteria, category))
wr_verror (wh, format, ap);
else
wr_vwarning (wh, format, ap);
}
va_end (ap);
}
void
wr_format_padding_message (enum message_category category,
struct where *wh,
uint64_t start, uint64_t end, char *kind)
{
wr_message (category, wh,
": 0x%" PRIx64 "..0x%" PRIx64 ": %s.\n", start, end, kind);
}
void
wr_format_leb128_message (struct where *where, const char *what,
const char *purpose,
const unsigned char *begin, const unsigned char *end)
{
enum message_category category = mc_leb128 | mc_acc_bloat | mc_impact_3;
char buf[(end - begin) * 3 + 1]; // 2 hexa digits+" " per byte, and term. 0
char *ptr = buf;
for (; begin < end; ++begin)
ptr += sprintf (ptr, " %02x", *begin);
wr_message (category, where,
": %s: value %s encoded as `%s'.\n",
what, purpose, buf + 1);
}
void
wr_message_padding_0 (enum message_category category,
struct where *wh,
uint64_t start, uint64_t end)
{
wr_format_padding_message (category | mc_acc_bloat | mc_impact_1,
wh, start, end,
"unnecessary padding with zero bytes");
}
void
wr_message_padding_n0 (enum message_category category,
struct where *wh,
uint64_t start, uint64_t end)
{
wr_format_padding_message (category | mc_acc_bloat | mc_impact_1,
wh, start, end,
"unreferenced non-zero bytes");
}
/* True if no message is to be printed if the run is succesful. */
static bool be_quiet = false; /* -q */
static bool be_verbose = false; /* -v */
static bool be_strict = false; /* --strict */
static bool be_gnu = false; /* --gnu */
static bool be_tolerant = false; /* --tolerant */
static bool show_refs = false; /* --ref */
static bool do_high_level = true; /* ! --nohl */
static int
layout_rel_file (Elf *elf)
{
GElf_Ehdr ehdr;
if (gelf_getehdr (elf, &ehdr) == NULL)
return 1;
if (ehdr.e_type != ET_REL)
return 0;
/* Taken from libdwfl. */
GElf_Addr base = 0;
GElf_Addr start = 0, end = 0, bias = 0;
bool first = true;
Elf_Scn *scn = NULL;
while ((scn = elf_nextscn (elf, scn)) != NULL)
{
GElf_Shdr shdr_mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
if (unlikely (shdr == NULL))
return 1;
if (shdr->sh_flags & SHF_ALLOC)
{
const GElf_Xword align = shdr->sh_addralign ?: 1;
const GElf_Addr next = (end + align - 1) & -align;
if (shdr->sh_addr == 0
/* Once we've started doing layout we have to do it all,
unless we just layed out the first section at 0 when
it already was at 0. */
|| (bias == 0 && end > start && end != next))
{
shdr->sh_addr = next;
if (end == base)
/* This is the first section assigned a location.
Use its aligned address as the module's base. */
start = base = shdr->sh_addr;
else if (unlikely (base & (align - 1)))
{
/* If BASE has less than the maximum alignment of
any section, we eat more than the optimal amount
of padding and so make the module's apparent
size come out larger than it would when placed
at zero. So reset the layout with a better base. */
start = end = base = (base + align - 1) & -align;
Elf_Scn *prev_scn = NULL;
do
{
prev_scn = elf_nextscn (elf, prev_scn);
GElf_Shdr prev_shdr_mem;
GElf_Shdr *prev_shdr = gelf_getshdr (prev_scn,
&prev_shdr_mem);
if (unlikely (prev_shdr == NULL))
return 1;
if (prev_shdr->sh_flags & SHF_ALLOC)
{
const GElf_Xword prev_align
= prev_shdr->sh_addralign ?: 1;
prev_shdr->sh_addr
= (end + prev_align - 1) & -prev_align;
end = prev_shdr->sh_addr + prev_shdr->sh_size;
if (unlikely (! gelf_update_shdr (prev_scn,
prev_shdr)))
return 1;
}
}
while (prev_scn != scn);
continue;
}
end = shdr->sh_addr + shdr->sh_size;
if (likely (shdr->sh_addr != 0)
&& unlikely (! gelf_update_shdr (scn, shdr)))
return 1;
}
else
{
/* The address is already assigned. Just track it. */
if (first || end < shdr->sh_addr + shdr->sh_size)
end = shdr->sh_addr + shdr->sh_size;
if (first || bias > shdr->sh_addr)
/* This is the lowest address in the module. */
bias = shdr->sh_addr;
if ((shdr->sh_addr - bias + base) & (align - 1))
/* This section winds up misaligned using BASE.
Adjust BASE upwards to make it congruent to
the lowest section address in the file modulo ALIGN. */
base = (((base + align - 1) & -align)
+ (bias & (align - 1)));
}
first = false;
}
}
return 0;
}
int
main (int argc, char *argv[])
{
/* Set locale. */
setlocale (LC_ALL, "");
/* Initialize the message catalog. */
textdomain (PACKAGE_TARNAME);
/* Parse and process arguments. */
int remaining;
argp_parse (&argp, argc, argv, 0, &remaining, NULL);
/* Initialize warning & error criteria. */
message_cri_or (&warning_criteria,
&(struct message_term){mc_none, mc_none});
message_cri_or (&error_criteria,
&(struct message_term){mc_impact_4, mc_none});
message_cri_or (&error_criteria,
&(struct message_term){mc_error, mc_none});
/* Configure warning & error criteria according to configuration. */
if (tolerate_nodebug)
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_elf});
if (be_gnu)
{
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_acc_bloat});
}
if (!be_strict)
{
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_strings});
message_cri_and_not (&warning_criteria,
&(struct message_term)
{mc_line | mc_header | mc_acc_bloat, mc_none});
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_pubtypes});
}
if (be_tolerant)
{
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_loc});
message_cri_and (&warning_criteria,
&(struct message_term){mc_none, mc_ranges});
}
if (be_verbose)
{
printf ("warning criteria: %s\n", message_cri_str (&warning_criteria));
printf ("error criteria: %s\n", message_cri_str (&error_criteria));
}
/* Before we start tell the ELF library which version we are using. */
elf_version (EV_CURRENT);
/* Now process all the files given at the command line. */
bool only_one = remaining + 1 == argc;
do
{
/* Open the file. */
int fd = open (argv[remaining], O_RDONLY);
if (fd == -1)
{
error (0, errno, gettext ("cannot open input file"));
continue;
}
/* Create an `Elf' descriptor. */
Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
if (elf == NULL)
invalid_elf:
wr_error (NULL,
gettext ("Error processing ELF file: %s\n"),
elf_errmsg (-1));
else
{
unsigned int prev_error_count = error_count;
if (layout_rel_file (elf))
goto invalid_elf;
Dwarf *dwarf = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
if (dwarf == NULL)
{
if (!tolerate_nodebug)
wr_error (NULL,
gettext ("cannot generate Dwarf descriptor: %s\n"),
dwarf_errmsg (-1));
}
else
{
process_file (dwarf, argv[remaining], only_one);
if (dwarf_end (dwarf) != 0)
wr_error (NULL,
gettext ("error while closing Dwarf descriptor: %s\n"),
dwarf_errmsg (-1));
}
if (elf_end (elf) != 0)
wr_error (NULL,
gettext ("error while closing Elf descriptor: %s\n"),
elf_errmsg (-1));
if (prev_error_count == error_count && !be_quiet)
puts (gettext ("No errors"));
}
close (fd);
}
while (++remaining < argc);
return error_count != 0;
}
/* Handle program arguments. */
static error_t
parse_opt (int key, char *arg __attribute__ ((unused)),
struct argp_state *state __attribute__ ((unused)))
{
switch (key)
{
case ARGP_strict:
be_strict = true;
break;
case ARGP_gnu:
be_gnu = true;
break;
case ARGP_tolerant:
be_tolerant = true;
break;
case ARGP_ref:
show_refs = true;
break;
case ARGP_nohl:
do_high_level = false;
break;
case 'i':
tolerate_nodebug = true;
break;
case 'q':
be_quiet = true;
be_verbose = false;
break;
case 'v':
be_quiet = false;
be_verbose = true;
break;
case ARGP_KEY_NO_ARGS:
fputs (gettext ("Missing file name.\n"), stderr);
argp_help (&argp, stderr, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
program_invocation_short_name);
exit (1);
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
#define PRI_CU "CU 0x%" PRIx64
#define PRI_DIE "DIE 0x%" PRIx64
#define PRI_NOT_ENOUGH ": not enough data for %s.\n"
#define PRI_LACK_RELOCATION ": %s seems to lack a relocation.\n"
struct relocation
{
uint64_t offset;
uint64_t addend;
int symndx;
int type;
bool invalid; /* Whether this one relocation should be
ignored. Necessary so that we don't
double-report invalid & missing
relocation. */
};
struct relocation_data
{
struct elf_file *file;
Elf_Data *symdata; /* Symbol table associated with this
relocation section. */
size_t type; /* SHT_REL or SHT_RELA. */
struct relocation *rel; /* Array of relocations. May be NULL
if there are no associated
relocation data. */
size_t size;
size_t alloc;
size_t index; /* Current index. */
};
struct section_data
{
struct elf_file *file;
size_t secndx; /* Index into file->sec. */
Elf_Data *data;
struct relocation_data rel;
};
static struct sec *
data_get_sec (struct section_data *data)
{
return data->file->sec + data->secndx;
}
/* Functions and data structures related to bounds-checked
reading. */
struct read_ctx
{
Dwarf *dbg;
Elf_Data *data;
const unsigned char *ptr;
const unsigned char *begin;
const unsigned char *end;
};
static void read_ctx_init (struct read_ctx *ctx, Dwarf *dbg,
Elf_Data *data);
static bool read_ctx_init_sub (struct read_ctx *ctx,
struct read_ctx *parent,
const unsigned char *begin,
const unsigned char *end);
static uint64_t read_ctx_get_offset (struct read_ctx *ctx);
static bool read_ctx_need_data (struct read_ctx *ctx, size_t length);
static bool read_ctx_read_ubyte (struct read_ctx *ctx, unsigned char *ret);
static int read_ctx_read_uleb128 (struct read_ctx *ctx, uint64_t *ret);
static int read_ctx_read_sleb128 (struct read_ctx *ctx, int64_t *ret);
static bool read_ctx_read_2ubyte (struct read_ctx *ctx, uint16_t *ret);
static bool read_ctx_read_4ubyte (struct read_ctx *ctx, uint32_t *ret);
static bool read_ctx_read_8ubyte (struct read_ctx *ctx, uint64_t *ret);
static bool read_ctx_read_offset (struct read_ctx *ctx, bool dwarf64,
uint64_t *ret);
static bool read_ctx_read_var (struct read_ctx *ctx, int width, uint64_t *ret);
static const char *read_ctx_read_str (struct read_ctx *ctx);
static bool read_ctx_skip (struct read_ctx *ctx, uint64_t len);
static bool read_ctx_eof (struct read_ctx *ctx);
/* Functions and data structures related to raw (i.e. unassisted by
libdw) Dwarf abbreviation handling. */
struct abbrev
{
uint64_t code;
/* While ULEB128 can hold numbers > 32bit, these are not legal
values of many enum types. So just use as large type as
necessary to cover valid values. */
uint16_t tag;
bool has_children;
struct where where;
/* Whether some DIE uses this abbrev. */
bool used;
/* Attributes. */
struct abbrev_attrib
{
uint16_t name;
uint8_t form;
struct where where;
} *attribs;
size_t size;
size_t alloc;
};
struct abbrev_table
{
uint64_t offset;
struct abbrev *abbr;
size_t size;
size_t alloc;
struct abbrev_table *next;
};
static struct abbrev_table *abbrev_table_load (struct read_ctx *ctx);
static void abbrev_table_free (struct abbrev_table *abbr);
static struct abbrev *abbrev_table_find_abbrev (struct abbrev_table *abbrevs,
uint64_t abbrev_code);
/* Functions and data structures for address record handling. We use
that to check that all DIE references actually point to an existing
die, not somewhere mid-DIE, where it just happens to be
interpretable as a DIE. */
struct addr_record
{
size_t size;
size_t alloc;
uint64_t *addrs;
};
static size_t addr_record_find_addr (struct addr_record *ar, uint64_t addr);
static bool addr_record_has_addr (struct addr_record *ar, uint64_t addr);
static void addr_record_add (struct addr_record *ar, uint64_t addr);
static void addr_record_free (struct addr_record *ar);
/* Functions and data structures for reference handling. Just like
the above, we use this to check validity of DIE references. Unlike
the above, this is not stored as sorted set, but simply as an array
of records, because duplicates are unlikely. */
struct ref
{
uint64_t addr; // Referree address
struct where who; // Referrer
};
struct ref_record
{
size_t size;
size_t alloc;
struct ref *refs;
};
static void ref_record_add (struct ref_record *rr, uint64_t addr, struct where *referrer);
static void ref_record_free (struct ref_record *rr);
/* Functions and data structures for CU handling. */
struct cu
{
uint64_t offset;
uint64_t cudie_offset;
uint64_t length;
int address_size; // Address size in bytes on the target machine.
uint64_t base; // DW_AT_low_pc value of CU DIE, 0 if not present.
struct addr_record die_addrs; // Addresses where DIEs begin in this CU.
struct ref_record die_refs; // DIE references into other CUs from this CU.
struct ref_record loc_refs; // references into .debug_loc from this CU.
struct ref_record range_refs; // references into .debug_ranges from this CU.
struct ref_record line_refs; // references into .debug_line from this CU.
struct where where; // Where was this section defined.
bool has_arange; // Whether we saw arange section pointing to this CU.
bool has_pubnames; // Likewise for pubnames.
bool has_pubtypes; // Likewise for pubtypes.
struct cu *next;
};
static void cu_free (struct cu *cu_chain);
static struct cu *cu_find_cu (struct cu *cu_chain, uint64_t offset);
/* Functions for checking of structural integrity. */
static struct cu * check_info_structural (struct section_data *data,
struct abbrev_table *abbrev_chain,
Elf_Data *strings);
static bool check_aranges_structural (struct section_data *data,
struct cu *cu_chain);
static bool check_pub_structural (struct section_data *data,
struct cu *cu_chain);
static bool check_location_expression (struct read_ctx *ctx,
uint64_t init_off,
struct relocation_data *reloc,
size_t length,
struct where *wh,
bool addr_64);
static bool check_loc_or_range_structural (struct section_data *data,
struct cu *cu_chain);
static bool read_rel (struct section_data *secdata,
Elf_Data *reldata,
bool elf_64);
static bool check_line_structural (struct section_data *data,
struct cu *cu_chain);
const char *
where_fmt (const struct where *wh, char *ptr)
{
if (wh == NULL)
return "";
static char buf[256];
struct section_info
{
const char *name;
const char *addr1n;
const char *addr1f;
const char *addr2n;
const char *addr2f;
const char *addr3n;
const char *addr3f;
};
static struct section_info section_names[] =
{
[sec_info] = {".debug_info", "CU", "%"PRId64,
"DIE", "%#"PRIx64, NULL, NULL},
[sec_abbrev] = {".debug_abbrev", "section", "%"PRId64,
"abbreviation", "%"PRId64, "abbr. attribute", "%#"PRIx64},
[sec_aranges] = {".debug_aranges", "table", "%"PRId64,
"arange", "%#"PRIx64, NULL, NULL},
[sec_pubnames] = {".debug_pubnames", "pubname table", "%"PRId64,
"pubname", "%#"PRIx64, NULL, NULL},
[sec_pubtypes] = {".debug_pubtypes", "pubtype table", "%"PRId64,
"pubtype", "%#"PRIx64, NULL, NULL},
[sec_str] = {".debug_str", "offset", "%#"PRIx64,
NULL, NULL, NULL, NULL},
[sec_line] = {".debug_line", "table", "%"PRId64,
"offset", "%#"PRIx64, NULL, NULL},
[sec_loc] = {".debug_loc", "loclist", "%#"PRIx64,
"offset", "%#"PRIx64, NULL, NULL},
[sec_mac] = {".debug_mac", NULL, NULL, NULL, NULL, NULL, NULL},
[sec_ranges] = {".debug_ranges", "rangelist", "%#"PRIx64,
"offset", "%#"PRIx64, NULL, NULL},
[sec_locexpr] = {"location expression", "offset", "%#"PRIx64,
NULL, NULL, NULL, NULL},
[sec_rel] = {".rel", "relocation", "%"PRId64,
"offset", "%#"PRIx64, NULL, NULL},
[sec_rela] = {".rela", "relocation", "%"PRId64,
"offset", "%#"PRIx64, NULL, NULL},
};
static struct section_info special_formats[] =
{
[wf_cudie] = {".debug_info", "CU DIE", "%"PRId64, NULL, NULL, NULL, NULL}
};
assert (wh->section < sizeof (section_names) / sizeof (*section_names));
struct section_info *inf
= (wh->formatting == wf_plain)
? section_names + wh->section
: special_formats + wh->formatting;
assert (inf->name);
assert ((inf->addr1n == NULL) == (inf->addr1f == NULL));
assert ((inf->addr2n == NULL) == (inf->addr2f == NULL));
assert ((inf->addr3n == NULL) == (inf->addr3f == NULL));
assert ((wh->addr1 != (uint64_t)-1) ? inf->addr1n != NULL : true);
assert ((wh->addr2 != (uint64_t)-1) ? inf->addr2n != NULL : true);
assert ((wh->addr3 != (uint64_t)-1) ? inf->addr3n != NULL : true);
assert ((wh->addr3 != (uint64_t)-1) ? (wh->addr2 != (uint64_t)-1) : true);
assert ((wh->addr2 != (uint64_t)-1) ? (wh->addr1 != (uint64_t)-1) : true);
/* GCC insists on checking format parameters and emits a warning
when we don't use string literal. With -Werror this ends up
being hard error. So instead we walk around this warning by
using function pointer. */
int (*x_asprintf)(char **strp, const char *fmt, ...) = asprintf;
#define SETUP_ADDR(N) \
char *addr##N##s; \
if (wh->addr##N == (uint64_t)-1) \
addr##N##s = NULL; \
else if (x_asprintf (&addr##N##s, inf->addr##N##f, wh->addr##N) < 0) \
addr##N##s = "(fmt error)"
SETUP_ADDR (1);
SETUP_ADDR (2);
SETUP_ADDR (3);
#undef SETUP_ADDR
char *orig = ptr;
bool is_reloc = wh->section == sec_rel || wh->section == sec_rela;
if (ptr == NULL)
{
ptr = stpcpy (buf, inf->name);
if (is_reloc)
{
struct where *ref = wh->ref;
assert (ref != NULL);
if (ref->section == sec_locexpr)
{
ref = ref->next;
assert (ref != NULL);
assert (ref->section != sec_locexpr);
}
ptr = stpcpy (ptr, section_names[ref->section].name);
}
if (addr1s != NULL)
ptr = stpcpy (ptr, ": ");
}
if (addr3s != NULL)
ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr3n), " "), addr3s);
else if (addr2s != NULL)
ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr2n), " "), addr2s);
else if (addr1s != NULL)
ptr = stpcpy (stpcpy (stpcpy (ptr, inf->addr1n), " "), addr1s);
if (wh->ref != NULL && !is_reloc)
{
ptr = stpcpy (ptr, " (");
ptr = (char *)where_fmt (wh->ref, ptr);
*ptr++ = ')';
*ptr = 0;
}
if (orig == NULL)
return buf;
else
return ptr;
}
void
where_fmt_chain (const struct where *wh, const char *severity)
{
if (wh != NULL && show_refs)
for (struct where *it = wh->next; it != NULL; it = it->next)
printf ("%s: %s: caused by this reference.\n",
severity, where_fmt (it, NULL));
}
void
where_reset_1 (struct where *wh, uint64_t addr)
{
wh->addr1 = addr;
wh->addr2 = wh->addr3 = (uint64_t)-1;
}
void
where_reset_2 (struct where *wh, uint64_t addr)
{
wh->addr2 = addr;
wh->addr3 = (uint64_t)-1;
}
void
where_reset_3 (struct where *wh, uint64_t addr)
{
wh->addr3 = addr;
}
#define WHERE_SECDATA(DATA, PARENT) (WHERE (data_get_sec (DATA)->id, PARENT))
static void
process_file (Dwarf *dwarf, const char *fname, bool only_one)
{
if (!only_one)
printf ("\n%s:\n", fname);
struct elf_file file;
WIPE (file);
file.dwarf = dwarf;
file.ebl = ebl_openbackend (dwarf->elf);
if (file.ebl == NULL)
goto invalid_elf;
if (gelf_getehdr (dwarf->elf, &file.ehdr) == NULL)
goto invalid_elf;
file.addr_64 = file.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
#define DEF_SECDATA(VAR, SEC) \
struct section_data VAR = {&file, (size_t)-1, NULL, \
{&file, NULL, SHT_NULL, NULL, 0, 0, 0}}
DEF_SECDATA (abbrev_data, sec_abbrev);
DEF_SECDATA (aranges_data, sec_aranges);
DEF_SECDATA (info_data, sec_info);
DEF_SECDATA (line_data, sec_line);
DEF_SECDATA (loc_data, sec_loc);
DEF_SECDATA (pubnames_data, sec_pubnames);
DEF_SECDATA (pubtypes_data, sec_pubtypes);
DEF_SECDATA (ranges_data, sec_ranges);
DEF_SECDATA (str_data, sec_str);
#undef DEF_SECDATA
struct secinfo
{
const char *name;
struct section_data *dataptr;
Elf_Data *reldata;
enum section_id sec;
};
struct secinfo secinfo[] = {
#define DEF_SECINFO(SEC) {".debug_" #SEC, &SEC##_data, NULL, sec_##SEC}
#define DEF_NOINFO(SEC) {".debug_" #SEC, NULL, NULL, sec_##SEC}
DEF_SECINFO (abbrev),
DEF_SECINFO (aranges),
DEF_SECINFO (info),
DEF_SECINFO (line),
DEF_SECINFO (loc),
DEF_SECINFO (pubnames),
DEF_SECINFO (pubtypes),
DEF_SECINFO (ranges),
DEF_SECINFO (str),
#undef DEF_NOINFO
#undef DEF_SECINFO
};
Elf_Scn *reloc_symtab = NULL;
struct secinfo *find_secentry (const char *secname)
{
for (size_t i = 0; i < sizeof (secinfo) / sizeof (*secinfo); ++i)
if (strcmp (secinfo[i].name, secname) == 0)
return secinfo + i;
return NULL;
}
struct section_data *find_secdata (const char *secname)
{
struct secinfo *info = find_secentry (secname);
if (info != NULL)
return info->dataptr;
else
return NULL;
}
/* Now find all necessary debuginfo sections and associated
relocation sections. */
Elf_Scn *scn = NULL;
/* Section 0 is special, skip it. */
REALLOC (&file, sec);
file.sec[file.size++].id = sec_invalid;
while ((scn = elf_nextscn (dwarf->elf, scn)) != NULL)
{
REALLOC (&file, sec);
size_t curndx = file.size++;
struct sec *cursec = file.sec + curndx;
GElf_Shdr *shdr = gelf_getshdr (scn, &cursec->shdr);
if (shdr == NULL)
{
invalid_elf:
/* A "can't happen". libdw already managed to parse the Elf
file when constructing the Dwarf object. */
wr_error (NULL, "Broken ELF.\n");
goto skip_rel;
}
const char *scnname = elf_strptr (dwarf->elf, file.ehdr.e_shstrndx,
shdr->sh_name);
if (scnname == NULL)
goto invalid_elf;
struct secinfo *secentry = find_secentry (scnname);
struct section_data *secdata = secentry != NULL ? secentry->dataptr : NULL;
cursec->scn = scn;
cursec->id = secentry != NULL ? secentry->sec : sec_invalid;
cursec->name = scnname;
if (secdata != NULL)
{
if (secdata->secndx == (size_t)-1)
{
secdata->data = elf_getdata (scn, NULL);
if (secdata->data == NULL || secdata->data->d_buf == NULL)
wr_error (NULL, "Data-less section %s.\n", scnname);
secdata->secndx = curndx;
}
else
wr_error (NULL, "Multiple occurrences of section %s.\n", scnname);
}
else if (shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL)
{
/* Get data of section that this REL(A) section relocates. */
Elf_Scn *relocated_scn = elf_getscn (dwarf->elf, shdr->sh_info);
Elf_Scn *symtab_scn = elf_getscn (dwarf->elf, shdr->sh_link);
if (relocated_scn == NULL || symtab_scn == NULL)
goto invalid_elf;
GElf_Shdr relocated_shdr_mem;
GElf_Shdr *relocated_shdr = gelf_getshdr (relocated_scn,
&relocated_shdr_mem);
if (relocated_shdr == NULL)
goto invalid_elf;
const char *relocated_scnname
= elf_strptr (dwarf->elf, file.ehdr.e_shstrndx,
relocated_shdr->sh_name);
struct secinfo *relocated
= find_secentry (relocated_scnname);
if (relocated != NULL && relocated->dataptr != NULL)
{
if (relocated->reldata != NULL)
wr_error (NULL,
"Several relocation sections for debug section %s."
" Ignoring %s.\n",
relocated_scnname, scnname);
else
{
relocated->reldata = elf_getdata (scn, NULL);
if (relocated->reldata == NULL)
wr_error (NULL,
"Data-less relocation section %s.\n", scnname);
relocated->dataptr->rel.type = shdr->sh_type;
}
if (reloc_symtab == NULL)
reloc_symtab = symtab_scn;
else if (reloc_symtab != symtab_scn)
wr_error (NULL,
"Relocation sections use multiple symbol tables.\n");
}
}
}
Elf_Data *reloc_symdata = NULL;
if (reloc_symtab != NULL)
{
reloc_symdata = elf_getdata (reloc_symtab, NULL);
if (reloc_symdata == NULL)
{
wr_error (NULL,
"Couldn't obtain symtab data.\n");
/* But carry on, we can check a lot of stuff even without
symbol table. */
}
}
/* Check relocation sections that we've got. */
for (size_t i = 0; i < sizeof (secinfo) / sizeof (*secinfo); ++i)
{
struct secinfo *cur = secinfo + i;
if (cur->dataptr != NULL
&& cur->reldata != NULL)
{
if (cur->dataptr->data == NULL)
wr_error (&WHERE_SECDATA (secinfo[i].dataptr, NULL),
": this data-less section has a relocation section.\n");
else if (read_rel (cur->dataptr, cur->reldata, file.addr_64))
cur->dataptr->rel.symdata = reloc_symdata;
}
}
if (str_data.rel.size > 0)
wr_message (mc_impact_2 | mc_elf, &WHERE (sec_str, NULL),
": there's a relocation section associated with this section.\n");
skip_rel:;
struct abbrev_table *abbrev_chain = NULL;
struct cu *cu_chain = NULL;
struct read_ctx ctx;
struct hl_ctx *hlctx = hl_ctx_new (dwarf);
/* If we got Dwarf pointer, .debug_abbrev and .debug_info are
present inside the file. But let's be paranoid. */
if (likely (abbrev_data.data != NULL))
{
read_ctx_init (&ctx, dwarf, abbrev_data.data);
abbrev_chain = abbrev_table_load (&ctx);
}
else if (!tolerate_nodebug)
/* Hard error, not a message. We can't debug without this. */
wr_error (NULL, ".debug_abbrev data not found.\n");
if (abbrev_chain != NULL)
{
if (info_data.data != NULL)
{
cu_chain = check_info_structural (&info_data, abbrev_chain,
str_data.data);
if (cu_chain != NULL && do_high_level)
check_expected_trees (hlctx);
}
else if (!tolerate_nodebug)
/* Hard error, not a message. We can't debug without this. */
wr_error (NULL, ".debug_info data not found.\n");
}
bool ranges_sound;
if (ranges_data.data != NULL && cu_chain != NULL)
ranges_sound = check_loc_or_range_structural (&ranges_data, cu_chain);
else
ranges_sound = false;
if (loc_data.data != NULL && cu_chain != NULL)
check_loc_or_range_structural (&loc_data, cu_chain);
if (aranges_data.data != NULL)
{
read_ctx_init (&ctx, dwarf, aranges_data.data);
if (check_aranges_structural (&aranges_data, cu_chain)
&& ranges_sound && do_high_level)
check_matching_ranges (hlctx);
}
if (pubnames_data.data != NULL)
check_pub_structural (&pubnames_data, cu_chain);
else
wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf,
&WHERE (sec_pubnames, NULL), ": data not found.\n");
if (pubtypes_data.data != NULL)
check_pub_structural (&pubtypes_data, cu_chain);
else
wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf | mc_pubtypes,
&WHERE (sec_pubtypes, NULL), ": data not found.\n");
if (line_data.data != NULL)
check_line_structural (&line_data, cu_chain);
else
wr_message (mc_impact_4 | mc_acc_suboptimal | mc_elf | mc_loc,
&WHERE (sec_line, NULL), ": data not found.\n");
cu_free (cu_chain);
abbrev_table_free (abbrev_chain);
if (file.ebl != NULL)
ebl_closebackend (file.ebl);
free (file.sec);
hl_ctx_delete (hlctx);
}
static void
read_ctx_init (struct read_ctx *ctx, Dwarf *dbg, Elf_Data *data)
{
if (data == NULL)
abort ();
ctx->dbg = dbg;
ctx->data = data;
ctx->begin = data->d_buf;
ctx->end = data->d_buf + data->d_size;
ctx->ptr = data->d_buf;
}
static bool
read_ctx_init_sub (struct read_ctx *ctx, struct read_ctx *parent,
const unsigned char *begin, const unsigned char *end)
{
if (parent == NULL)
abort ();
if (begin < parent->begin
|| end > parent->end)
return false;
ctx->dbg = parent->dbg;
ctx->data = parent->data;
ctx->begin = begin;
ctx->end = end;
ctx->ptr = begin;
return true;
}
static uint64_t
read_ctx_get_offset (struct read_ctx *ctx)
{
assert (ctx->ptr >= ctx->begin);
return (uint64_t)(ctx->ptr - ctx->begin);
}
static bool
read_ctx_need_data (struct read_ctx *ctx, size_t length)
{
const unsigned char *ptr = ctx->ptr + length;
return ptr <= ctx->end && (length == 0 || ptr > ctx->ptr);
}
static bool
read_ctx_read_ubyte (struct read_ctx *ctx, unsigned char *ret)
{
if (!read_ctx_need_data (ctx, 1))
return false;
if (ret != NULL)
*ret = *ctx->ptr;
ctx->ptr++;
return true;
}
static int
read_ctx_read_uleb128 (struct read_ctx *ctx, uint64_t *ret)
{
uint64_t result = 0;
int shift = 0;
int size = 8 * sizeof (result);
bool zero_tail = false;
while (1)
{
uint8_t byte;
if (!read_ctx_read_ubyte (ctx, &byte))
return -1;
uint8_t payload = byte & 0x7f;
zero_tail = payload == 0 && shift > 0;
result |= (uint64_t)payload << shift;
shift += 7;
if (shift > size && byte != 0x1)
return -1;
if ((byte & 0x80) == 0)
break;
}
if (ret != NULL)
*ret = result;
return zero_tail ? 1 : 0;
}
static bool
checked_read_uleb128 (struct read_ctx *ctx, uint64_t *ret,
struct where *where, const char *what)
{
const unsigned char *ptr = ctx->ptr;
int st = read_ctx_read_uleb128 (ctx, ret);
if (st < 0)
wr_error (where, ": can't read %s.\n", what);
else if (st > 0)
{
char buf[19]; // 16 hexa digits, "0x", terminating zero
sprintf (buf, "%#" PRIx64, *ret);
wr_format_leb128_message (where, what, buf, ptr, ctx->ptr);
}
return st >= 0;
}
static int
read_ctx_read_sleb128 (struct read_ctx *ctx, int64_t *ret)
{
int64_t result = 0;
int shift = 0;
int size = 8 * sizeof (result);
bool zero_tail = false;
bool sign = false;
while (1)
{
uint8_t byte;
if (!read_ctx_read_ubyte (ctx, &byte))
return -1;
uint8_t payload = byte & 0x7f;
zero_tail = shift > 0 && ((payload == 0x7f && sign)
|| (payload == 0 && !sign));
sign = (byte & 0x40) != 0; /* Set sign for rest of loop & next round. */
result |= (int64_t)payload << shift;
shift += 7;
if ((byte & 0x80) == 0)
{
if (shift < size && sign)
result |= -((int64_t)1 << shift);
break;
}
if (shift > size)
return -1;
}
if (ret != NULL)
*ret = result;
return zero_tail ? 1 : 0;
}
static bool
checked_read_sleb128 (struct read_ctx *ctx, int64_t *ret,
struct where *where, const char *what)
{
const unsigned char *ptr = ctx->ptr;
int st = read_ctx_read_sleb128 (ctx, ret);
if (st < 0)
wr_error (where, ": can't read %s.\n", what);
else if (st > 0)
{
char buf[20]; // sign, "0x", 16 hexa digits, terminating zero
int64_t val = *ret;
sprintf (buf, "%s%#" PRIx64, val < 0 ? "-" : "", val < 0 ? -val : val);
wr_format_leb128_message (where, what, buf, ptr, ctx->ptr);
}
return st >= 0;
}
static bool
read_ctx_read_2ubyte (struct read_ctx *ctx, uint16_t *ret)
{
if (!read_ctx_need_data (ctx, 2))
return false;
uint16_t val = read_2ubyte_unaligned_inc (ctx->dbg, ctx->ptr);
if (ret != NULL)
*ret = val;
return true;
}
static bool
read_ctx_read_4ubyte (struct read_ctx *ctx, uint32_t *ret)
{
if (!read_ctx_need_data (ctx, 4))
return false;
uint32_t val = read_4ubyte_unaligned_inc (ctx->dbg, ctx->ptr);
if (ret != NULL)
*ret = val;
return true;
}
static bool
read_ctx_read_8ubyte (struct read_ctx *ctx, uint64_t *ret)
{
if (!read_ctx_need_data (ctx, 8))
return false;
uint64_t val = read_8ubyte_unaligned_inc (ctx->dbg, ctx->ptr);
if (ret != NULL)
*ret = val;
return true;
}
static bool
read_ctx_read_offset (struct read_ctx *ctx, bool dwarf64, uint64_t *ret)
{
if (dwarf64)
return read_ctx_read_8ubyte (ctx, ret);
uint32_t v;
if (!read_ctx_read_4ubyte (ctx, &v))
return false;
if (ret != NULL)
*ret = (uint64_t)v;
return true;
}
static bool
read_ctx_read_var (struct read_ctx *ctx, int width, uint64_t *ret)
{
switch (width)
{
case 4:
case 8:
return read_ctx_read_offset (ctx, width == 8, ret);
case 2:
{
uint16_t val;
if (!read_ctx_read_2ubyte (ctx, &val))
return false;
*ret = val;
return true;
}
case 1:
{
uint8_t val;
if (!read_ctx_read_ubyte (ctx, &val))
return false;
*ret = val;
return true;
}
default:
return false;
};
}
/* The value passed back in uint64_t VALUEP may actually be
type-casted int64_t. WHAT and WHERE describe error message and
context for LEB128 loading. */
static bool
read_ctx_read_form (struct read_ctx *ctx, bool addr_64, uint8_t form,
uint64_t *valuep, struct where *where, const char *what)
{
switch (form)
{
case DW_FORM_addr:
return read_ctx_read_offset (ctx, addr_64, valuep);
case DW_FORM_udata:
return checked_read_uleb128 (ctx, valuep, where, what);
case DW_FORM_sdata:
return checked_read_sleb128 (ctx, (int64_t *)valuep, where, what);
case DW_FORM_data1:
{
uint8_t v;
if (!read_ctx_read_ubyte (ctx, &v))
return false;
if (valuep != NULL)
*valuep = v;
return true;
}
case DW_FORM_data2:
{
uint16_t v;
if (!read_ctx_read_2ubyte (ctx, &v))
return false;
if (valuep != NULL)
*valuep = v;
return true;
}
case DW_FORM_data4:
{
uint32_t v;
if (!read_ctx_read_4ubyte (ctx, &v))
return false;
if (valuep != NULL)
*valuep = v;
return true;
}
case DW_FORM_data8:
return read_ctx_read_8ubyte (ctx, valuep);
};
return false;
}
static const char *
read_ctx_read_str (struct read_ctx *ctx)
{
const char *ret = (const char *)ctx->ptr;
uint8_t byte;
do
if (!read_ctx_read_ubyte (ctx, &byte))
return NULL;
while (byte != 0);
return ret;
}
static bool
read_ctx_skip (struct read_ctx *ctx, uint64_t len)
{
if (!read_ctx_need_data (ctx, len))
return false;
ctx->ptr += len;
return true;
}
static bool
read_ctx_eof (struct read_ctx *ctx)
{
return !read_ctx_need_data (ctx, 1);
}
static bool
attrib_form_valid (uint64_t form)
{
return form > 0 && form <= DW_FORM_indirect;
}
static int
check_sibling_form (uint64_t form)
{
switch (form)
{
case DW_FORM_indirect:
/* Tolerate this in abbrev loading, even during the DIE loading.
We check that dereferenced indirect form yields valid form. */
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
case DW_FORM_ref8:
case DW_FORM_ref_udata:
return 0;
case DW_FORM_ref_addr:
return -1;
default:
return -2;
};
}
/* Check that given form may in fact be valid in some CU. */
static bool
check_abbrev_location_form (uint64_t form)
{
switch (form)
{
case DW_FORM_indirect:
/* loclistptr */
case DW_FORM_data4:
case DW_FORM_data8:
/* block */
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
case DW_FORM_block:
return true;
default:
return false;
};
}
static bool
is_location_attrib (uint64_t name)
{
switch (name)
{
case DW_AT_location:
case DW_AT_frame_base:
case DW_AT_data_location:
case DW_AT_data_member_location:
return true;
default:
return false;
}
}
static struct abbrev_table *
abbrev_table_load (struct read_ctx *ctx)
{
struct abbrev_table *section_chain = NULL;
struct abbrev_table *section = NULL;
uint64_t first_attr_off = 0;
struct where where = WHERE (sec_abbrev, NULL);
where.addr1 = 0;
while (!read_ctx_eof (ctx))
{
uint64_t abbr_off;
uint64_t abbr_code;
{
uint64_t prev_abbr_off = (uint64_t)-1;
uint64_t prev_abbr_code = (uint64_t)-1;
uint64_t zero_seq_off = (uint64_t)-1;
while (!read_ctx_eof (ctx))
{
abbr_off = read_ctx_get_offset (ctx);
where_reset_2 (&where, abbr_off);
/* Abbreviation code. */
if (!checked_read_uleb128 (ctx, &abbr_code, &where, "abbrev code"))
goto free_and_out;
if (abbr_code == 0 && prev_abbr_code == 0
&& zero_seq_off == (uint64_t)-1)
zero_seq_off = abbr_off;
if (abbr_code != 0)
break;
else
section = NULL;
prev_abbr_code = abbr_code;
prev_abbr_off = abbr_off;
}
if (zero_seq_off != (uint64_t)-1)
{
/* Don't report abbrev address, this is section-wide
padding. */
struct where wh = WHERE (where.section, NULL);
wr_message_padding_0 (mc_abbrevs | mc_header, &wh,
zero_seq_off, abbr_off - 1);
}
}
if (read_ctx_eof (ctx))
break;
if (section == NULL)
{
section = xcalloc (1, sizeof (*section));
section->offset = abbr_off;
section->next = section_chain;
section_chain = section;
where_reset_1 (&where, abbr_off);
where_reset_2 (&where, abbr_off);
}
REALLOC (section, abbr);
struct abbrev *cur = section->abbr + section->size++;
WIPE (*cur);
cur->code = abbr_code;
cur->where = where;
/* Abbreviation tag. */
uint64_t abbr_tag;
if (!checked_read_uleb128 (ctx, &abbr_tag, &where, "abbrev tag"))
goto free_and_out;
if (abbr_tag > DW_TAG_hi_user)
{
wr_error (&where, ": invalid abbrev tag 0x%" PRIx64 ".\n", abbr_tag);
goto free_and_out;
}
cur->tag = (typeof (cur->tag))abbr_tag;
/* Abbreviation has_children. */
uint8_t has_children;
if (!read_ctx_read_ubyte (ctx, &has_children))
{
wr_error (&where, ": can't read abbrev has_children.\n");
goto free_and_out;
}
if (has_children != DW_CHILDREN_no
&& has_children != DW_CHILDREN_yes)
{
wr_error (&where,
": invalid has_children value 0x%x.\n", cur->has_children);
goto free_and_out;
}
cur->has_children = has_children == DW_CHILDREN_yes;
bool null_attrib;
uint64_t sibling_attr = 0;
bool low_pc = false, high_pc = false;
do
{
uint64_t attr_off = read_ctx_get_offset (ctx);
uint64_t attrib_name, attrib_form;
if (first_attr_off == 0)
first_attr_off = attr_off;
/* Shift to match elfutils reporting. */
where_reset_3 (&where, attr_off - first_attr_off);
/* Load attribute name and form. */
if (!checked_read_uleb128 (ctx, &attrib_name, &where,
"attribute name"))
goto free_and_out;
if (!checked_read_uleb128 (ctx, &attrib_form, &where,
"attribute form"))
goto free_and_out;
null_attrib = attrib_name == 0 && attrib_form == 0;
/* Now if both are zero, this was the last attribute. */
if (!null_attrib)
{
/* Otherwise validate name and form. */
if (attrib_name > DW_AT_hi_user)
{
wr_error (&where,
": invalid name 0x%" PRIx64 ".\n", attrib_name);
goto free_and_out;
}
if (!attrib_form_valid (attrib_form))
{
wr_error (&where,
": invalid form 0x%" PRIx64 ".\n", attrib_form);
goto free_and_out;
}
}
REALLOC (cur, attribs);
struct abbrev_attrib *acur = cur->attribs + cur->size++;
WIPE (*acur);
/* We do structural checking of sibling attribute, so make
sure our assumptions in actual DIE-loading code are
right. We expect at most one DW_AT_sibling attribute,
with form from reference class, but only CU-local, not
DW_FORM_ref_addr. */
if (attrib_name == DW_AT_sibling)
{
if (sibling_attr != 0)
wr_error (&where,
": Another DW_AT_sibling attribute in one abbreviation. "
"(First was 0x%" PRIx64 ".)\n", sibling_attr);
else
{
assert (attr_off > 0);
sibling_attr = attr_off;
if (!cur->has_children)
wr_message (mc_die_rel | mc_acc_bloat | mc_impact_1,
&where,
": Excessive DW_AT_sibling attribute at childless abbrev.\n");
}
switch (check_sibling_form (attrib_form))
{
case -1:
wr_message (mc_die_rel | mc_impact_2, &where,
": DW_AT_sibling attribute with form DW_FORM_ref_addr.\n");
break;
case -2:
wr_error (&where,
": DW_AT_sibling attribute with non-reference form \"%s\".\n",
dwarf_form_string (attrib_form));
};
}
/* Similar for DW_AT_location and friends. */
else if (is_location_attrib (attrib_name))
{
if (!check_abbrev_location_form (attrib_form))
wr_error (&where,
": %s with invalid form \"%s\".\n",
dwarf_attr_string (attrib_name),
dwarf_form_string (attrib_form));
}
/* Similar for DW_AT_ranges. */
else if (attrib_name == DW_AT_ranges
|| attrib_name == DW_AT_stmt_list)
{
if (attrib_form != DW_FORM_data4
&& attrib_form != DW_FORM_data8
&& attrib_form != DW_FORM_indirect)
wr_error (&where,
": %s with invalid form \"%s\".\n",
dwarf_attr_string (attrib_name),
dwarf_form_string (attrib_form));
}
/* Similar for DW_AT_{low,high}_pc, plus also make sure we
don't see high_pc without low_pc. */
else if (attrib_name == DW_AT_low_pc
|| attrib_name == DW_AT_high_pc)
{
if (attrib_form != DW_FORM_addr
&& attrib_form != DW_FORM_ref_addr)
wr_error (&where,
": %s with invalid form \"%s\".\n",
dwarf_attr_string (attrib_name),
dwarf_form_string (attrib_form));
if (attrib_name == DW_AT_low_pc)
low_pc = true;
else if (attrib_name == DW_AT_high_pc)
high_pc = true;
}
acur->name = attrib_name;
acur->form = attrib_form;
acur->where = where;
}
while (!null_attrib);
where_reset_2 (&where, where.addr2); // drop addr 3
if (high_pc && !low_pc)
wr_error (&where,
": the abbrev has DW_AT_high_pc without also having DW_AT_low_pc.\n");
}
for (section = section_chain; section != NULL; section = section->next)
{
int cmp_abbrs (const void *a, const void *b)
{
struct abbrev *aa = (struct abbrev *)a;
struct abbrev *bb = (struct abbrev *)b;
return aa->code - bb->code;
}
/* The array is most likely already sorted in the file, but just
to be sure... */
qsort (section->abbr, section->size, sizeof (*section->abbr), cmp_abbrs);
}
return section_chain;
free_and_out:
abbrev_table_free (section_chain);
return NULL;
}
static void
abbrev_table_free (struct abbrev_table *abbr)
{
for (struct abbrev_table *it = abbr; it != NULL; )
{
for (size_t i = 0; i < it->size; ++i)
free (it->abbr[i].attribs);
free (it->abbr);
struct abbrev_table *temp = it;
it = it->next;
free (temp);
}
}
static struct abbrev *
abbrev_table_find_abbrev (struct abbrev_table *abbrevs, uint64_t abbrev_code)
{
size_t a = 0;
size_t b = abbrevs->size;
struct abbrev *ab = NULL;
while (a < b)
{
size_t i = (a + b) / 2;
ab = abbrevs->abbr + i;
if (ab->code > abbrev_code)
b = i;
else if (ab->code < abbrev_code)
a = i + 1;
else
return ab;
}
return NULL;
}
static size_t
addr_record_find_addr (struct addr_record *ar, uint64_t addr)
{
size_t a = 0;
size_t b = ar->size;
while (a < b)
{
size_t i = (a + b) / 2;
uint64_t v = ar->addrs[i];
if (v > addr)
b = i;
else if (v < addr)
a = i + 1;
else
return i;
}
return a;
}
static bool
addr_record_has_addr (struct addr_record *ar, uint64_t addr)
{
if (ar->size == 0
|| addr < ar->addrs[0]
|| addr > ar->addrs[ar->size - 1])
return false;
size_t a = addr_record_find_addr (ar, addr);
return a < ar->size && ar->addrs[a] == addr;
}
static void
addr_record_add (struct addr_record *ar, uint64_t addr)
{
size_t a = addr_record_find_addr (ar, addr);
if (a >= ar->size || ar->addrs[a] != addr)
{
REALLOC (ar, addrs);
size_t len = ar->size - a;
memmove (ar->addrs + a + 1, ar->addrs + a, len * sizeof (*ar->addrs));
ar->addrs[a] = addr;
ar->size++;
}
}
static void
addr_record_free (struct addr_record *ar)
{
if (ar != NULL)
free (ar->addrs);
}
static void
ref_record_add (struct ref_record *rr, uint64_t addr, struct where *referrer)
{
REALLOC (rr, refs);
struct ref *ref = rr->refs + rr->size++;
ref->addr = addr;
ref->who = *referrer;
}
static void
ref_record_free (struct ref_record *rr)
{
if (rr != NULL)
free (rr->refs);
}
bool
coverage_pristine (struct coverage *cov, uint64_t begin, uint64_t length)
{
for (uint64_t i = 0; i < length; ++i)
if (coverage_is_covered (cov, begin + i))
return false;
return true;
}
bool
found_hole (uint64_t start, uint64_t length, void *data)
{
struct hole_info *info = (struct hole_info *)data;
bool all_zeroes = true;
for (uint64_t i = start; i < start + length; ++i)
if (((char*)info->data)[i] != 0)
{
all_zeroes = false;
break;
}
uint64_t end = start + length;
if (all_zeroes)
{
/* Zero padding is valid, if it aligns on the bounds of
info->align bytes, and is not excessive. */
if (!(info->align != 0 && info->align != 1
&& (end % info->align == 0) && (start % 4 != 0)
&& (length < info->align)))
wr_message_padding_0 (info->category, &WHERE (info->section, NULL),
start, end - 1);
}
else
/* XXX: This actually lies when the unreferenced portion is
composed of sequences of zeroes and non-zeroes. */
wr_message_padding_n0 (info->category, &WHERE (info->section, NULL),
start, end - 1);
return true;
}
bool
coverage_map_found_hole (uint64_t begin, uint64_t end,
struct section_coverage *sco, void *user)
{
struct coverage_map_hole_info *info = (struct coverage_map_hole_info *)user;
struct where where = WHERE (info->info.section, NULL);
const char *scnname = sco->sec->name;
Elf_Data *data = elf_getdata (sco->sec->scn, NULL);
if (data == NULL)
{
wr_error (&where, ": couldn't read the data of section %s.\n", scnname);
return false;
}
/* We don't expect some sections to be covered. But if they
are at least partially covered, we expect the same
coverage criteria as for .text. */
if (!sco->hit
&& ((sco->sec->shdr.sh_flags & SHF_EXECINSTR) == 0
|| strcmp (scnname, ".init") == 0
|| strcmp (scnname, ".fini") == 0
|| strcmp (scnname, ".plt") == 0))
return true;
/* For REL files, don't print addresses mangled by our layout. */
uint64_t base = info->elf->ehdr.e_type == ET_REL ? 0 : sco->sec->shdr.sh_addr;
/* If we get stripped debuginfo file, the data simply may not be
available. In that case simply report the hole. */
if (data->d_buf != NULL)
{
bool zeroes = true;
for (uint64_t j = begin; j < end; ++j)
if (((char *)data->d_buf)[j] != 0)
{
zeroes = false;
break;
}
if (!zeroes)
return true;
}
wr_message (info->info.category | mc_acc_suboptimal | mc_impact_4, &where,
": addresses %#" PRIx64 "..%#" PRIx64
" of section %s are not covered.\n",
begin + base, end + base, scnname);
return true;
}
void
section_coverage_init (struct section_coverage *sco,
struct sec *sec, bool warn)
{
assert (sco != NULL);
assert (sec != NULL);
sco->sec = sec;
WIPE (sco->cov);
sco->hit = false;
sco->warn = warn;
}
bool
coverage_map_init (struct coverage_map *coverage_map,
struct elf_file *elf,
Elf64_Xword mask,
Elf64_Xword warn_mask,
bool allow_overlap)
{
assert (coverage_map != NULL);
assert (elf != NULL);
WIPE (*coverage_map);
coverage_map->elf = elf;
coverage_map->allow_overlap = allow_overlap;
for (size_t i = 0; i < elf->size; ++i)
{
struct sec *sec = elf->sec + i;
bool normal = (sec->shdr.sh_flags & mask) == mask;
bool warn = (sec->shdr.sh_flags & warn_mask) == warn_mask;
if (normal || warn)
{
REALLOC (coverage_map, scos);
section_coverage_init
(coverage_map->scos + coverage_map->size++, sec, !normal);
}
}
return true;
}
void
coverage_map_add (struct coverage_map *coverage_map,
uint64_t address,
uint64_t length,
struct where *where,
enum message_category cat)
{
bool found = false;
bool crosses_boundary = false;
bool overlap = false;
uint64_t end = address + length;
/* This is for analyzing how much of the current range falls into
sections in coverage map. Whatever is left uncovered doesn't
fall anywhere and is reported. */
struct coverage range_cov;
WIPE (range_cov);
for (size_t i = 0; i < coverage_map->size; ++i)
{
struct section_coverage *sco = coverage_map->scos + i;
GElf_Shdr *shdr = &sco->sec->shdr;
struct coverage *cov = &sco->cov;
Elf64_Addr s_end = shdr->sh_addr + shdr->sh_size;
if (end <= shdr->sh_addr || address >= s_end)
/* no overlap */
continue;
if (found && !crosses_boundary)
{
/* While probably not an error, it's very suspicious. */
wr_message (cat | mc_impact_2, where,
": the range %#" PRIx64 "..%#" PRIx64
" crosses section boundaries.\n",
address, end);
crosses_boundary = true;
}
found = true;
if (length == 0)
/* Empty range. That means no actual coverage, and we can
also be sure that there are no more sections that this one
falls into. */
break;
uint64_t cov_begin
= address < shdr->sh_addr ? 0 : address - shdr->sh_addr;
uint64_t cov_end
= end < s_end ? end - shdr->sh_addr : shdr->sh_size;
assert (cov_begin < cov_end);
uint64_t r_delta = shdr->sh_addr - address;
uint64_t r_cov_begin = cov_begin + r_delta;
uint64_t r_cov_end = cov_end + r_delta;
if (!overlap && !coverage_map->allow_overlap
&& !coverage_pristine (cov, cov_begin, cov_end - cov_begin))
{
/* Not a show stopper, this shouldn't derail high-level. */
wr_message (cat | mc_impact_2 | mc_error, where,
": the range %#" PRIx64 "..%#" PRIx64
" overlaps with another one.\n",
address, end);
overlap = true;
}
if (sco->warn)
wr_message (cat | mc_impact_2, where,
": the range %#" PRIx64 "..%#" PRIx64
" covers section %s.\n",
address, end, sco->sec->name);
/* Section coverage... */
coverage_add (cov, cov_begin, cov_end - cov_begin);
sco->hit = true;
/* And range coverage... */
coverage_add (&range_cov, r_cov_begin, r_cov_end - r_cov_begin);
}
if (!found)
/* Not a show stopper. */
wr_error (where,
": couldn't find a section that the range %#"
PRIx64 "..%#" PRIx64 " covers.\n", address, end);
else if (length > 0)
{
bool range_hole (uint64_t h_start, uint64_t h_length,
void *user __attribute__ ((unused)))
{
assert (h_length != 0);
wr_error (where,
": portion %#" PRIx64 "..%#" PRIx64
", of the range %#" PRIx64 "..%#" PRIx64
" doesn't fall into any ALLOC section.\n",
h_start + address, h_start + address + h_length - 1,
address, end);
return true;
}
coverage_find_holes (&range_cov, 0, length, range_hole, NULL);
}
coverage_free (&range_cov);
}
bool
coverage_map_find_holes (struct coverage_map *coverage_map,
bool (*cb) (uint64_t begin, uint64_t end,
struct section_coverage *, void *),
void *user)
{
for (size_t i = 0; i < coverage_map->size; ++i)
{
struct section_coverage *sco = coverage_map->scos + i;
bool wrap_cb (uint64_t h_start, uint64_t h_length, void *h_user)
{
return cb (h_start, h_start + h_length - 1, sco, h_user);
}
if (!coverage_find_holes (&sco->cov, 0, sco->sec->shdr.sh_size,
wrap_cb, user))
return false;
}
return true;
}
void
coverage_map_free (struct coverage_map *coverage_map)
{
for (size_t i = 0; i < coverage_map->size; ++i)
coverage_free (&coverage_map->scos[i].cov);
free (coverage_map->scos);
}
static void
cu_free (struct cu *cu_chain)
{
for (struct cu *it = cu_chain; it != NULL; )
{
addr_record_free (&it->die_addrs);
struct cu *temp = it;
it = it->next;
free (temp);
}
}
static struct cu *
cu_find_cu (struct cu *cu_chain, uint64_t offset)
{
for (struct cu *it = cu_chain; it != NULL; it = it->next)
if (it->offset == offset)
return it;
return NULL;
}
static bool
check_die_references (struct cu *cu,
struct ref_record *die_refs)
{
bool retval = true;
for (size_t i = 0; i < die_refs->size; ++i)
{
struct ref *ref = die_refs->refs + i;
if (!addr_record_has_addr (&cu->die_addrs, ref->addr))
{
wr_error (&ref->who,
": unresolved reference to " PRI_DIE ".\n", ref->addr);
retval = false;
}
}
return retval;
}
static bool
check_global_die_references (struct cu *cu_chain)
{
bool retval = true;
for (struct cu *it = cu_chain; it != NULL; it = it->next)
for (size_t i = 0; i < it->die_refs.size; ++i)
{
struct ref *ref = it->die_refs.refs + i;
struct cu *ref_cu = NULL;
for (struct cu *jt = cu_chain; jt != NULL; jt = jt->next)
if (addr_record_has_addr (&jt->die_addrs, ref->addr))
{
ref_cu = jt;
break;
}
if (ref_cu == NULL)
{
wr_error (&ref->who,
": unresolved (non-CU-local) reference to " PRI_DIE ".\n",
ref->addr);
retval = false;
}
else if (ref_cu == it)
/* This is technically not a problem, so long as the
reference is valid, which it is. But warn about this
anyway, perhaps local reference could be formed on fewer
number of bytes. */
wr_message (mc_impact_2 | mc_acc_suboptimal | mc_die_rel,
&ref->who,
": local reference to " PRI_DIE " formed as global.\n",
ref->addr);
}
return retval;
}
static bool
read_size_extra (struct read_ctx *ctx, uint32_t size32, uint64_t *sizep,
bool *dwarf_64p, struct where *wh)
{
if (size32 == DWARF3_LENGTH_64_BIT)
{
if (!read_ctx_read_8ubyte (ctx, sizep))
{
wr_error (wh, ": can't read 64bit CU length.\n");
return false;
}
*dwarf_64p = true;
}
else if (size32 >= DWARF3_LENGTH_MIN_ESCAPE_CODE)
{
wr_error (wh, ": unrecognized CU length escape value: "
"%" PRIx32 ".\n", size32);
return false;
}
else
{
*sizep = size32;
*dwarf_64p = false;
}
return true;
}
static bool
check_zero_padding (struct read_ctx *ctx,
enum message_category category,
struct where *wh)
{
assert (ctx->ptr != ctx->end);
const unsigned char *save_ptr = ctx->ptr;
while (!read_ctx_eof (ctx))
if (*ctx->ptr++ != 0)
{
ctx->ptr = save_ptr;
return false;
}
wr_message_padding_0 (category, wh,
(uint64_t)(save_ptr - ctx->begin),
(uint64_t)(ctx->end - ctx->begin));
return true;
}
static struct where
where_from_reloc (struct relocation_data *reloc, struct where *ref)
{
struct where where
= WHERE (reloc->type == SHT_REL ? sec_rel : sec_rela, NULL);
where_reset_1 (&where, reloc->rel[reloc->index].offset);
where.ref = ref;
return where;
}
enum skip_type
{
skip_unref = 0,
skip_mismatched = 1,
skip_ok,
};
static struct relocation *
relocation_next (struct relocation_data *reloc, uint64_t offset,
struct where *where, enum skip_type st)
{
if (reloc == NULL || reloc->rel == NULL)
return NULL;
while (reloc->index < reloc->size)
{
struct where reloc_where = where_from_reloc (reloc, where);
struct relocation *rel = reloc->rel + reloc->index;
where_reset_2 (&reloc_where, rel->offset);
/* This relocation entry is ahead of us. */
if (rel->offset > offset)
return NULL;
reloc->index++;
if (rel->invalid)
continue;
if (rel->offset < offset)
{
if (st != skip_ok)
{
void (*w) (const struct where *, const char *, ...) = wr_error;
(*w) (&reloc_where,
((const char *[])
{": relocation targets unreferenced portion of the section.\n",
": relocation is mismatched.\n"})[st]);
}
continue;
}
return rel;
}
return NULL;
}
/* Skip all relocation up to offset, and leave cursor pointing at that
relocation, so that next time relocation_next is called, relocation
matching that offset is immediately yielded. */
static void
relocation_skip (struct relocation_data *reloc, uint64_t offset,
struct where *where, enum skip_type st)
{
if (reloc != NULL && reloc->rel != NULL)
relocation_next (reloc, offset - 1, where, st);
}
/* Skip all the remaining relocations. */
static void
relocation_skip_rest (struct section_data *data)
{
if (data->rel.rel != NULL)
relocation_next (&data->rel, (uint64_t)-1, &WHERE_SECDATA (data, NULL),
skip_mismatched);
}
/* SYMPTR may be NULL, otherwise (**SYMPTR) has to yield valid memory
location. When the function returns, (*SYMPTR) is either NULL, in
which case we failed or didn't get around to obtain the symbol from
symbol table, or non-NULL, in which case the symbol was initialized. */
static void
relocate_one (struct relocation_data *reloc, struct relocation *rel,
unsigned width, uint64_t *value, struct where *where,
enum section_id offset_into, GElf_Sym **symptr)
{
if (rel->invalid)
return;
struct where reloc_where = where_from_reloc (reloc, where);
where_reset_2 (&reloc_where, rel->offset);
struct where reloc_ref_where = reloc_where;
reloc_ref_where.next = where;
GElf_Sym symbol_mem, *symbol;
if (symptr != NULL)
{
symbol = *symptr;
*symptr = NULL;
}
else
symbol = &symbol_mem;
if (offset_into == sec_invalid)
{
wr_message (mc_impact_3 | mc_reloc, &reloc_ref_where,
": relocates a datum that shouldn't be relocated.\n");
return;
}
Elf_Type type = ebl_reloc_simple_type (reloc->file->ebl, rel->type);
unsigned rel_width;
switch (type)
{
case ELF_T_BYTE:
rel_width = 1;
break;
case ELF_T_HALF:
rel_width = 2;
break;
case ELF_T_WORD:
case ELF_T_SWORD:
rel_width = 4;
break;
case ELF_T_XWORD:
case ELF_T_SXWORD:
rel_width = 8;
break;
default:
/* This has already been diagnosed during the isolated
validation of relocation section. */
return;
};
if (rel_width != width)
wr_error (&reloc_ref_where,
": %d-byte relocation relocates %d-byte datum.\n",
rel_width, width);
/* Tolerate that we might have failed to obtain a symbol table. */
if (reloc->symdata != NULL)
{
symbol = gelf_getsym (reloc->symdata, rel->symndx, symbol);
if (symptr != NULL)
*symptr = symbol;
if (symbol == NULL)
{
wr_error (&reloc_where,
": couldn't obtain symbol #%d: %s.\n",
rel->symndx, elf_errmsg (-1));
return;
}
uint64_t section_index = symbol->st_shndx;
/* XXX We should handle SHN_XINDEX here. Or, instead, maybe it
would be possible to use dwfl, which already does XINDEX
translation. */
/* For ET_REL files, we do section layout manually. But we
don't update symbol table doing that. So instead of looking
at symbol value, look at section address. */
uint64_t sym_value = symbol->st_value;
if (reloc->file->ehdr.e_type == ET_REL
&& ELF64_ST_TYPE (symbol->st_info) == STT_SECTION)
{
assert (sym_value == 0);
sym_value = reloc->file->sec[section_index].shdr.sh_addr;
}
/* It's target value, not section offset. */
if (offset_into == rel_value
|| offset_into == rel_address
|| offset_into == rel_exec)
{
/* If a target value is what's expected, then complain if
it's not either SHN_ABS, an SHF_ALLOC section, or
SHN_UNDEF. For data forms of address_size, an SHN_UNDEF
reloc is acceptable, otherwise reject it. */
if (!(section_index == SHN_ABS
|| (offset_into == rel_address
&& (section_index == SHN_UNDEF
|| section_index == SHN_COMMON))))
{
Elf_Scn *scn;
GElf_Shdr shdr_mem, *shdr;
if (offset_into != rel_address && section_index == SHN_UNDEF)
wr_error (&reloc_where,
": relocation of an address is formed against SHN_UNDEF symbol"
" (symtab index %d).\n", rel->symndx);
else if ((scn = elf_getscn (reloc->file->dwarf->elf,
section_index)) == NULL)
wr_error (&reloc_where,
": couldn't obtain associated section #%" PRId64 ".\n",
section_index);
else if ((shdr = gelf_getshdr (scn, &shdr_mem)) == NULL)
wr_error (&reloc_where,
": couldn't obtain header of associated section #%" PRId64 ".\n",
section_index);
else
{
if ((shdr->sh_flags & SHF_ALLOC) != SHF_ALLOC)
wr_message (mc_reloc | mc_impact_3, &reloc_where,
": associated section %s isn't SHF_ALLOC.\n",
reloc->file->sec[section_index].name);
if (offset_into == rel_exec
&& (shdr->sh_flags & SHF_EXECINSTR) != SHF_EXECINSTR)
/* This may still be kosher, but it's suspicious. */
wr_message (mc_reloc | mc_impact_2, &reloc_where,
": relocation against %s is suspicious, expected executable section.\n",
reloc->file->sec[section_index].name);
}
}
}
else
{
enum section_id id;
/* If symtab[symndx].st_shndx does not match the expected
debug section's index, complain. */
if (section_index >= reloc->file->size)
wr_error (&reloc_where,
": invalid associated section #%" PRId64 ".\n",
section_index);
else if ((id = reloc->file->sec[section_index].id) != offset_into)
{
char *wh1 = id != sec_invalid
? strdup (where_fmt (&WHERE (id, NULL), NULL))
: (char *)reloc->file->sec[section_index].name;
char *wh2 = strdup (where_fmt (&WHERE (offset_into, NULL), NULL));
wr_error (&reloc_where,
": relocation references section %s, but %s was expected.\n",
wh1, wh2);
free (wh2);
if (id != sec_invalid)
free (wh1);
}
}
/* Only do the actual relocation if we have ET_REL files. For
non-ET_REL files, only do the above checking. */
if (reloc->file->ehdr.e_type == ET_REL)
{
*value = rel->addend + sym_value;
if (rel_width == 4)
*value = *value & (uint64_t)(uint32_t)-1;
}
}
}
static enum section_id
reloc_target (uint8_t form, struct abbrev_attrib *at)
{
switch (form)
{
case DW_FORM_strp:
return sec_str;
case DW_FORM_addr:
switch (at->name)
{
case DW_AT_low_pc:
case DW_AT_high_pc:
case DW_AT_entry_pc:
return rel_exec;
case DW_AT_const_value:
/* Appears in some kernel modules. It's not allowed by the
standard, but leave that for high-level checks. */
return rel_address;
};
break;
case DW_FORM_ref_addr:
return sec_info;
case DW_FORM_data1:
case DW_FORM_data2:
/* While these are technically legal, they are never used in
DWARF sections. So better mark them as illegal, and have
dwarflint flag them. */
return sec_invalid;
case DW_FORM_data4:
case DW_FORM_data8:
switch (at->name)
{
case DW_AT_stmt_list:
return sec_line;
case DW_AT_location:
case DW_AT_string_length:
case DW_AT_return_addr:
case DW_AT_data_member_location:
case DW_AT_frame_base:
case DW_AT_segment:
case DW_AT_static_link:
case DW_AT_use_location:
case DW_AT_vtable_elem_location:
return sec_loc;
case DW_AT_mac_info:
return sec_mac;
case DW_AT_ranges:
return sec_ranges;
}
break;
case DW_FORM_string:
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
/* Shouldn't be relocated. */
return sec_invalid;
case DW_FORM_sdata:
case DW_FORM_udata:
case DW_FORM_flag:
case DW_FORM_ref_udata:
assert (!"Can't be relocated!");
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
case DW_FORM_block:
assert (!"Should be handled specially!");
};
printf ("XXX don't know how to handle form=%s, at=%s\n",
dwarf_form_string (form), dwarf_attr_string (at->name));
return rel_value;
}
static enum section_id
reloc_target_loc (uint8_t opcode)
{
switch (opcode)
{
case DW_OP_call2:
case DW_OP_call4:
return sec_info;
case DW_OP_addr:
return rel_address;
case DW_OP_call_ref:
assert (!"Can't handle call_ref!");
};
printf ("XXX don't know how to handle opcode=%s\n",
dwarf_locexpr_opcode_string (opcode));
return rel_value;
}
static bool
supported_version (unsigned version,
size_t num_supported, struct where *where, ...)
{
bool retval = false;
va_list ap;
va_start (ap, where);
for (size_t i = 0; i < num_supported; ++i)
{
unsigned v = va_arg (ap, unsigned);
if (version == v)
{
retval = true;
break;
}
}
va_end (ap);
if (!retval)
wr_error (where, ": unsupported version %d.\n", version);
return retval;
}
static void
check_range_relocations (enum message_category cat,
struct where *where,
struct elf_file *file,
GElf_Sym *begin_symbol,
GElf_Sym *end_symbol,
const char *description)
{
if (begin_symbol != NULL
&& end_symbol != NULL
&& begin_symbol->st_shndx != end_symbol->st_shndx)
wr_message (cat | mc_impact_2 | mc_reloc, where,
": %s relocated against different sections (%s and %s).\n",
description,
file->sec[begin_symbol->st_shndx].name,
file->sec[end_symbol->st_shndx].name);
}
/*
Returns:
-1 in case of error
+0 in case of no error, but the chain only consisted of a
terminating zero die.
+1 in case some dies were actually loaded
*/
static int
read_die_chain (struct read_ctx *ctx,
struct cu *cu,
struct abbrev_table *abbrevs,
Elf_Data *strings,
bool dwarf_64, bool addr_64,
struct ref_record *local_die_refs,
struct coverage *strings_coverage,
struct relocation_data *reloc,
struct elf_file *file)
{
bool got_die = false;
uint64_t sibling_addr = 0;
uint64_t die_off, prev_die_off = 0;
struct abbrev *abbrev, *prev_abbrev = NULL;
struct where where = WHERE (sec_info, NULL);
while (!read_ctx_eof (ctx))
{
where = cu->where;
die_off = read_ctx_get_offset (ctx);
/* Shift reported DIE offset by CU offset, to match the way
readelf reports DIEs. */
where_reset_2 (&where, die_off + cu->offset);
uint64_t abbr_code;
prev_die_off = die_off;
if (!checked_read_uleb128 (ctx, &abbr_code, &where, "abbrev code"))
return -1;
/* Check sibling value advertised last time through the loop. */
if (sibling_addr != 0)
{
if (abbr_code == 0)
wr_error (&where,
": is the last sibling in chain, but has a DW_AT_sibling attribute.\n");
else if (sibling_addr != die_off)
wr_error (&where, ": This DIE should have had its sibling at 0x%"
PRIx64 ", but it's at 0x%" PRIx64 " instead.\n",
sibling_addr, die_off);
sibling_addr = 0;
}
else if (prev_abbrev != NULL && prev_abbrev->has_children)
/* Even if it has children, the DIE can't have a sibling
attribute if it's the last DIE in chain. That's the reason
we can't simply check this when loading abbrevs. */
wr_message (mc_die_rel | mc_acc_suboptimal | mc_impact_4, &where,
": This DIE had children, but no DW_AT_sibling attribute.\n");
/* The section ended. */
if (abbr_code == 0)
break;
if (read_ctx_eof (ctx))
{
wr_error (&where, ": DIE chain not terminated with DIE with zero abbrev code.\n");
break;
}
prev_die_off = die_off;
got_die = true;
/* Find the abbrev matching the code. */
abbrev = abbrev_table_find_abbrev (abbrevs, abbr_code);
if (abbrev == NULL)
{
wr_error (&where,
": abbrev section at 0x%" PRIx64
" doesn't contain code %" PRIu64 ".\n",
abbrevs->offset, abbr_code);
return -1;
}
abbrev->used = true;
addr_record_add (&cu->die_addrs, cu->offset + die_off);
uint64_t low_pc = (uint64_t)-1, high_pc = (uint64_t)-1;
bool low_pc_relocated = false, high_pc_relocated = false;
GElf_Sym low_pc_symbol_mem, *low_pc_symbol = &low_pc_symbol_mem;
GElf_Sym high_pc_symbol_mem, *high_pc_symbol = &high_pc_symbol_mem;
/* Attribute values. */
for (struct abbrev_attrib *it = abbrev->attribs;
it->name != 0; ++it)
{
where.ref = &it->where;
void record_ref (uint64_t addr, struct where *who, bool local)
{
struct ref_record *record = &cu->die_refs;
if (local)
{
assert (ctx->end > ctx->begin);
if (addr > (uint64_t)(ctx->end - ctx->begin))
{
wr_error (&where,
": invalid reference outside the CU: 0x%" PRIx64 ".\n",
addr);
return;
}
/* Address holds a CU-local reference, so add CU
offset to turn it into section offset. */
addr += cu->offset;
record = local_die_refs;
}
if (record != NULL)
ref_record_add (record, addr, who);
}
uint8_t form = it->form;
bool indirect = form == DW_FORM_indirect;
if (indirect)
{
uint64_t value;
if (!checked_read_uleb128 (ctx, &value, &where,
"indirect attribute form"))
return -1;
if (!attrib_form_valid (value))
{
wr_error (&where,
": invalid indirect form 0x%" PRIx64 ".\n", value);
return -1;
}
form = value;
if (it->name == DW_AT_sibling)
switch (check_sibling_form (form))
{
case -1:
wr_message (mc_die_rel | mc_impact_2, &where,
": DW_AT_sibling attribute with (indirect) form DW_FORM_ref_addr.\n");
break;
case -2:
wr_error (&where,
": DW_AT_sibling attribute with non-reference (indirect) form \"%s\".\n",
dwarf_form_string (value));
};
}
enum check_what_t
{
check_nothing = 0,
check_locptr,
check_lineptr,
check_rangeptr
};
static enum message_category mc_check[] =
{
[check_nothing] = mc_none,
[check_locptr] = mc_loc,
[check_lineptr] = mc_line,
[check_rangeptr] = mc_ranges
};
void do_check_ptr (enum check_what_t what, uint64_t value)
{
assert (what != check_nothing);
if (what == check_rangeptr && ((value % cu->address_size) != 0))
wr_message (mc_ranges | mc_impact_2, &where,
": rangeptr value %#" PRIx64
" not aligned to CU address size.\n", value);
struct ref_record *ref = NULL;
switch (what)
{
case check_rangeptr:
ref = &cu->range_refs;
break;
case check_lineptr:
ref = &cu->line_refs;
break;
case check_locptr:
ref = &cu->loc_refs;
case check_nothing:
break;
};
ref_record_add (ref, value, &where);
}
enum check_what_t check_ptr = check_nothing;
if (is_location_attrib (it->name))
{
switch (form)
{
case DW_FORM_data8:
if (!dwarf_64)
wr_error (&where,
": location attribute with form \"%s\" in 32-bit CU.\n",
dwarf_form_string (form));
/* fall-through */
case DW_FORM_data4:
check_ptr = check_locptr;
/* fall-through */
case DW_FORM_block1:
case DW_FORM_block2:
case DW_FORM_block4:
case DW_FORM_block:
break;
default:
/* Only print error if it's indirect. Otherwise we
gave diagnostic during abbrev loading. */
if (indirect)
wr_error (&where,
": location attribute with invalid (indirect) form \"%s\".\n",
dwarf_form_string (form));
};
}
else if (it->name == DW_AT_ranges
|| it->name == DW_AT_stmt_list)
switch (form)
{
case DW_FORM_data8:
if (!dwarf_64)
wr_error (&where,
": %s with form DW_FORM_data8 in 32-bit CU.\n",
dwarf_attr_string (it->name));
/* fall-through */
case DW_FORM_data4:
if (it->name == DW_AT_ranges)
check_ptr = check_rangeptr;
else
{
assert (it->name == DW_AT_stmt_list);
check_ptr = check_lineptr;
}
break;
default:
/* Only print error if it's indirect. Otherwise we
gave diagnostic during abbrev loading. */
if (indirect)
wr_error (&where,
": %s with invalid (indirect) form \"%s\".\n",
dwarf_attr_string (it->name),
dwarf_form_string (form));
};
uint64_t ctx_offset = read_ctx_get_offset (ctx) + cu->offset;
struct relocation *rel;
bool type_is_rel = file->ehdr.e_type == ET_REL;
switch (form)
{
case DW_FORM_strp:
{
uint64_t addr;
if (!read_ctx_read_offset (ctx, dwarf_64, &addr))
{
cant_read:
wr_error (&where, ": can't read attribute value.\n");
return -1;
}
if ((rel = relocation_next (reloc, ctx_offset,
&where, skip_mismatched)))
relocate_one (reloc, rel, dwarf_64 ? 8 : 4,
&addr, &where, sec_str, NULL);
else if (type_is_rel)
wr_message (mc_impact_2 | mc_die_other | mc_reloc | mc_strings,
&where, PRI_LACK_RELOCATION, "DW_FORM_strp");
if (strings == NULL)
wr_error (&where,
": strp attribute, but no .debug_str section.\n");
else if (addr >= strings->d_size)
wr_error (&where,
": Invalid offset outside .debug_str: 0x%" PRIx64 ".\n",
addr);
else
{
/* Record used part of .debug_str. */
const char *strp = (const char *)strings->d_buf + addr;
if (strings_coverage != NULL)
coverage_add (strings_coverage, addr, strlen (strp) + 1);
}
break;
}
case DW_FORM_string:
{
if (!read_ctx_read_str (ctx))
goto cant_read;
break;
}
case DW_FORM_addr:
case DW_FORM_ref_addr:
{
uint64_t addr;
if (!read_ctx_read_offset (ctx, addr_64, &addr))
goto cant_read;
uint64_t *addrp = NULL;
bool *relocatedp = NULL;
GElf_Sym **symbolp = NULL;
switch (it->name)
{
case DW_AT_low_pc:
relocatedp = &low_pc_relocated;
symbolp = &low_pc_symbol;
addrp = &low_pc;
break;
case DW_AT_high_pc:
relocatedp = &high_pc_relocated;
symbolp = &high_pc_symbol;
addrp = &high_pc;
};
if ((rel = relocation_next (reloc, ctx_offset,
&where, skip_mismatched)))
{
relocate_one (reloc, rel, addr_64 ? 8 : 4, &addr, &where,
reloc_target (form, it), symbolp);
if (relocatedp != NULL)
*relocatedp = true;
if (addrp != NULL)
*addrp = addr;
}
else if (type_is_rel && addr != 0)
/* In non-rel files, neither addr, nor ref_addr
/need/ a relocation. We at least check that
ref_addr points to sensible datum by recording
the reference below. */
wr_message (mc_impact_2 | mc_die_rel | mc_reloc, &where,
PRI_LACK_RELOCATION, dwarf_form_string (form));
if (form == DW_FORM_ref_addr)
record_ref (addr, &where, false);
if (it->name == DW_AT_low_pc
&& (abbrev->tag == DW_TAG_compile_unit
|| abbrev->tag == DW_TAG_partial_unit))
cu->base = addr;
break;
}
case DW_FORM_udata:
case DW_FORM_ref_udata:
{
uint64_t value;
if (!checked_read_uleb128 (ctx, &value, &where,
"attribute value"))
return -1;
if (it->name == DW_AT_sibling)
sibling_addr = value;
else if (form == DW_FORM_ref_udata)
record_ref (value, &where, true);
break;
}
case DW_FORM_flag:
case DW_FORM_data1:
case DW_FORM_ref1:
{
/* Neither of these should be relocated. */
uint8_t value;
if (!read_ctx_read_ubyte (ctx, &value))
goto cant_read;
if (it->name == DW_AT_sibling)
sibling_addr = value;
else if (form == DW_FORM_ref1)
record_ref (value, &where, true);
break;
}
case DW_FORM_data2:
case DW_FORM_ref2:
{
/* Neither of these should be relocated. */
uint16_t value;
if (!read_ctx_read_2ubyte (ctx, &value))
goto cant_read;
if (it->name == DW_AT_sibling)
sibling_addr = value;
else if (form == DW_FORM_ref2)
record_ref (value, &where, true);
break;
}
case DW_FORM_data4:
case DW_FORM_ref4:
{
uint32_t raw_value;
if (!read_ctx_read_4ubyte (ctx, &raw_value))
goto cant_read;
/* DW_FORM_ref4 shouldn't be relocated. */
uint64_t value = raw_value;
if (form == DW_FORM_data4)
{
if ((rel = relocation_next (reloc, ctx_offset,
&where, skip_mismatched)))
relocate_one (reloc, rel, 4, &value, &where,
reloc_target (form, it), NULL);
else if (type_is_rel && check_ptr != check_nothing)
wr_message (mc_impact_2 | mc_die_other | mc_reloc
| mc_check[check_ptr],
&where, PRI_LACK_RELOCATION,
dwarf_form_string (form));
}
if (it->name == DW_AT_sibling)
sibling_addr = value;
else if (check_ptr != check_nothing)
do_check_ptr (check_ptr, value);
else if (form == DW_FORM_ref4)
record_ref (value, &where, true);
break;
}
case DW_FORM_data8:
case DW_FORM_ref8:
{
uint64_t value;
if (!read_ctx_read_8ubyte (ctx, &value))
goto cant_read;
/* DW_FORM_ref8 shouldn't be relocated. */
if (form == DW_FORM_data8)
{
if ((rel = relocation_next (reloc, ctx_offset,
&where, skip_mismatched)))
relocate_one (reloc, rel, 8, &value, &where,
reloc_target (form, it), NULL);
else if (type_is_rel && check_ptr != check_nothing)
wr_message (mc_impact_2 | mc_die_other | mc_reloc
| mc_check[check_ptr],
&where, PRI_LACK_RELOCATION,
dwarf_form_string (form));
}
if (it->name == DW_AT_sibling)
sibling_addr = value;
else if (check_ptr != check_nothing)
do_check_ptr (check_ptr, value);
else if (form == DW_FORM_ref8)
record_ref (value, &where, true);
break;
}
case DW_FORM_sdata:
{
int64_t value;
if (!checked_read_sleb128 (ctx, &value, &where,
"attribute value"))
return -1;
break;
}
case DW_FORM_block:
{
int width = 0;
uint64_t length;
goto process_DW_FORM_block;
case DW_FORM_block1:
width = 1;
goto process_DW_FORM_block;
case DW_FORM_block2:
width = 2;
goto process_DW_FORM_block;
case DW_FORM_block4:
width = 4;
process_DW_FORM_block:
if (width == 0)
{
if (!checked_read_uleb128 (ctx, &length, &where,
"attribute value"))
return -1;
}
else if (!read_ctx_read_var (ctx, width, &length))
goto cant_read;
if (is_location_attrib (it->name))
{
uint64_t expr_start = cu->offset + read_ctx_get_offset (ctx);
if (!check_location_expression (ctx, expr_start,
reloc, length,
&where, addr_64))
return -1;
}
else
relocation_skip (reloc,
read_ctx_get_offset (ctx) + length,
&where, skip_mismatched);
if (!read_ctx_skip (ctx, length))
goto cant_read;
break;
}
case DW_FORM_indirect:
wr_error (&where, ": indirect form is again indirect.\n");
return -1;
default:
wr_error (&where,
": internal error: unhandled form 0x%x.\n", form);
}
}
where.ref = NULL;
if (high_pc != (uint64_t)-1 && low_pc != (uint64_t)-1)
{
if (high_pc_relocated != low_pc_relocated)
wr_message (mc_die_other | mc_impact_2 | mc_reloc, &where,
": only one of DW_AT_low_pc and DW_AT_high_pc is relocated.\n");
else
check_range_relocations (mc_die_other, &where,
file,
low_pc_symbol, high_pc_symbol,
"DW_AT_low_pc and DW_AT_high_pc");
}
where.ref = &abbrev->where;
if (abbrev->has_children)
{
int st = read_die_chain (ctx, cu, abbrevs, strings,
dwarf_64, addr_64,
local_die_refs,
strings_coverage, reloc, file);
if (st == -1)
return -1;
else if (st == 0)
wr_message (mc_impact_3 | mc_acc_suboptimal | mc_die_rel,
&where,
": abbrev has_children, but the chain was empty.\n");
}
}
if (sibling_addr != 0)
wr_error (&where,
": this DIE should have had its sibling at 0x%"
PRIx64 ", but the DIE chain ended.\n", sibling_addr);
return got_die ? 1 : 0;
}
static bool
check_cu_structural (struct read_ctx *ctx,
struct cu *const cu,
struct abbrev_table *abbrev_chain,
Elf_Data *strings,
bool dwarf_64,
struct coverage *strings_coverage,
struct relocation_data *reloc,
struct elf_file *file)
{
uint8_t address_size;
bool retval = true;
/* Version. */
uint16_t version;
if (!read_ctx_read_2ubyte (ctx, &version))
{
wr_error (&cu->where, ": can't read version.\n");
return false;
}
if (!supported_version (version, 2, &cu->where, 2, 3))
return false;
if (version == 2 && dwarf_64)
/* Keep going. It's a standard violation, but we may still be
able to read the unit under consideration and do high-level
checks. */
wr_error (&cu->where, ": invalid 64-bit unit in DWARF 2 format.\n");
/* Abbrev offset. */
uint64_t abbrev_offset;
uint64_t ctx_offset = read_ctx_get_offset (ctx) + cu->offset;
if (!read_ctx_read_offset (ctx, dwarf_64, &abbrev_offset))
{
wr_error (&cu->where, ": can't read abbrev offset.\n");
return false;
}
struct relocation *rel
= relocation_next (reloc, ctx_offset, &cu->where, skip_mismatched);
if (rel != NULL)
relocate_one (reloc, rel, dwarf_64 ? 8 : 4,
&abbrev_offset, &cu->where, sec_abbrev, NULL);
else if (file->ehdr.e_type == ET_REL)
wr_message (mc_impact_2 | mc_info | mc_reloc, &cu->where,
PRI_LACK_RELOCATION, "abbrev offset");
/* Address size. */
if (!read_ctx_read_ubyte (ctx, &address_size))
{
wr_error (&cu->where, ": can't read address size.\n");
return false;
}
if (address_size != 4 && address_size != 8)
{
wr_error (&cu->where,
": invalid address size: %d (only 4 or 8 allowed).\n",
address_size);
return false;
}
cu->address_size = address_size;
struct abbrev_table *abbrevs = abbrev_chain;
for (; abbrevs != NULL; abbrevs = abbrevs->next)
if (abbrevs->offset == abbrev_offset)
break;
if (abbrevs == NULL)
{
wr_error (&cu->where,
": couldn't find abbrev section with offset 0x%" PRIx64 ".\n",
abbrev_offset);
return false;
}
struct ref_record local_die_refs;
WIPE (local_die_refs);
cu->cudie_offset = read_ctx_get_offset (ctx) + cu->offset;
if (read_die_chain (ctx, cu, abbrevs, strings,
dwarf_64, address_size == 8,
&local_die_refs, strings_coverage,
(reloc != NULL && reloc->size > 0) ? reloc : NULL,
file) >= 0)
{
for (size_t i = 0; i < abbrevs->size; ++i)
if (!abbrevs->abbr[i].used)
wr_message (mc_impact_3 | mc_acc_bloat | mc_abbrevs, &cu->where,
": abbreviation with code %" PRIu64 " is never used.\n",
abbrevs->abbr[i].code);
if (!check_die_references (cu, &local_die_refs))
retval = false;
}
else
retval = false;
ref_record_free (&local_die_refs);
return retval;
}
static struct cu *
check_info_structural (struct section_data *data,
struct abbrev_table *abbrev_chain,
Elf_Data *strings)
{
struct read_ctx ctx;
read_ctx_init (&ctx, data->file->dwarf, data->data);
struct ref_record die_refs;
WIPE (die_refs);
struct cu *cu_chain = NULL;
bool success = true;
struct coverage strings_coverage_mem, *strings_coverage = NULL;
if (strings != NULL && check_category (mc_strings))
{
WIPE (strings_coverage_mem);
strings_coverage = &strings_coverage_mem;
}
struct relocation_data *reloc = data->rel.size > 0 ? &data->rel : NULL;
while (!read_ctx_eof (&ctx))
{
const unsigned char *cu_begin = ctx.ptr;
struct where where = WHERE (sec_info, NULL);
where_reset_1 (&where, read_ctx_get_offset (&ctx));
struct cu *cur = xcalloc (1, sizeof (*cur));
cur->offset = where.addr1;
cur->next = cu_chain;
cur->where = where;
cur->base = (uint64_t)-1;
cu_chain = cur;
uint32_t size32;
uint64_t size;
bool dwarf_64 = false;
/* Reading CU header is a bit tricky, because we don't know if
we have run into (superfluous but allowed) zero padding. */
if (!read_ctx_need_data (&ctx, 4)
&& check_zero_padding (&ctx, mc_info | mc_header, &where))
break;
/* CU length. */
if (!read_ctx_read_4ubyte (&ctx, &size32))
{
wr_error (&where, ": can't read CU length.\n");
success = false;
break;
}
if (size32 == 0 && check_zero_padding (&ctx, mc_info | mc_header, &where))
break;
if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where))
{
success = false;
break;
}
if (!read_ctx_need_data (&ctx, size))
{
wr_error (&where,
": section doesn't have enough data"
" to read CU of size %" PRIx64 ".\n", size);
ctx.ptr = ctx.end;
success = false;
break;
}
const unsigned char *cu_end = ctx.ptr + size;
cur->length = cu_end - cu_begin; // Length including the length field.
/* version + debug_abbrev_offset + address_size */
uint64_t cu_header_size = 2 + (dwarf_64 ? 8 : 4) + 1;
if (size < cu_header_size)
{
wr_error (&where, ": claimed length of %" PRIx64
" doesn't even cover CU header.\n", size);
success = false;
break;
}
else
{
/* Make CU context begin just before the CU length, so that DIE
offsets are computed correctly. */
struct read_ctx cu_ctx;
if (!read_ctx_init_sub (&cu_ctx, &ctx, cu_begin, cu_end))
{
not_enough:
wr_error (&where, PRI_NOT_ENOUGH, "next CU");
success = false;
break;
}
cu_ctx.ptr = ctx.ptr;
if (!check_cu_structural (&cu_ctx, cur, abbrev_chain,
strings, dwarf_64,
strings_coverage, reloc, data->file))
{
success = false;
break;
}
if (cu_ctx.ptr != cu_ctx.end
&& !check_zero_padding (&cu_ctx, mc_info, &where))
wr_message_padding_n0 (mc_info, &where,
read_ctx_get_offset (&ctx),
read_ctx_get_offset (&ctx) + size - 1);
}
if (!read_ctx_skip (&ctx, size))
goto not_enough;
}
if (success)
{
if (ctx.ptr != ctx.end)
/* Did we read up everything? */
wr_message (mc_die_other | mc_impact_4,
&WHERE (sec_info, NULL),
": CU lengths don't exactly match Elf_Data contents.");
else
/* Did we consume all the relocations? */
relocation_skip_rest (data);
}
int address_size = 0;
if (cu_chain != NULL)
{
uint64_t offset = 0;
for (struct cu *it = cu_chain; it != NULL; it = it->next)
if (address_size == 0)
{
address_size = it->address_size;
offset = it->where.addr1;
}
else if (address_size != it->address_size)
{
/* XXX would be nice to check consistency of CU address
size declared in various other .debug_* sections. */
wr_message (mc_info, &it->where,
": has different address size than CU 0x%"
PRIx64 ".\n", offset);
address_size = 0;
break;
}
}
bool references_sound = check_global_die_references (cu_chain);
ref_record_free (&die_refs);
if (strings_coverage != NULL)
{
if (success)
coverage_find_holes (strings_coverage, 0, strings->d_size, found_hole,
&((struct hole_info)
{sec_str, mc_strings, 0, strings->d_buf}));
coverage_free (strings_coverage);
}
if (!success || !references_sound)
{
cu_free (cu_chain);
cu_chain = NULL;
}
/* Reverse the chain, so that it's organized "naturally". Has
significant impact on performance when handling loc_ref and
range_ref fields in loc/range validation. */
struct cu *last = NULL;
for (struct cu *it = cu_chain; it != NULL; )
{
struct cu *next = it->next;
it->next = last;
last = it;
it = next;
}
cu_chain = last;
return cu_chain;
}
static struct coverage_map *
coverage_map_alloc_XA (struct elf_file *elf, bool allow_overlap)
{
struct coverage_map *ret = xmalloc (sizeof (*ret));
if (!coverage_map_init (ret, elf,
SHF_EXECINSTR | SHF_ALLOC,
SHF_ALLOC,
allow_overlap))
{
free (ret);
return NULL;
}
return ret;
}
static void
coverage_map_free_XA (struct coverage_map *coverage_map)
{
if (coverage_map != NULL)
{
coverage_map_free (coverage_map);
free (coverage_map);
}
}
static bool
check_aranges_structural (struct section_data *data, struct cu *cu_chain)
{
struct read_ctx ctx;
read_ctx_init (&ctx, data->file->dwarf, data->data);
bool retval = true;
struct coverage_map *coverage_map;
if ((coverage_map = coverage_map_alloc_XA (data->file, false)) == NULL)
{
wr_error (&WHERE (sec_aranges, NULL),
": couldn't read ELF, skipping coverage analysis.\n");
retval = false;
}
while (!read_ctx_eof (&ctx))
{
struct where where = WHERE (sec_aranges, NULL);
where_reset_1 (&where, read_ctx_get_offset (&ctx));
const unsigned char *atab_begin = ctx.ptr;
/* Size. */
uint32_t size32;
uint64_t size;
bool dwarf_64;
if (!read_ctx_read_4ubyte (&ctx, &size32))
{
wr_error (&where, ": can't read table length.\n");
return false;
}
if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where))
return false;
struct read_ctx sub_ctx;
const unsigned char *atab_end = ctx.ptr + size;
if (!read_ctx_init_sub (&sub_ctx, &ctx, atab_begin, atab_end))
{
not_enough:
wr_error (&where, PRI_NOT_ENOUGH, "next table");
return false;
}
sub_ctx.ptr = ctx.ptr;
/* Version. */
uint16_t version;
if (!read_ctx_read_2ubyte (&sub_ctx, &version))
{
wr_error (&where, ": can't read version.\n");
retval = false;
goto next;
}
if (!supported_version (version, 1, &where, 2))
{
retval = false;
goto next;
}
/* CU offset. */
uint64_t cu_offset;
uint64_t ctx_offset = sub_ctx.ptr - ctx.begin;
if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_offset))
{
wr_error (&where, ": can't read debug info offset.\n");
retval = false;
goto next;
}
struct relocation *rel;
if ((rel = relocation_next (&data->rel, ctx_offset,
&where, skip_mismatched)))
relocate_one (&data->rel, rel, dwarf_64 ? 8 : 4,
&cu_offset, &where, sec_info, NULL);
else if (data->file->ehdr.e_type == ET_REL)
wr_message (mc_impact_2 | mc_aranges | mc_reloc | mc_header, &where,
PRI_LACK_RELOCATION, "debug info offset");
struct cu *cu = NULL;
if (cu_chain != NULL && (cu = cu_find_cu (cu_chain, cu_offset)) == NULL)
wr_error (&where, ": unresolved reference to " PRI_CU ".\n", cu_offset);
struct where where_cudie;
if (cu != NULL)
{
where_cudie = WHERE (sec_info, NULL);
where_reset_1 (&where_cudie, cu->cudie_offset);
where.ref = &where_cudie;
where_cudie.formatting = wf_cudie;
if (cu->has_arange)
wr_message (mc_impact_2 | mc_aranges | mc_header, &where,
": there has already been arange section for this CU.\n");
else
cu->has_arange = true;
}
/* Address size. */
uint8_t address_size;
if (!read_ctx_read_ubyte (&sub_ctx, &address_size))
{
wr_error (&where, ": can't read address size.\n");
retval = false;
goto next;
}
if (cu != NULL)
{
if (address_size != cu->address_size)
{
wr_error (&where,
": address size %d doesn't match referred CU.\n",
address_size);
retval = false;
}
}
/* Try to parse it anyway, unless the address size is wacky. */
else if (address_size != 4 && address_size != 8)
{
wr_error (&where, ": invalid address size: %d.\n", address_size);
retval = false;
goto next;
}
/* Segment size. */
uint8_t segment_size;
if (!read_ctx_read_ubyte (&sub_ctx, &segment_size))
{
wr_error (&where, ": can't read unit segment size.\n");
retval = false;
goto next;
}
if (segment_size != 0)
{
wr_warning (&where, ": dwarflint can't handle segment_size != 0.\n");
retval = false;
goto next;
}
/* 7.20: The first tuple following the header in each set begins
at an offset that is a multiple of the size of a single tuple
(that is, twice the size of an address). The header is
padded, if necessary, to the appropriate boundary. */
const uint8_t tuple_size = 2 * address_size;
uint64_t off = read_ctx_get_offset (&sub_ctx);
if ((off % tuple_size) != 0)
{
uint64_t noff = ((off / tuple_size) + 1) * tuple_size;
for (uint64_t i = off; i < noff; ++i)
{
uint8_t c;
if (!read_ctx_read_ubyte (&sub_ctx, &c))
{
wr_error (&where,
": section ends after the header, but before the first entry.\n");
retval = false;
goto next;
}
if (c != 0)
wr_message (mc_impact_2 | mc_aranges | mc_header, &where,
": non-zero byte at 0x%" PRIx64
" in padding before the first entry.\n",
read_ctx_get_offset (&sub_ctx));
}
}
assert ((read_ctx_get_offset (&sub_ctx) % tuple_size) == 0);
while (!read_ctx_eof (&sub_ctx))
{
/* We would like to report aranges the same way that readelf
does. But readelf uses index of the arange in the array
as returned by dwarf_getaranges, which sorts the aranges
beforehand. We don't want to disturb the memory this
way, the better to catch structural errors accurately.
So report arange offset instead. If this becomes a
problem, we will achieve this by two-pass analysis. */
where_reset_2 (&where, read_ctx_get_offset (&sub_ctx));
/* Record address. */
uint64_t address;
ctx_offset = sub_ctx.ptr - ctx.begin;
bool address_relocated = false;
if (!read_ctx_read_var (&sub_ctx, address_size, &address))
{
wr_error (&where, ": can't read address field.\n");
retval = false;
goto next;
}
if ((rel = relocation_next (&data->rel, ctx_offset,
&where, skip_mismatched)))
{
address_relocated = true;
relocate_one (&data->rel, rel, address_size,
&address, &where, rel_address, NULL);
}
else if (data->file->ehdr.e_type == ET_REL
&& address != 0)
wr_message (mc_impact_2 | mc_aranges | mc_reloc, &where,
PRI_LACK_RELOCATION, "address field");
/* Record length. */
uint64_t length;
if (!read_ctx_read_var (&sub_ctx, address_size, &length))
{
wr_error (&where, ": can't read length field.\n");
retval = false;
goto next;
}
if (address == 0 && length == 0 && !address_relocated)
break;
if (length == 0)
/* DWARF 3 spec, 6.1.2 Lookup by Address: Each descriptor
is a pair consisting of the beginning address [...],
followed by the _non-zero_ length of that range. */
wr_error (&where, ": zero-length address range.\n");
else if (retval)
/* Skip coverage analysis if we have errors. */
coverage_map_add (coverage_map, address, length, &where,
mc_aranges);
}
if (sub_ctx.ptr != sub_ctx.end
&& !check_zero_padding (&sub_ctx, mc_pubtables,
&WHERE (where.section, NULL)))
{
wr_message_padding_n0 (mc_pubtables | mc_error,
&WHERE (where.section, NULL),
read_ctx_get_offset (&sub_ctx),
read_ctx_get_offset (&sub_ctx) + size - 1);
retval = false;
}
next:
if (!read_ctx_skip (&ctx, size))
/* A "can't happen" error. */
goto not_enough;
}
if (retval && coverage_map != NULL)
coverage_map_find_holes (coverage_map, &coverage_map_found_hole,
&(struct coverage_map_hole_info)
{{sec_aranges, mc_aranges, 0, NULL},
coverage_map->elf});
coverage_map_free_XA (coverage_map);
return retval;
}
static bool
check_pub_structural (struct section_data *data,
struct cu *cu_chain)
{
struct read_ctx ctx;
read_ctx_init (&ctx, data->file->dwarf, data->data);
bool retval = true;
while (!read_ctx_eof (&ctx))
{
struct where where = WHERE_SECDATA (data, NULL);
where_reset_1 (&where, read_ctx_get_offset (&ctx));
const unsigned char *set_begin = ctx.ptr;
/* Size. */
uint32_t size32;
uint64_t size;
bool dwarf_64;
if (!read_ctx_read_4ubyte (&ctx, &size32))
{
wr_error (&where, ": can't read table length.\n");
return false;
}
if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where))
return false;
struct read_ctx sub_ctx;
const unsigned char *set_end = ctx.ptr + size;
if (!read_ctx_init_sub (&sub_ctx, &ctx, set_begin, set_end))
{
not_enough:
wr_error (&where, PRI_NOT_ENOUGH, "next set");
return false;
}
sub_ctx.ptr = ctx.ptr;
/* Version. */
uint16_t version;
if (!read_ctx_read_2ubyte (&sub_ctx, &version))
{
wr_error (&where, ": can't read set version.\n");
retval = false;
goto next;
}
if (!supported_version (version, 1, &where, 2))
{
retval = false;
goto next;
}
/* CU offset. */
uint64_t cu_offset; /* Offset of related CU. */
uint64_t ctx_offset = sub_ctx.ptr - ctx.begin;
if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_offset))
{
wr_error (&where, ": can't read debug info offset.\n");
retval = false;
goto next;
}
struct relocation *rel;
if ((rel = relocation_next (&data->rel, ctx_offset,
&where, skip_mismatched)))
relocate_one (&data->rel, rel, dwarf_64 ? 8 : 4,
&cu_offset, &where, sec_info, NULL);
else if (data->file->ehdr.e_type == ET_REL)
wr_message (mc_impact_2 | mc_pubtables | mc_reloc | mc_header, &where,
PRI_LACK_RELOCATION, "debug info offset");
struct cu *cu = NULL;
enum section_id sec = data_get_sec (data)->id;
if (cu_chain != NULL && (cu = cu_find_cu (cu_chain, cu_offset)) == NULL)
wr_error (&where, ": unresolved reference to " PRI_CU ".\n", cu_offset);
if (cu != NULL)
{
where.ref = &cu->where;
bool *has = sec == sec_pubnames
? &cu->has_pubnames : &cu->has_pubtypes;
if (*has)
wr_message (mc_impact_2 | mc_pubtables | mc_header, &where,
": there has already been section for this CU.\n");
else
*has = true;
}
/* Covered length. */
uint64_t cu_len;
if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &cu_len))
{
wr_error (&where, ": can't read covered length.\n");
retval = false;
goto next;
}
if (cu != NULL && cu_len != cu->length)
{
wr_error (&where,
": the table covers length %" PRId64
" but CU has length %" PRId64 ".\n", cu_len, cu->length);
retval = false;
goto next;
}
/* Records... */
while (!read_ctx_eof (&sub_ctx))
{
ctx_offset = sub_ctx.ptr - ctx.begin;
where_reset_2 (&where, ctx_offset);
uint64_t offset;
if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &offset))
{
wr_error (&where, ": can't read offset field.\n");
retval = false;
goto next;
}
if (offset == 0)
break;
if (cu != NULL
&& !addr_record_has_addr (&cu->die_addrs, offset + cu->offset))
{
wr_error (&where,
": unresolved reference to " PRI_DIE ".\n", offset);
retval = false;
goto next;
}
uint8_t c;
do
if (!read_ctx_read_ubyte (&sub_ctx, &c))
{
wr_error (&where, ": can't read symbol name.\n");
retval = false;
goto next;
}
while (c);
}
if (sub_ctx.ptr != sub_ctx.end
&& !check_zero_padding (&sub_ctx, mc_pubtables,
&WHERE (sec, NULL)))
{
wr_message_padding_n0 (mc_pubtables | mc_error,
&WHERE (sec, NULL),
read_ctx_get_offset (&sub_ctx),
read_ctx_get_offset (&sub_ctx) + size - 1);
retval = false;
}
next:
if (!read_ctx_skip (&ctx, size))
goto not_enough;
}
if (retval)
relocation_skip_rest (data);
return retval;
}
/* Operands are passed back as attribute forms. In particular,
DW_FORM_dataX for X-byte operands, DW_FORM_[us]data for
ULEB128/SLEB128 operands, and DW_FORM_addr for 32b/64b operands.
If the opcode takes no operands, 0 is passed.
Return value is false if we couldn't determine (i.e. invalid
opcode).
*/
static bool
get_location_opcode_operands (uint8_t opcode, uint8_t *op1, uint8_t *op2)
{
switch (opcode)
{
#define DEF_DW_OP(OPCODE, OP1, OP2) \
case OPCODE: *op1 = OP1; *op2 = OP2; return true;
# include "expr_opcodes.h"
#undef DEF_DW_OP
default:
return false;
};
}
static bool
check_location_expression (struct read_ctx *parent_ctx,
uint64_t init_off,
struct relocation_data *reloc,
size_t length,
struct where *wh,
bool addr_64)
{
struct read_ctx ctx;
if (!read_ctx_init_sub (&ctx, parent_ctx, parent_ctx->ptr,
parent_ctx->ptr + length))
{
wr_error (wh, PRI_NOT_ENOUGH, "location expression");
return false;
}
struct ref_record oprefs;
WIPE (oprefs);
struct addr_record opaddrs;
WIPE (opaddrs);
while (!read_ctx_eof (&ctx))
{
struct where where = WHERE (sec_locexpr, wh);
uint64_t opcode_off = read_ctx_get_offset (&ctx) + init_off;
where_reset_1 (&where, opcode_off);
addr_record_add (&opaddrs, opcode_off);
uint8_t opcode;
if (!read_ctx_read_ubyte (&ctx, &opcode))
{
wr_error (&where, ": can't read opcode.\n");
break;
}
uint8_t op1, op2;
if (!get_location_opcode_operands (opcode, &op1, &op2))
{
wr_error (&where, ": can't decode opcode \"%s\".\n",
dwarf_locexpr_opcode_string (opcode));
break;
}
#define READ_FORM(OP, STR, PTR) \
do { \
if (OP != 0) \
{ \
uint64_t _off = read_ctx_get_offset (&ctx) + init_off; \
uint64_t *_ptr = (PTR); \
if (!read_ctx_read_form (&ctx, addr_64, (OP), \
_ptr, &where, STR " operand")) \
{ \
wr_error (&where, ": opcode \"%s\"" \
": can't read " STR " operand (form \"%s\").\n", \
dwarf_locexpr_opcode_string (opcode), \
dwarf_form_string ((OP))); \
goto out; \
} \
struct relocation *_rel; \
if ((_rel = relocation_next (reloc, _off, \
&where, skip_mismatched))) \
relocate_one (reloc, _rel, \
addr_64 ? 8 : 4, _ptr, &where, \
reloc_target_loc (opcode), NULL); \
} \
} while (0)
uint64_t value1, value2;
READ_FORM (op1, "1st", &value1);
READ_FORM (op2, "2st", &value2);
#undef READ_FORM
switch (opcode)
{
case DW_OP_bra:
case DW_OP_skip:
{
int16_t skip = (uint16_t)value1;
if (skip == 0)
wr_message (mc_loc | mc_acc_bloat | mc_impact_3, &where,
": %s with skip 0.\n",
dwarf_locexpr_opcode_string (opcode));
else if (skip > 0 && !read_ctx_need_data (&ctx, (size_t)skip))
wr_error (&where, ": %s branches out of location expression.\n",
dwarf_locexpr_opcode_string (opcode));
/* Compare with the offset after the two-byte skip value. */
else if (skip < 0 && ((uint64_t)-skip) > read_ctx_get_offset (&ctx))
wr_error (&where,
": %s branches before the beginning of location expression.\n",
dwarf_locexpr_opcode_string (opcode));
else
ref_record_add (&oprefs, opcode_off + skip, &where);
break;
}
case DW_OP_const8u:
case DW_OP_const8s:
if (!addr_64)
wr_error (&where, ": %s on 32-bit machine.\n",
dwarf_locexpr_opcode_string (opcode));
break;
default:
if (!addr_64
&& (opcode == DW_OP_constu
|| opcode == DW_OP_consts
|| opcode == DW_OP_deref_size
|| opcode == DW_OP_plus_uconst)
&& (value1 > (uint64_t)(uint32_t)-1))
wr_message (mc_loc | mc_acc_bloat | mc_impact_3, &where,
": %s with operand %#" PRIx64 " on 32-bit machine.\n",
dwarf_locexpr_opcode_string (opcode), value1);
};
}
out:
for (size_t i = 0; i < oprefs.size; ++i)
{
struct ref *ref = oprefs.refs + i;
if (!addr_record_has_addr (&opaddrs, ref->addr))
wr_error (&ref->who,
": unresolved reference to opcode at %#" PRIx64 ".\n",
ref->addr);
}
addr_record_free (&opaddrs);
ref_record_free (&oprefs);
return true;
}
static bool
check_loc_or_range_ref (const struct read_ctx *parent_ctx,
struct cu *cu,
struct section_data *data,
struct coverage *coverage,
struct coverage_map *coverage_map,
uint64_t addr,
bool addr_64,
struct where *wh,
enum message_category cat)
{
struct read_ctx ctx;
read_ctx_init (&ctx, parent_ctx->dbg, parent_ctx->data);
enum section_id sec = data_get_sec (data)->id;
assert (sec == sec_loc || sec == sec_ranges);
assert (cat == mc_loc || cat == mc_ranges);
assert ((sec == sec_loc) == (cat == mc_loc));
assert (coverage != NULL);
if (!read_ctx_skip (&ctx, addr))
{
wr_error (wh, ": invalid reference outside the section "
"%#" PRIx64 ", size only %#tx.\n",
addr, ctx.end - ctx.begin);
return false;
}
bool retval = true;
bool contains_locations = sec == sec_loc;
if (coverage_is_covered (coverage, addr))
{
wr_error (wh, ": reference to %#" PRIx64
" points at the middle of location or range list.\n", addr);
retval = false;
}
uint64_t escape = addr_64 ? (uint64_t)-1 : (uint64_t)(uint32_t)-1;
bool overlap = false;
uint64_t base = cu->base;
while (!read_ctx_eof (&ctx))
{
struct where where = WHERE (sec, wh);
where_reset_1 (&where, read_ctx_get_offset (&ctx));
#define HAVE_OVERLAP \
do { \
wr_error (&where, ": range definitions overlap.\n"); \
retval = false; \
overlap = true; \
} while (0)
/* begin address */
uint64_t begin_addr;
uint64_t begin_off = read_ctx_get_offset (&ctx);
GElf_Sym begin_symbol_mem, *begin_symbol = &begin_symbol_mem;
bool begin_relocated = false;
if (!overlap
&& !coverage_pristine (coverage, begin_off, addr_64 ? 8 : 4))
HAVE_OVERLAP;
if (!read_ctx_read_offset (&ctx, addr_64, &begin_addr))
{
wr_error (&where, ": can't read address range beginning.\n");
return false;
}
struct relocation *rel;
if ((rel = relocation_next (&data->rel, begin_off,
&where, skip_mismatched)))
{
begin_relocated = true;
relocate_one (&data->rel, rel, addr_64 ? 8 : 4,
&begin_addr, &where, rel_value, &begin_symbol);
}
/* end address */
uint64_t end_addr;
uint64_t end_off = read_ctx_get_offset (&ctx);
GElf_Sym end_symbol_mem, *end_symbol = &end_symbol_mem;
bool end_relocated = false;
if (!overlap
&& !coverage_pristine (coverage, end_off, addr_64 ? 8 : 4))
HAVE_OVERLAP;
if (!read_ctx_read_offset (&ctx, addr_64, &end_addr))
{
wr_error (&where, ": can't read address range ending.\n");
return false;
}
if ((rel = relocation_next (&data->rel, end_off,
&where, skip_mismatched)))
{
end_relocated = true;
relocate_one (&data->rel, rel, addr_64 ? 8 : 4,
&end_addr, &where, rel_value, &end_symbol);
if (begin_addr != escape)
{
if (!begin_relocated)
wr_message (cat | mc_impact_2 | mc_reloc, &where,
": end of address range is relocated, but the beginning wasn't.\n");
else
check_range_relocations (cat, &where,
data->file,
begin_symbol, end_symbol,
"begin and end address");
}
}
else if (begin_relocated)
wr_message (cat | mc_impact_2 | mc_reloc, &where,
": end of address range is not relocated, but the beginning was.\n");
bool done = false;
if (begin_addr == 0 && end_addr == 0 && !begin_relocated && !end_relocated)
done = true;
else if (begin_addr != escape)
{
if (base == (uint64_t)-1)
{
wr_error (&where,
": address range with no base address set (%#"
PRIx64 "..%#" PRIx64 ").\n", begin_addr, end_addr);
/* This is not something that would derail high-level,
so carry on. */
}
if (end_addr < begin_addr)
wr_message (cat | mc_error, &where,
": has negative range 0x%" PRIx64 "..0x%" PRIx64 ".\n",
begin_addr, end_addr);
else if (begin_addr == end_addr)
/* 2.6.6: A location list entry [...] whose beginning
and ending addresses are equal has no effect. */
wr_message (cat | mc_acc_bloat | mc_impact_3, &where,
": entry covers no range.\n");
/* Skip coverage analysis if we have errors or have no base
(or just don't do coverage analysis at all). */
else if (base < (uint64_t)-2 && retval && coverage_map != NULL)
{
uint64_t address = begin_addr + base;
uint64_t length = end_addr - begin_addr;
coverage_map_add (coverage_map, address, length, &where, cat);
}
if (contains_locations)
{
/* location expression length */
uint16_t len;
if (!overlap
&& !coverage_pristine (coverage,
read_ctx_get_offset (&ctx), 2))
HAVE_OVERLAP;
if (!read_ctx_read_2ubyte (&ctx, &len))
{
wr_error (&where, ": can't read length of location expression.\n");
return false;
}
/* location expression itself */
uint64_t expr_start = read_ctx_get_offset (&ctx);
if (!check_location_expression (&ctx, expr_start, &data->rel,
len, &where, addr_64))
return false;
uint64_t expr_end = read_ctx_get_offset (&ctx);
if (!overlap
&& !coverage_pristine (coverage,
expr_start, expr_end - expr_start))
HAVE_OVERLAP;
if (!read_ctx_skip (&ctx, len))
{
/* "can't happen" */
wr_error (&where, PRI_NOT_ENOUGH, "location expression");
return false;
}
}
}
else
{
if (end_addr == base)
wr_message (cat | mc_acc_bloat | mc_impact_3, &where,
": base address selection doesn't change base address"
" (%#" PRIx64 ").\n", base);
else
base = end_addr;
}
#undef HAVE_OVERLAP
coverage_add (coverage, where.addr1, read_ctx_get_offset (&ctx) - where.addr1);
if (done)
break;
}
return retval;
}
static bool
check_loc_or_range_structural (struct section_data *data,
struct cu *cu_chain)
{
enum section_id sec = data_get_sec (data)->id;
assert (sec == sec_loc || sec == sec_ranges);
assert (cu_chain != NULL);
struct read_ctx ctx;
read_ctx_init (&ctx, data->file->dwarf, data->data);
bool retval = true;
struct coverage_map *coverage_map = NULL;
#ifdef FIND_SECTION_HOLES
if ((coverage_map = coverage_map_alloc_XA (ctx.dbg->elf,
data->sec == sec_loc)) == NULL)
{
wr_error (&WHERE (data->sec, NULL),
": couldn't read ELF, skipping coverage analysis.\n");
retval = false;
}
#endif
struct coverage coverage;
WIPE (coverage);
enum message_category cat = sec == sec_loc ? mc_loc : mc_ranges;
/* Relocation checking in the followings assumes that all the
references are organized in monotonously increasing order. That
doesn't have to be the case. So merge all the references into
them into one sorted array. */
size_t size = 0;
for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next)
{
struct ref_record *rec
= sec == sec_loc ? &cu->loc_refs : &cu->range_refs;
size += rec->size;
}
struct ref_cu
{
struct ref ref;
struct cu *cu;
};
struct ref_cu *refs = xmalloc (sizeof (*refs) * size);
struct ref_cu *refptr = refs;
for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next)
{
struct ref_record *rec
= sec == sec_loc ? &cu->loc_refs : &cu->range_refs;
for (size_t i = 0; i < rec->size; ++i)
*refptr++ = ((struct ref_cu){.ref = rec->refs[i], .cu = cu});
}
int compare_refs (const void *a, const void *b)
{
const struct ref_cu *ref_a = (const struct ref_cu *)a;
const struct ref_cu *ref_b = (const struct ref_cu *)b;
if (ref_a->ref.addr > ref_b->ref.addr)
return 1;
else if (ref_a->ref.addr < ref_b->ref.addr)
return -1;
else
return 0;
}
qsort (refs, size, sizeof (*refs), compare_refs);
uint64_t last_off = 0;
for (size_t i = 0; i < size; ++i)
{
uint64_t off = refs[i].ref.addr;
if (i > 0)
{
if (off == last_off)
continue;
relocation_skip (&data->rel, off,
&WHERE (sec, NULL), skip_unref);
}
if (!check_loc_or_range_ref (&ctx, refs[i].cu, data,
&coverage, coverage_map,
off, refs[i].cu->address_size == 8,
&refs[i].ref.who, cat))
retval = false;
last_off = off;
}
if (retval)
{
relocation_skip_rest (data);
/* We check that all CUs have the same address size when building
the CU chain. So just take the address size of the first CU in
chain. */
coverage_find_holes (&coverage, 0, ctx.data->d_size, found_hole,
&((struct hole_info)
{sec, cat, cu_chain->address_size,
ctx.data->d_buf}));
if (coverage_map)
coverage_map_find_holes (coverage_map, &coverage_map_found_hole,
&(struct coverage_map_hole_info)
{{sec, cat, 0, NULL},
coverage_map->elf});
}
coverage_free (&coverage);
coverage_map_free_XA (coverage_map);
return retval;
}
static GElf_Rela *
get_rel_or_rela (Elf_Data *data, int ndx,
GElf_Rela *dst, size_t type)
{
if (type == SHT_RELA)
return gelf_getrela (data, ndx, dst);
else
{
assert (type == SHT_REL);
GElf_Rel rel_mem;
if (gelf_getrel (data, ndx, &rel_mem) == NULL)
return NULL;
dst->r_offset = rel_mem.r_offset;
dst->r_info = rel_mem.r_info;
dst->r_addend = 0;
return dst;
}
}
static bool
read_rel (struct section_data *secdata, Elf_Data *reldata, bool elf_64)
{
assert (secdata->rel.type == SHT_REL
|| secdata->rel.type == SHT_RELA);
bool is_rela = secdata->rel.type == SHT_RELA;
struct read_ctx ctx;
read_ctx_init (&ctx, secdata->file->dwarf, secdata->data);
size_t entrysize
= elf_64
? (is_rela ? sizeof (Elf64_Rela) : sizeof (Elf64_Rel))
: (is_rela ? sizeof (Elf32_Rela) : sizeof (Elf32_Rel));
size_t count = reldata->d_size / entrysize;
struct where parent = WHERE_SECDATA (secdata, NULL);
struct where where = WHERE (is_rela ? sec_rela : sec_rel, NULL);
where.ref = &parent;
for (unsigned i = 0; i < count; ++i)
{
where_reset_1 (&where, i);
REALLOC (&secdata->rel, rel);
struct relocation *cur = secdata->rel.rel + secdata->rel.size++;
WIPE (*cur);
GElf_Rela rela_mem, *rela
= get_rel_or_rela (reldata, i, &rela_mem, secdata->rel.type);
if (rela == NULL)
{
wr_error (&where, ": couldn't read relocation.\n");
skip:
cur->invalid = true;
continue;
}
int cur_type = GELF_R_TYPE (rela->r_info);
if (cur_type == 0) /* No relocation. */
{
wr_message (mc_impact_3 | mc_reloc | mc_acc_bloat, &where,
": NONE relocation is superfluous.\n");
goto skip;
}
cur->offset = rela->r_offset;
cur->symndx = GELF_R_SYM (rela->r_info);
cur->type = cur_type;
where_reset_2 (&where, cur->offset);
Elf_Type type = ebl_reloc_simple_type (secdata->file->ebl, cur->type);
int width;
switch (type)
{
case ELF_T_WORD:
case ELF_T_SWORD:
width = 4;
break;
case ELF_T_XWORD:
case ELF_T_SXWORD:
width = 8;
break;
case ELF_T_BYTE:
case ELF_T_HALF:
/* Technically legal, but never used. Better have dwarflint
flag them as erroneous, because it's more likely these
are a result of a bug than actually being used. */
{
char buf[64];
wr_error (&where, ": 8 or 16-bit relocation type %s.\n",
ebl_reloc_type_name (secdata->file->ebl,
cur->type, buf, sizeof (buf)));
goto skip;
}
default:
{
char buf[64];
wr_error (&where, ": invalid relocation %d (%s).\n",
cur->type,
ebl_reloc_type_name (secdata->file->ebl,
cur->type, buf, sizeof (buf)));
goto skip;
}
};
if (cur->offset + width >= secdata->data->d_size)
{
wr_error (&where,
": relocation doesn't fall into relocated section.\n");
goto skip;
}
uint64_t value;
if (width == 4)
value = read_4ubyte_unaligned (secdata->file->dwarf,
secdata->data->d_buf + cur->offset);
else
{
assert (width == 8);
value = read_8ubyte_unaligned (secdata->file->dwarf,
secdata->data->d_buf + cur->offset);
}
if (is_rela)
{
if (value != 0)
wr_message (mc_impact_2 | mc_reloc, &where,
": SHR_RELA relocates a place with non-zero value (addend=%#"
PRIx64", value=%#"PRIx64").\n", rela->r_addend, value);
cur->addend = rela->r_addend;
}
else
cur->addend = value;
}
/* Sort the reloc section so that the applicable addresses of
relocation entries are monotonously increasing. */
int compare (const void *a, const void *b)
{
return ((struct relocation *)a)->offset
- ((struct relocation *)b)->offset;
}
qsort (secdata->rel.rel, secdata->rel.size,
sizeof (*secdata->rel.rel), &compare);
return true;
}
static bool
check_line_structural (struct section_data *data,
struct cu *cu_chain)
{
struct read_ctx ctx;
read_ctx_init (&ctx, data->file->dwarf, data->data);
bool retval = true;
struct addr_record line_tables;
WIPE (line_tables);
while (!read_ctx_eof (&ctx))
{
struct where where = WHERE_SECDATA (data, NULL);
uint64_t set_offset = read_ctx_get_offset (&ctx);
where_reset_1 (&where, set_offset);
addr_record_add (&line_tables, set_offset);
const unsigned char *set_begin = ctx.ptr;
/* Size. */
uint32_t size32;
uint64_t size;
bool dwarf_64;
if (!read_ctx_read_4ubyte (&ctx, &size32))
{
wr_error (&where, ": can't read table length.\n");
return false;
}
if (!read_size_extra (&ctx, size32, &size, &dwarf_64, &where))
return false;
struct read_ctx sub_ctx;
const unsigned char *set_end = ctx.ptr + size;
if (!read_ctx_init_sub (&sub_ctx, &ctx, set_begin, set_end))
{
not_enough:
wr_error (&where, PRI_NOT_ENOUGH, "next unit");
return false;
}
sub_ctx.ptr = ctx.ptr;
sub_ctx.begin = ctx.begin;
{
/* Version. */
uint16_t version;
if (!read_ctx_read_2ubyte (&sub_ctx, &version))
{
wr_error (&where, ": can't read set version.\n");
skip:
retval = false;
goto next;
}
if (!supported_version (version, 2, &where, 2, 3))
goto skip;
/* Header length. */
uint64_t header_length;
if (!read_ctx_read_offset (&sub_ctx, dwarf_64, &header_length))
{
wr_error (&where, ": can't read attribute value.\n");
goto skip;
}
const unsigned char *program_start = sub_ctx.ptr + header_length;
/* Minimum instruction length. */
uint8_t minimum_i_length;
if (!read_ctx_read_ubyte (&sub_ctx, &minimum_i_length))
{
wr_error (&where, ": can't read minimum instruction length.\n");
goto skip;
}
/* Default value of is_stmt. */
uint8_t default_is_stmt;
if (!read_ctx_read_ubyte (&sub_ctx, &default_is_stmt))
{
wr_error (&where, ": can't read default_is_stmt.\n");
goto skip;
}
/* 7.21: The boolean values "true" and "false" used by the line
number information program are encoded as a single byte
containing the value 0 for "false," and a non-zero value for
"true." [But give a notice if it's not 0 or 1.] */
if (default_is_stmt != 0
&& default_is_stmt != 1)
wr_message (mc_line | mc_impact_2 | mc_header, &where,
": default_is_stmt should be 0 or 1, not %ud\n",
default_is_stmt);
/* Line base. */
int8_t line_base;
if (!read_ctx_read_ubyte (&sub_ctx, (uint8_t *)&line_base))
{
wr_error (&where, ": can't read line_base.\n");
goto skip;
}
/* Line range. */
uint8_t line_range;
if (!read_ctx_read_ubyte (&sub_ctx, &line_range))
{
wr_error (&where, ": can't read line_range.\n");
goto skip;
}
/* Opcode base. */
uint8_t opcode_base;
if (!read_ctx_read_ubyte (&sub_ctx, &opcode_base))
{
wr_error (&where, ": can't read opcode_base.\n");
goto skip;
}
/* Standard opcode lengths. */
if (opcode_base == 0)
{
wr_error (&where, ": opcode base set to 0.\n");
opcode_base = 1; // so that in following, our -1s don't underrun
}
uint8_t std_opc_lengths[opcode_base - 1]; /* -1, opcodes go from 1. */
for (unsigned i = 0; i < (unsigned)(opcode_base - 1); ++i)
if (!read_ctx_read_ubyte (&sub_ctx, std_opc_lengths + i))
{
wr_error (&where,
": can't read length of standard opcode #%d.\n", i);
goto skip;
}
/* Include directories. */
struct include_directory_t
{
const char *name;
bool used;
};
struct include_directories_t
{
size_t size;
size_t alloc;
struct include_directory_t *dirs;
} include_directories;
WIPE (include_directories);
while (!read_ctx_eof (&sub_ctx))
{
const char *name = read_ctx_read_str (&sub_ctx);
if (name == NULL)
{
wr_error (&where,
": can't read name of include directory #%zd.\n",
include_directories.size + 1); /* Numbered from 1. */
goto skip;
}
if (*name == 0)
break;
REALLOC (&include_directories, dirs);
include_directories.dirs[include_directories.size++] =
(struct include_directory_t){name, false};
}
/* File names. */
struct file_t
{
const char *name;
uint64_t dir_idx;
bool used;
};
struct files_t
{
size_t size;
size_t alloc;
struct file_t *files;
} files;
WIPE (files);
/* Directory index. */
bool read_directory_index (const char *name, uint64_t *ptr)
{
if (!checked_read_uleb128 (&sub_ctx, ptr,
&where, "directory index"))
return false;
if (*name == '/' && *ptr != 0)
wr_message (mc_impact_2 | mc_line | mc_header, &where,
": file #%zd has absolute pathname, but refers to directory != 0.\n",
files.size + 1);
if (*ptr > include_directories.size) /* Not >=, dirs indexed from 1. */
{
wr_message (mc_impact_4 | mc_line | mc_header, &where,
": file #%zd refers to directory #%" PRId64 ", which wasn't defined.\n",
files.size + 1, *ptr);
/* Consumer might choke on that. */
retval = false;
}
else if (*ptr != 0)
include_directories.dirs[*ptr - 1].used = true;
return true;
}
while (1)
{
const char *name = read_ctx_read_str (&sub_ctx);
if (name == NULL)
{
wr_error (&where,
": can't read name of file #%zd.\n",
files.size + 1); /* Numbered from 1. */
goto skip;
}
if (*name == 0)
break;
uint64_t dir_idx;
if (!read_directory_index (name, &dir_idx))
goto skip;
/* Time of last modification. */
uint64_t timestamp;
if (!checked_read_uleb128 (&sub_ctx, ×tamp,
&where, "timestamp of file entry"))
goto skip;
/* Size of the file. */
uint64_t file_size;
if (!checked_read_uleb128 (&sub_ctx, &file_size,
&where, "file size of file entry"))
goto skip;
REALLOC (&files, files);
files.files[files.size++]
= (struct file_t){name, dir_idx, false};
}
/* Skip the rest of the header. */
if (sub_ctx.ptr > program_start)
{
wr_error (&where,
": header claims that it has a size of %#" PRIx64
", but in fact it has a size of %#" PRIx64 ".\n",
header_length, sub_ctx.ptr - program_start + header_length);
/* Assume that the header lies, and what follows is in
fact line number program. */
retval = false;
}
else if (sub_ctx.ptr < program_start)
{
if (!check_zero_padding (&sub_ctx, mc_line | mc_header, &where))
wr_message_padding_n0 (mc_line | mc_header, &WHERE (sec_line, NULL),
read_ctx_get_offset (&sub_ctx),
program_start - sub_ctx.begin - 1);
sub_ctx.ptr = program_start;
}
bool terminated = false;
bool first_file = true;
bool seen_opcode = false;
while (!read_ctx_eof (&sub_ctx))
{
where_reset_2 (&where, read_ctx_get_offset (&sub_ctx));
uint8_t opcode;
if (!read_ctx_read_ubyte (&sub_ctx, &opcode))
{
wr_error (&where, ": can't read opcode.\n");
goto skip;
}
void use_file (uint64_t file_idx)
{
if (file_idx == 0 || file_idx > files.size)
{
wr_error (&where,
": DW_LNS_set_file: invalid file index %" PRId64 ".\n",
file_idx);
retval = false;
}
else
files.files[file_idx - 1].used = true;
}
unsigned operands = 0;
uint8_t extended = 0;
switch (opcode)
{
/* Extended opcodes. */
case 0:
{
uint64_t skip_len;
if (!checked_read_uleb128 (&sub_ctx, &skip_len, &where,
"length of extended opcode"))
goto skip;
const unsigned char *next = sub_ctx.ptr + skip_len;
if (!read_ctx_read_ubyte (&sub_ctx, &extended))
{
wr_error (&where, ": can't read extended opcode.\n");
goto skip;
}
bool handled = true;
switch (extended)
{
case DW_LNE_end_sequence:
terminated = true;
break;
case DW_LNE_set_address:
{
uint64_t ctx_offset = read_ctx_get_offset (&sub_ctx);
uint64_t addr;
if (!read_ctx_read_offset (&sub_ctx, data->file->addr_64, &addr))
{
wr_error (&where, ": can't read operand of DW_LNE_set_address.\n");
goto skip;
}
struct relocation *rel;
if ((rel = relocation_next (&data->rel, ctx_offset,
&where, skip_mismatched)))
relocate_one (&data->rel, rel, data->file->addr_64 ? 8 : 4,
&addr, &where, rel_address, NULL);
else if (data->file->ehdr.e_type == ET_REL)
wr_message (mc_impact_2 | mc_line | mc_reloc, &where,
PRI_LACK_RELOCATION, "DW_LNE_set_address");
break;
}
case DW_LNE_define_file:
{
const char *name;
if ((name = read_ctx_read_str (&sub_ctx)) == NULL)
{
wr_error (&where,
": can't read filename operand of DW_LNE_define_file.\n");
goto skip;
}
uint64_t dir_idx;
if (!read_directory_index (name, &dir_idx))
goto skip;
REALLOC (&files, files);
files.files[files.size++] =
(struct file_t){name, dir_idx, false};
operands = 2; /* Skip mtime & size of the file. */
}
/* See if we know about any other standard opcodes. */
default:
handled = false;
switch (extended)
{
#define ONE_KNOWN_DW_LNE(NAME, CODE) case CODE: break;
ALL_KNOWN_DW_LNE
#undef ONE_KNOWN_DW_LNE
default:
/* No we don't, emit a warning. */
wr_message (mc_impact_2 | mc_line, &where,
": unknown extended opcode #%d.\n", extended);
};
};
if (sub_ctx.ptr > next)
{
wr_error (&where,
": opcode claims that it has a size of %#" PRIx64
", but in fact it has a size of %#" PRIx64 ".\n",
skip_len, skip_len + (next - sub_ctx.ptr));
retval = false;
}
else if (sub_ctx.ptr < next)
{
if (handled
&& !check_zero_padding (&sub_ctx, mc_line, &where))
wr_message_padding_n0 (mc_line, &WHERE (sec_line, NULL),
read_ctx_get_offset (&sub_ctx),
next - sub_ctx.begin - 1);
sub_ctx.ptr = next;
}
break;
}
/* Standard opcodes that need validation or have
non-ULEB operands. */
case DW_LNS_fixed_advance_pc:
{
uint16_t a;
if (!read_ctx_read_2ubyte (&sub_ctx, &a))
{
wr_error (&where, ": can't read operand of DW_LNS_fixed_advance_pc.\n");
goto skip;
}
break;
}
case DW_LNS_set_file:
{
uint64_t file_idx;
if (!checked_read_uleb128 (&sub_ctx, &file_idx, &where,
"DW_LNS_set_file operand"))
goto skip;
use_file (file_idx);
first_file = false;
}
break;
case DW_LNS_set_isa:
// XXX is it possible to validate this?
operands = 1;
break;
/* All the other opcodes. */
default:
if (opcode < opcode_base)
operands = std_opc_lengths[opcode - 1];
switch (opcode)
{
#define ONE_KNOWN_DW_LNS(NAME, CODE) case CODE: break;
ALL_KNOWN_DW_LNS
#undef ONE_KNOWN_DW_LNS
default:
if (opcode < opcode_base)
wr_message (mc_impact_2 | mc_line, &where,
": unknown standard opcode #%d.\n", opcode);
};
};
for (unsigned i = 0; i < operands; ++i)
{
uint64_t operand;
char buf[128];
if (opcode != 0)
sprintf (buf, "operand #%d of DW_LNS_%s",
i, dwarf_locexpr_opcode_string (opcode));
else
sprintf (buf, "operand #%d of extended opcode %d",
i, extended);
if (!checked_read_uleb128 (&sub_ctx, &operand, &where, buf))
goto skip;
}
if (first_file)
{
use_file (1);
first_file = false;
}
if (opcode != 0 || extended != DW_LNE_end_sequence)
seen_opcode = true;
}
for (size_t i = 0; i < include_directories.size; ++i)
if (!include_directories.dirs[i].used)
wr_message (mc_impact_3 | mc_acc_bloat | mc_line | mc_header,
&where, ": the include #%zd `%s' is not used.\n",
i + 1, include_directories.dirs[i].name);
for (size_t i = 0; i < files.size; ++i)
if (!files.files[i].used)
wr_message (mc_impact_3 | mc_acc_bloat | mc_line | mc_header,
&where, ": the file #%zd `%s' is not used.\n",
i + 1, files.files[i].name);
if (!seen_opcode)
wr_message (mc_line | mc_acc_bloat | mc_impact_3, &where,
": empty line number program.\n");
if (!terminated)
{
if (seen_opcode)
wr_error (&where,
": sequence of opcodes not terminated with DW_LNE_end_sequence.\n");
}
else if (sub_ctx.ptr != sub_ctx.end
&& !check_zero_padding (&sub_ctx, mc_line,
&WHERE (sec_line, NULL)))
wr_message_padding_n0 (mc_line, &WHERE (sec_line, NULL),
/*begin*/read_ctx_get_offset (&sub_ctx),
/*end*/sub_ctx.end - sub_ctx.begin - 1);
}
/* XXX overlaps in defined addresses are probably OK, one
instruction can be derived from several statements. But
certain flags in table should be consistent in that case,
namely is_stmt, basic_block, end_sequence, prologue_end,
epilogue_begin, isa. */
next:
if (!read_ctx_skip (&ctx, size))
goto not_enough;
}
if (retval)
{
relocation_skip_rest (data);
for (struct cu *cu = cu_chain; cu != NULL; cu = cu->next)
for (size_t i = 0; i < cu->line_refs.size; ++i)
{
struct ref *ref = cu->line_refs.refs + i;
if (!addr_record_has_addr (&line_tables, ref->addr))
wr_error (&ref->who,
": unresolved reference to .debug_line table %#" PRIx64 ".\n",
ref->addr);
}
}
return retval;
}
|