From: Fangrui Song Date: Sun, 28 Nov 2021 21:44:41 +0000 (-0800) Subject: [ELF] Simplify assignFileOffsets X-Git-Tag: upstream/15.0.7~24643 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cecc6893a08618fc753cd55893b720a01fbd2b51;p=platform%2Fupstream%2Fllvm.git [ELF] Simplify assignFileOffsets There is a difference with non-SHF_ALLOC SHT_NOBITS when off%sh_addralign!=0 which doesn't happen/matter in practice. --- diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index dce928319b09..fc18e4f45be9 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2549,17 +2549,6 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) { return first->offset + os->addr - first->addr; } -// Set an in-file position to a given section and returns the end position of -// the section. -static uint64_t setFileOffset(OutputSection *os, uint64_t off) { - off = computeFileOffset(os, off); - os->offset = off; - - if (os->type == SHT_NOBITS) - return off; - return off + os->size; -} - template void Writer::assignFileOffsetsBinary() { // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr. auto needsOffset = [](OutputSection &sec) { @@ -2601,7 +2590,10 @@ template void Writer::assignFileOffsets() { for (OutputSection *sec : outputSections) { if (!(sec->flags & SHF_ALLOC)) continue; - off = setFileOffset(sec, off); + off = computeFileOffset(sec, off); + sec->offset = off; + if (sec->type != SHT_NOBITS) + off += sec->size; // If this is a last section of the last executable segment and that // segment is the last loadable segment, align the offset of the @@ -2610,9 +2602,11 @@ template void Writer::assignFileOffsets() { lastRX->lastSec == sec) off = alignTo(off, config->maxPageSize); } - for (OutputSection *sec : outputSections) - if (!(sec->flags & SHF_ALLOC)) - off = setFileOffset(sec, off); + for (OutputSection *osec : outputSections) + if (!(osec->flags & SHF_ALLOC)) { + osec->offset = alignTo(off, osec->alignment); + off = osec->offset + osec->size; + } sectionHeaderOff = alignTo(off, config->wordsize); fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);