{"id":13318135,"url":"https://github.com/zekunyan/TTGRemakeMethodSignatureForSelector","last_synced_at":"2025-03-11T01:31:24.126Z","repository":{"id":86192343,"uuid":"87719752","full_name":"zekunyan/TTGRemakeMethodSignatureForSelector","owner":"zekunyan","description":"Remake methodSignatureForSelector. Just for research. :)","archived":false,"fork":false,"pushed_at":"2017-04-10T02:37:19.000Z","size":77,"stargazers_count":11,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T22:25:45.402Z","etag":null,"topics":["assembly","core","hopper-disassembler","ios","objective-c"],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zekunyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-09T15:46:33.000Z","updated_at":"2024-02-07T15:14:19.000Z","dependencies_parsed_at":"2023-03-01T03:15:28.737Z","dependency_job_id":null,"html_url":"https://github.com/zekunyan/TTGRemakeMethodSignatureForSelector","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zekunyan%2FTTGRemakeMethodSignatureForSelector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zekunyan%2FTTGRemakeMethodSignatureForSelector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zekunyan%2FTTGRemakeMethodSignatureForSelector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zekunyan%2FTTGRemakeMethodSignatureForSelector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zekunyan","download_url":"https://codeload.github.com/zekunyan/TTGRemakeMethodSignatureForSelector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242955384,"owners_count":20212317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly","core","hopper-disassembler","ios","objective-c"],"created_at":"2024-07-29T18:29:38.202Z","updated_at":"2025-03-11T01:31:24.112Z","avatar_url":"https://github.com/zekunyan.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TTGRemakeMethodSignatureForSelector\n\nRemake `methodSignatureForSelector:`，just for research.\n\n如果你在天朝：\n团队博客：[http://icbu.info/diy_methodsignatureforselector/](http://icbu.info/diy_methodsignatureforselector/)  \n土土哥的技术Blog: [http://tutuge.me/2017/04/08/diy-methodSignatureForSelector/](http://tutuge.me/2017/04/08/diy-methodSignatureForSelector/)\n\n## Core code\n\nttg_MethodDescription\n\n```\nstruct objc_method_description ttg_MethodDescription(Class class, SEL sel) {\n    // Result\n    struct objc_method_description description = (struct objc_method_description){NULL, NULL};\n    \n    // Loop class\n    Class currentClass = class;\n    \n    while (currentClass \u0026\u0026 description.name == NULL) {\n        // Get protocol list\n        unsigned int count = 0;\n        __unsafe_unretained Protocol **protocols = class_copyProtocolList(currentClass, \u0026count);\n        \n        // Check each protocol\n        for (unsigned int i = 0; i \u003c count; i++) {\n            // Required method\n            description = protocol_getMethodDescription(protocols[i], sel, YES, class_isMetaClass(currentClass) ^ 1);\n            if (description.name != NULL) {\n                break;\n            }\n            \n            // Optional method\n            description = protocol_getMethodDescription(protocols[i], sel, NO, class_isMetaClass(currentClass) ^ 1);\n            if (description.name != NULL) {\n                break;\n            }\n        }\n        \n        // release\n        free(protocols);\n        \n        // Found in protocol\n        if (description.name != NULL) {\n            return description;\n        }\n        \n        // Get superClass and continue\n        currentClass = class_getSuperclass(currentClass);\n    }\n    \n    // Get implemented instance method\n    Method method = class_getInstanceMethod(class, sel);\n    if (method) {\n        // Get description\n        return *method_getDescription(method);\n    } else {\n        // Return Null result\n        return (struct objc_method_description){NULL, NULL};\n    }\n}\n```\n\nNSObject Category \n\n```\n@implementation NSObject (TTGRemakeMethodSignatureForSelector)\n\n- (NSMethodSignature *)ttg_methodSignatureForSelector:(SEL)sel {\n    struct objc_method_description description = ttg_MethodDescription([self class], sel); // Get from class\n    if (sel \u0026\u0026 description.types != NULL) {\n        return [NSMethodSignature signatureWithObjCTypes:description.types];\n    } else {\n        return nil;\n    }\n}\n\n+ (NSMethodSignature *)ttg_methodSignatureForSelector:(SEL)sel {\n    struct objc_method_description description = ttg_MethodDescription(object_getClass(self), sel); //  Get from metaClass\n    if (sel \u0026\u0026 description.types != NULL) {\n        return [NSMethodSignature signatureWithObjCTypes:description.types];\n    } else {\n        return nil;\n    }\n}\n\n@end\n```\n\n## CoreFoundation.framework implementation from Hopper Disassemabler\n\n[NSObject methodSignatureForSelector:]\n\n```\nvoid * +[NSObject methodSignatureForSelector:](void * self, void * _cmd, void * arg2) {\n    rdi = self;\n    rbx = arg2;\n    if ((rbx != 0x0) \u0026\u0026 (___methodDescriptionForSelector(object_getClass(rdi), rbx) != 0x0)) {\n            rax = [NSMethodSignature signatureWithObjCTypes:rdx];\n    }\n    else {\n            rax = 0x0;\n    }\n    return rax;\n}\n```\n\n___methodDescriptionForSelector:\n\n![](https://github.com/zekunyan/TTGRemakeMethodSignatureForSelector/raw/master/Resources/TTGRemakeMethodSignatureForSelector_0.png)\n\n```\nint ___methodDescriptionForSelector(int arg0, int arg1) {\n    var_40 = arg1;\n    var_38 = arg0;\n    if (arg0 == 0x0) goto loc_918d6;\n\nloc_917e2:\n    r15 = var_38;\n    goto loc_917f0;\n\nloc_917f0:\n    r12 = class_copyProtocolList(r15, 0x0);\n    var_50 = r15;\n    if (0x0 == 0x0) goto loc_918a0;\n\nloc_91814:\n    r13 = 0x0;\n    var_48 = r12;\n    goto loc_91820;\n\nloc_91820:\n    rbx = var_40;\n    rdi = r15;\n    r15 = protocol_getMethodDescription(*(r12 + r13 * 0x8), rbx, 0x1, (class_isMetaClass(r15) ^ 0x1) \u0026 0xff);\n    r14 = 0x1;\n    if (r15 != 0x0) goto loc_918b4;\n\nloc_91853:\n    r15 = protocol_getMethodDescription(*(r12 + r13 * 0x8), rbx, 0x0, (class_isMetaClass(rdi) ^ 0x1) \u0026 0xff);\n    r14 = 0x0;\n    if (r15 != 0x0) goto loc_918b0;\n\nloc_91879:\n    r13 = r13 + 0x1;\n    r15 = var_50;\n    r12 = var_48;\n    if (r13 \u003c 0x0) goto loc_91820;\n\nloc_9188c:\n    r15 = 0x0;\n    goto loc_918b4;\n\nloc_918b4:\n    free(r12);\n    if (r15 != 0x0) goto loc_918ff;\n\nloc_918c1:\n    r15 = class_getSuperclass(var_50);\n    if (r15 != 0x0) goto loc_917f0;\n\nloc_918d6:\n    rax = class_getInstanceMethod(var_38, var_40);\n    if (rax != 0x0) {\n            rax = method_getDescription(rax);\n            r15 = *rax;\n            r14 = *(rax + 0x8);\n    }\n    else {\n            r15 = 0x0;\n            r14 = 0x0;\n    }\n    goto loc_918ff;\n\nloc_918ff:\n    rax = r15;\n    return rax;\n\nloc_918b0:\n    r12 = var_48;\n    goto loc_918b4;\n\nloc_918a0:\n    if (r12 == 0x0) goto loc_918c1;\n\nloc_918a5:\n    r14 = 0x0;\n    r15 = 0x0;\n    goto loc_918b4;\n}\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzekunyan%2FTTGRemakeMethodSignatureForSelector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzekunyan%2FTTGRemakeMethodSignatureForSelector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzekunyan%2FTTGRemakeMethodSignatureForSelector/lists"}