1 //===- Config.h -------------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_ELF_CONFIG_H 10 #define LLD_ELF_CONFIG_H 11 12 #include "lld/Common/ErrorHandler.h" 13 #include "llvm/ADT/MapVector.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/ADT/StringSet.h" 16 #include "llvm/BinaryFormat/ELF.h" 17 #include "llvm/Support/CachePruning.h" 18 #include "llvm/Support/CodeGen.h" 19 #include "llvm/Support/Endian.h" 20 #include <atomic> 21 #include <vector> 22 23 namespace lld { 24 namespace elf { 25 26 class InputFile; 27 class InputSectionBase; 28 29 enum ELFKind { 30 ELFNoneKind, 31 ELF32LEKind, 32 ELF32BEKind, 33 ELF64LEKind, 34 ELF64BEKind 35 }; 36 37 // For --build-id. 38 enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid }; 39 40 // For --discard-{all,locals,none}. 41 enum class DiscardPolicy { Default, All, Locals, None }; 42 43 // For --icf={none,safe,all}. 44 enum class ICFLevel { None, Safe, All }; 45 46 // For --strip-{all,debug}. 47 enum class StripPolicy { None, All, Debug }; 48 49 // For --unresolved-symbols. 50 enum class UnresolvedPolicy { ReportError, Warn, Ignore }; 51 52 // For --orphan-handling. 53 enum class OrphanHandlingPolicy { Place, Warn, Error }; 54 55 // For --sort-section and linkerscript sorting rules. 56 enum class SortSectionPolicy { Default, None, Alignment, Name, Priority }; 57 58 // For --target2 59 enum class Target2Policy { Abs, Rel, GotRel }; 60 61 // For tracking ARM Float Argument PCS 62 enum class ARMVFPArgKind { Default, Base, VFP, ToolChain }; 63 64 // For -z noseparate-code, -z separate-code and -z separate-loadable-segments. 65 enum class SeparateSegmentKind { None, Code, Loadable }; 66 67 // For -z *stack 68 enum class GnuStackKind { None, Exec, NoExec }; 69 70 struct SymbolVersion { 71 llvm::StringRef name; 72 bool isExternCpp; 73 bool hasWildcard; 74 }; 75 76 // This struct contains symbols version definition that 77 // can be found in version script if it is used for link. 78 struct VersionDefinition { 79 llvm::StringRef name; 80 uint16_t id; 81 std::vector<SymbolVersion> patterns; 82 }; 83 84 // This struct contains the global configuration for the linker. 85 // Most fields are direct mapping from the command line options 86 // and such fields have the same name as the corresponding options. 87 // Most fields are initialized by the driver. 88 struct Configuration { 89 uint8_t osabi = 0; 90 uint32_t andFeatures = 0; 91 llvm::CachePruningPolicy thinLTOCachePolicy; 92 llvm::StringMap<uint64_t> sectionStartMap; 93 llvm::StringRef chroot; 94 llvm::StringRef dynamicLinker; 95 llvm::StringRef dwoDir; 96 llvm::StringRef entry; 97 llvm::StringRef emulation; 98 llvm::StringRef fini; 99 llvm::StringRef init; 100 llvm::StringRef ltoAAPipeline; 101 llvm::StringRef ltoCSProfileFile; 102 llvm::StringRef ltoNewPmPasses; 103 llvm::StringRef ltoObjPath; 104 llvm::StringRef ltoSampleProfile; 105 llvm::StringRef mapFile; 106 llvm::StringRef outputFile; 107 llvm::StringRef optRemarksFilename; 108 llvm::StringRef optRemarksPasses; 109 llvm::StringRef optRemarksFormat; 110 llvm::StringRef progName; 111 llvm::StringRef printSymbolOrder; 112 llvm::StringRef soName; 113 llvm::StringRef sysroot; 114 llvm::StringRef thinLTOCacheDir; 115 llvm::StringRef thinLTOIndexOnlyArg; 116 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace; 117 std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace; 118 std::string rpath; 119 std::vector<VersionDefinition> versionDefinitions; 120 std::vector<llvm::StringRef> auxiliaryList; 121 std::vector<llvm::StringRef> filterList; 122 std::vector<llvm::StringRef> searchPaths; 123 std::vector<llvm::StringRef> symbolOrderingFile; 124 std::vector<llvm::StringRef> undefined; 125 std::vector<SymbolVersion> dynamicList; 126 std::vector<uint8_t> buildIdVector; 127 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>, 128 uint64_t> 129 callGraphProfile; 130 bool allowMultipleDefinition; 131 bool allowShlibUndefined; 132 bool androidPackDynRelocs; 133 bool armHasBlx = false; 134 bool armHasMovtMovw = false; 135 bool armJ1J2BranchEncoding = false; 136 bool asNeeded = false; 137 bool bsymbolic; 138 bool bsymbolicFunctions; 139 bool callGraphProfileSort; 140 bool checkSections; 141 bool compressDebugSections; 142 bool cref; 143 bool defineCommon; 144 bool demangle = true; 145 bool dependentLibraries; 146 bool disableVerify; 147 bool ehFrameHdr; 148 bool emitLLVM; 149 bool emitRelocs; 150 bool enableNewDtags; 151 bool executeOnly; 152 bool exportDynamic; 153 bool fixCortexA53Errata843419; 154 bool fixCortexA8; 155 bool forceBTI; 156 bool formatBinary = false; 157 bool requireCET; 158 bool gcSections; 159 bool gdbIndex; 160 bool gnuHash = false; 161 bool gnuUnique; 162 bool hasDynamicList = false; 163 bool hasDynSymTab; 164 bool ignoreDataAddressEquality; 165 bool ignoreFunctionAddressEquality; 166 bool ltoCSProfileGenerate; 167 bool ltoDebugPassManager; 168 bool ltoNewPassManager; 169 bool mergeArmExidx; 170 bool mipsN32Abi = false; 171 bool mmapOutputFile; 172 bool nmagic; 173 bool noinhibitExec; 174 bool nostdlib; 175 bool oFormatBinary; 176 bool omagic; 177 bool optRemarksWithHotness; 178 bool pacPlt; 179 bool picThunk; 180 bool pie; 181 bool printGcSections; 182 bool printIcfSections; 183 bool relocatable; 184 bool relrPackDynRelocs; 185 bool saveTemps; 186 bool singleRoRx; 187 bool shared; 188 bool isStatic = false; 189 bool sysvHash = false; 190 bool target1Rel; 191 bool trace; 192 bool thinLTOEmitImportsFiles; 193 bool thinLTOIndexOnly; 194 bool tocOptimize; 195 bool undefinedVersion; 196 bool useAndroidRelrTags = false; 197 bool warnBackrefs; 198 bool warnCommon; 199 bool warnIfuncTextrel; 200 bool warnMissingEntry; 201 bool warnSymbolOrdering; 202 bool writeAddends; 203 bool zCombreloc; 204 bool zCopyreloc; 205 bool zExecstack; 206 bool zGlobal; 207 bool zHazardplt; 208 bool zIfuncNoplt; 209 bool zInitfirst; 210 bool zInterpose; 211 bool zKeepTextSectionPrefix; 212 bool zNodefaultlib; 213 bool zNodelete; 214 bool zNodlopen; 215 bool zNow; 216 bool zOrigin; 217 bool zRelro; 218 bool zRodynamic; 219 bool zText; 220 bool zRetpolineplt; 221 bool zWxneeded; 222 DiscardPolicy discard; 223 GnuStackKind zGnustack; 224 ICFLevel icf; 225 OrphanHandlingPolicy orphanHandling; 226 SortSectionPolicy sortSection; 227 StripPolicy strip; 228 UnresolvedPolicy unresolvedSymbols; 229 Target2Policy target2; 230 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default; 231 BuildIdKind buildId = BuildIdKind::None; 232 SeparateSegmentKind zSeparate; 233 ELFKind ekind = ELFNoneKind; 234 uint16_t emachine = llvm::ELF::EM_NONE; 235 llvm::Optional<uint64_t> imageBase; 236 uint64_t commonPageSize; 237 uint64_t maxPageSize; 238 uint64_t mipsGotSize; 239 uint64_t zStackSize; 240 unsigned ltoPartitions; 241 unsigned ltoo; 242 unsigned optimize; 243 unsigned thinLTOJobs; 244 int32_t splitStackAdjustSize; 245 246 // The following config options do not directly correspond to any 247 // particular command line options. 248 249 // True if we need to pass through relocations in input files to the 250 // output file. Usually false because we consume relocations. 251 bool copyRelocs; 252 253 // True if the target is ELF64. False if ELF32. 254 bool is64; 255 256 // True if the target is little-endian. False if big-endian. 257 bool isLE; 258 259 // endianness::little if isLE is true. endianness::big otherwise. 260 llvm::support::endianness endianness; 261 262 // True if the target is the little-endian MIPS64. 263 // 264 // The reason why we have this variable only for the MIPS is because 265 // we use this often. Some ELF headers for MIPS64EL are in a 266 // mixed-endian (which is horrible and I'd say that's a serious spec 267 // bug), and we need to know whether we are reading MIPS ELF files or 268 // not in various places. 269 // 270 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official 271 // name whatever that means. A fun hypothesis is that "EL" is short for 272 // little-endian written in the little-endian order, but I don't know 273 // if that's true.) 274 bool isMips64EL; 275 276 // True if we need to set the DF_STATIC_TLS flag to an output file, 277 // which works as a hint to the dynamic loader that the file contains 278 // code compiled with the static TLS model. The thread-local variable 279 // compiled with the static TLS model is faster but less flexible, and 280 // it may not be loaded using dlopen(). 281 // 282 // We set this flag to true when we see a relocation for the static TLS 283 // model. Once this becomes true, it will never become false. 284 // 285 // Since the flag is updated by multi-threaded code, we use std::atomic. 286 // (Writing to a variable is not considered thread-safe even if the 287 // variable is boolean and we always set the same value from all threads.) 288 std::atomic<bool> hasStaticTlsModel{false}; 289 290 // Holds set of ELF header flags for the target. 291 uint32_t eflags = 0; 292 293 // The ELF spec defines two types of relocation table entries, RELA and 294 // REL. RELA is a triplet of (offset, info, addend) while REL is a 295 // tuple of (offset, info). Addends for REL are implicit and read from 296 // the location where the relocations are applied. So, REL is more 297 // compact than RELA but requires a bit of more work to process. 298 // 299 // (From the linker writer's view, this distinction is not necessary. 300 // If the ELF had chosen whichever and sticked with it, it would have 301 // been easier to write code to process relocations, but it's too late 302 // to change the spec.) 303 // 304 // Each ABI defines its relocation type. IsRela is true if target 305 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A 306 // few 32-bit ABIs are using RELA too. 307 bool isRela; 308 309 // True if we are creating position-independent code. 310 bool isPic; 311 312 // 4 for ELF32, 8 for ELF64. 313 int wordsize; 314 }; 315 316 // The only instance of Configuration struct. 317 extern Configuration *config; 318 319 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and 320 // VER_NDX_GLOBAL. This helper returns other elements. 321 static inline ArrayRef<VersionDefinition> namedVersionDefs() { 322 return llvm::makeArrayRef(config->versionDefinitions).slice(2); 323 } 324 325 static inline void errorOrWarn(const Twine &msg) { 326 if (!config->noinhibitExec) 327 error(msg); 328 else 329 warn(msg); 330 } 331 } // namespace elf 332 } // namespace lld 333 334 #endif 335