> The following program under the Next Objective-C runtime crashes
> when compiled like this:
> gcc -o ConstantCrash -fconstant-string-class=MyConstantString
> ConstantCrash.m -lobjc
> Under the GNU runtime this program works just fine. It seems that
> the compiler option -fconstant-string-class is basically broken
> under the Next runtime.
> Am I doing something dumb, or is this a bug?
Not dumb, you just need to read the compiler source to figure out
how to get this to work :)
Below is a version that works. The key points you were missing
are the type of _MyConstantStringClassReference and copying in the
compiler generated class. Don't get me wrong, this is a totally
goofy way for this to work, but there you go.
Maybe there is some issue with getting it to link, but in a sane
world, I'd want @"" to emit a 3-word tuple with the first one being a
relocation to the actual class structure generated by the compiler.
Some of the compiler folks on the list can comment to this better
than I can.
BTW, the 'objc_addClass' call below isn't really necessary for
this to work, but it makes sure that [@"foo" class] results in
something that is registered as a class. Also, the +load works, even
though it looks like a method since it gets compiled into a function
marked as a constructor (i.e., it isn't called by objc_msgSend, as
far as I know).
// our @"" strings will have their isa pointing at
_MyConstantStringClassReference, but this is empty and isn't in the
// Copy over the compiler-generated class. Note that if there
are any subclasses of MyConstantString, they'll point to the compiler-
generated class as their superclass!
Class cls = objc_getClass("MyConstantString");
memcpy(&_MyConstantStringClassReference, cls, sizeof
// Override the compiler version of the class
- (const char *)name;
printf("I would be a %s if I didn't crash\n", [@"DoggyBoy" name]);