Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===// |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +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 | // |
| 10 | // This file contains support for writing dwarf debug info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | b14490d | 2010-03-09 00:39:24 +0000 | [diff] [blame] | 13 | |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 14 | #include "DwarfDebug.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 15 | #include "ByteStreamer.h" |
Eric Christopher | 4573198 | 2013-08-08 23:45:55 +0000 | [diff] [blame] | 16 | #include "DIEHash.h" |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 17 | #include "DebugLocEntry.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 18 | #include "DebugLocStream.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 19 | #include "DwarfCompileUnit.h" |
| 20 | #include "DwarfExpression.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 21 | #include "DwarfFile.h" |
David Blaikie | 2c86a72 | 2013-12-02 19:33:15 +0000 | [diff] [blame] | 22 | #include "DwarfUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
| 25 | #include "llvm/ADT/DenseSet.h" |
| 26 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Triple.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 32 | #include "llvm/BinaryFormat/Dwarf.h" |
Jonas Devlieghere | 855fc3b | 2018-01-29 14:52:41 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/AccelTable.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/AsmPrinter.h" |
Frederic Riss | e541e0b | 2015-01-05 21:29:41 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/DIE.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/LexicalScopes.h" |
| 37 | #include "llvm/CodeGen/MachineBasicBlock.h" |
David Greene | 829b3e8 | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineInstr.h" |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 43 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 45 | #include "llvm/IR/DebugInfoMetadata.h" |
| 46 | #include "llvm/IR/DebugLoc.h" |
| 47 | #include "llvm/IR/Function.h" |
| 48 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Module.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 50 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 51 | #include "llvm/MC/MCContext.h" |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 52 | #include "llvm/MC/MCDwarf.h" |
Chris Lattner | 4d2c0f9 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 53 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 4b7dadb | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 54 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | e13c372 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 55 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 56 | #include "llvm/MC/MCTargetOptions.h" |
| 57 | #include "llvm/MC/MachineLocation.h" |
| 58 | #include "llvm/MC/SectionKind.h" |
| 59 | #include "llvm/Pass.h" |
| 60 | #include "llvm/Support/Casting.h" |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 61 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | cdf01b5 | 2009-10-13 06:47:08 +0000 | [diff] [blame] | 62 | #include "llvm/Support/Debug.h" |
| 63 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 6764643 | 2013-07-26 17:02:41 +0000 | [diff] [blame] | 64 | #include "llvm/Support/MD5.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 65 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 66 | #include "llvm/Support/Timer.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 67 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame^] | 68 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 69 | #include "llvm/Target/TargetMachine.h" |
| 70 | #include "llvm/Target/TargetOptions.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 71 | #include <algorithm> |
| 72 | #include <cassert> |
| 73 | #include <cstddef> |
| 74 | #include <cstdint> |
| 75 | #include <iterator> |
| 76 | #include <string> |
| 77 | #include <utility> |
| 78 | #include <vector> |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 79 | |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 80 | using namespace llvm; |
| 81 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 82 | #define DEBUG_TYPE "dwarfdebug" |
| 83 | |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 84 | static cl::opt<bool> |
| 85 | DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, |
| 86 | cl::desc("Disable debug info printing")); |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 87 | |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 88 | static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier( |
| 89 | "use-dwarf-ranges-base-address-specifier", cl::Hidden, |
David Blaikie | c2be863 | 2017-08-01 14:50:50 +0000 | [diff] [blame] | 90 | cl::desc("Use base address specifiers in debug_ranges"), cl::init(false)); |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 91 | |
Eric Christopher | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 92 | static cl::opt<bool> GenerateARangeSection("generate-arange-section", |
| 93 | cl::Hidden, |
| 94 | cl::desc("Generate dwarf aranges"), |
| 95 | cl::init(false)); |
| 96 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 97 | static cl::opt<bool> SplitDwarfCrossCuReferences( |
| 98 | "split-dwarf-cross-cu-references", cl::Hidden, |
| 99 | cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); |
| 100 | |
Eric Christopher | f07ee3a | 2014-01-27 23:50:03 +0000 | [diff] [blame] | 101 | enum DefaultOnOff { Default, Enable, Disable }; |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 102 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 103 | static cl::opt<DefaultOnOff> UnknownLocations( |
| 104 | "use-unknown-locations", cl::Hidden, |
| 105 | cl::desc("Make an absence of debug location information explicit."), |
| 106 | cl::values(clEnumVal(Default, "At top of block or after label"), |
| 107 | clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), |
| 108 | cl::init(Default)); |
| 109 | |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 110 | static cl::opt<DefaultOnOff> |
| 111 | DwarfAccelTables("dwarf-accel-tables", cl::Hidden, |
| 112 | cl::desc("Output prototype dwarf accelerator tables."), |
| 113 | cl::values(clEnumVal(Default, "Default for platform"), |
| 114 | clEnumVal(Enable, "Enabled"), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 115 | clEnumVal(Disable, "Disabled")), |
Eric Christopher | 7f2b551 | 2013-07-23 22:16:41 +0000 | [diff] [blame] | 116 | cl::init(Default)); |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 117 | |
Alexey Bataev | 0d6aead | 2018-02-20 15:28:08 +0000 | [diff] [blame] | 118 | static cl::opt<DefaultOnOff> |
| 119 | DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, |
| 120 | cl::desc("Use inlined strings rather than string section."), |
| 121 | cl::values(clEnumVal(Default, "Default for platform"), |
| 122 | clEnumVal(Enable, "Enabled"), |
| 123 | clEnumVal(Disable, "Disabled")), |
| 124 | cl::init(Default)); |
| 125 | |
Alexey Bataev | 648ed2d | 2018-03-20 16:04:40 +0000 | [diff] [blame] | 126 | static cl::opt<bool> |
| 127 | NoDwarfPubSections("no-dwarf-pub-sections", cl::Hidden, |
| 128 | cl::desc("Disable emission of DWARF pub sections."), |
| 129 | cl::init(false)); |
| 130 | |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 131 | static cl::opt<bool> |
| 132 | NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, |
| 133 | cl::desc("Disable emission .debug_ranges section."), |
| 134 | cl::init(false)); |
| 135 | |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 136 | static cl::opt<DefaultOnOff> DwarfSectionsAsReferences( |
| 137 | "dwarf-sections-as-references", cl::Hidden, |
| 138 | cl::desc("Use sections+offset as references rather than labels."), |
| 139 | cl::values(clEnumVal(Default, "Default for platform"), |
| 140 | clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), |
| 141 | cl::init(Default)); |
| 142 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 143 | enum LinkageNameOption { |
| 144 | DefaultLinkageNames, |
| 145 | AllLinkageNames, |
| 146 | AbstractLinkageNames |
| 147 | }; |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 148 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 149 | static cl::opt<LinkageNameOption> |
| 150 | DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, |
| 151 | cl::desc("Which DWARF linkage-name attributes to emit."), |
| 152 | cl::values(clEnumValN(DefaultLinkageNames, "Default", |
| 153 | "Default for platform"), |
| 154 | clEnumValN(AllLinkageNames, "All", "All"), |
| 155 | clEnumValN(AbstractLinkageNames, "Abstract", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 156 | "Abstract subprograms")), |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 157 | cl::init(DefaultLinkageNames)); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 158 | |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 159 | static const char *const DWARFGroupName = "dwarf"; |
| 160 | static const char *const DWARFGroupDescription = "DWARF Emission"; |
| 161 | static const char *const DbgTimerName = "writer"; |
| 162 | static const char *const DbgTimerDescription = "DWARF Debug Writer"; |
Bill Wendling | fcc1414 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 163 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 164 | void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 165 | BS.EmitInt8( |
| 166 | Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) |
| 167 | : dwarf::OperationEncodingString(Op)); |
| 168 | } |
| 169 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 170 | void DebugLocDwarfExpression::emitSigned(int64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 171 | BS.EmitSLEB128(Value, Twine(Value)); |
| 172 | } |
| 173 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 174 | void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 175 | BS.EmitULEB128(Value, Twine(Value)); |
| 176 | } |
| 177 | |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 178 | bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, |
| 179 | unsigned MachineReg) { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 180 | // This information is not available while emitting .debug_loc entries. |
| 181 | return false; |
| 182 | } |
| 183 | |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 184 | bool DbgVariable::isBlockByrefVariable() const { |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 185 | assert(Var && "Invalid complex DbgVariable!"); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 186 | return Var->getType().resolve()->isBlockByrefStruct(); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 189 | const DIType *DbgVariable::getType() const { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 190 | DIType *Ty = Var->getType().resolve(); |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 191 | // FIXME: isBlockByrefVariable should be reformulated in terms of complex |
| 192 | // addresses instead. |
Duncan P. N. Exon Smith | 571baeb | 2015-04-14 01:59:58 +0000 | [diff] [blame] | 193 | if (Ty->isBlockByrefStruct()) { |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 194 | /* Byref variables, in Blocks, are declared by the programmer as |
| 195 | "SomeType VarName;", but the compiler creates a |
| 196 | __Block_byref_x_VarName struct, and gives the variable VarName |
| 197 | either the struct, or a pointer to the struct, as its type. This |
| 198 | is necessary for various behind-the-scenes things the compiler |
| 199 | needs to do with by-reference variables in blocks. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 200 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 201 | However, as far as the original *programmer* is concerned, the |
| 202 | variable should still have type 'SomeType', as originally declared. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 203 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 204 | The following function dives into the __Block_byref_x_VarName |
| 205 | struct to find the original type of the variable. This will be |
| 206 | passed back to the code generating the type for the Debug |
| 207 | Information Entry for the variable 'VarName'. 'VarName' will then |
| 208 | have the original type 'SomeType' in its debug information. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 209 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 210 | The original type 'SomeType' will be the type of the field named |
| 211 | 'VarName' inside the __Block_byref_x_VarName struct. |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 212 | |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 213 | NOTE: In order for this to not completely fail on the debugger |
| 214 | side, the Debug Information Entry for the variable VarName needs to |
| 215 | have a DW_AT_location that tells the debugger how to unwind through |
| 216 | the pointers and __Block_byref_x_VarName struct to find the actual |
| 217 | value of the variable. The function addBlockByrefType does this. */ |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 218 | DIType *subType = Ty; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 219 | uint16_t tag = Ty->getTag(); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 220 | |
Eric Christopher | 9adc55f | 2013-09-04 19:53:21 +0000 | [diff] [blame] | 221 | if (tag == dwarf::DW_TAG_pointer_type) |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 222 | subType = resolve(cast<DIDerivedType>(Ty)->getBaseType()); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 223 | |
Duncan P. N. Exon Smith | 6ac940d | 2015-07-24 18:17:17 +0000 | [diff] [blame] | 224 | auto Elements = cast<DICompositeType>(subType)->getElements(); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 225 | for (unsigned i = 0, N = Elements.size(); i < N; ++i) { |
Duncan P. N. Exon Smith | 099ea1c | 2015-07-24 18:58:32 +0000 | [diff] [blame] | 226 | auto *DT = cast<DIDerivedType>(Elements[i]); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 227 | if (getName() == DT->getName()) |
Duncan P. N. Exon Smith | 848af38 | 2015-04-20 18:20:03 +0000 | [diff] [blame] | 228 | return resolve(DT->getBaseType()); |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 229 | } |
Devang Patel | 6d9f9fe | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 230 | } |
Devang Patel | f20c4f7 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 231 | return Ty; |
| 232 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 233 | |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 234 | ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 235 | if (FrameIndexExprs.size() == 1) |
| 236 | return FrameIndexExprs; |
| 237 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 238 | assert(llvm::all_of(FrameIndexExprs, |
| 239 | [](const FrameIndexExpr &A) { |
| 240 | return A.Expr->isFragment(); |
| 241 | }) && |
Adrian Prantl | 5503077 | 2017-03-22 16:50:16 +0000 | [diff] [blame] | 242 | "multiple FI expressions without DW_OP_LLVM_fragment"); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 243 | std::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(), |
| 244 | [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { |
| 245 | return A.Expr->getFragmentInfo()->OffsetInBits < |
| 246 | B.Expr->getFragmentInfo()->OffsetInBits; |
| 247 | }); |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 248 | |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 249 | return FrameIndexExprs; |
| 250 | } |
| 251 | |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 252 | void DbgVariable::addMMIEntry(const DbgVariable &V) { |
| 253 | assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry"); |
| 254 | assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry"); |
| 255 | assert(V.Var == Var && "conflicting variable"); |
| 256 | assert(V.IA == IA && "conflicting inlined-at location"); |
| 257 | |
| 258 | assert(!FrameIndexExprs.empty() && "Expected an MMI entry"); |
| 259 | assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry"); |
| 260 | |
Daniel Jasper | 4d93120 | 2017-10-12 13:25:05 +0000 | [diff] [blame] | 261 | // FIXME: This logic should not be necessary anymore, as we now have proper |
| 262 | // deduplication. However, without it, we currently run into the assertion |
| 263 | // below, which means that we are likely dealing with broken input, i.e. two |
| 264 | // non-fragment entries for the same variable at different frame indices. |
| 265 | if (FrameIndexExprs.size()) { |
| 266 | auto *Expr = FrameIndexExprs.back().Expr; |
| 267 | if (!Expr || !Expr->isFragment()) |
| 268 | return; |
| 269 | } |
| 270 | |
Bjorn Steinbrink | d36bbe9 | 2017-10-10 07:46:17 +0000 | [diff] [blame] | 271 | for (const auto &FIE : V.FrameIndexExprs) |
| 272 | // Ignore duplicate entries. |
| 273 | if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) { |
| 274 | return FIE.FI == Other.FI && FIE.Expr == Other.Expr; |
| 275 | })) |
| 276 | FrameIndexExprs.push_back(FIE); |
| 277 | |
| 278 | assert((FrameIndexExprs.size() == 1 || |
| 279 | llvm::all_of(FrameIndexExprs, |
| 280 | [](FrameIndexExpr &FIE) { |
| 281 | return FIE.Expr && FIE.Expr->isFragment(); |
| 282 | })) && |
| 283 | "conflicting locations for variable"); |
| 284 | } |
| 285 | |
Chris Lattner | f0d6bd3 | 2010-04-05 05:11:15 +0000 | [diff] [blame] | 286 | DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 287 | : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), |
| 288 | InfoHolder(A, "info_string", DIEValueAllocator), |
Frederic Riss | 9412d63 | 2015-03-04 02:30:17 +0000 | [diff] [blame] | 289 | SkeletonHolder(A, "skel_string", DIEValueAllocator), |
Pavel Labath | a7c457d | 2018-02-19 16:12:20 +0000 | [diff] [blame] | 290 | IsDarwin(A->TM.getTargetTriple().isOSDarwin()) { |
Eric Christopher | 364dbe0 | 2016-10-01 01:50:29 +0000 | [diff] [blame] | 291 | const Triple &TT = Asm->TM.getTargetTriple(); |
Eric Christopher | ad9fe89 | 2012-04-02 17:58:52 +0000 | [diff] [blame] | 292 | |
Paul Robinson | 6c27a2c | 2015-12-16 19:58:30 +0000 | [diff] [blame] | 293 | // Make sure we know our "debugger tuning." The target option takes |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 294 | // precedence; fall back to triple-based defaults. |
Paul Robinson | 6c27a2c | 2015-12-16 19:58:30 +0000 | [diff] [blame] | 295 | if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) |
| 296 | DebuggerTuning = Asm->TM.Options.DebuggerTuning; |
Dimitry Andric | 2c36421 | 2016-01-07 22:09:12 +0000 | [diff] [blame] | 297 | else if (IsDarwin) |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 298 | DebuggerTuning = DebuggerKind::LLDB; |
| 299 | else if (TT.isPS4CPU()) |
| 300 | DebuggerTuning = DebuggerKind::SCE; |
| 301 | else |
| 302 | DebuggerTuning = DebuggerKind::GDB; |
| 303 | |
Pavel Labath | 67530e4 | 2018-01-17 11:52:13 +0000 | [diff] [blame] | 304 | // Turn on accelerator tables by default, if tuning for LLDB and the target is |
| 305 | // supported. |
Eric Christopher | 574b5c8 | 2013-08-19 21:41:38 +0000 | [diff] [blame] | 306 | if (DwarfAccelTables == Default) |
Pavel Labath | 67530e4 | 2018-01-17 11:52:13 +0000 | [diff] [blame] | 307 | HasDwarfAccelTables = |
| 308 | tuneForLLDB() && A->TM.getTargetTriple().isOSBinFormatMachO(); |
Eric Christopher | 574b5c8 | 2013-08-19 21:41:38 +0000 | [diff] [blame] | 309 | else |
Eric Christopher | 5297df0 | 2013-08-26 20:58:35 +0000 | [diff] [blame] | 310 | HasDwarfAccelTables = DwarfAccelTables == Enable; |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 311 | |
Alexey Bataev | 0d6aead | 2018-02-20 15:28:08 +0000 | [diff] [blame] | 312 | UseInlineStrings = DwarfInlinedStrings == Enable; |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 313 | HasAppleExtensionAttributes = tuneForLLDB(); |
| 314 | |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 315 | // Handle split DWARF. |
| 316 | HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); |
Eric Christopher | 2942431 | 2012-11-12 22:22:20 +0000 | [diff] [blame] | 317 | |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 318 | // SCE defaults to linkage names only for abstract subprograms. |
| 319 | if (DwarfLinkageNames == DefaultLinkageNames) |
| 320 | UseAllLinkageNames = !tuneForSCE(); |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 321 | else |
Paul Robinson | 43d1e45 | 2016-04-18 22:41:41 +0000 | [diff] [blame] | 322 | UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; |
Paul Robinson | 78046b4 | 2015-08-11 21:36:45 +0000 | [diff] [blame] | 323 | |
Eric Christopher | 4c5bff3 | 2014-06-19 06:22:08 +0000 | [diff] [blame] | 324 | unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 325 | unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber |
Eric Christopher | 793c747 | 2014-04-28 20:42:22 +0000 | [diff] [blame] | 326 | : MMI->getModule()->getDwarfVersion(); |
Reid Kleckner | 12d2c12 | 2015-08-05 22:26:20 +0000 | [diff] [blame] | 327 | // Use dwarf 4 by default if nothing is requested. |
| 328 | DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION; |
Manman Ren | ac8062b | 2013-07-02 23:40:10 +0000 | [diff] [blame] | 329 | |
Alexey Bataev | 648ed2d | 2018-03-20 16:04:40 +0000 | [diff] [blame] | 330 | UsePubSections = !NoDwarfPubSections; |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 331 | UseRangesSection = !NoDwarfRangesSection; |
Alexey Bataev | 648ed2d | 2018-03-20 16:04:40 +0000 | [diff] [blame] | 332 | |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 333 | // Use sections as references. |
| 334 | UseSectionsAsReferences = DwarfSectionsAsReferences == Enable; |
| 335 | |
Paul Robinson | b9de106 | 2015-07-15 22:04:54 +0000 | [diff] [blame] | 336 | // Work around a GDB bug. GDB doesn't support the standard opcode; |
| 337 | // SCE doesn't support GNU's; LLDB prefers the standard opcode, which |
| 338 | // is defined as of DWARF 3. |
| 339 | // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented |
| 340 | // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 |
| 341 | UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; |
Paul Robinson | 78cc082 | 2015-03-04 20:55:11 +0000 | [diff] [blame] | 342 | |
Adrian Prantl | 6323ddf | 2016-05-17 21:07:16 +0000 | [diff] [blame] | 343 | // GDB does not fully support the DWARF 4 representation for bitfields. |
| 344 | UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); |
| 345 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 346 | // The DWARF v5 string offsets table has - possibly shared - contributions |
| 347 | // from each compile and type unit each preceded by a header. The string |
| 348 | // offsets table used by the pre-DWARF v5 split-DWARF implementation uses |
| 349 | // a monolithic string offsets table without any header. |
| 350 | UseSegmentedStringOffsetsTable = DwarfVersion >= 5; |
| 351 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 352 | Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 353 | } |
Bill Wendling | 2f921f8 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 354 | |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 355 | // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 356 | DwarfDebug::~DwarfDebug() = default; |
Reid Kleckner | dd2647e | 2014-04-30 20:34:31 +0000 | [diff] [blame] | 357 | |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 358 | static bool isObjCClass(StringRef Name) { |
| 359 | return Name.startswith("+") || Name.startswith("-"); |
| 360 | } |
| 361 | |
| 362 | static bool hasObjCCategory(StringRef Name) { |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 363 | if (!isObjCClass(Name)) |
| 364 | return false; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 365 | |
Benjamin Kramer | 260de74 | 2013-08-24 12:15:54 +0000 | [diff] [blame] | 366 | return Name.find(") ") != StringRef::npos; |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void getObjCClassCategory(StringRef In, StringRef &Class, |
| 370 | StringRef &Category) { |
| 371 | if (!hasObjCCategory(In)) { |
| 372 | Class = In.slice(In.find('[') + 1, In.find(' ')); |
| 373 | Category = ""; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | Class = In.slice(In.find('[') + 1, In.find('(')); |
| 378 | Category = In.slice(In.find('[') + 1, In.find(' ')); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static StringRef getObjCMethodName(StringRef In) { |
| 382 | return In.slice(In.find(' ') + 1, In.find(']')); |
| 383 | } |
| 384 | |
| 385 | // Add the various names to the Dwarf accelerator table names. |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 386 | // TODO: Determine whether or not we should add names for programs |
| 387 | // that do not have a DW_AT_name or DW_AT_linkage_name field - this |
| 388 | // is only slightly different than the lookup of non-standard ObjC names. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 389 | void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 390 | if (!SP->isDefinition()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 391 | return; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 392 | addAccelName(SP->getName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 393 | |
| 394 | // If the linkage name is different than the name, go ahead and output |
| 395 | // that as well into the name table. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 396 | if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName()) |
| 397 | addAccelName(SP->getLinkageName(), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 398 | |
| 399 | // If this is an Objective-C selector name add it to the ObjC accelerator |
| 400 | // too. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 401 | if (isObjCClass(SP->getName())) { |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 402 | StringRef Class, Category; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 403 | getObjCClassCategory(SP->getName(), Class, Category); |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 404 | addAccelObjC(Class, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 405 | if (Category != "") |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 406 | addAccelObjC(Category, Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 407 | // Also add the base method name to the name table. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 408 | addAccelName(getObjCMethodName(SP->getName()), Die); |
Eric Christopher | d9843b3 | 2011-11-10 19:25:34 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 412 | /// Check whether we should create a DIE for the given Scope, return true |
| 413 | /// if we don't create a DIE (the corresponding DIE is null). |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 414 | bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { |
| 415 | if (Scope->isAbstractScope()) |
| 416 | return false; |
| 417 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 418 | // We don't create a DIE if there is no Range. |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 419 | const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); |
| 420 | if (Ranges.empty()) |
| 421 | return true; |
| 422 | |
| 423 | if (Ranges.size() > 1) |
| 424 | return false; |
| 425 | |
Manman Ren | 5b2f4b0 | 2013-09-11 19:40:28 +0000 | [diff] [blame] | 426 | // We don't create a DIE if we have a single Range and the end label |
| 427 | // is null. |
David Blaikie | cd4b8a2 | 2014-08-31 02:14:26 +0000 | [diff] [blame] | 428 | return !getLabelAfterInsn(Ranges.front().second); |
Manman Ren | 2312ed3 | 2013-09-10 18:40:41 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 431 | template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 432 | F(CU); |
| 433 | if (auto *SkelCU = CU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 434 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 435 | F(*SkelCU); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 436 | } |
| 437 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 438 | bool DwarfDebug::shareAcrossDWOCUs() const { |
| 439 | return SplitDwarfCrossCuReferences; |
| 440 | } |
| 441 | |
| 442 | void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, |
| 443 | LexicalScope *Scope) { |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 444 | assert(Scope && Scope->getScopeNode()); |
| 445 | assert(Scope->isAbstractScope()); |
| 446 | assert(!Scope->getInlinedAt()); |
| 447 | |
David Blaikie | b461468 | 2016-12-14 19:38:39 +0000 | [diff] [blame] | 448 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 449 | |
David Blaikie | 1ea9db2 | 2014-05-21 23:14:12 +0000 | [diff] [blame] | 450 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 451 | // was inlined from another compile unit. |
Mehdi Amini | 4181205 | 2017-05-29 06:25:30 +0000 | [diff] [blame] | 452 | if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) |
| 453 | // Avoid building the original CU if it won't be used |
| 454 | SrcCU.constructAbstractSubprogramScopeDIE(Scope); |
| 455 | else { |
| 456 | auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
| 457 | if (auto *SkelCU = CU.getSkeleton()) { |
| 458 | (shareAcrossDWOCUs() ? CU : SrcCU) |
| 459 | .constructAbstractSubprogramScopeDIE(Scope); |
| 460 | if (CU.getCUNode()->getSplitDebugInlining()) |
| 461 | SkelCU->constructAbstractSubprogramScopeDIE(Scope); |
| 462 | } else |
| 463 | CU.constructAbstractSubprogramScopeDIE(Scope); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 464 | } |
David Blaikie | e872a6e | 2014-04-29 15:58:35 +0000 | [diff] [blame] | 465 | } |
| 466 | |
David Blaikie | b3cee2f | 2017-05-25 18:50:28 +0000 | [diff] [blame] | 467 | void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 468 | if (!U.hasDwarfPubSections()) |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 469 | return; |
| 470 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 471 | U.addFlag(D, dwarf::DW_AT_GNU_pubnames); |
David Blaikie | 3c84262 | 2013-12-04 21:31:26 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 474 | // Create new DwarfCompileUnit for the given metadata node with tag |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 475 | // DW_TAG_compile_unit. |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 476 | DwarfCompileUnit & |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 477 | DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { |
| 478 | if (auto *CU = CUMap.lookup(DIUnit)) |
| 479 | return *CU; |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 480 | StringRef FN = DIUnit->getFilename(); |
| 481 | CompilationDir = DIUnit->getDirectory(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 482 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 483 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
David Blaikie | bd57905 | 2014-04-28 21:14:27 +0000 | [diff] [blame] | 484 | InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 485 | DwarfCompileUnit &NewCU = *OwnedUnit; |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 486 | DIE &Die = NewCU.getUnitDie(); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 487 | InfoHolder.addUnit(std::move(OwnedUnit)); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 488 | if (useSplitDwarf()) { |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 489 | NewCU.setSkeleton(constructSkeletonCU(NewCU)); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 490 | NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 491 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
David Blaikie | 0b214e4 | 2016-03-24 18:37:08 +0000 | [diff] [blame] | 492 | } |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 493 | |
David Blaikie | 89daf77 | 2017-07-28 03:06:25 +0000 | [diff] [blame] | 494 | for (auto *IE : DIUnit->getImportedEntities()) |
| 495 | NewCU.addImportedEntity(IE); |
| 496 | |
David Blaikie | 7ac5149 | 2014-03-20 17:05:45 +0000 | [diff] [blame] | 497 | // LTO with assembly output shares a single line table amongst multiple CUs. |
| 498 | // To avoid the compilation directory being ambiguous, let the line table |
| 499 | // explicitly describe the directory of all files, never relying on the |
| 500 | // compilation directory. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 501 | if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) |
Alexander Kornienko | e12a48b | 2018-03-07 16:27:44 +0000 | [diff] [blame] | 502 | Asm->OutStreamer->getContext().setMCLineTableCompilationDir( |
| 503 | NewCU.getUniqueID(), CompilationDir); |
David Blaikie | 2666e24 | 2013-12-06 19:38:46 +0000 | [diff] [blame] | 504 | |
Eric Christopher | 9ea300f | 2017-03-29 23:34:27 +0000 | [diff] [blame] | 505 | StringRef Producer = DIUnit->getProducer(); |
| 506 | StringRef Flags = DIUnit->getFlags(); |
| 507 | if (!Flags.empty()) { |
| 508 | std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); |
| 509 | NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); |
| 510 | } else |
| 511 | NewCU.addString(Die, dwarf::DW_AT_producer, Producer); |
| 512 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 513 | NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 514 | DIUnit->getSourceLanguage()); |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 515 | NewCU.addString(Die, dwarf::DW_AT_name, FN); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 516 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 517 | // Add DW_str_offsets_base to the unit DIE, except for split units. |
| 518 | if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) |
| 519 | NewCU.addStringOffsetsStart(); |
| 520 | |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 521 | if (!useSplitDwarf()) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 522 | NewCU.initStmtList(); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 523 | |
| 524 | // If we're using split dwarf the compilation dir is going to be in the |
| 525 | // skeleton CU and so we don't need to duplicate it here. |
| 526 | if (!CompilationDir.empty()) |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 527 | NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
Eric Christopher | a51d3fc | 2013-09-27 22:50:48 +0000 | [diff] [blame] | 528 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 529 | addGnuPubAttributes(NewCU, Die); |
Eric Christopher | 52ce718 | 2013-04-09 19:23:15 +0000 | [diff] [blame] | 530 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 531 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 532 | if (useAppleExtensionAttributes()) { |
| 533 | if (DIUnit->isOptimized()) |
| 534 | NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 535 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 536 | StringRef Flags = DIUnit->getFlags(); |
| 537 | if (!Flags.empty()) |
| 538 | NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 539 | |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 540 | if (unsigned RVer = DIUnit->getRuntimeVersion()) |
| 541 | NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
| 542 | dwarf::DW_FORM_data1, RVer); |
| 543 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 544 | |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 545 | if (useSplitDwarf()) |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 546 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 547 | else |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 548 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
Eric Christopher | d039baa | 2013-12-30 03:40:32 +0000 | [diff] [blame] | 549 | |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 550 | if (DIUnit->getDWOId()) { |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 551 | // This CU is either a clang module DWO or a skeleton CU. |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 552 | NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, |
| 553 | DIUnit->getDWOId()); |
Adrian Prantl | 77fefeb | 2015-09-22 23:21:00 +0000 | [diff] [blame] | 554 | if (!DIUnit->getSplitDebugFilename().empty()) |
| 555 | // This is a prefabricated skeleton CU. |
| 556 | NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, |
| 557 | DIUnit->getSplitDebugFilename()); |
Adrian Prantl | deef90d | 2015-09-14 22:10:22 +0000 | [diff] [blame] | 558 | } |
| 559 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 560 | CUMap.insert({DIUnit, &NewCU}); |
| 561 | CUDieMap.insert({&Die, &NewCU}); |
Devang Patel | eb1bb4e | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 562 | return NewCU; |
Devang Patel | 1a0df9a | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 563 | } |
| 564 | |
David Blaikie | 8912df1 | 2014-08-31 05:41:15 +0000 | [diff] [blame] | 565 | void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 566 | const DIImportedEntity *N) { |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 567 | if (isa<DILocalScope>(N->getScope())) |
| 568 | return; |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 569 | if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) |
| 570 | D->addChild(TheCU.constructImportedEntityDIE(N)); |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 573 | /// Sort and unique GVEs by comparing their fragment offset. |
| 574 | static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & |
| 575 | sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { |
| 576 | std::sort(GVEs.begin(), GVEs.end(), |
| 577 | [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { |
Eli Friedman | 48e7549 | 2017-10-12 20:54:08 +0000 | [diff] [blame] | 578 | // Sort order: first null exprs, then exprs without fragment |
| 579 | // info, then sort by fragment offset in bits. |
| 580 | // FIXME: Come up with a more comprehensive comparator so |
| 581 | // the sorting isn't non-deterministic, and so the following |
| 582 | // std::unique call works correctly. |
| 583 | if (!A.Expr || !B.Expr) |
| 584 | return !!B.Expr; |
| 585 | auto FragmentA = A.Expr->getFragmentInfo(); |
| 586 | auto FragmentB = B.Expr->getFragmentInfo(); |
| 587 | if (!FragmentA || !FragmentB) |
| 588 | return !!FragmentB; |
| 589 | return FragmentA->OffsetInBits < FragmentB->OffsetInBits; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 590 | }); |
| 591 | GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), |
| 592 | [](DwarfCompileUnit::GlobalExpr A, |
| 593 | DwarfCompileUnit::GlobalExpr B) { |
| 594 | return A.Expr == B.Expr; |
| 595 | }), |
| 596 | GVEs.end()); |
| 597 | return GVEs; |
| 598 | } |
| 599 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 600 | // Emit all Dwarf sections that should come prior to the content. Create |
| 601 | // global DIEs and emit initial debug info sections. This is invoked by |
| 602 | // the target AsmPrinter. |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 603 | void DwarfDebug::beginModule() { |
Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 604 | NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, |
| 605 | DWARFGroupDescription, TimePassesIsEnabled); |
Devang Patel | 6c74a87 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 606 | if (DisableDebugInfoPrinting) |
| 607 | return; |
| 608 | |
Eric Christopher | 58f4195 | 2012-11-19 22:42:15 +0000 | [diff] [blame] | 609 | const Module *M = MMI->getModule(); |
| 610 | |
Peter Collingbourne | b326986 | 2016-06-01 02:58:40 +0000 | [diff] [blame] | 611 | unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), |
| 612 | M->debug_compile_units_end()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 613 | // Tell MMI whether we have debug info. |
| 614 | MMI->setDebugInfoAvailability(NumDebugCUs > 0); |
| 615 | SingleCU = NumDebugCUs == 1; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 616 | DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> |
| 617 | GVMap; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 618 | for (const GlobalVariable &Global : M->globals()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 619 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 620 | Global.getDebugInfo(GVs); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 621 | for (auto *GVE : GVs) |
| 622 | GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); |
Peter Collingbourne | d4135bb | 2016-09-13 01:12:59 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 625 | // Create the symbol that designates the start of the unit's contribution |
| 626 | // to the string offsets table. In a split DWARF scenario, only the skeleton |
| 627 | // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol). |
| 628 | if (useSegmentedStringOffsetsTable()) |
| 629 | (useSplitDwarf() ? SkeletonHolder : InfoHolder) |
| 630 | .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base")); |
| 631 | |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 632 | for (DICompileUnit *CUNode : M->debug_compile_units()) { |
David Blaikie | 89daf77 | 2017-07-28 03:06:25 +0000 | [diff] [blame] | 633 | // FIXME: Move local imported entities into a list attached to the |
| 634 | // subprogram, then this search won't be needed and a |
| 635 | // getImportedEntities().empty() test should go below with the rest. |
| 636 | bool HasNonLocalImportedEntities = llvm::any_of( |
| 637 | CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { |
| 638 | return !isa<DILocalScope>(IE->getScope()); |
| 639 | }); |
| 640 | |
| 641 | if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && |
| 642 | CUNode->getRetainedTypes().empty() && |
| 643 | CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 644 | continue; |
| 645 | |
| 646 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 647 | |
| 648 | // Global Variables. |
Adrian Prantl | 2116dd3 | 2017-08-19 01:15:06 +0000 | [diff] [blame] | 649 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 650 | // Don't bother adding DIGlobalVariableExpressions listed in the CU if we |
| 651 | // already know about the variable and it isn't adding a constant |
| 652 | // expression. |
| 653 | auto &GVMapEntry = GVMap[GVE->getVariable()]; |
| 654 | auto *Expr = GVE->getExpression(); |
| 655 | if (!GVMapEntry.size() || (Expr && Expr->isConstant())) |
| 656 | GVMapEntry.push_back({nullptr, Expr}); |
| 657 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 658 | DenseSet<DIGlobalVariable *> Processed; |
| 659 | for (auto *GVE : CUNode->getGlobalVariables()) { |
| 660 | DIGlobalVariable *GV = GVE->getVariable(); |
| 661 | if (Processed.insert(GV).second) |
| 662 | CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); |
| 663 | } |
| 664 | |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 665 | for (auto *Ty : CUNode->getEnumTypes()) { |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 666 | // The enum types array by design contains pointers to |
| 667 | // MDNodes rather than DIRefs. Unique them here. |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 668 | CU.getOrCreateTypeDIE(cast<DIType>(Ty)); |
Manman Ren | bd1628a | 2014-07-28 23:04:20 +0000 | [diff] [blame] | 669 | } |
Duncan P. N. Exon Smith | 9928a90 | 2015-04-20 18:52:06 +0000 | [diff] [blame] | 670 | for (auto *Ty : CUNode->getRetainedTypes()) { |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 671 | // The retained types array by design contains pointers to |
| 672 | // MDNodes rather than DIRefs. Unique them here. |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 673 | if (DIType *RT = dyn_cast<DIType>(Ty)) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 674 | // There is no point in force-emitting a forward declaration. |
| 675 | CU.getOrCreateTypeDIE(RT); |
Adrian Prantl | 0aa1aa2 | 2014-03-18 02:35:03 +0000 | [diff] [blame] | 676 | } |
David Blaikie | f55abea | 2013-04-22 06:12:31 +0000 | [diff] [blame] | 677 | // Emit imported_modules last so that the relevant context is already |
| 678 | // available. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 679 | for (auto *IE : CUNode->getImportedEntities()) |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 680 | constructAndAddImportedEntityDIE(CU, IE); |
David Blaikie | dc69ebb | 2013-03-11 23:39:23 +0000 | [diff] [blame] | 681 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 682 | } |
| 683 | |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 684 | void DwarfDebug::finishVariableDefinitions() { |
| 685 | for (const auto &Var : ConcreteVariables) { |
| 686 | DIE *VariableDie = Var->getDIE(); |
David Blaikie | f73ae4f | 2014-08-12 00:00:31 +0000 | [diff] [blame] | 687 | assert(VariableDie); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 688 | // FIXME: Consider the time-space tradeoff of just storing the unit pointer |
| 689 | // in the ConcreteVariables list, rather than looking it up again here. |
| 690 | // DIE::getUnit isn't simple - it walks parent pointers, etc. |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 691 | DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie()); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 692 | assert(Unit); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 693 | Unit->finishVariableDefinition(*Var); |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
David Blaikie | f7221ad | 2014-05-27 18:37:43 +0000 | [diff] [blame] | 697 | void DwarfDebug::finishSubprogramDefinitions() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 698 | for (const DISubprogram *SP : ProcessedSPNodes) { |
| 699 | assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); |
| 700 | forBothCUs( |
| 701 | getOrCreateDwarfCompileUnit(SP->getUnit()), |
| 702 | [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); |
| 703 | } |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void DwarfDebug::finalizeModuleInfo() { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 707 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 708 | |
David Blaikie | 3c2fff3 | 2014-05-27 18:37:48 +0000 | [diff] [blame] | 709 | finishSubprogramDefinitions(); |
| 710 | |
David Blaikie | eb1a272 | 2014-06-13 22:18:23 +0000 | [diff] [blame] | 711 | finishVariableDefinitions(); |
| 712 | |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 713 | // Include the DWO file name in the hash if there's more than one CU. |
| 714 | // This handles ThinLTO's situation where imported CUs may very easily be |
| 715 | // duplicate with the same CU partially imported into another ThinLTO unit. |
| 716 | StringRef DWOName; |
| 717 | if (CUMap.size() > 1) |
| 718 | DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; |
| 719 | |
Eric Christopher | ad10cb5 | 2013-12-04 23:24:38 +0000 | [diff] [blame] | 720 | // Handle anything that needs to be done on a per-unit basis after |
| 721 | // all other generation. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 722 | for (const auto &P : CUMap) { |
| 723 | auto &TheCU = *P.second; |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 724 | // Emit DW_AT_containing_type attribute to connect types with their |
| 725 | // vtable holding type. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 726 | TheCU.constructContainingTypeDIEs(); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 727 | |
Eric Christopher | 46e2343 | 2013-12-20 04:16:18 +0000 | [diff] [blame] | 728 | // Add CU specific attributes if we need to add any. |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 729 | // If we're splitting the dwarf out now that we've got the entire |
| 730 | // CU then add the dwo id to it. |
| 731 | auto *SkCU = TheCU.getSkeleton(); |
| 732 | if (useSplitDwarf()) { |
| 733 | // Emit a unique identifier for this CU. |
Mehdi Amini | 96ab48f | 2017-05-29 06:32:34 +0000 | [diff] [blame] | 734 | uint64_t ID = |
| 735 | DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 736 | TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 737 | dwarf::DW_FORM_data8, ID); |
| 738 | SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, |
| 739 | dwarf::DW_FORM_data8, ID); |
David Blaikie | 3ffe4dd | 2014-03-25 05:34:24 +0000 | [diff] [blame] | 740 | |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 741 | // We don't keep track of which addresses are used in which CU so this |
| 742 | // is a bit pessimistic under LTO. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 743 | if (!AddrPool.isEmpty()) { |
| 744 | const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol(); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 745 | SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base, |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 746 | Sym, Sym); |
| 747 | } |
| 748 | if (!SkCU->getRangeLists().empty()) { |
| 749 | const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 750 | SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 751 | Sym, Sym); |
| 752 | } |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 753 | } |
Eric Christopher | d866720 | 2013-12-30 17:22:27 +0000 | [diff] [blame] | 754 | |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 755 | // If we have code split among multiple sections or non-contiguous |
| 756 | // ranges of code then emit a DW_AT_ranges attribute on the unit that will |
| 757 | // remain in the .o file, otherwise add a DW_AT_low_pc. |
| 758 | // FIXME: We should use ranges allow reordering of code ala |
| 759 | // .subsections_via_symbols in mach-o. This would mean turning on |
| 760 | // ranges for all subprogram DIEs for mach-o. |
David Blaikie | a8be0a7 | 2014-11-01 01:15:24 +0000 | [diff] [blame] | 761 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 762 | if (unsigned NumRanges = TheCU.getRanges().size()) { |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 763 | if (NumRanges > 1 && useRangesSection()) |
David Blaikie | 1998a2e | 2014-11-01 01:11:19 +0000 | [diff] [blame] | 764 | // A DW_AT_low_pc attribute may also be specified in combination with |
| 765 | // DW_AT_ranges to specify the default base address for use in |
| 766 | // location lists (see Section 2.6.2) and range lists (see Section |
| 767 | // 2.17.3). |
| 768 | U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 769 | else |
David Blaikie | 72d03ef | 2015-05-02 02:31:49 +0000 | [diff] [blame] | 770 | U.setBaseAddress(TheCU.getRanges().front().getStart()); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 771 | U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 772 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 773 | |
| 774 | auto *CUNode = cast<DICompileUnit>(P.first); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 775 | // If compile Unit has macros, emit "DW_AT_macro_info" attribute. |
| 776 | if (CUNode->getMacros()) |
| 777 | U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, |
| 778 | U.getMacroLabelBegin(), |
| 779 | TLOF.getDwarfMacinfoSection()->getBeginSymbol()); |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 782 | // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. |
| 783 | for (auto *CUNode : MMI->getModule()->debug_compile_units()) |
Adrian Prantl | 960e766 | 2017-07-27 15:24:20 +0000 | [diff] [blame] | 784 | if (CUNode->getDWOId()) |
Adrian Prantl | 833ad37 | 2017-07-26 18:48:32 +0000 | [diff] [blame] | 785 | getOrCreateDwarfCompileUnit(CUNode); |
| 786 | |
Eric Christopher | 60eb769 | 2013-08-12 20:27:48 +0000 | [diff] [blame] | 787 | // Compute DIE offsets and sizes. |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 788 | InfoHolder.computeSizeAndOffsets(); |
| 789 | if (useSplitDwarf()) |
| 790 | SkeletonHolder.computeSizeAndOffsets(); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 793 | // Emit all Dwarf sections that should come after the content. |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 794 | void DwarfDebug::endModule() { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 795 | assert(CurFn == nullptr); |
| 796 | assert(CurMI == nullptr); |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 797 | |
David Blaikie | 0dc623c | 2014-10-24 17:53:38 +0000 | [diff] [blame] | 798 | // If we aren't actually generating debug info (check beginModule - |
| 799 | // conditionalized on !DisableDebugInfoPrinting and the presence of the |
| 800 | // llvm.dbg.cu metadata node) |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 801 | if (!MMI->hasDebugInfo()) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 802 | return; |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 803 | |
Eric Christopher | 960ac37 | 2012-11-22 00:59:49 +0000 | [diff] [blame] | 804 | // Finalize the debug info for the module. |
| 805 | finalizeModuleInfo(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 806 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 807 | emitDebugStr(); |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 808 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 809 | if (useSplitDwarf()) |
| 810 | emitDebugLocDWO(); |
| 811 | else |
| 812 | // Emit info into a debug loc section. |
| 813 | emitDebugLoc(); |
| 814 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 815 | // Corresponding abbreviations into a abbrev section. |
| 816 | emitAbbreviations(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 817 | |
Rafael Espindola | f1a13f5 | 2015-03-11 00:51:37 +0000 | [diff] [blame] | 818 | // Emit all the DIEs into a debug info section. |
| 819 | emitDebugInfo(); |
| 820 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 821 | // Emit info into a debug aranges section. |
Eric Christopher | 02dbadb | 2014-02-14 01:26:55 +0000 | [diff] [blame] | 822 | if (GenerateARangeSection) |
| 823 | emitDebugARanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 824 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 825 | // Emit info into a debug ranges section. |
| 826 | emitDebugRanges(); |
Eric Christopher | 95198f50 | 2012-11-27 22:43:42 +0000 | [diff] [blame] | 827 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 828 | // Emit info into a debug macinfo section. |
| 829 | emitDebugMacinfo(); |
| 830 | |
Eric Christopher | 9a8f5ed | 2013-11-19 09:04:50 +0000 | [diff] [blame] | 831 | if (useSplitDwarf()) { |
| 832 | emitDebugStrDWO(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 833 | emitDebugInfoDWO(); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 834 | emitDebugAbbrevDWO(); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 835 | emitDebugLineDWO(); |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 836 | // Emit DWO addresses. |
David Blaikie | d75fb28 | 2014-04-23 21:20:10 +0000 | [diff] [blame] | 837 | AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 838 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 839 | |
Eric Christopher | a876b82 | 2012-08-23 07:32:06 +0000 | [diff] [blame] | 840 | // Emit info into the dwarf accelerator table sections. |
Eric Christopher | 20b76a7 | 2012-08-23 22:36:40 +0000 | [diff] [blame] | 841 | if (useDwarfAccelTables()) { |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 842 | emitAccelNames(); |
| 843 | emitAccelObjC(); |
| 844 | emitAccelNamespaces(); |
| 845 | emitAccelTypes(); |
| 846 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 847 | |
Eric Christopher | 4b35818 | 2013-08-30 00:40:17 +0000 | [diff] [blame] | 848 | // Emit the pubnames and pubtypes sections if requested. |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 849 | emitDebugPubSections(); |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 850 | |
Devang Patel | d070128 | 2010-08-02 17:32:15 +0000 | [diff] [blame] | 851 | // clean up. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 852 | // FIXME: AbstractVariables.clear(); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 853 | } |
| 854 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 855 | void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV, |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 856 | const MDNode *ScopeNode) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 857 | const DILocalVariable *Cleansed = nullptr; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 858 | if (CU.getExistingAbstractVariable(IV, Cleansed)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 859 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 860 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 861 | CU.createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 862 | cast<DILocalScope>(ScopeNode))); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 863 | } |
| 864 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 865 | void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU, |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 866 | InlinedVariable IV, const MDNode *ScopeNode) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 867 | const DILocalVariable *Cleansed = nullptr; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 868 | if (CU.getExistingAbstractVariable(IV, Cleansed)) |
David Blaikie | 6f9e867 | 2014-06-13 23:52:55 +0000 | [diff] [blame] | 869 | return; |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 870 | |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 871 | if (LexicalScope *Scope = |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 872 | LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 873 | CU.createAbstractVariable(Cleansed, Scope); |
David Blaikie | 36408e7 | 2014-06-04 23:50:52 +0000 | [diff] [blame] | 874 | } |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 875 | |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 876 | // Collect variable information from side table maintained by MF. |
| 877 | void DwarfDebug::collectVariableInfoFromMFTable( |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 878 | DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) { |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 879 | SmallDenseMap<InlinedVariable, DbgVariable *> MFVars; |
Matthias Braun | ef331ef | 2016-11-30 23:48:50 +0000 | [diff] [blame] | 880 | for (const auto &VI : Asm->MF->getVariableDbgInfo()) { |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 881 | if (!VI.Var) |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 882 | continue; |
Duncan P. N. Exon Smith | 78a9527 | 2015-04-16 22:12:59 +0000 | [diff] [blame] | 883 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 884 | "Expected inlined-at fields to agree"); |
| 885 | |
| 886 | InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt()); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 887 | Processed.insert(Var); |
Benjamin Kramer | 2abfd6c7 | 2014-03-09 15:44:39 +0000 | [diff] [blame] | 888 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
Jim Grosbach | a8683bb | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 889 | |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 890 | // If variable scope is not found then skip this variable. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 891 | if (!Scope) |
Devang Patel | cdb7d44 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 892 | continue; |
Devang Patel | f6eeaeb | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 893 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 894 | ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode()); |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 895 | auto RegVar = llvm::make_unique<DbgVariable>(Var.first, Var.second); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 896 | RegVar->initializeMMI(VI.Expr, VI.Slot); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 897 | if (DbgVariable *DbgVar = MFVars.lookup(Var)) |
| 898 | DbgVar->addMMIEntry(*RegVar); |
| 899 | else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { |
| 900 | MFVars.insert({Var, RegVar.get()}); |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 901 | ConcreteVariables.push_back(std::move(RegVar)); |
Adrian Prantl | be66271 | 2017-07-25 23:32:59 +0000 | [diff] [blame] | 902 | } |
Devang Patel | 475d32a | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 903 | } |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 904 | } |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 905 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 906 | // Get .debug_loc entry for the instruction range starting at MI. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 907 | static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 908 | const DIExpression *Expr = MI->getDebugExpression(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 909 | assert(MI->getNumOperands() == 4); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 910 | if (MI->getOperand(0).isReg()) { |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 911 | auto RegOp = MI->getOperand(0); |
| 912 | auto Op1 = MI->getOperand(1); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 913 | // If the second operand is an immediate, this is a |
| 914 | // register-indirect address. |
Adrian Prantl | bd6d291 | 2017-08-02 17:19:13 +0000 | [diff] [blame] | 915 | assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset"); |
| 916 | MachineLocation MLoc(RegOp.getReg(), Op1.isImm()); |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 917 | return DebugLocEntry::Value(Expr, MLoc); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 918 | } |
| 919 | if (MI->getOperand(0).isImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 920 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 921 | if (MI->getOperand(0).isFPImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 922 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 923 | if (MI->getOperand(0).isCImm()) |
Duncan P. N. Exon Smith | 546c8be | 2015-04-17 16:33:37 +0000 | [diff] [blame] | 924 | return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm()); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 925 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 926 | llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); |
Devang Patel | 2442a89 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 929 | /// \brief If this and Next are describing different fragments of the same |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 930 | /// variable, merge them by appending Next's values to the current |
| 931 | /// list of values. |
| 932 | /// Return true if the merge was successful. |
| 933 | bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) { |
| 934 | if (Begin == Next.Begin) { |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 935 | auto *FirstExpr = cast<DIExpression>(Values[0].Expression); |
| 936 | auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 937 | if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment()) |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 938 | return false; |
| 939 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 940 | // We can only merge entries if none of the fragments overlap any others. |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 941 | // In doing so, we can take advantage of the fact that both lists are |
| 942 | // sorted. |
| 943 | for (unsigned i = 0, j = 0; i < Values.size(); ++i) { |
| 944 | for (; j < Next.Values.size(); ++j) { |
Bjorn Pettersson | a223f815 | 2018-03-12 18:02:39 +0000 | [diff] [blame] | 945 | int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp( |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 946 | cast<DIExpression>(Next.Values[j].Expression)); |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 947 | if (res == 0) // The two expressions overlap, we can't merge. |
| 948 | return false; |
| 949 | // Values[i] is entirely before Next.Values[j], |
| 950 | // so go back to the next entry of Values. |
| 951 | else if (res == -1) |
| 952 | break; |
| 953 | // Next.Values[j] is entirely before Values[i], so go on to the |
| 954 | // next entry of Next.Values. |
| 955 | } |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 956 | } |
Keno Fischer | 6c1e47a | 2016-02-03 21:13:33 +0000 | [diff] [blame] | 957 | |
| 958 | addValues(Next.Values); |
| 959 | End = Next.End; |
| 960 | return true; |
Keno Fischer | f8eb6a1 | 2016-01-16 01:11:33 +0000 | [diff] [blame] | 961 | } |
| 962 | return false; |
| 963 | } |
| 964 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 965 | /// Build the location list for all DBG_VALUEs in the function that |
| 966 | /// describe the same variable. If the ranges of several independent |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 967 | /// fragments of the same variable overlap partially, split them up and |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 968 | /// combine the ranges. The resulting DebugLocEntries are will have |
| 969 | /// strict monotonically increasing begin addresses and will never |
| 970 | /// overlap. |
| 971 | // |
| 972 | // Input: |
| 973 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 974 | // Ranges History [var, loc, fragment ofs size] |
| 975 | // 0 | [x, (reg0, fragment 0, 32)] |
| 976 | // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 977 | // 2 | | ... |
| 978 | // 3 | [clobber reg0] |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 979 | // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of |
Eric Christopher | ffc5ff3 | 2015-02-17 20:02:28 +0000 | [diff] [blame] | 980 | // x. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 981 | // |
| 982 | // Output: |
| 983 | // |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 984 | // [0-1] [x, (reg0, fragment 0, 32)] |
| 985 | // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)] |
| 986 | // [3-4] [x, (reg1, fragment 32, 32)] |
| 987 | // [4- ] [x, (mem, fragment 0, 64)] |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 988 | void |
| 989 | DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, |
| 990 | const DbgValueHistoryMap::InstrRanges &Ranges) { |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 991 | SmallVector<DebugLocEntry::Value, 4> OpenRanges; |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 992 | |
| 993 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 994 | const MachineInstr *Begin = I->first; |
| 995 | const MachineInstr *End = I->second; |
| 996 | assert(Begin->isDebugValue() && "Invalid History entry"); |
| 997 | |
| 998 | // Check if a variable is inaccessible in this range. |
Adrian Prantl | 5e1fa85 | 2014-08-12 21:55:58 +0000 | [diff] [blame] | 999 | if (Begin->getNumOperands() > 1 && |
| 1000 | Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) { |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1001 | OpenRanges.clear(); |
| 1002 | continue; |
| 1003 | } |
| 1004 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1005 | // If this fragment overlaps with any open ranges, truncate them. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1006 | const DIExpression *DIExpr = Begin->getDebugExpression(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 1007 | auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) { |
Bjorn Pettersson | a223f815 | 2018-03-12 18:02:39 +0000 | [diff] [blame] | 1008 | return DIExpr->fragmentsOverlap(R.getExpression()); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1009 | }); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1010 | OpenRanges.erase(Last, OpenRanges.end()); |
| 1011 | |
| 1012 | const MCSymbol *StartLabel = getLabelBeforeInsn(Begin); |
| 1013 | assert(StartLabel && "Forgot label before DBG_VALUE starting a range!"); |
| 1014 | |
| 1015 | const MCSymbol *EndLabel; |
| 1016 | if (End != nullptr) |
| 1017 | EndLabel = getLabelAfterInsn(End); |
| 1018 | else if (std::next(I) == Ranges.end()) |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 1019 | EndLabel = Asm->getFunctionEnd(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1020 | else |
| 1021 | EndLabel = getLabelBeforeInsn(std::next(I)->first); |
| 1022 | assert(EndLabel && "Forgot label after instruction ending a range!"); |
| 1023 | |
| 1024 | DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n"); |
| 1025 | |
| 1026 | auto Value = getDebugLocValue(Begin); |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 1027 | DebugLocEntry Loc(StartLabel, EndLabel, Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1028 | bool couldMerge = false; |
| 1029 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1030 | // If this is a fragment, it may belong to the current DebugLocEntry. |
| 1031 | if (DIExpr->isFragment()) { |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1032 | // Add this value to the list of open ranges. |
Adrian Prantl | caaf053 | 2014-08-11 21:05:57 +0000 | [diff] [blame] | 1033 | OpenRanges.push_back(Value); |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1034 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1035 | // Attempt to add the fragment to the last entry. |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1036 | if (!DebugLoc.empty()) |
| 1037 | if (DebugLoc.back().MergeValues(Loc)) |
| 1038 | couldMerge = true; |
| 1039 | } |
| 1040 | |
| 1041 | if (!couldMerge) { |
| 1042 | // Need to add a new DebugLocEntry. Add all values from still |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1043 | // valid non-overlapping fragments. |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 1044 | if (OpenRanges.size()) |
| 1045 | Loc.addValues(OpenRanges); |
| 1046 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1047 | DebugLoc.push_back(std::move(Loc)); |
| 1048 | } |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Attempt to coalesce the ranges of two otherwise identical |
| 1051 | // DebugLocEntries. |
| 1052 | auto CurEntry = DebugLoc.rbegin(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1053 | DEBUG({ |
| 1054 | dbgs() << CurEntry->getValues().size() << " Values:\n"; |
Adrian Prantl | 6f8c1b6 | 2015-05-26 20:06:51 +0000 | [diff] [blame] | 1055 | for (auto &Value : CurEntry->getValues()) |
Adrian Prantl | dba58fb | 2016-02-29 22:28:22 +0000 | [diff] [blame] | 1056 | Value.dump(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1057 | dbgs() << "-----\n"; |
| 1058 | }); |
Adrian Prantl | 7570731 | 2015-05-26 20:06:48 +0000 | [diff] [blame] | 1059 | |
| 1060 | auto PrevEntry = std::next(CurEntry); |
| 1061 | if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) |
| 1062 | DebugLoc.pop_back(); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1066 | DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU, |
| 1067 | LexicalScope &Scope, |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1068 | InlinedVariable IV) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1069 | ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode()); |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1070 | ConcreteVariables.push_back( |
| 1071 | llvm::make_unique<DbgVariable>(IV.first, IV.second)); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1072 | InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get()); |
| 1073 | return ConcreteVariables.back().get(); |
| 1074 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1075 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1076 | /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its |
| 1077 | /// enclosing lexical scope. The check ensures there are no other instructions |
| 1078 | /// in the same lexical scope preceding the DBG_VALUE and that its range is |
| 1079 | /// either open or otherwise rolls off the end of the scope. |
| 1080 | static bool validThroughout(LexicalScopes &LScopes, |
| 1081 | const MachineInstr *DbgValue, |
| 1082 | const MachineInstr *RangeEnd) { |
| 1083 | assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); |
| 1084 | auto MBB = DbgValue->getParent(); |
| 1085 | auto DL = DbgValue->getDebugLoc(); |
| 1086 | auto *LScope = LScopes.findLexicalScope(DL); |
| 1087 | // Scope doesn't exist; this is a dead DBG_VALUE. |
| 1088 | if (!LScope) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1089 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1090 | auto &LSRange = LScope->getRanges(); |
| 1091 | if (LSRange.size() == 0) |
| 1092 | return false; |
| 1093 | |
| 1094 | // Determine if the DBG_VALUE is valid at the beginning of its lexical block. |
| 1095 | const MachineInstr *LScopeBegin = LSRange.front().first; |
| 1096 | // Early exit if the lexical scope begins outside of the current block. |
| 1097 | if (LScopeBegin->getParent() != MBB) |
| 1098 | return false; |
| 1099 | MachineBasicBlock::const_reverse_iterator Pred(DbgValue); |
| 1100 | for (++Pred; Pred != MBB->rend(); ++Pred) { |
| 1101 | if (Pred->getFlag(MachineInstr::FrameSetup)) |
| 1102 | break; |
| 1103 | auto PredDL = Pred->getDebugLoc(); |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1104 | if (!PredDL || Pred->isMetaInstruction()) |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1105 | continue; |
| 1106 | // Check whether the instruction preceding the DBG_VALUE is in the same |
| 1107 | // (sub)scope as the DBG_VALUE. |
Adrian Prantl | 25422dc | 2017-06-20 21:08:52 +0000 | [diff] [blame] | 1108 | if (DL->getScope() == PredDL->getScope()) |
| 1109 | return false; |
| 1110 | auto *PredScope = LScopes.findLexicalScope(PredDL); |
| 1111 | if (!PredScope || LScope->dominates(PredScope)) |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1112 | return false; |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // If the range of the DBG_VALUE is open-ended, report success. |
| 1116 | if (!RangeEnd) |
| 1117 | return true; |
| 1118 | |
| 1119 | // Fail if there are instructions belonging to our scope in another block. |
| 1120 | const MachineInstr *LScopeEnd = LSRange.back().second; |
| 1121 | if (LScopeEnd->getParent() != MBB) |
| 1122 | return false; |
| 1123 | |
| 1124 | // Single, constant DBG_VALUEs in the prologue are promoted to be live |
| 1125 | // throughout the function. This is a hack, presumably for DWARF v2 and not |
| 1126 | // necessarily correct. It would be much better to use a dbg.declare instead |
| 1127 | // if we know the constant is live throughout the scope. |
| 1128 | if (DbgValue->getOperand(0).isImm() && MBB->pred_empty()) |
| 1129 | return true; |
| 1130 | |
| 1131 | return false; |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1134 | // Find variables for each lexical scope. |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 1135 | void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1136 | const DISubprogram *SP, |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1137 | DenseSet<InlinedVariable> &Processed) { |
Eric Christopher | 270a12c | 2013-07-03 21:37:03 +0000 | [diff] [blame] | 1138 | // Grab the variable info that was squirreled away in the MMI side-table. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1139 | collectVariableInfoFromMFTable(TheCU, Processed); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1140 | |
Alexey Samsonov | 0436caa | 2014-04-30 23:02:40 +0000 | [diff] [blame] | 1141 | for (const auto &I : DbgValues) { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1142 | InlinedVariable IV = I.first; |
| 1143 | if (Processed.count(IV)) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1144 | continue; |
| 1145 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1146 | // Instruction ranges, specifying where IV is accessible. |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1147 | const auto &Ranges = I.second; |
| 1148 | if (Ranges.empty()) |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1149 | continue; |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1150 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1151 | LexicalScope *Scope = nullptr; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1152 | if (const DILocation *IA = IV.second) |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1153 | Scope = LScopes.findInlinedScope(IV.first->getScope(), IA); |
Duncan P. N. Exon Smith | b474937 | 2015-02-17 00:02:27 +0000 | [diff] [blame] | 1154 | else |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1155 | Scope = LScopes.findLexicalScope(IV.first->getScope()); |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1156 | // If variable scope is not found then skip this variable. |
Devang Patel | fbd6c45 | 2010-05-21 00:10:20 +0000 | [diff] [blame] | 1157 | if (!Scope) |
Devang Patel | 490c8ab | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 1158 | continue; |
| 1159 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1160 | Processed.insert(IV); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1161 | DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1162 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 1163 | const MachineInstr *MInsn = Ranges.front().first; |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1164 | assert(MInsn->isDebugValue() && "History must begin with debug value"); |
Jakob Stoklund Olesen | 9a624fa | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1165 | |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1166 | // Check if there is a single DBG_VALUE, valid throughout the var's scope. |
Adrian Prantl | fb2add2 | 2016-02-29 19:49:46 +0000 | [diff] [blame] | 1167 | if (Ranges.size() == 1 && |
Adrian Prantl | 274bcbc | 2017-06-16 22:40:04 +0000 | [diff] [blame] | 1168 | validThroughout(LScopes, MInsn, Ranges.front().second)) { |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1169 | RegVar->initializeDbgValue(MInsn); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1170 | continue; |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1171 | } |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1172 | |
Eric Christopher | 59cc071 | 2013-01-28 17:33:26 +0000 | [diff] [blame] | 1173 | // Handle multiple DBG_VALUE instructions describing one variable. |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1174 | DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); |
Jakob Stoklund Olesen | 9c057ee | 2011-03-22 00:21:41 +0000 | [diff] [blame] | 1175 | |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1176 | // Build the location list for this variable. |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1177 | SmallVector<DebugLocEntry, 8> Entries; |
| 1178 | buildLocationList(Entries, Ranges); |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1179 | |
Adrian Prantl | 693e8de | 2016-02-29 17:06:46 +0000 | [diff] [blame] | 1180 | // If the variable has a DIBasicType, extract it. Basic types cannot have |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1181 | // unique identifiers, so don't bother resolving the type with the |
| 1182 | // identifier map. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1183 | const DIBasicType *BT = dyn_cast<DIBasicType>( |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1184 | static_cast<const Metadata *>(IV.first->getType())); |
| 1185 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1186 | // Finalize the entry by lowering it into a DWARF bytestream. |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1187 | for (auto &Entry : Entries) |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1188 | Entry.finalize(*Asm, List, BT); |
Devang Patel | a3e9c9c | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1189 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1190 | |
| 1191 | // Collect info for variables that were optimized out. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1192 | for (const DILocalVariable *DV : SP->getVariables()) { |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1193 | if (Processed.insert(InlinedVariable(DV, nullptr)).second) |
| 1194 | if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope())) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1195 | createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr)); |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1196 | } |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1197 | } |
Devang Patel | e0a94bf | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1198 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1199 | // Process beginning of an instruction. |
Devang Patel | b5694e7 | 2010-10-26 17:49:02 +0000 | [diff] [blame] | 1200 | void DwarfDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1201 | DebugHandlerBase::beginInstruction(MI); |
| 1202 | assert(CurMI); |
| 1203 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1204 | const auto *SP = MI->getMF()->getFunction().getSubprogram(); |
David Blaikie | 7f2b717 | 2017-05-26 17:05:15 +0000 | [diff] [blame] | 1205 | if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
| 1206 | return; |
| 1207 | |
Paul Robinson | 4fa7b57a | 2016-12-09 19:15:32 +0000 | [diff] [blame] | 1208 | // Check if source location changes, but ignore DBG_VALUE and CFI locations. |
Paul Robinson | ee88ed6 | 2018-02-14 17:35:52 +0000 | [diff] [blame] | 1209 | // If the instruction is part of the function frame setup code, do not emit |
| 1210 | // any line record, as there is no correspondence with any user code. |
| 1211 | if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup)) |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1212 | return; |
| 1213 | const DebugLoc &DL = MI->getDebugLoc(); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1214 | // When we emit a line-0 record, we don't update PrevInstLoc; so look at |
| 1215 | // the last line number actually emitted, to see if it was line 0. |
| 1216 | unsigned LastAsmLine = |
| 1217 | Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); |
Paul Robinson | 96de8c7 | 2016-11-29 22:41:16 +0000 | [diff] [blame] | 1218 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1219 | if (DL == PrevInstLoc) { |
| 1220 | // If we have an ongoing unspecified location, nothing to do here. |
| 1221 | if (!DL) |
| 1222 | return; |
| 1223 | // We have an explicit location, same as the previous location. |
| 1224 | // But we might be coming back to it after a line 0 record. |
| 1225 | if (LastAsmLine == 0 && DL.getLine() != 0) { |
| 1226 | // Reinstate the source location but not marked as a statement. |
| 1227 | const MDNode *Scope = DL.getScope(); |
| 1228 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); |
Paul Robinson | 37a13dd | 2016-11-30 22:49:55 +0000 | [diff] [blame] | 1229 | } |
| 1230 | return; |
| 1231 | } |
| 1232 | |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1233 | if (!DL) { |
| 1234 | // We have an unspecified location, which might want to be line 0. |
| 1235 | // If we have already emitted a line-0 record, don't repeat it. |
| 1236 | if (LastAsmLine == 0) |
| 1237 | return; |
| 1238 | // If user said Don't Do That, don't do that. |
| 1239 | if (UnknownLocations == Disable) |
| 1240 | return; |
| 1241 | // See if we have a reason to emit a line-0 record now. |
| 1242 | // Reasons to emit a line-0 record include: |
| 1243 | // - User asked for it (UnknownLocations). |
| 1244 | // - Instruction has a label, so it's referenced from somewhere else, |
| 1245 | // possibly debug information; we want it to have a source location. |
| 1246 | // - Instruction is at the top of a block; we don't want to inherit the |
| 1247 | // location from the physically previous (maybe unrelated) block. |
| 1248 | if (UnknownLocations == Enable || PrevLabel || |
| 1249 | (PrevInstBB && PrevInstBB != MI->getParent())) { |
Paul Robinson | 8fec3da | 2016-12-14 00:27:35 +0000 | [diff] [blame] | 1250 | // Preserve the file and column numbers, if we can, to save space in |
| 1251 | // the encoded line table. |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1252 | // Do not update PrevInstLoc, it remembers the last non-0 line. |
Paul Robinson | 8fec3da | 2016-12-14 00:27:35 +0000 | [diff] [blame] | 1253 | const MDNode *Scope = nullptr; |
| 1254 | unsigned Column = 0; |
| 1255 | if (PrevInstLoc) { |
| 1256 | Scope = PrevInstLoc.getScope(); |
| 1257 | Column = PrevInstLoc.getCol(); |
| 1258 | } |
| 1259 | recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1260 | } |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | // We have an explicit location, different from the previous location. |
| 1265 | // Don't repeat a line-0 record, but otherwise emit the new location. |
| 1266 | // (The new location might be an explicit line 0, which we do emit.) |
Paul Robinson | 2dfb688 | 2016-12-16 23:54:33 +0000 | [diff] [blame] | 1267 | if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0) |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1268 | return; |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1269 | unsigned Flags = 0; |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1270 | if (DL == PrologEndLoc) { |
| 1271 | Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; |
| 1272 | PrologEndLoc = DebugLoc(); |
| 1273 | } |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1274 | // If the line changed, we call that a new statement; unless we went to |
| 1275 | // line 0 and came back, in which case it is not a new statement. |
| 1276 | unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; |
| 1277 | if (DL.getLine() && DL.getLine() != OldLine) |
Paul Robinson | f428c9b | 2016-11-22 19:46:51 +0000 | [diff] [blame] | 1278 | Flags |= DWARF2_FLAG_IS_STMT; |
| 1279 | |
| 1280 | const MDNode *Scope = DL.getScope(); |
| 1281 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 1282 | |
| 1283 | // If we're not at line 0, remember this location. |
| 1284 | if (DL.getLine()) |
| 1285 | PrevInstLoc = DL; |
Devang Patel | 75cc16c | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1288 | static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { |
| 1289 | // First known non-DBG_VALUE and non-frame setup location marks |
| 1290 | // the beginning of the function body. |
| 1291 | for (const auto &MBB : *MF) |
| 1292 | for (const auto &MI : MBB) |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 1293 | if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && |
Michael Kuperstein | 2b3c16c | 2015-10-08 07:48:49 +0000 | [diff] [blame] | 1294 | MI.getDebugLoc()) |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1295 | return MI.getDebugLoc(); |
| 1296 | return DebugLoc(); |
| 1297 | } |
| 1298 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1299 | // Gather pre-function debug information. Assumes being called immediately |
| 1300 | // after the function entry point has been emitted. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1301 | void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1302 | CurFn = MF; |
Eric Christopher | fedfa44 | 2013-11-01 23:14:17 +0000 | [diff] [blame] | 1303 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1304 | auto *SP = MF->getFunction().getSubprogram(); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1305 | assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1306 | if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 1307 | return; |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1308 | |
| 1309 | DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function |
| 1312 | // belongs to so that we add to the correct per-cu line table in the |
| 1313 | // non-asm case. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1314 | if (Asm->OutStreamer->hasRawTextSupport()) |
Rafael Espindola | b4eec1d | 2014-02-05 18:00:21 +0000 | [diff] [blame] | 1315 | // Use a single line table if we are generating assembly. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1316 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Manman Ren | 9d4c735 | 2013-05-21 00:57:22 +0000 | [diff] [blame] | 1317 | else |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1318 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); |
Manman Ren | 4e042a6 | 2013-02-05 21:52:47 +0000 | [diff] [blame] | 1319 | |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1320 | // Record beginning of function. |
Alexey Samsonov | 8a86d6d | 2014-05-27 22:47:41 +0000 | [diff] [blame] | 1321 | PrologEndLoc = findPrologueEndLoc(MF); |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1322 | if (PrologEndLoc) { |
Saleem Abdulrasool | fc07c72 | 2015-01-24 20:19:45 +0000 | [diff] [blame] | 1323 | // We'd like to list the prologue as "not statements" but GDB behaves |
| 1324 | // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. |
David Blaikie | 2c78f18 | 2017-05-25 23:11:28 +0000 | [diff] [blame] | 1325 | auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); |
Duncan P. N. Exon Smith | fd07a2a | 2015-03-30 21:32:28 +0000 | [diff] [blame] | 1326 | recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT); |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1327 | } |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1330 | void DwarfDebug::skippedNonDebugFunction() { |
| 1331 | // If we don't have a subprogram for this function then there will be a hole |
| 1332 | // in the range information. Keep note of this by setting the previously used |
| 1333 | // section to nullptr. |
| 1334 | PrevCU = nullptr; |
| 1335 | CurFn = nullptr; |
| 1336 | } |
| 1337 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1338 | // Gather and emit post-function debug information. |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1339 | void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1340 | const DISubprogram *SP = MF->getFunction().getSubprogram(); |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 1341 | |
David Blaikie | e75f963 | 2014-10-14 17:12:02 +0000 | [diff] [blame] | 1342 | assert(CurFn == MF && |
| 1343 | "endFunction should be called with the same function as beginFunction"); |
Timur Iskhodzhanov | 1cd1444 | 2013-12-03 15:10:23 +0000 | [diff] [blame] | 1344 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1345 | // Set DwarfDwarfCompileUnitID in MCContext to default value. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1346 | Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1347 | |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1348 | LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1349 | assert(!FnScope || SP == FnScope->getScopeNode()); |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 1350 | DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); |
David Blaikie | 263a008 | 2014-10-23 00:06:27 +0000 | [diff] [blame] | 1351 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1352 | DenseSet<InlinedVariable> ProcessedVars; |
David Blaikie | 263a008 | 2014-10-23 00:06:27 +0000 | [diff] [blame] | 1353 | collectVariableInfo(TheCU, SP, ProcessedVars); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1354 | |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1355 | // Add the range of this function to the list of ranges for the CU. |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 1356 | TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Under -gmlt, skip building the subprogram if there are no inlined |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1359 | // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram |
| 1360 | // is still needed as we need its source location. |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 1361 | if (!TheCU.getCUNode()->getDebugInfoForProfiling() && |
Dehao Chen | 1ce8d6c | 2017-01-19 00:44:11 +0000 | [diff] [blame] | 1362 | TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && |
David Blaikie | e1c7974 | 2014-09-30 21:28:32 +0000 | [diff] [blame] | 1363 | LScopes.getAbstractScopesList().empty() && !IsDarwin) { |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1364 | assert(InfoHolder.getScopeVariables().empty()); |
David Blaikie | 3a7ce25 | 2014-09-19 17:03:16 +0000 | [diff] [blame] | 1365 | PrevLabel = nullptr; |
| 1366 | CurFn = nullptr; |
| 1367 | return; |
| 1368 | } |
| 1369 | |
Adrian Prantl | 049d21c | 2014-10-13 20:44:58 +0000 | [diff] [blame] | 1370 | #ifndef NDEBUG |
| 1371 | size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); |
| 1372 | #endif |
Devang Patel | 7e62302 | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1373 | // Construct abstract scopes. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1374 | for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1375 | auto *SP = cast<DISubprogram>(AScope->getScopeNode()); |
David Blaikie | 4abe19e | 2014-05-12 18:23:35 +0000 | [diff] [blame] | 1376 | // Collect info for variables that were optimized out. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1377 | for (const DILocalVariable *DV : SP->getVariables()) { |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1378 | if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second) |
David Blaikie | 4abe19e | 2014-05-12 18:23:35 +0000 | [diff] [blame] | 1379 | continue; |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1380 | ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr), |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 1381 | DV->getScope()); |
Adrian Prantl | 049d21c | 2014-10-13 20:44:58 +0000 | [diff] [blame] | 1382 | assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes |
| 1383 | && "ensureAbstractVariableIsCreated inserted abstract scopes"); |
Devang Patel | 5c0f85c | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 1384 | } |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1385 | constructAbstractSubprogramScopeDIE(TheCU, AScope); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1386 | } |
Eric Christopher | 6a84138 | 2012-11-19 22:42:10 +0000 | [diff] [blame] | 1387 | |
David Blaikie | 38b74bf | 2016-12-15 23:37:38 +0000 | [diff] [blame] | 1388 | ProcessedSPNodes.insert(SP); |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1389 | TheCU.constructSubprogramScopeDIE(SP, FnScope); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1390 | if (auto *SkelCU = TheCU.getSkeleton()) |
David Blaikie | a01f295 | 2016-08-24 18:29:49 +0000 | [diff] [blame] | 1391 | if (!LScopes.getAbstractScopesList().empty() && |
| 1392 | TheCU.getCUNode()->getSplitDebugInlining()) |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 1393 | SkelCU->constructSubprogramScopeDIE(SP, FnScope); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1394 | |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1395 | // Clear debug info |
David Blaikie | 825bdd2 | 2014-05-21 22:41:17 +0000 | [diff] [blame] | 1396 | // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the |
| 1397 | // DbgVariables except those that are also in AbstractVariables (since they |
| 1398 | // can be used cross-function) |
David Blaikie | 80e5b1e | 2014-10-24 17:57:34 +0000 | [diff] [blame] | 1399 | InfoHolder.getScopeVariables().clear(); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1400 | PrevLabel = nullptr; |
| 1401 | CurFn = nullptr; |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1404 | // Register a source line with debug info. Returns the unique label that was |
| 1405 | // emitted and which provides correspondence to the source line list. |
Devang Patel | 34a6620 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1406 | void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, |
| 1407 | unsigned Flags) { |
Devang Patel | 2d9caf9 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1408 | StringRef Fn; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1409 | unsigned FileNo = 1; |
Diego Novillo | 282450d | 2014-03-03 18:53:17 +0000 | [diff] [blame] | 1410 | unsigned Discriminator = 0; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1411 | if (auto *Scope = cast_or_null<DIScope>(S)) { |
Duncan P. N. Exon Smith | b273d06 | 2015-04-16 01:37:00 +0000 | [diff] [blame] | 1412 | Fn = Scope->getFilename(); |
Paul Robinson | bb92137 | 2017-09-07 22:15:44 +0000 | [diff] [blame] | 1413 | if (Line != 0 && getDwarfVersion() >= 4) |
| 1414 | if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) |
Dehao Chen | 6e0c844 | 2016-10-07 15:21:31 +0000 | [diff] [blame] | 1415 | Discriminator = LBF->getDiscriminator(); |
Dan Gohman | 50849c6 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1416 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1417 | unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID(); |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1418 | FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID]) |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 1419 | .getOrCreateSourceID(Scope->getFile()); |
Dan Gohman | 50849c6 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1420 | } |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1421 | Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0, |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1422 | Discriminator, Fn); |
Bill Wendling | 2b128d7 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Bill Wendling | 806535f | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 1425 | //===----------------------------------------------------------------------===// |
| 1426 | // Emit Methods |
| 1427 | //===----------------------------------------------------------------------===// |
| 1428 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1429 | // Emit the debug info section. |
| 1430 | void DwarfDebug::emitDebugInfo() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1431 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1432 | Holder.emitUnits(/* UseOffsets */ false); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1435 | // Emit the abbreviation section. |
Eric Christopher | 3837195 | 2012-11-20 23:30:11 +0000 | [diff] [blame] | 1436 | void DwarfDebug::emitAbbreviations() { |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1437 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 1438 | |
| 1439 | Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 1440 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1441 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1442 | void DwarfDebug::emitStringOffsetsTableHeader() { |
| 1443 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
| 1444 | Holder.emitStringOffsetsTableHeader( |
| 1445 | Asm->getObjFileLowering().getDwarfStrOffSection()); |
| 1446 | } |
| 1447 | |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 1448 | template <typename AccelTableT> |
| 1449 | void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section, |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1450 | StringRef TableName) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1451 | Asm->OutStreamer->SwitchSection(Section); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Emit the full data. |
Pavel Labath | a7c457d | 2018-02-19 16:12:20 +0000 | [diff] [blame] | 1454 | emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol()); |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | // Emit visible names into a hashed accelerator table section. |
| 1458 | void DwarfDebug::emitAccelNames() { |
| 1459 | emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1460 | "Names"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Eric Christopher | 48fef59 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 1463 | // Emit objective C classes and categories into a hashed accelerator table |
| 1464 | // section. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1465 | void DwarfDebug::emitAccelObjC() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1466 | emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1467 | "ObjC"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1470 | // Emit namespace dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1471 | void DwarfDebug::emitAccelNamespaces() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1472 | emitAccel(AccelNamespace, |
| 1473 | Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1474 | "namespac"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Eric Christopher | acdcbdb | 2012-11-27 22:43:45 +0000 | [diff] [blame] | 1477 | // Emit type dies into a hashed accelerator table. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1478 | void DwarfDebug::emitAccelTypes() { |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 1479 | emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), |
Rafael Espindola | 6b9998b | 2015-03-10 22:00:25 +0000 | [diff] [blame] | 1480 | "types"); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1483 | // Public name handling. |
| 1484 | // The format for the various pubnames: |
| 1485 | // |
| 1486 | // dwarf pubnames - offset/name pairs where the offset is the offset into the CU |
| 1487 | // for the DIE that is named. |
| 1488 | // |
| 1489 | // gnu pubnames - offset/index value/name tuples where the offset is the offset |
| 1490 | // into the CU and the index value is computed according to the type of value |
| 1491 | // for the DIE that is named. |
| 1492 | // |
| 1493 | // For type units the offset is the offset of the skeleton DIE. For split dwarf |
| 1494 | // it's the offset within the debug_info/debug_types dwo section, however, the |
| 1495 | // reference in the pubname header doesn't change. |
| 1496 | |
| 1497 | /// computeIndexValue - Compute the gdb index value for the DIE and CU. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1498 | static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, |
Eric Christopher | 0fe676a | 2013-11-21 00:48:22 +0000 | [diff] [blame] | 1499 | const DIE *Die) { |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1500 | // Entities that ended up only in a Type Unit reference the CU instead (since |
| 1501 | // the pub entry has offsets within the CU there's no real offset that can be |
| 1502 | // provided anyway). As it happens all such entities (namespaces and types, |
| 1503 | // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out |
| 1504 | // not to be true it would be necessary to persist this information from the |
| 1505 | // point at which the entry is added to the index data structure - since by |
| 1506 | // the time the index is built from that, the original type/namespace DIE in a |
| 1507 | // type unit has already been destroyed so it can't be queried for properties |
| 1508 | // like tag, etc. |
| 1509 | if (Die->getTag() == dwarf::DW_TAG_compile_unit) |
| 1510 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, |
| 1511 | dwarf::GIEL_EXTERNAL); |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1512 | dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; |
| 1513 | |
| 1514 | // We could have a specification DIE that has our most of our knowledge, |
| 1515 | // look for that now. |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1516 | if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { |
| 1517 | DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); |
David Blaikie | 8dbcc3f | 2014-04-25 19:33:43 +0000 | [diff] [blame] | 1518 | if (SpecDIE.findAttribute(dwarf::DW_AT_external)) |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1519 | Linkage = dwarf::GIEL_EXTERNAL; |
| 1520 | } else if (Die->findAttribute(dwarf::DW_AT_external)) |
| 1521 | Linkage = dwarf::GIEL_EXTERNAL; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1522 | |
| 1523 | switch (Die->getTag()) { |
| 1524 | case dwarf::DW_TAG_class_type: |
| 1525 | case dwarf::DW_TAG_structure_type: |
| 1526 | case dwarf::DW_TAG_union_type: |
| 1527 | case dwarf::DW_TAG_enumeration_type: |
Eric Christopher | 261d234 | 2013-09-23 20:55:35 +0000 | [diff] [blame] | 1528 | return dwarf::PubIndexEntryDescriptor( |
| 1529 | dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus |
| 1530 | ? dwarf::GIEL_STATIC |
| 1531 | : dwarf::GIEL_EXTERNAL); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1532 | case dwarf::DW_TAG_typedef: |
| 1533 | case dwarf::DW_TAG_base_type: |
| 1534 | case dwarf::DW_TAG_subrange_type: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1535 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1536 | case dwarf::DW_TAG_namespace: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1537 | return dwarf::GIEK_TYPE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1538 | case dwarf::DW_TAG_subprogram: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1539 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1540 | case dwarf::DW_TAG_variable: |
Eric Christopher | ccac5c4 | 2013-09-23 22:59:14 +0000 | [diff] [blame] | 1541 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1542 | case dwarf::DW_TAG_enumerator: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1543 | return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, |
| 1544 | dwarf::GIEL_STATIC); |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1545 | default: |
David Blaikie | 8dec407 | 2013-09-19 20:40:26 +0000 | [diff] [blame] | 1546 | return dwarf::GIEK_NONE; |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1547 | } |
Eric Christopher | dd1a012 | 2013-09-13 00:35:05 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1550 | /// emitDebugPubSections - Emit visible names and types into debug pubnames and |
| 1551 | /// pubtypes sections. |
| 1552 | void DwarfDebug::emitDebugPubSections() { |
David Blaikie | c3d9e9e | 2014-03-06 01:42:00 +0000 | [diff] [blame] | 1553 | for (const auto &NU : CUMap) { |
| 1554 | DwarfCompileUnit *TheU = NU.second; |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1555 | if (!TheU->hasDwarfPubSections()) |
David Blaikie | ce2f1cb | 2014-03-11 23:35:06 +0000 | [diff] [blame] | 1556 | continue; |
| 1557 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1558 | bool GnuStyle = TheU->getCUNode()->getGnuPubnames(); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1559 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1560 | Asm->OutStreamer->SwitchSection( |
| 1561 | GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() |
| 1562 | : Asm->getObjFileLowering().getDwarfPubNamesSection()); |
| 1563 | emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1564 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1565 | Asm->OutStreamer->SwitchSection( |
| 1566 | GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() |
| 1567 | : Asm->getObjFileLowering().getDwarfPubTypesSection()); |
| 1568 | emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes()); |
Krzysztof Parzyszek | 228daa6 | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1572 | void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) { |
| 1573 | if (useSectionsAsReferences()) |
| 1574 | Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(), |
| 1575 | CU.getDebugSectionOffset()); |
| 1576 | else |
| 1577 | Asm->emitDwarfSymbolReference(CU.getLabelBegin()); |
| 1578 | } |
| 1579 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1580 | void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name, |
| 1581 | DwarfCompileUnit *TheU, |
| 1582 | const StringMap<const DIE *> &Globals) { |
| 1583 | if (auto *Skeleton = TheU->getSkeleton()) |
| 1584 | TheU = Skeleton; |
Eric Christopher | 8b3737f | 2013-09-13 00:34:58 +0000 | [diff] [blame] | 1585 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1586 | // Emit the header. |
| 1587 | Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); |
| 1588 | MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); |
| 1589 | MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); |
| 1590 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); |
| 1591 | |
| 1592 | Asm->OutStreamer->EmitLabel(BeginLabel); |
| 1593 | |
| 1594 | Asm->OutStreamer->AddComment("DWARF Version"); |
| 1595 | Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); |
| 1596 | |
| 1597 | Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1598 | emitSectionReference(*TheU); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1599 | |
| 1600 | Asm->OutStreamer->AddComment("Compilation Unit Length"); |
| 1601 | Asm->EmitInt32(TheU->getLength()); |
| 1602 | |
| 1603 | // Emit the pubnames for this compilation unit. |
| 1604 | for (const auto &GI : Globals) { |
| 1605 | const char *Name = GI.getKeyData(); |
| 1606 | const DIE *Entity = GI.second; |
| 1607 | |
| 1608 | Asm->OutStreamer->AddComment("DIE offset"); |
| 1609 | Asm->EmitInt32(Entity->getOffset()); |
| 1610 | |
| 1611 | if (GnuStyle) { |
| 1612 | dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); |
| 1613 | Asm->OutStreamer->AddComment( |
| 1614 | Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " + |
| 1615 | dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); |
| 1616 | Asm->EmitInt8(Desc.toBits()); |
| 1617 | } |
| 1618 | |
| 1619 | Asm->OutStreamer->AddComment("External Name"); |
| 1620 | Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); |
| 1621 | } |
| 1622 | |
| 1623 | Asm->OutStreamer->AddComment("End Mark"); |
| 1624 | Asm->EmitInt32(0); |
| 1625 | Asm->OutStreamer->EmitLabel(EndLabel); |
Devang Patel | 04d2f2d | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1628 | /// Emit null-terminated strings into a debug str section. |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1629 | void DwarfDebug::emitDebugStr() { |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1630 | MCSection *StringOffsetsSection = nullptr; |
| 1631 | if (useSegmentedStringOffsetsTable()) { |
| 1632 | emitStringOffsetsTableHeader(); |
| 1633 | StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection(); |
| 1634 | } |
Eric Christopher | f819485 | 2013-12-05 18:06:10 +0000 | [diff] [blame] | 1635 | DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 1636 | Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(), |
| 1637 | StringOffsetsSection, /* UseRelativeOffsets = */ true); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1640 | void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1641 | const DebugLocStream::Entry &Entry) { |
| 1642 | auto &&Comments = DebugLocs.getComments(Entry); |
| 1643 | auto Comment = Comments.begin(); |
| 1644 | auto End = Comments.end(); |
| 1645 | for (uint8_t Byte : DebugLocs.getBytes(Entry)) |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1646 | Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : ""); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1649 | static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1650 | ByteStreamer &Streamer, |
| 1651 | const DebugLocEntry::Value &Value, |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1652 | DwarfExpression &DwarfExpr) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1653 | auto *DIExpr = Value.getExpression(); |
| 1654 | DIExpressionCursor ExprCursor(DIExpr); |
| 1655 | DwarfExpr.addFragmentOffset(DIExpr); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 1656 | // Regular entry. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1657 | if (Value.isInt()) { |
Duncan P. N. Exon Smith | fba25d6 | 2015-04-17 16:28:58 +0000 | [diff] [blame] | 1658 | if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || |
| 1659 | BT->getEncoding() == dwarf::DW_ATE_signed_char)) |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1660 | DwarfExpr.addSignedConstant(Value.getInt()); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 1661 | else |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1662 | DwarfExpr.addUnsignedConstant(Value.getInt()); |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 1663 | } else if (Value.isLocation()) { |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1664 | MachineLocation Location = Value.getLoc(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1665 | if (Location.isIndirect()) |
| 1666 | DwarfExpr.setMemoryLocationKind(); |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1667 | DIExpressionCursor Cursor(DIExpr); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1668 | const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1669 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1670 | return; |
| 1671 | return DwarfExpr.addExpression(std::move(Cursor)); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 1672 | } else if (Value.isConstantFP()) { |
| 1673 | APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1674 | DwarfExpr.addUnsignedConstant(RawBytes); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1675 | } |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 1676 | DwarfExpr.addExpression(std::move(ExprCursor)); |
Eric Christopher | 29e874d | 2014-03-07 22:40:37 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1679 | void DebugLocEntry::finalize(const AsmPrinter &AP, |
| 1680 | DebugLocStream::ListBuilder &List, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1681 | const DIBasicType *BT) { |
Duncan P. N. Exon Smith | 3a73d9e | 2015-06-21 16:54:56 +0000 | [diff] [blame] | 1682 | DebugLocStream::EntryBuilder Entry(List, Begin, End); |
| 1683 | BufferByteStreamer Streamer = Entry.getStreamer(); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1684 | DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1685 | const DebugLocEntry::Value &Value = Values[0]; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1686 | if (Value.isFragment()) { |
| 1687 | // Emit all fragments that belong to the same variable and range. |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1688 | assert(llvm::all_of(Values, [](DebugLocEntry::Value P) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1689 | return P.isFragment(); |
| 1690 | }) && "all values are expected to be fragments"); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1691 | assert(std::is_sorted(Values.begin(), Values.end()) && |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1692 | "fragments are expected to be sorted"); |
Duncan P. N. Exon Smith | c82570b | 2015-04-13 18:53:11 +0000 | [diff] [blame] | 1693 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1694 | for (auto Fragment : Values) |
| 1695 | emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr); |
| 1696 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1697 | } else { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 1698 | assert(Values.size() == 1 && "only fragments may have >1 value"); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1699 | emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1700 | } |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 1701 | DwarfExpr.finalize(); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1704 | void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) { |
Duncan P. N. Exon Smith | 653c109 | 2015-05-06 19:11:20 +0000 | [diff] [blame] | 1705 | // Emit the size. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1706 | Asm->OutStreamer->AddComment("Loc expr size"); |
Duncan P. N. Exon Smith | 653c109 | 2015-05-06 19:11:20 +0000 | [diff] [blame] | 1707 | Asm->EmitInt16(DebugLocs.getBytes(Entry).size()); |
| 1708 | |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1709 | // Emit the entry. |
| 1710 | APByteStreamer Streamer(*Asm); |
| 1711 | emitDebugLocEntry(Streamer, Entry); |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Eric Christopher | 9046f94 | 2013-07-02 21:36:07 +0000 | [diff] [blame] | 1714 | // Emit locations into the debug loc section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1715 | void DwarfDebug::emitDebugLoc() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1716 | if (DebugLocs.getLists().empty()) |
| 1717 | return; |
| 1718 | |
Daniel Dunbar | fd95b01 | 2011-03-16 22:16:39 +0000 | [diff] [blame] | 1719 | // Start the dwarf loc section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1720 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1721 | Asm->getObjFileLowering().getDwarfLocSection()); |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1722 | unsigned char Size = Asm->MAI->getCodePointerSize(); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1723 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1724 | Asm->OutStreamer->EmitLabel(List.Label); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1725 | const DwarfCompileUnit *CU = List.CU; |
| 1726 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1727 | // Set up the range. This range is relative to the entry point of the |
| 1728 | // compile unit. This is a hard coded 0 for low_pc when we're emitting |
| 1729 | // ranges, or the DW_AT_low_pc on the compile unit otherwise. |
David Blaikie | ce34349 | 2014-11-03 21:15:30 +0000 | [diff] [blame] | 1730 | if (auto *Base = CU->getBaseAddress()) { |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1731 | Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); |
| 1732 | Asm->EmitLabelDifference(Entry.EndSym, Base, Size); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1733 | } else { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1734 | Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); |
| 1735 | Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); |
Eric Christopher | 384f3fe | 2014-03-20 19:16:16 +0000 | [diff] [blame] | 1736 | } |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1737 | |
David Blaikie | 0e84adc | 2014-04-01 16:17:41 +0000 | [diff] [blame] | 1738 | emitDebugLocEntryLocation(Entry); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1739 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1740 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 1741 | Asm->OutStreamer->EmitIntValue(0, Size); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | void DwarfDebug::emitDebugLocDWO() { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1746 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1747 | Asm->getObjFileLowering().getDwarfLocDWOSection()); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1748 | for (const auto &List : DebugLocs.getLists()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1749 | Asm->OutStreamer->EmitLabel(List.Label); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1750 | for (const auto &Entry : DebugLocs.getEntries(List)) { |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1751 | // Just always use start_length for now - at least that's one address |
| 1752 | // rather than two. We could get fancier and try to, say, reuse an |
| 1753 | // address we know we've emitted elsewhere (the start of the function? |
| 1754 | // The start of the CU or CU subrange that encloses this range?) |
Adrian Prantl | c4fbbcf | 2016-10-28 17:59:50 +0000 | [diff] [blame] | 1755 | Asm->EmitInt8(dwarf::DW_LLE_startx_length); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1756 | unsigned idx = AddrPool.getIndex(Entry.BeginSym); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1757 | Asm->EmitULEB128(idx); |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 1758 | Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); |
David Blaikie | 94c1d7f | 2014-04-02 01:50:20 +0000 | [diff] [blame] | 1759 | |
| 1760 | emitDebugLocEntryLocation(Entry); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 1761 | } |
Adrian Prantl | c4fbbcf | 2016-10-28 17:59:50 +0000 | [diff] [blame] | 1762 | Asm->EmitInt8(dwarf::DW_LLE_end_of_list); |
Devang Patel | 9fc1170 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1763 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1766 | struct ArangeSpan { |
| 1767 | const MCSymbol *Start, *End; |
| 1768 | }; |
| 1769 | |
| 1770 | // Emit a debug aranges section, containing a CU lookup for any |
| 1771 | // address we can tie back to a CU. |
Eric Christopher | 7b30f2e4 | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 1772 | void DwarfDebug::emitDebugARanges() { |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1773 | // Provides a unique id per text section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1774 | MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1775 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1776 | // Filter labels by section. |
| 1777 | for (const SymbolCU &SCU : ArangeLabels) { |
| 1778 | if (SCU.Sym->isInSection()) { |
| 1779 | // Make a note of this symbol and it's section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1780 | MCSection *Section = &SCU.Sym->getSection(); |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1781 | if (!Section->getKind().isMetadata()) |
| 1782 | SectionMap[Section].push_back(SCU); |
| 1783 | } else { |
| 1784 | // Some symbols (e.g. common/bss on mach-o) can have no section but still |
| 1785 | // appear in the output. This sucks as we rely on sections to build |
| 1786 | // arange spans. We can do it without, but it's icky. |
| 1787 | SectionMap[nullptr].push_back(SCU); |
| 1788 | } |
| 1789 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1790 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1791 | DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; |
| 1792 | |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 1793 | for (auto &I : SectionMap) { |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1794 | MCSection *Section = I.first; |
Rafael Espindola | 4f4ef15 | 2015-03-09 22:08:37 +0000 | [diff] [blame] | 1795 | SmallVector<SymbolCU, 8> &List = I.second; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1796 | if (List.size() < 1) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1797 | continue; |
| 1798 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1799 | // If we have no section (e.g. common), just write out |
| 1800 | // individual spans for each symbol. |
| 1801 | if (!Section) { |
| 1802 | for (const SymbolCU &Cur : List) { |
| 1803 | ArangeSpan Span; |
| 1804 | Span.Start = Cur.Sym; |
| 1805 | Span.End = nullptr; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1806 | assert(Cur.CU); |
| 1807 | Spans[Cur.CU].push_back(Span); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1808 | } |
| 1809 | continue; |
| 1810 | } |
| 1811 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1812 | // Sort the symbols by offset within the section. |
Mandeep Singh Grang | 46d02de | 2018-02-25 19:52:34 +0000 | [diff] [blame] | 1813 | std::stable_sort( |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 1814 | List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) { |
| 1815 | unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; |
| 1816 | unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; |
Benjamin Kramer | 571e2fe | 2014-03-07 19:41:22 +0000 | [diff] [blame] | 1817 | |
Rafael Espindola | 0a78f8c | 2016-05-19 23:17:37 +0000 | [diff] [blame] | 1818 | // Symbols with no order assigned should be placed at the end. |
| 1819 | // (e.g. section end labels) |
| 1820 | if (IA == 0) |
| 1821 | return false; |
| 1822 | if (IB == 0) |
| 1823 | return true; |
| 1824 | return IA < IB; |
| 1825 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1826 | |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1827 | // Insert a final terminator. |
| 1828 | List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); |
| 1829 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1830 | // Build spans between each label. |
| 1831 | const MCSymbol *StartSym = List[0].Sym; |
| 1832 | for (size_t n = 1, e = List.size(); n < e; n++) { |
| 1833 | const SymbolCU &Prev = List[n - 1]; |
| 1834 | const SymbolCU &Cur = List[n]; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1835 | |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1836 | // Try and build the longest span we can within the same CU. |
| 1837 | if (Cur.CU != Prev.CU) { |
| 1838 | ArangeSpan Span; |
| 1839 | Span.Start = StartSym; |
| 1840 | Span.End = Cur.Sym; |
Rafael Espindola | 78d947b | 2016-05-20 00:38:28 +0000 | [diff] [blame] | 1841 | assert(Prev.CU); |
Rafael Espindola | 6ffb1d7 | 2015-02-02 19:22:51 +0000 | [diff] [blame] | 1842 | Spans[Prev.CU].push_back(Span); |
| 1843 | StartSym = Cur.Sym; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1848 | // Start the dwarf aranges section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1849 | Asm->OutStreamer->SwitchSection( |
Rafael Espindola | e8fd00d | 2015-02-26 22:02:02 +0000 | [diff] [blame] | 1850 | Asm->getObjFileLowering().getDwarfARangesSection()); |
| 1851 | |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1852 | unsigned PtrSize = Asm->MAI->getCodePointerSize(); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1853 | |
| 1854 | // Build a list of CUs used. |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1855 | std::vector<DwarfCompileUnit *> CUs; |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1856 | for (const auto &it : Spans) { |
| 1857 | DwarfCompileUnit *CU = it.first; |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1858 | CUs.push_back(CU); |
| 1859 | } |
| 1860 | |
| 1861 | // Sort the CU list (again, to ensure consistent output order). |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 1862 | std::sort(CUs.begin(), CUs.end(), |
| 1863 | [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { |
| 1864 | return A->getUniqueID() < B->getUniqueID(); |
| 1865 | }); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1866 | |
| 1867 | // Emit an arange table for each CU we used. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1868 | for (DwarfCompileUnit *CU : CUs) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1869 | std::vector<ArangeSpan> &List = Spans[CU]; |
| 1870 | |
David Blaikie | 27e35f2 | 2014-11-02 01:21:43 +0000 | [diff] [blame] | 1871 | // Describe the skeleton CU's offset and length, not the dwo file's. |
| 1872 | if (auto *Skel = CU->getSkeleton()) |
| 1873 | CU = Skel; |
| 1874 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1875 | // Emit size of content not including length itself. |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 1876 | unsigned ContentSize = |
| 1877 | sizeof(int16_t) + // DWARF ARange version number |
| 1878 | sizeof(int32_t) + // Offset of CU in the .debug_info section |
| 1879 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 1880 | sizeof(int8_t); // Segment Size (in bytes) |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1881 | |
| 1882 | unsigned TupleSize = PtrSize * 2; |
| 1883 | |
| 1884 | // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. |
Benjamin Kramer | 8a68ab3 | 2014-01-07 19:28:14 +0000 | [diff] [blame] | 1885 | unsigned Padding = |
| 1886 | OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1887 | |
| 1888 | ContentSize += Padding; |
| 1889 | ContentSize += (List.size() + 1) * TupleSize; |
| 1890 | |
| 1891 | // For each compile unit, write the list of spans it covers. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1892 | Asm->OutStreamer->AddComment("Length of ARange Set"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1893 | Asm->EmitInt32(ContentSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1894 | Asm->OutStreamer->AddComment("DWARF Arange version number"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1895 | Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1896 | Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1897 | emitSectionReference(*CU); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1898 | Asm->OutStreamer->AddComment("Address Size (in bytes)"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1899 | Asm->EmitInt8(PtrSize); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1900 | Asm->OutStreamer->AddComment("Segment Size (in bytes)"); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1901 | Asm->EmitInt8(0); |
| 1902 | |
Petr Hosek | faef320 | 2016-06-01 01:59:58 +0000 | [diff] [blame] | 1903 | Asm->OutStreamer->emitFill(Padding, 0xff); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1904 | |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1905 | for (const ArangeSpan &Span : List) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1906 | Asm->EmitLabelReference(Span.Start, PtrSize); |
| 1907 | |
| 1908 | // Calculate the size as being from the span start to it's end. |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1909 | if (Span.End) { |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1910 | Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1911 | } else { |
| 1912 | // For symbols without an end marker (e.g. common), we |
| 1913 | // write a single arange entry containing just that one symbol. |
| 1914 | uint64_t Size = SymSize[Span.Start]; |
| 1915 | if (Size == 0) |
| 1916 | Size = 1; |
| 1917 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1918 | Asm->OutStreamer->EmitIntValue(Size, PtrSize); |
Richard Mitton | 089ed89 | 2013-09-23 17:56:20 +0000 | [diff] [blame] | 1919 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1922 | Asm->OutStreamer->AddComment("ARange terminator"); |
| 1923 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
| 1924 | Asm->OutStreamer->EmitIntValue(0, PtrSize); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 1925 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 1928 | /// Emit address ranges into a debug ranges section. |
Devang Patel | 930143b | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1929 | void DwarfDebug::emitDebugRanges() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 1930 | if (CUMap.empty()) |
| 1931 | return; |
| 1932 | |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 1933 | if (!useRangesSection()) { |
| 1934 | assert(llvm::all_of( |
| 1935 | CUMap, |
| 1936 | [](const decltype(CUMap)::const_iterator::value_type &Pair) { |
| 1937 | return Pair.second->getRangeLists().empty(); |
| 1938 | }) && |
| 1939 | "No debug ranges expected."); |
| 1940 | return; |
| 1941 | } |
| 1942 | |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1943 | // Start the dwarf ranges section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1944 | Asm->OutStreamer->SwitchSection( |
Eric Christopher | b4bef6d | 2013-11-19 09:04:36 +0000 | [diff] [blame] | 1945 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Eric Christopher | 4751d70 | 2013-11-23 00:05:29 +0000 | [diff] [blame] | 1946 | |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1947 | // Size for our labels. |
Konstantin Zhuravlyov | dc77b2e | 2017-04-17 17:41:25 +0000 | [diff] [blame] | 1948 | unsigned char Size = Asm->MAI->getCodePointerSize(); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1949 | |
| 1950 | // Grab the specific ranges for the compile units in the module. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1951 | for (const auto &I : CUMap) { |
| 1952 | DwarfCompileUnit *TheCU = I.second; |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1953 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1954 | if (auto *Skel = TheCU->getSkeleton()) |
| 1955 | TheCU = Skel; |
| 1956 | |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1957 | // Iterate over the misc ranges for the compile units in the module. |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1958 | for (const RangeSpanList &List : TheCU->getRangeLists()) { |
Eric Christopher | f879064 | 2013-12-04 22:04:50 +0000 | [diff] [blame] | 1959 | // Emit our symbol so we can find the beginning of the range. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1960 | Asm->OutStreamer->EmitLabel(List.getSym()); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 1961 | |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1962 | // Gather all the ranges that apply to the same section so they can share |
| 1963 | // a base address entry. |
| 1964 | MapVector<const MCSection *, std::vector<const RangeSpan *>> MV; |
Benjamin Kramer | 15596c7 | 2014-03-07 19:09:39 +0000 | [diff] [blame] | 1965 | for (const RangeSpan &Range : List.getRanges()) { |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1966 | MV[&Range.getStart()->getSection()].push_back(&Range); |
| 1967 | } |
| 1968 | |
| 1969 | auto *CUBase = TheCU->getBaseAddress(); |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1970 | bool BaseIsSet = false; |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1971 | for (const auto &P : MV) { |
| 1972 | // Don't bother with a base address entry if there's only one range in |
| 1973 | // this section in this range list - for example ranges for a CU will |
| 1974 | // usually consist of single regions from each of many sections |
| 1975 | // (-ffunction-sections, or just C++ inline functions) except under LTO |
| 1976 | // or optnone where there may be holes in a single CU's section |
| 1977 | // contrubutions. |
| 1978 | auto *Base = CUBase; |
David Blaikie | 038e28a | 2017-07-31 21:48:42 +0000 | [diff] [blame] | 1979 | if (!Base && P.second.size() > 1 && |
| 1980 | UseDwarfRangesBaseAddressSpecifier) { |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1981 | BaseIsSet = true; |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1982 | // FIXME/use care: This may not be a useful base address if it's not |
| 1983 | // the lowest address/range in this object. |
| 1984 | Base = P.second.front()->getStart(); |
| 1985 | Asm->OutStreamer->EmitIntValue(-1, Size); |
| 1986 | Asm->OutStreamer->EmitSymbolValue(Base, Size); |
David Blaikie | 4dd6637 | 2017-07-31 00:18:24 +0000 | [diff] [blame] | 1987 | } else if (BaseIsSet) { |
| 1988 | BaseIsSet = false; |
| 1989 | Asm->OutStreamer->EmitIntValue(-1, Size); |
| 1990 | Asm->OutStreamer->EmitIntValue(0, Size); |
David Blaikie | 89c81a0 | 2017-07-30 22:10:00 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | for (const auto *RS : P.second) { |
| 1994 | const MCSymbol *Begin = RS->getStart(); |
| 1995 | const MCSymbol *End = RS->getEnd(); |
| 1996 | assert(Begin && "Range without a begin symbol?"); |
| 1997 | assert(End && "Range without an end symbol?"); |
| 1998 | if (Base) { |
| 1999 | Asm->EmitLabelDifference(Begin, Base, Size); |
| 2000 | Asm->EmitLabelDifference(End, Base, Size); |
| 2001 | } else { |
| 2002 | Asm->OutStreamer->EmitSymbolValue(Begin, Size); |
| 2003 | Asm->OutStreamer->EmitSymbolValue(End, Size); |
| 2004 | } |
Eric Christopher | ece0e90 | 2014-04-25 22:23:54 +0000 | [diff] [blame] | 2005 | } |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | // And terminate the list with two 0 values. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2009 | Asm->OutStreamer->EmitIntValue(0, Size); |
| 2010 | Asm->OutStreamer->EmitIntValue(0, Size); |
Eric Christopher | 0f63d06 | 2013-12-03 00:45:45 +0000 | [diff] [blame] | 2011 | } |
Devang Patel | 12563b3 | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 2012 | } |
Bill Wendling | 480ff32 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2015 | void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2016 | for (auto *MN : Nodes) { |
| 2017 | if (auto *M = dyn_cast<DIMacro>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2018 | emitMacro(*M); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2019 | else if (auto *F = dyn_cast<DIMacroFile>(MN)) |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2020 | emitMacroFile(*F, U); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2021 | else |
| 2022 | llvm_unreachable("Unexpected DI type!"); |
| 2023 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2026 | void DwarfDebug::emitMacro(DIMacro &M) { |
| 2027 | Asm->EmitULEB128(M.getMacinfoType()); |
| 2028 | Asm->EmitULEB128(M.getLine()); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2029 | StringRef Name = M.getName(); |
| 2030 | StringRef Value = M.getValue(); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2031 | Asm->OutStreamer->EmitBytes(Name); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2032 | if (!Value.empty()) { |
| 2033 | // There should be one space between macro name and macro value. |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2034 | Asm->EmitInt8(' '); |
| 2035 | Asm->OutStreamer->EmitBytes(Value); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2036 | } |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2037 | Asm->EmitInt8('\0'); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2040 | void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2041 | assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2042 | Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); |
| 2043 | Asm->EmitULEB128(F.getLine()); |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 2044 | Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile())); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2045 | handleMacroNodes(F.getElements(), U); |
| 2046 | Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Amjad Aboud | c077841 | 2016-01-24 08:18:55 +0000 | [diff] [blame] | 2049 | /// Emit macros into a debug macinfo section. |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2050 | void DwarfDebug::emitDebugMacinfo() { |
David Blaikie | 07963bd | 2017-05-26 18:52:56 +0000 | [diff] [blame] | 2051 | if (CUMap.empty()) |
| 2052 | return; |
| 2053 | |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 2054 | // Start the dwarf macinfo section. |
| 2055 | Asm->OutStreamer->SwitchSection( |
| 2056 | Asm->getObjFileLowering().getDwarfMacinfoSection()); |
| 2057 | |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2058 | for (const auto &P : CUMap) { |
| 2059 | auto &TheCU = *P.second; |
| 2060 | auto *SkCU = TheCU.getSkeleton(); |
| 2061 | DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; |
| 2062 | auto *CUNode = cast<DICompileUnit>(P.first); |
Alexey Bataev | bd786944 | 2018-02-22 16:20:30 +0000 | [diff] [blame] | 2063 | DIMacroNodeArray Macros = CUNode->getMacros(); |
| 2064 | if (!Macros.empty()) { |
| 2065 | Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); |
| 2066 | handleMacroNodes(Macros, U); |
| 2067 | } |
Amjad Aboud | d7cfb48 | 2016-01-07 14:28:20 +0000 | [diff] [blame] | 2068 | } |
| 2069 | Asm->OutStreamer->AddComment("End Of Macro List Mark"); |
| 2070 | Asm->EmitInt8(0); |
| 2071 | } |
| 2072 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2073 | // DWARF5 Experimental Separate Dwarf emitters. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2074 | |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2075 | void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2076 | std::unique_ptr<DwarfCompileUnit> NewU) { |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 2077 | NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name, |
David Blaikie | 96b1ed5 | 2017-04-21 23:35:26 +0000 | [diff] [blame] | 2078 | Asm->TM.Options.MCOptions.SplitDwarfFile); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2079 | |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2080 | if (!CompilationDir.empty()) |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 2081 | NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2082 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2083 | addGnuPubAttributes(*NewU, Die); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2084 | |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2085 | SkeletonHolder.addUnit(std::move(NewU)); |
David Blaikie | 38fe634 | 2014-01-09 04:28:46 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2088 | // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list, |
| 2089 | // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id, |
David Blaikie | 96dea05 | 2014-03-24 21:31:35 +0000 | [diff] [blame] | 2090 | // DW_AT_addr_base, DW_AT_ranges_base. |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2091 | DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2092 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2093 | auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( |
David Blaikie | bd57905 | 2014-04-28 21:14:27 +0000 | [diff] [blame] | 2094 | CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2095 | DwarfCompileUnit &NewCU = *OwnedUnit; |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2096 | NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); |
Eric Christopher | 4c7765f | 2013-01-17 03:00:04 +0000 | [diff] [blame] | 2097 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2098 | NewCU.initStmtList(); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2099 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2100 | if (useSegmentedStringOffsetsTable()) |
| 2101 | NewCU.addStringOffsetsStart(); |
| 2102 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2103 | initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); |
Eric Christopher | c8a310e | 2012-12-10 23:34:43 +0000 | [diff] [blame] | 2104 | |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2105 | return NewCU; |
| 2106 | } |
| 2107 | |
Eric Christopher | d692c1d | 2012-12-11 19:42:09 +0000 | [diff] [blame] | 2108 | // Emit the .debug_info.dwo section for separated dwarf. This contains the |
| 2109 | // compile units that would normally be in debug_info. |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2110 | void DwarfDebug::emitDebugInfoDWO() { |
Eric Christopher | cdf218d | 2012-12-10 19:51:21 +0000 | [diff] [blame] | 2111 | assert(useSplitDwarf() && "No split dwarf debug info?"); |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 2112 | // Don't emit relocations into the dwo file. |
| 2113 | InfoHolder.emitUnits(/* UseOffsets */ true); |
Eric Christopher | 3c5a191 | 2012-12-19 22:02:53 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the |
| 2117 | // abbreviations for the .debug_info.dwo section. |
| 2118 | void DwarfDebug::emitDebugAbbrevDWO() { |
| 2119 | assert(useSplitDwarf() && "No split dwarf?"); |
David Blaikie | 0504cda | 2013-12-05 07:43:55 +0000 | [diff] [blame] | 2120 | InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); |
Eric Christopher | 9c2ecd9 | 2012-11-30 23:59:06 +0000 | [diff] [blame] | 2121 | } |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2122 | |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2123 | void DwarfDebug::emitDebugLineDWO() { |
| 2124 | assert(useSplitDwarf() && "No split dwarf?"); |
Paul Robinson | 1e0116c | 2018-03-01 00:12:35 +0000 | [diff] [blame] | 2125 | if (!HasSplitTypeUnits) |
| 2126 | return; |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2127 | Asm->OutStreamer->SwitchSection( |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2128 | Asm->getObjFileLowering().getDwarfLineDWOSection()); |
Frederic Riss | a5ab844 | 2015-08-07 15:14:08 +0000 | [diff] [blame] | 2129 | SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams()); |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2132 | void DwarfDebug::emitStringOffsetsTableHeaderDWO() { |
| 2133 | assert(useSplitDwarf() && "No split dwarf?"); |
| 2134 | InfoHolder.emitStringOffsetsTableHeader( |
| 2135 | Asm->getObjFileLowering().getDwarfStrOffDWOSection()); |
| 2136 | } |
| 2137 | |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2138 | // Emit the .debug_str.dwo section for separated dwarf. This contains the |
| 2139 | // string section and is identical in format to traditional .debug_str |
| 2140 | // sections. |
| 2141 | void DwarfDebug::emitDebugStrDWO() { |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2142 | if (useSegmentedStringOffsetsTable()) |
| 2143 | emitStringOffsetsTableHeaderDWO(); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2144 | assert(useSplitDwarf() && "No split dwarf?"); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 2145 | MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); |
Eric Christopher | 2cbd576 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 2146 | InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2147 | OffSec, /* UseRelativeOffsets = */ false); |
Eric Christopher | 3bf29fd | 2012-12-27 02:14:01 +0000 | [diff] [blame] | 2148 | } |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2149 | |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2150 | MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { |
| 2151 | if (!useSplitDwarf()) |
| 2152 | return nullptr; |
Alexander Kornienko | e12a48b | 2018-03-07 16:27:44 +0000 | [diff] [blame] | 2153 | if (SingleCU) |
| 2154 | SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory()); |
David Blaikie | 47f4b82 | 2014-03-19 00:11:28 +0000 | [diff] [blame] | 2155 | return &SplitTypeUnitFileTable; |
| 2156 | } |
| 2157 | |
Adrian Prantl | ee5feaf | 2015-07-15 17:01:41 +0000 | [diff] [blame] | 2158 | uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2159 | MD5 Hash; |
| 2160 | Hash.update(Identifier); |
| 2161 | // ... take the least significant 8 bytes and return those. Our MD5 |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2162 | // implementation always returns its results in little endian, so we actually |
| 2163 | // need the "high" word. |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2164 | MD5::MD5Result Result; |
| 2165 | Hash.final(Result); |
Zachary Turner | 82a0c97 | 2017-03-20 23:33:18 +0000 | [diff] [blame] | 2166 | return Result.high(); |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
David Blaikie | 15632ae | 2014-02-12 00:31:30 +0000 | [diff] [blame] | 2169 | void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, |
David Blaikie | 65a7466 | 2014-04-25 18:26:14 +0000 | [diff] [blame] | 2170 | StringRef Identifier, DIE &RefDie, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2171 | const DICompositeType *CTy) { |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2172 | // Fast path if we're building some type units and one has already used the |
| 2173 | // address pool we know we're going to throw away all this work anyway, so |
| 2174 | // don't bother building dependent types. |
| 2175 | if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) |
| 2176 | return; |
| 2177 | |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2178 | auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); |
| 2179 | if (!Ins.second) { |
| 2180 | CU.addDIETypeSignature(RefDie, Ins.first->second); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2181 | return; |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2184 | bool TopLevelType = TypeUnitsUnderConstruction.empty(); |
| 2185 | AddrPool.resetUsedFlag(); |
| 2186 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 2187 | auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, |
| 2188 | getDwoLineTable(CU)); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2189 | DwarfTypeUnit &NewTU = *OwnedUnit; |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2190 | DIE &UnitDie = NewTU.getUnitDie(); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 2191 | TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2192 | |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2193 | NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
David Blaikie | 637cac4 | 2014-04-22 23:09:36 +0000 | [diff] [blame] | 2194 | CU.getLanguage()); |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2195 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2196 | uint64_t Signature = makeTypeSignature(Identifier); |
David Blaikie | f9b6a55 | 2014-04-22 22:39:41 +0000 | [diff] [blame] | 2197 | NewTU.setTypeSignature(Signature); |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2198 | Ins.first->second = Signature; |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2199 | |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2200 | if (useSplitDwarf()) |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2201 | NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection()); |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2202 | else { |
David Blaikie | 92a2f8a | 2014-04-28 21:04:29 +0000 | [diff] [blame] | 2203 | CU.applyStmtList(UnitDie); |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 2204 | NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature)); |
David Blaikie | 29459ae | 2014-07-25 17:11:58 +0000 | [diff] [blame] | 2205 | } |
Chandler Carruth | b587ab6 | 2014-01-20 08:07:07 +0000 | [diff] [blame] | 2206 | |
Wolfgang Pieb | 456b555 | 2018-01-26 18:52:58 +0000 | [diff] [blame] | 2207 | // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type |
| 2208 | // units. |
| 2209 | if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) |
| 2210 | NewTU.addStringOffsetsStart(); |
| 2211 | |
David Blaikie | f3de2ab | 2014-04-26 16:26:41 +0000 | [diff] [blame] | 2212 | NewTU.setType(NewTU.createTypeDIE(CTy)); |
| 2213 | |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2214 | if (TopLevelType) { |
| 2215 | auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); |
| 2216 | TypeUnitsUnderConstruction.clear(); |
| 2217 | |
| 2218 | // Types referencing entries in the address table cannot be placed in type |
| 2219 | // units. |
| 2220 | if (AddrPool.hasBeenUsed()) { |
| 2221 | |
| 2222 | // Remove all the types built while building this type. |
| 2223 | // This is pessimistic as some of these types might not be dependent on |
| 2224 | // the type that used an address. |
| 2225 | for (const auto &TU : TypeUnitsToAdd) |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2226 | TypeSignatures.erase(TU.second); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2227 | |
| 2228 | // Construct this type in the CU directly. |
| 2229 | // This is inefficient because all the dependent types will be rebuilt |
| 2230 | // from scratch, including building them in type units, discovering that |
| 2231 | // they depend on addresses, throwing them out and rebuilding them. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 2232 | CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2233 | return; |
| 2234 | } |
| 2235 | |
| 2236 | // If the type wasn't dependent on fission addresses, finish adding the type |
| 2237 | // and all its dependent types. |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2238 | for (auto &TU : TypeUnitsToAdd) { |
| 2239 | InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); |
| 2240 | InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); |
| 2241 | } |
Paul Robinson | 1e0116c | 2018-03-01 00:12:35 +0000 | [diff] [blame] | 2242 | HasSplitTypeUnits = useSplitDwarf(); |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 2243 | } |
Peter Collingbourne | 7c384cc | 2016-02-11 19:57:46 +0000 | [diff] [blame] | 2244 | CU.addDIETypeSignature(RefDie, Signature); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 2245 | } |
David Blaikie | 4bd13b7 | 2014-03-07 18:49:45 +0000 | [diff] [blame] | 2246 | |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2247 | // Accelerator table mutators - add each name along with its companion |
| 2248 | // DIE to the proper table while ensuring that the name that we're going |
| 2249 | // to reference is in the string table. We do this since the names we |
| 2250 | // add may not only be identical to the names in the DIE. |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2251 | void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) { |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2252 | if (!useDwarfAccelTables()) |
| 2253 | return; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 2254 | AccelNames.addName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 2406a062 | 2014-04-23 23:37:35 +0000 | [diff] [blame] | 2255 | } |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2256 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2257 | void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) { |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2258 | if (!useDwarfAccelTables()) |
| 2259 | return; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 2260 | AccelObjC.addName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 0ee82b9 | 2014-04-24 00:53:32 +0000 | [diff] [blame] | 2261 | } |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2262 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2263 | void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) { |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2264 | if (!useDwarfAccelTables()) |
| 2265 | return; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 2266 | AccelNamespace.addName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | ecf0415 | 2014-04-24 01:02:42 +0000 | [diff] [blame] | 2267 | } |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2268 | |
David Blaikie | b0b3fcf | 2014-04-25 18:52:29 +0000 | [diff] [blame] | 2269 | void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) { |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2270 | if (!useDwarfAccelTables()) |
| 2271 | return; |
Jonas Devlieghere | e699dfa | 2018-01-29 14:52:34 +0000 | [diff] [blame] | 2272 | AccelTypes.addName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die); |
David Blaikie | 18d3375 | 2014-04-24 01:23:49 +0000 | [diff] [blame] | 2273 | } |
Greg Clayton | e654397 | 2016-11-23 23:30:37 +0000 | [diff] [blame] | 2274 | |
| 2275 | uint16_t DwarfDebug::getDwarfVersion() const { |
| 2276 | return Asm->OutStreamer->getContext().getDwarfVersion(); |
| 2277 | } |