Path: blob/main/contrib/llvm-project/lld/ELF/OutputSections.h
34878 views
//===- OutputSections.h -----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_ELF_OUTPUT_SECTIONS_H9#define LLD_ELF_OUTPUT_SECTIONS_H1011#include "InputSection.h"12#include "LinkerScript.h"13#include "lld/Common/LLVM.h"14#include "llvm/Support/Compiler.h"15#include "llvm/Support/Parallel.h"1617#include <array>1819namespace lld::elf {2021struct PhdrEntry;2223struct CompressedData {24std::unique_ptr<SmallVector<uint8_t, 0>[]> shards;25uint32_t type = 0;26uint32_t numShards = 0;27uint32_t checksum = 0;28uint64_t uncompressedSize;29};3031// This represents a section in an output file.32// It is composed of multiple InputSections.33// The writer creates multiple OutputSections and assign them unique,34// non-overlapping file offsets and VAs.35class OutputSection final : public SectionBase {36public:37OutputSection(StringRef name, uint32_t type, uint64_t flags);3839static bool classof(const SectionBase *s) {40return s->kind() == SectionBase::Output;41}4243uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; }44template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *sHdr);4546uint32_t sectionIndex = UINT32_MAX;47unsigned sortRank;4849uint32_t getPhdrFlags() const;5051// Pointer to the PT_LOAD segment, which this section resides in. This field52// is used to correctly compute file offset of a section. When two sections53// share the same load segment, difference between their file offsets should54// be equal to difference between their virtual addresses. To compute some55// section offset we use the following formula: Off = Off_first + VA -56// VA_first, where Off_first and VA_first is file offset and VA of first57// section in PT_LOAD.58PhdrEntry *ptLoad = nullptr;5960// Pointer to a relocation section for this section. Usually nullptr because61// we consume relocations, but if --emit-relocs is specified (which is rare),62// it may have a non-null value.63OutputSection *relocationSection = nullptr;6465// Initially this field is the number of InputSections that have been added to66// the OutputSection so far. Later on, after a call to assignAddresses, it67// corresponds to the Elf_Shdr member.68uint64_t size = 0;6970// The following fields correspond to Elf_Shdr members.71uint64_t offset = 0;72uint64_t addr = 0;73uint32_t shName = 0;7475void recordSection(InputSectionBase *isec);76void commitSection(InputSection *isec);77void finalizeInputSections(LinkerScript *script = nullptr);7879// The following members are normally only used in linker scripts.80MemoryRegion *memRegion = nullptr;81MemoryRegion *lmaRegion = nullptr;82Expr addrExpr;83Expr alignExpr;84Expr lmaExpr;85Expr subalignExpr;8687// Used by non-alloc SHT_CREL to hold the header and content byte stream.88uint64_t crelHeader = 0;89SmallVector<char, 0> crelBody;9091SmallVector<SectionCommand *, 0> commands;92SmallVector<StringRef, 0> phdrs;93std::optional<std::array<uint8_t, 4>> filler;94ConstraintKind constraint = ConstraintKind::NoConstraint;95std::string location;96std::string memoryRegionName;97std::string lmaRegionName;98bool nonAlloc = false;99bool typeIsSet = false;100bool expressionsUseSymbols = false;101bool usedInExpression = false;102bool inOverlay = false;103104// Tracks whether the section has ever had an input section added to it, even105// if the section was later removed (e.g. because it is a synthetic section106// that wasn't needed). This is needed for orphan placement.107bool hasInputSections = false;108109// The output section description is specified between DATA_SEGMENT_ALIGN and110// DATA_RELRO_END.111bool relro = false;112113template <bool is64> void finalizeNonAllocCrel();114void finalize();115template <class ELFT>116void writeTo(uint8_t *buf, llvm::parallel::TaskGroup &tg);117// Check that the addends for dynamic relocations were written correctly.118void checkDynRelAddends(const uint8_t *bufStart);119template <class ELFT> void maybeCompress();120121void sort(llvm::function_ref<int(InputSectionBase *s)> order);122void sortInitFini();123void sortCtorsDtors();124125// Used for implementation of --compress-debug-sections and126// --compress-sections.127CompressedData compressed;128129private:130SmallVector<InputSection *, 0> storage;131132std::array<uint8_t, 4> getFiller();133};134135struct OutputDesc final : SectionCommand {136OutputSection osec;137OutputDesc(StringRef name, uint32_t type, uint64_t flags)138: SectionCommand(OutputSectionKind), osec(name, type, flags) {}139140static bool classof(const SectionCommand *c) {141return c->kind == OutputSectionKind;142}143};144145int getPriority(StringRef s);146147InputSection *getFirstInputSection(const OutputSection *os);148llvm::ArrayRef<InputSection *>149getInputSections(const OutputSection &os,150SmallVector<InputSection *, 0> &storage);151152// All output sections that are handled by the linker specially are153// globally accessible. Writer initializes them, so don't use them154// until Writer is initialized.155struct Out {156static uint8_t *bufferStart;157static PhdrEntry *tlsPhdr;158static OutputSection *elfHeader;159static OutputSection *programHeaders;160static OutputSection *preinitArray;161static OutputSection *initArray;162static OutputSection *finiArray;163};164165uint64_t getHeaderSize();166167LLVM_LIBRARY_VISIBILITY extern llvm::SmallVector<OutputSection *, 0>168outputSections;169} // namespace lld::elf170171#endif172173174