Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv64: Implement ELF TLS GD Relocations #7003

Merged
merged 6 commits into from
Sep 18, 2023

Conversation

afonso360
Copy link
Contributor

@afonso360 afonso360 commented Sep 12, 2023

👋 Hey,

This PR implements ELF TLS GD relocations in the RISC-V backend. This is required to get the rustc_codegen_cranelift's testsuite passing on this architecture.

The TLS GD model for RISC-V works slightly differently from the rest of the existing architectures. Usually you have 2 or 3 relocations pointing to a symbol and then a call to __tls_get_addr. RISC-V however has one relocation pointing to the symbol in a auipc instruction, and then a relocation pointing to a label in that auipc instruction, not the final symbol itself.

label:
    auipc a0,0                    # R_RISCV_TLS_GD_HI20 (symbol)
    addi  a0,a0,0                 # R_RISCV_PCREL_LO12_I (label)
    call __tls_get_addr@plt

This presents a problem as we currently don't have a way to export labels into the final binary, or a way to target labels with relocations.

Most of this PR is dealing with that issue. In the first commit I've introduced a notion of label visibility that allows a label to be marked as public, and if so, be carried all the way through to binary object emission.

Secondly is altering our allowed relocation targets. Currently we only allow external names, such as libcalls or other functions. I've altered this to allow targeting other labels within the function.

Only the final commit deals with implementing the TLS GD relocations in the RISC-V backend.


I also considered implementing TLS descriptors, but their spec isn't finalized yet (riscv-non-isa/riscv-elf-psabi-doc#94)

This doesn't have a great deal of tests as its quite difficult to test these kinds of binary emissions. But I've manually tested this by running cg_clif's testsuite.

CC: @bjorn3

@afonso360 afonso360 added the cranelift:area:riscv64 Issues related to the RISC-V 64 backend. label Sep 12, 2023
@afonso360 afonso360 requested a review from a team as a code owner September 12, 2023 12:14
@afonso360 afonso360 requested review from cfallin and removed request for a team September 12, 2023 12:14
@afonso360 afonso360 requested a review from a team as a code owner September 12, 2023 12:49
@afonso360 afonso360 requested review from alexcrichton and removed request for a team September 12, 2023 12:49
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:module labels Sep 12, 2023
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

ELF bits all look good to me (in that I'm assuming you're checking correctness and otherwise can confirm they're idiomatic for Cranelift)

For the relocation/label bits I wrote out a suggestion below and am wondering if it can help simplify things a bit and keep MachLabel internal

cranelift/codegen/src/machinst/buffer.rs Show resolved Hide resolved
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

👍

@afonso360 afonso360 added this pull request to the merge queue Sep 18, 2023
Merged via the queue into bytecodealliance:main with commit b23f534 Sep 18, 2023
19 checks passed
@afonso360 afonso360 deleted the riscv-tls-2 branch September 18, 2023 15:25
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 22, 2023
* cranelift: Add support for public labels

* cranelift: Allow targeting labels with relocations

* cranelift: Emit label related relocations in module

* riscv64: Implement TLS GD

* cranelift: Rename `label_is_public` field

* cranelift: Avoid making MachLabel part of the exposed API
@bjorn3 bjorn3 mentioned this pull request Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:riscv64 Issues related to the RISC-V 64 backend. cranelift:module cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants