diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -353,7 +353,7 @@ if (nonZeroFiller) fill(buf, sections.empty() ? size : sections[0]->outSecOff, filler); - parallelForEachN(0, sections.size(), [&](size_t i) { + for (size_t i = 0, end = sections.size(); i != end; ++i) { InputSection *isec = sections[i]; isec->writeTo(buf); @@ -371,7 +371,7 @@ } else fill(start, end - start, filler); } - }); + } // Linker scripts may have BYTE()-family commands with which you // can write arbitrary bytes to the output. Process them if any. diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2881,17 +2881,31 @@ // Write section contents to a mmap'ed file. template void Writer::writeSections() { llvm::TimeTraceScope timeScope("Write sections"); + SmallVector vec; // In -r or --emit-relocs mode, write the relocation sections first as in // ELf_Rel targets we might find out that we need to modify the relocated // section while doing it. for (OutputSection *sec : outputSections) if (sec->type == SHT_REL || sec->type == SHT_RELA) - sec->writeTo(Out::bufferStart + sec->offset); + vec.push_back(sec); + llvm::sort(vec, [](OutputSection *a, OutputSection *b) { + return a->size > b->size; + }); + parallelForEach(vec, [](OutputSection *sec) { + sec->writeTo(Out::bufferStart + sec->offset); + }); + vec.clear(); for (OutputSection *sec : outputSections) if (sec->type != SHT_REL && sec->type != SHT_RELA) - sec->writeTo(Out::bufferStart + sec->offset); + vec.push_back(sec); + llvm::sort(vec, [](OutputSection *a, OutputSection *b) { + return a->size > b->size; + }); + parallelForEach(vec, [](OutputSection *sec) { + sec->writeTo(Out::bufferStart + sec->offset); + }); // Finally, check that all dynamic relocation addends were written correctly. if (config->checkDynamicRelocs && config->writeAddends) {