1 //===- ELF AttributeParser.h - ELF Attribute Parser -------------*- 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 #ifndef LLVM_SUPPORT_ELFATTRIBUTEPARSER_H 10 #define LLVM_SUPPORT_ELFATTRIBUTEPARSER_H 11 12 #include "ELFAttributes.h" 13 #include "ScopedPrinter.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/Support/DataExtractor.h" 16 #include "llvm/Support/Error.h" 17 18 #include <unordered_map> 19 20 namespace llvm { 21 class StringRef; 22 23 class ELFAttributeParser { 24 StringRef vendor; 25 std::unordered_map<unsigned, unsigned> attributes; 26 std::unordered_map<unsigned, StringRef> attributesStr; 27 28 virtual Error handler(uint64_t tag, bool &handled) = 0; 29 30 protected: 31 ScopedPrinter *sw; 32 TagNameMap tagToStringMap; 33 DataExtractor de{ArrayRef<uint8_t>{}, true, 0}; 34 DataExtractor::Cursor cursor{0}; 35 36 void printAttribute(unsigned tag, unsigned value, StringRef valueDesc); 37 38 Error parseStringAttribute(const char *name, unsigned tag, 39 ArrayRef<const char *> strings); 40 Error parseAttributeList(uint32_t length); 41 void parseIndexList(SmallVectorImpl<uint8_t> &indexList); 42 Error parseSubsection(uint32_t length); 43 44 public: ~ELFAttributeParser()45 virtual ~ELFAttributeParser() { static_cast<void>(!cursor.takeError()); } 46 Error integerAttribute(unsigned tag); 47 Error stringAttribute(unsigned tag); 48 ELFAttributeParser(ScopedPrinter * sw,TagNameMap tagNameMap,StringRef vendor)49 ELFAttributeParser(ScopedPrinter *sw, TagNameMap tagNameMap, StringRef vendor) 50 : vendor(vendor), sw(sw), tagToStringMap(tagNameMap) {} 51 ELFAttributeParser(TagNameMap tagNameMap,StringRef vendor)52 ELFAttributeParser(TagNameMap tagNameMap, StringRef vendor) 53 : vendor(vendor), sw(nullptr), tagToStringMap(tagNameMap) {} 54 55 Error parse(ArrayRef<uint8_t> section, support::endianness endian); 56 getAttributeValue(unsigned tag)57 Optional<unsigned> getAttributeValue(unsigned tag) const { 58 auto I = attributes.find(tag); 59 if (I == attributes.end()) 60 return None; 61 return I->second; 62 } getAttributeString(unsigned tag)63 Optional<StringRef> getAttributeString(unsigned tag) const { 64 auto I = attributesStr.find(tag); 65 if (I == attributesStr.end()) 66 return None; 67 return I->second; 68 } 69 }; 70 71 } // namespace llvm 72 #endif 73