#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <mach-o/dyld_priv.h>
int mainSymbol = 4;
int main()
{
printf("[BEGIN] dlsym-handle\n");
void* fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
if ( fooHandle == NULL ) {
printf("[FAIL] dlsym-handle: libfoo.dylib could not be loaded, %s\n", dlerror());
return 0;
}
void* barHandle = dlopen(RUN_DIR "/libbar.dylib", RTLD_LAZY);
if ( barHandle == NULL ) {
printf("[FAIL] dlsym-handle: libbar.dylib could not be loaded, %s\n", dlerror());
return 0;
}
if ( dlsym(fooHandle, "mainSymbol") != NULL ) {
printf("[FAIL] dlsym-handle: mainSymbol was found with fooHandle\n");
return 0;
}
if ( dlsym(fooHandle, "foo") == NULL ) {
printf("[FAIL] dlsym-handle: foo not found with fooHandle\n");
return 0;
}
if ( dlsym(fooHandle, "base") == NULL ) {
printf("[FAIL] dlsym-handle: base not found with fooHandle\n");
return 0;
}
if ( dlsym(fooHandle, "bar") != NULL ) {
printf("[FAIL] dlsym-handle: bar found with fooHandle\n");
return 0;
}
if ( dlsym(barHandle, "bar") == NULL ) {
printf("[FAIL] dlsym-handle: bar not found with barHandle\n");
return 0;
}
if ( dlsym(barHandle, "base") == NULL ) {
printf("[FAIL] dlsym-handle: base not found with barHandle\n");
return 0;
}
if ( dlsym(barHandle, "foo") != NULL ) {
printf("[FAIL] dlsym-handle: foo found with barHandle\n");
return 0;
}
if ( dlsym(RTLD_DEFAULT, "strcmp") == NULL ) {
printf("[FAIL] dlsym-handle: strcmp not found\n");
return 0;
}
if ( dlsym((void*)0xdeadbeef, "malloc") != NULL ) {
printf("[FAIL] dlsym-handle: malloc found with bad handle\n");
return 0;
}
else {
const char* message = dlerror();
if ( strstr(message, "invalid") == NULL ) {
printf("[FAIL] dlsym-handle: invalid handle error message missing 'invalid'\n");
return 0;
}
}
printf("[PASS] dlsym-handle\n");
return 0;
}