1
0
mirror of https://github.com/golang/go synced 2024-11-06 22:46:14 -07:00

[dev.link] cmd/link: fix macho host objects

Small updates to macho loader, fixing some misunderstandings I had about
using the new object file format.

Change-Id: I9224b01ca327e3a087ebfa36800bd6eef6abcc80
Reviewed-on: https://go-review.googlesource.com/c/go/+/204097
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Jeremy Faller 2019-10-29 10:52:20 -04:00
parent 931845aee3
commit 396a9b98ba
4 changed files with 13 additions and 19 deletions

View File

@ -168,7 +168,7 @@ func canLinkHostObj(ctxt *Link) bool {
if !*flagNewobj {
return true
}
return ctxt.IsELF
return ctxt.IsELF || objabi.GOOS == "darwin"
}
// mustLinkExternal reports whether the program being linked requires

View File

@ -1653,11 +1653,12 @@ func ldobj(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, pn string,
if magic&^1 == 0xfeedface || magic&^0x01000000 == 0xcefaedfe {
if *flagNewobj {
ldmacho := func(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
err := loadmacho.Load(ctxt.loader, ctxt.Arch, ctxt.Syms, f, pkg, length, pn)
textp, err := loadmacho.Load(ctxt.loader, ctxt.Arch, ctxt.Syms, f, pkg, length, pn)
if err != nil {
Errorf(nil, "%v", err)
return
}
ctxt.Textp = append(ctxt.Textp, textp...)
}
return ldhostobj(ldmacho, ctxt.HeadType, f, pkg, length, pn, file)
} else {

View File

@ -511,7 +511,7 @@ func (l *Loader) OuterSym(i Sym) Sym {
return 0
}
// SubSym gets sub symbols for a previously loaded host object symbol.
// SubSym gets the subsymbol for host object loaded symbols.
func (l *Loader) SubSym(i Sym) Sym {
sym := l.Syms[i]
if sym != nil && sym.Sub != nil {

View File

@ -424,31 +424,24 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
return 0
}
func Load(l *loader.Loader, arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length int64, pn string) error {
lookup := func(name string, version int) *sym.Symbol {
// Check to see if we've already defined the symbol.
if i := l.Lookup(name, version); i != 0 {
return l.Syms[i]
func Load(l *loader.Loader, arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length int64, pn string) ([]*sym.Symbol, error) {
newSym := func(name string, version int) *sym.Symbol {
i := l.Lookup(name, version)
if i != 0 {
return l.LoadSymbol(name, version, syms)
}
// Not defined, let's make one.
if s := l.AddExtSym(name, version); s == 0 {
if i = l.AddExtSym(name, version); i == 0 {
panic("AddExtSym returned bad index")
} else if int(s) != len(l.Syms) {
panic("unexpected length of loaded symbols")
}
newSym := syms.Newsym(name, version)
l.Syms = append(l.Syms, newSym)
l.Syms[i] = newSym
return newSym
}
_, err := load(arch, syms.IncVersion(), lookup, f, pkg, length, pn)
return err
return load(arch, syms.IncVersion(), newSym, f, pkg, length, pn)
}
func LoadOld(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length int64, pn string) (textp []*sym.Symbol, err error) {
lookup := func(name string, version int) *sym.Symbol {
return syms.Lookup(name, version)
}
return load(arch, syms.IncVersion(), lookup, f, pkg, length, pn)
return load(arch, syms.IncVersion(), syms.Lookup, f, pkg, length, pn)
}
// load the Mach-O file pn from f.