Diff
Not logged in

Differences From Artifact [81173f3fcf]:

To Artifact [6b86cdbe0d]:


8
9
10
11
12
13
14
15

16
17
18
19
20



21
22
23
24
25
26
27
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







-
+





+
+
+







 *
 * Copyright (c) 1995 Apple Computer, Inc.
 * Copyright (c) 2005 Daniel A. Steffen <das@users.sourceforge.net>
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclLoadDyld.c,v 1.14.2.4 2005/07/30 07:58:16 das Exp $
 * RCS: @(#) $Id: tclLoadDyld.c,v 1.14.2.5 2005/11/27 02:34:42 das Exp $
 */

#include "tclInt.h"
#include "tclPort.h"
#include <mach-o/dyld.h>
#include <mach-o/fat.h>
#include <mach-o/swap.h>
#include <mach-o/arch.h>
#undef panic
#include <mach/mach.h>

typedef struct Tcl_DyldModuleHandle {
    struct Tcl_DyldModuleHandle *nextPtr;
    NSModule module;
} Tcl_DyldModuleHandle;
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
119
120
121
122
123
124
125

126

127
128
129
130
131
132
133







-
+
-







    /*
     * First try the full path the user gave us. This is particularly
     * important if the cwd is inside a vfs, and we are trying to load using a
     * relative path.
     */

    native = Tcl_FSGetNativePath(pathPtr);
    dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_WITH_SEARCHING |
    dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
	    NSADDIMAGE_OPTION_RETURN_ON_ERROR);

    if (!dyldLibHeader) {
	NSLinkEditErrors editError;
	int errorNumber;
	CONST char *name, *msg, *objFileImageErrMsg = NULL;

	NSLinkEditError(&editError, &errorNumber, &name, &msg);
144
145
146
147
148
149
150
151


152
153
154
155
156
157
158
146
147
148
149
150
151
152

153
154
155
156
157
158
159
160
161







-
+
+








	    dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_WITH_SEARCHING
		    | NSADDIMAGE_OPTION_RETURN_ON_ERROR);
	    Tcl_DStringFree(&ds);
	    if (!dyldLibHeader) {
		NSLinkEditError(&editError, &errorNumber, &name, &msg);
	    }
	} else if ((editError==NSLinkEditFileFormatError && errorNumber==EBADMACHO)
	} else if ((editError == NSLinkEditFileFormatError
		&& errorNumber == EBADMACHO)
		|| editError == NSLinkEditOtherError){
	    /*
	     * The requested file was found but was not of type MH_DYLIB,
	     * attempt to load it as a MH_BUNDLE.
	     */

	    NSObjectFileImageReturnCode err =
228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245







-
+







    Tcl_Interp *interp;		/* For error reporting. */
    Tcl_LoadHandle loadHandle;	/* Handle from TclpDlopen. */
    CONST char *symbol;		/* Symbol name to look up. */
{
    NSSymbol nsSymbol;
    CONST char *native;
    Tcl_DString newName, ds;
    Tcl_PackageInitProc* proc = NULL;
    Tcl_PackageInitProc *proc = NULL;
    Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;

    /*
     * dyld adds an underscore to the beginning of symbol names.
     */

    native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
460
461
462
463
464
465
466
467


468
469
470



471
472
473



474
475








































476
477
478
479
480
481
482
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







-
+
+

-
-
+
+
+

-
-
+
+
+

-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








    /*
     * Try to create an object file image that we can load from.
     */

    if (codeSize >= 0) {
	NSObjectFileImageReturnCode err = NSObjectFileImageSuccess;

	CONST struct fat_header *fh = buffer;
	uint32_t ms = 0;
#ifndef __LP64__
	struct mach_header *mh = buffer;
	if (codeSize < sizeof(struct mach_header) || mh->magic != MH_MAGIC
	CONST struct mach_header *mh = NULL;
	#define mh_magic OSSwapHostToBigInt32(MH_MAGIC)
	#define mh_size  sizeof(struct mach_header)
#else
	struct mach_header_64 *mh = buffer;
	if (codeSize < sizeof(struct mach_header_64) || mh->magic != MH_MAGIC_64
	CONST struct mach_header_64 *mh = NULL;
	#define mh_magic OSSwapHostToBigInt32(MH_MAGIC_64)
	#define mh_size  sizeof(struct mach_header_64)
#endif
		|| mh->filetype != MH_BUNDLE) {
	
	if (codeSize >= sizeof(struct fat_header)
		&& fh->magic == OSSwapHostToBigInt32(FAT_MAGIC)) {
	    /*
	     * Fat binary, try to find mach_header for our architecture
	     */
	    uint32_t fh_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch);
	    
	    if (codeSize >= sizeof(struct fat_header) + 
		    fh_nfat_arch * sizeof(struct fat_arch)) {
		void *fatarchs = buffer + sizeof(struct fat_header);
		CONST NXArchInfo *arch = NXGetLocalArchInfo();
		struct fat_arch *fa;
		
		if (fh->magic != FAT_MAGIC) {
		    swap_fat_arch(fatarchs, fh_nfat_arch, arch->byteorder);
		}
		fa = NXFindBestFatArch(arch->cputype, arch->cpusubtype,
			fatarchs, fh_nfat_arch);
		if (fa) {
		    mh = buffer + fa->offset;
		    ms = fa->size;
		} else {
		    err = NSObjectFileImageInappropriateFile;
		}
		if (fh->magic != FAT_MAGIC) {
		    swap_fat_arch(fatarchs, fh_nfat_arch, arch->byteorder);
		}
	    } else {
		err = NSObjectFileImageInappropriateFile;
	    }
	} else {
	    /*
	     * Thin binary
	     */
	    mh = buffer;
	    ms = codeSize;
	}
	if (ms && !(ms >= mh_size && mh->magic == mh_magic &&
		 mh->filetype == OSSwapHostToBigInt32(MH_BUNDLE))) {
	    err = NSObjectFileImageInappropriateFile;
	}
	if (err == NSObjectFileImageSuccess) {
	    err = NSCreateObjectFileImageFromMemory(buffer, codeSize,
		    &dyldObjFileImage);
	}
	objFileImageErrMsg = DyldOFIErrorMsg(err);