Skip to content

[LLD][COFF] Move entry thunk offset writing to writeSections (NFC) #151254

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

Merged
merged 1 commit into from
Jul 31, 2025

Conversation

cjacek
Copy link
Contributor

@cjacek cjacek commented Jul 29, 2025

To make it easier to add entry thunks to other chunk types.

To make it easier to add entry thunks to other chunk types.
@llvmbot
Copy link
Member

llvmbot commented Jul 29, 2025

@llvm/pr-subscribers-lld-coff

@llvm/pr-subscribers-lld

Author: Jacek Caban (cjacek)

Changes

To make it easier to add entry thunks to other chunk types.


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

2 Files Affected:

  • (modified) lld/COFF/Chunks.cpp (-6)
  • (modified) lld/COFF/Writer.cpp (+9-1)
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 01752cdc6a9da..4ba5d80b581ef 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -422,12 +422,6 @@ void SectionChunk::writeTo(uint8_t *buf) const {
 
     applyRelocation(buf + rel.VirtualAddress, rel);
   }
-
-  // Write the offset to EC entry thunk preceding section contents. The low bit
-  // is always set, so it's effectively an offset from the last byte of the
-  // offset.
-  if (Defined *entryThunk = getEntryThunk())
-    write32le(buf - sizeof(uint32_t), entryThunk->getRVA() - rva + 1);
 }
 
 void SectionChunk::applyRelocation(uint8_t *off,
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 076561807af47..62019f19292a1 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2544,7 +2544,15 @@ void Writer::writeSections() {
     }
 
     parallelForEach(sec->chunks, [&](Chunk *c) {
-      c->writeTo(secBuf + c->getRVA() - sec->getRVA());
+      uint8_t *buf = secBuf + c->getRVA() - sec->getRVA();
+      c->writeTo(buf);
+
+      // Write the offset to EC entry thunk preceding section contents. The low
+      // bit is always set, so it's effectively an offset from the last byte of
+      // the offset.
+      if (Defined *entryThunk = c->getEntryThunk())
+        write32le(buf - sizeof(uint32_t),
+                  entryThunk->getRVA() - c->getRVA() + 1);
     });
   }
 }

@llvmbot
Copy link
Member

llvmbot commented Jul 29, 2025

@llvm/pr-subscribers-platform-windows

Author: Jacek Caban (cjacek)

Changes

To make it easier to add entry thunks to other chunk types.


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

2 Files Affected:

  • (modified) lld/COFF/Chunks.cpp (-6)
  • (modified) lld/COFF/Writer.cpp (+9-1)
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 01752cdc6a9da..4ba5d80b581ef 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -422,12 +422,6 @@ void SectionChunk::writeTo(uint8_t *buf) const {
 
     applyRelocation(buf + rel.VirtualAddress, rel);
   }
-
-  // Write the offset to EC entry thunk preceding section contents. The low bit
-  // is always set, so it's effectively an offset from the last byte of the
-  // offset.
-  if (Defined *entryThunk = getEntryThunk())
-    write32le(buf - sizeof(uint32_t), entryThunk->getRVA() - rva + 1);
 }
 
 void SectionChunk::applyRelocation(uint8_t *off,
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 076561807af47..62019f19292a1 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2544,7 +2544,15 @@ void Writer::writeSections() {
     }
 
     parallelForEach(sec->chunks, [&](Chunk *c) {
-      c->writeTo(secBuf + c->getRVA() - sec->getRVA());
+      uint8_t *buf = secBuf + c->getRVA() - sec->getRVA();
+      c->writeTo(buf);
+
+      // Write the offset to EC entry thunk preceding section contents. The low
+      // bit is always set, so it's effectively an offset from the last byte of
+      // the offset.
+      if (Defined *entryThunk = c->getEntryThunk())
+        write32le(buf - sizeof(uint32_t),
+                  entryThunk->getRVA() - c->getRVA() + 1);
     });
   }
 }

@cjacek
Copy link
Contributor Author

cjacek commented Jul 29, 2025

Split from a larger -arm64xsameaddress patch, which will use it for a same-address thunks.

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine - it looks a bit unintuitive, but probably is good if it makes the following commit easier. However I don't quite see in which way this makes the later commit easier, as I don't see anything touching this area in that commit?

@cjacek
Copy link
Contributor Author

cjacek commented Jul 30, 2025

With #151255, we also need entry thunk support for SameAddressThunkARM64EC. On top of this PR, that just requires overriding getEntryThunk. Before the recent change that moved the write logic, I also had to override writeTo to write the offset there (that’s not a big deal, so I can go back to that if preferred).

@mstorsjo
Copy link
Member

With #151255, we also need entry thunk support for SameAddressThunkARM64EC. On top of this PR, that just requires overriding getEntryThunk. Before the recent change that moved the write logic, I also had to override writeTo to write the offset there (that’s not a big deal, so I can go back to that if preferred).

Ah, I see, thanks! Then this does indeed seem well motivated.

@cjacek cjacek merged commit 462c208 into llvm:main Jul 31, 2025
13 checks passed
@cjacek cjacek deleted the write-entry-thunk branch July 31, 2025 10:59
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.

3 participants