From 6e03de7b83426fa2f598c428a19db707a845bf7e Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Thu, 5 May 2022 17:45:27 -0400 Subject: [PATCH] cmd/asm: require -p flag CL 391014 requires the compiler to be invoked with the -p flag, to specify the package path. Later, CL 394217 makes the compiler to produce an unlinkable object file, so "go tool compile x.go" can still be used on the command line. This CL does the same for the assembler, requiring -p, otherwise generating an unlinkable object. No special case for the main package, as the main package cannot be only assembly code, and there is no way to tell if it is the main package from an assembly file. Now we guarantee that we always have an expanded package path in the object file. A later CL will delete the name expansion code in the linker. Change-Id: I8c10661aaea2ff794614924ead958d80e7e2487d Reviewed-on: https://go-review.googlesource.com/c/go/+/404298 Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot Reviewed-by: Than McIntosh Reviewed-by: Matthew Dempsky --- src/cmd/asm/internal/flags/flags.go | 3 ++- src/cmd/internal/goobj/objfile.go | 10 +++++----- src/cmd/internal/obj/objfile.go | 2 +- src/cmd/link/link_test.go | 4 ++-- test/run.go | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/cmd/asm/internal/flags/flags.go b/src/cmd/asm/internal/flags/flags.go index 273d422370..1c8b908860 100644 --- a/src/cmd/asm/internal/flags/flags.go +++ b/src/cmd/asm/internal/flags/flags.go @@ -6,6 +6,7 @@ package flags import ( + "cmd/internal/obj" "cmd/internal/objabi" "flag" "fmt" @@ -23,7 +24,7 @@ var ( Linkshared = flag.Bool("linkshared", false, "generate code that will be linked against Go 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") + Importpath = flag.String("p", obj.UnlinkablePkg, "set expected package import to path") Spectre = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)") CompilingRuntime = flag.Bool("compiling-runtime", false, "source to be compiled is part of the Go runtime") ) diff --git a/src/cmd/internal/goobj/objfile.go b/src/cmd/internal/goobj/objfile.go index 665fa41475..34c5bb97f8 100644 --- a/src/cmd/internal/goobj/objfile.go +++ b/src/cmd/internal/goobj/objfile.go @@ -281,10 +281,10 @@ const SymSize = stringRefSize + 2 + 1 + 1 + 1 + 4 + 4 const SymABIstatic = ^uint16(0) const ( - ObjFlagShared = 1 << iota // this object is built with -shared - ObjFlagNeedNameExpansion // the linker needs to expand `"".` to package path in symbol names - ObjFlagFromAssembly // object is from asm src, not go - ObjFlagUnlinkable // unlinkable package (linker will emit an error) + ObjFlagShared = 1 << iota // this object is built with -shared + _ // was ObjFlagNeedNameExpansion + ObjFlagFromAssembly // object is from asm src, not go + ObjFlagUnlinkable // unlinkable package (linker will emit an error) ) // Sym.Flag @@ -873,6 +873,6 @@ func (r *Reader) Flags() uint32 { } func (r *Reader) Shared() bool { return r.Flags()&ObjFlagShared != 0 } -func (r *Reader) NeedNameExpansion() bool { return r.Flags()&ObjFlagNeedNameExpansion != 0 } +func (r *Reader) NeedNameExpansion() bool { return false } // TODO: delete func (r *Reader) FromAssembly() bool { return r.Flags()&ObjFlagFromAssembly != 0 } func (r *Reader) Unlinkable() bool { return r.Flags()&ObjFlagUnlinkable != 0 } diff --git a/src/cmd/internal/obj/objfile.go b/src/cmd/internal/obj/objfile.go index d31afda703..89339b0147 100644 --- a/src/cmd/internal/obj/objfile.go +++ b/src/cmd/internal/obj/objfile.go @@ -51,7 +51,7 @@ func WriteObjFile(ctxt *Link, b *bio.Writer) { flags |= goobj.ObjFlagUnlinkable } if w.pkgpath == "" { - flags |= goobj.ObjFlagNeedNameExpansion + log.Fatal("empty package path") } if ctxt.IsAsm { flags |= goobj.ObjFlagFromAssembly diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go index ac68008d8d..d86f81fac8 100644 --- a/src/cmd/link/link_test.go +++ b/src/cmd/link/link_test.go @@ -235,9 +235,9 @@ void foo() { cflags := strings.Fields(runGo("env", "GOGCCFLAGS")) // Compile, assemble and pack the Go and C code. - runGo("tool", "asm", "-gensymabis", "-o", "symabis", "x.s") + runGo("tool", "asm", "-p=main", "-gensymabis", "-o", "symabis", "x.s") runGo("tool", "compile", "-symabis", "symabis", "-p=main", "-o", "x1.o", "main.go") - runGo("tool", "asm", "-o", "x2.o", "x.s") + runGo("tool", "asm", "-p=main", "-o", "x2.o", "x.s") run(cc, append(cflags, "-c", "-o", "x3.o", "x.c")...) runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o") diff --git a/test/run.go b/test/run.go index 27e16f6892..00f869bc2b 100644 --- a/test/run.go +++ b/test/run.go @@ -1024,7 +1024,7 @@ func (t *test) run() { t.err = fmt.Errorf("write empty go_asm.h: %s", err) return } - cmd := []string{goTool(), "tool", "asm", "-gensymabis", "-o", "symabis"} + cmd := []string{goTool(), "tool", "asm", "-p=main", "-gensymabis", "-o", "symabis"} cmd = append(cmd, asms...) _, err = runcmd(cmd...) if err != nil { @@ -1045,7 +1045,7 @@ func (t *test) run() { } objs = append(objs, "go.o") if len(asms) > 0 { - cmd = []string{goTool(), "tool", "asm", "-e", "-I", ".", "-o", "asm.o"} + cmd = []string{goTool(), "tool", "asm", "-p=main", "-e", "-I", ".", "-o", "asm.o"} cmd = append(cmd, asms...) _, err = runcmd(cmd...) if err != nil {