[llvm-branch-commits] [lld] 7081585 - [lld][COFF][NFC] Store pdata range as ChunkRange. (#74024)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 4 11:02:32 PST 2023


Author: Jacek Caban
Date: 2023-12-02T13:09:51+01:00
New Revision: 708158529b82007a34d623ba6f52d641c43b3416

URL: https://github.com/llvm/llvm-project/commit/708158529b82007a34d623ba6f52d641c43b3416
DIFF: https://github.com/llvm/llvm-project/commit/708158529b82007a34d623ba6f52d641c43b3416.diff

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

Added: 
    

Modified: 
    lld/COFF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 6dbfa8e730646..3490ff1b3c290 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-branch-commits mailing list