From ce49f9bc76e34651d817bfcbc5e910af2aa2128f Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton@mozilla.com>
Date: Fri, 13 Jan 2023 14:25:54 +0100
Subject: [PATCH] [lld][COFF] Provide unwinding information for Chunk injected
 by /delayloaded

For each symbol in a /delayloaded library, lld injects a small piece of
code to handle the symbol lazy loading. This code doesn't have unwind
information, which may be troublesome.

Provide these information for AMD64.

Fix #59639
---
 lld/COFF/DLL.cpp    | 84 ++++++++++++++++++++++++++++++++++++++++++++-
 lld/COFF/DLL.h      |  6 ++++
 lld/COFF/Writer.cpp |  4 +++
 3 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/lld/COFF/DLL.cpp b/lld/COFF/DLL.cpp
index b6e91ea1c197..b4cf640be2af 100644
--- a/lld/COFF/DLL.cpp
+++ b/lld/COFF/DLL.cpp
@@ -229,6 +229,19 @@ static const uint8_t tailMergeX64[] = {
     0xFF, 0xE0,                         // jmp     rax
 };
 
+static const uint8_t tailMergeUnwindInfoX64[] = {
+    0x01,       // Version=1, Flags=UNW_FLAG_NHANDLER
+    0x0a,       // Size of prolog
+    0x05,       // Count of unwind codes
+    0x00,       // No frame register
+    0x0a, 0x82, // Offset 0xa: UWOP_ALLOC_SMALL(0x48)
+    0x06, 0x02, // Offset 6: UWOP_ALLOC_SMALL(8)
+    0x04, 0x02, // Offset 4: UWOP_ALLOC_SMALL(8)
+    0x02, 0x02, // Offset 2: UWOP_ALLOC_SMALL(8)
+    0x01, 0x02, // Offset 1: UWOP_ALLOC_SMALL(8)
+    0x00, 0x00  // Padding to align on 32-bits
+};
+
 static const uint8_t thunkX86[] = {
     0xB8, 0, 0, 0, 0,  // mov   eax, offset ___imp__<FUNCNAME>
     0xE9, 0, 0, 0, 0,  // jmp   __tailMerge_<lib>
@@ -332,6 +345,36 @@ public:
   Defined *helper = nullptr;
 };
 
+class TailMergePDataChunkX64 : public NonSectionChunk {
+public:
+  TailMergePDataChunkX64(Defined *h, Chunk *tm, Chunk *unwind)
+      : helper(h), tm(tm), unwind(unwind) {}
+
+  size_t getSize() const override { return 3 * sizeof(uint32_t); }
+
+  void writeTo(uint8_t *buf) const override {
+    // FIXME
+    write32le(buf + 0, tm->getRVA()); // TailMergeChunk start RVA
+    write32le(buf + 4, tm->getRVA() + tm->getSize()); // TailMergeChunk stop RVA
+    write32le(buf + 8, unwind->getRVA());             // UnwindInfo RVA
+  }
+
+  Defined *helper = nullptr;
+  Chunk *tm = nullptr;
+  Chunk *unwind = nullptr;
+};
+
+class TailMergeUnwindInfoX64 : public NonSectionChunk {
+public:
+  TailMergeUnwindInfoX64() = default;
+
+  size_t getSize() const override { return sizeof(tailMergeUnwindInfoX64); }
+
+  void writeTo(uint8_t *buf) const override {
+    memcpy(buf, tailMergeUnwindInfoX64, sizeof(tailMergeUnwindInfoX64));
+  }
+};
+
 class ThunkChunkX86 : public NonSectionChunk {
 public:
   ThunkChunkX86(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
@@ -672,6 +715,8 @@ void DelayLoadContents::create(Defined *h) {
   helper = h;
   std::vector<std::vector<DefinedImportData *>> v = binImports(ctx, imports);
 
+  Chunk *unwind = newTailMergeUnwindInfoChunk();
+
   // Create .didat contents for each DLL.
   for (std::vector<DefinedImportData *> &syms : v) {
     // Create the delay import table header.
@@ -680,6 +725,7 @@ void DelayLoadContents::create(Defined *h) {
 
     size_t base = addresses.size();
     Chunk *tm = newTailMergeChunk(dir);
+    Chunk *pdataChunk = newTailMergePDataChunk(tm, unwind);
     for (DefinedImportData *s : syms) {
       Chunk *t = newThunkChunk(s, tm);
       auto *a = make<DelayAddressChunk>(ctx, t);
@@ -692,7 +738,7 @@ void DelayLoadContents::create(Defined *h) {
         auto *c = make<HintNameChunk>(extName, 0);
         names.push_back(make<LookupChunk>(ctx, c));
         hintNames.push_back(c);
-        // Add a syntentic symbol for this load thunk, using the "__imp___load"
+        // Add a synthetic symbol for this load thunk, using the "__imp___load"
         // prefix, in case this thunk needs to be added to the list of valid
         // call targets for Control Flow Guard.
         StringRef symName = saver().save("__imp___load_" + extName);
@@ -701,6 +747,7 @@ void DelayLoadContents::create(Defined *h) {
       }
     }
     thunks.push_back(tm);
+    pdata.push_back(pdataChunk);
     StringRef tmName =
         saver().save("__tailMerge_" + syms[0]->getDLLName().lower());
     ctx.symtab.addSynthetic(tmName, tm);
@@ -720,6 +767,8 @@ void DelayLoadContents::create(Defined *h) {
     dir->nameTab = names[base];
     dirs.push_back(dir);
   }
+
+  unwindinfo.push_back(unwind);
   // Add null terminator.
   dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
 }
@@ -739,6 +788,39 @@ Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
   }
 }
 
+Chunk *DelayLoadContents::newTailMergeUnwindInfoChunk() {
+  switch (ctx.config.machine) {
+  case AMD64:
+    return make<TailMergeUnwindInfoX64>();
+#if 0
+  case I386:
+    return make<TailMergeChunkX86>(ctx, dir, helper);
+  case ARMNT:
+    return make<TailMergeChunkARM>(ctx, dir, helper);
+  case ARM64:
+    return make<TailMergeChunkARM64>(dir, helper);
+#endif
+  default:
+    llvm_unreachable("unsupported machine type");
+  }
+}
+Chunk *DelayLoadContents::newTailMergePDataChunk(Chunk *tm, Chunk *unwind) {
+  switch (ctx.config.machine) {
+  case AMD64:
+    return make<TailMergePDataChunkX64>(helper, tm, unwind);
+#if 0
+  case I386:
+    return make<TailMergeChunkX86>(ctx, dir, helper);
+  case ARMNT:
+    return make<TailMergeChunkARM>(ctx, dir, helper);
+  case ARM64:
+    return make<TailMergeChunkARM64>(dir, helper);
+#endif
+  default:
+    llvm_unreachable("unsupported machine type");
+  }
+}
+
 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
                                         Chunk *tailMerge) {
   switch (ctx.config.machine) {
diff --git a/lld/COFF/DLL.h b/lld/COFF/DLL.h
index f90fd575efc1..7cf71f59d7c7 100644
--- a/lld/COFF/DLL.h
+++ b/lld/COFF/DLL.h
@@ -44,6 +44,8 @@ public:
   std::vector<Chunk *> getChunks();
   std::vector<Chunk *> getDataChunks();
   ArrayRef<Chunk *> getCodeChunks() { return thunks; }
+  ArrayRef<Chunk *> getCodePData() { return pdata; }
+  ArrayRef<Chunk *> getCodeUnwindInfo() { return unwindinfo; }
 
   uint64_t getDirRVA() { return dirs[0]->getRVA(); }
   uint64_t getDirSize();
@@ -51,6 +53,8 @@ public:
 private:
   Chunk *newThunkChunk(DefinedImportData *s, Chunk *tailMerge);
   Chunk *newTailMergeChunk(Chunk *dir);
+  Chunk *newTailMergePDataChunk(Chunk *tm, Chunk *unwind);
+  Chunk *newTailMergeUnwindInfoChunk();
 
   Defined *helper;
   std::vector<DefinedImportData *> imports;
@@ -60,6 +64,8 @@ private:
   std::vector<Chunk *> names;
   std::vector<Chunk *> hintNames;
   std::vector<Chunk *> thunks;
+  std::vector<Chunk *> pdata;
+  std::vector<Chunk *> unwindinfo;
   std::vector<Chunk *> dllNames;
 
   COFFLinkerContext &ctx;
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 09cca5667a47..f0dd3aba196f 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1121,6 +1121,10 @@ void Writer::appendImportThunks() {
       dataSec->addChunk(c);
     for (Chunk *c : delayIdata.getCodeChunks())
       textSec->addChunk(c);
+    for (Chunk *c : delayIdata.getCodePData())
+      pdataSec->addChunk(c);
+    for (Chunk *c : delayIdata.getCodeUnwindInfo())
+      rdataSec->addChunk(c);
   }
 }
 
-- 
2.38.1

