Skip to content

Commit ffc85ee

Browse files
committed
cmd/internal/objabi,cmd/link: add support for additional riscv64 relocations
These additional relocation types are encountered when using the Go race detector with riscv64. Updates #64345 Change-Id: I6de6430165fb378463da1af9402198a3a31485ca Reviewed-on: https://go-review.googlesource.com/c/go/+/690496 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Meng Zhuo <[email protected]>
1 parent 9de69f6 commit ffc85ee

File tree

4 files changed

+74
-30
lines changed

4 files changed

+74
-30
lines changed

src/cmd/internal/objabi/reloctype.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ const (
305305
// R_RISCV_BRANCH resolves a 12-bit PC-relative branch offset.
306306
R_RISCV_BRANCH
307307

308+
// R_RISCV_ADD32 resolves a 32-bit label addition, being the stored value,
309+
// plus the symbol address plus the addend (V + S + A).
310+
R_RISCV_ADD32
311+
312+
// R_RISCV_SUB32 resolves a 32-bit label subtraction, being the stored value,
313+
// minus the symbol address minus the addend (V - S - A).
314+
R_RISCV_SUB32
315+
308316
// R_RISCV_RVC_BRANCH resolves an 8-bit PC-relative offset for a CB-type
309317
// instruction.
310318
R_RISCV_RVC_BRANCH

src/cmd/internal/objabi/reloctype_string.go

Lines changed: 32 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/link/internal/loadelf/ldelf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ func relSize(arch *sys.Arch, pn string, elftype uint32) (uint8, uint8, error) {
11781178
RISCV64 | uint32(elf.R_RISCV_SET32)<<16,
11791179
RISCV64 | uint32(elf.R_RISCV_SUB32)<<16,
11801180
RISCV64 | uint32(elf.R_RISCV_32_PCREL)<<16,
1181+
RISCV64 | uint32(elf.R_RISCV_JAL)<<16,
11811182
RISCV64 | uint32(elf.R_RISCV_RELAX)<<16:
11821183
return 4, 4, nil
11831184

src/cmd/link/internal/riscv64/asm.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
118118
su.SetRelocType(rIdx, objabi.R_RISCV_PCREL_LO12_S)
119119
return true
120120

121+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_32_PCREL):
122+
su := ldr.MakeSymbolUpdater(s)
123+
su.SetRelocType(rIdx, objabi.R_PCREL)
124+
return true
125+
126+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_64):
127+
su := ldr.MakeSymbolUpdater(s)
128+
su.SetRelocType(rIdx, objabi.R_ADDR)
129+
return true
130+
131+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_ADD32):
132+
su := ldr.MakeSymbolUpdater(s)
133+
su.SetRelocType(rIdx, objabi.R_RISCV_ADD32)
134+
return true
135+
136+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_SUB32):
137+
su := ldr.MakeSymbolUpdater(s)
138+
su.SetRelocType(rIdx, objabi.R_RISCV_SUB32)
139+
return true
140+
121141
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_RVC_BRANCH):
122142
su := ldr.MakeSymbolUpdater(s)
123143
su.SetRelocType(rIdx, objabi.R_RISCV_RVC_BRANCH)
@@ -137,6 +157,11 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
137157
// Ignore relaxations, at least for now.
138158
return true
139159

160+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_RISCV_JAL):
161+
su := ldr.MakeSymbolUpdater(s)
162+
su.SetRelocType(rIdx, objabi.R_RISCV_JAL)
163+
return true
164+
140165
default:
141166
if r.Type() >= objabi.ElfRelocOffset {
142167
ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type()))
@@ -639,6 +664,14 @@ func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loade
639664
second = (second &^ secondImmMask) | int64(uint32(secondImm))
640665

641666
return second<<32 | auipc, 0, true
667+
668+
case objabi.R_RISCV_ADD32:
669+
addr := val + ldr.SymValue(rs) + r.Add()
670+
return int64(uint32(addr)), 0, true
671+
672+
case objabi.R_RISCV_SUB32:
673+
addr := val - ldr.SymValue(rs) - r.Add()
674+
return int64(uint32(addr)), 0, true
642675
}
643676

644677
return val, 0, false

0 commit comments

Comments
 (0)