C++ Logo

std-proposals

Advanced search

Re: Make side effects in static initialization more consistent

From: kh <konstantin.harmuth_at_[hidden]>
Date: Tue, 22 Dec 2020 05:59:01 +0100
I don't think it is necessary to put "libraries" into the standard.
This was only mentioned as a practical description of the problem.

My proposed solutions might be difficult or impossible to implement.
Especially my second approach is irreconcilable with the current
linker architecture as we don't have the necessary information
available in that stage.

However, my first proposed solution (the extra attribute) is more
viable. It would *only* require to put some kind of flag inside the
object/library file for that symbol, that prevents the linker from
removing it.

gcc/ld even has some kind of support for this. "--whole-archive"
enables this behavior on a per-file basis. I don't think It would be
impossible to change that to a per-symbol basis.

gcc also has "__attribute__((used))", which sadly only works on functions.

Regards,
Konstantin Harmuth

Am Mo., 21. Dez. 2020 um 14:21 Uhr schrieb Thiago Macieira via
Std-Proposals <std-proposals_at_[hidden]>:
>
> On Monday, 21 December 2020 08:05:07 -03 kh via Std-Proposals wrote:
> > The main problem is that the linker could remove unused variables. The
> > linker then also eliminates any side effects of the initialization.
> > This might cause problems in some cases when the side effect is
> > expected. The problem is strongly related to static linking. I
> > couldn't observe it when linking the object files directly into an
> > executable or in a shared library. This inconsistency is also
> > problematic.
>
> This is your problem: you've stepped out of the standard when you mentioned
> "libraries".
>
> The standard does not recognise the existence of them today. Either we need to
> update the standard so it does, or we punt the problem by saying "modules will
> solve everything", or this is an implementation issue.
>
> Currently, it is the latter. This happens because of the way linkers have
> worked for 40 years. I doubt you're going to get linkers to change much. But
> since this is a platform issue, maybe all we need are platform solutions too.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel DPG Cloud Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-12-21 22:59:16