+ write debug sections in the wasm internal exe writer

This commit is contained in:
Nikolay Nikolov 2024-01-04 15:59:00 +02:00
parent f86a9bd5c0
commit 543b341203

View File

@ -257,13 +257,16 @@ interface
end; end;
FWasmSections: array [TWasmSectionID] of tdynamicarray; FWasmSections: array [TWasmSectionID] of tdynamicarray;
FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
FStackPointerSym: TWasmObjSymbol; FStackPointerSym: TWasmObjSymbol;
FMinMemoryPages: Integer; FMinMemoryPages: Integer;
procedure WriteWasmSection(wsid: TWasmSectionID); procedure WriteWasmSection(wsid: TWasmSectionID);
procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
procedure PrepareImports; procedure PrepareImports;
procedure PrepareFunctions; procedure PrepareFunctions;
function AddOrGetIndirectFunctionTableIndex(FuncIdx: Integer): integer; function AddOrGetIndirectFunctionTableIndex(FuncIdx: Integer): integer;
procedure SetStackPointer; procedure SetStackPointer;
procedure WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);
protected protected
function writeData:boolean;override; function writeData:boolean;override;
procedure DoRelocationFixup(objsec:TObjSection);override; procedure DoRelocationFixup(objsec:TObjSection);override;
@ -4297,6 +4300,16 @@ implementation
Writer.writearray(FWasmSections[wsid]); Writer.writearray(FWasmSections[wsid]);
end; end;
procedure TWasmExeOutput.WriteWasmCustomSection(wcst: TWasmCustomSectionType);
var
b: byte;
begin
b:=0;
Writer.write(b,1);
WriteUleb(Writer,FWasmCustomSections[wcst].size);
Writer.writearray(FWasmCustomSections[wcst]);
end;
function TWasmExeOutput.writeData: boolean; function TWasmExeOutput.writeData: boolean;
procedure WriteImportSection; procedure WriteImportSection;
@ -4525,9 +4538,27 @@ implementation
end; end;
end; end;
procedure MaybeWriteDebugSection(st: TWasmCustomDebugSectionType);
var
exesec: TExeSection;
begin
exesec:=FindExeSection(WasmCustomSectionName[st]);
if assigned(exesec) then
begin
WriteExeSectionToDynArray(exesec,FWasmCustomSections[st]);
WriteWasmCustomSection(st);
end;
end;
var
cust_sec: TWasmCustomSectionType;
begin begin
result:=false; result:=false;
{ each custom sections starts with its name }
for cust_sec in TWasmCustomSectionType do
WriteName(FWasmCustomSections[cust_sec],WasmCustomSectionName[cust_sec]);
SetStackPointer; SetStackPointer;
FFuncTypes.WriteTo(FWasmSections[wsiType]); FFuncTypes.WriteTo(FWasmSections[wsiType]);
@ -4558,6 +4589,14 @@ implementation
WriteWasmSection(wsiCode); WriteWasmSection(wsiCode);
WriteWasmSection(wsiData); WriteWasmSection(wsiData);
MaybeWriteDebugSection(wcstDebugAbbrev);
MaybeWriteDebugSection(wcstDebugInfo);
MaybeWriteDebugSection(wcstDebugStr);
MaybeWriteDebugSection(wcstDebugLine);
MaybeWriteDebugSection(wcstDebugFrame);
MaybeWriteDebugSection(wcstDebugAranges);
MaybeWriteDebugSection(wcstDebugRanges);
result := true; result := true;
end; end;
@ -4664,6 +4703,7 @@ implementation
constructor TWasmExeOutput.create; constructor TWasmExeOutput.create;
var var
i: TWasmSectionID; i: TWasmSectionID;
j: TWasmCustomSectionType;
begin begin
inherited create; inherited create;
CObjData:=TWasmObjData; CObjData:=TWasmObjData;
@ -4671,6 +4711,8 @@ implementation
FFuncTypes:=TWasmFuncTypeTable.Create; FFuncTypes:=TWasmFuncTypeTable.Create;
for i in TWasmSectionID do for i in TWasmSectionID do
FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow); FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
for j in TWasmCustomSectionType do
FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
SetLength(FIndirectFunctionTable,1); SetLength(FIndirectFunctionTable,1);
FIndirectFunctionTable[0].FuncIdx:=-1; FIndirectFunctionTable[0].FuncIdx:=-1;
end; end;
@ -4678,9 +4720,12 @@ implementation
destructor TWasmExeOutput.destroy; destructor TWasmExeOutput.destroy;
var var
i: TWasmSectionID; i: TWasmSectionID;
j: TWasmCustomSectionType;
begin begin
for i in TWasmSectionID do for i in TWasmSectionID do
FWasmSections[i].Free; FWasmSections[i].Free;
for j in TWasmCustomSectionType do
FWasmCustomSections[j].Free;
FFuncTypes.Free; FFuncTypes.Free;
inherited destroy; inherited destroy;
end; end;
@ -4902,6 +4947,37 @@ implementation
FStackPointerSym.LinkingData.GlobalInitializer.init_i32:=Int32(InitialStackPtrAddr); FStackPointerSym.LinkingData.GlobalInitializer.init_i32:=Int32(InitialStackPtrAddr);
end; end;
procedure TWasmExeOutput.WriteExeSectionToDynArray(exesec: TExeSection; dynarr: tdynamicarray);
var
exesecdatapos: LongWord;
i: Integer;
objsec: TObjSection;
dpos, pad: QWord;
begin
exesecdatapos:=dynarr.size;
for i:=0 to exesec.ObjSectionList.Count-1 do
begin
objsec:=TObjSection(exesec.ObjSectionList[i]);
if not (oso_data in objsec.secoptions) then
internalerror(2024010104);
if not assigned(objsec.data) then
internalerror(2024010105);
dpos:=objsec.MemPos-exesec.MemPos+exesecdatapos;
pad:=dpos-dynarr.size;
{ objsection must be within SecAlign bytes from the previous one }
if (dpos<dynarr.Size) or
(pad>=max(objsec.SecAlign,1)) then
internalerror(2024010106);
writeZeros(dynarr,pad);
objsec.data.seek(0);
CopyDynamicArray(objsec.data,dynarr,objsec.data.size);
end;
if (dynarr.size-exesecdatapos)<>exesec.Size then
internalerror(2024010107);
end;
{**************************************************************************** {****************************************************************************
TWasmAssembler TWasmAssembler