/* * tls-test.c * Verifies the MontaukOS TLS runtime: PT_TLS loading, FS base, * .tdata initialization, .tbss zeroing, and the ABI self-pointer. * Built by the devkit target and shipped at 0:/sdk/bin/tls-test.elf. */ #include #include __thread int tls_int = 41; __thread char tls_str[32] = "montauk-tls"; __thread int tls_bss[8]; /* .tbss, must arrive zeroed */ int main(void) { int ok = 1; tls_int++; tls_bss[3] = 7; printf("tls_int = %d (want 42)\n", tls_int); printf("tls_str = %s (want montauk-tls)\n", tls_str); printf("tls_bss = %d,%d (want 0,7)\n", tls_bss[0], tls_bss[3]); if (tls_int != 42) ok = 0; if (tls_str[0] != 'm' || tls_str[10] != 's') ok = 0; if (tls_bss[0] != 0 || tls_bss[3] != 7) ok = 0; /* The thread pointer must hold a self-pointer at %fs:0. */ uint64_t self; __asm__("mov %%fs:0, %0" : "=r"(self)); printf("fs:0 self = 0x%lx\n", (unsigned long)self); if (self == 0) ok = 0; printf(ok ? "TLS OK\n" : "TLS BROKEN\n"); return ok ? 0 : 1; }