Skip to content

Commit 41edcc7

Browse files
committed
riscv-elf.md: add new definitions for the compact code model
Add the basic structures to support the compact code model.
1 parent f58d5dc commit 41edcc7

File tree

1 file changed

+111
-62
lines changed

1 file changed

+111
-62
lines changed

riscv-elf.md

Lines changed: 111 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [ILP32E Calling Convention](#ilp32e-calling-convention)
1111
* [Named ABIs](#named-abis)
1212
* [Default ABIs](#default-abis)
13+
* [Code models](#code-models)
1314
3. [C type details](#c-types)
1415
* [C type sizes and alignments](#c-type-sizes)
1516
* [C type representations](#c-type-representation)
@@ -320,6 +321,41 @@ default ABIs for specific architectures:
320321

321322
* **on RV32G**: [ILP32D](#abi-ilp32d)
322323

324+
## <a name=code-models /> Code models
325+
326+
The RISC-V architecture constrains the addressing of positions in the
327+
address space. There is no single instruction that can refer to an arbitrary
328+
memory position using a literal as its argument. Rather, instructions exist
329+
that, when combined together, can then be used to refer to a memory position
330+
via its literal. And, when not, other data structures are used to help the
331+
code to address the memory space. The coding conventions governing their use
332+
are known as code models.
333+
334+
### Medium low
335+
336+
The medium low code model, or `medlow`, allows the code to address the lower
337+
2 GiB of the address space. By using the instruction `lui` and `ld` or `addi`
338+
instructions, for example, a 32-bit address literal can be produced.
339+
This code model is not position independent.
340+
341+
### Meidum any
342+
343+
The medium any code model, or `medany`, allows the code to address the range
344+
between -2 GiB and +2 GiB from its position. By using the instruction `auipc`
345+
and `st` or `addi` instructions, for example, a signed 32-bit offset, relative
346+
to the value of the `pc` register, can be produced. This code model is
347+
position independent.
348+
349+
### Compact
350+
351+
The compact code model allows the code to address the whole 64-bit address space,
352+
especially when code and data are located far apart. By using the Global
353+
Offset Table, or GOT, to hold the 64-bit address literals, any memory position
354+
can be referred. By using the instructions `lui` and `addi`, a signed 32-bit
355+
offset, relative to the value of the `gp` register, can be produced, referring
356+
to address literals in the GOT. This code model is position independent.
357+
Does not apply to the ILP32 ABIs.
358+
323359
# <a name=c-types></a> C type details
324360
## <a name=c-type-sizes></a> C type sizes and alignments
325361

@@ -413,9 +449,9 @@ rules about 2✕XLEN aligned arguments being passed in "aligned" register pairs.
413449
* e_flags: Describes the format of this ELF file. These flags are used by the
414450
linker to disallow linking ELF files with incompatible ABIs together.
415451

416-
Bit 0 | Bit 1 - 2 | Bit 3 | Bit 4 | Bit 5 - 31
417-
-------|------------|-------|-------|------------
418-
RVC | Float ABI | RVE | TSO | *Reserved*
452+
Bit 0 | Bit 1 - 2 | Bit 3 | Bit 4 | Bit 5 | Bit 6 - 31
453+
-------|------------|-------|-------|---------|------------
454+
RVC | Float ABI | RVE | TSO | Compact | *Reserved*
419455

420456

421457
* EF_RISCV_RVC (0x0001): This bit is set when the binary targets the C ABI,
@@ -441,6 +477,8 @@ rules about 2✕XLEN aligned arguments being passed in "aligned" register pairs.
441477
* EF_RISCV_RVE (0x0008): This bit is set when the binary targets the E ABI.
442478
* EF_RISCV_TSO (0x0010): This bit is set when the binary requires the RVTSO
443479
memory consistency model.
480+
* EF_RISCV_COMPACT (0x0020): This bit is set when the binary targets the
481+
compact code model.
444482

445483
Until such a time that the *Reserved* bits (0xffffffe0) are allocated by
446484
future versions of this specification, they shall not be set by standard
@@ -469,65 +507,76 @@ Global Offset Table or DWARF meta data.
469507
The following table provides details of the RISC-V ELF relocations (instruction
470508
specific relocations show the instruction type in the Details column):
471509

472-
Enum | ELF Reloc Type | Description | Field | Calculation | Details
473-
:--- | :------------------ | :--------------- | :---- | :---------- | :-------
474-
0 | R_RISCV_NONE | None |
475-
1 | R_RISCV_32 | Runtime relocation | _word32_ | S + A
476-
2 | R_RISCV_64 | Runtime relocation | _word64_ | S + A
477-
3 | R_RISCV_RELATIVE | Runtime relocation | _wordclass_ | B + A
478-
4 | R_RISCV_COPY | Runtime relocation | | | Must be in executable; not allowed in shared library
479-
5 | R_RISCV_JUMP_SLOT | Runtime relocation | _wordclass_ | S | Handled by PLT unless `LD_BIND_NOW`
480-
6 | R_RISCV_TLS_DTPMOD32 | TLS relocation | _word32_ | S->TLSINDEX
481-
7 | R_RISCV_TLS_DTPMOD64 | TLS relocation | _word64_ | S->TLSINDEX
482-
8 | R_RISCV_TLS_DTPREL32 | TLS relocation | _word32_ | S + A + TLS - TLS_TP_OFFSET
483-
9 | R_RISCV_TLS_DTPREL64 | TLS relocation | _word64_ | S + A + TLS - TLS_TP_OFFSET
484-
10 | R_RISCV_TLS_TPREL32 | TLS relocation | _word32_ | S + A + TLS + S_TLS_OFFSET - TLS_DTV_OFFSET
485-
11 | R_RISCV_TLS_TPREL64 | TLS relocation | _word64_ | S + A + TLS + S_TLS_OFFSET - TLS_DTV_OFFSET
486-
16 | R_RISCV_BRANCH | PC-relative branch | _B-Type_ | S + A - P
487-
17 | R_RISCV_JAL | PC-relative jump | _J-Type_ | S + A - P
488-
18 | R_RISCV_CALL | PC-relative call | _J-Type_ | S + A - P | Macros `call`, `tail`
489-
19 | R_RISCV_CALL_PLT | PC-relative call (PLT) | _J-Type_ | S + A - P | Macros `call`, `tail` (PIC)
490-
20 | R_RISCV_GOT_HI20 | PC-relative GOT reference | _U-Type_ | G + A | `%got_pcrel_hi(symbol)`
491-
21 | R_RISCV_TLS_GOT_HI20 | PC-relative TLS IE GOT offset | _U-Type_ | | Macro `la.tls.ie`
492-
22 | R_RISCV_TLS_GD_HI20 | PC-relative TLS GD reference | _U-Type_ | | Macro `la.tls.gd`
493-
23 | R_RISCV_PCREL_HI20 | PC-relative reference | _U-Type_ | S + A - P | `%pcrel_hi(symbol)`
494-
24 | R_RISCV_PCREL_LO12_I | PC-relative reference | _I-type_ | S + A - P | `%pcrel_lo(address of %pcrel_hi)`
495-
25 | R_RISCV_PCREL_LO12_S | PC-relative reference | _S-Type_ | S + A - P | `%pcrel_lo(address of %pcrel_hi)`
496-
26 | R_RISCV_HI20 | Absolute address | _U-Type_ | S + A | `%hi(symbol)`
497-
27 | R_RISCV_LO12_I | Absolute address | _I-Type_ | S + A | `%lo(symbol)`
498-
28 | R_RISCV_LO12_S | Absolute address | _S-Type_ | S + A | `%lo(symbol)`
499-
29 | R_RISCV_TPREL_HI20 | TLS LE thread offset | _U-Type_ | | `%tprel_hi(symbol)`
500-
30 | R_RISCV_TPREL_LO12_I | TLS LE thread offset | _I-Type_ | | `%tprel_lo(symbol)`
501-
31 | R_RISCV_TPREL_LO12_S | TLS LE thread offset | _S-Type_ | | `%tprel_lo(symbol)`
502-
32 | R_RISCV_TPREL_ADD | TLS LE thread usage | | | `%tprel_add(symbol)`
503-
33 | R_RISCV_ADD8 | 8-bit label addition | _word8_ | V + S + A
504-
34 | R_RISCV_ADD16 | 16-bit label addition | _word16_ | V + S + A
505-
35 | R_RISCV_ADD32 | 32-bit label addition | _word32_ | V + S + A
506-
36 | R_RISCV_ADD64 | 64-bit label addition | _word64_ | V + S + A
507-
37 | R_RISCV_SUB8 | 8-bit label subtraction | _word8_ | V - S - A
508-
38 | R_RISCV_SUB16 | 16-bit label subtraction | _word16_ | V - S - A
509-
39 | R_RISCV_SUB32 | 32-bit label subtraction | _word32_ | V - S - A
510-
40 | R_RISCV_SUB64 | 64-bit label subtraction | _word64_ | V - S - A
511-
41 | R_RISCV_GNU_VTINHERIT | GNU C++ vtable hierarchy |
512-
42 | R_RISCV_GNU_VTENTRY | GNU C++ vtable member usage |
513-
43 | R_RISCV_ALIGN | Alignment statement |
514-
44 | R_RISCV_RVC_BRANCH | PC-relative branch offset | _CB-Type_ | S + A - P
515-
45 | R_RISCV_RVC_JUMP | PC-relative jump offset | _CJ-Type_ | S + A - P
516-
46 | R_RISCV_RVC_LUI | Absolute address | _CI-Type_ | S + A
517-
47 | R_RISCV_GPREL_I | GP-relative reference | _I-Type_ | S + A - GP
518-
48 | R_RISCV_GPREL_S | GP-relative reference | _S-Type_ | S + A - GP
519-
49 | R_RISCV_TPREL_I | TP-relative TLS LE load | _I-Type_
520-
50 | R_RISCV_TPREL_S | TP-relative TLS LE store | _S-Type_
521-
51 | R_RISCV_RELAX | Instruction pair can be relaxed |
522-
52 | R_RISCV_SUB6 | Local label subtraction | _word6_ | V - S - A
523-
53 | R_RISCV_SET6 | Local label assignment | _word6_ | S + A
524-
54 | R_RISCV_SET8 | Local label assignment | _word8_ | S + A
525-
55 | R_RISCV_SET16 | Local label assignment | _word16_ | S + A
526-
56 | R_RISCV_SET32 | Local label assignment | _word32_ | S + A
527-
57 | R_RISCV_32_PCREL | PC-relative reference | _word32_ | S + A - P
528-
58 | R_RISCV_IRELATIVE | Runtime relocation | _wordclass_ | `ifunc_resolver(B + A)`
529-
59-191 | *Reserved* | Reserved for future standard use
530-
192-255 | *Reserved* | Reserved for nonstandard ABI extensions
510+
Enum | ELF Reloc Type | Description | Field | Calculation | Details
511+
:--- | :------------- | :---------- | :---- | :---------- | :------
512+
0 | R_RISCV_NONE | None |
513+
1 | R_RISCV_32 | Runtime relocation | _word32_ | S + A
514+
2 | R_RISCV_64 | Runtime relocation | _word64_ | S + A
515+
3 | R_RISCV_RELATIVE | Runtime relocation | _wordclass_ | B + A
516+
4 | R_RISCV_COPY | Runtime relocation | | | Must be in executable. not allowed in shared library
517+
5 | R_RISCV_JUMP_SLOT | Runtime relocation | _wordclass_ | S | Handled by PLT unless `LD_BIND_NOW`
518+
6 | R_RISCV_TLS_DTPMOD32 | TLS relocation | _word32_ | S->TLSINDEX
519+
7 | R_RISCV_TLS_DTPMOD64 | TLS relocation | _word64_ | S->TLSINDEX
520+
8 | R_RISCV_TLS_DTPREL32 | TLS relocation | _word32_ | S + A + TLS - TLS_TP_OFFSET
521+
9 | R_RISCV_TLS_DTPREL64 | TLS relocation | _word64_ | S + A + TLS - TLS_TP_OFFSET
522+
10 | R_RISCV_TLS_TPREL32 | TLS relocation | _word32_ | S + A + TLS + S_TLS_OFFSET - TLS_DTV_OFFSET
523+
11 | R_RISCV_TLS_TPREL64 | TLS relocation | _word64_ | S + A + TLS + S_TLS_OFFSET - TLS_DTV_OFFSET
524+
16 | R_RISCV_BRANCH | PC-relative branch | _B-Type_ | S + A - P
525+
17 | R_RISCV_JAL | PC-relative jump | _J-Type_ | S + A - P
526+
18 | R_RISCV_CALL | PC-relative call | _J-Type_ | S + A - P | Macros `call`, `tail`
527+
19 | R_RISCV_CALL_PLT | PC-relative call (PLT) | _J-Type_ | S + A - P | Macros `call`, `tail` (PIC)
528+
20 | R_RISCV_GOT_HI20 | PC-relative GOT reference | _U-Type_ | G + A | `%got_pcrel_hi(symbol)`
529+
21 | R_RISCV_TLS_GOT_HI20 | PC-relative TLS IE GOT offset | _U-Type_ | | Macro `la.tls.ie`
530+
22 | R_RISCV_TLS_GD_HI20 | PC-relative TLS GD reference | _U-Type_ | | Macro `la.tls.gd`
531+
23 | R_RISCV_PCREL_HI20 | PC-relative reference | _U-Type_ | S + A - P | `%pcrel_hi(symbol)`
532+
24 | R_RISCV_PCREL_LO12_I | PC-relative reference | _I-type_ | S + A - P | `%pcrel_lo(address of %pcrel_hi)`
533+
25 | R_RISCV_PCREL_LO12_S | PC-relative reference | _S-Type_ | S + A - P | `%pcrel_lo(address of %pcrel_hi)`
534+
26 | R_RISCV_HI20 | Absolute address | _U-Type_ | S + A | `%hi(symbol)`
535+
27 | R_RISCV_LO12_I | Absolute address | _I-Type_ | S + A | `%lo(symbol)`
536+
28 | R_RISCV_LO12_S | Absolute address | _S-Type_ | S + A | `%lo(symbol)`
537+
29 | R_RISCV_TPREL_HI20 | TLS LE thread offset | _U-Type_ | | `%tprel_hi(symbol)`
538+
30 | R_RISCV_TPREL_LO12_I | TLS LE thread offset | _I-Type_ | | `%tprel_lo(symbol)`
539+
31 | R_RISCV_TPREL_LO12_S | TLS LE thread offset | _S-Type_ | | `%tprel_lo(symbol)`
540+
32 | R_RISCV_TPREL_ADD | TLS LE thread usage | | | `%tprel_add(symbol)`
541+
33 | R_RISCV_ADD8 | 8-bit label addition | _word8_ | S + A + V
542+
34 | R_RISCV_ADD16 | 16-bit label addition | _word16_ | S + A + V
543+
35 | R_RISCV_ADD32 | 32-bit label addition | _word32_ | S + A + V
544+
36 | R_RISCV_ADD64 | 64-bit label addition | _word64_ | S + A + V
545+
37 | R_RISCV_SUB8 | 8-bit label subtraction | _word8_ | V - S - A
546+
38 | R_RISCV_SUB16 | 16-bit label subtraction | _word16_ | V - S - A
547+
39 | R_RISCV_SUB32 | 32-bit label subtraction | _word32_ | V - S - A
548+
40 | R_RISCV_SUB64 | 64-bit label subtraction | _word64_ | V - S - A
549+
41 | R_RISCV_GNU_VTINHERIT | GNU C++ vtable hierarchy |
550+
42 | R_RISCV_GNU_VTENTRY | GNU C++ vtable member usage |
551+
43 | R_RISCV_ALIGN | Alignment statement |
552+
44 | R_RISCV_RVC_BRANCH | PC-relative branch offset | _CB-Type_ | S + A - P
553+
45 | R_RISCV_RVC_JUMP | PC-relative jump offset | _CJ-Type_ | S + A - P
554+
46 | R_RISCV_RVC_LUI | Absolute address | _CI-Type_ | S + A - P
555+
47 | R_RISCV_GPREL_I | GP-relative reference | _I-Type_ | S + A - GP
556+
48 | R_RISCV_GPREL_S | GP-relative reference | _S-Type_ | S + A - GP
557+
49 | R_RISCV_TPREL_I | TP-relative TLS LE load | _I-Type_
558+
50 | R_RISCV_TPREL_S | TP-relative TLS LE store | _S-Type_
559+
51 | R_RISCV_RELAX | Instruction pair can be relaxed |
560+
52 | R_RISCV_SUB6 | Local label subtraction |
561+
53 | R_RISCV_SET6 | Local label subtraction |
562+
54 | R_RISCV_SET8 | Local label subtraction |
563+
55 | R_RISCV_SET16 | Local label subtraction |
564+
56 | R_RISCV_SET32 | Local label subtraction |
565+
57 | R_RISCV_32_PCREL | PC-relative reference | _word32_ | S + A - P
566+
58 | R_RISCV_IRELATIVE | Runtime relocation | _wordclass_ | `ifunc_resolver(B + A)`
567+
59 | R_RISCV_64_PCREL | PC-relative reference | _word64_ | S + A - P
568+
60 | R_RISCV_GPREL_HI20 | GP-relative reference | _U-type_ | S + A - GP | `%gprel_hi(symbol)`
569+
61 | R_RISCV_GPREL_LO12_I | GP-relative reference | _I-type_ | S + A - GP | `%gprel_lo(symbol)`
570+
62 | R_RISCV_GPREL_LO12_S | GP-relative reference | _S-type_ | S + A - GP | `%gprel_lo(symbol)`
571+
63 | R_RISCV_GPREL_ADD | GP-relative usage | | | `%gprel(symbol)`
572+
64 | R_RISCV_GPREL_LOAD | GP-relative usage | | | `%gprel(symbol)`
573+
65 | R_RISCV_GOT_GPREL_HI20 | GP-relative GOT reference | _U-type_ | G + A - GP | `%got_gprel_hi(symbol)`
574+
66 | R_RISCV_GOT_GPREL_LO12_I | GP-relative GOT reference | _I-type_ | G + A - GP | `%got_gprel_lo(symbol)`
575+
67 | R_RISCV_GOT_GPREL_ADD | GP-relative GOT usage | | | `%got_gprel(symbol)`
576+
68 | R_RISCV_GOT_GPREL_LOAD | GP-relative GOT usage | | | `%got_gprel(symbol)`
577+
69 | R_RISCV_GOT_GPREL_STORE | GP-relative GOT usage | | | `%got_gprel(symbol)`
578+
70-191 | *Reserved* | Reserved for future standard use |
579+
192-255 | *Reserved* | Reserved for nonstandard ABI extensions |
531580

532581
Nonstandard extensions are free to use relocation numbers 192-255 for any
533582
purpose. These relocations may conflict with other nonstandard extensions.

0 commit comments

Comments
 (0)