DynamicLoader: Add an option to dry-run an ELF exectuable

This option is useful for measuring time of dynamic linking and
profiling the DynamicLoader itself.
This commit is contained in:
Liav A 2023-09-16 12:14:10 +03:00 committed by Andrew Kaster
parent 8fc1f470ef
commit a5e2694ebf
Notes: sideshowbarker 2024-07-17 18:13:59 +09:00

View file

@ -55,11 +55,13 @@ static int _main(int argc, char** argv, char** envp, bool is_secure)
for (int i = 0; i < argc; ++i)
arguments.unchecked_append({ argv[i], strlen(argv[i]) });
bool flag_dry_run { false };
Vector<StringView> command;
Core::ArgsParser args_parser;
args_parser.set_general_help("Run dynamically-linked ELF executables");
args_parser.set_stop_on_first_non_option(true);
args_parser.add_option(flag_dry_run, "Run in dry-run mode", "dry-run", 'd');
args_parser.add_positional_argument(command, "Command to execute", "command");
// NOTE: Don't use regular PrintUsageAndExit policy for ArgsParser, as it will simply
// fail with a nullptr-dereference as the LibC exit function is not suitable for usage
@ -90,6 +92,8 @@ static int _main(int argc, char** argv, char** envp, bool is_secure)
argv[index] = const_cast<char*>(command_with_args[index].characters_without_null_termination());
auto entry_point = ELF::DynamicLinker::linker_main(move(main_program_path), main_program_fd, is_secure, envp);
if (flag_dry_run)
return 0;
_invoke_entry(command.size(), argv, envp, entry_point);
VERIFY_NOT_REACHED();
}