BaseElf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef BaseElf_h
  5. #define BaseElf_h
  6. #include "ElfLoader.h"
  7. #include "Elfxx.h"
  8. /**
  9. * Base class for ELF libraries. This class includes things that will be
  10. * common between SystemElfs and CustomElfs.
  11. */
  12. class BaseElf: public LibHandle
  13. {
  14. public:
  15. /**
  16. * Hash function for symbol lookup, as defined in ELF standard for System V.
  17. */
  18. static unsigned long Hash(const char *symbol);
  19. /**
  20. * Returns the address corresponding to the given symbol name (with a
  21. * pre-computed hash).
  22. */
  23. void *GetSymbolPtr(const char *symbol, unsigned long hash) const;
  24. /**
  25. * Returns a pointer to the Elf Symbol in the Dynamic Symbol table
  26. * corresponding to the given symbol name (with a pre-computed hash).
  27. */
  28. const Elf::Sym *GetSymbol(const char *symbol, unsigned long hash) const;
  29. BaseElf(const char *path, Mappable *mappable = nullptr)
  30. : LibHandle(path)
  31. , mappable(mappable)
  32. {
  33. }
  34. protected:
  35. /**
  36. * Inherited from LibHandle. Those are temporary and are not supposed to
  37. * be used.
  38. */
  39. virtual void *GetSymbolPtr(const char *symbol) const;
  40. virtual bool Contains(void *addr) const;
  41. virtual void *GetBase() const { return GetPtr(0); }
  42. #ifdef __ARM_EABI__
  43. virtual const void *FindExidx(int *pcount) const;
  44. #endif
  45. virtual Mappable *GetMappable() const { return NULL; };
  46. public:
  47. /* private: */
  48. /**
  49. * Returns a pointer relative to the base address where the library is
  50. * loaded.
  51. */
  52. void *GetPtr(const Elf::Addr offset) const
  53. {
  54. if (reinterpret_cast<void *>(offset) > base)
  55. return reinterpret_cast<void *>(offset);
  56. return base + offset;
  57. }
  58. /**
  59. * Like the above, but returns a typed (const) pointer
  60. */
  61. template <typename T>
  62. const T *GetPtr(const Elf::Addr offset) const
  63. {
  64. if (reinterpret_cast<void *>(offset) > base)
  65. return reinterpret_cast<const T *>(offset);
  66. return reinterpret_cast<const T *>(base + offset);
  67. }
  68. /* Appropriated Mappable */
  69. /* /!\ we rely on this being nullptr for BaseElf instances, but not
  70. * CustomElf instances. */
  71. RefPtr<Mappable> mappable;
  72. /* Base address where the library is loaded */
  73. MappedPtr base;
  74. /* Buckets and chains for the System V symbol hash table */
  75. Array<Elf::Word> buckets;
  76. UnsizedArray<Elf::Word> chains;
  77. /* protected: */
  78. /* String table */
  79. Elf::Strtab strtab;
  80. /* Symbol table */
  81. UnsizedArray<Elf::Sym> symtab;
  82. #ifdef __ARM_EABI__
  83. /* ARM.exidx information used by FindExidx */
  84. Array<uint32_t[2]> arm_exidx;
  85. #endif
  86. };
  87. /**
  88. * Class for ELF libraries that already loaded in memory.
  89. */
  90. class LoadedElf: public BaseElf
  91. {
  92. public:
  93. /**
  94. * Returns a LoadedElf corresponding to the already loaded ELF
  95. * at the given base address.
  96. */
  97. static already_AddRefed<LibHandle> Create(const char *path,
  98. void *base_addr);
  99. private:
  100. LoadedElf(const char *path)
  101. : BaseElf(path) { }
  102. ~LoadedElf()
  103. {
  104. /* Avoid base's destructor unmapping something that doesn't actually
  105. * belong to it. */
  106. base.release();
  107. ElfLoader::Singleton.Forget(this);
  108. }
  109. /**
  110. * Initializes the library according to information found in the given
  111. * PT_DYNAMIC header.
  112. * Returns whether this succeeded or failed.
  113. */
  114. bool InitDyn(const Elf::Phdr *pt_dyn);
  115. };
  116. #endif /* BaseElf_h */