Coverage Report

Created: 2021-08-24 07:12

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