This is the mail archive of the
mailing list for the binutils project.
Re: [gold-linker] [ld-linker] Implementation of dynamic initialization for global variables and static member variables in C++
- From: Ian Lance Taylor <iant at google dot com>
- To: Sanee Berlow <nepnepparadise at gmail dot com>
- Cc: Binutils <binutils at sourceware dot org>
- Date: Wed, 8 Jul 2015 16:46:44 -0700
- Subject: Re: [gold-linker] [ld-linker] Implementation of dynamic initialization for global variables and static member variables in C++
- Authentication-results: sourceware.org; auth=none
- References: <CAB+W9wVLC7+EJGK-CU8YVGHh-cmdWRWk8WhnZvT+KLnP2RKoUg at mail dot gmail dot com>
On Thu, Jul 2, 2015 at 8:02 AM, Sanee Berlow <firstname.lastname@example.org> wrote:
> What information does the compiler place into a generated object file
> relating to dynamic initialization of global variables and static
> member variables for the linker to use?
The compiler doesn't put any information as such. It just generates a
global constructor that initializes the variables. To the linker it
just looks like any other code.
> What information does the linker place into the final linked module
> during the linking process so that the OS module loader is able to
> correctly initialize all variables (including dynamically initialized
> global/static member variables that make calls to functions as part of
The linker arranges to run all global constructors when the program is
started or the shared library is loaded. The exact details of this
are system-specific, and you didn't mention which system you care
> How is the function that needs to be executed during dynamic variable
> initialization mapped to the particular variable that needs to be
> initialized with that code?
It's just ordinary code that sets the variable as though it were an
ordinary global variable. There is no specific association between
the code and the variable.
> When an executable or dynamically linked module is loaded, how is
> dynamic initialization of variables performed?
It's a global constructor.
> Does the implementation of C++11 constant expressions (marked by the
> constexpr specifier) involve any special considerations compared to
> the implementation of regular static member variables and functions?
Not from the linker's point of view.