From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by sourceware.org (Postfix) with ESMTPS id 7A58D3858D35 for ; Wed, 7 Oct 2020 17:16:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7A58D3858D35 Received: by mail-qt1-x843.google.com with SMTP id s47so2573395qth.4 for ; Wed, 07 Oct 2020 10:16:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:references:from:autocrypt:subject:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=cHsFzB6SvlsNNuQ3LS09Ju8O49TP/TRiipQPAS8WSpk=; b=pOZoR5kAqZ1+r+ku5xBdt2/qYphd3U6aWVrgMZvE3egX/Xt1VJ+4sSV0RWJMsWzdV1 BQWXNsmg/TV1+J3Tf+Swyb5rHzPb1GKQ/PqdtsCco0tRdtOmWUCBO3o85CBu3c3ixCiX Ozffkb6eeD27l/G/LX6KgyOafvSaaFRAHptBaxshcG6oNpB5oaeGC1GLxTPUZi5elIPP kSq3CoNa8Vm256mOI4UixtUWe3bU/uOFa/sS2AfsaXzR0z2U5fOQMEy5p2aNhmH7AaMI V00zP8szZBX74UsdshF9dwmiVbSm0/6IYlP9d0IdiSzZ2UyVjNpCBV9Wt5CG/H1NGbhe r1xQ== X-Gm-Message-State: AOAM5316XaQIbaqO5uf9fEwDPpylm3mHbYOqEoLuqfpcyy1fCmFAxRSG +dfAUVkub2OUG98bkL4yihkl5bqHf9SGJg== X-Google-Smtp-Source: ABdhPJyzqjoH/hRBhos0M4HAjE394h5hFK5uQ327qrPGzBJFXSaX7NgcMx8va8kXGGPeV+7YS6WQTA== X-Received: by 2002:ac8:4cc1:: with SMTP id l1mr4316572qtv.128.1602091014569; Wed, 07 Oct 2020 10:16:54 -0700 (PDT) Received: from [192.168.1.4] ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id e17sm1871389qte.11.2020.10.07.10.16.53 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 07 Oct 2020 10:16:54 -0700 (PDT) To: libc-alpha@sourceware.org References: <05bb7e01b9a1cb063ec4619285a08bcb25fd6d97.1601569371.git.fweimer@redhat.com> From: Adhemerval Zanella Autocrypt: addr=adhemerval.zanella@linaro.org; prefer-encrypt=mutual; keydata= mQINBFcVGkoBEADiQU2x/cBBmAVf5C2d1xgz6zCnlCefbqaflUBw4hB/bEME40QsrVzWZ5Nq 8kxkEczZzAOKkkvv4pRVLlLn/zDtFXhlcvQRJ3yFMGqzBjofucOrmdYkOGo0uCaoJKPT186L NWp53SACXguFJpnw4ODI64ziInzXQs/rUJqrFoVIlrPDmNv/LUv1OVPKz20ETjgfpg8MNwG6 iMizMefCl+RbtXbIEZ3TE/IaDT/jcOirjv96lBKrc/pAL0h/O71Kwbbp43fimW80GhjiaN2y WGByepnkAVP7FyNarhdDpJhoDmUk9yfwNuIuESaCQtfd3vgKKuo6grcKZ8bHy7IXX1XJj2X/ BgRVhVgMHAnDPFIkXtP+SiarkUaLjGzCz7XkUn4XAGDskBNfbizFqYUQCaL2FdbW3DeZqNIa nSzKAZK7Dm9+0VVSRZXP89w71Y7JUV56xL/PlOE+YKKFdEw+gQjQi0e+DZILAtFjJLoCrkEX w4LluMhYX/X8XP6/C3xW0yOZhvHYyn72sV4yJ1uyc/qz3OY32CRy+bwPzAMAkhdwcORA3JPb kPTlimhQqVgvca8m+MQ/JFZ6D+K7QPyvEv7bQ7M+IzFmTkOCwCJ3xqOD6GjX3aphk8Sr0dq3 4Awlf5xFDAG8dn8Uuutb7naGBd/fEv6t8dfkNyzj6yvc4jpVxwARAQABtElBZGhlbWVydmFs IFphbmVsbGEgTmV0dG8gKExpbmFybyBWUE4gS2V5KSA8YWRoZW1lcnZhbC56YW5lbGxhQGxp bmFyby5vcmc+iQI3B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BJ2d4+m/qO6+ cFTAceXmIoFsN8+gFN3i8Is3u12u8xGudcBPvpoy4OoG Subject: Re: [PATCH 07/28] elf: Implement ld.so --help Message-ID: <9e2b33e4-92a9-a238-cedd-e0ee87927bbe@linaro.org> Date: Wed, 7 Oct 2020 14:16:51 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <05bb7e01b9a1cb063ec4619285a08bcb25fd6d97.1601569371.git.fweimer@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Oct 2020 17:16:57 -0000 On 01/10/2020 13:32, Florian Weimer via Libc-alpha wrote: > --help processing is deferred to the point where the executable has > been loaded, so that it is possible to eventually include information > from the main executable in the help output. > > As suggested in the GNU command-line interface guidelines, the help > message is printed to standard output, and the exit status is > successful. > > Handle usage errors closer to the GNU command-line interface > guidelines. LGTM with some nits below. Reviewed-by: Adhemerval Zanella > --- > elf/dl-main.h | 9 +++++++-- > elf/dl-usage.c | 24 ++++++++++++++++++---- > elf/rtld.c | 55 ++++++++++++++++++++++++++++++++++++++++++-------- > 3 files changed, 74 insertions(+), 14 deletions(-) > > diff --git a/elf/dl-main.h b/elf/dl-main.h > index 68dd27d0d7..71ca5114de 100644 > --- a/elf/dl-main.h > +++ b/elf/dl-main.h > @@ -60,7 +60,7 @@ struct audit_list > }; > > /* This is a list of all the modes the dynamic loader can be in. */ > -enum mode { normal, list, verify, trace }; > +enum mode { normal, list, verify, trace, rtld_help }; > > /* Aggregated state information extracted from environment variables > and the ld.so command line. */ Ok. > @@ -98,6 +98,11 @@ call_init_paths (const struct dl_main_state *state) > } > > /* Print ld.so usage information and exit. */ > -void _dl_usage (void) attribute_hidden __attribute__ ((__noreturn__)); > +void _dl_usage (const char *argv0, const char *wrong_option) > + attribute_hidden __attribute__ ((__noreturn__)); > + > +/* Print ld.so --help output and exit. */ > +void _dl_help (const char *argv0, struct dl_main_state *state) > + attribute_hidden __attribute__ ((__noreturn__)); > > #endif /* _DL_MAIN */ Maybe use _Noreturn here? > diff --git a/elf/dl-usage.c b/elf/dl-usage.c > index f3d89d22b7..72ff99e70f 100644 > --- a/elf/dl-usage.c > +++ b/elf/dl-usage.c > @@ -19,12 +19,24 @@ > #include > #include > #include > +#include > > void > -_dl_usage (void) > +_dl_usage (const char *argv0, const char *wrong_option) > { > - _dl_fatal_printf ("\ > -Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ > + if (wrong_option != NULL) > + _dl_error_printf ("%s: unrecognized option '%s'\n", argv0, wrong_option); > + else > + _dl_error_printf ("%s: missing program name\n", argv0); > + _dl_error_printf ("Try '%s --help' for more information.\n", argv0); > + _exit (1); > +} > + Maybe EXIT_FAILURE here? > +void > +_dl_help (const char *argv0, struct dl_main_state *state) > +{ > + _dl_printf ("\ > +Usage: %s [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ > You have invoked `ld.so', the helper program for shared library executables.\n\ > This program usually lives in the file `/lib/ld.so', and special directives\n\ > in executable files using ELF shared libraries tell the system's program\n\ > @@ -47,5 +59,9 @@ of this helper program; chances are you did not intend to run this program.\n\ > in LIST\n\ > --audit LIST use objects named in LIST as auditors\n\ > --preload LIST preload objects named in LIST\n\ > - --argv0 STRING set argv[0] to STRING before running\n"); > + --argv0 STRING set argv[0] to STRING before running\n\ > + --help display this help and exit\n\ > +", > + argv0); > + _exit (0); > } Maybe EXIT_SUCCESS here? > diff --git a/elf/rtld.c b/elf/rtld.c > index d11fe22b83..5cdab3c99c 100644 > --- a/elf/rtld.c > +++ b/elf/rtld.c > @@ -1151,6 +1151,7 @@ dl_main (const ElfW(Phdr) *phdr, > _dl_starting_up = 1; > #endif > > + const char *ld_so_name = _dl_argv[0]; > if (*user_entry == (ElfW(Addr)) ENTRY_POINT) > { > /* Ho ho. We are not the program interpreter! We are the program > @@ -1178,8 +1179,12 @@ dl_main (const ElfW(Phdr) *phdr, > while (_dl_argc > 1) > if (! strcmp (_dl_argv[1], "--list")) > { > - state.mode = list; > - GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */ > + if (state.mode != rtld_help) > + { > + state.mode = list; > + /* This means do no dependency analysis. */ > + GLRO(dl_lazy) = -1; > + } > > ++_dl_skip_args; > --_dl_argc; Ok. > @@ -1187,7 +1192,8 @@ dl_main (const ElfW(Phdr) *phdr, > } > else if (! strcmp (_dl_argv[1], "--verify")) > { > - state.mode = verify; > + if (state.mode != rtld_help) > + state.mode = verify; > > ++_dl_skip_args; > --_dl_argc; Ok. > @@ -1242,13 +1248,34 @@ dl_main (const ElfW(Phdr) *phdr, > _dl_argc -= 2; > _dl_argv += 2; > } > + else if (strcmp (_dl_argv[1], "--help") == 0) > + { > + state.mode = rtld_help; > + --_dl_argc; > + ++_dl_argv; > + } > + else if (_dl_argv[1][0] == '-' && _dl_argv[1][1] == '-') > + { > + if (_dl_argv[1][1] == '\0') > + /* End of option list. */ > + break; > + else > + /* Unrecognized option. */ > + _dl_usage (ld_so_name, _dl_argv[1]); > + } > else > break; > > /* If we have no further argument the program was called incorrectly. > Grant the user some education. */ > if (_dl_argc < 2) > - _dl_usage (); > + { > + if (state.mode == rtld_help) > + /* --help without an executable is not an error. */ > + _dl_help (ld_so_name, &state); > + else > + _dl_usage (ld_so_name, NULL); > + } > Why open brackets here? There is no stack variable to possible escape. > ++_dl_skip_args; > --_dl_argc; > @@ -1273,7 +1300,7 @@ dl_main (const ElfW(Phdr) *phdr, > break; > } > > - if (__builtin_expect (state.mode, normal) == verify) > + if (state.mode == verify || state.mode == rtld_help) > { > const char *objname; > const char *err_str = NULL; Ok. > @@ -1286,9 +1313,16 @@ dl_main (const ElfW(Phdr) *phdr, > (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, > &args); > if (__glibc_unlikely (err_str != NULL)) > - /* We don't free the returned string, the programs stops > - anyway. */ > - _exit (EXIT_FAILURE); > + { > + /* We don't free the returned string, the programs stops > + anyway. */ > + if (state.mode == rtld_help) > + /* Mask the failure to load the main object. The help > + message contains less information in this case. */ > + _dl_help (ld_so_name, &state); > + else > + _exit (EXIT_FAILURE); > + } > } > else > { Ok. > @@ -1647,6 +1681,11 @@ dl_main (const ElfW(Phdr) *phdr, > audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_AUDIT); > audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_DEPAUDIT); > > + /* At this point, all data has been obtained that is included in the > + --help output. */ > + if (__builtin_expect (state.mode, normal) == rtld_help) > + _dl_help (ld_so_name, &state); > + > /* If we have auditing DSOs to load, do it now. */ > bool need_security_init = true; > if (state.audit_list.length > 0) > Use __glibc_{un}likely or just remove the branch hint (I really don't think this matter here anyway).