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_ELFCOMPACTATTRPARSER_H 10 #define LLVM_SUPPORT_ELFCOMPACTATTRPARSER_H 11 12 #include "llvm/ADT/ArrayRef.h" 13 #include "llvm/Support/Compiler.h" 14 #include "llvm/Support/DataExtractor.h" 15 #include "llvm/Support/ELFAttributeParser.h" 16 #include "llvm/Support/ELFAttributes.h" 17 #include "llvm/Support/Error.h" 18 19 #include <optional> 20 #include <unordered_map> 21 22 namespace llvm { 23 class StringRef; 24 class ScopedPrinter; 25 26 class LLVM_ABI ELFCompactAttrParser : public ELFAttributeParser { 27 StringRef vendor; 28 std::unordered_map<unsigned, unsigned> attributes; 29 std::unordered_map<unsigned, StringRef> attributesStr; 30 31 virtual Error handler(uint64_t tag, bool &handled) = 0; 32 33 protected: 34 ScopedPrinter *sw; 35 TagNameMap tagToStringMap; 36 DataExtractor de{ArrayRef<uint8_t>{}, true, 0}; 37 DataExtractor::Cursor cursor{0}; 38 39 void printAttribute(unsigned tag, unsigned value, StringRef valueDesc); 40 41 Error parseStringAttribute(const char *name, unsigned tag, 42 ArrayRef<const char *> strings); 43 Error parseAttributeList(uint32_t length); 44 void parseIndexList(SmallVectorImpl<uint8_t> &indexList); 45 Error parseSubsection(uint32_t length); 46 setAttributeString(unsigned tag,StringRef value)47 void setAttributeString(unsigned tag, StringRef value) { 48 attributesStr.emplace(tag, value); 49 } 50 51 public: ~ELFCompactAttrParser()52 virtual ~ELFCompactAttrParser() { static_cast<void>(!cursor.takeError()); } 53 Error integerAttribute(unsigned tag); 54 Error stringAttribute(unsigned tag); 55 ELFCompactAttrParser(ScopedPrinter * sw,TagNameMap tagNameMap,StringRef vendor)56 ELFCompactAttrParser(ScopedPrinter *sw, TagNameMap tagNameMap, 57 StringRef vendor) 58 : vendor(vendor), sw(sw), tagToStringMap(tagNameMap) {} ELFCompactAttrParser(TagNameMap tagNameMap,StringRef vendor)59 ELFCompactAttrParser(TagNameMap tagNameMap, StringRef vendor) 60 : vendor(vendor), sw(nullptr), tagToStringMap(tagNameMap) {} 61 62 Error parse(ArrayRef<uint8_t> section, llvm::endianness endian) override; 63 getAttributeValue(unsigned tag)64 std::optional<unsigned> getAttributeValue(unsigned tag) const override { 65 auto I = attributes.find(tag); 66 if (I == attributes.end()) 67 return std::nullopt; 68 return I->second; 69 } 70 std::optional<unsigned> getAttributeValue(StringRef buildAttributeSubsectionName,unsigned tag)71 getAttributeValue(StringRef buildAttributeSubsectionName, 72 unsigned tag) const override { 73 assert("" == buildAttributeSubsectionName && 74 "buildAttributeSubsectionName must be an empty string"); 75 return getAttributeValue(tag); 76 } getAttributeString(unsigned tag)77 std::optional<StringRef> getAttributeString(unsigned tag) const override { 78 auto I = attributesStr.find(tag); 79 if (I == attributesStr.end()) 80 return std::nullopt; 81 return I->second; 82 } 83 std::optional<StringRef> getAttributeString(StringRef buildAttributeSubsectionName,unsigned tag)84 getAttributeString(StringRef buildAttributeSubsectionName, 85 unsigned tag) const override { 86 assert("" == buildAttributeSubsectionName && 87 "buildAttributeSubsectionName must be an empty string"); 88 return getAttributeString(tag); 89 } 90 }; 91 92 } // namespace llvm 93 #endif // LLVM_SUPPORT_ELFCOMPACTATTRPARSER_H 94