1

Recently I hit an issue with compiling constant @strings in objective-c.

Below is a reproduction of the issue.

This case is fine.

#import<Foundation/Foundation.h>

int main() {
   BOOL condition = NO;
   while(condition) {
      NSLog(@"12345678");
      return 0;
   }
}

This case is not fine.

#import<Foundation/Foundation.h>

int main() {
   BOOL condition = NO;
   while(condition) {
      NSLog(@"123456789");
   }
}

Error

/tmp/test-1ad248.o:test.m:.objc_init: error: undefined reference to '__start___objc_constant_string'
/tmp/test-1ad248.o:test.m:.objc_init: error: undefined reference to '__stop___objc_constant_string'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Furthermore, this case is fine.

#import<Foundation/Foundation.h>

int main() {
   NSLog(@"123456789");
}

My guess is that this is something to do with storing the string as a bytes in a pointer in a dense fashion. IE 64-bit system has 8 char bytes per pointer. so 1 through 8 is fine but not 1 through 9. However, I do not know how to fix this issue.

It even fails with the following

#import<Foundation/Foundation.h>

int main() {
  BOOL condition = NO;
  NSString *msg = @"123456789";
  while (condition) {
    NSLog(@"%@", msg);
  }
}

System

  • Ubuntu 20.04
  • Gnustep Package 7.10
  • Compile command: clang -x objective-c $(gnustep-config --objc-libs & gnustep-config --objc-flags) -lgnustep-base -lgnustep-gui test.m
  • This is a strange issue! I suspect however that it has something to do with compiler optimisations. Those times when you get trouble is when the piece of code could be optimised away e.g. because the condition is ```NO```. I think you also should look at what your ```-fconstant-string-class``` setting is. Can you edit that into your question? – skaak Nov 17 '20 at 06:30
  • @skaak I noticed the same thing. I have posted an updated version of this on LLVM's bug board (https://bugs.llvm.org/show_bug.cgi?id=48192). It contains so additional examples and talks similarly about the seeming optimization issue. I suspect that in optimization the loop is removed but the removal of the constant string is only partial. For example, removing the string but leaving the reference to it. This would be an issue as the initialization function is still called and trying to initialize something that does not exist. – Chase Minor Nov 18 '20 at 16:54

0 Answers0