PassPlugin.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===- lib/Passes/PassPluginLoader.cpp - Load Plugins for New PM Passes ---===//
  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. #include "llvm/Passes/PassPlugin.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. #include <cstdint>
  11. using namespace llvm;
  12. Expected<PassPlugin> PassPlugin::Load(const std::string &Filename) {
  13. std::string Error;
  14. auto Library =
  15. sys::DynamicLibrary::getPermanentLibrary(Filename.c_str(), &Error);
  16. if (!Library.isValid())
  17. return make_error<StringError>(Twine("Could not load library '") +
  18. Filename + "': " + Error,
  19. inconvertibleErrorCode());
  20. PassPlugin P{Filename, Library};
  21. // llvmGetPassPluginInfo should be resolved to the definition from the plugin
  22. // we are currently loading.
  23. intptr_t getDetailsFn =
  24. (intptr_t)Library.getAddressOfSymbol("llvmGetPassPluginInfo");
  25. if (!getDetailsFn)
  26. // If the symbol isn't found, this is probably a legacy plugin, which is an
  27. // error
  28. return make_error<StringError>(Twine("Plugin entry point not found in '") +
  29. Filename + "'. Is this a legacy plugin?",
  30. inconvertibleErrorCode());
  31. P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
  32. if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
  33. return make_error<StringError>(
  34. Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
  35. Twine(P.Info.APIVersion) + ", supported version is " +
  36. Twine(LLVM_PLUGIN_API_VERSION) + ".",
  37. inconvertibleErrorCode());
  38. if (!P.Info.RegisterPassBuilderCallbacks)
  39. return make_error<StringError>(Twine("Empty entry callback in plugin '") +
  40. Filename + "'.'",
  41. inconvertibleErrorCode());
  42. return P;
  43. }