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

Bevin Hansson via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 01:42:13 PST 2023


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

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.


>From 5674de6bb12b95f0863277ad7e2bb01b0c1c1840 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] [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



More information about the llvm-commits mailing list