Back to index

python3.2  3.2.2
Defines | Functions | Variables
dlfcn_simple.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <limits.h>
#include <mach-o/dyld.h>
#include <AvailabilityMacros.h>
#include "dlfcn.h"

Go to the source code of this file.

Defines

#define ERR_STR_LEN   256
#define MAC_OS_X_VERSION_10_3   1030
#define dlopen   darwin_dlopen
#define dlsym   darwin_dlsym
#define dlerror   darwin_dlerror
#define dlclose   darwin_dlclose
#define dladdr   darwin_dladdr

Functions

static voiddlsymIntern (void *handle, const char *symbol)
static const char * error (int setget, const char *str,...)
static voiddarwin_dlopen (const char *path, int mode)
static const char * darwin_dlerror (void)
static int darwin_dlclose (void *handle)
static voiddarwin_dlsym (void *handle, const char *symbol)
static int darwin_dladdr (const void *handle, Dl_info *info)
void ctypes_dlfcn_init (void)

Variables

void *(* ctypes_dlopen )(const char *path, int mode)
void *(* ctypes_dlsym )(void *handle, const char *symbol)
const char *(* ctypes_dlerror )(void)
int(* ctypes_dlclose )(void *handle)
int(* ctypes_dladdr )(const void *, Dl_info *)

Define Documentation

#define dladdr   darwin_dladdr

Definition at line 65 of file dlfcn_simple.c.

#define dlclose   darwin_dlclose

Definition at line 64 of file dlfcn_simple.c.

#define dlerror   darwin_dlerror

Definition at line 63 of file dlfcn_simple.c.

#define dlopen   darwin_dlopen

Definition at line 61 of file dlfcn_simple.c.

#define dlsym   darwin_dlsym

Definition at line 62 of file dlfcn_simple.c.

#define ERR_STR_LEN   256

Definition at line 45 of file dlfcn_simple.c.

#define MAC_OS_X_VERSION_10_3   1030

Definition at line 48 of file dlfcn_simple.c.


Function Documentation

Definition at line 254 of file dlfcn_simple.c.

                             {
    if (dlopen != NULL) {
        ctypes_dlsym = dlsym;
        ctypes_dlopen = dlopen;
        ctypes_dlerror = dlerror;
        ctypes_dlclose = dlclose;
        ctypes_dladdr = dladdr;
    } else {
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3
        ctypes_dlsym = darwin_dlsym;
        ctypes_dlopen = darwin_dlopen;
        ctypes_dlerror = darwin_dlerror;
        ctypes_dlclose = darwin_dlclose;
        ctypes_dladdr = darwin_dladdr;
#endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */
    }
}

Here is the call graph for this function:

static int darwin_dladdr ( const void handle,
Dl_info info 
) [static]

Definition at line 243 of file dlfcn_simple.c.

                                                            {
    return 0;
}

Here is the caller graph for this function:

static int darwin_dlclose ( void handle) [static]

Definition at line 196 of file dlfcn_simple.c.

{
    if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
        (((struct mach_header *)handle)->magic == MH_CIGAM))
    {
        error(0, "Can't remove dynamic libraries on darwin");
        return 0;
    }
    if (!NSUnLinkModule(handle, 0))
    {
        error(0, "unable to unlink module %s", NSNameOfModule(handle));
        return 1;
    }
    return 0;
}

Here is the caller graph for this function:

static const char* darwin_dlerror ( void  ) [static]

Definition at line 191 of file dlfcn_simple.c.

{
    return error(1, (char *)NULL);
}

Here is the caller graph for this function:

static void* darwin_dlopen ( const char *  path,
int  mode 
) [static]

Definition at line 109 of file dlfcn_simple.c.

{
    void *module = 0;
    NSObjectFileImage ofi = 0;
    NSObjectFileImageReturnCode ofirc;

    /* If we got no path, the app wants the global namespace, use -1 as the marker
       in this case */
    if (!path)
        return (void *)-1;

    /* Create the object file image, works for things linked with the -bundle arg to ld */
    ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
    switch (ofirc)
    {
        case NSObjectFileImageSuccess:
            /* It was okay, so use NSLinkModule to link in the image */
            module = NSLinkModule(ofi, path,
                                                      NSLINKMODULE_OPTION_RETURN_ON_ERROR
                                                      | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE
                                                      | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
            NSDestroyObjectFileImage(ofi);
            break;
        case NSObjectFileImageInappropriateFile:
            /* It may have been a dynamic library rather than a bundle, try to load it */
            module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
            break;
        default:
            /* God knows what we got */
            error(0, "Can not open \"%s\"", path);
            return 0;
    }
    if (!module)
        error(0, "Can not open \"%s\"", path);
    return module;

}

Here is the caller graph for this function:

static void* darwin_dlsym ( void handle,
const char *  symbol 
) [static]

Definition at line 214 of file dlfcn_simple.c.

{
    static char undersym[257];          /* Saves calls to malloc(3) */
    int sym_len = strlen(symbol);
    void *value = NULL;
    char *malloc_sym = NULL;

    if (sym_len < 256)
    {
        snprintf(undersym, 256, "_%s", symbol);
        value = dlsymIntern(handle, undersym);
    }
    else
    {
        malloc_sym = malloc(sym_len + 2);
        if (malloc_sym)
        {
            sprintf(malloc_sym, "_%s", symbol);
            value = dlsymIntern(handle, malloc_sym);
            free(malloc_sym);
        }
        else
        {
            error(0, "Unable to allocate memory");
        }
    }
    return value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void * dlsymIntern ( void handle,
const char *  symbol 
) [static]

Definition at line 148 of file dlfcn_simple.c.

{
    NSSymbol nssym = 0;
    /* If the handle is -1, if is the app global context */
    if (handle == (void *)-1)
    {
        /* Global context, use NSLookupAndBindSymbol */
        if (NSIsSymbolNameDefined(symbol))
        {
            nssym = NSLookupAndBindSymbol(symbol);
        }

    }
    /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
       for libraries, and NSLookupSymbolInModule for bundles */
    else
    {
        /* Check for both possible magic numbers depending on x86/ppc byte order */
        if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
            (((struct mach_header *)handle)->magic == MH_CIGAM))
        {
            if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
            {
                nssym = NSLookupSymbolInImage((struct mach_header *)handle,
                                                                          symbol,
                                                                          NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
                                                                          | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
            }

        }
        else
        {
            nssym = NSLookupSymbolInModule(handle, symbol);
        }
    }
    if (!nssym)
    {
        error(0, "Symbol \"%s\" Not found", symbol);
        return NULL;
    }
    return NSAddressOfSymbol(nssym);
}

Here is the caller graph for this function:

static const char* error ( int  setget,
const char *  str,
  ... 
) [static]

Variable Documentation

Definition at line 72 of file dlfcn_simple.c.

int(* ctypes_dlclose)(void *handle)

Definition at line 71 of file dlfcn_simple.c.

Definition at line 70 of file dlfcn_simple.c.

void*(* ctypes_dlopen)(const char *path, int mode)

Definition at line 68 of file dlfcn_simple.c.

void*(* ctypes_dlsym)(void *handle, const char *symbol)

Definition at line 69 of file dlfcn_simple.c.