Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang ignores -femulated-tls on Darwin with LTO enabled #117192

Open
Un1q32 opened this issue Nov 21, 2024 · 0 comments
Open

Clang ignores -femulated-tls on Darwin with LTO enabled #117192

Un1q32 opened this issue Nov 21, 2024 · 0 comments
Labels
clang Clang issues not falling into any other category

Comments

@Un1q32
Copy link
Contributor

Un1q32 commented Nov 21, 2024

tls.c:

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

__thread int var = 0;

void *func(void *arg) {
  (void)arg;
  if (var == 0)
    var = 1;
  else
    puts("race");
  return NULL;
}

int main(void) {
  pthread_t ptid;
  int i = 0;
  while (i < 10) {
    pthread_create(&ptid, NULL, func, NULL);
    i++;
  }
  return 0;
}

Run on macOS:

$ clang -mmacos-version-min=14.0 tls.c -c
$ ld -arch x86_64 -platform_version macos 14.0 14.0 tls.o
Undefined symbols for architecture x86_64:
  "__tlv_bootstrap", referenced from:
      _var in tls.o
  "_pthread_create", referenced from:
      _main in tls.o
  "_puts", referenced from:
      _func in tls.o
ld: symbol(s) not found for architecture x86_64
$ # __tlv_bootstrap indicates normal TLS
$ clang -mmacos-version-min=14.0 tls.c -c -femulated-tls
$ ld -arch x86_64 -platform_version macos 14.0 14.0 tls.o
Undefined symbols for architecture x86_64:
  "___emutls_get_address", referenced from:
      _func in tls.o
      _func in tls.o
  "_pthread_create", referenced from:
      _main in tls.o
  "_puts", referenced from:
      _func in tls.o
ld: symbol(s) not found for architecture x86_64
$ # ___emutls_get_address indicates emulated TLS
$ clang tls.c -c -femulated-tls -flto
$ ld -arch x86_64 -platform_version macos 14.0 14.0 tls.o
Undefined symbols for architecture x86_64:
  "__tlv_bootstrap", referenced from:
      _var in lto.o
  "_pthread_create", referenced from:
      _main in lto.o
  "_puts", referenced from:
      _func in lto.o
ld: symbol(s) not found for architecture x86_64
$ # __tlv_bootstrap is used dispite -femulated-tls being passed.

It happens when using -emit-llvm instead of -flto too. My guess is that the TLS model used is decided after LLVM IR generation, is there a workaround to use emulated TLS with LTO?

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

1 participant