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 support for relocations #39

Merged
merged 15 commits into from
Nov 28, 2024
Merged

Conversation

janvrany
Copy link
Collaborator

This PR adds basic support for relocations.

At the moment, this PR is work in progress. I'm still not 100% happy with it - design wise - but this should be enough to enable further experiments with TR and AoT withing ST25.

One thing that is probably missing is to add proper support for code object sections, so compiler can for example produce jump table for PICs in "data" section or big constants or global variable addresses in "rodata".

Once finalized, this would probably need to be split into 2 or three smallter PRs.

@shingarov
Copy link
Owner

Hmm… looking at this code, I am now inclined to re-read Stephen Kell's The Missing Link, much more carefully this time.

@janvrany janvrany force-pushed the pr/add-relocations branch 2 times, most recently from 88229db to 282fbd9 Compare June 24, 2024 11:42
@janvrany janvrany force-pushed the pr/add-relocations branch from 282fbd9 to c857bd7 Compare June 27, 2024 10:48
@janvrany janvrany force-pushed the pr/add-relocations branch from c857bd7 to 97f5adb Compare August 5, 2024 12:32
This commit add `AcDSLCodeObject >> exports` that return symbols
"exported" by this code object, that is, a dictionary mapping
exported symbol to its address (respecting object's base address).
Each exported symbol must be paired with corresponding label in
instructions sequence.

To export a symbol, `AcDSLAssembler` provides method `global: symbol`
just like GNU assembler has .global directive [1].

[1]: https://sourceware.org/binutils/docs/as/Global.html
This commit adds basic support for relocation entries.

Each processor instruction can have a relocation entry. A relocation
entry is a subclass of `AcRelocation` (one subclass per relocation type)
and contains a (relocatable) symbol.

Particular relocation type (a concrete subclass of `AcRelocation`) is
valid only for some instructions (`#isValidForInstruction`) and knows
how to fixup (patch) that instruction, knowing the final address of its
symbol (note, that this final address might be address of trampoline or
into address table if final address is beyond the maximum displacement).

In some cases, relocation entries must come in pairs (like %lo must
follow %hi). Currently, there's no way to ensure nor validate that -
this is user's responsibility.
This commit adds relocation support to DSL assembler. To to relocate
an operand, use `relocation_type % operand`, for example:

    asm auipc: ra, ra, R_RISCV_PCREL_HI20 % label
This commit adds pool with `*_MIN` and `*_MAX` values for fixed-length
integers (both signed and unsigned). Using them rather than integer
literals improve readability.
This commit adds pool containing various constants related to RISC-V
ISAm such as bit-size of immediates, min / max values of those and so
on to improve readability of code.
This commit add basic support for GOT accesses. During relocation
processing (linking) a relocation may request GOT entry for its symbol
by

    gotentry := AcRelocationRequestGOTEntry query: self

The response (`gotentry` in the above example) is an association from
GOT entry symbol to its address (that is, address of the entry). Actual
allocation of GOT entries is the responsibility of the linker.
This commit add basic support for PLT calls, using similar mechanism
as for GOT entries introduced in previous commit.
This commit adds `R_RISCV_GOT_HI20` relocation class and fixes
`R_RISCV_PCREL_LO12_*` to also work with it. This allows consumers to
generate GOT access for external symbols.
This commit add simple example showing how to use relocations when
using DSL assembler.
@janvrany janvrany marked this pull request as ready for review November 28, 2024 15:24
@janvrany janvrany merged commit 2bb183e into shingarov:pure-z3 Nov 28, 2024
2 checks passed
@janvrany janvrany deleted the pr/add-relocations branch November 28, 2024 15:28
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.

2 participants