c - 获取函数地址时如何获得 RTLD_LAZY 行为?

标签 c linux

我们有一个严重依赖使用 dlopen() 加载模块的系统。我们的模块是自描述的,带有一个符号,指向与加载模块相关的一些元数据(描述、加载顺序、加载标志等)。

我们首先加载为 dlopen() 指定 RTLD_LAZY 标志的模块,从元数据中获取我们需要的内容,然后稍后加载真正的模块(在我们知道加载顺序应该是什么之后) ,如何加载它们等)。

这已经工作了一段时间,但我们最近发现,获取函数的地址需要在加载时解析该函数​​。我们可以采取哪些方法来解决这个问题?

我整理了一个相当小的例子来演示这个问题。

/* foo.h */
void foo(void);

/* foo.c */
void foo(void) {}

/* bar.c */
#include "foo.h"
/* Calls foo normally */
void bar(void) { foo(); }

/* bam.c */
#include "foo.h"
static void (*f)(void);
/* Takes the address of foo */
void bam(void) { f = foo; }

/* rtld_lazy.c */
#include <dlfcn.h>
#include <stdio.h>

void check(const char *module) {
  void *mod = dlopen(module, RTLD_LAZY);
  if (mod) {
    printf("%s successfully loaded\n", module);
    dlclose(mod);
  } else {
    printf("%s failed to load: %s\n", module, dlerror());
  }
}

int main() {
  check("./bar.so");
  check("./bam.so");
  check("./foo.so");
}

输出如下:

./bar.so successfully loaded
./bam.so failed to load: ./bam.so: undefined symbol: foo
./foo.so successfully loaded

最佳答案

您应该使用 dlsym() 来获取符号的地址。请参阅 POSIX:

http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html

关于c - 获取函数地址时如何获得 RTLD_LAZY 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17491465/

相关文章:

linux - 最终构建内容外壳并共享

C++ MP3、WAV 和 AAC(低级)解码器?

linux - 如何使用node在Windows CMD中运行文件

c - 如何修改 TI SensorTag 固件以无限期地做广告?

计算给定边界框以及图像的宽度和高度的比例

c - fputs 读取到文件末尾后返回-1

c++ - GLib HashTable 将函数作为参数传递

linux - 未找到 EMCC - 仅在 emsdk 中有效,在终端中不全局

linux - 如何通过 cron 运行 Scala 程序?

c - "error: (1098) conflicting declarations"当函数类型改变时