I can see lots of copied lore that functions registered with the .init_array section have the command-line arguments argc and argv, like main(), but I am failing to find any actual published documentation online that confirms that this is the case.
Yes, for clarity, the function itself is not "declared in" the .init_array, but a pointer to the function is declared there, "registering" the function, and it is called by some iterator during start-up. Question remains: show me some documentation for the argument list passed in by that iterator.
My intent is to change these arguments from a dynamic library in a subtle but generally safe way, so I want to find the "real deal" in memory - not from /proc/self/.
For more information, follow the link below.
Some Stack-overflow lore: Accessing main arguments outside of main on Linux
Even my favoured Oracle ( docs.oracle.com/cd/E23824_01/html/819-0690/chapter3-8.html ) only mentions that the functions get called, but no promise of what arguments there might be. Same with the elf and gcc documentation, as far as I can see.
In the land of C/C++ UB paranoia, Ideally I need some certainty that this is documented behaviour before I go ahead with it? Does it exist? Can it be implied in some way?
Summary of comments/answers so-far:
At least for GNU libc, a relevant change occurred with this patch: BZ #974. https://sourceware.org/pipermail/libc-alpha/2005-July/019240.html (It is mentioned in glibc's ChangeLog.old/ChangeLog.16 entry 2005-04-13 H.J. Lu.) – Ian Abbott
To me, this demonstrates that the glbc maintainers were aware of the requirement to pass argc/argv/env - that it is not accidental - and extended it to main exe registrations. It also tells us that it was working for dynamic libraries prior to that date.
It is an interesting question whether this binds other libc implementers to follow the pattern.
The init () method arguments are optional. We can define a constructor with any number of arguments. Let’s look at some examples of the constructor function in different scenarios.
It binds the instance to the init () method. It’s usually named “self” to follow the naming convention. You can read more about it at Python self variable. The init () method arguments are optional. We can define a constructor with any number of arguments.
1 Method 1: Using for loop and Python range () function. Python for loop and range () function together can be used to initialize an array with a default value. 2 Method 2: Python NumPy module to create and initialize array. ... 3 Method 3: Direct method to initialize a Python array. ...
Note that it's not possible to initialize an array after the declaration using this approach; an attempt to do so will result in a compilation error. 4. Using Arrays.fill () The java.util.Arrays class has several methods named fill (), which accept different types of arguments and fill the whole array with the same value:
I've found this interesting article about Linux programs' start-up procedure by Patrick Horgan. But I may not guarantee the correctness of this source.
At least, it explains the code behind the .init_array
section:
void __libc_csu_init (int argc, char **argv, char **envp) {
_init ();
const size_t size = __init_array_end - __init_array_start;
for (size_t i = 0; i < size; i++) {
(*__init_array_start [i]) (argc, argv, envp);
}
}
It appears that __libc_csu_init()
function first calculates the number of elements inside .init_array
section, and then calls every function pointer with arguments argc
, argv
and envp
. This function (__libc_csu_init()
) is called before main()
.
NOTE: the .init_array
section appears to be specific to the ELF binary format.
It appears that the implementation of __libc_csu_init()
(and, more in general, how .init_array
functions are called) is platform-dependent and libc-dependent.
However, GLIBC on Linux appears to correctly call the functions with the desired arguments, as you can see from its source code.
Plus, reading the GLIBC changelog, it appears that this behavior has been introduced in 2005.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With