1 //===- DWARFYAML.cpp - DWARF YAMLIO implementation ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines classes for handling the YAML representation of DWARF Debug 10 // Info. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ObjectYAML/DWARFYAML.h" 15 #include "llvm/BinaryFormat/Dwarf.h" 16 #include "llvm/Support/Errc.h" 17 #include "llvm/Support/Error.h" 18 19 namespace llvm { 20 21 bool DWARFYAML::Data::isEmpty() const { 22 return getNonEmptySectionNames().empty(); 23 } 24 25 SetVector<StringRef> DWARFYAML::Data::getNonEmptySectionNames() const { 26 SetVector<StringRef> SecNames; 27 if (DebugStrings) 28 SecNames.insert("debug_str"); 29 if (DebugAranges) 30 SecNames.insert("debug_aranges"); 31 if (!DebugRanges.empty()) 32 SecNames.insert("debug_ranges"); 33 if (!DebugLines.empty()) 34 SecNames.insert("debug_line"); 35 if (!DebugAddr.empty()) 36 SecNames.insert("debug_addr"); 37 if (!DebugAbbrev.empty()) 38 SecNames.insert("debug_abbrev"); 39 if (!CompileUnits.empty()) 40 SecNames.insert("debug_info"); 41 if (PubNames) 42 SecNames.insert("debug_pubnames"); 43 if (PubTypes) 44 SecNames.insert("debug_pubtypes"); 45 if (GNUPubNames) 46 SecNames.insert("debug_gnu_pubnames"); 47 if (GNUPubTypes) 48 SecNames.insert("debug_gnu_pubtypes"); 49 if (DebugStrOffsets) 50 SecNames.insert("debug_str_offsets"); 51 if (DebugRnglists) 52 SecNames.insert("debug_rnglists"); 53 if (DebugLoclists) 54 SecNames.insert("debug_loclists"); 55 return SecNames; 56 } 57 58 Expected<DWARFYAML::Data::AbbrevTableInfo> 59 DWARFYAML::Data::getAbbrevTableInfoByID(uint64_t ID) const { 60 if (AbbrevTableInfoMap.empty()) { 61 uint64_t AbbrevTableOffset = 0; 62 for (auto &AbbrevTable : enumerate(DebugAbbrev)) { 63 // If the abbrev table's ID isn't specified, we use the index as its ID. 64 uint64_t AbbrevTableID = 65 AbbrevTable.value().ID.getValueOr(AbbrevTable.index()); 66 auto It = AbbrevTableInfoMap.insert( 67 {AbbrevTableID, AbbrevTableInfo{/*Index=*/AbbrevTable.index(), 68 /*Offset=*/AbbrevTableOffset}}); 69 if (!It.second) 70 return createStringError( 71 errc::invalid_argument, 72 "the ID (%" PRIu64 ") of abbrev table with index %zu has been used " 73 "by abbrev table with index %" PRIu64, 74 AbbrevTableID, AbbrevTable.index(), It.first->second.Index); 75 76 AbbrevTableOffset += 77 getAbbrevTableContentByIndex(AbbrevTable.index()).size(); 78 } 79 } 80 81 auto It = AbbrevTableInfoMap.find(ID); 82 if (It == AbbrevTableInfoMap.end()) 83 return createStringError(errc::invalid_argument, 84 "cannot find abbrev table whose ID is %" PRIu64, 85 ID); 86 return It->second; 87 } 88 89 namespace yaml { 90 91 void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) { 92 void *OldContext = IO.getContext(); 93 DWARFYAML::DWARFContext DWARFCtx; 94 IO.setContext(&DWARFCtx); 95 IO.mapOptional("debug_str", DWARF.DebugStrings); 96 IO.mapOptional("debug_abbrev", DWARF.DebugAbbrev); 97 IO.mapOptional("debug_aranges", DWARF.DebugAranges); 98 if (!DWARF.DebugRanges.empty() || !IO.outputting()) 99 IO.mapOptional("debug_ranges", DWARF.DebugRanges); 100 IO.mapOptional("debug_pubnames", DWARF.PubNames); 101 IO.mapOptional("debug_pubtypes", DWARF.PubTypes); 102 DWARFCtx.IsGNUPubSec = true; 103 IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames); 104 IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes); 105 IO.mapOptional("debug_info", DWARF.CompileUnits); 106 IO.mapOptional("debug_line", DWARF.DebugLines); 107 IO.mapOptional("debug_addr", DWARF.DebugAddr); 108 IO.mapOptional("debug_str_offsets", DWARF.DebugStrOffsets); 109 IO.mapOptional("debug_rnglists", DWARF.DebugRnglists); 110 IO.mapOptional("debug_loclists", DWARF.DebugLoclists); 111 IO.setContext(OldContext); 112 } 113 114 void MappingTraits<DWARFYAML::AbbrevTable>::mapping( 115 IO &IO, DWARFYAML::AbbrevTable &AbbrevTable) { 116 IO.mapOptional("ID", AbbrevTable.ID); 117 IO.mapOptional("Table", AbbrevTable.Table); 118 } 119 120 void MappingTraits<DWARFYAML::Abbrev>::mapping(IO &IO, 121 DWARFYAML::Abbrev &Abbrev) { 122 IO.mapOptional("Code", Abbrev.Code); 123 IO.mapRequired("Tag", Abbrev.Tag); 124 IO.mapRequired("Children", Abbrev.Children); 125 IO.mapOptional("Attributes", Abbrev.Attributes); 126 } 127 128 void MappingTraits<DWARFYAML::AttributeAbbrev>::mapping( 129 IO &IO, DWARFYAML::AttributeAbbrev &AttAbbrev) { 130 IO.mapRequired("Attribute", AttAbbrev.Attribute); 131 IO.mapRequired("Form", AttAbbrev.Form); 132 if(AttAbbrev.Form == dwarf::DW_FORM_implicit_const) 133 IO.mapRequired("Value", AttAbbrev.Value); 134 } 135 136 void MappingTraits<DWARFYAML::ARangeDescriptor>::mapping( 137 IO &IO, DWARFYAML::ARangeDescriptor &Descriptor) { 138 IO.mapRequired("Address", Descriptor.Address); 139 IO.mapRequired("Length", Descriptor.Length); 140 } 141 142 void MappingTraits<DWARFYAML::ARange>::mapping(IO &IO, 143 DWARFYAML::ARange &ARange) { 144 IO.mapOptional("Format", ARange.Format, dwarf::DWARF32); 145 IO.mapOptional("Length", ARange.Length); 146 IO.mapRequired("Version", ARange.Version); 147 IO.mapRequired("CuOffset", ARange.CuOffset); 148 IO.mapOptional("AddressSize", ARange.AddrSize); 149 IO.mapOptional("SegmentSelectorSize", ARange.SegSize, 0); 150 IO.mapOptional("Descriptors", ARange.Descriptors); 151 } 152 153 void MappingTraits<DWARFYAML::RangeEntry>::mapping( 154 IO &IO, DWARFYAML::RangeEntry &Descriptor) { 155 IO.mapRequired("LowOffset", Descriptor.LowOffset); 156 IO.mapRequired("HighOffset", Descriptor.HighOffset); 157 } 158 159 void MappingTraits<DWARFYAML::Ranges>::mapping(IO &IO, 160 DWARFYAML::Ranges &DebugRanges) { 161 IO.mapOptional("Offset", DebugRanges.Offset); 162 IO.mapOptional("AddrSize", DebugRanges.AddrSize); 163 IO.mapRequired("Entries", DebugRanges.Entries); 164 } 165 166 void MappingTraits<DWARFYAML::PubEntry>::mapping(IO &IO, 167 DWARFYAML::PubEntry &Entry) { 168 IO.mapRequired("DieOffset", Entry.DieOffset); 169 if (static_cast<DWARFYAML::DWARFContext *>(IO.getContext())->IsGNUPubSec) 170 IO.mapRequired("Descriptor", Entry.Descriptor); 171 IO.mapRequired("Name", Entry.Name); 172 } 173 174 void MappingTraits<DWARFYAML::PubSection>::mapping( 175 IO &IO, DWARFYAML::PubSection &Section) { 176 IO.mapOptional("Format", Section.Format, dwarf::DWARF32); 177 IO.mapRequired("Length", Section.Length); 178 IO.mapRequired("Version", Section.Version); 179 IO.mapRequired("UnitOffset", Section.UnitOffset); 180 IO.mapRequired("UnitSize", Section.UnitSize); 181 IO.mapRequired("Entries", Section.Entries); 182 } 183 184 void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) { 185 IO.mapOptional("Format", Unit.Format, dwarf::DWARF32); 186 IO.mapOptional("Length", Unit.Length); 187 IO.mapRequired("Version", Unit.Version); 188 if (Unit.Version >= 5) 189 IO.mapRequired("UnitType", Unit.Type); 190 IO.mapOptional("AbbrevTableID", Unit.AbbrevTableID); 191 IO.mapOptional("AbbrOffset", Unit.AbbrOffset); 192 IO.mapOptional("AddrSize", Unit.AddrSize); 193 IO.mapOptional("Entries", Unit.Entries); 194 } 195 196 void MappingTraits<DWARFYAML::Entry>::mapping(IO &IO, DWARFYAML::Entry &Entry) { 197 IO.mapRequired("AbbrCode", Entry.AbbrCode); 198 IO.mapOptional("Values", Entry.Values); 199 } 200 201 void MappingTraits<DWARFYAML::FormValue>::mapping( 202 IO &IO, DWARFYAML::FormValue &FormValue) { 203 IO.mapOptional("Value", FormValue.Value); 204 if (!FormValue.CStr.empty() || !IO.outputting()) 205 IO.mapOptional("CStr", FormValue.CStr); 206 if (!FormValue.BlockData.empty() || !IO.outputting()) 207 IO.mapOptional("BlockData", FormValue.BlockData); 208 } 209 210 void MappingTraits<DWARFYAML::File>::mapping(IO &IO, DWARFYAML::File &File) { 211 IO.mapRequired("Name", File.Name); 212 IO.mapRequired("DirIdx", File.DirIdx); 213 IO.mapRequired("ModTime", File.ModTime); 214 IO.mapRequired("Length", File.Length); 215 } 216 217 void MappingTraits<DWARFYAML::LineTableOpcode>::mapping( 218 IO &IO, DWARFYAML::LineTableOpcode &LineTableOpcode) { 219 IO.mapRequired("Opcode", LineTableOpcode.Opcode); 220 if (LineTableOpcode.Opcode == dwarf::DW_LNS_extended_op) { 221 IO.mapRequired("ExtLen", LineTableOpcode.ExtLen); 222 IO.mapRequired("SubOpcode", LineTableOpcode.SubOpcode); 223 } 224 225 if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting()) 226 IO.mapOptional("UnknownOpcodeData", LineTableOpcode.UnknownOpcodeData); 227 if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting()) 228 IO.mapOptional("StandardOpcodeData", LineTableOpcode.StandardOpcodeData); 229 if (!LineTableOpcode.FileEntry.Name.empty() || !IO.outputting()) 230 IO.mapOptional("FileEntry", LineTableOpcode.FileEntry); 231 if (LineTableOpcode.Opcode == dwarf::DW_LNS_advance_line || !IO.outputting()) 232 IO.mapOptional("SData", LineTableOpcode.SData); 233 IO.mapOptional("Data", LineTableOpcode.Data); 234 } 235 236 void MappingTraits<DWARFYAML::LineTable>::mapping( 237 IO &IO, DWARFYAML::LineTable &LineTable) { 238 IO.mapOptional("Format", LineTable.Format, dwarf::DWARF32); 239 IO.mapOptional("Length", LineTable.Length); 240 IO.mapRequired("Version", LineTable.Version); 241 IO.mapOptional("PrologueLength", LineTable.PrologueLength); 242 IO.mapRequired("MinInstLength", LineTable.MinInstLength); 243 if(LineTable.Version >= 4) 244 IO.mapRequired("MaxOpsPerInst", LineTable.MaxOpsPerInst); 245 IO.mapRequired("DefaultIsStmt", LineTable.DefaultIsStmt); 246 IO.mapRequired("LineBase", LineTable.LineBase); 247 IO.mapRequired("LineRange", LineTable.LineRange); 248 IO.mapRequired("OpcodeBase", LineTable.OpcodeBase); 249 IO.mapRequired("StandardOpcodeLengths", LineTable.StandardOpcodeLengths); 250 IO.mapRequired("IncludeDirs", LineTable.IncludeDirs); 251 IO.mapRequired("Files", LineTable.Files); 252 IO.mapRequired("Opcodes", LineTable.Opcodes); 253 } 254 255 void MappingTraits<DWARFYAML::SegAddrPair>::mapping( 256 IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) { 257 IO.mapOptional("Segment", SegAddrPair.Segment, 0); 258 IO.mapOptional("Address", SegAddrPair.Address, 0); 259 } 260 261 void MappingTraits<DWARFYAML::AddrTableEntry>::mapping( 262 IO &IO, DWARFYAML::AddrTableEntry &AddrTable) { 263 IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32); 264 IO.mapOptional("Length", AddrTable.Length); 265 IO.mapRequired("Version", AddrTable.Version); 266 IO.mapOptional("AddressSize", AddrTable.AddrSize); 267 IO.mapOptional("SegmentSelectorSize", AddrTable.SegSelectorSize, 0); 268 IO.mapOptional("Entries", AddrTable.SegAddrPairs); 269 } 270 271 void MappingTraits<DWARFYAML::StringOffsetsTable>::mapping( 272 IO &IO, DWARFYAML::StringOffsetsTable &StrOffsetsTable) { 273 IO.mapOptional("Format", StrOffsetsTable.Format, dwarf::DWARF32); 274 IO.mapOptional("Length", StrOffsetsTable.Length); 275 IO.mapOptional("Version", StrOffsetsTable.Version, 5); 276 IO.mapOptional("Padding", StrOffsetsTable.Padding, 0); 277 IO.mapOptional("Offsets", StrOffsetsTable.Offsets); 278 } 279 280 void MappingTraits<DWARFYAML::DWARFOperation>::mapping( 281 IO &IO, DWARFYAML::DWARFOperation &DWARFOperation) { 282 IO.mapRequired("Operator", DWARFOperation.Operator); 283 IO.mapOptional("Values", DWARFOperation.Values); 284 } 285 286 void MappingTraits<DWARFYAML::RnglistEntry>::mapping( 287 IO &IO, DWARFYAML::RnglistEntry &RnglistEntry) { 288 IO.mapRequired("Operator", RnglistEntry.Operator); 289 IO.mapOptional("Values", RnglistEntry.Values); 290 } 291 292 void MappingTraits<DWARFYAML::LoclistEntry>::mapping( 293 IO &IO, DWARFYAML::LoclistEntry &LoclistEntry) { 294 IO.mapRequired("Operator", LoclistEntry.Operator); 295 IO.mapOptional("Values", LoclistEntry.Values); 296 IO.mapOptional("DescriptionsLength", LoclistEntry.DescriptionsLength); 297 IO.mapOptional("Descriptions", LoclistEntry.Descriptions); 298 } 299 300 template <typename EntryType> 301 void MappingTraits<DWARFYAML::ListEntries<EntryType>>::mapping( 302 IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) { 303 IO.mapOptional("Entries", ListEntries.Entries); 304 IO.mapOptional("Content", ListEntries.Content); 305 } 306 307 template <typename EntryType> 308 StringRef MappingTraits<DWARFYAML::ListEntries<EntryType>>::validate( 309 IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) { 310 if (ListEntries.Entries && ListEntries.Content) 311 return "Entries and Content can't be used together"; 312 return StringRef(); 313 } 314 315 template <typename EntryType> 316 void MappingTraits<DWARFYAML::ListTable<EntryType>>::mapping( 317 IO &IO, DWARFYAML::ListTable<EntryType> &ListTable) { 318 IO.mapOptional("Format", ListTable.Format, dwarf::DWARF32); 319 IO.mapOptional("Length", ListTable.Length); 320 IO.mapOptional("Version", ListTable.Version, 5); 321 IO.mapOptional("AddressSize", ListTable.AddrSize); 322 IO.mapOptional("SegmentSelectorSize", ListTable.SegSelectorSize, 0); 323 IO.mapOptional("OffsetEntryCount", ListTable.OffsetEntryCount); 324 IO.mapOptional("Offsets", ListTable.Offsets); 325 IO.mapOptional("Lists", ListTable.Lists); 326 } 327 328 } // end namespace yaml 329 330 } // end namespace llvm 331