Skip to content
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

[clang][driver] Add avr-libc's default linker script to lld #68507

Merged
merged 1 commit into from
Oct 15, 2023
Merged

[clang][driver] Add avr-libc's default linker script to lld #68507

merged 1 commit into from
Oct 15, 2023

Conversation

benshi001
Copy link
Member

If -fuse-ld=lld is specified but no user linker script is offered, we try to use avr-libc's default one for lld. (not needed for GNU ld)

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 8, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2023

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Changes

If -fuse-ld=lld is specified but no user linker script is offered, we try to use avr-libc's default one for lld. (not needed for GNU ld)


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

4 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/AVR.cpp (+12-2)
  • (added) clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrtiny.x ()
  • (added) clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x ()
  • (modified) clang/test/Driver/avr-ld.c (+21)
diff --git a/clang/lib/Driver/ToolChains/AVR.cpp b/clang/lib/Driver/ToolChains/AVR.cpp
index 81f501d417345d1..9b15b22ecb2218a 100644
--- a/clang/lib/Driver/ToolChains/AVR.cpp
+++ b/clang/lib/Driver/ToolChains/AVR.cpp
@@ -549,8 +549,18 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
     CmdArgs.push_back("--end-group");
 
-    // Add user specified linker script.
-    Args.AddAllArgs(CmdArgs, options::OPT_T);
+    // Add avr-libc's linker script to lld by default, if it exists.
+    if (!Args.hasArg(options::OPT_T) &&
+        Linker.find("lld") != std::string::npos) {
+      std::string Path(*AVRLibcRoot + "/lib/ldscripts/");
+      Path += *FamilyName;
+      Path += ".x";
+      if (llvm::sys::fs::exists(Path))
+        CmdArgs.push_back(Args.MakeArgString("-T" + Path));
+    }
+    // Otherwise add user specified linker script to either avr-ld or lld.
+    else
+      Args.AddAllArgs(CmdArgs, options::OPT_T);
 
     if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
       CmdArgs.push_back("--relax");
diff --git a/clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrtiny.x b/clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrtiny.x
new file mode 100644
index 000000000000000..e69de29bb2d1d64
diff --git a/clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x b/clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x
new file mode 100644
index 000000000000000..e69de29bb2d1d64
diff --git a/clang/test/Driver/avr-ld.c b/clang/test/Driver/avr-ld.c
index 4042ecb89adf5f1..769eb8763caaf61 100644
--- a/clang/test/Driver/avr-ld.c
+++ b/clang/test/Driver/avr-ld.c
@@ -57,3 +57,24 @@
 // RUN: %clang -### --target=avr -mmcu=atmega328 -fuse-ld=lld -flto --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKS %s
 // LINKS: {{".*ld.*"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x800100" "-plugin-opt=mcpu=atmega328"
 // LINKS-NOT: "-plugin-opt=thinlto"
+
+// RUN: %clang -### --target=avr -mmcu=attiny40 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT0 %s
+// LINKT0: {{".*lld.*"}} {{.*}} {{"-T.*avrtiny.x"}}
+// LINKT0-NOT: "-mavrtiny"
+
+// RUN: %clang -### --target=avr -mmcu=atxmega384c3 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT1 %s
+// LINKT1: {{".*lld.*"}} {{.*}} {{"-T.*avrxmega6.x"}}
+// LINKT1-NOT: "-mavrxmega6"
+
+// RUN: %clang -### --target=avr -mmcu=atmega328 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT2 %s
+// LINKT2: {{".*lld.*"}}
+// LINKT2-NOT: {{"-T.*"}}
+
+// RUN: %clang -### --target=avr -mmcu=attiny40 --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT3 %s
+// LINKT3: {{".*ld.*"}} {{.*}} "-mavrtiny"
+// LINKT3-NOT: {{"-T.*"}}
+
+// RUN: %clang -### --target=avr -mmcu=attiny40 --sysroot %S/Inputs/basic_avr_tree -fuse-ld=lld -T %S/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x %s 2>&1 | FileCheck -check-prefix LINKT4 %s
+// LINKT4: {{".*lld.*"}} {{.*}} {{"-T.*avrxmega6.x"}}
+// LINKT4-NOT: {{"-T.*avrtiny.x"}}
+// LINKT4-NOT: "-mavrtiny"

If lld is specified but no user linker script is offered,
we try to use avr-libc's default ones. This is unnecessary
for GNU ld.
Copy link
Contributor

@jacquesguan jacquesguan left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants