Source file src/cmd/link/internal/ld/target.go

     1  // Copyright 2020 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 ld
     6  
     7  import (
     8  	"cmd/internal/objabi"
     9  	"cmd/internal/sys"
    10  	"encoding/binary"
    11  )
    12  
    13  // Target holds the configuration we're building for.
    14  type Target struct {
    15  	Arch *sys.Arch
    16  
    17  	HeadType objabi.HeadType
    18  
    19  	LinkMode  LinkMode
    20  	BuildMode BuildMode
    21  
    22  	linkShared    bool
    23  	canUsePlugins bool
    24  	IsELF         bool
    25  }
    26  
    27  //
    28  // Target type functions
    29  //
    30  
    31  func (t *Target) IsExe() bool {
    32  	return t.BuildMode == BuildModeExe
    33  }
    34  
    35  func (t *Target) IsShared() bool {
    36  	return t.BuildMode == BuildModeShared
    37  }
    38  
    39  func (t *Target) IsPlugin() bool {
    40  	return t.BuildMode == BuildModePlugin
    41  }
    42  
    43  func (t *Target) IsInternal() bool {
    44  	return t.LinkMode == LinkInternal
    45  }
    46  
    47  func (t *Target) IsExternal() bool {
    48  	return t.LinkMode == LinkExternal
    49  }
    50  
    51  func (t *Target) IsPIE() bool {
    52  	return t.BuildMode == BuildModePIE
    53  }
    54  
    55  func (t *Target) IsSharedGoLink() bool {
    56  	return t.linkShared
    57  }
    58  
    59  func (t *Target) CanUsePlugins() bool {
    60  	return t.canUsePlugins
    61  }
    62  
    63  func (t *Target) IsElf() bool {
    64  	t.mustSetHeadType()
    65  	return t.IsELF
    66  }
    67  
    68  func (t *Target) IsDynlinkingGo() bool {
    69  	return t.IsShared() || t.IsSharedGoLink() || t.IsPlugin() || t.CanUsePlugins()
    70  }
    71  
    72  // UseRelro reports whether to make use of "read only relocations" aka
    73  // relro.
    74  func (t *Target) UseRelro() bool {
    75  	switch t.BuildMode {
    76  	case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePIE, BuildModePlugin:
    77  		return t.IsELF || t.HeadType == objabi.Haix || t.HeadType == objabi.Hdarwin
    78  	default:
    79  		if t.HeadType == objabi.Hdarwin && t.IsARM64() {
    80  			// On darwin/ARM64, everything is PIE.
    81  			return true
    82  		}
    83  		return t.linkShared || (t.HeadType == objabi.Haix && t.LinkMode == LinkExternal)
    84  	}
    85  }
    86  
    87  //
    88  // Processor functions
    89  //
    90  
    91  func (t *Target) Is386() bool {
    92  	return t.Arch.Family == sys.I386
    93  }
    94  
    95  func (t *Target) IsARM() bool {
    96  	return t.Arch.Family == sys.ARM
    97  }
    98  
    99  func (t *Target) IsARM64() bool {
   100  	return t.Arch.Family == sys.ARM64
   101  }
   102  
   103  func (t *Target) IsAMD64() bool {
   104  	return t.Arch.Family == sys.AMD64
   105  }
   106  
   107  func (t *Target) IsMIPS() bool {
   108  	return t.Arch.Family == sys.MIPS
   109  }
   110  
   111  func (t *Target) IsMIPS64() bool {
   112  	return t.Arch.Family == sys.MIPS64
   113  }
   114  
   115  func (t *Target) IsLOONG64() bool {
   116  	return t.Arch.Family == sys.Loong64
   117  }
   118  
   119  func (t *Target) IsPPC64() bool {
   120  	return t.Arch.Family == sys.PPC64
   121  }
   122  
   123  func (t *Target) IsRISCV64() bool {
   124  	return t.Arch.Family == sys.RISCV64
   125  }
   126  
   127  func (t *Target) IsS390X() bool {
   128  	return t.Arch.Family == sys.S390X
   129  }
   130  
   131  func (t *Target) IsWasm() bool {
   132  	return t.Arch.Family == sys.Wasm
   133  }
   134  
   135  //
   136  // OS Functions
   137  //
   138  
   139  func (t *Target) IsLinux() bool {
   140  	t.mustSetHeadType()
   141  	return t.HeadType == objabi.Hlinux
   142  }
   143  
   144  func (t *Target) IsDarwin() bool {
   145  	t.mustSetHeadType()
   146  	return t.HeadType == objabi.Hdarwin
   147  }
   148  
   149  func (t *Target) IsWindows() bool {
   150  	t.mustSetHeadType()
   151  	return t.HeadType == objabi.Hwindows
   152  }
   153  
   154  func (t *Target) IsPlan9() bool {
   155  	t.mustSetHeadType()
   156  	return t.HeadType == objabi.Hplan9
   157  }
   158  
   159  func (t *Target) IsAIX() bool {
   160  	t.mustSetHeadType()
   161  	return t.HeadType == objabi.Haix
   162  }
   163  
   164  func (t *Target) IsSolaris() bool {
   165  	t.mustSetHeadType()
   166  	return t.HeadType == objabi.Hsolaris
   167  }
   168  
   169  func (t *Target) IsNetbsd() bool {
   170  	t.mustSetHeadType()
   171  	return t.HeadType == objabi.Hnetbsd
   172  }
   173  
   174  func (t *Target) IsOpenbsd() bool {
   175  	t.mustSetHeadType()
   176  	return t.HeadType == objabi.Hopenbsd
   177  }
   178  
   179  func (t *Target) IsFreebsd() bool {
   180  	t.mustSetHeadType()
   181  	return t.HeadType == objabi.Hfreebsd
   182  }
   183  
   184  func (t *Target) mustSetHeadType() {
   185  	if t.HeadType == objabi.Hunknown {
   186  		panic("HeadType is not set")
   187  	}
   188  }
   189  
   190  //
   191  // MISC
   192  //
   193  
   194  func (t *Target) IsBigEndian() bool {
   195  	return t.Arch.ByteOrder == binary.BigEndian
   196  }
   197  
   198  func (t *Target) UsesLibc() bool {
   199  	t.mustSetHeadType()
   200  	switch t.HeadType {
   201  	case objabi.Haix, objabi.Hdarwin, objabi.Hopenbsd, objabi.Hsolaris, objabi.Hwindows:
   202  		// platforms where we use libc for syscalls.
   203  		return true
   204  	}
   205  	return false
   206  }
   207  

View as plain text