5

Summary: When I set the -mcmodel=large flag when compiling with clang my application segfaults when accessing thread local storage. This does not happen when compiling with gcc. Is this a bug with clang or something I am doing wrong?

Details:

The following code segment crashes when compiled with clang when setting the -mcmodel flag, but it runs fine when compiled with gcc

#include <stdio.h>
#include <pthread.h>

__thread int tlsTest;

int main(int argc, char **argv) {
  printf("&tlsTest is %p\n", &tlsTest);
  tlsTest = argc;
  printf("tlsTest is %d\n", tlsTest);
  return 0;
}

When I compile with: clang test.c -pthread -mcmodel=large the result is:

&tlsTest is 0x7fd24262c6fc
Segmentation fault (core dumped)

But with: gcc test.c -pthread -mcmodel=large the result is:

&tlsTest is 0x7f1cf785c6fc
tlsTest is 1

The program also works fine when compiled with: clang test.c -pthread

I read the following link about mcmodel but I'm not sure how this relates to the segfault that I've observed. Note that the problem occurs for -mcmodel=medium also, but not for -mcmodel=small.

Is this a bug with clang/llvm or is it a different interpretation of the standard or some unimplemented feature?

Also my system is Ubuntu 12.04. My version of gcc is 4.6.3 and the version of clang/llvm that I tested is a recent snapshot of 3.3 development, and I also tested with clang 3.2.

Gabriel Southern
  • 9,602
  • 12
  • 56
  • 95

0 Answers0