File indexing completed on 2026-05-10 08:44:29
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_SUPPORT_ELFATTRIBUTEPARSER_H
0010 #define LLVM_SUPPORT_ELFATTRIBUTEPARSER_H
0011
0012 #include "ELFAttributes.h"
0013 #include "llvm/ADT/ArrayRef.h"
0014 #include "llvm/Support/DataExtractor.h"
0015 #include "llvm/Support/Error.h"
0016
0017 #include <optional>
0018 #include <unordered_map>
0019
0020 namespace llvm {
0021 class StringRef;
0022 class ScopedPrinter;
0023
0024 class ELFAttributeParser {
0025 StringRef vendor;
0026 std::unordered_map<unsigned, unsigned> attributes;
0027 std::unordered_map<unsigned, StringRef> attributesStr;
0028
0029 virtual Error handler(uint64_t tag, bool &handled) = 0;
0030
0031 protected:
0032 ScopedPrinter *sw;
0033 TagNameMap tagToStringMap;
0034 DataExtractor de{ArrayRef<uint8_t>{}, true, 0};
0035 DataExtractor::Cursor cursor{0};
0036
0037 void printAttribute(unsigned tag, unsigned value, StringRef valueDesc);
0038
0039 Error parseStringAttribute(const char *name, unsigned tag,
0040 ArrayRef<const char *> strings);
0041 Error parseAttributeList(uint32_t length);
0042 void parseIndexList(SmallVectorImpl<uint8_t> &indexList);
0043 Error parseSubsection(uint32_t length);
0044
0045 void setAttributeString(unsigned tag, StringRef value) {
0046 attributesStr.emplace(tag, value);
0047 }
0048
0049 public:
0050 virtual ~ELFAttributeParser() { static_cast<void>(!cursor.takeError()); }
0051 Error integerAttribute(unsigned tag);
0052 Error stringAttribute(unsigned tag);
0053
0054 ELFAttributeParser(ScopedPrinter *sw, TagNameMap tagNameMap, StringRef vendor)
0055 : vendor(vendor), sw(sw), tagToStringMap(tagNameMap) {}
0056
0057 ELFAttributeParser(TagNameMap tagNameMap, StringRef vendor)
0058 : vendor(vendor), sw(nullptr), tagToStringMap(tagNameMap) {}
0059
0060 Error parse(ArrayRef<uint8_t> section, llvm::endianness endian);
0061
0062 std::optional<unsigned> getAttributeValue(unsigned tag) const {
0063 auto I = attributes.find(tag);
0064 if (I == attributes.end())
0065 return std::nullopt;
0066 return I->second;
0067 }
0068 std::optional<StringRef> getAttributeString(unsigned tag) const {
0069 auto I = attributesStr.find(tag);
0070 if (I == attributesStr.end())
0071 return std::nullopt;
0072 return I->second;
0073 }
0074 };
0075
0076 }
0077 #endif