Source file src/cmd/asm/main.go

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bufio"
     9  	"flag"
    10  	"fmt"
    11  	"internal/buildcfg"
    12  	"log"
    13  	"os"
    14  
    15  	"cmd/asm/internal/arch"
    16  	"cmd/asm/internal/asm"
    17  	"cmd/asm/internal/flags"
    18  	"cmd/asm/internal/lex"
    19  
    20  	"cmd/internal/bio"
    21  	"cmd/internal/obj"
    22  	"cmd/internal/objabi"
    23  	"cmd/internal/telemetry/counter"
    24  )
    25  
    26  func main() {
    27  	log.SetFlags(0)
    28  	log.SetPrefix("asm: ")
    29  	counter.Open()
    30  
    31  	buildcfg.Check()
    32  	GOARCH := buildcfg.GOARCH
    33  
    34  	flags.Parse()
    35  	counter.Inc("asm/invocations")
    36  	counter.CountFlags("asm/flag:", *flag.CommandLine)
    37  
    38  	architecture := arch.Set(GOARCH, *flags.Shared || *flags.Dynlink)
    39  	if architecture == nil {
    40  		log.Fatalf("unrecognized architecture %s", GOARCH)
    41  	}
    42  	ctxt := obj.Linknew(architecture.LinkArch)
    43  	ctxt.Debugasm = flags.PrintOut
    44  	ctxt.Debugvlog = flags.DebugV
    45  	ctxt.Flag_dynlink = *flags.Dynlink
    46  	ctxt.Flag_linkshared = *flags.Linkshared
    47  	ctxt.Flag_shared = *flags.Shared || *flags.Dynlink
    48  	ctxt.Flag_maymorestack = flags.DebugFlags.MayMoreStack
    49  	ctxt.Debugpcln = flags.DebugFlags.PCTab
    50  	ctxt.IsAsm = true
    51  	ctxt.Pkgpath = *flags.Importpath
    52  	ctxt.DwTextCount = objabi.DummyDwarfFunctionCountForAssembler()
    53  	switch *flags.Spectre {
    54  	default:
    55  		log.Printf("unknown setting -spectre=%s", *flags.Spectre)
    56  		os.Exit(2)
    57  	case "":
    58  		// nothing
    59  	case "index":
    60  		// known to compiler; ignore here so people can use
    61  		// the same list with -gcflags=-spectre=LIST and -asmflags=-spectrre=LIST
    62  	case "all", "ret":
    63  		ctxt.Retpoline = true
    64  	}
    65  
    66  	ctxt.Bso = bufio.NewWriter(os.Stdout)
    67  	defer ctxt.Bso.Flush()
    68  
    69  	architecture.Init(ctxt)
    70  
    71  	// Create object file, write header.
    72  	buf, err := bio.Create(*flags.OutputFile)
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  	defer buf.Close()
    77  
    78  	if !*flags.SymABIs {
    79  		buf.WriteString(objabi.HeaderString())
    80  		fmt.Fprintf(buf, "!\n")
    81  	}
    82  
    83  	// Set macros for GOEXPERIMENTs so we can easily switch
    84  	// runtime assembly code based on them.
    85  	if objabi.LookupPkgSpecial(ctxt.Pkgpath).AllowAsmABI {
    86  		for _, exp := range buildcfg.Experiment.Enabled() {
    87  			flags.D = append(flags.D, "GOEXPERIMENT_"+exp)
    88  		}
    89  	}
    90  
    91  	var ok, diag bool
    92  	var failedFile string
    93  	for _, f := range flag.Args() {
    94  		lexer := lex.NewLexer(f)
    95  		parser := asm.NewParser(ctxt, architecture, lexer)
    96  		ctxt.DiagFunc = func(format string, args ...interface{}) {
    97  			diag = true
    98  			log.Printf(format, args...)
    99  		}
   100  		if *flags.SymABIs {
   101  			ok = parser.ParseSymABIs(buf)
   102  		} else {
   103  			pList := new(obj.Plist)
   104  			pList.Firstpc, ok = parser.Parse()
   105  			// reports errors to parser.Errorf
   106  			if ok {
   107  				obj.Flushplist(ctxt, pList, nil)
   108  			}
   109  		}
   110  		if !ok {
   111  			failedFile = f
   112  			break
   113  		}
   114  	}
   115  	if ok && !*flags.SymABIs {
   116  		ctxt.NumberSyms()
   117  		obj.WriteObjFile(ctxt, buf)
   118  	}
   119  	if !ok || diag {
   120  		if failedFile != "" {
   121  			log.Printf("assembly of %s failed", failedFile)
   122  		} else {
   123  			log.Print("assembly failed")
   124  		}
   125  		buf.Close()
   126  		os.Remove(*flags.OutputFile)
   127  		os.Exit(1)
   128  	}
   129  }
   130  

View as plain text