#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <mach-o/dyld_priv.h>
#include "test_support.h"
int mainSymbol = 4;
void free(void* p) { }
static bool symbolInImage(const char* symName, const char* image)
{
void* sym = dlsym(RTLD_DEFAULT, symName);
if ( sym == NULL )
return false;
const char* imagePath = dyld_image_path_containing_address(sym);
if ( imagePath == NULL )
return false;
return (strstr(imagePath, image) != NULL);
}
int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
if ( !symbolInImage("mainSymbol", "dlsym-RTLD_DEFAULT") ) {
FAIL("mainSymbol");
}
if ( !symbolInImage("free", "dlsym-RTLD_DEFAULT") ) {
FAIL("free");
}
if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
FAIL("foo not in libfoo-static.dylib");
}
void* handle = dlopen(RUN_DIR "/libfoo-dynamic.dylib", RTLD_LAZY);
if ( handle == NULL ) {
FAIL("libfoo-dynamic.dylib could not be loaded");
}
if ( !symbolInImage("foo", "libfoo-static.dylib") ) {
FAIL("foo not in libfoo-static.dylib");
}
if ( !symbolInImage("foo2", "libfoo-dynamic.dylib") ) {
FAIL("foo2 not in libfoo-dynamic.dylib");
}
if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
FAIL("strcmp not found");
}
PASS("Success");
}