Skip to content

Improve g TLS offset retrieval for binaries built with buildmode=pie#730

Merged
fabled merged 4 commits intoopen-telemetry:mainfrom
DataDog:nsavoire/improve_g_tls_offset_retrieval
Sep 4, 2025
Merged

Improve g TLS offset retrieval for binaries built with buildmode=pie#730
fabled merged 4 commits intoopen-telemetry:mainfrom
DataDog:nsavoire/improve_g_tls_offset_retrieval

Conversation

@nsavoire
Copy link
Copy Markdown
Contributor

Go binaries built with -buildmode=pie have slightly different assembly code. On arm64, the runtime.load_g symbol has 2 mov (movz and movk) instructions instead of 1 mov:

0x00000000000c2290 <+0>: adrp x27, 0x2ca000 <runtime.itabTableInit+3072>
0x00000000000c2294 <+4>: ldrsb x0, [x27, #1766]
0x00000000000c2298 <+8>: cbz x0, 0xc22ac <runtime.load_g+28>
0x00000000000c229c <+12>: mrs x0, tpidr_el0
0x00000000000c22a0 <+16>: movz x27, #0x0, lsl #16
0x00000000000c22a4 <+20>: movk x27, #0x10
0x00000000000c22a8 <+24>: ldr x28, [x0, x27]
0x00000000000c22ac <+28>: ret

And on amd64, the runtime.stackcheck symbol has also 2 mov instructions instead of 1:
0x00000000007ec320 <+0>: mov $0xfffffffffffffff8,%rcx
0x00000000007ec327 <+7>: mov %fs:(%rcx),%rax

This commit fixes the TLS offset retrieval for both architectures. On amd64, it uses amd.Interpreter to find the mov instruction with fs segment and then determine the offset into the segment when displacement is a register. On arm64, it looks for mov or movk instructions (ignoring movz instruction since it seems to always be 0).

@nsavoire nsavoire requested review from a team as code owners August 25, 2025 08:11
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Aug 25, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Copy Markdown
Contributor

@gnurizen gnurizen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

Copy link
Copy Markdown
Contributor

@fabled fabled left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

Comment thread interpreter/golabels/tls_arm64.go Dismissed
florianl added a commit that referenced this pull request Aug 26, 2025
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
@florianl
Copy link
Copy Markdown
Member

We need to land #728 first, as the CI fails here as symbols can't be found.

@florianl
Copy link
Copy Markdown
Member

As #728 got merged, can you rebase this change onto most recent main?

Go binaries built with -buildmode=pie have slightly different assembly code.
On arm64, the runtime.load_g symbol has 2 mov (movz and movk) instructions
instead of 1 mov:

0x00000000000c2290 <+0>:	adrp	x27, 0x2ca000 <runtime.itabTableInit+3072>
0x00000000000c2294 <+4>:	ldrsb	x0, [x27, #1766]
0x00000000000c2298 <+8>:	cbz	x0, 0xc22ac <runtime.load_g+28>
0x00000000000c229c <+12>:	mrs	x0, tpidr_el0
0x00000000000c22a0 <+16>:	movz	x27, #0x0, lsl #16
0x00000000000c22a4 <+20>:	movk	x27, #0x10
0x00000000000c22a8 <+24>:	ldr	x28, [x0, x27]
0x00000000000c22ac <+28>:	ret

And on amd64, the runtime.stackcheck symbol has also 2 mov instructions instead of 1:
0x00000000007ec320 <+0>:	mov    $0xfffffffffffffff8,%rcx
0x00000000007ec327 <+7>:	mov    %fs:(%rcx),%rax

This commit fixes the TLS offset retrieval for both architectures.
On amd64, it uses amd.Interpreter to find the mov instruction with fs segment
and then determine the offset into the segment when displacement is a register.
On arm64, it looks for mov or movk instructions (ignoring movz instruction since
it seems to always be 0).
@nsavoire nsavoire force-pushed the nsavoire/improve_g_tls_offset_retrieval branch from c67d71f to 670d11e Compare September 1, 2025 08:16
@nsavoire
Copy link
Copy Markdown
Contributor Author

nsavoire commented Sep 1, 2025

As #728 got merged, can you rebase this change onto most recent main?

Done !

Comment thread Makefile Outdated
Comment thread interpreter/golabels/tls_amd64.go Outdated
@nsavoire nsavoire force-pushed the nsavoire/improve_g_tls_offset_retrieval branch from f1aa1a1 to e793b25 Compare September 3, 2025 13:53
@fabled fabled merged commit 5fc6d5d into open-telemetry:main Sep 4, 2025
28 checks passed
@nsavoire nsavoire deleted the nsavoire/improve_g_tls_offset_retrieval branch September 15, 2025 07:53
gnurizen pushed a commit to parca-dev/opentelemetry-ebpf-profiler that referenced this pull request Nov 4, 2025
gnurizen pushed a commit to parca-dev/opentelemetry-ebpf-profiler that referenced this pull request Nov 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants