diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt index 0ccdfd5a787..8218bd2c872 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt @@ -437,7 +437,14 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) { val allNonCachedDependencies = context.librariesWithDependencies.filter { context.config.cachedLibraries.getLibraryCache(it) == null } - (allNonCachedDependencies + allCachedBitcodeDependencies).distinct() + val set = (allNonCachedDependencies + allCachedBitcodeDependencies).toSet() + // This list is used in particular to build the libraries' initializers chain. + // The initializers must be called in the topological order, so make sure that the + // libraries list being returned is also toposorted. + context.config.resolvedLibraries + .getFullList(TopologicalLibraryOrder) + .cast>() + .filter { it in set } } val bitcodeToLink: List by lazy { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index fa0bb8c20b1..5087605fccb 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -2369,8 +2369,8 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map()