Skip to content

[RuntimeDyld][ELF] Fix unwanted sign extension. #94482

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

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

al45tair
Copy link
Contributor

@al45tair al45tair commented Jun 5, 2024

Casting the result of Section.getAddressWithOffset() goes wrong if we are on a 32-bit platform whose addresses are regarded as signed; in that case, just doing

(uint64_t)Section.getAddressWithOffset(...)

or

reinterpret_cast<uint64_t>(Section.getAddressWithOffset(...))

will result in sign-extension.

We use these expressions when constructing branch stubs, which is before we know the final load address, so we can just switch to the Section.getLoadAddressWithOffset(...) method instead.

Doing that is also more consistent, since when calculating relative offsets for relocations, we use the load address anyway, so the code currently only works because Section.Address is equal to Section.LoadAddress at this point.

Fixes #94478.

Casting the result of `Section.getAddressWithOffset()` goes wrong if
we are on a 32-bit platform whose addresses are regarded as signed;
in that case, just doing
```
(uint64_t)Section.getAddressWithOffset(...)
```
or
```
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(...))
```
will result in sign-extension.

We use these expressions when constructing branch stubs, which is
before we know the final load address, so we can just switch to the
`Section.getLoadAddressWithOffset(...)` method instead.

Doing that is also more consistent, since when calculating relative
offsets for relocations, we use the load address anyway, so the
code currently only works because `Section.Address` is equal to
`Section.LoadAddress` at this point.

Fixes llvm#94478.
@al45tair al45tair marked this pull request as ready for review June 5, 2024 16:02
@al45tair
Copy link
Contributor Author

al45tair commented Jun 5, 2024

@mgorny Could you try this fix and check that it works for you?

Copy link
Member

@mgorny mgorny left a comment

Choose a reason for hiding this comment

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

I can confirm that the tests pass on x86 with this patch. Thanks!

@lhames lhames self-requested a review June 6, 2024 01:31
Copy link
Contributor

@lhames lhames 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 @al45tair!

@lhames lhames merged commit fb8ea85 into llvm:main Jun 6, 2024
10 checks passed
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.

[RuntimeDyld][ELF] Unwanted sign-extension when computing stub addresses.
3 participants