Skip to content

[lld][COFF][NFC] Store pdata range as ChunkRange. #74024

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
Dec 2, 2023

Conversation

cjacek
Copy link
Contributor

@cjacek cjacek commented Dec 1, 2023

As mentioned in #72521, his is meant to make ARM64EC/ARM64X support slightly nicer. On top of that, #72521 could be changed to cjacek@f3584dd.

@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2023

@llvm/pr-subscribers-lld
@llvm/pr-subscribers-lld-coff

@llvm/pr-subscribers-platform-windows

Author: Jacek Caban (cjacek)

Changes

As mentioned in #72521, his is meant to make ARM64EC/ARM64X support slightly nicer. On top of that, #72521 could be changed to cjacek@f3584dd.


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

1 Files Affected:

  • (modified) lld/COFF/Writer.cpp (+22-18)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 6dbfa8e730646e0..160e1c339caf136 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -197,6 +197,10 @@ class PartialSectionKey {
   }
 };
 
+struct ChunkRange {
+  Chunk *first = nullptr, *last;
+};
+
 // The writer writes a SymbolTable result to a file.
 class Writer {
 public:
@@ -247,7 +251,7 @@ class Writer {
   void writeBuildId();
   void writePEChecksum();
   void sortSections();
-  template <typename T> void sortExceptionTable(Chunk *first, Chunk *last);
+  template <typename T> void sortExceptionTable(ChunkRange &exceptionTable);
   void sortExceptionTables();
   void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
   void addSyntheticIdata();
@@ -311,7 +315,7 @@ class Writer {
   OutputSection *ctorsSec;
   OutputSection *dtorsSec;
 
-  // The first and last .pdata sections in the output file.
+  // The range or .pdata sections in the output file.
   //
   // We need to keep track of the location of .pdata in whichever section it
   // gets merged into so that we can sort its contents and emit a correct data
@@ -320,8 +324,7 @@ class Writer {
   // are entirely linker-generated we can keep track of their locations using
   // the chunks that the linker creates. All .pdata chunks come from input
   // files, so we need to keep track of them separately.
-  Chunk *firstPdata = nullptr;
-  Chunk *lastPdata;
+  ChunkRange pdata;
 
   COFFLinkerContext &ctx;
 };
@@ -1408,8 +1411,8 @@ void Writer::createSymbolAndStringTable() {
 void Writer::mergeSections() {
   llvm::TimeTraceScope timeScope("Merge sections");
   if (!pdataSec->chunks.empty()) {
-    firstPdata = pdataSec->chunks.front();
-    lastPdata = pdataSec->chunks.back();
+    pdata.first = pdataSec->chunks.front();
+    pdata.last = pdataSec->chunks.back();
   }
 
   for (auto &p : ctx.config.merge) {
@@ -1665,10 +1668,10 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
     dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
     dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
   }
-  if (firstPdata) {
-    dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
-    dir[EXCEPTION_TABLE].Size =
-        lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
+  if (pdata.first) {
+    dir[EXCEPTION_TABLE].RelativeVirtualAddress = pdata.first->getRVA();
+    dir[EXCEPTION_TABLE].Size = pdata.last->getRVA() +
+                                pdata.last->getSize() - pdata.first->getRVA();
   }
   if (relocSec->getVirtualSize()) {
     dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
@@ -2166,15 +2169,18 @@ void Writer::writeBuildId() {
 
 // Sort .pdata section contents according to PE/COFF spec 5.5.
 template <typename T>
-void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
+void Writer::sortExceptionTable(ChunkRange &exceptionTable) {
+  if (!exceptionTable.first)
+    return;
+
   // We assume .pdata contains function table entries only.
   auto bufAddr = [&](Chunk *c) {
     OutputSection *os = ctx.getOutputSection(c);
     return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
            os->getRVA();
   };
-  uint8_t *begin = bufAddr(first);
-  uint8_t *end = bufAddr(last) + last->getSize();
+  uint8_t *begin = bufAddr(exceptionTable.first);
+  uint8_t *end = bufAddr(exceptionTable.last) + exceptionTable.last->getSize();
   if ((end - begin) % sizeof(T) != 0) {
     fatal("unexpected .pdata size: " + Twine(end - begin) +
           " is not a multiple of " + Twine(sizeof(T)));
@@ -2198,16 +2204,14 @@ void Writer::sortExceptionTables() {
 
   switch (ctx.config.machine) {
   case AMD64:
-    if (firstPdata)
-      sortExceptionTable<EntryX64>(firstPdata, lastPdata);
+    sortExceptionTable<EntryX64>(pdata);
     break;
   case ARMNT:
   case ARM64:
-    if (firstPdata)
-      sortExceptionTable<EntryArm>(firstPdata, lastPdata);
+    sortExceptionTable<EntryArm>(pdata);
     break;
   default:
-    if (firstPdata)
+    if (pdata.first)
       lld::errs() << "warning: don't know how to handle .pdata.\n";
     break;
   }

Copy link

github-actions bot commented Dec 1, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

Looks good with the exception of the possible typo.

@@ -311,7 +315,7 @@ class Writer {
OutputSection *ctorsSec;
OutputSection *dtorsSec;

// The first and last .pdata sections in the output file.
// The range or .pdata sections in the output file.
Copy link
Member

Choose a reason for hiding this comment

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

Did you intend this to be "range of"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks!

@cjacek cjacek merged commit 7081585 into llvm:main Dec 2, 2023
@cjacek cjacek deleted the lld-chunk-range branch December 2, 2023 12:09
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