summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--tests/load.test17
-rw-r--r--unix/tclLoadDyld.c543
3 files changed, 389 insertions, 178 deletions
diff --git a/ChangeLog b/ChangeLog
index 296f312..4b3114a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2007-08-14 Daniel Steffen <das@users.sourceforge.net>
+
+ * unix/tclLoadDyld.c: use dlfcn API on Mac OS X 10.4 and later; fix
+ issues with loading from memory on intel and 64bit; add debug messages.
+
+ * tests/load.test: add test load-10.1 for loading from vfs.
+
2007-08-07 Daniel Steffen <das@users.sourceforge.net>
* generic/tclEnv.c: improve environ handling on Mac OS X (adapted
diff --git a/tests/load.test b/tests/load.test
index 9fe26ab..3824d67 100644
--- a/tests/load.test
+++ b/tests/load.test
@@ -10,7 +10,7 @@
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
-# RCS: @(#) $Id: load.test,v 1.11.2.1 2004/09/14 17:02:56 das Exp $
+# RCS: @(#) $Id: load.test,v 1.11.2.2 2007/08/14 06:34:13 das Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest 2
@@ -44,7 +44,12 @@ set alreadyTotalLoaded [info loaded]
# Certain tests require the 'teststaticpkg' command from tcltest
::tcltest::testConstraint teststaticpkg \
- [string compare {} [info commands teststaticpkg]]
+ [string compare {} [info commands teststaticpkg]]
+
+# Test load-10.1 requires the 'testsimplefilesystem' command from tcltest
+
+::tcltest::testConstraint testsimplefilesystem \
+ [string compare {} [info commands testsimplefilesystem]]
test load-1.1 {basic errors} {} {
@@ -201,7 +206,13 @@ test load-9.1 {Tcl_StaticPackage, load already-loaded package into another inter
-result {{{{} Loadninepointone} {{} Tcltest}} {{{} Loadninepointone} {{} Tcltest}}} \
-cleanup { interp delete child1 ; interp delete child2 }
-
+test load-10.1 {load from vfs} \
+ -constraints [list $dll $loaded testsimplefilesystem] \
+ -setup {set dir [pwd]; cd $testDir; testsimplefilesystem 1} \
+ -body {list [catch {load simplefs:/pkgd$ext pkgd} msg] $msg} \
+ -result {0 {}} \
+ -cleanup {testsimplefilesystem 0; cd $dir; unset dir}
+
# cleanup
::tcltest::cleanupTests
return
diff --git a/unix/tclLoadDyld.c b/unix/tclLoadDyld.c
index a206019..25bd436 100644
--- a/unix/tclLoadDyld.c
+++ b/unix/tclLoadDyld.c
@@ -3,7 +3,7 @@
*
* This procedure provides a version of the TclLoadFile that works with
* Apple's dyld dynamic loading.
- * Original version of his file (now superseded long ago) provided by
+ * Original version of his file (superseded long ago) provided by
* Wilfredo Sanchez (wsanchez@apple.com).
*
* Copyright (c) 1995 Apple Computer, Inc.
@@ -12,11 +12,51 @@
* 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.9 2007/04/29 02:20:16 das Exp $
+ * RCS: @(#) $Id: tclLoadDyld.c,v 1.14.2.10 2007/08/14 06:34:13 das Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
+
+#ifndef MODULE_SCOPE
+#define MODULE_SCOPE extern
+#endif
+
+#ifndef TCL_DYLD_USE_DLFCN
+/*
+ * Use preferred dlfcn API on 10.4 and later
+ */
+# if !defined(NO_DLFCN_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
+# define TCL_DYLD_USE_DLFCN 1
+# else
+# define TCL_DYLD_USE_DLFCN 0
+# endif
+#endif
+#ifndef TCL_DYLD_USE_NSMODULE
+/*
+ * Use deprecated NSModule API only to support 10.3 and earlier:
+ */
+# if MAC_OS_X_VERSION_MIN_REQUIRED < 1040
+# define TCL_DYLD_USE_NSMODULE 1
+# else
+# define TCL_DYLD_USE_NSMODULE 0
+# endif
+#endif
+
+#if TCL_DYLD_USE_DLFCN
+#include <dlfcn.h>
+#if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1040
+/*
+ * Support for weakly importing dlfcn API.
+ */
+extern void *dlopen(const char *path, int mode) WEAK_IMPORT_ATTRIBUTE;
+extern void *dlsym(void *handle, const char *symbol) WEAK_IMPORT_ATTRIBUTE;
+extern int dlclose(void *handle) WEAK_IMPORT_ATTRIBUTE;
+extern char *dlerror(void) WEAK_IMPORT_ATTRIBUTE;
+#endif
+#endif
+
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
#include <mach-o/dyld.h>
#include <mach-o/fat.h>
#include <mach-o/swap.h>
@@ -25,24 +65,37 @@
#undef panic
#include <mach/mach.h>
-#ifndef MODULE_SCOPE
-#define MODULE_SCOPE extern
-#endif
-
typedef struct Tcl_DyldModuleHandle {
struct Tcl_DyldModuleHandle *nextPtr;
NSModule module;
} Tcl_DyldModuleHandle;
+#endif /* TCL_DYLD_USE_NSMODULE */
typedef struct Tcl_DyldLoadHandle {
- CONST struct mach_header *dyldLibHeader;
+#if TCL_DYLD_USE_DLFCN
+ void *dlHandle;
+#endif
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
+ const struct mach_header *dyldLibHeader;
Tcl_DyldModuleHandle *modulePtr;
+#endif
} Tcl_DyldLoadHandle;
-#ifdef TCL_LOAD_FROM_MEMORY
+#if (TCL_DYLD_USE_DLFCN && MAC_OS_X_VERSION_MIN_REQUIRED < 1040) || \
+ defined(TCL_LOAD_FROM_MEMORY)
MODULE_SCOPE long tclMacOSXDarwinRelease;
#endif
+
+#ifdef TCL_DEBUG_LOAD
+#define TclLoadDbgMsg(m, ...) do { \
+ fprintf(stderr, "%s:%d: %s(): " m ".\n", \
+ strrchr(__FILE__, '/')+1, __LINE__, __func__, ##__VA_ARGS__); \
+ } while (0)
+#else
+#define TclLoadDbgMsg(m, ...)
+#endif
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
/*
*----------------------------------------------------------------------
*
@@ -81,6 +134,7 @@ DyldOFIErrorMsg(
return "unknown error";
}
}
+#endif /* TCL_DYLD_USE_NSMODULE */
/*
*----------------------------------------------------------------------
@@ -105,7 +159,7 @@ TclpDlopen(
Tcl_Interp *interp, /* Used for error reporting. */
Tcl_Obj *pathPtr, /* Name of the file containing the desired
* code (UTF-8). */
- Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded
+ Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded
* file which will be passed back to
* (*unloadProcPtr)() to unload the file. */
Tcl_FSUnloadFileProc **unloadProcPtr)
@@ -114,10 +168,23 @@ TclpDlopen(
* file. */
{
Tcl_DyldLoadHandle *dyldLoadHandle;
- CONST struct mach_header *dyldLibHeader;
- NSObjectFileImage dyldObjFileImage = NULL;
+#if TCL_DYLD_USE_DLFCN
+ void *dlHandle = NULL;
+#endif
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
+ const struct mach_header *dyldLibHeader = NULL;
Tcl_DyldModuleHandle *modulePtr = NULL;
- CONST char *native;
+#endif
+#if TCL_DYLD_USE_NSMODULE
+ NSLinkEditErrors editError;
+ int errorNumber;
+ const char *errorName, *objFileImageErrMsg = NULL;
+#endif
+ const char *errMsg = NULL;
+ int result;
+ Tcl_DString ds;
+ char *fileName = NULL;
+ const char *nativePath, *nativeFileName = NULL;
/*
* First try the full path the user gave us. This is particularly
@@ -125,92 +192,139 @@ TclpDlopen(
* relative path.
*/
- native = Tcl_FSGetNativePath(pathPtr);
- dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
+ nativePath = Tcl_FSGetNativePath(pathPtr);
- if (!dyldLibHeader) {
- NSLinkEditErrors editError;
- int errorNumber;
- CONST char *name, *msg, *objFileImageErrMsg = NULL;
-
- NSLinkEditError(&editError, &errorNumber, &name, &msg);
-
- if (editError == NSLinkEditFileAccessError) {
- /*
- * The requested file was not found. Let the OS loader examine the
- * binary search path for whatever string the user gave us which
- * hopefully refers to a file on the binary path.
- */
-
- Tcl_DString ds;
- char *fileName = Tcl_GetString(pathPtr);
- CONST char *native =
- Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
-
- 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)
- || editError == NSLinkEditOtherError){
+#if TCL_DYLD_USE_DLFCN
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1040
+ if (tclMacOSXDarwinRelease >= 8)
+#endif
+ {
+ dlHandle = dlopen(nativePath, RTLD_NOW | RTLD_LOCAL);
+ if (!dlHandle) {
/*
- * The requested file was found but was not of type MH_DYLIB,
- * attempt to load it as a MH_BUNDLE.
+ * Let the OS loader examine the binary search path for whatever
+ * string the user gave us which hopefully refers to a file on the
+ * binary path.
*/
- NSObjectFileImageReturnCode err =
- NSCreateObjectFileImageFromFile(native, &dyldObjFileImage);
- objFileImageErrMsg = DyldOFIErrorMsg(err);
+ fileName = Tcl_GetString(pathPtr);
+ nativeFileName = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
+ dlHandle = dlopen(nativeFileName, RTLD_NOW | RTLD_LOCAL);
}
-
- if (!dyldLibHeader && !dyldObjFileImage) {
- Tcl_AppendResult(interp, msg, NULL);
- if (msg && *msg) {
- Tcl_AppendResult(interp, "\n", NULL);
- }
- if (objFileImageErrMsg) {
- Tcl_AppendResult(interp,
- "NSCreateObjectFileImageFromFile() error: ",
- objFileImageErrMsg, NULL);
+ if (dlHandle) {
+ TclLoadDbgMsg("dlopen() successful");
+ } else {
+ errMsg = dlerror();
+ TclLoadDbgMsg("dlopen() failed: %s", errMsg);
+ }
+ }
+ if (!dlHandle)
+#endif /* TCL_DYLD_USE_DLFCN */
+ {
+#if TCL_DYLD_USE_NSMODULE
+ dyldLibHeader = NSAddImage(nativePath,
+ NSADDIMAGE_OPTION_RETURN_ON_ERROR);
+ if (dyldLibHeader) {
+ TclLoadDbgMsg("NSAddImage() successful");
+ } else {
+ NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg);
+ if (editError == NSLinkEditFileAccessError) {
+ /*
+ * The requested file was not found. Let the OS loader examine
+ * the binary search path for whatever string the user gave us
+ * which hopefully refers to a file on the binary path.
+ */
+
+ if (!fileName) {
+ fileName = Tcl_GetString(pathPtr);
+ nativeFileName = Tcl_UtfToExternalDString(NULL, fileName,
+ -1, &ds);
+ }
+ dyldLibHeader = NSAddImage(nativeFileName,
+ NSADDIMAGE_OPTION_WITH_SEARCHING |
+ NSADDIMAGE_OPTION_RETURN_ON_ERROR);
+ if (dyldLibHeader) {
+ TclLoadDbgMsg("NSAddImage() successful");
+ } else {
+ NSLinkEditError(&editError, &errorNumber, &errorName,
+ &errMsg);
+ TclLoadDbgMsg("NSAddImage() failed: %s", errMsg);
+ }
+ } else if ((editError == NSLinkEditFileFormatError
+ && errorNumber == EBADMACHO)
+ || editError == NSLinkEditOtherError){
+ NSObjectFileImageReturnCode err;
+ NSObjectFileImage dyldObjFileImage;
+ NSModule module;
+
+ /*
+ * The requested file was found but was not of type MH_DYLIB,
+ * attempt to load it as a MH_BUNDLE.
+ */
+
+ err = NSCreateObjectFileImageFromFile(nativePath,
+ &dyldObjFileImage);
+ if (err == NSObjectFileImageSuccess && dyldObjFileImage) {
+ TclLoadDbgMsg("NSCreateObjectFileImageFromFile() "
+ "successful");
+ module = NSLinkModule(dyldObjFileImage, nativePath,
+ NSLINKMODULE_OPTION_BINDNOW
+ | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
+ NSDestroyObjectFileImage(dyldObjFileImage);
+ if (module) {
+ modulePtr = (Tcl_DyldModuleHandle *)
+ ckalloc(sizeof(Tcl_DyldModuleHandle));
+ modulePtr->module = module;
+ modulePtr->nextPtr = NULL;
+ TclLoadDbgMsg("NSLinkModule() successful");
+ } else {
+ NSLinkEditError(&editError, &errorNumber, &errorName,
+ &errMsg);
+ TclLoadDbgMsg("NSLinkModule() failed: %s", errMsg);
+ }
+ } else {
+ objFileImageErrMsg = DyldOFIErrorMsg(err);
+ TclLoadDbgMsg("NSCreateObjectFileImageFromFile() failed: "
+ "%s", objFileImageErrMsg);
+ }
}
- return TCL_ERROR;
}
+#endif /* TCL_DYLD_USE_NSMODULE */
}
-
- if (dyldObjFileImage) {
- NSModule module;
-
- module = NSLinkModule(dyldObjFileImage, native,
- NSLINKMODULE_OPTION_BINDNOW
- | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
- NSDestroyObjectFileImage(dyldObjFileImage);
-
- if (!module) {
- NSLinkEditErrors editError;
- int errorNumber;
- CONST char *name, *msg;
-
- NSLinkEditError(&editError, &errorNumber, &name, &msg);
- Tcl_AppendResult(interp, msg, NULL);
- return TCL_ERROR;
+ if (0
+#if TCL_DYLD_USE_DLFCN
+ || dlHandle
+#endif
+#if TCL_DYLD_USE_NSMODULE
+ || dyldLibHeader || modulePtr
+#endif
+ ) {
+ dyldLoadHandle = (Tcl_DyldLoadHandle *)
+ ckalloc(sizeof(Tcl_DyldLoadHandle));
+#if TCL_DYLD_USE_DLFCN
+ dyldLoadHandle->dlHandle = dlHandle;
+#endif
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
+ dyldLoadHandle->dyldLibHeader = dyldLibHeader;
+ dyldLoadHandle->modulePtr = modulePtr;
+#endif
+ *loadHandle = (Tcl_LoadHandle) dyldLoadHandle;
+ *unloadProcPtr = &TclpUnloadFile;
+ result = TCL_OK;
+ } else {
+ Tcl_AppendResult(interp, errMsg, NULL);
+#if TCL_DYLD_USE_NSMODULE
+ if (objFileImageErrMsg) {
+ Tcl_AppendResult(interp, "\nNSCreateObjectFileImageFromFile() "
+ "error: ", objFileImageErrMsg, NULL);
}
-
- modulePtr = (Tcl_DyldModuleHandle *)
- ckalloc(sizeof(Tcl_DyldModuleHandle));
- modulePtr->module = module;
- modulePtr->nextPtr = NULL;
+#endif
+ result = TCL_ERROR;
}
-
- dyldLoadHandle = (Tcl_DyldLoadHandle *)
- ckalloc(sizeof(Tcl_DyldLoadHandle));
- dyldLoadHandle->dyldLibHeader = dyldLibHeader;
- dyldLoadHandle->modulePtr = modulePtr;
- *loadHandle = (Tcl_LoadHandle) dyldLoadHandle;
- *unloadProcPtr = &TclpUnloadFile;
- return TCL_OK;
+ if(fileName) {
+ Tcl_DStringFree(&ds);
+ }
+ return result;
}
/*
@@ -235,68 +349,97 @@ TclpFindSymbol(
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_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
-
- /*
- * dyld adds an underscore to the beginning of symbol names.
- */
+ Tcl_PackageInitProc *proc = NULL;
+ const char *errMsg = NULL;
+ Tcl_DString ds;
+ const char *native;
native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
- Tcl_DStringInit(&newName);
- Tcl_DStringAppend(&newName, "_", 1);
- native = Tcl_DStringAppend(&newName, native, -1);
-
- if (dyldLoadHandle->dyldLibHeader) {
- nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader, native,
- NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW |
- NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
- if (nsSymbol) {
- /*
- * Until dyld supports unloading of MY_DYLIB binaries, the
- * following is not needed.
- */
+#if TCL_DYLD_USE_DLFCN
+ if (dyldLoadHandle->dlHandle) {
+ proc = dlsym(dyldLoadHandle->dlHandle, native);
+ if (proc) {
+ TclLoadDbgMsg("dlsym() successful");
+ } else {
+ errMsg = dlerror();
+ TclLoadDbgMsg("dlsym() failed: %s", errMsg);
+ }
+ } else
+#endif /* TCL_DYLD_USE_DLFCN */
+ {
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
+ NSSymbol nsSymbol = NULL;
+ Tcl_DString newName;
-#ifdef DYLD_SUPPORTS_DYLIB_UNLOADING
- NSModule module = NSModuleForSymbol(nsSymbol);
- Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr;
+ /*
+ * dyld adds an underscore to the beginning of symbol names.
+ */
- while (modulePtr != NULL) {
- if (module == modulePtr->module) {
- break;
+ Tcl_DStringInit(&newName);
+ Tcl_DStringAppend(&newName, "_", 1);
+ native = Tcl_DStringAppend(&newName, native, -1);
+ if (dyldLoadHandle->dyldLibHeader) {
+ nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader,
+ native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW |
+ NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
+ if (nsSymbol) {
+ TclLoadDbgMsg("NSLookupSymbolInImage() successful");
+#ifdef DYLD_SUPPORTS_DYLIB_UNLOADING
+ /*
+ * Until dyld supports unloading of MY_DYLIB binaries, the
+ * following is not needed.
+ */
+
+ NSModule module = NSModuleForSymbol(nsSymbol);
+ Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr;
+
+ while (modulePtr != NULL) {
+ if (module == modulePtr->module) {
+ break;
+ }
+ modulePtr = modulePtr->nextPtr;
+ }
+ if (modulePtr == NULL) {
+ modulePtr = (Tcl_DyldModuleHandle *)
+ ckalloc(sizeof(Tcl_DyldModuleHandle));
+ modulePtr->module = module;
+ modulePtr->nextPtr = dyldLoadHandle->modulePtr;
+ dyldLoadHandle->modulePtr = modulePtr;
}
- modulePtr = modulePtr->nextPtr;
- }
- if (modulePtr == NULL) {
- modulePtr = (Tcl_DyldModuleHandle *)
- ckalloc(sizeof(Tcl_DyldModuleHandle));
- modulePtr->module = module;
- modulePtr->nextPtr = dyldLoadHandle->modulePtr;
- dyldLoadHandle->modulePtr = modulePtr;
- }
#endif /* DYLD_SUPPORTS_DYLIB_UNLOADING */
+ } else {
+ NSLinkEditErrors editError;
+ int errorNumber;
+ const char *errorName;
- } else {
- NSLinkEditErrors editError;
- int errorNumber;
- CONST char *name, *msg;
-
- NSLinkEditError(&editError, &errorNumber, &name, &msg);
- Tcl_AppendResult(interp, msg, NULL);
+ NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg);
+ TclLoadDbgMsg("NSLookupSymbolInImage() failed: %s", errMsg);
+ }
+ } else if (dyldLoadHandle->modulePtr) {
+ nsSymbol = NSLookupSymbolInModule(
+ dyldLoadHandle->modulePtr->module, native);
+ if (nsSymbol) {
+ TclLoadDbgMsg("NSLookupSymbolInModule() successful");
+ } else {
+ TclLoadDbgMsg("NSLookupSymbolInModule() failed");
+ }
}
- } else {
- nsSymbol = NSLookupSymbolInModule(dyldLoadHandle->modulePtr->module,
- native);
- }
- if (nsSymbol) {
- proc = NSAddressOfSymbol(nsSymbol);
+ if (nsSymbol) {
+ proc = NSAddressOfSymbol(nsSymbol);
+ if (proc) {
+ TclLoadDbgMsg("NSAddressOfSymbol() successful");
+ } else {
+ TclLoadDbgMsg("NSAddressOfSymbol() failed");
+ }
+ }
+ Tcl_DStringFree(&newName);
+#endif /* TCL_DYLD_USE_NSMODULE */
}
- Tcl_DStringFree(&newName);
Tcl_DStringFree(&ds);
-
+ if (errMsg) {
+ Tcl_AppendResult(interp, errMsg, NULL);
+ }
return proc;
}
@@ -327,16 +470,39 @@ TclpUnloadFile(
* that represents the loaded file. */
{
Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
- Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr;
- while (modulePtr != NULL) {
- void *ptr;
+#if TCL_DYLD_USE_DLFCN
+ if (dyldLoadHandle->dlHandle) {
+ int result;
- NSUnLinkModule(modulePtr->module,
- NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
- ptr = modulePtr;
- modulePtr = modulePtr->nextPtr;
- ckfree(ptr);
+ result = dlclose(dyldLoadHandle->dlHandle);
+ if (!result) {
+ TclLoadDbgMsg("dlclose() successful");
+ } else {
+ TclLoadDbgMsg("dlclose() failed: %s", dlerror());
+ }
+ } else
+#endif /* TCL_DYLD_USE_DLFCN */
+ {
+#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
+ Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr;
+
+ while (modulePtr != NULL) {
+ void *ptr;
+ bool result;
+
+ result = NSUnLinkModule(modulePtr->module,
+ NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
+ if (result) {
+ TclLoadDbgMsg("NSUnLinkModule() successful");
+ } else {
+ TclLoadDbgMsg("NSUnLinkModule() failed");
+ }
+ ptr = modulePtr;
+ modulePtr = modulePtr->nextPtr;
+ ckfree(ptr);
+ }
+#endif /* TCL_DYLD_USE_NSMODULE */
}
ckfree((char*) dyldLoadHandle);
}
@@ -440,7 +606,7 @@ TclpLoadMemory(
int codeSize, /* Size of code data read into buffer or -1 if
* an error occurred and the buffer should
* just be freed. */
- Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded
+ Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded
* file which will be passed back to
* (*unloadProcPtr)() to unload the file. */
Tcl_FSUnloadFileProc **unloadProcPtr)
@@ -452,7 +618,7 @@ TclpLoadMemory(
NSObjectFileImage dyldObjFileImage = NULL;
Tcl_DyldModuleHandle *modulePtr;
NSModule module;
- CONST char *objFileImageErrMsg = NULL;
+ const char *objFileImageErrMsg = NULL;
/*
* Try to create an object file image that we can load from.
@@ -460,64 +626,88 @@ TclpLoadMemory(
if (codeSize >= 0) {
NSObjectFileImageReturnCode err = NSObjectFileImageSuccess;
- CONST struct fat_header *fh = buffer;
+ const struct fat_header *fh = buffer;
uint32_t ms = 0;
#ifndef __LP64__
- CONST struct mach_header *mh = NULL;
- #define mh_magic OSSwapHostToBigInt32(MH_MAGIC)
+ const struct mach_header *mh = NULL;
#define mh_size sizeof(struct mach_header)
+ #define mh_magic MH_MAGIC
+ #define arch_abi 0
#else
- CONST struct mach_header_64 *mh = NULL;
- #define mh_magic OSSwapHostToBigInt32(MH_MAGIC_64)
+ const struct mach_header_64 *mh = NULL;
#define mh_size sizeof(struct mach_header_64)
+ #define mh_magic MH_MAGIC_64
+ #define arch_abi CPU_ARCH_ABI64
#endif
-
+
if ((size_t) codeSize >= sizeof(struct fat_header)
&& fh->magic == OSSwapHostToBigInt32(FAT_MAGIC)) {
+ uint32_t fh_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch);
+
/*
* Fat binary, try to find mach_header for our architecture
*/
- uint32_t fh_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch);
-
- if ((size_t) codeSize >= sizeof(struct fat_header) +
+
+ TclLoadDbgMsg("Fat binary, %d archs", fh_nfat_arch);
+ if ((size_t) codeSize >= sizeof(struct fat_header) +
fh_nfat_arch * sizeof(struct fat_arch)) {
void *fatarchs = (char*)buffer + sizeof(struct fat_header);
- CONST NXArchInfo *arch = NXGetLocalArchInfo();
+ 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);
+ fa = NXFindBestFatArch(arch->cputype | arch_abi,
+ arch->cpusubtype, fatarchs, fh_nfat_arch);
if (fa) {
+ TclLoadDbgMsg("NXFindBestFatArch() successful: "
+ "local cputype %d subtype %d, "
+ "fat cputype %d subtype %d",
+ arch->cputype | arch_abi, arch->cpusubtype,
+ fa->cputype, fa->cpusubtype);
mh = (void*)((char*)buffer + fa->offset);
ms = fa->size;
} else {
+ TclLoadDbgMsg("NXFindBestFatArch() failed");
err = NSObjectFileImageInappropriateFile;
}
if (fh->magic != FAT_MAGIC) {
swap_fat_arch(fatarchs, fh_nfat_arch, arch->byteorder);
}
} else {
+ TclLoadDbgMsg("Fat binary header failure");
err = NSObjectFileImageInappropriateFile;
}
} else {
/*
* Thin binary
*/
+
+ TclLoadDbgMsg("Thin binary");
mh = buffer;
ms = codeSize;
}
if (ms && !(ms >= mh_size && mh->magic == mh_magic &&
- mh->filetype == OSSwapHostToBigInt32(MH_BUNDLE))) {
+ mh->filetype == MH_BUNDLE)) {
+ TclLoadDbgMsg("Inappropriate file: magic %x filetype %d",
+ mh->magic, mh->filetype);
err = NSObjectFileImageInappropriateFile;
}
if (err == NSObjectFileImageSuccess) {
err = NSCreateObjectFileImageFromMemory(buffer, codeSize,
&dyldObjFileImage);
+ if (err == NSObjectFileImageSuccess) {
+ TclLoadDbgMsg("NSCreateObjectFileImageFromMemory() "
+ "successful");
+ } else {
+ objFileImageErrMsg = DyldOFIErrorMsg(err);
+ TclLoadDbgMsg("NSCreateObjectFileImageFromMemory() failed: %s",
+ objFileImageErrMsg);
+ }
+ } else {
+ objFileImageErrMsg = DyldOFIErrorMsg(err);
}
- objFileImageErrMsg = DyldOFIErrorMsg(err);
}
/*
@@ -528,9 +718,8 @@ TclpLoadMemory(
if (dyldObjFileImage == NULL) {
vm_deallocate(mach_task_self(), (vm_address_t) buffer, size);
if (objFileImageErrMsg != NULL) {
- Tcl_AppendResult(interp,
- "NSCreateObjectFileImageFromMemory() error: ",
- objFileImageErrMsg, NULL);
+ Tcl_AppendResult(interp, "NSCreateObjectFileImageFromMemory() "
+ "error: ", objFileImageErrMsg, NULL);
}
return TCL_ERROR;
}
@@ -542,14 +731,16 @@ TclpLoadMemory(
module = NSLinkModule(dyldObjFileImage, "[Memory Based Bundle]",
NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
NSDestroyObjectFileImage(dyldObjFileImage);
-
- if (!module) {
+ if (module) {
+ TclLoadDbgMsg("NSLinkModule() successful");
+ } else {
NSLinkEditErrors editError;
int errorNumber;
- CONST char *name, *msg;
+ const char *errorName, *errMsg;
- NSLinkEditError(&editError, &errorNumber, &name, &msg);
- Tcl_AppendResult(interp, msg, NULL);
+ NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg);
+ TclLoadDbgMsg("NSLinkModule() failed: %s", errMsg);
+ Tcl_AppendResult(interp, errMsg, NULL);
return TCL_ERROR;
}
@@ -560,21 +751,23 @@ TclpLoadMemory(
modulePtr = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle));
modulePtr->module = module;
modulePtr->nextPtr = NULL;
-
dyldLoadHandle = (Tcl_DyldLoadHandle *)
ckalloc(sizeof(Tcl_DyldLoadHandle));
+#if TCL_DYLD_USE_DLFCN
+ dyldLoadHandle->dlHandle = NULL;
+#endif
dyldLoadHandle->dyldLibHeader = NULL;
dyldLoadHandle->modulePtr = modulePtr;
*loadHandle = (Tcl_LoadHandle) dyldLoadHandle;
*unloadProcPtr = &TclpUnloadFile;
return TCL_OK;
}
-#endif
+#endif /* TCL_LOAD_FROM_MEMORY */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
- * fill-column: 78
+ * fill-column: 79
* End:
*/
ass="hl esc">\\x00\\xde\\xf0\fR. .RE .IP \fBc\fR 5 Stores one or more 8-bit integer values in the output string. If no \fIcount\fR is specified, then \fIarg\fR must consist of an integer value; otherwise \fIarg\fR must consist of a list containing at least \fIcount\fR integer elements. The low-order 8 bits of each integer are stored as a one-byte value at the cursor position. If \fIcount\fR is \fB*\fR, then all of the integers in the list are formatted. If the number of elements in the list is fewer than \fIcount\fR, then an error is generated. If the number of elements in the list is greater than \fIcount\fR, then the extra elements are ignored. For example, .RS .CS \fBbinary format\fR c3cc* {3 -3 128 1} 260 {2 5} .CE will return a string equivalent to \fB\\x03\\xfd\\x80\\x04\\x02\\x05\fR, whereas .CS \fBbinary format\fR c {2 5} .CE will generate an error. .RE .IP \fBs\fR 5 This form is the same as \fBc\fR except that it stores one or more 16-bit integers in little-endian byte order in the output string. The low-order 16-bits of each integer are stored as a two-byte value at the cursor position with the least significant byte stored first. For example, .RS .CS \fBbinary format\fR s3 {3 -3 258 1} .CE will return a string equivalent to \fB\\x03\\x00\\xfd\\xff\\x02\\x01\fR. .RE .IP \fBS\fR 5 This form is the same as \fBs\fR except that it stores one or more 16-bit integers in big-endian byte order in the output string. For example, .RS .CS \fBbinary format\fR S3 {3 -3 258 1} .CE will return a string equivalent to \fB\\x00\\x03\\xff\\xfd\\x01\\x02\fR. .RE .IP \fBt\fR 5 .VS 8.5 This form (mnemonically \fItiny\fR) is the same as \fBs\fR and \fBS\fR except that it stores the 16-bit integers in the output string in the native byte order of the machine where the Tcl script is running. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBi\fR 5 This form is the same as \fBc\fR except that it stores one or more 32-bit integers in little-endian byte order in the output string. The low-order 32-bits of each integer are stored as a four-byte value at the cursor position with the least significant byte stored first. For example, .RS .CS \fBbinary format\fR i3 {3 -3 65536 1} .CE will return a string equivalent to \fB\\x03\\x00\\x00\\x00\\xfd\\xff\\xff\\xff\\x00\\x00\\x01\\x00\fR .RE .IP \fBI\fR 5 This form is the same as \fBi\fR except that it stores one or more one or more 32-bit integers in big-endian byte order in the output string. For example, .RS .CS \fBbinary format\fR I3 {3 -3 65536 1} .CE will return a string equivalent to \fB\\x00\\x00\\x00\\x03\\xff\\xff\\xff\\xfd\\x00\\x01\\x00\\x00\fR .RE .IP \fBn\fR 5 .VS 8.5 This form (mnemonically \fInumber\fR or \fInormal\fR) is the same as \fBi\fR and \fBI\fR except that it stores the 32-bit integers in the output string in the native byte order of the machine where the Tcl script is running. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBw\fR 5 This form is the same as \fBc\fR except that it stores one or more 64-bit integers in little-endian byte order in the output string. The low-order 64-bits of each integer are stored as an eight-byte value at the cursor position with the least significant byte stored first. For example, .RS .CS \fBbinary format\fR w 7810179016327718216 .CE will return the string \fBHelloTcl\fR .RE .IP \fBW\fR 5 This form is the same as \fBw\fR except that it stores one or more one or more 64-bit integers in big-endian byte order in the output string. For example, .RS .CS \fBbinary format\fR Wc 4785469626960341345 110 .CE will return the string \fBBigEndian\fR .RE .IP \fBm\fR 5 .VS 8.5 This form (mnemonically the mirror of \fBw\fR) is the same as \fBw\fR and \fBW\fR except that it stores the 64-bit integers in the output string in the native byte order of the machine where the Tcl script is running. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBf\fR 5 This form is the same as \fBc\fR except that it stores one or more one or more single-precision floating point numbers in the machine's native representation in the output string. This representation is not portable across architectures, so it should not be used to communicate floating point numbers across the network. The size of a floating point number may vary across architectures, so the number of bytes that are generated may vary. If the value overflows the machine's native representation, then the value of FLT_MAX as defined by the system will be used instead. Because Tcl uses double-precision floating point numbers internally, there may be some loss of precision in the conversion to single-precision. For example, on a Windows system running on an Intel Pentium processor, .RS .CS \fBbinary format\fR f2 {1.6 3.4} .CE will return a string equivalent to \fB\\xcd\\xcc\\xcc\\x3f\\x9a\\x99\\x59\\x40\fR. .RE .IP \fBr\fR 5 .VS 8.5 This form (mnemonically \fIreal\fR) is the same as \fBf\fR except that it stores the single-precision floating point numbers in little-endian order. This conversion only produces meaningful output when used on machines which use the IEEE floating point representation (very common, but not universal.) .VE 8.5 .IP \fBR\fR 5 .VS 8.5 This form is the same as \fBr\fR except that it stores the single-precision floating point numbers in big-endian order. .VE 8.5 .IP \fBd\fR 5 This form is the same as \fBf\fR except that it stores one or more one or more double-precision floating point numbers in the machine's native representation in the output string. For example, on a Windows system running on an Intel Pentium processor, .RS .CS \fBbinary format\fR d1 {1.6} .CE will return a string equivalent to \fB\\x9a\\x99\\x99\\x99\\x99\\x99\\xf9\\x3f\fR. .RE .IP \fBq\fR 5 .VS 8.5 This form (mnemonically the mirror of \fBd\fR) is the same as \fBd\fR except that it stores the double-precision floating point numbers in little-endian order. This conversion only produces meaningful output when used on machines which use the IEEE floating point representation (very common, but not universal.) .VE 8.5 .IP \fBQ\fR 5 .VS 8.5 This form is the same as \fBq\fR except that it stores the double-precision floating point numbers in big-endian order. .VE 8.5 .IP \fBx\fR 5 Stores \fIcount\fR null bytes in the output string. If \fIcount\fR is not specified, stores one null byte. If \fIcount\fR is \fB*\fR, generates an error. This type does not consume an argument. For example, .RS .CS \fBbinary format\fR a3xa3x2a3 abc def ghi .CE will return a string equivalent to \fBabc\\000def\\000\\000ghi\fR. .RE .IP \fBX\fR 5 Moves the cursor back \fIcount\fR bytes in the output string. If \fIcount\fR is \fB*\fR or is larger than the current cursor position, then the cursor is positioned at location 0 so that the next byte stored will be the first byte in the result string. If \fIcount\fR is omitted then the cursor is moved back one byte. This type does not consume an argument. For example, .RS .CS \fBbinary format\fR a3X*a3X2a3 abc def ghi .CE will return \fBdghi\fR. .RE .IP \fB@\fR 5 Moves the cursor to the absolute location in the output string specified by \fIcount\fR. Position 0 refers to the first byte in the output string. If \fIcount\fR refers to a position beyond the last byte stored so far, then null bytes will be placed in the uninitialized locations and the cursor will be placed at the specified location. If \fIcount\fR is \fB*\fR, then the cursor is moved to the current end of the output string. If \fIcount\fR is omitted, then an error will be generated. This type does not consume an argument. For example, .RS .CS \fBbinary format\fR a5@2a1@*a3@10a1 abcde f ghi j .CE will return \fBabfdeghi\\000\\000j\fR. .RE .SH "BINARY SCAN" .PP The \fBbinary scan\fR command parses fields from a binary string, returning the number of conversions performed. \fIString\fR gives the input bytes to be parsed (one byte per character, and characters not representable as a byte have their high bits chopped) and \fIformatString\fR indicates how to parse it. Each \fIvarName\fR gives the name of a variable; when a field is scanned from \fIstring\fR the result is assigned to the corresponding variable. .PP As with \fBbinary format\fR, the \fIformatString\fR consists of a sequence of zero or more field specifiers separated by zero or more spaces. Each field specifier is a single type character followed by an optional flag character followed by an optional numeric \fIcount\fR. Most field specifiers consume one argument to obtain the variable into which the scanned values should be placed. The type character specifies how the binary data is to be interpreted. The \fIcount\fR typically indicates how many items of the specified type are taken from the data. If present, the \fIcount\fR is a non-negative decimal integer or \fB*\fR, which normally indicates that all of the remaining items in the data are to be used. If there are not enough bytes left after the current cursor position to satisfy the current field specifier, then the corresponding variable is left untouched and \fBbinary scan\fR returns immediately with the number of variables that were set. If there are not enough arguments for all of the fields in the format string that consume arguments, then an error is generated. The flag character 'u' may be given to cause some types to be read as unsigned values. The flag is accepted for all field types but is ignored for non-integer fields. .PP A similar example as with \fBbinary format\fR should explain the relation between field specifiers and arguments in case of the binary scan subcommand: .CS \fBbinary scan\fR $bytes s3s first second .CE .PP This command (provided the binary string in the variable \fIbytes\fR is long enough) assigns a list of three integers to the variable \fIfirst\fR and assigns a single value to the variable \fIsecond\fR. If \fIbytes\fR contains fewer than 8 bytes (i.e. four 2-byte integers), no assignment to \fIsecond\fR will be made, and if \fIbytes\fR contains fewer than 6 bytes (i.e. three 2-byte integers), no assignment to \fIfirst\fR will be made. Hence: .CS puts [\fBbinary scan\fR abcdefg s3s first second] puts $first puts $second .CE will print (assuming neither variable is set previously): .CS 1 25185 25699 26213 can't read "second": no such variable .CE .PP It is \fIimportant\fR to note that the \fBc\fR, \fBs\fR, and \fBS\fR (and \fBi\fR and \fBI\fR on 64bit systems) will be scanned into long data size values. In doing this, values that have their high bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints), will be sign extended. Thus the following will occur: .CS set signShort [\fBbinary format\fR s1 0x8000] \fBbinary scan\fR $signShort s1 val; \fI# val == 0xFFFF8000\fR .CE If you require unsigned values you can include the 'u' flag character following the field type. For example, to read an unsigned short value: .CS set signShort [\fBbinary format\fR s1 0x8000] \fBbinary scan\fR $signShort su1 val; \fI# val == 0x00008000\fR .CE .PP Each type-count pair moves an imaginary cursor through the binary data, reading bytes from the current position. The cursor is initially at position 0 at the beginning of the data. The type may be any one of the following characters: .IP \fBa\fR 5 The data is a byte string of length \fIcount\fR. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned into the variable. If \fIcount\fR is omitted, then one byte will be scanned. All bytes scanned will be interpreted as being characters in the range \\u0000-\\u00ff so the \fBencoding convertfrom\fR command might be needed if the string is not an ISO 8859\-1 string. For example, .RS .CS \fBbinary scan\fR abcde\\000fghi a6a10 var1 var2 .CE will return \fB1\fR with the string equivalent to \fBabcde\\000\fR stored in \fIvar1\fR and \fIvar2\fR left unmodified. .RE .IP \fBA\fR 5 This form is the same as \fBa\fR, except trailing blanks and nulls are stripped from the scanned value before it is stored in the variable. For example, .RS .CS \fBbinary scan\fR "abc efghi \\000" A* var1 .CE will return \fB1\fR with \fBabc efghi\fR stored in \fIvar1\fR. .RE .IP \fBb\fR 5 The data is turned into a string of \fIcount\fR binary digits in low-to-high order represented as a sequence of ``1'' and ``0'' characters. The data bytes are scanned in first to last order with the bits being taken in low-to-high order within each byte. Any extra bits in the last byte are ignored. If \fIcount\fR is \fB*\fR, then all of the remaining bits in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one bit will be scanned. For example, .RS .CS \fBbinary scan\fR \\x07\\x87\\x05 b5b* var1 var2 .CE will return \fB2\fR with \fB11100\fR stored in \fIvar1\fR and \fB1110000110100000\fR stored in \fIvar2\fR. .RE .IP \fBB\fR 5 This form is the same as \fBb\fR, except the bits are taken in high-to-low order within each byte. For example, .RS .CS \fBbinary scan\fR \\x70\\x87\\x05 B5B* var1 var2 .CE will return \fB2\fR with \fB01110\fR stored in \fIvar1\fR and \fB1000011100000101\fR stored in \fIvar2\fR. .RE .IP \fBH\fR 5 The data is turned into a string of \fIcount\fR hexadecimal digits in high-to-low order represented as a sequence of characters in the set ``0123456789abcdef''. The data bytes are scanned in first to last order with the hex digits being taken in high-to-low order within each byte. Any extra bits in the last byte are ignored. If \fIcount\fR is \fB*\fR, then all of the remaining hex digits in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one hex digit will be scanned. For example, .RS .CS \fBbinary scan\fR \\x07\\x86\\x05\\x12\\x34 H3H* var1 var2 .CE will return \fB2\fR with \fB078\fR stored in \fIvar1\fR and \fB051234\fR stored in \fIvar2\fR. .RE .IP \fBh\fR 5 This form is the same as \fBH\fR, except the digits are taken in reverse (low-to-high) order within each byte. For example, .RS .CS \fBbinary scan\fR \\x07\\x86\\x05\\x12\\x34 h3h* var1 var2 .CE will return \fB2\fR with \fB706\fR stored in \fIvar1\fR and \fB502143\fR stored in \fIvar2\fR. .RE Note that most code that wishes to parse the hexadecimal digits from multiple bytes in order should use the \fBH\fR format. .IP \fBc\fR 5 The data is turned into \fIcount\fR 8-bit signed integers and stored in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one 8-bit integer will be scanned. For example, .RS .CS \fBbinary scan\fR \\x07\\x86\\x05 c2c* var1 var2 .CE will return \fB2\fR with \fB7 -122\fR stored in \fIvar1\fR and \fB5\fR stored in \fIvar2\fR. Note that the integers returned are signed, but they can be converted to unsigned 8-bit quantities using an expression like: .CS set num [expr { $num & 0xff }] .CE .RE .IP \fBs\fR 5 The data is interpreted as \fIcount\fR 16-bit signed integers represented in little-endian byte order. The integers are stored in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one 16-bit integer will be scanned. For example, .RS .CS \fBbinary scan\fR \\x05\\x00\\x07\\x00\\xf0\\xff s2s* var1 var2 .CE will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. Note that the integers returned are signed, but they can be converted to unsigned 16-bit quantities using an expression like: .CS set num [expr { $num & 0xffff }] .CE .RE .IP \fBS\fR 5 This form is the same as \fBs\fR except that the data is interpreted as \fIcount\fR 16-bit signed integers represented in big-endian byte order. For example, .RS .CS \fBbinary scan\fR \\x00\\x05\\x00\\x07\\xff\\xf0 S2S* var1 var2 .CE will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. .RE .IP \fBt\fR 5 .VS 8.5 The data is interpreted as \fIcount\fR 16-bit signed integers represented in the native byte order of the machine running the Tcl script. It is otherwise identical to \fBs\fR and \fBS\fR. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBi\fR 5 The data is interpreted as \fIcount\fR 32-bit signed integers represented in little-endian byte order. The integers are stored in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one 32-bit integer will be scanned. For example, .RS .CS set str \\x05\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\xf0\\xff\\xff\\xff \fBbinary scan\fR $str i2i* var1 var2 .CE will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. Note that the integers returned are signed, but they can be converted to unsigned 32-bit quantities using an expression like: .CS set num [expr { $num & 0xffffffff }] .CE .RE .IP \fBI\fR 5 This form is the same as \fBI\fR except that the data is interpreted as \fIcount\fR 32-bit signed integers represented in big-endian byte order. For example, .RS .CS set str \\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x07\\xff\\xff\\xff\\xf0 \fBbinary scan\fR $str I2I* var1 var2 .CE will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. .RE .IP \fBn\fR 5 .VS 8.5 The data is interpreted as \fIcount\fR 32-bit signed integers represented in the native byte order of the machine running the Tcl script. It is otherwise identical to \fBi\fR and \fBI\fR. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBw\fR 5 The data is interpreted as \fIcount\fR 64-bit signed integers represented in little-endian byte order. The integers are stored in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one 64-bit integer will be scanned. For example, .RS .CS set str \\x05\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\xf0\\xff\\xff\\xff \fBbinary scan\fR $str wi* var1 var2 .CE will return \fB2\fR with \fB30064771077\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. Note that the integers returned are signed and cannot be represented by Tcl as unsigned values. .RE .IP \fBW\fR 5 This form is the same as \fBw\fR except that the data is interpreted as \fIcount\fR 64-bit signed integers represented in big-endian byte order. For example, .RS .CS set str \\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x07\\xff\\xff\\xff\\xf0 \fBbinary scan\fR $str WI* var1 var2 .CE will return \fB2\fR with \fB21474836487\fR stored in \fIvar1\fR and \fB-16\fR stored in \fIvar2\fR. .RE .IP \fBm\fR 5 .VS 8.5 The data is interpreted as \fIcount\fR 64-bit signed integers represented in the native byte order of the machine running the Tcl script. It is otherwise identical to \fBw\fR and \fBW\fR. To determine what the native byte order of the machine is, refer to the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. .VE 8.5 .IP \fBf\fR 5 The data is interpreted as \fIcount\fR single-precision floating point numbers in the machine's native representation. The floating point numbers are stored in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be scanned. If \fIcount\fR is omitted, then one single-precision floating point number will be scanned. The size of a floating point number may vary across architectures, so the number of bytes that are scanned may vary. If the data does not represent a valid floating point number, the resulting value is undefined and compiler dependent. For example, on a Windows system running on an Intel Pentium processor, .RS .CS \fBbinary scan\fR \\x3f\\xcc\\xcc\\xcd f var1 .CE will return \fB1\fR with \fB1.6000000238418579\fR stored in \fIvar1\fR. .RE .IP \fBr\fR 5 .VS 8.5 This form is the same as \fBf\fR except that the data is interpreted as \fIcount\fR single-precision floating point number in little-endian order. This conversion is not portable to systems not using IEEE floating point representations. .VE 8.5 .IP \fBR\fR 5 .VS 8.5 This form is the same as \fBf\fR except that the data is interpreted as \fIcount\fR single-precision floating point number in big-endian order. This conversion is not portable to systems not using IEEE floating point representations. .VE 8.5 .IP \fBd\fR 5 This form is the same as \fBf\fR except that the data is interpreted as \fIcount\fR double-precision floating point numbers in the machine's native representation. For example, on a Windows system running on an Intel Pentium processor, .RS .CS \fBbinary scan\fR \\x9a\\x99\\x99\\x99\\x99\\x99\\xf9\\x3f d var1 .CE will return \fB1\fR with \fB1.6000000000000001\fR stored in \fIvar1\fR. .RE .IP \fBq\fR 5 .VS 8.5 This form is the same as \fBd\fR except that the data is interpreted as \fIcount\fR double-precision floating point number in little-endian order. This conversion is not portable to systems not using IEEE floating point representations. .VE 8.5 .IP \fBQ\fR 5 .VS 8.5 This form is the same as \fBd\fR except that the data is interpreted as \fIcount\fR double-precision floating point number in big-endian order. This conversion is not portable to systems not using IEEE floating point representations. .VE 8.5 .IP \fBx\fR 5 Moves the cursor forward \fIcount\fR bytes in \fIstring\fR. If \fIcount\fR is \fB*\fR or is larger than the number of bytes after the current cursor position, then the cursor is positioned after the last byte in \fIstring\fR. If \fIcount\fR is omitted, then the cursor is moved forward one byte. Note that this type does not consume an argument. For example, .RS .CS \fBbinary scan\fR \\x01\\x02\\x03\\x04 x2H* var1 .CE will return \fB1\fR with \fB0304\fR stored in \fIvar1\fR. .RE .IP \fBX\fR 5 Moves the cursor back \fIcount\fR bytes in \fIstring\fR. If \fIcount\fR is \fB*\fR or is larger than the current cursor position, then the cursor is positioned at location 0 so that the next byte scanned will be the first byte in \fIstring\fR. If \fIcount\fR is omitted then the cursor is moved back one byte. Note that this type does not consume an argument. For example, .RS .CS \fBbinary scan\fR \\x01\\x02\\x03\\x04 c2XH* var1 var2 .CE will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR stored in \fIvar2\fR. .RE .IP \fB@\fR 5 Moves the cursor to the absolute location in the data string specified by \fIcount\fR. Note that position 0 refers to the first byte in \fIstring\fR. If \fIcount\fR refers to a position beyond the end of \fIstring\fR, then the cursor is positioned after the last byte. If \fIcount\fR is omitted, then an error will be generated. For example, .RS .CS \fBbinary scan\fR \\x01\\x02\\x03\\x04 c2@1H* var1 var2 .CE will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR stored in \fIvar2\fR. .RE .SH "PORTABILITY ISSUES" The \fBr\fR, \fBR\fR, \fBq\fR and \fBQ\fR conversions will only work reliably for transferring data between computers which are all using IEEE floating point representations. This is very common, but not universal. To transfer floating-point numbers portably between all architectures, use their textual representation (as produced by \fBformat\fR) instead. .SH EXAMPLES This is a procedure to write a Tcl string to a binary-encoded channel as UTF-8 data preceded by a length word: .CS proc \fIwriteString\fR {channel string} { set data [encoding convertto utf-8 $string] puts -nonewline [\fBbinary format\fR Ia* \e [string length $data] $data] } .CE .PP This procedure reads a string from a channel that was written by the previously presented \fIwriteString\fR procedure: .CS proc \fIreadString\fR {channel} { if {![\fBbinary scan\fR [read $channel 4] I length]} { error "missing length" } set data [read $channel $length] return [encoding convertfrom utf-8 $data] } .CE .SH "SEE ALSO" format(n), scan(n), tclvars(n) .SH KEYWORDS binary, format, scan