+ WebAssembly internal linker: create a name custom section to help debugging

This commit is contained in:
Nikolay Nikolov 2024-09-17 05:23:38 +03:00
parent 9d7ea2c069
commit cf7d9fa4c5
2 changed files with 68 additions and 1 deletions

View File

@ -279,6 +279,7 @@ interface
FWasmSections: array [TWasmSectionID] of tdynamicarray;
FWasmCustomSections: array [TWasmCustomSectionType] of tdynamicarray;
FWasmNameSubsections: array [TWasmNameSubsectionType] of tdynamicarray;
FStackPointerSym: TWasmObjSymbol;
FTlsBaseSym: TWasmObjSymbol;
FTlsSizeSym: TWasmObjSymbol;
@ -287,6 +288,12 @@ interface
FInitSharedMemoryFunctionSym: TWasmObjSymbol;
FMinMemoryPages,
FMaxMemoryPages: Integer;
{ use for the Name section }
FFunctionNameMap: array of record
idx: UInt32;
name: string;
end;
procedure AddToFunctionNameMap(aidx: UInt32; const aname: string);
procedure WriteWasmSection(wsid: TWasmSectionID);
procedure WriteWasmSectionIfNotEmpty(wsid: TWasmSectionID);
procedure WriteWasmCustomSection(wcst: TWasmCustomSectionType);
@ -321,7 +328,7 @@ interface
implementation
uses
cutils,verbose,version,globals,ogmap;
cutils,verbose,version,globals,fmodule,ogmap;
const
StackPointerSymStr='__stack_pointer';
@ -4764,6 +4771,16 @@ implementation
TWasmExeOutput
****************************************************************************}
procedure TWasmExeOutput.AddToFunctionNameMap(aidx: UInt32; const aname: string);
begin
SetLength(FFunctionNameMap,Length(FFunctionNameMap)+1);
with FFunctionNameMap[High(FFunctionNameMap)] do
begin
idx:=aidx;
name:=aname;
end;
end;
procedure TWasmExeOutput.WriteWasmSection(wsid: TWasmSectionID);
var
b: byte;
@ -5084,6 +5101,35 @@ implementation
end;
end;
procedure WriteNameSubsection(wnst: TWasmNameSubsectionType);
begin
if FWasmNameSubsections[wnst].size>0 then
begin
WriteByte(FWasmCustomSections[wcstName],Ord(wnst));
WriteUleb(FWasmCustomSections[wcstName],FWasmNameSubsections[wnst].size);
FWasmNameSubsections[wnst].seek(0);
CopyDynamicArray(FWasmNameSubsections[wnst],FWasmCustomSections[wcstName],FWasmNameSubsections[wnst].size);
end;
end;
procedure WriteNameSection;
var
i: Integer;
begin
WriteName(FWasmNameSubsections[wnstModuleName],current_module.exefilename);
WriteUleb(FWasmNameSubsections[wnstFunctionNames],Length(FFunctionNameMap));
for i:=low(FFunctionNameMap) to high(FFunctionNameMap) do
with FFunctionNameMap[i] do
begin
WriteUleb(FWasmNameSubsections[wnstFunctionNames],idx);
WriteName(FWasmNameSubsections[wnstFunctionNames],name);
end;
WriteNameSubsection(wnstModuleName);
WriteNameSubsection(wnstFunctionNames);
end;
var
cust_sec: TWasmCustomSectionType;
begin
@ -5128,6 +5174,8 @@ implementation
if ts_wasm_threads in current_settings.targetswitches then
WriteUleb(FWasmSections[wsiStart],FInitSharedMemoryFunctionSym.LinkingData.ExeFunctionIndex);
WriteNameSection;
Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
Writer.write(WasmVersion,SizeOf(WasmVersion));
WriteWasmSection(wsiType);
@ -5154,6 +5202,8 @@ implementation
MaybeWriteDebugSection(wcstDebugAranges);
MaybeWriteDebugSection(wcstDebugRanges);
WriteWasmCustomSection(wcstName);
result := true;
end;
@ -5309,6 +5359,7 @@ implementation
var
i: TWasmSectionID;
j: TWasmCustomSectionType;
k: TWasmNameSubsectionType;
begin
inherited create;
CObjData:=TWasmObjData;
@ -5319,6 +5370,8 @@ implementation
FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
for j in TWasmCustomSectionType do
FWasmCustomSections[j] := tdynamicarray.create(SectionDataMaxGrow);
for k:=low(FWasmNameSubsections) to high(FWasmNameSubsections) do
FWasmNameSubsections[k] := tdynamicarray.create(SectionDataMaxGrow);
SetLength(FIndirectFunctionTable,1);
FIndirectFunctionTable[0].FuncIdx:=-1;
end;
@ -5327,11 +5380,14 @@ implementation
var
i: TWasmSectionID;
j: TWasmCustomSectionType;
k: TWasmNameSubsectionType;
begin
for i in TWasmSectionID do
FWasmSections[i].Free;
for j in TWasmCustomSectionType do
FWasmCustomSections[j].Free;
for k:=low(FWasmNameSubsections) to high(FWasmNameSubsections) do
FWasmNameSubsections[k].Free;
FFuncTypes.Free;
inherited destroy;
end;
@ -5537,6 +5593,7 @@ implementation
newdll:=True;
TWasmObjSymbol(exesym.ObjSymbol).LinkingData.ExeFunctionIndex:=
AddFunctionImport(ImportLibrary.Name,ImportSymbol.Name,TWasmObjSymbol(exesym.ObjSymbol).LinkingData.FuncType);
AddToFunctionNameMap(TWasmObjSymbol(exesym.ObjSymbol).LinkingData.ExeFunctionIndex,ImportSymbol.MangledName);
end;
end;
end;
@ -5588,6 +5645,7 @@ implementation
begin
exemap.Add(' Function[' + tostr(fsym.LinkingData.ExeFunctionIndex) + '] ' + fsym.Name + fsym.LinkingData.FuncType.ToString);
end;
AddToFunctionNameMap(fsym.LinkingData.ExeFunctionIndex,fsym.Name);
end;
{ set ExeFunctionIndex to the alias symbols as well }
for i:=0 to ObjDataList.Count-1 do

View File

@ -50,6 +50,7 @@ type
wcstLinking,
wcstRelocCode,
wcstRelocData,
wcstName,
wcstProducers,
wcstTargetFeatures,
@ -71,11 +72,19 @@ type
TWasmCustomDebugSectionType = wcstDebugFrame..wcstDebugStr;
TWasmNameSubsectionType = (
wnstModuleName = 0,
wnstFunctionNames = 1,
wnstLocalNames = 2
);
const
WasmCustomSectionName: array [TWasmCustomSectionType] of string =
('linking',
'reloc.CODE',
'reloc.DATA',
'name',
'producers',
'target_features',