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