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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
|
|
|
// All symbols are handled as SymbolBodies regardless of their types.
|
|
|
|
// This file defines various types of SymbolBodies.
|
|
|
|
//
|
|
|
|
// File-scope symbols in ELF objects are the only exception of SymbolBody
|
|
|
|
// instantiation. We will never create SymbolBodies for them for performance
|
|
|
|
// reason. They are often represented as nullptrs. This is fine for symbol
|
|
|
|
// resolution because the symbol table naturally cares only about
|
|
|
|
// externally-visible symbols. For relocations, you have to deal with both
|
|
|
|
// local and non-local functions, and we have two different functions
|
|
|
|
// where we need them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
#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-10-16 06:27:29 +08:00
|
|
|
template <class ELFT> class OutputSectionBase;
|
2015-10-12 04:59:12 +08:00
|
|
|
template <class ELFT> class SharedFile;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-08 07:46:11 +08:00
|
|
|
// Initializes global objects defined in this file.
|
|
|
|
// Called at the beginning of main().
|
|
|
|
void initSymbols();
|
|
|
|
|
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 {
|
|
|
|
SymbolBody *Body;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The base class for real symbol classes.
|
|
|
|
class SymbolBody {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
2015-09-26 05:20:23 +08:00
|
|
|
DefinedFirst,
|
|
|
|
DefinedRegularKind = DefinedFirst,
|
|
|
|
DefinedCommonKind,
|
|
|
|
SharedKind,
|
2015-12-24 08:47:42 +08:00
|
|
|
DefinedElfLast = SharedKind,
|
|
|
|
DefinedSyntheticKind,
|
|
|
|
DefinedLast = DefinedSyntheticKind,
|
2015-12-23 07:00:50 +08:00
|
|
|
UndefinedElfKind,
|
2015-09-26 05:20:23 +08:00
|
|
|
UndefinedKind,
|
|
|
|
LazyKind
|
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-12-23 07:00:50 +08:00
|
|
|
bool isUndefined() const {
|
|
|
|
return SymbolKind == UndefinedKind || SymbolKind == UndefinedElfKind;
|
|
|
|
}
|
2015-09-05 06:28:10 +08:00
|
|
|
bool isDefined() const { return SymbolKind <= DefinedLast; }
|
2015-08-31 07:17:30 +08:00
|
|
|
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-12-17 08:12:03 +08:00
|
|
|
bool isTls() const { return IsTls; }
|
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-10-22 02:13:47 +08:00
|
|
|
uint8_t getVisibility() const { return Visibility; }
|
2015-09-02 07:12:52 +08:00
|
|
|
|
2015-12-17 08:12:04 +08:00
|
|
|
unsigned DynamicSymbolTableIndex = 0;
|
2015-12-02 03:20:26 +08:00
|
|
|
uint32_t GlobalDynIndex = -1;
|
2015-10-09 08:42:06 +08:00
|
|
|
uint32_t GotIndex = -1;
|
2015-10-20 16:54:27 +08:00
|
|
|
uint32_t GotPltIndex = -1;
|
2015-10-09 08:42:06 +08:00
|
|
|
uint32_t PltIndex = -1;
|
2015-12-02 03:20:26 +08:00
|
|
|
bool hasGlobalDynIndex() { return GlobalDynIndex != uint32_t(-1); }
|
2015-09-18 22:40:19 +08:00
|
|
|
bool isInGot() const { return GotIndex != -1U; }
|
2015-10-20 16:54:27 +08:00
|
|
|
bool isInGotPlt() const { return GotPltIndex != -1U; }
|
2015-09-21 23:11:29 +08:00
|
|
|
bool isInPlt() const { return PltIndex != -1U; }
|
|
|
|
|
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-10-08 07:20:23 +08:00
|
|
|
SymbolBody *repl() { 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-10-09 17:58:39 +08:00
|
|
|
SymbolBody(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
|
2015-12-17 08:12:03 +08:00
|
|
|
bool IsTls)
|
|
|
|
: SymbolKind(K), IsWeak(IsWeak), Visibility(Visibility), IsTls(IsTls),
|
2015-10-22 02:13:47 +08:00
|
|
|
Name(Name) {
|
2015-09-08 23:50:05 +08:00
|
|
|
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-10-06 22:33:58 +08:00
|
|
|
unsigned IsWeak : 1;
|
2015-10-22 02:13:47 +08:00
|
|
|
unsigned Visibility : 2;
|
2015-09-08 23:50:05 +08:00
|
|
|
unsigned IsUsedInRegularObj : 1;
|
2015-09-23 07:38:23 +08:00
|
|
|
unsigned IsUsedInDynamicReloc : 1;
|
2015-12-17 08:12:03 +08:00
|
|
|
unsigned IsTls : 1;
|
2015-07-29 06:58:25 +08:00
|
|
|
StringRef Name;
|
2015-07-25 05:03:07 +08:00
|
|
|
Symbol *Backref = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-12-24 22:22:24 +08:00
|
|
|
// The base class for any defined symbols.
|
2015-12-24 08:47:42 +08:00
|
|
|
class Defined : public SymbolBody {
|
|
|
|
public:
|
|
|
|
Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, bool IsTls);
|
|
|
|
static bool classof(const SymbolBody *S) { return S->isDefined(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Any defined symbol from an ELF file.
|
|
|
|
template <class ELFT> class DefinedElf : public Defined {
|
2015-08-27 20:40:06 +08:00
|
|
|
protected:
|
2015-12-22 05:07:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-08-27 20:40:06 +08:00
|
|
|
|
|
|
|
public:
|
2015-12-24 08:47:42 +08:00
|
|
|
DefinedElf(Kind K, StringRef N, const Elf_Sym &Sym)
|
|
|
|
: Defined(K, N, Sym.getBinding() == llvm::ELF::STB_WEAK,
|
|
|
|
Sym.getVisibility(), Sym.getType() == llvm::ELF::STT_TLS),
|
2015-12-23 07:00:50 +08:00
|
|
|
Sym(Sym) {}
|
2015-08-29 04:19:34 +08:00
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
const Elf_Sym &Sym;
|
2015-12-24 08:47:42 +08:00
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
return S->kind() <= DefinedElfLast;
|
|
|
|
}
|
2015-08-27 20:40:06 +08:00
|
|
|
};
|
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
template <class ELFT> class DefinedCommon : public DefinedElf<ELFT> {
|
2015-12-22 05:07:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-08-29 05:26:51 +08:00
|
|
|
|
|
|
|
public:
|
2015-10-16 06:27:29 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-09-30 10:06:17 +08:00
|
|
|
DefinedCommon(StringRef N, const Elf_Sym &Sym)
|
2015-12-24 08:47:42 +08:00
|
|
|
: DefinedElf<ELFT>(SymbolBody::DefinedCommonKind, N, Sym) {
|
2015-09-01 09:19:12 +08:00
|
|
|
MaxAlignment = Sym.st_value;
|
|
|
|
}
|
2015-08-29 05:26:51 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-12-22 04:47:33 +08:00
|
|
|
return S->kind() == SymbolBody::DefinedCommonKind;
|
2015-08-29 05:26:51 +08:00
|
|
|
}
|
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-12-24 08:47:42 +08:00
|
|
|
template <class ELFT> class DefinedRegular : public DefinedElf<ELFT> {
|
2015-12-22 05:07:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-08-14 23:10:49 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-10-20 05:00:02 +08:00
|
|
|
DefinedRegular(StringRef N, const Elf_Sym &Sym,
|
2015-12-24 22:22:24 +08:00
|
|
|
InputSectionBase<ELFT> *Section)
|
2015-12-24 08:47:42 +08:00
|
|
|
: DefinedElf<ELFT>(SymbolBody::DefinedRegularKind, N, Sym),
|
2015-12-22 04:47:33 +08:00
|
|
|
Section(Section) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-12-22 04:47:33 +08:00
|
|
|
return S->kind() == SymbolBody::DefinedRegularKind;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 22:22:24 +08:00
|
|
|
// If this is null, the symbol is absolute.
|
|
|
|
InputSectionBase<ELFT> *Section;
|
|
|
|
|
|
|
|
static Elf_Sym IgnoreUndef;
|
|
|
|
|
|
|
|
// The following symbols must be added early to reserve their places
|
|
|
|
// in symbol tables. The value of the symbols are set when all sections
|
|
|
|
// are finalized and their addresses are determined.
|
|
|
|
|
|
|
|
// The content for _end and end symbols.
|
|
|
|
static Elf_Sym End;
|
|
|
|
|
|
|
|
// The content for _gp symbol for MIPS target.
|
|
|
|
static Elf_Sym MipsGp;
|
|
|
|
|
|
|
|
// __rel_iplt_start/__rel_iplt_end for signaling
|
|
|
|
// where R_[*]_IRELATIVE relocations do live.
|
|
|
|
static Elf_Sym RelaIpltStart;
|
|
|
|
static Elf_Sym RelaIpltEnd;
|
2015-08-12 01:33:02 +08:00
|
|
|
};
|
|
|
|
|
2015-12-24 22:22:24 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedRegular<ELFT>::Elf_Sym DefinedRegular<ELFT>::IgnoreUndef;
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedRegular<ELFT>::Elf_Sym DefinedRegular<ELFT>::End;
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedRegular<ELFT>::Elf_Sym DefinedRegular<ELFT>::MipsGp;
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedRegular<ELFT>::Elf_Sym DefinedRegular<ELFT>::RelaIpltStart;
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
typename DefinedRegular<ELFT>::Elf_Sym DefinedRegular<ELFT>::RelaIpltEnd;
|
|
|
|
|
2015-12-17 08:48:16 +08:00
|
|
|
// DefinedSynthetic is a class to represent linker-generated ELF symbols.
|
|
|
|
// The difference from the regular symbol is that DefinedSynthetic symbols
|
|
|
|
// don't belong to any input files or sections. Thus, its constructor
|
|
|
|
// takes an output section to calculate output VA, etc.
|
2015-12-24 08:47:42 +08:00
|
|
|
template <class ELFT> class DefinedSynthetic : public Defined {
|
2015-09-26 02:56:53 +08:00
|
|
|
public:
|
2015-12-22 05:07:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-12-24 08:47:42 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
DefinedSynthetic(StringRef N, uintX_t Value,
|
|
|
|
OutputSectionBase<ELFT> &Section);
|
2015-09-26 02:56:53 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-12-22 04:47:33 +08:00
|
|
|
return S->kind() == SymbolBody::DefinedSyntheticKind;
|
2015-09-26 02:56:53 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
uintX_t Value;
|
2015-10-16 06:27:29 +08:00
|
|
|
const OutputSectionBase<ELFT> &Section;
|
2015-09-26 02:56:53 +08:00
|
|
|
};
|
|
|
|
|
2015-08-31 09:46:20 +08:00
|
|
|
// Undefined symbol.
|
2015-12-23 07:00:50 +08:00
|
|
|
class Undefined : public SymbolBody {
|
|
|
|
typedef SymbolBody::Kind Kind;
|
|
|
|
bool CanKeepUndefined;
|
2015-08-15 00:46:28 +08:00
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
protected:
|
|
|
|
Undefined(Kind K, StringRef N, bool IsWeak, uint8_t Visibility, bool IsTls);
|
2015-08-31 09:46:20 +08:00
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
public:
|
|
|
|
Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
|
|
|
|
bool CanKeepUndefined);
|
2015-08-15 00:46:28 +08:00
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
static bool classof(const SymbolBody *S) { return S->isUndefined(); }
|
2015-10-05 17:43:57 +08:00
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
bool canKeepUndefined() const { return CanKeepUndefined; }
|
2015-08-15 00:46:28 +08:00
|
|
|
};
|
|
|
|
|
2015-12-23 07:00:50 +08:00
|
|
|
template <class ELFT> class UndefinedElf : public Undefined {
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UndefinedElf(StringRef N, const Elf_Sym &Sym);
|
|
|
|
const Elf_Sym &Sym;
|
2015-12-23 09:06:39 +08:00
|
|
|
|
|
|
|
static bool classof(const SymbolBody *S) {
|
|
|
|
return S->kind() == SymbolBody::UndefinedElfKind;
|
|
|
|
}
|
2015-12-23 07:00:50 +08:00
|
|
|
};
|
2015-08-31 09:46:20 +08:00
|
|
|
|
2015-12-24 08:47:42 +08:00
|
|
|
template <class ELFT> class SharedSymbol : public DefinedElf<ELFT> {
|
2015-12-22 05:07:31 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
2015-10-29 00:48:58 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
|
2015-09-08 23:50:05 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool classof(const SymbolBody *S) {
|
2015-12-22 04:47:33 +08:00
|
|
|
return S->kind() == SymbolBody::SharedKind;
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 04:59:12 +08:00
|
|
|
SharedSymbol(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym)
|
2015-12-24 08:47:42 +08:00
|
|
|
: DefinedElf<ELFT>(SymbolBody::SharedKind, Name, Sym), File(F) {}
|
2015-10-12 04:59:12 +08:00
|
|
|
|
|
|
|
SharedFile<ELFT> *File;
|
2015-10-29 00:48:58 +08:00
|
|
|
|
2015-12-17 09:14:23 +08:00
|
|
|
// True if the linker has to generate a copy relocation for this shared
|
|
|
|
// symbol. OffsetInBSS is significant only when NeedsCopy is true.
|
|
|
|
bool NeedsCopy = false;
|
|
|
|
uintX_t OffsetInBSS = 0;
|
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)
|
2015-10-09 17:58:39 +08:00
|
|
|
: SymbolBody(LazyKind, S.getName(), false, llvm::ELF::STV_DEFAULT, false),
|
2015-09-05 06:28:10 +08:00
|
|
|
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();
|
|
|
|
|
2015-10-06 22:33:58 +08:00
|
|
|
void setWeak() { IsWeak = true; }
|
|
|
|
void setUsedInRegularObj() { IsUsedInRegularObj = true; }
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
private:
|
|
|
|
ArchiveFile *File;
|
|
|
|
const llvm::object::Archive::Symbol Sym;
|
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|