2015-08-14 14:12:54 +00:00
|
|
|
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
|
2015-07-24 21:03:07 +00: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 09:13:03 +00:00
|
|
|
#include "Config.h"
|
2015-09-22 00:01:39 +00:00
|
|
|
#include "InputSection.h"
|
2015-09-04 22:28:10 +00:00
|
|
|
#include "Error.h"
|
2015-08-28 02:40:04 +00:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
#include "lld/Core/LLVM.h"
|
2015-09-04 22:28:10 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-03-11 18:46:51 +00:00
|
|
|
#include "llvm/IR/Comdat.h"
|
2015-09-04 22:28:10 +00:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
#include "llvm/Object/ELF.h"
|
2016-03-11 18:46:51 +00:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2016-02-12 20:54:57 +00:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
|
namespace lld {
|
2016-02-28 00:25:54 +00:00
|
|
|
namespace elf {
|
2015-09-04 22:28:10 +00:00
|
|
|
|
|
|
|
|
using llvm::object::Archive;
|
|
|
|
|
|
2015-09-30 17:06:09 +00:00
|
|
|
class InputFile;
|
2015-09-04 22:28:10 +00:00
|
|
|
class Lazy;
|
2015-07-24 21:03:07 +00:00
|
|
|
class SymbolBody;
|
|
|
|
|
|
|
|
|
|
// The root class of input files.
|
|
|
|
|
class InputFile {
|
|
|
|
|
public:
|
2016-04-07 19:24:51 +00:00
|
|
|
enum Kind {
|
|
|
|
|
ObjectKind,
|
|
|
|
|
SharedKind,
|
|
|
|
|
LazyObjectKind,
|
|
|
|
|
ArchiveKind,
|
|
|
|
|
BitcodeKind,
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
Kind kind() const { return FileKind; }
|
|
|
|
|
|
2015-08-05 12:03:34 +00:00
|
|
|
StringRef getName() const { return MB.getBufferIdentifier(); }
|
2016-02-12 20:54:57 +00:00
|
|
|
MemoryBufferRef MB;
|
2015-08-05 12:03:34 +00:00
|
|
|
|
2016-02-02 08:22:41 +00:00
|
|
|
// Filename of .a which contained this file. If this file was
|
|
|
|
|
// not in an archive file, it is the empty string. We use this
|
|
|
|
|
// string for creating error messages.
|
2016-02-02 20:24:31 +00:00
|
|
|
StringRef ArchiveName;
|
2016-02-02 08:22:41 +00:00
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
protected:
|
2015-09-30 02:06:17 +00:00
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
2015-07-24 21:03:07 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const Kind FileKind;
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-12 01:55:32 +00:00
|
|
|
template <typename ELFT> class ELFFileBase : public InputFile {
|
2015-09-03 20:03:54 +00:00
|
|
|
public:
|
2016-03-14 23:16:09 +00:00
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
|
typedef typename ELFT::Word Elf_Word;
|
|
|
|
|
typedef typename ELFT::SymRange Elf_Sym_Range;
|
2015-10-12 01:55:32 +00:00
|
|
|
|
2015-10-13 01:17:02 +00:00
|
|
|
ELFFileBase(Kind K, MemoryBufferRef M);
|
2015-09-03 20:03:54 +00:00
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
|
Kind K = F->kind();
|
|
|
|
|
return K == ObjectKind || K == SharedKind;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 02:10:52 +00:00
|
|
|
static ELFKind getELFKind();
|
2015-10-12 01:55:32 +00: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-22 16:53:55 +00:00
|
|
|
|
2015-11-03 14:13:40 +00:00
|
|
|
uint32_t getSectionIndex(const Elf_Sym &Sym) const;
|
|
|
|
|
|
2015-09-03 20:03:54 +00:00
|
|
|
protected:
|
2015-10-12 15:15:45 +00:00
|
|
|
llvm::object::ELFFile<ELFT> ELFObj;
|
|
|
|
|
const Elf_Shdr *Symtab = nullptr;
|
2015-11-03 14:13:40 +00:00
|
|
|
ArrayRef<Elf_Word> SymtabSHNDX;
|
2015-10-12 15:15:45 +00:00
|
|
|
StringRef StringTable;
|
|
|
|
|
void initStringTable();
|
2016-03-11 12:06:30 +00:00
|
|
|
Elf_Sym_Range getElfSymbols(bool OnlyGlobals);
|
2015-10-12 12:14:30 +00:00
|
|
|
};
|
|
|
|
|
|
2015-10-12 02:22:58 +00:00
|
|
|
// .o file.
|
|
|
|
|
template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
|
|
|
|
|
typedef ELFFileBase<ELFT> Base;
|
2016-03-14 23:16:09 +00:00
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
|
typedef typename ELFT::SymRange Elf_Sym_Range;
|
|
|
|
|
typedef typename ELFT::Word Elf_Word;
|
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2015-10-09 19:25:07 +00:00
|
|
|
StringRef getShtGroupSignature(const Elf_Shdr &Sec);
|
2016-03-13 22:02:04 +00:00
|
|
|
ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
|
2015-10-09 19:25:07 +00:00
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
public:
|
2015-09-02 20:43:43 +00:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-13 01:17:02 +00:00
|
|
|
return F->kind() == Base::ObjectKind;
|
2015-09-02 20:43:43 +00:00
|
|
|
}
|
2015-08-05 12:03:34 +00:00
|
|
|
|
2016-03-11 12:06:30 +00:00
|
|
|
ArrayRef<SymbolBody *> getSymbols();
|
|
|
|
|
ArrayRef<SymbolBody *> getLocalSymbols();
|
|
|
|
|
ArrayRef<SymbolBody *> getNonLocalSymbols();
|
2015-10-12 02:22:58 +00:00
|
|
|
|
2015-09-24 15:11:50 +00:00
|
|
|
explicit ObjectFile(MemoryBufferRef M);
|
2016-01-06 02:06:33 +00:00
|
|
|
void parse(llvm::DenseSet<StringRef> &ComdatGroups);
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2015-10-19 21:00:02 +00:00
|
|
|
ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
|
|
|
|
|
InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
|
2015-08-05 13:55:34 +00:00
|
|
|
|
2016-03-11 12:06:30 +00:00
|
|
|
SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
|
|
|
|
|
return *SymbolBodies[SymbolIndex];
|
2015-08-27 23:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-18 18:28:08 +00:00
|
|
|
const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
|
2015-09-18 01:08:17 +00:00
|
|
|
|
2015-12-25 13:02:13 +00:00
|
|
|
// Get MIPS GP0 value defined by this file. This value represents the gp value
|
|
|
|
|
// used to create the relocatable object and required to support
|
|
|
|
|
// R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
|
|
|
|
|
uint32_t getMipsGp0() const;
|
|
|
|
|
|
2016-01-29 01:24:25 +00:00
|
|
|
// The number is the offset in the string table. It will be used as the
|
|
|
|
|
// st_name of the symbol.
|
2016-04-04 14:04:16 +00:00
|
|
|
std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
|
2016-01-27 18:04:26 +00:00
|
|
|
|
2015-07-24 21:03:07 +00:00
|
|
|
private:
|
2016-01-06 02:06:33 +00:00
|
|
|
void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
|
2015-07-24 21:03:07 +00:00
|
|
|
void initializeSymbols();
|
2016-03-13 21:52:57 +00:00
|
|
|
InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
|
2015-12-24 08:41:12 +00:00
|
|
|
InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec);
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2016-01-21 02:10:12 +00:00
|
|
|
SymbolBody *createSymbolBody(const Elf_Sym *Sym);
|
2015-07-24 21:03:07 +00:00
|
|
|
|
2015-09-22 00:16:19 +00:00
|
|
|
// List of all sections defined by this file.
|
2015-10-19 21:00:02 +00:00
|
|
|
std::vector<InputSectionBase<ELFT> *> Sections;
|
2015-08-10 15:12:17 +00:00
|
|
|
|
2015-10-12 02:22:58 +00:00
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
|
|
|
|
|
2015-12-25 13:02:13 +00:00
|
|
|
// MIPS .reginfo section defined by this file.
|
2016-04-06 02:52:47 +00:00
|
|
|
std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo;
|
2015-12-25 13:02:13 +00:00
|
|
|
|
2015-10-12 02:22:58 +00:00
|
|
|
llvm::BumpPtrAllocator Alloc;
|
2016-04-06 01:11:10 +00:00
|
|
|
llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
|
2015-12-23 01:18:40 +00:00
|
|
|
llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
|
2015-12-23 03:40:17 +00:00
|
|
|
llvm::SpecificBumpPtrAllocator<EHInputSection<ELFT>> EHAlloc;
|
2015-07-24 21:03:07 +00:00
|
|
|
};
|
|
|
|
|
|
2016-04-07 19:24:51 +00:00
|
|
|
// LazyObjectFile is analogous to ArchiveFile in the sense that
|
|
|
|
|
// the file contains lazy symbols. The difference is that
|
|
|
|
|
// LazyObjectFile wraps a single file instead of multiple files.
|
|
|
|
|
//
|
|
|
|
|
// This class is used for --start-lib and --end-lib options which
|
|
|
|
|
// instruct the linker to link object files between them with the
|
|
|
|
|
// archive file semantics.
|
|
|
|
|
class LazyObjectFile : public InputFile {
|
|
|
|
|
public:
|
|
|
|
|
explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
|
|
|
|
|
|
|
|
|
|
static bool classof(const InputFile *F) {
|
|
|
|
|
return F->kind() == LazyObjectKind;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void parse();
|
|
|
|
|
|
|
|
|
|
llvm::MutableArrayRef<LazyObject> getLazySymbols() { return LazySymbols; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<StringRef> getSymbols();
|
|
|
|
|
template <class ELFT> std::vector<StringRef> getElfSymbols();
|
|
|
|
|
std::vector<StringRef> getBitcodeSymbols();
|
|
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator Alloc;
|
|
|
|
|
llvm::StringSaver Saver{Alloc};
|
|
|
|
|
std::vector<LazyObject> LazySymbols;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// An ArchiveFile object represents a .a file.
|
2015-09-04 22:28:10 +00: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-09 19:25:07 +00:00
|
|
|
void parse();
|
2015-09-04 22:28:10 +00: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);
|
|
|
|
|
|
2016-04-07 19:24:51 +00:00
|
|
|
llvm::MutableArrayRef<LazyArchive> getLazySymbols() { return LazySymbols; }
|
2015-09-04 22:28:10 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<Archive> File;
|
2016-04-07 19:24:51 +00:00
|
|
|
std::vector<LazyArchive> LazySymbols;
|
2015-09-04 22:28:10 +00:00
|
|
|
llvm::DenseSet<uint64_t> Seen;
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-12 20:54:57 +00:00
|
|
|
class BitcodeFile : public InputFile {
|
|
|
|
|
public:
|
|
|
|
|
explicit BitcodeFile(MemoryBufferRef M);
|
|
|
|
|
static bool classof(const InputFile *F);
|
2016-03-02 15:43:50 +00:00
|
|
|
void parse(llvm::DenseSet<StringRef> &ComdatGroups);
|
2016-02-26 21:31:34 +00:00
|
|
|
ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; }
|
2016-03-11 18:46:51 +00:00
|
|
|
static bool shouldSkip(const llvm::object::BasicSymbolRef &Sym);
|
2016-02-12 20:54:57 +00:00
|
|
|
|
|
|
|
|
private:
|
2016-02-26 21:31:34 +00:00
|
|
|
std::vector<SymbolBody *> SymbolBodies;
|
2016-02-12 20:54:57 +00:00
|
|
|
llvm::BumpPtrAllocator Alloc;
|
|
|
|
|
llvm::StringSaver Saver{Alloc};
|
2016-03-11 18:46:51 +00:00
|
|
|
SymbolBody *
|
|
|
|
|
createSymbolBody(const llvm::DenseSet<const llvm::Comdat *> &KeptComdats,
|
|
|
|
|
const llvm::object::IRObjectFile &Obj,
|
|
|
|
|
const llvm::object::BasicSymbolRef &Sym);
|
2016-02-12 20:54:57 +00:00
|
|
|
};
|
|
|
|
|
|
2015-09-03 20:03:54 +00:00
|
|
|
// .so file.
|
2015-10-12 02:22:58 +00:00
|
|
|
template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
|
2015-10-12 01:55:32 +00:00
|
|
|
typedef ELFFileBase<ELFT> Base;
|
2016-03-14 23:16:09 +00:00
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
|
typedef typename ELFT::Word Elf_Word;
|
|
|
|
|
typedef typename ELFT::SymRange Elf_Sym_Range;
|
2015-09-08 15:50:05 +00:00
|
|
|
|
|
|
|
|
std::vector<SharedSymbol<ELFT>> SymbolBodies;
|
2015-10-13 16:34:14 +00:00
|
|
|
std::vector<StringRef> Undefs;
|
2015-10-12 02:22:58 +00:00
|
|
|
StringRef SoName;
|
2015-09-08 15:50:05 +00:00
|
|
|
|
2015-09-03 20:03:54 +00:00
|
|
|
public:
|
2015-10-12 02:22:58 +00:00
|
|
|
StringRef getSoName() const { return SoName; }
|
2015-09-08 15:50:05 +00:00
|
|
|
llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() {
|
|
|
|
|
return SymbolBodies;
|
|
|
|
|
}
|
2015-11-03 14:13:40 +00:00
|
|
|
const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
|
2015-10-13 16:34:14 +00:00
|
|
|
llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
|
|
|
|
|
|
2015-09-03 20:03:54 +00:00
|
|
|
static bool classof(const InputFile *F) {
|
2015-10-13 01:17:02 +00:00
|
|
|
return F->kind() == Base::SharedKind;
|
2015-09-03 20:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-24 15:11:50 +00:00
|
|
|
explicit SharedFile(MemoryBufferRef M);
|
2015-09-03 20:03:54 +00:00
|
|
|
|
2015-10-12 02:22:58 +00:00
|
|
|
void parseSoName();
|
2016-01-06 01:56:36 +00:00
|
|
|
void parseRest();
|
2015-10-12 02:22:58 +00:00
|
|
|
|
|
|
|
|
// Used for --as-needed
|
|
|
|
|
bool AsNeeded = false;
|
|
|
|
|
bool IsUsed = false;
|
|
|
|
|
bool isNeeded() const { return !AsNeeded || IsUsed; }
|
2015-09-03 20:03:54 +00:00
|
|
|
};
|
|
|
|
|
|
2016-02-02 08:22:41 +00:00
|
|
|
std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
|
|
|
|
|
StringRef ArchiveName = "");
|
2016-01-06 00:09:43 +00:00
|
|
|
std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);
|
2015-09-04 22:28:10 +00:00
|
|
|
|
2016-02-28 00:25:54 +00:00
|
|
|
} // namespace elf
|
2015-07-24 21:03:07 +00:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
|
|
#endif
|