Skip to content

Commit 570e4e1

Browse files
author
Evandro Menezes
committed
riscv-elf.md: update the PLT
Add the PLT entries for the compact code model.
1 parent d188bd1 commit 570e4e1

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

riscv-elf.md

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,10 @@ GOT offsets lazily on the first call to any function, except when
700700
`LD_BIND_NOW` is set in which case the GOT entries are populated by the
701701
dynamic linker before the executable is started. Lazy resolution of GOT
702702
entries is intended to speed up program loading by deferring symbol
703-
resolution to the first time the function is called. The first entry
704-
in the PLT occupies two 16 byte entries:
703+
resolution to the first time the function is called.
704+
705+
For the small and medium code models, the first entry in the PLT occupies
706+
two 16 byte entries:
705707

706708
```
707709
1: auipc t2, %pcrel_hi(.got.plt)
@@ -714,18 +716,51 @@ in the PLT occupies two 16 byte entries:
714716
jr t3
715717
```
716718

717-
Subsequent function entry stubs in the PLT take up 16 bytes and load a
718-
function pointer from the GOT. On the first call to a function, the
719-
entry redirects to the first PLT entry which calls `_dl_runtime_resolve`
720-
and fills in the GOT entry for subsequent calls to the function:
719+
For the compact code model, the third entry in the PLT has a stub that
720+
calculates the absolute address of a function pointer in the GOT.
721+
It occupies three 16 byte entries:
722+
723+
```
724+
1: auipc t0, %hi_pcrel(2f) # address of 2f
725+
addi t0, %lo_pcrel(1b)
726+
ld t2, (t0) # difference between .got.plt - 2f
727+
add t0, t0, t2 # address of .got.plt
728+
add t0, t0, t3 # address of the function pointer
729+
ld t3, (t0) # dereference the function pointer
730+
jr t3
731+
nop
732+
nop
733+
nop
734+
2: .quad .got.plt - ., 0
735+
```
736+
737+
For the small and medium code models, subsequent function entries in the PLT
738+
take up 16 bytes and load a function pointer from the GOT.
739+
On the first call to a function, the entry redirects to the first PLT entr
740+
which calls `_dl_runtime_resolve` and fills in the GOT entry
741+
for subsequent calls to the function:
721742

722743
```
723-
1: auipc t3, %pcrel_hi([email protected])
724-
l[w|d] t3, %pcrel_lo(1b)(t3)
744+
1: auipc t3, %pcrel_hi([email protected]) # address of the function pointer
745+
l[w|d] t3, %pcrel_lo(1b)(t3) # dereference the function pointer
725746
jalr t1, t3
726747
nop
727748
```
728749

750+
For the compact code model, subsequent function entries in the PLT
751+
take up 16 bytes and load a function pointer from the GOT.
752+
On the first call to a function, the entry redirects to the first PLT entr
753+
which calls `_dl_runtime_resolve` and fills in the GOT entry
754+
for subsequent calls to the function:
755+
756+
```
757+
1: lui t3, %hi([email protected] - .got.plt) # offset to the function pointer
758+
addi t3, %lo([email protected] - .got.plt)
759+
760+
nop
761+
762+
```
763+
729764

730765
### Procedure Calls
731766

0 commit comments

Comments
 (0)