Source file src/cmd/asm/internal/flags/flags.go

     1  // Copyright 2015 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 flags implements top-level flags and the usage message for the assembler.
     6  package flags
     7  
     8  import (
     9  	"cmd/internal/obj"
    10  	"cmd/internal/objabi"
    11  	"flag"
    12  	"fmt"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  )
    17  
    18  var (
    19  	Debug      = flag.Bool("debug", false, "dump instructions as they are parsed")
    20  	OutputFile = flag.String("o", "", "output file; default foo.o for /a/b/c/foo.s as first argument")
    21  	TrimPath   = flag.String("trimpath", "", "remove prefix from recorded source file paths")
    22  	Shared     = flag.Bool("shared", false, "generate code that can be linked into a shared library")
    23  	Dynlink    = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
    24  	Linkshared = flag.Bool("linkshared", false, "generate code that will be linked against Go shared libraries")
    25  	AllErrors  = flag.Bool("e", false, "no limit on number of errors reported")
    26  	SymABIs    = flag.Bool("gensymabis", false, "write symbol ABI information to output file, don't assemble")
    27  	Importpath = flag.String("p", obj.UnlinkablePkg, "set expected package import to path")
    28  	Spectre    = flag.String("spectre", "", "enable spectre mitigations in `list` (all, ret)")
    29  )
    30  
    31  var DebugFlags struct {
    32  	CompressInstructions int    `help:"use compressed instructions when possible (if supported by architecture)"`
    33  	MayMoreStack         string `help:"call named function before all stack growth checks"`
    34  	PCTab                string `help:"print named pc-value table\nOne of: pctospadj, pctofile, pctoline, pctoinline, pctopcdata"`
    35  }
    36  
    37  var (
    38  	D        MultiFlag
    39  	I        MultiFlag
    40  	PrintOut int
    41  	DebugV   bool
    42  )
    43  
    44  func init() {
    45  	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
    46  	flag.Var(&I, "I", "include directory; can be set multiple times")
    47  	flag.BoolVar(&DebugV, "v", false, "print debug output")
    48  	flag.Var(objabi.NewDebugFlag(&DebugFlags, nil), "d", "enable debugging settings; try -d help")
    49  	objabi.AddVersionFlag() // -V
    50  	objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
    51  
    52  	DebugFlags.CompressInstructions = 1
    53  }
    54  
    55  // MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
    56  type MultiFlag []string
    57  
    58  func (m *MultiFlag) String() string {
    59  	if len(*m) == 0 {
    60  		return ""
    61  	}
    62  	return fmt.Sprint(*m)
    63  }
    64  
    65  func (m *MultiFlag) Set(val string) error {
    66  	(*m) = append(*m, val)
    67  	return nil
    68  }
    69  
    70  func Usage() {
    71  	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
    72  	fmt.Fprintf(os.Stderr, "Flags:\n")
    73  	flag.PrintDefaults()
    74  	os.Exit(2)
    75  }
    76  
    77  func Parse() {
    78  	objabi.Flagparse(Usage)
    79  	if flag.NArg() == 0 {
    80  		flag.Usage()
    81  	}
    82  
    83  	// Flag refinement.
    84  	if *OutputFile == "" {
    85  		if flag.NArg() != 1 {
    86  			flag.Usage()
    87  		}
    88  		input := filepath.Base(flag.Arg(0))
    89  		input = strings.TrimSuffix(input, ".s")
    90  		*OutputFile = fmt.Sprintf("%s.o", input)
    91  	}
    92  }
    93  

View as plain text