@@ -1288,18 +1288,29 @@ void DwarfDebug::finalizeModuleInfo() {
1288
1288
}
1289
1289
1290
1290
auto *CUNode = cast<DICompileUnit>(P.first );
1291
- // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
1291
+ // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
1292
+ // attribute.
1292
1293
if (CUNode->getMacros ()) {
1293
- if (useSplitDwarf ())
1294
- TheCU.addSectionDelta (TheCU.getUnitDie (), dwarf::DW_AT_macro_info,
1294
+ if (getDwarfVersion () >= 5 ) {
1295
+ // FIXME: Add support for DWARFv5 DW_AT_macros attribute for split
1296
+ // case.
1297
+ if (!useSplitDwarf ())
1298
+ U.addSectionLabel (U.getUnitDie (), dwarf::DW_AT_macros,
1295
1299
U.getMacroLabelBegin (),
1296
- TLOF.getDwarfMacinfoDWOSection ()->getBeginSymbol ());
1297
- else
1298
- U.addSectionLabel (U.getUnitDie (), dwarf::DW_AT_macro_info,
1299
- U.getMacroLabelBegin (),
1300
- TLOF.getDwarfMacinfoSection ()->getBeginSymbol ());
1300
+ TLOF.getDwarfMacroSection ()->getBeginSymbol ());
1301
+ } else {
1302
+ if (useSplitDwarf ())
1303
+ TheCU.addSectionDelta (
1304
+ TheCU.getUnitDie (), dwarf::DW_AT_macro_info,
1305
+ U.getMacroLabelBegin (),
1306
+ TLOF.getDwarfMacinfoDWOSection ()->getBeginSymbol ());
1307
+ else
1308
+ U.addSectionLabel (U.getUnitDie (), dwarf::DW_AT_macro_info,
1309
+ U.getMacroLabelBegin (),
1310
+ TLOF.getDwarfMacinfoSection ()->getBeginSymbol ());
1311
+ }
1312
+ }
1301
1313
}
1302
- }
1303
1314
1304
1315
// Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
1305
1316
for (auto *CUNode : MMI->getModule ()->debug_compile_units ())
@@ -1355,7 +1366,7 @@ void DwarfDebug::endModule() {
1355
1366
// Emit info into a debug macinfo.dwo section.
1356
1367
emitDebugMacinfoDWO ();
1357
1368
else
1358
- // Emit info into a debug macinfo section.
1369
+ // Emit info into a debug macinfo/macro section.
1359
1370
emitDebugMacinfo ();
1360
1371
1361
1372
emitDebugStr ();
@@ -2854,6 +2865,27 @@ void DwarfDebug::emitDebugRangesDWO() {
2854
2865
Asm->getObjFileLowering ().getDwarfRnglistsDWOSection ());
2855
2866
}
2856
2867
2868
+ // / Emit the header of a DWARF 5 macro section.
2869
+ static void emitMacroHeader (AsmPrinter *Asm, const DwarfDebug &DD,
2870
+ const DwarfCompileUnit &CU) {
2871
+ enum HeaderFlagMask {
2872
+ #define HANDLE_MACRO_FLAG (ID, NAME ) MACRO_FLAG_##NAME = ID,
2873
+ #include " llvm/BinaryFormat/Dwarf.def"
2874
+ };
2875
+ uint8_t Flags = 0 ;
2876
+ Asm->OutStreamer ->AddComment (" Macro information version" );
2877
+ Asm->emitInt16 (5 );
2878
+ // We are setting Offset and line offset flags unconditionally here,
2879
+ // since we're only supporting DWARF32 and line offset should be mostly
2880
+ // present.
2881
+ // FIXME: Add support for DWARF64.
2882
+ Flags |= MACRO_FLAG_DEBUG_LINE_OFFSET;
2883
+ Asm->OutStreamer ->AddComment (" Flags: 32 bit, debug_line_offset present" );
2884
+ Asm->emitInt8 (Flags);
2885
+ Asm->OutStreamer ->AddComment (" debug_line_offset" );
2886
+ Asm->OutStreamer ->emitSymbolValue (CU.getLineTableStartSym (), /* Size=*/ 4 );
2887
+ }
2888
+
2857
2889
void DwarfDebug::handleMacroNodes (DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
2858
2890
for (auto *MN : Nodes) {
2859
2891
if (auto *M = dyn_cast<DIMacro>(MN))
@@ -2866,26 +2898,75 @@ void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
2866
2898
}
2867
2899
2868
2900
void DwarfDebug::emitMacro (DIMacro &M) {
2869
- Asm->emitULEB128 (M.getMacinfoType ());
2870
- Asm->emitULEB128 (M.getLine ());
2871
2901
StringRef Name = M.getName ();
2872
2902
StringRef Value = M.getValue ();
2873
- Asm->OutStreamer ->emitBytes (Name);
2874
- if (!Value.empty ()) {
2875
- // There should be one space between macro name and macro value.
2876
- Asm->emitInt8 (' ' );
2877
- Asm->OutStreamer ->emitBytes (Value);
2903
+ bool UseMacro = getDwarfVersion () >= 5 ;
2904
+
2905
+ if (UseMacro) {
2906
+ unsigned Type = M.getMacinfoType () == dwarf::DW_MACINFO_define
2907
+ ? dwarf::DW_MACRO_define_strp
2908
+ : dwarf::DW_MACRO_undef_strp;
2909
+ Asm->OutStreamer ->AddComment (dwarf::MacroString (Type));
2910
+ Asm->emitULEB128 (Type);
2911
+ Asm->OutStreamer ->AddComment (" Line Number" );
2912
+ Asm->emitULEB128 (M.getLine ());
2913
+ Asm->OutStreamer ->AddComment (" Macro String" );
2914
+ if (!Value.empty ())
2915
+ Asm->OutStreamer ->emitSymbolValue (
2916
+ this ->InfoHolder .getStringPool ()
2917
+ .getEntry (*Asm, (Name + " " + Value).str ())
2918
+ .getSymbol (),
2919
+ 4 );
2920
+ else
2921
+ // DW_MACRO_undef_strp doesn't have a value, so just emit the macro
2922
+ // string.
2923
+ Asm->OutStreamer ->emitSymbolValue (this ->InfoHolder .getStringPool ()
2924
+ .getEntry (*Asm, (Name).str ())
2925
+ .getSymbol (),
2926
+ 4 );
2927
+ } else {
2928
+ Asm->OutStreamer ->AddComment (dwarf::MacinfoString (M.getMacinfoType ()));
2929
+ Asm->emitULEB128 (M.getMacinfoType ());
2930
+ Asm->OutStreamer ->AddComment (" Line Number" );
2931
+ Asm->emitULEB128 (M.getLine ());
2932
+ Asm->OutStreamer ->AddComment (" Macro String" );
2933
+ Asm->OutStreamer ->emitBytes (Name);
2934
+ if (!Value.empty ()) {
2935
+ // There should be one space between macro name and macro value.
2936
+ Asm->emitInt8 (' ' );
2937
+ Asm->OutStreamer ->AddComment (" Macro Value=" );
2938
+ Asm->OutStreamer ->emitBytes (Value);
2939
+ }
2940
+ Asm->emitInt8 (' \0 ' );
2878
2941
}
2879
- Asm->emitInt8 (' \0 ' );
2880
2942
}
2881
2943
2882
- void DwarfDebug::emitMacroFile (DIMacroFile &F, DwarfCompileUnit &U) {
2883
- assert (F.getMacinfoType () == dwarf::DW_MACINFO_start_file);
2884
- Asm->emitULEB128 (dwarf::DW_MACINFO_start_file);
2944
+ void DwarfDebug::emitMacroFileImpl (
2945
+ DIMacroFile &F, DwarfCompileUnit &U, unsigned StartFile, unsigned EndFile,
2946
+ StringRef (*MacroFormToString)(unsigned Form)) {
2947
+
2948
+ Asm->OutStreamer ->AddComment (MacroFormToString (StartFile));
2949
+ Asm->emitULEB128 (StartFile);
2950
+ Asm->OutStreamer ->AddComment (" Line Number" );
2885
2951
Asm->emitULEB128 (F.getLine ());
2952
+ Asm->OutStreamer ->AddComment (" File Number" );
2886
2953
Asm->emitULEB128 (U.getOrCreateSourceID (F.getFile ()));
2887
2954
handleMacroNodes (F.getElements (), U);
2888
- Asm->emitULEB128 (dwarf::DW_MACINFO_end_file);
2955
+ Asm->OutStreamer ->AddComment (MacroFormToString (EndFile));
2956
+ Asm->emitULEB128 (EndFile);
2957
+ }
2958
+
2959
+ void DwarfDebug::emitMacroFile (DIMacroFile &F, DwarfCompileUnit &U) {
2960
+ // DWARFv5 macro and DWARFv4 macinfo share some common encodings,
2961
+ // so for readibility/uniformity, We are explicitly emitting those.
2962
+ assert (F.getMacinfoType () == dwarf::DW_MACINFO_start_file);
2963
+ bool UseMacro = getDwarfVersion () >= 5 ;
2964
+ if (UseMacro)
2965
+ emitMacroFileImpl (F, U, dwarf::DW_MACRO_start_file,
2966
+ dwarf::DW_MACRO_end_file, dwarf::MacroString);
2967
+ else
2968
+ emitMacroFileImpl (F, U, dwarf::DW_MACINFO_start_file,
2969
+ dwarf::DW_MACINFO_end_file, dwarf::MacinfoString);
2889
2970
}
2890
2971
2891
2972
void DwarfDebug::emitDebugMacinfoImpl (MCSection *Section) {
@@ -2899,18 +2980,26 @@ void DwarfDebug::emitDebugMacinfoImpl(MCSection *Section) {
2899
2980
continue ;
2900
2981
Asm->OutStreamer ->SwitchSection (Section);
2901
2982
Asm->OutStreamer ->emitLabel (U.getMacroLabelBegin ());
2983
+ if (getDwarfVersion () >= 5 )
2984
+ emitMacroHeader (Asm, *this , U);
2902
2985
handleMacroNodes (Macros, U);
2903
2986
Asm->OutStreamer ->AddComment (" End Of Macro List Mark" );
2904
2987
Asm->emitInt8 (0 );
2905
2988
}
2906
2989
}
2907
2990
2908
- // / Emit macros into a debug macinfo section.
2991
+ // / Emit macros into a debug macinfo/macro section.
2909
2992
void DwarfDebug::emitDebugMacinfo () {
2910
- emitDebugMacinfoImpl (Asm->getObjFileLowering ().getDwarfMacinfoSection ());
2993
+ auto &ObjLower = Asm->getObjFileLowering ();
2994
+ emitDebugMacinfoImpl (getDwarfVersion () >= 5
2995
+ ? ObjLower.getDwarfMacroSection ()
2996
+ : ObjLower.getDwarfMacinfoSection ());
2911
2997
}
2912
2998
2913
2999
void DwarfDebug::emitDebugMacinfoDWO () {
3000
+ // FIXME: Add support for macro.dwo section.
3001
+ if (getDwarfVersion () >= 5 )
3002
+ return ;
2914
3003
emitDebugMacinfoImpl (Asm->getObjFileLowering ().getDwarfMacinfoDWOSection ());
2915
3004
}
2916
3005
0 commit comments