[lld] [LLD]Discard invalid "DW.ref.__gxx_personality_v0" pieces in rela.eh_frame (PR #158590)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 22 06:01:35 PDT 2025


https://github.com/MaooJian updated https://github.com/llvm/llvm-project/pull/158590

>From 5ed881dfe8917516b6daef5b8859333d32c3dcab Mon Sep 17 00:00:00 2001
From: MaoJian <maojian16 at huawei.com>
Date: Mon, 15 Sep 2025 17:27:48 +0800
Subject: [PATCH 1/3] Discard invalid "DW.ref.__gxx_personality_v0" pieces in
 rela.eh_frame

When the "DW.ref.__gxx_personality_v0" section is invalid, the generated relocation may overlap the FDE length. Discard such relocation entries to avoid corrupting the .eh_frame data.
---
 lld/ELF/InputSection.cpp | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index ea6bcc5bb272b..54396cd0515ed 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -491,6 +491,33 @@ void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,
     p->setSymbolAndType(ctx.in.symTab->getSymbolIndex(sym), type,
                         ctx.arg.isMips64EL);
 
+    // Discard the invalid pieces among those named "DW.ref.__gxx_personality_v0".
+    StringRef symName = sym.getName();
+    if (symName == "DW.ref.__gxx_personality_v0") {
+      if (auto *es = dyn_cast<EhInputSection>(sec)) {
+        auto it = partition_point(es->fdes, [=](EhSectionPiece p) {
+          return p.inputOff <= rel.offset;
+        });
+
+        if (it == es->fdes.begin() ||
+            it[-1].inputOff + it[-1].size <= rel.offset) {
+          it = partition_point(es->cies, [=](EhSectionPiece p) {
+            return p.inputOff <= rel.offset;
+          });
+          if (it == es->cies.begin()) {
+            // invalid piece
+            p->setSymbolAndType(0, 0, false);
+            continue;
+          }
+        }
+
+        if (it[-1].outputOff == -1) {
+          p->setSymbolAndType(0, 0, false);
+          continue;
+        }
+      }
+    }
+    
     if (sym.type == STT_SECTION) {
       // We combine multiple section symbols into only one per
       // section. This means we have to update the addend. That is

>From f2b0e45a9c26301c62efee454b506fc5fa0ce8ec Mon Sep 17 00:00:00 2001
From: MaoJian <maojian16 at huawei.com>
Date: Mon, 22 Sep 2025 20:57:31 +0800
Subject: [PATCH 2/3] Update InputSection.cpp

Code format
---
 lld/ELF/InputSection.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 54396cd0515ed..31675a808c291 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -486,12 +486,13 @@ void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,
       p->r_addend = rel.addend;
 
     // Output section VA is zero for -r, so r_offset is an offset within the
-    // section, but for --emit-relocs it is a virtual address.
+    // section, but for --emit-relocs it is a virtual address.3333
     p->r_offset = sec->getVA(rel.offset);
     p->setSymbolAndType(ctx.in.symTab->getSymbolIndex(sym), type,
                         ctx.arg.isMips64EL);
 
-    // Discard the invalid pieces among those named "DW.ref.__gxx_personality_v0".
+    // Discard the invalid pieces among those named
+    // "DW.ref.__gxx_personality_v0".
     StringRef symName = sym.getName();
     if (symName == "DW.ref.__gxx_personality_v0") {
       if (auto *es = dyn_cast<EhInputSection>(sec)) {
@@ -517,7 +518,7 @@ void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,
         }
       }
     }
-    
+
     if (sym.type == STT_SECTION) {
       // We combine multiple section symbols into only one per
       // section. This means we have to update the addend. That is

>From 4496b502ed60d603ac69673e230cc760c11a5d19 Mon Sep 17 00:00:00 2001
From: MaoJian <maojian16 at huawei.com>
Date: Mon, 22 Sep 2025 21:01:25 +0800
Subject: [PATCH 3/3] Update InputSection.cpp

Code format
---
 lld/ELF/InputSection.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 31675a808c291..db710a305056a 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -486,7 +486,7 @@ void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,
       p->r_addend = rel.addend;
 
     // Output section VA is zero for -r, so r_offset is an offset within the
-    // section, but for --emit-relocs it is a virtual address.3333
+    // section, but for --emit-relocs it is a virtual address.
     p->r_offset = sec->getVA(rel.offset);
     p->setSymbolAndType(ctx.in.symTab->getSymbolIndex(sym), type,
                         ctx.arg.isMips64EL);



More information about the llvm-commits mailing list