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 "Strings.h" 16 #include "SyntheticSections.h" 17 #include "Target.h" 18 #include "Writer.h" 19 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/Support/Path.h" 22 #include <cstring> 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 const OutputSectionBase *Sec = D.Section; 40 if (!Sec) 41 return D.Value; 42 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd) 43 return Sec->Addr + Sec->Size; 44 return Sec->Addr + D.Value; 45 } 46 case SymbolBody::DefinedRegularKind: { 47 auto &D = cast<DefinedRegular<ELFT>>(Body); 48 InputSectionBase<ELFT> *IS = D.Section; 49 50 // According to the ELF spec reference to a local symbol from outside 51 // the group are not allowed. Unfortunately .eh_frame breaks that rule 52 // and must be treated specially. For now we just replace the symbol with 53 // 0. 54 if (IS == &InputSection<ELFT>::Discarded) 55 return 0; 56 57 // This is an absolute symbol. 58 if (!IS) 59 return D.Value; 60 61 uintX_t Offset = D.Value; 62 if (D.isSection()) { 63 Offset += Addend; 64 Addend = 0; 65 } 66 uintX_t VA = (IS->OutSec ? IS->OutSec->Addr : 0) + IS->getOffset(Offset); 67 if (D.isTls() && !Config->Relocatable) { 68 if (!Out<ELFT>::TlsPhdr) 69 fatal(toString(D.File) + 70 " has a STT_TLS symbol but doesn't have a PT_TLS section"); 71 return VA - Out<ELFT>::TlsPhdr->p_vaddr; 72 } 73 return VA; 74 } 75 case SymbolBody::DefinedCommonKind: 76 return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff + 77 cast<DefinedCommon>(Body).Offset; 78 case SymbolBody::SharedKind: { 79 auto &SS = cast<SharedSymbol<ELFT>>(Body); 80 if (!SS.NeedsCopyOrPltAddr) 81 return 0; 82 if (SS.isFunc()) 83 return Body.getPltVA<ELFT>(); 84 return Out<ELFT>::Bss->Addr + SS.OffsetInBss; 85 } 86 case SymbolBody::UndefinedKind: 87 return 0; 88 case SymbolBody::LazyArchiveKind: 89 case SymbolBody::LazyObjectKind: 90 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer"); 91 return 0; 92 } 93 llvm_unreachable("invalid symbol kind"); 94 } 95 96 SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, 97 uint8_t Type) 98 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(IsLocal), 99 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false), 100 IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {} 101 102 // Returns true if a symbol can be replaced at load-time by a symbol 103 // with the same name defined in other ELF executable or DSO. 104 bool SymbolBody::isPreemptible() const { 105 if (isLocal()) 106 return false; 107 108 // Shared symbols resolve to the definition in the DSO. The exceptions are 109 // symbols with copy relocations (which resolve to .bss) or preempt plt 110 // entries (which resolve to that plt entry). 111 if (isShared()) 112 return !NeedsCopyOrPltAddr; 113 114 // That's all that can be preempted in a non-DSO. 115 if (!Config->Shared) 116 return false; 117 118 // Only symbols that appear in dynsym can be preempted. 119 if (!symbol()->includeInDynsym()) 120 return false; 121 122 // Only default visibility symbols can be preempted. 123 if (symbol()->Visibility != STV_DEFAULT) 124 return false; 125 126 // -Bsymbolic means that definitions are not preempted. 127 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) 128 return !isDefined(); 129 return true; 130 } 131 132 template <class ELFT> bool SymbolBody::hasThunk() const { 133 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) 134 return DR->ThunkData != nullptr; 135 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) 136 return S->ThunkData != nullptr; 137 return false; 138 } 139 140 template <class ELFT> 141 typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { 142 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend); 143 return OutVA + Addend; 144 } 145 146 template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const { 147 return In<ELFT>::Got->getVA() + getGotOffset<ELFT>(); 148 } 149 150 template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const { 151 return GotIndex * Target->GotEntrySize; 152 } 153 154 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { 155 if (this->IsInIgot) 156 return In<ELFT>::IgotPlt->getVA() + getGotPltOffset<ELFT>(); 157 return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>(); 158 } 159 160 template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const { 161 return GotPltIndex * Target->GotPltEntrySize; 162 } 163 164 template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const { 165 if (this->IsInIplt) 166 return In<ELFT>::Iplt->getVA() + PltIndex * Target->PltEntrySize; 167 return In<ELFT>::Plt->getVA() + Target->PltHeaderSize + 168 PltIndex * Target->PltEntrySize; 169 } 170 171 template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { 172 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) 173 return DR->ThunkData->getVA(); 174 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) 175 return S->ThunkData->getVA(); 176 fatal("getThunkVA() not supported for Symbol class\n"); 177 } 178 179 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { 180 if (const auto *C = dyn_cast<DefinedCommon>(this)) 181 return C->Size; 182 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) 183 return DR->Size; 184 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) 185 return S->Sym.st_size; 186 return 0; 187 } 188 189 // If a symbol name contains '@', the characters after that is 190 // a symbol version name. This function parses that. 191 void SymbolBody::parseSymbolVersion() { 192 StringRef S = getName(); 193 size_t Pos = S.find('@'); 194 if (Pos == 0 || Pos == StringRef::npos) 195 return; 196 StringRef Verstr = S.substr(Pos + 1); 197 if (Verstr.empty()) 198 return; 199 200 // Truncate the symbol name so that it doesn't include the version string. 201 Name = {S.data(), Pos}; 202 203 // '@@' in a symbol name means the default version. 204 // It is usually the most recent one. 205 bool IsDefault = (Verstr[0] == '@'); 206 if (IsDefault) 207 Verstr = Verstr.substr(1); 208 209 for (VersionDefinition &Ver : Config->VersionDefinitions) { 210 if (Ver.Name != Verstr) 211 continue; 212 213 if (IsDefault) 214 symbol()->VersionId = Ver.Id; 215 else 216 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN; 217 return; 218 } 219 220 // It is an error if the specified version is not defined. 221 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr); 222 } 223 224 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, 225 uint8_t Type) 226 : SymbolBody(K, Name, IsLocal, StOther, Type) {} 227 228 template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const { 229 if (!Section || !isFunc()) 230 return false; 231 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC || 232 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC); 233 } 234 235 Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, 236 uint8_t Type, InputFile *File) 237 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) { 238 this->File = File; 239 } 240 241 template <typename ELFT> 242 DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef Name, uintX_t Value, 243 const OutputSectionBase *Section) 244 : Defined(SymbolBody::DefinedSyntheticKind, Name, /*IsLocal=*/false, 245 STV_HIDDEN, 0 /* Type */), 246 Value(Value), Section(Section) {} 247 248 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment, 249 uint8_t StOther, uint8_t Type, InputFile *File) 250 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther, 251 Type), 252 Alignment(Alignment), Size(Size) { 253 this->File = File; 254 } 255 256 InputFile *Lazy::fetch() { 257 if (auto *S = dyn_cast<LazyArchive>(this)) 258 return S->fetch(); 259 return cast<LazyObject>(this)->fetch(); 260 } 261 262 LazyArchive::LazyArchive(ArchiveFile &File, 263 const llvm::object::Archive::Symbol S, uint8_t Type) 264 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) { 265 this->File = &File; 266 } 267 268 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type) 269 : Lazy(LazyObjectKind, Name, Type) { 270 this->File = &File; 271 } 272 273 InputFile *LazyArchive::fetch() { 274 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym); 275 276 // getMember returns an empty buffer if the member was already 277 // read from the library. 278 if (MBInfo.first.getBuffer().empty()) 279 return nullptr; 280 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second); 281 } 282 283 InputFile *LazyObject::fetch() { 284 MemoryBufferRef MBRef = file()->getBuffer(); 285 if (MBRef.getBuffer().empty()) 286 return nullptr; 287 return createObjectFile(MBRef); 288 } 289 290 bool Symbol::includeInDynsym() const { 291 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) 292 return false; 293 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() || 294 (body()->isUndefined() && Config->Shared); 295 } 296 297 // Print out a log message for --trace-symbol. 298 void elf::printTraceSymbol(Symbol *Sym) { 299 SymbolBody *B = Sym->body(); 300 outs() << toString(B->File); 301 302 if (B->isUndefined()) 303 outs() << ": reference to "; 304 else if (B->isCommon()) 305 outs() << ": common definition of "; 306 else 307 outs() << ": definition of "; 308 outs() << B->getName() << "\n"; 309 } 310 311 // Returns a symbol for an error message. 312 std::string elf::toString(const SymbolBody &B) { 313 if (Config->Demangle) 314 if (Optional<std::string> S = demangle(B.getName())) 315 return *S; 316 return B.getName(); 317 } 318 319 template bool SymbolBody::hasThunk<ELF32LE>() const; 320 template bool SymbolBody::hasThunk<ELF32BE>() const; 321 template bool SymbolBody::hasThunk<ELF64LE>() const; 322 template bool SymbolBody::hasThunk<ELF64BE>() const; 323 324 template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; 325 template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; 326 template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; 327 template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; 328 329 template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; 330 template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; 331 template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; 332 template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; 333 334 template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const; 335 template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const; 336 template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const; 337 template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const; 338 339 template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; 340 template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; 341 template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; 342 template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; 343 344 template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; 345 template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; 346 template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; 347 template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const; 348 349 template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const; 350 template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const; 351 template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const; 352 template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const; 353 354 template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; 355 template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; 356 template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; 357 template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; 358 359 template uint32_t SymbolBody::template getSize<ELF32LE>() const; 360 template uint32_t SymbolBody::template getSize<ELF32BE>() const; 361 template uint64_t SymbolBody::template getSize<ELF64LE>() const; 362 template uint64_t SymbolBody::template getSize<ELF64BE>() const; 363 364 template class elf::DefinedRegular<ELF32LE>; 365 template class elf::DefinedRegular<ELF32BE>; 366 template class elf::DefinedRegular<ELF64LE>; 367 template class elf::DefinedRegular<ELF64BE>; 368 369 template class elf::DefinedSynthetic<ELF32LE>; 370 template class elf::DefinedSynthetic<ELF32BE>; 371 template class elf::DefinedSynthetic<ELF64LE>; 372 template class elf::DefinedSynthetic<ELF64BE>; 373