ARMAttributeParser.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //===- ARMAttributeParser.cpp - ARM Attribute Information Printer ---------===//
  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 "llvm/Support/ARMAttributeParser.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/Support/ARMBuildAttributes.h"
  11. #include "llvm/Support/Errc.h"
  12. #include "llvm/Support/ScopedPrinter.h"
  13. #include <optional>
  14. using namespace llvm;
  15. using namespace llvm::ARMBuildAttrs;
  16. #define ATTRIBUTE_HANDLER(attr) \
  17. { ARMBuildAttrs::attr, &ARMAttributeParser::attr }
  18. const ARMAttributeParser::DisplayHandler ARMAttributeParser::displayRoutines[] =
  19. {
  20. {ARMBuildAttrs::CPU_raw_name, &ARMAttributeParser::stringAttribute},
  21. {ARMBuildAttrs::CPU_name, &ARMAttributeParser::stringAttribute},
  22. ATTRIBUTE_HANDLER(CPU_arch),
  23. ATTRIBUTE_HANDLER(CPU_arch_profile),
  24. ATTRIBUTE_HANDLER(ARM_ISA_use),
  25. ATTRIBUTE_HANDLER(THUMB_ISA_use),
  26. ATTRIBUTE_HANDLER(FP_arch),
  27. ATTRIBUTE_HANDLER(WMMX_arch),
  28. ATTRIBUTE_HANDLER(Advanced_SIMD_arch),
  29. ATTRIBUTE_HANDLER(MVE_arch),
  30. ATTRIBUTE_HANDLER(PCS_config),
  31. ATTRIBUTE_HANDLER(ABI_PCS_R9_use),
  32. ATTRIBUTE_HANDLER(ABI_PCS_RW_data),
  33. ATTRIBUTE_HANDLER(ABI_PCS_RO_data),
  34. ATTRIBUTE_HANDLER(ABI_PCS_GOT_use),
  35. ATTRIBUTE_HANDLER(ABI_PCS_wchar_t),
  36. ATTRIBUTE_HANDLER(ABI_FP_rounding),
  37. ATTRIBUTE_HANDLER(ABI_FP_denormal),
  38. ATTRIBUTE_HANDLER(ABI_FP_exceptions),
  39. ATTRIBUTE_HANDLER(ABI_FP_user_exceptions),
  40. ATTRIBUTE_HANDLER(ABI_FP_number_model),
  41. ATTRIBUTE_HANDLER(ABI_align_needed),
  42. ATTRIBUTE_HANDLER(ABI_align_preserved),
  43. ATTRIBUTE_HANDLER(ABI_enum_size),
  44. ATTRIBUTE_HANDLER(ABI_HardFP_use),
  45. ATTRIBUTE_HANDLER(ABI_VFP_args),
  46. ATTRIBUTE_HANDLER(ABI_WMMX_args),
  47. ATTRIBUTE_HANDLER(ABI_optimization_goals),
  48. ATTRIBUTE_HANDLER(ABI_FP_optimization_goals),
  49. ATTRIBUTE_HANDLER(compatibility),
  50. ATTRIBUTE_HANDLER(CPU_unaligned_access),
  51. ATTRIBUTE_HANDLER(FP_HP_extension),
  52. ATTRIBUTE_HANDLER(ABI_FP_16bit_format),
  53. ATTRIBUTE_HANDLER(MPextension_use),
  54. ATTRIBUTE_HANDLER(DIV_use),
  55. ATTRIBUTE_HANDLER(DSP_extension),
  56. ATTRIBUTE_HANDLER(T2EE_use),
  57. ATTRIBUTE_HANDLER(Virtualization_use),
  58. ATTRIBUTE_HANDLER(PAC_extension),
  59. ATTRIBUTE_HANDLER(BTI_extension),
  60. ATTRIBUTE_HANDLER(PACRET_use),
  61. ATTRIBUTE_HANDLER(BTI_use),
  62. ATTRIBUTE_HANDLER(nodefaults),
  63. ATTRIBUTE_HANDLER(also_compatible_with),
  64. };
  65. #undef ATTRIBUTE_HANDLER
  66. Error ARMAttributeParser::stringAttribute(AttrType tag) {
  67. StringRef tagName =
  68. ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
  69. StringRef desc = de.getCStrRef(cursor);
  70. if (sw) {
  71. DictScope scope(*sw, "Attribute");
  72. sw->printNumber("Tag", tag);
  73. if (!tagName.empty())
  74. sw->printString("TagName", tagName);
  75. sw->printString("Value", desc);
  76. }
  77. return Error::success();
  78. }
  79. static const char *CPU_arch_strings[] = {
  80. "Pre-v4", "ARM v4", "ARM v4T", "ARM v5T", "ARM v5TE", "ARM v5TEJ",
  81. "ARM v6", "ARM v6KZ", "ARM v6T2", "ARM v6K", "ARM v7", "ARM v6-M",
  82. "ARM v6S-M", "ARM v7E-M", "ARM v8-A", "ARM v8-R", "ARM v8-M Baseline",
  83. "ARM v8-M Mainline", nullptr, nullptr, nullptr, "ARM v8.1-M Mainline",
  84. "ARM v9-A"};
  85. Error ARMAttributeParser::CPU_arch(AttrType tag) {
  86. return parseStringAttribute("CPU_arch", tag, ArrayRef(CPU_arch_strings));
  87. }
  88. Error ARMAttributeParser::CPU_arch_profile(AttrType tag) {
  89. uint64_t value = de.getULEB128(cursor);
  90. StringRef profile;
  91. switch (value) {
  92. default: profile = "Unknown"; break;
  93. case 'A': profile = "Application"; break;
  94. case 'R': profile = "Real-time"; break;
  95. case 'M': profile = "Microcontroller"; break;
  96. case 'S': profile = "Classic"; break;
  97. case 0: profile = "None"; break;
  98. }
  99. printAttribute(tag, value, profile);
  100. return Error::success();
  101. }
  102. Error ARMAttributeParser::ARM_ISA_use(AttrType tag) {
  103. static const char *strings[] = {"Not Permitted", "Permitted"};
  104. return parseStringAttribute("ARM_ISA_use", tag, ArrayRef(strings));
  105. }
  106. Error ARMAttributeParser::THUMB_ISA_use(AttrType tag) {
  107. static const char *strings[] = {"Not Permitted", "Thumb-1", "Thumb-2", "Permitted"};
  108. return parseStringAttribute("THUMB_ISA_use", tag, ArrayRef(strings));
  109. }
  110. Error ARMAttributeParser::FP_arch(AttrType tag) {
  111. static const char *strings[] = {
  112. "Not Permitted", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16",
  113. "VFPv4", "VFPv4-D16", "ARMv8-a FP", "ARMv8-a FP-D16"};
  114. return parseStringAttribute("FP_arch", tag, ArrayRef(strings));
  115. }
  116. Error ARMAttributeParser::WMMX_arch(AttrType tag) {
  117. static const char *strings[] = {"Not Permitted", "WMMXv1", "WMMXv2"};
  118. return parseStringAttribute("WMMX_arch", tag, ArrayRef(strings));
  119. }
  120. Error ARMAttributeParser::Advanced_SIMD_arch(AttrType tag) {
  121. static const char *strings[] = {"Not Permitted", "NEONv1", "NEONv2+FMA",
  122. "ARMv8-a NEON", "ARMv8.1-a NEON"};
  123. return parseStringAttribute("Advanced_SIMD_arch", tag, ArrayRef(strings));
  124. }
  125. Error ARMAttributeParser::MVE_arch(AttrType tag) {
  126. static const char *strings[] = {"Not Permitted", "MVE integer",
  127. "MVE integer and float"};
  128. return parseStringAttribute("MVE_arch", tag, ArrayRef(strings));
  129. }
  130. Error ARMAttributeParser::PCS_config(AttrType tag) {
  131. static const char *strings[] = {
  132. "None", "Bare Platform", "Linux Application", "Linux DSO", "Palm OS 2004",
  133. "Reserved (Palm OS)", "Symbian OS 2004", "Reserved (Symbian OS)"};
  134. return parseStringAttribute("PCS_config", tag, ArrayRef(strings));
  135. }
  136. Error ARMAttributeParser::ABI_PCS_R9_use(AttrType tag) {
  137. static const char *strings[] = {"v6", "Static Base", "TLS", "Unused"};
  138. return parseStringAttribute("ABI_PCS_R9_use", tag, ArrayRef(strings));
  139. }
  140. Error ARMAttributeParser::ABI_PCS_RW_data(AttrType tag) {
  141. static const char *strings[] = {"Absolute", "PC-relative", "SB-relative",
  142. "Not Permitted"};
  143. return parseStringAttribute("ABI_PCS_RW_data", tag, ArrayRef(strings));
  144. }
  145. Error ARMAttributeParser::ABI_PCS_RO_data(AttrType tag) {
  146. static const char *strings[] = {"Absolute", "PC-relative", "Not Permitted"};
  147. return parseStringAttribute("ABI_PCS_RO_data", tag, ArrayRef(strings));
  148. }
  149. Error ARMAttributeParser::ABI_PCS_GOT_use(AttrType tag) {
  150. static const char *strings[] = {"Not Permitted", "Direct", "GOT-Indirect"};
  151. return parseStringAttribute("ABI_PCS_GOT_use", tag, ArrayRef(strings));
  152. }
  153. Error ARMAttributeParser::ABI_PCS_wchar_t(AttrType tag) {
  154. static const char *strings[] = {"Not Permitted", "Unknown", "2-byte",
  155. "Unknown", "4-byte"};
  156. return parseStringAttribute("ABI_PCS_wchar_t", tag, ArrayRef(strings));
  157. }
  158. Error ARMAttributeParser::ABI_FP_rounding(AttrType tag) {
  159. static const char *strings[] = {"IEEE-754", "Runtime"};
  160. return parseStringAttribute("ABI_FP_rounding", tag, ArrayRef(strings));
  161. }
  162. Error ARMAttributeParser::ABI_FP_denormal(AttrType tag) {
  163. static const char *strings[] = {"Unsupported", "IEEE-754", "Sign Only"};
  164. return parseStringAttribute("ABI_FP_denormal", tag, ArrayRef(strings));
  165. }
  166. Error ARMAttributeParser::ABI_FP_exceptions(AttrType tag) {
  167. static const char *strings[] = {"Not Permitted", "IEEE-754"};
  168. return parseStringAttribute("ABI_FP_exceptions", tag, ArrayRef(strings));
  169. }
  170. Error ARMAttributeParser::ABI_FP_user_exceptions(AttrType tag) {
  171. static const char *strings[] = {"Not Permitted", "IEEE-754"};
  172. return parseStringAttribute("ABI_FP_user_exceptions", tag, ArrayRef(strings));
  173. }
  174. Error ARMAttributeParser::ABI_FP_number_model(AttrType tag) {
  175. static const char *strings[] = {"Not Permitted", "Finite Only", "RTABI",
  176. "IEEE-754"};
  177. return parseStringAttribute("ABI_FP_number_model", tag, ArrayRef(strings));
  178. }
  179. Error ARMAttributeParser::ABI_align_needed(AttrType tag) {
  180. static const char *strings[] = {"Not Permitted", "8-byte alignment",
  181. "4-byte alignment", "Reserved"};
  182. uint64_t value = de.getULEB128(cursor);
  183. std::string description;
  184. if (value < std::size(strings))
  185. description = strings[value];
  186. else if (value <= 12)
  187. description = "8-byte alignment, " + utostr(1ULL << value) +
  188. "-byte extended alignment";
  189. else
  190. description = "Invalid";
  191. printAttribute(tag, value, description);
  192. return Error::success();
  193. }
  194. Error ARMAttributeParser::ABI_align_preserved(AttrType tag) {
  195. static const char *strings[] = {"Not Required", "8-byte data alignment",
  196. "8-byte data and code alignment", "Reserved"};
  197. uint64_t value = de.getULEB128(cursor);
  198. std::string description;
  199. if (value < std::size(strings))
  200. description = std::string(strings[value]);
  201. else if (value <= 12)
  202. description = std::string("8-byte stack alignment, ") +
  203. utostr(1ULL << value) + std::string("-byte data alignment");
  204. else
  205. description = "Invalid";
  206. printAttribute(tag, value, description);
  207. return Error::success();
  208. }
  209. Error ARMAttributeParser::ABI_enum_size(AttrType tag) {
  210. static const char *strings[] = {"Not Permitted", "Packed", "Int32",
  211. "External Int32"};
  212. return parseStringAttribute("ABI_enum_size", tag, ArrayRef(strings));
  213. }
  214. Error ARMAttributeParser::ABI_HardFP_use(AttrType tag) {
  215. static const char *strings[] = {"Tag_FP_arch", "Single-Precision", "Reserved",
  216. "Tag_FP_arch (deprecated)"};
  217. return parseStringAttribute("ABI_HardFP_use", tag, ArrayRef(strings));
  218. }
  219. Error ARMAttributeParser::ABI_VFP_args(AttrType tag) {
  220. static const char *strings[] = {"AAPCS", "AAPCS VFP", "Custom",
  221. "Not Permitted"};
  222. return parseStringAttribute("ABI_VFP_args", tag, ArrayRef(strings));
  223. }
  224. Error ARMAttributeParser::ABI_WMMX_args(AttrType tag) {
  225. static const char *strings[] = {"AAPCS", "iWMMX", "Custom"};
  226. return parseStringAttribute("ABI_WMMX_args", tag, ArrayRef(strings));
  227. }
  228. Error ARMAttributeParser::ABI_optimization_goals(AttrType tag) {
  229. static const char *strings[] = {
  230. "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size", "Debugging",
  231. "Best Debugging"
  232. };
  233. return parseStringAttribute("ABI_optimization_goals", tag, ArrayRef(strings));
  234. }
  235. Error ARMAttributeParser::ABI_FP_optimization_goals(AttrType tag) {
  236. static const char *strings[] = {
  237. "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size",
  238. "Accuracy", "Best Accuracy"};
  239. return parseStringAttribute("ABI_FP_optimization_goals", tag,
  240. ArrayRef(strings));
  241. }
  242. Error ARMAttributeParser::compatibility(AttrType tag) {
  243. uint64_t integer = de.getULEB128(cursor);
  244. StringRef string = de.getCStrRef(cursor);
  245. if (sw) {
  246. DictScope scope(*sw, "Attribute");
  247. sw->printNumber("Tag", tag);
  248. sw->startLine() << "Value: " << integer << ", " << string << '\n';
  249. sw->printString("TagName",
  250. ELFAttrs::attrTypeAsString(tag, tagToStringMap,
  251. /*hasTagPrefix=*/false));
  252. switch (integer) {
  253. case 0:
  254. sw->printString("Description", StringRef("No Specific Requirements"));
  255. break;
  256. case 1:
  257. sw->printString("Description", StringRef("AEABI Conformant"));
  258. break;
  259. default:
  260. sw->printString("Description", StringRef("AEABI Non-Conformant"));
  261. break;
  262. }
  263. }
  264. return Error::success();
  265. }
  266. Error ARMAttributeParser::CPU_unaligned_access(AttrType tag) {
  267. static const char *strings[] = {"Not Permitted", "v6-style"};
  268. return parseStringAttribute("CPU_unaligned_access", tag, ArrayRef(strings));
  269. }
  270. Error ARMAttributeParser::FP_HP_extension(AttrType tag) {
  271. static const char *strings[] = {"If Available", "Permitted"};
  272. return parseStringAttribute("FP_HP_extension", tag, ArrayRef(strings));
  273. }
  274. Error ARMAttributeParser::ABI_FP_16bit_format(AttrType tag) {
  275. static const char *strings[] = {"Not Permitted", "IEEE-754", "VFPv3"};
  276. return parseStringAttribute("ABI_FP_16bit_format", tag, ArrayRef(strings));
  277. }
  278. Error ARMAttributeParser::MPextension_use(AttrType tag) {
  279. static const char *strings[] = {"Not Permitted", "Permitted"};
  280. return parseStringAttribute("MPextension_use", tag, ArrayRef(strings));
  281. }
  282. Error ARMAttributeParser::DIV_use(AttrType tag) {
  283. static const char *strings[] = {"If Available", "Not Permitted", "Permitted"};
  284. return parseStringAttribute("DIV_use", tag, ArrayRef(strings));
  285. }
  286. Error ARMAttributeParser::DSP_extension(AttrType tag) {
  287. static const char *strings[] = {"Not Permitted", "Permitted"};
  288. return parseStringAttribute("DSP_extension", tag, ArrayRef(strings));
  289. }
  290. Error ARMAttributeParser::T2EE_use(AttrType tag) {
  291. static const char *strings[] = {"Not Permitted", "Permitted"};
  292. return parseStringAttribute("T2EE_use", tag, ArrayRef(strings));
  293. }
  294. Error ARMAttributeParser::Virtualization_use(AttrType tag) {
  295. static const char *strings[] = {"Not Permitted", "TrustZone",
  296. "Virtualization Extensions",
  297. "TrustZone + Virtualization Extensions"};
  298. return parseStringAttribute("Virtualization_use", tag, ArrayRef(strings));
  299. }
  300. Error ARMAttributeParser::PAC_extension(ARMBuildAttrs::AttrType tag) {
  301. static const char *strings[] = {"Not Permitted", "Permitted in NOP space",
  302. "Permitted"};
  303. return parseStringAttribute("PAC_extension", tag, ArrayRef(strings));
  304. }
  305. Error ARMAttributeParser::BTI_extension(ARMBuildAttrs::AttrType tag) {
  306. static const char *strings[] = {"Not Permitted", "Permitted in NOP space",
  307. "Permitted"};
  308. return parseStringAttribute("BTI_extension", tag, ArrayRef(strings));
  309. }
  310. Error ARMAttributeParser::PACRET_use(ARMBuildAttrs::AttrType tag) {
  311. static const char *strings[] = {"Not Used", "Used"};
  312. return parseStringAttribute("PACRET_use", tag, ArrayRef(strings));
  313. }
  314. Error ARMAttributeParser::BTI_use(ARMBuildAttrs::AttrType tag) {
  315. static const char *strings[] = {"Not Used", "Used"};
  316. return parseStringAttribute("BTI_use", tag, ArrayRef(strings));
  317. }
  318. Error ARMAttributeParser::nodefaults(AttrType tag) {
  319. uint64_t value = de.getULEB128(cursor);
  320. printAttribute(tag, value, "Unspecified Tags UNDEFINED");
  321. return Error::success();
  322. }
  323. Error ARMAttributeParser::also_compatible_with(AttrType tag) {
  324. // Parse value as a C string first in order to print it in escaped form later.
  325. // Then, parse it again to catch errors or to pretty print if Tag_CPU_arch.
  326. std::optional<Error> returnValue;
  327. SmallString<8> Description;
  328. raw_svector_ostream DescStream(Description);
  329. uint64_t InitialOffset = cursor.tell();
  330. StringRef RawStringValue = de.getCStrRef(cursor);
  331. uint64_t FinalOffset = cursor.tell();
  332. cursor.seek(InitialOffset);
  333. uint64_t InnerTag = de.getULEB128(cursor);
  334. bool ValidInnerTag =
  335. any_of(tagToStringMap, [InnerTag](const TagNameItem &Item) {
  336. return Item.attr == InnerTag;
  337. });
  338. if (!ValidInnerTag) {
  339. returnValue =
  340. createStringError(errc::argument_out_of_domain,
  341. Twine(InnerTag) + " is not a valid tag number");
  342. } else {
  343. switch (InnerTag) {
  344. case ARMBuildAttrs::CPU_arch: {
  345. uint64_t InnerValue = de.getULEB128(cursor);
  346. auto strings = ArrayRef(CPU_arch_strings);
  347. if (InnerValue >= strings.size()) {
  348. returnValue = createStringError(
  349. errc::argument_out_of_domain,
  350. Twine(InnerValue) + " is not a valid " +
  351. ELFAttrs::attrTypeAsString(InnerTag, tagToStringMap) +
  352. " value");
  353. } else {
  354. DescStream << ELFAttrs::attrTypeAsString(InnerTag, tagToStringMap)
  355. << " = " << InnerValue;
  356. if (strings[InnerValue])
  357. DescStream << " (" << strings[InnerValue] << ')';
  358. }
  359. break;
  360. }
  361. case ARMBuildAttrs::also_compatible_with:
  362. returnValue = createStringError(
  363. errc::invalid_argument,
  364. ELFAttrs::attrTypeAsString(InnerTag, tagToStringMap) +
  365. " cannot be recursively defined");
  366. break;
  367. case ARMBuildAttrs::CPU_raw_name:
  368. case ARMBuildAttrs::CPU_name:
  369. case ARMBuildAttrs::compatibility:
  370. case ARMBuildAttrs::conformance: {
  371. StringRef InnerValue = de.getCStrRef(cursor);
  372. DescStream << ELFAttrs::attrTypeAsString(InnerTag, tagToStringMap)
  373. << " = " << InnerValue;
  374. break;
  375. }
  376. default: {
  377. uint64_t InnerValue = de.getULEB128(cursor);
  378. DescStream << ELFAttrs::attrTypeAsString(InnerTag, tagToStringMap)
  379. << " = " << InnerValue;
  380. }
  381. }
  382. }
  383. setAttributeString(tag, RawStringValue);
  384. if (sw) {
  385. DictScope scope(*sw, "Attribute");
  386. sw->printNumber("Tag", tag);
  387. sw->printString("TagName",
  388. ELFAttrs::attrTypeAsString(tag, tagToStringMap, false));
  389. sw->printStringEscaped("Value", RawStringValue);
  390. if (!Description.empty()) {
  391. sw->printString("Description", Description);
  392. }
  393. }
  394. cursor.seek(FinalOffset);
  395. return returnValue ? std::move(*returnValue) : Error::success();
  396. }
  397. Error ARMAttributeParser::handler(uint64_t tag, bool &handled) {
  398. handled = false;
  399. for (const auto &AH : displayRoutines) {
  400. if (uint64_t(AH.attribute) == tag) {
  401. if (Error e = (this->*AH.routine)(static_cast<AttrType>(tag)))
  402. return e;
  403. handled = true;
  404. break;
  405. }
  406. }
  407. return Error::success();
  408. }