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
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
|
/* Mach-O support for BFD.
Copyright (C) 1999-2015 Free Software Foundation, Inc.
This file is part of BFD, the Binary File Descriptor library.
This program 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; either version 3 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "mach-o.h"
#include "bfd.h"
#include "libbfd.h"
#include "libiberty.h"
#include "aout/stab_gnu.h"
#include "mach-o/reloc.h"
#include "mach-o/external.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define bfd_mach_o_object_p bfd_mach_o_gen_object_p
#define bfd_mach_o_core_p bfd_mach_o_gen_core_p
#define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
#define FILE_ALIGN(off, algn) \
(((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
static bfd_boolean
bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
unsigned int
bfd_mach_o_version (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = NULL;
BFD_ASSERT (bfd_mach_o_valid (abfd));
mdata = bfd_mach_o_get_data (abfd);
return mdata->header.version;
}
bfd_boolean
bfd_mach_o_valid (bfd *abfd)
{
if (abfd == NULL || abfd->xvec == NULL)
return FALSE;
if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
return FALSE;
if (bfd_mach_o_get_data (abfd) == NULL)
return FALSE;
return TRUE;
}
static INLINE bfd_boolean
mach_o_wide_p (bfd_mach_o_header *header)
{
switch (header->version)
{
case 1:
return FALSE;
case 2:
return TRUE;
default:
BFD_FAIL ();
return FALSE;
}
}
static INLINE bfd_boolean
bfd_mach_o_wide_p (bfd *abfd)
{
return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
}
/* Tables to translate well known Mach-O segment/section names to bfd
names. Use of canonical names (such as .text or .debug_frame) is required
by gdb. */
/* __TEXT Segment. */
static const mach_o_section_name_xlat text_section_names_xlat[] =
{
{ ".text", "__text",
SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0},
{ ".const", "__const",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".static_const", "__static_const",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".cstring", "__cstring",
SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
BFD_MACH_O_S_CSTRING_LITERALS,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".literal4", "__literal4",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS,
BFD_MACH_O_S_ATTR_NONE, 2},
{ ".literal8", "__literal8",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS,
BFD_MACH_O_S_ATTR_NONE, 3},
{ ".literal16", "__literal16",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS,
BFD_MACH_O_S_ATTR_NONE, 4},
{ ".constructor", "__constructor",
SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".destructor", "__destructor",
SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".eh_frame", "__eh_frame",
SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED,
BFD_MACH_O_S_ATTR_LIVE_SUPPORT
| BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
| BFD_MACH_O_S_ATTR_NO_TOC, 2},
{ NULL, NULL, 0, 0, 0, 0}
};
/* __DATA Segment. */
static const mach_o_section_name_xlat data_section_names_xlat[] =
{
{ ".data", "__data",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".bss", "__bss",
SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".const_data", "__const",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".static_data", "__static_data",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".mod_init_func", "__mod_init_func",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
BFD_MACH_O_S_ATTR_NONE, 2},
{ ".mod_term_func", "__mod_term_func",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
BFD_MACH_O_S_ATTR_NONE, 2},
{ ".dyld", "__dyld",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 0},
{ ".cfstring", "__cfstring",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NONE, 2},
{ NULL, NULL, 0, 0, 0, 0}
};
/* __DWARF Segment. */
static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
{
{ ".debug_frame", "__debug_frame",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_info", "__debug_info",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_abbrev", "__debug_abbrev",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_aranges", "__debug_aranges",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_macinfo", "__debug_macinfo",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_line", "__debug_line",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_loc", "__debug_loc",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_pubnames", "__debug_pubnames",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_pubtypes", "__debug_pubtypes",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_str", "__debug_str",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_ranges", "__debug_ranges",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_macro", "__debug_macro",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ ".debug_gdb_scripts", "__debug_gdb_scri",
SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_DEBUG, 0},
{ NULL, NULL, 0, 0, 0, 0}
};
/* __OBJC Segment. */
static const mach_o_section_name_xlat objc_section_names_xlat[] =
{
{ ".objc_class", "__class",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_meta_class", "__meta_class",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_cat_cls_meth", "__cat_cls_meth",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_cat_inst_meth", "__cat_inst_meth",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_protocol", "__protocol",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_string_object", "__string_object",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_cls_meth", "__cls_meth",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_inst_meth", "__inst_meth",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_cls_refs", "__cls_refs",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_message_refs", "__message_refs",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_symbols", "__symbols",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_category", "__category",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_class_vars", "__class_vars",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_instance_vars", "__instance_vars",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_module_info", "__module_info",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_selector_strs", "__selector_strs",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_image_info", "__image_info",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc_selector_fixup", "__sel_fixup",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
/* Objc V1 */
{ ".objc1_class_ext", "__class_ext",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc1_property_list", "__property",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ ".objc1_protocol_ext", "__protocol_ext",
SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
{ NULL, NULL, 0, 0, 0, 0}
};
static const mach_o_segment_name_xlat segsec_names_xlat[] =
{
{ "__TEXT", text_section_names_xlat },
{ "__DATA", data_section_names_xlat },
{ "__DWARF", dwarf_section_names_xlat },
{ "__OBJC", objc_section_names_xlat },
{ NULL, NULL }
};
static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF";
/* For both cases bfd-name => mach-o name and vice versa, the specific target
is checked before the generic. This allows a target (e.g. ppc for cstring)
to override the generic definition with a more specific one. */
/* Fetch the translation from a Mach-O section designation (segment, section)
as a bfd short name, if one exists. Otherwise return NULL.
Allow the segment and section names to be unterminated 16 byte arrays. */
const mach_o_section_name_xlat *
bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
const char *sectname)
{
const struct mach_o_segment_name_xlat *seg;
const mach_o_section_name_xlat *sec;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
/* First try any target-specific translations defined... */
if (bed->segsec_names_xlat)
for (seg = bed->segsec_names_xlat; seg->segname; seg++)
if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
for (sec = seg->sections; sec->mach_o_name; sec++)
if (strncmp (sec->mach_o_name, sectname,
BFD_MACH_O_SECTNAME_SIZE) == 0)
return sec;
/* ... and then the Mach-O generic ones. */
for (seg = segsec_names_xlat; seg->segname; seg++)
if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
for (sec = seg->sections; sec->mach_o_name; sec++)
if (strncmp (sec->mach_o_name, sectname,
BFD_MACH_O_SECTNAME_SIZE) == 0)
return sec;
return NULL;
}
/* If the bfd_name for this section is a 'canonical' form for which we
know the Mach-O data, return the segment name and the data for the
Mach-O equivalent. Otherwise return NULL. */
const mach_o_section_name_xlat *
bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
const char **segname)
{
const struct mach_o_segment_name_xlat *seg;
const mach_o_section_name_xlat *sec;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
*segname = NULL;
if (bfd_name[0] != '.')
return NULL;
/* First try any target-specific translations defined... */
if (bed->segsec_names_xlat)
for (seg = bed->segsec_names_xlat; seg->segname; seg++)
for (sec = seg->sections; sec->bfd_name; sec++)
if (strcmp (bfd_name, sec->bfd_name) == 0)
{
*segname = seg->segname;
return sec;
}
/* ... and then the Mach-O generic ones. */
for (seg = segsec_names_xlat; seg->segname; seg++)
for (sec = seg->sections; sec->bfd_name; sec++)
if (strcmp (bfd_name, sec->bfd_name) == 0)
{
*segname = seg->segname;
return sec;
}
return NULL;
}
/* Convert Mach-O section name to BFD.
Try to use standard/canonical names, for which we have tables including
default flag settings - which are returned. Otherwise forge a new name
in the form "<segmentname>.<sectionname>" this will be prefixed with
LC_SEGMENT. if the segment name does not begin with an underscore.
SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
terminated if the name length is exactly 16 bytes - but must be if the name
length is less than 16 characters). */
void
bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
const char *secname, const char **name,
flagword *flags)
{
const mach_o_section_name_xlat *xlat;
char *res;
unsigned int len;
const char *pfx = "";
*name = NULL;
*flags = SEC_NO_FLAGS;
/* First search for a canonical name...
xlat will be non-null if there is an entry for segname, secname. */
xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
if (xlat)
{
len = strlen (xlat->bfd_name);
res = bfd_alloc (abfd, len + 1);
if (res == NULL)
return;
memcpy (res, xlat->bfd_name, len+1);
*name = res;
*flags = xlat->bfd_flags;
return;
}
/* ... else we make up a bfd name from the segment concatenated with the
section. */
len = 16 + 1 + 16 + 1;
/* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
with an underscore. */
if (segname[0] != '_')
{
static const char seg_pfx[] = "LC_SEGMENT.";
pfx = seg_pfx;
len += sizeof (seg_pfx) - 1;
}
res = bfd_alloc (abfd, len);
if (res == NULL)
return;
snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
*name = res;
}
/* Convert a bfd section name to a Mach-O segment + section name.
If the name is a canonical one for which we have a Darwin match
return the translation table - which contains defaults for flags,
type, attribute and default alignment data.
Otherwise, expand the bfd_name (assumed to be in the form
"[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
static const mach_o_section_name_xlat *
bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
asection *sect,
bfd_mach_o_section *section)
{
const mach_o_section_name_xlat *xlat;
const char *name = bfd_get_section_name (abfd, sect);
const char *segname;
const char *dot;
unsigned int len;
unsigned int seglen;
unsigned int seclen;
memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
/* See if is a canonical name ... */
xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
if (xlat)
{
strcpy (section->segname, segname);
strcpy (section->sectname, xlat->mach_o_name);
return xlat;
}
/* .. else we convert our constructed one back to Mach-O.
Strip LC_SEGMENT. prefix, if present. */
if (strncmp (name, "LC_SEGMENT.", 11) == 0)
name += 11;
/* Find a dot. */
dot = strchr (name, '.');
len = strlen (name);
/* Try to split name into segment and section names. */
if (dot && dot != name)
{
seglen = dot - name;
seclen = len - (dot + 1 - name);
if (seglen <= BFD_MACH_O_SEGNAME_SIZE
&& seclen <= BFD_MACH_O_SECTNAME_SIZE)
{
memcpy (section->segname, name, seglen);
section->segname[seglen] = 0;
memcpy (section->sectname, dot + 1, seclen);
section->sectname[seclen] = 0;
return NULL;
}
}
/* The segment and section names are both missing - don't make them
into dots. */
if (dot && dot == name)
return NULL;
/* Just duplicate the name into both segment and section. */
if (len > 16)
len = 16;
memcpy (section->segname, name, len);
section->segname[len] = 0;
memcpy (section->sectname, name, len);
section->sectname[len] = 0;
return NULL;
}
/* Return the size of an entry for section SEC.
Must be called only for symbol pointer section and symbol stubs
sections. */
unsigned int
bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
{
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
return bfd_mach_o_wide_p (abfd) ? 8 : 4;
case BFD_MACH_O_S_SYMBOL_STUBS:
return sec->reserved2;
default:
BFD_FAIL ();
return 0;
}
}
/* Return the number of indirect symbols for a section.
Must be called only for symbol pointer section and symbol stubs
sections. */
unsigned int
bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
{
unsigned int elsz;
elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
if (elsz == 0)
return 0;
else
return sec->size / elsz;
}
/* Append command CMD to ABFD. Note that header.ncmds is not updated. */
static void
bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata->last_command != NULL)
mdata->last_command->next = cmd;
else
mdata->first_command = cmd;
mdata->last_command = cmd;
cmd->next = NULL;
}
/* Copy any private info we understand from the input symbol
to the output symbol. */
bfd_boolean
bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
asymbol *isymbol,
bfd *obfd ATTRIBUTE_UNUSED,
asymbol *osymbol)
{
bfd_mach_o_asymbol *os, *is;
os = (bfd_mach_o_asymbol *)osymbol;
is = (bfd_mach_o_asymbol *)isymbol;
os->n_type = is->n_type;
os->n_sect = is->n_sect;
os->n_desc = is->n_desc;
os->symbol.udata.i = is->symbol.udata.i;
return TRUE;
}
/* Copy any private info we understand from the input section
to the output section. */
bfd_boolean
bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
bfd *obfd, asection *osection)
{
bfd_mach_o_section *os = bfd_mach_o_get_mach_o_section (osection);
bfd_mach_o_section *is = bfd_mach_o_get_mach_o_section (isection);
if (ibfd->xvec->flavour != bfd_target_mach_o_flavour
|| obfd->xvec->flavour != bfd_target_mach_o_flavour)
return TRUE;
BFD_ASSERT (is != NULL && os != NULL);
os->flags = is->flags;
os->reserved1 = is->reserved1;
os->reserved2 = is->reserved2;
os->reserved3 = is->reserved3;
return TRUE;
}
/* Copy any private info we understand from the input bfd
to the output bfd. */
bfd_boolean
bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
{
bfd_mach_o_data_struct *imdata;
bfd_mach_o_data_struct *omdata;
bfd_mach_o_load_command *icmd;
if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
|| bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
return TRUE;
BFD_ASSERT (bfd_mach_o_valid (ibfd));
BFD_ASSERT (bfd_mach_o_valid (obfd));
imdata = bfd_mach_o_get_data (ibfd);
omdata = bfd_mach_o_get_data (obfd);
/* Copy header flags. */
omdata->header.flags = imdata->header.flags;
/* Copy commands. */
for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
{
bfd_mach_o_load_command *ocmd;
switch (icmd->type)
{
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LOAD_DYLINKER:
case BFD_MACH_O_LC_DYLD_INFO:
/* Command is copied. */
ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command));
if (ocmd == NULL)
return FALSE;
/* Copy common fields. */
ocmd->type = icmd->type;
ocmd->type_required = icmd->type_required;
ocmd->offset = 0;
ocmd->len = icmd->len;
break;
default:
/* Command is not copied. */
continue;
break;
}
switch (icmd->type)
{
case BFD_MACH_O_LC_LOAD_DYLIB:
{
bfd_mach_o_dylib_command *idy = &icmd->command.dylib;
bfd_mach_o_dylib_command *ody = &ocmd->command.dylib;
ody->name_offset = idy->name_offset;
ody->timestamp = idy->timestamp;
ody->current_version = idy->current_version;
ody->compatibility_version = idy->compatibility_version;
ody->name_str = idy->name_str;
}
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
{
bfd_mach_o_dylinker_command *idy = &icmd->command.dylinker;
bfd_mach_o_dylinker_command *ody = &ocmd->command.dylinker;
ody->name_offset = idy->name_offset;
ody->name_str = idy->name_str;
}
break;
case BFD_MACH_O_LC_DYLD_INFO:
{
bfd_mach_o_dyld_info_command *idy = &icmd->command.dyld_info;
bfd_mach_o_dyld_info_command *ody = &ocmd->command.dyld_info;
if (bfd_mach_o_read_dyld_content (ibfd, idy))
{
ody->rebase_size = idy->rebase_size;
ody->rebase_content = idy->rebase_content;
ody->bind_size = idy->bind_size;
ody->bind_content = idy->bind_content;
ody->weak_bind_size = idy->weak_bind_size;
ody->weak_bind_content = idy->weak_bind_content;
ody->lazy_bind_size = idy->lazy_bind_size;
ody->lazy_bind_content = idy->lazy_bind_content;
ody->export_size = idy->export_size;
ody->export_content = idy->export_content;
}
/* PR 17512L: file: 730e492d. */
else
{
ody->rebase_size =
ody->bind_size =
ody->weak_bind_size =
ody->lazy_bind_size =
ody->export_size = 0;
ody->rebase_content =
ody->bind_content =
ody->weak_bind_content =
ody->lazy_bind_content =
ody->export_content = NULL;
}
}
break;
default:
/* That command should be handled. */
abort ();
}
/* Insert command. */
bfd_mach_o_append_command (obfd, ocmd);
}
return TRUE;
}
/* This allows us to set up to 32 bits of flags (unless we invent some
fiendish scheme to subdivide). For now, we'll just set the file flags
without error checking - just overwrite. */
bfd_boolean
bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (!mdata)
return FALSE;
mdata->header.flags = flags;
return TRUE;
}
/* Count the total number of symbols. */
static long
bfd_mach_o_count_symbols (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata->symtab == NULL)
return 0;
return mdata->symtab->nsyms;
}
long
bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
{
long nsyms = bfd_mach_o_count_symbols (abfd);
return ((nsyms + 1) * sizeof (asymbol *));
}
long
bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
long nsyms = bfd_mach_o_count_symbols (abfd);
bfd_mach_o_symtab_command *sym = mdata->symtab;
unsigned long j;
if (nsyms < 0)
return nsyms;
if (nsyms == 0)
{
/* Do not try to read symbols if there are none. */
alocation[0] = NULL;
return 0;
}
if (!bfd_mach_o_read_symtab_symbols (abfd))
{
(*_bfd_error_handler)
(_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
return 0;
}
BFD_ASSERT (sym->symbols != NULL);
for (j = 0; j < sym->nsyms; j++)
alocation[j] = &sym->symbols[j].symbol;
alocation[j] = NULL;
return nsyms;
}
/* Create synthetic symbols for indirect symbols. */
long
bfd_mach_o_get_synthetic_symtab (bfd *abfd,
long symcount ATTRIBUTE_UNUSED,
asymbol **syms ATTRIBUTE_UNUSED,
long dynsymcount ATTRIBUTE_UNUSED,
asymbol **dynsyms ATTRIBUTE_UNUSED,
asymbol **ret)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
bfd_mach_o_symtab_command *symtab = mdata->symtab;
asymbol *s;
char * s_start;
char * s_end;
unsigned long count, i, j, n;
size_t size;
char *names;
char *nul_name;
const char stub [] = "$stub";
*ret = NULL;
/* Stop now if no symbols or no indirect symbols. */
if (dysymtab == NULL || dysymtab->nindirectsyms == 0
|| symtab == NULL || symtab->symbols == NULL)
return 0;
/* We need to allocate a bfd symbol for every indirect symbol and to
allocate the memory for its name. */
count = dysymtab->nindirectsyms;
size = count * sizeof (asymbol) + 1;
for (j = 0; j < count; j++)
{
const char * strng;
unsigned int isym = dysymtab->indirect_syms[j];
/* Some indirect symbols are anonymous. */
if (isym < symtab->nsyms && (strng = symtab->symbols[isym].symbol.name))
/* PR 17512: file: f5b8eeba. */
size += strnlen (strng, symtab->strsize - (strng - symtab->strtab)) + sizeof (stub);
}
s_start = bfd_malloc (size);
s = *ret = (asymbol *) s_start;
if (s == NULL)
return -1;
names = (char *) (s + count);
nul_name = names;
*names++ = 0;
s_end = s_start + size;
n = 0;
for (i = 0; i < mdata->nsects; i++)
{
bfd_mach_o_section *sec = mdata->sections[i];
unsigned int first, last;
bfd_vma addr;
bfd_vma entry_size;
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
/* Only these sections have indirect symbols. */
first = sec->reserved1;
last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
addr = sec->addr;
entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
/* PR 17512: file: 08e15eec. */
if (first >= count || last >= count || first > last)
goto fail;
for (j = first; j < last; j++)
{
unsigned int isym = dysymtab->indirect_syms[j];
/* PR 17512: file: 04d64d9b. */
if (((char *) s) + sizeof (* s) > s_end)
goto fail;
s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
s->section = sec->bfdsection;
s->value = addr - sec->addr;
s->udata.p = NULL;
if (isym < symtab->nsyms
&& symtab->symbols[isym].symbol.name)
{
const char *sym = symtab->symbols[isym].symbol.name;
size_t len;
s->name = names;
len = strlen (sym);
/* PR 17512: file: 47dfd4d2. */
if (names + len >= s_end)
goto fail;
memcpy (names, sym, len);
names += len;
/* PR 17512: file: 18f340a4. */
if (names + sizeof (stub) >= s_end)
goto fail;
memcpy (names, stub, sizeof (stub));
names += sizeof (stub);
}
else
s->name = nul_name;
addr += entry_size;
s++;
n++;
}
break;
default:
break;
}
}
return n;
fail:
free (s_start);
* ret = NULL;
return -1;
}
void
bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
asymbol *symbol,
symbol_info *ret)
{
bfd_symbol_info (symbol, ret);
}
void
bfd_mach_o_print_symbol (bfd *abfd,
void * afile,
asymbol *symbol,
bfd_print_symbol_type how)
{
FILE *file = (FILE *) afile;
const char *name;
bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
switch (how)
{
case bfd_print_symbol_name:
fprintf (file, "%s", symbol->name);
break;
default:
bfd_print_symbol_vandf (abfd, (void *) file, symbol);
if (asym->n_type & BFD_MACH_O_N_STAB)
name = bfd_get_stab_name (asym->n_type);
else
switch (asym->n_type & BFD_MACH_O_N_TYPE)
{
case BFD_MACH_O_N_UNDF:
if (symbol->value == 0)
name = "UND";
else
name = "COM";
break;
case BFD_MACH_O_N_ABS:
name = "ABS";
break;
case BFD_MACH_O_N_INDR:
name = "INDR";
break;
case BFD_MACH_O_N_PBUD:
name = "PBUD";
break;
case BFD_MACH_O_N_SECT:
name = "SECT";
break;
default:
name = "???";
break;
}
if (name == NULL)
name = "";
fprintf (file, " %02x %-6s %02x %04x",
asym->n_type, name, asym->n_sect, asym->n_desc);
if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
&& (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
fprintf (file, " [%s]", symbol->section->name);
fprintf (file, " %s", symbol->name);
}
}
static void
bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
bfd_mach_o_cpu_subtype msubtype,
enum bfd_architecture *type,
unsigned long *subtype)
{
*subtype = bfd_arch_unknown;
switch (mtype)
{
case BFD_MACH_O_CPU_TYPE_VAX:
*type = bfd_arch_vax;
break;
case BFD_MACH_O_CPU_TYPE_MC680x0:
*type = bfd_arch_m68k;
break;
case BFD_MACH_O_CPU_TYPE_I386:
*type = bfd_arch_i386;
*subtype = bfd_mach_i386_i386;
break;
case BFD_MACH_O_CPU_TYPE_X86_64:
*type = bfd_arch_i386;
*subtype = bfd_mach_x86_64;
break;
case BFD_MACH_O_CPU_TYPE_MIPS:
*type = bfd_arch_mips;
break;
case BFD_MACH_O_CPU_TYPE_MC98000:
*type = bfd_arch_m98k;
break;
case BFD_MACH_O_CPU_TYPE_HPPA:
*type = bfd_arch_hppa;
break;
case BFD_MACH_O_CPU_TYPE_ARM:
*type = bfd_arch_arm;
switch (msubtype)
{
case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
*subtype = bfd_mach_arm_4T;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
*subtype = bfd_mach_arm_4T; /* Best fit ? */
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
*subtype = bfd_mach_arm_5TE;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
*subtype = bfd_mach_arm_XScale;
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
*subtype = bfd_mach_arm_5TE; /* Best fit ? */
break;
case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
default:
break;
}
break;
case BFD_MACH_O_CPU_TYPE_MC88000:
*type = bfd_arch_m88k;
break;
case BFD_MACH_O_CPU_TYPE_SPARC:
*type = bfd_arch_sparc;
*subtype = bfd_mach_sparc;
break;
case BFD_MACH_O_CPU_TYPE_I860:
*type = bfd_arch_i860;
break;
case BFD_MACH_O_CPU_TYPE_ALPHA:
*type = bfd_arch_alpha;
break;
case BFD_MACH_O_CPU_TYPE_POWERPC:
*type = bfd_arch_powerpc;
*subtype = bfd_mach_ppc;
break;
case BFD_MACH_O_CPU_TYPE_POWERPC_64:
*type = bfd_arch_powerpc;
*subtype = bfd_mach_ppc64;
break;
case BFD_MACH_O_CPU_TYPE_ARM64:
*type = bfd_arch_aarch64;
*subtype = bfd_mach_aarch64;
break;
default:
*type = bfd_arch_unknown;
break;
}
}
/* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the
number of bytes written or -1 in case of error. */
static int
bfd_mach_o_pad4 (bfd *abfd, unsigned int len)
{
if (len % 4 != 0)
{
char pad[4] = {0,0,0,0};
unsigned int padlen = 4 - (len % 4);
if (bfd_bwrite (pad, padlen, abfd) != padlen)
return -1;
return padlen;
}
else
return 0;
}
/* Likewise, but for a command. */
static int
bfd_mach_o_pad_command (bfd *abfd, unsigned int len)
{
unsigned int align = bfd_mach_o_wide_p (abfd) ? 8 : 4;
if (len % align != 0)
{
char pad[8] = {0};
unsigned int padlen = align - (len % align);
if (bfd_bwrite (pad, padlen, abfd) != padlen)
return -1;
return padlen;
}
else
return 0;
}
static bfd_boolean
bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
{
struct mach_o_header_external raw;
unsigned int size;
size = mach_o_wide_p (header) ?
BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
bfd_h_put_32 (abfd, header->magic, raw.magic);
bfd_h_put_32 (abfd, header->cputype, raw.cputype);
bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
bfd_h_put_32 (abfd, header->filetype, raw.filetype);
bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
bfd_h_put_32 (abfd, header->flags, raw.flags);
if (mach_o_wide_p (header))
bfd_h_put_32 (abfd, header->reserved, raw.reserved);
if (bfd_seek (abfd, 0, SEEK_SET) != 0
|| bfd_bwrite (&raw, size, abfd) != size)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_thread_command *cmd = &command->command.thread;
unsigned int i;
struct mach_o_thread_command_external raw;
unsigned int offset;
BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
|| (command->type == BFD_MACH_O_LC_UNIXTHREAD));
offset = BFD_MACH_O_LC_SIZE;
for (i = 0; i < cmd->nflavours; i++)
{
BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
BFD_ASSERT (cmd->flavours[i].offset ==
(command->offset + offset + BFD_MACH_O_LC_SIZE));
bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
offset += cmd->flavours[i].size + sizeof (raw);
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
struct mach_o_str_command_external raw;
unsigned int namelen;
bfd_h_put_32 (abfd, cmd->name_offset, raw.str);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
namelen = strlen (cmd->name_str) + 1;
if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen)
return FALSE;
if (bfd_mach_o_pad_command (abfd, namelen) < 0)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylib_command *cmd = &command->command.dylib;
struct mach_o_dylib_command_external raw;
unsigned int namelen;
bfd_h_put_32 (abfd, cmd->name_offset, raw.name);
bfd_h_put_32 (abfd, cmd->timestamp, raw.timestamp);
bfd_h_put_32 (abfd, cmd->current_version, raw.current_version);
bfd_h_put_32 (abfd, cmd->compatibility_version, raw.compatibility_version);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
namelen = strlen (cmd->name_str) + 1;
if (bfd_bwrite (cmd->name_str, namelen, abfd) != namelen)
return FALSE;
if (bfd_mach_o_pad_command (abfd, namelen) < 0)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_main_command *cmd = &command->command.main;
struct mach_o_entry_point_command_external raw;
bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff);
bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
struct mach_o_dyld_info_command_external raw;
bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off);
bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size);
bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off);
bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size);
bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off);
bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size);
bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off);
bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size);
bfd_h_put_32 (abfd, cmd->export_off, raw.export_off);
bfd_h_put_32 (abfd, cmd->export_size, raw.export_size);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
if (cmd->rebase_size != 0)
if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0
|| (bfd_bwrite (cmd->rebase_content, cmd->rebase_size, abfd) !=
cmd->rebase_size))
return FALSE;
if (cmd->bind_size != 0)
if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0
|| (bfd_bwrite (cmd->bind_content, cmd->bind_size, abfd) !=
cmd->bind_size))
return FALSE;
if (cmd->weak_bind_size != 0)
if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0
|| (bfd_bwrite (cmd->weak_bind_content, cmd->weak_bind_size, abfd) !=
cmd->weak_bind_size))
return FALSE;
if (cmd->lazy_bind_size != 0)
if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0
|| (bfd_bwrite (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) !=
cmd->lazy_bind_size))
return FALSE;
if (cmd->export_size != 0)
if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0
|| (bfd_bwrite (cmd->export_content, cmd->export_size, abfd) !=
cmd->export_size))
return FALSE;
return TRUE;
}
long
bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
asection *asect)
{
return (asect->reloc_count + 1) * sizeof (arelent *);
}
/* In addition to the need to byte-swap the symbol number, the bit positions
of the fields in the relocation information vary per target endian-ness. */
static void
bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
unsigned char *fields)
{
unsigned char info = fields[3];
if (bfd_big_endian (abfd))
{
rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
& BFD_MACH_O_LENGTH_MASK;
rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
}
else
{
rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
& BFD_MACH_O_LENGTH_MASK;
rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
}
}
static int
bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
struct mach_o_reloc_info_external *raw,
arelent *res, asymbol **syms)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
bfd_mach_o_reloc_info reloc;
bfd_vma addr;
asymbol **sym;
addr = bfd_get_32 (abfd, raw->r_address);
res->sym_ptr_ptr = NULL;
res->addend = 0;
if (addr & BFD_MACH_O_SR_SCATTERED)
{
unsigned int j;
bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum);
/* Scattered relocation, can't be extern. */
reloc.r_scattered = 1;
reloc.r_extern = 0;
/* Extract section and offset from r_value (symnum). */
reloc.r_value = symnum;
/* FIXME: This breaks when a symbol in a reloc exactly follows the
end of the data for the section (e.g. in a calculation of section
data length). At present, the symbol will end up associated with
the following section or, if it falls within alignment padding, as
null - which will assert later. */
for (j = 0; j < mdata->nsects; j++)
{
bfd_mach_o_section *sect = mdata->sections[j];
if (symnum >= sect->addr && symnum < sect->addr + sect->size)
{
res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
res->addend = symnum - sect->addr;
break;
}
}
/* Extract the info and address fields from r_address. */
reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
reloc.r_address = BFD_MACH_O_GET_SR_TYPE (addr);
res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
}
else
{
unsigned int num;
/* Non-scattered relocation. */
reloc.r_scattered = 0;
/* The value and info fields have to be extracted dependent on target
endian-ness. */
bfd_mach_o_swap_in_non_scattered_reloc (abfd, &reloc, raw->r_symbolnum);
num = reloc.r_value;
if (reloc.r_extern)
{
/* PR 17512: file: 8396-1185-0.004. */
if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
sym = bfd_und_section_ptr->symbol_ptr_ptr;
else if (syms == NULL)
sym = bfd_und_section_ptr->symbol_ptr_ptr;
else
/* An external symbol number. */
sym = syms + num;
}
else if (num == 0x00ffffff || num == 0)
{
/* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
is generic code, we don't know wether this is really a PAIR.
This value is almost certainly not a valid section number, hence
this specific case to avoid an assertion failure.
Target specific swap_reloc_in routine should adjust that. */
sym = bfd_abs_section_ptr->symbol_ptr_ptr;
}
else
{
/* PR 17512: file: 006-2964-0.004. */
if (num > mdata->nsects)
return -1;
/* A section number. */
sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
/* For a symbol defined in section S, the addend (stored in the
binary) contains the address of the section. To comply with
bfd convention, subtract the section address.
Use the address from the header, so that the user can modify
the vma of the section. */
res->addend = -mdata->sections[num - 1]->addr;
}
/* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
in the lower 16bits of the address value. So we have to find the
'symbol' from the preceding reloc. We do this even though the
section symbol is probably not needed here, because NULL symbol
values cause an assert in generic BFD code. This must be done in
the PPC swap_reloc_in routine. */
res->sym_ptr_ptr = sym;
/* The 'address' is just r_address.
??? maybe this should be masked with 0xffffff for safety. */
res->address = addr;
reloc.r_address = addr;
}
/* We have set up a reloc with all the information present, so the swapper
can modify address, value and addend fields, if necessary, to convey
information in the generic BFD reloc that is mach-o specific. */
if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
return -1;
return 0;
}
static int
bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
unsigned long count,
arelent *res, asymbol **syms)
{
unsigned long i;
struct mach_o_reloc_info_external *native_relocs;
bfd_size_type native_size;
/* Allocate and read relocs. */
native_size = count * BFD_MACH_O_RELENT_SIZE;
/* PR 17512: file: 09477b57. */
if (native_size < count)
return -1;
native_relocs =
(struct mach_o_reloc_info_external *) bfd_malloc (native_size);
if (native_relocs == NULL)
return -1;
if (bfd_seek (abfd, filepos, SEEK_SET) != 0
|| bfd_bread (native_relocs, native_size, abfd) != native_size)
goto err;
for (i = 0; i < count; i++)
{
if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
&res[i], syms) < 0)
goto err;
}
free (native_relocs);
return i;
err:
free (native_relocs);
return -1;
}
long
bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
arelent **rels, asymbol **syms)
{
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
unsigned long i;
arelent *res;
if (asect->reloc_count == 0)
return 0;
/* No need to go further if we don't know how to read relocs. */
if (bed->_bfd_mach_o_swap_reloc_in == NULL)
return 0;
if (asect->relocation == NULL)
{
if (asect->reloc_count * sizeof (arelent) < asect->reloc_count)
return -1;
res = bfd_malloc (asect->reloc_count * sizeof (arelent));
if (res == NULL)
return -1;
if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
asect->reloc_count, res, syms) < 0)
{
free (res);
return -1;
}
asect->relocation = res;
}
res = asect->relocation;
for (i = 0; i < asect->reloc_count; i++)
rels[i] = &res[i];
rels[i] = NULL;
return i;
}
long
bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata->dysymtab == NULL)
return 1;
return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
* sizeof (arelent *);
}
long
bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
struct bfd_symbol **syms)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
unsigned long i;
arelent *res;
if (dysymtab == NULL)
return 0;
if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
return 0;
/* No need to go further if we don't know how to read relocs. */
if (bed->_bfd_mach_o_swap_reloc_in == NULL)
return 0;
if (mdata->dyn_reloc_cache == NULL)
{
if ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent)
< (dysymtab->nextrel + dysymtab->nlocrel))
return -1;
res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel)
* sizeof (arelent));
if (res == NULL)
return -1;
if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
dysymtab->nextrel, res, syms) < 0)
{
free (res);
return -1;
}
if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
dysymtab->nlocrel,
res + dysymtab->nextrel, syms) < 0)
{
free (res);
return -1;
}
mdata->dyn_reloc_cache = res;
}
res = mdata->dyn_reloc_cache;
for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
rels[i] = &res[i];
rels[i] = NULL;
return i;
}
/* In addition to the need to byte-swap the symbol number, the bit positions
of the fields in the relocation information vary per target endian-ness. */
static void
bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
bfd_mach_o_reloc_info *rel)
{
unsigned char info = 0;
BFD_ASSERT (rel->r_type <= 15);
BFD_ASSERT (rel->r_length <= 3);
if (bfd_big_endian (abfd))
{
fields[0] = (rel->r_value >> 16) & 0xff;
fields[1] = (rel->r_value >> 8) & 0xff;
fields[2] = rel->r_value & 0xff;
info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT;
info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0;
info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT;
info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0;
}
else
{
fields[2] = (rel->r_value >> 16) & 0xff;
fields[1] = (rel->r_value >> 8) & 0xff;
fields[0] = rel->r_value & 0xff;
info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT;
info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0;
info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT;
info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0;
}
fields[3] = info;
}
static bfd_boolean
bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
{
unsigned int i;
arelent **entries;
asection *sec;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
sec = section->bfdsection;
if (sec->reloc_count == 0)
return TRUE;
if (bed->_bfd_mach_o_swap_reloc_out == NULL)
return TRUE;
if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
return FALSE;
/* Convert and write. */
entries = section->bfdsection->orelocation;
for (i = 0; i < section->nreloc; i++)
{
arelent *rel = entries[i];
struct mach_o_reloc_info_external raw;
bfd_mach_o_reloc_info info, *pinfo = &info;
/* Convert relocation to an intermediate representation. */
if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
return FALSE;
/* Lower the relocation info. */
if (pinfo->r_scattered)
{
unsigned long v;
v = BFD_MACH_O_SR_SCATTERED
| (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
| BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
| BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
| BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
/* Note: scattered relocs have field in reverse order... */
bfd_put_32 (abfd, v, raw.r_address);
bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
}
else
{
bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
pinfo);
}
if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
!= BFD_MACH_O_RELENT_SIZE)
return FALSE;
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
{
struct mach_o_section_32_external raw;
memcpy (raw.sectname, section->sectname, 16);
memcpy (raw.segname, section->segname, 16);
bfd_h_put_32 (abfd, section->addr, raw.addr);
bfd_h_put_32 (abfd, section->size, raw.size);
bfd_h_put_32 (abfd, section->offset, raw.offset);
bfd_h_put_32 (abfd, section->align, raw.align);
bfd_h_put_32 (abfd, section->reloff, raw.reloff);
bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
bfd_h_put_32 (abfd, section->flags, raw.flags);
bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
!= BFD_MACH_O_SECTION_SIZE)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
{
struct mach_o_section_64_external raw;
memcpy (raw.sectname, section->sectname, 16);
memcpy (raw.segname, section->segname, 16);
bfd_h_put_64 (abfd, section->addr, raw.addr);
bfd_h_put_64 (abfd, section->size, raw.size);
bfd_h_put_32 (abfd, section->offset, raw.offset);
bfd_h_put_32 (abfd, section->align, raw.align);
bfd_h_put_32 (abfd, section->reloff, raw.reloff);
bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
bfd_h_put_32 (abfd, section->flags, raw.flags);
bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
!= BFD_MACH_O_SECTION_64_SIZE)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
{
struct mach_o_segment_command_32_external raw;
bfd_mach_o_segment_command *seg = &command->command.segment;
bfd_mach_o_section *sec;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_relocs (abfd, sec))
return FALSE;
memcpy (raw.segname, seg->segname, 16);
bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
bfd_h_put_32 (abfd, seg->flags, raw.flags);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_section_32 (abfd, sec))
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
{
struct mach_o_segment_command_64_external raw;
bfd_mach_o_segment_command *seg = &command->command.segment;
bfd_mach_o_section *sec;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_relocs (abfd, sec))
return FALSE;
memcpy (raw.segname, seg->segname, 16);
bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
bfd_h_put_32 (abfd, seg->flags, raw.flags);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
if (!bfd_mach_o_write_section_64 (abfd, sec))
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned long i;
unsigned int wide = bfd_mach_o_wide_p (abfd);
struct bfd_strtab_hash *strtab;
asymbol **symbols = bfd_get_outsymbols (abfd);
int padlen;
/* Write the symbols first. */
if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
return FALSE;
strtab = _bfd_stringtab_init ();
if (strtab == NULL)
return FALSE;
if (sym->nsyms > 0)
/* Although we don't strictly need to do this, for compatibility with
Darwin system tools, actually output an empty string for the index
0 entry. */
_bfd_stringtab_add (strtab, "", TRUE, FALSE);
for (i = 0; i < sym->nsyms; i++)
{
bfd_size_type str_index;
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
/* An index of 0 always means the empty string. */
str_index = 0;
else
{
str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
if (str_index == (bfd_size_type) -1)
goto err;
}
if (wide)
{
struct mach_o_nlist_64_external raw;
bfd_h_put_32 (abfd, str_index, raw.n_strx);
bfd_h_put_8 (abfd, s->n_type, raw.n_type);
bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
raw.n_value);
if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
goto err;
}
else
{
struct mach_o_nlist_external raw;
bfd_h_put_32 (abfd, str_index, raw.n_strx);
bfd_h_put_8 (abfd, s->n_type, raw.n_type);
bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
raw.n_value);
if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
goto err;
}
}
sym->strsize = _bfd_stringtab_size (strtab);
sym->stroff = mdata->filelen;
mdata->filelen += sym->strsize;
if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
goto err;
if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
goto err;
/* Pad string table. */
padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
if (padlen < 0)
return FALSE;
mdata->filelen += padlen;
sym->strsize += padlen;
return TRUE;
err:
_bfd_stringtab_free (strtab);
sym->strsize = 0;
return FALSE;
}
static bfd_boolean
bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_symtab_command *sym = &command->command.symtab;
struct mach_o_symtab_command_external raw;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
/* The command. */
bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
return TRUE;
}
/* Count the number of indirect symbols in the image.
Requires that the sections are in their final order. */
static unsigned int
bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
{
unsigned int i;
unsigned int nisyms = 0;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *sec = mdata->sections[i];
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec);
break;
default:
break;
}
}
return nisyms;
}
/* Create the dysymtab. */
static bfd_boolean
bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
/* TODO:
We are not going to try and fill these in yet and, moreover, we are
going to bail if they are already set. */
if (cmd->nmodtab != 0
|| cmd->ntoc != 0
|| cmd->nextrefsyms != 0)
{
(*_bfd_error_handler) (_("sorry: modtab, toc and extrefsyms are not yet"
" implemented for dysymtab commands."));
return FALSE;
}
cmd->ilocalsym = 0;
if (bfd_get_symcount (abfd) > 0)
{
asymbol **symbols = bfd_get_outsymbols (abfd);
unsigned long i;
/* Count the number of each kind of symbol. */
for (i = 0; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))
break;
}
cmd->nlocalsym = i;
cmd->iextdefsym = i;
for (; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF)
break;
}
cmd->nextdefsym = i - cmd->nlocalsym;
cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym;
cmd->nundefsym = bfd_get_symcount (abfd)
- cmd->nlocalsym
- cmd->nextdefsym;
}
else
{
cmd->nlocalsym = 0;
cmd->iextdefsym = 0;
cmd->nextdefsym = 0;
cmd->iundefsym = 0;
cmd->nundefsym = 0;
}
cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
if (cmd->nindirectsyms > 0)
{
unsigned i;
unsigned n;
mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
cmd->indirectsymoff = mdata->filelen;
mdata->filelen += cmd->nindirectsyms * 4;
if (cmd->nindirectsyms * 4 < cmd->nindirectsyms)
return FALSE;
cmd->indirect_syms = bfd_zalloc (abfd, cmd->nindirectsyms * 4);
if (cmd->indirect_syms == NULL)
return FALSE;
n = 0;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *sec = mdata->sections[i];
switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
{
case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
case BFD_MACH_O_S_SYMBOL_STUBS:
{
unsigned j, num;
bfd_mach_o_asymbol **isyms = sec->indirect_syms;
num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
if (isyms == NULL || num == 0)
break;
/* Record the starting index in the reserved1 field. */
sec->reserved1 = n;
for (j = 0; j < num; j++, n++)
{
if (isyms[j] == NULL)
cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
else if (isyms[j]->symbol.section == bfd_abs_section_ptr
&& ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
| BFD_MACH_O_INDIRECT_SYM_ABS;
else
cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
}
}
break;
default:
break;
}
}
}
return TRUE;
}
/* Write a dysymtab command.
TODO: Possibly coalesce writes of smaller objects. */
static bfd_boolean
bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
if (cmd->nmodtab != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nmodtab; i++)
{
bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
unsigned int iinit;
unsigned int ninit;
iinit = module->iinit & 0xffff;
iinit |= ((module->iterm & 0xffff) << 16);
ninit = module->ninit & 0xffff;
ninit |= ((module->nterm & 0xffff) << 16);
if (bfd_mach_o_wide_p (abfd))
{
struct mach_o_dylib_module_64_external w;
bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name);
bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym);
bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym);
bfd_h_put_32 (abfd, module->irefsym, &w.irefsym);
bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym);
bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym);
bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym);
bfd_h_put_32 (abfd, module->iextrel, &w.iextrel);
bfd_h_put_32 (abfd, module->nextrel, &w.nextrel);
bfd_h_put_32 (abfd, iinit, &w.iinit_iterm);
bfd_h_put_32 (abfd, ninit, &w.ninit_nterm);
bfd_h_put_64 (abfd, module->objc_module_info_addr,
&w.objc_module_info_addr);
bfd_h_put_32 (abfd, module->objc_module_info_size,
&w.objc_module_info_size);
if (bfd_bwrite ((void *) &w, sizeof (w), abfd) != sizeof (w))
return FALSE;
}
else
{
struct mach_o_dylib_module_external n;
bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name);
bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym);
bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym);
bfd_h_put_32 (abfd, module->irefsym, &n.irefsym);
bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym);
bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym);
bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym);
bfd_h_put_32 (abfd, module->iextrel, &n.iextrel);
bfd_h_put_32 (abfd, module->nextrel, &n.nextrel);
bfd_h_put_32 (abfd, iinit, &n.iinit_iterm);
bfd_h_put_32 (abfd, ninit, &n.ninit_nterm);
bfd_h_put_32 (abfd, module->objc_module_info_addr,
&n.objc_module_info_addr);
bfd_h_put_32 (abfd, module->objc_module_info_size,
&n.objc_module_info_size);
if (bfd_bwrite ((void *) &n, sizeof (n), abfd) != sizeof (n))
return FALSE;
}
}
}
if (cmd->ntoc != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->ntoc; i++)
{
struct mach_o_dylib_table_of_contents_external raw;
bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index);
bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
}
}
if (cmd->nindirectsyms > 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nindirectsyms; ++i)
{
unsigned char raw[4];
bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
}
}
if (cmd->nextrefsyms != 0)
{
unsigned int i;
if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nextrefsyms; i++)
{
unsigned long v;
unsigned char raw[4];
bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
/* Fields isym and flags are written as bit-fields, thus we need
a specific processing for endianness. */
if (bfd_big_endian (abfd))
{
v = ((ref->isym & 0xffffff) << 8);
v |= ref->flags & 0xff;
}
else
{
v = ref->isym & 0xffffff;
v |= ((ref->flags & 0xff) << 24);
}
bfd_h_put_32 (abfd, v, raw);
if (bfd_bwrite (raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
}
}
/* The command. */
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0)
return FALSE;
else
{
struct mach_o_dysymtab_command_external raw;
bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym);
bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym);
bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym);
bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym);
bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym);
bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym);
bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff);
bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc);
bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff);
bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab);
bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff);
bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms);
bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff);
bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms);
bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff);
bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel);
bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff);
bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel);
if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
}
return TRUE;
}
static unsigned
bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
{
unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE;
/* Just leave debug symbols where they are (pretend they are local, and
then they will just be sorted on position). */
if (s->n_type & BFD_MACH_O_N_STAB)
return 0;
/* Local (we should never see an undefined local AFAICT). */
if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
return 0;
/* Common symbols look like undefined externs. */
if (mtyp == BFD_MACH_O_N_UNDF)
return 2;
/* A defined non-local, non-debug symbol. */
return 1;
}
static int
bfd_mach_o_cf_symbols (const void *a, const void *b)
{
bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a;
bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
unsigned int soa, sob;
soa = bfd_mach_o_primary_symbol_sort_key (sa);
sob = bfd_mach_o_primary_symbol_sort_key (sb);
if (soa < sob)
return -1;
if (soa > sob)
return 1;
/* If it's local or stab, just preserve the input order. */
if (soa == 0)
{
if (sa->symbol.udata.i < sb->symbol.udata.i)
return -1;
if (sa->symbol.udata.i > sb->symbol.udata.i)
return 1;
/* This is probably an error. */
return 0;
}
/* The second sort key is name. */
return strcmp (sa->symbol.name, sb->symbol.name);
}
/* Process the symbols.
This should be OK for single-module files - but it is not likely to work
for multi-module shared libraries.
(a) If the application has not filled in the relevant mach-o fields, make
an estimate.
(b) Order them, like this:
( i) local.
(unsorted)
( ii) external defined
(by name)
(iii) external undefined/common
(by name)
( iv) common
(by name)
*/
static bfd_boolean
bfd_mach_o_mangle_symbols (bfd *abfd)
{
unsigned long i;
asymbol **symbols = bfd_get_outsymbols (abfd);
if (symbols == NULL || bfd_get_symcount (abfd) == 0)
return TRUE;
for (i = 0; i < bfd_get_symcount (abfd); i++)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
/* We use this value, which is out-of-range as a symbol index, to signal
that the mach-o-specific data are not filled in and need to be created
from the bfd values. It is much preferable for the application to do
this, since more meaningful diagnostics can be made that way. */
if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
{
/* No symbol information has been set - therefore determine
it from the bfd symbol flags/info. */
if (s->symbol.section == bfd_abs_section_ptr)
s->n_type = BFD_MACH_O_N_ABS;
else if (s->symbol.section == bfd_und_section_ptr)
{
s->n_type = BFD_MACH_O_N_UNDF;
if (s->symbol.flags & BSF_WEAK)
s->n_desc |= BFD_MACH_O_N_WEAK_REF;
/* mach-o automatically makes undefined symbols extern. */
s->n_type |= BFD_MACH_O_N_EXT;
s->symbol.flags |= BSF_GLOBAL;
}
else if (s->symbol.section == bfd_com_section_ptr)
{
s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
s->symbol.flags |= BSF_GLOBAL;
}
else
s->n_type = BFD_MACH_O_N_SECT;
if (s->symbol.flags & BSF_GLOBAL)
s->n_type |= BFD_MACH_O_N_EXT;
}
/* Put the section index in, where required. */
if ((s->symbol.section != bfd_abs_section_ptr
&& s->symbol.section != bfd_und_section_ptr
&& s->symbol.section != bfd_com_section_ptr)
|| ((s->n_type & BFD_MACH_O_N_STAB) != 0
&& s->symbol.name == NULL))
s->n_sect = s->symbol.section->output_section->target_index;
/* Number to preserve order for local and debug syms. */
s->symbol.udata.i = i;
}
/* Sort the symbols. */
qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
sizeof (asymbol *), bfd_mach_o_cf_symbols);
for (i = 0; i < bfd_get_symcount (abfd); ++i)
{
bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
s->symbol.udata.i = i; /* renumber. */
}
return TRUE;
}
/* We build a flat table of sections, which can be re-ordered if necessary.
Fill in the section number and other mach-o-specific data. */
static bfd_boolean
bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
{
asection *sec;
unsigned target_index;
unsigned nsect;
nsect = bfd_count_sections (abfd);
/* Don't do it if it's already set - assume the application knows what it's
doing. */
if (mdata->nsects == nsect
&& (mdata->nsects == 0 || mdata->sections != NULL))
return TRUE;
/* We need to check that this can be done... */
if (nsect > 255)
{
(*_bfd_error_handler) (_("mach-o: there are too many sections (%u)"
" maximum is 255,\n"), nsect);
return FALSE;
}
mdata->nsects = nsect;
mdata->sections = bfd_alloc2 (abfd,
mdata->nsects, sizeof (bfd_mach_o_section *));
if (mdata->sections == NULL)
return FALSE;
/* Create Mach-O sections.
Section type, attribute and align should have been set when the
section was created - either read in or specified. */
target_index = 0;
for (sec = abfd->sections; sec; sec = sec->next)
{
unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
mdata->sections[target_index] = msect;
msect->addr = bfd_get_section_vma (abfd, sec);
msect->size = bfd_get_section_size (sec);
/* Use the largest alignment set, in case it was bumped after the
section was created. */
msect->align = msect->align > bfd_align ? msect->align : bfd_align;
msect->offset = 0;
sec->target_index = ++target_index;
}
return TRUE;
}
bfd_boolean
bfd_mach_o_write_contents (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_load_command *cmd;
bfd_mach_o_symtab_command *symtab = NULL;
bfd_mach_o_dysymtab_command *dysymtab = NULL;
bfd_mach_o_segment_command *linkedit = NULL;
/* Make the commands, if not already present. */
if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
return FALSE;
abfd->output_has_begun = TRUE;
/* Write the header. */
if (!bfd_mach_o_write_header (abfd, &mdata->header))
return FALSE;
/* First pass: allocate the linkedit segment. */
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
switch (cmd->type)
{
case BFD_MACH_O_LC_SEGMENT_64:
case BFD_MACH_O_LC_SEGMENT:
if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0)
linkedit = &cmd->command.segment;
break;
case BFD_MACH_O_LC_SYMTAB:
symtab = &cmd->command.symtab;
break;
case BFD_MACH_O_LC_DYSYMTAB:
dysymtab = &cmd->command.dysymtab;
break;
case BFD_MACH_O_LC_DYLD_INFO:
{
bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info;
if (di->rebase_size != 0)
{
di->rebase_off = mdata->filelen;
mdata->filelen += di->rebase_size;
}
if (di->bind_size != 0)
{
di->bind_off = mdata->filelen;
mdata->filelen += di->bind_size;
}
if (di->weak_bind_size != 0)
{
di->weak_bind_off = mdata->filelen;
mdata->filelen += di->weak_bind_size;
}
if (di->lazy_bind_size != 0)
{
di->lazy_bind_off = mdata->filelen;
mdata->filelen += di->lazy_bind_size;
}
if (di->export_size != 0)
{
di->export_off = mdata->filelen;
mdata->filelen += di->export_size;
}
}
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LOAD_DYLINKER:
case BFD_MACH_O_LC_MAIN:
/* Nothing to do. */
break;
default:
(*_bfd_error_handler)
(_("unable to allocate data for load command 0x%lx"),
(unsigned long) cmd->type);
break;
}
/* Specially handle symtab and dysymtab. */
/* Pre-allocate the symbol table (but not the string table). The reason
is that the dysymtab is after the symbol table but before the string
table (required by the native strip tool). */
if (symtab != NULL)
{
unsigned int symlen;
unsigned int wide = bfd_mach_o_wide_p (abfd);
symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
/* Align for symbols. */
mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
symtab->symoff = mdata->filelen;
symtab->nsyms = bfd_get_symcount (abfd);
mdata->filelen += symtab->nsyms * symlen;
}
/* Build the dysymtab. */
if (dysymtab != NULL)
if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
return FALSE;
/* Write symtab and strtab. */
if (symtab != NULL)
if (!bfd_mach_o_write_symtab_content (abfd, symtab))
return FALSE;
/* Adjust linkedit size. */
if (linkedit != NULL)
{
/* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
linkedit->vmsize = mdata->filelen - linkedit->fileoff;
/* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
linkedit->filesize = mdata->filelen - linkedit->fileoff;
linkedit->initprot = BFD_MACH_O_PROT_READ;
linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_EXECUTE;
}
/* Second pass: write commands. */
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
struct mach_o_load_command_external raw;
unsigned long typeflag;
typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
bfd_h_put_32 (abfd, typeflag, raw.cmd);
bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
|| bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
return FALSE;
switch (cmd->type)
{
case BFD_MACH_O_LC_SEGMENT:
if (!bfd_mach_o_write_segment_32 (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_SEGMENT_64:
if (!bfd_mach_o_write_segment_64 (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_SYMTAB:
if (!bfd_mach_o_write_symtab (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_DYSYMTAB:
if (!bfd_mach_o_write_dysymtab (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_THREAD:
case BFD_MACH_O_LC_UNIXTHREAD:
if (!bfd_mach_o_write_thread (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
if (!bfd_mach_o_write_dylib (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
if (!bfd_mach_o_write_dylinker (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_MAIN:
if (!bfd_mach_o_write_main (abfd, cmd))
return FALSE;
break;
case BFD_MACH_O_LC_DYLD_INFO:
if (!bfd_mach_o_write_dyld_info (abfd, cmd))
return FALSE;
break;
default:
(*_bfd_error_handler)
(_("unable to write unknown load command 0x%lx"),
(unsigned long) cmd->type);
return FALSE;
}
}
return TRUE;
}
static void
bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
bfd_mach_o_section *s)
{
if (seg->sect_head == NULL)
seg->sect_head = s;
else
seg->sect_tail->next = s;
seg->sect_tail = s;
}
/* Create section Mach-O flags from BFD flags. */
static void
bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
asection *sec)
{
flagword bfd_flags;
bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
/* Create default flags. */
bfd_flags = bfd_get_section_flags (abfd, sec);
if ((bfd_flags & SEC_CODE) == SEC_CODE)
s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
| BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
| BFD_MACH_O_S_REGULAR;
else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
s->flags = BFD_MACH_O_S_ZEROFILL;
else if (bfd_flags & SEC_DEBUGGING)
s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
else
s->flags = BFD_MACH_O_S_REGULAR;
}
static bfd_boolean
bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int i, j;
seg->vmaddr = 0;
seg->fileoff = mdata->filelen;
seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_EXECUTE;
seg->maxprot = seg->initprot;
/* Append sections to the segment.
This is a little tedious, we have to honor the need to account zerofill
sections after all the rest. This forces us to do the calculation of
total vmsize in three passes so that any alignment increments are
properly accounted. */
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *s = mdata->sections[i];
asection *sec = s->bfdsection;
/* Although we account for zerofill section sizes in vm order, they are
placed in the file in source sequence. */
bfd_mach_o_append_section_to_segment (seg, s);
s->offset = 0;
/* Zerofill sections have zero file size & offset, the only content
written to the file is the symbols. */
if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
|| ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
== BFD_MACH_O_S_GB_ZEROFILL))
continue;
/* The Darwin system tools (in MH_OBJECT files, at least) always account
sections, even those with zero size. */
if (s->size > 0)
{
seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
seg->vmsize += s->size;
/* MH_OBJECT files have unaligned content. */
if (1)
{
seg->filesize = FILE_ALIGN (seg->filesize, s->align);
mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
}
seg->filesize += s->size;
/* The system tools write even zero-sized sections with an offset
field set to the current file position. */
s->offset = mdata->filelen;
}
sec->filepos = s->offset;
mdata->filelen += s->size;
}
/* Now pass through again, for zerofill, only now we just update the
vmsize, and then for zerofill_GB. */
for (j = 0; j < 2; j++)
{
unsigned int stype;
if (j == 0)
stype = BFD_MACH_O_S_ZEROFILL;
else
stype = BFD_MACH_O_S_GB_ZEROFILL;
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *s = mdata->sections[i];
if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype)
continue;
if (s->size > 0)
{
seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
seg->vmsize += s->size;
}
}
}
/* Allocate space for the relocations. */
mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
for (i = 0; i < mdata->nsects; ++i)
{
bfd_mach_o_section *ms = mdata->sections[i];
asection *sec = ms->bfdsection;
ms->nreloc = sec->reloc_count;
if (ms->nreloc == 0)
{
/* Clear nreloc and reloff if there is no relocs. */
ms->reloff = 0;
continue;
}
sec->rel_filepos = mdata->filelen;
ms->reloff = sec->rel_filepos;
mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int i;
bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1;
bfd_vma vma;
bfd_mach_o_section *s;
seg->vmsize = 0;
seg->fileoff = mdata->filelen;
seg->maxprot = 0;
seg->initprot = 0;
seg->flags = 0;
/* Append sections to the segment. We assume they are properly ordered
by vma (but we check that). */
vma = 0;
for (i = 0; i < mdata->nsects; ++i)
{
s = mdata->sections[i];
/* Consider only sections for this segment. */
if (strcmp (seg->segname, s->segname) != 0)
continue;
bfd_mach_o_append_section_to_segment (seg, s);
if (s->addr < vma)
{
(*_bfd_error_handler)
(_("section address (%lx) below start of segment (%lx)"),
(unsigned long) s->addr, (unsigned long) vma);
return FALSE;
}
vma = s->addr + s->size;
}
/* Set segment file offset: make it page aligned. */
vma = seg->sect_head->addr;
seg->vmaddr = vma & ~pagemask;
if ((mdata->filelen & pagemask) > (vma & pagemask))
mdata->filelen += pagemask + 1;
seg->fileoff = mdata->filelen & ~pagemask;
mdata->filelen = seg->fileoff + (vma & pagemask);
/* Set section file offset. */
for (s = seg->sect_head; s != NULL; s = s->next)
{
asection *sec = s->bfdsection;
flagword flags = bfd_get_section_flags (abfd, sec);
/* Adjust segment size. */
seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
seg->vmsize += s->size;
/* File offset and length. */
seg->filesize = FILE_ALIGN (seg->filesize, s->align);
if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL
&& ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
!= BFD_MACH_O_S_GB_ZEROFILL))
{
mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
s->offset = mdata->filelen;
s->bfdsection->filepos = s->offset;
seg->filesize += s->size;
mdata->filelen += s->size;
}
else
{
s->offset = 0;
s->bfdsection->filepos = 0;
}
/* Set protection. */
if (flags & SEC_LOAD)
{
if (flags & SEC_CODE)
seg->initprot |= BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_EXECUTE;
if ((flags & (SEC_DATA | SEC_READONLY)) == SEC_DATA)
seg->initprot |= BFD_MACH_O_PROT_WRITE | BFD_MACH_O_PROT_READ;
}
/* Relocs shouldn't appear in non-object files. */
if (s->bfdsection->reloc_count != 0)
return FALSE;
}
/* Set maxprot. */
if (seg->initprot != 0)
seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_EXECUTE;
else
seg->maxprot = 0;
/* Round segment size (and file size). */
seg->vmsize = (seg->vmsize + pagemask) & ~pagemask;
seg->filesize = (seg->filesize + pagemask) & ~pagemask;
mdata->filelen = (mdata->filelen + pagemask) & ~pagemask;
return TRUE;
}
/* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
fields in header. */
static bfd_boolean
bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
{
unsigned wide = mach_o_wide_p (&mdata->header);
unsigned int hdrlen;
ufile_ptr offset;
bfd_mach_o_load_command *cmd;
unsigned int align;
bfd_boolean ret = TRUE;
hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
align = wide ? 8 - 1 : 4 - 1;
offset = hdrlen;
mdata->header.ncmds = 0;
for (cmd = mdata->first_command; cmd; cmd = cmd->next)
{
mdata->header.ncmds++;
cmd->offset = offset;
switch (cmd->type)
{
case BFD_MACH_O_LC_SEGMENT_64:
cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
+ BFD_MACH_O_SECTION_64_SIZE * cmd->command.segment.nsects;
break;
case BFD_MACH_O_LC_SEGMENT:
cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
+ BFD_MACH_O_SECTION_SIZE * cmd->command.segment.nsects;
break;
case BFD_MACH_O_LC_SYMTAB:
cmd->len = sizeof (struct mach_o_symtab_command_external)
+ BFD_MACH_O_LC_SIZE;
break;
case BFD_MACH_O_LC_DYSYMTAB:
cmd->len = sizeof (struct mach_o_dysymtab_command_external)
+ BFD_MACH_O_LC_SIZE;
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
cmd->len = sizeof (struct mach_o_dylib_command_external)
+ BFD_MACH_O_LC_SIZE;
cmd->command.dylib.name_offset = cmd->len;
cmd->len += strlen (cmd->command.dylib.name_str);
cmd->len = (cmd->len + align) & ~align;
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
cmd->len = sizeof (struct mach_o_str_command_external)
+ BFD_MACH_O_LC_SIZE;
cmd->command.dylinker.name_offset = cmd->len;
cmd->len += strlen (cmd->command.dylinker.name_str);
cmd->len = (cmd->len + align) & ~align;
break;
case BFD_MACH_O_LC_MAIN:
cmd->len = sizeof (struct mach_o_entry_point_command_external)
+ BFD_MACH_O_LC_SIZE;
break;
case BFD_MACH_O_LC_DYLD_INFO:
cmd->len = sizeof (struct mach_o_dyld_info_command_external)
+ BFD_MACH_O_LC_SIZE;
break;
default:
(*_bfd_error_handler)
(_("unable to layout unknown load command 0x%lx"),
(unsigned long) cmd->type);
ret = FALSE;
break;
}
BFD_ASSERT (cmd->len % (align + 1) == 0);
offset += cmd->len;
}
mdata->header.sizeofcmds = offset - hdrlen;
mdata->filelen = offset;
return ret;
}
/* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
segment. */
static void
bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata,
bfd_mach_o_load_command *cmd,
const char *segname, unsigned int nbr_sect)
{
bfd_mach_o_segment_command *seg = &cmd->command.segment;
unsigned wide = mach_o_wide_p (&mdata->header);
/* Init segment command. */
cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT;
cmd->type_required = FALSE;
strcpy (seg->segname, segname);
seg->nsects = nbr_sect;
seg->vmaddr = 0;
seg->vmsize = 0;
seg->fileoff = 0;
seg->filesize = 0;
seg->maxprot = 0;
seg->initprot = 0;
seg->flags = 0;
seg->sect_head = NULL;
seg->sect_tail = NULL;
}
/* Build Mach-O load commands (currently assuming an MH_OBJECT file).
TODO: Other file formats, rebuilding symtab/dysymtab commands for strip
and copy functionality. */
bfd_boolean
bfd_mach_o_build_commands (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned wide = mach_o_wide_p (&mdata->header);
unsigned int nbr_segcmd = 0;
bfd_mach_o_load_command *commands;
unsigned int nbr_commands;
int symtab_idx = -1;
int dysymtab_idx = -1;
int main_idx = -1;
unsigned int i;
/* Return now if already built. */
if (mdata->header.ncmds != 0)
return TRUE;
/* Fill in the file type, if not already set. */
if (mdata->header.filetype == 0)
{
if (abfd->flags & EXEC_P)
mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
else if (abfd->flags & DYNAMIC)
mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
else
mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
}
/* If hasn't already been done, flatten sections list, and sort
if/when required. Must be done before the symbol table is adjusted,
since that depends on properly numbered sections. */
if (mdata->nsects == 0 || mdata->sections == NULL)
if (! bfd_mach_o_mangle_sections (abfd, mdata))
return FALSE;
/* Order the symbol table, fill-in/check mach-o specific fields and
partition out any indirect symbols. */
if (!bfd_mach_o_mangle_symbols (abfd))
return FALSE;
/* Segment commands. */
if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
{
/* Only one segment for all the sections. But the segment is
optional if there is no sections. */
nbr_segcmd = (mdata->nsects > 0) ? 1 : 0;
}
else
{
bfd_mach_o_section *prev_sect = NULL;
/* One pagezero segment and one linkedit segment. */
nbr_segcmd = 2;
/* Create one segment for associated segment name in sections.
Assume that sections with the same segment name are consecutive. */
for (i = 0; i < mdata->nsects; i++)
{
bfd_mach_o_section *this_sect = mdata->sections[i];
if (prev_sect == NULL
|| strcmp (prev_sect->segname, this_sect->segname) != 0)
{
nbr_segcmd++;
prev_sect = this_sect;
}
}
}
nbr_commands = nbr_segcmd;
/* One command for the symbol table (only if there are symbols. */
if (bfd_get_symcount (abfd) > 0)
symtab_idx = nbr_commands++;
/* FIXME:
This is a rather crude test for whether we should build a dysymtab. */
if (bfd_mach_o_should_emit_dysymtab ()
&& bfd_get_symcount (abfd))
{
/* If there should be a case where a dysymtab could be emitted without
a symtab (seems improbable), this would need amending. */
dysymtab_idx = nbr_commands++;
}
/* Add an entry point command. */
if (mdata->header.filetype == BFD_MACH_O_MH_EXECUTE
&& bfd_get_start_address (abfd) != 0)
main_idx = nbr_commands++;
/* Well, we must have a header, at least. */
mdata->filelen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
/* A bit unusual, but no content is valid;
as -n empty.s -o empty.o */
if (nbr_commands == 0)
{
/* Layout commands (well none...) and set headers command fields. */
return bfd_mach_o_layout_commands (mdata);
}
/* Create commands for segments (and symtabs), prepend them. */
commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command));
if (commands == NULL)
return FALSE;
for (i = 0; i < nbr_commands - 1; i++)
commands[i].next = &commands[i + 1];
commands[nbr_commands - 1].next = mdata->first_command;
if (mdata->first_command == NULL)
mdata->last_command = &commands[nbr_commands - 1];
mdata->first_command = &commands[0];
if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT && nbr_segcmd != 0)
{
/* For object file, there is only one segment. */
bfd_mach_o_init_segment (mdata, &commands[0], "", mdata->nsects);
}
else if (nbr_segcmd != 0)
{
bfd_mach_o_load_command *cmd;
BFD_ASSERT (nbr_segcmd >= 2);
/* The pagezero. */
cmd = &commands[0];
bfd_mach_o_init_segment (mdata, cmd, "__PAGEZERO", 0);
/* Segments from sections. */
cmd++;
for (i = 0; i < mdata->nsects;)
{
const char *segname = mdata->sections[i]->segname;
unsigned int nbr_sect = 1;
/* Count number of sections for this segment. */
for (i++; i < mdata->nsects; i++)
if (strcmp (mdata->sections[i]->segname, segname) == 0)
nbr_sect++;
else
break;
bfd_mach_o_init_segment (mdata, cmd, segname, nbr_sect);
cmd++;
}
/* The linkedit. */
bfd_mach_o_init_segment (mdata, cmd, "__LINKEDIT", 0);
}
if (symtab_idx >= 0)
{
/* Init symtab command. */
bfd_mach_o_load_command *cmd = &commands[symtab_idx];
cmd->type = BFD_MACH_O_LC_SYMTAB;
cmd->type_required = FALSE;
}
/* If required, setup symtab command, see comment above about the quality
of this test. */
if (dysymtab_idx >= 0)
{
bfd_mach_o_load_command *cmd = &commands[dysymtab_idx];
cmd->type = BFD_MACH_O_LC_DYSYMTAB;
cmd->type_required = FALSE;
}
/* Create the main command. */
if (main_idx >= 0)
{
bfd_mach_o_load_command *cmd = &commands[main_idx];
cmd->type = BFD_MACH_O_LC_MAIN;
cmd->type_required = TRUE;
cmd->command.main.entryoff = 0;
cmd->command.main.stacksize = 0;
}
/* Layout commands. */
if (! bfd_mach_o_layout_commands (mdata))
return FALSE;
/* So, now we have sized the commands and the filelen set to that.
Now we can build the segment command and set the section file offsets. */
if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
{
for (i = 0; i < nbr_segcmd; i++)
if (!bfd_mach_o_build_obj_seg_command
(abfd, &commands[i].command.segment))
return FALSE;
}
else
{
bfd_vma maxvma = 0;
/* Skip pagezero and linkedit segments. */
for (i = 1; i < nbr_segcmd - 1; i++)
{
bfd_mach_o_segment_command *seg = &commands[i].command.segment;
if (!bfd_mach_o_build_exec_seg_command (abfd, seg))
return FALSE;
if (seg->vmaddr + seg->vmsize > maxvma)
maxvma = seg->vmaddr + seg->vmsize;
}
/* Set the size of __PAGEZERO. */
commands[0].command.segment.vmsize =
commands[1].command.segment.vmaddr;
/* Set the vma and fileoff of __LINKEDIT. */
commands[nbr_segcmd - 1].command.segment.vmaddr = maxvma;
commands[nbr_segcmd - 1].command.segment.fileoff = mdata->filelen;
/* Set entry point (once segments have been laid out). */
if (main_idx >= 0)
commands[main_idx].command.main.entryoff =
bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
}
return TRUE;
}
/* Set the contents of a section. */
bfd_boolean
bfd_mach_o_set_section_contents (bfd *abfd,
asection *section,
const void * location,
file_ptr offset,
bfd_size_type count)
{
file_ptr pos;
/* Trying to write the first section contents will trigger the creation of
the load commands if they are not already present. */
if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
return FALSE;
if (count == 0)
return TRUE;
pos = section->filepos + offset;
if (bfd_seek (abfd, pos, SEEK_SET) != 0
|| bfd_bwrite (location, count, abfd) != count)
return FALSE;
return TRUE;
}
int
bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
struct bfd_link_info *info ATTRIBUTE_UNUSED)
{
return 0;
}
/* Make an empty symbol. This is required only because
bfd_make_section_anyway wants to create a symbol for the section. */
asymbol *
bfd_mach_o_make_empty_symbol (bfd *abfd)
{
asymbol *new_symbol;
new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
if (new_symbol == NULL)
return new_symbol;
new_symbol->the_bfd = abfd;
new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET;
return new_symbol;
}
static bfd_boolean
bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
{
struct mach_o_header_external raw;
unsigned int size;
bfd_vma (*get32) (const void *) = NULL;
/* Just read the magic number. */
if (bfd_seek (abfd, 0, SEEK_SET) != 0
|| bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
return FALSE;
if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
{
header->byteorder = BFD_ENDIAN_BIG;
header->magic = BFD_MACH_O_MH_MAGIC;
header->version = 1;
get32 = bfd_getb32;
}
else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
{
header->byteorder = BFD_ENDIAN_LITTLE;
header->magic = BFD_MACH_O_MH_MAGIC;
header->version = 1;
get32 = bfd_getl32;
}
else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
{
header->byteorder = BFD_ENDIAN_BIG;
header->magic = BFD_MACH_O_MH_MAGIC_64;
header->version = 2;
get32 = bfd_getb32;
}
else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
{
header->byteorder = BFD_ENDIAN_LITTLE;
header->magic = BFD_MACH_O_MH_MAGIC_64;
header->version = 2;
get32 = bfd_getl32;
}
else
{
header->byteorder = BFD_ENDIAN_UNKNOWN;
return FALSE;
}
/* Once the size of the header is known, read the full header. */
size = mach_o_wide_p (header) ?
BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
if (bfd_seek (abfd, 0, SEEK_SET) != 0
|| bfd_bread (&raw, size, abfd) != size)
return FALSE;
header->cputype = (*get32) (raw.cputype);
header->cpusubtype = (*get32) (raw.cpusubtype);
header->filetype = (*get32) (raw.filetype);
header->ncmds = (*get32) (raw.ncmds);
header->sizeofcmds = (*get32) (raw.sizeofcmds);
header->flags = (*get32) (raw.flags);
if (mach_o_wide_p (header))
header->reserved = (*get32) (raw.reserved);
else
header->reserved = 0;
return TRUE;
}
bfd_boolean
bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
{
bfd_mach_o_section *s;
unsigned bfdalign = bfd_get_section_alignment (abfd, sec);
s = bfd_mach_o_get_mach_o_section (sec);
if (s == NULL)
{
flagword bfd_flags;
static const mach_o_section_name_xlat * xlat;
s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
if (s == NULL)
return FALSE;
sec->used_by_bfd = s;
s->bfdsection = sec;
/* Create the Darwin seg/sect name pair from the bfd name.
If this is a canonical name for which a specific paiting exists
there will also be defined flags, type, attribute and alignment
values. */
xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
if (xlat != NULL)
{
s->flags = xlat->macho_sectype | xlat->macho_secattr;
s->align = xlat->sectalign > bfdalign ? xlat->sectalign
: bfdalign;
(void) bfd_set_section_alignment (abfd, sec, s->align);
bfd_flags = bfd_get_section_flags (abfd, sec);
if (bfd_flags == SEC_NO_FLAGS)
bfd_set_section_flags (abfd, sec, xlat->bfd_flags);
}
else
/* Create default flags. */
bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
}
return _bfd_generic_new_section_hook (abfd, sec);
}
static void
bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
unsigned long prot)
{
flagword flags;
bfd_mach_o_section *section;
flags = bfd_get_section_flags (abfd, sec);
section = bfd_mach_o_get_mach_o_section (sec);
/* TODO: see if we should use the xlat system for doing this by
preference and fall back to this for unknown sections. */
if (flags == SEC_NO_FLAGS)
{
/* Try to guess flags. */
if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
flags = SEC_DEBUGGING;
else
{
flags = SEC_ALLOC;
if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
!= BFD_MACH_O_S_ZEROFILL)
{
flags |= SEC_LOAD;
if (prot & BFD_MACH_O_PROT_EXECUTE)
flags |= SEC_CODE;
if (prot & BFD_MACH_O_PROT_WRITE)
flags |= SEC_DATA;
else if (prot & BFD_MACH_O_PROT_READ)
flags |= SEC_READONLY;
}
}
}
else
{
if ((flags & SEC_DEBUGGING) == 0)
flags |= SEC_ALLOC;
}
if (section->offset != 0)
flags |= SEC_HAS_CONTENTS;
if (section->nreloc != 0)
flags |= SEC_RELOC;
bfd_set_section_flags (abfd, sec, flags);
sec->vma = section->addr;
sec->lma = section->addr;
sec->size = section->size;
sec->filepos = section->offset;
sec->alignment_power = section->align;
sec->segment_mark = 0;
sec->reloc_count = section->nreloc;
sec->rel_filepos = section->reloff;
}
static asection *
bfd_mach_o_make_bfd_section (bfd *abfd,
const unsigned char *segname,
const unsigned char *sectname)
{
const char *sname;
flagword flags;
bfd_mach_o_convert_section_name_to_bfd
(abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
if (sname == NULL)
return NULL;
return bfd_make_section_anyway_with_flags (abfd, sname, flags);
}
static asection *
bfd_mach_o_read_section_32 (bfd *abfd,
unsigned int offset,
unsigned long prot)
{
struct mach_o_section_32_external raw;
asection *sec;
bfd_mach_o_section *section;
if (bfd_seek (abfd, offset, SEEK_SET) != 0
|| (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
!= BFD_MACH_O_SECTION_SIZE))
return NULL;
sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
if (sec == NULL)
return NULL;
section = bfd_mach_o_get_mach_o_section (sec);
memcpy (section->segname, raw.segname, sizeof (raw.segname));
section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
section->addr = bfd_h_get_32 (abfd, raw.addr);
section->size = bfd_h_get_32 (abfd, raw.size);
section->offset = bfd_h_get_32 (abfd, raw.offset);
section->align = bfd_h_get_32 (abfd, raw.align);
/* PR 17512: file: 0017eb76. */
if (section->align > 64)
{
(*_bfd_error_handler) (_("bfd_mach_o_read_section_32: overlarge alignment value: 0x%x, using 32 instead"),
section->align);
section->align = 32;
}
section->reloff = bfd_h_get_32 (abfd, raw.reloff);
section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
section->flags = bfd_h_get_32 (abfd, raw.flags);
section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
section->reserved3 = 0;
bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
return sec;
}
static asection *
bfd_mach_o_read_section_64 (bfd *abfd,
unsigned int offset,
unsigned long prot)
{
struct mach_o_section_64_external raw;
asection *sec;
bfd_mach_o_section *section;
if (bfd_seek (abfd, offset, SEEK_SET) != 0
|| (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
!= BFD_MACH_O_SECTION_64_SIZE))
return NULL;
sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
if (sec == NULL)
return NULL;
section = bfd_mach_o_get_mach_o_section (sec);
memcpy (section->segname, raw.segname, sizeof (raw.segname));
section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
section->addr = bfd_h_get_64 (abfd, raw.addr);
section->size = bfd_h_get_64 (abfd, raw.size);
section->offset = bfd_h_get_32 (abfd, raw.offset);
section->align = bfd_h_get_32 (abfd, raw.align);
if (section->align > 64)
{
(*_bfd_error_handler) (_("bfd_mach_o_read_section_64: overlarge alignment value: 0x%x, using 32 instead"),
section->align);
section->align = 32;
}
section->reloff = bfd_h_get_32 (abfd, raw.reloff);
section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
section->flags = bfd_h_get_32 (abfd, raw.flags);
section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
return sec;
}
static asection *
bfd_mach_o_read_section (bfd *abfd,
unsigned int offset,
unsigned long prot,
unsigned int wide)
{
if (wide)
return bfd_mach_o_read_section_64 (abfd, offset, prot);
else
return bfd_mach_o_read_section_32 (abfd, offset, prot);
}
static bfd_boolean
bfd_mach_o_read_symtab_symbol (bfd *abfd,
bfd_mach_o_symtab_command *sym,
bfd_mach_o_asymbol *s,
unsigned long i)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int wide = mach_o_wide_p (&mdata->header);
unsigned int symwidth =
wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
unsigned int symoff = sym->symoff + (i * symwidth);
struct mach_o_nlist_64_external raw;
unsigned char type = -1;
unsigned char section = -1;
short desc = -1;
symvalue value = -1;
unsigned long stroff = -1;
unsigned int symtype = -1;
BFD_ASSERT (sym->strtab != NULL);
if (bfd_seek (abfd, symoff, SEEK_SET) != 0
|| bfd_bread (&raw, symwidth, abfd) != symwidth)
{
(*_bfd_error_handler)
(_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
symwidth, (unsigned long) symoff);
return FALSE;
}
stroff = bfd_h_get_32 (abfd, raw.n_strx);
type = bfd_h_get_8 (abfd, raw.n_type);
symtype = type & BFD_MACH_O_N_TYPE;
section = bfd_h_get_8 (abfd, raw.n_sect);
desc = bfd_h_get_16 (abfd, raw.n_desc);
if (wide)
value = bfd_h_get_64 (abfd, raw.n_value);
else
value = bfd_h_get_32 (abfd, raw.n_value);
if (stroff >= sym->strsize)
{
(*_bfd_error_handler)
(_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
(unsigned long) stroff,
(unsigned long) sym->strsize);
return FALSE;
}
s->symbol.the_bfd = abfd;
s->symbol.name = sym->strtab + stroff;
s->symbol.value = value;
s->symbol.flags = 0x0;
s->symbol.udata.i = i;
s->n_type = type;
s->n_sect = section;
s->n_desc = desc;
if (type & BFD_MACH_O_N_STAB)
{
s->symbol.flags |= BSF_DEBUGGING;
s->symbol.section = bfd_und_section_ptr;
switch (type)
{
case N_FUN:
case N_STSYM:
case N_LCSYM:
case N_BNSYM:
case N_SLINE:
case N_ENSYM:
case N_ECOMM:
case N_ECOML:
case N_GSYM:
if ((section > 0) && (section <= mdata->nsects))
{
s->symbol.section = mdata->sections[section - 1]->bfdsection;
s->symbol.value =
s->symbol.value - mdata->sections[section - 1]->addr;
}
break;
}
}
else
{
if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
s->symbol.flags |= BSF_GLOBAL;
else
s->symbol.flags |= BSF_LOCAL;
switch (symtype)
{
case BFD_MACH_O_N_UNDF:
if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
&& s->symbol.value != 0)
{
/* A common symbol. */
s->symbol.section = bfd_com_section_ptr;
s->symbol.flags = BSF_NO_FLAGS;
}
else
{
s->symbol.section = bfd_und_section_ptr;
if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
s->symbol.flags |= BSF_WEAK;
}
break;
case BFD_MACH_O_N_PBUD:
s->symbol.section = bfd_und_section_ptr;
break;
case BFD_MACH_O_N_ABS:
s->symbol.section = bfd_abs_section_ptr;
break;
case BFD_MACH_O_N_SECT:
if ((section > 0) && (section <= mdata->nsects))
{
s->symbol.section = mdata->sections[section - 1]->bfdsection;
s->symbol.value =
s->symbol.value - mdata->sections[section - 1]->addr;
}
else
{
/* Mach-O uses 0 to mean "no section"; not an error. */
if (section != 0)
{
(*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
"symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
s->symbol.name, section, mdata->nsects);
}
s->symbol.section = bfd_und_section_ptr;
}
break;
case BFD_MACH_O_N_INDR:
/* FIXME: we don't follow the BFD convention as this indirect symbol
won't be followed by the referenced one. This looks harmless
unless we start using the linker. */
s->symbol.flags |= BSF_INDIRECT;
s->symbol.section = bfd_ind_section_ptr;
s->symbol.value = 0;
break;
default:
(*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
"symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
s->symbol.name, symtype);
s->symbol.section = bfd_und_section_ptr;
break;
}
}
return TRUE;
}
bfd_boolean
bfd_mach_o_read_symtab_strtab (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_symtab_command *sym = mdata->symtab;
/* Fail if there is no symtab. */
if (sym == NULL)
return FALSE;
/* Success if already loaded. */
if (sym->strtab)
return TRUE;
if (abfd->flags & BFD_IN_MEMORY)
{
struct bfd_in_memory *b;
b = (struct bfd_in_memory *) abfd->iostream;
if ((sym->stroff + sym->strsize) > b->size)
{
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
sym->strtab = (char *) b->buffer + sym->stroff;
}
else
{
sym->strtab = bfd_alloc (abfd, sym->strsize + 1);
if (sym->strtab == NULL)
return FALSE;
if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
|| bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize)
{
/* PR 17512: file: 10888-1609-0.004. */
bfd_release (abfd, sym->strtab);
sym->strtab = NULL;
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
/* Zero terminate the string table. */
sym->strtab[sym->strsize] = 0;
}
return TRUE;
}
bfd_boolean
bfd_mach_o_read_symtab_symbols (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_symtab_command *sym = mdata->symtab;
unsigned long i;
if (sym == NULL || sym->symbols)
/* Return now if there are no symbols or if already loaded. */
return TRUE;
sym->symbols = bfd_alloc2 (abfd, sym->nsyms, sizeof (bfd_mach_o_asymbol));
if (sym->symbols == NULL)
{
(*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
sym->nsyms = 0;
return FALSE;
}
if (!bfd_mach_o_read_symtab_strtab (abfd))
goto fail;
for (i = 0; i < sym->nsyms; i++)
if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
goto fail;
return TRUE;
fail:
bfd_release (abfd, sym->symbols);
sym->symbols = NULL;
sym->nsyms = 0;
return FALSE;
}
static const char *
bfd_mach_o_i386_flavour_string (unsigned int flavour)
{
switch ((int) flavour)
{
case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
default: return "UNKNOWN";
}
}
static const char *
bfd_mach_o_ppc_flavour_string (unsigned int flavour)
{
switch ((int) flavour)
{
case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
default: return "UNKNOWN";
}
}
static bfd_boolean
bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
struct mach_o_str_command_external raw;
unsigned int nameoff;
unsigned int namelen;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
nameoff = bfd_h_get_32 (abfd, raw.str);
cmd->name_offset = nameoff;
namelen = command->len - nameoff;
nameoff += command->offset;
cmd->name_str = bfd_alloc (abfd, namelen);
if (cmd->name_str == NULL)
return FALSE;
if (bfd_seek (abfd, nameoff, SEEK_SET) != 0
|| bfd_bread (cmd->name_str, namelen, abfd) != namelen)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dylib_command *cmd = &command->command.dylib;
struct mach_o_dylib_command_external raw;
unsigned int nameoff;
unsigned int namelen;
switch (command->type)
{
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
case BFD_MACH_O_LC_ID_DYLIB:
case BFD_MACH_O_LC_REEXPORT_DYLIB:
case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
break;
default:
BFD_FAIL ();
return FALSE;
}
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
nameoff = bfd_h_get_32 (abfd, raw.name);
cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
cmd->name_offset = command->offset + nameoff;
namelen = command->len - nameoff;
cmd->name_str = bfd_alloc (abfd, namelen);
if (cmd->name_str == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
|| bfd_bread (cmd->name_str, namelen, abfd) != namelen)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_prebound_dylib (bfd *abfd,
bfd_mach_o_load_command *command)
{
bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
struct mach_o_prebound_dylib_command_external raw;
unsigned int nameoff;
unsigned int modoff;
unsigned int str_len;
unsigned char *str;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
nameoff = bfd_h_get_32 (abfd, raw.name);
modoff = bfd_h_get_32 (abfd, raw.linked_modules);
if (nameoff > command->len || modoff > command->len)
return FALSE;
str_len = command->len - sizeof (raw);
str = bfd_alloc (abfd, str_len);
if (str == NULL)
return FALSE;
if (bfd_bread (str, str_len, abfd) != str_len)
return FALSE;
cmd->name_offset = command->offset + nameoff;
cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
cmd->linked_modules_offset = command->offset + modoff;
cmd->name_str = (char *)str + nameoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
cmd->linked_modules = str + modoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_prebind_cksum (bfd *abfd,
bfd_mach_o_load_command *command)
{
bfd_mach_o_prebind_cksum_command *cmd = &command->command.prebind_cksum;
struct mach_o_prebind_cksum_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->cksum = bfd_get_32 (abfd, raw.cksum);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_twolevel_hints (bfd *abfd,
bfd_mach_o_load_command *command)
{
bfd_mach_o_twolevel_hints_command *cmd = &command->command.twolevel_hints;
struct mach_o_twolevel_hints_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->offset = bfd_get_32 (abfd, raw.offset);
cmd->nhints = bfd_get_32 (abfd, raw.nhints);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
struct mach_o_fvmlib_command_external raw;
unsigned int nameoff;
unsigned int namelen;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
nameoff = bfd_h_get_32 (abfd, raw.name);
fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version);
fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr);
fvm->name_offset = command->offset + nameoff;
namelen = command->len - nameoff;
fvm->name_str = bfd_alloc (abfd, namelen);
if (fvm->name_str == NULL)
return FALSE;
if (bfd_seek (abfd, fvm->name_offset, SEEK_SET) != 0
|| bfd_bread (fvm->name_str, namelen, abfd) != namelen)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_thread_command *cmd = &command->command.thread;
unsigned int offset;
unsigned int nflavours;
unsigned int i;
BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
|| (command->type == BFD_MACH_O_LC_UNIXTHREAD));
/* Count the number of threads. */
offset = 8;
nflavours = 0;
while (offset != command->len)
{
struct mach_o_thread_command_external raw;
if (offset >= command->len)
return FALSE;
if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
nflavours++;
}
/* Allocate threads. */
cmd->flavours = bfd_alloc2
(abfd, nflavours, sizeof (bfd_mach_o_thread_flavour));
if (cmd->flavours == NULL)
return FALSE;
cmd->nflavours = nflavours;
offset = 8;
nflavours = 0;
while (offset != command->len)
{
struct mach_o_thread_command_external raw;
if (offset >= command->len)
return FALSE;
if (nflavours >= cmd->nflavours)
return FALSE;
if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
offset += cmd->flavours[nflavours].size + sizeof (raw);
nflavours++;
}
for (i = 0; i < nflavours; i++)
{
asection *bfdsec;
unsigned int snamelen;
char *sname;
const char *flavourstr;
const char *prefix = "LC_THREAD";
unsigned int j = 0;
switch (mdata->header.cputype)
{
case BFD_MACH_O_CPU_TYPE_POWERPC:
case BFD_MACH_O_CPU_TYPE_POWERPC_64:
flavourstr =
bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
break;
case BFD_MACH_O_CPU_TYPE_I386:
case BFD_MACH_O_CPU_TYPE_X86_64:
flavourstr =
bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
break;
default:
flavourstr = "UNKNOWN_ARCHITECTURE";
break;
}
snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
sname = bfd_alloc (abfd, snamelen);
if (sname == NULL)
return FALSE;
for (;;)
{
sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
if (bfd_get_section_by_name (abfd, sname) == NULL)
break;
j++;
}
bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
bfdsec->vma = 0;
bfdsec->lma = 0;
bfdsec->size = cmd->flavours[i].size;
bfdsec->filepos = cmd->flavours[i].offset;
bfdsec->alignment_power = 0x0;
cmd->section = bfdsec;
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
{
struct mach_o_dysymtab_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
}
if (cmd->nmodtab != 0)
{
unsigned int i;
int wide = bfd_mach_o_wide_p (abfd);
unsigned int module_len = wide ? 56 : 52;
cmd->dylib_module =
bfd_alloc2 (abfd, cmd->nmodtab, sizeof (bfd_mach_o_dylib_module));
if (cmd->dylib_module == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nmodtab; i++)
{
bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
unsigned long v;
unsigned char buf[56];
if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
return FALSE;
module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
module->irefsym = bfd_h_get_32 (abfd, buf + 12);
module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
module->iextrel = bfd_h_get_32 (abfd, buf + 28);
module->nextrel = bfd_h_get_32 (abfd, buf + 32);
v = bfd_h_get_32 (abfd, buf +36);
module->iinit = v & 0xffff;
module->iterm = (v >> 16) & 0xffff;
v = bfd_h_get_32 (abfd, buf + 40);
module->ninit = v & 0xffff;
module->nterm = (v >> 16) & 0xffff;
if (wide)
{
module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
}
else
{
module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
}
}
}
if (cmd->ntoc != 0)
{
unsigned long i;
cmd->dylib_toc = bfd_alloc2
(abfd, cmd->ntoc, sizeof (bfd_mach_o_dylib_table_of_content));
if (cmd->dylib_toc == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->ntoc; i++)
{
struct mach_o_dylib_table_of_contents_external raw;
bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
}
}
if (cmd->nindirectsyms != 0)
{
unsigned int i;
cmd->indirect_syms = bfd_alloc2
(abfd, cmd->nindirectsyms, sizeof (unsigned int));
if (cmd->indirect_syms == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nindirectsyms; i++)
{
unsigned char raw[4];
unsigned int *is = &cmd->indirect_syms[i];
if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
*is = bfd_h_get_32 (abfd, raw);
}
}
if (cmd->nextrefsyms != 0)
{
unsigned long v;
unsigned int i;
cmd->ext_refs = bfd_alloc2
(abfd, cmd->nextrefsyms, sizeof (bfd_mach_o_dylib_reference));
if (cmd->ext_refs == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
return FALSE;
for (i = 0; i < cmd->nextrefsyms; i++)
{
unsigned char raw[4];
bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
/* Fields isym and flags are written as bit-fields, thus we need
a specific processing for endianness. */
v = bfd_h_get_32 (abfd, raw);
if (bfd_big_endian (abfd))
{
ref->isym = (v >> 8) & 0xffffff;
ref->flags = v & 0xff;
}
else
{
ref->isym = v & 0xffffff;
ref->flags = (v >> 24) & 0xff;
}
}
}
if (mdata->dysymtab)
return FALSE;
mdata->dysymtab = cmd;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_symtab_command *symtab = &command->command.symtab;
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
struct mach_o_symtab_command_external raw;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
symtab->symbols = NULL;
symtab->strtab = NULL;
if (symtab->nsyms != 0)
abfd->flags |= HAS_SYMS;
if (mdata->symtab)
return FALSE;
mdata->symtab = symtab;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_uuid_command *cmd = &command->command.uuid;
BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (cmd->uuid, 16, abfd) != 16)
return FALSE;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
struct mach_o_linkedit_data_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
cmd->datasize = bfd_get_32 (abfd, raw.datasize);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_str_command *cmd = &command->command.str;
struct mach_o_str_command_external raw;
unsigned long off;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
off = bfd_get_32 (abfd, raw.str);
cmd->stroff = command->offset + off;
cmd->str_len = command->len - off;
cmd->str = bfd_alloc (abfd, cmd->str_len);
if (cmd->str == NULL)
return FALSE;
if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
|| bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
return FALSE;
return TRUE;
}
static unsigned char *
bfd_mach_o_alloc_and_read (bfd *abfd, unsigned int off, unsigned int size)
{
unsigned char *buf;
buf = bfd_alloc (abfd, size);
if (buf == NULL)
return NULL;
if (bfd_seek (abfd, off, SEEK_SET) != 0
|| bfd_bread (buf, size, abfd) != size)
return NULL;
return buf;
}
static bfd_boolean
bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd)
{
/* Read rebase content. */
if (cmd->rebase_content == NULL && cmd->rebase_size != 0)
{
cmd->rebase_content =
bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, cmd->rebase_size);
if (cmd->rebase_content == NULL)
return FALSE;
}
/* Read bind content. */
if (cmd->bind_content == NULL && cmd->bind_size != 0)
{
cmd->bind_content =
bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, cmd->bind_size);
if (cmd->bind_content == NULL)
return FALSE;
}
/* Read weak bind content. */
if (cmd->weak_bind_content == NULL && cmd->weak_bind_size != 0)
{
cmd->weak_bind_content = bfd_mach_o_alloc_and_read
(abfd, cmd->weak_bind_off, cmd->weak_bind_size);
if (cmd->weak_bind_content == NULL)
return FALSE;
}
/* Read lazy bind content. */
if (cmd->lazy_bind_content == NULL && cmd->lazy_bind_size != 0)
{
cmd->lazy_bind_content = bfd_mach_o_alloc_and_read
(abfd, cmd->lazy_bind_off, cmd->lazy_bind_size);
if (cmd->lazy_bind_content == NULL)
return FALSE;
}
/* Read export content. */
if (cmd->export_content == NULL && cmd->export_size != 0)
{
cmd->export_content = bfd_mach_o_alloc_and_read
(abfd, cmd->export_off, cmd->export_size);
if (cmd->export_content == NULL)
return FALSE;
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
struct mach_o_dyld_info_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
cmd->rebase_content = NULL;
cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
cmd->bind_content = NULL;
cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
cmd->weak_bind_content = NULL;
cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
cmd->lazy_bind_content = NULL;
cmd->export_off = bfd_get_32 (abfd, raw.export_off);
cmd->export_size = bfd_get_32 (abfd, raw.export_size);
cmd->export_content = NULL;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_version_min_command *cmd = &command->command.version_min;
struct mach_o_version_min_command_external raw;
unsigned int ver;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
ver = bfd_get_32 (abfd, raw.version);
cmd->rel = ver >> 16;
cmd->maj = ver >> 8;
cmd->min = ver;
cmd->reserved = bfd_get_32 (abfd, raw.reserved);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
struct mach_o_encryption_info_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_main (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_main_command *cmd = &command->command.main;
struct mach_o_entry_point_command_external raw;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
cmd->entryoff = bfd_get_64 (abfd, raw.entryoff);
cmd->stacksize = bfd_get_64 (abfd, raw.stacksize);
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command)
{
bfd_mach_o_source_version_command *cmd = &command->command.source_version;
struct mach_o_source_version_command_external raw;
bfd_uint64_t ver;
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
ver = bfd_get_64 (abfd, raw.version);
/* Note: we use a serie of shift to avoid shift > 32 (for which gcc
generates warnings) in case of the host doesn't support 64 bit
integers. */
cmd->e = ver & 0x3ff;
ver >>= 10;
cmd->d = ver & 0x3ff;
ver >>= 10;
cmd->c = ver & 0x3ff;
ver >>= 10;
cmd->b = ver & 0x3ff;
ver >>= 10;
cmd->a = ver & 0xffffff;
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_segment (bfd *abfd,
bfd_mach_o_load_command *command,
unsigned int wide)
{
bfd_mach_o_segment_command *seg = &command->command.segment;
unsigned long i;
if (wide)
{
struct mach_o_segment_command_64_external raw;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
memcpy (seg->segname, raw.segname, 16);
seg->segname[16] = '\0';
seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
seg->flags = bfd_h_get_32 (abfd, raw.flags);
}
else
{
struct mach_o_segment_command_32_external raw;
BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
|| bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
return FALSE;
memcpy (seg->segname, raw.segname, 16);
seg->segname[16] = '\0';
seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
seg->flags = bfd_h_get_32 (abfd, raw.flags);
}
seg->sect_head = NULL;
seg->sect_tail = NULL;
for (i = 0; i < seg->nsects; i++)
{
bfd_vma segoff;
asection *sec;
if (wide)
segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
+ (i * BFD_MACH_O_SECTION_64_SIZE);
else
segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
+ (i * BFD_MACH_O_SECTION_SIZE);
sec = bfd_mach_o_read_section (abfd, segoff, seg->initprot, wide);
if (sec == NULL)
return FALSE;
bfd_mach_o_append_section_to_segment
(seg, bfd_mach_o_get_mach_o_section (sec));
}
return TRUE;
}
static bfd_boolean
bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
{
return bfd_mach_o_read_segment (abfd, command, 0);
}
static bfd_boolean
bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
{
return bfd_mach_o_read_segment (abfd, command, 1);
}
static bfd_boolean
bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
{
struct mach_o_load_command_external raw;
unsigned int cmd;
/* Read command type and length. */
if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
|| bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
return FALSE;
cmd = bfd_h_get_32 (abfd, raw.cmd);
command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
command->type_required = cmd & BFD_MACH_O_LC_REQ_DYLD ? TRUE : FALSE;
command->len = bfd_h_get_32 (abfd, raw.cmdsize);
switch (command->type)
{
case BFD_MACH_O_LC_SEGMENT:
if (!bfd_mach_o_read_segment_32 (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_SEGMENT_64:
if (!bfd_mach_o_read_segment_64 (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_SYMTAB:
if (!bfd_mach_o_read_symtab (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_SYMSEG:
break;
case BFD_MACH_O_LC_THREAD:
case BFD_MACH_O_LC_UNIXTHREAD:
if (!bfd_mach_o_read_thread (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_LOAD_DYLINKER:
case BFD_MACH_O_LC_ID_DYLINKER:
case BFD_MACH_O_LC_DYLD_ENVIRONMENT:
if (!bfd_mach_o_read_dylinker (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_LOAD_DYLIB:
case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
case BFD_MACH_O_LC_ID_DYLIB:
case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
case BFD_MACH_O_LC_REEXPORT_DYLIB:
case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
if (!bfd_mach_o_read_dylib (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_PREBOUND_DYLIB:
if (!bfd_mach_o_read_prebound_dylib (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_LOADFVMLIB:
case BFD_MACH_O_LC_IDFVMLIB:
if (!bfd_mach_o_read_fvmlib (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_IDENT:
case BFD_MACH_O_LC_FVMFILE:
case BFD_MACH_O_LC_PREPAGE:
case BFD_MACH_O_LC_ROUTINES:
case BFD_MACH_O_LC_ROUTINES_64:
break;
case BFD_MACH_O_LC_SUB_FRAMEWORK:
case BFD_MACH_O_LC_SUB_UMBRELLA:
case BFD_MACH_O_LC_SUB_LIBRARY:
case BFD_MACH_O_LC_SUB_CLIENT:
case BFD_MACH_O_LC_RPATH:
if (!bfd_mach_o_read_str (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_DYSYMTAB:
if (!bfd_mach_o_read_dysymtab (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_PREBIND_CKSUM:
if (!bfd_mach_o_read_prebind_cksum (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_TWOLEVEL_HINTS:
if (!bfd_mach_o_read_twolevel_hints (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_UUID:
if (!bfd_mach_o_read_uuid (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_CODE_SIGNATURE:
case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
case BFD_MACH_O_LC_FUNCTION_STARTS:
case BFD_MACH_O_LC_DATA_IN_CODE:
case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS:
if (!bfd_mach_o_read_linkedit (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_ENCRYPTION_INFO:
if (!bfd_mach_o_read_encryption_info (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_DYLD_INFO:
if (!bfd_mach_o_read_dyld_info (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
if (!bfd_mach_o_read_version_min (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_MAIN:
if (!bfd_mach_o_read_main (abfd, command))
return FALSE;
break;
case BFD_MACH_O_LC_SOURCE_VERSION:
if (!bfd_mach_o_read_source_version (abfd, command))
return FALSE;
break;
default:
command->len = 0;
(*_bfd_error_handler)(_("%B: unknown load command 0x%lx"),
abfd, (unsigned long) command->type);
return FALSE;
}
return TRUE;
}
static void
bfd_mach_o_flatten_sections (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_load_command *cmd;
long csect = 0;
/* Count total number of sections. */
mdata->nsects = 0;
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
if (cmd->type == BFD_MACH_O_LC_SEGMENT
|| cmd->type == BFD_MACH_O_LC_SEGMENT_64)
{
bfd_mach_o_segment_command *seg = &cmd->command.segment;
mdata->nsects += seg->nsects;
}
}
/* Allocate sections array. */
mdata->sections = bfd_alloc2 (abfd,
mdata->nsects, sizeof (bfd_mach_o_section *));
/* Fill the array. */
csect = 0;
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
if (cmd->type == BFD_MACH_O_LC_SEGMENT
|| cmd->type == BFD_MACH_O_LC_SEGMENT_64)
{
bfd_mach_o_segment_command *seg = &cmd->command.segment;
bfd_mach_o_section *sec;
BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
for (sec = seg->sect_head; sec != NULL; sec = sec->next)
mdata->sections[csect++] = sec;
}
}
}
static bfd_boolean
bfd_mach_o_scan_start_address (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_thread_command *thr = NULL;
bfd_mach_o_load_command *cmd;
unsigned long i;
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
if (cmd->type == BFD_MACH_O_LC_THREAD
|| cmd->type == BFD_MACH_O_LC_UNIXTHREAD)
{
thr = &cmd->command.thread;
break;
}
else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1)
{
bfd_mach_o_main_command *main_cmd = &cmd->command.main;
bfd_mach_o_section *text_sect = mdata->sections[0];
if (text_sect)
{
abfd->start_address = main_cmd->entryoff
+ (text_sect->addr - text_sect->offset);
return TRUE;
}
}
/* An object file has no start address, so do not fail if not found. */
if (thr == NULL)
return TRUE;
/* FIXME: create a subtarget hook ? */
for (i = 0; i < thr->nflavours; i++)
{
if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
&& (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE32))
{
unsigned char buf[4];
if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0
|| bfd_bread (buf, 4, abfd) != 4)
return FALSE;
abfd->start_address = bfd_h_get_32 (abfd, buf);
}
else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
&& (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
{
unsigned char buf[4];
if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
|| bfd_bread (buf, 4, abfd) != 4)
return FALSE;
abfd->start_address = bfd_h_get_32 (abfd, buf);
}
else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
&& (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
{
unsigned char buf[8];
if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
|| bfd_bread (buf, 8, abfd) != 8)
return FALSE;
abfd->start_address = bfd_h_get_64 (abfd, buf);
}
else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
&& (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
{
unsigned char buf[8];
if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0
|| bfd_bread (buf, 8, abfd) != 8)
return FALSE;
abfd->start_address = bfd_h_get_64 (abfd, buf);
}
}
return TRUE;
}
bfd_boolean
bfd_mach_o_set_arch_mach (bfd *abfd,
enum bfd_architecture arch,
unsigned long machine)
{
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
/* If this isn't the right architecture for this backend, and this
isn't the generic backend, fail. */
if (arch != bed->arch
&& arch != bfd_arch_unknown
&& bed->arch != bfd_arch_unknown)
return FALSE;
return bfd_default_set_arch_mach (abfd, arch, machine);
}
static bfd_boolean
bfd_mach_o_scan (bfd *abfd,
bfd_mach_o_header *header,
bfd_mach_o_data_struct *mdata)
{
unsigned int i;
enum bfd_architecture cputype;
unsigned long cpusubtype;
unsigned int hdrsize;
hdrsize = mach_o_wide_p (header) ?
BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
mdata->header = *header;
abfd->flags = abfd->flags & BFD_IN_MEMORY;
switch (header->filetype)
{
case BFD_MACH_O_MH_OBJECT:
abfd->flags |= HAS_RELOC;
break;
case BFD_MACH_O_MH_EXECUTE:
abfd->flags |= EXEC_P;
break;
case BFD_MACH_O_MH_DYLIB:
case BFD_MACH_O_MH_BUNDLE:
abfd->flags |= DYNAMIC;
break;
}
abfd->tdata.mach_o_data = mdata;
bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
&cputype, &cpusubtype);
if (cputype == bfd_arch_unknown)
{
(*_bfd_error_handler)
(_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
header->cputype, header->cpusubtype);
return FALSE;
}
bfd_set_arch_mach (abfd, cputype, cpusubtype);
if (header->ncmds != 0)
{
bfd_mach_o_load_command *cmd;
mdata->first_command = NULL;
mdata->last_command = NULL;
cmd = bfd_alloc2 (abfd, header->ncmds, sizeof (bfd_mach_o_load_command));
if (cmd == NULL)
return FALSE;
for (i = 0; i < header->ncmds; i++)
{
bfd_mach_o_load_command *cur = &cmd[i];
bfd_mach_o_append_command (abfd, cur);
if (i == 0)
cur->offset = hdrsize;
else
{
bfd_mach_o_load_command *prev = &cmd[i - 1];
cur->offset = prev->offset + prev->len;
}
if (!bfd_mach_o_read_command (abfd, cur))
return FALSE;
}
}
/* Sections should be flatten before scanning start address. */
bfd_mach_o_flatten_sections (abfd);
if (!bfd_mach_o_scan_start_address (abfd))
return FALSE;
return TRUE;
}
bfd_boolean
bfd_mach_o_mkobject_init (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = NULL;
mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
if (mdata == NULL)
return FALSE;
abfd->tdata.mach_o_data = mdata;
mdata->header.magic = 0;
mdata->header.cputype = 0;
mdata->header.cpusubtype = 0;
mdata->header.filetype = 0;
mdata->header.ncmds = 0;
mdata->header.sizeofcmds = 0;
mdata->header.flags = 0;
mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
mdata->first_command = NULL;
mdata->last_command = NULL;
mdata->nsects = 0;
mdata->sections = NULL;
mdata->dyn_reloc_cache = NULL;
return TRUE;
}
static bfd_boolean
bfd_mach_o_gen_mkobject (bfd *abfd)
{
bfd_mach_o_data_struct *mdata;
if (!bfd_mach_o_mkobject_init (abfd))
return FALSE;
mdata = bfd_mach_o_get_data (abfd);
mdata->header.magic = BFD_MACH_O_MH_MAGIC;
mdata->header.cputype = 0;
mdata->header.cpusubtype = 0;
mdata->header.byteorder = abfd->xvec->byteorder;
mdata->header.version = 1;
return TRUE;
}
const bfd_target *
bfd_mach_o_header_p (bfd *abfd,
bfd_mach_o_filetype filetype,
bfd_mach_o_cpu_type cputype)
{
bfd_mach_o_header header;
bfd_mach_o_data_struct *mdata;
if (!bfd_mach_o_read_header (abfd, &header))
goto wrong;
if (! (header.byteorder == BFD_ENDIAN_BIG
|| header.byteorder == BFD_ENDIAN_LITTLE))
{
(*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
(unsigned long) header.byteorder);
goto wrong;
}
if (! ((header.byteorder == BFD_ENDIAN_BIG
&& abfd->xvec->byteorder == BFD_ENDIAN_BIG
&& abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
|| (header.byteorder == BFD_ENDIAN_LITTLE
&& abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
&& abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
goto wrong;
/* Check cputype and filetype.
In case of wildcard, do not accept magics that are handled by existing
targets. */
if (cputype)
{
if (header.cputype != cputype)
goto wrong;
}
else
{
#ifndef BFD64
/* Do not recognize 64 architectures if not configured for 64bit targets.
This could happen only for generic targets. */
if (mach_o_wide_p (&header))
goto wrong;
#endif
}
if (filetype)
{
if (header.filetype != filetype)
goto wrong;
}
else
{
switch (header.filetype)
{
case BFD_MACH_O_MH_CORE:
/* Handled by core_p */
goto wrong;
default:
break;
}
}
mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
if (mdata == NULL)
goto fail;
if (!bfd_mach_o_scan (abfd, &header, mdata))
goto wrong;
return abfd->xvec;
wrong:
bfd_set_error (bfd_error_wrong_format);
fail:
return NULL;
}
static const bfd_target *
bfd_mach_o_gen_object_p (bfd *abfd)
{
return bfd_mach_o_header_p (abfd, 0, 0);
}
static const bfd_target *
bfd_mach_o_gen_core_p (bfd *abfd)
{
return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
}
/* Return the base address of ABFD, ie the address at which the image is
mapped. The possible initial pagezero is ignored. */
bfd_vma
bfd_mach_o_get_base_address (bfd *abfd)
{
bfd_mach_o_data_struct *mdata;
bfd_mach_o_load_command *cmd;
/* Check for Mach-O. */
if (!bfd_mach_o_valid (abfd))
return 0;
mdata = bfd_mach_o_get_data (abfd);
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
if ((cmd->type == BFD_MACH_O_LC_SEGMENT
|| cmd->type == BFD_MACH_O_LC_SEGMENT_64))
{
struct bfd_mach_o_segment_command *segcmd = &cmd->command.segment;
if (segcmd->initprot != 0)
return segcmd->vmaddr;
}
}
return 0;
}
typedef struct mach_o_fat_archentry
{
unsigned long cputype;
unsigned long cpusubtype;
unsigned long offset;
unsigned long size;
unsigned long align;
} mach_o_fat_archentry;
typedef struct mach_o_fat_data_struct
{
unsigned long magic;
unsigned long nfat_arch;
mach_o_fat_archentry *archentries;
} mach_o_fat_data_struct;
const bfd_target *
bfd_mach_o_archive_p (bfd *abfd)
{
mach_o_fat_data_struct *adata = NULL;
struct mach_o_fat_header_external hdr;
unsigned long i;
if (bfd_seek (abfd, 0, SEEK_SET) != 0
|| bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
goto error;
adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
if (adata == NULL)
goto error;
adata->magic = bfd_getb32 (hdr.magic);
adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
if (adata->magic != 0xcafebabe)
goto error;
/* Avoid matching Java bytecode files, which have the same magic number.
In the Java bytecode file format this field contains the JVM version,
which starts at 43.0. */
if (adata->nfat_arch > 30)
goto error;
adata->archentries =
bfd_alloc2 (abfd, adata->nfat_arch, sizeof (mach_o_fat_archentry));
if (adata->archentries == NULL)
goto error;
for (i = 0; i < adata->nfat_arch; i++)
{
struct mach_o_fat_arch_external arch;
if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
goto error;
adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
adata->archentries[i].offset = bfd_getb32 (arch.offset);
adata->archentries[i].size = bfd_getb32 (arch.size);
adata->archentries[i].align = bfd_getb32 (arch.align);
}
abfd->tdata.mach_o_fat_data = adata;
return abfd->xvec;
error:
if (adata != NULL)
bfd_release (abfd, adata);
bfd_set_error (bfd_error_wrong_format);
return NULL;
}
/* Set the filename for a fat binary member ABFD, whose bfd architecture is
ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
Set arelt_data and origin fields too. */
static void
bfd_mach_o_fat_member_init (bfd *abfd,
enum bfd_architecture arch_type,
unsigned long arch_subtype,
mach_o_fat_archentry *entry)
{
struct areltdata *areltdata;
/* Create the member filename. Use ARCH_NAME. */
const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
if (ap)
{
/* Use the architecture name if known. */
abfd->filename = xstrdup (ap->printable_name);
}
else
{
/* Forge a uniq id. */
const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1;
char *name = xmalloc (namelen);
snprintf (name, namelen, "0x%lx-0x%lx",
entry->cputype, entry->cpusubtype);
abfd->filename = name;
}
areltdata = bfd_zmalloc (sizeof (struct areltdata));
areltdata->parsed_size = entry->size;
abfd->arelt_data = areltdata;
abfd->iostream = NULL;
abfd->origin = entry->offset;
}
bfd *
bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
{
mach_o_fat_data_struct *adata;
mach_o_fat_archentry *entry = NULL;
unsigned long i;
bfd *nbfd;
enum bfd_architecture arch_type;
unsigned long arch_subtype;
adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
BFD_ASSERT (adata != NULL);
/* Find index of previous entry. */
if (prev == NULL)
{
/* Start at first one. */
i = 0;
}
else
{
/* Find index of PREV. */
for (i = 0; i < adata->nfat_arch; i++)
{
if (adata->archentries[i].offset == prev->origin)
break;
}
if (i == adata->nfat_arch)
{
/* Not found. */
bfd_set_error (bfd_error_bad_value);
return NULL;
}
/* Get next entry. */
i++;
}
if (i >= adata->nfat_arch)
{
bfd_set_error (bfd_error_no_more_archived_files);
return NULL;
}
entry = &adata->archentries[i];
nbfd = _bfd_new_bfd_contained_in (archive);
if (nbfd == NULL)
return NULL;
bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
&arch_type, &arch_subtype);
bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry);
bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
return nbfd;
}
/* Analogous to stat call. */
static int
bfd_mach_o_fat_stat_arch_elt (bfd *abfd, struct stat *buf)
{
if (abfd->arelt_data == NULL)
{
bfd_set_error (bfd_error_invalid_operation);
return -1;
}
buf->st_mtime = 0;
buf->st_uid = 0;
buf->st_gid = 0;
buf->st_mode = 0644;
buf->st_size = arelt_size (abfd);
return 0;
}
/* If ABFD format is FORMAT and architecture is ARCH, return it.
If ABFD is a fat image containing a member that corresponds to FORMAT
and ARCH, returns it.
In other case, returns NULL.
This function allows transparent uses of fat images. */
bfd *
bfd_mach_o_fat_extract (bfd *abfd,
bfd_format format,
const bfd_arch_info_type *arch)
{
bfd *res;
mach_o_fat_data_struct *adata;
unsigned int i;
if (bfd_check_format (abfd, format))
{
if (bfd_get_arch_info (abfd) == arch)
return abfd;
return NULL;
}
if (!bfd_check_format (abfd, bfd_archive)
|| abfd->xvec != &mach_o_fat_vec)
return NULL;
/* This is a Mach-O fat image. */
adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
BFD_ASSERT (adata != NULL);
for (i = 0; i < adata->nfat_arch; i++)
{
struct mach_o_fat_archentry *e = &adata->archentries[i];
enum bfd_architecture cpu_type;
unsigned long cpu_subtype;
bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
&cpu_type, &cpu_subtype);
if (cpu_type != arch->arch || cpu_subtype != arch->mach)
continue;
/* The architecture is found. */
res = _bfd_new_bfd_contained_in (abfd);
if (res == NULL)
return NULL;
bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e);
if (bfd_check_format (res, format))
{
BFD_ASSERT (bfd_get_arch_info (res) == arch);
return res;
}
bfd_close (res);
return NULL;
}
return NULL;
}
int
bfd_mach_o_lookup_command (bfd *abfd,
bfd_mach_o_load_command_type type,
bfd_mach_o_load_command **mcommand)
{
struct mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
struct bfd_mach_o_load_command *cmd;
unsigned int num;
BFD_ASSERT (mdata != NULL);
BFD_ASSERT (mcommand != NULL);
num = 0;
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
if (cmd->type != type)
continue;
if (num == 0)
*mcommand = cmd;
num++;
}
return num;
}
unsigned long
bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
{
switch (type)
{
case BFD_MACH_O_CPU_TYPE_MC680x0:
return 0x04000000;
case BFD_MACH_O_CPU_TYPE_MC88000:
return 0xffffe000;
case BFD_MACH_O_CPU_TYPE_POWERPC:
return 0xc0000000;
case BFD_MACH_O_CPU_TYPE_I386:
return 0xc0000000;
case BFD_MACH_O_CPU_TYPE_SPARC:
return 0xf0000000;
case BFD_MACH_O_CPU_TYPE_I860:
return 0;
case BFD_MACH_O_CPU_TYPE_HPPA:
return 0xc0000000 - 0x04000000;
default:
return 0;
}
}
/* The following two tables should be kept, as far as possible, in order of
most frequently used entries to optimize their use from gas. */
const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
{
{ "regular", BFD_MACH_O_S_REGULAR},
{ "coalesced", BFD_MACH_O_S_COALESCED},
{ "zerofill", BFD_MACH_O_S_ZEROFILL},
{ "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
{ "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
{ "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
{ "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
{ "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
{ "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
{ "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
{ "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
{ "interposing", BFD_MACH_O_S_INTERPOSING},
{ "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
{ "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
{ "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
{ "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
{ "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
{ NULL, 0}
};
const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
{
{ "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
{ "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
{ "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
{ "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
{ "debug", BFD_MACH_O_S_ATTR_DEBUG },
{ "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
{ "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
{ "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
{ "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
{ "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
{ "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
{ NULL, 0}
};
/* Get the section type from NAME. Return 256 if NAME is unknown. */
unsigned int
bfd_mach_o_get_section_type_from_name (bfd *abfd, const char *name)
{
const bfd_mach_o_xlat_name *x;
bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
for (x = bfd_mach_o_section_type_name; x->name; x++)
if (strcmp (x->name, name) == 0)
{
/* We found it... does the target support it? */
if (bed->bfd_mach_o_section_type_valid_for_target == NULL
|| bed->bfd_mach_o_section_type_valid_for_target (x->val))
return x->val; /* OK. */
else
break; /* Not supported. */
}
/* Maximum section ID = 0xff. */
return 256;
}
/* Get the section attribute from NAME. Return -1 if NAME is unknown. */
unsigned int
bfd_mach_o_get_section_attribute_from_name (const char *name)
{
const bfd_mach_o_xlat_name *x;
for (x = bfd_mach_o_section_attribute_name; x->name; x++)
if (strcmp (x->name, name) == 0)
return x->val;
return (unsigned int)-1;
}
int
bfd_mach_o_core_fetch_environment (bfd *abfd,
unsigned char **rbuf,
unsigned int *rlen)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
bfd_mach_o_load_command *cmd;
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
bfd_mach_o_segment_command *seg;
if (cmd->type != BFD_MACH_O_LC_SEGMENT)
continue;
seg = &cmd->command.segment;
if ((seg->vmaddr + seg->vmsize) == stackaddr)
{
unsigned long start = seg->fileoff;
unsigned long end = seg->fileoff + seg->filesize;
unsigned char *buf = bfd_malloc (1024);
unsigned long size = 1024;
for (;;)
{
bfd_size_type nread = 0;
unsigned long offset;
int found_nonnull = 0;
if (size > (end - start))
size = (end - start);
buf = bfd_realloc_or_free (buf, size);
if (buf == NULL)
return -1;
if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
{
free (buf);
return -1;
}
nread = bfd_bread (buf, size, abfd);
if (nread != size)
{
free (buf);
return -1;
}
for (offset = 4; offset <= size; offset += 4)
{
unsigned long val;
val = *((unsigned long *) (buf + size - offset));
if (! found_nonnull)
{
if (val != 0)
found_nonnull = 1;
}
else if (val == 0x0)
{
unsigned long bottom;
unsigned long top;
bottom = seg->fileoff + seg->filesize - offset;
top = seg->fileoff + seg->filesize - 4;
*rbuf = bfd_malloc (top - bottom);
*rlen = top - bottom;
memcpy (*rbuf, buf + size - *rlen, *rlen);
free (buf);
return 0;
}
}
if (size == (end - start))
break;
size *= 2;
}
free (buf);
}
}
return -1;
}
char *
bfd_mach_o_core_file_failing_command (bfd *abfd)
{
unsigned char *buf = NULL;
unsigned int len = 0;
int ret;
ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
if (ret < 0)
return NULL;
return (char *) buf;
}
int
bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
{
return 0;
}
static bfd_mach_o_uuid_command *
bfd_mach_o_lookup_uuid_command (bfd *abfd)
{
bfd_mach_o_load_command *uuid_cmd;
int ncmd = bfd_mach_o_lookup_command (abfd, BFD_MACH_O_LC_UUID, &uuid_cmd);
if (ncmd != 1)
return FALSE;
return &uuid_cmd->command.uuid;
}
/* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */
static bfd_boolean
bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd)
{
bfd_mach_o_uuid_command *dsym_uuid_cmd;
BFD_ASSERT (abfd);
BFD_ASSERT (uuid_cmd);
if (!bfd_check_format (abfd, bfd_object))
return FALSE;
if (bfd_get_flavour (abfd) != bfd_target_mach_o_flavour
|| bfd_mach_o_get_data (abfd) == NULL
|| bfd_mach_o_get_data (abfd)->header.filetype != BFD_MACH_O_MH_DSYM)
return FALSE;
dsym_uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
if (dsym_uuid_cmd == NULL)
return FALSE;
if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid,
sizeof (uuid_cmd->uuid)) != 0)
return FALSE;
return TRUE;
}
/* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
The caller is responsible for closing the returned BFD object and
its my_archive if the returned BFD is in a fat dSYM. */
static bfd *
bfd_mach_o_find_dsym (const char *dsym_filename,
const bfd_mach_o_uuid_command *uuid_cmd,
const bfd_arch_info_type *arch)
{
bfd *base_dsym_bfd, *dsym_bfd;
BFD_ASSERT (uuid_cmd);
base_dsym_bfd = bfd_openr (dsym_filename, NULL);
if (base_dsym_bfd == NULL)
return NULL;
dsym_bfd = bfd_mach_o_fat_extract (base_dsym_bfd, bfd_object, arch);
if (bfd_mach_o_dsym_for_uuid_p (dsym_bfd, uuid_cmd))
return dsym_bfd;
bfd_close (dsym_bfd);
if (base_dsym_bfd != dsym_bfd)
bfd_close (base_dsym_bfd);
return NULL;
}
/* Return a BFD created from a dSYM file for ABFD.
The caller is responsible for closing the returned BFD object, its
filename, and its my_archive if the returned BFD is in a fat dSYM. */
static bfd *
bfd_mach_o_follow_dsym (bfd *abfd)
{
char *dsym_filename;
bfd_mach_o_uuid_command *uuid_cmd;
bfd *dsym_bfd, *base_bfd = abfd;
const char *base_basename;
if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_mach_o_flavour)
return NULL;
if (abfd->my_archive)
base_bfd = abfd->my_archive;
/* BFD may have been opened from a stream. */
if (base_bfd->filename == NULL)
{
bfd_set_error (bfd_error_invalid_operation);
return NULL;
}
base_basename = lbasename (base_bfd->filename);
uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
if (uuid_cmd == NULL)
return NULL;
/* TODO: We assume the DWARF file has the same as the binary's.
It seems apple's GDB checks all files in the dSYM bundle directory.
http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c
*/
dsym_filename = (char *)bfd_malloc (strlen (base_bfd->filename)
+ strlen (dsym_subdir) + 1
+ strlen (base_basename) + 1);
sprintf (dsym_filename, "%s%s/%s",
base_bfd->filename, dsym_subdir, base_basename);
dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd,
bfd_get_arch_info (abfd));
if (dsym_bfd == NULL)
free (dsym_filename);
return dsym_bfd;
}
bfd_boolean
bfd_mach_o_find_nearest_line (bfd *abfd,
asymbol **symbols,
asection *section,
bfd_vma offset,
const char **filename_ptr,
const char **functionname_ptr,
unsigned int *line_ptr,
unsigned int *discriminator_ptr)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (mdata == NULL)
return FALSE;
switch (mdata->header.filetype)
{
case BFD_MACH_O_MH_OBJECT:
break;
case BFD_MACH_O_MH_EXECUTE:
case BFD_MACH_O_MH_DYLIB:
case BFD_MACH_O_MH_BUNDLE:
case BFD_MACH_O_MH_KEXT_BUNDLE:
if (mdata->dwarf2_find_line_info == NULL)
{
mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd);
/* When we couldn't find dSYM for this binary, we look for
the debug information in the binary itself. In this way,
we won't try finding separated dSYM again because
mdata->dwarf2_find_line_info will be filled. */
if (! mdata->dsym_bfd)
break;
if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd,
dwarf_debug_sections, symbols,
&mdata->dwarf2_find_line_info,
FALSE))
return FALSE;
}
break;
default:
return FALSE;
}
return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
filename_ptr, functionname_ptr,
line_ptr, discriminator_ptr,
dwarf_debug_sections, 0,
&mdata->dwarf2_find_line_info);
}
bfd_boolean
bfd_mach_o_close_and_cleanup (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
{
_bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
bfd_mach_o_free_cached_info (abfd);
if (mdata->dsym_bfd != NULL)
{
bfd *fat_bfd = mdata->dsym_bfd->my_archive;
char *dsym_filename = (char *)(fat_bfd
? fat_bfd->filename
: mdata->dsym_bfd->filename);
bfd_close (mdata->dsym_bfd);
mdata->dsym_bfd = NULL;
if (fat_bfd)
bfd_close (fat_bfd);
free (dsym_filename);
}
}
if (bfd_get_format (abfd) == bfd_archive
&& abfd->xvec == &mach_o_fat_vec)
return TRUE;
return _bfd_generic_close_and_cleanup (abfd);
}
bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
asection *asect;
free (mdata->dyn_reloc_cache);
mdata->dyn_reloc_cache = NULL;
for (asect = abfd->sections; asect != NULL; asect = asect->next)
{
free (asect->relocation);
asect->relocation = NULL;
}
return TRUE;
}
#define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
#define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
#define bfd_mach_o_swap_reloc_in NULL
#define bfd_mach_o_swap_reloc_out NULL
#define bfd_mach_o_print_thread NULL
#define bfd_mach_o_tgt_seg_table NULL
#define bfd_mach_o_section_type_valid_for_tgt NULL
#define TARGET_NAME mach_o_be_vec
#define TARGET_STRING "mach-o-be"
#define TARGET_ARCHITECTURE bfd_arch_unknown
#define TARGET_PAGESIZE 1
#define TARGET_BIG_ENDIAN 1
#define TARGET_ARCHIVE 0
#define TARGET_PRIORITY 1
#include "mach-o-target.c"
#undef TARGET_NAME
#undef TARGET_STRING
#undef TARGET_ARCHITECTURE
#undef TARGET_PAGESIZE
#undef TARGET_BIG_ENDIAN
#undef TARGET_ARCHIVE
#undef TARGET_PRIORITY
#define TARGET_NAME mach_o_le_vec
#define TARGET_STRING "mach-o-le"
#define TARGET_ARCHITECTURE bfd_arch_unknown
#define TARGET_PAGESIZE 1
#define TARGET_BIG_ENDIAN 0
#define TARGET_ARCHIVE 0
#define TARGET_PRIORITY 1
#include "mach-o-target.c"
#undef TARGET_NAME
#undef TARGET_STRING
#undef TARGET_ARCHITECTURE
#undef TARGET_PAGESIZE
#undef TARGET_BIG_ENDIAN
#undef TARGET_ARCHIVE
#undef TARGET_PRIORITY
/* Not yet handled: creating an archive. */
#define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
/* Not used. */
#define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
#define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
#define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
#define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
#define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
#define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
#define bfd_mach_o_write_armap _bfd_noarchive_write_armap
#define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
#define bfd_mach_o_generic_stat_arch_elt bfd_mach_o_fat_stat_arch_elt
#define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
#define TARGET_NAME mach_o_fat_vec
#define TARGET_STRING "mach-o-fat"
#define TARGET_ARCHITECTURE bfd_arch_unknown
#define TARGET_PAGESIZE 1
#define TARGET_BIG_ENDIAN 1
#define TARGET_ARCHIVE 1
#define TARGET_PRIORITY 0
#include "mach-o-target.c"
#undef TARGET_NAME
#undef TARGET_STRING
#undef TARGET_ARCHITECTURE
#undef TARGET_PAGESIZE
#undef TARGET_BIG_ENDIAN
#undef TARGET_ARCHIVE
#undef TARGET_PRIORITY
|