如何在iOS中查找文本段范围 [英] How to find text segment range in iOS

查看:314
本文介绍了如何在iOS中查找文本段范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS中找到文本片段(AKA代码段)范围?意思是,文本段的起始地址和结束地址是什么?

How can I find the text segment (AKA code segment) range in iOS? Meaning, what is the start address and the end address of the text segment?

我发现这个有趣的帖子但它适用于Android但不适用于iOS。

I found this interesting post but it works for me on Android but not on iOS.

推荐答案

经过一些挖掘和专家帮助(感谢Moshe Kravchik),我得到了理想的解决方案 - 通过解析mach头并检索加载命令,段和部分来获取文本段范围。

After some digging and expert help (thanks Moshe Kravchik) I got to the desired solution - getting the text segment range by parsing the mach header and retrieving the load commands, segments and sections.

#include <mach-o/dyld.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#import <Foundation/Foundation.h>

#define PRINT_STR "Found __text Section of %s, addr 0x%x, size %u, offset 0x%x, calc address 0x%x"
#define LC_SEGMENT_NATIVE LC_SEGMENT
#define segment_command_native segment_command
#define section_native section

struct libRange
{
    uint32_t start;
    uint32_t end;
};

void getTextSegmentAddr(struct libRange *txtSegRange)
{
    if (txtSegRange==NULL)
        return;

    txtSegRange->start=0;

    const struct mach_header *mach_hdr;
    mach_hdr = _dyld_get_image_header(0);                           

    const struct load_command *cmds = (const struct load_command *)(mach_hdr + 1);
    uint32_t cmdsleft;
    const struct load_command *lc;

    for(lc = cmds, cmdsleft = mach_hdr->ncmds; cmdsleft-- && (0 == txtSegRange->start);) {
            if(lc->cmd == LC_SEGMENT_NATIVE) {
                    const struct segment_command_native *sc = (void *) lc;
                    const struct section_native *sect = (void *) (sc + 1);
                    for(uint32_t sect_idx = 0; sect_idx < sc->nsects; sect_idx++) {
                            if(!strcmp("__TEXT", sect->segname) && !strcmp("__text", sect->sectname)) {
                                    uint32_t memAddr = (sc->vmaddr + _dyld_get_image_vmaddr_slide(0) + sect->offset - sc->fileoff);                                                                                                   
                                    NSLog(@PRINT_STR,_dyld_get_image_name(0), sect->addr, sect->size, sect->offset, memAddr);                                                                                           
                                    txtSegRange->start = memAddr;                                                                                            
                                    txtSegRange->end = memAddr + sect->size;
                                    break;
                             }
                             sect++;    
                    }
            }
            lc = (void *) ((char *) lc + lc->cmdsize);
    }
}

int main()
{
    struct libRange txtSegRange;
    getTextSegmentAddr(&txtSegRange);
    return 0;
}

这篇关于如何在iOS中查找文本段范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆