• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 检测代码是否在 Framework vs. App vs. Bundle

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:00:23 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

有没有办法检测正在编译的代码是在框架、 bundle 还是动态库中?

原因是因为崩溃报告库需要在获取结构变量的地址之前知道它是否存在..

IE:

#ifdef MH_EXECUTE_SYM
return (uint8_t*)&_mh_execute_header;
#else
return (uint8_t*)&_mh_dylib_header;
#endif

问题是 MH_EXECUTE_SYM, MH_BUNDLE_SYM, MH_DYLIB_SYM 总是为每一种可执行文件、 bundle 、框架定义..

所以我需要一种方法来确定要获取哪个结构变量的地址。有什么想法吗?



Best Answer-推荐答案


看起来您真的只是想获得一个指向适当的 mach_header_64(或 32 位系统上的 mach_header)的指针。

如果你有一个指针,你可以使用 dladdr 函数找出它是从哪个(如果有的话)mach-o 加载的。该函数填充了一个 Dl_info 结构,其中包括一个指向 mach-o 的 mach_header_64 的指针。

// For TARGET_RT_64_BIT:
#import <TargetConditionals.h>

// For dladdr:
#import <dlfcn.h>

// For mach_header and mach_header_64:
#import <mach-o/loader.h>

#ifdef TARGET_RT_64_BIT

struct mach_header_64 *mach_header_for_address(const void *address) {
    Dl_info info;
    if (dladdr(address, &info) == 0) {
        // address doesn't point into a mach-o.
        return 0;
    }
    struct mach_header_64 *header = (struct mach_header_64 *)info.dli_fbase;
    if (header->magic != MH_MAGIC_64) {
        // Something went wrong...
        return 0;
    }

    return header;
}

#else

struct mach_header mach_header_for_address(const void *address) {
    Dl_info info;
    if (dladdr(address, &info) == 0) {
        // address doesn't point into a mach-o.
        return 0;
    }
    struct mach_header *header = (struct mach_header *)info.dli_fbase;
    if (header->magic != MH_MAGIC) {
        // Something went wrong...
        return 0;
    }

    return header;
}

#endif

关于ios - 检测代码是否在 Framework vs. App vs. Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54222359/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap