[lld] [LLD][ELF] Skip non-SHF_ALLOC sections when checking max VA and max VA difference in relaxOnce() (PR #145863)

Mingjie Xu via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 02:59:16 PDT 2025


https://github.com/Enna1 created https://github.com/llvm/llvm-project/pull/145863

For non-SHF_ALLOC sections, sh_addr is set to 0.
Skip sections without SHF_ALLOC flag, so `minVA` will not be set to 0 with non-SHF_ALLOC sections, and the size of non-SHF_ALLOC sections will not contribute to `maxVA`.

>From cd72557d0f285d4af9080ed36d093e8e524001a3 Mon Sep 17 00:00:00 2001
From: "xumingjie.enna1" <xumingjie.enna1 at bytedance.com>
Date: Thu, 26 Jun 2025 09:35:14 +0800
Subject: [PATCH] [LLD][ELF] Skip non-SHF_ALLOC sections when checking max VA
 and max VA difference in relaxOnce()

For non-SHF_ALLOC sections, sh_addr is set to 0.
Skip sections without SHF_ALLOC flag, so `minVA` will not be set to 0 with
non-SHF_ALLOC sections, and the size of non-SHF_ALLOC sections will not
contribute to `maxVA`.
---
 lld/ELF/Arch/X86_64.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index 163505102d0ec..488f4803b2cb4 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -320,6 +320,8 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
 bool X86_64::relaxOnce(int pass) const {
   uint64_t minVA = UINT64_MAX, maxVA = 0;
   for (OutputSection *osec : ctx.outputSections) {
+    if (!(osec->flags & SHF_ALLOC))
+      continue;
     minVA = std::min(minVA, osec->addr);
     maxVA = std::max(maxVA, osec->addr + osec->size);
   }



More information about the llvm-commits mailing list