From fa4d1860d20a5afa6f96673ba02a99f09f69869c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 19 Nov 2024 21:59:47 -0800 Subject: [PATCH] [ELF] Move PhdrEntry to SyntheticSections The next change will change Partition::phdrs to a unique_ptr vector, which requires PhdrEntry to be a complete type. And make OutputSection::getLMA out-of-line, since it should not include either SyntheticSections.h or Writer.h. --- lld/ELF/OutputSections.cpp | 4 ++++ lld/ELF/OutputSections.h | 2 +- lld/ELF/SyntheticSections.h | 25 +++++++++++++++++++++++++ lld/ELF/Writer.h | 28 ---------------------------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 94cf62d79abb..9bcbea250e7d 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -72,6 +72,10 @@ OutputSection::OutputSection(Ctx &ctx, StringRef name, uint32_t type, /*info=*/0, /*link=*/0), ctx(ctx) {} +uint64_t OutputSection::getLMA() const { + return ptLoad ? addr + ptLoad->lmaOffset : addr; +} + // We allow sections of types listed below to merged into a // single progbits section. This is typically done by linker // scripts. Merging nobits and progbits will force disk space diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index f8509d5a7aaa..67191392d1db 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -41,7 +41,7 @@ public: return s->kind() == SectionBase::Output; } - uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; } + uint64_t getLMA() const; template void writeHeaderTo(typename ELFT::Shdr *sHdr); Ctx &ctx; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 9f78bd3a3483..bfe318fb10d2 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -1439,6 +1439,31 @@ Defined *addSyntheticLocal(Ctx &ctx, StringRef name, uint8_t type, void addVerneed(Ctx &, Symbol &ss); +// This describes a program header entry. +// Each contains type, access flags and range of output sections that will be +// placed in it. +struct PhdrEntry { + PhdrEntry(Ctx &ctx, unsigned type, unsigned flags) + : p_align(type == llvm::ELF::PT_LOAD ? ctx.arg.maxPageSize : 0), + p_type(type), p_flags(flags) {} + void add(OutputSection *sec); + + uint64_t p_paddr = 0; + uint64_t p_vaddr = 0; + uint64_t p_memsz = 0; + uint64_t p_filesz = 0; + uint64_t p_offset = 0; + uint32_t p_align = 0; + uint32_t p_type = 0; + uint32_t p_flags = 0; + + OutputSection *firstSec = nullptr; + OutputSection *lastSec = nullptr; + bool hasLMA = false; + + uint64_t lmaOffset = 0; +}; + // Linker generated per-partition sections. struct Partition { Ctx &ctx; diff --git a/lld/ELF/Writer.h b/lld/ELF/Writer.h index bd6efe9cde4a..b1072f61f725 100644 --- a/lld/ELF/Writer.h +++ b/lld/ELF/Writer.h @@ -10,40 +10,12 @@ #define LLD_ELF_WRITER_H #include "Config.h" -#include "llvm/ADT/StringRef.h" -#include namespace lld::elf { -class InputFile; class OutputSection; void copySectionsIntoPartitions(Ctx &ctx); template void writeResult(Ctx &ctx); -// This describes a program header entry. -// Each contains type, access flags and range of output sections that will be -// placed in it. -struct PhdrEntry { - PhdrEntry(Ctx &ctx, unsigned type, unsigned flags) - : p_align(type == llvm::ELF::PT_LOAD ? ctx.arg.maxPageSize : 0), - p_type(type), p_flags(flags) {} - void add(OutputSection *sec); - - uint64_t p_paddr = 0; - uint64_t p_vaddr = 0; - uint64_t p_memsz = 0; - uint64_t p_filesz = 0; - uint64_t p_offset = 0; - uint32_t p_align = 0; - uint32_t p_type = 0; - uint32_t p_flags = 0; - - OutputSection *firstSec = nullptr; - OutputSection *lastSec = nullptr; - bool hasLMA = false; - - uint64_t lmaOffset = 0; -}; - void addReservedSymbols(Ctx &ctx); bool includeInSymtab(Ctx &, const Symbol &); unsigned getSectionRank(Ctx &, OutputSection &osec);