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