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

Jacek Caban via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 2 04:07:28 PST 2023


https://github.com/cjacek updated https://github.com/llvm/llvm-project/pull/74024

>From de3b382a49c059cdc78e9cca20137c85d02cb4d3 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Thu, 30 Nov 2023 16:56:50 +0100
Subject: [PATCH] [lld][COFF][NFC] Store pdata range as ChunkRange.

---
 lld/COFF/Writer.cpp | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 6dbfa8e73064..3490ff1b3c29 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 of .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();
+  if (pdata.first) {
+    dir[EXCEPTION_TABLE].RelativeVirtualAddress = pdata.first->getRVA();
     dir[EXCEPTION_TABLE].Size =
-        lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
+        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;
   }



More information about the llvm-commits mailing list