ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dlm-loader-factory.cc
Go to the documentation of this file.
1 #include "dlm-loader-factory.h"
2 #include "ns3/log.h"
3 #include "ns3/fatal-error.h"
4 #include <list>
5 #include <dlfcn.h>
6 
7 NS_LOG_COMPONENT_DEFINE ("DlmLoaderFactory");
8 
9 extern "C" {
10 // The function and structure declarations below are not exactly equal to the
11 // corresponding declarations in the elf loader, mostly for simplicity to
12 // avoid dragging in too many dependent declarations.
13 
14 typedef Lmid_t (*DlLmidNew)(int, char **, char **);
15 typedef void (*DlLmidDelete)(Lmid_t);
16 typedef int (*DlLmidAddLibRemap)(Lmid_t lmid, const char *src, const char *dst);
17 typedef void (*Ns3ReportTestError)(const char *);
18 
19 }
20 
21 
22 namespace ns3 {
23 
24 NS_OBJECT_ENSURE_REGISTERED (DlmLoaderFactory);
25 
26 class DlmLoader : public Loader
27 {
28 public:
29  DlmLoader (int argc, char **argv, char **envp);
30  virtual ~DlmLoader ();
31  virtual Loader * Clone (void)
32  {
33  return 0; /* XXX */
34  }
35  virtual void UnloadAll (void);
36  virtual void * Load (std::string filename, int flag);
37  virtual void Unload (void *module);
38  virtual void * Lookup (void *module, std::string symbol);
39 private:
40  Lmid_t m_lmid;
41  std::list<void *> m_loaded;
42 };
43 
44 DlmLoader::DlmLoader (int argc, char **argv, char **envp)
45 {
46  NS_LOG_FUNCTION (this << argc);
47  void *libvdl = dlopen ("libvdl.so", RTLD_LAZY | RTLD_LOCAL);
48  DlLmidNew dlLmidNew = (DlLmidNew) dlsym (libvdl, "dl_lmid_new");
49  dlclose (libvdl);
50  if (dlLmidNew == 0)
51  {
52  NS_FATAL_ERROR ("Could not find our fancy elf loader");
53  // Note: we do not link explicitely against our fancy elf loader
54  // because it appears to break our stupid build system
55  // so, we have to lookup the necessary magic functions at runtime.
56  }
57 
58  // create a new context
59  m_lmid = dlLmidNew (argc, argv, envp);
60 
61 }
63 {
64  NS_LOG_FUNCTION (this);
65  m_loaded.clear ();
66  void *libvdl = dlopen ("libvdl.so", RTLD_LAZY | RTLD_LOCAL);
67  DlLmidDelete dlLmidDelete = (DlLmidDelete) dlsym (libvdl, "dl_lmid_delete");
68  dlclose (libvdl);
69  if (dlLmidDelete == 0)
70  {
71  NS_FATAL_ERROR ("Could not find our fancy elf loader for lmid deletion");
72  // Note: we do not link explicitely against our fancy elf loader
73  // because it appears to break our stupid build system
74  // so, we have to lookup the necessary magic functions at runtime.
75  return;
76  }
77  dlLmidDelete (m_lmid);
78 }
79 void
81 {
82  NS_LOG_FUNCTION (this);
83  for (std::list<void *>::const_iterator i = m_loaded.begin ();
84  i != m_loaded.end (); ++i)
85  {
86  ::dlclose (*i);
87  }
88 }
89 void *
90 DlmLoader::Load (std::string filename, int flag)
91 {
92  NS_LOG_FUNCTION (this << filename << flag);
93  void *module = dlmopen (m_lmid, filename.c_str (), flag);
94  m_loaded.push_back (module);
95  return module;
96 }
97 void
98 DlmLoader::Unload (void *module)
99 {
100  NS_LOG_FUNCTION (this << module);
101  ::dlclose (module);
102  m_loaded.remove (module);
103 }
104 void *
105 DlmLoader::Lookup (void *module, std::string symbol)
106 {
107  NS_LOG_FUNCTION (this << module << symbol);
108  void *p = dlsym (module, symbol.c_str ());
109  return p;
110 }
111 
112 
113 TypeId
115 {
116  static TypeId tid = TypeId ("ns3::DlmLoaderFactory")
117  .SetParent<LoaderFactory> ()
118  .AddConstructor<DlmLoaderFactory> ()
119  ;
120  return tid;
121 }
122 
124 {
125 }
127 {
128 }
129 Loader *
130 DlmLoaderFactory::Create (int argc, char **argv, char **envp)
131 {
132  DlmLoader *loader = new DlmLoader (argc, argv, envp);
133  return loader;
134 }
135 
136 
137 } // namespace ns3