Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -440,9 +440,6 @@ // For each OutputSection that needs a VA fabricate an OutputSectionCommand // with an InputSectionDescription describing the InputSections for (OutputSection *Sec : *OutputSections) { - if (!(Sec->Flags & SHF_ALLOC)) - continue; - auto *OSCmd = make(Sec->Name); OSCmd->Sec = Sec; SecToCommand[Sec] = OSCmd; Index: ELF/MapFile.h =================================================================== --- ELF/MapFile.h +++ ELF/MapFile.h @@ -14,9 +14,8 @@ namespace lld { namespace elf { -class OutputSection; -template -void writeMapFile(llvm::ArrayRef OutputSections); +struct BaseCommand; +template void writeMapFile(llvm::ArrayRef Script); } } Index: ELF/MapFile.cpp =================================================================== --- ELF/MapFile.cpp +++ ELF/MapFile.cpp @@ -21,6 +21,7 @@ #include "MapFile.h" #include "InputFiles.h" +#include "LinkerScript.h" #include "OutputSections.h" #include "Strings.h" #include "SymbolTable.h" @@ -99,7 +100,7 @@ } template -void elf::writeMapFile(ArrayRef OutputSections) { +void elf::writeMapFile(llvm::ArrayRef Script) { if (Config->MapFile.empty()) return; @@ -122,7 +123,11 @@ << " Align Out In Symbol\n"; // Print out file contents. - for (OutputSection *OSec : OutputSections) { + for (BaseCommand *Base : Script) { + auto *Cmd = dyn_cast(Base); + if (!Cmd) + continue; + OutputSection *OSec = Cmd->Sec; writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment); OS << OSec->Name << '\n'; @@ -137,7 +142,7 @@ } } -template void elf::writeMapFile(ArrayRef); -template void elf::writeMapFile(ArrayRef); -template void elf::writeMapFile(ArrayRef); -template void elf::writeMapFile(ArrayRef); +template void elf::writeMapFile(ArrayRef); +template void elf::writeMapFile(ArrayRef); +template void elf::writeMapFile(ArrayRef); +template void elf::writeMapFile(ArrayRef); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -288,8 +288,13 @@ if (ErrorCount) return; + // Clear the OutputSections to make sure it is not used anymore. Any + // code from this point on should be using the linker script + // commands. + OutputSections.clear(); + // Handle -Map option. - writeMapFile(OutputSections); + writeMapFile(Script->Opt.Commands); if (ErrorCount) return;