#include #include #include #include #include __thread int a; static struct tls_tcb * ThreadSelfTlsTcb() { struct tls_tcb * tcb; # ifdef __HAVE___LWP_GETTCB_FAST tcb = (struct tls_tcb *)__lwp_gettcb_fast(); # elif defined(__HAVE___LWP_GETPRIVATE_FAST) tcb = (struct tls_tcb *)__lwp_getprivate_fast(); # endif return tcb; } int GetSizeFromHdr(struct dl_phdr_info *info, size_t size, void *data) { const Elf_Phdr *hdr = info->dlpi_phdr; const Elf_Phdr *last_hdr = hdr + info->dlpi_phnum; for (; hdr != last_hdr; ++hdr) { if (hdr->p_type == PT_TLS && info->dlpi_tls_modid == 1) { *(long*)data = hdr->p_memsz; break; } } return 0; } int main(int argc, char **argv) { long addr, size; struct tls_tcb * const tcb = ThreadSelfTlsTcb(); addr = 0; size = 0; if (tcb != 0) { // Find size (p_memsz) of dlpi_tls_modid 1 (TLS block of the main program). // ld.elf_so hardcodes the index 1. dl_iterate_phdr(GetSizeFromHdr, &size); if (size != 0) { // The block has been found and tcb_dtv[1] contains the base address addr = (long)tcb->tcb_dtv[1]; } } printf("addr=%#lx size=%ld\n", addr, size); return 0; }