https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/68507
>From b2a442d6aca019a42baef45e1f245dc1ea5bd8a1 Mon Sep 17 00:00:00 2001 From: Ben Shi <benn...@tencent.com> Date: Wed, 11 Oct 2023 14:05:14 +0800 Subject: [PATCH] [clang][driver] Add avr-libc's default linker script to lld 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. --- clang/lib/Driver/ToolChains/AVR.cpp | 14 ++++++++++-- .../usr/lib/avr/lib/ldscripts/avrtiny.x | 0 .../usr/lib/avr/lib/ldscripts/avrxmega6.x | 0 clang/test/Driver/avr-ld.c | 22 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrtiny.x create mode 100644 clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x diff --git a/clang/lib/Driver/ToolChains/AVR.cpp b/clang/lib/Driver/ToolChains/AVR.cpp index e312fa155e11bf8..2e46b25aeba75ef 100644 --- a/clang/lib/Driver/ToolChains/AVR.cpp +++ b/clang/lib/Driver/ToolChains/AVR.cpp @@ -554,8 +554,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 0f12607fe9d69eb..3e4114485332fcd 100644 --- a/clang/test/Driver/avr-ld.c +++ b/clang/test/Driver/avr-ld.c @@ -58,6 +58,28 @@ // 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: "-m + +// 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: "-m + +// 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.*"}} {{.*}} "--start-group" {{.*}} "--end-group" +// LINKT2-NOT: "-T +// LINKT2-NOT: "-m + +// 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: "-m + // RUN: %clang -### -r --target=avr -mmcu=atmega328 --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck --check-prefix=LINKU %s // LINKU: {{".*ld.*"}} {{.*}} "-r" {{.*}} "-mavr5" // LINKU-NOT: "--gc-sections" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits