From e82d19822ee225e75c22d0362e34e970962adf89 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Sun, 31 May 2020 22:37:33 +0000 Subject: [PATCH] rtld: Add -b option to allow to specify image name different from arg0. Sponsored by: The FreeBSD Foundation MFC after: 1 week --- libexec/rtld-elf/rtld.1 | 14 +++++++++++++- libexec/rtld-elf/rtld.c | 32 +++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/libexec/rtld-elf/rtld.1 b/libexec/rtld-elf/rtld.1 index 5eeb17a841bf..ed2ce96eac8f 100644 --- a/libexec/rtld-elf/rtld.1 +++ b/libexec/rtld-elf/rtld.1 @@ -28,7 +28,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 20, 2017 +.Dd June 1, 2020 .Dt RTLD 1 .Os .Sh NAME @@ -302,6 +302,7 @@ Execution options may be specified. The syntax of the direct invocation is .Bd -ragged -offset indent .Pa /libexec/ld-elf.so.1 +.Op Fl b Ar exe .Op Fl f Ar fd .Op Fl p .Op Fl - @@ -311,6 +312,17 @@ The syntax of the direct invocation is .Pp The options are: .Bl -tag -width indent +.It Fl b Ar exe +Use the executable +.Fa exe +instead of +.Fa image_path +for activation. +If this option is specified, +.Ar image_path +is only used to provide the +.Va argv[0] +value to the program. .It Fl f Ar fd File descriptor .Ar fd diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index cf1110d47bd8..82376e85bc07 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -136,7 +136,8 @@ static void objlist_put_after(Objlist *, Obj_Entry *, Obj_Entry *); static void objlist_remove(Objlist *, Obj_Entry *); static int open_binary_fd(const char *argv0, bool search_in_path, const char **binpath_res); -static int parse_args(char* argv[], int argc, bool *use_pathp, int *fdp); +static int parse_args(char* argv[], int argc, bool *use_pathp, int *fdp, + const char **argv0); static int parse_integer(const char *); static void *path_enumerate(const char *, path_enum_proc, const char *, void *); static void print_usage(const char *argv0); @@ -467,8 +468,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) } dbg("opening main program in direct exec mode"); if (argc >= 2) { - rtld_argc = parse_args(argv, argc, &search_in_path, &fd); - argv0 = argv[rtld_argc]; + rtld_argc = parse_args(argv, argc, &search_in_path, &fd, &argv0); explicit_fd = (fd != -1); binpath = NULL; if (!explicit_fd) @@ -5610,17 +5610,20 @@ open_binary_fd(const char *argv0, bool search_in_path, * Parse a set of command-line arguments. */ static int -parse_args(char* argv[], int argc, bool *use_pathp, int *fdp) +parse_args(char* argv[], int argc, bool *use_pathp, int *fdp, + const char **argv0) { const char *arg; char machine[64]; size_t sz; int arglen, fd, i, j, mib[2]; char opt; + bool seen_b, seen_f; dbg("Parsing command-line arguments"); *use_pathp = false; *fdp = -1; + seen_b = seen_f = false; for (i = 1; i < argc; i++ ) { arg = argv[i]; @@ -5647,7 +5650,21 @@ parse_args(char* argv[], int argc, bool *use_pathp, int *fdp) if (opt == 'h') { print_usage(argv[0]); _exit(0); + } else if (opt == 'b') { + if (seen_f) { + _rtld_error("Both -b and -f specified"); + rtld_die(); + } + i++; + *argv0 = argv[i]; + seen_b = true; + break; } else if (opt == 'f') { + if (seen_b) { + _rtld_error("Both -b and -f specified"); + rtld_die(); + } + /* * -f XX can be used to specify a * descriptor for the binary named at @@ -5671,6 +5688,7 @@ parse_args(char* argv[], int argc, bool *use_pathp, int *fdp) rtld_die(); } *fdp = fd; + seen_f = true; break; } else if (opt == 'p') { *use_pathp = true; @@ -5700,6 +5718,8 @@ parse_args(char* argv[], int argc, bool *use_pathp, int *fdp) } } + if (!seen_b) + *argv0 = argv[i]; return (i); } @@ -5734,10 +5754,12 @@ static void print_usage(const char *argv0) { - rtld_printf("Usage: %s [-h] [-f ] [-p] [--] []\n" + rtld_printf( + "Usage: %s [-h] [-b ] [-f ] [-p] [--] []\n" "\n" "Options:\n" " -h Display this help message\n" + " -b Execute instead of , arg0 is \n" " -f Execute instead of searching for \n" " -p Search in PATH for named binary\n" " -v Display identification information\n"