summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorJesse Buhagiar <jooster669@gmail.com>2022-01-02 14:59:55 +1100
committerIdan Horowitz <idan.horowitz@gmail.com>2022-01-05 15:01:14 +0200
commit48c93500365f719e255cc0155019f9f7a9fe06d7 (patch)
tree77a3891ec1295f37912072f3999f15a41ca33451 /Userland/Libraries
parent360cdabcdc6967aaa5449ad80734575653295126 (diff)
downloadserenity-48c93500365f719e255cc0155019f9f7a9fe06d7.zip
LibELF: Add `LD_LIBRARY_PATH` envvar support :^)
The dynamic linker now supports having custom library paths as specified by the user.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibELF/DynamicLinker.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/Userland/Libraries/LibELF/DynamicLinker.cpp b/Userland/Libraries/LibELF/DynamicLinker.cpp
index 418bc14411..b66b6401b5 100644
--- a/Userland/Libraries/LibELF/DynamicLinker.cpp
+++ b/Userland/Libraries/LibELF/DynamicLinker.cpp
@@ -2,6 +2,7 @@
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
+ * Copyright (c) 2022, Jesse Buhagiar <jooster669@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -52,6 +53,7 @@ static __pthread_mutex_t s_loader_lock = __PTHREAD_MUTEX_INITIALIZER;
static bool s_allowed_to_check_environment_variables { false };
static bool s_do_breakpoint_trap_before_entry { false };
+static StringView s_ld_library_path;
static Result<void, DlErrorMessage> __dlclose(void* handle);
static Result<void*, DlErrorMessage> __dlopen(const char* filename, int flags);
@@ -108,6 +110,18 @@ static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(const St
return map_library(name, fd);
}
+ // Scan the LD_LIBRARY_PATH environment variable if applicable
+ if (s_ld_library_path != nullptr) {
+ for (const auto& search_path : s_ld_library_path.split_view(':')) {
+ LexicalPath library_path(search_path);
+ int fd = open(library_path.append(name).string().characters(), O_RDONLY);
+ if (fd < 0)
+ continue;
+ return map_library(name, fd);
+ }
+ }
+
+ // Now check the default paths.
// TODO: Do we want to also look for libs in other paths too?
const char* search_paths[] = { "/usr/lib/{}", "/usr/local/lib/{}" };
for (auto& search_path : search_paths) {
@@ -494,9 +508,15 @@ static Result<void, DlErrorMessage> __dladdr(void* addr, Dl_info* info)
static void read_environment_variables()
{
for (char** env = s_envp; *env; ++env) {
- if (StringView { *env } == "_LOADER_BREAKPOINT=1"sv) {
+ StringView env_string { *env };
+ if (env_string == "_LOADER_BREAKPOINT=1"sv) {
s_do_breakpoint_trap_before_entry = true;
}
+
+ constexpr auto library_path_string = "LD_LIBRARY_PATH="sv;
+ if (env_string.starts_with(library_path_string)) {
+ s_ld_library_path = env_string.substring_view(library_path_string.length());
+ }
}
}