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
|
/* GNU Hurd standard exec server.
Copyright (C) 1992,93,94,95,96,98,99,2000,01,02,04
Free Software Foundation, Inc.
Written by Roland McGrath.
Can exec ELF format directly.
#ifdef GZIP
Can gunzip executables into core on the fly.
#endif
#ifdef BFD
Can exec any executable format the BFD library understands
to be for this flavor of machine.
#endif
#ifdef BZIP2
Can bunzip2 executables into core on the fly.
#endif
This file is part of the GNU Hurd.
The GNU Hurd 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 2, or (at your option)
any later version.
The GNU Hurd 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 the GNU Hurd; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "priv.h"
#include <hurd.h>
#include <hurd/exec.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdbool.h>
mach_port_t procserver; /* Our proc port. */
/* Standard exec data for secure execs. */
mach_port_t *std_ports;
int *std_ints;
size_t std_nports, std_nints;
pthread_rwlock_t std_lock = PTHREAD_RWLOCK_INITIALIZER;
#ifdef BFD
/* Return a Hurd error code corresponding to the most recent BFD error. */
static error_t
b2he (error_t deflt)
{
switch (bfd_get_error ())
{
case bfd_error_system_call:
return errno;
case bfd_error_no_memory:
return ENOMEM;
default:
return deflt;
}
}
#else
#define b2he() a2he (errno)
#endif
#ifdef GZIP
static void check_gzip (struct execdata *);
#endif
#ifdef BZIP2
static void check_bzip2 (struct execdata *);
#endif
#ifdef BFD
/* Check a section, updating the `locations' vector [BFD]. */
static void
check_section (bfd *bfd, asection *sec, void *userdata)
{
struct execdata *u = userdata;
static const union
{
char string[8];
unsigned int quadword __attribute__ ((mode (DI)));
} interp = { string: ".interp" };
if (u->error)
return;
/* Fast strcmp for this 8-byte constant string. */
if (*(const __typeof (interp.quadword) *) sec->name == interp.quadword)
u->interp.section = sec;
if (!(sec->flags & (SEC_ALLOC|SEC_LOAD)) ||
(sec->flags & SEC_NEVER_LOAD))
/* Nothing to do for this section. */
return;
if (sec->flags & SEC_LOAD)
{
u->info.bfd_locations[sec->index] = sec->filepos;
if ((off_t) sec->filepos < 0 || (off_t) sec->filepos > u->file_size)
u->error = ENOEXEC;
}
}
#endif
/* Zero the specified region but don't crash the server if it faults. */
#include <hurd/sigpreempt.h>
static error_t
safe_bzero (void *ptr, size_t size)
{
return hurd_safe_memset (ptr, 0, size);
}
/* Load or allocate a section. */
static void
load_section (void *section, struct execdata *u)
{
vm_address_t addr = 0;
vm_offset_t filepos = 0;
vm_size_t filesz = 0, memsz = 0;
vm_prot_t vm_prot;
int anywhere;
vm_address_t mask = 0;
#ifdef BFD
asection *const sec = section;
#endif
const ElfW(Phdr) *const ph = section;
if (u->error)
return;
#ifdef BFD
if (u->bfd && sec->flags & SEC_NEVER_LOAD)
/* Nothing to do for this section. */
return;
#endif
vm_prot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
#ifdef BFD
if (u->bfd)
{
addr = (vm_address_t) sec->vma;
filepos = u->info.bfd_locations[sec->index];
memsz = sec->_raw_size;
filesz = (sec->flags & SEC_LOAD) ? memsz : 0;
if (sec->flags & (SEC_READONLY|SEC_ROM))
vm_prot &= ~VM_PROT_WRITE;
anywhere = 0;
}
else
#endif
{
addr = ph->p_vaddr & ~(ph->p_align - 1);
memsz = ph->p_vaddr + ph->p_memsz - addr;
filepos = ph->p_offset & ~(ph->p_align - 1);
filesz = ph->p_offset + ph->p_filesz - filepos;
if ((ph->p_flags & PF_R) == 0)
vm_prot &= ~VM_PROT_READ;
if ((ph->p_flags & PF_W) == 0)
vm_prot &= ~VM_PROT_WRITE;
if ((ph->p_flags & PF_X) == 0)
vm_prot &= ~VM_PROT_EXECUTE;
anywhere = u->info.elf.anywhere;
if (! anywhere)
addr += u->info.elf.loadbase;
else
#if 0
switch (elf_machine)
{
case EM_386:
case EM_486:
/* On the i386, programs normally load at 0x08000000, and
expect their data segment to be able to grow dynamically
upward from its start near that address. We need to make
sure that the dynamic linker is not mapped in a conflicting
address. */
/* mask = 0xf8000000UL; */ /* XXX */
break;
default:
break;
}
#endif
if (anywhere && addr < vm_page_size)
addr = vm_page_size;
}
if (memsz == 0)
/* This section is empty; ignore it. */
return;
if (filesz != 0)
{
vm_address_t mapstart = round_page (addr);
/* Allocate space in the task and write CONTENTS into it. */
void write_to_task (vm_address_t mapstart, vm_size_t size,
vm_prot_t vm_prot, vm_address_t contents)
{
vm_size_t off = size % vm_page_size;
/* Allocate with vm_map to set max protections. */
u->error = vm_map (u->task,
&mapstart, size, mask, anywhere,
MACH_PORT_NULL, 0, 1,
vm_prot|VM_PROT_WRITE,
VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
VM_INHERIT_COPY);
if (! u->error && size >= vm_page_size)
u->error = vm_write (u->task, mapstart, contents, size - off);
if (! u->error && off != 0)
{
vm_address_t page = 0;
page = (vm_address_t) mmap (0, vm_page_size,
PROT_READ|PROT_WRITE, MAP_ANON,
0, 0);
u->error = (page == -1) ? errno : 0;
if (! u->error)
{
u->error = hurd_safe_copyin ((void *) page, /* XXX/fault */
(void *) (contents + (size - off)),
off);
if (! u->error)
u->error = vm_write (u->task, mapstart + (size - off),
page, vm_page_size);
munmap ((caddr_t) page, vm_page_size);
}
}
/* Reset the current protections to the desired state. */
if (! u->error && (vm_prot & VM_PROT_WRITE) == 0)
u->error = vm_protect (u->task, mapstart, size, 0, vm_prot);
}
if (mapstart - addr < filesz)
{
/* MAPSTART is the first page that starts inside the section.
Map all the pages that start inside the section. */
#define SECTION_IN_MEMORY_P (u->file_data != NULL)
#define SECTION_CONTENTS (u->file_data + filepos)
if (SECTION_IN_MEMORY_P)
/* Data is already in memory; write it into the task. */
write_to_task (mapstart, filesz - (mapstart - addr), vm_prot,
(vm_address_t) SECTION_CONTENTS
+ (mapstart - addr));
else if (u->filemap != MACH_PORT_NULL)
/* Map the data into the task directly from the file. */
u->error = vm_map (u->task,
&mapstart, filesz - (mapstart - addr),
mask, anywhere,
u->filemap, filepos + (mapstart - addr), 1,
vm_prot,
VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
VM_INHERIT_COPY);
else
{
/* Cannot map the data. Read it into a buffer and vm_write
it into the task. */
const vm_size_t size = filesz - (mapstart - addr);
void *buf = map (u, filepos + (mapstart - addr), size);
if (buf)
write_to_task (mapstart, size, vm_prot, (vm_address_t) buf);
}
if (u->error)
return;
if (anywhere)
{
/* We let the kernel choose the location of the mapping.
Now record where it ended up. Later sections cannot
be mapped anywhere, they must come after this one. */
u->info.elf.loadbase = mapstart;
addr = mapstart + (addr % vm_page_size);
anywhere = u->info.elf.anywhere = 0;
mask = 0;
}
}
if (mapstart > addr)
{
/* We must read and copy in the space in the section before the
first page boundary. */
vm_address_t overlap_page = trunc_page (addr);
vm_address_t ourpage = 0;
vm_size_t size = 0;
void *readaddr;
size_t readsize;
u->error = vm_read (u->task, overlap_page, vm_page_size,
&ourpage, &size);
if (u->error)
{
if (u->error == KERN_INVALID_ADDRESS)
{
/* The space is unallocated. */
u->error = vm_allocate (u->task,
&overlap_page, vm_page_size, 0);
size = vm_page_size;
if (!u->error)
{
ourpage = (vm_address_t) mmap (0, vm_page_size,
PROT_READ|PROT_WRITE,
MAP_ANON, 0, 0);
u->error = (ourpage == -1) ? errno : 0;
}
}
if (u->error)
{
maplose:
vm_deallocate (u->task, mapstart, filesz);
return;
}
}
readaddr = (void *) (ourpage + (addr - overlap_page));
readsize = size - (addr - overlap_page);
if (readsize > filesz)
readsize = filesz;
if (SECTION_IN_MEMORY_P)
memcpy (readaddr, SECTION_CONTENTS, readsize);
else
{
const void *contents = map (u, filepos, readsize);
if (!contents)
goto maplose;
u->error = hurd_safe_copyin (readaddr, contents,
readsize); /* XXX/fault */
if (u->error)
goto maplose;
}
u->error = vm_write (u->task, overlap_page, ourpage, size);
if (u->error == KERN_PROTECTION_FAILURE)
{
/* The overlap page is not writable; the section
that appears in preceding memory is read-only.
Change the page's protection so we can write it. */
u->error = vm_protect (u->task, overlap_page, size,
0, vm_prot | VM_PROT_WRITE);
if (!u->error)
u->error = vm_write (u->task, overlap_page, ourpage, size);
/* If this section is not supposed to be writable either,
restore the page's protection to read-only. */
if (!u->error && !(vm_prot & VM_PROT_WRITE))
u->error = vm_protect (u->task, overlap_page, size,
0, vm_prot);
}
munmap ((caddr_t) ourpage, size);
if (u->error)
goto maplose;
}
if (u->cntl)
u->cntl->accessed = 1;
/* Tell the code below to zero-fill the remaining area. */
addr += filesz;
memsz -= filesz;
}
if (memsz != 0)
{
/* SEC_ALLOC: Allocate zero-filled memory for the section. */
vm_address_t mapstart = round_page (addr);
if (mapstart - addr < memsz)
{
/* MAPSTART is the first page that starts inside the section.
Allocate all the pages that start inside the section. */
u->error = vm_map (u->task, &mapstart, memsz - (mapstart - addr),
mask, anywhere, MACH_PORT_NULL, 0, 1,
vm_prot, VM_PROT_ALL, VM_INHERIT_COPY);
if (u->error)
return;
}
if (anywhere)
{
/* We let the kernel choose the location of the zero space.
Now record where it ended up. Later sections cannot
be mapped anywhere, they must come after this one. */
u->info.elf.loadbase = mapstart;
addr = mapstart + (addr % vm_page_size);
anywhere = u->info.elf.anywhere = 0;
mask = 0;
}
if (mapstart > addr)
{
/* Zero space in the section before the first page boundary. */
vm_address_t overlap_page = trunc_page (addr);
vm_address_t ourpage = 0;
vm_size_t size = 0;
u->error = vm_read (u->task, overlap_page, vm_page_size,
&ourpage, &size);
if (u->error)
{
vm_deallocate (u->task, mapstart, memsz);
return;
}
u->error = safe_bzero ((void *) (ourpage + (addr - overlap_page)),
size - (addr - overlap_page));
if (! u->error && !(vm_prot & VM_PROT_WRITE))
u->error = vm_protect (u->task, overlap_page, size,
0, VM_PROT_WRITE);
if (! u->error)
u->error = vm_write (u->task, overlap_page, ourpage, size);
if (! u->error && !(vm_prot & VM_PROT_WRITE))
u->error = vm_protect (u->task, overlap_page, size, 0, vm_prot);
munmap ((caddr_t) ourpage, size);
}
}
}
/* XXX all accesses of the mapped data need to use fault handling
to abort the RPC when mapped file data generates bad page faults.
I've marked some accesses with XXX/fault comments.
--roland */
void *
map (struct execdata *e, off_t posn, size_t len)
{
const size_t size = e->file_size;
size_t offset;
if ((map_filepos (e) & ~(map_vsize (e) - 1)) == (posn & ~(map_vsize (e) - 1))
&& posn + len - map_filepos (e) <= map_fsize (e))
/* The current mapping window covers it. */
offset = posn & (map_vsize (e) - 1);
else if (posn + len > size)
/* The requested data wouldn't fit in the file. */
return NULL;
else if (e->file_data != NULL) {
return e->file_data + posn;
} else if (e->filemap == MACH_PORT_NULL)
{
/* No mapping for the file. Read the data by RPC. */
char *buffer = map_buffer (e);
mach_msg_type_number_t nread = map_vsize (e);
assert (e->file_data == NULL); /* Must be first or second case. */
/* Read as much as we can get into the buffer right now. */
e->error = io_read (e->file, &buffer, &nread, posn, round_page (len));
if (e->error)
return NULL;
if (buffer != map_buffer (e))
{
/* The data was returned out of line. Discard the old buffer. */
if (map_vsize (e) != 0)
munmap (map_buffer (e), map_vsize (e));
map_buffer (e) = buffer;
map_vsize (e) = round_page (nread);
}
map_filepos (e) = posn;
map_set_fsize (e, nread);
offset = 0;
}
else
{
/* Deallocate the old mapping area. */
if (map_buffer (e) != NULL)
munmap (map_buffer (e), map_vsize (e));
map_buffer (e) = NULL;
/* Make sure our mapping is page-aligned in the file. */
offset = posn & (vm_page_size - 1);
map_filepos (e) = trunc_page (posn);
map_vsize (e) = round_page (posn + len) - map_filepos (e);
/* Map the data from the file. */
if (vm_map (mach_task_self (),
(vm_address_t *) &map_buffer (e), map_vsize (e), 0, 1,
e->filemap, map_filepos (e), 1, VM_PROT_READ, VM_PROT_READ,
VM_INHERIT_NONE))
{
e->error = EIO;
return NULL;
}
if (e->cntl)
e->cntl->accessed = 1;
map_set_fsize (e, MIN (map_vsize (e), size - map_filepos (e)));
}
return map_buffer (e) + offset;
}
/* Initialize E's stdio stream. */
static void prepare_stream (struct execdata *e);
/* Point the stream at the buffer of file data in E->file_data. */
static void prepare_in_memory (struct execdata *e);
#ifndef EXECDATA_STREAM
/* We don't have a stdio stream, but we have a mapping window
we need to initialize. */
static void
prepare_stream (struct execdata *e)
{
e->map_buffer = NULL;
e->map_vsize = e->map_fsize = 0;
e->map_filepos = 0;
}
static void prepare_in_memory (struct execdata *e)
{
prepare_stream(e);
}
#else
#ifdef _STDIO_USES_IOSTREAM
# error implement me for libio!
#else /* old GNU stdio */
#if 0
void *
map (struct execdata *e, off_t posn, size_t len)
{
FILE *f = &e->stream;
const size_t size = e->file_size;
size_t offset;
if ((f->__offset & ~(f->__bufsize - 1)) == (posn & ~(f->__bufsize - 1)) &&
f->__buffer + (posn + len - f->__offset) < f->__get_limit)
/* The current mapping window covers it. */
offset = posn & (f->__bufsize - 1);
else if (e->file_data != NULL)
{
/* The current "mapping window" is in fact the whole file contents.
So if it's not in there, it's not in there. */
f->__eof = 1;
return NULL;
}
else if (e->filemap == MACH_PORT_NULL)
{
/* No mapping for the file. Read the data by RPC. */
char *buffer = f->__buffer;
mach_msg_type_number_t nread = f->__bufsize;
/* Read as much as we can get into the buffer right now. */
e->error = io_read (e->file, &buffer, &nread, posn, round_page (len));
if (e->error)
{
errno = e->error;
f->__error = 1;
return NULL;
}
if (buffer != f->__buffer)
{
/* The data was returned out of line. Discard the old buffer. */
if (f->__bufsize != 0)
munmap (f->__buffer, f->__bufsize);
f->__buffer = buffer;
f->__bufsize = round_page (nread);
}
f->__offset = posn;
f->__get_limit = f->__buffer + nread;
offset = 0;
}
else
{
/* Deallocate the old mapping area. */
if (f->__buffer != NULL)
munmap (f->__buffer, f->__bufsize);
f->__buffer = NULL;
/* Make sure our mapping is page-aligned in the file. */
offset = posn & (vm_page_size - 1);
f->__offset = trunc_page (posn);
f->__bufsize = round_page (posn + len) - f->__offset;
/* Map the data from the file. */
if (vm_map (mach_task_self (),
(vm_address_t *) &f->__buffer, f->__bufsize, 0, 1,
e->filemap, f->__offset, 1, VM_PROT_READ, VM_PROT_READ,
VM_INHERIT_NONE))
{
errno = e->error = EIO;
f->__error = 1;
return NULL;
}
if (e->cntl)
e->cntl->accessed = 1;
if (f->__offset + f->__bufsize > size)
f->__get_limit = f->__buffer + (size - f->__offset);
else
f->__get_limit = f->__buffer + f->__bufsize;
}
f->__target = f->__offset;
f->__bufp = f->__buffer + offset;
if (f->__bufp + len > f->__get_limit)
{
f->__eof = 1;
return NULL;
}
return f->__bufp;
}
#endif
/* stdio input-room function.
XXX/fault in the stdio case (or libio replacement), i.e. for bfd
(if ever revived), need to check all the mapping fault issues */
static int
input_room (FILE *f)
{
struct execdata *e = f->__cookie;
char *p = map (e, f->__target, 1);
if (p == NULL)
{
(e->error ? f->__error : f->__eof) = 1;
return EOF;
}
f->__target = f->__offset;
f->__bufp = p;
return (unsigned char) *f->__bufp++;
}
static int
close_exec_stream (void *cookie)
{
struct execdata *e = cookie;
if (e->stream.__buffer != NULL)
munmap (e->stream.__buffer, e->stream.__bufsize);
return 0;
}
/* stdio seek function. */
static int
fake_seek (void *cookie, fpos_t *pos, int whence)
{
struct execdata *e = cookie;
/* Set __target to match the specifed seek location */
switch (whence)
{
case SEEK_END:
e->stream.__target = e->file_size + *pos;
break;
case SEEK_CUR:
e->stream.__target += *pos;
break;
case SEEK_SET:
e->stream.__target = *pos;
break;
}
*pos = e->stream.__target;
return 0;
}
/* Initialize E's stdio stream. */
static void
prepare_stream (struct execdata *e)
{
memset (&e->stream, 0, sizeof (e->stream));
e->stream.__magic = _IOMAGIC;
e->stream.__mode.__read = 1;
e->stream.__userbuf = 1;
e->stream.__room_funcs.__input = input_room;
e->stream.__io_funcs.seek = fake_seek;
e->stream.__io_funcs.close = close_exec_stream;
e->stream.__cookie = e;
e->stream.__seen = 1;
}
/* Point the stream at the buffer of file data. */
static void
prepare_in_memory (struct execdata *e)
{
memset (&e->stream, 0, sizeof (e->stream));
e->stream.__magic = _IOMAGIC;
e->stream.__mode.__read = 1;
e->stream.__buffer = e->file_data;
e->stream.__bufsize = e->file_size;
e->stream.__get_limit = e->stream.__buffer + e->stream.__bufsize;
e->stream.__bufp = e->stream.__buffer;
e->stream.__seen = 1;
}
#endif
#endif
/* Prepare to check and load FILE. */
static void
prepare (file_t file, struct execdata *e)
{
memory_object_t rd, wr;
e->file = file;
#ifdef BFD
e->bfd = NULL;
#endif
e->file_data = NULL;
e->cntl = NULL;
e->filemap = MACH_PORT_NULL;
e->cntlmap = MACH_PORT_NULL;
e->interp.section = NULL;
/* Initialize E's stdio stream. */
prepare_stream (e);
/* Try to mmap FILE. */
e->error = io_map (file, &rd, &wr);
if (! e->error)
/* Mapping is O.K. */
{
if (wr != MACH_PORT_NULL)
mach_port_deallocate (mach_task_self (), wr);
if (rd == MACH_PORT_NULL)
{
e->error = EBADF; /* ? XXX */
return;
}
e->filemap = rd;
e->error = /* io_map_cntl (file, &e->cntlmap) */ EOPNOTSUPP; /* XXX */
if (!e->error)
e->error = vm_map (mach_task_self (), (vm_address_t *) &e->cntl,
vm_page_size, 0, 1, e->cntlmap, 0, 0,
VM_PROT_READ|VM_PROT_WRITE,
VM_PROT_READ|VM_PROT_WRITE, VM_INHERIT_NONE);
if (e->cntl)
while (1)
{
pthread_spin_lock (&e->cntl->lock);
switch (e->cntl->conch_status)
{
case USER_COULD_HAVE_CONCH:
e->cntl->conch_status = USER_HAS_CONCH;
case USER_HAS_CONCH:
pthread_spin_unlock (&e->cntl->lock);
/* Break out of the loop. */
break;
case USER_RELEASE_CONCH:
case USER_HAS_NOT_CONCH:
default: /* Oops. */
pthread_spin_unlock (&e->cntl->lock);
e->error = io_get_conch (e->file);
if (e->error)
return;
/* Continue the loop. */
continue;
}
/* Get here if we are now IT. */
e->file_size = 0;
if (e->cntl->use_file_size)
e->file_size = e->cntl->file_size;
if (e->cntl->use_read_size && e->cntl->read_size > e->file_size)
e->file_size = e->cntl->read_size;
break;
}
}
if (!e->cntl && (!e->error || e->error == EOPNOTSUPP))
{
/* No shared page. Do a stat to find the file size. */
struct stat st;
e->error = io_stat (file, &st);
if (e->error)
return;
e->file_size = st.st_size;
e->optimal_block = st.st_blksize;
}
}
/* Check the magic number, etc. of the file.
On successful return, the caller must allocate the
E->locations vector, and map check_section over the BFD. */
#ifdef BFD
static void
check_bfd (struct execdata *e)
{
bfd_set_error (bfd_error_no_error);
e->bfd = bfd_openstreamr (NULL, NULL, &e->stream);
if (e->bfd == NULL)
{
e->error = b2he (ENOEXEC);
return;
}
if (!bfd_check_format (e->bfd, bfd_object))
{
e->error = b2he (ENOEXEC);
return;
}
else if (/* !(e->bfd->flags & EXEC_P) || XXX */
(host_bfd.arch_info->compatible = e->bfd->arch_info->compatible,
bfd_arch_get_compatible (&host_bfd, e->bfd)) != host_bfd.arch_info)
{
/* This file is of a recognized binary file format, but it is not
executable on this machine. */
e->error = b2he (ENOEXEC);
return;
}
e->entry = e->bfd->start_address;
}
#endif
#include <endian.h>
#if BYTE_ORDER == BIG_ENDIAN
#define host_ELFDATA ELFDATA2MSB
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
#define host_ELFDATA ELFDATA2LSB
#endif
static void
check_elf (struct execdata *e)
{
ElfW(Ehdr) *ehdr = map (e, 0, sizeof (ElfW(Ehdr)));
ElfW(Phdr) *phdr;
if (! ehdr)
{
if (!e->error)
e->error = ENOEXEC;
return;
}
if (*(ElfW(Word) *) ehdr != ((union { ElfW(Word) word;
unsigned char string[SELFMAG]; })
{ string: ELFMAG }).word)
{
e->error = ENOEXEC;
return;
}
if (ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
ehdr->e_ident[EI_DATA] != host_ELFDATA ||
ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
ehdr->e_version != EV_CURRENT ||
ehdr->e_ehsize < sizeof *ehdr ||
ehdr->e_phentsize != sizeof (ElfW(Phdr)))
{
e->error = ENOEXEC;
return;
}
e->error = elf_machine_matches_host (ehdr->e_machine);
if (e->error)
return;
/* Extract all this information now, while EHDR is mapped.
The `map' call below for the phdrs may reuse the mapping window. */
e->entry = ehdr->e_entry;
e->info.elf.anywhere = (ehdr->e_type == ET_DYN ||
ehdr->e_type == ET_REL);
e->info.elf.loadbase = 0;
e->info.elf.phnum = ehdr->e_phnum;
phdr = map (e, ehdr->e_phoff, ehdr->e_phnum * sizeof (ElfW(Phdr)));
if (! phdr)
{
if (!e->error)
e->error = ENOEXEC;
return;
}
e->info.elf.phdr = phdr;
e->info.elf.phdr_addr = ehdr->e_phoff;
}
/* Copy MAPPED_PHDR into E->info.elf.phdr, filling in E->interp.phdr
in the process. */
static void
check_elf_phdr (struct execdata *e, const ElfW(Phdr) *mapped_phdr)
{
const ElfW(Phdr) *phdr;
bool seen_phdr = false;
memcpy (e->info.elf.phdr, mapped_phdr,
e->info.elf.phnum * sizeof (ElfW(Phdr)));
/* Default state if we do not see PT_GNU_STACK telling us what to do.
Executable stack is the compatible default.
(XXX should be machine-dependent??)
*/
e->info.elf.execstack = 1;
for (phdr = e->info.elf.phdr;
phdr < &e->info.elf.phdr[e->info.elf.phnum];
++phdr)
switch (phdr->p_type)
{
case PT_INTERP:
e->interp.phdr = phdr;
break;
case PT_LOAD:
if (e->file_size <= (off_t) (phdr->p_offset +
phdr->p_filesz))
{
e->error = ENOEXEC;
return;
}
/* Check if this is the segment that contains the phdr image. */
if (!seen_phdr
&& (phdr->p_offset & -phdr->p_align) == 0 /* Sanity check. */
&& phdr->p_offset <= e->info.elf.phdr_addr
&& e->info.elf.phdr_addr - phdr->p_offset < phdr->p_filesz)
{
e->info.elf.phdr_addr += phdr->p_vaddr - phdr->p_offset;
seen_phdr = true;
}
break;
case PT_GNU_STACK:
e->info.elf.execstack = phdr->p_flags & PF_X;
break;
}
if (!seen_phdr)
e->info.elf.phdr_addr = 0;
}
static void
check (struct execdata *e)
{
check_elf (e); /* XXX/fault */
#ifdef BFD
if (e->error == ENOEXEC)
{
e->error = 0;
check_bfd (e);
}
#endif
}
/* Release the conch and clean up mapping the file and control page. */
static void
finish_mapping (struct execdata *e)
{
if (e->cntl != NULL)
{
pthread_spin_lock (&e->cntl->lock);
if (e->cntl->conch_status == USER_RELEASE_CONCH)
{
pthread_spin_unlock (&e->cntl->lock);
io_release_conch (e->file);
}
else
{
e->cntl->conch_status = USER_HAS_NOT_CONCH;
pthread_spin_unlock (&e->cntl->lock);
}
munmap (e->cntl, vm_page_size);
e->cntl = NULL;
}
if (e->filemap != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), e->filemap);
e->filemap = MACH_PORT_NULL;
}
if (e->cntlmap != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), e->cntlmap);
e->cntlmap = MACH_PORT_NULL;
}
}
/* Clean up after reading the file (need not be completed).
Note: this may be called several times for the E, so it must take care
of checking what was already freed. */
void
finish (struct execdata *e, int dealloc_file)
{
finish_mapping (e);
#ifdef BFD
if (e->bfd != NULL)
{
bfd_close (e->bfd);
e->bfd = NULL;
}
else
#endif
{
#ifdef EXECDATA_STREAM
fclose (&e->stream);
#else
if (e->file_data != NULL) {
free (e->file_data);
e->file_data = NULL;
} else if (map_buffer (e) != NULL) {
munmap (map_buffer (e), map_vsize (e));
map_buffer (e) = NULL;
}
#endif
}
if (dealloc_file && e->file != MACH_PORT_NULL)
{
mach_port_deallocate (mach_task_self (), e->file);
e->file = MACH_PORT_NULL;
}
}
/* Load the file. */
static void
load (task_t usertask, struct execdata *e)
{
e->task = usertask;
if (! e->error)
{
#ifdef BFD
if (e->bfd)
{
void load_bfd_section (bfd *bfd, asection *sec, void *userdata)
{
load_section (sec, userdata);
}
bfd_map_over_sections (e->bfd, &load_bfd_section, e);
}
else
#endif
{
ElfW(Word) i;
for (i = 0; i < e->info.elf.phnum; ++i)
if (e->info.elf.phdr[i].p_type == PT_LOAD)
load_section (&e->info.elf.phdr[i], e);
/* The entry point address is relative to wherever we loaded the
program text. */
e->entry += e->info.elf.loadbase;
}
}
/* Release the conch for the file. */
finish_mapping (e);
if (! e->error)
{
/* Do post-loading processing on the task. */
#ifdef BFD
if (e->bfd)
{
/* Do post-loading processing for a section. This consists of
peeking the pages of non-demand-paged executables. */
void postload_section (bfd *bfd, asection *sec, void *userdata)
{
struct execdata *u = userdata;
vm_address_t addr = 0;
vm_size_t secsize = 0;
addr = (vm_address_t) sec->vma;
secsize = sec->_raw_size;
if ((sec->flags & SEC_LOAD) && !(bfd->flags & D_PAGED))
{
/* Pre-load the section by peeking every mapped page. */
vm_address_t myaddr, a;
vm_size_t mysize;
myaddr = 0;
/* We have already mapped the file into the task in
load_section. Now read from the task's memory into our
own address space so we can peek each page and cause it to
be paged in. */
u->error = vm_read (u->task, trunc_page (addr),
round_page (secsize), &myaddr, &mysize);
if (u->error)
return;
/* Peek at the first word of each page. */
for (a = ((myaddr + mysize) & ~(vm_page_size - 1));
a >= myaddr; a -= vm_page_size)
/* Force it to be paged in. */
(void) *(volatile int *) a;
munmap ((caddr_t) myaddr, mysize);
}
}
bfd_map_over_sections (e->bfd, postload_section, e);
}
#endif
}
}
#ifdef GZIP
/* Check the file for being a gzip'd image. Return with ENOEXEC means not
a valid gzip file; return with another error means lossage in decoding;
return with zero means the file was uncompressed into memory which E now
points to, and `check' can be run again. */
static void
check_gzip (struct execdata *earg)
{
struct execdata *e = earg;
/* Entry points to unzip engine. */
int get_method (int);
void unzip (int, int);
extern long int bytes_out;
/* Callbacks from unzip for I/O and error interface. */
extern int (*unzip_read) (char *buf, size_t maxread);
extern void (*unzip_write) (const char *buf, size_t nwrite);
extern void (*unzip_read_error) (void);
extern void (*unzip_error) (const char *msg);
char *zipdata = NULL;
size_t zipdatasz = 0;
FILE *zipout = NULL;
jmp_buf ziperr;
off_t zipread_pos = 0;
int zipread (char *buf, size_t maxread)
{
char *contents = map (e, zipread_pos, 1);
size_t n;
if (contents == NULL)
{
errno = e->error;
return -1;
}
n = MIN (maxread, map_buffer (e) + map_fsize (e) - contents);
errno = hurd_safe_copyin (buf, contents, n); /* XXX/fault */
if (errno)
longjmp (ziperr, 2);
zipread_pos += n;
return n;
}
void zipwrite (const char *buf, size_t nwrite)
{
if (fwrite (buf, nwrite, 1, zipout) != 1)
longjmp (ziperr, 1);
}
void ziprderr (void)
{
errno = ENOEXEC;
longjmp (ziperr, 2);
}
void ziperror (const char *msg)
{
errno = ENOEXEC;
longjmp (ziperr, 2);
}
unzip_read = zipread;
unzip_write = zipwrite;
unzip_read_error = ziprderr;
unzip_error = ziperror;
if (setjmp (ziperr))
{
/* Error in unzipping jumped out. */
if (zipout)
{
fclose (zipout);
free (zipdata);
}
e->error = errno;
return;
}
if (get_method (0) != 0)
{
/* Not a happy gzip file. */
e->error = ENOEXEC;
return;
}
/* Matched gzip magic number. Ready to unzip.
Set up the output stream and let 'er rip. */
zipout = open_memstream (&zipdata, &zipdatasz);
if (! zipout)
{
e->error = errno;
return;
}
/* Call the gunzip engine. */
bytes_out = 0;
unzip (17, 23); /* Arguments ignored. */
/* The output is complete. Clean up the stream and store its resultant
buffer and size in the execdata as the file contents. */
fclose (zipout);
/* Clean up the old exec file stream's state.
Now that we have the contents all in memory (in E->file_data),
nothing will in fact ever try to use E->stream again. */
finish (e, 0);
/* Prepare the stream state to use the file contents already in memory. */
e->file_data = zipdata;
e->file_size = zipdatasz;
prepare_in_memory (e);
}
#endif
#ifdef BZIP2
/* Check the file for being a bzip2'd image. Return with ENOEXEC means not
a valid bzip2 file; return with another error means lossage in decoding;
return with zero means the file was uncompressed into memory which E now
points to, and `check' can be run again. */
static void
check_bzip2 (struct execdata *earg)
{
struct execdata *e = earg;
/* Entry points to bunzip2 engine. */
void do_bunzip2 (void);
/* Callbacks from unzip for I/O and error interface. */
extern int (*unzip_read) (char *buf, size_t maxread);
extern void (*unzip_write) (const char *buf, size_t nwrite);
extern void (*unzip_read_error) (void);
extern void (*unzip_error) (const char *msg);
char *zipdata = NULL;
size_t zipdatasz = 0;
FILE *zipout = NULL;
jmp_buf ziperr;
off_t zipread_pos = 0;
int zipread (char *buf, size_t maxread)
{
char *contents = map (e, zipread_pos, 1);
size_t n;
if (contents == NULL)
{
errno = e->error;
return -1;
}
n = MIN (maxread, map_buffer (e) + map_fsize (e) - contents);
errno = hurd_safe_copyin (buf, contents, n); /* XXX/fault */
if (errno)
longjmp (ziperr, 2);
zipread_pos += n;
return n;
}
void zipwrite (const char *buf, size_t nwrite)
{
if (fwrite (buf, nwrite, 1, zipout) != 1)
longjmp (ziperr, 1);
}
void ziprderr (void)
{
errno = ENOEXEC;
longjmp (ziperr, 2);
}
void ziperror (const char *msg)
{
errno = ENOEXEC;
longjmp (ziperr, 2);
}
unzip_read = zipread;
unzip_write = zipwrite;
unzip_read_error = ziprderr;
unzip_error = ziperror;
if (setjmp (ziperr))
{
/* Error in unzipping jumped out. */
if (zipout)
{
fclose (zipout);
free (zipdata);
}
e->error = errno;
return;
}
zipout = open_memstream (&zipdata, &zipdatasz);
if (! zipout)
{
e->error = errno;
return;
}
/* Call the bunzip2 engine. */
do_bunzip2 ();
/* The output is complete. Clean up the stream and store its resultant
buffer and size in the execdata as the file contents. */
fclose (zipout);
/* Clean up the old exec file stream's state.
Now that we have the contents all in memory (in E->file_data),
nothing will in fact ever try to use E->stream again. */
finish (e, 0);
/* Prepare the stream state to use the file contents already in memory. */
e->file_data = zipdata;
e->file_size = zipdatasz;
prepare_in_memory (e);
}
#endif
static inline void *
servercopy (void *arg, mach_msg_type_number_t argsize, boolean_t argcopy,
error_t *errorp)
{
if (! argcopy)
return arg;
/* ARG came in-line, so we must copy it. */
void *copy;
copy = mmap (0, argsize, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
if (copy == MAP_FAILED)
{
*errorp = errno;
return NULL;
}
memcpy (copy, arg, argsize);
return copy;
}
static error_t
do_exec (file_t file,
task_t oldtask,
int flags,
char *argv, mach_msg_type_number_t argvlen, boolean_t argv_copy,
char *envp, mach_msg_type_number_t envplen, boolean_t envp_copy,
mach_port_t *dtable, mach_msg_type_number_t dtablesize,
boolean_t dtable_copy,
mach_port_t *portarray, mach_msg_type_number_t nports,
boolean_t portarray_copy,
int *intarray, mach_msg_type_number_t nints, boolean_t intarray_copy,
mach_port_t *deallocnames, mach_msg_type_number_t ndeallocnames,
mach_port_t *destroynames, mach_msg_type_number_t ndestroynames)
{
struct execdata e, interp;
task_t newtask = MACH_PORT_NULL;
thread_t thread = MACH_PORT_NULL;
struct bootinfo *boot = 0;
int *ports_replaced;
int secure, defaults;
mach_msg_type_number_t i;
int intarray_dealloc = 0; /* Dealloc INTARRAY before returning? */
int oldtask_trashed = 0; /* Have we trashed the old task? */
/* Prime E for executing FILE and check its validity. This must be an
inline function because it stores pointers into alloca'd storage in E
for later use in `load'. */
void prepare_and_check (file_t file, struct execdata *e)
{
/* Prepare E to read the file. */
prepare (file, e);
if (e->error)
return;
/* Check the file for validity first. */
check (e);
#ifdef GZIP
if (e->error == ENOEXEC)
{
/* See if it is a compressed image. */
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
/* The gzip code is really cheesy, not even close to thread-safe.
So we serialize all uses of it. */
pthread_mutex_lock (&lock);
e->error = 0;
check_gzip (e);
pthread_mutex_unlock (&lock);
if (e->error == 0)
/* The file was uncompressed into memory, and now E describes the
uncompressed image rather than the actual file. Check it again
for a valid magic number. */
check (e);
}
#endif
#ifdef BZIP2
if (e->error == ENOEXEC)
{
/* See if it is a compressed image. */
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
/* The bzip2 code is really cheesy, not even close to thread-safe.
So we serialize all uses of it. */
pthread_mutex_lock (&lock);
e->error = 0;
check_bzip2 (e);
pthread_mutex_unlock (&lock);
if (e->error == 0)
/* The file was uncompressed into memory, and now E describes the
uncompressed image rather than the actual file. Check it again
for a valid magic number. */
check (e);
}
#endif
}
/* Here is the main body of the function. */
interp.file = MACH_PORT_NULL;
/* Catch this error now, rather than later. */
/* XXX For EXEC_DEFAULTS, this is only an error if one of the user's
ports is null; if they are all provided, then EXEC_DEFAULTS would
have no effect, and the lack of installed standard ports should
not cause an error. -mib */
if ((!std_ports || !std_ints) && (flags & (EXEC_SECURE|EXEC_DEFAULTS)))
return EIEIO;
/* Suspend the existing task before frobnicating it. */
if (oldtask != MACH_PORT_NULL && (e.error = task_suspend (oldtask)))
return e.error;
/* Prime E for executing FILE and check its validity. */
prepare_and_check (file, &e);
if (e.error == ENOEXEC)
{
/* Check for a #! executable file. */
check_hashbang (&e,
file, oldtask, flags,
argv, argvlen, argv_copy,
envp, envplen, envp_copy,
dtable, dtablesize, dtable_copy,
portarray, nports, portarray_copy,
intarray, nints, intarray_copy,
deallocnames, ndeallocnames,
destroynames, ndestroynames);
if (! e.error)
/* The #! exec succeeded; nothing more to do. */
return 0;
}
if (e.error)
/* The file is not a valid executable. */
goto out;
#ifdef BFD
if (e.bfd)
{
e.info.bfd_locations = alloca (e.bfd->section_count *
sizeof (vm_offset_t));
bfd_map_over_sections (e.bfd, check_section, &e);
}
else
#endif
{
const ElfW(Phdr) *phdr = e.info.elf.phdr;
e.info.elf.phdr = alloca (e.info.elf.phnum * sizeof (ElfW(Phdr)));
check_elf_phdr (&e, phdr);
}
if (oldtask == MACH_PORT_NULL)
flags |= EXEC_NEWTASK;
if (flags & (EXEC_NEWTASK|EXEC_SECURE))
{
/* Create the new task. If we are not being secure, then use OLDTASK
for the task_create RPC, in case it is something magical. */
e.error = task_create (((flags & EXEC_SECURE) ||
oldtask == MACH_PORT_NULL) ?
mach_task_self () : oldtask,
#ifdef KERN_INVALID_LEDGER
NULL, 0, /* OSF Mach */
#endif
0, &newtask);
if (e.error)
goto out;
}
else
newtask = oldtask;
pthread_rwlock_rdlock (&std_lock);
{
/* Store the data that we will give in response
to the RPC on the new task's bootstrap port. */
/* Set boot->portarray[IDX] to NEW. If REAUTH is nonzero,
io_reauthenticate NEW and set it to the authenticated port.
If CONSUME is nonzero, a reference on NEW is consumed;
it is invalid to give nonzero values to both REAUTH and CONSUME. */
#define use(idx, new, reauth, consume) \
do { use1 (idx, new, reauth, consume); \
if (e.error) goto stdout; } while (0)
void use1 (unsigned int idx, mach_port_t new,
int reauth, int consume)
{
if (new != MACH_PORT_NULL && reauth)
{
mach_port_t ref = mach_reply_port (), authed;
/* MAKE_SEND is safe here because we destroy REF ourselves. */
e.error = io_reauthenticate (new, ref, MACH_MSG_TYPE_MAKE_SEND);
if (! e.error)
e.error = auth_user_authenticate
(boot->portarray[INIT_PORT_AUTH],
ref, MACH_MSG_TYPE_MAKE_SEND, &authed);
mach_port_destroy (mach_task_self (), ref);
if (e.error)
return;
new = authed;
}
else
{
if (!consume && new != MACH_PORT_NULL)
mach_port_mod_refs (mach_task_self (),
new, MACH_PORT_RIGHT_SEND, 1);
}
boot->portarray[idx] = new;
ports_replaced[idx] = 1;
}
e.error = ports_create_port (execboot_portclass, port_bucket,
sizeof *boot, &boot);
if (boot == NULL)
{
stdout:
pthread_rwlock_unlock (&std_lock);
goto out;
}
bzero (&boot->pi + 1, (char *) &boot[1] - (char *) (&boot->pi + 1));
/* These flags say the information we pass through to the new program
may need to be modified. */
secure = (flags & EXEC_SECURE);
defaults = (flags & EXEC_DEFAULTS);
/* Now record the big blocks of data we shuffle around unchanged.
Whatever arrived inline, we must allocate space for so it can
survive after this RPC returns. */
boot->flags = flags;
argv = servercopy (argv, argvlen, argv_copy, &e.error);
if (e.error)
goto stdout;
boot->argv = argv;
boot->argvlen = argvlen;
envp = servercopy (envp, envplen, envp_copy, &e.error);
if (e.error)
goto stdout;
boot->envp = envp;
boot->envplen = envplen;
dtable = servercopy (dtable, dtablesize * sizeof (mach_port_t),
dtable_copy, &e.error);
if (e.error)
goto stdout;
boot->dtable = dtable;
boot->dtablesize = dtablesize;
if ((secure || defaults) && nints < INIT_INT_MAX)
{
/* Make sure the intarray is at least big enough. */
if (intarray_copy || (round_page (nints * sizeof (int)) <
round_page (INIT_INT_MAX * sizeof (int))))
{
/* Allocate a new vector that is big enough. */
boot->intarray = mmap (0, INIT_INT_MAX * sizeof (int),
PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
memcpy (boot->intarray, intarray, nints * sizeof (int));
intarray_dealloc = !intarray_copy;
}
else
boot->intarray = intarray;
boot->nints = INIT_INT_MAX;
}
else
{
intarray = servercopy (intarray, nints * sizeof (int), intarray_copy,
&e.error);
if (e.error)
goto stdout;
boot->intarray = intarray;
boot->nints = nints;
}
if (secure)
boot->intarray[INIT_UMASK] = std_ints ? std_ints[INIT_UMASK] : CMASK;
/* Now choose the ports to give the new program. */
boot->nports = nports < INIT_PORT_MAX ? INIT_PORT_MAX : nports;
boot->portarray = mmap (0, boot->nports * sizeof (mach_port_t),
PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
/* Start by copying the array as passed. */
for (i = 0; i < nports; ++i)
boot->portarray[i] = portarray[i];
if (MACH_PORT_NULL != 0)
for (; i < boot->nports; ++i)
boot->portarray[i] = MACH_PORT_NULL;
/* Keep track of which ports in BOOT->portarray come from the original
PORTARRAY, and which we replace. */
ports_replaced = alloca (boot->nports * sizeof *ports_replaced);
bzero (ports_replaced, boot->nports * sizeof *ports_replaced);
if (portarray[INIT_PORT_BOOTSTRAP] == MACH_PORT_NULL &&
oldtask != MACH_PORT_NULL)
{
if (! task_get_bootstrap_port (oldtask,
&boot->portarray[INIT_PORT_BOOTSTRAP]))
ports_replaced[INIT_PORT_BOOTSTRAP] = 1;
}
/* Note that the parentheses on this first test are different from the
others below it. */
if ((secure || defaults)
&& boot->portarray[INIT_PORT_AUTH] == MACH_PORT_NULL)
/* Q: Doesn't this let anyone run a program and make it
get a root auth port?
A: No; the standard port for INIT_PORT_AUTH has no UID's at all.
See init.trim/init.c (init_stdarrays). */
use (INIT_PORT_AUTH, std_ports[INIT_PORT_AUTH], 0, 0);
if (secure || (defaults
&& boot->portarray[INIT_PORT_PROC] == MACH_PORT_NULL))
{
/* Ask the proc server for the proc port for this task. */
mach_port_t new;
e.error = proc_task2proc (procserver, newtask, &new);
if (e.error)
goto stdout;
use (INIT_PORT_PROC, new, 0, 1);
}
else if (oldtask != newtask && oldtask != MACH_PORT_NULL
&& boot->portarray[INIT_PORT_PROC] != MACH_PORT_NULL)
{
mach_port_t new;
/* This task port refers to the old task; use it to fetch a new
one for the new task. */
e.error = proc_task2proc (boot->portarray[INIT_PORT_PROC],
newtask, &new);
if (e.error)
goto stdout;
use (INIT_PORT_PROC, new, 0, 1);
}
if (secure || (defaults
&& boot->portarray[INIT_PORT_CRDIR] == MACH_PORT_NULL))
use (INIT_PORT_CRDIR, std_ports[INIT_PORT_CRDIR], 1, 0);
if ((secure || defaults)
&& boot->portarray[INIT_PORT_CWDIR] == MACH_PORT_NULL)
use (INIT_PORT_CWDIR, std_ports[INIT_PORT_CWDIR], 1, 0);
}
pthread_rwlock_unlock (&std_lock);
/* We have now concocted in BOOT the complete Hurd context (ports and
ints) that the new program image will run under. We will use these
ports for looking up the interpreter file if there is one. */
if (! e.error && e.interp.section)
{
/* There is an interpreter section specifying another file to load
along with this executable. Find the name of the file and open
it. */
#ifdef BFD
char namebuf[e.bfd ? e.interp.section->_raw_size : 0];
#endif
char *name;
#ifdef BFD
if (e.bfd)
{
if (! bfd_get_section_contents (e.bfd, e.interp.section,
namebuf, 0,
e.interp.section->_raw_size))
{
e.error = b2he (errno);
name = NULL;
}
else
name = namebuf;
}
else
#endif
{
name = map (&e, (e.interp.phdr->p_offset
& ~(e.interp.phdr->p_align - 1)),
e.interp.phdr->p_filesz);
if (! name && ! e.error)
e.error = ENOEXEC;
}
if (! name)
e.interp.section = NULL;
else
{
/* Open the named file using the appropriate directory ports for
the user. */
error_t user_port (int which, error_t (*operate) (mach_port_t))
{
return (*operate) (boot->nports > which ?
boot->portarray[which] :
MACH_PORT_NULL);
}
file_t user_fd (int fd)
{
if (fd < 0 || fd >= boot->dtablesize ||
boot->dtable[fd] == MACH_PORT_NULL)
{
errno = EBADF;
return MACH_PORT_NULL;
}
mach_port_mod_refs (mach_task_self (), boot->dtable[fd],
MACH_PORT_RIGHT_SEND, +1);
return boot->dtable[fd];
}
/* XXX/fault */
e.error = hurd_file_name_lookup (&user_port, &user_fd, 0,
name, O_READ, 0, &interp.file);
}
}
if (interp.file != MACH_PORT_NULL)
{
/* We opened an interpreter file. Prepare it for loading too. */
prepare_and_check (interp.file, &interp);
if (! interp.error)
{
#ifdef BFD
if (interp.bfd)
{
interp.info.bfd_locations = alloca (interp.bfd->section_count *
sizeof (vm_offset_t));
bfd_map_over_sections (interp.bfd, check_section, &e);
}
else
#endif
{
const ElfW(Phdr) *phdr = interp.info.elf.phdr;
interp.info.elf.phdr = alloca (interp.info.elf.phnum *
sizeof (ElfW(Phdr)));
check_elf_phdr (&interp, phdr);
}
}
e.error = interp.error;
}
if (e.error)
goto out;
/* We are now committed to the exec. It "should not fail".
If it does fail now, the task will be hopelessly munged. */
if (newtask == oldtask)
{
thread_t *threads;
mach_msg_type_number_t nthreads, i;
/* Terminate all the threads of the old task. */
e.error = task_threads (oldtask, &threads, &nthreads);
if (e.error)
goto out;
for (i = 0; i < nthreads; ++i)
{
thread_terminate (threads[i]);
mach_port_deallocate (mach_task_self (), threads[i]);
}
munmap ((caddr_t) threads, nthreads * sizeof (thread_t));
/* Deallocate the entire virtual address space of the task. */
vm_deallocate (oldtask,
VM_MIN_ADDRESS, VM_MAX_ADDRESS - VM_MIN_ADDRESS);
/* Nothing is supposed to go wrong any more. If anything does, the
old task is now in a hopeless state and must be killed. */
oldtask_trashed = 1;
/* Deallocate and destroy the ports requested by the caller.
These are ports the task wants not to lose if the exec call
fails, but wants removed from the new program task. */
for (i = 0; i < ndeallocnames; ++i)
mach_port_deallocate (oldtask, deallocnames[i]);
for (i = 0; i < ndestroynames; ++i)
mach_port_destroy (oldtask, destroynames[i]);
}
/* XXX this should be below
it is here to work around a vm_map kernel bug. */
if (interp.file != MACH_PORT_NULL)
{
/* Load the interpreter file. */
load (newtask, &interp);
if (interp.error)
{
e.error = interp.error;
goto out;
}
finish (&interp, 1);
}
/* Load the file into the task. */
load (newtask, &e);
if (e.error)
goto out;
/* XXX loading of interp belongs here */
/* Clean up. */
finish (&e, 0);
/* Now record some essential addresses from the image itself that the
program's startup code will need to know. We do this after loading
the image so that a load-anywhere image gets the adjusted addresses. */
if (e.info.elf.phdr_addr != 0)
{
#ifdef BFD
if (!e.bfd)
#endif
e.info.elf.phdr_addr += e.info.elf.loadbase;
boot->phdr_addr = e.info.elf.phdr_addr;
boot->phdr_size = e.info.elf.phnum * sizeof (ElfW(Phdr));
}
boot->user_entry = e.entry; /* already adjusted in `load' */
/* Create the initial thread. */
e.error = thread_create (newtask, &thread);
if (e.error)
goto out;
/* Start up the initial thread at the entry point. */
boot->stack_base = 0, boot->stack_size = 0; /* Don't care about values. */
e.error = mach_setup_thread (newtask, thread,
(void *) (e.interp.section ? interp.entry :
e.entry),
&boot->stack_base, &boot->stack_size);
if (e.error)
goto out;
#ifdef BFD
if (!e.bfd)
#endif
{
/* It would probably be better to change mach_setup_thread so
it does a vm_map with the right permissions to start with. */
if (!e.info.elf.execstack)
e.error = vm_protect (newtask, boot->stack_base, boot->stack_size,
0, VM_PROT_READ | VM_PROT_WRITE);
}
if (oldtask != newtask && oldtask != MACH_PORT_NULL)
{
/* The program is on its way. The old task can be nuked. */
process_t proc;
process_t psrv;
/* Use the canonical proc server if secure, or there is none other.
When not secure, it is nice to let processes associate with
whatever proc server turns them on, regardless of which exec
itself is using. */
if (secure
|| boot->nports <= INIT_PORT_PROC
|| boot->portarray[INIT_PORT_PROC] == MACH_PORT_NULL)
psrv = procserver;
else
psrv = boot->portarray[INIT_PORT_PROC];
/* XXX there is a race here for SIGKILLing the process. -roland
I don't think it matters. -mib */
if (! proc_task2proc (psrv, oldtask, &proc))
{
proc_reassign (proc, newtask);
mach_port_deallocate (mach_task_self (), proc);
}
}
/* Make sure the proc server has the right idea of our identity. */
if (secure)
{
uid_t euidbuf[10], egidbuf[10], auidbuf[10], agidbuf[10];
uid_t *euids, *egids, *auids, *agids;
size_t neuids, negids, nauids, nagids;
error_t err;
/* Find out what our UID is from the auth server. */
neuids = negids = nauids = nagids = 10;
euids = euidbuf, egids = egidbuf;
auids = auidbuf, agids = agidbuf;
err = auth_getids (boot->portarray[INIT_PORT_AUTH],
&euids, &neuids, &auids, &nauids,
&egids, &negids, &agids, &nagids);
if (!err)
{
/* Set the owner with the proc server */
/* Not much we can do about errors here; caller is responsible
for making sure that the provided proc port is correctly
authenticated anyhow. */
proc_setowner (boot->portarray[INIT_PORT_PROC],
neuids ? euids[0] : 0, !neuids);
/* Clean up */
if (euids != euidbuf)
munmap (euids, neuids * sizeof (uid_t));
if (egids != egidbuf)
munmap (egids, negids * sizeof (uid_t));
if (auids != auidbuf)
munmap (auids, nauids * sizeof (uid_t));
if (agids != agidbuf)
munmap (agids, nagids * sizeof (uid_t));
}
}
{
mach_port_t btport = ports_get_send_right (boot);
e.error = task_set_bootstrap_port (newtask, btport);
mach_port_deallocate (mach_task_self (), btport);
}
out:
if (interp.file != MACH_PORT_NULL)
finish (&interp, 1);
finish (&e, !e.error);
if (!e.error && (flags & EXEC_SIGTRAP)) /* XXX && !secure ? */
{
/* This is a "traced" exec, i.e. the new task is to be debugged. The
caller has requested that the new process stop with SIGTRAP before
it starts. Since the process has no signal thread yet to do its
own POSIX signal mechanics, we simulate it by notifying the proc
server of the signal and leaving the initial thread with a suspend
count of one, as it would be if the process were stopped by a
POSIX signal. */
mach_port_t proc;
if (boot->nports > INIT_PORT_PROC)
proc = boot->portarray[INIT_PORT_PROC];
else
/* Ask the proc server for the proc port for this task. */
e.error = proc_task2proc (procserver, newtask, &proc);
if (!e.error)
/* Tell the proc server that the process has stopped with the
SIGTRAP signal. Don't bother to check for errors from the RPC
here; for non-secure execs PROC may be the user's own proc
server its confusion shouldn't make the exec fail. */
proc_mark_stop (proc, SIGTRAP, 0);
}
if (boot)
{
/* Release the original reference. Now there is only one
reference, which will be released on no-senders notification.
If we are bailing out due to error before setting the task's
bootstrap port, this will be the last reference and BOOT
will get cleaned up here. */
if (e.error)
/* Kill the pointers to the argument information so the cleanup
of BOOT doesn't deallocate it. It will be deallocated my MiG
when we return the error. */
bzero (&boot->pi + 1, (char *) &boot[1] - (char *) (&boot->pi + 1));
else
/* Do this before we release the last reference. */
if (boot->nports > INIT_PORT_PROC)
proc_mark_exec (boot->portarray[INIT_PORT_PROC]);
ports_port_deref (boot);
}
if (thread != MACH_PORT_NULL)
{
if (!e.error && !(flags & EXEC_SIGTRAP))
thread_resume (thread);
mach_port_deallocate (mach_task_self (), thread);
}
if (e.error)
{
if (oldtask != newtask)
{
/* We created a new task but failed to set it up. Kill it. */
task_terminate (newtask);
mach_port_deallocate (mach_task_self (), newtask);
}
if (oldtask_trashed)
/* The old task is hopelessly trashed; there is no way it
can resume execution. Coup de grace. */
task_terminate (oldtask);
else
/* Resume the old task, which we suspended earlier. */
task_resume (oldtask);
}
else
{
if (oldtask != newtask)
{
/* We successfully set the new task up.
Terminate the old task and deallocate our right to it. */
task_terminate (oldtask);
mach_port_deallocate (mach_task_self (), oldtask);
}
else
/* Resume the task, it is ready to run the new program. */
task_resume (oldtask);
/* Deallocate the right to the new task we created. */
mach_port_deallocate (mach_task_self (), newtask);
for (i = 0; i < nports; ++i)
if (ports_replaced[i] && portarray[i] != MACH_PORT_NULL)
/* This port was replaced, so the reference that arrived in the
original portarray is not being saved in BOOT for transfer to
the user task. Deallocate it; we don't want it, and MiG will
leave it for us on successful return. */
mach_port_deallocate (mach_task_self (), portarray[i]);
/* If there is vm_allocate'd space for the original intarray and/or
portarray, and we are not saving those pointers in BOOT for later
transfer, deallocate the original space now. */
if (intarray_dealloc)
munmap (intarray, nints * sizeof intarray[0]);
if (!portarray_copy)
munmap (portarray, nports * sizeof portarray[0]);
}
return e.error;
}
kern_return_t
S_exec_exec (struct trivfs_protid *protid,
file_t file,
task_t oldtask,
int flags,
char *argv, mach_msg_type_number_t argvlen, boolean_t argv_copy,
char *envp, mach_msg_type_number_t envplen, boolean_t envp_copy,
mach_port_t *dtable, mach_msg_type_number_t dtablesize,
boolean_t dtable_copy,
mach_port_t *portarray, mach_msg_type_number_t nports,
boolean_t portarray_copy,
int *intarray, mach_msg_type_number_t nints,
boolean_t intarray_copy,
mach_port_t *deallocnames, mach_msg_type_number_t ndeallocnames,
mach_port_t *destroynames, mach_msg_type_number_t ndestroynames)
{
if (! protid)
return EOPNOTSUPP;
/* There were no user-specified exec servers,
or none of them could be found. */
return do_exec (file, oldtask, flags,
argv, argvlen, argv_copy,
envp, envplen, envp_copy,
dtable, dtablesize, dtable_copy,
portarray, nports, portarray_copy,
intarray, nints, intarray_copy,
deallocnames, ndeallocnames,
destroynames, ndestroynames);
}
kern_return_t
S_exec_setexecdata (struct trivfs_protid *protid,
mach_port_t *ports, mach_msg_type_number_t nports, int ports_copy,
int *ints, mach_msg_type_number_t nints, int ints_copy)
{
error_t err;
if (! protid || (protid->realnode != MACH_PORT_NULL && ! protid->isroot))
return EPERM;
if (nports < INIT_PORT_MAX || nints < INIT_INT_MAX)
return EINVAL; /* */
err = 0;
ports = servercopy (ports, nports * sizeof (mach_port_t), ports_copy, &err);
if (err)
return err;
ints = servercopy (ints, nints * sizeof (int), ints_copy, &err);
if (err)
{
munmap (ports, nports * sizeof (mach_port_t));
return err;
}
pthread_rwlock_wrlock (&std_lock);
if (std_ports)
{
mach_msg_type_number_t i;
for (i = 0; i < std_nports; ++i)
mach_port_deallocate (mach_task_self (), std_ports[i]);
munmap (std_ports, std_nports * sizeof (mach_port_t));
}
std_ports = ports;
std_nports = nports;
if (std_ints)
munmap (std_ints, std_nints * sizeof (int));
std_ints = ints;
std_nints = nints;
pthread_rwlock_unlock (&std_lock);
return 0;
}
#include "exec_startup_S.h"
/* RPC sent on the bootstrap port. */
kern_return_t
S_exec_startup_get_info (mach_port_t port,
vm_address_t *user_entry,
vm_address_t *phdr_data, vm_size_t *phdr_size,
vm_address_t *stack_base, vm_size_t *stack_size,
int *flags,
char **argvp, mach_msg_type_number_t *argvlen,
char **envpp, mach_msg_type_number_t *envplen,
mach_port_t **dtable,
mach_msg_type_name_t *dtablepoly,
mach_msg_type_number_t *dtablesize,
mach_port_t **portarray,
mach_msg_type_name_t *portpoly,
mach_msg_type_number_t *nports,
int **intarray, mach_msg_type_number_t *nints)
{
struct bootinfo *boot = ports_lookup_port (port_bucket, port,
execboot_portclass);
if (! boot)
return EOPNOTSUPP;
ports_port_deref (boot);
/* Pass back all the information we are storing. */
*user_entry = boot->user_entry;
*phdr_data = boot->phdr_addr;
*phdr_size = boot->phdr_size;
*stack_base = boot->stack_base;
*stack_size = boot->stack_size;
*argvp = boot->argv;
*argvlen = boot->argvlen;
boot->argvlen = 0;
*envpp = boot->envp;
*envplen = boot->envplen;
boot->envplen = 0;
*dtable = boot->dtable;
*dtablesize = boot->dtablesize;
*dtablepoly = MACH_MSG_TYPE_MOVE_SEND;
boot->dtablesize = 0;
*intarray = boot->intarray;
*nints = boot->nints;
boot->nints = 0;
*portarray = boot->portarray;
*nports = boot->nports;
*portpoly = MACH_MSG_TYPE_MOVE_SEND;
boot->nports = 0;
*flags = boot->flags;
return 0;
}
|