Add support for the GNU extensions 'program_invocation_name' and
'program_invocation_short_name'. These are useful to print error
messages, which by convention include the program name.

As these are global variables which take up memory even if not used,
similar to 'errno', gate them behind NOLIBC_IGNORE_ERRNO.

Signed-off-by: Thomas Weißschuh <[email protected]>
---
 tools/include/nolibc/crt.h                   | 26 ++++++++++++++++++++++++++
 tools/include/nolibc/errno.h                 |  2 ++
 tools/testing/selftests/nolibc/nolibc-test.c |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
index d9262998dae9..842f86e41f2f 100644
--- a/tools/include/nolibc/crt.h
+++ b/tools/include/nolibc/crt.h
@@ -17,6 +17,7 @@ const unsigned long *_auxv __attribute__((weak));
 void _start(void);
 static void __stack_chk_init(void);
 static void exit(int);
+static char *strrchr(const char *s, int c);
 
 extern void (*const __preinit_array_start[])(int, char **, char**) 
__attribute__((weak));
 extern void (*const __preinit_array_end[])(int, char **, char**) 
__attribute__((weak));
@@ -27,6 +28,24 @@ extern void (*const __init_array_end[])(int, char **, 
char**) __attribute__((wea
 extern void (*const __fini_array_start[])(void) __attribute__((weak));
 extern void (*const __fini_array_end[])(void) __attribute__((weak));
 
+extern char *program_invocation_name __attribute__((weak));
+extern char *program_invocation_short_name __attribute__((weak));
+
+static __inline__
+char *__nolibc_program_invocation_short_name(char *long_name)
+{
+       char *short_name;
+
+       if (!long_name)
+               return NULL;
+
+       short_name = strrchr(long_name, '/');
+       if (!short_name || !short_name[0])
+               return NULL;
+
+       return short_name + 1;
+}
+
 void _start_c(long *sp);
 __attribute__((weak,used))
 #if __nolibc_has_feature(undefined_behavior_sanitizer)
@@ -76,6 +95,13 @@ void _start_c(long *sp)
                ;
        _auxv = auxv;
 
+#ifndef NOLIBC_IGNORE_ERRNO
+       if (argc > 0) {
+               program_invocation_name = argv[0];
+               program_invocation_short_name = 
__nolibc_program_invocation_short_name(argv[0]);
+       }
+#endif /* NOLIBC_IGNORE_ERRNO */
+
        for (ctor_func = __preinit_array_start; ctor_func < 
__preinit_array_end; ctor_func++)
                (*ctor_func)(argc, argv, envp);
        for (ctor_func = __init_array_start; ctor_func < __init_array_end; 
ctor_func++)
diff --git a/tools/include/nolibc/errno.h b/tools/include/nolibc/errno.h
index 08a33c40ec0c..64d9f9bc6539 100644
--- a/tools/include/nolibc/errno.h
+++ b/tools/include/nolibc/errno.h
@@ -15,6 +15,8 @@
 #ifndef NOLIBC_IGNORE_ERRNO
 #define SET_ERRNO(v) do { errno = (v); } while (0)
 int errno __attribute__((weak));
+char *program_invocation_name __attribute__((weak));
+char *program_invocation_short_name __attribute__((weak));
 #else
 #define SET_ERRNO(v) do { } while (0)
 #endif
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c 
b/tools/testing/selftests/nolibc/nolibc-test.c
index a140c54e4d72..5565ada679cc 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -754,6 +754,8 @@ int run_startup(int min, int max)
                CASE_TEST(constructor);      EXPECT_EQ(is_nolibc, 
constructor_test_value, 0x3); break;
                CASE_TEST(linkage_errno);    EXPECT_PTREQ(1, 
linkage_test_errno_addr(), &errno); break;
                CASE_TEST(linkage_constr);   EXPECT_EQ(1, 
linkage_test_constructor_test_value, 0x3); break;
+               CASE_TEST(prog_name_nolibc); EXPECT_STREQ(is_nolibc, 
program_invocation_short_name, "nolibc-test"); break;
+               CASE_TEST(prog_name_libc);   EXPECT_STREQ(!is_nolibc, 
program_invocation_short_name, "libc-test"); break;
                case __LINE__:
                        return ret; /* must be last */
                /* note: do not set any defaults so as to permit holes above */

-- 
2.53.0


Reply via email to