Skip to content

LLD and -flto=thin calls ldc.register_dso multiple times instead of once at startup/shutdown #3786

@JohanEngelen

Description

@JohanEngelen

When using LLD with -flto=thin, we are running into the problem that each D module's call to ldc.register_dso in llvm.global_ctors is not folded into one call. We are relying on LLVM functionality where if a D module's ldc.register_dso is "discarded" because of another module's ldc.register_dso overriding it, then the entry in llvm.global_ctors is also discarded. This is apparently happening with ld.gold, but not with ld.lld.

It's related to this work: #2870

Strangely, when I manually put the ctor/dtor in .init_array/.fini_array sections, the symbols are not merged either (not with bfd, gold, or lld). That is, replacing

ldc/gen/modules.cpp

Lines 367 to 368 in 2ec2fd7

llvm::appendToGlobalCtors(gIR->module, registerDSO, 65535, registerDSO);
llvm::appendToGlobalDtors(gIR->module, registerDSO, 65535, registerDSO);
with

    const auto dsoCtor = defineDSOGlobal("ldc.dso_ctor", registerDSO);
    const auto dsoDtor = defineDSOGlobal("ldc.dso_dtor", registerDSO);
    gIR->usedArray.push_back(dsoCtor);
    gIR->usedArray.push_back(dsoDtor);
    dsoCtor->setSection(".init_array");
    dsoDtor->setSection(".fini_array");

Metadata

Metadata

Assignees

No one assigned

    Labels

    llvmRelated to LLVM

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions