iOS动态链接器dyld中有一个神秘的变量__dso_handle:

// dyld/dyldMain.cpp
static const MachOAnalyzer* getDyldMH()
{
#if __LP64__
// 声明 __dso_handle
extern const MachOAnalyzer __dso_handle;
return &__dso_handle;
#else
...
#endif // __LP64__
}

这个函数内部声明了一个变量__dso_handle,其类型是struct MachOAnalyzer

查看struct MachOAnalyzer的定义,它继承自struct mach_header:

image

struct mach_header正是XNU内核里面,定义的Mach-O文件头:

// EXTENERL_HEADERS/mach-o/loader.h
struct mach_header {
uint32_t magic; /* mach magic number identifier */
cpu_type_t cputype; /* cpu specifier */
cpu_subtype_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
};

从上面函数getDyldMH的名字来看,它返回dyld这个Mach-O文件的文件头,而这确实也符合变量__dso_handle的类型定义。

但是奇怪的事情发生了,搜遍整个dyld源码库,都无法找到变量__dso_handle的定义。所有能搜到的地方,都只是对这个变量__dso_handle的声明。

众所周知,动态连接器dyld本身是静态链接的。

也就是说,动态连接器dyld本身是不依赖任何其他动态库的。

因此,这个变量__dso_handle不可能定义在其他动态库。

既然这样,动态链接器dyld本身是如何静态链接通过的呢?

答案只可能是静态链接器ld在链接过程中做了手脚。

查看静态链接器ld的源码,也就是llvm的源码,可以找到如下代码:

// lld/MachO/SyntheticSections.cpp
void macho::createSyntheticSymbols() {
// addHeaderSymbol 的 lamba 表达式
auto addHeaderSymbol = [](const char *name) {
symtab->addSynthetic(name, in.header->isec, /*value=*/0,
/*isPrivateExtern=*/true, /*includeInSymtab=*/false,
/*referencedDynamically=*/false);
};
...
// The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
// which does e.g. cleanup of static global variables. The ABI document
// says that the pointer can point to any address in one of the dylib's
// segments, but in practice ld64 seems to set it to point to the header,
// so that's what's implemented here.
addHeaderSymbol("___dso_handle");
}

上面代码定义了一个addHeaderSymbollamda表达式,然后使用它添加了一个符号,这个符号正是__dso_handle

调用addHeaderSymbol上方的注释使用chatGPT翻译过来如下:

Itanium C++ ABI 要求动态库传递一个指向 __cxa_atexit 的指针,该函数负责例如静态全局变量的清理。ABI 文档指出,指针可以指向动态库的某个段中的任意地址,但实际上,ld64(苹果的链接器)似乎将其设置为指向头部,所以这里实现了这种做法。

注释中提到的Itanium C++ ABI最初是为英特尔和惠普联合开发的Itanium处理器架构设计的。

但其影响已经超过了最初设计的架构范围,并被广泛用于其他架构,比如x86x86-64上的多种编译器,包括GCCClang

而且,注释中还提到,__dso_handle在苹果的实现里,是指向了Mach-O的头部。

至此,谜底解开~。

posted @ 2024-11-19 03:01 chaoguo1234 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 一文搞懂 ARM 64 系列: ADD(立即数版) 阅读全文
posted @ 2024-11-15 01:36 chaoguo1234 阅读(171) 评论(0) 推荐(0) 编辑
摘要: 一文搞懂 ARM 64: AUTIBSP 阅读全文
posted @ 2024-11-13 02:04 chaoguo1234 阅读(58) 评论(0) 推荐(0) 编辑
摘要: 一文搞懂 ARM 64 系列: PACISB 阅读全文
posted @ 2024-11-13 01:35 chaoguo1234 阅读(257) 评论(0) 推荐(0) 编辑
摘要: XCode 编译 PAG 源码 阅读全文
posted @ 2024-07-28 15:46 chaoguo1234 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 一文搞懂 ARM 64 系列: 一文搞懂 ARM 64 系列: 函数调用传参与返回值 阅读全文
posted @ 2024-06-09 04:50 chaoguo1234 阅读(1376) 评论(0) 推荐(2) 编辑
摘要: 一文搞懂 ARM 64 系列: 寄存器 阅读全文
posted @ 2024-06-08 21:44 chaoguo1234 阅读(2684) 评论(0) 推荐(2) 编辑
摘要: 一文搞懂 ARM 64 系列: ADCS 阅读全文
posted @ 2024-05-10 00:33 chaoguo1234 阅读(244) 评论(0) 推荐(0) 编辑
摘要: 一文搞懂 ARM 64 系列: ADC 阅读全文
posted @ 2024-05-09 02:19 chaoguo1234 阅读(522) 评论(0) 推荐(0) 编辑
摘要: ARM64: ARDP 阅读全文
posted @ 2024-03-24 14:18 chaoguo1234 阅读(237) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示