diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 8d529f2bdb9f9..e8a7b19a95ee4 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1338,9 +1338,11 @@ static bool compareByFilePosition(InputSection *a, InputSection *b) { OutputSection *aOut = la->getParent(); OutputSection *bOut = lb->getParent(); - if (aOut != bOut) - return aOut->addr < bOut->addr; - return la->outSecOff < lb->outSecOff; + if (aOut == bOut) + return la->outSecOff < lb->outSecOff; + if (aOut->addr == bOut->addr) + return aOut->sectionIndex < bOut->sectionIndex; + return aOut->addr < bOut->addr; } template void Writer::resolveShfLinkOrder() {