3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Just Standard Profile Kernel
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
9 * ¾åµÃøºî¸¢¼Ô¤Ï¡¤°Ê²¼¤Î (1)¡Á(4) ¤Î¾ò·ï¤«¡¤Free Software Foundation
10 * ¤Ë¤è¤Ã¤Æ¸øɽ¤µ¤ì¤Æ¤¤¤ë GNU General Public License ¤Î Version 2 ¤Ëµ
11 * ½Ò¤µ¤ì¤Æ¤¤¤ë¾ò·ï¤òËþ¤¿¤¹¾ì¹ç¤Ë¸Â¤ê¡¤ËÜ¥½¥Õ¥È¥¦¥§¥¢¡ÊËÜ¥½¥Õ¥È¥¦¥§¥¢
12 * ¤ò²þÊѤ·¤¿¤â¤Î¤ò´Þ¤à¡¥°Ê²¼Æ±¤¸¡Ë¤ò»ÈÍÑ¡¦Ê£À½¡¦²þÊÑ¡¦ºÆÇÛÉۡʰʲ¼¡¤
13 * ÍøÍѤȸƤ֡ˤ¹¤ë¤³¤È¤ò̵½þ¤ÇµöÂú¤¹¤ë¡¥
14 * (1) ËÜ¥½¥Õ¥È¥¦¥§¥¢¤ò¥½¡¼¥¹¥³¡¼¥É¤Î·Á¤ÇÍøÍѤ¹¤ë¾ì¹ç¤Ë¤Ï¡¤¾åµ¤ÎÃøºî
15 * ¸¢É½¼¨¡¤¤³¤ÎÍøÍѾò·ï¤ª¤è¤Ó²¼µ¤Î̵Êݾڵ¬Ä꤬¡¤¤½¤Î¤Þ¤Þ¤Î·Á¤Ç¥½¡¼
16 * ¥¹¥³¡¼¥ÉÃæ¤Ë´Þ¤Þ¤ì¤Æ¤¤¤ë¤³¤È¡¥
17 * (2) ËÜ¥½¥Õ¥È¥¦¥§¥¢¤ò¡¤¥é¥¤¥Ö¥é¥ê·Á¼°¤Ê¤É¡¤Â¾¤Î¥½¥Õ¥È¥¦¥§¥¢³«È¯¤Ë»È
18 * ÍѤǤ¤ë·Á¤ÇºÆÇÛÉÛ¤¹¤ë¾ì¹ç¤Ë¤Ï¡¤ºÆÇÛÉÛ¤Ëȼ¤¦¥É¥¥å¥á¥ó¥È¡ÊÍøÍÑ
19 * ¼Ô¥Þ¥Ë¥å¥¢¥ë¤Ê¤É¡Ë¤Ë¡¤¾åµ¤ÎÃøºî¸¢É½¼¨¡¤¤³¤ÎÍøÍѾò·ï¤ª¤è¤Ó²¼µ
20 * ¤Î̵Êݾڵ¬Äê¤ò·ÇºÜ¤¹¤ë¤³¤È¡¥
21 * (3) ËÜ¥½¥Õ¥È¥¦¥§¥¢¤ò¡¤µ¡´ï¤ËÁȤ߹þ¤à¤Ê¤É¡¤Â¾¤Î¥½¥Õ¥È¥¦¥§¥¢³«È¯¤Ë»È
22 * ÍѤǤ¤Ê¤¤·Á¤ÇºÆÇÛÉÛ¤¹¤ë¾ì¹ç¤Ë¤Ï¡¤¼¡¤Î¤¤¤º¤ì¤«¤Î¾ò·ï¤òËþ¤¿¤¹¤³
24 * (a) ºÆÇÛÉÛ¤Ëȼ¤¦¥É¥¥å¥á¥ó¥È¡ÊÍøÍѼԥޥ˥奢¥ë¤Ê¤É¡Ë¤Ë¡¤¾åµ¤ÎÃø
25 * ºî¸¢É½¼¨¡¤¤³¤ÎÍøÍѾò·ï¤ª¤è¤Ó²¼µ¤Î̵Êݾڵ¬Äê¤ò·ÇºÜ¤¹¤ë¤³¤È¡¥
26 * (b) ºÆÇÛÉۤηÁÂÖ¤ò¡¤Ê̤ËÄê¤á¤ëÊýË¡¤Ë¤è¤Ã¤Æ¡¤TOPPERS¥×¥í¥¸¥§¥¯¥È¤Ë
28 * (4) ËÜ¥½¥Õ¥È¥¦¥§¥¢¤ÎÍøÍѤˤè¤êľÀÜŪ¤Þ¤¿¤Ï´ÖÀÜŪ¤ËÀ¸¤¸¤ë¤¤¤«¤Ê¤ë»
29 * ³²¤«¤é¤â¡¤¾åµÃøºî¸¢¼Ô¤ª¤è¤ÓTOPPERS¥×¥í¥¸¥§¥¯¥È¤òÌÈÀÕ¤¹¤ë¤³¤È¡¥
31 * ËÜ¥½¥Õ¥È¥¦¥§¥¢¤Ï¡¤ÌµÊݾڤÇÄ󶡤µ¤ì¤Æ¤¤¤ë¤â¤Î¤Ç¤¢¤ë¡¥¾åµÃøºî¸¢¼Ô¤ª
32 * ¤è¤ÓTOPPERS¥×¥í¥¸¥§¥¯¥È¤Ï¡¤ËÜ¥½¥Õ¥È¥¦¥§¥¢¤Ë´Ø¤·¤Æ¡¤¤½¤ÎŬÍѲÄǽÀ¤â
33 * ´Þ¤á¤Æ¡¤¤¤¤«¤Ê¤ëÊݾڤâ¹Ô¤ï¤Ê¤¤¡¥¤Þ¤¿¡¤ËÜ¥½¥Õ¥È¥¦¥§¥¢¤ÎÍøÍѤˤè¤êľ
34 * ÀÜŪ¤Þ¤¿¤Ï´ÖÀÜŪ¤ËÀ¸¤¸¤¿¤¤¤«¤Ê¤ë»³²¤Ë´Ø¤·¤Æ¤â¡¤¤½¤ÎÀÕǤ¤òÉé¤ï¤Ê¤¤¡¥
36 * @(#) $Id: fc_windows.cpp,v 1.12 2003/12/20 06:51:58 takayuki Exp $
39 #if (defined(FILECONTAINER_WINDOWS) || defined(TESTSUITE)) && defined(_MSC_VER)
41 #pragma warning(disable:4786)
43 #include "base/filecontainer.h"
52 class FileContainerWindowsImpl : public FileContainer
59 inline bool isLoaded(void) const
63 FileContainerWindowsImpl(void) throw();
64 virtual ~FileContainerWindowsImpl(void) throw();
66 /* ¥¤¥ó¥¿¥Õ¥§¡¼¥¹Éô */
67 virtual void attachModule(const string & filename) throw(Exception);
68 virtual void loadContents(void * dest, address_t address, size_t size) throw(Exception);
69 virtual address_t getSymbolAddress(const string & symbol) throw(Exception);
70 virtual std::string getArchitecture(void) throw();
72 TESTSUITE_PROTOTYPE(main)
75 FileContainerWindowsImpl instance_of_FileContaienrWindowsImpl;
78 FileContainerWindowsImpl::FileContainerWindowsImpl(void) throw()
79 : process(NULL), image(), base(0)
83 FileContainerWindowsImpl::~FileContainerWindowsImpl(void) throw()
86 ::UnMapAndLoad(&image);
87 ::SymUnloadModule(process, base);
93 /* Âоݥ⥸¥å¡¼¥ë¤Î³äÉÕ */
94 void FileContainerWindowsImpl::attachModule(const std::string & _filename) throw(Exception)
96 string filename(_filename);
98 process = ::GetCurrentProcess();
99 if(::SymInitialize( process , NULL, FALSE) == FALSE)
100 ExceptionMessage("[Internal Error] ImageHelper API initialization failure","[ÆâÉô¥¨¥é¡¼] ½é´ü²½¤Ë¼ºÇÔ¤·¤Þ¤·¤¿ (ImageHlp)").throwException();
102 base = ::SymLoadModule(process, NULL, (PSTR)filename.c_str(), NULL, 0, 0);
104 image.SizeOfImage = sizeof(LOADED_IMAGE);
105 if(::MapAndLoad((PSTR)filename.c_str(), NULL, &image, FALSE, TRUE) == FALSE)
106 ExceptionMessage("[Internel error] Module loading failure [%]","[ÆâÉô¥¨¥é¡¼] ¥â¥¸¥å¡¼¥ë¤ÎÆɤ߹þ¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿ [%]") << filename << throwException;
110 void FileContainerWindowsImpl::loadContents(void * dest, address_t address, size_t size) throw(Exception)
112 PIMAGE_SECTION_HEADER header;
116 for(i=0;i<image.NumberOfSections;i++) {
117 header = image.Sections+i;
118 if(address >= header->VirtualAddress && address < header->VirtualAddress + header->SizeOfRawData) {
119 address -= header->VirtualAddress - header->PointerToRawData;
120 ::CopyMemory(dest,image.MappedAddress + address,size);
125 if(i == image.NumberOfSections)
126 ExceptionMessage("[Internel error] Memory read with unmapped address","[ÆâÉô¥¨¥é¡¼] ¥Þ¥Ã¥×¤µ¤ì¤Æ¤Ê¤¤¥¢¥É¥ì¥¹¤ò»È¤Ã¤Æ¥á¥â¥ê¥ê¡¼¥É¤¬¹Ô¤ï¤ì¤Þ¤·¤¿").throwException();
129 /* ¥·¥ó¥Ü¥ë¥¢¥É¥ì¥¹¤Î²ò·è */
130 FileContainer::address_t FileContainerWindowsImpl::getSymbolAddress(const string & _symbol) throw(Exception)
132 FileContainer::address_t result = 0;
133 string symbol(_symbol);
136 if(process == NULL || base == 0)
137 ExceptionMessage("Not initialized","½é´ü²½¤µ¤ì¤Æ¤Þ¤»¤ó").throwException();
139 sym.SizeOfStruct = sizeof(sym);
140 sym.MaxNameLength = 0;
142 if(::SymGetSymFromName(process, (PSTR)symbol.c_str(), &sym) == TRUE)
143 result = static_cast<FileContainer::address_t>(sym.Address);
146 ExceptionMessage("Unknown symbol [%]","ÉÔÀµ¤Ê¥·¥ó¥Ü¥ë̾ [%]") << symbol << throwException;
148 return static_cast<FileContainer::address_t>(sym.Address);
151 string FileContainerWindowsImpl::getArchitecture(void) throw()
152 { return "Windows (Win32)"; }
156 //---------------------------------------------
159 #include "base/coverage_undefs.h"
161 #pragma warning(disable:4311) //'reinterpret_cast' : ¥Ý¥¤¥ó¥¿¤ò 'const int *__w64 ' ¤«¤é 'FileContainer::address_t' ¤ØÀÚ¤êµÍ¤á¤Þ¤¹¡£
163 extern "C" const int FileContainerWindowsImplTestVariable = 0x01234567;
164 extern "C" const int _FileContainerWindowsImplTestVariableWithUnderbar = 0x89abcdef;
166 TESTSUITE(main, FileContainerWindowsImpl)
168 PREDECESSOR("TFileContainer");
170 SingletonBase::ContextChain chain;
171 chain.saveContext<RuntimeObjectTable>();
172 chain.renewInstance();
174 BEGIN_CASE("attachModule","attachModule") {
175 BEGIN_CASE("1","¼Â¹Ô¤·¤Æ¤¤¤ë¥×¥í¥°¥é¥à¤¬³«¤±¤ë¤«") {
176 FileContainerWindowsImpl fcwi;
178 try { fcwi.attachModule(TestSuite::getProgName()); } catch(...) { result = false; }
180 TEST_CASE("1", "Îã³°¤Ïµ¯¤¤Ê¤¤", result);
183 BEGIN_CASE("2","¸ºß¤·¤Ê¤¤¥Õ¥¡¥¤¥ë̾¤ÇÎã³°") {
184 FileContainerWindowsImpl fcwi;
186 try { fcwi.attachModule("..."); } catch(...) { result = true; }
188 TEST_CASE("1", "Îã³°¤¬µ¯¤¤ë", result);
192 BEGIN_CASE("getSymbolAddress","getSymbolAddress") {
193 FileContainerWindowsImpl fcwi;
195 BEGIN_CASE("1","½é´ü²½¤·¤Æ¤¤¤Ê¤¤¾õÂ֤Ǹ¡º÷¤¹¤ë") {
197 try { fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = true; }
198 TEST_CASE("1","Îã³°¤¬¤ª¤¤ë", result);
201 fcwi.attachModule(TestSuite::getProgName());
203 BEGIN_CASE("2","¸ºß¤¹¤ë¥·¥ó¥Ü¥ë¤ò¸¡º÷¤¹¤ë") {
204 FileContainer::address_t addr = 0;
207 try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable"); } catch(...) { result = false; }
209 TEST_CASE("1","Îã³°¤Ïµ¯¤¤Ê¤¤", result);
210 TEST_CASE("2","¥¢¥É¥ì¥¹¤¬Àµ¤·¤¤", addr == reinterpret_cast<FileContainer::address_t>(&FileContainerWindowsImplTestVariable));
213 BEGIN_CASE("3",";·×¤Ê_¤ò¾¡¼ê¤ËÉղ䷤ʤ¤") {
214 FileContainer::address_t addr = 0;
217 try { addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariableWithUnderbar"); } catch(...) { result = true; }
219 TEST_CASE("1","Îã³°¤¬µ¯¤¤ë", result);
220 TEST_CASE("2","¥¢¥É¥ì¥¹¤Ï0¤Î¤Þ¤Þ", addr == 0);
223 BEGIN_CASE("4","¸ºß¤·¤Ê¤¤¥·¥ó¥Ü¥ë¤ò¸¡º÷¤¹¤ë") {
224 FileContainer::address_t addr = 0;
227 try { addr = fcwi.getSymbolAddress("____unknown____"); } catch(...) { result = true; }
229 TEST_CASE("1","Îã³°¤¬¤ª¤¤ë", result);
233 BEGIN_CASE("loadContents","loadContents") {
234 FileContainerWindowsImpl fcwi;
235 fcwi.attachModule(TestSuite::getProgName());
237 BEGIN_CASE("1","¸ºß¤¹¤ëÊÑ¿ô¤òÆɤ߽Ф¹") {
238 FileContainer::address_t addr;
241 addr = fcwi.getSymbolAddress("FileContainerWindowsImplTestVariable");
243 try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(...) { result = false; }
245 TEST_CASE("1","Îã³°¤Ïµ¯¤¤Ê¤¤", result);
246 TEST_CASE("2","Æɤ߽Фµ¤ì¤¿ÆâÍƤÏÀµ¤·¤¤", i == FileContainerWindowsImplTestVariable);
249 BEGIN_CASE("2","¸ºß¤·¤Ê¤¤ÊÑ¿ô¤òÆɤ߽Ф¹") {
250 FileContainer::address_t addr;
255 try { fcwi.loadContents(&i, addr, sizeof(i)); } catch(Exception &) { result = true; }
257 TEST_CASE("1","Îã³°¤¬µ¯¤³¤ë", result);
262 chain.restoreContext();
265 #endif /* TESTSUITE */
267 #endif /* FILECONTAINER_WINDOWS || TESTSUITE */