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"
|
2016-11-25 04:24:18 +08:00
|
|
|
#include "Strings.h"
|
2016-11-06 07:05:47 +08:00
|
|
|
#include "SyntheticSections.h"
|
2016-02-02 05:00:35 +08:00
|
|
|
#include "Target.h"
|
2016-12-20 01:01:01 +08:00
|
|
|
#include "Writer.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-10-12 16:19:30 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-23 12:57:25 +08:00
|
|
|
#include <cstring>
|
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
|
|
|
|
2017-04-05 18:03:25 +08:00
|
|
|
DefinedRegular *ElfSym::Bss;
|
2017-04-14 05:37:56 +08:00
|
|
|
DefinedRegular *ElfSym::Etext1;
|
2017-03-09 06:36:28 +08:00
|
|
|
DefinedRegular *ElfSym::Etext2;
|
2017-04-14 05:37:56 +08:00
|
|
|
DefinedRegular *ElfSym::Edata1;
|
2017-03-09 06:36:28 +08:00
|
|
|
DefinedRegular *ElfSym::Edata2;
|
2017-04-14 05:37:56 +08:00
|
|
|
DefinedRegular *ElfSym::End1;
|
2017-03-09 06:36:28 +08:00
|
|
|
DefinedRegular *ElfSym::End2;
|
2017-06-26 23:11:24 +08:00
|
|
|
DefinedRegular *ElfSym::GlobalOffsetTable;
|
2017-04-14 05:37:56 +08:00
|
|
|
DefinedRegular *ElfSym::MipsGp;
|
2017-03-01 03:29:55 +08:00
|
|
|
DefinedRegular *ElfSym::MipsGpDisp;
|
|
|
|
DefinedRegular *ElfSym::MipsLocalGp;
|
|
|
|
|
2017-03-17 19:56:54 +08:00
|
|
|
static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) {
|
2016-03-11 20:19:05 +08:00
|
|
|
switch (Body.kind()) {
|
|
|
|
case SymbolBody::DefinedRegularKind: {
|
2017-03-01 03:29:55 +08:00
|
|
|
auto &D = cast<DefinedRegular>(Body);
|
2017-03-09 06:36:28 +08:00
|
|
|
SectionBase *IS = D.Section;
|
|
|
|
if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
|
|
|
|
IS = ISB->Repl;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
// According to the ELF spec reference to a local symbol from outside
|
|
|
|
// the group are not allowed. Unfortunately .eh_frame breaks that rule
|
|
|
|
// and must be treated specially. For now we just replace the symbol with
|
|
|
|
// 0.
|
2017-02-24 00:49:07 +08:00
|
|
|
if (IS == &InputSection::Discarded)
|
2016-04-04 22:04:16 +08:00
|
|
|
return 0;
|
|
|
|
|
2016-02-02 05:00:35 +08:00
|
|
|
// This is an absolute symbol.
|
2016-12-15 08:57:53 +08:00
|
|
|
if (!IS)
|
2016-04-04 22:04:16 +08:00
|
|
|
return D.Value;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
2017-03-08 23:21:32 +08:00
|
|
|
uint64_t Offset = D.Value;
|
2017-02-28 16:32:56 +08:00
|
|
|
|
|
|
|
// An object in an SHF_MERGE section might be referenced via a
|
|
|
|
// section symbol (as a hack for reducing the number of local
|
|
|
|
// symbols).
|
2017-03-01 12:44:04 +08:00
|
|
|
// Depending on the addend, the reference via a section symbol
|
|
|
|
// refers to a different object in the merge section.
|
|
|
|
// Since the objects in the merge section are not necessarily
|
|
|
|
// contiguous in the output, the addend can thus affect the final
|
|
|
|
// VA in a non-linear way.
|
|
|
|
// To make this work, we incorporate the addend into the section
|
|
|
|
// offset (and zero out the addend for later processing) so that
|
|
|
|
// we find the right object in the section.
|
2016-04-04 22:04:16 +08:00
|
|
|
if (D.isSection()) {
|
2016-03-11 22:21:37 +08:00
|
|
|
Offset += Addend;
|
|
|
|
Addend = 0;
|
|
|
|
}
|
2017-02-28 16:32:56 +08:00
|
|
|
|
2017-03-09 00:08:36 +08:00
|
|
|
const OutputSection *OutSec = IS->getOutputSection();
|
2017-02-28 17:01:58 +08:00
|
|
|
|
|
|
|
// In the typical case, this is actually very simple and boils
|
|
|
|
// down to adding together 3 numbers:
|
|
|
|
// 1. The address of the output section.
|
|
|
|
// 2. The offset of the input section within the output section.
|
|
|
|
// 3. The offset within the input section (this addition happens
|
|
|
|
// inside InputSection::getOffset).
|
|
|
|
//
|
|
|
|
// If you understand the data structures involved with this next
|
|
|
|
// line (and how they get built), then you have a pretty good
|
|
|
|
// understanding of the linker.
|
2017-03-09 00:03:41 +08:00
|
|
|
uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
|
2017-02-28 17:01:58 +08:00
|
|
|
|
2016-10-04 16:52:51 +08:00
|
|
|
if (D.isTls() && !Config->Relocatable) {
|
2017-02-27 10:31:26 +08:00
|
|
|
if (!Out::TlsPhdr)
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(D.File) +
|
2016-10-04 16:52:51 +08:00
|
|
|
" has a STT_TLS symbol but doesn't have a PT_TLS section");
|
2017-02-27 10:31:26 +08:00
|
|
|
return VA - Out::TlsPhdr->p_vaddr;
|
2016-10-04 16:52:51 +08:00
|
|
|
}
|
2016-03-11 22:21:37 +08:00
|
|
|
return VA;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
2016-08-02 09:35:13 +08:00
|
|
|
case SymbolBody::DefinedCommonKind:
|
2017-01-24 11:41:20 +08:00
|
|
|
if (!Config->DefineCommon)
|
|
|
|
return 0;
|
2017-06-01 04:17:44 +08:00
|
|
|
return InX::Common->getParent()->Addr + InX::Common->OutSecOff +
|
2016-08-31 21:28:33 +08:00
|
|
|
cast<DefinedCommon>(Body).Offset;
|
2017-03-09 01:24:24 +08:00
|
|
|
case SymbolBody::SharedKind: {
|
|
|
|
auto &SS = cast<SharedSymbol>(Body);
|
|
|
|
if (SS.NeedsCopy)
|
2017-06-01 04:17:44 +08:00
|
|
|
return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff +
|
2017-03-17 18:14:53 +08:00
|
|
|
SS.CopyRelSecOff;
|
2017-03-09 01:24:24 +08:00
|
|
|
if (SS.NeedsPltAddr)
|
2017-03-17 19:56:54 +08:00
|
|
|
return Body.getPltVA();
|
2017-02-16 14:12:22 +08:00
|
|
|
return 0;
|
2017-03-09 01:24:24 +08:00
|
|
|
}
|
2016-04-27 08:05:06 +08:00
|
|
|
case SymbolBody::UndefinedKind:
|
2016-02-02 05:00:35 +08:00
|
|
|
return 0;
|
2016-04-08 03:24:51 +08:00
|
|
|
case SymbolBody::LazyArchiveKind:
|
|
|
|
case SymbolBody::LazyObjectKind:
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
|
2016-02-02 05:00:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-12 16:31:34 +08:00
|
|
|
llvm_unreachable("invalid symbol kind");
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2016-11-30 02:05:04 +08:00
|
|
|
SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
|
|
|
|
uint8_t Type)
|
2017-03-09 01:24:24 +08:00
|
|
|
: SymbolKind(K), NeedsCopy(false), NeedsPltAddr(false), IsLocal(IsLocal),
|
2016-12-08 20:58:55 +08:00
|
|
|
IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
|
2017-02-16 14:12:22 +08:00
|
|
|
IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {}
|
2016-04-06 21:22:41 +08:00
|
|
|
|
2016-03-14 03:48:18 +08:00
|
|
|
// Returns true if a symbol can be replaced at load-time by a symbol
|
|
|
|
// with the same name defined in other ELF executable or DSO.
|
|
|
|
bool SymbolBody::isPreemptible() const {
|
|
|
|
if (isLocal())
|
|
|
|
return false;
|
|
|
|
|
2016-05-06 03:41:49 +08:00
|
|
|
// Shared symbols resolve to the definition in the DSO. The exceptions are
|
2017-03-09 01:24:24 +08:00
|
|
|
// symbols with copy relocations (which resolve to .bss) or preempt plt
|
|
|
|
// entries (which resolve to that plt entry).
|
2016-03-14 03:48:18 +08:00
|
|
|
if (isShared())
|
2017-03-09 01:24:24 +08:00
|
|
|
return !NeedsCopy && !NeedsPltAddr;
|
2016-03-14 03:48:18 +08:00
|
|
|
|
2016-04-23 04:21:26 +08:00
|
|
|
// That's all that can be preempted in a non-DSO.
|
|
|
|
if (!Config->Shared)
|
2016-04-15 19:57:07 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-24 12:29:59 +08:00
|
|
|
// Only symbols that appear in dynsym can be preempted.
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
if (!symbol()->includeInDynsym())
|
2016-03-14 03:48:18 +08:00
|
|
|
return false;
|
2016-04-23 04:21:26 +08:00
|
|
|
|
2016-07-08 06:50:54 +08:00
|
|
|
// Only default visibility symbols can be preempted.
|
|
|
|
if (symbol()->Visibility != STV_DEFAULT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// -Bsymbolic means that definitions are not preempted.
|
2016-04-24 12:29:59 +08:00
|
|
|
if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
|
|
|
|
return !isDefined();
|
2016-07-08 06:50:54 +08:00
|
|
|
return true;
|
2016-03-14 03:48:18 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 03:43:02 +08:00
|
|
|
// Overwrites all attributes except symbol name with Other's so that
|
|
|
|
// this symbol becomes an alias to Other. This is useful for handling
|
|
|
|
// some options such as --wrap.
|
|
|
|
//
|
|
|
|
// The reason why we want to keep the symbol name is because, if we
|
|
|
|
// copy symbol names, we'll end up having symbol tables in resulting
|
|
|
|
// executables or DSOs containing two or more identical symbols, which
|
|
|
|
// is just inconvenient.
|
|
|
|
void SymbolBody::copy(SymbolBody *Other) {
|
|
|
|
StringRef S = Name;
|
|
|
|
memcpy(symbol()->Body.buffer, Other->symbol()->Body.buffer,
|
|
|
|
sizeof(Symbol::Body));
|
|
|
|
Name = S;
|
|
|
|
}
|
|
|
|
|
2017-03-17 19:56:54 +08:00
|
|
|
uint64_t SymbolBody::getVA(int64_t Addend) const {
|
|
|
|
uint64_t OutVA = getSymVA(*this, Addend);
|
2016-03-18 07:36:19 +08:00
|
|
|
return OutVA + Addend;
|
2016-03-11 20:19:05 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 07:28:49 +08:00
|
|
|
uint64_t SymbolBody::getGotVA() const {
|
2017-05-12 07:26:03 +08:00
|
|
|
return InX::Got->getVA() + getGotOffset();
|
2016-04-07 23:20:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:12:51 +08:00
|
|
|
uint64_t SymbolBody::getGotOffset() const {
|
2016-07-14 02:55:14 +08:00
|
|
|
return GotIndex * Target->GotEntrySize;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:58:11 +08:00
|
|
|
uint64_t SymbolBody::getGotPltVA() const {
|
2016-12-08 20:58:55 +08:00
|
|
|
if (this->IsInIgot)
|
2017-03-16 20:58:11 +08:00
|
|
|
return InX::IgotPlt->getVA() + getGotPltOffset();
|
|
|
|
return InX::GotPlt->getVA() + getGotPltOffset();
|
2016-04-07 23:20:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:58:11 +08:00
|
|
|
uint64_t SymbolBody::getGotPltOffset() const {
|
2016-07-14 02:55:14 +08:00
|
|
|
return GotPltIndex * Target->GotPltEntrySize;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 19:56:54 +08:00
|
|
|
uint64_t SymbolBody::getPltVA() const {
|
2016-12-08 20:58:55 +08:00
|
|
|
if (this->IsInIplt)
|
2017-03-17 19:56:54 +08:00
|
|
|
return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
|
|
|
|
return InX::Plt->getVA() + Target->PltHeaderSize +
|
2016-02-02 05:00:35 +08:00
|
|
|
PltIndex * Target->PltEntrySize;
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:16:09 +08:00
|
|
|
template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
|
2016-08-31 21:28:33 +08:00
|
|
|
if (const auto *C = dyn_cast<DefinedCommon>(this))
|
2016-04-04 22:04:16 +08:00
|
|
|
return C->Size;
|
2017-03-01 03:29:55 +08:00
|
|
|
if (const auto *DR = dyn_cast<DefinedRegular>(this))
|
2016-04-04 22:04:16 +08:00
|
|
|
return DR->Size;
|
2017-02-27 07:35:34 +08:00
|
|
|
if (const auto *S = dyn_cast<SharedSymbol>(this))
|
|
|
|
return S->getSize<ELFT>();
|
2016-02-03 08:12:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-16 19:06:13 +08:00
|
|
|
OutputSection *SymbolBody::getOutputSection() const {
|
2017-03-01 03:29:55 +08:00
|
|
|
if (auto *S = dyn_cast<DefinedRegular>(this)) {
|
2017-02-28 12:02:42 +08:00
|
|
|
if (S->Section)
|
2017-03-09 00:08:36 +08:00
|
|
|
return S->Section->getOutputSection();
|
2017-02-28 12:02:42 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-09 01:24:24 +08:00
|
|
|
if (auto *S = dyn_cast<SharedSymbol>(this)) {
|
|
|
|
if (S->NeedsCopy)
|
2017-06-01 04:17:44 +08:00
|
|
|
return S->CopyRelSec->getParent();
|
2017-02-28 12:02:42 +08:00
|
|
|
return nullptr;
|
2017-03-09 01:24:24 +08:00
|
|
|
}
|
2017-02-28 12:02:42 +08:00
|
|
|
|
|
|
|
if (isa<DefinedCommon>(this)) {
|
|
|
|
if (Config->DefineCommon)
|
2017-06-01 04:17:44 +08:00
|
|
|
return InX::Common->getParent();
|
2017-02-28 12:02:42 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:48:40 +08:00
|
|
|
// If a symbol name contains '@', the characters after that is
|
|
|
|
// a symbol version name. This function parses that.
|
|
|
|
void SymbolBody::parseSymbolVersion() {
|
|
|
|
StringRef S = getName();
|
|
|
|
size_t Pos = S.find('@');
|
|
|
|
if (Pos == 0 || Pos == StringRef::npos)
|
|
|
|
return;
|
|
|
|
StringRef Verstr = S.substr(Pos + 1);
|
|
|
|
if (Verstr.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Truncate the symbol name so that it doesn't include the version string.
|
2016-11-30 02:05:04 +08:00
|
|
|
Name = {S.data(), Pos};
|
2016-11-23 13:48:40 +08:00
|
|
|
|
2017-01-18 00:08:06 +08:00
|
|
|
// If this is not in this DSO, it is not a definition.
|
|
|
|
if (!isInCurrentDSO())
|
2017-01-07 06:30:35 +08:00
|
|
|
return;
|
|
|
|
|
2016-11-23 13:48:40 +08:00
|
|
|
// '@@' in a symbol name means the default version.
|
|
|
|
// It is usually the most recent one.
|
|
|
|
bool IsDefault = (Verstr[0] == '@');
|
|
|
|
if (IsDefault)
|
|
|
|
Verstr = Verstr.substr(1);
|
|
|
|
|
|
|
|
for (VersionDefinition &Ver : Config->VersionDefinitions) {
|
|
|
|
if (Ver.Name != Verstr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (IsDefault)
|
|
|
|
symbol()->VersionId = Ver.Id;
|
|
|
|
else
|
|
|
|
symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is an error if the specified version is not defined.
|
2016-12-06 02:40:14 +08:00
|
|
|
error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
|
2016-11-23 13:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-11-30 02:05:04 +08:00
|
|
|
Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
|
|
|
|
uint8_t Type)
|
|
|
|
: SymbolBody(K, Name, IsLocal, StOther, Type) {}
|
2015-12-24 08:47:42 +08:00
|
|
|
|
2017-03-01 03:29:55 +08:00
|
|
|
template <class ELFT> bool DefinedRegular::isMipsPIC() const {
|
2017-06-10 05:09:08 +08:00
|
|
|
typedef typename ELFT::Ehdr Elf_Ehdr;
|
2016-09-29 20:58:36 +08:00
|
|
|
if (!Section || !isFunc())
|
|
|
|
return false;
|
2017-06-10 05:09:08 +08:00
|
|
|
|
|
|
|
auto *Sec = cast<InputSectionBase>(Section);
|
|
|
|
const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader();
|
2016-09-29 20:58:36 +08:00
|
|
|
return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
|
2017-06-10 05:09:08 +08:00
|
|
|
(Hdr->e_flags & EF_MIPS_PIC);
|
2016-09-29 20:58:36 +08:00
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
|
|
|
|
uint8_t Type, InputFile *File)
|
2016-11-30 02:05:04 +08:00
|
|
|
: SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
|
2016-07-17 11:11:46 +08:00
|
|
|
this->File = File;
|
|
|
|
}
|
2015-12-23 07:00:50 +08:00
|
|
|
|
2017-03-09 03:35:29 +08:00
|
|
|
DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
|
2016-08-31 21:28:33 +08:00
|
|
|
uint8_t StOther, uint8_t Type, InputFile *File)
|
2016-11-30 02:05:04 +08:00
|
|
|
: Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
|
|
|
|
Type),
|
2016-07-18 01:36:22 +08:00
|
|
|
Alignment(Alignment), Size(Size) {
|
|
|
|
this->File = File;
|
|
|
|
}
|
2015-12-25 00:23:37 +08:00
|
|
|
|
2017-02-27 07:35:34 +08:00
|
|
|
// If a shared symbol is referred via a copy relocation, its alignment
|
|
|
|
// becomes part of the ABI. This function returns a symbol alignment.
|
|
|
|
// Because symbols don't have alignment attributes, we need to infer that.
|
2017-03-09 03:35:29 +08:00
|
|
|
template <class ELFT> uint32_t SharedSymbol::getAlignment() const {
|
2017-02-27 07:35:34 +08:00
|
|
|
auto *File = cast<SharedFile<ELFT>>(this->File);
|
2017-03-09 03:35:29 +08:00
|
|
|
uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign;
|
2017-02-27 07:35:34 +08:00
|
|
|
uint64_t SymValue = getSym<ELFT>().st_value;
|
2017-03-09 03:35:29 +08:00
|
|
|
uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue);
|
2017-02-27 07:35:34 +08:00
|
|
|
return std::min(SecAlign, SymAlign);
|
|
|
|
}
|
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
InputFile *Lazy::fetch() {
|
2016-04-08 03:24:51 +08:00
|
|
|
if (auto *S = dyn_cast<LazyArchive>(this))
|
2016-10-29 04:57:25 +08:00
|
|
|
return S->fetch();
|
|
|
|
return cast<LazyObject>(this)->fetch();
|
2016-04-08 03:24:51 +08:00
|
|
|
}
|
|
|
|
|
2016-07-17 11:11:46 +08:00
|
|
|
LazyArchive::LazyArchive(ArchiveFile &File,
|
|
|
|
const llvm::object::Archive::Symbol S, uint8_t Type)
|
|
|
|
: Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
|
|
|
|
this->File = &File;
|
|
|
|
}
|
|
|
|
|
|
|
|
LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
|
|
|
|
: Lazy(LazyObjectKind, Name, Type) {
|
|
|
|
this->File = &File;
|
|
|
|
}
|
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
InputFile *LazyArchive::fetch() {
|
2016-10-13 03:35:54 +08:00
|
|
|
std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
// getMember returns an empty buffer if the member was already
|
|
|
|
// read from the library.
|
2016-10-13 03:35:54 +08:00
|
|
|
if (MBInfo.first.getBuffer().empty())
|
2016-09-14 08:05:51 +08:00
|
|
|
return nullptr;
|
2016-10-29 04:57:25 +08:00
|
|
|
return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 22:54:48 +08:00
|
|
|
InputFile *LazyObject::fetch() { return file()->fetch(); }
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2017-01-11 01:08:13 +08:00
|
|
|
uint8_t Symbol::computeBinding() const {
|
|
|
|
if (Config->Relocatable)
|
|
|
|
return Binding;
|
2016-04-23 02:42:48 +08:00
|
|
|
if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
|
2017-01-11 01:08:13 +08:00
|
|
|
return STB_LOCAL;
|
2017-04-25 07:50:58 +08:00
|
|
|
if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO())
|
2017-01-11 01:08:13 +08:00
|
|
|
return STB_LOCAL;
|
|
|
|
if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
|
|
|
|
return STB_GLOBAL;
|
|
|
|
return Binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::includeInDynsym() const {
|
|
|
|
if (computeBinding() == STB_LOCAL)
|
2016-04-22 05:44:25 +08:00
|
|
|
return false;
|
2017-01-11 01:08:13 +08:00
|
|
|
return ExportDynamic || body()->isShared() ||
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
(body()->isUndefined() && Config->Shared);
|
2016-04-22 04:35:25 +08:00
|
|
|
}
|
2016-07-18 01:50:09 +08:00
|
|
|
|
|
|
|
// Print out a log message for --trace-symbol.
|
|
|
|
void elf::printTraceSymbol(Symbol *Sym) {
|
|
|
|
SymbolBody *B = Sym->body();
|
2017-02-22 07:22:56 +08:00
|
|
|
std::string S;
|
2016-07-18 01:50:09 +08:00
|
|
|
if (B->isUndefined())
|
2017-02-22 07:22:56 +08:00
|
|
|
S = ": reference to ";
|
2016-07-18 01:50:09 +08:00
|
|
|
else if (B->isCommon())
|
2017-02-22 07:22:56 +08:00
|
|
|
S = ": common definition of ";
|
2016-07-18 01:50:09 +08:00
|
|
|
else
|
2017-02-22 07:22:56 +08:00
|
|
|
S = ": definition of ";
|
|
|
|
|
|
|
|
message(toString(B->File) + S + B->getName());
|
2016-07-18 01:50:09 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 04:24:18 +08:00
|
|
|
// Returns a symbol for an error message.
|
2017-01-06 18:04:08 +08:00
|
|
|
std::string lld::toString(const SymbolBody &B) {
|
2016-11-25 04:24:18 +08:00
|
|
|
if (Config->Demangle)
|
2016-12-08 07:17:05 +08:00
|
|
|
if (Optional<std::string> S = demangle(B.getName()))
|
|
|
|
return *S;
|
2016-11-25 04:24:18 +08:00
|
|
|
return B.getName();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2017-03-01 03:29:55 +08:00
|
|
|
template bool DefinedRegular::template isMipsPIC<ELF32LE>() const;
|
|
|
|
template bool DefinedRegular::template isMipsPIC<ELF32BE>() const;
|
|
|
|
template bool DefinedRegular::template isMipsPIC<ELF64LE>() const;
|
|
|
|
template bool DefinedRegular::template isMipsPIC<ELF64BE>() const;
|
2017-02-27 07:35:34 +08:00
|
|
|
|
2017-03-09 03:35:29 +08:00
|
|
|
template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const;
|
|
|
|
template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const;
|
|
|
|
template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const;
|
|
|
|
template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const;
|