xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/AVR.cpp (revision 3a9a9c0ca44ec535dcf73fe8462bee458e54814b)
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 constexpr struct {
32   StringRef Name;
33   StringRef 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 
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 
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 
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     "/avr",
302     "/usr/avr",
303     "/usr/lib/avr",
304 };
305 
306 } // end anonymous namespace
307 
308 /// AVR Toolchain
309 AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
310                            const ArgList &Args)
311     : Generic_ELF(D, Triple, Args), LinkStdlib(false) {
312   GCCInstallation.init(Triple, Args);
313 
314   // Only add default libraries if the user hasn't explicitly opted out.
315   if (!Args.hasArg(options::OPT_nostdlib) &&
316       !Args.hasArg(options::OPT_nodefaultlibs) &&
317       !Args.hasArg(options::OPT_c /* does not apply when not linking */)) {
318     std::string CPU = getCPUName(D, Args, Triple);
319 
320     if (CPU.empty()) {
321       // We cannot link any standard libraries without an MCU specified.
322       D.Diag(diag::warn_drv_avr_mcu_not_specified);
323     } else {
324       Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
325       Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
326 
327       if (!FamilyName.hasValue()) {
328         // We do not have an entry for this CPU in the family
329         // mapping table yet.
330         D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
331             << CPU;
332       } else if (!GCCInstallation.isValid()) {
333         // No avr-gcc found and so no runtime linked.
334         D.Diag(diag::warn_drv_avr_gcc_not_found);
335       } else if (!AVRLibcRoot.hasValue()) {
336         // No avr-libc found and so no runtime linked.
337         D.Diag(diag::warn_drv_avr_libc_not_found);
338       } else { // We have enough information to link stdlibs
339         std::string GCCRoot(GCCInstallation.getInstallPath());
340         std::string GCCParentPath(GCCInstallation.getParentLibPath());
341         std::string LibcRoot = AVRLibcRoot.getValue();
342         std::string SubPath = GetMCUSubPath(CPU);
343 
344         getProgramPaths().push_back(GCCParentPath + "/../bin");
345         getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath);
346         getFilePaths().push_back(GCCRoot + std::string("/") + SubPath);
347 
348         LinkStdlib = true;
349       }
350     }
351 
352     if (!LinkStdlib)
353       D.Diag(diag::warn_drv_avr_stdlib_not_linked);
354   }
355 }
356 
357 void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
358                                              ArgStringList &CC1Args) const {
359   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
360       DriverArgs.hasArg(options::OPT_nostdlibinc))
361     return;
362 
363   // Omit if there is no avr-libc installed.
364   Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
365   if (!AVRLibcRoot.hasValue())
366     return;
367 
368   // Add 'avr-libc/include' to clang system include paths if applicable.
369   std::string AVRInc = AVRLibcRoot.getValue() + "/include";
370   if (llvm::sys::fs::is_directory(AVRInc))
371     addSystemInclude(DriverArgs, CC1Args, AVRInc);
372 }
373 
374 void AVRToolChain::addClangTargetOptions(
375     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
376     Action::OffloadKind DeviceOffloadKind) const {
377   // By default, use `.ctors` (not `.init_array`), as required by libgcc, which
378   // runs constructors/destructors on AVR.
379   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
380                           options::OPT_fno_use_init_array, false))
381     CC1Args.push_back("-fno-use-init-array");
382   // Use `-fno-use-cxa-atexit` as default, since avr-libc does not support
383   // `__cxa_atexit()`.
384   if (!DriverArgs.hasFlag(options::OPT_fuse_cxa_atexit,
385                           options::OPT_fno_use_cxa_atexit, false))
386     CC1Args.push_back("-fno-use-cxa-atexit");
387 }
388 
389 Tool *AVRToolChain::buildLinker() const {
390   return new tools::AVR::Linker(getTriple(), *this, LinkStdlib);
391 }
392 
393 void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
394                                const InputInfo &Output,
395                                const InputInfoList &Inputs, const ArgList &Args,
396                                const char *LinkingOutput) const {
397   const Driver &D = getToolChain().getDriver();
398 
399   // Compute information about the target AVR.
400   std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
401   llvm::Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
402   llvm::Optional<unsigned> SectionAddressData = GetMCUSectionAddressData(CPU);
403 
404   std::string Linker = getToolChain().GetProgramPath(getShortName());
405   ArgStringList CmdArgs;
406   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
407 
408   CmdArgs.push_back("-o");
409   CmdArgs.push_back(Output.getFilename());
410 
411   // Enable garbage collection of unused sections.
412   CmdArgs.push_back("--gc-sections");
413 
414   // Add library search paths before we specify libraries.
415   Args.AddAllArgs(CmdArgs, options::OPT_L);
416   getToolChain().AddFilePathLibArgs(Args, CmdArgs);
417 
418   if (SectionAddressData.hasValue()) {
419     std::string DataSectionArg = std::string("-Tdata=0x") +
420                                  llvm::utohexstr(SectionAddressData.getValue());
421     CmdArgs.push_back(Args.MakeArgString(DataSectionArg));
422   } else {
423     // We do not have an entry for this CPU in the address mapping table yet.
424     D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented) << CPU;
425   }
426 
427   // If the family name is known, we can link with the device-specific libgcc.
428   // Without it, libgcc will simply not be linked. This matches avr-gcc
429   // behavior.
430   if (LinkStdlib) {
431     assert(!CPU.empty() && "CPU name must be known in order to link stdlibs");
432 
433     CmdArgs.push_back("--start-group");
434 
435     // Add the object file for the CRT.
436     std::string CrtFileName = std::string("-l:crt") + CPU + std::string(".o");
437     CmdArgs.push_back(Args.MakeArgString(CrtFileName));
438 
439     CmdArgs.push_back("-lgcc");
440     CmdArgs.push_back("-lm");
441     CmdArgs.push_back("-lc");
442 
443     // Add the link library specific to the MCU.
444     CmdArgs.push_back(Args.MakeArgString(std::string("-l") + CPU));
445 
446     CmdArgs.push_back("--end-group");
447 
448     // Specify the family name as the emulation mode to use.
449     // This is almost always required because otherwise avr-ld
450     // will assume 'avr2' and warn about the program being larger
451     // than the bare minimum supports.
452     CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
453   }
454 
455   C.addCommand(std::make_unique<Command>(
456       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
457       CmdArgs, Inputs, Output));
458 }
459 
460 llvm::Optional<std::string> AVRToolChain::findAVRLibcInstallation() const {
461   // Search avr-libc installation according to avr-gcc installation.
462   std::string GCCParent(GCCInstallation.getParentLibPath());
463   std::string Path(GCCParent + "/avr");
464   if (llvm::sys::fs::is_directory(Path))
465     return Path;
466   Path = GCCParent + "/../avr";
467   if (llvm::sys::fs::is_directory(Path))
468     return Path;
469 
470   // Search avr-libc installation from possible locations, and return the first
471   // one that exists, if there is no avr-gcc installed.
472   for (StringRef PossiblePath : PossibleAVRLibcLocations) {
473     std::string Path = getDriver().SysRoot + PossiblePath.str();
474     if (llvm::sys::fs::is_directory(Path))
475       return Path;
476   }
477 
478   return llvm::None;
479 }
480