feat(asm): resolve external GOOBJ symbols from archive data
This commit is contained in:
+66
-21
@@ -113,16 +113,47 @@ func (img *Image) GOObjectRISCV(pkgPath, srcPath string) ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
// Resolve external symbol references (cross-package).
|
||||
var extPkgTable []string
|
||||
var extPkgIdx map[string]int
|
||||
var extSymIdx map[string]int
|
||||
if len(img.Externals) > 0 {
|
||||
var err error
|
||||
extPkgTable, extPkgIdx, extSymIdx, err = resolveExternalSymbols(img.Externals)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GOOBJ emission: resolving external symbols: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Relocations, per defined symbol in definition order (package defs,
|
||||
// then non-package defs). Only file-local GLOBL references resolve;
|
||||
// external symbols need the import machinery of a later increment.
|
||||
// then non-package defs).
|
||||
nsyms := len(defs) + len(nps)
|
||||
symRelocs := make([][]byte, nsyms) // flat 23-byte records
|
||||
for i, fn := range img.Funcs {
|
||||
si := len(defs) + fnNpIdx[i]
|
||||
for _, r := range fn.Relocs {
|
||||
if r.External {
|
||||
return nil, fmt.Errorf("GOOBJ emission: external symbol %q is not supported yet", r.Name)
|
||||
pkg, name := splitQualified(r.Name)
|
||||
if pkg == "" {
|
||||
return nil, fmt.Errorf("GOOBJ emission: external symbol %q has no package prefix", r.Name)
|
||||
}
|
||||
pIdx, ok := extPkgIdx[pkg]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("GOOBJ emission: package %q not resolved", pkg)
|
||||
}
|
||||
sIdx, ok := extSymIdx[pkg+"·"+name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("GOOBJ emission: symbol %s·%s not resolved", pkg, name)
|
||||
}
|
||||
var rec [23]byte
|
||||
binary.LittleEndian.PutUint32(rec[0:], uint32(int32(r.Off)))
|
||||
rec[4] = 4 // field width
|
||||
binary.LittleEndian.PutUint16(rec[5:], relocRISCVPcrelHi20)
|
||||
binary.LittleEndian.PutUint64(rec[7:], uint64(r.Addend))
|
||||
binary.LittleEndian.PutUint32(rec[15:], uint32(pIdx))
|
||||
binary.LittleEndian.PutUint32(rec[19:], uint32(sIdx))
|
||||
symRelocs[si] = append(symRelocs[si], rec[:]...)
|
||||
continue
|
||||
}
|
||||
di, ok := defIdx[r.Name]
|
||||
if !ok {
|
||||
@@ -179,6 +210,20 @@ func (img *Image) GOObjectRISCV(pkgPath, srcPath string) ([]byte, error) {
|
||||
for _, s := range nps {
|
||||
addStr(s.sym.name)
|
||||
}
|
||||
for _, pkg := range extPkgTable {
|
||||
addStr(pkg)
|
||||
}
|
||||
|
||||
// Package index block: index 0 is the dummy invalid package, followed by
|
||||
// external packages.
|
||||
var pkgIdxBlk bytes.Buffer
|
||||
pkgIdxBlk.Write(binary.LittleEndian.AppendUint32(nil, 0)) // len("")
|
||||
pkgIdxBlk.Write(binary.LittleEndian.AppendUint32(nil, 0)) // offset of ""
|
||||
for _, pkg := range extPkgTable {
|
||||
off := strOff[pkg]
|
||||
pkgIdxBlk.Write(binary.LittleEndian.AppendUint32(nil, uint32(len(pkg))))
|
||||
pkgIdxBlk.Write(binary.LittleEndian.AppendUint32(nil, off))
|
||||
}
|
||||
|
||||
// Symbol definition records (21 bytes each).
|
||||
var symdef, nonpkgdef []byte
|
||||
@@ -258,24 +303,24 @@ func (img *Image) GOObjectRISCV(pkgPath, srcPath string) ([]byte, error) {
|
||||
// Blocks in order: autolib, pkgidx, file, symdef, hashed64def, hasheddef,
|
||||
// nonpkgdef, nonpkgref, refflags, hash64, hash, relocidx, auxidx, dataidx,
|
||||
// reloc, aux, data, refname.
|
||||
writeBlock(nil) // autolib
|
||||
writeBlock(nil) // pkgidx
|
||||
writeBlock(fileBlk) // file
|
||||
writeBlock(symdef) // symdef
|
||||
writeBlock(nil) // hashed64def
|
||||
writeBlock(nil) // hasheddef
|
||||
writeBlock(nonpkgdef) // nonpkgdef
|
||||
writeBlock(nil) // nonpkgref
|
||||
writeBlock(nil) // refflags
|
||||
writeBlock(nil) // hash64
|
||||
writeBlock(nil) // hash
|
||||
writeBlock(relocIdx) // relocidx
|
||||
writeBlock(auxIdx) // auxidx
|
||||
writeBlock(dataIdx) // dataidx
|
||||
writeBlock(relocBlk) // reloc
|
||||
writeBlock(auxBlk) // aux
|
||||
writeBlock(dataBlk) // data
|
||||
writeBlock(nil) // refname
|
||||
writeBlock(nil) // autolib
|
||||
writeBlock(pkgIdxBlk.Bytes()) // pkgidx
|
||||
writeBlock(fileBlk) // file
|
||||
writeBlock(symdef) // symdef
|
||||
writeBlock(nil) // hashed64def
|
||||
writeBlock(nil) // hasheddef
|
||||
writeBlock(nonpkgdef) // nonpkgdef
|
||||
writeBlock(nil) // nonpkgref
|
||||
writeBlock(nil) // refflags
|
||||
writeBlock(nil) // hash64
|
||||
writeBlock(nil) // hash
|
||||
writeBlock(relocIdx) // relocidx
|
||||
writeBlock(auxIdx) // auxidx
|
||||
writeBlock(dataIdx) // dataidx
|
||||
writeBlock(relocBlk) // reloc
|
||||
writeBlock(auxBlk) // aux
|
||||
writeBlock(dataBlk) // data
|
||||
writeBlock(nil) // refname
|
||||
|
||||
// Fill in the block offsets.
|
||||
le := binary.LittleEndian
|
||||
|
||||
Reference in New Issue
Block a user