[lld] [ELF] Remove duplicate SHT_CREL writeTo branch and cover discarded section symbol path. NFC (PR #195558)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun May 3 16:11:06 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/195558

OutputSection::writeTo had two identical
`if (type == SHT_CREL && !(flags & SHF_ALLOC))` blocks added together in
0af07c078798 (#98115). Drop a dead copy.

A section relocation against a /DISCARD/'d section sees the section
symbol demoted to Undefined. Add a -r --crel case to relocatable-crel.s
that exercises that path.

>From 17e1c1bb62784cdf4b1a849289ca35ecbd471197 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 3 May 2026 16:05:56 -0700
Subject: [PATCH] [ELF] Remove duplicate SHT_CREL writeTo branch and cover
 discarded section symbol path. NFC

OutputSection::writeTo had two identical
`if (type == SHT_CREL && !(flags & SHF_ALLOC))` blocks added together in
0af07c078798 (#98115). Drop a dead copy.

A section relocation against a /DISCARD/'d section sees the section
symbol demoted to Undefined. Add a -r --crel case to relocatable-crel.s
that exercises that path.
---
 lld/ELF/OutputSections.cpp      |  6 ------
 lld/test/ELF/relocatable-crel.s | 22 ++++++++++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 278e9f56b1d11..ce5f1e551078d 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -536,12 +536,6 @@ void OutputSection::writeTo(Ctx &ctx, uint8_t *buf, parallel::TaskGroup &tg) {
   if (nonZeroFiller)
     fill(buf, sections.empty() ? size : sections[0]->outSecOff, filler);
 
-  if (type == SHT_CREL && !(flags & SHF_ALLOC)) {
-    buf += encodeULEB128(crelHeader, buf);
-    memcpy(buf, crelBody.data(), crelBody.size());
-    return;
-  }
-
   auto fn = [=, &ctx](size_t begin, size_t end) {
     size_t numSections = sections.size();
     for (size_t i = begin; i != end; ++i) {
diff --git a/lld/test/ELF/relocatable-crel.s b/lld/test/ELF/relocatable-crel.s
index 6e97c3e24d66c..28a2f96d979f4 100644
--- a/lld/test/ELF/relocatable-crel.s
+++ b/lld/test/ELF/relocatable-crel.s
@@ -105,3 +105,25 @@ fb:
 .rept 12
 .long _start
 .endr
+
+#--- c.s
+## The section symbol is demoted to Undefined, so encodeOneCrel emits R_*_NONE with symidx=0.
+# RUN: llvm-mc -filetype=obj -triple=x86_64 -crel c.s -o c.o
+# RUN: ld.lld -r -T c.lds c.o -o out3
+# RUN: llvm-readobj -r out3 | FileCheck %s --check-prefix=DISCARD
+
+# DISCARD:      .crel.data {
+# DISCARD-NEXT:   0x0 R_X86_64_32 foo 0x1
+# DISCARD-NEXT:   0x4 R_X86_64_NONE - 0x0
+# DISCARD-NEXT:   0x8 R_X86_64_32 foo 0x2
+# DISCARD-NEXT: }
+.section .text.discard,"ax"
+.byte 0
+
+.data
+.long foo+1
+.long .text.discard
+.long foo+2
+
+#--- c.lds
+SECTIONS { /DISCARD/ : { *(.text.discard) } }



More information about the llvm-commits mailing list