1131fb978SPhilip Pfaffe //===- lib/Passes/PassPluginLoader.cpp - Load Plugins for New PM Passes ---===//
2131fb978SPhilip Pfaffe //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6131fb978SPhilip Pfaffe //
7131fb978SPhilip Pfaffe //===----------------------------------------------------------------------===//
8131fb978SPhilip Pfaffe
9131fb978SPhilip Pfaffe #include "llvm/Passes/PassPlugin.h"
10131fb978SPhilip Pfaffe #include "llvm/Support/raw_ostream.h"
11131fb978SPhilip Pfaffe
12ad9a0429SGabor Buella #include <cstdint>
13ad9a0429SGabor Buella
14131fb978SPhilip Pfaffe using namespace llvm;
15131fb978SPhilip Pfaffe
Load(const std::string & Filename)16131fb978SPhilip Pfaffe Expected<PassPlugin> PassPlugin::Load(const std::string &Filename) {
17131fb978SPhilip Pfaffe std::string Error;
18131fb978SPhilip Pfaffe auto Library =
19131fb978SPhilip Pfaffe sys::DynamicLibrary::getPermanentLibrary(Filename.c_str(), &Error);
20131fb978SPhilip Pfaffe if (!Library.isValid())
21131fb978SPhilip Pfaffe return make_error<StringError>(Twine("Could not load library '") +
22131fb978SPhilip Pfaffe Filename + "': " + Error,
23131fb978SPhilip Pfaffe inconvertibleErrorCode());
24131fb978SPhilip Pfaffe
25131fb978SPhilip Pfaffe PassPlugin P{Filename, Library};
26*f617ab10STomas Matheson
27*f617ab10STomas Matheson // llvmGetPassPluginInfo should be resolved to the definition from the plugin
28*f617ab10STomas Matheson // we are currently loading.
29ad9a0429SGabor Buella intptr_t getDetailsFn =
30*f617ab10STomas Matheson (intptr_t)Library.getAddressOfSymbol("llvmGetPassPluginInfo");
31131fb978SPhilip Pfaffe
32131fb978SPhilip Pfaffe if (!getDetailsFn)
33131fb978SPhilip Pfaffe // If the symbol isn't found, this is probably a legacy plugin, which is an
34131fb978SPhilip Pfaffe // error
35131fb978SPhilip Pfaffe return make_error<StringError>(Twine("Plugin entry point not found in '") +
36131fb978SPhilip Pfaffe Filename + "'. Is this a legacy plugin?",
37131fb978SPhilip Pfaffe inconvertibleErrorCode());
38131fb978SPhilip Pfaffe
39131fb978SPhilip Pfaffe P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
40131fb978SPhilip Pfaffe
41131fb978SPhilip Pfaffe if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
42131fb978SPhilip Pfaffe return make_error<StringError>(
43131fb978SPhilip Pfaffe Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
44131fb978SPhilip Pfaffe Twine(P.Info.APIVersion) + ", supported version is " +
45131fb978SPhilip Pfaffe Twine(LLVM_PLUGIN_API_VERSION) + ".",
46131fb978SPhilip Pfaffe inconvertibleErrorCode());
47131fb978SPhilip Pfaffe
48131fb978SPhilip Pfaffe if (!P.Info.RegisterPassBuilderCallbacks)
49131fb978SPhilip Pfaffe return make_error<StringError>(Twine("Empty entry callback in plugin '") +
50131fb978SPhilip Pfaffe Filename + "'.'",
51131fb978SPhilip Pfaffe inconvertibleErrorCode());
52131fb978SPhilip Pfaffe
53131fb978SPhilip Pfaffe return P;
54131fb978SPhilip Pfaffe }
55