2015-08-14 22:12:54 +08:00
|
|
|
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_INPUT_FILES_H
|
|
|
|
#define LLD_ELF_INPUT_FILES_H
|
|
|
|
|
2015-10-07 17:13:03 +08:00
|
|
|
#include "Config.h"
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "Error.h"
|
2015-08-28 10:40:04 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#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
|
|
|
|
|
|
|
using llvm::object::Archive;
|
|
|
|
|
2015-10-01 01:06:09 +08:00
|
|
|
class InputFile;
|
2015-09-05 06:28:10 +08:00
|
|
|
class Lazy;
|
2015-07-25 05:03:07 +08:00
|
|
|
class SymbolBody;
|
|
|
|
|
|
|
|
// The root class of input files.
|
|
|
|
class InputFile {
|
|
|
|
public:
|
2015-09-05 06:28:10 +08:00
|
|
|
enum Kind { ObjectKind, SharedKind, ArchiveKind };
|
2015-07-25 05:03:07 +08:00
|
|
|
Kind kind() const { return FileKind; }
|
|
|
|
virtual ~InputFile() {}
|
|
|
|
|
2015-08-05 20:03:34 +08:00
|
|
|
StringRef getName() const { return MB.getBufferIdentifier(); }
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
protected:
|
2015-09-30 10:06:17 +08:00
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
MemoryBufferRef MB;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Kind FileKind;
|
|
|
|
};
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <typename ELFT> class ELFFileBase : public InputFile {
|
2015-09-04 04:03:54 +08:00
|
|
|
public:
|
2015-10-12 09:55:32 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
|
|
|
ELFFileBase(Kind K, ELFKind EKind, MemoryBufferRef M);
|
2015-09-04 04:03:54 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
Kind K = F->kind();
|
|
|
|
return K == ObjectKind || K == SharedKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
ELFKind getELFKind() const { return EKind; }
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
|
|
|
|
llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }
|
|
|
|
|
|
|
|
uint16_t getEMachine() const { return getObj().getHeader()->e_machine; }
|
|
|
|
uint8_t getOSABI() const {
|
|
|
|
return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getStringTable() const { return StringTable; }
|
2015-09-23 00:53:55 +08:00
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
protected:
|
|
|
|
const ELFKind EKind;
|
2015-10-12 09:55:32 +08:00
|
|
|
llvm::object::ELFFile<ELFT> ELFObj;
|
|
|
|
const Elf_Shdr *Symtab = nullptr;
|
|
|
|
StringRef StringTable;
|
|
|
|
void initStringTable();
|
|
|
|
Elf_Sym_Range getNonLocalSymbols();
|
|
|
|
Elf_Sym_Range getSymbolsHelper(bool);
|
2015-09-04 04:03:54 +08:00
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// .o file.
|
2015-10-12 09:55:32 +08:00
|
|
|
template <typename ELFT> class ObjectFileBase : public ELFFileBase<ELFT> {
|
|
|
|
typedef ELFFileBase<ELFT> Base;
|
|
|
|
|
2015-08-04 22:29:01 +08:00
|
|
|
public:
|
2015-09-30 10:06:17 +08:00
|
|
|
ObjectFileBase(ELFKind EKind, MemoryBufferRef M)
|
2015-10-12 09:55:32 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::ObjectKind, EKind, M) {}
|
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
return F->kind() == Base::ObjectKind;
|
|
|
|
}
|
2015-08-04 22:29:01 +08:00
|
|
|
|
2015-09-03 07:01:37 +08:00
|
|
|
ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; }
|
2015-10-10 03:25:07 +08:00
|
|
|
virtual void parse(llvm::DenseSet<StringRef> &Comdats) = 0;
|
2015-08-04 22:29:01 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
|
|
|
};
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
template <class ELFT> static ELFKind getStaticELFKind() {
|
|
|
|
if (!ELFT::Is64Bits) {
|
|
|
|
if (ELFT::TargetEndianness == llvm::support::little)
|
|
|
|
return ELF32LEKind;
|
|
|
|
return ELF32BEKind;
|
|
|
|
}
|
|
|
|
if (ELFT::TargetEndianness == llvm::support::little)
|
|
|
|
return ELF64LEKind;
|
|
|
|
return ELF64BEKind;
|
|
|
|
}
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <class ELFT> class ObjectFile : public ObjectFileBase<ELFT> {
|
|
|
|
typedef ObjectFileBase<ELFT> Base;
|
2015-07-25 05:03:07 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
2015-08-25 05:43:25 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
typedef llvm::support::detail::packed_endian_specific_integral<
|
|
|
|
uint32_t, ELFT::TargetEndianness, 2> GroupEntryType;
|
|
|
|
StringRef getShtGroupSignature(const Elf_Shdr &Sec);
|
|
|
|
ArrayRef<GroupEntryType> getShtGroupEntries(const Elf_Shdr &Sec);
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
2015-09-03 04:43:43 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-12 09:55:32 +08:00
|
|
|
return F->kind() == Base::ObjectKind &&
|
|
|
|
cast<ELFFileBase<ELFT>>(F)->getELFKind() == getStaticELFKind<ELFT>();
|
2015-09-03 04:43:43 +08:00
|
|
|
}
|
2015-08-05 20:03:34 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
explicit ObjectFile(MemoryBufferRef M);
|
2015-10-10 03:25:07 +08:00
|
|
|
void parse(llvm::DenseSet<StringRef> &Comdats) override;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
ArrayRef<InputSection<ELFT> *> getSections() const { return Sections; }
|
2015-08-05 21:55:34 +08:00
|
|
|
|
2015-09-18 22:40:19 +08:00
|
|
|
SymbolBody *getSymbolBody(uint32_t SymbolIndex) const {
|
2015-09-08 23:50:05 +08:00
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
2015-08-28 07:15:56 +08:00
|
|
|
if (SymbolIndex < FirstNonLocal)
|
|
|
|
return nullptr;
|
2015-10-12 09:55:32 +08:00
|
|
|
return this->SymbolBodies[SymbolIndex - FirstNonLocal];
|
2015-08-28 07:15:56 +08:00
|
|
|
}
|
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
Elf_Sym_Range getLocalSymbols();
|
|
|
|
|
2015-09-19 02:28:08 +08:00
|
|
|
const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
|
|
|
|
ArrayRef<Elf_Word> getSymbolTableShndx() const { return SymtabSHNDX; };
|
2015-09-18 09:08:17 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
private:
|
2015-10-10 03:25:07 +08:00
|
|
|
void initializeSections(llvm::DenseSet<StringRef> &Comdats);
|
2015-07-25 05:03:07 +08:00
|
|
|
void initializeSymbols();
|
|
|
|
|
|
|
|
SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym);
|
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
// List of all sections defined by this file.
|
|
|
|
std::vector<InputSection<ELFT> *> Sections;
|
2015-08-10 23:12:17 +08:00
|
|
|
|
2015-08-25 05:43:25 +08:00
|
|
|
ArrayRef<Elf_Word> SymtabSHNDX;
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
class ArchiveFile : public InputFile {
|
|
|
|
public:
|
|
|
|
explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
|
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
|
2015-10-10 03:25:07 +08:00
|
|
|
void parse();
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
// Returns a memory buffer for a given symbol. An empty memory buffer
|
|
|
|
// is returned if we have already returned the same memory buffer.
|
|
|
|
// (So that we don't instantiate same members more than once.)
|
|
|
|
MemoryBufferRef getMember(const Archive::Symbol *Sym);
|
|
|
|
|
|
|
|
llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; }
|
2015-10-02 02:02:21 +08:00
|
|
|
std::vector<MemoryBufferRef> getMembers();
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Archive> File;
|
|
|
|
std::vector<Lazy> LazySymbols;
|
|
|
|
llvm::DenseSet<uint64_t> Seen;
|
|
|
|
};
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
// .so file.
|
2015-10-12 09:55:32 +08:00
|
|
|
template <typename ELFT> class SharedFileBase : public ELFFileBase<ELFT> {
|
|
|
|
typedef ELFFileBase<ELFT> Base;
|
|
|
|
|
2015-10-01 23:47:50 +08:00
|
|
|
protected:
|
|
|
|
StringRef SoName;
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
public:
|
2015-09-30 10:06:17 +08:00
|
|
|
SharedFileBase(ELFKind EKind, MemoryBufferRef M)
|
2015-10-12 09:55:32 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::SharedKind, EKind, M) {
|
|
|
|
AsNeeded = Config->AsNeeded;
|
|
|
|
}
|
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
return F->kind() == Base::SharedKind;
|
|
|
|
}
|
2015-10-01 23:47:50 +08:00
|
|
|
StringRef getSoName() const { return SoName; }
|
2015-10-02 03:52:48 +08:00
|
|
|
virtual void parseSoName() = 0;
|
2015-10-10 03:25:07 +08:00
|
|
|
virtual void parse() = 0;
|
2015-10-12 04:59:12 +08:00
|
|
|
|
|
|
|
// Used for --as-needed
|
|
|
|
bool AsNeeded = false;
|
|
|
|
bool IsUsed = false;
|
|
|
|
bool isNeeded() const { return !AsNeeded || IsUsed; }
|
2015-09-04 04:03:54 +08:00
|
|
|
};
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <class ELFT> class SharedFile : public SharedFileBase<ELFT> {
|
|
|
|
typedef SharedFileBase<ELFT> Base;
|
2015-09-08 23:50:05 +08:00
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
|
|
|
|
typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
|
|
|
|
|
|
|
|
std::vector<SharedSymbol<ELFT>> SymbolBodies;
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
public:
|
2015-09-08 23:50:05 +08:00
|
|
|
llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() {
|
|
|
|
return SymbolBodies;
|
|
|
|
}
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-12 09:55:32 +08:00
|
|
|
return F->kind() == Base::SharedKind &&
|
|
|
|
cast<ELFFileBase<ELFT>>(F)->getELFKind() == getStaticELFKind<ELFT>();
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
explicit SharedFile(MemoryBufferRef M);
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2015-10-02 03:52:48 +08:00
|
|
|
void parseSoName() override;
|
2015-09-04 04:03:54 +08:00
|
|
|
void parse() override;
|
|
|
|
};
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <typename T>
|
|
|
|
std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
|
|
|
|
std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
|
|
|
|
|
|
|
|
if (!Config->FirstElf)
|
|
|
|
Config->FirstElf = Ret.get();
|
|
|
|
|
|
|
|
if (Config->ElfKind == ELFNoneKind) {
|
|
|
|
Config->ElfKind = Ret->getELFKind();
|
|
|
|
Config->EMachine = Ret->getEMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Ret);
|
|
|
|
}
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
template <template <class> class T>
|
2015-10-12 09:55:32 +08:00
|
|
|
std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
|
2015-09-05 06:28:10 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
std::pair<unsigned char, unsigned char> Type =
|
|
|
|
object::getElfArchType(MB.getBuffer());
|
|
|
|
if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
|
2015-09-29 02:20:41 +08:00
|
|
|
error("Invalid data encoding: " + MB.getBufferIdentifier());
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
if (Type.first == ELF::ELFCLASS32) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
2015-10-12 09:55:32 +08:00
|
|
|
return createELFFileAux<T<object::ELF32LE>>(MB);
|
|
|
|
return createELFFileAux<T<object::ELF32BE>>(MB);
|
2015-10-11 11:36:49 +08:00
|
|
|
}
|
2015-10-12 09:55:32 +08:00
|
|
|
if (Type.first == ELF::ELFCLASS64) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
|
|
|
return createELFFileAux<T<object::ELF64LE>>(MB);
|
|
|
|
return createELFFileAux<T<object::ELF64BE>>(MB);
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
2015-10-12 09:55:32 +08:00
|
|
|
error("Invalid file class: " + MB.getBufferIdentifier());
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace elf2
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|