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.
6package flags
7
8import (
9	"cmd/internal/obj"
10	"cmd/internal/objabi"
11	"flag"
12	"fmt"
13	"os"
14	"path/filepath"
15	"strings"
16)
17
18var (
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
31var DebugFlags struct {
32	MayMoreStack string `help:"call named function before all stack growth checks"`
33	PCTab        string `help:"print named pc-value table\nOne of: pctospadj, pctofile, pctoline, pctoinline, pctopcdata"`
34}
35
36var (
37	D        MultiFlag
38	I        MultiFlag
39	PrintOut int
40	DebugV   bool
41)
42
43func init() {
44	flag.Var(&D, "D", "predefined symbol with optional simple value -D=identifier=value; can be set multiple times")
45	flag.Var(&I, "I", "include directory; can be set multiple times")
46	flag.BoolVar(&DebugV, "v", false, "print debug output")
47	flag.Var(objabi.NewDebugFlag(&DebugFlags, nil), "d", "enable debugging settings; try -d help")
48	objabi.AddVersionFlag() // -V
49	objabi.Flagcount("S", "print assembly and machine code", &PrintOut)
50}
51
52// MultiFlag allows setting a value multiple times to collect a list, as in -I=dir1 -I=dir2.
53type MultiFlag []string
54
55func (m *MultiFlag) String() string {
56	if len(*m) == 0 {
57		return ""
58	}
59	return fmt.Sprint(*m)
60}
61
62func (m *MultiFlag) Set(val string) error {
63	(*m) = append(*m, val)
64	return nil
65}
66
67func Usage() {
68	fmt.Fprintf(os.Stderr, "usage: asm [options] file.s ...\n")
69	fmt.Fprintf(os.Stderr, "Flags:\n")
70	flag.PrintDefaults()
71	os.Exit(2)
72}
73
74func Parse() {
75	objabi.Flagparse(Usage)
76	if flag.NArg() == 0 {
77		flag.Usage()
78	}
79
80	// Flag refinement.
81	if *OutputFile == "" {
82		if flag.NArg() != 1 {
83			flag.Usage()
84		}
85		input := filepath.Base(flag.Arg(0))
86		input = strings.TrimSuffix(input, ".s")
87		*OutputFile = fmt.Sprintf("%s.o", input)
88	}
89}
90