7109ea749fdb8449053f883b4dac8ddcfe86d726
[vuplus_xbmc] / xbmc / cores / DllLoader / DllLoader.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "coffldr.h"
24 #include "LibraryLoader.h"
25
26 #if defined(__linux__) && !defined(__powerpc__) && !defined(__arm__)
27 #define USE_LDT_KEEPER
28 #include "ldt_keeper.h"
29 #endif
30
31 #ifndef NULL
32 #define NULL 0
33 #endif
34
35 class DllLoader;
36
37
38 typedef struct Export
39 {
40   const char*   name;
41   unsigned long ordinal;
42   void*         function;
43   void*         track_function;
44 } Export;
45
46 typedef struct ExportEntry
47 {
48   Export exp;
49   ExportEntry* next;
50 } ExportEntry;
51
52 typedef struct _LoadedList
53 {
54   DllLoader* pDll;
55   _LoadedList* pNext;
56 } LoadedList;
57
58 class DllLoader : public CoffLoader, public LibraryLoader
59 {
60 public:
61   DllLoader(const char *dll, bool track = false, bool bSystemDll = false, bool bLoadSymbols = false, Export* exports = NULL);
62   virtual ~DllLoader();
63
64   virtual bool Load();
65   virtual void Unload();
66
67   virtual int ResolveExport(const char*, void** ptr, bool logging = true);
68   virtual int ResolveOrdinal(unsigned long ordinal, void** ptr);
69   virtual bool HasSymbols() { return m_bLoadSymbols && !m_bUnloadSymbols; }
70   virtual bool IsSystemDll() { return m_bSystemDll; }
71   virtual HMODULE GetHModule() { return (HMODULE)hModule; }
72
73   Export* GetExportByFunctionName(const char* sFunctionName);
74   Export* GetExportByOrdinal(unsigned long ordinal);
75 protected:
76   int Parse();
77   int ResolveImports();
78
79   void AddExport(unsigned long ordinal, void* function, void* track_function = NULL);
80   void AddExport(char* sFunctionName, unsigned long ordinal, void* function, void* track_function = NULL);
81   void AddExport(char* sFunctionName, void* function, void* track_function = NULL);
82   void SetExports(Export* exports) { m_pStaticExports = exports; }
83
84 protected:
85   // Just pointers; dont' delete...
86   ImportDirTable_t *ImportDirTable;
87   ExportDirTable_t *ExportDirTable;
88   bool m_bTrack;
89   bool m_bSystemDll; // true if this dll should not be removed
90   bool m_bLoadSymbols; // when true this dll should not be removed
91   bool m_bUnloadSymbols;
92   ExportEntry* m_pExportHead;
93   Export* m_pStaticExports;
94   LoadedList* m_pDlls;
95
96 #ifdef USE_LDT_KEEPER
97   ldt_fs_t* m_ldt_fs;
98 #endif
99
100   void PrintImportLookupTable(unsigned long ImportLookupTable_RVA);
101   void PrintImportTable(ImportDirTable_t *ImportDirTable);
102   void PrintExportTable(ExportDirTable_t *ExportDirTable);
103
104   int ResolveOrdinal(char*, unsigned long, void**);
105   int ResolveName(char*, char*, void **);
106   char* ResolveReferencedDll(char* dll);
107   int LoadExports();
108   void LoadSymbols();
109   static void UnloadSymbols();
110 };