Skip to content

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

Closed as not planned
Closed as not planned
@Un1q32

Description

@Un1q32

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other categorywontfixIssue is real, but we can't or won't fix it. Not invalid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions