source: dev/trunk/abdev/BasicCompiler_Common/src/Linker.cpp@ 270

Last change on this file since 270 was 263, checked in by dai_9181, 17 years ago
File size: 3.6 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5
6// データテーブルスケジュール
7void Linker::ResolveDataTableSchedules( long dataSectionBaseOffset )
8{
9 BOOST_FOREACH( const Schedule &schedule, nativeCode.GetSchedules() )
10 {
11 if( schedule.GetType() == Schedule::DataTable )
12 {
13 nativeCode.Overwrite(
14 schedule.GetOffset(),
15 static_cast<long>( nativeCode.GetLong( schedule.GetOffset() ) + imageBase + dataSectionBaseOffset )
16 );
17 }
18 }
19}
20
21// DLL関数スケジュール
22void Linker::ResolveDllProcSchedules( long codeSectionBaseOffset, long importSectionBaseOffset, long lookupSize, long hintSize )
23{
24 BOOST_FOREACH( const Schedule &schedule, nativeCode.GetSchedules() )
25 {
26 if( schedule.GetType() == Schedule::DllProc )
27 {
28#ifdef _AMD64_
29 nativeCode.Overwrite(
30 schedule.GetOffset(),
31 static_cast<long>( importSectionBaseOffset + schedule.GetDllProc().GetLookupAddress()
32 - ( codeSectionBaseOffset + schedule.GetOffset() + sizeof(long) ) )
33 );
34#else
35 nativeCode.Overwrite(
36 schedule.GetOffset(),
37 static_cast<long>( imageBase + importSectionBaseOffset + lookupSize + hintSize
38 + schedule.GetDllProc().GetLookupAddress() )
39 );
40#endif
41 }
42 }
43}
44
45// ユーザ定義関数スケジュール
46void Linker::ResolveUserProcSchedules( long codeSectionBaseOffset )
47{
48 BOOST_FOREACH( const Schedule &schedule, nativeCode.GetSchedules() )
49 {
50 if( schedule.GetType() == Schedule::UserProc
51 || schedule.GetType() == Schedule::AddressOf )
52 {
53 if( schedule.GetUserProc().GetBeginOpAddress() == 0
54 && schedule.GetUserProc().GetEndOpAddress() == 0 )
55 {
56 SetError();
57 }
58
59 if( schedule.GetType() == Schedule::UserProc )
60 {
61 nativeCode.Overwrite(
62 schedule.GetOffset(),
63 static_cast<long>( schedule.GetUserProc().GetBeginOpAddress() - ( schedule.GetOffset() + sizeof(long) ) )
64 );
65 }
66 else if( schedule.GetType() == Schedule::AddressOf )
67 {
68 nativeCode.Overwrite(
69 schedule.GetOffset(),
70 static_cast<long>( schedule.GetUserProc().GetBeginOpAddress() + imageBase + codeSectionBaseOffset )
71 );
72 }
73 }
74 }
75}
76
77// グローバル変数スケジュール
78void Linker::ResolveGlobalVarSchedules( long rwSectionBaseOffset )
79{
80 BOOST_FOREACH( const Schedule &schedule, nativeCode.GetSchedules() )
81 {
82 if( schedule.GetType() == Schedule::GlobalVar )
83 {
84 if( nativeCode.GetLong( schedule.GetOffset() ) & 0x80000000 )
85 {
86 extern int AllInitGlobalVarSize;
87 nativeCode.Overwrite(
88 schedule.GetOffset(),
89 static_cast<long>( AllInitGlobalVarSize + (nativeCode.GetLong( schedule.GetOffset() ) & 0x7FFFFFFF) + imageBase + rwSectionBaseOffset )
90 );
91 }
92 else
93 {
94 nativeCode.Overwrite(
95 schedule.GetOffset(),
96 static_cast<long>( nativeCode.GetLong( schedule.GetOffset() ) + imageBase + rwSectionBaseOffset )
97 );
98 }
99 }
100 }
101}
102
103void Linker::Link( vector<ObjectModule *> &objectModules )
104{
105 // nativeCodeは初期状態でなければならない
106 if( nativeCode.GetSize() > 0 )
107 {
108 SetError();
109 }
110
111 /*
112 BOOST_FOREACH( ObjectModule *pObjectModule, objectModules )
113 {
114 }*/
115 ObjectModule &masterObjectModule = *objectModules[0];
116
117 nativeCode.Put( masterObjectModule.globalNativeCode, false );
118
119 masterObjectModule.meta.GetUserProcs().Iterator_Reset();
120 while( masterObjectModule.meta.GetUserProcs().Iterator_HasNext() )
121 {
122 const UserProc *pUserProc = masterObjectModule.meta.GetUserProcs().Iterator_GetNext();
123
124 if( pUserProc->IsUsing() )
125 {
126 pUserProc->SetBeginOpAddress( nativeCode.GetSize() );
127
128 nativeCode.Put( pUserProc->GetNativeCode(), false );
129
130 pUserProc->SetEndOpAddress( nativeCode.GetSize() );
131 }
132 }
133}
Note: See TracBrowser for help on using the repository browser.