diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 2fa2ff433a0c..15acaf01984b 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -101,8 +101,12 @@ static void assignSymbol(SymbolAssignment *Cmd, typename ELFT::uint Dot = 0) { if (auto *Body = dyn_cast(Cmd->Sym)) { Body->Section = Cmd->Expression.Section(); - if (Body->Section) - Body->Value = Cmd->Expression(Dot) - Body->Section->Addr; + if (Body->Section) { + uint64_t VA = 0; + if (Body->Section->Flags & SHF_ALLOC) + VA = Body->Section->Addr; + Body->Value = Cmd->Expression(Dot) - VA; + } return; } @@ -802,9 +806,12 @@ void LinkerScript::assignAddresses(std::vector &Phdrs) { } uintX_t MinVA = std::numeric_limits::max(); - for (OutputSectionBase *Sec : *OutputSections) + for (OutputSectionBase *Sec : *OutputSections) { if (Sec->Flags & SHF_ALLOC) MinVA = std::min(MinVA, Sec->Addr); + else + Sec->Addr = 0; + } allocateHeaders(Phdrs, *OutputSections, MinVA); } diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 6a8ab55b2d59..6de30447913c 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -57,7 +57,7 @@ void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) { Shdr->sh_flags = Flags; Shdr->sh_info = Info; Shdr->sh_link = Link; - Shdr->sh_addr = (Flags & SHF_ALLOC) ? Addr : 0; + Shdr->sh_addr = Addr; Shdr->sh_size = Size; Shdr->sh_name = ShName; }