clang  15.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"
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 // NOTE: This list has been synchronized with gcc-avr 7.3.0 and avr-libc 2.0.0.
32 constexpr struct {
33  StringRef Name;
34  StringRef SubPath;
35  StringRef Family;
36  unsigned DataAddr;
37 } MCUInfo[] = {
38  {"at90s1200", "", "avr1", 0},
39  {"attiny11", "", "avr1", 0},
40  {"attiny12", "", "avr1", 0},
41  {"attiny15", "", "avr1", 0},
42  {"attiny28", "", "avr1", 0},
43  {"at90s2313", "tiny-stack", "avr2", 0x800060},
44  {"at90s2323", "tiny-stack", "avr2", 0x800060},
45  {"at90s2333", "tiny-stack", "avr2", 0x800060},
46  {"at90s2343", "tiny-stack", "avr2", 0x800060},
47  {"at90s4433", "tiny-stack", "avr2", 0x800060},
48  {"attiny22", "tiny-stack", "avr2", 0x800060},
49  {"attiny26", "tiny-stack", "avr2", 0x800060},
50  {"at90s4414", "", "avr2", 0x800060},
51  {"at90s4434", "", "avr2", 0x800060},
52  {"at90s8515", "", "avr2", 0x800060},
53  {"at90c8534", "", "avr2", 0x800060},
54  {"at90s8535", "", "avr2", 0x800060},
55  {"attiny13", "avr25/tiny-stack", "avr25", 0x800060},
56  {"attiny13a", "avr25/tiny-stack", "avr25", 0x800060},
57  {"attiny2313", "avr25/tiny-stack", "avr25", 0x800060},
58  {"attiny2313a", "avr25/tiny-stack", "avr25", 0x800060},
59  {"attiny24", "avr25/tiny-stack", "avr25", 0x800060},
60  {"attiny24a", "avr25/tiny-stack", "avr25", 0x800060},
61  {"attiny25", "avr25/tiny-stack", "avr25", 0x800060},
62  {"attiny261", "avr25/tiny-stack", "avr25", 0x800060},
63  {"attiny261a", "avr25/tiny-stack", "avr25", 0x800060},
64  {"at86rf401", "avr25", "avr25", 0x800060},
65  {"ata5272", "avr25", "avr25", 0x800100},
66  {"ata6616c", "avr25", "avr25", 0x800100},
67  {"attiny4313", "avr25", "avr25", 0x800060},
68  {"attiny44", "avr25", "avr25", 0x800060},
69  {"attiny44a", "avr25", "avr25", 0x800060},
70  {"attiny84", "avr25", "avr25", 0x800060},
71  {"attiny84a", "avr25", "avr25", 0x800060},
72  {"attiny45", "avr25", "avr25", 0x800060},
73  {"attiny85", "avr25", "avr25", 0x800060},
74  {"attiny441", "avr25", "avr25", 0x800100},
75  {"attiny461", "avr25", "avr25", 0x800060},
76  {"attiny461a", "avr25", "avr25", 0x800060},
77  {"attiny841", "avr25", "avr25", 0x800100},
78  {"attiny861", "avr25", "avr25", 0x800060},
79  {"attiny861a", "avr25", "avr25", 0x800060},
80  {"attiny87", "avr25", "avr25", 0x800100},
81  {"attiny43u", "avr25", "avr25", 0x800060},
82  {"attiny48", "avr25", "avr25", 0x800100},
83  {"attiny88", "avr25", "avr25", 0x800100},
84  {"attiny828", "avr25", "avr25", 0x800100},
85  {"at43usb355", "avr3", "avr3", 0x800100},
86  {"at76c711", "avr3", "avr3", 0x800060},
87  {"atmega103", "avr31", "avr31", 0x800060},
88  {"at43usb320", "avr31", "avr31", 0x800060},
89  {"attiny167", "avr35", "avr35", 0x800100},
90  {"at90usb82", "avr35", "avr35", 0x800100},
91  {"at90usb162", "avr35", "avr35", 0x800100},
92  {"ata5505", "avr35", "avr35", 0x800100},
93  {"ata6617c", "avr35", "avr35", 0x800100},
94  {"ata664251", "avr35", "avr35", 0x800100},
95  {"atmega8u2", "avr35", "avr35", 0x800100},
96  {"atmega16u2", "avr35", "avr35", 0x800100},
97  {"atmega32u2", "avr35", "avr35", 0x800100},
98  {"attiny1634", "avr35", "avr35", 0x800100},
99  {"atmega8", "avr4", "avr4", 0x800060},
100  {"ata6289", "avr4", "avr4", 0x800100},
101  {"atmega8a", "avr4", "avr4", 0x800060},
102  {"ata6285", "avr4", "avr4", 0x800100},
103  {"ata6286", "avr4", "avr4", 0x800100},
104  {"ata6612c", "avr4", "avr4", 0x800100},
105  {"atmega48", "avr4", "avr4", 0x800100},
106  {"atmega48a", "avr4", "avr4", 0x800100},
107  {"atmega48pa", "avr4", "avr4", 0x800100},
108  {"atmega48pb", "avr4", "avr4", 0x800100},
109  {"atmega48p", "avr4", "avr4", 0x800100},
110  {"atmega88", "avr4", "avr4", 0x800100},
111  {"atmega88a", "avr4", "avr4", 0x800100},
112  {"atmega88p", "avr4", "avr4", 0x800100},
113  {"atmega88pa", "avr4", "avr4", 0x800100},
114  {"atmega88pb", "avr4", "avr4", 0x800100},
115  {"atmega8515", "avr4", "avr4", 0x800060},
116  {"atmega8535", "avr4", "avr4", 0x800060},
117  {"atmega8hva", "avr4", "avr4", 0x800100},
118  {"at90pwm1", "avr4", "avr4", 0x800100},
119  {"at90pwm2", "avr4", "avr4", 0x800100},
120  {"at90pwm2b", "avr4", "avr4", 0x800100},
121  {"at90pwm3", "avr4", "avr4", 0x800100},
122  {"at90pwm3b", "avr4", "avr4", 0x800100},
123  {"at90pwm81", "avr4", "avr4", 0x800100},
124  {"ata5702m322", "avr5", "avr5", 0x800200},
125  {"ata5782", "avr5", "avr5", 0x800200},
126  {"ata5790", "avr5", "avr5", 0x800100},
127  {"ata5790n", "avr5", "avr5", 0x800100},
128  {"ata5791", "avr5", "avr5", 0x800100},
129  {"ata5795", "avr5", "avr5", 0x800100},
130  {"ata5831", "avr5", "avr5", 0x800200},
131  {"ata6613c", "avr5", "avr5", 0x800100},
132  {"ata6614q", "avr5", "avr5", 0x800100},
133  {"ata8210", "avr5", "avr5", 0x800200},
134  {"ata8510", "avr5", "avr5", 0x800200},
135  {"atmega16", "avr5", "avr5", 0x800060},
136  {"atmega16a", "avr5", "avr5", 0x800060},
137  {"atmega161", "avr5", "avr5", 0x800060},
138  {"atmega162", "avr5", "avr5", 0x800100},
139  {"atmega163", "avr5", "avr5", 0x800060},
140  {"atmega164a", "avr5", "avr5", 0x800100},
141  {"atmega164p", "avr5", "avr5", 0x800100},
142  {"atmega164pa", "avr5", "avr5", 0x800100},
143  {"atmega165", "avr5", "avr5", 0x800100},
144  {"atmega165a", "avr5", "avr5", 0x800100},
145  {"atmega165p", "avr5", "avr5", 0x800100},
146  {"atmega165pa", "avr5", "avr5", 0x800100},
147  {"atmega168", "avr5", "avr5", 0x800100},
148  {"atmega168a", "avr5", "avr5", 0x800100},
149  {"atmega168p", "avr5", "avr5", 0x800100},
150  {"atmega168pa", "avr5", "avr5", 0x800100},
151  {"atmega168pb", "avr5", "avr5", 0x800100},
152  {"atmega169", "avr5", "avr5", 0x800100},
153  {"atmega169a", "avr5", "avr5", 0x800100},
154  {"atmega169p", "avr5", "avr5", 0x800100},
155  {"atmega169pa", "avr5", "avr5", 0x800100},
156  {"atmega32", "avr5", "avr5", 0x800060},
157  {"atmega32a", "avr5", "avr5", 0x800060},
158  {"atmega323", "avr5", "avr5", 0x800060},
159  {"atmega324a", "avr5", "avr5", 0x800100},
160  {"atmega324p", "avr5", "avr5", 0x800100},
161  {"atmega324pa", "avr5", "avr5", 0x800100},
162  {"atmega324pb", "avr5", "avr5", 0x800100},
163  {"atmega325", "avr5", "avr5", 0x800100},
164  {"atmega325a", "avr5", "avr5", 0x800100},
165  {"atmega325p", "avr5", "avr5", 0x800100},
166  {"atmega325pa", "avr5", "avr5", 0x800100},
167  {"atmega3250", "avr5", "avr5", 0x800100},
168  {"atmega3250a", "avr5", "avr5", 0x800100},
169  {"atmega3250p", "avr5", "avr5", 0x800100},
170  {"atmega3250pa", "avr5", "avr5", 0x800100},
171  {"atmega328", "avr5", "avr5", 0x800100},
172  {"atmega328p", "avr5", "avr5", 0x800100},
173  {"atmega328pb", "avr5", "avr5", 0x800100},
174  {"atmega329", "avr5", "avr5", 0x800100},
175  {"atmega329a", "avr5", "avr5", 0x800100},
176  {"atmega329p", "avr5", "avr5", 0x800100},
177  {"atmega329pa", "avr5", "avr5", 0x800100},
178  {"atmega3290", "avr5", "avr5", 0x800100},
179  {"atmega3290a", "avr5", "avr5", 0x800100},
180  {"atmega3290p", "avr5", "avr5", 0x800100},
181  {"atmega3290pa", "avr5", "avr5", 0x800100},
182  {"atmega406", "avr5", "avr5", 0x800100},
183  {"atmega64", "avr5", "avr5", 0x800100},
184  {"atmega64a", "avr5", "avr5", 0x800100},
185  {"atmega640", "avr5", "avr5", 0x800200},
186  {"atmega644", "avr5", "avr5", 0x800100},
187  {"atmega644a", "avr5", "avr5", 0x800100},
188  {"atmega644p", "avr5", "avr5", 0x800100},
189  {"atmega644pa", "avr5", "avr5", 0x800100},
190  {"atmega645", "avr5", "avr5", 0x800100},
191  {"atmega645a", "avr5", "avr5", 0x800100},
192  {"atmega645p", "avr5", "avr5", 0x800100},
193  {"atmega649", "avr5", "avr5", 0x800100},
194  {"atmega649a", "avr5", "avr5", 0x800100},
195  {"atmega649p", "avr5", "avr5", 0x800100},
196  {"atmega6450", "avr5", "avr5", 0x800100},
197  {"atmega6450a", "avr5", "avr5", 0x800100},
198  {"atmega6450p", "avr5", "avr5", 0x800100},
199  {"atmega6490", "avr5", "avr5", 0x800100},
200  {"atmega6490a", "avr5", "avr5", 0x800100},
201  {"atmega6490p", "avr5", "avr5", 0x800100},
202  {"atmega64rfr2", "avr5", "avr5", 0x800200},
203  {"atmega644rfr2", "avr5", "avr5", 0x800200},
204  {"atmega16hva", "avr5", "avr5", 0x800100},
205  {"atmega16hva2", "avr5", "avr5", 0x800100},
206  {"atmega16hvb", "avr5", "avr5", 0x800100},
207  {"atmega16hvbrevb", "avr5", "avr5", 0x800100},
208  {"atmega32hvb", "avr5", "avr5", 0x800100},
209  {"atmega32hvbrevb", "avr5", "avr5", 0x800100},
210  {"atmega64hve", "avr5", "avr5", 0x800100},
211  {"atmega64hve2", "avr5", "avr5", 0x800100},
212  {"at90can32", "avr5", "avr5", 0x800100},
213  {"at90can64", "avr5", "avr5", 0x800100},
214  {"at90pwm161", "avr5", "avr5", 0x800100},
215  {"at90pwm216", "avr5", "avr5", 0x800100},
216  {"at90pwm316", "avr5", "avr5", 0x800100},
217  {"atmega32c1", "avr5", "avr5", 0x800100},
218  {"atmega64c1", "avr5", "avr5", 0x800100},
219  {"atmega16m1", "avr5", "avr5", 0x800100},
220  {"atmega32m1", "avr5", "avr5", 0x800100},
221  {"atmega64m1", "avr5", "avr5", 0x800100},
222  {"atmega16u4", "avr5", "avr5", 0x800100},
223  {"atmega32u4", "avr5", "avr5", 0x800100},
224  {"atmega32u6", "avr5", "avr5", 0x800100},
225  {"at90usb646", "avr5", "avr5", 0x800100},
226  {"at90usb647", "avr5", "avr5", 0x800100},
227  {"at90scr100", "avr5", "avr5", 0x800100},
228  {"at94k", "avr5", "avr5", 0x800060},
229  {"m3000", "avr5", "avr5", 0x800060},
230  {"atmega128", "avr51", "avr51", 0x800100},
231  {"atmega128a", "avr51", "avr51", 0x800100},
232  {"atmega1280", "avr51", "avr51", 0x800200},
233  {"atmega1281", "avr51", "avr51", 0x800200},
234  {"atmega1284", "avr51", "avr51", 0x800100},
235  {"atmega1284p", "avr51", "avr51", 0x800100},
236  {"atmega128rfa1", "avr51", "avr51", 0x800200},
237  {"atmega128rfr2", "avr51", "avr51", 0x800200},
238  {"atmega1284rfr2", "avr51", "avr51", 0x800200},
239  {"at90can128", "avr51", "avr51", 0x800200},
240  {"at90usb1286", "avr51", "avr51", 0x800200},
241  {"at90usb1287", "avr51", "avr51", 0x800200},
242  {"atmega2560", "avr6", "avr6", 0x800200},
243  {"atmega2561", "avr6", "avr6", 0x800200},
244  {"atmega256rfr2", "avr6", "avr6", 0x800200},
245  {"atmega2564rfr2", "avr6", "avr6", 0x800200},
246  {"attiny4", "avrtiny", "avrtiny", 0x800040},
247  {"attiny5", "avrtiny", "avrtiny", 0x800040},
248  {"attiny9", "avrtiny", "avrtiny", 0x800040},
249  {"attiny10", "avrtiny", "avrtiny", 0x800040},
250  {"attiny20", "avrtiny", "avrtiny", 0x800040},
251  {"attiny40", "avrtiny", "avrtiny", 0x800040},
252  {"attiny102", "avrtiny", "avrtiny", 0x800040},
253  {"attiny104", "avrtiny", "avrtiny", 0x800040},
254  {"atxmega16a4", "avrxmega2", "avrxmega2", 0x802000},
255  {"atxmega16a4u", "avrxmega2", "avrxmega2", 0x802000},
256  {"atxmega16c4", "avrxmega2", "avrxmega2", 0x802000},
257  {"atxmega16d4", "avrxmega2", "avrxmega2", 0x802000},
258  {"atxmega32a4", "avrxmega2", "avrxmega2", 0x802000},
259  {"atxmega32a4u", "avrxmega2", "avrxmega2", 0x802000},
260  {"atxmega32c3", "avrxmega2", "avrxmega2", 0x802000},
261  {"atxmega32c4", "avrxmega2", "avrxmega2", 0x802000},
262  {"atxmega32d3", "avrxmega2", "avrxmega2", 0x802000},
263  {"atxmega32d4", "avrxmega2", "avrxmega2", 0x802000},
264  {"atxmega32e5", "avrxmega2", "avrxmega2", 0x802000},
265  {"atxmega16e5", "avrxmega2", "avrxmega2", 0x802000},
266  {"atxmega8e5", "avrxmega2", "avrxmega2", 0x802000},
267  {"atxmega64a3", "avrxmega4", "avrxmega4", 0x802000},
268  {"atxmega64a3u", "avrxmega4", "avrxmega4", 0x802000},
269  {"atxmega64a4u", "avrxmega4", "avrxmega4", 0x802000},
270  {"atxmega64b1", "avrxmega4", "avrxmega4", 0x802000},
271  {"atxmega64b3", "avrxmega4", "avrxmega4", 0x802000},
272  {"atxmega64c3", "avrxmega4", "avrxmega4", 0x802000},
273  {"atxmega64d3", "avrxmega4", "avrxmega4", 0x802000},
274  {"atxmega64d4", "avrxmega4", "avrxmega4", 0x802000},
275  {"atxmega64a1", "avrxmega5", "avrxmega5", 0x802000},
276  {"atxmega64a1u", "avrxmega5", "avrxmega5", 0x802000},
277  {"atxmega128a3", "avrxmega6", "avrxmega6", 0x802000},
278  {"atxmega128a3u", "avrxmega6", "avrxmega6", 0x802000},
279  {"atxmega128b1", "avrxmega6", "avrxmega6", 0x802000},
280  {"atxmega128b3", "avrxmega6", "avrxmega6", 0x802000},
281  {"atxmega128c3", "avrxmega6", "avrxmega6", 0x802000},
282  {"atxmega128d3", "avrxmega6", "avrxmega6", 0x802000},
283  {"atxmega128d4", "avrxmega6", "avrxmega6", 0x802000},
284  {"atxmega192a3", "avrxmega6", "avrxmega6", 0x802000},
285  {"atxmega192a3u", "avrxmega6", "avrxmega6", 0x802000},
286  {"atxmega192c3", "avrxmega6", "avrxmega6", 0x802000},
287  {"atxmega192d3", "avrxmega6", "avrxmega6", 0x802000},
288  {"atxmega256a3", "avrxmega6", "avrxmega6", 0x802000},
289  {"atxmega256a3u", "avrxmega6", "avrxmega6", 0x802000},
290  {"atxmega256a3b", "avrxmega6", "avrxmega6", 0x802000},
291  {"atxmega256a3bu", "avrxmega6", "avrxmega6", 0x802000},
292  {"atxmega256c3", "avrxmega6", "avrxmega6", 0x802000},
293  {"atxmega256d3", "avrxmega6", "avrxmega6", 0x802000},
294  {"atxmega384c3", "avrxmega6", "avrxmega6", 0x802000},
295  {"atxmega384d3", "avrxmega6", "avrxmega6", 0x802000},
296  {"atxmega128a1", "avrxmega7", "avrxmega7", 0x802000},
297  {"atxmega128a1u", "avrxmega7", "avrxmega7", 0x802000},
298  {"atxmega128a4u", "avrxmega7", "avrxmega7", 0x802000},
299  {"attiny202", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
300  {"attiny204", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
301  {"attiny212", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
302  {"attiny214", "avrxmega3/short-calls", "avrxmega3", 0x803F80},
303  {"attiny402", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
304  {"attiny404", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
305  {"attiny406", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
306  {"attiny412", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
307  {"attiny414", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
308  {"attiny416", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
309  {"attiny417", "avrxmega3/short-calls", "avrxmega3", 0x803F00},
310  {"attiny804", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
311  {"attiny806", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
312  {"attiny807", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
313  {"attiny814", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
314  {"attiny816", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
315  {"attiny817", "avrxmega3/short-calls", "avrxmega3", 0x803E00},
316  {"atmega808", "avrxmega3/short-calls", "avrxmega3", 0x803C00},
317  {"atmega809", "avrxmega3/short-calls", "avrxmega3", 0x803C00},
318  {"atmega1608", "avrxmega3", "avrxmega3", 0x803800},
319  {"atmega1609", "avrxmega3", "avrxmega3", 0x803800},
320  {"atmega3208", "avrxmega3", "avrxmega3", 0x803000},
321  {"atmega3209", "avrxmega3", "avrxmega3", 0x803000},
322  {"atmega4808", "avrxmega3", "avrxmega3", 0x802800},
323  {"atmega4809", "avrxmega3", "avrxmega3", 0x802800},
324  {"attiny1604", "avrxmega3", "avrxmega3", 0x803C00},
325  {"attiny1606", "avrxmega3", "avrxmega3", 0x803C00},
326  {"attiny1607", "avrxmega3", "avrxmega3", 0x803C00},
327  {"attiny1614", "avrxmega3", "avrxmega3", 0x803800},
328  {"attiny1616", "avrxmega3", "avrxmega3", 0x803800},
329  {"attiny1617", "avrxmega3", "avrxmega3", 0x803800},
330  {"attiny1624", "avrxmega3", "avrxmega3", 0x803800},
331  {"attiny1626", "avrxmega3", "avrxmega3", 0x803800},
332  {"attiny1627", "avrxmega3", "avrxmega3", 0x803800},
333  {"attiny3216", "avrxmega3", "avrxmega3", 0x803800},
334  {"attiny3217", "avrxmega3", "avrxmega3", 0x803800},
335 };
336 
337 std::string GetMCUSubPath(StringRef MCUName) {
338  for (const auto &MCU : MCUInfo)
339  if (MCU.Name == MCUName)
340  return std::string(MCU.SubPath);
341  return "";
342 }
343 
344 llvm::Optional<StringRef> GetMCUFamilyName(StringRef MCUName) {
345  for (const auto &MCU : MCUInfo)
346  if (MCU.Name == MCUName)
347  return Optional<StringRef>(MCU.Family);
348  return Optional<StringRef>();
349 }
350 
351 llvm::Optional<unsigned> GetMCUSectionAddressData(StringRef MCUName) {
352  for (const auto &MCU : MCUInfo)
353  if (MCU.Name == MCUName && MCU.DataAddr > 0)
354  return Optional<unsigned>(MCU.DataAddr);
355  return Optional<unsigned>();
356 }
357 
358 const StringRef PossibleAVRLibcLocations[] = {
359  "/avr",
360  "/usr/avr",
361  "/usr/lib/avr",
362 };
363 
364 } // end anonymous namespace
365 
366 /// AVR Toolchain
367 AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
368  const ArgList &Args)
369  : Generic_ELF(D, Triple, Args) {
370  GCCInstallation.init(Triple, Args);
371 
372  std::string CPU = getCPUName(D, Args, Triple);
373  if (CPU.empty())
374  D.Diag(diag::warn_drv_avr_mcu_not_specified);
375 
376  // Only add default libraries if the user hasn't explicitly opted out.
377  if (!Args.hasArg(options::OPT_nostdlib) &&
378  !Args.hasArg(options::OPT_nodefaultlibs) &&
380  GCCInstallPath = GCCInstallation.getInstallPath();
382  getProgramPaths().push_back(GCCParentPath + "/../bin");
383  }
384 }
385 
386 void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
387  ArgStringList &CC1Args) const {
388  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
389  DriverArgs.hasArg(options::OPT_nostdlibinc))
390  return;
391 
392  // Omit if there is no avr-libc installed.
394  if (!AVRLibcRoot.hasValue())
395  return;
396 
397  // Add 'avr-libc/include' to clang system include paths if applicable.
398  std::string AVRInc = AVRLibcRoot.getValue() + "/include";
399  if (llvm::sys::fs::is_directory(AVRInc))
400  addSystemInclude(DriverArgs, CC1Args, AVRInc);
401 }
402 
404  const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
405  Action::OffloadKind DeviceOffloadKind) const {
406  // By default, use `.ctors` (not `.init_array`), as required by libgcc, which
407  // runs constructors/destructors on AVR.
408  if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
409  options::OPT_fno_use_init_array, false))
410  CC1Args.push_back("-fno-use-init-array");
411  // Use `-fno-use-cxa-atexit` as default, since avr-libc does not support
412  // `__cxa_atexit()`.
413  if (!DriverArgs.hasFlag(options::OPT_fuse_cxa_atexit,
414  options::OPT_fno_use_cxa_atexit, false))
415  CC1Args.push_back("-fno-use-cxa-atexit");
416 }
417 
419  return new tools::AVR::Linker(getTriple(), *this);
420 }
421 
422 void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
423  const InputInfo &Output,
424  const InputInfoList &Inputs, const ArgList &Args,
425  const char *LinkingOutput) const {
426  const auto &TC = static_cast<const AVRToolChain &>(getToolChain());
427  const Driver &D = getToolChain().getDriver();
428 
429  // Compute information about the target AVR.
430  std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
431  llvm::Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
432  llvm::Optional<unsigned> SectionAddressData = GetMCUSectionAddressData(CPU);
433 
434  std::string Linker = getToolChain().GetProgramPath(getShortName());
435  ArgStringList CmdArgs;
436  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
437 
438  CmdArgs.push_back("-o");
439  CmdArgs.push_back(Output.getFilename());
440 
441  // Enable garbage collection of unused sections.
442  CmdArgs.push_back("--gc-sections");
443 
444  // Add library search paths before we specify libraries.
445  Args.AddAllArgs(CmdArgs, options::OPT_L);
446  getToolChain().AddFilePathLibArgs(Args, CmdArgs);
447 
448  // Only add default libraries if the user hasn't explicitly opted out.
449  bool LinkStdlib = false;
450  if (!Args.hasArg(options::OPT_nostdlib) &&
451  !Args.hasArg(options::OPT_nodefaultlibs)) {
452  if (!CPU.empty()) {
453  Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
454  Optional<std::string> AVRLibcRoot = TC.findAVRLibcInstallation();
455 
456  if (!FamilyName) {
457  // We do not have an entry for this CPU in the family
458  // mapping table yet.
459  D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
460  << CPU;
461  } else if (TC.getGCCInstallPath().empty()) {
462  // We can not link since there is no avr-ld.
463  D.Diag(diag::warn_drv_avr_gcc_not_found);
464  } else if (!AVRLibcRoot) {
465  // No avr-libc found and so no runtime linked.
466  D.Diag(diag::warn_drv_avr_libc_not_found);
467  } else {
468  std::string SubPath = GetMCUSubPath(CPU);
469  CmdArgs.push_back(
470  Args.MakeArgString(Twine("-L") + *AVRLibcRoot + "/lib/" + SubPath));
471  CmdArgs.push_back(
472  Args.MakeArgString("-L" + TC.getGCCInstallPath() + "/" + SubPath));
473  LinkStdlib = true;
474  }
475  }
476 
477  if (!LinkStdlib)
478  D.Diag(diag::warn_drv_avr_stdlib_not_linked);
479  }
480 
481  if (SectionAddressData.hasValue()) {
482  std::string DataSectionArg = std::string("-Tdata=0x") +
483  llvm::utohexstr(SectionAddressData.getValue());
484  CmdArgs.push_back(Args.MakeArgString(DataSectionArg));
485  } else {
486  // We do not have an entry for this CPU in the address mapping table yet.
487  D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented) << CPU;
488  }
489 
490  // If the family name is known, we can link with the device-specific libgcc.
491  // Without it, libgcc will simply not be linked. This matches avr-gcc
492  // behavior.
493  if (LinkStdlib) {
494  assert(!CPU.empty() && "CPU name must be known in order to link stdlibs");
495 
496  CmdArgs.push_back("--start-group");
497 
498  // Add the object file for the CRT.
499  std::string CrtFileName = std::string("-l:crt") + CPU + std::string(".o");
500  CmdArgs.push_back(Args.MakeArgString(CrtFileName));
501 
502  CmdArgs.push_back("-lgcc");
503  CmdArgs.push_back("-lm");
504  CmdArgs.push_back("-lc");
505 
506  // Add the link library specific to the MCU.
507  CmdArgs.push_back(Args.MakeArgString(std::string("-l") + CPU));
508 
509  CmdArgs.push_back("--end-group");
510 
511  // Specify the family name as the emulation mode to use.
512  // This is almost always required because otherwise avr-ld
513  // will assume 'avr2' and warn about the program being larger
514  // than the bare minimum supports.
515  CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
516  }
517 
518  C.addCommand(std::make_unique<Command>(
519  JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
520  CmdArgs, Inputs, Output));
521 }
522 
524  // Search avr-libc installation according to avr-gcc installation.
526  std::string Path(GCCParent + "/avr");
527  if (llvm::sys::fs::is_directory(Path))
528  return Path;
529  Path = GCCParent + "/../avr";
530  if (llvm::sys::fs::is_directory(Path))
531  return Path;
532 
533  // Search avr-libc installation from possible locations, and return the first
534  // one that exists, if there is no avr-gcc installed.
535  for (StringRef PossiblePath : PossibleAVRLibcLocations) {
536  std::string Path = getDriver().SysRoot + PossiblePath.str();
537  if (llvm::sys::fs::is_directory(Path))
538  return Path;
539  }
540 
541  return llvm::None;
542 }
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:266
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::toolchains::Generic_GCC::GCCInstallation
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:288
clang::driver::tools
Definition: AIX.h:17
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:225
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::isValid
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:224
clang::driver::tools::getCPUName
std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
llvm::Optional
Definition: LLVM.h:40
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::init
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1993
clang::driver::toolchains::AVRToolChain::AddClangSystemIncludeArgs
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:386
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
InputInfo.h
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:132
clang::driver::tools::AVR::Linker
Definition: AVR.h:48
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::toolchains::Generic_ELF
Definition: Gnu.h:362
Options.h
clang::driver::toolchains::AVRToolChain::findAVRLibcInstallation
llvm::Optional< std::string > findAVRLibcInstallation() const
Definition: AVR.cpp:523
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
clang::driver::ResponseFileSupport::AtFileCurCP
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92
clang::driver::toolchains::AVRToolChain::addClangTargetOptions
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:403
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getParentLibPath
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:233
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:227
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:171
clang::driver::toolchains::AVRToolChain
Definition: AVR.h:21
Compilation.h
clang::driver::toolchains::AVRToolChain::buildLinker
Tool * buildLinker() const override
Definition: AVR.cpp:418
clang::driver::toolchains::Generic_GCC::GCCInstallationDetector::getInstallPath
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:230
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:71
clang::driver::toolchains::AVRToolChain::AVRToolChain
AVRToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
AVR Toolchain.
Definition: AVR.cpp:367
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:88
clang::driver
Definition: Action.h:31
clang::driver::ToolChain::addSystemInclude
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:878
AVR.h
clang::driver::JobAction
Definition: Action.h:395