[variable-search] Correctly implement on OS X.

On Mac OS X, we just want to pass RTLD_DEFAULT to dlsym() rather
than a handle from dlopen() (which doesn't work).
[variable-search] Reorganize to share more code.
[variable-search] Add tests.
Merge pull request #784 from waywardmonkeys/variable-search-improvements

Variable search improvements