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