[lld] [LLD] Avoid accumulating .bss size for zero-sized sections (PR #223349)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 03:11:26 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld-elf
Author: Shivam Gupta (xgupta)
<details>
<summary>Changes</summary>
A zero-sized output section following a NOBITS section in the same PT_LOAD was assigned a file offset that included the size of the preceding NOBITS section.
Do not apply the usual VA-delta calculation in this case, since a zero-sized section has no file contents.
Fixes #<!-- -->78261
---
Full diff: https://github.com/llvm/llvm-project/pull/223349.diff
3 Files Affected:
- (modified) lld/ELF/Writer.cpp (+12-2)
- (added) lld/test/ELF/nobits-followed-by-nonzero-section.s (+31)
- (added) lld/test/ELF/nobits-followed-by-zero-size-section.s (+40)
``````````diff
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index e2792dc31d031..6fb3ffa418950 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2578,7 +2578,8 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
// Compute an in-file position for a given section. The file offset must be the
// same with its virtual address modulo the page size, so that the loader can
// load executables without any address adjustment.
-static uint64_t computeFileOffset(Ctx &ctx, OutputSection *os, uint64_t off) {
+static uint64_t computeFileOffset(Ctx &ctx, OutputSection *os, uint64_t off,
+ OutputSection *prev) {
// The first section in a PT_LOAD has to have congruent offset and address
// modulo the maximum page size.
if (os->ptLoad && os->ptLoad->firstSec == os)
@@ -2594,6 +2595,13 @@ static uint64_t computeFileOffset(Ctx &ctx, OutputSection *os, uint64_t off) {
if (!os->ptLoad)
return alignToPowerOf2(off, os->addralign);
+ // A zero-sized section following a NOBITS section in the same PT_LOAD does
+ // not occupy file space, so its file offset does not need to account for the
+ // size of the preceding NOBITS section.
+ if (os->size == 0 && prev && prev->ptLoad == os->ptLoad &&
+ prev->type == SHT_NOBITS)
+ return off;
+
// If two sections share the same PT_LOAD the file offset is calculated
// using this formula: Off2 = Off1 + (VA2 - VA1).
OutputSection *first = os->ptLoad->firstSec;
@@ -2637,13 +2645,15 @@ template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
// Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
// will not occupy file offsets contained by a PT_LOAD.
+ OutputSection *prev = nullptr;
for (OutputSection *sec : ctx.outputSections) {
if (!(sec->flags & SHF_ALLOC))
continue;
- off = computeFileOffset(ctx, sec, off);
+ off = computeFileOffset(ctx, sec, off, prev);
sec->offset = off;
if (sec->type != SHT_NOBITS)
off += sec->size;
+ prev = sec;
// If this is a last section of the last executable segment and that
// segment is the last loadable segment, align the offset of the
diff --git a/lld/test/ELF/nobits-followed-by-nonzero-section.s b/lld/test/ELF/nobits-followed-by-nonzero-section.s
new file mode 100644
index 0000000000000..fea641b1374df
--- /dev/null
+++ b/lld/test/ELF/nobits-followed-by-nonzero-section.s
@@ -0,0 +1,31 @@
+# REQUIRES: x86
+## A non-empty section following .bss must account for .bss's size.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: echo 'SECTIONS { \
+# RUN: .text : {} \
+# RUN: .data : {} \
+# RUN: .bss : {} \
+# RUN: .ldata : { *(.ldata) } \
+# RUN: }' > %t.lds
+# RUN: ld.lld -T %t.lds %t.o -o %t
+# RUN: llvm-readelf -S %t | FileCheck %s
+
+## The .ldata section's file offset must equal .bss's file offset
+## plus .bss's size.
+# CHECK: .bss NOBITS {{[0-9a-f]+}} [[#%.6x,OFF:]] [[#%.6x,SZ:]]
+# CHECK-NEXT: .ldata PROGBITS {{[0-9a-f]+}} [[#%.6x,OFF+SZ]] 000001
+
+.text
+.globl _start
+_start:
+ ret
+
+.data
+.byte 1
+
+.bss
+.space 0xfa0
+
+.section .ldata,"aw"
+.byte 2
diff --git a/lld/test/ELF/nobits-followed-by-zero-size-section.s b/lld/test/ELF/nobits-followed-by-zero-size-section.s
new file mode 100644
index 0000000000000..3f605ac7d944c
--- /dev/null
+++ b/lld/test/ELF/nobits-followed-by-zero-size-section.s
@@ -0,0 +1,40 @@
+# REQUIRES: x86
+## A zero-sized section following .bss must not account for .bss's size.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: echo 'SECTIONS { \
+# RUN: .text : {} \
+# RUN: .data : {} \
+# RUN: .bss : {} \
+# RUN: .ldata : { . = .; } \
+# RUN: }' > %t-a.lds
+# RUN: ld.lld -T %t-a.lds %t.o -o %t-a
+# RUN: llvm-readelf -S %t-a | FileCheck --check-prefix=ZERO-SIZE %s
+
+# RUN: echo 'SECTIONS { \
+# RUN: .text : {} \
+# RUN: .data : {} \
+# RUN: .bss : {} \
+# RUN: .ldata : { . = ALIGN(8); } \
+# RUN: }' > %t-b.lds
+# RUN: ld.lld -T %t-b.lds %t.o -o %t-b
+# RUN: llvm-readelf -S %t-b | FileCheck --check-prefix=ALIGN-ZERO-SIZE %s
+
+## The .ldata section's file offset must equal .bss's file offset.
+# ZERO-SIZE: .bss NOBITS {{[0-9a-f]+}} [[OFF:[0-9a-f]+]] {{[0-9a-f]+}}
+# ZERO-SIZE-NEXT: .ldata PROGBITS {{[0-9a-f]+}} [[OFF]] 000000
+
+# ALIGN-ZERO-SIZE: .bss NOBITS {{[0-9a-f]+}} [[OFF:[0-9a-f]+]] {{[0-9a-f]+}}
+# ALIGN-ZERO-SIZE-NEXT: .ldata PROGBITS {{[0-9a-f]+}} [[OFF]] 000000
+
+.text
+.globl _start
+_start:
+ ret
+
+.data
+.byte 1
+
+.bss
+.space 0xfa6
``````````
</details>
https://github.com/llvm/llvm-project/pull/223349
More information about the llvm-commits
mailing list