I was messing around with Objective-C trying to compile Objective-C code without linking it to libobjc , and I am having problems with segmentation problems with the program, so I generated the build file from This. I think it is not necessary to show the entire assembly file. At some point in my main function, I have the following line (which, incidentally, is the line after which I get a seg error):
callq *l_objc_msgSend_fixup_alloc
and here is the definition for l_objc_msgSend_fixup_alloc :
.hidden l_objc_msgSend_fixup_alloc # @"\01l_objc_msgSend_fixup_alloc" .type l_objc_msgSend_fixup_alloc,@object .section "__DATA, __objc_msgrefs, coalesced","aw",@progbits .weak l_objc_msgSend_fixup_alloc .align 16 l_objc_msgSend_fixup_alloc: .quad objc_msgSend_fixup .quad L_OBJC_METH_VAR_NAME_ .size l_objc_msgSend_fixup_alloc, 16
I redefined objc_msgSend_fixup as a function ( id objc_msgSend_fixup(id self, SEL op, ...) ) that returns nil (just to find out what is happening), but this function is not even called (the program crashes before the call).
So my question is what is the callq *l_objc_msgSend_fixup_alloc and what objc_msgSend_fixup (after l_objc_msgSend_fixup_alloc: should be (function or object)?
Edit
To better explain, I am not linking my source file to the objc library. What I'm trying to do is implement some parts of libray, just to see how it works. Here is the approach to what I did:
If the runtime needs to access the vtable object or the list of methods of the obect class to find the correct method to call, what is the function that actually does this? I think in this case it is objc_msgSend_fixup . So, when objc_msgSend_fixup is objc_msgSend_fixup , it receives the object as one of its parameters, and if this object has not been initialized, the function does not work.
So, I implemented my own version of objc_msgSend_fixup . According to the build source above, it should be called. It does not matter if the function is really looking for an implementation of the selector passed as a parameter. I just want objc_msgSend_lookup to objc_msgSend_lookup called. But it is not called, that is, the function that searches for object data is not even called, but does not cause and causes an error (because it returns nil (which, by the way, is not the case)). The seg program fails before objc_msgSend_lookup is objc_msgSend_lookup ...
Edit 2
A more complete assembly fragment:
.globl main .align 16, 0x90 .type main,@function main: # @main .Ltmp20: .cfi_startproc # BB#0: pushq %rbp .Ltmp21: .cfi_def_cfa_offset 16 .Ltmp22: .cfi_offset %rbp, -16 movq %rsp, %rbp .Ltmp23: .cfi_def_cfa_register %rbp subq $32, %rsp movl $0, %eax leaq l_objc_msgSend_fixup_alloc, %rcx movl $0, -4(%rbp) movl %edi, -8(%rbp) movq %rsi, -16(%rbp) movq L_OBJC_CLASSLIST_REFERENCES_$_, %rsi movq %rsi, %rdi movq %rcx, %rsi movl %eax, -28(%rbp) # 4-byte Spill callq *l_objc_msgSend_fixup_alloc movq %rax, -24(%rbp) movl -28(%rbp), %eax # 4-byte Reload addq $32, %rsp popq %rbp ret
For l_objc_msgSend_fixup_alloc we have:
.hidden l_objc_msgSend_fixup_alloc # @"\01l_objc_msgSend_fixup_alloc" .type l_objc_msgSend_fixup_alloc,@object .section "__DATA, __objc_msgrefs, coalesced","aw",@progbits .weak l_objc_msgSend_fixup_alloc .align 16 l_objc_msgSend_fixup_alloc: .quad objc_msgSend_fixup .quad L_OBJC_METH_VAR_NAME_ .size l_objc_msgSend_fixup_alloc, 16
For L_OBJC_CLASSLIST_REFERENCES_$_ :
.type L_OBJC_CLASSLIST_REFERENCES_$_,@object # @"\01L_OBJC_CLASSLIST_REFERENCES_$_" .section "__DATA, __objc_classrefs, regular, no_dead_strip","aw",@progbits .align 8 L_OBJC_CLASSLIST_REFERENCES_$_: .quad OBJC_CLASS_$_MyClass .size L_OBJC_CLASSLIST_REFERENCES_$_, 8
OBJC_CLASS_$_MyClass is a pointer to the MyClass structure definition, which was also generated by the compiler and is also present in the assembly code.