DynamicLoader.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/Assertions.h>
  29. #include <AK/OwnPtr.h>
  30. #include <AK/RefCounted.h>
  31. #include <AK/String.h>
  32. #include <LibC/elf.h>
  33. #include <LibELF/DynamicObject.h>
  34. #include <LibELF/Image.h>
  35. #include <sys/mman.h>
  36. namespace ELF {
  37. class LoadedSegment {
  38. public:
  39. LoadedSegment(VirtualAddress address, size_t size)
  40. : m_address(address)
  41. , m_size(size)
  42. {
  43. }
  44. VirtualAddress address() const { return m_address; }
  45. size_t size() const { return m_size; }
  46. private:
  47. VirtualAddress m_address;
  48. size_t m_size;
  49. };
  50. enum class ShouldInitializeWeak {
  51. Yes,
  52. No
  53. };
  54. class DynamicLoader : public RefCounted<DynamicLoader> {
  55. public:
  56. static RefPtr<DynamicLoader> try_create(int fd, String filename);
  57. ~DynamicLoader();
  58. const String& filename() const { return m_filename; }
  59. bool is_valid() const { return m_valid; }
  60. // Load a full ELF image from file into the current process and create an DynamicObject
  61. // from the SHT_DYNAMIC in the file.
  62. // Note that the DynamicObject will not be linked yet. Callers are responsible for calling link() to finish it.
  63. RefPtr<DynamicObject> map();
  64. bool link(unsigned flags, size_t total_tls_size);
  65. // Stage 2 of loading: dynamic object loading and primary relocations
  66. bool load_stage_2(unsigned flags, size_t total_tls_size);
  67. // Stage 3 of loading: lazy relocations
  68. RefPtr<DynamicObject> load_stage_3(unsigned flags, size_t total_tls_size);
  69. // Stage 4 of loading: initializers
  70. void load_stage_4();
  71. // Intended for use by dlsym or other internal methods
  72. void* symbol_for_name(const StringView&);
  73. void set_tls_offset(size_t offset) { m_tls_offset = offset; };
  74. size_t tls_size() const { return m_tls_size; }
  75. size_t tls_offset() const { return m_tls_offset; }
  76. const ELF::Image& image() const { return m_elf_image; }
  77. template<typename F>
  78. void for_each_needed_library(F) const;
  79. VirtualAddress base_address() const { return m_base_address; }
  80. const Vector<LoadedSegment> text_segments() const { return m_text_segments; }
  81. bool is_dynamic() const { return m_elf_image.is_dynamic(); }
  82. static Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&);
  83. private:
  84. DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
  85. class ProgramHeaderRegion {
  86. public:
  87. void set_program_header(const Elf32_Phdr& header) { m_program_header = header; }
  88. // Information from ELF Program header
  89. u32 type() const { return m_program_header.p_type; }
  90. u32 flags() const { return m_program_header.p_flags; }
  91. u32 offset() const { return m_program_header.p_offset; }
  92. VirtualAddress desired_load_address() const { return VirtualAddress(m_program_header.p_vaddr); }
  93. u32 size_in_memory() const { return m_program_header.p_memsz; }
  94. u32 size_in_image() const { return m_program_header.p_filesz; }
  95. u32 alignment() const { return m_program_header.p_align; }
  96. bool is_readable() const { return flags() & PF_R; }
  97. bool is_writable() const { return flags() & PF_W; }
  98. bool is_executable() const { return flags() & PF_X; }
  99. bool is_tls_template() const { return type() == PT_TLS; }
  100. bool is_load() const { return type() == PT_LOAD; }
  101. bool is_dynamic() const { return type() == PT_DYNAMIC; }
  102. bool is_relro() const { return type() == PT_GNU_RELRO; }
  103. private:
  104. Elf32_Phdr m_program_header; // Explicitly a copy of the PHDR in the image
  105. };
  106. const DynamicObject& dynamic_object() const;
  107. // Stage 1
  108. void load_program_headers();
  109. // Stage 2
  110. void do_main_relocations(size_t total_tls_size);
  111. // Stage 3
  112. void do_lazy_relocations(size_t total_tls_size);
  113. void setup_plt_trampoline();
  114. // Stage 4
  115. void call_object_init_functions();
  116. bool validate();
  117. enum class RelocationResult : uint8_t {
  118. Failed = 0,
  119. Success = 1,
  120. ResolveLater = 2,
  121. };
  122. RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&, ShouldInitializeWeak should_initialize_weak);
  123. size_t calculate_tls_size() const;
  124. String m_filename;
  125. String m_program_interpreter;
  126. size_t m_file_size { 0 };
  127. int m_image_fd { -1 };
  128. void* m_file_data { nullptr };
  129. ELF::Image m_elf_image;
  130. bool m_valid { true };
  131. RefPtr<DynamicObject> m_dynamic_object;
  132. VirtualAddress m_base_address;
  133. Vector<LoadedSegment> m_text_segments;
  134. VirtualAddress m_relro_segment_address;
  135. size_t m_relro_segment_size { 0 };
  136. VirtualAddress m_dynamic_section_address;
  137. size_t m_tls_offset { 0 };
  138. size_t m_tls_size { 0 };
  139. Vector<DynamicObject::Relocation> m_unresolved_relocations;
  140. mutable RefPtr<DynamicObject> m_cached_dynamic_object;
  141. };
  142. template<typename F>
  143. void DynamicLoader::for_each_needed_library(F func) const
  144. {
  145. dynamic_object().for_each_needed_library(move(func));
  146. }
  147. } // end namespace ELF