ELFAttributeParser.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ELF AttributeParser.h - ELF Attribute Parser -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_ELFATTRIBUTEPARSER_H
  14. #define LLVM_SUPPORT_ELFATTRIBUTEPARSER_H
  15. #include "ELFAttributes.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/Support/DataExtractor.h"
  18. #include "llvm/Support/Endian.h"
  19. #include "llvm/Support/Error.h"
  20. #include <optional>
  21. #include <unordered_map>
  22. namespace llvm {
  23. class StringRef;
  24. class ScopedPrinter;
  25. class ELFAttributeParser {
  26. StringRef vendor;
  27. std::unordered_map<unsigned, unsigned> attributes;
  28. std::unordered_map<unsigned, StringRef> attributesStr;
  29. virtual Error handler(uint64_t tag, bool &handled) = 0;
  30. protected:
  31. ScopedPrinter *sw;
  32. TagNameMap tagToStringMap;
  33. DataExtractor de{ArrayRef<uint8_t>{}, true, 0};
  34. DataExtractor::Cursor cursor{0};
  35. void printAttribute(unsigned tag, unsigned value, StringRef valueDesc);
  36. Error parseStringAttribute(const char *name, unsigned tag,
  37. ArrayRef<const char *> strings);
  38. Error parseAttributeList(uint32_t length);
  39. void parseIndexList(SmallVectorImpl<uint8_t> &indexList);
  40. Error parseSubsection(uint32_t length);
  41. void setAttributeString(unsigned tag, StringRef value) {
  42. attributesStr.emplace(tag, value);
  43. }
  44. public:
  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. : vendor(vendor), sw(sw), tagToStringMap(tagNameMap) {}
  50. ELFAttributeParser(TagNameMap tagNameMap, StringRef vendor)
  51. : vendor(vendor), sw(nullptr), tagToStringMap(tagNameMap) {}
  52. Error parse(ArrayRef<uint8_t> section, support::endianness endian);
  53. std::optional<unsigned> getAttributeValue(unsigned tag) const {
  54. auto I = attributes.find(tag);
  55. if (I == attributes.end())
  56. return std::nullopt;
  57. return I->second;
  58. }
  59. std::optional<StringRef> getAttributeString(unsigned tag) const {
  60. auto I = attributesStr.find(tag);
  61. if (I == attributesStr.end())
  62. return std::nullopt;
  63. return I->second;
  64. }
  65. };
  66. } // namespace llvm
  67. #endif
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic pop
  70. #endif