Skip to content

Commit 146fac2

Browse files
William Marlowawesomekling
authored andcommitted
DynamicLoader: Handle Loader.so being invoked directly as an executable
Loader.so is an actual executable, as well as the interpreter for dynamic libraries. Currently launching Loader.so as a standalone executable results in an obsucre crash as it tries to load itself over itself. Now we at least print a helpful message saying that you're doing the wrong thing and exit gracefully. In future we may wish to allow users to specify additional options to learn more about what's going on during dynamic linking, such as ld-linux.so.2 on Linux.
1 parent a7c0141 commit 146fac2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Userland/DynamicLoader/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,20 @@ static void clear_temporary_objects_mappings()
250250
g_loaders.clear();
251251
}
252252

253+
static void display_help()
254+
{
255+
const char message[] =
256+
R"(You have invoked `Loader.so'. This is the helper program for programs that
257+
use shared libraries. Special directives embedded in executables tell the
258+
kernel to load this program.
259+
260+
This helper program loads the shared libraries needed by the program,
261+
prepares the program to run, and runs it. You do not need to invoke
262+
this helper program directly.
263+
)";
264+
write(1, message, sizeof(message));
265+
}
266+
253267
static FlatPtr loader_main(auxv_t* auxvp)
254268
{
255269
int main_program_fd = -1;
@@ -265,6 +279,15 @@ static FlatPtr loader_main(auxv_t* auxvp)
265279
ASSERT(main_program_fd >= 0);
266280
ASSERT(!main_program_name.is_null());
267281

282+
if (main_program_name == "/usr/lib/Loader.so") {
283+
// We've been invoked directly as an executable rather than as the
284+
// ELF interpreter for some other binary. In the future we may want
285+
// to support launching a program directly from the dynamic loader
286+
// like ld.so on Linux.
287+
display_help();
288+
_exit(1);
289+
}
290+
268291
map_library(main_program_name, main_program_fd);
269292
map_dependencies(main_program_name);
270293

0 commit comments

Comments
 (0)