Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 1 | //===- DWARFContext.cpp ---------------------------------------------------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | e16e16a | 2012-07-19 07:03:58 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/Dwarf.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" |
| 23 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 24 | #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" |
| 25 | #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" |
| 28 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 29 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 30 | #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" |
| 31 | #include "llvm/DebugInfo/DWARF/DWARFSection.h" |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" |
George Rimar | 4bf3083 | 2017-01-11 15:26:41 +0000 | [diff] [blame] | 34 | #include "llvm/Object/Decompressor.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 35 | #include "llvm/Object/MachO.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 36 | #include "llvm/Object/ObjectFile.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 37 | #include "llvm/Object/RelocVisitor.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Casting.h" |
| 39 | #include "llvm/Support/DataExtractor.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Error.h" |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Format.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 42 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 44 | #include <algorithm> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 45 | #include <cstdint> |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 46 | #include <map> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 47 | #include <string> |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 48 | #include <tuple> |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 49 | #include <utility> |
| 50 | #include <vector> |
| 51 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 53 | using namespace dwarf; |
Rafael Espindola | 4f60a38 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 54 | using namespace object; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 55 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "dwarf" |
| 57 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 58 | using DWARFLineTable = DWARFDebugLine::LineTable; |
| 59 | using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; |
| 60 | using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind; |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 61 | |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 62 | static void dumpAccelSection(raw_ostream &OS, StringRef Name, |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 63 | const DWARFObject &Obj, |
| 64 | const DWARFSection &Section, |
| 65 | StringRef StringSection, bool LittleEndian) { |
| 66 | DWARFDataExtractor AccelSection(Obj, Section, LittleEndian, 0); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 67 | DataExtractor StrData(StringSection, LittleEndian, 0); |
| 68 | OS << "\n." << Name << " contents:\n"; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 69 | DWARFAcceleratorTable Accel(AccelSection, StrData); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 70 | if (!Accel.extract()) |
| 71 | return; |
| 72 | Accel.dump(OS); |
| 73 | } |
| 74 | |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 75 | static void |
| 76 | dumpDWARFv5StringOffsetsSection(raw_ostream &OS, StringRef SectionName, |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 77 | const DWARFObject &Obj, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 78 | const DWARFSection &StringOffsetsSection, |
| 79 | StringRef StringSection, bool LittleEndian) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 80 | DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 81 | uint32_t Offset = 0; |
| 82 | uint64_t SectionSize = StringOffsetsSection.Data.size(); |
| 83 | |
| 84 | while (Offset < SectionSize) { |
| 85 | unsigned Version = 0; |
| 86 | DwarfFormat Format = DWARF32; |
| 87 | unsigned EntrySize = 4; |
| 88 | // Perform validation and extract the segment size from the header. |
| 89 | if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, 4)) { |
| 90 | OS << "error: invalid contribution to string offsets table in section ." |
| 91 | << SectionName << ".\n"; |
| 92 | return; |
| 93 | } |
| 94 | uint32_t ContributionStart = Offset; |
| 95 | uint64_t ContributionSize = StrOffsetExt.getU32(&Offset); |
| 96 | // A contribution size of 0xffffffff indicates DWARF64, with the actual size |
| 97 | // in the following 8 bytes. Otherwise, the DWARF standard mandates that |
| 98 | // the contribution size must be at most 0xfffffff0. |
| 99 | if (ContributionSize == 0xffffffff) { |
| 100 | if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, 8)) { |
| 101 | OS << "error: invalid contribution to string offsets table in section ." |
| 102 | << SectionName << ".\n"; |
| 103 | return; |
| 104 | } |
| 105 | Format = DWARF64; |
| 106 | EntrySize = 8; |
| 107 | ContributionSize = StrOffsetExt.getU64(&Offset); |
| 108 | } else if (ContributionSize > 0xfffffff0) { |
| 109 | OS << "error: invalid contribution to string offsets table in section ." |
| 110 | << SectionName << ".\n"; |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // We must ensure that we don't read a partial record at the end, so we |
| 115 | // validate for a multiple of EntrySize. Also, we're expecting a version |
| 116 | // number and padding, which adds an additional 4 bytes. |
| 117 | uint64_t ValidationSize = |
| 118 | 4 + ((ContributionSize + EntrySize - 1) & (-(uint64_t)EntrySize)); |
| 119 | if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, ValidationSize)) { |
| 120 | OS << "error: contribution to string offsets table in section ." |
| 121 | << SectionName << " has invalid length.\n"; |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | Version = StrOffsetExt.getU16(&Offset); |
| 126 | Offset += 2; |
| 127 | OS << format("0x%8.8x: ", ContributionStart); |
| 128 | OS << "Contribution size = " << ContributionSize |
| 129 | << ", Version = " << Version << "\n"; |
| 130 | |
| 131 | uint32_t ContributionBase = Offset; |
| 132 | DataExtractor StrData(StringSection, LittleEndian, 0); |
| 133 | while (Offset - ContributionBase < ContributionSize) { |
| 134 | OS << format("0x%8.8x: ", Offset); |
| 135 | // FIXME: We can only extract strings in DWARF32 format at the moment. |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 136 | uint64_t StringOffset = |
| 137 | StrOffsetExt.getRelocatedValue(EntrySize, &Offset); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 138 | if (Format == DWARF32) { |
| 139 | OS << format("%8.8x ", StringOffset); |
| 140 | uint32_t StringOffset32 = (uint32_t)StringOffset; |
| 141 | const char *S = StrData.getCStr(&StringOffset32); |
| 142 | if (S) |
| 143 | OS << format("\"%s\"", S); |
| 144 | } else |
| 145 | OS << format("%16.16x ", StringOffset); |
| 146 | OS << "\n"; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Dump a DWARF string offsets section. This may be a DWARF v5 formatted |
| 152 | // string offsets section, where each compile or type unit contributes a |
| 153 | // number of entries (string offsets), with each contribution preceded by |
| 154 | // a header containing size and version number. Alternatively, it may be a |
| 155 | // monolithic series of string offsets, as generated by the pre-DWARF v5 |
| 156 | // implementation of split DWARF. |
| 157 | static void dumpStringOffsetsSection(raw_ostream &OS, StringRef SectionName, |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 158 | const DWARFObject &Obj, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 159 | const DWARFSection &StringOffsetsSection, |
| 160 | StringRef StringSection, bool LittleEndian, |
| 161 | unsigned MaxVersion) { |
| 162 | if (StringOffsetsSection.Data.empty()) |
| 163 | return; |
| 164 | OS << "\n." << SectionName << " contents:\n"; |
| 165 | // If we have at least one (compile or type) unit with DWARF v5 or greater, |
| 166 | // we assume that the section is formatted like a DWARF v5 string offsets |
| 167 | // section. |
| 168 | if (MaxVersion >= 5) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 169 | dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection, |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 170 | StringSection, LittleEndian); |
| 171 | else { |
| 172 | DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0); |
| 173 | uint32_t offset = 0; |
| 174 | uint64_t size = StringOffsetsSection.Data.size(); |
| 175 | // Ensure that size is a multiple of the size of an entry. |
| 176 | if (size & ((uint64_t)(sizeof(uint32_t) - 1))) { |
| 177 | OS << "error: size of ." << SectionName << " is not a multiple of " |
| 178 | << sizeof(uint32_t) << ".\n"; |
| 179 | size &= -(uint64_t)sizeof(uint32_t); |
| 180 | } |
| 181 | DataExtractor StrData(StringSection, LittleEndian, 0); |
| 182 | while (offset < size) { |
| 183 | OS << format("0x%8.8x: ", offset); |
| 184 | uint32_t StringOffset = strOffsetExt.getU32(&offset); |
| 185 | OS << format("%8.8x ", StringOffset); |
| 186 | const char *S = StrData.getCStr(&StringOffset); |
| 187 | if (S) |
| 188 | OS << format("\"%s\"", S); |
| 189 | OS << "\n"; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 194 | void DWARFContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts) { |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 195 | DIDumpType DumpType = DumpOpts.DumpType; |
| 196 | bool DumpEH = DumpOpts.DumpEH; |
| 197 | bool SummarizeTypes = DumpOpts.SummarizeTypes; |
| 198 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 199 | if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) { |
| 200 | OS << ".debug_abbrev contents:\n"; |
| 201 | getDebugAbbrev()->dump(OS); |
| 202 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 203 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 204 | if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) |
| 205 | if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) { |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 206 | OS << "\n.debug_abbrev.dwo contents:\n"; |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 207 | D->dump(OS); |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 208 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 209 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 210 | if (DumpType == DIDT_All || DumpType == DIDT_Info) { |
| 211 | OS << "\n.debug_info contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 212 | for (const auto &CU : compile_units()) |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 213 | CU->dump(OS, DumpOpts); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 214 | } |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 215 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 216 | if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) && |
| 217 | getNumDWOCompileUnits()) { |
| 218 | OS << "\n.debug_info.dwo contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 219 | for (const auto &DWOCU : dwo_compile_units()) |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 220 | DWOCU->dump(OS, DumpOpts); |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 221 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 222 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 223 | if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 224 | OS << "\n.debug_types contents:\n"; |
Frederic Riss | 312a02e | 2014-09-29 13:56:39 +0000 | [diff] [blame] | 225 | for (const auto &TUS : type_unit_sections()) |
| 226 | for (const auto &TU : TUS) |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 227 | TU->dump(OS, SummarizeTypes); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 230 | if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) && |
| 231 | getNumDWOTypeUnits()) { |
| 232 | OS << "\n.debug_types.dwo contents:\n"; |
Frederic Riss | 312a02e | 2014-09-29 13:56:39 +0000 | [diff] [blame] | 233 | for (const auto &DWOTUS : dwo_type_unit_sections()) |
| 234 | for (const auto &DWOTU : DWOTUS) |
David Blaikie | 50cc27e | 2016-10-18 21:09:48 +0000 | [diff] [blame] | 235 | DWOTU->dump(OS, SummarizeTypes); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 236 | } |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 237 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 238 | if (DumpType == DIDT_All || DumpType == DIDT_Loc) { |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 239 | OS << "\n.debug_loc contents:\n"; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 240 | getDebugLoc()->dump(OS); |
| 241 | } |
| 242 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 243 | if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) { |
| 244 | OS << "\n.debug_loc.dwo contents:\n"; |
| 245 | getDebugLocDWO()->dump(OS); |
| 246 | } |
| 247 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 248 | if (DumpType == DIDT_All || DumpType == DIDT_Frames) { |
| 249 | OS << "\n.debug_frame contents:\n"; |
| 250 | getDebugFrame()->dump(OS); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 251 | if (DumpEH) { |
| 252 | OS << "\n.eh_frame contents:\n"; |
| 253 | getEHFrame()->dump(OS); |
| 254 | } |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 257 | if (DumpType == DIDT_All || DumpType == DIDT_Macro) { |
| 258 | OS << "\n.debug_macinfo contents:\n"; |
| 259 | getDebugMacro()->dump(OS); |
| 260 | } |
| 261 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 262 | uint32_t offset = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 263 | if (DumpType == DIDT_All || DumpType == DIDT_Aranges) { |
| 264 | OS << "\n.debug_aranges contents:\n"; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 265 | DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 266 | DWARFDebugArangeSet set; |
| 267 | while (set.extract(arangesData, &offset)) |
| 268 | set.dump(OS); |
| 269 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 270 | |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 271 | uint8_t savedAddressByteSize = 0; |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 272 | if (DumpType == DIDT_All || DumpType == DIDT_Line) { |
| 273 | OS << "\n.debug_line contents:\n"; |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 274 | for (const auto &CU : compile_units()) { |
| 275 | savedAddressByteSize = CU->getAddressByteSize(); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 276 | auto CUDIE = CU->getUnitDIE(); |
| 277 | if (!CUDIE) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 278 | continue; |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 279 | if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 280 | DWARFDataExtractor lineData(*DObj, DObj->getLineSection(), |
| 281 | isLittleEndian(), savedAddressByteSize); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 282 | DWARFDebugLine::LineTable LineTable; |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 283 | uint32_t Offset = *StmtOffset; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 284 | LineTable.parse(lineData, &Offset); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 285 | LineTable.dump(OS); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 286 | } |
Benjamin Kramer | 6dda032 | 2011-09-15 18:02:20 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 289 | |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 290 | if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) { |
| 291 | OS << "\n.debug_cu_index contents:\n"; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 292 | getCUIndex().dump(OS); |
David Blaikie | 65a8efe | 2015-11-11 19:28:21 +0000 | [diff] [blame] | 293 | } |
| 294 | |
David Blaikie | 51c4028 | 2015-11-11 19:40:49 +0000 | [diff] [blame] | 295 | if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) { |
| 296 | OS << "\n.debug_tu_index contents:\n"; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 297 | getTUIndex().dump(OS); |
David Blaikie | 51c4028 | 2015-11-11 19:40:49 +0000 | [diff] [blame] | 298 | } |
| 299 | |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 300 | if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) { |
| 301 | OS << "\n.debug_line.dwo contents:\n"; |
| 302 | unsigned stmtOffset = 0; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 303 | DWARFDataExtractor lineData(*DObj, DObj->getLineDWOSection(), |
| 304 | isLittleEndian(), savedAddressByteSize); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 305 | DWARFDebugLine::LineTable LineTable; |
| 306 | while (LineTable.Prologue.parse(lineData, &stmtOffset)) { |
| 307 | LineTable.dump(OS); |
| 308 | LineTable.clear(); |
| 309 | } |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 312 | if (DumpType == DIDT_All || DumpType == DIDT_Str) { |
| 313 | OS << "\n.debug_str contents:\n"; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 314 | DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 315 | offset = 0; |
| 316 | uint32_t strOffset = 0; |
| 317 | while (const char *s = strData.getCStr(&offset)) { |
| 318 | OS << format("0x%8.8x: \"%s\"\n", strOffset, s); |
| 319 | strOffset = offset; |
| 320 | } |
Benjamin Kramer | 07d4b1c | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 321 | } |
Alexey Samsonov | 034e57a | 2012-08-27 07:17:47 +0000 | [diff] [blame] | 322 | |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 323 | if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) && |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 324 | !DObj->getStringDWOSection().empty()) { |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 325 | OS << "\n.debug_str.dwo contents:\n"; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 326 | DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0); |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 327 | offset = 0; |
| 328 | uint32_t strDWOOffset = 0; |
| 329 | while (const char *s = strDWOData.getCStr(&offset)) { |
| 330 | OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); |
| 331 | strDWOOffset = offset; |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 332 | } |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 333 | } |
David Blaikie | 622dce4 | 2014-01-08 23:29:59 +0000 | [diff] [blame] | 334 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 335 | if (DumpType == DIDT_All || DumpType == DIDT_Ranges) { |
| 336 | OS << "\n.debug_ranges contents:\n"; |
| 337 | // In fact, different compile units may have different address byte |
| 338 | // sizes, but for simplicity we just use the address byte size of the last |
| 339 | // compile unit (there is no easy and fast way to associate address range |
| 340 | // list and the compile unit it describes). |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 341 | DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(), |
| 342 | isLittleEndian(), savedAddressByteSize); |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 343 | offset = 0; |
| 344 | DWARFDebugRangeList rangeList; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 345 | while (rangeList.extract(rangesData, &offset)) |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 346 | rangeList.dump(OS); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 347 | } |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 348 | |
Eric Christopher | 0de5359 | 2013-09-25 23:02:36 +0000 | [diff] [blame] | 349 | if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 350 | DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 351 | .dump("debug_pubnames", OS); |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 352 | |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 353 | if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 354 | DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false) |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 355 | .dump("debug_pubtypes", OS); |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 356 | |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 357 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 358 | DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(), |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 359 | true /* GnuStyle */) |
| 360 | .dump("debug_gnu_pubnames", OS); |
David Blaikie | ecd21ff | 2013-09-24 19:50:00 +0000 | [diff] [blame] | 361 | |
| 362 | if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 363 | DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(), |
George Rimar | e71e33f | 2016-12-17 09:10:32 +0000 | [diff] [blame] | 364 | true /* GnuStyle */) |
| 365 | .dump("debug_gnu_pubtypes", OS); |
David Blaikie | 404d304 | 2013-09-19 23:01:29 +0000 | [diff] [blame] | 366 | |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 367 | if (DumpType == DIDT_All || DumpType == DIDT_StrOffsets) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 368 | dumpStringOffsetsSection( |
| 369 | OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(), |
| 370 | DObj->getStringSection(), isLittleEndian(), getMaxVersion()); |
Wolfgang Pieb | 77d3e93 | 2017-06-06 01:22:34 +0000 | [diff] [blame] | 371 | |
| 372 | if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 373 | dumpStringOffsetsSection( |
| 374 | OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(), |
| 375 | DObj->getStringDWOSection(), isLittleEndian(), getMaxVersion()); |
David Blaikie | 66865d6 | 2014-01-09 00:13:35 +0000 | [diff] [blame] | 376 | } |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 377 | |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 378 | if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) && |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 379 | !DObj->getGdbIndexSection().empty()) { |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 380 | OS << "\n.gnu_index contents:\n"; |
| 381 | getGdbIndex().dump(OS); |
| 382 | } |
| 383 | |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 384 | if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 385 | dumpAccelSection(OS, "apple_names", *DObj, DObj->getAppleNamesSection(), |
| 386 | DObj->getStringSection(), isLittleEndian()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 387 | |
| 388 | if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 389 | dumpAccelSection(OS, "apple_types", *DObj, DObj->getAppleTypesSection(), |
| 390 | DObj->getStringSection(), isLittleEndian()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 391 | |
| 392 | if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 393 | dumpAccelSection(OS, "apple_namespaces", *DObj, |
| 394 | DObj->getAppleNamespacesSection(), |
| 395 | DObj->getStringSection(), isLittleEndian()); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 396 | |
| 397 | if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC) |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 398 | dumpAccelSection(OS, "apple_objc", *DObj, DObj->getAppleObjCSection(), |
| 399 | DObj->getStringSection(), isLittleEndian()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 400 | } |
| 401 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 402 | DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) { |
| 403 | // FIXME: Improve this for the case where this DWO file is really a DWP file |
| 404 | // with an index - use the index for lookup instead of a linear search. |
| 405 | for (const auto &DWOCU : dwo_compile_units()) |
| 406 | if (DWOCU->getDWOId() == Hash) |
| 407 | return DWOCU.get(); |
| 408 | return nullptr; |
| 409 | } |
| 410 | |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 411 | DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) { |
| 412 | parseCompileUnits(); |
| 413 | if (auto *CU = CUs.getUnitForOffset(Offset)) |
| 414 | return CU->getDIEForOffset(Offset); |
| 415 | return DWARFDie(); |
| 416 | } |
| 417 | |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 418 | bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) { |
| 419 | bool Success = true; |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 420 | DWARFVerifier verifier(OS, *this); |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 421 | if (DumpType == DIDT_All || DumpType == DIDT_Info) { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 422 | if (!verifier.handleDebugInfo()) |
Greg Clayton | c7695a8 | 2017-05-02 20:28:33 +0000 | [diff] [blame] | 423 | Success = false; |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 424 | } |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 425 | if (DumpType == DIDT_All || DumpType == DIDT_Line) { |
Greg Clayton | b8c162b | 2017-05-03 16:02:29 +0000 | [diff] [blame] | 426 | if (!verifier.handleDebugLine()) |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 427 | Success = false; |
| 428 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 429 | if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) { |
| 430 | if (!verifier.handleAppleNames()) |
| 431 | Success = false; |
| 432 | } |
Greg Clayton | 48432cf | 2017-05-01 22:07:02 +0000 | [diff] [blame] | 433 | return Success; |
| 434 | } |
Spyridoula Gravani | e41823b | 2017-06-14 00:17:55 +0000 | [diff] [blame] | 435 | |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 436 | const DWARFUnitIndex &DWARFContext::getCUIndex() { |
| 437 | if (CUIndex) |
| 438 | return *CUIndex; |
| 439 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 440 | DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 441 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 442 | CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 443 | CUIndex->parse(CUIndexData); |
| 444 | return *CUIndex; |
| 445 | } |
| 446 | |
| 447 | const DWARFUnitIndex &DWARFContext::getTUIndex() { |
| 448 | if (TUIndex) |
| 449 | return *TUIndex; |
| 450 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 451 | DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 452 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 453 | TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES); |
David Blaikie | 82641be | 2015-11-17 00:39:55 +0000 | [diff] [blame] | 454 | TUIndex->parse(TUIndexData); |
| 455 | return *TUIndex; |
| 456 | } |
| 457 | |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 458 | DWARFGdbIndex &DWARFContext::getGdbIndex() { |
| 459 | if (GdbIndex) |
| 460 | return *GdbIndex; |
| 461 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 462 | DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0); |
George Rimar | 4f82df5 | 2016-09-23 11:01:53 +0000 | [diff] [blame] | 463 | GdbIndex = llvm::make_unique<DWARFGdbIndex>(); |
| 464 | GdbIndex->parse(GdbIndexData); |
| 465 | return *GdbIndex; |
| 466 | } |
| 467 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 468 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { |
| 469 | if (Abbrev) |
| 470 | return Abbrev.get(); |
| 471 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 472 | DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 473 | |
| 474 | Abbrev.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 475 | Abbrev->extract(abbrData); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 476 | return Abbrev.get(); |
| 477 | } |
| 478 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 479 | const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { |
| 480 | if (AbbrevDWO) |
| 481 | return AbbrevDWO.get(); |
| 482 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 483 | DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 484 | AbbrevDWO.reset(new DWARFDebugAbbrev()); |
Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 485 | AbbrevDWO->extract(abbrData); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 486 | return AbbrevDWO.get(); |
| 487 | } |
| 488 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 489 | const DWARFDebugLoc *DWARFContext::getDebugLoc() { |
| 490 | if (Loc) |
| 491 | return Loc.get(); |
| 492 | |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 493 | Loc.reset(new DWARFDebugLoc); |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 494 | // assume all compile units have the same address byte size |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 495 | if (getNumCompileUnits()) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 496 | DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(), |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 497 | getCompileUnitAtIndex(0)->getAddressByteSize()); |
| 498 | Loc->parse(LocData); |
| 499 | } |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 500 | return Loc.get(); |
| 501 | } |
| 502 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 503 | const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() { |
| 504 | if (LocDWO) |
| 505 | return LocDWO.get(); |
| 506 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 507 | DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), 0); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 508 | LocDWO.reset(new DWARFDebugLocDWO()); |
| 509 | LocDWO->parse(LocData); |
| 510 | return LocDWO.get(); |
| 511 | } |
| 512 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 513 | const DWARFDebugAranges *DWARFContext::getDebugAranges() { |
| 514 | if (Aranges) |
| 515 | return Aranges.get(); |
| 516 | |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 517 | Aranges.reset(new DWARFDebugAranges()); |
Alexey Samsonov | a1694c1 | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 518 | Aranges->generate(this); |
Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 519 | return Aranges.get(); |
| 520 | } |
| 521 | |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 522 | const DWARFDebugFrame *DWARFContext::getDebugFrame() { |
| 523 | if (DebugFrame) |
| 524 | return DebugFrame.get(); |
| 525 | |
| 526 | // There's a "bug" in the DWARFv3 standard with respect to the target address |
| 527 | // size within debug frame sections. While DWARF is supposed to be independent |
| 528 | // of its container, FDEs have fields with size being "target address size", |
| 529 | // which isn't specified in DWARF in general. It's only specified for CUs, but |
| 530 | // .eh_frame can appear without a .debug_info section. Follow the example of |
| 531 | // other tools (libdwarf) and extract this from the container (ObjectFile |
| 532 | // provides this information). This problem is fixed in DWARFv4 |
| 533 | // See this dwarf-discuss discussion for more details: |
| 534 | // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 535 | DataExtractor debugFrameData(DObj->getDebugFrameSection(), isLittleEndian(), |
| 536 | DObj->getAddressSize()); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 537 | DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */)); |
| 538 | DebugFrame->parse(debugFrameData); |
| 539 | return DebugFrame.get(); |
| 540 | } |
| 541 | |
| 542 | const DWARFDebugFrame *DWARFContext::getEHFrame() { |
| 543 | if (EHFrame) |
| 544 | return EHFrame.get(); |
| 545 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 546 | DataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(), |
| 547 | DObj->getAddressSize()); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 548 | DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */)); |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 549 | DebugFrame->parse(debugFrameData); |
| 550 | return DebugFrame.get(); |
| 551 | } |
| 552 | |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 553 | const DWARFDebugMacro *DWARFContext::getDebugMacro() { |
| 554 | if (Macro) |
| 555 | return Macro.get(); |
| 556 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 557 | DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0); |
Amjad Aboud | e59cc3e | 2015-11-12 09:38:54 +0000 | [diff] [blame] | 558 | Macro.reset(new DWARFDebugMacro()); |
| 559 | Macro->parse(MacinfoData); |
| 560 | return Macro.get(); |
| 561 | } |
| 562 | |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 563 | const DWARFLineTable * |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 564 | DWARFContext::getLineTableForUnit(DWARFUnit *U) { |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 565 | if (!Line) |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 566 | Line.reset(new DWARFDebugLine); |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 567 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 568 | auto UnitDIE = U->getUnitDIE(); |
| 569 | if (!UnitDIE) |
Alexey Samsonov | 7a18c06 | 2015-05-19 21:54:32 +0000 | [diff] [blame] | 570 | return nullptr; |
David Blaikie | c4e2bed | 2015-11-17 21:08:05 +0000 | [diff] [blame] | 571 | |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 572 | auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 573 | if (!Offset) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 574 | return nullptr; // No line table for this compile unit. |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 575 | |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 576 | uint32_t stmtOffset = *Offset + U->getLineTableOffset(); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 577 | // See if the line table is cached. |
Eric Christopher | 494109b | 2012-10-16 23:46:25 +0000 | [diff] [blame] | 578 | if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 579 | return lt; |
| 580 | |
Greg Clayton | 48ff66a | 2017-05-04 18:29:44 +0000 | [diff] [blame] | 581 | // Make sure the offset is good before we try to parse. |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 582 | if (stmtOffset >= U->getLineSection().Data.size()) |
Greg Clayton | 48ff66a | 2017-05-04 18:29:44 +0000 | [diff] [blame] | 583 | return nullptr; |
| 584 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 585 | // We have to parse it first. |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 586 | DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(), |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 587 | U->getAddressByteSize()); |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 588 | return Line->getOrParseLineTable(lineData, stmtOffset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 591 | void DWARFContext::parseCompileUnits() { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 592 | CUs.parse(*this, DObj->getInfoSection()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 593 | } |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 594 | |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 595 | void DWARFContext::parseTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 596 | if (!TUs.empty()) |
| 597 | return; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 598 | DObj->forEachTypesSections([&](const DWARFSection &S) { |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 599 | TUs.emplace_back(); |
Rafael Espindola | 5e5dfa1 | 2017-07-12 21:08:24 +0000 | [diff] [blame] | 600 | TUs.back().parse(*this, S); |
| 601 | }); |
David Blaikie | 03c089c | 2013-09-23 22:44:47 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 604 | void DWARFContext::parseDWOCompileUnits() { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 605 | DWOCUs.parseDWO(*this, DObj->getInfoDWOSection()); |
Eric Christopher | da4b219 | 2013-01-02 23:52:13 +0000 | [diff] [blame] | 606 | } |
| 607 | |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 608 | void DWARFContext::parseDWOTypeUnits() { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 609 | if (!DWOTUs.empty()) |
| 610 | return; |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 611 | DObj->forEachTypesDWOSections([&](const DWARFSection &S) { |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 612 | DWOTUs.emplace_back(); |
Rafael Espindola | 5e5dfa1 | 2017-07-12 21:08:24 +0000 | [diff] [blame] | 613 | DWOTUs.back().parseDWO(*this, S); |
| 614 | }); |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 617 | DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 618 | parseCompileUnits(); |
Frederic Riss | 4e126a0 | 2014-09-15 07:50:27 +0000 | [diff] [blame] | 619 | return CUs.getUnitForOffset(Offset); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 622 | DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 623 | // First, get the offset of the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 624 | uint32_t CUOffset = getDebugAranges()->findAddress(Address); |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 625 | // Retrieve the compile unit. |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 626 | return getCompileUnitForOffset(CUOffset); |
| 627 | } |
| 628 | |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 629 | static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, |
| 630 | uint64_t Address, |
| 631 | FunctionNameKind Kind, |
| 632 | std::string &FunctionName, |
| 633 | uint32_t &StartLine) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 634 | // The address may correspond to instruction in some inlined function, |
| 635 | // so we have to build the chain of inlined functions and take the |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 636 | // name of the topmost function in it. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 637 | SmallVector<DWARFDie, 4> InlinedChain; |
| 638 | CU->getInlinedChainForAddress(Address, InlinedChain); |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 639 | if (InlinedChain.empty()) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 640 | return false; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 641 | |
| 642 | const DWARFDie &DIE = InlinedChain[0]; |
| 643 | bool FoundResult = false; |
| 644 | const char *Name = nullptr; |
| 645 | if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 646 | FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 647 | FoundResult = true; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 648 | } |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 649 | if (auto DeclLineResult = DIE.getDeclLine()) { |
| 650 | StartLine = DeclLineResult; |
| 651 | FoundResult = true; |
| 652 | } |
| 653 | |
| 654 | return FoundResult; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 657 | DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 658 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 659 | DILineInfo Result; |
| 660 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 661 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 662 | if (!CU) |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 663 | return Result; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 664 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, |
| 665 | Result.FunctionName, |
| 666 | Result.StartLine); |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 667 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 668 | if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) |
| 669 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 670 | Spec.FLIKind, Result); |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 671 | } |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 672 | return Result; |
Benjamin Kramer | 2602ca6 | 2011-09-15 20:43:22 +0000 | [diff] [blame] | 673 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 674 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 675 | DILineInfoTable |
| 676 | DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size, |
| 677 | DILineInfoSpecifier Spec) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 678 | DILineInfoTable Lines; |
| 679 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 680 | if (!CU) |
| 681 | return Lines; |
| 682 | |
| 683 | std::string FunctionName = "<invalid>"; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 684 | uint32_t StartLine = 0; |
| 685 | getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName, |
| 686 | StartLine); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 687 | |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 688 | // If the Specifier says we don't need FileLineInfo, just |
| 689 | // return the top-most function at the starting address. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 690 | if (Spec.FLIKind == FileLineInfoKind::None) { |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 691 | DILineInfo Result; |
| 692 | Result.FunctionName = FunctionName; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 693 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 694 | Lines.push_back(std::make_pair(Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 695 | return Lines; |
| 696 | } |
| 697 | |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 698 | const DWARFLineTable *LineTable = getLineTableForUnit(CU); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 699 | |
| 700 | // Get the index of row we're looking for in the line table. |
| 701 | std::vector<uint32_t> RowVector; |
| 702 | if (!LineTable->lookupAddressRange(Address, Size, RowVector)) |
| 703 | return Lines; |
| 704 | |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 705 | for (uint32_t RowIndex : RowVector) { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 706 | // Take file number and line/column from the row. |
| 707 | const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 708 | DILineInfo Result; |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 709 | LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), |
| 710 | Spec.FLIKind, Result.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 711 | Result.FunctionName = FunctionName; |
| 712 | Result.Line = Row.Line; |
| 713 | Result.Column = Row.Column; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 714 | Result.StartLine = StartLine; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 715 | Lines.push_back(std::make_pair(Row.Address, Result)); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return Lines; |
| 719 | } |
| 720 | |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 721 | DIInliningInfo |
| 722 | DWARFContext::getInliningInfoForAddress(uint64_t Address, |
| 723 | DILineInfoSpecifier Spec) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 724 | DIInliningInfo InliningInfo; |
| 725 | |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 726 | DWARFCompileUnit *CU = getCompileUnitForAddress(Address); |
| 727 | if (!CU) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 728 | return InliningInfo; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 729 | |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 730 | const DWARFLineTable *LineTable = nullptr; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 731 | SmallVector<DWARFDie, 4> InlinedChain; |
| 732 | CU->getInlinedChainForAddress(Address, InlinedChain); |
| 733 | if (InlinedChain.size() == 0) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 734 | // If there is no DIE for address (e.g. it is in unavailable .dwo file), |
| 735 | // try to at least get file/line info from symbol table. |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 736 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 737 | DILineInfo Frame; |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 738 | LineTable = getLineTableForUnit(CU); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 739 | if (LineTable && |
| 740 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 741 | Spec.FLIKind, Frame)) |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 742 | InliningInfo.addFrame(Frame); |
Alexey Samsonov | 5c39fdf | 2014-04-18 22:22:44 +0000 | [diff] [blame] | 743 | } |
| 744 | return InliningInfo; |
| 745 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 746 | |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 747 | uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 748 | for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { |
| 749 | DWARFDie &FunctionDIE = InlinedChain[i]; |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 750 | DILineInfo Frame; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 751 | // Get function name if necessary. |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 752 | if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind)) |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 753 | Frame.FunctionName = Name; |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 754 | if (auto DeclLineResult = FunctionDIE.getDeclLine()) |
| 755 | Frame.StartLine = DeclLineResult; |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 756 | if (Spec.FLIKind != FileLineInfoKind::None) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 757 | if (i == 0) { |
| 758 | // For the topmost frame, initialize the line table of this |
| 759 | // compile unit and fetch file/line info from it. |
Frederic Riss | ec8a5ba | 2014-09-04 06:14:40 +0000 | [diff] [blame] | 760 | LineTable = getLineTableForUnit(CU); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 761 | // For the topmost routine, get file/line info from line table. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 762 | if (LineTable) |
| 763 | LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), |
| 764 | Spec.FLIKind, Frame); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 765 | } else { |
| 766 | // Otherwise, use call file, call line and call column from |
| 767 | // previous DIE in inlined chain. |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 768 | if (LineTable) |
| 769 | LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), |
| 770 | Spec.FLIKind, Frame.FileName); |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 771 | Frame.Line = CallLine; |
| 772 | Frame.Column = CallColumn; |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 773 | Frame.Discriminator = CallDiscriminator; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 774 | } |
| 775 | // Get call file/line/column of a current DIE. |
| 776 | if (i + 1 < n) { |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 777 | FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn, |
| 778 | CallDiscriminator); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 781 | InliningInfo.addFrame(Frame); |
| 782 | } |
| 783 | return InliningInfo; |
| 784 | } |
| 785 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 786 | /// DWARFContextInMemory is the simplest possible implementation of a |
| 787 | /// DWARFContext. It assumes all content is available in memory and stores |
| 788 | /// pointers to it. |
| 789 | class DWARFContextInMemory : public DWARFContext { |
| 790 | public: |
| 791 | DWARFContextInMemory( |
| 792 | const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr, |
| 793 | function_ref<ErrorPolicy(Error)> HandleError = defaultErrorHandler); |
| 794 | |
| 795 | DWARFContextInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 796 | uint8_t AddrSize, |
| 797 | bool isLittleEndian = sys::IsLittleEndianHost); |
| 798 | }; |
| 799 | |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 800 | std::shared_ptr<DWARFContext> |
| 801 | DWARFContext::getDWOContext(StringRef AbsolutePath) { |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 802 | if (auto S = DWP.lock()) { |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 803 | DWARFContext *Ctxt = S->Context.get(); |
| 804 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 805 | } |
| 806 | |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 807 | std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath]; |
| 808 | |
| 809 | if (auto S = Entry->lock()) { |
| 810 | DWARFContext *Ctxt = S->Context.get(); |
| 811 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 812 | } |
| 813 | |
| 814 | SmallString<128> DWPName; |
| 815 | Expected<OwningBinary<ObjectFile>> Obj = [&] { |
| 816 | if (!CheckedForDWP) { |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 817 | (DObj->getFileName() + ".dwp").toVector(DWPName); |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 818 | auto Obj = object::ObjectFile::createObjectFile(DWPName); |
| 819 | if (Obj) { |
| 820 | Entry = &DWP; |
| 821 | return Obj; |
| 822 | } else { |
| 823 | CheckedForDWP = true; |
| 824 | // TODO: Should this error be handled (maybe in a high verbosity mode) |
| 825 | // before falling back to .dwo files? |
| 826 | consumeError(Obj.takeError()); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return object::ObjectFile::createObjectFile(AbsolutePath); |
| 831 | }(); |
| 832 | |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 833 | if (!Obj) { |
| 834 | // TODO: Actually report errors helpfully. |
| 835 | consumeError(Obj.takeError()); |
| 836 | return nullptr; |
| 837 | } |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 838 | |
| 839 | auto S = std::make_shared<DWOFile>(); |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 840 | S->File = std::move(Obj.get()); |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 841 | S->Context = DWARFContext::create(*S->File.getBinary()); |
David Blaikie | 15d85fc | 2017-05-23 06:48:53 +0000 | [diff] [blame] | 842 | *Entry = S; |
David Blaikie | f9803fb | 2017-05-23 00:30:42 +0000 | [diff] [blame] | 843 | auto *Ctxt = S->Context.get(); |
| 844 | return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); |
| 845 | } |
| 846 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 847 | static Error createError(const Twine &Reason, llvm::Error E) { |
| 848 | return make_error<StringError>(Reason + toString(std::move(E)), |
| 849 | inconvertibleErrorCode()); |
| 850 | } |
| 851 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 852 | /// SymInfo contains information about symbol: it's address |
| 853 | /// and section index which is -1LL for absolute symbols. |
| 854 | struct SymInfo { |
| 855 | uint64_t Address; |
| 856 | uint64_t SectionIndex; |
| 857 | }; |
| 858 | |
| 859 | /// Returns the address of symbol relocation used against and a section index. |
| 860 | /// Used for futher relocations computation. Symbol's section load address is |
| 861 | static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj, |
| 862 | const RelocationRef &Reloc, |
| 863 | const LoadedObjectInfo *L, |
| 864 | std::map<SymbolRef, SymInfo> &Cache) { |
| 865 | SymInfo Ret = {0, (uint64_t)-1LL}; |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 866 | object::section_iterator RSec = Obj.section_end(); |
| 867 | object::symbol_iterator Sym = Reloc.getSymbol(); |
| 868 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 869 | std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end(); |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 870 | // First calculate the address of the symbol or section as it appears |
| 871 | // in the object file |
| 872 | if (Sym != Obj.symbol_end()) { |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 873 | bool New; |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 874 | std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}}); |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 875 | if (!New) |
| 876 | return CacheIt->second; |
| 877 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 878 | Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); |
| 879 | if (!SymAddrOrErr) |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 880 | return createError("failed to compute symbol address: ", |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 881 | SymAddrOrErr.takeError()); |
| 882 | |
| 883 | // Also remember what section this symbol is in for later |
| 884 | auto SectOrErr = Sym->getSection(); |
| 885 | if (!SectOrErr) |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 886 | return createError("failed to get symbol section: ", |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 887 | SectOrErr.takeError()); |
| 888 | |
| 889 | RSec = *SectOrErr; |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 890 | Ret.Address = *SymAddrOrErr; |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 891 | } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { |
| 892 | RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 893 | Ret.Address = RSec->getAddress(); |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 894 | } |
| 895 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 896 | if (RSec != Obj.section_end()) |
| 897 | Ret.SectionIndex = RSec->getIndex(); |
| 898 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 899 | // If we are given load addresses for the sections, we need to adjust: |
| 900 | // SymAddr = (Address of Symbol Or Section in File) - |
| 901 | // (Address of Section in File) + |
| 902 | // (Load Address of Section) |
| 903 | // RSec is now either the section being targeted or the section |
| 904 | // containing the symbol being targeted. In either case, |
| 905 | // we need to perform the same computation. |
| 906 | if (L && RSec != Obj.section_end()) |
| 907 | if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 908 | Ret.Address += SectionLoadAddress - RSec->getAddress(); |
George Rimar | 958b01a | 2017-05-15 11:45:28 +0000 | [diff] [blame] | 909 | |
| 910 | if (CacheIt != Cache.end()) |
| 911 | CacheIt->second = Ret; |
| 912 | |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 913 | return Ret; |
| 914 | } |
| 915 | |
| 916 | static bool isRelocScattered(const object::ObjectFile &Obj, |
| 917 | const RelocationRef &Reloc) { |
George Rimar | d4998b0 | 2017-04-13 09:52:50 +0000 | [diff] [blame] | 918 | const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj); |
| 919 | if (!MachObj) |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 920 | return false; |
| 921 | // MachO also has relocations that point to sections and |
| 922 | // scattered relocations. |
George Rimar | 702dac6 | 2017-04-12 08:59:15 +0000 | [diff] [blame] | 923 | auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl()); |
| 924 | return MachObj->isRelocationScattered(RelocInfo); |
| 925 | } |
| 926 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 927 | ErrorPolicy DWARFContext::defaultErrorHandler(Error E) { |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 928 | errs() << "error: " + toString(std::move(E)) << '\n'; |
| 929 | return ErrorPolicy::Continue; |
| 930 | } |
| 931 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 932 | class DWARFObjInMemory final : public DWARFObject { |
| 933 | bool IsLittleEndian; |
| 934 | uint8_t AddressSize; |
| 935 | StringRef FileName; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 936 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 937 | using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap, |
| 938 | std::map<object::SectionRef, unsigned>>; |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 939 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 940 | TypeSectionMap TypesSections; |
| 941 | TypeSectionMap TypesDWOSections; |
| 942 | |
| 943 | DWARFSectionMap InfoSection; |
| 944 | DWARFSectionMap LocSection; |
| 945 | DWARFSectionMap LineSection; |
| 946 | DWARFSectionMap RangeSection; |
| 947 | DWARFSectionMap StringOffsetSection; |
| 948 | DWARFSectionMap InfoDWOSection; |
| 949 | DWARFSectionMap LineDWOSection; |
| 950 | DWARFSectionMap LocDWOSection; |
| 951 | DWARFSectionMap StringOffsetDWOSection; |
| 952 | DWARFSectionMap RangeDWOSection; |
| 953 | DWARFSectionMap AddrSection; |
| 954 | DWARFSectionMap AppleNamesSection; |
| 955 | DWARFSectionMap AppleTypesSection; |
| 956 | DWARFSectionMap AppleNamespacesSection; |
| 957 | DWARFSectionMap AppleObjCSection; |
| 958 | |
| 959 | DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { |
| 960 | return StringSwitch<DWARFSectionMap *>(Name) |
| 961 | .Case("debug_info", &InfoSection) |
| 962 | .Case("debug_loc", &LocSection) |
| 963 | .Case("debug_line", &LineSection) |
| 964 | .Case("debug_str_offsets", &StringOffsetSection) |
| 965 | .Case("debug_ranges", &RangeSection) |
| 966 | .Case("debug_info.dwo", &InfoDWOSection) |
| 967 | .Case("debug_loc.dwo", &LocDWOSection) |
| 968 | .Case("debug_line.dwo", &LineDWOSection) |
| 969 | .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) |
| 970 | .Case("debug_addr", &AddrSection) |
| 971 | .Case("apple_names", &AppleNamesSection) |
| 972 | .Case("apple_types", &AppleTypesSection) |
| 973 | .Case("apple_namespaces", &AppleNamespacesSection) |
| 974 | .Case("apple_namespac", &AppleNamespacesSection) |
| 975 | .Case("apple_objc", &AppleObjCSection) |
| 976 | .Default(nullptr); |
| 977 | } |
| 978 | |
| 979 | StringRef AbbrevSection; |
| 980 | StringRef ARangeSection; |
| 981 | StringRef DebugFrameSection; |
| 982 | StringRef EHFrameSection; |
| 983 | StringRef StringSection; |
| 984 | StringRef MacinfoSection; |
| 985 | StringRef PubNamesSection; |
| 986 | StringRef PubTypesSection; |
| 987 | StringRef GnuPubNamesSection; |
| 988 | StringRef AbbrevDWOSection; |
| 989 | StringRef StringDWOSection; |
| 990 | StringRef GnuPubTypesSection; |
| 991 | StringRef CUIndexSection; |
| 992 | StringRef GdbIndexSection; |
| 993 | StringRef TUIndexSection; |
| 994 | |
| 995 | SmallVector<SmallString<32>, 4> UncompressedSections; |
| 996 | |
| 997 | StringRef *mapSectionToMember(StringRef Name) { |
| 998 | if (DWARFSection *Sec = mapNameToDWARFSection(Name)) |
| 999 | return &Sec->Data; |
| 1000 | return StringSwitch<StringRef *>(Name) |
| 1001 | .Case("debug_abbrev", &AbbrevSection) |
| 1002 | .Case("debug_aranges", &ARangeSection) |
| 1003 | .Case("debug_frame", &DebugFrameSection) |
| 1004 | .Case("eh_frame", &EHFrameSection) |
| 1005 | .Case("debug_str", &StringSection) |
| 1006 | .Case("debug_macinfo", &MacinfoSection) |
| 1007 | .Case("debug_pubnames", &PubNamesSection) |
| 1008 | .Case("debug_pubtypes", &PubTypesSection) |
| 1009 | .Case("debug_gnu_pubnames", &GnuPubNamesSection) |
| 1010 | .Case("debug_gnu_pubtypes", &GnuPubTypesSection) |
| 1011 | .Case("debug_abbrev.dwo", &AbbrevDWOSection) |
| 1012 | .Case("debug_str.dwo", &StringDWOSection) |
| 1013 | .Case("debug_cu_index", &CUIndexSection) |
| 1014 | .Case("debug_tu_index", &TUIndexSection) |
| 1015 | .Case("gdb_index", &GdbIndexSection) |
| 1016 | // Any more debug info sections go here. |
| 1017 | .Default(nullptr); |
| 1018 | } |
| 1019 | |
| 1020 | /// If Sec is compressed section, decompresses and updates its contents |
| 1021 | /// provided by Data. Otherwise leaves it unchanged. |
| 1022 | Error maybeDecompress(const object::SectionRef &Sec, StringRef Name, |
| 1023 | StringRef &Data) { |
| 1024 | if (!Decompressor::isCompressed(Sec)) |
| 1025 | return Error::success(); |
| 1026 | |
| 1027 | Expected<Decompressor> Decompressor = |
| 1028 | Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8); |
| 1029 | if (!Decompressor) |
| 1030 | return Decompressor.takeError(); |
| 1031 | |
| 1032 | SmallString<32> Out; |
| 1033 | if (auto Err = Decompressor->resizeAndDecompress(Out)) |
| 1034 | return Err; |
| 1035 | |
| 1036 | UncompressedSections.emplace_back(std::move(Out)); |
| 1037 | Data = UncompressedSections.back(); |
| 1038 | |
| 1039 | return Error::success(); |
| 1040 | } |
| 1041 | |
| 1042 | public: |
| 1043 | DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 1044 | uint8_t AddrSize, bool IsLittleEndian) |
| 1045 | : IsLittleEndian(IsLittleEndian) { |
| 1046 | for (const auto &SecIt : Sections) { |
| 1047 | if (StringRef *SectionData = mapSectionToMember(SecIt.first())) |
| 1048 | *SectionData = SecIt.second->getBuffer(); |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 1049 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1050 | } |
| 1051 | DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L, |
| 1052 | function_ref<ErrorPolicy(Error)> HandleError) |
| 1053 | : IsLittleEndian(Obj.isLittleEndian()), |
| 1054 | AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()) { |
Alexey Samsonov | 068fc8a | 2013-04-23 10:17:34 +0000 | [diff] [blame] | 1055 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1056 | for (const SectionRef &Section : Obj.sections()) { |
| 1057 | StringRef Name; |
| 1058 | Section.getName(Name); |
| 1059 | // Skip BSS and Virtual sections, they aren't interesting. |
| 1060 | if (Section.isBSS() || Section.isVirtual()) |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1061 | continue; |
Keno Fischer | c780e8e | 2015-05-21 21:24:32 +0000 | [diff] [blame] | 1062 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1063 | StringRef Data; |
| 1064 | section_iterator RelocatedSection = Section.getRelocatedSection(); |
| 1065 | // Try to obtain an already relocated version of this section. |
| 1066 | // Else use the unrelocated section from the object file. We'll have to |
| 1067 | // apply relocations ourselves later. |
| 1068 | if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) |
| 1069 | Section.getContents(Data); |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1070 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1071 | if (auto Err = maybeDecompress(Section, Name, Data)) { |
| 1072 | ErrorPolicy EP = HandleError(createError( |
| 1073 | "failed to decompress '" + Name + "', ", std::move(Err))); |
George Rimar | 1af3cb2 | 2017-06-28 08:21:19 +0000 | [diff] [blame] | 1074 | if (EP == ErrorPolicy::Halt) |
| 1075 | return; |
George Rimar | fed9f09 | 2017-05-17 12:10:51 +0000 | [diff] [blame] | 1076 | continue; |
| 1077 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1078 | |
| 1079 | // Compressed sections names in GNU style starts from ".z", |
| 1080 | // at this point section is decompressed and we drop compression prefix. |
| 1081 | Name = Name.substr( |
| 1082 | Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes. |
| 1083 | |
| 1084 | // Map platform specific debug section names to DWARF standard section |
| 1085 | // names. |
| 1086 | Name = Obj.mapDebugSectionName(Name); |
| 1087 | |
| 1088 | if (StringRef *SectionData = mapSectionToMember(Name)) { |
| 1089 | *SectionData = Data; |
| 1090 | if (Name == "debug_ranges") { |
| 1091 | // FIXME: Use the other dwo range section when we emit it. |
| 1092 | RangeDWOSection.Data = Data; |
| 1093 | } |
| 1094 | } else if (Name == "debug_types") { |
| 1095 | // Find debug_types data by section rather than name as there are |
| 1096 | // multiple, comdat grouped, debug_types sections. |
| 1097 | TypesSections[Section].Data = Data; |
| 1098 | } else if (Name == "debug_types.dwo") { |
| 1099 | TypesDWOSections[Section].Data = Data; |
| 1100 | } |
| 1101 | |
| 1102 | if (RelocatedSection == Obj.section_end()) |
| 1103 | continue; |
| 1104 | |
| 1105 | StringRef RelSecName; |
| 1106 | StringRef RelSecData; |
| 1107 | RelocatedSection->getName(RelSecName); |
| 1108 | |
| 1109 | // If the section we're relocating was relocated already by the JIT, |
| 1110 | // then we used the relocated version above, so we do not need to process |
| 1111 | // relocations for it now. |
| 1112 | if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) |
| 1113 | continue; |
| 1114 | |
| 1115 | // In Mach-o files, the relocations do not need to be applied if |
| 1116 | // there is no load offset to apply. The value read at the |
| 1117 | // relocation point already factors in the section address |
| 1118 | // (actually applying the relocations will produce wrong results |
| 1119 | // as the section address will be added twice). |
| 1120 | if (!L && isa<MachOObjectFile>(&Obj)) |
| 1121 | continue; |
| 1122 | |
| 1123 | RelSecName = RelSecName.substr( |
| 1124 | RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes. |
| 1125 | |
| 1126 | // TODO: Add support for relocations in other sections as needed. |
| 1127 | // Record relocations for the debug_info and debug_line sections. |
| 1128 | DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName); |
| 1129 | RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr; |
| 1130 | if (!Map) { |
| 1131 | // Find debug_types relocs by section rather than name as there are |
| 1132 | // multiple, comdat grouped, debug_types sections. |
| 1133 | if (RelSecName == "debug_types") |
| 1134 | Map = |
| 1135 | &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection]) |
| 1136 | .Relocs; |
| 1137 | else if (RelSecName == "debug_types.dwo") |
| 1138 | Map = &static_cast<DWARFSectionMap &>( |
| 1139 | TypesDWOSections[*RelocatedSection]) |
| 1140 | .Relocs; |
| 1141 | else |
| 1142 | continue; |
| 1143 | } |
| 1144 | |
| 1145 | if (Section.relocation_begin() == Section.relocation_end()) |
| 1146 | continue; |
| 1147 | |
| 1148 | // Symbol to [address, section index] cache mapping. |
| 1149 | std::map<SymbolRef, SymInfo> AddrCache; |
| 1150 | for (const RelocationRef &Reloc : Section.relocations()) { |
| 1151 | // FIXME: it's not clear how to correctly handle scattered |
| 1152 | // relocations. |
| 1153 | if (isRelocScattered(Obj, Reloc)) |
| 1154 | continue; |
| 1155 | |
| 1156 | Expected<SymInfo> SymInfoOrErr = |
| 1157 | getSymbolInfo(Obj, Reloc, L, AddrCache); |
| 1158 | if (!SymInfoOrErr) { |
| 1159 | if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt) |
| 1160 | return; |
| 1161 | continue; |
| 1162 | } |
| 1163 | |
| 1164 | object::RelocVisitor V(Obj); |
| 1165 | uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address); |
| 1166 | if (V.error()) { |
| 1167 | SmallString<32> Type; |
| 1168 | Reloc.getTypeName(Type); |
| 1169 | ErrorPolicy EP = HandleError( |
| 1170 | createError("failed to compute relocation: " + Type + ", ", |
| 1171 | errorCodeToError(object_error::parse_failed))); |
| 1172 | if (EP == ErrorPolicy::Halt) |
| 1173 | return; |
| 1174 | continue; |
| 1175 | } |
| 1176 | RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val}; |
| 1177 | Map->insert({Reloc.getOffset(), Rel}); |
| 1178 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1181 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1182 | Optional<RelocAddrEntry> find(const DWARFSection &S, |
| 1183 | uint64_t Pos) const override { |
| 1184 | auto &Sec = static_cast<const DWARFSectionMap &>(S); |
| 1185 | RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos); |
| 1186 | if (AI == Sec.Relocs.end()) |
| 1187 | return None; |
| 1188 | return AI->second; |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 1189 | } |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1190 | |
| 1191 | bool isLittleEndian() const override { return IsLittleEndian; } |
| 1192 | StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; } |
| 1193 | const DWARFSection &getLineDWOSection() const override { |
| 1194 | return LineDWOSection; |
| 1195 | } |
| 1196 | const DWARFSection &getLocDWOSection() const override { |
| 1197 | return LocDWOSection; |
| 1198 | } |
| 1199 | StringRef getStringDWOSection() const override { return StringDWOSection; } |
| 1200 | const DWARFSection &getStringOffsetDWOSection() const override { |
| 1201 | return StringOffsetDWOSection; |
| 1202 | } |
| 1203 | const DWARFSection &getRangeDWOSection() const override { |
| 1204 | return RangeDWOSection; |
| 1205 | } |
| 1206 | const DWARFSection &getAddrSection() const override { return AddrSection; } |
| 1207 | StringRef getCUIndexSection() const override { return CUIndexSection; } |
| 1208 | StringRef getGdbIndexSection() const override { return GdbIndexSection; } |
| 1209 | StringRef getTUIndexSection() const override { return TUIndexSection; } |
| 1210 | |
| 1211 | // DWARF v5 |
| 1212 | const DWARFSection &getStringOffsetSection() const override { |
| 1213 | return StringOffsetSection; |
| 1214 | } |
| 1215 | |
| 1216 | // Sections for DWARF5 split dwarf proposal. |
| 1217 | const DWARFSection &getInfoDWOSection() const override { |
| 1218 | return InfoDWOSection; |
| 1219 | } |
| 1220 | void forEachTypesDWOSections( |
| 1221 | function_ref<void(const DWARFSection &)> F) const override { |
| 1222 | for (auto &P : TypesDWOSections) |
| 1223 | F(P.second); |
| 1224 | } |
| 1225 | |
| 1226 | StringRef getAbbrevSection() const override { return AbbrevSection; } |
| 1227 | const DWARFSection &getLocSection() const override { return LocSection; } |
| 1228 | StringRef getARangeSection() const override { return ARangeSection; } |
| 1229 | StringRef getDebugFrameSection() const override { return DebugFrameSection; } |
| 1230 | StringRef getEHFrameSection() const override { return EHFrameSection; } |
| 1231 | const DWARFSection &getLineSection() const override { return LineSection; } |
| 1232 | StringRef getStringSection() const override { return StringSection; } |
| 1233 | const DWARFSection &getRangeSection() const override { return RangeSection; } |
| 1234 | StringRef getMacinfoSection() const override { return MacinfoSection; } |
| 1235 | StringRef getPubNamesSection() const override { return PubNamesSection; } |
| 1236 | StringRef getPubTypesSection() const override { return PubTypesSection; } |
| 1237 | StringRef getGnuPubNamesSection() const override { |
| 1238 | return GnuPubNamesSection; |
| 1239 | } |
| 1240 | StringRef getGnuPubTypesSection() const override { |
| 1241 | return GnuPubTypesSection; |
| 1242 | } |
| 1243 | const DWARFSection &getAppleNamesSection() const override { |
| 1244 | return AppleNamesSection; |
| 1245 | } |
| 1246 | const DWARFSection &getAppleTypesSection() const override { |
| 1247 | return AppleTypesSection; |
| 1248 | } |
| 1249 | const DWARFSection &getAppleNamespacesSection() const override { |
| 1250 | return AppleNamespacesSection; |
| 1251 | } |
| 1252 | const DWARFSection &getAppleObjCSection() const override { |
| 1253 | return AppleObjCSection; |
| 1254 | } |
| 1255 | |
| 1256 | StringRef getFileName() const override { return FileName; } |
| 1257 | uint8_t getAddressSize() const override { return AddressSize; } |
| 1258 | const DWARFSection &getInfoSection() const override { return InfoSection; } |
| 1259 | void forEachTypesSections( |
| 1260 | function_ref<void(const DWARFSection &)> F) const override { |
| 1261 | for (auto &P : TypesSections) |
| 1262 | F(P.second); |
| 1263 | } |
| 1264 | }; |
| 1265 | |
| 1266 | std::unique_ptr<DWARFContext> |
| 1267 | DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L, |
| 1268 | function_ref<ErrorPolicy(Error)> HandleError) { |
| 1269 | auto DObj = make_unique<DWARFObjInMemory>(Obj, L, HandleError); |
| 1270 | return make_unique<DWARFContext>(std::move(DObj)); |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame^] | 1273 | std::unique_ptr<DWARFContext> |
| 1274 | DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, |
| 1275 | uint8_t AddrSize, bool isLittleEndian) { |
| 1276 | auto DObj = make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian); |
| 1277 | return make_unique<DWARFContext>(std::move(DObj)); |
Rafael Espindola | 77e87b3 | 2017-07-07 05:36:53 +0000 | [diff] [blame] | 1278 | } |