Skip to content

Commit 3999822

Browse files
committed
Add a new lga pseudo instruction to mirror lla
There are cases where using the GOT even for non-PIC code is desirable, such as when getting the address of a possibly-undefined weak symbol. This addition provides a clean way to express that, with la now just being an alias for either lla or lga depending on -fPIC. Closes: #50
1 parent 0bc55c5 commit 3999822

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

riscv-asm.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,7 @@ assembled as PIC:
325325

326326
For non-PIC this is an alias for the `lla` pseudo instruction documented below.
327327

328-
For PIC this generates the following instructions and relocations as seen by
329-
`objdump` (for RV64; RV32 will use `lw` instead of `ld`):
330-
331-
```assembly
332-
0000000000000000 <.text>:
333-
0: 00000517 auipc a0,0x0
334-
0: R_RISCV_GOT_HI20 msg+0x1
335-
4: 00053503 ld a0,0(a0) # 0 <.text>
336-
4: R_RISCV_PCREL_LO12_I .L0
337-
```
328+
For PIC this is an alias for the `lga` pseudo instruction documented below.
338329

339330
The `la` pseudo instruction is the preferred way for getting the address of
340331
variables in assembly unless explicit control over PC-relative or GOT-indirect
@@ -360,6 +351,27 @@ This generates the following instructions and relocations as seen by `objdump`:
360351
4: R_RISCV_PCREL_LO12_I .L0
361352
```
362353

354+
Load Global Address
355+
------------------
356+
357+
The following example shows the `lga` pseudo instruction which is used to load
358+
global symbol addresses:
359+
360+
```assembly
361+
lga a0, msg + 1
362+
```
363+
364+
This generates the following instructions and relocations as seen by `objdump`
365+
(for RV64; RV32 will use `lw` instead of `ld`):
366+
367+
```assembly
368+
0000000000000000 <.text>:
369+
0: 00000517 auipc a0,0x0
370+
0: R_RISCV_GOT_HI20 msg+0x1
371+
4: 00053503 ld a0,0(a0) # 0 <.text>
372+
4: R_RISCV_PCREL_LO12_I .L0
373+
```
374+
363375
Load and Store Global
364376
---------------------
365377

@@ -559,6 +571,7 @@ Pseudoinstruction | Base Instruction(s)
559571
la rd, symbol | auipc rd, symbol[31:12]; addi rd, rd, symbol[11:0] | Load address | Without -fPIC
560572
la rd, symbol | auipc rd, symbol@GOT[31:12]; l{w\|d} rd, symbol@GOT[11:0]\(rd\) | Load address | With -fPIC
561573
lla rd, symbol | auipc rd, symbol[31:12]; addi rd, rd, symbol[11:0] | Load local address
574+
lga rd, symbol | auipc rd, symbol@GOT[31:12]; l{w\|d} rd, symbol@GOT[11:0]\(rd\) | Load global address
562575
l{b\|h\|w\|d} rd, symbol | auipc rd, symbol[31:12]; l{b\|h\|w\|d} rd, symbol[11:0]\(rd\) | Load global
563576
s{b\|h\|w\|d} rd, symbol, rt | auipc rt, symbol[31:12]; s{b\|h\|w\|d} rd, symbol[11:0]\(rt\) | Store global
564577
fl{w\|d} rd, symbol, rt | auipc rt, symbol[31:12]; fl{w\|d} rd, symbol[11:0]\(rt\) | Floating-point load global

0 commit comments

Comments
 (0)