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-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-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
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-28 10:15:26 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
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-11-06 12:35:31 +08:00
|
|
|
Defined *ElfSym::Bss;
|
|
|
|
Defined *ElfSym::Etext1;
|
|
|
|
Defined *ElfSym::Etext2;
|
|
|
|
Defined *ElfSym::Edata1;
|
|
|
|
Defined *ElfSym::Edata2;
|
|
|
|
Defined *ElfSym::End1;
|
|
|
|
Defined *ElfSym::End2;
|
|
|
|
Defined *ElfSym::GlobalOffsetTable;
|
|
|
|
Defined *ElfSym::MipsGp;
|
|
|
|
Defined *ElfSym::MipsGpDisp;
|
|
|
|
Defined *ElfSym::MipsLocalGp;
|
2017-03-01 03:29:55 +08:00
|
|
|
|
2017-11-04 08:31:04 +08:00
|
|
|
static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
|
|
|
|
switch (Sym.kind()) {
|
2017-11-06 12:35:31 +08:00
|
|
|
case Symbol::DefinedKind: {
|
|
|
|
auto &D = cast<Defined>(Sym);
|
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)
|
2017-11-30 06:47:35 +08:00
|
|
|
fatal(toString(D.File) +
|
2017-07-14 08:22:46 +08:00
|
|
|
" has an STT_TLS symbol but doesn't have an SHF_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
|
|
|
}
|
2017-11-04 05:21:47 +08:00
|
|
|
case Symbol::SharedKind: {
|
2017-11-04 08:31:04 +08:00
|
|
|
auto &SS = cast<SharedSymbol>(Sym);
|
2017-08-05 01:43:54 +08:00
|
|
|
if (SS.CopyRelSec)
|
2017-09-14 00:59:12 +08:00
|
|
|
return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff;
|
2017-03-09 01:24:24 +08:00
|
|
|
if (SS.NeedsPltAddr)
|
2017-11-04 08:31:04 +08:00
|
|
|
return Sym.getPltVA();
|
2017-02-16 14:12:22 +08:00
|
|
|
return 0;
|
2017-03-09 01:24:24 +08:00
|
|
|
}
|
2017-11-04 05:21:47 +08:00
|
|
|
case Symbol::UndefinedKind:
|
2016-02-02 05:00:35 +08:00
|
|
|
return 0;
|
2017-11-04 05:21:47 +08:00
|
|
|
case Symbol::LazyArchiveKind:
|
|
|
|
case Symbol::LazyObjectKind:
|
2017-11-04 08:31:04 +08:00
|
|
|
assert(Sym.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
|
|
|
}
|
|
|
|
|
2017-10-13 10:57:59 +08:00
|
|
|
// Returns true if this is a weak undefined symbol.
|
2017-11-04 05:21:47 +08:00
|
|
|
bool Symbol::isUndefWeak() const {
|
2017-10-25 00:27:31 +08:00
|
|
|
// See comment on Lazy in Symbols.h for the details.
|
2017-11-30 09:08:09 +08:00
|
|
|
return isWeak() && (isUndefined() || isLazy());
|
2017-09-14 04:43:04 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::getVA(int64_t Addend) const {
|
2017-03-17 19:56:54 +08:00
|
|
|
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-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
|
2016-04-07 23:20:56 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::getGotOffset() const {
|
2016-07-14 02:55:14 +08:00
|
|
|
return GotIndex * Target->GotEntrySize;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::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-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::getGotPltOffset() const {
|
2016-07-14 02:55:14 +08:00
|
|
|
return GotPltIndex * Target->GotPltEntrySize;
|
2016-02-02 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::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;
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint64_t Symbol::getSize() const {
|
2017-11-06 12:35:31 +08:00
|
|
|
if (const auto *DR = dyn_cast<Defined>(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))
|
2017-10-29 04:15:56 +08:00
|
|
|
return S->Size;
|
2016-02-03 08:12:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
OutputSection *Symbol::getOutputSection() const {
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *S = dyn_cast<Defined>(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)) {
|
2017-08-05 01:43:54 +08:00
|
|
|
if (S->CopyRelSec)
|
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
|
|
|
|
|
|
|
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.
|
2017-11-04 05:21:47 +08:00
|
|
|
void Symbol::parseSymbolVersion() {
|
2016-11-23 13:48:40 +08:00
|
|
|
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.
|
2017-11-06 12:39:07 +08:00
|
|
|
if (!isDefined())
|
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)
|
2017-11-01 00:07:41 +08:00
|
|
|
VersionId = Ver.Id;
|
2016-11-23 13:48:40 +08:00
|
|
|
else
|
2017-11-01 00:07:41 +08:00
|
|
|
VersionId = Ver.Id | VERSYM_HIDDEN;
|
2016-11-23 13:48:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is an error if the specified version is not defined.
|
2017-07-04 21:19:13 +08:00
|
|
|
// Usually version script is not provided when linking executable,
|
|
|
|
// but we may still want to override a versioned symbol from DSO,
|
|
|
|
// so we do not report error in this case.
|
|
|
|
if (Config->Shared)
|
2017-11-30 06:47:35 +08:00
|
|
|
error(toString(File) + ": symbol " + S + " has undefined version " +
|
2017-07-04 21:19:13 +08:00
|
|
|
Verstr);
|
2016-11-23 13:48:40 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-30 06:47:35 +08:00
|
|
|
ArchiveFile *LazyArchive::getFile() { return cast<ArchiveFile>(File); }
|
2016-07-17 11:11:46 +08:00
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
InputFile *LazyArchive::fetch() {
|
2017-08-05 06:31:42 +08:00
|
|
|
std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile()->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;
|
2017-08-05 06:31:42 +08:00
|
|
|
return createObjectFile(MBInfo.first, getFile()->getName(), MBInfo.second);
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:47:35 +08:00
|
|
|
LazyObjFile *LazyObject::getFile() { return cast<LazyObjFile>(File); }
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2017-08-05 06:31:42 +08:00
|
|
|
InputFile *LazyObject::fetch() { return getFile()->fetch(); }
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
uint8_t Symbol::computeBinding() const {
|
2017-01-11 01:08:13 +08:00
|
|
|
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-11-06 12:39:07 +08:00
|
|
|
if (VersionId == VER_NDX_LOCAL && isDefined())
|
2017-01-11 01:08:13 +08:00
|
|
|
return STB_LOCAL;
|
|
|
|
if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
|
|
|
|
return STB_GLOBAL;
|
|
|
|
return Binding;
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
bool Symbol::includeInDynsym() const {
|
2017-09-16 02:05:02 +08:00
|
|
|
if (!Config->HasDynSymTab)
|
|
|
|
return false;
|
2017-01-11 01:08:13 +08:00
|
|
|
if (computeBinding() == STB_LOCAL)
|
2016-04-22 05:44:25 +08:00
|
|
|
return false;
|
2017-11-06 12:39:07 +08:00
|
|
|
if (!isDefined())
|
2017-09-14 04:43:04 +08:00
|
|
|
return true;
|
2017-09-14 04:47:53 +08:00
|
|
|
return ExportDynamic;
|
2016-04-22 04:35:25 +08:00
|
|
|
}
|
2016-07-18 01:50:09 +08:00
|
|
|
|
|
|
|
// Print out a log message for --trace-symbol.
|
2017-11-04 05:21:47 +08:00
|
|
|
void elf::printTraceSymbol(Symbol *Sym) {
|
2017-02-22 07:22:56 +08:00
|
|
|
std::string S;
|
2017-11-01 00:07:41 +08:00
|
|
|
if (Sym->isUndefined())
|
2017-02-22 07:22:56 +08:00
|
|
|
S = ": reference to ";
|
2017-11-01 00:07:41 +08:00
|
|
|
else if (Sym->isLazy())
|
2017-10-28 02:30:11 +08:00
|
|
|
S = ": lazy definition of ";
|
2017-11-01 00:07:41 +08:00
|
|
|
else if (Sym->isShared())
|
2017-10-28 02:30:11 +08:00
|
|
|
S = ": shared definition of ";
|
2017-11-06 12:35:31 +08:00
|
|
|
else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
|
2017-11-06 12:33:58 +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 ";
|
|
|
|
|
2017-11-01 00:07:41 +08:00
|
|
|
message(toString(Sym->File) + S + Sym->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-11-04 05:21:47 +08:00
|
|
|
std::string lld::toString(const Symbol &B) {
|
2016-11-25 04:24:18 +08:00
|
|
|
if (Config->Demangle)
|
2017-11-28 10:15:26 +08:00
|
|
|
if (Optional<std::string> S = demangleItanium(B.getName()))
|
2016-12-08 07:17:05 +08:00
|
|
|
return *S;
|
2016-11-25 04:24:18 +08:00
|
|
|
return B.getName();
|
|
|
|
}
|