dylib中自动执行构造函数代码

发布于 2025-01-15 22:39:08 字数 1006 浏览 2 评论 0原文

据我所知,当使用 dlopen 加载库时,构造函数中的代码会自动执行。

我如何为 iOS 框架执行此操作?我希望 dylib 在加载时在构造函数中运行一些代码。

在Apple的文档中(https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html),我看到了这个片段

// Initializer.
__attribute__((constructor))
static void initializer(void) {                             // 2
    printf("[%s] initializer()\n", __FILE__);
}
 
// Finalizer.
__attribute__((destructor))
static void finalizer(void) {                               // 3
    printf("[%s] finalizer()\n", __FILE__);
}

我尝试了以下

  • 我创建的框架的 Xcode 项目。在框架中,我添加了上面的代码片段。然后我构建了框架的调试版本。

我想要做的是

  • 修改用户的位码文件(从启用位码的 IPA 中提取)以将我自己的一些代码合并到其中。我的代码(用 C 编写并转换为位码以进行合并)将在 dylib 上调用 dlopen,当加载它时,构造函数代码将执行。

From what I read, code in the constructor is executed automatically when the library is loaded using dlopen.

How can I do this for a Framework for iOS? I want the dylib to run some code in the constructor when it is loaded.

In Apple's documentation (https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html), I saw this snippet

// Initializer.
__attribute__((constructor))
static void initializer(void) {                             // 2
    printf("[%s] initializer()\n", __FILE__);
}
 
// Finalizer.
__attribute__((destructor))
static void finalizer(void) {                               // 3
    printf("[%s] finalizer()\n", __FILE__);
}

I have tried the following

  • I created an Xcode Project for a Framework. In the Framework, I added the above code snippet. Then I build a debug version of the Framework.

What I want to do is

  • Modify a user's bitcode file (extracted from a bitcode enabled IPA) to merge some of my own code into it. My code (written in C and converted to bitcode for merging) would call dlopen on the dylib and when it is loaded, the constructor code will execute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文