clang 20.0.0git
AVR.cpp
Go to the documentation of this file.
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"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/MC/MCSubtargetInfo.h"
17#include "llvm/Option/ArgList.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/TargetParser/SubtargetFeature.h"
21
22using namespace clang::driver;
23using namespace clang::driver::toolchains;
24using namespace clang::driver::tools;
25using namespace clang;
26using namespace llvm::opt;
27
28namespace {
29
30// NOTE: This list has been synchronized with gcc-avr 7.3.0 and avr-libc 2.0.0.
31constexpr 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 {"ata6616c", "avr25", "avr25", 0x800100},
66 {"attiny4313", "avr25", "avr25", 0x800060},
67 {"attiny44", "avr25", "avr25", 0x800060},
68 {"attiny44a", "avr25", "avr25", 0x800060},
69 {"attiny84", "avr25", "avr25", 0x800060},
70 {"attiny84a", "avr25", "avr25", 0x800060},
71 {"attiny45", "avr25", "avr25", 0x800060},
72 {"attiny85", "avr25", "avr25", 0x800060},
73 {"attiny441", "avr25", "avr25", 0x800100},
74 {"attiny461", "avr25", "avr25", 0x800060},
75 {"attiny461a", "avr25", "avr25", 0x800060},
76 {"attiny841", "avr25", "avr25", 0x800100},
77 {"attiny861", "avr25", "avr25", 0x800060},
78 {"attiny861a", "avr25", "avr25", 0x800060},
79 {"attiny87", "avr25", "avr25", 0x800100},
80 {"attiny43u", "avr25", "avr25", 0x800060},
81 {"attiny48", "avr25", "avr25", 0x800100},
82 {"attiny88", "avr25", "avr25", 0x800100},
83 {"attiny828", "avr25", "avr25", 0x800100},
84 {"at43usb355", "avr3", "avr3", 0x800100},
85 {"at76c711", "avr3", "avr3", 0x800060},
86 {"atmega103", "avr31", "avr31", 0x800060},
87 {"at43usb320", "avr31", "avr31", 0x800060},
88 {"attiny167", "avr35", "avr35", 0x800100},
89 {"at90usb82", "avr35", "avr35", 0x800100},
90 {"at90usb162", "avr35", "avr35", 0x800100},
91 {"ata5505", "avr35", "avr35", 0x800100},
92 {"ata6617c", "avr35", "avr35", 0x800100},
93 {"ata664251", "avr35", "avr35", 0x800100},
94 {"atmega8u2", "avr35", "avr35", 0x800100},
95 {"atmega16u2", "avr35", "avr35", 0x800100},
96 {"atmega32u2", "avr35", "avr35", 0x800100},
97 {"attiny1634", "avr35", "avr35", 0x800100},
98 {"atmega8", "avr4", "avr4", 0x800060},
99 {"ata6289", "avr4", "avr4", 0x800100},
100 {"atmega8a", "avr4", "avr4", 0x800060},
101 {"ata6285", "avr4", "avr4", 0x800100},
102 {"ata6286", "avr4", "avr4", 0x800100},
103 {"ata6612c", "avr4", "avr4", 0x800100},
104 {"atmega48", "avr4", "avr4", 0x800100},
105 {"atmega48a", "avr4", "avr4", 0x800100},
106 {"atmega48pa", "avr4", "avr4", 0x800100},
107 {"atmega48pb", "avr4", "avr4", 0x800100},
108 {"atmega48p", "avr4", "avr4", 0x800100},
109 {"atmega88", "avr4", "avr4", 0x800100},
110 {"atmega88a", "avr4", "avr4", 0x800100},
111 {"atmega88p", "avr4", "avr4", 0x800100},
112 {"atmega88pa", "avr4", "avr4", 0x800100},
113 {"atmega88pb", "avr4", "avr4", 0x800100},
114 {"atmega8515", "avr4", "avr4", 0x800060},
115 {"atmega8535", "avr4", "avr4", 0x800060},
116 {"atmega8hva", "avr4", "avr4", 0x800100},
117 {"at90pwm1", "avr4", "avr4", 0x800100},
118 {"at90pwm2", "avr4", "avr4", 0x800100},
119 {"at90pwm2b", "avr4", "avr4", 0x800100},
120 {"at90pwm3", "avr4", "avr4", 0x800100},
121 {"at90pwm3b", "avr4", "avr4", 0x800100},
122 {"at90pwm81", "avr4", "avr4", 0x800100},
123 {"ata5702m322", "avr5", "avr5", 0x800200},
124 {"ata5782", "avr5", "avr5", 0x800200},
125 {"ata5790", "avr5", "avr5", 0x800100},
126 {"ata5790n", "avr5", "avr5", 0x800100},
127 {"ata5791", "avr5", "avr5", 0x800100},
128 {"ata5795", "avr5", "avr5", 0x800100},
129 {"ata5831", "avr5", "avr5", 0x800200},
130 {"ata6613c", "avr5", "avr5", 0x800100},
131 {"ata6614q", "avr5", "avr5", 0x800100},
132 {"ata8210", "avr5", "avr5", 0x800200},
133 {"ata8510", "avr5", "avr5", 0x800200},
134 {"atmega16", "avr5", "avr5", 0x800060},
135 {"atmega16a", "avr5", "avr5", 0x800060},
136 {"atmega161", "avr5", "avr5", 0x800060},
137 {"atmega162", "avr5", "avr5", 0x800100},
138 {"atmega163", "avr5", "avr5", 0x800060},
139 {"atmega164a", "avr5", "avr5", 0x800100},
140 {"atmega164p", "avr5", "avr5", 0x800100},
141 {"atmega164pa", "avr5", "avr5", 0x800100},
142 {"atmega165", "avr5", "avr5", 0x800100},
143 {"atmega165a", "avr5", "avr5", 0x800100},
144 {"atmega165p", "avr5", "avr5", 0x800100},
145 {"atmega165pa", "avr5", "avr5", 0x800100},
146 {"atmega168", "avr5", "avr5", 0x800100},
147 {"atmega168a", "avr5", "avr5", 0x800100},
148 {"atmega168p", "avr5", "avr5", 0x800100},
149 {"atmega168pa", "avr5", "avr5", 0x800100},
150 {"atmega168pb", "avr5", "avr5", 0x800100},
151 {"atmega169", "avr5", "avr5", 0x800100},
152 {"atmega169a", "avr5", "avr5", 0x800100},
153 {"atmega169p", "avr5", "avr5", 0x800100},
154 {"atmega169pa", "avr5", "avr5", 0x800100},
155 {"atmega32", "avr5", "avr5", 0x800060},
156 {"atmega32a", "avr5", "avr5", 0x800060},
157 {"atmega323", "avr5", "avr5", 0x800060},
158 {"atmega324a", "avr5", "avr5", 0x800100},
159 {"atmega324p", "avr5", "avr5", 0x800100},
160 {"atmega324pa", "avr5", "avr5", 0x800100},
161 {"atmega324pb", "avr5", "avr5", 0x800100},
162 {"atmega325", "avr5", "avr5", 0x800100},
163 {"atmega325a", "avr5", "avr5", 0x800100},
164 {"atmega325p", "avr5", "avr5", 0x800100},
165 {"atmega325pa", "avr5", "avr5", 0x800100},
166 {"atmega3250", "avr5", "avr5", 0x800100},
167 {"atmega3250a", "avr5", "avr5", 0x800100},
168 {"atmega3250p", "avr5", "avr5", 0x800100},
169 {"atmega3250pa", "avr5", "avr5", 0x800100},
170 {"atmega328", "avr5", "avr5", 0x800100},
171 {"atmega328p", "avr5", "avr5", 0x800100},
172 {"atmega328pb", "avr5", "avr5", 0x800100},
173 {"atmega329", "avr5", "avr5", 0x800100},
174 {"atmega329a", "avr5", "avr5", 0x800100},
175 {"atmega329p", "avr5", "avr5", 0x800100},
176 {"atmega329pa", "avr5", "avr5", 0x800100},
177 {"atmega3290", "avr5", "avr5", 0x800100},
178 {"atmega3290a", "avr5", "avr5", 0x800100},
179 {"atmega3290p", "avr5", "avr5", 0x800100},
180 {"atmega3290pa", "avr5", "avr5", 0x800100},
181 {"atmega406", "avr5", "avr5", 0x800100},
182 {"atmega64", "avr5", "avr5", 0x800100},
183 {"atmega64a", "avr5", "avr5", 0x800100},
184 {"atmega640", "avr5", "avr5", 0x800200},
185 {"atmega644", "avr5", "avr5", 0x800100},
186 {"atmega644a", "avr5", "avr5", 0x800100},
187 {"atmega644p", "avr5", "avr5", 0x800100},
188 {"atmega644pa", "avr5", "avr5", 0x800100},
189 {"atmega645", "avr5", "avr5", 0x800100},
190 {"atmega645a", "avr5", "avr5", 0x800100},
191 {"atmega645p", "avr5", "avr5", 0x800100},
192 {"atmega649", "avr5", "avr5", 0x800100},
193 {"atmega649a", "avr5", "avr5", 0x800100},
194 {"atmega649p", "avr5", "avr5", 0x800100},
195 {"atmega6450", "avr5", "avr5", 0x800100},
196 {"atmega6450a", "avr5", "avr5", 0x800100},
197 {"atmega6450p", "avr5", "avr5", 0x800100},
198 {"atmega6490", "avr5", "avr5", 0x800100},
199 {"atmega6490a", "avr5", "avr5", 0x800100},
200 {"atmega6490p", "avr5", "avr5", 0x800100},
201 {"atmega64rfr2", "avr5", "avr5", 0x800200},
202 {"atmega644rfr2", "avr5", "avr5", 0x800200},
203 {"atmega16hva", "avr5", "avr5", 0x800100},
204 {"atmega16hva2", "avr5", "avr5", 0x800100},
205 {"atmega16hvb", "avr5", "avr5", 0x800100},
206 {"atmega16hvbrevb", "avr5", "avr5", 0x800100},
207 {"atmega32hvb", "avr5", "avr5", 0x800100},
208 {"atmega32hvbrevb", "avr5", "avr5", 0x800100},
209 {"atmega64hve", "avr5", "avr5", 0x800100},
210 {"atmega64hve2", "avr5", "avr5", 0x800100},
211 {"at90can32", "avr5", "avr5", 0x800100},
212 {"at90can64", "avr5", "avr5", 0x800100},
213 {"at90pwm161", "avr5", "avr5", 0x800100},
214 {"at90pwm216", "avr5", "avr5", 0x800100},
215 {"at90pwm316", "avr5", "avr5", 0x800100},
216 {"atmega32c1", "avr5", "avr5", 0x800100},
217 {"atmega64c1", "avr5", "avr5", 0x800100},
218 {"atmega16m1", "avr5", "avr5", 0x800100},
219 {"atmega32m1", "avr5", "avr5", 0x800100},
220 {"atmega64m1", "avr5", "avr5", 0x800100},
221 {"atmega16u4", "avr5", "avr5", 0x800100},
222 {"atmega32u4", "avr5", "avr5", 0x800100},
223 {"atmega32u6", "avr5", "avr5", 0x800100},
224 {"at90usb646", "avr5", "avr5", 0x800100},
225 {"at90usb647", "avr5", "avr5", 0x800100},
226 {"at90scr100", "avr5", "avr5", 0x800100},
227 {"at94k", "avr5", "avr5", 0x800060},
228 {"m3000", "avr5", "avr5", 0x800060},
229 {"atmega128", "avr51", "avr51", 0x800100},
230 {"atmega128a", "avr51", "avr51", 0x800100},
231 {"atmega1280", "avr51", "avr51", 0x800200},
232 {"atmega1281", "avr51", "avr51", 0x800200},
233 {"atmega1284", "avr51", "avr51", 0x800100},
234 {"atmega1284p", "avr51", "avr51", 0x800100},
235 {"atmega128rfa1", "avr51", "avr51", 0x800200},
236 {"atmega128rfr2", "avr51", "avr51", 0x800200},
237 {"atmega1284rfr2", "avr51", "avr51", 0x800200},
238 {"at90can128", "avr51", "avr51", 0x800200},
239 {"at90usb1286", "avr51", "avr51", 0x800200},
240 {"at90usb1287", "avr51", "avr51", 0x800200},
241 {"atmega2560", "avr6", "avr6", 0x800200},
242 {"atmega2561", "avr6", "avr6", 0x800200},
243 {"atmega256rfr2", "avr6", "avr6", 0x800200},
244 {"atmega2564rfr2", "avr6", "avr6", 0x800200},
245 {"attiny4", "avrtiny", "avrtiny", 0x800040},
246 {"attiny5", "avrtiny", "avrtiny", 0x800040},
247 {"attiny9", "avrtiny", "avrtiny", 0x800040},
248 {"attiny10", "avrtiny", "avrtiny", 0x800040},
249 {"attiny20", "avrtiny", "avrtiny", 0x800040},
250 {"attiny40", "avrtiny", "avrtiny", 0x800040},
251 {"attiny102", "avrtiny", "avrtiny", 0x800040},
252 {"attiny104", "avrtiny", "avrtiny", 0x800040},
253 {"atxmega16a4", "avrxmega2", "avrxmega2", 0x802000},
254 {"atxmega16a4u", "avrxmega2", "avrxmega2", 0x802000},
255 {"atxmega16c4", "avrxmega2", "avrxmega2", 0x802000},
256 {"atxmega16d4", "avrxmega2", "avrxmega2", 0x802000},
257 {"atxmega32a4", "avrxmega2", "avrxmega2", 0x802000},
258 {"atxmega32a4u", "avrxmega2", "avrxmega2", 0x802000},
259 {"atxmega32c3", "avrxmega2", "avrxmega2", 0x802000},
260 {"atxmega32c4", "avrxmega2", "avrxmega2", 0x802000},
261 {"atxmega32d3", "avrxmega2", "avrxmega2", 0x802000},
262 {"atxmega32d4", "avrxmega2", "avrxmega2", 0x802000},
263 {"atxmega32e5", "avrxmega2", "avrxmega2", 0x802000},
264 {"atxmega16e5", "avrxmega2", "avrxmega2", 0x802000},
265 {"atxmega8e5", "avrxmega2", "avrxmega2", 0x802000},
266 {"atxmega64a3", "avrxmega4", "avrxmega4", 0x802000},
267 {"atxmega64a3u", "avrxmega4", "avrxmega4", 0x802000},
268 {"atxmega64a4u", "avrxmega4", "avrxmega4", 0x802000},
269 {"atxmega64b1", "avrxmega4", "avrxmega4", 0x802000},
270 {"atxmega64b3", "avrxmega4", "avrxmega4", 0x802000},
271 {"atxmega64c3", "avrxmega4", "avrxmega4", 0x802000},
272 {"atxmega64d3", "avrxmega4", "avrxmega4", 0x802000},
273 {"atxmega64d4", "avrxmega4", "avrxmega4", 0x802000},
274 {"atxmega64a1", "avrxmega5", "avrxmega5", 0x802000},
275 {"atxmega64a1u", "avrxmega5", "avrxmega5", 0x802000},
276 {"atxmega128a3", "avrxmega6", "avrxmega6", 0x802000},
277 {"atxmega128a3u", "avrxmega6", "avrxmega6", 0x802000},
278 {"atxmega128b1", "avrxmega6", "avrxmega6", 0x802000},
279 {"atxmega128b3", "avrxmega6", "avrxmega6", 0x802000},
280 {"atxmega128c3", "avrxmega6", "avrxmega6", 0x802000},
281 {"atxmega128d3", "avrxmega6", "avrxmega6", 0x802000},
282 {"atxmega128d4", "avrxmega6", "avrxmega6", 0x802000},
283 {"atxmega192a3", "avrxmega6", "avrxmega6", 0x802000},
284 {"atxmega192a3u", "avrxmega6", "avrxmega6", 0x802000},
285 {"atxmega192c3", "avrxmega6", "avrxmega6", 0x802000},
286 {"atxmega192d3", "avrxmega6", "avrxmega6", 0x802000},
287 {"atxmega256a3", "avrxmega6", "avrxmega6", 0x802000},
288 {"atxmega256a3u", "avrxmega6", "avrxmega6", 0x802000},
289 {"atxmega256a3b", "avrxmega6", "avrxmega6", 0x802000},
290 {"atxmega256a3bu", "avrxmega6", "avrxmega6", 0x802000},
291 {"atxmega256c3", "avrxmega6", "avrxmega6", 0x802000},
292 {"atxmega256d3", "avrxmega6", "avrxmega6", 0x802000},
293 {"atxmega384c3", "avrxmega6", "avrxmega6", 0x802000},
294 {"atxmega384d3", "avrxmega6", "avrxmega6", 0x802000},
295 {"atxmega128a1", "avrxmega7", "avrxmega7", 0x802000},
296 {"atxmega128a1u", "avrxmega7", "avrxmega7", 0x802000},
297 {"atxmega128a4u", "avrxmega7", "avrxmega7", 0x802000},
298 {"attiny202", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
299 {"attiny204", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
300 {"attiny212", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
301 {"attiny214", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
302 {"attiny402", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
303 {"attiny404", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
304 {"attiny406", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
305 {"attiny412", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
306 {"attiny414", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
307 {"attiny416", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
308 {"attiny417", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
309 {"attiny804", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
310 {"attiny806", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
311 {"attiny807", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
312 {"attiny814", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
313 {"attiny816", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
314 {"attiny817", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
315 {"atmega808", "avrxmega3/short-calls", "avrxmega3", 0x803C00},
316 {"atmega809", "avrxmega3/short-calls", "avrxmega3", 0x803C00},
317 {"atmega1608", "avrxmega3", "avrxmega3", 0x803800},
318 {"atmega1609", "avrxmega3", "avrxmega3", 0x803800},
319 {"atmega3208", "avrxmega3", "avrxmega3", 0x803000},
320 {"atmega3209", "avrxmega3", "avrxmega3", 0x803000},
321 {"atmega4808", "avrxmega3", "avrxmega3", 0x802800},
322 {"atmega4809", "avrxmega3", "avrxmega3", 0x802800},
323 {"attiny1604", "avrxmega3", "avrxmega3", 0x803C00},
324 {"attiny1606", "avrxmega3", "avrxmega3", 0x803C00},
325 {"attiny1607", "avrxmega3", "avrxmega3", 0x803C00},
326 {"attiny1614", "avrxmega3", "avrxmega3", 0x803800},
327 {"attiny1616", "avrxmega3", "avrxmega3", 0x803800},
328 {"attiny1617", "avrxmega3", "avrxmega3", 0x803800},
329 {"attiny1624", "avrxmega3", "avrxmega3", 0x803800},
330 {"attiny1626", "avrxmega3", "avrxmega3", 0x803800},
331 {"attiny1627", "avrxmega3", "avrxmega3", 0x803800},
332 {"attiny3216", "avrxmega3", "avrxmega3", 0x803800},
333 {"attiny3217", "avrxmega3", "avrxmega3", 0x803800},
334};
335
336std::string GetMCUSubPath(StringRef MCUName) {
337 for (const auto &MCU : MCUInfo)
338 if (MCU.Name == MCUName)
339 return std::string(MCU.SubPath);
340 return "";
341}
342
343std::optional<StringRef> GetMCUFamilyName(StringRef MCUName) {
344 for (const auto &MCU : MCUInfo)
345 if (MCU.Name == MCUName)
346 return std::optional<StringRef>(MCU.Family);
347 return std::nullopt;
348}
349
350std::optional<unsigned> GetMCUSectionAddressData(StringRef MCUName) {
351 for (const auto &MCU : MCUInfo)
352 if (MCU.Name == MCUName && MCU.DataAddr > 0)
353 return std::optional<unsigned>(MCU.DataAddr);
354 return std::nullopt;
355}
356
357const StringRef PossibleAVRLibcLocations[] = {
358 "/avr",
359 "/usr/avr",
360 "/usr/lib/avr",
361};
362
363} // end anonymous namespace
364
365/// AVR Toolchain
366AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
367 const ArgList &Args)
368 : Generic_ELF(D, Triple, Args) {
369 GCCInstallation.init(Triple, Args);
370
371 if (getCPUName(D, Args, Triple).empty())
372 D.Diag(diag::warn_drv_avr_mcu_not_specified);
373
374 // Only add default libraries if the user hasn't explicitly opted out.
375 if (!Args.hasArg(options::OPT_nostdlib) &&
376 !Args.hasArg(options::OPT_nodefaultlibs) && GCCInstallation.isValid()) {
377 GCCInstallPath = GCCInstallation.getInstallPath();
378 std::string GCCParentPath(GCCInstallation.getParentLibPath());
379 getProgramPaths().push_back(GCCParentPath + "/../bin");
380 }
381}
382
383void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
384 ArgStringList &CC1Args) const {
385 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
386 DriverArgs.hasArg(options::OPT_nostdlibinc))
387 return;
388
389 // Omit if there is no avr-libc installed.
390 std::optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
391 if (!AVRLibcRoot)
392 return;
393
394 // Add 'avr-libc/include' to clang system include paths if applicable.
395 std::string AVRInc = *AVRLibcRoot + "/include";
396 if (llvm::sys::fs::is_directory(AVRInc))
397 addSystemInclude(DriverArgs, CC1Args, AVRInc);
398}
399
401 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
402 Action::OffloadKind DeviceOffloadKind) const {
403 // Reject C/C++ compilation for avr1 devices since they have no SRAM.
404 const Driver &D = getDriver();
405 std::string CPU = getCPUName(D, DriverArgs, getTriple());
406 std::optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
407 if (CPU == "avr1" || (FamilyName && *FamilyName == "avr1"))
408 D.Diag(diag::err_drv_opt_unsupported_input_type)
409 << "-mmcu=" + CPU << "c/c++";
410
411 // By default, use `.ctors` (not `.init_array`), as required by libgcc, which
412 // runs constructors/destructors on AVR.
413 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
414 options::OPT_fno_use_init_array, false))
415 CC1Args.push_back("-fno-use-init-array");
416 // Use `-fno-use-cxa-atexit` as default, since avr-libc does not support
417 // `__cxa_atexit()`.
418 if (!DriverArgs.hasFlag(options::OPT_fuse_cxa_atexit,
419 options::OPT_fno_use_cxa_atexit, false))
420 CC1Args.push_back("-fno-use-cxa-atexit");
421}
422
424 return new tools::AVR::Linker(getTriple(), *this);
425}
426
427std::string
428AVRToolChain::getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component,
430 assert(Type == ToolChain::FT_Static && "AVR only supports static libraries");
431 // Since AVR can never be a host environment, its compiler-rt library files
432 // should always have ".a" suffix, even on windows.
433 SmallString<32> File("/libclang_rt.");
434 File += Component.str();
435 File += ".a";
436 // Return the default compiler-rt path appended with
437 // "avr/libclang_rt.$COMPONENT.a".
439 llvm::sys::path::append(Path, "avr");
440 llvm::sys::path::append(Path, File.str());
441 return std::string(Path);
442}
443
445 const InputInfo &Output,
446 const InputInfoList &Inputs, const ArgList &Args,
447 const char *LinkingOutput) const {
448 const auto &TC = static_cast<const AVRToolChain &>(getToolChain());
449 const Driver &D = getToolChain().getDriver();
450
451 // Compute information about the target AVR.
452 std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
453 std::optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
454 std::optional<std::string> AVRLibcRoot = TC.findAVRLibcInstallation();
455 std::optional<unsigned> SectionAddressData = GetMCUSectionAddressData(CPU);
456
457 // Compute the linker program path, and use GNU "avr-ld" as default.
458 const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ);
459 std::string Linker = A ? getToolChain().GetLinkerPath(nullptr)
460 : getToolChain().GetProgramPath(getShortName());
461
462 ArgStringList CmdArgs;
463
464 CmdArgs.push_back("-o");
465 CmdArgs.push_back(Output.getFilename());
466
467 // Enable garbage collection of unused sections.
468 if (!Args.hasArg(options::OPT_r))
469 CmdArgs.push_back("--gc-sections");
470
471 // Add library search paths before we specify libraries.
472 Args.AddAllArgs(CmdArgs, options::OPT_L);
473 getToolChain().AddFilePathLibArgs(Args, CmdArgs);
474
475 // Currently we only support libgcc and compiler-rt.
476 auto RtLib = TC.GetRuntimeLibType(Args);
477 assert(
478 (RtLib == ToolChain::RLT_Libgcc || RtLib == ToolChain::RLT_CompilerRT) &&
479 "unknown runtime library");
480
481 // Only add default libraries if the user hasn't explicitly opted out.
482 bool LinkStdlib = false;
483 if (!Args.hasArg(options::OPT_nostdlib) && !Args.hasArg(options::OPT_r) &&
484 !Args.hasArg(options::OPT_nodefaultlibs)) {
485 if (!CPU.empty()) {
486 if (!FamilyName) {
487 // We do not have an entry for this CPU in the family
488 // mapping table yet.
489 D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
490 << CPU;
491 } else if (!AVRLibcRoot) {
492 // No avr-libc found and so no runtime linked.
493 D.Diag(diag::warn_drv_avr_libc_not_found);
494 } else {
495 std::string SubPath = GetMCUSubPath(CPU);
496 // Add path of avr-libc.
497 CmdArgs.push_back(
498 Args.MakeArgString(Twine("-L") + *AVRLibcRoot + "/lib/" + SubPath));
499 if (RtLib == ToolChain::RLT_Libgcc)
500 CmdArgs.push_back(Args.MakeArgString("-L" + TC.getGCCInstallPath() +
501 "/" + SubPath));
502 LinkStdlib = true;
503 }
504 }
505 if (!LinkStdlib)
506 D.Diag(diag::warn_drv_avr_stdlib_not_linked);
507 }
508
509 if (!Args.hasArg(options::OPT_r)) {
510 if (SectionAddressData) {
511 CmdArgs.push_back(
512 Args.MakeArgString("--defsym=__DATA_REGION_ORIGIN__=0x" +
513 Twine::utohexstr(*SectionAddressData)));
514 } else {
515 // We do not have an entry for this CPU in the address mapping table
516 // yet.
517 D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented)
518 << CPU;
519 }
520 }
521
522 if (D.isUsingLTO()) {
523 assert(!Inputs.empty() && "Must have at least one input.");
524 // Find the first filename InputInfo object.
525 auto Input = llvm::find_if(
526 Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
527 if (Input == Inputs.end())
528 // For a very rare case, all of the inputs to the linker are
529 // InputArg. If that happens, just use the first InputInfo.
530 Input = Inputs.begin();
531
532 addLTOOptions(TC, Args, CmdArgs, Output, *Input,
533 D.getLTOMode() == LTOK_Thin);
534 }
535
536 // If the family name is known, we can link with the device-specific libgcc.
537 // Without it, libgcc will simply not be linked. This matches avr-gcc
538 // behavior.
539 if (LinkStdlib) {
540 assert(!CPU.empty() && "CPU name must be known in order to link stdlibs");
541
542 CmdArgs.push_back("--start-group");
543
544 // Add the object file for the CRT.
545 std::string CrtFileName = std::string("-l:crt") + CPU + std::string(".o");
546 CmdArgs.push_back(Args.MakeArgString(CrtFileName));
547
548 // Link to libgcc.
549 if (RtLib == ToolChain::RLT_Libgcc)
550 CmdArgs.push_back("-lgcc");
551
552 // Link to generic libraries of avr-libc.
553 CmdArgs.push_back("-lm");
554 CmdArgs.push_back("-lc");
555
556 // Add the link library specific to the MCU.
557 CmdArgs.push_back(Args.MakeArgString(std::string("-l") + CPU));
558
559 // Add the relocatable inputs.
560 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
561
562 // We directly use libclang_rt.builtins.a as input file, instead of using
563 // '-lclang_rt.builtins'.
564 if (RtLib == ToolChain::RLT_CompilerRT) {
565 std::string RtLib =
566 getToolChain().getCompilerRT(Args, "builtins", ToolChain::FT_Static);
567 if (llvm::sys::fs::exists(RtLib))
568 CmdArgs.push_back(Args.MakeArgString(RtLib));
569 }
570
571 CmdArgs.push_back("--end-group");
572
573 // Add avr-libc's linker script to lld by default, if it exists.
574 if (!Args.hasArg(options::OPT_T) &&
575 Linker.find("avr-ld") == std::string::npos) {
576 std::string Path(*AVRLibcRoot + "/lib/ldscripts/");
577 Path += *FamilyName;
578 Path += ".x";
579 if (llvm::sys::fs::exists(Path))
580 CmdArgs.push_back(Args.MakeArgString("-T" + Path));
581 }
582 // Otherwise add user specified linker script to either avr-ld or lld.
583 else
584 Args.AddAllArgs(CmdArgs, options::OPT_T);
585
586 if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
587 CmdArgs.push_back("--relax");
588 } else {
589 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
590 }
591
592 // Specify the family name as the emulation mode to use.
593 // This is almost always required because otherwise avr-ld
594 // will assume 'avr2' and warn about the program being larger
595 // than the bare minimum supports.
596 if (Linker.find("avr-ld") != std::string::npos && FamilyName)
597 CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
598
599 C.addCommand(std::make_unique<Command>(
600 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
601 CmdArgs, Inputs, Output));
602}
603
604std::optional<std::string> AVRToolChain::findAVRLibcInstallation() const {
605 // Search avr-libc installation according to avr-gcc installation.
606 std::string GCCParent(GCCInstallation.getParentLibPath());
607 std::string Path(GCCParent + "/avr");
608 if (llvm::sys::fs::is_directory(Path))
609 return Path;
610 Path = GCCParent + "/../avr";
611 if (llvm::sys::fs::is_directory(Path))
612 return Path;
613
614 // Search avr-libc installation from possible locations, and return the first
615 // one that exists, if there is no avr-gcc installed.
616 for (StringRef PossiblePath : PossibleAVRLibcLocations) {
617 std::string Path = getDriver().SysRoot + PossiblePath.str();
618 if (llvm::sys::fs::is_directory(Path))
619 return Path;
620 }
621
622 return std::nullopt;
623}
const Decl * D
IndirectLocalPath & Path
The base class of the type hierarchy.
Definition: Type.h:1828
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
std::string SysRoot
sysroot, if present
Definition: Driver.h:180
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
bool isFilename() const
Definition: InputInfo.h:75
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1265
const Driver & getDriver() const
Definition: ToolChain.h:252
path_list & getProgramPaths()
Definition: ToolChain.h:297
const llvm::Triple & getTriple() const
Definition: ToolChain.h:254
virtual std::string getCompilerRTPath() const
Definition: ToolChain.cpp:698
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: AVR.cpp:400
std::optional< std::string > findAVRLibcInstallation() const
Definition: AVR.cpp:604
std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type) const override
Definition: AVR.cpp:428
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: AVR.cpp:383
Tool * buildLinker() const override
Definition: AVR.cpp:423
AVRToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
AVR Toolchain.
Definition: AVR.cpp:366
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:2224
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: AVR.cpp:444
std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92