aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj/data.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2024-11-05 13:33:17 -0500
committerRuss Cox <rsc@golang.org>2024-11-07 12:17:10 +0000
commit5b20eec8a0eb3bd4681e315c0c4023c5164ea7b9 (patch)
tree184613d170d137337c65c238153caafe4ececa21 /src/cmd/internal/obj/data.go
parent4ce8c0604ee1c36c221b1a3d767dfa131d5cce8c (diff)
downloadgo-5b20eec8a0eb3bd4681e315c0c4023c5164ea7b9.tar.xz
cmd/internal/obj: replace obj.Addrel func with LSym.AddRel method
The old API was to do r := obj.AddRel(sym) r.Type = this r.Off = that etc The new API is: sym.AddRel(ctxt, obj.Reloc{Type: this: Off: that, etc}) This new API is more idiomatic and avoids ever having relocations that are only partially constructed. Most importantly, it sets up for sym.AddRel being able to check relocation validity in the future. (Passing ctxt is for use in validity checking.) Passes golang.org/x/tools/cmd/toolstash/buildall. Change-Id: I042ea76e61bb3bf6402f98ca11291a13f4799972 Reviewed-on: https://go-review.googlesource.com/c/go/+/625616 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/internal/obj/data.go')
-rw-r--r--src/cmd/internal/obj/data.go56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/cmd/internal/obj/data.go b/src/cmd/internal/obj/data.go
index bcba53c3a4..361ea05a0f 100644
--- a/src/cmd/internal/obj/data.go
+++ b/src/cmd/internal/obj/data.go
@@ -118,15 +118,16 @@ func (s *LSym) writeAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64,
ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
}
s.prepwrite(ctxt, off, siz)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
+ if int64(int32(off)) != off {
ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
}
- r.Siz = uint8(siz)
- r.Sym = rsym
- r.Type = rtype
- r.Add = roff
+ s.AddRel(ctxt, Reloc{
+ Type: rtype,
+ Off: int32(off),
+ Siz: uint8(siz),
+ Sym: rsym,
+ Add: roff,
+ })
}
// WriteAddr writes an address of size siz into s at offset off.
@@ -155,15 +156,16 @@ func (s *LSym) WriteCURelativeAddr(ctxt *Link, off int64, rsym *LSym, roff int64
// rsym+roff-(start of section that s is in).
func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
s.prepwrite(ctxt, off, 4)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
+ if int64(int32(off)) != off {
ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
}
- r.Siz = 4
- r.Sym = rsym
- r.Type = objabi.R_ADDROFF
- r.Add = roff
+ s.AddRel(ctxt, Reloc{
+ Type: objabi.R_ADDROFF,
+ Off: int32(off),
+ Siz: 4,
+ Sym: rsym,
+ Add: roff,
+ })
}
// WriteWeakOff writes a weak 4 byte offset to rsym+roff into s at offset off.
@@ -171,15 +173,16 @@ func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
// rsym+roff-(start of section that s is in).
func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
s.prepwrite(ctxt, off, 4)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
- ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
+ if int64(int32(off)) != off {
+ ctxt.Diag("WriteWeakOff: off overflow %d in %s", off, s.Name)
}
- r.Siz = 4
- r.Sym = rsym
- r.Type = objabi.R_WEAKADDROFF
- r.Add = roff
+ s.AddRel(ctxt, Reloc{
+ Type: objabi.R_WEAKADDROFF,
+ Off: int32(off),
+ Siz: 4,
+ Sym: rsym,
+ Add: roff,
+ })
}
// WriteString writes a string of size siz into s at offset off.
@@ -198,10 +201,7 @@ func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 {
return off + int64(len(b))
}
-func Addrel(s *LSym) *Reloc {
- if s.R == nil {
- s.R = make([]Reloc, 0, 4)
- }
- s.R = append(s.R, Reloc{})
- return &s.R[len(s.R)-1]
+// AddRel adds the relocation rel to s.
+func (s *LSym) AddRel(ctxt *Link, rel Reloc) {
+ s.R = append(s.R, rel)
}