Maxime B.
Maxime B.

Reputation: 1236

How are Thread Local Storage offset resolved in Linux Kernel?

I am learning about Thread Local storage and their usage in Linux kernel. I understood that it allows to store per-thread data. This is for instance the case when accessing the current task_struct.

I see that in my x86_64 architecture this is done by using the global segment (%gs). My question is: How does GCC resolve addresses for this global segment (i.e. how does it translates mov rax,QWORD PTR gs:0 in the .(k)o to mov rax,QWORD PTR gs:0x17d80 in the compiled object ?)

Upvotes: 1

Views: 228

Answers (1)

Jester
Jester

Reputation: 58762

The .ko is the compiled object. The gs prefix is emitted explicitly, the rest is just normal symbol relocation. For example, excerpt from running objdump -dr amdgpu.ko:

12b51:       65 48 8b 14 25 00 00    mov    %gs:0x0,%rdx
12b58:       00 00 
                    12b56: R_X86_64_32S     current_task

The R_X86_64_32S is just a standard symbol relocation, it will be resolved by the kernel module loader. The gs prefix is already there in the code.

Upvotes: 4

Related Questions