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

Last change on this file since 258 was 258, checked in by dai_9181, 17 years ago

Linkerの骨格を作成した

File size: 3.5 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 /*
106 BOOST_FOREACH( ObjectModule *pObjectModule, objectModules )
107 {
108 }*/
109 ObjectModule &masterObjectModule = *objectModules[0];
110
111 nativeCode.Put( masterObjectModule.globalNativeCode, false );
112
113 masterObjectModule.meta.GetUserProcs().Iterator_Reset();
114 while( masterObjectModule.meta.GetUserProcs().Iterator_HasNext() )
115 {
116 const UserProc *pUserProc = masterObjectModule.meta.GetUserProcs().Iterator_GetNext();
117
118 if( pUserProc->IsUsing() )
119 {
120 pUserProc->SetBeginOpAddress( nativeCode.GetSize() );
121
122 nativeCode.Put( pUserProc->GetNativeCode(), false );
123
124 pUserProc->SetEndOpAddress( nativeCode.GetSize() );
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.