Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates gcc 4.2.1 variadic function weirdness for x86_64 architecture
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
static id pear()
{
__asm__("nop");
return @"pear";
}
static id banana(id self, SEL _cmd, ...)
{
return @"banana";
}
@interface Fruit : NSObject
@end
@implementation Fruit
- (id) apple
{
return @"apple";
}
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
FILE *devnull = fopen("/dev/null", "w");
Method apple = class_getInstanceMethod([Fruit class], @selector(apple));
method_setImplementation(apple, banana);
Fruit *fruit = [[[Fruit alloc] init] autorelease];
fprintf(devnull, "(0x100000c3c - 0x100000bac) / 4 = 36");
printf("%s\n", [[fruit apple] UTF8String]);
fclose(devnull);
[pool drain];
return 0;
}
# $ gcc --version
# i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
# Copyright (C) 2007 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions. There is NO
# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# $ clang --version
# Apple clang version 1.7 (tags/Apple/clang-77) (based on LLVM 2.9svn)
# Target: x86_64-apple-darwin10
# Thread model: posix
all:
gcc -arch x86_64 fruit.m -o fruit-gcc -framework Foundation
clang -arch x86_64 fruit.m -o fruit-clang -framework Foundation
clean:
rm -f fruit-gcc fruit-clang
run: all
./fruit-gcc
./fruit-clang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.