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/ErrorHandler.h"
11#include "lld/Common/Memory.h"
12
13#include "llvm/CodeGen/CommandFlags.h"
14
15using namespace llvm;
16using namespace lld;
17
18// Reference to the current LLD instance. This is a temporary situation, until
19// we pass this context everywhere by reference, or we make it a thread_local,
20// as in https://reviews.llvm.org/D108850?id=370678 where each thread can be
21// associated with a LLD instance. Only then will LLD be free of global
22// state.
23static CommonLinkerContext *lctx;
24
25CommonLinkerContext::CommonLinkerContext() {
26 // A nested context, e.g. the dynamic debugging link, does not install itself.
27 if (lctx)
28 return;
29
30 lctx = this;
31 // Fire off the static initializations in CGF's constructor.
32 codegen::RegisterCodeGenFlags CGF;
33}
34
35CommonLinkerContext::~CommonLinkerContext() {
36 assert(lctx);
37 // Explicitly call the destructors since we created the objects with placement
38 // new in SpecificAlloc::create().
39 for (auto &it : instances)
40 it.second->~SpecificAllocBase();
41
42 if (lctx == this)
43 lctx = nullptr;
44}
45
46CommonLinkerContext &lld::commonContext() {
47 assert(lctx);
48 return *lctx;
49}
50
51bool lld::hasContext() { return lctx != nullptr; }
52
53void CommonLinkerContext::destroy() {
54 if (lctx == nullptr)
55 return;
56 delete lctx;
57}
58