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

View as plain text