From 8e0b17931530e84f45586e31b58b031d6d68ee6c Mon Sep 17 00:00:00 2001 From: Bob Haarman Date: Tue, 12 Jan 2021 20:55:18 +0000 Subject: [PATCH] [ELF] report section sizes when output file too large Fixes PR48523. When the linker errors with "output file too large", one question that comes to mind is how the section sizes differ from what they were previously. Unfortunately, this information is lost when the linker exits without writing the output file. This change makes it so that the error message includes the sizes of the largest sections. Reviewed By: MaskRay, grimar, jhenderson Differential Revision: https://reviews.llvm.org/D94560 --- lld/ELF/Writer.cpp | 8 +++++++- lld/test/ELF/linkerscript/output-too-large.s | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 0397dae8f891..f550e6a73335 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -2872,7 +2872,13 @@ template void Writer::writeHeader() { template void Writer::openFile() { uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX; if (fileSize != size_t(fileSize) || maxSize < fileSize) { - error("output file too large: " + Twine(fileSize) + " bytes"); + std::string msg; + raw_string_ostream s(msg); + s << "output file too large: " << Twine(fileSize) << " bytes\n" + << "section sizes:\n"; + for (OutputSection *os : outputSections) + s << os->name << ' ' << os->size << "\n"; + error(s.str()); return; } diff --git a/lld/test/ELF/linkerscript/output-too-large.s b/lld/test/ELF/linkerscript/output-too-large.s index d916a2e556bc..c496d99d3cef 100644 --- a/lld/test/ELF/linkerscript/output-too-large.s +++ b/lld/test/ELF/linkerscript/output-too-large.s @@ -21,14 +21,26 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t2.o # RUN: echo "SECTIONS { .text : { . = 0x8fffffffffffffff; *(.text*); } }" > %t2.script # RUN: not ld.lld -T %t2.script -M %t2.o -o /dev/null 2>&1 | \ -# RUN: FileCheck --check-prefixes=MAP2,CHECK %s +# RUN: FileCheck --check-prefixes=MAP2,CHECK64 %s # MAP2: VMA LMA Size Align Out In Symbol # MAP2: 9000000000000000 9000000000000000 1 4 {{.*}}.o:(.text) # MAP2-NEXT: 9000000000000000 9000000000000000 0 1 _start # CHECK: error: output file too large +# CHECK-NEXT: section sizes: +# CHECK-NEXT: .text 4294967297 +# CHECK-NEXT: .data 1 +# CHECK64: error: output file too large +# CHECK64-NEXT: section sizes: +# CHECK64-NEXT: .text 10376293541461622785 +# CHECK64-NEXT: .data 1 + +.data +.byte 42 + +.text .global _start _start: nop