Skip to content

Commit

Permalink
debug: define ELF relocation for loong64
Browse files Browse the repository at this point in the history
Contributors to the loong64 port are:
  Weining Lu <[email protected]>
  Lei Wang <[email protected]>
  Lingqin Gong <[email protected]>
  Xiaolin Zhao <[email protected]>
  Meidan Li <[email protected]>
  Xiaojuan Zhai <[email protected]>
  Qiyuan Pu <[email protected]>
  Guoqi Chen <[email protected]>

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 <[email protected]>
Trust: Cherry Mui <[email protected]>
  • Loading branch information
XiaodongLoong authored and ianlancetaylor committed Apr 4, 2022
1 parent 0b3cb1a commit cd33b40
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 0 deletions.
105 changes: 105 additions & 0 deletions api/next/46229.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
pkg debug/elf, const EM_LOONGARCH = 258 #46229
pkg debug/elf, const EM_LOONGARCH Machine #46229
pkg debug/elf, const R_LARCH_32 = 1 #46229
pkg debug/elf, const R_LARCH_32 R_LARCH #46229
pkg debug/elf, const R_LARCH_64 = 2 #46229
pkg debug/elf, const R_LARCH_64 R_LARCH #46229
pkg debug/elf, const R_LARCH_ADD16 = 48 #46229
pkg debug/elf, const R_LARCH_ADD16 R_LARCH #46229
pkg debug/elf, const R_LARCH_ADD24 = 49 #46229
pkg debug/elf, const R_LARCH_ADD24 R_LARCH #46229
pkg debug/elf, const R_LARCH_ADD32 = 50 #46229
pkg debug/elf, const R_LARCH_ADD32 R_LARCH #46229
pkg debug/elf, const R_LARCH_ADD64 = 51 #46229
pkg debug/elf, const R_LARCH_ADD64 R_LARCH #46229
pkg debug/elf, const R_LARCH_ADD8 = 47 #46229
pkg debug/elf, const R_LARCH_ADD8 R_LARCH #46229
pkg debug/elf, const R_LARCH_COPY = 4 #46229
pkg debug/elf, const R_LARCH_COPY R_LARCH #46229
pkg debug/elf, const R_LARCH_IRELATIVE = 12 #46229
pkg debug/elf, const R_LARCH_IRELATIVE R_LARCH #46229
pkg debug/elf, const R_LARCH_JUMP_SLOT = 5 #46229
pkg debug/elf, const R_LARCH_JUMP_SLOT R_LARCH #46229
pkg debug/elf, const R_LARCH_MARK_LA = 20 #46229
pkg debug/elf, const R_LARCH_MARK_LA R_LARCH #46229
pkg debug/elf, const R_LARCH_MARK_PCREL = 21 #46229
pkg debug/elf, const R_LARCH_MARK_PCREL R_LARCH #46229
pkg debug/elf, const R_LARCH_NONE = 0 #46229
pkg debug/elf, const R_LARCH_NONE R_LARCH #46229
pkg debug/elf, const R_LARCH_RELATIVE = 3 #46229
pkg debug/elf, const R_LARCH_RELATIVE R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_ADD = 35 #46229
pkg debug/elf, const R_LARCH_SOP_ADD R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_AND = 36 #46229
pkg debug/elf, const R_LARCH_SOP_AND R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_ASSERT = 30 #46229
pkg debug/elf, const R_LARCH_SOP_ASSERT R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_IF_ELSE = 37 #46229
pkg debug/elf, const R_LARCH_SOP_IF_ELSE R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_NOT = 31 #46229
pkg debug/elf, const R_LARCH_SOP_NOT R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_0_10_10_16_S2 = 45 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_0_10_10_16_S2 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_0_5_10_16_S2 = 44 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_0_5_10_16_S2 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_12 = 40 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_12 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_16 = 41 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_16 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_16_S2 = 42 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_16_S2 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_5 = 38 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_10_5 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_5_20 = 43 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_S_5_20 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_U = 46 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_U R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_U_10_12 = 39 #46229
pkg debug/elf, const R_LARCH_SOP_POP_32_U_10_12 R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_ABSOLUTE = 23 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_ABSOLUTE R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_DUP = 24 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_DUP R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_GPREL = 25 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_GPREL R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_PCREL = 22 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_PCREL R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_PLT_PCREL = 29 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_PLT_PCREL R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_GD = 28 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_GD R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_GOT = 27 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_GOT R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_TPREL = 26 #46229
pkg debug/elf, const R_LARCH_SOP_PUSH_TLS_TPREL R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_SL = 33 #46229
pkg debug/elf, const R_LARCH_SOP_SL R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_SR = 34 #46229
pkg debug/elf, const R_LARCH_SOP_SR R_LARCH #46229
pkg debug/elf, const R_LARCH_SOP_SUB = 32 #46229
pkg debug/elf, const R_LARCH_SOP_SUB R_LARCH #46229
pkg debug/elf, const R_LARCH_SUB16 = 53 #46229
pkg debug/elf, const R_LARCH_SUB16 R_LARCH #46229
pkg debug/elf, const R_LARCH_SUB24 = 54 #46229
pkg debug/elf, const R_LARCH_SUB24 R_LARCH #46229
pkg debug/elf, const R_LARCH_SUB32 = 55 #46229
pkg debug/elf, const R_LARCH_SUB32 R_LARCH #46229
pkg debug/elf, const R_LARCH_SUB64 = 56 #46229
pkg debug/elf, const R_LARCH_SUB64 R_LARCH #46229
pkg debug/elf, const R_LARCH_SUB8 = 52 #46229
pkg debug/elf, const R_LARCH_SUB8 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_DTPMOD32 = 6 #46229
pkg debug/elf, const R_LARCH_TLS_DTPMOD32 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_DTPMOD64 = 7 #46229
pkg debug/elf, const R_LARCH_TLS_DTPMOD64 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_DTPREL32 = 8 #46229
pkg debug/elf, const R_LARCH_TLS_DTPREL32 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_DTPREL64 = 9 #46229
pkg debug/elf, const R_LARCH_TLS_DTPREL64 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_TPREL32 = 10 #46229
pkg debug/elf, const R_LARCH_TLS_TPREL32 R_LARCH #46229
pkg debug/elf, const R_LARCH_TLS_TPREL64 = 11 #46229
pkg debug/elf, const R_LARCH_TLS_TPREL64 R_LARCH #46229
pkg debug/elf, method (R_LARCH) GoString() string #46229
pkg debug/elf, method (R_LARCH) String() string #46229
pkg debug/elf, type R_LARCH int #46229
114 changes: 114 additions & 0 deletions src/debug/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/debug/elf/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cd33b40

Please sign in to comment.