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;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
template <class ELFT>
|
2016-03-11 20:19:05 +08:00
|
|
|
static typename ELFFile<ELFT>::uintX_t
|
|
|
|
getSymVA(const SymbolBody &Body, typename ELFFile<ELFT>::uintX_t &Addend) {
|
|
|
|
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
|
|
|
|
switch (Body.kind()) {
|
|
|
|
case SymbolBody::DefinedSyntheticKind: {
|
|
|
|
auto &D = cast<DefinedSynthetic<ELFT>>(Body);
|
|
|
|
return D.Section.getVA() + D.Value;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
2016-03-11 20:19:05 +08:00
|
|
|
case SymbolBody::DefinedRegularKind: {
|
|
|
|
auto &D = cast<DefinedRegular<ELFT>>(Body);
|
|
|
|
InputSectionBase<ELFT> *SC = D.Section;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
|
|
|
// This is an absolute symbol.
|
|
|
|
if (!SC)
|
2016-03-11 20:19:05 +08:00
|
|
|
return D.Sym.st_value;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
2016-03-11 22:21:37 +08:00
|
|
|
const Elf_Sym &Sym = D.Sym;
|
|
|
|
uintX_t Offset = Sym.st_value;
|
|
|
|
if (Sym.getType() == STT_SECTION) {
|
|
|
|
Offset += Addend;
|
|
|
|
Addend = 0;
|
|
|
|
}
|
|
|
|
uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
|
|
|
|
if (Sym.getType() == STT_TLS)
|
|
|
|
return VA - Out<ELFT>::TlsPhdr->p_vaddr;
|
|
|
|
return VA;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
2016-03-11 20:19:05 +08:00
|
|
|
case SymbolBody::DefinedCommonKind:
|
|
|
|
return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
|
|
|
|
case SymbolBody::SharedKind: {
|
|
|
|
auto &SS = cast<SharedSymbol<ELFT>>(Body);
|
|
|
|
if (!SS.NeedsCopyOrPltAddr)
|
2016-02-09 23:11:01 +08:00
|
|
|
return 0;
|
2016-03-11 20:19:05 +08:00
|
|
|
if (SS.IsFunc)
|
|
|
|
return Body.getPltVA<ELFT>();
|
2016-02-09 23:11:01 +08:00
|
|
|
else
|
2016-03-11 20:19:05 +08:00
|
|
|
return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
2016-03-11 20:19:05 +08:00
|
|
|
case SymbolBody::UndefinedElfKind:
|
|
|
|
case SymbolBody::UndefinedKind:
|
2016-02-02 05:00:35 +08:00
|
|
|
return 0;
|
2016-03-11 20:19:05 +08:00
|
|
|
case SymbolBody::LazyKind:
|
2016-03-12 02:46:51 +08:00
|
|
|
assert(Body.isUsedInRegularObj() && "Lazy symbol reached writer");
|
2016-02-02 05:00:35 +08:00
|
|
|
return 0;
|
2016-03-11 20:19:05 +08:00
|
|
|
case SymbolBody::DefinedBitcodeKind:
|
2016-03-12 02:46:51 +08:00
|
|
|
llvm_unreachable("Should have been replaced");
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
2016-03-12 02:46:51 +08:00
|
|
|
llvm_unreachable("Invalid symbol kind");
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2016-03-11 20:19:05 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFFile<ELFT>::uintX_t
|
|
|
|
SymbolBody::getVA(typename ELFFile<ELFT>::uintX_t Addend) const {
|
|
|
|
return getSymVA<ELFT>(*this, Addend) + Addend;
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:49:24 +08:00
|
|
|
static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
|
2016-03-11 02:58:53 +08:00
|
|
|
A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
|
2016-03-11 02:49:24 +08:00
|
|
|
if (A->Size < B->Size)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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-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-03-11 02:49:24 +08:00
|
|
|
if (!isDefined() || isShared() || isWeak())
|
2015-09-10 01:40:51 +08:00
|
|
|
return 1;
|
2016-03-11 02:49:24 +08:00
|
|
|
if (!isCommon() && !Other->isCommon())
|
|
|
|
return 0;
|
|
|
|
if (isCommon() && Other->isCommon())
|
|
|
|
return compareCommons(cast<DefinedCommon>(this),
|
|
|
|
cast<DefinedCommon>(Other));
|
|
|
|
return isCommon() ? -1 : 1;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-08-31 09:16:19 +08:00
|
|
|
|
2016-03-11 22:21:37 +08:00
|
|
|
Defined::Defined(Kind K, StringRef Name, bool IsWeak, bool IsLocal,
|
|
|
|
uint8_t Visibility, uint8_t Type)
|
|
|
|
: SymbolBody(K, Name, IsWeak, IsLocal, Visibility, Type) {}
|
2015-12-24 08:47:42 +08:00
|
|
|
|
2016-03-08 01:14:36 +08:00
|
|
|
DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
|
2016-03-11 22:21:37 +08:00
|
|
|
: Defined(DefinedBitcodeKind, Name, IsWeak, false, Visibility,
|
|
|
|
0 /* Type */) {}
|
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,
|
2016-03-04 09:55:28 +08:00
|
|
|
uint8_t Visibility, uint8_t Type)
|
2016-03-11 22:21:37 +08:00
|
|
|
: SymbolBody(K, N, IsWeak, false, Visibility, Type),
|
2016-02-02 17:28:53 +08:00
|
|
|
CanKeepUndefined(false) {}
|
2015-12-23 07:00:50 +08:00
|
|
|
|
|
|
|
Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
|
|
|
|
bool CanKeepUndefined)
|
2016-03-04 09:55:28 +08:00
|
|
|
: Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
|
2015-12-23 07:00:50 +08:00
|
|
|
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(),
|
2016-03-04 09:55:28 +08:00
|
|
|
Sym.getType()),
|
2015-12-23 07:00:50 +08:00
|
|
|
Sym(Sym) {}
|
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
template <typename ELFT>
|
|
|
|
DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
|
2016-03-02 00:23:13 +08:00
|
|
|
OutputSectionBase<ELFT> &Section,
|
|
|
|
uint8_t Visibility)
|
2016-03-11 22:21:37 +08:00
|
|
|
: Defined(SymbolBody::DefinedSyntheticKind, N, false, false, Visibility,
|
2016-03-04 09:55:28 +08:00
|
|
|
0 /* Type */),
|
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-03-11 22:21:37 +08:00
|
|
|
: Defined(SymbolBody::DefinedCommonKind, N, IsWeak, false, Visibility,
|
2016-03-11 02:58:53 +08:00
|
|
|
0 /* Type */),
|
|
|
|
Alignment(Alignment), Size(Size) {}
|
2015-12-25 00:23:37 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-14 02:55:39 +08:00
|
|
|
// Returns the demangled C++ symbol name for Name.
|
2016-02-28 08:25:54 +08:00
|
|
|
std::string elf::demangle(StringRef Name) {
|
2016-01-14 02:55:39 +08:00
|
|
|
#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-03-11 20:19:05 +08:00
|
|
|
template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
|
|
|
|
template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
|
|
|
|
template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
|
|
|
|
template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
|
|
|
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-02-28 08:25:54 +08:00
|
|
|
template class elf::UndefinedElf<ELF32LE>;
|
|
|
|
template class elf::UndefinedElf<ELF32BE>;
|
|
|
|
template class elf::UndefinedElf<ELF64LE>;
|
|
|
|
template class elf::UndefinedElf<ELF64BE>;
|
2015-12-24 08:47:42 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::DefinedSynthetic<ELF32LE>;
|
|
|
|
template class elf::DefinedSynthetic<ELF32BE>;
|
|
|
|
template class elf::DefinedSynthetic<ELF64LE>;
|
|
|
|
template class elf::DefinedSynthetic<ELF64BE>;
|