aboutsummaryrefslogtreecommitdiff
path: root/src/debug
diff options
context:
space:
mode:
authorXiaodong Liu <liuxiaodong@loongson.cn>2022-03-30 16:52:59 +0800
committerIan Lance Taylor <iant@golang.org>2022-04-04 21:43:42 +0000
commitcd33b4089caf362203cd749ee1b3680b72a8c502 (patch)
tree5c5132a41d3bdb36cbc79b8b1507d0e49f92c5db /src/debug
parent0b3cb1a56cde72066645a182c93bd710708c45f5 (diff)
downloadgo-cd33b4089caf362203cd749ee1b3680b72a8c502.tar.xz
debug: define ELF relocation for loong64
Contributors to the loong64 port are: Weining Lu <luweining@loongson.cn> Lei Wang <wanglei@loongson.cn> Lingqin Gong <gonglingqin@loongson.cn> Xiaolin Zhao <zhaoxiaolin@loongson.cn> Meidan Li <limeidan@loongson.cn> Xiaojuan Zhai <zhaixiaojuan@loongson.cn> Qiyuan Pu <puqiyuan@loongson.cn> Guoqi Chen <chenguoqi@loongson.cn> This port has been updated to Go 1.15.6: https://github.com/loongson/go For #46229 Change-Id: I0c58305754c20d2a59328adbd82caa527de254ec Reviewed-on: https://go-review.googlesource.com/c/go/+/396735 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/debug')
-rw-r--r--src/debug/elf/elf.go114
-rw-r--r--src/debug/elf/file.go50
2 files changed, 164 insertions, 0 deletions
diff --git a/src/debug/elf/elf.go b/src/debug/elf/elf.go
index 4c51bc4de2..5b2e6d9d3f 100644
--- a/src/debug/elf/elf.go
+++ b/src/debug/elf/elf.go
@@ -384,6 +384,7 @@ const (
EM_RISCV Machine = 243 /* RISC-V */
EM_LANAI Machine = 244 /* Lanai 32-bit processor */
EM_BPF Machine = 247 /* Linux BPF – in-kernel virtual machine */
+ EM_LOONGARCH Machine = 258 /* LoongArch */
/* Non-standard or deprecated. */
EM_486 Machine = 6 /* Intel i486. */
@@ -575,6 +576,7 @@ var machineStrings = []intName{
{243, "EM_RISCV"},
{244, "EM_LANAI"},
{247, "EM_BPF"},
+ {258, "EM_LOONGARCH"},
/* Non-standard or deprecated. */
{6, "EM_486"},
@@ -2150,6 +2152,118 @@ var rmipsStrings = []intName{
func (i R_MIPS) String() string { return stringName(uint32(i), rmipsStrings, false) }
func (i R_MIPS) GoString() string { return stringName(uint32(i), rmipsStrings, true) }
+// Relocation types for LARCH.
+type R_LARCH int
+
+const (
+ R_LARCH_NONE R_LARCH = 0
+ R_LARCH_32 R_LARCH = 1
+ R_LARCH_64 R_LARCH = 2
+ R_LARCH_RELATIVE R_LARCH = 3
+ R_LARCH_COPY R_LARCH = 4
+ R_LARCH_JUMP_SLOT R_LARCH = 5
+ R_LARCH_TLS_DTPMOD32 R_LARCH = 6
+ R_LARCH_TLS_DTPMOD64 R_LARCH = 7
+ R_LARCH_TLS_DTPREL32 R_LARCH = 8
+ R_LARCH_TLS_DTPREL64 R_LARCH = 9
+ R_LARCH_TLS_TPREL32 R_LARCH = 10
+ R_LARCH_TLS_TPREL64 R_LARCH = 11
+ R_LARCH_IRELATIVE R_LARCH = 12
+ R_LARCH_MARK_LA R_LARCH = 20
+ R_LARCH_MARK_PCREL R_LARCH = 21
+ R_LARCH_SOP_PUSH_PCREL R_LARCH = 22
+ R_LARCH_SOP_PUSH_ABSOLUTE R_LARCH = 23
+ R_LARCH_SOP_PUSH_DUP R_LARCH = 24
+ R_LARCH_SOP_PUSH_GPREL R_LARCH = 25
+ R_LARCH_SOP_PUSH_TLS_TPREL R_LARCH = 26
+ R_LARCH_SOP_PUSH_TLS_GOT R_LARCH = 27
+ R_LARCH_SOP_PUSH_TLS_GD R_LARCH = 28
+ R_LARCH_SOP_PUSH_PLT_PCREL R_LARCH = 29
+ R_LARCH_SOP_ASSERT R_LARCH = 30
+ R_LARCH_SOP_NOT R_LARCH = 31
+ R_LARCH_SOP_SUB R_LARCH = 32
+ R_LARCH_SOP_SL R_LARCH = 33
+ R_LARCH_SOP_SR R_LARCH = 34
+ R_LARCH_SOP_ADD R_LARCH = 35
+ R_LARCH_SOP_AND R_LARCH = 36
+ R_LARCH_SOP_IF_ELSE R_LARCH = 37
+ R_LARCH_SOP_POP_32_S_10_5 R_LARCH = 38
+ R_LARCH_SOP_POP_32_U_10_12 R_LARCH = 39
+ R_LARCH_SOP_POP_32_S_10_12 R_LARCH = 40
+ R_LARCH_SOP_POP_32_S_10_16 R_LARCH = 41
+ R_LARCH_SOP_POP_32_S_10_16_S2 R_LARCH = 42
+ R_LARCH_SOP_POP_32_S_5_20 R_LARCH = 43
+ R_LARCH_SOP_POP_32_S_0_5_10_16_S2 R_LARCH = 44
+ R_LARCH_SOP_POP_32_S_0_10_10_16_S2 R_LARCH = 45
+ R_LARCH_SOP_POP_32_U R_LARCH = 46
+ R_LARCH_ADD8 R_LARCH = 47
+ R_LARCH_ADD16 R_LARCH = 48
+ R_LARCH_ADD24 R_LARCH = 49
+ R_LARCH_ADD32 R_LARCH = 50
+ R_LARCH_ADD64 R_LARCH = 51
+ R_LARCH_SUB8 R_LARCH = 52
+ R_LARCH_SUB16 R_LARCH = 53
+ R_LARCH_SUB24 R_LARCH = 54
+ R_LARCH_SUB32 R_LARCH = 55
+ R_LARCH_SUB64 R_LARCH = 56
+)
+
+var rlarchStrings = []intName{
+ {0, "R_LARCH_NONE"},
+ {1, "R_LARCH_32"},
+ {2, "R_LARCH_64"},
+ {3, "R_LARCH_RELATIVE"},
+ {4, "R_LARCH_COPY"},
+ {5, "R_LARCH_JUMP_SLOT"},
+ {6, "R_LARCH_TLS_DTPMOD32"},
+ {7, "R_LARCH_TLS_DTPMOD64"},
+ {8, "R_LARCH_TLS_DTPREL32"},
+ {9, "R_LARCH_TLS_DTPREL64"},
+ {10, "R_LARCH_TLS_TPREL32"},
+ {11, "R_LARCH_TLS_TPREL64"},
+ {12, "R_LARCH_IRELATIVE"},
+ {20, "R_LARCH_MARK_LA"},
+ {21, "R_LARCH_MARK_PCREL"},
+ {22, "R_LARCH_SOP_PUSH_PCREL"},
+ {23, "R_LARCH_SOP_PUSH_ABSOLUTE"},
+ {24, "R_LARCH_SOP_PUSH_DUP"},
+ {25, "R_LARCH_SOP_PUSH_GPREL"},
+ {26, "R_LARCH_SOP_PUSH_TLS_TPREL"},
+ {27, "R_LARCH_SOP_PUSH_TLS_GOT"},
+ {28, "R_LARCH_SOP_PUSH_TLS_GD"},
+ {29, "R_LARCH_SOP_PUSH_PLT_PCREL"},
+ {30, "R_LARCH_SOP_ASSERT"},
+ {31, "R_LARCH_SOP_NOT"},
+ {32, "R_LARCH_SOP_SUB"},
+ {33, "R_LARCH_SOP_SL"},
+ {34, "R_LARCH_SOP_SR"},
+ {35, "R_LARCH_SOP_ADD"},
+ {36, "R_LARCH_SOP_AND"},
+ {37, "R_LARCH_SOP_IF_ELSE"},
+ {38, "R_LARCH_SOP_POP_32_S_10_5"},
+ {39, "R_LARCH_SOP_POP_32_U_10_12"},
+ {40, "R_LARCH_SOP_POP_32_S_10_12"},
+ {41, "R_LARCH_SOP_POP_32_S_10_16"},
+ {42, "R_LARCH_SOP_POP_32_S_10_16_S2"},
+ {43, "R_LARCH_SOP_POP_32_S_5_20"},
+ {44, "R_LARCH_SOP_POP_32_S_0_5_10_16_S2"},
+ {45, "R_LARCH_SOP_POP_32_S_0_10_10_16_S2"},
+ {46, "R_LARCH_SOP_POP_32_U"},
+ {47, "R_LARCH_ADD8"},
+ {48, "R_LARCH_ADD16"},
+ {49, "R_LARCH_ADD24"},
+ {50, "R_LARCH_ADD32"},
+ {51, "R_LARCH_ADD64"},
+ {52, "R_LARCH_SUB8"},
+ {53, "R_LARCH_SUB16"},
+ {54, "R_LARCH_SUB24"},
+ {55, "R_LARCH_SUB32"},
+ {56, "R_LARCH_SUB64"},
+}
+
+func (i R_LARCH) String() string { return stringName(uint32(i), rlarchStrings, false) }
+func (i R_LARCH) GoString() string { return stringName(uint32(i), rlarchStrings, true) }
+
// Relocation types for PowerPC.
//
// Values that are shared by both R_PPC and R_PPC64 are prefixed with
diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go
index 5f339596a7..6bfcd2a3f8 100644
--- a/src/debug/elf/file.go
+++ b/src/debug/elf/file.go
@@ -633,6 +633,8 @@ func (f *File) applyRelocations(dst []byte, rels []byte) error {
return f.applyRelocationsMIPS(dst, rels)
case f.Class == ELFCLASS64 && f.Machine == EM_MIPS:
return f.applyRelocationsMIPS64(dst, rels)
+ case f.Class == ELFCLASS64 && f.Machine == EM_LOONGARCH:
+ return f.applyRelocationsLOONG64(dst, rels)
case f.Class == ELFCLASS64 && f.Machine == EM_RISCV:
return f.applyRelocationsRISCV64(dst, rels)
case f.Class == ELFCLASS64 && f.Machine == EM_S390:
@@ -1006,6 +1008,54 @@ func (f *File) applyRelocationsMIPS64(dst []byte, rels []byte) error {
return nil
}
+func (f *File) applyRelocationsLOONG64(dst []byte, rels []byte) error {
+ // 24 is the size of Rela64.
+ if len(rels)%24 != 0 {
+ return errors.New("length of relocation section is not a multiple of 24")
+ }
+
+ symbols, _, err := f.getSymbols(SHT_SYMTAB)
+ if err != nil {
+ return err
+ }
+
+ b := bytes.NewReader(rels)
+ var rela Rela64
+
+ for b.Len() > 0 {
+ binary.Read(b, f.ByteOrder, &rela)
+ var symNo uint64
+ var t R_LARCH
+ symNo = rela.Info >> 32
+ t = R_LARCH(rela.Info & 0xffff)
+
+ if symNo == 0 || symNo > uint64(len(symbols)) {
+ continue
+ }
+ sym := &symbols[symNo-1]
+ if !canApplyRelocation(sym) {
+ continue
+ }
+
+ switch t {
+ case R_LARCH_64:
+ if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 {
+ continue
+ }
+ val64 := sym.Value + uint64(rela.Addend)
+ f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], val64)
+ case R_LARCH_32:
+ if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 {
+ continue
+ }
+ val32 := uint32(sym.Value) + uint32(rela.Addend)
+ f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], val32)
+ }
+ }
+
+ return nil
+}
+
func (f *File) applyRelocationsRISCV64(dst []byte, rels []byte) error {
// 24 is the size of Rela64.
if len(rels)%24 != 0 {