DynamicLoader.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  5. * Copyright (c) 2022-2023, Daniel Bertalan <dani@danielbertalan.dev>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Debug.h>
  10. #include <AK/Optional.h>
  11. #include <AK/QuickSort.h>
  12. #include <AK/StringBuilder.h>
  13. #include <LibELF/Arch/GenericDynamicRelocationType.h>
  14. #include <LibELF/Arch/tls.h>
  15. #include <LibELF/DynamicLinker.h>
  16. #include <LibELF/DynamicLoader.h>
  17. #include <LibELF/Hashes.h>
  18. #include <LibELF/Validation.h>
  19. #include <assert.h>
  20. #include <bits/dlfcn_integration.h>
  21. #include <dlfcn.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/mman.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #ifndef AK_OS_SERENITY
  30. static void* mmap_with_name(void* addr, size_t length, int prot, int flags, int fd, off_t offset, char const*)
  31. {
  32. return mmap(addr, length, prot, flags, fd, offset);
  33. }
  34. # define MAP_RANDOMIZED 0
  35. #endif
  36. #if ARCH(AARCH64)
  37. # define HAS_TLSDESC_SUPPORT
  38. extern "C" {
  39. void* __tlsdesc_static(void*);
  40. }
  41. #endif
  42. namespace ELF {
  43. Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> DynamicLoader::try_create(int fd, ByteString filepath)
  44. {
  45. VERIFY(filepath.starts_with('/'));
  46. struct stat stat;
  47. if (fstat(fd, &stat) < 0) {
  48. return DlErrorMessage { "DynamicLoader::try_create fstat" };
  49. }
  50. VERIFY(stat.st_size >= 0);
  51. auto size = static_cast<size_t>(stat.st_size);
  52. if (size < sizeof(Elf_Ehdr))
  53. return DlErrorMessage { ByteString::formatted("File {} has invalid ELF header", filepath) };
  54. ByteString file_mmap_name = ByteString::formatted("ELF_DYN: {}", filepath);
  55. auto* data = mmap_with_name(nullptr, size, PROT_READ, MAP_SHARED, fd, 0, file_mmap_name.characters());
  56. if (data == MAP_FAILED) {
  57. return DlErrorMessage { "DynamicLoader::try_create mmap" };
  58. }
  59. auto loader = adopt_ref(*new DynamicLoader(fd, move(filepath), data, size));
  60. if (!loader->is_valid())
  61. return DlErrorMessage { "ELF image validation failed" };
  62. return loader;
  63. }
  64. DynamicLoader::DynamicLoader(int fd, ByteString filepath, void* data, size_t size)
  65. : m_filepath(move(filepath))
  66. , m_file_size(size)
  67. , m_image_fd(fd)
  68. , m_file_data(data)
  69. {
  70. m_elf_image = adopt_own(*new ELF::Image((u8*)m_file_data, m_file_size));
  71. m_valid = validate();
  72. if (m_valid)
  73. find_tls_size_and_alignment();
  74. else
  75. dbgln("Image validation failed for file {}", m_filepath);
  76. }
  77. DynamicLoader::~DynamicLoader()
  78. {
  79. if (munmap(m_file_data, m_file_size) < 0) {
  80. perror("munmap");
  81. VERIFY_NOT_REACHED();
  82. }
  83. if (close(m_image_fd) < 0) {
  84. perror("close");
  85. VERIFY_NOT_REACHED();
  86. }
  87. }
  88. DynamicObject const& DynamicLoader::dynamic_object() const
  89. {
  90. if (!m_cached_dynamic_object) {
  91. VirtualAddress dynamic_section_address;
  92. image().for_each_program_header([&dynamic_section_address](auto program_header) {
  93. if (program_header.type() == PT_DYNAMIC) {
  94. dynamic_section_address = VirtualAddress(program_header.raw_data());
  95. }
  96. });
  97. VERIFY(!dynamic_section_address.is_null());
  98. m_cached_dynamic_object = ELF::DynamicObject::create(m_filepath, VirtualAddress(image().base_address()), dynamic_section_address);
  99. }
  100. return *m_cached_dynamic_object;
  101. }
  102. void DynamicLoader::find_tls_size_and_alignment()
  103. {
  104. image().for_each_program_header([this](auto program_header) {
  105. if (program_header.type() == PT_TLS) {
  106. m_tls_size_of_current_object = program_header.size_in_memory();
  107. auto alignment = program_header.alignment();
  108. VERIFY(!alignment || is_power_of_two(alignment));
  109. m_tls_alignment_of_current_object = alignment > 1 ? alignment : 0; // No need to reserve extra space for single byte alignment
  110. return IterationDecision::Break;
  111. }
  112. return IterationDecision::Continue;
  113. });
  114. }
  115. bool DynamicLoader::validate()
  116. {
  117. if (!image().is_valid())
  118. return false;
  119. auto* elf_header = (Elf_Ehdr*)m_file_data;
  120. if (!validate_elf_header(*elf_header, m_file_size))
  121. return false;
  122. auto result_or_error = validate_program_headers(*elf_header, m_file_size, { m_file_data, m_file_size });
  123. if (result_or_error.is_error() || !result_or_error.value())
  124. return false;
  125. return true;
  126. }
  127. RefPtr<DynamicObject> DynamicLoader::map()
  128. {
  129. if (m_dynamic_object) {
  130. // Already mapped.
  131. return nullptr;
  132. }
  133. if (!m_valid) {
  134. dbgln("DynamicLoader::map failed: image is invalid");
  135. return nullptr;
  136. }
  137. load_program_headers();
  138. VERIFY(!m_base_address.is_null());
  139. m_dynamic_object = DynamicObject::create(m_filepath, m_base_address, m_dynamic_section_address);
  140. m_dynamic_object->set_tls_offset(m_tls_offset);
  141. m_dynamic_object->set_tls_size(m_tls_size_of_current_object);
  142. return m_dynamic_object;
  143. }
  144. bool DynamicLoader::link(unsigned flags)
  145. {
  146. return load_stage_2(flags);
  147. }
  148. bool DynamicLoader::load_stage_2(unsigned flags)
  149. {
  150. VERIFY(flags & RTLD_GLOBAL);
  151. if (m_dynamic_object->has_text_relocations()) {
  152. dbgln("\033[33mWarning:\033[0m Dynamic object {} has text relocations", m_dynamic_object->filepath());
  153. for (auto& text_segment : m_text_segments) {
  154. VERIFY(text_segment.address().get() != 0);
  155. #ifndef AK_OS_MACOS
  156. // Remap this text region as private.
  157. if (mremap(text_segment.address().as_ptr(), text_segment.size(), text_segment.size(), MAP_PRIVATE) == MAP_FAILED) {
  158. perror("mremap .text: MAP_PRIVATE");
  159. return false;
  160. }
  161. #endif
  162. if (0 > mprotect(text_segment.address().as_ptr(), text_segment.size(), PROT_READ | PROT_WRITE)) {
  163. perror("mprotect .text: PROT_READ | PROT_WRITE"); // FIXME: dlerror?
  164. return false;
  165. }
  166. }
  167. } else {
  168. // .text needs to be executable while we process relocations because it might contain IFUNC resolvers.
  169. // We don't allow IFUNC resolvers in objects with textrels.
  170. for (auto& text_segment : m_text_segments) {
  171. if (mprotect(text_segment.address().as_ptr(), text_segment.size(), PROT_READ | PROT_EXEC) < 0) {
  172. perror("mprotect .text: PROT_READ | PROT_EXEC");
  173. return false;
  174. }
  175. }
  176. }
  177. do_main_relocations();
  178. return true;
  179. }
  180. void DynamicLoader::do_main_relocations()
  181. {
  182. do_relr_relocations();
  183. Optional<DynamicLoader::CachedLookupResult> cached_result;
  184. m_dynamic_object->relocation_section().for_each_relocation([&](DynamicObject::Relocation const& relocation) {
  185. switch (do_direct_relocation(relocation, cached_result, ShouldInitializeWeak::No, ShouldCallIfuncResolver::No)) {
  186. case RelocationResult::Failed:
  187. dbgln("Loader.so: {} unresolved symbol '{}'", m_filepath, relocation.symbol().name());
  188. VERIFY_NOT_REACHED();
  189. case RelocationResult::ResolveLater:
  190. m_unresolved_relocations.append(relocation);
  191. break;
  192. case RelocationResult::CallIfuncResolver:
  193. m_direct_ifunc_relocations.append(relocation);
  194. break;
  195. case RelocationResult::Success:
  196. break;
  197. }
  198. });
  199. // If the object is position-independent, the pointer to the PLT trampoline needs to be relocated.
  200. auto fixup_trampoline_pointer = [&](DynamicObject::Relocation const& relocation) {
  201. VERIFY(static_cast<GenericDynamicRelocationType>(relocation.type()) == GenericDynamicRelocationType::JUMP_SLOT);
  202. if (image().is_dynamic())
  203. *((FlatPtr*)relocation.address().as_ptr()) += m_dynamic_object->base_address().get();
  204. };
  205. m_dynamic_object->plt_relocation_section().for_each_relocation([&](DynamicObject::Relocation const& relocation) {
  206. if (static_cast<GenericDynamicRelocationType>(relocation.type()) == GenericDynamicRelocationType::IRELATIVE) {
  207. m_direct_ifunc_relocations.append(relocation);
  208. return;
  209. }
  210. if (static_cast<GenericDynamicRelocationType>(relocation.type()) == GenericDynamicRelocationType::TLSDESC) {
  211. // GNU ld for some reason puts TLSDESC relocations into .rela.plt
  212. // https://sourceware.org/bugzilla/show_bug.cgi?id=28387
  213. VERIFY(do_direct_relocation(relocation, cached_result, ShouldInitializeWeak::No, ShouldCallIfuncResolver::No) == RelocationResult::Success);
  214. return;
  215. }
  216. // FIXME: Or LD_BIND_NOW is set?
  217. if (m_dynamic_object->must_bind_now()) {
  218. switch (do_plt_relocation(relocation, ShouldCallIfuncResolver::No)) {
  219. case RelocationResult::Failed:
  220. dbgln("Loader.so: {} unresolved symbol '{}'", m_filepath, relocation.symbol().name());
  221. VERIFY_NOT_REACHED();
  222. case RelocationResult::ResolveLater:
  223. VERIFY_NOT_REACHED();
  224. case RelocationResult::CallIfuncResolver:
  225. m_plt_ifunc_relocations.append(relocation);
  226. // Set up lazy binding, in case an IFUNC resolver calls another IFUNC that hasn't been resolved yet.
  227. fixup_trampoline_pointer(relocation);
  228. break;
  229. case RelocationResult::Success:
  230. break;
  231. }
  232. } else {
  233. fixup_trampoline_pointer(relocation);
  234. }
  235. });
  236. }
  237. Result<NonnullRefPtr<DynamicObject>, DlErrorMessage> DynamicLoader::load_stage_3(unsigned flags)
  238. {
  239. do_lazy_relocations();
  240. if (flags & RTLD_LAZY) {
  241. if (m_dynamic_object->has_plt())
  242. setup_plt_trampoline();
  243. }
  244. // IFUNC resolvers can only be called after the PLT has been populated,
  245. // as they may call arbitrary functions via the PLT.
  246. for (auto const& relocation : m_plt_ifunc_relocations) {
  247. auto result = do_plt_relocation(relocation, ShouldCallIfuncResolver::Yes);
  248. VERIFY(result == RelocationResult::Success);
  249. }
  250. Optional<DynamicLoader::CachedLookupResult> cached_result;
  251. for (auto const& relocation : m_direct_ifunc_relocations) {
  252. auto result = do_direct_relocation(relocation, cached_result, ShouldInitializeWeak::No, ShouldCallIfuncResolver::Yes);
  253. VERIFY(result == RelocationResult::Success);
  254. }
  255. if (m_dynamic_object->has_text_relocations()) {
  256. // If we don't have textrels, .text has already been made executable by this point in load_stage_2.
  257. for (auto& text_segment : m_text_segments) {
  258. if (mprotect(text_segment.address().as_ptr(), text_segment.size(), PROT_READ | PROT_EXEC) < 0) {
  259. return DlErrorMessage { ByteString::formatted("mprotect .text: PROT_READ | PROT_EXEC: {}", strerror(errno)) };
  260. }
  261. }
  262. }
  263. if (m_relro_segment_size) {
  264. if (mprotect(m_relro_segment_address.as_ptr(), m_relro_segment_size, PROT_READ) < 0) {
  265. return DlErrorMessage { ByteString::formatted("mprotect .relro: PROT_READ: {}", strerror(errno)) };
  266. }
  267. #ifdef AK_OS_SERENITY
  268. if (set_mmap_name(m_relro_segment_address.as_ptr(), m_relro_segment_size, ByteString::formatted("{}: .relro", m_filepath).characters()) < 0) {
  269. return DlErrorMessage { ByteString::formatted("set_mmap_name .relro: {}", strerror(errno)) };
  270. }
  271. #endif
  272. }
  273. m_fully_relocated = true;
  274. return NonnullRefPtr<DynamicObject> { *m_dynamic_object };
  275. }
  276. void DynamicLoader::load_stage_4()
  277. {
  278. call_object_init_functions();
  279. m_fully_initialized = true;
  280. }
  281. void DynamicLoader::do_lazy_relocations()
  282. {
  283. Optional<DynamicLoader::CachedLookupResult> cached_result;
  284. for (auto const& relocation : m_unresolved_relocations) {
  285. if (auto res = do_direct_relocation(relocation, cached_result, ShouldInitializeWeak::Yes, ShouldCallIfuncResolver::Yes); res != RelocationResult::Success) {
  286. dbgln("Loader.so: {} unresolved symbol '{}'", m_filepath, relocation.symbol().name());
  287. VERIFY_NOT_REACHED();
  288. }
  289. }
  290. }
  291. void DynamicLoader::load_program_headers()
  292. {
  293. FlatPtr ph_load_start = SIZE_MAX;
  294. FlatPtr ph_load_end = 0;
  295. // We walk the program header list once to find the requested address ranges of the program.
  296. // We don't fill in the list of regions yet to keep malloc memory blocks from interfering with our reservation.
  297. image().for_each_program_header([&](Image::ProgramHeader const& program_header) {
  298. if (program_header.type() != PT_LOAD)
  299. return;
  300. FlatPtr section_start = program_header.vaddr().get();
  301. FlatPtr section_end = section_start + program_header.size_in_memory();
  302. if (ph_load_start > section_start)
  303. ph_load_start = section_start;
  304. if (ph_load_end < section_end)
  305. ph_load_end = section_end;
  306. });
  307. void* requested_load_address = image().is_dynamic() ? nullptr : reinterpret_cast<void*>(ph_load_start);
  308. int reservation_mmap_flags = MAP_ANON | MAP_PRIVATE | MAP_NORESERVE;
  309. if (image().is_dynamic())
  310. reservation_mmap_flags |= MAP_RANDOMIZED;
  311. #ifdef MAP_FIXED_NOREPLACE
  312. else
  313. reservation_mmap_flags |= MAP_FIXED_NOREPLACE;
  314. #endif
  315. // First, we make a dummy reservation mapping, in order to allocate enough VM
  316. // to hold all regions contiguously in the address space.
  317. FlatPtr ph_load_base = ph_load_start & ~(FlatPtr)0xfffu;
  318. ph_load_end = round_up_to_power_of_two(ph_load_end, PAGE_SIZE);
  319. size_t total_mapping_size = ph_load_end - ph_load_base;
  320. // Before we make our reservation, unmap our existing mapped ELF image that we used for reading header information.
  321. // This leaves our pointers dangling momentarily, but it reduces the chance that we will conflict with ourselves.
  322. if (munmap(m_file_data, m_file_size) < 0) {
  323. perror("munmap old mapping");
  324. VERIFY_NOT_REACHED();
  325. }
  326. m_elf_image = nullptr;
  327. m_file_data = nullptr;
  328. auto* reservation = mmap(requested_load_address, total_mapping_size, PROT_NONE, reservation_mmap_flags, 0, 0);
  329. if (reservation == MAP_FAILED) {
  330. perror("mmap reservation");
  331. VERIFY_NOT_REACHED();
  332. }
  333. // Now that we can't accidentally block our requested space, re-map our ELF image.
  334. ByteString file_mmap_name = ByteString::formatted("ELF_DYN: {}", m_filepath);
  335. auto* data = mmap_with_name(nullptr, m_file_size, PROT_READ, MAP_SHARED, m_image_fd, 0, file_mmap_name.characters());
  336. if (data == MAP_FAILED) {
  337. perror("mmap new mapping");
  338. VERIFY_NOT_REACHED();
  339. }
  340. m_file_data = data;
  341. m_elf_image = adopt_own(*new ELF::Image((u8*)m_file_data, m_file_size));
  342. VERIFY(requested_load_address == nullptr || reservation == requested_load_address);
  343. m_base_address = VirtualAddress { reservation };
  344. // Most binaries have four loadable regions, three of which are mapped
  345. // (symbol tables/relocation information, executable instructions, read-only data)
  346. // and one of which is copied (modifiable data).
  347. // These are allocated in-line to cut down on the malloc calls.
  348. Vector<ProgramHeaderRegion, 3> map_regions;
  349. Vector<ProgramHeaderRegion, 1> copy_regions;
  350. Optional<ProgramHeaderRegion> relro_region;
  351. VirtualAddress dynamic_region_desired_vaddr;
  352. image().for_each_program_header([&](Image::ProgramHeader const& program_header) {
  353. ProgramHeaderRegion region {};
  354. region.set_program_header(program_header.raw_header());
  355. if (region.is_tls_template()) {
  356. // Skip, this is handled in DynamicLoader::copy_initial_tls_data_into.
  357. } else if (region.is_load()) {
  358. if (region.size_in_memory() == 0)
  359. return;
  360. if (region.is_writable()) {
  361. copy_regions.append(region);
  362. } else {
  363. map_regions.append(region);
  364. }
  365. } else if (region.is_dynamic()) {
  366. dynamic_region_desired_vaddr = region.desired_load_address();
  367. } else if (region.is_relro()) {
  368. VERIFY(!relro_region.has_value());
  369. relro_region = region;
  370. }
  371. });
  372. VERIFY(!map_regions.is_empty() || !copy_regions.is_empty());
  373. auto compare_load_address = [](ProgramHeaderRegion& a, ProgramHeaderRegion& b) {
  374. return a.desired_load_address().as_ptr() < b.desired_load_address().as_ptr();
  375. };
  376. quick_sort(map_regions, compare_load_address);
  377. quick_sort(copy_regions, compare_load_address);
  378. // Pre-allocate any malloc memory needed before unmapping the reservation.
  379. // We don't want any future malloc to accidentally mmap a reserved address!
  380. ByteString text_segment_name = ByteString::formatted("{}: .text", m_filepath);
  381. ByteString rodata_segment_name = ByteString::formatted("{}: .rodata", m_filepath);
  382. ByteString data_segment_name = ByteString::formatted("{}: .data", m_filepath);
  383. m_text_segments.ensure_capacity(map_regions.size());
  384. // Finally, we unmap the reservation.
  385. if (munmap(reservation, total_mapping_size) < 0) {
  386. perror("munmap reservation");
  387. VERIFY_NOT_REACHED();
  388. }
  389. // WARNING: Allocating after this point has the possibility of malloc stealing our reserved
  390. // virtual memory addresses. Be careful not to malloc below!
  391. // Process regions in order: .text, .data, .tls
  392. for (auto& region : map_regions) {
  393. FlatPtr ph_desired_base = region.desired_load_address().get();
  394. FlatPtr ph_base = region.desired_load_address().page_base().get();
  395. FlatPtr ph_end = ph_base + round_up_to_power_of_two(region.size_in_memory() + region.desired_load_address().get() - ph_base, PAGE_SIZE);
  396. char const* const segment_name = region.is_executable() ? text_segment_name.characters() : rodata_segment_name.characters();
  397. // Now we can map the text segment at the reserved address.
  398. auto* segment_base = (u8*)mmap_with_name(
  399. (u8*)reservation + ph_base - ph_load_base,
  400. ph_desired_base - ph_base + region.size_in_image(),
  401. PROT_READ,
  402. MAP_SHARED | MAP_FIXED,
  403. m_image_fd,
  404. VirtualAddress { region.offset() }.page_base().get(),
  405. segment_name);
  406. if (segment_base == MAP_FAILED) {
  407. perror("mmap non-writable");
  408. VERIFY_NOT_REACHED();
  409. }
  410. // NOTE: Capacity ensured above the line of no malloc above
  411. if (region.is_executable())
  412. m_text_segments.unchecked_append({ VirtualAddress { segment_base }, ph_end - ph_base });
  413. }
  414. VERIFY(requested_load_address == nullptr || requested_load_address == reservation);
  415. if (relro_region.has_value()) {
  416. m_relro_segment_size = relro_region->size_in_memory();
  417. m_relro_segment_address = VirtualAddress { (u8*)reservation + relro_region->desired_load_address().get() - ph_load_base };
  418. }
  419. if (image().is_dynamic())
  420. m_dynamic_section_address = VirtualAddress { (u8*)reservation + dynamic_region_desired_vaddr.get() - ph_load_base };
  421. else
  422. m_dynamic_section_address = dynamic_region_desired_vaddr;
  423. for (auto& region : copy_regions) {
  424. FlatPtr ph_data_base = region.desired_load_address().page_base().get();
  425. FlatPtr ph_data_end = ph_data_base + round_up_to_power_of_two(region.size_in_memory() + region.desired_load_address().get() - ph_data_base, PAGE_SIZE);
  426. auto* data_segment_address = (u8*)reservation + ph_data_base - ph_load_base;
  427. size_t data_segment_size = ph_data_end - ph_data_base;
  428. // Finally, we make an anonymous mapping for the data segment. Contents are then copied from the file.
  429. auto* data_segment = (u8*)mmap_with_name(
  430. data_segment_address,
  431. data_segment_size,
  432. PROT_READ | PROT_WRITE,
  433. MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,
  434. 0,
  435. 0,
  436. data_segment_name.characters());
  437. if (MAP_FAILED == data_segment) {
  438. perror("mmap writable");
  439. VERIFY_NOT_REACHED();
  440. }
  441. VirtualAddress data_segment_start;
  442. if (image().is_dynamic())
  443. data_segment_start = VirtualAddress { (u8*)reservation + region.desired_load_address().get() };
  444. else
  445. data_segment_start = region.desired_load_address();
  446. VERIFY(data_segment_start.as_ptr() + region.size_in_memory() <= data_segment + data_segment_size);
  447. memcpy(data_segment_start.as_ptr(), (u8*)m_file_data + region.offset(), region.size_in_image());
  448. }
  449. }
  450. DynamicLoader::RelocationResult DynamicLoader::do_direct_relocation(DynamicObject::Relocation const& relocation,
  451. Optional<DynamicLoader::CachedLookupResult>& cached_result,
  452. [[maybe_unused]] ShouldInitializeWeak should_initialize_weak,
  453. ShouldCallIfuncResolver should_call_ifunc_resolver)
  454. {
  455. FlatPtr* patch_ptr = nullptr;
  456. if (is_dynamic())
  457. patch_ptr = (FlatPtr*)(m_dynamic_object->base_address().as_ptr() + relocation.offset());
  458. else
  459. patch_ptr = (FlatPtr*)(FlatPtr)relocation.offset();
  460. auto call_ifunc_resolver = [](VirtualAddress address) {
  461. return VirtualAddress { reinterpret_cast<DynamicObject::IfuncResolver>(address.get())() };
  462. };
  463. auto lookup_symbol = [&](DynamicObject::Symbol const& symbol) {
  464. // The static linker sorts relocations by the referenced symbol. Especially when vtables
  465. // in large inheritance hierarchies are involved, there might be tens of references to
  466. // the same symbol. We can avoid redundant lookups by keeping track of the previous result.
  467. if (!cached_result.has_value() || !cached_result.value().symbol.definitely_equals(symbol))
  468. cached_result = DynamicLoader::CachedLookupResult { symbol, DynamicLoader::lookup_symbol(symbol) };
  469. return cached_result.value().result;
  470. };
  471. struct ResolvedTLSSymbol {
  472. DynamicObject const& dynamic_object;
  473. FlatPtr value;
  474. };
  475. auto resolve_tls_symbol = [&](DynamicObject::Relocation const& relocation) -> Optional<ResolvedTLSSymbol> {
  476. if (relocation.symbol_index() == 0)
  477. return ResolvedTLSSymbol { relocation.dynamic_object(), 0 };
  478. auto res = lookup_symbol(relocation.symbol());
  479. if (!res.has_value())
  480. return {};
  481. VERIFY(relocation.symbol().type() != STT_GNU_IFUNC);
  482. VERIFY(res.value().dynamic_object != nullptr);
  483. return ResolvedTLSSymbol { *res.value().dynamic_object, res.value().value };
  484. };
  485. using enum GenericDynamicRelocationType;
  486. switch (static_cast<GenericDynamicRelocationType>(relocation.type())) {
  487. case NONE:
  488. // Apparently most loaders will just skip these?
  489. // Seems if the 'link editor' generates one something is funky with your code
  490. break;
  491. case ABSOLUTE: {
  492. auto symbol = relocation.symbol();
  493. auto res = lookup_symbol(symbol);
  494. if (!res.has_value()) {
  495. if (symbol.bind() == STB_WEAK)
  496. return RelocationResult::ResolveLater;
  497. dbgln("ERROR: symbol not found: {}.", symbol.name());
  498. return RelocationResult::Failed;
  499. }
  500. if (res.value().type == STT_GNU_IFUNC && should_call_ifunc_resolver == ShouldCallIfuncResolver::No)
  501. return RelocationResult::CallIfuncResolver;
  502. auto symbol_address = res.value().address;
  503. if (relocation.addend_used())
  504. *patch_ptr = symbol_address.get() + relocation.addend();
  505. else
  506. *patch_ptr += symbol_address.get();
  507. if (res.value().type == STT_GNU_IFUNC)
  508. *patch_ptr = call_ifunc_resolver(VirtualAddress { *patch_ptr }).get();
  509. break;
  510. }
  511. #if !ARCH(RISCV64)
  512. case GLOB_DAT: {
  513. auto symbol = relocation.symbol();
  514. auto res = lookup_symbol(symbol);
  515. VirtualAddress symbol_location;
  516. if (!res.has_value()) {
  517. if (symbol.bind() == STB_WEAK) {
  518. if (should_initialize_weak == ShouldInitializeWeak::No)
  519. return RelocationResult::ResolveLater;
  520. } else {
  521. // Symbol not found
  522. return RelocationResult::Failed;
  523. }
  524. symbol_location = VirtualAddress { (FlatPtr)0 };
  525. } else {
  526. symbol_location = res.value().address;
  527. if (res.value().type == STT_GNU_IFUNC) {
  528. if (should_call_ifunc_resolver == ShouldCallIfuncResolver::No)
  529. return RelocationResult::CallIfuncResolver;
  530. if (res.value().dynamic_object != nullptr && res.value().dynamic_object->has_text_relocations()) {
  531. dbgln("\033[31mError:\033[0m Refusing to call IFUNC resolver defined in an object with text relocations.");
  532. return RelocationResult::Failed;
  533. }
  534. symbol_location = call_ifunc_resolver(symbol_location);
  535. }
  536. }
  537. VERIFY(symbol_location != m_dynamic_object->base_address());
  538. *patch_ptr = symbol_location.get();
  539. break;
  540. }
  541. #endif
  542. case RELATIVE: {
  543. if (!image().is_dynamic())
  544. break;
  545. // FIXME: According to the spec, R_386_relative ones must be done first.
  546. // We could explicitly do them first using m_number_of_relocations from DT_RELCOUNT
  547. // However, our compiler is nice enough to put them at the front of the relocations for us :)
  548. if (relocation.addend_used())
  549. *patch_ptr = m_dynamic_object->base_address().offset(relocation.addend()).get();
  550. else
  551. *patch_ptr += m_dynamic_object->base_address().get();
  552. break;
  553. }
  554. case TLS_TPREL: {
  555. auto maybe_resolution = resolve_tls_symbol(relocation);
  556. if (!maybe_resolution.has_value())
  557. break;
  558. auto [dynamic_object_of_symbol, symbol_value] = maybe_resolution.value();
  559. size_t addend = relocation.addend_used() ? relocation.addend() : *patch_ptr;
  560. *patch_ptr = addend + dynamic_object_of_symbol.tls_offset().value() + symbol_value + TLS_TP_STATIC_TLS_BLOCK_OFFSET;
  561. if constexpr (TLS_VARIANT == 1) {
  562. // Until offset TLS_TP_STATIC_TLS_BLOCK_OFFSET there's the thread's ThreadControlBlock, we don't want to collide with it.
  563. VERIFY(static_cast<ssize_t>(*patch_ptr) >= static_cast<ssize_t>(TLS_TP_STATIC_TLS_BLOCK_OFFSET));
  564. } else if constexpr (TLS_VARIANT == 2) {
  565. // At offset 0 there's the thread's ThreadControlBlock, we don't want to collide with it.
  566. VERIFY(static_cast<ssize_t>(*patch_ptr) < 0);
  567. }
  568. break;
  569. }
  570. case TLS_DTPMOD: {
  571. auto maybe_resolution = resolve_tls_symbol(relocation);
  572. if (!maybe_resolution.has_value())
  573. break;
  574. // We repurpose the module index to store the TLS block's TP offset. This is fine
  575. // because we currently only support a single static TLS block.
  576. *patch_ptr = maybe_resolution->dynamic_object.tls_offset().value();
  577. break;
  578. }
  579. case TLS_DTPREL: {
  580. auto maybe_resolution = resolve_tls_symbol(relocation);
  581. if (!maybe_resolution.has_value())
  582. break;
  583. size_t addend = relocation.addend_used() ? relocation.addend() : *patch_ptr;
  584. *patch_ptr = addend + maybe_resolution->value - TLS_DTV_OFFSET + TLS_TP_STATIC_TLS_BLOCK_OFFSET;
  585. break;
  586. }
  587. #ifdef HAS_TLSDESC_SUPPORT
  588. case TLSDESC: {
  589. auto maybe_resolution = resolve_tls_symbol(relocation);
  590. if (!maybe_resolution.has_value())
  591. break;
  592. auto [dynamic_object_of_symbol, symbol_value] = maybe_resolution.value();
  593. size_t addend = relocation.addend_used() ? relocation.addend() : *patch_ptr;
  594. patch_ptr[0] = (FlatPtr)__tlsdesc_static;
  595. patch_ptr[1] = addend + dynamic_object_of_symbol.tls_offset().value() + symbol_value;
  596. break;
  597. }
  598. #endif
  599. case IRELATIVE: {
  600. if (should_call_ifunc_resolver == ShouldCallIfuncResolver::No)
  601. return RelocationResult::CallIfuncResolver;
  602. VirtualAddress resolver;
  603. if (relocation.addend_used())
  604. resolver = m_dynamic_object->base_address().offset(relocation.addend());
  605. else
  606. resolver = m_dynamic_object->base_address().offset(*patch_ptr);
  607. if (m_dynamic_object->has_text_relocations()) {
  608. dbgln("\033[31mError:\033[0m Refusing to call IFUNC resolver defined in an object with text relocations.");
  609. return RelocationResult::Failed;
  610. }
  611. *patch_ptr = call_ifunc_resolver(resolver).get();
  612. break;
  613. }
  614. case JUMP_SLOT:
  615. VERIFY_NOT_REACHED(); // PLT relocations are handled by do_plt_relocation.
  616. default:
  617. // Raise the alarm! Someone needs to implement this relocation type
  618. dbgln("Found a new exciting relocation type {}", relocation.type());
  619. VERIFY_NOT_REACHED();
  620. }
  621. return RelocationResult::Success;
  622. }
  623. DynamicLoader::RelocationResult DynamicLoader::do_plt_relocation(DynamicObject::Relocation const& relocation, ShouldCallIfuncResolver should_call_ifunc_resolver)
  624. {
  625. VERIFY(static_cast<GenericDynamicRelocationType>(relocation.type()) == GenericDynamicRelocationType::JUMP_SLOT);
  626. auto symbol = relocation.symbol();
  627. auto* relocation_address = (FlatPtr*)relocation.address().as_ptr();
  628. VirtualAddress symbol_location {};
  629. if (auto result = lookup_symbol(symbol); result.has_value()) {
  630. auto address = result.value().address;
  631. if (result.value().type == STT_GNU_IFUNC) {
  632. if (should_call_ifunc_resolver == ShouldCallIfuncResolver::No)
  633. return RelocationResult::CallIfuncResolver;
  634. symbol_location = VirtualAddress { reinterpret_cast<DynamicObject::IfuncResolver>(address.get())() };
  635. } else {
  636. symbol_location = address;
  637. }
  638. } else if (symbol.bind() != STB_WEAK) {
  639. return RelocationResult::Failed;
  640. }
  641. dbgln_if(DYNAMIC_LOAD_DEBUG, "DynamicLoader: Jump slot relocation: putting {} ({}) into PLT at {}", symbol.name(), symbol_location, (void*)relocation_address);
  642. *relocation_address = symbol_location.get();
  643. return RelocationResult::Success;
  644. }
  645. void DynamicLoader::do_relr_relocations()
  646. {
  647. auto base_address = m_dynamic_object->base_address().get();
  648. m_dynamic_object->for_each_relr_relocation([base_address](FlatPtr address) {
  649. *(FlatPtr*)address += base_address;
  650. });
  651. }
  652. void DynamicLoader::copy_initial_tls_data_into(Bytes buffer) const
  653. {
  654. image().for_each_program_header([this, &buffer](ELF::Image::ProgramHeader program_header) {
  655. if (program_header.type() != PT_TLS)
  656. return IterationDecision::Continue;
  657. // Note: The "size in image" is only concerned with initialized data. Uninitialized data (.tbss) is
  658. // only included in the "size in memory" metric, and is expected to not be touched or read from, as
  659. // it is not present in the image and zeroed out in-memory. We will still check that the buffer has
  660. // space for both the initialized and the uninitialized data.
  661. // TODO: Is the initialized data always in the beginning of the TLS segment, or should we walk the
  662. // sections to figure that out?
  663. VERIFY(program_header.size_in_image() <= program_header.size_in_memory());
  664. VERIFY(program_header.size_in_memory() <= m_tls_size_of_current_object);
  665. if constexpr (TLS_VARIANT == 1) {
  666. size_t tls_start_in_buffer = m_tls_offset;
  667. VERIFY(tls_start_in_buffer + program_header.size_in_memory() <= buffer.size());
  668. memcpy(buffer.data() + tls_start_in_buffer, static_cast<u8 const*>(m_file_data) + program_header.offset(), program_header.size_in_image());
  669. } else if constexpr (TLS_VARIANT == 2) {
  670. size_t tls_start_in_buffer = buffer.size() + m_tls_offset;
  671. VERIFY(tls_start_in_buffer + program_header.size_in_memory() <= buffer.size());
  672. memcpy(buffer.data() + tls_start_in_buffer, static_cast<u8 const*>(m_file_data) + program_header.offset(), program_header.size_in_image());
  673. }
  674. return IterationDecision::Break;
  675. });
  676. }
  677. // Defined in <arch>/plt_trampoline.S
  678. extern "C" void _plt_trampoline(void) __attribute__((visibility("hidden")));
  679. void DynamicLoader::setup_plt_trampoline()
  680. {
  681. VERIFY(m_dynamic_object);
  682. VERIFY(m_dynamic_object->has_plt());
  683. VirtualAddress got_address = m_dynamic_object->plt_got_base_address();
  684. auto* got_ptr = (FlatPtr*)got_address.as_ptr();
  685. #if ARCH(AARCH64) || ARCH(X86_64)
  686. got_ptr[1] = (FlatPtr)m_dynamic_object.ptr();
  687. got_ptr[2] = (FlatPtr)&_plt_trampoline;
  688. #elif ARCH(RISCV64)
  689. got_ptr[0] = (FlatPtr)&_plt_trampoline;
  690. got_ptr[1] = (FlatPtr)m_dynamic_object.ptr();
  691. #else
  692. # error Unknown architecture
  693. #endif
  694. }
  695. // Called from our ASM routine _plt_trampoline.
  696. extern "C" FlatPtr _fixup_plt_entry(DynamicObject* object, u32 relocation_offset)
  697. {
  698. auto const& relocation = object->plt_relocation_section().relocation_at_offset(relocation_offset);
  699. auto result = DynamicLoader::do_plt_relocation(relocation, ShouldCallIfuncResolver::Yes);
  700. if (result != DynamicLoader::RelocationResult::Success) {
  701. dbgln("Loader.so: {} unresolved symbol '{}'", object->filepath(), relocation.symbol().name());
  702. VERIFY_NOT_REACHED();
  703. }
  704. return *reinterpret_cast<FlatPtr*>(relocation.address().as_ptr());
  705. }
  706. void DynamicLoader::call_object_init_functions()
  707. {
  708. typedef void (*InitFunc)();
  709. if (m_dynamic_object->has_init_section()) {
  710. auto init_function = m_dynamic_object->init_section_function();
  711. (init_function)();
  712. }
  713. if (m_dynamic_object->has_init_array_section()) {
  714. auto init_array_section = m_dynamic_object->init_array_section();
  715. InitFunc* init_begin = (InitFunc*)(init_array_section.address().as_ptr());
  716. InitFunc* init_end = init_begin + init_array_section.entry_count();
  717. while (init_begin != init_end) {
  718. // Android sources claim that these can be -1, to be ignored.
  719. // 0 definitely shows up. Apparently 0/-1 are valid? Confusing.
  720. if (!*init_begin || ((FlatPtr)*init_begin == (FlatPtr)-1))
  721. continue;
  722. (*init_begin)();
  723. ++init_begin;
  724. }
  725. }
  726. }
  727. Optional<DynamicObject::SymbolLookupResult> DynamicLoader::lookup_symbol(const ELF::DynamicObject::Symbol& symbol)
  728. {
  729. if (symbol.is_undefined() || symbol.bind() == STB_WEAK)
  730. return DynamicLinker::lookup_global_symbol(symbol.name());
  731. return DynamicObject::SymbolLookupResult { symbol.value(), symbol.size(), symbol.address(), symbol.bind(), symbol.type(), &symbol.object() };
  732. }
  733. } // end namespace ELF