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