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

Add RWPI/ROPI relocation model support #41560

Merged
merged 2 commits into from
May 1, 2017
Merged

Conversation

alevy
Copy link
Contributor

@alevy alevy commented Apr 26, 2017

This PR adds support for using LLVM 4's ROPI and RWPI relocation models for ARM.

ROPI (Read-Only Position Independence) and RWPI (Read-Write Position Independence) are two new relocation models in LLVM for the ARM backend (LLVM changset). The motivation is that these are the specific strategies we use in userspace Tock apps, so supporting this is an important step (perhaps the final step, but can't confirm yet) in enabling userspace Rust processes.

Explanation

ROPI makes all code and immutable accesses PC relative, but not assumed to be overriden at runtime (so for example, jumps are always relative).

RWPI uses a base register (r9) that stores the addresses of the GOT in memory so the runtime (e.g. a kernel) only adjusts r9 tell running code where the GOT is.

Complications adding support in Rust

While this landed in LLVM master back in August, the header files in llvm-c have not been updated yet to reflect it. Rust replicates that header file's version of the LLVMRelocMode enum as the Rust enum llvm::RelocMode and uses an implicit cast in the ffi to translate from Rust's notion of the relocation model to the LLVM library's notion.

My workaround for this currently is to replace the LLVMRelocMode argument to LLVMTargetMachineRef with an int and using the hardcoded int representation of the RelocMode enum. This is A Bad Idea(tm), but I think very nearly the right thing.

Would a better alternative be to patch rust-llvm to support these enum variants (also a fairly trivial change)?

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

Adds support for using LLVM 4's ROPI and RWPI relocation models for ARM
@nikomatsakis
Copy link
Contributor

Hmm, this is a bit out of my wheelhouse. Perhaps @nagisa, @eddyb, or @michaelwoerister would be a better fit?

@nikomatsakis
Copy link
Contributor

I didn't realize how short this PR is. @alevy, I think the main question is: is there any way to write a test for this?

@eddyb
Copy link
Member

eddyb commented Apr 27, 2017

@alevy You should repeat what we did for other LLVMRust types, e.g. LLVMRustCodeModel.
That way it's really straight-forward to check that the names and values match up.

@alexcrichton alexcrichton added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 27, 2017
@alevy
Copy link
Contributor Author

alevy commented Apr 28, 2017

@eddyb thanks for the suggestion, I implemented something like LLVMRelocCodeMode, although due to compatibility issues with LLVM <= 3.8, there is a little bit of ifdef hell that seems required. I'm happy to change following any suggestions you have (particularly since I'm not sure I've captured the intent of the original ifdefs).

@nikomatsakis Tests would be nice, I'm just not sure where to put them. A quick grep for LLVMRelocCodeMode doesn't show up with any tests. Any pointers for where to place them?

LLVMRustRelocModeDynamicNoPic,
LLVMRustRelocModeROPI,
LLVMRustRelocModeRWPI,
LLVMRustRelocModeROPIRWPI,
Copy link
Member

Choose a reason for hiding this comment

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

You don't need the prefix on each one, that's why it's enum class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh duh.... fixed.

@eddyb
Copy link
Member

eddyb commented Apr 28, 2017

@alevy I think the ifdefs look fine, there's mainly one style problem I mentioned.

@@ -288,6 +288,9 @@ pub enum RelocMode {
Static = 1,
PIC = 2,
DynamicNoPic = 3,
ROPI = 4,
RWPI = 5,
ROPI_RWPI = 6,
Copy link
Member

Choose a reason for hiding this comment

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

If you won't have numbers in the C++ bindings, you should remove them here, btw.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, do you think it makes more sense to add numbers in the C++ or remove them here? (the other C++ enums don't have them, so I'm assuming remove them here)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, removing is fine. As long as they look the same, they'll behave the same.

Replaces the llvm-c exposed LLVMRelocMode, which does not include all
relocation model variants, with a LLVMRustRelocMode modeled after
LLVMRustCodeMode.
@alevy
Copy link
Contributor Author

alevy commented Apr 29, 2017

OK @eddyb I think I've addressed your comments. Thanks!

@eddyb
Copy link
Member

eddyb commented Apr 29, 2017

@bors r+

@alevy
Copy link
Contributor Author

alevy commented May 1, 2017

@bors doesn't seem to like this PR for some reason?

@eddyb
Copy link
Member

eddyb commented May 1, 2017

@bors r+

@alexcrichton
Copy link
Member

@bors: r=eddyb

@bors
Copy link
Contributor

bors commented May 1, 2017

📌 Commit 0f00f27 has been approved by eddyb

@bors
Copy link
Contributor

bors commented May 1, 2017

⌛ Testing commit 0f00f27 with merge 4cb396c...

bors added a commit that referenced this pull request May 1, 2017
Add RWPI/ROPI relocation model support

This PR adds support for using LLVM 4's ROPI and RWPI relocation models for ARM.

ROPI (Read-Only Position Independence) and RWPI (Read-Write Position Independence) are two new relocation models in LLVM for the ARM backend ([LLVM changset](https://reviews.llvm.org/rL278015)). The motivation is that these are the specific strategies we use in userspace [Tock](https://www.tockos.org) apps, so supporting this is an important step (perhaps the final step, but can't confirm yet) in enabling userspace Rust processes.

## Explanation

ROPI makes all code and immutable accesses PC relative, but not assumed to be overriden at runtime (so for example, jumps are always relative).

RWPI uses a base register (`r9`) that stores the addresses of the GOT in memory so the runtime (e.g. a kernel) only adjusts r9 tell running code where the GOT is.

## Complications adding support in Rust

While this landed in LLVM master back in August, the header files in `llvm-c` have not been updated yet to reflect it. Rust replicates that header file's version of the `LLVMRelocMode` enum as the Rust enum `llvm::RelocMode` and uses an implicit cast in the ffi to translate from Rust's notion of the relocation model to the LLVM library's notion.

My workaround for this currently is to replace the `LLVMRelocMode` argument to `LLVMTargetMachineRef` with an int and using the hardcoded int representation of the `RelocMode` enum. This is A Bad Idea(tm), but I think very nearly the right thing.

Would a better alternative be to patch rust-llvm to support these enum variants (also a fairly trivial change)?
@bors
Copy link
Contributor

bors commented May 1, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 4cb396c to master...

@bors bors merged commit 0f00f27 into rust-lang:master May 1, 2017
@alevy alevy deleted the rwpi-ropi branch December 26, 2018 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants