#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;
std::optional<MemoryBufferRef> readFile(StringRef path);
void parseFile(InputFile *file);
void parseFiles(const std::vector<InputFile *> &files,
InputFile *armCmseImpLib);
class InputFile {
protected:
std::unique_ptr<Symbol *[]> symbols;
uint32_t numSymbols = 0;
SmallVector<InputSectionBase *, 0> sections;
public:
enum Kind : uint8_t {
ObjKind,
SharedKind,
BitcodeKind,
BinaryKind,
InternalKind,
};
InputFile(Kind k, MemoryBufferRef m);
Kind kind() const { return fileKind; }
bool isElf() const {
Kind k = kind();
return k == ObjKind || k == SharedKind;
}
bool isInternal() const { return kind() == InternalKind; }
StringRef getName() const { return mb.getBufferIdentifier(); }
MemoryBufferRef mb;
ArrayRef<InputSectionBase *> getSections() const {
assert(fileKind == ObjKind || fileKind == BinaryKind);
return sections;
}
void cacheDecodedCrel(size_t i, InputSectionBase *s) { sections[i] = s; }
ArrayRef<Symbol *> getSymbols() const {
assert(fileKind == BinaryKind || fileKind == ObjKind ||
fileKind == BitcodeKind);
return {symbols.get(), numSymbols};
}
MutableArrayRef<Symbol *> getMutableSymbols() {
assert(fileKind == BinaryKind || fileKind == ObjKind ||
fileKind == BitcodeKind);
return {symbols.get(), numSymbols};
}
Symbol &getSymbol(uint32_t symbolIndex) const {
assert(fileKind == ObjKind);
if (symbolIndex >= numSymbols)
fatal(toString(this) + ": invalid symbol index");
return *this->symbols[symbolIndex];
}
template <typename RelT> Symbol &getRelocTargetSym(const RelT &rel) const {
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
return getSymbol(symIndex);
}
StringRef getNameForScript() const;
bool shouldExtractForCommon(StringRef name) const;
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, const InputSectionBase &sec,
uint64_t offset);
bool ppc64SmallCodeModelTocRelocs = false;
bool ppc64DisableTLSRelax = false;
public:
SmallString<0> archiveName;
mutable SmallString<0> toStringCache;
private:
mutable SmallString<0> nameForScriptCache;
};
class ELFFileBase : public InputFile {
public:
ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef m);
static bool classof(const InputFile *f) { return f->isElf(); }
void init();
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 (numSymbols == 0)
return {};
return llvm::ArrayRef(symbols.get() + 1, firstGlobal - 1);
}
ArrayRef<Symbol *> getGlobalSymbols() {
return llvm::ArrayRef(symbols.get() + firstGlobal,
numSymbols - firstGlobal);
}
MutableArrayRef<Symbol *> getMutableGlobalSymbols() {
return llvm::MutableArrayRef(symbols.get() + firstGlobal,
numSymbols - 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(InputFile::Kind k);
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;
ArrayRef<uint8_t> aarch64PauthAbiCoreInfo;
};
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(ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
: ELFFileBase(ObjKind, ekind, m) {
this->archiveName = archiveName;
}
void parse(bool ignoreComdats = false);
void parseLazy();
StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
const Elf_Shdr &sec);
uint32_t getSectionIndex(const Elf_Sym &sym) const;
std::optional<llvm::DILineInfo> getDILineInfo(const InputSectionBase *,
uint64_t);
std::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 initSectionsAndLocalSyms(bool ignoreComdats);
void postParse();
void importCmseSymbols();
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, 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<DWARFCache> dwarf;
llvm::once_flag initDwarf;
};
class BitcodeFile : public InputFile {
public:
BitcodeFile(MemoryBufferRef m, StringRef archiveName,
uint64_t offsetInArchive, bool lazy);
static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
void parse();
void parseLazy();
void postParse();
std::unique_ptr<llvm::lto::InputFile> obj;
std::vector<bool> keptComdats;
};
class SharedFile : public ELFFileBase {
public:
SharedFile(MemoryBufferRef m, StringRef defaultSoName);
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();
};
InputFile *createInternalFile(StringRef name);
ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
bool lazy = false);
std::string replaceThinLTOSuffix(StringRef path);
}
}
#endif