Skip to content

Commit 0797728

Browse files
authored
Merge pull request #349 from linsinan1995/zcmt-doc
Document relaxation and speical section in zc extension
2 parents 460e2c8 + ecf2772 commit 0797728

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

riscv-elf.adoc

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,16 +949,20 @@ The defined processor-specific section types are listed in <<rv-section-type>>.
949949

950950
[[rv-section]]
951951
.RISC-V-specific sections
952-
[cols="3,3,1"]
952+
[cols="3,3,3"]
953953
[width=80%]
954954
|===
955955
| Name | Type | Attributes
956956

957957
| .riscv.attributes | SHT_RISCV_ATTRIBUTES | none
958+
| .riscv.jvt | SHT_PROGBITS | SHF_ALLOC + SHF_EXECINSTR
958959
|===
959960

960961
+++.riscv.attributes+++ names a section that contains RISC-V ELF attributes.
961962

963+
+++.riscv.jvt+++ is a linker-created section to store table jump
964+
target addresses. The minimum alignment of this section is 64 bytes.
965+
962966
=== Program Header Table
963967

964968
The defined processor-specific segment types are listed in <<rv-seg-type>>.
@@ -1215,6 +1219,7 @@ NOTE: Using address of PLT stubs of the target symbol or address target symbol
12151219
directly will resolve by linker according to the visibility of the target
12161220
symbol.
12171221

1222+
[[compress-func-call-relax]]
12181223
==== Compressed Function Call Relaxation
12191224

12201225
Target Relocation::: R_RISCV_CALL, R_RISCV_CALL_PLT.
@@ -1248,6 +1253,7 @@ Relaxation result:
12481253
----
12491254
--
12501255

1256+
[[compress-tailcall-relax]]
12511257
==== Compressed Tail Call Relaxation
12521258

12531259
Target Relocation::: R_RISCV_CALL, R_RISCV_CALL_PLT.
@@ -1330,6 +1336,7 @@ strongly recommended initialize `gp` at the beginning of the program entry
13301336
function like `_start`, and code fragments of initialization must disable
13311337
linker relaxation to prevent initialization instruction relaxed into a NOP-like
13321338
instruction (e.g. `mv gp, gp`).
1339+
[[gp-relax-asm]]
13331340
[,asm]
13341341
----
13351342
# Recommended way to initialize the gp register.
@@ -1450,6 +1457,81 @@ Relaxation result:
14501457
----
14511458
--
14521459

1460+
==== Table Jump Relaxation
1461+
1462+
Target Relocation::: R_RISCV_CALL, R_RISCV_CALL_PLT, R_RISCV_JAL.
1463+
1464+
Description:: This relaxation type can relax a function call or jump
1465+
instruction into a single table jump instruction with the index of the target
1466+
address in table jump section (<<rv-section>>).
1467+
Before relaxation, the linker scans all relocations and calculates whether
1468+
additional gains can be obtained by using table jump instructions, where
1469+
expected size saving from function-call-related relaxations and the size of jump
1470+
table will be taken into account. If there is no additional gain, then table
1471+
jump relaxation is ignored. Otherwise, this relaxation is switched on.
1472+
<<compress-tailcall-relax, Compressed Tail Call Relaxation>> and
1473+
<<compress-func-call-relax, Compressed Function Call Relaxation>> are
1474+
always prefered during relaxation, since table jump relaxation has no
1475+
extra size saving over these two relaxations and might bring a performance
1476+
overhead.
1477+
1478+
Condition:: The `zcmt` extension is required, the linker output is not
1479+
position-independent and the rd operand of a function call or jump instruction
1480+
is `X0` or `RA`.
1481+
1482+
Relaxation::
1483+
- Instruction sequence associated with `R_RISCV_CALL` or `R_RISCV_CALL_PLT`
1484+
can be rewritten to a table jump instruction.
1485+
- Instruction associated with `R_RISCV_JAL` can be rewritten to a table
1486+
jump instruction.
1487+
Example::
1488+
+
1489+
--
1490+
Relaxation candidate:
1491+
[,asm]
1492+
----
1493+
auipc ra, 0 # R_RISCV_CALL (symbol), R_RISCV_RELAX (symbol)
1494+
jalr ra, ra, 0
1495+
1496+
auipc ra, 0 # R_RISCV_CALL_PLT (symbol), R_RISCV_RELAX (symbol)
1497+
jalr x0, ra, 0
1498+
1499+
jal ra, 0 # R_RISCV_JAL (symbol), R_RISCV_RELAX (symbol)
1500+
1501+
jal x0, 0 # R_RISCV_JAL (symbol), R_RISCV_RELAX (symbol)
1502+
----
1503+
1504+
Relaxation result:
1505+
[,asm]
1506+
----
1507+
cm.jalt <index-for-symbol>
1508+
1509+
cm.jt <index-for-symbol>
1510+
1511+
cm.jalt <index-for-symbol>
1512+
----
1513+
--
1514+
1515+
NOTE: The `zcmt` extension cannot be used in position-independent binaries.
1516+
1517+
NOTE: Jump or call instructions with the rd operand `RA` will be relaxed into
1518+
`cm.jalt` and instructions with the rd operand `X0` will be relaxed into
1519+
`cm.jt`. The table jump section holds target addresses for these two
1520+
instructions separately. More details are available in the _ZC* extension
1521+
specification_ <<riscv-zc-extension-group>>.
1522+
1523+
NOTE: This relaxation requires programs to initialize the `jvt` CSR with the
1524+
address of the `__jvt_base$` symbol before executing table jump
1525+
instructions. It is recommended to initialize `jvt` CSR immediately after
1526+
<<gp-relax-asm,global pointer initialization>>.
1527+
[,asm]
1528+
----
1529+
# Recommended way to initialize the jvt CSR.
1530+
1: auipc a0, %pcrel_hi(__jvt_base$)
1531+
addi a0, a0, %pcrel_lo(1b)
1532+
csrw jvt, a0
1533+
----
1534+
14531535
[bibliography]
14541536
== References
14551537

@@ -1468,3 +1550,6 @@ https://www.akkadia.org/drepper/tls.pdf, Ulrich Drepper
14681550
* [[[riscv-unpriv]]] "The RISC-V Instruction Set Manual, Volume I: User-Level
14691551
ISA, Document", Editors Andrew Waterman and Krste Asanovi´c,
14701552
RISC-V International.
1553+
1554+
* [[[riscv-zc-extension-group]]] "ZC* extension specification"
1555+
https://github.com/riscv/riscv-code-size-reduction

0 commit comments

Comments
 (0)