Skip to content

Conversation

CompilerWang
Copy link

@CompilerWang CompilerWang commented Sep 20, 2025

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.

#158569

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2025

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: None (CompilerWang)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/159895.diff

2 Files Affected:

  • (modified) lld/ELF/ScriptParser.cpp (+7-2)
  • (added) lld/test/ELF/linkerscript/overlay-symbol-assign.test (+31)
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..b48385cf22358
--- /dev/null
+++ b/lld/test/ELF/linkerscript/overlay-symbol-assign.test
@@ -0,0 +1,31 @@
+# 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-unknown-linux - -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 --sections -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
\ No newline at end of file

@CompilerWang
Copy link
Author

@EugeneZelenko Could you please be a reviewer or add other reviewer to this PR? Thank you!

@CompilerWang
Copy link
Author

@MaskRay Could you please be a reviewer or add other reviewer to this PR? Thank you!

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.
@CompilerWang CompilerWang force-pushed the support-symbol-assign-in-overlay-section branch from 64eb239 to 68e8da8 Compare September 20, 2025 08:22
# 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete blank lines at the end

# CHECK: Name Type Address Off Size
# CHECK: .out.big PROGBITS 0000000000001000 001000 000108
# CHECK-NEXT: .out.small PROGBITS 0000000000001000 002000 000004

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one blank line

@@ -0,0 +1,32 @@
# REQUIRES: x86
# RUN: echo 'nop; .section .small, "a"; .long 0; .section .big1, "a"; .quad 1; .section .big2, "a"; .quad 2;' \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RUN: rm -rf %t && split-file %s %t && cd %t pattern to prepare the input assembly file.
You can use #--- a.s for assembly and #--- 1.lds for the linker script.

.out.big {
*(.big1)
. = ALIGN(0x100);
*(.big2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also define a symbol sym = .? Does GNU ld accept it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants