CustomElf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 CustomElf_h
  5. #define CustomElf_h
  6. #include "ElfLoader.h"
  7. #include "BaseElf.h"
  8. #include "Logging.h"
  9. #include "Elfxx.h"
  10. /**
  11. * Library Handle class for ELF libraries we don't let the system linker
  12. * handle.
  13. */
  14. class CustomElf: public BaseElf, private ElfLoader::link_map
  15. {
  16. friend class ElfLoader;
  17. friend class SEGVHandler;
  18. public:
  19. /**
  20. * Returns a new CustomElf using the given file descriptor to map ELF
  21. * content. The file descriptor ownership is stolen, and it will be closed
  22. * in CustomElf's destructor if an instance is created, or by the Load
  23. * method otherwise. The path corresponds to the file descriptor, and flags
  24. * are the same kind of flags that would be given to dlopen(), though
  25. * currently, none are supported and the behaviour is more or less that of
  26. * RTLD_GLOBAL | RTLD_BIND_NOW.
  27. */
  28. static already_AddRefed<LibHandle> Load(Mappable *mappable,
  29. const char *path, int flags);
  30. /**
  31. * Inherited from LibHandle/BaseElf
  32. */
  33. virtual ~CustomElf();
  34. protected:
  35. virtual Mappable *GetMappable() const;
  36. public:
  37. /**
  38. * Shows some stats about the Mappable instance. The when argument is to be
  39. * used by the caller to give an identifier of the when the stats call is
  40. * made.
  41. */
  42. virtual void stats(const char *when) const;
  43. /**
  44. * Returns the instance, casted as BaseElf. (short of a better way to do
  45. * this without RTTI)
  46. */
  47. virtual BaseElf *AsBaseElf() { return this; }
  48. private:
  49. /**
  50. * Scan dependent libraries to find the address corresponding to the
  51. * given symbol name. This is used to find symbols that are undefined
  52. * in the Elf object.
  53. */
  54. void *GetSymbolPtrInDeps(const char *symbol) const;
  55. /**
  56. * Private constructor
  57. */
  58. CustomElf(Mappable *mappable, const char *path)
  59. : BaseElf(path, mappable)
  60. , link_map()
  61. , init(0)
  62. , fini(0)
  63. , initialized(false)
  64. , has_text_relocs(false)
  65. { }
  66. /**
  67. * Loads an Elf segment defined by the given PT_LOAD header.
  68. * Returns whether this succeeded or failed.
  69. */
  70. bool LoadSegment(const Elf::Phdr *pt_load) const;
  71. /**
  72. * Initializes the library according to information found in the given
  73. * PT_DYNAMIC header.
  74. * Returns whether this succeeded or failed.
  75. */
  76. bool InitDyn(const Elf::Phdr *pt_dyn);
  77. /**
  78. * Apply .rel.dyn/.rela.dyn relocations.
  79. * Returns whether this succeeded or failed.
  80. */
  81. bool Relocate();
  82. /**
  83. * Apply .rel.plt/.rela.plt relocations.
  84. * Returns whether this succeeded or failed.
  85. */
  86. bool RelocateJumps();
  87. /**
  88. * Call initialization functions (.init/.init_array)
  89. * Returns true;
  90. */
  91. bool CallInit();
  92. /**
  93. * Call destructor functions (.fini_array/.fini)
  94. * Returns whether this succeeded or failed.
  95. */
  96. void CallFini();
  97. /**
  98. * Call a function given a pointer to its location.
  99. */
  100. void CallFunction(void *ptr) const
  101. {
  102. /* C++ doesn't allow direct conversion between pointer-to-object
  103. * and pointer-to-function. */
  104. union {
  105. void *ptr;
  106. void (*func)(void);
  107. } f;
  108. f.ptr = ptr;
  109. DEBUG_LOG("%s: Calling function @%p", GetPath(), ptr);
  110. f.func();
  111. }
  112. /**
  113. * Call a function given a an address relative to the library base
  114. */
  115. void CallFunction(Elf::Addr addr) const
  116. {
  117. return CallFunction(GetPtr(addr));
  118. }
  119. /* List of dependent libraries */
  120. std::vector<RefPtr<LibHandle> > dependencies;
  121. /* List of .rel.dyn/.rela.dyn relocations */
  122. Array<Elf::Reloc> relocations;
  123. /* List of .rel.plt/.rela.plt relocation */
  124. Array<Elf::Reloc> jumprels;
  125. /* Relative address of the initialization and destruction functions
  126. * (.init/.fini) */
  127. Elf::Addr init, fini;
  128. /* List of initialization and destruction functions
  129. * (.init_array/.fini_array) */
  130. Array<void *> init_array, fini_array;
  131. bool initialized;
  132. bool has_text_relocs;
  133. };
  134. #endif /* CustomElf_h */