1 //===- Symbols.cpp --------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Symbols.h" 11 #include "Error.h" 12 #include "InputFiles.h" 13 #include "InputSection.h" 14 #include "OutputSections.h" 15 #include "Target.h" 16 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/Config/config.h" 19 20 #ifdef HAVE_CXXABI_H 21 #include <cxxabi.h> 22 #endif 23 24 using namespace llvm; 25 using namespace llvm::object; 26 using namespace llvm::ELF; 27 28 using namespace lld; 29 using namespace lld::elf; 30 31 template <class ELFT> 32 static typename ELFT::uint getSymVA(const SymbolBody &Body, 33 typename ELFT::uint &Addend) { 34 typedef typename ELFT::uint uintX_t; 35 36 switch (Body.kind()) { 37 case SymbolBody::DefinedSyntheticKind: { 38 auto &D = cast<DefinedSynthetic<ELFT>>(Body); 39 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd) 40 return D.Section.getVA() + D.Section.getSize(); 41 return D.Section.getVA() + D.Value; 42 } 43 case SymbolBody::DefinedRegularKind: { 44 auto &D = cast<DefinedRegular<ELFT>>(Body); 45 InputSectionBase<ELFT> *SC = D.Section; 46 47 // According to the ELF spec reference to a local symbol from outside 48 // the group are not allowed. Unfortunately .eh_frame breaks that rule 49 // and must be treated specially. For now we just replace the symbol with 50 // 0. 51 if (SC == &InputSection<ELFT>::Discarded) 52 return 0; 53 54 // This is an absolute symbol. 55 if (!SC) 56 return D.Value; 57 58 uintX_t Offset = D.Value; 59 if (D.isSection()) { 60 Offset += Addend; 61 Addend = 0; 62 } 63 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset); 64 if (D.isTls()) 65 return VA - Out<ELFT>::TlsPhdr->p_vaddr; 66 return VA; 67 } 68 case SymbolBody::DefinedCommonKind: 69 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss; 70 case SymbolBody::SharedKind: { 71 auto &SS = cast<SharedSymbol<ELFT>>(Body); 72 if (!SS.NeedsCopyOrPltAddr) 73 return 0; 74 if (SS.isFunc()) 75 return Body.getPltVA<ELFT>(); 76 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss; 77 } 78 case SymbolBody::UndefinedKind: 79 return 0; 80 case SymbolBody::LazyArchiveKind: 81 case SymbolBody::LazyObjectKind: 82 assert(Body.Backref->IsUsedInRegularObj && "lazy symbol reached writer"); 83 return 0; 84 case SymbolBody::DefinedBitcodeKind: 85 llvm_unreachable("should have been replaced"); 86 } 87 llvm_unreachable("invalid symbol kind"); 88 } 89 90 SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, 91 uint8_t Type) 92 : SymbolKind(K), Type(Type), Binding(STB_LOCAL), StOther(StOther), 93 NameOffset(NameOffset) { 94 init(); 95 } 96 97 SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, 98 uint8_t Type) 99 : SymbolKind(K), Type(Type), Binding(Binding), StOther(StOther), 100 Name({Name.data(), Name.size()}) { 101 assert(!isLocal()); 102 init(); 103 } 104 105 void SymbolBody::init() { 106 NeedsCopyOrPltAddr = false; 107 CanOmitFromDynSym = false; 108 } 109 110 // Returns true if a symbol can be replaced at load-time by a symbol 111 // with the same name defined in other ELF executable or DSO. 112 bool SymbolBody::isPreemptible() const { 113 if (isLocal()) 114 return false; 115 116 // Shared symbols resolve to the definition in the DSO. 117 if (isShared()) 118 return true; 119 120 // That's all that can be preempted in a non-DSO. 121 if (!Config->Shared) 122 return false; 123 124 // Only symbols that appear in dynsym can be preempted. 125 if (!Backref->includeInDynsym()) 126 return false; 127 128 // Normally only default visibility symbols can be preempted, but -Bsymbolic 129 // means that not even they can be preempted. 130 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) 131 return !isDefined(); 132 return Backref->Visibility == STV_DEFAULT; 133 } 134 135 template <class ELFT> 136 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { 137 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend); 138 return OutVA + Addend; 139 } 140 141 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const { 142 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>(); 143 } 144 145 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const { 146 return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) * 147 sizeof(typename ELFT::uint); 148 } 149 150 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { 151 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>(); 152 } 153 154 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const { 155 return GotPltIndex * sizeof(typename ELFT::uint); 156 } 157 158 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const { 159 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize + 160 PltIndex * Target->PltEntrySize; 161 } 162 163 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { 164 auto *D = cast<DefinedRegular<ELFT>>(this); 165 auto *S = cast<InputSection<ELFT>>(D->Section); 166 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() + 167 ThunkIndex * Target->ThunkSize; 168 } 169 170 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { 171 if (const auto *C = dyn_cast<DefinedCommon>(this)) 172 return C->Size; 173 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) 174 return DR->Size; 175 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) 176 return S->Sym.st_size; 177 return 0; 178 } 179 180 // Returns 1, 0 or -1 if this symbol should take precedence 181 // over the Other, tie or lose, respectively. 182 int SymbolBody::compare(SymbolBody *Other) { 183 assert(!isLazy() && !Other->isLazy()); 184 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); 185 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), 186 !Other->isWeak()); 187 188 // Compare the two by symbol type. 189 if (L > R) 190 return -Other->compare(this); 191 if (L != R) 192 return -1; 193 if (!isDefined() || isShared() || isWeak()) 194 return 1; 195 196 // If both are equal in terms of symbol type, then at least 197 // one of them must be a common symbol. Otherwise, they conflict. 198 auto *A = dyn_cast<DefinedCommon>(this); 199 auto *B = dyn_cast<DefinedCommon>(Other); 200 if (!A && !B) 201 return 0; 202 203 // If both are common, the larger one is chosen. 204 if (A && B) { 205 if (Config->WarnCommon) 206 warning("multiple common of " + A->getName()); 207 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment); 208 return A->Size < B->Size ? -1 : 1; 209 } 210 211 // Non-common symbols takes precedence over common symbols. 212 if (Config->WarnCommon) 213 warning("common " + this->getName() + " is overridden"); 214 return A ? -1 : 1; 215 } 216 217 Defined::Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, 218 uint8_t Type) 219 : SymbolBody(K, Name, Binding, StOther, Type) {} 220 221 Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type) 222 : SymbolBody(K, NameOffset, StOther, Type) {} 223 224 DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther) 225 : Defined(DefinedBitcodeKind, Name, IsWeak ? STB_WEAK : STB_GLOBAL, 226 StOther, 0 /* Type */) {} 227 228 bool DefinedBitcode::classof(const SymbolBody *S) { 229 return S->kind() == DefinedBitcodeKind; 230 } 231 232 Undefined::Undefined(StringRef Name, uint8_t Binding, uint8_t StOther, 233 uint8_t Type, bool IsBitcode) 234 : SymbolBody(SymbolBody::UndefinedKind, Name, Binding, StOther, Type) { 235 this->IsUndefinedBitcode = IsBitcode; 236 } 237 238 Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type) 239 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) { 240 this->IsUndefinedBitcode = false; 241 } 242 243 template <typename ELFT> 244 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, 245 OutputSectionBase<ELFT> &Section) 246 : Defined(SymbolBody::DefinedSyntheticKind, N, STB_GLOBAL, STV_HIDDEN, 247 0 /* Type */), 248 Value(Value), Section(Section) {} 249 250 DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, 251 uint8_t Binding, uint8_t StOther, uint8_t Type) 252 : Defined(SymbolBody::DefinedCommonKind, N, Binding, StOther, Type), 253 Alignment(Alignment), Size(Size) {} 254 255 std::unique_ptr<InputFile> Lazy::getFile() { 256 if (auto *S = dyn_cast<LazyArchive>(this)) 257 return S->getFile(); 258 return cast<LazyObject>(this)->getFile(); 259 } 260 261 std::unique_ptr<InputFile> LazyArchive::getFile() { 262 MemoryBufferRef MBRef = File->getMember(&Sym); 263 264 // getMember returns an empty buffer if the member was already 265 // read from the library. 266 if (MBRef.getBuffer().empty()) 267 return std::unique_ptr<InputFile>(nullptr); 268 return createObjectFile(MBRef, File->getName()); 269 } 270 271 std::unique_ptr<InputFile> LazyObject::getFile() { 272 return createObjectFile(MBRef); 273 } 274 275 // Returns the demangled C++ symbol name for Name. 276 std::string elf::demangle(StringRef Name) { 277 #if !defined(HAVE_CXXABI_H) 278 return Name; 279 #else 280 if (!Config->Demangle) 281 return Name; 282 283 // __cxa_demangle can be used to demangle strings other than symbol 284 // names which do not necessarily start with "_Z". Name can be 285 // either a C or C++ symbol. Don't call __cxa_demangle if the name 286 // does not look like a C++ symbol name to avoid getting unexpected 287 // result for a C symbol that happens to match a mangled type name. 288 if (!Name.startswith("_Z")) 289 return Name; 290 291 char *Buf = 292 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); 293 if (!Buf) 294 return Name; 295 std::string S(Buf); 296 free(Buf); 297 return S; 298 #endif 299 } 300 301 bool Symbol::includeInDynsym() const { 302 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) 303 return false; 304 return (ExportDynamic && VersionScriptGlobal) || Body->isShared() || 305 (Body->isUndefined() && Config->Shared); 306 } 307 308 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; 309 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; 310 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; 311 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; 312 313 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; 314 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; 315 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; 316 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; 317 318 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const; 319 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const; 320 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const; 321 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const; 322 323 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; 324 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; 325 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; 326 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; 327 328 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const; 329 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const; 330 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const; 331 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const; 332 333 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; 334 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; 335 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; 336 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; 337 338 template uint32_t SymbolBody::template getSize<ELF32LE>() const; 339 template uint32_t SymbolBody::template getSize<ELF32BE>() const; 340 template uint64_t SymbolBody::template getSize<ELF64LE>() const; 341 template uint64_t SymbolBody::template getSize<ELF64BE>() const; 342 343 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; 344 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; 345 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; 346 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const; 347 348 template class elf::DefinedSynthetic<ELF32LE>; 349 template class elf::DefinedSynthetic<ELF32BE>; 350 template class elf::DefinedSynthetic<ELF64LE>; 351 template class elf::DefinedSynthetic<ELF64BE>; 352