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