2015-01-22 10:48:33 -08:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2016-04-08 19:30:41 +10:00
|
|
|
"bufio"
|
2015-01-22 10:48:33 -08:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"cmd/asm/internal/arch"
|
|
|
|
|
"cmd/asm/internal/asm"
|
|
|
|
|
"cmd/asm/internal/flags"
|
|
|
|
|
"cmd/asm/internal/lex"
|
|
|
|
|
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/bio"
|
2015-01-22 10:48:33 -08:00
|
|
|
"cmd/internal/obj"
|
2017-04-18 12:53:25 -07:00
|
|
|
"cmd/internal/objabi"
|
2015-01-22 10:48:33 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
log.SetPrefix("asm: ")
|
|
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
GOARCH := objabi.GOARCH
|
2015-01-22 10:48:33 -08:00
|
|
|
|
|
|
|
|
architecture := arch.Set(GOARCH)
|
|
|
|
|
if architecture == nil {
|
2016-02-19 16:02:54 -08:00
|
|
|
log.Fatalf("unrecognized architecture %s", GOARCH)
|
2015-01-22 10:48:33 -08:00
|
|
|
}
|
|
|
|
|
|
2015-05-21 13:28:17 -04:00
|
|
|
flags.Parse()
|
2015-01-22 10:48:33 -08:00
|
|
|
|
|
|
|
|
ctxt := obj.Linknew(architecture.LinkArch)
|
|
|
|
|
if *flags.PrintOut {
|
2018-12-07 10:00:36 -08:00
|
|
|
ctxt.Debugasm = 1
|
2015-01-22 10:48:33 -08:00
|
|
|
}
|
2015-03-30 00:49:25 +00:00
|
|
|
ctxt.Flag_dynlink = *flags.Dynlink
|
2016-04-13 18:41:59 -07:00
|
|
|
ctxt.Flag_shared = *flags.Shared || *flags.Dynlink
|
2020-04-23 19:52:31 -04:00
|
|
|
ctxt.IsAsm = true
|
2020-01-17 13:54:30 -05:00
|
|
|
switch *flags.Spectre {
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("unknown setting -spectre=%s", *flags.Spectre)
|
|
|
|
|
os.Exit(2)
|
|
|
|
|
case "":
|
|
|
|
|
// nothing
|
|
|
|
|
case "index":
|
|
|
|
|
// known to compiler; ignore here so people can use
|
|
|
|
|
// the same list with -gcflags=-spectre=LIST and -asmflags=-spectrre=LIST
|
|
|
|
|
case "all", "ret":
|
|
|
|
|
ctxt.Retpoline = true
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 19:30:41 +10:00
|
|
|
ctxt.Bso = bufio.NewWriter(os.Stdout)
|
2015-05-01 11:51:47 +10:00
|
|
|
defer ctxt.Bso.Flush()
|
2016-04-08 19:30:41 +10:00
|
|
|
|
2017-04-06 13:42:31 -07:00
|
|
|
architecture.Init(ctxt)
|
|
|
|
|
|
2016-04-08 19:30:41 +10:00
|
|
|
// Create object file, write header.
|
2019-09-11 16:11:31 -04:00
|
|
|
buf, err := bio.Create(*flags.OutputFile)
|
2016-04-08 19:30:41 +10:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2019-09-11 16:11:31 -04:00
|
|
|
defer buf.Close()
|
2016-04-12 17:58:46 -07:00
|
|
|
|
2018-10-19 16:24:59 -04:00
|
|
|
if !*flags.SymABIs {
|
|
|
|
|
fmt.Fprintf(buf, "go object %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version)
|
|
|
|
|
fmt.Fprintf(buf, "!\n")
|
|
|
|
|
}
|
2015-01-22 10:48:33 -08:00
|
|
|
|
2016-05-18 13:27:11 -07:00
|
|
|
var ok, diag bool
|
|
|
|
|
var failedFile string
|
|
|
|
|
for _, f := range flag.Args() {
|
2016-12-09 17:15:05 -08:00
|
|
|
lexer := lex.NewLexer(f)
|
2016-05-18 13:27:11 -07:00
|
|
|
parser := asm.NewParser(ctxt, architecture, lexer)
|
|
|
|
|
ctxt.DiagFunc = func(format string, args ...interface{}) {
|
|
|
|
|
diag = true
|
|
|
|
|
log.Printf(format, args...)
|
|
|
|
|
}
|
2018-10-19 16:24:59 -04:00
|
|
|
if *flags.SymABIs {
|
|
|
|
|
ok = parser.ParseSymABIs(buf)
|
|
|
|
|
} else {
|
|
|
|
|
pList := new(obj.Plist)
|
|
|
|
|
pList.Firstpc, ok = parser.Parse()
|
|
|
|
|
// reports errors to parser.Errorf
|
|
|
|
|
if ok {
|
2020-03-06 12:43:59 -05:00
|
|
|
obj.Flushplist(ctxt, pList, nil, *flags.Importpath)
|
2018-10-19 16:24:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-18 13:27:11 -07:00
|
|
|
if !ok {
|
|
|
|
|
failedFile = f
|
|
|
|
|
break
|
|
|
|
|
}
|
2016-01-21 22:48:29 -05:00
|
|
|
}
|
2018-10-19 16:24:59 -04:00
|
|
|
if ok && !*flags.SymABIs {
|
2020-05-18 18:47:17 -04:00
|
|
|
ctxt.NumberSyms()
|
2020-05-18 18:27:10 -04:00
|
|
|
obj.WriteObjFile(ctxt, buf, *flags.Importpath)
|
2016-01-21 22:48:29 -05:00
|
|
|
}
|
|
|
|
|
if !ok || diag {
|
2016-05-18 13:27:11 -07:00
|
|
|
if failedFile != "" {
|
|
|
|
|
log.Printf("assembly of %s failed", failedFile)
|
|
|
|
|
} else {
|
|
|
|
|
log.Print("assembly failed")
|
|
|
|
|
}
|
2019-09-11 16:11:31 -04:00
|
|
|
buf.Close()
|
2015-04-08 11:24:48 -07:00
|
|
|
os.Remove(*flags.OutputFile)
|
2015-01-22 10:48:33 -08:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|