[dev.link] cmd/link: set direct fn address in dwarf gen where possible

If we are internal linking a static executable, and address assignment
has happened, then when emitting some parts of DWARF we can just emit
a function address directly instead of generating a relocation. For
external linking or other build modes, we are generating a relocatable
binary so we still need to emit relocations.

This CL inspired by Cherry's similar CL for pclntab at
https://go-review.googlesource.com/c/go/+/228478.

Change-Id: Ib03fbe2dd72d0ba746bf46015e0f2d6c3f3d53ab
Reviewed-on: https://go-review.googlesource.com/c/go/+/228537
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Than McIntosh 2020-04-16 08:29:43 -04:00
parent 4a0bca37d2
commit da9f383ca1
2 changed files with 29 additions and 2 deletions

View file

@ -405,3 +405,24 @@ func (sb *SymbolBuilder) AddSize(arch *sys.Arch, tgt Sym) int64 {
sb.setReachable()
return sb.addSymRef(tgt, 0, objabi.R_SIZE, arch.PtrSize)
}
// GenAddAddrPlusFunc returns a function to be called when capturing
// a function symbol's address. In later stages of the link (when
// address assignment is done) when doing internal linking and
// targeting an executable, we can just emit the address of a function
// directly instead of generating a relocation. Clients can call
// this function (setting 'internalExec' based on build mode and target)
// and then invoke the returned function in roughly the same way that
// loader.*SymbolBuilder.AddAddrPlus would be used.
func GenAddAddrPlusFunc(internalExec bool) func(s *SymbolBuilder, arch *sys.Arch, tgt Sym, add int64) int64 {
if internalExec {
return func(s *SymbolBuilder, arch *sys.Arch, tgt Sym, add int64) int64 {
if v := s.l.SymValue(tgt); v != 0 {
return s.AddUint(arch, uint64(v+add))
}
return s.AddAddrPlus(arch, tgt, add)
}
} else {
return (*SymbolBuilder).AddAddrPlus
}
}