[dev.link] cmd/asm: new -p option, changes to DWARF generation

Adds a new "-p" option to the assembler, for specifying the import
path of the package being compiled. DWARF generation is now conditional
on having a valid package path -- if we don't know the package path,
then don't emit DWARF.

This is intended to lay the groundwork for removing the various
"patchDWARFname" hacks in the linker.

Change-Id: I5f8315c0881791eb8fe1f2ba32f5bb0ae76f6b98
Reviewed-on: https://go-review.googlesource.com/c/go/+/222718
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Than McIntosh 2020-03-06 12:43:59 -05:00
parent cdc1b19513
commit 9cae3b33c3
4 changed files with 19 additions and 9 deletions

View File

@ -23,6 +23,7 @@ var (
Dynlink = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries") Dynlink = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
AllErrors = flag.Bool("e", false, "no limit on number of errors reported") AllErrors = flag.Bool("e", false, "no limit on number of errors reported")
SymABIs = flag.Bool("gensymabis", false, "write symbol ABI information to output file, don't assemble") SymABIs = flag.Bool("gensymabis", false, "write symbol ABI information to output file, don't assemble")
Importpath = flag.String("p", "", "set expected package import to path")
) )
var ( var (

View File

@ -73,7 +73,7 @@ func main() {
pList.Firstpc, ok = parser.Parse() pList.Firstpc, ok = parser.Parse()
// reports errors to parser.Errorf // reports errors to parser.Errorf
if ok { if ok {
obj.Flushplist(ctxt, pList, nil, "") obj.Flushplist(ctxt, pList, nil, *flags.Importpath)
} }
} }
if !ok { if !ok {

View File

@ -37,6 +37,17 @@ func (gcToolchain) linker() string {
return base.Tool("link") return base.Tool("link")
} }
func pkgPath(a *Action) string {
p := a.Package
ppath := p.ImportPath
if cfg.BuildBuildmode == "plugin" {
ppath = pluginPath(a)
} else if p.Name == "main" && !p.Internal.ForceLibrary {
ppath = "main"
}
return ppath
}
func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, output []byte, err error) { func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, output []byte, err error) {
p := a.Package p := a.Package
objdir := a.Objdir objdir := a.Objdir
@ -47,12 +58,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, s
ofile = objdir + out ofile = objdir + out
} }
pkgpath := p.ImportPath pkgpath := pkgPath(a)
if cfg.BuildBuildmode == "plugin" {
pkgpath = pluginPath(a)
} else if p.Name == "main" && !p.Internal.ForceLibrary {
pkgpath = "main"
}
gcargs := []string{"-p", pkgpath} gcargs := []string{"-p", pkgpath}
if p.Module != nil && p.Module.GoVersion != "" && allowedVersion(p.Module.GoVersion) { if p.Module != nil && p.Module.GoVersion != "" && allowedVersion(p.Module.GoVersion) {
gcargs = append(gcargs, "-lang=go"+p.Module.GoVersion) gcargs = append(gcargs, "-lang=go"+p.Module.GoVersion)
@ -240,7 +246,8 @@ func (a *Action) trimpath() string {
func asmArgs(a *Action, p *load.Package) []interface{} { func asmArgs(a *Action, p *load.Package) []interface{} {
// Add -I pkg/GOOS_GOARCH so #include "textflag.h" works in .s files. // Add -I pkg/GOOS_GOARCH so #include "textflag.h" works in .s files.
inc := filepath.Join(cfg.GOROOT, "pkg", "include") inc := filepath.Join(cfg.GOROOT, "pkg", "include")
args := []interface{}{cfg.BuildToolexec, base.Tool("asm"), "-trimpath", a.trimpath(), "-I", a.Objdir, "-I", inc, "-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch, forcedAsmflags, p.Internal.Asmflags} pkgpath := pkgPath(a)
args := []interface{}{cfg.BuildToolexec, base.Tool("asm"), "-p", pkgpath, "-trimpath", a.trimpath(), "-I", a.Objdir, "-I", inc, "-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch, forcedAsmflags, p.Internal.Asmflags}
if p.ImportPath == "runtime" && cfg.Goarch == "386" { if p.ImportPath == "runtime" && cfg.Goarch == "386" {
for _, arg := range forcedAsmflags { for _, arg := range forcedAsmflags {
if arg == "-dynlink" { if arg == "-dynlink" {

View File

@ -109,9 +109,11 @@ func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc, myimportpath string
continue continue
} }
linkpcln(ctxt, s) linkpcln(ctxt, s)
if myimportpath != "" {
ctxt.populateDWARF(plist.Curfn, s, myimportpath) ctxt.populateDWARF(plist.Curfn, s, myimportpath)
} }
} }
}
func (ctxt *Link) InitTextSym(s *LSym, flag int) { func (ctxt *Link) InitTextSym(s *LSym, flag int) {
if s == nil { if s == nil {