From 1d09005542d7a350fd5dd5b1b8ebf7e6a3cd2e23 Mon Sep 17 00:00:00 2001 From: sergei Date: Thu, 26 Jul 2012 10:04:12 +0000 Subject: [PATCH] + Implemented two-stage removal of empty exe sections. Candidates for removal are first marked with oso_disabled flag, then actually removed. Descendants of TExeOutput that override MemPos_Start may modify list of sections pending removal. In particular, the COFF-specific .reloc section no longer has to be handled in base TExeOutput class. git-svn-id: trunk@21971 - --- compiler/link.pas | 2 +- compiler/ogbase.pas | 30 ++++++++++++++++++++++++------ compiler/ogcoff.pas | 19 ++++++++++--------- compiler/ognlm.pas | 12 ++++++------ 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/compiler/link.pas b/compiler/link.pas index 0ae610ccdd..b8838c9454 100644 --- a/compiler/link.pas +++ b/compiler/link.pas @@ -1295,7 +1295,7 @@ Implementation { if UseStabs then, this would remove STABS for empty linker scripts } exeoutput.MergeStabs; - exeoutput.RemoveEmptySections; + exeoutput.MarkEmptySections; if ErrorCount>0 then goto myexit; diff --git a/compiler/ogbase.pas b/compiler/ogbase.pas index dee5da250d..b40ff850a0 100644 --- a/compiler/ogbase.pas +++ b/compiler/ogbase.pas @@ -131,7 +131,9 @@ interface { Contains debug info and can be stripped } oso_debug, { Contains only strings } - oso_strings + oso_strings, + { Ignore this section } + oso_disabled ); TObjSectionOptions = set of TObjSectionOption; @@ -488,8 +490,9 @@ interface procedure FixupRelocations; procedure RemoveUnusedExeSymbols; procedure MergeStabs; + procedure MarkEmptySections; procedure RemoveUnreferencedSections; - procedure RemoveEmptySections; + procedure RemoveDisabledSections; procedure RemoveDebugInfo; procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);virtual; procedure GenerateDebugLink(const dbgname:string;dbgcrc:cardinal); @@ -1898,6 +1901,7 @@ implementation procedure TExeOutput.MemPos_Start; begin CurrMemPos:=0; + RemoveDisabledSections; end; @@ -2657,7 +2661,7 @@ implementation end; - procedure TExeOutput.RemoveEmptySections; + procedure TExeOutput.MarkEmptySections; var i, j : longint; exesec : TExeSection; @@ -2687,12 +2691,26 @@ implementation break; end; end; - if doremove and not (RelocSection and (exesec.Name='.reloc')) then + if doremove then begin - Comment(V_Debug,'Deleting empty section '+exesec.name); - ExeSectionList[i]:=nil; + Comment(V_Debug,'Disabling empty section '+exesec.name); + exesec.SecOptions:=exesec.SecOptions+[oso_disabled]; end; end; + end; + + + procedure TExeOutput.RemoveDisabledSections; + var + i: longint; + exesec: TExeSection; + begin + for i:=0 to ExeSectionList.Count-1 do + begin + exesec:=TExeSection(ExeSectionList[i]); + if (oso_disabled in exesec.SecOptions) then + ExeSectionList[i]:=nil; + end; ExeSectionList.Pack; end; diff --git a/compiler/ogcoff.pas b/compiler/ogcoff.pas index 4d2c92a288..90b9c977a4 100644 --- a/compiler/ogcoff.pas +++ b/compiler/ogcoff.pas @@ -226,7 +226,7 @@ interface public constructor create;override; procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override; - procedure Order_End;override; + procedure MemPos_Start;override; procedure MemPos_ExeSection(const aname:string);override; end; @@ -2656,7 +2656,7 @@ const pemagic : array[0..3] of byte = ( exesec:=FindExeSection('.reloc'); if exesec=nil then exit; - objsec:=internalObjData.createsection('.reloc',0,exesec.SecOptions+[oso_data]); + objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_load,oso_keep]); exesec.AddObjSection(objsec); pgaddr:=longword(-1); hdrpos:=longword(-1); @@ -2702,17 +2702,18 @@ const pemagic : array[0..3] of byte = ( end; - procedure TPECoffexeoutput.Order_End; + procedure TPECoffexeoutput.MemPos_Start; var exesec : TExeSection; begin + if RelocSection then + begin + exesec:=FindExeSection('.reloc'); + if exesec=nil then + InternalError(2012072401); + exesec.SecOptions:=exesec.SecOptions-[oso_disabled]; + end; inherited; - if not IsSharedLibrary then - exit; - exesec:=FindExeSection('.reloc'); - if exesec=nil then - exit; - exesec.SecOptions:=exesec.SecOptions + [oso_Data,oso_keep,oso_load]; end; diff --git a/compiler/ognlm.pas b/compiler/ognlm.pas index 42606b3af1..74c2b0f59c 100644 --- a/compiler/ognlm.pas +++ b/compiler/ognlm.pas @@ -281,7 +281,7 @@ const NLM_MAX_DESCRIPTION_LENGTH = 127; procedure DataPos_Header;override; procedure fillNlmVersionHeader; procedure GenerateLibraryImports(ImportLibraryList:TFPHashObjectList);override; - procedure Order_End;override; + procedure MemPos_Start;override; procedure MemPos_ExeSection(const aname:string);override; procedure DataPos_ExeSection(const aname:string);override; procedure NLMwriteString (const s : string; terminateWithZero : boolean); @@ -1172,7 +1172,7 @@ function SecOpts(SecOptions:TObjSectionOptions):string; exesec:=FindExeSection('.reloc'); if exesec=nil then exit; - objsec:=internalObjData.createsection('.reloc',0,exesec.SecOptions+[oso_data]); + objsec:=internalObjData.createsection('.reloc',0,[oso_data,oso_load,oso_keep]); exesec.AddObjSection(objsec); for i:=0 to ExeSectionList.Count-1 do begin @@ -1227,15 +1227,15 @@ function SecOpts(SecOptions:TObjSectionOptions):string; end; - procedure TNLMexeoutput.Order_End; + procedure TNLMexeoutput.MemPos_Start; var exesec : TExeSection; begin - inherited; exesec:=FindExeSection('.reloc'); if exesec=nil then - exit; - exesec.SecOptions:=exesec.SecOptions + [oso_Data,oso_keep,oso_load]; + InternalError(2012072602); + exesec.SecOptions:=exesec.SecOptions-[oso_disabled]; + inherited; end;