DynamicLinker.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. static void initialize_libc(DynamicObject& libc)
  270. {
  271. auto res = libc.lookup_symbol("__libc_init"sv);
  272. VERIFY(res.has_value());
  273. using libc_init_func = decltype(__libc_init);
  274. ((libc_init_func*)res.value().address.as_ptr())();
  275. }
  276. static void drop_loader_promise(StringView promise_to_drop)
  277. {
  278. if (s_main_program_pledge_promises.is_empty() || s_loader_pledge_promises.is_empty())
  279. return;
  280. s_loader_pledge_promises = s_loader_pledge_promises.replace(promise_to_drop, ""sv, ReplaceMode::All);
  281. auto extended_promises = ByteString::formatted("{} {}", s_main_program_pledge_promises, s_loader_pledge_promises);
  282. Syscall::SC_pledge_params params {
  283. { extended_promises.characters(), extended_promises.length() },
  284. { nullptr, 0 },
  285. };
  286. int rc = syscall(SC_pledge, &params);
  287. if (rc < 0 && rc > -EMAXERRNO) {
  288. warnln("Failed to drop loader pledge promise: {}. errno={}", promise_to_drop, errno);
  289. _exit(1);
  290. }
  291. }
  292. static ErrorOr<void, DlErrorMessage> link_main_library(int flags, DependencyOrdering const& objects)
  293. {
  294. // Verify that all objects are already mapped
  295. for (auto& loader : objects.load_order)
  296. VERIFY(!loader->map());
  297. // FIXME: Are there any observable differences between doing stages 2 and 3 in topological vs
  298. // load order? POSIX says to do relocations in load order but does the order really
  299. // matter here?
  300. for (auto& loader : objects.load_order) {
  301. bool success = loader->link(flags);
  302. if (!success) {
  303. return DlErrorMessage { ByteString::formatted("Failed to link library {}", loader->filepath()) };
  304. }
  305. }
  306. for (auto& loader : objects.load_order) {
  307. auto result = loader->load_stage_3(flags);
  308. VERIFY(!result.is_error());
  309. auto& object = result.value();
  310. if (loader->filepath().ends_with("/libc.so"sv)) {
  311. initialize_libc(*object);
  312. }
  313. if (loader->filepath().ends_with("/libsystem.so"sv)) {
  314. VERIFY(!loader->text_segments().is_empty());
  315. for (auto const& segment : loader->text_segments()) {
  316. auto flags = static_cast<int>(VirtualMemoryRangeFlags::SyscallCode) | static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  317. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  318. VERIFY_NOT_REACHED();
  319. }
  320. }
  321. } else {
  322. for (auto const& segment : loader->text_segments()) {
  323. auto flags = static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  324. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  325. VERIFY_NOT_REACHED();
  326. }
  327. }
  328. }
  329. }
  330. drop_loader_promise("prot_exec"sv);
  331. for (auto& loader : objects.topological_order)
  332. loader->load_stage_4();
  333. return {};
  334. }
  335. static Result<void, DlErrorMessage> __dlclose(void* handle)
  336. {
  337. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlclose: {}", handle);
  338. pthread_mutex_lock(&s_loader_lock);
  339. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  340. // FIXME: this will not currently destroy the dynamic object
  341. // because we're intentionally holding a strong reference to it
  342. // via s_global_objects until there's proper unload support.
  343. auto object = static_cast<ELF::DynamicObject*>(handle);
  344. object->unref();
  345. return {};
  346. }
  347. static Optional<DlErrorMessage> verify_tls_for_dlopen(DynamicLoader const& loader)
  348. {
  349. if (loader.tls_size_of_current_object() == 0)
  350. return {};
  351. 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)
  352. return DlErrorMessage("TLS size too large");
  353. bool tls_data_is_all_zero = true;
  354. loader.image().for_each_program_header([&loader, &tls_data_is_all_zero](ELF::Image::ProgramHeader program_header) {
  355. if (program_header.type() != PT_TLS)
  356. return IterationDecision::Continue;
  357. auto* tls_data = (u8 const*)loader.image().base_address() + program_header.offset();
  358. for (size_t i = 0; i < program_header.size_in_image(); ++i) {
  359. if (tls_data[i] != 0) {
  360. tls_data_is_all_zero = false;
  361. break;
  362. }
  363. }
  364. return IterationDecision::Break;
  365. });
  366. if (tls_data_is_all_zero)
  367. return {};
  368. return DlErrorMessage("Using dlopen() with libraries that have non-zeroed TLS is currently not supported");
  369. }
  370. static Result<void*, DlErrorMessage> __dlopen(char const* filename, int flags)
  371. {
  372. // FIXME: RTLD_NOW and RTLD_LOCAL are not supported
  373. flags &= ~RTLD_NOW;
  374. flags |= RTLD_LAZY;
  375. flags &= ~RTLD_LOCAL;
  376. flags |= RTLD_GLOBAL;
  377. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlopen invoked, filename={}, flags={}", filename, flags);
  378. if (pthread_mutex_trylock(&s_loader_lock) != 0)
  379. return DlErrorMessage { "Nested calls to dlopen() are not permitted." };
  380. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  381. // FIXME: We must resolve filename relative to the caller, not the main executable.
  382. auto const& [name, parent_object] = *s_global_objects.begin();
  383. VERIFY(name == s_main_program_path);
  384. auto library_path = (filename ? DynamicLinker::resolve_library(filename, parent_object) : s_main_program_path);
  385. if (!library_path.has_value())
  386. return DlErrorMessage { ByteString::formatted("Could not find required shared library: {}", filename) };
  387. auto existing_elf_object = s_global_objects.get(library_path.value());
  388. if (existing_elf_object.has_value()) {
  389. // It's up to the caller to release the ref with dlclose().
  390. existing_elf_object.value()->ref();
  391. return *existing_elf_object;
  392. }
  393. auto loader = TRY(map_library(library_path.value()));
  394. // FIXME: This only checks main shared object but not its dependencies.
  395. if (auto error = verify_tls_for_dlopen(loader); error.has_value())
  396. return error.value();
  397. auto objects = TRY(map_dependencies(loader));
  398. TRY(link_main_library(flags, objects));
  399. s_tls_data.total_tls_size += loader->tls_size_of_current_object() + loader->tls_alignment_of_current_object();
  400. auto object = s_global_objects.get(library_path.value());
  401. if (!object.has_value())
  402. return DlErrorMessage { "Could not load ELF object." };
  403. // It's up to the caller to release the ref with dlclose().
  404. object.value()->ref();
  405. return *object;
  406. }
  407. static Result<void*, DlErrorMessage> __dlsym(void* handle, char const* symbol_name)
  408. {
  409. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlsym: {}, {}", handle, symbol_name);
  410. pthread_mutex_lock(&s_loader_lock);
  411. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  412. StringView symbol_name_view { symbol_name, strlen(symbol_name) };
  413. Optional<DynamicObject::SymbolLookupResult> symbol;
  414. if (handle) {
  415. auto object = static_cast<DynamicObject*>(handle);
  416. symbol = object->lookup_symbol(symbol_name_view);
  417. } else {
  418. // When handle is 0 (RTLD_DEFAULT) we should look up the symbol in all global modules
  419. // https://pubs.opengroup.org/onlinepubs/009604499/functions/dlsym.html
  420. symbol = DynamicLinker::lookup_global_symbol(symbol_name_view);
  421. }
  422. if (!symbol.has_value())
  423. return DlErrorMessage { ByteString::formatted("Symbol {} not found", symbol_name_view) };
  424. if (symbol.value().type == STT_GNU_IFUNC)
  425. return (void*)reinterpret_cast<DynamicObject::IfuncResolver>(symbol.value().address.as_ptr())();
  426. return symbol.value().address.as_ptr();
  427. }
  428. static Result<void, DlErrorMessage> __dladdr(void const* addr, Dl_info* info)
  429. {
  430. VirtualAddress user_addr { addr };
  431. pthread_mutex_lock(&s_loader_lock);
  432. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  433. RefPtr<DynamicObject> best_matching_library;
  434. VirtualAddress best_library_offset;
  435. for (auto& lib : s_global_objects) {
  436. if (user_addr < lib.value->base_address())
  437. continue;
  438. auto offset = user_addr - lib.value->base_address();
  439. if (!best_matching_library || offset < best_library_offset) {
  440. best_matching_library = lib.value;
  441. best_library_offset = offset;
  442. }
  443. }
  444. if (!best_matching_library) {
  445. return DlErrorMessage { "No library found which contains the specified address" };
  446. }
  447. Optional<DynamicObject::Symbol> best_matching_symbol;
  448. best_matching_library->for_each_symbol([&](auto const& symbol) {
  449. if (user_addr < symbol.address() || user_addr > symbol.address().offset(symbol.size()))
  450. return;
  451. best_matching_symbol = symbol;
  452. });
  453. info->dli_fbase = best_matching_library->base_address().as_ptr();
  454. // This works because we don't support unloading objects.
  455. info->dli_fname = best_matching_library->filepath().characters();
  456. if (best_matching_symbol.has_value()) {
  457. info->dli_saddr = best_matching_symbol.value().address().as_ptr();
  458. info->dli_sname = best_matching_symbol.value().raw_name();
  459. } else {
  460. info->dli_saddr = nullptr;
  461. info->dli_sname = nullptr;
  462. }
  463. return {};
  464. }
  465. static void __call_fini_functions()
  466. {
  467. typedef void (*FiniFunc)();
  468. // FIXME: This is not and never has been the correct order to call finalizers in.
  469. for (auto& it : s_global_objects) {
  470. auto object = it.value;
  471. if (object->has_fini_array_section()) {
  472. auto fini_array_section = object->fini_array_section();
  473. FiniFunc* fini_begin = (FiniFunc*)(fini_array_section.address().as_ptr());
  474. FiniFunc* fini_end = fini_begin + fini_array_section.entry_count();
  475. while (fini_begin != fini_end) {
  476. --fini_end;
  477. // Android sources claim that these can be -1, to be ignored.
  478. // 0 deffiniely shows up. Apparently 0/-1 are valid? Confusing.
  479. if (!*fini_end || ((FlatPtr)*fini_end == (FlatPtr)-1))
  480. continue;
  481. (*fini_end)();
  482. }
  483. }
  484. if (object->has_fini_section()) {
  485. auto fini_function = object->fini_section_function();
  486. (fini_function)();
  487. }
  488. }
  489. }
  490. static char** __environ_value()
  491. {
  492. return s_envp;
  493. }
  494. static void read_environment_variables()
  495. {
  496. for (char** env = s_envp; *env; ++env) {
  497. StringView env_string { *env, strlen(*env) };
  498. if (env_string == "_LOADER_BREAKPOINT=1"sv) {
  499. s_do_breakpoint_trap_before_entry = true;
  500. }
  501. constexpr auto library_path_string = "LD_LIBRARY_PATH="sv;
  502. if (env_string.starts_with(library_path_string)) {
  503. s_ld_library_path = env_string.substring_view(library_path_string.length());
  504. }
  505. constexpr auto main_pledge_promises_key = "_LOADER_MAIN_PROGRAM_PLEDGE_PROMISES="sv;
  506. if (env_string.starts_with(main_pledge_promises_key)) {
  507. s_main_program_pledge_promises = env_string.substring_view(main_pledge_promises_key.length());
  508. }
  509. constexpr auto loader_pledge_promises_key = "_LOADER_PLEDGE_PROMISES="sv;
  510. if (env_string.starts_with(loader_pledge_promises_key)) {
  511. s_loader_pledge_promises = env_string.substring_view(loader_pledge_promises_key.length());
  512. }
  513. }
  514. }
  515. EntryPointFunction ELF::DynamicLinker::linker_main(ByteString&& main_program_path, int main_program_fd, bool is_secure, char** envp)
  516. {
  517. VERIFY(main_program_path.starts_with('/'));
  518. s_envp = envp;
  519. auto define_magic_function = [&](StringView name, auto function) {
  520. s_magic_functions.set(name,
  521. DynamicObject::SymbolLookupResult {
  522. .size = 8,
  523. .address = VirtualAddress { reinterpret_cast<void*>(function) },
  524. .bind = STB_GLOBAL,
  525. .type = STT_FUNC,
  526. });
  527. };
  528. define_magic_function("__call_fini_functions"sv, __call_fini_functions);
  529. define_magic_function("__create_new_tls_region"sv, __create_new_tls_region);
  530. define_magic_function("__dl_iterate_phdr"sv, __dl_iterate_phdr);
  531. define_magic_function("__dladdr"sv, __dladdr);
  532. define_magic_function("__dlclose"sv, __dlclose);
  533. define_magic_function("__dlopen"sv, __dlopen);
  534. define_magic_function("__dlsym"sv, __dlsym);
  535. define_magic_function("__environ_value"sv, __environ_value);
  536. define_magic_function("__free_tls_region"sv, __free_tls_region);
  537. char* raw_current_directory = getcwd(nullptr, 0);
  538. s_cwd = raw_current_directory;
  539. free(raw_current_directory);
  540. s_allowed_to_check_environment_variables = !is_secure;
  541. if (s_allowed_to_check_environment_variables)
  542. read_environment_variables();
  543. s_main_program_path = main_program_path;
  544. // NOTE: We always map the main library first, since it may require
  545. // placement at a specific address.
  546. auto result1 = map_library(main_program_path, main_program_fd);
  547. if (result1.is_error()) {
  548. warnln("{}", result1.error().text);
  549. fflush(stderr);
  550. _exit(1);
  551. }
  552. auto executable = result1.release_value();
  553. size_t needed_dependencies = 0;
  554. executable->for_each_needed_library([&needed_dependencies](auto) {
  555. needed_dependencies++;
  556. });
  557. bool has_interpreter = false;
  558. executable->image().for_each_program_header([&has_interpreter](const ELF::Image::ProgramHeader& program_header) {
  559. if (program_header.type() == PT_INTERP)
  560. has_interpreter = true;
  561. });
  562. // NOTE: Refuse to run a program if it has a dynamic section,
  563. // it is pie, and does not have an interpreter or needed libraries
  564. // which is also called "static-pie". These binaries are probably
  565. // some sort of ELF packers or dynamic loaders, and there's no added
  566. // value in trying to run them, as they will probably crash due to trying
  567. // to invoke syscalls from a non-syscall memory executable (code) region.
  568. if (executable->is_dynamic() && (!has_interpreter || needed_dependencies == 0) && executable->dynamic_object().is_pie()) {
  569. 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
  570. outside of the defined syscall memory executable (code) region security measure we implement.
  571. Examples of static-pie ELF objects are ELF packers, and the system dynamic loader itself.)";
  572. fprintf(stderr, "%s", message);
  573. fflush(stderr);
  574. _exit(1);
  575. }
  576. auto result2 = map_dependencies(executable);
  577. if (result2.is_error()) {
  578. warnln("{}", result2.error().text);
  579. fflush(stderr);
  580. _exit(1);
  581. }
  582. auto objects = result2.release_value();
  583. dbgln_if(DYNAMIC_LOAD_DEBUG, "loaded all dependencies");
  584. for ([[maybe_unused]] auto& object : objects.load_order) {
  585. dbgln_if(DYNAMIC_LOAD_DEBUG, "{} - tls size: {}, tls alignment: {}, tls offset: {}",
  586. object->filepath(), object->tls_size_of_current_object(), object->tls_alignment_of_current_object(), object->tls_offset());
  587. }
  588. allocate_tls(objects.load_order);
  589. auto result = link_main_library(RTLD_GLOBAL | RTLD_LAZY, objects);
  590. if (result.is_error()) {
  591. warnln("{}", result.error().text);
  592. _exit(1);
  593. }
  594. drop_loader_promise("rpath"sv);
  595. auto& main_executable_loader = objects.load_order.first();
  596. auto entry_point = main_executable_loader->image().entry();
  597. if (main_executable_loader->is_dynamic())
  598. entry_point = entry_point.offset(main_executable_loader->base_address().get());
  599. auto entry_point_function = reinterpret_cast<EntryPointFunction>(entry_point.as_ptr());
  600. int rc = syscall(SC_prctl, PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS, 1, 0, nullptr);
  601. if (rc < 0) {
  602. VERIFY_NOT_REACHED();
  603. }
  604. dbgln_if(DYNAMIC_LOAD_DEBUG, "Jumping to entry point: {:p}", entry_point_function);
  605. if (s_do_breakpoint_trap_before_entry) {
  606. #if ARCH(AARCH64)
  607. asm("brk #0");
  608. #elif ARCH(RISCV64)
  609. asm("ebreak");
  610. #elif ARCH(X86_64)
  611. asm("int3");
  612. #else
  613. # error "Unknown architecture"
  614. #endif
  615. }
  616. return entry_point_function;
  617. }
  618. }