[lld] Fix .bss section accumulated in ELF file (PR #78265)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 20:29:43 PDT 2025


================
@@ -2629,17 +2629,22 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
   // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
   // increasing rather than setting to zero.
   if (os->type == SHT_NOBITS &&
-      (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
-     return off;
+      (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os)) {
+    // Update NOBITS size in the current segment
+    if (os->ptLoad)
+      os->ptLoad->p_nobits += os->size;
+    return off;
+  }
 
   // If the section is not in a PT_LOAD, we just have to align it.
   if (!os->ptLoad)
-     return alignToPowerOf2(off, os->addralign);
+    return alignToPowerOf2(off, os->addralign);
 
   // If two sections share the same PT_LOAD the file offset is calculated
-  // using this formula: Off2 = Off1 + (VA2 - VA1).
+  // using this formula: Off2 = Off1 + (VA2 - VA1 - p_nobits).
   OutputSection *first = os->ptLoad->firstSec;
-  return first->offset + os->addr - first->addr;
+
+  return first->offset + (os->addr - first->addr - os->ptLoad->p_nobits);
----------------
MaskRay wrote:

This is incorrect. If two sections are within the same segment, their file offset difference must match their address difference, regardless of whether there is a NOBITS section in between.

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


More information about the llvm-commits mailing list