PassPlugin.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. intptr_t getDetailsFn =
  22. (intptr_t)Library.SearchForAddressOfSymbol("llvmGetPassPluginInfo");
  23. if (!getDetailsFn)
  24. // If the symbol isn't found, this is probably a legacy plugin, which is an
  25. // error
  26. return make_error<StringError>(Twine("Plugin entry point not found in '") +
  27. Filename + "'. Is this a legacy plugin?",
  28. inconvertibleErrorCode());
  29. P.Info = reinterpret_cast<decltype(llvmGetPassPluginInfo) *>(getDetailsFn)();
  30. if (P.Info.APIVersion != LLVM_PLUGIN_API_VERSION)
  31. return make_error<StringError>(
  32. Twine("Wrong API version on plugin '") + Filename + "'. Got version " +
  33. Twine(P.Info.APIVersion) + ", supported version is " +
  34. Twine(LLVM_PLUGIN_API_VERSION) + ".",
  35. inconvertibleErrorCode());
  36. if (!P.Info.RegisterPassBuilderCallbacks)
  37. return make_error<StringError>(Twine("Empty entry callback in plugin '") +
  38. Filename + "'.'",
  39. inconvertibleErrorCode());
  40. return P;
  41. }