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) IsPPC64() bool {
   116  	return t.Arch.Family == sys.PPC64
   117  }
   118  
   119  func (t *Target) IsRISCV64() bool {
   120  	return t.Arch.Family == sys.RISCV64
   121  }
   122  
   123  func (t *Target) IsS390X() bool {
   124  	return t.Arch.Family == sys.S390X
   125  }
   126  
   127  func (t *Target) IsWasm() bool {
   128  	return t.Arch.Family == sys.Wasm
   129  }
   130  
   131  //
   132  // OS Functions
   133  //
   134  
   135  func (t *Target) IsLinux() bool {
   136  	t.mustSetHeadType()
   137  	return t.HeadType == objabi.Hlinux
   138  }
   139  
   140  func (t *Target) IsDarwin() bool {
   141  	t.mustSetHeadType()
   142  	return t.HeadType == objabi.Hdarwin
   143  }
   144  
   145  func (t *Target) IsWindows() bool {
   146  	t.mustSetHeadType()
   147  	return t.HeadType == objabi.Hwindows
   148  }
   149  
   150  func (t *Target) IsPlan9() bool {
   151  	t.mustSetHeadType()
   152  	return t.HeadType == objabi.Hplan9
   153  }
   154  
   155  func (t *Target) IsAIX() bool {
   156  	t.mustSetHeadType()
   157  	return t.HeadType == objabi.Haix
   158  }
   159  
   160  func (t *Target) IsSolaris() bool {
   161  	t.mustSetHeadType()
   162  	return t.HeadType == objabi.Hsolaris
   163  }
   164  
   165  func (t *Target) IsNetbsd() bool {
   166  	t.mustSetHeadType()
   167  	return t.HeadType == objabi.Hnetbsd
   168  }
   169  
   170  func (t *Target) IsOpenbsd() bool {
   171  	t.mustSetHeadType()
   172  	return t.HeadType == objabi.Hopenbsd
   173  }
   174  
   175  func (t *Target) mustSetHeadType() {
   176  	if t.HeadType == objabi.Hunknown {
   177  		panic("HeadType is not set")
   178  	}
   179  }
   180  
   181  //
   182  // MISC
   183  //
   184  
   185  func (t *Target) IsBigEndian() bool {
   186  	return t.Arch.ByteOrder == binary.BigEndian
   187  }
   188  
   189  func (t *Target) UsesLibc() bool {
   190  	t.mustSetHeadType()
   191  	switch t.HeadType {
   192  	case objabi.Haix, objabi.Hdarwin, objabi.Hopenbsd, objabi.Hsolaris, objabi.Hwindows:
   193  		// platforms where we use libc for syscalls.
   194  		return true
   195  	}
   196  	return false
   197  }
   198  

View as plain text