From d29473154267150c65be268cf070941573a58ffc Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Sat, 9 Apr 2022 14:48:38 +0200 Subject: [PATCH] LLVM: enable DWARF debug info generation --- compiler/dbgbase.pas | 7 +++++++ compiler/llvm/llvminfo.pas | 13 +++++++++++++ compiler/llvm/llvmnode.pas | 3 ++- compiler/llvm/nllvmutil.pas | 36 ++++++++++++++++++++++++++++++++++-- compiler/systems.inc | 2 +- compiler/systems.pas | 2 -- 6 files changed, 57 insertions(+), 6 deletions(-) diff --git a/compiler/dbgbase.pas b/compiler/dbgbase.pas index af894216f2..fe092dd96b 100644 --- a/compiler/dbgbase.pas +++ b/compiler/dbgbase.pas @@ -630,7 +630,14 @@ implementation Comment(V_Fatal,'cg_f_debuginfo_output_not_supported'); exit; end; +{$ifndef llvm} hp.DebugInfo:=CDebugInfo[target_dbg.id].Create; +{$else} + { we can't override the assignment of target_dbg with the LLVM class, + because we still need to know whether to tell LLVM to generate + DWARFv2/3/4/5/... } + hp.DebugInfo:=CDebugInfo[dbg_llvm].Create; +{$endif} if restore_current_debuginfo then begin if current_debuginfo=nil then diff --git a/compiler/llvm/llvminfo.pas b/compiler/llvm/llvminfo.pas index e8e9b9f258..363f23bf00 100644 --- a/compiler/llvm/llvminfo.pas +++ b/compiler/llvm/llvminfo.pas @@ -73,6 +73,19 @@ Const '11.0' ); + llvm_debuginfo_metadata_format : array[tllvmversion] of byte = ( + 0, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3 + ); + llvmversion_properties: array[tllvmversion] of tllvmversionflags = ( { invalid } [], diff --git a/compiler/llvm/llvmnode.pas b/compiler/llvm/llvmnode.pas index 729225c9e0..5617e89981 100644 --- a/compiler/llvm/llvmnode.pas +++ b/compiler/llvm/llvmnode.pas @@ -41,6 +41,7 @@ implementation nllvmmat,nllvmmem,nllvmset,nllvmtcon,nllvmutil, llvmpara,llvmpi, symllvm, - llvmcfi; + llvmcfi, + dbgllvm; end. diff --git a/compiler/llvm/nllvmutil.pas b/compiler/llvm/nllvmutil.pas index 6ce426fb65..88f889d70e 100644 --- a/compiler/llvm/nllvmutil.pas +++ b/compiler/llvm/nllvmutil.pas @@ -53,7 +53,7 @@ implementation aasmtai,cpubase,llvmbase,aasmllvm, aasmcnst,nllvmtcon, symbase,symtable,defutil, - llvmtype,llvmdef, + llvminfo,llvmtype,llvmdef, objcasm; class procedure tllvmnodeutils.insertbsssym(list: tasmlist; sym: tstaticvarsym; size: asizeint; varalign: shortint; _typ:Tasmsymtype); @@ -257,7 +257,8 @@ implementation class procedure tllvmnodeutils.GenerateObjCImageInfo; var llvmmoduleflags, - objcmoduleflag: tai_llvmbasemetadatanode; + objcmoduleflag, + dwarfversionflag: tai_llvmbasemetadatanode; objcabiversion: longint; begin llvmmoduleflags:=tai_llvmnamedmetadatanode.create('llvm.module.flags'); @@ -299,6 +300,37 @@ implementation objcmoduleflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(0))); llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(objcmoduleflag)); current_asmdata.AsmLists[al_rotypedconsts].Concat(objcmoduleflag); + + { debug information } + if (([cs_debuginfo,cs_lineinfo]*current_settings.moduleswitches)<>[]) and + (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4]) then + begin + { the debug info version is the version of the debug info metadata + format } + dwarfversionflag:=tai_llvmunnamedmetadatanode.create; + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2))); + dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Debug Info Version'))); + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(llvm_debuginfo_metadata_format[current_settings.llvmversion]))); + llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag)); + current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag); + + { dwarf version } + dwarfversionflag:=tai_llvmunnamedmetadatanode.create; + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2))); + dwarfversionflag.addvalue(tai_simpletypedconst.create(charpointertype,tai_string.Create('Dwarf Version'))); + case target_dbg.id of + dbg_dwarf2: + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(2))); + dbg_dwarf3: + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(3))); + dbg_dwarf4: + dwarfversionflag.addvalue(tai_simpletypedconst.create(s32inttype,tai_const.Create_32bit(4))); + else + internalerror(2022022012); + end; + llvmmoduleflags.addvalue(llvm_getmetadatareftypedconst(dwarfversionflag)); + current_asmdata.AsmLists[al_rotypedconsts].Concat(dwarfversionflag); + end; end; diff --git a/compiler/systems.inc b/compiler/systems.inc index 5f2bf2f809..96914d4a2b 100644 --- a/compiler/systems.inc +++ b/compiler/systems.inc @@ -347,7 +347,7 @@ tdbg = (dbg_none ,dbg_stabs,dbg_stabx,dbg_dwarf2,dbg_dwarf3,dbg_dwarf4,dbg_jasmin - ,dbg_codeview + ,dbg_codeview,dbg_llvm ); tscripttype = (script_none diff --git a/compiler/systems.pas b/compiler/systems.pas index 7d9436fbe1..f11b9bfb10 100644 --- a/compiler/systems.pas +++ b/compiler/systems.pas @@ -685,14 +685,12 @@ function set_target_dbg(t:tdbg):boolean; begin result:=false; { no debugging support for llvm yet } -{$ifndef llvm} if assigned(dbginfos[t]) then begin target_dbg:=dbginfos[t]^; result:=true; exit; end; -{$endif} end;