2015-07-25 05:03:07 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2016-02-11 23:24:48 +08:00
|
|
|
#include "InputSection.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-02 23:43:50 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
using namespace llvm;
|
2015-07-25 05:03:07 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-09-04 04:03:54 +08:00
|
|
|
using namespace llvm::object;
|
2015-10-01 01:06:09 +08:00
|
|
|
using namespace llvm::sys::fs;
|
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
|
|
|
|
2016-03-03 14:22:29 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
|
2015-07-25 05:03:07 +08:00
|
|
|
std::error_code EC;
|
2016-03-03 14:22:29 +08:00
|
|
|
ELFFile<ELFT> F(MB.getBuffer(), EC);
|
2016-03-04 06:24:39 +08:00
|
|
|
check(EC);
|
2016-03-03 14:22:29 +08:00
|
|
|
return F;
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
2016-03-03 14:22:29 +08:00
|
|
|
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
|
|
|
|
: InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
|
2015-09-24 23:11:50 +08:00
|
|
|
|
2015-11-20 10:10:52 +08:00
|
|
|
template <class ELFT>
|
|
|
|
ELFKind ELFFileBase<ELFT>::getELFKind() {
|
2016-01-06 08:09:41 +08:00
|
|
|
if (ELFT::TargetEndianness == support::little)
|
|
|
|
return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
|
|
|
|
return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
|
2015-11-20 10:10:52 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:50:05 +08:00
|
|
|
template <class ELFT>
|
2015-10-12 09:55:32 +08:00
|
|
|
typename ELFFileBase<ELFT>::Elf_Sym_Range
|
|
|
|
ELFFileBase<ELFT>::getSymbolsHelper(bool Local) {
|
2015-09-08 23:50:05 +08:00
|
|
|
if (!Symtab)
|
|
|
|
return Elf_Sym_Range(nullptr, nullptr);
|
2015-09-24 23:11:50 +08:00
|
|
|
Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
|
2015-09-17 04:45:57 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
|
|
|
uint32_t FirstNonLocal = Symtab->sh_info;
|
|
|
|
if (FirstNonLocal > NumSymbols)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid sh_info in symbol table");
|
2015-09-17 04:45:57 +08:00
|
|
|
if (!Local)
|
2015-09-30 10:37:51 +08:00
|
|
|
return make_range(Syms.begin() + FirstNonLocal, Syms.end());
|
|
|
|
// +1 to skip over dummy symbol.
|
|
|
|
return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
|
2015-09-17 04:45:57 +08:00
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-11-03 22:13:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
|
2015-12-24 16:36:56 +08:00
|
|
|
uint32_t I = Sym.st_shndx;
|
|
|
|
if (I == ELF::SHN_XINDEX)
|
2016-01-06 09:14:11 +08:00
|
|
|
return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
|
2016-03-09 22:31:18 +08:00
|
|
|
if (I >= ELF::SHN_LORESERVE)
|
2015-11-03 22:13:40 +08:00
|
|
|
return 0;
|
2015-12-24 16:36:56 +08:00
|
|
|
return I;
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
|
2015-10-02 04:26:37 +08:00
|
|
|
if (!Symtab)
|
|
|
|
return;
|
2016-03-04 06:24:39 +08:00
|
|
|
StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-12 09:55:32 +08:00
|
|
|
typename ELFFileBase<ELFT>::Elf_Sym_Range
|
|
|
|
ELFFileBase<ELFT>::getNonLocalSymbols() {
|
2015-09-17 04:45:57 +08:00
|
|
|
return getSymbolsHelper(false);
|
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
|
2015-10-13 09:17:02 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::ObjectKind, M) {}
|
2015-09-24 23:11:50 +08:00
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
typename elf::ObjectFile<ELFT>::Elf_Sym_Range
|
|
|
|
elf::ObjectFile<ELFT>::getLocalSymbols() {
|
2015-09-17 04:45:57 +08:00
|
|
|
return this->getSymbolsHelper(true);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
|
2016-01-07 06:42:43 +08:00
|
|
|
if (MipsReginfo)
|
|
|
|
return MipsReginfo->Reginfo->ri_gp_value;
|
|
|
|
return 0;
|
2015-12-25 21:02:13 +08:00
|
|
|
}
|
|
|
|
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
const typename elf::ObjectFile<ELFT>::Elf_Sym *
|
|
|
|
elf::ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) {
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
|
|
|
if (SymIndex >= FirstNonLocal)
|
|
|
|
return nullptr;
|
|
|
|
Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab);
|
|
|
|
return Syms.begin() + SymIndex;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Read section and symbol tables.
|
2016-01-06 10:06:33 +08:00
|
|
|
initializeSections(ComdatGroups);
|
2015-07-25 05:03:07 +08:00
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
// Sections with SHT_GROUP and comdat bits define comdat section groups.
|
|
|
|
// They are identified and deduplicated by group name. This function
|
|
|
|
// returns a group name.
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
|
2015-10-10 03:25:07 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
uint32_t SymtabdSectionIndex = Sec.sh_link;
|
2016-03-04 06:24:39 +08:00
|
|
|
const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
|
2015-10-10 03:25:07 +08:00
|
|
|
uint32_t SymIndex = Sec.sh_info;
|
|
|
|
const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
|
|
|
|
return check(Sym->getName(StringTable));
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
ArrayRef<typename elf::ObjectFile<ELFT>::uint32_X>
|
|
|
|
elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
|
2015-10-10 03:25:07 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
2016-03-04 00:21:44 +08:00
|
|
|
ArrayRef<uint32_X> Entries =
|
2016-03-04 06:24:39 +08:00
|
|
|
check(Obj.template getSectionContentsAsArray<uint32_X>(&Sec));
|
2015-10-10 03:25:07 +08:00
|
|
|
if (Entries.empty() || Entries[0] != GRP_COMDAT)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Unsupported SHT_GROUP format");
|
2015-10-10 03:25:07 +08:00
|
|
|
return Entries.slice(1);
|
|
|
|
}
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
uintX_t Flags = Sec.sh_flags;
|
|
|
|
if (!(Flags & SHF_MERGE))
|
|
|
|
return false;
|
|
|
|
if (Flags & SHF_WRITE)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Writable SHF_MERGE sections are not supported");
|
2015-10-25 06:51:01 +08:00
|
|
|
uintX_t EntSize = Sec.sh_entsize;
|
2015-11-09 16:40:44 +08:00
|
|
|
if (!EntSize || Sec.sh_size % EntSize)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("SHF_MERGE section size must be a multiple of sh_entsize");
|
2015-10-25 06:51:01 +08:00
|
|
|
|
2016-02-19 22:17:40 +08:00
|
|
|
// Don't try to merge if the aligment is larger than the sh_entsize and this
|
|
|
|
// is not SHF_STRINGS.
|
2015-10-25 06:51:01 +08:00
|
|
|
//
|
2016-02-19 22:17:40 +08:00
|
|
|
// Since this is not a SHF_STRINGS, we would need to pad after every entity.
|
|
|
|
// It would be equivalent for the producer of the .o to just set a larger
|
2015-10-25 06:51:01 +08:00
|
|
|
// sh_entsize.
|
2016-02-19 22:17:40 +08:00
|
|
|
if (Flags & SHF_STRINGS)
|
|
|
|
return true;
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
if (Sec.sh_addralign > EntSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
void elf::ObjectFile<ELFT>::initializeSections(
|
2016-02-13 05:17:10 +08:00
|
|
|
DenseSet<StringRef> &ComdatGroups) {
|
2015-09-24 23:11:50 +08:00
|
|
|
uint64_t Size = this->ELFObj.getNumSections();
|
2015-09-22 08:16:19 +08:00
|
|
|
Sections.resize(Size);
|
2015-10-10 03:25:07 +08:00
|
|
|
unsigned I = -1;
|
2015-10-08 20:02:38 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
2015-10-10 03:25:07 +08:00
|
|
|
++I;
|
2016-02-25 02:33:35 +08:00
|
|
|
if (Sections[I] == InputSection<ELFT>::Discarded)
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
|
|
|
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (Sec.sh_type) {
|
2015-10-10 03:25:07 +08:00
|
|
|
case SHT_GROUP:
|
2016-02-25 02:33:35 +08:00
|
|
|
Sections[I] = InputSection<ELFT>::Discarded;
|
2016-01-06 10:06:33 +08:00
|
|
|
if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
2016-01-07 04:30:02 +08:00
|
|
|
for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
|
2015-10-10 03:25:07 +08:00
|
|
|
if (SecIndex >= Size)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid section index in group");
|
2016-02-25 02:33:35 +08:00
|
|
|
Sections[SecIndex] = InputSection<ELFT>::Discarded;
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_SYMTAB:
|
2015-09-08 23:50:05 +08:00
|
|
|
this->Symtab = &Sec;
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2016-03-04 00:21:44 +08:00
|
|
|
case SHT_SYMTAB_SHNDX:
|
2016-03-04 06:24:39 +08:00
|
|
|
this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
|
2015-08-25 05:43:25 +08:00
|
|
|
break;
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_STRTAB:
|
|
|
|
case SHT_NULL:
|
2015-08-28 07:15:56 +08:00
|
|
|
break;
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_RELA:
|
2015-08-28 07:15:56 +08:00
|
|
|
case SHT_REL: {
|
|
|
|
uint32_t RelocatedSectionIndex = Sec.sh_info;
|
|
|
|
if (RelocatedSectionIndex >= Size)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid relocated section index");
|
2015-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *RelocatedSection =
|
|
|
|
Sections[RelocatedSectionIndex];
|
2016-02-05 05:41:07 +08:00
|
|
|
// Strictly speaking, a relocation section must be included in the
|
|
|
|
// group of the section it relocates. However, LLVM 3.3 and earlier
|
|
|
|
// would fail to do so, so we gracefully handle that case.
|
2016-02-25 02:33:35 +08:00
|
|
|
if (RelocatedSection == InputSection<ELFT>::Discarded)
|
2016-02-05 05:41:07 +08:00
|
|
|
continue;
|
2015-08-28 07:15:56 +08:00
|
|
|
if (!RelocatedSection)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Unsupported relocation reference");
|
2016-02-25 16:23:37 +08:00
|
|
|
if (Config->Relocatable) {
|
|
|
|
// For -r, relocation sections are handled as regular input sections.
|
|
|
|
Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
|
|
|
|
} else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
|
2015-10-20 05:00:02 +08:00
|
|
|
S->RelocSections.push_back(&Sec);
|
2015-11-12 03:54:14 +08:00
|
|
|
} else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
|
|
|
|
if (S->RelocSection)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Multiple relocation sections to .eh_frame are not supported");
|
2015-11-12 03:54:14 +08:00
|
|
|
S->RelocSection = &Sec;
|
|
|
|
} else {
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Relocations pointing to SHF_MERGE are not supported");
|
2015-11-12 03:54:14 +08:00
|
|
|
}
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-08-28 07:15:56 +08:00
|
|
|
}
|
2015-11-22 06:19:32 +08:00
|
|
|
default:
|
2015-12-24 16:41:12 +08:00
|
|
|
Sections[I] = createInputSection(Sec);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:17:10 +08:00
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
2016-02-28 08:25:54 +08:00
|
|
|
elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef Name = check(this->ELFObj.getSectionName(&Sec));
|
2015-12-24 16:41:12 +08:00
|
|
|
|
|
|
|
// .note.GNU-stack is a marker section to control the presence of
|
|
|
|
// PT_GNU_STACK segment in outputs. Since the presence of the segment
|
|
|
|
// is controlled only by the command line option (-z execstack) in LLD,
|
|
|
|
// .note.GNU-stack is ignored.
|
|
|
|
if (Name == ".note.GNU-stack")
|
2016-02-25 02:33:35 +08:00
|
|
|
return InputSection<ELFT>::Discarded;
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2016-03-10 02:01:45 +08:00
|
|
|
if (Name == ".note.GNU-split-stack")
|
|
|
|
error("Objects using splitstacks are not supported");
|
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
// A MIPS object file has a special section that contains register
|
|
|
|
// usage info, which needs to be handled by the linker specially.
|
2015-12-25 21:02:13 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
|
2016-01-06 09:14:11 +08:00
|
|
|
MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
|
2015-12-25 21:02:13 +08:00
|
|
|
return MipsReginfo;
|
|
|
|
}
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2016-03-03 15:49:35 +08:00
|
|
|
// We dont need special handling of .eh_frame sections if relocatable
|
|
|
|
// output was choosen. Proccess them as usual input sections.
|
|
|
|
if (!Config->Relocatable && Name == ".eh_frame")
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
if (shouldMerge<ELFT>(Sec))
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
|
|
|
|
return new (Alloc) InputSection<ELFT>(this, &Sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
|
2015-10-02 03:52:48 +08:00
|
|
|
this->initStringTable();
|
2015-09-08 23:50:05 +08:00
|
|
|
Elf_Sym_Range Syms = this->getNonLocalSymbols();
|
2015-08-12 04:06:51 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
2016-01-06 09:14:11 +08:00
|
|
|
SymbolBodies.reserve(NumSymbols);
|
2015-08-04 22:00:56 +08:00
|
|
|
for (const Elf_Sym &Sym : Syms)
|
2016-01-21 10:10:12 +08:00
|
|
|
SymbolBodies.push_back(createSymbolBody(&Sym));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 23:29:48 +08:00
|
|
|
template <class ELFT>
|
2015-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *
|
2016-02-28 08:25:54 +08:00
|
|
|
elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
|
2015-11-03 22:13:40 +08:00
|
|
|
uint32_t Index = this->getSectionIndex(Sym);
|
|
|
|
if (Index == 0)
|
2015-10-16 23:29:48 +08:00
|
|
|
return nullptr;
|
2015-11-03 22:13:40 +08:00
|
|
|
if (Index >= Sections.size() || !Sections[Index])
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid section index");
|
2016-02-26 02:43:51 +08:00
|
|
|
InputSectionBase<ELFT> *S = Sections[Index];
|
|
|
|
if (S == InputSectionBase<ELFT>::Discarded)
|
|
|
|
return S;
|
|
|
|
return S->Repl;
|
2015-10-16 23:29:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef Name = check(Sym->getName(this->StringTable));
|
2015-08-25 05:43:25 +08:00
|
|
|
|
2015-10-16 23:29:48 +08:00
|
|
|
switch (Sym->st_shndx) {
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_UNDEF:
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_COMMON:
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
|
|
|
|
Sym->getBinding() == llvm::ELF::STB_WEAK,
|
|
|
|
Sym->getVisibility());
|
2015-08-29 05:26:51 +08:00
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
2015-08-12 01:33:02 +08:00
|
|
|
switch (Sym->getBinding()) {
|
|
|
|
default:
|
2016-03-11 00:58:34 +08:00
|
|
|
fatal("Unexpected binding");
|
2015-08-12 01:33:02 +08:00
|
|
|
case STB_GLOBAL:
|
2015-08-29 04:19:34 +08:00
|
|
|
case STB_WEAK:
|
2015-10-10 03:25:07 +08:00
|
|
|
case STB_GNU_UNIQUE: {
|
2015-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *Sec = getSection(*Sym);
|
2016-02-25 02:33:35 +08:00
|
|
|
if (Sec == InputSection<ELFT>::Discarded)
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
|
|
|
|
return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
2015-08-12 01:33:02 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 02:02:21 +08:00
|
|
|
void ArchiveFile::parse() {
|
2016-03-04 06:24:39 +08:00
|
|
|
File = check(Archive::create(MB), "Failed to parse archive");
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
// Allocate a buffer for Lazy objects.
|
|
|
|
size_t NumSyms = File->getNumberOfSymbols();
|
|
|
|
LazySymbols.reserve(NumSyms);
|
|
|
|
|
|
|
|
// Read the symbol table to construct Lazy objects.
|
|
|
|
for (const Archive::Symbol &Sym : File->symbols())
|
|
|
|
LazySymbols.emplace_back(this, Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
|
|
|
MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
|
2016-03-04 00:21:44 +08:00
|
|
|
Archive::Child C =
|
2016-03-04 06:24:39 +08:00
|
|
|
check(Sym->getMember(),
|
2016-03-04 00:21:44 +08:00
|
|
|
"Could not get the member for symbol " + Sym->getName());
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2015-11-05 22:40:28 +08:00
|
|
|
if (!Seen.insert(C.getChildOffset()).second)
|
2015-09-05 06:28:10 +08:00
|
|
|
return MemoryBufferRef();
|
2015-09-09 04:36:20 +08:00
|
|
|
|
2016-03-04 06:24:39 +08:00
|
|
|
return check(C.getMemoryBufferRef(),
|
2016-03-04 00:21:44 +08:00
|
|
|
"Could not get the buffer for the member defining symbol " +
|
|
|
|
Sym->getName());
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
|
2016-01-06 08:09:41 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-11-03 22:13:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
const typename ELFFile<ELFT>::Elf_Shdr *
|
|
|
|
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
|
|
|
|
uint32_t Index = this->getSectionIndex(Sym);
|
|
|
|
if (Index == 0)
|
|
|
|
return nullptr;
|
2016-03-04 06:24:39 +08:00
|
|
|
return check(this->ELFObj.getSection(Index));
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 09:56:36 +08:00
|
|
|
// Partially parse the shared object file so that we can call
|
|
|
|
// getSoName on this object.
|
2015-10-02 03:52:48 +08:00
|
|
|
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
|
2015-10-01 23:47:50 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
const Elf_Shdr *DynamicSec = nullptr;
|
|
|
|
|
|
|
|
const ELFFile<ELFT> Obj = this->ELFObj;
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
2015-11-03 22:13:40 +08:00
|
|
|
switch (Sec.sh_type) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case SHT_DYNSYM:
|
2015-09-08 23:50:05 +08:00
|
|
|
this->Symtab = &Sec;
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
|
|
|
case SHT_DYNAMIC:
|
2015-10-01 23:47:50 +08:00
|
|
|
DynamicSec = &Sec;
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
2016-03-04 00:21:44 +08:00
|
|
|
case SHT_SYMTAB_SHNDX:
|
2016-03-04 06:24:39 +08:00
|
|
|
this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 03:52:48 +08:00
|
|
|
this->initStringTable();
|
2016-01-06 09:14:11 +08:00
|
|
|
SoName = this->getName();
|
2015-10-01 23:47:50 +08:00
|
|
|
|
2015-10-12 23:49:02 +08:00
|
|
|
if (!DynamicSec)
|
|
|
|
return;
|
|
|
|
auto *Begin =
|
|
|
|
reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
|
|
|
|
const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
|
|
|
|
|
|
|
|
for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
|
|
|
|
if (Dyn.d_tag == DT_SONAME) {
|
|
|
|
uintX_t Val = Dyn.getVal();
|
|
|
|
if (Val >= this->StringTable.size())
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid DT_SONAME entry");
|
2016-01-06 09:14:11 +08:00
|
|
|
SoName = StringRef(this->StringTable.data() + Val);
|
2015-10-12 23:49:02 +08:00
|
|
|
return;
|
2015-10-01 23:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
2015-10-01 23:47:50 +08:00
|
|
|
|
2016-01-06 09:56:36 +08:00
|
|
|
// Fully parse the shared object file. This must be called after parseSoName().
|
|
|
|
template <class ELFT> void SharedFile<ELFT>::parseRest() {
|
2015-10-02 03:52:48 +08:00
|
|
|
Elf_Sym_Range Syms = this->getNonLocalSymbols();
|
2015-09-08 23:50:05 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
|
|
|
SymbolBodies.reserve(NumSymbols);
|
|
|
|
for (const Elf_Sym &Sym : Syms) {
|
2016-03-04 09:56:52 +08:00
|
|
|
StringRef Name = check(Sym.getName(this->StringTable));
|
2015-10-14 00:34:14 +08:00
|
|
|
if (Sym.isUndefined())
|
|
|
|
Undefs.push_back(Name);
|
|
|
|
else
|
|
|
|
SymbolBodies.emplace_back(this, Name, Sym);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2016-02-13 04:54:57 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
|
|
|
|
|
|
|
|
bool BitcodeFile::classof(const InputFile *F) {
|
|
|
|
return F->kind() == BitcodeKind;
|
|
|
|
}
|
|
|
|
|
2016-03-07 08:54:17 +08:00
|
|
|
static uint8_t getGvVisibility(const GlobalValue *GV) {
|
|
|
|
switch (GV->getVisibility()) {
|
2016-03-08 03:06:14 +08:00
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
return STV_DEFAULT;
|
2016-03-07 08:54:17 +08:00
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
return STV_HIDDEN;
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
return STV_PROTECTED;
|
|
|
|
}
|
2016-03-10 02:05:34 +08:00
|
|
|
llvm_unreachable("Unknown visibility");
|
2016-03-07 08:54:17 +08:00
|
|
|
}
|
|
|
|
|
2016-03-02 23:43:50 +08:00
|
|
|
void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
|
2016-02-13 04:54:57 +08:00
|
|
|
LLVMContext Context;
|
2016-03-04 06:24:39 +08:00
|
|
|
std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context));
|
2016-03-04 00:21:44 +08:00
|
|
|
const Module &M = Obj->getModule();
|
2016-03-02 23:43:50 +08:00
|
|
|
|
|
|
|
DenseSet<const Comdat *> KeptComdats;
|
|
|
|
for (const auto &P : M.getComdatSymbolTable()) {
|
|
|
|
StringRef N = Saver.save(P.first());
|
|
|
|
if (ComdatGroups.insert(N).second)
|
|
|
|
KeptComdats.insert(&P.second);
|
|
|
|
}
|
|
|
|
|
2016-03-04 00:21:44 +08:00
|
|
|
for (const BasicSymbolRef &Sym : Obj->symbols()) {
|
2016-03-08 03:15:57 +08:00
|
|
|
const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
|
2016-03-09 08:31:06 +08:00
|
|
|
assert(GV);
|
2016-03-09 07:50:56 +08:00
|
|
|
uint32_t Flags = Sym.getFlags();
|
2016-03-09 08:31:06 +08:00
|
|
|
if (const Comdat *C = GV->getComdat())
|
|
|
|
if (!KeptComdats.count(C))
|
2016-03-09 07:50:56 +08:00
|
|
|
continue;
|
2016-03-09 10:02:26 +08:00
|
|
|
if (!(Flags & BasicSymbolRef::SF_Global))
|
2016-03-09 08:31:06 +08:00
|
|
|
continue;
|
2016-03-11 08:50:05 +08:00
|
|
|
if (GV->hasAppendingLinkage()) {
|
|
|
|
ExtraKeeps.push_back(GV->getName().copy(Alloc));
|
2016-03-09 11:42:39 +08:00
|
|
|
continue;
|
2016-03-11 08:50:05 +08:00
|
|
|
}
|
2016-03-11 13:42:34 +08:00
|
|
|
if (Flags & BasicSymbolRef::SF_FormatSpecific)
|
2016-03-11 13:48:04 +08:00
|
|
|
continue;
|
2016-03-09 08:31:06 +08:00
|
|
|
uint8_t Visibility = getGvVisibility(GV);
|
2016-03-02 23:43:50 +08:00
|
|
|
|
2016-02-13 04:54:57 +08:00
|
|
|
SmallString<64> Name;
|
|
|
|
raw_svector_ostream OS(Name);
|
|
|
|
Sym.printName(OS);
|
|
|
|
StringRef NameRef = Saver.save(StringRef(Name));
|
2016-03-07 08:54:17 +08:00
|
|
|
|
2016-02-20 06:50:16 +08:00
|
|
|
SymbolBody *Body;
|
2016-02-29 22:29:48 +08:00
|
|
|
bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
|
2016-03-08 03:15:57 +08:00
|
|
|
if (Flags & BasicSymbolRef::SF_Undefined) {
|
2016-03-05 08:09:37 +08:00
|
|
|
Body = new (Alloc) Undefined(NameRef, IsWeak, Visibility, false);
|
2016-03-08 03:15:57 +08:00
|
|
|
} else if (Flags & BasicSymbolRef::SF_Common) {
|
|
|
|
const DataLayout &DL = M.getDataLayout();
|
|
|
|
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
|
|
|
|
Body = new (Alloc)
|
|
|
|
DefinedCommon(NameRef, Size, GV->getAlignment(), IsWeak, Visibility);
|
|
|
|
} else {
|
2016-03-08 01:14:36 +08:00
|
|
|
Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
|
2016-03-08 03:15:57 +08:00
|
|
|
}
|
2016-03-09 08:47:47 +08:00
|
|
|
Body->IsTls = GV->isThreadLocal();
|
2016-02-13 04:54:57 +08:00
|
|
|
SymbolBodies.push_back(Body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:31:09 +08:00
|
|
|
template <typename T>
|
|
|
|
static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
|
|
|
|
std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
|
|
|
|
|
|
|
|
if (!Config->FirstElf)
|
|
|
|
Config->FirstElf = Ret.get();
|
|
|
|
|
2015-10-14 00:20:50 +08:00
|
|
|
if (Config->EKind == ELFNoneKind) {
|
|
|
|
Config->EKind = Ret->getELFKind();
|
2015-10-12 23:31:09 +08:00
|
|
|
Config->EMachine = Ret->getEMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <template <class> class T>
|
2016-01-06 08:09:43 +08:00
|
|
|
static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
|
2015-11-20 10:19:36 +08:00
|
|
|
std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
|
2015-10-12 23:31:09 +08:00
|
|
|
if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid data encoding: " + MB.getBufferIdentifier());
|
2015-10-12 23:31:09 +08:00
|
|
|
|
|
|
|
if (Type.first == ELF::ELFCLASS32) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
2015-11-20 10:19:36 +08:00
|
|
|
return createELFFileAux<T<ELF32LE>>(MB);
|
|
|
|
return createELFFileAux<T<ELF32BE>>(MB);
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
if (Type.first == ELF::ELFCLASS64) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
2015-11-20 10:19:36 +08:00
|
|
|
return createELFFileAux<T<ELF64LE>>(MB);
|
|
|
|
return createELFFileAux<T<ELF64BE>>(MB);
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("Invalid file class: " + MB.getBufferIdentifier());
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
|
|
|
|
StringRef ArchiveName) {
|
2016-02-24 02:17:11 +08:00
|
|
|
using namespace sys::fs;
|
|
|
|
std::unique_ptr<InputFile> F;
|
|
|
|
if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
|
|
|
|
F.reset(new BitcodeFile(MB));
|
|
|
|
else
|
|
|
|
F = createELFFile<ObjectFile>(MB);
|
2016-02-02 16:22:41 +08:00
|
|
|
F->ArchiveName = ArchiveName;
|
|
|
|
return F;
|
2016-01-06 08:09:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
|
2016-01-06 08:09:43 +08:00
|
|
|
return createELFFile<SharedFile>(MB);
|
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::ELFFileBase<ELF32LE>;
|
|
|
|
template class elf::ELFFileBase<ELF32BE>;
|
|
|
|
template class elf::ELFFileBase<ELF64LE>;
|
|
|
|
template class elf::ELFFileBase<ELF64BE>;
|
2015-11-20 10:19:36 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::ObjectFile<ELF32LE>;
|
|
|
|
template class elf::ObjectFile<ELF32BE>;
|
|
|
|
template class elf::ObjectFile<ELF64LE>;
|
|
|
|
template class elf::ObjectFile<ELF64BE>;
|
2015-11-20 10:19:36 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::SharedFile<ELF32LE>;
|
|
|
|
template class elf::SharedFile<ELF32BE>;
|
|
|
|
template class elf::SharedFile<ELF64LE>;
|
|
|
|
template class elf::SharedFile<ELF64BE>;
|