Skip to content

Commit

Permalink
[clang][driver] Support option '-r' for target AVR (#68484)
Browse files Browse the repository at this point in the history
If '-r' is specified with target AVR:

1. Do not link to the avr-libc.
2. Do not emit some conflict options.
3. Do not emit any sub-target related address information/warning.
  • Loading branch information
benshi001 committed Oct 11, 2023
1 parent 776889b commit 1d8d326
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
37 changes: 21 additions & 16 deletions clang/lib/Driver/ToolChains/AVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Output.getFilename());

// Enable garbage collection of unused sections.
CmdArgs.push_back("--gc-sections");
if (!Args.hasArg(options::OPT_r))
CmdArgs.push_back("--gc-sections");

// Add library search paths before we specify libraries.
Args.AddAllArgs(CmdArgs, options::OPT_L);
Expand All @@ -471,7 +472,7 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,

// Only add default libraries if the user hasn't explicitly opted out.
bool LinkStdlib = false;
if (!Args.hasArg(options::OPT_nostdlib) &&
if (!Args.hasArg(options::OPT_nostdlib) && !Args.hasArg(options::OPT_r) &&
!Args.hasArg(options::OPT_nodefaultlibs)) {
if (!CPU.empty()) {
if (!FamilyName) {
Expand All @@ -497,13 +498,17 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
D.Diag(diag::warn_drv_avr_stdlib_not_linked);
}

if (SectionAddressData) {
CmdArgs.push_back(
Args.MakeArgString("--defsym=__DATA_REGION_ORIGIN__=0x" +
Twine::utohexstr(*SectionAddressData)));
} else {
// We do not have an entry for this CPU in the address mapping table yet.
D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented) << CPU;
if (!Args.hasArg(options::OPT_r)) {
if (SectionAddressData) {
CmdArgs.push_back(
Args.MakeArgString("--defsym=__DATA_REGION_ORIGIN__=0x" +
Twine::utohexstr(*SectionAddressData)));
} else {
// We do not have an entry for this CPU in the address mapping table
// yet.
D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented)
<< CPU;
}
}

if (D.isUsingLTO()) {
Expand Down Expand Up @@ -554,17 +559,17 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,

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

// Specify the family name as the emulation mode to use.
// This is almost always required because otherwise avr-ld
// will assume 'avr2' and warn about the program being larger
// than the bare minimum supports.
if (Linker.find("avr-ld") != std::string::npos)
CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
} else {
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
}

// Specify the family name as the emulation mode to use.
// This is almost always required because otherwise avr-ld
// will assume 'avr2' and warn about the program being larger
// than the bare minimum supports.
if (Linker.find("avr-ld") != std::string::npos && FamilyName)
CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));

C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
CmdArgs, Inputs, Output));
Expand Down
28 changes: 28 additions & 0 deletions clang/test/Driver/avr-ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@
// 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 -### -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"
// LINKU-NOT: "--defsym
// LINKU-NOT: "-l

// RUN: %clang -### -r --target=avr -mmcu=atmega328 -fuse-ld=lld --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck --check-prefix=LINKV %s
// LINKV: {{".*ld.*"}} {{.*}} "-r"
// LINKV-NOT: "--gc-sections"
// LINKV-NOT: "--defsym
// LINKV-NOT: "-l
// LINKV-NOT: "-m

// RUN: %clang -### -r --target=avr -mmcu=atmega328 -lm --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck --check-prefix=LINKW %s
// LINKW: {{".*ld.*"}} {{.*}} "-r" "-lm" {{.*}} "-mavr5"
// LINKW-NOT: "--gc-sections"
// LINKW-NOT: "--defsym

// RUN: %clang -### -r --target=avr --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck --check-prefix=LINKX %s
// LINKX: warning: no target microcontroller specified
// LINKX: {{".*ld.*"}} {{.*}} "-r" {{.*}}
// LINKX-NOT: warning: {{.*}} standard library
// LINKX-NOT: warning: {{.*}} data section address
// LINKX-NOT: "--gc-sections"
// LINKX-NOT: "--defsym
// LINKX-NOT: "-l
// LINKX-NOT: "-m

0 comments on commit 1d8d326

Please sign in to comment.