Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/asm/main.go

Documentation: cmd/asm

     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  )
    24  
    25  func main() {
    26  	log.SetFlags(0)
    27  	log.SetPrefix("asm: ")
    28  
    29  	buildcfg.Check()
    30  	GOARCH := buildcfg.GOARCH
    31  
    32  	architecture := arch.Set(GOARCH)
    33  	if architecture == nil {
    34  		log.Fatalf("unrecognized architecture %s", GOARCH)
    35  	}
    36  
    37  	flags.Parse()
    38  
    39  	ctxt := obj.Linknew(architecture.LinkArch)
    40  	ctxt.Debugasm = flags.PrintOut
    41  	ctxt.Debugvlog = flags.DebugV
    42  	ctxt.Flag_dynlink = *flags.Dynlink
    43  	ctxt.Flag_linkshared = *flags.Linkshared
    44  	ctxt.Flag_shared = *flags.Shared || *flags.Dynlink
    45  	ctxt.IsAsm = true
    46  	ctxt.Pkgpath = *flags.Importpath
    47  	switch *flags.Spectre {
    48  	default:
    49  		log.Printf("unknown setting -spectre=%s", *flags.Spectre)
    50  		os.Exit(2)
    51  	case "":
    52  		// nothing
    53  	case "index":
    54  		// known to compiler; ignore here so people can use
    55  		// the same list with -gcflags=-spectre=LIST and -asmflags=-spectrre=LIST
    56  	case "all", "ret":
    57  		ctxt.Retpoline = true
    58  	}
    59  
    60  	ctxt.Bso = bufio.NewWriter(os.Stdout)
    61  	defer ctxt.Bso.Flush()
    62  
    63  	architecture.Init(ctxt)
    64  
    65  	// Create object file, write header.
    66  	buf, err := bio.Create(*flags.OutputFile)
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	defer buf.Close()
    71  
    72  	if !*flags.SymABIs {
    73  		buf.WriteString(objabi.HeaderString())
    74  		fmt.Fprintf(buf, "!\n")
    75  	}
    76  
    77  	var ok, diag bool
    78  	var failedFile string
    79  	for _, f := range flag.Args() {
    80  		lexer := lex.NewLexer(f)
    81  		parser := asm.NewParser(ctxt, architecture, lexer,
    82  			*flags.CompilingRuntime)
    83  		ctxt.DiagFunc = func(format string, args ...interface{}) {
    84  			diag = true
    85  			log.Printf(format, args...)
    86  		}
    87  		if *flags.SymABIs {
    88  			ok = parser.ParseSymABIs(buf)
    89  		} else {
    90  			pList := new(obj.Plist)
    91  			pList.Firstpc, ok = parser.Parse()
    92  			// reports errors to parser.Errorf
    93  			if ok {
    94  				obj.Flushplist(ctxt, pList, nil, *flags.Importpath)
    95  			}
    96  		}
    97  		if !ok {
    98  			failedFile = f
    99  			break
   100  		}
   101  	}
   102  	if ok && !*flags.SymABIs {
   103  		ctxt.NumberSyms()
   104  		obj.WriteObjFile(ctxt, buf)
   105  	}
   106  	if !ok || diag {
   107  		if failedFile != "" {
   108  			log.Printf("assembly of %s failed", failedFile)
   109  		} else {
   110  			log.Print("assembly failed")
   111  		}
   112  		buf.Close()
   113  		os.Remove(*flags.OutputFile)
   114  		os.Exit(1)
   115  	}
   116  }
   117  

View as plain text