Skip to content

[RISCV] Add support for TLSDESC#307

Merged
quic-seaswara merged 6 commits intomainfrom
riscv-tlsdesc
Feb 27, 2026
Merged

[RISCV] Add support for TLSDESC#307
quic-seaswara merged 6 commits intomainfrom
riscv-tlsdesc

Conversation

@quic-akaryaki
Copy link
Copy Markdown
Contributor

No description provided.

@quic-akaryaki quic-akaryaki force-pushed the riscv-apply-reloc-nfc branch 2 times, most recently from 5108c07 to f371760 Compare August 7, 2025 18:49
Base automatically changed from riscv-apply-reloc-nfc to main August 8, 2025 14:36
Comment thread lib/Target/GNULDBackend.cpp
Comment thread lib/Target/GNULDBackend.cpp Outdated
@quic-akaryaki
Copy link
Copy Markdown
Contributor Author

Looks like the build failed because upstream LLVM does not emit R_RISCV_ALIGN.

@quic-akaryaki quic-akaryaki force-pushed the riscv-tlsdesc branch 2 times, most recently from e6d8711 to 6112a1c Compare August 13, 2025 23:33
@quic-akaryaki quic-akaryaki requested a review from partaror August 13, 2025 23:35
@quic-akaryaki quic-akaryaki force-pushed the riscv-tlsdesc branch 2 times, most recently from bde6e34 to 04957ed Compare August 14, 2025 01:07
@quic-akaryaki quic-akaryaki marked this pull request as ready for review August 14, 2025 01:15
Comment thread lib/Target/RISCV/RISCVLDBackend.cpp Outdated
Comment thread test/AArch64/standalone/BSS_none_bss/BSS_none_bss.yaml Outdated
Comment thread lib/Target/RISCV/RISCVRelocator.cpp
Comment thread lib/Target/RISCV/RISCVRelocator.cpp
Comment thread test/lld/ELF/riscv-tlsdesc-relax.test
@quic-seaswara
Copy link
Copy Markdown
Contributor

@lenary please review

@quic-seaswara
Copy link
Copy Markdown
Contributor

quic-seaswara commented Aug 15, 2025

I would like to see tests using 'C' code as well. You can still keep the asm test.

I would like to see more detailed testing mentioned below :

  • Basic TLSDESC tests without relaxation (Executable, Shared library, PIE, static linking, symbol visibility tests)
    • Probably we want to have a flag --no-tlsdesc-relax
  • TLSDESC tests with relaxation for the cases above
  • Please add what relaxations is supported right now, and what is punted for later
    • It is not clear from the commit message on what TLS relaxationare included in this patch
  • Any addition to map file for TLS relaxations will be useful
  • Test cases with mno-relax as needed
  • Documentation should follow as a seperate subtask (to know what the linker did for our users)

Please create subtasks as needed and totally I am ok to see the limited support that we add for 21.0 but my focus is to build this feature with more clarity.

cat > 1.c << \!
__attribute__((visibility("hidden"))) __thread int tls_var = 10;
int foo() { return tls_var; }
!

cat > 2.c << \!
extern __thread int tls_var;
int bar() { return tls_var; }
!

clang -c -target riscv32 1.c -fPIC -mtls-dialect=desc
clang -c -target riscv32 2.c -fPIC
# case 1
ld.eld -march riscv32 1.o 2.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE
# case 2 (partial link)
ld.eld -march riscv32 1.o 2.o -r -o r.o
ld.eld -march riscv32 r.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE
# case 3 (no relax)
clang -c -target riscv32 1.c -fPIC -mtls-dialect=desc -mno-relax
clang -c -target riscv32 2.c -fPIC -mno-relax
ld.eld -march riscv32 1.o 2.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE

@quic-akaryaki
Copy link
Copy Markdown
Contributor Author

I would like to see tests using 'C' code as well. You can still keep the asm test.

I would like to see more detailed testing mentioned below :

  • Basic TLSDESC tests without relaxation (Executable, Shared library, PIE, static linking, symbol visibility tests)

    • Probably we want to have a flag --no-tlsdesc-relax
  • TLSDESC tests with relaxation for the cases above

  • Please add what relaxations is supported right now, and what is punted for later

    • It is not clear from the commit message on what TLS relaxationare included in this patch
  • Any addition to map file for TLS relaxations will be useful

  • Test cases with mno-relax as needed

  • Documentation should follow as a seperate subtask (to know what the linker did for our users)

Please create subtasks as needed and totally I am ok to see the limited support that we add for 21.0 but my focus is to build this feature with more clarity.

cat > 1.c << \!
__attribute__((visibility("hidden"))) __thread int tls_var = 10;
int foo() { return tls_var; }
!

cat > 2.c << \!
extern __thread int tls_var;
int bar() { return tls_var; }
!

clang -c -target riscv32 1.c -fPIC -mtls-dialect=desc
clang -c -target riscv32 2.c -fPIC
# case 1
ld.eld -march riscv32 1.o 2.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE
# case 2 (partial link)
ld.eld -march riscv32 1.o 2.o -r -o r.o
ld.eld -march riscv32 r.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE
# case 3 (no relax)
clang -c -target riscv32 1.c -fPIC -mtls-dialect=desc -mno-relax
clang -c -target riscv32 2.c -fPIC -mno-relax
ld.eld -march riscv32 1.o 2.o -shared -o 1.so
llvm-readelf --dyn-syms 1.so | grep tls_var && echo BADIMAGE
llvm-readelf -r 1.so | grep tls_var && echo BADIMAGE

Just to clarify, this patch has full support for TLSDESC, including all 4 types of static relocations, linker optimizations (replacing instructions with nops) and relaxations (removing instructions). Of course, it may have bugs, which is the case with existing code, which could be fixed before the 21 releases. Since new requirements come 1 day before the 21.x feature deadline, it won't go into the release.

Also, please clarify:
-- if --no-tlsdesc-relax is needed
-- which addition to map file for TLS relaxations will be useful

Thank you

@quic-seaswara
Copy link
Copy Markdown
Contributor

Just to clarify, this patch has full support for TLSDESC, including all 4 types of static relocations, linker optimizations (replacing instructions with nops) and relaxations (removing instructions). Of course, it may have bugs, which is the case with existing code, which could be fixed before the 21 releases. Since new requirements come 1 day before the 21.x feature deadline, it won't go into the release.

Yes, sorry!.

My point was to make TLSDESC without relaxation work before relaxations are added.

Also cases such as TLSDESC with symbol visibility is something users typically do. We should look into those cases to make sure that we have tests that show functionality.

Also, please clarify: -- if --no-tlsdesc-relax is needed -- which addition to map file for TLS relaxations will be useful

I think it will be useful to have --no-tlsdesc-relax like --no-gp-relax or --no-relax-c to have a workaround if any.

Map file can be annotated to point out what relaxations were applied.

@quic-akaryaki quic-akaryaki marked this pull request as draft February 23, 2026 19:47
@quic-akaryaki quic-akaryaki force-pushed the riscv-tlsdesc branch 2 times, most recently from 05e5826 to 6038fea Compare February 23, 2026 19:58
@quic-akaryaki quic-akaryaki marked this pull request as ready for review February 23, 2026 20:17
Copy link
Copy Markdown
Contributor

@quic-seaswara quic-seaswara left a comment

Choose a reason for hiding this comment

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

Great!. Let us get it merged.

@lenary if you can go through it and have your comments, it would be great!

Copy link
Copy Markdown
Member

@lenary lenary left a comment

Choose a reason for hiding this comment

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

I only have one high-level comment. Otherwise I'm happy.

Comment thread lib/LinkerWrapper/RISCVLinkDriver.cpp
@quic-akaryaki
Copy link
Copy Markdown
Contributor Author

The last force push is only a rename no_riscv_tlsdesc -> no_relax_tlsdesc.

Signed-off-by: Alexey Karyakin <akaryaki@quicinc.com>
Add '.yaml' to config.suffixes and fix tests that had non-test yaml
files outside of their Inputs directory.

Signed-off-by: Alexey Karyakin <akaryaki@quicinc.com>
quic-akaryaki and others added 4 commits February 26, 2026 14:13
The RISC-V psABI is silent on where R_RISCV_RELAX has to be in the
relocation table relative to the affected relocation, as long as it
is "at the same position". It is also not clear where exactly `.reloc`
assembly directive must place the relocation.

Therefore, sort all the RISC-V relocations. They were already sorted,
but not always.

Signed-off-by: Alexey Karyakin <akaryaki@quicinc.com>
Signed-off-by: Alexey Karyakin <akaryaki@qti.qualcomm.com>
Signed-off-by: Alexey Karyakin <akaryaki@qti.qualcomm.com>
Signed-off-by: Alexey Karyakin <akaryaki@qti.qualcomm.com>
@quic-seaswara quic-seaswara merged commit 52b0e2b into main Feb 27, 2026
3 checks passed
@quic-seaswara quic-seaswara deleted the riscv-tlsdesc branch February 27, 2026 17:29
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.

4 participants