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::UndefinedElfKind: 79 case SymbolBody::UndefinedBitcodeKind: 80 return 0; 81 case SymbolBody::LazyArchiveKind: 82 case SymbolBody::LazyObjectKind: 83 assert(Body.isUsedInRegularObj() && "lazy symbol reached writer"); 84 return 0; 85 case SymbolBody::DefinedBitcodeKind: 86 llvm_unreachable("should have been replaced"); 87 } 88 llvm_unreachable("invalid symbol kind"); 89 } 90 91 SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, 92 uint8_t Type) 93 : SymbolKind(K), Type(Type), Binding(STB_LOCAL), StOther(StOther), 94 NameOffset(NameOffset) { 95 init(); 96 } 97 98 SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, 99 uint8_t Type) 100 : SymbolKind(K), Type(Type), Binding(Binding), StOther(StOther), 101 Name({Name.data(), Name.size()}) { 102 assert(!isLocal()); 103 init(); 104 } 105 106 void SymbolBody::init() { 107 Kind K = kind(); 108 IsUsedInRegularObj = K == DefinedRegularKind || K == DefinedCommonKind || 109 K == DefinedSyntheticKind || K == UndefinedElfKind; 110 CanKeepUndefined = false; 111 MustBeInDynSym = false; 112 NeedsCopyOrPltAddr = false; 113 } 114 115 // Returns true if a symbol can be replaced at load-time by a symbol 116 // with the same name defined in other ELF executable or DSO. 117 bool SymbolBody::isPreemptible() const { 118 if (isLocal()) 119 return false; 120 121 if (isShared()) 122 return true; 123 124 if (getVisibility() != STV_DEFAULT) 125 return false; 126 127 if (isUndefined()) { 128 if (!isWeak()) 129 return true; 130 131 // Ideally the static linker should see a definition for every symbol, but 132 // shared object are normally allowed to have undefined references that the 133 // static linker never sees a definition for. 134 if (Config->Shared) 135 return true; 136 137 // Otherwise, just resolve to 0. 138 return false; 139 } 140 141 if (!Config->Shared) 142 return false; 143 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) 144 return false; 145 return true; 146 } 147 148 template <class ELFT> 149 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { 150 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend); 151 return OutVA + Addend; 152 } 153 154 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const { 155 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>(); 156 } 157 158 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const { 159 return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) * 160 sizeof(typename ELFT::uint); 161 } 162 163 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { 164 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>(); 165 } 166 167 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const { 168 return GotPltIndex * sizeof(typename ELFT::uint); 169 } 170 171 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const { 172 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize + 173 PltIndex * Target->PltEntrySize; 174 } 175 176 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { 177 auto *D = cast<DefinedRegular<ELFT>>(this); 178 auto *S = cast<InputSection<ELFT>>(D->Section); 179 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() + 180 ThunkIndex * Target->ThunkSize; 181 } 182 183 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { 184 if (const auto *C = dyn_cast<DefinedCommon>(this)) 185 return C->Size; 186 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) 187 return DR->Size; 188 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) 189 return S->Sym.st_size; 190 if (const auto *U = dyn_cast<UndefinedElf<ELFT>>(this)) 191 return U->Size; 192 return 0; 193 } 194 195 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { 196 if (VA == STV_DEFAULT) 197 return VB; 198 if (VB == STV_DEFAULT) 199 return VA; 200 return std::min(VA, VB); 201 } 202 203 static int compareCommons(DefinedCommon *A, DefinedCommon *B) { 204 if (Config->WarnCommon) 205 warning("multiple common of " + A->getName()); 206 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment); 207 return A->Size < B->Size ? -1 : 1; 208 } 209 210 // Returns 1, 0 or -1 if this symbol should take precedence 211 // over the Other, tie or lose, respectively. 212 int SymbolBody::compare(SymbolBody *Other) { 213 assert(!isLazy() && !Other->isLazy()); 214 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); 215 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), 216 !Other->isWeak()); 217 218 // Normalize 219 if (L > R) 220 return -Other->compare(this); 221 222 uint8_t V = getMinVisibility(getVisibility(), Other->getVisibility()); 223 if (isShared() != Other->isShared()) { 224 SymbolBody *Shared = isShared() ? this : Other; 225 Shared->MustBeInDynSym = true; 226 if (Shared->getVisibility() == STV_DEFAULT && 227 (V == STV_DEFAULT || V == STV_PROTECTED)) { 228 // We want to export all symbols that exist in the executable and are 229 // preemptable in DSOs, so that the symbols in the executable can 230 // preempt symbols in the DSO at runtime. 231 SymbolBody *NonShared = isShared() ? Other : this; 232 NonShared->MustBeInDynSym = true; 233 } 234 } 235 236 if (!isShared() && !Other->isShared()) { 237 setVisibility(V); 238 Other->setVisibility(V); 239 } 240 241 if (IsUsedInRegularObj || Other->IsUsedInRegularObj) 242 IsUsedInRegularObj = Other->IsUsedInRegularObj = true; 243 244 if (L != R) 245 return -1; 246 if (!isDefined() || isShared() || isWeak()) 247 return 1; 248 if (!isCommon() && !Other->isCommon()) 249 return 0; 250 if (isCommon() && Other->isCommon()) 251 return compareCommons(cast<DefinedCommon>(this), 252 cast<DefinedCommon>(Other)); 253 if (Config->WarnCommon) 254 warning("common " + this->getName() + " is overridden"); 255 return isCommon() ? -1 : 1; 256 } 257 258 Defined::Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, 259 uint8_t Type) 260 : SymbolBody(K, Name, Binding, StOther, Type) {} 261 262 Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type) 263 : SymbolBody(K, NameOffset, StOther, Type) {} 264 265 DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther) 266 : Defined(DefinedBitcodeKind, Name, IsWeak ? STB_WEAK : STB_GLOBAL, 267 StOther, 0 /* Type */) {} 268 269 bool DefinedBitcode::classof(const SymbolBody *S) { 270 return S->kind() == DefinedBitcodeKind; 271 } 272 273 UndefinedBitcode::UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther) 274 : SymbolBody(SymbolBody::UndefinedBitcodeKind, N, 275 IsWeak ? STB_WEAK : STB_GLOBAL, StOther, 0 /* Type */) {} 276 277 template <typename ELFT> 278 UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym) 279 : SymbolBody(SymbolBody::UndefinedElfKind, N, Sym.getBinding(), 280 Sym.st_other, Sym.getType()), 281 Size(Sym.st_size) {} 282 283 template <typename ELFT> 284 UndefinedElf<ELFT>::UndefinedElf(StringRef Name, uint8_t Binding, 285 uint8_t StOther, uint8_t Type, 286 bool CanKeepUndefined) 287 : SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) { 288 this->CanKeepUndefined = CanKeepUndefined; 289 } 290 291 template <typename ELFT> 292 UndefinedElf<ELFT>::UndefinedElf(const Elf_Sym &Sym) 293 : SymbolBody(SymbolBody::UndefinedElfKind, Sym.st_name, Sym.st_other, 294 Sym.getType()), 295 Size(Sym.st_size) { 296 assert(Sym.getBinding() == STB_LOCAL); 297 } 298 299 template <typename ELFT> 300 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, 301 OutputSectionBase<ELFT> &Section) 302 : Defined(SymbolBody::DefinedSyntheticKind, N, STB_GLOBAL, STV_HIDDEN, 303 0 /* Type */), 304 Value(Value), Section(Section) {} 305 306 DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, 307 uint8_t Binding, uint8_t StOther, uint8_t Type) 308 : Defined(SymbolBody::DefinedCommonKind, N, Binding, StOther, Type), 309 Alignment(Alignment), Size(Size) {} 310 311 std::unique_ptr<InputFile> Lazy::getFile() { 312 if (auto *S = dyn_cast<LazyArchive>(this)) 313 return S->getFile(); 314 return cast<LazyObject>(this)->getFile(); 315 } 316 317 std::unique_ptr<InputFile> LazyArchive::getFile() { 318 MemoryBufferRef MBRef = File->getMember(&Sym); 319 320 // getMember returns an empty buffer if the member was already 321 // read from the library. 322 if (MBRef.getBuffer().empty()) 323 return std::unique_ptr<InputFile>(nullptr); 324 return createObjectFile(MBRef, File->getName()); 325 } 326 327 std::unique_ptr<InputFile> LazyObject::getFile() { 328 return createObjectFile(MBRef); 329 } 330 331 // Returns the demangled C++ symbol name for Name. 332 std::string elf::demangle(StringRef Name) { 333 #if !defined(HAVE_CXXABI_H) 334 return Name; 335 #else 336 if (!Config->Demangle) 337 return Name; 338 339 // __cxa_demangle can be used to demangle strings other than symbol 340 // names which do not necessarily start with "_Z". Name can be 341 // either a C or C++ symbol. Don't call __cxa_demangle if the name 342 // does not look like a C++ symbol name to avoid getting unexpected 343 // result for a C symbol that happens to match a mangled type name. 344 if (!Name.startswith("_Z")) 345 return Name; 346 347 char *Buf = 348 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); 349 if (!Buf) 350 return Name; 351 std::string S(Buf); 352 free(Buf); 353 return S; 354 #endif 355 } 356 357 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; 358 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; 359 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; 360 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; 361 362 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; 363 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; 364 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; 365 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; 366 367 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const; 368 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const; 369 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const; 370 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const; 371 372 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; 373 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; 374 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; 375 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; 376 377 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const; 378 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const; 379 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const; 380 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const; 381 382 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; 383 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; 384 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; 385 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; 386 387 template uint32_t SymbolBody::template getSize<ELF32LE>() const; 388 template uint32_t SymbolBody::template getSize<ELF32BE>() const; 389 template uint64_t SymbolBody::template getSize<ELF64LE>() const; 390 template uint64_t SymbolBody::template getSize<ELF64BE>() const; 391 392 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; 393 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; 394 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; 395 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const; 396 397 template class elf::UndefinedElf<ELF32LE>; 398 template class elf::UndefinedElf<ELF32BE>; 399 template class elf::UndefinedElf<ELF64LE>; 400 template class elf::UndefinedElf<ELF64BE>; 401 402 template class elf::DefinedSynthetic<ELF32LE>; 403 template class elf::DefinedSynthetic<ELF32BE>; 404 template class elf::DefinedSynthetic<ELF64LE>; 405 template class elf::DefinedSynthetic<ELF64BE>; 406