2015-08-14 22:12:54 +08:00
|
|
|
//===- Symbols.h ------------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_SYMBOLS_H
|
|
|
|
#define LLD_ELF_SYMBOLS_H
|
|
|
|
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-08-25 04:06:32 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
class ArchiveFile;
|
2015-07-25 05:03:07 +08:00
|
|
|
class InputFile;
|
|
|
|
class SymbolBody;
|
2015-07-29 06:58:25 +08:00
|
|
|
template <class ELFT> class ObjectFile;
|
2015-09-19 06:13:25 +08:00
|
|
|
template <class ELFT> class OutputSection;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// A real symbol object, SymbolBody, is usually accessed indirectly
|
|
|
|
// through a Symbol. There's always one Symbol for each symbol name.
|
|
|
|
// The resolver updates SymbolBody pointers as it resolves symbols.
|
|
|
|
struct Symbol {
|
|
|
|
explicit Symbol(SymbolBody *P) : Body(P) {}
|
|
|
|
SymbolBody *Body;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The base class for real symbol classes.
|
|
|
|
class SymbolBody {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
2015-08-12 01:10:02 +08:00
|
|
|
DefinedFirst = 0,
|
|
|
|
DefinedRegularKind = 0,
|
2015-08-27 20:40:06 +08:00
|
|
|
DefinedAbsoluteKind = 1,
|
2015-08-29 05:26:51 +08:00
|
|
|
DefinedCommonKind = 2,
|
2015-09-08 23:50:05 +08:00
|
|
|
SharedKind = 3,
|
|
|
|
DefinedLast = 3,
|
|
|
|
UndefinedKind = 4,
|
|
|
|
LazyKind = 5,
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2015-07-29 06:58:25 +08:00
|
|
|
Kind kind() const { return static_cast<Kind>(SymbolKind); }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-08-29 04:19:34 +08:00
|
|
|
bool isWeak() const { return IsWeak; }
|
2015-08-31 09:46:20 +08:00
|
|
|
bool isUndefined() const { return SymbolKind == UndefinedKind; }
|
2015-09-05 06:28:10 +08:00
|
|
|
bool isDefined() const { return SymbolKind <= DefinedLast; }
|
2015-08-31 07:17:30 +08:00
|
|
|
bool isStrongUndefined() const { return !IsWeak && isUndefined(); }
|
|
|
|
bool isCommon() const { return SymbolKind == DefinedCommonKind; }
|
2015-09-05 06:28:10 +08:00
|
|
|
bool isLazy() const { return SymbolKind == LazyKind; }
|
2015-09-08 23:50:05 +08:00
|
|
|
bool isShared() const { return SymbolKind == SharedKind; }
|
|
|
|
bool isUsedInRegularObj() const { return IsUsedInRegularObj; }
|
2015-09-23 07:38:23 +08:00
|
|
|
bool isUsedInDynamicReloc() const { return IsUsedInDynamicReloc; }
|
|
|
|
void setUsedInDynamicReloc() { IsUsedInDynamicReloc = true; }
|
2015-08-15 00:46:28 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Returns the symbol name.
|
2015-07-29 06:58:25 +08:00
|
|
|
StringRef getName() const { return Name; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-02 07:12:52 +08:00
|
|
|
uint8_t getMostConstrainingVisibility() const {
|
|
|
|
return MostConstrainingVisibility;
|
|
|
|
}
|
|
|
|
|
2015-09-16 23:54:15 +08:00
|
|
|
unsigned getDynamicSymbolTableIndex() const {
|
|
|
|
return DynamicSymbolTableIndex;
|
|
|
|
}
|
|
|
|
void setDynamicSymbolTableIndex(unsigned V) { DynamicSymbolTableIndex = V; }
|
|
|
|
|
2015-09-18 22:40:19 +08:00
|
|
|
unsigned getGotIndex() const { return GotIndex; }
|
|
|
|
bool isInGot() const { return GotIndex != -1U; }
|
|
|
|
void setGotIndex(unsigned I) { GotIndex = I; }
|
|
|
|
|
2015-09-21 23:11:29 +08:00
|
|
|
unsigned getPltIndex() const { return PltIndex; }
|
|
|
|
bool isInPlt() const { return PltIndex != -1U; }
|
|
|
|
void setPltIndex(unsigned I) { PltIndex = I; }
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// A SymbolBody has a backreference to a Symbol. Originally they are
|
|
|
|
// doubly-linked. A backreference will never change. But the pointer
|
|
|
|
// in the Symbol may be mutated by the resolver. If you have a
|
|
|
|
// pointer P to a SymbolBody and are not sure whether the resolver
|
|
|
|
// has chosen the object among other objects having the same name,
|
|
|
|
// you can access P->Backref->Body to get the resolver's result.
|
|
|
|
void setBackref(Symbol *P) { Backref = P; }
|
2015-08-28 07:15:56 +08:00
|
|
|
SymbolBody *getReplacement() { return Backref ? Backref->Body : this; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// Decides which symbol should "win" in the symbol table, this or
|
|
|
|
// the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
|
|
|
|
// they are duplicate (conflicting) symbols.
|
2015-08-31 09:16:19 +08:00
|
|
|
template <class ELFT> int compare(SymbolBody *Other);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
protected:
|
2015-09-02 07:12:52 +08:00
|
|
|
SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility)
|
|
|
|
: SymbolKind(K), IsWeak(IsWeak), MostConstrainingVisibility(Visibility),
|
2015-09-08 23:50:05 +08:00
|
|
|
Name(Name) {
|
|
|
|
IsUsedInRegularObj = K != SharedKind && K != LazyKind;
|
2015-09-23 07:38:23 +08:00
|
|
|
IsUsedInDynamicReloc = 0;
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-07-29 06:58:25 +08:00
|
|
|
const unsigned SymbolKind : 8;
|
2015-08-29 04:19:34 +08:00
|
|
|
const unsigned IsWeak : 1;
|
2015-09-02 07:12:52 +08:00
|
|
|
unsigned MostConstrainingVisibility : 2;
|
2015-09-08 23:50:05 +08:00
|
|
|
unsigned IsUsedInRegularObj : 1;
|
2015-09-23 07:38:23 +08:00
|
|
|
unsigned IsUsedInDynamicReloc : 1;
|
2015-09-16 23:54:15 +08:00
|
|
|
unsigned DynamicSymbolTableIndex = 0;
|
2015-09-18 22:40:19 +08:00
|
|
|
unsigned GotIndex = -1;
|
2015-09-21 23:11:29 +08:00
|
|
|
unsigned PltIndex = -1;
|
2015-07-29 06:58:25 +08:00
|
|
|
StringRef Name;
|
2015-07-25 05:03:07 +08:00
|
|
|
Symbol *Backref = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-08-15 00:46:28 +08:00
|
|
|
// This is for symbols created from elf files and not from the command line.
|
|
|
|
// Since they come from object files, they have a Elf_Sym.
|
|
|
|
//
|
|
|
|
// FIXME: Another alternative is to give every symbol an Elf_Sym. To do that
|
|
|
|
// we have to delay creating the symbol table until the output format is
|
|
|
|
// known and some of its methods will be templated. We should experiment with
|
|
|
|
// that once we have a bit more code.
|
|
|
|
template <class ELFT> class ELFSymbolBody : public SymbolBody {
|
|
|
|
protected:
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
ELFSymbolBody(Kind K, StringRef Name, const Elf_Sym &Sym)
|
2015-09-02 07:12:52 +08:00
|
|
|
: SymbolBody(K, Name, Sym.getBinding() == llvm::ELF::STB_WEAK,
|
|
|
|
Sym.getVisibility()),
|
|
|
|
Sym(Sym) {}
|
2015-08-15 00:46:28 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
const Elf_Sym &Sym;
|
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
Kind K = S->kind();
|
|
|
|
return K >= DefinedFirst && K <= UndefinedKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// The base class for any defined symbols, including absolute symbols,
|
|
|
|
// etc.
|
2015-08-15 00:46:28 +08:00
|
|
|
template <class ELFT> class Defined : public ELFSymbolBody<ELFT> {
|
|
|
|
typedef ELFSymbolBody<ELFT> Base;
|
2015-08-27 20:40:06 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef typename Base::Kind Kind;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Defined(Kind K, StringRef N, const Elf_Sym &Sym)
|
|
|
|
: ELFSymbolBody<ELFT>(K, N, Sym) {}
|
2015-08-29 04:19:34 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) { return S->isDefined(); }
|
2015-08-27 20:40:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> class DefinedAbsolute : public Defined<ELFT> {
|
|
|
|
typedef ELFSymbolBody<ELFT> Base;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
2015-09-24 21:34:01 +08:00
|
|
|
static Elf_Sym IgnoreUndef;
|
|
|
|
|
2015-08-27 20:40:06 +08:00
|
|
|
explicit DefinedAbsolute(StringRef N, const Elf_Sym &Sym)
|
|
|
|
: Defined<ELFT>(Base::DefinedAbsoluteKind, N, Sym) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-08-29 04:19:34 +08:00
|
|
|
return S->kind() == Base::DefinedAbsoluteKind;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-24 21:34:01 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedAbsolute<ELFT>::Elf_Sym DefinedAbsolute<ELFT>::IgnoreUndef;
|
|
|
|
|
2015-08-29 05:26:51 +08:00
|
|
|
template <class ELFT> class DefinedCommon : public Defined<ELFT> {
|
|
|
|
typedef ELFSymbolBody<ELFT> Base;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
2015-09-01 06:55:21 +08:00
|
|
|
typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
|
|
|
|
uintX_t;
|
2015-08-29 05:26:51 +08:00
|
|
|
explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
|
2015-09-01 09:19:12 +08:00
|
|
|
: Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
|
|
|
|
MaxAlignment = Sym.st_value;
|
|
|
|
}
|
2015-08-29 05:26:51 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
return S->kind() == Base::DefinedCommonKind;
|
|
|
|
}
|
2015-09-01 06:55:21 +08:00
|
|
|
|
|
|
|
// The output offset of this common symbol in the output bss. Computed by the
|
|
|
|
// writer.
|
|
|
|
uintX_t OffsetInBSS;
|
2015-09-01 09:19:12 +08:00
|
|
|
|
|
|
|
// The maximum alignment we have seen for this symbol.
|
|
|
|
uintX_t MaxAlignment;
|
2015-08-29 05:26:51 +08:00
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Regular defined symbols read from object file symbol tables.
|
2015-08-29 04:19:34 +08:00
|
|
|
template <class ELFT> class DefinedRegular : public Defined<ELFT> {
|
2015-08-14 23:10:49 +08:00
|
|
|
typedef Defined<ELFT> Base;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-08-25 04:06:32 +08:00
|
|
|
explicit DefinedRegular(StringRef N, const Elf_Sym &Sym,
|
2015-09-22 01:47:00 +08:00
|
|
|
InputSection<ELFT> &Section)
|
2015-08-29 04:19:34 +08:00
|
|
|
: Defined<ELFT>(Base::DefinedRegularKind, N, Sym), Section(Section) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-08-14 23:10:49 +08:00
|
|
|
return S->kind() == Base::DefinedRegularKind;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 01:47:00 +08:00
|
|
|
const InputSection<ELFT> &Section;
|
2015-08-12 01:33:02 +08:00
|
|
|
};
|
|
|
|
|
2015-08-31 09:46:20 +08:00
|
|
|
// Undefined symbol.
|
2015-08-15 00:46:28 +08:00
|
|
|
template <class ELFT> class Undefined : public ELFSymbolBody<ELFT> {
|
|
|
|
typedef ELFSymbolBody<ELFT> Base;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
2015-08-31 09:46:20 +08:00
|
|
|
static Elf_Sym Synthetic;
|
|
|
|
|
2015-08-15 00:46:28 +08:00
|
|
|
explicit Undefined(StringRef N, const Elf_Sym &Sym)
|
|
|
|
: ELFSymbolBody<ELFT>(Base::UndefinedKind, N, Sym) {}
|
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
return S->kind() == Base::UndefinedKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-31 09:46:20 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename Undefined<ELFT>::Elf_Sym Undefined<ELFT>::Synthetic;
|
|
|
|
|
2015-09-25 23:34:03 +08:00
|
|
|
template <class ELFT> class SharedSymbol : public Defined<ELFT> {
|
2015-09-08 23:50:05 +08:00
|
|
|
typedef ELFSymbolBody<ELFT> Base;
|
|
|
|
typedef typename Base::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
return S->kind() == Base::SharedKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSymbol(StringRef Name, const Elf_Sym &Sym)
|
2015-09-25 23:34:03 +08:00
|
|
|
: Defined<ELFT>(Base::SharedKind, Name, Sym) {}
|
2015-09-08 23:50:05 +08:00
|
|
|
};
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
// This class represents a symbol defined in an archive file. It is
|
|
|
|
// created from an archive file header, and it knows how to load an
|
|
|
|
// object file from an archive to replace itself with a defined
|
|
|
|
// symbol. If the resolver finds both Undefined and Lazy for
|
|
|
|
// the same name, it will ask the Lazy to load a file.
|
|
|
|
class Lazy : public SymbolBody {
|
|
|
|
public:
|
|
|
|
Lazy(ArchiveFile *F, const llvm::object::Archive::Symbol S)
|
|
|
|
: SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT),
|
|
|
|
File(F), Sym(S) {}
|
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
|
|
|
|
|
|
|
|
// Returns an object file for this symbol, or a nullptr if the file
|
|
|
|
// was already returned.
|
|
|
|
std::unique_ptr<InputFile> getMember();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ArchiveFile *File;
|
|
|
|
const llvm::object::Archive::Symbol Sym;
|
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|