使用Xcode,我希望从mach-o bundle二进制文件中重新导出一个符号(特别是一个函数),其中该符号最初是在dylib中定义的。
我尝试过-sub_library链接器开关,但似乎无法重新导出dylib符号,可能是因为我自己没有构建dylib(?)
而且在Xcode的链接器中似乎不支持再导出-l/reexport_library开关。
有什么想法吗?
如果我理解正确的话,这可能就是你想要的。我将使用libpthread作为假设的dylib,其中包含要重新导出的函数。
mybundle.c:
#include <pthread.h>
#include <stdio.h>
void *foo(void *ctx) {
puts((char *)ctx);
return 0;
}
mybundle.exp:
_foo
_pthread_create
_pthread_join
编译捆绑包,动态链接到libpthread.dylib:
josh$ gcc -bundle -lpthread -Wl,-exported_symbols_list,mybundle.exp -o mybundle.so mybundle.c
myloader.c:
#include <dlfcn.h>
#include <pthread.h> // merely for type definitions
#include <assert.h>
#include <stdio.h>
int main() {
void *(*foo)(void *ctx);
/* the following cannot be declared globally without changing their names,
as they are already (and unnecessarily) declared in <pthread.h> */
int (*pthread_create)(pthread_t *thrd, const pthread_attr_t *attr, void *(*proc)(void *), void *arg);
int (*pthread_join)(pthread_t thrd, void **val);
void *bundle;
assert(bundle = dlopen("mybundle.so", RTLD_NOW));
assert(foo = dlsym(bundle, "foo"));
assert(pthread_create = dlsym(bundle, "pthread_create"));
assert(pthread_join = dlsym(bundle, "pthread_join"));
pthread_t myThrd;
pthread_create(&myThrd, 0, foo, "in another thread");
pthread_join(myThrd, 0);
return 0;
}
编译加载器:
josh$ gcc myloader.c -o myloader
运行:
josh$ ./myloader
in another thread
请注意,myloader并没有链接到pthread,但是pthread函数是通过捆绑包加载的,并且在运行时可用。