#ifndef LLD_ELF_INPUT_FILES_H
#define LLD_ELF_INPUT_FILES_H
#include "Config.h"
#include "Symbols.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/LLVM.h"
#include "lld/Common/Reproduce.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/Threading.h"
namespace llvm {
struct DILineInfo;
class TarWriter;
namespace lto {
class InputFile;
}
}
namespace lld {
class DWARFCache;
std::string toString(const elf::InputFile *f);
namespace elf {
class InputSection;
class Symbol;
extern std::unique_ptr<llvm::TarWriter> tar;
llvm::Optional<MemoryBufferRef> readFile(StringRef path);
void parseFile(InputFile *file);
class InputFile {
protected:
SmallVector<Symbol *, 0> symbols;
SmallVector<InputSectionBase *, 0> sections;
public:
enum Kind : uint8_t {
ObjKind,
SharedKind,
ArchiveKind,
BitcodeKind,
BinaryKind,
};
Kind kind() const { return fileKind; }
bool isElf() const {
Kind k = kind();
return k == ObjKind || k == SharedKind;
}
StringRef getName() const { return mb.getBufferIdentifier(); }
MemoryBufferRef mb;
ArrayRef<InputSectionBase *> getSections() const {
assert(fileKind == ObjKind || fileKind == BinaryKind);
return sections;
}
ArrayRef<Symbol *> getSymbols() const {
assert(fileKind == BinaryKind || fileKind == ObjKind ||
fileKind == BitcodeKind);
return symbols;
}
StringRef getNameForScript() const;
bool shouldExtractForCommon(StringRef name);
InputSection *ppc32Got2 = nullptr;
uint32_t mipsGotIndex = -1;
uint32_t groupId;
static bool isInGroup;
static uint32_t nextGroupId;
uint16_t emachine = llvm::ELF::EM_NONE;
const Kind fileKind;
ELFKind ekind = ELFNoneKind;
uint8_t osabi = 0;
uint8_t abiVersion = 0;
bool lazy = false;
bool justSymbols = false;
std::string getSrcMsg(const Symbol &sym, InputSectionBase &sec,
uint64_t offset);
bool ppc64SmallCodeModelTocRelocs = false;
bool ppc64DisableTLSRelax = false;
protected:
InputFile(Kind k, MemoryBufferRef m);
public:
SmallString<0> archiveName;
mutable SmallString<0> toStringCache;
private:
mutable SmallString<0> nameForScriptCache;
};
class ELFFileBase : public InputFile {
public:
ELFFileBase(Kind k, MemoryBufferRef m);
static bool classof(const InputFile *f) { return f->isElf(); }
template <typename ELFT> llvm::object::ELFFile<ELFT> getObj() const {
return check(llvm::object::ELFFile<ELFT>::create(mb.getBuffer()));
}
StringRef getStringTable() const { return stringTable; }
ArrayRef<Symbol *> getLocalSymbols() {
if (symbols.empty())
return {};
return llvm::makeArrayRef(symbols).slice(1, firstGlobal - 1);
}
ArrayRef<Symbol *> getGlobalSymbols() {
return llvm::makeArrayRef(symbols).slice(firstGlobal);
}
MutableArrayRef<Symbol *> getMutableGlobalSymbols() {
return llvm::makeMutableArrayRef(symbols.data(), symbols.size())
.slice(firstGlobal);
}
template <typename ELFT> typename ELFT::ShdrRange getELFShdrs() const {
return typename ELFT::ShdrRange(
reinterpret_cast<const typename ELFT::Shdr *>(elfShdrs), numELFShdrs);
}
template <typename ELFT> typename ELFT::SymRange getELFSyms() const {
return typename ELFT::SymRange(
reinterpret_cast<const typename ELFT::Sym *>(elfSyms), numELFSyms);
}
template <typename ELFT> typename ELFT::SymRange getGlobalELFSyms() const {
return getELFSyms<ELFT>().slice(firstGlobal);
}
protected:
template <typename ELFT> void init();
StringRef stringTable;
const void *elfShdrs = nullptr;
const void *elfSyms = nullptr;
uint32_t numELFShdrs = 0;
uint32_t numELFSyms = 0;
uint32_t firstGlobal = 0;
public:
uint32_t andFeatures = 0;
bool hasCommonSyms = false;
};
template <class ELFT> class ObjFile : public ELFFileBase {
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
public:
static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
llvm::object::ELFFile<ELFT> getObj() const {
return this->ELFFileBase::getObj<ELFT>();
}
ObjFile(MemoryBufferRef m, StringRef archiveName) : ELFFileBase(ObjKind, m) {
this->archiveName = archiveName;
}
void parse(bool ignoreComdats = false);
void parseLazy();
StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
const Elf_Shdr &sec);
Symbol &getSymbol(uint32_t symbolIndex) const {
if (symbolIndex >= this->symbols.size())
fatal(toString(this) + ": invalid symbol index");
return *this->symbols[symbolIndex];
}
uint32_t getSectionIndex(const Elf_Sym &sym) const;
template <typename RelT> Symbol &getRelocTargetSym(const RelT &rel) const {
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
return getSymbol(symIndex);
}
llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef name);
StringRef sourceFile;
const Elf_Shdr *addrsigSec = nullptr;
uint32_t cgProfileSectionIndex = 0;
uint32_t mipsGp0 = 0;
bool splitStack = false;
bool someNoSplitStack = false;
DWARFCache *getDwarf();
void initializeLocalSymbols();
void postParse();
private:
void initializeSections(bool ignoreComdats,
const llvm::object::ELFFile<ELFT> &obj);
void initializeSymbols(const llvm::object::ELFFile<ELFT> &obj);
void initializeJustSymbols();
InputSectionBase *getRelocTarget(uint32_t idx, const Elf_Shdr &sec,
uint32_t info);
InputSectionBase *createInputSection(uint32_t idx, const Elf_Shdr &sec,
StringRef name);
bool shouldMerge(const Elf_Shdr &sec, StringRef name);
ArrayRef<Elf_Word> shndxTable;
std::unique_ptr<SymbolUnion[]> localSymStorage;
std::unique_ptr<DWARFCache> dwarf;
llvm::once_flag initDwarf;
};
class BitcodeFile : public InputFile {
public:
BitcodeFile(MemoryBufferRef m, StringRef archiveName,
uint64_t offsetInArchive, bool lazy, bool ExportSym);
static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
template <class ELFT> void parse();
void parseLazy();
void postParse();
std::unique_ptr<llvm::lto::InputFile> obj;
std::vector<bool> keptComdats;
bool ExportSymbols;
};
class SharedFile : public ELFFileBase {
public:
SharedFile(MemoryBufferRef m, StringRef defaultSoName)
: ELFFileBase(SharedKind, m), soName(defaultSoName),
isNeeded(!config->asNeeded) {}
SmallVector<const void *, 0> verdefs;
SmallVector<uint32_t, 0> vernauxs;
static unsigned vernauxNum;
SmallVector<StringRef, 0> dtNeeded;
StringRef soName;
static bool classof(const InputFile *f) { return f->kind() == SharedKind; }
template <typename ELFT> void parse();
bool isNeeded;
SmallVector<Symbol *, 0> requiredSymbols;
private:
template <typename ELFT>
std::vector<uint32_t> parseVerneed(const llvm::object::ELFFile<ELFT> &obj,
const typename ELFT::Shdr *sec);
};
class BinaryFile : public InputFile {
public:
explicit BinaryFile(MemoryBufferRef m) : InputFile(BinaryKind, m) {}
static bool classof(const InputFile *f) { return f->kind() == BinaryKind; }
void parse();
};
ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
bool lazy = false);
std::string replaceThinLTOSuffix(StringRef path);
}
}
#endif