[lld] [lld][ELF] Skip finalizeAddressDependentContent if assignAddresses produces errors. (PR #75581)

Bevin Hansson via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 06:39:19 PST 2023


https://github.com/bevin-hansson updated https://github.com/llvm/llvm-project/pull/75581

>From 7d5c48729d60b165751a68cc9b05642f58cc1e5d Mon Sep 17 00:00:00 2001
From: Bevin Hansson <bevin.hansson at ericsson.com>
Date: Fri, 15 Dec 2023 10:37:42 +0100
Subject: [PATCH 1/2] [lld][ELF] Skip finalizeAddressDependentContent if
 assignAddresses produces errors.

There's no point in running the rest of finalizeAddressDependentContent
if the initial assignAddresses gives an error. Since we will also end
up running it again at least once, we might emit duplicate errors for
no reason.

Remember how many errors we had before we run assignAddresses the
first time, and if it changes, simply return.
---
 lld/ELF/Writer.cpp                           | 4 ++++
 lld/test/ELF/linkerscript/symbolreferenced.s | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index a84e4864ab0e5a..9a538e0ce54253 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1679,7 +1679,11 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
   ThunkCreator tc;
   AArch64Err843419Patcher a64p;
   ARMErr657417Patcher a32p;
+  unsigned errors = errorHandler().errorCount;
   script->assignAddresses();
+  // Exit out early if the first assignAddresses produced errors.
+  if (errors != errorHandler().errorCount)
+    return;
   // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
   // do require the relative addresses of OutputSections because linker scripts
   // can assign Virtual Addresses to OutputSections that are not monotonically
diff --git a/lld/test/ELF/linkerscript/symbolreferenced.s b/lld/test/ELF/linkerscript/symbolreferenced.s
index ba7a7721ea9697..d8ea9be80aa5d5 100644
--- a/lld/test/ELF/linkerscript/symbolreferenced.s
+++ b/lld/test/ELF/linkerscript/symbolreferenced.s
@@ -28,7 +28,7 @@
 # CHECK-NEXT: 0000000000001000 A newsym
 
 # RUN: not ld.lld -T chain2.t a.o 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
-# ERR-COUNT-3: error: chain2.t:1: symbol not found: undef
+# ERR-COUNT-2: error: chain2.t:1: symbol not found: undef
 
 #--- a.s
 .global _start

>From f7bf2a7b500830d07b676eb8bedc75cf067bce34 Mon Sep 17 00:00:00 2001
From: Bevin Hansson <bevin.hansson at ericsson.com>
Date: Mon, 18 Dec 2023 15:10:01 +0100
Subject: [PATCH 2/2] Add a skip to processSymbolAssignments as well.

---
 lld/ELF/LinkerScript.cpp                              | 2 ++
 lld/test/ELF/linkerscript/copy-rel-symbol-value-err.s | 2 +-
 lld/test/ELF/linkerscript/symbol-assignexpr.s         | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 28ae4b8543062f..a3904500b1f4a9 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -677,6 +677,8 @@ void LinkerScript::processSectionCommands() {
 }
 
 void LinkerScript::processSymbolAssignments() {
+  if (errorCount())
+    return;
   // Dot outside an output section still represents a relative address, whose
   // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
   // that fills the void outside a section. It has an index of one, which is
diff --git a/lld/test/ELF/linkerscript/copy-rel-symbol-value-err.s b/lld/test/ELF/linkerscript/copy-rel-symbol-value-err.s
index 70ca582affc462..a1a4cff981ee72 100644
--- a/lld/test/ELF/linkerscript/copy-rel-symbol-value-err.s
+++ b/lld/test/ELF/linkerscript/copy-rel-symbol-value-err.s
@@ -5,7 +5,7 @@
 # RUN: echo "SECTIONS { . = . + SIZEOF_HEADERS; foo = bar; }" > %t.script
 # RUN: not ld.lld %t.o %t2.so --script %t.script -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
 
-# CHECK-COUNT-2: error: {{.*}}.script:1: symbol not found: bar
+# CHECK: error: {{.*}}.script:1: symbol not found: bar
 
 .global _start
 _start:
diff --git a/lld/test/ELF/linkerscript/symbol-assignexpr.s b/lld/test/ELF/linkerscript/symbol-assignexpr.s
index 7f1d9b702d0579..fe429ab78a63d5 100644
--- a/lld/test/ELF/linkerscript/symbol-assignexpr.s
+++ b/lld/test/ELF/linkerscript/symbol-assignexpr.s
@@ -8,7 +8,7 @@
 # RUN: not ld.lld -o /dev/null --noinhibit-exec -T %t2.script %t.o 2>&1 \
 # RUN:   | FileCheck --check-prefix=ERR %s --implicit-check-not=error:
 
-# ERR-COUNT-3: {{.*}}.script:1: symbol not found: symbol
+# ERR-COUNT-2: {{.*}}.script:1: symbol not found: symbol
 
 # MAP:      VMA              LMA     Size Align Out     In      Symbol
 # MAP-NEXT:   0                0        0     1 symbol2 = symbol



More information about the llvm-commits mailing list