From 371ee1469cf30ecdbc8d1b55cf307a310ff3d630 Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Thu, 12 Sep 2024 20:03:59 +0800 Subject: cmd/link,cmd/internal: add R_GOT_PCREL_ITYPE_RELOC for riscv64 This CL adds new relocation type for riscv64: R_GOT_PCREL_ITYPE_RELOC which generate an AUIPC + I-type pair with relocation type of GOT_HI20 and PCREL_LO12_I. According to RISCV elf psabi doc, medium position independent code model, the GNU as example is: ``` # Calculate address of non-local symbol .Ltmp3: aupipc a0, %got_pcrel_hi(symbol) ld a0, %pcrel_lo(.Ltmp3)(a0) ``` Change-Id: I719dd05e009ca2d9291f0689b346c059f9c56918 Reviewed-on: https://go-review.googlesource.com/c/go/+/612635 Reviewed-by: Dmitri Shuralyov Reviewed-by: Joel Sing Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- src/cmd/internal/obj/riscv/cpu.go | 5 +++++ src/cmd/internal/obj/riscv/obj.go | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'src/cmd/internal/obj') diff --git a/src/cmd/internal/obj/riscv/cpu.go b/src/cmd/internal/obj/riscv/cpu.go index 69a8516696..2b75ed38a6 100644 --- a/src/cmd/internal/obj/riscv/cpu.go +++ b/src/cmd/internal/obj/riscv/cpu.go @@ -317,6 +317,11 @@ const ( // it is the first instruction in an AUIPC + S-type pair that needs a // R_RISCV_PCREL_STYPE relocation. NEED_PCREL_STYPE_RELOC + + // NEED_GOT_PCREL_ITYPE_RELOC is set on AUIPC instructions to indicate that + // it is the first instruction in an AUIPC + I-type pair that needs a + // R_RISCV_GOT_PCREL_ITYPE relocation. + NEED_GOT_PCREL_ITYPE_RELOC ) // RISC-V mnemonics, as defined in the "opcodes" and "opcodes-pseudo" files diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go index 381dc08560..54c34af2f4 100644 --- a/src/cmd/internal/obj/riscv/obj.go +++ b/src/cmd/internal/obj/riscv/obj.go @@ -215,11 +215,15 @@ func markRelocs(p *obj.Prog) { switch p.From.Name { case obj.NAME_EXTERN, obj.NAME_STATIC: p.Mark |= NEED_PCREL_ITYPE_RELOC + case obj.NAME_GOTREF: + p.Mark |= NEED_GOT_PCREL_ITYPE_RELOC } case p.From.Type == obj.TYPE_MEM && p.To.Type == obj.TYPE_REG: switch p.From.Name { case obj.NAME_EXTERN, obj.NAME_STATIC: p.Mark |= NEED_PCREL_ITYPE_RELOC + case obj.NAME_GOTREF: + p.Mark |= NEED_GOT_PCREL_ITYPE_RELOC } case p.From.Type == obj.TYPE_REG && p.To.Type == obj.TYPE_MEM: switch p.To.Name { @@ -2203,7 +2207,7 @@ func instructionsForMOV(p *obj.Prog) []*instruction { // MOV c(Rs), Rd -> L $c, Rs, Rd inss = instructionsForLoad(p, movToLoad(p.As), addrToReg(p.From)) - case obj.NAME_EXTERN, obj.NAME_STATIC: + case obj.NAME_EXTERN, obj.NAME_STATIC, obj.NAME_GOTREF: if p.From.Sym.Type == objabi.STLSBSS { return instructionsForTLSLoad(p) } @@ -2631,6 +2635,9 @@ func assemble(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) { } else if p.Mark&NEED_PCREL_STYPE_RELOC == NEED_PCREL_STYPE_RELOC { rt = objabi.R_RISCV_PCREL_STYPE addr = &p.To + } else if p.Mark&NEED_GOT_PCREL_ITYPE_RELOC == NEED_GOT_PCREL_ITYPE_RELOC { + rt = objabi.R_RISCV_GOT_PCREL_ITYPE + addr = &p.From } else { break } -- cgit v1.3-5-g9baa