Loading...
   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
/*
 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 *	Copyright (C) 1988, 1989,  NeXT, Inc.
 *
 *	File:	kern/mach_loader.c
 *	Author:	Avadis Tevanian, Jr.
 *
 *	Mach object file loader (kernel version, for now).
 *
 * 21-Jul-88  Avadis Tevanian, Jr. (avie) at NeXT
 *	Started.
 */
#include <sys/param.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/fcntl.h>
#include <sys/ubc.h>

#include <mach/mach_types.h>

#include <kern/mach_loader.h>
#include <kern/task.h>

#include <mach-o/fat.h>
#include <mach-o/loader.h>

#include <kern/cpu_number.h>

#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <vm/vm_pager.h>
#include <vm/vnode_pager.h>
#include <mach/vm_statistics.h>

#include <mach/shared_memory_server.h>
#include <vm/vm_shared_memory_server.h>

#include <machine/vmparam.h>

/*
 * Prototypes of static functions.
 */
static
load_return_t
parse_machfile(
	struct vnode		*vp,
	vm_map_t			map,
	thread_act_t		thr_act,
	struct mach_header	*header,
	unsigned long		file_offset,
	unsigned long		macho_size,
	int					depth,
	load_result_t		*result,
	boolean_t		clean_regions
),
load_segment(
	struct segment_command	*scp,
	void * 					pager,
	unsigned long			pager_offset,
	unsigned long			macho_size,
	unsigned long			end_of_file,
	vm_map_t				map,
	load_result_t			*result
),
load_unixthread(
	struct thread_command	*tcp,
	thread_act_t			thr_act,
	load_result_t			*result
),
load_thread(
	struct thread_command	*tcp,
	thread_act_t			thr_act,
	load_result_t			*result
),
load_threadstate(
	thread_t		thread,
	unsigned long	*ts,
	unsigned long	total_size
),
load_threadstack(
	thread_t		thread,
	unsigned long	*ts,
	unsigned long	total_size,
	vm_offset_t		*user_stack,
	int				*customstack
),
load_threadentry(
	thread_t		thread,
	unsigned long	*ts,
	unsigned long	total_size,
	vm_offset_t		*entry_point
),
load_dylinker(
	struct dylinker_command	*lcp,
	vm_map_t				map,
	thread_act_t			thr_act,
	int						depth,
	load_result_t			*result,
	boolean_t			clean_regions
),
get_macho_vnode(
	char				*path,
	struct mach_header	*mach_header,
	unsigned long		*file_offset,
	unsigned long		*macho_size,
	struct vnode		**vpp
);

load_return_t
load_machfile(
	struct vnode		*vp,
	struct mach_header	*header,
	unsigned long		file_offset,
	unsigned long		macho_size,
	load_result_t		*result,
	thread_act_t 		thr_act,
	vm_map_t 		new_map,
	boolean_t		clean_regions
)
{
	pmap_t			pmap;
	vm_map_t		map;
	vm_map_t		old_map;
	load_result_t		myresult;
	kern_return_t		kret;
	load_return_t		lret;
	boolean_t create_map = TRUE;
#ifndef i386
	extern pmap_t pmap_create(vm_size_t   size); /* XXX */
#endif

	if (new_map != VM_MAP_NULL) {
		create_map = FALSE;
	}

	if (create_map) {
		old_map = current_map();
#ifdef i386
		pmap = get_task_pmap(current_task());
		pmap_reference(pmap);
#else
		pmap = pmap_create((vm_size_t) 0);
#endif
		map = vm_map_create(pmap,
				get_map_min(old_map),
				get_map_max(old_map),
				TRUE); /**** FIXME ****/
	} else
		map = new_map;
		
	if (!result)
		result = &myresult;

	*result = (load_result_t) { 0 };

	lret = parse_machfile(vp, map, thr_act, header, file_offset, macho_size,
			     0, result, clean_regions);

	if (lret != LOAD_SUCCESS) {
		if (create_map) {
			vm_map_deallocate(map);	/* will lose pmap reference too */
		}
		return(lret);
	}

	/*
	 *	Commit to new map.  First make sure that the current
	 *	users of the task get done with it, and that we clean
	 *	up the old contents of IPC and memory.  The task is
	 *	guaranteed to be single threaded upon return (us).
	 *
	 *	Swap the new map for the old, which  consumes our new map
	 *	reference but each leaves us responsible for the old_map reference.
	 *	That lets us get off the pmap associated with it, and
	 *	then we can release it.
	 */
	 if (create_map) {
		task_halt(current_task());

		old_map = swap_task_map(current_task(), map);
#ifndef i386
		pmap_switch(pmap);	/* Make sure we are using the new pmap */
#endif
		vm_map_deallocate(old_map);
	}
	return(LOAD_SUCCESS);
}

int	dylink_test = 1;

static
load_return_t
parse_machfile(
	struct vnode		*vp,
	vm_map_t		map,
	thread_act_t		thr_act,
	struct mach_header	*header,
	unsigned long		file_offset,
	unsigned long		macho_size,
	int			depth,
	load_result_t		*result,
	boolean_t		clean_regions
)
{
	struct machine_slot	*ms;
	int			ncmds;
	struct load_command	*lcp, *next;
	struct dylinker_command	*dlp = 0;
	void *			pager;
	load_return_t		ret = LOAD_SUCCESS;
	vm_offset_t		addr, kl_addr;
	vm_size_t		size,kl_size;
	int			offset;
	int			pass;
	struct proc *p = current_proc();		/* XXXX */
	int			error;
	int resid=0;
	task_t task;

	/*
	 *	Break infinite recursion
	 */
	if (depth > 6)
		return(LOAD_FAILURE);

	task = (task_t)get_threadtask(thr_act);

	depth++;

	/*
	 *	Check to see if right machine type.
	 */
	ms = &machine_slot[cpu_number()];
	if ((header->cputype != ms->cpu_type) ||
	    !check_cpu_subtype(header->cpusubtype))
		return(LOAD_BADARCH);
		
	switch (header->filetype) {
	
	case MH_OBJECT:
	case MH_EXECUTE:
	case MH_PRELOAD:
		if (depth != 1)
			return (LOAD_FAILURE);
		break;
		
	case MH_FVMLIB:
	case MH_DYLIB:
		if (depth == 1)
			return (LOAD_FAILURE);
		break;

	case MH_DYLINKER:
		if (depth != 2)
			return (LOAD_FAILURE);
		break;
		
	default:
		return (LOAD_FAILURE);
	}

	/*
	 *	Get the pager for the file.
	 */
	UBCINFOCHECK("parse_machfile", vp);
	pager = (void *) ubc_getpager(vp);

	/*
	 *	Map portion that must be accessible directly into
	 *	kernel's map.
	 */
	if ((sizeof (struct mach_header) + header->sizeofcmds) > macho_size)
		return(LOAD_BADMACHO);

	/*
	 *	Round size of Mach-O commands up to page boundry.
	 */
	size = round_page_32(sizeof (struct mach_header) + header->sizeofcmds);
	if (size <= 0)
		return(LOAD_BADMACHO);

	/*
	 * Map the load commands into kernel memory.
	 */
	addr = 0;
	kl_size = size;
	kl_addr = kalloc(size);
	addr = kl_addr;
	if (addr == NULL)
		return(LOAD_NOSPACE);

	if(error = vn_rdwr(UIO_READ, vp, (caddr_t)addr, size, file_offset,
	    UIO_SYSSPACE, 0, p->p_ucred, &resid, p)) {
		if (kl_addr )
			kfree(kl_addr, kl_size);
		return(LOAD_IOERROR);
	}
	/* ubc_map(vp); */ /* NOT HERE */
	
	/*
	 *	Scan through the commands, processing each one as necessary.
	 */
	for (pass = 1; pass <= 2; pass++) {
		offset = sizeof(struct mach_header);
		ncmds = header->ncmds;
		while (ncmds--) {
			/*
			 *	Get a pointer to the command.
			 */
			lcp = (struct load_command *)(addr + offset);
			offset += lcp->cmdsize;

			/*
			 *	Check for valid lcp pointer by checking
			 *	next offset.
			 */
			if (offset > header->sizeofcmds
					+ sizeof(struct mach_header)) {
				if (kl_addr )
					kfree(kl_addr, kl_size);
				return(LOAD_BADMACHO);
			}

			/*
			 *	Check for valid command.
			 */
			switch(lcp->cmd) {
			case LC_SEGMENT:
				if (pass != 1)
					break;
				ret = load_segment(
					       (struct segment_command *) lcp,
						   pager, file_offset,
						   macho_size,
						   (unsigned long)ubc_getsize(vp),
						   map,
						   result);
				break;
			case LC_THREAD:
				if (pass != 2)
					break;
				ret = load_thread((struct thread_command *)lcp, thr_act,
						  result);
				break;
			case LC_UNIXTHREAD:
				if (pass != 2)
					break;
				ret = load_unixthread(
						 (struct thread_command *) lcp, thr_act,
						 result);
				break;
			case LC_LOAD_DYLINKER:
				if (pass != 2)
					break;
				if ((depth == 1) && (dlp == 0))
					dlp = (struct dylinker_command *)lcp;
				else
					ret = LOAD_FAILURE;
				break;
			default:
				ret = LOAD_SUCCESS;/* ignore other stuff */
			}
			if (ret != LOAD_SUCCESS)
				break;
		}
		if (ret != LOAD_SUCCESS)
			break;
	}
	if ((ret == LOAD_SUCCESS) && (depth == 1)) {
		vm_offset_t addr;
		shared_region_mapping_t shared_region;
		struct shared_region_task_mappings	map_info;
		shared_region_mapping_t next;

RedoLookup:
		vm_get_shared_region(task, &shared_region);
		map_info.self = (vm_offset_t)shared_region;
		shared_region_mapping_info(shared_region,
			&(map_info.text_region),   
			&(map_info.text_size),
			&(map_info.data_region),
			&(map_info.data_size),
			&(map_info.region_mappings),
			&(map_info.client_base),
			&(map_info.alternate_base),
			&(map_info.alternate_next), 
			&(map_info.fs_base),
			&(map_info.system),
			&(map_info.flags), &next);

		if((map_info.flags & SHARED_REGION_FULL) ||
			(map_info.flags & SHARED_REGION_STALE)) {
			shared_region_mapping_t system_region;
			system_region = lookup_default_shared_region(
				map_info.fs_base, map_info.system);
			if((map_info.self != (vm_offset_t)system_region) &&
				(map_info.flags & SHARED_REGION_SYSTEM)) {
			   if(system_region == NULL) {
				shared_file_boot_time_init(
					map_info.fs_base, map_info.system);
			   } else {
			   	vm_set_shared_region(task, system_region);
			   }
			   shared_region_mapping_dealloc(
					(shared_region_mapping_t)map_info.self);
			   goto RedoLookup;
			} else if (map_info.flags & SHARED_REGION_SYSTEM) {
			      shared_region_mapping_dealloc(system_region);
			      shared_file_boot_time_init(
					map_info.fs_base, map_info.system);
			      shared_region_mapping_dealloc(
				     (shared_region_mapping_t)map_info.self);
			} else {
			      shared_region_mapping_dealloc(system_region);
			}
		}


		if (dylink_test) {
			p->p_flag |=  P_NOSHLIB; /* no shlibs in use */
			addr = map_info.client_base;
			if(clean_regions) {
			   vm_map(map, &addr, map_info.text_size, 
				0, SHARED_LIB_ALIAS,
				map_info.text_region, 0, FALSE,
				VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE);
			} else {
			   vm_map(map, &addr, map_info.text_size, 0, 
				(VM_MEMORY_SHARED_PMAP << 24) 
						| SHARED_LIB_ALIAS,
				map_info.text_region, 0, FALSE,
				VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE);
			}
			addr = map_info.client_base + map_info.text_size;
			vm_map(map, &addr, map_info.data_size, 
				0, SHARED_LIB_ALIAS,
				map_info.data_region, 0, TRUE,
				VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE);
	
			while (next) {
		           /* this should be fleshed out for the general case */
			   /* but this is not necessary for now.  Indeed we   */
			   /* are handling the com page inside of the         */
			   /* shared_region mapping create calls for now for  */
			   /* simplicities sake.  If more general support is  */
			   /* needed the code to manipulate the shared range  */
			   /* chain can be pulled out and moved to the callers*/
			   shared_region_mapping_info(next,
				&(map_info.text_region),   
				&(map_info.text_size),
				&(map_info.data_region),
				&(map_info.data_size),
				&(map_info.region_mappings),
				&(map_info.client_base),
				&(map_info.alternate_base),
				&(map_info.alternate_next), 
				&(map_info.fs_base),
				&(map_info.system),
				&(map_info.flags), &next);

			   addr = map_info.client_base;
			   vm_map(map, &addr, map_info.text_size, 
				0, SHARED_LIB_ALIAS,
				map_info.text_region, 0, FALSE,
				VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE);
			}
		}
        if (dlp != 0) {
            ret = load_dylinker(dlp, map, thr_act, 
                        depth, result, clean_regions);
        }
	}

	if (kl_addr )
		kfree(kl_addr, kl_size);

	if ((ret == LOAD_SUCCESS) && (depth == 1) &&
				(result->thread_count == 0))
		ret = LOAD_FAILURE;
	if (ret == LOAD_SUCCESS)
		ubc_map(vp);
		
	return(ret);
}

static
load_return_t
load_segment(
	struct segment_command	*scp,
	void *			pager,
	unsigned long		pager_offset,
	unsigned long		macho_size,
	unsigned long		end_of_file,
	vm_map_t		map,
	load_result_t		*result
)
{
	kern_return_t		ret;
	vm_offset_t		map_addr, map_offset;
	vm_size_t		map_size, seg_size, delta_size;
	caddr_t			tmp;
	vm_prot_t 		initprot;
	vm_prot_t		maxprot;

	/*
	 * Make sure what we get from the file is really ours (as specified
	 * by macho_size).
	 */
	if (scp->fileoff + scp->filesize > macho_size)
		return (LOAD_BADMACHO);

	seg_size = round_page_32(scp->vmsize);
	if (seg_size == 0)
		return(KERN_SUCCESS);

	/*
	 *	Round sizes to page size.
	 */
	map_size = round_page_32(scp->filesize);
	map_addr = trunc_page_32(scp->vmaddr);

	map_offset = pager_offset + scp->fileoff;

	if (map_size > 0) {
		initprot = (scp->initprot) & VM_PROT_ALL;
		maxprot = (scp->maxprot) & VM_PROT_ALL;
		/*
		 *	Map a copy of the file into the address space.
		 */
		ret = vm_map(map,
				&map_addr, map_size, (vm_offset_t)0, FALSE,
				pager, map_offset, TRUE,
				initprot, maxprot,
				VM_INHERIT_DEFAULT);
		if (ret != KERN_SUCCESS)
			return(LOAD_NOSPACE);
	
		/*
		 *	If the file didn't end on a page boundary,
		 *	we need to zero the leftover.
		 */
		delta_size = map_size - scp->filesize;
#if FIXME
		if (delta_size > 0) {
			vm_offset_t	tmp;
	
			ret = vm_allocate(kernel_map, &tmp, delta_size, TRUE);
			if (ret != KERN_SUCCESS)
				return(LOAD_RESOURCE);
	
			if (copyout(tmp, map_addr + scp->filesize,
								delta_size)) {
				(void) vm_deallocate(
						kernel_map, tmp, delta_size);
				return(LOAD_FAILURE);
			}
			
			(void) vm_deallocate(kernel_map, tmp, delta_size);
		}
#endif /* FIXME */
	}

	/*
	 *	If the virtual size of the segment is greater
	 *	than the size from the file, we need to allocate
	 *	zero fill memory for the rest.
	 */
	delta_size = seg_size - map_size;
	if (delta_size > 0) {
		vm_offset_t	tmp = map_addr + map_size;

		ret = vm_allocate(map, &tmp, delta_size, FALSE);
		if (ret != KERN_SUCCESS)
			return(LOAD_NOSPACE);
	}

	/*
	 *	Set protection values. (Note: ignore errors!)
	 */

	if (scp->maxprot != VM_PROT_DEFAULT) {
		(void) vm_protect(map,
					map_addr, seg_size,
					TRUE, scp->maxprot);
	}
	if (scp->initprot != VM_PROT_DEFAULT) {
		(void) vm_protect(map,
				      map_addr, seg_size,
				      FALSE, scp->initprot);
	}
	if ( (scp->fileoff == 0) && (scp->filesize != 0) )
		result->mach_header = map_addr;
	return(LOAD_SUCCESS);
}

static
load_return_t
load_unixthread(
	struct thread_command	*tcp,
	thread_act_t		thread,
	load_result_t		*result
)
{
	load_return_t	ret;
	int customstack =0;
	
	if (result->thread_count != 0)
		return (LOAD_FAILURE);
	
	ret = load_threadstack(thread,
		       (unsigned long *)(((vm_offset_t)tcp) + 
		       		sizeof(struct thread_command)),
		       tcp->cmdsize - sizeof(struct thread_command),
		       &result->user_stack,
			   &customstack);
	if (ret != LOAD_SUCCESS)
		return(ret);

	if (customstack)
			result->customstack = 1;
	else
			result->customstack = 0;
	ret = load_threadentry(thread,
		       (unsigned long *)(((vm_offset_t)tcp) + 
		       		sizeof(struct thread_command)),
		       tcp->cmdsize - sizeof(struct thread_command),
		       &result->entry_point);
	if (ret != LOAD_SUCCESS)
		return(ret);

	ret = load_threadstate(thread,
		       (unsigned long *)(((vm_offset_t)tcp) + 
		       		sizeof(struct thread_command)),
		       tcp->cmdsize - sizeof(struct thread_command));
	if (ret != LOAD_SUCCESS)
		return (ret);

	result->unixproc = TRUE;
	result->thread_count++;

	return(LOAD_SUCCESS);
}

static
load_return_t
load_thread(
	struct thread_command	*tcp,
	thread_act_t			thread,
	load_result_t		*result
)
{
	kern_return_t	kret;
	load_return_t	lret;
	task_t			task;
	int customstack=0;

	task = get_threadtask(thread);

	/* if count is 0; same as thr_act */
	if (result->thread_count != 0) {
		kret = thread_create(task, &thread);
		if (kret != KERN_SUCCESS)
			return(LOAD_RESOURCE);
		act_deallocate(thread);
	}

	lret = load_threadstate(thread,
		       (unsigned long *)(((vm_offset_t)tcp) + 
		       		sizeof(struct thread_command)),
		       tcp->cmdsize - sizeof(struct thread_command));
	if (lret != LOAD_SUCCESS)
		return (lret);

	if (result->thread_count == 0) {
		lret = load_threadstack(thread,
				(unsigned long *)(((vm_offset_t)tcp) + 
					sizeof(struct thread_command)),
				tcp->cmdsize - sizeof(struct thread_command),
				&result->user_stack,
				&customstack);
		if (customstack)
				result->customstack = 1;
		else
				result->customstack = 0;
			
		if (lret != LOAD_SUCCESS)
			return(lret);

		lret = load_threadentry(thread,
				(unsigned long *)(((vm_offset_t)tcp) + 
					sizeof(struct thread_command)),
				tcp->cmdsize - sizeof(struct thread_command),
				&result->entry_point);
		if (lret != LOAD_SUCCESS)
			return(lret);
	}
	/*
	 *	Resume thread now, note that this means that the thread
	 *	commands should appear after all the load commands to
	 *	be sure they don't reference anything not yet mapped.
	 */
	else
		thread_resume(thread);
		
	result->thread_count++;

	return(LOAD_SUCCESS);
}

static
load_return_t
load_threadstate(
	thread_t	thread,
	unsigned long	*ts,
	unsigned long	total_size
)
{
	kern_return_t	ret;
	unsigned long	size;
	int		flavor;

	/*
	 *	Set the thread state.
	 */

	while (total_size > 0) {
		flavor = *ts++;
		size = *ts++;
		total_size -= (size+2)*sizeof(unsigned long);
		if (total_size < 0)
			return(LOAD_BADMACHO);
		ret = thread_setstatus(thread, flavor, ts, size);
		if (ret != KERN_SUCCESS)
			return(LOAD_FAILURE);
		ts += size;	/* ts is a (unsigned long *) */
	}
	return(LOAD_SUCCESS);
}

static
load_return_t
load_threadstack(
	thread_t	thread,
	unsigned long	*ts,
	unsigned long	total_size,
	vm_offset_t	*user_stack,
	int *customstack
)
{
	kern_return_t	ret;
	unsigned long	size;
	int		flavor;

	while (total_size > 0) {
		flavor = *ts++;
		size = *ts++;
		total_size -= (size+2)*sizeof(unsigned long);
		if (total_size < 0)
			return(LOAD_BADMACHO);
		*user_stack = USRSTACK;
		ret = thread_userstack(thread, flavor, ts, size,
				user_stack, customstack);
		if (ret != KERN_SUCCESS)
			return(LOAD_FAILURE);
		ts += size;	/* ts is a (unsigned long *) */
	}
	return(LOAD_SUCCESS);
}

static
load_return_t
load_threadentry(
	thread_t	thread,
	unsigned long	*ts,
	unsigned long	total_size,
	vm_offset_t	*entry_point
)
{
	kern_return_t	ret;
	unsigned long	size;
	int		flavor;

	/*
	 *	Set the thread state.
	 */
	*entry_point = 0;
	while (total_size > 0) {
		flavor = *ts++;
		size = *ts++;
		total_size -= (size+2)*sizeof(unsigned long);
		if (total_size < 0)
			return(LOAD_BADMACHO);
		ret = thread_entrypoint(thread, flavor, ts, size, entry_point);
		if (ret != KERN_SUCCESS)
			return(LOAD_FAILURE);
		ts += size;	/* ts is a (unsigned long *) */
	}
	return(LOAD_SUCCESS);
}


static
load_return_t
load_dylinker(
	struct dylinker_command	*lcp,
	vm_map_t		map,
	thread_act_t	thr_act,
	int			depth,
	load_result_t		*result,
	boolean_t		clean_regions
)
{
	char			*name;
	char			*p;
	struct vnode		*vp;
	struct mach_header	header;
	unsigned long		file_offset;
	unsigned long		macho_size;
	vm_map_t		copy_map;
	load_result_t		myresult;
	kern_return_t		ret;
	vm_map_copy_t	tmp;
	vm_offset_t	dyl_start, map_addr;
	vm_size_t	dyl_length;
	extern pmap_t pmap_create(vm_size_t   size); /* XXX */

	name = (char *)lcp + lcp->name.offset;
	/*
	 *	Check for a proper null terminated string.
	 */
	p = name;
	do {
		if (p >= (char *)lcp + lcp->cmdsize)
			return(LOAD_BADMACHO);
	} while (*p++);

	ret = get_macho_vnode(name, &header, &file_offset, &macho_size, &vp);
	if (ret)
		return (ret);
			
	myresult = (load_result_t) { 0 };

	/*
	 *	Load the Mach-O.
	 */
		
	copy_map = vm_map_create(pmap_create(macho_size),
			get_map_min(map), get_map_max( map), TRUE);

	ret = parse_machfile(vp, copy_map, thr_act, &header,
				file_offset, macho_size,
				depth, &myresult, clean_regions);

	if (ret)
		goto out;

	if (get_map_nentries(copy_map) > 0) {

		dyl_start = get_map_start(copy_map);
		dyl_length = get_map_end(copy_map) - dyl_start;

		map_addr = dyl_start;
		ret = vm_allocate(map, &map_addr, dyl_length, FALSE);
		if (ret != KERN_SUCCESS)  {
			ret = vm_allocate(map, &map_addr, dyl_length, TRUE);
		}

		if (ret != KERN_SUCCESS) {
			ret = LOAD_NOSPACE;
			goto out;
		
		}
		ret = vm_map_copyin(copy_map, dyl_start, dyl_length, TRUE,
				&tmp);
		if (ret != KERN_SUCCESS) {
			(void) vm_map_remove(map,
					     map_addr,
					     map_addr + dyl_length,
					     VM_MAP_NO_FLAGS);
			goto out;
		}

		ret = vm_map_copy_overwrite(map, map_addr, tmp, FALSE);
		if (ret != KERN_SUCCESS) {
				vm_map_copy_discard(tmp);
				(void) vm_map_remove(map,
						     map_addr,
						     map_addr + dyl_length,
						     VM_MAP_NO_FLAGS);
				goto out;		}

		if (map_addr != dyl_start)
			myresult.entry_point += (map_addr - dyl_start);
	} else
		ret = LOAD_FAILURE;
	
	if (ret == LOAD_SUCCESS) {		
		result->dynlinker = TRUE;
		result->entry_point = myresult.entry_point;
		ubc_map(vp);
	}
out:
	vm_map_deallocate(copy_map);
	
	vrele(vp);
	return (ret);

}

static
load_return_t
get_macho_vnode(
	char			*path,
	struct mach_header	*mach_header,
	unsigned long		*file_offset,
	unsigned long		*macho_size,
	struct vnode		**vpp
)
{
	struct vnode		*vp;
	struct vattr attr, *atp;
	struct nameidata nid, *ndp;
	struct proc *p = current_proc();		/* XXXX */
	boolean_t		is_fat;
	struct fat_arch		fat_arch;
	int			error = LOAD_SUCCESS;
	int resid;
	union {
		struct mach_header	mach_header;
		struct fat_header	fat_header;
		char	pad[512];
	} header;
	off_t fsize = (off_t)0;
	struct	ucred *cred = p->p_ucred;
	int err2;
	
	ndp = &nid;
	atp = &attr;
	
	/* init the namei data to point the file user's program name */
	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);

	if (error = namei(ndp)) {
		if (error == ENOENT)
			error = LOAD_ENOENT;
		else
			error = LOAD_FAILURE;
		return(error);
	}
	
	vp = ndp->ni_vp;
	
	/* check for regular file */
	if (vp->v_type != VREG) {
		error = LOAD_PROTECT;
		goto bad1;
	}

	/* get attributes */
	if (error = VOP_GETATTR(vp, &attr, cred, p)) {
		error = LOAD_FAILURE;
		goto bad1;
	}

	/* Check mount point */
	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
		error = LOAD_PROTECT;
		goto bad1;
	}

	if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
		atp->va_mode &= ~(VSUID | VSGID);

	/* check access.  for root we have to see if any exec bit on */
	if (error = VOP_ACCESS(vp, VEXEC, cred, p)) {
		error = LOAD_PROTECT;
		goto bad1;
	}
	if ((atp->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
		error = LOAD_PROTECT;
		goto bad1;
	}

	/* hold the vnode for the IO */
	if (UBCINFOEXISTS(vp) && !ubc_hold(vp)) {
		error = LOAD_ENOENT;
		goto bad1;
	}

	/* try to open it */
	if (error = VOP_OPEN(vp, FREAD, cred, p)) {
		error = LOAD_PROTECT;
		ubc_rele(vp);
		goto bad1;
	}

	if(error = vn_rdwr(UIO_READ, vp, (caddr_t)&header, sizeof(header), 0,
	    UIO_SYSSPACE, IO_NODELOCKED, cred, &resid, p)) {
		error = LOAD_IOERROR;
		goto bad2;
	}
	
	if (header.mach_header.magic == MH_MAGIC)
	    is_fat = FALSE;
	else if (header.fat_header.magic == FAT_MAGIC ||
		 header.fat_header.magic == FAT_CIGAM)
	    is_fat = TRUE;
	else {
	    error = LOAD_BADMACHO;
	    goto bad2;
	}

	if (is_fat) {
		/* Look up our architecture in the fat file. */
		error = fatfile_getarch(vp, (vm_offset_t)(&header.fat_header), &fat_arch);
		if (error != LOAD_SUCCESS)
			goto bad2;

		/* Read the Mach-O header out of it */
		error = vn_rdwr(UIO_READ, vp, (caddr_t)&header.mach_header,
				sizeof(header.mach_header), fat_arch.offset,
				UIO_SYSSPACE, IO_NODELOCKED, cred, &resid, p);
		if (error) {
			error = LOAD_IOERROR;
			goto bad2;
		}

		/* Is this really a Mach-O? */
		if (header.mach_header.magic != MH_MAGIC) {
			error = LOAD_BADMACHO;
			goto bad2;
		}

		*file_offset = fat_arch.offset;
		*macho_size = fsize = fat_arch.size;
	} else {

		*file_offset = 0;
		*macho_size = fsize = attr.va_size;
	}

	*mach_header = header.mach_header;
	*vpp = vp;
	if (UBCISVALID(vp))
		ubc_setsize(vp, fsize);	/* XXX why? */
	
	VOP_UNLOCK(vp, 0, p);
	ubc_rele(vp);
	return (error);

bad2:
	VOP_UNLOCK(vp, 0, p);
	err2 = VOP_CLOSE(vp, FREAD, cred, p);
	ubc_rele(vp);
	vrele(vp);
	return (error);

bad1:
	vput(vp);
	return(error);
}