[lld] [LLD][ELF] Support OVERLAY symbol assignments (PR #159895)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 20 01:22:06 PDT 2025
https://github.com/CompilerWang updated https://github.com/llvm/llvm-project/pull/159895
>From 68e8da8b1dfec43ac8fe874a41baf9051b1e1198 Mon Sep 17 00:00:00 2001
From: wangwei <wangwei822 at hisilicon.com>
Date: Mon, 15 Sep 2025 07:57:24 +0000
Subject: [PATCH] [LLD][ELF] Support OVERLAY symbol assignments
This enables symbol assignment within OVERLAY linker script descriptions. This is a valuable addition to the OVERLAY syntax, as it allows defining symbols that can be used for alignment within overlay sections.
---
lld/ELF/ScriptParser.cpp | 9 ++++--
.../linkerscript/overlay-symbol-assign.test | 32 +++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
create mode 100644 lld/test/ELF/linkerscript/overlay-symbol-assign.test
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index ddfa24d9cacf5..babc5cfbf43ae 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -996,8 +996,13 @@ OutputDesc *ScriptParser::readOverlaySectionDescription() {
ctx.script->createOutputSection(readName(), getCurrentLocation());
osd->osec.inOverlay = true;
expect("{");
- while (auto tok = till("}"))
- osd->osec.commands.push_back(readInputSectionDescription(tok));
+ while (auto tok = till("}")) {
+ if (SymbolAssignment *assign = readAssignment(tok)) {
+ osd->osec.commands.push_back(assign);
+ } else {
+ osd->osec.commands.push_back(readInputSectionDescription(tok));
+ }
+ }
osd->osec.phdrs = readOutputSectionPhdrs();
return osd;
}
diff --git a/lld/test/ELF/linkerscript/overlay-symbol-assign.test b/lld/test/ELF/linkerscript/overlay-symbol-assign.test
new file mode 100644
index 0000000000000..281063bbbbf74
--- /dev/null
+++ b/lld/test/ELF/linkerscript/overlay-symbol-assign.test
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+# RUN: echo 'nop; .section .small, "a"; .long 0; .section .big1, "a"; .quad 1; .section .big2, "a"; .quad 2;' \
+# RUN: | llvm-mc -filetype=obj -triple=x86_64 - -o %t.o
+# RUN: ld.lld %t.o --script %s -o %t
+
+SECTIONS {
+ OVERLAY 0x1000 : AT ( 0x4000 ) {
+ .out.big {
+ *(.big1)
+ . = ALIGN(0x100);
+ *(.big2)
+ }
+ .out.small { *(.small) }
+ }
+}
+
+## A variant of overlay.test with explicit program header assingment.
+## Check that we generate two program headers consistent with the overlay
+
+# RUN: llvm-readelf -S -l %t | FileCheck %s
+
+# CHECK: Section Headers:
+# CHECK: Name Type Address Off Size
+# CHECK: .out.big PROGBITS 0000000000001000 001000 000108
+# CHECK-NEXT: .out.small PROGBITS 0000000000001000 002000 000004
+
+
+# CHECK: Program Headers:
+# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
+# CHECK-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000004000 0x000108 0x000108 R 0x1000
+# CHECK-NEXT: LOAD 0x002000 0x0000000000001000 0x0000000000004108 0x000004 0x000004 R 0x1000
+
More information about the llvm-commits
mailing list