2015-07-25 05:03:07 +08:00
|
|
|
//===- Symbols.cpp --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Symbols.h"
|
2015-08-06 23:33:19 +08:00
|
|
|
#include "Error.h"
|
2015-07-29 06:58:25 +08:00
|
|
|
#include "InputFiles.h"
|
2016-02-02 05:00:35 +08:00
|
|
|
#include "InputSection.h"
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "Target.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-01-14 02:55:39 +08:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_CXXABI_H
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-07-25 05:03:07 +08:00
|
|
|
using namespace llvm::object;
|
2015-09-02 07:12:52 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t SymbolBody::getVA() const {
|
|
|
|
switch (kind()) {
|
|
|
|
case DefinedSyntheticKind: {
|
|
|
|
auto *D = cast<DefinedSynthetic<ELFT>>(this);
|
|
|
|
return D->Section.getVA() + D->Value;
|
|
|
|
}
|
|
|
|
case DefinedRegularKind: {
|
|
|
|
auto *D = cast<DefinedRegular<ELFT>>(this);
|
|
|
|
InputSectionBase<ELFT> *SC = D->Section;
|
|
|
|
|
|
|
|
// This is an absolute symbol.
|
|
|
|
if (!SC)
|
|
|
|
return D->Sym.st_value;
|
2016-02-26 02:43:51 +08:00
|
|
|
assert(SC->Live);
|
2016-02-02 05:00:35 +08:00
|
|
|
|
2016-02-26 23:39:26 +08:00
|
|
|
// Symbol offsets for AMDGPU are the offsets in bytes of the symbols
|
|
|
|
// from the beginning of the section. Note that this part of AMDGPU's
|
|
|
|
// ELF spec is odd and not in line with the standard ELF.
|
2016-02-02 05:00:35 +08:00
|
|
|
if (Config->EMachine == EM_AMDGPU)
|
|
|
|
return SC->getOffset(D->Sym);
|
2016-02-26 23:39:26 +08:00
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
if (D->Sym.getType() == STT_TLS)
|
|
|
|
return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
|
|
|
|
Out<ELFT>::TlsPhdr->p_vaddr;
|
|
|
|
return SC->OutSec->getVA() + SC->getOffset(D->Sym);
|
|
|
|
}
|
|
|
|
case DefinedCommonKind:
|
|
|
|
return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
|
|
|
|
case SharedKind: {
|
|
|
|
auto *SS = cast<SharedSymbol<ELFT>>(this);
|
2016-02-09 23:11:01 +08:00
|
|
|
if (!SS->NeedsCopyOrPltAddr)
|
|
|
|
return 0;
|
|
|
|
if (SS->IsFunc)
|
|
|
|
return getPltVA<ELFT>();
|
|
|
|
else
|
2016-02-02 05:00:35 +08:00
|
|
|
return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
|
|
|
|
}
|
|
|
|
case UndefinedElfKind:
|
|
|
|
case UndefinedKind:
|
|
|
|
return 0;
|
|
|
|
case LazyKind:
|
|
|
|
assert(isUsedInRegularObj() && "Lazy symbol reached writer");
|
|
|
|
return 0;
|
2016-02-13 04:54:57 +08:00
|
|
|
case DefinedBitcodeKind:
|
|
|
|
llvm_unreachable("Should have been replaced");
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid symbol kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
|
|
|
|
return Out<ELFT>::Got->getVA() +
|
|
|
|
(Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
|
|
|
|
sizeof(typename ELFFile<ELFT>::uintX_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
|
|
|
|
return Out<ELFT>::GotPlt->getVA() +
|
|
|
|
GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
|
|
|
|
return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
|
|
|
|
PltIndex * Target->PltEntrySize;
|
|
|
|
}
|
|
|
|
|
2016-02-03 08:12:24 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
|
|
|
|
if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
|
|
|
|
return B->Sym.st_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-02 07:12:52 +08:00
|
|
|
static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
|
|
|
|
if (VA == STV_DEFAULT)
|
|
|
|
return VB;
|
|
|
|
if (VB == STV_DEFAULT)
|
|
|
|
return VA;
|
|
|
|
return std::min(VA, VB);
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Returns 1, 0 or -1 if this symbol should take precedence
|
|
|
|
// over the Other, tie or lose, respectively.
|
2015-08-31 09:16:19 +08:00
|
|
|
template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
|
2015-12-17 07:49:19 +08:00
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
2015-09-05 06:28:10 +08:00
|
|
|
assert(!isLazy() && !Other->isLazy());
|
2016-01-19 07:54:05 +08:00
|
|
|
std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
|
|
|
|
std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
|
|
|
|
!Other->isWeak());
|
2015-07-28 04:39:01 +08:00
|
|
|
|
2015-08-29 04:19:34 +08:00
|
|
|
// Normalize
|
|
|
|
if (L > R)
|
2015-08-31 09:16:19 +08:00
|
|
|
return -Other->compare<ELFT>(this);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-22 02:13:47 +08:00
|
|
|
Visibility = Other->Visibility =
|
|
|
|
getMinVisibility(Visibility, Other->Visibility);
|
2015-09-02 07:12:52 +08:00
|
|
|
|
2015-10-22 03:41:03 +08:00
|
|
|
if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
|
|
|
|
IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2016-01-25 16:44:38 +08:00
|
|
|
// We want to export all symbols that exist both in the executable
|
|
|
|
// and in DSOs, so that the symbols in the executable can interrupt
|
|
|
|
// symbols in the DSO at runtime.
|
|
|
|
if (isShared() != Other->isShared())
|
|
|
|
if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
|
2016-02-05 23:27:15 +08:00
|
|
|
MustBeInDynSym = Other->MustBeInDynSym = true;
|
2016-01-25 16:44:38 +08:00
|
|
|
|
2015-08-29 04:19:34 +08:00
|
|
|
if (L != R)
|
|
|
|
return -1;
|
2016-01-19 07:54:05 +08:00
|
|
|
if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
|
2015-09-23 22:23:59 +08:00
|
|
|
return 1;
|
2015-09-10 01:40:51 +08:00
|
|
|
if (isCommon()) {
|
2015-09-10 01:55:09 +08:00
|
|
|
if (!Other->isCommon())
|
2015-08-31 07:17:30 +08:00
|
|
|
return -1;
|
2015-12-25 00:23:37 +08:00
|
|
|
auto *ThisC = cast<DefinedCommon>(this);
|
|
|
|
auto *OtherC = cast<DefinedCommon>(Other);
|
2015-12-17 07:49:19 +08:00
|
|
|
uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
|
2015-12-25 00:23:37 +08:00
|
|
|
if (ThisC->Size >= OtherC->Size) {
|
2015-12-17 07:49:19 +08:00
|
|
|
ThisC->MaxAlignment = Align;
|
2015-09-10 01:55:09 +08:00
|
|
|
return 1;
|
2015-08-31 09:16:19 +08:00
|
|
|
}
|
2015-12-17 07:49:19 +08:00
|
|
|
OtherC->MaxAlignment = Align;
|
2015-09-10 01:40:51 +08:00
|
|
|
return -1;
|
2015-08-31 07:17:30 +08:00
|
|
|
}
|
2015-09-10 01:40:51 +08:00
|
|
|
if (Other->isCommon())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-08-31 09:16:19 +08:00
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
|
2016-02-02 17:28:53 +08:00
|
|
|
bool IsTls, bool IsFunction)
|
|
|
|
: SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {}
|
2015-12-24 08:47:42 +08:00
|
|
|
|
2016-02-26 00:25:41 +08:00
|
|
|
DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak)
|
|
|
|
: Defined(DefinedBitcodeKind, Name, IsWeak, STV_DEFAULT, false, false) {}
|
2016-02-13 04:54:57 +08:00
|
|
|
|
|
|
|
bool DefinedBitcode::classof(const SymbolBody *S) {
|
|
|
|
return S->kind() == DefinedBitcodeKind;
|
|
|
|
}
|
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
|
|
|
|
uint8_t Visibility, bool IsTls)
|
2016-02-02 17:28:53 +08:00
|
|
|
: SymbolBody(K, N, IsWeak, Visibility, IsTls, /*IsFunction*/ false),
|
|
|
|
CanKeepUndefined(false) {}
|
2015-12-23 07:00:50 +08:00
|
|
|
|
|
|
|
Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
|
|
|
|
bool CanKeepUndefined)
|
|
|
|
: Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
|
|
|
|
/*IsTls*/ false) {
|
|
|
|
this->CanKeepUndefined = CanKeepUndefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ELFT>
|
|
|
|
UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
|
|
|
|
: Undefined(SymbolBody::UndefinedElfKind, N,
|
|
|
|
Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
|
|
|
|
Sym.getType() == llvm::ELF::STT_TLS),
|
|
|
|
Sym(Sym) {}
|
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
template <typename ELFT>
|
|
|
|
DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
|
|
|
|
OutputSectionBase<ELFT> &Section)
|
2016-02-02 17:28:53 +08:00
|
|
|
: Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT,
|
|
|
|
/*IsTls*/ false, /*IsFunction*/ false),
|
2015-12-24 08:47:42 +08:00
|
|
|
Value(Value), Section(Section) {}
|
|
|
|
|
2015-12-25 00:23:37 +08:00
|
|
|
DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
|
|
|
|
bool IsWeak, uint8_t Visibility)
|
2016-02-02 17:28:53 +08:00
|
|
|
: Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
|
|
|
|
/*IsTls*/ false, /*IsFunction*/ false) {
|
2015-12-25 00:23:37 +08:00
|
|
|
MaxAlignment = Alignment;
|
|
|
|
this->Size = Size;
|
|
|
|
}
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
std::unique_ptr<InputFile> Lazy::getMember() {
|
|
|
|
MemoryBufferRef MBRef = File->getMember(&Sym);
|
|
|
|
|
|
|
|
// getMember returns an empty buffer if the member was already
|
|
|
|
// read from the library.
|
|
|
|
if (MBRef.getBuffer().empty())
|
|
|
|
return std::unique_ptr<InputFile>(nullptr);
|
2016-02-02 16:22:41 +08:00
|
|
|
return createObjectFile(MBRef, File->getName());
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 08:44:28 +08:00
|
|
|
template <class ELFT> static void doInitSymbols() {
|
2016-02-26 22:36:36 +08:00
|
|
|
ElfSym<ELFT>::Etext.setBinding(STB_GLOBAL);
|
|
|
|
ElfSym<ELFT>::Edata.setBinding(STB_GLOBAL);
|
2015-12-25 14:12:18 +08:00
|
|
|
ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
|
2016-01-20 05:19:52 +08:00
|
|
|
ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
|
|
|
|
ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
|
2015-10-08 08:44:28 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 04:11:55 +08:00
|
|
|
void elf2::initSymbols() {
|
2015-10-08 08:44:28 +08:00
|
|
|
doInitSymbols<ELF32LE>();
|
|
|
|
doInitSymbols<ELF32BE>();
|
|
|
|
doInitSymbols<ELF64LE>();
|
|
|
|
doInitSymbols<ELF64BE>();
|
2015-10-08 07:46:11 +08:00
|
|
|
}
|
|
|
|
|
2016-01-14 02:55:39 +08:00
|
|
|
// Returns the demangled C++ symbol name for Name.
|
|
|
|
std::string elf2::demangle(StringRef Name) {
|
|
|
|
#if !defined(HAVE_CXXABI_H)
|
|
|
|
return Name;
|
|
|
|
#else
|
|
|
|
if (!Config->Demangle)
|
|
|
|
return Name;
|
2016-01-14 03:40:13 +08:00
|
|
|
|
2016-01-14 06:09:09 +08:00
|
|
|
// __cxa_demangle can be used to demangle strings other than symbol
|
|
|
|
// names which do not necessarily start with "_Z". Name can be
|
|
|
|
// either a C or C++ symbol. Don't call __cxa_demangle if the name
|
|
|
|
// does not look like a C++ symbol name to avoid getting unexpected
|
|
|
|
// result for a C symbol that happens to match a mangled type name.
|
2016-01-14 03:40:13 +08:00
|
|
|
if (!Name.startswith("_Z"))
|
|
|
|
return Name;
|
|
|
|
|
2016-01-14 02:55:39 +08:00
|
|
|
char *Buf =
|
|
|
|
abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
|
|
|
|
if (!Buf)
|
|
|
|
return Name;
|
|
|
|
std::string S(Buf);
|
|
|
|
free(Buf);
|
|
|
|
return S;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
template uint32_t SymbolBody::template getVA<ELF32LE>() const;
|
|
|
|
template uint32_t SymbolBody::template getVA<ELF32BE>() const;
|
|
|
|
template uint64_t SymbolBody::template getVA<ELF64LE>() const;
|
|
|
|
template uint64_t SymbolBody::template getVA<ELF64BE>() const;
|
|
|
|
|
|
|
|
template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
|
|
|
|
template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
|
|
|
|
template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
|
|
|
|
template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
|
|
|
|
|
|
|
|
template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
|
|
|
|
template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
|
|
|
|
template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
|
|
|
|
template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
|
|
|
|
|
|
|
|
template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
|
|
|
|
template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
|
|
|
|
template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
|
|
|
|
template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
|
|
|
|
|
2016-02-03 08:12:24 +08:00
|
|
|
template uint32_t SymbolBody::template getSize<ELF32LE>() const;
|
|
|
|
template uint32_t SymbolBody::template getSize<ELF32BE>() const;
|
|
|
|
template uint64_t SymbolBody::template getSize<ELF64LE>() const;
|
|
|
|
template uint64_t SymbolBody::template getSize<ELF64BE>() const;
|
|
|
|
|
2015-08-31 09:16:19 +08:00
|
|
|
template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
|
|
|
|
template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
|
|
|
|
template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
|
|
|
|
template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
|
2015-12-23 07:00:50 +08:00
|
|
|
|
2016-01-07 04:11:55 +08:00
|
|
|
template class elf2::UndefinedElf<ELF32LE>;
|
|
|
|
template class elf2::UndefinedElf<ELF32BE>;
|
|
|
|
template class elf2::UndefinedElf<ELF64LE>;
|
|
|
|
template class elf2::UndefinedElf<ELF64BE>;
|
2015-12-24 08:47:42 +08:00
|
|
|
|
2016-01-07 04:11:55 +08:00
|
|
|
template class elf2::DefinedSynthetic<ELF32LE>;
|
|
|
|
template class elf2::DefinedSynthetic<ELF32BE>;
|
|
|
|
template class elf2::DefinedSynthetic<ELF64LE>;
|
|
|
|
template class elf2::DefinedSynthetic<ELF64BE>;
|