[lld] [LLD] Allow all output-section-commands in OVERLAYS. (PR #203524)
Peter Smith via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 12 06:21:59 PDT 2026
https://github.com/smithp35 created https://github.com/llvm/llvm-project/pull/203524
The GNU ld grammar for overlays is:
secname1
{
output-section-command
output-section-command
...
}
secname2
...
The output-section-commands are the same as in an OutputSection. At present we have a stripped down parser that only supports InputSectionDescriptions, this does not permit other useful commands such as defining symbols.
Due to recent refactoring it is now simple to reuse the parser for an Output Section command rather than using a custom one.
I came to this via a comment in #202266. There looks to have been an attempt to fix this in #159895, but it looks abandoned after review comments.
Fixes #158569.
>From e43276c2b7b669bfb6959dcf25a9132f712faa8f Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.smith at arm.com>
Date: Fri, 12 Jun 2026 13:41:00 +0100
Subject: [PATCH] [LLD] Allow all output-section-commands in OVERLAYS.
The GNU ld grammar for overlays is:
secname1
{
output-section-command
output-section-command
...
}
secname2
...
The output-section-commands are the same as in an OutputSection. At
present we have a stripped down parser that only supports
InputSectionDescriptions, this does not permit other useful
commands such as defining symbols.
Due to recent refactoring it is now simple to reuse the parser for
an Output Section command rather than using a custom one.
I came to this via a comment in #202266. There looks to have been
an attempt to fix this in #159895, but it looks abandoned after
review comments.
Fixes #158569.
---
lld/ELF/ScriptParser.cpp | 2 +-
.../ELF/linkerscript/overlay-symbols.test | 49 +++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/linkerscript/overlay-symbols.test
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 5383f3eed3f8b..0c54fde488292 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1027,7 +1027,7 @@ OutputDesc *ScriptParser::readOverlaySectionDescription() {
osd->osec.inOverlay = true;
expect("{");
while (auto tok = till("}"))
- osd->osec.commands.push_back(readInputSectionDescription(tok));
+ readOutputSectionStmt(osd->osec, tok);
osd->osec.phdrs = readOutputSectionPhdrs();
return osd;
}
diff --git a/lld/test/ELF/linkerscript/overlay-symbols.test b/lld/test/ELF/linkerscript/overlay-symbols.test
new file mode 100644
index 0000000000000..69475afdee57b
--- /dev/null
+++ b/lld/test/ELF/linkerscript/overlay-symbols.test
@@ -0,0 +1,49 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
+# RUN: ld.lld a.o --script overlay.lds -o a
+# RUN: llvm-readelf --symbols a | FileCheck %s
+
+CHECK: {{[0-9]+}}: 0000000000001000 0 NOTYPE GLOBAL DEFAULT 2 __data_start
+CHECK-NEXT: {{[0-9]+}}: 0000000000001017 0 NOTYPE GLOBAL DEFAULT 2 __data_end
+CHECK-NEXT: {{[0-9]+}}: 0000000000001000 0 NOTYPE GLOBAL DEFAULT 3 __bss_start
+CHECK-NEXT: {{[0-9]+}}: 0000000000001008 0 NOTYPE GLOBAL DEFAULT 3 __bss_end
+CHECK-NEXT: {{[0-9]+}}: 0000000000001017 0 NOTYPE GLOBAL DEFAULT 3 __end
+
+
+#--- overlay.lds
+
+SECTIONS {
+ .text : { *(.text) }
+ OVERLAY 0x1000 : {
+ .data {
+ __data_start = .;
+ *(.data)
+ QUAD(1)
+ LONG(1)
+ SHORT(1)
+ BYTE(1)
+ __data_end = .;
+ }
+ .bss {
+ __bss_start = .;
+ *(.bss)
+ . += 0x4;
+ __bss_end = .;
+ }
+ }
+ __end = .;
+}
+
+#--- a.s
+
+ .globl __start
+ .text
+__start:
+ nop
+
+ .data
+ .space 0x8
+
+ .bss
+ .space 0x4
More information about the llvm-commits
mailing list