C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::argc std::argv (available to global constructors)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 13 Jun 2023 10:32:33 -0700
On Tuesday, 13 June 2023 04:41:19 PDT Tom Honermann via Std-Proposals wrote:
> This is being pursued as WG14 N2948 (Accessing the command line
> arguments outside of main())
> <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2948.pdf>.

Global constructors already get argc, argv and envp in most ELF systems:

https://codebrowser.dev/glibc/glibc/csu/libc-start.c.html#__preinit_array_start
https://codebrowser.dev/glibc/glibc/elf/dl-init.c.html#56
https://github.com/freebsd/freebsd-src/blob/
9247238cc4b8835892a47701136b0fd073f8d67c/lib/libc/csu/libc_start1.c#L34-L37
https://github.com/openbsd/src/blob/14e2a040958ba4aae34e88f7b0f2412cd00e3274/
lib/csu/crt0.c#L95-L102

Though not on NetBSD or MUSL:
https://github.com/justincormack/netbsd-src/blob/
e208d780aeeca41ae53c6240f23fef62f2a0db69/src/libexec/ld.elf_so/rtld.c#L251
https://github.com/bminor/musl/blob/718f363bc2067b6487900eddc9180c84e7739f80/
src/env/__libc_start_main.c#L59

This appears to be a common extension to ELF, because the last specification I
could find (from 1995) doesn't even mention the init array.

Can be used like this:
https://github.com/opendcdiag/opendcdiag/blob/
10533f0ddbb6c46423bcb3c630ec384b11d4e6e2/framework/main.cpp#L112-L123

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-06-13 17:32:34