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-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-08-06 23:33:19 +08:00
|
|
|
#include "Error.h"
|
2015-07-29 06:58:25 +08:00
|
|
|
#include "InputFiles.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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());
|
2015-08-31 07:17:30 +08:00
|
|
|
std::pair<bool, bool> L(isDefined(), !isWeak());
|
|
|
|
std::pair<bool, bool> R(Other->isDefined(), !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
|
|
|
|
2015-08-29 04:19:34 +08:00
|
|
|
if (L != R)
|
|
|
|
return -1;
|
2015-09-10 01:40:51 +08:00
|
|
|
if (!L.first || !L.second)
|
|
|
|
return 1;
|
2015-09-23 22:23:59 +08:00
|
|
|
if (isShared())
|
|
|
|
return -1;
|
|
|
|
if (Other->isShared())
|
|
|
|
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,
|
|
|
|
bool IsTls)
|
|
|
|
: SymbolBody(K, Name, IsWeak, Visibility, IsTls) {}
|
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
|
|
|
|
uint8_t Visibility, bool IsTls)
|
|
|
|
: SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {}
|
|
|
|
|
|
|
|
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)
|
|
|
|
: Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false),
|
|
|
|
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)
|
|
|
|
: Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return createELFFile<ObjectFile>(MBRef);
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:44:28 +08:00
|
|
|
template <class ELFT> static void doInitSymbols() {
|
2015-12-25 14:12:18 +08:00
|
|
|
ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
|
|
|
|
ElfSym<ELFT>::IgnoreUndef.setBinding(STB_WEAK);
|
|
|
|
ElfSym<ELFT>::IgnoreUndef.setVisibility(STV_HIDDEN);
|
2015-10-08 08:44:28 +08:00
|
|
|
}
|
|
|
|
|
2015-10-08 07:46:11 +08:00
|
|
|
void lld::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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
template class lld::elf2::UndefinedElf<ELF32LE>;
|
|
|
|
template class lld::elf2::UndefinedElf<ELF32BE>;
|
|
|
|
template class lld::elf2::UndefinedElf<ELF64LE>;
|
|
|
|
template class lld::elf2::UndefinedElf<ELF64BE>;
|
2015-12-24 08:47:42 +08:00
|
|
|
|
|
|
|
template class lld::elf2::DefinedSynthetic<ELF32LE>;
|
|
|
|
template class lld::elf2::DefinedSynthetic<ELF32BE>;
|
|
|
|
template class lld::elf2::DefinedSynthetic<ELF64LE>;
|
|
|
|
template class lld::elf2::DefinedSynthetic<ELF64BE>;
|