Appending linkage and DGE

Sorry if this question is a repeat, I never got an answer the last time I asked :slight_smile:

How does appending linkage interact with dead global elimination? From what I understand, appending linkage arrays are stitched together by the linker. Dead global elimination usually happens after linking, so if any of those arrays are “live”, then any globals that they point to will be live as well. My conclusion, therefore, is that any global that is marked as having appending linkage can never be considered dead, unless all same-named globals are also dead.

So for example, if I were to use appending linkage to generate a list of module initialization functions (one per module), then would this cause every module to be included in the final binary, regardless of whether it was used or not?

Suppose I wanted it to work the other way - that appending linkage would only stitch together the globals that survived dead global elimination. Is there some way I could get the linker to behave this way?

Sorry if this question is a repeat, I never got an answer the last time I asked :slight_smile:

How does appending linkage interact with dead global elimination?

Appending linkage is not "local", so it won't be deleted.

From what I understand, appending linkage arrays are stitched together by the linker. Dead global elimination usually happens after linking,

globaldce is run at compile time as well.

so if any of those arrays are "live", then any globals that they point to will be live as well. My conclusion, therefore, is that any global that is marked as having appending linkage can never be considered dead, unless all same-named globals are also dead.

Close, but actually these are never considered dead.

So for example, if I were to use appending linkage to generate a list of module initialization functions (one per module), then would this cause every module to be included in the final binary, regardless of whether it was used or not?

It depends on what gets linked in, but yes.

Suppose I wanted it to work the other way - that appending linkage would only stitch together the globals that survived dead global elimination. Is there some way I could get the linker to behave this way?

Nope. The typical way to handle this is with archive files, whose .o files are only sucked in if there is a reference to the .o file in the .a.

-Chris