[lld] [lld][ELF] Fold LSDA-bearing sections with equivalent LSDA and CIE (PR #224929)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 06:15:53 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: artemkulyk

<details>
<summary>Changes</summary>

# [lld][ELF] Fold LSDA-bearing sections with equivalent LSDA and CIE

## Problem

`--icf=all` never folds a text section whose `.eh_frame` FDE has an LSDA.
Commit 21b4f8060ab3 ("[ELF] --icf: don't fold text sections with LSDA",
D84610) introduced this to fix PR36272/PR46835: two functions with identical
code can have different `catch` blocks, and folding them would make one
function unwind through the other function's exception table.  The commit
noted that "there may be some percentage we can reclaim without affecting
correctness, if we analyze .eh_frame and .gcc_except_table sections" (for
ClickHouse it estimated 9% -> 5% ICF efficiency).

## Why current lld misses valid folds

The current rule disables folding for *every* LSDA-bearing section, including
duplicated functions whose LSDA sections and CIE metadata are identical.  This
is the common case for template instantiations and duplicated functions in
exception-heavy C++ programs built with `-ffunction-sections` (clang gives each
function its own `.gcc_except_table.<sym>` section at offset 0).

lld/MachO already folds FDEs with identical unwinding data
(#<!-- -->216895, #<!-- -->220155); this is the ELF analogue.

## Semantic correctness model

Two text sections `A` and `B` are folded only if all of the following hold:

1. **Existing ICF rules**: identical contents and equivalent relocations
   (`equalsConstant`/`equalsVariable`).
2. **LSDA equivalence**: the FDEs of `A` and `B` reference `.gcc_except_table`
   sections in the same ICF equivalence class (identical contents and
   equivalent relocation targets).  A section with an LSDA never equals a
   section without one.
3. **CIE equivalence**: the CIEs referenced by the two FDEs have identical
   bytes, the same personality function (and addend) and identical remaining
   CIE relocations.  The personality is resolved through the
   `DW_EH_PE_indirect` pointer-to-pointer thunk (`DW.ref.<personality>`) a
   compiler emits, so the same personality in different objects compares
   equal even though the local thunk symbols differ.

Under these conditions the surviving function's FDE references byte-identical
LSDA data with the same personality and encodings, so the unwinder observes
the same behavior for either function.  Anything that cannot be analyzed
stays unfolded:

* LSDA targets that are not a section (absolute symbols, thunks);
* shared `.gcc_except_table` sections referenced at a nonzero offset
  (this change intentionally keeps the conservative offset-0 rule);
* text sections whose multiple FDEs disagree about LSDA/CIE;
* sections kept unique by `--keep-unique`, by the dynamic symbol table or by
  a real `.llvm_addrsig` (the change only clears the conservative fallback
  marking that lld applies to objects without an address-significance table).

## What changed

* `lld/ELF/EhFrame.{h,cpp}`: expose the CIE personality encoding; allow
  suppressing `.eh_frame` diagnostics in pre-passes
  (`EhFrameSection::finalizeContents` still reports them once).
* `lld/ELF/SyntheticSections.{h,cpp}`: new `iterateFDEWithLSDATarget` that
  reports, for each live FDE with an LSDA, the described section, the CIE and
  the LSDA symbol/addend.  The existing `iterateFDEWithLSDA` is reimplemented
  on top of it (no duplicated FDE scanning).
* `lld/ELF/ICF.cpp`: build the per-section LSDA/CIE information, compare it in
  `equalsVariable`, and mix the LSDA/CIE hashes into the initial partition.
* `lld/ELF/Driver.cpp`: clear the conservative address-significance marking of
  LSDA sections so they can be folded; explicit `--keep-unique`, dynsym and
  `.llvm_addrsig` markings still win.

## Tests

* `lld/test/ELF/icf-eh-frame.s` (extended): positive fold with the same LSDA;
  different LSDA not folded; shared exception table referenced at a nonzero
  offset not folded; functions without LSDA still fold.
* `lld/test/ELF/icf-lsda-personality.s` (new): identical code+LSDA with
  different personalities not folded; same personality folded (including an
  exported LSDA symbol, exercising the conservative-marking clearing);
  `--keep-unique` on an LSDA symbol keeps its functions unique.
* `lld/test/ELF/icf-lsda-personality-indirect.s` (new): `DW_EH_PE_indirect`
  personality thunks resolving to the same/different personalities.

Both REL (i386) and RELA (x86-64) are covered.  `llvm-lit` passes for the
three tests plus the surrounding ICF tests.

## Measured impact (benchmark evidence, separate from correctness)

Frozen-input A/B: A = pristine lld 22.1.8, B = the same revision with only
this change; identical ThinLTO inputs and link command.  All numbers are
B relative to A:

| project (release recipe) | effect |
|---|---|
| ClickHouse v26.8.7.19-lts (official recipe, ThinLTO, `--icf=all`) | final artifact **-7.19%**, `.text` -10.06%, `.eh_frame` -12.89%, `.eh_frame_hdr` -12.07%, `.gcc_except_table` -7.66%, -48,353 FDEs, -44,349 LSDA FDEs; link time and peak RSS unchanged |
| Proton v3.0.31 (ClickHouse fork, same flags) | stripped artifact **-8.81%** |
| dwarfs v0.15.7 (clang, lld, `--icf=all`) | **-3.32%** |
| Next.js/Turbopack native SWC (Rust, ThinLTO, `--icf=all`) | **-2.20%** (`.gcc_except_table` -33.5%) |
| capnproto 1.0.2 with `--icf=all` forced (opt-in) | -6.86% |

## Validation summary

* Differential runtime testing of A/B binaries: ClickHouse (39-query battery
  with 25 exception/error paths, server smoke, official stateless subset,
  multithreaded stress), Proton (30-query native-dialect battery, streaming,
  start/stop), dwarfs (2,024-file image create/check/extract, corrupt inputs,
  FUSE), Next.js/SWC (binding API, error/panic paths, 200 sequential + 50
  concurrent transforms), capnproto (225+135 upstream tests), MegEngine
  (upstream CPU tests).  A and B are behaviorally identical in all cases
  (only documented nondeterminism differs).
* Link cost: A and B link time/user CPU/peak RSS are indistinguishable.
* No new diagnostics: malformed `.eh_frame` reporting is unchanged (the
  pre-pass is silent, `finalizeContents` reports as before).

## Known limitations / non-goals

* Offset-0 rule only: a shared `.gcc_except_table` section referenced at a
  nonzero offset is still not foldable.  A range-level rule would need to
  parse LSDA records and compare their relocations; not attempted here.
* Non-section LSDA targets (absolute symbols, thunks) remain uncomparable.
* A text section whose FDEs reference different LSDAs/CIEs stays uneligible.
* No changes to `--icf=safe` semantics beyond not blocking equivalent LSDA
  folds that are already permitted by address-significance information.

## AI tool disclosure

The code and text were developed with an AI coding assistant and reviewed,
tested and edited by the human contributor, who is responsible for the
content.  Per the LLVM AI tool policy, this PR description is the disclosure
of tool usage (the commit message carries no `Assisted-by:` trailer).


---

Patch is 37.65 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/224929.diff


10 Files Affected:

- (modified) lld/ELF/Driver.cpp (+37-6) 
- (modified) lld/ELF/EhFrame.cpp (+42-5) 
- (modified) lld/ELF/EhFrame.h (+8-1) 
- (modified) lld/ELF/ICF.cpp (+231-12) 
- (modified) lld/ELF/SyntheticSections.cpp (+86-15) 
- (modified) lld/ELF/SyntheticSections.h (+23-3) 
- (modified) lld/docs/ReleaseNotes.md (+3) 
- (modified) lld/test/ELF/icf-eh-frame.s (+63-15) 
- (added) lld/test/ELF/icf-lsda-personality-indirect.s (+92) 
- (added) lld/test/ELF/icf-lsda-personality.s (+120) 


``````````diff
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index c5e8d1be2e4e9..e863e28ff7ec6 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2677,13 +2677,17 @@ static void replaceCommonSymbols(Ctx &ctx) {
 
 // The section referred to by `s` is considered address-significant. Set the
 // keepUnique flag on the section if appropriate.
-static void markAddrsig(bool icfSafe, Symbol *s) {
+static void markAddrsig(bool icfSafe, Symbol *s,
+                        DenseSet<InputSectionBase *> *explicitlySignificant) {
   // We don't need to keep text sections unique under --icf=all even if they
   // are address-significant.
   if (auto *d = dyn_cast_or_null<Defined>(s))
     if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
-      if (icfSafe || !(sec->flags & SHF_EXECINSTR))
+      if (icfSafe || !(sec->flags & SHF_EXECINSTR)) {
         sec->keepUnique = true;
+        if (explicitlySignificant)
+          explicitlySignificant->insert(sec);
+      }
 }
 
 // Record sections that define symbols mentioned in --keep-unique <symbol>
@@ -2691,6 +2695,9 @@ static void markAddrsig(bool icfSafe, Symbol *s) {
 // ineligible for ICF.
 template <class ELFT>
 static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
+  // Sections explicitly requested via --keep-unique; they win over the LSDA
+  // exemption below.
+  SmallPtrSet<InputSectionBase *, 4> forced;
   for (auto *arg : args.filtered(OPT_keep_unique)) {
     StringRef name = arg->getValue();
     auto *d = dyn_cast_or_null<Defined>(ctx.symtab->find(name));
@@ -2698,10 +2705,18 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
       Warn(ctx) << "could not find symbol " << name << " to keep unique";
       continue;
     }
-    if (auto *sec = dyn_cast<InputSectionBase>(d->section))
+    if (auto *sec = dyn_cast<InputSectionBase>(d->section)) {
       sec->keepUnique = true;
+      forced.insert(sec);
+    }
   }
 
+  // Sections marked address-significant by the dynsym or an address-
+  // significance table. The LSDA exemption below only clears the conservative
+  // fallback marking for objects without an address-significance table, so
+  // these sections stay unique.
+  DenseSet<InputSectionBase *> explicitlySignificant;
+
   // --icf=all --ignore-data-address-equality means that we can ignore
   // the dynsym and address-significance tables entirely.
   if (ctx.arg.icf == ICFLevel::All && ctx.arg.ignoreDataAddressEquality)
@@ -2712,7 +2727,7 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
   bool icfSafe = ctx.arg.icf == ICFLevel::Safe;
   for (Symbol *sym : ctx.symtab->getSymbols())
     if (sym->isExported)
-      markAddrsig(icfSafe, sym);
+      markAddrsig(icfSafe, sym, &explicitlySignificant);
 
   // Visit the address-significance table in each object file and mark each
   // referenced symbol as address-significant.
@@ -2731,16 +2746,32 @@ static void findKeepUniqueSections(Ctx &ctx, opt::InputArgList &args) {
           Err(ctx) << f << ": could not decode addrsig section: " << err;
           break;
         }
-        markAddrsig(icfSafe, syms[symIndex]);
+        markAddrsig(icfSafe, syms[symIndex], &explicitlySignificant);
         cur += size;
       }
     } else {
       // If an object file does not have an address-significance table,
       // conservatively mark all of its symbols as address-significant.
       for (Symbol *s : syms)
-        markAddrsig(icfSafe, s);
+        markAddrsig(icfSafe, s, nullptr);
     }
   }
+
+  // ICF folds an LSDA-bearing function only when its LSDA and CIE are
+  // equivalent to the surviving function's (ICF::lsdaEqualVariable), so the
+  // conservative keepUnique marking of an LSDA section can be cleared. Explicit
+  // --keep-unique requests and address-significant sections (dynsym or
+  // .llvm_addrsig) stay unique. Diagnostics are reported later by
+  // EhFrameSection::finalizeContents.
+  ctx.in.ehFrame->iterateFDEWithLSDATarget<ELFT>(
+      [&](InputSection &, const CieInfo &, const Symbol &sym, int64_t) {
+        auto *d = dyn_cast<Defined>(&sym);
+        auto *sec = d ? dyn_cast_or_null<InputSection>(d->section) : nullptr;
+        if (sec && !forced.contains(sec) &&
+            !explicitlySignificant.contains(sec))
+          sec->keepUnique = false;
+      },
+      /*reportErrors=*/false);
 }
 
 static void markBuffersAsDontNeed(Ctx &ctx, bool skipLinkedOutput) {
diff --git a/lld/ELF/EhFrame.cpp b/lld/ELF/EhFrame.cpp
index aade9291d1fba..17d2888cd76bf 100644
--- a/lld/ELF/EhFrame.cpp
+++ b/lld/ELF/EhFrame.cpp
@@ -33,12 +33,16 @@ using namespace lld::elf;
 namespace {
 class EhReader {
 public:
-  EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {}
+  EhReader(InputSectionBase *s, ArrayRef<uint8_t> d, bool reportErrors = true)
+      : isec(s), d(d), reportErrors(reportErrors) {}
   uint8_t getFdeEncoding();
+  std::optional<uint8_t> getPersonalityEncoding();
   bool hasLSDA();
 
 private:
   template <class P> void errOn(const P *loc, const Twine &msg) {
+    if (!reportErrors)
+      return;
     Ctx &ctx = isec->file->ctx;
     Err(ctx) << "corrupted .eh_frame: " << msg << "\n>>> defined in "
              << isec->getObjMsg((const uint8_t *)loc - isec->content().data());
@@ -49,10 +53,12 @@ class EhReader {
   StringRef readString();
   void skipLeb128();
   void skipAugP();
+  void skipAugPData(uint8_t enc);
   StringRef getAugmentation();
 
   InputSectionBase *isec;
   ArrayRef<uint8_t> d;
+  bool reportErrors;
 };
 }
 
@@ -119,8 +125,7 @@ static size_t getAugPSize(Ctx &ctx, unsigned enc) {
   return 0;
 }
 
-void EhReader::skipAugP() {
-  uint8_t enc = readByte();
+void EhReader::skipAugPData(uint8_t enc) {
   if ((enc & 0xf0) == DW_EH_PE_aligned)
     return errOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
   size_t size = getAugPSize(isec->getCtx(), enc);
@@ -131,12 +136,22 @@ void EhReader::skipAugP() {
   d = d.slice(size);
 }
 
+void EhReader::skipAugP() {
+  uint8_t enc = readByte();
+  skipAugPData(enc);
+}
+
 uint8_t elf::getFdeEncoding(EhSectionPiece *p) {
   return EhReader(p->sec, p->data()).getFdeEncoding();
 }
 
-bool elf::hasLSDA(const EhSectionPiece &p) {
-  return EhReader(p.sec, p.data()).hasLSDA();
+std::optional<uint8_t> elf::getPersonalityEncoding(const EhSectionPiece &p,
+                                                   bool reportErrors) {
+  return EhReader(p.sec, p.data(), reportErrors).getPersonalityEncoding();
+}
+
+bool elf::hasLSDA(const EhSectionPiece &p, bool reportErrors) {
+  return EhReader(p.sec, p.data(), reportErrors).hasLSDA();
 }
 
 StringRef EhReader::getAugmentation() {
@@ -185,6 +200,28 @@ uint8_t EhReader::getFdeEncoding() {
   return DW_EH_PE_absptr;
 }
 
+std::optional<uint8_t> EhReader::getPersonalityEncoding() {
+  StringRef aug = getAugmentation();
+  for (char c : aug) {
+    if (c == 'P') {
+      uint8_t enc = readByte();
+      skipAugPData(enc);
+      return enc;
+    }
+    if (c == 'z')
+      skipLeb128();
+    else if (c == 'L')
+      readByte();
+    else if (c == 'R')
+      readByte();
+    else if (c != 'B' && c != 'S' && c != 'G') {
+      errOn(aug.data(), "unknown .eh_frame augmentation string: " + aug);
+      break;
+    }
+  }
+  return std::nullopt;
+}
+
 bool EhReader::hasLSDA() {
   StringRef aug = getAugmentation();
   for (char c : aug) {
diff --git a/lld/ELF/EhFrame.h b/lld/ELF/EhFrame.h
index 95264166e36cb..6980cbd6767d6 100644
--- a/lld/ELF/EhFrame.h
+++ b/lld/ELF/EhFrame.h
@@ -11,11 +11,18 @@
 
 #include "lld/Common/LLVM.h"
 
+#include <optional>
+
 namespace lld::elf {
 struct EhSectionPiece;
 
 uint8_t getFdeEncoding(EhSectionPiece *p);
-bool hasLSDA(const EhSectionPiece &p);
+// Returns the 'P' (personality) encoding of a CIE, if it has one.
+std::optional<uint8_t> getPersonalityEncoding(const EhSectionPiece &p,
+                                              bool reportErrors = true);
+// reportErrors=false suppresses diagnostics (used by pre-passes that run
+// before EhFrameSection::finalizeContents, which reports them).
+bool hasLSDA(const EhSectionPiece &p, bool reportErrors = true);
 }
 
 #endif
diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index 62c42bdbb550d..cb0726c3583dd 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -80,6 +80,7 @@
 #include "SymbolTable.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
+#include "llvm/BinaryFormat/Dwarf.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/TimeProfiler.h"
@@ -113,6 +114,25 @@ template <class ELFT> class ICF {
   bool equalsConstant(const InputSection *a, const InputSection *b);
   bool equalsVariable(const InputSection *a, const InputSection *b);
 
+  bool lsdaEqualVariable(const InputSection *a, const InputSection *b);
+  bool cieRelsEqual(ArrayRef<Relocation> a, ArrayRef<Relocation> b) const;
+  bool personalityEqual(const Symbol *a, const Symbol *b) const;
+
+  struct LsdaInfo {
+    // The .gcc_except_table section the FDE's LSDA pointer refers to.
+    const InputSection *lsda;
+    // The CIE the FDE references.
+    ArrayRef<uint8_t> cieData;
+    // The personality function and its addend, resolved through a
+    // DW_EH_PE_indirect thunk if needed. personalityKnown is false when the
+    // personality cannot be resolved (conservatively not foldable).
+    bool personalityKnown;
+    const Symbol *personality;
+    int64_t personalityAddend;
+    // CIE relocations other than the personality pointer.
+    ArrayRef<Relocation> otherCieRels;
+  };
+
   size_t findBoundary(size_t begin, size_t end);
 
   void forEachClassRange(size_t begin, size_t end,
@@ -123,6 +143,13 @@ template <class ELFT> class ICF {
   Ctx &ctx;
   SmallVector<InputSection *, 0> sections;
 
+  // For each section whose .eh_frame FDE has an LSDA, the associated
+  // .gcc_except_table section and the CIE the FDE references. Two sections may
+  // be folded only if their LSDA sections are equal and their CIEs are
+  // equivalent (personality, encodings, CFI), otherwise exception handling
+  // behavior could change.
+  llvm::DenseMap<const InputSection *, LsdaInfo> lsdaMap;
+
   // We repeat the main loop while `Repeat` is true.
   std::atomic<bool> repeat;
 
@@ -375,6 +402,9 @@ bool ICF<ELFT>::variableEq(const InputSection *secA, Relocs<RelTy> ra,
 // Compare "moving" part of two InputSections, namely relocation targets.
 template <class ELFT>
 bool ICF<ELFT>::equalsVariable(const InputSection *a, const InputSection *b) {
+  if (!lsdaEqualVariable(a, b))
+    return false;
+
   const RelsOrRelas<ELFT> ra = a->template relsOrRelas<ELFT>();
   const RelsOrRelas<ELFT> rb = b->template relsOrRelas<ELFT>();
   if (ra.areRelocsCrel() || rb.areRelocsCrel())
@@ -384,6 +414,129 @@ bool ICF<ELFT>::equalsVariable(const InputSection *a, const InputSection *b) {
              : variableEq(a, ra.relas, b, rb.relas);
 }
 
+// Compare two lists of CIE relocations (e.g. the personality function).
+// Relocation targets are compared like variableEq() compares section
+// relocations: same symbol, or equivalent section-defined targets whose
+// equivalence classes have converged. This looks through the per-object
+// indirection thunk a compiler emits for an indirect personality pointer:
+// equal thunk sections imply equal personality functions.
+template <class ELFT>
+bool ICF<ELFT>::cieRelsEqual(ArrayRef<Relocation> a,
+                             ArrayRef<Relocation> b) const {
+  if (a.size() != b.size())
+    return false;
+  for (size_t i = 0; i != a.size(); ++i) {
+    if (a[i].offset != b[i].offset || a[i].type != b[i].type ||
+        a[i].addend != b[i].addend || a[i].expr != b[i].expr)
+      return false;
+    Symbol *sa = a[i].sym;
+    Symbol *sb = b[i].sym;
+    if (sa == sb)
+      continue;
+    auto *da = dyn_cast<Defined>(sa);
+    auto *db = dyn_cast<Defined>(sb);
+    if (!da || !db)
+      return false;
+    // Absolute symbols are equal if their values are equal.
+    if (!da->section || !db->section) {
+      if (da->value != db->value)
+        return false;
+      continue;
+    }
+    // Section-defined symbols with different kind or offset are not equal.
+    if (da->section->kind() != db->section->kind() ||
+        da->value + a[i].addend != db->value + b[i].addend)
+      return false;
+    auto *x = dyn_cast<InputSection>(da->section);
+    auto *y = dyn_cast<InputSection>(db->section);
+    if (!x || !y)
+      return false;
+    if (x->eqClass[current] == 0 || x->eqClass[current] != y->eqClass[current])
+      return false;
+  }
+  return true;
+}
+
+// Return the symbol referenced by the relocation at `off` in `sec` and set
+// `addend`, or return nullptr if there is no such relocation (or more than
+// one).
+template <class ELFT>
+static const Symbol *relocTargetAt(const InputSection *sec, uint64_t off,
+                                   int64_t &addend) {
+  const RelsOrRelas<ELFT> rs = sec->template relsOrRelas<ELFT>();
+  if (rs.areRelocsCrel())
+    return nullptr;
+  const Symbol *sym = nullptr;
+  if (rs.areRelocsRel()) {
+    for (const typename ELFT::Rel &r : rs.rels)
+      if (r.r_offset == off) {
+        if (sym)
+          return nullptr;
+        sym = &sec->file->getRelocTargetSym(r);
+        addend = 0;
+      }
+  } else {
+    for (const typename ELFT::Rela &r : rs.relas)
+      if (r.r_offset == off) {
+        if (sym)
+          return nullptr;
+        sym = &sec->file->getRelocTargetSym(r);
+        addend = r.r_addend;
+      }
+  }
+  return sym;
+}
+
+// Compare two resolved personality functions like variableEq() compares
+// relocation targets: same symbol, or equivalent section-defined targets whose
+// equivalence classes have converged.
+template <class ELFT>
+bool ICF<ELFT>::personalityEqual(const Symbol *a, const Symbol *b) const {
+  if (a == b)
+    return true;
+  auto *da = dyn_cast<Defined>(a);
+  auto *db = dyn_cast<Defined>(b);
+  if (!da || !db)
+    return false;
+  if (!da->section || !db->section)
+    return da->value == db->value;
+  if (da->section->kind() != db->section->kind() || da->value != db->value)
+    return false;
+  auto *x = dyn_cast<InputSection>(da->section);
+  auto *y = dyn_cast<InputSection>(db->section);
+  if (!x || !y)
+    return false;
+  return x->eqClass[current] != 0 && x->eqClass[current] == y->eqClass[current];
+}
+
+// Return whether two text sections have equivalent exception-handling
+// metadata: the same LSDA (equal .gcc_except_table section and relocation
+// targets) and the same CIE (personality, encodings, CFI). A section with an
+// LSDA never equals a section without one.
+template <class ELFT>
+bool ICF<ELFT>::lsdaEqualVariable(const InputSection *a,
+                                  const InputSection *b) {
+  auto itA = lsdaMap.find(a);
+  auto itB = lsdaMap.find(b);
+  if (itA == lsdaMap.end() && itB == lsdaMap.end())
+    return true;
+  if (itA == lsdaMap.end() || itB == lsdaMap.end())
+    return false;
+  const LsdaInfo &x = itA->second;
+  const LsdaInfo &y = itB->second;
+  // The LSDA contents and their relocation targets must be equivalent. This
+  // uses the same equivalence-class refinement as normal ICF comparisons.
+  if (x.lsda->eqClass[current] == 0 ||
+      x.lsda->eqClass[current] != y.lsda->eqClass[current])
+    return false;
+  // The CIE (personality, encodings, CFI) must be equivalent as well.
+  if (x.cieData != y.cieData || !x.personalityKnown || !y.personalityKnown ||
+      x.personalityAddend != y.personalityAddend ||
+      !personalityEqual(x.personality, y.personality))
+    return false;
+  return cieRelsEqual(x.otherCieRels, y.otherCieRels);
+}
+
 template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t begin, size_t end) {
   uint32_t eqClass = sections[begin]->eqClass[current];
   for (size_t i = begin + 1; i < end; ++i)
@@ -463,17 +616,77 @@ static void combineRelocHashes(unsigned cnt, InputSection *isec,
 // The main function of ICF.
 template <class ELFT> void ICF<ELFT>::run() {
   // Two text sections may have identical content and relocations but different
-  // LSDA, e.g. the two functions may have catch blocks of different types. If a
-  // text section is referenced by a .eh_frame FDE with LSDA, it is not
-  // eligible. This is implemented by iterating over CIE/FDE and setting
-  // eqClass[0] to the referenced text section from a live FDE.
-  //
-  // If two .gcc_except_table have identical semantics (usually identical
-  // content with PC-relative encoding), we will lose folding opportunity.
+  // LSDA, e.g. the two functions may have catch blocks of different types.
+  // A section whose FDE has an LSDA is foldable only when the associated LSDA
+  // section is known and is equal for both sections (see lsdaMap). Sections
+  // with an LSDA that cannot be analyzed (e.g. a shared .gcc_except_table
+  // section referenced at a nonzero offset) are not eligible, as before.
   uint32_t uniqueId = 0;
-  ctx.in.ehFrame->iterateFDEWithLSDA<ELFT>(
-      [&](InputSection &s) { s.eqClass[0] = s.eqClass[1] = ++uniqueId; });
-
+  DenseMap<InputSection *, LsdaInfo> lsdaSecs;
+  DenseSet<InputSection *> lsdaConflict;
+  SmallVector<InputSection *, 0> lsdaUncomparable;
+  ctx.in.ehFrame->iterateFDEWithLSDATarget<ELFT>(
+      [&](InputSection &text, const CieInfo &cie, const Symbol &sym,
+          int64_t addend) {
+        auto *d = dyn_cast<Defined>(&sym);
+        InputSection *lsda =
+            d ? dyn_cast_or_null<InputSection>(d->section) : nullptr;
+        if (!lsda || d->value + addend != 0) {
+          lsdaUncomparable.push_back(&text);
+          return;
+        }
+        // Resolve the personality function. A compiler usually materializes
+        // the CIE personality pointer indirectly through a per-object pointer
+        // ("DW.ref.<personality>"); follow that indirection so that the same
+        // personality in different objects compares equal even though the
+        // local thunk symbols differ.
+        const Symbol *personality = nullptr;
+        int64_t personalityAddend = 0;
+        bool personalityKnown = true;
+        ArrayRef<Relocation> otherCieRels = cie.rels;
+        if (cie.personalityEncoding) {
+          if (cie.rels.empty()) {
+            personalityKnown = false;
+          } else {
+            const Relocation &r = cie.rels.front();
+            otherCieRels = cie.rels.drop_front();
+            if (*cie.personalityEncoding & llvm::dwarf::DW_EH_PE_indirect) {
+              auto *pd = dyn_cast<Defined>(r.sym);
+              auto *thunk =
+                  pd ? dyn_cast_or_null<InputSection>(pd->section) : nullptr;
+              personality =
+                  thunk ? relocTargetAt<ELFT>(thunk, pd->value + r.addend,
+                                              personalityAddend)
+                        : nullptr;
+              if (!personality)
+                personalityKnown = false;
+            } else {
+              personality = r.sym;
+              personalityAddend = r.addend;
+            }
+          }
+        }
+        LsdaInfo info{lsda,        cie.piece->data(), personalityKnown,
+                      personality, personalityAddend, otherCieRels};
+        auto it = lsdaSecs.try_emplace(&text, info);
+        if (!it.second) {
+          const LsdaInfo &prev = it.first->second;
+          if (prev.lsda != lsda || prev.cieData != cie.piece->data() ||
+              prev.personalityKnown != personalityKnown ||
+              prev.personality != personality ||
+              prev.personalityAddend != personalityAddend ||
+              !cieRelsEqual(prev.otherCieRels, otherCieRels))
+            lsdaConflict.insert(&text);
+        }
+      });
+  for (InputSection *s : lsdaUncomparable)
+    s->eqClass[0] = s->eqClass[1] = ++uniqueId;
+  for (auto &kv : lsdaSecs) {
+    if (lsdaConflict.contains(kv.first))
+      kv.first->eqClass[0] = kv.first->eqClass[1] = ++uniqueId;
+    else
+      lsdaMap.try_emplace(kv.first, kv.second);
+  }
   // Collect sections to merge.
   for (InputSectionBase *sec : ctx.inputSections) {
     auto *s = dyn_cast<InputSection>(sec);
@@ -487,10 +700,16 @@ template <class ELFT> void ICF<ELFT>::run() {
     }
   }
 
-  // Initially, we use hash values to partition sections.
+  // Initially, we use hash values to partition sections. Mix in the LSDA and
+  // CIE hashes so that sections that only differ in t...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/224929


More information about the llvm-commits mailing list