1 //===--- ARMAttributeParser.cpp - ARM Attribute Information Printer -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ARMAttributeParser.h"
11 #include "StreamWriter.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/LEB128.h"
15 
16 using namespace llvm;
17 using namespace llvm::ARMBuildAttrs;
18 
19 
20 static const EnumEntry<unsigned> TagNames[] = {
21   { "Tag_File", ARMBuildAttrs::File },
22   { "Tag_Section", ARMBuildAttrs::Section },
23   { "Tag_Symbol", ARMBuildAttrs::Symbol },
24 };
25 
26 namespace llvm {
27 #define ATTRIBUTE_HANDLER(Attr_)                                                \
28   { ARMBuildAttrs::Attr_, &ARMAttributeParser::Attr_ }
29 
30 const ARMAttributeParser::DisplayHandler
31 ARMAttributeParser::DisplayRoutines[] = {
32   { ARMBuildAttrs::CPU_raw_name, &ARMAttributeParser::StringAttribute, },
33   { ARMBuildAttrs::CPU_name, &ARMAttributeParser::StringAttribute },
34   ATTRIBUTE_HANDLER(CPU_arch),
35   ATTRIBUTE_HANDLER(CPU_arch_profile),
36   ATTRIBUTE_HANDLER(ARM_ISA_use),
37   ATTRIBUTE_HANDLER(THUMB_ISA_use),
38   ATTRIBUTE_HANDLER(FP_arch),
39   ATTRIBUTE_HANDLER(WMMX_arch),
40   ATTRIBUTE_HANDLER(Advanced_SIMD_arch),
41   ATTRIBUTE_HANDLER(PCS_config),
42   ATTRIBUTE_HANDLER(ABI_PCS_R9_use),
43   ATTRIBUTE_HANDLER(ABI_PCS_RW_data),
44   ATTRIBUTE_HANDLER(ABI_PCS_RO_data),
45   ATTRIBUTE_HANDLER(ABI_PCS_GOT_use),
46   ATTRIBUTE_HANDLER(ABI_PCS_wchar_t),
47   ATTRIBUTE_HANDLER(ABI_FP_rounding),
48   ATTRIBUTE_HANDLER(ABI_FP_denormal),
49   ATTRIBUTE_HANDLER(ABI_FP_exceptions),
50   ATTRIBUTE_HANDLER(ABI_FP_user_exceptions),
51   ATTRIBUTE_HANDLER(ABI_FP_number_model),
52   ATTRIBUTE_HANDLER(ABI_align_needed),
53   ATTRIBUTE_HANDLER(ABI_align_preserved),
54   ATTRIBUTE_HANDLER(ABI_enum_size),
55   ATTRIBUTE_HANDLER(ABI_HardFP_use),
56   ATTRIBUTE_HANDLER(ABI_VFP_args),
57   ATTRIBUTE_HANDLER(ABI_WMMX_args),
58   ATTRIBUTE_HANDLER(ABI_optimization_goals),
59   ATTRIBUTE_HANDLER(ABI_FP_optimization_goals),
60   ATTRIBUTE_HANDLER(compatibility),
61   ATTRIBUTE_HANDLER(CPU_unaligned_access),
62   ATTRIBUTE_HANDLER(FP_HP_extension),
63   ATTRIBUTE_HANDLER(ABI_FP_16bit_format),
64   ATTRIBUTE_HANDLER(MPextension_use),
65   ATTRIBUTE_HANDLER(DIV_use),
66   ATTRIBUTE_HANDLER(T2EE_use),
67   ATTRIBUTE_HANDLER(Virtualization_use),
68   ATTRIBUTE_HANDLER(nodefaults)
69 };
70 
71 #undef ATTRIBUTE_HANDLER
72 
ParseInteger(const uint8_t * Data,uint32_t & Offset)73 uint64_t ARMAttributeParser::ParseInteger(const uint8_t *Data,
74                                           uint32_t &Offset) {
75   unsigned Length;
76   uint64_t Value = decodeULEB128(Data + Offset, &Length);
77   Offset = Offset + Length;
78   return Value;
79 }
80 
ParseString(const uint8_t * Data,uint32_t & Offset)81 StringRef ARMAttributeParser::ParseString(const uint8_t *Data,
82                                           uint32_t &Offset) {
83   const char *String = reinterpret_cast<const char*>(Data + Offset);
84   size_t Length = std::strlen(String);
85   Offset = Offset + Length + 1;
86   return StringRef(String, Length);
87 }
88 
IntegerAttribute(AttrType Tag,const uint8_t * Data,uint32_t & Offset)89 void ARMAttributeParser::IntegerAttribute(AttrType Tag, const uint8_t *Data,
90                                           uint32_t &Offset) {
91   SW.printNumber(ARMBuildAttrs::AttrTypeAsString(Tag),
92                  ParseInteger(Data, Offset));
93 }
94 
StringAttribute(AttrType Tag,const uint8_t * Data,uint32_t & Offset)95 void ARMAttributeParser::StringAttribute(AttrType Tag, const uint8_t *Data,
96                                          uint32_t &Offset) {
97   StringRef TagName = ARMBuildAttrs::AttrTypeAsString(Tag, /*TagPrefix*/false);
98 
99   DictScope AS(SW, "Attribute");
100   SW.printNumber("Tag", Tag);
101   if (!TagName.empty())
102     SW.printString("TagName", TagName);
103   SW.printString("Value", ParseString(Data, Offset));
104 }
105 
PrintAttribute(unsigned Tag,unsigned Value,StringRef ValueDesc)106 void ARMAttributeParser::PrintAttribute(unsigned Tag, unsigned Value,
107                                         StringRef ValueDesc) {
108   StringRef TagName = ARMBuildAttrs::AttrTypeAsString(Tag, /*TagPrefix*/false);
109 
110   DictScope AS(SW, "Attribute");
111   SW.printNumber("Tag", Tag);
112   SW.printNumber("Value", Value);
113   if (!TagName.empty())
114     SW.printString("TagName", TagName);
115   if (!ValueDesc.empty())
116     SW.printString("Description", ValueDesc);
117 }
118 
CPU_arch(AttrType Tag,const uint8_t * Data,uint32_t & Offset)119 void ARMAttributeParser::CPU_arch(AttrType Tag, const uint8_t *Data,
120                                   uint32_t &Offset) {
121   static const char *Strings[] = {
122     "Pre-v4", "ARM v4", "ARM v4T", "ARM v5T", "ARM v5TE", "ARM v5TEJ", "ARM v6",
123     "ARM v6KZ", "ARM v6T2", "ARM v6K", "ARM v7", "ARM v6-M", "ARM v6S-M",
124     "ARM v7E-M", "ARM v8"
125   };
126 
127   uint64_t Value = ParseInteger(Data, Offset);
128   StringRef ValueDesc =
129     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
130   PrintAttribute(Tag, Value, ValueDesc);
131 }
132 
CPU_arch_profile(AttrType Tag,const uint8_t * Data,uint32_t & Offset)133 void ARMAttributeParser::CPU_arch_profile(AttrType Tag, const uint8_t *Data,
134                                           uint32_t &Offset) {
135   uint64_t Encoded = ParseInteger(Data, Offset);
136 
137   StringRef Profile;
138   switch (Encoded) {
139   default:  Profile = "Unknown"; break;
140   case 'A': Profile = "Application"; break;
141   case 'R': Profile = "Real-time"; break;
142   case 'M': Profile = "Microcontroller"; break;
143   case 'S': Profile = "Classic"; break;
144   case 0: Profile = "None"; break;
145   }
146 
147   PrintAttribute(Tag, Encoded, Profile);
148 }
149 
ARM_ISA_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)150 void ARMAttributeParser::ARM_ISA_use(AttrType Tag, const uint8_t *Data,
151                                      uint32_t &Offset) {
152   static const char *Strings[] = { "Not Permitted", "Permitted" };
153 
154   uint64_t Value = ParseInteger(Data, Offset);
155   StringRef ValueDesc =
156     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
157   PrintAttribute(Tag, Value, ValueDesc);
158 }
159 
THUMB_ISA_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)160 void ARMAttributeParser::THUMB_ISA_use(AttrType Tag, const uint8_t *Data,
161                                        uint32_t &Offset) {
162   static const char *Strings[] = { "Not Permitted", "Thumb-1", "Thumb-2" };
163 
164   uint64_t Value = ParseInteger(Data, Offset);
165   StringRef ValueDesc =
166     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
167   PrintAttribute(Tag, Value, ValueDesc);
168 }
169 
FP_arch(AttrType Tag,const uint8_t * Data,uint32_t & Offset)170 void ARMAttributeParser::FP_arch(AttrType Tag, const uint8_t *Data,
171                                  uint32_t &Offset) {
172   static const char *Strings[] = {
173     "Not Permitted", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4",
174     "VFPv4-D16", "ARMv8-a FP", "ARMv8-a FP-D16"
175   };
176 
177   uint64_t Value = ParseInteger(Data, Offset);
178   StringRef ValueDesc =
179     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
180   PrintAttribute(Tag, Value, ValueDesc);
181 }
182 
WMMX_arch(AttrType Tag,const uint8_t * Data,uint32_t & Offset)183 void ARMAttributeParser::WMMX_arch(AttrType Tag, const uint8_t *Data,
184                                    uint32_t &Offset) {
185   static const char *Strings[] = { "Not Permitted", "WMMXv1", "WMMXv2" };
186 
187   uint64_t Value = ParseInteger(Data, Offset);
188   StringRef ValueDesc =
189     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
190   PrintAttribute(Tag, Value, ValueDesc);
191 }
192 
Advanced_SIMD_arch(AttrType Tag,const uint8_t * Data,uint32_t & Offset)193 void ARMAttributeParser::Advanced_SIMD_arch(AttrType Tag, const uint8_t *Data,
194                                             uint32_t &Offset) {
195   static const char *Strings[] = {
196     "Not Permitted", "NEONv1", "NEONv2+FMA", "ARMv8-a NEON"
197   };
198 
199   uint64_t Value = ParseInteger(Data, Offset);
200   StringRef ValueDesc =
201     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
202   PrintAttribute(Tag, Value, ValueDesc);
203 }
204 
PCS_config(AttrType Tag,const uint8_t * Data,uint32_t & Offset)205 void ARMAttributeParser::PCS_config(AttrType Tag, const uint8_t *Data,
206                                     uint32_t &Offset) {
207   static const char *Strings[] = {
208     "None", "Bare Platform", "Linux Application", "Linux DSO", "Palm OS 2004",
209     "Reserved (Palm OS)", "Symbian OS 2004", "Reserved (Symbian OS)"
210   };
211 
212   uint64_t Value = ParseInteger(Data, Offset);
213   StringRef ValueDesc =
214     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
215   PrintAttribute(Tag, Value, ValueDesc);
216 }
217 
ABI_PCS_R9_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)218 void ARMAttributeParser::ABI_PCS_R9_use(AttrType Tag, const uint8_t *Data,
219                                         uint32_t &Offset) {
220   static const char *Strings[] = { "v6", "Static Base", "TLS", "Unused" };
221 
222   uint64_t Value = ParseInteger(Data, Offset);
223   StringRef ValueDesc =
224     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
225   PrintAttribute(Tag, Value, ValueDesc);
226 }
227 
ABI_PCS_RW_data(AttrType Tag,const uint8_t * Data,uint32_t & Offset)228 void ARMAttributeParser::ABI_PCS_RW_data(AttrType Tag, const uint8_t *Data,
229                                          uint32_t &Offset) {
230   static const char *Strings[] = {
231     "Absolute", "PC-relative", "SB-relative", "Not Permitted"
232   };
233 
234   uint64_t Value = ParseInteger(Data, Offset);
235   StringRef ValueDesc =
236     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
237   PrintAttribute(Tag, Value, ValueDesc);
238 }
239 
ABI_PCS_RO_data(AttrType Tag,const uint8_t * Data,uint32_t & Offset)240 void ARMAttributeParser::ABI_PCS_RO_data(AttrType Tag, const uint8_t *Data,
241                                          uint32_t &Offset) {
242   static const char *Strings[] = { "Absolute", "PC-relative", "Not Permitted" };
243 
244   uint64_t Value = ParseInteger(Data, Offset);
245   StringRef ValueDesc =
246     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
247   PrintAttribute(Tag, Value, ValueDesc);
248 }
249 
ABI_PCS_GOT_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)250 void ARMAttributeParser::ABI_PCS_GOT_use(AttrType Tag, const uint8_t *Data,
251                                          uint32_t &Offset) {
252   static const char *Strings[] = { "Not Permitted", "Direct", "GOT-Indirect" };
253 
254   uint64_t Value = ParseInteger(Data, Offset);
255   StringRef ValueDesc =
256     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
257   PrintAttribute(Tag, Value, ValueDesc);
258 }
259 
ABI_PCS_wchar_t(AttrType Tag,const uint8_t * Data,uint32_t & Offset)260 void ARMAttributeParser::ABI_PCS_wchar_t(AttrType Tag, const uint8_t *Data,
261                                          uint32_t &Offset) {
262   static const char *Strings[] = {
263     "Not Permitted", "Unknown", "2-byte", "Unknown", "4-byte"
264   };
265 
266   uint64_t Value = ParseInteger(Data, Offset);
267   StringRef ValueDesc =
268     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
269   PrintAttribute(Tag, Value, ValueDesc);
270 }
271 
ABI_FP_rounding(AttrType Tag,const uint8_t * Data,uint32_t & Offset)272 void ARMAttributeParser::ABI_FP_rounding(AttrType Tag, const uint8_t *Data,
273                                          uint32_t &Offset) {
274   static const char *Strings[] = { "IEEE-754", "Runtime" };
275 
276   uint64_t Value = ParseInteger(Data, Offset);
277   StringRef ValueDesc =
278     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
279   PrintAttribute(Tag, Value, ValueDesc);
280 }
281 
ABI_FP_denormal(AttrType Tag,const uint8_t * Data,uint32_t & Offset)282 void ARMAttributeParser::ABI_FP_denormal(AttrType Tag, const uint8_t *Data,
283                                          uint32_t &Offset) {
284   static const char *Strings[] = { "Unsupported", "IEEE-754", "Sign Only" };
285 
286   uint64_t Value = ParseInteger(Data, Offset);
287   StringRef ValueDesc =
288     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
289   PrintAttribute(Tag, Value, ValueDesc);
290 }
291 
ABI_FP_exceptions(AttrType Tag,const uint8_t * Data,uint32_t & Offset)292 void ARMAttributeParser::ABI_FP_exceptions(AttrType Tag, const uint8_t *Data,
293                                            uint32_t &Offset) {
294   static const char *Strings[] = { "Not Permitted", "IEEE-754" };
295 
296   uint64_t Value = ParseInteger(Data, Offset);
297   StringRef ValueDesc =
298     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
299   PrintAttribute(Tag, Value, ValueDesc);
300 }
301 
ABI_FP_user_exceptions(AttrType Tag,const uint8_t * Data,uint32_t & Offset)302 void ARMAttributeParser::ABI_FP_user_exceptions(AttrType Tag,
303                                                 const uint8_t *Data,
304                                                 uint32_t &Offset) {
305   static const char *Strings[] = { "Not Permitted", "IEEE-754" };
306 
307   uint64_t Value = ParseInteger(Data, Offset);
308   StringRef ValueDesc =
309     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
310   PrintAttribute(Tag, Value, ValueDesc);
311 }
312 
ABI_FP_number_model(AttrType Tag,const uint8_t * Data,uint32_t & Offset)313 void ARMAttributeParser::ABI_FP_number_model(AttrType Tag, const uint8_t *Data,
314                                              uint32_t &Offset) {
315   static const char *Strings[] = {
316     "Not Permitted", "Finite Only", "RTABI", "IEEE-754"
317   };
318 
319   uint64_t Value = ParseInteger(Data, Offset);
320   StringRef ValueDesc =
321     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
322   PrintAttribute(Tag, Value, ValueDesc);
323 }
324 
ABI_align_needed(AttrType Tag,const uint8_t * Data,uint32_t & Offset)325 void ARMAttributeParser::ABI_align_needed(AttrType Tag, const uint8_t *Data,
326                                           uint32_t &Offset) {
327   static const char *Strings[] = {
328     "Not Permitted", "8-byte alignment", "4-byte alignment", "Reserved"
329   };
330 
331   uint64_t Value = ParseInteger(Data, Offset);
332 
333   std::string Description;
334   if (Value < array_lengthof(Strings))
335     Description = std::string(Strings[Value]);
336   else if (Value <= 12)
337     Description = std::string("8-byte alignment, ") + utostr(1 << Value)
338                 + std::string("-byte extended alignment");
339   else
340     Description = "Invalid";
341 
342   PrintAttribute(Tag, Value, Description);
343 }
344 
ABI_align_preserved(AttrType Tag,const uint8_t * Data,uint32_t & Offset)345 void ARMAttributeParser::ABI_align_preserved(AttrType Tag, const uint8_t *Data,
346                                              uint32_t &Offset) {
347   static const char *Strings[] = {
348     "Not Required", "8-byte data alignment", "8-byte data and code alignment",
349     "Reserved"
350   };
351 
352   uint64_t Value = ParseInteger(Data, Offset);
353 
354   std::string Description;
355   if (Value < array_lengthof(Strings))
356     Description = std::string(Strings[Value]);
357   else if (Value <= 12)
358     Description = std::string("8-byte stack alignment, ") + utostr(1 << Value)
359                 + std::string("-byte data alignment");
360   else
361     Description = "Invalid";
362 
363   PrintAttribute(Tag, Value, Description);
364 }
365 
ABI_enum_size(AttrType Tag,const uint8_t * Data,uint32_t & Offset)366 void ARMAttributeParser::ABI_enum_size(AttrType Tag, const uint8_t *Data,
367                                        uint32_t &Offset) {
368   static const char *Strings[] = {
369     "Not Permitted", "Packed", "Int32", "External Int32"
370   };
371 
372   uint64_t Value = ParseInteger(Data, Offset);
373   StringRef ValueDesc =
374     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
375   PrintAttribute(Tag, Value, ValueDesc);
376 }
377 
ABI_HardFP_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)378 void ARMAttributeParser::ABI_HardFP_use(AttrType Tag, const uint8_t *Data,
379                                         uint32_t &Offset) {
380   static const char *Strings[] = {
381     "Tag_FP_arch", "Single-Precision", "Reserved", "Tag_FP_arch (deprecated)"
382   };
383 
384   uint64_t Value = ParseInteger(Data, Offset);
385   StringRef ValueDesc =
386     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
387   PrintAttribute(Tag, Value, ValueDesc);
388 }
389 
ABI_VFP_args(AttrType Tag,const uint8_t * Data,uint32_t & Offset)390 void ARMAttributeParser::ABI_VFP_args(AttrType Tag, const uint8_t *Data,
391                                       uint32_t &Offset) {
392   static const char *Strings[] = {
393     "AAPCS", "AAPCS VFP", "Custom", "Not Permitted"
394   };
395 
396   uint64_t Value = ParseInteger(Data, Offset);
397   StringRef ValueDesc =
398     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
399   PrintAttribute(Tag, Value, ValueDesc);
400 }
401 
ABI_WMMX_args(AttrType Tag,const uint8_t * Data,uint32_t & Offset)402 void ARMAttributeParser::ABI_WMMX_args(AttrType Tag, const uint8_t *Data,
403                                        uint32_t &Offset) {
404   static const char *Strings[] = { "AAPCS", "iWMMX", "Custom" };
405 
406   uint64_t Value = ParseInteger(Data, Offset);
407   StringRef ValueDesc =
408     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
409   PrintAttribute(Tag, Value, ValueDesc);
410 }
411 
ABI_optimization_goals(AttrType Tag,const uint8_t * Data,uint32_t & Offset)412 void ARMAttributeParser::ABI_optimization_goals(AttrType Tag,
413                                                 const uint8_t *Data,
414                                                 uint32_t &Offset) {
415   static const char *Strings[] = {
416     "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size", "Debugging",
417     "Best Debugging"
418   };
419 
420   uint64_t Value = ParseInteger(Data, Offset);
421   StringRef ValueDesc =
422     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
423   PrintAttribute(Tag, Value, ValueDesc);
424 }
425 
ABI_FP_optimization_goals(AttrType Tag,const uint8_t * Data,uint32_t & Offset)426 void ARMAttributeParser::ABI_FP_optimization_goals(AttrType Tag,
427                                                    const uint8_t *Data,
428                                                    uint32_t &Offset) {
429   static const char *Strings[] = {
430     "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size", "Accuracy",
431     "Best Accuracy"
432   };
433 
434   uint64_t Value = ParseInteger(Data, Offset);
435   StringRef ValueDesc =
436     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
437   PrintAttribute(Tag, Value, ValueDesc);
438 }
439 
compatibility(AttrType Tag,const uint8_t * Data,uint32_t & Offset)440 void ARMAttributeParser::compatibility(AttrType Tag, const uint8_t *Data,
441                                        uint32_t &Offset) {
442   uint64_t Integer = ParseInteger(Data, Offset);
443   StringRef String = ParseString(Data, Offset);
444 
445   DictScope AS(SW, "Attribute");
446   SW.printNumber("Tag", Tag);
447   SW.startLine() << "Value: " << Integer << ", " << String << '\n';
448   SW.printString("TagName", AttrTypeAsString(Tag, /*TagPrefix*/false));
449   switch (Integer) {
450   case 0:
451     SW.printString("Description", StringRef("No Specific Requirements"));
452     break;
453   case 1:
454     SW.printString("Description", StringRef("AEABI Conformant"));
455     break;
456   default:
457     SW.printString("Description", StringRef("AEABI Non-Conformant"));
458     break;
459   }
460 }
461 
CPU_unaligned_access(AttrType Tag,const uint8_t * Data,uint32_t & Offset)462 void ARMAttributeParser::CPU_unaligned_access(AttrType Tag, const uint8_t *Data,
463                                               uint32_t &Offset) {
464   static const char *Strings[] = { "Not Permitted", "v6-style" };
465 
466   uint64_t Value = ParseInteger(Data, Offset);
467   StringRef ValueDesc =
468     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
469   PrintAttribute(Tag, Value, ValueDesc);
470 }
471 
FP_HP_extension(AttrType Tag,const uint8_t * Data,uint32_t & Offset)472 void ARMAttributeParser::FP_HP_extension(AttrType Tag, const uint8_t *Data,
473                                          uint32_t &Offset) {
474   static const char *Strings[] = { "If Available", "Permitted" };
475 
476   uint64_t Value = ParseInteger(Data, Offset);
477   StringRef ValueDesc =
478     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
479   PrintAttribute(Tag, Value, ValueDesc);
480 }
481 
ABI_FP_16bit_format(AttrType Tag,const uint8_t * Data,uint32_t & Offset)482 void ARMAttributeParser::ABI_FP_16bit_format(AttrType Tag, const uint8_t *Data,
483                                              uint32_t &Offset) {
484   static const char *Strings[] = { "Not Permitted", "IEEE-754", "VFPv3" };
485 
486   uint64_t Value = ParseInteger(Data, Offset);
487   StringRef ValueDesc =
488     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
489   PrintAttribute(Tag, Value, ValueDesc);
490 }
491 
MPextension_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)492 void ARMAttributeParser::MPextension_use(AttrType Tag, const uint8_t *Data,
493                                          uint32_t &Offset) {
494   static const char *Strings[] = { "Not Permitted", "Permitted" };
495 
496   uint64_t Value = ParseInteger(Data, Offset);
497   StringRef ValueDesc =
498     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
499   PrintAttribute(Tag, Value, ValueDesc);
500 }
501 
DIV_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)502 void ARMAttributeParser::DIV_use(AttrType Tag, const uint8_t *Data,
503                                  uint32_t &Offset) {
504   static const char *Strings[] = {
505     "If Available", "Not Permitted", "Permitted"
506   };
507 
508   uint64_t Value = ParseInteger(Data, Offset);
509   StringRef ValueDesc =
510     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
511   PrintAttribute(Tag, Value, ValueDesc);
512 }
513 
T2EE_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)514 void ARMAttributeParser::T2EE_use(AttrType Tag, const uint8_t *Data,
515                                   uint32_t &Offset) {
516   static const char *Strings[] = { "Not Permitted", "Permitted" };
517 
518   uint64_t Value = ParseInteger(Data, Offset);
519   StringRef ValueDesc =
520     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
521   PrintAttribute(Tag, Value, ValueDesc);
522 }
523 
Virtualization_use(AttrType Tag,const uint8_t * Data,uint32_t & Offset)524 void ARMAttributeParser::Virtualization_use(AttrType Tag, const uint8_t *Data,
525                                             uint32_t &Offset) {
526   static const char *Strings[] = {
527     "Not Permitted", "TrustZone", "Virtualization Extensions",
528     "TrustZone + Virtualization Extensions"
529   };
530 
531   uint64_t Value = ParseInteger(Data, Offset);
532   StringRef ValueDesc =
533     (Value < array_lengthof(Strings)) ? Strings[Value] : nullptr;
534   PrintAttribute(Tag, Value, ValueDesc);
535 }
536 
nodefaults(AttrType Tag,const uint8_t * Data,uint32_t & Offset)537 void ARMAttributeParser::nodefaults(AttrType Tag, const uint8_t *Data,
538                                     uint32_t &Offset) {
539   uint64_t Value = ParseInteger(Data, Offset);
540   PrintAttribute(Tag, Value, "Unspecified Tags UNDEFINED");
541 }
542 
ParseIndexList(const uint8_t * Data,uint32_t & Offset,SmallVectorImpl<uint8_t> & IndexList)543 void ARMAttributeParser::ParseIndexList(const uint8_t *Data, uint32_t &Offset,
544                                         SmallVectorImpl<uint8_t> &IndexList) {
545   for (;;) {
546     unsigned Length;
547     uint64_t Value = decodeULEB128(Data + Offset, &Length);
548     Offset = Offset + Length;
549     if (Value == 0)
550       break;
551     IndexList.push_back(Value);
552   }
553 }
554 
ParseAttributeList(const uint8_t * Data,uint32_t & Offset,uint32_t Length)555 void ARMAttributeParser::ParseAttributeList(const uint8_t *Data,
556                                             uint32_t &Offset, uint32_t Length) {
557   while (Offset < Length) {
558     unsigned Length;
559     uint64_t Tag = decodeULEB128(Data + Offset, &Length);
560     Offset += Length;
561 
562     bool Handled = false;
563     for (unsigned AHI = 0, AHE = array_lengthof(DisplayRoutines);
564          AHI != AHE && !Handled; ++AHI) {
565       if (DisplayRoutines[AHI].Attribute == Tag) {
566         (this->*DisplayRoutines[AHI].Routine)(ARMBuildAttrs::AttrType(Tag),
567                                               Data, Offset);
568         Handled = true;
569         break;
570       }
571     }
572     if (!Handled) {
573       if (Tag < 32) {
574         errs() << "unhandled AEABI Tag " << Tag
575                << " (" << ARMBuildAttrs::AttrTypeAsString(Tag) << ")\n";
576         continue;
577       }
578 
579       if (Tag % 2 == 0)
580         IntegerAttribute(ARMBuildAttrs::AttrType(Tag), Data, Offset);
581       else
582         StringAttribute(ARMBuildAttrs::AttrType(Tag), Data, Offset);
583     }
584   }
585 }
586 
ParseSubsection(const uint8_t * Data,uint32_t Length)587 void ARMAttributeParser::ParseSubsection(const uint8_t *Data, uint32_t Length) {
588   uint32_t Offset = sizeof(uint32_t); /* SectionLength */
589 
590   SW.printNumber("SectionLength", Length);
591 
592   const char *VendorName = reinterpret_cast<const char*>(Data + Offset);
593   size_t VendorNameLength = std::strlen(VendorName);
594   SW.printString("Vendor", StringRef(VendorName, VendorNameLength));
595   Offset = Offset + VendorNameLength + 1;
596 
597   if (StringRef(VendorName, VendorNameLength).lower() != "aeabi")
598     return;
599 
600   while (Offset < Length) {
601     /// Tag_File | Tag_Section | Tag_Symbol   uleb128:byte-size
602     uint8_t Tag = Data[Offset];
603     SW.printEnum("Tag", Tag, makeArrayRef(TagNames));
604     Offset = Offset + sizeof(Tag);
605 
606     uint32_t Size =
607       *reinterpret_cast<const support::ulittle32_t*>(Data + Offset);
608     SW.printNumber("Size", Size);
609     Offset = Offset + sizeof(Size);
610 
611     if (Size > Length) {
612       errs() << "subsection length greater than section length\n";
613       return;
614     }
615 
616     StringRef ScopeName, IndexName;
617     SmallVector<uint8_t, 8> Indicies;
618     switch (Tag) {
619     case ARMBuildAttrs::File:
620       ScopeName = "FileAttributes";
621       break;
622     case ARMBuildAttrs::Section:
623       ScopeName = "SectionAttributes";
624       IndexName = "Sections";
625       ParseIndexList(Data, Offset, Indicies);
626       break;
627     case ARMBuildAttrs::Symbol:
628       ScopeName = "SymbolAttributes";
629       IndexName = "Symbols";
630       ParseIndexList(Data, Offset, Indicies);
631       break;
632     default:
633       errs() << "unrecognised tag: 0x" << utohexstr(Tag) << '\n';
634       return;
635     }
636 
637     DictScope ASS(SW, ScopeName);
638 
639     if (!Indicies.empty())
640       SW.printList(IndexName, Indicies);
641 
642     ParseAttributeList(Data, Offset, Length);
643   }
644 }
645 
Parse(ArrayRef<uint8_t> Section)646 void ARMAttributeParser::Parse(ArrayRef<uint8_t> Section) {
647   size_t Offset = 1;
648   unsigned SectionNumber = 0;
649 
650   while (Offset < Section.size()) {
651     uint32_t SectionLength =
652       *reinterpret_cast<const support::ulittle32_t*>(Section.data() + Offset);
653 
654     SW.startLine() << "Section " << ++SectionNumber << " {\n";
655     SW.indent();
656 
657     ParseSubsection(Section.data() + Offset, SectionLength);
658     Offset = Offset + SectionLength;
659 
660     SW.unindent();
661     SW.startLine() << "}\n";
662   }
663 }
664 }
665 
666