1 //===--- AVR.cpp - AVR ToolChain Implementations ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "AVR.h"
10 #include "CommonArgs.h"
11 #include "clang/Driver/Compilation.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/InputInfo.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/MC/MCSubtargetInfo.h"
19 #include "llvm/MC/SubtargetFeature.h"
20 #include "llvm/Option/ArgList.h"
21 #include "llvm/Support/FileSystem.h"
22 
23 using namespace clang::driver;
24 using namespace clang::driver::toolchains;
25 using namespace clang::driver::tools;
26 using namespace clang;
27 using namespace llvm::opt;
28 
29 namespace {
30 
31 const struct {
32   StringRef Name;
33   std::string SubPath;
34   StringRef Family;
35   unsigned DataAddr;
36 } MCUInfo[] = {
37     {"at90s1200", "", "avr1", 0},
38     {"attiny11", "", "avr1", 0},
39     {"attiny12", "", "avr1", 0},
40     {"attiny15", "", "avr1", 0},
41     {"attiny28", "", "avr1", 0},
42     {"at90s2313", "tiny-stack", "avr2", 0x800060},
43     {"at90s2323", "tiny-stack", "avr2", 0x800060},
44     {"at90s2333", "tiny-stack", "avr2", 0x800060},
45     {"at90s2343", "tiny-stack", "avr2", 0x800060},
46     {"at90s4433", "tiny-stack", "avr2", 0x800060},
47     {"attiny22", "tiny-stack", "avr2", 0x800060},
48     {"attiny26", "tiny-stack", "avr2", 0x800060},
49     {"at90s4414", "", "avr2", 0x800060},
50     {"at90s4434", "", "avr2", 0x800060},
51     {"at90s8515", "", "avr2", 0x800060},
52     {"at90c8534", "", "avr2", 0x800060},
53     {"at90s8535", "", "avr2", 0x800060},
54     {"attiny13", "avr25/tiny-stack", "avr25", 0x800060},
55     {"attiny13a", "avr25/tiny-stack", "avr25", 0x800060},
56     {"attiny2313", "avr25/tiny-stack", "avr25", 0x800060},
57     {"attiny2313a", "avr25/tiny-stack", "avr25", 0x800060},
58     {"attiny24", "avr25/tiny-stack", "avr25", 0x800060},
59     {"attiny24a", "avr25/tiny-stack", "avr25", 0x800060},
60     {"attiny25", "avr25/tiny-stack", "avr25", 0x800060},
61     {"attiny261", "avr25/tiny-stack", "avr25", 0x800060},
62     {"attiny261a", "avr25/tiny-stack", "avr25", 0x800060},
63     {"at86rf401", "avr25", "avr25", 0x800060},
64     {"ata5272", "avr25", "avr25", 0x800100},
65     {"attiny4313", "avr25", "avr25", 0x800060},
66     {"attiny44", "avr25", "avr25", 0x800060},
67     {"attiny44a", "avr25", "avr25", 0x800060},
68     {"attiny84", "avr25", "avr25", 0x800060},
69     {"attiny84a", "avr25", "avr25", 0x800060},
70     {"attiny45", "avr25", "avr25", 0x800060},
71     {"attiny85", "avr25", "avr25", 0x800060},
72     {"attiny441", "avr25", "avr25", 0x800100},
73     {"attiny461", "avr25", "avr25", 0x800060},
74     {"attiny461a", "avr25", "avr25", 0x800060},
75     {"attiny841", "avr25", "avr25", 0x800100},
76     {"attiny861", "avr25", "avr25", 0x800060},
77     {"attiny861a", "avr25", "avr25", 0x800060},
78     {"attiny87", "avr25", "avr25", 0x800100},
79     {"attiny43u", "avr25", "avr25", 0x800060},
80     {"attiny48", "avr25", "avr25", 0x800100},
81     {"attiny88", "avr25", "avr25", 0x800100},
82     {"attiny828", "avr25", "avr25", 0x800100},
83     {"at43usb355", "avr3", "avr3", 0x800100},
84     {"at76c711", "avr3", "avr3", 0x800060},
85     {"atmega103", "avr31", "avr31", 0x800060},
86     {"at43usb320", "avr31", "avr31", 0x800060},
87     {"attiny167", "avr35", "avr35", 0x800100},
88     {"at90usb82", "avr35", "avr35", 0x800100},
89     {"at90usb162", "avr35", "avr35", 0x800100},
90     {"ata5505", "avr35", "avr35", 0x800100},
91     {"atmega8u2", "avr35", "avr35", 0x800100},
92     {"atmega16u2", "avr35", "avr35", 0x800100},
93     {"atmega32u2", "avr35", "avr35", 0x800100},
94     {"attiny1634", "avr35", "avr35", 0x800100},
95     {"atmega8", "avr4", "avr4", 0x800060},
96     {"ata6289", "avr4", "avr4", 0x800100},
97     {"atmega8a", "avr4", "avr4", 0x800060},
98     {"ata6285", "avr4", "avr4", 0x800100},
99     {"ata6286", "avr4", "avr4", 0x800100},
100     {"atmega48", "avr4", "avr4", 0x800100},
101     {"atmega48a", "avr4", "avr4", 0x800100},
102     {"atmega48pa", "avr4", "avr4", 0x800100},
103     {"atmega48pb", "avr4", "avr4", 0x800100},
104     {"atmega48p", "avr4", "avr4", 0x800100},
105     {"atmega88", "avr4", "avr4", 0x800100},
106     {"atmega88a", "avr4", "avr4", 0x800100},
107     {"atmega88p", "avr4", "avr4", 0x800100},
108     {"atmega88pa", "avr4", "avr4", 0x800100},
109     {"atmega88pb", "avr4", "avr4", 0x800100},
110     {"atmega8515", "avr4", "avr4", 0x800060},
111     {"atmega8535", "avr4", "avr4", 0x800060},
112     {"atmega8hva", "avr4", "avr4", 0x800100},
113     {"at90pwm1", "avr4", "avr4", 0x800100},
114     {"at90pwm2", "avr4", "avr4", 0x800100},
115     {"at90pwm2b", "avr4", "avr4", 0x800100},
116     {"at90pwm3", "avr4", "avr4", 0x800100},
117     {"at90pwm3b", "avr4", "avr4", 0x800100},
118     {"at90pwm81", "avr4", "avr4", 0x800100},
119     {"ata5790", "avr5", "avr5", 0x800100},
120     {"ata5795", "avr5", "avr5", 0x800100},
121     {"atmega16", "avr5", "avr5", 0x800060},
122     {"atmega16a", "avr5", "avr5", 0x800060},
123     {"atmega161", "avr5", "avr5", 0x800060},
124     {"atmega162", "avr5", "avr5", 0x800100},
125     {"atmega163", "avr5", "avr5", 0x800060},
126     {"atmega164a", "avr5", "avr5", 0x800100},
127     {"atmega164p", "avr5", "avr5", 0x800100},
128     {"atmega164pa", "avr5", "avr5", 0x800100},
129     {"atmega165", "avr5", "avr5", 0x800100},
130     {"atmega165a", "avr5", "avr5", 0x800100},
131     {"atmega165p", "avr5", "avr5", 0x800100},
132     {"atmega165pa", "avr5", "avr5", 0x800100},
133     {"atmega168", "avr5", "avr5", 0x800100},
134     {"atmega168a", "avr5", "avr5", 0x800100},
135     {"atmega168p", "avr5", "avr5", 0x800100},
136     {"atmega168pa", "avr5", "avr5", 0x800100},
137     {"atmega168pb", "avr5", "avr5", 0x800100},
138     {"atmega169", "avr5", "avr5", 0x800100},
139     {"atmega169a", "avr5", "avr5", 0x800100},
140     {"atmega169p", "avr5", "avr5", 0x800100},
141     {"atmega169pa", "avr5", "avr5", 0x800100},
142     {"atmega32", "avr5", "avr5", 0x800060},
143     {"atmega32a", "avr5", "avr5", 0x800060},
144     {"atmega323", "avr5", "avr5", 0x800060},
145     {"atmega324a", "avr5", "avr5", 0x800100},
146     {"atmega324p", "avr5", "avr5", 0x800100},
147     {"atmega324pa", "avr5", "avr5", 0x800100},
148     {"atmega325", "avr5", "avr5", 0x800100},
149     {"atmega325a", "avr5", "avr5", 0x800100},
150     {"atmega325p", "avr5", "avr5", 0x800100},
151     {"atmega325pa", "avr5", "avr5", 0x800100},
152     {"atmega3250", "avr5", "avr5", 0x800100},
153     {"atmega3250a", "avr5", "avr5", 0x800100},
154     {"atmega3250p", "avr5", "avr5", 0x800100},
155     {"atmega3250pa", "avr5", "avr5", 0x800100},
156     {"atmega328", "avr5", "avr5", 0x800100},
157     {"atmega328p", "avr5", "avr5", 0x800100},
158     {"atmega329", "avr5", "avr5", 0x800100},
159     {"atmega329a", "avr5", "avr5", 0x800100},
160     {"atmega329p", "avr5", "avr5", 0x800100},
161     {"atmega329pa", "avr5", "avr5", 0x800100},
162     {"atmega3290", "avr5", "avr5", 0x800100},
163     {"atmega3290a", "avr5", "avr5", 0x800100},
164     {"atmega3290p", "avr5", "avr5", 0x800100},
165     {"atmega3290pa", "avr5", "avr5", 0x800100},
166     {"atmega406", "avr5", "avr5", 0x800100},
167     {"atmega64", "avr5", "avr5", 0x800100},
168     {"atmega64a", "avr5", "avr5", 0x800100},
169     {"atmega640", "avr5", "avr5", 0x800200},
170     {"atmega644", "avr5", "avr5", 0x800100},
171     {"atmega644a", "avr5", "avr5", 0x800100},
172     {"atmega644p", "avr5", "avr5", 0x800100},
173     {"atmega644pa", "avr5", "avr5", 0x800100},
174     {"atmega645", "avr5", "avr5", 0x800100},
175     {"atmega645a", "avr5", "avr5", 0x800100},
176     {"atmega645p", "avr5", "avr5", 0x800100},
177     {"atmega649", "avr5", "avr5", 0x800100},
178     {"atmega649a", "avr5", "avr5", 0x800100},
179     {"atmega649p", "avr5", "avr5", 0x800100},
180     {"atmega6450", "avr5", "avr5", 0x800100},
181     {"atmega6450a", "avr5", "avr5", 0x800100},
182     {"atmega6450p", "avr5", "avr5", 0x800100},
183     {"atmega6490", "avr5", "avr5", 0x800100},
184     {"atmega6490a", "avr5", "avr5", 0x800100},
185     {"atmega6490p", "avr5", "avr5", 0x800100},
186     {"atmega64rfr2", "avr5", "avr5", 0x800200},
187     {"atmega644rfr2", "avr5", "avr5", 0x800200},
188     {"atmega16hva", "avr5", "avr5", 0x800100},
189     {"atmega16hva2", "avr5", "avr5", 0x800100},
190     {"atmega16hvb", "avr5", "avr5", 0x800100},
191     {"atmega16hvbrevb", "avr5", "avr5", 0x800100},
192     {"atmega32hvb", "avr5", "avr5", 0x800100},
193     {"atmega32hvbrevb", "avr5", "avr5", 0x800100},
194     {"atmega64hve", "avr5", "avr5", 0x800100},
195     {"at90can32", "avr5", "avr5", 0x800100},
196     {"at90can64", "avr5", "avr5", 0x800100},
197     {"at90pwm161", "avr5", "avr5", 0x800100},
198     {"at90pwm216", "avr5", "avr5", 0x800100},
199     {"at90pwm316", "avr5", "avr5", 0x800100},
200     {"atmega32c1", "avr5", "avr5", 0x800100},
201     {"atmega64c1", "avr5", "avr5", 0x800100},
202     {"atmega16m1", "avr5", "avr5", 0x800100},
203     {"atmega32m1", "avr5", "avr5", 0x800100},
204     {"atmega64m1", "avr5", "avr5", 0x800100},
205     {"atmega16u4", "avr5", "avr5", 0x800100},
206     {"atmega32u4", "avr5", "avr5", 0x800100},
207     {"atmega32u6", "avr5", "avr5", 0x800100},
208     {"at90usb646", "avr5", "avr5", 0x800100},
209     {"at90usb647", "avr5", "avr5", 0x800100},
210     {"at90scr100", "avr5", "avr5", 0x800100},
211     {"at94k", "avr5", "avr5", 0x800060},
212     {"m3000", "avr5", "avr5", 0x800060},
213     {"atmega128", "avr51", "avr51", 0x800100},
214     {"atmega128a", "avr51", "avr51", 0x800100},
215     {"atmega1280", "avr51", "avr51", 0x800200},
216     {"atmega1281", "avr51", "avr51", 0x800200},
217     {"atmega1284", "avr51", "avr51", 0x800100},
218     {"atmega1284p", "avr51", "avr51", 0x800100},
219     {"atmega128rfa1", "avr51", "avr51", 0x800200},
220     {"atmega128rfr2", "avr51", "avr51", 0x800200},
221     {"atmega1284rfr2", "avr51", "avr51", 0x800200},
222     {"at90can128", "avr51", "avr51", 0x800200},
223     {"at90usb1286", "avr51", "avr51", 0x800200},
224     {"at90usb1287", "avr51", "avr51", 0x800200},
225     {"atmega2560", "avr6", "avr6", 0x800200},
226     {"atmega2561", "avr6", "avr6", 0x800200},
227     {"atmega256rfr2", "avr6", "avr6", 0x800200},
228     {"atmega2564rfr2", "avr6", "avr6", 0x800200},
229     {"attiny4", "avrtiny", "avrtiny", 0x800040},
230     {"attiny5", "avrtiny", "avrtiny", 0x800040},
231     {"attiny9", "avrtiny", "avrtiny", 0x800040},
232     {"attiny10", "avrtiny", "avrtiny", 0x800040},
233     {"attiny20", "avrtiny", "avrtiny", 0x800040},
234     {"attiny40", "avrtiny", "avrtiny", 0x800040},
235     {"atxmega16a4", "avrxmega2", "avrxmega2", 0x802000},
236     {"atxmega16a4u", "avrxmega2", "avrxmega2", 0x802000},
237     {"atxmega16c4", "avrxmega2", "avrxmega2", 0x802000},
238     {"atxmega16d4", "avrxmega2", "avrxmega2", 0x802000},
239     {"atxmega32a4", "avrxmega2", "avrxmega2", 0x802000},
240     {"atxmega32a4u", "avrxmega2", "avrxmega2", 0x802000},
241     {"atxmega32c4", "avrxmega2", "avrxmega2", 0x802000},
242     {"atxmega32d4", "avrxmega2", "avrxmega2", 0x802000},
243     {"atxmega32e5", "avrxmega2", "avrxmega2", 0x802000},
244     {"atxmega16e5", "avrxmega2", "avrxmega2", 0x802000},
245     {"atxmega8e5", "avrxmega2", "avrxmega2", 0x802000},
246     {"atxmega64a3u", "avrxmega4", "avrxmega4", 0x802000},
247     {"atxmega64a4u", "avrxmega4", "avrxmega4", 0x802000},
248     {"atxmega64b1", "avrxmega4", "avrxmega4", 0x802000},
249     {"atxmega64b3", "avrxmega4", "avrxmega4", 0x802000},
250     {"atxmega64c3", "avrxmega4", "avrxmega4", 0x802000},
251     {"atxmega64d3", "avrxmega4", "avrxmega4", 0x802000},
252     {"atxmega64d4", "avrxmega4", "avrxmega4", 0x802000},
253     {"atxmega64a1", "avrxmega5", "avrxmega5", 0x802000},
254     {"atxmega64a1u", "avrxmega5", "avrxmega5", 0x802000},
255     {"atxmega128a3", "avrxmega6", "avrxmega6", 0x802000},
256     {"atxmega128a3u", "avrxmega6", "avrxmega6", 0x802000},
257     {"atxmega128b1", "avrxmega6", "avrxmega6", 0x802000},
258     {"atxmega128b3", "avrxmega6", "avrxmega6", 0x802000},
259     {"atxmega128c3", "avrxmega6", "avrxmega6", 0x802000},
260     {"atxmega128d3", "avrxmega6", "avrxmega6", 0x802000},
261     {"atxmega128d4", "avrxmega6", "avrxmega6", 0x802000},
262     {"atxmega192a3", "avrxmega6", "avrxmega6", 0x802000},
263     {"atxmega192a3u", "avrxmega6", "avrxmega6", 0x802000},
264     {"atxmega192c3", "avrxmega6", "avrxmega6", 0x802000},
265     {"atxmega192d3", "avrxmega6", "avrxmega6", 0x802000},
266     {"atxmega256a3", "avrxmega6", "avrxmega6", 0x802000},
267     {"atxmega256a3u", "avrxmega6", "avrxmega6", 0x802000},
268     {"atxmega256a3b", "avrxmega6", "avrxmega6", 0x802000},
269     {"atxmega256a3bu", "avrxmega6", "avrxmega6", 0x802000},
270     {"atxmega256c3", "avrxmega6", "avrxmega6", 0x802000},
271     {"atxmega256d3", "avrxmega6", "avrxmega6", 0x802000},
272     {"atxmega384c3", "avrxmega6", "avrxmega6", 0x802000},
273     {"atxmega384d3", "avrxmega6", "avrxmega6", 0x802000},
274     {"atxmega128a1", "avrxmega7", "avrxmega7", 0x802000},
275     {"atxmega128a1u", "avrxmega7", "avrxmega7", 0x802000},
276     {"atxmega128a4u", "avrxmega7", "avrxmega7", 0x802000},
277 };
278 
GetMCUSubPath(StringRef MCUName)279 std::string GetMCUSubPath(StringRef MCUName) {
280   for (const auto &MCU : MCUInfo)
281     if (MCU.Name == MCUName)
282       return std::string(MCU.SubPath);
283   return "";
284 }
285 
GetMCUFamilyName(StringRef MCUName)286 llvm::Optional<StringRef> GetMCUFamilyName(StringRef MCUName) {
287   for (const auto &MCU : MCUInfo)
288     if (MCU.Name == MCUName)
289       return Optional<StringRef>(MCU.Family);
290   return Optional<StringRef>();
291 }
292 
GetMCUSectionAddressData(StringRef MCUName)293 llvm::Optional<unsigned> GetMCUSectionAddressData(StringRef MCUName) {
294   for (const auto &MCU : MCUInfo)
295     if (MCU.Name == MCUName && MCU.DataAddr > 0)
296       return Optional<unsigned>(MCU.DataAddr);
297   return Optional<unsigned>();
298 }
299 
300 const StringRef PossibleAVRLibcLocations[] = {
301     "/usr/avr",
302     "/usr/lib/avr",
303 };
304 
305 } // end anonymous namespace
306 
307 /// AVR Toolchain
AVRToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)308 AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
309                            const ArgList &Args)
310     : Generic_ELF(D, Triple, Args), LinkStdlib(false) {
311   GCCInstallation.init(Triple, Args);
312 
313   // Only add default libraries if the user hasn't explicitly opted out.
314   if (!Args.hasArg(options::OPT_nostdlib) &&
315       !Args.hasArg(options::OPT_nodefaultlibs) &&
316       !Args.hasArg(options::OPT_c /* does not apply when not linking */)) {
317     std::string CPU = getCPUName(Args, Triple);
318 
319     if (CPU.empty()) {
320       // We cannot link any standard libraries without an MCU specified.
321       D.Diag(diag::warn_drv_avr_mcu_not_specified);
322     } else {
323       Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
324       Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
325 
326       if (!FamilyName.hasValue()) {
327         // We do not have an entry for this CPU in the family
328         // mapping table yet.
329         D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
330             << CPU;
331       } else if (!GCCInstallation.isValid()) {
332         // No avr-gcc found and so no runtime linked.
333         D.Diag(diag::warn_drv_avr_gcc_not_found);
334       } else if (!AVRLibcRoot.hasValue()) {
335         // No avr-libc found and so no runtime linked.
336         D.Diag(diag::warn_drv_avr_libc_not_found);
337       } else { // We have enough information to link stdlibs
338         std::string GCCRoot(GCCInstallation.getInstallPath());
339         std::string GCCParentPath(GCCInstallation.getParentLibPath());
340         std::string LibcRoot = AVRLibcRoot.getValue();
341         std::string SubPath = GetMCUSubPath(CPU);
342 
343         getProgramPaths().push_back(GCCParentPath + "/../bin");
344         getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath);
345         getFilePaths().push_back(GCCRoot + std::string("/") + SubPath);
346 
347         LinkStdlib = true;
348       }
349     }
350 
351     if (!LinkStdlib)
352       D.Diag(diag::warn_drv_avr_stdlib_not_linked);
353   }
354 }
355 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const356 void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
357                                              ArgStringList &CC1Args) const {
358   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
359       DriverArgs.hasArg(options::OPT_nostdlibinc))
360     return;
361 
362   // Omit if there is no avr-libc installed.
363   Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
364   if (!AVRLibcRoot.hasValue())
365     return;
366 
367   // Add 'avr-libc/include' to clang system include paths if applicable.
368   std::string AVRInc = AVRLibcRoot.getValue() + "/include";
369   if (llvm::sys::fs::is_directory(AVRInc))
370     addSystemInclude(DriverArgs, CC1Args, AVRInc);
371 }
372 
buildLinker() const373 Tool *AVRToolChain::buildLinker() const {
374   return new tools::AVR::Linker(getTriple(), *this, LinkStdlib);
375 }
376 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const377 void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
378                                const InputInfo &Output,
379                                const InputInfoList &Inputs, const ArgList &Args,
380                                const char *LinkingOutput) const {
381   // Compute information about the target AVR.
382   std::string CPU = getCPUName(Args, getToolChain().getTriple());
383   llvm::Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
384   llvm::Optional<unsigned> SectionAddressData = GetMCUSectionAddressData(CPU);
385 
386   std::string Linker = getToolChain().GetProgramPath(getShortName());
387   ArgStringList CmdArgs;
388   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
389 
390   CmdArgs.push_back("-o");
391   CmdArgs.push_back(Output.getFilename());
392 
393   // Enable garbage collection of unused sections.
394   CmdArgs.push_back("--gc-sections");
395 
396   // Add library search paths before we specify libraries.
397   Args.AddAllArgs(CmdArgs, options::OPT_L);
398   getToolChain().AddFilePathLibArgs(Args, CmdArgs);
399 
400   if (SectionAddressData.hasValue()) {
401     std::string DataSectionArg = std::string("-Tdata=0x") +
402                                  llvm::utohexstr(SectionAddressData.getValue());
403     CmdArgs.push_back(Args.MakeArgString(DataSectionArg));
404   } else {
405     // We do not have an entry for this CPU in the address mapping table yet.
406     getToolChain().getDriver().Diag(
407         diag::warn_drv_avr_linker_section_addresses_not_implemented)
408         << CPU;
409   }
410 
411   // If the family name is known, we can link with the device-specific libgcc.
412   // Without it, libgcc will simply not be linked. This matches avr-gcc
413   // behavior.
414   if (LinkStdlib) {
415     assert(!CPU.empty() && "CPU name must be known in order to link stdlibs");
416 
417     // Add the object file for the CRT.
418     std::string CrtFileName = std::string("-l:crt") + CPU + std::string(".o");
419     CmdArgs.push_back(Args.MakeArgString(CrtFileName));
420 
421     CmdArgs.push_back("-lgcc");
422     CmdArgs.push_back("-lm");
423     CmdArgs.push_back("-lc");
424 
425     // Add the link library specific to the MCU.
426     CmdArgs.push_back(Args.MakeArgString(std::string("-l") + CPU));
427 
428     // Specify the family name as the emulation mode to use.
429     // This is almost always required because otherwise avr-ld
430     // will assume 'avr2' and warn about the program being larger
431     // than the bare minimum supports.
432     CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
433   }
434 
435   C.addCommand(std::make_unique<Command>(
436       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
437       CmdArgs, Inputs, Output));
438 }
439 
findAVRLibcInstallation() const440 llvm::Optional<std::string> AVRToolChain::findAVRLibcInstallation() const {
441   for (StringRef PossiblePath : PossibleAVRLibcLocations) {
442     std::string Path = getDriver().SysRoot + PossiblePath.str();
443     // Return the first avr-libc installation that exists.
444     if (llvm::sys::fs::is_directory(Path))
445       return Optional<std::string>(Path);
446   }
447 
448   return llvm::None;
449 }
450