mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-17 00:39:15 +02:00
* Moved 4 procedures for basic reading/writing TObjSection contents into ogcoff.pas, so they don't have to be reimplemented for every output format.
git-svn-id: trunk@21492 -
This commit is contained in:
parent
efe5362670
commit
7d3294b504
@ -273,6 +273,7 @@ interface
|
||||
procedure afteralloc;virtual;
|
||||
procedure afterwrite;virtual;
|
||||
procedure resetsections;
|
||||
procedure layoutsections(var datapos:aword);
|
||||
property Name:TString80 read FName;
|
||||
property CurrObjSec:TObjSection read FCurrObjSec;
|
||||
property ObjSymbolList:TFPHashObjectList read FObjSymbolList;
|
||||
@ -290,6 +291,7 @@ interface
|
||||
FWriter : TObjectwriter;
|
||||
function writeData(Data:TObjData):boolean;virtual;abstract;
|
||||
property CObjData : TObjDataClass read FCObjData write FCObjData;
|
||||
procedure WriteSectionContent(Data:TObjData);
|
||||
public
|
||||
constructor create(AWriter:TObjectWriter);virtual;
|
||||
destructor destroy;override;
|
||||
@ -309,6 +311,7 @@ interface
|
||||
FReader : TObjectReader;
|
||||
InputFileName : string;
|
||||
property CObjData : TObjDataClass read FCObjData write FCObjData;
|
||||
procedure ReadSectionContent(Data:TObjData);
|
||||
public
|
||||
constructor create;virtual;
|
||||
destructor destroy;override;
|
||||
@ -440,6 +443,7 @@ interface
|
||||
property CExeSection:TExeSectionClass read FCExeSection write FCExeSection;
|
||||
property CObjData:TObjDataClass read FCObjData write FCObjData;
|
||||
procedure Order_ObjSectionList(ObjSectionList : TFPObjectList; const aPattern:string);virtual;
|
||||
procedure WriteExeSectionContent;
|
||||
public
|
||||
CurrDataPos : aword;
|
||||
MaxMemPos : qword;
|
||||
@ -1248,6 +1252,15 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
procedure TObjData.layoutsections(var DataPos:aword);
|
||||
var
|
||||
i: longint;
|
||||
begin
|
||||
for i:=0 to FObjSectionList.Count-1 do
|
||||
TObjSection(FObjSectionList[i]).setDatapos(DataPos);
|
||||
end;
|
||||
|
||||
|
||||
{****************************************************************************
|
||||
TObjOutput
|
||||
****************************************************************************}
|
||||
@ -1304,6 +1317,25 @@ implementation
|
||||
FWriter.writesym(p.name);
|
||||
end;
|
||||
|
||||
procedure TObjOutput.WriteSectionContent(Data:TObjData);
|
||||
var
|
||||
i:longint;
|
||||
sec:TObjSection;
|
||||
begin
|
||||
for i:=0 to Data.ObjSectionList.Count-1 do
|
||||
begin
|
||||
sec:=TObjSection(Data.ObjSectionList[i]);
|
||||
if (oso_data in sec.SecOptions) then
|
||||
begin
|
||||
if sec.Data=nil then
|
||||
internalerror(200403073);
|
||||
FWriter.writezeros(sec.dataalignbytes);
|
||||
if sec.Datapos<>FWriter.ObjSize then
|
||||
internalerror(200604031);
|
||||
FWriter.writearray(sec.data);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{****************************************************************************
|
||||
TExeVTable
|
||||
@ -2823,6 +2855,45 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
procedure TExeOutput.WriteExeSectionContent;
|
||||
var
|
||||
exesec : TExeSection;
|
||||
objsec : TObjSection;
|
||||
i,j : longint;
|
||||
begin
|
||||
for j:=0 to ExeSectionList.Count-1 do
|
||||
begin
|
||||
exesec:=TExeSection(ExeSectionList[j]);
|
||||
{ don't write normal section if writing only debug info }
|
||||
if (ExeWriteMode=ewm_dbgonly) and
|
||||
not(oso_debug in exesec.SecOptions) then
|
||||
exit;
|
||||
|
||||
if oso_data in exesec.SecOptions then
|
||||
begin
|
||||
FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
|
||||
for i:=0 to exesec.ObjSectionList.Count-1 do
|
||||
begin
|
||||
objsec:=TObjSection(exesec.ObjSectionList[i]);
|
||||
if oso_data in objsec.secoptions then
|
||||
begin
|
||||
if not assigned(objsec.data) then
|
||||
internalerror(200603042);
|
||||
FWriter.writezeros(objsec.dataalignbytes);
|
||||
if objsec.DataPos<>FWriter.Size then
|
||||
begin
|
||||
writeln(objsec.name,' ',hexstr(objsec.datapos,8),' should be: ',hexstr(fwriter.size,8));
|
||||
internalerror(200602251);
|
||||
|
||||
end;
|
||||
FWriter.writearray(objsec.data);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{****************************************************************************
|
||||
TObjInput
|
||||
****************************************************************************}
|
||||
@ -2850,6 +2921,33 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
procedure TObjInput.ReadSectionContent(Data:TObjData);
|
||||
var
|
||||
i: longint;
|
||||
sec: TObjSection;
|
||||
begin
|
||||
for i:=0 to Data.ObjSectionList.Count-1 do
|
||||
begin
|
||||
sec:=TObjSection(Data.ObjSectionList[i]);
|
||||
{ Skip debug sections }
|
||||
if (oso_debug in sec.SecOptions) and
|
||||
(cs_link_strip in current_settings.globalswitches) and
|
||||
not(cs_link_separate_dbg_file in current_settings.globalswitches) then
|
||||
continue;
|
||||
|
||||
if assigned(sec.Data) then
|
||||
begin
|
||||
FReader.Seek(sec.datapos);
|
||||
if not FReader.ReadArray(sec.data,sec.Size) then
|
||||
begin
|
||||
InputError('Can''t read object data');
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{$ifdef MEMDEBUG}
|
||||
initialization
|
||||
memobjsymbols:=TMemDebug.create('ObjSymbols');
|
||||
|
@ -108,7 +108,6 @@ interface
|
||||
coffrelocpos : aword;
|
||||
public
|
||||
secidx : longword;
|
||||
flags : longword;
|
||||
constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
|
||||
procedure addsymsizereloc(ofs:aword;p:TObjSymbol;symsize:aword;reloctype:TObjRelocationType);
|
||||
procedure fixuprelocs;override;
|
||||
@ -154,10 +153,8 @@ interface
|
||||
procedure section_write_symbol(p:TObject;arg:pointer);
|
||||
procedure section_write_relocs(p:TObject;arg:pointer);
|
||||
procedure create_symbols(data:TObjData);
|
||||
procedure section_set_datapos(p:TObject;arg:pointer);
|
||||
procedure section_set_reloc_datapos(p:TObject;arg:pointer);
|
||||
procedure section_write_header(p:TObject;arg:pointer);
|
||||
procedure section_write_data(p:TObject;arg:pointer);
|
||||
protected
|
||||
function writedata(data:TObjData):boolean;override;
|
||||
public
|
||||
@ -187,7 +184,6 @@ interface
|
||||
function Read_str(strpos:longword):string;
|
||||
procedure read_relocs(s:TCoffObjSection);
|
||||
procedure read_symbols(objdata:TObjData);
|
||||
procedure ObjSections_read_data(p:TObject;arg:pointer);
|
||||
procedure ObjSections_read_relocs(p:TObject;arg:pointer);
|
||||
public
|
||||
constructor createcoff(awin32:boolean);
|
||||
@ -231,7 +227,6 @@ interface
|
||||
procedure write_symbol(const name:string;value:aword;section:smallint;typ,aux:byte);
|
||||
procedure globalsyms_write_symbol(p:TObject;arg:pointer);
|
||||
procedure ExeSectionList_write_header(p:TObject;arg:pointer);
|
||||
procedure ExeSectionList_write_data(p:TObject;arg:pointer);
|
||||
protected
|
||||
function writedata:boolean;override;
|
||||
procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
|
||||
@ -1418,12 +1413,6 @@ const pemagic : array[0..3] of byte = (
|
||||
end;
|
||||
|
||||
|
||||
procedure TCoffObjOutput.section_set_datapos(p:TObject;arg:pointer);
|
||||
begin
|
||||
TObjSection(p).setdatapos(paword(arg)^);
|
||||
end;
|
||||
|
||||
|
||||
procedure TCoffObjOutput.section_set_reloc_datapos(p:TObject;arg:pointer);
|
||||
begin
|
||||
TCoffObjSection(p).coffrelocpos:=paint(arg)^;
|
||||
@ -1474,21 +1463,6 @@ const pemagic : array[0..3] of byte = (
|
||||
end;
|
||||
|
||||
|
||||
procedure TCoffObjOutput.section_write_data(p:TObject;arg:pointer);
|
||||
begin
|
||||
with TObjSection(p) do
|
||||
begin
|
||||
if assigned(data) then
|
||||
begin
|
||||
FWriter.writezeros(dataalignbytes);
|
||||
if Datapos<>FWriter.ObjSize then
|
||||
internalerror(200603052);
|
||||
FWriter.writearray(data);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
function TCoffObjOutput.writedata(data:TObjData):boolean;
|
||||
var
|
||||
orgdatapos,
|
||||
@ -1509,7 +1483,7 @@ const pemagic : array[0..3] of byte = (
|
||||
{ Calculate the filepositions }
|
||||
datapos:=sizeof(tcoffheader)+sizeof(tcoffsechdr)*ObjSectionList.Count;
|
||||
{ Sections first }
|
||||
ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
|
||||
layoutsections(datapos);
|
||||
{ relocs }
|
||||
orgdatapos:=datapos;
|
||||
ObjSectionList.ForEachCall(@section_set_reloc_datapos,@datapos);
|
||||
@ -1545,7 +1519,7 @@ const pemagic : array[0..3] of byte = (
|
||||
{ Section headers }
|
||||
ObjSectionList.ForEachCall(@section_write_header,nil);
|
||||
{ ObjSections }
|
||||
ObjSectionList.ForEachCall(@section_write_data,nil);
|
||||
WriteSectionContent(data);
|
||||
{ Relocs }
|
||||
ObjSectionList.ForEachCall(@section_write_relocs,nil);
|
||||
{ ObjSymbols }
|
||||
@ -1826,29 +1800,6 @@ const pemagic : array[0..3] of byte = (
|
||||
end;
|
||||
|
||||
|
||||
procedure TCoffObjInput.ObjSections_read_data(p:TObject;arg:pointer);
|
||||
begin
|
||||
with TCoffObjSection(p) do
|
||||
begin
|
||||
{ Skip debug sections }
|
||||
if (oso_debug in secoptions) and
|
||||
(cs_link_strip in current_settings.globalswitches) and
|
||||
not(cs_link_separate_dbg_file in current_settings.globalswitches) then
|
||||
exit;
|
||||
|
||||
if assigned(data) then
|
||||
begin
|
||||
FReader.Seek(datapos);
|
||||
if not FReader.ReadArray(data,Size) then
|
||||
begin
|
||||
Comment(V_Error,'Error reading coff file, can''t read object data');
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure TCoffObjInput.ObjSections_read_relocs(p:TObject;arg:pointer);
|
||||
begin
|
||||
with TCoffObjSection(p) do
|
||||
@ -1983,7 +1934,7 @@ const pemagic : array[0..3] of byte = (
|
||||
{ Insert all ObjSymbols }
|
||||
read_symbols(objdata);
|
||||
{ Section Data }
|
||||
ObjSectionList.ForEachCall(@objsections_read_data,nil);
|
||||
ReadSectionContent(objdata);
|
||||
{ Relocs }
|
||||
ObjSectionList.ForEachCall(@objsections_read_relocs,nil);
|
||||
end;
|
||||
@ -2183,39 +2134,6 @@ const pemagic : array[0..3] of byte = (
|
||||
end;
|
||||
|
||||
|
||||
procedure Tcoffexeoutput.ExeSectionList_write_Data(p:TObject;arg:pointer);
|
||||
var
|
||||
objsec : TObjSection;
|
||||
i : longint;
|
||||
begin
|
||||
with texesection(p) do
|
||||
begin
|
||||
{ don't write normal section if writing only debug info }
|
||||
if (ExeWriteMode=ewm_dbgonly) and
|
||||
not(oso_debug in SecOptions) then
|
||||
exit;
|
||||
|
||||
if oso_data in secoptions then
|
||||
begin
|
||||
FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
|
||||
for i:=0 to ObjSectionList.Count-1 do
|
||||
begin
|
||||
objsec:=TObjSection(ObjSectionList[i]);
|
||||
if oso_data in objsec.secoptions then
|
||||
begin
|
||||
if not assigned(objsec.data) then
|
||||
internalerror(200603042);
|
||||
FWriter.writezeros(objsec.dataalignbytes);
|
||||
if objsec.DataPos<>FWriter.Size then
|
||||
internalerror(200602251);
|
||||
FWriter.writearray(objsec.data);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
function tcoffexeoutput.totalheadersize:longword;
|
||||
var
|
||||
stubsize,
|
||||
@ -2521,7 +2439,7 @@ const pemagic : array[0..3] of byte = (
|
||||
{ Section headers }
|
||||
ExeSectionList.ForEachCall(@ExeSectionList_write_header,nil);
|
||||
{ Section data }
|
||||
ExeSectionList.ForEachCall(@ExeSectionList_write_data,nil);
|
||||
WriteExeSectionContent;
|
||||
{ Align after the last section }
|
||||
FWriter.Writezeros(Align(FWriter.Size,SectionDataAlign)-FWriter.Size);
|
||||
|
||||
|
@ -74,14 +74,11 @@ interface
|
||||
procedure createshstrtab(data:TObjData);
|
||||
procedure createsymtab(data: TObjData);
|
||||
procedure writesectionheader(s:TElfObjSection);
|
||||
procedure writesectiondata(s:TElfObjSection);
|
||||
procedure write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
|
||||
procedure section_write_symbol(p:TObject;arg:pointer);
|
||||
procedure section_write_sh_string(p:TObject;arg:pointer);
|
||||
procedure section_count_sections(p:TObject;arg:pointer);
|
||||
procedure section_create_relocsec(p:TObject;arg:pointer);
|
||||
procedure section_set_datapos(p:TObject;arg:pointer);
|
||||
procedure section_write_data(p:TObject;arg:pointer);
|
||||
procedure section_write_sechdr(p:TObject;arg:pointer);
|
||||
protected
|
||||
function writedata(data:TObjData):boolean;override;
|
||||
@ -1200,15 +1197,6 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
|
||||
begin
|
||||
FWriter.writezeros(s.dataalignbytes);
|
||||
if s.Datapos<>FWriter.ObjSize then
|
||||
internalerror(200604031);
|
||||
FWriter.writearray(s.data);
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
|
||||
begin
|
||||
TElfObjSection(p).secshidx:=pword(arg)^;
|
||||
@ -1223,23 +1211,6 @@ implementation
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
|
||||
begin
|
||||
TObjSection(p).setdatapos(paword(arg)^);
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
|
||||
begin
|
||||
if (oso_data in TObjSection(p).secoptions) then
|
||||
begin
|
||||
if TObjSection(p).data=nil then
|
||||
internalerror(200403073);
|
||||
writesectiondata(TElfObjSection(p));
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
|
||||
begin
|
||||
writesectionheader(TElfObjSection(p));
|
||||
@ -1250,7 +1221,7 @@ implementation
|
||||
var
|
||||
header : telfheader;
|
||||
shoffset,
|
||||
datapos : aint;
|
||||
datapos : aword;
|
||||
nsections : word;
|
||||
begin
|
||||
result:=false;
|
||||
@ -1284,7 +1255,7 @@ implementation
|
||||
{ Calculate the filepositions }
|
||||
datapos:=$40; { elfheader + alignment }
|
||||
{ section data }
|
||||
ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
|
||||
layoutsections(datapos);
|
||||
{ section headers }
|
||||
shoffset:=datapos;
|
||||
inc(datapos,(nsections+1)*sizeof(telfsechdr));
|
||||
@ -1319,7 +1290,7 @@ implementation
|
||||
writer.write(header,sizeof(header));
|
||||
writer.writezeros($40-sizeof(header)); { align }
|
||||
{ Sections }
|
||||
ObjSectionList.ForEachCall(@section_write_data,nil);
|
||||
WriteSectionContent(data);
|
||||
{ section headers, start with an empty header for sh_undef }
|
||||
writer.writezeros(sizeof(telfsechdr));
|
||||
ObjSectionList.ForEachCall(@section_write_sechdr,nil);
|
||||
|
Loading…
Reference in New Issue
Block a user