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.CompressInstructions = flags.DebugFlags.CompressInstructions != 0
    44  	ctxt.Debugasm = flags.PrintOut
    45  	ctxt.Debugvlog = flags.DebugV
    46  	ctxt.Flag_dynlink = *flags.Dynlink
    47  	ctxt.Flag_linkshared = *flags.Linkshared
    48  	ctxt.Flag_shared = *flags.Shared || *flags.Dynlink
    49  	ctxt.Flag_maymorestack = flags.DebugFlags.MayMoreStack
    50  	ctxt.Debugpcln = flags.DebugFlags.PCTab
    51  	ctxt.IsAsm = true
    52  	ctxt.Pkgpath = *flags.Importpath
    53  	ctxt.DwTextCount = objabi.DummyDwarfFunctionCountForAssembler()
    54  	switch *flags.Spectre {
    55  	default:
    56  		log.Printf("unknown setting -spectre=%s", *flags.Spectre)
    57  		os.Exit(2)
    58  	case "":
    59  		// nothing
    60  	case "index":
    61  		// known to compiler; ignore here so people can use
    62  		// the same list with -gcflags=-spectre=LIST and -asmflags=-spectre=LIST
    63  	case "all", "ret":
    64  		ctxt.Retpoline = true
    65  	}
    66  
    67  	ctxt.Bso = bufio.NewWriter(os.Stdout)
    68  	defer ctxt.Bso.Flush()
    69  
    70  	architecture.Init(ctxt)
    71  
    72  	// Create object file, write header.
    73  	buf, err := bio.Create(*flags.OutputFile)
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  	defer buf.Close()
    78  
    79  	if !*flags.SymABIs {
    80  		buf.WriteString(objabi.HeaderString())
    81  		fmt.Fprintf(buf, "!\n")
    82  	}
    83  
    84  	// Set macros for GOEXPERIMENTs so we can easily switch
    85  	// runtime assembly code based on them.
    86  	if objabi.LookupPkgSpecial(ctxt.Pkgpath).AllowAsmABI {
    87  		for _, exp := range buildcfg.Experiment.Enabled() {
    88  			flags.D = append(flags.D, "GOEXPERIMENT_"+exp)
    89  		}
    90  	}
    91  
    92  	var ok, diag bool
    93  	var failedFile string
    94  	for _, f := range flag.Args() {
    95  		lexer := lex.NewLexer(f)
    96  		parser := asm.NewParser(ctxt, architecture, lexer)
    97  		ctxt.DiagFunc = func(format string, args ...any) {
    98  			diag = true
    99  			log.Printf(format, args...)
   100  		}
   101  		if *flags.SymABIs {
   102  			ok = parser.ParseSymABIs(buf)
   103  		} else {
   104  			pList := new(obj.Plist)
   105  			pList.Firstpc, ok = parser.Parse()
   106  			// reports errors to parser.Errorf
   107  			if ok {
   108  				obj.Flushplist(ctxt, pList, nil)
   109  			}
   110  		}
   111  		if !ok {
   112  			failedFile = f
   113  			break
   114  		}
   115  	}
   116  	if ok && !*flags.SymABIs {
   117  		ctxt.NumberSyms()
   118  		obj.WriteObjFile(ctxt, buf)
   119  	}
   120  	if !ok || diag {
   121  		if failedFile != "" {
   122  			log.Printf("assembly of %s failed", failedFile)
   123  		} else {
   124  			log.Print("assembly failed")
   125  		}
   126  		buf.Close()
   127  		os.Remove(*flags.OutputFile)
   128  		os.Exit(1)
   129  	}
   130  }
   131  

View as plain text