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