xref: /freebsd/contrib/llvm-project/lld/Common/CommonLinkerContext.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
104eeddc0SDimitry Andric //===- CommonLinkerContext.cpp --------------------------------------------===//
204eeddc0SDimitry Andric //
304eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
404eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
504eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
604eeddc0SDimitry Andric //
704eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
804eeddc0SDimitry Andric 
904eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
1004eeddc0SDimitry Andric #include "lld/Common/ErrorHandler.h"
1104eeddc0SDimitry Andric #include "lld/Common/Memory.h"
1204eeddc0SDimitry Andric 
13*81ad6265SDimitry Andric #include "llvm/CodeGen/CommandFlags.h"
14*81ad6265SDimitry Andric 
1504eeddc0SDimitry Andric using namespace llvm;
1604eeddc0SDimitry Andric using namespace lld;
1704eeddc0SDimitry Andric 
1804eeddc0SDimitry Andric // Reference to the current LLD instance. This is a temporary situation, until
1904eeddc0SDimitry Andric // we pass this context everywhere by reference, or we make it a thread_local,
2004eeddc0SDimitry Andric // as in https://reviews.llvm.org/D108850?id=370678 where each thread can be
2104eeddc0SDimitry Andric // associated with a LLD instance. Only then will LLD be free of global
2204eeddc0SDimitry Andric // state.
2304eeddc0SDimitry Andric static CommonLinkerContext *lctx;
2404eeddc0SDimitry Andric 
CommonLinkerContext()25*81ad6265SDimitry Andric CommonLinkerContext::CommonLinkerContext() {
26*81ad6265SDimitry Andric   lctx = this;
27*81ad6265SDimitry Andric   // Fire off the static initializations in CGF's constructor.
28*81ad6265SDimitry Andric   codegen::RegisterCodeGenFlags CGF;
29*81ad6265SDimitry Andric }
3004eeddc0SDimitry Andric 
~CommonLinkerContext()3104eeddc0SDimitry Andric CommonLinkerContext::~CommonLinkerContext() {
3204eeddc0SDimitry Andric   assert(lctx);
3304eeddc0SDimitry Andric   // Explicitly call the destructors since we created the objects with placement
3404eeddc0SDimitry Andric   // new in SpecificAlloc::create().
3504eeddc0SDimitry Andric   for (auto &it : instances)
3604eeddc0SDimitry Andric     it.second->~SpecificAllocBase();
3704eeddc0SDimitry Andric   lctx = nullptr;
3804eeddc0SDimitry Andric }
3904eeddc0SDimitry Andric 
commonContext()4004eeddc0SDimitry Andric CommonLinkerContext &lld::commonContext() {
4104eeddc0SDimitry Andric   assert(lctx);
4204eeddc0SDimitry Andric   return *lctx;
4304eeddc0SDimitry Andric }
4404eeddc0SDimitry Andric 
hasContext()4504eeddc0SDimitry Andric bool lld::hasContext() { return lctx != nullptr; }
4604eeddc0SDimitry Andric 
destroy()4704eeddc0SDimitry Andric void CommonLinkerContext::destroy() {
4804eeddc0SDimitry Andric   if (lctx == nullptr)
4904eeddc0SDimitry Andric     return;
5004eeddc0SDimitry Andric   delete lctx;
5104eeddc0SDimitry Andric }
52