[lld] 5014d6f - [ELF] -Map --why-extract=: print despite errors

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 12 10:40:38 PST 2022


Author: Fangrui Song
Date: 2022-01-12T10:40:33-08:00
New Revision: 5014d6fc53d8208cf5940fb1ccab62e78ee2f146

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

LOG: [ELF] -Map --why-extract=: print despite errors

Fix https://github.com/llvm/llvm-project/issues/53073

In case of a relocation error, GNU ld's link map includes
the archive member extraction information but not output sections.

Our -Map and --why-extract= are currently no-op in case of an error.
This change makes the two options work.

Reviewed By: ikudrin, peter.smith

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

Added: 
    

Modified: 
    lld/ELF/Writer.cpp
    lld/test/ELF/map-file.s
    lld/test/ELF/why-extract.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index d02584e9471bd..e51e1c432722e 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -538,8 +538,6 @@ template <class ELFT> void Writer<ELFT>::run() {
   // finalizeSections does that.
   finalizeSections();
   checkExecuteOnly();
-  if (errorCount())
-    return;
 
   // If --compressed-debug-sections is specified, compress .debug_* sections.
   // Do it right now because it changes the size of output sections.
@@ -1998,10 +1996,6 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
     }
   }
 
-  // Do not proceed if there was an undefined symbol.
-  if (errorCount())
-    return;
-
   if (in.mipsGot)
     in.mipsGot->build();
 
@@ -2134,6 +2128,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
   //    sometimes using forward symbol declarations. We want to set the correct
   //    values. They also might change after adding the thunks.
   finalizeAddressDependentContent();
+
+  // All information needed for OutputSection part of Map file is available.
   if (errorCount())
     return;
 

diff  --git a/lld/test/ELF/map-file.s b/lld/test/ELF/map-file.s
index 726334a3921c6..4f6cd320ced68 100644
--- a/lld/test/ELF/map-file.s
+++ b/lld/test/ELF/map-file.s
@@ -6,6 +6,7 @@
 # RUN: echo '.global baz; baz: ret' | llvm-mc -filetype=obj -triple=x86_64 - -o %t4.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64 %p/Inputs/map-file5.s -o %t5.o
 # RUN: echo '.global hey; hey: ret' | llvm-mc -filetype=obj -triple=x86_64 - -o %t6.o
+# RUN: echo '.reloc ., R_X86_64_RELATIVE, 0' | llvm-mc -filetype=obj -triple=x86_64 - -o %t7.o
 # RUN: ld.lld -shared %t5.o -o %t5.so -soname dso
 # RUN: rm -f %t4.a
 # RUN: llvm-ar rc %t4.a %t4.o
@@ -16,6 +17,9 @@
 # RUN: ld.lld %t1.o %t2.o %t3.o %t4.a %t5.so %t6.a -o %t -Map=%t.map
 # RUN: FileCheck -match-full-lines -strict-whitespace %s < %t.map
 
+## A relocation error does not suppress the output.
+# RUN: not ld.lld %t1.o %t2.o %t3.o %t4.a %t5.so %t6.a %t7.o -o /dev/null -M | FileCheck --strict-whitespace --check-prefix=CHECK2 %s
+
 .global _start
 _start:
 .cfi_startproc
@@ -102,6 +106,12 @@ labs = 0x1AB5
 # CHECK-NEXT:               0                0       71     1 .strtab
 # CHECK-NEXT:               0                0       71     1         <internal>:(.strtab)
 
+# CHECK2:                 VMA              LMA     Size Align Out     In      Symbol
+# CHECK2-NEXT:         200200           200200       78     8 .dynsym
+# CHECK2-NEXT:         200200           200200       78     8         <internal>:(.dynsym)
+# CHECK2-NEXT:         200278           200278       2c     8 .gnu.hash
+# CHECK2-NEXT:         200278           200278       2c     8         <internal>:(.gnu.hash)
+
 # RUN: not ld.lld %t1.o %t2.o %t3.o %t4.a -o /dev/null -Map=/ 2>&1 \
 # RUN:  | FileCheck --check-prefix=FAIL %s
 # FAIL: cannot open map file /

diff  --git a/lld/test/ELF/why-extract.s b/lld/test/ELF/why-extract.s
index c5b5784c4a0fb..3b3a11f28cd74 100644
--- a/lld/test/ELF/why-extract.s
+++ b/lld/test/ELF/why-extract.s
@@ -5,6 +5,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a_b.s -o %t/a_b.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64 %t/b.s -o %t/b.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/err.s -o %t/err.o
 # RUN: llvm-ar rc %t/a.a %t/a.o
 # RUN: llvm-ar rc %t/a_b.a %t/a_b.o
 # RUN: llvm-ar rc %t/b.a %t/b.o
@@ -21,6 +22,10 @@
 # RUN: ld.lld main.o a_b.a b.a -o /dev/null --why-extract=why2.txt
 # RUN: FileCheck %s --input-file=why2.txt --check-prefix=CHECK2 --match-full-lines --strict-whitespace
 
+## A relocation error does not suppress the output.
+# RUN: rm -f why2.txt && not ld.lld main.o a_b.a b.a err.o -o /dev/null --why-extract=why2.txt
+# RUN: FileCheck %s --input-file=why2.txt --check-prefix=CHECK2 --match-full-lines --strict-whitespace
+
 #      CHECK2:reference	extracted	symbol
 # CHECK2-NEXT:main.o	a_b.a(a_b.o)	a
 # CHECK2-NEXT:a_b.a(a_b.o)	b.a(b.o)	b()
@@ -84,3 +89,6 @@ _Z1bv:
 
 #--- a.lds
 a = _Z1bv;
+
+#--- err.s
+.reloc ., R_X86_64_RELATIVE, 0


        


More information about the llvm-commits mailing list