diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -601,6 +601,13 @@ // It does not make sense try to open the file if we have error already. if (errorCount()) return; + + // Handle --print-map(-M)/--Map and --cref options. Dump them before + // openFile() because the files may be useful in case openFile() fails, for + // example, due to an erroneous filesize. + writeMapFile(); + writeCrossReferenceTable(); + // Write the result down to a file. openFile(); if (errorCount()) @@ -621,12 +628,6 @@ if (errorCount()) return; - // Handle -Map and -cref options. - writeMapFile(); - writeCrossReferenceTable(); - if (errorCount()) - return; - if (auto e = buffer->commit()) error("failed to write to the output file: " + toString(std::move(e))); } diff --git a/lld/test/ELF/linkerscript/output-too-large.s b/lld/test/ELF/linkerscript/output-too-large.s --- a/lld/test/ELF/linkerscript/output-too-large.s +++ b/lld/test/ELF/linkerscript/output-too-large.s @@ -4,11 +4,16 @@ # RUN: echo "SECTIONS { .text : { . = 0xffffffff; *(.text*); } }" > %t.script # RUN: not ld.lld --no-check-sections --script %t.script %t.o -o /dev/null 2>&1 | FileCheck %s +# CHECK: error: output file too large + # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o # RUN: echo "SECTIONS { .text : { . = 0x8fffffffffffffff; *(.text*); } }" > %t.script -# RUN: not ld.lld --no-check-sections --script %t.script %t.o -o /dev/null 2>&1 | FileCheck %s +# RUN: not ld.lld --no-check-sections --script %t.script -M %t.o -o /dev/null 2>&1 | FileCheck --check-prefix=MAP %s -# CHECK: error: output file too large +# MAP: VMA LMA Size Align Out In Symbol +# MAP: 9000000000000000 9000000000000000 1 4 {{.*}}.o:(.text) +# MAP-NEXT: 9000000000000000 9000000000000000 0 1 _start +# MAP: error: output file too large .global _start _start: