DynamicLinker.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, the SerenityOS developers.
  5. * Copyright (c) 2022, Jesse Buhagiar <jooster669@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Debug.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/HashTable.h>
  13. #include <AK/LexicalPath.h>
  14. #include <AK/Platform.h>
  15. #include <AK/Random.h>
  16. #include <AK/ScopeGuard.h>
  17. #include <AK/Vector.h>
  18. #include <Kernel/API/VirtualMemoryAnnotations.h>
  19. #include <Kernel/API/prctl_numbers.h>
  20. #include <LibELF/Arch/tls.h>
  21. #include <LibELF/AuxiliaryVector.h>
  22. #include <LibELF/DynamicLinker.h>
  23. #include <LibELF/DynamicLoader.h>
  24. #include <LibELF/DynamicObject.h>
  25. #include <LibELF/Hashes.h>
  26. #include <bits/dlfcn_integration.h>
  27. #include <bits/pthread_integration.h>
  28. #include <dlfcn.h>
  29. #include <fcntl.h>
  30. #include <link.h>
  31. #include <pthread.h>
  32. #include <string.h>
  33. #include <sys/internals.h>
  34. #include <sys/mman.h>
  35. #include <sys/types.h>
  36. #include <syscall.h>
  37. #include <unistd.h>
  38. namespace ELF {
  39. static ByteString s_main_program_path;
  40. // The order of objects here corresponds to the "load order" from POSIX specification.
  41. static OrderedHashMap<ByteString, NonnullRefPtr<ELF::DynamicObject>> s_global_objects;
  42. using LibCExitFunction = void (*)(int);
  43. using DlIteratePhdrCallbackFunction = int (*)(struct dl_phdr_info*, size_t, void*);
  44. using DlIteratePhdrFunction = int (*)(DlIteratePhdrCallbackFunction, void*);
  45. using CallFiniFunctionsFunction = void (*)();
  46. struct TLSData {
  47. size_t total_tls_size { 0 };
  48. void* tls_template { nullptr };
  49. size_t tls_template_size { 0 };
  50. size_t alignment { 0 };
  51. size_t static_tls_region_size { 0 };
  52. size_t static_tls_region_alignment { 0 };
  53. };
  54. static TLSData s_tls_data;
  55. static char** s_envp = nullptr;
  56. static __pthread_mutex_t s_loader_lock = __PTHREAD_MUTEX_INITIALIZER;
  57. static ByteString s_cwd;
  58. static bool s_allowed_to_check_environment_variables { false };
  59. static bool s_do_breakpoint_trap_before_entry { false };
  60. static StringView s_ld_library_path;
  61. static StringView s_main_program_pledge_promises;
  62. static ByteString s_loader_pledge_promises;
  63. static HashMap<StringView, DynamicObject::SymbolLookupResult> s_magic_functions;
  64. Optional<DynamicObject::SymbolLookupResult> DynamicLinker::lookup_global_symbol(StringView name)
  65. {
  66. auto symbol = DynamicObject::HashSymbol { name };
  67. for (auto& lib : s_global_objects) {
  68. auto res = lib.value->lookup_symbol(symbol);
  69. if (!res.has_value())
  70. continue;
  71. if (res.value().bind == STB_GLOBAL || res.value().bind == STB_WEAK)
  72. return res;
  73. // We don't want to allow local symbols to be pulled in to other modules
  74. }
  75. if (auto magic_lookup = s_magic_functions.get(name); magic_lookup.has_value())
  76. return *magic_lookup;
  77. return {};
  78. }
  79. static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(ByteString const& filepath, int fd)
  80. {
  81. VERIFY(filepath.starts_with('/'));
  82. auto loader = TRY(ELF::DynamicLoader::try_create(fd, filepath));
  83. static size_t s_current_tls_offset = 0;
  84. if constexpr (TLS_VARIANT == 1) {
  85. if (loader->tls_alignment_of_current_object() != 0)
  86. s_current_tls_offset = align_up_to(s_current_tls_offset, loader->tls_alignment_of_current_object());
  87. loader->set_tls_offset(s_current_tls_offset);
  88. s_current_tls_offset += loader->tls_size_of_current_object();
  89. } else if constexpr (TLS_VARIANT == 2) {
  90. s_current_tls_offset -= loader->tls_size_of_current_object();
  91. if (loader->tls_alignment_of_current_object() != 0)
  92. s_current_tls_offset = align_down_to(s_current_tls_offset, loader->tls_alignment_of_current_object());
  93. loader->set_tls_offset(s_current_tls_offset);
  94. }
  95. // This actually maps the library at the intended and final place.
  96. auto main_library_object = loader->map();
  97. s_global_objects.set(filepath, *main_library_object);
  98. return loader;
  99. }
  100. Optional<ByteString> DynamicLinker::resolve_library(ByteString const& name, DynamicObject const& parent_object)
  101. {
  102. // Absolute and relative (to the current working directory) paths are already considered resolved.
  103. // However, ensure that the returned path is absolute and canonical, so pass it through LexicalPath.
  104. if (name.contains('/'))
  105. return LexicalPath::absolute_path(s_cwd, name);
  106. Vector<StringView> search_paths;
  107. // Search RPATH values indicated by the ELF (only if RUNPATH is not present).
  108. if (parent_object.runpath().is_empty())
  109. search_paths.extend(parent_object.rpath().split_view(':'));
  110. // Scan the LD_LIBRARY_PATH environment variable if applicable.
  111. search_paths.extend(s_ld_library_path.split_view(':'));
  112. // Search RUNPATH values indicated by the ELF.
  113. search_paths.extend(parent_object.runpath().split_view(':'));
  114. // Last are the default search paths.
  115. search_paths.append("/usr/lib"sv);
  116. search_paths.append("/usr/local/lib"sv);
  117. for (auto const& search_path : search_paths) {
  118. LexicalPath library_path(search_path.replace("$ORIGIN"sv, LexicalPath::dirname(parent_object.filepath()), ReplaceMode::FirstOnly));
  119. ByteString library_name = library_path.append(name).string();
  120. if (access(library_name.characters(), F_OK) == 0) {
  121. if (!library_name.starts_with('/')) {
  122. // FIXME: Non-absolute paths should resolve from the current working directory. However,
  123. // since that's almost never the effect that is actually desired, let's print
  124. // a warning and only implement it once something actually needs that behavior.
  125. dbgln("\033[33mWarning:\033[0m Resolving library '{}' resulted in non-absolute path '{}'. Check your binary for relative RPATHs and RUNPATHs.", name, library_name);
  126. }
  127. return library_name;
  128. }
  129. }
  130. return {};
  131. }
  132. static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(ByteString const& path)
  133. {
  134. VERIFY(path.starts_with('/'));
  135. int fd = open(path.characters(), O_RDONLY);
  136. if (fd < 0)
  137. return DlErrorMessage { ByteString::formatted("Could not open shared library '{}': {}", path, strerror(errno)) };
  138. return map_library(path, fd);
  139. }
  140. static Vector<ByteString> get_dependencies(NonnullRefPtr<DynamicLoader> const& loader)
  141. {
  142. auto name = LexicalPath::basename(loader->filepath());
  143. Vector<ByteString> dependencies;
  144. loader->for_each_needed_library([&dependencies, &name](auto needed_name) {
  145. if (name == needed_name)
  146. return;
  147. dependencies.append(needed_name);
  148. });
  149. return dependencies;
  150. }
  151. struct DependencyOrdering {
  152. Vector<NonnullRefPtr<DynamicLoader>> load_order;
  153. // In addition to "load order" (and "dependency order") from POSIX, we also define "topological
  154. // order". This is a topological ordering of "NEEDED" dependencies, where we ignore edges that
  155. // result in cycles. Edges that are not ignored are called true dependencies.
  156. Vector<NonnullRefPtr<DynamicLoader>> topological_order;
  157. };
  158. static ErrorOr<DependencyOrdering, DlErrorMessage> map_dependencies(NonnullRefPtr<DynamicLoader> const& loader)
  159. {
  160. Vector<NonnullRefPtr<DynamicLoader>> load_order = { loader };
  161. HashMap<ByteString, NonnullRefPtr<DynamicLoader>> current_loaders;
  162. current_loaders.set(loader->filepath(), loader);
  163. // First, we do BFS on NEEDED dependencies graph while using load_order as a poor man's queue.
  164. // NOTE: BFS is mandated by POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlopen.html#:~:text=Dependency%20ordering%20uses%20a%20breadth%2Dfirst%20order%20starting .
  165. for (size_t i = 0; i < load_order.size(); ++i) {
  166. auto loader = load_order[i];
  167. auto const& parent_object = loader->dynamic_object();
  168. dbgln_if(DYNAMIC_LOAD_DEBUG, "mapping dependencies for: {}", loader->filepath());
  169. for (auto const& needed_name : get_dependencies(loader)) {
  170. dbgln_if(DYNAMIC_LOAD_DEBUG, "needed library: {}", needed_name.characters());
  171. auto maybe_dependency_path = DynamicLinker::resolve_library(needed_name, parent_object);
  172. if (!maybe_dependency_path.has_value())
  173. return DlErrorMessage { ByteString::formatted("Could not find required shared library: {}", needed_name) };
  174. auto dependency_path = maybe_dependency_path.release_value();
  175. if (!s_global_objects.contains(dependency_path)) {
  176. auto dependency_loader = TRY(map_library(dependency_path));
  177. load_order.append(dependency_loader);
  178. current_loaders.set(dependency_loader->filepath(), dependency_loader);
  179. }
  180. if (auto it = current_loaders.find(dependency_path); it != current_loaders.end()) {
  181. // Even if the object is already mapped, the dependency might still affect topological order.
  182. loader->add_dependency(it->value);
  183. }
  184. }
  185. dbgln_if(DYNAMIC_LOAD_DEBUG, "mapped dependencies for {}", loader->filepath());
  186. }
  187. // Next, we compute topological order using the classical algorithm involving DFS. Topological
  188. // ordering is used for calling initializers: https://www.sco.com/developers/gabi/latest/ch5.dynamic.html#init_fini .
  189. Vector<NonnullRefPtr<DynamicLoader>> topological_order;
  190. topological_order.ensure_capacity(load_order.size());
  191. loader->compute_topological_order(topological_order);
  192. VERIFY(topological_order.size() == load_order.size());
  193. VERIFY(topological_order.last()->filepath() == loader->filepath());
  194. return DependencyOrdering {
  195. .load_order = move(load_order),
  196. .topological_order = move(topological_order),
  197. };
  198. }
  199. static ErrorOr<FlatPtr> __create_new_tls_region()
  200. {
  201. void* static_tls_region = serenity_mmap(nullptr, s_tls_data.static_tls_region_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0, s_tls_data.static_tls_region_alignment, "Static TLS Data");
  202. if (static_tls_region == MAP_FAILED)
  203. return Error::from_syscall("mmap"sv, -errno);
  204. auto thread_pointer = calculate_tp_value_from_static_tls_region_address(bit_cast<FlatPtr>(static_tls_region), s_tls_data.tls_template_size, s_tls_data.static_tls_region_alignment);
  205. VERIFY(thread_pointer % s_tls_data.static_tls_region_alignment == 0);
  206. auto* tcb = get_tcb_pointer_from_thread_pointer(thread_pointer);
  207. // FIXME: Add support for dynamically-allocated TLS blocks.
  208. tcb->dynamic_thread_vector = nullptr;
  209. #if ARCH(X86_64)
  210. tcb->thread_pointer = bit_cast<void*>(thread_pointer);
  211. #endif
  212. auto* static_tls_blocks = get_pointer_to_first_static_tls_block_from_thread_pointer(thread_pointer, s_tls_data.tls_template_size, s_tls_data.static_tls_region_alignment);
  213. if (s_tls_data.tls_template_size != 0)
  214. memcpy(static_tls_blocks, s_tls_data.tls_template, s_tls_data.tls_template_size);
  215. return thread_pointer;
  216. }
  217. static ErrorOr<void> __free_tls_region(FlatPtr thread_pointer)
  218. {
  219. auto* static_tls_region = get_pointer_to_static_tls_region_from_thread_pointer(thread_pointer, s_tls_data.tls_template_size, s_tls_data.static_tls_region_alignment);
  220. if (munmap(static_tls_region, s_tls_data.static_tls_region_size) != 0)
  221. return Error::from_syscall("mmap"sv, -errno);
  222. return {};
  223. }
  224. static void allocate_tls(Vector<NonnullRefPtr<DynamicLoader>> const& loaded_objects)
  225. {
  226. // FIXME: Use the max p_align of all TLS segments.
  227. // We currently pass s_tls_data.static_tls_region_alignment as the alignment to mmap,
  228. // so we would have to manually insert padding, as mmap only accepts alignments that
  229. // are multiples of PAGE_SIZE. Or instead use aligned_alloc/posix_memalign?
  230. s_tls_data.alignment = PAGE_SIZE;
  231. for (auto const& object : loaded_objects) {
  232. dbgln_if(DYNAMIC_LOAD_DEBUG, "{}: TLS Size: {}, TLS Alignment: {}", object->filepath(), object->tls_size_of_current_object(), object->tls_alignment_of_current_object());
  233. s_tls_data.total_tls_size += object->tls_size_of_current_object() + object->tls_alignment_of_current_object();
  234. }
  235. if (s_tls_data.total_tls_size == 0)
  236. return;
  237. s_tls_data.tls_template_size = align_up_to(s_tls_data.total_tls_size, PAGE_SIZE);
  238. s_tls_data.tls_template = mmap_with_name(nullptr, s_tls_data.tls_template_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0, "TLS Template");
  239. if (s_tls_data.tls_template == MAP_FAILED) {
  240. dbgln("Failed to allocate memory for the TLS template");
  241. VERIFY_NOT_REACHED();
  242. }
  243. s_tls_data.static_tls_region_alignment = max(s_tls_data.alignment, sizeof(ThreadControlBlock));
  244. s_tls_data.static_tls_region_size = calculate_static_tls_region_size(s_tls_data.tls_template_size, s_tls_data.static_tls_region_alignment);
  245. auto tls_template = Bytes(s_tls_data.tls_template, s_tls_data.tls_template_size);
  246. // Initialize TLS data
  247. for (auto const& object : loaded_objects)
  248. object->copy_initial_tls_data_into(tls_template);
  249. set_thread_pointer_register(MUST(__create_new_tls_region()));
  250. }
  251. static int __dl_iterate_phdr(DlIteratePhdrCallbackFunction callback, void* data)
  252. {
  253. pthread_mutex_lock(&s_loader_lock);
  254. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  255. for (auto& it : s_global_objects) {
  256. auto& object = it.value;
  257. auto info = dl_phdr_info {
  258. .dlpi_addr = (Elf_Addr)object->base_address().as_ptr(),
  259. .dlpi_name = object->filepath().characters(),
  260. .dlpi_phdr = object->program_headers(),
  261. .dlpi_phnum = object->program_header_count()
  262. };
  263. auto res = callback(&info, sizeof(info), data);
  264. if (res != 0)
  265. return res;
  266. }
  267. return 0;
  268. }
  269. int DynamicLinker::iterate_over_loaded_shared_objects(int (*callback)(struct dl_phdr_info* info, size_t size, void* data), void* data)
  270. {
  271. return __dl_iterate_phdr(callback, data);
  272. }
  273. static void initialize_libc(DynamicObject& libc)
  274. {
  275. auto res = libc.lookup_symbol("__libc_init"sv);
  276. VERIFY(res.has_value());
  277. using libc_init_func = decltype(__libc_init);
  278. ((libc_init_func*)res.value().address.as_ptr())();
  279. }
  280. static void drop_loader_promise(StringView promise_to_drop)
  281. {
  282. if (s_main_program_pledge_promises.is_empty() || s_loader_pledge_promises.is_empty())
  283. return;
  284. s_loader_pledge_promises = s_loader_pledge_promises.replace(promise_to_drop, ""sv, ReplaceMode::All);
  285. auto extended_promises = ByteString::formatted("{} {}", s_main_program_pledge_promises, s_loader_pledge_promises);
  286. Syscall::SC_pledge_params params {
  287. { extended_promises.characters(), extended_promises.length() },
  288. { nullptr, 0 },
  289. };
  290. int rc = syscall(SC_pledge, &params);
  291. if (rc < 0 && rc > -EMAXERRNO) {
  292. warnln("Failed to drop loader pledge promise: {}. errno={}", promise_to_drop, errno);
  293. _exit(1);
  294. }
  295. }
  296. static ErrorOr<void, DlErrorMessage> link_main_library(int flags, DependencyOrdering const& objects)
  297. {
  298. // Verify that all objects are already mapped
  299. for (auto& loader : objects.load_order)
  300. VERIFY(!loader->map());
  301. // FIXME: Are there any observable differences between doing stages 2 and 3 in topological vs
  302. // load order? POSIX says to do relocations in load order but does the order really
  303. // matter here?
  304. for (auto& loader : objects.load_order) {
  305. bool success = loader->link(flags);
  306. if (!success) {
  307. return DlErrorMessage { ByteString::formatted("Failed to link library {}", loader->filepath()) };
  308. }
  309. }
  310. for (auto& loader : objects.load_order) {
  311. auto result = loader->load_stage_3(flags);
  312. VERIFY(!result.is_error());
  313. auto& object = result.value();
  314. if (loader->filepath().ends_with("/libc.so"sv)) {
  315. initialize_libc(*object);
  316. }
  317. if (loader->filepath().ends_with("/libsystem.so"sv)) {
  318. VERIFY(!loader->text_segments().is_empty());
  319. for (auto const& segment : loader->text_segments()) {
  320. auto flags = static_cast<int>(VirtualMemoryRangeFlags::SyscallCode) | static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  321. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  322. VERIFY_NOT_REACHED();
  323. }
  324. }
  325. } else {
  326. for (auto const& segment : loader->text_segments()) {
  327. auto flags = static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  328. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  329. VERIFY_NOT_REACHED();
  330. }
  331. }
  332. }
  333. }
  334. drop_loader_promise("prot_exec"sv);
  335. for (auto& loader : objects.topological_order)
  336. loader->load_stage_4();
  337. return {};
  338. }
  339. static Result<void, DlErrorMessage> __dlclose(void* handle)
  340. {
  341. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlclose: {}", handle);
  342. pthread_mutex_lock(&s_loader_lock);
  343. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  344. // FIXME: this will not currently destroy the dynamic object
  345. // because we're intentionally holding a strong reference to it
  346. // via s_global_objects until there's proper unload support.
  347. auto object = static_cast<ELF::DynamicObject*>(handle);
  348. object->unref();
  349. return {};
  350. }
  351. static Optional<DlErrorMessage> verify_tls_for_dlopen(DynamicLoader const& loader)
  352. {
  353. if (loader.tls_size_of_current_object() == 0)
  354. return {};
  355. if (s_tls_data.total_tls_size + loader.tls_size_of_current_object() + loader.tls_alignment_of_current_object() > s_tls_data.tls_template_size)
  356. return DlErrorMessage("TLS size too large");
  357. bool tls_data_is_all_zero = true;
  358. loader.image().for_each_program_header([&loader, &tls_data_is_all_zero](ELF::Image::ProgramHeader program_header) {
  359. if (program_header.type() != PT_TLS)
  360. return IterationDecision::Continue;
  361. auto* tls_data = (u8 const*)loader.image().base_address() + program_header.offset();
  362. for (size_t i = 0; i < program_header.size_in_image(); ++i) {
  363. if (tls_data[i] != 0) {
  364. tls_data_is_all_zero = false;
  365. break;
  366. }
  367. }
  368. return IterationDecision::Break;
  369. });
  370. if (tls_data_is_all_zero)
  371. return {};
  372. return DlErrorMessage("Using dlopen() with libraries that have non-zeroed TLS is currently not supported");
  373. }
  374. static Result<void*, DlErrorMessage> __dlopen(char const* filename, int flags)
  375. {
  376. // FIXME: RTLD_NOW and RTLD_LOCAL are not supported
  377. flags &= ~RTLD_NOW;
  378. flags |= RTLD_LAZY;
  379. flags &= ~RTLD_LOCAL;
  380. flags |= RTLD_GLOBAL;
  381. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlopen invoked, filename={}, flags={}", filename, flags);
  382. if (pthread_mutex_trylock(&s_loader_lock) != 0)
  383. return DlErrorMessage { "Nested calls to dlopen() are not permitted." };
  384. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  385. // FIXME: We must resolve filename relative to the caller, not the main executable.
  386. auto const& [name, parent_object] = *s_global_objects.begin();
  387. VERIFY(name == s_main_program_path);
  388. auto library_path = (filename ? DynamicLinker::resolve_library(filename, parent_object) : s_main_program_path);
  389. if (!library_path.has_value())
  390. return DlErrorMessage { ByteString::formatted("Could not find required shared library: {}", filename) };
  391. auto existing_elf_object = s_global_objects.get(library_path.value());
  392. if (existing_elf_object.has_value()) {
  393. // It's up to the caller to release the ref with dlclose().
  394. existing_elf_object.value()->ref();
  395. return *existing_elf_object;
  396. }
  397. auto loader = TRY(map_library(library_path.value()));
  398. // FIXME: This only checks main shared object but not its dependencies.
  399. if (auto error = verify_tls_for_dlopen(loader); error.has_value())
  400. return error.value();
  401. auto objects = TRY(map_dependencies(loader));
  402. TRY(link_main_library(flags, objects));
  403. s_tls_data.total_tls_size += loader->tls_size_of_current_object() + loader->tls_alignment_of_current_object();
  404. auto object = s_global_objects.get(library_path.value());
  405. if (!object.has_value())
  406. return DlErrorMessage { "Could not load ELF object." };
  407. // It's up to the caller to release the ref with dlclose().
  408. object.value()->ref();
  409. return *object;
  410. }
  411. static Result<void*, DlErrorMessage> __dlsym(void* handle, char const* symbol_name)
  412. {
  413. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlsym: {}, {}", handle, symbol_name);
  414. pthread_mutex_lock(&s_loader_lock);
  415. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  416. StringView symbol_name_view { symbol_name, strlen(symbol_name) };
  417. Optional<DynamicObject::SymbolLookupResult> symbol;
  418. if (handle) {
  419. auto object = static_cast<DynamicObject*>(handle);
  420. symbol = object->lookup_symbol(symbol_name_view);
  421. } else {
  422. // When handle is 0 (RTLD_DEFAULT) we should look up the symbol in all global modules
  423. // https://pubs.opengroup.org/onlinepubs/009604499/functions/dlsym.html
  424. symbol = DynamicLinker::lookup_global_symbol(symbol_name_view);
  425. }
  426. if (!symbol.has_value())
  427. return DlErrorMessage { ByteString::formatted("Symbol {} not found", symbol_name_view) };
  428. if (symbol.value().type == STT_GNU_IFUNC)
  429. return (void*)reinterpret_cast<DynamicObject::IfuncResolver>(symbol.value().address.as_ptr())();
  430. return symbol.value().address.as_ptr();
  431. }
  432. static Result<void, DlErrorMessage> __dladdr(void const* addr, Dl_info* info)
  433. {
  434. VirtualAddress user_addr { addr };
  435. pthread_mutex_lock(&s_loader_lock);
  436. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  437. RefPtr<DynamicObject> best_matching_library;
  438. VirtualAddress best_library_offset;
  439. for (auto& lib : s_global_objects) {
  440. if (user_addr < lib.value->base_address())
  441. continue;
  442. auto offset = user_addr - lib.value->base_address();
  443. if (!best_matching_library || offset < best_library_offset) {
  444. best_matching_library = lib.value;
  445. best_library_offset = offset;
  446. }
  447. }
  448. if (!best_matching_library) {
  449. return DlErrorMessage { "No library found which contains the specified address" };
  450. }
  451. Optional<DynamicObject::Symbol> best_matching_symbol;
  452. best_matching_library->for_each_symbol([&](auto const& symbol) {
  453. if (user_addr < symbol.address() || user_addr > symbol.address().offset(symbol.size()))
  454. return;
  455. best_matching_symbol = symbol;
  456. });
  457. info->dli_fbase = best_matching_library->base_address().as_ptr();
  458. // This works because we don't support unloading objects.
  459. info->dli_fname = best_matching_library->filepath().characters();
  460. if (best_matching_symbol.has_value()) {
  461. info->dli_saddr = best_matching_symbol.value().address().as_ptr();
  462. info->dli_sname = best_matching_symbol.value().raw_name();
  463. } else {
  464. info->dli_saddr = nullptr;
  465. info->dli_sname = nullptr;
  466. }
  467. return {};
  468. }
  469. static void __call_fini_functions()
  470. {
  471. typedef void (*FiniFunc)();
  472. // FIXME: This is not and never has been the correct order to call finalizers in.
  473. for (auto& it : s_global_objects) {
  474. auto object = it.value;
  475. if (object->has_fini_array_section()) {
  476. auto fini_array_section = object->fini_array_section();
  477. FiniFunc* fini_begin = (FiniFunc*)(fini_array_section.address().as_ptr());
  478. FiniFunc* fini_end = fini_begin + fini_array_section.entry_count();
  479. while (fini_begin != fini_end) {
  480. --fini_end;
  481. // Android sources claim that these can be -1, to be ignored.
  482. // 0 deffiniely shows up. Apparently 0/-1 are valid? Confusing.
  483. if (!*fini_end || ((FlatPtr)*fini_end == (FlatPtr)-1))
  484. continue;
  485. (*fini_end)();
  486. }
  487. }
  488. if (object->has_fini_section()) {
  489. auto fini_function = object->fini_section_function();
  490. (fini_function)();
  491. }
  492. }
  493. }
  494. static char** __environ_value()
  495. {
  496. return s_envp;
  497. }
  498. static void read_environment_variables()
  499. {
  500. for (char** env = s_envp; *env; ++env) {
  501. StringView env_string { *env, strlen(*env) };
  502. if (env_string == "_LOADER_BREAKPOINT=1"sv) {
  503. s_do_breakpoint_trap_before_entry = true;
  504. }
  505. constexpr auto library_path_string = "LD_LIBRARY_PATH="sv;
  506. if (env_string.starts_with(library_path_string)) {
  507. s_ld_library_path = env_string.substring_view(library_path_string.length());
  508. }
  509. constexpr auto main_pledge_promises_key = "_LOADER_MAIN_PROGRAM_PLEDGE_PROMISES="sv;
  510. if (env_string.starts_with(main_pledge_promises_key)) {
  511. s_main_program_pledge_promises = env_string.substring_view(main_pledge_promises_key.length());
  512. }
  513. constexpr auto loader_pledge_promises_key = "_LOADER_PLEDGE_PROMISES="sv;
  514. if (env_string.starts_with(loader_pledge_promises_key)) {
  515. s_loader_pledge_promises = env_string.substring_view(loader_pledge_promises_key.length());
  516. }
  517. }
  518. }
  519. EntryPointFunction ELF::DynamicLinker::linker_main(ByteString&& main_program_path, int main_program_fd, bool is_secure, char** envp)
  520. {
  521. VERIFY(main_program_path.starts_with('/'));
  522. s_envp = envp;
  523. auto define_magic_function = [&](StringView name, auto function) {
  524. s_magic_functions.set(name,
  525. DynamicObject::SymbolLookupResult {
  526. .size = 8,
  527. .address = VirtualAddress { reinterpret_cast<void*>(function) },
  528. .bind = STB_GLOBAL,
  529. .type = STT_FUNC,
  530. });
  531. };
  532. define_magic_function("__call_fini_functions"sv, __call_fini_functions);
  533. define_magic_function("__create_new_tls_region"sv, __create_new_tls_region);
  534. define_magic_function("__dl_iterate_phdr"sv, __dl_iterate_phdr);
  535. define_magic_function("__dladdr"sv, __dladdr);
  536. define_magic_function("__dlclose"sv, __dlclose);
  537. define_magic_function("__dlopen"sv, __dlopen);
  538. define_magic_function("__dlsym"sv, __dlsym);
  539. define_magic_function("__environ_value"sv, __environ_value);
  540. define_magic_function("__free_tls_region"sv, __free_tls_region);
  541. char* raw_current_directory = getcwd(nullptr, 0);
  542. s_cwd = raw_current_directory;
  543. free(raw_current_directory);
  544. s_allowed_to_check_environment_variables = !is_secure;
  545. if (s_allowed_to_check_environment_variables)
  546. read_environment_variables();
  547. s_main_program_path = main_program_path;
  548. // NOTE: We always map the main library first, since it may require
  549. // placement at a specific address.
  550. auto result1 = map_library(main_program_path, main_program_fd);
  551. if (result1.is_error()) {
  552. warnln("{}", result1.error().text);
  553. fflush(stderr);
  554. _exit(1);
  555. }
  556. auto executable = result1.release_value();
  557. size_t needed_dependencies = 0;
  558. executable->for_each_needed_library([&needed_dependencies](auto) {
  559. needed_dependencies++;
  560. });
  561. bool has_interpreter = false;
  562. executable->image().for_each_program_header([&has_interpreter](const ELF::Image::ProgramHeader& program_header) {
  563. if (program_header.type() == PT_INTERP)
  564. has_interpreter = true;
  565. });
  566. // NOTE: Refuse to run a program if it has a dynamic section,
  567. // it is pie, and does not have an interpreter or needed libraries
  568. // which is also called "static-pie". These binaries are probably
  569. // some sort of ELF packers or dynamic loaders, and there's no added
  570. // value in trying to run them, as they will probably crash due to trying
  571. // to invoke syscalls from a non-syscall memory executable (code) region.
  572. if (executable->is_dynamic() && (!has_interpreter || needed_dependencies == 0) && executable->dynamic_object().is_pie()) {
  573. char const message[] = R"(error: the dynamic loader can't reasonably run static-pie ELF. static-pie ELFs might run executable code that invokes syscalls
  574. outside of the defined syscall memory executable (code) region security measure we implement.
  575. Examples of static-pie ELF objects are ELF packers, and the system dynamic loader itself.)";
  576. fprintf(stderr, "%s", message);
  577. fflush(stderr);
  578. _exit(1);
  579. }
  580. auto result2 = map_dependencies(executable);
  581. if (result2.is_error()) {
  582. warnln("{}", result2.error().text);
  583. fflush(stderr);
  584. _exit(1);
  585. }
  586. auto objects = result2.release_value();
  587. dbgln_if(DYNAMIC_LOAD_DEBUG, "loaded all dependencies");
  588. for ([[maybe_unused]] auto& object : objects.load_order) {
  589. dbgln_if(DYNAMIC_LOAD_DEBUG, "{} - tls size: {}, tls alignment: {}, tls offset: {}",
  590. object->filepath(), object->tls_size_of_current_object(), object->tls_alignment_of_current_object(), object->tls_offset());
  591. }
  592. allocate_tls(objects.load_order);
  593. auto result = link_main_library(RTLD_GLOBAL | RTLD_LAZY, objects);
  594. if (result.is_error()) {
  595. warnln("{}", result.error().text);
  596. _exit(1);
  597. }
  598. drop_loader_promise("rpath"sv);
  599. auto& main_executable_loader = objects.load_order.first();
  600. auto entry_point = main_executable_loader->image().entry();
  601. if (main_executable_loader->is_dynamic())
  602. entry_point = entry_point.offset(main_executable_loader->base_address().get());
  603. auto entry_point_function = reinterpret_cast<EntryPointFunction>(entry_point.as_ptr());
  604. int rc = syscall(SC_prctl, PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS, 0, 0, nullptr);
  605. if (rc < 0) {
  606. VERIFY_NOT_REACHED();
  607. }
  608. rc = syscall(SC_prctl, PR_SET_NO_TRANSITION_TO_EXECUTABLE_FROM_WRITABLE_PROT, 0, 0, nullptr);
  609. if (rc < 0) {
  610. VERIFY_NOT_REACHED();
  611. }
  612. dbgln_if(DYNAMIC_LOAD_DEBUG, "Jumping to entry point: {:p}", entry_point_function);
  613. if (s_do_breakpoint_trap_before_entry) {
  614. #if ARCH(AARCH64)
  615. asm("brk #0");
  616. #elif ARCH(RISCV64)
  617. asm("ebreak");
  618. #elif ARCH(X86_64)
  619. asm("int3");
  620. #else
  621. # error "Unknown architecture"
  622. #endif
  623. }
  624. return entry_point_function;
  625. }
  626. }