Index: trunk/abdev/BasicCompiler32/BasicCompiler.vcproj
===================================================================
--- trunk/abdev/BasicCompiler32/BasicCompiler.vcproj	(revision 272)
+++ trunk/abdev/BasicCompiler32/BasicCompiler.vcproj	(revision 273)
@@ -1317,4 +1317,8 @@
 					</File>
 					<File
+						RelativePath="..\BasicCompiler_Common\src\ObjectModule.cpp"
+						>
+					</File>
+					<File
 						RelativePath="..\BasicCompiler_Common\src\Procedure.cpp"
 						>
Index: trunk/abdev/BasicCompiler32/Compile_Var.cpp
===================================================================
--- trunk/abdev/BasicCompiler32/Compile_Var.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler32/Compile_Var.cpp	(revision 273)
@@ -14,7 +14,4 @@
 //変数
 // TODO: xml未完成
-int AllGlobalVarSize;
-int AllInitGlobalVarSize;
-
 int AllLocalVarSize;
 
Index: trunk/abdev/BasicCompiler32/MakePeHdr.cpp
===================================================================
--- trunk/abdev/BasicCompiler32/MakePeHdr.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler32/MakePeHdr.cpp	(revision 273)
@@ -149,5 +149,4 @@
 	//////////////////
 	// データテーブル
-	compiler.GetObjectModule().dataTable.Clear();
 	if(bDebugCompile){
 		compiler.GetObjectModule().dataTable.Add( (long)0x00000002 );
@@ -493,5 +492,5 @@
 
 		//グローバル実行領域のコードサイズ
-		GlobalOpBufferSize=obp;
+		GlobalOpBufferSize = compiler.linker.GetNativeCode().GetSize();
 
 		//With情報のメモリを解放
@@ -541,7 +540,5 @@
 	trace( "コード生成が終了しました。" );
 
-	vector<ObjectModule *> masterObjectModules;
-	masterObjectModules.push_back( &compiler.GetObjectModule() );
-	compiler.linker.Link( masterObjectModules );
+	compiler.linker.Link( compiler.GetObjectModule() );
 
 	oldSourceLines = compiler.linker.GetNativeCode().GetSourceLines();
@@ -884,8 +881,10 @@
 	//グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
 	//（デバッグ情報で利用される）
-	extern int AllInitGlobalVarSize;
 	BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
 		if(pVar->GetOffsetAddress()&0x80000000){
-			pVar->SetOffsetAddress( (pVar->GetOffsetAddress()&0x7FFFFFFF)+AllInitGlobalVarSize );
+			pVar->SetOffsetAddress(
+				(pVar->GetOffsetAddress()&0x7FFFFFFF)
+				+ compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
+			);
 		}
 	}
@@ -913,6 +912,14 @@
 
 	//コードセッションのファイル上のサイズ
-	if(obp%FILE_ALIGNMENT) FileSize_CodeSection=obp+(FILE_ALIGNMENT-obp%FILE_ALIGNMENT);
-	else FileSize_CodeSection=obp;
+	if(compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT)
+	{
+		FileSize_CodeSection =
+			compiler.linker.GetNativeCode().GetSize()
+			+ (FILE_ALIGNMENT-compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT);
+	}
+	else
+	{
+		FileSize_CodeSection = compiler.linker.GetNativeCode().GetSize();
+	}
 	if(FileSize_CodeSection) bUse_CodeSection=1;
 	else bUse_CodeSection=0;
@@ -947,7 +954,15 @@
 
 	//リライタブルセクションのファイル上のサイズ（グローバル変数の初期情報のみを格納）
-	if(AllInitGlobalVarSize%FILE_ALIGNMENT) FileSize_RWSection=AllInitGlobalVarSize+(FILE_ALIGNMENT-AllInitGlobalVarSize%FILE_ALIGNMENT);
+	if( compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize() % FILE_ALIGNMENT )
+	{
+		FileSize_RWSection =
+			compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
+			+ (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()%FILE_ALIGNMENT);
+	}
 	else{
-		if(AllInitGlobalVarSize) FileSize_RWSection=AllInitGlobalVarSize;
+		if( compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize() )
+		{
+			FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize();
+		}
 		else FileSize_RWSection=FILE_ALIGNMENT;
 	}
@@ -1011,6 +1026,6 @@
 
 	//リライタブルセクションのメモリ上のサイズ
-	extern int AllGlobalVarSize;
-	i=AllInitGlobalVarSize+AllGlobalVarSize;
+	i = compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
+		+ compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
 	if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
 	else MemSize_RWSection=i;
@@ -1189,5 +1204,5 @@
 			//初期バッファなし
 			*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))=
-				AllInitGlobalVarSize+
+				compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize() +
 				(*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x7FFFFFFF)+
 				ImageBase+MemPos_RWSection;
@@ -1207,13 +1222,4 @@
 	compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
 	compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
-
-	if( obp == compiler.linker.GetNativeCode().GetSize() )
-	{
-		memcpy( OpBuffer, compiler.linker.GetNativeCode().GetCodeBuffer(), compiler.linker.GetNativeCode().GetSize() );
-	}
-	else
-	{
-		SetError();
-	}
 
 
@@ -1475,5 +1481,6 @@
 	memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
 	lstrcpy((char *)RWSectionHeader.Name,".data");
-	RWSectionHeader.Misc.VirtualSize=			AllInitGlobalVarSize+AllGlobalVarSize;
+	RWSectionHeader.Misc.VirtualSize=			compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
+												+ compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
 	RWSectionHeader.VirtualAddress=				MemPos_RWSection;
 	RWSectionHeader.SizeOfRawData=				FileSize_RWSection;
@@ -1605,5 +1612,11 @@
 
 	//コード
-	WriteFile(hFile,OpBuffer,obp,(DWORD *)&i2,NULL);
+	WriteFile(
+		hFile,
+		compiler.linker.GetNativeCode().GetCodeBuffer(),
+		compiler.linker.GetNativeCode().GetSize(),
+		(DWORD *)&i2,
+		NULL
+	);
 	i+=i2;
 
Index: trunk/abdev/BasicCompiler_Common/MakeExe.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/MakeExe.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/MakeExe.cpp	(revision 273)
@@ -135,12 +135,4 @@
 	//"コンパイル中..."
 	CompileMessage(STRING_COMPILE_COMPILING);
-
-	//グローバル変数に関する情報
-	// TODO: AllGlobalVarSize、AllInitGlobalVarSizeをVariablesクラスに入れる
-	extern int AllGlobalVarSize;
-	extern int AllInitGlobalVarSize;
-	compiler.GetObjectModule().meta.GetGlobalVars().clear();
-	AllGlobalVarSize=0;
-	AllInitGlobalVarSize=0;
 
 	Compile();
Index: trunk/abdev/BasicCompiler_Common/VariableOpe.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/VariableOpe.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/VariableOpe.cpp	(revision 273)
@@ -970,6 +970,4 @@
 	// グローバル変数を追加
 	/////////////////////////
-	extern int AllInitGlobalVarSize;
-	extern int AllGlobalVarSize;
 
 	if( compiler.GetObjectModule().meta.GetGlobalVars().DuplicateCheck( Symbol( name ) ) ){
@@ -997,36 +995,8 @@
 
 	// 変数を追加
-	compiler.GetObjectModule().meta.GetGlobalVars().push_back( pVar );
-
-	//アラインメントを考慮
-	int alignment = 0;
-	if( pVar->GetType().IsStruct() ){
-		alignment = pVar->GetType().GetClass().GetFixedAlignment();
-	}
-
-	if(InitBuf[0]||dwFlag==DIMFLAG_INITDEBUGVAR){
-		//初期バッファがあるとき
-
-		if( alignment ){
-			if( AllInitGlobalVarSize % alignment ){
-				AllInitGlobalVarSize += alignment - (AllInitGlobalVarSize % alignment);
-			}
-		}
-
-		pVar->SetOffsetAddress( AllInitGlobalVarSize );
-		AllInitGlobalVarSize += pVar->GetMemorySize();
-	}
-	else{
-		//初期バッファがないとき
-
-		if( alignment ){
-			if( AllGlobalVarSize % alignment ){
-				AllGlobalVarSize += alignment - (AllGlobalVarSize % alignment);
-			}
-		}
-
-		pVar->SetOffsetAddress( AllGlobalVarSize | 0x80000000 );
-		AllGlobalVarSize += pVar->GetMemorySize();
-	}
+	compiler.GetObjectModule().meta.GetGlobalVars().Add(
+		pVar,
+		( InitBuf[0] != 0 || dwFlag == DIMFLAG_INITDEBUGVAR )
+	);
 
 	if(InitBuf[0]){
@@ -1035,8 +1005,10 @@
 			//初期バッファにデータをセット
 			extern BYTE *initGlobalBuf;
-			initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
+			initGlobalBuf = (BYTE *)HeapReAlloc(
+				hHeap,
 				HEAP_ZERO_MEMORY,
 				initGlobalBuf,
-				AllInitGlobalVarSize);
+				compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
+			);
 
 			result = SetInitGlobalData(pVar->GetOffsetAddress(),
Index: trunk/abdev/BasicCompiler_Common/include/DataTable.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/DataTable.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/DataTable.h	(revision 273)
@@ -7,4 +7,10 @@
 	char *buffer;
 	int size;
+
+	void Realloc( int size )
+	{
+		this->buffer = (char *)realloc( this->buffer, size + 100 );
+		this->size = size;
+	}
 
 	// XMLシリアライズ用
@@ -75,5 +81,5 @@
 	}
 
-	void operator =( const DataTable &dataTable )
+	void operator = ( const DataTable &dataTable )
 	{
 		Clear();
@@ -81,5 +87,4 @@
 	}
 
-	void Realloc( int size );
 	int AddBinary( const void *buffer, int size );
 	int Add( _int64 i64data );
@@ -89,4 +94,8 @@
 	int AddString( const char *str, int length );
 	int AddString( const char *str );
+	void Add( const DataTable &dataTable )
+	{
+		AddBinary( dataTable.GetPtr(), dataTable.GetSize() );
+	}
 
 	const void *GetPtr() const;
Index: trunk/abdev/BasicCompiler_Common/include/Linker.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/Linker.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/Linker.h	(revision 273)
@@ -35,4 +35,4 @@
 
 	// リンク
-	void Link( vector<ObjectModule *> &objectModules );
+	void Link( ObjectModule &masterObjectModule );
 };
Index: trunk/abdev/BasicCompiler_Common/include/Meta.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/Meta.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/Meta.h	(revision 273)
@@ -75,5 +75,5 @@
 
 	// 静的リンク
-	void StaticLink( Meta &meta );
+	void StaticLink( Meta &meta, long dataSectionBaseOffset );
 
 	const NamespaceScopesCollection &GetNamespaces() const
Index: trunk/abdev/BasicCompiler_Common/include/NativeCode.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/NativeCode.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/NativeCode.h	(revision 273)
@@ -427,3 +427,5 @@
 	}
 	void NextSourceLine();
+
+	void ResetDataSectionBaseOffset( long dataSectionBaseOffset );
 };
Index: trunk/abdev/BasicCompiler_Common/include/ObjectModule.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/ObjectModule.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/ObjectModule.h	(revision 273)
@@ -28,4 +28,7 @@
 		ar & BOOST_SERIALIZATION_NVP( dataTable );
 	}
+
+public:
+	void StaticLink( ObjectModule &objectModule );
 };
 typedef std::vector<ObjectModule *> ObjectModules;
Index: trunk/abdev/BasicCompiler_Common/include/Variable.h
===================================================================
--- trunk/abdev/BasicCompiler_Common/include/Variable.h	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/include/Variable.h	(revision 273)
@@ -214,6 +214,12 @@
 class Variables : public vector<Variable *>
 {
+	int allSize;
+	int allInitSize;
 public:
-	Variables(){}
+	Variables()
+		: allSize( 0 )
+		, allInitSize( 0 )
+	{
+	}
 	~Variables(){
 		Clear();
@@ -225,4 +231,6 @@
 		}
 
+		allSize = 0;
+		allInitSize = 0;
 		clear();
 	}
@@ -235,6 +243,16 @@
 
 	const Variable *BackSearch( const Symbol &symbol ) const;
-
 	const Variable *Find( const Symbol &symbol )const;
+
+	void Add( Variable *pVar, bool isInitArea );
+
+	int GetAllSize() const
+	{
+		return allSize;
+	}
+	int GetAllInitSize() const
+	{
+		return allInitSize;
+	}
 
 
Index: trunk/abdev/BasicCompiler_Common/src/Compiler.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/Compiler.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/Compiler.cpp	(revision 273)
@@ -13,5 +13,5 @@
 	{
 		// メタ情報
-		pNowObjectModule->meta.StaticLink( pStaticLibrary->meta );
+		pNowObjectModule->StaticLink( *pStaticLibrary );
 	}
 }
Index: trunk/abdev/BasicCompiler_Common/src/DataTable.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/DataTable.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/DataTable.cpp	(revision 273)
@@ -7,10 +7,4 @@
 #include <memory.h>
 #include <stdlib.h>
-
-void DataTable::Realloc( int size )
-{
-	this->buffer = (char *)realloc( this->buffer, size + 100 );
-	this->size = size;
-}
 
 int DataTable::AddBinary( const void *buffer, int size ){
Index: trunk/abdev/BasicCompiler_Common/src/Linker.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/Linker.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/Linker.cpp	(revision 273)
@@ -78,4 +78,6 @@
 void Linker::ResolveGlobalVarSchedules( long rwSectionBaseOffset )
 {
+	int allInitVarSize = compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize();
+
 	BOOST_FOREACH( const Schedule &schedule, nativeCode.GetSchedules() )
 	{
@@ -84,8 +86,7 @@
 			if( nativeCode.GetLong( schedule.GetOffset() ) & 0x80000000 )
 			{
-				extern int AllInitGlobalVarSize;
 				nativeCode.Overwrite(
 					schedule.GetOffset(),
-					static_cast<long>( AllInitGlobalVarSize + (nativeCode.GetLong( schedule.GetOffset() ) & 0x7FFFFFFF) + imageBase + rwSectionBaseOffset )
+					static_cast<long>( allInitVarSize + (nativeCode.GetLong( schedule.GetOffset() ) & 0x7FFFFFFF) + imageBase + rwSectionBaseOffset )
 				);
 			}
@@ -101,5 +102,5 @@
 }
 
-void Linker::Link( vector<ObjectModule *> &objectModules )
+void Linker::Link( ObjectModule &masterObjectModule )
 {
 	// nativeCodeは初期状態でなければならない
@@ -108,10 +109,4 @@
 		SetError();
 	}
-
-	/*
-	BOOST_FOREACH( ObjectModule *pObjectModule, objectModules )
-	{
-	}*/
-	ObjectModule &masterObjectModule = *objectModules[0];
 
 	nativeCode.Put( masterObjectModule.globalNativeCode, false );
Index: trunk/abdev/BasicCompiler_Common/src/Meta.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/Meta.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/Meta.cpp	(revision 273)
@@ -36,5 +36,5 @@
 }
 
-void Meta::StaticLink( Meta &meta )
+void Meta::StaticLink( Meta &meta, long dataSectionBaseOffset )
 {
 	// 名前空間
@@ -53,4 +53,7 @@
 		UserProc *pUserProc = meta.GetUserProcs().Iterator_GetNext();
 		pUserProc->isTargetObjectModule = false;
+
+		pUserProc->GetNativeCode().ResetDataSectionBaseOffset( dataSectionBaseOffset );
+
 		this->userProcs.Put( pUserProc );
 	}
Index: trunk/abdev/BasicCompiler_Common/src/NativeCode.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/NativeCode.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/NativeCode.cpp	(revision 273)
@@ -125,2 +125,16 @@
 	sourceLines.push_back( SourceLine( (long)sourceLines.size(), size, cp, sourceLineType ) );
 }
+
+void NativeCode::ResetDataSectionBaseOffset( long dataSectionBaseOffset )
+{
+	BOOST_FOREACH( const Schedule &schedule, schedules )
+	{
+		if( schedule.GetType() == Schedule::DataTable )
+		{
+			Overwrite(
+				schedule.GetOffset(),
+				GetLong( schedule.GetOffset() ) + dataSectionBaseOffset
+			);
+		}
+	}
+}
Index: trunk/abdev/BasicCompiler_Common/src/Variable.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/src/Variable.cpp	(revision 272)
+++ trunk/abdev/BasicCompiler_Common/src/Variable.cpp	(revision 273)
@@ -73,2 +73,37 @@
 	return NULL;
 }
+
+void Variables::Add( Variable *pVar, bool isInitArea )
+{
+	int alignment = 0;
+	if( pVar->GetType().IsStruct() ){
+		alignment = pVar->GetType().GetClass().GetFixedAlignment();
+	}
+
+	if( isInitArea ){
+		//初期バッファがあるとき
+
+		if( alignment ){
+			if( allInitSize % alignment ){
+				allInitSize += alignment - (allInitSize % alignment);
+			}
+		}
+
+		pVar->SetOffsetAddress( allInitSize );
+		allInitSize += pVar->GetMemorySize();
+	}
+	else{
+		//初期バッファがないとき
+
+		if( alignment ){
+			if( allSize % alignment ){
+				allSize += alignment - (allSize % alignment);
+			}
+		}
+
+		pVar->SetOffsetAddress( allSize | 0x80000000 );
+		allSize += pVar->GetMemorySize();
+	}
+
+	push_back( pVar );
+}
