[bolt] Errors when TLS relaxation to LE is atypical
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
I have an object file with TLS global dynamic (TLSDESC) for AARCH64, I linked this to static executable using mold linker. This linker is emitting relaxation code that is not typically what lld or bfd linkers emit.
```
# Object file
0000000000000060 :
...
68: 90000000 adrp x0, 0
68: R_AARCH64_TLSDESC_ADR_PAGE21 tls_var
6c: f9400001 ldr x1, [x0]
6c: R_AARCH64_TLSDESC_LD64_LO12 tls_var
70: 91000000 add x0, x0, #0x0
70: R_AARCH64_TLSDESC_ADD_LO12 tls_var
74: d63f0020 blr x1
74: R_AARCH64_TLSDESC_CALL tls_var
78: d53bd048 mrs x8, tpidr_el0
7c: b8606908 ldr w8, [x8, x0]
...
```
During relaxation it ends up inserting NOP pair before MOVZ/MOVK pair.
```
0000000000210350 :
...
210358: d503201f nop
210358: R_AARCH64_TLSDESC_ADR_PAGE21 tls_var
21035c: d503201f nop
21035c: R_AARCH64_TLSDESC_LD64_LO12 tls_var
210360: d2a00000 movz x0, #0x0, lsl #16
210360: R_AARCH64_TLSDESC_ADD_LO12 tls_var
210364: f2800200 movk x0, #0x10
210364: R_AARCH64_TLSDESC_CALL tls_var
210368: d53bd048 mrs x8, tpidr_el0
21036c: b8606908 ldr w8, [x8, x0]
...
```
LLD emits something like this:
```
0000000000210254 :
...
21025c: d2a00000 movz x0, #0x0, lsl #16
21025c: R_AARCH64_TLSDESC_ADR_PAGE21 tls_var
210260: f2800200 movk x0, #0x10
210260: R_AARCH64_TLSDESC_LD64_LO12 tls_var
210264: d503201f nop
210264: R_AARCH64_TLSDESC_ADD_LO12 tls_var
210268: d503201f nop
210268: R_AARCH64_TLSDESC_CALL tls_var
21026c: d53bd048 mrs x8, tpidr_el0
210270: b8606908 ldr w8, [x8, x0]
...
```
I see that the bolt adjusts and makes sure the fixup is not added to these isntructions with immediate values inside adjustRelocation() when performing disassembly. This has a missing reloc - R_AARCH64_TLSDESC_ADD_LO12. This causes the MOVKXi instruction to be associated with S_LO12 fixup which is not valid.
I have a change that fixes this, it passes bolt tests and the large-bolt-arm target for all AArch64 bolt tests.
Contributor guide
Research direction
Start by reading BOLT's adjustRelocation() handling for AArch64 TLSDESC relocations and compare the relocation sequence with the examples in this issue. Run the AArch64 BOLT tests, including the large-bolt-arm target, and confirm that R_AARCH64_TLSDESC_ADD_LO12 is associated correctly without invalid S_LO12 fixups.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100