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