Skip to content

[LLD] [ELF] Add support for linker script unary plus operator #121508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

partaror
Copy link
Contributor

@partaror partaror commented Jan 2, 2025

This commit adds support for linker script unary plus ('+') operator. It is helpful for improving compatibility between LLD and GNU LD.

Closes #118047

@llvmbot
Copy link
Member

llvmbot commented Jan 2, 2025

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Parth Arora (partaror)

Changes

This commit adds support for linker script unary plus ('+') operator. It is helpful for improving compatibility between LLD and GNU LD.


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

2 Files Affected:

  • (modified) lld/ELF/ScriptParser.cpp (+3)
  • (modified) lld/test/ELF/linkerscript/operators.test (+2)
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index f53515cc3f3c0d..49ddd5e7999cfd 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1489,6 +1489,9 @@ Expr ScriptParser::readPrimary() {
     Expr e = readPrimary();
     return [=] { return -e().getValue(); };
   }
+  if (consume("+")) {
+    return readPrimary();
+  }
 
   StringRef tok = next();
   std::string location = getCurrentLocation();
diff --git a/lld/test/ELF/linkerscript/operators.test b/lld/test/ELF/linkerscript/operators.test
index 27209a2e40f598..d7b3becf36d706 100644
--- a/lld/test/ELF/linkerscript/operators.test
+++ b/lld/test/ELF/linkerscript/operators.test
@@ -73,6 +73,7 @@ SECTIONS {
   log2ceil100000000 = LOG2CEIL(0x100000000);
   log2ceil100000001 = LOG2CEIL(0x100000001);
   log2ceilmax = LOG2CEIL(0xffffffffffffffff);
+  unaryadd = +3 + ++5;
 }
 
 # CHECK:      0000000000000002 A unary
@@ -126,6 +127,7 @@ SECTIONS {
 # CHECK-NEXT: 0000000000000020 A log2ceil100000000
 # CHECK-NEXT: 0000000000000021 A log2ceil100000001
 # CHECK-NEXT: 0000000000000040 A log2ceilmax
+# CHECK-NEXT: 0000000000000008 A unaryadd
 
 ## Mailformed number error.
 # RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script

Copy link
Member

@lenary lenary left a comment

Choose a reason for hiding this comment

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

LGTM

@lenary lenary requested a review from smithp35 January 3, 2025 15:01
@MaskRay
Copy link
Member

MaskRay commented Jan 3, 2025

> Fixes 118047

I'd prefer "Close" since this is a new minor feature instead of a bugfix.

@partaror
Copy link
Contributor Author

partaror commented Jan 3, 2025

I'd prefer "Close" since this is a new minor feature instead of a bugfix.

Agreed. I have replaced Fixes with Closes in both the PR description and the commit message.

@partaror
Copy link
Contributor Author

Gentle ping for review @smithp35

Copy link
Collaborator

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

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

Apologies, I didn't think you were waiting on me. I think an approval from MaskRay is sufficient.

LGTM too.

@partaror
Copy link
Contributor Author

@MaskRay Can you please let me know if it is okay if I merge the pull-request myself?

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

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

LGTM

This commit adds support for linker script unary plus ('+') operator.
It is helpful for improving compatibility between LLD and GNU LD.

Closes llvm#118047
@MaskRay MaskRay merged commit 8c2030b into llvm:main Jan 22, 2025
8 checks passed
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.

[LLD] Unary '+' operator is not supported in linker script
5 participants