Skip to content

Commit 169f60f

Browse files
authored
[clang][driver] Add avr-libc's default linker script to lld (#68507)
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)
1 parent 0d661e9 commit 169f60f

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

clang/lib/Driver/ToolChains/AVR.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,18 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
554554

555555
CmdArgs.push_back("--end-group");
556556

557-
// Add user specified linker script.
558-
Args.AddAllArgs(CmdArgs, options::OPT_T);
557+
// Add avr-libc's linker script to lld by default, if it exists.
558+
if (!Args.hasArg(options::OPT_T) &&
559+
Linker.find("lld") != std::string::npos) {
560+
std::string Path(*AVRLibcRoot + "/lib/ldscripts/");
561+
Path += *FamilyName;
562+
Path += ".x";
563+
if (llvm::sys::fs::exists(Path))
564+
CmdArgs.push_back(Args.MakeArgString("-T" + Path));
565+
}
566+
// Otherwise add user specified linker script to either avr-ld or lld.
567+
else
568+
Args.AddAllArgs(CmdArgs, options::OPT_T);
559569

560570
if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
561571
CmdArgs.push_back("--relax");

clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrtiny.x

Whitespace-only changes.

clang/test/Driver/Inputs/basic_avr_tree/usr/lib/avr/lib/ldscripts/avrxmega6.x

Whitespace-only changes.

clang/test/Driver/avr-ld.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@
5858
// LINKS: {{".*ld.*"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x800100" "-plugin-opt=mcpu=atmega328"
5959
// LINKS-NOT: "-plugin-opt=thinlto"
6060

61+
// RUN: %clang -### --target=avr -mmcu=attiny40 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT0 %s
62+
// LINKT0: {{".*lld.*"}} {{.*}} {{"-T.*avrtiny.x"}}
63+
// LINKT0-NOT: "-m
64+
65+
// RUN: %clang -### --target=avr -mmcu=atxmega384c3 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT1 %s
66+
// LINKT1: {{".*lld.*"}} {{.*}} {{"-T.*avrxmega6.x"}}
67+
// LINKT1-NOT: "-m
68+
69+
// RUN: %clang -### --target=avr -mmcu=atmega328 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT2 %s
70+
// LINKT2: {{".*lld.*"}} {{.*}} "--start-group" {{.*}} "--end-group"
71+
// LINKT2-NOT: "-T
72+
// LINKT2-NOT: "-m
73+
74+
// RUN: %clang -### --target=avr -mmcu=attiny40 --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKT3 %s
75+
// LINKT3: {{".*ld.*"}} {{.*}} "-mavrtiny"
76+
// LINKT3-NOT: "-T
77+
78+
// 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
79+
// LINKT4: {{".*lld.*"}} {{.*}} {{"-T.*avrxmega6.x"}}
80+
// LINKT4-NOT: {{"-T.*avrtiny.x"}}
81+
// LINKT4-NOT: "-m
82+
6183
// RUN: %clang -### -r --target=avr -mmcu=atmega328 --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck --check-prefix=LINKU %s
6284
// LINKU: {{".*ld.*"}} {{.*}} "-r" {{.*}} "-mavr5"
6385
// LINKU-NOT: "--gc-sections"

0 commit comments

Comments
 (0)