1 | /*
|
---|
2 | * TOPPERS/JSP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Just Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨è使¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã® (1)ã(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
11 | * ã«ãã£ã¦å
|
---|
12 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
13 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
14 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
15 | å¸ï¼ä»¥ä¸ï¼
|
---|
16 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
17 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
18 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
19 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
20 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
21 | * ç¨ã§ããå½¢ã§åé
|
---|
22 | å¸ããå ´åã«ã¯ï¼åé
|
---|
23 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
24 | * è
|
---|
25 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
26 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
27 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
28 | * ç¨ã§ããªãå½¢ã§åé
|
---|
29 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
30 | * ã¨ï¼
|
---|
31 | * (a) åé
|
---|
32 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
33 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
34 | * 使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
35 | * (b) åé
|
---|
36 | å¸ã®å½¢æ
|
---|
37 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
38 | * å ±åãããã¨ï¼
|
---|
39 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
40 | * 害ãããï¼ä¸è¨è使¨©è
|
---|
41 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
42 | 責ãããã¨ï¼
|
---|
43 | *
|
---|
44 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨è使¨©è
|
---|
45 | ã
|
---|
46 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
47 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
48 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæå®³ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: fc_windows.cpp,v 1.12 2003/12/20 06:51:58 takayuki Exp $
|
---|
51 | */
|
---|
52 |
|
---|
53 | #if (defined(FILECONTAINER_WINDOWS) || defined(TESTSUITE)) && defined(_MSC_VER)
|
---|
54 |
|
---|
55 | #pragma warning(disable:4786)
|
---|
56 |
|
---|
57 | #include "base/filecontainer.h"
|
---|
58 | #include <windows.h>
|
---|
59 | #include <imagehlp.h>
|
---|
60 | #include <string>
|
---|
61 |
|
---|
62 | using namespace std;
|
---|
63 |
|
---|
64 | namespace {
|
---|
65 |
|
---|
66 | class FileContainerWindowsImpl : public FileContainer
|
---|
67 | {
|
---|
68 | protected:
|
---|
69 | HANDLE process;
|
---|
70 | LOADED_IMAGE image;
|
---|
71 | DWORD base;
|
---|
72 |
|
---|
73 | inline bool isLoaded(void) const
|
---|
74 | { return base != 0; }
|
---|
75 |
|
---|
76 | public:
|
---|
77 | FileContainerWindowsImpl(void) throw();
|
---|
78 | virtual ~FileContainerWindowsImpl(void) throw();
|
---|
79 |
|
---|
80 | /* ã¤ã³ã¿ãã§ã¼ã¹é¨ */
|
---|
81 | virtual void attachModule(const string & filename) throw(Exception);
|
---|
82 | virtual void loadContents(void * dest, address_t address, size_t size) throw(Exception);
|
---|
83 | virtual address_t getSymbolAddress(const string & symbol) throw(Exception);
|
---|
84 | virtual std::string getArchitecture(void) throw();
|
---|
85 |
|
---|
86 | TESTSUITE_PROTOTYPE(main)
|
---|
87 | };
|
---|
88 |
|
---|
89 | FileContainerWindowsImpl instance_of_FileContaienrWindowsImpl;
|
---|
90 |
|
---|
91 | /* ã³ã³ã¹ãã©ã¯ã¿ */
|
---|
92 | FileContainerWindowsImpl::FileContainerWindowsImpl(void) throw()
|
---|
93 | : process(NULL), image(), base(0)
|
---|
94 | {}
|
---|
95 |
|
---|
96 | /* ãã¹ãã©ã¯ã¿ */
|
---|
97 | FileContainerWindowsImpl::~FileContainerWindowsImpl(void) throw()
|
---|
98 | {
|
---|
99 | if(isLoaded()) {
|
---|
100 | ::UnMapAndLoad(&image);
|
---|
101 | ::SymUnloadModule(process, base);
|
---|
102 | process = NULL;
|
---|
103 | base = 0;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* 対象ã¢ã¸ã¥ã¼ã«ã®å²ä» */
|
---|
108 | void FileContainerWindowsImpl::attachModule(const std::string & _filename) throw(Exception)
|
---|
109 | {
|
---|
110 | string filename(_filename);
|
---|
111 |
|
---|
112 | process = ::GetCurrentProcess();
|
---|
113 | if(::SymInitialize( process , NULL, FALSE) == FALSE)
|
---|
114 | ExceptionMessage("[Internal Error] ImageHelper API initialization failure","[å
|
---|
115 | é¨ã¨ã©ã¼] åæåã«å¤±æãã¾ãã (ImageHlp)").throwException();
|
---|
116 |
|
---|
117 | base = ::SymLoadModule(process, NULL, (PSTR)filename.c_str(), NULL, 0, 0);
|
---|
118 |
|
---|
119 | image.SizeOfImage = sizeof(LOADED_IMAGE);
|
---|
120 | if(::MapAndLoad((PSTR)filename.c_str(), NULL, &image, FALSE, TRUE) == FALSE)
|
---|
121 | ExceptionMessage("[Internel error] Module loading failure [%]","[å
|
---|
122 | é¨ã¨ã©ã¼] ã¢ã¸ã¥ã¼ã«ã®èªã¿è¾¼ã¿ã«å¤±æãã¾ãã [%]") << filename << throwException;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* å
|
---|
126 | 容ã®åå¾ */
|
---|
127 | void FileContainerWindowsImpl::loadContents(void * dest, address_t address, size_t size) throw(Exception)
|
---|
128 | {
|
---|
129 | PIMAGE_SECTION_HEADER header;
|
---|
130 | unsigned int i;
|
---|
131 |
|
---|
132 | address -= base;
|
---|
133 | for(i=0;i<image.NumberOfSections;i++) {
|
---|
134 | header = image.Sections+i;
|
---|
135 | if(address >= header->VirtualAddress && address < header->VirtualAddress + header->SizeOfRawData) {
|
---|
136 | address -= header->VirtualAddress - header->PointerToRawData;
|
---|
137 | ::CopyMemory(dest,image.MappedAddress + address,size);
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | if(i == image.NumberOfSections)
|
---|
143 | ExceptionMessage("[Internel error] Memory read with unmapped address","[å
|
---|
144 | é¨ã¨ã©ã¼] ãããããã¦ãªãã¢ãã¬ã¹ã使ã£ã¦ã¡ã¢ãªãªã¼ããè¡ããã¾ãã").throwException();
|
---|
145 | }
|
---|
146 |
|
---|
147 | /* ã·ã³ãã«ã¢ãã¬ã¹ã®è§£æ±º */
|
---|
148 | FileContainer::address_t FileContainerWindowsImpl::getSymbolAddress(const string & _symbol) throw(Exception)
|
---|
149 | {
|
---|
150 | FileContainer::address_t result = 0;
|
---|
151 | string symbol(_symbol);
|
---|
152 | IMAGEHLP_SYMBOL sym;
|
---|
153 |
|
---|
154 | if(process == NULL || base == 0)
|
---|
155 | ExceptionMessage("Not initialized","åæåããã¦ã¾ãã").throwException();
|
---|
156 |
|
---|
157 | sym.SizeOfStruct = sizeof(sym);
|
---|
158 | sym.MaxNameLength = 0;
|
---|
159 |
|
---|
160 | if(::SymGetSymFromName(process, (PSTR)symbol.c_str(), &sym) == TRUE)
|
---|
161 | result = static_cast<FileContainer::address_t>(sym.Address);
|
---|
162 |
|
---|
163 | if(result == 0)
|
---|
164 | ExceptionMessage("Unknown symbol [%]","䏿£ãªã·ã³ãã«å [%]") << symbol << throwException;
|
---|
165 |
|
---|
166 | return static_cast<FileContainer::address_t>(sym.Address);
|
---|
167 | }
|
---|
168 |
|
---|
169 | string FileContainerWindowsImpl::getArchitecture(void) throw()
|
---|
170 | { return "Windows (Win32)"; }
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | //---------------------------------------------
|
---|
175 |
|
---|
176 | #ifdef TESTSUITE
|
---|
177 | #include "base/coverage_undefs.h"
|
---|
178 |
|
---|
179 | #pragma warning(disable:4311) //'reinterpret_cast' : ãã¤ã³ã¿ã 'const int *__w64 ' ãã 'FileContainer::address_t' ã¸åãè©°ãã¾ãã
|
---|
180 |
|
---|
181 | extern "C" const int FileContainerWindowsImplTestVariable = 0x01234567;
|
---|
182 | extern "C" const int _FileContainerWindowsImplTestVariableWithUnderbar = 0x89abcdef;
|
---|
183 |
|
---|
184 | TESTSUITE(main, FileContainerWindowsImpl)
|
---|
185 | {
|
---|
186 | PREDECESSOR("TFileContainer");
|
---|
187 |
|
---|
188 | SingletonBase::ContextChain chain;
|
---|
189 | chain.saveContext<RuntimeObjectTable>();
|
---|
190 | chain.renewInstance();
|
---|
191 |
|
---|
192 | BEGIN_CASE("attachModule","attachModule") {
|
---|
193 | BEGIN_CASE("1","å®è¡ãã¦ããããã°ã©ã ãéããã") {
|
---|
194 | FileContainerWindowsImpl fcwi;
|
---|
195 | bool result = true;
|
---|
196 | try { fcwi.attachModule(TestSuite::getProgName()); } catch(...) { result = false; }
|
---|
197 |
|
---|
198 | TEST_CASE("1", "ä¾å¤ã¯èµ·ããªã", result);
|
---|
199 | } END_CASE;
|
---|
200 |
|
---|
201 | BEGIN_CASE("2","åå¨ããªããã¡ã¤ã«åã§ä¾å¤") {
|
---|
202 | FileContainerWindowsImpl fcwi;
|
---|
203 | bool result = false;
|
---|
204 | try { fcwi.attachModule("..."); } catch(...) { result = true; }
|
---|
205 |
|
---|
206 | TEST_CASE("1", "ä¾å¤ãèµ·ãã", result);
|
---|
207 | } END_CASE;
|
---|
208 | } END_CASE;
|
---|
209 |
|
---|
210 | BEGIN_CASE("getSymbolAddress","getSymbolAddress") {
|
---|
211 | FileContainerWindowsImpl fcwi;
|
---|
212 |
|
---|
213 | BEGIN_CASE("1","åæåãã¦ããªãç¶æ
|
---|
214 | ã§æ¤ç´¢ãã") {
|
---|
215 | bool result = false;
|
---|
216 | try { fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = true; }
|
---|
217 | TEST_CASE("1","ä¾å¤ãããã", result);
|
---|
218 | } END_CASE;
|
---|
219 |
|
---|
220 | fcwi.attachModule(TestSuite::getProgName());
|
---|
221 |
|
---|
222 | BEGIN_CASE("2","åå¨ããã·ã³ãã«ãæ¤ç´¢ãã") {
|
---|
223 | FileContainer::address_t addr = 0;
|
---|
224 | bool result = true;
|
---|
225 |
|
---|
226 | try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = false; }
|
---|
227 |
|
---|
228 | TEST_CASE("1","ä¾å¤ã¯èµ·ããªã", result);
|
---|
229 | TEST_CASE("2","ã¢ãã¬ã¹ãæ£ãã", addr == reinterpret_cast<FileContainer::address_t>(&FileContainerWindowsImplTestVariable));
|
---|
230 | } END_CASE;
|
---|
231 |
|
---|
232 | BEGIN_CASE("3","ä½è¨ãª_ãåæã«ä»å ããªã") {
|
---|
233 | FileContainer::address_t addr = 0;
|
---|
234 | bool result = false;
|
---|
235 |
|
---|
236 | try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariableWithUnderbar"); } catch(...) { result = true; }
|
---|
237 |
|
---|
238 | TEST_CASE("1","ä¾å¤ãèµ·ãã", result);
|
---|
239 | TEST_CASE("2","ã¢ãã¬ã¹ã¯0ã®ã¾ã¾", addr == 0);
|
---|
240 | } END_CASE;
|
---|
241 |
|
---|
242 | BEGIN_CASE("4","åå¨ããªãã·ã³ãã«ãæ¤ç´¢ãã") {
|
---|
243 | FileContainer::address_t addr = 0;
|
---|
244 | bool result = false;
|
---|
245 |
|
---|
246 | try { addr = fcwi.getSymbolAddress("____unknown____"); } catch(...) { result = true; }
|
---|
247 |
|
---|
248 | TEST_CASE("1","ä¾å¤ãããã", result);
|
---|
249 | } END_CASE;
|
---|
250 | } END_CASE;
|
---|
251 |
|
---|
252 | BEGIN_CASE("loadContents","loadContents") {
|
---|
253 | FileContainerWindowsImpl fcwi;
|
---|
254 | fcwi.attachModule(TestSuite::getProgName());
|
---|
255 |
|
---|
256 | BEGIN_CASE("1","åå¨ãã夿°ãèªã¿åºã") {
|
---|
257 | FileContainer::address_t addr;
|
---|
258 | int i;
|
---|
259 |
|
---|
260 | addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable");
|
---|
261 | bool result = true;
|
---|
262 | try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(...) { result = false; }
|
---|
263 |
|
---|
264 | TEST_CASE("1","ä¾å¤ã¯èµ·ããªã", result);
|
---|
265 | TEST_CASE("2","èªã¿åºãããå
|
---|
266 | å®¹ã¯æ£ãã", i == FileContainerWindowsImplTestVariable);
|
---|
267 | } END_CASE;
|
---|
268 |
|
---|
269 | BEGIN_CASE("2","åå¨ããªã夿°ãèªã¿åºã") {
|
---|
270 | FileContainer::address_t addr;
|
---|
271 | int i;
|
---|
272 |
|
---|
273 | addr = ~0;
|
---|
274 | bool result = false;
|
---|
275 | try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(Exception &) { result = true; }
|
---|
276 |
|
---|
277 | TEST_CASE("1","ä¾å¤ãèµ·ãã", result);
|
---|
278 | } END_CASE;
|
---|
279 |
|
---|
280 | } END_CASE;
|
---|
281 |
|
---|
282 | chain.restoreContext();
|
---|
283 | }
|
---|
284 |
|
---|
285 | #endif /* TESTSUITE */
|
---|
286 |
|
---|
287 | #endif /* FILECONTAINER_WINDOWS || TESTSUITE */
|
---|
288 |
|
---|