Index: llvm/include/llvm-c/lto.h =================================================================== --- llvm/include/llvm-c/lto.h +++ llvm/include/llvm-c/lto.h @@ -46,7 +46,7 @@ * @{ */ -#define LTO_API_VERSION 28 +#define LTO_API_VERSION 29 /** * \since prior to LTO_API_VERSION=3 @@ -312,6 +312,16 @@ unsigned int *out_cputype, unsigned int *out_cpusubtype); +/** + * This function can be used by the linker to check if a given module has + * any constructor or destructor functions. + * + * Returns true if the module has either the @llvm.global_ctors or the + * @llvm.global_dtors symbol. Otherwise returns false. + * + * \since LTO_API_VERSION=29 + */ +extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod); /** * Diagnostic severity. * Index: llvm/include/llvm/LTO/legacy/LTOModule.h =================================================================== --- llvm/include/llvm/LTO/legacy/LTOModule.h +++ llvm/include/llvm/LTO/legacy/LTOModule.h @@ -167,6 +167,8 @@ Expected getMachOCPUSubType() const; + bool hasCtorDtor() const; + private: /// Parse metadata from the module // FIXME: it only parses "llvm.linker.options" metadata at the moment Index: llvm/lib/LTO/LTOModule.cpp =================================================================== --- llvm/lib/LTO/LTOModule.cpp +++ llvm/lib/LTO/LTOModule.cpp @@ -688,3 +688,16 @@ Expected LTOModule::getMachOCPUSubType() const { return MachO::getCPUSubType(Triple(Mod->getTargetTriple())); } + +bool LTOModule::hasCtorDtor() const { + for (auto Sym : SymTab.symbols()) { + if (auto *GV = Sym.dyn_cast()) { + StringRef Name = GV->getName(); + if (Name.consume_front("llvm.global_")) { + if (Name.equals("ctors") || Name.equals("dtors")) + return true; + } + } + } + return false; +} Index: llvm/tools/lto/lto.cpp =================================================================== --- llvm/tools/lto/lto.cpp +++ llvm/tools/lto/lto.cpp @@ -580,6 +580,10 @@ unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists); } +lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod) { + return unwrap(mod)->hasCtorDtor(); +} + // ThinLTO API below thinlto_code_gen_t thinlto_create_codegen(void) { Index: llvm/tools/lto/lto.exports =================================================================== --- llvm/tools/lto/lto.exports +++ llvm/tools/lto/lto.exports @@ -8,6 +8,7 @@ lto_module_create_from_memory_with_path lto_module_create_in_local_context lto_module_create_in_codegen_context +lto_module_has_ctor_dtor lto_module_get_linkeropts lto_module_get_macho_cputype lto_module_get_num_symbols