[lld] eb4b5a3 - [ELF] Move --print-map(-M)/--cref before checkSections() and openFile()

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 08:00:27 PDT 2020


Author: Fangrui Song
Date: 2020-03-12T08:00:18-07:00
New Revision: eb4b5a36a6331a0de3559a01e6854895cacce6b3

URL: https://github.com/llvm/llvm-project/commit/eb4b5a36a6331a0de3559a01e6854895cacce6b3
DIFF: https://github.com/llvm/llvm-project/commit/eb4b5a36a6331a0de3559a01e6854895cacce6b3.diff

LOG: [ELF] Move --print-map(-M)/--cref before checkSections() and openFile()

-M output can be useful when diagnosing an "error: output file too large" problem (emitted in openFile()).

I just ran into such a situation where I had to debug an erronerous
Linux kernel linker script. It tried to create a file larger than
INT64_MAX bytes.

This patch could have helped https://bugs.llvm.org/show_bug.cgi?id=44715 as well.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D75966

Added: 
    

Modified: 
    lld/ELF/Writer.cpp
    lld/test/ELF/linkerscript/output-too-large.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index d6678ac2548d..c916f35cc2b4 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -595,6 +595,12 @@ template <class ELFT> void Writer<ELFT>::run() {
     for (OutputSection *sec : outputSections)
       sec->addr = 0;
 
+  // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
+  // because the files may be useful in case checkSections() or openFile()
+  // fails, for example, due to an erroneous file size.
+  writeMapFile();
+  writeCrossReferenceTable();
+
   if (config->checkSections)
     checkSections();
 
@@ -621,12 +627,6 @@ template <class ELFT> void Writer<ELFT>::run() {
   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
index a5130d27a070..d916a2e556bc 100644
--- a/lld/test/ELF/linkerscript/output-too-large.s
+++ b/lld/test/ELF/linkerscript/output-too-large.s
@@ -1,12 +1,31 @@
 # REQUIRES: x86
 
-# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
-# 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
+# RUN: llvm-mc -filetype=obj -triple=i686 %s -o %t1.o
+# RUN: echo "SECTIONS { .text : { . = 0xffffffff; *(.text*); } }" > %t1.script
+# RUN: not ld.lld --no-check-sections -T %t1.script %t1.o -o /dev/null 2>&1 | FileCheck %s
 
-# 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
+## Error if an address is greater than or equal to 2**32 for ELF32.
+## When -M is specified, print the link map even if such an error occurs,
+## because the link map can help diagnose problems.
+# RUN: not ld.lld -T %t1.script %t1.o -M -o /dev/null 2>&1 | \
+# RUN:   FileCheck --check-prefix=MAP1 %s
+
+# MAP1:           VMA      LMA     Size Align Out     In      Symbol
+# MAP1-NEXT:        0        0 100000001     4 .text
+# MAP1-NEXT:        0        0 ffffffff     1         . = 0xffffffff
+# MAP1-NEXT: 100000000 100000000        1     4         {{.*}}.o:(.text)
+# MAP1:      error: section .text at 0x0 of size 0x100000001 exceeds available address space
+
+## Error if an address is greater than or equal to 2**63.
+## Print a link map if -M is specified.
+# 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
+
+# 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
 


        


More information about the llvm-commits mailing list