[PATCH] D75966: [ELF] Move --print-map(-M)/--cref before openFile()

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 16:26:23 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, nickdesaulniers, psmith, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

-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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75966

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


Index: lld/test/ELF/linkerscript/output-too-large.s
===================================================================
--- lld/test/ELF/linkerscript/output-too-large.s
+++ 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:
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ 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)));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75966.249513.patch
Type: text/x-patch
Size: 2077 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200310/c3cbb76a/attachment.bin>


More information about the llvm-commits mailing list