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> thinLTOModulesToCompile; 129 std::vector<llvm::StringRef> undefined; 130 std::vector<SymbolVersion> dynamicList; 131 std::vector<uint8_t> buildIdVector; 132 llvm::MapVector<std::pair<const InputSectionBase *, const InputSectionBase *>, 133 uint64_t> 134 callGraphProfile; 135 bool allowMultipleDefinition; 136 bool allowShlibUndefined; 137 bool androidPackDynRelocs; 138 bool armHasBlx = false; 139 bool armHasMovtMovw = false; 140 bool armJ1J2BranchEncoding = false; 141 bool asNeeded = false; 142 bool bsymbolic; 143 bool bsymbolicFunctions; 144 bool callGraphProfileSort; 145 bool checkSections; 146 bool compressDebugSections; 147 bool cref; 148 std::vector<std::pair<llvm::GlobPattern, uint64_t>> deadRelocInNonAlloc; 149 bool defineCommon; 150 bool demangle = true; 151 bool dependentLibraries; 152 bool disableVerify; 153 bool ehFrameHdr; 154 bool emitLLVM; 155 bool emitRelocs; 156 bool enableNewDtags; 157 bool executeOnly; 158 bool exportDynamic; 159 bool fixCortexA53Errata843419; 160 bool fixCortexA8; 161 bool formatBinary = false; 162 bool gcSections; 163 bool gdbIndex; 164 bool gnuHash = false; 165 bool gnuUnique; 166 bool hasDynSymTab; 167 bool ignoreDataAddressEquality; 168 bool ignoreFunctionAddressEquality; 169 bool ltoCSProfileGenerate; 170 bool ltoDebugPassManager; 171 bool ltoEmitAsm; 172 bool ltoNewPassManager; 173 bool ltoUniqueBasicBlockSectionNames; 174 bool ltoWholeProgramVisibility; 175 bool mergeArmExidx; 176 bool mipsN32Abi = false; 177 bool mmapOutputFile; 178 bool nmagic; 179 bool noDynamicLinker = false; 180 bool noinhibitExec; 181 bool nostdlib; 182 bool oFormatBinary; 183 bool omagic; 184 bool optimizeBBJumps; 185 bool optRemarksWithHotness; 186 bool picThunk; 187 bool pie; 188 bool printGcSections; 189 bool printIcfSections; 190 bool relocatable; 191 bool relrPackDynRelocs; 192 bool saveTemps; 193 llvm::Optional<uint32_t> shuffleSectionSeed; 194 bool singleRoRx; 195 bool shared; 196 bool symbolic; 197 bool isStatic = false; 198 bool sysvHash = false; 199 bool target1Rel; 200 bool trace; 201 bool thinLTOEmitImportsFiles; 202 bool thinLTOIndexOnly; 203 bool timeTraceEnabled; 204 bool tocOptimize; 205 bool undefinedVersion; 206 bool unique; 207 bool useAndroidRelrTags = false; 208 bool warnBackrefs; 209 std::vector<llvm::GlobPattern> warnBackrefsExclude; 210 bool warnCommon; 211 bool warnIfuncTextrel; 212 bool warnMissingEntry; 213 bool warnSymbolOrdering; 214 bool writeAddends; 215 bool zCombreloc; 216 bool zCopyreloc; 217 bool zForceBti; 218 bool zForceIbt; 219 bool zGlobal; 220 bool zHazardplt; 221 bool zIfuncNoplt; 222 bool zInitfirst; 223 bool zInterpose; 224 bool zKeepTextSectionPrefix; 225 bool zNodefaultlib; 226 bool zNodelete; 227 bool zNodlopen; 228 bool zNow; 229 bool zOrigin; 230 bool zPacPlt; 231 bool zRelro; 232 bool zRodynamic; 233 bool zShstk; 234 uint8_t zStartStopVisibility; 235 bool zText; 236 bool zRetpolineplt; 237 bool zWxneeded; 238 DiscardPolicy discard; 239 GnuStackKind zGnustack; 240 ICFLevel icf; 241 OrphanHandlingPolicy orphanHandling; 242 SortSectionPolicy sortSection; 243 StripPolicy strip; 244 UnresolvedPolicy unresolvedSymbols; 245 Target2Policy target2; 246 ARMVFPArgKind armVFPArgs = ARMVFPArgKind::Default; 247 BuildIdKind buildId = BuildIdKind::None; 248 SeparateSegmentKind zSeparate; 249 ELFKind ekind = ELFNoneKind; 250 uint16_t emachine = llvm::ELF::EM_NONE; 251 llvm::Optional<uint64_t> imageBase; 252 uint64_t commonPageSize; 253 uint64_t maxPageSize; 254 uint64_t mipsGotSize; 255 uint64_t zStackSize; 256 unsigned ltoPartitions; 257 unsigned ltoo; 258 unsigned optimize; 259 StringRef thinLTOJobs; 260 unsigned timeTraceGranularity; 261 int32_t splitStackAdjustSize; 262 263 // The following config options do not directly correspond to any 264 // particular command line options. 265 266 // True if we need to pass through relocations in input files to the 267 // output file. Usually false because we consume relocations. 268 bool copyRelocs; 269 270 // True if the target is ELF64. False if ELF32. 271 bool is64; 272 273 // True if the target is little-endian. False if big-endian. 274 bool isLE; 275 276 // endianness::little if isLE is true. endianness::big otherwise. 277 llvm::support::endianness endianness; 278 279 // True if the target is the little-endian MIPS64. 280 // 281 // The reason why we have this variable only for the MIPS is because 282 // we use this often. Some ELF headers for MIPS64EL are in a 283 // mixed-endian (which is horrible and I'd say that's a serious spec 284 // bug), and we need to know whether we are reading MIPS ELF files or 285 // not in various places. 286 // 287 // (Note that MIPS64EL is not a typo for MIPS64LE. This is the official 288 // name whatever that means. A fun hypothesis is that "EL" is short for 289 // little-endian written in the little-endian order, but I don't know 290 // if that's true.) 291 bool isMips64EL; 292 293 // True if we need to set the DF_STATIC_TLS flag to an output file, 294 // which works as a hint to the dynamic loader that the file contains 295 // code compiled with the static TLS model. The thread-local variable 296 // compiled with the static TLS model is faster but less flexible, and 297 // it may not be loaded using dlopen(). 298 // 299 // We set this flag to true when we see a relocation for the static TLS 300 // model. Once this becomes true, it will never become false. 301 // 302 // Since the flag is updated by multi-threaded code, we use std::atomic. 303 // (Writing to a variable is not considered thread-safe even if the 304 // variable is boolean and we always set the same value from all threads.) 305 std::atomic<bool> hasStaticTlsModel{false}; 306 307 // Holds set of ELF header flags for the target. 308 uint32_t eflags = 0; 309 310 // The ELF spec defines two types of relocation table entries, RELA and 311 // REL. RELA is a triplet of (offset, info, addend) while REL is a 312 // tuple of (offset, info). Addends for REL are implicit and read from 313 // the location where the relocations are applied. So, REL is more 314 // compact than RELA but requires a bit of more work to process. 315 // 316 // (From the linker writer's view, this distinction is not necessary. 317 // If the ELF had chosen whichever and sticked with it, it would have 318 // been easier to write code to process relocations, but it's too late 319 // to change the spec.) 320 // 321 // Each ABI defines its relocation type. IsRela is true if target 322 // uses RELA. As far as we know, all 64-bit ABIs are using RELA. A 323 // few 32-bit ABIs are using RELA too. 324 bool isRela; 325 326 // True if we are creating position-independent code. 327 bool isPic; 328 329 // 4 for ELF32, 8 for ELF64. 330 int wordsize; 331 }; 332 333 // The only instance of Configuration struct. 334 extern Configuration *config; 335 336 // The first two elements of versionDefinitions represent VER_NDX_LOCAL and 337 // VER_NDX_GLOBAL. This helper returns other elements. 338 static inline ArrayRef<VersionDefinition> namedVersionDefs() { 339 return llvm::makeArrayRef(config->versionDefinitions).slice(2); 340 } 341 342 static inline void errorOrWarn(const Twine &msg) { 343 if (!config->noinhibitExec) 344 error(msg); 345 else 346 warn(msg); 347 } 348 } // namespace elf 349 } // namespace lld 350 351 #endif 352