1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:18:32 -06:00

[dev.link] cmd/link: add methods for adding relocations in Reloc2 format

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>
This commit is contained in:
Cherry Zhang 2020-04-08 19:45:12 -04:00
parent e7c16412b7
commit fd7666881b

View 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.