Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/internal/objabi/util.go

Documentation: cmd/internal/objabi

     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 objabi
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  func envOr(key, value string) string {
    15  	if x := os.Getenv(key); x != "" {
    16  		return x
    17  	}
    18  	return value
    19  }
    20  
    21  var (
    22  	defaultGOROOT string // set by linker
    23  
    24  	GOROOT   = envOr("GOROOT", defaultGOROOT)
    25  	GOARCH   = envOr("GOARCH", defaultGOARCH)
    26  	GOOS     = envOr("GOOS", defaultGOOS)
    27  	GO386    = envOr("GO386", defaultGO386)
    28  	GOARM    = goarm()
    29  	GOMIPS   = gomips()
    30  	GOMIPS64 = gomips64()
    31  	GOPPC64  = goppc64()
    32  	GOWASM   = gowasm()
    33  	GO_LDSO  = defaultGO_LDSO
    34  	Version  = version
    35  )
    36  
    37  const (
    38  	ElfRelocOffset   = 256
    39  	MachoRelocOffset = 2048 // reserve enough space for ELF relocations
    40  )
    41  
    42  func goarm() int {
    43  	def := defaultGOARM
    44  	if GOOS == "android" && GOARCH == "arm" {
    45  		// Android arm devices always support GOARM=7.
    46  		def = "7"
    47  	}
    48  	switch v := envOr("GOARM", def); v {
    49  	case "5":
    50  		return 5
    51  	case "6":
    52  		return 6
    53  	case "7":
    54  		return 7
    55  	}
    56  	// Fail here, rather than validate at multiple call sites.
    57  	log.Fatalf("Invalid GOARM value. Must be 5, 6, or 7.")
    58  	panic("unreachable")
    59  }
    60  
    61  func gomips() string {
    62  	switch v := envOr("GOMIPS", defaultGOMIPS); v {
    63  	case "hardfloat", "softfloat":
    64  		return v
    65  	}
    66  	log.Fatalf("Invalid GOMIPS value. Must be hardfloat or softfloat.")
    67  	panic("unreachable")
    68  }
    69  
    70  func gomips64() string {
    71  	switch v := envOr("GOMIPS64", defaultGOMIPS64); v {
    72  	case "hardfloat", "softfloat":
    73  		return v
    74  	}
    75  	log.Fatalf("Invalid GOMIPS64 value. Must be hardfloat or softfloat.")
    76  	panic("unreachable")
    77  }
    78  
    79  func goppc64() int {
    80  	switch v := envOr("GOPPC64", defaultGOPPC64); v {
    81  	case "power8":
    82  		return 8
    83  	case "power9":
    84  		return 9
    85  	}
    86  	log.Fatalf("Invalid GOPPC64 value. Must be power8 or power9.")
    87  	panic("unreachable")
    88  }
    89  
    90  type gowasmFeatures struct {
    91  	SignExt bool
    92  	SatConv bool
    93  }
    94  
    95  func (f gowasmFeatures) String() string {
    96  	var flags []string
    97  	if f.SatConv {
    98  		flags = append(flags, "satconv")
    99  	}
   100  	if f.SignExt {
   101  		flags = append(flags, "signext")
   102  	}
   103  	return strings.Join(flags, ",")
   104  }
   105  
   106  func gowasm() (f gowasmFeatures) {
   107  	for _, opt := range strings.Split(envOr("GOWASM", ""), ",") {
   108  		switch opt {
   109  		case "satconv":
   110  			f.SatConv = true
   111  		case "signext":
   112  			f.SignExt = true
   113  		case "":
   114  			// ignore
   115  		default:
   116  			log.Fatalf("Invalid GOWASM value. No such feature: " + opt)
   117  		}
   118  	}
   119  	return
   120  }
   121  
   122  func Getgoextlinkenabled() string {
   123  	return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
   124  }
   125  
   126  func init() {
   127  	for _, f := range strings.Split(goexperiment, ",") {
   128  		if f != "" {
   129  			addexp(f)
   130  		}
   131  	}
   132  
   133  	// regabi is only supported on amd64.
   134  	if GOARCH != "amd64" {
   135  		Regabi_enabled = 0
   136  	}
   137  }
   138  
   139  // Note: must agree with runtime.framepointer_enabled.
   140  var Framepointer_enabled = GOARCH == "amd64" || GOARCH == "arm64" && (GOOS == "linux" || GOOS == "darwin" || GOOS == "ios")
   141  
   142  func addexp(s string) {
   143  	// Could do general integer parsing here, but the runtime copy doesn't yet.
   144  	v := 1
   145  	name := s
   146  	if len(name) > 2 && name[:2] == "no" {
   147  		v = 0
   148  		name = name[2:]
   149  	}
   150  	for i := 0; i < len(exper); i++ {
   151  		if exper[i].name == name {
   152  			if exper[i].val != nil {
   153  				*exper[i].val = v
   154  			}
   155  			return
   156  		}
   157  	}
   158  
   159  	fmt.Printf("unknown experiment %s\n", s)
   160  	os.Exit(2)
   161  }
   162  
   163  var (
   164  	Fieldtrack_enabled        int
   165  	Preemptibleloops_enabled  int
   166  	Staticlockranking_enabled int
   167  	Regabi_enabled            int
   168  )
   169  
   170  // Toolchain experiments.
   171  // These are controlled by the GOEXPERIMENT environment
   172  // variable recorded when the toolchain is built.
   173  // This list is also known to cmd/gc.
   174  var exper = []struct {
   175  	name string
   176  	val  *int
   177  }{
   178  	{"fieldtrack", &Fieldtrack_enabled},
   179  	{"preemptibleloops", &Preemptibleloops_enabled},
   180  	{"staticlockranking", &Staticlockranking_enabled},
   181  	{"regabi", &Regabi_enabled},
   182  }
   183  
   184  var defaultExpstring = Expstring()
   185  
   186  func DefaultExpstring() string {
   187  	return defaultExpstring
   188  }
   189  
   190  func Expstring() string {
   191  	buf := "X"
   192  	for i := range exper {
   193  		if *exper[i].val != 0 {
   194  			buf += "," + exper[i].name
   195  		}
   196  	}
   197  	if buf == "X" {
   198  		buf += ",none"
   199  	}
   200  	return "X:" + buf[2:]
   201  }
   202  

View as plain text