c2e4e89813a85a63725a284387d158b16540758b
[oota-llvm.git] / support / lib / Support / PluginLoader.cpp
1 //===-- PluginLoader.cpp - Implement -load command line option ------------===//
2 //
3 // This file implements the -load <plugin> command line option processor.  When
4 // linked into a program, this new command line option is available that allows
5 // users to load shared objects into the running program.
6 //
7 // Note that there are no symbols exported by the .o file generated for this
8 // .cpp file.  Because of this, a program must link against support.o instead of
9 // support.a: otherwise this translation unit will not be included.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "Support/CommandLine.h"
14 #include <dlfcn.h>
15 #include <link.h>
16
17 namespace {
18   struct PluginLoader {
19     void operator=(const std::string &Filename) {
20       if (dlopen(Filename.c_str(), RTLD_NOW) == 0)
21         std::cerr << "Error opening '" << Filename << "': " << dlerror()
22                   << "\n  -load request ignored.\n";
23     }
24   };
25 }
26
27 // This causes operator= above to be invoked for every -load option.
28 static cl::opt<PluginLoader, false, cl::parser<string> >
29 LoadOpt("load", cl::ZeroOrMore, cl::value_desc("plugin.so"),
30         cl::desc("Load the specified plugin"));