diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 37b51ce19240..d4577c2f41d6 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -587,8 +587,7 @@ template void LinkerScript::assignAddresses() { Sec->setVA(0); } - uintX_t HeaderSize = - Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + uintX_t HeaderSize = getHeaderSize(); if (HeaderSize > MinVA) fatal("Not enough space for ELF and program headers"); @@ -723,7 +722,7 @@ uint64_t LinkerScript::getOutputSectionAlign(StringRef Name) { } template uint64_t LinkerScript::getHeaderSize() { - return Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + return elf::getHeaderSize(); } template uint64_t LinkerScript::getSymbolValue(StringRef S) { diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index bf799e27e028..fedda160f702 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -812,6 +812,12 @@ private: llvm::SmallDenseMap *> Map; }; +template uint64_t getHeaderSize() { + if (Config->OFormatBinary) + return 0; + return Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); +} + template BuildIdSection *Out::BuildId; template DynamicSection *Out::Dynamic; template EhFrameHeader *Out::EhFrameHdr; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 6cdf0978e5ac..e42240fdd830 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1148,11 +1148,7 @@ template void Writer::fixHeaders() { // Assign VAs (addresses at run-time) to output sections. template void Writer::assignAddresses() { - uintX_t VA = Config->ImageBase; - if (!Config->OFormatBinary) - VA += - Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); - + uintX_t VA = Config->ImageBase + getHeaderSize(); uintX_t ThreadBssOffset = 0; for (OutputSectionBase *Sec : OutputSections) { uintX_t Alignment = Sec->getAlignment(); diff --git a/lld/test/ELF/oformat-binary.s b/lld/test/ELF/oformat-binary.s index 3f6a2497d006..acd95c7cef36 100644 --- a/lld/test/ELF/oformat-binary.s +++ b/lld/test/ELF/oformat-binary.s @@ -11,6 +11,10 @@ # RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary # RUN: od -t x1 -v %t2.out | FileCheck %s +# RUN: echo "SECTIONS { }" > %t.script +# RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary +# RUN: od -t x1 -v %t2.out | FileCheck %s + # RUN: not ld.lld -o %t3.out %t --oformat foo 2>&1 \ # RUN: | FileCheck %s --check-prefix ERR # ERR: unknown --oformat value: foo