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 <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cherry Mui 2022-05-05 17:45:27 -04:00
parent b89a194889
commit 6e03de7b83
5 changed files with 12 additions and 11 deletions

View File

@ -6,6 +6,7 @@
package flags package flags
import ( import (
"cmd/internal/obj"
"cmd/internal/objabi" "cmd/internal/objabi"
"flag" "flag"
"fmt" "fmt"
@ -23,7 +24,7 @@ var (
Linkshared = flag.Bool("linkshared", false, "generate code that will be linked against Go shared libraries") 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") 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") Importpath = flag.String("p", obj.UnlinkablePkg, "set expected package import to path")
Spectre = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)") 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") CompilingRuntime = flag.Bool("compiling-runtime", false, "source to be compiled is part of the Go runtime")
) )

View File

@ -281,10 +281,10 @@ const SymSize = stringRefSize + 2 + 1 + 1 + 1 + 4 + 4
const SymABIstatic = ^uint16(0) const SymABIstatic = ^uint16(0)
const ( const (
ObjFlagShared = 1 << iota // this object is built with -shared ObjFlagShared = 1 << iota // this object is built with -shared
ObjFlagNeedNameExpansion // the linker needs to expand `"".` to package path in symbol names _ // was ObjFlagNeedNameExpansion
ObjFlagFromAssembly // object is from asm src, not go ObjFlagFromAssembly // object is from asm src, not go
ObjFlagUnlinkable // unlinkable package (linker will emit an error) ObjFlagUnlinkable // unlinkable package (linker will emit an error)
) )
// Sym.Flag // Sym.Flag
@ -873,6 +873,6 @@ func (r *Reader) Flags() uint32 {
} }
func (r *Reader) Shared() bool { return r.Flags()&ObjFlagShared != 0 } 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) FromAssembly() bool { return r.Flags()&ObjFlagFromAssembly != 0 }
func (r *Reader) Unlinkable() bool { return r.Flags()&ObjFlagUnlinkable != 0 } func (r *Reader) Unlinkable() bool { return r.Flags()&ObjFlagUnlinkable != 0 }

View File

@ -51,7 +51,7 @@ func WriteObjFile(ctxt *Link, b *bio.Writer) {
flags |= goobj.ObjFlagUnlinkable flags |= goobj.ObjFlagUnlinkable
} }
if w.pkgpath == "" { if w.pkgpath == "" {
flags |= goobj.ObjFlagNeedNameExpansion log.Fatal("empty package path")
} }
if ctxt.IsAsm { if ctxt.IsAsm {
flags |= goobj.ObjFlagFromAssembly flags |= goobj.ObjFlagFromAssembly

View File

@ -235,9 +235,9 @@ void foo() {
cflags := strings.Fields(runGo("env", "GOGCCFLAGS")) cflags := strings.Fields(runGo("env", "GOGCCFLAGS"))
// Compile, assemble and pack the Go and C code. // 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", "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")...) run(cc, append(cflags, "-c", "-o", "x3.o", "x.c")...)
runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o") runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o")

View File

@ -1024,7 +1024,7 @@ func (t *test) run() {
t.err = fmt.Errorf("write empty go_asm.h: %s", err) t.err = fmt.Errorf("write empty go_asm.h: %s", err)
return return
} }
cmd := []string{goTool(), "tool", "asm", "-gensymabis", "-o", "symabis"} cmd := []string{goTool(), "tool", "asm", "-p=main", "-gensymabis", "-o", "symabis"}
cmd = append(cmd, asms...) cmd = append(cmd, asms...)
_, err = runcmd(cmd...) _, err = runcmd(cmd...)
if err != nil { if err != nil {
@ -1045,7 +1045,7 @@ func (t *test) run() {
} }
objs = append(objs, "go.o") objs = append(objs, "go.o")
if len(asms) > 0 { 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...) cmd = append(cmd, asms...)
_, err = runcmd(cmd...) _, err = runcmd(cmd...)
if err != nil { if err != nil {