c-为什么LC_SYMTAB具有无效的stroff/strsize,但仅适用于某些加载的图像



我编写了下面的程序来迭代内存中的所有图像并转储它们的字符串表。

#include <mach-o/dyld.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
uint32_t count = _dyld_image_count();
for (uint32_t i = 0 ; i < count ; i++) {
const char* imageName = _dyld_get_image_name(i);
printf("IMAGE[%u]=%sn", i, imageName);
const struct mach_header* header = _dyld_get_image_header(i);
if (header->magic != MH_MAGIC_64)
continue;
struct mach_header_64* header64 = (struct mach_header_64*)header;
char *ptr = ((void*)header64) + sizeof(struct mach_header_64);
for (uint32_t j = 0; j < header64->ncmds; j++) {
struct load_command *lc = (struct load_command *)ptr;
ptr += lc->cmdsize;
if (lc->cmd != LC_SYMTAB)
continue;
struct symtab_command* symtab = (struct symtab_command*)lc;
printf("ttLC_SYMTAB.stroff=%un", symtab->stroff);
printf("ttLC_SYMTAB.strsize=%un", symtab->strsize);
if (symtab->strsize > 100*1024*1024) {
printf("ttHUH? Don't believe string table is over 100MiB in size!n");
continue;
}
char *strtab = (((void*)header64) + symtab->stroff);
uint32_t off = 0;
while (off < symtab->strsize) {
char *e = &(strtab[off]);
if (e[0] != 0)
printf("ttSTR[%u]="%s"n", off, e);
off += strlen(e) + 1;
}
}
}
return 0;
}

对于某些图像,它似乎是随机工作的,但对于其他图像,stroff/strsize具有荒谬的值:

LC_SYMTAB.stroff=1266154560
LC_SYMTAB.strsize=143767728

这似乎总是相同的两个神奇值,但我不确定这是否在某种程度上取决于系统,或者其他人是否会得到相同的特定值。

如果我注释掉strsize超过100MiB的检查,那么打印字符串表segfault。

大多数图像似乎都有这个问题,但有些图像没有。当我运行它时,我得到了38张图片中的29张的问题。

我无法观察到任何模式,即哪些做,哪些不做。这是怎么回事?

如果相关的话,我正在macOS 10.14.6上进行测试,并使用Apple LLVM 10.0.1版本(clang-1001.0.46.4(进行编译

正如您已经计算出的,这些来自dyld_shared_cache0x80000000标志确实被记录在Xcode或任何半新的XNU来源的标题中:

#define MH_DYLIB_IN_CACHE 0x80000000    /* Only for use on dylibs. When this bit
is set, the dylib is part of the dyld
shared cache, rather than loose in
the filesystem. */

正如您还发现的,stroff/strsize值在添加到dyld_shared_cache基数时不会产生可用的结果。这是因为这些不是内存偏移,而是文件的偏移。对于所有的Mach-O都是如此,通常情况下,非缓存二进制文件的段在文件和内存偏移中具有相同的相对位置。但对于共享缓存来说,这绝对不是真的。

要将文件偏移量转换为内存地址,您必须解析共享缓存头中的段。您可以在dyld源中找到结构定义。

这里有一个程序,它打印出dyld共享缓存的字符串表的内容。

我在问题中的原始程序可以被增强为跳过MH_DYLIB_IN_CACHE设置的图像的转储字符串表,并与此程序相结合来转储共享缓存字符串表。(共享缓存中的所有图像共享相同的字符串表。(

#include <mach-o/dyld.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
const void* _dyld_get_shared_cache_range(size_t* cacheLen);
struct dyld_cache_header {
char            magic[16];
uint32_t        mappingOffset;
uint32_t        mappingCount;
// Omitted remaining fields, not relevant to this task
};
struct dyld_cache_mapping_info {
uint64_t    address;
uint64_t    size;
uint64_t    fileOffset;
uint32_t    maxProt;
uint32_t    initProt;
};
#ifndef MH_DYLIB_IN_CACHE
#       define MH_DYLIB_IN_CACHE 0x80000000
#endif
// Finds first shared cache DYLD image. Any will do, just grab the first
const struct mach_header_64* findSharedCacheDyldImage(void) {
uint32_t count = _dyld_image_count();
for (uint32_t i = 0 ; i < count ; i++) {
const struct mach_header* header = _dyld_get_image_header(i);
if (header->magic != MH_MAGIC_64)
continue;
const struct mach_header_64* header64 = (const struct mach_header_64*)header;
if (!(header64->flags & MH_DYLIB_IN_CACHE))
continue;
return header64;
}
return NULL;
}
// Find first instance of given load command in image
const struct load_command* findFirstLoadCommand(const struct mach_header_64* header64, uint32_t cmd) {
const char *ptr = ((void*)header64) + sizeof(struct mach_header_64);
for (uint32_t j = 0; j < header64->ncmds; j++) {
const struct load_command *lc = (const struct load_command *)ptr;
ptr += lc->cmdsize;
if (lc->cmd == cmd)
return lc;
}
return NULL;
}
// Translates a shared cache file offset to a memory address
void *translateOffset(const struct dyld_cache_header *cache, uint64_t offset) {
const struct dyld_cache_mapping_info* mappings = (struct dyld_cache_mapping_info*)(((void*)cache) + cache->mappingOffset);
for (uint32_t i = 0; i < cache->mappingCount; i++) {
if (offset < mappings[i].fileOffset) continue;
if (offset >= (mappings[i].fileOffset + mappings[i].size)) continue;
return (void*)(mappings[i].address - mappings[0].address + (offset - mappings[i].fileOffset) + (uint64_t)cache);
}
return NULL;
}
int main(int argc, char** argv) {
size_t cacheLen;
const struct dyld_cache_header *cache = _dyld_get_shared_cache_range(&cacheLen);
const struct mach_header_64* sharedCacheDyldImage = findSharedCacheDyldImage();
const struct symtab_command* symtab = (const struct symtab_command*)findFirstLoadCommand(sharedCacheDyldImage,LC_SYMTAB);
const void *stringTbl = translateOffset(cache, symtab->stroff);
uint32_t off = 0;
while (off < symtab->strsize) {
const char *e = &(stringTbl[off]);
if (e[0] != 0)
printf("STR[%u]="%s"n", off, e);
off += strlen(e) + 1;
}
return 0;
}

相关内容

最新更新