1 //===- CommonLinkerContext.cpp --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lld/Common/CommonLinkerContext.h" 10 #include "lld/Common/Driver.h" 11 #include "lld/Common/ErrorHandler.h" 12 #include "lld/Common/Memory.h" 13 14 using namespace llvm; 15 using namespace lld; 16 17 // Reference to the current LLD instance. This is a temporary situation, until 18 // we pass this context everywhere by reference, or we make it a thread_local, 19 // as in https://reviews.llvm.org/D108850?id=370678 where each thread can be 20 // associated with a LLD instance. Only then will LLD be free of global 21 // state. 22 static CommonLinkerContext *lctx; 23 24 CommonLinkerContext::CommonLinkerContext() { lctx = this; } 25 26 CommonLinkerContext::~CommonLinkerContext() { 27 assert(lctx); 28 // Explicitly call the destructors since we created the objects with placement 29 // new in SpecificAlloc::create(). 30 for (auto &it : instances) 31 it.second->~SpecificAllocBase(); 32 lctx = nullptr; 33 } 34 35 CommonLinkerContext &lld::commonContext() { 36 assert(lctx); 37 return *lctx; 38 } 39 40 bool lld::hasContext() { return lctx != nullptr; } 41 42 void CommonLinkerContext::destroy() { 43 if (lctx == nullptr) 44 return; 45 delete lctx; 46 } 47 48 // Temporary API that forces global state cleanup between explicit calls to 49 // drivers. See discussion in https://reviews.llvm.org/D119049. 50 void lld::cleanup() { 51 // Delete the global context and clear the global context pointer, so that it 52 // cannot be accessed anymore. 53 CommonLinkerContext::destroy(); 54 } 55