Skip to content

Commit

Permalink
[clang][driver] Add avr-libc's default linker script to lld (#68507)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
benshi001 authored Oct 15, 2023
1 parent 0d661e9 commit 169f60f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions clang/lib/Driver/ToolChains/AVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions clang/test/Driver/avr-ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 169f60f

Please sign in to comment.