From 9cae3b33c3553932ca9689dda0de8ff215a39c89 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Fri, 6 Mar 2020 12:43:59 -0500 Subject: [PATCH] [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 TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang Reviewed-by: Jeremy Faller --- src/cmd/asm/internal/flags/flags.go | 1 + src/cmd/asm/main.go | 2 +- src/cmd/go/internal/work/gc.go | 21 ++++++++++++++------- src/cmd/internal/obj/plist.go | 4 +++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cmd/asm/internal/flags/flags.go b/src/cmd/asm/internal/flags/flags.go index 5fe3fd9d53..6aefc96639 100644 --- a/src/cmd/asm/internal/flags/flags.go +++ b/src/cmd/asm/internal/flags/flags.go @@ -23,6 +23,7 @@ var ( 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") 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 ( diff --git a/src/cmd/asm/main.go b/src/cmd/asm/main.go index 51f995055c..d42093a48f 100644 --- a/src/cmd/asm/main.go +++ b/src/cmd/asm/main.go @@ -73,7 +73,7 @@ func main() { pList.Firstpc, ok = parser.Parse() // reports errors to parser.Errorf if ok { - obj.Flushplist(ctxt, pList, nil, "") + obj.Flushplist(ctxt, pList, nil, *flags.Importpath) } } if !ok { diff --git a/src/cmd/go/internal/work/gc.go b/src/cmd/go/internal/work/gc.go index 7d17c0c01e..78db845ae7 100644 --- a/src/cmd/go/internal/work/gc.go +++ b/src/cmd/go/internal/work/gc.go @@ -37,6 +37,17 @@ func (gcToolchain) linker() string { 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) { p := a.Package objdir := a.Objdir @@ -47,12 +58,7 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, s ofile = objdir + out } - pkgpath := p.ImportPath - if cfg.BuildBuildmode == "plugin" { - pkgpath = pluginPath(a) - } else if p.Name == "main" && !p.Internal.ForceLibrary { - pkgpath = "main" - } + pkgpath := pkgPath(a) gcargs := []string{"-p", pkgpath} if p.Module != nil && p.Module.GoVersion != "" && allowedVersion(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{} { // Add -I pkg/GOOS_GOARCH so #include "textflag.h" works in .s files. 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" { for _, arg := range forcedAsmflags { if arg == "-dynlink" { diff --git a/src/cmd/internal/obj/plist.go b/src/cmd/internal/obj/plist.go index 7579dd0390..4d02042956 100644 --- a/src/cmd/internal/obj/plist.go +++ b/src/cmd/internal/obj/plist.go @@ -109,7 +109,9 @@ func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc, myimportpath string continue } linkpcln(ctxt, s) - ctxt.populateDWARF(plist.Curfn, s, myimportpath) + if myimportpath != "" { + ctxt.populateDWARF(plist.Curfn, s, myimportpath) + } } }