#ifndef LLD_ELF_OUTPUT_SECTIONS_H
#define LLD_ELF_OUTPUT_SECTIONS_H
#include "InputSection.h"
#include "LinkerScript.h"
#include "lld/Common/LLVM.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Parallel.h"
#include <array>
namespace lld::elf {
struct PhdrEntry;
struct CompressedData {
std::unique_ptr<SmallVector<uint8_t, 0>[]> shards;
uint32_t type = 0;
uint32_t numShards = 0;
uint32_t checksum = 0;
uint64_t uncompressedSize;
};
class OutputSection final : public SectionBase {
public:
OutputSection(StringRef name, uint32_t type, uint64_t flags);
static bool classof(const SectionBase *s) {
return s->kind() == SectionBase::Output;
}
uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; }
template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *sHdr);
uint32_t sectionIndex = UINT32_MAX;
unsigned sortRank;
uint32_t getPhdrFlags() const;
PhdrEntry *ptLoad = nullptr;
OutputSection *relocationSection = nullptr;
uint64_t size = 0;
uint64_t offset = 0;
uint64_t addr = 0;
uint32_t shName = 0;
void recordSection(InputSectionBase *isec);
void commitSection(InputSection *isec);
void finalizeInputSections(LinkerScript *script = nullptr);
MemoryRegion *memRegion = nullptr;
MemoryRegion *lmaRegion = nullptr;
Expr addrExpr;
Expr alignExpr;
Expr lmaExpr;
Expr subalignExpr;
uint64_t crelHeader = 0;
SmallVector<char, 0> crelBody;
SmallVector<SectionCommand *, 0> commands;
SmallVector<StringRef, 0> phdrs;
std::optional<std::array<uint8_t, 4>> filler;
ConstraintKind constraint = ConstraintKind::NoConstraint;
std::string location;
std::string memoryRegionName;
std::string lmaRegionName;
bool nonAlloc = false;
bool typeIsSet = false;
bool expressionsUseSymbols = false;
bool usedInExpression = false;
bool inOverlay = false;
bool hasInputSections = false;
bool relro = false;
template <bool is64> void finalizeNonAllocCrel();
void finalize();
template <class ELFT>
void writeTo(uint8_t *buf, llvm::parallel::TaskGroup &tg);
void checkDynRelAddends(const uint8_t *bufStart);
template <class ELFT> void maybeCompress();
void sort(llvm::function_ref<int(InputSectionBase *s)> order);
void sortInitFini();
void sortCtorsDtors();
CompressedData compressed;
private:
SmallVector<InputSection *, 0> storage;
std::array<uint8_t, 4> getFiller();
};
struct OutputDesc final : SectionCommand {
OutputSection osec;
OutputDesc(StringRef name, uint32_t type, uint64_t flags)
: SectionCommand(OutputSectionKind), osec(name, type, flags) {}
static bool classof(const SectionCommand *c) {
return c->kind == OutputSectionKind;
}
};
int getPriority(StringRef s);
InputSection *getFirstInputSection(const OutputSection *os);
llvm::ArrayRef<InputSection *>
getInputSections(const OutputSection &os,
SmallVector<InputSection *, 0> &storage);
struct Out {
static uint8_t *bufferStart;
static PhdrEntry *tlsPhdr;
static OutputSection *elfHeader;
static OutputSection *programHeaders;
static OutputSection *preinitArray;
static OutputSection *initArray;
static OutputSection *finiArray;
};
uint64_t getHeaderSize();
LLVM_LIBRARY_VISIBILITY extern llvm::SmallVector<OutputSection *, 0>
outputSections;
}
#endif