首页 > 解决方案 > 使用 -static -nostdlib 时如何设置 __thread?

问题描述

我怀疑问题是我需要设置fs寄存器。我只是想知道什么是正确的方法?我对如何确定线程局部变量(.tbbs?)的大小一无所知。我知道我可以在编译后使用 readelf 手动检查,但我假设该解决方案不涉及解析 elf 文件。

我有时会像这样编译二进制文件,所以我要阅读的程序集更少。需要 clang 和 gcc 的解决方案

gcc -O2 -fno-stack-protector -static -nostdlib -Wl,--build-id=none -Wl,--nmagic -fno-asynchronous-unwind-tables tiny.c ; objcopy --remove-section .comment a.out a.out ; 剥离 ./a.out

来源

//gcc -static -nostdlib tiny.c && ./a.out
static inline void my_exit(int errcode) {
    register int rax __asm__ ("eax") = 60;
    register int rdi __asm__ ("edi") = errcode;
    __asm__ __volatile__ (
        "syscall"
        : "+r" (rax)
        : "r" (rdi)
        : "cc", "memory"
    );
}

__thread int counter;

int _start()
{
    counter++; //Comment this out or remove `__thread` and this runs fine
    my_exit(1);
    return 0;
}

标签: cassemblygccx86-64thread-local-storage

解决方案


推荐阅读