blob: bf38fdb842e7f70f931b20f81caf082b216094b5 [file] [log] [blame]
Philip Pfaffe53016772018-04-05 15:04:13 +00001//===- lib/Passes/PassPluginLoader.cpp - Load Plugins for New PM Passes ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Passes/PassPlugin.h"
11#include "llvm/Support/raw_ostream.h"
12
Gabor Buella636aeba2018-04-30 14:21:28 +000013#include <cstdint>
14
Philip Pfaffe53016772018-04-05 15:04:13 +000015using namespace llvm;
16
17Expected<PassPlugin> PassPlugin::Load(const std::string &Filename) {
18 std::string Error;
19 auto Library =
20 sys::DynamicLibrary::getPermanentLibrary(Filename.c_str(), &Error);
21 if (!Library.isValid())
22 return make_error<StringError>(Twine("Could not load library '") +
23 Filename + "': " + Error,
24 inconvertibleErrorCode());
25
26 PassPlugin P{Filename, Library};
Gabor Buella636aeba2018-04-30 14:21:28 +000027 intptr_t getDetailsFn =
28 (intptr_t)Library.SearchForAddressOfSymbol("llvmGetPassPluginInfo");
Philip Pfaffe53016772018-04-05 15:04:13 +000029
30 if (!getDetailsFn)
31 // If the symbol isn't found, this is probably a legacy plugin, which is an
32 // error
33 return make_error<StringError>(Twine("Plugin entry point not found in '") +
34 Filename + "'. Is this a legacy plugin?",
35 inconvertibleErrorCode());
36
37 P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
38
39 if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
40 return make_error<StringError>(
41 Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
42 Twine(P.Info.APIVersion) + ", supported version is " +
43 Twine(LLVM_PLUGIN_API_VERSION) + ".",
44 inconvertibleErrorCode());
45
46 if (!P.Info.RegisterPassBuilderCallbacks)
47 return make_error<StringError>(Twine("Empty entry callback in plugin '") +
48 Filename + "'.'",
49 inconvertibleErrorCode());
50
51 return P;
52}