| // Copyright 2019 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package riscv64 |
| |
| import ( |
| "cmd/internal/obj/riscv" |
| "cmd/internal/objabi" |
| "cmd/internal/sys" |
| "cmd/link/internal/ld" |
| "cmd/link/internal/loader" |
| "cmd/link/internal/sym" |
| "fmt" |
| "log" |
| ) |
| |
| func gentext(ctxt *ld.Link, ldr *loader.Loader) { |
| } |
| |
| func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool { |
| log.Fatalf("elfreloc1") |
| return false |
| } |
| |
| func elfsetupplt(ctxt *ld.Link, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym) { |
| log.Fatalf("elfsetuplt") |
| } |
| |
| func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool { |
| log.Fatalf("machoreloc1 not implemented") |
| return false |
| } |
| |
| func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (o int64, nExtReloc int, ok bool) { |
| rs := r.Sym() |
| rs = ldr.ResolveABIAlias(rs) |
| switch r.Type() { |
| case objabi.R_CALLRISCV: |
| // Nothing to do. |
| return val, 0, true |
| |
| case objabi.R_RISCV_PCREL_ITYPE, objabi.R_RISCV_PCREL_STYPE: |
| pc := ldr.SymValue(s) + int64(r.Off()) |
| off := ldr.SymValue(rs) + r.Add() - pc |
| |
| // Generate AUIPC and second instruction immediates. |
| low, high, err := riscv.Split32BitImmediate(off) |
| if err != nil { |
| ldr.Errorf(s, "R_RISCV_PCREL_ relocation does not fit in 32-bits: %d", off) |
| } |
| |
| auipcImm, err := riscv.EncodeUImmediate(high) |
| if err != nil { |
| ldr.Errorf(s, "cannot encode R_RISCV_PCREL_ AUIPC relocation offset for %s: %v", ldr.SymName(rs), err) |
| } |
| |
| var secondImm, secondImmMask int64 |
| switch r.Type() { |
| case objabi.R_RISCV_PCREL_ITYPE: |
| secondImmMask = riscv.ITypeImmMask |
| secondImm, err = riscv.EncodeIImmediate(low) |
| if err != nil { |
| ldr.Errorf(s, "cannot encode R_RISCV_PCREL_ITYPE I-type instruction relocation offset for %s: %v", ldr.SymName(rs), err) |
| } |
| case objabi.R_RISCV_PCREL_STYPE: |
| secondImmMask = riscv.STypeImmMask |
| secondImm, err = riscv.EncodeSImmediate(low) |
| if err != nil { |
| ldr.Errorf(s, "cannot encode R_RISCV_PCREL_STYPE S-type instruction relocation offset for %s: %v", ldr.SymName(rs), err) |
| } |
| default: |
| panic(fmt.Sprintf("Unknown relocation type: %v", r.Type())) |
| } |
| |
| auipc := int64(uint32(val)) |
| second := int64(uint32(val >> 32)) |
| |
| auipc = (auipc &^ riscv.UTypeImmMask) | int64(uint32(auipcImm)) |
| second = (second &^ secondImmMask) | int64(uint32(secondImm)) |
| |
| return second<<32 | auipc, 0, true |
| } |
| |
| return val, 0, false |
| } |
| |
| func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64) int64 { |
| log.Fatalf("archrelocvariant") |
| return -1 |
| } |