]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: add methods for adding relocations in Reloc2 format
authorCherry Zhang <cherryyz@google.com>
Wed, 8 Apr 2020 23:45:12 +0000 (19:45 -0400)
committerCherry Zhang <cherryyz@google.com>
Thu, 9 Apr 2020 19:02:37 +0000 (19:02 +0000)
This is in prepration of removing the old loader.Reloc. This also
introduces a way of adding a slice of relocations more
efficiently (will be used in the next CL).

Change-Id: I3eaee7fb3a3e102a8670990f4a31c40d0b17b8c2
Reviewed-on: https://go-review.googlesource.com/c/go/+/227761
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/link/internal/loader/symbolbuilder.go

index 3d5dc876167b0dfc8dabb519058535b5caa4f831..8f142985434a70f6279abea7381635f8c2dee1f2 100644 (file)
@@ -10,6 +10,7 @@ import (
        "cmd/internal/sys"
        "cmd/link/internal/sym"
        "fmt"
+       "sort"
 )
 
 // SymbolBuilder is a helper designed to help with the construction
@@ -141,6 +142,38 @@ func (sb *SymbolBuilder) SetRelocs(rslice []Reloc) {
        }
 }
 
+// Add n relocations, return a handle to the relocations.
+func (sb *SymbolBuilder) AddRelocs(n int) Relocs {
+       sb.relocs = append(sb.relocs, make([]goobj2.Reloc2, n)...)
+       sb.reltypes = append(sb.reltypes, make([]objabi.RelocType, n)...)
+       return sb.l.Relocs(sb.symIdx)
+}
+
+// Add a relocation with given type, return its handle and index
+// (to set other fields).
+func (sb *SymbolBuilder) AddRel(typ objabi.RelocType) (Reloc2, int) {
+       j := len(sb.relocs)
+       sb.relocs = append(sb.relocs, goobj2.Reloc2{})
+       sb.reltypes = append(sb.reltypes, typ)
+       relocs := sb.Relocs()
+       return relocs.At2(j), j
+}
+
+// Sort relocations by offset.
+func (sb *SymbolBuilder) SortRelocs() {
+       sort.Sort((*relocsByOff)(sb.extSymPayload))
+}
+
+// Implement sort.Interface
+type relocsByOff extSymPayload
+
+func (p *relocsByOff) Len() int           { return len(p.relocs) }
+func (p *relocsByOff) Less(i, j int) bool { return p.relocs[i].Off() < p.relocs[j].Off() }
+func (p *relocsByOff) Swap(i, j int) {
+       p.relocs[i], p.relocs[j] = p.relocs[j], p.relocs[i]
+       p.reltypes[i], p.reltypes[j] = p.reltypes[j], p.reltypes[i]
+}
+
 // AddReloc appends the specified reloc to the symbols list of
 // relocations. Return value is the index of the newly created
 // reloc.