On Mon, Feb 13, 2012 at 04:38, yoram bar haim <yora...@zend.com> wrote:
> Here is a simple test program that reproduce the issue on mac:
[snip]
> shell> gcc -o lib.so -shared lib.c
> shell> gcc -o main main.c lib.so
> shell> ./main

This example is flawed.

The manpage for dlclose(3) on Darwin explicitly states that a library
linked by the executable will never be unloaded. You link directly to
the library here. Also, you use prototypes of the library's functions
rather than calling dlsym(3), forcing you to link to the library,
defeating the entire point of loading it with dlopen(3) in the first
place. I've included a test which does not show the issue at the
bottom of this message.

(As a side note of interest, OS X has historically always had issues
with unloading images from the address space; for example, even in
Lion, if you unload an image containing Objective-C symbols, you stand
a pretty good chance of crashing. Image unloading simply didn't exist
at all in any useful form before Leopard.)

-- Gwynne Raskind

/* dylib.c - clang dylib.c -o dylib.dylib -dynamiclib */
static int      var = 0;
extern int getVar(void);
extern void setVar(int value);
int     getVar(void) { return var; }
void    setVar(int value) { var = value; }

/* loader.c - clang loader.c -o loader */
#include <stdio.h>
#include <dlfcn.h>
int    main(int argc, char **argv)
{
        void    *libh = dlopen("dylib.dylib", 0);
        int     (*getfunc)() = dlsym(libh, "getVar");
        void    (*setfunc)(int) = dlsym(libh, "setVar");

        printf("First load value: %d\n", getfunc());
        setfunc(5);
        printf("First load set value: %d\n", getfunc());
        dlclose(libh);
        libh = dlopen("dylib.dylib", 0);
        getfunc = dlsym(libh, "getVar");
        setfunc = dlsym(libh, "setVar");
        printf("Second load value: %d\n", getfunc());
        return 0;
}

/* output */
$ uname -v ; clang --version ; ./loader
Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012;
root:xnu-1699.24.23~1/RELEASE_X86_64
clang version 3.0 (tags/RELEASE_30/final)
First load value: 0
First load set value: 5
Second load value: 0

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to