Skip to content

[lld] Only report "unable to move location counter backward" error for the last run to assignAddresses #66840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

PiJoules
Copy link
Contributor

It's possible for sections to fit in a memory segment after other sections have had fixups and relaxations applied, so if this error occurs, we should only pay attention to the last call to assignAddresses which could have updated section addresses and sizes.

the last run to assignAddresses

It's possible for sections to fit in a memory segment after other
sections have had fixups and relaxations applied, so if this error
occurs, we should only pay attention to the last call to assignAddresses
which could have updated section addresses and sizes.
@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Changes

It's possible for sections to fit in a memory segment after other sections have had fixups and relaxations applied, so if this error occurs, we should only pay attention to the last call to assignAddresses which could have updated section addresses and sizes.


Full diff: https://github.com/llvm/llvm-project/pull/66840.diff

3 Files Affected:

  • (modified) lld/ELF/LinkerScript.cpp (+2-2)
  • (modified) lld/ELF/LinkerScript.h (+4)
  • (modified) lld/ELF/Writer.cpp (+7)
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 943fd865ae0c15c..ac8b854002b7463 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -170,8 +170,7 @@ void LinkerScript::expandOutputSection(uint64_t size) {
 void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
   uint64_t val = e().getValue();
   if (val < dot && inSec)
-    error(loc + ": unable to move location counter backward for: " +
-          state->outSec->name);
+    lastSectionWithBackwardsCounter = state->outSec;
 
   // Update to location counter means update to section size.
   if (inSec)
@@ -1327,6 +1326,7 @@ LinkerScript::AddressState::AddressState() {
 // Returns a symbol that has changed its section or value, or nullptr if no
 // symbol has changed.
 const Defined *LinkerScript::assignAddresses() {
+  lastSectionWithBackwardsCounter = nullptr;
   if (script->hasSectionsCommand) {
     // With a linker script, assignment of addresses to headers is covered by
     // allocateHeaders().
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 89780bb60f48244..d1bb4152b8c8e01 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -378,6 +378,10 @@ class LinkerScript final {
 
   // Sections that will be warned/errored by --orphan-handling.
   SmallVector<const InputSectionBase *, 0> orphanSections;
+
+  // If assignAddress() at any point caused the location counter to move
+  // backwards, this will point to the section where this occured.
+  OutputSection *lastSectionWithBackwardsCounter = nullptr;
 };
 
 LLVM_LIBRARY_VISIBILITY extern std::unique_ptr<LinkerScript> script;
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 40f7d7981d9d441..2db52eaa836fb51 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1697,6 +1697,13 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
       }
     }
   }
+
+  if (script->lastSectionWithBackwardsCounter) {
+    error(script->lastSectionWithBackwardsCounter->location +
+          ": unable to move location counter backward for: " +
+          script->lastSectionWithBackwardsCounter->name);
+  }
+
   if (!config->relocatable && config->emachine == EM_RISCV)
     riscvFinalizeRelax(pass);
 

@PiJoules
Copy link
Contributor Author

This was fixed with MaskRay@b12993b

@PiJoules PiJoules closed this Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LLD] LLD can report "unable to move location counter backward" error too early
2 participants