objective-c-runtime

What is `objc_msgSend_fixup`, exactly?

我是研究僧i 提交于 2019-12-04 09:32:09
问题 I'm messing around with the Objective-C runtime, trying to compile objective-c code without linking it against libobjc , and I'm having some segmentation fault problems with a program, so I generated an assembly file from it. I think it's not necessary to show the whole assembly file. At some point of my main function, I've got the following line (which, by the way, is the line after which I get the seg fault): callq *l_objc_msgSend_fixup_alloc and here is the definition for l_objc_msgSend

Swizzling a method with variable arguments and forward the message - Bad Access

旧巷老猫 提交于 2019-12-04 07:19:43
I'm implementing a "Code Injector Class", that through method swizzling can give you the possibility to do something like this: FLCodeInjector *injector = [FLCodeInjector injectorForClass:[self class]]; [injector injectCodeBeforeSelector:@selector(aSelector:) code:^{ NSLog(@"This code should be injected"); }]; aSelector can be a method with variable number of arguments, and variable return type. Arguments / and return type can be objects or primitive type. First, I attach the code of injectCodeBeforeSelector: to let you understand what I'm doing (I removed not interesting parts of the code): -

How can one obtain the sizeof a type for which one has an encoding?

给你一囗甜甜゛ 提交于 2019-12-04 01:33:19
Given an Objective-C type type , one can obtain the encoding encoding and size size of the type easily: const char *encoding = @encode(type); size_t size = sizeof(type); Put a little differently, we have mappings @encode: type_t -> const char * sizeof: type_t -> size_t This raises two questions: (1) Suppose that rather than having a type, we have only an encoding. It would be nice to obtain a mapping sizeofencodedtype: const char * -> size_t such that for every type_t type we have that sizeofencodedtype(@encode(type)) = sizeof(type) Does such a function already exist? If not, how might one go

How to implement an NSRunLoop inside an NSOperation

风流意气都作罢 提交于 2019-12-03 18:30:19
问题 Im posting this question because I have seen a lot of confusion over this topic and I spent several hours debugging NSOperation subclasses as a result. The problem is that NSOperation doesnt do you much good when you execute Asynchronous methods which are not actually complete until the asynchronous callback completes. If the NSOperation itself is the callback delegate it may not even be sufficient to properly complete the operation due to the callback occurring on a different thread. Lets

object_setClass to bigger class

流过昼夜 提交于 2019-12-03 16:38:47
问题 I am changing the class of some objects using object_setClass(id object, Class cls) . I am changing the class to a subclass of the original class. Then I set some properties that are only defined on the subclass, and things seem to work fine. I was a bit surprised that this worked, because object_setClass , as far as I understand, doesn't reallocate the object, it only changes the isa pointer. If the subclass instances are considerably larger (meaning having many more ivars) than the original

Objective-C: preferred way to retrieve the superclass of a Class instance

余生颓废 提交于 2019-12-03 16:10:46
问题 I am wondering which of the two following methods is the correct or preferred one to retrieve the superclass of a Class variable: Class getSuperclass(Class cls) { return [cls superclass]; } Class getSuperclass(Class cls) { return class_getSuperclass(cls); } 回答1: Well, the docs on class_getSuperclass() say this: You should usually use NSObject‘s superclass method instead of this function So, I'd go with door #1. 回答2: The accepted answer is technically correct (yes, that's what the docs say),

How to dynamically add a class method?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 15:44:06
问题 Using the Objective-C Runtime, how do I add the method +layerClass to the private UIGroupTableViewCellBackground class (not to its superclass, UIView )? Note: This is only for testing (to see how UITableViewStyleGrouped sets cell backgroundView & selectedBackgroundView ). 回答1: To dynamically add a class method, instead of an instance method, use object_getClass(cls) to get the meta class and then add the method to the meta class. E.g.: UIKIT_STATIC_INLINE Class my_layerClass(id self, SEL _cmd

What's required to implement root class of Objective-C?

99封情书 提交于 2019-12-03 15:24:50
I tried this code: // main.m #import <stdio.h> @interface Test + (void)test; @end @implementation Test + (void)test { printf("test"); } @end int main() { [Test test]; return 0; } with LLVM/Clang without any framework, it doesn't compiled with this error: Undefined symbols: "_objc_msgSend", referenced from: _main in main.o ld: symbol(s) not found clang: error: linker command failed with exit code 1 (use -v to see invocation) So I added libobjc.dylib . Code compiled, but threw this runtime exception: objc[13896]: Test: Does not recognize selector forward:: Program received signal: “EXC_BAD

Why can't we use C-strings as SELs?

假装没事ソ 提交于 2019-12-03 12:45:09
问题 So, I've been messing around with the objc-runtime again (surprise surprise), and I found an interesting block of code here: const char *sel_getName(SEL sel) { #if SUPPORT_IGNORED_SELECTOR_CONSTANT if ((uintptr_t)sel == kIgnore) return "<ignored selector>"; #endif return sel ? (const char *)sel : "<null selector>"; } So, what this tells me is that a SEL is equivalent to a C-string, in every mannerism. Doing a hex dump of the first 16 bytes of SEL that contains @selector(addObject:) gives the

How does the Objective-C runtime retrieve the list of classes and methods?

对着背影说爱祢 提交于 2019-12-03 12:29:45
问题 If I get the following Objective-C source file: // test.m #import <objc/Object.h> @interface MySuperClass: Object { } -(void) myMessage1; @end @implementation MySuperClass -(void) myMessage1 { } @end @interface MyClass: MySuperClass { } -(void) myMessage2; @end @implementation MyClass -(void) myMessage2 { } @end int main() { return 0; } and try to generate an assembly file from it with clang -fobjc-nonfragile-abi -fnext-runtime -S test.m , I get the following assembly code: .file "test.m"