AVR.cpp 21 KB

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