Skip to content

Commit

Permalink
ELF: Merge UndefinedBitcode and UndefinedElf. NFC.
Browse files Browse the repository at this point in the history
Differential Revision: http://reviews.llvm.org/D19566

llvm-svn: 267640
  • Loading branch information
pcc committed Apr 27, 2016
1 parent 892d498 commit 60976ed
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 64 deletions.
24 changes: 15 additions & 9 deletions lld/ELF/InputFiles.cpp
Expand Up @@ -316,15 +316,18 @@ SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
InputSectionBase<ELFT> *Sec = getSection(*Sym);
if (Binding == STB_LOCAL) {
if (Sym->st_shndx == SHN_UNDEF)
return new (Alloc) UndefinedElf<ELFT>(*Sym);
return new (Alloc)
Undefined(Sym->st_name, Sym->st_other, Sym->getType(), Sym->st_size);
return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
}

StringRef Name = check(Sym->getName(this->StringTable));

switch (Sym->st_shndx) {
case SHN_UNDEF:
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
return new (Alloc)
Undefined(Name, Binding, Sym->st_other, Sym->getType(), Sym->st_size,
/*IsBitcode*/ false);
case SHN_COMMON:
return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Binding,
Sym->st_other, Sym->getType());
Expand All @@ -337,7 +340,9 @@ SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
case STB_WEAK:
case STB_GNU_UNIQUE:
if (Sec == &InputSection<ELFT>::Discarded)
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
return new (Alloc)
Undefined(Name, Binding, Sym->st_other, Sym->getType(), Sym->st_size,
/*IsBitcode*/ false);
return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
}
}
Expand Down Expand Up @@ -470,6 +475,7 @@ BitcodeFile::createBody(const DenseSet<const Comdat *> &KeptComdats,

uint32_t Flags = Sym.getFlags();
bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
uint32_t Binding = IsWeak ? STB_WEAK : STB_GLOBAL;

uint8_t Visibility;
if (GV)
Expand All @@ -482,23 +488,23 @@ BitcodeFile::createBody(const DenseSet<const Comdat *> &KeptComdats,
if (GV)
if (const Comdat *C = GV->getComdat())
if (!KeptComdats.count(C)) {
Body = new (Alloc)
UndefinedBitcode(NameRef, IsWeak, Visibility);
Body = new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
/*Size*/ 0, /*IsBitcode*/ true);
return Body;
}

const Module &M = Obj.getModule();
if (Flags & BasicSymbolRef::SF_Undefined)
return new (Alloc) UndefinedBitcode(NameRef, IsWeak, Visibility);
return new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
/*Size*/ 0, /*IsBitcode*/ true);
if (Flags & BasicSymbolRef::SF_Common) {
// FIXME: Set SF_Common flag correctly for module asm symbols, and expose
// size and alignment.
assert(GV);
const DataLayout &DL = M.getDataLayout();
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
return new (Alloc)
DefinedCommon(NameRef, Size, GV->getAlignment(),
IsWeak ? STB_WEAK : STB_GLOBAL, Visibility, /*Type*/ 0);
return new (Alloc) DefinedCommon(NameRef, Size, GV->getAlignment(), Binding,
Visibility, /*Type*/ 0);
}
return new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
}
Expand Down
3 changes: 1 addition & 2 deletions lld/ELF/OutputSections.cpp
Expand Up @@ -1503,8 +1503,7 @@ SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
return Out<ELFT>::Bss;
break;
case SymbolBody::UndefinedElfKind:
case SymbolBody::UndefinedBitcodeKind:
case SymbolBody::UndefinedKind:
case SymbolBody::LazyArchiveKind:
case SymbolBody::LazyObjectKind:
break;
Expand Down
5 changes: 3 additions & 2 deletions lld/ELF/SymbolTable.cpp
Expand Up @@ -151,7 +151,8 @@ template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
template <class ELFT>
SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
auto *Sym = new (Alloc)
UndefinedElf<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0);
Undefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, /*Size*/ 0,
/*IsBitcode*/ false);
resolve(Sym);
return Sym;
}
Expand Down Expand Up @@ -314,7 +315,7 @@ template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
if (K == SymbolBody::DefinedRegularKind ||
K == SymbolBody::DefinedCommonKind ||
K == SymbolBody::DefinedSyntheticKind ||
K == SymbolBody::UndefinedElfKind)
(K == SymbolBody::UndefinedKind && !New->IsUndefinedBitcode))
Sym->IsUsedInRegularObj = true;
return Sym;
}
Expand Down
41 changes: 13 additions & 28 deletions lld/ELF/Symbols.cpp
Expand Up @@ -75,8 +75,7 @@ static typename ELFT::uint getSymVA(const SymbolBody &Body,
return Body.getPltVA<ELFT>();
return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
}
case SymbolBody::UndefinedElfKind:
case SymbolBody::UndefinedBitcodeKind:
case SymbolBody::UndefinedKind:
return 0;
case SymbolBody::LazyArchiveKind:
case SymbolBody::LazyObjectKind:
Expand Down Expand Up @@ -175,7 +174,7 @@ template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
return DR->Size;
if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
return S->Sym.st_size;
if (const auto *U = dyn_cast<UndefinedElf<ELFT>>(this))
if (const auto *U = dyn_cast<Undefined>(this))
return U->Size;
return 0;
}
Expand Down Expand Up @@ -232,27 +231,18 @@ bool DefinedBitcode::classof(const SymbolBody *S) {
return S->kind() == DefinedBitcodeKind;
}

UndefinedBitcode::UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther)
: SymbolBody(SymbolBody::UndefinedBitcodeKind, N,
IsWeak ? STB_WEAK : STB_GLOBAL, StOther, 0 /* Type */) {}

template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
: SymbolBody(SymbolBody::UndefinedElfKind, N, Sym.getBinding(),
Sym.st_other, Sym.getType()),
Size(Sym.st_size) {}

template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(StringRef Name, uint8_t Binding,
uint8_t StOther, uint8_t Type)
: SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) {}
Undefined::Undefined(StringRef Name, uint8_t Binding, uint8_t StOther,
uint8_t Type, uint64_t Size, bool IsBitcode)
: SymbolBody(SymbolBody::UndefinedKind, Name, Binding, StOther, Type),
Size(Size) {
this->IsUndefinedBitcode = IsBitcode;
}

template <typename ELFT>
UndefinedElf<ELFT>::UndefinedElf(const Elf_Sym &Sym)
: SymbolBody(SymbolBody::UndefinedElfKind, Sym.st_name, Sym.st_other,
Sym.getType()),
Size(Sym.st_size) {
assert(Sym.getBinding() == STB_LOCAL);
Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
uint64_t Size)
: SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type),
Size(Size) {
this->IsUndefinedBitcode = false;
}

template <typename ELFT>
Expand Down Expand Up @@ -360,11 +350,6 @@ template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;

template class elf::UndefinedElf<ELF32LE>;
template class elf::UndefinedElf<ELF32BE>;
template class elf::UndefinedElf<ELF64LE>;
template class elf::UndefinedElf<ELF64BE>;

template class elf::DefinedSynthetic<ELF32LE>;
template class elf::DefinedSynthetic<ELF32BE>;
template class elf::DefinedSynthetic<ELF64LE>;
Expand Down
36 changes: 13 additions & 23 deletions lld/ELF/Symbols.h
Expand Up @@ -90,18 +90,15 @@ class SymbolBody {
DefinedBitcodeKind,
DefinedSyntheticKind,
DefinedLast = DefinedSyntheticKind,
UndefinedElfKind,
UndefinedBitcodeKind,
UndefinedKind,
LazyArchiveKind,
LazyObjectKind,
};

Kind kind() const { return static_cast<Kind>(SymbolKind); }

bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
bool isUndefined() const {
return SymbolKind == UndefinedBitcodeKind || SymbolKind == UndefinedElfKind;
}
bool isUndefined() const { return SymbolKind == UndefinedKind; }
bool isDefined() const { return SymbolKind <= DefinedLast; }
bool isCommon() const { return SymbolKind == DefinedCommonKind; }
bool isLazy() const {
Expand Down Expand Up @@ -177,6 +174,11 @@ class SymbolBody {
// symbol or if the symbol should point to its plt entry.
unsigned NeedsCopyOrPltAddr : 1;

// True if the symbol is undefined and comes from a bitcode file. We need to
// keep track of this because undefined symbols only prevent internalization
// of bitcode symbols if they did not come from a bitcode file.
unsigned IsUndefinedBitcode : 1;

// The following fields have the same meaning as the ELF symbol attributes.
uint8_t Type; // symbol type
uint8_t Binding; // symbol binding
Expand Down Expand Up @@ -307,28 +309,16 @@ template <class ELFT> class DefinedSynthetic : public Defined {
const OutputSectionBase<ELFT> &Section;
};

class UndefinedBitcode : public SymbolBody {
class Undefined : public SymbolBody {
public:
UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther);
Undefined(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
uint64_t Size, bool IsBitcode);
Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, uint64_t Size);

static bool classof(const SymbolBody *S) {
return S->kind() == UndefinedBitcodeKind;
}
};

template <class ELFT> class UndefinedElf : public SymbolBody {
typedef typename ELFT::uint uintX_t;
typedef typename ELFT::Sym Elf_Sym;

public:
UndefinedElf(StringRef N, const Elf_Sym &Sym);
UndefinedElf(const Elf_Sym &Sym);
UndefinedElf(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type);

uintX_t Size;
uint64_t Size;

static bool classof(const SymbolBody *S) {
return S->kind() == SymbolBody::UndefinedElfKind;
return S->kind() == UndefinedKind;
}
};

Expand Down

0 comments on commit 60976ed

Please sign in to comment.