美文网首页
通过Mach-O获取方法名类名

通过Mach-O获取方法名类名

作者: 西博尔 | 来源:发表于2016-12-29 14:16 被阅读90次
未完待续....
下面的代码还是有问题的, 只能找非fat类型的 Mach-O , 正在写 fat类型的
uint64_t FilegetSize(const char *file_path){
    struct stat buf;
    if ( stat(file_path,&buf) < 0 )
    {
        perror(file_path);
        exit(1);
    }
    return buf.st_size;
}


void loadMachO()
{
    filePath = [[[NSBundle mainBundle] pathForResource:@"ZhaNiao_pjf-mobile" ofType:@""] UTF8String];
    
    FILE *fp_open = fopen(filePath,"r");
    uint64_t file_size = FilegetSize(filePath);
    if(!fp_open){
        printf("file isn't exist\n");
        exit(1);
    }
    printf("file size is 0x%llx\n\n",file_size);
    void *file_buf = malloc(file_size);
    if(fread(file_buf,1,file_size,fp_open)!=file_size){
        printf("fread error\n");
        exit(1);
    }
    fclose(fp_open);
    
    //检查是否为Fat头
    struct fat_header* fileStartAsFat = (struct fat_header*)file_buf;
    if(fileStartAsFat->magic==FAT_CIGAM||fileStartAsFat->magic==FAT_MAGIC){
        printf("is fat\n");
        //        exit(1);
    }
    
    //检查mach-o文件最前面几个字节的内容.
    struct mach_header *mh = (struct mach_header*)file_buf;
    int is32 = 1;
    
    if(mh->magic==MH_MAGIC||mh->magic==MH_CIGAM){
        is32 = 1;
    }
    else if(mh->magic==MH_MAGIC_64||mh->magic==MH_CIGAM_64){
        is32 = 0;
    }
    
    const uint32_t cmd_count = mh->ncmds;
    
    const struct load_command* const startCmds    = (struct load_command*)(((uint8_t*)mh) + sizeof(struct mach_header));
    //获取command段结束的地址,endCmds = mach-o地址 + mach-o头部长度 + cmds所用的长度
    const struct load_command* cmd = startCmds;
    
    for (uint32_t i = 0; i < cmd_count; ++i) {
     
        uint32_t cmdLength = cmd->cmdsize;
        
        struct segment_command * segCmd;
        
        const struct load_command* const nextCmd = (const struct load_command*)(((char*)cmd)+cmdLength);
        
        switch (cmd->cmd) {
            case LC_SEGMENT:
            {
                segCmd = (struct segment_command *)cmd;
                NSLog(@"segname = %s",segCmd->segname);
                

             
                if (strcmp(segCmd->segname, "__TEXT") == 0) {
                    
                    
                    struct section * sectionsStart = NULL;
                    struct section * nextSection = NULL;
                    for (int i = 0; i < segCmd->nsects; i++) {
                        
                        if (i == 0) {
                            sectionsStart = (struct section *)((char *)cmd + sizeof(struct segment_command));
                            nextSection = sectionsStart;

                        }else
                        {
                            nextSection = (struct section *)((char *)nextSection + sizeof(struct section));
                        }
                        
                        if (strncmp(nextSection->sectname, "__objc_methname", strlen("__objc_methname"))==0) {
                            NSLog(@"***** 修改方法名 ****");
                            
                            
                            
                        }
                        if (strncmp(nextSection->sectname, "__objc_classname", strlen("__objc_classname"))==0) {
                            NSLog(@"***** 修改类名 *****");
                         
                        }
                  
                        NSLog(@"sectionName = %s , segName = %s",nextSection->sectname,nextSection->segname);
                        
                        
                        
                    }

                    
                }
                
                
                
                
            }
                break;
                
            default:
                break;
        }
        cmd = nextCmd;
    }

}


相关文章

网友评论

      本文标题:通过Mach-O获取方法名类名

      本文链接:https://www.haomeiwen.com/subject/hsxpvttx.html