Update contrib.
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\loader\exet.cpp
18 #define __INCLUDE_DEPENDENCY_GRAPH
27 #pragma warning(disable:4706)
30 const TInt KMaxHandlesPerDll=4;
31 const TInt KMaxHandles=KMaxHandlesPerDll*KNumModules;
33 extern "C" TInt _E32Startup();
35 extern "C" IMPORT_C void RegisterConstructorCall(TInt aDllNum);
36 extern "C" IMPORT_C void RegisterInitCall(TInt aDllNum);
37 extern "C" IMPORT_C void RegisterDestructorCall(TInt aDllNum);
39 #define PANIC() ExeTPanic(__LINE__)
40 #define EXET_ASSERT(c) ((void)((c)||(PANIC(),0)))
42 void ExeTPanic(TInt aLine)
44 User::Panic(_L("EXET"),aLine);
47 /******************************************************************************
49 ******************************************************************************/
51 class TDllList : public MDllList
55 virtual TBool IsPresent(const SDllInfo& aInfo);
56 virtual TInt Add(const SDllInfo& aInfo);
57 virtual void MoveToEnd(TInt aPos);
60 SDllInfo iInfo[KNumModules];
63 class CTestServer : public CServer2
67 virtual ~CTestServer();
68 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
69 virtual TInt RunError(TInt aError);
74 class CTestSession : public CSession2
77 virtual ~CTestSession();
78 virtual void CreateL();
79 virtual void ServiceL(const RMessage2& aMessage);
81 TInt GetExeDepList(const RMessage2& aMessage);
82 TInt GetCDList(const RMessage2& aMessage);
83 TInt LoadDll(const RMessage2& aMessage);
84 TInt CloseDll(TInt aHandle);
85 TInt CallBlkI(TInt aHandle, TInt aIn);
86 TInt CallRBlkI(TInt aHandle, TInt aIn);
91 class CDllInfo : public CBase
97 TInt StoreHandle(TInt aHandle);
98 void RemoveHandle(TInt aIndex);
101 void RegisterConstructorCall(TInt aDllNum);
102 void RegisterInitCall(TInt aDllNum);
103 void RegisterDestructorCall(TInt aDllNum);
108 TInt iHandles[KMaxHandles];
109 TInt iModuleNum[KMaxHandles];
110 TDllList iExeDepList;
111 TDllList iDllConstructList;
114 /******************************************************************************
116 ******************************************************************************/
117 #ifdef __MODULE_HAS_DATA
133 TFullName StartThread=RThread().FullName();
134 TName StartProcess=RProcess().Name();
135 TExeData TheExeDataObject;
138 : iFileName(RProcess().FileName())
141 CHKDEPS(r); // Check our dependencies are initialised
143 User::Panic(_L("CHKDEPS"),r);
144 iStartTime.HomeTime();
146 RegisterConstructorCall(EXENUM);
150 /******************************************************************************
152 ******************************************************************************/
156 Mem::Fill(iInfo, KNumModules*sizeof(SDllInfo), 0xff);
159 TBool TDllList::IsPresent(const SDllInfo& aInfo)
162 for (i=0; i<iCount; ++i)
164 if (iInfo[i].iDllNum==aInfo.iDllNum)
170 TInt TDllList::Add(const SDllInfo& aInfo)
172 EXET_ASSERT(iCount<KNumModules);
174 iInfo[iCount++]=aInfo;
178 void TDllList::MoveToEnd(TInt aPos)
182 SDllInfo x(iInfo[aPos]);
183 Mem::Move(iInfo+aPos, iInfo+aPos+1, (iCount-aPos-1)*sizeof(SDllInfo));
188 /******************************************************************************
189 * Class CTestSession/CTestServer
190 ******************************************************************************/
191 void ExceptionHandler(TExcType)
193 User::Leave(KErrGeneral);
196 TUint32 Read(TLinAddr a)
198 return *(volatile TUint32*)a;
201 TInt CheckReadable(TLinAddr a)
203 User::SetExceptionHandler(ExceptionHandler,KExceptionFault);
205 User::SetExceptionHandler(ExceptionHandler,0);
209 CTestSession::~CTestSession()
213 void CTestSession::CreateL()
215 // CSession2::CreateL();
218 void CTestSession::ServiceL(const RMessage2& aMessage)
220 TInt r=KErrNotSupported;
221 TInt mid=aMessage.Function();
224 case RLoaderTest::EMsgGetExeDepList:
225 r=GetExeDepList(aMessage);
227 case RLoaderTest::EMsgLoadDll:
230 case RLoaderTest::EMsgCallBlkI:
231 r=CallBlkI(aMessage.Int0(), aMessage.Int1());
233 case RLoaderTest::EMsgCallRBlkI:
234 r=CallRBlkI(aMessage.Int0(), aMessage.Int1());
236 case RLoaderTest::EMsgCloseDll:
237 r=CloseDll(aMessage.Int0());
239 case RLoaderTest::EMsgGetCDList:
240 r=GetCDList(aMessage);
242 case RLoaderTest::EMsgCheckReadable:
243 r=CheckReadable((TLinAddr)aMessage.Int0());
245 case RLoaderTest::EMsgExit:
247 CActiveScheduler::Stop();
252 aMessage.Complete(r);
255 TInt CTestSession::GetExeDepList(const RMessage2& aMsg)
257 TPtrC8 dep_list_ptr((const TUint8*)&iInfo->iExeDepList.iInfo, KNumModules*sizeof(SDllInfo));
258 aMsg.WriteL(0, dep_list_ptr, 0);
262 TInt CTestSession::GetCDList(const RMessage2& aMsg)
264 TPtrC8 list_ptr((const TUint8*)&iInfo->iDllConstructList.iInfo, KNumModules*sizeof(SDllInfo));
265 aMsg.WriteL(0, list_ptr, 0);
269 TInt CTestSession::LoadDll(const RMessage2& aMsg)
271 TInt module=aMsg.Int0();
273 TPtrC8 dll_list_ptr((const TUint8*)dll_list.iInfo, KNumModules*sizeof(SDllInfo));
274 TPtrC dllname=MODULE_FILENAME(module);
275 iInfo->SetupCDList();
277 TInt r=l.Load(dllname, TUidType());
280 TInitFunction f=(TInitFunction)l.Lookup(INIT_ORDINAL);
285 TBlkIFunction bf=(TBlkIFunction)l.Lookup(BLOCK_INC_ORDINAL);
287 TInt result=(*bf)(531441);
288 EXET_ASSERT(result==BlkIValue(module, 531441));
289 TInt h=iInfo->StoreHandle(l.Handle());
291 iInfo->iModuleNum[h]=module;
292 aMsg.WriteL(1, dll_list_ptr, 0);
296 TInt CTestSession::CallBlkI(TInt aHandle, TInt aIn)
298 TInt h=iInfo->iHandles[aHandle];
302 TBlkIFunction bf=(TBlkIFunction)l.Lookup(BLOCK_INC_ORDINAL);
307 TInt CTestSession::CallRBlkI(TInt aHandle, TInt aIn)
309 TInt h=iInfo->iHandles[aHandle];
313 TRBlkIFunction rbf=(TRBlkIFunction)l.Lookup(REC_BLOCK_INC_ORDINAL);
316 return (*rbf)(aIn,iInfo->iNextGen);
319 TInt CTestSession::CloseDll(TInt aHandle)
321 TInt h=iInfo->iHandles[aHandle];
322 TInt m=iInfo->iModuleNum[aHandle];
325 iInfo->iHandles[aHandle]=0;
326 iInfo->iModuleNum[aHandle]=-1;
327 iInfo->SetupCDList();
334 CTestServer::CTestServer()
335 : CServer2(0,ESharableSessions)
339 CTestServer::~CTestServer()
343 CSession2* CTestServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
346 CTestSession* s = new (ELeave) CTestSession;
351 _LIT(KExetErr,"EXETERR");
352 TInt CTestServer::RunError(TInt aError)
354 User::Panic(KExetErr,aError);
358 /******************************************************************************
360 ******************************************************************************/
363 #ifdef __MODULE_HAS_DATA
364 TInt init_mark=~((EXENUM+DLLNUMOFFSET)*(EXENUM+DLLNUMOFFSET));
365 if (InitFlag==init_mark)
371 for (i=0; i<16; ++i) x|=Bss[i];
378 if (StartProcess!=RProcess().Name())
380 if (TheExeDataObject.iTest1!=299792458)
382 if (TheExeDataObject.iFileName != RProcess().FileName())
385 RDebug::Print(_L("ChkC %S OK"),&TheExeDataObject.iFileName);
390 TInt Init(MDllList& aList)
395 info.iEntryPointAddress=((TInt)&_E32Startup);
398 info.iModuleHandle=ldd.ModuleHandleFromAddr((TInt)&_E32Startup);
400 if (!aList.IsPresent(info))
402 TInt pos=aList.Add(info);
403 INITDEPS(r,aList); // Call Init on our dependencies
404 aList.MoveToEnd(pos);
405 #ifdef __MODULE_HAS_DATA
407 r=ChkC(); // Check initial values for .data/.bss and check constructors have been called
409 RegisterInitCall(DLLNUM);
416 Mem::Fill(iModuleNum, sizeof(iModuleNum), 0xff);
419 CDllInfo::~CDllInfo()
423 TInt CDllInfo::Create()
426 r=UserSvr::DllSetTls(0, this);
428 r=UserSvr::DllSetTls(TLS_INDEX, NULL);
436 void CDllInfo::RegisterConstructorCall(TInt aDllNum)
441 void CDllInfo::RegisterInitCall(TInt aDllNum)
446 void CDllInfo::RegisterDestructorCall(TInt aDllNum)
451 TInt CDllInfo::StoreHandle(TInt aHandle)
453 if (iHandleCount==KMaxHandles)
456 for (; i<KMaxHandles && iHandles[i]!=0; ++i) {}
459 for (i=0; i<iNextHandle && iHandles[i]!=0; ++i) {}
460 EXET_ASSERT(i!=iNextHandle);
468 void CDllInfo::RemoveHandle(TInt aIndex)
474 void CDllInfo::SetupCDList()
476 new (&iDllConstructList) TDllList;
477 EXET_ASSERT(UserSvr::DllSetTls(TLS_INDEX, &iDllConstructList)==KErrNone);
480 /******************************************************************************
482 ******************************************************************************/
483 extern "C" __MODULE_EXPORT void RegisterConstructorCall(TInt aDllNum)
485 CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
487 p->RegisterConstructorCall(aDllNum);
490 extern "C" __MODULE_EXPORT void RegisterInitCall(TInt aDllNum)
492 CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
494 p->RegisterInitCall(aDllNum);
497 extern "C" __MODULE_EXPORT void RegisterDestructorCall(TInt aDllNum)
499 CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
501 p->RegisterDestructorCall(aDllNum);
504 GLDEF_C TInt E32Main()
506 CTrapCleanup* cleanup=CTrapCleanup::New();
507 EXET_ASSERT(cleanup);
508 CActiveScheduler* sched=new CActiveScheduler;
510 CActiveScheduler::Install(sched);
511 CTestServer* svr=new CTestServer;
514 User::CommandLine(suffix);
515 TName svr_name=KServerName();
518 svr_name.Append('.');
521 EXET_ASSERT(svr->Start(svr_name)==KErrNone);
522 CDllInfo* dllinfo=new CDllInfo;
523 EXET_ASSERT(dllinfo);
524 EXET_ASSERT(dllinfo->Create()==KErrNone);
527 CActiveScheduler::Start();
529 UserSvr::DllFreeTls(0);
530 UserSvr::DllFreeTls(TLS_INDEX);